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