mac_biba.c revision 187014
1/*-
2 * Copyright (c) 1999-2002, 2007-2008 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 187014 2009-01-10 09:17:16Z 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, &ptys_equal,
125    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 || strcmp(string, "hi") == 0) {
642		element->mbe_type = MAC_BIBA_TYPE_HIGH;
643		element->mbe_grade = MAC_BIBA_TYPE_UNDEF;
644	} else if (strcmp(string, "low") == 0 || strcmp(string, "lo") == 0) {
645		element->mbe_type = MAC_BIBA_TYPE_LOW;
646		element->mbe_grade = MAC_BIBA_TYPE_UNDEF;
647	} else if (strcmp(string, "equal") == 0 ||
648	    strcmp(string, "eq") == 0) {
649		element->mbe_type = MAC_BIBA_TYPE_EQUAL;
650		element->mbe_grade = MAC_BIBA_TYPE_UNDEF;
651	} else {
652		element->mbe_type = MAC_BIBA_TYPE_GRADE;
653
654		/*
655		 * Numeric grade piece of the element.
656		 */
657		grade = strsep(&string, ":");
658		value = strtol(grade, &end, 10);
659		if (end == grade || *end != '\0')
660			return (EINVAL);
661		if (value < 0 || value > 65535)
662			return (EINVAL);
663		element->mbe_grade = value;
664
665		/*
666		 * Optional compartment piece of the element.  If none are
667		 * included, we assume that the label has no compartments.
668		 */
669		if (string == NULL)
670			return (0);
671		if (*string == '\0')
672			return (0);
673
674		while ((compartment = strsep(&string, "+")) != NULL) {
675			value = strtol(compartment, &end, 10);
676			if (compartment == end || *end != '\0')
677				return (EINVAL);
678			if (value < 1 || value > MAC_BIBA_MAX_COMPARTMENTS)
679				return (EINVAL);
680			MAC_BIBA_BIT_SET(value, element->mbe_compartments);
681		}
682	}
683
684	return (0);
685}
686
687/*
688 * Note: destructively consumes the string, make a local copy before calling
689 * if that's a problem.
690 */
691static int
692biba_parse(struct mac_biba *mb, char *string)
693{
694	char *rangehigh, *rangelow, *effective;
695	int error;
696
697	effective = strsep(&string, "(");
698	if (*effective == '\0')
699		effective = NULL;
700
701	if (string != NULL) {
702		rangelow = strsep(&string, "-");
703		if (string == NULL)
704			return (EINVAL);
705		rangehigh = strsep(&string, ")");
706		if (string == NULL)
707			return (EINVAL);
708		if (*string != '\0')
709			return (EINVAL);
710	} else {
711		rangelow = NULL;
712		rangehigh = NULL;
713	}
714
715	KASSERT((rangelow != NULL && rangehigh != NULL) ||
716	    (rangelow == NULL && rangehigh == NULL),
717	    ("biba_parse: range mismatch"));
718
719	bzero(mb, sizeof(*mb));
720	if (effective != NULL) {
721		error = biba_parse_element(&mb->mb_effective, effective);
722		if (error)
723			return (error);
724		mb->mb_flags |= MAC_BIBA_FLAG_EFFECTIVE;
725	}
726
727	if (rangelow != NULL) {
728		error = biba_parse_element(&mb->mb_rangelow, rangelow);
729		if (error)
730			return (error);
731		error = biba_parse_element(&mb->mb_rangehigh, rangehigh);
732		if (error)
733			return (error);
734		mb->mb_flags |= MAC_BIBA_FLAG_RANGE;
735	}
736
737	error = biba_valid(mb);
738	if (error)
739		return (error);
740
741	return (0);
742}
743
744static int
745biba_internalize_label(struct label *label, char *element_name,
746    char *element_data, int *claimed)
747{
748	struct mac_biba *mb, mb_temp;
749	int error;
750
751	if (strcmp(MAC_BIBA_LABEL_NAME, element_name) != 0)
752		return (0);
753
754	(*claimed)++;
755
756	error = biba_parse(&mb_temp, element_data);
757	if (error)
758		return (error);
759
760	mb = SLOT(label);
761	*mb = mb_temp;
762
763	return (0);
764}
765
766static void
767biba_copy_label(struct label *src, struct label *dest)
768{
769
770	*SLOT(dest) = *SLOT(src);
771}
772
773/*
774 * Object-specific entry point implementations are sorted alphabetically by
775 * object type name and then by operation.
776 */
777static int
778biba_bpfdesc_check_receive(struct bpf_d *d, struct label *dlabel,
779    struct ifnet *ifp, struct label *ifplabel)
780{
781	struct mac_biba *a, *b;
782
783	if (!biba_enabled)
784		return (0);
785
786	a = SLOT(dlabel);
787	b = SLOT(ifplabel);
788
789	if (biba_equal_effective(a, b))
790		return (0);
791	return (EACCES);
792}
793
794static void
795biba_bpfdesc_create(struct ucred *cred, struct bpf_d *d,
796    struct label *dlabel)
797{
798	struct mac_biba *source, *dest;
799
800	source = SLOT(cred->cr_label);
801	dest = SLOT(dlabel);
802
803	biba_copy_effective(source, dest);
804}
805
806static void
807biba_bpfdesc_create_mbuf(struct bpf_d *d, struct label *dlabel,
808    struct mbuf *m, struct label *mlabel)
809{
810	struct mac_biba *source, *dest;
811
812	source = SLOT(dlabel);
813	dest = SLOT(mlabel);
814
815	biba_copy_effective(source, dest);
816}
817
818static void
819biba_cred_associate_nfsd(struct ucred *cred)
820{
821	struct mac_biba *label;
822
823	label = SLOT(cred->cr_label);
824	biba_set_effective(label, MAC_BIBA_TYPE_LOW, 0, NULL);
825	biba_set_range(label, MAC_BIBA_TYPE_LOW, 0, NULL, MAC_BIBA_TYPE_HIGH,
826	    0, NULL);
827}
828
829static int
830biba_cred_check_relabel(struct ucred *cred, struct label *newlabel)
831{
832	struct mac_biba *subj, *new;
833	int error;
834
835	subj = SLOT(cred->cr_label);
836	new = SLOT(newlabel);
837
838	/*
839	 * If there is a Biba label update for the credential, it may
840	 * be an update of the effective, range, or both.
841	 */
842	error = biba_atmostflags(new, MAC_BIBA_FLAGS_BOTH);
843	if (error)
844		return (error);
845
846	/*
847	 * If the Biba label is to be changed, authorize as appropriate.
848	 */
849	if (new->mb_flags & MAC_BIBA_FLAGS_BOTH) {
850		/*
851		 * If the change request modifies both the Biba label
852		 * effective and range, check that the new effective will be
853		 * in the new range.
854		 */
855		if ((new->mb_flags & MAC_BIBA_FLAGS_BOTH) ==
856		    MAC_BIBA_FLAGS_BOTH &&
857		    !biba_effective_in_range(new, new))
858			return (EINVAL);
859
860		/*
861		 * To change the Biba effective label on a credential, the
862		 * new effective label must be in the current range.
863		 */
864		if (new->mb_flags & MAC_BIBA_FLAG_EFFECTIVE &&
865		    !biba_effective_in_range(new, subj))
866			return (EPERM);
867
868		/*
869		 * To change the Biba range on a credential, the new range
870		 * label must be in the current range.
871		 */
872		if (new->mb_flags & MAC_BIBA_FLAG_RANGE &&
873		    !biba_range_in_range(new, subj))
874			return (EPERM);
875
876		/*
877		 * To have EQUAL in any component of the new credential Biba
878		 * label, the subject must already have EQUAL in their label.
879		 */
880		if (biba_contains_equal(new)) {
881			error = biba_subject_privileged(subj);
882			if (error)
883				return (error);
884		}
885	}
886
887	return (0);
888}
889
890static int
891biba_cred_check_visible(struct ucred *u1, struct ucred *u2)
892{
893	struct mac_biba *subj, *obj;
894
895	if (!biba_enabled)
896		return (0);
897
898	subj = SLOT(u1->cr_label);
899	obj = SLOT(u2->cr_label);
900
901	/* XXX: range */
902	if (!biba_dominate_effective(obj, subj))
903		return (ESRCH);
904
905	return (0);
906}
907
908static void
909biba_cred_create_init(struct ucred *cred)
910{
911	struct mac_biba *dest;
912
913	dest = SLOT(cred->cr_label);
914
915	biba_set_effective(dest, MAC_BIBA_TYPE_HIGH, 0, NULL);
916	biba_set_range(dest, MAC_BIBA_TYPE_LOW, 0, NULL, MAC_BIBA_TYPE_HIGH,
917	    0, NULL);
918}
919
920static void
921biba_cred_create_swapper(struct ucred *cred)
922{
923	struct mac_biba *dest;
924
925	dest = SLOT(cred->cr_label);
926
927	biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
928	biba_set_range(dest, MAC_BIBA_TYPE_LOW, 0, NULL, MAC_BIBA_TYPE_HIGH,
929	    0, NULL);
930}
931
932static void
933biba_cred_relabel(struct ucred *cred, struct label *newlabel)
934{
935	struct mac_biba *source, *dest;
936
937	source = SLOT(newlabel);
938	dest = SLOT(cred->cr_label);
939
940	biba_copy(source, dest);
941}
942
943static void
944biba_devfs_create_device(struct ucred *cred, struct mount *mp,
945    struct cdev *dev, struct devfs_dirent *de, struct label *delabel)
946{
947	struct mac_biba *mb;
948	int biba_type;
949
950	mb = SLOT(delabel);
951	if (strcmp(dev->si_name, "null") == 0 ||
952	    strcmp(dev->si_name, "zero") == 0 ||
953	    strcmp(dev->si_name, "random") == 0 ||
954	    strncmp(dev->si_name, "fd/", strlen("fd/")) == 0)
955		biba_type = MAC_BIBA_TYPE_EQUAL;
956	else if (ptys_equal &&
957	    (strncmp(dev->si_name, "ttyp", strlen("ttyp")) == 0 ||
958	    strncmp(dev->si_name, "ptyp", strlen("ptyp")) == 0))
959		biba_type = MAC_BIBA_TYPE_EQUAL;
960	else
961		biba_type = MAC_BIBA_TYPE_HIGH;
962	biba_set_effective(mb, biba_type, 0, NULL);
963}
964
965static void
966biba_devfs_create_directory(struct mount *mp, char *dirname, int dirnamelen,
967    struct devfs_dirent *de, struct label *delabel)
968{
969	struct mac_biba *mb;
970
971	mb = SLOT(delabel);
972
973	biba_set_effective(mb, MAC_BIBA_TYPE_HIGH, 0, NULL);
974}
975
976static void
977biba_devfs_create_symlink(struct ucred *cred, struct mount *mp,
978    struct devfs_dirent *dd, struct label *ddlabel, struct devfs_dirent *de,
979    struct label *delabel)
980{
981	struct mac_biba *source, *dest;
982
983	source = SLOT(cred->cr_label);
984	dest = SLOT(delabel);
985
986	biba_copy_effective(source, dest);
987}
988
989static void
990biba_devfs_update(struct mount *mp, struct devfs_dirent *de,
991    struct label *delabel, struct vnode *vp, struct label *vplabel)
992{
993	struct mac_biba *source, *dest;
994
995	source = SLOT(vplabel);
996	dest = SLOT(delabel);
997
998	biba_copy(source, dest);
999}
1000
1001static void
1002biba_devfs_vnode_associate(struct mount *mp, struct label *mntlabel,
1003    struct devfs_dirent *de, struct label *delabel, struct vnode *vp,
1004    struct label *vplabel)
1005{
1006	struct mac_biba *source, *dest;
1007
1008	source = SLOT(delabel);
1009	dest = SLOT(vplabel);
1010
1011	biba_copy_effective(source, dest);
1012}
1013
1014static int
1015biba_ifnet_check_relabel(struct ucred *cred, struct ifnet *ifp,
1016    struct label *ifplabel, struct label *newlabel)
1017{
1018	struct mac_biba *subj, *new;
1019	int error;
1020
1021	subj = SLOT(cred->cr_label);
1022	new = SLOT(newlabel);
1023
1024	/*
1025	 * If there is a Biba label update for the interface, it may be an
1026	 * update of the effective, range, or both.
1027	 */
1028	error = biba_atmostflags(new, MAC_BIBA_FLAGS_BOTH);
1029	if (error)
1030		return (error);
1031
1032	/*
1033	 * Relabling network interfaces requires Biba privilege.
1034	 */
1035	error = biba_subject_privileged(subj);
1036	if (error)
1037		return (error);
1038
1039	return (0);
1040}
1041
1042static int
1043biba_ifnet_check_transmit(struct ifnet *ifp, struct label *ifplabel,
1044    struct mbuf *m, struct label *mlabel)
1045{
1046	struct mac_biba *p, *i;
1047
1048	if (!biba_enabled)
1049		return (0);
1050
1051	p = SLOT(mlabel);
1052	i = SLOT(ifplabel);
1053
1054	return (biba_effective_in_range(p, i) ? 0 : EACCES);
1055}
1056
1057static void
1058biba_ifnet_create(struct ifnet *ifp, struct label *ifplabel)
1059{
1060	char tifname[IFNAMSIZ], *p, *q;
1061	char tiflist[sizeof(trusted_interfaces)];
1062	struct mac_biba *dest;
1063	int len, type;
1064
1065	dest = SLOT(ifplabel);
1066
1067	if (ifp->if_type == IFT_LOOP || interfaces_equal != 0) {
1068		type = MAC_BIBA_TYPE_EQUAL;
1069		goto set;
1070	}
1071
1072	if (trust_all_interfaces) {
1073		type = MAC_BIBA_TYPE_HIGH;
1074		goto set;
1075	}
1076
1077	type = MAC_BIBA_TYPE_LOW;
1078
1079	if (trusted_interfaces[0] == '\0' ||
1080	    !strvalid(trusted_interfaces, sizeof(trusted_interfaces)))
1081		goto set;
1082
1083	bzero(tiflist, sizeof(tiflist));
1084	for (p = trusted_interfaces, q = tiflist; *p != '\0'; p++, q++)
1085		if(*p != ' ' && *p != '\t')
1086			*q = *p;
1087
1088	for (p = q = tiflist;; p++) {
1089		if (*p == ',' || *p == '\0') {
1090			len = p - q;
1091			if (len < IFNAMSIZ) {
1092				bzero(tifname, sizeof(tifname));
1093				bcopy(q, tifname, len);
1094				if (strcmp(tifname, ifp->if_xname) == 0) {
1095					type = MAC_BIBA_TYPE_HIGH;
1096					break;
1097				}
1098			} else {
1099				*p = '\0';
1100				printf("mac_biba warning: interface name "
1101				    "\"%s\" is too long (must be < %d)\n",
1102				    q, IFNAMSIZ);
1103			}
1104			if (*p == '\0')
1105				break;
1106			q = p + 1;
1107		}
1108	}
1109set:
1110	biba_set_effective(dest, type, 0, NULL);
1111	biba_set_range(dest, type, 0, NULL, type, 0, NULL);
1112}
1113
1114static void
1115biba_ifnet_create_mbuf(struct ifnet *ifp, struct label *ifplabel,
1116    struct mbuf *m, struct label *mlabel)
1117{
1118	struct mac_biba *source, *dest;
1119
1120	source = SLOT(ifplabel);
1121	dest = SLOT(mlabel);
1122
1123	biba_copy_effective(source, dest);
1124}
1125
1126static void
1127biba_ifnet_relabel(struct ucred *cred, struct ifnet *ifp,
1128    struct label *ifplabel, struct label *newlabel)
1129{
1130	struct mac_biba *source, *dest;
1131
1132	source = SLOT(newlabel);
1133	dest = SLOT(ifplabel);
1134
1135	biba_copy(source, dest);
1136}
1137
1138static int
1139biba_inpcb_check_deliver(struct inpcb *inp, struct label *inplabel,
1140    struct mbuf *m, struct label *mlabel)
1141{
1142	struct mac_biba *p, *i;
1143
1144	if (!biba_enabled)
1145		return (0);
1146
1147	p = SLOT(mlabel);
1148	i = SLOT(inplabel);
1149
1150	return (biba_equal_effective(p, i) ? 0 : EACCES);
1151}
1152
1153static int
1154biba_inpcb_check_visible(struct ucred *cred, struct inpcb *inp,
1155    struct label *inplabel)
1156{
1157	struct mac_biba *subj, *obj;
1158
1159	if (!biba_enabled)
1160		return (0);
1161
1162	subj = SLOT(cred->cr_label);
1163	obj = SLOT(inplabel);
1164
1165	if (!biba_dominate_effective(obj, subj))
1166		return (ENOENT);
1167
1168	return (0);
1169}
1170
1171static void
1172biba_inpcb_create(struct socket *so, struct label *solabel,
1173    struct inpcb *inp, struct label *inplabel)
1174{
1175	struct mac_biba *source, *dest;
1176
1177	source = SLOT(solabel);
1178	dest = SLOT(inplabel);
1179
1180	biba_copy_effective(source, dest);
1181}
1182
1183static void
1184biba_inpcb_create_mbuf(struct inpcb *inp, struct label *inplabel,
1185    struct mbuf *m, struct label *mlabel)
1186{
1187	struct mac_biba *source, *dest;
1188
1189	source = SLOT(inplabel);
1190	dest = SLOT(mlabel);
1191
1192	biba_copy_effective(source, dest);
1193}
1194
1195static void
1196biba_inpcb_sosetlabel(struct socket *so, struct label *solabel,
1197    struct inpcb *inp, struct label *inplabel)
1198{
1199	struct mac_biba *source, *dest;
1200
1201	source = SLOT(solabel);
1202	dest = SLOT(inplabel);
1203
1204	biba_copy(source, dest);
1205}
1206
1207static void
1208biba_ip6q_create(struct mbuf *m, struct label *mlabel, struct ip6q *q6,
1209    struct label *q6label)
1210{
1211	struct mac_biba *source, *dest;
1212
1213	source = SLOT(mlabel);
1214	dest = SLOT(q6label);
1215
1216	biba_copy_effective(source, dest);
1217}
1218
1219static int
1220biba_ip6q_match(struct mbuf *m, struct label *mlabel, struct ip6q *q6,
1221    struct label *q6label)
1222{
1223	struct mac_biba *a, *b;
1224
1225	a = SLOT(q6label);
1226	b = SLOT(mlabel);
1227
1228	return (biba_equal_effective(a, b));
1229}
1230
1231static void
1232biba_ip6q_reassemble(struct ip6q *q6, struct label *q6label, struct mbuf *m,
1233    struct label *mlabel)
1234{
1235	struct mac_biba *source, *dest;
1236
1237	source = SLOT(q6label);
1238	dest = SLOT(mlabel);
1239
1240	/* Just use the head, since we require them all to match. */
1241	biba_copy_effective(source, dest);
1242}
1243
1244static void
1245biba_ip6q_update(struct mbuf *m, struct label *mlabel, struct ip6q *q6,
1246    struct label *q6label)
1247{
1248
1249	/* NOOP: we only accept matching labels, so no need to update */
1250}
1251
1252static void
1253biba_ipq_create(struct mbuf *m, struct label *mlabel, struct ipq *q,
1254    struct label *qlabel)
1255{
1256	struct mac_biba *source, *dest;
1257
1258	source = SLOT(mlabel);
1259	dest = SLOT(qlabel);
1260
1261	biba_copy_effective(source, dest);
1262}
1263
1264static int
1265biba_ipq_match(struct mbuf *m, struct label *mlabel, struct ipq *q,
1266    struct label *qlabel)
1267{
1268	struct mac_biba *a, *b;
1269
1270	a = SLOT(qlabel);
1271	b = SLOT(mlabel);
1272
1273	return (biba_equal_effective(a, b));
1274}
1275
1276static void
1277biba_ipq_reassemble(struct ipq *q, struct label *qlabel, struct mbuf *m,
1278    struct label *mlabel)
1279{
1280	struct mac_biba *source, *dest;
1281
1282	source = SLOT(qlabel);
1283	dest = SLOT(mlabel);
1284
1285	/* Just use the head, since we require them all to match. */
1286	biba_copy_effective(source, dest);
1287}
1288
1289static void
1290biba_ipq_update(struct mbuf *m, struct label *mlabel, struct ipq *q,
1291    struct label *qlabel)
1292{
1293
1294	/* NOOP: we only accept matching labels, so no need to update */
1295}
1296
1297static int
1298biba_kld_check_load(struct ucred *cred, struct vnode *vp,
1299    struct label *vplabel)
1300{
1301	struct mac_biba *subj, *obj;
1302	int error;
1303
1304	if (!biba_enabled)
1305		return (0);
1306
1307	subj = SLOT(cred->cr_label);
1308
1309	error = biba_subject_privileged(subj);
1310	if (error)
1311		return (error);
1312
1313	obj = SLOT(vplabel);
1314	if (!biba_high_effective(obj))
1315		return (EACCES);
1316
1317	return (0);
1318}
1319
1320static int
1321biba_mount_check_stat(struct ucred *cred, struct mount *mp,
1322    struct label *mplabel)
1323{
1324	struct mac_biba *subj, *obj;
1325
1326	if (!biba_enabled)
1327		return (0);
1328
1329	subj = SLOT(cred->cr_label);
1330	obj = SLOT(mplabel);
1331
1332	if (!biba_dominate_effective(obj, subj))
1333		return (EACCES);
1334
1335	return (0);
1336}
1337
1338static void
1339biba_mount_create(struct ucred *cred, struct mount *mp,
1340    struct label *mplabel)
1341{
1342	struct mac_biba *source, *dest;
1343
1344	source = SLOT(cred->cr_label);
1345	dest = SLOT(mplabel);
1346
1347	biba_copy_effective(source, dest);
1348}
1349
1350static void
1351biba_netatalk_aarp_send(struct ifnet *ifp, struct label *ifplabel,
1352    struct mbuf *m, struct label *mlabel)
1353{
1354	struct mac_biba *dest;
1355
1356	dest = SLOT(mlabel);
1357
1358	biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
1359}
1360
1361static void
1362biba_netinet_arp_send(struct ifnet *ifp, struct label *ifplabel,
1363    struct mbuf *m, struct label *mlabel)
1364{
1365	struct mac_biba *dest;
1366
1367	dest = SLOT(mlabel);
1368
1369	biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
1370}
1371
1372static void
1373biba_netinet_firewall_reply(struct mbuf *mrecv, struct label *mrecvlabel,
1374    struct mbuf *msend, struct label *msendlabel)
1375{
1376	struct mac_biba *source, *dest;
1377
1378	source = SLOT(mrecvlabel);
1379	dest = SLOT(msendlabel);
1380
1381	biba_copy_effective(source, dest);
1382}
1383
1384static void
1385biba_netinet_firewall_send(struct mbuf *m, struct label *mlabel)
1386{
1387	struct mac_biba *dest;
1388
1389	dest = SLOT(mlabel);
1390
1391	/* XXX: where is the label for the firewall really coming from? */
1392	biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
1393}
1394
1395static void
1396biba_netinet_fragment(struct mbuf *m, struct label *mlabel,
1397    struct mbuf *frag, struct label *fraglabel)
1398{
1399	struct mac_biba *source, *dest;
1400
1401	source = SLOT(mlabel);
1402	dest = SLOT(fraglabel);
1403
1404	biba_copy_effective(source, dest);
1405}
1406
1407static void
1408biba_netinet_icmp_reply(struct mbuf *mrecv, struct label *mrecvlabel,
1409    struct mbuf *msend, struct label *msendlabel)
1410{
1411	struct mac_biba *source, *dest;
1412
1413	source = SLOT(mrecvlabel);
1414	dest = SLOT(msendlabel);
1415
1416	biba_copy_effective(source, dest);
1417}
1418
1419static void
1420biba_netinet_igmp_send(struct ifnet *ifp, struct label *ifplabel,
1421    struct mbuf *m, struct label *mlabel)
1422{
1423	struct mac_biba *dest;
1424
1425	dest = SLOT(mlabel);
1426
1427	biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
1428}
1429
1430static void
1431biba_netinet6_nd6_send(struct ifnet *ifp, struct label *ifplabel,
1432    struct mbuf *m, struct label *mlabel)
1433{
1434	struct mac_biba *dest;
1435
1436	dest = SLOT(mlabel);
1437
1438	biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
1439}
1440
1441static int
1442biba_pipe_check_ioctl(struct ucred *cred, struct pipepair *pp,
1443    struct label *pplabel, unsigned long cmd, void /* caddr_t */ *data)
1444{
1445
1446	if(!biba_enabled)
1447		return (0);
1448
1449	/* XXX: This will be implemented soon... */
1450
1451	return (0);
1452}
1453
1454static int
1455biba_pipe_check_poll(struct ucred *cred, struct pipepair *pp,
1456    struct label *pplabel)
1457{
1458	struct mac_biba *subj, *obj;
1459
1460	if (!biba_enabled)
1461		return (0);
1462
1463	subj = SLOT(cred->cr_label);
1464	obj = SLOT(pplabel);
1465
1466	if (!biba_dominate_effective(obj, subj))
1467		return (EACCES);
1468
1469	return (0);
1470}
1471
1472static int
1473biba_pipe_check_read(struct ucred *cred, struct pipepair *pp,
1474    struct label *pplabel)
1475{
1476	struct mac_biba *subj, *obj;
1477
1478	if (!biba_enabled)
1479		return (0);
1480
1481	subj = SLOT(cred->cr_label);
1482	obj = SLOT(pplabel);
1483
1484	if (!biba_dominate_effective(obj, subj))
1485		return (EACCES);
1486
1487	return (0);
1488}
1489
1490static int
1491biba_pipe_check_relabel(struct ucred *cred, struct pipepair *pp,
1492    struct label *pplabel, struct label *newlabel)
1493{
1494	struct mac_biba *subj, *obj, *new;
1495	int error;
1496
1497	new = SLOT(newlabel);
1498	subj = SLOT(cred->cr_label);
1499	obj = SLOT(pplabel);
1500
1501	/*
1502	 * If there is a Biba label update for a pipe, it must be a effective
1503	 * update.
1504	 */
1505	error = biba_atmostflags(new, MAC_BIBA_FLAG_EFFECTIVE);
1506	if (error)
1507		return (error);
1508
1509	/*
1510	 * To perform a relabel of a pipe (Biba label or not), Biba must
1511	 * authorize the relabel.
1512	 */
1513	if (!biba_effective_in_range(obj, subj))
1514		return (EPERM);
1515
1516	/*
1517	 * If the Biba label is to be changed, authorize as appropriate.
1518	 */
1519	if (new->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) {
1520		/*
1521		 * To change the Biba label on a pipe, the new pipe label
1522		 * must be in the subject range.
1523		 */
1524		if (!biba_effective_in_range(new, subj))
1525			return (EPERM);
1526
1527		/*
1528		 * To change the Biba label on a pipe to be EQUAL, the
1529		 * subject must have appropriate privilege.
1530		 */
1531		if (biba_contains_equal(new)) {
1532			error = biba_subject_privileged(subj);
1533			if (error)
1534				return (error);
1535		}
1536	}
1537
1538	return (0);
1539}
1540
1541static int
1542biba_pipe_check_stat(struct ucred *cred, struct pipepair *pp,
1543    struct label *pplabel)
1544{
1545	struct mac_biba *subj, *obj;
1546
1547	if (!biba_enabled)
1548		return (0);
1549
1550	subj = SLOT(cred->cr_label);
1551	obj = SLOT(pplabel);
1552
1553	if (!biba_dominate_effective(obj, subj))
1554		return (EACCES);
1555
1556	return (0);
1557}
1558
1559static int
1560biba_pipe_check_write(struct ucred *cred, struct pipepair *pp,
1561    struct label *pplabel)
1562{
1563	struct mac_biba *subj, *obj;
1564
1565	if (!biba_enabled)
1566		return (0);
1567
1568	subj = SLOT(cred->cr_label);
1569	obj = SLOT(pplabel);
1570
1571	if (!biba_dominate_effective(subj, obj))
1572		return (EACCES);
1573
1574	return (0);
1575}
1576
1577static void
1578biba_pipe_create(struct ucred *cred, struct pipepair *pp,
1579    struct label *pplabel)
1580{
1581	struct mac_biba *source, *dest;
1582
1583	source = SLOT(cred->cr_label);
1584	dest = SLOT(pplabel);
1585
1586	biba_copy_effective(source, dest);
1587}
1588
1589static void
1590biba_pipe_relabel(struct ucred *cred, struct pipepair *pp,
1591    struct label *pplabel, struct label *newlabel)
1592{
1593	struct mac_biba *source, *dest;
1594
1595	source = SLOT(newlabel);
1596	dest = SLOT(pplabel);
1597
1598	biba_copy(source, dest);
1599}
1600
1601static int
1602biba_posixsem_check_openunlink(struct ucred *cred, struct ksem *ks,
1603    struct label *kslabel)
1604{
1605	struct mac_biba *subj, *obj;
1606
1607	if (!biba_enabled)
1608		return (0);
1609
1610	subj = SLOT(cred->cr_label);
1611	obj = SLOT(kslabel);
1612
1613	if (!biba_dominate_effective(subj, obj))
1614		return (EACCES);
1615
1616	return (0);
1617}
1618
1619static int
1620biba_posixsem_check_write(struct ucred *active_cred, struct ucred *file_cred,
1621    struct ksem *ks, struct label *kslabel)
1622{
1623	struct mac_biba *subj, *obj;
1624
1625	if (!biba_enabled)
1626		return (0);
1627
1628	subj = SLOT(active_cred->cr_label);
1629	obj = SLOT(kslabel);
1630
1631	if (!biba_dominate_effective(subj, obj))
1632		return (EACCES);
1633
1634	return (0);
1635}
1636
1637static int
1638biba_posixsem_check_rdonly(struct ucred *active_cred, struct ucred *file_cred,
1639    struct ksem *ks, struct label *kslabel)
1640{
1641	struct mac_biba *subj, *obj;
1642
1643	if (!biba_enabled)
1644		return (0);
1645
1646	subj = SLOT(active_cred->cr_label);
1647	obj = SLOT(kslabel);
1648
1649	if (!biba_dominate_effective(obj, subj))
1650		return (EACCES);
1651
1652	return (0);
1653}
1654
1655static void
1656biba_posixsem_create(struct ucred *cred, struct ksem *ks,
1657    struct label *kslabel)
1658{
1659	struct mac_biba *source, *dest;
1660
1661	source = SLOT(cred->cr_label);
1662	dest = SLOT(kslabel);
1663
1664	biba_copy_effective(source, dest);
1665}
1666
1667/*
1668 * Some system privileges are allowed regardless of integrity grade; others
1669 * are allowed only when running with privilege with respect to the Biba
1670 * policy as they might otherwise allow bypassing of the integrity policy.
1671 */
1672static int
1673biba_priv_check(struct ucred *cred, int priv)
1674{
1675	struct mac_biba *subj;
1676	int error;
1677
1678	if (!biba_enabled)
1679		return (0);
1680
1681	/*
1682	 * Exempt only specific privileges from the Biba integrity policy.
1683	 */
1684	switch (priv) {
1685	case PRIV_KTRACE:
1686	case PRIV_MSGBUF:
1687
1688	/*
1689	 * Allow processes to manipulate basic process audit properties, and
1690	 * to submit audit records.
1691	 */
1692	case PRIV_AUDIT_GETAUDIT:
1693	case PRIV_AUDIT_SETAUDIT:
1694	case PRIV_AUDIT_SUBMIT:
1695
1696	/*
1697	 * Allow processes to manipulate their regular UNIX credentials.
1698	 */
1699	case PRIV_CRED_SETUID:
1700	case PRIV_CRED_SETEUID:
1701	case PRIV_CRED_SETGID:
1702	case PRIV_CRED_SETEGID:
1703	case PRIV_CRED_SETGROUPS:
1704	case PRIV_CRED_SETREUID:
1705	case PRIV_CRED_SETREGID:
1706	case PRIV_CRED_SETRESUID:
1707	case PRIV_CRED_SETRESGID:
1708
1709	/*
1710	 * Allow processes to perform system monitoring.
1711	 */
1712	case PRIV_SEEOTHERGIDS:
1713	case PRIV_SEEOTHERUIDS:
1714		break;
1715
1716	/*
1717	 * Allow access to general process debugging facilities.  We
1718	 * separately control debugging based on MAC label.
1719	 */
1720	case PRIV_DEBUG_DIFFCRED:
1721	case PRIV_DEBUG_SUGID:
1722	case PRIV_DEBUG_UNPRIV:
1723
1724	/*
1725	 * Allow manipulating jails.
1726	 */
1727	case PRIV_JAIL_ATTACH:
1728
1729	/*
1730	 * Allow privilege with respect to the Partition policy, but not the
1731	 * Privs policy.
1732	 */
1733	case PRIV_MAC_PARTITION:
1734
1735	/*
1736	 * Allow privilege with respect to process resource limits and login
1737	 * context.
1738	 */
1739	case PRIV_PROC_LIMIT:
1740	case PRIV_PROC_SETLOGIN:
1741	case PRIV_PROC_SETRLIMIT:
1742
1743	/*
1744	 * Allow System V and POSIX IPC privileges.
1745	 */
1746	case PRIV_IPC_READ:
1747	case PRIV_IPC_WRITE:
1748	case PRIV_IPC_ADMIN:
1749	case PRIV_IPC_MSGSIZE:
1750	case PRIV_MQ_ADMIN:
1751
1752	/*
1753	 * Allow certain scheduler manipulations -- possibly this should be
1754	 * controlled by more fine-grained policy, as potentially low
1755	 * integrity processes can deny CPU to higher integrity ones.
1756	 */
1757	case PRIV_SCHED_DIFFCRED:
1758	case PRIV_SCHED_SETPRIORITY:
1759	case PRIV_SCHED_RTPRIO:
1760	case PRIV_SCHED_SETPOLICY:
1761	case PRIV_SCHED_SET:
1762	case PRIV_SCHED_SETPARAM:
1763
1764	/*
1765	 * More IPC privileges.
1766	 */
1767	case PRIV_SEM_WRITE:
1768
1769	/*
1770	 * Allow signaling privileges subject to integrity policy.
1771	 */
1772	case PRIV_SIGNAL_DIFFCRED:
1773	case PRIV_SIGNAL_SUGID:
1774
1775	/*
1776	 * Allow access to only limited sysctls from lower integrity levels;
1777	 * piggy-back on the Jail definition.
1778	 */
1779	case PRIV_SYSCTL_WRITEJAIL:
1780
1781	/*
1782	 * Allow TTY-based privileges, subject to general device access using
1783	 * labels on TTY device nodes, but not console privilege.
1784	 */
1785	case PRIV_TTY_DRAINWAIT:
1786	case PRIV_TTY_DTRWAIT:
1787	case PRIV_TTY_EXCLUSIVE:
1788	case PRIV_TTY_PRISON:
1789	case PRIV_TTY_STI:
1790	case PRIV_TTY_SETA:
1791
1792	/*
1793	 * Grant most VFS privileges, as almost all are in practice bounded
1794	 * by more specific checks using labels.
1795	 */
1796	case PRIV_VFS_READ:
1797	case PRIV_VFS_WRITE:
1798	case PRIV_VFS_ADMIN:
1799	case PRIV_VFS_EXEC:
1800	case PRIV_VFS_LOOKUP:
1801	case PRIV_VFS_CHFLAGS_DEV:
1802	case PRIV_VFS_CHOWN:
1803	case PRIV_VFS_CHROOT:
1804	case PRIV_VFS_RETAINSUGID:
1805	case PRIV_VFS_EXCEEDQUOTA:
1806	case PRIV_VFS_FCHROOT:
1807	case PRIV_VFS_FHOPEN:
1808	case PRIV_VFS_FHSTATFS:
1809	case PRIV_VFS_GENERATION:
1810	case PRIV_VFS_GETFH:
1811	case PRIV_VFS_GETQUOTA:
1812	case PRIV_VFS_LINK:
1813	case PRIV_VFS_MOUNT:
1814	case PRIV_VFS_MOUNT_OWNER:
1815	case PRIV_VFS_MOUNT_PERM:
1816	case PRIV_VFS_MOUNT_SUIDDIR:
1817	case PRIV_VFS_MOUNT_NONUSER:
1818	case PRIV_VFS_SETGID:
1819	case PRIV_VFS_STICKYFILE:
1820	case PRIV_VFS_SYSFLAGS:
1821	case PRIV_VFS_UNMOUNT:
1822
1823	/*
1824	 * Allow VM privileges; it would be nice if these were subject to
1825	 * resource limits.
1826	 */
1827	case PRIV_VM_MADV_PROTECT:
1828	case PRIV_VM_MLOCK:
1829	case PRIV_VM_MUNLOCK:
1830
1831	/*
1832	 * Allow some but not all network privileges.  In general, dont allow
1833	 * reconfiguring the network stack, just normal use.
1834	 */
1835	case PRIV_NETATALK_RESERVEDPORT:
1836	case PRIV_NETINET_RESERVEDPORT:
1837	case PRIV_NETINET_RAW:
1838	case PRIV_NETINET_REUSEPORT:
1839	case PRIV_NETIPX_RESERVEDPORT:
1840	case PRIV_NETIPX_RAW:
1841		break;
1842
1843	/*
1844	 * All remaining system privileges are allow only if the process
1845	 * holds privilege with respect to the Biba policy.
1846	 */
1847	default:
1848		subj = SLOT(cred->cr_label);
1849		error = biba_subject_privileged(subj);
1850		if (error)
1851			return (error);
1852	}
1853	return (0);
1854}
1855
1856static int
1857biba_proc_check_debug(struct ucred *cred, struct proc *p)
1858{
1859	struct mac_biba *subj, *obj;
1860
1861	if (!biba_enabled)
1862		return (0);
1863
1864	subj = SLOT(cred->cr_label);
1865	obj = SLOT(p->p_ucred->cr_label);
1866
1867	/* XXX: range checks */
1868	if (!biba_dominate_effective(obj, subj))
1869		return (ESRCH);
1870	if (!biba_dominate_effective(subj, obj))
1871		return (EACCES);
1872
1873	return (0);
1874}
1875
1876static int
1877biba_proc_check_sched(struct ucred *cred, struct proc *p)
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(p->p_ucred->cr_label);
1886
1887	/* XXX: range checks */
1888	if (!biba_dominate_effective(obj, subj))
1889		return (ESRCH);
1890	if (!biba_dominate_effective(subj, obj))
1891		return (EACCES);
1892
1893	return (0);
1894}
1895
1896static int
1897biba_proc_check_signal(struct ucred *cred, struct proc *p, int signum)
1898{
1899	struct mac_biba *subj, *obj;
1900
1901	if (!biba_enabled)
1902		return (0);
1903
1904	subj = SLOT(cred->cr_label);
1905	obj = SLOT(p->p_ucred->cr_label);
1906
1907	/* XXX: range checks */
1908	if (!biba_dominate_effective(obj, subj))
1909		return (ESRCH);
1910	if (!biba_dominate_effective(subj, obj))
1911		return (EACCES);
1912
1913	return (0);
1914}
1915
1916static int
1917biba_socket_check_deliver(struct socket *so, struct label *solabel,
1918    struct mbuf *m, struct label *mlabel)
1919{
1920	struct mac_biba *p, *s;
1921
1922	if (!biba_enabled)
1923		return (0);
1924
1925	p = SLOT(mlabel);
1926	s = SLOT(solabel);
1927
1928	return (biba_equal_effective(p, s) ? 0 : EACCES);
1929}
1930
1931static int
1932biba_socket_check_relabel(struct ucred *cred, struct socket *so,
1933    struct label *solabel, struct label *newlabel)
1934{
1935	struct mac_biba *subj, *obj, *new;
1936	int error;
1937
1938	new = SLOT(newlabel);
1939	subj = SLOT(cred->cr_label);
1940	obj = SLOT(solabel);
1941
1942	/*
1943	 * If there is a Biba label update for the socket, it may be an
1944	 * update of effective.
1945	 */
1946	error = biba_atmostflags(new, MAC_BIBA_FLAG_EFFECTIVE);
1947	if (error)
1948		return (error);
1949
1950	/*
1951	 * To relabel a socket, the old socket effective must be in the
1952	 * subject range.
1953	 */
1954	if (!biba_effective_in_range(obj, subj))
1955		return (EPERM);
1956
1957	/*
1958	 * If the Biba label is to be changed, authorize as appropriate.
1959	 */
1960	if (new->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) {
1961		/*
1962		 * To relabel a socket, the new socket effective must be in
1963		 * the subject range.
1964		 */
1965		if (!biba_effective_in_range(new, subj))
1966			return (EPERM);
1967
1968		/*
1969		 * To change the Biba label on the socket to contain EQUAL,
1970		 * the subject must have appropriate privilege.
1971		 */
1972		if (biba_contains_equal(new)) {
1973			error = biba_subject_privileged(subj);
1974			if (error)
1975				return (error);
1976		}
1977	}
1978
1979	return (0);
1980}
1981
1982static int
1983biba_socket_check_visible(struct ucred *cred, struct socket *so,
1984    struct label *solabel)
1985{
1986	struct mac_biba *subj, *obj;
1987
1988	if (!biba_enabled)
1989		return (0);
1990
1991	subj = SLOT(cred->cr_label);
1992	obj = SLOT(solabel);
1993
1994	if (!biba_dominate_effective(obj, subj))
1995		return (ENOENT);
1996
1997	return (0);
1998}
1999
2000static void
2001biba_socket_create(struct ucred *cred, struct socket *so,
2002    struct label *solabel)
2003{
2004	struct mac_biba *source, *dest;
2005
2006	source = SLOT(cred->cr_label);
2007	dest = SLOT(solabel);
2008
2009	biba_copy_effective(source, dest);
2010}
2011
2012static void
2013biba_socket_create_mbuf(struct socket *so, struct label *solabel,
2014    struct mbuf *m, struct label *mlabel)
2015{
2016	struct mac_biba *source, *dest;
2017
2018	source = SLOT(solabel);
2019	dest = SLOT(mlabel);
2020
2021	biba_copy_effective(source, dest);
2022}
2023
2024static void
2025biba_socket_newconn(struct socket *oldso, struct label *oldsolabel,
2026    struct socket *newso, struct label *newsolabel)
2027{
2028	struct mac_biba *source, *dest;
2029
2030	source = SLOT(oldsolabel);
2031	dest = SLOT(newsolabel);
2032
2033	biba_copy_effective(source, dest);
2034}
2035
2036static void
2037biba_socket_relabel(struct ucred *cred, struct socket *so,
2038    struct label *solabel, struct label *newlabel)
2039{
2040	struct mac_biba *source, *dest;
2041
2042	source = SLOT(newlabel);
2043	dest = SLOT(solabel);
2044
2045	biba_copy(source, dest);
2046}
2047
2048static void
2049biba_socketpeer_set_from_mbuf(struct mbuf *m, struct label *mlabel,
2050    struct socket *so, struct label *sopeerlabel)
2051{
2052	struct mac_biba *source, *dest;
2053
2054	source = SLOT(mlabel);
2055	dest = SLOT(sopeerlabel);
2056
2057	biba_copy_effective(source, dest);
2058}
2059
2060static void
2061biba_socketpeer_set_from_socket(struct socket *oldso,
2062    struct label *oldsolabel, struct socket *newso,
2063    struct label *newsopeerlabel)
2064{
2065	struct mac_biba *source, *dest;
2066
2067	source = SLOT(oldsolabel);
2068	dest = SLOT(newsopeerlabel);
2069
2070	biba_copy_effective(source, dest);
2071}
2072
2073static void
2074biba_syncache_create(struct label *label, struct inpcb *inp)
2075{
2076	struct mac_biba *source, *dest;
2077
2078	source = SLOT(inp->inp_label);
2079	dest = SLOT(label);
2080	biba_copy_effective(source, dest);
2081}
2082
2083static void
2084biba_syncache_create_mbuf(struct label *sc_label, struct mbuf *m,
2085    struct label *mlabel)
2086{
2087	struct mac_biba *source, *dest;
2088
2089	source = SLOT(sc_label);
2090	dest = SLOT(mlabel);
2091	biba_copy_effective(source, dest);
2092}
2093
2094static int
2095biba_system_check_acct(struct ucred *cred, struct vnode *vp,
2096    struct label *vplabel)
2097{
2098	struct mac_biba *subj, *obj;
2099	int error;
2100
2101	if (!biba_enabled)
2102		return (0);
2103
2104	subj = SLOT(cred->cr_label);
2105
2106	error = biba_subject_privileged(subj);
2107	if (error)
2108		return (error);
2109
2110	if (vplabel == NULL)
2111		return (0);
2112
2113	obj = SLOT(vplabel);
2114	if (!biba_high_effective(obj))
2115		return (EACCES);
2116
2117	return (0);
2118}
2119
2120static int
2121biba_system_check_auditctl(struct ucred *cred, struct vnode *vp,
2122    struct label *vplabel)
2123{
2124	struct mac_biba *subj, *obj;
2125	int error;
2126
2127	if (!biba_enabled)
2128		return (0);
2129
2130	subj = SLOT(cred->cr_label);
2131
2132	error = biba_subject_privileged(subj);
2133	if (error)
2134		return (error);
2135
2136	if (vplabel == NULL)
2137		return (0);
2138
2139	obj = SLOT(vplabel);
2140	if (!biba_high_effective(obj))
2141		return (EACCES);
2142
2143	return (0);
2144}
2145
2146static int
2147biba_system_check_auditon(struct ucred *cred, int cmd)
2148{
2149	struct mac_biba *subj;
2150	int error;
2151
2152	if (!biba_enabled)
2153		return (0);
2154
2155	subj = SLOT(cred->cr_label);
2156
2157	error = biba_subject_privileged(subj);
2158	if (error)
2159		return (error);
2160
2161	return (0);
2162}
2163
2164static int
2165biba_system_check_swapoff(struct ucred *cred, struct vnode *vp,
2166    struct label *label)
2167{
2168	struct mac_biba *subj;
2169	int error;
2170
2171	if (!biba_enabled)
2172		return (0);
2173
2174	subj = SLOT(cred->cr_label);
2175
2176	error = biba_subject_privileged(subj);
2177	if (error)
2178		return (error);
2179
2180	return (0);
2181}
2182
2183static int
2184biba_system_check_swapon(struct ucred *cred, struct vnode *vp,
2185    struct label *vplabel)
2186{
2187	struct mac_biba *subj, *obj;
2188	int error;
2189
2190	if (!biba_enabled)
2191		return (0);
2192
2193	subj = SLOT(cred->cr_label);
2194	obj = SLOT(vplabel);
2195
2196	error = biba_subject_privileged(subj);
2197	if (error)
2198		return (error);
2199
2200	if (!biba_high_effective(obj))
2201		return (EACCES);
2202
2203	return (0);
2204}
2205
2206static int
2207biba_system_check_sysctl(struct ucred *cred, struct sysctl_oid *oidp,
2208    void *arg1, int arg2, struct sysctl_req *req)
2209{
2210	struct mac_biba *subj;
2211	int error;
2212
2213	if (!biba_enabled)
2214		return (0);
2215
2216	subj = SLOT(cred->cr_label);
2217
2218	/*
2219	 * Treat sysctl variables without CTLFLAG_ANYBODY flag as biba/high,
2220	 * but also require privilege to change them.
2221	 */
2222	if (req->newptr != NULL && (oidp->oid_kind & CTLFLAG_ANYBODY) == 0) {
2223		if (!biba_subject_dominate_high(subj))
2224			return (EACCES);
2225
2226		error = biba_subject_privileged(subj);
2227		if (error)
2228			return (error);
2229	}
2230
2231	return (0);
2232}
2233
2234static void
2235biba_sysvmsg_cleanup(struct label *msglabel)
2236{
2237
2238	bzero(SLOT(msglabel), sizeof(struct mac_biba));
2239}
2240
2241static void
2242biba_sysvmsg_create(struct ucred *cred, struct msqid_kernel *msqkptr,
2243    struct label *msqlabel, struct msg *msgptr, struct label *msglabel)
2244{
2245	struct mac_biba *source, *dest;
2246
2247	/* Ignore the msgq label */
2248	source = SLOT(cred->cr_label);
2249	dest = SLOT(msglabel);
2250
2251	biba_copy_effective(source, dest);
2252}
2253
2254static int
2255biba_sysvmsq_check_msgrcv(struct ucred *cred, struct msg *msgptr,
2256    struct label *msglabel)
2257{
2258	struct mac_biba *subj, *obj;
2259
2260	if (!biba_enabled)
2261		return (0);
2262
2263	subj = SLOT(cred->cr_label);
2264	obj = SLOT(msglabel);
2265
2266	if (!biba_dominate_effective(obj, subj))
2267		return (EACCES);
2268
2269	return (0);
2270}
2271
2272static int
2273biba_sysvmsq_check_msgrmid(struct ucred *cred, struct msg *msgptr,
2274    struct label *msglabel)
2275{
2276	struct mac_biba *subj, *obj;
2277
2278	if (!biba_enabled)
2279		return (0);
2280
2281	subj = SLOT(cred->cr_label);
2282	obj = SLOT(msglabel);
2283
2284	if (!biba_dominate_effective(subj, obj))
2285		return (EACCES);
2286
2287	return (0);
2288}
2289
2290static int
2291biba_sysvmsq_check_msqget(struct ucred *cred, struct msqid_kernel *msqkptr,
2292    struct label *msqklabel)
2293{
2294	struct mac_biba *subj, *obj;
2295
2296	if (!biba_enabled)
2297		return (0);
2298
2299	subj = SLOT(cred->cr_label);
2300	obj = SLOT(msqklabel);
2301
2302	if (!biba_dominate_effective(obj, subj))
2303		return (EACCES);
2304
2305	return (0);
2306}
2307
2308static int
2309biba_sysvmsq_check_msqsnd(struct ucred *cred, struct msqid_kernel *msqkptr,
2310    struct label *msqklabel)
2311{
2312	struct mac_biba *subj, *obj;
2313
2314	if (!biba_enabled)
2315		return (0);
2316
2317	subj = SLOT(cred->cr_label);
2318	obj = SLOT(msqklabel);
2319
2320	if (!biba_dominate_effective(subj, obj))
2321		return (EACCES);
2322
2323	return (0);
2324}
2325
2326static int
2327biba_sysvmsq_check_msqrcv(struct ucred *cred, struct msqid_kernel *msqkptr,
2328    struct label *msqklabel)
2329{
2330	struct mac_biba *subj, *obj;
2331
2332	if (!biba_enabled)
2333		return (0);
2334
2335	subj = SLOT(cred->cr_label);
2336	obj = SLOT(msqklabel);
2337
2338	if (!biba_dominate_effective(obj, subj))
2339		return (EACCES);
2340
2341	return (0);
2342}
2343
2344static int
2345biba_sysvmsq_check_msqctl(struct ucred *cred, struct msqid_kernel *msqkptr,
2346    struct label *msqklabel, int cmd)
2347{
2348	struct mac_biba *subj, *obj;
2349
2350	if (!biba_enabled)
2351		return (0);
2352
2353	subj = SLOT(cred->cr_label);
2354	obj = SLOT(msqklabel);
2355
2356	switch(cmd) {
2357	case IPC_RMID:
2358	case IPC_SET:
2359		if (!biba_dominate_effective(subj, obj))
2360			return (EACCES);
2361		break;
2362
2363	case IPC_STAT:
2364		if (!biba_dominate_effective(obj, subj))
2365			return (EACCES);
2366		break;
2367
2368	default:
2369		return (EACCES);
2370	}
2371
2372	return (0);
2373}
2374
2375static void
2376biba_sysvmsq_cleanup(struct label *msqlabel)
2377{
2378
2379	bzero(SLOT(msqlabel), sizeof(struct mac_biba));
2380}
2381
2382static void
2383biba_sysvmsq_create(struct ucred *cred, struct msqid_kernel *msqkptr,
2384    struct label *msqlabel)
2385{
2386	struct mac_biba *source, *dest;
2387
2388	source = SLOT(cred->cr_label);
2389	dest = SLOT(msqlabel);
2390
2391	biba_copy_effective(source, dest);
2392}
2393
2394static int
2395biba_sysvsem_check_semctl(struct ucred *cred, struct semid_kernel *semakptr,
2396    struct label *semaklabel, int cmd)
2397{
2398	struct mac_biba *subj, *obj;
2399
2400	if (!biba_enabled)
2401		return (0);
2402
2403	subj = SLOT(cred->cr_label);
2404	obj = SLOT(semaklabel);
2405
2406	switch(cmd) {
2407	case IPC_RMID:
2408	case IPC_SET:
2409	case SETVAL:
2410	case SETALL:
2411		if (!biba_dominate_effective(subj, obj))
2412			return (EACCES);
2413		break;
2414
2415	case IPC_STAT:
2416	case GETVAL:
2417	case GETPID:
2418	case GETNCNT:
2419	case GETZCNT:
2420	case GETALL:
2421		if (!biba_dominate_effective(obj, subj))
2422			return (EACCES);
2423		break;
2424
2425	default:
2426		return (EACCES);
2427	}
2428
2429	return (0);
2430}
2431
2432static int
2433biba_sysvsem_check_semget(struct ucred *cred, struct semid_kernel *semakptr,
2434    struct label *semaklabel)
2435{
2436	struct mac_biba *subj, *obj;
2437
2438	if (!biba_enabled)
2439		return (0);
2440
2441	subj = SLOT(cred->cr_label);
2442	obj = SLOT(semaklabel);
2443
2444	if (!biba_dominate_effective(obj, subj))
2445		return (EACCES);
2446
2447	return (0);
2448}
2449
2450static int
2451biba_sysvsem_check_semop(struct ucred *cred, struct semid_kernel *semakptr,
2452    struct label *semaklabel, size_t accesstype)
2453{
2454	struct mac_biba *subj, *obj;
2455
2456	if (!biba_enabled)
2457		return (0);
2458
2459	subj = SLOT(cred->cr_label);
2460	obj = SLOT(semaklabel);
2461
2462	if (accesstype & SEM_R)
2463		if (!biba_dominate_effective(obj, subj))
2464			return (EACCES);
2465
2466	if (accesstype & SEM_A)
2467		if (!biba_dominate_effective(subj, obj))
2468			return (EACCES);
2469
2470	return (0);
2471}
2472
2473static void
2474biba_sysvsem_cleanup(struct label *semalabel)
2475{
2476
2477	bzero(SLOT(semalabel), sizeof(struct mac_biba));
2478}
2479
2480static void
2481biba_sysvsem_create(struct ucred *cred, struct semid_kernel *semakptr,
2482    struct label *semalabel)
2483{
2484	struct mac_biba *source, *dest;
2485
2486	source = SLOT(cred->cr_label);
2487	dest = SLOT(semalabel);
2488
2489	biba_copy_effective(source, dest);
2490}
2491
2492static int
2493biba_sysvshm_check_shmat(struct ucred *cred, struct shmid_kernel *shmsegptr,
2494    struct label *shmseglabel, int shmflg)
2495{
2496	struct mac_biba *subj, *obj;
2497
2498	if (!biba_enabled)
2499		return (0);
2500
2501	subj = SLOT(cred->cr_label);
2502	obj = SLOT(shmseglabel);
2503
2504	if (!biba_dominate_effective(obj, subj))
2505		return (EACCES);
2506	if ((shmflg & SHM_RDONLY) == 0) {
2507		if (!biba_dominate_effective(subj, obj))
2508			return (EACCES);
2509	}
2510
2511	return (0);
2512}
2513
2514static int
2515biba_sysvshm_check_shmctl(struct ucred *cred, struct shmid_kernel *shmsegptr,
2516    struct label *shmseglabel, int cmd)
2517{
2518	struct mac_biba *subj, *obj;
2519
2520	if (!biba_enabled)
2521		return (0);
2522
2523	subj = SLOT(cred->cr_label);
2524	obj = SLOT(shmseglabel);
2525
2526	switch(cmd) {
2527	case IPC_RMID:
2528	case IPC_SET:
2529		if (!biba_dominate_effective(subj, obj))
2530			return (EACCES);
2531		break;
2532
2533	case IPC_STAT:
2534	case SHM_STAT:
2535		if (!biba_dominate_effective(obj, subj))
2536			return (EACCES);
2537		break;
2538
2539	default:
2540		return (EACCES);
2541	}
2542
2543	return (0);
2544}
2545
2546static int
2547biba_sysvshm_check_shmget(struct ucred *cred, struct shmid_kernel *shmsegptr,
2548    struct label *shmseglabel, int shmflg)
2549{
2550	struct mac_biba *subj, *obj;
2551
2552	if (!biba_enabled)
2553		return (0);
2554
2555	subj = SLOT(cred->cr_label);
2556	obj = SLOT(shmseglabel);
2557
2558	if (!biba_dominate_effective(obj, subj))
2559		return (EACCES);
2560
2561	return (0);
2562}
2563
2564static void
2565biba_sysvshm_cleanup(struct label *shmlabel)
2566{
2567
2568	bzero(SLOT(shmlabel), sizeof(struct mac_biba));
2569}
2570
2571static void
2572biba_sysvshm_create(struct ucred *cred, struct shmid_kernel *shmsegptr,
2573    struct label *shmlabel)
2574{
2575	struct mac_biba *source, *dest;
2576
2577	source = SLOT(cred->cr_label);
2578	dest = SLOT(shmlabel);
2579
2580	biba_copy_effective(source, dest);
2581}
2582
2583static int
2584biba_vnode_associate_extattr(struct mount *mp, struct label *mplabel,
2585    struct vnode *vp, struct label *vplabel)
2586{
2587	struct mac_biba mb_temp, *source, *dest;
2588	int buflen, error;
2589
2590	source = SLOT(mplabel);
2591	dest = SLOT(vplabel);
2592
2593	buflen = sizeof(mb_temp);
2594	bzero(&mb_temp, buflen);
2595
2596	error = vn_extattr_get(vp, IO_NODELOCKED, MAC_BIBA_EXTATTR_NAMESPACE,
2597	    MAC_BIBA_EXTATTR_NAME, &buflen, (char *) &mb_temp, curthread);
2598	if (error == ENOATTR || error == EOPNOTSUPP) {
2599		/* Fall back to the mntlabel. */
2600		biba_copy_effective(source, dest);
2601		return (0);
2602	} else if (error)
2603		return (error);
2604
2605	if (buflen != sizeof(mb_temp)) {
2606		printf("biba_vnode_associate_extattr: bad size %d\n",
2607		    buflen);
2608		return (EPERM);
2609	}
2610	if (biba_valid(&mb_temp) != 0) {
2611		printf("biba_vnode_associate_extattr: invalid\n");
2612		return (EPERM);
2613	}
2614	if ((mb_temp.mb_flags & MAC_BIBA_FLAGS_BOTH) !=
2615	    MAC_BIBA_FLAG_EFFECTIVE) {
2616		printf("biba_vnode_associate_extattr: not effective\n");
2617		return (EPERM);
2618	}
2619
2620	biba_copy_effective(&mb_temp, dest);
2621	return (0);
2622}
2623
2624static void
2625biba_vnode_associate_singlelabel(struct mount *mp, struct label *mplabel,
2626    struct vnode *vp, struct label *vplabel)
2627{
2628	struct mac_biba *source, *dest;
2629
2630	source = SLOT(mplabel);
2631	dest = SLOT(vplabel);
2632
2633	biba_copy_effective(source, dest);
2634}
2635
2636static int
2637biba_vnode_check_chdir(struct ucred *cred, struct vnode *dvp,
2638    struct label *dvplabel)
2639{
2640	struct mac_biba *subj, *obj;
2641
2642	if (!biba_enabled)
2643		return (0);
2644
2645	subj = SLOT(cred->cr_label);
2646	obj = SLOT(dvplabel);
2647
2648	if (!biba_dominate_effective(obj, subj))
2649		return (EACCES);
2650
2651	return (0);
2652}
2653
2654static int
2655biba_vnode_check_chroot(struct ucred *cred, struct vnode *dvp,
2656    struct label *dvplabel)
2657{
2658	struct mac_biba *subj, *obj;
2659
2660	if (!biba_enabled)
2661		return (0);
2662
2663	subj = SLOT(cred->cr_label);
2664	obj = SLOT(dvplabel);
2665
2666	if (!biba_dominate_effective(obj, subj))
2667		return (EACCES);
2668
2669	return (0);
2670}
2671
2672static int
2673biba_vnode_check_create(struct ucred *cred, struct vnode *dvp,
2674    struct label *dvplabel, struct componentname *cnp, struct vattr *vap)
2675{
2676	struct mac_biba *subj, *obj;
2677
2678	if (!biba_enabled)
2679		return (0);
2680
2681	subj = SLOT(cred->cr_label);
2682	obj = SLOT(dvplabel);
2683
2684	if (!biba_dominate_effective(subj, obj))
2685		return (EACCES);
2686
2687	return (0);
2688}
2689
2690static int
2691biba_vnode_check_deleteacl(struct ucred *cred, struct vnode *vp,
2692    struct label *vplabel, acl_type_t type)
2693{
2694	struct mac_biba *subj, *obj;
2695
2696	if (!biba_enabled)
2697		return (0);
2698
2699	subj = SLOT(cred->cr_label);
2700	obj = SLOT(vplabel);
2701
2702	if (!biba_dominate_effective(subj, obj))
2703		return (EACCES);
2704
2705	return (0);
2706}
2707
2708static int
2709biba_vnode_check_deleteextattr(struct ucred *cred, struct vnode *vp,
2710    struct label *vplabel, int attrnamespace, const char *name)
2711{
2712	struct mac_biba *subj, *obj;
2713
2714	if (!biba_enabled)
2715		return (0);
2716
2717	subj = SLOT(cred->cr_label);
2718	obj = SLOT(vplabel);
2719
2720	if (!biba_dominate_effective(subj, obj))
2721		return (EACCES);
2722
2723	return (0);
2724}
2725
2726static int
2727biba_vnode_check_exec(struct ucred *cred, struct vnode *vp,
2728    struct label *vplabel, struct image_params *imgp,
2729    struct label *execlabel)
2730{
2731	struct mac_biba *subj, *obj, *exec;
2732	int error;
2733
2734	if (execlabel != NULL) {
2735		/*
2736		 * We currently don't permit labels to be changed at
2737		 * exec-time as part of Biba, so disallow non-NULL Biba label
2738		 * elements in the execlabel.
2739		 */
2740		exec = SLOT(execlabel);
2741		error = biba_atmostflags(exec, 0);
2742		if (error)
2743			return (error);
2744	}
2745
2746	if (!biba_enabled)
2747		return (0);
2748
2749	subj = SLOT(cred->cr_label);
2750	obj = SLOT(vplabel);
2751
2752	if (!biba_dominate_effective(obj, subj))
2753		return (EACCES);
2754
2755	return (0);
2756}
2757
2758static int
2759biba_vnode_check_getacl(struct ucred *cred, struct vnode *vp,
2760    struct label *vplabel, acl_type_t type)
2761{
2762	struct mac_biba *subj, *obj;
2763
2764	if (!biba_enabled)
2765		return (0);
2766
2767	subj = SLOT(cred->cr_label);
2768	obj = SLOT(vplabel);
2769
2770	if (!biba_dominate_effective(obj, subj))
2771		return (EACCES);
2772
2773	return (0);
2774}
2775
2776static int
2777biba_vnode_check_getextattr(struct ucred *cred, struct vnode *vp,
2778    struct label *vplabel, int attrnamespace, const char *name,
2779    struct uio *uio)
2780{
2781	struct mac_biba *subj, *obj;
2782
2783	if (!biba_enabled)
2784		return (0);
2785
2786	subj = SLOT(cred->cr_label);
2787	obj = SLOT(vplabel);
2788
2789	if (!biba_dominate_effective(obj, subj))
2790		return (EACCES);
2791
2792	return (0);
2793}
2794
2795static int
2796biba_vnode_check_link(struct ucred *cred, struct vnode *dvp,
2797    struct label *dvplabel, struct vnode *vp, struct label *vplabel,
2798    struct componentname *cnp)
2799{
2800	struct mac_biba *subj, *obj;
2801
2802	if (!biba_enabled)
2803		return (0);
2804
2805	subj = SLOT(cred->cr_label);
2806	obj = SLOT(dvplabel);
2807
2808	if (!biba_dominate_effective(subj, obj))
2809		return (EACCES);
2810
2811	obj = SLOT(vplabel);
2812
2813	if (!biba_dominate_effective(subj, obj))
2814		return (EACCES);
2815
2816	return (0);
2817}
2818
2819static int
2820biba_vnode_check_listextattr(struct ucred *cred, struct vnode *vp,
2821    struct label *vplabel, int attrnamespace)
2822{
2823	struct mac_biba *subj, *obj;
2824
2825	if (!biba_enabled)
2826		return (0);
2827
2828	subj = SLOT(cred->cr_label);
2829	obj = SLOT(vplabel);
2830
2831	if (!biba_dominate_effective(obj, subj))
2832		return (EACCES);
2833
2834	return (0);
2835}
2836
2837static int
2838biba_vnode_check_lookup(struct ucred *cred, struct vnode *dvp,
2839    struct label *dvplabel, struct componentname *cnp)
2840{
2841	struct mac_biba *subj, *obj;
2842
2843	if (!biba_enabled)
2844		return (0);
2845
2846	subj = SLOT(cred->cr_label);
2847	obj = SLOT(dvplabel);
2848
2849	if (!biba_dominate_effective(obj, subj))
2850		return (EACCES);
2851
2852	return (0);
2853}
2854
2855static int
2856biba_vnode_check_mmap(struct ucred *cred, struct vnode *vp,
2857    struct label *vplabel, int prot, int flags)
2858{
2859	struct mac_biba *subj, *obj;
2860
2861	/*
2862	 * Rely on the use of open()-time protections to handle
2863	 * non-revocation cases.
2864	 */
2865	if (!biba_enabled || !revocation_enabled)
2866		return (0);
2867
2868	subj = SLOT(cred->cr_label);
2869	obj = SLOT(vplabel);
2870
2871	if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
2872		if (!biba_dominate_effective(obj, subj))
2873			return (EACCES);
2874	}
2875	if (((prot & VM_PROT_WRITE) != 0) && ((flags & MAP_SHARED) != 0)) {
2876		if (!biba_dominate_effective(subj, obj))
2877			return (EACCES);
2878	}
2879
2880	return (0);
2881}
2882
2883static int
2884biba_vnode_check_open(struct ucred *cred, struct vnode *vp,
2885    struct label *vplabel, accmode_t accmode)
2886{
2887	struct mac_biba *subj, *obj;
2888
2889	if (!biba_enabled)
2890		return (0);
2891
2892	subj = SLOT(cred->cr_label);
2893	obj = SLOT(vplabel);
2894
2895	/* XXX privilege override for admin? */
2896	if (accmode & (VREAD | VEXEC | VSTAT)) {
2897		if (!biba_dominate_effective(obj, subj))
2898			return (EACCES);
2899	}
2900	if (accmode & (VWRITE | VAPPEND | VADMIN)) {
2901		if (!biba_dominate_effective(subj, obj))
2902			return (EACCES);
2903	}
2904
2905	return (0);
2906}
2907
2908static int
2909biba_vnode_check_poll(struct ucred *active_cred, struct ucred *file_cred,
2910    struct vnode *vp, struct label *vplabel)
2911{
2912	struct mac_biba *subj, *obj;
2913
2914	if (!biba_enabled || !revocation_enabled)
2915		return (0);
2916
2917	subj = SLOT(active_cred->cr_label);
2918	obj = SLOT(vplabel);
2919
2920	if (!biba_dominate_effective(obj, subj))
2921		return (EACCES);
2922
2923	return (0);
2924}
2925
2926static int
2927biba_vnode_check_read(struct ucred *active_cred, struct ucred *file_cred,
2928    struct vnode *vp, struct label *vplabel)
2929{
2930	struct mac_biba *subj, *obj;
2931
2932	if (!biba_enabled || !revocation_enabled)
2933		return (0);
2934
2935	subj = SLOT(active_cred->cr_label);
2936	obj = SLOT(vplabel);
2937
2938	if (!biba_dominate_effective(obj, subj))
2939		return (EACCES);
2940
2941	return (0);
2942}
2943
2944static int
2945biba_vnode_check_readdir(struct ucred *cred, struct vnode *dvp,
2946    struct label *dvplabel)
2947{
2948	struct mac_biba *subj, *obj;
2949
2950	if (!biba_enabled)
2951		return (0);
2952
2953	subj = SLOT(cred->cr_label);
2954	obj = SLOT(dvplabel);
2955
2956	if (!biba_dominate_effective(obj, subj))
2957		return (EACCES);
2958
2959	return (0);
2960}
2961
2962static int
2963biba_vnode_check_readlink(struct ucred *cred, struct vnode *vp,
2964    struct label *vplabel)
2965{
2966	struct mac_biba *subj, *obj;
2967
2968	if (!biba_enabled)
2969		return (0);
2970
2971	subj = SLOT(cred->cr_label);
2972	obj = SLOT(vplabel);
2973
2974	if (!biba_dominate_effective(obj, subj))
2975		return (EACCES);
2976
2977	return (0);
2978}
2979
2980static int
2981biba_vnode_check_relabel(struct ucred *cred, struct vnode *vp,
2982    struct label *vplabel, struct label *newlabel)
2983{
2984	struct mac_biba *old, *new, *subj;
2985	int error;
2986
2987	old = SLOT(vplabel);
2988	new = SLOT(newlabel);
2989	subj = SLOT(cred->cr_label);
2990
2991	/*
2992	 * If there is a Biba label update for the vnode, it must be a
2993	 * effective label.
2994	 */
2995	error = biba_atmostflags(new, MAC_BIBA_FLAG_EFFECTIVE);
2996	if (error)
2997		return (error);
2998
2999	/*
3000	 * To perform a relabel of the vnode (Biba label or not), Biba must
3001	 * authorize the relabel.
3002	 */
3003	if (!biba_effective_in_range(old, subj))
3004		return (EPERM);
3005
3006	/*
3007	 * If the Biba label is to be changed, authorize as appropriate.
3008	 */
3009	if (new->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) {
3010		/*
3011		 * To change the Biba label on a vnode, the new vnode label
3012		 * must be in the subject range.
3013		 */
3014		if (!biba_effective_in_range(new, subj))
3015			return (EPERM);
3016
3017		/*
3018		 * To change the Biba label on the vnode to be EQUAL, the
3019		 * subject must have appropriate privilege.
3020		 */
3021		if (biba_contains_equal(new)) {
3022			error = biba_subject_privileged(subj);
3023			if (error)
3024				return (error);
3025		}
3026	}
3027
3028	return (0);
3029}
3030
3031static int
3032biba_vnode_check_rename_from(struct ucred *cred, struct vnode *dvp,
3033    struct label *dvplabel, struct vnode *vp, struct label *vplabel,
3034    struct componentname *cnp)
3035{
3036	struct mac_biba *subj, *obj;
3037
3038	if (!biba_enabled)
3039		return (0);
3040
3041	subj = SLOT(cred->cr_label);
3042	obj = SLOT(dvplabel);
3043
3044	if (!biba_dominate_effective(subj, obj))
3045		return (EACCES);
3046
3047	obj = SLOT(vplabel);
3048
3049	if (!biba_dominate_effective(subj, obj))
3050		return (EACCES);
3051
3052	return (0);
3053}
3054
3055static int
3056biba_vnode_check_rename_to(struct ucred *cred, struct vnode *dvp,
3057    struct label *dvplabel, struct vnode *vp, struct label *vplabel,
3058    int samedir, struct componentname *cnp)
3059{
3060	struct mac_biba *subj, *obj;
3061
3062	if (!biba_enabled)
3063		return (0);
3064
3065	subj = SLOT(cred->cr_label);
3066	obj = SLOT(dvplabel);
3067
3068	if (!biba_dominate_effective(subj, obj))
3069		return (EACCES);
3070
3071	if (vp != NULL) {
3072		obj = SLOT(vplabel);
3073
3074		if (!biba_dominate_effective(subj, obj))
3075			return (EACCES);
3076	}
3077
3078	return (0);
3079}
3080
3081static int
3082biba_vnode_check_revoke(struct ucred *cred, struct vnode *vp,
3083    struct label *vplabel)
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_setacl(struct ucred *cred, struct vnode *vp,
3101    struct label *vplabel, acl_type_t type, struct acl *acl)
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_setextattr(struct ucred *cred, struct vnode *vp,
3119    struct label *vplabel, int attrnamespace, const char *name,
3120    struct uio *uio)
3121{
3122	struct mac_biba *subj, *obj;
3123
3124	if (!biba_enabled)
3125		return (0);
3126
3127	subj = SLOT(cred->cr_label);
3128	obj = SLOT(vplabel);
3129
3130	if (!biba_dominate_effective(subj, obj))
3131		return (EACCES);
3132
3133	/* XXX: protect the MAC EA in a special way? */
3134
3135	return (0);
3136}
3137
3138static int
3139biba_vnode_check_setflags(struct ucred *cred, struct vnode *vp,
3140    struct label *vplabel, u_long flags)
3141{
3142	struct mac_biba *subj, *obj;
3143
3144	if (!biba_enabled)
3145		return (0);
3146
3147	subj = SLOT(cred->cr_label);
3148	obj = SLOT(vplabel);
3149
3150	if (!biba_dominate_effective(subj, obj))
3151		return (EACCES);
3152
3153	return (0);
3154}
3155
3156static int
3157biba_vnode_check_setmode(struct ucred *cred, struct vnode *vp,
3158    struct label *vplabel, mode_t mode)
3159{
3160	struct mac_biba *subj, *obj;
3161
3162	if (!biba_enabled)
3163		return (0);
3164
3165	subj = SLOT(cred->cr_label);
3166	obj = SLOT(vplabel);
3167
3168	if (!biba_dominate_effective(subj, obj))
3169		return (EACCES);
3170
3171	return (0);
3172}
3173
3174static int
3175biba_vnode_check_setowner(struct ucred *cred, struct vnode *vp,
3176    struct label *vplabel, uid_t uid, gid_t gid)
3177{
3178	struct mac_biba *subj, *obj;
3179
3180	if (!biba_enabled)
3181		return (0);
3182
3183	subj = SLOT(cred->cr_label);
3184	obj = SLOT(vplabel);
3185
3186	if (!biba_dominate_effective(subj, obj))
3187		return (EACCES);
3188
3189	return (0);
3190}
3191
3192static int
3193biba_vnode_check_setutimes(struct ucred *cred, struct vnode *vp,
3194    struct label *vplabel, struct timespec atime, struct timespec mtime)
3195{
3196	struct mac_biba *subj, *obj;
3197
3198	if (!biba_enabled)
3199		return (0);
3200
3201	subj = SLOT(cred->cr_label);
3202	obj = SLOT(vplabel);
3203
3204	if (!biba_dominate_effective(subj, obj))
3205		return (EACCES);
3206
3207	return (0);
3208}
3209
3210static int
3211biba_vnode_check_stat(struct ucred *active_cred, struct ucred *file_cred,
3212    struct vnode *vp, struct label *vplabel)
3213{
3214	struct mac_biba *subj, *obj;
3215
3216	if (!biba_enabled)
3217		return (0);
3218
3219	subj = SLOT(active_cred->cr_label);
3220	obj = SLOT(vplabel);
3221
3222	if (!biba_dominate_effective(obj, subj))
3223		return (EACCES);
3224
3225	return (0);
3226}
3227
3228static int
3229biba_vnode_check_unlink(struct ucred *cred, struct vnode *dvp,
3230    struct label *dvplabel, struct vnode *vp, struct label *vplabel,
3231    struct componentname *cnp)
3232{
3233	struct mac_biba *subj, *obj;
3234
3235	if (!biba_enabled)
3236		return (0);
3237
3238	subj = SLOT(cred->cr_label);
3239	obj = SLOT(dvplabel);
3240
3241	if (!biba_dominate_effective(subj, obj))
3242		return (EACCES);
3243
3244	obj = SLOT(vplabel);
3245
3246	if (!biba_dominate_effective(subj, obj))
3247		return (EACCES);
3248
3249	return (0);
3250}
3251
3252static int
3253biba_vnode_check_write(struct ucred *active_cred,
3254    struct ucred *file_cred, struct vnode *vp, struct label *vplabel)
3255{
3256	struct mac_biba *subj, *obj;
3257
3258	if (!biba_enabled || !revocation_enabled)
3259		return (0);
3260
3261	subj = SLOT(active_cred->cr_label);
3262	obj = SLOT(vplabel);
3263
3264	if (!biba_dominate_effective(subj, obj))
3265		return (EACCES);
3266
3267	return (0);
3268}
3269
3270static int
3271biba_vnode_create_extattr(struct ucred *cred, struct mount *mp,
3272    struct label *mplabel, struct vnode *dvp, struct label *dvplabel,
3273    struct vnode *vp, struct label *vplabel, struct componentname *cnp)
3274{
3275	struct mac_biba *source, *dest, mb_temp;
3276	size_t buflen;
3277	int error;
3278
3279	buflen = sizeof(mb_temp);
3280	bzero(&mb_temp, buflen);
3281
3282	source = SLOT(cred->cr_label);
3283	dest = SLOT(vplabel);
3284	biba_copy_effective(source, &mb_temp);
3285
3286	error = vn_extattr_set(vp, IO_NODELOCKED, MAC_BIBA_EXTATTR_NAMESPACE,
3287	    MAC_BIBA_EXTATTR_NAME, buflen, (char *) &mb_temp, curthread);
3288	if (error == 0)
3289		biba_copy_effective(source, dest);
3290	return (error);
3291}
3292
3293static void
3294biba_vnode_relabel(struct ucred *cred, struct vnode *vp,
3295    struct label *vplabel, struct label *newlabel)
3296{
3297	struct mac_biba *source, *dest;
3298
3299	source = SLOT(newlabel);
3300	dest = SLOT(vplabel);
3301
3302	biba_copy(source, dest);
3303}
3304
3305static int
3306biba_vnode_setlabel_extattr(struct ucred *cred, struct vnode *vp,
3307    struct label *vplabel, struct label *intlabel)
3308{
3309	struct mac_biba *source, mb_temp;
3310	size_t buflen;
3311	int error;
3312
3313	buflen = sizeof(mb_temp);
3314	bzero(&mb_temp, buflen);
3315
3316	source = SLOT(intlabel);
3317	if ((source->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) == 0)
3318		return (0);
3319
3320	biba_copy_effective(source, &mb_temp);
3321
3322	error = vn_extattr_set(vp, IO_NODELOCKED, MAC_BIBA_EXTATTR_NAMESPACE,
3323	    MAC_BIBA_EXTATTR_NAME, buflen, (char *) &mb_temp, curthread);
3324	return (error);
3325}
3326
3327static struct mac_policy_ops mac_biba_ops =
3328{
3329	.mpo_init = biba_init,
3330
3331	.mpo_bpfdesc_check_receive = biba_bpfdesc_check_receive,
3332	.mpo_bpfdesc_create = biba_bpfdesc_create,
3333	.mpo_bpfdesc_create_mbuf = biba_bpfdesc_create_mbuf,
3334	.mpo_bpfdesc_destroy_label = biba_destroy_label,
3335	.mpo_bpfdesc_init_label = biba_init_label,
3336
3337	.mpo_cred_associate_nfsd = biba_cred_associate_nfsd,
3338	.mpo_cred_check_relabel = biba_cred_check_relabel,
3339	.mpo_cred_check_visible = biba_cred_check_visible,
3340	.mpo_cred_copy_label = biba_copy_label,
3341	.mpo_cred_create_init = biba_cred_create_init,
3342	.mpo_cred_create_swapper = biba_cred_create_swapper,
3343	.mpo_cred_destroy_label = biba_destroy_label,
3344	.mpo_cred_externalize_label = biba_externalize_label,
3345	.mpo_cred_init_label = biba_init_label,
3346	.mpo_cred_internalize_label = biba_internalize_label,
3347	.mpo_cred_relabel = biba_cred_relabel,
3348
3349	.mpo_devfs_create_device = biba_devfs_create_device,
3350	.mpo_devfs_create_directory = biba_devfs_create_directory,
3351	.mpo_devfs_create_symlink = biba_devfs_create_symlink,
3352	.mpo_devfs_destroy_label = biba_destroy_label,
3353	.mpo_devfs_init_label = biba_init_label,
3354	.mpo_devfs_update = biba_devfs_update,
3355	.mpo_devfs_vnode_associate = biba_devfs_vnode_associate,
3356
3357	.mpo_ifnet_check_relabel = biba_ifnet_check_relabel,
3358	.mpo_ifnet_check_transmit = biba_ifnet_check_transmit,
3359	.mpo_ifnet_copy_label = biba_copy_label,
3360	.mpo_ifnet_create = biba_ifnet_create,
3361	.mpo_ifnet_create_mbuf = biba_ifnet_create_mbuf,
3362	.mpo_ifnet_destroy_label = biba_destroy_label,
3363	.mpo_ifnet_externalize_label = biba_externalize_label,
3364	.mpo_ifnet_init_label = biba_init_label,
3365	.mpo_ifnet_internalize_label = biba_internalize_label,
3366	.mpo_ifnet_relabel = biba_ifnet_relabel,
3367
3368	.mpo_inpcb_check_deliver = biba_inpcb_check_deliver,
3369	.mpo_inpcb_check_visible = biba_inpcb_check_visible,
3370	.mpo_inpcb_create = biba_inpcb_create,
3371	.mpo_inpcb_create_mbuf = biba_inpcb_create_mbuf,
3372	.mpo_inpcb_destroy_label = biba_destroy_label,
3373	.mpo_inpcb_init_label = biba_init_label_waitcheck,
3374	.mpo_inpcb_sosetlabel = biba_inpcb_sosetlabel,
3375
3376	.mpo_ip6q_create = biba_ip6q_create,
3377	.mpo_ip6q_destroy_label = biba_destroy_label,
3378	.mpo_ip6q_init_label = biba_init_label_waitcheck,
3379	.mpo_ip6q_match = biba_ip6q_match,
3380	.mpo_ip6q_reassemble = biba_ip6q_reassemble,
3381	.mpo_ip6q_update = biba_ip6q_update,
3382
3383	.mpo_ipq_create = biba_ipq_create,
3384	.mpo_ipq_destroy_label = biba_destroy_label,
3385	.mpo_ipq_init_label = biba_init_label_waitcheck,
3386	.mpo_ipq_match = biba_ipq_match,
3387	.mpo_ipq_reassemble = biba_ipq_reassemble,
3388	.mpo_ipq_update = biba_ipq_update,
3389
3390	.mpo_kld_check_load = biba_kld_check_load,
3391
3392	.mpo_mbuf_copy_label = biba_copy_label,
3393	.mpo_mbuf_destroy_label = biba_destroy_label,
3394	.mpo_mbuf_init_label = biba_init_label_waitcheck,
3395
3396	.mpo_mount_check_stat = biba_mount_check_stat,
3397	.mpo_mount_create = biba_mount_create,
3398	.mpo_mount_destroy_label = biba_destroy_label,
3399	.mpo_mount_init_label = biba_init_label,
3400
3401	.mpo_netatalk_aarp_send = biba_netatalk_aarp_send,
3402
3403	.mpo_netinet_arp_send = biba_netinet_arp_send,
3404	.mpo_netinet_firewall_reply = biba_netinet_firewall_reply,
3405	.mpo_netinet_firewall_send = biba_netinet_firewall_send,
3406	.mpo_netinet_fragment = biba_netinet_fragment,
3407	.mpo_netinet_icmp_reply = biba_netinet_icmp_reply,
3408	.mpo_netinet_igmp_send = biba_netinet_igmp_send,
3409
3410	.mpo_netinet6_nd6_send = biba_netinet6_nd6_send,
3411
3412	.mpo_pipe_check_ioctl = biba_pipe_check_ioctl,
3413	.mpo_pipe_check_poll = biba_pipe_check_poll,
3414	.mpo_pipe_check_read = biba_pipe_check_read,
3415	.mpo_pipe_check_relabel = biba_pipe_check_relabel,
3416	.mpo_pipe_check_stat = biba_pipe_check_stat,
3417	.mpo_pipe_check_write = biba_pipe_check_write,
3418	.mpo_pipe_copy_label = biba_copy_label,
3419	.mpo_pipe_create = biba_pipe_create,
3420	.mpo_pipe_destroy_label = biba_destroy_label,
3421	.mpo_pipe_externalize_label = biba_externalize_label,
3422	.mpo_pipe_init_label = biba_init_label,
3423	.mpo_pipe_internalize_label = biba_internalize_label,
3424	.mpo_pipe_relabel = biba_pipe_relabel,
3425
3426	.mpo_posixsem_check_getvalue = biba_posixsem_check_rdonly,
3427	.mpo_posixsem_check_open = biba_posixsem_check_openunlink,
3428	.mpo_posixsem_check_post = biba_posixsem_check_write,
3429	.mpo_posixsem_check_stat = biba_posixsem_check_rdonly,
3430	.mpo_posixsem_check_unlink = biba_posixsem_check_openunlink,
3431	.mpo_posixsem_check_wait = biba_posixsem_check_write,
3432	.mpo_posixsem_create = biba_posixsem_create,
3433	.mpo_posixsem_destroy_label = biba_destroy_label,
3434	.mpo_posixsem_init_label = biba_init_label,
3435
3436	.mpo_priv_check = biba_priv_check,
3437
3438	.mpo_proc_check_debug = biba_proc_check_debug,
3439	.mpo_proc_check_sched = biba_proc_check_sched,
3440	.mpo_proc_check_signal = biba_proc_check_signal,
3441
3442	.mpo_socket_check_deliver = biba_socket_check_deliver,
3443	.mpo_socket_check_relabel = biba_socket_check_relabel,
3444	.mpo_socket_check_visible = biba_socket_check_visible,
3445	.mpo_socket_copy_label = biba_copy_label,
3446	.mpo_socket_create = biba_socket_create,
3447	.mpo_socket_create_mbuf = biba_socket_create_mbuf,
3448	.mpo_socket_destroy_label = biba_destroy_label,
3449	.mpo_socket_externalize_label = biba_externalize_label,
3450	.mpo_socket_init_label = biba_init_label_waitcheck,
3451	.mpo_socket_internalize_label = biba_internalize_label,
3452	.mpo_socket_newconn = biba_socket_newconn,
3453	.mpo_socket_relabel = biba_socket_relabel,
3454
3455	.mpo_socketpeer_destroy_label = biba_destroy_label,
3456	.mpo_socketpeer_externalize_label = biba_externalize_label,
3457	.mpo_socketpeer_init_label = biba_init_label_waitcheck,
3458	.mpo_socketpeer_set_from_mbuf = biba_socketpeer_set_from_mbuf,
3459	.mpo_socketpeer_set_from_socket = biba_socketpeer_set_from_socket,
3460
3461	.mpo_syncache_create = biba_syncache_create,
3462	.mpo_syncache_create_mbuf = biba_syncache_create_mbuf,
3463	.mpo_syncache_destroy_label = biba_destroy_label,
3464	.mpo_syncache_init_label = biba_init_label_waitcheck,
3465
3466	.mpo_system_check_acct = biba_system_check_acct,
3467	.mpo_system_check_auditctl = biba_system_check_auditctl,
3468	.mpo_system_check_auditon = biba_system_check_auditon,
3469	.mpo_system_check_swapoff = biba_system_check_swapoff,
3470	.mpo_system_check_swapon = biba_system_check_swapon,
3471	.mpo_system_check_sysctl = biba_system_check_sysctl,
3472
3473	.mpo_sysvmsg_cleanup = biba_sysvmsg_cleanup,
3474	.mpo_sysvmsg_create = biba_sysvmsg_create,
3475	.mpo_sysvmsg_destroy_label = biba_destroy_label,
3476	.mpo_sysvmsg_init_label = biba_init_label,
3477
3478	.mpo_sysvmsq_check_msgrcv = biba_sysvmsq_check_msgrcv,
3479	.mpo_sysvmsq_check_msgrmid = biba_sysvmsq_check_msgrmid,
3480	.mpo_sysvmsq_check_msqget = biba_sysvmsq_check_msqget,
3481	.mpo_sysvmsq_check_msqsnd = biba_sysvmsq_check_msqsnd,
3482	.mpo_sysvmsq_check_msqrcv = biba_sysvmsq_check_msqrcv,
3483	.mpo_sysvmsq_check_msqctl = biba_sysvmsq_check_msqctl,
3484	.mpo_sysvmsq_cleanup = biba_sysvmsq_cleanup,
3485	.mpo_sysvmsq_create = biba_sysvmsq_create,
3486	.mpo_sysvmsq_destroy_label = biba_destroy_label,
3487	.mpo_sysvmsq_init_label = biba_init_label,
3488
3489	.mpo_sysvsem_check_semctl = biba_sysvsem_check_semctl,
3490	.mpo_sysvsem_check_semget = biba_sysvsem_check_semget,
3491	.mpo_sysvsem_check_semop = biba_sysvsem_check_semop,
3492	.mpo_sysvsem_cleanup = biba_sysvsem_cleanup,
3493	.mpo_sysvsem_create = biba_sysvsem_create,
3494	.mpo_sysvsem_destroy_label = biba_destroy_label,
3495	.mpo_sysvsem_init_label = biba_init_label,
3496
3497	.mpo_sysvshm_check_shmat = biba_sysvshm_check_shmat,
3498	.mpo_sysvshm_check_shmctl = biba_sysvshm_check_shmctl,
3499	.mpo_sysvshm_check_shmget = biba_sysvshm_check_shmget,
3500	.mpo_sysvshm_cleanup = biba_sysvshm_cleanup,
3501	.mpo_sysvshm_create = biba_sysvshm_create,
3502	.mpo_sysvshm_destroy_label = biba_destroy_label,
3503	.mpo_sysvshm_init_label = biba_init_label,
3504
3505	.mpo_vnode_associate_extattr = biba_vnode_associate_extattr,
3506	.mpo_vnode_associate_singlelabel = biba_vnode_associate_singlelabel,
3507	.mpo_vnode_check_access = biba_vnode_check_open,
3508	.mpo_vnode_check_chdir = biba_vnode_check_chdir,
3509	.mpo_vnode_check_chroot = biba_vnode_check_chroot,
3510	.mpo_vnode_check_create = biba_vnode_check_create,
3511	.mpo_vnode_check_deleteacl = biba_vnode_check_deleteacl,
3512	.mpo_vnode_check_deleteextattr = biba_vnode_check_deleteextattr,
3513	.mpo_vnode_check_exec = biba_vnode_check_exec,
3514	.mpo_vnode_check_getacl = biba_vnode_check_getacl,
3515	.mpo_vnode_check_getextattr = biba_vnode_check_getextattr,
3516	.mpo_vnode_check_link = biba_vnode_check_link,
3517	.mpo_vnode_check_listextattr = biba_vnode_check_listextattr,
3518	.mpo_vnode_check_lookup = biba_vnode_check_lookup,
3519	.mpo_vnode_check_mmap = biba_vnode_check_mmap,
3520	.mpo_vnode_check_open = biba_vnode_check_open,
3521	.mpo_vnode_check_poll = biba_vnode_check_poll,
3522	.mpo_vnode_check_read = biba_vnode_check_read,
3523	.mpo_vnode_check_readdir = biba_vnode_check_readdir,
3524	.mpo_vnode_check_readlink = biba_vnode_check_readlink,
3525	.mpo_vnode_check_relabel = biba_vnode_check_relabel,
3526	.mpo_vnode_check_rename_from = biba_vnode_check_rename_from,
3527	.mpo_vnode_check_rename_to = biba_vnode_check_rename_to,
3528	.mpo_vnode_check_revoke = biba_vnode_check_revoke,
3529	.mpo_vnode_check_setacl = biba_vnode_check_setacl,
3530	.mpo_vnode_check_setextattr = biba_vnode_check_setextattr,
3531	.mpo_vnode_check_setflags = biba_vnode_check_setflags,
3532	.mpo_vnode_check_setmode = biba_vnode_check_setmode,
3533	.mpo_vnode_check_setowner = biba_vnode_check_setowner,
3534	.mpo_vnode_check_setutimes = biba_vnode_check_setutimes,
3535	.mpo_vnode_check_stat = biba_vnode_check_stat,
3536	.mpo_vnode_check_unlink = biba_vnode_check_unlink,
3537	.mpo_vnode_check_write = biba_vnode_check_write,
3538	.mpo_vnode_create_extattr = biba_vnode_create_extattr,
3539	.mpo_vnode_copy_label = biba_copy_label,
3540	.mpo_vnode_destroy_label = biba_destroy_label,
3541	.mpo_vnode_externalize_label = biba_externalize_label,
3542	.mpo_vnode_init_label = biba_init_label,
3543	.mpo_vnode_internalize_label = biba_internalize_label,
3544	.mpo_vnode_relabel = biba_vnode_relabel,
3545	.mpo_vnode_setlabel_extattr = biba_vnode_setlabel_extattr,
3546};
3547
3548#define	BIBA_OBJECTS	(MPC_OBJECT_CRED |				\
3549			 /* MPC_OBJECT_PROC | */			\
3550			 MPC_OBJECT_VNODE |				\
3551			 MPC_OBJECT_INPCB |				\
3552			 MPC_OBJECT_SOCKET |				\
3553			 MPC_OBJECT_DEVFS |				\
3554			 MPC_OBJECT_MBUF |				\
3555			 MPC_OBJECT_IPQ |				\
3556			 MPC_OBJECT_IP6Q |				\
3557			 MPC_OBJECT_IFNET |				\
3558			 MPC_OBJECT_BPFDESC |				\
3559			 MPC_OBJECT_PIPE |				\
3560			 MPC_OBJECT_MOUNT |				\
3561			 MPC_OBJECT_POSIXSEM |				\
3562			 /* MPC_OBJECT_POSIXSHM | */			\
3563			 MPC_OBJECT_SYSVMSG |				\
3564			 MPC_OBJECT_SYSVMSQ |				\
3565			 MPC_OBJECT_SYSVSEM |				\
3566			 MPC_OBJECT_SYSVSHM |				\
3567			 MPC_OBJECT_SYNCACHE)
3568
3569MAC_POLICY_SET(&mac_biba_ops, mac_biba, "TrustedBSD MAC/Biba",
3570    MPC_LOADTIME_FLAG_NOTLATE, &biba_slot, BIBA_OBJECTS);
3571