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