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