xref: /freebsd/sys/security/mac/mac_framework.c (revision 4020272933cbe552f932d9642419f8becee04bd5)
17bc82500SRobert Watson /*-
291ec0006SRobert Watson  * Copyright (c) 1999-2002, 2006, 2009 Robert N. M. Watson
37bc82500SRobert Watson  * Copyright (c) 2001 Ilmar S. Habibulin
4f0c2044bSRobert Watson  * Copyright (c) 2001-2005 Networks Associates Technology, Inc.
5aed55708SRobert Watson  * Copyright (c) 2005-2006 SPARTA, Inc.
69162f64bSRobert Watson  * Copyright (c) 2008-2009 Apple Inc.
77bc82500SRobert Watson  * All rights reserved.
87bc82500SRobert Watson  *
97bc82500SRobert Watson  * This software was developed by Robert Watson and Ilmar Habibulin for the
107bc82500SRobert Watson  * TrustedBSD Project.
117bc82500SRobert Watson  *
126201265bSRobert Watson  * This software was developed for the FreeBSD Project in part by Network
136201265bSRobert Watson  * Associates Laboratories, the Security Research Division of Network
146201265bSRobert Watson  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
156201265bSRobert Watson  * as part of the DARPA CHATS research program.
167bc82500SRobert Watson  *
1749bb6870SRobert Watson  * This software was enhanced by SPARTA ISSO under SPAWAR contract
1849bb6870SRobert Watson  * N66001-04-C-6019 ("SEFOS").
1949bb6870SRobert Watson  *
206f6174a7SRobert Watson  * This software was developed at the University of Cambridge Computer
216f6174a7SRobert Watson  * Laboratory with support from a grant from Google, Inc.
226f6174a7SRobert Watson  *
237bc82500SRobert Watson  * Redistribution and use in source and binary forms, with or without
247bc82500SRobert Watson  * modification, are permitted provided that the following conditions
257bc82500SRobert Watson  * are met:
267bc82500SRobert Watson  * 1. Redistributions of source code must retain the above copyright
277bc82500SRobert Watson  *    notice, this list of conditions and the following disclaimer.
287bc82500SRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
297bc82500SRobert Watson  *    notice, this list of conditions and the following disclaimer in the
307bc82500SRobert Watson  *    documentation and/or other materials provided with the distribution.
317bc82500SRobert Watson  *
327bc82500SRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
337bc82500SRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
347bc82500SRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
357bc82500SRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
367bc82500SRobert Watson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
377bc82500SRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
387bc82500SRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
397bc82500SRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
407bc82500SRobert Watson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
417bc82500SRobert Watson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
427bc82500SRobert Watson  * SUCH DAMAGE.
437bc82500SRobert Watson  */
44677b542eSDavid E. O'Brien 
45c8e7bf92SRobert Watson /*-
46471e5756SRobert Watson  * Framework for extensible kernel access control.  This file contains core
47471e5756SRobert Watson  * kernel infrastructure for the TrustedBSD MAC Framework, including policy
48471e5756SRobert Watson  * registration, versioning, locking, error composition operator, and system
49471e5756SRobert Watson  * calls.
50471e5756SRobert Watson  *
51471e5756SRobert Watson  * The MAC Framework implements three programming interfaces:
52471e5756SRobert Watson  *
53471e5756SRobert Watson  * - The kernel MAC interface, defined in mac_framework.h, and invoked
54471e5756SRobert Watson  *   throughout the kernel to request security decisions, notify of security
55471e5756SRobert Watson  *   related events, etc.
56471e5756SRobert Watson  *
57471e5756SRobert Watson  * - The MAC policy module interface, defined in mac_policy.h, which is
58471e5756SRobert Watson  *   implemented by MAC policy modules and invoked by the MAC Framework to
59471e5756SRobert Watson  *   forward kernel security requests and notifications to policy modules.
60471e5756SRobert Watson  *
61471e5756SRobert Watson  * - The user MAC API, defined in mac.h, which allows user programs to query
62471e5756SRobert Watson  *   and set label state on objects.
63471e5756SRobert Watson  *
64471e5756SRobert Watson  * The majority of the MAC Framework implementation may be found in
65471e5756SRobert Watson  * src/sys/security/mac.  Sample policy modules may be found in
6649869305STom Rhodes  * src/sys/security/mac_*.
677bc82500SRobert Watson  */
687bc82500SRobert Watson 
6991ec0006SRobert Watson #include "opt_kdtrace.h"
7039b73a30SRobert Watson #include "opt_mac.h"
7139b73a30SRobert Watson 
72677b542eSDavid E. O'Brien #include <sys/cdefs.h>
73677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
74677b542eSDavid E. O'Brien 
757bc82500SRobert Watson #include <sys/param.h>
76a96acd1aSRobert Watson #include <sys/condvar.h>
7795fab37eSRobert Watson #include <sys/kernel.h>
7895fab37eSRobert Watson #include <sys/lock.h>
7995fab37eSRobert Watson #include <sys/mac.h>
807ba28492SRobert Watson #include <sys/module.h>
8140202729SRobert Watson #include <sys/rwlock.h>
8291ec0006SRobert Watson #include <sys/sdt.h>
8340202729SRobert Watson #include <sys/sx.h>
8495fab37eSRobert Watson #include <sys/systm.h>
8595fab37eSRobert Watson #include <sys/sysctl.h>
8695fab37eSRobert Watson 
87aed55708SRobert Watson #include <security/mac/mac_framework.h>
886fa0475dSRobert Watson #include <security/mac/mac_internal.h>
890efd6615SRobert Watson #include <security/mac/mac_policy.h>
906fa0475dSRobert Watson 
917ba28492SRobert Watson /*
922087a58cSRobert Watson  * DTrace SDT providers for MAC.
9391ec0006SRobert Watson  */
9491ec0006SRobert Watson SDT_PROVIDER_DEFINE(mac);
952087a58cSRobert Watson SDT_PROVIDER_DEFINE(mac_framework);
962087a58cSRobert Watson 
9773e416e3SRobert Watson SDT_PROBE_DEFINE2(mac, kernel, policy, modevent, "int",
9891ec0006SRobert Watson     "struct mac_policy_conf *mpc");
9973e416e3SRobert Watson SDT_PROBE_DEFINE1(mac, kernel, policy, register, "struct mac_policy_conf *");
10073e416e3SRobert Watson SDT_PROBE_DEFINE1(mac, kernel, policy, unregister, "struct mac_policy_conf *");
10191ec0006SRobert Watson 
10291ec0006SRobert Watson /*
103471e5756SRobert Watson  * Root sysctl node for all MAC and MAC policy controls.
1047ba28492SRobert Watson  */
10595fab37eSRobert Watson SYSCTL_NODE(_security, OID_AUTO, mac, CTLFLAG_RW, 0,
10695fab37eSRobert Watson     "TrustedBSD MAC policy controls");
107b2f0927aSRobert Watson 
10817041e67SRobert Watson /*
109471e5756SRobert Watson  * Declare that the kernel provides MAC support, version 3 (FreeBSD 7.x).
110471e5756SRobert Watson  * This permits modules to refuse to be loaded if the necessary support isn't
111471e5756SRobert Watson  * present, even if it's pre-boot.
112471e5756SRobert Watson  */
113471e5756SRobert Watson MODULE_VERSION(kernel_mac_support, MAC_VERSION);
114be23ba9aSRobert Watson 
115be23ba9aSRobert Watson static unsigned int	mac_version = MAC_VERSION;
116471e5756SRobert Watson SYSCTL_UINT(_security_mac, OID_AUTO, version, CTLFLAG_RD, &mac_version, 0,
117471e5756SRobert Watson     "");
118471e5756SRobert Watson 
119471e5756SRobert Watson /*
12017041e67SRobert Watson  * Labels consist of a indexed set of "slots", which are allocated policies
12117041e67SRobert Watson  * as required.  The MAC Framework maintains a bitmask of slots allocated so
12217041e67SRobert Watson  * far to prevent reuse.  Slots cannot be reused, as the MAC Framework
12317041e67SRobert Watson  * guarantees that newly allocated slots in labels will be NULL unless
12417041e67SRobert Watson  * otherwise initialized, and because we do not have a mechanism to garbage
12517041e67SRobert Watson  * collect slots on policy unload.  As labeled policies tend to be statically
12617041e67SRobert Watson  * loaded during boot, and not frequently unloaded and reloaded, this is not
12717041e67SRobert Watson  * generally an issue.
12817041e67SRobert Watson  */
129b2aef571SRobert Watson #if MAC_MAX_SLOTS > 32
130b2aef571SRobert Watson #error "MAC_MAX_SLOTS too large"
13195fab37eSRobert Watson #endif
132a13c67daSRobert Watson 
133b2aef571SRobert Watson static unsigned int mac_max_slots = MAC_MAX_SLOTS;
134b2aef571SRobert Watson static unsigned int mac_slot_offsets_free = (1 << MAC_MAX_SLOTS) - 1;
135471e5756SRobert Watson SYSCTL_UINT(_security_mac, OID_AUTO, max_slots, CTLFLAG_RD, &mac_max_slots,
136471e5756SRobert Watson     0, "");
13795fab37eSRobert Watson 
138a67fe518SRobert Watson /*
139a67fe518SRobert Watson  * Has the kernel started generating labeled objects yet?  All read/write
140a67fe518SRobert Watson  * access to this variable is serialized during the boot process.  Following
141a67fe518SRobert Watson  * the end of serialization, we don't update this flag; no locking.
142a67fe518SRobert Watson  */
143be23ba9aSRobert Watson static int	mac_late = 0;
144763bbd2fSRobert Watson 
145225bff6fSRobert Watson /*
1466356dba0SRobert Watson  * Each policy declares a mask of object types requiring labels to be
1476356dba0SRobert Watson  * allocated for them.  For convenience, we combine and cache the bitwise or
1486356dba0SRobert Watson  * of the per-policy object flags to track whether we will allocate a label
1496356dba0SRobert Watson  * for an object type at run-time.
150225bff6fSRobert Watson  */
1516356dba0SRobert Watson uint64_t	mac_labeled;
1526356dba0SRobert Watson SYSCTL_QUAD(_security_mac, OID_AUTO, labeled, CTLFLAG_RD, &mac_labeled, 0,
1536356dba0SRobert Watson     "Mask of object types being labeled");
154225bff6fSRobert Watson 
155f7b951a8SRobert Watson MALLOC_DEFINE(M_MACTEMP, "mactemp", "MAC temporary label storage");
15695fab37eSRobert Watson 
15795fab37eSRobert Watson /*
15840202729SRobert Watson  * MAC policy modules are placed in one of two lists: mac_static_policy_list,
15940202729SRobert Watson  * for policies that are loaded early and cannot be unloaded, and
16040202729SRobert Watson  * mac_policy_list, which holds policies either loaded later in the boot
16140202729SRobert Watson  * cycle or that may be unloaded.  The static policy list does not require
16240202729SRobert Watson  * locks to iterate over, but the dynamic list requires synchronization.
16340202729SRobert Watson  * Support for dynamic policy loading can be compiled out using the
16440202729SRobert Watson  * MAC_STATIC kernel option.
16541a17fe3SRobert Watson  *
16640202729SRobert Watson  * The dynamic policy list is protected by two locks: modifying the list
16740202729SRobert Watson  * requires both locks to be held exclusively.  One of the locks,
16840202729SRobert Watson  * mac_policy_rw, is acquired over policy entry points that will never sleep;
16940202729SRobert Watson  * the other, mac_policy_sx, is acquire over policy entry points that may
17040202729SRobert Watson  * sleep.  The former category will be used when kernel locks may be held
17140202729SRobert Watson  * over calls to the MAC Framework, during network processing in ithreads,
17240202729SRobert Watson  * etc.  The latter will tend to involve potentially blocking memory
17340202729SRobert Watson  * allocations, extended attribute I/O, etc.
17495fab37eSRobert Watson  */
1750a05006dSRobert Watson #ifndef MAC_STATIC
17640202729SRobert Watson static struct rwlock mac_policy_rw;	/* Non-sleeping entry points. */
17740202729SRobert Watson static struct sx mac_policy_sx;		/* Sleeping entry points. */
1780a05006dSRobert Watson #endif
17940202729SRobert Watson 
180089c1bdaSRobert Watson struct mac_policy_list_head mac_policy_list;
181089c1bdaSRobert Watson struct mac_policy_list_head mac_static_policy_list;
182a96acd1aSRobert Watson 
18340202729SRobert Watson static void	mac_policy_xlock(void);
18440202729SRobert Watson static void	mac_policy_xlock_assert(void);
18540202729SRobert Watson static void	mac_policy_xunlock(void);
18640202729SRobert Watson 
187089c1bdaSRobert Watson void
18840202729SRobert Watson mac_policy_slock_nosleep(void)
18941a17fe3SRobert Watson {
190c8e7bf92SRobert Watson 
1910a05006dSRobert Watson #ifndef MAC_STATIC
1921e4cadcbSRobert Watson 	if (!mac_late)
1931e4cadcbSRobert Watson 		return;
1941e4cadcbSRobert Watson 
19540202729SRobert Watson 	rw_rlock(&mac_policy_rw);
19640202729SRobert Watson #endif
19740202729SRobert Watson }
19840202729SRobert Watson 
19940202729SRobert Watson void
20040202729SRobert Watson mac_policy_slock_sleep(void)
20140202729SRobert Watson {
20240202729SRobert Watson 
20341a17fe3SRobert Watson 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
20440202729SRobert Watson  	    "mac_policy_slock_sleep");
20540202729SRobert Watson 
20640202729SRobert Watson #ifndef MAC_STATIC
20740202729SRobert Watson 	if (!mac_late)
20840202729SRobert Watson 		return;
20940202729SRobert Watson 
21040202729SRobert Watson 	sx_slock(&mac_policy_sx);
2110a05006dSRobert Watson #endif
21241a17fe3SRobert Watson }
21395fab37eSRobert Watson 
214089c1bdaSRobert Watson void
21540202729SRobert Watson mac_policy_sunlock_nosleep(void)
21641a17fe3SRobert Watson {
217c8e7bf92SRobert Watson 
2180a05006dSRobert Watson #ifndef MAC_STATIC
2191e4cadcbSRobert Watson 	if (!mac_late)
2201e4cadcbSRobert Watson 		return;
2211e4cadcbSRobert Watson 
22240202729SRobert Watson 	rw_runlock(&mac_policy_rw);
2230a05006dSRobert Watson #endif
22441a17fe3SRobert Watson }
225225bff6fSRobert Watson 
226089c1bdaSRobert Watson void
22740202729SRobert Watson mac_policy_sunlock_sleep(void)
22841a17fe3SRobert Watson {
229c8e7bf92SRobert Watson 
2300a05006dSRobert Watson #ifndef MAC_STATIC
2311e4cadcbSRobert Watson 	if (!mac_late)
2321e4cadcbSRobert Watson 		return;
2331e4cadcbSRobert Watson 
23440202729SRobert Watson 	sx_sunlock(&mac_policy_sx);
2350a05006dSRobert Watson #endif
23641a17fe3SRobert Watson }
23741a17fe3SRobert Watson 
23840202729SRobert Watson static void
23940202729SRobert Watson mac_policy_xlock(void)
24041a17fe3SRobert Watson {
24140202729SRobert Watson 
24240202729SRobert Watson 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
24340202729SRobert Watson  	    "mac_policy_xlock()");
24440202729SRobert Watson 
2450a05006dSRobert Watson #ifndef MAC_STATIC
2461e4cadcbSRobert Watson 	if (!mac_late)
2471e4cadcbSRobert Watson 		return;
2481e4cadcbSRobert Watson 
24940202729SRobert Watson 	sx_xlock(&mac_policy_sx);
25040202729SRobert Watson 	rw_wlock(&mac_policy_rw);
25140202729SRobert Watson #endif
25240202729SRobert Watson }
253989d4098SRobert Watson 
25440202729SRobert Watson static void
25540202729SRobert Watson mac_policy_xunlock(void)
25640202729SRobert Watson {
25740202729SRobert Watson 
25840202729SRobert Watson #ifndef MAC_STATIC
25940202729SRobert Watson 	if (!mac_late)
26040202729SRobert Watson 		return;
26140202729SRobert Watson 
26240202729SRobert Watson 	rw_wunlock(&mac_policy_rw);
26340202729SRobert Watson 	sx_xunlock(&mac_policy_sx);
26440202729SRobert Watson #endif
26540202729SRobert Watson }
26640202729SRobert Watson 
26740202729SRobert Watson static void
26840202729SRobert Watson mac_policy_xlock_assert(void)
26940202729SRobert Watson {
27040202729SRobert Watson 
27140202729SRobert Watson #ifndef MAC_STATIC
27240202729SRobert Watson 	if (!mac_late)
27340202729SRobert Watson 		return;
27440202729SRobert Watson 
27540202729SRobert Watson 	rw_assert(&mac_policy_rw, RA_WLOCKED);
27640202729SRobert Watson 	sx_assert(&mac_policy_sx, SA_XLOCKED);
2770a05006dSRobert Watson #endif
27841a17fe3SRobert Watson }
27995fab37eSRobert Watson 
28095fab37eSRobert Watson /*
28195fab37eSRobert Watson  * Initialize the MAC subsystem, including appropriate SMP locks.
28295fab37eSRobert Watson  */
28395fab37eSRobert Watson static void
28495fab37eSRobert Watson mac_init(void)
28595fab37eSRobert Watson {
28695fab37eSRobert Watson 
28741a17fe3SRobert Watson 	LIST_INIT(&mac_static_policy_list);
28895fab37eSRobert Watson 	LIST_INIT(&mac_policy_list);
289eca8a663SRobert Watson 	mac_labelzone_init();
29041a17fe3SRobert Watson 
2910a05006dSRobert Watson #ifndef MAC_STATIC
29240202729SRobert Watson 	rw_init(&mac_policy_rw, "mac_policy_rw");
29340202729SRobert Watson 	sx_init(&mac_policy_sx, "mac_policy_sx");
2940a05006dSRobert Watson #endif
29595fab37eSRobert Watson }
29695fab37eSRobert Watson 
29795fab37eSRobert Watson /*
29817041e67SRobert Watson  * For the purposes of modules that want to know if they were loaded "early",
29917041e67SRobert Watson  * set the mac_late flag once we've processed modules either linked into the
30017041e67SRobert Watson  * kernel, or loaded before the kernel startup.
30195fab37eSRobert Watson  */
30295fab37eSRobert Watson static void
30395fab37eSRobert Watson mac_late_init(void)
30495fab37eSRobert Watson {
30595fab37eSRobert Watson 
30695fab37eSRobert Watson 	mac_late = 1;
30795fab37eSRobert Watson }
30895fab37eSRobert Watson 
30995fab37eSRobert Watson /*
3109162f64bSRobert Watson  * Given a policy, derive from its set of non-NULL label init methods what
3119162f64bSRobert Watson  * object types the policy is interested in.
3129162f64bSRobert Watson  */
3139162f64bSRobert Watson static uint64_t
3149162f64bSRobert Watson mac_policy_getlabeled(struct mac_policy_conf *mpc)
3159162f64bSRobert Watson {
3169162f64bSRobert Watson 	uint64_t labeled;
3179162f64bSRobert Watson 
3189162f64bSRobert Watson #define	MPC_FLAG(method, flag)					\
3199162f64bSRobert Watson 	if (mpc->mpc_ops->mpo_ ## method != NULL)			\
3209162f64bSRobert Watson 		labeled |= (flag);					\
3219162f64bSRobert Watson 
3229162f64bSRobert Watson 	labeled = 0;
3239162f64bSRobert Watson 	MPC_FLAG(cred_init_label, MPC_OBJECT_CRED);
3249162f64bSRobert Watson 	MPC_FLAG(proc_init_label, MPC_OBJECT_PROC);
3259162f64bSRobert Watson 	MPC_FLAG(vnode_init_label, MPC_OBJECT_VNODE);
3269162f64bSRobert Watson 	MPC_FLAG(inpcb_init_label, MPC_OBJECT_INPCB);
3279162f64bSRobert Watson 	MPC_FLAG(socket_init_label, MPC_OBJECT_SOCKET);
3289162f64bSRobert Watson 	MPC_FLAG(devfs_init_label, MPC_OBJECT_DEVFS);
3299162f64bSRobert Watson 	MPC_FLAG(mbuf_init_label, MPC_OBJECT_MBUF);
3309162f64bSRobert Watson 	MPC_FLAG(ipq_init_label, MPC_OBJECT_IPQ);
3319162f64bSRobert Watson 	MPC_FLAG(ifnet_init_label, MPC_OBJECT_IFNET);
3329162f64bSRobert Watson 	MPC_FLAG(bpfdesc_init_label, MPC_OBJECT_BPFDESC);
3339162f64bSRobert Watson 	MPC_FLAG(pipe_init_label, MPC_OBJECT_PIPE);
3349162f64bSRobert Watson 	MPC_FLAG(mount_init_label, MPC_OBJECT_MOUNT);
3359162f64bSRobert Watson 	MPC_FLAG(posixsem_init_label, MPC_OBJECT_POSIXSEM);
3369162f64bSRobert Watson 	MPC_FLAG(posixshm_init_label, MPC_OBJECT_POSIXSHM);
3379162f64bSRobert Watson 	MPC_FLAG(sysvmsg_init_label, MPC_OBJECT_SYSVMSG);
3389162f64bSRobert Watson 	MPC_FLAG(sysvmsq_init_label, MPC_OBJECT_SYSVMSQ);
3399162f64bSRobert Watson 	MPC_FLAG(sysvsem_init_label, MPC_OBJECT_SYSVSEM);
3409162f64bSRobert Watson 	MPC_FLAG(sysvshm_init_label, MPC_OBJECT_SYSVSHM);
3419162f64bSRobert Watson 	MPC_FLAG(syncache_init_label, MPC_OBJECT_SYNCACHE);
3429162f64bSRobert Watson 	MPC_FLAG(ip6q_init_label, MPC_OBJECT_IP6Q);
3439162f64bSRobert Watson 
3449162f64bSRobert Watson #undef MPC_FLAG
3459162f64bSRobert Watson 	return (labeled);
3469162f64bSRobert Watson }
3479162f64bSRobert Watson 
3489162f64bSRobert Watson /*
3499162f64bSRobert Watson  * When policies are loaded or unloaded, walk the list of registered policies
3509162f64bSRobert Watson  * and built mac_labeled, a bitmask representing the union of all objects
3519162f64bSRobert Watson  * requiring labels across all policies.
352225bff6fSRobert Watson  */
353225bff6fSRobert Watson static void
354225bff6fSRobert Watson mac_policy_updateflags(void)
355225bff6fSRobert Watson {
3566356dba0SRobert Watson 	struct mac_policy_conf *mpc;
357225bff6fSRobert Watson 
35840202729SRobert Watson 	mac_policy_xlock_assert();
359225bff6fSRobert Watson 
3606356dba0SRobert Watson 	mac_labeled = 0;
3616356dba0SRobert Watson 	LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list)
3629162f64bSRobert Watson 		mac_labeled |= mac_policy_getlabeled(mpc);
3636356dba0SRobert Watson 	LIST_FOREACH(mpc, &mac_policy_list, mpc_list)
3649162f64bSRobert Watson 		mac_labeled |= mac_policy_getlabeled(mpc);
365225bff6fSRobert Watson }
366225bff6fSRobert Watson 
36795fab37eSRobert Watson static int
36895fab37eSRobert Watson mac_policy_register(struct mac_policy_conf *mpc)
36995fab37eSRobert Watson {
37095fab37eSRobert Watson 	struct mac_policy_conf *tmpc;
37141a17fe3SRobert Watson 	int error, slot, static_entry;
37295fab37eSRobert Watson 
37341a17fe3SRobert Watson 	error = 0;
37441a17fe3SRobert Watson 
37541a17fe3SRobert Watson 	/*
37617041e67SRobert Watson 	 * We don't technically need exclusive access while !mac_late, but
37717041e67SRobert Watson 	 * hold it for assertion consistency.
37841a17fe3SRobert Watson 	 */
37940202729SRobert Watson 	mac_policy_xlock();
38041a17fe3SRobert Watson 
38141a17fe3SRobert Watson 	/*
38217041e67SRobert Watson 	 * If the module can potentially be unloaded, or we're loading late,
38317041e67SRobert Watson 	 * we have to stick it in the non-static list and pay an extra
38417041e67SRobert Watson 	 * performance overhead.  Otherwise, we can pay a light locking cost
38517041e67SRobert Watson 	 * and stick it in the static list.
38641a17fe3SRobert Watson 	 */
38741a17fe3SRobert Watson 	static_entry = (!mac_late &&
38841a17fe3SRobert Watson 	    !(mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK));
38941a17fe3SRobert Watson 
39041a17fe3SRobert Watson 	if (static_entry) {
39141a17fe3SRobert Watson 		LIST_FOREACH(tmpc, &mac_static_policy_list, mpc_list) {
39241a17fe3SRobert Watson 			if (strcmp(tmpc->mpc_name, mpc->mpc_name) == 0) {
39341a17fe3SRobert Watson 				error = EEXIST;
39441a17fe3SRobert Watson 				goto out;
39541a17fe3SRobert Watson 			}
39641a17fe3SRobert Watson 		}
39741a17fe3SRobert Watson 	} else {
39895fab37eSRobert Watson 		LIST_FOREACH(tmpc, &mac_policy_list, mpc_list) {
39995fab37eSRobert Watson 			if (strcmp(tmpc->mpc_name, mpc->mpc_name) == 0) {
40041a17fe3SRobert Watson 				error = EEXIST;
40141a17fe3SRobert Watson 				goto out;
40241a17fe3SRobert Watson 			}
40395fab37eSRobert Watson 		}
40495fab37eSRobert Watson 	}
40595fab37eSRobert Watson 	if (mpc->mpc_field_off != NULL) {
406b2aef571SRobert Watson 		slot = ffs(mac_slot_offsets_free);
40795fab37eSRobert Watson 		if (slot == 0) {
40841a17fe3SRobert Watson 			error = ENOMEM;
40941a17fe3SRobert Watson 			goto out;
41095fab37eSRobert Watson 		}
41195fab37eSRobert Watson 		slot--;
412b2aef571SRobert Watson 		mac_slot_offsets_free &= ~(1 << slot);
41395fab37eSRobert Watson 		*mpc->mpc_field_off = slot;
41495fab37eSRobert Watson 	}
41595fab37eSRobert Watson 	mpc->mpc_runtime_flags |= MPC_RUNTIME_FLAG_REGISTERED;
41641a17fe3SRobert Watson 
41741a17fe3SRobert Watson 	/*
41817041e67SRobert Watson 	 * If we're loading a MAC module after the framework has initialized,
41917041e67SRobert Watson 	 * it has to go into the dynamic list.  If we're loading it before
42017041e67SRobert Watson 	 * we've finished initializing, it can go into the static list with
42117041e67SRobert Watson 	 * weaker locker requirements.
42241a17fe3SRobert Watson 	 */
42341a17fe3SRobert Watson 	if (static_entry)
42441a17fe3SRobert Watson 		LIST_INSERT_HEAD(&mac_static_policy_list, mpc, mpc_list);
42541a17fe3SRobert Watson 	else
42695fab37eSRobert Watson 		LIST_INSERT_HEAD(&mac_policy_list, mpc, mpc_list);
42795fab37eSRobert Watson 
42817041e67SRobert Watson 	/*
42917041e67SRobert Watson 	 * Per-policy initialization.  Currently, this takes place under the
43017041e67SRobert Watson 	 * exclusive lock, so policies must not sleep in their init method.
43117041e67SRobert Watson 	 * In the future, we may want to separate "init" from "start", with
43217041e67SRobert Watson 	 * "init" occuring without the lock held.  Likewise, on tear-down,
43317041e67SRobert Watson 	 * breaking out "stop" from "destroy".
43417041e67SRobert Watson 	 */
43595fab37eSRobert Watson 	if (mpc->mpc_ops->mpo_init != NULL)
43695fab37eSRobert Watson 		(*(mpc->mpc_ops->mpo_init))(mpc);
437225bff6fSRobert Watson 	mac_policy_updateflags();
43895fab37eSRobert Watson 
43991ec0006SRobert Watson 	SDT_PROBE(mac, kernel, policy, register, mpc, 0, 0, 0, 0);
44095fab37eSRobert Watson 	printf("Security policy loaded: %s (%s)\n", mpc->mpc_fullname,
44195fab37eSRobert Watson 	    mpc->mpc_name);
44295fab37eSRobert Watson 
44341a17fe3SRobert Watson out:
44440202729SRobert Watson 	mac_policy_xunlock();
44541a17fe3SRobert Watson 	return (error);
44695fab37eSRobert Watson }
44795fab37eSRobert Watson 
44895fab37eSRobert Watson static int
44995fab37eSRobert Watson mac_policy_unregister(struct mac_policy_conf *mpc)
45095fab37eSRobert Watson {
45195fab37eSRobert Watson 
452ea599aa0SRobert Watson 	/*
45317041e67SRobert Watson 	 * If we fail the load, we may get a request to unload.  Check to see
45417041e67SRobert Watson 	 * if we did the run-time registration, and if not, silently succeed.
455ea599aa0SRobert Watson 	 */
45640202729SRobert Watson 	mac_policy_xlock();
457ea599aa0SRobert Watson 	if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED) == 0) {
45840202729SRobert Watson 		mac_policy_xunlock();
459ea599aa0SRobert Watson 		return (0);
460ea599aa0SRobert Watson 	}
46195fab37eSRobert Watson #if 0
46295fab37eSRobert Watson 	/*
46395fab37eSRobert Watson 	 * Don't allow unloading modules with private data.
46495fab37eSRobert Watson 	 */
465ea599aa0SRobert Watson 	if (mpc->mpc_field_off != NULL) {
46640202729SRobert Watson 		mac_policy_xunlock();
46795fab37eSRobert Watson 		return (EBUSY);
468ea599aa0SRobert Watson 	}
46995fab37eSRobert Watson #endif
470ea599aa0SRobert Watson 	/*
47117041e67SRobert Watson 	 * Only allow the unload to proceed if the module is unloadable by
47217041e67SRobert Watson 	 * its own definition.
473ea599aa0SRobert Watson 	 */
474ea599aa0SRobert Watson 	if ((mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK) == 0) {
47540202729SRobert Watson 		mac_policy_xunlock();
47695fab37eSRobert Watson 		return (EBUSY);
477ea599aa0SRobert Watson 	}
47895fab37eSRobert Watson 	if (mpc->mpc_ops->mpo_destroy != NULL)
47995fab37eSRobert Watson 		(*(mpc->mpc_ops->mpo_destroy))(mpc);
48095fab37eSRobert Watson 
48195fab37eSRobert Watson 	LIST_REMOVE(mpc, mpc_list);
4829aeffb2bSRobert Watson 	mpc->mpc_runtime_flags &= ~MPC_RUNTIME_FLAG_REGISTERED;
483225bff6fSRobert Watson 	mac_policy_updateflags();
48440202729SRobert Watson 	mac_policy_xunlock();
485a96acd1aSRobert Watson 
48691ec0006SRobert Watson 	SDT_PROBE(mac, kernel, policy, unregister, mpc, 0, 0, 0, 0);
48795fab37eSRobert Watson 	printf("Security policy unload: %s (%s)\n", mpc->mpc_fullname,
48895fab37eSRobert Watson 	    mpc->mpc_name);
48995fab37eSRobert Watson 
49095fab37eSRobert Watson 	return (0);
49195fab37eSRobert Watson }
49295fab37eSRobert Watson 
49395fab37eSRobert Watson /*
494c441d123SRobert Watson  * Allow MAC policy modules to register during boot, etc.
495c441d123SRobert Watson  */
496c441d123SRobert Watson int
497c441d123SRobert Watson mac_policy_modevent(module_t mod, int type, void *data)
498c441d123SRobert Watson {
499c441d123SRobert Watson 	struct mac_policy_conf *mpc;
500c441d123SRobert Watson 	int error;
501c441d123SRobert Watson 
502c441d123SRobert Watson 	error = 0;
503c441d123SRobert Watson 	mpc = (struct mac_policy_conf *) data;
504c441d123SRobert Watson 
505c441d123SRobert Watson #ifdef MAC_STATIC
506c441d123SRobert Watson 	if (mac_late) {
507c441d123SRobert Watson 		printf("mac_policy_modevent: MAC_STATIC and late\n");
508c441d123SRobert Watson 		return (EBUSY);
509c441d123SRobert Watson 	}
510c441d123SRobert Watson #endif
511c441d123SRobert Watson 
51291ec0006SRobert Watson 	SDT_PROBE(mac, kernel, policy, modevent, type, mpc, 0, 0, 0);
513c441d123SRobert Watson 	switch (type) {
514c441d123SRobert Watson 	case MOD_LOAD:
515c441d123SRobert Watson 		if (mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_NOTLATE &&
516c441d123SRobert Watson 		    mac_late) {
517c441d123SRobert Watson 			printf("mac_policy_modevent: can't load %s policy "
518c441d123SRobert Watson 			    "after booting\n", mpc->mpc_name);
519c441d123SRobert Watson 			error = EBUSY;
520c441d123SRobert Watson 			break;
521c441d123SRobert Watson 		}
522c441d123SRobert Watson 		error = mac_policy_register(mpc);
523c441d123SRobert Watson 		break;
524c441d123SRobert Watson 	case MOD_UNLOAD:
525c441d123SRobert Watson 		/* Don't unregister the module if it was never registered. */
526c441d123SRobert Watson 		if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED)
527c441d123SRobert Watson 		    != 0)
528c441d123SRobert Watson 			error = mac_policy_unregister(mpc);
529c441d123SRobert Watson 		else
530c441d123SRobert Watson 			error = 0;
531c441d123SRobert Watson 		break;
532c441d123SRobert Watson 	default:
533c441d123SRobert Watson 		error = EOPNOTSUPP;
534c441d123SRobert Watson 		break;
535c441d123SRobert Watson 	}
536c441d123SRobert Watson 
537c441d123SRobert Watson 	return (error);
538c441d123SRobert Watson }
539c441d123SRobert Watson 
540c441d123SRobert Watson /*
54195fab37eSRobert Watson  * Define an error value precedence, and given two arguments, selects the
54295fab37eSRobert Watson  * value with the higher precedence.
54395fab37eSRobert Watson  */
5449e7bf51cSRobert Watson int
5459e7bf51cSRobert Watson mac_error_select(int error1, int error2)
54695fab37eSRobert Watson {
54795fab37eSRobert Watson 
54895fab37eSRobert Watson 	/* Certain decision-making errors take top priority. */
54995fab37eSRobert Watson 	if (error1 == EDEADLK || error2 == EDEADLK)
55095fab37eSRobert Watson 		return (EDEADLK);
55195fab37eSRobert Watson 
55295fab37eSRobert Watson 	/* Invalid arguments should be reported where possible. */
55395fab37eSRobert Watson 	if (error1 == EINVAL || error2 == EINVAL)
55495fab37eSRobert Watson 		return (EINVAL);
55595fab37eSRobert Watson 
55695fab37eSRobert Watson 	/* Precedence goes to "visibility", with both process and file. */
55795fab37eSRobert Watson 	if (error1 == ESRCH || error2 == ESRCH)
55895fab37eSRobert Watson 		return (ESRCH);
55995fab37eSRobert Watson 
56095fab37eSRobert Watson 	if (error1 == ENOENT || error2 == ENOENT)
56195fab37eSRobert Watson 		return (ENOENT);
56295fab37eSRobert Watson 
56395fab37eSRobert Watson 	/* Precedence goes to DAC/MAC protections. */
56495fab37eSRobert Watson 	if (error1 == EACCES || error2 == EACCES)
56595fab37eSRobert Watson 		return (EACCES);
56695fab37eSRobert Watson 
56795fab37eSRobert Watson 	/* Precedence goes to privilege. */
56895fab37eSRobert Watson 	if (error1 == EPERM || error2 == EPERM)
56995fab37eSRobert Watson 		return (EPERM);
57095fab37eSRobert Watson 
57195fab37eSRobert Watson 	/* Precedence goes to error over success; otherwise, arbitrary. */
57295fab37eSRobert Watson 	if (error1 != 0)
57395fab37eSRobert Watson 		return (error1);
57495fab37eSRobert Watson 	return (error2);
57595fab37eSRobert Watson }
57695fab37eSRobert Watson 
5775e7ce478SRobert Watson int
578f7b951a8SRobert Watson mac_check_structmac_consistent(struct mac *mac)
579f7b951a8SRobert Watson {
580f7b951a8SRobert Watson 
581cc7b13bfSRobert Watson 	if (mac->m_buflen < 0 ||
582cc7b13bfSRobert Watson 	    mac->m_buflen > MAC_MAX_LABEL_BUF_LEN)
583f7b951a8SRobert Watson 		return (EINVAL);
584f7b951a8SRobert Watson 
585f7b951a8SRobert Watson 	return (0);
586f7b951a8SRobert Watson }
587f7b951a8SRobert Watson 
58895fab37eSRobert Watson SYSINIT(mac, SI_SUB_MAC, SI_ORDER_FIRST, mac_init, NULL);
58995fab37eSRobert Watson SYSINIT(mac_late, SI_SUB_MAC_LATE, SI_ORDER_FIRST, mac_late_init, NULL);
590