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