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