xref: /freebsd/sys/security/mac_lomac/mac_lomac.c (revision a4128aad8503277614f2d214011ef60a19447b83)
1 /*-
2  * Copyright (c) 1999-2002, 2007-2009 Robert N. M. Watson
3  * Copyright (c) 2001-2005 Networks Associates Technology, Inc.
4  * Copyright (c) 2006 SPARTA, Inc.
5  * All rights reserved.
6  *
7  * This software was developed by Robert Watson for the TrustedBSD Project.
8  *
9  * This software was developed for the FreeBSD Project in part by NAI Labs,
10  * the Security Research Division of Network Associates, Inc. under
11  * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
12  * CHATS research program.
13  *
14  * This software was enhanced by SPARTA ISSO under SPAWAR contract
15  * N66001-04-C-6019 ("SEFOS").
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 /*
40  * Developed by the TrustedBSD Project.
41  *
42  * Low-watermark floating label mandatory integrity policy.
43  */
44 
45 #include <sys/types.h>
46 #include <sys/param.h>
47 #include <sys/acl.h>
48 #include <sys/conf.h>
49 #include <sys/extattr.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/mman.h>
53 #include <sys/mount.h>
54 #include <sys/priv.h>
55 #include <sys/proc.h>
56 #include <sys/sbuf.h>
57 #include <sys/systm.h>
58 #include <sys/sysproto.h>
59 #include <sys/sysent.h>
60 #include <sys/systm.h>
61 #include <sys/vnode.h>
62 #include <sys/file.h>
63 #include <sys/socket.h>
64 #include <sys/socketvar.h>
65 #include <sys/sx.h>
66 #include <sys/pipe.h>
67 #include <sys/sysctl.h>
68 #include <sys/syslog.h>
69 
70 #include <fs/devfs/devfs.h>
71 
72 #include <net/bpfdesc.h>
73 #include <net/if.h>
74 #include <net/if_types.h>
75 #include <net/if_var.h>
76 
77 #include <netinet/in.h>
78 #include <netinet/in_pcb.h>
79 #include <netinet/ip_var.h>
80 
81 #include <vm/vm.h>
82 
83 #include <security/mac/mac_policy.h>
84 #include <security/mac/mac_framework.h>
85 #include <security/mac_lomac/mac_lomac.h>
86 
87 struct mac_lomac_proc {
88 	struct mac_lomac mac_lomac;
89 	struct mtx mtx;
90 };
91 
92 SYSCTL_DECL(_security_mac);
93 
94 static SYSCTL_NODE(_security_mac, OID_AUTO, lomac,
95     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
96     "TrustedBSD mac_lomac policy controls");
97 
98 static int	lomac_label_size = sizeof(struct mac_lomac);
99 SYSCTL_INT(_security_mac_lomac, OID_AUTO, label_size, CTLFLAG_RD,
100     &lomac_label_size, 0, "Size of struct mac_lomac");
101 
102 static int	lomac_enabled = 1;
103 SYSCTL_INT(_security_mac_lomac, OID_AUTO, enabled, CTLFLAG_RWTUN,
104     &lomac_enabled, 0, "Enforce MAC/LOMAC policy");
105 
106 static int	destroyed_not_inited;
107 SYSCTL_INT(_security_mac_lomac, OID_AUTO, destroyed_not_inited, CTLFLAG_RD,
108     &destroyed_not_inited, 0, "Count of labels destroyed but not inited");
109 
110 static int	trust_all_interfaces = 0;
111 SYSCTL_INT(_security_mac_lomac, OID_AUTO, trust_all_interfaces, CTLFLAG_RDTUN,
112     &trust_all_interfaces, 0, "Consider all interfaces 'trusted' by MAC/LOMAC");
113 
114 static char	trusted_interfaces[128];
115 SYSCTL_STRING(_security_mac_lomac, OID_AUTO, trusted_interfaces, CTLFLAG_RDTUN,
116     trusted_interfaces, 0, "Interfaces considered 'trusted' by MAC/LOMAC");
117 
118 static int	ptys_equal = 0;
119 SYSCTL_INT(_security_mac_lomac, OID_AUTO, ptys_equal, CTLFLAG_RWTUN,
120     &ptys_equal, 0, "Label pty devices as lomac/equal on create");
121 
122 static int	revocation_enabled = 1;
123 SYSCTL_INT(_security_mac_lomac, OID_AUTO, revocation_enabled, CTLFLAG_RWTUN,
124     &revocation_enabled, 0, "Revoke access to objects on relabel");
125 
126 static int	lomac_slot;
127 #define	SLOT(l)	((struct mac_lomac *)mac_label_get((l), lomac_slot))
128 #define	SLOT_SET(l, val) mac_label_set((l), lomac_slot, (uintptr_t)(val))
129 #define	PSLOT(l) ((struct mac_lomac_proc *)				\
130     mac_label_get((l), lomac_slot))
131 #define	PSLOT_SET(l, val) mac_label_set((l), lomac_slot, (uintptr_t)(val))
132 
133 static MALLOC_DEFINE(M_LOMAC, "mac_lomac_label", "MAC/LOMAC labels");
134 
135 static struct mac_lomac *
136 lomac_alloc(int flag)
137 {
138 	struct mac_lomac *ml;
139 
140 	ml = malloc(sizeof(*ml), M_LOMAC, M_ZERO | flag);
141 
142 	return (ml);
143 }
144 
145 static void
146 lomac_free(struct mac_lomac *ml)
147 {
148 
149 	if (ml != NULL)
150 		free(ml, M_LOMAC);
151 	else
152 		atomic_add_int(&destroyed_not_inited, 1);
153 }
154 
155 static int
156 lomac_atmostflags(struct mac_lomac *ml, int flags)
157 {
158 
159 	if ((ml->ml_flags & flags) != ml->ml_flags)
160 		return (EINVAL);
161 	return (0);
162 }
163 
164 static int
165 lomac_dominate_element(struct mac_lomac_element *a,
166     struct mac_lomac_element *b)
167 {
168 
169 	switch (a->mle_type) {
170 	case MAC_LOMAC_TYPE_EQUAL:
171 	case MAC_LOMAC_TYPE_HIGH:
172 		return (1);
173 
174 	case MAC_LOMAC_TYPE_LOW:
175 		switch (b->mle_type) {
176 		case MAC_LOMAC_TYPE_GRADE:
177 		case MAC_LOMAC_TYPE_HIGH:
178 			return (0);
179 
180 		case MAC_LOMAC_TYPE_EQUAL:
181 		case MAC_LOMAC_TYPE_LOW:
182 			return (1);
183 
184 		default:
185 			panic("lomac_dominate_element: b->mle_type invalid");
186 		}
187 
188 	case MAC_LOMAC_TYPE_GRADE:
189 		switch (b->mle_type) {
190 		case MAC_LOMAC_TYPE_EQUAL:
191 		case MAC_LOMAC_TYPE_LOW:
192 			return (1);
193 
194 		case MAC_LOMAC_TYPE_HIGH:
195 			return (0);
196 
197 		case MAC_LOMAC_TYPE_GRADE:
198 			return (a->mle_grade >= b->mle_grade);
199 
200 		default:
201 			panic("lomac_dominate_element: b->mle_type invalid");
202 		}
203 
204 	default:
205 		panic("lomac_dominate_element: a->mle_type invalid");
206 	}
207 }
208 
209 static int
210 lomac_range_in_range(struct mac_lomac *rangea, struct mac_lomac *rangeb)
211 {
212 
213 	return (lomac_dominate_element(&rangeb->ml_rangehigh,
214 	    &rangea->ml_rangehigh) &&
215 	    lomac_dominate_element(&rangea->ml_rangelow,
216 	    &rangeb->ml_rangelow));
217 }
218 
219 static int
220 lomac_single_in_range(struct mac_lomac *single, struct mac_lomac *range)
221 {
222 
223 	KASSERT((single->ml_flags & MAC_LOMAC_FLAG_SINGLE) != 0,
224 	    ("lomac_single_in_range: a not single"));
225 	KASSERT((range->ml_flags & MAC_LOMAC_FLAG_RANGE) != 0,
226 	    ("lomac_single_in_range: b not range"));
227 
228 	return (lomac_dominate_element(&range->ml_rangehigh,
229 	    &single->ml_single) && lomac_dominate_element(&single->ml_single,
230 	    &range->ml_rangelow));
231 }
232 
233 static int
234 lomac_auxsingle_in_range(struct mac_lomac *single, struct mac_lomac *range)
235 {
236 
237 	KASSERT((single->ml_flags & MAC_LOMAC_FLAG_AUX) != 0,
238 	    ("lomac_single_in_range: a not auxsingle"));
239 	KASSERT((range->ml_flags & MAC_LOMAC_FLAG_RANGE) != 0,
240 	    ("lomac_single_in_range: b not range"));
241 
242 	return (lomac_dominate_element(&range->ml_rangehigh,
243 	    &single->ml_auxsingle) &&
244 	    lomac_dominate_element(&single->ml_auxsingle,
245 	    &range->ml_rangelow));
246 }
247 
248 static int
249 lomac_dominate_single(struct mac_lomac *a, struct mac_lomac *b)
250 {
251 	KASSERT((a->ml_flags & MAC_LOMAC_FLAG_SINGLE) != 0,
252 	    ("lomac_dominate_single: a not single"));
253 	KASSERT((b->ml_flags & MAC_LOMAC_FLAG_SINGLE) != 0,
254 	    ("lomac_dominate_single: b not single"));
255 
256 	return (lomac_dominate_element(&a->ml_single, &b->ml_single));
257 }
258 
259 static int
260 lomac_subject_dominate(struct mac_lomac *a, struct mac_lomac *b)
261 {
262 	KASSERT((~a->ml_flags &
263 	    (MAC_LOMAC_FLAG_SINGLE | MAC_LOMAC_FLAG_RANGE)) == 0,
264 	    ("lomac_dominate_single: a not subject"));
265 	KASSERT((b->ml_flags & MAC_LOMAC_FLAG_SINGLE) != 0,
266 	    ("lomac_dominate_single: b not single"));
267 
268 	return (lomac_dominate_element(&a->ml_rangehigh, &b->ml_single));
269 }
270 
271 static int
272 lomac_equal_element(struct mac_lomac_element *a, struct mac_lomac_element *b)
273 {
274 
275 	if (a->mle_type == MAC_LOMAC_TYPE_EQUAL ||
276 	    b->mle_type == MAC_LOMAC_TYPE_EQUAL)
277 		return (1);
278 
279 	return (a->mle_type == b->mle_type && a->mle_grade == b->mle_grade);
280 }
281 
282 static int
283 lomac_equal_single(struct mac_lomac *a, struct mac_lomac *b)
284 {
285 
286 	KASSERT((a->ml_flags & MAC_LOMAC_FLAG_SINGLE) != 0,
287 	    ("lomac_equal_single: a not single"));
288 	KASSERT((b->ml_flags & MAC_LOMAC_FLAG_SINGLE) != 0,
289 	    ("lomac_equal_single: b not single"));
290 
291 	return (lomac_equal_element(&a->ml_single, &b->ml_single));
292 }
293 
294 static int
295 lomac_contains_equal(struct mac_lomac *ml)
296 {
297 
298 	if (ml->ml_flags & MAC_LOMAC_FLAG_SINGLE)
299 		if (ml->ml_single.mle_type == MAC_LOMAC_TYPE_EQUAL)
300 			return (1);
301 	if (ml->ml_flags & MAC_LOMAC_FLAG_AUX)
302 		if (ml->ml_auxsingle.mle_type == MAC_LOMAC_TYPE_EQUAL)
303 			return (1);
304 
305 	if (ml->ml_flags & MAC_LOMAC_FLAG_RANGE) {
306 		if (ml->ml_rangelow.mle_type == MAC_LOMAC_TYPE_EQUAL)
307 			return (1);
308 		if (ml->ml_rangehigh.mle_type == MAC_LOMAC_TYPE_EQUAL)
309 			return (1);
310 	}
311 
312 	return (0);
313 }
314 
315 static int
316 lomac_subject_privileged(struct mac_lomac *ml)
317 {
318 
319 	KASSERT((ml->ml_flags & MAC_LOMAC_FLAGS_BOTH) ==
320 	    MAC_LOMAC_FLAGS_BOTH,
321 	    ("lomac_subject_privileged: subject doesn't have both labels"));
322 
323 	/* If the single is EQUAL, it's ok. */
324 	if (ml->ml_single.mle_type == MAC_LOMAC_TYPE_EQUAL)
325 		return (0);
326 
327 	/* If either range endpoint is EQUAL, it's ok. */
328 	if (ml->ml_rangelow.mle_type == MAC_LOMAC_TYPE_EQUAL ||
329 	    ml->ml_rangehigh.mle_type == MAC_LOMAC_TYPE_EQUAL)
330 		return (0);
331 
332 	/* If the range is low-high, it's ok. */
333 	if (ml->ml_rangelow.mle_type == MAC_LOMAC_TYPE_LOW &&
334 	    ml->ml_rangehigh.mle_type == MAC_LOMAC_TYPE_HIGH)
335 		return (0);
336 
337 	/* It's not ok. */
338 	return (EPERM);
339 }
340 
341 static int
342 lomac_high_single(struct mac_lomac *ml)
343 {
344 
345 	KASSERT((ml->ml_flags & MAC_LOMAC_FLAG_SINGLE) != 0,
346 	    ("lomac_high_single: mac_lomac not single"));
347 
348 	return (ml->ml_single.mle_type == MAC_LOMAC_TYPE_HIGH);
349 }
350 
351 static int
352 lomac_valid(struct mac_lomac *ml)
353 {
354 
355 	if (ml->ml_flags & MAC_LOMAC_FLAG_SINGLE) {
356 		switch (ml->ml_single.mle_type) {
357 		case MAC_LOMAC_TYPE_GRADE:
358 		case MAC_LOMAC_TYPE_EQUAL:
359 		case MAC_LOMAC_TYPE_HIGH:
360 		case MAC_LOMAC_TYPE_LOW:
361 			break;
362 
363 		default:
364 			return (EINVAL);
365 		}
366 	} else {
367 		if (ml->ml_single.mle_type != MAC_LOMAC_TYPE_UNDEF)
368 			return (EINVAL);
369 	}
370 
371 	if (ml->ml_flags & MAC_LOMAC_FLAG_AUX) {
372 		switch (ml->ml_auxsingle.mle_type) {
373 		case MAC_LOMAC_TYPE_GRADE:
374 		case MAC_LOMAC_TYPE_EQUAL:
375 		case MAC_LOMAC_TYPE_HIGH:
376 		case MAC_LOMAC_TYPE_LOW:
377 			break;
378 
379 		default:
380 			return (EINVAL);
381 		}
382 	} else {
383 		if (ml->ml_auxsingle.mle_type != MAC_LOMAC_TYPE_UNDEF)
384 			return (EINVAL);
385 	}
386 
387 	if (ml->ml_flags & MAC_LOMAC_FLAG_RANGE) {
388 		switch (ml->ml_rangelow.mle_type) {
389 		case MAC_LOMAC_TYPE_GRADE:
390 		case MAC_LOMAC_TYPE_EQUAL:
391 		case MAC_LOMAC_TYPE_HIGH:
392 		case MAC_LOMAC_TYPE_LOW:
393 			break;
394 
395 		default:
396 			return (EINVAL);
397 		}
398 
399 		switch (ml->ml_rangehigh.mle_type) {
400 		case MAC_LOMAC_TYPE_GRADE:
401 		case MAC_LOMAC_TYPE_EQUAL:
402 		case MAC_LOMAC_TYPE_HIGH:
403 		case MAC_LOMAC_TYPE_LOW:
404 			break;
405 
406 		default:
407 			return (EINVAL);
408 		}
409 		if (!lomac_dominate_element(&ml->ml_rangehigh,
410 		    &ml->ml_rangelow))
411 			return (EINVAL);
412 	} else {
413 		if (ml->ml_rangelow.mle_type != MAC_LOMAC_TYPE_UNDEF ||
414 		    ml->ml_rangehigh.mle_type != MAC_LOMAC_TYPE_UNDEF)
415 			return (EINVAL);
416 	}
417 
418 	return (0);
419 }
420 
421 static void
422 lomac_set_range(struct mac_lomac *ml, u_short typelow, u_short gradelow,
423     u_short typehigh, u_short gradehigh)
424 {
425 
426 	ml->ml_rangelow.mle_type = typelow;
427 	ml->ml_rangelow.mle_grade = gradelow;
428 	ml->ml_rangehigh.mle_type = typehigh;
429 	ml->ml_rangehigh.mle_grade = gradehigh;
430 	ml->ml_flags |= MAC_LOMAC_FLAG_RANGE;
431 }
432 
433 static void
434 lomac_set_single(struct mac_lomac *ml, u_short type, u_short grade)
435 {
436 
437 	ml->ml_single.mle_type = type;
438 	ml->ml_single.mle_grade = grade;
439 	ml->ml_flags |= MAC_LOMAC_FLAG_SINGLE;
440 }
441 
442 static void
443 lomac_copy_range(struct mac_lomac *labelfrom, struct mac_lomac *labelto)
444 {
445 
446 	KASSERT((labelfrom->ml_flags & MAC_LOMAC_FLAG_RANGE) != 0,
447 	    ("lomac_copy_range: labelfrom not range"));
448 
449 	labelto->ml_rangelow = labelfrom->ml_rangelow;
450 	labelto->ml_rangehigh = labelfrom->ml_rangehigh;
451 	labelto->ml_flags |= MAC_LOMAC_FLAG_RANGE;
452 }
453 
454 static void
455 lomac_copy_single(struct mac_lomac *labelfrom, struct mac_lomac *labelto)
456 {
457 
458 	KASSERT((labelfrom->ml_flags & MAC_LOMAC_FLAG_SINGLE) != 0,
459 	    ("lomac_copy_single: labelfrom not single"));
460 
461 	labelto->ml_single = labelfrom->ml_single;
462 	labelto->ml_flags |= MAC_LOMAC_FLAG_SINGLE;
463 }
464 
465 static void
466 lomac_copy_auxsingle(struct mac_lomac *labelfrom, struct mac_lomac *labelto)
467 {
468 
469 	KASSERT((labelfrom->ml_flags & MAC_LOMAC_FLAG_AUX) != 0,
470 	    ("lomac_copy_auxsingle: labelfrom not auxsingle"));
471 
472 	labelto->ml_auxsingle = labelfrom->ml_auxsingle;
473 	labelto->ml_flags |= MAC_LOMAC_FLAG_AUX;
474 }
475 
476 static void
477 lomac_copy(struct mac_lomac *source, struct mac_lomac *dest)
478 {
479 
480 	if (source->ml_flags & MAC_LOMAC_FLAG_SINGLE)
481 		lomac_copy_single(source, dest);
482 	if (source->ml_flags & MAC_LOMAC_FLAG_AUX)
483 		lomac_copy_auxsingle(source, dest);
484 	if (source->ml_flags & MAC_LOMAC_FLAG_RANGE)
485 		lomac_copy_range(source, dest);
486 }
487 
488 static int	lomac_to_string(struct sbuf *sb, struct mac_lomac *ml);
489 
490 static int
491 maybe_demote(struct mac_lomac *subjlabel, struct mac_lomac *objlabel,
492     const char *actionname, const char *objname, struct vnode *vp)
493 {
494 	struct sbuf subjlabel_sb, subjtext_sb, objlabel_sb;
495 	char *subjlabeltext, *objlabeltext, *subjtext;
496 	struct mac_lomac cached_subjlabel;
497 	struct mac_lomac_proc *subj;
498 	struct vattr va;
499 	struct proc *p;
500 	pid_t pgid;
501 
502 	subj = PSLOT(curthread->td_proc->p_label);
503 
504 	p = curthread->td_proc;
505 	mtx_lock(&subj->mtx);
506         if (subj->mac_lomac.ml_flags & MAC_LOMAC_FLAG_UPDATE) {
507 		/*
508 		 * Check to see if the pending demotion would be more or less
509 		 * severe than this one, and keep the more severe.  This can
510 		 * only happen for a multi-threaded application.
511 		 */
512 		if (lomac_dominate_single(objlabel, &subj->mac_lomac)) {
513 			mtx_unlock(&subj->mtx);
514 			return (0);
515 		}
516 	}
517 	bzero(&subj->mac_lomac, sizeof(subj->mac_lomac));
518 	/*
519 	 * Always demote the single label.
520 	 */
521 	lomac_copy_single(objlabel, &subj->mac_lomac);
522 	/*
523 	 * Start with the original range, then minimize each side of the
524 	 * range to the point of not dominating the object.  The high side
525 	 * will always be demoted, of course.
526 	 */
527 	lomac_copy_range(subjlabel, &subj->mac_lomac);
528 	if (!lomac_dominate_element(&objlabel->ml_single,
529 	    &subj->mac_lomac.ml_rangelow))
530 		subj->mac_lomac.ml_rangelow = objlabel->ml_single;
531 	subj->mac_lomac.ml_rangehigh = objlabel->ml_single;
532 	subj->mac_lomac.ml_flags |= MAC_LOMAC_FLAG_UPDATE;
533 	ast_sched(curthread, TDA_MAC);
534 
535 	/*
536 	 * Avoid memory allocation while holding a mutex; cache the label.
537 	 */
538 	lomac_copy_single(&subj->mac_lomac, &cached_subjlabel);
539 	mtx_unlock(&subj->mtx);
540 
541 	sbuf_new(&subjlabel_sb, NULL, 0, SBUF_AUTOEXTEND);
542 	lomac_to_string(&subjlabel_sb, subjlabel);
543 	sbuf_finish(&subjlabel_sb);
544 	subjlabeltext = sbuf_data(&subjlabel_sb);
545 
546 	sbuf_new(&subjtext_sb, NULL, 0, SBUF_AUTOEXTEND);
547 	lomac_to_string(&subjtext_sb, &subj->mac_lomac);
548 	sbuf_finish(&subjtext_sb);
549 	subjtext = sbuf_data(&subjtext_sb);
550 
551 	sbuf_new(&objlabel_sb, NULL, 0, SBUF_AUTOEXTEND);
552 	lomac_to_string(&objlabel_sb, objlabel);
553 	sbuf_finish(&objlabel_sb);
554 	objlabeltext = sbuf_data(&objlabel_sb);
555 
556 	pgid = p->p_pgrp->pg_id;		/* XXX could be stale? */
557 	if (vp != NULL && VOP_GETATTR(vp, &va, curthread->td_ucred) == 0) {
558 		log(LOG_INFO, "LOMAC: level-%s subject p%dg%du%d:%s demoted to"
559 		    " level %s after %s a level-%s %s (inode=%ju, "
560 		    "mountpount=%s)\n",
561 		    subjlabeltext, p->p_pid, pgid, curthread->td_ucred->cr_uid,
562 		    p->p_comm, subjtext, actionname, objlabeltext, objname,
563 		    (uintmax_t)va.va_fileid, vp->v_mount->mnt_stat.f_mntonname);
564 	} else {
565 		log(LOG_INFO, "LOMAC: level-%s subject p%dg%du%d:%s demoted to"
566 		    " level %s after %s a level-%s %s\n",
567 		    subjlabeltext, p->p_pid, pgid, curthread->td_ucred->cr_uid,
568 		    p->p_comm, subjtext, actionname, objlabeltext, objname);
569 	}
570 
571 	sbuf_delete(&subjlabel_sb);
572 	sbuf_delete(&subjtext_sb);
573 	sbuf_delete(&objlabel_sb);
574 
575 	return (0);
576 }
577 
578 /*
579  * Relabel "to" to "from" only if "from" is a valid label (contains at least
580  * a single), as for a relabel operation which may or may not involve a
581  * relevant label.
582  */
583 static void
584 try_relabel(struct mac_lomac *from, struct mac_lomac *to)
585 {
586 
587 	if (from->ml_flags & MAC_LOMAC_FLAG_SINGLE) {
588 		bzero(to, sizeof(*to));
589 		lomac_copy(from, to);
590 	}
591 }
592 
593 static void
594 ast_mac(struct thread *td, int tda __unused)
595 {
596 	mac_thread_userret(td);
597 }
598 
599 /*
600  * Policy module operations.
601  */
602 static void
603 lomac_init(struct mac_policy_conf *conf __unused)
604 {
605 	ast_register(TDA_MAC, ASTR_ASTF_REQUIRED, 0, ast_mac);
606 }
607 
608 static void
609 lomac_fini(struct mac_policy_conf *conf __unused)
610 {
611 	ast_deregister(TDA_MAC);
612 }
613 
614 /*
615  * Label operations.
616  */
617 static void
618 lomac_init_label(struct label *label)
619 {
620 
621 	SLOT_SET(label, lomac_alloc(M_WAITOK));
622 }
623 
624 static int
625 lomac_init_label_waitcheck(struct label *label, int flag)
626 {
627 
628 	SLOT_SET(label, lomac_alloc(flag));
629 	if (SLOT(label) == NULL)
630 		return (ENOMEM);
631 
632 	return (0);
633 }
634 
635 static void
636 lomac_destroy_label(struct label *label)
637 {
638 
639 	lomac_free(SLOT(label));
640 	SLOT_SET(label, NULL);
641 }
642 
643 static int
644 lomac_element_to_string(struct sbuf *sb, struct mac_lomac_element *element)
645 {
646 
647 	switch (element->mle_type) {
648 	case MAC_LOMAC_TYPE_HIGH:
649 		return (sbuf_printf(sb, "high"));
650 
651 	case MAC_LOMAC_TYPE_LOW:
652 		return (sbuf_printf(sb, "low"));
653 
654 	case MAC_LOMAC_TYPE_EQUAL:
655 		return (sbuf_printf(sb, "equal"));
656 
657 	case MAC_LOMAC_TYPE_GRADE:
658 		return (sbuf_printf(sb, "%d", element->mle_grade));
659 
660 	default:
661 		panic("lomac_element_to_string: invalid type (%d)",
662 		    element->mle_type);
663 	}
664 }
665 
666 static int
667 lomac_to_string(struct sbuf *sb, struct mac_lomac *ml)
668 {
669 
670 	if (ml->ml_flags & MAC_LOMAC_FLAG_SINGLE) {
671 		if (lomac_element_to_string(sb, &ml->ml_single) == -1)
672 			return (EINVAL);
673 	}
674 
675 	if (ml->ml_flags & MAC_LOMAC_FLAG_AUX) {
676 		if (sbuf_putc(sb, '[') == -1)
677 			return (EINVAL);
678 
679 		if (lomac_element_to_string(sb, &ml->ml_auxsingle) == -1)
680 			return (EINVAL);
681 
682 		if (sbuf_putc(sb, ']') == -1)
683 			return (EINVAL);
684 	}
685 
686 	if (ml->ml_flags & MAC_LOMAC_FLAG_RANGE) {
687 		if (sbuf_putc(sb, '(') == -1)
688 			return (EINVAL);
689 
690 		if (lomac_element_to_string(sb, &ml->ml_rangelow) == -1)
691 			return (EINVAL);
692 
693 		if (sbuf_putc(sb, '-') == -1)
694 			return (EINVAL);
695 
696 		if (lomac_element_to_string(sb, &ml->ml_rangehigh) == -1)
697 			return (EINVAL);
698 
699 		if (sbuf_putc(sb, ')') == -1)
700 			return (EINVAL);
701 	}
702 
703 	return (0);
704 }
705 
706 static int
707 lomac_externalize_label(struct label *label, char *element_name,
708     struct sbuf *sb, int *claimed)
709 {
710 	struct mac_lomac *ml;
711 
712 	if (strcmp(MAC_LOMAC_LABEL_NAME, element_name) != 0)
713 		return (0);
714 
715 	(*claimed)++;
716 
717 	ml = SLOT(label);
718 
719 	return (lomac_to_string(sb, ml));
720 }
721 
722 static int
723 lomac_parse_element(struct mac_lomac_element *element, char *string)
724 {
725 
726 	if (strcmp(string, "high") == 0 || strcmp(string, "hi") == 0) {
727 		element->mle_type = MAC_LOMAC_TYPE_HIGH;
728 		element->mle_grade = MAC_LOMAC_TYPE_UNDEF;
729 	} else if (strcmp(string, "low") == 0 || strcmp(string, "lo") == 0) {
730 		element->mle_type = MAC_LOMAC_TYPE_LOW;
731 		element->mle_grade = MAC_LOMAC_TYPE_UNDEF;
732 	} else if (strcmp(string, "equal") == 0 ||
733 	    strcmp(string, "eq") == 0) {
734 		element->mle_type = MAC_LOMAC_TYPE_EQUAL;
735 		element->mle_grade = MAC_LOMAC_TYPE_UNDEF;
736 	} else {
737 		char *p0, *p1;
738 		int d;
739 
740 		p0 = string;
741 		d = strtol(p0, &p1, 10);
742 
743 		if (d < 0 || d > 65535)
744 			return (EINVAL);
745 		element->mle_type = MAC_LOMAC_TYPE_GRADE;
746 		element->mle_grade = d;
747 
748 		if (p1 == p0 || *p1 != '\0')
749 			return (EINVAL);
750 	}
751 
752 	return (0);
753 }
754 
755 /*
756  * Note: destructively consumes the string, make a local copy before calling
757  * if that's a problem.
758  */
759 static int
760 lomac_parse(struct mac_lomac *ml, char *string)
761 {
762 	char *range, *rangeend, *rangehigh, *rangelow, *single, *auxsingle,
763 	    *auxsingleend;
764 	int error;
765 
766 	/* Do we have a range? */
767 	single = string;
768 	range = strchr(string, '(');
769 	if (range == single)
770 		single = NULL;
771 	auxsingle = strchr(string, '[');
772 	if (auxsingle == single)
773 		single = NULL;
774 	if (range != NULL && auxsingle != NULL)
775 		return (EINVAL);
776 	rangelow = rangehigh = NULL;
777 	if (range != NULL) {
778 		/* Nul terminate the end of the single string. */
779 		*range = '\0';
780 		range++;
781 		rangelow = range;
782 		rangehigh = strchr(rangelow, '-');
783 		if (rangehigh == NULL)
784 			return (EINVAL);
785 		rangehigh++;
786 		if (*rangelow == '\0' || *rangehigh == '\0')
787 			return (EINVAL);
788 		rangeend = strchr(rangehigh, ')');
789 		if (rangeend == NULL)
790 			return (EINVAL);
791 		if (*(rangeend + 1) != '\0')
792 			return (EINVAL);
793 		/* Nul terminate the ends of the ranges. */
794 		*(rangehigh - 1) = '\0';
795 		*rangeend = '\0';
796 	}
797 	KASSERT((rangelow != NULL && rangehigh != NULL) ||
798 	    (rangelow == NULL && rangehigh == NULL),
799 	    ("lomac_internalize_label: range mismatch"));
800 	if (auxsingle != NULL) {
801 		/* Nul terminate the end of the single string. */
802 		*auxsingle = '\0';
803 		auxsingle++;
804 		auxsingleend = strchr(auxsingle, ']');
805 		if (auxsingleend == NULL)
806 			return (EINVAL);
807 		if (*(auxsingleend + 1) != '\0')
808 			return (EINVAL);
809 		/* Nul terminate the end of the auxsingle. */
810 		*auxsingleend = '\0';
811 	}
812 
813 	bzero(ml, sizeof(*ml));
814 	if (single != NULL) {
815 		error = lomac_parse_element(&ml->ml_single, single);
816 		if (error)
817 			return (error);
818 		ml->ml_flags |= MAC_LOMAC_FLAG_SINGLE;
819 	}
820 
821 	if (auxsingle != NULL) {
822 		error = lomac_parse_element(&ml->ml_auxsingle, auxsingle);
823 		if (error)
824 			return (error);
825 		ml->ml_flags |= MAC_LOMAC_FLAG_AUX;
826 	}
827 
828 	if (rangelow != NULL) {
829 		error = lomac_parse_element(&ml->ml_rangelow, rangelow);
830 		if (error)
831 			return (error);
832 		error = lomac_parse_element(&ml->ml_rangehigh, rangehigh);
833 		if (error)
834 			return (error);
835 		ml->ml_flags |= MAC_LOMAC_FLAG_RANGE;
836 	}
837 
838 	error = lomac_valid(ml);
839 	if (error)
840 		return (error);
841 
842 	return (0);
843 }
844 
845 static int
846 lomac_internalize_label(struct label *label, char *element_name,
847     char *element_data, int *claimed)
848 {
849 	struct mac_lomac *ml, ml_temp;
850 	int error;
851 
852 	if (strcmp(MAC_LOMAC_LABEL_NAME, element_name) != 0)
853 		return (0);
854 
855 	(*claimed)++;
856 
857 	error = lomac_parse(&ml_temp, element_data);
858 	if (error)
859 		return (error);
860 
861 	ml = SLOT(label);
862 	*ml = ml_temp;
863 
864 	return (0);
865 }
866 
867 static void
868 lomac_copy_label(struct label *src, struct label *dest)
869 {
870 
871 	*SLOT(dest) = *SLOT(src);
872 }
873 
874 /*
875  * Object-specific entry point implementations are sorted alphabetically by
876  * object type name and then by operation.
877  */
878 static int
879 lomac_bpfdesc_check_receive(struct bpf_d *d, struct label *dlabel,
880     struct ifnet *ifp, struct label *ifplabel)
881 {
882 	struct mac_lomac *a, *b;
883 
884 	if (!lomac_enabled)
885 		return (0);
886 
887 	a = SLOT(dlabel);
888 	b = SLOT(ifplabel);
889 
890 	if (lomac_equal_single(a, b))
891 		return (0);
892 	return (EACCES);
893 }
894 
895 static void
896 lomac_bpfdesc_create(struct ucred *cred, struct bpf_d *d,
897     struct label *dlabel)
898 {
899 	struct mac_lomac *source, *dest;
900 
901 	source = SLOT(cred->cr_label);
902 	dest = SLOT(dlabel);
903 
904 	lomac_copy_single(source, dest);
905 }
906 
907 static void
908 lomac_bpfdesc_create_mbuf(struct bpf_d *d, struct label *dlabel,
909     struct mbuf *m, struct label *mlabel)
910 {
911 	struct mac_lomac *source, *dest;
912 
913 	source = SLOT(dlabel);
914 	dest = SLOT(mlabel);
915 
916 	lomac_copy_single(source, dest);
917 }
918 
919 static int
920 lomac_cred_check_relabel(struct ucred *cred, struct label *newlabel)
921 {
922 	struct mac_lomac *subj, *new;
923 	int error;
924 
925 	subj = SLOT(cred->cr_label);
926 	new = SLOT(newlabel);
927 
928 	/*
929 	 * If there is a LOMAC label update for the credential, it may be an
930 	 * update of the single, range, or both.
931 	 */
932 	error = lomac_atmostflags(new, MAC_LOMAC_FLAGS_BOTH);
933 	if (error)
934 		return (error);
935 
936 	/*
937 	 * If the LOMAC label is to be changed, authorize as appropriate.
938 	 */
939 	if (new->ml_flags & MAC_LOMAC_FLAGS_BOTH) {
940 		/*
941 		 * Fill in the missing parts from the previous label.
942 		 */
943 		if ((new->ml_flags & MAC_LOMAC_FLAG_SINGLE) == 0)
944 			lomac_copy_single(subj, new);
945 		if ((new->ml_flags & MAC_LOMAC_FLAG_RANGE) == 0)
946 			lomac_copy_range(subj, new);
947 
948 		/*
949 		 * To change the LOMAC range on a credential, the new range
950 		 * label must be in the current range.
951 		 */
952 		if (!lomac_range_in_range(new, subj))
953 			return (EPERM);
954 
955 		/*
956 		 * To change the LOMAC single label on a credential, the new
957 		 * single label must be in the new range.  Implicitly from
958 		 * the previous check, the new single is in the old range.
959 		 */
960 		if (!lomac_single_in_range(new, new))
961 			return (EPERM);
962 
963 		/*
964 		 * To have EQUAL in any component of the new credential LOMAC
965 		 * label, the subject must already have EQUAL in their label.
966 		 */
967 		if (lomac_contains_equal(new)) {
968 			error = lomac_subject_privileged(subj);
969 			if (error)
970 				return (error);
971 		}
972 
973 		/*
974 		 * XXXMAC: Additional consistency tests regarding the single
975 		 * and range of the new label might be performed here.
976 		 */
977 	}
978 
979 	return (0);
980 }
981 
982 static int
983 lomac_cred_check_visible(struct ucred *cr1, struct ucred *cr2)
984 {
985 	struct mac_lomac *subj, *obj;
986 
987 	if (!lomac_enabled)
988 		return (0);
989 
990 	subj = SLOT(cr1->cr_label);
991 	obj = SLOT(cr2->cr_label);
992 
993 	/* XXX: range */
994 	if (!lomac_dominate_single(obj, subj))
995 		return (ESRCH);
996 
997 	return (0);
998 }
999 
1000 static void
1001 lomac_cred_create_init(struct ucred *cred)
1002 {
1003 	struct mac_lomac *dest;
1004 
1005 	dest = SLOT(cred->cr_label);
1006 
1007 	lomac_set_single(dest, MAC_LOMAC_TYPE_HIGH, 0);
1008 	lomac_set_range(dest, MAC_LOMAC_TYPE_LOW, 0, MAC_LOMAC_TYPE_HIGH, 0);
1009 }
1010 
1011 static void
1012 lomac_cred_create_swapper(struct ucred *cred)
1013 {
1014 	struct mac_lomac *dest;
1015 
1016 	dest = SLOT(cred->cr_label);
1017 
1018 	lomac_set_single(dest, MAC_LOMAC_TYPE_EQUAL, 0);
1019 	lomac_set_range(dest, MAC_LOMAC_TYPE_LOW, 0, MAC_LOMAC_TYPE_HIGH, 0);
1020 }
1021 
1022 static void
1023 lomac_cred_relabel(struct ucred *cred, struct label *newlabel)
1024 {
1025 	struct mac_lomac *source, *dest;
1026 
1027 	source = SLOT(newlabel);
1028 	dest = SLOT(cred->cr_label);
1029 
1030 	try_relabel(source, dest);
1031 }
1032 
1033 static void
1034 lomac_devfs_create_device(struct ucred *cred, struct mount *mp,
1035     struct cdev *dev, struct devfs_dirent *de, struct label *delabel)
1036 {
1037 	struct mac_lomac *ml;
1038 	const char *dn;
1039 	int lomac_type;
1040 
1041 	ml = SLOT(delabel);
1042 	dn = devtoname(dev);
1043 	if (strcmp(dn, "null") == 0 ||
1044 	    strcmp(dn, "zero") == 0 ||
1045 	    strcmp(dn, "random") == 0 ||
1046 	    strncmp(dn, "fd/", strlen("fd/")) == 0 ||
1047 	    strncmp(dn, "ttyv", strlen("ttyv")) == 0)
1048 		lomac_type = MAC_LOMAC_TYPE_EQUAL;
1049 	else if (ptys_equal &&
1050 	    (strncmp(dn, "ttyp", strlen("ttyp")) == 0 ||
1051 	    strncmp(dn, "pts/", strlen("pts/")) == 0 ||
1052 	    strncmp(dn, "ptyp", strlen("ptyp")) == 0))
1053 		lomac_type = MAC_LOMAC_TYPE_EQUAL;
1054 	else
1055 		lomac_type = MAC_LOMAC_TYPE_HIGH;
1056 	lomac_set_single(ml, lomac_type, 0);
1057 }
1058 
1059 static void
1060 lomac_devfs_create_directory(struct mount *mp, char *dirname, int dirnamelen,
1061     struct devfs_dirent *de, struct label *delabel)
1062 {
1063 	struct mac_lomac *ml;
1064 
1065 	ml = SLOT(delabel);
1066 	lomac_set_single(ml, MAC_LOMAC_TYPE_HIGH, 0);
1067 }
1068 
1069 static void
1070 lomac_devfs_create_symlink(struct ucred *cred, struct mount *mp,
1071     struct devfs_dirent *dd, struct label *ddlabel, struct devfs_dirent *de,
1072     struct label *delabel)
1073 {
1074 	struct mac_lomac *source, *dest;
1075 
1076 	source = SLOT(cred->cr_label);
1077 	dest = SLOT(delabel);
1078 
1079 	lomac_copy_single(source, dest);
1080 }
1081 
1082 static void
1083 lomac_devfs_update(struct mount *mp, struct devfs_dirent *de,
1084     struct label *delabel, struct vnode *vp, struct label *vplabel)
1085 {
1086 	struct mac_lomac *source, *dest;
1087 
1088 	source = SLOT(vplabel);
1089 	dest = SLOT(delabel);
1090 
1091 	lomac_copy(source, dest);
1092 }
1093 
1094 static void
1095 lomac_devfs_vnode_associate(struct mount *mp, struct label *mplabel,
1096     struct devfs_dirent *de, struct label *delabel, struct vnode *vp,
1097     struct label *vplabel)
1098 {
1099 	struct mac_lomac *source, *dest;
1100 
1101 	source = SLOT(delabel);
1102 	dest = SLOT(vplabel);
1103 
1104 	lomac_copy_single(source, dest);
1105 }
1106 
1107 static int
1108 lomac_ifnet_check_relabel(struct ucred *cred, struct ifnet *ifp,
1109     struct label *ifplabel, struct label *newlabel)
1110 {
1111 	struct mac_lomac *subj, *new;
1112 	int error;
1113 
1114 	subj = SLOT(cred->cr_label);
1115 	new = SLOT(newlabel);
1116 
1117 	/*
1118 	 * If there is a LOMAC label update for the interface, it may be an
1119 	 * update of the single, range, or both.
1120 	 */
1121 	error = lomac_atmostflags(new, MAC_LOMAC_FLAGS_BOTH);
1122 	if (error)
1123 		return (error);
1124 
1125 	/*
1126 	 * Relabling network interfaces requires LOMAC privilege.
1127 	 */
1128 	error = lomac_subject_privileged(subj);
1129 	if (error)
1130 		return (error);
1131 
1132 	/*
1133 	 * If the LOMAC label is to be changed, authorize as appropriate.
1134 	 */
1135 	if (new->ml_flags & MAC_LOMAC_FLAGS_BOTH) {
1136 		/*
1137 		 * Fill in the missing parts from the previous label.
1138 		 */
1139 		if ((new->ml_flags & MAC_LOMAC_FLAG_SINGLE) == 0)
1140 			lomac_copy_single(subj, new);
1141 		if ((new->ml_flags & MAC_LOMAC_FLAG_RANGE) == 0)
1142 			lomac_copy_range(subj, new);
1143 
1144 		/*
1145 		 * Rely on the traditional superuser status for the LOMAC
1146 		 * interface relabel requirements.  XXXMAC: This will go
1147 		 * away.
1148 		 *
1149 		 * XXXRW: This is also redundant to a higher layer check.
1150 		 */
1151 		error = priv_check_cred(cred, PRIV_NET_SETIFMAC);
1152 		if (error)
1153 			return (EPERM);
1154 
1155 		/*
1156 		 * XXXMAC: Additional consistency tests regarding the single
1157 		 * and the range of the new label might be performed here.
1158 		 */
1159 	}
1160 
1161 	return (0);
1162 }
1163 
1164 static int
1165 lomac_ifnet_check_transmit(struct ifnet *ifp, struct label *ifplabel,
1166     struct mbuf *m, struct label *mlabel)
1167 {
1168 	struct mac_lomac *p, *i;
1169 
1170 	if (!lomac_enabled)
1171 		return (0);
1172 
1173 	p = SLOT(mlabel);
1174 	i = SLOT(ifplabel);
1175 
1176 	return (lomac_single_in_range(p, i) ? 0 : EACCES);
1177 }
1178 
1179 static void
1180 lomac_ifnet_create(struct ifnet *ifp, struct label *ifplabel)
1181 {
1182 	char tifname[IFNAMSIZ], *p, *q;
1183 	char tiflist[sizeof(trusted_interfaces)];
1184 	struct mac_lomac *dest;
1185 	int len, grade;
1186 
1187 	dest = SLOT(ifplabel);
1188 
1189 	if (if_gettype(ifp) == IFT_LOOP) {
1190 		grade = MAC_LOMAC_TYPE_EQUAL;
1191 		goto set;
1192 	}
1193 
1194 	if (trust_all_interfaces) {
1195 		grade = MAC_LOMAC_TYPE_HIGH;
1196 		goto set;
1197 	}
1198 
1199 	grade = MAC_LOMAC_TYPE_LOW;
1200 
1201 	if (trusted_interfaces[0] == '\0' ||
1202 	    !strvalid(trusted_interfaces, sizeof(trusted_interfaces)))
1203 		goto set;
1204 
1205 	bzero(tiflist, sizeof(tiflist));
1206 	for (p = trusted_interfaces, q = tiflist; *p != '\0'; p++, q++)
1207 		if(*p != ' ' && *p != '\t')
1208 			*q = *p;
1209 
1210 	for (p = q = tiflist;; p++) {
1211 		if (*p == ',' || *p == '\0') {
1212 			len = p - q;
1213 			if (len < IFNAMSIZ) {
1214 				bzero(tifname, sizeof(tifname));
1215 				bcopy(q, tifname, len);
1216 				if (strcmp(tifname, if_name(ifp)) == 0) {
1217 					grade = MAC_LOMAC_TYPE_HIGH;
1218 					break;
1219 				}
1220 			}
1221 			else {
1222 				*p = '\0';
1223 				printf("MAC/LOMAC warning: interface name "
1224 				    "\"%s\" is too long (must be < %d)\n",
1225 				    q, IFNAMSIZ);
1226 			}
1227 			if (*p == '\0')
1228 				break;
1229 			q = p + 1;
1230 		}
1231 	}
1232 set:
1233 	lomac_set_single(dest, grade, 0);
1234 	lomac_set_range(dest, grade, 0, grade, 0);
1235 }
1236 
1237 static void
1238 lomac_ifnet_create_mbuf(struct ifnet *ifp, struct label *ifplabel,
1239     struct mbuf *m, struct label *mlabel)
1240 {
1241 	struct mac_lomac *source, *dest;
1242 
1243 	source = SLOT(ifplabel);
1244 	dest = SLOT(mlabel);
1245 
1246 	lomac_copy_single(source, dest);
1247 }
1248 
1249 static void
1250 lomac_ifnet_relabel(struct ucred *cred, struct ifnet *ifp,
1251     struct label *ifplabel, struct label *newlabel)
1252 {
1253 	struct mac_lomac *source, *dest;
1254 
1255 	source = SLOT(newlabel);
1256 	dest = SLOT(ifplabel);
1257 
1258 	try_relabel(source, dest);
1259 }
1260 
1261 static int
1262 lomac_inpcb_check_deliver(struct inpcb *inp, struct label *inplabel,
1263     struct mbuf *m, struct label *mlabel)
1264 {
1265 	struct mac_lomac *p, *i;
1266 
1267 	if (!lomac_enabled)
1268 		return (0);
1269 
1270 	p = SLOT(mlabel);
1271 	i = SLOT(inplabel);
1272 
1273 	return (lomac_equal_single(p, i) ? 0 : EACCES);
1274 }
1275 
1276 static int
1277 lomac_inpcb_check_visible(struct ucred *cred, struct inpcb *inp,
1278     struct label *inplabel)
1279 {
1280 	struct mac_lomac *subj, *obj;
1281 
1282 	if (!lomac_enabled)
1283 		return (0);
1284 
1285 	subj = SLOT(cred->cr_label);
1286 	obj = SLOT(inplabel);
1287 
1288 	if (!lomac_dominate_single(obj, subj))
1289 		return (ENOENT);
1290 
1291 	return (0);
1292 }
1293 
1294 static void
1295 lomac_inpcb_create(struct socket *so, struct label *solabel,
1296     struct inpcb *inp, struct label *inplabel)
1297 {
1298 	struct mac_lomac *source, *dest;
1299 
1300 	source = SLOT(solabel);
1301 	dest = SLOT(inplabel);
1302 
1303 	lomac_copy_single(source, dest);
1304 }
1305 
1306 static void
1307 lomac_inpcb_create_mbuf(struct inpcb *inp, struct label *inplabel,
1308     struct mbuf *m, struct label *mlabel)
1309 {
1310 	struct mac_lomac *source, *dest;
1311 
1312 	source = SLOT(inplabel);
1313 	dest = SLOT(mlabel);
1314 
1315 	lomac_copy_single(source, dest);
1316 }
1317 
1318 static void
1319 lomac_inpcb_sosetlabel(struct socket *so, struct label *solabel,
1320     struct inpcb *inp, struct label *inplabel)
1321 {
1322 	struct mac_lomac *source, *dest;
1323 
1324 	SOCK_LOCK_ASSERT(so);
1325 
1326 	source = SLOT(solabel);
1327 	dest = SLOT(inplabel);
1328 
1329 	lomac_copy_single(source, dest);
1330 }
1331 
1332 static void
1333 lomac_ip6q_create(struct mbuf *m, struct label *mlabel, struct ip6q *q6,
1334     struct label *q6label)
1335 {
1336 	struct mac_lomac *source, *dest;
1337 
1338 	source = SLOT(mlabel);
1339 	dest = SLOT(q6label);
1340 
1341 	lomac_copy_single(source, dest);
1342 }
1343 
1344 static int
1345 lomac_ip6q_match(struct mbuf *m, struct label *mlabel, struct ip6q *q6,
1346     struct label *q6label)
1347 {
1348 	struct mac_lomac *a, *b;
1349 
1350 	a = SLOT(q6label);
1351 	b = SLOT(mlabel);
1352 
1353 	return (lomac_equal_single(a, b));
1354 }
1355 
1356 static void
1357 lomac_ip6q_reassemble(struct ip6q *q6, struct label *q6label, struct mbuf *m,
1358     struct label *mlabel)
1359 {
1360 	struct mac_lomac *source, *dest;
1361 
1362 	source = SLOT(q6label);
1363 	dest = SLOT(mlabel);
1364 
1365 	/* Just use the head, since we require them all to match. */
1366 	lomac_copy_single(source, dest);
1367 }
1368 
1369 static void
1370 lomac_ip6q_update(struct mbuf *m, struct label *mlabel, struct ip6q *q6,
1371     struct label *q6label)
1372 {
1373 
1374 	/* NOOP: we only accept matching labels, so no need to update */
1375 }
1376 
1377 static void
1378 lomac_ipq_create(struct mbuf *m, struct label *mlabel, struct ipq *q,
1379     struct label *qlabel)
1380 {
1381 	struct mac_lomac *source, *dest;
1382 
1383 	source = SLOT(mlabel);
1384 	dest = SLOT(qlabel);
1385 
1386 	lomac_copy_single(source, dest);
1387 }
1388 
1389 static int
1390 lomac_ipq_match(struct mbuf *m, struct label *mlabel, struct ipq *q,
1391     struct label *qlabel)
1392 {
1393 	struct mac_lomac *a, *b;
1394 
1395 	a = SLOT(qlabel);
1396 	b = SLOT(mlabel);
1397 
1398 	return (lomac_equal_single(a, b));
1399 }
1400 
1401 static void
1402 lomac_ipq_reassemble(struct ipq *q, struct label *qlabel, struct mbuf *m,
1403     struct label *mlabel)
1404 {
1405 	struct mac_lomac *source, *dest;
1406 
1407 	source = SLOT(qlabel);
1408 	dest = SLOT(mlabel);
1409 
1410 	/* Just use the head, since we require them all to match. */
1411 	lomac_copy_single(source, dest);
1412 }
1413 
1414 static void
1415 lomac_ipq_update(struct mbuf *m, struct label *mlabel, struct ipq *q,
1416     struct label *qlabel)
1417 {
1418 
1419 	/* NOOP: we only accept matching labels, so no need to update */
1420 }
1421 
1422 static int
1423 lomac_kld_check_load(struct ucred *cred, struct vnode *vp,
1424     struct label *vplabel)
1425 {
1426 	struct mac_lomac *subj, *obj;
1427 
1428 	if (!lomac_enabled)
1429 		return (0);
1430 
1431 	subj = SLOT(cred->cr_label);
1432 	obj = SLOT(vplabel);
1433 
1434 	if (lomac_subject_privileged(subj))
1435 		return (EPERM);
1436 
1437 	if (!lomac_high_single(obj))
1438 		return (EACCES);
1439 
1440 	return (0);
1441 }
1442 
1443 static void
1444 lomac_mount_create(struct ucred *cred, struct mount *mp,
1445     struct label *mplabel)
1446 {
1447 	struct mac_lomac *source, *dest;
1448 
1449 	source = SLOT(cred->cr_label);
1450 	dest = SLOT(mplabel);
1451 	lomac_copy_single(source, dest);
1452 }
1453 
1454 static void
1455 lomac_netinet_arp_send(struct ifnet *ifp, struct label *ifplabel,
1456     struct mbuf *m, struct label *mlabel)
1457 {
1458 	struct mac_lomac *dest;
1459 
1460 	dest = SLOT(mlabel);
1461 
1462 	lomac_set_single(dest, MAC_LOMAC_TYPE_EQUAL, 0);
1463 }
1464 
1465 static void
1466 lomac_netinet_firewall_reply(struct mbuf *mrecv, struct label *mrecvlabel,
1467     struct mbuf *msend, struct label *msendlabel)
1468 {
1469 	struct mac_lomac *source, *dest;
1470 
1471 	source = SLOT(mrecvlabel);
1472 	dest = SLOT(msendlabel);
1473 
1474 	lomac_copy_single(source, dest);
1475 }
1476 
1477 static void
1478 lomac_netinet_firewall_send(struct mbuf *m, struct label *mlabel)
1479 {
1480 	struct mac_lomac *dest;
1481 
1482 	dest = SLOT(mlabel);
1483 
1484 	/* XXX: where is the label for the firewall really coming from? */
1485 	lomac_set_single(dest, MAC_LOMAC_TYPE_EQUAL, 0);
1486 }
1487 
1488 static void
1489 lomac_netinet_fragment(struct mbuf *m, struct label *mlabel,
1490     struct mbuf *frag, struct label *fraglabel)
1491 {
1492 	struct mac_lomac *source, *dest;
1493 
1494 	source = SLOT(mlabel);
1495 	dest = SLOT(fraglabel);
1496 
1497 	lomac_copy_single(source, dest);
1498 }
1499 
1500 static void
1501 lomac_netinet_icmp_reply(struct mbuf *mrecv, struct label *mrecvlabel,
1502     struct mbuf *msend, struct label *msendlabel)
1503 {
1504 	struct mac_lomac *source, *dest;
1505 
1506 	source = SLOT(mrecvlabel);
1507 	dest = SLOT(msendlabel);
1508 
1509 	lomac_copy_single(source, dest);
1510 }
1511 
1512 static void
1513 lomac_netinet_igmp_send(struct ifnet *ifp, struct label *ifplabel,
1514     struct mbuf *m, struct label *mlabel)
1515 {
1516 	struct mac_lomac *dest;
1517 
1518 	dest = SLOT(mlabel);
1519 
1520 	lomac_set_single(dest, MAC_LOMAC_TYPE_EQUAL, 0);
1521 }
1522 
1523 static void
1524 lomac_netinet6_nd6_send(struct ifnet *ifp, struct label *ifplabel,
1525     struct mbuf *m, struct label *mlabel)
1526 {
1527 	struct mac_lomac *dest;
1528 
1529 	dest = SLOT(mlabel);
1530 
1531 	lomac_set_single(dest, MAC_LOMAC_TYPE_EQUAL, 0);
1532 }
1533 
1534 static int
1535 lomac_pipe_check_ioctl(struct ucred *cred, struct pipepair *pp,
1536     struct label *pplabel, unsigned long cmd, void /* caddr_t */ *data)
1537 {
1538 
1539 	if (!lomac_enabled)
1540 		return (0);
1541 
1542 	/* XXX: This will be implemented soon... */
1543 
1544 	return (0);
1545 }
1546 
1547 static int
1548 lomac_pipe_check_read(struct ucred *cred, struct pipepair *pp,
1549     struct label *pplabel)
1550 {
1551 	struct mac_lomac *subj, *obj;
1552 
1553 	if (!lomac_enabled)
1554 		return (0);
1555 
1556 	subj = SLOT(cred->cr_label);
1557 	obj = SLOT(pplabel);
1558 
1559 	if (!lomac_dominate_single(obj, subj))
1560 		return (maybe_demote(subj, obj, "reading", "pipe", NULL));
1561 
1562 	return (0);
1563 }
1564 
1565 static int
1566 lomac_pipe_check_relabel(struct ucred *cred, struct pipepair *pp,
1567     struct label *pplabel, struct label *newlabel)
1568 {
1569 	struct mac_lomac *subj, *obj, *new;
1570 	int error;
1571 
1572 	new = SLOT(newlabel);
1573 	subj = SLOT(cred->cr_label);
1574 	obj = SLOT(pplabel);
1575 
1576 	/*
1577 	 * If there is a LOMAC label update for a pipe, it must be a single
1578 	 * update.
1579 	 */
1580 	error = lomac_atmostflags(new, MAC_LOMAC_FLAG_SINGLE);
1581 	if (error)
1582 		return (error);
1583 
1584 	/*
1585 	 * To perform a relabel of a pipe (LOMAC label or not), LOMAC must
1586 	 * authorize the relabel.
1587 	 */
1588 	if (!lomac_single_in_range(obj, subj))
1589 		return (EPERM);
1590 
1591 	/*
1592 	 * If the LOMAC label is to be changed, authorize as appropriate.
1593 	 */
1594 	if (new->ml_flags & MAC_LOMAC_FLAG_SINGLE) {
1595 		/*
1596 		 * To change the LOMAC label on a pipe, the new pipe label
1597 		 * must be in the subject range.
1598 		 */
1599 		if (!lomac_single_in_range(new, subj))
1600 			return (EPERM);
1601 
1602 		/*
1603 		 * To change the LOMAC label on a pipe to be EQUAL, the
1604 		 * subject must have appropriate privilege.
1605 		 */
1606 		if (lomac_contains_equal(new)) {
1607 			error = lomac_subject_privileged(subj);
1608 			if (error)
1609 				return (error);
1610 		}
1611 	}
1612 
1613 	return (0);
1614 }
1615 
1616 static int
1617 lomac_pipe_check_write(struct ucred *cred, struct pipepair *pp,
1618     struct label *pplabel)
1619 {
1620 	struct mac_lomac *subj, *obj;
1621 
1622 	if (!lomac_enabled)
1623 		return (0);
1624 
1625 	subj = SLOT(cred->cr_label);
1626 	obj = SLOT(pplabel);
1627 
1628 	if (!lomac_subject_dominate(subj, obj))
1629 		return (EACCES);
1630 
1631 	return (0);
1632 }
1633 
1634 static void
1635 lomac_pipe_create(struct ucred *cred, struct pipepair *pp,
1636     struct label *pplabel)
1637 {
1638 	struct mac_lomac *source, *dest;
1639 
1640 	source = SLOT(cred->cr_label);
1641 	dest = SLOT(pplabel);
1642 
1643 	lomac_copy_single(source, dest);
1644 }
1645 
1646 static void
1647 lomac_pipe_relabel(struct ucred *cred, struct pipepair *pp,
1648     struct label *pplabel, struct label *newlabel)
1649 {
1650 	struct mac_lomac *source, *dest;
1651 
1652 	source = SLOT(newlabel);
1653 	dest = SLOT(pplabel);
1654 
1655 	try_relabel(source, dest);
1656 }
1657 
1658 /*
1659  * Some system privileges are allowed regardless of integrity grade; others
1660  * are allowed only when running with privilege with respect to the LOMAC
1661  * policy as they might otherwise allow bypassing of the integrity policy.
1662  */
1663 static int
1664 lomac_priv_check(struct ucred *cred, int priv)
1665 {
1666 	struct mac_lomac *subj;
1667 	int error;
1668 
1669 	if (!lomac_enabled)
1670 		return (0);
1671 
1672 	/*
1673 	 * Exempt only specific privileges from the LOMAC integrity policy.
1674 	 */
1675 	switch (priv) {
1676 	case PRIV_KTRACE:
1677 	case PRIV_MSGBUF:
1678 
1679 	/*
1680 	 * Allow processes to manipulate basic process audit properties, and
1681 	 * to submit audit records.
1682 	 */
1683 	case PRIV_AUDIT_GETAUDIT:
1684 	case PRIV_AUDIT_SETAUDIT:
1685 	case PRIV_AUDIT_SUBMIT:
1686 
1687 	/*
1688 	 * Allow processes to manipulate their regular UNIX credentials.
1689 	 */
1690 	case PRIV_CRED_SETUID:
1691 	case PRIV_CRED_SETEUID:
1692 	case PRIV_CRED_SETGID:
1693 	case PRIV_CRED_SETEGID:
1694 	case PRIV_CRED_SETGROUPS:
1695 	case PRIV_CRED_SETREUID:
1696 	case PRIV_CRED_SETREGID:
1697 	case PRIV_CRED_SETRESUID:
1698 	case PRIV_CRED_SETRESGID:
1699 
1700 	/*
1701 	 * Allow processes to perform system monitoring.
1702 	 */
1703 	case PRIV_SEEOTHERGIDS:
1704 	case PRIV_SEEOTHERUIDS:
1705 	case PRIV_SEEJAILPROC:
1706 		break;
1707 
1708 	/*
1709 	 * Allow access to general process debugging facilities.  We
1710 	 * separately control debugging based on MAC label.
1711 	 */
1712 	case PRIV_DEBUG_DIFFCRED:
1713 	case PRIV_DEBUG_SUGID:
1714 	case PRIV_DEBUG_UNPRIV:
1715 
1716 	/*
1717 	 * Allow manipulating jails.
1718 	 */
1719 	case PRIV_JAIL_ATTACH:
1720 
1721 	/*
1722 	 * Allow privilege with respect to the Partition policy, but not the
1723 	 * Privs policy.
1724 	 */
1725 	case PRIV_MAC_PARTITION:
1726 
1727 	/*
1728 	 * Allow privilege with respect to process resource limits and login
1729 	 * context.
1730 	 */
1731 	case PRIV_PROC_LIMIT:
1732 	case PRIV_PROC_SETLOGIN:
1733 	case PRIV_PROC_SETRLIMIT:
1734 
1735 	/*
1736 	 * Allow System V and POSIX IPC privileges.
1737 	 */
1738 	case PRIV_IPC_READ:
1739 	case PRIV_IPC_WRITE:
1740 	case PRIV_IPC_ADMIN:
1741 	case PRIV_IPC_MSGSIZE:
1742 	case PRIV_MQ_ADMIN:
1743 
1744 	/*
1745 	 * Allow certain scheduler manipulations -- possibly this should be
1746 	 * controlled by more fine-grained policy, as potentially low
1747 	 * integrity processes can deny CPU to higher integrity ones.
1748 	 */
1749 	case PRIV_SCHED_DIFFCRED:
1750 	case PRIV_SCHED_SETPRIORITY:
1751 	case PRIV_SCHED_RTPRIO:
1752 	case PRIV_SCHED_SETPOLICY:
1753 	case PRIV_SCHED_SET:
1754 	case PRIV_SCHED_SETPARAM:
1755 	case PRIV_SCHED_IDPRIO:
1756 
1757 	/*
1758 	 * More IPC privileges.
1759 	 */
1760 	case PRIV_SEM_WRITE:
1761 
1762 	/*
1763 	 * Allow signaling privileges subject to integrity policy.
1764 	 */
1765 	case PRIV_SIGNAL_DIFFCRED:
1766 	case PRIV_SIGNAL_SUGID:
1767 
1768 	/*
1769 	 * Allow access to only limited sysctls from lower integrity levels;
1770 	 * piggy-back on the Jail definition.
1771 	 */
1772 	case PRIV_SYSCTL_WRITEJAIL:
1773 
1774 	/*
1775 	 * Allow TTY-based privileges, subject to general device access using
1776 	 * labels on TTY device nodes, but not console privilege.
1777 	 */
1778 	case PRIV_TTY_DRAINWAIT:
1779 	case PRIV_TTY_DTRWAIT:
1780 	case PRIV_TTY_EXCLUSIVE:
1781 	case PRIV_TTY_STI:
1782 	case PRIV_TTY_SETA:
1783 
1784 	/*
1785 	 * Grant most VFS privileges, as almost all are in practice bounded
1786 	 * by more specific checks using labels.
1787 	 */
1788 	case PRIV_VFS_READ:
1789 	case PRIV_VFS_WRITE:
1790 	case PRIV_VFS_ADMIN:
1791 	case PRIV_VFS_EXEC:
1792 	case PRIV_VFS_LOOKUP:
1793 	case PRIV_VFS_CHFLAGS_DEV:
1794 	case PRIV_VFS_CHOWN:
1795 	case PRIV_VFS_CHROOT:
1796 	case PRIV_VFS_RETAINSUGID:
1797 	case PRIV_VFS_EXCEEDQUOTA:
1798 	case PRIV_VFS_FCHROOT:
1799 	case PRIV_VFS_FHOPEN:
1800 	case PRIV_VFS_FHSTATFS:
1801 	case PRIV_VFS_GENERATION:
1802 	case PRIV_VFS_GETFH:
1803 	case PRIV_VFS_GETQUOTA:
1804 	case PRIV_VFS_LINK:
1805 	case PRIV_VFS_MOUNT:
1806 	case PRIV_VFS_MOUNT_OWNER:
1807 	case PRIV_VFS_MOUNT_PERM:
1808 	case PRIV_VFS_MOUNT_SUIDDIR:
1809 	case PRIV_VFS_MOUNT_NONUSER:
1810 	case PRIV_VFS_SETGID:
1811 	case PRIV_VFS_STICKYFILE:
1812 	case PRIV_VFS_SYSFLAGS:
1813 	case PRIV_VFS_UNMOUNT:
1814 
1815 	/*
1816 	 * Allow VM privileges; it would be nice if these were subject to
1817 	 * resource limits.
1818 	 */
1819 	case PRIV_VM_MADV_PROTECT:
1820 	case PRIV_VM_MLOCK:
1821 	case PRIV_VM_MUNLOCK:
1822 	case PRIV_VM_SWAP_NOQUOTA:
1823 	case PRIV_VM_SWAP_NORLIMIT:
1824 
1825 	/*
1826 	 * Allow some but not all network privileges.  In general, dont allow
1827 	 * reconfiguring the network stack, just normal use.
1828 	 */
1829 	case PRIV_NETINET_RESERVEDPORT:
1830 	case PRIV_NETINET_RAW:
1831 	case PRIV_NETINET_REUSEPORT:
1832 		break;
1833 
1834 	/*
1835 	 * All remaining system privileges are allow only if the process
1836 	 * holds privilege with respect to the LOMAC policy.
1837 	 */
1838 	default:
1839 		subj = SLOT(cred->cr_label);
1840 		error = lomac_subject_privileged(subj);
1841 		if (error)
1842 			return (error);
1843 	}
1844 	return (0);
1845 }
1846 
1847 static int
1848 lomac_proc_check_debug(struct ucred *cred, struct proc *p)
1849 {
1850 	struct mac_lomac *subj, *obj;
1851 
1852 	if (!lomac_enabled)
1853 		return (0);
1854 
1855 	subj = SLOT(cred->cr_label);
1856 	obj = SLOT(p->p_ucred->cr_label);
1857 
1858 	/* XXX: range checks */
1859 	if (!lomac_dominate_single(obj, subj))
1860 		return (ESRCH);
1861 	if (!lomac_subject_dominate(subj, obj))
1862 		return (EACCES);
1863 
1864 	return (0);
1865 }
1866 
1867 static int
1868 lomac_proc_check_sched(struct ucred *cred, struct proc *p)
1869 {
1870 	struct mac_lomac *subj, *obj;
1871 
1872 	if (!lomac_enabled)
1873 		return (0);
1874 
1875 	subj = SLOT(cred->cr_label);
1876 	obj = SLOT(p->p_ucred->cr_label);
1877 
1878 	/* XXX: range checks */
1879 	if (!lomac_dominate_single(obj, subj))
1880 		return (ESRCH);
1881 	if (!lomac_subject_dominate(subj, obj))
1882 		return (EACCES);
1883 
1884 	return (0);
1885 }
1886 
1887 static int
1888 lomac_proc_check_signal(struct ucred *cred, struct proc *p, int signum)
1889 {
1890 	struct mac_lomac *subj, *obj;
1891 
1892 	if (!lomac_enabled)
1893 		return (0);
1894 
1895 	subj = SLOT(cred->cr_label);
1896 	obj = SLOT(p->p_ucred->cr_label);
1897 
1898 	/* XXX: range checks */
1899 	if (!lomac_dominate_single(obj, subj))
1900 		return (ESRCH);
1901 	if (!lomac_subject_dominate(subj, obj))
1902 		return (EACCES);
1903 
1904 	return (0);
1905 }
1906 
1907 static void
1908 lomac_proc_destroy_label(struct label *label)
1909 {
1910 
1911 	mtx_destroy(&PSLOT(label)->mtx);
1912 	free(PSLOT(label), M_LOMAC);
1913 	PSLOT_SET(label, NULL);
1914 }
1915 
1916 static void
1917 lomac_proc_init_label(struct label *label)
1918 {
1919 
1920 	PSLOT_SET(label, malloc(sizeof(struct mac_lomac_proc), M_LOMAC,
1921 	    M_ZERO | M_WAITOK));
1922 	mtx_init(&PSLOT(label)->mtx, "MAC/Lomac proc lock", NULL, MTX_DEF);
1923 }
1924 
1925 static int
1926 lomac_socket_check_deliver(struct socket *so, struct label *solabel,
1927     struct mbuf *m, struct label *mlabel)
1928 {
1929 	struct mac_lomac *p, *s;
1930 	int error;
1931 
1932 	if (!lomac_enabled)
1933 		return (0);
1934 
1935 	p = SLOT(mlabel);
1936 	s = SLOT(solabel);
1937 
1938 	SOCK_LOCK(so);
1939 	error = lomac_equal_single(p, s) ? 0 : EACCES;
1940 	SOCK_UNLOCK(so);
1941 	return (error);
1942 }
1943 
1944 static int
1945 lomac_socket_check_relabel(struct ucred *cred, struct socket *so,
1946     struct label *solabel, struct label *newlabel)
1947 {
1948 	struct mac_lomac *subj, *obj, *new;
1949 	int error;
1950 
1951 	SOCK_LOCK_ASSERT(so);
1952 
1953 	new = SLOT(newlabel);
1954 	subj = SLOT(cred->cr_label);
1955 	obj = SLOT(solabel);
1956 
1957 	/*
1958 	 * If there is a LOMAC label update for the socket, it may be an
1959 	 * update of single.
1960 	 */
1961 	error = lomac_atmostflags(new, MAC_LOMAC_FLAG_SINGLE);
1962 	if (error)
1963 		return (error);
1964 
1965 	/*
1966 	 * To relabel a socket, the old socket single must be in the subject
1967 	 * range.
1968 	 */
1969 	if (!lomac_single_in_range(obj, subj))
1970 		return (EPERM);
1971 
1972 	/*
1973 	 * If the LOMAC label is to be changed, authorize as appropriate.
1974 	 */
1975 	if (new->ml_flags & MAC_LOMAC_FLAG_SINGLE) {
1976 		/*
1977 		 * To relabel a socket, the new socket single must be in the
1978 		 * subject range.
1979 		 */
1980 		if (!lomac_single_in_range(new, subj))
1981 			return (EPERM);
1982 
1983 		/*
1984 		 * To change the LOMAC label on the socket to contain EQUAL,
1985 		 * the subject must have appropriate privilege.
1986 		 */
1987 		if (lomac_contains_equal(new)) {
1988 			error = lomac_subject_privileged(subj);
1989 			if (error)
1990 				return (error);
1991 		}
1992 	}
1993 
1994 	return (0);
1995 }
1996 
1997 static int
1998 lomac_socket_check_visible(struct ucred *cred, struct socket *so,
1999     struct label *solabel)
2000 {
2001 	struct mac_lomac *subj, *obj;
2002 
2003 	if (!lomac_enabled)
2004 		return (0);
2005 
2006 	subj = SLOT(cred->cr_label);
2007 	obj = SLOT(solabel);
2008 
2009 	SOCK_LOCK(so);
2010 	if (!lomac_dominate_single(obj, subj)) {
2011 		SOCK_UNLOCK(so);
2012 		return (ENOENT);
2013 	}
2014 	SOCK_UNLOCK(so);
2015 
2016 	return (0);
2017 }
2018 
2019 static void
2020 lomac_socket_create(struct ucred *cred, struct socket *so,
2021     struct label *solabel)
2022 {
2023 	struct mac_lomac *source, *dest;
2024 
2025 	source = SLOT(cred->cr_label);
2026 	dest = SLOT(solabel);
2027 
2028 	lomac_copy_single(source, dest);
2029 }
2030 
2031 static void
2032 lomac_socket_create_mbuf(struct socket *so, struct label *solabel,
2033     struct mbuf *m, struct label *mlabel)
2034 {
2035 	struct mac_lomac *source, *dest;
2036 
2037 	source = SLOT(solabel);
2038 	dest = SLOT(mlabel);
2039 
2040 	SOCK_LOCK(so);
2041 	lomac_copy_single(source, dest);
2042 	SOCK_UNLOCK(so);
2043 }
2044 
2045 static void
2046 lomac_socket_newconn(struct socket *oldso, struct label *oldsolabel,
2047     struct socket *newso, struct label *newsolabel)
2048 {
2049 	struct mac_lomac source, *dest;
2050 
2051 	SOCK_LOCK(oldso);
2052 	source = *SLOT(oldsolabel);
2053 	SOCK_UNLOCK(oldso);
2054 
2055 	dest = SLOT(newsolabel);
2056 
2057 	SOCK_LOCK(newso);
2058 	lomac_copy_single(&source, dest);
2059 	SOCK_UNLOCK(newso);
2060 }
2061 
2062 static void
2063 lomac_socket_relabel(struct ucred *cred, struct socket *so,
2064     struct label *solabel, struct label *newlabel)
2065 {
2066 	struct mac_lomac *source, *dest;
2067 
2068 	SOCK_LOCK_ASSERT(so);
2069 
2070 	source = SLOT(newlabel);
2071 	dest = SLOT(solabel);
2072 
2073 	try_relabel(source, dest);
2074 }
2075 
2076 static void
2077 lomac_socketpeer_set_from_mbuf(struct mbuf *m, struct label *mlabel,
2078     struct socket *so, struct label *sopeerlabel)
2079 {
2080 	struct mac_lomac *source, *dest;
2081 
2082 	source = SLOT(mlabel);
2083 	dest = SLOT(sopeerlabel);
2084 
2085 	SOCK_LOCK(so);
2086 	lomac_copy_single(source, dest);
2087 	SOCK_UNLOCK(so);
2088 }
2089 
2090 static void
2091 lomac_socketpeer_set_from_socket(struct socket *oldso,
2092     struct label *oldsolabel, struct socket *newso,
2093     struct label *newsopeerlabel)
2094 {
2095 	struct mac_lomac source, *dest;
2096 
2097 	SOCK_LOCK(oldso);
2098 	source = *SLOT(oldsolabel);
2099 	SOCK_UNLOCK(oldso);
2100 
2101 	dest = SLOT(newsopeerlabel);
2102 
2103 	SOCK_LOCK(newso);
2104 	lomac_copy_single(&source, dest);
2105 	SOCK_UNLOCK(newso);
2106 }
2107 
2108 static void
2109 lomac_syncache_create(struct label *label, struct inpcb *inp)
2110 {
2111 	struct mac_lomac *source, *dest;
2112 
2113 	source = SLOT(inp->inp_label);
2114 	dest = SLOT(label);
2115 	lomac_copy(source, dest);
2116 }
2117 
2118 static void
2119 lomac_syncache_create_mbuf(struct label *sc_label, struct mbuf *m,
2120     struct label *mlabel)
2121 {
2122 	struct mac_lomac *source, *dest;
2123 
2124 	source = SLOT(sc_label);
2125 	dest = SLOT(mlabel);
2126 	lomac_copy(source, dest);
2127 }
2128 
2129 static int
2130 lomac_system_check_acct(struct ucred *cred, struct vnode *vp,
2131     struct label *vplabel)
2132 {
2133 	struct mac_lomac *subj, *obj;
2134 
2135 	if (!lomac_enabled)
2136 		return (0);
2137 
2138 	subj = SLOT(cred->cr_label);
2139 	obj = SLOT(vplabel);
2140 
2141 	if (lomac_subject_privileged(subj))
2142 		return (EPERM);
2143 
2144 	if (!lomac_high_single(obj))
2145 		return (EACCES);
2146 
2147 	return (0);
2148 }
2149 
2150 static int
2151 lomac_system_check_auditctl(struct ucred *cred, struct vnode *vp,
2152     struct label *vplabel)
2153 {
2154 	struct mac_lomac *subj, *obj;
2155 
2156 	if (!lomac_enabled)
2157 		return (0);
2158 
2159 	subj = SLOT(cred->cr_label);
2160 	obj = SLOT(vplabel);
2161 
2162 	if (lomac_subject_privileged(subj))
2163 		return (EPERM);
2164 
2165 	if (!lomac_high_single(obj))
2166 		return (EACCES);
2167 
2168 	return (0);
2169 }
2170 
2171 static int
2172 lomac_system_check_swapoff(struct ucred *cred, struct vnode *vp,
2173     struct label *vplabel)
2174 {
2175 	struct mac_lomac *subj;
2176 
2177 	if (!lomac_enabled)
2178 		return (0);
2179 
2180 	subj = SLOT(cred->cr_label);
2181 
2182 	if (lomac_subject_privileged(subj))
2183 		return (EPERM);
2184 
2185 	return (0);
2186 }
2187 
2188 static int
2189 lomac_system_check_swapon(struct ucred *cred, struct vnode *vp,
2190     struct label *vplabel)
2191 {
2192 	struct mac_lomac *subj, *obj;
2193 
2194 	if (!lomac_enabled)
2195 		return (0);
2196 
2197 	subj = SLOT(cred->cr_label);
2198 	obj = SLOT(vplabel);
2199 
2200 	if (lomac_subject_privileged(subj))
2201 		return (EPERM);
2202 
2203 	if (!lomac_high_single(obj))
2204 		return (EACCES);
2205 
2206 	return (0);
2207 }
2208 
2209 static int
2210 lomac_system_check_sysctl(struct ucred *cred, struct sysctl_oid *oidp,
2211     void *arg1, int arg2, struct sysctl_req *req)
2212 {
2213 	struct mac_lomac *subj;
2214 
2215 	if (!lomac_enabled)
2216 		return (0);
2217 
2218 	subj = SLOT(cred->cr_label);
2219 
2220 	/*
2221 	 * Treat sysctl variables without CTLFLAG_ANYBODY flag as lomac/high,
2222 	 * but also require privilege to change them.
2223 	 */
2224 	if (req->newptr != NULL && (oidp->oid_kind & CTLFLAG_ANYBODY) == 0) {
2225 #ifdef notdef
2226 		if (!lomac_subject_dominate_high(subj))
2227 			return (EACCES);
2228 #endif
2229 
2230 		if (lomac_subject_privileged(subj))
2231 			return (EPERM);
2232 	}
2233 
2234 	return (0);
2235 }
2236 
2237 static void
2238 lomac_thread_userret(struct thread *td)
2239 {
2240 	struct proc *p = td->td_proc;
2241 	struct mac_lomac_proc *subj = PSLOT(p->p_label);
2242 	struct ucred *newcred, *oldcred;
2243 	int dodrop;
2244 
2245 	mtx_lock(&subj->mtx);
2246 	if (subj->mac_lomac.ml_flags & MAC_LOMAC_FLAG_UPDATE) {
2247 		dodrop = 0;
2248 		mtx_unlock(&subj->mtx);
2249 		newcred = crget();
2250 		PROC_LOCK(p);
2251 		mtx_lock(&subj->mtx);
2252 		/*
2253 		 * Check if we lost the race while allocating the cred.
2254 		 */
2255 		if ((subj->mac_lomac.ml_flags & MAC_LOMAC_FLAG_UPDATE) == 0) {
2256 			crfree(newcred);
2257 			goto out;
2258 		}
2259 		oldcred = p->p_ucred;
2260 		crcopy(newcred, oldcred);
2261 		crhold(newcred);
2262 		lomac_copy(&subj->mac_lomac, SLOT(newcred->cr_label));
2263 		proc_set_cred(p, newcred);
2264 		crfree(oldcred);
2265 		dodrop = 1;
2266 	out:
2267 		mtx_unlock(&subj->mtx);
2268 		PROC_UNLOCK(p);
2269 		if (dodrop)
2270 			mac_proc_vm_revoke(curthread);
2271 	} else {
2272 		mtx_unlock(&subj->mtx);
2273 	}
2274 }
2275 
2276 static int
2277 lomac_vnode_associate_extattr(struct mount *mp, struct label *mplabel,
2278     struct vnode *vp, struct label *vplabel)
2279 {
2280 	struct mac_lomac ml_temp, *source, *dest;
2281 	int buflen, error;
2282 
2283 	source = SLOT(mplabel);
2284 	dest = SLOT(vplabel);
2285 
2286 	buflen = sizeof(ml_temp);
2287 	bzero(&ml_temp, buflen);
2288 
2289 	error = vn_extattr_get(vp, IO_NODELOCKED, MAC_LOMAC_EXTATTR_NAMESPACE,
2290 	    MAC_LOMAC_EXTATTR_NAME, &buflen, (char *)&ml_temp, curthread);
2291 	if (error == ENOATTR || error == EOPNOTSUPP) {
2292 		/* Fall back to the mntlabel. */
2293 		lomac_copy_single(source, dest);
2294 		return (0);
2295 	} else if (error)
2296 		return (error);
2297 
2298 	if (buflen != sizeof(ml_temp)) {
2299 		if (buflen != sizeof(ml_temp) - sizeof(ml_temp.ml_auxsingle)) {
2300 			printf("lomac_vnode_associate_extattr: bad size %d\n",
2301 			    buflen);
2302 			return (EPERM);
2303 		}
2304 		bzero(&ml_temp.ml_auxsingle, sizeof(ml_temp.ml_auxsingle));
2305 		buflen = sizeof(ml_temp);
2306 		(void)vn_extattr_set(vp, IO_NODELOCKED,
2307 		    MAC_LOMAC_EXTATTR_NAMESPACE, MAC_LOMAC_EXTATTR_NAME,
2308 		    buflen, (char *)&ml_temp, curthread);
2309 	}
2310 	if (lomac_valid(&ml_temp) != 0) {
2311 		printf("lomac_vnode_associate_extattr: invalid\n");
2312 		return (EPERM);
2313 	}
2314 	if ((ml_temp.ml_flags & MAC_LOMAC_FLAGS_BOTH) !=
2315 	    MAC_LOMAC_FLAG_SINGLE) {
2316 		printf("lomac_vnode_associate_extattr: not single\n");
2317 		return (EPERM);
2318 	}
2319 
2320 	lomac_copy_single(&ml_temp, dest);
2321 	return (0);
2322 }
2323 
2324 static void
2325 lomac_vnode_associate_singlelabel(struct mount *mp, struct label *mplabel,
2326     struct vnode *vp, struct label *vplabel)
2327 {
2328 	struct mac_lomac *source, *dest;
2329 
2330 	source = SLOT(mplabel);
2331 	dest = SLOT(vplabel);
2332 
2333 	lomac_copy_single(source, dest);
2334 }
2335 
2336 static int
2337 lomac_vnode_check_create(struct ucred *cred, struct vnode *dvp,
2338     struct label *dvplabel, struct componentname *cnp, struct vattr *vap)
2339 {
2340 	struct mac_lomac *subj, *obj;
2341 
2342 	if (!lomac_enabled)
2343 		return (0);
2344 
2345 	subj = SLOT(cred->cr_label);
2346 	obj = SLOT(dvplabel);
2347 
2348 	if (!lomac_subject_dominate(subj, obj))
2349 		return (EACCES);
2350 	if (obj->ml_flags & MAC_LOMAC_FLAG_AUX &&
2351 	    !lomac_dominate_element(&subj->ml_single, &obj->ml_auxsingle))
2352 		return (EACCES);
2353 
2354 	return (0);
2355 }
2356 
2357 static int
2358 lomac_vnode_check_deleteacl(struct ucred *cred, struct vnode *vp,
2359     struct label *vplabel, acl_type_t type)
2360 {
2361 	struct mac_lomac *subj, *obj;
2362 
2363 	if (!lomac_enabled)
2364 		return (0);
2365 
2366 	subj = SLOT(cred->cr_label);
2367 	obj = SLOT(vplabel);
2368 
2369 	if (!lomac_subject_dominate(subj, obj))
2370 		return (EACCES);
2371 
2372 	return (0);
2373 }
2374 
2375 static int
2376 lomac_vnode_check_link(struct ucred *cred, struct vnode *dvp,
2377     struct label *dvplabel, struct vnode *vp, struct label *vplabel,
2378     struct componentname *cnp)
2379 {
2380 	struct mac_lomac *subj, *obj;
2381 
2382 	if (!lomac_enabled)
2383 		return (0);
2384 
2385 	subj = SLOT(cred->cr_label);
2386 	obj = SLOT(dvplabel);
2387 
2388 	if (!lomac_subject_dominate(subj, obj))
2389 		return (EACCES);
2390 
2391 	obj = SLOT(vplabel);
2392 
2393 	if (!lomac_subject_dominate(subj, obj))
2394 		return (EACCES);
2395 
2396 	return (0);
2397 }
2398 
2399 static int
2400 lomac_vnode_check_mmap(struct ucred *cred, struct vnode *vp,
2401     struct label *vplabel, int prot, int flags)
2402 {
2403 	struct mac_lomac *subj, *obj;
2404 
2405 	/*
2406 	 * Rely on the use of open()-time protections to handle
2407 	 * non-revocation cases.
2408 	 */
2409 	if (!lomac_enabled)
2410 		return (0);
2411 
2412 	subj = SLOT(cred->cr_label);
2413 	obj = SLOT(vplabel);
2414 
2415 	if (((prot & VM_PROT_WRITE) != 0) && ((flags & MAP_SHARED) != 0)) {
2416 		if (!lomac_subject_dominate(subj, obj))
2417 			return (EACCES);
2418 	}
2419 	if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
2420 		if (!lomac_dominate_single(obj, subj))
2421 			return (maybe_demote(subj, obj, "mapping", "file", vp));
2422 	}
2423 
2424 	return (0);
2425 }
2426 
2427 static void
2428 lomac_vnode_check_mmap_downgrade(struct ucred *cred, struct vnode *vp,
2429     struct label *vplabel, /* XXX vm_prot_t */ int *prot)
2430 {
2431 	struct mac_lomac *subj, *obj;
2432 
2433 	/*
2434 	 * Rely on the use of open()-time protections to handle
2435 	 * non-revocation cases.
2436 	 */
2437 	if (!lomac_enabled || !revocation_enabled)
2438 		return;
2439 
2440 	subj = SLOT(cred->cr_label);
2441 	obj = SLOT(vplabel);
2442 
2443 	if (!lomac_subject_dominate(subj, obj))
2444 		*prot &= ~VM_PROT_WRITE;
2445 }
2446 
2447 static int
2448 lomac_vnode_check_open(struct ucred *cred, struct vnode *vp,
2449     struct label *vplabel, accmode_t accmode)
2450 {
2451 	struct mac_lomac *subj, *obj;
2452 
2453 	if (!lomac_enabled)
2454 		return (0);
2455 
2456 	subj = SLOT(cred->cr_label);
2457 	obj = SLOT(vplabel);
2458 
2459 	/* XXX privilege override for admin? */
2460 	if (accmode & VMODIFY_PERMS) {
2461 		if (!lomac_subject_dominate(subj, obj))
2462 			return (EACCES);
2463 	}
2464 
2465 	return (0);
2466 }
2467 
2468 static int
2469 lomac_vnode_check_read(struct ucred *active_cred, struct ucred *file_cred,
2470     struct vnode *vp, struct label *vplabel)
2471 {
2472 	struct mac_lomac *subj, *obj;
2473 
2474 	if (!lomac_enabled || !revocation_enabled)
2475 		return (0);
2476 
2477 	subj = SLOT(active_cred->cr_label);
2478 	obj = SLOT(vplabel);
2479 
2480 	if (!lomac_dominate_single(obj, subj))
2481 		return (maybe_demote(subj, obj, "reading", "file", vp));
2482 
2483 	return (0);
2484 }
2485 
2486 static int
2487 lomac_vnode_check_relabel(struct ucred *cred, struct vnode *vp,
2488     struct label *vplabel, struct label *newlabel)
2489 {
2490 	struct mac_lomac *old, *new, *subj;
2491 	int error;
2492 
2493 	old = SLOT(vplabel);
2494 	new = SLOT(newlabel);
2495 	subj = SLOT(cred->cr_label);
2496 
2497 	/*
2498 	 * If there is a LOMAC label update for the vnode, it must be a
2499 	 * single label, with an optional explicit auxiliary single.
2500 	 */
2501 	error = lomac_atmostflags(new,
2502 	    MAC_LOMAC_FLAG_SINGLE | MAC_LOMAC_FLAG_AUX);
2503 	if (error)
2504 		return (error);
2505 
2506 	/*
2507 	 * To perform a relabel of the vnode (LOMAC label or not), LOMAC must
2508 	 * authorize the relabel.
2509 	 */
2510 	if (!lomac_single_in_range(old, subj))
2511 		return (EPERM);
2512 
2513 	/*
2514 	 * If the LOMAC label is to be changed, authorize as appropriate.
2515 	 */
2516 	if (new->ml_flags & MAC_LOMAC_FLAG_SINGLE) {
2517 		/*
2518 		 * To change the LOMAC label on a vnode, the new vnode label
2519 		 * must be in the subject range.
2520 		 */
2521 		if (!lomac_single_in_range(new, subj))
2522 			return (EPERM);
2523 
2524 		/*
2525 		 * To change the LOMAC label on the vnode to be EQUAL, the
2526 		 * subject must have appropriate privilege.
2527 		 */
2528 		if (lomac_contains_equal(new)) {
2529 			error = lomac_subject_privileged(subj);
2530 			if (error)
2531 				return (error);
2532 		}
2533 	}
2534 	if (new->ml_flags & MAC_LOMAC_FLAG_AUX) {
2535 		/*
2536 		 * Fill in the missing parts from the previous label.
2537 		 */
2538 		if ((new->ml_flags & MAC_LOMAC_FLAG_SINGLE) == 0)
2539 			lomac_copy_single(subj, new);
2540 
2541 		/*
2542 		 * To change the auxiliary LOMAC label on a vnode, the new
2543 		 * vnode label must be in the subject range.
2544 		 */
2545 		if (!lomac_auxsingle_in_range(new, subj))
2546 			return (EPERM);
2547 
2548 		/*
2549 		 * To change the auxiliary LOMAC label on the vnode to be
2550 		 * EQUAL, the subject must have appropriate privilege.
2551 		 */
2552 		if (lomac_contains_equal(new)) {
2553 			error = lomac_subject_privileged(subj);
2554 			if (error)
2555 				return (error);
2556 		}
2557 	}
2558 
2559 	return (0);
2560 }
2561 
2562 static int
2563 lomac_vnode_check_rename_from(struct ucred *cred, struct vnode *dvp,
2564     struct label *dvplabel, struct vnode *vp, struct label *vplabel,
2565     struct componentname *cnp)
2566 {
2567 	struct mac_lomac *subj, *obj;
2568 
2569 	if (!lomac_enabled)
2570 		return (0);
2571 
2572 	subj = SLOT(cred->cr_label);
2573 	obj = SLOT(dvplabel);
2574 
2575 	if (!lomac_subject_dominate(subj, obj))
2576 		return (EACCES);
2577 
2578 	obj = SLOT(vplabel);
2579 
2580 	if (!lomac_subject_dominate(subj, obj))
2581 		return (EACCES);
2582 
2583 	return (0);
2584 }
2585 
2586 static int
2587 lomac_vnode_check_rename_to(struct ucred *cred, struct vnode *dvp,
2588     struct label *dvplabel, struct vnode *vp, struct label *vplabel,
2589     int samedir, struct componentname *cnp)
2590 {
2591 	struct mac_lomac *subj, *obj;
2592 
2593 	if (!lomac_enabled)
2594 		return (0);
2595 
2596 	subj = SLOT(cred->cr_label);
2597 	obj = SLOT(dvplabel);
2598 
2599 	if (!lomac_subject_dominate(subj, obj))
2600 		return (EACCES);
2601 
2602 	if (vp != NULL) {
2603 		obj = SLOT(vplabel);
2604 
2605 		if (!lomac_subject_dominate(subj, obj))
2606 			return (EACCES);
2607 	}
2608 
2609 	return (0);
2610 }
2611 
2612 static int
2613 lomac_vnode_check_revoke(struct ucred *cred, struct vnode *vp,
2614     struct label *vplabel)
2615 {
2616 	struct mac_lomac *subj, *obj;
2617 
2618 	if (!lomac_enabled)
2619 		return (0);
2620 
2621 	subj = SLOT(cred->cr_label);
2622 	obj = SLOT(vplabel);
2623 
2624 	if (!lomac_subject_dominate(subj, obj))
2625 		return (EACCES);
2626 
2627 	return (0);
2628 }
2629 
2630 static int
2631 lomac_vnode_check_setacl(struct ucred *cred, struct vnode *vp,
2632     struct label *vplabel, acl_type_t type, struct acl *acl)
2633 {
2634 	struct mac_lomac *subj, *obj;
2635 
2636 	if (!lomac_enabled)
2637 		return (0);
2638 
2639 	subj = SLOT(cred->cr_label);
2640 	obj = SLOT(vplabel);
2641 
2642 	if (!lomac_subject_dominate(subj, obj))
2643 		return (EACCES);
2644 
2645 	return (0);
2646 }
2647 
2648 static int
2649 lomac_vnode_check_setextattr(struct ucred *cred, struct vnode *vp,
2650     struct label *vplabel, int attrnamespace, const char *name)
2651 {
2652 	struct mac_lomac *subj, *obj;
2653 
2654 	if (!lomac_enabled)
2655 		return (0);
2656 
2657 	subj = SLOT(cred->cr_label);
2658 	obj = SLOT(vplabel);
2659 
2660 	if (!lomac_subject_dominate(subj, obj))
2661 		return (EACCES);
2662 
2663 	/* XXX: protect the MAC EA in a special way? */
2664 
2665 	return (0);
2666 }
2667 
2668 static int
2669 lomac_vnode_check_setflags(struct ucred *cred, struct vnode *vp,
2670     struct label *vplabel, u_long flags)
2671 {
2672 	struct mac_lomac *subj, *obj;
2673 
2674 	if (!lomac_enabled)
2675 		return (0);
2676 
2677 	subj = SLOT(cred->cr_label);
2678 	obj = SLOT(vplabel);
2679 
2680 	if (!lomac_subject_dominate(subj, obj))
2681 		return (EACCES);
2682 
2683 	return (0);
2684 }
2685 
2686 static int
2687 lomac_vnode_check_setmode(struct ucred *cred, struct vnode *vp,
2688     struct label *vplabel, mode_t mode)
2689 {
2690 	struct mac_lomac *subj, *obj;
2691 
2692 	if (!lomac_enabled)
2693 		return (0);
2694 
2695 	subj = SLOT(cred->cr_label);
2696 	obj = SLOT(vplabel);
2697 
2698 	if (!lomac_subject_dominate(subj, obj))
2699 		return (EACCES);
2700 
2701 	return (0);
2702 }
2703 
2704 static int
2705 lomac_vnode_check_setowner(struct ucred *cred, struct vnode *vp,
2706     struct label *vplabel, uid_t uid, gid_t gid)
2707 {
2708 	struct mac_lomac *subj, *obj;
2709 
2710 	if (!lomac_enabled)
2711 		return (0);
2712 
2713 	subj = SLOT(cred->cr_label);
2714 	obj = SLOT(vplabel);
2715 
2716 	if (!lomac_subject_dominate(subj, obj))
2717 		return (EACCES);
2718 
2719 	return (0);
2720 }
2721 
2722 static int
2723 lomac_vnode_check_setutimes(struct ucred *cred, struct vnode *vp,
2724     struct label *vplabel, struct timespec atime, struct timespec mtime)
2725 {
2726 	struct mac_lomac *subj, *obj;
2727 
2728 	if (!lomac_enabled)
2729 		return (0);
2730 
2731 	subj = SLOT(cred->cr_label);
2732 	obj = SLOT(vplabel);
2733 
2734 	if (!lomac_subject_dominate(subj, obj))
2735 		return (EACCES);
2736 
2737 	return (0);
2738 }
2739 
2740 static int
2741 lomac_vnode_check_unlink(struct ucred *cred, struct vnode *dvp,
2742     struct label *dvplabel, struct vnode *vp, struct label *vplabel,
2743     struct componentname *cnp)
2744 {
2745 	struct mac_lomac *subj, *obj;
2746 
2747 	if (!lomac_enabled)
2748 		return (0);
2749 
2750 	subj = SLOT(cred->cr_label);
2751 	obj = SLOT(dvplabel);
2752 
2753 	if (!lomac_subject_dominate(subj, obj))
2754 		return (EACCES);
2755 
2756 	obj = SLOT(vplabel);
2757 
2758 	if (!lomac_subject_dominate(subj, obj))
2759 		return (EACCES);
2760 
2761 	return (0);
2762 }
2763 
2764 static int
2765 lomac_vnode_check_write(struct ucred *active_cred,
2766     struct ucred *file_cred, struct vnode *vp, struct label *vplabel)
2767 {
2768 	struct mac_lomac *subj, *obj;
2769 
2770 	if (!lomac_enabled || !revocation_enabled)
2771 		return (0);
2772 
2773 	subj = SLOT(active_cred->cr_label);
2774 	obj = SLOT(vplabel);
2775 
2776 	if (!lomac_subject_dominate(subj, obj))
2777 		return (EACCES);
2778 
2779 	return (0);
2780 }
2781 
2782 static int
2783 lomac_vnode_create_extattr(struct ucred *cred, struct mount *mp,
2784     struct label *mplabel, struct vnode *dvp, struct label *dvplabel,
2785     struct vnode *vp, struct label *vplabel, struct componentname *cnp)
2786 {
2787 	struct mac_lomac *source, *dest, *dir, temp;
2788 	size_t buflen;
2789 	int error;
2790 
2791 	buflen = sizeof(temp);
2792 	bzero(&temp, buflen);
2793 
2794 	source = SLOT(cred->cr_label);
2795 	dest = SLOT(vplabel);
2796 	dir = SLOT(dvplabel);
2797 	if (dir->ml_flags & MAC_LOMAC_FLAG_AUX) {
2798 		lomac_copy_auxsingle(dir, &temp);
2799 		lomac_set_single(&temp, dir->ml_auxsingle.mle_type,
2800 		    dir->ml_auxsingle.mle_grade);
2801 	} else {
2802 		lomac_copy_single(source, &temp);
2803 	}
2804 
2805 	error = vn_extattr_set(vp, IO_NODELOCKED, MAC_LOMAC_EXTATTR_NAMESPACE,
2806 	    MAC_LOMAC_EXTATTR_NAME, buflen, (char *)&temp, curthread);
2807 	if (error == 0)
2808 		lomac_copy(&temp, dest);
2809 	return (error);
2810 }
2811 
2812 static void
2813 lomac_vnode_execve_transition(struct ucred *old, struct ucred *new,
2814     struct vnode *vp, struct label *vplabel, struct label *interpvplabel,
2815     struct image_params *imgp, struct label *execlabel)
2816 {
2817 	struct mac_lomac *source, *dest, *obj, *robj;
2818 
2819 	source = SLOT(old->cr_label);
2820 	dest = SLOT(new->cr_label);
2821 	obj = SLOT(vplabel);
2822 	robj = interpvplabel != NULL ? SLOT(interpvplabel) : obj;
2823 
2824 	lomac_copy(source, dest);
2825 	/*
2826 	 * If there's an auxiliary label on the real object, respect it and
2827 	 * assume that this level should be assumed immediately if a higher
2828 	 * level is currently in place.
2829 	 */
2830 	if (robj->ml_flags & MAC_LOMAC_FLAG_AUX &&
2831 	    !lomac_dominate_element(&robj->ml_auxsingle, &dest->ml_single)
2832 	    && lomac_auxsingle_in_range(robj, dest))
2833 		lomac_set_single(dest, robj->ml_auxsingle.mle_type,
2834 		    robj->ml_auxsingle.mle_grade);
2835 	/*
2836 	 * Restructuring to use the execve transitioning mechanism instead of
2837 	 * the normal demotion mechanism here would be difficult, so just
2838 	 * copy the label over and perform standard demotion.  This is also
2839 	 * non-optimal because it will result in the intermediate label "new"
2840 	 * being created and immediately recycled.
2841 	 */
2842 	if (lomac_enabled && revocation_enabled &&
2843 	    !lomac_dominate_single(obj, source))
2844 		(void)maybe_demote(source, obj, "executing", "file", vp);
2845 }
2846 
2847 static int
2848 lomac_vnode_execve_will_transition(struct ucred *old, struct vnode *vp,
2849     struct label *vplabel, struct label *interpvplabel,
2850     struct image_params *imgp, struct label *execlabel)
2851 {
2852 	struct mac_lomac *subj, *obj, *robj;
2853 
2854 	if (!lomac_enabled || !revocation_enabled)
2855 		return (0);
2856 
2857 	subj = SLOT(old->cr_label);
2858 	obj = SLOT(vplabel);
2859 	robj = interpvplabel != NULL ? SLOT(interpvplabel) : obj;
2860 
2861 	return ((robj->ml_flags & MAC_LOMAC_FLAG_AUX &&
2862 	    !lomac_dominate_element(&robj->ml_auxsingle, &subj->ml_single)
2863 	    && lomac_auxsingle_in_range(robj, subj)) ||
2864 	    !lomac_dominate_single(obj, subj));
2865 }
2866 
2867 static void
2868 lomac_vnode_relabel(struct ucred *cred, struct vnode *vp,
2869     struct label *vplabel, struct label *newlabel)
2870 {
2871 	struct mac_lomac *source, *dest;
2872 
2873 	source = SLOT(newlabel);
2874 	dest = SLOT(vplabel);
2875 
2876 	try_relabel(source, dest);
2877 }
2878 
2879 static int
2880 lomac_vnode_setlabel_extattr(struct ucred *cred, struct vnode *vp,
2881     struct label *vplabel, struct label *intlabel)
2882 {
2883 	struct mac_lomac *source, temp;
2884 	size_t buflen;
2885 	int error;
2886 
2887 	buflen = sizeof(temp);
2888 	bzero(&temp, buflen);
2889 
2890 	source = SLOT(intlabel);
2891 	if ((source->ml_flags & MAC_LOMAC_FLAG_SINGLE) == 0)
2892 		return (0);
2893 
2894 	lomac_copy_single(source, &temp);
2895 	error = vn_extattr_set(vp, IO_NODELOCKED, MAC_LOMAC_EXTATTR_NAMESPACE,
2896 	    MAC_LOMAC_EXTATTR_NAME, buflen, (char *)&temp, curthread);
2897 	return (error);
2898 }
2899 
2900 static struct mac_policy_ops lomac_ops =
2901 {
2902 	.mpo_init = lomac_init,
2903 	.mpo_destroy = lomac_fini,
2904 
2905 	.mpo_bpfdesc_check_receive = lomac_bpfdesc_check_receive,
2906 	.mpo_bpfdesc_create = lomac_bpfdesc_create,
2907 	.mpo_bpfdesc_create_mbuf = lomac_bpfdesc_create_mbuf,
2908 	.mpo_bpfdesc_destroy_label = lomac_destroy_label,
2909 	.mpo_bpfdesc_init_label = lomac_init_label,
2910 
2911 	.mpo_cred_check_relabel = lomac_cred_check_relabel,
2912 	.mpo_cred_check_visible = lomac_cred_check_visible,
2913 	.mpo_cred_copy_label = lomac_copy_label,
2914 	.mpo_cred_create_swapper = lomac_cred_create_swapper,
2915 	.mpo_cred_create_init = lomac_cred_create_init,
2916 	.mpo_cred_destroy_label = lomac_destroy_label,
2917 	.mpo_cred_externalize_label = lomac_externalize_label,
2918 	.mpo_cred_init_label = lomac_init_label,
2919 	.mpo_cred_internalize_label = lomac_internalize_label,
2920 	.mpo_cred_relabel = lomac_cred_relabel,
2921 
2922 	.mpo_devfs_create_device = lomac_devfs_create_device,
2923 	.mpo_devfs_create_directory = lomac_devfs_create_directory,
2924 	.mpo_devfs_create_symlink = lomac_devfs_create_symlink,
2925 	.mpo_devfs_destroy_label = lomac_destroy_label,
2926 	.mpo_devfs_init_label = lomac_init_label,
2927 	.mpo_devfs_update = lomac_devfs_update,
2928 	.mpo_devfs_vnode_associate = lomac_devfs_vnode_associate,
2929 
2930 	.mpo_ifnet_check_relabel = lomac_ifnet_check_relabel,
2931 	.mpo_ifnet_check_transmit = lomac_ifnet_check_transmit,
2932 	.mpo_ifnet_copy_label = lomac_copy_label,
2933 	.mpo_ifnet_create = lomac_ifnet_create,
2934 	.mpo_ifnet_create_mbuf = lomac_ifnet_create_mbuf,
2935 	.mpo_ifnet_destroy_label = lomac_destroy_label,
2936 	.mpo_ifnet_externalize_label = lomac_externalize_label,
2937 	.mpo_ifnet_init_label = lomac_init_label,
2938 	.mpo_ifnet_internalize_label = lomac_internalize_label,
2939 	.mpo_ifnet_relabel = lomac_ifnet_relabel,
2940 
2941 	.mpo_syncache_create = lomac_syncache_create,
2942 	.mpo_syncache_destroy_label = lomac_destroy_label,
2943 	.mpo_syncache_init_label = lomac_init_label_waitcheck,
2944 
2945 	.mpo_inpcb_check_deliver = lomac_inpcb_check_deliver,
2946 	.mpo_inpcb_check_visible = lomac_inpcb_check_visible,
2947 	.mpo_inpcb_create = lomac_inpcb_create,
2948 	.mpo_inpcb_create_mbuf = lomac_inpcb_create_mbuf,
2949 	.mpo_inpcb_destroy_label = lomac_destroy_label,
2950 	.mpo_inpcb_init_label = lomac_init_label_waitcheck,
2951 	.mpo_inpcb_sosetlabel = lomac_inpcb_sosetlabel,
2952 
2953 	.mpo_ip6q_create = lomac_ip6q_create,
2954 	.mpo_ip6q_destroy_label = lomac_destroy_label,
2955 	.mpo_ip6q_init_label = lomac_init_label_waitcheck,
2956 	.mpo_ip6q_match = lomac_ip6q_match,
2957 	.mpo_ip6q_reassemble = lomac_ip6q_reassemble,
2958 	.mpo_ip6q_update = lomac_ip6q_update,
2959 
2960 	.mpo_ipq_create = lomac_ipq_create,
2961 	.mpo_ipq_destroy_label = lomac_destroy_label,
2962 	.mpo_ipq_init_label = lomac_init_label_waitcheck,
2963 	.mpo_ipq_match = lomac_ipq_match,
2964 	.mpo_ipq_reassemble = lomac_ipq_reassemble,
2965 	.mpo_ipq_update = lomac_ipq_update,
2966 
2967 	.mpo_kld_check_load = lomac_kld_check_load,
2968 
2969 	.mpo_mbuf_copy_label = lomac_copy_label,
2970 	.mpo_mbuf_destroy_label = lomac_destroy_label,
2971 	.mpo_mbuf_init_label = lomac_init_label_waitcheck,
2972 
2973 	.mpo_mount_create = lomac_mount_create,
2974 	.mpo_mount_destroy_label = lomac_destroy_label,
2975 	.mpo_mount_init_label = lomac_init_label,
2976 
2977 	.mpo_netinet_arp_send = lomac_netinet_arp_send,
2978 	.mpo_netinet_firewall_reply = lomac_netinet_firewall_reply,
2979 	.mpo_netinet_firewall_send = lomac_netinet_firewall_send,
2980 	.mpo_netinet_fragment = lomac_netinet_fragment,
2981 	.mpo_netinet_icmp_reply = lomac_netinet_icmp_reply,
2982 	.mpo_netinet_igmp_send = lomac_netinet_igmp_send,
2983 
2984 	.mpo_netinet6_nd6_send = lomac_netinet6_nd6_send,
2985 
2986 	.mpo_pipe_check_ioctl = lomac_pipe_check_ioctl,
2987 	.mpo_pipe_check_read = lomac_pipe_check_read,
2988 	.mpo_pipe_check_relabel = lomac_pipe_check_relabel,
2989 	.mpo_pipe_check_write = lomac_pipe_check_write,
2990 	.mpo_pipe_copy_label = lomac_copy_label,
2991 	.mpo_pipe_create = lomac_pipe_create,
2992 	.mpo_pipe_destroy_label = lomac_destroy_label,
2993 	.mpo_pipe_externalize_label = lomac_externalize_label,
2994 	.mpo_pipe_init_label = lomac_init_label,
2995 	.mpo_pipe_internalize_label = lomac_internalize_label,
2996 	.mpo_pipe_relabel = lomac_pipe_relabel,
2997 
2998 	.mpo_priv_check = lomac_priv_check,
2999 
3000 	.mpo_proc_check_debug = lomac_proc_check_debug,
3001 	.mpo_proc_check_sched = lomac_proc_check_sched,
3002 	.mpo_proc_check_signal = lomac_proc_check_signal,
3003 	.mpo_proc_destroy_label = lomac_proc_destroy_label,
3004 	.mpo_proc_init_label = lomac_proc_init_label,
3005 
3006 	.mpo_socket_check_deliver = lomac_socket_check_deliver,
3007 	.mpo_socket_check_relabel = lomac_socket_check_relabel,
3008 	.mpo_socket_check_visible = lomac_socket_check_visible,
3009 	.mpo_socket_copy_label = lomac_copy_label,
3010 	.mpo_socket_create = lomac_socket_create,
3011 	.mpo_socket_create_mbuf = lomac_socket_create_mbuf,
3012 	.mpo_socket_destroy_label = lomac_destroy_label,
3013 	.mpo_socket_externalize_label = lomac_externalize_label,
3014 	.mpo_socket_init_label = lomac_init_label_waitcheck,
3015 	.mpo_socket_internalize_label = lomac_internalize_label,
3016 	.mpo_socket_newconn = lomac_socket_newconn,
3017 	.mpo_socket_relabel = lomac_socket_relabel,
3018 
3019 	.mpo_socketpeer_destroy_label = lomac_destroy_label,
3020 	.mpo_socketpeer_externalize_label = lomac_externalize_label,
3021 	.mpo_socketpeer_init_label = lomac_init_label_waitcheck,
3022 	.mpo_socketpeer_set_from_mbuf = lomac_socketpeer_set_from_mbuf,
3023 	.mpo_socketpeer_set_from_socket = lomac_socketpeer_set_from_socket,
3024 
3025 	.mpo_syncache_create_mbuf = lomac_syncache_create_mbuf,
3026 
3027 	.mpo_system_check_acct = lomac_system_check_acct,
3028 	.mpo_system_check_auditctl = lomac_system_check_auditctl,
3029 	.mpo_system_check_swapoff = lomac_system_check_swapoff,
3030 	.mpo_system_check_swapon = lomac_system_check_swapon,
3031 	.mpo_system_check_sysctl = lomac_system_check_sysctl,
3032 
3033 	.mpo_thread_userret = lomac_thread_userret,
3034 
3035 	.mpo_vnode_associate_extattr = lomac_vnode_associate_extattr,
3036 	.mpo_vnode_associate_singlelabel = lomac_vnode_associate_singlelabel,
3037 	.mpo_vnode_check_access = lomac_vnode_check_open,
3038 	.mpo_vnode_check_create = lomac_vnode_check_create,
3039 	.mpo_vnode_check_deleteacl = lomac_vnode_check_deleteacl,
3040 	.mpo_vnode_check_link = lomac_vnode_check_link,
3041 	.mpo_vnode_check_mmap = lomac_vnode_check_mmap,
3042 	.mpo_vnode_check_mmap_downgrade = lomac_vnode_check_mmap_downgrade,
3043 	.mpo_vnode_check_open = lomac_vnode_check_open,
3044 	.mpo_vnode_check_read = lomac_vnode_check_read,
3045 	.mpo_vnode_check_relabel = lomac_vnode_check_relabel,
3046 	.mpo_vnode_check_rename_from = lomac_vnode_check_rename_from,
3047 	.mpo_vnode_check_rename_to = lomac_vnode_check_rename_to,
3048 	.mpo_vnode_check_revoke = lomac_vnode_check_revoke,
3049 	.mpo_vnode_check_setacl = lomac_vnode_check_setacl,
3050 	.mpo_vnode_check_setextattr = lomac_vnode_check_setextattr,
3051 	.mpo_vnode_check_setflags = lomac_vnode_check_setflags,
3052 	.mpo_vnode_check_setmode = lomac_vnode_check_setmode,
3053 	.mpo_vnode_check_setowner = lomac_vnode_check_setowner,
3054 	.mpo_vnode_check_setutimes = lomac_vnode_check_setutimes,
3055 	.mpo_vnode_check_unlink = lomac_vnode_check_unlink,
3056 	.mpo_vnode_check_write = lomac_vnode_check_write,
3057 	.mpo_vnode_copy_label = lomac_copy_label,
3058 	.mpo_vnode_create_extattr = lomac_vnode_create_extattr,
3059 	.mpo_vnode_destroy_label = lomac_destroy_label,
3060 	.mpo_vnode_execve_transition = lomac_vnode_execve_transition,
3061 	.mpo_vnode_execve_will_transition = lomac_vnode_execve_will_transition,
3062 	.mpo_vnode_externalize_label = lomac_externalize_label,
3063 	.mpo_vnode_init_label = lomac_init_label,
3064 	.mpo_vnode_internalize_label = lomac_internalize_label,
3065 	.mpo_vnode_relabel = lomac_vnode_relabel,
3066 	.mpo_vnode_setlabel_extattr = lomac_vnode_setlabel_extattr,
3067 };
3068 
3069 MAC_POLICY_SET(&lomac_ops, mac_lomac, "TrustedBSD MAC/LOMAC",
3070     MPC_LOADTIME_FLAG_NOTLATE, &lomac_slot);
3071