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