]> git.saurik.com Git - apple/xnu.git/blob - security/mac_process.c
xnu-7195.60.75.tar.gz
[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 #include <kern/task.h>
77
78 #include <security/mac_internal.h>
79 #include <security/mac_mach_internal.h>
80
81 #include <bsd/security/audit/audit.h>
82
83 struct label *
84 mac_cred_label_alloc(void)
85 {
86 struct label *label;
87
88 label = mac_labelzone_alloc(MAC_WAITOK);
89 if (label == NULL) {
90 return NULL;
91 }
92 MAC_PERFORM(cred_label_init, label);
93 return label;
94 }
95
96 void
97 mac_cred_label_init(struct ucred *cred)
98 {
99 cred->cr_label = mac_cred_label_alloc();
100 }
101
102 void
103 mac_cred_label_free(struct label *label)
104 {
105 MAC_PERFORM(cred_label_destroy, label);
106 mac_labelzone_free(label);
107 }
108
109 int
110 mac_cred_label_compare(struct label *a, struct label *b)
111 {
112 return bcmp(a, b, sizeof(*a)) == 0;
113 }
114
115 int
116 mac_cred_label_externalize_audit(struct proc *p, struct mac *mac)
117 {
118 kauth_cred_t cr;
119 int error;
120
121 cr = kauth_cred_proc_ref(p);
122
123 error = MAC_EXTERNALIZE_AUDIT(cred, cr->cr_label,
124 mac->m_string, mac->m_buflen);
125
126 kauth_cred_unref(&cr);
127 return error;
128 }
129
130 void
131 mac_cred_label_destroy(kauth_cred_t cred)
132 {
133 mac_cred_label_free(cred->cr_label);
134 cred->cr_label = NULL;
135 }
136
137 int
138 mac_cred_label_externalize(struct label *label, char *elements,
139 char *outbuf, size_t outbuflen, int flags __unused)
140 {
141 int error = 0;
142
143 error = MAC_EXTERNALIZE(cred, label, elements, outbuf, outbuflen);
144
145 return error;
146 }
147
148 int
149 mac_cred_label_internalize(struct label *label, char *string)
150 {
151 int error;
152
153 error = MAC_INTERNALIZE(cred, label, string);
154
155 return error;
156 }
157
158 /*
159 * By default, fork just adds a reference to the parent
160 * credential. Policies may need to know about this reference
161 * if they are tracking exit calls to know when to free the
162 * label.
163 */
164 void
165 mac_cred_label_associate_fork(kauth_cred_t cred, proc_t proc)
166 {
167 MAC_PERFORM(cred_label_associate_fork, cred, proc);
168 }
169
170 /*
171 * Initialize MAC label for the first kernel process, from which other
172 * kernel processes and threads are spawned.
173 */
174 void
175 mac_cred_label_associate_kernel(kauth_cred_t cred)
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 MAC_PERFORM(cred_label_associate_user, cred);
188 }
189
190 /*
191 * When a new process is created, its label must be initialized. Generally,
192 * this involves inheritence from the parent process, modulo possible
193 * deltas. This function allows that processing to take place.
194 */
195 void
196 mac_cred_label_associate(struct ucred *parent_cred, struct ucred *child_cred)
197 {
198 MAC_PERFORM(cred_label_associate, parent_cred, child_cred);
199 }
200
201 int
202 mac_execve_enter(user_addr_t mac_p, struct image_params *imgp)
203 {
204 struct user_mac mac;
205 struct label *execlabel;
206 char *buffer;
207 int error;
208 size_t ulen;
209
210 if (mac_p == USER_ADDR_NULL) {
211 return 0;
212 }
213
214 if (IS_64BIT_PROCESS(current_proc())) {
215 struct user64_mac mac64;
216 error = copyin(mac_p, &mac64, sizeof(mac64));
217 mac.m_buflen = mac64.m_buflen;
218 mac.m_string = mac64.m_string;
219 } else {
220 struct user32_mac mac32;
221 error = copyin(mac_p, &mac32, sizeof(mac32));
222 mac.m_buflen = mac32.m_buflen;
223 mac.m_string = mac32.m_string;
224 }
225 if (error) {
226 return error;
227 }
228
229 error = mac_check_structmac_consistent(&mac);
230 if (error) {
231 return error;
232 }
233
234 execlabel = mac_cred_label_alloc();
235 MALLOC(buffer, char *, mac.m_buflen, M_MACTEMP, M_WAITOK);
236 error = copyinstr(CAST_USER_ADDR_T(mac.m_string), buffer, mac.m_buflen, &ulen);
237 if (error) {
238 goto out;
239 }
240 AUDIT_ARG(mac_string, buffer);
241
242 error = mac_cred_label_internalize(execlabel, buffer);
243 out:
244 if (error) {
245 mac_cred_label_free(execlabel);
246 execlabel = NULL;
247 }
248 imgp->ip_execlabelp = execlabel;
249 FREE(buffer, M_MACTEMP);
250 return error;
251 }
252
253 /*
254 * When the subject's label changes, it may require revocation of privilege
255 * to mapped objects. This can't be done on-the-fly later with a unified
256 * buffer cache.
257 *
258 * XXX: CRF_MAC_ENFORCE should be in a kauth_cred_t field, rather
259 * XXX: than a posix_cred_t field.
260 */
261 void
262 mac_cred_label_update(kauth_cred_t cred, struct label *newlabel)
263 {
264 posix_cred_t pcred = posix_cred_get(cred);
265
266 /* force label to be part of "matching" for credential */
267 pcred->cr_flags |= CRF_MAC_ENFORCE;
268
269 /* inform the policies of the update */
270 MAC_PERFORM(cred_label_update, cred, newlabel);
271 }
272
273 int
274 mac_cred_check_label_update(kauth_cred_t cred, struct label *newlabel)
275 {
276 int error;
277
278 #if SECURITY_MAC_CHECK_ENFORCE
279 /* 21167099 - only check if we allow write */
280 if (!mac_proc_enforce) {
281 return 0;
282 }
283 #endif
284
285 MAC_CHECK(cred_check_label_update, cred, newlabel);
286
287 return error;
288 }
289
290 int
291 mac_cred_check_visible(kauth_cred_t u1, kauth_cred_t u2)
292 {
293 int error;
294
295 #if SECURITY_MAC_CHECK_ENFORCE
296 /* 21167099 - only check if we allow write */
297 if (!mac_proc_enforce) {
298 return 0;
299 }
300 #endif
301
302 MAC_CHECK(cred_check_visible, u1, u2);
303
304 return error;
305 }
306
307 int
308 mac_proc_check_debug(proc_ident_t tracing_ident, kauth_cred_t tracing_cred, proc_ident_t traced_ident)
309 {
310 int error;
311 bool enforce;
312 proc_t tracingp;
313
314 #if SECURITY_MAC_CHECK_ENFORCE
315 /* 21167099 - only check if we allow write */
316 if (!mac_proc_enforce) {
317 return 0;
318 }
319 #endif
320 /*
321 * Once all mac hooks adopt proc_ident_t, finding proc_t and releasing
322 * it below should go to mac_proc_check_enforce().
323 */
324 if ((tracingp = proc_find_ident(tracing_ident)) == PROC_NULL) {
325 return ESRCH;
326 }
327 enforce = mac_proc_check_enforce(tracingp);
328 proc_rele(tracingp);
329
330 if (!enforce) {
331 return 0;
332 }
333 MAC_CHECK(proc_check_debug, tracing_cred, traced_ident);
334
335 return error;
336 }
337
338 int
339 mac_proc_check_dump_core(struct proc *proc)
340 {
341 int error;
342
343 #if SECURITY_MAC_CHECK_ENFORCE
344 /* 21167099 - only check if we allow write */
345 if (!mac_proc_enforce) {
346 return 0;
347 }
348 #endif
349 if (!mac_proc_check_enforce(proc)) {
350 return 0;
351 }
352
353 MAC_CHECK(proc_check_dump_core, proc);
354
355 return error;
356 }
357
358 int
359 mac_proc_check_remote_thread_create(struct task *task, int flavor, thread_state_t new_state, mach_msg_type_number_t new_state_count)
360 {
361 proc_t curp = current_proc();
362 proc_t proc;
363 kauth_cred_t cred;
364 int error;
365
366 #if SECURITY_MAC_CHECK_ENFORCE
367 /* 21167099 - only check if we allow write */
368 if (!mac_proc_enforce) {
369 return 0;
370 }
371 #endif
372 if (!mac_proc_check_enforce(curp)) {
373 return 0;
374 }
375
376 proc = proc_find(task_pid(task));
377 if (proc == PROC_NULL) {
378 return ESRCH;
379 }
380
381 cred = kauth_cred_proc_ref(curp);
382 MAC_CHECK(proc_check_remote_thread_create, cred, proc, flavor, new_state, new_state_count);
383 kauth_cred_unref(&cred);
384 proc_rele(proc);
385
386 return error;
387 }
388
389 int
390 mac_proc_check_fork(proc_t curp)
391 {
392 kauth_cred_t cred;
393 int error;
394
395 #if SECURITY_MAC_CHECK_ENFORCE
396 /* 21167099 - only check if we allow write */
397 if (!mac_proc_enforce) {
398 return 0;
399 }
400 #endif
401 if (!mac_proc_check_enforce(curp)) {
402 return 0;
403 }
404
405 cred = kauth_cred_proc_ref(curp);
406 MAC_CHECK(proc_check_fork, cred, curp);
407 kauth_cred_unref(&cred);
408
409 return error;
410 }
411
412 int
413 mac_proc_check_get_task_name(struct ucred *cred, proc_ident_t pident)
414 {
415 int error;
416
417 MAC_CHECK(proc_check_get_task_name, cred, pident);
418
419 return error;
420 }
421
422 int
423 mac_proc_check_get_task(struct ucred *cred, proc_ident_t pident)
424 {
425 int error;
426
427 MAC_CHECK(proc_check_get_task, cred, pident);
428
429 return error;
430 }
431
432 int
433 mac_proc_check_expose_task(struct ucred *cred, proc_ident_t pident)
434 {
435 int error;
436
437 MAC_CHECK(proc_check_expose_task, cred, pident);
438
439 return error;
440 }
441
442 int
443 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)
444 {
445 int error;
446
447 MAC_CHECK(proc_check_inherit_ipc_ports, p, cur_vp, cur_offset, img_vp, img_offset, scriptvp);
448
449 return error;
450 }
451
452 /*
453 * The type of maxprot in proc_check_map_anon must be equivalent to vm_prot_t
454 * (defined in <mach/vm_prot.h>). mac_policy.h does not include any header
455 * files, so cannot use the typedef itself.
456 */
457 int
458 mac_proc_check_map_anon(proc_t proc, user_addr_t u_addr,
459 user_size_t u_size, int prot, int flags, int *maxprot)
460 {
461 kauth_cred_t cred;
462 int error;
463
464 #if SECURITY_MAC_CHECK_ENFORCE
465 /* 21167099 - only check if we allow write */
466 if (!mac_vm_enforce) {
467 return 0;
468 }
469 #endif
470 if (!mac_proc_check_enforce(proc)) {
471 return 0;
472 }
473
474 cred = kauth_cred_proc_ref(proc);
475 MAC_CHECK(proc_check_map_anon, proc, cred, u_addr, u_size, prot, flags, maxprot);
476 kauth_cred_unref(&cred);
477
478 return error;
479 }
480
481 int
482 mac_proc_check_mprotect(proc_t proc,
483 user_addr_t addr, user_size_t size, int prot)
484 {
485 kauth_cred_t cred;
486 int error;
487
488 #if SECURITY_MAC_CHECK_ENFORCE
489 /* 21167099 - only check if we allow write */
490 if (!mac_vm_enforce) {
491 return 0;
492 }
493 #endif
494 if (!mac_proc_check_enforce(proc)) {
495 return 0;
496 }
497
498 cred = kauth_cred_proc_ref(proc);
499 MAC_CHECK(proc_check_mprotect, cred, proc, addr, size, prot);
500 kauth_cred_unref(&cred);
501
502 return error;
503 }
504
505 int
506 mac_proc_check_run_cs_invalid(proc_t proc)
507 {
508 int error;
509
510 #if SECURITY_MAC_CHECK_ENFORCE
511 /* 21167099 - only check if we allow write */
512 if (!mac_vm_enforce) {
513 return 0;
514 }
515 #endif
516
517 MAC_CHECK(proc_check_run_cs_invalid, proc);
518
519 return error;
520 }
521
522 void
523 mac_proc_notify_cs_invalidated(proc_t proc)
524 {
525 MAC_PERFORM(proc_notify_cs_invalidated, proc);
526 }
527
528 int
529 mac_proc_check_sched(proc_t curp, struct proc *proc)
530 {
531 kauth_cred_t cred;
532 int error;
533
534 #if SECURITY_MAC_CHECK_ENFORCE
535 /* 21167099 - only check if we allow write */
536 if (!mac_proc_enforce) {
537 return 0;
538 }
539 #endif
540 if (!mac_proc_check_enforce(curp)) {
541 return 0;
542 }
543
544 cred = kauth_cred_proc_ref(curp);
545 MAC_CHECK(proc_check_sched, cred, proc);
546 kauth_cred_unref(&cred);
547
548 return error;
549 }
550
551 int
552 mac_proc_check_signal(proc_t curp, struct proc *proc, int signum)
553 {
554 kauth_cred_t cred;
555 int error;
556
557 #if SECURITY_MAC_CHECK_ENFORCE
558 /* 21167099 - only check if we allow write */
559 if (!mac_proc_enforce) {
560 return 0;
561 }
562 #endif
563 if (!mac_proc_check_enforce(curp)) {
564 return 0;
565 }
566
567 cred = kauth_cred_proc_ref(curp);
568 MAC_CHECK(proc_check_signal, cred, proc, signum);
569 kauth_cred_unref(&cred);
570
571 return error;
572 }
573
574 int
575 mac_proc_check_syscall_unix(proc_t curp, int scnum)
576 {
577 int error;
578
579 #if SECURITY_MAC_CHECK_ENFORCE
580 /* 21167099 - only check if we allow write */
581 if (!mac_proc_enforce) {
582 return 0;
583 }
584 #endif
585 if (!mac_proc_check_enforce(curp)) {
586 return 0;
587 }
588
589 MAC_CHECK(proc_check_syscall_unix, curp, scnum);
590
591 return error;
592 }
593
594 int
595 mac_proc_check_wait(proc_t curp, struct proc *proc)
596 {
597 kauth_cred_t cred;
598 int error;
599
600 #if SECURITY_MAC_CHECK_ENFORCE
601 /* 21167099 - only check if we allow write */
602 if (!mac_proc_enforce) {
603 return 0;
604 }
605 #endif
606 if (!mac_proc_check_enforce(curp)) {
607 return 0;
608 }
609
610 cred = kauth_cred_proc_ref(curp);
611 MAC_CHECK(proc_check_wait, cred, proc);
612 kauth_cred_unref(&cred);
613
614 return error;
615 }
616
617 void
618 mac_proc_notify_exit(struct proc *proc)
619 {
620 MAC_PERFORM(proc_notify_exit, proc);
621 }
622
623 int
624 mac_proc_check_suspend_resume(proc_t proc, int sr)
625 {
626 kauth_cred_t cred;
627 int error;
628
629 #if SECURITY_MAC_CHECK_ENFORCE
630 /* 21167099 - only check if we allow write */
631 if (!mac_proc_enforce) {
632 return 0;
633 }
634 #endif
635 if (!mac_proc_check_enforce(current_proc())) {
636 return 0;
637 }
638
639 cred = kauth_cred_proc_ref(current_proc());
640 MAC_CHECK(proc_check_suspend_resume, cred, proc, sr);
641 kauth_cred_unref(&cred);
642
643 return error;
644 }
645
646 int
647 mac_proc_check_ledger(proc_t curp, proc_t proc, int ledger_op)
648 {
649 kauth_cred_t cred;
650 int error = 0;
651
652 #if SECURITY_MAC_CHECK_ENFORCE
653 /* 21167099 - only check if we allow write */
654 if (!mac_proc_enforce) {
655 return 0;
656 }
657 #endif
658 if (!mac_proc_check_enforce(curp)) {
659 return 0;
660 }
661
662 cred = kauth_cred_proc_ref(curp);
663 MAC_CHECK(proc_check_ledger, cred, proc, ledger_op);
664 kauth_cred_unref(&cred);
665
666 return error;
667 }
668
669 int
670 mac_proc_check_proc_info(proc_t curp, proc_t target, int callnum, int flavor)
671 {
672 kauth_cred_t cred;
673 int error = 0;
674
675 #if SECURITY_MAC_CHECK_ENFORCE
676 /* 21167099 - only check if we allow write */
677 if (!mac_proc_enforce) {
678 return 0;
679 }
680 #endif
681 if (!mac_proc_check_enforce(curp)) {
682 return 0;
683 }
684
685 cred = kauth_cred_proc_ref(curp);
686 MAC_CHECK(proc_check_proc_info, cred, target, callnum, flavor);
687 kauth_cred_unref(&cred);
688
689 return error;
690 }
691
692 int
693 mac_proc_check_get_cs_info(proc_t curp, proc_t target, unsigned int op)
694 {
695 kauth_cred_t cred;
696 int error = 0;
697
698 #if SECURITY_MAC_CHECK_ENFORCE
699 /* 21167099 - only check if we allow write */
700 if (!mac_proc_enforce) {
701 return 0;
702 }
703 #endif
704 if (!mac_proc_check_enforce(curp)) {
705 return 0;
706 }
707
708 cred = kauth_cred_proc_ref(curp);
709 MAC_CHECK(proc_check_get_cs_info, cred, target, op);
710 kauth_cred_unref(&cred);
711
712 return error;
713 }
714
715 int
716 mac_proc_check_set_cs_info(proc_t curp, proc_t target, unsigned int op)
717 {
718 kauth_cred_t cred;
719 int error = 0;
720
721 #if SECURITY_MAC_CHECK_ENFORCE
722 /* 21167099 - only check if we allow write */
723 if (!mac_proc_enforce) {
724 return 0;
725 }
726 #endif
727 if (!mac_proc_check_enforce(curp)) {
728 return 0;
729 }
730
731 cred = kauth_cred_proc_ref(curp);
732 MAC_CHECK(proc_check_set_cs_info, cred, target, op);
733 kauth_cred_unref(&cred);
734
735 return error;
736 }