mac_biba.c revision 115497
1/*-
2 * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson
3 * Copyright (c) 2001, 2002, 2003 Networks Associates Technology, Inc.
4 * All rights reserved.
5 *
6 * This software was developed by Robert Watson for the TrustedBSD Project.
7 *
8 * This software was developed for the FreeBSD Project in part by Network
9 * Associates Laboratories, the Security Research Division of Network
10 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
11 * as part of the DARPA CHATS research program.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
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.
21 *
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
32 * SUCH DAMAGE.
33 *
34 * $FreeBSD: head/sys/security/mac_biba/mac_biba.c 115497 2003-05-31 19:01:44Z rwatson $
35 */
36
37/*
38 * Developed by the TrustedBSD Project.
39 * Biba fixed label mandatory integrity policy.
40 */
41
42#include <sys/types.h>
43#include <sys/param.h>
44#include <sys/acl.h>
45#include <sys/conf.h>
46#include <sys/extattr.h>
47#include <sys/kernel.h>
48#include <sys/mac.h>
49#include <sys/malloc.h>
50#include <sys/mount.h>
51#include <sys/proc.h>
52#include <sys/sbuf.h>
53#include <sys/systm.h>
54#include <sys/sysproto.h>
55#include <sys/sysent.h>
56#include <sys/systm.h>
57#include <sys/vnode.h>
58#include <sys/file.h>
59#include <sys/socket.h>
60#include <sys/socketvar.h>
61#include <sys/pipe.h>
62#include <sys/sysctl.h>
63
64#include <fs/devfs/devfs.h>
65
66#include <net/bpfdesc.h>
67#include <net/if.h>
68#include <net/if_types.h>
69#include <net/if_var.h>
70
71#include <netinet/in.h>
72#include <netinet/ip_var.h>
73
74#include <vm/vm.h>
75
76#include <sys/mac_policy.h>
77
78#include <security/mac_biba/mac_biba.h>
79
80SYSCTL_DECL(_security_mac);
81
82SYSCTL_NODE(_security_mac, OID_AUTO, biba, CTLFLAG_RW, 0,
83    "TrustedBSD mac_biba policy controls");
84
85static int	mac_biba_label_size = sizeof(struct mac_biba);
86SYSCTL_INT(_security_mac_biba, OID_AUTO, label_size, CTLFLAG_RD,
87    &mac_biba_label_size, 0, "Size of struct mac_biba");
88
89static int	mac_biba_enabled = 1;
90SYSCTL_INT(_security_mac_biba, OID_AUTO, enabled, CTLFLAG_RW,
91    &mac_biba_enabled, 0, "Enforce MAC/Biba policy");
92TUNABLE_INT("security.mac.biba.enabled", &mac_biba_enabled);
93
94static int	destroyed_not_inited;
95SYSCTL_INT(_security_mac_biba, OID_AUTO, destroyed_not_inited, CTLFLAG_RD,
96    &destroyed_not_inited, 0, "Count of labels destroyed but not inited");
97
98static int	trust_all_interfaces = 0;
99SYSCTL_INT(_security_mac_biba, OID_AUTO, trust_all_interfaces, CTLFLAG_RD,
100    &trust_all_interfaces, 0, "Consider all interfaces 'trusted' by MAC/Biba");
101TUNABLE_INT("security.mac.biba.trust_all_interfaces", &trust_all_interfaces);
102
103static char	trusted_interfaces[128];
104SYSCTL_STRING(_security_mac_biba, OID_AUTO, trusted_interfaces, CTLFLAG_RD,
105    trusted_interfaces, 0, "Interfaces considered 'trusted' by MAC/Biba");
106TUNABLE_STR("security.mac.biba.trusted_interfaces", trusted_interfaces,
107    sizeof(trusted_interfaces));
108
109static int	max_compartments = MAC_BIBA_MAX_COMPARTMENTS;
110SYSCTL_INT(_security_mac_biba, OID_AUTO, max_compartments, CTLFLAG_RD,
111    &max_compartments, 0, "Maximum supported compartments");
112
113static int	ptys_equal = 0;
114SYSCTL_INT(_security_mac_biba, OID_AUTO, ptys_equal, CTLFLAG_RW,
115    &ptys_equal, 0, "Label pty devices as biba/equal on create");
116TUNABLE_INT("security.mac.biba.ptys_equal", &ptys_equal);
117
118static int	revocation_enabled = 0;
119SYSCTL_INT(_security_mac_biba, OID_AUTO, revocation_enabled, CTLFLAG_RW,
120    &revocation_enabled, 0, "Revoke access to objects on relabel");
121TUNABLE_INT("security.mac.biba.revocation_enabled", &revocation_enabled);
122
123static int	mac_biba_slot;
124#define	SLOT(l)	((struct mac_biba *)LABEL_TO_SLOT((l), mac_biba_slot).l_ptr)
125
126MALLOC_DEFINE(M_MACBIBA, "biba label", "MAC/Biba labels");
127
128static __inline int
129biba_bit_set_empty(u_char *set) {
130	int i;
131
132	for (i = 0; i < MAC_BIBA_MAX_COMPARTMENTS >> 3; i++)
133		if (set[i] != 0)
134			return (0);
135	return (1);
136}
137
138static struct mac_biba *
139biba_alloc(int flag)
140{
141	struct mac_biba *mac_biba;
142
143	mac_biba = malloc(sizeof(struct mac_biba), M_MACBIBA, M_ZERO | flag);
144
145	return (mac_biba);
146}
147
148static void
149biba_free(struct mac_biba *mac_biba)
150{
151
152	if (mac_biba != NULL)
153		free(mac_biba, M_MACBIBA);
154	else
155		atomic_add_int(&destroyed_not_inited, 1);
156}
157
158static int
159biba_atmostflags(struct mac_biba *mac_biba, int flags)
160{
161
162	if ((mac_biba->mb_flags & flags) != mac_biba->mb_flags)
163		return (EINVAL);
164	return (0);
165}
166
167static int
168mac_biba_dominate_element(struct mac_biba_element *a,
169    struct mac_biba_element *b)
170{
171	int bit;
172
173	switch (a->mbe_type) {
174	case MAC_BIBA_TYPE_EQUAL:
175	case MAC_BIBA_TYPE_HIGH:
176		return (1);
177
178	case MAC_BIBA_TYPE_LOW:
179		switch (b->mbe_type) {
180		case MAC_BIBA_TYPE_GRADE:
181		case MAC_BIBA_TYPE_HIGH:
182			return (0);
183
184		case MAC_BIBA_TYPE_EQUAL:
185		case MAC_BIBA_TYPE_LOW:
186			return (1);
187
188		default:
189			panic("mac_biba_dominate_element: b->mbe_type invalid");
190		}
191
192	case MAC_BIBA_TYPE_GRADE:
193		switch (b->mbe_type) {
194		case MAC_BIBA_TYPE_EQUAL:
195		case MAC_BIBA_TYPE_LOW:
196			return (1);
197
198		case MAC_BIBA_TYPE_HIGH:
199			return (0);
200
201		case MAC_BIBA_TYPE_GRADE:
202			for (bit = 1; bit <= MAC_BIBA_MAX_COMPARTMENTS; bit++)
203				if (!MAC_BIBA_BIT_TEST(bit,
204				    a->mbe_compartments) &&
205				    MAC_BIBA_BIT_TEST(bit, b->mbe_compartments))
206					return (0);
207			return (a->mbe_grade >= b->mbe_grade);
208
209		default:
210			panic("mac_biba_dominate_element: b->mbe_type invalid");
211		}
212
213	default:
214		panic("mac_biba_dominate_element: a->mbe_type invalid");
215	}
216
217	return (0);
218}
219
220static int
221mac_biba_subject_dominate_high(struct mac_biba *mac_biba)
222{
223	struct mac_biba_element *element;
224
225	KASSERT((mac_biba->mb_flags & MAC_BIBA_FLAG_SINGLE) != 0,
226	    ("mac_biba_single_in_range: mac_biba not single"));
227	element = &mac_biba->mb_single;
228
229	return (element->mbe_type == MAC_BIBA_TYPE_EQUAL ||
230	    element->mbe_type == MAC_BIBA_TYPE_HIGH);
231}
232
233static int
234mac_biba_range_in_range(struct mac_biba *rangea, struct mac_biba *rangeb)
235{
236
237	return (mac_biba_dominate_element(&rangeb->mb_rangehigh,
238	    &rangea->mb_rangehigh) &&
239	    mac_biba_dominate_element(&rangea->mb_rangelow,
240	    &rangeb->mb_rangelow));
241}
242
243static int
244mac_biba_single_in_range(struct mac_biba *single, struct mac_biba *range)
245{
246
247	KASSERT((single->mb_flags & MAC_BIBA_FLAG_SINGLE) != 0,
248	    ("mac_biba_single_in_range: a not single"));
249	KASSERT((range->mb_flags & MAC_BIBA_FLAG_RANGE) != 0,
250	    ("mac_biba_single_in_range: b not range"));
251
252	return (mac_biba_dominate_element(&range->mb_rangehigh,
253	    &single->mb_single) &&
254	    mac_biba_dominate_element(&single->mb_single,
255	    &range->mb_rangelow));
256
257	return (1);
258}
259
260static int
261mac_biba_dominate_single(struct mac_biba *a, struct mac_biba *b)
262{
263	KASSERT((a->mb_flags & MAC_BIBA_FLAG_SINGLE) != 0,
264	    ("mac_biba_dominate_single: a not single"));
265	KASSERT((b->mb_flags & MAC_BIBA_FLAG_SINGLE) != 0,
266	    ("mac_biba_dominate_single: b not single"));
267
268	return (mac_biba_dominate_element(&a->mb_single, &b->mb_single));
269}
270
271static int
272mac_biba_equal_element(struct mac_biba_element *a, struct mac_biba_element *b)
273{
274
275	if (a->mbe_type == MAC_BIBA_TYPE_EQUAL ||
276	    b->mbe_type == MAC_BIBA_TYPE_EQUAL)
277		return (1);
278
279	return (a->mbe_type == b->mbe_type && a->mbe_grade == b->mbe_grade);
280}
281
282static int
283mac_biba_equal_single(struct mac_biba *a, struct mac_biba *b)
284{
285
286	KASSERT((a->mb_flags & MAC_BIBA_FLAG_SINGLE) != 0,
287	    ("mac_biba_equal_single: a not single"));
288	KASSERT((b->mb_flags & MAC_BIBA_FLAG_SINGLE) != 0,
289	    ("mac_biba_equal_single: b not single"));
290
291	return (mac_biba_equal_element(&a->mb_single, &b->mb_single));
292}
293
294static int
295mac_biba_contains_equal(struct mac_biba *mac_biba)
296{
297
298	if (mac_biba->mb_flags & MAC_BIBA_FLAG_SINGLE)
299		if (mac_biba->mb_single.mbe_type == MAC_BIBA_TYPE_EQUAL)
300			return (1);
301
302	if (mac_biba->mb_flags & MAC_BIBA_FLAG_RANGE) {
303		if (mac_biba->mb_rangelow.mbe_type == MAC_BIBA_TYPE_EQUAL)
304			return (1);
305		if (mac_biba->mb_rangehigh.mbe_type == MAC_BIBA_TYPE_EQUAL)
306			return (1);
307	}
308
309	return (0);
310}
311
312static int
313mac_biba_subject_privileged(struct mac_biba *mac_biba)
314{
315
316	KASSERT((mac_biba->mb_flags & MAC_BIBA_FLAGS_BOTH) ==
317	    MAC_BIBA_FLAGS_BOTH,
318	    ("mac_biba_subject_privileged: subject doesn't have both labels"));
319
320	/* If the single is EQUAL, it's ok. */
321	if (mac_biba->mb_single.mbe_type == MAC_BIBA_TYPE_EQUAL)
322		return (0);
323
324	/* If either range endpoint is EQUAL, it's ok. */
325	if (mac_biba->mb_rangelow.mbe_type == MAC_BIBA_TYPE_EQUAL ||
326	    mac_biba->mb_rangehigh.mbe_type == MAC_BIBA_TYPE_EQUAL)
327		return (0);
328
329	/* If the range is low-high, it's ok. */
330	if (mac_biba->mb_rangelow.mbe_type == MAC_BIBA_TYPE_LOW &&
331	    mac_biba->mb_rangehigh.mbe_type == MAC_BIBA_TYPE_HIGH)
332		return (0);
333
334	/* It's not ok. */
335	return (EPERM);
336}
337
338static int
339mac_biba_high_single(struct mac_biba *mac_biba)
340{
341
342	KASSERT((mac_biba->mb_flags & MAC_BIBA_FLAG_SINGLE) != 0,
343	    ("mac_biba_equal_single: mac_biba not single"));
344
345	return (mac_biba->mb_single.mbe_type == MAC_BIBA_TYPE_HIGH);
346}
347
348static int
349mac_biba_valid(struct mac_biba *mac_biba)
350{
351
352	if (mac_biba->mb_flags & MAC_BIBA_FLAG_SINGLE) {
353		switch (mac_biba->mb_single.mbe_type) {
354		case MAC_BIBA_TYPE_GRADE:
355			break;
356
357		case MAC_BIBA_TYPE_EQUAL:
358		case MAC_BIBA_TYPE_HIGH:
359		case MAC_BIBA_TYPE_LOW:
360			if (mac_biba->mb_single.mbe_grade != 0 ||
361			    !MAC_BIBA_BIT_SET_EMPTY(
362			    mac_biba->mb_single.mbe_compartments))
363				return (EINVAL);
364			break;
365
366		default:
367			return (EINVAL);
368		}
369	} else {
370		if (mac_biba->mb_single.mbe_type != MAC_BIBA_TYPE_UNDEF)
371			return (EINVAL);
372	}
373
374	if (mac_biba->mb_flags & MAC_BIBA_FLAG_RANGE) {
375		switch (mac_biba->mb_rangelow.mbe_type) {
376		case MAC_BIBA_TYPE_GRADE:
377			break;
378
379		case MAC_BIBA_TYPE_EQUAL:
380		case MAC_BIBA_TYPE_HIGH:
381		case MAC_BIBA_TYPE_LOW:
382			if (mac_biba->mb_rangelow.mbe_grade != 0 ||
383			    !MAC_BIBA_BIT_SET_EMPTY(
384			    mac_biba->mb_rangelow.mbe_compartments))
385				return (EINVAL);
386			break;
387
388		default:
389			return (EINVAL);
390		}
391
392		switch (mac_biba->mb_rangehigh.mbe_type) {
393		case MAC_BIBA_TYPE_GRADE:
394			break;
395
396		case MAC_BIBA_TYPE_EQUAL:
397		case MAC_BIBA_TYPE_HIGH:
398		case MAC_BIBA_TYPE_LOW:
399			if (mac_biba->mb_rangehigh.mbe_grade != 0 ||
400			    !MAC_BIBA_BIT_SET_EMPTY(
401			    mac_biba->mb_rangehigh.mbe_compartments))
402				return (EINVAL);
403			break;
404
405		default:
406			return (EINVAL);
407		}
408		if (!mac_biba_dominate_element(&mac_biba->mb_rangehigh,
409		    &mac_biba->mb_rangelow))
410			return (EINVAL);
411	} else {
412		if (mac_biba->mb_rangelow.mbe_type != MAC_BIBA_TYPE_UNDEF ||
413		    mac_biba->mb_rangehigh.mbe_type != MAC_BIBA_TYPE_UNDEF)
414			return (EINVAL);
415	}
416
417	return (0);
418}
419
420static void
421mac_biba_set_range(struct mac_biba *mac_biba, u_short typelow,
422    u_short gradelow, u_char *compartmentslow, u_short typehigh,
423    u_short gradehigh, u_char *compartmentshigh)
424{
425
426	mac_biba->mb_rangelow.mbe_type = typelow;
427	mac_biba->mb_rangelow.mbe_grade = gradelow;
428	if (compartmentslow != NULL)
429		memcpy(mac_biba->mb_rangelow.mbe_compartments,
430		    compartmentslow,
431		    sizeof(mac_biba->mb_rangelow.mbe_compartments));
432	mac_biba->mb_rangehigh.mbe_type = typehigh;
433	mac_biba->mb_rangehigh.mbe_grade = gradehigh;
434	if (compartmentshigh != NULL)
435		memcpy(mac_biba->mb_rangehigh.mbe_compartments,
436		    compartmentshigh,
437		    sizeof(mac_biba->mb_rangehigh.mbe_compartments));
438	mac_biba->mb_flags |= MAC_BIBA_FLAG_RANGE;
439}
440
441static void
442mac_biba_set_single(struct mac_biba *mac_biba, u_short type, u_short grade,
443    u_char *compartments)
444{
445
446	mac_biba->mb_single.mbe_type = type;
447	mac_biba->mb_single.mbe_grade = grade;
448	if (compartments != NULL)
449		memcpy(mac_biba->mb_single.mbe_compartments, compartments,
450		    sizeof(mac_biba->mb_single.mbe_compartments));
451	mac_biba->mb_flags |= MAC_BIBA_FLAG_SINGLE;
452}
453
454static void
455mac_biba_copy_range(struct mac_biba *labelfrom, struct mac_biba *labelto)
456{
457
458	KASSERT((labelfrom->mb_flags & MAC_BIBA_FLAG_RANGE) != 0,
459	    ("mac_biba_copy_range: labelfrom not range"));
460
461	labelto->mb_rangelow = labelfrom->mb_rangelow;
462	labelto->mb_rangehigh = labelfrom->mb_rangehigh;
463	labelto->mb_flags |= MAC_BIBA_FLAG_RANGE;
464}
465
466static void
467mac_biba_copy_single(struct mac_biba *labelfrom, struct mac_biba *labelto)
468{
469
470	KASSERT((labelfrom->mb_flags & MAC_BIBA_FLAG_SINGLE) != 0,
471	    ("mac_biba_copy_single: labelfrom not single"));
472
473	labelto->mb_single = labelfrom->mb_single;
474	labelto->mb_flags |= MAC_BIBA_FLAG_SINGLE;
475}
476
477static void
478mac_biba_copy(struct mac_biba *source, struct mac_biba *dest)
479{
480
481	if (source->mb_flags & MAC_BIBA_FLAG_SINGLE)
482		mac_biba_copy_single(source, dest);
483	if (source->mb_flags & MAC_BIBA_FLAG_RANGE)
484		mac_biba_copy_range(source, dest);
485}
486
487/*
488 * Policy module operations.
489 */
490static void
491mac_biba_destroy(struct mac_policy_conf *conf)
492{
493
494}
495
496static void
497mac_biba_init(struct mac_policy_conf *conf)
498{
499
500}
501
502/*
503 * Label operations.
504 */
505static void
506mac_biba_init_label(struct label *label)
507{
508
509	SLOT(label) = biba_alloc(M_WAITOK);
510}
511
512static int
513mac_biba_init_label_waitcheck(struct label *label, int flag)
514{
515
516	SLOT(label) = biba_alloc(flag);
517	if (SLOT(label) == NULL)
518		return (ENOMEM);
519
520	return (0);
521}
522
523static void
524mac_biba_destroy_label(struct label *label)
525{
526
527	biba_free(SLOT(label));
528	SLOT(label) = NULL;
529}
530
531/*
532 * mac_biba_element_to_string() accepts an sbuf and Biba element.  It
533 * converts the Biba element to a string and stores the result in the
534 * sbuf; if there isn't space in the sbuf, -1 is returned.
535 */
536static int
537mac_biba_element_to_string(struct sbuf *sb, struct mac_biba_element *element)
538{
539	int i, first;
540
541	switch (element->mbe_type) {
542	case MAC_BIBA_TYPE_HIGH:
543		return (sbuf_printf(sb, "high"));
544
545	case MAC_BIBA_TYPE_LOW:
546		return (sbuf_printf(sb, "low"));
547
548	case MAC_BIBA_TYPE_EQUAL:
549		return (sbuf_printf(sb, "equal"));
550
551	case MAC_BIBA_TYPE_GRADE:
552		if (sbuf_printf(sb, "%d", element->mbe_grade) == -1)
553			return (-1);
554
555		first = 1;
556		for (i = 1; i <= MAC_BIBA_MAX_COMPARTMENTS; i++) {
557			if (MAC_BIBA_BIT_TEST(i, element->mbe_compartments)) {
558				if (first) {
559					if (sbuf_putc(sb, ':') == -1)
560						return (-1);
561					if (sbuf_printf(sb, "%d", i) == -1)
562						return (-1);
563					first = 0;
564				} else {
565					if (sbuf_printf(sb, "+%d", i) == -1)
566						return (-1);
567				}
568			}
569		}
570		return (0);
571
572	default:
573		panic("mac_biba_element_to_string: invalid type (%d)",
574		    element->mbe_type);
575	}
576}
577
578/*
579 * mac_biba_to_string() converts an Biba label to a string, placing the
580 * results in the passed string buffer.  It returns 0 on success,
581 * or EINVAL if there isn't room in the buffer.  The size of the
582 * string appended, leaving out the nul termination, is returned to
583 * the caller via *caller_len.  Eventually, we should expose the
584 * sbuf to the caller rather than using C strings at this layer.
585 */
586static int
587mac_biba_to_string(char *string, size_t size, size_t *caller_len,
588    struct mac_biba *mac_biba)
589{
590	struct sbuf sb;
591
592	sbuf_new(&sb, string, size, SBUF_FIXEDLEN);
593
594	if (mac_biba->mb_flags & MAC_BIBA_FLAG_SINGLE) {
595		if (mac_biba_element_to_string(&sb, &mac_biba->mb_single)
596		    == -1)
597			return (EINVAL);
598	}
599
600	if (mac_biba->mb_flags & MAC_BIBA_FLAG_RANGE) {
601		if (sbuf_putc(&sb, '(') == -1)
602			return (EINVAL);
603
604		if (mac_biba_element_to_string(&sb, &mac_biba->mb_rangelow)
605		    == -1)
606			return (EINVAL);
607
608		if (sbuf_putc(&sb, '-') == -1)
609			return (EINVAL);
610
611		if (mac_biba_element_to_string(&sb, &mac_biba->mb_rangehigh)
612		    == -1)
613			return (EINVAL);
614
615		if (sbuf_putc(&sb, ')') == -1)
616			return (EINVAL);
617	}
618
619	sbuf_finish(&sb);
620	*caller_len = strlen(string);
621	return (0);
622}
623
624static int
625mac_biba_externalize_label(struct label *label, char *element_name,
626    char *element_data, size_t size, size_t *len, int *claimed)
627{
628	struct mac_biba *mac_biba;
629	int error;
630
631	if (strcmp(MAC_BIBA_LABEL_NAME, element_name) != 0)
632		return (0);
633
634	(*claimed)++;
635
636	mac_biba = SLOT(label);
637	error = mac_biba_to_string(element_data, size, len, mac_biba);
638	if (error)
639		return (error);
640
641	return (0);
642}
643
644static int
645mac_biba_parse_element(struct mac_biba_element *element, char *string)
646{
647	char *compartment, *end, *grade;
648	int value;
649
650	if (strcmp(string, "high") == 0 ||
651	    strcmp(string, "hi") == 0) {
652		element->mbe_type = MAC_BIBA_TYPE_HIGH;
653		element->mbe_grade = MAC_BIBA_TYPE_UNDEF;
654	} else if (strcmp(string, "low") == 0 ||
655	    strcmp(string, "lo") == 0) {
656		element->mbe_type = MAC_BIBA_TYPE_LOW;
657		element->mbe_grade = MAC_BIBA_TYPE_UNDEF;
658	} else if (strcmp(string, "equal") == 0 ||
659	    strcmp(string, "eq") == 0) {
660		element->mbe_type = MAC_BIBA_TYPE_EQUAL;
661		element->mbe_grade = MAC_BIBA_TYPE_UNDEF;
662	} else {
663		element->mbe_type = MAC_BIBA_TYPE_GRADE;
664
665		/*
666		 * Numeric grade piece of the element.
667		 */
668		grade = strsep(&string, ":");
669		value = strtol(grade, &end, 10);
670		if (end == grade || *end != '\0')
671			return (EINVAL);
672		if (value < 0 || value > 65535)
673			return (EINVAL);
674		element->mbe_grade = value;
675
676		/*
677		 * Optional compartment piece of the element.  If none
678		 * are included, we assume that the label has no
679		 * compartments.
680		 */
681		if (string == NULL)
682			return (0);
683		if (*string == '\0')
684			return (0);
685
686		while ((compartment = strsep(&string, "+")) != NULL) {
687			value = strtol(compartment, &end, 10);
688			if (compartment == end || *end != '\0')
689				return (EINVAL);
690			if (value < 1 || value > MAC_BIBA_MAX_COMPARTMENTS)
691				return (EINVAL);
692			MAC_BIBA_BIT_SET(value, element->mbe_compartments);
693		}
694	}
695
696	return (0);
697}
698
699/*
700 * Note: destructively consumes the string, make a local copy before
701 * calling if that's a problem.
702 */
703static int
704mac_biba_parse(struct mac_biba *mac_biba, char *string)
705{
706	char *rangehigh, *rangelow, *single;
707	int error;
708
709	single = strsep(&string, "(");
710	if (*single == '\0')
711		single = NULL;
712
713	if (string != NULL) {
714		rangelow = strsep(&string, "-");
715		if (string == NULL)
716			return (EINVAL);
717		rangehigh = strsep(&string, ")");
718		if (string == NULL)
719			return (EINVAL);
720		if (*string != '\0')
721			return (EINVAL);
722	} else {
723		rangelow = NULL;
724		rangehigh = NULL;
725	}
726
727	KASSERT((rangelow != NULL && rangehigh != NULL) ||
728	    (rangelow == NULL && rangehigh == NULL),
729	    ("mac_biba_parse: range mismatch"));
730
731	bzero(mac_biba, sizeof(*mac_biba));
732	if (single != NULL) {
733		error = mac_biba_parse_element(&mac_biba->mb_single, single);
734		if (error)
735			return (error);
736		mac_biba->mb_flags |= MAC_BIBA_FLAG_SINGLE;
737	}
738
739	if (rangelow != NULL) {
740		error = mac_biba_parse_element(&mac_biba->mb_rangelow,
741		    rangelow);
742		if (error)
743			return (error);
744		error = mac_biba_parse_element(&mac_biba->mb_rangehigh,
745		    rangehigh);
746		if (error)
747			return (error);
748		mac_biba->mb_flags |= MAC_BIBA_FLAG_RANGE;
749	}
750
751	error = mac_biba_valid(mac_biba);
752	if (error)
753		return (error);
754
755	return (0);
756}
757
758static int
759mac_biba_internalize_label(struct label *label, char *element_name,
760    char *element_data, int *claimed)
761{
762	struct mac_biba *mac_biba, mac_biba_temp;
763	int error;
764
765	if (strcmp(MAC_BIBA_LABEL_NAME, element_name) != 0)
766		return (0);
767
768	(*claimed)++;
769
770	error = mac_biba_parse(&mac_biba_temp, element_data);
771	if (error)
772		return (error);
773
774	mac_biba = SLOT(label);
775	*mac_biba = mac_biba_temp;
776
777	return (0);
778}
779
780static void
781mac_biba_copy_label(struct label *src, struct label *dest)
782{
783
784	*SLOT(dest) = *SLOT(src);
785}
786
787/*
788 * Labeling event operations: file system objects, and things that look
789 * a lot like file system objects.
790 */
791static void
792mac_biba_create_devfs_device(struct mount *mp, dev_t dev,
793    struct devfs_dirent *devfs_dirent, struct label *label)
794{
795	struct mac_biba *mac_biba;
796	int biba_type;
797
798	mac_biba = SLOT(label);
799	if (strcmp(dev->si_name, "null") == 0 ||
800	    strcmp(dev->si_name, "zero") == 0 ||
801	    strcmp(dev->si_name, "random") == 0 ||
802	    strncmp(dev->si_name, "fd/", strlen("fd/")) == 0)
803		biba_type = MAC_BIBA_TYPE_EQUAL;
804	else if (ptys_equal &&
805	    (strncmp(dev->si_name, "ttyp", strlen("ttyp")) == 0 ||
806	    strncmp(dev->si_name, "ptyp", strlen("ptyp")) == 0))
807		biba_type = MAC_BIBA_TYPE_EQUAL;
808	else
809		biba_type = MAC_BIBA_TYPE_HIGH;
810	mac_biba_set_single(mac_biba, biba_type, 0, NULL);
811}
812
813static void
814mac_biba_create_devfs_directory(struct mount *mp, char *dirname,
815    int dirnamelen, struct devfs_dirent *devfs_dirent, struct label *label)
816{
817	struct mac_biba *mac_biba;
818
819	mac_biba = SLOT(label);
820	mac_biba_set_single(mac_biba, MAC_BIBA_TYPE_HIGH, 0, NULL);
821}
822
823static void
824mac_biba_create_devfs_symlink(struct ucred *cred, struct mount *mp,
825    struct devfs_dirent *dd, struct label *ddlabel, struct devfs_dirent *de,
826    struct label *delabel)
827{
828	struct mac_biba *source, *dest;
829
830	source = SLOT(&cred->cr_label);
831	dest = SLOT(delabel);
832
833	mac_biba_copy_single(source, dest);
834}
835
836static void
837mac_biba_create_mount(struct ucred *cred, struct mount *mp,
838    struct label *mntlabel, struct label *fslabel)
839{
840	struct mac_biba *source, *dest;
841
842	source = SLOT(&cred->cr_label);
843	dest = SLOT(mntlabel);
844	mac_biba_copy_single(source, dest);
845	dest = SLOT(fslabel);
846	mac_biba_copy_single(source, dest);
847}
848
849static void
850mac_biba_create_root_mount(struct ucred *cred, struct mount *mp,
851    struct label *mntlabel, struct label *fslabel)
852{
853	struct mac_biba *mac_biba;
854
855	/* Always mount root as high integrity. */
856	mac_biba = SLOT(fslabel);
857	mac_biba_set_single(mac_biba, MAC_BIBA_TYPE_HIGH, 0, NULL);
858	mac_biba = SLOT(mntlabel);
859	mac_biba_set_single(mac_biba, MAC_BIBA_TYPE_HIGH, 0, NULL);
860}
861
862static void
863mac_biba_relabel_vnode(struct ucred *cred, struct vnode *vp,
864    struct label *vnodelabel, struct label *label)
865{
866	struct mac_biba *source, *dest;
867
868	source = SLOT(label);
869	dest = SLOT(vnodelabel);
870
871	mac_biba_copy(source, dest);
872}
873
874static void
875mac_biba_update_devfsdirent(struct mount *mp,
876    struct devfs_dirent *devfs_dirent, struct label *direntlabel,
877    struct vnode *vp, struct label *vnodelabel)
878{
879	struct mac_biba *source, *dest;
880
881	source = SLOT(vnodelabel);
882	dest = SLOT(direntlabel);
883
884	mac_biba_copy(source, dest);
885}
886
887static void
888mac_biba_associate_vnode_devfs(struct mount *mp, struct label *fslabel,
889    struct devfs_dirent *de, struct label *delabel, struct vnode *vp,
890    struct label *vlabel)
891{
892	struct mac_biba *source, *dest;
893
894	source = SLOT(delabel);
895	dest = SLOT(vlabel);
896
897	mac_biba_copy_single(source, dest);
898}
899
900static int
901mac_biba_associate_vnode_extattr(struct mount *mp, struct label *fslabel,
902    struct vnode *vp, struct label *vlabel)
903{
904	struct mac_biba temp, *source, *dest;
905	int buflen, error;
906
907	source = SLOT(fslabel);
908	dest = SLOT(vlabel);
909
910	buflen = sizeof(temp);
911	bzero(&temp, buflen);
912
913	error = vn_extattr_get(vp, IO_NODELOCKED, MAC_BIBA_EXTATTR_NAMESPACE,
914	    MAC_BIBA_EXTATTR_NAME, &buflen, (char *) &temp, curthread);
915	if (error == ENOATTR || error == EOPNOTSUPP) {
916		/* Fall back to the fslabel. */
917		mac_biba_copy_single(source, dest);
918		return (0);
919	} else if (error)
920		return (error);
921
922	if (buflen != sizeof(temp)) {
923		printf("mac_biba_associate_vnode_extattr: bad size %d\n",
924		    buflen);
925		return (EPERM);
926	}
927	if (mac_biba_valid(&temp) != 0) {
928		printf("mac_biba_associate_vnode_extattr: invalid\n");
929		return (EPERM);
930	}
931	if ((temp.mb_flags & MAC_BIBA_FLAGS_BOTH) != MAC_BIBA_FLAG_SINGLE) {
932		printf("mac_biba_associate_vnode_extattr: not single\n");
933		return (EPERM);
934	}
935
936	mac_biba_copy_single(&temp, dest);
937	return (0);
938}
939
940static void
941mac_biba_associate_vnode_singlelabel(struct mount *mp,
942    struct label *fslabel, struct vnode *vp, struct label *vlabel)
943{
944	struct mac_biba *source, *dest;
945
946	source = SLOT(fslabel);
947	dest = SLOT(vlabel);
948
949	mac_biba_copy_single(source, dest);
950}
951
952static int
953mac_biba_create_vnode_extattr(struct ucred *cred, struct mount *mp,
954    struct label *fslabel, struct vnode *dvp, struct label *dlabel,
955    struct vnode *vp, struct label *vlabel, struct componentname *cnp)
956{
957	struct mac_biba *source, *dest, temp;
958	size_t buflen;
959	int error;
960
961	buflen = sizeof(temp);
962	bzero(&temp, buflen);
963
964	source = SLOT(&cred->cr_label);
965	dest = SLOT(vlabel);
966	mac_biba_copy_single(source, &temp);
967
968	error = vn_extattr_set(vp, IO_NODELOCKED, MAC_BIBA_EXTATTR_NAMESPACE,
969	    MAC_BIBA_EXTATTR_NAME, buflen, (char *) &temp, curthread);
970	if (error == 0)
971		mac_biba_copy_single(source, dest);
972	return (error);
973}
974
975static int
976mac_biba_setlabel_vnode_extattr(struct ucred *cred, struct vnode *vp,
977    struct label *vlabel, struct label *intlabel)
978{
979	struct mac_biba *source, temp;
980	size_t buflen;
981	int error;
982
983	buflen = sizeof(temp);
984	bzero(&temp, buflen);
985
986	source = SLOT(intlabel);
987	if ((source->mb_flags & MAC_BIBA_FLAG_SINGLE) == 0)
988		return (0);
989
990	mac_biba_copy_single(source, &temp);
991
992	error = vn_extattr_set(vp, IO_NODELOCKED, MAC_BIBA_EXTATTR_NAMESPACE,
993	    MAC_BIBA_EXTATTR_NAME, buflen, (char *) &temp, curthread);
994	return (error);
995}
996
997/*
998 * Labeling event operations: IPC object.
999 */
1000static void
1001mac_biba_create_mbuf_from_socket(struct socket *so, struct label *socketlabel,
1002    struct mbuf *m, struct label *mbuflabel)
1003{
1004	struct mac_biba *source, *dest;
1005
1006	source = SLOT(socketlabel);
1007	dest = SLOT(mbuflabel);
1008
1009	mac_biba_copy_single(source, dest);
1010}
1011
1012static void
1013mac_biba_create_socket(struct ucred *cred, struct socket *socket,
1014    struct label *socketlabel)
1015{
1016	struct mac_biba *source, *dest;
1017
1018	source = SLOT(&cred->cr_label);
1019	dest = SLOT(socketlabel);
1020
1021	mac_biba_copy_single(source, dest);
1022}
1023
1024static void
1025mac_biba_create_pipe(struct ucred *cred, struct pipe *pipe,
1026    struct label *pipelabel)
1027{
1028	struct mac_biba *source, *dest;
1029
1030	source = SLOT(&cred->cr_label);
1031	dest = SLOT(pipelabel);
1032
1033	mac_biba_copy_single(source, dest);
1034}
1035
1036static void
1037mac_biba_create_socket_from_socket(struct socket *oldsocket,
1038    struct label *oldsocketlabel, struct socket *newsocket,
1039    struct label *newsocketlabel)
1040{
1041	struct mac_biba *source, *dest;
1042
1043	source = SLOT(oldsocketlabel);
1044	dest = SLOT(newsocketlabel);
1045
1046	mac_biba_copy_single(source, dest);
1047}
1048
1049static void
1050mac_biba_relabel_socket(struct ucred *cred, struct socket *socket,
1051    struct label *socketlabel, struct label *newlabel)
1052{
1053	struct mac_biba *source, *dest;
1054
1055	source = SLOT(newlabel);
1056	dest = SLOT(socketlabel);
1057
1058	mac_biba_copy(source, dest);
1059}
1060
1061static void
1062mac_biba_relabel_pipe(struct ucred *cred, struct pipe *pipe,
1063    struct label *pipelabel, struct label *newlabel)
1064{
1065	struct mac_biba *source, *dest;
1066
1067	source = SLOT(newlabel);
1068	dest = SLOT(pipelabel);
1069
1070	mac_biba_copy(source, dest);
1071}
1072
1073static void
1074mac_biba_set_socket_peer_from_mbuf(struct mbuf *mbuf, struct label *mbuflabel,
1075    struct socket *socket, struct label *socketpeerlabel)
1076{
1077	struct mac_biba *source, *dest;
1078
1079	source = SLOT(mbuflabel);
1080	dest = SLOT(socketpeerlabel);
1081
1082	mac_biba_copy_single(source, dest);
1083}
1084
1085/*
1086 * Labeling event operations: network objects.
1087 */
1088static void
1089mac_biba_set_socket_peer_from_socket(struct socket *oldsocket,
1090    struct label *oldsocketlabel, struct socket *newsocket,
1091    struct label *newsocketpeerlabel)
1092{
1093	struct mac_biba *source, *dest;
1094
1095	source = SLOT(oldsocketlabel);
1096	dest = SLOT(newsocketpeerlabel);
1097
1098	mac_biba_copy_single(source, dest);
1099}
1100
1101static void
1102mac_biba_create_bpfdesc(struct ucred *cred, struct bpf_d *bpf_d,
1103    struct label *bpflabel)
1104{
1105	struct mac_biba *source, *dest;
1106
1107	source = SLOT(&cred->cr_label);
1108	dest = SLOT(bpflabel);
1109
1110	mac_biba_copy_single(source, dest);
1111}
1112
1113static void
1114mac_biba_create_ifnet(struct ifnet *ifnet, struct label *ifnetlabel)
1115{
1116	char tifname[IFNAMSIZ], ifname[IFNAMSIZ], *p, *q;
1117	char tiflist[sizeof(trusted_interfaces)];
1118	struct mac_biba *dest;
1119	int len, type;
1120
1121	dest = SLOT(ifnetlabel);
1122
1123	if (ifnet->if_type == IFT_LOOP) {
1124		type = MAC_BIBA_TYPE_EQUAL;
1125		goto set;
1126	}
1127
1128	if (trust_all_interfaces) {
1129		type = MAC_BIBA_TYPE_HIGH;
1130		goto set;
1131	}
1132
1133	type = MAC_BIBA_TYPE_LOW;
1134
1135	if (trusted_interfaces[0] == '\0' ||
1136	    !strvalid(trusted_interfaces, sizeof(trusted_interfaces)))
1137		goto set;
1138
1139	bzero(tiflist, sizeof(tiflist));
1140	for (p = trusted_interfaces, q = tiflist; *p != '\0'; p++, q++)
1141		if(*p != ' ' && *p != '\t')
1142			*q = *p;
1143
1144	snprintf(ifname, IFNAMSIZ, "%s%d", ifnet->if_name, ifnet->if_unit);
1145
1146	for (p = q = tiflist;; p++) {
1147		if (*p == ',' || *p == '\0') {
1148			len = p - q;
1149			if (len < IFNAMSIZ) {
1150				bzero(tifname, sizeof(tifname));
1151				bcopy(q, tifname, len);
1152				if (strcmp(tifname, ifname) == 0) {
1153					type = MAC_BIBA_TYPE_HIGH;
1154					break;
1155				}
1156			} else {
1157				*p = '\0';
1158				printf("mac_biba warning: interface name "
1159				    "\"%s\" is too long (must be < %d)\n",
1160				    q, IFNAMSIZ);
1161			}
1162			if (*p == '\0')
1163				break;
1164			q = p + 1;
1165		}
1166	}
1167set:
1168	mac_biba_set_single(dest, type, 0, NULL);
1169	mac_biba_set_range(dest, type, 0, NULL, type, 0, NULL);
1170}
1171
1172static void
1173mac_biba_create_ipq(struct mbuf *fragment, struct label *fragmentlabel,
1174    struct ipq *ipq, struct label *ipqlabel)
1175{
1176	struct mac_biba *source, *dest;
1177
1178	source = SLOT(fragmentlabel);
1179	dest = SLOT(ipqlabel);
1180
1181	mac_biba_copy_single(source, dest);
1182}
1183
1184static void
1185mac_biba_create_datagram_from_ipq(struct ipq *ipq, struct label *ipqlabel,
1186    struct mbuf *datagram, struct label *datagramlabel)
1187{
1188	struct mac_biba *source, *dest;
1189
1190	source = SLOT(ipqlabel);
1191	dest = SLOT(datagramlabel);
1192
1193	/* Just use the head, since we require them all to match. */
1194	mac_biba_copy_single(source, dest);
1195}
1196
1197static void
1198mac_biba_create_fragment(struct mbuf *datagram, struct label *datagramlabel,
1199    struct mbuf *fragment, struct label *fragmentlabel)
1200{
1201	struct mac_biba *source, *dest;
1202
1203	source = SLOT(datagramlabel);
1204	dest = SLOT(fragmentlabel);
1205
1206	mac_biba_copy_single(source, dest);
1207}
1208
1209static void
1210mac_biba_create_mbuf_from_mbuf(struct mbuf *oldmbuf,
1211    struct label *oldmbuflabel, struct mbuf *newmbuf,
1212    struct label *newmbuflabel)
1213{
1214	struct mac_biba *source, *dest;
1215
1216	source = SLOT(oldmbuflabel);
1217	dest = SLOT(newmbuflabel);
1218
1219	/*
1220	 * Because the source mbuf may not yet have been "created",
1221	 * just initialized, we do a conditional copy.  Since we don't
1222	 * allow mbufs to have ranges, do a KASSERT to make sure that
1223	 * doesn't happen.
1224	 */
1225	KASSERT((source->mb_flags & MAC_BIBA_FLAG_RANGE) == 0,
1226	    ("mac_biba_create_mbuf_from_mbuf: source mbuf has range"));
1227	mac_biba_copy(source, dest);
1228}
1229
1230static void
1231mac_biba_create_mbuf_linklayer(struct ifnet *ifnet, struct label *ifnetlabel,
1232    struct mbuf *mbuf, struct label *mbuflabel)
1233{
1234	struct mac_biba *dest;
1235
1236	dest = SLOT(mbuflabel);
1237
1238	mac_biba_set_single(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
1239}
1240
1241static void
1242mac_biba_create_mbuf_from_bpfdesc(struct bpf_d *bpf_d, struct label *bpflabel,
1243    struct mbuf *mbuf, struct label *mbuflabel)
1244{
1245	struct mac_biba *source, *dest;
1246
1247	source = SLOT(bpflabel);
1248	dest = SLOT(mbuflabel);
1249
1250	mac_biba_copy_single(source, dest);
1251}
1252
1253static void
1254mac_biba_create_mbuf_from_ifnet(struct ifnet *ifnet, struct label *ifnetlabel,
1255    struct mbuf *m, struct label *mbuflabel)
1256{
1257	struct mac_biba *source, *dest;
1258
1259	source = SLOT(ifnetlabel);
1260	dest = SLOT(mbuflabel);
1261
1262	mac_biba_copy_single(source, dest);
1263}
1264
1265static void
1266mac_biba_create_mbuf_multicast_encap(struct mbuf *oldmbuf,
1267    struct label *oldmbuflabel, struct ifnet *ifnet, struct label *ifnetlabel,
1268    struct mbuf *newmbuf, struct label *newmbuflabel)
1269{
1270	struct mac_biba *source, *dest;
1271
1272	source = SLOT(oldmbuflabel);
1273	dest = SLOT(newmbuflabel);
1274
1275	mac_biba_copy_single(source, dest);
1276}
1277
1278static void
1279mac_biba_create_mbuf_netlayer(struct mbuf *oldmbuf, struct label *oldmbuflabel,
1280    struct mbuf *newmbuf, struct label *newmbuflabel)
1281{
1282	struct mac_biba *source, *dest;
1283
1284	source = SLOT(oldmbuflabel);
1285	dest = SLOT(newmbuflabel);
1286
1287	mac_biba_copy_single(source, dest);
1288}
1289
1290static int
1291mac_biba_fragment_match(struct mbuf *fragment, struct label *fragmentlabel,
1292    struct ipq *ipq, struct label *ipqlabel)
1293{
1294	struct mac_biba *a, *b;
1295
1296	a = SLOT(ipqlabel);
1297	b = SLOT(fragmentlabel);
1298
1299	return (mac_biba_equal_single(a, b));
1300}
1301
1302static void
1303mac_biba_relabel_ifnet(struct ucred *cred, struct ifnet *ifnet,
1304    struct label *ifnetlabel, struct label *newlabel)
1305{
1306	struct mac_biba *source, *dest;
1307
1308	source = SLOT(newlabel);
1309	dest = SLOT(ifnetlabel);
1310
1311	mac_biba_copy(source, dest);
1312}
1313
1314static void
1315mac_biba_update_ipq(struct mbuf *fragment, struct label *fragmentlabel,
1316    struct ipq *ipq, struct label *ipqlabel)
1317{
1318
1319	/* NOOP: we only accept matching labels, so no need to update */
1320}
1321
1322/*
1323 * Labeling event operations: processes.
1324 */
1325static void
1326mac_biba_create_cred(struct ucred *cred_parent, struct ucred *cred_child)
1327{
1328	struct mac_biba *source, *dest;
1329
1330	source = SLOT(&cred_parent->cr_label);
1331	dest = SLOT(&cred_child->cr_label);
1332
1333	mac_biba_copy_single(source, dest);
1334	mac_biba_copy_range(source, dest);
1335}
1336
1337static void
1338mac_biba_create_proc0(struct ucred *cred)
1339{
1340	struct mac_biba *dest;
1341
1342	dest = SLOT(&cred->cr_label);
1343
1344	mac_biba_set_single(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
1345	mac_biba_set_range(dest, MAC_BIBA_TYPE_LOW, 0, NULL,
1346	    MAC_BIBA_TYPE_HIGH, 0, NULL);
1347}
1348
1349static void
1350mac_biba_create_proc1(struct ucred *cred)
1351{
1352	struct mac_biba *dest;
1353
1354	dest = SLOT(&cred->cr_label);
1355
1356	mac_biba_set_single(dest, MAC_BIBA_TYPE_HIGH, 0, NULL);
1357	mac_biba_set_range(dest, MAC_BIBA_TYPE_LOW, 0, NULL,
1358	    MAC_BIBA_TYPE_HIGH, 0, NULL);
1359}
1360
1361static void
1362mac_biba_relabel_cred(struct ucred *cred, struct label *newlabel)
1363{
1364	struct mac_biba *source, *dest;
1365
1366	source = SLOT(newlabel);
1367	dest = SLOT(&cred->cr_label);
1368
1369	mac_biba_copy(source, dest);
1370}
1371
1372/*
1373 * Access control checks.
1374 */
1375static int
1376mac_biba_check_bpfdesc_receive(struct bpf_d *bpf_d, struct label *bpflabel,
1377    struct ifnet *ifnet, struct label *ifnetlabel)
1378{
1379	struct mac_biba *a, *b;
1380
1381	if (!mac_biba_enabled)
1382		return (0);
1383
1384	a = SLOT(bpflabel);
1385	b = SLOT(ifnetlabel);
1386
1387	if (mac_biba_equal_single(a, b))
1388		return (0);
1389	return (EACCES);
1390}
1391
1392static int
1393mac_biba_check_cred_relabel(struct ucred *cred, struct label *newlabel)
1394{
1395	struct mac_biba *subj, *new;
1396	int error;
1397
1398	subj = SLOT(&cred->cr_label);
1399	new = SLOT(newlabel);
1400
1401	/*
1402	 * If there is a Biba label update for the credential, it may
1403	 * be an update of the single, range, or both.
1404	 */
1405	error = biba_atmostflags(new, MAC_BIBA_FLAGS_BOTH);
1406	if (error)
1407		return (error);
1408
1409	/*
1410	 * If the Biba label is to be changed, authorize as appropriate.
1411	 */
1412	if (new->mb_flags & MAC_BIBA_FLAGS_BOTH) {
1413		/*
1414		 * If the change request modifies both the Biba label
1415		 * single and range, check that the new single will be
1416		 * in the new range.
1417		 */
1418		if ((new->mb_flags & MAC_BIBA_FLAGS_BOTH) ==
1419		    MAC_BIBA_FLAGS_BOTH &&
1420		    !mac_biba_single_in_range(new, new))
1421			return (EINVAL);
1422
1423		/*
1424		 * To change the Biba single label on a credential, the
1425		 * new single label must be in the current range.
1426		 */
1427		if (new->mb_flags & MAC_BIBA_FLAG_SINGLE &&
1428		    !mac_biba_single_in_range(new, subj))
1429			return (EPERM);
1430
1431		/*
1432		 * To change the Biba range on a credential, the new
1433		 * range label must be in the current range.
1434		 */
1435		if (new->mb_flags & MAC_BIBA_FLAG_RANGE &&
1436		    !mac_biba_range_in_range(new, subj))
1437			return (EPERM);
1438
1439		/*
1440		 * To have EQUAL in any component of the new credential
1441		 * Biba label, the subject must already have EQUAL in
1442		 * their label.
1443		 */
1444		if (mac_biba_contains_equal(new)) {
1445			error = mac_biba_subject_privileged(subj);
1446			if (error)
1447				return (error);
1448		}
1449	}
1450
1451	return (0);
1452}
1453
1454static int
1455mac_biba_check_cred_visible(struct ucred *u1, struct ucred *u2)
1456{
1457	struct mac_biba *subj, *obj;
1458
1459	if (!mac_biba_enabled)
1460		return (0);
1461
1462	subj = SLOT(&u1->cr_label);
1463	obj = SLOT(&u2->cr_label);
1464
1465	/* XXX: range */
1466	if (!mac_biba_dominate_single(obj, subj))
1467		return (ESRCH);
1468
1469	return (0);
1470}
1471
1472static int
1473mac_biba_check_ifnet_relabel(struct ucred *cred, struct ifnet *ifnet,
1474    struct label *ifnetlabel, struct label *newlabel)
1475{
1476	struct mac_biba *subj, *new;
1477	int error;
1478
1479	subj = SLOT(&cred->cr_label);
1480	new = SLOT(newlabel);
1481
1482	/*
1483	 * If there is a Biba label update for the interface, it may
1484	 * be an update of the single, range, or both.
1485	 */
1486	error = biba_atmostflags(new, MAC_BIBA_FLAGS_BOTH);
1487	if (error)
1488		return (error);
1489
1490	/*
1491	 * Relabling network interfaces requires Biba privilege.
1492	 */
1493	error = mac_biba_subject_privileged(subj);
1494	if (error)
1495		return (error);
1496
1497	/*
1498	 * If the Biba label is to be changed, authorize as appropriate.
1499	 */
1500	if (new->mb_flags & MAC_BIBA_FLAGS_BOTH) {
1501		/*
1502		 * Rely on the traditional superuser status for the Biba
1503		 * interface relabel requirements.  XXXMAC: This will go
1504		 * away.
1505		 */
1506		error = suser_cred(cred, 0);
1507		if (error)
1508			return (EPERM);
1509
1510		/*
1511		 * XXXMAC: Additional consistency tests regarding the single
1512		 * and the range of the new label might be performed here.
1513		 */
1514	}
1515
1516	return (0);
1517}
1518
1519static int
1520mac_biba_check_ifnet_transmit(struct ifnet *ifnet, struct label *ifnetlabel,
1521    struct mbuf *m, struct label *mbuflabel)
1522{
1523	struct mac_biba *p, *i;
1524
1525	if (!mac_biba_enabled)
1526		return (0);
1527
1528	p = SLOT(mbuflabel);
1529	i = SLOT(ifnetlabel);
1530
1531	return (mac_biba_single_in_range(p, i) ? 0 : EACCES);
1532}
1533
1534static int
1535mac_biba_check_kld_load(struct ucred *cred, struct vnode *vp,
1536    struct label *label)
1537{
1538	struct mac_biba *subj, *obj;
1539	int error;
1540
1541	if (!mac_biba_enabled)
1542		return (0);
1543
1544	subj = SLOT(&cred->cr_label);
1545
1546	error = mac_biba_subject_privileged(subj);
1547	if (error)
1548		return (error);
1549
1550	obj = SLOT(label);
1551	if (!mac_biba_high_single(obj))
1552		return (EACCES);
1553
1554	return (0);
1555}
1556
1557
1558static int
1559mac_biba_check_kld_unload(struct ucred *cred)
1560{
1561	struct mac_biba *subj;
1562
1563	if (!mac_biba_enabled)
1564		return (0);
1565
1566	subj = SLOT(&cred->cr_label);
1567
1568	return (mac_biba_subject_privileged(subj));
1569}
1570
1571static int
1572mac_biba_check_mount_stat(struct ucred *cred, struct mount *mp,
1573    struct label *mntlabel)
1574{
1575	struct mac_biba *subj, *obj;
1576
1577	if (!mac_biba_enabled)
1578		return (0);
1579
1580	subj = SLOT(&cred->cr_label);
1581	obj = SLOT(mntlabel);
1582
1583	if (!mac_biba_dominate_single(obj, subj))
1584		return (EACCES);
1585
1586	return (0);
1587}
1588
1589static int
1590mac_biba_check_pipe_ioctl(struct ucred *cred, struct pipe *pipe,
1591    struct label *pipelabel, unsigned long cmd, void /* caddr_t */ *data)
1592{
1593
1594	if(!mac_biba_enabled)
1595		return (0);
1596
1597	/* XXX: This will be implemented soon... */
1598
1599	return (0);
1600}
1601
1602static int
1603mac_biba_check_pipe_poll(struct ucred *cred, struct pipe *pipe,
1604    struct label *pipelabel)
1605{
1606	struct mac_biba *subj, *obj;
1607
1608	if (!mac_biba_enabled)
1609		return (0);
1610
1611	subj = SLOT(&cred->cr_label);
1612	obj = SLOT((pipelabel));
1613
1614	if (!mac_biba_dominate_single(obj, subj))
1615		return (EACCES);
1616
1617	return (0);
1618}
1619
1620static int
1621mac_biba_check_pipe_read(struct ucred *cred, struct pipe *pipe,
1622    struct label *pipelabel)
1623{
1624	struct mac_biba *subj, *obj;
1625
1626	if (!mac_biba_enabled)
1627		return (0);
1628
1629	subj = SLOT(&cred->cr_label);
1630	obj = SLOT((pipelabel));
1631
1632	if (!mac_biba_dominate_single(obj, subj))
1633		return (EACCES);
1634
1635	return (0);
1636}
1637
1638static int
1639mac_biba_check_pipe_relabel(struct ucred *cred, struct pipe *pipe,
1640    struct label *pipelabel, struct label *newlabel)
1641{
1642	struct mac_biba *subj, *obj, *new;
1643	int error;
1644
1645	new = SLOT(newlabel);
1646	subj = SLOT(&cred->cr_label);
1647	obj = SLOT(pipelabel);
1648
1649	/*
1650	 * If there is a Biba label update for a pipe, it must be a
1651	 * single update.
1652	 */
1653	error = biba_atmostflags(new, MAC_BIBA_FLAG_SINGLE);
1654	if (error)
1655		return (error);
1656
1657	/*
1658	 * To perform a relabel of a pipe (Biba label or not), Biba must
1659	 * authorize the relabel.
1660	 */
1661	if (!mac_biba_single_in_range(obj, subj))
1662		return (EPERM);
1663
1664	/*
1665	 * If the Biba label is to be changed, authorize as appropriate.
1666	 */
1667	if (new->mb_flags & MAC_BIBA_FLAG_SINGLE) {
1668		/*
1669		 * To change the Biba label on a pipe, the new pipe label
1670		 * must be in the subject range.
1671		 */
1672		if (!mac_biba_single_in_range(new, subj))
1673			return (EPERM);
1674
1675		/*
1676		 * To change the Biba label on a pipe to be EQUAL, the
1677		 * subject must have appropriate privilege.
1678		 */
1679		if (mac_biba_contains_equal(new)) {
1680			error = mac_biba_subject_privileged(subj);
1681			if (error)
1682				return (error);
1683		}
1684	}
1685
1686	return (0);
1687}
1688
1689static int
1690mac_biba_check_pipe_stat(struct ucred *cred, struct pipe *pipe,
1691    struct label *pipelabel)
1692{
1693	struct mac_biba *subj, *obj;
1694
1695	if (!mac_biba_enabled)
1696		return (0);
1697
1698	subj = SLOT(&cred->cr_label);
1699	obj = SLOT((pipelabel));
1700
1701	if (!mac_biba_dominate_single(obj, subj))
1702		return (EACCES);
1703
1704	return (0);
1705}
1706
1707static int
1708mac_biba_check_pipe_write(struct ucred *cred, struct pipe *pipe,
1709    struct label *pipelabel)
1710{
1711	struct mac_biba *subj, *obj;
1712
1713	if (!mac_biba_enabled)
1714		return (0);
1715
1716	subj = SLOT(&cred->cr_label);
1717	obj = SLOT((pipelabel));
1718
1719	if (!mac_biba_dominate_single(subj, obj))
1720		return (EACCES);
1721
1722	return (0);
1723}
1724
1725static int
1726mac_biba_check_proc_debug(struct ucred *cred, struct proc *proc)
1727{
1728	struct mac_biba *subj, *obj;
1729
1730	if (!mac_biba_enabled)
1731		return (0);
1732
1733	subj = SLOT(&cred->cr_label);
1734	obj = SLOT(&proc->p_ucred->cr_label);
1735
1736	/* XXX: range checks */
1737	if (!mac_biba_dominate_single(obj, subj))
1738		return (ESRCH);
1739	if (!mac_biba_dominate_single(subj, obj))
1740		return (EACCES);
1741
1742	return (0);
1743}
1744
1745static int
1746mac_biba_check_proc_sched(struct ucred *cred, struct proc *proc)
1747{
1748	struct mac_biba *subj, *obj;
1749
1750	if (!mac_biba_enabled)
1751		return (0);
1752
1753	subj = SLOT(&cred->cr_label);
1754	obj = SLOT(&proc->p_ucred->cr_label);
1755
1756	/* XXX: range checks */
1757	if (!mac_biba_dominate_single(obj, subj))
1758		return (ESRCH);
1759	if (!mac_biba_dominate_single(subj, obj))
1760		return (EACCES);
1761
1762	return (0);
1763}
1764
1765static int
1766mac_biba_check_proc_signal(struct ucred *cred, struct proc *proc, int signum)
1767{
1768	struct mac_biba *subj, *obj;
1769
1770	if (!mac_biba_enabled)
1771		return (0);
1772
1773	subj = SLOT(&cred->cr_label);
1774	obj = SLOT(&proc->p_ucred->cr_label);
1775
1776	/* XXX: range checks */
1777	if (!mac_biba_dominate_single(obj, subj))
1778		return (ESRCH);
1779	if (!mac_biba_dominate_single(subj, obj))
1780		return (EACCES);
1781
1782	return (0);
1783}
1784
1785static int
1786mac_biba_check_socket_deliver(struct socket *so, struct label *socketlabel,
1787    struct mbuf *m, struct label *mbuflabel)
1788{
1789	struct mac_biba *p, *s;
1790
1791	if (!mac_biba_enabled)
1792		return (0);
1793
1794	p = SLOT(mbuflabel);
1795	s = SLOT(socketlabel);
1796
1797	return (mac_biba_equal_single(p, s) ? 0 : EACCES);
1798}
1799
1800static int
1801mac_biba_check_socket_relabel(struct ucred *cred, struct socket *so,
1802    struct label *socketlabel, struct label *newlabel)
1803{
1804	struct mac_biba *subj, *obj, *new;
1805	int error;
1806
1807	new = SLOT(newlabel);
1808	subj = SLOT(&cred->cr_label);
1809	obj = SLOT(socketlabel);
1810
1811	/*
1812	 * If there is a Biba label update for the socket, it may be
1813	 * an update of single.
1814	 */
1815	error = biba_atmostflags(new, MAC_BIBA_FLAG_SINGLE);
1816	if (error)
1817		return (error);
1818
1819	/*
1820	 * To relabel a socket, the old socket single must be in the subject
1821	 * range.
1822	 */
1823	if (!mac_biba_single_in_range(obj, subj))
1824		return (EPERM);
1825
1826	/*
1827	 * If the Biba label is to be changed, authorize as appropriate.
1828	 */
1829	if (new->mb_flags & MAC_BIBA_FLAG_SINGLE) {
1830		/*
1831		 * To relabel a socket, the new socket single must be in
1832		 * the subject range.
1833		 */
1834		if (!mac_biba_single_in_range(new, subj))
1835			return (EPERM);
1836
1837		/*
1838		 * To change the Biba label on the socket to contain EQUAL,
1839		 * the subject must have appropriate privilege.
1840		 */
1841		if (mac_biba_contains_equal(new)) {
1842			error = mac_biba_subject_privileged(subj);
1843			if (error)
1844				return (error);
1845		}
1846	}
1847
1848	return (0);
1849}
1850
1851static int
1852mac_biba_check_socket_visible(struct ucred *cred, struct socket *socket,
1853    struct label *socketlabel)
1854{
1855	struct mac_biba *subj, *obj;
1856
1857	if (!mac_biba_enabled)
1858		return (0);
1859
1860	subj = SLOT(&cred->cr_label);
1861	obj = SLOT(socketlabel);
1862
1863	if (!mac_biba_dominate_single(obj, subj))
1864		return (ENOENT);
1865
1866	return (0);
1867}
1868
1869static int
1870mac_biba_check_sysarch_ioperm(struct ucred *cred)
1871{
1872	struct mac_biba *subj;
1873	int error;
1874
1875	if (!mac_biba_enabled)
1876		return (0);
1877
1878	subj = SLOT(&cred->cr_label);
1879
1880	error = mac_biba_subject_privileged(subj);
1881	if (error)
1882		return (error);
1883
1884	return (0);
1885}
1886
1887static int
1888mac_biba_check_system_acct(struct ucred *cred, struct vnode *vp,
1889    struct label *label)
1890{
1891	struct mac_biba *subj, *obj;
1892	int error;
1893
1894	if (!mac_biba_enabled)
1895		return (0);
1896
1897	subj = SLOT(&cred->cr_label);
1898
1899	error = mac_biba_subject_privileged(subj);
1900	if (error)
1901		return (error);
1902
1903	if (label == NULL)
1904		return (0);
1905
1906	obj = SLOT(label);
1907	if (!mac_biba_high_single(obj))
1908		return (EACCES);
1909
1910	return (0);
1911}
1912
1913static int
1914mac_biba_check_system_settime(struct ucred *cred)
1915{
1916	struct mac_biba *subj;
1917	int error;
1918
1919	if (!mac_biba_enabled)
1920		return (0);
1921
1922	subj = SLOT(&cred->cr_label);
1923
1924	error = mac_biba_subject_privileged(subj);
1925	if (error)
1926		return (error);
1927
1928	return (0);
1929}
1930
1931static int
1932mac_biba_check_system_swapon(struct ucred *cred, struct vnode *vp,
1933    struct label *label)
1934{
1935	struct mac_biba *subj, *obj;
1936	int error;
1937
1938	if (!mac_biba_enabled)
1939		return (0);
1940
1941	subj = SLOT(&cred->cr_label);
1942	obj = SLOT(label);
1943
1944	error = mac_biba_subject_privileged(subj);
1945	if (error)
1946		return (error);
1947
1948	if (!mac_biba_high_single(obj))
1949		return (EACCES);
1950
1951	return (0);
1952}
1953
1954static int
1955mac_biba_check_system_swapoff(struct ucred *cred, struct vnode *vp,
1956    struct label *label)
1957{
1958	struct mac_biba *subj, *obj;
1959	int error;
1960
1961	if (!mac_biba_enabled)
1962		return (0);
1963
1964	subj = SLOT(&cred->cr_label);
1965	obj = SLOT(label);
1966
1967	error = mac_biba_subject_privileged(subj);
1968	if (error)
1969		return (error);
1970
1971	return (0);
1972}
1973
1974static int
1975mac_biba_check_system_sysctl(struct ucred *cred, int *name, u_int namelen,
1976    void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen)
1977{
1978	struct mac_biba *subj;
1979	int error;
1980
1981	if (!mac_biba_enabled)
1982		return (0);
1983
1984	subj = SLOT(&cred->cr_label);
1985
1986	/*
1987	 * In general, treat sysctl variables as biba/high, but also
1988	 * require privilege to change them, since they are a
1989	 * communications channel between grades.  Exempt MIB
1990	 * queries from this due to undocmented sysctl magic.
1991	 * XXXMAC: This probably requires some more review.
1992	 */
1993	if (new != NULL) {
1994		if (namelen > 0 && name[0] == 0)
1995			return (0);
1996
1997		if (!mac_biba_subject_dominate_high(subj))
1998			return (EACCES);
1999
2000		error = mac_biba_subject_privileged(subj);
2001		if (error)
2002			return (error);
2003	}
2004
2005	return (0);
2006}
2007
2008static int
2009mac_biba_check_vnode_chdir(struct ucred *cred, struct vnode *dvp,
2010    struct label *dlabel)
2011{
2012	struct mac_biba *subj, *obj;
2013
2014	if (!mac_biba_enabled)
2015		return (0);
2016
2017	subj = SLOT(&cred->cr_label);
2018	obj = SLOT(dlabel);
2019
2020	if (!mac_biba_dominate_single(obj, subj))
2021		return (EACCES);
2022
2023	return (0);
2024}
2025
2026static int
2027mac_biba_check_vnode_chroot(struct ucred *cred, struct vnode *dvp,
2028    struct label *dlabel)
2029{
2030	struct mac_biba *subj, *obj;
2031
2032	if (!mac_biba_enabled)
2033		return (0);
2034
2035	subj = SLOT(&cred->cr_label);
2036	obj = SLOT(dlabel);
2037
2038	if (!mac_biba_dominate_single(obj, subj))
2039		return (EACCES);
2040
2041	return (0);
2042}
2043
2044static int
2045mac_biba_check_vnode_create(struct ucred *cred, struct vnode *dvp,
2046    struct label *dlabel, struct componentname *cnp, struct vattr *vap)
2047{
2048	struct mac_biba *subj, *obj;
2049
2050	if (!mac_biba_enabled)
2051		return (0);
2052
2053	subj = SLOT(&cred->cr_label);
2054	obj = SLOT(dlabel);
2055
2056	if (!mac_biba_dominate_single(subj, obj))
2057		return (EACCES);
2058
2059	return (0);
2060}
2061
2062static int
2063mac_biba_check_vnode_delete(struct ucred *cred, struct vnode *dvp,
2064    struct label *dlabel, struct vnode *vp, struct label *label,
2065    struct componentname *cnp)
2066{
2067	struct mac_biba *subj, *obj;
2068
2069	if (!mac_biba_enabled)
2070		return (0);
2071
2072	subj = SLOT(&cred->cr_label);
2073	obj = SLOT(dlabel);
2074
2075	if (!mac_biba_dominate_single(subj, obj))
2076		return (EACCES);
2077
2078	obj = SLOT(label);
2079
2080	if (!mac_biba_dominate_single(subj, obj))
2081		return (EACCES);
2082
2083	return (0);
2084}
2085
2086static int
2087mac_biba_check_vnode_deleteacl(struct ucred *cred, struct vnode *vp,
2088    struct label *label, acl_type_t type)
2089{
2090	struct mac_biba *subj, *obj;
2091
2092	if (!mac_biba_enabled)
2093		return (0);
2094
2095	subj = SLOT(&cred->cr_label);
2096	obj = SLOT(label);
2097
2098	if (!mac_biba_dominate_single(subj, obj))
2099		return (EACCES);
2100
2101	return (0);
2102}
2103
2104static int
2105mac_biba_check_vnode_exec(struct ucred *cred, struct vnode *vp,
2106    struct label *label, struct image_params *imgp,
2107    struct label *execlabel)
2108{
2109	struct mac_biba *subj, *obj, *exec;
2110	int error;
2111
2112	if (execlabel != NULL) {
2113		/*
2114		 * We currently don't permit labels to be changed at
2115		 * exec-time as part of Biba, so disallow non-NULL
2116		 * Biba label elements in the execlabel.
2117		 */
2118		exec = SLOT(execlabel);
2119		error = biba_atmostflags(exec, 0);
2120		if (error)
2121			return (error);
2122	}
2123
2124	if (!mac_biba_enabled)
2125		return (0);
2126
2127	subj = SLOT(&cred->cr_label);
2128	obj = SLOT(label);
2129
2130	if (!mac_biba_dominate_single(obj, subj))
2131		return (EACCES);
2132
2133	return (0);
2134}
2135
2136static int
2137mac_biba_check_vnode_getacl(struct ucred *cred, struct vnode *vp,
2138    struct label *label, acl_type_t type)
2139{
2140	struct mac_biba *subj, *obj;
2141
2142	if (!mac_biba_enabled)
2143		return (0);
2144
2145	subj = SLOT(&cred->cr_label);
2146	obj = SLOT(label);
2147
2148	if (!mac_biba_dominate_single(obj, subj))
2149		return (EACCES);
2150
2151	return (0);
2152}
2153
2154static int
2155mac_biba_check_vnode_getextattr(struct ucred *cred, struct vnode *vp,
2156    struct label *label, int attrnamespace, const char *name, struct uio *uio)
2157{
2158	struct mac_biba *subj, *obj;
2159
2160	if (!mac_biba_enabled)
2161		return (0);
2162
2163	subj = SLOT(&cred->cr_label);
2164	obj = SLOT(label);
2165
2166	if (!mac_biba_dominate_single(obj, subj))
2167		return (EACCES);
2168
2169	return (0);
2170}
2171
2172static int
2173mac_biba_check_vnode_link(struct ucred *cred, struct vnode *dvp,
2174    struct label *dlabel, struct vnode *vp, struct label *label,
2175    struct componentname *cnp)
2176{
2177	struct mac_biba *subj, *obj;
2178
2179	if (!mac_biba_enabled)
2180		return (0);
2181
2182	subj = SLOT(&cred->cr_label);
2183	obj = SLOT(dlabel);
2184
2185	if (!mac_biba_dominate_single(subj, obj))
2186		return (EACCES);
2187
2188	obj = SLOT(label);
2189
2190	if (!mac_biba_dominate_single(subj, obj))
2191		return (EACCES);
2192
2193	return (0);
2194}
2195
2196static int
2197mac_biba_check_vnode_lookup(struct ucred *cred, struct vnode *dvp,
2198    struct label *dlabel, struct componentname *cnp)
2199{
2200	struct mac_biba *subj, *obj;
2201
2202	if (!mac_biba_enabled)
2203		return (0);
2204
2205	subj = SLOT(&cred->cr_label);
2206	obj = SLOT(dlabel);
2207
2208	if (!mac_biba_dominate_single(obj, subj))
2209		return (EACCES);
2210
2211	return (0);
2212}
2213
2214static int
2215mac_biba_check_vnode_mmap(struct ucred *cred, struct vnode *vp,
2216    struct label *label, int prot)
2217{
2218	struct mac_biba *subj, *obj;
2219
2220	/*
2221	 * Rely on the use of open()-time protections to handle
2222	 * non-revocation cases.
2223	 */
2224	if (!mac_biba_enabled || !revocation_enabled)
2225		return (0);
2226
2227	subj = SLOT(&cred->cr_label);
2228	obj = SLOT(label);
2229
2230	if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
2231		if (!mac_biba_dominate_single(obj, subj))
2232			return (EACCES);
2233	}
2234	if (prot & VM_PROT_WRITE) {
2235		if (!mac_biba_dominate_single(subj, obj))
2236			return (EACCES);
2237	}
2238
2239	return (0);
2240}
2241
2242static int
2243mac_biba_check_vnode_open(struct ucred *cred, struct vnode *vp,
2244    struct label *vnodelabel, int acc_mode)
2245{
2246	struct mac_biba *subj, *obj;
2247
2248	if (!mac_biba_enabled)
2249		return (0);
2250
2251	subj = SLOT(&cred->cr_label);
2252	obj = SLOT(vnodelabel);
2253
2254	/* XXX privilege override for admin? */
2255	if (acc_mode & (VREAD | VEXEC | VSTAT)) {
2256		if (!mac_biba_dominate_single(obj, subj))
2257			return (EACCES);
2258	}
2259	if (acc_mode & (VWRITE | VAPPEND | VADMIN)) {
2260		if (!mac_biba_dominate_single(subj, obj))
2261			return (EACCES);
2262	}
2263
2264	return (0);
2265}
2266
2267static int
2268mac_biba_check_vnode_poll(struct ucred *active_cred, struct ucred *file_cred,
2269    struct vnode *vp, struct label *label)
2270{
2271	struct mac_biba *subj, *obj;
2272
2273	if (!mac_biba_enabled || !revocation_enabled)
2274		return (0);
2275
2276	subj = SLOT(&active_cred->cr_label);
2277	obj = SLOT(label);
2278
2279	if (!mac_biba_dominate_single(obj, subj))
2280		return (EACCES);
2281
2282	return (0);
2283}
2284
2285static int
2286mac_biba_check_vnode_read(struct ucred *active_cred, struct ucred *file_cred,
2287    struct vnode *vp, struct label *label)
2288{
2289	struct mac_biba *subj, *obj;
2290
2291	if (!mac_biba_enabled || !revocation_enabled)
2292		return (0);
2293
2294	subj = SLOT(&active_cred->cr_label);
2295	obj = SLOT(label);
2296
2297	if (!mac_biba_dominate_single(obj, subj))
2298		return (EACCES);
2299
2300	return (0);
2301}
2302
2303static int
2304mac_biba_check_vnode_readdir(struct ucred *cred, struct vnode *dvp,
2305    struct label *dlabel)
2306{
2307	struct mac_biba *subj, *obj;
2308
2309	if (!mac_biba_enabled)
2310		return (0);
2311
2312	subj = SLOT(&cred->cr_label);
2313	obj = SLOT(dlabel);
2314
2315	if (!mac_biba_dominate_single(obj, subj))
2316		return (EACCES);
2317
2318	return (0);
2319}
2320
2321static int
2322mac_biba_check_vnode_readlink(struct ucred *cred, struct vnode *vp,
2323    struct label *label)
2324{
2325	struct mac_biba *subj, *obj;
2326
2327	if (!mac_biba_enabled)
2328		return (0);
2329
2330	subj = SLOT(&cred->cr_label);
2331	obj = SLOT(label);
2332
2333	if (!mac_biba_dominate_single(obj, subj))
2334		return (EACCES);
2335
2336	return (0);
2337}
2338
2339static int
2340mac_biba_check_vnode_relabel(struct ucred *cred, struct vnode *vp,
2341    struct label *vnodelabel, struct label *newlabel)
2342{
2343	struct mac_biba *old, *new, *subj;
2344	int error;
2345
2346	old = SLOT(vnodelabel);
2347	new = SLOT(newlabel);
2348	subj = SLOT(&cred->cr_label);
2349
2350	/*
2351	 * If there is a Biba label update for the vnode, it must be a
2352	 * single label.
2353	 */
2354	error = biba_atmostflags(new, MAC_BIBA_FLAG_SINGLE);
2355	if (error)
2356		return (error);
2357
2358	/*
2359	 * To perform a relabel of the vnode (Biba label or not), Biba must
2360	 * authorize the relabel.
2361	 */
2362	if (!mac_biba_single_in_range(old, subj))
2363		return (EPERM);
2364
2365	/*
2366	 * If the Biba label is to be changed, authorize as appropriate.
2367	 */
2368	if (new->mb_flags & MAC_BIBA_FLAG_SINGLE) {
2369		/*
2370		 * To change the Biba label on a vnode, the new vnode label
2371		 * must be in the subject range.
2372		 */
2373		if (!mac_biba_single_in_range(new, subj))
2374			return (EPERM);
2375
2376		/*
2377		 * To change the Biba label on the vnode to be EQUAL,
2378		 * the subject must have appropriate privilege.
2379		 */
2380		if (mac_biba_contains_equal(new)) {
2381			error = mac_biba_subject_privileged(subj);
2382			if (error)
2383				return (error);
2384		}
2385	}
2386
2387	return (0);
2388}
2389
2390static int
2391mac_biba_check_vnode_rename_from(struct ucred *cred, struct vnode *dvp,
2392    struct label *dlabel, struct vnode *vp, struct label *label,
2393    struct componentname *cnp)
2394{
2395	struct mac_biba *subj, *obj;
2396
2397	if (!mac_biba_enabled)
2398		return (0);
2399
2400	subj = SLOT(&cred->cr_label);
2401	obj = SLOT(dlabel);
2402
2403	if (!mac_biba_dominate_single(subj, obj))
2404		return (EACCES);
2405
2406	obj = SLOT(label);
2407
2408	if (!mac_biba_dominate_single(subj, obj))
2409		return (EACCES);
2410
2411	return (0);
2412}
2413
2414static int
2415mac_biba_check_vnode_rename_to(struct ucred *cred, struct vnode *dvp,
2416    struct label *dlabel, struct vnode *vp, struct label *label, int samedir,
2417    struct componentname *cnp)
2418{
2419	struct mac_biba *subj, *obj;
2420
2421	if (!mac_biba_enabled)
2422		return (0);
2423
2424	subj = SLOT(&cred->cr_label);
2425	obj = SLOT(dlabel);
2426
2427	if (!mac_biba_dominate_single(subj, obj))
2428		return (EACCES);
2429
2430	if (vp != NULL) {
2431		obj = SLOT(label);
2432
2433		if (!mac_biba_dominate_single(subj, obj))
2434			return (EACCES);
2435	}
2436
2437	return (0);
2438}
2439
2440static int
2441mac_biba_check_vnode_revoke(struct ucred *cred, struct vnode *vp,
2442    struct label *label)
2443{
2444	struct mac_biba *subj, *obj;
2445
2446	if (!mac_biba_enabled)
2447		return (0);
2448
2449	subj = SLOT(&cred->cr_label);
2450	obj = SLOT(label);
2451
2452	if (!mac_biba_dominate_single(subj, obj))
2453		return (EACCES);
2454
2455	return (0);
2456}
2457
2458static int
2459mac_biba_check_vnode_setacl(struct ucred *cred, struct vnode *vp,
2460    struct label *label, acl_type_t type, struct acl *acl)
2461{
2462	struct mac_biba *subj, *obj;
2463
2464	if (!mac_biba_enabled)
2465		return (0);
2466
2467	subj = SLOT(&cred->cr_label);
2468	obj = SLOT(label);
2469
2470	if (!mac_biba_dominate_single(subj, obj))
2471		return (EACCES);
2472
2473	return (0);
2474}
2475
2476static int
2477mac_biba_check_vnode_setextattr(struct ucred *cred, struct vnode *vp,
2478    struct label *vnodelabel, int attrnamespace, const char *name,
2479    struct uio *uio)
2480{
2481	struct mac_biba *subj, *obj;
2482
2483	if (!mac_biba_enabled)
2484		return (0);
2485
2486	subj = SLOT(&cred->cr_label);
2487	obj = SLOT(vnodelabel);
2488
2489	if (!mac_biba_dominate_single(subj, obj))
2490		return (EACCES);
2491
2492	/* XXX: protect the MAC EA in a special way? */
2493
2494	return (0);
2495}
2496
2497static int
2498mac_biba_check_vnode_setflags(struct ucred *cred, struct vnode *vp,
2499    struct label *vnodelabel, u_long flags)
2500{
2501	struct mac_biba *subj, *obj;
2502
2503	if (!mac_biba_enabled)
2504		return (0);
2505
2506	subj = SLOT(&cred->cr_label);
2507	obj = SLOT(vnodelabel);
2508
2509	if (!mac_biba_dominate_single(subj, obj))
2510		return (EACCES);
2511
2512	return (0);
2513}
2514
2515static int
2516mac_biba_check_vnode_setmode(struct ucred *cred, struct vnode *vp,
2517    struct label *vnodelabel, mode_t mode)
2518{
2519	struct mac_biba *subj, *obj;
2520
2521	if (!mac_biba_enabled)
2522		return (0);
2523
2524	subj = SLOT(&cred->cr_label);
2525	obj = SLOT(vnodelabel);
2526
2527	if (!mac_biba_dominate_single(subj, obj))
2528		return (EACCES);
2529
2530	return (0);
2531}
2532
2533static int
2534mac_biba_check_vnode_setowner(struct ucred *cred, struct vnode *vp,
2535    struct label *vnodelabel, uid_t uid, gid_t gid)
2536{
2537	struct mac_biba *subj, *obj;
2538
2539	if (!mac_biba_enabled)
2540		return (0);
2541
2542	subj = SLOT(&cred->cr_label);
2543	obj = SLOT(vnodelabel);
2544
2545	if (!mac_biba_dominate_single(subj, obj))
2546		return (EACCES);
2547
2548	return (0);
2549}
2550
2551static int
2552mac_biba_check_vnode_setutimes(struct ucred *cred, struct vnode *vp,
2553    struct label *vnodelabel, struct timespec atime, struct timespec mtime)
2554{
2555	struct mac_biba *subj, *obj;
2556
2557	if (!mac_biba_enabled)
2558		return (0);
2559
2560	subj = SLOT(&cred->cr_label);
2561	obj = SLOT(vnodelabel);
2562
2563	if (!mac_biba_dominate_single(subj, obj))
2564		return (EACCES);
2565
2566	return (0);
2567}
2568
2569static int
2570mac_biba_check_vnode_stat(struct ucred *active_cred, struct ucred *file_cred,
2571    struct vnode *vp, struct label *vnodelabel)
2572{
2573	struct mac_biba *subj, *obj;
2574
2575	if (!mac_biba_enabled)
2576		return (0);
2577
2578	subj = SLOT(&active_cred->cr_label);
2579	obj = SLOT(vnodelabel);
2580
2581	if (!mac_biba_dominate_single(obj, subj))
2582		return (EACCES);
2583
2584	return (0);
2585}
2586
2587static int
2588mac_biba_check_vnode_write(struct ucred *active_cred,
2589    struct ucred *file_cred, struct vnode *vp, struct label *label)
2590{
2591	struct mac_biba *subj, *obj;
2592
2593	if (!mac_biba_enabled || !revocation_enabled)
2594		return (0);
2595
2596	subj = SLOT(&active_cred->cr_label);
2597	obj = SLOT(label);
2598
2599	if (!mac_biba_dominate_single(subj, obj))
2600		return (EACCES);
2601
2602	return (0);
2603}
2604
2605static struct mac_policy_ops mac_biba_ops =
2606{
2607	.mpo_destroy = mac_biba_destroy,
2608	.mpo_init = mac_biba_init,
2609	.mpo_init_bpfdesc_label = mac_biba_init_label,
2610	.mpo_init_cred_label = mac_biba_init_label,
2611	.mpo_init_devfsdirent_label = mac_biba_init_label,
2612	.mpo_init_ifnet_label = mac_biba_init_label,
2613	.mpo_init_ipq_label = mac_biba_init_label_waitcheck,
2614	.mpo_init_mbuf_label = mac_biba_init_label_waitcheck,
2615	.mpo_init_mount_label = mac_biba_init_label,
2616	.mpo_init_mount_fs_label = mac_biba_init_label,
2617	.mpo_init_pipe_label = mac_biba_init_label,
2618	.mpo_init_socket_label = mac_biba_init_label_waitcheck,
2619	.mpo_init_socket_peer_label = mac_biba_init_label_waitcheck,
2620	.mpo_init_vnode_label = mac_biba_init_label,
2621	.mpo_destroy_bpfdesc_label = mac_biba_destroy_label,
2622	.mpo_destroy_cred_label = mac_biba_destroy_label,
2623	.mpo_destroy_devfsdirent_label = mac_biba_destroy_label,
2624	.mpo_destroy_ifnet_label = mac_biba_destroy_label,
2625	.mpo_destroy_ipq_label = mac_biba_destroy_label,
2626	.mpo_destroy_mbuf_label = mac_biba_destroy_label,
2627	.mpo_destroy_mount_label = mac_biba_destroy_label,
2628	.mpo_destroy_mount_fs_label = mac_biba_destroy_label,
2629	.mpo_destroy_pipe_label = mac_biba_destroy_label,
2630	.mpo_destroy_socket_label = mac_biba_destroy_label,
2631	.mpo_destroy_socket_peer_label = mac_biba_destroy_label,
2632	.mpo_destroy_vnode_label = mac_biba_destroy_label,
2633	.mpo_copy_pipe_label = mac_biba_copy_label,
2634	.mpo_copy_vnode_label = mac_biba_copy_label,
2635	.mpo_externalize_cred_label = mac_biba_externalize_label,
2636	.mpo_externalize_ifnet_label = mac_biba_externalize_label,
2637	.mpo_externalize_pipe_label = mac_biba_externalize_label,
2638	.mpo_externalize_socket_label = mac_biba_externalize_label,
2639	.mpo_externalize_socket_peer_label = mac_biba_externalize_label,
2640	.mpo_externalize_vnode_label = mac_biba_externalize_label,
2641	.mpo_internalize_cred_label = mac_biba_internalize_label,
2642	.mpo_internalize_ifnet_label = mac_biba_internalize_label,
2643	.mpo_internalize_pipe_label = mac_biba_internalize_label,
2644	.mpo_internalize_socket_label = mac_biba_internalize_label,
2645	.mpo_internalize_vnode_label = mac_biba_internalize_label,
2646	.mpo_create_devfs_device = mac_biba_create_devfs_device,
2647	.mpo_create_devfs_directory = mac_biba_create_devfs_directory,
2648	.mpo_create_devfs_symlink = mac_biba_create_devfs_symlink,
2649	.mpo_create_mount = mac_biba_create_mount,
2650	.mpo_create_root_mount = mac_biba_create_root_mount,
2651	.mpo_relabel_vnode = mac_biba_relabel_vnode,
2652	.mpo_update_devfsdirent = mac_biba_update_devfsdirent,
2653	.mpo_associate_vnode_devfs = mac_biba_associate_vnode_devfs,
2654	.mpo_associate_vnode_extattr = mac_biba_associate_vnode_extattr,
2655	.mpo_associate_vnode_singlelabel = mac_biba_associate_vnode_singlelabel,
2656	.mpo_create_vnode_extattr = mac_biba_create_vnode_extattr,
2657	.mpo_setlabel_vnode_extattr = mac_biba_setlabel_vnode_extattr,
2658	.mpo_create_mbuf_from_socket = mac_biba_create_mbuf_from_socket,
2659	.mpo_create_pipe = mac_biba_create_pipe,
2660	.mpo_create_socket = mac_biba_create_socket,
2661	.mpo_create_socket_from_socket = mac_biba_create_socket_from_socket,
2662	.mpo_relabel_pipe = mac_biba_relabel_pipe,
2663	.mpo_relabel_socket = mac_biba_relabel_socket,
2664	.mpo_set_socket_peer_from_mbuf = mac_biba_set_socket_peer_from_mbuf,
2665	.mpo_set_socket_peer_from_socket = mac_biba_set_socket_peer_from_socket,
2666	.mpo_create_bpfdesc = mac_biba_create_bpfdesc,
2667	.mpo_create_datagram_from_ipq = mac_biba_create_datagram_from_ipq,
2668	.mpo_create_fragment = mac_biba_create_fragment,
2669	.mpo_create_ifnet = mac_biba_create_ifnet,
2670	.mpo_create_ipq = mac_biba_create_ipq,
2671	.mpo_create_mbuf_from_mbuf = mac_biba_create_mbuf_from_mbuf,
2672	.mpo_create_mbuf_linklayer = mac_biba_create_mbuf_linklayer,
2673	.mpo_create_mbuf_from_bpfdesc = mac_biba_create_mbuf_from_bpfdesc,
2674	.mpo_create_mbuf_from_ifnet = mac_biba_create_mbuf_from_ifnet,
2675	.mpo_create_mbuf_multicast_encap = mac_biba_create_mbuf_multicast_encap,
2676	.mpo_create_mbuf_netlayer = mac_biba_create_mbuf_netlayer,
2677	.mpo_fragment_match = mac_biba_fragment_match,
2678	.mpo_relabel_ifnet = mac_biba_relabel_ifnet,
2679	.mpo_update_ipq = mac_biba_update_ipq,
2680	.mpo_create_cred = mac_biba_create_cred,
2681	.mpo_create_proc0 = mac_biba_create_proc0,
2682	.mpo_create_proc1 = mac_biba_create_proc1,
2683	.mpo_relabel_cred = mac_biba_relabel_cred,
2684	.mpo_check_bpfdesc_receive = mac_biba_check_bpfdesc_receive,
2685	.mpo_check_cred_relabel = mac_biba_check_cred_relabel,
2686	.mpo_check_cred_visible = mac_biba_check_cred_visible,
2687	.mpo_check_ifnet_relabel = mac_biba_check_ifnet_relabel,
2688	.mpo_check_ifnet_transmit = mac_biba_check_ifnet_transmit,
2689	.mpo_check_kld_load = mac_biba_check_kld_load,
2690	.mpo_check_kld_unload = mac_biba_check_kld_unload,
2691	.mpo_check_mount_stat = mac_biba_check_mount_stat,
2692	.mpo_check_pipe_ioctl = mac_biba_check_pipe_ioctl,
2693	.mpo_check_pipe_poll = mac_biba_check_pipe_poll,
2694	.mpo_check_pipe_read = mac_biba_check_pipe_read,
2695	.mpo_check_pipe_relabel = mac_biba_check_pipe_relabel,
2696	.mpo_check_pipe_stat = mac_biba_check_pipe_stat,
2697	.mpo_check_pipe_write = mac_biba_check_pipe_write,
2698	.mpo_check_proc_debug = mac_biba_check_proc_debug,
2699	.mpo_check_proc_sched = mac_biba_check_proc_sched,
2700	.mpo_check_proc_signal = mac_biba_check_proc_signal,
2701	.mpo_check_socket_deliver = mac_biba_check_socket_deliver,
2702	.mpo_check_socket_relabel = mac_biba_check_socket_relabel,
2703	.mpo_check_socket_visible = mac_biba_check_socket_visible,
2704	.mpo_check_sysarch_ioperm = mac_biba_check_sysarch_ioperm,
2705	.mpo_check_system_acct = mac_biba_check_system_acct,
2706	.mpo_check_system_settime = mac_biba_check_system_settime,
2707	.mpo_check_system_swapon = mac_biba_check_system_swapon,
2708	.mpo_check_system_swapoff = mac_biba_check_system_swapoff,
2709	.mpo_check_system_sysctl = mac_biba_check_system_sysctl,
2710	.mpo_check_vnode_access = mac_biba_check_vnode_open,
2711	.mpo_check_vnode_chdir = mac_biba_check_vnode_chdir,
2712	.mpo_check_vnode_chroot = mac_biba_check_vnode_chroot,
2713	.mpo_check_vnode_create = mac_biba_check_vnode_create,
2714	.mpo_check_vnode_delete = mac_biba_check_vnode_delete,
2715	.mpo_check_vnode_deleteacl = mac_biba_check_vnode_deleteacl,
2716	.mpo_check_vnode_exec = mac_biba_check_vnode_exec,
2717	.mpo_check_vnode_getacl = mac_biba_check_vnode_getacl,
2718	.mpo_check_vnode_getextattr = mac_biba_check_vnode_getextattr,
2719	.mpo_check_vnode_link = mac_biba_check_vnode_link,
2720	.mpo_check_vnode_lookup = mac_biba_check_vnode_lookup,
2721	.mpo_check_vnode_mmap = mac_biba_check_vnode_mmap,
2722	.mpo_check_vnode_mprotect = mac_biba_check_vnode_mmap,
2723	.mpo_check_vnode_open = mac_biba_check_vnode_open,
2724	.mpo_check_vnode_poll = mac_biba_check_vnode_poll,
2725	.mpo_check_vnode_read = mac_biba_check_vnode_read,
2726	.mpo_check_vnode_readdir = mac_biba_check_vnode_readdir,
2727	.mpo_check_vnode_readlink = mac_biba_check_vnode_readlink,
2728	.mpo_check_vnode_relabel = mac_biba_check_vnode_relabel,
2729	.mpo_check_vnode_rename_from = mac_biba_check_vnode_rename_from,
2730	.mpo_check_vnode_rename_to = mac_biba_check_vnode_rename_to,
2731	.mpo_check_vnode_revoke = mac_biba_check_vnode_revoke,
2732	.mpo_check_vnode_setacl = mac_biba_check_vnode_setacl,
2733	.mpo_check_vnode_setextattr = mac_biba_check_vnode_setextattr,
2734	.mpo_check_vnode_setflags = mac_biba_check_vnode_setflags,
2735	.mpo_check_vnode_setmode = mac_biba_check_vnode_setmode,
2736	.mpo_check_vnode_setowner = mac_biba_check_vnode_setowner,
2737	.mpo_check_vnode_setutimes = mac_biba_check_vnode_setutimes,
2738	.mpo_check_vnode_stat = mac_biba_check_vnode_stat,
2739	.mpo_check_vnode_write = mac_biba_check_vnode_write,
2740};
2741
2742MAC_POLICY_SET(&mac_biba_ops, mac_biba, "TrustedBSD MAC/Biba",
2743    MPC_LOADTIME_FLAG_NOTLATE | MPC_LOADTIME_FLAG_LABELMBUFS, &mac_biba_slot);
2744