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