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_with_policy(p, IDENT_VALIDATION_PROC_EXACT);
91
92 struct ucred *cred = kauth_cred_get();
93 proc_rele(p);
94
95 MAC_CHECK(proc_check_expose_task_with_flavor, cred, &pident, flavor);
96
97 return error;
98 }
99
100 int
mac_task_check_task_id_token_get_task(struct task * task,mach_task_flavor_t flavor)101 mac_task_check_task_id_token_get_task(struct task *task, mach_task_flavor_t flavor)
102 {
103 int error;
104 struct proc *target_proc = NULL;
105 struct proc_ident *pidentp = NULL;
106 struct proc_ident pident;
107
108 assert(flavor <= TASK_FLAVOR_NAME);
109
110 if (!task_is_a_corpse(task)) {
111 /* only live task has proc */
112 target_proc = mac_task_get_proc(task);
113 if (target_proc == NULL) {
114 return ESRCH;
115 }
116 pident = proc_ident_with_policy(target_proc, IDENT_VALIDATION_PROC_EXACT);
117 pidentp = &pident;
118 proc_rele(target_proc);
119 }
120
121 /* pidentp is NULL for corpse task */
122 MAC_CHECK(proc_check_task_id_token_get_task,
123 current_cached_proc_cred(PROC_NULL), pidentp, flavor);
124
125 return error;
126 }
127
128 int
mac_task_check_get_movable_control_port(void)129 mac_task_check_get_movable_control_port(void)
130 {
131 int error;
132
133 MAC_CHECK(proc_check_get_movable_control_port,
134 current_cached_proc_cred(PROC_NULL));
135
136 return error;
137 }
138
139 /*
140 * This function should only be called from spawn/fork context because,
141 * in general, it is not allowed to reference the proc from get_bsdtask_info
142 * if we only have a task ref. In spawn context, however, we have reference
143 * to both task and proc.
144 */
145 int
mac_task_check_get_movable_control_port_during_spawn(struct task * new_task)146 mac_task_check_get_movable_control_port_during_spawn(struct task *new_task)
147 {
148 int error;
149 kauth_cred_t p_cred;
150
151 p_cred = kauth_cred_proc_ref(get_bsdtask_info(new_task));
152
153 MAC_CHECK(proc_check_get_movable_control_port, p_cred);
154
155 kauth_cred_unref(&p_cred);
156
157 return error;
158 }
159
160 int
mac_task_check_set_host_special_port(struct task * task,int id,struct ipc_port * port)161 mac_task_check_set_host_special_port(struct task *task, int id, struct ipc_port *port)
162 {
163 #pragma unused(task)
164 int error;
165
166 assert(task == current_task());
167 MAC_CHECK(proc_check_set_host_special_port,
168 current_cached_proc_cred(PROC_NULL), id, port);
169
170 return error;
171 }
172
173 int
mac_task_check_set_host_exception_port(struct task * task,unsigned int exception)174 mac_task_check_set_host_exception_port(struct task *task, unsigned int exception)
175 {
176 #pragma unused(task)
177 int error;
178
179 assert(task == current_task());
180 MAC_CHECK(proc_check_set_host_exception_port,
181 current_cached_proc_cred(PROC_NULL), exception);
182
183 return error;
184 }
185
186 int
mac_task_check_get_task_special_port(struct task * task,struct task * target,int which)187 mac_task_check_get_task_special_port(struct task *task, struct task *target, int which)
188 {
189 #pragma unused(task)
190 int error;
191 struct proc *target_proc = NULL;
192 struct proc_ident *pidentp = NULL;
193 struct proc_ident pident;
194
195 assert(task == current_task());
196
197 if (!task_is_a_corpse(target)) {
198 /* only live task has proc */
199 target_proc = mac_task_get_proc(target);
200 if (target_proc == NULL) {
201 return ESRCH;
202 }
203 pident = proc_ident_with_policy(target_proc, IDENT_VALIDATION_PROC_EXACT);
204 pidentp = &pident;
205 proc_rele(target_proc);
206 }
207
208 /* pidentp is NULL for corpse task */
209 MAC_CHECK(proc_check_get_task_special_port,
210 current_cached_proc_cred(PROC_NULL), pidentp, which);
211
212 return error;
213 }
214
215 int
mac_task_check_set_task_special_port(struct task * task,struct task * target,int which,struct ipc_port * port)216 mac_task_check_set_task_special_port(struct task *task, struct task *target, int which, struct ipc_port *port)
217 {
218 #pragma unused(task)
219 int error;
220
221 assert(task == current_task());
222
223 /* disallow setting task special ports for corpse */
224 if (task_is_a_corpse(target)) {
225 return EPERM;
226 }
227
228 struct proc *targetp = mac_task_get_proc(target);
229 if (targetp == NULL) {
230 return ESRCH;
231 }
232
233 struct proc_ident pident = proc_ident_with_policy(targetp, IDENT_VALIDATION_PROC_EXACT);
234 proc_rele(targetp);
235
236 MAC_CHECK(proc_check_set_task_special_port,
237 current_cached_proc_cred(PROC_NULL), &pident, which, port);
238
239 return error;
240 }
241
242 int
mac_task_check_set_task_exception_ports(struct task * task,struct task * target,unsigned int exception_mask,int new_behavior)243 mac_task_check_set_task_exception_ports(struct task *task, struct task *target, unsigned int exception_mask, int new_behavior)
244 {
245 #pragma unused(task)
246 int error = 0;
247 int exception;
248 kauth_cred_t cred = current_cached_proc_cred(PROC_NULL);
249
250 assert(task == current_task());
251
252 /* disallow setting task exception ports for corpse */
253 if (task_is_a_corpse(target)) {
254 return EPERM;
255 }
256
257 struct proc *targetp = mac_task_get_proc(target);
258 if (targetp == NULL) {
259 return ESRCH;
260 }
261
262 struct proc_ident pident = proc_ident_with_policy(targetp, IDENT_VALIDATION_PROC_EXACT);
263 proc_rele(targetp);
264
265 for (exception = FIRST_EXCEPTION; exception < EXC_TYPES_COUNT; exception++) {
266 if (exception_mask & (1 << exception)) {
267 MAC_CHECK(proc_check_set_task_exception_port,
268 cred, &pident, exception, new_behavior);
269 if (error) {
270 break;
271 }
272 }
273 }
274
275 return error;
276 }
277
278 int
mac_task_check_set_thread_exception_ports(struct task * task,struct task * target,unsigned int exception_mask,int new_behavior)279 mac_task_check_set_thread_exception_ports(struct task *task, struct task *target, unsigned int exception_mask, int new_behavior)
280 {
281 #pragma unused(task)
282 int error = 0;
283 int exception;
284 kauth_cred_t cred = current_cached_proc_cred(PROC_NULL);
285
286 assert(task == current_task());
287
288 /* disallow setting thread exception ports for corpse */
289 if (task_is_a_corpse(target)) {
290 return EPERM;
291 }
292
293 struct proc *targetp = mac_task_get_proc(target);
294 if (targetp == NULL) {
295 return ESRCH;
296 }
297
298 struct proc_ident pident = proc_ident_with_policy(targetp, IDENT_VALIDATION_PROC_EXACT);
299 proc_rele(targetp);
300
301 for (exception = FIRST_EXCEPTION; exception < EXC_TYPES_COUNT; exception++) {
302 if (exception_mask & (1 << exception)) {
303 MAC_CHECK(proc_check_set_thread_exception_port,
304 cred, &pident, exception, new_behavior);
305 if (error) {
306 break;
307 }
308 }
309 }
310
311 return error;
312 }
313
314 int
mac_task_check_dyld_process_info_notify_register(void)315 mac_task_check_dyld_process_info_notify_register(void)
316 {
317 int error;
318
319 MAC_CHECK(proc_check_dyld_process_info_notify_register,
320 current_cached_proc_cred(PROC_NULL));
321
322 return error;
323 }
324
325 int
mac_task_check_set_host_exception_ports(struct task * task,unsigned int exception_mask)326 mac_task_check_set_host_exception_ports(struct task *task, unsigned int exception_mask)
327 {
328 #pragma unused(task)
329 int error = 0;
330 int exception;
331 kauth_cred_t cred = current_cached_proc_cred(PROC_NULL);
332
333 assert(task == current_task());
334
335 for (exception = FIRST_EXCEPTION; exception < EXC_TYPES_COUNT; exception++) {
336 if (exception_mask & (1 << exception)) {
337 MAC_CHECK(proc_check_set_host_exception_port,
338 cred, exception);
339 if (error) {
340 break;
341 }
342 }
343 }
344
345 return error;
346 }
347
348 void
mac_thread_userret(struct thread * td)349 mac_thread_userret(struct thread *td)
350 {
351 MAC_PERFORM(thread_userret, td);
352 }
353
354 void
mac_thread_telemetry(struct thread * t,int err,void * data,size_t length)355 mac_thread_telemetry(struct thread *t, int err, void *data, size_t length)
356 {
357 MAC_PERFORM(thread_telemetry, t, err, data, length);
358 }
359
360 void
mac_proc_notify_exec_complete(struct proc * proc)361 mac_proc_notify_exec_complete(struct proc *proc)
362 {
363 thread_t thread = current_thread();
364
365 /*
366 * Since this MAC hook was designed to support upcalls, make sure the hook
367 * is called with kernel importance propagation enabled so any daemons
368 * can get any appropriate importance donations.
369 */
370 thread_enable_send_importance(thread, TRUE);
371 MAC_PERFORM(proc_notify_exec_complete, proc);
372 thread_enable_send_importance(thread, FALSE);
373 }
374
375 /**** Exception Policy
376 *
377 * Note that the functions below do not fully follow the usual convention for mac policy functions
378 * in the kernel. Besides avoiding confusion in how the mac function names are mixed with the actual
379 * policy function names, we diverge because the exception policy is somewhat special:
380 * It is used in places where allocation and association must be separate, and its labels do not
381 * only belong to one type of object as usual, but to two (on exception actions and on tasks as
382 * crash labels).
383 */
384
385 struct label *
mac_exc_label(struct exception_action * action)386 mac_exc_label(struct exception_action *action)
387 {
388 return mac_label_verify(&action->label);
389 }
390
391 void
mac_exc_set_label(struct exception_action * action,struct label * label)392 mac_exc_set_label(struct exception_action *action, struct label *label)
393 {
394 action->label = label;
395 }
396
397 // Label allocation and deallocation, may sleep.
398
399 struct label *
mac_exc_create_label(struct exception_action * action)400 mac_exc_create_label(struct exception_action *action)
401 {
402 return mac_labelzone_alloc_for_owner(action ? &action->label : NULL, MAC_WAITOK, ^(struct label *label) {
403 // Policy initialization of the label, typically performs allocations as well.
404 // (Unless the policy's full data really fits into a pointer size.)
405 MAC_PERFORM(exc_action_label_init, label);
406 });
407 }
408
409 void
mac_exc_free_label(struct label * label)410 mac_exc_free_label(struct label *label)
411 {
412 MAC_PERFORM(exc_action_label_destroy, label);
413 mac_labelzone_free(label);
414 }
415
416 // Action label initialization and teardown, may sleep.
417
418 void
mac_exc_associate_action_label(struct exception_action * action,struct label * label)419 mac_exc_associate_action_label(struct exception_action *action, struct label *label)
420 {
421 mac_exc_set_label(action, label);
422 MAC_PERFORM(exc_action_label_associate, action, mac_exc_label(action));
423 }
424
425 void
mac_exc_free_action_label(struct exception_action * action)426 mac_exc_free_action_label(struct exception_action *action)
427 {
428 mac_exc_free_label(mac_exc_label(action));
429 mac_exc_set_label(action, NULL);
430 }
431
432 // Action label update and inheritance, may NOT sleep and must be quick.
433
434 int
mac_exc_update_action_label(struct exception_action * action,struct label * newlabel)435 mac_exc_update_action_label(struct exception_action *action,
436 struct label *newlabel)
437 {
438 int error;
439
440 MAC_CHECK(exc_action_label_update, action, mac_exc_label(action), newlabel);
441
442 return error;
443 }
444
445 int
mac_exc_inherit_action_label(struct exception_action * parent,struct exception_action * child)446 mac_exc_inherit_action_label(struct exception_action *parent,
447 struct exception_action *child)
448 {
449 return mac_exc_update_action_label(child, mac_exc_label(parent));
450 }
451
452 int
mac_exc_update_task_crash_label(struct task * task,struct label * label)453 mac_exc_update_task_crash_label(struct task *task, struct label *label)
454 {
455 int error;
456
457 assert(task != kernel_task);
458
459 struct label *crash_label = get_task_crash_label(task);
460
461 MAC_CHECK(exc_action_label_update, NULL, crash_label, label);
462
463 return error;
464 }
465
466 // Process label creation, may sleep.
467
468 struct label *
mac_exc_create_label_for_proc(struct proc * proc)469 mac_exc_create_label_for_proc(struct proc *proc)
470 {
471 struct label *label = mac_exc_create_label(NULL);
472 MAC_PERFORM(exc_action_label_populate, label, proc);
473 return label;
474 }
475
476 struct label *
mac_exc_create_label_for_current_proc(void)477 mac_exc_create_label_for_current_proc(void)
478 {
479 return mac_exc_create_label_for_proc(current_proc());
480 }
481
482 // Exception handler policy checking, may sleep.
483
484 int
mac_exc_action_check_exception_send(struct task * victim_task,struct exception_action * action)485 mac_exc_action_check_exception_send(struct task *victim_task, struct exception_action *action)
486 {
487 int error = 0;
488
489 struct proc *p = get_bsdtask_info(victim_task);
490 struct label *bsd_label = NULL;
491 struct label *label = NULL;
492
493 if (p != NULL) {
494 // Create a label from the still existing bsd process...
495 label = bsd_label = mac_exc_create_label_for_proc(p);
496 } else {
497 // ... otherwise use the crash label on the task.
498 label = get_task_crash_label(victim_task);
499 }
500
501 if (label == NULL) {
502 MAC_MACH_UNEXPECTED("mac_exc_action_check_exception_send: no exc_action label for process");
503 return EPERM;
504 }
505
506 MAC_CHECK(exc_action_check_exception_send, label, action, mac_exc_label(action));
507
508 if (bsd_label != NULL) {
509 mac_exc_free_label(bsd_label);
510 }
511
512 return error;
513 }
514
515 int
mac_schedule_telemetry(void)516 mac_schedule_telemetry(void)
517 {
518 return telemetry_macf_mark_curthread();
519 }
520