xref: /xnu-10063.121.3/security/mac_mach.c (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions /*
2*2c2f96dcSApple OSS Distributions  * Copyright (c) 2015 Apple Inc. All rights reserved.
3*2c2f96dcSApple OSS Distributions  *
4*2c2f96dcSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*2c2f96dcSApple OSS Distributions  *
6*2c2f96dcSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*2c2f96dcSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*2c2f96dcSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*2c2f96dcSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*2c2f96dcSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*2c2f96dcSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*2c2f96dcSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*2c2f96dcSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*2c2f96dcSApple OSS Distributions  *
15*2c2f96dcSApple OSS Distributions  * Please obtain a copy of the License at
16*2c2f96dcSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*2c2f96dcSApple OSS Distributions  *
18*2c2f96dcSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*2c2f96dcSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*2c2f96dcSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*2c2f96dcSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*2c2f96dcSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*2c2f96dcSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*2c2f96dcSApple OSS Distributions  * limitations under the License.
25*2c2f96dcSApple OSS Distributions  *
26*2c2f96dcSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*2c2f96dcSApple OSS Distributions  */
28*2c2f96dcSApple OSS Distributions 
29*2c2f96dcSApple OSS Distributions #include <mach/exception_types.h>
30*2c2f96dcSApple OSS Distributions #include <mach/mach_types.h>
31*2c2f96dcSApple OSS Distributions #include <osfmk/kern/exception.h>
32*2c2f96dcSApple OSS Distributions #include <osfmk/kern/task.h>
33*2c2f96dcSApple OSS Distributions #include <sys/codesign.h>
34*2c2f96dcSApple OSS Distributions #include <sys/param.h>
35*2c2f96dcSApple OSS Distributions #include <sys/user.h>
36*2c2f96dcSApple OSS Distributions #include <sys/proc.h>
37*2c2f96dcSApple OSS Distributions #include <sys/proc_internal.h>
38*2c2f96dcSApple OSS Distributions #include <sys/kauth.h>
39*2c2f96dcSApple OSS Distributions 
40*2c2f96dcSApple OSS Distributions #include <kern/task.h>
41*2c2f96dcSApple OSS Distributions #include <kern/telemetry.h>
42*2c2f96dcSApple OSS Distributions 
43*2c2f96dcSApple OSS Distributions #include <security/mac_framework.h>
44*2c2f96dcSApple OSS Distributions #include <security/mac_internal.h>
45*2c2f96dcSApple OSS Distributions #include <security/mac_mach_internal.h>
46*2c2f96dcSApple OSS Distributions 
47*2c2f96dcSApple OSS Distributions #if CONFIG_CSR
48*2c2f96dcSApple OSS Distributions #include <sys/csr.h>
49*2c2f96dcSApple OSS Distributions // Panic on internal builds, just log otherwise.
50*2c2f96dcSApple OSS Distributions #define MAC_MACH_UNEXPECTED(fmt...) \
51*2c2f96dcSApple OSS Distributions 	if (csr_check(CSR_ALLOW_APPLE_INTERNAL) == 0) { panic(fmt); } else { printf(fmt); }
52*2c2f96dcSApple OSS Distributions #else
53*2c2f96dcSApple OSS Distributions #define MAC_MACH_UNEXPECTED(fmt...) printf(fmt)
54*2c2f96dcSApple OSS Distributions #endif
55*2c2f96dcSApple OSS Distributions 
56*2c2f96dcSApple OSS Distributions static struct proc *
mac_task_get_proc(struct task * task)57*2c2f96dcSApple OSS Distributions mac_task_get_proc(struct task *task)
58*2c2f96dcSApple OSS Distributions {
59*2c2f96dcSApple OSS Distributions 	if (task == current_task()) {
60*2c2f96dcSApple OSS Distributions 		return proc_self();
61*2c2f96dcSApple OSS Distributions 	}
62*2c2f96dcSApple OSS Distributions 
63*2c2f96dcSApple OSS Distributions 	/*
64*2c2f96dcSApple OSS Distributions 	 * Tasks don't really hold a reference on a proc unless the
65*2c2f96dcSApple OSS Distributions 	 * calling thread belongs to the task in question.
66*2c2f96dcSApple OSS Distributions 	 */
67*2c2f96dcSApple OSS Distributions 	int pid = task_pid(task);
68*2c2f96dcSApple OSS Distributions 	struct proc *p = proc_find(pid);
69*2c2f96dcSApple OSS Distributions 
70*2c2f96dcSApple OSS Distributions 	if (p != NULL) {
71*2c2f96dcSApple OSS Distributions 		if (proc_task(p) == task) {
72*2c2f96dcSApple OSS Distributions 			return p;
73*2c2f96dcSApple OSS Distributions 		}
74*2c2f96dcSApple OSS Distributions 		proc_rele(p);
75*2c2f96dcSApple OSS Distributions 	}
76*2c2f96dcSApple OSS Distributions 	return NULL;
77*2c2f96dcSApple OSS Distributions }
78*2c2f96dcSApple OSS Distributions 
79*2c2f96dcSApple OSS Distributions int
mac_task_check_expose_task(struct task * task,mach_task_flavor_t flavor)80*2c2f96dcSApple OSS Distributions mac_task_check_expose_task(struct task *task, mach_task_flavor_t flavor)
81*2c2f96dcSApple OSS Distributions {
82*2c2f96dcSApple OSS Distributions 	int error;
83*2c2f96dcSApple OSS Distributions 
84*2c2f96dcSApple OSS Distributions 	assert(flavor <= TASK_FLAVOR_NAME);
85*2c2f96dcSApple OSS Distributions 
86*2c2f96dcSApple OSS Distributions 	struct proc *p = mac_task_get_proc(task);
87*2c2f96dcSApple OSS Distributions 	if (p == NULL) {
88*2c2f96dcSApple OSS Distributions 		return ESRCH;
89*2c2f96dcSApple OSS Distributions 	}
90*2c2f96dcSApple OSS Distributions 	struct proc_ident pident = proc_ident(p);
91*2c2f96dcSApple OSS Distributions 
92*2c2f96dcSApple OSS Distributions 	struct ucred *cred = kauth_cred_get();
93*2c2f96dcSApple OSS Distributions 	proc_rele(p);
94*2c2f96dcSApple OSS Distributions 
95*2c2f96dcSApple OSS Distributions 	MAC_CHECK(proc_check_expose_task_with_flavor, cred, &pident, flavor);
96*2c2f96dcSApple OSS Distributions 
97*2c2f96dcSApple OSS Distributions 	return error;
98*2c2f96dcSApple OSS Distributions }
99*2c2f96dcSApple OSS Distributions 
100*2c2f96dcSApple OSS Distributions int
mac_task_check_task_id_token_get_task(struct task * task,mach_task_flavor_t flavor)101*2c2f96dcSApple OSS Distributions mac_task_check_task_id_token_get_task(struct task *task, mach_task_flavor_t flavor)
102*2c2f96dcSApple OSS Distributions {
103*2c2f96dcSApple OSS Distributions 	int error;
104*2c2f96dcSApple OSS Distributions 	struct proc *target_proc = NULL;
105*2c2f96dcSApple OSS Distributions 	struct proc_ident *pidentp = NULL;
106*2c2f96dcSApple OSS Distributions 	struct proc_ident pident;
107*2c2f96dcSApple OSS Distributions 
108*2c2f96dcSApple OSS Distributions 	assert(flavor <= TASK_FLAVOR_NAME);
109*2c2f96dcSApple OSS Distributions 
110*2c2f96dcSApple OSS Distributions 	if (!task_is_a_corpse(task)) {
111*2c2f96dcSApple OSS Distributions 		/* only live task has proc */
112*2c2f96dcSApple OSS Distributions 		target_proc = mac_task_get_proc(task);
113*2c2f96dcSApple OSS Distributions 		if (target_proc == NULL) {
114*2c2f96dcSApple OSS Distributions 			return ESRCH;
115*2c2f96dcSApple OSS Distributions 		}
116*2c2f96dcSApple OSS Distributions 		pident = proc_ident(target_proc);
117*2c2f96dcSApple OSS Distributions 		pidentp = &pident;
118*2c2f96dcSApple OSS Distributions 		proc_rele(target_proc);
119*2c2f96dcSApple OSS Distributions 	}
120*2c2f96dcSApple OSS Distributions 
121*2c2f96dcSApple OSS Distributions 	/* pidentp is NULL for corpse task */
122*2c2f96dcSApple OSS Distributions 	MAC_CHECK(proc_check_task_id_token_get_task,
123*2c2f96dcSApple OSS Distributions 	    current_cached_proc_cred(PROC_NULL), pidentp, flavor);
124*2c2f96dcSApple OSS Distributions 
125*2c2f96dcSApple OSS Distributions 	return error;
126*2c2f96dcSApple OSS Distributions }
127*2c2f96dcSApple OSS Distributions 
128*2c2f96dcSApple OSS Distributions int
mac_task_check_get_movable_control_port(void)129*2c2f96dcSApple OSS Distributions mac_task_check_get_movable_control_port(void)
130*2c2f96dcSApple OSS Distributions {
131*2c2f96dcSApple OSS Distributions 	int error;
132*2c2f96dcSApple OSS Distributions 
133*2c2f96dcSApple OSS Distributions 	MAC_CHECK(proc_check_get_movable_control_port,
134*2c2f96dcSApple OSS Distributions 	    current_cached_proc_cred(PROC_NULL));
135*2c2f96dcSApple OSS Distributions 
136*2c2f96dcSApple OSS Distributions 	return error;
137*2c2f96dcSApple OSS Distributions }
138*2c2f96dcSApple OSS Distributions 
139*2c2f96dcSApple OSS Distributions int
mac_task_check_set_host_special_port(struct task * task,int id,struct ipc_port * port)140*2c2f96dcSApple OSS Distributions mac_task_check_set_host_special_port(struct task *task, int id, struct ipc_port *port)
141*2c2f96dcSApple OSS Distributions {
142*2c2f96dcSApple OSS Distributions #pragma unused(task)
143*2c2f96dcSApple OSS Distributions 	int error;
144*2c2f96dcSApple OSS Distributions 
145*2c2f96dcSApple OSS Distributions 	assert(task == current_task());
146*2c2f96dcSApple OSS Distributions 	MAC_CHECK(proc_check_set_host_special_port,
147*2c2f96dcSApple OSS Distributions 	    current_cached_proc_cred(PROC_NULL), id, port);
148*2c2f96dcSApple OSS Distributions 
149*2c2f96dcSApple OSS Distributions 	return error;
150*2c2f96dcSApple OSS Distributions }
151*2c2f96dcSApple OSS Distributions 
152*2c2f96dcSApple OSS Distributions int
mac_task_check_set_host_exception_port(struct task * task,unsigned int exception)153*2c2f96dcSApple OSS Distributions mac_task_check_set_host_exception_port(struct task *task, unsigned int exception)
154*2c2f96dcSApple OSS Distributions {
155*2c2f96dcSApple OSS Distributions #pragma unused(task)
156*2c2f96dcSApple OSS Distributions 	int error;
157*2c2f96dcSApple OSS Distributions 
158*2c2f96dcSApple OSS Distributions 	assert(task == current_task());
159*2c2f96dcSApple OSS Distributions 	MAC_CHECK(proc_check_set_host_exception_port,
160*2c2f96dcSApple OSS Distributions 	    current_cached_proc_cred(PROC_NULL), exception);
161*2c2f96dcSApple OSS Distributions 
162*2c2f96dcSApple OSS Distributions 	return error;
163*2c2f96dcSApple OSS Distributions }
164*2c2f96dcSApple OSS Distributions 
165*2c2f96dcSApple OSS Distributions int
mac_task_check_get_task_special_port(struct task * task,struct task * target,int which)166*2c2f96dcSApple OSS Distributions mac_task_check_get_task_special_port(struct task *task, struct task *target, int which)
167*2c2f96dcSApple OSS Distributions {
168*2c2f96dcSApple OSS Distributions #pragma unused(task)
169*2c2f96dcSApple OSS Distributions 	int error;
170*2c2f96dcSApple OSS Distributions 	struct proc *target_proc = NULL;
171*2c2f96dcSApple OSS Distributions 	struct proc_ident *pidentp = NULL;
172*2c2f96dcSApple OSS Distributions 	struct proc_ident pident;
173*2c2f96dcSApple OSS Distributions 
174*2c2f96dcSApple OSS Distributions 	assert(task == current_task());
175*2c2f96dcSApple OSS Distributions 
176*2c2f96dcSApple OSS Distributions 	if (!task_is_a_corpse(target)) {
177*2c2f96dcSApple OSS Distributions 		/* only live task has proc */
178*2c2f96dcSApple OSS Distributions 		target_proc = mac_task_get_proc(target);
179*2c2f96dcSApple OSS Distributions 		if (target_proc == NULL) {
180*2c2f96dcSApple OSS Distributions 			return ESRCH;
181*2c2f96dcSApple OSS Distributions 		}
182*2c2f96dcSApple OSS Distributions 		pident = proc_ident(target_proc);
183*2c2f96dcSApple OSS Distributions 		pidentp = &pident;
184*2c2f96dcSApple OSS Distributions 		proc_rele(target_proc);
185*2c2f96dcSApple OSS Distributions 	}
186*2c2f96dcSApple OSS Distributions 
187*2c2f96dcSApple OSS Distributions 	/* pidentp is NULL for corpse task */
188*2c2f96dcSApple OSS Distributions 	MAC_CHECK(proc_check_get_task_special_port,
189*2c2f96dcSApple OSS Distributions 	    current_cached_proc_cred(PROC_NULL), pidentp, which);
190*2c2f96dcSApple OSS Distributions 
191*2c2f96dcSApple OSS Distributions 	return error;
192*2c2f96dcSApple OSS Distributions }
193*2c2f96dcSApple OSS Distributions 
194*2c2f96dcSApple OSS Distributions int
mac_task_check_set_task_special_port(struct task * task,struct task * target,int which,struct ipc_port * port)195*2c2f96dcSApple OSS Distributions mac_task_check_set_task_special_port(struct task *task, struct task *target, int which, struct ipc_port *port)
196*2c2f96dcSApple OSS Distributions {
197*2c2f96dcSApple OSS Distributions #pragma unused(task)
198*2c2f96dcSApple OSS Distributions 	int error;
199*2c2f96dcSApple OSS Distributions 
200*2c2f96dcSApple OSS Distributions 	assert(task == current_task());
201*2c2f96dcSApple OSS Distributions 
202*2c2f96dcSApple OSS Distributions 	/*
203*2c2f96dcSApple OSS Distributions 	 * task_set_special_port() is a CONTROL level interface, so we are guaranteed
204*2c2f96dcSApple OSS Distributions 	 * by MIG intrans that target is not a corpse.
205*2c2f96dcSApple OSS Distributions 	 */
206*2c2f96dcSApple OSS Distributions 	assert(!task_is_a_corpse(target));
207*2c2f96dcSApple OSS Distributions 
208*2c2f96dcSApple OSS Distributions 	struct proc *targetp = mac_task_get_proc(target);
209*2c2f96dcSApple OSS Distributions 	if (targetp == NULL) {
210*2c2f96dcSApple OSS Distributions 		return ESRCH;
211*2c2f96dcSApple OSS Distributions 	}
212*2c2f96dcSApple OSS Distributions 
213*2c2f96dcSApple OSS Distributions 	struct proc_ident pident = proc_ident(targetp);
214*2c2f96dcSApple OSS Distributions 	proc_rele(targetp);
215*2c2f96dcSApple OSS Distributions 
216*2c2f96dcSApple OSS Distributions 	MAC_CHECK(proc_check_set_task_special_port,
217*2c2f96dcSApple OSS Distributions 	    current_cached_proc_cred(PROC_NULL), &pident, which, port);
218*2c2f96dcSApple OSS Distributions 
219*2c2f96dcSApple OSS Distributions 	return error;
220*2c2f96dcSApple OSS Distributions }
221*2c2f96dcSApple OSS Distributions 
222*2c2f96dcSApple OSS Distributions int
mac_task_check_dyld_process_info_notify_register(void)223*2c2f96dcSApple OSS Distributions mac_task_check_dyld_process_info_notify_register(void)
224*2c2f96dcSApple OSS Distributions {
225*2c2f96dcSApple OSS Distributions 	int error;
226*2c2f96dcSApple OSS Distributions 
227*2c2f96dcSApple OSS Distributions 	MAC_CHECK(proc_check_dyld_process_info_notify_register,
228*2c2f96dcSApple OSS Distributions 	    current_cached_proc_cred(PROC_NULL));
229*2c2f96dcSApple OSS Distributions 
230*2c2f96dcSApple OSS Distributions 	return error;
231*2c2f96dcSApple OSS Distributions }
232*2c2f96dcSApple OSS Distributions 
233*2c2f96dcSApple OSS Distributions int
mac_task_check_set_host_exception_ports(struct task * task,unsigned int exception_mask)234*2c2f96dcSApple OSS Distributions mac_task_check_set_host_exception_ports(struct task *task, unsigned int exception_mask)
235*2c2f96dcSApple OSS Distributions {
236*2c2f96dcSApple OSS Distributions #pragma unused(task)
237*2c2f96dcSApple OSS Distributions 	int error = 0;
238*2c2f96dcSApple OSS Distributions 	int exception;
239*2c2f96dcSApple OSS Distributions 	kauth_cred_t cred = current_cached_proc_cred(PROC_NULL);
240*2c2f96dcSApple OSS Distributions 
241*2c2f96dcSApple OSS Distributions 	assert(task == current_task());
242*2c2f96dcSApple OSS Distributions 
243*2c2f96dcSApple OSS Distributions 	for (exception = FIRST_EXCEPTION; exception < EXC_TYPES_COUNT; exception++) {
244*2c2f96dcSApple OSS Distributions 		if (exception_mask & (1 << exception)) {
245*2c2f96dcSApple OSS Distributions 			MAC_CHECK(proc_check_set_host_exception_port,
246*2c2f96dcSApple OSS Distributions 			    cred, exception);
247*2c2f96dcSApple OSS Distributions 			if (error) {
248*2c2f96dcSApple OSS Distributions 				break;
249*2c2f96dcSApple OSS Distributions 			}
250*2c2f96dcSApple OSS Distributions 		}
251*2c2f96dcSApple OSS Distributions 	}
252*2c2f96dcSApple OSS Distributions 
253*2c2f96dcSApple OSS Distributions 	return error;
254*2c2f96dcSApple OSS Distributions }
255*2c2f96dcSApple OSS Distributions 
256*2c2f96dcSApple OSS Distributions void
mac_thread_userret(struct thread * td)257*2c2f96dcSApple OSS Distributions mac_thread_userret(struct thread *td)
258*2c2f96dcSApple OSS Distributions {
259*2c2f96dcSApple OSS Distributions 	MAC_PERFORM(thread_userret, td);
260*2c2f96dcSApple OSS Distributions }
261*2c2f96dcSApple OSS Distributions 
262*2c2f96dcSApple OSS Distributions void
mac_thread_telemetry(struct thread * t,int err,void * data,size_t length)263*2c2f96dcSApple OSS Distributions mac_thread_telemetry(struct thread *t, int err, void *data, size_t length)
264*2c2f96dcSApple OSS Distributions {
265*2c2f96dcSApple OSS Distributions 	MAC_PERFORM(thread_telemetry, t, err, data, length);
266*2c2f96dcSApple OSS Distributions }
267*2c2f96dcSApple OSS Distributions 
268*2c2f96dcSApple OSS Distributions void
mac_proc_notify_exec_complete(struct proc * proc)269*2c2f96dcSApple OSS Distributions mac_proc_notify_exec_complete(struct proc *proc)
270*2c2f96dcSApple OSS Distributions {
271*2c2f96dcSApple OSS Distributions 	thread_t thread = current_thread();
272*2c2f96dcSApple OSS Distributions 
273*2c2f96dcSApple OSS Distributions 	/*
274*2c2f96dcSApple OSS Distributions 	 * Since this MAC hook was designed to support upcalls, make sure the hook
275*2c2f96dcSApple OSS Distributions 	 * is called with kernel importance propagation enabled so any daemons
276*2c2f96dcSApple OSS Distributions 	 * can get any appropriate importance donations.
277*2c2f96dcSApple OSS Distributions 	 */
278*2c2f96dcSApple OSS Distributions 	thread_enable_send_importance(thread, TRUE);
279*2c2f96dcSApple OSS Distributions 	MAC_PERFORM(proc_notify_exec_complete, proc);
280*2c2f96dcSApple OSS Distributions 	thread_enable_send_importance(thread, FALSE);
281*2c2f96dcSApple OSS Distributions }
282*2c2f96dcSApple OSS Distributions 
283*2c2f96dcSApple OSS Distributions /**** Exception Policy
284*2c2f96dcSApple OSS Distributions  *
285*2c2f96dcSApple OSS Distributions  * Note that the functions below do not fully follow the usual convention for mac policy functions
286*2c2f96dcSApple OSS Distributions  * in the kernel. Besides avoiding confusion in how the mac function names are mixed with the actual
287*2c2f96dcSApple OSS Distributions  * policy function names, we diverge because the exception policy is somewhat special:
288*2c2f96dcSApple OSS Distributions  * It is used in places where allocation and association must be separate, and its labels do not
289*2c2f96dcSApple OSS Distributions  * only belong to one type of object as usual, but to two (on exception actions and on tasks as
290*2c2f96dcSApple OSS Distributions  * crash labels).
291*2c2f96dcSApple OSS Distributions  */
292*2c2f96dcSApple OSS Distributions 
293*2c2f96dcSApple OSS Distributions struct label *
mac_exc_label(struct exception_action * action)294*2c2f96dcSApple OSS Distributions mac_exc_label(struct exception_action *action)
295*2c2f96dcSApple OSS Distributions {
296*2c2f96dcSApple OSS Distributions 	return mac_label_verify(&action->label);
297*2c2f96dcSApple OSS Distributions }
298*2c2f96dcSApple OSS Distributions 
299*2c2f96dcSApple OSS Distributions void
mac_exc_set_label(struct exception_action * action,struct label * label)300*2c2f96dcSApple OSS Distributions mac_exc_set_label(struct exception_action *action, struct label *label)
301*2c2f96dcSApple OSS Distributions {
302*2c2f96dcSApple OSS Distributions 	action->label = label;
303*2c2f96dcSApple OSS Distributions }
304*2c2f96dcSApple OSS Distributions 
305*2c2f96dcSApple OSS Distributions // Label allocation and deallocation, may sleep.
306*2c2f96dcSApple OSS Distributions 
307*2c2f96dcSApple OSS Distributions struct label *
mac_exc_create_label(struct exception_action * action)308*2c2f96dcSApple OSS Distributions mac_exc_create_label(struct exception_action *action)
309*2c2f96dcSApple OSS Distributions {
310*2c2f96dcSApple OSS Distributions 	return mac_labelzone_alloc_for_owner(action ? &action->label : NULL, MAC_WAITOK, ^(struct label *label) {
311*2c2f96dcSApple OSS Distributions 		// Policy initialization of the label, typically performs allocations as well.
312*2c2f96dcSApple OSS Distributions 		// (Unless the policy's full data really fits into a pointer size.)
313*2c2f96dcSApple OSS Distributions 		MAC_PERFORM(exc_action_label_init, label);
314*2c2f96dcSApple OSS Distributions 	});
315*2c2f96dcSApple OSS Distributions }
316*2c2f96dcSApple OSS Distributions 
317*2c2f96dcSApple OSS Distributions void
mac_exc_free_label(struct label * label)318*2c2f96dcSApple OSS Distributions mac_exc_free_label(struct label *label)
319*2c2f96dcSApple OSS Distributions {
320*2c2f96dcSApple OSS Distributions 	MAC_PERFORM(exc_action_label_destroy, label);
321*2c2f96dcSApple OSS Distributions 	mac_labelzone_free(label);
322*2c2f96dcSApple OSS Distributions }
323*2c2f96dcSApple OSS Distributions 
324*2c2f96dcSApple OSS Distributions // Action label initialization and teardown, may sleep.
325*2c2f96dcSApple OSS Distributions 
326*2c2f96dcSApple OSS Distributions void
mac_exc_associate_action_label(struct exception_action * action,struct label * label)327*2c2f96dcSApple OSS Distributions mac_exc_associate_action_label(struct exception_action *action, struct label *label)
328*2c2f96dcSApple OSS Distributions {
329*2c2f96dcSApple OSS Distributions 	mac_exc_set_label(action, label);
330*2c2f96dcSApple OSS Distributions 	MAC_PERFORM(exc_action_label_associate, action, mac_exc_label(action));
331*2c2f96dcSApple OSS Distributions }
332*2c2f96dcSApple OSS Distributions 
333*2c2f96dcSApple OSS Distributions void
mac_exc_free_action_label(struct exception_action * action)334*2c2f96dcSApple OSS Distributions mac_exc_free_action_label(struct exception_action *action)
335*2c2f96dcSApple OSS Distributions {
336*2c2f96dcSApple OSS Distributions 	mac_exc_free_label(mac_exc_label(action));
337*2c2f96dcSApple OSS Distributions 	mac_exc_set_label(action, NULL);
338*2c2f96dcSApple OSS Distributions }
339*2c2f96dcSApple OSS Distributions 
340*2c2f96dcSApple OSS Distributions // Action label update and inheritance, may NOT sleep and must be quick.
341*2c2f96dcSApple OSS Distributions 
342*2c2f96dcSApple OSS Distributions int
mac_exc_update_action_label(struct exception_action * action,struct label * newlabel)343*2c2f96dcSApple OSS Distributions mac_exc_update_action_label(struct exception_action *action,
344*2c2f96dcSApple OSS Distributions     struct label *newlabel)
345*2c2f96dcSApple OSS Distributions {
346*2c2f96dcSApple OSS Distributions 	int error;
347*2c2f96dcSApple OSS Distributions 
348*2c2f96dcSApple OSS Distributions 	MAC_CHECK(exc_action_label_update, action, mac_exc_label(action), newlabel);
349*2c2f96dcSApple OSS Distributions 
350*2c2f96dcSApple OSS Distributions 	return error;
351*2c2f96dcSApple OSS Distributions }
352*2c2f96dcSApple OSS Distributions 
353*2c2f96dcSApple OSS Distributions int
mac_exc_inherit_action_label(struct exception_action * parent,struct exception_action * child)354*2c2f96dcSApple OSS Distributions mac_exc_inherit_action_label(struct exception_action *parent,
355*2c2f96dcSApple OSS Distributions     struct exception_action *child)
356*2c2f96dcSApple OSS Distributions {
357*2c2f96dcSApple OSS Distributions 	return mac_exc_update_action_label(child, mac_exc_label(parent));
358*2c2f96dcSApple OSS Distributions }
359*2c2f96dcSApple OSS Distributions 
360*2c2f96dcSApple OSS Distributions int
mac_exc_update_task_crash_label(struct task * task,struct label * label)361*2c2f96dcSApple OSS Distributions mac_exc_update_task_crash_label(struct task *task, struct label *label)
362*2c2f96dcSApple OSS Distributions {
363*2c2f96dcSApple OSS Distributions 	int error;
364*2c2f96dcSApple OSS Distributions 
365*2c2f96dcSApple OSS Distributions 	assert(task != kernel_task);
366*2c2f96dcSApple OSS Distributions 
367*2c2f96dcSApple OSS Distributions 	struct label *crash_label = get_task_crash_label(task);
368*2c2f96dcSApple OSS Distributions 
369*2c2f96dcSApple OSS Distributions 	MAC_CHECK(exc_action_label_update, NULL, crash_label, label);
370*2c2f96dcSApple OSS Distributions 
371*2c2f96dcSApple OSS Distributions 	return error;
372*2c2f96dcSApple OSS Distributions }
373*2c2f96dcSApple OSS Distributions 
374*2c2f96dcSApple OSS Distributions // Process label creation, may sleep.
375*2c2f96dcSApple OSS Distributions 
376*2c2f96dcSApple OSS Distributions struct label *
mac_exc_create_label_for_proc(struct proc * proc)377*2c2f96dcSApple OSS Distributions mac_exc_create_label_for_proc(struct proc *proc)
378*2c2f96dcSApple OSS Distributions {
379*2c2f96dcSApple OSS Distributions 	struct label *label = mac_exc_create_label(NULL);
380*2c2f96dcSApple OSS Distributions 	MAC_PERFORM(exc_action_label_populate, label, proc);
381*2c2f96dcSApple OSS Distributions 	return label;
382*2c2f96dcSApple OSS Distributions }
383*2c2f96dcSApple OSS Distributions 
384*2c2f96dcSApple OSS Distributions struct label *
mac_exc_create_label_for_current_proc(void)385*2c2f96dcSApple OSS Distributions mac_exc_create_label_for_current_proc(void)
386*2c2f96dcSApple OSS Distributions {
387*2c2f96dcSApple OSS Distributions 	return mac_exc_create_label_for_proc(current_proc());
388*2c2f96dcSApple OSS Distributions }
389*2c2f96dcSApple OSS Distributions 
390*2c2f96dcSApple OSS Distributions // Exception handler policy checking, may sleep.
391*2c2f96dcSApple OSS Distributions 
392*2c2f96dcSApple OSS Distributions int
mac_exc_action_check_exception_send(struct task * victim_task,struct exception_action * action)393*2c2f96dcSApple OSS Distributions mac_exc_action_check_exception_send(struct task *victim_task, struct exception_action *action)
394*2c2f96dcSApple OSS Distributions {
395*2c2f96dcSApple OSS Distributions 	int error = 0;
396*2c2f96dcSApple OSS Distributions 
397*2c2f96dcSApple OSS Distributions 	struct proc *p = get_bsdtask_info(victim_task);
398*2c2f96dcSApple OSS Distributions 	struct label *bsd_label = NULL;
399*2c2f96dcSApple OSS Distributions 	struct label *label = NULL;
400*2c2f96dcSApple OSS Distributions 
401*2c2f96dcSApple OSS Distributions 	if (p != NULL) {
402*2c2f96dcSApple OSS Distributions 		// Create a label from the still existing bsd process...
403*2c2f96dcSApple OSS Distributions 		label = bsd_label = mac_exc_create_label_for_proc(p);
404*2c2f96dcSApple OSS Distributions 	} else {
405*2c2f96dcSApple OSS Distributions 		// ... otherwise use the crash label on the task.
406*2c2f96dcSApple OSS Distributions 		label = get_task_crash_label(victim_task);
407*2c2f96dcSApple OSS Distributions 	}
408*2c2f96dcSApple OSS Distributions 
409*2c2f96dcSApple OSS Distributions 	if (label == NULL) {
410*2c2f96dcSApple OSS Distributions 		MAC_MACH_UNEXPECTED("mac_exc_action_check_exception_send: no exc_action label for process");
411*2c2f96dcSApple OSS Distributions 		return EPERM;
412*2c2f96dcSApple OSS Distributions 	}
413*2c2f96dcSApple OSS Distributions 
414*2c2f96dcSApple OSS Distributions 	MAC_CHECK(exc_action_check_exception_send, label, action, mac_exc_label(action));
415*2c2f96dcSApple OSS Distributions 
416*2c2f96dcSApple OSS Distributions 	if (bsd_label != NULL) {
417*2c2f96dcSApple OSS Distributions 		mac_exc_free_label(bsd_label);
418*2c2f96dcSApple OSS Distributions 	}
419*2c2f96dcSApple OSS Distributions 
420*2c2f96dcSApple OSS Distributions 	return error;
421*2c2f96dcSApple OSS Distributions }
422*2c2f96dcSApple OSS Distributions 
423*2c2f96dcSApple OSS Distributions int
mac_schedule_telemetry(void)424*2c2f96dcSApple OSS Distributions mac_schedule_telemetry(void)
425*2c2f96dcSApple OSS Distributions {
426*2c2f96dcSApple OSS Distributions 	return telemetry_macf_mark_curthread();
427*2c2f96dcSApple OSS Distributions }
428