reason.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright (c) 2016 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. #ifndef _REASON_H_
  29. #define _REASON_H_
  30. #include <stdint.h>
  31. __BEGIN_DECLS
  32. #ifdef KERNEL_PRIVATE
  33. #include <kern/kern_cdata.h>
  34. #ifdef XNU_KERNEL_PRIVATE
  35. #include <kern/locks.h>
  36. typedef struct os_reason {
  37. decl_lck_mtx_data(, osr_lock)
  38. unsigned int osr_refcount;
  39. uint32_t osr_namespace;
  40. uint64_t osr_code;
  41. uint64_t osr_flags;
  42. uint32_t osr_bufsize;
  43. struct kcdata_descriptor osr_kcd_descriptor;
  44. char *osr_kcd_buf;
  45. } *os_reason_t;
  46. #define OS_REASON_NULL ((os_reason_t) 0)
  47. /* We only include 800 bytes of the exit reason description to not blow through the panic buffer */
  48. #define LAUNCHD_PANIC_REASON_STRING_MAXLEN "800"
  49. void os_reason_init(void);
  50. os_reason_t build_userspace_exit_reason(uint32_t reason_namespace, uint64_t reason_code, user_addr_t payload, uint32_t payload_size,
  51. user_addr_t reason_string, uint64_t reason_flags);
  52. char *launchd_exit_reason_get_string_desc(os_reason_t exit_reason);
  53. /* The blocking allocation is currently not exported to KEXTs */
  54. int os_reason_alloc_buffer(os_reason_t cur_reason, uint32_t osr_bufsize);
  55. #else /* XNU_KERNEL_PRIVATE */
  56. typedef void * os_reason_t;
  57. #endif /* XNU_KERNEL_PRIVATE */
  58. os_reason_t os_reason_create(uint32_t osr_namespace, uint64_t osr_code);
  59. int os_reason_alloc_buffer_noblock(os_reason_t cur_reason, uint32_t osr_bufsize);
  60. struct kcdata_descriptor * os_reason_get_kcdata_descriptor(os_reason_t cur_reason);
  61. void os_reason_ref(os_reason_t cur_reason);
  62. void os_reason_free(os_reason_t cur_reason);
  63. #endif /* KERNEL_PRIVATE */
  64. /*
  65. * Reason namespaces.
  66. */
  67. #define OS_REASON_INVALID 0
  68. #define OS_REASON_JETSAM 1
  69. #define OS_REASON_SIGNAL 2
  70. #define OS_REASON_CODESIGNING 3
  71. #define OS_REASON_HANGTRACER 4
  72. #define OS_REASON_TEST 5
  73. #define OS_REASON_DYLD 6
  74. #define OS_REASON_LIBXPC 7
  75. #define OS_REASON_OBJC 8
  76. #define OS_REASON_EXEC 9
  77. #define OS_REASON_SPRINGBOARD 10
  78. #define OS_REASON_TCC 11
  79. #define OS_REASON_REPORTCRASH 12
  80. #define OS_REASON_COREANIMATION 13
  81. #define OS_REASON_AGGREGATED 14
  82. #define OS_REASON_ASSERTIOND 15
  83. #define OS_REASON_SKYWALK 16
  84. #define OS_REASON_SETTINGS 17
  85. #define OS_REASON_LIBSYSTEM 18
  86. #define OS_REASON_FOUNDATION 19
  87. #define OS_REASON_WATCHDOG 20
  88. #define OS_REASON_METAL 21
  89. #define OS_REASON_WATCHKIT 22
  90. #define OS_REASON_GUARD 23
  91. #define OS_REASON_ANALYTICS 24
  92. /*
  93. * Update whenever new OS_REASON namespaces are added.
  94. */
  95. #define OS_REASON_MAX_VALID_NAMESPACE OS_REASON_ANALYTICS
  96. #define OS_REASON_BUFFER_MAX_SIZE 5120
  97. #define OS_REASON_FLAG_NO_CRASH_REPORT 0x1 /* Don't create a crash report */
  98. #define OS_REASON_FLAG_GENERATE_CRASH_REPORT 0x2 /* Create a crash report - the default for userspace requests */
  99. #define OS_REASON_FLAG_FROM_USERSPACE 0x4 /* Reason created from a userspace syscall */
  100. #define OS_REASON_FLAG_FAILED_DATA_COPYIN 0x8 /* We failed to copyin data from userspace */
  101. #define OS_REASON_FLAG_PAYLOAD_TRUNCATED 0x10 /* The payload was truncated because it was longer than allowed */
  102. #define OS_REASON_FLAG_BAD_PARAMS 0x20 /* Invalid parameters were passed involved with creating this reason */
  103. #define OS_REASON_FLAG_CONSISTENT_FAILURE 0x40 /* Whatever caused this reason to be created will happen again */
  104. #define OS_REASON_FLAG_ONE_TIME_FAILURE 0x80 /* Whatever caused this reason to be created was a one time issue */
  105. #define OS_REASON_FLAG_NO_CRASHED_TID 0x100 /* Don't include the TID that processed the exit in the crash report */
  106. #define OS_REASON_FLAG_ABORT 0x200 /* Reason created from abort_* rather than terminate_* */
  107. /*
  108. * Set of flags that are allowed to be passed from userspace
  109. */
  110. #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)
  111. /*
  112. * Macros to encode the exit reason namespace and first 32 bits of code in exception code
  113. * which is used by Report Crash as a hint. It should be only used as a hint since it
  114. * looses higher 32 bits of exit reason code.
  115. */
  116. #define ENCODE_OSR_NAMESPACE_TO_MACH_EXCEPTION_CODE(code, osr_namespace) \
  117. (code) = (code) | (((osr_namespace) & ((uint64_t)UINT32_MAX)) << 32)
  118. #define ENCODE_OSR_CODE_TO_MACH_EXCEPTION_CODE(code, osr_code) \
  119. (code) = (code) | ((osr_code) & ((uint64_t)UINT32_MAX))
  120. #ifndef KERNEL
  121. /*
  122. * abort_with_reason: Used to exit the current process and pass along
  123. * specific information about why it is being terminated.
  124. *
  125. * Inputs: args->reason_namespace - OS_REASON namespace specified for the reason
  126. * args->reason_code - code in the specified namespace for the reason
  127. * args->reason_string - additional string formatted information about the request
  128. * args->reason_flags - options requested for how the process should be terminated (see OS_REASON_FLAG_* above).
  129. *
  130. * Outputs: Does not return.
  131. */
  132. void abort_with_reason(uint32_t reason_namespace, uint64_t reason_code, const char *reason_string, uint64_t reason_flags) __attribute__((noreturn));
  133. /*
  134. * abort_with_payload: Used to exit the current process and pass along
  135. * specific information about why it is being terminated. The payload pointer
  136. * should point to structured data that can be interpreted by the consumer of
  137. * exit reason information.
  138. *
  139. * Inputs: args->reason_namespace - OS_REASON namespace specified for the reason
  140. * args->reason_code - code in the specified namespace for the reason
  141. * args->payload - pointer to payload structure in user space
  142. * args->payload_size - length of payload buffer (this will be truncated to EXIT_REASON_PAYLOAD_MAX_LEN)
  143. * args->reason_string - additional string formatted information about the request
  144. * args->reason_flags - options requested for how the process should be terminated (see OS_REASON_FLAG_* above).
  145. *
  146. * Outputs: Does not return.
  147. */
  148. void abort_with_payload(uint32_t reason_namespace, uint64_t reason_code, void *payload, uint32_t payload_size, const char *reason_string,
  149. uint64_t reason_flags) __attribute__((noreturn));
  150. /*
  151. * terminate_with_reason: Used to terminate a specific process and pass along
  152. * specific information about why it is being terminated.
  153. *
  154. * Inputs: args->pid - the PID of the process to be terminated
  155. * args->reason_namespace - OS_REASON namespace specified for the reason
  156. * args->reason_code - code in the specified namespace for the reason
  157. * args->reason_string - additional string formatted information about the request
  158. * args->reason_flags - options requested for how the process should be terminated (see OS_REASON_FLAG_* above)
  159. *
  160. * Outputs: EINVAL if the PID requested is the same as that of the calling process, invalid or the namespace provided is invalid.
  161. * ESRCH if we couldn't find a live process with the requested PID
  162. * EPERM if the caller is not privileged enough to kill the process with the requested PID
  163. * returns 0 otherwise
  164. */
  165. int terminate_with_reason(int pid, uint32_t reason_namespace, uint64_t reason_code, const char *reason_string, uint64_t reason_flags);
  166. /*
  167. * terminate_with_payload: Used to terminate a specific process and pass along
  168. * specific information about why it is being terminated. The payload pointer
  169. * should point to structured data that can be interpreted by the consumer of
  170. * exit reason information.
  171. *
  172. * Inputs: args->pid - the PID of the process to be terminated.
  173. * args->reason_namespace - OS_REASON namespace specified for the reason
  174. * args->reason_code - code in the specified namespace for the reason
  175. * args->payload - pointer to payload structure in user space
  176. * args->payload_size - length of payload buffer (this will be truncated to EXIT_REASON_PAYLOAD_MAX_LEN)
  177. * args->reason_string - additional string formatted information about the request
  178. * args->reason_flags - options requested for how the process should be terminated (see OS_REASON_FLAG_* above)
  179. *
  180. * Outputs: EINVAL if the PID requested is the same as that of the calling process, is invalid or the namespace provided is invalid.
  181. * ESRCH if we couldn't find a live process with the requested PID
  182. * EPERM if the caller is not privileged enough to kill the process with the requested PID
  183. * returns 0 otherwise
  184. */
  185. int terminate_with_payload(int pid, uint32_t reason_namespace, uint64_t reason_code, void *payload, uint32_t payload_size,
  186. const char *reason_string, uint64_t reason_flags);
  187. #endif /* KERNEL */
  188. /*
  189. * codesigning exit reasons
  190. */
  191. #define CODESIGNING_EXIT_REASON_TASKGATED_INVALID_SIG 1
  192. #define CODESIGNING_EXIT_REASON_INVALID_PAGE 2
  193. #define CODESIGNING_EXIT_REASON_TASK_ACCESS_PORT 3
  194. /*
  195. * exec path specific exit reasons
  196. */
  197. #define EXEC_EXIT_REASON_BAD_MACHO 1
  198. #define EXEC_EXIT_REASON_SUGID_FAILURE 2
  199. #define EXEC_EXIT_REASON_ACTV_THREADSTATE 3
  200. #define EXEC_EXIT_REASON_STACK_ALLOC 4
  201. #define EXEC_EXIT_REASON_APPLE_STRING_INIT 5
  202. #define EXEC_EXIT_REASON_COPYOUT_STRINGS 6
  203. #define EXEC_EXIT_REASON_COPYOUT_DYNLINKER 7
  204. #define EXEC_EXIT_REASON_SECURITY_POLICY 8
  205. #define EXEC_EXIT_REASON_TASKGATED_OTHER 9
  206. #define EXEC_EXIT_REASON_FAIRPLAY_DECRYPT 10
  207. #define EXEC_EXIT_REASON_DECRYPT 11
  208. #define EXEC_EXIT_REASON_UPX 12
  209. #define EXEC_EXIT_REASON_NO32EXEC 13
  210. /*
  211. * guard reasons
  212. */
  213. #define GUARD_REASON_VNODE 1
  214. #define GUARD_REASON_VIRT_MEMORY 2
  215. #define GUARD_REASON_MACH_PORT 3
  216. __END_DECLS
  217. #endif /* _REASON_H_ */