xref: /xnu-8020.140.41/osfmk/kern/ux_handler.c (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
1 /*
2  * Copyright (c) 2017 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 #include <kern/ux_handler.h>
30 #include <sys/ux_exception.h>
31 
32 #include <mach/exception.h>
33 #include <mach/kern_return.h>
34 #include <mach/port.h>
35 #include <mach/mach_port.h>
36 #include <mach/mig_errors.h>
37 
38 #include <kern/thread.h>
39 #include <kern/task.h>
40 #include <kern/ipc_kobject.h>
41 #include <kern/ipc_tt.h>
42 
43 #include <ipc/ipc_port.h>
44 
45 #include <mach/host_priv.h>
46 #include <kern/host.h>
47 
48 #include <mach/exc_server.h>
49 #include <mach/mach_exc_server.h>
50 
51 #include <libkern/section_keywords.h>
52 
53 /*
54  * Mach kobject port to reflect Mach exceptions into Unix signals.
55  *
56  * This is the default Mach exception handler for initproc, which
57  * then filters to all subprocesses as the host level exception handler for
58  * most Mach exceptions.
59  */
60 
61 static SECURITY_READ_ONLY_LATE(const void *)    ux_handler_kobject    = NULL;
62 SECURITY_READ_ONLY_LATE(ipc_port_t)             ux_handler_port       = IP_NULL;
63 
64 IPC_KOBJECT_DEFINE(IKOT_UX_HANDLER,
65     .iko_op_stable    = true,
66     .iko_op_permanent = true);
67 
68 /*
69  * init is called early in Mach initialization
70  * when we can initialize read-only memory
71  */
72 void
ux_handler_init(void)73 ux_handler_init(void)
74 {
75 	ux_handler_port = ipc_kobject_alloc_port((ipc_kobject_t)&ux_handler_kobject,
76 	    IKOT_UX_HANDLER, IPC_KOBJECT_ALLOC_NONE);
77 }
78 
79 /*
80  * setup is called late in BSD initialization from initproc's context
81  * so the MAC hook goo inside host_set_exception_ports will be able to
82  * set up labels without falling over.
83  */
84 void
ux_handler_setup(void)85 ux_handler_setup(void)
86 {
87 	ipc_port_t ux_handler_send_right = ipc_port_make_send(ux_handler_port);
88 
89 	if (!IP_VALID(ux_handler_send_right)) {
90 		panic("Couldn't allocate send right for ux_handler_port!");
91 	}
92 
93 	kern_return_t kr = KERN_SUCCESS;
94 
95 	/*
96 	 * Consumes 1 send right.
97 	 *
98 	 * Instruments uses the RPC_ALERT port, so don't register for that.
99 	 */
100 	kr = host_set_exception_ports(host_priv_self(),
101 	    EXC_MASK_ALL & ~(EXC_MASK_RPC_ALERT | EXC_MASK_GUARD),
102 	    ux_handler_send_right,
103 	    EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES,
104 	    0);
105 
106 	if (kr != KERN_SUCCESS) {
107 		panic("host_set_exception_ports failed to set ux_handler! %d", kr);
108 	}
109 }
110 
111 /*
112  * Is this port the ux_handler?
113  * If so, it's safe to send an exception without checking labels.
114  */
115 boolean_t
is_ux_handler_port(mach_port_t port)116 is_ux_handler_port(mach_port_t port)
117 {
118 	if (ux_handler_port == port) {
119 		return TRUE;
120 	} else {
121 		return FALSE;
122 	}
123 }
124 
125 kern_return_t
catch_mach_exception_raise(mach_port_t exception_port,mach_port_t thread_port,mach_port_t task_port,exception_type_t exception,mach_exception_data_t code,__unused mach_msg_type_number_t codeCnt)126 catch_mach_exception_raise(
127 	mach_port_t                  exception_port,
128 	mach_port_t                  thread_port,
129 	mach_port_t                  task_port,
130 	exception_type_t             exception,
131 	mach_exception_data_t        code,
132 	__unused mach_msg_type_number_t       codeCnt)
133 {
134 	if (exception_port != ux_handler_port) {
135 		return KERN_FAILURE;
136 	}
137 
138 	kern_return_t kr = KERN_SUCCESS;
139 
140 	thread_t    target_thread   = THREAD_NULL;
141 	task_t      target_task     = TASK_NULL;
142 
143 	if ((target_thread = convert_port_to_thread(thread_port)) == THREAD_NULL) {
144 		kr = KERN_INVALID_ARGUMENT;
145 		goto out;
146 	}
147 
148 	if ((target_task = convert_port_to_task(task_port)) == TASK_NULL) {
149 		kr = KERN_INVALID_ARGUMENT;
150 		goto out;
151 	}
152 
153 	kr = handle_ux_exception(target_thread, exception, code[0], code[1]);
154 
155 out:
156 	if (kr == KERN_SUCCESS) {
157 		/*
158 		 * Following the MIG 'consume on success' protocol,
159 		 * consume references to the port arguments.
160 		 * (but NOT the exception_port, as the first argument is borrowed)
161 		 *
162 		 * If we return non-success, the kobject server will eat the port
163 		 * references for us.
164 		 */
165 
166 		ipc_port_release_send(thread_port);
167 		ipc_port_release_send(task_port);
168 	}
169 
170 	thread_deallocate(target_thread);
171 	task_deallocate(target_task);
172 
173 	return kr;
174 }
175 
176 kern_return_t
catch_exception_raise(mach_port_t exception_port,mach_port_t thread,mach_port_t task,exception_type_t exception,exception_data_t code,mach_msg_type_number_t codeCnt)177 catch_exception_raise(
178 	mach_port_t               exception_port,
179 	mach_port_t               thread,
180 	mach_port_t               task,
181 	exception_type_t          exception,
182 	exception_data_t          code,
183 	mach_msg_type_number_t    codeCnt)
184 {
185 	if (exception_port != ux_handler_port) {
186 		return KERN_FAILURE;
187 	}
188 
189 	mach_exception_data_type_t big_code[EXCEPTION_CODE_MAX] = {
190 		[0] = code[0],
191 		[1] = code[1],
192 	};
193 
194 	return catch_mach_exception_raise(exception_port,
195 	           thread,
196 	           task,
197 	           exception,
198 	           big_code,
199 	           codeCnt);
200 }
201 
202 kern_return_t
catch_exception_raise_state(__unused mach_port_t exception_port,__unused exception_type_t exception,__unused const exception_data_t code,__unused mach_msg_type_number_t codeCnt,__unused int * flavor,__unused const thread_state_t old_state,__unused mach_msg_type_number_t old_stateCnt,__unused thread_state_t new_state,__unused mach_msg_type_number_t * new_stateCnt)203 catch_exception_raise_state(
204 	__unused mach_port_t                 exception_port,
205 	__unused exception_type_t            exception,
206 	__unused const exception_data_t      code,
207 	__unused mach_msg_type_number_t      codeCnt,
208 	__unused int                        *flavor,
209 	__unused const thread_state_t        old_state,
210 	__unused mach_msg_type_number_t      old_stateCnt,
211 	__unused thread_state_t              new_state,
212 	__unused mach_msg_type_number_t     *new_stateCnt)
213 {
214 	return KERN_INVALID_ARGUMENT;
215 }
216 
217 kern_return_t
catch_mach_exception_raise_state(__unused mach_port_t exception_port,__unused exception_type_t exception,__unused const mach_exception_data_t code,__unused mach_msg_type_number_t codeCnt,__unused int * flavor,__unused const thread_state_t old_state,__unused mach_msg_type_number_t old_stateCnt,__unused thread_state_t new_state,__unused mach_msg_type_number_t * new_stateCnt)218 catch_mach_exception_raise_state(
219 	__unused mach_port_t                    exception_port,
220 	__unused exception_type_t               exception,
221 	__unused const mach_exception_data_t    code,
222 	__unused mach_msg_type_number_t         codeCnt,
223 	__unused int                           *flavor,
224 	__unused const thread_state_t           old_state,
225 	__unused mach_msg_type_number_t         old_stateCnt,
226 	__unused thread_state_t                 new_state,
227 	__unused mach_msg_type_number_t        *new_stateCnt)
228 {
229 	return KERN_INVALID_ARGUMENT;
230 }
231 
232 kern_return_t
catch_exception_raise_state_identity(__unused mach_port_t exception_port,__unused mach_port_t thread,__unused mach_port_t task,__unused exception_type_t exception,__unused exception_data_t code,__unused mach_msg_type_number_t codeCnt,__unused int * flavor,__unused thread_state_t old_state,__unused mach_msg_type_number_t old_stateCnt,__unused thread_state_t new_state,__unused mach_msg_type_number_t * new_stateCnt)233 catch_exception_raise_state_identity(
234 	__unused mach_port_t                exception_port,
235 	__unused mach_port_t                thread,
236 	__unused mach_port_t                task,
237 	__unused exception_type_t           exception,
238 	__unused exception_data_t           code,
239 	__unused mach_msg_type_number_t     codeCnt,
240 	__unused int                       *flavor,
241 	__unused thread_state_t             old_state,
242 	__unused mach_msg_type_number_t     old_stateCnt,
243 	__unused thread_state_t             new_state,
244 	__unused mach_msg_type_number_t    *new_stateCnt)
245 {
246 	return KERN_INVALID_ARGUMENT;
247 }
248 
249 kern_return_t
catch_mach_exception_raise_state_identity(__unused mach_port_t exception_port,__unused mach_port_t thread,__unused mach_port_t task,__unused exception_type_t exception,__unused mach_exception_data_t code,__unused mach_msg_type_number_t codeCnt,__unused int * flavor,__unused thread_state_t old_state,__unused mach_msg_type_number_t old_stateCnt,__unused thread_state_t new_state,__unused mach_msg_type_number_t * new_stateCnt)250 catch_mach_exception_raise_state_identity(
251 	__unused mach_port_t                   exception_port,
252 	__unused mach_port_t                   thread,
253 	__unused mach_port_t                   task,
254 	__unused exception_type_t              exception,
255 	__unused mach_exception_data_t         code,
256 	__unused mach_msg_type_number_t        codeCnt,
257 	__unused int                          *flavor,
258 	__unused thread_state_t                old_state,
259 	__unused mach_msg_type_number_t        old_stateCnt,
260 	__unused thread_state_t                new_state,
261 	__unused mach_msg_type_number_t       *new_stateCnt)
262 {
263 	return KERN_INVALID_ARGUMENT;
264 }
265 
266 kern_return_t
catch_mach_exception_raise_identity_protected(__unused mach_port_t exception_port,__unused uint64_t thread_id,__unused mach_port_t task_id_token,__unused exception_type_t exception,__unused mach_exception_data_t code,__unused mach_msg_type_number_t codeCnt)267 catch_mach_exception_raise_identity_protected(
268 	__unused mach_port_t               exception_port,
269 	__unused uint64_t                  thread_id,
270 	__unused mach_port_t               task_id_token,
271 	__unused exception_type_t          exception,
272 	__unused mach_exception_data_t     code,
273 	__unused mach_msg_type_number_t    codeCnt)
274 {
275 	return KERN_INVALID_ARGUMENT;
276 }
277