mac_biba.c revision 183980
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 183980 2008-10-17 15:11:12Z bz $
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 int
1119biba_inpcb_check_visible(struct ucred *cred, struct inpcb *inp,
1120    struct label *inplabel)
1121{
1122	struct mac_biba *subj, *obj;
1123
1124	if (!biba_enabled)
1125		return (0);
1126
1127	subj = SLOT(cred->cr_label);
1128	obj = SLOT(inplabel);
1129
1130	if (!biba_dominate_effective(obj, subj))
1131		return (ENOENT);
1132
1133	return (0);
1134}
1135
1136static void
1137biba_inpcb_create(struct socket *so, struct label *solabel,
1138    struct inpcb *inp, struct label *inplabel)
1139{
1140	struct mac_biba *source, *dest;
1141
1142	source = SLOT(solabel);
1143	dest = SLOT(inplabel);
1144
1145	biba_copy_effective(source, dest);
1146}
1147
1148static void
1149biba_inpcb_create_mbuf(struct inpcb *inp, struct label *inplabel,
1150    struct mbuf *m, struct label *mlabel)
1151{
1152	struct mac_biba *source, *dest;
1153
1154	source = SLOT(inplabel);
1155	dest = SLOT(mlabel);
1156
1157	biba_copy_effective(source, dest);
1158}
1159
1160static void
1161biba_inpcb_sosetlabel(struct socket *so, struct label *solabel,
1162    struct inpcb *inp, struct label *inplabel)
1163{
1164	struct mac_biba *source, *dest;
1165
1166	source = SLOT(solabel);
1167	dest = SLOT(inplabel);
1168
1169	biba_copy(source, dest);
1170}
1171
1172static void
1173biba_ipq_create(struct mbuf *m, struct label *mlabel, struct ipq *q,
1174    struct label *qlabel)
1175{
1176	struct mac_biba *source, *dest;
1177
1178	source = SLOT(mlabel);
1179	dest = SLOT(qlabel);
1180
1181	biba_copy_effective(source, dest);
1182}
1183
1184static int
1185biba_ipq_match(struct mbuf *m, struct label *mlabel, struct ipq *q,
1186    struct label *qlabel)
1187{
1188	struct mac_biba *a, *b;
1189
1190	a = SLOT(qlabel);
1191	b = SLOT(mlabel);
1192
1193	return (biba_equal_effective(a, b));
1194}
1195
1196static void
1197biba_ipq_reassemble(struct ipq *q, struct label *qlabel, struct mbuf *m,
1198    struct label *mlabel)
1199{
1200	struct mac_biba *source, *dest;
1201
1202	source = SLOT(qlabel);
1203	dest = SLOT(mlabel);
1204
1205	/* Just use the head, since we require them all to match. */
1206	biba_copy_effective(source, dest);
1207}
1208
1209static void
1210biba_ipq_update(struct mbuf *m, struct label *mlabel, struct ipq *q,
1211    struct label *qlabel)
1212{
1213
1214	/* NOOP: we only accept matching labels, so no need to update */
1215}
1216
1217static int
1218biba_kld_check_load(struct ucred *cred, struct vnode *vp,
1219    struct label *vplabel)
1220{
1221	struct mac_biba *subj, *obj;
1222	int error;
1223
1224	if (!biba_enabled)
1225		return (0);
1226
1227	subj = SLOT(cred->cr_label);
1228
1229	error = biba_subject_privileged(subj);
1230	if (error)
1231		return (error);
1232
1233	obj = SLOT(vplabel);
1234	if (!biba_high_effective(obj))
1235		return (EACCES);
1236
1237	return (0);
1238}
1239
1240static int
1241biba_mount_check_stat(struct ucred *cred, struct mount *mp,
1242    struct label *mplabel)
1243{
1244	struct mac_biba *subj, *obj;
1245
1246	if (!biba_enabled)
1247		return (0);
1248
1249	subj = SLOT(cred->cr_label);
1250	obj = SLOT(mplabel);
1251
1252	if (!biba_dominate_effective(obj, subj))
1253		return (EACCES);
1254
1255	return (0);
1256}
1257
1258static void
1259biba_mount_create(struct ucred *cred, struct mount *mp,
1260    struct label *mplabel)
1261{
1262	struct mac_biba *source, *dest;
1263
1264	source = SLOT(cred->cr_label);
1265	dest = SLOT(mplabel);
1266
1267	biba_copy_effective(source, dest);
1268}
1269
1270static void
1271biba_netatalk_aarp_send(struct ifnet *ifp, struct label *ifplabel,
1272    struct mbuf *m, struct label *mlabel)
1273{
1274	struct mac_biba *dest;
1275
1276	dest = SLOT(mlabel);
1277
1278	biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
1279}
1280
1281static void
1282biba_netinet_arp_send(struct ifnet *ifp, struct label *ifplabel,
1283    struct mbuf *m, struct label *mlabel)
1284{
1285	struct mac_biba *dest;
1286
1287	dest = SLOT(mlabel);
1288
1289	biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
1290}
1291
1292static void
1293biba_netinet_firewall_reply(struct mbuf *mrecv, struct label *mrecvlabel,
1294    struct mbuf *msend, struct label *msendlabel)
1295{
1296	struct mac_biba *source, *dest;
1297
1298	source = SLOT(mrecvlabel);
1299	dest = SLOT(msendlabel);
1300
1301	biba_copy_effective(source, dest);
1302}
1303
1304static void
1305biba_netinet_firewall_send(struct mbuf *m, struct label *mlabel)
1306{
1307	struct mac_biba *dest;
1308
1309	dest = SLOT(mlabel);
1310
1311	/* XXX: where is the label for the firewall really coming from? */
1312	biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
1313}
1314
1315static void
1316biba_netinet_fragment(struct mbuf *m, struct label *mlabel,
1317    struct mbuf *frag, struct label *fraglabel)
1318{
1319	struct mac_biba *source, *dest;
1320
1321	source = SLOT(mlabel);
1322	dest = SLOT(fraglabel);
1323
1324	biba_copy_effective(source, dest);
1325}
1326
1327static void
1328biba_netinet_icmp_reply(struct mbuf *mrecv, struct label *mrecvlabel,
1329    struct mbuf *msend, struct label *msendlabel)
1330{
1331	struct mac_biba *source, *dest;
1332
1333	source = SLOT(mrecvlabel);
1334	dest = SLOT(msendlabel);
1335
1336	biba_copy_effective(source, dest);
1337}
1338
1339static void
1340biba_netinet_igmp_send(struct ifnet *ifp, struct label *ifplabel,
1341    struct mbuf *m, struct label *mlabel)
1342{
1343	struct mac_biba *dest;
1344
1345	dest = SLOT(mlabel);
1346
1347	biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
1348}
1349
1350static void
1351biba_netinet6_nd6_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 int
1362biba_pipe_check_ioctl(struct ucred *cred, struct pipepair *pp,
1363    struct label *pplabel, unsigned long cmd, void /* caddr_t */ *data)
1364{
1365
1366	if(!biba_enabled)
1367		return (0);
1368
1369	/* XXX: This will be implemented soon... */
1370
1371	return (0);
1372}
1373
1374static int
1375biba_pipe_check_poll(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_read(struct ucred *cred, struct pipepair *pp,
1394    struct label *pplabel)
1395{
1396	struct mac_biba *subj, *obj;
1397
1398	if (!biba_enabled)
1399		return (0);
1400
1401	subj = SLOT(cred->cr_label);
1402	obj = SLOT(pplabel);
1403
1404	if (!biba_dominate_effective(obj, subj))
1405		return (EACCES);
1406
1407	return (0);
1408}
1409
1410static int
1411biba_pipe_check_relabel(struct ucred *cred, struct pipepair *pp,
1412    struct label *pplabel, struct label *newlabel)
1413{
1414	struct mac_biba *subj, *obj, *new;
1415	int error;
1416
1417	new = SLOT(newlabel);
1418	subj = SLOT(cred->cr_label);
1419	obj = SLOT(pplabel);
1420
1421	/*
1422	 * If there is a Biba label update for a pipe, it must be a effective
1423	 * update.
1424	 */
1425	error = biba_atmostflags(new, MAC_BIBA_FLAG_EFFECTIVE);
1426	if (error)
1427		return (error);
1428
1429	/*
1430	 * To perform a relabel of a pipe (Biba label or not), Biba must
1431	 * authorize the relabel.
1432	 */
1433	if (!biba_effective_in_range(obj, subj))
1434		return (EPERM);
1435
1436	/*
1437	 * If the Biba label is to be changed, authorize as appropriate.
1438	 */
1439	if (new->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) {
1440		/*
1441		 * To change the Biba label on a pipe, the new pipe label
1442		 * must be in the subject range.
1443		 */
1444		if (!biba_effective_in_range(new, subj))
1445			return (EPERM);
1446
1447		/*
1448		 * To change the Biba label on a pipe to be EQUAL, the
1449		 * subject must have appropriate privilege.
1450		 */
1451		if (biba_contains_equal(new)) {
1452			error = biba_subject_privileged(subj);
1453			if (error)
1454				return (error);
1455		}
1456	}
1457
1458	return (0);
1459}
1460
1461static int
1462biba_pipe_check_stat(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(obj, subj))
1474		return (EACCES);
1475
1476	return (0);
1477}
1478
1479static int
1480biba_pipe_check_write(struct ucred *cred, struct pipepair *pp,
1481    struct label *pplabel)
1482{
1483	struct mac_biba *subj, *obj;
1484
1485	if (!biba_enabled)
1486		return (0);
1487
1488	subj = SLOT(cred->cr_label);
1489	obj = SLOT(pplabel);
1490
1491	if (!biba_dominate_effective(subj, obj))
1492		return (EACCES);
1493
1494	return (0);
1495}
1496
1497static void
1498biba_pipe_create(struct ucred *cred, struct pipepair *pp,
1499    struct label *pplabel)
1500{
1501	struct mac_biba *source, *dest;
1502
1503	source = SLOT(cred->cr_label);
1504	dest = SLOT(pplabel);
1505
1506	biba_copy_effective(source, dest);
1507}
1508
1509static void
1510biba_pipe_relabel(struct ucred *cred, struct pipepair *pp,
1511    struct label *pplabel, struct label *newlabel)
1512{
1513	struct mac_biba *source, *dest;
1514
1515	source = SLOT(newlabel);
1516	dest = SLOT(pplabel);
1517
1518	biba_copy(source, dest);
1519}
1520
1521static int
1522biba_posixsem_check_openunlink(struct ucred *cred, struct ksem *ks,
1523    struct label *kslabel)
1524{
1525	struct mac_biba *subj, *obj;
1526
1527	if (!biba_enabled)
1528		return (0);
1529
1530	subj = SLOT(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_write(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(subj, obj))
1552		return (EACCES);
1553
1554	return (0);
1555}
1556
1557static int
1558biba_posixsem_check_rdonly(struct ucred *active_cred, struct ucred *file_cred,
1559    struct ksem *ks, struct label *kslabel)
1560{
1561	struct mac_biba *subj, *obj;
1562
1563	if (!biba_enabled)
1564		return (0);
1565
1566	subj = SLOT(active_cred->cr_label);
1567	obj = SLOT(kslabel);
1568
1569	if (!biba_dominate_effective(obj, subj))
1570		return (EACCES);
1571
1572	return (0);
1573}
1574
1575static void
1576biba_posixsem_create(struct ucred *cred, struct ksem *ks,
1577    struct label *kslabel)
1578{
1579	struct mac_biba *source, *dest;
1580
1581	source = SLOT(cred->cr_label);
1582	dest = SLOT(kslabel);
1583
1584	biba_copy_effective(source, dest);
1585}
1586
1587/*
1588 * Some system privileges are allowed regardless of integrity grade; others
1589 * are allowed only when running with privilege with respect to the Biba
1590 * policy as they might otherwise allow bypassing of the integrity policy.
1591 */
1592static int
1593biba_priv_check(struct ucred *cred, int priv)
1594{
1595	struct mac_biba *subj;
1596	int error;
1597
1598	if (!biba_enabled)
1599		return (0);
1600
1601	/*
1602	 * Exempt only specific privileges from the Biba integrity policy.
1603	 */
1604	switch (priv) {
1605	case PRIV_KTRACE:
1606	case PRIV_MSGBUF:
1607
1608	/*
1609	 * Allow processes to manipulate basic process audit properties, and
1610	 * to submit audit records.
1611	 */
1612	case PRIV_AUDIT_GETAUDIT:
1613	case PRIV_AUDIT_SETAUDIT:
1614	case PRIV_AUDIT_SUBMIT:
1615
1616	/*
1617	 * Allow processes to manipulate their regular UNIX credentials.
1618	 */
1619	case PRIV_CRED_SETUID:
1620	case PRIV_CRED_SETEUID:
1621	case PRIV_CRED_SETGID:
1622	case PRIV_CRED_SETEGID:
1623	case PRIV_CRED_SETGROUPS:
1624	case PRIV_CRED_SETREUID:
1625	case PRIV_CRED_SETREGID:
1626	case PRIV_CRED_SETRESUID:
1627	case PRIV_CRED_SETRESGID:
1628
1629	/*
1630	 * Allow processes to perform system monitoring.
1631	 */
1632	case PRIV_SEEOTHERGIDS:
1633	case PRIV_SEEOTHERUIDS:
1634		break;
1635
1636	/*
1637	 * Allow access to general process debugging facilities.  We
1638	 * separately control debugging based on MAC label.
1639	 */
1640	case PRIV_DEBUG_DIFFCRED:
1641	case PRIV_DEBUG_SUGID:
1642	case PRIV_DEBUG_UNPRIV:
1643
1644	/*
1645	 * Allow manipulating jails.
1646	 */
1647	case PRIV_JAIL_ATTACH:
1648
1649	/*
1650	 * Allow privilege with respect to the Partition policy, but not the
1651	 * Privs policy.
1652	 */
1653	case PRIV_MAC_PARTITION:
1654
1655	/*
1656	 * Allow privilege with respect to process resource limits and login
1657	 * context.
1658	 */
1659	case PRIV_PROC_LIMIT:
1660	case PRIV_PROC_SETLOGIN:
1661	case PRIV_PROC_SETRLIMIT:
1662
1663	/*
1664	 * Allow System V and POSIX IPC privileges.
1665	 */
1666	case PRIV_IPC_READ:
1667	case PRIV_IPC_WRITE:
1668	case PRIV_IPC_ADMIN:
1669	case PRIV_IPC_MSGSIZE:
1670	case PRIV_MQ_ADMIN:
1671
1672	/*
1673	 * Allow certain scheduler manipulations -- possibly this should be
1674	 * controlled by more fine-grained policy, as potentially low
1675	 * integrity processes can deny CPU to higher integrity ones.
1676	 */
1677	case PRIV_SCHED_DIFFCRED:
1678	case PRIV_SCHED_SETPRIORITY:
1679	case PRIV_SCHED_RTPRIO:
1680	case PRIV_SCHED_SETPOLICY:
1681	case PRIV_SCHED_SET:
1682	case PRIV_SCHED_SETPARAM:
1683
1684	/*
1685	 * More IPC privileges.
1686	 */
1687	case PRIV_SEM_WRITE:
1688
1689	/*
1690	 * Allow signaling privileges subject to integrity policy.
1691	 */
1692	case PRIV_SIGNAL_DIFFCRED:
1693	case PRIV_SIGNAL_SUGID:
1694
1695	/*
1696	 * Allow access to only limited sysctls from lower integrity levels;
1697	 * piggy-back on the Jail definition.
1698	 */
1699	case PRIV_SYSCTL_WRITEJAIL:
1700
1701	/*
1702	 * Allow TTY-based privileges, subject to general device access using
1703	 * labels on TTY device nodes, but not console privilege.
1704	 */
1705	case PRIV_TTY_DRAINWAIT:
1706	case PRIV_TTY_DTRWAIT:
1707	case PRIV_TTY_EXCLUSIVE:
1708	case PRIV_TTY_PRISON:
1709	case PRIV_TTY_STI:
1710	case PRIV_TTY_SETA:
1711
1712	/*
1713	 * Grant most VFS privileges, as almost all are in practice bounded
1714	 * by more specific checks using labels.
1715	 */
1716	case PRIV_VFS_READ:
1717	case PRIV_VFS_WRITE:
1718	case PRIV_VFS_ADMIN:
1719	case PRIV_VFS_EXEC:
1720	case PRIV_VFS_LOOKUP:
1721	case PRIV_VFS_CHFLAGS_DEV:
1722	case PRIV_VFS_CHOWN:
1723	case PRIV_VFS_CHROOT:
1724	case PRIV_VFS_RETAINSUGID:
1725	case PRIV_VFS_EXCEEDQUOTA:
1726	case PRIV_VFS_FCHROOT:
1727	case PRIV_VFS_FHOPEN:
1728	case PRIV_VFS_FHSTATFS:
1729	case PRIV_VFS_GENERATION:
1730	case PRIV_VFS_GETFH:
1731	case PRIV_VFS_GETQUOTA:
1732	case PRIV_VFS_LINK:
1733	case PRIV_VFS_MOUNT:
1734	case PRIV_VFS_MOUNT_OWNER:
1735	case PRIV_VFS_MOUNT_PERM:
1736	case PRIV_VFS_MOUNT_SUIDDIR:
1737	case PRIV_VFS_MOUNT_NONUSER:
1738	case PRIV_VFS_SETGID:
1739	case PRIV_VFS_STICKYFILE:
1740	case PRIV_VFS_SYSFLAGS:
1741	case PRIV_VFS_UNMOUNT:
1742
1743	/*
1744	 * Allow VM privileges; it would be nice if these were subject to
1745	 * resource limits.
1746	 */
1747	case PRIV_VM_MADV_PROTECT:
1748	case PRIV_VM_MLOCK:
1749	case PRIV_VM_MUNLOCK:
1750
1751	/*
1752	 * Allow some but not all network privileges.  In general, dont allow
1753	 * reconfiguring the network stack, just normal use.
1754	 */
1755	case PRIV_NETATALK_RESERVEDPORT:
1756	case PRIV_NETINET_RESERVEDPORT:
1757	case PRIV_NETINET_RAW:
1758	case PRIV_NETINET_REUSEPORT:
1759	case PRIV_NETIPX_RESERVEDPORT:
1760	case PRIV_NETIPX_RAW:
1761		break;
1762
1763	/*
1764	 * All remaining system privileges are allow only if the process
1765	 * holds privilege with respect to the Biba policy.
1766	 */
1767	default:
1768		subj = SLOT(cred->cr_label);
1769		error = biba_subject_privileged(subj);
1770		if (error)
1771			return (error);
1772	}
1773	return (0);
1774}
1775
1776static void
1777biba_proc_associate_nfsd(struct ucred *cred)
1778{
1779	struct mac_biba *label;
1780
1781	label = SLOT(cred->cr_label);
1782	biba_set_effective(label, MAC_BIBA_TYPE_LOW, 0, NULL);
1783	biba_set_range(label, MAC_BIBA_TYPE_LOW, 0, NULL, MAC_BIBA_TYPE_HIGH,
1784	    0, NULL);
1785}
1786
1787static int
1788biba_proc_check_debug(struct ucred *cred, struct proc *p)
1789{
1790	struct mac_biba *subj, *obj;
1791
1792	if (!biba_enabled)
1793		return (0);
1794
1795	subj = SLOT(cred->cr_label);
1796	obj = SLOT(p->p_ucred->cr_label);
1797
1798	/* XXX: range checks */
1799	if (!biba_dominate_effective(obj, subj))
1800		return (ESRCH);
1801	if (!biba_dominate_effective(subj, obj))
1802		return (EACCES);
1803
1804	return (0);
1805}
1806
1807static int
1808biba_proc_check_sched(struct ucred *cred, struct proc *p)
1809{
1810	struct mac_biba *subj, *obj;
1811
1812	if (!biba_enabled)
1813		return (0);
1814
1815	subj = SLOT(cred->cr_label);
1816	obj = SLOT(p->p_ucred->cr_label);
1817
1818	/* XXX: range checks */
1819	if (!biba_dominate_effective(obj, subj))
1820		return (ESRCH);
1821	if (!biba_dominate_effective(subj, obj))
1822		return (EACCES);
1823
1824	return (0);
1825}
1826
1827static int
1828biba_proc_check_signal(struct ucred *cred, struct proc *p, int signum)
1829{
1830	struct mac_biba *subj, *obj;
1831
1832	if (!biba_enabled)
1833		return (0);
1834
1835	subj = SLOT(cred->cr_label);
1836	obj = SLOT(p->p_ucred->cr_label);
1837
1838	/* XXX: range checks */
1839	if (!biba_dominate_effective(obj, subj))
1840		return (ESRCH);
1841	if (!biba_dominate_effective(subj, obj))
1842		return (EACCES);
1843
1844	return (0);
1845}
1846
1847static int
1848biba_socket_check_deliver(struct socket *so, struct label *solabel,
1849    struct mbuf *m, struct label *mlabel)
1850{
1851	struct mac_biba *p, *s;
1852
1853	if (!biba_enabled)
1854		return (0);
1855
1856	p = SLOT(mlabel);
1857	s = SLOT(solabel);
1858
1859	return (biba_equal_effective(p, s) ? 0 : EACCES);
1860}
1861
1862static void
1863biba_proc_create_init(struct ucred *cred)
1864{
1865	struct mac_biba *dest;
1866
1867	dest = SLOT(cred->cr_label);
1868
1869	biba_set_effective(dest, MAC_BIBA_TYPE_HIGH, 0, NULL);
1870	biba_set_range(dest, MAC_BIBA_TYPE_LOW, 0, NULL, MAC_BIBA_TYPE_HIGH,
1871	    0, NULL);
1872}
1873
1874static void
1875biba_proc_create_swapper(struct ucred *cred)
1876{
1877	struct mac_biba *dest;
1878
1879	dest = SLOT(cred->cr_label);
1880
1881	biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
1882	biba_set_range(dest, MAC_BIBA_TYPE_LOW, 0, NULL, MAC_BIBA_TYPE_HIGH,
1883	    0, NULL);
1884}
1885
1886static int
1887biba_socket_check_relabel(struct ucred *cred, struct socket *so,
1888    struct label *solabel, struct label *newlabel)
1889{
1890	struct mac_biba *subj, *obj, *new;
1891	int error;
1892
1893	new = SLOT(newlabel);
1894	subj = SLOT(cred->cr_label);
1895	obj = SLOT(solabel);
1896
1897	/*
1898	 * If there is a Biba label update for the socket, it may be an
1899	 * update of effective.
1900	 */
1901	error = biba_atmostflags(new, MAC_BIBA_FLAG_EFFECTIVE);
1902	if (error)
1903		return (error);
1904
1905	/*
1906	 * To relabel a socket, the old socket effective must be in the
1907	 * subject range.
1908	 */
1909	if (!biba_effective_in_range(obj, subj))
1910		return (EPERM);
1911
1912	/*
1913	 * If the Biba label is to be changed, authorize as appropriate.
1914	 */
1915	if (new->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) {
1916		/*
1917		 * To relabel a socket, the new socket effective must be in
1918		 * the subject range.
1919		 */
1920		if (!biba_effective_in_range(new, subj))
1921			return (EPERM);
1922
1923		/*
1924		 * To change the Biba label on the socket to contain EQUAL,
1925		 * the subject must have appropriate privilege.
1926		 */
1927		if (biba_contains_equal(new)) {
1928			error = biba_subject_privileged(subj);
1929			if (error)
1930				return (error);
1931		}
1932	}
1933
1934	return (0);
1935}
1936
1937static int
1938biba_socket_check_visible(struct ucred *cred, struct socket *so,
1939    struct label *solabel)
1940{
1941	struct mac_biba *subj, *obj;
1942
1943	if (!biba_enabled)
1944		return (0);
1945
1946	subj = SLOT(cred->cr_label);
1947	obj = SLOT(solabel);
1948
1949	if (!biba_dominate_effective(obj, subj))
1950		return (ENOENT);
1951
1952	return (0);
1953}
1954
1955static void
1956biba_socket_create(struct ucred *cred, struct socket *so,
1957    struct label *solabel)
1958{
1959	struct mac_biba *source, *dest;
1960
1961	source = SLOT(cred->cr_label);
1962	dest = SLOT(solabel);
1963
1964	biba_copy_effective(source, dest);
1965}
1966
1967static void
1968biba_socket_create_mbuf(struct socket *so, struct label *solabel,
1969    struct mbuf *m, struct label *mlabel)
1970{
1971	struct mac_biba *source, *dest;
1972
1973	source = SLOT(solabel);
1974	dest = SLOT(mlabel);
1975
1976	biba_copy_effective(source, dest);
1977}
1978
1979static void
1980biba_socket_newconn(struct socket *oldso, struct label *oldsolabel,
1981    struct socket *newso, struct label *newsolabel)
1982{
1983	struct mac_biba *source, *dest;
1984
1985	source = SLOT(oldsolabel);
1986	dest = SLOT(newsolabel);
1987
1988	biba_copy_effective(source, dest);
1989}
1990
1991static void
1992biba_socket_relabel(struct ucred *cred, struct socket *so,
1993    struct label *solabel, struct label *newlabel)
1994{
1995	struct mac_biba *source, *dest;
1996
1997	source = SLOT(newlabel);
1998	dest = SLOT(solabel);
1999
2000	biba_copy(source, dest);
2001}
2002
2003static void
2004biba_socketpeer_set_from_mbuf(struct mbuf *m, struct label *mlabel,
2005    struct socket *so, struct label *sopeerlabel)
2006{
2007	struct mac_biba *source, *dest;
2008
2009	source = SLOT(mlabel);
2010	dest = SLOT(sopeerlabel);
2011
2012	biba_copy_effective(source, dest);
2013}
2014
2015static void
2016biba_socketpeer_set_from_socket(struct socket *oldso,
2017    struct label *oldsolabel, struct socket *newso,
2018    struct label *newsopeerlabel)
2019{
2020	struct mac_biba *source, *dest;
2021
2022	source = SLOT(oldsolabel);
2023	dest = SLOT(newsopeerlabel);
2024
2025	biba_copy_effective(source, dest);
2026}
2027
2028static void
2029biba_syncache_create(struct label *label, struct inpcb *inp)
2030{
2031	struct mac_biba *source, *dest;
2032
2033	source = SLOT(inp->inp_label);
2034	dest = SLOT(label);
2035	biba_copy_effective(source, dest);
2036}
2037
2038static void
2039biba_syncache_create_mbuf(struct label *sc_label, struct mbuf *m,
2040    struct label *mlabel)
2041{
2042	struct mac_biba *source, *dest;
2043
2044	source = SLOT(sc_label);
2045	dest = SLOT(mlabel);
2046	biba_copy_effective(source, dest);
2047}
2048
2049static int
2050biba_system_check_acct(struct ucred *cred, struct vnode *vp,
2051    struct label *vplabel)
2052{
2053	struct mac_biba *subj, *obj;
2054	int error;
2055
2056	if (!biba_enabled)
2057		return (0);
2058
2059	subj = SLOT(cred->cr_label);
2060
2061	error = biba_subject_privileged(subj);
2062	if (error)
2063		return (error);
2064
2065	if (vplabel == NULL)
2066		return (0);
2067
2068	obj = SLOT(vplabel);
2069	if (!biba_high_effective(obj))
2070		return (EACCES);
2071
2072	return (0);
2073}
2074
2075static int
2076biba_system_check_auditctl(struct ucred *cred, struct vnode *vp,
2077    struct label *vplabel)
2078{
2079	struct mac_biba *subj, *obj;
2080	int error;
2081
2082	if (!biba_enabled)
2083		return (0);
2084
2085	subj = SLOT(cred->cr_label);
2086
2087	error = biba_subject_privileged(subj);
2088	if (error)
2089		return (error);
2090
2091	if (vplabel == NULL)
2092		return (0);
2093
2094	obj = SLOT(vplabel);
2095	if (!biba_high_effective(obj))
2096		return (EACCES);
2097
2098	return (0);
2099}
2100
2101static int
2102biba_system_check_auditon(struct ucred *cred, int cmd)
2103{
2104	struct mac_biba *subj;
2105	int error;
2106
2107	if (!biba_enabled)
2108		return (0);
2109
2110	subj = SLOT(cred->cr_label);
2111
2112	error = biba_subject_privileged(subj);
2113	if (error)
2114		return (error);
2115
2116	return (0);
2117}
2118
2119static int
2120biba_system_check_swapoff(struct ucred *cred, struct vnode *vp,
2121    struct label *label)
2122{
2123	struct mac_biba *subj;
2124	int error;
2125
2126	if (!biba_enabled)
2127		return (0);
2128
2129	subj = SLOT(cred->cr_label);
2130
2131	error = biba_subject_privileged(subj);
2132	if (error)
2133		return (error);
2134
2135	return (0);
2136}
2137
2138static int
2139biba_system_check_swapon(struct ucred *cred, struct vnode *vp,
2140    struct label *vplabel)
2141{
2142	struct mac_biba *subj, *obj;
2143	int error;
2144
2145	if (!biba_enabled)
2146		return (0);
2147
2148	subj = SLOT(cred->cr_label);
2149	obj = SLOT(vplabel);
2150
2151	error = biba_subject_privileged(subj);
2152	if (error)
2153		return (error);
2154
2155	if (!biba_high_effective(obj))
2156		return (EACCES);
2157
2158	return (0);
2159}
2160
2161static int
2162biba_system_check_sysctl(struct ucred *cred, struct sysctl_oid *oidp,
2163    void *arg1, int arg2, struct sysctl_req *req)
2164{
2165	struct mac_biba *subj;
2166	int error;
2167
2168	if (!biba_enabled)
2169		return (0);
2170
2171	subj = SLOT(cred->cr_label);
2172
2173	/*
2174	 * Treat sysctl variables without CTLFLAG_ANYBODY flag as biba/high,
2175	 * but also require privilege to change them.
2176	 */
2177	if (req->newptr != NULL && (oidp->oid_kind & CTLFLAG_ANYBODY) == 0) {
2178		if (!biba_subject_dominate_high(subj))
2179			return (EACCES);
2180
2181		error = biba_subject_privileged(subj);
2182		if (error)
2183			return (error);
2184	}
2185
2186	return (0);
2187}
2188
2189static void
2190biba_sysvmsg_cleanup(struct label *msglabel)
2191{
2192
2193	bzero(SLOT(msglabel), sizeof(struct mac_biba));
2194}
2195
2196static void
2197biba_sysvmsg_create(struct ucred *cred, struct msqid_kernel *msqkptr,
2198    struct label *msqlabel, struct msg *msgptr, struct label *msglabel)
2199{
2200	struct mac_biba *source, *dest;
2201
2202	/* Ignore the msgq label */
2203	source = SLOT(cred->cr_label);
2204	dest = SLOT(msglabel);
2205
2206	biba_copy_effective(source, dest);
2207}
2208
2209static int
2210biba_sysvmsq_check_msgrcv(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(obj, subj))
2222		return (EACCES);
2223
2224	return (0);
2225}
2226
2227static int
2228biba_sysvmsq_check_msgrmid(struct ucred *cred, struct msg *msgptr,
2229    struct label *msglabel)
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(msglabel);
2238
2239	if (!biba_dominate_effective(subj, obj))
2240		return (EACCES);
2241
2242	return (0);
2243}
2244
2245static int
2246biba_sysvmsq_check_msqget(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(obj, subj))
2258		return (EACCES);
2259
2260	return (0);
2261}
2262
2263static int
2264biba_sysvmsq_check_msqsnd(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(subj, obj))
2276		return (EACCES);
2277
2278	return (0);
2279}
2280
2281static int
2282biba_sysvmsq_check_msqrcv(struct ucred *cred, struct msqid_kernel *msqkptr,
2283    struct label *msqklabel)
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	if (!biba_dominate_effective(obj, subj))
2294		return (EACCES);
2295
2296	return (0);
2297}
2298
2299static int
2300biba_sysvmsq_check_msqctl(struct ucred *cred, struct msqid_kernel *msqkptr,
2301    struct label *msqklabel, int cmd)
2302{
2303	struct mac_biba *subj, *obj;
2304
2305	if (!biba_enabled)
2306		return (0);
2307
2308	subj = SLOT(cred->cr_label);
2309	obj = SLOT(msqklabel);
2310
2311	switch(cmd) {
2312	case IPC_RMID:
2313	case IPC_SET:
2314		if (!biba_dominate_effective(subj, obj))
2315			return (EACCES);
2316		break;
2317
2318	case IPC_STAT:
2319		if (!biba_dominate_effective(obj, subj))
2320			return (EACCES);
2321		break;
2322
2323	default:
2324		return (EACCES);
2325	}
2326
2327	return (0);
2328}
2329
2330static void
2331biba_sysvmsq_cleanup(struct label *msqlabel)
2332{
2333
2334	bzero(SLOT(msqlabel), sizeof(struct mac_biba));
2335}
2336
2337static void
2338biba_sysvmsq_create(struct ucred *cred, struct msqid_kernel *msqkptr,
2339    struct label *msqlabel)
2340{
2341	struct mac_biba *source, *dest;
2342
2343	source = SLOT(cred->cr_label);
2344	dest = SLOT(msqlabel);
2345
2346	biba_copy_effective(source, dest);
2347}
2348
2349static int
2350biba_sysvsem_check_semctl(struct ucred *cred, struct semid_kernel *semakptr,
2351    struct label *semaklabel, int cmd)
2352{
2353	struct mac_biba *subj, *obj;
2354
2355	if (!biba_enabled)
2356		return (0);
2357
2358	subj = SLOT(cred->cr_label);
2359	obj = SLOT(semaklabel);
2360
2361	switch(cmd) {
2362	case IPC_RMID:
2363	case IPC_SET:
2364	case SETVAL:
2365	case SETALL:
2366		if (!biba_dominate_effective(subj, obj))
2367			return (EACCES);
2368		break;
2369
2370	case IPC_STAT:
2371	case GETVAL:
2372	case GETPID:
2373	case GETNCNT:
2374	case GETZCNT:
2375	case GETALL:
2376		if (!biba_dominate_effective(obj, subj))
2377			return (EACCES);
2378		break;
2379
2380	default:
2381		return (EACCES);
2382	}
2383
2384	return (0);
2385}
2386
2387static int
2388biba_sysvsem_check_semget(struct ucred *cred, struct semid_kernel *semakptr,
2389    struct label *semaklabel)
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 (!biba_dominate_effective(obj, subj))
2400		return (EACCES);
2401
2402	return (0);
2403}
2404
2405static int
2406biba_sysvsem_check_semop(struct ucred *cred, struct semid_kernel *semakptr,
2407    struct label *semaklabel, size_t accesstype)
2408{
2409	struct mac_biba *subj, *obj;
2410
2411	if (!biba_enabled)
2412		return (0);
2413
2414	subj = SLOT(cred->cr_label);
2415	obj = SLOT(semaklabel);
2416
2417	if (accesstype & SEM_R)
2418		if (!biba_dominate_effective(obj, subj))
2419			return (EACCES);
2420
2421	if (accesstype & SEM_A)
2422		if (!biba_dominate_effective(subj, obj))
2423			return (EACCES);
2424
2425	return (0);
2426}
2427
2428static void
2429biba_sysvsem_cleanup(struct label *semalabel)
2430{
2431
2432	bzero(SLOT(semalabel), sizeof(struct mac_biba));
2433}
2434
2435static void
2436biba_sysvsem_create(struct ucred *cred, struct semid_kernel *semakptr,
2437    struct label *semalabel)
2438{
2439	struct mac_biba *source, *dest;
2440
2441	source = SLOT(cred->cr_label);
2442	dest = SLOT(semalabel);
2443
2444	biba_copy_effective(source, dest);
2445}
2446
2447static int
2448biba_sysvshm_check_shmat(struct ucred *cred, struct shmid_kernel *shmsegptr,
2449    struct label *shmseglabel, int shmflg)
2450{
2451	struct mac_biba *subj, *obj;
2452
2453	if (!biba_enabled)
2454		return (0);
2455
2456	subj = SLOT(cred->cr_label);
2457	obj = SLOT(shmseglabel);
2458
2459	if (!biba_dominate_effective(obj, subj))
2460		return (EACCES);
2461	if ((shmflg & SHM_RDONLY) == 0) {
2462		if (!biba_dominate_effective(subj, obj))
2463			return (EACCES);
2464	}
2465
2466	return (0);
2467}
2468
2469static int
2470biba_sysvshm_check_shmctl(struct ucred *cred, struct shmid_kernel *shmsegptr,
2471    struct label *shmseglabel, int cmd)
2472{
2473	struct mac_biba *subj, *obj;
2474
2475	if (!biba_enabled)
2476		return (0);
2477
2478	subj = SLOT(cred->cr_label);
2479	obj = SLOT(shmseglabel);
2480
2481	switch(cmd) {
2482	case IPC_RMID:
2483	case IPC_SET:
2484		if (!biba_dominate_effective(subj, obj))
2485			return (EACCES);
2486		break;
2487
2488	case IPC_STAT:
2489	case SHM_STAT:
2490		if (!biba_dominate_effective(obj, subj))
2491			return (EACCES);
2492		break;
2493
2494	default:
2495		return (EACCES);
2496	}
2497
2498	return (0);
2499}
2500
2501static int
2502biba_sysvshm_check_shmget(struct ucred *cred, struct shmid_kernel *shmsegptr,
2503    struct label *shmseglabel, int shmflg)
2504{
2505	struct mac_biba *subj, *obj;
2506
2507	if (!biba_enabled)
2508		return (0);
2509
2510	subj = SLOT(cred->cr_label);
2511	obj = SLOT(shmseglabel);
2512
2513	if (!biba_dominate_effective(obj, subj))
2514		return (EACCES);
2515
2516	return (0);
2517}
2518
2519static void
2520biba_sysvshm_cleanup(struct label *shmlabel)
2521{
2522
2523	bzero(SLOT(shmlabel), sizeof(struct mac_biba));
2524}
2525
2526static void
2527biba_sysvshm_create(struct ucred *cred, struct shmid_kernel *shmsegptr,
2528    struct label *shmlabel)
2529{
2530	struct mac_biba *source, *dest;
2531
2532	source = SLOT(cred->cr_label);
2533	dest = SLOT(shmlabel);
2534
2535	biba_copy_effective(source, dest);
2536}
2537
2538static int
2539biba_vnode_associate_extattr(struct mount *mp, struct label *mplabel,
2540    struct vnode *vp, struct label *vplabel)
2541{
2542	struct mac_biba mb_temp, *source, *dest;
2543	int buflen, error;
2544
2545	source = SLOT(mplabel);
2546	dest = SLOT(vplabel);
2547
2548	buflen = sizeof(mb_temp);
2549	bzero(&mb_temp, buflen);
2550
2551	error = vn_extattr_get(vp, IO_NODELOCKED, MAC_BIBA_EXTATTR_NAMESPACE,
2552	    MAC_BIBA_EXTATTR_NAME, &buflen, (char *) &mb_temp, curthread);
2553	if (error == ENOATTR || error == EOPNOTSUPP) {
2554		/* Fall back to the mntlabel. */
2555		biba_copy_effective(source, dest);
2556		return (0);
2557	} else if (error)
2558		return (error);
2559
2560	if (buflen != sizeof(mb_temp)) {
2561		printf("biba_vnode_associate_extattr: bad size %d\n",
2562		    buflen);
2563		return (EPERM);
2564	}
2565	if (biba_valid(&mb_temp) != 0) {
2566		printf("biba_vnode_associate_extattr: invalid\n");
2567		return (EPERM);
2568	}
2569	if ((mb_temp.mb_flags & MAC_BIBA_FLAGS_BOTH) !=
2570	    MAC_BIBA_FLAG_EFFECTIVE) {
2571		printf("biba_vnode_associate_extattr: not effective\n");
2572		return (EPERM);
2573	}
2574
2575	biba_copy_effective(&mb_temp, dest);
2576	return (0);
2577}
2578
2579static void
2580biba_vnode_associate_singlelabel(struct mount *mp, struct label *mplabel,
2581    struct vnode *vp, struct label *vplabel)
2582{
2583	struct mac_biba *source, *dest;
2584
2585	source = SLOT(mplabel);
2586	dest = SLOT(vplabel);
2587
2588	biba_copy_effective(source, dest);
2589}
2590
2591static int
2592biba_vnode_check_chdir(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_chroot(struct ucred *cred, struct vnode *dvp,
2611    struct label *dvplabel)
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(obj, subj))
2622		return (EACCES);
2623
2624	return (0);
2625}
2626
2627static int
2628biba_vnode_check_create(struct ucred *cred, struct vnode *dvp,
2629    struct label *dvplabel, struct componentname *cnp, struct vattr *vap)
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(dvplabel);
2638
2639	if (!biba_dominate_effective(subj, obj))
2640		return (EACCES);
2641
2642	return (0);
2643}
2644
2645static int
2646biba_vnode_check_deleteacl(struct ucred *cred, struct vnode *vp,
2647    struct label *vplabel, acl_type_t type)
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_deleteextattr(struct ucred *cred, struct vnode *vp,
2665    struct label *vplabel, int attrnamespace, const char *name)
2666{
2667	struct mac_biba *subj, *obj;
2668
2669	if (!biba_enabled)
2670		return (0);
2671
2672	subj = SLOT(cred->cr_label);
2673	obj = SLOT(vplabel);
2674
2675	if (!biba_dominate_effective(subj, obj))
2676		return (EACCES);
2677
2678	return (0);
2679}
2680
2681static int
2682biba_vnode_check_exec(struct ucred *cred, struct vnode *vp,
2683    struct label *vplabel, struct image_params *imgp,
2684    struct label *execlabel)
2685{
2686	struct mac_biba *subj, *obj, *exec;
2687	int error;
2688
2689	if (execlabel != NULL) {
2690		/*
2691		 * We currently don't permit labels to be changed at
2692		 * exec-time as part of Biba, so disallow non-NULL Biba label
2693		 * elements in the execlabel.
2694		 */
2695		exec = SLOT(execlabel);
2696		error = biba_atmostflags(exec, 0);
2697		if (error)
2698			return (error);
2699	}
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_getacl(struct ucred *cred, struct vnode *vp,
2715    struct label *vplabel, acl_type_t type)
2716{
2717	struct mac_biba *subj, *obj;
2718
2719	if (!biba_enabled)
2720		return (0);
2721
2722	subj = SLOT(cred->cr_label);
2723	obj = SLOT(vplabel);
2724
2725	if (!biba_dominate_effective(obj, subj))
2726		return (EACCES);
2727
2728	return (0);
2729}
2730
2731static int
2732biba_vnode_check_getextattr(struct ucred *cred, struct vnode *vp,
2733    struct label *vplabel, int attrnamespace, const char *name,
2734    struct uio *uio)
2735{
2736	struct mac_biba *subj, *obj;
2737
2738	if (!biba_enabled)
2739		return (0);
2740
2741	subj = SLOT(cred->cr_label);
2742	obj = SLOT(vplabel);
2743
2744	if (!biba_dominate_effective(obj, subj))
2745		return (EACCES);
2746
2747	return (0);
2748}
2749
2750static int
2751biba_vnode_check_link(struct ucred *cred, struct vnode *dvp,
2752    struct label *dvplabel, struct vnode *vp, struct label *vplabel,
2753    struct componentname *cnp)
2754{
2755	struct mac_biba *subj, *obj;
2756
2757	if (!biba_enabled)
2758		return (0);
2759
2760	subj = SLOT(cred->cr_label);
2761	obj = SLOT(dvplabel);
2762
2763	if (!biba_dominate_effective(subj, obj))
2764		return (EACCES);
2765
2766	obj = SLOT(vplabel);
2767
2768	if (!biba_dominate_effective(subj, obj))
2769		return (EACCES);
2770
2771	return (0);
2772}
2773
2774static int
2775biba_vnode_check_listextattr(struct ucred *cred, struct vnode *vp,
2776    struct label *vplabel, int attrnamespace)
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(vplabel);
2785
2786	if (!biba_dominate_effective(obj, subj))
2787		return (EACCES);
2788
2789	return (0);
2790}
2791
2792static int
2793biba_vnode_check_lookup(struct ucred *cred, struct vnode *dvp,
2794    struct label *dvplabel, struct componentname *cnp)
2795{
2796	struct mac_biba *subj, *obj;
2797
2798	if (!biba_enabled)
2799		return (0);
2800
2801	subj = SLOT(cred->cr_label);
2802	obj = SLOT(dvplabel);
2803
2804	if (!biba_dominate_effective(obj, subj))
2805		return (EACCES);
2806
2807	return (0);
2808}
2809
2810static int
2811biba_vnode_check_mmap(struct ucred *cred, struct vnode *vp,
2812    struct label *vplabel, int prot, int flags)
2813{
2814	struct mac_biba *subj, *obj;
2815
2816	/*
2817	 * Rely on the use of open()-time protections to handle
2818	 * non-revocation cases.
2819	 */
2820	if (!biba_enabled || !revocation_enabled)
2821		return (0);
2822
2823	subj = SLOT(cred->cr_label);
2824	obj = SLOT(vplabel);
2825
2826	if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
2827		if (!biba_dominate_effective(obj, subj))
2828			return (EACCES);
2829	}
2830	if (((prot & VM_PROT_WRITE) != 0) && ((flags & MAP_SHARED) != 0)) {
2831		if (!biba_dominate_effective(subj, obj))
2832			return (EACCES);
2833	}
2834
2835	return (0);
2836}
2837
2838static int
2839biba_vnode_check_open(struct ucred *cred, struct vnode *vp,
2840    struct label *vplabel, int acc_mode)
2841{
2842	struct mac_biba *subj, *obj;
2843
2844	if (!biba_enabled)
2845		return (0);
2846
2847	subj = SLOT(cred->cr_label);
2848	obj = SLOT(vplabel);
2849
2850	/* XXX privilege override for admin? */
2851	if (acc_mode & (VREAD | VEXEC | VSTAT)) {
2852		if (!biba_dominate_effective(obj, subj))
2853			return (EACCES);
2854	}
2855	if (acc_mode & (VWRITE | VAPPEND | VADMIN)) {
2856		if (!biba_dominate_effective(subj, obj))
2857			return (EACCES);
2858	}
2859
2860	return (0);
2861}
2862
2863static int
2864biba_vnode_check_poll(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_read(struct ucred *active_cred, struct ucred *file_cred,
2883    struct vnode *vp, struct label *vplabel)
2884{
2885	struct mac_biba *subj, *obj;
2886
2887	if (!biba_enabled || !revocation_enabled)
2888		return (0);
2889
2890	subj = SLOT(active_cred->cr_label);
2891	obj = SLOT(vplabel);
2892
2893	if (!biba_dominate_effective(obj, subj))
2894		return (EACCES);
2895
2896	return (0);
2897}
2898
2899static int
2900biba_vnode_check_readdir(struct ucred *cred, struct vnode *dvp,
2901    struct label *dvplabel)
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(dvplabel);
2910
2911	if (!biba_dominate_effective(obj, subj))
2912		return (EACCES);
2913
2914	return (0);
2915}
2916
2917static int
2918biba_vnode_check_readlink(struct ucred *cred, struct vnode *vp,
2919    struct label *vplabel)
2920{
2921	struct mac_biba *subj, *obj;
2922
2923	if (!biba_enabled)
2924		return (0);
2925
2926	subj = SLOT(cred->cr_label);
2927	obj = SLOT(vplabel);
2928
2929	if (!biba_dominate_effective(obj, subj))
2930		return (EACCES);
2931
2932	return (0);
2933}
2934
2935static int
2936biba_vnode_check_relabel(struct ucred *cred, struct vnode *vp,
2937    struct label *vplabel, struct label *newlabel)
2938{
2939	struct mac_biba *old, *new, *subj;
2940	int error;
2941
2942	old = SLOT(vplabel);
2943	new = SLOT(newlabel);
2944	subj = SLOT(cred->cr_label);
2945
2946	/*
2947	 * If there is a Biba label update for the vnode, it must be a
2948	 * effective label.
2949	 */
2950	error = biba_atmostflags(new, MAC_BIBA_FLAG_EFFECTIVE);
2951	if (error)
2952		return (error);
2953
2954	/*
2955	 * To perform a relabel of the vnode (Biba label or not), Biba must
2956	 * authorize the relabel.
2957	 */
2958	if (!biba_effective_in_range(old, subj))
2959		return (EPERM);
2960
2961	/*
2962	 * If the Biba label is to be changed, authorize as appropriate.
2963	 */
2964	if (new->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) {
2965		/*
2966		 * To change the Biba label on a vnode, the new vnode label
2967		 * must be in the subject range.
2968		 */
2969		if (!biba_effective_in_range(new, subj))
2970			return (EPERM);
2971
2972		/*
2973		 * To change the Biba label on the vnode to be EQUAL, the
2974		 * subject must have appropriate privilege.
2975		 */
2976		if (biba_contains_equal(new)) {
2977			error = biba_subject_privileged(subj);
2978			if (error)
2979				return (error);
2980		}
2981	}
2982
2983	return (0);
2984}
2985
2986static int
2987biba_vnode_check_rename_from(struct ucred *cred, struct vnode *dvp,
2988    struct label *dvplabel, struct vnode *vp, struct label *vplabel,
2989    struct componentname *cnp)
2990{
2991	struct mac_biba *subj, *obj;
2992
2993	if (!biba_enabled)
2994		return (0);
2995
2996	subj = SLOT(cred->cr_label);
2997	obj = SLOT(dvplabel);
2998
2999	if (!biba_dominate_effective(subj, obj))
3000		return (EACCES);
3001
3002	obj = SLOT(vplabel);
3003
3004	if (!biba_dominate_effective(subj, obj))
3005		return (EACCES);
3006
3007	return (0);
3008}
3009
3010static int
3011biba_vnode_check_rename_to(struct ucred *cred, struct vnode *dvp,
3012    struct label *dvplabel, struct vnode *vp, struct label *vplabel,
3013    int samedir, struct componentname *cnp)
3014{
3015	struct mac_biba *subj, *obj;
3016
3017	if (!biba_enabled)
3018		return (0);
3019
3020	subj = SLOT(cred->cr_label);
3021	obj = SLOT(dvplabel);
3022
3023	if (!biba_dominate_effective(subj, obj))
3024		return (EACCES);
3025
3026	if (vp != NULL) {
3027		obj = SLOT(vplabel);
3028
3029		if (!biba_dominate_effective(subj, obj))
3030			return (EACCES);
3031	}
3032
3033	return (0);
3034}
3035
3036static int
3037biba_vnode_check_revoke(struct ucred *cred, struct vnode *vp,
3038    struct label *vplabel)
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_setacl(struct ucred *cred, struct vnode *vp,
3056    struct label *vplabel, acl_type_t type, struct acl *acl)
3057{
3058	struct mac_biba *subj, *obj;
3059
3060	if (!biba_enabled)
3061		return (0);
3062
3063	subj = SLOT(cred->cr_label);
3064	obj = SLOT(vplabel);
3065
3066	if (!biba_dominate_effective(subj, obj))
3067		return (EACCES);
3068
3069	return (0);
3070}
3071
3072static int
3073biba_vnode_check_setextattr(struct ucred *cred, struct vnode *vp,
3074    struct label *vplabel, int attrnamespace, const char *name,
3075    struct uio *uio)
3076{
3077	struct mac_biba *subj, *obj;
3078
3079	if (!biba_enabled)
3080		return (0);
3081
3082	subj = SLOT(cred->cr_label);
3083	obj = SLOT(vplabel);
3084
3085	if (!biba_dominate_effective(subj, obj))
3086		return (EACCES);
3087
3088	/* XXX: protect the MAC EA in a special way? */
3089
3090	return (0);
3091}
3092
3093static int
3094biba_vnode_check_setflags(struct ucred *cred, struct vnode *vp,
3095    struct label *vplabel, u_long flags)
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_setmode(struct ucred *cred, struct vnode *vp,
3113    struct label *vplabel, mode_t mode)
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_setowner(struct ucred *cred, struct vnode *vp,
3131    struct label *vplabel, uid_t uid, gid_t gid)
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_setutimes(struct ucred *cred, struct vnode *vp,
3149    struct label *vplabel, struct timespec atime, struct timespec mtime)
3150{
3151	struct mac_biba *subj, *obj;
3152
3153	if (!biba_enabled)
3154		return (0);
3155
3156	subj = SLOT(cred->cr_label);
3157	obj = SLOT(vplabel);
3158
3159	if (!biba_dominate_effective(subj, obj))
3160		return (EACCES);
3161
3162	return (0);
3163}
3164
3165static int
3166biba_vnode_check_stat(struct ucred *active_cred, struct ucred *file_cred,
3167    struct vnode *vp, struct label *vplabel)
3168{
3169	struct mac_biba *subj, *obj;
3170
3171	if (!biba_enabled)
3172		return (0);
3173
3174	subj = SLOT(active_cred->cr_label);
3175	obj = SLOT(vplabel);
3176
3177	if (!biba_dominate_effective(obj, subj))
3178		return (EACCES);
3179
3180	return (0);
3181}
3182
3183static int
3184biba_vnode_check_unlink(struct ucred *cred, struct vnode *dvp,
3185    struct label *dvplabel, struct vnode *vp, struct label *vplabel,
3186    struct componentname *cnp)
3187{
3188	struct mac_biba *subj, *obj;
3189
3190	if (!biba_enabled)
3191		return (0);
3192
3193	subj = SLOT(cred->cr_label);
3194	obj = SLOT(dvplabel);
3195
3196	if (!biba_dominate_effective(subj, obj))
3197		return (EACCES);
3198
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_check_write(struct ucred *active_cred,
3209    struct ucred *file_cred, struct vnode *vp, struct label *vplabel)
3210{
3211	struct mac_biba *subj, *obj;
3212
3213	if (!biba_enabled || !revocation_enabled)
3214		return (0);
3215
3216	subj = SLOT(active_cred->cr_label);
3217	obj = SLOT(vplabel);
3218
3219	if (!biba_dominate_effective(subj, obj))
3220		return (EACCES);
3221
3222	return (0);
3223}
3224
3225static int
3226biba_vnode_create_extattr(struct ucred *cred, struct mount *mp,
3227    struct label *mplabel, struct vnode *dvp, struct label *dvplabel,
3228    struct vnode *vp, struct label *vplabel, struct componentname *cnp)
3229{
3230	struct mac_biba *source, *dest, mb_temp;
3231	size_t buflen;
3232	int error;
3233
3234	buflen = sizeof(mb_temp);
3235	bzero(&mb_temp, buflen);
3236
3237	source = SLOT(cred->cr_label);
3238	dest = SLOT(vplabel);
3239	biba_copy_effective(source, &mb_temp);
3240
3241	error = vn_extattr_set(vp, IO_NODELOCKED, MAC_BIBA_EXTATTR_NAMESPACE,
3242	    MAC_BIBA_EXTATTR_NAME, buflen, (char *) &mb_temp, curthread);
3243	if (error == 0)
3244		biba_copy_effective(source, dest);
3245	return (error);
3246}
3247
3248static void
3249biba_vnode_relabel(struct ucred *cred, struct vnode *vp,
3250    struct label *vplabel, struct label *newlabel)
3251{
3252	struct mac_biba *source, *dest;
3253
3254	source = SLOT(newlabel);
3255	dest = SLOT(vplabel);
3256
3257	biba_copy(source, dest);
3258}
3259
3260static int
3261biba_vnode_setlabel_extattr(struct ucred *cred, struct vnode *vp,
3262    struct label *vplabel, struct label *intlabel)
3263{
3264	struct mac_biba *source, mb_temp;
3265	size_t buflen;
3266	int error;
3267
3268	buflen = sizeof(mb_temp);
3269	bzero(&mb_temp, buflen);
3270
3271	source = SLOT(intlabel);
3272	if ((source->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) == 0)
3273		return (0);
3274
3275	biba_copy_effective(source, &mb_temp);
3276
3277	error = vn_extattr_set(vp, IO_NODELOCKED, MAC_BIBA_EXTATTR_NAMESPACE,
3278	    MAC_BIBA_EXTATTR_NAME, buflen, (char *) &mb_temp, curthread);
3279	return (error);
3280}
3281
3282static struct mac_policy_ops mac_biba_ops =
3283{
3284	.mpo_init = biba_init,
3285
3286	.mpo_bpfdesc_check_receive = biba_bpfdesc_check_receive,
3287	.mpo_bpfdesc_create = biba_bpfdesc_create,
3288	.mpo_bpfdesc_create_mbuf = biba_bpfdesc_create_mbuf,
3289	.mpo_bpfdesc_destroy_label = biba_destroy_label,
3290	.mpo_bpfdesc_init_label = biba_init_label,
3291
3292	.mpo_cred_check_relabel = biba_cred_check_relabel,
3293	.mpo_cred_check_visible = biba_cred_check_visible,
3294	.mpo_cred_copy_label = biba_copy_label,
3295	.mpo_cred_destroy_label = biba_destroy_label,
3296	.mpo_cred_externalize_label = biba_externalize_label,
3297	.mpo_cred_init_label = biba_init_label,
3298	.mpo_cred_internalize_label = biba_internalize_label,
3299	.mpo_cred_relabel = biba_cred_relabel,
3300
3301	.mpo_devfs_create_device = biba_devfs_create_device,
3302	.mpo_devfs_create_directory = biba_devfs_create_directory,
3303	.mpo_devfs_create_symlink = biba_devfs_create_symlink,
3304	.mpo_devfs_destroy_label = biba_destroy_label,
3305	.mpo_devfs_init_label = biba_init_label,
3306	.mpo_devfs_update = biba_devfs_update,
3307	.mpo_devfs_vnode_associate = biba_devfs_vnode_associate,
3308
3309	.mpo_ifnet_check_relabel = biba_ifnet_check_relabel,
3310	.mpo_ifnet_check_transmit = biba_ifnet_check_transmit,
3311	.mpo_ifnet_copy_label = biba_copy_label,
3312	.mpo_ifnet_create = biba_ifnet_create,
3313	.mpo_ifnet_create_mbuf = biba_ifnet_create_mbuf,
3314	.mpo_ifnet_destroy_label = biba_destroy_label,
3315	.mpo_ifnet_externalize_label = biba_externalize_label,
3316	.mpo_ifnet_init_label = biba_init_label,
3317	.mpo_ifnet_internalize_label = biba_internalize_label,
3318	.mpo_ifnet_relabel = biba_ifnet_relabel,
3319
3320	.mpo_inpcb_check_deliver = biba_inpcb_check_deliver,
3321	.mpo_inpcb_check_visible = biba_inpcb_check_visible,
3322	.mpo_inpcb_create = biba_inpcb_create,
3323	.mpo_inpcb_create_mbuf = biba_inpcb_create_mbuf,
3324	.mpo_inpcb_destroy_label = biba_destroy_label,
3325	.mpo_inpcb_init_label = biba_init_label_waitcheck,
3326	.mpo_inpcb_sosetlabel = biba_inpcb_sosetlabel,
3327
3328	.mpo_ipq_create = biba_ipq_create,
3329	.mpo_ipq_destroy_label = biba_destroy_label,
3330	.mpo_ipq_init_label = biba_init_label_waitcheck,
3331	.mpo_ipq_match = biba_ipq_match,
3332	.mpo_ipq_reassemble = biba_ipq_reassemble,
3333	.mpo_ipq_update = biba_ipq_update,
3334
3335	.mpo_kld_check_load = biba_kld_check_load,
3336
3337	.mpo_mbuf_copy_label = biba_copy_label,
3338	.mpo_mbuf_destroy_label = biba_destroy_label,
3339	.mpo_mbuf_init_label = biba_init_label_waitcheck,
3340
3341	.mpo_mount_check_stat = biba_mount_check_stat,
3342	.mpo_mount_create = biba_mount_create,
3343	.mpo_mount_destroy_label = biba_destroy_label,
3344	.mpo_mount_init_label = biba_init_label,
3345
3346	.mpo_netatalk_aarp_send = biba_netatalk_aarp_send,
3347
3348	.mpo_netinet_arp_send = biba_netinet_arp_send,
3349	.mpo_netinet_firewall_reply = biba_netinet_firewall_reply,
3350	.mpo_netinet_firewall_send = biba_netinet_firewall_send,
3351	.mpo_netinet_fragment = biba_netinet_fragment,
3352	.mpo_netinet_icmp_reply = biba_netinet_icmp_reply,
3353	.mpo_netinet_igmp_send = biba_netinet_igmp_send,
3354
3355	.mpo_netinet6_nd6_send = biba_netinet6_nd6_send,
3356
3357	.mpo_pipe_check_ioctl = biba_pipe_check_ioctl,
3358	.mpo_pipe_check_poll = biba_pipe_check_poll,
3359	.mpo_pipe_check_read = biba_pipe_check_read,
3360	.mpo_pipe_check_relabel = biba_pipe_check_relabel,
3361	.mpo_pipe_check_stat = biba_pipe_check_stat,
3362	.mpo_pipe_check_write = biba_pipe_check_write,
3363	.mpo_pipe_copy_label = biba_copy_label,
3364	.mpo_pipe_create = biba_pipe_create,
3365	.mpo_pipe_destroy_label = biba_destroy_label,
3366	.mpo_pipe_externalize_label = biba_externalize_label,
3367	.mpo_pipe_init_label = biba_init_label,
3368	.mpo_pipe_internalize_label = biba_internalize_label,
3369	.mpo_pipe_relabel = biba_pipe_relabel,
3370
3371	.mpo_posixsem_check_getvalue = biba_posixsem_check_rdonly,
3372	.mpo_posixsem_check_open = biba_posixsem_check_openunlink,
3373	.mpo_posixsem_check_post = biba_posixsem_check_write,
3374	.mpo_posixsem_check_stat = biba_posixsem_check_rdonly,
3375	.mpo_posixsem_check_unlink = biba_posixsem_check_openunlink,
3376	.mpo_posixsem_check_wait = biba_posixsem_check_write,
3377	.mpo_posixsem_create = biba_posixsem_create,
3378	.mpo_posixsem_destroy_label = biba_destroy_label,
3379	.mpo_posixsem_init_label = biba_init_label,
3380
3381	.mpo_priv_check = biba_priv_check,
3382
3383	.mpo_proc_associate_nfsd = biba_proc_associate_nfsd,
3384	.mpo_proc_check_debug = biba_proc_check_debug,
3385	.mpo_proc_check_sched = biba_proc_check_sched,
3386	.mpo_proc_check_signal = biba_proc_check_signal,
3387	.mpo_proc_create_init = biba_proc_create_init,
3388	.mpo_proc_create_swapper = biba_proc_create_swapper,
3389
3390	.mpo_socket_check_deliver = biba_socket_check_deliver,
3391	.mpo_socket_check_relabel = biba_socket_check_relabel,
3392	.mpo_socket_check_visible = biba_socket_check_visible,
3393	.mpo_socket_copy_label = biba_copy_label,
3394	.mpo_socket_create = biba_socket_create,
3395	.mpo_socket_create_mbuf = biba_socket_create_mbuf,
3396	.mpo_socket_destroy_label = biba_destroy_label,
3397	.mpo_socket_externalize_label = biba_externalize_label,
3398	.mpo_socket_init_label = biba_init_label_waitcheck,
3399	.mpo_socket_internalize_label = biba_internalize_label,
3400	.mpo_socket_newconn = biba_socket_newconn,
3401	.mpo_socket_relabel = biba_socket_relabel,
3402
3403	.mpo_socketpeer_destroy_label = biba_destroy_label,
3404	.mpo_socketpeer_externalize_label = biba_externalize_label,
3405	.mpo_socketpeer_init_label = biba_init_label_waitcheck,
3406	.mpo_socketpeer_set_from_mbuf = biba_socketpeer_set_from_mbuf,
3407	.mpo_socketpeer_set_from_socket = biba_socketpeer_set_from_socket,
3408
3409	.mpo_syncache_create = biba_syncache_create,
3410	.mpo_syncache_create_mbuf = biba_syncache_create_mbuf,
3411	.mpo_syncache_destroy_label = biba_destroy_label,
3412	.mpo_syncache_init_label = biba_init_label_waitcheck,
3413
3414	.mpo_system_check_acct = biba_system_check_acct,
3415	.mpo_system_check_auditctl = biba_system_check_auditctl,
3416	.mpo_system_check_auditon = biba_system_check_auditon,
3417	.mpo_system_check_swapoff = biba_system_check_swapoff,
3418	.mpo_system_check_swapon = biba_system_check_swapon,
3419	.mpo_system_check_sysctl = biba_system_check_sysctl,
3420
3421	.mpo_sysvmsg_cleanup = biba_sysvmsg_cleanup,
3422	.mpo_sysvmsg_create = biba_sysvmsg_create,
3423	.mpo_sysvmsg_destroy_label = biba_destroy_label,
3424	.mpo_sysvmsg_init_label = biba_init_label,
3425
3426	.mpo_sysvmsq_check_msgrcv = biba_sysvmsq_check_msgrcv,
3427	.mpo_sysvmsq_check_msgrmid = biba_sysvmsq_check_msgrmid,
3428	.mpo_sysvmsq_check_msqget = biba_sysvmsq_check_msqget,
3429	.mpo_sysvmsq_check_msqsnd = biba_sysvmsq_check_msqsnd,
3430	.mpo_sysvmsq_check_msqrcv = biba_sysvmsq_check_msqrcv,
3431	.mpo_sysvmsq_check_msqctl = biba_sysvmsq_check_msqctl,
3432	.mpo_sysvmsq_cleanup = biba_sysvmsq_cleanup,
3433	.mpo_sysvmsq_create = biba_sysvmsq_create,
3434	.mpo_sysvmsq_destroy_label = biba_destroy_label,
3435	.mpo_sysvmsq_init_label = biba_init_label,
3436
3437	.mpo_sysvsem_check_semctl = biba_sysvsem_check_semctl,
3438	.mpo_sysvsem_check_semget = biba_sysvsem_check_semget,
3439	.mpo_sysvsem_check_semop = biba_sysvsem_check_semop,
3440	.mpo_sysvsem_cleanup = biba_sysvsem_cleanup,
3441	.mpo_sysvsem_create = biba_sysvsem_create,
3442	.mpo_sysvsem_destroy_label = biba_destroy_label,
3443	.mpo_sysvsem_init_label = biba_init_label,
3444
3445	.mpo_sysvshm_check_shmat = biba_sysvshm_check_shmat,
3446	.mpo_sysvshm_check_shmctl = biba_sysvshm_check_shmctl,
3447	.mpo_sysvshm_check_shmget = biba_sysvshm_check_shmget,
3448	.mpo_sysvshm_cleanup = biba_sysvshm_cleanup,
3449	.mpo_sysvshm_create = biba_sysvshm_create,
3450	.mpo_sysvshm_destroy_label = biba_destroy_label,
3451	.mpo_sysvshm_init_label = biba_init_label,
3452
3453	.mpo_vnode_associate_extattr = biba_vnode_associate_extattr,
3454	.mpo_vnode_associate_singlelabel = biba_vnode_associate_singlelabel,
3455	.mpo_vnode_check_access = biba_vnode_check_open,
3456	.mpo_vnode_check_chdir = biba_vnode_check_chdir,
3457	.mpo_vnode_check_chroot = biba_vnode_check_chroot,
3458	.mpo_vnode_check_create = biba_vnode_check_create,
3459	.mpo_vnode_check_deleteacl = biba_vnode_check_deleteacl,
3460	.mpo_vnode_check_deleteextattr = biba_vnode_check_deleteextattr,
3461	.mpo_vnode_check_exec = biba_vnode_check_exec,
3462	.mpo_vnode_check_getacl = biba_vnode_check_getacl,
3463	.mpo_vnode_check_getextattr = biba_vnode_check_getextattr,
3464	.mpo_vnode_check_link = biba_vnode_check_link,
3465	.mpo_vnode_check_listextattr = biba_vnode_check_listextattr,
3466	.mpo_vnode_check_lookup = biba_vnode_check_lookup,
3467	.mpo_vnode_check_mmap = biba_vnode_check_mmap,
3468	.mpo_vnode_check_open = biba_vnode_check_open,
3469	.mpo_vnode_check_poll = biba_vnode_check_poll,
3470	.mpo_vnode_check_read = biba_vnode_check_read,
3471	.mpo_vnode_check_readdir = biba_vnode_check_readdir,
3472	.mpo_vnode_check_readlink = biba_vnode_check_readlink,
3473	.mpo_vnode_check_relabel = biba_vnode_check_relabel,
3474	.mpo_vnode_check_rename_from = biba_vnode_check_rename_from,
3475	.mpo_vnode_check_rename_to = biba_vnode_check_rename_to,
3476	.mpo_vnode_check_revoke = biba_vnode_check_revoke,
3477	.mpo_vnode_check_setacl = biba_vnode_check_setacl,
3478	.mpo_vnode_check_setextattr = biba_vnode_check_setextattr,
3479	.mpo_vnode_check_setflags = biba_vnode_check_setflags,
3480	.mpo_vnode_check_setmode = biba_vnode_check_setmode,
3481	.mpo_vnode_check_setowner = biba_vnode_check_setowner,
3482	.mpo_vnode_check_setutimes = biba_vnode_check_setutimes,
3483	.mpo_vnode_check_stat = biba_vnode_check_stat,
3484	.mpo_vnode_check_unlink = biba_vnode_check_unlink,
3485	.mpo_vnode_check_write = biba_vnode_check_write,
3486	.mpo_vnode_create_extattr = biba_vnode_create_extattr,
3487	.mpo_vnode_copy_label = biba_copy_label,
3488	.mpo_vnode_destroy_label = biba_destroy_label,
3489	.mpo_vnode_externalize_label = biba_externalize_label,
3490	.mpo_vnode_init_label = biba_init_label,
3491	.mpo_vnode_internalize_label = biba_internalize_label,
3492	.mpo_vnode_relabel = biba_vnode_relabel,
3493	.mpo_vnode_setlabel_extattr = biba_vnode_setlabel_extattr,
3494};
3495
3496#define	BIBA_OBJECTS	(MPC_OBJECT_CRED |				\
3497			 /* MPC_OBJECT_PROC | */			\
3498			 MPC_OBJECT_VNODE |				\
3499			 MPC_OBJECT_INPCB |				\
3500			 MPC_OBJECT_SOCKET |				\
3501			 MPC_OBJECT_DEVFS |				\
3502			 MPC_OBJECT_MBUF |				\
3503			 MPC_OBJECT_IPQ |				\
3504			 MPC_OBJECT_IFNET |				\
3505			 MPC_OBJECT_BPFDESC |				\
3506			 MPC_OBJECT_PIPE |				\
3507			 MPC_OBJECT_MOUNT |				\
3508			 MPC_OBJECT_POSIXSEM |				\
3509			 /* MPC_OBJECT_POSIXSHM | */			\
3510			 MPC_OBJECT_SYSVMSG |				\
3511			 MPC_OBJECT_SYSVMSQ |				\
3512			 MPC_OBJECT_SYSVSEM |				\
3513			 MPC_OBJECT_SYSVSHM |				\
3514			 MPC_OBJECT_SYNCACHE)
3515
3516MAC_POLICY_SET(&mac_biba_ops, mac_biba, "TrustedBSD MAC/Biba",
3517    MPC_LOADTIME_FLAG_NOTLATE, &biba_slot, BIBA_OBJECTS);
3518