xref: /freebsd/sys/security/mac/mac_framework.c (revision deb2e577a22e1447102421656135ce3fa0437102)
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 
6939b73a30SRobert Watson #include "opt_mac.h"
7039b73a30SRobert Watson 
71677b542eSDavid E. O'Brien #include <sys/cdefs.h>
72677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
73677b542eSDavid E. O'Brien 
747bc82500SRobert Watson #include <sys/param.h>
757a7ce668SAndriy Gapon #include <sys/systm.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>
8181fee06fSRobert Watson #include <sys/rmlock.h>
8291ec0006SRobert Watson #include <sys/sdt.h>
8340202729SRobert Watson #include <sys/sx.h>
8495fab37eSRobert Watson #include <sys/sysctl.h>
8595fab37eSRobert Watson 
86aed55708SRobert Watson #include <security/mac/mac_framework.h>
876fa0475dSRobert Watson #include <security/mac/mac_internal.h>
880efd6615SRobert Watson #include <security/mac/mac_policy.h>
896fa0475dSRobert Watson 
907ba28492SRobert Watson /*
912087a58cSRobert Watson  * DTrace SDT providers for MAC.
9291ec0006SRobert Watson  */
9391ec0006SRobert Watson SDT_PROVIDER_DEFINE(mac);
942087a58cSRobert Watson SDT_PROVIDER_DEFINE(mac_framework);
952087a58cSRobert Watson 
9636160958SMark Johnston SDT_PROBE_DEFINE2(mac, , policy, modevent, "int",
9792c6196cSMark Johnston     "struct mac_policy_conf *");
9836160958SMark Johnston SDT_PROBE_DEFINE1(mac, , policy, register,
9979856499SRui Paulo     "struct mac_policy_conf *");
10036160958SMark Johnston SDT_PROBE_DEFINE1(mac, , policy, unregister,
10179856499SRui Paulo     "struct mac_policy_conf *");
10291ec0006SRobert Watson 
10391ec0006SRobert Watson /*
104471e5756SRobert Watson  * Root sysctl node for all MAC and MAC policy controls.
1057ba28492SRobert Watson  */
10695fab37eSRobert Watson SYSCTL_NODE(_security, OID_AUTO, mac, CTLFLAG_RW, 0,
10795fab37eSRobert Watson     "TrustedBSD MAC policy controls");
108b2f0927aSRobert Watson 
10917041e67SRobert Watson /*
110471e5756SRobert Watson  * Declare that the kernel provides MAC support, version 3 (FreeBSD 7.x).
111471e5756SRobert Watson  * This permits modules to refuse to be loaded if the necessary support isn't
112471e5756SRobert Watson  * present, even if it's pre-boot.
113471e5756SRobert Watson  */
114471e5756SRobert Watson MODULE_VERSION(kernel_mac_support, MAC_VERSION);
115be23ba9aSRobert Watson 
116be23ba9aSRobert Watson static unsigned int	mac_version = MAC_VERSION;
117471e5756SRobert Watson SYSCTL_UINT(_security_mac, OID_AUTO, version, CTLFLAG_RD, &mac_version, 0,
118471e5756SRobert Watson     "");
119471e5756SRobert Watson 
120471e5756SRobert Watson /*
12117041e67SRobert Watson  * Labels consist of a indexed set of "slots", which are allocated policies
12217041e67SRobert Watson  * as required.  The MAC Framework maintains a bitmask of slots allocated so
12317041e67SRobert Watson  * far to prevent reuse.  Slots cannot be reused, as the MAC Framework
12417041e67SRobert Watson  * guarantees that newly allocated slots in labels will be NULL unless
12517041e67SRobert Watson  * otherwise initialized, and because we do not have a mechanism to garbage
12617041e67SRobert Watson  * collect slots on policy unload.  As labeled policies tend to be statically
12717041e67SRobert Watson  * loaded during boot, and not frequently unloaded and reloaded, this is not
12817041e67SRobert Watson  * generally an issue.
12917041e67SRobert Watson  */
130b2aef571SRobert Watson #if MAC_MAX_SLOTS > 32
131b2aef571SRobert Watson #error "MAC_MAX_SLOTS too large"
13295fab37eSRobert Watson #endif
133a13c67daSRobert Watson 
134b2aef571SRobert Watson static unsigned int mac_max_slots = MAC_MAX_SLOTS;
135b2aef571SRobert Watson static unsigned int mac_slot_offsets_free = (1 << MAC_MAX_SLOTS) - 1;
136471e5756SRobert Watson SYSCTL_UINT(_security_mac, OID_AUTO, max_slots, CTLFLAG_RD, &mac_max_slots,
137471e5756SRobert Watson     0, "");
13895fab37eSRobert Watson 
139a67fe518SRobert Watson /*
140a67fe518SRobert Watson  * Has the kernel started generating labeled objects yet?  All read/write
141a67fe518SRobert Watson  * access to this variable is serialized during the boot process.  Following
142a67fe518SRobert Watson  * the end of serialization, we don't update this flag; no locking.
143a67fe518SRobert Watson  */
144be23ba9aSRobert Watson static int	mac_late = 0;
145763bbd2fSRobert Watson 
146225bff6fSRobert Watson /*
1476356dba0SRobert Watson  * Each policy declares a mask of object types requiring labels to be
1486356dba0SRobert Watson  * allocated for them.  For convenience, we combine and cache the bitwise or
1496356dba0SRobert Watson  * of the per-policy object flags to track whether we will allocate a label
1506356dba0SRobert Watson  * for an object type at run-time.
151225bff6fSRobert Watson  */
1526356dba0SRobert Watson uint64_t	mac_labeled;
153123d2cb7SMatthew D Fleming SYSCTL_UQUAD(_security_mac, OID_AUTO, labeled, CTLFLAG_RD, &mac_labeled, 0,
1546356dba0SRobert Watson     "Mask of object types being labeled");
155225bff6fSRobert Watson 
156f7b951a8SRobert Watson MALLOC_DEFINE(M_MACTEMP, "mactemp", "MAC temporary label storage");
15795fab37eSRobert Watson 
15895fab37eSRobert Watson /*
15940202729SRobert Watson  * MAC policy modules are placed in one of two lists: mac_static_policy_list,
16040202729SRobert Watson  * for policies that are loaded early and cannot be unloaded, and
16140202729SRobert Watson  * mac_policy_list, which holds policies either loaded later in the boot
16240202729SRobert Watson  * cycle or that may be unloaded.  The static policy list does not require
16340202729SRobert Watson  * locks to iterate over, but the dynamic list requires synchronization.
16440202729SRobert Watson  * Support for dynamic policy loading can be compiled out using the
16540202729SRobert Watson  * MAC_STATIC kernel option.
16641a17fe3SRobert Watson  *
16740202729SRobert Watson  * The dynamic policy list is protected by two locks: modifying the list
16840202729SRobert Watson  * requires both locks to be held exclusively.  One of the locks,
16981fee06fSRobert Watson  * mac_policy_rm, is acquired over policy entry points that will never sleep;
17040202729SRobert Watson  * the other, mac_policy_sx, is acquire over policy entry points that may
17140202729SRobert Watson  * sleep.  The former category will be used when kernel locks may be held
17240202729SRobert Watson  * over calls to the MAC Framework, during network processing in ithreads,
17340202729SRobert Watson  * etc.  The latter will tend to involve potentially blocking memory
17440202729SRobert Watson  * allocations, extended attribute I/O, etc.
17595fab37eSRobert Watson  */
1760a05006dSRobert Watson #ifndef MAC_STATIC
17781fee06fSRobert Watson static struct rmlock mac_policy_rm;	/* Non-sleeping entry points. */
17840202729SRobert Watson static struct sx mac_policy_sx;		/* Sleeping entry points. */
179*deb2e577SMateusz Guzik static struct rmslock mac_policy_rms;
1800a05006dSRobert Watson #endif
18140202729SRobert Watson 
182089c1bdaSRobert Watson struct mac_policy_list_head mac_policy_list;
183089c1bdaSRobert Watson struct mac_policy_list_head mac_static_policy_list;
184f93bfb23SRobert Watson u_int mac_policy_count;			/* Registered policy count. */
185a96acd1aSRobert Watson 
18640202729SRobert Watson static void	mac_policy_xlock(void);
18740202729SRobert Watson static void	mac_policy_xlock_assert(void);
18840202729SRobert Watson static void	mac_policy_xunlock(void);
18940202729SRobert Watson 
190089c1bdaSRobert Watson void
19181fee06fSRobert Watson mac_policy_slock_nosleep(struct rm_priotracker *tracker)
19241a17fe3SRobert Watson {
193c8e7bf92SRobert Watson 
1940a05006dSRobert Watson #ifndef MAC_STATIC
1951e4cadcbSRobert Watson 	if (!mac_late)
1961e4cadcbSRobert Watson 		return;
1971e4cadcbSRobert Watson 
19881fee06fSRobert Watson 	rm_rlock(&mac_policy_rm, tracker);
19940202729SRobert Watson #endif
20040202729SRobert Watson }
20140202729SRobert Watson 
20240202729SRobert Watson void
20340202729SRobert Watson mac_policy_slock_sleep(void)
20440202729SRobert Watson {
20540202729SRobert Watson 
20641a17fe3SRobert Watson 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
20740202729SRobert Watson  	    "mac_policy_slock_sleep");
20840202729SRobert Watson 
20940202729SRobert Watson #ifndef MAC_STATIC
21040202729SRobert Watson 	if (!mac_late)
21140202729SRobert Watson 		return;
21240202729SRobert Watson 
213*deb2e577SMateusz Guzik 	rms_rlock(&mac_policy_rms);
2140a05006dSRobert Watson #endif
21541a17fe3SRobert Watson }
21695fab37eSRobert Watson 
217089c1bdaSRobert Watson void
21881fee06fSRobert Watson mac_policy_sunlock_nosleep(struct rm_priotracker *tracker)
21941a17fe3SRobert Watson {
220c8e7bf92SRobert Watson 
2210a05006dSRobert Watson #ifndef MAC_STATIC
2221e4cadcbSRobert Watson 	if (!mac_late)
2231e4cadcbSRobert Watson 		return;
2241e4cadcbSRobert Watson 
22581fee06fSRobert Watson 	rm_runlock(&mac_policy_rm, tracker);
2260a05006dSRobert Watson #endif
22741a17fe3SRobert Watson }
228225bff6fSRobert Watson 
229089c1bdaSRobert Watson void
23040202729SRobert Watson mac_policy_sunlock_sleep(void)
23141a17fe3SRobert Watson {
232c8e7bf92SRobert Watson 
2330a05006dSRobert Watson #ifndef MAC_STATIC
2341e4cadcbSRobert Watson 	if (!mac_late)
2351e4cadcbSRobert Watson 		return;
2361e4cadcbSRobert Watson 
237*deb2e577SMateusz Guzik 	rms_runlock(&mac_policy_rms);
2380a05006dSRobert Watson #endif
23941a17fe3SRobert Watson }
24041a17fe3SRobert Watson 
24140202729SRobert Watson static void
24240202729SRobert Watson mac_policy_xlock(void)
24341a17fe3SRobert Watson {
24440202729SRobert Watson 
24540202729SRobert Watson 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
24640202729SRobert Watson  	    "mac_policy_xlock()");
24740202729SRobert Watson 
2480a05006dSRobert Watson #ifndef MAC_STATIC
2491e4cadcbSRobert Watson 	if (!mac_late)
2501e4cadcbSRobert Watson 		return;
2511e4cadcbSRobert Watson 
25240202729SRobert Watson 	sx_xlock(&mac_policy_sx);
253*deb2e577SMateusz Guzik 	rms_wlock(&mac_policy_rms);
25481fee06fSRobert Watson 	rm_wlock(&mac_policy_rm);
25540202729SRobert Watson #endif
25640202729SRobert Watson }
257989d4098SRobert Watson 
25840202729SRobert Watson static void
25940202729SRobert Watson mac_policy_xunlock(void)
26040202729SRobert Watson {
26140202729SRobert Watson 
26240202729SRobert Watson #ifndef MAC_STATIC
26340202729SRobert Watson 	if (!mac_late)
26440202729SRobert Watson 		return;
26540202729SRobert Watson 
26681fee06fSRobert Watson 	rm_wunlock(&mac_policy_rm);
267*deb2e577SMateusz Guzik 	rms_wunlock(&mac_policy_rms);
26840202729SRobert Watson 	sx_xunlock(&mac_policy_sx);
26940202729SRobert Watson #endif
27040202729SRobert Watson }
27140202729SRobert Watson 
27240202729SRobert Watson static void
27340202729SRobert Watson mac_policy_xlock_assert(void)
27440202729SRobert Watson {
27540202729SRobert Watson 
27640202729SRobert Watson #ifndef MAC_STATIC
27740202729SRobert Watson 	if (!mac_late)
27840202729SRobert Watson 		return;
27940202729SRobert Watson 
28081fee06fSRobert Watson 	/* XXXRW: rm_assert(&mac_policy_rm, RA_WLOCKED); */
28140202729SRobert Watson 	sx_assert(&mac_policy_sx, SA_XLOCKED);
2820a05006dSRobert Watson #endif
28341a17fe3SRobert Watson }
28495fab37eSRobert Watson 
28595fab37eSRobert Watson /*
28695fab37eSRobert Watson  * Initialize the MAC subsystem, including appropriate SMP locks.
28795fab37eSRobert Watson  */
28895fab37eSRobert Watson static void
28995fab37eSRobert Watson mac_init(void)
29095fab37eSRobert Watson {
29195fab37eSRobert Watson 
29241a17fe3SRobert Watson 	LIST_INIT(&mac_static_policy_list);
29395fab37eSRobert Watson 	LIST_INIT(&mac_policy_list);
294eca8a663SRobert Watson 	mac_labelzone_init();
29541a17fe3SRobert Watson 
2960a05006dSRobert Watson #ifndef MAC_STATIC
297f7fadf1fSKonstantin Belousov 	rm_init_flags(&mac_policy_rm, "mac_policy_rm", RM_NOWITNESS |
298f7fadf1fSKonstantin Belousov 	    RM_RECURSE);
2995f51fb48SRobert Watson 	sx_init_flags(&mac_policy_sx, "mac_policy_sx", SX_NOWITNESS);
300*deb2e577SMateusz Guzik 	rms_init(&mac_policy_rms, "mac_policy_rms");
3010a05006dSRobert Watson #endif
30295fab37eSRobert Watson }
30395fab37eSRobert Watson 
30495fab37eSRobert Watson /*
30517041e67SRobert Watson  * For the purposes of modules that want to know if they were loaded "early",
30617041e67SRobert Watson  * set the mac_late flag once we've processed modules either linked into the
30717041e67SRobert Watson  * kernel, or loaded before the kernel startup.
30895fab37eSRobert Watson  */
30995fab37eSRobert Watson static void
31095fab37eSRobert Watson mac_late_init(void)
31195fab37eSRobert Watson {
31295fab37eSRobert Watson 
31395fab37eSRobert Watson 	mac_late = 1;
31495fab37eSRobert Watson }
31595fab37eSRobert Watson 
31695fab37eSRobert Watson /*
3179162f64bSRobert Watson  * Given a policy, derive from its set of non-NULL label init methods what
3189162f64bSRobert Watson  * object types the policy is interested in.
3199162f64bSRobert Watson  */
3209162f64bSRobert Watson static uint64_t
3219162f64bSRobert Watson mac_policy_getlabeled(struct mac_policy_conf *mpc)
3229162f64bSRobert Watson {
3239162f64bSRobert Watson 	uint64_t labeled;
3249162f64bSRobert Watson 
3259162f64bSRobert Watson #define	MPC_FLAG(method, flag)					\
3269162f64bSRobert Watson 	if (mpc->mpc_ops->mpo_ ## method != NULL)			\
3279162f64bSRobert Watson 		labeled |= (flag);					\
3289162f64bSRobert Watson 
3299162f64bSRobert Watson 	labeled = 0;
3309162f64bSRobert Watson 	MPC_FLAG(cred_init_label, MPC_OBJECT_CRED);
3319162f64bSRobert Watson 	MPC_FLAG(proc_init_label, MPC_OBJECT_PROC);
3329162f64bSRobert Watson 	MPC_FLAG(vnode_init_label, MPC_OBJECT_VNODE);
3339162f64bSRobert Watson 	MPC_FLAG(inpcb_init_label, MPC_OBJECT_INPCB);
3349162f64bSRobert Watson 	MPC_FLAG(socket_init_label, MPC_OBJECT_SOCKET);
3359162f64bSRobert Watson 	MPC_FLAG(devfs_init_label, MPC_OBJECT_DEVFS);
3369162f64bSRobert Watson 	MPC_FLAG(mbuf_init_label, MPC_OBJECT_MBUF);
3379162f64bSRobert Watson 	MPC_FLAG(ipq_init_label, MPC_OBJECT_IPQ);
3389162f64bSRobert Watson 	MPC_FLAG(ifnet_init_label, MPC_OBJECT_IFNET);
3399162f64bSRobert Watson 	MPC_FLAG(bpfdesc_init_label, MPC_OBJECT_BPFDESC);
3409162f64bSRobert Watson 	MPC_FLAG(pipe_init_label, MPC_OBJECT_PIPE);
3419162f64bSRobert Watson 	MPC_FLAG(mount_init_label, MPC_OBJECT_MOUNT);
3429162f64bSRobert Watson 	MPC_FLAG(posixsem_init_label, MPC_OBJECT_POSIXSEM);
3439162f64bSRobert Watson 	MPC_FLAG(posixshm_init_label, MPC_OBJECT_POSIXSHM);
3449162f64bSRobert Watson 	MPC_FLAG(sysvmsg_init_label, MPC_OBJECT_SYSVMSG);
3459162f64bSRobert Watson 	MPC_FLAG(sysvmsq_init_label, MPC_OBJECT_SYSVMSQ);
3469162f64bSRobert Watson 	MPC_FLAG(sysvsem_init_label, MPC_OBJECT_SYSVSEM);
3479162f64bSRobert Watson 	MPC_FLAG(sysvshm_init_label, MPC_OBJECT_SYSVSHM);
3489162f64bSRobert Watson 	MPC_FLAG(syncache_init_label, MPC_OBJECT_SYNCACHE);
3499162f64bSRobert Watson 	MPC_FLAG(ip6q_init_label, MPC_OBJECT_IP6Q);
3509162f64bSRobert Watson 
3519162f64bSRobert Watson #undef MPC_FLAG
3529162f64bSRobert Watson 	return (labeled);
3539162f64bSRobert Watson }
3549162f64bSRobert Watson 
3559162f64bSRobert Watson /*
3569162f64bSRobert Watson  * When policies are loaded or unloaded, walk the list of registered policies
3579162f64bSRobert Watson  * and built mac_labeled, a bitmask representing the union of all objects
3589162f64bSRobert Watson  * requiring labels across all policies.
359225bff6fSRobert Watson  */
360225bff6fSRobert Watson static void
361f93bfb23SRobert Watson mac_policy_update(void)
362225bff6fSRobert Watson {
3636356dba0SRobert Watson 	struct mac_policy_conf *mpc;
364225bff6fSRobert Watson 
36540202729SRobert Watson 	mac_policy_xlock_assert();
366225bff6fSRobert Watson 
3676356dba0SRobert Watson 	mac_labeled = 0;
368f93bfb23SRobert Watson 	mac_policy_count = 0;
369f93bfb23SRobert Watson 	LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {
3709162f64bSRobert Watson 		mac_labeled |= mac_policy_getlabeled(mpc);
371f93bfb23SRobert Watson 		mac_policy_count++;
372f93bfb23SRobert Watson 	}
373f93bfb23SRobert Watson 	LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {
3749162f64bSRobert Watson 		mac_labeled |= mac_policy_getlabeled(mpc);
375f93bfb23SRobert Watson 		mac_policy_count++;
376f93bfb23SRobert Watson 	}
377225bff6fSRobert Watson }
378225bff6fSRobert Watson 
37995fab37eSRobert Watson static int
38095fab37eSRobert Watson mac_policy_register(struct mac_policy_conf *mpc)
38195fab37eSRobert Watson {
38295fab37eSRobert Watson 	struct mac_policy_conf *tmpc;
38341a17fe3SRobert Watson 	int error, slot, static_entry;
38495fab37eSRobert Watson 
38541a17fe3SRobert Watson 	error = 0;
38641a17fe3SRobert Watson 
38741a17fe3SRobert Watson 	/*
38817041e67SRobert Watson 	 * We don't technically need exclusive access while !mac_late, but
38917041e67SRobert Watson 	 * hold it for assertion consistency.
39041a17fe3SRobert Watson 	 */
39140202729SRobert Watson 	mac_policy_xlock();
39241a17fe3SRobert Watson 
39341a17fe3SRobert Watson 	/*
39417041e67SRobert Watson 	 * If the module can potentially be unloaded, or we're loading late,
39517041e67SRobert Watson 	 * we have to stick it in the non-static list and pay an extra
39617041e67SRobert Watson 	 * performance overhead.  Otherwise, we can pay a light locking cost
39717041e67SRobert Watson 	 * and stick it in the static list.
39841a17fe3SRobert Watson 	 */
39941a17fe3SRobert Watson 	static_entry = (!mac_late &&
40041a17fe3SRobert Watson 	    !(mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK));
40141a17fe3SRobert Watson 
40241a17fe3SRobert Watson 	if (static_entry) {
40341a17fe3SRobert Watson 		LIST_FOREACH(tmpc, &mac_static_policy_list, mpc_list) {
40441a17fe3SRobert Watson 			if (strcmp(tmpc->mpc_name, mpc->mpc_name) == 0) {
40541a17fe3SRobert Watson 				error = EEXIST;
40641a17fe3SRobert Watson 				goto out;
40741a17fe3SRobert Watson 			}
40841a17fe3SRobert Watson 		}
40941a17fe3SRobert Watson 	} else {
41095fab37eSRobert Watson 		LIST_FOREACH(tmpc, &mac_policy_list, mpc_list) {
41195fab37eSRobert Watson 			if (strcmp(tmpc->mpc_name, mpc->mpc_name) == 0) {
41241a17fe3SRobert Watson 				error = EEXIST;
41341a17fe3SRobert Watson 				goto out;
41441a17fe3SRobert Watson 			}
41595fab37eSRobert Watson 		}
41695fab37eSRobert Watson 	}
41795fab37eSRobert Watson 	if (mpc->mpc_field_off != NULL) {
418b2aef571SRobert Watson 		slot = ffs(mac_slot_offsets_free);
41995fab37eSRobert Watson 		if (slot == 0) {
42041a17fe3SRobert Watson 			error = ENOMEM;
42141a17fe3SRobert Watson 			goto out;
42295fab37eSRobert Watson 		}
42395fab37eSRobert Watson 		slot--;
424b2aef571SRobert Watson 		mac_slot_offsets_free &= ~(1 << slot);
42595fab37eSRobert Watson 		*mpc->mpc_field_off = slot;
42695fab37eSRobert Watson 	}
42795fab37eSRobert Watson 	mpc->mpc_runtime_flags |= MPC_RUNTIME_FLAG_REGISTERED;
42841a17fe3SRobert Watson 
42941a17fe3SRobert Watson 	/*
43017041e67SRobert Watson 	 * If we're loading a MAC module after the framework has initialized,
43117041e67SRobert Watson 	 * it has to go into the dynamic list.  If we're loading it before
43217041e67SRobert Watson 	 * we've finished initializing, it can go into the static list with
43317041e67SRobert Watson 	 * weaker locker requirements.
43441a17fe3SRobert Watson 	 */
43541a17fe3SRobert Watson 	if (static_entry)
43641a17fe3SRobert Watson 		LIST_INSERT_HEAD(&mac_static_policy_list, mpc, mpc_list);
43741a17fe3SRobert Watson 	else
43895fab37eSRobert Watson 		LIST_INSERT_HEAD(&mac_policy_list, mpc, mpc_list);
43995fab37eSRobert Watson 
44017041e67SRobert Watson 	/*
44117041e67SRobert Watson 	 * Per-policy initialization.  Currently, this takes place under the
44217041e67SRobert Watson 	 * exclusive lock, so policies must not sleep in their init method.
44317041e67SRobert Watson 	 * In the future, we may want to separate "init" from "start", with
444bc5ade0dSPedro F. Giffuni 	 * "init" occurring without the lock held.  Likewise, on tear-down,
44517041e67SRobert Watson 	 * breaking out "stop" from "destroy".
44617041e67SRobert Watson 	 */
44795fab37eSRobert Watson 	if (mpc->mpc_ops->mpo_init != NULL)
44895fab37eSRobert Watson 		(*(mpc->mpc_ops->mpo_init))(mpc);
449f93bfb23SRobert Watson 	mac_policy_update();
45095fab37eSRobert Watson 
45136160958SMark Johnston 	SDT_PROBE1(mac, , policy, register, mpc);
45295fab37eSRobert Watson 	printf("Security policy loaded: %s (%s)\n", mpc->mpc_fullname,
45395fab37eSRobert Watson 	    mpc->mpc_name);
45495fab37eSRobert Watson 
45541a17fe3SRobert Watson out:
45640202729SRobert Watson 	mac_policy_xunlock();
45741a17fe3SRobert Watson 	return (error);
45895fab37eSRobert Watson }
45995fab37eSRobert Watson 
46095fab37eSRobert Watson static int
46195fab37eSRobert Watson mac_policy_unregister(struct mac_policy_conf *mpc)
46295fab37eSRobert Watson {
46395fab37eSRobert Watson 
464ea599aa0SRobert Watson 	/*
46517041e67SRobert Watson 	 * If we fail the load, we may get a request to unload.  Check to see
46617041e67SRobert Watson 	 * if we did the run-time registration, and if not, silently succeed.
467ea599aa0SRobert Watson 	 */
46840202729SRobert Watson 	mac_policy_xlock();
469ea599aa0SRobert Watson 	if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED) == 0) {
47040202729SRobert Watson 		mac_policy_xunlock();
471ea599aa0SRobert Watson 		return (0);
472ea599aa0SRobert Watson 	}
47395fab37eSRobert Watson #if 0
47495fab37eSRobert Watson 	/*
47595fab37eSRobert Watson 	 * Don't allow unloading modules with private data.
47695fab37eSRobert Watson 	 */
477ea599aa0SRobert Watson 	if (mpc->mpc_field_off != NULL) {
47840202729SRobert Watson 		mac_policy_xunlock();
47995fab37eSRobert Watson 		return (EBUSY);
480ea599aa0SRobert Watson 	}
48195fab37eSRobert Watson #endif
482ea599aa0SRobert Watson 	/*
48317041e67SRobert Watson 	 * Only allow the unload to proceed if the module is unloadable by
48417041e67SRobert Watson 	 * its own definition.
485ea599aa0SRobert Watson 	 */
486ea599aa0SRobert Watson 	if ((mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK) == 0) {
48740202729SRobert Watson 		mac_policy_xunlock();
48895fab37eSRobert Watson 		return (EBUSY);
489ea599aa0SRobert Watson 	}
49095fab37eSRobert Watson 	if (mpc->mpc_ops->mpo_destroy != NULL)
49195fab37eSRobert Watson 		(*(mpc->mpc_ops->mpo_destroy))(mpc);
49295fab37eSRobert Watson 
49395fab37eSRobert Watson 	LIST_REMOVE(mpc, mpc_list);
4949aeffb2bSRobert Watson 	mpc->mpc_runtime_flags &= ~MPC_RUNTIME_FLAG_REGISTERED;
495f93bfb23SRobert Watson 	mac_policy_update();
49640202729SRobert Watson 	mac_policy_xunlock();
497a96acd1aSRobert Watson 
49836160958SMark Johnston 	SDT_PROBE1(mac, , policy, unregister, mpc);
49995fab37eSRobert Watson 	printf("Security policy unload: %s (%s)\n", mpc->mpc_fullname,
50095fab37eSRobert Watson 	    mpc->mpc_name);
50195fab37eSRobert Watson 
50295fab37eSRobert Watson 	return (0);
50395fab37eSRobert Watson }
50495fab37eSRobert Watson 
50595fab37eSRobert Watson /*
506c441d123SRobert Watson  * Allow MAC policy modules to register during boot, etc.
507c441d123SRobert Watson  */
508c441d123SRobert Watson int
509c441d123SRobert Watson mac_policy_modevent(module_t mod, int type, void *data)
510c441d123SRobert Watson {
511c441d123SRobert Watson 	struct mac_policy_conf *mpc;
512c441d123SRobert Watson 	int error;
513c441d123SRobert Watson 
514c441d123SRobert Watson 	error = 0;
515c441d123SRobert Watson 	mpc = (struct mac_policy_conf *) data;
516c441d123SRobert Watson 
517c441d123SRobert Watson #ifdef MAC_STATIC
518c441d123SRobert Watson 	if (mac_late) {
519c441d123SRobert Watson 		printf("mac_policy_modevent: MAC_STATIC and late\n");
520c441d123SRobert Watson 		return (EBUSY);
521c441d123SRobert Watson 	}
522c441d123SRobert Watson #endif
523c441d123SRobert Watson 
52436160958SMark Johnston 	SDT_PROBE2(mac, , policy, modevent, type, mpc);
525c441d123SRobert Watson 	switch (type) {
526c441d123SRobert Watson 	case MOD_LOAD:
527c441d123SRobert Watson 		if (mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_NOTLATE &&
528c441d123SRobert Watson 		    mac_late) {
529c441d123SRobert Watson 			printf("mac_policy_modevent: can't load %s policy "
530c441d123SRobert Watson 			    "after booting\n", mpc->mpc_name);
531c441d123SRobert Watson 			error = EBUSY;
532c441d123SRobert Watson 			break;
533c441d123SRobert Watson 		}
534c441d123SRobert Watson 		error = mac_policy_register(mpc);
535c441d123SRobert Watson 		break;
536c441d123SRobert Watson 	case MOD_UNLOAD:
537c441d123SRobert Watson 		/* Don't unregister the module if it was never registered. */
538c441d123SRobert Watson 		if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED)
539c441d123SRobert Watson 		    != 0)
540c441d123SRobert Watson 			error = mac_policy_unregister(mpc);
541c441d123SRobert Watson 		else
542c441d123SRobert Watson 			error = 0;
543c441d123SRobert Watson 		break;
544c441d123SRobert Watson 	default:
545c441d123SRobert Watson 		error = EOPNOTSUPP;
546c441d123SRobert Watson 		break;
547c441d123SRobert Watson 	}
548c441d123SRobert Watson 
549c441d123SRobert Watson 	return (error);
550c441d123SRobert Watson }
551c441d123SRobert Watson 
552c441d123SRobert Watson /*
55395fab37eSRobert Watson  * Define an error value precedence, and given two arguments, selects the
55495fab37eSRobert Watson  * value with the higher precedence.
55595fab37eSRobert Watson  */
5569e7bf51cSRobert Watson int
5579e7bf51cSRobert Watson mac_error_select(int error1, int error2)
55895fab37eSRobert Watson {
55995fab37eSRobert Watson 
56095fab37eSRobert Watson 	/* Certain decision-making errors take top priority. */
56195fab37eSRobert Watson 	if (error1 == EDEADLK || error2 == EDEADLK)
56295fab37eSRobert Watson 		return (EDEADLK);
56395fab37eSRobert Watson 
56495fab37eSRobert Watson 	/* Invalid arguments should be reported where possible. */
56595fab37eSRobert Watson 	if (error1 == EINVAL || error2 == EINVAL)
56695fab37eSRobert Watson 		return (EINVAL);
56795fab37eSRobert Watson 
56895fab37eSRobert Watson 	/* Precedence goes to "visibility", with both process and file. */
56995fab37eSRobert Watson 	if (error1 == ESRCH || error2 == ESRCH)
57095fab37eSRobert Watson 		return (ESRCH);
57195fab37eSRobert Watson 
57295fab37eSRobert Watson 	if (error1 == ENOENT || error2 == ENOENT)
57395fab37eSRobert Watson 		return (ENOENT);
57495fab37eSRobert Watson 
57595fab37eSRobert Watson 	/* Precedence goes to DAC/MAC protections. */
57695fab37eSRobert Watson 	if (error1 == EACCES || error2 == EACCES)
57795fab37eSRobert Watson 		return (EACCES);
57895fab37eSRobert Watson 
57995fab37eSRobert Watson 	/* Precedence goes to privilege. */
58095fab37eSRobert Watson 	if (error1 == EPERM || error2 == EPERM)
58195fab37eSRobert Watson 		return (EPERM);
58295fab37eSRobert Watson 
58395fab37eSRobert Watson 	/* Precedence goes to error over success; otherwise, arbitrary. */
58495fab37eSRobert Watson 	if (error1 != 0)
58595fab37eSRobert Watson 		return (error1);
58695fab37eSRobert Watson 	return (error2);
58795fab37eSRobert Watson }
58895fab37eSRobert Watson 
5895e7ce478SRobert Watson int
590f7b951a8SRobert Watson mac_check_structmac_consistent(struct mac *mac)
591f7b951a8SRobert Watson {
592f7b951a8SRobert Watson 
5936324de03SMark Johnston 	/* Require that labels have a non-zero length. */
5946324de03SMark Johnston 	if (mac->m_buflen > MAC_MAX_LABEL_BUF_LEN ||
5956324de03SMark Johnston 	    mac->m_buflen <= sizeof(""))
596f7b951a8SRobert Watson 		return (EINVAL);
597f7b951a8SRobert Watson 
598f7b951a8SRobert Watson 	return (0);
599f7b951a8SRobert Watson }
600f7b951a8SRobert Watson 
60195fab37eSRobert Watson SYSINIT(mac, SI_SUB_MAC, SI_ORDER_FIRST, mac_init, NULL);
60295fab37eSRobert Watson SYSINIT(mac_late, SI_SUB_MAC_LATE, SI_ORDER_FIRST, mac_late_init, NULL);
603