2 * Copyright (c) 2007-2010 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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.
34 * This software was developed by Robert Watson and Ilmar Habibulin for the
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.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
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.
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
66 #include <sys/param.h>
67 #include <sys/ucred.h>
68 #include <sys/malloc.h>
70 #include <sys/vnode.h>
72 #include <sys/proc_internal.h>
73 #include <sys/kauth.h>
74 #include <sys/imgact.h>
75 #include <mach/mach_types.h>
77 #include <security/mac_internal.h>
78 #include <security/mac_mach_internal.h>
80 #include <bsd/security/audit/audit.h>
83 mac_cred_label_alloc(void)
87 label
= mac_labelzone_alloc(MAC_WAITOK
);
90 MAC_PERFORM(cred_label_init
, label
);
95 mac_cred_label_init(struct ucred
*cred
)
97 cred
->cr_label
= mac_cred_label_alloc();
101 mac_cred_label_free(struct label
*label
)
103 MAC_PERFORM(cred_label_destroy
, label
);
104 mac_labelzone_free(label
);
108 mac_cred_label_compare(struct label
*a
, struct label
*b
)
110 return (bcmp(a
, b
, sizeof (*a
)) == 0);
114 mac_cred_label_externalize_audit(struct proc
*p
, struct mac
*mac
)
119 cr
= kauth_cred_proc_ref(p
);
121 error
= MAC_EXTERNALIZE_AUDIT(cred
, cr
->cr_label
,
122 mac
->m_string
, mac
->m_buflen
);
124 kauth_cred_unref(&cr
);
129 mac_cred_label_destroy(kauth_cred_t cred
)
132 mac_cred_label_free(cred
->cr_label
);
133 cred
->cr_label
= NULL
;
137 mac_cred_label_externalize(struct label
*label
, char *elements
,
138 char *outbuf
, size_t outbuflen
, int flags __unused
)
142 error
= MAC_EXTERNALIZE(cred
, label
, elements
, outbuf
, outbuflen
);
148 mac_cred_label_internalize(struct label
*label
, char *string
)
152 error
= MAC_INTERNALIZE(cred
, label
, string
);
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
164 mac_cred_label_associate_fork(kauth_cred_t cred
, proc_t proc
)
166 MAC_PERFORM(cred_label_associate_fork
, cred
, proc
);
170 * Initialize MAC label for the first kernel process, from which other
171 * kernel processes and threads are spawned.
174 mac_cred_label_associate_kernel(kauth_cred_t cred
)
177 MAC_PERFORM(cred_label_associate_kernel
, cred
);
181 * Initialize MAC label for the first userland process, from which other
182 * userland processes and threads are spawned.
185 mac_cred_label_associate_user(kauth_cred_t cred
)
188 MAC_PERFORM(cred_label_associate_user
, cred
);
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.
197 mac_cred_label_associate(struct ucred
*parent_cred
, struct ucred
*child_cred
)
200 MAC_PERFORM(cred_label_associate
, parent_cred
, child_cred
);
204 mac_execve_enter(user_addr_t mac_p
, struct image_params
*imgp
)
207 struct label
*execlabel
;
212 if (mac_p
== USER_ADDR_NULL
)
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
;
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
;
229 error
= mac_check_structmac_consistent(&mac
);
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
);
238 AUDIT_ARG(mac_string
, buffer
);
240 error
= mac_cred_label_internalize(execlabel
, buffer
);
243 mac_cred_label_free(execlabel
);
246 imgp
->ip_execlabelp
= execlabel
;
247 FREE(buffer
, M_MACTEMP
);
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
256 * XXX: CRF_MAC_ENFORCE should be in a kauth_cred_t field, rather
257 * XXX: than a posix_cred_t field.
260 mac_cred_label_update(kauth_cred_t cred
, struct label
*newlabel
)
262 posix_cred_t pcred
= posix_cred_get(cred
);
264 /* force label to be part of "matching" for credential */
265 pcred
->cr_flags
|= CRF_MAC_ENFORCE
;
267 /* inform the policies of the update */
268 MAC_PERFORM(cred_label_update
, cred
, newlabel
);
272 mac_cred_check_label_update(kauth_cred_t cred
, struct label
*newlabel
)
276 if (!mac_proc_enforce
)
279 MAC_CHECK(cred_check_label_update
, cred
, newlabel
);
285 mac_cred_check_visible(kauth_cred_t u1
, kauth_cred_t u2
)
291 if (!mac_proc_enforce
)
296 MAC_CHECK(cred_check_visible
, u1
, u2
);
303 * called with process locked.
305 void mac_proc_set_enforce(proc_t p
, int enforce_flags
)
307 p
->p_mac_enforce
|= enforce_flags
;
311 mac_proc_check_debug(proc_t curp
, struct proc
*proc
)
318 if (!mac_proc_enforce
||
319 !mac_proc_check_enforce(curp
, MAC_PROC_ENFORCE
))
322 cred
= kauth_cred_proc_ref(curp
);
323 MAC_CHECK(proc_check_debug
, cred
, proc
);
324 kauth_cred_unref(&cred
);
330 mac_proc_check_fork(proc_t curp
)
335 if (!mac_proc_enforce
||
336 !mac_proc_check_enforce(curp
, MAC_PROC_ENFORCE
))
339 cred
= kauth_cred_proc_ref(curp
);
340 MAC_CHECK(proc_check_fork
, cred
, curp
);
341 kauth_cred_unref(&cred
);
347 mac_proc_check_get_task_name(struct ucred
*cred
, struct proc
*p
)
351 MAC_CHECK(proc_check_get_task_name
, cred
, p
);
357 mac_proc_check_get_task(struct ucred
*cred
, struct proc
*p
)
361 MAC_CHECK(proc_check_get_task
, cred
, p
);
367 * The type of maxprot in proc_check_map_anon must be equivalent to vm_prot_t
368 * (defined in <mach/vm_prot.h>). mac_policy.h does not include any header
369 * files, so cannot use the typedef itself.
372 mac_proc_check_map_anon(proc_t proc
, user_addr_t u_addr
,
373 user_size_t u_size
, int prot
, int flags
, int *maxprot
)
378 if (!mac_vm_enforce
||
379 !mac_proc_check_enforce(proc
, MAC_VM_ENFORCE
))
382 cred
= kauth_cred_proc_ref(proc
);
383 MAC_CHECK(proc_check_map_anon
, proc
, cred
, u_addr
, u_size
, prot
, flags
, maxprot
);
384 kauth_cred_unref(&cred
);
390 mac_proc_check_mprotect(proc_t proc
,
391 user_addr_t addr
, user_size_t size
, int prot
)
396 if (!mac_vm_enforce
||
397 !mac_proc_check_enforce(proc
, MAC_VM_ENFORCE
))
400 cred
= kauth_cred_proc_ref(proc
);
401 MAC_CHECK(proc_check_mprotect
, cred
, proc
, addr
, size
, prot
);
402 kauth_cred_unref(&cred
);
408 mac_proc_check_run_cs_invalid(proc_t proc
)
412 if (!mac_vm_enforce
) return (0);
414 MAC_CHECK(proc_check_run_cs_invalid
, proc
);
420 mac_proc_check_sched(proc_t curp
, struct proc
*proc
)
427 if (!mac_proc_enforce
||
428 !mac_proc_check_enforce(curp
, MAC_PROC_ENFORCE
))
431 cred
= kauth_cred_proc_ref(curp
);
432 MAC_CHECK(proc_check_sched
, cred
, proc
);
433 kauth_cred_unref(&cred
);
439 mac_proc_check_signal(proc_t curp
, struct proc
*proc
, int signum
)
446 if (!mac_proc_enforce
||
447 !mac_proc_check_enforce(curp
, MAC_PROC_ENFORCE
))
450 cred
= kauth_cred_proc_ref(curp
);
451 MAC_CHECK(proc_check_signal
, cred
, proc
, signum
);
452 kauth_cred_unref(&cred
);
458 mac_proc_check_wait(proc_t curp
, struct proc
*proc
)
465 if (!mac_proc_enforce
||
466 !mac_proc_check_enforce(curp
, MAC_PROC_ENFORCE
))
469 cred
= kauth_cred_proc_ref(curp
);
470 MAC_CHECK(proc_check_wait
, cred
, proc
);
471 kauth_cred_unref(&cred
);
482 mac_proc_check_setlcid (struct proc
*p0
, struct proc
*p
,
483 pid_t pid
, pid_t lcid
)
487 if (!mac_proc_enforce
||
488 !mac_proc_check_enforce(p0
, MAC_PROC_ENFORCE
))
491 MAC_CHECK(proc_check_setlcid
, p0
, p
, pid
, lcid
);
496 mac_proc_check_getlcid (struct proc
*p0
, struct proc
*p
, pid_t pid
)
500 if (!mac_proc_enforce
||
501 !mac_proc_check_enforce(p0
, MAC_PROC_ENFORCE
))
504 MAC_CHECK(proc_check_getlcid
, p0
, p
, pid
);
509 mac_lctx_notify_create (struct proc
*p
, struct lctx
*l
)
511 MAC_PERFORM(lctx_notify_create
, p
, l
);
515 mac_lctx_notify_join (struct proc
*p
, struct lctx
*l
)
517 MAC_PERFORM(lctx_notify_join
, p
, l
);
521 mac_lctx_notify_leave (struct proc
*p
, struct lctx
*l
)
523 MAC_PERFORM(lctx_notify_leave
, p
, l
);
527 mac_lctx_label_alloc(void)
531 label
= mac_labelzone_alloc(MAC_WAITOK
);
534 MAC_PERFORM(lctx_label_init
, label
);
539 mac_lctx_label_free(struct label
*label
)
542 MAC_PERFORM(lctx_label_destroy
, label
);
543 mac_labelzone_free(label
);
547 mac_lctx_label_externalize(struct label
*label
, char *elements
,
548 char *outbuf
, size_t outbuflen
)
552 error
= MAC_EXTERNALIZE(lctx
, label
, elements
, outbuf
, outbuflen
);
558 mac_lctx_label_internalize(struct label
*label
, char *string
)
562 error
= MAC_INTERNALIZE(lctx
, label
, string
);
568 mac_lctx_label_update(struct lctx
*l
, struct label
*newlabel
)
571 MAC_PERFORM(lctx_label_update
, l
, newlabel
);
575 mac_lctx_check_label_update(struct lctx
*l
, struct label
*newlabel
)
579 MAC_CHECK(lctx_check_label_update
, l
, newlabel
);
586 mac_proc_check_suspend_resume(proc_t curp
, int sr
)
591 if (!mac_proc_enforce
||
592 !mac_proc_check_enforce(curp
, MAC_PROC_ENFORCE
))
595 cred
= kauth_cred_proc_ref(curp
);
596 MAC_CHECK(proc_check_suspend_resume
, cred
, curp
, sr
);
597 kauth_cred_unref(&cred
);
603 mac_proc_check_ledger(proc_t curp
, proc_t proc
, int ledger_op
)
608 if (!mac_proc_enforce
||
609 !mac_proc_check_enforce(curp
, MAC_PROC_ENFORCE
))
612 cred
= kauth_cred_proc_ref(curp
);
613 MAC_CHECK(proc_check_ledger
, cred
, proc
, ledger_op
);
614 kauth_cred_unref(&cred
);
620 mac_proc_check_cpumon(proc_t curp
)
625 if (!mac_proc_enforce
||
626 !mac_proc_check_enforce(curp
, MAC_PROC_ENFORCE
))
629 cred
= kauth_cred_proc_ref(curp
);
630 MAC_CHECK(proc_check_cpumon
, cred
);
631 kauth_cred_unref(&cred
);
637 mac_proc_check_proc_info(proc_t curp
, proc_t target
, int callnum
, int flavor
)
642 if (!mac_proc_enforce
||
643 !mac_proc_check_enforce(curp
, MAC_PROC_ENFORCE
))
646 cred
= kauth_cred_proc_ref(curp
);
647 MAC_CHECK(proc_check_proc_info
, cred
, target
, callnum
, flavor
);
648 kauth_cred_unref(&cred
);
654 mac_thread_label_alloc(void)
658 label
= mac_labelzone_alloc(MAC_WAITOK
);
661 MAC_PERFORM(thread_label_init
, label
);
666 mac_thread_label_init(struct uthread
*uthread
)
668 uthread
->uu_label
= mac_thread_label_alloc();
672 mac_thread_label_free(struct label
*label
)
674 MAC_PERFORM(thread_label_destroy
, label
);
675 mac_labelzone_free(label
);
679 mac_thread_label_destroy(struct uthread
*uthread
)
682 mac_thread_label_free(uthread
->uu_label
);
683 uthread
->uu_label
= NULL
;
687 mac_thread_userret(struct thread
*td
)
690 MAC_PERFORM(thread_userret
, td
);
694 mac_thread_get_uthreadlabel(struct uthread
*uthread
)
697 return (uthread
->uu_label
);
701 mac_thread_get_threadlabel(struct thread
*thread
)
703 struct uthread
*uthread
= get_bsdthread_info(thread
);
705 return (mac_thread_get_uthreadlabel(uthread
));