mac_biba.c revision 173018
1/*-
2 * Copyright (c) 1999-2002, 2007 Robert N. M. Watson
3 * Copyright (c) 2001-2005 McAfee, 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 McAfee
10 * Research, the Security Research Division of McAfee, 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 * $FreeBSD: head/sys/security/mac_biba/mac_biba.c 173018 2007-10-26 13:18:38Z rwatson $
39 */
40
41/*
42 * Developed by the TrustedBSD Project.
43 *
44 * Biba fixed label mandatory integrity policy.
45 */
46
47#include <sys/param.h>
48#include <sys/conf.h>
49#include <sys/extattr.h>
50#include <sys/kernel.h>
51#include <sys/ksem.h>
52#include <sys/malloc.h>
53#include <sys/mman.h>
54#include <sys/mount.h>
55#include <sys/priv.h>
56#include <sys/proc.h>
57#include <sys/sbuf.h>
58#include <sys/systm.h>
59#include <sys/sysproto.h>
60#include <sys/sysent.h>
61#include <sys/systm.h>
62#include <sys/vnode.h>
63#include <sys/file.h>
64#include <sys/socket.h>
65#include <sys/socketvar.h>
66#include <sys/pipe.h>
67#include <sys/sx.h>
68#include <sys/sysctl.h>
69#include <sys/msg.h>
70#include <sys/sem.h>
71#include <sys/shm.h>
72
73#include <fs/devfs/devfs.h>
74
75#include <net/bpfdesc.h>
76#include <net/if.h>
77#include <net/if_types.h>
78#include <net/if_var.h>
79
80#include <netinet/in.h>
81#include <netinet/in_pcb.h>
82#include <netinet/ip_var.h>
83
84#include <vm/uma.h>
85#include <vm/vm.h>
86
87#include <security/mac/mac_policy.h>
88#include <security/mac_biba/mac_biba.h>
89
90SYSCTL_DECL(_security_mac);
91
92SYSCTL_NODE(_security_mac, OID_AUTO, biba, CTLFLAG_RW, 0,
93    "TrustedBSD mac_biba policy controls");
94
95static int	biba_label_size = sizeof(struct mac_biba);
96SYSCTL_INT(_security_mac_biba, OID_AUTO, label_size, CTLFLAG_RD,
97    &biba_label_size, 0, "Size of struct mac_biba");
98
99static int	biba_enabled = 1;
100SYSCTL_INT(_security_mac_biba, OID_AUTO, enabled, CTLFLAG_RW, &biba_enabled,
101    0, "Enforce MAC/Biba policy");
102TUNABLE_INT("security.mac.biba.enabled", &biba_enabled);
103
104static int	destroyed_not_inited;
105SYSCTL_INT(_security_mac_biba, OID_AUTO, destroyed_not_inited, CTLFLAG_RD,
106    &destroyed_not_inited, 0, "Count of labels destroyed but not inited");
107
108static int	trust_all_interfaces = 0;
109SYSCTL_INT(_security_mac_biba, OID_AUTO, trust_all_interfaces, CTLFLAG_RD,
110    &trust_all_interfaces, 0, "Consider all interfaces 'trusted' by MAC/Biba");
111TUNABLE_INT("security.mac.biba.trust_all_interfaces", &trust_all_interfaces);
112
113static char	trusted_interfaces[128];
114SYSCTL_STRING(_security_mac_biba, OID_AUTO, trusted_interfaces, CTLFLAG_RD,
115    trusted_interfaces, 0, "Interfaces considered 'trusted' by MAC/Biba");
116TUNABLE_STR("security.mac.biba.trusted_interfaces", trusted_interfaces,
117    sizeof(trusted_interfaces));
118
119static int	max_compartments = MAC_BIBA_MAX_COMPARTMENTS;
120SYSCTL_INT(_security_mac_biba, OID_AUTO, max_compartments, CTLFLAG_RD,
121    &max_compartments, 0, "Maximum supported compartments");
122
123static int	ptys_equal = 0;
124SYSCTL_INT(_security_mac_biba, OID_AUTO, ptys_equal, CTLFLAG_RW,
125    &ptys_equal, 0, "Label pty devices as biba/equal on create");
126TUNABLE_INT("security.mac.biba.ptys_equal", &ptys_equal);
127
128static int	interfaces_equal;
129SYSCTL_INT(_security_mac_biba, OID_AUTO, interfaces_equal, CTLFLAG_RW,
130    &interfaces_equal, 0, "Label network interfaces as biba/equal on create");
131TUNABLE_INT("security.mac.biba.interfaces_equal", &interfaces_equal);
132
133static int	revocation_enabled = 0;
134SYSCTL_INT(_security_mac_biba, OID_AUTO, revocation_enabled, CTLFLAG_RW,
135    &revocation_enabled, 0, "Revoke access to objects on relabel");
136TUNABLE_INT("security.mac.biba.revocation_enabled", &revocation_enabled);
137
138static int	biba_slot;
139#define	SLOT(l)	((struct mac_biba *)mac_label_get((l), biba_slot))
140#define	SLOT_SET(l, val) mac_label_set((l), biba_slot, (uintptr_t)(val))
141
142static uma_zone_t	zone_biba;
143
144static __inline int
145biba_bit_set_empty(u_char *set) {
146	int i;
147
148	for (i = 0; i < MAC_BIBA_MAX_COMPARTMENTS >> 3; i++)
149		if (set[i] != 0)
150			return (0);
151	return (1);
152}
153
154static struct mac_biba *
155biba_alloc(int flag)
156{
157
158	return (uma_zalloc(zone_biba, flag | M_ZERO));
159}
160
161static void
162biba_free(struct mac_biba *mb)
163{
164
165	if (mb != NULL)
166		uma_zfree(zone_biba, mb);
167	else
168		atomic_add_int(&destroyed_not_inited, 1);
169}
170
171static int
172biba_atmostflags(struct mac_biba *mb, int flags)
173{
174
175	if ((mb->mb_flags & flags) != mb->mb_flags)
176		return (EINVAL);
177	return (0);
178}
179
180static int
181biba_dominate_element(struct mac_biba_element *a, struct mac_biba_element *b)
182{
183	int bit;
184
185	switch (a->mbe_type) {
186	case MAC_BIBA_TYPE_EQUAL:
187	case MAC_BIBA_TYPE_HIGH:
188		return (1);
189
190	case MAC_BIBA_TYPE_LOW:
191		switch (b->mbe_type) {
192		case MAC_BIBA_TYPE_GRADE:
193		case MAC_BIBA_TYPE_HIGH:
194			return (0);
195
196		case MAC_BIBA_TYPE_EQUAL:
197		case MAC_BIBA_TYPE_LOW:
198			return (1);
199
200		default:
201			panic("biba_dominate_element: b->mbe_type invalid");
202		}
203
204	case MAC_BIBA_TYPE_GRADE:
205		switch (b->mbe_type) {
206		case MAC_BIBA_TYPE_EQUAL:
207		case MAC_BIBA_TYPE_LOW:
208			return (1);
209
210		case MAC_BIBA_TYPE_HIGH:
211			return (0);
212
213		case MAC_BIBA_TYPE_GRADE:
214			for (bit = 1; bit <= MAC_BIBA_MAX_COMPARTMENTS; bit++)
215				if (!MAC_BIBA_BIT_TEST(bit,
216				    a->mbe_compartments) &&
217				    MAC_BIBA_BIT_TEST(bit, b->mbe_compartments))
218					return (0);
219			return (a->mbe_grade >= b->mbe_grade);
220
221		default:
222			panic("biba_dominate_element: b->mbe_type invalid");
223		}
224
225	default:
226		panic("biba_dominate_element: a->mbe_type invalid");
227	}
228
229	return (0);
230}
231
232static int
233biba_subject_dominate_high(struct mac_biba *mb)
234{
235	struct mac_biba_element *element;
236
237	KASSERT((mb->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) != 0,
238	    ("biba_effective_in_range: mb not effective"));
239	element = &mb->mb_effective;
240
241	return (element->mbe_type == MAC_BIBA_TYPE_EQUAL ||
242	    element->mbe_type == MAC_BIBA_TYPE_HIGH);
243}
244
245static int
246biba_range_in_range(struct mac_biba *rangea, struct mac_biba *rangeb)
247{
248
249	return (biba_dominate_element(&rangeb->mb_rangehigh,
250	    &rangea->mb_rangehigh) &&
251	    biba_dominate_element(&rangea->mb_rangelow,
252	    &rangeb->mb_rangelow));
253}
254
255static int
256biba_effective_in_range(struct mac_biba *effective, struct mac_biba *range)
257{
258
259	KASSERT((effective->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) != 0,
260	    ("biba_effective_in_range: a not effective"));
261	KASSERT((range->mb_flags & MAC_BIBA_FLAG_RANGE) != 0,
262	    ("biba_effective_in_range: b not range"));
263
264	return (biba_dominate_element(&range->mb_rangehigh,
265	    &effective->mb_effective) &&
266	    biba_dominate_element(&effective->mb_effective,
267	    &range->mb_rangelow));
268
269	return (1);
270}
271
272static int
273biba_dominate_effective(struct mac_biba *a, struct mac_biba *b)
274{
275	KASSERT((a->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) != 0,
276	    ("biba_dominate_effective: a not effective"));
277	KASSERT((b->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) != 0,
278	    ("biba_dominate_effective: b not effective"));
279
280	return (biba_dominate_element(&a->mb_effective, &b->mb_effective));
281}
282
283static int
284biba_equal_element(struct mac_biba_element *a, struct mac_biba_element *b)
285{
286
287	if (a->mbe_type == MAC_BIBA_TYPE_EQUAL ||
288	    b->mbe_type == MAC_BIBA_TYPE_EQUAL)
289		return (1);
290
291	return (a->mbe_type == b->mbe_type && a->mbe_grade == b->mbe_grade);
292}
293
294static int
295biba_equal_effective(struct mac_biba *a, struct mac_biba *b)
296{
297
298	KASSERT((a->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) != 0,
299	    ("biba_equal_effective: a not effective"));
300	KASSERT((b->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) != 0,
301	    ("biba_equal_effective: b not effective"));
302
303	return (biba_equal_element(&a->mb_effective, &b->mb_effective));
304}
305
306static int
307biba_contains_equal(struct mac_biba *mb)
308{
309
310	if (mb->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) {
311		if (mb->mb_effective.mbe_type == MAC_BIBA_TYPE_EQUAL)
312			return (1);
313	}
314
315	if (mb->mb_flags & MAC_BIBA_FLAG_RANGE) {
316		if (mb->mb_rangelow.mbe_type == MAC_BIBA_TYPE_EQUAL)
317			return (1);
318		if (mb->mb_rangehigh.mbe_type == MAC_BIBA_TYPE_EQUAL)
319			return (1);
320	}
321
322	return (0);
323}
324
325static int
326biba_subject_privileged(struct mac_biba *mb)
327{
328
329	KASSERT((mb->mb_flags & MAC_BIBA_FLAGS_BOTH) == MAC_BIBA_FLAGS_BOTH,
330	    ("biba_subject_privileged: subject doesn't have both labels"));
331
332	/* If the effective is EQUAL, it's ok. */
333	if (mb->mb_effective.mbe_type == MAC_BIBA_TYPE_EQUAL)
334		return (0);
335
336	/* If either range endpoint is EQUAL, it's ok. */
337	if (mb->mb_rangelow.mbe_type == MAC_BIBA_TYPE_EQUAL ||
338	    mb->mb_rangehigh.mbe_type == MAC_BIBA_TYPE_EQUAL)
339		return (0);
340
341	/* If the range is low-high, it's ok. */
342	if (mb->mb_rangelow.mbe_type == MAC_BIBA_TYPE_LOW &&
343	    mb->mb_rangehigh.mbe_type == MAC_BIBA_TYPE_HIGH)
344		return (0);
345
346	/* It's not ok. */
347	return (EPERM);
348}
349
350static int
351biba_high_effective(struct mac_biba *mb)
352{
353
354	KASSERT((mb->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) != 0,
355	    ("biba_equal_effective: mb not effective"));
356
357	return (mb->mb_effective.mbe_type == MAC_BIBA_TYPE_HIGH);
358}
359
360static int
361biba_valid(struct mac_biba *mb)
362{
363
364	if (mb->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) {
365		switch (mb->mb_effective.mbe_type) {
366		case MAC_BIBA_TYPE_GRADE:
367			break;
368
369		case MAC_BIBA_TYPE_EQUAL:
370		case MAC_BIBA_TYPE_HIGH:
371		case MAC_BIBA_TYPE_LOW:
372			if (mb->mb_effective.mbe_grade != 0 ||
373			    !MAC_BIBA_BIT_SET_EMPTY(
374			    mb->mb_effective.mbe_compartments))
375				return (EINVAL);
376			break;
377
378		default:
379			return (EINVAL);
380		}
381	} else {
382		if (mb->mb_effective.mbe_type != MAC_BIBA_TYPE_UNDEF)
383			return (EINVAL);
384	}
385
386	if (mb->mb_flags & MAC_BIBA_FLAG_RANGE) {
387		switch (mb->mb_rangelow.mbe_type) {
388		case MAC_BIBA_TYPE_GRADE:
389			break;
390
391		case MAC_BIBA_TYPE_EQUAL:
392		case MAC_BIBA_TYPE_HIGH:
393		case MAC_BIBA_TYPE_LOW:
394			if (mb->mb_rangelow.mbe_grade != 0 ||
395			    !MAC_BIBA_BIT_SET_EMPTY(
396			    mb->mb_rangelow.mbe_compartments))
397				return (EINVAL);
398			break;
399
400		default:
401			return (EINVAL);
402		}
403
404		switch (mb->mb_rangehigh.mbe_type) {
405		case MAC_BIBA_TYPE_GRADE:
406			break;
407
408		case MAC_BIBA_TYPE_EQUAL:
409		case MAC_BIBA_TYPE_HIGH:
410		case MAC_BIBA_TYPE_LOW:
411			if (mb->mb_rangehigh.mbe_grade != 0 ||
412			    !MAC_BIBA_BIT_SET_EMPTY(
413			    mb->mb_rangehigh.mbe_compartments))
414				return (EINVAL);
415			break;
416
417		default:
418			return (EINVAL);
419		}
420		if (!biba_dominate_element(&mb->mb_rangehigh,
421		    &mb->mb_rangelow))
422			return (EINVAL);
423	} else {
424		if (mb->mb_rangelow.mbe_type != MAC_BIBA_TYPE_UNDEF ||
425		    mb->mb_rangehigh.mbe_type != MAC_BIBA_TYPE_UNDEF)
426			return (EINVAL);
427	}
428
429	return (0);
430}
431
432static void
433biba_set_range(struct mac_biba *mb, u_short typelow, u_short gradelow,
434    u_char *compartmentslow, u_short typehigh, u_short gradehigh,
435    u_char *compartmentshigh)
436{
437
438	mb->mb_rangelow.mbe_type = typelow;
439	mb->mb_rangelow.mbe_grade = gradelow;
440	if (compartmentslow != NULL)
441		memcpy(mb->mb_rangelow.mbe_compartments, compartmentslow,
442		    sizeof(mb->mb_rangelow.mbe_compartments));
443	mb->mb_rangehigh.mbe_type = typehigh;
444	mb->mb_rangehigh.mbe_grade = gradehigh;
445	if (compartmentshigh != NULL)
446		memcpy(mb->mb_rangehigh.mbe_compartments, compartmentshigh,
447		    sizeof(mb->mb_rangehigh.mbe_compartments));
448	mb->mb_flags |= MAC_BIBA_FLAG_RANGE;
449}
450
451static void
452biba_set_effective(struct mac_biba *mb, u_short type, u_short grade,
453    u_char *compartments)
454{
455
456	mb->mb_effective.mbe_type = type;
457	mb->mb_effective.mbe_grade = grade;
458	if (compartments != NULL)
459		memcpy(mb->mb_effective.mbe_compartments, compartments,
460		    sizeof(mb->mb_effective.mbe_compartments));
461	mb->mb_flags |= MAC_BIBA_FLAG_EFFECTIVE;
462}
463
464static void
465biba_copy_range(struct mac_biba *labelfrom, struct mac_biba *labelto)
466{
467
468	KASSERT((labelfrom->mb_flags & MAC_BIBA_FLAG_RANGE) != 0,
469	    ("biba_copy_range: labelfrom not range"));
470
471	labelto->mb_rangelow = labelfrom->mb_rangelow;
472	labelto->mb_rangehigh = labelfrom->mb_rangehigh;
473	labelto->mb_flags |= MAC_BIBA_FLAG_RANGE;
474}
475
476static void
477biba_copy_effective(struct mac_biba *labelfrom, struct mac_biba *labelto)
478{
479
480	KASSERT((labelfrom->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) != 0,
481	    ("biba_copy_effective: labelfrom not effective"));
482
483	labelto->mb_effective = labelfrom->mb_effective;
484	labelto->mb_flags |= MAC_BIBA_FLAG_EFFECTIVE;
485}
486
487static void
488biba_copy(struct mac_biba *source, struct mac_biba *dest)
489{
490
491	if (source->mb_flags & MAC_BIBA_FLAG_EFFECTIVE)
492		biba_copy_effective(source, dest);
493	if (source->mb_flags & MAC_BIBA_FLAG_RANGE)
494		biba_copy_range(source, dest);
495}
496
497/*
498 * Policy module operations.
499 */
500static void
501biba_init(struct mac_policy_conf *conf)
502{
503
504	zone_biba = uma_zcreate("mac_biba", sizeof(struct mac_biba), NULL,
505	    NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
506}
507
508/*
509 * Label operations.
510 */
511static void
512biba_init_label(struct label *label)
513{
514
515	SLOT_SET(label, biba_alloc(M_WAITOK));
516}
517
518static int
519biba_init_label_waitcheck(struct label *label, int flag)
520{
521
522	SLOT_SET(label, biba_alloc(flag));
523	if (SLOT(label) == NULL)
524		return (ENOMEM);
525
526	return (0);
527}
528
529static void
530biba_destroy_label(struct label *label)
531{
532
533	biba_free(SLOT(label));
534	SLOT_SET(label, NULL);
535}
536
537/*
538 * biba_element_to_string() accepts an sbuf and Biba element.  It converts
539 * the Biba element to a string and stores the result in the sbuf; if there
540 * isn't space in the sbuf, -1 is returned.
541 */
542static int
543biba_element_to_string(struct sbuf *sb, struct mac_biba_element *element)
544{
545	int i, first;
546
547	switch (element->mbe_type) {
548	case MAC_BIBA_TYPE_HIGH:
549		return (sbuf_printf(sb, "high"));
550
551	case MAC_BIBA_TYPE_LOW:
552		return (sbuf_printf(sb, "low"));
553
554	case MAC_BIBA_TYPE_EQUAL:
555		return (sbuf_printf(sb, "equal"));
556
557	case MAC_BIBA_TYPE_GRADE:
558		if (sbuf_printf(sb, "%d", element->mbe_grade) == -1)
559			return (-1);
560
561		first = 1;
562		for (i = 1; i <= MAC_BIBA_MAX_COMPARTMENTS; i++) {
563			if (MAC_BIBA_BIT_TEST(i, element->mbe_compartments)) {
564				if (first) {
565					if (sbuf_putc(sb, ':') == -1)
566						return (-1);
567					if (sbuf_printf(sb, "%d", i) == -1)
568						return (-1);
569					first = 0;
570				} else {
571					if (sbuf_printf(sb, "+%d", i) == -1)
572						return (-1);
573				}
574			}
575		}
576		return (0);
577
578	default:
579		panic("biba_element_to_string: invalid type (%d)",
580		    element->mbe_type);
581	}
582}
583
584/*
585 * biba_to_string() converts a Biba label to a string, and places the results
586 * in the passed sbuf.  It returns 0 on success, or EINVAL if there isn't
587 * room in the sbuf.  Note: the sbuf will be modified even in a failure case,
588 * so the caller may need to revert the sbuf by restoring the offset if
589 * that's undesired.
590 */
591static int
592biba_to_string(struct sbuf *sb, struct mac_biba *mb)
593{
594
595	if (mb->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) {
596		if (biba_element_to_string(sb, &mb->mb_effective) == -1)
597			return (EINVAL);
598	}
599
600	if (mb->mb_flags & MAC_BIBA_FLAG_RANGE) {
601		if (sbuf_putc(sb, '(') == -1)
602			return (EINVAL);
603
604		if (biba_element_to_string(sb, &mb->mb_rangelow) == -1)
605			return (EINVAL);
606
607		if (sbuf_putc(sb, '-') == -1)
608			return (EINVAL);
609
610		if (biba_element_to_string(sb, &mb->mb_rangehigh) == -1)
611			return (EINVAL);
612
613		if (sbuf_putc(sb, ')') == -1)
614			return (EINVAL);
615	}
616
617	return (0);
618}
619
620static int
621biba_externalize_label(struct label *label, char *element_name,
622    struct sbuf *sb, int *claimed)
623{
624	struct mac_biba *mb;
625
626	if (strcmp(MAC_BIBA_LABEL_NAME, element_name) != 0)
627		return (0);
628
629	(*claimed)++;
630
631	mb = SLOT(label);
632	return (biba_to_string(sb, mb));
633}
634
635static int
636biba_parse_element(struct mac_biba_element *element, char *string)
637{
638	char *compartment, *end, *grade;
639	int value;
640
641	if (strcmp(string, "high") == 0 ||
642	    strcmp(string, "hi") == 0) {
643		element->mbe_type = MAC_BIBA_TYPE_HIGH;
644		element->mbe_grade = MAC_BIBA_TYPE_UNDEF;
645	} else if (strcmp(string, "low") == 0 ||
646	    strcmp(string, "lo") == 0) {
647		element->mbe_type = MAC_BIBA_TYPE_LOW;
648		element->mbe_grade = MAC_BIBA_TYPE_UNDEF;
649	} else if (strcmp(string, "equal") == 0 ||
650	    strcmp(string, "eq") == 0) {
651		element->mbe_type = MAC_BIBA_TYPE_EQUAL;
652		element->mbe_grade = MAC_BIBA_TYPE_UNDEF;
653	} else {
654		element->mbe_type = MAC_BIBA_TYPE_GRADE;
655
656		/*
657		 * Numeric grade piece of the element.
658		 */
659		grade = strsep(&string, ":");
660		value = strtol(grade, &end, 10);
661		if (end == grade || *end != '\0')
662			return (EINVAL);
663		if (value < 0 || value > 65535)
664			return (EINVAL);
665		element->mbe_grade = value;
666
667		/*
668		 * Optional compartment piece of the element.  If none
669		 * are included, we assume that the label has no
670		 * compartments.
671		 */
672		if (string == NULL)
673			return (0);
674		if (*string == '\0')
675			return (0);
676
677		while ((compartment = strsep(&string, "+")) != NULL) {
678			value = strtol(compartment, &end, 10);
679			if (compartment == end || *end != '\0')
680				return (EINVAL);
681			if (value < 1 || value > MAC_BIBA_MAX_COMPARTMENTS)
682				return (EINVAL);
683			MAC_BIBA_BIT_SET(value, element->mbe_compartments);
684		}
685	}
686
687	return (0);
688}
689
690/*
691 * Note: destructively consumes the string, make a local copy before
692 * calling if that's a problem.
693 */
694static int
695biba_parse(struct mac_biba *mb, char *string)
696{
697	char *rangehigh, *rangelow, *effective;
698	int error;
699
700	effective = strsep(&string, "(");
701	if (*effective == '\0')
702		effective = NULL;
703
704	if (string != NULL) {
705		rangelow = strsep(&string, "-");
706		if (string == NULL)
707			return (EINVAL);
708		rangehigh = strsep(&string, ")");
709		if (string == NULL)
710			return (EINVAL);
711		if (*string != '\0')
712			return (EINVAL);
713	} else {
714		rangelow = NULL;
715		rangehigh = NULL;
716	}
717
718	KASSERT((rangelow != NULL && rangehigh != NULL) ||
719	    (rangelow == NULL && rangehigh == NULL),
720	    ("biba_parse: range mismatch"));
721
722	bzero(mb, sizeof(*mb));
723	if (effective != NULL) {
724		error = biba_parse_element(&mb->mb_effective, effective);
725		if (error)
726			return (error);
727		mb->mb_flags |= MAC_BIBA_FLAG_EFFECTIVE;
728	}
729
730	if (rangelow != NULL) {
731		error = biba_parse_element(&mb->mb_rangelow, rangelow);
732		if (error)
733			return (error);
734		error = biba_parse_element(&mb->mb_rangehigh, rangehigh);
735		if (error)
736			return (error);
737		mb->mb_flags |= MAC_BIBA_FLAG_RANGE;
738	}
739
740	error = biba_valid(mb);
741	if (error)
742		return (error);
743
744	return (0);
745}
746
747static int
748biba_internalize_label(struct label *label, char *element_name,
749    char *element_data, int *claimed)
750{
751	struct mac_biba *mb, mb_temp;
752	int error;
753
754	if (strcmp(MAC_BIBA_LABEL_NAME, element_name) != 0)
755		return (0);
756
757	(*claimed)++;
758
759	error = biba_parse(&mb_temp, element_data);
760	if (error)
761		return (error);
762
763	mb = SLOT(label);
764	*mb = mb_temp;
765
766	return (0);
767}
768
769static void
770biba_copy_label(struct label *src, struct label *dest)
771{
772
773	*SLOT(dest) = *SLOT(src);
774}
775
776/*
777 * Labeling event operations: file system objects, and things that look a lot
778 * like file system objects.
779 */
780static void
781biba_devfs_create_device(struct ucred *cred, struct mount *mp,
782    struct cdev *dev, struct devfs_dirent *de, struct label *delabel)
783{
784	struct mac_biba *mb;
785	int biba_type;
786
787	mb = SLOT(delabel);
788	if (strcmp(dev->si_name, "null") == 0 ||
789	    strcmp(dev->si_name, "zero") == 0 ||
790	    strcmp(dev->si_name, "random") == 0 ||
791	    strncmp(dev->si_name, "fd/", strlen("fd/")) == 0)
792		biba_type = MAC_BIBA_TYPE_EQUAL;
793	else if (ptys_equal &&
794	    (strncmp(dev->si_name, "ttyp", strlen("ttyp")) == 0 ||
795	    strncmp(dev->si_name, "ptyp", strlen("ptyp")) == 0))
796		biba_type = MAC_BIBA_TYPE_EQUAL;
797	else
798		biba_type = MAC_BIBA_TYPE_HIGH;
799	biba_set_effective(mb, biba_type, 0, NULL);
800}
801
802static void
803biba_devfs_create_directory(struct mount *mp, char *dirname, int dirnamelen,
804    struct devfs_dirent *de, struct label *delabel)
805{
806	struct mac_biba *mb;
807
808	mb = SLOT(delabel);
809
810	biba_set_effective(mb, MAC_BIBA_TYPE_HIGH, 0, NULL);
811}
812
813static void
814biba_devfs_create_symlink(struct ucred *cred, struct mount *mp,
815    struct devfs_dirent *dd, struct label *ddlabel, struct devfs_dirent *de,
816    struct label *delabel)
817{
818	struct mac_biba *source, *dest;
819
820	source = SLOT(cred->cr_label);
821	dest = SLOT(delabel);
822
823	biba_copy_effective(source, dest);
824}
825
826static void
827biba_mount_create(struct ucred *cred, struct mount *mp,
828    struct label *mplabel)
829{
830	struct mac_biba *source, *dest;
831
832	source = SLOT(cred->cr_label);
833	dest = SLOT(mplabel);
834
835	biba_copy_effective(source, dest);
836}
837
838static void
839biba_vnode_relabel(struct ucred *cred, struct vnode *vp,
840    struct label *vplabel, struct label *newlabel)
841{
842	struct mac_biba *source, *dest;
843
844	source = SLOT(newlabel);
845	dest = SLOT(vplabel);
846
847	biba_copy(source, dest);
848}
849
850static void
851biba_devfs_update(struct mount *mp, struct devfs_dirent *de,
852    struct label *delabel, struct vnode *vp, struct label *vplabel)
853{
854	struct mac_biba *source, *dest;
855
856	source = SLOT(vplabel);
857	dest = SLOT(delabel);
858
859	biba_copy(source, dest);
860}
861
862static void
863biba_devfs_vnode_associate(struct mount *mp, struct label *mntlabel,
864    struct devfs_dirent *de, struct label *delabel, struct vnode *vp,
865    struct label *vplabel)
866{
867	struct mac_biba *source, *dest;
868
869	source = SLOT(delabel);
870	dest = SLOT(vplabel);
871
872	biba_copy_effective(source, dest);
873}
874
875static int
876biba_vnode_associate_extattr(struct mount *mp, struct label *mplabel,
877    struct vnode *vp, struct label *vplabel)
878{
879	struct mac_biba mb_temp, *source, *dest;
880	int buflen, error;
881
882	source = SLOT(mplabel);
883	dest = SLOT(vplabel);
884
885	buflen = sizeof(mb_temp);
886	bzero(&mb_temp, buflen);
887
888	error = vn_extattr_get(vp, IO_NODELOCKED, MAC_BIBA_EXTATTR_NAMESPACE,
889	    MAC_BIBA_EXTATTR_NAME, &buflen, (char *) &mb_temp, curthread);
890	if (error == ENOATTR || error == EOPNOTSUPP) {
891		/* Fall back to the mntlabel. */
892		biba_copy_effective(source, dest);
893		return (0);
894	} else if (error)
895		return (error);
896
897	if (buflen != sizeof(mb_temp)) {
898		printf("biba_vnode_associate_extattr: bad size %d\n",
899		    buflen);
900		return (EPERM);
901	}
902	if (biba_valid(&mb_temp) != 0) {
903		printf("biba_vnode_associate_extattr: invalid\n");
904		return (EPERM);
905	}
906	if ((mb_temp.mb_flags & MAC_BIBA_FLAGS_BOTH) !=
907	    MAC_BIBA_FLAG_EFFECTIVE) {
908		printf("biba_vnode_associate_extattr: not effective\n");
909		return (EPERM);
910	}
911
912	biba_copy_effective(&mb_temp, dest);
913	return (0);
914}
915
916static void
917biba_vnode_associate_singlelabel(struct mount *mp, struct label *mplabel,
918    struct vnode *vp, struct label *vplabel)
919{
920	struct mac_biba *source, *dest;
921
922	source = SLOT(mplabel);
923	dest = SLOT(vplabel);
924
925	biba_copy_effective(source, dest);
926}
927
928static int
929biba_vnode_create_extattr(struct ucred *cred, struct mount *mp,
930    struct label *mplabel, struct vnode *dvp, struct label *dvplabel,
931    struct vnode *vp, struct label *vplabel, struct componentname *cnp)
932{
933	struct mac_biba *source, *dest, mb_temp;
934	size_t buflen;
935	int error;
936
937	buflen = sizeof(mb_temp);
938	bzero(&mb_temp, buflen);
939
940	source = SLOT(cred->cr_label);
941	dest = SLOT(vplabel);
942	biba_copy_effective(source, &mb_temp);
943
944	error = vn_extattr_set(vp, IO_NODELOCKED, MAC_BIBA_EXTATTR_NAMESPACE,
945	    MAC_BIBA_EXTATTR_NAME, buflen, (char *) &mb_temp, curthread);
946	if (error == 0)
947		biba_copy_effective(source, dest);
948	return (error);
949}
950
951static int
952biba_vnode_setlabel_extattr(struct ucred *cred, struct vnode *vp,
953    struct label *vplabel, struct label *intlabel)
954{
955	struct mac_biba *source, mb_temp;
956	size_t buflen;
957	int error;
958
959	buflen = sizeof(mb_temp);
960	bzero(&mb_temp, buflen);
961
962	source = SLOT(intlabel);
963	if ((source->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) == 0)
964		return (0);
965
966	biba_copy_effective(source, &mb_temp);
967
968	error = vn_extattr_set(vp, IO_NODELOCKED, MAC_BIBA_EXTATTR_NAMESPACE,
969	    MAC_BIBA_EXTATTR_NAME, buflen, (char *) &mb_temp, curthread);
970	return (error);
971}
972
973/*
974 * Labeling event operations: IPC object.
975 */
976static void
977biba_inpcb_create(struct socket *so, struct label *solabel,
978    struct inpcb *inp, struct label *inplabel)
979{
980	struct mac_biba *source, *dest;
981
982	source = SLOT(solabel);
983	dest = SLOT(inplabel);
984
985	biba_copy_effective(source, dest);
986}
987
988static void
989biba_socket_create_mbuf(struct socket *so, struct label *solabel,
990    struct mbuf *m, struct label *mlabel)
991{
992	struct mac_biba *source, *dest;
993
994	source = SLOT(solabel);
995	dest = SLOT(mlabel);
996
997	biba_copy_effective(source, dest);
998}
999
1000static void
1001biba_socket_create(struct ucred *cred, struct socket *so,
1002    struct label *solabel)
1003{
1004	struct mac_biba *source, *dest;
1005
1006	source = SLOT(cred->cr_label);
1007	dest = SLOT(solabel);
1008
1009	biba_copy_effective(source, dest);
1010}
1011
1012static void
1013biba_pipe_create(struct ucred *cred, struct pipepair *pp,
1014    struct label *pplabel)
1015{
1016	struct mac_biba *source, *dest;
1017
1018	source = SLOT(cred->cr_label);
1019	dest = SLOT(pplabel);
1020
1021	biba_copy_effective(source, dest);
1022}
1023
1024static void
1025biba_posixsem_create(struct ucred *cred, struct ksem *ks,
1026    struct label *kslabel)
1027{
1028	struct mac_biba *source, *dest;
1029
1030	source = SLOT(cred->cr_label);
1031	dest = SLOT(kslabel);
1032
1033	biba_copy_effective(source, dest);
1034}
1035
1036static void
1037biba_socket_newconn(struct socket *oldso, struct label *oldsolabel,
1038    struct socket *newso, struct label *newsolabel)
1039{
1040	struct mac_biba *source, *dest;
1041
1042	source = SLOT(oldsolabel);
1043	dest = SLOT(newsolabel);
1044
1045	biba_copy_effective(source, dest);
1046}
1047
1048static void
1049biba_socket_relabel(struct ucred *cred, struct socket *so,
1050    struct label *solabel, struct label *newlabel)
1051{
1052	struct mac_biba *source, *dest;
1053
1054	source = SLOT(newlabel);
1055	dest = SLOT(solabel);
1056
1057	biba_copy(source, dest);
1058}
1059
1060static void
1061biba_pipe_relabel(struct ucred *cred, struct pipepair *pp,
1062    struct label *pplabel, struct label *newlabel)
1063{
1064	struct mac_biba *source, *dest;
1065
1066	source = SLOT(newlabel);
1067	dest = SLOT(pplabel);
1068
1069	biba_copy(source, dest);
1070}
1071
1072static void
1073biba_socketpeer_set_from_mbuf(struct mbuf *m, struct label *mlabel,
1074    struct socket *so, struct label *sopeerlabel)
1075{
1076	struct mac_biba *source, *dest;
1077
1078	source = SLOT(mlabel);
1079	dest = SLOT(sopeerlabel);
1080
1081	biba_copy_effective(source, dest);
1082}
1083
1084/*
1085 * Labeling event operations: System V IPC objects.
1086 */
1087static void
1088biba_sysvmsg_create(struct ucred *cred, struct msqid_kernel *msqkptr,
1089    struct label *msqlabel, struct msg *msgptr, struct label *msglabel)
1090{
1091	struct mac_biba *source, *dest;
1092
1093	/* Ignore the msgq label */
1094	source = SLOT(cred->cr_label);
1095	dest = SLOT(msglabel);
1096
1097	biba_copy_effective(source, dest);
1098}
1099
1100static void
1101biba_sysvmsq_create(struct ucred *cred, struct msqid_kernel *msqkptr,
1102    struct label *msqlabel)
1103{
1104	struct mac_biba *source, *dest;
1105
1106	source = SLOT(cred->cr_label);
1107	dest = SLOT(msqlabel);
1108
1109	biba_copy_effective(source, dest);
1110}
1111
1112static void
1113biba_sysvsem_create(struct ucred *cred, struct semid_kernel *semakptr,
1114    struct label *semalabel)
1115{
1116	struct mac_biba *source, *dest;
1117
1118	source = SLOT(cred->cr_label);
1119	dest = SLOT(semalabel);
1120
1121	biba_copy_effective(source, dest);
1122}
1123
1124static void
1125biba_sysvshm_create(struct ucred *cred, struct shmid_kernel *shmsegptr,
1126    struct label *shmlabel)
1127{
1128	struct mac_biba *source, *dest;
1129
1130	source = SLOT(cred->cr_label);
1131	dest = SLOT(shmlabel);
1132
1133	biba_copy_effective(source, dest);
1134}
1135
1136/*
1137 * Labeling event operations: network objects.
1138 */
1139static void
1140biba_socketpeer_set_from_socket(struct socket *oldso,
1141    struct label *oldsolabel, struct socket *newso,
1142    struct label *newsopeerlabel)
1143{
1144	struct mac_biba *source, *dest;
1145
1146	source = SLOT(oldsolabel);
1147	dest = SLOT(newsopeerlabel);
1148
1149	biba_copy_effective(source, dest);
1150}
1151
1152static void
1153biba_bpfdesc_create(struct ucred *cred, struct bpf_d *d,
1154    struct label *dlabel)
1155{
1156	struct mac_biba *source, *dest;
1157
1158	source = SLOT(cred->cr_label);
1159	dest = SLOT(dlabel);
1160
1161	biba_copy_effective(source, dest);
1162}
1163
1164static void
1165biba_ifnet_create(struct ifnet *ifp, struct label *ifplabel)
1166{
1167	char tifname[IFNAMSIZ], *p, *q;
1168	char tiflist[sizeof(trusted_interfaces)];
1169	struct mac_biba *dest;
1170	int len, type;
1171
1172	dest = SLOT(ifplabel);
1173
1174	if (ifp->if_type == IFT_LOOP || interfaces_equal != 0) {
1175		type = MAC_BIBA_TYPE_EQUAL;
1176		goto set;
1177	}
1178
1179	if (trust_all_interfaces) {
1180		type = MAC_BIBA_TYPE_HIGH;
1181		goto set;
1182	}
1183
1184	type = MAC_BIBA_TYPE_LOW;
1185
1186	if (trusted_interfaces[0] == '\0' ||
1187	    !strvalid(trusted_interfaces, sizeof(trusted_interfaces)))
1188		goto set;
1189
1190	bzero(tiflist, sizeof(tiflist));
1191	for (p = trusted_interfaces, q = tiflist; *p != '\0'; p++, q++)
1192		if(*p != ' ' && *p != '\t')
1193			*q = *p;
1194
1195	for (p = q = tiflist;; p++) {
1196		if (*p == ',' || *p == '\0') {
1197			len = p - q;
1198			if (len < IFNAMSIZ) {
1199				bzero(tifname, sizeof(tifname));
1200				bcopy(q, tifname, len);
1201				if (strcmp(tifname, ifp->if_xname) == 0) {
1202					type = MAC_BIBA_TYPE_HIGH;
1203					break;
1204				}
1205			} else {
1206				*p = '\0';
1207				printf("mac_biba warning: interface name "
1208				    "\"%s\" is too long (must be < %d)\n",
1209				    q, IFNAMSIZ);
1210			}
1211			if (*p == '\0')
1212				break;
1213			q = p + 1;
1214		}
1215	}
1216set:
1217	biba_set_effective(dest, type, 0, NULL);
1218	biba_set_range(dest, type, 0, NULL, type, 0, NULL);
1219}
1220
1221static void
1222biba_ipq_create(struct mbuf *m, struct label *mlabel, struct ipq *ipq,
1223    struct label *ipqlabel)
1224{
1225	struct mac_biba *source, *dest;
1226
1227	source = SLOT(mlabel);
1228	dest = SLOT(ipqlabel);
1229
1230	biba_copy_effective(source, dest);
1231}
1232
1233static void
1234biba_ipq_reassemble(struct ipq *ipq, struct label *ipqlabel, struct mbuf *m,
1235    struct label *mlabel)
1236{
1237	struct mac_biba *source, *dest;
1238
1239	source = SLOT(ipqlabel);
1240	dest = SLOT(mlabel);
1241
1242	/* Just use the head, since we require them all to match. */
1243	biba_copy_effective(source, dest);
1244}
1245
1246static void
1247biba_netinet_fragment(struct mbuf *m, struct label *mlabel,
1248    struct mbuf *frag, struct label *fraglabel)
1249{
1250	struct mac_biba *source, *dest;
1251
1252	source = SLOT(mlabel);
1253	dest = SLOT(fraglabel);
1254
1255	biba_copy_effective(source, dest);
1256}
1257
1258static void
1259biba_inpcb_create_mbuf(struct inpcb *inp, struct label *inplabel,
1260    struct mbuf *m, struct label *mlabel)
1261{
1262	struct mac_biba *source, *dest;
1263
1264	source = SLOT(inplabel);
1265	dest = SLOT(mlabel);
1266
1267	biba_copy_effective(source, dest);
1268}
1269
1270static void
1271biba_mbuf_create_linklayer(struct ifnet *ifp, struct label *ifplabel,
1272    struct mbuf *m, struct label *mlabel)
1273{
1274	struct mac_biba *dest;
1275
1276	dest = SLOT(mlabel);
1277
1278	biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
1279}
1280
1281static void
1282biba_bpfdesc_create_mbuf(struct bpf_d *d, struct label *dlabel,
1283    struct mbuf *m, struct label *mlabel)
1284{
1285	struct mac_biba *source, *dest;
1286
1287	source = SLOT(dlabel);
1288	dest = SLOT(mlabel);
1289
1290	biba_copy_effective(source, dest);
1291}
1292
1293static void
1294biba_ifnet_create_mbuf(struct ifnet *ifp, struct label *ifplabel,
1295    struct mbuf *m, struct label *mlabel)
1296{
1297	struct mac_biba *source, *dest;
1298
1299	source = SLOT(ifplabel);
1300	dest = SLOT(mlabel);
1301
1302	biba_copy_effective(source, dest);
1303}
1304
1305static void
1306biba_mbuf_create_multicast_encap(struct mbuf *m, struct label *mlabel,
1307    struct ifnet *ifp, struct label *ifplabel, struct mbuf *mnew,
1308    struct label *mnewlabel)
1309{
1310	struct mac_biba *source, *dest;
1311
1312	source = SLOT(mlabel);
1313	dest = SLOT(mnewlabel);
1314
1315	biba_copy_effective(source, dest);
1316}
1317
1318static void
1319biba_mbuf_create_netlayer(struct mbuf *m, struct label *mlabel,
1320    struct mbuf *newm, struct label *mnewlabel)
1321{
1322	struct mac_biba *source, *dest;
1323
1324	source = SLOT(mlabel);
1325	dest = SLOT(mnewlabel);
1326
1327	biba_copy_effective(source, dest);
1328}
1329
1330static int
1331biba_ipq_match(struct mbuf *m, struct label *mlabel, struct ipq *ipq,
1332    struct label *ipqlabel)
1333{
1334	struct mac_biba *a, *b;
1335
1336	a = SLOT(ipqlabel);
1337	b = SLOT(mlabel);
1338
1339	return (biba_equal_effective(a, b));
1340}
1341
1342static void
1343biba_ifnet_relabel(struct ucred *cred, struct ifnet *ifp,
1344    struct label *ifplabel, struct label *newlabel)
1345{
1346	struct mac_biba *source, *dest;
1347
1348	source = SLOT(newlabel);
1349	dest = SLOT(ifplabel);
1350
1351	biba_copy(source, dest);
1352}
1353
1354static void
1355biba_ipq_update(struct mbuf *m, struct label *mlabel, struct ipq *ipq,
1356    struct label *ipqlabel)
1357{
1358
1359	/* NOOP: we only accept matching labels, so no need to update */
1360}
1361
1362static void
1363biba_inpcb_sosetlabel(struct socket *so, struct label *solabel,
1364    struct inpcb *inp, struct label *inplabel)
1365{
1366	struct mac_biba *source, *dest;
1367
1368	source = SLOT(solabel);
1369	dest = SLOT(inplabel);
1370
1371	biba_copy(source, dest);
1372}
1373
1374static void
1375biba_netinet_firewall_send(struct mbuf *m, struct label *mlabel)
1376{
1377	struct mac_biba *dest;
1378
1379	dest = SLOT(mlabel);
1380
1381	/* XXX: where is the label for the firewall really coming from? */
1382	biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
1383}
1384
1385/*
1386 * Labeling event operations: processes.
1387 */
1388static void
1389biba_proc_create_swapper(struct ucred *cred)
1390{
1391	struct mac_biba *dest;
1392
1393	dest = SLOT(cred->cr_label);
1394
1395	biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
1396	biba_set_range(dest, MAC_BIBA_TYPE_LOW, 0, NULL, MAC_BIBA_TYPE_HIGH,
1397	    0, NULL);
1398}
1399
1400static void
1401biba_proc_create_init(struct ucred *cred)
1402{
1403	struct mac_biba *dest;
1404
1405	dest = SLOT(cred->cr_label);
1406
1407	biba_set_effective(dest, MAC_BIBA_TYPE_HIGH, 0, NULL);
1408	biba_set_range(dest, MAC_BIBA_TYPE_LOW, 0, NULL, MAC_BIBA_TYPE_HIGH,
1409	    0, NULL);
1410}
1411
1412static void
1413biba_proc_associate_nfsd(struct ucred *cred)
1414{
1415	struct mac_biba *label;
1416
1417	label = SLOT(cred->cr_label);
1418	biba_set_effective(label, MAC_BIBA_TYPE_LOW, 0, NULL);
1419	biba_set_range(label, MAC_BIBA_TYPE_LOW, 0, NULL, MAC_BIBA_TYPE_HIGH,
1420	    0, NULL);
1421}
1422
1423static void
1424biba_cred_relabel(struct ucred *cred, struct label *newlabel)
1425{
1426	struct mac_biba *source, *dest;
1427
1428	source = SLOT(newlabel);
1429	dest = SLOT(cred->cr_label);
1430
1431	biba_copy(source, dest);
1432}
1433
1434/*
1435 * Label cleanup/flush operations
1436 */
1437static void
1438biba_sysvmsg_cleanup(struct label *msglabel)
1439{
1440
1441	bzero(SLOT(msglabel), sizeof(struct mac_biba));
1442}
1443
1444static void
1445biba_sysvmsq_cleanup(struct label *msqlabel)
1446{
1447
1448	bzero(SLOT(msqlabel), sizeof(struct mac_biba));
1449}
1450
1451static void
1452biba_sysvsem_cleanup(struct label *semalabel)
1453{
1454
1455	bzero(SLOT(semalabel), sizeof(struct mac_biba));
1456}
1457
1458static void
1459biba_sysvshm_cleanup(struct label *shmlabel)
1460{
1461	bzero(SLOT(shmlabel), sizeof(struct mac_biba));
1462}
1463
1464/*
1465 * Access control checks.
1466 */
1467static int
1468biba_bpfdesc_check_receive(struct bpf_d *d, struct label *dlabel,
1469    struct ifnet *ifp, struct label *ifplabel)
1470{
1471	struct mac_biba *a, *b;
1472
1473	if (!biba_enabled)
1474		return (0);
1475
1476	a = SLOT(dlabel);
1477	b = SLOT(ifplabel);
1478
1479	if (biba_equal_effective(a, b))
1480		return (0);
1481	return (EACCES);
1482}
1483
1484static int
1485biba_cred_check_relabel(struct ucred *cred, struct label *newlabel)
1486{
1487	struct mac_biba *subj, *new;
1488	int error;
1489
1490	subj = SLOT(cred->cr_label);
1491	new = SLOT(newlabel);
1492
1493	/*
1494	 * If there is a Biba label update for the credential, it may
1495	 * be an update of the effective, range, or both.
1496	 */
1497	error = biba_atmostflags(new, MAC_BIBA_FLAGS_BOTH);
1498	if (error)
1499		return (error);
1500
1501	/*
1502	 * If the Biba label is to be changed, authorize as appropriate.
1503	 */
1504	if (new->mb_flags & MAC_BIBA_FLAGS_BOTH) {
1505		/*
1506		 * If the change request modifies both the Biba label
1507		 * effective and range, check that the new effective will be
1508		 * in the new range.
1509		 */
1510		if ((new->mb_flags & MAC_BIBA_FLAGS_BOTH) ==
1511		    MAC_BIBA_FLAGS_BOTH &&
1512		    !biba_effective_in_range(new, new))
1513			return (EINVAL);
1514
1515		/*
1516		 * To change the Biba effective label on a credential, the
1517		 * new effective label must be in the current range.
1518		 */
1519		if (new->mb_flags & MAC_BIBA_FLAG_EFFECTIVE &&
1520		    !biba_effective_in_range(new, subj))
1521			return (EPERM);
1522
1523		/*
1524		 * To change the Biba range on a credential, the new range
1525		 * label must be in the current range.
1526		 */
1527		if (new->mb_flags & MAC_BIBA_FLAG_RANGE &&
1528		    !biba_range_in_range(new, subj))
1529			return (EPERM);
1530
1531		/*
1532		 * To have EQUAL in any component of the new credential Biba
1533		 * label, the subject must already have EQUAL in their label.
1534		 */
1535		if (biba_contains_equal(new)) {
1536			error = biba_subject_privileged(subj);
1537			if (error)
1538				return (error);
1539		}
1540	}
1541
1542	return (0);
1543}
1544
1545static int
1546biba_cred_check_visible(struct ucred *u1, struct ucred *u2)
1547{
1548	struct mac_biba *subj, *obj;
1549
1550	if (!biba_enabled)
1551		return (0);
1552
1553	subj = SLOT(u1->cr_label);
1554	obj = SLOT(u2->cr_label);
1555
1556	/* XXX: range */
1557	if (!biba_dominate_effective(obj, subj))
1558		return (ESRCH);
1559
1560	return (0);
1561}
1562
1563static int
1564biba_ifnet_check_relabel(struct ucred *cred, struct ifnet *ifp,
1565    struct label *ifplabel, struct label *newlabel)
1566{
1567	struct mac_biba *subj, *new;
1568	int error;
1569
1570	subj = SLOT(cred->cr_label);
1571	new = SLOT(newlabel);
1572
1573	/*
1574	 * If there is a Biba label update for the interface, it may be an
1575	 * update of the effective, range, or both.
1576	 */
1577	error = biba_atmostflags(new, MAC_BIBA_FLAGS_BOTH);
1578	if (error)
1579		return (error);
1580
1581	/*
1582	 * Relabling network interfaces requires Biba privilege.
1583	 */
1584	error = biba_subject_privileged(subj);
1585	if (error)
1586		return (error);
1587
1588	return (0);
1589}
1590
1591static int
1592biba_ifnet_check_transmit(struct ifnet *ifp, struct label *ifplabel,
1593    struct mbuf *m, struct label *mlabel)
1594{
1595	struct mac_biba *p, *i;
1596
1597	if (!biba_enabled)
1598		return (0);
1599
1600	p = SLOT(mlabel);
1601	i = SLOT(ifplabel);
1602
1603	return (biba_effective_in_range(p, i) ? 0 : EACCES);
1604}
1605
1606static int
1607biba_inpcb_check_deliver(struct inpcb *inp, struct label *inplabel,
1608    struct mbuf *m, struct label *mlabel)
1609{
1610	struct mac_biba *p, *i;
1611
1612	if (!biba_enabled)
1613		return (0);
1614
1615	p = SLOT(mlabel);
1616	i = SLOT(inplabel);
1617
1618	return (biba_equal_effective(p, i) ? 0 : EACCES);
1619}
1620
1621static int
1622biba_sysvmsq_check_msgrcv(struct ucred *cred, struct msg *msgptr,
1623    struct label *msglabel)
1624{
1625	struct mac_biba *subj, *obj;
1626
1627	if (!biba_enabled)
1628		return (0);
1629
1630	subj = SLOT(cred->cr_label);
1631	obj = SLOT(msglabel);
1632
1633	if (!biba_dominate_effective(obj, subj))
1634		return (EACCES);
1635
1636	return (0);
1637}
1638
1639static int
1640biba_sysvmsq_check_msgrmid(struct ucred *cred, struct msg *msgptr,
1641    struct label *msglabel)
1642{
1643	struct mac_biba *subj, *obj;
1644
1645	if (!biba_enabled)
1646		return (0);
1647
1648	subj = SLOT(cred->cr_label);
1649	obj = SLOT(msglabel);
1650
1651	if (!biba_dominate_effective(subj, obj))
1652		return (EACCES);
1653
1654	return (0);
1655}
1656
1657static int
1658biba_sysvmsq_check_msqget(struct ucred *cred, struct msqid_kernel *msqkptr,
1659    struct label *msqklabel)
1660{
1661	struct mac_biba *subj, *obj;
1662
1663	if (!biba_enabled)
1664		return (0);
1665
1666	subj = SLOT(cred->cr_label);
1667	obj = SLOT(msqklabel);
1668
1669	if (!biba_dominate_effective(obj, subj))
1670		return (EACCES);
1671
1672	return (0);
1673}
1674
1675static int
1676biba_sysvmsq_check_msqsnd(struct ucred *cred, struct msqid_kernel *msqkptr,
1677    struct label *msqklabel)
1678{
1679	struct mac_biba *subj, *obj;
1680
1681	if (!biba_enabled)
1682		return (0);
1683
1684	subj = SLOT(cred->cr_label);
1685	obj = SLOT(msqklabel);
1686
1687	if (!biba_dominate_effective(subj, obj))
1688		return (EACCES);
1689
1690	return (0);
1691}
1692
1693static int
1694biba_sysvmsq_check_msqrcv(struct ucred *cred, struct msqid_kernel *msqkptr,
1695    struct label *msqklabel)
1696{
1697	struct mac_biba *subj, *obj;
1698
1699	if (!biba_enabled)
1700		return (0);
1701
1702	subj = SLOT(cred->cr_label);
1703	obj = SLOT(msqklabel);
1704
1705	if (!biba_dominate_effective(obj, subj))
1706		return (EACCES);
1707
1708	return (0);
1709}
1710
1711static int
1712biba_sysvmsq_check_msqctl(struct ucred *cred, struct msqid_kernel *msqkptr,
1713    struct label *msqklabel, int cmd)
1714{
1715	struct mac_biba *subj, *obj;
1716
1717	if (!biba_enabled)
1718		return (0);
1719
1720	subj = SLOT(cred->cr_label);
1721	obj = SLOT(msqklabel);
1722
1723	switch(cmd) {
1724	case IPC_RMID:
1725	case IPC_SET:
1726		if (!biba_dominate_effective(subj, obj))
1727			return (EACCES);
1728		break;
1729
1730	case IPC_STAT:
1731		if (!biba_dominate_effective(obj, subj))
1732			return (EACCES);
1733		break;
1734
1735	default:
1736		return (EACCES);
1737	}
1738
1739	return (0);
1740}
1741
1742static int
1743biba_sysvsem_check_semctl(struct ucred *cred, struct semid_kernel *semakptr,
1744    struct label *semaklabel, int cmd)
1745{
1746	struct mac_biba *subj, *obj;
1747
1748	if (!biba_enabled)
1749		return (0);
1750
1751	subj = SLOT(cred->cr_label);
1752	obj = SLOT(semaklabel);
1753
1754	switch(cmd) {
1755	case IPC_RMID:
1756	case IPC_SET:
1757	case SETVAL:
1758	case SETALL:
1759		if (!biba_dominate_effective(subj, obj))
1760			return (EACCES);
1761		break;
1762
1763	case IPC_STAT:
1764	case GETVAL:
1765	case GETPID:
1766	case GETNCNT:
1767	case GETZCNT:
1768	case GETALL:
1769		if (!biba_dominate_effective(obj, subj))
1770			return (EACCES);
1771		break;
1772
1773	default:
1774		return (EACCES);
1775	}
1776
1777	return (0);
1778}
1779
1780static int
1781biba_sysvsem_check_semget(struct ucred *cred, struct semid_kernel *semakptr,
1782    struct label *semaklabel)
1783{
1784	struct mac_biba *subj, *obj;
1785
1786	if (!biba_enabled)
1787		return (0);
1788
1789	subj = SLOT(cred->cr_label);
1790	obj = SLOT(semaklabel);
1791
1792	if (!biba_dominate_effective(obj, subj))
1793		return (EACCES);
1794
1795	return (0);
1796}
1797
1798static int
1799biba_sysvsem_check_semop(struct ucred *cred, struct semid_kernel *semakptr,
1800    struct label *semaklabel, size_t accesstype)
1801{
1802	struct mac_biba *subj, *obj;
1803
1804	if (!biba_enabled)
1805		return (0);
1806
1807	subj = SLOT(cred->cr_label);
1808	obj = SLOT(semaklabel);
1809
1810	if (accesstype & SEM_R)
1811		if (!biba_dominate_effective(obj, subj))
1812			return (EACCES);
1813
1814	if (accesstype & SEM_A)
1815		if (!biba_dominate_effective(subj, obj))
1816			return (EACCES);
1817
1818	return (0);
1819}
1820
1821static int
1822biba_sysvshm_check_shmat(struct ucred *cred, struct shmid_kernel *shmsegptr,
1823    struct label *shmseglabel, int shmflg)
1824{
1825	struct mac_biba *subj, *obj;
1826
1827	if (!biba_enabled)
1828		return (0);
1829
1830	subj = SLOT(cred->cr_label);
1831	obj = SLOT(shmseglabel);
1832
1833	if (!biba_dominate_effective(obj, subj))
1834		return (EACCES);
1835	if ((shmflg & SHM_RDONLY) == 0) {
1836		if (!biba_dominate_effective(subj, obj))
1837			return (EACCES);
1838	}
1839
1840	return (0);
1841}
1842
1843static int
1844biba_sysvshm_check_shmctl(struct ucred *cred, struct shmid_kernel *shmsegptr,
1845    struct label *shmseglabel, int cmd)
1846{
1847	struct mac_biba *subj, *obj;
1848
1849	if (!biba_enabled)
1850		return (0);
1851
1852	subj = SLOT(cred->cr_label);
1853	obj = SLOT(shmseglabel);
1854
1855	switch(cmd) {
1856	case IPC_RMID:
1857	case IPC_SET:
1858		if (!biba_dominate_effective(subj, obj))
1859			return (EACCES);
1860		break;
1861
1862	case IPC_STAT:
1863	case SHM_STAT:
1864		if (!biba_dominate_effective(obj, subj))
1865			return (EACCES);
1866		break;
1867
1868	default:
1869		return (EACCES);
1870	}
1871
1872	return (0);
1873}
1874
1875static int
1876biba_sysvshm_check_shmget(struct ucred *cred, struct shmid_kernel *shmsegptr,
1877    struct label *shmseglabel, int shmflg)
1878{
1879	struct mac_biba *subj, *obj;
1880
1881	if (!biba_enabled)
1882		return (0);
1883
1884	subj = SLOT(cred->cr_label);
1885	obj = SLOT(shmseglabel);
1886
1887	if (!biba_dominate_effective(obj, subj))
1888		return (EACCES);
1889
1890	return (0);
1891}
1892
1893static int
1894biba_kld_check_load(struct ucred *cred, struct vnode *vp,
1895    struct label *vplabel)
1896{
1897	struct mac_biba *subj, *obj;
1898	int error;
1899
1900	if (!biba_enabled)
1901		return (0);
1902
1903	subj = SLOT(cred->cr_label);
1904
1905	error = biba_subject_privileged(subj);
1906	if (error)
1907		return (error);
1908
1909	obj = SLOT(vplabel);
1910	if (!biba_high_effective(obj))
1911		return (EACCES);
1912
1913	return (0);
1914}
1915
1916static int
1917biba_mount_check_stat(struct ucred *cred, struct mount *mp,
1918    struct label *mplabel)
1919{
1920	struct mac_biba *subj, *obj;
1921
1922	if (!biba_enabled)
1923		return (0);
1924
1925	subj = SLOT(cred->cr_label);
1926	obj = SLOT(mplabel);
1927
1928	if (!biba_dominate_effective(obj, subj))
1929		return (EACCES);
1930
1931	return (0);
1932}
1933
1934static int
1935biba_pipe_check_ioctl(struct ucred *cred, struct pipepair *pp,
1936    struct label *pplabel, unsigned long cmd, void /* caddr_t */ *data)
1937{
1938
1939	if(!biba_enabled)
1940		return (0);
1941
1942	/* XXX: This will be implemented soon... */
1943
1944	return (0);
1945}
1946
1947static int
1948biba_pipe_check_poll(struct ucred *cred, struct pipepair *pp,
1949    struct label *pplabel)
1950{
1951	struct mac_biba *subj, *obj;
1952
1953	if (!biba_enabled)
1954		return (0);
1955
1956	subj = SLOT(cred->cr_label);
1957	obj = SLOT(pplabel);
1958
1959	if (!biba_dominate_effective(obj, subj))
1960		return (EACCES);
1961
1962	return (0);
1963}
1964
1965static int
1966biba_pipe_check_read(struct ucred *cred, struct pipepair *pp,
1967    struct label *pplabel)
1968{
1969	struct mac_biba *subj, *obj;
1970
1971	if (!biba_enabled)
1972		return (0);
1973
1974	subj = SLOT(cred->cr_label);
1975	obj = SLOT(pplabel);
1976
1977	if (!biba_dominate_effective(obj, subj))
1978		return (EACCES);
1979
1980	return (0);
1981}
1982
1983static int
1984biba_pipe_check_relabel(struct ucred *cred, struct pipepair *pp,
1985    struct label *pplabel, struct label *newlabel)
1986{
1987	struct mac_biba *subj, *obj, *new;
1988	int error;
1989
1990	new = SLOT(newlabel);
1991	subj = SLOT(cred->cr_label);
1992	obj = SLOT(pplabel);
1993
1994	/*
1995	 * If there is a Biba label update for a pipe, it must be a effective
1996	 * update.
1997	 */
1998	error = biba_atmostflags(new, MAC_BIBA_FLAG_EFFECTIVE);
1999	if (error)
2000		return (error);
2001
2002	/*
2003	 * To perform a relabel of a pipe (Biba label or not), Biba must
2004	 * authorize the relabel.
2005	 */
2006	if (!biba_effective_in_range(obj, subj))
2007		return (EPERM);
2008
2009	/*
2010	 * If the Biba label is to be changed, authorize as appropriate.
2011	 */
2012	if (new->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) {
2013		/*
2014		 * To change the Biba label on a pipe, the new pipe label
2015		 * must be in the subject range.
2016		 */
2017		if (!biba_effective_in_range(new, subj))
2018			return (EPERM);
2019
2020		/*
2021		 * To change the Biba label on a pipe to be EQUAL, the
2022		 * subject must have appropriate privilege.
2023		 */
2024		if (biba_contains_equal(new)) {
2025			error = biba_subject_privileged(subj);
2026			if (error)
2027				return (error);
2028		}
2029	}
2030
2031	return (0);
2032}
2033
2034static int
2035biba_pipe_check_stat(struct ucred *cred, struct pipepair *pp,
2036    struct label *pplabel)
2037{
2038	struct mac_biba *subj, *obj;
2039
2040	if (!biba_enabled)
2041		return (0);
2042
2043	subj = SLOT(cred->cr_label);
2044	obj = SLOT(pplabel);
2045
2046	if (!biba_dominate_effective(obj, subj))
2047		return (EACCES);
2048
2049	return (0);
2050}
2051
2052static int
2053biba_pipe_check_write(struct ucred *cred, struct pipepair *pp,
2054    struct label *pplabel)
2055{
2056	struct mac_biba *subj, *obj;
2057
2058	if (!biba_enabled)
2059		return (0);
2060
2061	subj = SLOT(cred->cr_label);
2062	obj = SLOT(pplabel);
2063
2064	if (!biba_dominate_effective(subj, obj))
2065		return (EACCES);
2066
2067	return (0);
2068}
2069
2070static int
2071biba_posixsem_check_write(struct ucred *cred, struct ksem *ks,
2072    struct label *kslabel)
2073{
2074	struct mac_biba *subj, *obj;
2075
2076	if (!biba_enabled)
2077		return (0);
2078
2079	subj = SLOT(cred->cr_label);
2080	obj = SLOT(kslabel);
2081
2082	if (!biba_dominate_effective(subj, obj))
2083		return (EACCES);
2084
2085	return (0);
2086}
2087
2088static int
2089biba_posixsem_check_rdonly(struct ucred *cred, struct ksem *ks,
2090    struct label *kslabel)
2091{
2092	struct mac_biba *subj, *obj;
2093
2094	if (!biba_enabled)
2095		return (0);
2096
2097	subj = SLOT(cred->cr_label);
2098	obj = SLOT(kslabel);
2099
2100	if (!biba_dominate_effective(obj, subj))
2101		return (EACCES);
2102
2103	return (0);
2104}
2105
2106static int
2107biba_proc_check_debug(struct ucred *cred, struct proc *p)
2108{
2109	struct mac_biba *subj, *obj;
2110
2111	if (!biba_enabled)
2112		return (0);
2113
2114	subj = SLOT(cred->cr_label);
2115	obj = SLOT(p->p_ucred->cr_label);
2116
2117	/* XXX: range checks */
2118	if (!biba_dominate_effective(obj, subj))
2119		return (ESRCH);
2120	if (!biba_dominate_effective(subj, obj))
2121		return (EACCES);
2122
2123	return (0);
2124}
2125
2126static int
2127biba_proc_check_sched(struct ucred *cred, struct proc *p)
2128{
2129	struct mac_biba *subj, *obj;
2130
2131	if (!biba_enabled)
2132		return (0);
2133
2134	subj = SLOT(cred->cr_label);
2135	obj = SLOT(p->p_ucred->cr_label);
2136
2137	/* XXX: range checks */
2138	if (!biba_dominate_effective(obj, subj))
2139		return (ESRCH);
2140	if (!biba_dominate_effective(subj, obj))
2141		return (EACCES);
2142
2143	return (0);
2144}
2145
2146static int
2147biba_proc_check_signal(struct ucred *cred, struct proc *p, int signum)
2148{
2149	struct mac_biba *subj, *obj;
2150
2151	if (!biba_enabled)
2152		return (0);
2153
2154	subj = SLOT(cred->cr_label);
2155	obj = SLOT(p->p_ucred->cr_label);
2156
2157	/* XXX: range checks */
2158	if (!biba_dominate_effective(obj, subj))
2159		return (ESRCH);
2160	if (!biba_dominate_effective(subj, obj))
2161		return (EACCES);
2162
2163	return (0);
2164}
2165
2166static int
2167biba_socket_check_deliver(struct socket *so, struct label *solabel,
2168    struct mbuf *m, struct label *mlabel)
2169{
2170	struct mac_biba *p, *s;
2171
2172	if (!biba_enabled)
2173		return (0);
2174
2175	p = SLOT(mlabel);
2176	s = SLOT(solabel);
2177
2178	return (biba_equal_effective(p, s) ? 0 : EACCES);
2179}
2180
2181static int
2182biba_socket_check_relabel(struct ucred *cred, struct socket *so,
2183    struct label *solabel, struct label *newlabel)
2184{
2185	struct mac_biba *subj, *obj, *new;
2186	int error;
2187
2188	new = SLOT(newlabel);
2189	subj = SLOT(cred->cr_label);
2190	obj = SLOT(solabel);
2191
2192	/*
2193	 * If there is a Biba label update for the socket, it may be an
2194	 * update of effective.
2195	 */
2196	error = biba_atmostflags(new, MAC_BIBA_FLAG_EFFECTIVE);
2197	if (error)
2198		return (error);
2199
2200	/*
2201	 * To relabel a socket, the old socket effective must be in the
2202	 * subject range.
2203	 */
2204	if (!biba_effective_in_range(obj, subj))
2205		return (EPERM);
2206
2207	/*
2208	 * If the Biba label is to be changed, authorize as appropriate.
2209	 */
2210	if (new->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) {
2211		/*
2212		 * To relabel a socket, the new socket effective must be in
2213		 * the subject range.
2214		 */
2215		if (!biba_effective_in_range(new, subj))
2216			return (EPERM);
2217
2218		/*
2219		 * To change the Biba label on the socket to contain EQUAL,
2220		 * the subject must have appropriate privilege.
2221		 */
2222		if (biba_contains_equal(new)) {
2223			error = biba_subject_privileged(subj);
2224			if (error)
2225				return (error);
2226		}
2227	}
2228
2229	return (0);
2230}
2231
2232static int
2233biba_socket_check_visible(struct ucred *cred, struct socket *so,
2234    struct label *solabel)
2235{
2236	struct mac_biba *subj, *obj;
2237
2238	if (!biba_enabled)
2239		return (0);
2240
2241	subj = SLOT(cred->cr_label);
2242	obj = SLOT(solabel);
2243
2244	if (!biba_dominate_effective(obj, subj))
2245		return (ENOENT);
2246
2247	return (0);
2248}
2249
2250/*
2251 * Some system privileges are allowed regardless of integrity grade; others
2252 * are allowed only when running with privilege with respect to the Biba
2253 * policy as they might otherwise allow bypassing of the integrity policy.
2254 */
2255static int
2256biba_priv_check(struct ucred *cred, int priv)
2257{
2258	struct mac_biba *subj;
2259	int error;
2260
2261	if (!biba_enabled)
2262		return (0);
2263
2264	/*
2265	 * Exempt only specific privileges from the Biba integrity policy.
2266	 */
2267	switch (priv) {
2268	case PRIV_KTRACE:
2269	case PRIV_MSGBUF:
2270
2271	/*
2272	 * Allow processes to manipulate basic process audit properties, and
2273	 * to submit audit records.
2274	 */
2275	case PRIV_AUDIT_GETAUDIT:
2276	case PRIV_AUDIT_SETAUDIT:
2277	case PRIV_AUDIT_SUBMIT:
2278
2279	/*
2280	 * Allow processes to manipulate their regular UNIX credentials.
2281	 */
2282	case PRIV_CRED_SETUID:
2283	case PRIV_CRED_SETEUID:
2284	case PRIV_CRED_SETGID:
2285	case PRIV_CRED_SETEGID:
2286	case PRIV_CRED_SETGROUPS:
2287	case PRIV_CRED_SETREUID:
2288	case PRIV_CRED_SETREGID:
2289	case PRIV_CRED_SETRESUID:
2290	case PRIV_CRED_SETRESGID:
2291
2292	/*
2293	 * Allow processes to perform system monitoring.
2294	 */
2295	case PRIV_SEEOTHERGIDS:
2296	case PRIV_SEEOTHERUIDS:
2297		break;
2298
2299	/*
2300	 * Allow access to general process debugging facilities.  We
2301	 * separately control debugging based on MAC label.
2302	 */
2303	case PRIV_DEBUG_DIFFCRED:
2304	case PRIV_DEBUG_SUGID:
2305	case PRIV_DEBUG_UNPRIV:
2306
2307	/*
2308	 * Allow manipulating jails.
2309	 */
2310	case PRIV_JAIL_ATTACH:
2311
2312	/*
2313	 * Allow privilege with respect to the Partition policy, but not the
2314	 * Privs policy.
2315	 */
2316	case PRIV_MAC_PARTITION:
2317
2318	/*
2319	 * Allow privilege with respect to process resource limits and login
2320	 * context.
2321	 */
2322	case PRIV_PROC_LIMIT:
2323	case PRIV_PROC_SETLOGIN:
2324	case PRIV_PROC_SETRLIMIT:
2325
2326	/*
2327	 * Allow System V and POSIX IPC privileges.
2328	 */
2329	case PRIV_IPC_READ:
2330	case PRIV_IPC_WRITE:
2331	case PRIV_IPC_ADMIN:
2332	case PRIV_IPC_MSGSIZE:
2333	case PRIV_MQ_ADMIN:
2334
2335	/*
2336	 * Allow certain scheduler manipulations -- possibly this should be
2337	 * controlled by more fine-grained policy, as potentially low
2338	 * integrity processes can deny CPU to higher integrity ones.
2339	 */
2340	case PRIV_SCHED_DIFFCRED:
2341	case PRIV_SCHED_SETPRIORITY:
2342	case PRIV_SCHED_RTPRIO:
2343	case PRIV_SCHED_SETPOLICY:
2344	case PRIV_SCHED_SET:
2345	case PRIV_SCHED_SETPARAM:
2346
2347	/*
2348	 * More IPC privileges.
2349	 */
2350	case PRIV_SEM_WRITE:
2351
2352	/*
2353	 * Allow signaling privileges subject to integrity policy.
2354	 */
2355	case PRIV_SIGNAL_DIFFCRED:
2356	case PRIV_SIGNAL_SUGID:
2357
2358	/*
2359	 * Allow access to only limited sysctls from lower integrity levels;
2360	 * piggy-back on the Jail definition.
2361	 */
2362	case PRIV_SYSCTL_WRITEJAIL:
2363
2364	/*
2365	 * Allow TTY-based privileges, subject to general device access using
2366	 * labels on TTY device nodes, but not console privilege.
2367	 */
2368	case PRIV_TTY_DRAINWAIT:
2369	case PRIV_TTY_DTRWAIT:
2370	case PRIV_TTY_EXCLUSIVE:
2371	case PRIV_TTY_PRISON:
2372	case PRIV_TTY_STI:
2373	case PRIV_TTY_SETA:
2374
2375	/*
2376	 * Grant most VFS privileges, as almost all are in practice bounded
2377	 * by more specific checks using labels.
2378	 */
2379	case PRIV_VFS_READ:
2380	case PRIV_VFS_WRITE:
2381	case PRIV_VFS_ADMIN:
2382	case PRIV_VFS_EXEC:
2383	case PRIV_VFS_LOOKUP:
2384	case PRIV_VFS_CHFLAGS_DEV:
2385	case PRIV_VFS_CHOWN:
2386	case PRIV_VFS_CHROOT:
2387	case PRIV_VFS_RETAINSUGID:
2388	case PRIV_VFS_EXCEEDQUOTA:
2389	case PRIV_VFS_FCHROOT:
2390	case PRIV_VFS_FHOPEN:
2391	case PRIV_VFS_FHSTATFS:
2392	case PRIV_VFS_GENERATION:
2393	case PRIV_VFS_GETFH:
2394	case PRIV_VFS_GETQUOTA:
2395	case PRIV_VFS_LINK:
2396	case PRIV_VFS_MOUNT:
2397	case PRIV_VFS_MOUNT_OWNER:
2398	case PRIV_VFS_MOUNT_PERM:
2399	case PRIV_VFS_MOUNT_SUIDDIR:
2400	case PRIV_VFS_MOUNT_NONUSER:
2401	case PRIV_VFS_SETGID:
2402	case PRIV_VFS_STICKYFILE:
2403	case PRIV_VFS_SYSFLAGS:
2404	case PRIV_VFS_UNMOUNT:
2405
2406	/*
2407	 * Allow VM privileges; it would be nice if these were subject to
2408	 * resource limits.
2409	 */
2410	case PRIV_VM_MADV_PROTECT:
2411	case PRIV_VM_MLOCK:
2412	case PRIV_VM_MUNLOCK:
2413
2414	/*
2415	 * Allow some but not all network privileges.  In general, dont allow
2416	 * reconfiguring the network stack, just normal use.
2417	 */
2418	case PRIV_NETATALK_RESERVEDPORT:
2419	case PRIV_NETINET_RESERVEDPORT:
2420	case PRIV_NETINET_RAW:
2421	case PRIV_NETINET_REUSEPORT:
2422	case PRIV_NETIPX_RESERVEDPORT:
2423	case PRIV_NETIPX_RAW:
2424		break;
2425
2426	/*
2427	 * All remaining system privileges are allow only if the process
2428	 * holds privilege with respect to the Biba policy.
2429	 */
2430	default:
2431		subj = SLOT(cred->cr_label);
2432		error = biba_subject_privileged(subj);
2433		if (error)
2434			return (error);
2435	}
2436	return (0);
2437}
2438
2439static int
2440biba_system_check_acct(struct ucred *cred, struct vnode *vp,
2441    struct label *vplabel)
2442{
2443	struct mac_biba *subj, *obj;
2444	int error;
2445
2446	if (!biba_enabled)
2447		return (0);
2448
2449	subj = SLOT(cred->cr_label);
2450
2451	error = biba_subject_privileged(subj);
2452	if (error)
2453		return (error);
2454
2455	if (vplabel == NULL)
2456		return (0);
2457
2458	obj = SLOT(vplabel);
2459	if (!biba_high_effective(obj))
2460		return (EACCES);
2461
2462	return (0);
2463}
2464
2465static int
2466biba_system_check_auditctl(struct ucred *cred, struct vnode *vp,
2467    struct label *vplabel)
2468{
2469	struct mac_biba *subj, *obj;
2470	int error;
2471
2472	if (!biba_enabled)
2473		return (0);
2474
2475	subj = SLOT(cred->cr_label);
2476
2477	error = biba_subject_privileged(subj);
2478	if (error)
2479		return (error);
2480
2481	if (vplabel == NULL)
2482		return (0);
2483
2484	obj = SLOT(vplabel);
2485	if (!biba_high_effective(obj))
2486		return (EACCES);
2487
2488	return (0);
2489}
2490
2491static int
2492biba_system_check_auditon(struct ucred *cred, int cmd)
2493{
2494	struct mac_biba *subj;
2495	int error;
2496
2497	if (!biba_enabled)
2498		return (0);
2499
2500	subj = SLOT(cred->cr_label);
2501
2502	error = biba_subject_privileged(subj);
2503	if (error)
2504		return (error);
2505
2506	return (0);
2507}
2508
2509static int
2510biba_system_check_swapon(struct ucred *cred, struct vnode *vp,
2511    struct label *vplabel)
2512{
2513	struct mac_biba *subj, *obj;
2514	int error;
2515
2516	if (!biba_enabled)
2517		return (0);
2518
2519	subj = SLOT(cred->cr_label);
2520	obj = SLOT(vplabel);
2521
2522	error = biba_subject_privileged(subj);
2523	if (error)
2524		return (error);
2525
2526	if (!biba_high_effective(obj))
2527		return (EACCES);
2528
2529	return (0);
2530}
2531
2532static int
2533biba_system_check_swapoff(struct ucred *cred, struct vnode *vp,
2534    struct label *label)
2535{
2536	struct mac_biba *subj;
2537	int error;
2538
2539	if (!biba_enabled)
2540		return (0);
2541
2542	subj = SLOT(cred->cr_label);
2543
2544	error = biba_subject_privileged(subj);
2545	if (error)
2546		return (error);
2547
2548	return (0);
2549}
2550
2551static int
2552biba_system_check_sysctl(struct ucred *cred, struct sysctl_oid *oidp,
2553    void *arg1, int arg2, struct sysctl_req *req)
2554{
2555	struct mac_biba *subj;
2556	int error;
2557
2558	if (!biba_enabled)
2559		return (0);
2560
2561	subj = SLOT(cred->cr_label);
2562
2563	/*
2564	 * Treat sysctl variables without CTLFLAG_ANYBODY flag as biba/high,
2565	 * but also require privilege to change them.
2566	 */
2567	if (req->newptr != NULL && (oidp->oid_kind & CTLFLAG_ANYBODY) == 0) {
2568		if (!biba_subject_dominate_high(subj))
2569			return (EACCES);
2570
2571		error = biba_subject_privileged(subj);
2572		if (error)
2573			return (error);
2574	}
2575
2576	return (0);
2577}
2578
2579static int
2580biba_vnode_check_chdir(struct ucred *cred, struct vnode *dvp,
2581    struct label *dvplabel)
2582{
2583	struct mac_biba *subj, *obj;
2584
2585	if (!biba_enabled)
2586		return (0);
2587
2588	subj = SLOT(cred->cr_label);
2589	obj = SLOT(dvplabel);
2590
2591	if (!biba_dominate_effective(obj, subj))
2592		return (EACCES);
2593
2594	return (0);
2595}
2596
2597static int
2598biba_vnode_check_chroot(struct ucred *cred, struct vnode *dvp,
2599    struct label *dvplabel)
2600{
2601	struct mac_biba *subj, *obj;
2602
2603	if (!biba_enabled)
2604		return (0);
2605
2606	subj = SLOT(cred->cr_label);
2607	obj = SLOT(dvplabel);
2608
2609	if (!biba_dominate_effective(obj, subj))
2610		return (EACCES);
2611
2612	return (0);
2613}
2614
2615static int
2616biba_vnode_check_create(struct ucred *cred, struct vnode *dvp,
2617    struct label *dvplabel, struct componentname *cnp, struct vattr *vap)
2618{
2619	struct mac_biba *subj, *obj;
2620
2621	if (!biba_enabled)
2622		return (0);
2623
2624	subj = SLOT(cred->cr_label);
2625	obj = SLOT(dvplabel);
2626
2627	if (!biba_dominate_effective(subj, obj))
2628		return (EACCES);
2629
2630	return (0);
2631}
2632
2633static int
2634biba_vnode_check_deleteacl(struct ucred *cred, struct vnode *vp,
2635    struct label *vplabel, acl_type_t type)
2636{
2637	struct mac_biba *subj, *obj;
2638
2639	if (!biba_enabled)
2640		return (0);
2641
2642	subj = SLOT(cred->cr_label);
2643	obj = SLOT(vplabel);
2644
2645	if (!biba_dominate_effective(subj, obj))
2646		return (EACCES);
2647
2648	return (0);
2649}
2650
2651static int
2652biba_vnode_check_deleteextattr(struct ucred *cred, struct vnode *vp,
2653    struct label *vplabel, int attrnamespace, const char *name)
2654{
2655	struct mac_biba *subj, *obj;
2656
2657	if (!biba_enabled)
2658		return (0);
2659
2660	subj = SLOT(cred->cr_label);
2661	obj = SLOT(vplabel);
2662
2663	if (!biba_dominate_effective(subj, obj))
2664		return (EACCES);
2665
2666	return (0);
2667}
2668
2669static int
2670biba_vnode_check_exec(struct ucred *cred, struct vnode *vp,
2671    struct label *vplabel, struct image_params *imgp,
2672    struct label *execlabel)
2673{
2674	struct mac_biba *subj, *obj, *exec;
2675	int error;
2676
2677	if (execlabel != NULL) {
2678		/*
2679		 * We currently don't permit labels to be changed at
2680		 * exec-time as part of Biba, so disallow non-NULL Biba label
2681		 * elements in the execlabel.
2682		 */
2683		exec = SLOT(execlabel);
2684		error = biba_atmostflags(exec, 0);
2685		if (error)
2686			return (error);
2687	}
2688
2689	if (!biba_enabled)
2690		return (0);
2691
2692	subj = SLOT(cred->cr_label);
2693	obj = SLOT(vplabel);
2694
2695	if (!biba_dominate_effective(obj, subj))
2696		return (EACCES);
2697
2698	return (0);
2699}
2700
2701static int
2702biba_vnode_check_getacl(struct ucred *cred, struct vnode *vp,
2703    struct label *vplabel, acl_type_t type)
2704{
2705	struct mac_biba *subj, *obj;
2706
2707	if (!biba_enabled)
2708		return (0);
2709
2710	subj = SLOT(cred->cr_label);
2711	obj = SLOT(vplabel);
2712
2713	if (!biba_dominate_effective(obj, subj))
2714		return (EACCES);
2715
2716	return (0);
2717}
2718
2719static int
2720biba_vnode_check_getextattr(struct ucred *cred, struct vnode *vp,
2721    struct label *vplabel, int attrnamespace, const char *name,
2722    struct uio *uio)
2723{
2724	struct mac_biba *subj, *obj;
2725
2726	if (!biba_enabled)
2727		return (0);
2728
2729	subj = SLOT(cred->cr_label);
2730	obj = SLOT(vplabel);
2731
2732	if (!biba_dominate_effective(obj, subj))
2733		return (EACCES);
2734
2735	return (0);
2736}
2737
2738static int
2739biba_vnode_check_link(struct ucred *cred, struct vnode *dvp,
2740    struct label *dvplabel, struct vnode *vp, struct label *vplabel,
2741    struct componentname *cnp)
2742{
2743	struct mac_biba *subj, *obj;
2744
2745	if (!biba_enabled)
2746		return (0);
2747
2748	subj = SLOT(cred->cr_label);
2749	obj = SLOT(dvplabel);
2750
2751	if (!biba_dominate_effective(subj, obj))
2752		return (EACCES);
2753
2754	obj = SLOT(vplabel);
2755
2756	if (!biba_dominate_effective(subj, obj))
2757		return (EACCES);
2758
2759	return (0);
2760}
2761
2762static int
2763biba_vnode_check_listextattr(struct ucred *cred, struct vnode *vp,
2764    struct label *vplabel, int attrnamespace)
2765{
2766	struct mac_biba *subj, *obj;
2767
2768	if (!biba_enabled)
2769		return (0);
2770
2771	subj = SLOT(cred->cr_label);
2772	obj = SLOT(vplabel);
2773
2774	if (!biba_dominate_effective(obj, subj))
2775		return (EACCES);
2776
2777	return (0);
2778}
2779
2780static int
2781biba_vnode_check_lookup(struct ucred *cred, struct vnode *dvp,
2782    struct label *dvplabel, struct componentname *cnp)
2783{
2784	struct mac_biba *subj, *obj;
2785
2786	if (!biba_enabled)
2787		return (0);
2788
2789	subj = SLOT(cred->cr_label);
2790	obj = SLOT(dvplabel);
2791
2792	if (!biba_dominate_effective(obj, subj))
2793		return (EACCES);
2794
2795	return (0);
2796}
2797
2798static int
2799biba_vnode_check_mmap(struct ucred *cred, struct vnode *vp,
2800    struct label *vplabel, int prot, int flags)
2801{
2802	struct mac_biba *subj, *obj;
2803
2804	/*
2805	 * Rely on the use of open()-time protections to handle
2806	 * non-revocation cases.
2807	 */
2808	if (!biba_enabled || !revocation_enabled)
2809		return (0);
2810
2811	subj = SLOT(cred->cr_label);
2812	obj = SLOT(vplabel);
2813
2814	if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
2815		if (!biba_dominate_effective(obj, subj))
2816			return (EACCES);
2817	}
2818	if (((prot & VM_PROT_WRITE) != 0) && ((flags & MAP_SHARED) != 0)) {
2819		if (!biba_dominate_effective(subj, obj))
2820			return (EACCES);
2821	}
2822
2823	return (0);
2824}
2825
2826static int
2827biba_vnode_check_open(struct ucred *cred, struct vnode *vp,
2828    struct label *vplabel, int acc_mode)
2829{
2830	struct mac_biba *subj, *obj;
2831
2832	if (!biba_enabled)
2833		return (0);
2834
2835	subj = SLOT(cred->cr_label);
2836	obj = SLOT(vplabel);
2837
2838	/* XXX privilege override for admin? */
2839	if (acc_mode & (VREAD | VEXEC | VSTAT)) {
2840		if (!biba_dominate_effective(obj, subj))
2841			return (EACCES);
2842	}
2843	if (acc_mode & (VWRITE | VAPPEND | VADMIN)) {
2844		if (!biba_dominate_effective(subj, obj))
2845			return (EACCES);
2846	}
2847
2848	return (0);
2849}
2850
2851static int
2852biba_vnode_check_poll(struct ucred *active_cred, struct ucred *file_cred,
2853    struct vnode *vp, struct label *vplabel)
2854{
2855	struct mac_biba *subj, *obj;
2856
2857	if (!biba_enabled || !revocation_enabled)
2858		return (0);
2859
2860	subj = SLOT(active_cred->cr_label);
2861	obj = SLOT(vplabel);
2862
2863	if (!biba_dominate_effective(obj, subj))
2864		return (EACCES);
2865
2866	return (0);
2867}
2868
2869static int
2870biba_vnode_check_read(struct ucred *active_cred, struct ucred *file_cred,
2871    struct vnode *vp, struct label *vplabel)
2872{
2873	struct mac_biba *subj, *obj;
2874
2875	if (!biba_enabled || !revocation_enabled)
2876		return (0);
2877
2878	subj = SLOT(active_cred->cr_label);
2879	obj = SLOT(vplabel);
2880
2881	if (!biba_dominate_effective(obj, subj))
2882		return (EACCES);
2883
2884	return (0);
2885}
2886
2887static int
2888biba_vnode_check_readdir(struct ucred *cred, struct vnode *dvp,
2889    struct label *dvplabel)
2890{
2891	struct mac_biba *subj, *obj;
2892
2893	if (!biba_enabled)
2894		return (0);
2895
2896	subj = SLOT(cred->cr_label);
2897	obj = SLOT(dvplabel);
2898
2899	if (!biba_dominate_effective(obj, subj))
2900		return (EACCES);
2901
2902	return (0);
2903}
2904
2905static int
2906biba_vnode_check_readlink(struct ucred *cred, struct vnode *vp,
2907    struct label *vplabel)
2908{
2909	struct mac_biba *subj, *obj;
2910
2911	if (!biba_enabled)
2912		return (0);
2913
2914	subj = SLOT(cred->cr_label);
2915	obj = SLOT(vplabel);
2916
2917	if (!biba_dominate_effective(obj, subj))
2918		return (EACCES);
2919
2920	return (0);
2921}
2922
2923static int
2924biba_vnode_check_relabel(struct ucred *cred, struct vnode *vp,
2925    struct label *vplabel, struct label *newlabel)
2926{
2927	struct mac_biba *old, *new, *subj;
2928	int error;
2929
2930	old = SLOT(vplabel);
2931	new = SLOT(newlabel);
2932	subj = SLOT(cred->cr_label);
2933
2934	/*
2935	 * If there is a Biba label update for the vnode, it must be a
2936	 * effective label.
2937	 */
2938	error = biba_atmostflags(new, MAC_BIBA_FLAG_EFFECTIVE);
2939	if (error)
2940		return (error);
2941
2942	/*
2943	 * To perform a relabel of the vnode (Biba label or not), Biba must
2944	 * authorize the relabel.
2945	 */
2946	if (!biba_effective_in_range(old, subj))
2947		return (EPERM);
2948
2949	/*
2950	 * If the Biba label is to be changed, authorize as appropriate.
2951	 */
2952	if (new->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) {
2953		/*
2954		 * To change the Biba label on a vnode, the new vnode label
2955		 * must be in the subject range.
2956		 */
2957		if (!biba_effective_in_range(new, subj))
2958			return (EPERM);
2959
2960		/*
2961		 * To change the Biba label on the vnode to be EQUAL, the
2962		 * subject must have appropriate privilege.
2963		 */
2964		if (biba_contains_equal(new)) {
2965			error = biba_subject_privileged(subj);
2966			if (error)
2967				return (error);
2968		}
2969	}
2970
2971	return (0);
2972}
2973
2974static int
2975biba_vnode_check_rename_from(struct ucred *cred, struct vnode *dvp,
2976    struct label *dvplabel, struct vnode *vp, struct label *vplabel,
2977    struct componentname *cnp)
2978{
2979	struct mac_biba *subj, *obj;
2980
2981	if (!biba_enabled)
2982		return (0);
2983
2984	subj = SLOT(cred->cr_label);
2985	obj = SLOT(dvplabel);
2986
2987	if (!biba_dominate_effective(subj, obj))
2988		return (EACCES);
2989
2990	obj = SLOT(vplabel);
2991
2992	if (!biba_dominate_effective(subj, obj))
2993		return (EACCES);
2994
2995	return (0);
2996}
2997
2998static int
2999biba_vnode_check_rename_to(struct ucred *cred, struct vnode *dvp,
3000    struct label *dvplabel, struct vnode *vp, struct label *vplabel,
3001    int samedir, struct componentname *cnp)
3002{
3003	struct mac_biba *subj, *obj;
3004
3005	if (!biba_enabled)
3006		return (0);
3007
3008	subj = SLOT(cred->cr_label);
3009	obj = SLOT(dvplabel);
3010
3011	if (!biba_dominate_effective(subj, obj))
3012		return (EACCES);
3013
3014	if (vp != NULL) {
3015		obj = SLOT(vplabel);
3016
3017		if (!biba_dominate_effective(subj, obj))
3018			return (EACCES);
3019	}
3020
3021	return (0);
3022}
3023
3024static int
3025biba_vnode_check_revoke(struct ucred *cred, struct vnode *vp,
3026    struct label *vplabel)
3027{
3028	struct mac_biba *subj, *obj;
3029
3030	if (!biba_enabled)
3031		return (0);
3032
3033	subj = SLOT(cred->cr_label);
3034	obj = SLOT(vplabel);
3035
3036	if (!biba_dominate_effective(subj, obj))
3037		return (EACCES);
3038
3039	return (0);
3040}
3041
3042static int
3043biba_vnode_check_setacl(struct ucred *cred, struct vnode *vp,
3044    struct label *vplabel, acl_type_t type, struct acl *acl)
3045{
3046	struct mac_biba *subj, *obj;
3047
3048	if (!biba_enabled)
3049		return (0);
3050
3051	subj = SLOT(cred->cr_label);
3052	obj = SLOT(vplabel);
3053
3054	if (!biba_dominate_effective(subj, obj))
3055		return (EACCES);
3056
3057	return (0);
3058}
3059
3060static int
3061biba_vnode_check_setextattr(struct ucred *cred, struct vnode *vp,
3062    struct label *vplabel, int attrnamespace, const char *name,
3063    struct uio *uio)
3064{
3065	struct mac_biba *subj, *obj;
3066
3067	if (!biba_enabled)
3068		return (0);
3069
3070	subj = SLOT(cred->cr_label);
3071	obj = SLOT(vplabel);
3072
3073	if (!biba_dominate_effective(subj, obj))
3074		return (EACCES);
3075
3076	/* XXX: protect the MAC EA in a special way? */
3077
3078	return (0);
3079}
3080
3081static int
3082biba_vnode_check_setflags(struct ucred *cred, struct vnode *vp,
3083    struct label *vplabel, u_long flags)
3084{
3085	struct mac_biba *subj, *obj;
3086
3087	if (!biba_enabled)
3088		return (0);
3089
3090	subj = SLOT(cred->cr_label);
3091	obj = SLOT(vplabel);
3092
3093	if (!biba_dominate_effective(subj, obj))
3094		return (EACCES);
3095
3096	return (0);
3097}
3098
3099static int
3100biba_vnode_check_setmode(struct ucred *cred, struct vnode *vp,
3101    struct label *vplabel, mode_t mode)
3102{
3103	struct mac_biba *subj, *obj;
3104
3105	if (!biba_enabled)
3106		return (0);
3107
3108	subj = SLOT(cred->cr_label);
3109	obj = SLOT(vplabel);
3110
3111	if (!biba_dominate_effective(subj, obj))
3112		return (EACCES);
3113
3114	return (0);
3115}
3116
3117static int
3118biba_vnode_check_setowner(struct ucred *cred, struct vnode *vp,
3119    struct label *vplabel, uid_t uid, gid_t gid)
3120{
3121	struct mac_biba *subj, *obj;
3122
3123	if (!biba_enabled)
3124		return (0);
3125
3126	subj = SLOT(cred->cr_label);
3127	obj = SLOT(vplabel);
3128
3129	if (!biba_dominate_effective(subj, obj))
3130		return (EACCES);
3131
3132	return (0);
3133}
3134
3135static int
3136biba_vnode_check_setutimes(struct ucred *cred, struct vnode *vp,
3137    struct label *vplabel, struct timespec atime, struct timespec mtime)
3138{
3139	struct mac_biba *subj, *obj;
3140
3141	if (!biba_enabled)
3142		return (0);
3143
3144	subj = SLOT(cred->cr_label);
3145	obj = SLOT(vplabel);
3146
3147	if (!biba_dominate_effective(subj, obj))
3148		return (EACCES);
3149
3150	return (0);
3151}
3152
3153static int
3154biba_vnode_check_stat(struct ucred *active_cred, struct ucred *file_cred,
3155    struct vnode *vp, struct label *vplabel)
3156{
3157	struct mac_biba *subj, *obj;
3158
3159	if (!biba_enabled)
3160		return (0);
3161
3162	subj = SLOT(active_cred->cr_label);
3163	obj = SLOT(vplabel);
3164
3165	if (!biba_dominate_effective(obj, subj))
3166		return (EACCES);
3167
3168	return (0);
3169}
3170
3171static int
3172biba_vnode_check_unlink(struct ucred *cred, struct vnode *dvp,
3173    struct label *dvplabel, struct vnode *vp, struct label *vplabel,
3174    struct componentname *cnp)
3175{
3176	struct mac_biba *subj, *obj;
3177
3178	if (!biba_enabled)
3179		return (0);
3180
3181	subj = SLOT(cred->cr_label);
3182	obj = SLOT(dvplabel);
3183
3184	if (!biba_dominate_effective(subj, obj))
3185		return (EACCES);
3186
3187	obj = SLOT(vplabel);
3188
3189	if (!biba_dominate_effective(subj, obj))
3190		return (EACCES);
3191
3192	return (0);
3193}
3194
3195static int
3196biba_vnode_check_write(struct ucred *active_cred,
3197    struct ucred *file_cred, struct vnode *vp, struct label *vplabel)
3198{
3199	struct mac_biba *subj, *obj;
3200
3201	if (!biba_enabled || !revocation_enabled)
3202		return (0);
3203
3204	subj = SLOT(active_cred->cr_label);
3205	obj = SLOT(vplabel);
3206
3207	if (!biba_dominate_effective(subj, obj))
3208		return (EACCES);
3209
3210	return (0);
3211}
3212
3213static void
3214biba_syncache_create(struct label *label, struct inpcb *inp)
3215{
3216	struct mac_biba *source, *dest;
3217
3218	source = SLOT(inp->inp_label);
3219	dest = SLOT(label);
3220	biba_copy_effective(source, dest);
3221}
3222
3223static void
3224biba_syncache_create_mbuf(struct label *sc_label, struct mbuf *m,
3225    struct label *mlabel)
3226{
3227	struct mac_biba *source, *dest;
3228
3229	source = SLOT(sc_label);
3230	dest = SLOT(mlabel);
3231	biba_copy_effective(source, dest);
3232}
3233
3234static struct mac_policy_ops mac_biba_ops =
3235{
3236	.mpo_init = biba_init,
3237	.mpo_bpfdesc_init_label = biba_init_label,
3238	.mpo_cred_init_label = biba_init_label,
3239	.mpo_devfs_init_label = biba_init_label,
3240	.mpo_ifnet_init_label = biba_init_label,
3241	.mpo_inpcb_init_label = biba_init_label_waitcheck,
3242	.mpo_syncache_init_label = biba_init_label_waitcheck,
3243	.mpo_sysvmsg_init_label = biba_init_label,
3244	.mpo_sysvmsq_init_label = biba_init_label,
3245	.mpo_sysvsem_init_label = biba_init_label,
3246	.mpo_sysvshm_init_label = biba_init_label,
3247	.mpo_ipq_init_label = biba_init_label_waitcheck,
3248	.mpo_mbuf_init_label = biba_init_label_waitcheck,
3249	.mpo_mount_init_label = biba_init_label,
3250	.mpo_pipe_init_label = biba_init_label,
3251	.mpo_posixsem_init_label = biba_init_label,
3252	.mpo_socket_init_label = biba_init_label_waitcheck,
3253	.mpo_socketpeer_init_label = biba_init_label_waitcheck,
3254	.mpo_syncache_create = biba_syncache_create,
3255	.mpo_vnode_init_label = biba_init_label,
3256	.mpo_bpfdesc_destroy_label = biba_destroy_label,
3257	.mpo_cred_destroy_label = biba_destroy_label,
3258	.mpo_devfs_destroy_label = biba_destroy_label,
3259	.mpo_ifnet_destroy_label = biba_destroy_label,
3260	.mpo_inpcb_destroy_label = biba_destroy_label,
3261	.mpo_syncache_destroy_label = biba_destroy_label,
3262	.mpo_sysvmsg_destroy_label = biba_destroy_label,
3263	.mpo_sysvmsq_destroy_label = biba_destroy_label,
3264	.mpo_sysvsem_destroy_label = biba_destroy_label,
3265	.mpo_sysvshm_destroy_label = biba_destroy_label,
3266	.mpo_ipq_destroy_label = biba_destroy_label,
3267	.mpo_mbuf_destroy_label = biba_destroy_label,
3268	.mpo_mount_destroy_label = biba_destroy_label,
3269	.mpo_pipe_destroy_label = biba_destroy_label,
3270	.mpo_posixsem_destroy_label = biba_destroy_label,
3271	.mpo_socket_destroy_label = biba_destroy_label,
3272	.mpo_socketpeer_destroy_label = biba_destroy_label,
3273	.mpo_vnode_destroy_label = biba_destroy_label,
3274	.mpo_cred_copy_label = biba_copy_label,
3275	.mpo_ifnet_copy_label = biba_copy_label,
3276	.mpo_mbuf_copy_label = biba_copy_label,
3277	.mpo_pipe_copy_label = biba_copy_label,
3278	.mpo_socket_copy_label = biba_copy_label,
3279	.mpo_vnode_copy_label = biba_copy_label,
3280	.mpo_cred_externalize_label = biba_externalize_label,
3281	.mpo_ifnet_externalize_label = biba_externalize_label,
3282	.mpo_pipe_externalize_label = biba_externalize_label,
3283	.mpo_socket_externalize_label = biba_externalize_label,
3284	.mpo_socketpeer_externalize_label = biba_externalize_label,
3285	.mpo_vnode_externalize_label = biba_externalize_label,
3286	.mpo_cred_internalize_label = biba_internalize_label,
3287	.mpo_ifnet_internalize_label = biba_internalize_label,
3288	.mpo_pipe_internalize_label = biba_internalize_label,
3289	.mpo_socket_internalize_label = biba_internalize_label,
3290	.mpo_vnode_internalize_label = biba_internalize_label,
3291	.mpo_devfs_create_device = biba_devfs_create_device,
3292	.mpo_devfs_create_directory = biba_devfs_create_directory,
3293	.mpo_devfs_create_symlink = biba_devfs_create_symlink,
3294	.mpo_mount_create = biba_mount_create,
3295	.mpo_vnode_relabel = biba_vnode_relabel,
3296	.mpo_devfs_update = biba_devfs_update,
3297	.mpo_devfs_vnode_associate = biba_devfs_vnode_associate,
3298	.mpo_vnode_associate_extattr = biba_vnode_associate_extattr,
3299	.mpo_vnode_associate_singlelabel = biba_vnode_associate_singlelabel,
3300	.mpo_vnode_create_extattr = biba_vnode_create_extattr,
3301	.mpo_vnode_setlabel_extattr = biba_vnode_setlabel_extattr,
3302	.mpo_socket_create_mbuf = biba_socket_create_mbuf,
3303	.mpo_syncache_create_mbuf = biba_syncache_create_mbuf,
3304	.mpo_pipe_create = biba_pipe_create,
3305	.mpo_posixsem_create = biba_posixsem_create,
3306	.mpo_socket_create = biba_socket_create,
3307	.mpo_socket_newconn = biba_socket_newconn,
3308	.mpo_pipe_relabel = biba_pipe_relabel,
3309	.mpo_socket_relabel = biba_socket_relabel,
3310	.mpo_socketpeer_set_from_mbuf = biba_socketpeer_set_from_mbuf,
3311	.mpo_socketpeer_set_from_socket = biba_socketpeer_set_from_socket,
3312	.mpo_bpfdesc_create = biba_bpfdesc_create,
3313	.mpo_ipq_reassemble = biba_ipq_reassemble,
3314	.mpo_netinet_fragment = biba_netinet_fragment,
3315	.mpo_ifnet_create = biba_ifnet_create,
3316	.mpo_inpcb_create = biba_inpcb_create,
3317	.mpo_sysvmsg_create = biba_sysvmsg_create,
3318	.mpo_sysvmsq_create = biba_sysvmsq_create,
3319	.mpo_sysvsem_create = biba_sysvsem_create,
3320	.mpo_sysvshm_create = biba_sysvshm_create,
3321	.mpo_ipq_create = biba_ipq_create,
3322	.mpo_inpcb_create_mbuf = biba_inpcb_create_mbuf,
3323	.mpo_mbuf_create_linklayer = biba_mbuf_create_linklayer,
3324	.mpo_bpfdesc_create_mbuf = biba_bpfdesc_create_mbuf,
3325	.mpo_ifnet_create_mbuf = biba_ifnet_create_mbuf,
3326	.mpo_mbuf_create_multicast_encap = biba_mbuf_create_multicast_encap,
3327	.mpo_mbuf_create_netlayer = biba_mbuf_create_netlayer,
3328	.mpo_ipq_match = biba_ipq_match,
3329	.mpo_ifnet_relabel = biba_ifnet_relabel,
3330	.mpo_ipq_update = biba_ipq_update,
3331	.mpo_inpcb_sosetlabel = biba_inpcb_sosetlabel,
3332	.mpo_proc_create_swapper = biba_proc_create_swapper,
3333	.mpo_proc_create_init = biba_proc_create_init,
3334	.mpo_proc_associate_nfsd = biba_proc_associate_nfsd,
3335	.mpo_cred_relabel = biba_cred_relabel,
3336	.mpo_sysvmsg_cleanup = biba_sysvmsg_cleanup,
3337	.mpo_sysvmsq_cleanup = biba_sysvmsq_cleanup,
3338	.mpo_sysvsem_cleanup = biba_sysvsem_cleanup,
3339	.mpo_sysvshm_cleanup = biba_sysvshm_cleanup,
3340	.mpo_bpfdesc_check_receive = biba_bpfdesc_check_receive,
3341	.mpo_cred_check_relabel = biba_cred_check_relabel,
3342	.mpo_cred_check_visible = biba_cred_check_visible,
3343	.mpo_ifnet_check_relabel = biba_ifnet_check_relabel,
3344	.mpo_ifnet_check_transmit = biba_ifnet_check_transmit,
3345	.mpo_inpcb_check_deliver = biba_inpcb_check_deliver,
3346	.mpo_sysvmsq_check_msgrcv = biba_sysvmsq_check_msgrcv,
3347	.mpo_sysvmsq_check_msgrmid = biba_sysvmsq_check_msgrmid,
3348	.mpo_sysvmsq_check_msqget = biba_sysvmsq_check_msqget,
3349	.mpo_sysvmsq_check_msqsnd = biba_sysvmsq_check_msqsnd,
3350	.mpo_sysvmsq_check_msqrcv = biba_sysvmsq_check_msqrcv,
3351	.mpo_sysvmsq_check_msqctl = biba_sysvmsq_check_msqctl,
3352	.mpo_sysvsem_check_semctl = biba_sysvsem_check_semctl,
3353	.mpo_sysvsem_check_semget = biba_sysvsem_check_semget,
3354	.mpo_sysvsem_check_semop = biba_sysvsem_check_semop,
3355	.mpo_sysvshm_check_shmat = biba_sysvshm_check_shmat,
3356	.mpo_sysvshm_check_shmctl = biba_sysvshm_check_shmctl,
3357	.mpo_sysvshm_check_shmget = biba_sysvshm_check_shmget,
3358	.mpo_kld_check_load = biba_kld_check_load,
3359	.mpo_mount_check_stat = biba_mount_check_stat,
3360	.mpo_pipe_check_ioctl = biba_pipe_check_ioctl,
3361	.mpo_pipe_check_poll = biba_pipe_check_poll,
3362	.mpo_pipe_check_read = biba_pipe_check_read,
3363	.mpo_pipe_check_relabel = biba_pipe_check_relabel,
3364	.mpo_pipe_check_stat = biba_pipe_check_stat,
3365	.mpo_pipe_check_write = biba_pipe_check_write,
3366	.mpo_posixsem_check_destroy = biba_posixsem_check_write,
3367	.mpo_posixsem_check_getvalue = biba_posixsem_check_rdonly,
3368	.mpo_posixsem_check_open = biba_posixsem_check_write,
3369	.mpo_posixsem_check_post = biba_posixsem_check_write,
3370	.mpo_posixsem_check_unlink = biba_posixsem_check_write,
3371	.mpo_posixsem_check_wait = biba_posixsem_check_write,
3372	.mpo_proc_check_debug = biba_proc_check_debug,
3373	.mpo_proc_check_sched = biba_proc_check_sched,
3374	.mpo_proc_check_signal = biba_proc_check_signal,
3375	.mpo_socket_check_deliver = biba_socket_check_deliver,
3376	.mpo_socket_check_relabel = biba_socket_check_relabel,
3377	.mpo_socket_check_visible = biba_socket_check_visible,
3378	.mpo_system_check_acct = biba_system_check_acct,
3379	.mpo_system_check_auditctl = biba_system_check_auditctl,
3380	.mpo_system_check_auditon = biba_system_check_auditon,
3381	.mpo_system_check_swapon = biba_system_check_swapon,
3382	.mpo_system_check_swapoff = biba_system_check_swapoff,
3383	.mpo_system_check_sysctl = biba_system_check_sysctl,
3384	.mpo_vnode_check_access = biba_vnode_check_open,
3385	.mpo_vnode_check_chdir = biba_vnode_check_chdir,
3386	.mpo_vnode_check_chroot = biba_vnode_check_chroot,
3387	.mpo_vnode_check_create = biba_vnode_check_create,
3388	.mpo_vnode_check_deleteacl = biba_vnode_check_deleteacl,
3389	.mpo_vnode_check_deleteextattr = biba_vnode_check_deleteextattr,
3390	.mpo_vnode_check_exec = biba_vnode_check_exec,
3391	.mpo_vnode_check_getacl = biba_vnode_check_getacl,
3392	.mpo_vnode_check_getextattr = biba_vnode_check_getextattr,
3393	.mpo_vnode_check_link = biba_vnode_check_link,
3394	.mpo_vnode_check_listextattr = biba_vnode_check_listextattr,
3395	.mpo_vnode_check_lookup = biba_vnode_check_lookup,
3396	.mpo_vnode_check_mmap = biba_vnode_check_mmap,
3397	.mpo_vnode_check_open = biba_vnode_check_open,
3398	.mpo_vnode_check_poll = biba_vnode_check_poll,
3399	.mpo_vnode_check_read = biba_vnode_check_read,
3400	.mpo_vnode_check_readdir = biba_vnode_check_readdir,
3401	.mpo_vnode_check_readlink = biba_vnode_check_readlink,
3402	.mpo_vnode_check_relabel = biba_vnode_check_relabel,
3403	.mpo_vnode_check_rename_from = biba_vnode_check_rename_from,
3404	.mpo_vnode_check_rename_to = biba_vnode_check_rename_to,
3405	.mpo_vnode_check_revoke = biba_vnode_check_revoke,
3406	.mpo_vnode_check_setacl = biba_vnode_check_setacl,
3407	.mpo_vnode_check_setextattr = biba_vnode_check_setextattr,
3408	.mpo_vnode_check_setflags = biba_vnode_check_setflags,
3409	.mpo_vnode_check_setmode = biba_vnode_check_setmode,
3410	.mpo_vnode_check_setowner = biba_vnode_check_setowner,
3411	.mpo_vnode_check_setutimes = biba_vnode_check_setutimes,
3412	.mpo_vnode_check_stat = biba_vnode_check_stat,
3413	.mpo_vnode_check_unlink = biba_vnode_check_unlink,
3414	.mpo_vnode_check_write = biba_vnode_check_write,
3415	.mpo_netinet_firewall_send = biba_netinet_firewall_send,
3416	.mpo_priv_check = biba_priv_check,
3417};
3418
3419MAC_POLICY_SET(&mac_biba_ops, mac_biba, "TrustedBSD MAC/Biba",
3420    MPC_LOADTIME_FLAG_NOTLATE | MPC_LOADTIME_FLAG_LABELMBUFS, &biba_slot);
3421