2 * Copyright (c) 1999-2002, 2007 Robert N. M. Watson
3 * Copyright (c) 2001-2005 Networks Associates Technology, Inc.
6 * This software was developed by Robert Watson for the TrustedBSD Project.
8 * This software was developed for the FreeBSD Project in part by NAI Labs,
9 * the Security Research Division of Network Associates, Inc. under
10 * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
11 * CHATS research program.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * Developed by the TrustedBSD Project.
40 * Low-watermark floating label mandatory integrity policy.
43 #include <sys/types.h>
44 #include <sys/param.h>
47 #include <sys/extattr.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
51 #include <sys/mount.h>
55 #include <sys/systm.h>
56 #include <sys/sysproto.h>
57 #include <sys/sysent.h>
58 #include <sys/systm.h>
59 #include <sys/vnode.h>
61 #include <sys/socket.h>
62 #include <sys/socketvar.h>
65 #include <sys/sysctl.h>
66 #include <sys/syslog.h>
68 #include <fs/devfs/devfs.h>
70 #include <net/bpfdesc.h>
72 #include <net/if_types.h>
73 #include <net/if_var.h>
75 #include <netinet/in.h>
76 #include <netinet/in_pcb.h>
77 #include <netinet/ip_var.h>
81 #include <security/mac/mac_policy.h>
82 #include <security/mac/mac_framework.h>
83 #include <security/mac_lomac/mac_lomac.h>
85 struct mac_lomac_proc {
86 struct mac_lomac mac_lomac;
90 SYSCTL_DECL(_security_mac);
92 SYSCTL_NODE(_security_mac, OID_AUTO, lomac, CTLFLAG_RW, 0,
93 "TrustedBSD mac_lomac policy controls");
95 static int mac_lomac_label_size = sizeof(struct mac_lomac);
96 SYSCTL_INT(_security_mac_lomac, OID_AUTO, label_size, CTLFLAG_RD,
97 &mac_lomac_label_size, 0, "Size of struct mac_lomac");
99 static int mac_lomac_enabled = 1;
100 SYSCTL_INT(_security_mac_lomac, OID_AUTO, enabled, CTLFLAG_RW,
101 &mac_lomac_enabled, 0, "Enforce MAC/LOMAC policy");
102 TUNABLE_INT("security.mac.lomac.enabled", &mac_lomac_enabled);
104 static int destroyed_not_inited;
105 SYSCTL_INT(_security_mac_lomac, OID_AUTO, destroyed_not_inited, CTLFLAG_RD,
106 &destroyed_not_inited, 0, "Count of labels destroyed but not inited");
108 static int trust_all_interfaces = 0;
109 SYSCTL_INT(_security_mac_lomac, OID_AUTO, trust_all_interfaces, CTLFLAG_RD,
110 &trust_all_interfaces, 0, "Consider all interfaces 'trusted' by MAC/LOMAC");
111 TUNABLE_INT("security.mac.lomac.trust_all_interfaces", &trust_all_interfaces);
113 static char trusted_interfaces[128];
114 SYSCTL_STRING(_security_mac_lomac, OID_AUTO, trusted_interfaces, CTLFLAG_RD,
115 trusted_interfaces, 0, "Interfaces considered 'trusted' by MAC/LOMAC");
116 TUNABLE_STR("security.mac.lomac.trusted_interfaces", trusted_interfaces,
117 sizeof(trusted_interfaces));
119 static int ptys_equal = 0;
120 SYSCTL_INT(_security_mac_lomac, OID_AUTO, ptys_equal, CTLFLAG_RW,
121 &ptys_equal, 0, "Label pty devices as lomac/equal on create");
122 TUNABLE_INT("security.mac.lomac.ptys_equal", &ptys_equal);
124 static int revocation_enabled = 1;
125 SYSCTL_INT(_security_mac_lomac, OID_AUTO, revocation_enabled, CTLFLAG_RW,
126 &revocation_enabled, 0, "Revoke access to objects on relabel");
127 TUNABLE_INT("security.mac.lomac.revocation_enabled", &revocation_enabled);
129 static int mac_lomac_slot;
130 #define SLOT(l) ((struct mac_lomac *)mac_label_get((l), mac_lomac_slot))
131 #define SLOT_SET(l, val) mac_label_set((l), mac_lomac_slot, (uintptr_t)(val))
132 #define PSLOT(l) ((struct mac_lomac_proc *) \
133 mac_label_get((l), mac_lomac_slot))
134 #define PSLOT_SET(l, val) mac_label_set((l), mac_lomac_slot, (uintptr_t)(val))
136 MALLOC_DEFINE(M_MACLOMAC, "mac_lomac_label", "MAC/LOMAC labels");
138 static struct mac_lomac *
139 lomac_alloc(int flag)
141 struct mac_lomac *mac_lomac;
143 mac_lomac = malloc(sizeof(struct mac_lomac), M_MACLOMAC, M_ZERO | flag);
149 lomac_free(struct mac_lomac *mac_lomac)
152 if (mac_lomac != NULL)
153 free(mac_lomac, M_MACLOMAC);
155 atomic_add_int(&destroyed_not_inited, 1);
159 lomac_atmostflags(struct mac_lomac *mac_lomac, int flags)
162 if ((mac_lomac->ml_flags & flags) != mac_lomac->ml_flags)
168 mac_lomac_dominate_element(struct mac_lomac_element *a,
169 struct mac_lomac_element *b)
172 switch (a->mle_type) {
173 case MAC_LOMAC_TYPE_EQUAL:
174 case MAC_LOMAC_TYPE_HIGH:
177 case MAC_LOMAC_TYPE_LOW:
178 switch (b->mle_type) {
179 case MAC_LOMAC_TYPE_GRADE:
180 case MAC_LOMAC_TYPE_HIGH:
183 case MAC_LOMAC_TYPE_EQUAL:
184 case MAC_LOMAC_TYPE_LOW:
188 panic("mac_lomac_dominate_element: b->mle_type invalid");
191 case MAC_LOMAC_TYPE_GRADE:
192 switch (b->mle_type) {
193 case MAC_LOMAC_TYPE_EQUAL:
194 case MAC_LOMAC_TYPE_LOW:
197 case MAC_LOMAC_TYPE_HIGH:
200 case MAC_LOMAC_TYPE_GRADE:
201 return (a->mle_grade >= b->mle_grade);
204 panic("mac_lomac_dominate_element: b->mle_type invalid");
208 panic("mac_lomac_dominate_element: a->mle_type invalid");
213 mac_lomac_range_in_range(struct mac_lomac *rangea, struct mac_lomac *rangeb)
216 return (mac_lomac_dominate_element(&rangeb->ml_rangehigh,
217 &rangea->ml_rangehigh) &&
218 mac_lomac_dominate_element(&rangea->ml_rangelow,
219 &rangeb->ml_rangelow));
223 mac_lomac_single_in_range(struct mac_lomac *single, struct mac_lomac *range)
226 KASSERT((single->ml_flags & MAC_LOMAC_FLAG_SINGLE) != 0,
227 ("mac_lomac_single_in_range: a not single"));
228 KASSERT((range->ml_flags & MAC_LOMAC_FLAG_RANGE) != 0,
229 ("mac_lomac_single_in_range: b not range"));
231 return (mac_lomac_dominate_element(&range->ml_rangehigh,
232 &single->ml_single) &&
233 mac_lomac_dominate_element(&single->ml_single,
234 &range->ml_rangelow));
238 mac_lomac_auxsingle_in_range(struct mac_lomac *single, struct mac_lomac *range)
241 KASSERT((single->ml_flags & MAC_LOMAC_FLAG_AUX) != 0,
242 ("mac_lomac_single_in_range: a not auxsingle"));
243 KASSERT((range->ml_flags & MAC_LOMAC_FLAG_RANGE) != 0,
244 ("mac_lomac_single_in_range: b not range"));
246 return (mac_lomac_dominate_element(&range->ml_rangehigh,
247 &single->ml_auxsingle) &&
248 mac_lomac_dominate_element(&single->ml_auxsingle,
249 &range->ml_rangelow));
253 mac_lomac_dominate_single(struct mac_lomac *a, struct mac_lomac *b)
255 KASSERT((a->ml_flags & MAC_LOMAC_FLAG_SINGLE) != 0,
256 ("mac_lomac_dominate_single: a not single"));
257 KASSERT((b->ml_flags & MAC_LOMAC_FLAG_SINGLE) != 0,
258 ("mac_lomac_dominate_single: b not single"));
260 return (mac_lomac_dominate_element(&a->ml_single, &b->ml_single));
264 mac_lomac_subject_dominate(struct mac_lomac *a, struct mac_lomac *b)
266 KASSERT((~a->ml_flags &
267 (MAC_LOMAC_FLAG_SINGLE | MAC_LOMAC_FLAG_RANGE)) == 0,
268 ("mac_lomac_dominate_single: a not subject"));
269 KASSERT((b->ml_flags & MAC_LOMAC_FLAG_SINGLE) != 0,
270 ("mac_lomac_dominate_single: b not single"));
272 return (mac_lomac_dominate_element(&a->ml_rangehigh,
277 mac_lomac_equal_element(struct mac_lomac_element *a, struct mac_lomac_element *b)
280 if (a->mle_type == MAC_LOMAC_TYPE_EQUAL ||
281 b->mle_type == MAC_LOMAC_TYPE_EQUAL)
284 return (a->mle_type == b->mle_type && a->mle_grade == b->mle_grade);
288 mac_lomac_equal_single(struct mac_lomac *a, struct mac_lomac *b)
291 KASSERT((a->ml_flags & MAC_LOMAC_FLAG_SINGLE) != 0,
292 ("mac_lomac_equal_single: a not single"));
293 KASSERT((b->ml_flags & MAC_LOMAC_FLAG_SINGLE) != 0,
294 ("mac_lomac_equal_single: b not single"));
296 return (mac_lomac_equal_element(&a->ml_single, &b->ml_single));
300 mac_lomac_contains_equal(struct mac_lomac *mac_lomac)
303 if (mac_lomac->ml_flags & MAC_LOMAC_FLAG_SINGLE)
304 if (mac_lomac->ml_single.mle_type == MAC_LOMAC_TYPE_EQUAL)
306 if (mac_lomac->ml_flags & MAC_LOMAC_FLAG_AUX)
307 if (mac_lomac->ml_auxsingle.mle_type == MAC_LOMAC_TYPE_EQUAL)
310 if (mac_lomac->ml_flags & MAC_LOMAC_FLAG_RANGE) {
311 if (mac_lomac->ml_rangelow.mle_type == MAC_LOMAC_TYPE_EQUAL)
313 if (mac_lomac->ml_rangehigh.mle_type == MAC_LOMAC_TYPE_EQUAL)
321 mac_lomac_subject_privileged(struct mac_lomac *mac_lomac)
324 KASSERT((mac_lomac->ml_flags & MAC_LOMAC_FLAGS_BOTH) ==
325 MAC_LOMAC_FLAGS_BOTH,
326 ("mac_lomac_subject_privileged: subject doesn't have both labels"));
328 /* If the single is EQUAL, it's ok. */
329 if (mac_lomac->ml_single.mle_type == MAC_LOMAC_TYPE_EQUAL)
332 /* If either range endpoint is EQUAL, it's ok. */
333 if (mac_lomac->ml_rangelow.mle_type == MAC_LOMAC_TYPE_EQUAL ||
334 mac_lomac->ml_rangehigh.mle_type == MAC_LOMAC_TYPE_EQUAL)
337 /* If the range is low-high, it's ok. */
338 if (mac_lomac->ml_rangelow.mle_type == MAC_LOMAC_TYPE_LOW &&
339 mac_lomac->ml_rangehigh.mle_type == MAC_LOMAC_TYPE_HIGH)
347 mac_lomac_high_single(struct mac_lomac *mac_lomac)
350 KASSERT((mac_lomac->ml_flags & MAC_LOMAC_FLAG_SINGLE) != 0,
351 ("mac_lomac_high_single: mac_lomac not single"));
353 return (mac_lomac->ml_single.mle_type == MAC_LOMAC_TYPE_HIGH);
357 mac_lomac_valid(struct mac_lomac *mac_lomac)
360 if (mac_lomac->ml_flags & MAC_LOMAC_FLAG_SINGLE) {
361 switch (mac_lomac->ml_single.mle_type) {
362 case MAC_LOMAC_TYPE_GRADE:
363 case MAC_LOMAC_TYPE_EQUAL:
364 case MAC_LOMAC_TYPE_HIGH:
365 case MAC_LOMAC_TYPE_LOW:
372 if (mac_lomac->ml_single.mle_type != MAC_LOMAC_TYPE_UNDEF)
376 if (mac_lomac->ml_flags & MAC_LOMAC_FLAG_AUX) {
377 switch (mac_lomac->ml_auxsingle.mle_type) {
378 case MAC_LOMAC_TYPE_GRADE:
379 case MAC_LOMAC_TYPE_EQUAL:
380 case MAC_LOMAC_TYPE_HIGH:
381 case MAC_LOMAC_TYPE_LOW:
388 if (mac_lomac->ml_auxsingle.mle_type != MAC_LOMAC_TYPE_UNDEF)
392 if (mac_lomac->ml_flags & MAC_LOMAC_FLAG_RANGE) {
393 switch (mac_lomac->ml_rangelow.mle_type) {
394 case MAC_LOMAC_TYPE_GRADE:
395 case MAC_LOMAC_TYPE_EQUAL:
396 case MAC_LOMAC_TYPE_HIGH:
397 case MAC_LOMAC_TYPE_LOW:
404 switch (mac_lomac->ml_rangehigh.mle_type) {
405 case MAC_LOMAC_TYPE_GRADE:
406 case MAC_LOMAC_TYPE_EQUAL:
407 case MAC_LOMAC_TYPE_HIGH:
408 case MAC_LOMAC_TYPE_LOW:
414 if (!mac_lomac_dominate_element(&mac_lomac->ml_rangehigh,
415 &mac_lomac->ml_rangelow))
418 if (mac_lomac->ml_rangelow.mle_type != MAC_LOMAC_TYPE_UNDEF ||
419 mac_lomac->ml_rangehigh.mle_type != MAC_LOMAC_TYPE_UNDEF)
427 mac_lomac_set_range(struct mac_lomac *mac_lomac, u_short typelow,
428 u_short gradelow, u_short typehigh, u_short gradehigh)
431 mac_lomac->ml_rangelow.mle_type = typelow;
432 mac_lomac->ml_rangelow.mle_grade = gradelow;
433 mac_lomac->ml_rangehigh.mle_type = typehigh;
434 mac_lomac->ml_rangehigh.mle_grade = gradehigh;
435 mac_lomac->ml_flags |= MAC_LOMAC_FLAG_RANGE;
439 mac_lomac_set_single(struct mac_lomac *mac_lomac, u_short type, u_short grade)
442 mac_lomac->ml_single.mle_type = type;
443 mac_lomac->ml_single.mle_grade = grade;
444 mac_lomac->ml_flags |= MAC_LOMAC_FLAG_SINGLE;
448 mac_lomac_copy_range(struct mac_lomac *labelfrom, struct mac_lomac *labelto)
451 KASSERT((labelfrom->ml_flags & MAC_LOMAC_FLAG_RANGE) != 0,
452 ("mac_lomac_copy_range: labelfrom not range"));
454 labelto->ml_rangelow = labelfrom->ml_rangelow;
455 labelto->ml_rangehigh = labelfrom->ml_rangehigh;
456 labelto->ml_flags |= MAC_LOMAC_FLAG_RANGE;
460 mac_lomac_copy_single(struct mac_lomac *labelfrom, struct mac_lomac *labelto)
463 KASSERT((labelfrom->ml_flags & MAC_LOMAC_FLAG_SINGLE) != 0,
464 ("mac_lomac_copy_single: labelfrom not single"));
466 labelto->ml_single = labelfrom->ml_single;
467 labelto->ml_flags |= MAC_LOMAC_FLAG_SINGLE;
471 mac_lomac_copy_auxsingle(struct mac_lomac *labelfrom, struct mac_lomac *labelto)
474 KASSERT((labelfrom->ml_flags & MAC_LOMAC_FLAG_AUX) != 0,
475 ("mac_lomac_copy_auxsingle: labelfrom not auxsingle"));
477 labelto->ml_auxsingle = labelfrom->ml_auxsingle;
478 labelto->ml_flags |= MAC_LOMAC_FLAG_AUX;
482 mac_lomac_copy(struct mac_lomac *source, struct mac_lomac *dest)
485 if (source->ml_flags & MAC_LOMAC_FLAG_SINGLE)
486 mac_lomac_copy_single(source, dest);
487 if (source->ml_flags & MAC_LOMAC_FLAG_AUX)
488 mac_lomac_copy_auxsingle(source, dest);
489 if (source->ml_flags & MAC_LOMAC_FLAG_RANGE)
490 mac_lomac_copy_range(source, dest);
493 static int mac_lomac_to_string(struct sbuf *sb,
494 struct mac_lomac *mac_lomac);
497 maybe_demote(struct mac_lomac *subjlabel, struct mac_lomac *objlabel,
498 const char *actionname, const char *objname, struct vnode *vp)
500 struct sbuf subjlabel_sb, subjtext_sb, objlabel_sb;
501 char *subjlabeltext, *objlabeltext, *subjtext;
502 struct mac_lomac cached_subjlabel;
503 struct mac_lomac_proc *subj;
508 subj = PSLOT(curthread->td_proc->p_label);
510 p = curthread->td_proc;
511 mtx_lock(&subj->mtx);
512 if (subj->mac_lomac.ml_flags & MAC_LOMAC_FLAG_UPDATE) {
514 * Check to see if the pending demotion would be more or
515 * less severe than this one, and keep the more severe.
516 * This can only happen for a multi-threaded application.
518 if (mac_lomac_dominate_single(objlabel, &subj->mac_lomac)) {
519 mtx_unlock(&subj->mtx);
523 bzero(&subj->mac_lomac, sizeof(subj->mac_lomac));
525 * Always demote the single label.
527 mac_lomac_copy_single(objlabel, &subj->mac_lomac);
529 * Start with the original range, then minimize each side of
530 * the range to the point of not dominating the object. The
531 * high side will always be demoted, of course.
533 mac_lomac_copy_range(subjlabel, &subj->mac_lomac);
534 if (!mac_lomac_dominate_element(&objlabel->ml_single,
535 &subj->mac_lomac.ml_rangelow))
536 subj->mac_lomac.ml_rangelow = objlabel->ml_single;
537 subj->mac_lomac.ml_rangehigh = objlabel->ml_single;
538 subj->mac_lomac.ml_flags |= MAC_LOMAC_FLAG_UPDATE;
539 thread_lock(curthread);
540 curthread->td_flags |= TDF_ASTPENDING;
541 curthread->td_proc->p_sflag |= PS_MACPEND;
542 thread_unlock(curthread);
545 * Avoid memory allocation while holding a mutex; cache the
548 mac_lomac_copy_single(&subj->mac_lomac, &cached_subjlabel);
549 mtx_unlock(&subj->mtx);
551 sbuf_new(&subjlabel_sb, NULL, 0, SBUF_AUTOEXTEND);
552 mac_lomac_to_string(&subjlabel_sb, subjlabel);
553 sbuf_finish(&subjlabel_sb);
554 subjlabeltext = sbuf_data(&subjlabel_sb);
556 sbuf_new(&subjtext_sb, NULL, 0, SBUF_AUTOEXTEND);
557 mac_lomac_to_string(&subjtext_sb, &subj->mac_lomac);
558 sbuf_finish(&subjtext_sb);
559 subjtext = sbuf_data(&subjtext_sb);
561 sbuf_new(&objlabel_sb, NULL, 0, SBUF_AUTOEXTEND);
562 mac_lomac_to_string(&objlabel_sb, objlabel);
563 sbuf_finish(&objlabel_sb);
564 objlabeltext = sbuf_data(&objlabel_sb);
566 pgid = p->p_pgrp->pg_id; /* XXX could be stale? */
567 if (vp != NULL && VOP_GETATTR(vp, &va, curthread->td_ucred,
569 log(LOG_INFO, "LOMAC: level-%s subject p%dg%du%d:%s demoted to"
570 " level %s after %s a level-%s %s (inode=%ld, "
572 subjlabeltext, p->p_pid, pgid, curthread->td_ucred->cr_uid,
573 p->p_comm, subjtext, actionname, objlabeltext, objname,
574 va.va_fileid, vp->v_mount->mnt_stat.f_mntonname);
576 log(LOG_INFO, "LOMAC: level-%s subject p%dg%du%d:%s demoted to"
577 " level %s after %s a level-%s %s\n",
578 subjlabeltext, p->p_pid, pgid, curthread->td_ucred->cr_uid,
579 p->p_comm, subjtext, actionname, objlabeltext, objname);
582 sbuf_delete(&subjlabel_sb);
583 sbuf_delete(&subjtext_sb);
584 sbuf_delete(&objlabel_sb);
590 * Relabel "to" to "from" only if "from" is a valid label (contains
591 * at least a single), as for a relabel operation which may or may
592 * not involve a relevant label.
595 try_relabel(struct mac_lomac *from, struct mac_lomac *to)
598 if (from->ml_flags & MAC_LOMAC_FLAG_SINGLE) {
599 bzero(to, sizeof(*to));
600 mac_lomac_copy(from, to);
605 * Policy module operations.
608 mac_lomac_init(struct mac_policy_conf *conf)
617 mac_lomac_init_label(struct label *label)
620 SLOT_SET(label, lomac_alloc(M_WAITOK));
624 mac_lomac_init_label_waitcheck(struct label *label, int flag)
627 SLOT_SET(label, lomac_alloc(flag));
628 if (SLOT(label) == NULL)
635 mac_lomac_init_proc_label(struct label *label)
638 PSLOT_SET(label, malloc(sizeof(struct mac_lomac_proc), M_MACLOMAC,
640 mtx_init(&PSLOT(label)->mtx, "MAC/Lomac proc lock", NULL, MTX_DEF);
644 mac_lomac_destroy_label(struct label *label)
647 lomac_free(SLOT(label));
648 SLOT_SET(label, NULL);
652 mac_lomac_destroy_proc_label(struct label *label)
655 mtx_destroy(&PSLOT(label)->mtx);
656 FREE(PSLOT(label), M_MACLOMAC);
657 PSLOT_SET(label, NULL);
661 mac_lomac_element_to_string(struct sbuf *sb, struct mac_lomac_element *element)
664 switch (element->mle_type) {
665 case MAC_LOMAC_TYPE_HIGH:
666 return (sbuf_printf(sb, "high"));
668 case MAC_LOMAC_TYPE_LOW:
669 return (sbuf_printf(sb, "low"));
671 case MAC_LOMAC_TYPE_EQUAL:
672 return (sbuf_printf(sb, "equal"));
674 case MAC_LOMAC_TYPE_GRADE:
675 return (sbuf_printf(sb, "%d", element->mle_grade));
678 panic("mac_lomac_element_to_string: invalid type (%d)",
684 mac_lomac_to_string(struct sbuf *sb, struct mac_lomac *mac_lomac)
687 if (mac_lomac->ml_flags & MAC_LOMAC_FLAG_SINGLE) {
688 if (mac_lomac_element_to_string(sb, &mac_lomac->ml_single)
693 if (mac_lomac->ml_flags & MAC_LOMAC_FLAG_AUX) {
694 if (sbuf_putc(sb, '[') == -1)
697 if (mac_lomac_element_to_string(sb, &mac_lomac->ml_auxsingle)
701 if (sbuf_putc(sb, ']') == -1)
705 if (mac_lomac->ml_flags & MAC_LOMAC_FLAG_RANGE) {
706 if (sbuf_putc(sb, '(') == -1)
709 if (mac_lomac_element_to_string(sb, &mac_lomac->ml_rangelow)
713 if (sbuf_putc(sb, '-') == -1)
716 if (mac_lomac_element_to_string(sb, &mac_lomac->ml_rangehigh)
720 if (sbuf_putc(sb, ')') == -1)
728 mac_lomac_externalize_label(struct label *label, char *element_name,
729 struct sbuf *sb, int *claimed)
731 struct mac_lomac *mac_lomac;
733 if (strcmp(MAC_LOMAC_LABEL_NAME, element_name) != 0)
738 mac_lomac = SLOT(label);
740 return (mac_lomac_to_string(sb, mac_lomac));
744 mac_lomac_parse_element(struct mac_lomac_element *element, char *string)
747 if (strcmp(string, "high") == 0 ||
748 strcmp(string, "hi") == 0) {
749 element->mle_type = MAC_LOMAC_TYPE_HIGH;
750 element->mle_grade = MAC_LOMAC_TYPE_UNDEF;
751 } else if (strcmp(string, "low") == 0 ||
752 strcmp(string, "lo") == 0) {
753 element->mle_type = MAC_LOMAC_TYPE_LOW;
754 element->mle_grade = MAC_LOMAC_TYPE_UNDEF;
755 } else if (strcmp(string, "equal") == 0 ||
756 strcmp(string, "eq") == 0) {
757 element->mle_type = MAC_LOMAC_TYPE_EQUAL;
758 element->mle_grade = MAC_LOMAC_TYPE_UNDEF;
764 d = strtol(p0, &p1, 10);
766 if (d < 0 || d > 65535)
768 element->mle_type = MAC_LOMAC_TYPE_GRADE;
769 element->mle_grade = d;
771 if (p1 == p0 || *p1 != '\0')
779 * Note: destructively consumes the string, make a local copy before
780 * calling if that's a problem.
783 mac_lomac_parse(struct mac_lomac *mac_lomac, char *string)
785 char *range, *rangeend, *rangehigh, *rangelow, *single, *auxsingle,
789 /* Do we have a range? */
791 range = index(string, '(');
794 auxsingle = index(string, '[');
795 if (auxsingle == single)
797 if (range != NULL && auxsingle != NULL)
799 rangelow = rangehigh = NULL;
801 /* Nul terminate the end of the single string. */
805 rangehigh = index(rangelow, '-');
806 if (rangehigh == NULL)
809 if (*rangelow == '\0' || *rangehigh == '\0')
811 rangeend = index(rangehigh, ')');
812 if (rangeend == NULL)
814 if (*(rangeend + 1) != '\0')
816 /* Nul terminate the ends of the ranges. */
817 *(rangehigh - 1) = '\0';
820 KASSERT((rangelow != NULL && rangehigh != NULL) ||
821 (rangelow == NULL && rangehigh == NULL),
822 ("mac_lomac_internalize_label: range mismatch"));
823 if (auxsingle != NULL) {
824 /* Nul terminate the end of the single string. */
827 auxsingleend = index(auxsingle, ']');
828 if (auxsingleend == NULL)
830 if (*(auxsingleend + 1) != '\0')
832 /* Nul terminate the end of the auxsingle. */
833 *auxsingleend = '\0';
836 bzero(mac_lomac, sizeof(*mac_lomac));
837 if (single != NULL) {
838 error = mac_lomac_parse_element(&mac_lomac->ml_single, single);
841 mac_lomac->ml_flags |= MAC_LOMAC_FLAG_SINGLE;
844 if (auxsingle != NULL) {
845 error = mac_lomac_parse_element(&mac_lomac->ml_auxsingle,
849 mac_lomac->ml_flags |= MAC_LOMAC_FLAG_AUX;
852 if (rangelow != NULL) {
853 error = mac_lomac_parse_element(&mac_lomac->ml_rangelow,
857 error = mac_lomac_parse_element(&mac_lomac->ml_rangehigh,
861 mac_lomac->ml_flags |= MAC_LOMAC_FLAG_RANGE;
864 error = mac_lomac_valid(mac_lomac);
872 mac_lomac_internalize_label(struct label *label, char *element_name,
873 char *element_data, int *claimed)
875 struct mac_lomac *mac_lomac, mac_lomac_temp;
878 if (strcmp(MAC_LOMAC_LABEL_NAME, element_name) != 0)
883 error = mac_lomac_parse(&mac_lomac_temp, element_data);
887 mac_lomac = SLOT(label);
888 *mac_lomac = mac_lomac_temp;
894 mac_lomac_copy_label(struct label *src, struct label *dest)
897 *SLOT(dest) = *SLOT(src);
901 * Labeling event operations: file system objects, and things that look
902 * a lot like file system objects.
905 mac_lomac_create_devfs_device(struct ucred *cred, struct mount *mp,
906 struct cdev *dev, struct devfs_dirent *de, struct label *delabel)
908 struct mac_lomac *mac_lomac;
911 mac_lomac = SLOT(delabel);
912 if (strcmp(dev->si_name, "null") == 0 ||
913 strcmp(dev->si_name, "zero") == 0 ||
914 strcmp(dev->si_name, "random") == 0 ||
915 strncmp(dev->si_name, "fd/", strlen("fd/")) == 0 ||
916 strncmp(dev->si_name, "ttyv", strlen("ttyv")) == 0)
917 lomac_type = MAC_LOMAC_TYPE_EQUAL;
918 else if (ptys_equal &&
919 (strncmp(dev->si_name, "ttyp", strlen("ttyp")) == 0 ||
920 strncmp(dev->si_name, "ptyp", strlen("ptyp")) == 0))
921 lomac_type = MAC_LOMAC_TYPE_EQUAL;
923 lomac_type = MAC_LOMAC_TYPE_HIGH;
924 mac_lomac_set_single(mac_lomac, lomac_type, 0);
928 mac_lomac_create_devfs_directory(struct mount *mp, char *dirname,
929 int dirnamelen, struct devfs_dirent *de, struct label *delabel)
931 struct mac_lomac *mac_lomac;
933 mac_lomac = SLOT(delabel);
934 mac_lomac_set_single(mac_lomac, MAC_LOMAC_TYPE_HIGH, 0);
938 mac_lomac_create_devfs_symlink(struct ucred *cred, struct mount *mp,
939 struct devfs_dirent *dd, struct label *ddlabel, struct devfs_dirent *de,
940 struct label *delabel)
942 struct mac_lomac *source, *dest;
944 source = SLOT(cred->cr_label);
945 dest = SLOT(delabel);
947 mac_lomac_copy_single(source, dest);
951 mac_lomac_create_mount(struct ucred *cred, struct mount *mp,
952 struct label *mplabel)
954 struct mac_lomac *source, *dest;
956 source = SLOT(cred->cr_label);
957 dest = SLOT(mplabel);
958 mac_lomac_copy_single(source, dest);
962 mac_lomac_relabel_vnode(struct ucred *cred, struct vnode *vp,
963 struct label *vplabel, struct label *newlabel)
965 struct mac_lomac *source, *dest;
967 source = SLOT(newlabel);
968 dest = SLOT(vplabel);
970 try_relabel(source, dest);
974 mac_lomac_update_devfs(struct mount *mp, struct devfs_dirent *de,
975 struct label *delabel, struct vnode *vp, struct label *vplabel)
977 struct mac_lomac *source, *dest;
979 source = SLOT(vplabel);
980 dest = SLOT(delabel);
982 mac_lomac_copy(source, dest);
986 mac_lomac_associate_vnode_devfs(struct mount *mp, struct label *mplabel,
987 struct devfs_dirent *de, struct label *delabel, struct vnode *vp,
988 struct label *vplabel)
990 struct mac_lomac *source, *dest;
992 source = SLOT(delabel);
993 dest = SLOT(vplabel);
995 mac_lomac_copy_single(source, dest);
999 mac_lomac_associate_vnode_extattr(struct mount *mp, struct label *mplabel,
1000 struct vnode *vp, struct label *vplabel)
1002 struct mac_lomac temp, *source, *dest;
1005 source = SLOT(mplabel);
1006 dest = SLOT(vplabel);
1008 buflen = sizeof(temp);
1009 bzero(&temp, buflen);
1011 error = vn_extattr_get(vp, IO_NODELOCKED, MAC_LOMAC_EXTATTR_NAMESPACE,
1012 MAC_LOMAC_EXTATTR_NAME, &buflen, (char *)&temp, curthread);
1013 if (error == ENOATTR || error == EOPNOTSUPP) {
1014 /* Fall back to the mntlabel. */
1015 mac_lomac_copy_single(source, dest);
1020 if (buflen != sizeof(temp)) {
1021 if (buflen != sizeof(temp) - sizeof(temp.ml_auxsingle)) {
1022 printf("mac_lomac_associate_vnode_extattr: bad size %d\n",
1026 bzero(&temp.ml_auxsingle, sizeof(temp.ml_auxsingle));
1027 buflen = sizeof(temp);
1028 (void)vn_extattr_set(vp, IO_NODELOCKED,
1029 MAC_LOMAC_EXTATTR_NAMESPACE, MAC_LOMAC_EXTATTR_NAME,
1030 buflen, (char *)&temp, curthread);
1032 if (mac_lomac_valid(&temp) != 0) {
1033 printf("mac_lomac_associate_vnode_extattr: invalid\n");
1036 if ((temp.ml_flags & MAC_LOMAC_FLAGS_BOTH) != MAC_LOMAC_FLAG_SINGLE) {
1037 printf("mac_lomac_associate_vnode_extattr: not single\n");
1041 mac_lomac_copy_single(&temp, dest);
1046 mac_lomac_associate_vnode_singlelabel(struct mount *mp,
1047 struct label *mplabel, struct vnode *vp, struct label *vplabel)
1049 struct mac_lomac *source, *dest;
1051 source = SLOT(mplabel);
1052 dest = SLOT(vplabel);
1054 mac_lomac_copy_single(source, dest);
1058 mac_lomac_create_vnode_extattr(struct ucred *cred, struct mount *mp,
1059 struct label *mplabel, struct vnode *dvp, struct label *dvplabel,
1060 struct vnode *vp, struct label *vplabel, struct componentname *cnp)
1062 struct mac_lomac *source, *dest, *dir, temp;
1066 buflen = sizeof(temp);
1067 bzero(&temp, buflen);
1069 source = SLOT(cred->cr_label);
1070 dest = SLOT(vplabel);
1071 dir = SLOT(dvplabel);
1072 if (dir->ml_flags & MAC_LOMAC_FLAG_AUX) {
1073 mac_lomac_copy_auxsingle(dir, &temp);
1074 mac_lomac_set_single(&temp, dir->ml_auxsingle.mle_type,
1075 dir->ml_auxsingle.mle_grade);
1077 mac_lomac_copy_single(source, &temp);
1080 error = vn_extattr_set(vp, IO_NODELOCKED, MAC_LOMAC_EXTATTR_NAMESPACE,
1081 MAC_LOMAC_EXTATTR_NAME, buflen, (char *)&temp, curthread);
1083 mac_lomac_copy(&temp, dest);
1088 mac_lomac_setlabel_vnode_extattr(struct ucred *cred, struct vnode *vp,
1089 struct label *vplabel, struct label *intlabel)
1091 struct mac_lomac *source, temp;
1095 buflen = sizeof(temp);
1096 bzero(&temp, buflen);
1098 source = SLOT(intlabel);
1099 if ((source->ml_flags & MAC_LOMAC_FLAG_SINGLE) == 0)
1102 mac_lomac_copy_single(source, &temp);
1103 error = vn_extattr_set(vp, IO_NODELOCKED, MAC_LOMAC_EXTATTR_NAMESPACE,
1104 MAC_LOMAC_EXTATTR_NAME, buflen, (char *)&temp, curthread);
1109 * Labeling event operations: IPC object.
1112 mac_lomac_create_inpcb_from_socket(struct socket *so, struct label *solabel,
1113 struct inpcb *inp, struct label *inplabel)
1115 struct mac_lomac *source, *dest;
1117 source = SLOT(solabel);
1118 dest = SLOT(inplabel);
1120 mac_lomac_copy_single(source, dest);
1124 mac_lomac_create_mbuf_from_socket(struct socket *so, struct label *solabel,
1125 struct mbuf *m, struct label *mlabel)
1127 struct mac_lomac *source, *dest;
1129 source = SLOT(solabel);
1130 dest = SLOT(mlabel);
1132 mac_lomac_copy_single(source, dest);
1136 mac_lomac_create_socket(struct ucred *cred, struct socket *so,
1137 struct label *solabel)
1139 struct mac_lomac *source, *dest;
1141 source = SLOT(cred->cr_label);
1142 dest = SLOT(solabel);
1144 mac_lomac_copy_single(source, dest);
1148 mac_lomac_create_pipe(struct ucred *cred, struct pipepair *pp,
1149 struct label *pplabel)
1151 struct mac_lomac *source, *dest;
1153 source = SLOT(cred->cr_label);
1154 dest = SLOT(pplabel);
1156 mac_lomac_copy_single(source, dest);
1160 mac_lomac_create_socket_from_socket(struct socket *oldso,
1161 struct label *oldsolabel, struct socket *newso, struct label *newsolabel)
1163 struct mac_lomac *source, *dest;
1165 source = SLOT(oldsolabel);
1166 dest = SLOT(newsolabel);
1168 mac_lomac_copy_single(source, dest);
1172 mac_lomac_relabel_socket(struct ucred *cred, struct socket *so,
1173 struct label *solabel, struct label *newlabel)
1175 struct mac_lomac *source, *dest;
1177 source = SLOT(newlabel);
1178 dest = SLOT(solabel);
1180 try_relabel(source, dest);
1184 mac_lomac_relabel_pipe(struct ucred *cred, struct pipepair *pp,
1185 struct label *pplabel, struct label *newlabel)
1187 struct mac_lomac *source, *dest;
1189 source = SLOT(newlabel);
1190 dest = SLOT(pplabel);
1192 try_relabel(source, dest);
1196 mac_lomac_set_socket_peer_from_mbuf(struct mbuf *m, struct label *mlabel,
1197 struct socket *so, struct label *sopeerlabel)
1199 struct mac_lomac *source, *dest;
1201 source = SLOT(mlabel);
1202 dest = SLOT(sopeerlabel);
1204 mac_lomac_copy_single(source, dest);
1208 * Labeling event operations: network objects.
1211 mac_lomac_set_socket_peer_from_socket(struct socket *oldso,
1212 struct label *oldsolabel, struct socket *newso,
1213 struct label *newsopeerlabel)
1215 struct mac_lomac *source, *dest;
1217 source = SLOT(oldsolabel);
1218 dest = SLOT(newsopeerlabel);
1220 mac_lomac_copy_single(source, dest);
1224 mac_lomac_create_bpfdesc(struct ucred *cred, struct bpf_d *d,
1225 struct label *dlabel)
1227 struct mac_lomac *source, *dest;
1229 source = SLOT(cred->cr_label);
1230 dest = SLOT(dlabel);
1232 mac_lomac_copy_single(source, dest);
1236 mac_lomac_create_ifnet(struct ifnet *ifp, struct label *ifplabel)
1238 char tifname[IFNAMSIZ], *p, *q;
1239 char tiflist[sizeof(trusted_interfaces)];
1240 struct mac_lomac *dest;
1243 dest = SLOT(ifplabel);
1245 if (ifp->if_type == IFT_LOOP) {
1246 grade = MAC_LOMAC_TYPE_EQUAL;
1250 if (trust_all_interfaces) {
1251 grade = MAC_LOMAC_TYPE_HIGH;
1255 grade = MAC_LOMAC_TYPE_LOW;
1257 if (trusted_interfaces[0] == '\0' ||
1258 !strvalid(trusted_interfaces, sizeof(trusted_interfaces)))
1261 bzero(tiflist, sizeof(tiflist));
1262 for (p = trusted_interfaces, q = tiflist; *p != '\0'; p++, q++)
1263 if(*p != ' ' && *p != '\t')
1266 for (p = q = tiflist;; p++) {
1267 if (*p == ',' || *p == '\0') {
1269 if (len < IFNAMSIZ) {
1270 bzero(tifname, sizeof(tifname));
1271 bcopy(q, tifname, len);
1272 if (strcmp(tifname, ifp->if_xname) == 0) {
1273 grade = MAC_LOMAC_TYPE_HIGH;
1279 printf("MAC/LOMAC warning: interface name "
1280 "\"%s\" is too long (must be < %d)\n",
1289 mac_lomac_set_single(dest, grade, 0);
1290 mac_lomac_set_range(dest, grade, 0, grade, 0);
1294 mac_lomac_create_ipq(struct mbuf *m, struct label *mlabel, struct ipq *ipq,
1295 struct label *ipqlabel)
1297 struct mac_lomac *source, *dest;
1299 source = SLOT(mlabel);
1300 dest = SLOT(ipqlabel);
1302 mac_lomac_copy_single(source, dest);
1306 mac_lomac_create_datagram_from_ipq(struct ipq *ipq, struct label *ipqlabel,
1307 struct mbuf *m, struct label *mlabel)
1309 struct mac_lomac *source, *dest;
1311 source = SLOT(ipqlabel);
1312 dest = SLOT(mlabel);
1314 /* Just use the head, since we require them all to match. */
1315 mac_lomac_copy_single(source, dest);
1319 mac_lomac_create_fragment(struct mbuf *m, struct label *mlabel,
1320 struct mbuf *frag, struct label *fraglabel)
1322 struct mac_lomac *source, *dest;
1324 source = SLOT(mlabel);
1325 dest = SLOT(fraglabel);
1327 mac_lomac_copy_single(source, dest);
1331 mac_lomac_create_mbuf_from_inpcb(struct inpcb *inp, struct label *inplabel,
1332 struct mbuf *m, struct label *mlabel)
1334 struct mac_lomac *source, *dest;
1336 source = SLOT(inplabel);
1337 dest = SLOT(mlabel);
1339 mac_lomac_copy_single(source, dest);
1343 mac_lomac_create_mbuf_linklayer(struct ifnet *ifp, struct label *ifplabel,
1344 struct mbuf *m, struct label *mlabel)
1346 struct mac_lomac *dest;
1348 dest = SLOT(mlabel);
1350 mac_lomac_set_single(dest, MAC_LOMAC_TYPE_EQUAL, 0);
1354 mac_lomac_create_mbuf_from_bpfdesc(struct bpf_d *d, struct label *dlabel,
1355 struct mbuf *m, struct label *mlabel)
1357 struct mac_lomac *source, *dest;
1359 source = SLOT(dlabel);
1360 dest = SLOT(mlabel);
1362 mac_lomac_copy_single(source, dest);
1366 mac_lomac_create_mbuf_from_ifnet(struct ifnet *ifp, struct label *ifplabel,
1367 struct mbuf *m, struct label *mlabel)
1369 struct mac_lomac *source, *dest;
1371 source = SLOT(ifplabel);
1372 dest = SLOT(mlabel);
1374 mac_lomac_copy_single(source, dest);
1378 mac_lomac_create_mbuf_multicast_encap(struct mbuf *m, struct label *mlabel,
1379 struct ifnet *ifp, struct label *ifplabel, struct mbuf *mnew,
1380 struct label *mnewlabel)
1382 struct mac_lomac *source, *dest;
1384 source = SLOT(mlabel);
1385 dest = SLOT(mnewlabel);
1387 mac_lomac_copy_single(source, dest);
1391 mac_lomac_create_mbuf_netlayer(struct mbuf *m, struct label *mlabel,
1392 struct mbuf *mnew, struct label *mnewlabel)
1394 struct mac_lomac *source, *dest;
1396 source = SLOT(mlabel);
1397 dest = SLOT(mnewlabel);
1399 mac_lomac_copy_single(source, dest);
1403 mac_lomac_fragment_match(struct mbuf *m, struct label *mlabel,
1404 struct ipq *ipq, struct label *ipqlabel)
1406 struct mac_lomac *a, *b;
1411 return (mac_lomac_equal_single(a, b));
1415 mac_lomac_relabel_ifnet(struct ucred *cred, struct ifnet *ifp,
1416 struct label *ifplabel, struct label *newlabel)
1418 struct mac_lomac *source, *dest;
1420 source = SLOT(newlabel);
1421 dest = SLOT(ifplabel);
1423 try_relabel(source, dest);
1427 mac_lomac_update_ipq(struct mbuf *m, struct label *mlabel, struct ipq *ipq,
1428 struct label *ipqlabel)
1431 /* NOOP: we only accept matching labels, so no need to update */
1435 mac_lomac_inpcb_sosetlabel(struct socket *so, struct label *solabel,
1436 struct inpcb *inp, struct label *inplabel)
1438 struct mac_lomac *source, *dest;
1440 source = SLOT(solabel);
1441 dest = SLOT(inplabel);
1443 mac_lomac_copy_single(source, dest);
1447 mac_lomac_init_syncache_from_inpcb(struct label *label, struct inpcb *inp)
1449 struct mac_lomac *source, *dest;
1451 source = SLOT(inp->inp_label);
1453 mac_lomac_copy(source, dest);
1457 mac_lomac_create_mbuf_from_syncache(struct label *sc_label, struct mbuf *m,
1458 struct label *mlabel)
1460 struct mac_lomac *source, *dest;
1462 source = SLOT(sc_label);
1463 dest = SLOT(mlabel);
1464 mac_lomac_copy(source, dest);
1468 mac_lomac_create_mbuf_from_firewall(struct mbuf *m, struct label *mlabel)
1470 struct mac_lomac *dest;
1472 dest = SLOT(mlabel);
1474 /* XXX: where is the label for the firewall really comming from? */
1475 mac_lomac_set_single(dest, MAC_LOMAC_TYPE_EQUAL, 0);
1479 * Labeling event operations: processes.
1482 mac_lomac_execve_transition(struct ucred *old, struct ucred *new,
1483 struct vnode *vp, struct label *vplabel, struct label *interpvnodelabel,
1484 struct image_params *imgp, struct label *execlabel)
1486 struct mac_lomac *source, *dest, *obj, *robj;
1488 source = SLOT(old->cr_label);
1489 dest = SLOT(new->cr_label);
1490 obj = SLOT(vplabel);
1491 robj = interpvnodelabel != NULL ? SLOT(interpvnodelabel) : obj;
1493 mac_lomac_copy(source, dest);
1495 * If there's an auxiliary label on the real object, respect it
1496 * and assume that this level should be assumed immediately if
1497 * a higher level is currently in place.
1499 if (robj->ml_flags & MAC_LOMAC_FLAG_AUX &&
1500 !mac_lomac_dominate_element(&robj->ml_auxsingle, &dest->ml_single)
1501 && mac_lomac_auxsingle_in_range(robj, dest))
1502 mac_lomac_set_single(dest, robj->ml_auxsingle.mle_type,
1503 robj->ml_auxsingle.mle_grade);
1505 * Restructuring to use the execve transitioning mechanism
1506 * instead of the normal demotion mechanism here would be
1507 * difficult, so just copy the label over and perform standard
1508 * demotion. This is also non-optimal because it will result
1509 * in the intermediate label "new" being created and immediately
1512 if (mac_lomac_enabled && revocation_enabled &&
1513 !mac_lomac_dominate_single(obj, source))
1514 (void)maybe_demote(source, obj, "executing", "file", vp);
1518 mac_lomac_execve_will_transition(struct ucred *old, struct vnode *vp,
1519 struct label *vplabel, struct label *interpvnodelabel,
1520 struct image_params *imgp, struct label *execlabel)
1522 struct mac_lomac *subj, *obj, *robj;
1524 if (!mac_lomac_enabled || !revocation_enabled)
1527 subj = SLOT(old->cr_label);
1528 obj = SLOT(vplabel);
1529 robj = interpvnodelabel != NULL ? SLOT(interpvnodelabel) : obj;
1531 return ((robj->ml_flags & MAC_LOMAC_FLAG_AUX &&
1532 !mac_lomac_dominate_element(&robj->ml_auxsingle, &subj->ml_single)
1533 && mac_lomac_auxsingle_in_range(robj, subj)) ||
1534 !mac_lomac_dominate_single(obj, subj));
1538 mac_lomac_create_proc0(struct ucred *cred)
1540 struct mac_lomac *dest;
1542 dest = SLOT(cred->cr_label);
1544 mac_lomac_set_single(dest, MAC_LOMAC_TYPE_EQUAL, 0);
1545 mac_lomac_set_range(dest, MAC_LOMAC_TYPE_LOW, 0, MAC_LOMAC_TYPE_HIGH,
1550 mac_lomac_create_proc1(struct ucred *cred)
1552 struct mac_lomac *dest;
1554 dest = SLOT(cred->cr_label);
1556 mac_lomac_set_single(dest, MAC_LOMAC_TYPE_HIGH, 0);
1557 mac_lomac_set_range(dest, MAC_LOMAC_TYPE_LOW, 0, MAC_LOMAC_TYPE_HIGH,
1562 mac_lomac_relabel_cred(struct ucred *cred, struct label *newlabel)
1564 struct mac_lomac *source, *dest;
1566 source = SLOT(newlabel);
1567 dest = SLOT(cred->cr_label);
1569 try_relabel(source, dest);
1573 * Access control checks.
1576 mac_lomac_check_bpfdesc_receive(struct bpf_d *d, struct label *dlabel,
1577 struct ifnet *ifp, struct label *ifplabel)
1579 struct mac_lomac *a, *b;
1581 if (!mac_lomac_enabled)
1587 if (mac_lomac_equal_single(a, b))
1593 mac_lomac_check_cred_relabel(struct ucred *cred, struct label *newlabel)
1595 struct mac_lomac *subj, *new;
1598 subj = SLOT(cred->cr_label);
1599 new = SLOT(newlabel);
1602 * If there is a LOMAC label update for the credential, it may
1603 * be an update of the single, range, or both.
1605 error = lomac_atmostflags(new, MAC_LOMAC_FLAGS_BOTH);
1610 * If the LOMAC label is to be changed, authorize as appropriate.
1612 if (new->ml_flags & MAC_LOMAC_FLAGS_BOTH) {
1614 * Fill in the missing parts from the previous label.
1616 if ((new->ml_flags & MAC_LOMAC_FLAG_SINGLE) == 0)
1617 mac_lomac_copy_single(subj, new);
1618 if ((new->ml_flags & MAC_LOMAC_FLAG_RANGE) == 0)
1619 mac_lomac_copy_range(subj, new);
1622 * To change the LOMAC range on a credential, the new
1623 * range label must be in the current range.
1625 if (!mac_lomac_range_in_range(new, subj))
1629 * To change the LOMAC single label on a credential, the
1630 * new single label must be in the new range. Implicitly
1631 * from the previous check, the new single is in the old
1634 if (!mac_lomac_single_in_range(new, new))
1638 * To have EQUAL in any component of the new credential
1639 * LOMAC label, the subject must already have EQUAL in
1642 if (mac_lomac_contains_equal(new)) {
1643 error = mac_lomac_subject_privileged(subj);
1649 * XXXMAC: Additional consistency tests regarding the
1650 * single and range of the new label might be performed
1659 mac_lomac_check_cred_visible(struct ucred *cr1, struct ucred *cr2)
1661 struct mac_lomac *subj, *obj;
1663 if (!mac_lomac_enabled)
1666 subj = SLOT(cr1->cr_label);
1667 obj = SLOT(cr2->cr_label);
1670 if (!mac_lomac_dominate_single(obj, subj))
1677 mac_lomac_check_ifnet_relabel(struct ucred *cred, struct ifnet *ifp,
1678 struct label *ifplabel, struct label *newlabel)
1680 struct mac_lomac *subj, *new;
1683 subj = SLOT(cred->cr_label);
1684 new = SLOT(newlabel);
1687 * If there is a LOMAC label update for the interface, it may
1688 * be an update of the single, range, or both.
1690 error = lomac_atmostflags(new, MAC_LOMAC_FLAGS_BOTH);
1695 * Relabling network interfaces requires LOMAC privilege.
1697 error = mac_lomac_subject_privileged(subj);
1702 * If the LOMAC label is to be changed, authorize as appropriate.
1704 if (new->ml_flags & MAC_LOMAC_FLAGS_BOTH) {
1706 * Fill in the missing parts from the previous label.
1708 if ((new->ml_flags & MAC_LOMAC_FLAG_SINGLE) == 0)
1709 mac_lomac_copy_single(subj, new);
1710 if ((new->ml_flags & MAC_LOMAC_FLAG_RANGE) == 0)
1711 mac_lomac_copy_range(subj, new);
1714 * Rely on the traditional superuser status for the LOMAC
1715 * interface relabel requirements. XXXMAC: This will go
1718 * XXXRW: This is also redundant to a higher layer check.
1720 error = priv_check_cred(cred, PRIV_NET_SETIFMAC, 0);
1725 * XXXMAC: Additional consistency tests regarding the single
1726 * and the range of the new label might be performed here.
1734 mac_lomac_check_ifnet_transmit(struct ifnet *ifp, struct label *ifplabel,
1735 struct mbuf *m, struct label *mlabel)
1737 struct mac_lomac *p, *i;
1739 if (!mac_lomac_enabled)
1745 return (mac_lomac_single_in_range(p, i) ? 0 : EACCES);
1749 mac_lomac_check_inpcb_deliver(struct inpcb *inp, struct label *inplabel,
1750 struct mbuf *m, struct label *mlabel)
1752 struct mac_lomac *p, *i;
1754 if (!mac_lomac_enabled)
1760 return (mac_lomac_equal_single(p, i) ? 0 : EACCES);
1764 mac_lomac_check_kld_load(struct ucred *cred, struct vnode *vp,
1765 struct label *vplabel)
1767 struct mac_lomac *subj, *obj;
1769 if (!mac_lomac_enabled)
1772 subj = SLOT(cred->cr_label);
1773 obj = SLOT(vplabel);
1775 if (mac_lomac_subject_privileged(subj))
1778 if (!mac_lomac_high_single(obj))
1785 mac_lomac_check_pipe_ioctl(struct ucred *cred, struct pipepair *pp,
1786 struct label *pplabel, unsigned long cmd, void /* caddr_t */ *data)
1789 if (!mac_lomac_enabled)
1792 /* XXX: This will be implemented soon... */
1798 mac_lomac_check_pipe_read(struct ucred *cred, struct pipepair *pp,
1799 struct label *pplabel)
1801 struct mac_lomac *subj, *obj;
1803 if (!mac_lomac_enabled)
1806 subj = SLOT(cred->cr_label);
1807 obj = SLOT(pplabel);
1809 if (!mac_lomac_dominate_single(obj, subj))
1810 return (maybe_demote(subj, obj, "reading", "pipe", NULL));
1816 mac_lomac_check_pipe_relabel(struct ucred *cred, struct pipepair *pp,
1817 struct label *pplabel, struct label *newlabel)
1819 struct mac_lomac *subj, *obj, *new;
1822 new = SLOT(newlabel);
1823 subj = SLOT(cred->cr_label);
1824 obj = SLOT(pplabel);
1827 * If there is a LOMAC label update for a pipe, it must be a
1830 error = lomac_atmostflags(new, MAC_LOMAC_FLAG_SINGLE);
1835 * To perform a relabel of a pipe (LOMAC label or not), LOMAC must
1836 * authorize the relabel.
1838 if (!mac_lomac_single_in_range(obj, subj))
1842 * If the LOMAC label is to be changed, authorize as appropriate.
1844 if (new->ml_flags & MAC_LOMAC_FLAG_SINGLE) {
1846 * To change the LOMAC label on a pipe, the new pipe label
1847 * must be in the subject range.
1849 if (!mac_lomac_single_in_range(new, subj))
1853 * To change the LOMAC label on a pipe to be EQUAL, the
1854 * subject must have appropriate privilege.
1856 if (mac_lomac_contains_equal(new)) {
1857 error = mac_lomac_subject_privileged(subj);
1867 mac_lomac_check_pipe_write(struct ucred *cred, struct pipepair *pp,
1868 struct label *pplabel)
1870 struct mac_lomac *subj, *obj;
1872 if (!mac_lomac_enabled)
1875 subj = SLOT(cred->cr_label);
1876 obj = SLOT(pplabel);
1878 if (!mac_lomac_subject_dominate(subj, obj))
1885 mac_lomac_check_proc_debug(struct ucred *cred, struct proc *p)
1887 struct mac_lomac *subj, *obj;
1889 if (!mac_lomac_enabled)
1892 subj = SLOT(cred->cr_label);
1893 obj = SLOT(p->p_ucred->cr_label);
1895 /* XXX: range checks */
1896 if (!mac_lomac_dominate_single(obj, subj))
1898 if (!mac_lomac_subject_dominate(subj, obj))
1905 mac_lomac_check_proc_sched(struct ucred *cred, struct proc *p)
1907 struct mac_lomac *subj, *obj;
1909 if (!mac_lomac_enabled)
1912 subj = SLOT(cred->cr_label);
1913 obj = SLOT(p->p_ucred->cr_label);
1915 /* XXX: range checks */
1916 if (!mac_lomac_dominate_single(obj, subj))
1918 if (!mac_lomac_subject_dominate(subj, obj))
1925 mac_lomac_check_proc_signal(struct ucred *cred, struct proc *p, int signum)
1927 struct mac_lomac *subj, *obj;
1929 if (!mac_lomac_enabled)
1932 subj = SLOT(cred->cr_label);
1933 obj = SLOT(p->p_ucred->cr_label);
1935 /* XXX: range checks */
1936 if (!mac_lomac_dominate_single(obj, subj))
1938 if (!mac_lomac_subject_dominate(subj, obj))
1945 mac_lomac_check_socket_deliver(struct socket *so, struct label *solabel,
1946 struct mbuf *m, struct label *mlabel)
1948 struct mac_lomac *p, *s;
1950 if (!mac_lomac_enabled)
1956 return (mac_lomac_equal_single(p, s) ? 0 : EACCES);
1960 mac_lomac_check_socket_relabel(struct ucred *cred, struct socket *so,
1961 struct label *solabel, struct label *newlabel)
1963 struct mac_lomac *subj, *obj, *new;
1966 new = SLOT(newlabel);
1967 subj = SLOT(cred->cr_label);
1968 obj = SLOT(solabel);
1971 * If there is a LOMAC label update for the socket, it may be
1972 * an update of single.
1974 error = lomac_atmostflags(new, MAC_LOMAC_FLAG_SINGLE);
1979 * To relabel a socket, the old socket single must be in the subject
1982 if (!mac_lomac_single_in_range(obj, subj))
1986 * If the LOMAC label is to be changed, authorize as appropriate.
1988 if (new->ml_flags & MAC_LOMAC_FLAG_SINGLE) {
1990 * To relabel a socket, the new socket single must be in
1991 * the subject range.
1993 if (!mac_lomac_single_in_range(new, subj))
1997 * To change the LOMAC label on the socket to contain EQUAL,
1998 * the subject must have appropriate privilege.
2000 if (mac_lomac_contains_equal(new)) {
2001 error = mac_lomac_subject_privileged(subj);
2011 mac_lomac_check_socket_visible(struct ucred *cred, struct socket *so,
2012 struct label *solabel)
2014 struct mac_lomac *subj, *obj;
2016 if (!mac_lomac_enabled)
2019 subj = SLOT(cred->cr_label);
2020 obj = SLOT(solabel);
2022 if (!mac_lomac_dominate_single(obj, subj))
2029 * Some system privileges are allowed regardless of integrity grade; others
2030 * are allowed only when running with privilege with respect to the LOMAC
2031 * policy as they might otherwise allow bypassing of the integrity policy.
2034 mac_lomac_priv_check(struct ucred *cred, int priv)
2036 struct mac_lomac *subj;
2039 if (!mac_lomac_enabled)
2043 * Exempt only specific privileges from the LOMAC integrity policy.
2050 * Allow processes to manipulate basic process audit properties, and
2051 * to submit audit records.
2053 case PRIV_AUDIT_GETAUDIT:
2054 case PRIV_AUDIT_SETAUDIT:
2055 case PRIV_AUDIT_SUBMIT:
2058 * Allow processes to manipulate their regular UNIX credentials.
2060 case PRIV_CRED_SETUID:
2061 case PRIV_CRED_SETEUID:
2062 case PRIV_CRED_SETGID:
2063 case PRIV_CRED_SETEGID:
2064 case PRIV_CRED_SETGROUPS:
2065 case PRIV_CRED_SETREUID:
2066 case PRIV_CRED_SETREGID:
2067 case PRIV_CRED_SETRESUID:
2068 case PRIV_CRED_SETRESGID:
2071 * Allow processes to perform system monitoring.
2073 case PRIV_SEEOTHERGIDS:
2074 case PRIV_SEEOTHERUIDS:
2078 * Allow access to general process debugging facilities. We
2079 * separately control debugging based on MAC label.
2081 case PRIV_DEBUG_DIFFCRED:
2082 case PRIV_DEBUG_SUGID:
2083 case PRIV_DEBUG_UNPRIV:
2086 * Allow manipulating jails.
2088 case PRIV_JAIL_ATTACH:
2091 * Allow privilege with respect to the Partition policy, but not the
2094 case PRIV_MAC_PARTITION:
2097 * Allow privilege with respect to process resource limits and login
2100 case PRIV_PROC_LIMIT:
2101 case PRIV_PROC_SETLOGIN:
2102 case PRIV_PROC_SETRLIMIT:
2105 * Allow System V and POSIX IPC privileges.
2108 case PRIV_IPC_WRITE:
2109 case PRIV_IPC_ADMIN:
2110 case PRIV_IPC_MSGSIZE:
2114 * Allow certain scheduler manipulations -- possibly this should be
2115 * controlled by more fine-grained policy, as potentially low
2116 * integrity processes can deny CPU to higher integrity ones.
2118 case PRIV_SCHED_DIFFCRED:
2119 case PRIV_SCHED_SETPRIORITY:
2120 case PRIV_SCHED_RTPRIO:
2121 case PRIV_SCHED_SETPOLICY:
2122 case PRIV_SCHED_SET:
2123 case PRIV_SCHED_SETPARAM:
2126 * More IPC privileges.
2128 case PRIV_SEM_WRITE:
2131 * Allow signaling privileges subject to integrity policy.
2133 case PRIV_SIGNAL_DIFFCRED:
2134 case PRIV_SIGNAL_SUGID:
2137 * Allow access to only limited sysctls from lower integrity levels;
2138 * piggy-back on the Jail definition.
2140 case PRIV_SYSCTL_WRITEJAIL:
2143 * Allow TTY-based privileges, subject to general device access using
2144 * labels on TTY device nodes, but not console privilege.
2146 case PRIV_TTY_DRAINWAIT:
2147 case PRIV_TTY_DTRWAIT:
2148 case PRIV_TTY_EXCLUSIVE:
2149 case PRIV_TTY_PRISON:
2154 * Grant most VFS privileges, as almost all are in practice bounded
2155 * by more specific checks using labels.
2158 case PRIV_VFS_WRITE:
2159 case PRIV_VFS_ADMIN:
2161 case PRIV_VFS_LOOKUP:
2162 case PRIV_VFS_CHFLAGS_DEV:
2163 case PRIV_VFS_CHOWN:
2164 case PRIV_VFS_CHROOT:
2165 case PRIV_VFS_RETAINSUGID:
2166 case PRIV_VFS_EXCEEDQUOTA:
2167 case PRIV_VFS_FCHROOT:
2168 case PRIV_VFS_FHOPEN:
2169 case PRIV_VFS_FHSTATFS:
2170 case PRIV_VFS_GENERATION:
2171 case PRIV_VFS_GETFH:
2172 case PRIV_VFS_GETQUOTA:
2174 case PRIV_VFS_MOUNT:
2175 case PRIV_VFS_MOUNT_OWNER:
2176 case PRIV_VFS_MOUNT_PERM:
2177 case PRIV_VFS_MOUNT_SUIDDIR:
2178 case PRIV_VFS_MOUNT_NONUSER:
2179 case PRIV_VFS_SETGID:
2180 case PRIV_VFS_STICKYFILE:
2181 case PRIV_VFS_SYSFLAGS:
2182 case PRIV_VFS_UNMOUNT:
2185 * Allow VM privileges; it would be nice if these were subject to
2188 case PRIV_VM_MADV_PROTECT:
2190 case PRIV_VM_MUNLOCK:
2193 * Allow some but not all network privileges. In general, dont allow
2194 * reconfiguring the network stack, just normal use.
2196 case PRIV_NETATALK_RESERVEDPORT:
2197 case PRIV_NETINET_RESERVEDPORT:
2198 case PRIV_NETINET_RAW:
2199 case PRIV_NETINET_REUSEPORT:
2200 case PRIV_NETIPX_RESERVEDPORT:
2201 case PRIV_NETIPX_RAW:
2205 * All remaining system privileges are allow only if the process
2206 * holds privilege with respect to the LOMAC policy.
2209 subj = SLOT(cred->cr_label);
2210 error = mac_lomac_subject_privileged(subj);
2219 mac_lomac_check_system_acct(struct ucred *cred, struct vnode *vp,
2220 struct label *vplabel)
2222 struct mac_lomac *subj, *obj;
2224 if (!mac_lomac_enabled)
2227 subj = SLOT(cred->cr_label);
2228 obj = SLOT(vplabel);
2230 if (mac_lomac_subject_privileged(subj))
2233 if (!mac_lomac_high_single(obj))
2240 mac_lomac_check_system_auditctl(struct ucred *cred, struct vnode *vp,
2241 struct label *vplabel)
2243 struct mac_lomac *subj, *obj;
2245 if (!mac_lomac_enabled)
2248 subj = SLOT(cred->cr_label);
2249 obj = SLOT(vplabel);
2251 if (mac_lomac_subject_privileged(subj))
2254 if (!mac_lomac_high_single(obj))
2261 mac_lomac_check_system_swapoff(struct ucred *cred, struct vnode *vp,
2262 struct label *vplabel)
2264 struct mac_lomac *subj;
2266 if (!mac_lomac_enabled)
2269 subj = SLOT(cred->cr_label);
2271 if (mac_lomac_subject_privileged(subj))
2278 mac_lomac_check_system_swapon(struct ucred *cred, struct vnode *vp,
2279 struct label *vplabel)
2281 struct mac_lomac *subj, *obj;
2283 if (!mac_lomac_enabled)
2286 subj = SLOT(cred->cr_label);
2287 obj = SLOT(vplabel);
2289 if (mac_lomac_subject_privileged(subj))
2292 if (!mac_lomac_high_single(obj))
2299 mac_lomac_check_system_sysctl(struct ucred *cred, struct sysctl_oid *oidp,
2300 void *arg1, int arg2, struct sysctl_req *req)
2302 struct mac_lomac *subj;
2304 if (!mac_lomac_enabled)
2307 subj = SLOT(cred->cr_label);
2310 * Treat sysctl variables without CTLFLAG_ANYBODY flag as
2311 * lomac/high, but also require privilege to change them.
2313 if (req->newptr != NULL && (oidp->oid_kind & CTLFLAG_ANYBODY) == 0) {
2315 if (!mac_lomac_subject_dominate_high(subj))
2319 if (mac_lomac_subject_privileged(subj))
2327 mac_lomac_check_vnode_create(struct ucred *cred, struct vnode *dvp,
2328 struct label *dvplabel, struct componentname *cnp, struct vattr *vap)
2330 struct mac_lomac *subj, *obj;
2332 if (!mac_lomac_enabled)
2335 subj = SLOT(cred->cr_label);
2336 obj = SLOT(dvplabel);
2338 if (!mac_lomac_subject_dominate(subj, obj))
2340 if (obj->ml_flags & MAC_LOMAC_FLAG_AUX &&
2341 !mac_lomac_dominate_element(&subj->ml_single, &obj->ml_auxsingle))
2348 mac_lomac_check_vnode_delete(struct ucred *cred, struct vnode *dvp,
2349 struct label *dvplabel, struct vnode *vp, struct label *vplabel,
2350 struct componentname *cnp)
2352 struct mac_lomac *subj, *obj;
2354 if (!mac_lomac_enabled)
2357 subj = SLOT(cred->cr_label);
2358 obj = SLOT(dvplabel);
2360 if (!mac_lomac_subject_dominate(subj, obj))
2363 obj = SLOT(vplabel);
2365 if (!mac_lomac_subject_dominate(subj, obj))
2372 mac_lomac_check_vnode_deleteacl(struct ucred *cred, struct vnode *vp,
2373 struct label *vplabel, acl_type_t type)
2375 struct mac_lomac *subj, *obj;
2377 if (!mac_lomac_enabled)
2380 subj = SLOT(cred->cr_label);
2381 obj = SLOT(vplabel);
2383 if (!mac_lomac_subject_dominate(subj, obj))
2390 mac_lomac_check_vnode_link(struct ucred *cred, struct vnode *dvp,
2391 struct label *dvplabel, struct vnode *vp, struct label *vplabel,
2392 struct componentname *cnp)
2394 struct mac_lomac *subj, *obj;
2396 if (!mac_lomac_enabled)
2399 subj = SLOT(cred->cr_label);
2400 obj = SLOT(dvplabel);
2402 if (!mac_lomac_subject_dominate(subj, obj))
2405 obj = SLOT(vplabel);
2407 if (!mac_lomac_subject_dominate(subj, obj))
2414 mac_lomac_check_vnode_mmap(struct ucred *cred, struct vnode *vp,
2415 struct label *vplabel, int prot, int flags)
2417 struct mac_lomac *subj, *obj;
2420 * Rely on the use of open()-time protections to handle
2421 * non-revocation cases.
2423 if (!mac_lomac_enabled)
2426 subj = SLOT(cred->cr_label);
2427 obj = SLOT(vplabel);
2429 if (((prot & VM_PROT_WRITE) != 0) && ((flags & MAP_SHARED) != 0)) {
2430 if (!mac_lomac_subject_dominate(subj, obj))
2433 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
2434 if (!mac_lomac_dominate_single(obj, subj))
2435 return (maybe_demote(subj, obj, "mapping", "file", vp));
2442 mac_lomac_check_vnode_mmap_downgrade(struct ucred *cred, struct vnode *vp,
2443 struct label *vplabel, /* XXX vm_prot_t */ int *prot)
2445 struct mac_lomac *subj, *obj;
2448 * Rely on the use of open()-time protections to handle
2449 * non-revocation cases.
2451 if (!mac_lomac_enabled || !revocation_enabled)
2454 subj = SLOT(cred->cr_label);
2455 obj = SLOT(vplabel);
2457 if (!mac_lomac_subject_dominate(subj, obj))
2458 *prot &= ~VM_PROT_WRITE;
2462 mac_lomac_check_vnode_open(struct ucred *cred, struct vnode *vp,
2463 struct label *vplabel, int acc_mode)
2465 struct mac_lomac *subj, *obj;
2467 if (!mac_lomac_enabled)
2470 subj = SLOT(cred->cr_label);
2471 obj = SLOT(vplabel);
2473 /* XXX privilege override for admin? */
2474 if (acc_mode & (VWRITE | VAPPEND | VADMIN)) {
2475 if (!mac_lomac_subject_dominate(subj, obj))
2483 mac_lomac_check_vnode_read(struct ucred *active_cred, struct ucred *file_cred,
2484 struct vnode *vp, struct label *vplabel)
2486 struct mac_lomac *subj, *obj;
2488 if (!mac_lomac_enabled || !revocation_enabled)
2491 subj = SLOT(active_cred->cr_label);
2492 obj = SLOT(vplabel);
2494 if (!mac_lomac_dominate_single(obj, subj))
2495 return (maybe_demote(subj, obj, "reading", "file", vp));
2501 mac_lomac_check_vnode_relabel(struct ucred *cred, struct vnode *vp,
2502 struct label *vplabel, struct label *newlabel)
2504 struct mac_lomac *old, *new, *subj;
2507 old = SLOT(vplabel);
2508 new = SLOT(newlabel);
2509 subj = SLOT(cred->cr_label);
2512 * If there is a LOMAC label update for the vnode, it must be a
2513 * single label, with an optional explicit auxiliary single.
2515 error = lomac_atmostflags(new,
2516 MAC_LOMAC_FLAG_SINGLE | MAC_LOMAC_FLAG_AUX);
2521 * To perform a relabel of the vnode (LOMAC label or not), LOMAC must
2522 * authorize the relabel.
2524 if (!mac_lomac_single_in_range(old, subj))
2528 * If the LOMAC label is to be changed, authorize as appropriate.
2530 if (new->ml_flags & MAC_LOMAC_FLAG_SINGLE) {
2532 * To change the LOMAC label on a vnode, the new vnode label
2533 * must be in the subject range.
2535 if (!mac_lomac_single_in_range(new, subj))
2539 * To change the LOMAC label on the vnode to be EQUAL,
2540 * the subject must have appropriate privilege.
2542 if (mac_lomac_contains_equal(new)) {
2543 error = mac_lomac_subject_privileged(subj);
2548 if (new->ml_flags & MAC_LOMAC_FLAG_AUX) {
2550 * Fill in the missing parts from the previous label.
2552 if ((new->ml_flags & MAC_LOMAC_FLAG_SINGLE) == 0)
2553 mac_lomac_copy_single(subj, new);
2556 * To change the auxiliary LOMAC label on a vnode, the new
2557 * vnode label must be in the subject range.
2559 if (!mac_lomac_auxsingle_in_range(new, subj))
2563 * To change the auxiliary LOMAC label on the vnode to be
2564 * EQUAL, the subject must have appropriate privilege.
2566 if (mac_lomac_contains_equal(new)) {
2567 error = mac_lomac_subject_privileged(subj);
2577 mac_lomac_check_vnode_rename_from(struct ucred *cred, struct vnode *dvp,
2578 struct label *dvplabel, struct vnode *vp, struct label *vplabel,
2579 struct componentname *cnp)
2581 struct mac_lomac *subj, *obj;
2583 if (!mac_lomac_enabled)
2586 subj = SLOT(cred->cr_label);
2587 obj = SLOT(dvplabel);
2589 if (!mac_lomac_subject_dominate(subj, obj))
2592 obj = SLOT(vplabel);
2594 if (!mac_lomac_subject_dominate(subj, obj))
2601 mac_lomac_check_vnode_rename_to(struct ucred *cred, struct vnode *dvp,
2602 struct label *dvplabel, struct vnode *vp, struct label *vplabel,
2603 int samedir, struct componentname *cnp)
2605 struct mac_lomac *subj, *obj;
2607 if (!mac_lomac_enabled)
2610 subj = SLOT(cred->cr_label);
2611 obj = SLOT(dvplabel);
2613 if (!mac_lomac_subject_dominate(subj, obj))
2617 obj = SLOT(vplabel);
2619 if (!mac_lomac_subject_dominate(subj, obj))
2627 mac_lomac_check_vnode_revoke(struct ucred *cred, struct vnode *vp,
2628 struct label *vplabel)
2630 struct mac_lomac *subj, *obj;
2632 if (!mac_lomac_enabled)
2635 subj = SLOT(cred->cr_label);
2636 obj = SLOT(vplabel);
2638 if (!mac_lomac_subject_dominate(subj, obj))
2645 mac_lomac_check_vnode_setacl(struct ucred *cred, struct vnode *vp,
2646 struct label *vplabel, acl_type_t type, struct acl *acl)
2648 struct mac_lomac *subj, *obj;
2650 if (!mac_lomac_enabled)
2653 subj = SLOT(cred->cr_label);
2654 obj = SLOT(vplabel);
2656 if (!mac_lomac_subject_dominate(subj, obj))
2663 mac_lomac_check_vnode_setextattr(struct ucred *cred, struct vnode *vp,
2664 struct label *vplabel, int attrnamespace, const char *name,
2667 struct mac_lomac *subj, *obj;
2669 if (!mac_lomac_enabled)
2672 subj = SLOT(cred->cr_label);
2673 obj = SLOT(vplabel);
2675 if (!mac_lomac_subject_dominate(subj, obj))
2678 /* XXX: protect the MAC EA in a special way? */
2684 mac_lomac_check_vnode_setflags(struct ucred *cred, struct vnode *vp,
2685 struct label *vplabel, u_long flags)
2687 struct mac_lomac *subj, *obj;
2689 if (!mac_lomac_enabled)
2692 subj = SLOT(cred->cr_label);
2693 obj = SLOT(vplabel);
2695 if (!mac_lomac_subject_dominate(subj, obj))
2702 mac_lomac_check_vnode_setmode(struct ucred *cred, struct vnode *vp,
2703 struct label *vplabel, mode_t mode)
2705 struct mac_lomac *subj, *obj;
2707 if (!mac_lomac_enabled)
2710 subj = SLOT(cred->cr_label);
2711 obj = SLOT(vplabel);
2713 if (!mac_lomac_subject_dominate(subj, obj))
2720 mac_lomac_check_vnode_setowner(struct ucred *cred, struct vnode *vp,
2721 struct label *vplabel, uid_t uid, gid_t gid)
2723 struct mac_lomac *subj, *obj;
2725 if (!mac_lomac_enabled)
2728 subj = SLOT(cred->cr_label);
2729 obj = SLOT(vplabel);
2731 if (!mac_lomac_subject_dominate(subj, obj))
2738 mac_lomac_check_vnode_setutimes(struct ucred *cred, struct vnode *vp,
2739 struct label *vplabel, struct timespec atime, struct timespec mtime)
2741 struct mac_lomac *subj, *obj;
2743 if (!mac_lomac_enabled)
2746 subj = SLOT(cred->cr_label);
2747 obj = SLOT(vplabel);
2749 if (!mac_lomac_subject_dominate(subj, obj))
2756 mac_lomac_check_vnode_write(struct ucred *active_cred,
2757 struct ucred *file_cred, struct vnode *vp, struct label *vplabel)
2759 struct mac_lomac *subj, *obj;
2761 if (!mac_lomac_enabled || !revocation_enabled)
2764 subj = SLOT(active_cred->cr_label);
2765 obj = SLOT(vplabel);
2767 if (!mac_lomac_subject_dominate(subj, obj))
2774 mac_lomac_thread_userret(struct thread *td)
2776 struct proc *p = td->td_proc;
2777 struct mac_lomac_proc *subj = PSLOT(p->p_label);
2778 struct ucred *newcred, *oldcred;
2781 mtx_lock(&subj->mtx);
2782 if (subj->mac_lomac.ml_flags & MAC_LOMAC_FLAG_UPDATE) {
2784 mtx_unlock(&subj->mtx);
2787 * Prevent a lock order reversal in
2788 * mac_cred_mmapped_drop_perms; ideally, the other
2789 * user of subj->mtx wouldn't be holding Giant.
2793 mtx_lock(&subj->mtx);
2795 * Check if we lost the race while allocating the cred.
2797 if ((subj->mac_lomac.ml_flags & MAC_LOMAC_FLAG_UPDATE) == 0) {
2801 oldcred = p->p_ucred;
2802 crcopy(newcred, oldcred);
2804 mac_lomac_copy(&subj->mac_lomac, SLOT(newcred->cr_label));
2805 p->p_ucred = newcred;
2809 mtx_unlock(&subj->mtx);
2812 mac_cred_mmapped_drop_perms(curthread, newcred);
2815 mtx_unlock(&subj->mtx);
2819 static struct mac_policy_ops mac_lomac_ops =
2821 .mpo_init = mac_lomac_init,
2822 .mpo_init_bpfdesc_label = mac_lomac_init_label,
2823 .mpo_init_cred_label = mac_lomac_init_label,
2824 .mpo_init_devfs_label = mac_lomac_init_label,
2825 .mpo_init_ifnet_label = mac_lomac_init_label,
2826 .mpo_init_syncache_label = mac_lomac_init_label_waitcheck,
2827 .mpo_init_inpcb_label = mac_lomac_init_label_waitcheck,
2828 .mpo_init_ipq_label = mac_lomac_init_label_waitcheck,
2829 .mpo_init_mbuf_label = mac_lomac_init_label_waitcheck,
2830 .mpo_init_mount_label = mac_lomac_init_label,
2831 .mpo_init_pipe_label = mac_lomac_init_label,
2832 .mpo_init_proc_label = mac_lomac_init_proc_label,
2833 .mpo_init_socket_label = mac_lomac_init_label_waitcheck,
2834 .mpo_init_socket_peer_label = mac_lomac_init_label_waitcheck,
2835 .mpo_init_vnode_label = mac_lomac_init_label,
2836 .mpo_init_syncache_from_inpcb = mac_lomac_init_syncache_from_inpcb,
2837 .mpo_destroy_bpfdesc_label = mac_lomac_destroy_label,
2838 .mpo_destroy_cred_label = mac_lomac_destroy_label,
2839 .mpo_destroy_devfs_label = mac_lomac_destroy_label,
2840 .mpo_destroy_ifnet_label = mac_lomac_destroy_label,
2841 .mpo_destroy_inpcb_label = mac_lomac_destroy_label,
2842 .mpo_destroy_ipq_label = mac_lomac_destroy_label,
2843 .mpo_destroy_mbuf_label = mac_lomac_destroy_label,
2844 .mpo_destroy_mount_label = mac_lomac_destroy_label,
2845 .mpo_destroy_pipe_label = mac_lomac_destroy_label,
2846 .mpo_destroy_proc_label = mac_lomac_destroy_proc_label,
2847 .mpo_destroy_syncache_label = mac_lomac_destroy_label,
2848 .mpo_destroy_socket_label = mac_lomac_destroy_label,
2849 .mpo_destroy_socket_peer_label = mac_lomac_destroy_label,
2850 .mpo_destroy_vnode_label = mac_lomac_destroy_label,
2851 .mpo_copy_cred_label = mac_lomac_copy_label,
2852 .mpo_copy_ifnet_label = mac_lomac_copy_label,
2853 .mpo_copy_mbuf_label = mac_lomac_copy_label,
2854 .mpo_copy_pipe_label = mac_lomac_copy_label,
2855 .mpo_copy_socket_label = mac_lomac_copy_label,
2856 .mpo_copy_vnode_label = mac_lomac_copy_label,
2857 .mpo_externalize_cred_label = mac_lomac_externalize_label,
2858 .mpo_externalize_ifnet_label = mac_lomac_externalize_label,
2859 .mpo_externalize_pipe_label = mac_lomac_externalize_label,
2860 .mpo_externalize_socket_label = mac_lomac_externalize_label,
2861 .mpo_externalize_socket_peer_label = mac_lomac_externalize_label,
2862 .mpo_externalize_vnode_label = mac_lomac_externalize_label,
2863 .mpo_internalize_cred_label = mac_lomac_internalize_label,
2864 .mpo_internalize_ifnet_label = mac_lomac_internalize_label,
2865 .mpo_internalize_pipe_label = mac_lomac_internalize_label,
2866 .mpo_internalize_socket_label = mac_lomac_internalize_label,
2867 .mpo_internalize_vnode_label = mac_lomac_internalize_label,
2868 .mpo_create_devfs_device = mac_lomac_create_devfs_device,
2869 .mpo_create_devfs_directory = mac_lomac_create_devfs_directory,
2870 .mpo_create_devfs_symlink = mac_lomac_create_devfs_symlink,
2871 .mpo_create_mount = mac_lomac_create_mount,
2872 .mpo_relabel_vnode = mac_lomac_relabel_vnode,
2873 .mpo_update_devfs = mac_lomac_update_devfs,
2874 .mpo_associate_vnode_devfs = mac_lomac_associate_vnode_devfs,
2875 .mpo_associate_vnode_extattr = mac_lomac_associate_vnode_extattr,
2876 .mpo_associate_vnode_singlelabel =
2877 mac_lomac_associate_vnode_singlelabel,
2878 .mpo_create_vnode_extattr = mac_lomac_create_vnode_extattr,
2879 .mpo_setlabel_vnode_extattr = mac_lomac_setlabel_vnode_extattr,
2880 .mpo_create_mbuf_from_socket = mac_lomac_create_mbuf_from_socket,
2881 .mpo_create_mbuf_from_syncache = mac_lomac_create_mbuf_from_syncache,
2882 .mpo_create_pipe = mac_lomac_create_pipe,
2883 .mpo_create_socket = mac_lomac_create_socket,
2884 .mpo_create_socket_from_socket = mac_lomac_create_socket_from_socket,
2885 .mpo_relabel_pipe = mac_lomac_relabel_pipe,
2886 .mpo_relabel_socket = mac_lomac_relabel_socket,
2887 .mpo_set_socket_peer_from_mbuf = mac_lomac_set_socket_peer_from_mbuf,
2888 .mpo_set_socket_peer_from_socket =
2889 mac_lomac_set_socket_peer_from_socket,
2890 .mpo_create_bpfdesc = mac_lomac_create_bpfdesc,
2891 .mpo_create_datagram_from_ipq = mac_lomac_create_datagram_from_ipq,
2892 .mpo_create_fragment = mac_lomac_create_fragment,
2893 .mpo_create_ifnet = mac_lomac_create_ifnet,
2894 .mpo_create_inpcb_from_socket = mac_lomac_create_inpcb_from_socket,
2895 .mpo_create_ipq = mac_lomac_create_ipq,
2896 .mpo_create_mbuf_from_inpcb = mac_lomac_create_mbuf_from_inpcb,
2897 .mpo_create_mbuf_linklayer = mac_lomac_create_mbuf_linklayer,
2898 .mpo_create_mbuf_from_bpfdesc = mac_lomac_create_mbuf_from_bpfdesc,
2899 .mpo_create_mbuf_from_ifnet = mac_lomac_create_mbuf_from_ifnet,
2900 .mpo_create_mbuf_multicast_encap =
2901 mac_lomac_create_mbuf_multicast_encap,
2902 .mpo_create_mbuf_netlayer = mac_lomac_create_mbuf_netlayer,
2903 .mpo_fragment_match = mac_lomac_fragment_match,
2904 .mpo_relabel_ifnet = mac_lomac_relabel_ifnet,
2905 .mpo_update_ipq = mac_lomac_update_ipq,
2906 .mpo_inpcb_sosetlabel = mac_lomac_inpcb_sosetlabel,
2907 .mpo_execve_transition = mac_lomac_execve_transition,
2908 .mpo_execve_will_transition = mac_lomac_execve_will_transition,
2909 .mpo_create_proc0 = mac_lomac_create_proc0,
2910 .mpo_create_proc1 = mac_lomac_create_proc1,
2911 .mpo_relabel_cred = mac_lomac_relabel_cred,
2912 .mpo_check_bpfdesc_receive = mac_lomac_check_bpfdesc_receive,
2913 .mpo_check_cred_relabel = mac_lomac_check_cred_relabel,
2914 .mpo_check_cred_visible = mac_lomac_check_cred_visible,
2915 .mpo_check_ifnet_relabel = mac_lomac_check_ifnet_relabel,
2916 .mpo_check_ifnet_transmit = mac_lomac_check_ifnet_transmit,
2917 .mpo_check_inpcb_deliver = mac_lomac_check_inpcb_deliver,
2918 .mpo_check_kld_load = mac_lomac_check_kld_load,
2919 .mpo_check_pipe_ioctl = mac_lomac_check_pipe_ioctl,
2920 .mpo_check_pipe_read = mac_lomac_check_pipe_read,
2921 .mpo_check_pipe_relabel = mac_lomac_check_pipe_relabel,
2922 .mpo_check_pipe_write = mac_lomac_check_pipe_write,
2923 .mpo_check_proc_debug = mac_lomac_check_proc_debug,
2924 .mpo_check_proc_sched = mac_lomac_check_proc_sched,
2925 .mpo_check_proc_signal = mac_lomac_check_proc_signal,
2926 .mpo_check_socket_deliver = mac_lomac_check_socket_deliver,
2927 .mpo_check_socket_relabel = mac_lomac_check_socket_relabel,
2928 .mpo_check_socket_visible = mac_lomac_check_socket_visible,
2929 .mpo_check_system_acct = mac_lomac_check_system_acct,
2930 .mpo_check_system_auditctl = mac_lomac_check_system_auditctl,
2931 .mpo_check_system_swapoff = mac_lomac_check_system_swapoff,
2932 .mpo_check_system_swapon = mac_lomac_check_system_swapon,
2933 .mpo_check_system_sysctl = mac_lomac_check_system_sysctl,
2934 .mpo_check_vnode_access = mac_lomac_check_vnode_open,
2935 .mpo_check_vnode_create = mac_lomac_check_vnode_create,
2936 .mpo_check_vnode_delete = mac_lomac_check_vnode_delete,
2937 .mpo_check_vnode_deleteacl = mac_lomac_check_vnode_deleteacl,
2938 .mpo_check_vnode_link = mac_lomac_check_vnode_link,
2939 .mpo_check_vnode_mmap = mac_lomac_check_vnode_mmap,
2940 .mpo_check_vnode_mmap_downgrade = mac_lomac_check_vnode_mmap_downgrade,
2941 .mpo_check_vnode_open = mac_lomac_check_vnode_open,
2942 .mpo_check_vnode_read = mac_lomac_check_vnode_read,
2943 .mpo_check_vnode_relabel = mac_lomac_check_vnode_relabel,
2944 .mpo_check_vnode_rename_from = mac_lomac_check_vnode_rename_from,
2945 .mpo_check_vnode_rename_to = mac_lomac_check_vnode_rename_to,
2946 .mpo_check_vnode_revoke = mac_lomac_check_vnode_revoke,
2947 .mpo_check_vnode_setacl = mac_lomac_check_vnode_setacl,
2948 .mpo_check_vnode_setextattr = mac_lomac_check_vnode_setextattr,
2949 .mpo_check_vnode_setflags = mac_lomac_check_vnode_setflags,
2950 .mpo_check_vnode_setmode = mac_lomac_check_vnode_setmode,
2951 .mpo_check_vnode_setowner = mac_lomac_check_vnode_setowner,
2952 .mpo_check_vnode_setutimes = mac_lomac_check_vnode_setutimes,
2953 .mpo_check_vnode_write = mac_lomac_check_vnode_write,
2954 .mpo_thread_userret = mac_lomac_thread_userret,
2955 .mpo_create_mbuf_from_firewall = mac_lomac_create_mbuf_from_firewall,
2956 .mpo_priv_check = mac_lomac_priv_check,
2959 MAC_POLICY_SET(&mac_lomac_ops, mac_lomac, "TrustedBSD MAC/LOMAC",
2960 MPC_LOADTIME_FLAG_NOTLATE | MPC_LOADTIME_FLAG_LABELMBUFS,