2 * Copyright (c) 1999-2002, 2006, 2009 Robert N. M. Watson
3 * Copyright (c) 2001 Ilmar S. Habibulin
4 * Copyright (c) 2001-2005 Networks Associates Technology, Inc.
5 * Copyright (c) 2005-2006 SPARTA, Inc.
6 * Copyright (c) 2008-2009 Apple Inc.
9 * This software was developed by Robert Watson and Ilmar Habibulin for the
12 * This software was developed for the FreeBSD Project in part by Network
13 * Associates Laboratories, the Security Research Division of Network
14 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
15 * as part of the DARPA CHATS research program.
17 * This software was enhanced by SPARTA ISSO under SPAWAR contract
18 * N66001-04-C-6019 ("SEFOS").
20 * This software was developed at the University of Cambridge Computer
21 * Laboratory with support from a grant from Google, Inc.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
32 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * Framework for extensible kernel access control. This file contains core
47 * kernel infrastructure for the TrustedBSD MAC Framework, including policy
48 * registration, versioning, locking, error composition operator, and system
51 * The MAC Framework implements three programming interfaces:
53 * - The kernel MAC interface, defined in mac_framework.h, and invoked
54 * throughout the kernel to request security decisions, notify of security
55 * related events, etc.
57 * - The MAC policy module interface, defined in mac_policy.h, which is
58 * implemented by MAC policy modules and invoked by the MAC Framework to
59 * forward kernel security requests and notifications to policy modules.
61 * - The user MAC API, defined in mac.h, which allows user programs to query
62 * and set label state on objects.
64 * The majority of the MAC Framework implementation may be found in
65 * src/sys/security/mac. Sample policy modules may be found in
66 * src/sys/security/mac_*.
71 #include <sys/cdefs.h>
72 __FBSDID("$FreeBSD$");
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/condvar.h>
77 #include <sys/kernel.h>
80 #include <sys/module.h>
81 #include <sys/rmlock.h>
84 #include <sys/sysctl.h>
86 #include <security/mac/mac_framework.h>
87 #include <security/mac/mac_internal.h>
88 #include <security/mac/mac_policy.h>
91 * DTrace SDT providers for MAC.
93 SDT_PROVIDER_DEFINE(mac);
94 SDT_PROVIDER_DEFINE(mac_framework);
96 SDT_PROBE_DEFINE2(mac, , policy, modevent, "int",
97 "struct mac_policy_conf *");
98 SDT_PROBE_DEFINE1(mac, , policy, register,
99 "struct mac_policy_conf *");
100 SDT_PROBE_DEFINE1(mac, , policy, unregister,
101 "struct mac_policy_conf *");
104 * Root sysctl node for all MAC and MAC policy controls.
106 SYSCTL_NODE(_security, OID_AUTO, mac, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
107 "TrustedBSD MAC policy controls");
110 * Declare that the kernel provides MAC support, version 3 (FreeBSD 7.x).
111 * This permits modules to refuse to be loaded if the necessary support isn't
112 * present, even if it's pre-boot.
114 MODULE_VERSION(kernel_mac_support, MAC_VERSION);
116 static unsigned int mac_version = MAC_VERSION;
117 SYSCTL_UINT(_security_mac, OID_AUTO, version, CTLFLAG_RD, &mac_version, 0,
121 * Flags for inlined checks.
124 bool __read_frequently mac_##f##_fp_flag
128 FPFLAG(vnode_check_lookup);
129 FPFLAG(vnode_check_open);
130 FPFLAG(vnode_check_stat);
131 FPFLAG(vnode_check_read);
132 FPFLAG(vnode_check_write);
133 FPFLAG(vnode_check_mmap);
138 * Labels consist of a indexed set of "slots", which are allocated policies
139 * as required. The MAC Framework maintains a bitmask of slots allocated so
140 * far to prevent reuse. Slots cannot be reused, as the MAC Framework
141 * guarantees that newly allocated slots in labels will be NULL unless
142 * otherwise initialized, and because we do not have a mechanism to garbage
143 * collect slots on policy unload. As labeled policies tend to be statically
144 * loaded during boot, and not frequently unloaded and reloaded, this is not
145 * generally an issue.
147 #if MAC_MAX_SLOTS > 32
148 #error "MAC_MAX_SLOTS too large"
151 static unsigned int mac_max_slots = MAC_MAX_SLOTS;
152 static unsigned int mac_slot_offsets_free = (1 << MAC_MAX_SLOTS) - 1;
153 SYSCTL_UINT(_security_mac, OID_AUTO, max_slots, CTLFLAG_RD, &mac_max_slots,
157 * Has the kernel started generating labeled objects yet? All read/write
158 * access to this variable is serialized during the boot process. Following
159 * the end of serialization, we don't update this flag; no locking.
161 static int mac_late = 0;
164 * Each policy declares a mask of object types requiring labels to be
165 * allocated for them. For convenience, we combine and cache the bitwise or
166 * of the per-policy object flags to track whether we will allocate a label
167 * for an object type at run-time.
169 uint64_t mac_labeled;
170 SYSCTL_UQUAD(_security_mac, OID_AUTO, labeled, CTLFLAG_RD, &mac_labeled, 0,
171 "Mask of object types being labeled");
173 MALLOC_DEFINE(M_MACTEMP, "mactemp", "MAC temporary label storage");
176 * MAC policy modules are placed in one of two lists: mac_static_policy_list,
177 * for policies that are loaded early and cannot be unloaded, and
178 * mac_policy_list, which holds policies either loaded later in the boot
179 * cycle or that may be unloaded. The static policy list does not require
180 * locks to iterate over, but the dynamic list requires synchronization.
181 * Support for dynamic policy loading can be compiled out using the
182 * MAC_STATIC kernel option.
184 * The dynamic policy list is protected by two locks: modifying the list
185 * requires both locks to be held exclusively. One of the locks,
186 * mac_policy_rm, is acquired over policy entry points that will never sleep;
187 * the other, mac_policy_rms, is acquired over policy entry points that may
188 * sleep. The former category will be used when kernel locks may be held
189 * over calls to the MAC Framework, during network processing in ithreads,
190 * etc. The latter will tend to involve potentially blocking memory
191 * allocations, extended attribute I/O, etc.
194 static struct rmlock mac_policy_rm; /* Non-sleeping entry points. */
195 static struct rmslock mac_policy_rms; /* Sleeping entry points. */
198 struct mac_policy_list_head mac_policy_list;
199 struct mac_policy_list_head mac_static_policy_list;
200 u_int mac_policy_count; /* Registered policy count. */
202 static void mac_policy_xlock(void);
203 static void mac_policy_xlock_assert(void);
204 static void mac_policy_xunlock(void);
207 mac_policy_slock_nosleep(struct rm_priotracker *tracker)
214 rm_rlock(&mac_policy_rm, tracker);
219 mac_policy_slock_sleep(void)
222 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
223 "mac_policy_slock_sleep");
229 rms_rlock(&mac_policy_rms);
234 mac_policy_sunlock_nosleep(struct rm_priotracker *tracker)
241 rm_runlock(&mac_policy_rm, tracker);
246 mac_policy_sunlock_sleep(void)
253 rms_runlock(&mac_policy_rms);
258 mac_policy_xlock(void)
261 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
262 "mac_policy_xlock()");
268 rms_wlock(&mac_policy_rms);
269 rm_wlock(&mac_policy_rm);
274 mac_policy_xunlock(void)
281 rm_wunlock(&mac_policy_rm);
282 rms_wunlock(&mac_policy_rms);
287 mac_policy_xlock_assert(void)
294 rm_assert(&mac_policy_rm, RA_WLOCKED);
299 * Initialize the MAC subsystem, including appropriate SMP locks.
305 LIST_INIT(&mac_static_policy_list);
306 LIST_INIT(&mac_policy_list);
307 mac_labelzone_init();
310 rm_init_flags(&mac_policy_rm, "mac_policy_rm", RM_NOWITNESS |
312 rms_init(&mac_policy_rms, "mac_policy_rms");
317 * For the purposes of modules that want to know if they were loaded "early",
318 * set the mac_late flag once we've processed modules either linked into the
319 * kernel, or loaded before the kernel startup.
329 * Given a policy, derive from its set of non-NULL label init methods what
330 * object types the policy is interested in.
333 mac_policy_getlabeled(struct mac_policy_conf *mpc)
337 #define MPC_FLAG(method, flag) \
338 if (mpc->mpc_ops->mpo_ ## method != NULL) \
342 MPC_FLAG(cred_init_label, MPC_OBJECT_CRED);
343 MPC_FLAG(proc_init_label, MPC_OBJECT_PROC);
344 MPC_FLAG(vnode_init_label, MPC_OBJECT_VNODE);
345 MPC_FLAG(inpcb_init_label, MPC_OBJECT_INPCB);
346 MPC_FLAG(socket_init_label, MPC_OBJECT_SOCKET);
347 MPC_FLAG(devfs_init_label, MPC_OBJECT_DEVFS);
348 MPC_FLAG(mbuf_init_label, MPC_OBJECT_MBUF);
349 MPC_FLAG(ipq_init_label, MPC_OBJECT_IPQ);
350 MPC_FLAG(ifnet_init_label, MPC_OBJECT_IFNET);
351 MPC_FLAG(bpfdesc_init_label, MPC_OBJECT_BPFDESC);
352 MPC_FLAG(pipe_init_label, MPC_OBJECT_PIPE);
353 MPC_FLAG(mount_init_label, MPC_OBJECT_MOUNT);
354 MPC_FLAG(posixsem_init_label, MPC_OBJECT_POSIXSEM);
355 MPC_FLAG(posixshm_init_label, MPC_OBJECT_POSIXSHM);
356 MPC_FLAG(sysvmsg_init_label, MPC_OBJECT_SYSVMSG);
357 MPC_FLAG(sysvmsq_init_label, MPC_OBJECT_SYSVMSQ);
358 MPC_FLAG(sysvsem_init_label, MPC_OBJECT_SYSVSEM);
359 MPC_FLAG(sysvshm_init_label, MPC_OBJECT_SYSVSHM);
360 MPC_FLAG(syncache_init_label, MPC_OBJECT_SYNCACHE);
361 MPC_FLAG(ip6q_init_label, MPC_OBJECT_IP6Q);
368 * When policies are loaded or unloaded, walk the list of registered policies
369 * and built mac_labeled, a bitmask representing the union of all objects
370 * requiring labels across all policies.
373 mac_policy_update(void)
375 struct mac_policy_conf *mpc;
377 mac_policy_xlock_assert();
380 mac_policy_count = 0;
381 LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {
382 mac_labeled |= mac_policy_getlabeled(mpc);
385 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {
386 mac_labeled |= mac_policy_getlabeled(mpc);
392 * There are frequently used code paths which check for rarely installed
393 * policies. Gross hack below enables doing it in a cheap manner.
396 #define FPO(f) (offsetof(struct mac_policy_ops, mpo_##f) / sizeof(uintptr_t))
398 struct mac_policy_fastpath_elem {
404 struct mac_policy_fastpath_elem mac_policy_fastpath_array[] = {
405 { .offset = FPO(priv_check), .flag = &mac_priv_check_fp_flag },
406 { .offset = FPO(priv_grant), .flag = &mac_priv_grant_fp_flag },
407 { .offset = FPO(vnode_check_lookup),
408 .flag = &mac_vnode_check_lookup_fp_flag },
409 { .offset = FPO(vnode_check_open),
410 .flag = &mac_vnode_check_open_fp_flag },
411 { .offset = FPO(vnode_check_stat),
412 .flag = &mac_vnode_check_stat_fp_flag },
413 { .offset = FPO(vnode_check_read),
414 .flag = &mac_vnode_check_read_fp_flag },
415 { .offset = FPO(vnode_check_write),
416 .flag = &mac_vnode_check_write_fp_flag },
417 { .offset = FPO(vnode_check_mmap),
418 .flag = &mac_vnode_check_mmap_fp_flag },
422 mac_policy_fastpath_enable(struct mac_policy_fastpath_elem *mpfe)
425 MPASS(mpfe->count >= 0);
427 if (mpfe->count == 1) {
428 MPASS(*mpfe->flag == false);
434 mac_policy_fastpath_disable(struct mac_policy_fastpath_elem *mpfe)
437 MPASS(mpfe->count >= 1);
439 if (mpfe->count == 0) {
440 MPASS(*mpfe->flag == true);
446 mac_policy_fastpath_register(struct mac_policy_conf *mpc)
448 struct mac_policy_fastpath_elem *mpfe;
452 mac_policy_xlock_assert();
454 ops = (uintptr_t **)mpc->mpc_ops;
455 for (i = 0; i < nitems(mac_policy_fastpath_array); i++) {
456 mpfe = &mac_policy_fastpath_array[i];
457 if (ops[mpfe->offset] != NULL)
458 mac_policy_fastpath_enable(mpfe);
463 mac_policy_fastpath_unregister(struct mac_policy_conf *mpc)
465 struct mac_policy_fastpath_elem *mpfe;
469 mac_policy_xlock_assert();
471 ops = (uintptr_t **)mpc->mpc_ops;
472 for (i = 0; i < nitems(mac_policy_fastpath_array); i++) {
473 mpfe = &mac_policy_fastpath_array[i];
474 if (ops[mpfe->offset] != NULL)
475 mac_policy_fastpath_disable(mpfe);
482 mac_policy_register(struct mac_policy_conf *mpc)
484 struct mac_policy_conf *tmpc;
485 int error, slot, static_entry;
490 * We don't technically need exclusive access while !mac_late, but
491 * hold it for assertion consistency.
496 * If the module can potentially be unloaded, or we're loading late,
497 * we have to stick it in the non-static list and pay an extra
498 * performance overhead. Otherwise, we can pay a light locking cost
499 * and stick it in the static list.
501 static_entry = (!mac_late &&
502 !(mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK));
505 LIST_FOREACH(tmpc, &mac_static_policy_list, mpc_list) {
506 if (strcmp(tmpc->mpc_name, mpc->mpc_name) == 0) {
512 LIST_FOREACH(tmpc, &mac_policy_list, mpc_list) {
513 if (strcmp(tmpc->mpc_name, mpc->mpc_name) == 0) {
519 if (mpc->mpc_field_off != NULL) {
520 slot = ffs(mac_slot_offsets_free);
526 mac_slot_offsets_free &= ~(1 << slot);
527 *mpc->mpc_field_off = slot;
529 mpc->mpc_runtime_flags |= MPC_RUNTIME_FLAG_REGISTERED;
532 * If we're loading a MAC module after the framework has initialized,
533 * it has to go into the dynamic list. If we're loading it before
534 * we've finished initializing, it can go into the static list with
535 * weaker locker requirements.
538 LIST_INSERT_HEAD(&mac_static_policy_list, mpc, mpc_list);
540 LIST_INSERT_HEAD(&mac_policy_list, mpc, mpc_list);
543 * Per-policy initialization. Currently, this takes place under the
544 * exclusive lock, so policies must not sleep in their init method.
545 * In the future, we may want to separate "init" from "start", with
546 * "init" occurring without the lock held. Likewise, on tear-down,
547 * breaking out "stop" from "destroy".
549 if (mpc->mpc_ops->mpo_init != NULL)
550 (*(mpc->mpc_ops->mpo_init))(mpc);
552 mac_policy_fastpath_register(mpc);
556 SDT_PROBE1(mac, , policy, register, mpc);
557 printf("Security policy loaded: %s (%s)\n", mpc->mpc_fullname,
561 mac_policy_xunlock();
566 mac_policy_unregister(struct mac_policy_conf *mpc)
570 * If we fail the load, we may get a request to unload. Check to see
571 * if we did the run-time registration, and if not, silently succeed.
574 if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED) == 0) {
575 mac_policy_xunlock();
580 * Don't allow unloading modules with private data.
582 if (mpc->mpc_field_off != NULL) {
583 mac_policy_xunlock();
588 * Only allow the unload to proceed if the module is unloadable by
589 * its own definition.
591 if ((mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK) == 0) {
592 mac_policy_xunlock();
596 mac_policy_fastpath_unregister(mpc);
598 if (mpc->mpc_ops->mpo_destroy != NULL)
599 (*(mpc->mpc_ops->mpo_destroy))(mpc);
601 LIST_REMOVE(mpc, mpc_list);
602 mpc->mpc_runtime_flags &= ~MPC_RUNTIME_FLAG_REGISTERED;
604 mac_policy_xunlock();
606 SDT_PROBE1(mac, , policy, unregister, mpc);
607 printf("Security policy unload: %s (%s)\n", mpc->mpc_fullname,
614 * Allow MAC policy modules to register during boot, etc.
617 mac_policy_modevent(module_t mod, int type, void *data)
619 struct mac_policy_conf *mpc;
623 mpc = (struct mac_policy_conf *) data;
627 printf("mac_policy_modevent: MAC_STATIC and late\n");
632 SDT_PROBE2(mac, , policy, modevent, type, mpc);
635 if (mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_NOTLATE &&
637 printf("mac_policy_modevent: can't load %s policy "
638 "after booting\n", mpc->mpc_name);
642 error = mac_policy_register(mpc);
645 /* Don't unregister the module if it was never registered. */
646 if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED)
648 error = mac_policy_unregister(mpc);
661 * Define an error value precedence, and given two arguments, selects the
662 * value with the higher precedence.
665 mac_error_select(int error1, int error2)
668 /* Certain decision-making errors take top priority. */
669 if (error1 == EDEADLK || error2 == EDEADLK)
672 /* Invalid arguments should be reported where possible. */
673 if (error1 == EINVAL || error2 == EINVAL)
676 /* Precedence goes to "visibility", with both process and file. */
677 if (error1 == ESRCH || error2 == ESRCH)
680 if (error1 == ENOENT || error2 == ENOENT)
683 /* Precedence goes to DAC/MAC protections. */
684 if (error1 == EACCES || error2 == EACCES)
687 /* Precedence goes to privilege. */
688 if (error1 == EPERM || error2 == EPERM)
691 /* Precedence goes to error over success; otherwise, arbitrary. */
698 mac_check_structmac_consistent(struct mac *mac)
701 /* Require that labels have a non-zero length. */
702 if (mac->m_buflen > MAC_MAX_LABEL_BUF_LEN ||
703 mac->m_buflen <= sizeof(""))
709 SYSINIT(mac, SI_SUB_MAC, SI_ORDER_FIRST, mac_init, NULL);
710 SYSINIT(mac_late, SI_SUB_MAC_LATE, SI_ORDER_FIRST, mac_late_init, NULL);