xref: /freebsd/sys/security/mac_lomac/mac_lomac.c (revision 95ee2897)
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 		break;
1706 
1707 	/*
1708 	 * Allow access to general process debugging facilities.  We
1709 	 * separately control debugging based on MAC label.
1710 	 */
1711 	case PRIV_DEBUG_DIFFCRED:
1712 	case PRIV_DEBUG_SUGID:
1713 	case PRIV_DEBUG_UNPRIV:
1714 
1715 	/*
1716 	 * Allow manipulating jails.
1717 	 */
1718 	case PRIV_JAIL_ATTACH:
1719 
1720 	/*
1721 	 * Allow privilege with respect to the Partition policy, but not the
1722 	 * Privs policy.
1723 	 */
1724 	case PRIV_MAC_PARTITION:
1725 
1726 	/*
1727 	 * Allow privilege with respect to process resource limits and login
1728 	 * context.
1729 	 */
1730 	case PRIV_PROC_LIMIT:
1731 	case PRIV_PROC_SETLOGIN:
1732 	case PRIV_PROC_SETRLIMIT:
1733 
1734 	/*
1735 	 * Allow System V and POSIX IPC privileges.
1736 	 */
1737 	case PRIV_IPC_READ:
1738 	case PRIV_IPC_WRITE:
1739 	case PRIV_IPC_ADMIN:
1740 	case PRIV_IPC_MSGSIZE:
1741 	case PRIV_MQ_ADMIN:
1742 
1743 	/*
1744 	 * Allow certain scheduler manipulations -- possibly this should be
1745 	 * controlled by more fine-grained policy, as potentially low
1746 	 * integrity processes can deny CPU to higher integrity ones.
1747 	 */
1748 	case PRIV_SCHED_DIFFCRED:
1749 	case PRIV_SCHED_SETPRIORITY:
1750 	case PRIV_SCHED_RTPRIO:
1751 	case PRIV_SCHED_SETPOLICY:
1752 	case PRIV_SCHED_SET:
1753 	case PRIV_SCHED_SETPARAM:
1754 	case PRIV_SCHED_IDPRIO:
1755 
1756 	/*
1757 	 * More IPC privileges.
1758 	 */
1759 	case PRIV_SEM_WRITE:
1760 
1761 	/*
1762 	 * Allow signaling privileges subject to integrity policy.
1763 	 */
1764 	case PRIV_SIGNAL_DIFFCRED:
1765 	case PRIV_SIGNAL_SUGID:
1766 
1767 	/*
1768 	 * Allow access to only limited sysctls from lower integrity levels;
1769 	 * piggy-back on the Jail definition.
1770 	 */
1771 	case PRIV_SYSCTL_WRITEJAIL:
1772 
1773 	/*
1774 	 * Allow TTY-based privileges, subject to general device access using
1775 	 * labels on TTY device nodes, but not console privilege.
1776 	 */
1777 	case PRIV_TTY_DRAINWAIT:
1778 	case PRIV_TTY_DTRWAIT:
1779 	case PRIV_TTY_EXCLUSIVE:
1780 	case PRIV_TTY_STI:
1781 	case PRIV_TTY_SETA:
1782 
1783 	/*
1784 	 * Grant most VFS privileges, as almost all are in practice bounded
1785 	 * by more specific checks using labels.
1786 	 */
1787 	case PRIV_VFS_READ:
1788 	case PRIV_VFS_WRITE:
1789 	case PRIV_VFS_ADMIN:
1790 	case PRIV_VFS_EXEC:
1791 	case PRIV_VFS_LOOKUP:
1792 	case PRIV_VFS_CHFLAGS_DEV:
1793 	case PRIV_VFS_CHOWN:
1794 	case PRIV_VFS_CHROOT:
1795 	case PRIV_VFS_RETAINSUGID:
1796 	case PRIV_VFS_EXCEEDQUOTA:
1797 	case PRIV_VFS_FCHROOT:
1798 	case PRIV_VFS_FHOPEN:
1799 	case PRIV_VFS_FHSTATFS:
1800 	case PRIV_VFS_GENERATION:
1801 	case PRIV_VFS_GETFH:
1802 	case PRIV_VFS_GETQUOTA:
1803 	case PRIV_VFS_LINK:
1804 	case PRIV_VFS_MOUNT:
1805 	case PRIV_VFS_MOUNT_OWNER:
1806 	case PRIV_VFS_MOUNT_PERM:
1807 	case PRIV_VFS_MOUNT_SUIDDIR:
1808 	case PRIV_VFS_MOUNT_NONUSER:
1809 	case PRIV_VFS_SETGID:
1810 	case PRIV_VFS_STICKYFILE:
1811 	case PRIV_VFS_SYSFLAGS:
1812 	case PRIV_VFS_UNMOUNT:
1813 
1814 	/*
1815 	 * Allow VM privileges; it would be nice if these were subject to
1816 	 * resource limits.
1817 	 */
1818 	case PRIV_VM_MADV_PROTECT:
1819 	case PRIV_VM_MLOCK:
1820 	case PRIV_VM_MUNLOCK:
1821 	case PRIV_VM_SWAP_NOQUOTA:
1822 	case PRIV_VM_SWAP_NORLIMIT:
1823 
1824 	/*
1825 	 * Allow some but not all network privileges.  In general, dont allow
1826 	 * reconfiguring the network stack, just normal use.
1827 	 */
1828 	case PRIV_NETINET_RESERVEDPORT:
1829 	case PRIV_NETINET_RAW:
1830 	case PRIV_NETINET_REUSEPORT:
1831 		break;
1832 
1833 	/*
1834 	 * All remaining system privileges are allow only if the process
1835 	 * holds privilege with respect to the LOMAC policy.
1836 	 */
1837 	default:
1838 		subj = SLOT(cred->cr_label);
1839 		error = lomac_subject_privileged(subj);
1840 		if (error)
1841 			return (error);
1842 	}
1843 	return (0);
1844 }
1845 
1846 static int
1847 lomac_proc_check_debug(struct ucred *cred, struct proc *p)
1848 {
1849 	struct mac_lomac *subj, *obj;
1850 
1851 	if (!lomac_enabled)
1852 		return (0);
1853 
1854 	subj = SLOT(cred->cr_label);
1855 	obj = SLOT(p->p_ucred->cr_label);
1856 
1857 	/* XXX: range checks */
1858 	if (!lomac_dominate_single(obj, subj))
1859 		return (ESRCH);
1860 	if (!lomac_subject_dominate(subj, obj))
1861 		return (EACCES);
1862 
1863 	return (0);
1864 }
1865 
1866 static int
1867 lomac_proc_check_sched(struct ucred *cred, struct proc *p)
1868 {
1869 	struct mac_lomac *subj, *obj;
1870 
1871 	if (!lomac_enabled)
1872 		return (0);
1873 
1874 	subj = SLOT(cred->cr_label);
1875 	obj = SLOT(p->p_ucred->cr_label);
1876 
1877 	/* XXX: range checks */
1878 	if (!lomac_dominate_single(obj, subj))
1879 		return (ESRCH);
1880 	if (!lomac_subject_dominate(subj, obj))
1881 		return (EACCES);
1882 
1883 	return (0);
1884 }
1885 
1886 static int
1887 lomac_proc_check_signal(struct ucred *cred, struct proc *p, int signum)
1888 {
1889 	struct mac_lomac *subj, *obj;
1890 
1891 	if (!lomac_enabled)
1892 		return (0);
1893 
1894 	subj = SLOT(cred->cr_label);
1895 	obj = SLOT(p->p_ucred->cr_label);
1896 
1897 	/* XXX: range checks */
1898 	if (!lomac_dominate_single(obj, subj))
1899 		return (ESRCH);
1900 	if (!lomac_subject_dominate(subj, obj))
1901 		return (EACCES);
1902 
1903 	return (0);
1904 }
1905 
1906 static void
1907 lomac_proc_destroy_label(struct label *label)
1908 {
1909 
1910 	mtx_destroy(&PSLOT(label)->mtx);
1911 	free(PSLOT(label), M_LOMAC);
1912 	PSLOT_SET(label, NULL);
1913 }
1914 
1915 static void
1916 lomac_proc_init_label(struct label *label)
1917 {
1918 
1919 	PSLOT_SET(label, malloc(sizeof(struct mac_lomac_proc), M_LOMAC,
1920 	    M_ZERO | M_WAITOK));
1921 	mtx_init(&PSLOT(label)->mtx, "MAC/Lomac proc lock", NULL, MTX_DEF);
1922 }
1923 
1924 static int
1925 lomac_socket_check_deliver(struct socket *so, struct label *solabel,
1926     struct mbuf *m, struct label *mlabel)
1927 {
1928 	struct mac_lomac *p, *s;
1929 	int error;
1930 
1931 	if (!lomac_enabled)
1932 		return (0);
1933 
1934 	p = SLOT(mlabel);
1935 	s = SLOT(solabel);
1936 
1937 	SOCK_LOCK(so);
1938 	error = lomac_equal_single(p, s) ? 0 : EACCES;
1939 	SOCK_UNLOCK(so);
1940 	return (error);
1941 }
1942 
1943 static int
1944 lomac_socket_check_relabel(struct ucred *cred, struct socket *so,
1945     struct label *solabel, struct label *newlabel)
1946 {
1947 	struct mac_lomac *subj, *obj, *new;
1948 	int error;
1949 
1950 	SOCK_LOCK_ASSERT(so);
1951 
1952 	new = SLOT(newlabel);
1953 	subj = SLOT(cred->cr_label);
1954 	obj = SLOT(solabel);
1955 
1956 	/*
1957 	 * If there is a LOMAC label update for the socket, it may be an
1958 	 * update of single.
1959 	 */
1960 	error = lomac_atmostflags(new, MAC_LOMAC_FLAG_SINGLE);
1961 	if (error)
1962 		return (error);
1963 
1964 	/*
1965 	 * To relabel a socket, the old socket single must be in the subject
1966 	 * range.
1967 	 */
1968 	if (!lomac_single_in_range(obj, subj))
1969 		return (EPERM);
1970 
1971 	/*
1972 	 * If the LOMAC label is to be changed, authorize as appropriate.
1973 	 */
1974 	if (new->ml_flags & MAC_LOMAC_FLAG_SINGLE) {
1975 		/*
1976 		 * To relabel a socket, the new socket single must be in the
1977 		 * subject range.
1978 		 */
1979 		if (!lomac_single_in_range(new, subj))
1980 			return (EPERM);
1981 
1982 		/*
1983 		 * To change the LOMAC label on the socket to contain EQUAL,
1984 		 * the subject must have appropriate privilege.
1985 		 */
1986 		if (lomac_contains_equal(new)) {
1987 			error = lomac_subject_privileged(subj);
1988 			if (error)
1989 				return (error);
1990 		}
1991 	}
1992 
1993 	return (0);
1994 }
1995 
1996 static int
1997 lomac_socket_check_visible(struct ucred *cred, struct socket *so,
1998     struct label *solabel)
1999 {
2000 	struct mac_lomac *subj, *obj;
2001 
2002 	if (!lomac_enabled)
2003 		return (0);
2004 
2005 	subj = SLOT(cred->cr_label);
2006 	obj = SLOT(solabel);
2007 
2008 	SOCK_LOCK(so);
2009 	if (!lomac_dominate_single(obj, subj)) {
2010 		SOCK_UNLOCK(so);
2011 		return (ENOENT);
2012 	}
2013 	SOCK_UNLOCK(so);
2014 
2015 	return (0);
2016 }
2017 
2018 static void
2019 lomac_socket_create(struct ucred *cred, struct socket *so,
2020     struct label *solabel)
2021 {
2022 	struct mac_lomac *source, *dest;
2023 
2024 	source = SLOT(cred->cr_label);
2025 	dest = SLOT(solabel);
2026 
2027 	lomac_copy_single(source, dest);
2028 }
2029 
2030 static void
2031 lomac_socket_create_mbuf(struct socket *so, struct label *solabel,
2032     struct mbuf *m, struct label *mlabel)
2033 {
2034 	struct mac_lomac *source, *dest;
2035 
2036 	source = SLOT(solabel);
2037 	dest = SLOT(mlabel);
2038 
2039 	SOCK_LOCK(so);
2040 	lomac_copy_single(source, dest);
2041 	SOCK_UNLOCK(so);
2042 }
2043 
2044 static void
2045 lomac_socket_newconn(struct socket *oldso, struct label *oldsolabel,
2046     struct socket *newso, struct label *newsolabel)
2047 {
2048 	struct mac_lomac source, *dest;
2049 
2050 	SOCK_LOCK(oldso);
2051 	source = *SLOT(oldsolabel);
2052 	SOCK_UNLOCK(oldso);
2053 
2054 	dest = SLOT(newsolabel);
2055 
2056 	SOCK_LOCK(newso);
2057 	lomac_copy_single(&source, dest);
2058 	SOCK_UNLOCK(newso);
2059 }
2060 
2061 static void
2062 lomac_socket_relabel(struct ucred *cred, struct socket *so,
2063     struct label *solabel, struct label *newlabel)
2064 {
2065 	struct mac_lomac *source, *dest;
2066 
2067 	SOCK_LOCK_ASSERT(so);
2068 
2069 	source = SLOT(newlabel);
2070 	dest = SLOT(solabel);
2071 
2072 	try_relabel(source, dest);
2073 }
2074 
2075 static void
2076 lomac_socketpeer_set_from_mbuf(struct mbuf *m, struct label *mlabel,
2077     struct socket *so, struct label *sopeerlabel)
2078 {
2079 	struct mac_lomac *source, *dest;
2080 
2081 	source = SLOT(mlabel);
2082 	dest = SLOT(sopeerlabel);
2083 
2084 	SOCK_LOCK(so);
2085 	lomac_copy_single(source, dest);
2086 	SOCK_UNLOCK(so);
2087 }
2088 
2089 static void
2090 lomac_socketpeer_set_from_socket(struct socket *oldso,
2091     struct label *oldsolabel, struct socket *newso,
2092     struct label *newsopeerlabel)
2093 {
2094 	struct mac_lomac source, *dest;
2095 
2096 	SOCK_LOCK(oldso);
2097 	source = *SLOT(oldsolabel);
2098 	SOCK_UNLOCK(oldso);
2099 
2100 	dest = SLOT(newsopeerlabel);
2101 
2102 	SOCK_LOCK(newso);
2103 	lomac_copy_single(&source, dest);
2104 	SOCK_UNLOCK(newso);
2105 }
2106 
2107 static void
2108 lomac_syncache_create(struct label *label, struct inpcb *inp)
2109 {
2110 	struct mac_lomac *source, *dest;
2111 
2112 	source = SLOT(inp->inp_label);
2113 	dest = SLOT(label);
2114 	lomac_copy(source, dest);
2115 }
2116 
2117 static void
2118 lomac_syncache_create_mbuf(struct label *sc_label, struct mbuf *m,
2119     struct label *mlabel)
2120 {
2121 	struct mac_lomac *source, *dest;
2122 
2123 	source = SLOT(sc_label);
2124 	dest = SLOT(mlabel);
2125 	lomac_copy(source, dest);
2126 }
2127 
2128 static int
2129 lomac_system_check_acct(struct ucred *cred, struct vnode *vp,
2130     struct label *vplabel)
2131 {
2132 	struct mac_lomac *subj, *obj;
2133 
2134 	if (!lomac_enabled)
2135 		return (0);
2136 
2137 	subj = SLOT(cred->cr_label);
2138 	obj = SLOT(vplabel);
2139 
2140 	if (lomac_subject_privileged(subj))
2141 		return (EPERM);
2142 
2143 	if (!lomac_high_single(obj))
2144 		return (EACCES);
2145 
2146 	return (0);
2147 }
2148 
2149 static int
2150 lomac_system_check_auditctl(struct ucred *cred, struct vnode *vp,
2151     struct label *vplabel)
2152 {
2153 	struct mac_lomac *subj, *obj;
2154 
2155 	if (!lomac_enabled)
2156 		return (0);
2157 
2158 	subj = SLOT(cred->cr_label);
2159 	obj = SLOT(vplabel);
2160 
2161 	if (lomac_subject_privileged(subj))
2162 		return (EPERM);
2163 
2164 	if (!lomac_high_single(obj))
2165 		return (EACCES);
2166 
2167 	return (0);
2168 }
2169 
2170 static int
2171 lomac_system_check_swapoff(struct ucred *cred, struct vnode *vp,
2172     struct label *vplabel)
2173 {
2174 	struct mac_lomac *subj;
2175 
2176 	if (!lomac_enabled)
2177 		return (0);
2178 
2179 	subj = SLOT(cred->cr_label);
2180 
2181 	if (lomac_subject_privileged(subj))
2182 		return (EPERM);
2183 
2184 	return (0);
2185 }
2186 
2187 static int
2188 lomac_system_check_swapon(struct ucred *cred, struct vnode *vp,
2189     struct label *vplabel)
2190 {
2191 	struct mac_lomac *subj, *obj;
2192 
2193 	if (!lomac_enabled)
2194 		return (0);
2195 
2196 	subj = SLOT(cred->cr_label);
2197 	obj = SLOT(vplabel);
2198 
2199 	if (lomac_subject_privileged(subj))
2200 		return (EPERM);
2201 
2202 	if (!lomac_high_single(obj))
2203 		return (EACCES);
2204 
2205 	return (0);
2206 }
2207 
2208 static int
2209 lomac_system_check_sysctl(struct ucred *cred, struct sysctl_oid *oidp,
2210     void *arg1, int arg2, struct sysctl_req *req)
2211 {
2212 	struct mac_lomac *subj;
2213 
2214 	if (!lomac_enabled)
2215 		return (0);
2216 
2217 	subj = SLOT(cred->cr_label);
2218 
2219 	/*
2220 	 * Treat sysctl variables without CTLFLAG_ANYBODY flag as lomac/high,
2221 	 * but also require privilege to change them.
2222 	 */
2223 	if (req->newptr != NULL && (oidp->oid_kind & CTLFLAG_ANYBODY) == 0) {
2224 #ifdef notdef
2225 		if (!lomac_subject_dominate_high(subj))
2226 			return (EACCES);
2227 #endif
2228 
2229 		if (lomac_subject_privileged(subj))
2230 			return (EPERM);
2231 	}
2232 
2233 	return (0);
2234 }
2235 
2236 static void
2237 lomac_thread_userret(struct thread *td)
2238 {
2239 	struct proc *p = td->td_proc;
2240 	struct mac_lomac_proc *subj = PSLOT(p->p_label);
2241 	struct ucred *newcred, *oldcred;
2242 	int dodrop;
2243 
2244 	mtx_lock(&subj->mtx);
2245 	if (subj->mac_lomac.ml_flags & MAC_LOMAC_FLAG_UPDATE) {
2246 		dodrop = 0;
2247 		mtx_unlock(&subj->mtx);
2248 		newcred = crget();
2249 		PROC_LOCK(p);
2250 		mtx_lock(&subj->mtx);
2251 		/*
2252 		 * Check if we lost the race while allocating the cred.
2253 		 */
2254 		if ((subj->mac_lomac.ml_flags & MAC_LOMAC_FLAG_UPDATE) == 0) {
2255 			crfree(newcred);
2256 			goto out;
2257 		}
2258 		oldcred = p->p_ucred;
2259 		crcopy(newcred, oldcred);
2260 		crhold(newcred);
2261 		lomac_copy(&subj->mac_lomac, SLOT(newcred->cr_label));
2262 		proc_set_cred(p, newcred);
2263 		crfree(oldcred);
2264 		dodrop = 1;
2265 	out:
2266 		mtx_unlock(&subj->mtx);
2267 		PROC_UNLOCK(p);
2268 		if (dodrop)
2269 			mac_proc_vm_revoke(curthread);
2270 	} else {
2271 		mtx_unlock(&subj->mtx);
2272 	}
2273 }
2274 
2275 static int
2276 lomac_vnode_associate_extattr(struct mount *mp, struct label *mplabel,
2277     struct vnode *vp, struct label *vplabel)
2278 {
2279 	struct mac_lomac ml_temp, *source, *dest;
2280 	int buflen, error;
2281 
2282 	source = SLOT(mplabel);
2283 	dest = SLOT(vplabel);
2284 
2285 	buflen = sizeof(ml_temp);
2286 	bzero(&ml_temp, buflen);
2287 
2288 	error = vn_extattr_get(vp, IO_NODELOCKED, MAC_LOMAC_EXTATTR_NAMESPACE,
2289 	    MAC_LOMAC_EXTATTR_NAME, &buflen, (char *)&ml_temp, curthread);
2290 	if (error == ENOATTR || error == EOPNOTSUPP) {
2291 		/* Fall back to the mntlabel. */
2292 		lomac_copy_single(source, dest);
2293 		return (0);
2294 	} else if (error)
2295 		return (error);
2296 
2297 	if (buflen != sizeof(ml_temp)) {
2298 		if (buflen != sizeof(ml_temp) - sizeof(ml_temp.ml_auxsingle)) {
2299 			printf("lomac_vnode_associate_extattr: bad size %d\n",
2300 			    buflen);
2301 			return (EPERM);
2302 		}
2303 		bzero(&ml_temp.ml_auxsingle, sizeof(ml_temp.ml_auxsingle));
2304 		buflen = sizeof(ml_temp);
2305 		(void)vn_extattr_set(vp, IO_NODELOCKED,
2306 		    MAC_LOMAC_EXTATTR_NAMESPACE, MAC_LOMAC_EXTATTR_NAME,
2307 		    buflen, (char *)&ml_temp, curthread);
2308 	}
2309 	if (lomac_valid(&ml_temp) != 0) {
2310 		printf("lomac_vnode_associate_extattr: invalid\n");
2311 		return (EPERM);
2312 	}
2313 	if ((ml_temp.ml_flags & MAC_LOMAC_FLAGS_BOTH) !=
2314 	    MAC_LOMAC_FLAG_SINGLE) {
2315 		printf("lomac_vnode_associate_extattr: not single\n");
2316 		return (EPERM);
2317 	}
2318 
2319 	lomac_copy_single(&ml_temp, dest);
2320 	return (0);
2321 }
2322 
2323 static void
2324 lomac_vnode_associate_singlelabel(struct mount *mp, struct label *mplabel,
2325     struct vnode *vp, struct label *vplabel)
2326 {
2327 	struct mac_lomac *source, *dest;
2328 
2329 	source = SLOT(mplabel);
2330 	dest = SLOT(vplabel);
2331 
2332 	lomac_copy_single(source, dest);
2333 }
2334 
2335 static int
2336 lomac_vnode_check_create(struct ucred *cred, struct vnode *dvp,
2337     struct label *dvplabel, struct componentname *cnp, struct vattr *vap)
2338 {
2339 	struct mac_lomac *subj, *obj;
2340 
2341 	if (!lomac_enabled)
2342 		return (0);
2343 
2344 	subj = SLOT(cred->cr_label);
2345 	obj = SLOT(dvplabel);
2346 
2347 	if (!lomac_subject_dominate(subj, obj))
2348 		return (EACCES);
2349 	if (obj->ml_flags & MAC_LOMAC_FLAG_AUX &&
2350 	    !lomac_dominate_element(&subj->ml_single, &obj->ml_auxsingle))
2351 		return (EACCES);
2352 
2353 	return (0);
2354 }
2355 
2356 static int
2357 lomac_vnode_check_deleteacl(struct ucred *cred, struct vnode *vp,
2358     struct label *vplabel, acl_type_t type)
2359 {
2360 	struct mac_lomac *subj, *obj;
2361 
2362 	if (!lomac_enabled)
2363 		return (0);
2364 
2365 	subj = SLOT(cred->cr_label);
2366 	obj = SLOT(vplabel);
2367 
2368 	if (!lomac_subject_dominate(subj, obj))
2369 		return (EACCES);
2370 
2371 	return (0);
2372 }
2373 
2374 static int
2375 lomac_vnode_check_link(struct ucred *cred, struct vnode *dvp,
2376     struct label *dvplabel, struct vnode *vp, struct label *vplabel,
2377     struct componentname *cnp)
2378 {
2379 	struct mac_lomac *subj, *obj;
2380 
2381 	if (!lomac_enabled)
2382 		return (0);
2383 
2384 	subj = SLOT(cred->cr_label);
2385 	obj = SLOT(dvplabel);
2386 
2387 	if (!lomac_subject_dominate(subj, obj))
2388 		return (EACCES);
2389 
2390 	obj = SLOT(vplabel);
2391 
2392 	if (!lomac_subject_dominate(subj, obj))
2393 		return (EACCES);
2394 
2395 	return (0);
2396 }
2397 
2398 static int
2399 lomac_vnode_check_mmap(struct ucred *cred, struct vnode *vp,
2400     struct label *vplabel, int prot, int flags)
2401 {
2402 	struct mac_lomac *subj, *obj;
2403 
2404 	/*
2405 	 * Rely on the use of open()-time protections to handle
2406 	 * non-revocation cases.
2407 	 */
2408 	if (!lomac_enabled)
2409 		return (0);
2410 
2411 	subj = SLOT(cred->cr_label);
2412 	obj = SLOT(vplabel);
2413 
2414 	if (((prot & VM_PROT_WRITE) != 0) && ((flags & MAP_SHARED) != 0)) {
2415 		if (!lomac_subject_dominate(subj, obj))
2416 			return (EACCES);
2417 	}
2418 	if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
2419 		if (!lomac_dominate_single(obj, subj))
2420 			return (maybe_demote(subj, obj, "mapping", "file", vp));
2421 	}
2422 
2423 	return (0);
2424 }
2425 
2426 static void
2427 lomac_vnode_check_mmap_downgrade(struct ucred *cred, struct vnode *vp,
2428     struct label *vplabel, /* XXX vm_prot_t */ int *prot)
2429 {
2430 	struct mac_lomac *subj, *obj;
2431 
2432 	/*
2433 	 * Rely on the use of open()-time protections to handle
2434 	 * non-revocation cases.
2435 	 */
2436 	if (!lomac_enabled || !revocation_enabled)
2437 		return;
2438 
2439 	subj = SLOT(cred->cr_label);
2440 	obj = SLOT(vplabel);
2441 
2442 	if (!lomac_subject_dominate(subj, obj))
2443 		*prot &= ~VM_PROT_WRITE;
2444 }
2445 
2446 static int
2447 lomac_vnode_check_open(struct ucred *cred, struct vnode *vp,
2448     struct label *vplabel, accmode_t accmode)
2449 {
2450 	struct mac_lomac *subj, *obj;
2451 
2452 	if (!lomac_enabled)
2453 		return (0);
2454 
2455 	subj = SLOT(cred->cr_label);
2456 	obj = SLOT(vplabel);
2457 
2458 	/* XXX privilege override for admin? */
2459 	if (accmode & VMODIFY_PERMS) {
2460 		if (!lomac_subject_dominate(subj, obj))
2461 			return (EACCES);
2462 	}
2463 
2464 	return (0);
2465 }
2466 
2467 static int
2468 lomac_vnode_check_read(struct ucred *active_cred, struct ucred *file_cred,
2469     struct vnode *vp, struct label *vplabel)
2470 {
2471 	struct mac_lomac *subj, *obj;
2472 
2473 	if (!lomac_enabled || !revocation_enabled)
2474 		return (0);
2475 
2476 	subj = SLOT(active_cred->cr_label);
2477 	obj = SLOT(vplabel);
2478 
2479 	if (!lomac_dominate_single(obj, subj))
2480 		return (maybe_demote(subj, obj, "reading", "file", vp));
2481 
2482 	return (0);
2483 }
2484 
2485 static int
2486 lomac_vnode_check_relabel(struct ucred *cred, struct vnode *vp,
2487     struct label *vplabel, struct label *newlabel)
2488 {
2489 	struct mac_lomac *old, *new, *subj;
2490 	int error;
2491 
2492 	old = SLOT(vplabel);
2493 	new = SLOT(newlabel);
2494 	subj = SLOT(cred->cr_label);
2495 
2496 	/*
2497 	 * If there is a LOMAC label update for the vnode, it must be a
2498 	 * single label, with an optional explicit auxiliary single.
2499 	 */
2500 	error = lomac_atmostflags(new,
2501 	    MAC_LOMAC_FLAG_SINGLE | MAC_LOMAC_FLAG_AUX);
2502 	if (error)
2503 		return (error);
2504 
2505 	/*
2506 	 * To perform a relabel of the vnode (LOMAC label or not), LOMAC must
2507 	 * authorize the relabel.
2508 	 */
2509 	if (!lomac_single_in_range(old, subj))
2510 		return (EPERM);
2511 
2512 	/*
2513 	 * If the LOMAC label is to be changed, authorize as appropriate.
2514 	 */
2515 	if (new->ml_flags & MAC_LOMAC_FLAG_SINGLE) {
2516 		/*
2517 		 * To change the LOMAC label on a vnode, the new vnode label
2518 		 * must be in the subject range.
2519 		 */
2520 		if (!lomac_single_in_range(new, subj))
2521 			return (EPERM);
2522 
2523 		/*
2524 		 * To change the LOMAC label on the vnode to be EQUAL, the
2525 		 * subject must have appropriate privilege.
2526 		 */
2527 		if (lomac_contains_equal(new)) {
2528 			error = lomac_subject_privileged(subj);
2529 			if (error)
2530 				return (error);
2531 		}
2532 	}
2533 	if (new->ml_flags & MAC_LOMAC_FLAG_AUX) {
2534 		/*
2535 		 * Fill in the missing parts from the previous label.
2536 		 */
2537 		if ((new->ml_flags & MAC_LOMAC_FLAG_SINGLE) == 0)
2538 			lomac_copy_single(subj, new);
2539 
2540 		/*
2541 		 * To change the auxiliary LOMAC label on a vnode, the new
2542 		 * vnode label must be in the subject range.
2543 		 */
2544 		if (!lomac_auxsingle_in_range(new, subj))
2545 			return (EPERM);
2546 
2547 		/*
2548 		 * To change the auxiliary LOMAC label on the vnode to be
2549 		 * EQUAL, the subject must have appropriate privilege.
2550 		 */
2551 		if (lomac_contains_equal(new)) {
2552 			error = lomac_subject_privileged(subj);
2553 			if (error)
2554 				return (error);
2555 		}
2556 	}
2557 
2558 	return (0);
2559 }
2560 
2561 static int
2562 lomac_vnode_check_rename_from(struct ucred *cred, struct vnode *dvp,
2563     struct label *dvplabel, struct vnode *vp, struct label *vplabel,
2564     struct componentname *cnp)
2565 {
2566 	struct mac_lomac *subj, *obj;
2567 
2568 	if (!lomac_enabled)
2569 		return (0);
2570 
2571 	subj = SLOT(cred->cr_label);
2572 	obj = SLOT(dvplabel);
2573 
2574 	if (!lomac_subject_dominate(subj, obj))
2575 		return (EACCES);
2576 
2577 	obj = SLOT(vplabel);
2578 
2579 	if (!lomac_subject_dominate(subj, obj))
2580 		return (EACCES);
2581 
2582 	return (0);
2583 }
2584 
2585 static int
2586 lomac_vnode_check_rename_to(struct ucred *cred, struct vnode *dvp,
2587     struct label *dvplabel, struct vnode *vp, struct label *vplabel,
2588     int samedir, struct componentname *cnp)
2589 {
2590 	struct mac_lomac *subj, *obj;
2591 
2592 	if (!lomac_enabled)
2593 		return (0);
2594 
2595 	subj = SLOT(cred->cr_label);
2596 	obj = SLOT(dvplabel);
2597 
2598 	if (!lomac_subject_dominate(subj, obj))
2599 		return (EACCES);
2600 
2601 	if (vp != NULL) {
2602 		obj = SLOT(vplabel);
2603 
2604 		if (!lomac_subject_dominate(subj, obj))
2605 			return (EACCES);
2606 	}
2607 
2608 	return (0);
2609 }
2610 
2611 static int
2612 lomac_vnode_check_revoke(struct ucred *cred, struct vnode *vp,
2613     struct label *vplabel)
2614 {
2615 	struct mac_lomac *subj, *obj;
2616 
2617 	if (!lomac_enabled)
2618 		return (0);
2619 
2620 	subj = SLOT(cred->cr_label);
2621 	obj = SLOT(vplabel);
2622 
2623 	if (!lomac_subject_dominate(subj, obj))
2624 		return (EACCES);
2625 
2626 	return (0);
2627 }
2628 
2629 static int
2630 lomac_vnode_check_setacl(struct ucred *cred, struct vnode *vp,
2631     struct label *vplabel, acl_type_t type, struct acl *acl)
2632 {
2633 	struct mac_lomac *subj, *obj;
2634 
2635 	if (!lomac_enabled)
2636 		return (0);
2637 
2638 	subj = SLOT(cred->cr_label);
2639 	obj = SLOT(vplabel);
2640 
2641 	if (!lomac_subject_dominate(subj, obj))
2642 		return (EACCES);
2643 
2644 	return (0);
2645 }
2646 
2647 static int
2648 lomac_vnode_check_setextattr(struct ucred *cred, struct vnode *vp,
2649     struct label *vplabel, int attrnamespace, const char *name)
2650 {
2651 	struct mac_lomac *subj, *obj;
2652 
2653 	if (!lomac_enabled)
2654 		return (0);
2655 
2656 	subj = SLOT(cred->cr_label);
2657 	obj = SLOT(vplabel);
2658 
2659 	if (!lomac_subject_dominate(subj, obj))
2660 		return (EACCES);
2661 
2662 	/* XXX: protect the MAC EA in a special way? */
2663 
2664 	return (0);
2665 }
2666 
2667 static int
2668 lomac_vnode_check_setflags(struct ucred *cred, struct vnode *vp,
2669     struct label *vplabel, u_long flags)
2670 {
2671 	struct mac_lomac *subj, *obj;
2672 
2673 	if (!lomac_enabled)
2674 		return (0);
2675 
2676 	subj = SLOT(cred->cr_label);
2677 	obj = SLOT(vplabel);
2678 
2679 	if (!lomac_subject_dominate(subj, obj))
2680 		return (EACCES);
2681 
2682 	return (0);
2683 }
2684 
2685 static int
2686 lomac_vnode_check_setmode(struct ucred *cred, struct vnode *vp,
2687     struct label *vplabel, mode_t mode)
2688 {
2689 	struct mac_lomac *subj, *obj;
2690 
2691 	if (!lomac_enabled)
2692 		return (0);
2693 
2694 	subj = SLOT(cred->cr_label);
2695 	obj = SLOT(vplabel);
2696 
2697 	if (!lomac_subject_dominate(subj, obj))
2698 		return (EACCES);
2699 
2700 	return (0);
2701 }
2702 
2703 static int
2704 lomac_vnode_check_setowner(struct ucred *cred, struct vnode *vp,
2705     struct label *vplabel, uid_t uid, gid_t gid)
2706 {
2707 	struct mac_lomac *subj, *obj;
2708 
2709 	if (!lomac_enabled)
2710 		return (0);
2711 
2712 	subj = SLOT(cred->cr_label);
2713 	obj = SLOT(vplabel);
2714 
2715 	if (!lomac_subject_dominate(subj, obj))
2716 		return (EACCES);
2717 
2718 	return (0);
2719 }
2720 
2721 static int
2722 lomac_vnode_check_setutimes(struct ucred *cred, struct vnode *vp,
2723     struct label *vplabel, struct timespec atime, struct timespec mtime)
2724 {
2725 	struct mac_lomac *subj, *obj;
2726 
2727 	if (!lomac_enabled)
2728 		return (0);
2729 
2730 	subj = SLOT(cred->cr_label);
2731 	obj = SLOT(vplabel);
2732 
2733 	if (!lomac_subject_dominate(subj, obj))
2734 		return (EACCES);
2735 
2736 	return (0);
2737 }
2738 
2739 static int
2740 lomac_vnode_check_unlink(struct ucred *cred, struct vnode *dvp,
2741     struct label *dvplabel, struct vnode *vp, struct label *vplabel,
2742     struct componentname *cnp)
2743 {
2744 	struct mac_lomac *subj, *obj;
2745 
2746 	if (!lomac_enabled)
2747 		return (0);
2748 
2749 	subj = SLOT(cred->cr_label);
2750 	obj = SLOT(dvplabel);
2751 
2752 	if (!lomac_subject_dominate(subj, obj))
2753 		return (EACCES);
2754 
2755 	obj = SLOT(vplabel);
2756 
2757 	if (!lomac_subject_dominate(subj, obj))
2758 		return (EACCES);
2759 
2760 	return (0);
2761 }
2762 
2763 static int
2764 lomac_vnode_check_write(struct ucred *active_cred,
2765     struct ucred *file_cred, struct vnode *vp, struct label *vplabel)
2766 {
2767 	struct mac_lomac *subj, *obj;
2768 
2769 	if (!lomac_enabled || !revocation_enabled)
2770 		return (0);
2771 
2772 	subj = SLOT(active_cred->cr_label);
2773 	obj = SLOT(vplabel);
2774 
2775 	if (!lomac_subject_dominate(subj, obj))
2776 		return (EACCES);
2777 
2778 	return (0);
2779 }
2780 
2781 static int
2782 lomac_vnode_create_extattr(struct ucred *cred, struct mount *mp,
2783     struct label *mplabel, struct vnode *dvp, struct label *dvplabel,
2784     struct vnode *vp, struct label *vplabel, struct componentname *cnp)
2785 {
2786 	struct mac_lomac *source, *dest, *dir, temp;
2787 	size_t buflen;
2788 	int error;
2789 
2790 	buflen = sizeof(temp);
2791 	bzero(&temp, buflen);
2792 
2793 	source = SLOT(cred->cr_label);
2794 	dest = SLOT(vplabel);
2795 	dir = SLOT(dvplabel);
2796 	if (dir->ml_flags & MAC_LOMAC_FLAG_AUX) {
2797 		lomac_copy_auxsingle(dir, &temp);
2798 		lomac_set_single(&temp, dir->ml_auxsingle.mle_type,
2799 		    dir->ml_auxsingle.mle_grade);
2800 	} else {
2801 		lomac_copy_single(source, &temp);
2802 	}
2803 
2804 	error = vn_extattr_set(vp, IO_NODELOCKED, MAC_LOMAC_EXTATTR_NAMESPACE,
2805 	    MAC_LOMAC_EXTATTR_NAME, buflen, (char *)&temp, curthread);
2806 	if (error == 0)
2807 		lomac_copy(&temp, dest);
2808 	return (error);
2809 }
2810 
2811 static void
2812 lomac_vnode_execve_transition(struct ucred *old, struct ucred *new,
2813     struct vnode *vp, struct label *vplabel, struct label *interpvplabel,
2814     struct image_params *imgp, struct label *execlabel)
2815 {
2816 	struct mac_lomac *source, *dest, *obj, *robj;
2817 
2818 	source = SLOT(old->cr_label);
2819 	dest = SLOT(new->cr_label);
2820 	obj = SLOT(vplabel);
2821 	robj = interpvplabel != NULL ? SLOT(interpvplabel) : obj;
2822 
2823 	lomac_copy(source, dest);
2824 	/*
2825 	 * If there's an auxiliary label on the real object, respect it and
2826 	 * assume that this level should be assumed immediately if a higher
2827 	 * level is currently in place.
2828 	 */
2829 	if (robj->ml_flags & MAC_LOMAC_FLAG_AUX &&
2830 	    !lomac_dominate_element(&robj->ml_auxsingle, &dest->ml_single)
2831 	    && lomac_auxsingle_in_range(robj, dest))
2832 		lomac_set_single(dest, robj->ml_auxsingle.mle_type,
2833 		    robj->ml_auxsingle.mle_grade);
2834 	/*
2835 	 * Restructuring to use the execve transitioning mechanism instead of
2836 	 * the normal demotion mechanism here would be difficult, so just
2837 	 * copy the label over and perform standard demotion.  This is also
2838 	 * non-optimal because it will result in the intermediate label "new"
2839 	 * being created and immediately recycled.
2840 	 */
2841 	if (lomac_enabled && revocation_enabled &&
2842 	    !lomac_dominate_single(obj, source))
2843 		(void)maybe_demote(source, obj, "executing", "file", vp);
2844 }
2845 
2846 static int
2847 lomac_vnode_execve_will_transition(struct ucred *old, struct vnode *vp,
2848     struct label *vplabel, struct label *interpvplabel,
2849     struct image_params *imgp, struct label *execlabel)
2850 {
2851 	struct mac_lomac *subj, *obj, *robj;
2852 
2853 	if (!lomac_enabled || !revocation_enabled)
2854 		return (0);
2855 
2856 	subj = SLOT(old->cr_label);
2857 	obj = SLOT(vplabel);
2858 	robj = interpvplabel != NULL ? SLOT(interpvplabel) : obj;
2859 
2860 	return ((robj->ml_flags & MAC_LOMAC_FLAG_AUX &&
2861 	    !lomac_dominate_element(&robj->ml_auxsingle, &subj->ml_single)
2862 	    && lomac_auxsingle_in_range(robj, subj)) ||
2863 	    !lomac_dominate_single(obj, subj));
2864 }
2865 
2866 static void
2867 lomac_vnode_relabel(struct ucred *cred, struct vnode *vp,
2868     struct label *vplabel, struct label *newlabel)
2869 {
2870 	struct mac_lomac *source, *dest;
2871 
2872 	source = SLOT(newlabel);
2873 	dest = SLOT(vplabel);
2874 
2875 	try_relabel(source, dest);
2876 }
2877 
2878 static int
2879 lomac_vnode_setlabel_extattr(struct ucred *cred, struct vnode *vp,
2880     struct label *vplabel, struct label *intlabel)
2881 {
2882 	struct mac_lomac *source, temp;
2883 	size_t buflen;
2884 	int error;
2885 
2886 	buflen = sizeof(temp);
2887 	bzero(&temp, buflen);
2888 
2889 	source = SLOT(intlabel);
2890 	if ((source->ml_flags & MAC_LOMAC_FLAG_SINGLE) == 0)
2891 		return (0);
2892 
2893 	lomac_copy_single(source, &temp);
2894 	error = vn_extattr_set(vp, IO_NODELOCKED, MAC_LOMAC_EXTATTR_NAMESPACE,
2895 	    MAC_LOMAC_EXTATTR_NAME, buflen, (char *)&temp, curthread);
2896 	return (error);
2897 }
2898 
2899 static struct mac_policy_ops lomac_ops =
2900 {
2901 	.mpo_init = lomac_init,
2902 	.mpo_destroy = lomac_fini,
2903 
2904 	.mpo_bpfdesc_check_receive = lomac_bpfdesc_check_receive,
2905 	.mpo_bpfdesc_create = lomac_bpfdesc_create,
2906 	.mpo_bpfdesc_create_mbuf = lomac_bpfdesc_create_mbuf,
2907 	.mpo_bpfdesc_destroy_label = lomac_destroy_label,
2908 	.mpo_bpfdesc_init_label = lomac_init_label,
2909 
2910 	.mpo_cred_check_relabel = lomac_cred_check_relabel,
2911 	.mpo_cred_check_visible = lomac_cred_check_visible,
2912 	.mpo_cred_copy_label = lomac_copy_label,
2913 	.mpo_cred_create_swapper = lomac_cred_create_swapper,
2914 	.mpo_cred_create_init = lomac_cred_create_init,
2915 	.mpo_cred_destroy_label = lomac_destroy_label,
2916 	.mpo_cred_externalize_label = lomac_externalize_label,
2917 	.mpo_cred_init_label = lomac_init_label,
2918 	.mpo_cred_internalize_label = lomac_internalize_label,
2919 	.mpo_cred_relabel = lomac_cred_relabel,
2920 
2921 	.mpo_devfs_create_device = lomac_devfs_create_device,
2922 	.mpo_devfs_create_directory = lomac_devfs_create_directory,
2923 	.mpo_devfs_create_symlink = lomac_devfs_create_symlink,
2924 	.mpo_devfs_destroy_label = lomac_destroy_label,
2925 	.mpo_devfs_init_label = lomac_init_label,
2926 	.mpo_devfs_update = lomac_devfs_update,
2927 	.mpo_devfs_vnode_associate = lomac_devfs_vnode_associate,
2928 
2929 	.mpo_ifnet_check_relabel = lomac_ifnet_check_relabel,
2930 	.mpo_ifnet_check_transmit = lomac_ifnet_check_transmit,
2931 	.mpo_ifnet_copy_label = lomac_copy_label,
2932 	.mpo_ifnet_create = lomac_ifnet_create,
2933 	.mpo_ifnet_create_mbuf = lomac_ifnet_create_mbuf,
2934 	.mpo_ifnet_destroy_label = lomac_destroy_label,
2935 	.mpo_ifnet_externalize_label = lomac_externalize_label,
2936 	.mpo_ifnet_init_label = lomac_init_label,
2937 	.mpo_ifnet_internalize_label = lomac_internalize_label,
2938 	.mpo_ifnet_relabel = lomac_ifnet_relabel,
2939 
2940 	.mpo_syncache_create = lomac_syncache_create,
2941 	.mpo_syncache_destroy_label = lomac_destroy_label,
2942 	.mpo_syncache_init_label = lomac_init_label_waitcheck,
2943 
2944 	.mpo_inpcb_check_deliver = lomac_inpcb_check_deliver,
2945 	.mpo_inpcb_check_visible = lomac_inpcb_check_visible,
2946 	.mpo_inpcb_create = lomac_inpcb_create,
2947 	.mpo_inpcb_create_mbuf = lomac_inpcb_create_mbuf,
2948 	.mpo_inpcb_destroy_label = lomac_destroy_label,
2949 	.mpo_inpcb_init_label = lomac_init_label_waitcheck,
2950 	.mpo_inpcb_sosetlabel = lomac_inpcb_sosetlabel,
2951 
2952 	.mpo_ip6q_create = lomac_ip6q_create,
2953 	.mpo_ip6q_destroy_label = lomac_destroy_label,
2954 	.mpo_ip6q_init_label = lomac_init_label_waitcheck,
2955 	.mpo_ip6q_match = lomac_ip6q_match,
2956 	.mpo_ip6q_reassemble = lomac_ip6q_reassemble,
2957 	.mpo_ip6q_update = lomac_ip6q_update,
2958 
2959 	.mpo_ipq_create = lomac_ipq_create,
2960 	.mpo_ipq_destroy_label = lomac_destroy_label,
2961 	.mpo_ipq_init_label = lomac_init_label_waitcheck,
2962 	.mpo_ipq_match = lomac_ipq_match,
2963 	.mpo_ipq_reassemble = lomac_ipq_reassemble,
2964 	.mpo_ipq_update = lomac_ipq_update,
2965 
2966 	.mpo_kld_check_load = lomac_kld_check_load,
2967 
2968 	.mpo_mbuf_copy_label = lomac_copy_label,
2969 	.mpo_mbuf_destroy_label = lomac_destroy_label,
2970 	.mpo_mbuf_init_label = lomac_init_label_waitcheck,
2971 
2972 	.mpo_mount_create = lomac_mount_create,
2973 	.mpo_mount_destroy_label = lomac_destroy_label,
2974 	.mpo_mount_init_label = lomac_init_label,
2975 
2976 	.mpo_netinet_arp_send = lomac_netinet_arp_send,
2977 	.mpo_netinet_firewall_reply = lomac_netinet_firewall_reply,
2978 	.mpo_netinet_firewall_send = lomac_netinet_firewall_send,
2979 	.mpo_netinet_fragment = lomac_netinet_fragment,
2980 	.mpo_netinet_icmp_reply = lomac_netinet_icmp_reply,
2981 	.mpo_netinet_igmp_send = lomac_netinet_igmp_send,
2982 
2983 	.mpo_netinet6_nd6_send = lomac_netinet6_nd6_send,
2984 
2985 	.mpo_pipe_check_ioctl = lomac_pipe_check_ioctl,
2986 	.mpo_pipe_check_read = lomac_pipe_check_read,
2987 	.mpo_pipe_check_relabel = lomac_pipe_check_relabel,
2988 	.mpo_pipe_check_write = lomac_pipe_check_write,
2989 	.mpo_pipe_copy_label = lomac_copy_label,
2990 	.mpo_pipe_create = lomac_pipe_create,
2991 	.mpo_pipe_destroy_label = lomac_destroy_label,
2992 	.mpo_pipe_externalize_label = lomac_externalize_label,
2993 	.mpo_pipe_init_label = lomac_init_label,
2994 	.mpo_pipe_internalize_label = lomac_internalize_label,
2995 	.mpo_pipe_relabel = lomac_pipe_relabel,
2996 
2997 	.mpo_priv_check = lomac_priv_check,
2998 
2999 	.mpo_proc_check_debug = lomac_proc_check_debug,
3000 	.mpo_proc_check_sched = lomac_proc_check_sched,
3001 	.mpo_proc_check_signal = lomac_proc_check_signal,
3002 	.mpo_proc_destroy_label = lomac_proc_destroy_label,
3003 	.mpo_proc_init_label = lomac_proc_init_label,
3004 
3005 	.mpo_socket_check_deliver = lomac_socket_check_deliver,
3006 	.mpo_socket_check_relabel = lomac_socket_check_relabel,
3007 	.mpo_socket_check_visible = lomac_socket_check_visible,
3008 	.mpo_socket_copy_label = lomac_copy_label,
3009 	.mpo_socket_create = lomac_socket_create,
3010 	.mpo_socket_create_mbuf = lomac_socket_create_mbuf,
3011 	.mpo_socket_destroy_label = lomac_destroy_label,
3012 	.mpo_socket_externalize_label = lomac_externalize_label,
3013 	.mpo_socket_init_label = lomac_init_label_waitcheck,
3014 	.mpo_socket_internalize_label = lomac_internalize_label,
3015 	.mpo_socket_newconn = lomac_socket_newconn,
3016 	.mpo_socket_relabel = lomac_socket_relabel,
3017 
3018 	.mpo_socketpeer_destroy_label = lomac_destroy_label,
3019 	.mpo_socketpeer_externalize_label = lomac_externalize_label,
3020 	.mpo_socketpeer_init_label = lomac_init_label_waitcheck,
3021 	.mpo_socketpeer_set_from_mbuf = lomac_socketpeer_set_from_mbuf,
3022 	.mpo_socketpeer_set_from_socket = lomac_socketpeer_set_from_socket,
3023 
3024 	.mpo_syncache_create_mbuf = lomac_syncache_create_mbuf,
3025 
3026 	.mpo_system_check_acct = lomac_system_check_acct,
3027 	.mpo_system_check_auditctl = lomac_system_check_auditctl,
3028 	.mpo_system_check_swapoff = lomac_system_check_swapoff,
3029 	.mpo_system_check_swapon = lomac_system_check_swapon,
3030 	.mpo_system_check_sysctl = lomac_system_check_sysctl,
3031 
3032 	.mpo_thread_userret = lomac_thread_userret,
3033 
3034 	.mpo_vnode_associate_extattr = lomac_vnode_associate_extattr,
3035 	.mpo_vnode_associate_singlelabel = lomac_vnode_associate_singlelabel,
3036 	.mpo_vnode_check_access = lomac_vnode_check_open,
3037 	.mpo_vnode_check_create = lomac_vnode_check_create,
3038 	.mpo_vnode_check_deleteacl = lomac_vnode_check_deleteacl,
3039 	.mpo_vnode_check_link = lomac_vnode_check_link,
3040 	.mpo_vnode_check_mmap = lomac_vnode_check_mmap,
3041 	.mpo_vnode_check_mmap_downgrade = lomac_vnode_check_mmap_downgrade,
3042 	.mpo_vnode_check_open = lomac_vnode_check_open,
3043 	.mpo_vnode_check_read = lomac_vnode_check_read,
3044 	.mpo_vnode_check_relabel = lomac_vnode_check_relabel,
3045 	.mpo_vnode_check_rename_from = lomac_vnode_check_rename_from,
3046 	.mpo_vnode_check_rename_to = lomac_vnode_check_rename_to,
3047 	.mpo_vnode_check_revoke = lomac_vnode_check_revoke,
3048 	.mpo_vnode_check_setacl = lomac_vnode_check_setacl,
3049 	.mpo_vnode_check_setextattr = lomac_vnode_check_setextattr,
3050 	.mpo_vnode_check_setflags = lomac_vnode_check_setflags,
3051 	.mpo_vnode_check_setmode = lomac_vnode_check_setmode,
3052 	.mpo_vnode_check_setowner = lomac_vnode_check_setowner,
3053 	.mpo_vnode_check_setutimes = lomac_vnode_check_setutimes,
3054 	.mpo_vnode_check_unlink = lomac_vnode_check_unlink,
3055 	.mpo_vnode_check_write = lomac_vnode_check_write,
3056 	.mpo_vnode_copy_label = lomac_copy_label,
3057 	.mpo_vnode_create_extattr = lomac_vnode_create_extattr,
3058 	.mpo_vnode_destroy_label = lomac_destroy_label,
3059 	.mpo_vnode_execve_transition = lomac_vnode_execve_transition,
3060 	.mpo_vnode_execve_will_transition = lomac_vnode_execve_will_transition,
3061 	.mpo_vnode_externalize_label = lomac_externalize_label,
3062 	.mpo_vnode_init_label = lomac_init_label,
3063 	.mpo_vnode_internalize_label = lomac_internalize_label,
3064 	.mpo_vnode_relabel = lomac_vnode_relabel,
3065 	.mpo_vnode_setlabel_extattr = lomac_vnode_setlabel_extattr,
3066 };
3067 
3068 MAC_POLICY_SET(&lomac_ops, mac_lomac, "TrustedBSD MAC/LOMAC",
3069     MPC_LOADTIME_FLAG_NOTLATE, &lomac_slot);
3070