xref: /xnu-8796.121.2/osfmk/kern/exception.c (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1 /*
2  * Copyright (c) 2000-2020 Apple Computer, 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  * @OSF_COPYRIGHT@
30  */
31 /*
32  * Mach Operating System
33  * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34  * All Rights Reserved.
35  *
36  * Permission to use, copy, modify and distribute this software and its
37  * documentation is hereby granted, provided that both the copyright
38  * notice and this permission notice appear in all copies of the
39  * software, derivative works or modified versions, and any portions
40  * thereof, and that both notices appear in supporting documentation.
41  *
42  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45  *
46  * Carnegie Mellon requests users of this software to return to
47  *
48  *  Software Distribution Coordinator  or  [email protected]
49  *  School of Computer Science
50  *  Carnegie Mellon University
51  *  Pittsburgh PA 15213-3890
52  *
53  * any improvements or extensions that they make and grant Carnegie Mellon
54  * the rights to redistribute these changes.
55  */
56 /*
57  */
58 
59 #include <mach/mach_types.h>
60 #include <mach/boolean.h>
61 #include <mach/kern_return.h>
62 #include <mach/message.h>
63 #include <mach/port.h>
64 #include <mach/mig_errors.h>
65 #include <mach/task.h>
66 #include <mach/thread_status.h>
67 #include <mach/exception_types.h>
68 #include <mach/exc.h>
69 #include <mach/mach_exc.h>
70 
71 #include <ipc/port.h>
72 #include <ipc/ipc_entry.h>
73 #include <ipc/ipc_object.h>
74 #include <ipc/ipc_notify.h>
75 #include <ipc/ipc_space.h>
76 #include <ipc/ipc_pset.h>
77 #include <ipc/ipc_machdep.h>
78 
79 #include <kern/ipc_tt.h>
80 #include <kern/task.h>
81 #include <kern/thread.h>
82 #include <kern/processor.h>
83 #include <kern/sched.h>
84 #include <kern/sched_prim.h>
85 #include <kern/host.h>
86 #include <kern/misc_protos.h>
87 #include <kern/ux_handler.h>
88 #include <kern/task_ident.h>
89 
90 #include <vm/vm_map.h>
91 
92 #include <security/mac_mach_internal.h>
93 #include <string.h>
94 
95 #include <pexpert/pexpert.h>
96 
97 #include <os/log.h>
98 #include <os/system_event_log.h>
99 
100 #include <libkern/coreanalytics/coreanalytics.h>
101 
102 #include <sys/code_signing.h> /* for developer mode state */
103 
104 bool panic_on_exception_triage = false;
105 
106 /* Not used in coded, only for inspection during debugging */
107 unsigned long c_thr_exc_raise = 0;
108 unsigned long c_thr_exc_raise_identity_token = 0;
109 unsigned long c_thr_exc_raise_state = 0;
110 unsigned long c_thr_exc_raise_state_id = 0;
111 unsigned long c_thr_exc_raise_backtrace = 0;
112 
113 /* forward declarations */
114 kern_return_t exception_deliver(
115 	thread_t                thread,
116 	exception_type_t        exception,
117 	mach_exception_data_t   code,
118 	mach_msg_type_number_t  codeCnt,
119 	struct exception_action *excp,
120 	lck_mtx_t                       *mutex);
121 
122 #ifdef MACH_BSD
123 kern_return_t bsd_exception(
124 	exception_type_t        exception,
125 	mach_exception_data_t   code,
126 	mach_msg_type_number_t  codeCnt);
127 #endif /* MACH_BSD */
128 
129 #if __has_feature(ptrauth_calls)
130 extern int exit_with_pac_exception(
131 	void *proc,
132 	exception_type_t         exception,
133 	mach_exception_code_t    code,
134 	mach_exception_subcode_t subcode);
135 #endif /* __has_feature(ptrauth_calls) */
136 
137 #ifdef MACH_BSD
138 extern bool proc_is_traced(void *p);
139 extern int      proc_selfpid(void);
140 extern char     *proc_name_address(struct proc *p);
141 #endif /* MACH_BSD */
142 
143 #if (DEVELOPMENT || DEBUG)
144 TUNABLE_WRITEABLE(unsigned int, exception_log_max_pid, "exception_log_max_pid", 0);
145 #endif /* (DEVELOPMENT || DEBUG) */
146 
147 /*
148  * Routine: exception_init
149  * Purpose:
150  *   Global initialization of state for exceptions.
151  * Conditions:
152  *   None.
153  */
154 void
exception_init(void)155 exception_init(void)
156 {
157 	int tmp = 0;
158 
159 	if (PE_parse_boot_argn("-panic_on_exception_triage", &tmp, sizeof(tmp))) {
160 		panic_on_exception_triage = true;
161 	}
162 
163 #if (DEVELOPMENT || DEBUG)
164 	if (exception_log_max_pid) {
165 		printf("Logging all exceptions where pid < exception_log_max_pid (%d)\n", exception_log_max_pid);
166 	}
167 #endif /* (DEVELOPMENT || DEBUG) */
168 }
169 
170 static TUNABLE(bool, pac_replace_ptrs_user, "pac_replace_ptrs_user", true);
171 
172 ipc_port_t
exception_port_copy_send(ipc_port_t port)173 exception_port_copy_send(ipc_port_t port)
174 {
175 	if (IP_VALID(port)) {
176 		if (is_ux_handler_port(port)) {
177 			/* is_ux_handler_port() compares against __DATA_CONST */
178 			port = ipc_port_copy_send_any(port);
179 		} else {
180 			port = ipc_port_copy_send_mqueue(port);
181 		}
182 	}
183 	return port;
184 }
185 
186 /*
187  *	Routine:	exception_deliver
188  *	Purpose:
189  *		Make an upcall to the exception server provided.
190  *	Conditions:
191  *		Nothing locked and no resources held.
192  *		Called from an exception context, so
193  *		thread_exception_return and thread_kdb_return
194  *		are possible.
195  *	Returns:
196  *		KERN_SUCCESS if the exception was handled
197  */
198 kern_return_t
exception_deliver(thread_t thread,exception_type_t exception,mach_exception_data_t code,mach_msg_type_number_t codeCnt,struct exception_action * excp,lck_mtx_t * mutex)199 exception_deliver(
200 	thread_t                thread,
201 	exception_type_t        exception,
202 	mach_exception_data_t   code,
203 	mach_msg_type_number_t  codeCnt,
204 	struct exception_action *excp,
205 	lck_mtx_t               *mutex)
206 {
207 	ipc_port_t              exc_port = IPC_PORT_NULL;
208 	exception_data_type_t   small_code[EXCEPTION_CODE_MAX];
209 	thread_state_t          new_state = NULL;
210 	int                     code64;
211 	int                     behavior;
212 	int                     flavor;
213 	kern_return_t           kr;
214 	task_t task;
215 	task_id_token_t task_token;
216 	ipc_port_t thread_port = IPC_PORT_NULL,
217 	    task_port = IPC_PORT_NULL,
218 	    task_token_port = IPC_PORT_NULL;
219 
220 	/*
221 	 *  Save work if we are terminating.
222 	 *  Just go back to our AST handler.
223 	 */
224 	if (!thread->active && !thread->inspection) {
225 		return KERN_SUCCESS;
226 	}
227 
228 	/*
229 	 * If there are no exception actions defined for this entity,
230 	 * we can't deliver here.
231 	 */
232 	if (excp == NULL) {
233 		return KERN_FAILURE;
234 	}
235 
236 	assert(exception < EXC_TYPES_COUNT);
237 	if (exception >= EXC_TYPES_COUNT) {
238 		return KERN_FAILURE;
239 	}
240 
241 	excp = &excp[exception];
242 
243 	/*
244 	 * Snapshot the exception action data under lock for consistency.
245 	 * Hold a reference to the port over the exception_raise_* calls
246 	 * so it can't be destroyed.  This seems like overkill, but keeps
247 	 * the port from disappearing between now and when
248 	 * ipc_object_copyin_from_kernel is finally called.
249 	 */
250 	lck_mtx_lock(mutex);
251 	exc_port = exception_port_copy_send(excp->port);
252 	if (!IP_VALID(exc_port)) {
253 		lck_mtx_unlock(mutex);
254 		return KERN_FAILURE;
255 	}
256 
257 	flavor = excp->flavor;
258 	behavior = excp->behavior;
259 	lck_mtx_unlock(mutex);
260 
261 	code64 = (behavior & MACH_EXCEPTION_CODES);
262 	behavior &= ~MACH_EXCEPTION_MASK;
263 
264 	if (!code64) {
265 		small_code[0] = CAST_DOWN_EXPLICIT(exception_data_type_t, code[0]);
266 		small_code[1] = CAST_DOWN_EXPLICIT(exception_data_type_t, code[1]);
267 	}
268 
269 	task = get_threadtask(thread);
270 
271 #if CONFIG_MACF
272 	/* Now is a reasonably good time to check if the exception action is
273 	 * permitted for this process, because after this point we will send
274 	 * the message out almost certainly.
275 	 * As with other failures, exception_triage_thread will go on
276 	 * to the next level.
277 	 */
278 
279 	/* The global exception-to-signal translation port is safe to be an exception handler. */
280 	if (is_ux_handler_port(exc_port) == FALSE &&
281 	    mac_exc_action_check_exception_send(task, excp) != 0) {
282 		kr = KERN_FAILURE;
283 		goto out_release_right;
284 	}
285 #endif
286 
287 	thread->options |= TH_IN_MACH_EXCEPTION;
288 
289 	switch (behavior) {
290 	case EXCEPTION_STATE: {
291 		mach_msg_type_number_t old_state_cnt, new_state_cnt;
292 		thread_state_data_t old_state;
293 		thread_set_status_flags_t get_flags = TSSF_TRANSLATE_TO_USER;
294 		thread_set_status_flags_t set_flags = TSSF_CHECK_USER_FLAGS;
295 		bool task_allow_user_state = task_needs_user_signed_thread_state(task);
296 
297 		if (pac_replace_ptrs_user || task_allow_user_state) {
298 			get_flags |= TSSF_RANDOM_USER_DIV;
299 			set_flags |= (TSSF_ALLOW_ONLY_USER_PTRS | TSSF_RANDOM_USER_DIV);
300 		}
301 
302 		c_thr_exc_raise_state++;
303 		old_state_cnt = _MachineStateCount[flavor];
304 		kr = thread_getstatus_to_user(thread, flavor,
305 		    (thread_state_t)old_state,
306 		    &old_state_cnt, get_flags);
307 		new_state_cnt = old_state_cnt;
308 		if (kr == KERN_SUCCESS) {
309 			new_state = (thread_state_t)kalloc_data(sizeof(thread_state_data_t), Z_WAITOK | Z_ZERO);
310 			if (new_state == NULL) {
311 				kr = KERN_RESOURCE_SHORTAGE;
312 				goto out_release_right;
313 			}
314 			if (code64) {
315 				kr = mach_exception_raise_state(exc_port,
316 				    exception,
317 				    code,
318 				    codeCnt,
319 				    &flavor,
320 				    old_state, old_state_cnt,
321 				    new_state, &new_state_cnt);
322 			} else {
323 				kr = exception_raise_state(exc_port, exception,
324 				    small_code,
325 				    codeCnt,
326 				    &flavor,
327 				    old_state, old_state_cnt,
328 				    new_state, &new_state_cnt);
329 			}
330 			if (kr == KERN_SUCCESS) {
331 				if (exception != EXC_CORPSE_NOTIFY) {
332 					kr = thread_setstatus_from_user(thread, flavor,
333 					    (thread_state_t)new_state, new_state_cnt,
334 					    (thread_state_t)old_state, old_state_cnt,
335 					    set_flags);
336 				}
337 				goto out_release_right;
338 			}
339 		}
340 
341 		goto out_release_right;
342 	}
343 
344 	case EXCEPTION_DEFAULT: {
345 		c_thr_exc_raise++;
346 
347 		task_reference(task);
348 		thread_reference(thread);
349 		/*
350 		 * Only deliver control port if Developer Mode enabled,
351 		 * or task is a corpse. Otherwise we only deliver the
352 		 * (immovable) read port in exception handler (both in
353 		 * or out of process). (94669540)
354 		 */
355 		if (developer_mode_state() || task_is_a_corpse(task)) {
356 			task_port = convert_task_to_port(task);
357 			thread_port = convert_thread_to_port(thread);
358 		} else {
359 			task_port = convert_task_read_to_port(task);
360 			thread_port = convert_thread_read_to_port(thread);
361 		}
362 		/* task and thread ref consumed */
363 
364 		if (code64) {
365 			kr = mach_exception_raise(exc_port,
366 			    thread_port,
367 			    task_port,
368 			    exception,
369 			    code,
370 			    codeCnt);
371 		} else {
372 			kr = exception_raise(exc_port,
373 			    thread_port,
374 			    task_port,
375 			    exception,
376 			    small_code,
377 			    codeCnt);
378 		}
379 
380 		goto out_release_right;
381 	}
382 
383 	case EXCEPTION_IDENTITY_PROTECTED: {
384 		c_thr_exc_raise_identity_token++;
385 
386 		kr = task_create_identity_token(task, &task_token);
387 		/* task_token now represents a task, or corpse */
388 		assert(kr == KERN_SUCCESS);
389 		task_token_port = convert_task_id_token_to_port(task_token);
390 		/* task token ref consumed */
391 
392 		if (code64) {
393 			kr = mach_exception_raise_identity_protected(exc_port,
394 			    thread->thread_id,
395 			    task_token_port,
396 			    exception,
397 			    code,
398 			    codeCnt);
399 		} else {
400 			panic("mach_exception_raise_identity_protected() must be code64");
401 		}
402 
403 		goto out_release_right;
404 	}
405 
406 	case EXCEPTION_STATE_IDENTITY: {
407 		mach_msg_type_number_t old_state_cnt, new_state_cnt;
408 		thread_state_data_t old_state;
409 		thread_set_status_flags_t get_flags = TSSF_TRANSLATE_TO_USER;
410 		thread_set_status_flags_t set_flags = TSSF_CHECK_USER_FLAGS;
411 		bool task_allow_user_state = task_needs_user_signed_thread_state(task);
412 
413 		if (pac_replace_ptrs_user || task_allow_user_state) {
414 			get_flags |= TSSF_RANDOM_USER_DIV;
415 			set_flags |= (TSSF_ALLOW_ONLY_USER_PTRS | TSSF_RANDOM_USER_DIV);
416 		}
417 
418 		c_thr_exc_raise_state_id++;
419 
420 		task_reference(task);
421 		thread_reference(thread);
422 		/*
423 		 * Only deliver control port if Developer Mode enabled,
424 		 * or task is a corpse. Otherwise we only deliver the
425 		 * (immovable) read port in exception handler (both in
426 		 * or out of process). (94669540)
427 		 */
428 		if (developer_mode_state() || task_is_a_corpse(task)) {
429 			task_port = convert_task_to_port(task);
430 			thread_port = convert_thread_to_port(thread);
431 		} else {
432 			task_port = convert_task_read_to_port(task);
433 			thread_port = convert_thread_read_to_port(thread);
434 		}
435 		/* task and thread ref consumed */
436 
437 		old_state_cnt = _MachineStateCount[flavor];
438 		kr = thread_getstatus_to_user(thread, flavor,
439 		    (thread_state_t)old_state,
440 		    &old_state_cnt, get_flags);
441 		new_state_cnt = old_state_cnt;
442 		if (kr == KERN_SUCCESS) {
443 			new_state = (thread_state_t)kalloc_data(sizeof(thread_state_data_t), Z_WAITOK | Z_ZERO);
444 			if (new_state == NULL) {
445 				kr = KERN_RESOURCE_SHORTAGE;
446 				goto out_release_right;
447 			}
448 			if (code64) {
449 				kr = mach_exception_raise_state_identity(
450 					exc_port,
451 					thread_port,
452 					task_port,
453 					exception,
454 					code,
455 					codeCnt,
456 					&flavor,
457 					old_state, old_state_cnt,
458 					new_state, &new_state_cnt);
459 			} else {
460 				kr = exception_raise_state_identity(exc_port,
461 				    thread_port,
462 				    task_port,
463 				    exception,
464 				    small_code,
465 				    codeCnt,
466 				    &flavor,
467 				    old_state, old_state_cnt,
468 				    new_state, &new_state_cnt);
469 			}
470 
471 			if (kr == KERN_SUCCESS) {
472 				if (exception != EXC_CORPSE_NOTIFY &&
473 				    ip_kotype(thread_port) == IKOT_THREAD_CONTROL) {
474 					kr = thread_setstatus_from_user(thread, flavor,
475 					    (thread_state_t)new_state, new_state_cnt,
476 					    (thread_state_t)old_state, old_state_cnt, set_flags);
477 				}
478 				goto out_release_right;
479 			}
480 		}
481 
482 		goto out_release_right;
483 	}
484 
485 	default:
486 		panic("bad exception behavior!");
487 		return KERN_FAILURE;
488 	}/* switch */
489 
490 out_release_right:
491 
492 	thread->options &= ~TH_IN_MACH_EXCEPTION;
493 
494 	if (task_port) {
495 		ipc_port_release_send(task_port);
496 	}
497 
498 	if (thread_port) {
499 		ipc_port_release_send(thread_port);
500 	}
501 
502 	if (exc_port) {
503 		ipc_port_release_send(exc_port);
504 	}
505 
506 	if (task_token_port) {
507 		ipc_port_release_send(task_token_port);
508 	}
509 
510 	if (new_state) {
511 		kfree_data(new_state, sizeof(thread_state_data_t));
512 	}
513 
514 	return kr;
515 }
516 
517 /*
518  * Attempt exception delivery with backtrace info to exception ports
519  * in exc_ports in order.
520  */
521 /*
522  *	Routine:	exception_deliver_backtrace
523  *	Purpose:
524  *      Attempt exception delivery with backtrace info to exception ports
525  *      in exc_ports in order.
526  *	Conditions:
527  *		Caller has a reference on bt_object, and send rights on exc_ports.
528  *		Does not consume any passed references or rights
529  */
530 void
exception_deliver_backtrace(kcdata_object_t bt_object,ipc_port_t exc_ports[static BT_EXC_PORTS_COUNT],exception_type_t exception)531 exception_deliver_backtrace(
532 	kcdata_object_t  bt_object,
533 	ipc_port_t       exc_ports[static BT_EXC_PORTS_COUNT],
534 	exception_type_t exception)
535 {
536 	kern_return_t kr;
537 	mach_exception_data_type_t code[EXCEPTION_CODE_MAX];
538 	ipc_port_t target_port, bt_obj_port;
539 
540 	assert(exception == EXC_GUARD);
541 
542 	code[0] = exception;
543 	code[1] = 0;
544 
545 	kcdata_object_reference(bt_object);
546 	bt_obj_port = convert_kcdata_object_to_port(bt_object);
547 	/* backtrace object ref consumed, no-senders is armed */
548 
549 	if (!IP_VALID(bt_obj_port)) {
550 		return;
551 	}
552 
553 	/*
554 	 * We are guaranteed at task_enqueue_exception_with_corpse() time
555 	 * that the exception port prefers backtrace delivery.
556 	 */
557 	for (unsigned int i = 0; i < BT_EXC_PORTS_COUNT; i++) {
558 		target_port = exc_ports[i];
559 
560 		if (!IP_VALID(target_port)) {
561 			continue;
562 		}
563 
564 		ip_mq_lock(target_port);
565 		if (!ip_active(target_port)) {
566 			ip_mq_unlock(target_port);
567 			continue;
568 		}
569 		ip_mq_unlock(target_port);
570 
571 		kr = mach_exception_raise_backtrace(target_port,
572 		    bt_obj_port,
573 		    EXC_CORPSE_NOTIFY,
574 		    code,
575 		    EXCEPTION_CODE_MAX);
576 
577 		if (kr == KERN_SUCCESS || kr == MACH_RCV_PORT_DIED) {
578 			/* Exception is handled at this level */
579 			break;
580 		}
581 	}
582 
583 	/* May trigger no-senders notification for backtrace object */
584 	ipc_port_release_send(bt_obj_port);
585 
586 	return;
587 }
588 
589 /*
590  * Routine: check_exc_receiver_dependency
591  * Purpose:
592  *      Verify that the port destined for receiving this exception is not
593  *      on the current task. This would cause hang in kernel for
594  *      EXC_CRASH primarily. Note: If port is transferred
595  *      between check and delivery then deadlock may happen.
596  *
597  * Conditions:
598  *		Nothing locked and no resources held.
599  *		Called from an exception context.
600  * Returns:
601  *      KERN_SUCCESS if its ok to send exception message.
602  */
603 static kern_return_t
check_exc_receiver_dependency(exception_type_t exception,struct exception_action * excp,lck_mtx_t * mutex)604 check_exc_receiver_dependency(
605 	exception_type_t exception,
606 	struct exception_action *excp,
607 	lck_mtx_t *mutex)
608 {
609 	kern_return_t retval = KERN_SUCCESS;
610 
611 	if (excp == NULL || exception != EXC_CRASH) {
612 		return retval;
613 	}
614 
615 	task_t task = current_task();
616 	lck_mtx_lock(mutex);
617 	ipc_port_t xport = excp[exception].port;
618 	if (IP_VALID(xport) && ip_in_space_noauth(xport, task->itk_space)) {
619 		retval = KERN_FAILURE;
620 	}
621 	lck_mtx_unlock(mutex);
622 	return retval;
623 }
624 
625 
626 /*
627  *	Routine:	exception_triage_thread
628  *	Purpose:
629  *		The thread caught an exception.
630  *		We make an up-call to the thread's exception server.
631  *	Conditions:
632  *		Nothing locked and no resources held.
633  *		Called from an exception context, so
634  *		thread_exception_return and thread_kdb_return
635  *		are possible.
636  *	Returns:
637  *		KERN_SUCCESS if exception is handled by any of the handlers.
638  */
639 kern_return_t
exception_triage_thread(exception_type_t exception,mach_exception_data_t code,mach_msg_type_number_t codeCnt,thread_t thread)640 exception_triage_thread(
641 	exception_type_t        exception,
642 	mach_exception_data_t   code,
643 	mach_msg_type_number_t  codeCnt,
644 	thread_t                thread)
645 {
646 	task_t                  task;
647 	thread_ro_t             tro;
648 	host_priv_t             host_priv;
649 	lck_mtx_t               *mutex;
650 	struct exception_action *actions;
651 	kern_return_t   kr = KERN_FAILURE;
652 
653 	assert(exception != EXC_RPC_ALERT);
654 
655 	/*
656 	 * If this behavior has been requested by the the kernel
657 	 * (due to the boot environment), we should panic if we
658 	 * enter this function.  This is intended as a debugging
659 	 * aid; it should allow us to debug why we caught an
660 	 * exception in environments where debugging is especially
661 	 * difficult.
662 	 */
663 	if (panic_on_exception_triage) {
664 		panic("called exception_triage when it was forbidden by the boot environment");
665 	}
666 
667 	/*
668 	 * Try to raise the exception at the activation level.
669 	 */
670 	mutex   = &thread->mutex;
671 	tro     = get_thread_ro(thread);
672 	actions = tro->tro_exc_actions;
673 	if (KERN_SUCCESS == check_exc_receiver_dependency(exception, actions, mutex)) {
674 		kr = exception_deliver(thread, exception, code, codeCnt, actions, mutex);
675 		if (kr == KERN_SUCCESS || kr == MACH_RCV_PORT_DIED) {
676 			goto out;
677 		}
678 	}
679 
680 	/*
681 	 * Maybe the task level will handle it.
682 	 */
683 	task    = tro->tro_task;
684 	mutex   = &task->itk_lock_data;
685 	actions = task->exc_actions;
686 	if (KERN_SUCCESS == check_exc_receiver_dependency(exception, actions, mutex)) {
687 		kr = exception_deliver(thread, exception, code, codeCnt, actions, mutex);
688 		if (kr == KERN_SUCCESS || kr == MACH_RCV_PORT_DIED) {
689 			goto out;
690 		}
691 	}
692 
693 	/*
694 	 * How about at the host level?
695 	 */
696 	host_priv = host_priv_self();
697 	mutex     = &host_priv->lock;
698 	actions   = host_priv->exc_actions;
699 	if (KERN_SUCCESS == check_exc_receiver_dependency(exception, actions, mutex)) {
700 		kr = exception_deliver(thread, exception, code, codeCnt, actions, mutex);
701 		if (kr == KERN_SUCCESS || kr == MACH_RCV_PORT_DIED) {
702 			goto out;
703 		}
704 	}
705 
706 out:
707 	if ((exception != EXC_CRASH) && (exception != EXC_RESOURCE) &&
708 	    (exception != EXC_GUARD) && (exception != EXC_CORPSE_NOTIFY)) {
709 		thread_exception_return();
710 	}
711 	return kr;
712 }
713 
714 #if __has_feature(ptrauth_calls)
715 static TUNABLE(bool, pac_exception_telemetry, "-pac_exception_telemetry", false);
716 
717 CA_EVENT(pac_exception_event,
718     CA_INT, exception,
719     CA_INT, exception_code_0,
720     CA_INT, exception_code_1,
721     CA_STATIC_STRING(CA_PROCNAME_LEN), proc_name);
722 
723 static void
pac_exception_triage(exception_type_t exception,mach_exception_data_t code)724 pac_exception_triage(
725 	exception_type_t        exception,
726 	mach_exception_data_t   code)
727 {
728 	boolean_t traced_flag = FALSE;
729 	task_t task = current_task();
730 	void *proc = get_bsdtask_info(task);
731 	char *proc_name = (char *) "unknown";
732 	int pid = 0;
733 
734 #ifdef MACH_BSD
735 	pid = proc_selfpid();
736 	if (proc) {
737 		traced_flag = proc_is_traced(proc);
738 		/* Should only be called on current proc */
739 		proc_name = proc_name_address(proc);
740 
741 		/*
742 		 * For a ptrauth violation, check if process isn't being ptraced and
743 		 * the task has the TFRO_PAC_EXC_FATAL flag set. If both conditions are true,
744 		 * terminate the task via exit_with_reason
745 		 */
746 		if (!traced_flag) {
747 			if (pac_exception_telemetry) {
748 				ca_event_t ca_event = CA_EVENT_ALLOCATE(pac_exception_event);
749 				CA_EVENT_TYPE(pac_exception_event) * pexc_event = ca_event->data;
750 				pexc_event->exception = exception;
751 				pexc_event->exception_code_0 = code[0];
752 				pexc_event->exception_code_1 = code[1];
753 				strlcpy(pexc_event->proc_name, proc_name, CA_PROCNAME_LEN);
754 				CA_EVENT_SEND(ca_event);
755 			}
756 			if (task_is_pac_exception_fatal(task)) {
757 				os_log_error(OS_LOG_DEFAULT, "%s: process %s[%d] hit a pac violation\n", __func__, proc_name, pid);
758 				exit_with_pac_exception(proc, exception, code[0], code[1]);
759 				thread_exception_return();
760 				/* NOT_REACHABLE */
761 			}
762 		}
763 	}
764 #endif /* MACH_BSD */
765 }
766 #endif /* __has_feature(ptrauth_calls) */
767 
768 /*
769  *	Routine:	exception_triage
770  *	Purpose:
771  *		The current thread caught an exception.
772  *		We make an up-call to the thread's exception server.
773  *	Conditions:
774  *		Nothing locked and no resources held.
775  *		Called from an exception context, so
776  *		thread_exception_return and thread_kdb_return
777  *		are possible.
778  *	Returns:
779  *		KERN_SUCCESS if exception is handled by any of the handlers.
780  */
781 int debug4k_panic_on_exception = 0;
782 kern_return_t
exception_triage(exception_type_t exception,mach_exception_data_t code,mach_msg_type_number_t codeCnt)783 exception_triage(
784 	exception_type_t        exception,
785 	mach_exception_data_t   code,
786 	mach_msg_type_number_t  codeCnt)
787 {
788 	thread_t thread = current_thread();
789 	task_t   task   = current_task();
790 
791 	assert(codeCnt > 0);
792 
793 	if (VM_MAP_PAGE_SIZE(task->map) < PAGE_SIZE) {
794 		DEBUG4K_EXC("thread %p task %p map %p exception %d codes 0x%llx 0x%llx\n",
795 		    thread, task, task->map, exception, code[0], codeCnt > 1 ? code[1] : 0);
796 		if (debug4k_panic_on_exception) {
797 			panic("DEBUG4K thread %p task %p map %p exception %d codes 0x%llx 0x%llx",
798 			    thread, task, task->map, exception, code[0], codeCnt > 1 ? code[1] : 0);
799 		}
800 	}
801 
802 #if (DEVELOPMENT || DEBUG)
803 #ifdef MACH_BSD
804 	if (proc_pid(get_bsdtask_info(task)) <= exception_log_max_pid) {
805 		record_system_event(SYSTEM_EVENT_TYPE_INFO, SYSTEM_EVENT_SUBSYSTEM_PROCESS, "process exit",
806 		    "exception_log_max_pid: pid %d (%s): sending exception %d (0x%llx 0x%llx)",
807 		    proc_pid(get_bsdtask_info(task)), proc_name_address(get_bsdtask_info(task)),
808 		    exception, code[0], codeCnt > 1 ? code[1] : 0);
809 	}
810 #endif /* MACH_BSD */
811 #endif /* DEVELOPMENT || DEBUG */
812 
813 #if __has_feature(ptrauth_calls)
814 	if (exception & EXC_PTRAUTH_BIT) {
815 		exception &= ~EXC_PTRAUTH_BIT;
816 		assert(codeCnt == 2);
817 		pac_exception_triage(exception, code);
818 	}
819 #endif /* __has_feature(ptrauth_calls) */
820 	return exception_triage_thread(exception, code, codeCnt, thread);
821 }
822 
823 kern_return_t
bsd_exception(exception_type_t exception,mach_exception_data_t code,mach_msg_type_number_t codeCnt)824 bsd_exception(
825 	exception_type_t        exception,
826 	mach_exception_data_t   code,
827 	mach_msg_type_number_t  codeCnt)
828 {
829 	task_t                  task;
830 	lck_mtx_t               *mutex;
831 	thread_t                self = current_thread();
832 	kern_return_t           kr;
833 
834 	/*
835 	 * Maybe the task level will handle it.
836 	 */
837 	task = current_task();
838 	mutex = &task->itk_lock_data;
839 
840 	kr = exception_deliver(self, exception, code, codeCnt, task->exc_actions, mutex);
841 
842 	if (kr == KERN_SUCCESS || kr == MACH_RCV_PORT_DIED) {
843 		return KERN_SUCCESS;
844 	}
845 	return KERN_FAILURE;
846 }
847 
848 
849 /*
850  * Raise an exception on a task.
851  * This should tell launchd to launch Crash Reporter for this task.
852  */
853 kern_return_t
task_exception_notify(exception_type_t exception,mach_exception_data_type_t exccode,mach_exception_data_type_t excsubcode)854 task_exception_notify(exception_type_t exception,
855     mach_exception_data_type_t exccode, mach_exception_data_type_t excsubcode)
856 {
857 	mach_exception_data_type_t      code[EXCEPTION_CODE_MAX];
858 	wait_interrupt_t                wsave;
859 	kern_return_t kr = KERN_SUCCESS;
860 
861 	code[0] = exccode;
862 	code[1] = excsubcode;
863 
864 	wsave = thread_interrupt_level(THREAD_UNINT);
865 	kr = exception_triage(exception, code, EXCEPTION_CODE_MAX);
866 	(void) thread_interrupt_level(wsave);
867 	return kr;
868 }
869 
870 
871 /*
872  *	Handle interface for special performance monitoring
873  *	This is a special case of the host exception handler
874  */
875 kern_return_t
sys_perf_notify(thread_t thread,int pid)876 sys_perf_notify(thread_t thread, int pid)
877 {
878 	host_priv_t             hostp;
879 	ipc_port_t              xport;
880 	wait_interrupt_t        wsave;
881 	kern_return_t           ret;
882 
883 	hostp = host_priv_self();       /* Get the host privileged ports */
884 	mach_exception_data_type_t      code[EXCEPTION_CODE_MAX];
885 	code[0] = 0xFF000001;           /* Set terminate code */
886 	code[1] = pid;          /* Pass out the pid */
887 
888 	lck_mtx_lock(&hostp->lock);
889 	xport = hostp->exc_actions[EXC_RPC_ALERT].port;
890 
891 	/* Make sure we're not catching our own exception */
892 	if (!IP_VALID(xport) ||
893 	    !ip_active(xport) ||
894 	    ip_in_space_noauth(xport, get_threadtask(thread)->itk_space)) {
895 		lck_mtx_unlock(&hostp->lock);
896 		return KERN_FAILURE;
897 	}
898 
899 	lck_mtx_unlock(&hostp->lock);
900 
901 	wsave = thread_interrupt_level(THREAD_UNINT);
902 	ret = exception_deliver(
903 		thread,
904 		EXC_RPC_ALERT,
905 		code,
906 		2,
907 		hostp->exc_actions,
908 		&hostp->lock);
909 	(void)thread_interrupt_level(wsave);
910 
911 	return ret;
912 }
913