1 /* 2 * Copyright (c) 2019 Apple Inc. All rights reserved. 3 * 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. The rights granted to you under the License 10 * may not be used to create, or enable the creation or redistribution of, 11 * unlawful or unlicensed copies of an Apple operating system, or to 12 * circumvent, violate, or enable the circumvention or violation of, any 13 * terms of an Apple operating system software license agreement. 14 * 15 * Please obtain a copy of the License at 16 * http://www.opensource.apple.com/apsl/ and read it before using this file. 17 * 18 * The Original Code and all software distributed under the License are 19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23 * Please see the License for the specific language governing rights and 24 * limitations under the License. 25 * 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27 */ 28 29 #ifndef _REASON_H_ 30 #define _REASON_H_ 31 32 #include <stdint.h> 33 34 __BEGIN_DECLS 35 36 #ifdef KERNEL_PRIVATE 37 38 #include <kern/kern_cdata.h> 39 40 #ifdef XNU_KERNEL_PRIVATE 41 #include <os/refcnt.h> 42 #include <kern/locks.h> 43 44 typedef struct os_reason { 45 decl_lck_mtx_data(, osr_lock); 46 os_refcnt_t osr_refcount; 47 uint32_t osr_namespace; 48 uint64_t osr_code; 49 uint64_t osr_flags; 50 uint32_t osr_bufsize; 51 struct kcdata_descriptor osr_kcd_descriptor; 52 char *osr_kcd_buf; 53 } *os_reason_t; 54 55 #define OS_REASON_NULL ((os_reason_t) NULL) 56 57 /* We only include 800 bytes of the exit reason description to not blow through the panic buffer */ 58 #define LAUNCHD_PANIC_REASON_STRING_MAXLEN "800" 59 60 void os_reason_init(void); 61 62 os_reason_t build_userspace_exit_reason(uint32_t reason_namespace, uint64_t reason_code, user_addr_t payload, uint32_t payload_size, 63 user_addr_t reason_string, uint64_t reason_flags); 64 char *exit_reason_get_string_desc(os_reason_t exit_reason); 65 66 /* The blocking allocation is currently not exported to KEXTs */ 67 int os_reason_alloc_buffer(os_reason_t cur_reason, uint32_t osr_bufsize); 68 69 typedef struct _exception_info { 70 int os_reason; 71 int signal; 72 exception_type_t exception_type; 73 mach_exception_data_type_t mx_code; 74 mach_exception_data_type_t mx_subcode; 75 struct kt_info { 76 int kt_subsys; 77 uint32_t kt_error; 78 } kt_info; 79 } exception_info_t; 80 81 #define PX_FLAGS_NONE 0 82 /* think twice about userspace debugging experience before using PX_DEBUG_NO_HONOR */ 83 #define PX_DEBUG_NO_HONOR (1 << 0) /* force exit even when debugging */ 84 #define PX_KTRIAGE (1 << 1) /* leave a ktriage record */ 85 #define PX_PSIGNAL (1 << 2) /* send sig instead of forced exit */ 86 #define PX_NO_EXCEPTION_UTHREAD (1 << 3) /* do not set bsdthread exception */ 87 88 int exit_with_mach_exception(struct proc *p, exception_info_t exception, uint32_t flags); 89 #if CONFIG_EXCLAVES 90 int exit_with_exclave_exception(struct proc *p, exception_info_t exception, uint32_t flags); 91 #endif 92 93 #else /* XNU_KERNEL_PRIVATE */ 94 95 typedef void * os_reason_t; 96 97 #endif /* XNU_KERNEL_PRIVATE */ 98 99 os_reason_t os_reason_create(uint32_t osr_namespace, uint64_t osr_code); 100 int os_reason_alloc_buffer_noblock(os_reason_t cur_reason, uint32_t osr_bufsize); 101 struct kcdata_descriptor * os_reason_get_kcdata_descriptor(os_reason_t cur_reason); 102 void os_reason_ref(os_reason_t cur_reason); 103 void os_reason_free(os_reason_t cur_reason); 104 void os_reason_set_flags(os_reason_t cur_reason, uint64_t flags); 105 void os_reason_set_description_data(os_reason_t cur_reason, uint32_t type, void *reason_data, uint32_t reason_data_len); 106 #endif /* KERNEL_PRIVATE */ 107 108 /* 109 * Reason namespaces. 110 */ 111 #define OS_REASON_INVALID 0 112 #define OS_REASON_JETSAM 1 113 #define OS_REASON_SIGNAL 2 114 #define OS_REASON_CODESIGNING 3 115 #define OS_REASON_HANGTRACER 4 116 #define OS_REASON_TEST 5 117 #define OS_REASON_DYLD 6 118 #define OS_REASON_LIBXPC 7 119 #define OS_REASON_OBJC 8 120 #define OS_REASON_EXEC 9 121 #define OS_REASON_SPRINGBOARD 10 122 #define OS_REASON_TCC 11 123 #define OS_REASON_REPORTCRASH 12 124 #define OS_REASON_COREANIMATION 13 125 #define OS_REASON_AGGREGATED 14 126 #define OS_REASON_RUNNINGBOARD 15 127 #define OS_REASON_ASSERTIOND OS_REASON_RUNNINGBOARD /* old name */ 128 #define OS_REASON_SKYWALK 16 129 #define OS_REASON_SETTINGS 17 130 #define OS_REASON_LIBSYSTEM 18 131 #define OS_REASON_FOUNDATION 19 132 #define OS_REASON_WATCHDOG 20 133 #define OS_REASON_METAL 21 134 #define OS_REASON_WATCHKIT 22 135 #define OS_REASON_GUARD 23 136 #define OS_REASON_ANALYTICS 24 137 #define OS_REASON_SANDBOX 25 138 #define OS_REASON_SECURITY 26 139 #define OS_REASON_ENDPOINTSECURITY 27 140 #define OS_REASON_PAC_EXCEPTION 28 141 #define OS_REASON_BLUETOOTH_CHIP 29 142 #define OS_REASON_PORT_SPACE 30 143 #define OS_REASON_WEBKIT 31 144 #define OS_REASON_BACKLIGHTSERVICES 32 145 #define OS_REASON_MEDIA 33 146 #define OS_REASON_ROSETTA 34 147 #define OS_REASON_LIBIGNITION 35 148 #define OS_REASON_BOOTMOUNT 36 149 150 151 #define OS_REASON_REALITYKIT 38 152 #define OS_REASON_AUDIO 39 153 #define OS_REASON_WAKEBOARD 40 154 #define OS_REASON_CORERC 41 155 #define OS_REASON_SELF_RESTRICT 42 156 157 /* 158 * Update whenever new OS_REASON namespaces are added. 159 */ 160 #define OS_REASON_MAX_VALID_NAMESPACE OS_REASON_SELF_RESTRICT 161 162 #define OS_REASON_BUFFER_MAX_SIZE 5120 163 164 #define OS_REASON_FLAG_NO_CRASH_REPORT 0x1 /* Don't create a crash report */ 165 #define OS_REASON_FLAG_GENERATE_CRASH_REPORT 0x2 /* Create a crash report - the default for userspace requests */ 166 #define OS_REASON_FLAG_FROM_USERSPACE 0x4 /* Reason created from a userspace syscall */ 167 #define OS_REASON_FLAG_FAILED_DATA_COPYIN 0x8 /* We failed to copyin data from userspace */ 168 #define OS_REASON_FLAG_PAYLOAD_TRUNCATED 0x10 /* The payload was truncated because it was longer than allowed */ 169 #define OS_REASON_FLAG_BAD_PARAMS 0x20 /* Invalid parameters were passed involved with creating this reason */ 170 #define OS_REASON_FLAG_CONSISTENT_FAILURE 0x40 /* Whatever caused this reason to be created will happen again */ 171 #define OS_REASON_FLAG_ONE_TIME_FAILURE 0x80 /* Whatever caused this reason to be created was a one time issue */ 172 #define OS_REASON_FLAG_NO_CRASHED_TID 0x100 /* Don't include the TID that processed the exit in the crash report */ 173 #define OS_REASON_FLAG_ABORT 0x200 /* Reason created from abort_* rather than terminate_* */ 174 #define OS_REASON_FLAG_SHAREDREGION_FAULT 0x400 /* Fault happened within the shared cache region */ 175 #define OS_REASON_FLAG_CAPTURE_LOGS 0x800 /* The report generated for this reason should capture logs */ 176 #define OS_REASON_FLAG_SECURITY_SENSITIVE 0x1000 /* Mark as security sensitive for priority treatment */ 177 178 /* 179 * Set of flags that are allowed to be passed from userspace 180 */ 181 #define OS_REASON_FLAG_MASK_ALLOWED_FROM_USER (OS_REASON_FLAG_CONSISTENT_FAILURE | OS_REASON_FLAG_ONE_TIME_FAILURE | OS_REASON_FLAG_NO_CRASH_REPORT | OS_REASON_FLAG_ABORT | OS_REASON_FLAG_CAPTURE_LOGS | OS_REASON_FLAG_SECURITY_SENSITIVE) 182 183 /* 184 * Macros to encode the exit reason namespace and first 32 bits of code in exception code 185 * which is used by Report Crash as a hint. It should be only used as a hint since it 186 * loses higher 32 bits of exit reason code. 187 */ 188 #define ENCODE_OSR_NAMESPACE_TO_MACH_EXCEPTION_CODE(code, osr_namespace) \ 189 (code) = (code) | (((osr_namespace) & ((uint64_t)UINT32_MAX)) << 32) 190 #define ENCODE_OSR_CODE_TO_MACH_EXCEPTION_CODE(code, osr_code) \ 191 (code) = (code) | ((osr_code) & ((uint64_t)UINT32_MAX)) 192 193 #ifndef KERNEL 194 /* 195 * abort_with_reason: Used to exit the current process and pass along 196 * specific information about why it is being terminated. 197 * 198 * Inputs: args->reason_namespace - OS_REASON namespace specified for the reason 199 * args->reason_code - code in the specified namespace for the reason 200 * args->reason_string - additional string formatted information about the request 201 * args->reason_flags - options requested for how the process should be terminated (see OS_REASON_FLAG_* above). 202 * 203 * Outputs: Does not return. 204 */ 205 void abort_with_reason(uint32_t reason_namespace, uint64_t reason_code, const char *reason_string, uint64_t reason_flags) 206 __attribute__((noreturn, cold)); 207 208 /* 209 * abort_with_payload: Used to exit the current process and pass along 210 * specific information about why it is being terminated. The payload pointer 211 * should point to structured data that can be interpreted by the consumer of 212 * exit reason information. 213 * 214 * Inputs: args->reason_namespace - OS_REASON namespace specified for the reason 215 * args->reason_code - code in the specified namespace for the reason 216 * args->payload - pointer to payload structure in user space 217 * args->payload_size - length of payload buffer (this will be truncated to EXIT_REASON_PAYLOAD_MAX_LEN) 218 * args->reason_string - additional string formatted information about the request 219 * args->reason_flags - options requested for how the process should be terminated (see OS_REASON_FLAG_* above). 220 * 221 * Outputs: Does not return. 222 */ 223 void abort_with_payload(uint32_t reason_namespace, uint64_t reason_code, void *payload, uint32_t payload_size, const char *reason_string, 224 uint64_t reason_flags) __attribute__((noreturn, cold)); 225 226 /* 227 * terminate_with_reason: Used to terminate a specific process and pass along 228 * specific information about why it is being terminated. 229 * 230 * Inputs: args->pid - the PID of the process to be terminated 231 * args->reason_namespace - OS_REASON namespace specified for the reason 232 * args->reason_code - code in the specified namespace for the reason 233 * args->reason_string - additional string formatted information about the request 234 * args->reason_flags - options requested for how the process should be terminated (see OS_REASON_FLAG_* above) 235 * 236 * Outputs: returns -1 and sets errno to EINVAL if the PID requested is the same as that of the calling process, invalid or the namespace provided is invalid. 237 * returns -1 and sets errno to ESRCH if we couldn't find a live process with the requested PID 238 * returns -1 and sets errno to EPERM if the caller is not privileged enough to kill the process with the requested PID 239 * returns 0 otherwise 240 */ 241 int terminate_with_reason(int pid, uint32_t reason_namespace, uint64_t reason_code, const char *reason_string, uint64_t reason_flags); 242 243 /* 244 * terminate_with_payload: Used to terminate a specific process and pass along 245 * specific information about why it is being terminated. The payload pointer 246 * should point to structured data that can be interpreted by the consumer of 247 * exit reason information. 248 * 249 * Inputs: args->pid - the PID of the process to be terminated. 250 * args->reason_namespace - OS_REASON namespace specified for the reason 251 * args->reason_code - code in the specified namespace for the reason 252 * args->payload - pointer to payload structure in user space 253 * args->payload_size - length of payload buffer (this will be truncated to EXIT_REASON_PAYLOAD_MAX_LEN) 254 * args->reason_string - additional string formatted information about the request 255 * args->reason_flags - options requested for how the process should be terminated (see OS_REASON_FLAG_* above) 256 * 257 * Outputs: returns -1 and sets errno to EINVAL if the PID requested is the same as that of the calling process, is invalid or the namespace provided is invalid. 258 * returns -1 and sets errno to ESRCH if we couldn't find a live process with the requested PID 259 * returns -1 and sets errno to EPERM if the caller is not privileged enough to kill the process with the requested PID 260 * returns 0 otherwise 261 */ 262 int terminate_with_payload(int pid, uint32_t reason_namespace, uint64_t reason_code, void *payload, uint32_t payload_size, 263 const char *reason_string, uint64_t reason_flags); 264 #endif /* KERNEL */ 265 266 /* 267 * codesigning exit reasons 268 */ 269 #define CODESIGNING_EXIT_REASON_TASKGATED_INVALID_SIG 1 270 #define CODESIGNING_EXIT_REASON_INVALID_PAGE 2 271 #define CODESIGNING_EXIT_REASON_TASK_ACCESS_PORT 3 272 #define CODESIGNING_EXIT_REASON_LAUNCH_CONSTRAINT_VIOLATION 4 273 /* 274 * exec path specific exit reasons 275 */ 276 #define EXEC_EXIT_REASON_BAD_MACHO 1 277 #define EXEC_EXIT_REASON_SUGID_FAILURE 2 278 #define EXEC_EXIT_REASON_ACTV_THREADSTATE 3 279 #define EXEC_EXIT_REASON_STACK_ALLOC 4 280 #define EXEC_EXIT_REASON_APPLE_STRING_INIT 5 281 #define EXEC_EXIT_REASON_COPYOUT_STRINGS 6 282 #define EXEC_EXIT_REASON_COPYOUT_DYNLINKER 7 283 #define EXEC_EXIT_REASON_SECURITY_POLICY 8 284 #define EXEC_EXIT_REASON_TASKGATED_OTHER 9 285 #define EXEC_EXIT_REASON_FAIRPLAY_DECRYPT 10 286 #define EXEC_EXIT_REASON_DECRYPT 11 287 #define EXEC_EXIT_REASON_UPX 12 288 #define EXEC_EXIT_REASON_NO32EXEC 13 289 #define EXEC_EXIT_REASON_WRONG_PLATFORM 14 290 #define EXEC_EXIT_REASON_MAIN_FD_ALLOC 15 291 #define EXEC_EXIT_REASON_COPYOUT_ROSETTA 16 292 #define EXEC_EXIT_REASON_SET_DYLD_INFO 17 293 #define EXEC_EXIT_REASON_MACHINE_THREAD 18 294 #define EXEC_EXIT_REASON_BAD_PSATTR 19 295 /* 296 * guard reasons 297 */ 298 #define GUARD_REASON_VNODE 1 299 #define GUARD_REASON_VIRT_MEMORY 2 300 #define GUARD_REASON_MACH_PORT 3 301 #define GUARD_REASON_EXCLAVES 4 302 #define GUARD_REASON_JIT 5 303 304 __END_DECLS 305 306 #endif /* _REASON_H_ */ 307