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 * Declare that the kernel provides MAC support, version 1. This permits
129 * modules to refuse to be loaded if the necessary support isn't present,
130 * even if it's pre-boot.
133 MODULE_VERSION(kernel_mac_support
, 1);
136 #if MAC_MAX_SLOTS > 32
137 #error "MAC_MAX_SLOTS too large"
140 static unsigned int mac_max_slots
= MAC_MAX_SLOTS
;
141 static unsigned int mac_slot_offsets_free
= (1 << MAC_MAX_SLOTS
) - 1;
142 SYSCTL_UINT(_security_mac
, OID_AUTO
, max_slots
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
143 &mac_max_slots
, 0, "");
146 * Has the kernel started generating labeled objects yet? All read/write
147 * access to this variable is serialized during the boot process. Following
148 * the end of serialization, we don't update this flag; no locking.
153 * Flag to indicate whether or not we should allocate label storage for
154 * new mbufs. Since most dynamic policies we currently work with don't
155 * rely on mbuf labeling, try to avoid paying the cost of mtag allocation
156 * unless specifically notified of interest. One result of this is
157 * that if a dynamically loaded policy requests mbuf labels, it must
158 * be able to deal with a NULL label being returned on any mbufs that
159 * were already in flight when the policy was loaded. Since the policy
160 * already has to deal with uninitialized labels, this probably won't
161 * be a problem. Note: currently no locking. Will this be a problem?
164 unsigned int mac_label_mbufs
= 1;
165 SYSCTL_UINT(_security_mac
, OID_AUTO
, label_mbufs
, SECURITY_MAC_CTLFLAGS
,
166 &mac_label_mbufs
, 0, "Label all MBUFs");
171 * Flag to indicate whether or not we should allocate label storage for
172 * new vnodes. Since most dynamic policies we currently work with don't
173 * rely on vnode labeling, try to avoid paying the cost of mtag allocation
174 * unless specifically notified of interest. One result of this is
175 * that if a dynamically loaded policy requests vnode labels, it must
176 * be able to deal with a NULL label being returned on any vnodes that
177 * were already in flight when the policy was loaded. Since the policy
178 * already has to deal with uninitialized labels, this probably won't
181 unsigned int mac_label_vnodes
= 0;
182 SYSCTL_UINT(_security_mac
, OID_AUTO
, labelvnodes
, SECURITY_MAC_CTLFLAGS
,
183 &mac_label_vnodes
, 0, "Label all vnodes");
186 unsigned int mac_mmap_revocation
= 0;
187 SYSCTL_UINT(_security_mac
, OID_AUTO
, mmap_revocation
, SECURITY_MAC_CTLFLAGS
,
188 &mac_mmap_revocation
, 0, "Revoke mmap access to files on subject "
191 unsigned int mac_mmap_revocation_via_cow
= 0;
192 SYSCTL_UINT(_security_mac
, OID_AUTO
, mmap_revocation_via_cow
, SECURITY_MAC_CTLFLAGS
,
193 &mac_mmap_revocation_via_cow
, 0, "Revoke mmap access to files via "
194 "copy-on-write semantics, or by removing all write access");
196 unsigned int mac_device_enforce
= 1;
197 SYSCTL_UINT(_security_mac
, OID_AUTO
, device_enforce
, SECURITY_MAC_CTLFLAGS
,
198 &mac_device_enforce
, 0, "Enforce MAC policy on device operations");
200 unsigned int mac_pipe_enforce
= 1;
201 SYSCTL_UINT(_security_mac
, OID_AUTO
, pipe_enforce
, SECURITY_MAC_CTLFLAGS
,
202 &mac_pipe_enforce
, 0, "Enforce MAC policy on pipe operations");
204 unsigned int mac_posixsem_enforce
= 1;
205 SYSCTL_UINT(_security_mac
, OID_AUTO
, posixsem_enforce
, SECURITY_MAC_CTLFLAGS
,
206 &mac_posixsem_enforce
, 0, "Enforce MAC policy on POSIX semaphores");
208 unsigned int mac_posixshm_enforce
= 1;
209 SYSCTL_UINT(_security_mac
, OID_AUTO
, posixshm_enforce
, SECURITY_MAC_CTLFLAGS
,
210 &mac_posixshm_enforce
, 0, "Enforce MAC policy on Posix Shared Memory");
212 unsigned int mac_proc_enforce
= 1;
213 SYSCTL_UINT(_security_mac
, OID_AUTO
, proc_enforce
, SECURITY_MAC_CTLFLAGS
,
214 &mac_proc_enforce
, 0, "Enforce MAC policy on process operations");
216 unsigned int mac_socket_enforce
= 1;
217 SYSCTL_UINT(_security_mac
, OID_AUTO
, socket_enforce
, SECURITY_MAC_CTLFLAGS
,
218 &mac_socket_enforce
, 0, "Enforce MAC policy on socket operations");
220 unsigned int mac_system_enforce
= 1;
221 SYSCTL_UINT(_security_mac
, OID_AUTO
, system_enforce
, SECURITY_MAC_CTLFLAGS
,
222 &mac_system_enforce
, 0, "Enforce MAC policy on system-wide interfaces");
224 unsigned int mac_sysvmsg_enforce
= 1;
225 SYSCTL_UINT(_security_mac
, OID_AUTO
, sysvmsg_enforce
, SECURITY_MAC_CTLFLAGS
,
226 &mac_sysvmsg_enforce
, 0, "Enforce MAC policy on System V IPC message queues");
228 unsigned int mac_sysvsem_enforce
= 1;
229 SYSCTL_UINT(_security_mac
, OID_AUTO
, sysvsem_enforce
, SECURITY_MAC_CTLFLAGS
,
230 &mac_sysvsem_enforce
, 0, "Enforce MAC policy on System V IPC semaphores");
232 unsigned int mac_sysvshm_enforce
= 1;
233 SYSCTL_INT(_security_mac
, OID_AUTO
, sysvshm_enforce
, SECURITY_MAC_CTLFLAGS
,
234 &mac_sysvshm_enforce
, 0, "Enforce MAC policy on System V Shared Memory");
236 unsigned int mac_vm_enforce
= 1;
237 SYSCTL_INT(_security_mac
, OID_AUTO
, vm_enforce
, SECURITY_MAC_CTLFLAGS
,
238 &mac_vm_enforce
, 0, "Enforce MAC policy on VM operations");
240 unsigned int mac_vnode_enforce
= 1;
241 SYSCTL_UINT(_security_mac
, OID_AUTO
, vnode_enforce
, SECURITY_MAC_CTLFLAGS
,
242 &mac_vnode_enforce
, 0, "Enforce MAC policy on vnode operations");
246 * mac_audit_data_zone is the zone used for data pushed into the audit
247 * record by policies. Using a zone simplifies memory management of this
248 * data, and allows tracking of the amount of data in flight.
250 extern zone_t mac_audit_data_zone
;
254 * mac_policy_list holds the list of policy modules. Modules with a
255 * handle lower than staticmax are considered "static" and cannot be
256 * unloaded. Such policies can be invoked without holding the busy count.
258 * Modules with a handle at or above the staticmax high water mark
259 * are considered to be "dynamic" policies. A busy count is maintained
260 * for the list, stored in mac_policy_busy. The busy count is protected
261 * by mac_policy_mtx; the list may be modified only while the busy
262 * count is 0, requiring that the lock be held to prevent new references
263 * to the list from being acquired. For almost all operations,
264 * incrementing the busy count is sufficient to guarantee consistency,
265 * as the list cannot be modified while the busy count is elevated.
266 * For a few special operations involving a change to the list of
267 * active policies, the mtx itself must be held.
269 static lck_mtx_t
*mac_policy_mtx
;
272 * Policy list array allocation chunk size. Trying to set this so that we
273 * allocate a page at a time.
275 #define MAC_POLICY_LIST_CHUNKSIZE 512
277 static int mac_policy_busy
;
279 mac_policy_list_t mac_policy_list
;
282 * mac_label_element_list holds the master list of label namespaces for
283 * all the policies. When a policy is loaded, each of it's label namespace
284 * elements is added to the master list if not already present. When a
285 * policy is unloaded, the namespace elements are removed if no other
286 * policy is interested in that namespace element.
288 struct mac_label_element_list_t mac_label_element_list
;
289 struct mac_label_element_list_t mac_static_label_element_list
;
292 mac_policy_grab_exclusive(void)
294 lck_mtx_lock(mac_policy_mtx
);
295 while (mac_policy_busy
!= 0) {
296 lck_mtx_sleep(mac_policy_mtx
, LCK_SLEEP_UNLOCK
,
297 (event_t
)&mac_policy_busy
, THREAD_UNINT
);
298 lck_mtx_lock(mac_policy_mtx
);
303 mac_policy_release_exclusive(void)
306 KASSERT(mac_policy_busy
== 0,
307 ("mac_policy_release_exclusive(): not exclusive"));
308 lck_mtx_unlock(mac_policy_mtx
);
309 thread_wakeup((event_t
) &mac_policy_busy
);
313 mac_policy_list_busy(void)
315 lck_mtx_lock(mac_policy_mtx
);
317 lck_mtx_unlock(mac_policy_mtx
);
321 mac_policy_list_conditional_busy(void)
325 if (mac_policy_list
.numloaded
<= mac_policy_list
.staticmax
)
328 lck_mtx_lock(mac_policy_mtx
);
329 if (mac_policy_list
.numloaded
> mac_policy_list
.staticmax
) {
334 lck_mtx_unlock(mac_policy_mtx
);
339 mac_policy_list_unbusy(void)
341 lck_mtx_lock(mac_policy_mtx
);
343 KASSERT(mac_policy_busy
>= 0, ("MAC_POLICY_LIST_LOCK"));
344 if (mac_policy_busy
== 0)
345 thread_wakeup(&mac_policy_busy
);
346 lck_mtx_unlock(mac_policy_mtx
);
350 * Early pre-malloc MAC initialization, including appropriate SMP locks.
353 mac_policy_init(void)
355 lck_grp_attr_t
*mac_lck_grp_attr
;
356 lck_attr_t
*mac_lck_attr
;
357 lck_grp_t
*mac_lck_grp
;
359 mac_policy_list
.numloaded
= 0;
360 mac_policy_list
.max
= MAC_POLICY_LIST_CHUNKSIZE
;
361 mac_policy_list
.maxindex
= 0;
362 mac_policy_list
.staticmax
= 0;
363 mac_policy_list
.freehint
= 0;
364 mac_policy_list
.chunks
= 1;
366 mac_policy_list
.entries
= kalloc(sizeof(struct mac_policy_list_element
) * MAC_POLICY_LIST_CHUNKSIZE
);
367 bzero(mac_policy_list
.entries
, sizeof(struct mac_policy_list_element
) * MAC_POLICY_LIST_CHUNKSIZE
);
369 LIST_INIT(&mac_label_element_list
);
370 LIST_INIT(&mac_static_label_element_list
);
372 mac_lck_grp_attr
= lck_grp_attr_alloc_init();
373 lck_grp_attr_setstat(mac_lck_grp_attr
);
374 mac_lck_grp
= lck_grp_alloc_init("MAC lock", mac_lck_grp_attr
);
375 mac_lck_attr
= lck_attr_alloc_init();
376 lck_attr_setdefault(mac_lck_attr
);
377 mac_policy_mtx
= lck_mtx_alloc_init(mac_lck_grp
, mac_lck_attr
);
378 lck_attr_free(mac_lck_attr
);
379 lck_grp_attr_free(mac_lck_grp_attr
);
380 lck_grp_free(mac_lck_grp
);
382 mac_labelzone_init();
385 /* Function pointer set up for loading security extensions.
386 * It is set to an actual function after OSlibkernInit()
387 * has been called, and is set back to 0 by OSKextRemoveKextBootstrap()
390 void (*load_security_extensions_function
)(void) = 0;
393 * Init after early Mach startup, but before BSD
396 mac_policy_initmach(void)
400 * For the purposes of modules that want to know if they were
401 * loaded "early", set the mac_late flag once we've processed
402 * modules either linked into the kernel, or loaded before the
406 if (load_security_extensions_function
) {
407 load_security_extensions_function();
416 mac_policy_initbsd(void)
418 struct mac_policy_conf
*mpc
;
422 mac_audit_data_zone
= zinit(MAC_AUDIT_DATA_LIMIT
,
423 AQ_HIWATER
* MAC_AUDIT_DATA_LIMIT
,
424 8192, "mac_audit_data_zone");
427 printf("MAC Framework successfully initialized\n");
429 /* Call bsd init functions of already loaded policies */
432 * Using the exclusive lock means no other framework entry
433 * points can proceed while initializations are running.
434 * This may not be necessary.
436 mac_policy_grab_exclusive();
438 for (i
= 0; i
<= mac_policy_list
.maxindex
; i
++) {
439 mpc
= mac_get_mpc(i
);
440 if ((mpc
!= NULL
) && (mpc
->mpc_ops
->mpo_policy_initbsd
!= NULL
))
441 (*(mpc
->mpc_ops
->mpo_policy_initbsd
))(mpc
);
444 mac_policy_release_exclusive();
448 * After a policy has been loaded, add the label namespaces managed by the
449 * policy to either the static or non-static label namespace list.
450 * A namespace is added to the the list only if it is not already on one of
454 mac_policy_addto_labellist(mac_policy_handle_t handle
, int static_entry
)
456 struct mac_label_listener
**new_mlls
;
457 struct mac_label_element
*mle
, **new_mles
;
458 struct mac_label_element_list_t
*list
;
459 struct mac_policy_conf
*mpc
;
460 const char *name
, *name2
;
461 u_int idx
, mle_free
, mll_free
;
463 mpc
= mac_get_mpc(handle
);
465 if (mpc
->mpc_labelnames
== NULL
)
468 if (mpc
->mpc_labelname_count
== 0)
472 list
= &mac_static_label_element_list
;
474 list
= &mac_label_element_list
;
477 * Before we grab the policy list lock, allocate enough memory
478 * to contain the potential new elements so we don't have to
479 * give up the lock, or allocate with the lock held.
481 MALLOC(new_mles
, struct mac_label_element
**,
482 sizeof(struct mac_label_element
*) *
483 mpc
->mpc_labelname_count
, M_MACTEMP
, M_WAITOK
| M_ZERO
);
484 for (idx
= 0; idx
< mpc
->mpc_labelname_count
; idx
++)
485 MALLOC(new_mles
[idx
], struct mac_label_element
*,
486 sizeof(struct mac_label_element
),
487 M_MACTEMP
, M_WAITOK
);
489 MALLOC(new_mlls
, struct mac_label_listener
**,
490 sizeof(struct mac_label_listener
*) *
491 mpc
->mpc_labelname_count
, M_MACTEMP
, M_WAITOK
);
492 for (idx
= 0; idx
< mpc
->mpc_labelname_count
; idx
++)
493 MALLOC(new_mlls
[idx
], struct mac_label_listener
*,
494 sizeof(struct mac_label_listener
), M_MACTEMP
, M_WAITOK
);
498 mac_policy_grab_exclusive();
499 for (idx
= 0; idx
< mpc
->mpc_labelname_count
; idx
++) {
501 if (*(name
= mpc
->mpc_labelnames
[idx
]) == '?')
504 * Check both label element lists and add to the
505 * appropriate list only if not already on a list.
507 LIST_FOREACH(mle
, &mac_static_label_element_list
, mle_list
) {
508 if (*(name2
= mle
->mle_name
) == '?')
510 if (strcmp(name
, name2
) == 0)
514 LIST_FOREACH(mle
, &mac_label_element_list
, mle_list
) {
515 if (*(name2
= mle
->mle_name
) == '?')
517 if (strcmp(name
, name2
) == 0)
522 mle
= new_mles
[mle_free
];
523 strlcpy(mle
->mle_name
, mpc
->mpc_labelnames
[idx
],
524 MAC_MAX_LABEL_ELEMENT_NAME
);
525 LIST_INIT(&mle
->mle_listeners
);
526 LIST_INSERT_HEAD(list
, mle
, mle_list
);
529 /* Add policy handler as a listener. */
530 new_mlls
[mll_free
]->mll_handle
= handle
;
531 LIST_INSERT_HEAD(&mle
->mle_listeners
, new_mlls
[mll_free
],
536 mac_policy_release_exclusive();
538 /* Free up any unused label elements and listeners */
539 for (idx
= mle_free
; idx
< mpc
->mpc_labelname_count
; idx
++)
540 FREE(new_mles
[idx
], M_MACTEMP
);
541 FREE(new_mles
, M_MACTEMP
);
542 for (idx
= mll_free
; idx
< mpc
->mpc_labelname_count
; idx
++)
543 FREE(new_mlls
[idx
], M_MACTEMP
);
544 FREE(new_mlls
, M_MACTEMP
);
548 * After a policy has been unloaded, remove the label namespaces that the
549 * the policy manages from the non-static list of namespaces.
550 * The removal only takes place when no other policy is interested in the
553 * Must be called with the policy exclusive lock held.
556 mac_policy_removefrom_labellist(mac_policy_handle_t handle
)
558 struct mac_label_listener
*mll
;
559 struct mac_label_element
*mle
;
560 struct mac_policy_conf
*mpc
;
562 mpc
= mac_get_mpc(handle
);
564 if (mpc
->mpc_labelnames
== NULL
)
567 if (mpc
->mpc_labelname_count
== 0)
571 * Unregister policy as being interested in any label
572 * namespaces. If no other policy is listening, remove
573 * that label element from the list. Note that we only
574 * have to worry about the non-static list.
576 LIST_FOREACH(mle
, &mac_label_element_list
, mle_list
) {
577 LIST_FOREACH(mll
, &mle
->mle_listeners
, mll_list
) {
578 if (mll
->mll_handle
== handle
) {
579 LIST_REMOVE(mll
, mll_list
);
580 FREE(mll
, M_MACTEMP
);
581 if (LIST_EMPTY(&mle
->mle_listeners
)) {
582 LIST_REMOVE(mle
, mle_list
);
583 FREE(mle
, M_MACTEMP
);
592 * After the policy list has changed, walk the list to update any global
596 mac_policy_updateflags(void)
601 mac_policy_fixup_mmd_list(struct mac_module_data
*new)
603 struct mac_module_data
*old
;
604 struct mac_module_data_element
*ele
, *aele
;
605 struct mac_module_data_list
*arr
, *dict
;
606 unsigned int i
, j
, k
;
608 old
= new->base_addr
;
609 DPRINTF(("fixup_mmd: old %p new %p\n", old
, new));
610 for (i
= 0; i
< new->count
; i
++) {
611 ele
= &(new->data
[i
]);
612 DPRINTF(("fixup_mmd: ele %p\n", ele
));
613 DPRINTF((" key %p value %p\n", ele
->key
, ele
->value
));
614 mmd_fixup_ele(old
, new, ele
); /* Fix up key/value ptrs. */
615 DPRINTF((" key %p value %p\n", ele
->key
, ele
->value
));
616 if (ele
->value_type
== MAC_DATA_TYPE_ARRAY
) {
617 arr
= (struct mac_module_data_list
*)ele
->value
;
618 DPRINTF(("fixup_mmd: array @%p\n", arr
));
619 for (j
= 0; j
< arr
->count
; j
++) {
620 aele
= &(arr
->list
[j
]);
621 DPRINTF(("fixup_mmd: aele %p\n", aele
));
622 DPRINTF((" key %p value %p\n", aele
->key
, aele
->value
));
623 mmd_fixup_ele(old
, new, aele
);
624 DPRINTF((" key %p value %p\n", aele
->key
, aele
->value
));
625 if (arr
->type
== MAC_DATA_TYPE_DICT
) {
626 dict
= (struct mac_module_data_list
*)aele
->value
;
627 DPRINTF(("fixup_mmd: dict @%p\n", dict
));
628 for (k
= 0; k
< dict
->count
; k
++)
629 mmd_fixup_ele(old
, new,
635 new->base_addr
= new;
639 mac_policy_register(struct mac_policy_conf
*mpc
, mac_policy_handle_t
*handlep
,
642 struct mac_policy_list_element
*tmac_policy_list_element
;
643 int error
, slot
, static_entry
= 0;
647 * Some preliminary checks to make sure the policy's conf structure
648 * contains the required fields.
650 if (mpc
->mpc_name
== NULL
)
651 panic("policy's name is not set\n");
653 if (mpc
->mpc_fullname
== NULL
)
654 panic("policy's full name is not set\n");
656 if (mpc
->mpc_labelname_count
> MAC_MAX_MANAGED_NAMESPACES
)
657 panic("policy's managed label namespaces exceeds maximum\n");
659 if (mpc
->mpc_ops
== NULL
)
660 panic("policy's OPs field is NULL\n");
665 if (mpc
->mpc_loadtime_flags
& MPC_LOADTIME_FLAG_NOTLATE
) {
666 printf("Module %s does not support late loading.\n",
670 mac_policy_grab_exclusive();
673 if (mac_policy_list
.numloaded
>= mac_policy_list
.max
) {
674 /* allocate new policy list array, zero new chunk */
675 tmac_policy_list_element
=
676 kalloc((sizeof(struct mac_policy_list_element
) *
677 MAC_POLICY_LIST_CHUNKSIZE
) * (mac_policy_list
.chunks
+ 1));
678 bzero(&tmac_policy_list_element
[mac_policy_list
.max
],
679 sizeof(struct mac_policy_list_element
) *
680 MAC_POLICY_LIST_CHUNKSIZE
);
682 /* copy old entries into new list */
683 memcpy(tmac_policy_list_element
, mac_policy_list
.entries
,
684 sizeof(struct mac_policy_list_element
) *
685 MAC_POLICY_LIST_CHUNKSIZE
* mac_policy_list
.chunks
);
688 kfree(mac_policy_list
.entries
,
689 sizeof(struct mac_policy_list_element
) *
690 MAC_POLICY_LIST_CHUNKSIZE
* mac_policy_list
.chunks
);
692 mac_policy_list
.entries
= tmac_policy_list_element
;
694 /* Update maximums, etc */
695 mac_policy_list
.max
+= MAC_POLICY_LIST_CHUNKSIZE
;
696 mac_policy_list
.chunks
++;
699 /* Check for policy with same name already loaded */
700 for (i
= 0; i
<= mac_policy_list
.maxindex
; i
++) {
701 if (mac_policy_list
.entries
[i
].mpc
== NULL
)
704 if (strcmp(mac_policy_list
.entries
[i
].mpc
->mpc_name
,
705 mpc
->mpc_name
) == 0) {
711 if (mpc
->mpc_field_off
!= NULL
) {
712 slot
= ffs(mac_slot_offsets_free
);
718 mac_slot_offsets_free
&= ~(1 << slot
);
719 *mpc
->mpc_field_off
= slot
;
721 mpc
->mpc_runtime_flags
|= MPC_RUNTIME_FLAG_REGISTERED
;
724 struct mac_module_data
*mmd
= xd
; /* module data from plist */
726 /* Make a copy of the data. */
727 mpc
->mpc_data
= (void *)kalloc(mmd
->size
);
728 if (mpc
->mpc_data
!= NULL
) {
729 memcpy(mpc
->mpc_data
, mmd
, mmd
->size
);
731 /* Fix up pointers after copy. */
732 mac_policy_fixup_mmd_list(mpc
->mpc_data
);
736 /* Find the first free handle in the list (using our hint). */
737 for (i
= mac_policy_list
.freehint
; i
< mac_policy_list
.max
; i
++) {
738 if (mac_policy_list
.entries
[i
].mpc
== NULL
) {
740 mac_policy_list
.freehint
= ++i
;
746 * If we are loading a MAC module before the framework has
747 * finished initializing or the module is not unloadable and
748 * we can place its handle adjacent to the last static entry,
749 * bump the static policy high water mark.
750 * Static policies can get by with weaker locking requirements.
753 ((mpc
->mpc_loadtime_flags
& MPC_LOADTIME_FLAG_UNLOADOK
) == 0 &&
754 *handlep
== mac_policy_list
.staticmax
)) {
756 mac_policy_list
.staticmax
++;
759 mac_policy_list
.entries
[*handlep
].mpc
= mpc
;
761 /* Update counters, etc */
762 if (*handlep
> mac_policy_list
.maxindex
)
763 mac_policy_list
.maxindex
= *handlep
;
764 mac_policy_list
.numloaded
++;
766 /* Per-policy initialization. */
767 printf ("calling mpo_policy_init for %s\n", mpc
->mpc_name
);
768 if (mpc
->mpc_ops
->mpo_policy_init
!= NULL
)
769 (*(mpc
->mpc_ops
->mpo_policy_init
))(mpc
);
771 if (mac_late
&& mpc
->mpc_ops
->mpo_policy_initbsd
!= NULL
) {
772 printf ("calling mpo_policy_initbsd for %s\n", mpc
->mpc_name
);
773 (*(mpc
->mpc_ops
->mpo_policy_initbsd
))(mpc
);
776 mac_policy_updateflags();
779 mac_policy_release_exclusive();
781 mac_policy_addto_labellist(*handlep
, static_entry
);
783 printf("Security policy loaded: %s (%s)\n", mpc
->mpc_fullname
,
790 mac_policy_release_exclusive();
796 mac_policy_unregister(mac_policy_handle_t handle
)
798 struct mac_policy_conf
*mpc
;
801 * If we fail the load, we may get a request to unload. Check
802 * to see if we did the run-time registration, and if not,
805 mac_policy_grab_exclusive();
806 mpc
= mac_get_mpc(handle
);
807 if ((mpc
->mpc_runtime_flags
& MPC_RUNTIME_FLAG_REGISTERED
) == 0) {
808 mac_policy_release_exclusive();
814 * Don't allow unloading modules with private data.
816 if (mpc
->mpc_field_off
!= NULL
) {
817 MAC_POLICY_LIST_UNLOCK();
822 * Only allow the unload to proceed if the module is unloadable
823 * by its own definition.
825 if ((mpc
->mpc_loadtime_flags
& MPC_LOADTIME_FLAG_UNLOADOK
) == 0) {
826 mac_policy_release_exclusive();
830 mac_policy_removefrom_labellist(handle
);
832 mac_get_mpc(handle
) = NULL
;
833 if (handle
< mac_policy_list
.freehint
&&
834 handle
>= mac_policy_list
.staticmax
)
835 mac_policy_list
.freehint
= handle
;
837 if (handle
== mac_policy_list
.maxindex
)
838 mac_policy_list
.maxindex
--;
840 mac_policy_list
.numloaded
--;
841 if (mpc
->mpc_field_off
!= NULL
) {
842 mac_slot_offsets_free
|= (1 << *mpc
->mpc_field_off
);
845 if (mpc
->mpc_ops
->mpo_policy_destroy
!= NULL
)
846 (*(mpc
->mpc_ops
->mpo_policy_destroy
))(mpc
);
848 mpc
->mpc_runtime_flags
&= ~MPC_RUNTIME_FLAG_REGISTERED
;
849 mac_policy_updateflags();
851 mac_policy_release_exclusive();
854 struct mac_module_data
*mmd
= mpc
->mpc_data
;
855 kfree(mmd
, mmd
->size
);
856 mpc
->mpc_data
= NULL
;
859 printf("Security policy unload: %s (%s)\n", mpc
->mpc_fullname
,
866 * Define an error value precedence, and given two arguments, selects the
867 * value with the higher precedence.
870 mac_error_select(int error1
, int error2
)
873 /* Certain decision-making errors take top priority. */
874 if (error1
== EDEADLK
|| error2
== EDEADLK
)
877 /* Invalid arguments should be reported where possible. */
878 if (error1
== EINVAL
|| error2
== EINVAL
)
881 /* Precedence goes to "visibility", with both process and file. */
882 if (error1
== ESRCH
|| error2
== ESRCH
)
885 if (error1
== ENOENT
|| error2
== ENOENT
)
888 /* Precedence goes to DAC/MAC protections. */
889 if (error1
== EACCES
|| error2
== EACCES
)
892 /* Precedence goes to privilege. */
893 if (error1
== EPERM
|| error2
== EPERM
)
896 /* Precedence goes to error over success; otherwise, arbitrary. */
903 mac_label_init(struct label
*label
)
906 bzero(label
, sizeof(*label
));
907 label
->l_flags
= MAC_FLAG_INITIALIZED
;
911 mac_label_destroy(struct label
*label
)
914 KASSERT(label
->l_flags
& MAC_FLAG_INITIALIZED
,
915 ("destroying uninitialized label"));
917 bzero(label
, sizeof(*label
));
918 /* implicit: label->l_flags &= ~MAC_FLAG_INITIALIZED; */
922 mac_check_structmac_consistent(struct user_mac
*mac
)
925 if (mac
->m_buflen
> MAC_MAX_LABEL_BUF_LEN
|| mac
->m_buflen
== 0)
932 * Get the external forms of labels from all policies, for a single
933 * label namespace or "*" for all namespaces. Returns ENOENT if no policy
934 * is registered for the namespace, unless the namespace begins with a '?'.
937 mac_label_externalize(size_t mpo_externalize_off
, struct label
*label
,
938 const char *element
, struct sbuf
*sb
)
940 struct mac_policy_conf
*mpc
;
941 struct mac_label_listener
*mll
;
942 struct mac_label_element
*mle
;
943 struct mac_label_element_list_t
*element_list
;
945 int (*mpo_externalize
)(struct label
*, char *, struct sbuf
*);
946 int all_labels
= 0, ignorenotfound
= 0, error
= 0, busy
= FALSE
;
947 unsigned int count
= 0;
949 if (element
[0] == '?') {
952 } else if (element
[0] == '*' && element
[1] == '\0')
955 element_list
= &mac_static_label_element_list
;
957 LIST_FOREACH(mle
, element_list
, mle_list
) {
958 name
= mle
->mle_name
;
965 if (strcmp(name
, element
) != 0)
968 LIST_FOREACH(mll
, &mle
->mle_listeners
, mll_list
) {
969 mpc
= mac_policy_list
.entries
[mll
->mll_handle
].mpc
;
972 mpo_externalize
= *(typeof(mpo_externalize
) *)
973 ((char *)mpc
->mpc_ops
+ mpo_externalize_off
);
974 if (mpo_externalize
== NULL
)
976 error
= sbuf_printf(sb
, "%s/", name
);
979 error
= mpo_externalize(label
, mle
->mle_name
, sb
);
984 * If a policy doesn't have a label to
985 * externalize it returns ENOENT. This
986 * may occur for policies that support
987 * multiple label elements for some
988 * (but not all) object types.
990 sbuf_setpos(sb
, sbuf_len(sb
) -
995 error
= sbuf_putc(sb
, ',');
1001 /* If there are dynamic policies present, check their elements too. */
1002 if (!busy
&& mac_policy_list_conditional_busy() == 1) {
1003 element_list
= &mac_label_element_list
;
1009 mac_policy_list_unbusy();
1010 if (!error
&& count
== 0) {
1011 if (!all_labels
&& !ignorenotfound
)
1012 error
= ENOENT
; /* XXX: ENOLABEL? */
1018 * Get the external forms of labels from all policies, for all label
1019 * namespaces contained in a list.
1021 * XXX This may be leaking an sbuf.
1024 mac_externalize(size_t mpo_externalize_off
, struct label
*label
,
1025 const char *elementlist
, char *outbuf
, size_t outbuflen
)
1033 /* allocate a scratch buffer the size of the string */
1034 MALLOC(scratch_base
, char *, strlen(elementlist
)+1, M_MACTEMP
, M_WAITOK
);
1035 if (scratch_base
== NULL
) {
1040 /* copy the elementlist to the scratch buffer */
1041 strlcpy(scratch_base
, elementlist
, strlen(elementlist
)+1);
1044 * set up a temporary pointer that can be used to iterate the
1045 * scratch buffer without losing the allocation address
1047 scratch
= scratch_base
;
1050 if (sbuf_new(&sb
, outbuf
, outbuflen
, SBUF_FIXEDLEN
) == NULL
) {
1051 /* could not allocate interior buffer */
1055 /* iterate the scratch buffer; NOTE: buffer contents modified! */
1056 while ((element
= strsep(&scratch
, ",")) != NULL
) {
1057 error
= mac_label_externalize(mpo_externalize_off
, label
,
1062 if ((len
= sbuf_len(&sb
)) > 0)
1063 sbuf_setpos(&sb
, len
- 1); /* trim trailing comma */
1067 if (scratch_base
!= NULL
)
1068 FREE(scratch_base
, M_MACTEMP
);
1074 * Have all policies set the internal form of a label, for a single
1078 mac_label_internalize(size_t mpo_internalize_off
, struct label
*label
,
1079 char *element_name
, char *element_data
)
1081 struct mac_policy_conf
*mpc
;
1082 struct mac_label_listener
*mll
;
1083 struct mac_label_element
*mle
;
1084 struct mac_label_element_list_t
*element_list
;
1085 int (*mpo_internalize
)(struct label
*, char *, char *);
1086 int error
= 0, busy
= FALSE
;
1087 unsigned int count
= 0;
1090 element_list
= &mac_static_label_element_list
;
1092 LIST_FOREACH(mle
, element_list
, mle_list
) {
1093 if (*(name
= mle
->mle_name
) == '?')
1095 if (strcmp(element_name
, name
) != 0)
1097 LIST_FOREACH(mll
, &mle
->mle_listeners
, mll_list
) {
1098 mpc
= mac_policy_list
.entries
[mll
->mll_handle
].mpc
;
1101 mpo_internalize
= *(typeof(mpo_internalize
) *)
1102 ((char *)mpc
->mpc_ops
+ mpo_internalize_off
);
1103 if (mpo_internalize
== NULL
)
1105 error
= mpo_internalize(label
, element_name
,
1112 /* If there are dynamic policies present, check their elements too. */
1113 if (!busy
&& mac_policy_list_conditional_busy() == 1) {
1114 element_list
= &mac_label_element_list
;
1120 mac_policy_list_unbusy();
1121 if (!error
&& count
== 0)
1127 mac_internalize(size_t mpo_internalize_off
, struct label
*label
,
1130 char *element_name
, *element_data
;
1133 while (!error
&& (element_name
= strsep(&textlabels
, ",")) != NULL
) {
1134 element_data
= strchr(element_name
, '/');
1135 if (element_data
== NULL
) {
1139 *element_data
++ = '\0';
1140 error
= mac_label_internalize(mpo_internalize_off
, label
,
1141 element_name
, element_data
);
1149 __mac_get_pid(struct proc
*p
, struct __mac_get_pid_args
*uap
, int *ret __unused
)
1151 char *elements
, *buffer
;
1152 struct user_mac mac
;
1154 struct ucred
*tcred
;
1158 AUDIT_ARG(pid
, uap
->pid
);
1159 if (IS_64BIT_PROCESS(p
)) {
1160 struct user64_mac mac64
;
1161 error
= copyin(uap
->mac_p
, &mac64
, sizeof(mac64
));
1162 mac
.m_buflen
= mac64
.m_buflen
;
1163 mac
.m_string
= mac64
.m_string
;
1165 struct user32_mac mac32
;
1166 error
= copyin(uap
->mac_p
, &mac32
, sizeof(mac32
));
1167 mac
.m_buflen
= mac32
.m_buflen
;
1168 mac
.m_string
= mac32
.m_string
;
1173 error
= mac_check_structmac_consistent(&mac
);
1177 tproc
= proc_find(uap
->pid
);
1180 tcred
= kauth_cred_proc_ref(tproc
);
1183 MALLOC(elements
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
);
1184 error
= copyinstr(mac
.m_string
, elements
, mac
.m_buflen
, &ulen
);
1186 FREE(elements
, M_MACTEMP
);
1187 kauth_cred_unref(&tcred
);
1190 AUDIT_ARG(mac_string
, elements
);
1192 MALLOC(buffer
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
| M_ZERO
);
1193 error
= mac_cred_label_externalize(tcred
->cr_label
, elements
,
1194 buffer
, mac
.m_buflen
, M_WAITOK
);
1196 error
= copyout(buffer
, mac
.m_string
, strlen(buffer
)+1);
1198 FREE(buffer
, M_MACTEMP
);
1199 FREE(elements
, M_MACTEMP
);
1200 kauth_cred_unref(&tcred
);
1205 __mac_get_proc(proc_t p
, struct __mac_get_proc_args
*uap
, int *ret __unused
)
1207 char *elements
, *buffer
;
1208 struct user_mac mac
;
1213 if (IS_64BIT_PROCESS(p
)) {
1214 struct user64_mac mac64
;
1215 error
= copyin(uap
->mac_p
, &mac64
, sizeof(mac64
));
1216 mac
.m_buflen
= mac64
.m_buflen
;
1217 mac
.m_string
= mac64
.m_string
;
1219 struct user32_mac mac32
;
1220 error
= copyin(uap
->mac_p
, &mac32
, sizeof(mac32
));
1221 mac
.m_buflen
= mac32
.m_buflen
;
1222 mac
.m_string
= mac32
.m_string
;
1227 error
= mac_check_structmac_consistent(&mac
);
1231 MALLOC(elements
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
);
1232 error
= copyinstr(mac
.m_string
, elements
, mac
.m_buflen
, &ulen
);
1234 FREE(elements
, M_MACTEMP
);
1237 AUDIT_ARG(mac_string
, elements
);
1239 cr
= kauth_cred_proc_ref(p
);
1241 MALLOC(buffer
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
| M_ZERO
);
1242 error
= mac_cred_label_externalize(cr
->cr_label
,
1243 elements
, buffer
, mac
.m_buflen
, M_WAITOK
);
1245 error
= copyout(buffer
, mac
.m_string
, strlen(buffer
)+1);
1247 FREE(buffer
, M_MACTEMP
);
1248 FREE(elements
, M_MACTEMP
);
1249 kauth_cred_unref(&cr
);
1254 __mac_set_proc(proc_t p
, struct __mac_set_proc_args
*uap
, int *ret __unused
)
1256 struct label
*intlabel
;
1257 struct user_mac mac
;
1262 if (IS_64BIT_PROCESS(p
)) {
1263 struct user64_mac mac64
;
1264 error
= copyin(uap
->mac_p
, &mac64
, sizeof(mac64
));
1265 mac
.m_buflen
= mac64
.m_buflen
;
1266 mac
.m_string
= mac64
.m_string
;
1268 struct user32_mac mac32
;
1269 error
= copyin(uap
->mac_p
, &mac32
, sizeof(mac32
));
1270 mac
.m_buflen
= mac32
.m_buflen
;
1271 mac
.m_string
= mac32
.m_string
;
1276 error
= mac_check_structmac_consistent(&mac
);
1280 MALLOC(buffer
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
);
1281 error
= copyinstr(mac
.m_string
, buffer
, mac
.m_buflen
, &ulen
);
1283 FREE(buffer
, M_MACTEMP
);
1286 AUDIT_ARG(mac_string
, buffer
);
1288 intlabel
= mac_cred_label_alloc();
1289 error
= mac_cred_label_internalize(intlabel
, buffer
);
1290 FREE(buffer
, M_MACTEMP
);
1294 error
= mac_cred_check_label_update(kauth_cred_get(), intlabel
);
1299 error
= kauth_proc_label_update(p
, intlabel
);
1304 mac_cred_label_free(intlabel
);
1309 __mac_get_fd(proc_t p
, struct __mac_get_fd_args
*uap
, int *ret __unused
)
1311 struct fileproc
*fp
;
1313 struct user_mac mac
;
1314 char *elements
, *buffer
;
1317 kauth_cred_t my_cred
;
1318 #if CONFIG_MACF_SOCKET
1320 #endif /* MAC_SOCKET */
1321 struct label
*intlabel
;
1323 AUDIT_ARG(fd
, uap
->fd
);
1325 if (IS_64BIT_PROCESS(p
)) {
1326 struct user64_mac mac64
;
1327 error
= copyin(uap
->mac_p
, &mac64
, sizeof(mac64
));
1328 mac
.m_buflen
= mac64
.m_buflen
;
1329 mac
.m_string
= mac64
.m_string
;
1331 struct user32_mac mac32
;
1332 error
= copyin(uap
->mac_p
, &mac32
, sizeof(mac32
));
1333 mac
.m_buflen
= mac32
.m_buflen
;
1334 mac
.m_string
= mac32
.m_string
;
1340 error
= mac_check_structmac_consistent(&mac
);
1344 MALLOC(elements
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
);
1345 error
= copyinstr(mac
.m_string
, elements
, mac
.m_buflen
, &ulen
);
1347 FREE(elements
, M_MACTEMP
);
1350 AUDIT_ARG(mac_string
, elements
);
1352 MALLOC(buffer
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
);
1353 error
= fp_lookup(p
, uap
->fd
, &fp
, 0);
1355 FREE(buffer
, M_MACTEMP
);
1356 FREE(elements
, M_MACTEMP
);
1360 my_cred
= kauth_cred_proc_ref(p
);
1361 error
= mac_file_check_get(my_cred
, fp
->f_fglob
, elements
, mac
.m_buflen
);
1362 kauth_cred_unref(&my_cred
);
1364 fp_drop(p
, uap
->fd
, fp
, 0);
1365 FREE(buffer
, M_MACTEMP
);
1366 FREE(elements
, M_MACTEMP
);
1370 switch (FILEGLOB_DTYPE(fp
->f_fglob
)) {
1372 intlabel
= mac_vnode_label_alloc();
1373 if (intlabel
== NULL
) {
1377 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
1378 error
= vnode_getwithref(vp
);
1380 mac_vnode_label_copy(vp
->v_label
, intlabel
);
1381 error
= mac_vnode_label_externalize(intlabel
,
1383 mac
.m_buflen
, M_WAITOK
);
1386 mac_vnode_label_free(intlabel
);
1389 #if CONFIG_MACF_SOCKET
1390 so
= (struct socket
*) fp
->f_fglob
->fg_data
;
1391 intlabel
= mac_socket_label_alloc(MAC_WAITOK
);
1393 mac_socket_label_copy(so
->so_label
, intlabel
);
1395 error
= mac_socket_label_externalize(intlabel
, elements
, buffer
, mac
.m_buflen
);
1396 mac_socket_label_free(intlabel
);
1403 case DTYPE_FSEVENTS
:
1405 error
= ENOSYS
; // only sockets/vnodes so far
1408 fp_drop(p
, uap
->fd
, fp
, 0);
1411 error
= copyout(buffer
, mac
.m_string
, strlen(buffer
)+1);
1413 FREE(buffer
, M_MACTEMP
);
1414 FREE(elements
, M_MACTEMP
);
1419 mac_get_filelink(proc_t p
, user_addr_t mac_p
, user_addr_t path_p
, int follow
)
1423 char *elements
, *buffer
;
1424 struct nameidata nd
;
1425 struct label
*intlabel
;
1426 struct user_mac mac
;
1430 if (IS_64BIT_PROCESS(p
)) {
1431 struct user64_mac mac64
;
1432 error
= copyin(mac_p
, &mac64
, sizeof(mac64
));
1433 mac
.m_buflen
= mac64
.m_buflen
;
1434 mac
.m_string
= mac64
.m_string
;
1436 struct user32_mac mac32
;
1437 error
= copyin(mac_p
, &mac32
, sizeof(mac32
));
1438 mac
.m_buflen
= mac32
.m_buflen
;
1439 mac
.m_string
= mac32
.m_string
;
1445 error
= mac_check_structmac_consistent(&mac
);
1449 MALLOC(elements
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
);
1450 MALLOC(buffer
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
| M_ZERO
);
1452 error
= copyinstr(mac
.m_string
, elements
, mac
.m_buflen
, &ulen
);
1454 FREE(buffer
, M_MACTEMP
);
1455 FREE(elements
, M_MACTEMP
);
1458 AUDIT_ARG(mac_string
, elements
);
1460 ctx
= vfs_context_current();
1462 NDINIT(&nd
, LOOKUP
, OP_LOOKUP
,
1463 LOCKLEAF
| (follow
? FOLLOW
: NOFOLLOW
) | AUDITVNPATH1
,
1464 UIO_USERSPACE
, path_p
, ctx
);
1467 FREE(buffer
, M_MACTEMP
);
1468 FREE(elements
, M_MACTEMP
);
1475 intlabel
= mac_vnode_label_alloc();
1476 mac_vnode_label_copy(vp
->v_label
, intlabel
);
1477 error
= mac_vnode_label_externalize(intlabel
, elements
, buffer
,
1478 mac
.m_buflen
, M_WAITOK
);
1479 mac_vnode_label_free(intlabel
);
1481 error
= copyout(buffer
, mac
.m_string
, strlen(buffer
) + 1);
1485 FREE(buffer
, M_MACTEMP
);
1486 FREE(elements
, M_MACTEMP
);
1492 __mac_get_file(proc_t p
, struct __mac_get_file_args
*uap
,
1496 return (mac_get_filelink(p
, uap
->mac_p
, uap
->path_p
, 1));
1500 __mac_get_link(proc_t p
, struct __mac_get_link_args
*uap
,
1504 return (mac_get_filelink(p
, uap
->mac_p
, uap
->path_p
, 0));
1508 __mac_set_fd(proc_t p
, struct __mac_set_fd_args
*uap
, int *ret __unused
)
1511 struct fileproc
*fp
;
1512 struct user_mac mac
;
1513 struct vfs_context
*ctx
= vfs_context_current();
1517 struct label
*intlabel
;
1518 #if CONFIG_MACF_SOCKET
1523 AUDIT_ARG(fd
, uap
->fd
);
1525 if (IS_64BIT_PROCESS(p
)) {
1526 struct user64_mac mac64
;
1527 error
= copyin(uap
->mac_p
, &mac64
, sizeof(mac64
));
1528 mac
.m_buflen
= mac64
.m_buflen
;
1529 mac
.m_string
= mac64
.m_string
;
1531 struct user32_mac mac32
;
1532 error
= copyin(uap
->mac_p
, &mac32
, sizeof(mac32
));
1533 mac
.m_buflen
= mac32
.m_buflen
;
1534 mac
.m_string
= mac32
.m_string
;
1539 error
= mac_check_structmac_consistent(&mac
);
1543 MALLOC(buffer
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
);
1544 error
= copyinstr(mac
.m_string
, buffer
, mac
.m_buflen
, &ulen
);
1546 FREE(buffer
, M_MACTEMP
);
1549 AUDIT_ARG(mac_string
, buffer
);
1551 error
= fp_lookup(p
, uap
->fd
, &fp
, 0);
1553 FREE(buffer
, M_MACTEMP
);
1558 error
= mac_file_check_set(vfs_context_ucred(ctx
), fp
->f_fglob
, buffer
, mac
.m_buflen
);
1560 fp_drop(p
, uap
->fd
, fp
, 0);
1561 FREE(buffer
, M_MACTEMP
);
1565 switch (FILEGLOB_DTYPE(fp
->f_fglob
)) {
1568 if (mac_label_vnodes
== 0) {
1573 intlabel
= mac_vnode_label_alloc();
1575 error
= mac_vnode_label_internalize(intlabel
, buffer
);
1577 mac_vnode_label_free(intlabel
);
1582 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
1584 error
= vnode_getwithref(vp
);
1586 error
= vn_setlabel(vp
, intlabel
, ctx
);
1589 mac_vnode_label_free(intlabel
);
1593 #if CONFIG_MACF_SOCKET
1594 intlabel
= mac_socket_label_alloc(MAC_WAITOK
);
1595 error
= mac_socket_label_internalize(intlabel
, buffer
);
1597 so
= (struct socket
*) fp
->f_fglob
->fg_data
;
1599 error
= mac_socket_label_update(vfs_context_ucred(ctx
), so
, intlabel
);
1602 mac_socket_label_free(intlabel
);
1609 case DTYPE_FSEVENTS
:
1611 error
= ENOSYS
; // only sockets/vnodes so far
1615 fp_drop(p
, uap
->fd
, fp
, 0);
1616 FREE(buffer
, M_MACTEMP
);
1621 mac_set_filelink(proc_t p
, user_addr_t mac_p
, user_addr_t path_p
,
1624 register struct vnode
*vp
;
1625 struct vfs_context
*ctx
= vfs_context_current();
1626 struct label
*intlabel
;
1627 struct nameidata nd
;
1628 struct user_mac mac
;
1633 if (mac_label_vnodes
== 0)
1636 if (IS_64BIT_PROCESS(p
)) {
1637 struct user64_mac mac64
;
1638 error
= copyin(mac_p
, &mac64
, sizeof(mac64
));
1639 mac
.m_buflen
= mac64
.m_buflen
;
1640 mac
.m_string
= mac64
.m_string
;
1642 struct user32_mac mac32
;
1643 error
= copyin(mac_p
, &mac32
, sizeof(mac32
));
1644 mac
.m_buflen
= mac32
.m_buflen
;
1645 mac
.m_string
= mac32
.m_string
;
1650 error
= mac_check_structmac_consistent(&mac
);
1652 printf("mac_set_file: failed structure consistency check\n");
1656 MALLOC(buffer
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
);
1657 error
= copyinstr(mac
.m_string
, buffer
, mac
.m_buflen
, &ulen
);
1659 FREE(buffer
, M_MACTEMP
);
1662 AUDIT_ARG(mac_string
, buffer
);
1664 intlabel
= mac_vnode_label_alloc();
1665 error
= mac_vnode_label_internalize(intlabel
, buffer
);
1666 FREE(buffer
, M_MACTEMP
);
1668 mac_vnode_label_free(intlabel
);
1672 NDINIT(&nd
, LOOKUP
, OP_LOOKUP
,
1673 LOCKLEAF
| (follow
? FOLLOW
: NOFOLLOW
) | AUDITVNPATH1
,
1674 UIO_USERSPACE
, path_p
, ctx
);
1677 mac_vnode_label_free(intlabel
);
1684 error
= vn_setlabel(vp
, intlabel
, ctx
);
1686 mac_vnode_label_free(intlabel
);
1692 __mac_set_file(proc_t p
, struct __mac_set_file_args
*uap
,
1696 return (mac_set_filelink(p
, uap
->mac_p
, uap
->path_p
, 1));
1700 __mac_set_link(proc_t p
, struct __mac_set_link_args
*uap
,
1704 return (mac_set_filelink(p
, uap
->mac_p
, uap
->path_p
, 0));
1708 * __mac_syscall: Perform a MAC policy system call
1710 * Parameters: p Process calling this routine
1711 * uap User argument descriptor (see below)
1714 * Indirect: uap->policy Name of target MAC policy
1715 * uap->call MAC policy-specific system call to perform
1716 * uap->arg MAC policy-specific system call arguments
1718 * Returns: 0 Success
1723 __mac_syscall(proc_t p
, struct __mac_syscall_args
*uap
, int *retv __unused
)
1725 struct mac_policy_conf
*mpc
;
1726 char target
[MAC_MAX_POLICY_NAME
];
1731 error
= copyinstr(uap
->policy
, target
, sizeof(target
), &ulen
);
1734 AUDIT_ARG(value32
, uap
->call
);
1735 AUDIT_ARG(mac_string
, target
);
1739 for (i
= 0; i
< mac_policy_list
.staticmax
; i
++) {
1740 mpc
= mac_policy_list
.entries
[i
].mpc
;
1744 if (strcmp(mpc
->mpc_name
, target
) == 0 &&
1745 mpc
->mpc_ops
->mpo_policy_syscall
!= NULL
) {
1746 error
= mpc
->mpc_ops
->mpo_policy_syscall(p
,
1747 uap
->call
, uap
->arg
);
1751 if (mac_policy_list_conditional_busy() != 0) {
1752 for (; i
<= mac_policy_list
.maxindex
; i
++) {
1753 mpc
= mac_policy_list
.entries
[i
].mpc
;
1757 if (strcmp(mpc
->mpc_name
, target
) == 0 &&
1758 mpc
->mpc_ops
->mpo_policy_syscall
!= NULL
) {
1759 error
= mpc
->mpc_ops
->mpo_policy_syscall(p
,
1760 uap
->call
, uap
->arg
);
1764 mac_policy_list_unbusy();
1772 mac_mount_label_get(struct mount
*mp
, user_addr_t mac_p
)
1774 char *elements
, *buffer
;
1775 struct label
*label
;
1776 struct user_mac mac
;
1780 if (IS_64BIT_PROCESS(current_proc())) {
1781 struct user64_mac mac64
;
1782 error
= copyin(mac_p
, &mac64
, sizeof(mac64
));
1783 mac
.m_buflen
= mac64
.m_buflen
;
1784 mac
.m_string
= mac64
.m_string
;
1786 struct user32_mac mac32
;
1787 error
= copyin(mac_p
, &mac32
, sizeof(mac32
));
1788 mac
.m_buflen
= mac32
.m_buflen
;
1789 mac
.m_string
= mac32
.m_string
;
1794 error
= mac_check_structmac_consistent(&mac
);
1798 MALLOC(elements
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
);
1799 error
= copyinstr(mac
.m_string
, elements
, mac
.m_buflen
, &ulen
);
1801 FREE(elements
, M_MACTEMP
);
1804 AUDIT_ARG(mac_string
, elements
);
1806 label
= mp
->mnt_mntlabel
;
1807 MALLOC(buffer
, char *, mac
.m_buflen
, M_MACTEMP
, M_WAITOK
| M_ZERO
);
1808 error
= mac_mount_label_externalize(label
, elements
, buffer
,
1810 FREE(elements
, M_MACTEMP
);
1813 error
= copyout(buffer
, mac
.m_string
, strlen(buffer
) + 1);
1814 FREE(buffer
, M_MACTEMP
);
1820 * __mac_get_mount: Get mount point label information for a given pathname
1822 * Parameters: p (ignored)
1823 * uap User argument descriptor (see below)
1826 * Indirect: uap->path Pathname
1827 * uap->mac_p MAC info
1829 * Returns: 0 Success
1833 __mac_get_mount(proc_t p __unused
, struct __mac_get_mount_args
*uap
,
1836 struct nameidata nd
;
1837 struct vfs_context
*ctx
= vfs_context_current();
1841 NDINIT(&nd
, LOOKUP
, OP_LOOKUP
, FOLLOW
| AUDITVNPATH1
,
1842 UIO_USERSPACE
, uap
->path
, ctx
);
1847 mp
= nd
.ni_vp
->v_mount
;
1848 vnode_put(nd
.ni_vp
);
1851 return mac_mount_label_get(mp
, uap
->mac_p
);
1855 * mac_schedule_userret()
1857 * Schedule a callback to the mpo_thread_userret hook. The mpo_thread_userret
1858 * hook is called just before the thread exit from the kernel in ast_taken().
1860 * Returns: 0 Success
1864 mac_schedule_userret(void)
1867 act_set_astmacf(current_thread());
1874 * Do a Mach exception. This should only be done in the mpo_thread_userret
1877 * params: code exception code
1878 * subcode exception subcode
1880 * MAC_DOEXCF_TRACED Only do exception if being
1884 * Returns: 0 Success
1888 mac_do_machexc(int64_t code
, int64_t subcode
, uint32_t flags
)
1890 mach_exception_data_type_t codes
[EXCEPTION_CODE_MAX
];
1891 proc_t p
= current_proc();
1893 /* Only allow execption codes in MACF's reserved range. */
1894 if ((code
< EXC_MACF_MIN
) || (code
> EXC_MACF_MAX
))
1897 if (flags
& MAC_DOEXCF_TRACED
&&
1898 !(p
->p_lflag
& P_LTRACED
&& (p
->p_lflag
& P_LPPWAIT
) == 0))
1902 /* Send the Mach exception */
1903 codes
[0] = (mach_exception_data_type_t
)code
;
1904 codes
[1] = (mach_exception_data_type_t
)subcode
;
1906 return (bsd_exception(EXC_SOFTWARE
, codes
, 2) != KERN_SUCCESS
);
1911 void (*load_security_extensions_function
)(void) = 0;
1913 struct sysctl_oid_list sysctl__security_mac_children
;
1916 mac_policy_register(struct mac_policy_conf
*mpc __unused
,
1917 mac_policy_handle_t
*handlep __unused
, void *xd __unused
)
1924 mac_policy_unregister(mac_policy_handle_t handle __unused
)
1931 mac_audit_text(char *text __unused
, mac_policy_handle_t handle __unused
)
1938 mac_vnop_setxattr(struct vnode
*vp __unused
, const char *name __unused
, char *buf __unused
, size_t len __unused
)
1945 mac_vnop_getxattr(struct vnode
*vp __unused
, const char *name __unused
,
1946 char *buf __unused
, size_t len __unused
, size_t *attrlen __unused
)
1953 mac_vnop_removexattr(struct vnode
*vp __unused
, const char *name __unused
)
1959 intptr_t mac_label_get(struct label
*l __unused
, int slot __unused
)
1964 void mac_label_set(struct label
*l __unused
, int slot __unused
, intptr_t v __unused
)
1969 void mac_proc_set_enforce(proc_t p
, int enforce_flags
);
1970 void mac_proc_set_enforce(proc_t p __unused
, int enforce_flags __unused
)
1975 int mac_iokit_check_hid_control(kauth_cred_t cred __unused
);
1976 int mac_iokit_check_hid_control(kauth_cred_t cred __unused
)