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>
76 #include <kern/task.h>
78 #include <security/mac_internal.h>
79 #include <security/mac_mach_internal.h>
81 #include <bsd/security/audit/audit.h>
84 mac_cred_label_alloc(void)
88 label
= mac_labelzone_alloc(MAC_WAITOK
);
91 MAC_PERFORM(cred_label_init
, label
);
96 mac_cred_label_init(struct ucred
*cred
)
98 cred
->cr_label
= mac_cred_label_alloc();
102 mac_cred_label_free(struct label
*label
)
104 MAC_PERFORM(cred_label_destroy
, label
);
105 mac_labelzone_free(label
);
109 mac_cred_label_compare(struct label
*a
, struct label
*b
)
111 return (bcmp(a
, b
, sizeof (*a
)) == 0);
115 mac_cred_label_externalize_audit(struct proc
*p
, struct mac
*mac
)
120 cr
= kauth_cred_proc_ref(p
);
122 error
= MAC_EXTERNALIZE_AUDIT(cred
, cr
->cr_label
,
123 mac
->m_string
, mac
->m_buflen
);
125 kauth_cred_unref(&cr
);
130 mac_cred_label_destroy(kauth_cred_t cred
)
133 mac_cred_label_free(cred
->cr_label
);
134 cred
->cr_label
= NULL
;
138 mac_cred_label_externalize(struct label
*label
, char *elements
,
139 char *outbuf
, size_t outbuflen
, int flags __unused
)
143 error
= MAC_EXTERNALIZE(cred
, label
, elements
, outbuf
, outbuflen
);
149 mac_cred_label_internalize(struct label
*label
, char *string
)
153 error
= MAC_INTERNALIZE(cred
, label
, string
);
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
165 mac_cred_label_associate_fork(kauth_cred_t cred
, proc_t proc
)
167 MAC_PERFORM(cred_label_associate_fork
, cred
, proc
);
171 * Initialize MAC label for the first kernel process, from which other
172 * kernel processes and threads are spawned.
175 mac_cred_label_associate_kernel(kauth_cred_t cred
)
178 MAC_PERFORM(cred_label_associate_kernel
, cred
);
182 * Initialize MAC label for the first userland process, from which other
183 * userland processes and threads are spawned.
186 mac_cred_label_associate_user(kauth_cred_t cred
)
189 MAC_PERFORM(cred_label_associate_user
, cred
);
193 * When a new process is created, its label must be initialized. Generally,
194 * this involves inheritence from the parent process, modulo possible
195 * deltas. This function allows that processing to take place.
198 mac_cred_label_associate(struct ucred
*parent_cred
, struct ucred
*child_cred
)
201 MAC_PERFORM(cred_label_associate
, parent_cred
, child_cred
);
205 mac_execve_enter(user_addr_t mac_p
, struct image_params
*imgp
)
208 struct label
*execlabel
;
213 if (mac_p
== USER_ADDR_NULL
)
216 if (IS_64BIT_PROCESS(current_proc())) {
217 struct user64_mac mac64
;
218 error
= copyin(mac_p
, &mac64
, sizeof(mac64
));
219 mac
.m_buflen
= mac64
.m_buflen
;
220 mac
.m_string
= mac64
.m_string
;
222 struct user32_mac mac32
;
223 error
= copyin(mac_p
, &mac32
, sizeof(mac32
));
224 mac
.m_buflen
= mac32
.m_buflen
;
225 mac
.m_string
= mac32
.m_string
;
230 error
= mac_check_structmac_consistent(&mac
);
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
);
239 AUDIT_ARG(mac_string
, buffer
);
241 error
= mac_cred_label_internalize(execlabel
, buffer
);
244 mac_cred_label_free(execlabel
);
247 imgp
->ip_execlabelp
= execlabel
;
248 FREE(buffer
, M_MACTEMP
);
253 * When the subject's label changes, it may require revocation of privilege
254 * to mapped objects. This can't be done on-the-fly later with a unified
257 * XXX: CRF_MAC_ENFORCE should be in a kauth_cred_t field, rather
258 * XXX: than a posix_cred_t field.
261 mac_cred_label_update(kauth_cred_t cred
, struct label
*newlabel
)
263 posix_cred_t pcred
= posix_cred_get(cred
);
265 /* force label to be part of "matching" for credential */
266 pcred
->cr_flags
|= CRF_MAC_ENFORCE
;
268 /* inform the policies of the update */
269 MAC_PERFORM(cred_label_update
, cred
, newlabel
);
273 mac_cred_check_label_update(kauth_cred_t cred
, struct label
*newlabel
)
277 #if SECURITY_MAC_CHECK_ENFORCE
278 /* 21167099 - only check if we allow write */
279 if (!mac_proc_enforce
)
283 MAC_CHECK(cred_check_label_update
, cred
, newlabel
);
289 mac_cred_check_visible(kauth_cred_t u1
, kauth_cred_t u2
)
293 #if SECURITY_MAC_CHECK_ENFORCE
294 /* 21167099 - only check if we allow write */
295 if (!mac_proc_enforce
)
299 MAC_CHECK(cred_check_visible
, u1
, u2
);
305 * called with process locked.
307 void mac_proc_set_enforce(proc_t p
, int enforce_flags
)
309 p
->p_mac_enforce
|= enforce_flags
;
313 mac_proc_check_debug(proc_t curp
, struct proc
*proc
)
318 #if SECURITY_MAC_CHECK_ENFORCE
319 /* 21167099 - only check if we allow write */
320 if (!mac_proc_enforce
)
323 if (!mac_proc_check_enforce(curp
, MAC_PROC_ENFORCE
))
326 cred
= kauth_cred_proc_ref(curp
);
327 MAC_CHECK(proc_check_debug
, cred
, proc
);
328 kauth_cred_unref(&cred
);
334 mac_proc_check_fork(proc_t curp
)
339 #if SECURITY_MAC_CHECK_ENFORCE
340 /* 21167099 - only check if we allow write */
341 if (!mac_proc_enforce
)
344 if (!mac_proc_check_enforce(curp
, MAC_PROC_ENFORCE
))
347 cred
= kauth_cred_proc_ref(curp
);
348 MAC_CHECK(proc_check_fork
, cred
, curp
);
349 kauth_cred_unref(&cred
);
355 mac_proc_check_get_task_name(struct ucred
*cred
, struct proc
*p
)
359 MAC_CHECK(proc_check_get_task_name
, cred
, p
);
365 mac_proc_check_get_task(struct ucred
*cred
, struct proc
*p
)
369 MAC_CHECK(proc_check_get_task
, cred
, p
);
375 mac_proc_check_expose_task(struct ucred
*cred
, struct proc
*p
)
379 MAC_CHECK(proc_check_expose_task
, cred
, p
);
385 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
)
389 MAC_CHECK(proc_check_inherit_ipc_ports
, p
, cur_vp
, cur_offset
, img_vp
, img_offset
, scriptvp
);
395 * The type of maxprot in proc_check_map_anon must be equivalent to vm_prot_t
396 * (defined in <mach/vm_prot.h>). mac_policy.h does not include any header
397 * files, so cannot use the typedef itself.
400 mac_proc_check_map_anon(proc_t proc
, user_addr_t u_addr
,
401 user_size_t u_size
, int prot
, int flags
, int *maxprot
)
406 #if SECURITY_MAC_CHECK_ENFORCE
407 /* 21167099 - only check if we allow write */
411 if (!mac_proc_check_enforce(proc
, MAC_VM_ENFORCE
))
414 cred
= kauth_cred_proc_ref(proc
);
415 MAC_CHECK(proc_check_map_anon
, proc
, cred
, u_addr
, u_size
, prot
, flags
, maxprot
);
416 kauth_cred_unref(&cred
);
422 mac_proc_check_mprotect(proc_t proc
,
423 user_addr_t addr
, user_size_t size
, int prot
)
428 #if SECURITY_MAC_CHECK_ENFORCE
429 /* 21167099 - only check if we allow write */
433 if (!mac_proc_check_enforce(proc
, MAC_VM_ENFORCE
))
436 cred
= kauth_cred_proc_ref(proc
);
437 MAC_CHECK(proc_check_mprotect
, cred
, proc
, addr
, size
, prot
);
438 kauth_cred_unref(&cred
);
444 mac_proc_check_run_cs_invalid(proc_t proc
)
448 #if SECURITY_MAC_CHECK_ENFORCE
449 /* 21167099 - only check if we allow write */
454 MAC_CHECK(proc_check_run_cs_invalid
, proc
);
460 mac_proc_check_sched(proc_t curp
, struct proc
*proc
)
465 #if SECURITY_MAC_CHECK_ENFORCE
466 /* 21167099 - only check if we allow write */
467 if (!mac_proc_enforce
)
470 if (!mac_proc_check_enforce(curp
, MAC_PROC_ENFORCE
))
473 cred
= kauth_cred_proc_ref(curp
);
474 MAC_CHECK(proc_check_sched
, cred
, proc
);
475 kauth_cred_unref(&cred
);
481 mac_proc_check_signal(proc_t curp
, struct proc
*proc
, int signum
)
486 #if SECURITY_MAC_CHECK_ENFORCE
487 /* 21167099 - only check if we allow write */
488 if (!mac_proc_enforce
)
491 if (!mac_proc_check_enforce(curp
, MAC_PROC_ENFORCE
))
494 cred
= kauth_cred_proc_ref(curp
);
495 MAC_CHECK(proc_check_signal
, cred
, proc
, signum
);
496 kauth_cred_unref(&cred
);
502 mac_proc_check_wait(proc_t curp
, struct proc
*proc
)
507 #if SECURITY_MAC_CHECK_ENFORCE
508 /* 21167099 - only check if we allow write */
509 if (!mac_proc_enforce
)
512 if (!mac_proc_check_enforce(curp
, MAC_PROC_ENFORCE
))
515 cred
= kauth_cred_proc_ref(curp
);
516 MAC_CHECK(proc_check_wait
, cred
, proc
);
517 kauth_cred_unref(&cred
);
523 mac_proc_check_suspend_resume(proc_t curp
, int sr
)
528 #if SECURITY_MAC_CHECK_ENFORCE
529 /* 21167099 - only check if we allow write */
530 if (!mac_proc_enforce
)
533 if (!mac_proc_check_enforce(curp
, MAC_PROC_ENFORCE
))
536 cred
= kauth_cred_proc_ref(curp
);
537 MAC_CHECK(proc_check_suspend_resume
, cred
, curp
, sr
);
538 kauth_cred_unref(&cred
);
544 mac_proc_check_ledger(proc_t curp
, proc_t proc
, int ledger_op
)
549 #if SECURITY_MAC_CHECK_ENFORCE
550 /* 21167099 - only check if we allow write */
551 if (!mac_proc_enforce
)
554 if (!mac_proc_check_enforce(curp
, MAC_PROC_ENFORCE
))
557 cred
= kauth_cred_proc_ref(curp
);
558 MAC_CHECK(proc_check_ledger
, cred
, proc
, ledger_op
);
559 kauth_cred_unref(&cred
);
565 mac_proc_check_cpumon(proc_t curp
)
570 #if SECURITY_MAC_CHECK_ENFORCE
571 /* 21167099 - only check if we allow write */
572 if (!mac_proc_enforce
)
575 if (!mac_proc_check_enforce(curp
, MAC_PROC_ENFORCE
))
578 cred
= kauth_cred_proc_ref(curp
);
579 MAC_CHECK(proc_check_cpumon
, cred
);
580 kauth_cred_unref(&cred
);
586 mac_proc_check_proc_info(proc_t curp
, proc_t target
, int callnum
, int flavor
)
591 #if SECURITY_MAC_CHECK_ENFORCE
592 /* 21167099 - only check if we allow write */
593 if (!mac_proc_enforce
)
596 if (!mac_proc_check_enforce(curp
, MAC_PROC_ENFORCE
))
599 cred
= kauth_cred_proc_ref(curp
);
600 MAC_CHECK(proc_check_proc_info
, cred
, target
, callnum
, flavor
);
601 kauth_cred_unref(&cred
);
607 mac_proc_check_get_cs_info(proc_t curp
, proc_t target
, unsigned int op
)
612 #if SECURITY_MAC_CHECK_ENFORCE
613 /* 21167099 - only check if we allow write */
614 if (!mac_proc_enforce
)
617 if (!mac_proc_check_enforce(curp
, MAC_PROC_ENFORCE
))
620 cred
= kauth_cred_proc_ref(curp
);
621 MAC_CHECK(proc_check_get_cs_info
, cred
, target
, op
);
622 kauth_cred_unref(&cred
);
628 mac_proc_check_set_cs_info(proc_t curp
, proc_t target
, unsigned int op
)
633 #if SECURITY_MAC_CHECK_ENFORCE
634 /* 21167099 - only check if we allow write */
635 if (!mac_proc_enforce
)
638 if (!mac_proc_check_enforce(curp
, MAC_PROC_ENFORCE
))
641 cred
= kauth_cred_proc_ref(curp
);
642 MAC_CHECK(proc_check_set_cs_info
, cred
, target
, op
);
643 kauth_cred_unref(&cred
);