2 * Copyright (c) 2007-2012 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@
29 * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson
30 * Copyright (c) 2001 Ilmar S. Habibulin
31 * Copyright (c) 2001, 2002, 2003, 2004 Networks Associates Technology, Inc.
32 * Copyright (c) 2005-2006 SPARTA, 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 * Framework for extensible kernel access control. This file contains
67 * Kernel and userland interface to the framework, policy registration
68 * and composition. Per-object interfaces, controls, and labeling may be
69 * found in src/sys/mac/. Sample policies may be found in src/sys/mac*.
74 #include <security/mac_internal.h>
75 #include <security/mac_mach_internal.h>
76 #include <sys/param.h>
77 #include <sys/vnode.h>
78 #include <sys/vnode_internal.h>
79 #include <sys/vfs_context.h>
80 #include <sys/namei.h>
81 #include <bsd/bsm/audit.h>
82 #include <bsd/security/audit/audit.h>
84 #include <sys/file_internal.h>
85 #include <sys/filedesc.h>
87 #include <sys/proc_internal.h>
88 #include <sys/kauth.h>
89 #include <sys/sysproto.h>
91 #include <mach/exception_types.h>
92 #include <mach/vm_types.h>
93 #include <mach/vm_prot.h>
95 #include <kern/zalloc.h>
96 #include <kern/sched_prim.h>
97 #include <osfmk/kern/task.h>
98 #include <osfmk/kern/kalloc.h>
101 #include <security/mac.h>
102 #include <security/mac_policy.h>
103 #include <security/mac_framework.h>
104 #include <security/mac_internal.h>
105 #include <security/mac_mach_internal.h>
110 * define MB_DEBUG to display run-time debugging information
115 #define DPRINTF(x) printf x
122 SYSCTL_NODE(, OID_AUTO
, security
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0,
123 "Security Controls");
124 SYSCTL_NODE(_security
, OID_AUTO
, mac
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0,
125 "TrustedBSD MAC policy controls");
128 #define SECURITY_MAC_CTLFLAGS CTLFLAG_RW | CTLFLAG_LOCKED
130 #define SECURITY_MAC_CTLFLAGS CTLFLAG_RD | CTLFLAG_LOCKED
134 * Declare that the kernel provides MAC support, version 1. This permits
135 * modules to refuse to be loaded if the necessary support isn't present,
136 * even if it's pre-boot.
139 MODULE_VERSION(kernel_mac_support
, 1);
142 #if MAC_MAX_SLOTS > 32
143 #error "MAC_MAX_SLOTS too large"
146 static unsigned int mac_max_slots
= MAC_MAX_SLOTS
;
147 static unsigned int mac_slot_offsets_free
= (1 << MAC_MAX_SLOTS
) - 1;
148 SYSCTL_UINT(_security_mac
, OID_AUTO
, max_slots
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
149 &mac_max_slots
, 0, "");
152 * Has the kernel started generating labeled objects yet? All read/write
153 * access to this variable is serialized during the boot process. Following
154 * the end of serialization, we don't update this flag; no locking.
159 * Flag to indicate whether or not we should allocate label storage for
160 * new mbufs. Since most dynamic policies we currently work with don't
161 * rely on mbuf labeling, try to avoid paying the cost of mtag allocation
162 * unless specifically notified of interest. One result of this is
163 * that if a dynamically loaded policy requests mbuf labels, it must
164 * be able to deal with a NULL label being returned on any mbufs that
165 * were already in flight when the policy was loaded. Since the policy
166 * already has to deal with uninitialized labels, this probably won't
167 * be a problem. Note: currently no locking. Will this be a problem?
170 unsigned int mac_label_mbufs
= 1;
171 SYSCTL_UINT(_security_mac
, OID_AUTO
, label_mbufs
, SECURITY_MAC_CTLFLAGS
,
172 &mac_label_mbufs
, 0, "Label all MBUFs");
177 * Flag to indicate whether or not we should allocate label storage for
178 * new vnodes. Since most dynamic policies we currently work with don't
179 * rely on vnode labeling, try to avoid paying the cost of mtag allocation
180 * unless specifically notified of interest. One result of this is
181 * that if a dynamically loaded policy requests vnode labels, it must
182 * be able to deal with a NULL label being returned on any vnodes that
183 * were already in flight when the policy was loaded. Since the policy
184 * already has to deal with uninitialized labels, this probably won't
187 unsigned int mac_label_vnodes
= 0;
188 SYSCTL_UINT(_security_mac
, OID_AUTO
, labelvnodes
, SECURITY_MAC_CTLFLAGS
,
189 &mac_label_vnodes
, 0, "Label all vnodes");
192 unsigned int mac_mmap_revocation
= 0;
193 SYSCTL_UINT(_security_mac
, OID_AUTO
, mmap_revocation
, SECURITY_MAC_CTLFLAGS
,
194 &mac_mmap_revocation
, 0, "Revoke mmap access to files on subject "
197 unsigned int mac_mmap_revocation_via_cow
= 0;
198 SYSCTL_UINT(_security_mac
, OID_AUTO
, mmap_revocation_via_cow
, SECURITY_MAC_CTLFLAGS
,
199 &mac_mmap_revocation_via_cow
, 0, "Revoke mmap access to files via "
200 "copy-on-write semantics, or by removing all write access");
202 unsigned int mac_device_enforce
= 1;
203 SYSCTL_UINT(_security_mac
, OID_AUTO
, device_enforce
, SECURITY_MAC_CTLFLAGS
,
204 &mac_device_enforce
, 0, "Enforce MAC policy on device operations");
206 unsigned int mac_pipe_enforce
= 1;
207 SYSCTL_UINT(_security_mac
, OID_AUTO
, pipe_enforce
, SECURITY_MAC_CTLFLAGS
,
208 &mac_pipe_enforce
, 0, "Enforce MAC policy on pipe operations");
210 unsigned int mac_posixsem_enforce
= 1;
211 SYSCTL_UINT(_security_mac
, OID_AUTO
, posixsem_enforce
, SECURITY_MAC_CTLFLAGS
,
212 &mac_posixsem_enforce
, 0, "Enforce MAC policy on POSIX semaphores");
214 unsigned int mac_posixshm_enforce
= 1;
215 SYSCTL_UINT(_security_mac
, OID_AUTO
, posixshm_enforce
, SECURITY_MAC_CTLFLAGS
,
216 &mac_posixshm_enforce
, 0, "Enforce MAC policy on Posix Shared Memory");
218 unsigned int mac_proc_enforce
= 1;
219 SYSCTL_UINT(_security_mac
, OID_AUTO
, proc_enforce
, SECURITY_MAC_CTLFLAGS
,
220 &mac_proc_enforce
, 0, "Enforce MAC policy on process operations");
222 unsigned int mac_socket_enforce
= 1;
223 SYSCTL_UINT(_security_mac
, OID_AUTO
, socket_enforce
, SECURITY_MAC_CTLFLAGS
,
224 &mac_socket_enforce
, 0, "Enforce MAC policy on socket operations");
226 unsigned int mac_system_enforce
= 1;
227 SYSCTL_UINT(_security_mac
, OID_AUTO
, system_enforce
, SECURITY_MAC_CTLFLAGS
,
228 &mac_system_enforce
, 0, "Enforce MAC policy on system-wide interfaces");
230 unsigned int mac_sysvmsg_enforce
= 1;
231 SYSCTL_UINT(_security_mac
, OID_AUTO
, sysvmsg_enforce
, SECURITY_MAC_CTLFLAGS
,
232 &mac_sysvmsg_enforce
, 0, "Enforce MAC policy on System V IPC message queues");
234 unsigned int mac_sysvsem_enforce
= 1;
235 SYSCTL_UINT(_security_mac
, OID_AUTO
, sysvsem_enforce
, SECURITY_MAC_CTLFLAGS
,
236 &mac_sysvsem_enforce
, 0, "Enforce MAC policy on System V IPC semaphores");
238 unsigned int mac_sysvshm_enforce
= 1;
239 SYSCTL_INT(_security_mac
, OID_AUTO
, sysvshm_enforce
, SECURITY_MAC_CTLFLAGS
,
240 &mac_sysvshm_enforce
, 0, "Enforce MAC policy on System V Shared Memory");
242 unsigned int mac_vm_enforce
= 1;
243 SYSCTL_INT(_security_mac
, OID_AUTO
, vm_enforce
, SECURITY_MAC_CTLFLAGS
,
244 &mac_vm_enforce
, 0, "Enforce MAC policy on VM operations");
246 unsigned int mac_vnode_enforce
= 1;
247 SYSCTL_UINT(_security_mac
, OID_AUTO
, vnode_enforce
, SECURITY_MAC_CTLFLAGS
,
248 &mac_vnode_enforce
, 0, "Enforce MAC policy on vnode operations");
252 * mac_audit_data_zone is the zone used for data pushed into the audit
253 * record by policies. Using a zone simplifies memory management of this
254 * data, and allows tracking of the amount of data in flight.
256 extern zone_t mac_audit_data_zone
;
260 * mac_policy_list holds the list of policy modules. Modules with a
261 * handle lower than staticmax are considered "static" and cannot be
262 * unloaded. Such policies can be invoked without holding the busy count.
264 * Modules with a handle at or above the staticmax high water mark
265 * are considered to be "dynamic" policies. A busy count is maintained
266 * for the list, stored in mac_policy_busy. The busy count is protected
267 * by mac_policy_mtx; the list may be modified only while the busy
268 * count is 0, requiring that the lock be held to prevent new references
269 * to the list from being acquired. For almost all operations,
270 * incrementing the busy count is sufficient to guarantee consistency,
271 * as the list cannot be modified while the busy count is elevated.
272 * For a few special operations involving a change to the list of
273 * active policies, the mtx itself must be held.
275 static lck_mtx_t
*mac_policy_mtx
;
278 * Policy list array allocation chunk size. Trying to set this so that we
279 * allocate a page at a time.
281 #define MAC_POLICY_LIST_CHUNKSIZE 512
283 static int mac_policy_busy
;
285 mac_policy_list_t mac_policy_list
;
288 * mac_label_element_list holds the master list of label namespaces for
289 * all the policies. When a policy is loaded, each of it's label namespace
290 * elements is added to the master list if not already present. When a
291 * policy is unloaded, the namespace elements are removed if no other
292 * policy is interested in that namespace element.
294 struct mac_label_element_list_t mac_label_element_list
;
295 struct mac_label_element_list_t mac_static_label_element_list
;
298 mac_policy_grab_exclusive(void)
300 lck_mtx_lock(mac_policy_mtx
);
301 while (mac_policy_busy
!= 0) {
302 lck_mtx_sleep(mac_policy_mtx
, LCK_SLEEP_UNLOCK
,
303 (event_t
)&mac_policy_busy
, THREAD_UNINT
);
304 lck_mtx_lock(mac_policy_mtx
);
309 mac_policy_release_exclusive(void)
312 KASSERT(mac_policy_busy
== 0,
313 ("mac_policy_release_exclusive(): not exclusive"));
314 lck_mtx_unlock(mac_policy_mtx
);
315 thread_wakeup((event_t
) &mac_policy_busy
);
319 mac_policy_list_busy(void)
321 lck_mtx_lock(mac_policy_mtx
);
323 lck_mtx_unlock(mac_policy_mtx
);
327 mac_policy_list_conditional_busy(void)
331 if (mac_policy_list
.numloaded
<= mac_policy_list
.staticmax
)
334 lck_mtx_lock(mac_policy_mtx
);
335 if (mac_policy_list
.numloaded
> mac_policy_list
.staticmax
) {
340 lck_mtx_unlock(mac_policy_mtx
);
345 mac_policy_list_unbusy(void)
347 lck_mtx_lock(mac_policy_mtx
);
349 KASSERT(mac_policy_busy
>= 0, ("MAC_POLICY_LIST_LOCK"));
350 if (mac_policy_busy
== 0)
351 thread_wakeup(&mac_policy_busy
);
352 lck_mtx_unlock(mac_policy_mtx
);
356 * Early pre-malloc MAC initialization, including appropriate SMP locks.
359 mac_policy_init(void)
361 lck_grp_attr_t
*mac_lck_grp_attr
;
362 lck_attr_t
*mac_lck_attr
;
363 lck_grp_t
*mac_lck_grp
;
365 mac_policy_list
.numloaded
= 0;
366 mac_policy_list
.max
= MAC_POLICY_LIST_CHUNKSIZE
;
367 mac_policy_list
.maxindex
= 0;
368 mac_policy_list
.staticmax
= 0;
369 mac_policy_list
.freehint
= 0;
370 mac_policy_list
.chunks
= 1;
372 mac_policy_list
.entries
= kalloc(sizeof(struct mac_policy_list_element
) * MAC_POLICY_LIST_CHUNKSIZE
);
373 bzero(mac_policy_list
.entries
, sizeof(struct mac_policy_list_element
) * MAC_POLICY_LIST_CHUNKSIZE
);
375 LIST_INIT(&mac_label_element_list
);
376 LIST_INIT(&mac_static_label_element_list
);
378 mac_lck_grp_attr
= lck_grp_attr_alloc_init();
379 lck_grp_attr_setstat(mac_lck_grp_attr
);
380 mac_lck_grp
= lck_grp_alloc_init("MAC lock", mac_lck_grp_attr
);
381 mac_lck_attr
= lck_attr_alloc_init();
382 lck_attr_setdefault(mac_lck_attr
);
383 mac_policy_mtx
= lck_mtx_alloc_init(mac_lck_grp
, mac_lck_attr
);
384 lck_attr_free(mac_lck_attr
);
385 lck_grp_attr_free(mac_lck_grp_attr
);
386 lck_grp_free(mac_lck_grp
);
388 mac_labelzone_init();
391 /* Function pointer set up for loading security extensions.
392 * It is set to an actual function after OSlibkernInit()
393 * has been called, and is set back to 0 by OSKextRemoveKextBootstrap()
396 void (*load_security_extensions_function
)(void) = 0;
399 * Init after early Mach startup, but before BSD
402 mac_policy_initmach(void)
406 * For the purposes of modules that want to know if they were
407 * loaded "early", set the mac_late flag once we've processed
408 * modules either linked into the kernel, or loaded before the
412 if (load_security_extensions_function
) {
413 load_security_extensions_function();
422 mac_policy_initbsd(void)
424 struct mac_policy_conf
*mpc
;
428 mac_audit_data_zone
= zinit(MAC_AUDIT_DATA_LIMIT
,
429 AQ_HIWATER
* MAC_AUDIT_DATA_LIMIT
,
430 8192, "mac_audit_data_zone");
433 printf("MAC Framework successfully initialized\n");
435 /* Call bsd init functions of already loaded policies */
438 * Using the exclusive lock means no other framework entry
439 * points can proceed while initializations are running.
440 * This may not be necessary.
442 mac_policy_grab_exclusive();
444 for (i
= 0; i
<= mac_policy_list
.maxindex
; i
++) {
445 mpc
= mac_get_mpc(i
);
446 if ((mpc
!= NULL
) && (mpc
->mpc_ops
->mpo_policy_initbsd
!= NULL
))
447 (*(mpc
->mpc_ops
->mpo_policy_initbsd
))(mpc
);
450 mac_policy_release_exclusive();
454 * After a policy has been loaded, add the label namespaces managed by the
455 * policy to either the static or non-static label namespace list.
456 * A namespace is added to the the list only if it is not already on one of
460 mac_policy_addto_labellist(mac_policy_handle_t handle
, int static_entry
)
462 struct mac_label_listener
**new_mlls
;
463 struct mac_label_element
*mle
, **new_mles
;
464 struct mac_label_element_list_t
*list
;
465 struct mac_policy_conf
*mpc
;
466 const char *name
, *name2
;
467 u_int idx
, mle_free
, mll_free
;
469 mpc
= mac_get_mpc(handle
);
471 if (mpc
->mpc_labelnames
== NULL
)
474 if (mpc
->mpc_labelname_count
== 0)
478 list
= &mac_static_label_element_list
;
480 list
= &mac_label_element_list
;
483 * Before we grab the policy list lock, allocate enough memory
484 * to contain the potential new elements so we don't have to
485 * give up the lock, or allocate with the lock held.
487 MALLOC(new_mles
, struct mac_label_element
**,
488 sizeof(struct mac_label_element
*) *
489 mpc
->mpc_labelname_count
, M_MACTEMP
, M_WAITOK
| M_ZERO
);
490 for (idx
= 0; idx
< mpc
->mpc_labelname_count
; idx
++)
491 MALLOC(new_mles
[idx
], struct mac_label_element
*,
492 sizeof(struct mac_label_element
),
493 M_MACTEMP
, M_WAITOK
);
495 MALLOC(new_mlls
, struct mac_label_listener
**,
496 sizeof(struct mac_label_listener
*) *
497 mpc
->mpc_labelname_count
, M_MACTEMP
, M_WAITOK
);
498 for (idx
= 0; idx
< mpc
->mpc_labelname_count
; idx
++)
499 MALLOC(new_mlls
[idx
], struct mac_label_listener
*,
500 sizeof(struct mac_label_listener
), M_MACTEMP
, M_WAITOK
);
504 mac_policy_grab_exclusive();
505 for (idx
= 0; idx
< mpc
->mpc_labelname_count
; idx
++) {
507 if (*(name
= mpc
->mpc_labelnames
[idx
]) == '?')
510 * Check both label element lists and add to the
511 * appropriate list only if not already on a list.
513 LIST_FOREACH(mle
, &mac_static_label_element_list
, mle_list
) {
514 if (*(name2
= mle
->mle_name
) == '?')
516 if (strcmp(name
, name2
) == 0)
520 LIST_FOREACH(mle
, &mac_label_element_list
, mle_list
) {
521 if (*(name2
= mle
->mle_name
) == '?')
523 if (strcmp(name
, name2
) == 0)
528 mle
= new_mles
[mle_free
];
529 strlcpy(mle
->mle_name
, mpc
->mpc_labelnames
[idx
],
530 MAC_MAX_LABEL_ELEMENT_NAME
);
531 LIST_INIT(&mle
->mle_listeners
);
532 LIST_INSERT_HEAD(list
, mle
, mle_list
);
535 /* Add policy handler as a listener. */
536 new_mlls
[mll_free
]->mll_handle
= handle
;
537 LIST_INSERT_HEAD(&mle
->mle_listeners
, new_mlls
[mll_free
],
542 mac_policy_release_exclusive();
544 /* Free up any unused label elements and listeners */
545 for (idx
= mle_free
; idx
< mpc
->mpc_labelname_count
; idx
++)
546 FREE(new_mles
[idx
], M_MACTEMP
);
547 FREE(new_mles
, M_MACTEMP
);
548 for (idx
= mll_free
; idx
< mpc
->mpc_labelname_count
; idx
++)
549 FREE(new_mlls
[idx
], M_MACTEMP
);
550 FREE(new_mlls
, M_MACTEMP
);
554 * After a policy has been unloaded, remove the label namespaces that the
555 * the policy manages from the non-static list of namespaces.
556 * The removal only takes place when no other policy is interested in the
559 * Must be called with the policy exclusive lock held.
562 mac_policy_removefrom_labellist(mac_policy_handle_t handle
)
564 struct mac_label_listener
*mll
;
565 struct mac_label_element
*mle
;
566 struct mac_policy_conf
*mpc
;
568 mpc
= mac_get_mpc(handle
);
570 if (mpc
->mpc_labelnames
== NULL
)
573 if (mpc
->mpc_labelname_count
== 0)
577 * Unregister policy as being interested in any label
578 * namespaces. If no other policy is listening, remove
579 * that label element from the list. Note that we only
580 * have to worry about the non-static list.
582 LIST_FOREACH(mle
, &mac_label_element_list
, mle_list
) {
583 LIST_FOREACH(mll
, &mle
->mle_listeners
, mll_list
) {
584 if (mll
->mll_handle
== handle
) {
585 LIST_REMOVE(mll
, mll_list
);
586 FREE(mll
, M_MACTEMP
);
587 if (LIST_EMPTY(&mle
->mle_listeners
)) {
588 LIST_REMOVE(mle
, mle_list
);
589 FREE(mle
, M_MACTEMP
);
598 * After the policy list has changed, walk the list to update any global
602 mac_policy_updateflags(void)
607 mac_policy_fixup_mmd_list(struct mac_module_data
*new)
609 struct mac_module_data
*old
;
610 struct mac_module_data_element
*ele
, *aele
;
611 struct mac_module_data_list
*arr
, *dict
;
612 unsigned int i
, j
, k
;
614 old
= new->base_addr
;
615 DPRINTF(("fixup_mmd: old %p new %p\n", old
, new));
616 for (i
= 0; i
< new->count
; i
++) {
617 ele
= &(new->data
[i
]);
618 DPRINTF(("fixup_mmd: ele %p\n", ele
));
619 DPRINTF((" key %p value %p\n", ele
->key
, ele
->value
));
620 mmd_fixup_ele(old
, new, ele
); /* Fix up key/value ptrs. */
621 DPRINTF((" key %p value %p\n", ele
->key
, ele
->value
));
622 if (ele
->value_type
== MAC_DATA_TYPE_ARRAY
) {
623 arr
= (struct mac_module_data_list
*)ele
->value
;
624 DPRINTF(("fixup_mmd: array @%p\n", arr
));
625 for (j
= 0; j
< arr
->count
; j
++) {
626 aele
= &(arr
->list
[j
]);
627 DPRINTF(("fixup_mmd: aele %p\n", aele
));
628 DPRINTF((" key %p value %p\n", aele
->key
, aele
->value
));
629 mmd_fixup_ele(old
, new, aele
);
630 DPRINTF((" key %p value %p\n", aele
->key
, aele
->value
));
631 if (arr
->type
== MAC_DATA_TYPE_DICT
) {
632 dict
= (struct mac_module_data_list
*)aele
->value
;
633 DPRINTF(("fixup_mmd: dict @%p\n", dict
));
634 for (k
= 0; k
< dict
->count
; k
++)
635 mmd_fixup_ele(old
, new,
641 new->base_addr
= new;
645 mac_policy_register(struct mac_policy_conf
*mpc
, mac_policy_handle_t
*handlep
,
648 struct mac_policy_list_element
*tmac_policy_list_element
;
649 int error
, slot
, static_entry
= 0;
653 * Some preliminary checks to make sure the policy's conf structure
654 * contains the required fields.
656 if (mpc
->mpc_name
== NULL
)
657 panic("policy's name is not set\n");
659 if (mpc
->mpc_fullname
== NULL
)
660 panic("policy's full name is not set\n");
662 if (mpc
->mpc_labelname_count
> MAC_MAX_MANAGED_NAMESPACES
)
663 panic("policy's managed label namespaces exceeds maximum\n");
665 if (mpc
->mpc_ops
== NULL
)
666 panic("policy's OPs field is NULL\n");
671 if (mpc
->mpc_loadtime_flags
& MPC_LOADTIME_FLAG_NOTLATE
) {
672 printf("Module %s does not support late loading.\n",
676 mac_policy_grab_exclusive();
679 if (mac_policy_list
.numloaded
>= mac_policy_list
.max
) {
680 /* allocate new policy list array, zero new chunk */
681 tmac_policy_list_element
=
682 kalloc((sizeof(struct mac_policy_list_element
) *
683 MAC_POLICY_LIST_CHUNKSIZE
) * (mac_policy_list
.chunks
+ 1));
684 bzero(&tmac_policy_list_element
[mac_policy_list
.max
],
685 sizeof(struct mac_policy_list_element
) *
686 MAC_POLICY_LIST_CHUNKSIZE
);
688 /* copy old entries into new list */
689 memcpy(tmac_policy_list_element
, mac_policy_list
.entries
,
690 sizeof(struct mac_policy_list_element
) *
691 MAC_POLICY_LIST_CHUNKSIZE
* mac_policy_list
.chunks
);
694 kfree(mac_policy_list
.entries
,
695 sizeof(struct mac_policy_list_element
) *
696 MAC_POLICY_LIST_CHUNKSIZE
* mac_policy_list
.chunks
);
698 mac_policy_list
.entries
= tmac_policy_list_element
;
700 /* Update maximums, etc */
701 mac_policy_list
.max
+= MAC_POLICY_LIST_CHUNKSIZE
;
702 mac_policy_list
.chunks
++;
705 /* Check for policy with same name already loaded */
706 for (i
= 0; i
<= mac_policy_list
.maxindex
; i
++) {
707 if (mac_policy_list
.entries
[i
].mpc
== NULL
)
710 if (strcmp(mac_policy_list
.entries
[i
].mpc
->mpc_name
,
711 mpc
->mpc_name
) == 0) {
717 if (mpc
->mpc_field_off
!= NULL
) {
718 slot
= ffs(mac_slot_offsets_free
);
724 mac_slot_offsets_free
&= ~(1 << slot
);
725 *mpc
->mpc_field_off
= slot
;
727 mpc
->mpc_runtime_flags
|= MPC_RUNTIME_FLAG_REGISTERED
;
730 struct mac_module_data
*mmd
= xd
; /* module data from plist */
732 /* Make a copy of the data. */
733 mpc
->mpc_data
= (void *)kalloc(mmd
->size
);
734 if (mpc
->mpc_data
!= NULL
) {
735 memcpy(mpc
->mpc_data
, mmd
, mmd
->size
);
737 /* Fix up pointers after copy. */
738 mac_policy_fixup_mmd_list(mpc
->mpc_data
);
742 /* Find the first free handle in the list (using our hint). */
743 for (i
= mac_policy_list
.freehint
; i
< mac_policy_list
.max
; i
++) {
744 if (mac_policy_list
.entries
[i
].mpc
== NULL
) {
746 mac_policy_list
.freehint
= ++i
;
752 * If we are loading a MAC module before the framework has
753 * finished initializing or the module is not unloadable and
754 * we can place its handle adjacent to the last static entry,
755 * bump the static policy high water mark.
756 * Static policies can get by with weaker locking requirements.
759 ((mpc
->mpc_loadtime_flags
& MPC_LOADTIME_FLAG_UNLOADOK
) == 0 &&
760 *handlep
== mac_policy_list
.staticmax
)) {
762 mac_policy_list
.staticmax
++;
765 mac_policy_list
.entries
[*handlep
].mpc
= mpc
;
767 /* Update counters, etc */
768 if (*handlep
> mac_policy_list
.maxindex
)
769 mac_policy_list
.maxindex
= *handlep
;
770 mac_policy_list
.numloaded
++;
772 /* Per-policy initialization. */
773 printf ("calling mpo_policy_init for %s\n", mpc
->mpc_name
);
774 if (mpc
->mpc_ops
->mpo_policy_init
!= NULL
)
775 (*(mpc
->mpc_ops
->mpo_policy_init
))(mpc
);
777 if (mac_late
&& mpc
->mpc_ops
->mpo_policy_initbsd
!= NULL
) {
778 printf ("calling mpo_policy_initbsd for %s\n", mpc
->mpc_name
);
779 (*(mpc
->mpc_ops
->mpo_policy_initbsd
))(mpc
);
782 mac_policy_updateflags();
785 mac_policy_release_exclusive();
787 mac_policy_addto_labellist(*handlep
, static_entry
);
789 printf("Security policy loaded: %s (%s)\n", mpc
->mpc_fullname
,
796 mac_policy_release_exclusive();
802 mac_policy_unregister(mac_policy_handle_t handle
)
804 struct mac_policy_conf
*mpc
;
807 * If we fail the load, we may get a request to unload. Check
808 * to see if we did the run-time registration, and if not,
811 mac_policy_grab_exclusive();
812 mpc
= mac_get_mpc(handle
);
813 if ((mpc
->mpc_runtime_flags
& MPC_RUNTIME_FLAG_REGISTERED
) == 0) {
814 mac_policy_release_exclusive();
820 * Don't allow unloading modules with private data.
822 if (mpc
->mpc_field_off
!= NULL
) {
823 MAC_POLICY_LIST_UNLOCK();
828 * Only allow the unload to proceed if the module is unloadable
829 * by its own definition.
831 if ((mpc
->mpc_loadtime_flags
& MPC_LOADTIME_FLAG_UNLOADOK
) == 0) {
832 mac_policy_release_exclusive();
836 mac_policy_removefrom_labellist(handle
);
838 mac_get_mpc(handle
) = NULL
;
839 if (handle
< mac_policy_list
.freehint
&&
840 handle
>= mac_policy_list
.staticmax
)
841 mac_policy_list
.freehint
= handle
;
843 if (handle
== mac_policy_list
.maxindex
)
844 mac_policy_list
.maxindex
--;
846 mac_policy_list
.numloaded
--;
847 if (mpc
->mpc_field_off
!= NULL
) {
848 mac_slot_offsets_free
|= (1 << *mpc
->mpc_field_off
);
851 if (mpc
->mpc_ops
->mpo_policy_destroy
!= NULL
)
852 (*(mpc
->mpc_ops
->mpo_policy_destroy
))(mpc
);
854 mpc
->mpc_runtime_flags
&= ~MPC_RUNTIME_FLAG_REGISTERED
;
855 mac_policy_updateflags();
857 mac_policy_release_exclusive();
860 struct mac_module_data
*mmd
= mpc
->mpc_data
;
861 kfree(mmd
, mmd
->size
);
862 mpc
->mpc_data
= NULL
;
865 printf("Security policy unload: %s (%s)\n", mpc
->mpc_fullname
,
872 * Define an error value precedence, and given two arguments, selects the
873 * value with the higher precedence.
876 mac_error_select(int error1
, int error2
)
879 /* Certain decision-making errors take top priority. */
880 if (error1
== EDEADLK
|| error2
== EDEADLK
)
883 /* Invalid arguments should be reported where possible. */
884 if (error1
== EINVAL
|| error2
== EINVAL
)
887 /* Precedence goes to "visibility", with both process and file. */
888 if (error1
== ESRCH
|| error2
== ESRCH
)
891 if (error1
== ENOENT
|| error2
== ENOENT
)
894 /* Precedence goes to DAC/MAC protections. */
895 if (error1
== EACCES
|| error2
== EACCES
)
898 /* Precedence goes to privilege. */
899 if (error1
== EPERM
|| error2
== EPERM
)
902 /* Precedence goes to error over success; otherwise, arbitrary. */
909 mac_label_init(struct label
*label
)
912 bzero(label
, sizeof(*label
));
913 label
->l_flags
= MAC_FLAG_INITIALIZED
;
917 mac_label_destroy(struct label
*label
)
920 KASSERT(label
->l_flags
& MAC_FLAG_INITIALIZED
,
921 ("destroying uninitialized label"));
923 bzero(label
, sizeof(*label
));
924 /* implicit: label->l_flags &= ~MAC_FLAG_INITIALIZED; */
928 mac_check_structmac_consistent(struct user_mac
*mac
)
931 if (mac
->m_buflen
> MAC_MAX_LABEL_BUF_LEN
|| mac
->m_buflen
== 0)
938 * Get the external forms of labels from all policies, for a single
939 * label namespace or "*" for all namespaces. Returns ENOENT if no policy
940 * is registered for the namespace, unless the namespace begins with a '?'.
943 mac_label_externalize(size_t mpo_externalize_off
, struct label
*label
,
944 const char *element
, struct sbuf
*sb
)
946 struct mac_policy_conf
*mpc
;
947 struct mac_label_listener
*mll
;
948 struct mac_label_element
*mle
;
949 struct mac_label_element_list_t
*element_list
;
951 int (*mpo_externalize
)(struct label
*, char *, struct sbuf
*);
952 int all_labels
= 0, ignorenotfound
= 0, error
= 0, busy
= FALSE
;
953 unsigned int count
= 0;
955 if (element
[0] == '?') {
958 } else if (element
[0] == '*' && element
[1] == '\0')
961 element_list
= &mac_static_label_element_list
;
963 LIST_FOREACH(mle
, element_list
, mle_list
) {
964 name
= mle
->mle_name
;
971 if (strcmp(name
, element
) != 0)
974 LIST_FOREACH(mll
, &mle
->mle_listeners
, mll_list
) {
975 mpc
= mac_policy_list
.entries
[mll
->mll_handle
].mpc
;
978 mpo_externalize
= *(typeof(mpo_externalize
) *)
979 ((char *)mpc
->mpc_ops
+ mpo_externalize_off
);
980 if (mpo_externalize
== NULL
)
982 error
= sbuf_printf(sb
, "%s/", name
);
985 error
= mpo_externalize(label
, mle
->mle_name
, sb
);
990 * If a policy doesn't have a label to
991 * externalize it returns ENOENT. This
992 * may occur for policies that support
993 * multiple label elements for some
994 * (but not all) object types.
996 sbuf_setpos(sb
, sbuf_len(sb
) -
1001 error
= sbuf_putc(sb
, ',');
1007 /* If there are dynamic policies present, check their elements too. */
1008 if (!busy
&& mac_policy_list_conditional_busy() == 1) {
1009 element_list
= &mac_label_element_list
;
1015 mac_policy_list_unbusy();
1016 if (!error
&& count
== 0) {
1017 if (!all_labels
&& !ignorenotfound
)
1018 error
= ENOENT
; /* XXX: ENOLABEL? */
1024 * Get the external forms of labels from all policies, for all label
1025 * namespaces contained in a list.
1027 * XXX This may be leaking an sbuf.
1030 mac_externalize(size_t mpo_externalize_off
, struct label
*label
,
1031 const char *elementlist
, char *outbuf
, size_t outbuflen
)
1039 /* allocate a scratch buffer the size of the string */
1040 MALLOC(scratch_base
, char *, strlen(elementlist
)+1, M_MACTEMP
, M_WAITOK
);
1041 if (scratch_base
== NULL
) {
1046 /* copy the elementlist to the scratch buffer */
1047 strlcpy(scratch_base
, elementlist
, strlen(elementlist
)+1);
1050 * set up a temporary pointer that can be used to iterate the
1051 * scratch buffer without losing the allocation address
1053 scratch
= scratch_base
;
1056 if (sbuf_new(&sb
, outbuf
, outbuflen
, SBUF_FIXEDLEN
) == NULL
) {
1057 /* could not allocate interior buffer */
1061 /* iterate the scratch buffer; NOTE: buffer contents modified! */
1062 while ((element
= strsep(&scratch
, ",")) != NULL
) {
1063 error
= mac_label_externalize(mpo_externalize_off
, label
,
1068 if ((len
= sbuf_len(&sb
)) > 0)
1069 sbuf_setpos(&sb
, len
- 1); /* trim trailing comma */
1073 if (scratch_base
!= NULL
)
1074 FREE(scratch_base
, M_MACTEMP
);
1080 * Have all policies set the internal form of a label, for a single
1084 mac_label_internalize(size_t mpo_internalize_off
, struct label
*label
,
1085 char *element_name
, char *element_data
)
1087 struct mac_policy_conf
*mpc
;
1088 struct mac_label_listener
*mll
;
1089 struct mac_label_element
*mle
;
1090 struct mac_label_element_list_t
*element_list
;
1091 int (*mpo_internalize
)(struct label
*, char *, char *);
1092 int error
= 0, busy
= FALSE
;
1093 unsigned int count
= 0;
1096 element_list
= &mac_static_label_element_list
;
1098 LIST_FOREACH(mle
, element_list
, mle_list
) {
1099 if (*(name
= mle
->mle_name
) == '?')
1101 if (strcmp(element_name
, name
) != 0)
1103 LIST_FOREACH(mll
, &mle
->mle_listeners
, mll_list
) {
1104 mpc
= mac_policy_list
.entries
[mll
->mll_handle
].mpc
;
1107 mpo_internalize
= *(typeof(mpo_internalize
) *)
1108 ((char *)mpc
->mpc_ops
+ mpo_internalize_off
);
1109 if (mpo_internalize
== NULL
)
1111 error
= mpo_internalize(label
, element_name
,
1118 /* If there are dynamic policies present, check their elements too. */
1119 if (!busy
&& mac_policy_list_conditional_busy() == 1) {
1120 element_list
= &mac_label_element_list
;
1126 mac_policy_list_unbusy();
1127 if (!error
&& count
== 0)
1133 mac_internalize(size_t mpo_internalize_off
, struct label
*label
,
1136 char *element_name
, *element_data
;
1139 while (!error
&& (element_name
= strsep(&textlabels
, ",")) != NULL
) {
1140 element_data
= strchr(element_name
, '/');
1141 if (element_data
== NULL
) {
1145 *element_data
++ = '\0';
1146 error
= mac_label_internalize(mpo_internalize_off
, label
,
1147 element_name
, element_data
);
1155 __mac_get_pid(struct proc
*p
, struct __mac_get_pid_args
*uap
, int *ret __unused
)
1157 char *elements
, *buffer
;
1158 struct user_mac mac
;
1160 struct ucred
*tcred
;
1164 AUDIT_ARG(pid
, uap
->pid
);
1165 if (IS_64BIT_PROCESS(p
)) {
1166 struct user64_mac mac64
;
1167 error
= copyin(uap
->mac_p
, &mac64
, sizeof(mac64
));
1168 mac
.m_buflen
= mac64
.m_buflen
;
1169 mac
.m_string
= mac64
.m_string
;
1171 struct user32_mac mac32
;
1172 error
= copyin(uap
->mac_p
, &mac32
, sizeof(mac32
));
1173 mac
.m_buflen
= mac32
.m_buflen
;
1174 mac
.m_string
= mac32
.m_string
;
1179 error
= mac_check_structmac_consistent(&mac
);
1183 tproc
= proc_find(uap
->pid
);
1186 tcred
= kauth_cred_proc_ref(tproc
);
1189 MALLOC(elements
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
);
1190 error
= copyinstr(mac
.m_string
, elements
, mac
.m_buflen
, &ulen
);
1192 FREE(elements
, M_MACTEMP
);
1193 kauth_cred_unref(&tcred
);
1196 AUDIT_ARG(mac_string
, elements
);
1198 MALLOC(buffer
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
| M_ZERO
);
1199 error
= mac_cred_label_externalize(tcred
->cr_label
, elements
,
1200 buffer
, mac
.m_buflen
, M_WAITOK
);
1202 error
= copyout(buffer
, mac
.m_string
, strlen(buffer
)+1);
1204 FREE(buffer
, M_MACTEMP
);
1205 FREE(elements
, M_MACTEMP
);
1206 kauth_cred_unref(&tcred
);
1211 __mac_get_proc(proc_t p
, struct __mac_get_proc_args
*uap
, int *ret __unused
)
1213 char *elements
, *buffer
;
1214 struct user_mac mac
;
1219 if (IS_64BIT_PROCESS(p
)) {
1220 struct user64_mac mac64
;
1221 error
= copyin(uap
->mac_p
, &mac64
, sizeof(mac64
));
1222 mac
.m_buflen
= mac64
.m_buflen
;
1223 mac
.m_string
= mac64
.m_string
;
1225 struct user32_mac mac32
;
1226 error
= copyin(uap
->mac_p
, &mac32
, sizeof(mac32
));
1227 mac
.m_buflen
= mac32
.m_buflen
;
1228 mac
.m_string
= mac32
.m_string
;
1233 error
= mac_check_structmac_consistent(&mac
);
1237 MALLOC(elements
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
);
1238 error
= copyinstr(mac
.m_string
, elements
, mac
.m_buflen
, &ulen
);
1240 FREE(elements
, M_MACTEMP
);
1243 AUDIT_ARG(mac_string
, elements
);
1245 cr
= kauth_cred_proc_ref(p
);
1247 MALLOC(buffer
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
| M_ZERO
);
1248 error
= mac_cred_label_externalize(cr
->cr_label
,
1249 elements
, buffer
, mac
.m_buflen
, M_WAITOK
);
1251 error
= copyout(buffer
, mac
.m_string
, strlen(buffer
)+1);
1253 FREE(buffer
, M_MACTEMP
);
1254 FREE(elements
, M_MACTEMP
);
1255 kauth_cred_unref(&cr
);
1260 __mac_set_proc(proc_t p
, struct __mac_set_proc_args
*uap
, int *ret __unused
)
1262 struct label
*intlabel
;
1263 struct user_mac mac
;
1268 if (IS_64BIT_PROCESS(p
)) {
1269 struct user64_mac mac64
;
1270 error
= copyin(uap
->mac_p
, &mac64
, sizeof(mac64
));
1271 mac
.m_buflen
= mac64
.m_buflen
;
1272 mac
.m_string
= mac64
.m_string
;
1274 struct user32_mac mac32
;
1275 error
= copyin(uap
->mac_p
, &mac32
, sizeof(mac32
));
1276 mac
.m_buflen
= mac32
.m_buflen
;
1277 mac
.m_string
= mac32
.m_string
;
1282 error
= mac_check_structmac_consistent(&mac
);
1286 MALLOC(buffer
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
);
1287 error
= copyinstr(mac
.m_string
, buffer
, mac
.m_buflen
, &ulen
);
1289 FREE(buffer
, M_MACTEMP
);
1292 AUDIT_ARG(mac_string
, buffer
);
1294 intlabel
= mac_cred_label_alloc();
1295 error
= mac_cred_label_internalize(intlabel
, buffer
);
1296 FREE(buffer
, M_MACTEMP
);
1300 error
= mac_cred_check_label_update(kauth_cred_get(), intlabel
);
1305 error
= kauth_proc_label_update(p
, intlabel
);
1310 mac_cred_label_free(intlabel
);
1317 * Get login context ID. A login context associates a BSD process
1318 * with an instance of a user. For more information see getlcid(2) man page.
1320 * Parameters: p Process requesting the get
1321 * uap User argument descriptor (see below)
1324 * Indirect: uap->lcid login context ID to search
1325 * uap->mac_p.m_buflen MAC info buffer size
1326 * uap->mac_p.m_string MAC info user address
1328 * Returns: 0 Success
1332 __mac_get_lcid(proc_t p
, struct __mac_get_lcid_args
*uap
, int *ret __unused
)
1334 char *elements
, *buffer
;
1335 struct user_mac mac
;
1340 AUDIT_ARG(value32
, uap
->lcid
);
1341 if (IS_64BIT_PROCESS(p
)) {
1342 struct user64_mac mac64
;
1343 error
= copyin(uap
->mac_p
, &mac64
, sizeof(mac64
));
1344 mac
.m_buflen
= mac64
.m_buflen
;
1345 mac
.m_string
= mac64
.m_string
;
1347 struct user32_mac mac32
;
1348 error
= copyin(uap
->mac_p
, &mac32
, sizeof(mac32
));
1349 mac
.m_buflen
= mac32
.m_buflen
;
1350 mac
.m_string
= mac32
.m_string
;
1356 error
= mac_check_structmac_consistent(&mac
);
1360 l
= lcfind(uap
->lcid
);
1364 MALLOC(elements
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
);
1365 error
= copyinstr(mac
.m_string
, elements
, mac
.m_buflen
, &ulen
);
1368 FREE(elements
, M_MACTEMP
);
1371 AUDIT_ARG(mac_string
, elements
);
1372 MALLOC(buffer
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
);
1373 error
= mac_lctx_label_externalize(l
->lc_label
, elements
,
1374 buffer
, mac
.m_buflen
);
1376 error
= copyout(buffer
, mac
.m_string
, strlen(buffer
)+1);
1379 FREE(buffer
, M_MACTEMP
);
1380 FREE(elements
, M_MACTEMP
);
1386 * Get login context label. A login context associates a BSD process
1387 * associated with an instance of a user.
1389 * Parameters: p Process requesting the get
1390 * uap User argument descriptor (see below)
1393 * Indirect: uap->lcid login context ID to search
1394 * uap->mac_p MAC info
1396 * Returns: 0 Success
1401 __mac_get_lctx(proc_t p
, struct __mac_get_lctx_args
*uap
, int *ret __unused
)
1403 char *elements
, *buffer
;
1404 struct user_mac mac
;
1408 if (IS_64BIT_PROCESS(p
)) {
1409 struct user64_mac mac64
;
1410 error
= copyin(uap
->mac_p
, &mac64
, sizeof(mac64
));
1411 mac
.m_buflen
= mac64
.m_buflen
;
1412 mac
.m_string
= mac64
.m_string
;
1414 struct user32_mac mac32
;
1415 error
= copyin(uap
->mac_p
, &mac32
, sizeof(mac32
));
1416 mac
.m_buflen
= mac32
.m_buflen
;
1417 mac
.m_string
= mac32
.m_string
;
1423 error
= mac_check_structmac_consistent(&mac
);
1427 MALLOC(elements
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
);
1428 error
= copyinstr(mac
.m_string
, elements
, mac
.m_buflen
, &ulen
);
1430 FREE(elements
, M_MACTEMP
);
1433 AUDIT_ARG(mac_string
, elements
);
1434 MALLOC(buffer
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
);
1437 if (p
->p_lctx
== NULL
) {
1443 error
= mac_lctx_label_externalize(p
->p_lctx
->lc_label
,
1444 elements
, buffer
, mac
.m_buflen
);
1447 error
= copyout(buffer
, mac
.m_string
, strlen(buffer
)+1);
1450 FREE(buffer
, M_MACTEMP
);
1451 FREE(elements
, M_MACTEMP
);
1456 __mac_set_lctx(proc_t p
, struct __mac_set_lctx_args
*uap
, int *ret __unused
)
1458 struct user_mac mac
;
1459 struct label
*intlabel
;
1464 if (IS_64BIT_PROCESS(p
)) {
1465 struct user64_mac mac64
;
1466 error
= copyin(uap
->mac_p
, &mac64
, sizeof(mac64
));
1467 mac
.m_buflen
= mac64
.m_buflen
;
1468 mac
.m_string
= mac64
.m_string
;
1470 struct user32_mac mac32
;
1471 error
= copyin(uap
->mac_p
, &mac32
, sizeof(mac32
));
1472 mac
.m_buflen
= mac32
.m_buflen
;
1473 mac
.m_string
= mac32
.m_string
;
1478 error
= mac_check_structmac_consistent(&mac
);
1482 MALLOC(buffer
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
);
1483 error
= copyinstr(mac
.m_string
, buffer
, mac
.m_buflen
, &ulen
);
1485 FREE(buffer
, M_MACTEMP
);
1488 AUDIT_ARG(mac_string
, buffer
);
1490 intlabel
= mac_lctx_label_alloc();
1491 error
= mac_lctx_label_internalize(intlabel
, buffer
);
1492 FREE(buffer
, M_MACTEMP
);
1497 if (p
->p_lctx
== NULL
) {
1503 error
= mac_lctx_check_label_update(p
->p_lctx
, intlabel
);
1508 mac_lctx_label_update(p
->p_lctx
, intlabel
);
1511 mac_lctx_label_free(intlabel
);
1518 __mac_get_lcid(proc_t p __unused
, struct __mac_get_lcid_args
*uap __unused
, int *ret __unused
)
1525 __mac_get_lctx(proc_t p __unused
, struct __mac_get_lctx_args
*uap __unused
, int *ret __unused
)
1532 __mac_set_lctx(proc_t p __unused
, struct __mac_set_lctx_args
*uap __unused
, int *ret __unused
)
1540 __mac_get_fd(proc_t p
, struct __mac_get_fd_args
*uap
, int *ret __unused
)
1542 struct fileproc
*fp
;
1544 struct user_mac mac
;
1545 char *elements
, *buffer
;
1548 kauth_cred_t my_cred
;
1549 #if CONFIG_MACF_SOCKET
1551 #endif /* MAC_SOCKET */
1552 struct label
*intlabel
;
1554 AUDIT_ARG(fd
, uap
->fd
);
1556 if (IS_64BIT_PROCESS(p
)) {
1557 struct user64_mac mac64
;
1558 error
= copyin(uap
->mac_p
, &mac64
, sizeof(mac64
));
1559 mac
.m_buflen
= mac64
.m_buflen
;
1560 mac
.m_string
= mac64
.m_string
;
1562 struct user32_mac mac32
;
1563 error
= copyin(uap
->mac_p
, &mac32
, sizeof(mac32
));
1564 mac
.m_buflen
= mac32
.m_buflen
;
1565 mac
.m_string
= mac32
.m_string
;
1571 error
= mac_check_structmac_consistent(&mac
);
1575 MALLOC(elements
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
);
1576 error
= copyinstr(mac
.m_string
, elements
, mac
.m_buflen
, &ulen
);
1578 FREE(elements
, M_MACTEMP
);
1581 AUDIT_ARG(mac_string
, elements
);
1583 MALLOC(buffer
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
);
1584 error
= fp_lookup(p
, uap
->fd
, &fp
, 0);
1586 FREE(buffer
, M_MACTEMP
);
1587 FREE(elements
, M_MACTEMP
);
1591 my_cred
= kauth_cred_proc_ref(p
);
1592 error
= mac_file_check_get(my_cred
, fp
->f_fglob
, elements
, mac
.m_buflen
);
1593 kauth_cred_unref(&my_cred
);
1595 fp_drop(p
, uap
->fd
, fp
, 0);
1596 FREE(buffer
, M_MACTEMP
);
1597 FREE(elements
, M_MACTEMP
);
1601 switch (FILEGLOB_DTYPE(fp
->f_fglob
)) {
1603 intlabel
= mac_vnode_label_alloc();
1604 if (intlabel
== NULL
) {
1608 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
1609 error
= vnode_getwithref(vp
);
1611 mac_vnode_label_copy(vp
->v_label
, intlabel
);
1612 error
= mac_vnode_label_externalize(intlabel
,
1614 mac
.m_buflen
, M_WAITOK
);
1617 mac_vnode_label_free(intlabel
);
1620 #if CONFIG_MACF_SOCKET
1621 so
= (struct socket
*) fp
->f_fglob
->fg_data
;
1622 intlabel
= mac_socket_label_alloc(MAC_WAITOK
);
1624 mac_socket_label_copy(so
->so_label
, intlabel
);
1626 error
= mac_socket_label_externalize(intlabel
, elements
, buffer
, mac
.m_buflen
);
1627 mac_socket_label_free(intlabel
);
1634 case DTYPE_FSEVENTS
:
1636 error
= ENOSYS
; // only sockets/vnodes so far
1639 fp_drop(p
, uap
->fd
, fp
, 0);
1642 error
= copyout(buffer
, mac
.m_string
, strlen(buffer
)+1);
1644 FREE(buffer
, M_MACTEMP
);
1645 FREE(elements
, M_MACTEMP
);
1650 mac_get_filelink(proc_t p
, user_addr_t mac_p
, user_addr_t path_p
, int follow
)
1654 char *elements
, *buffer
;
1655 struct nameidata nd
;
1656 struct label
*intlabel
;
1657 struct user_mac mac
;
1661 if (IS_64BIT_PROCESS(p
)) {
1662 struct user64_mac mac64
;
1663 error
= copyin(mac_p
, &mac64
, sizeof(mac64
));
1664 mac
.m_buflen
= mac64
.m_buflen
;
1665 mac
.m_string
= mac64
.m_string
;
1667 struct user32_mac mac32
;
1668 error
= copyin(mac_p
, &mac32
, sizeof(mac32
));
1669 mac
.m_buflen
= mac32
.m_buflen
;
1670 mac
.m_string
= mac32
.m_string
;
1676 error
= mac_check_structmac_consistent(&mac
);
1680 MALLOC(elements
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
);
1681 MALLOC(buffer
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
| M_ZERO
);
1683 error
= copyinstr(mac
.m_string
, elements
, mac
.m_buflen
, &ulen
);
1685 FREE(buffer
, M_MACTEMP
);
1686 FREE(elements
, M_MACTEMP
);
1689 AUDIT_ARG(mac_string
, elements
);
1691 ctx
= vfs_context_current();
1693 NDINIT(&nd
, LOOKUP
, OP_LOOKUP
,
1694 LOCKLEAF
| (follow
? FOLLOW
: NOFOLLOW
) | AUDITVNPATH1
,
1695 UIO_USERSPACE
, path_p
, ctx
);
1698 FREE(buffer
, M_MACTEMP
);
1699 FREE(elements
, M_MACTEMP
);
1706 intlabel
= mac_vnode_label_alloc();
1707 mac_vnode_label_copy(vp
->v_label
, intlabel
);
1708 error
= mac_vnode_label_externalize(intlabel
, elements
, buffer
,
1709 mac
.m_buflen
, M_WAITOK
);
1710 mac_vnode_label_free(intlabel
);
1712 error
= copyout(buffer
, mac
.m_string
, strlen(buffer
) + 1);
1716 FREE(buffer
, M_MACTEMP
);
1717 FREE(elements
, M_MACTEMP
);
1723 __mac_get_file(proc_t p
, struct __mac_get_file_args
*uap
,
1727 return (mac_get_filelink(p
, uap
->mac_p
, uap
->path_p
, 1));
1731 __mac_get_link(proc_t p
, struct __mac_get_link_args
*uap
,
1735 return (mac_get_filelink(p
, uap
->mac_p
, uap
->path_p
, 0));
1739 __mac_set_fd(proc_t p
, struct __mac_set_fd_args
*uap
, int *ret __unused
)
1742 struct fileproc
*fp
;
1743 struct user_mac mac
;
1744 struct vfs_context
*ctx
= vfs_context_current();
1748 struct label
*intlabel
;
1749 #if CONFIG_MACF_SOCKET
1754 AUDIT_ARG(fd
, uap
->fd
);
1756 if (IS_64BIT_PROCESS(p
)) {
1757 struct user64_mac mac64
;
1758 error
= copyin(uap
->mac_p
, &mac64
, sizeof(mac64
));
1759 mac
.m_buflen
= mac64
.m_buflen
;
1760 mac
.m_string
= mac64
.m_string
;
1762 struct user32_mac mac32
;
1763 error
= copyin(uap
->mac_p
, &mac32
, sizeof(mac32
));
1764 mac
.m_buflen
= mac32
.m_buflen
;
1765 mac
.m_string
= mac32
.m_string
;
1770 error
= mac_check_structmac_consistent(&mac
);
1774 MALLOC(buffer
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
);
1775 error
= copyinstr(mac
.m_string
, buffer
, mac
.m_buflen
, &ulen
);
1777 FREE(buffer
, M_MACTEMP
);
1780 AUDIT_ARG(mac_string
, buffer
);
1782 error
= fp_lookup(p
, uap
->fd
, &fp
, 0);
1784 FREE(buffer
, M_MACTEMP
);
1789 error
= mac_file_check_set(vfs_context_ucred(ctx
), fp
->f_fglob
, buffer
, mac
.m_buflen
);
1791 fp_drop(p
, uap
->fd
, fp
, 0);
1792 FREE(buffer
, M_MACTEMP
);
1796 switch (FILEGLOB_DTYPE(fp
->f_fglob
)) {
1799 if (mac_label_vnodes
== 0) {
1804 intlabel
= mac_vnode_label_alloc();
1806 error
= mac_vnode_label_internalize(intlabel
, buffer
);
1808 mac_vnode_label_free(intlabel
);
1813 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
1815 error
= vnode_getwithref(vp
);
1817 error
= vn_setlabel(vp
, intlabel
, ctx
);
1820 mac_vnode_label_free(intlabel
);
1824 #if CONFIG_MACF_SOCKET
1825 intlabel
= mac_socket_label_alloc(MAC_WAITOK
);
1826 error
= mac_socket_label_internalize(intlabel
, buffer
);
1828 so
= (struct socket
*) fp
->f_fglob
->fg_data
;
1830 error
= mac_socket_label_update(vfs_context_ucred(ctx
), so
, intlabel
);
1833 mac_socket_label_free(intlabel
);
1840 case DTYPE_FSEVENTS
:
1842 error
= ENOSYS
; // only sockets/vnodes so far
1846 fp_drop(p
, uap
->fd
, fp
, 0);
1847 FREE(buffer
, M_MACTEMP
);
1852 mac_set_filelink(proc_t p
, user_addr_t mac_p
, user_addr_t path_p
,
1855 register struct vnode
*vp
;
1856 struct vfs_context
*ctx
= vfs_context_current();
1857 struct label
*intlabel
;
1858 struct nameidata nd
;
1859 struct user_mac mac
;
1864 if (mac_label_vnodes
== 0)
1867 if (IS_64BIT_PROCESS(p
)) {
1868 struct user64_mac mac64
;
1869 error
= copyin(mac_p
, &mac64
, sizeof(mac64
));
1870 mac
.m_buflen
= mac64
.m_buflen
;
1871 mac
.m_string
= mac64
.m_string
;
1873 struct user32_mac mac32
;
1874 error
= copyin(mac_p
, &mac32
, sizeof(mac32
));
1875 mac
.m_buflen
= mac32
.m_buflen
;
1876 mac
.m_string
= mac32
.m_string
;
1881 error
= mac_check_structmac_consistent(&mac
);
1883 printf("mac_set_file: failed structure consistency check\n");
1887 MALLOC(buffer
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
);
1888 error
= copyinstr(mac
.m_string
, buffer
, mac
.m_buflen
, &ulen
);
1890 FREE(buffer
, M_MACTEMP
);
1893 AUDIT_ARG(mac_string
, buffer
);
1895 intlabel
= mac_vnode_label_alloc();
1896 error
= mac_vnode_label_internalize(intlabel
, buffer
);
1897 FREE(buffer
, M_MACTEMP
);
1899 mac_vnode_label_free(intlabel
);
1903 NDINIT(&nd
, LOOKUP
, OP_LOOKUP
,
1904 LOCKLEAF
| (follow
? FOLLOW
: NOFOLLOW
) | AUDITVNPATH1
,
1905 UIO_USERSPACE
, path_p
, ctx
);
1908 mac_vnode_label_free(intlabel
);
1915 error
= vn_setlabel(vp
, intlabel
, ctx
);
1917 mac_vnode_label_free(intlabel
);
1923 __mac_set_file(proc_t p
, struct __mac_set_file_args
*uap
,
1927 return (mac_set_filelink(p
, uap
->mac_p
, uap
->path_p
, 1));
1931 __mac_set_link(proc_t p
, struct __mac_set_link_args
*uap
,
1935 return (mac_set_filelink(p
, uap
->mac_p
, uap
->path_p
, 0));
1939 * __mac_syscall: Perform a MAC policy system call
1941 * Parameters: p Process calling this routine
1942 * uap User argument descriptor (see below)
1945 * Indirect: uap->policy Name of target MAC policy
1946 * uap->call MAC policy-specific system call to perform
1947 * uap->arg MAC policy-specific system call arguments
1949 * Returns: 0 Success
1954 __mac_syscall(proc_t p
, struct __mac_syscall_args
*uap
, int *retv __unused
)
1956 struct mac_policy_conf
*mpc
;
1957 char target
[MAC_MAX_POLICY_NAME
];
1962 error
= copyinstr(uap
->policy
, target
, sizeof(target
), &ulen
);
1965 AUDIT_ARG(value32
, uap
->call
);
1966 AUDIT_ARG(mac_string
, target
);
1970 for (i
= 0; i
< mac_policy_list
.staticmax
; i
++) {
1971 mpc
= mac_policy_list
.entries
[i
].mpc
;
1975 if (strcmp(mpc
->mpc_name
, target
) == 0 &&
1976 mpc
->mpc_ops
->mpo_policy_syscall
!= NULL
) {
1977 error
= mpc
->mpc_ops
->mpo_policy_syscall(p
,
1978 uap
->call
, uap
->arg
);
1982 if (mac_policy_list_conditional_busy() != 0) {
1983 for (; i
<= mac_policy_list
.maxindex
; i
++) {
1984 mpc
= mac_policy_list
.entries
[i
].mpc
;
1988 if (strcmp(mpc
->mpc_name
, target
) == 0 &&
1989 mpc
->mpc_ops
->mpo_policy_syscall
!= NULL
) {
1990 error
= mpc
->mpc_ops
->mpo_policy_syscall(p
,
1991 uap
->call
, uap
->arg
);
1995 mac_policy_list_unbusy();
2003 mac_mount_label_get(struct mount
*mp
, user_addr_t mac_p
)
2005 char *elements
, *buffer
;
2006 struct label
*label
;
2007 struct user_mac mac
;
2011 if (IS_64BIT_PROCESS(current_proc())) {
2012 struct user64_mac mac64
;
2013 error
= copyin(mac_p
, &mac64
, sizeof(mac64
));
2014 mac
.m_buflen
= mac64
.m_buflen
;
2015 mac
.m_string
= mac64
.m_string
;
2017 struct user32_mac mac32
;
2018 error
= copyin(mac_p
, &mac32
, sizeof(mac32
));
2019 mac
.m_buflen
= mac32
.m_buflen
;
2020 mac
.m_string
= mac32
.m_string
;
2025 error
= mac_check_structmac_consistent(&mac
);
2029 MALLOC(elements
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
);
2030 error
= copyinstr(mac
.m_string
, elements
, mac
.m_buflen
, &ulen
);
2032 FREE(elements
, M_MACTEMP
);
2035 AUDIT_ARG(mac_string
, elements
);
2037 label
= mp
->mnt_mntlabel
;
2038 MALLOC(buffer
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
| M_ZERO
);
2039 error
= mac_mount_label_externalize(label
, elements
, buffer
,
2041 FREE(elements
, M_MACTEMP
);
2044 error
= copyout(buffer
, mac
.m_string
, strlen(buffer
) + 1);
2045 FREE(buffer
, M_MACTEMP
);
2051 * __mac_get_mount: Get mount point label information for a given pathname
2053 * Parameters: p (ignored)
2054 * uap User argument descriptor (see below)
2057 * Indirect: uap->path Pathname
2058 * uap->mac_p MAC info
2060 * Returns: 0 Success
2064 __mac_get_mount(proc_t p __unused
, struct __mac_get_mount_args
*uap
,
2067 struct nameidata nd
;
2068 struct vfs_context
*ctx
= vfs_context_current();
2072 NDINIT(&nd
, LOOKUP
, OP_LOOKUP
, FOLLOW
| AUDITVNPATH1
,
2073 UIO_USERSPACE
, uap
->path
, ctx
);
2078 mp
= nd
.ni_vp
->v_mount
;
2079 vnode_put(nd
.ni_vp
);
2082 return mac_mount_label_get(mp
, uap
->mac_p
);
2086 * mac_schedule_userret()
2088 * Schedule a callback to the mpo_thread_userret hook. The mpo_thread_userret
2089 * hook is called just before the thread exit from the kernel in ast_taken().
2091 * Returns: 0 Success
2095 mac_schedule_userret(void)
2098 act_set_astmacf(current_thread());
2105 * Do a Mach exception. This should only be done in the mpo_thread_userret
2108 * params: code exception code
2109 * subcode exception subcode
2111 * MAC_DOEXCF_TRACED Only do exception if being
2115 * Returns: 0 Success
2119 mac_do_machexc(int64_t code
, int64_t subcode
, uint32_t flags
)
2121 mach_exception_data_type_t codes
[EXCEPTION_CODE_MAX
];
2122 proc_t p
= current_proc();
2124 /* Only allow execption codes in MACF's reserved range. */
2125 if ((code
< EXC_MACF_MIN
) || (code
> EXC_MACF_MAX
))
2128 if (flags
& MAC_DOEXCF_TRACED
&&
2129 !(p
->p_lflag
& P_LTRACED
&& (p
->p_lflag
& P_LPPWAIT
) == 0))
2133 /* Send the Mach exception */
2134 codes
[0] = (mach_exception_data_type_t
)code
;
2135 codes
[1] = (mach_exception_data_type_t
)subcode
;
2137 return (bsd_exception(EXC_SOFTWARE
, codes
, 2) != KERN_SUCCESS
);
2142 void (*load_security_extensions_function
)(void) = 0;
2144 struct sysctl_oid_list sysctl__security_mac_children
;
2147 mac_policy_register(struct mac_policy_conf
*mpc __unused
,
2148 mac_policy_handle_t
*handlep __unused
, void *xd __unused
)
2155 mac_policy_unregister(mac_policy_handle_t handle __unused
)
2162 mac_audit_text(char *text __unused
, mac_policy_handle_t handle __unused
)
2169 mac_vnop_setxattr(struct vnode
*vp __unused
, const char *name __unused
, char *buf __unused
, size_t len __unused
)
2176 mac_vnop_getxattr(struct vnode
*vp __unused
, const char *name __unused
,
2177 char *buf __unused
, size_t len __unused
, size_t *attrlen __unused
)
2184 mac_vnop_removexattr(struct vnode
*vp __unused
, const char *name __unused
)
2190 intptr_t mac_label_get(struct label
*l __unused
, int slot __unused
)
2195 void mac_label_set(struct label
*l __unused
, int slot __unused
, intptr_t v __unused
)
2200 struct label
*mac_thread_get_threadlabel(struct thread
*thread __unused
)
2205 struct label
*mac_thread_get_uthreadlabel(struct uthread
*uthread __unused
)
2210 void mac_proc_set_enforce(proc_t p
, int enforce_flags
);
2211 void mac_proc_set_enforce(proc_t p __unused
, int enforce_flags __unused
)
2216 int mac_iokit_check_hid_control(kauth_cred_t cred __unused
);
2217 int mac_iokit_check_hid_control(kauth_cred_t cred __unused
)