]> git.saurik.com Git - apple/xnu.git/blob - security/mac_process.c
929d8107c69e0094213ac092b35497017ded4107
[apple/xnu.git] / security / mac_process.c
1 /*
2 * Copyright (c) 2007-2010 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 /*-
30 * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson
31 * Copyright (c) 2001 Ilmar S. Habibulin
32 * Copyright (c) 2001, 2002, 2003, 2004 Networks Associates Technology, Inc.
33 *
34 * This software was developed by Robert Watson and Ilmar Habibulin for the
35 * TrustedBSD Project.
36 *
37 * This software was developed for the FreeBSD Project in part by Network
38 * Associates Laboratories, the Security Research Division of Network
39 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
40 * as part of the DARPA CHATS research program.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 *
63 */
64
65 #include <string.h>
66 #include <sys/param.h>
67 #include <sys/ucred.h>
68 #include <sys/malloc.h>
69 #include <sys/sbuf.h>
70 #include <sys/vnode.h>
71 #include <sys/proc.h>
72 #include <sys/proc_internal.h>
73 #include <sys/kauth.h>
74 #include <sys/imgact.h>
75 #include <mach/mach_types.h>
76
77 #include <security/mac_internal.h>
78 #include <security/mac_mach_internal.h>
79
80 #include <bsd/security/audit/audit.h>
81
82 struct label *
83 mac_cred_label_alloc(void)
84 {
85 struct label *label;
86
87 label = mac_labelzone_alloc(MAC_WAITOK);
88 if (label == NULL)
89 return (NULL);
90 MAC_PERFORM(cred_label_init, label);
91 return (label);
92 }
93
94 void
95 mac_cred_label_init(struct ucred *cred)
96 {
97 cred->cr_label = mac_cred_label_alloc();
98 }
99
100 void
101 mac_cred_label_free(struct label *label)
102 {
103 MAC_PERFORM(cred_label_destroy, label);
104 mac_labelzone_free(label);
105 }
106
107 int
108 mac_cred_label_compare(struct label *a, struct label *b)
109 {
110 return (bcmp(a, b, sizeof (*a)) == 0);
111 }
112
113 int
114 mac_cred_label_externalize_audit(struct proc *p, struct mac *mac)
115 {
116 kauth_cred_t cr;
117 int error;
118
119 cr = kauth_cred_proc_ref(p);
120
121 error = MAC_EXTERNALIZE_AUDIT(cred, cr->cr_label,
122 mac->m_string, mac->m_buflen);
123
124 kauth_cred_unref(&cr);
125 return (error);
126 }
127
128 void
129 mac_cred_label_destroy(kauth_cred_t cred)
130 {
131
132 mac_cred_label_free(cred->cr_label);
133 cred->cr_label = NULL;
134 }
135
136 int
137 mac_cred_label_externalize(struct label *label, char *elements,
138 char *outbuf, size_t outbuflen, int flags __unused)
139 {
140 int error = 0;
141
142 error = MAC_EXTERNALIZE(cred, label, elements, outbuf, outbuflen);
143
144 return (error);
145 }
146
147 int
148 mac_cred_label_internalize(struct label *label, char *string)
149 {
150 int error;
151
152 error = MAC_INTERNALIZE(cred, label, string);
153
154 return (error);
155 }
156
157 /*
158 * By default, fork just adds a reference to the parent
159 * credential. Policies may need to know about this reference
160 * if they are tracking exit calls to know when to free the
161 * label.
162 */
163 void
164 mac_cred_label_associate_fork(kauth_cred_t cred, proc_t proc)
165 {
166 MAC_PERFORM(cred_label_associate_fork, cred, proc);
167 }
168
169 /*
170 * Initialize MAC label for the first kernel process, from which other
171 * kernel processes and threads are spawned.
172 */
173 void
174 mac_cred_label_associate_kernel(kauth_cred_t cred)
175 {
176
177 MAC_PERFORM(cred_label_associate_kernel, cred);
178 }
179
180 /*
181 * Initialize MAC label for the first userland process, from which other
182 * userland processes and threads are spawned.
183 */
184 void
185 mac_cred_label_associate_user(kauth_cred_t cred)
186 {
187
188 MAC_PERFORM(cred_label_associate_user, cred);
189 }
190
191 /*
192 * When a new process is created, its label must be initialized. Generally,
193 * this involves inheritence from the parent process, modulo possible
194 * deltas. This function allows that processing to take place.
195 */
196 void
197 mac_cred_label_associate(struct ucred *parent_cred, struct ucred *child_cred)
198 {
199
200 MAC_PERFORM(cred_label_associate, parent_cred, child_cred);
201 }
202
203 int
204 mac_execve_enter(user_addr_t mac_p, struct image_params *imgp)
205 {
206 struct user_mac mac;
207 struct label *execlabel;
208 char *buffer;
209 int error;
210 size_t ulen;
211
212 if (mac_p == USER_ADDR_NULL)
213 return (0);
214
215 if (IS_64BIT_PROCESS(current_proc())) {
216 struct user64_mac mac64;
217 error = copyin(mac_p, &mac64, sizeof(mac64));
218 mac.m_buflen = mac64.m_buflen;
219 mac.m_string = mac64.m_string;
220 } else {
221 struct user32_mac mac32;
222 error = copyin(mac_p, &mac32, sizeof(mac32));
223 mac.m_buflen = mac32.m_buflen;
224 mac.m_string = mac32.m_string;
225 }
226 if (error)
227 return (error);
228
229 error = mac_check_structmac_consistent(&mac);
230 if (error)
231 return (error);
232
233 execlabel = mac_cred_label_alloc();
234 MALLOC(buffer, char *, mac.m_buflen, M_MACTEMP, M_WAITOK);
235 error = copyinstr(CAST_USER_ADDR_T(mac.m_string), buffer, mac.m_buflen, &ulen);
236 if (error)
237 goto out;
238 AUDIT_ARG(mac_string, buffer);
239
240 error = mac_cred_label_internalize(execlabel, buffer);
241 out:
242 if (error) {
243 mac_cred_label_free(execlabel);
244 execlabel = NULL;
245 }
246 imgp->ip_execlabelp = execlabel;
247 FREE(buffer, M_MACTEMP);
248 return (error);
249 }
250
251 /*
252 * When the subject's label changes, it may require revocation of privilege
253 * to mapped objects. This can't be done on-the-fly later with a unified
254 * buffer cache.
255 *
256 * XXX: CRF_MAC_ENFORCE should be in a kauth_cred_t field, rather
257 * XXX: than a posix_cred_t field.
258 */
259 void
260 mac_cred_label_update(kauth_cred_t cred, struct label *newlabel)
261 {
262 posix_cred_t pcred = posix_cred_get(cred);
263
264 /* force label to be part of "matching" for credential */
265 pcred->cr_flags |= CRF_MAC_ENFORCE;
266
267 /* inform the policies of the update */
268 MAC_PERFORM(cred_label_update, cred, newlabel);
269 }
270
271 int
272 mac_cred_check_label_update(kauth_cred_t cred, struct label *newlabel)
273 {
274 int error;
275
276 if (!mac_proc_enforce)
277 return (0);
278
279 MAC_CHECK(cred_check_label_update, cred, newlabel);
280
281 return (error);
282 }
283
284 int
285 mac_cred_check_visible(kauth_cred_t u1, kauth_cred_t u2)
286 {
287 int error;
288
289
290
291 if (!mac_proc_enforce)
292 return (0);
293
294
295
296 MAC_CHECK(cred_check_visible, u1, u2);
297
298
299 return (error);
300 }
301
302 /*
303 * called with process locked.
304 */
305 void mac_proc_set_enforce(proc_t p, int enforce_flags)
306 {
307 p->p_mac_enforce |= enforce_flags;
308 }
309
310 int
311 mac_proc_check_debug(proc_t curp, struct proc *proc)
312 {
313 kauth_cred_t cred;
314 int error;
315
316
317
318 if (!mac_proc_enforce ||
319 !mac_proc_check_enforce(curp, MAC_PROC_ENFORCE))
320 return (0);
321
322 cred = kauth_cred_proc_ref(curp);
323 MAC_CHECK(proc_check_debug, cred, proc);
324 kauth_cred_unref(&cred);
325
326 return (error);
327 }
328
329 int
330 mac_proc_check_fork(proc_t curp)
331 {
332 kauth_cred_t cred;
333 int error;
334
335 if (!mac_proc_enforce ||
336 !mac_proc_check_enforce(curp, MAC_PROC_ENFORCE))
337 return (0);
338
339 cred = kauth_cred_proc_ref(curp);
340 MAC_CHECK(proc_check_fork, cred, curp);
341 kauth_cred_unref(&cred);
342
343 return (error);
344 }
345
346 int
347 mac_proc_check_get_task_name(struct ucred *cred, struct proc *p)
348 {
349 int error;
350
351 MAC_CHECK(proc_check_get_task_name, cred, p);
352
353 return (error);
354 }
355
356 int
357 mac_proc_check_get_task(struct ucred *cred, struct proc *p)
358 {
359 int error;
360
361 MAC_CHECK(proc_check_get_task, cred, p);
362
363 return (error);
364 }
365
366 int
367 mac_proc_check_inherit_ipc_ports(struct proc *p, struct vnode *cur_vp, off_t cur_offset, struct vnode *img_vp, off_t img_offset, struct vnode *scriptvp)
368 {
369 int error;
370
371 MAC_CHECK(proc_check_inherit_ipc_ports, p, cur_vp, cur_offset, img_vp, img_offset, scriptvp);
372
373 return (error);
374 }
375
376 /*
377 * The type of maxprot in proc_check_map_anon must be equivalent to vm_prot_t
378 * (defined in <mach/vm_prot.h>). mac_policy.h does not include any header
379 * files, so cannot use the typedef itself.
380 */
381 int
382 mac_proc_check_map_anon(proc_t proc, user_addr_t u_addr,
383 user_size_t u_size, int prot, int flags, int *maxprot)
384 {
385 kauth_cred_t cred;
386 int error;
387
388 if (!mac_vm_enforce ||
389 !mac_proc_check_enforce(proc, MAC_VM_ENFORCE))
390 return (0);
391
392 cred = kauth_cred_proc_ref(proc);
393 MAC_CHECK(proc_check_map_anon, proc, cred, u_addr, u_size, prot, flags, maxprot);
394 kauth_cred_unref(&cred);
395
396 return (error);
397 }
398
399 int
400 mac_proc_check_mprotect(proc_t proc,
401 user_addr_t addr, user_size_t size, int prot)
402 {
403 kauth_cred_t cred;
404 int error;
405
406 if (!mac_vm_enforce ||
407 !mac_proc_check_enforce(proc, MAC_VM_ENFORCE))
408 return (0);
409
410 cred = kauth_cred_proc_ref(proc);
411 MAC_CHECK(proc_check_mprotect, cred, proc, addr, size, prot);
412 kauth_cred_unref(&cred);
413
414 return (error);
415 }
416
417 int
418 mac_proc_check_run_cs_invalid(proc_t proc)
419 {
420 int error;
421
422 if (!mac_vm_enforce) return (0);
423
424 MAC_CHECK(proc_check_run_cs_invalid, proc);
425
426 return (error);
427 }
428
429 int
430 mac_proc_check_sched(proc_t curp, struct proc *proc)
431 {
432 kauth_cred_t cred;
433 int error;
434
435
436
437 if (!mac_proc_enforce ||
438 !mac_proc_check_enforce(curp, MAC_PROC_ENFORCE))
439 return (0);
440
441 cred = kauth_cred_proc_ref(curp);
442 MAC_CHECK(proc_check_sched, cred, proc);
443 kauth_cred_unref(&cred);
444
445 return (error);
446 }
447
448 int
449 mac_proc_check_signal(proc_t curp, struct proc *proc, int signum)
450 {
451 kauth_cred_t cred;
452 int error;
453
454
455
456 if (!mac_proc_enforce ||
457 !mac_proc_check_enforce(curp, MAC_PROC_ENFORCE))
458 return (0);
459
460 cred = kauth_cred_proc_ref(curp);
461 MAC_CHECK(proc_check_signal, cred, proc, signum);
462 kauth_cred_unref(&cred);
463
464 return (error);
465 }
466
467 int
468 mac_proc_check_wait(proc_t curp, struct proc *proc)
469 {
470 kauth_cred_t cred;
471 int error;
472
473
474
475 if (!mac_proc_enforce ||
476 !mac_proc_check_enforce(curp, MAC_PROC_ENFORCE))
477 return (0);
478
479 cred = kauth_cred_proc_ref(curp);
480 MAC_CHECK(proc_check_wait, cred, proc);
481 kauth_cred_unref(&cred);
482
483 return (error);
484 }
485
486 #if CONFIG_LCTX
487 /*
488 * Login Context
489 */
490
491 int
492 mac_proc_check_setlcid (struct proc *p0, struct proc *p,
493 pid_t pid, pid_t lcid)
494 {
495 int error;
496
497 if (!mac_proc_enforce ||
498 !mac_proc_check_enforce(p0, MAC_PROC_ENFORCE))
499 return (0);
500
501 MAC_CHECK(proc_check_setlcid, p0, p, pid, lcid);
502 return (error);
503 }
504
505 int
506 mac_proc_check_getlcid (struct proc *p0, struct proc *p, pid_t pid)
507 {
508 int error;
509
510 if (!mac_proc_enforce ||
511 !mac_proc_check_enforce(p0, MAC_PROC_ENFORCE))
512 return (0);
513
514 MAC_CHECK(proc_check_getlcid, p0, p, pid);
515 return (error);
516 }
517
518 void
519 mac_lctx_notify_create (struct proc *p, struct lctx *l)
520 {
521 MAC_PERFORM(lctx_notify_create, p, l);
522 }
523
524 void
525 mac_lctx_notify_join (struct proc *p, struct lctx *l)
526 {
527 MAC_PERFORM(lctx_notify_join, p, l);
528 }
529
530 void
531 mac_lctx_notify_leave (struct proc *p, struct lctx *l)
532 {
533 MAC_PERFORM(lctx_notify_leave, p, l);
534 }
535
536 struct label *
537 mac_lctx_label_alloc(void)
538 {
539 struct label *label;
540
541 label = mac_labelzone_alloc(MAC_WAITOK);
542 if (label == NULL)
543 return (NULL);
544 MAC_PERFORM(lctx_label_init, label);
545 return (label);
546 }
547
548 void
549 mac_lctx_label_free(struct label *label)
550 {
551
552 MAC_PERFORM(lctx_label_destroy, label);
553 mac_labelzone_free(label);
554 }
555
556 int
557 mac_lctx_label_externalize(struct label *label, char *elements,
558 char *outbuf, size_t outbuflen)
559 {
560 int error;
561
562 error = MAC_EXTERNALIZE(lctx, label, elements, outbuf, outbuflen);
563
564 return (error);
565 }
566
567 int
568 mac_lctx_label_internalize(struct label *label, char *string)
569 {
570 int error;
571
572 error = MAC_INTERNALIZE(lctx, label, string);
573
574 return (error);
575 }
576
577 void
578 mac_lctx_label_update(struct lctx *l, struct label *newlabel)
579 {
580
581 MAC_PERFORM(lctx_label_update, l, newlabel);
582 }
583
584 int
585 mac_lctx_check_label_update(struct lctx *l, struct label *newlabel)
586 {
587 int error;
588
589 MAC_CHECK(lctx_check_label_update, l, newlabel);
590
591 return (error);
592 }
593 #endif /* LCTX */
594
595 int
596 mac_proc_check_suspend_resume(proc_t curp, int sr)
597 {
598 kauth_cred_t cred;
599 int error;
600
601 if (!mac_proc_enforce ||
602 !mac_proc_check_enforce(curp, MAC_PROC_ENFORCE))
603 return (0);
604
605 cred = kauth_cred_proc_ref(curp);
606 MAC_CHECK(proc_check_suspend_resume, cred, curp, sr);
607 kauth_cred_unref(&cred);
608
609 return (error);
610 }
611
612 int
613 mac_proc_check_ledger(proc_t curp, proc_t proc, int ledger_op)
614 {
615 kauth_cred_t cred;
616 int error = 0;
617
618 if (!mac_proc_enforce ||
619 !mac_proc_check_enforce(curp, MAC_PROC_ENFORCE))
620 return (0);
621
622 cred = kauth_cred_proc_ref(curp);
623 MAC_CHECK(proc_check_ledger, cred, proc, ledger_op);
624 kauth_cred_unref(&cred);
625
626 return (error);
627 }
628
629 int
630 mac_proc_check_cpumon(proc_t curp)
631 {
632 kauth_cred_t cred;
633 int error = 0;
634
635 if (!mac_proc_enforce ||
636 !mac_proc_check_enforce(curp, MAC_PROC_ENFORCE))
637 return (0);
638
639 cred = kauth_cred_proc_ref(curp);
640 MAC_CHECK(proc_check_cpumon, cred);
641 kauth_cred_unref(&cred);
642
643 return (error);
644 }
645
646 int
647 mac_proc_check_proc_info(proc_t curp, proc_t target, int callnum, int flavor)
648 {
649 kauth_cred_t cred;
650 int error = 0;
651
652 if (!mac_proc_enforce ||
653 !mac_proc_check_enforce(curp, MAC_PROC_ENFORCE))
654 return (0);
655
656 cred = kauth_cred_proc_ref(curp);
657 MAC_CHECK(proc_check_proc_info, cred, target, callnum, flavor);
658 kauth_cred_unref(&cred);
659
660 return (error);
661 }
662
663 struct label *
664 mac_thread_label_alloc(void)
665 {
666 struct label *label;
667
668 label = mac_labelzone_alloc(MAC_WAITOK);
669 if (label == NULL)
670 return (NULL);
671 MAC_PERFORM(thread_label_init, label);
672 return (label);
673 }
674
675 void
676 mac_thread_label_init(struct uthread *uthread)
677 {
678 uthread->uu_label = mac_thread_label_alloc();
679 }
680
681 void
682 mac_thread_label_free(struct label *label)
683 {
684 MAC_PERFORM(thread_label_destroy, label);
685 mac_labelzone_free(label);
686 }
687
688 void
689 mac_thread_label_destroy(struct uthread *uthread)
690 {
691
692 mac_thread_label_free(uthread->uu_label);
693 uthread->uu_label = NULL;
694 }
695
696 void
697 mac_thread_userret(struct thread *td)
698 {
699
700 MAC_PERFORM(thread_userret, td);
701 }
702
703 struct label *
704 mac_thread_get_uthreadlabel(struct uthread *uthread)
705 {
706
707 return (uthread->uu_label);
708 }
709
710 struct label *
711 mac_thread_get_threadlabel(struct thread *thread)
712 {
713 struct uthread *uthread = get_bsdthread_info(thread);
714
715 return (mac_thread_get_uthreadlabel(uthread));
716 }