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