xref: /freebsd/sys/security/mac/mac_framework.c (revision c697fb7f)
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 __FBSDID("$FreeBSD$");
73 
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/condvar.h>
77 #include <sys/kernel.h>
78 #include <sys/lock.h>
79 #include <sys/mac.h>
80 #include <sys/module.h>
81 #include <sys/rmlock.h>
82 #include <sys/sdt.h>
83 #include <sys/sx.h>
84 #include <sys/sysctl.h>
85 
86 #include <security/mac/mac_framework.h>
87 #include <security/mac/mac_internal.h>
88 #include <security/mac/mac_policy.h>
89 
90 /*
91  * DTrace SDT providers for MAC.
92  */
93 SDT_PROVIDER_DEFINE(mac);
94 SDT_PROVIDER_DEFINE(mac_framework);
95 
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 *");
102 
103 /*
104  * Root sysctl node for all MAC and MAC policy controls.
105  */
106 SYSCTL_NODE(_security, OID_AUTO, mac, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
107     "TrustedBSD MAC policy controls");
108 
109 /*
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.
113  */
114 MODULE_VERSION(kernel_mac_support, MAC_VERSION);
115 
116 static unsigned int	mac_version = MAC_VERSION;
117 SYSCTL_UINT(_security_mac, OID_AUTO, version, CTLFLAG_RD, &mac_version, 0,
118     "");
119 
120 /*
121  * Flags for inlined checks.
122  */
123 #define FPFLAG(f)	\
124 bool __read_frequently mac_##f##_fp_flag
125 
126 FPFLAG(priv_check);
127 FPFLAG(priv_grant);
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);
134 
135 #undef FPFLAG
136 
137 /*
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.
146  */
147 #if MAC_MAX_SLOTS > 32
148 #error "MAC_MAX_SLOTS too large"
149 #endif
150 
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,
154     0, "");
155 
156 /*
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.
160  */
161 static int	mac_late = 0;
162 
163 /*
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.
168  */
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");
172 
173 MALLOC_DEFINE(M_MACTEMP, "mactemp", "MAC temporary label storage");
174 
175 /*
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.
183  *
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_sx, is acquire 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.
192  */
193 #ifndef MAC_STATIC
194 static struct rmlock mac_policy_rm;	/* Non-sleeping entry points. */
195 static struct sx mac_policy_sx;		/* Sleeping entry points. */
196 static struct rmslock mac_policy_rms;
197 #endif
198 
199 struct mac_policy_list_head mac_policy_list;
200 struct mac_policy_list_head mac_static_policy_list;
201 u_int mac_policy_count;			/* Registered policy count. */
202 
203 static void	mac_policy_xlock(void);
204 static void	mac_policy_xlock_assert(void);
205 static void	mac_policy_xunlock(void);
206 
207 void
208 mac_policy_slock_nosleep(struct rm_priotracker *tracker)
209 {
210 
211 #ifndef MAC_STATIC
212 	if (!mac_late)
213 		return;
214 
215 	rm_rlock(&mac_policy_rm, tracker);
216 #endif
217 }
218 
219 void
220 mac_policy_slock_sleep(void)
221 {
222 
223 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
224  	    "mac_policy_slock_sleep");
225 
226 #ifndef MAC_STATIC
227 	if (!mac_late)
228 		return;
229 
230 	rms_rlock(&mac_policy_rms);
231 #endif
232 }
233 
234 void
235 mac_policy_sunlock_nosleep(struct rm_priotracker *tracker)
236 {
237 
238 #ifndef MAC_STATIC
239 	if (!mac_late)
240 		return;
241 
242 	rm_runlock(&mac_policy_rm, tracker);
243 #endif
244 }
245 
246 void
247 mac_policy_sunlock_sleep(void)
248 {
249 
250 #ifndef MAC_STATIC
251 	if (!mac_late)
252 		return;
253 
254 	rms_runlock(&mac_policy_rms);
255 #endif
256 }
257 
258 static void
259 mac_policy_xlock(void)
260 {
261 
262 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
263  	    "mac_policy_xlock()");
264 
265 #ifndef MAC_STATIC
266 	if (!mac_late)
267 		return;
268 
269 	sx_xlock(&mac_policy_sx);
270 	rms_wlock(&mac_policy_rms);
271 	rm_wlock(&mac_policy_rm);
272 #endif
273 }
274 
275 static void
276 mac_policy_xunlock(void)
277 {
278 
279 #ifndef MAC_STATIC
280 	if (!mac_late)
281 		return;
282 
283 	rm_wunlock(&mac_policy_rm);
284 	rms_wunlock(&mac_policy_rms);
285 	sx_xunlock(&mac_policy_sx);
286 #endif
287 }
288 
289 static void
290 mac_policy_xlock_assert(void)
291 {
292 
293 #ifndef MAC_STATIC
294 	if (!mac_late)
295 		return;
296 
297 	/* XXXRW: rm_assert(&mac_policy_rm, RA_WLOCKED); */
298 	sx_assert(&mac_policy_sx, SA_XLOCKED);
299 #endif
300 }
301 
302 /*
303  * Initialize the MAC subsystem, including appropriate SMP locks.
304  */
305 static void
306 mac_init(void)
307 {
308 
309 	LIST_INIT(&mac_static_policy_list);
310 	LIST_INIT(&mac_policy_list);
311 	mac_labelzone_init();
312 
313 #ifndef MAC_STATIC
314 	rm_init_flags(&mac_policy_rm, "mac_policy_rm", RM_NOWITNESS |
315 	    RM_RECURSE);
316 	sx_init_flags(&mac_policy_sx, "mac_policy_sx", SX_NOWITNESS);
317 	rms_init(&mac_policy_rms, "mac_policy_rms");
318 #endif
319 }
320 
321 /*
322  * For the purposes of modules that want to know if they were loaded "early",
323  * set the mac_late flag once we've processed modules either linked into the
324  * kernel, or loaded before the kernel startup.
325  */
326 static void
327 mac_late_init(void)
328 {
329 
330 	mac_late = 1;
331 }
332 
333 /*
334  * Given a policy, derive from its set of non-NULL label init methods what
335  * object types the policy is interested in.
336  */
337 static uint64_t
338 mac_policy_getlabeled(struct mac_policy_conf *mpc)
339 {
340 	uint64_t labeled;
341 
342 #define	MPC_FLAG(method, flag)					\
343 	if (mpc->mpc_ops->mpo_ ## method != NULL)			\
344 		labeled |= (flag);					\
345 
346 	labeled = 0;
347 	MPC_FLAG(cred_init_label, MPC_OBJECT_CRED);
348 	MPC_FLAG(proc_init_label, MPC_OBJECT_PROC);
349 	MPC_FLAG(vnode_init_label, MPC_OBJECT_VNODE);
350 	MPC_FLAG(inpcb_init_label, MPC_OBJECT_INPCB);
351 	MPC_FLAG(socket_init_label, MPC_OBJECT_SOCKET);
352 	MPC_FLAG(devfs_init_label, MPC_OBJECT_DEVFS);
353 	MPC_FLAG(mbuf_init_label, MPC_OBJECT_MBUF);
354 	MPC_FLAG(ipq_init_label, MPC_OBJECT_IPQ);
355 	MPC_FLAG(ifnet_init_label, MPC_OBJECT_IFNET);
356 	MPC_FLAG(bpfdesc_init_label, MPC_OBJECT_BPFDESC);
357 	MPC_FLAG(pipe_init_label, MPC_OBJECT_PIPE);
358 	MPC_FLAG(mount_init_label, MPC_OBJECT_MOUNT);
359 	MPC_FLAG(posixsem_init_label, MPC_OBJECT_POSIXSEM);
360 	MPC_FLAG(posixshm_init_label, MPC_OBJECT_POSIXSHM);
361 	MPC_FLAG(sysvmsg_init_label, MPC_OBJECT_SYSVMSG);
362 	MPC_FLAG(sysvmsq_init_label, MPC_OBJECT_SYSVMSQ);
363 	MPC_FLAG(sysvsem_init_label, MPC_OBJECT_SYSVSEM);
364 	MPC_FLAG(sysvshm_init_label, MPC_OBJECT_SYSVSHM);
365 	MPC_FLAG(syncache_init_label, MPC_OBJECT_SYNCACHE);
366 	MPC_FLAG(ip6q_init_label, MPC_OBJECT_IP6Q);
367 
368 #undef MPC_FLAG
369 	return (labeled);
370 }
371 
372 /*
373  * When policies are loaded or unloaded, walk the list of registered policies
374  * and built mac_labeled, a bitmask representing the union of all objects
375  * requiring labels across all policies.
376  */
377 static void
378 mac_policy_update(void)
379 {
380 	struct mac_policy_conf *mpc;
381 
382 	mac_policy_xlock_assert();
383 
384 	mac_labeled = 0;
385 	mac_policy_count = 0;
386 	LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) {
387 		mac_labeled |= mac_policy_getlabeled(mpc);
388 		mac_policy_count++;
389 	}
390 	LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {
391 		mac_labeled |= mac_policy_getlabeled(mpc);
392 		mac_policy_count++;
393 	}
394 }
395 
396 /*
397  * There are frequently used code paths which check for rarely installed
398  * policies. Gross hack below enables doing it in a cheap manner.
399  */
400 
401 #define FPO(f)	(offsetof(struct mac_policy_ops, mpo_##f) / sizeof(uintptr_t))
402 
403 struct mac_policy_fastpath_elem {
404 	int	count;
405 	bool	*flag;
406 	size_t	offset;
407 };
408 
409 struct mac_policy_fastpath_elem mac_policy_fastpath_array[] = {
410 	{ .offset = FPO(priv_check), .flag = &mac_priv_check_fp_flag },
411 	{ .offset = FPO(priv_grant), .flag = &mac_priv_grant_fp_flag },
412 	{ .offset = FPO(vnode_check_lookup),
413 		.flag = &mac_vnode_check_lookup_fp_flag },
414 	{ .offset = FPO(vnode_check_open),
415 		.flag = &mac_vnode_check_open_fp_flag },
416 	{ .offset = FPO(vnode_check_stat),
417 		.flag = &mac_vnode_check_stat_fp_flag },
418 	{ .offset = FPO(vnode_check_read),
419 		.flag = &mac_vnode_check_read_fp_flag },
420 	{ .offset = FPO(vnode_check_write),
421 		.flag = &mac_vnode_check_write_fp_flag },
422 	{ .offset = FPO(vnode_check_mmap),
423 		.flag = &mac_vnode_check_mmap_fp_flag },
424 };
425 
426 static void
427 mac_policy_fastpath_enable(struct mac_policy_fastpath_elem *mpfe)
428 {
429 
430 	MPASS(mpfe->count >= 0);
431 	mpfe->count++;
432 	if (mpfe->count == 1) {
433 		MPASS(*mpfe->flag == false);
434 		*mpfe->flag = true;
435 	}
436 }
437 
438 static void
439 mac_policy_fastpath_disable(struct mac_policy_fastpath_elem *mpfe)
440 {
441 
442 	MPASS(mpfe->count >= 1);
443 	mpfe->count--;
444 	if (mpfe->count == 0) {
445 		MPASS(*mpfe->flag == true);
446 		*mpfe->flag = false;
447 	}
448 }
449 
450 static void
451 mac_policy_fastpath_register(struct mac_policy_conf *mpc)
452 {
453 	struct mac_policy_fastpath_elem *mpfe;
454 	uintptr_t **ops;
455 	int i;
456 
457 	mac_policy_xlock_assert();
458 
459 	ops = (uintptr_t **)mpc->mpc_ops;
460 	for (i = 0; i < nitems(mac_policy_fastpath_array); i++) {
461 		mpfe = &mac_policy_fastpath_array[i];
462 		if (ops[mpfe->offset] != NULL)
463 			mac_policy_fastpath_enable(mpfe);
464 	}
465 }
466 
467 static void
468 mac_policy_fastpath_unregister(struct mac_policy_conf *mpc)
469 {
470 	struct mac_policy_fastpath_elem *mpfe;
471 	uintptr_t **ops;
472 	int i;
473 
474 	mac_policy_xlock_assert();
475 
476 	ops = (uintptr_t **)mpc->mpc_ops;
477 	for (i = 0; i < nitems(mac_policy_fastpath_array); i++) {
478 		mpfe = &mac_policy_fastpath_array[i];
479 		if (ops[mpfe->offset] != NULL)
480 			mac_policy_fastpath_disable(mpfe);
481 	}
482 }
483 
484 #undef FPO
485 
486 static int
487 mac_policy_register(struct mac_policy_conf *mpc)
488 {
489 	struct mac_policy_conf *tmpc;
490 	int error, slot, static_entry;
491 
492 	error = 0;
493 
494 	/*
495 	 * We don't technically need exclusive access while !mac_late, but
496 	 * hold it for assertion consistency.
497 	 */
498 	mac_policy_xlock();
499 
500 	/*
501 	 * If the module can potentially be unloaded, or we're loading late,
502 	 * we have to stick it in the non-static list and pay an extra
503 	 * performance overhead.  Otherwise, we can pay a light locking cost
504 	 * and stick it in the static list.
505 	 */
506 	static_entry = (!mac_late &&
507 	    !(mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK));
508 
509 	if (static_entry) {
510 		LIST_FOREACH(tmpc, &mac_static_policy_list, mpc_list) {
511 			if (strcmp(tmpc->mpc_name, mpc->mpc_name) == 0) {
512 				error = EEXIST;
513 				goto out;
514 			}
515 		}
516 	} else {
517 		LIST_FOREACH(tmpc, &mac_policy_list, mpc_list) {
518 			if (strcmp(tmpc->mpc_name, mpc->mpc_name) == 0) {
519 				error = EEXIST;
520 				goto out;
521 			}
522 		}
523 	}
524 	if (mpc->mpc_field_off != NULL) {
525 		slot = ffs(mac_slot_offsets_free);
526 		if (slot == 0) {
527 			error = ENOMEM;
528 			goto out;
529 		}
530 		slot--;
531 		mac_slot_offsets_free &= ~(1 << slot);
532 		*mpc->mpc_field_off = slot;
533 	}
534 	mpc->mpc_runtime_flags |= MPC_RUNTIME_FLAG_REGISTERED;
535 
536 	/*
537 	 * If we're loading a MAC module after the framework has initialized,
538 	 * it has to go into the dynamic list.  If we're loading it before
539 	 * we've finished initializing, it can go into the static list with
540 	 * weaker locker requirements.
541 	 */
542 	if (static_entry)
543 		LIST_INSERT_HEAD(&mac_static_policy_list, mpc, mpc_list);
544 	else
545 		LIST_INSERT_HEAD(&mac_policy_list, mpc, mpc_list);
546 
547 	/*
548 	 * Per-policy initialization.  Currently, this takes place under the
549 	 * exclusive lock, so policies must not sleep in their init method.
550 	 * In the future, we may want to separate "init" from "start", with
551 	 * "init" occurring without the lock held.  Likewise, on tear-down,
552 	 * breaking out "stop" from "destroy".
553 	 */
554 	if (mpc->mpc_ops->mpo_init != NULL)
555 		(*(mpc->mpc_ops->mpo_init))(mpc);
556 
557 	mac_policy_fastpath_register(mpc);
558 
559 	mac_policy_update();
560 
561 	SDT_PROBE1(mac, , policy, register, mpc);
562 	printf("Security policy loaded: %s (%s)\n", mpc->mpc_fullname,
563 	    mpc->mpc_name);
564 
565 out:
566 	mac_policy_xunlock();
567 	return (error);
568 }
569 
570 static int
571 mac_policy_unregister(struct mac_policy_conf *mpc)
572 {
573 
574 	/*
575 	 * If we fail the load, we may get a request to unload.  Check to see
576 	 * if we did the run-time registration, and if not, silently succeed.
577 	 */
578 	mac_policy_xlock();
579 	if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED) == 0) {
580 		mac_policy_xunlock();
581 		return (0);
582 	}
583 #if 0
584 	/*
585 	 * Don't allow unloading modules with private data.
586 	 */
587 	if (mpc->mpc_field_off != NULL) {
588 		mac_policy_xunlock();
589 		return (EBUSY);
590 	}
591 #endif
592 	/*
593 	 * Only allow the unload to proceed if the module is unloadable by
594 	 * its own definition.
595 	 */
596 	if ((mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK) == 0) {
597 		mac_policy_xunlock();
598 		return (EBUSY);
599 	}
600 
601 	mac_policy_fastpath_unregister(mpc);
602 
603 	if (mpc->mpc_ops->mpo_destroy != NULL)
604 		(*(mpc->mpc_ops->mpo_destroy))(mpc);
605 
606 	LIST_REMOVE(mpc, mpc_list);
607 	mpc->mpc_runtime_flags &= ~MPC_RUNTIME_FLAG_REGISTERED;
608 	mac_policy_update();
609 	mac_policy_xunlock();
610 
611 	SDT_PROBE1(mac, , policy, unregister, mpc);
612 	printf("Security policy unload: %s (%s)\n", mpc->mpc_fullname,
613 	    mpc->mpc_name);
614 
615 	return (0);
616 }
617 
618 /*
619  * Allow MAC policy modules to register during boot, etc.
620  */
621 int
622 mac_policy_modevent(module_t mod, int type, void *data)
623 {
624 	struct mac_policy_conf *mpc;
625 	int error;
626 
627 	error = 0;
628 	mpc = (struct mac_policy_conf *) data;
629 
630 #ifdef MAC_STATIC
631 	if (mac_late) {
632 		printf("mac_policy_modevent: MAC_STATIC and late\n");
633 		return (EBUSY);
634 	}
635 #endif
636 
637 	SDT_PROBE2(mac, , policy, modevent, type, mpc);
638 	switch (type) {
639 	case MOD_LOAD:
640 		if (mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_NOTLATE &&
641 		    mac_late) {
642 			printf("mac_policy_modevent: can't load %s policy "
643 			    "after booting\n", mpc->mpc_name);
644 			error = EBUSY;
645 			break;
646 		}
647 		error = mac_policy_register(mpc);
648 		break;
649 	case MOD_UNLOAD:
650 		/* Don't unregister the module if it was never registered. */
651 		if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED)
652 		    != 0)
653 			error = mac_policy_unregister(mpc);
654 		else
655 			error = 0;
656 		break;
657 	default:
658 		error = EOPNOTSUPP;
659 		break;
660 	}
661 
662 	return (error);
663 }
664 
665 /*
666  * Define an error value precedence, and given two arguments, selects the
667  * value with the higher precedence.
668  */
669 int
670 mac_error_select(int error1, int error2)
671 {
672 
673 	/* Certain decision-making errors take top priority. */
674 	if (error1 == EDEADLK || error2 == EDEADLK)
675 		return (EDEADLK);
676 
677 	/* Invalid arguments should be reported where possible. */
678 	if (error1 == EINVAL || error2 == EINVAL)
679 		return (EINVAL);
680 
681 	/* Precedence goes to "visibility", with both process and file. */
682 	if (error1 == ESRCH || error2 == ESRCH)
683 		return (ESRCH);
684 
685 	if (error1 == ENOENT || error2 == ENOENT)
686 		return (ENOENT);
687 
688 	/* Precedence goes to DAC/MAC protections. */
689 	if (error1 == EACCES || error2 == EACCES)
690 		return (EACCES);
691 
692 	/* Precedence goes to privilege. */
693 	if (error1 == EPERM || error2 == EPERM)
694 		return (EPERM);
695 
696 	/* Precedence goes to error over success; otherwise, arbitrary. */
697 	if (error1 != 0)
698 		return (error1);
699 	return (error2);
700 }
701 
702 int
703 mac_check_structmac_consistent(struct mac *mac)
704 {
705 
706 	/* Require that labels have a non-zero length. */
707 	if (mac->m_buflen > MAC_MAX_LABEL_BUF_LEN ||
708 	    mac->m_buflen <= sizeof(""))
709 		return (EINVAL);
710 
711 	return (0);
712 }
713 
714 SYSINIT(mac, SI_SUB_MAC, SI_ORDER_FIRST, mac_init, NULL);
715 SYSINIT(mac_late, SI_SUB_MAC_LATE, SI_ORDER_FIRST, mac_late_init, NULL);
716