xref: /freebsd/sys/security/mac/mac_framework.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*-
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.
7  * All rights reserved.
8  *
9  * This software was developed by Robert Watson and Ilmar Habibulin for the
10  * TrustedBSD Project.
11  *
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.
16  *
17  * This software was enhanced by SPARTA ISSO under SPAWAR contract
18  * N66001-04-C-6019 ("SEFOS").
19  *
20  * This software was developed at the University of Cambridge Computer
21  * Laboratory with support from a grant from Google, Inc.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
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.
31  *
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
42  * SUCH DAMAGE.
43  */
44 
45 /*-
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
49  * calls.
50  *
51  * The MAC Framework implements three programming interfaces:
52  *
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.
56  *
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.
60  *
61  * - The user MAC API, defined in mac.h, which allows user programs to query
62  *   and set label state on objects.
63  *
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_*.
67  */
68 
69 #include "opt_mac.h"
70 
71 #include <sys/cdefs.h>
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/condvar.h>
75 #include <sys/kernel.h>
76 #include <sys/lock.h>
77 #include <sys/mac.h>
78 #include <sys/module.h>
79 #include <sys/rmlock.h>
80 #include <sys/sdt.h>
81 #include <sys/sx.h>
82 #include <sys/sysctl.h>
83 #include <sys/vnode.h>
84 
85 #include <security/mac/mac_framework.h>
86 #include <security/mac/mac_internal.h>
87 #include <security/mac/mac_policy.h>
88 
89 /*
90  * DTrace SDT providers for MAC.
91  */
92 SDT_PROVIDER_DEFINE(mac);
93 SDT_PROVIDER_DEFINE(mac_framework);
94 
95 SDT_PROBE_DEFINE2(mac, , policy, modevent, "int",
96     "struct mac_policy_conf *");
97 SDT_PROBE_DEFINE1(mac, , policy, register,
98     "struct mac_policy_conf *");
99 SDT_PROBE_DEFINE1(mac, , policy, unregister,
100     "struct mac_policy_conf *");
101 
102 /*
103  * Root sysctl node for all MAC and MAC policy controls.
104  */
105 SYSCTL_NODE(_security, OID_AUTO, mac, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
106     "TrustedBSD MAC policy controls");
107 
108 /*
109  * Declare that the kernel provides MAC support, version 3 (FreeBSD 7.x).
110  * This permits modules to refuse to be loaded if the necessary support isn't
111  * present, even if it's pre-boot.
112  */
113 MODULE_VERSION(kernel_mac_support, MAC_VERSION);
114 
115 static unsigned int	mac_version = MAC_VERSION;
116 SYSCTL_UINT(_security_mac, OID_AUTO, version, CTLFLAG_RD, &mac_version, 0,
117     "");
118 
119 /*
120  * Flags for inlined checks. Note this would be best hotpatched at runtime.
121  * The following is a band-aid.
122  *
123  * Use FPFLAG for hooks running in commonly executed paths and FPFLAG_RARE
124  * for the rest.
125  */
126 #define FPFLAG(f)	\
127 bool __read_frequently mac_##f##_fp_flag
128 
129 #define FPFLAG_RARE(f)	\
130 bool __read_mostly mac_##f##_fp_flag
131 
132 FPFLAG(priv_check);
133 FPFLAG(priv_grant);
134 FPFLAG(vnode_check_lookup);
135 FPFLAG(vnode_check_open);
136 FPFLAG(vnode_check_stat);
137 FPFLAG(vnode_check_read);
138 FPFLAG(vnode_check_write);
139 FPFLAG(vnode_check_mmap);
140 FPFLAG_RARE(vnode_check_poll);
141 FPFLAG_RARE(vnode_check_rename_from);
142 FPFLAG_RARE(vnode_check_access);
143 FPFLAG_RARE(vnode_check_readlink);
144 FPFLAG_RARE(pipe_check_stat);
145 FPFLAG_RARE(pipe_check_poll);
146 FPFLAG_RARE(pipe_check_read);
147 FPFLAG_RARE(ifnet_create_mbuf);
148 FPFLAG_RARE(ifnet_check_transmit);
149 
150 #undef FPFLAG
151 #undef FPFLAG_RARE
152 
153 /*
154  * Labels consist of a indexed set of "slots", which are allocated policies
155  * as required.  The MAC Framework maintains a bitmask of slots allocated so
156  * far to prevent reuse.  Slots cannot be reused, as the MAC Framework
157  * guarantees that newly allocated slots in labels will be NULL unless
158  * otherwise initialized, and because we do not have a mechanism to garbage
159  * collect slots on policy unload.  As labeled policies tend to be statically
160  * loaded during boot, and not frequently unloaded and reloaded, this is not
161  * generally an issue.
162  */
163 #if MAC_MAX_SLOTS > 32
164 #error "MAC_MAX_SLOTS too large"
165 #endif
166 
167 static unsigned int mac_max_slots = MAC_MAX_SLOTS;
168 static unsigned int mac_slot_offsets_free = (1 << MAC_MAX_SLOTS) - 1;
169 SYSCTL_UINT(_security_mac, OID_AUTO, max_slots, CTLFLAG_RD, &mac_max_slots,
170     0, "");
171 
172 /*
173  * Has the kernel started generating labeled objects yet?  All read/write
174  * access to this variable is serialized during the boot process.  Following
175  * the end of serialization, we don't update this flag; no locking.
176  */
177 static int	mac_late = 0;
178 
179 /*
180  * Each policy declares a mask of object types requiring labels to be
181  * allocated for them.  For convenience, we combine and cache the bitwise or
182  * of the per-policy object flags to track whether we will allocate a label
183  * for an object type at run-time.
184  */
185 uint64_t	mac_labeled;
186 SYSCTL_UQUAD(_security_mac, OID_AUTO, labeled, CTLFLAG_RD, &mac_labeled, 0,
187     "Mask of object types being labeled");
188 
189 MALLOC_DEFINE(M_MACTEMP, "mactemp", "MAC temporary label storage");
190 
191 /*
192  * MAC policy modules are placed in one of two lists: mac_static_policy_list,
193  * for policies that are loaded early and cannot be unloaded, and
194  * mac_policy_list, which holds policies either loaded later in the boot
195  * cycle or that may be unloaded.  The static policy list does not require
196  * locks to iterate over, but the dynamic list requires synchronization.
197  * Support for dynamic policy loading can be compiled out using the
198  * MAC_STATIC kernel option.
199  *
200  * The dynamic policy list is protected by two locks: modifying the list
201  * requires both locks to be held exclusively.  One of the locks,
202  * mac_policy_rm, is acquired over policy entry points that will never sleep;
203  * the other, mac_policy_rms, is acquired over policy entry points that may
204  * sleep.  The former category will be used when kernel locks may be held
205  * over calls to the MAC Framework, during network processing in ithreads,
206  * etc.  The latter will tend to involve potentially blocking memory
207  * allocations, extended attribute I/O, etc.
208  */
209 #ifndef MAC_STATIC
210 static struct rmlock mac_policy_rm;	/* Non-sleeping entry points. */
211 static struct rmslock mac_policy_rms;	/* Sleeping entry points. */
212 #endif
213 
214 struct mac_policy_list_head mac_policy_list;
215 struct mac_policy_list_head mac_static_policy_list;
216 u_int mac_policy_count;			/* Registered policy count. */
217 
218 static void	mac_policy_xlock(void);
219 static void	mac_policy_xlock_assert(void);
220 static void	mac_policy_xunlock(void);
221 
222 void
223 mac_policy_slock_nosleep(struct rm_priotracker *tracker)
224 {
225 
226 #ifndef MAC_STATIC
227 	if (!mac_late)
228 		return;
229 
230 	rm_rlock(&mac_policy_rm, tracker);
231 #endif
232 }
233 
234 void
235 mac_policy_slock_sleep(void)
236 {
237 
238 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
239  	    "mac_policy_slock_sleep");
240 
241 #ifndef MAC_STATIC
242 	if (!mac_late)
243 		return;
244 
245 	rms_rlock(&mac_policy_rms);
246 #endif
247 }
248 
249 void
250 mac_policy_sunlock_nosleep(struct rm_priotracker *tracker)
251 {
252 
253 #ifndef MAC_STATIC
254 	if (!mac_late)
255 		return;
256 
257 	rm_runlock(&mac_policy_rm, tracker);
258 #endif
259 }
260 
261 void
262 mac_policy_sunlock_sleep(void)
263 {
264 
265 #ifndef MAC_STATIC
266 	if (!mac_late)
267 		return;
268 
269 	rms_runlock(&mac_policy_rms);
270 #endif
271 }
272 
273 static void
274 mac_policy_xlock(void)
275 {
276 
277 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
278  	    "mac_policy_xlock()");
279 
280 #ifndef MAC_STATIC
281 	if (!mac_late)
282 		return;
283 
284 	rms_wlock(&mac_policy_rms);
285 	rm_wlock(&mac_policy_rm);
286 #endif
287 }
288 
289 static void
290 mac_policy_xunlock(void)
291 {
292 
293 #ifndef MAC_STATIC
294 	if (!mac_late)
295 		return;
296 
297 	rm_wunlock(&mac_policy_rm);
298 	rms_wunlock(&mac_policy_rms);
299 #endif
300 }
301 
302 static void
303 mac_policy_xlock_assert(void)
304 {
305 
306 #ifndef MAC_STATIC
307 	if (!mac_late)
308 		return;
309 
310 	rm_assert(&mac_policy_rm, RA_WLOCKED);
311 #endif
312 }
313 
314 /*
315  * Initialize the MAC subsystem, including appropriate SMP locks.
316  */
317 static void
318 mac_init(void)
319 {
320 
321 	LIST_INIT(&mac_static_policy_list);
322 	LIST_INIT(&mac_policy_list);
323 	mac_labelzone_init();
324 
325 #ifndef MAC_STATIC
326 	rm_init_flags(&mac_policy_rm, "mac_policy_rm", RM_NOWITNESS |
327 	    RM_RECURSE);
328 	rms_init(&mac_policy_rms, "mac_policy_rms");
329 #endif
330 }
331 
332 /*
333  * For the purposes of modules that want to know if they were loaded "early",
334  * set the mac_late flag once we've processed modules either linked into the
335  * kernel, or loaded before the kernel startup.
336  */
337 static void
338 mac_late_init(void)
339 {
340 
341 	mac_late = 1;
342 }
343 
344 /*
345  * Given a policy, derive from its set of non-NULL label init methods what
346  * object types the policy is interested in.
347  */
348 static uint64_t
349 mac_policy_getlabeled(struct mac_policy_conf *mpc)
350 {
351 	uint64_t labeled;
352 
353 #define	MPC_FLAG(method, flag)					\
354 	if (mpc->mpc_ops->mpo_ ## method != NULL)			\
355 		labeled |= (flag);					\
356 
357 	labeled = 0;
358 	MPC_FLAG(cred_init_label, MPC_OBJECT_CRED);
359 	MPC_FLAG(proc_init_label, MPC_OBJECT_PROC);
360 	MPC_FLAG(vnode_init_label, MPC_OBJECT_VNODE);
361 	MPC_FLAG(inpcb_init_label, MPC_OBJECT_INPCB);
362 	MPC_FLAG(socket_init_label, MPC_OBJECT_SOCKET);
363 	MPC_FLAG(devfs_init_label, MPC_OBJECT_DEVFS);
364 	MPC_FLAG(mbuf_init_label, MPC_OBJECT_MBUF);
365 	MPC_FLAG(ipq_init_label, MPC_OBJECT_IPQ);
366 	MPC_FLAG(ifnet_init_label, MPC_OBJECT_IFNET);
367 	MPC_FLAG(bpfdesc_init_label, MPC_OBJECT_BPFDESC);
368 	MPC_FLAG(pipe_init_label, MPC_OBJECT_PIPE);
369 	MPC_FLAG(mount_init_label, MPC_OBJECT_MOUNT);
370 	MPC_FLAG(posixsem_init_label, MPC_OBJECT_POSIXSEM);
371 	MPC_FLAG(posixshm_init_label, MPC_OBJECT_POSIXSHM);
372 	MPC_FLAG(sysvmsg_init_label, MPC_OBJECT_SYSVMSG);
373 	MPC_FLAG(sysvmsq_init_label, MPC_OBJECT_SYSVMSQ);
374 	MPC_FLAG(sysvsem_init_label, MPC_OBJECT_SYSVSEM);
375 	MPC_FLAG(sysvshm_init_label, MPC_OBJECT_SYSVSHM);
376 	MPC_FLAG(syncache_init_label, MPC_OBJECT_SYNCACHE);
377 	MPC_FLAG(ip6q_init_label, MPC_OBJECT_IP6Q);
378 
379 #undef MPC_FLAG
380 	return (labeled);
381 }
382 
383 /*
384  * When policies are loaded or unloaded, walk the list of registered policies
385  * and built mac_labeled, a bitmask representing the union of all objects
386  * requiring labels across all policies.
387  */
388 static void
389 mac_policy_update(void)
390 {
391 	struct mac_policy_conf *mpc;
392 
393 	mac_policy_xlock_assert();
394 
395 	mac_labeled = 0;
396 	mac_policy_count = 0;
397 	LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {
398 		mac_labeled |= mac_policy_getlabeled(mpc);
399 		mac_policy_count++;
400 	}
401 	LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {
402 		mac_labeled |= mac_policy_getlabeled(mpc);
403 		mac_policy_count++;
404 	}
405 
406 	cache_fast_lookup_enabled_recalc();
407 }
408 
409 /*
410  * There are frequently used code paths which check for rarely installed
411  * policies. Gross hack below enables doing it in a cheap manner.
412  */
413 
414 #define FPO(f)	(offsetof(struct mac_policy_ops, mpo_##f) / sizeof(uintptr_t))
415 
416 struct mac_policy_fastpath_elem {
417 	int	count;
418 	bool	*flag;
419 	size_t	offset;
420 };
421 
422 struct mac_policy_fastpath_elem mac_policy_fastpath_array[] = {
423 	{ .offset = FPO(priv_check), .flag = &mac_priv_check_fp_flag },
424 	{ .offset = FPO(priv_grant), .flag = &mac_priv_grant_fp_flag },
425 	{ .offset = FPO(vnode_check_lookup),
426 		.flag = &mac_vnode_check_lookup_fp_flag },
427 	{ .offset = FPO(vnode_check_readlink),
428 		.flag = &mac_vnode_check_readlink_fp_flag },
429 	{ .offset = FPO(vnode_check_open),
430 		.flag = &mac_vnode_check_open_fp_flag },
431 	{ .offset = FPO(vnode_check_stat),
432 		.flag = &mac_vnode_check_stat_fp_flag },
433 	{ .offset = FPO(vnode_check_read),
434 		.flag = &mac_vnode_check_read_fp_flag },
435 	{ .offset = FPO(vnode_check_write),
436 		.flag = &mac_vnode_check_write_fp_flag },
437 	{ .offset = FPO(vnode_check_mmap),
438 		.flag = &mac_vnode_check_mmap_fp_flag },
439 	{ .offset = FPO(vnode_check_poll),
440 		.flag = &mac_vnode_check_poll_fp_flag },
441 	{ .offset = FPO(vnode_check_rename_from),
442 		.flag = &mac_vnode_check_rename_from_fp_flag },
443 	{ .offset = FPO(vnode_check_access),
444 		.flag = &mac_vnode_check_access_fp_flag },
445 	{ .offset = FPO(pipe_check_stat),
446 		.flag = &mac_pipe_check_stat_fp_flag },
447 	{ .offset = FPO(pipe_check_poll),
448 		.flag = &mac_pipe_check_poll_fp_flag },
449 	{ .offset = FPO(pipe_check_read),
450 		.flag = &mac_pipe_check_read_fp_flag },
451 	{ .offset = FPO(ifnet_create_mbuf),
452 		.flag = &mac_ifnet_create_mbuf_fp_flag },
453 	{ .offset = FPO(ifnet_check_transmit),
454 		.flag = &mac_ifnet_check_transmit_fp_flag },
455 };
456 
457 static void
458 mac_policy_fastpath_enable(struct mac_policy_fastpath_elem *mpfe)
459 {
460 
461 	MPASS(mpfe->count >= 0);
462 	mpfe->count++;
463 	if (mpfe->count == 1) {
464 		MPASS(*mpfe->flag == false);
465 		*mpfe->flag = true;
466 	}
467 }
468 
469 static void
470 mac_policy_fastpath_disable(struct mac_policy_fastpath_elem *mpfe)
471 {
472 
473 	MPASS(mpfe->count >= 1);
474 	mpfe->count--;
475 	if (mpfe->count == 0) {
476 		MPASS(*mpfe->flag == true);
477 		*mpfe->flag = false;
478 	}
479 }
480 
481 static void
482 mac_policy_fastpath_register(struct mac_policy_conf *mpc)
483 {
484 	struct mac_policy_fastpath_elem *mpfe;
485 	uintptr_t **ops;
486 	int i;
487 
488 	mac_policy_xlock_assert();
489 
490 	ops = (uintptr_t **)mpc->mpc_ops;
491 	for (i = 0; i < nitems(mac_policy_fastpath_array); i++) {
492 		mpfe = &mac_policy_fastpath_array[i];
493 		if (ops[mpfe->offset] != NULL)
494 			mac_policy_fastpath_enable(mpfe);
495 	}
496 }
497 
498 static void
499 mac_policy_fastpath_unregister(struct mac_policy_conf *mpc)
500 {
501 	struct mac_policy_fastpath_elem *mpfe;
502 	uintptr_t **ops;
503 	int i;
504 
505 	mac_policy_xlock_assert();
506 
507 	ops = (uintptr_t **)mpc->mpc_ops;
508 	for (i = 0; i < nitems(mac_policy_fastpath_array); i++) {
509 		mpfe = &mac_policy_fastpath_array[i];
510 		if (ops[mpfe->offset] != NULL)
511 			mac_policy_fastpath_disable(mpfe);
512 	}
513 }
514 
515 #undef FPO
516 
517 static int
518 mac_policy_register(struct mac_policy_conf *mpc)
519 {
520 	struct mac_policy_list_head *mpc_list;
521 	struct mac_policy_conf *last_mpc, *tmpc;
522 	int error, slot, static_entry;
523 
524 	error = 0;
525 
526 	/*
527 	 * We don't technically need exclusive access while !mac_late, but
528 	 * hold it for assertion consistency.
529 	 */
530 	mac_policy_xlock();
531 
532 	/*
533 	 * If the module can potentially be unloaded, or we're loading late,
534 	 * we have to stick it in the non-static list and pay an extra
535 	 * performance overhead.  Otherwise, we can pay a light locking cost
536 	 * and stick it in the static list.
537 	 */
538 	static_entry = (!mac_late &&
539 	    !(mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK));
540 
541 	mpc_list = (static_entry) ? &mac_static_policy_list :
542 	    &mac_policy_list;
543 	last_mpc = NULL;
544 	LIST_FOREACH(tmpc, mpc_list, mpc_list) {
545 		last_mpc = tmpc;
546 		if (strcmp(tmpc->mpc_name, mpc->mpc_name) == 0) {
547 			error = EEXIST;
548 			goto out;
549 		}
550 	}
551 	if (mpc->mpc_field_off != NULL) {
552 		slot = ffs(mac_slot_offsets_free);
553 		if (slot == 0) {
554 			error = ENOMEM;
555 			goto out;
556 		}
557 		slot--;
558 		mac_slot_offsets_free &= ~(1 << slot);
559 		*mpc->mpc_field_off = slot;
560 	}
561 	mpc->mpc_runtime_flags |= MPC_RUNTIME_FLAG_REGISTERED;
562 
563 	/*
564 	 * Some modules may depend on the operations of its dependencies.
565 	 * Inserting modules in order of registration ensures operations
566 	 * that work on the module list retain dependency order.
567 	 */
568 	if (last_mpc == NULL)
569 		LIST_INSERT_HEAD(mpc_list, mpc, mpc_list);
570 	else
571 		LIST_INSERT_AFTER(last_mpc, mpc, mpc_list);
572 	/*
573 	 * Per-policy initialization.  Currently, this takes place under the
574 	 * exclusive lock, so policies must not sleep in their init method.
575 	 * In the future, we may want to separate "init" from "start", with
576 	 * "init" occurring without the lock held.  Likewise, on tear-down,
577 	 * breaking out "stop" from "destroy".
578 	 */
579 	if (mpc->mpc_ops->mpo_init != NULL)
580 		(*(mpc->mpc_ops->mpo_init))(mpc);
581 
582 	mac_policy_fastpath_register(mpc);
583 
584 	mac_policy_update();
585 
586 	SDT_PROBE1(mac, , policy, register, mpc);
587 	printf("Security policy loaded: %s (%s)\n", mpc->mpc_fullname,
588 	    mpc->mpc_name);
589 
590 out:
591 	mac_policy_xunlock();
592 	return (error);
593 }
594 
595 static int
596 mac_policy_unregister(struct mac_policy_conf *mpc)
597 {
598 
599 	/*
600 	 * If we fail the load, we may get a request to unload.  Check to see
601 	 * if we did the run-time registration, and if not, silently succeed.
602 	 */
603 	mac_policy_xlock();
604 	if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED) == 0) {
605 		mac_policy_xunlock();
606 		return (0);
607 	}
608 #if 0
609 	/*
610 	 * Don't allow unloading modules with private data.
611 	 */
612 	if (mpc->mpc_field_off != NULL) {
613 		mac_policy_xunlock();
614 		return (EBUSY);
615 	}
616 #endif
617 	/*
618 	 * Only allow the unload to proceed if the module is unloadable by
619 	 * its own definition.
620 	 */
621 	if ((mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK) == 0) {
622 		mac_policy_xunlock();
623 		return (EBUSY);
624 	}
625 
626 	mac_policy_fastpath_unregister(mpc);
627 
628 	if (mpc->mpc_ops->mpo_destroy != NULL)
629 		(*(mpc->mpc_ops->mpo_destroy))(mpc);
630 
631 	LIST_REMOVE(mpc, mpc_list);
632 	mpc->mpc_runtime_flags &= ~MPC_RUNTIME_FLAG_REGISTERED;
633 	mac_policy_update();
634 	mac_policy_xunlock();
635 
636 	SDT_PROBE1(mac, , policy, unregister, mpc);
637 	printf("Security policy unload: %s (%s)\n", mpc->mpc_fullname,
638 	    mpc->mpc_name);
639 
640 	return (0);
641 }
642 
643 /*
644  * Allow MAC policy modules to register during boot, etc.
645  */
646 int
647 mac_policy_modevent(module_t mod, int type, void *data)
648 {
649 	struct mac_policy_conf *mpc;
650 	int error;
651 
652 	error = 0;
653 	mpc = (struct mac_policy_conf *) data;
654 
655 #ifdef MAC_STATIC
656 	if (mac_late) {
657 		printf("mac_policy_modevent: MAC_STATIC and late\n");
658 		return (EBUSY);
659 	}
660 #endif
661 
662 	SDT_PROBE2(mac, , policy, modevent, type, mpc);
663 	switch (type) {
664 	case MOD_LOAD:
665 		if (mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_NOTLATE &&
666 		    mac_late) {
667 			printf("mac_policy_modevent: can't load %s policy "
668 			    "after booting\n", mpc->mpc_name);
669 			error = EBUSY;
670 			break;
671 		}
672 		error = mac_policy_register(mpc);
673 		break;
674 	case MOD_UNLOAD:
675 		/* Don't unregister the module if it was never registered. */
676 		if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED)
677 		    != 0)
678 			error = mac_policy_unregister(mpc);
679 		else
680 			error = 0;
681 		break;
682 	default:
683 		error = EOPNOTSUPP;
684 		break;
685 	}
686 
687 	return (error);
688 }
689 
690 /*
691  * Define an error value precedence, and given two arguments, selects the
692  * value with the higher precedence.
693  */
694 int
695 mac_error_select(int error1, int error2)
696 {
697 
698 	/* Certain decision-making errors take top priority. */
699 	if (error1 == EDEADLK || error2 == EDEADLK)
700 		return (EDEADLK);
701 
702 	/* Invalid arguments should be reported where possible. */
703 	if (error1 == EINVAL || error2 == EINVAL)
704 		return (EINVAL);
705 
706 	/* Precedence goes to "visibility", with both process and file. */
707 	if (error1 == ESRCH || error2 == ESRCH)
708 		return (ESRCH);
709 
710 	if (error1 == ENOENT || error2 == ENOENT)
711 		return (ENOENT);
712 
713 	/* Precedence goes to DAC/MAC protections. */
714 	if (error1 == EACCES || error2 == EACCES)
715 		return (EACCES);
716 
717 	/* Precedence goes to privilege. */
718 	if (error1 == EPERM || error2 == EPERM)
719 		return (EPERM);
720 
721 	/* Precedence goes to error over success; otherwise, arbitrary. */
722 	if (error1 != 0)
723 		return (error1);
724 	return (error2);
725 }
726 
727 int
728 mac_check_structmac_consistent(struct mac *mac)
729 {
730 
731 	/* Require that labels have a non-zero length. */
732 	if (mac->m_buflen > MAC_MAX_LABEL_BUF_LEN ||
733 	    mac->m_buflen <= sizeof(""))
734 		return (EINVAL);
735 
736 	return (0);
737 }
738 
739 SYSINIT(mac, SI_SUB_MAC, SI_ORDER_FIRST, mac_init, NULL);
740 SYSINIT(mac_late, SI_SUB_MAC_LATE, SI_ORDER_FIRST, mac_late_init, NULL);
741