mac_biba.c revision 173095
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 173095 2007-10-28 15:55:23Z 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 void
1295biba_mbuf_create_multicast_encap(struct mbuf *m, struct label *mlabel,
1296    struct ifnet *ifp, struct label *ifplabel, struct mbuf *mnew,
1297    struct label *mnewlabel)
1298{
1299	struct mac_biba *source, *dest;
1300
1301	source = SLOT(mlabel);
1302	dest = SLOT(mnewlabel);
1303
1304	biba_copy_effective(source, dest);
1305}
1306
1307static void
1308biba_mbuf_create_netlayer(struct mbuf *m, struct label *mlabel,
1309    struct mbuf *newm, struct label *mnewlabel)
1310{
1311	struct mac_biba *source, *dest;
1312
1313	source = SLOT(mlabel);
1314	dest = SLOT(mnewlabel);
1315
1316	biba_copy_effective(source, dest);
1317}
1318
1319static int
1320biba_ipq_match(struct mbuf *m, struct label *mlabel, struct ipq *ipq,
1321    struct label *ipqlabel)
1322{
1323	struct mac_biba *a, *b;
1324
1325	a = SLOT(ipqlabel);
1326	b = SLOT(mlabel);
1327
1328	return (biba_equal_effective(a, b));
1329}
1330
1331static void
1332biba_ifnet_relabel(struct ucred *cred, struct ifnet *ifp,
1333    struct label *ifplabel, struct label *newlabel)
1334{
1335	struct mac_biba *source, *dest;
1336
1337	source = SLOT(newlabel);
1338	dest = SLOT(ifplabel);
1339
1340	biba_copy(source, dest);
1341}
1342
1343static void
1344biba_ipq_update(struct mbuf *m, struct label *mlabel, struct ipq *ipq,
1345    struct label *ipqlabel)
1346{
1347
1348	/* NOOP: we only accept matching labels, so no need to update */
1349}
1350
1351static void
1352biba_inpcb_sosetlabel(struct socket *so, struct label *solabel,
1353    struct inpcb *inp, struct label *inplabel)
1354{
1355	struct mac_biba *source, *dest;
1356
1357	source = SLOT(solabel);
1358	dest = SLOT(inplabel);
1359
1360	biba_copy(source, dest);
1361}
1362
1363static void
1364biba_netatalk_aarp_send(struct ifnet *ifp, struct label *ifplabel,
1365    struct mbuf *m, struct label *mlabel)
1366{
1367	struct mac_biba *dest;
1368
1369	dest = SLOT(mlabel);
1370
1371	biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
1372}
1373
1374static void
1375biba_netinet_arp_send(struct ifnet *ifp, struct label *ifplabel,
1376    struct mbuf *m, struct label *mlabel)
1377{
1378	struct mac_biba *dest;
1379
1380	dest = SLOT(mlabel);
1381
1382	biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
1383}
1384
1385static void
1386biba_netinet_firewall_send(struct mbuf *m, struct label *mlabel)
1387{
1388	struct mac_biba *dest;
1389
1390	dest = SLOT(mlabel);
1391
1392	/* XXX: where is the label for the firewall really coming from? */
1393	biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
1394}
1395
1396static void
1397biba_netinet_igmp_send(struct ifnet *ifp, struct label *ifplabel,
1398    struct mbuf *m, struct label *mlabel)
1399{
1400	struct mac_biba *dest;
1401
1402	dest = SLOT(mlabel);
1403
1404	biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
1405}
1406
1407static void
1408biba_netinet6_nd6_send(struct ifnet *ifp, struct label *ifplabel,
1409    struct mbuf *m, struct label *mlabel)
1410{
1411	struct mac_biba *dest;
1412
1413	dest = SLOT(mlabel);
1414
1415	biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
1416}
1417
1418/*
1419 * Labeling event operations: processes.
1420 */
1421static void
1422biba_proc_create_swapper(struct ucred *cred)
1423{
1424	struct mac_biba *dest;
1425
1426	dest = SLOT(cred->cr_label);
1427
1428	biba_set_effective(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
1429	biba_set_range(dest, MAC_BIBA_TYPE_LOW, 0, NULL, MAC_BIBA_TYPE_HIGH,
1430	    0, NULL);
1431}
1432
1433static void
1434biba_proc_create_init(struct ucred *cred)
1435{
1436	struct mac_biba *dest;
1437
1438	dest = SLOT(cred->cr_label);
1439
1440	biba_set_effective(dest, MAC_BIBA_TYPE_HIGH, 0, NULL);
1441	biba_set_range(dest, MAC_BIBA_TYPE_LOW, 0, NULL, MAC_BIBA_TYPE_HIGH,
1442	    0, NULL);
1443}
1444
1445static void
1446biba_proc_associate_nfsd(struct ucred *cred)
1447{
1448	struct mac_biba *label;
1449
1450	label = SLOT(cred->cr_label);
1451	biba_set_effective(label, MAC_BIBA_TYPE_LOW, 0, NULL);
1452	biba_set_range(label, MAC_BIBA_TYPE_LOW, 0, NULL, MAC_BIBA_TYPE_HIGH,
1453	    0, NULL);
1454}
1455
1456static void
1457biba_cred_relabel(struct ucred *cred, struct label *newlabel)
1458{
1459	struct mac_biba *source, *dest;
1460
1461	source = SLOT(newlabel);
1462	dest = SLOT(cred->cr_label);
1463
1464	biba_copy(source, dest);
1465}
1466
1467/*
1468 * Label cleanup/flush operations
1469 */
1470static void
1471biba_sysvmsg_cleanup(struct label *msglabel)
1472{
1473
1474	bzero(SLOT(msglabel), sizeof(struct mac_biba));
1475}
1476
1477static void
1478biba_sysvmsq_cleanup(struct label *msqlabel)
1479{
1480
1481	bzero(SLOT(msqlabel), sizeof(struct mac_biba));
1482}
1483
1484static void
1485biba_sysvsem_cleanup(struct label *semalabel)
1486{
1487
1488	bzero(SLOT(semalabel), sizeof(struct mac_biba));
1489}
1490
1491static void
1492biba_sysvshm_cleanup(struct label *shmlabel)
1493{
1494	bzero(SLOT(shmlabel), sizeof(struct mac_biba));
1495}
1496
1497/*
1498 * Access control checks.
1499 */
1500static int
1501biba_bpfdesc_check_receive(struct bpf_d *d, struct label *dlabel,
1502    struct ifnet *ifp, struct label *ifplabel)
1503{
1504	struct mac_biba *a, *b;
1505
1506	if (!biba_enabled)
1507		return (0);
1508
1509	a = SLOT(dlabel);
1510	b = SLOT(ifplabel);
1511
1512	if (biba_equal_effective(a, b))
1513		return (0);
1514	return (EACCES);
1515}
1516
1517static int
1518biba_cred_check_relabel(struct ucred *cred, struct label *newlabel)
1519{
1520	struct mac_biba *subj, *new;
1521	int error;
1522
1523	subj = SLOT(cred->cr_label);
1524	new = SLOT(newlabel);
1525
1526	/*
1527	 * If there is a Biba label update for the credential, it may
1528	 * be an update of the effective, range, or both.
1529	 */
1530	error = biba_atmostflags(new, MAC_BIBA_FLAGS_BOTH);
1531	if (error)
1532		return (error);
1533
1534	/*
1535	 * If the Biba label is to be changed, authorize as appropriate.
1536	 */
1537	if (new->mb_flags & MAC_BIBA_FLAGS_BOTH) {
1538		/*
1539		 * If the change request modifies both the Biba label
1540		 * effective and range, check that the new effective will be
1541		 * in the new range.
1542		 */
1543		if ((new->mb_flags & MAC_BIBA_FLAGS_BOTH) ==
1544		    MAC_BIBA_FLAGS_BOTH &&
1545		    !biba_effective_in_range(new, new))
1546			return (EINVAL);
1547
1548		/*
1549		 * To change the Biba effective label on a credential, the
1550		 * new effective label must be in the current range.
1551		 */
1552		if (new->mb_flags & MAC_BIBA_FLAG_EFFECTIVE &&
1553		    !biba_effective_in_range(new, subj))
1554			return (EPERM);
1555
1556		/*
1557		 * To change the Biba range on a credential, the new range
1558		 * label must be in the current range.
1559		 */
1560		if (new->mb_flags & MAC_BIBA_FLAG_RANGE &&
1561		    !biba_range_in_range(new, subj))
1562			return (EPERM);
1563
1564		/*
1565		 * To have EQUAL in any component of the new credential Biba
1566		 * label, the subject must already have EQUAL in their label.
1567		 */
1568		if (biba_contains_equal(new)) {
1569			error = biba_subject_privileged(subj);
1570			if (error)
1571				return (error);
1572		}
1573	}
1574
1575	return (0);
1576}
1577
1578static int
1579biba_cred_check_visible(struct ucred *u1, struct ucred *u2)
1580{
1581	struct mac_biba *subj, *obj;
1582
1583	if (!biba_enabled)
1584		return (0);
1585
1586	subj = SLOT(u1->cr_label);
1587	obj = SLOT(u2->cr_label);
1588
1589	/* XXX: range */
1590	if (!biba_dominate_effective(obj, subj))
1591		return (ESRCH);
1592
1593	return (0);
1594}
1595
1596static int
1597biba_ifnet_check_relabel(struct ucred *cred, struct ifnet *ifp,
1598    struct label *ifplabel, struct label *newlabel)
1599{
1600	struct mac_biba *subj, *new;
1601	int error;
1602
1603	subj = SLOT(cred->cr_label);
1604	new = SLOT(newlabel);
1605
1606	/*
1607	 * If there is a Biba label update for the interface, it may be an
1608	 * update of the effective, range, or both.
1609	 */
1610	error = biba_atmostflags(new, MAC_BIBA_FLAGS_BOTH);
1611	if (error)
1612		return (error);
1613
1614	/*
1615	 * Relabling network interfaces requires Biba privilege.
1616	 */
1617	error = biba_subject_privileged(subj);
1618	if (error)
1619		return (error);
1620
1621	return (0);
1622}
1623
1624static int
1625biba_ifnet_check_transmit(struct ifnet *ifp, struct label *ifplabel,
1626    struct mbuf *m, struct label *mlabel)
1627{
1628	struct mac_biba *p, *i;
1629
1630	if (!biba_enabled)
1631		return (0);
1632
1633	p = SLOT(mlabel);
1634	i = SLOT(ifplabel);
1635
1636	return (biba_effective_in_range(p, i) ? 0 : EACCES);
1637}
1638
1639static int
1640biba_inpcb_check_deliver(struct inpcb *inp, struct label *inplabel,
1641    struct mbuf *m, struct label *mlabel)
1642{
1643	struct mac_biba *p, *i;
1644
1645	if (!biba_enabled)
1646		return (0);
1647
1648	p = SLOT(mlabel);
1649	i = SLOT(inplabel);
1650
1651	return (biba_equal_effective(p, i) ? 0 : EACCES);
1652}
1653
1654static int
1655biba_sysvmsq_check_msgrcv(struct ucred *cred, struct msg *msgptr,
1656    struct label *msglabel)
1657{
1658	struct mac_biba *subj, *obj;
1659
1660	if (!biba_enabled)
1661		return (0);
1662
1663	subj = SLOT(cred->cr_label);
1664	obj = SLOT(msglabel);
1665
1666	if (!biba_dominate_effective(obj, subj))
1667		return (EACCES);
1668
1669	return (0);
1670}
1671
1672static int
1673biba_sysvmsq_check_msgrmid(struct ucred *cred, struct msg *msgptr,
1674    struct label *msglabel)
1675{
1676	struct mac_biba *subj, *obj;
1677
1678	if (!biba_enabled)
1679		return (0);
1680
1681	subj = SLOT(cred->cr_label);
1682	obj = SLOT(msglabel);
1683
1684	if (!biba_dominate_effective(subj, obj))
1685		return (EACCES);
1686
1687	return (0);
1688}
1689
1690static int
1691biba_sysvmsq_check_msqget(struct ucred *cred, struct msqid_kernel *msqkptr,
1692    struct label *msqklabel)
1693{
1694	struct mac_biba *subj, *obj;
1695
1696	if (!biba_enabled)
1697		return (0);
1698
1699	subj = SLOT(cred->cr_label);
1700	obj = SLOT(msqklabel);
1701
1702	if (!biba_dominate_effective(obj, subj))
1703		return (EACCES);
1704
1705	return (0);
1706}
1707
1708static int
1709biba_sysvmsq_check_msqsnd(struct ucred *cred, struct msqid_kernel *msqkptr,
1710    struct label *msqklabel)
1711{
1712	struct mac_biba *subj, *obj;
1713
1714	if (!biba_enabled)
1715		return (0);
1716
1717	subj = SLOT(cred->cr_label);
1718	obj = SLOT(msqklabel);
1719
1720	if (!biba_dominate_effective(subj, obj))
1721		return (EACCES);
1722
1723	return (0);
1724}
1725
1726static int
1727biba_sysvmsq_check_msqrcv(struct ucred *cred, struct msqid_kernel *msqkptr,
1728    struct label *msqklabel)
1729{
1730	struct mac_biba *subj, *obj;
1731
1732	if (!biba_enabled)
1733		return (0);
1734
1735	subj = SLOT(cred->cr_label);
1736	obj = SLOT(msqklabel);
1737
1738	if (!biba_dominate_effective(obj, subj))
1739		return (EACCES);
1740
1741	return (0);
1742}
1743
1744static int
1745biba_sysvmsq_check_msqctl(struct ucred *cred, struct msqid_kernel *msqkptr,
1746    struct label *msqklabel, int cmd)
1747{
1748	struct mac_biba *subj, *obj;
1749
1750	if (!biba_enabled)
1751		return (0);
1752
1753	subj = SLOT(cred->cr_label);
1754	obj = SLOT(msqklabel);
1755
1756	switch(cmd) {
1757	case IPC_RMID:
1758	case IPC_SET:
1759		if (!biba_dominate_effective(subj, obj))
1760			return (EACCES);
1761		break;
1762
1763	case IPC_STAT:
1764		if (!biba_dominate_effective(obj, subj))
1765			return (EACCES);
1766		break;
1767
1768	default:
1769		return (EACCES);
1770	}
1771
1772	return (0);
1773}
1774
1775static int
1776biba_sysvsem_check_semctl(struct ucred *cred, struct semid_kernel *semakptr,
1777    struct label *semaklabel, int cmd)
1778{
1779	struct mac_biba *subj, *obj;
1780
1781	if (!biba_enabled)
1782		return (0);
1783
1784	subj = SLOT(cred->cr_label);
1785	obj = SLOT(semaklabel);
1786
1787	switch(cmd) {
1788	case IPC_RMID:
1789	case IPC_SET:
1790	case SETVAL:
1791	case SETALL:
1792		if (!biba_dominate_effective(subj, obj))
1793			return (EACCES);
1794		break;
1795
1796	case IPC_STAT:
1797	case GETVAL:
1798	case GETPID:
1799	case GETNCNT:
1800	case GETZCNT:
1801	case GETALL:
1802		if (!biba_dominate_effective(obj, subj))
1803			return (EACCES);
1804		break;
1805
1806	default:
1807		return (EACCES);
1808	}
1809
1810	return (0);
1811}
1812
1813static int
1814biba_sysvsem_check_semget(struct ucred *cred, struct semid_kernel *semakptr,
1815    struct label *semaklabel)
1816{
1817	struct mac_biba *subj, *obj;
1818
1819	if (!biba_enabled)
1820		return (0);
1821
1822	subj = SLOT(cred->cr_label);
1823	obj = SLOT(semaklabel);
1824
1825	if (!biba_dominate_effective(obj, subj))
1826		return (EACCES);
1827
1828	return (0);
1829}
1830
1831static int
1832biba_sysvsem_check_semop(struct ucred *cred, struct semid_kernel *semakptr,
1833    struct label *semaklabel, size_t accesstype)
1834{
1835	struct mac_biba *subj, *obj;
1836
1837	if (!biba_enabled)
1838		return (0);
1839
1840	subj = SLOT(cred->cr_label);
1841	obj = SLOT(semaklabel);
1842
1843	if (accesstype & SEM_R)
1844		if (!biba_dominate_effective(obj, subj))
1845			return (EACCES);
1846
1847	if (accesstype & SEM_A)
1848		if (!biba_dominate_effective(subj, obj))
1849			return (EACCES);
1850
1851	return (0);
1852}
1853
1854static int
1855biba_sysvshm_check_shmat(struct ucred *cred, struct shmid_kernel *shmsegptr,
1856    struct label *shmseglabel, int shmflg)
1857{
1858	struct mac_biba *subj, *obj;
1859
1860	if (!biba_enabled)
1861		return (0);
1862
1863	subj = SLOT(cred->cr_label);
1864	obj = SLOT(shmseglabel);
1865
1866	if (!biba_dominate_effective(obj, subj))
1867		return (EACCES);
1868	if ((shmflg & SHM_RDONLY) == 0) {
1869		if (!biba_dominate_effective(subj, obj))
1870			return (EACCES);
1871	}
1872
1873	return (0);
1874}
1875
1876static int
1877biba_sysvshm_check_shmctl(struct ucred *cred, struct shmid_kernel *shmsegptr,
1878    struct label *shmseglabel, int cmd)
1879{
1880	struct mac_biba *subj, *obj;
1881
1882	if (!biba_enabled)
1883		return (0);
1884
1885	subj = SLOT(cred->cr_label);
1886	obj = SLOT(shmseglabel);
1887
1888	switch(cmd) {
1889	case IPC_RMID:
1890	case IPC_SET:
1891		if (!biba_dominate_effective(subj, obj))
1892			return (EACCES);
1893		break;
1894
1895	case IPC_STAT:
1896	case SHM_STAT:
1897		if (!biba_dominate_effective(obj, subj))
1898			return (EACCES);
1899		break;
1900
1901	default:
1902		return (EACCES);
1903	}
1904
1905	return (0);
1906}
1907
1908static int
1909biba_sysvshm_check_shmget(struct ucred *cred, struct shmid_kernel *shmsegptr,
1910    struct label *shmseglabel, int shmflg)
1911{
1912	struct mac_biba *subj, *obj;
1913
1914	if (!biba_enabled)
1915		return (0);
1916
1917	subj = SLOT(cred->cr_label);
1918	obj = SLOT(shmseglabel);
1919
1920	if (!biba_dominate_effective(obj, subj))
1921		return (EACCES);
1922
1923	return (0);
1924}
1925
1926static int
1927biba_kld_check_load(struct ucred *cred, struct vnode *vp,
1928    struct label *vplabel)
1929{
1930	struct mac_biba *subj, *obj;
1931	int error;
1932
1933	if (!biba_enabled)
1934		return (0);
1935
1936	subj = SLOT(cred->cr_label);
1937
1938	error = biba_subject_privileged(subj);
1939	if (error)
1940		return (error);
1941
1942	obj = SLOT(vplabel);
1943	if (!biba_high_effective(obj))
1944		return (EACCES);
1945
1946	return (0);
1947}
1948
1949static int
1950biba_mount_check_stat(struct ucred *cred, struct mount *mp,
1951    struct label *mplabel)
1952{
1953	struct mac_biba *subj, *obj;
1954
1955	if (!biba_enabled)
1956		return (0);
1957
1958	subj = SLOT(cred->cr_label);
1959	obj = SLOT(mplabel);
1960
1961	if (!biba_dominate_effective(obj, subj))
1962		return (EACCES);
1963
1964	return (0);
1965}
1966
1967static int
1968biba_pipe_check_ioctl(struct ucred *cred, struct pipepair *pp,
1969    struct label *pplabel, unsigned long cmd, void /* caddr_t */ *data)
1970{
1971
1972	if(!biba_enabled)
1973		return (0);
1974
1975	/* XXX: This will be implemented soon... */
1976
1977	return (0);
1978}
1979
1980static int
1981biba_pipe_check_poll(struct ucred *cred, struct pipepair *pp,
1982    struct label *pplabel)
1983{
1984	struct mac_biba *subj, *obj;
1985
1986	if (!biba_enabled)
1987		return (0);
1988
1989	subj = SLOT(cred->cr_label);
1990	obj = SLOT(pplabel);
1991
1992	if (!biba_dominate_effective(obj, subj))
1993		return (EACCES);
1994
1995	return (0);
1996}
1997
1998static int
1999biba_pipe_check_read(struct ucred *cred, struct pipepair *pp,
2000    struct label *pplabel)
2001{
2002	struct mac_biba *subj, *obj;
2003
2004	if (!biba_enabled)
2005		return (0);
2006
2007	subj = SLOT(cred->cr_label);
2008	obj = SLOT(pplabel);
2009
2010	if (!biba_dominate_effective(obj, subj))
2011		return (EACCES);
2012
2013	return (0);
2014}
2015
2016static int
2017biba_pipe_check_relabel(struct ucred *cred, struct pipepair *pp,
2018    struct label *pplabel, struct label *newlabel)
2019{
2020	struct mac_biba *subj, *obj, *new;
2021	int error;
2022
2023	new = SLOT(newlabel);
2024	subj = SLOT(cred->cr_label);
2025	obj = SLOT(pplabel);
2026
2027	/*
2028	 * If there is a Biba label update for a pipe, it must be a effective
2029	 * update.
2030	 */
2031	error = biba_atmostflags(new, MAC_BIBA_FLAG_EFFECTIVE);
2032	if (error)
2033		return (error);
2034
2035	/*
2036	 * To perform a relabel of a pipe (Biba label or not), Biba must
2037	 * authorize the relabel.
2038	 */
2039	if (!biba_effective_in_range(obj, subj))
2040		return (EPERM);
2041
2042	/*
2043	 * If the Biba label is to be changed, authorize as appropriate.
2044	 */
2045	if (new->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) {
2046		/*
2047		 * To change the Biba label on a pipe, the new pipe label
2048		 * must be in the subject range.
2049		 */
2050		if (!biba_effective_in_range(new, subj))
2051			return (EPERM);
2052
2053		/*
2054		 * To change the Biba label on a pipe to be EQUAL, the
2055		 * subject must have appropriate privilege.
2056		 */
2057		if (biba_contains_equal(new)) {
2058			error = biba_subject_privileged(subj);
2059			if (error)
2060				return (error);
2061		}
2062	}
2063
2064	return (0);
2065}
2066
2067static int
2068biba_pipe_check_stat(struct ucred *cred, struct pipepair *pp,
2069    struct label *pplabel)
2070{
2071	struct mac_biba *subj, *obj;
2072
2073	if (!biba_enabled)
2074		return (0);
2075
2076	subj = SLOT(cred->cr_label);
2077	obj = SLOT(pplabel);
2078
2079	if (!biba_dominate_effective(obj, subj))
2080		return (EACCES);
2081
2082	return (0);
2083}
2084
2085static int
2086biba_pipe_check_write(struct ucred *cred, struct pipepair *pp,
2087    struct label *pplabel)
2088{
2089	struct mac_biba *subj, *obj;
2090
2091	if (!biba_enabled)
2092		return (0);
2093
2094	subj = SLOT(cred->cr_label);
2095	obj = SLOT(pplabel);
2096
2097	if (!biba_dominate_effective(subj, obj))
2098		return (EACCES);
2099
2100	return (0);
2101}
2102
2103static int
2104biba_posixsem_check_write(struct ucred *cred, struct ksem *ks,
2105    struct label *kslabel)
2106{
2107	struct mac_biba *subj, *obj;
2108
2109	if (!biba_enabled)
2110		return (0);
2111
2112	subj = SLOT(cred->cr_label);
2113	obj = SLOT(kslabel);
2114
2115	if (!biba_dominate_effective(subj, obj))
2116		return (EACCES);
2117
2118	return (0);
2119}
2120
2121static int
2122biba_posixsem_check_rdonly(struct ucred *cred, struct ksem *ks,
2123    struct label *kslabel)
2124{
2125	struct mac_biba *subj, *obj;
2126
2127	if (!biba_enabled)
2128		return (0);
2129
2130	subj = SLOT(cred->cr_label);
2131	obj = SLOT(kslabel);
2132
2133	if (!biba_dominate_effective(obj, subj))
2134		return (EACCES);
2135
2136	return (0);
2137}
2138
2139static int
2140biba_proc_check_debug(struct ucred *cred, struct proc *p)
2141{
2142	struct mac_biba *subj, *obj;
2143
2144	if (!biba_enabled)
2145		return (0);
2146
2147	subj = SLOT(cred->cr_label);
2148	obj = SLOT(p->p_ucred->cr_label);
2149
2150	/* XXX: range checks */
2151	if (!biba_dominate_effective(obj, subj))
2152		return (ESRCH);
2153	if (!biba_dominate_effective(subj, obj))
2154		return (EACCES);
2155
2156	return (0);
2157}
2158
2159static int
2160biba_proc_check_sched(struct ucred *cred, struct proc *p)
2161{
2162	struct mac_biba *subj, *obj;
2163
2164	if (!biba_enabled)
2165		return (0);
2166
2167	subj = SLOT(cred->cr_label);
2168	obj = SLOT(p->p_ucred->cr_label);
2169
2170	/* XXX: range checks */
2171	if (!biba_dominate_effective(obj, subj))
2172		return (ESRCH);
2173	if (!biba_dominate_effective(subj, obj))
2174		return (EACCES);
2175
2176	return (0);
2177}
2178
2179static int
2180biba_proc_check_signal(struct ucred *cred, struct proc *p, int signum)
2181{
2182	struct mac_biba *subj, *obj;
2183
2184	if (!biba_enabled)
2185		return (0);
2186
2187	subj = SLOT(cred->cr_label);
2188	obj = SLOT(p->p_ucred->cr_label);
2189
2190	/* XXX: range checks */
2191	if (!biba_dominate_effective(obj, subj))
2192		return (ESRCH);
2193	if (!biba_dominate_effective(subj, obj))
2194		return (EACCES);
2195
2196	return (0);
2197}
2198
2199static int
2200biba_socket_check_deliver(struct socket *so, struct label *solabel,
2201    struct mbuf *m, struct label *mlabel)
2202{
2203	struct mac_biba *p, *s;
2204
2205	if (!biba_enabled)
2206		return (0);
2207
2208	p = SLOT(mlabel);
2209	s = SLOT(solabel);
2210
2211	return (biba_equal_effective(p, s) ? 0 : EACCES);
2212}
2213
2214static int
2215biba_socket_check_relabel(struct ucred *cred, struct socket *so,
2216    struct label *solabel, struct label *newlabel)
2217{
2218	struct mac_biba *subj, *obj, *new;
2219	int error;
2220
2221	new = SLOT(newlabel);
2222	subj = SLOT(cred->cr_label);
2223	obj = SLOT(solabel);
2224
2225	/*
2226	 * If there is a Biba label update for the socket, it may be an
2227	 * update of effective.
2228	 */
2229	error = biba_atmostflags(new, MAC_BIBA_FLAG_EFFECTIVE);
2230	if (error)
2231		return (error);
2232
2233	/*
2234	 * To relabel a socket, the old socket effective must be in the
2235	 * subject range.
2236	 */
2237	if (!biba_effective_in_range(obj, subj))
2238		return (EPERM);
2239
2240	/*
2241	 * If the Biba label is to be changed, authorize as appropriate.
2242	 */
2243	if (new->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) {
2244		/*
2245		 * To relabel a socket, the new socket effective must be in
2246		 * the subject range.
2247		 */
2248		if (!biba_effective_in_range(new, subj))
2249			return (EPERM);
2250
2251		/*
2252		 * To change the Biba label on the socket to contain EQUAL,
2253		 * the subject must have appropriate privilege.
2254		 */
2255		if (biba_contains_equal(new)) {
2256			error = biba_subject_privileged(subj);
2257			if (error)
2258				return (error);
2259		}
2260	}
2261
2262	return (0);
2263}
2264
2265static int
2266biba_socket_check_visible(struct ucred *cred, struct socket *so,
2267    struct label *solabel)
2268{
2269	struct mac_biba *subj, *obj;
2270
2271	if (!biba_enabled)
2272		return (0);
2273
2274	subj = SLOT(cred->cr_label);
2275	obj = SLOT(solabel);
2276
2277	if (!biba_dominate_effective(obj, subj))
2278		return (ENOENT);
2279
2280	return (0);
2281}
2282
2283/*
2284 * Some system privileges are allowed regardless of integrity grade; others
2285 * are allowed only when running with privilege with respect to the Biba
2286 * policy as they might otherwise allow bypassing of the integrity policy.
2287 */
2288static int
2289biba_priv_check(struct ucred *cred, int priv)
2290{
2291	struct mac_biba *subj;
2292	int error;
2293
2294	if (!biba_enabled)
2295		return (0);
2296
2297	/*
2298	 * Exempt only specific privileges from the Biba integrity policy.
2299	 */
2300	switch (priv) {
2301	case PRIV_KTRACE:
2302	case PRIV_MSGBUF:
2303
2304	/*
2305	 * Allow processes to manipulate basic process audit properties, and
2306	 * to submit audit records.
2307	 */
2308	case PRIV_AUDIT_GETAUDIT:
2309	case PRIV_AUDIT_SETAUDIT:
2310	case PRIV_AUDIT_SUBMIT:
2311
2312	/*
2313	 * Allow processes to manipulate their regular UNIX credentials.
2314	 */
2315	case PRIV_CRED_SETUID:
2316	case PRIV_CRED_SETEUID:
2317	case PRIV_CRED_SETGID:
2318	case PRIV_CRED_SETEGID:
2319	case PRIV_CRED_SETGROUPS:
2320	case PRIV_CRED_SETREUID:
2321	case PRIV_CRED_SETREGID:
2322	case PRIV_CRED_SETRESUID:
2323	case PRIV_CRED_SETRESGID:
2324
2325	/*
2326	 * Allow processes to perform system monitoring.
2327	 */
2328	case PRIV_SEEOTHERGIDS:
2329	case PRIV_SEEOTHERUIDS:
2330		break;
2331
2332	/*
2333	 * Allow access to general process debugging facilities.  We
2334	 * separately control debugging based on MAC label.
2335	 */
2336	case PRIV_DEBUG_DIFFCRED:
2337	case PRIV_DEBUG_SUGID:
2338	case PRIV_DEBUG_UNPRIV:
2339
2340	/*
2341	 * Allow manipulating jails.
2342	 */
2343	case PRIV_JAIL_ATTACH:
2344
2345	/*
2346	 * Allow privilege with respect to the Partition policy, but not the
2347	 * Privs policy.
2348	 */
2349	case PRIV_MAC_PARTITION:
2350
2351	/*
2352	 * Allow privilege with respect to process resource limits and login
2353	 * context.
2354	 */
2355	case PRIV_PROC_LIMIT:
2356	case PRIV_PROC_SETLOGIN:
2357	case PRIV_PROC_SETRLIMIT:
2358
2359	/*
2360	 * Allow System V and POSIX IPC privileges.
2361	 */
2362	case PRIV_IPC_READ:
2363	case PRIV_IPC_WRITE:
2364	case PRIV_IPC_ADMIN:
2365	case PRIV_IPC_MSGSIZE:
2366	case PRIV_MQ_ADMIN:
2367
2368	/*
2369	 * Allow certain scheduler manipulations -- possibly this should be
2370	 * controlled by more fine-grained policy, as potentially low
2371	 * integrity processes can deny CPU to higher integrity ones.
2372	 */
2373	case PRIV_SCHED_DIFFCRED:
2374	case PRIV_SCHED_SETPRIORITY:
2375	case PRIV_SCHED_RTPRIO:
2376	case PRIV_SCHED_SETPOLICY:
2377	case PRIV_SCHED_SET:
2378	case PRIV_SCHED_SETPARAM:
2379
2380	/*
2381	 * More IPC privileges.
2382	 */
2383	case PRIV_SEM_WRITE:
2384
2385	/*
2386	 * Allow signaling privileges subject to integrity policy.
2387	 */
2388	case PRIV_SIGNAL_DIFFCRED:
2389	case PRIV_SIGNAL_SUGID:
2390
2391	/*
2392	 * Allow access to only limited sysctls from lower integrity levels;
2393	 * piggy-back on the Jail definition.
2394	 */
2395	case PRIV_SYSCTL_WRITEJAIL:
2396
2397	/*
2398	 * Allow TTY-based privileges, subject to general device access using
2399	 * labels on TTY device nodes, but not console privilege.
2400	 */
2401	case PRIV_TTY_DRAINWAIT:
2402	case PRIV_TTY_DTRWAIT:
2403	case PRIV_TTY_EXCLUSIVE:
2404	case PRIV_TTY_PRISON:
2405	case PRIV_TTY_STI:
2406	case PRIV_TTY_SETA:
2407
2408	/*
2409	 * Grant most VFS privileges, as almost all are in practice bounded
2410	 * by more specific checks using labels.
2411	 */
2412	case PRIV_VFS_READ:
2413	case PRIV_VFS_WRITE:
2414	case PRIV_VFS_ADMIN:
2415	case PRIV_VFS_EXEC:
2416	case PRIV_VFS_LOOKUP:
2417	case PRIV_VFS_CHFLAGS_DEV:
2418	case PRIV_VFS_CHOWN:
2419	case PRIV_VFS_CHROOT:
2420	case PRIV_VFS_RETAINSUGID:
2421	case PRIV_VFS_EXCEEDQUOTA:
2422	case PRIV_VFS_FCHROOT:
2423	case PRIV_VFS_FHOPEN:
2424	case PRIV_VFS_FHSTATFS:
2425	case PRIV_VFS_GENERATION:
2426	case PRIV_VFS_GETFH:
2427	case PRIV_VFS_GETQUOTA:
2428	case PRIV_VFS_LINK:
2429	case PRIV_VFS_MOUNT:
2430	case PRIV_VFS_MOUNT_OWNER:
2431	case PRIV_VFS_MOUNT_PERM:
2432	case PRIV_VFS_MOUNT_SUIDDIR:
2433	case PRIV_VFS_MOUNT_NONUSER:
2434	case PRIV_VFS_SETGID:
2435	case PRIV_VFS_STICKYFILE:
2436	case PRIV_VFS_SYSFLAGS:
2437	case PRIV_VFS_UNMOUNT:
2438
2439	/*
2440	 * Allow VM privileges; it would be nice if these were subject to
2441	 * resource limits.
2442	 */
2443	case PRIV_VM_MADV_PROTECT:
2444	case PRIV_VM_MLOCK:
2445	case PRIV_VM_MUNLOCK:
2446
2447	/*
2448	 * Allow some but not all network privileges.  In general, dont allow
2449	 * reconfiguring the network stack, just normal use.
2450	 */
2451	case PRIV_NETATALK_RESERVEDPORT:
2452	case PRIV_NETINET_RESERVEDPORT:
2453	case PRIV_NETINET_RAW:
2454	case PRIV_NETINET_REUSEPORT:
2455	case PRIV_NETIPX_RESERVEDPORT:
2456	case PRIV_NETIPX_RAW:
2457		break;
2458
2459	/*
2460	 * All remaining system privileges are allow only if the process
2461	 * holds privilege with respect to the Biba policy.
2462	 */
2463	default:
2464		subj = SLOT(cred->cr_label);
2465		error = biba_subject_privileged(subj);
2466		if (error)
2467			return (error);
2468	}
2469	return (0);
2470}
2471
2472static int
2473biba_system_check_acct(struct ucred *cred, struct vnode *vp,
2474    struct label *vplabel)
2475{
2476	struct mac_biba *subj, *obj;
2477	int error;
2478
2479	if (!biba_enabled)
2480		return (0);
2481
2482	subj = SLOT(cred->cr_label);
2483
2484	error = biba_subject_privileged(subj);
2485	if (error)
2486		return (error);
2487
2488	if (vplabel == NULL)
2489		return (0);
2490
2491	obj = SLOT(vplabel);
2492	if (!biba_high_effective(obj))
2493		return (EACCES);
2494
2495	return (0);
2496}
2497
2498static int
2499biba_system_check_auditctl(struct ucred *cred, struct vnode *vp,
2500    struct label *vplabel)
2501{
2502	struct mac_biba *subj, *obj;
2503	int error;
2504
2505	if (!biba_enabled)
2506		return (0);
2507
2508	subj = SLOT(cred->cr_label);
2509
2510	error = biba_subject_privileged(subj);
2511	if (error)
2512		return (error);
2513
2514	if (vplabel == NULL)
2515		return (0);
2516
2517	obj = SLOT(vplabel);
2518	if (!biba_high_effective(obj))
2519		return (EACCES);
2520
2521	return (0);
2522}
2523
2524static int
2525biba_system_check_auditon(struct ucred *cred, int cmd)
2526{
2527	struct mac_biba *subj;
2528	int error;
2529
2530	if (!biba_enabled)
2531		return (0);
2532
2533	subj = SLOT(cred->cr_label);
2534
2535	error = biba_subject_privileged(subj);
2536	if (error)
2537		return (error);
2538
2539	return (0);
2540}
2541
2542static int
2543biba_system_check_swapon(struct ucred *cred, struct vnode *vp,
2544    struct label *vplabel)
2545{
2546	struct mac_biba *subj, *obj;
2547	int error;
2548
2549	if (!biba_enabled)
2550		return (0);
2551
2552	subj = SLOT(cred->cr_label);
2553	obj = SLOT(vplabel);
2554
2555	error = biba_subject_privileged(subj);
2556	if (error)
2557		return (error);
2558
2559	if (!biba_high_effective(obj))
2560		return (EACCES);
2561
2562	return (0);
2563}
2564
2565static int
2566biba_system_check_swapoff(struct ucred *cred, struct vnode *vp,
2567    struct label *label)
2568{
2569	struct mac_biba *subj;
2570	int error;
2571
2572	if (!biba_enabled)
2573		return (0);
2574
2575	subj = SLOT(cred->cr_label);
2576
2577	error = biba_subject_privileged(subj);
2578	if (error)
2579		return (error);
2580
2581	return (0);
2582}
2583
2584static int
2585biba_system_check_sysctl(struct ucred *cred, struct sysctl_oid *oidp,
2586    void *arg1, int arg2, struct sysctl_req *req)
2587{
2588	struct mac_biba *subj;
2589	int error;
2590
2591	if (!biba_enabled)
2592		return (0);
2593
2594	subj = SLOT(cred->cr_label);
2595
2596	/*
2597	 * Treat sysctl variables without CTLFLAG_ANYBODY flag as biba/high,
2598	 * but also require privilege to change them.
2599	 */
2600	if (req->newptr != NULL && (oidp->oid_kind & CTLFLAG_ANYBODY) == 0) {
2601		if (!biba_subject_dominate_high(subj))
2602			return (EACCES);
2603
2604		error = biba_subject_privileged(subj);
2605		if (error)
2606			return (error);
2607	}
2608
2609	return (0);
2610}
2611
2612static int
2613biba_vnode_check_chdir(struct ucred *cred, struct vnode *dvp,
2614    struct label *dvplabel)
2615{
2616	struct mac_biba *subj, *obj;
2617
2618	if (!biba_enabled)
2619		return (0);
2620
2621	subj = SLOT(cred->cr_label);
2622	obj = SLOT(dvplabel);
2623
2624	if (!biba_dominate_effective(obj, subj))
2625		return (EACCES);
2626
2627	return (0);
2628}
2629
2630static int
2631biba_vnode_check_chroot(struct ucred *cred, struct vnode *dvp,
2632    struct label *dvplabel)
2633{
2634	struct mac_biba *subj, *obj;
2635
2636	if (!biba_enabled)
2637		return (0);
2638
2639	subj = SLOT(cred->cr_label);
2640	obj = SLOT(dvplabel);
2641
2642	if (!biba_dominate_effective(obj, subj))
2643		return (EACCES);
2644
2645	return (0);
2646}
2647
2648static int
2649biba_vnode_check_create(struct ucred *cred, struct vnode *dvp,
2650    struct label *dvplabel, struct componentname *cnp, struct vattr *vap)
2651{
2652	struct mac_biba *subj, *obj;
2653
2654	if (!biba_enabled)
2655		return (0);
2656
2657	subj = SLOT(cred->cr_label);
2658	obj = SLOT(dvplabel);
2659
2660	if (!biba_dominate_effective(subj, obj))
2661		return (EACCES);
2662
2663	return (0);
2664}
2665
2666static int
2667biba_vnode_check_deleteacl(struct ucred *cred, struct vnode *vp,
2668    struct label *vplabel, acl_type_t type)
2669{
2670	struct mac_biba *subj, *obj;
2671
2672	if (!biba_enabled)
2673		return (0);
2674
2675	subj = SLOT(cred->cr_label);
2676	obj = SLOT(vplabel);
2677
2678	if (!biba_dominate_effective(subj, obj))
2679		return (EACCES);
2680
2681	return (0);
2682}
2683
2684static int
2685biba_vnode_check_deleteextattr(struct ucred *cred, struct vnode *vp,
2686    struct label *vplabel, int attrnamespace, const char *name)
2687{
2688	struct mac_biba *subj, *obj;
2689
2690	if (!biba_enabled)
2691		return (0);
2692
2693	subj = SLOT(cred->cr_label);
2694	obj = SLOT(vplabel);
2695
2696	if (!biba_dominate_effective(subj, obj))
2697		return (EACCES);
2698
2699	return (0);
2700}
2701
2702static int
2703biba_vnode_check_exec(struct ucred *cred, struct vnode *vp,
2704    struct label *vplabel, struct image_params *imgp,
2705    struct label *execlabel)
2706{
2707	struct mac_biba *subj, *obj, *exec;
2708	int error;
2709
2710	if (execlabel != NULL) {
2711		/*
2712		 * We currently don't permit labels to be changed at
2713		 * exec-time as part of Biba, so disallow non-NULL Biba label
2714		 * elements in the execlabel.
2715		 */
2716		exec = SLOT(execlabel);
2717		error = biba_atmostflags(exec, 0);
2718		if (error)
2719			return (error);
2720	}
2721
2722	if (!biba_enabled)
2723		return (0);
2724
2725	subj = SLOT(cred->cr_label);
2726	obj = SLOT(vplabel);
2727
2728	if (!biba_dominate_effective(obj, subj))
2729		return (EACCES);
2730
2731	return (0);
2732}
2733
2734static int
2735biba_vnode_check_getacl(struct ucred *cred, struct vnode *vp,
2736    struct label *vplabel, acl_type_t type)
2737{
2738	struct mac_biba *subj, *obj;
2739
2740	if (!biba_enabled)
2741		return (0);
2742
2743	subj = SLOT(cred->cr_label);
2744	obj = SLOT(vplabel);
2745
2746	if (!biba_dominate_effective(obj, subj))
2747		return (EACCES);
2748
2749	return (0);
2750}
2751
2752static int
2753biba_vnode_check_getextattr(struct ucred *cred, struct vnode *vp,
2754    struct label *vplabel, int attrnamespace, const char *name,
2755    struct uio *uio)
2756{
2757	struct mac_biba *subj, *obj;
2758
2759	if (!biba_enabled)
2760		return (0);
2761
2762	subj = SLOT(cred->cr_label);
2763	obj = SLOT(vplabel);
2764
2765	if (!biba_dominate_effective(obj, subj))
2766		return (EACCES);
2767
2768	return (0);
2769}
2770
2771static int
2772biba_vnode_check_link(struct ucred *cred, struct vnode *dvp,
2773    struct label *dvplabel, struct vnode *vp, struct label *vplabel,
2774    struct componentname *cnp)
2775{
2776	struct mac_biba *subj, *obj;
2777
2778	if (!biba_enabled)
2779		return (0);
2780
2781	subj = SLOT(cred->cr_label);
2782	obj = SLOT(dvplabel);
2783
2784	if (!biba_dominate_effective(subj, obj))
2785		return (EACCES);
2786
2787	obj = SLOT(vplabel);
2788
2789	if (!biba_dominate_effective(subj, obj))
2790		return (EACCES);
2791
2792	return (0);
2793}
2794
2795static int
2796biba_vnode_check_listextattr(struct ucred *cred, struct vnode *vp,
2797    struct label *vplabel, int attrnamespace)
2798{
2799	struct mac_biba *subj, *obj;
2800
2801	if (!biba_enabled)
2802		return (0);
2803
2804	subj = SLOT(cred->cr_label);
2805	obj = SLOT(vplabel);
2806
2807	if (!biba_dominate_effective(obj, subj))
2808		return (EACCES);
2809
2810	return (0);
2811}
2812
2813static int
2814biba_vnode_check_lookup(struct ucred *cred, struct vnode *dvp,
2815    struct label *dvplabel, struct componentname *cnp)
2816{
2817	struct mac_biba *subj, *obj;
2818
2819	if (!biba_enabled)
2820		return (0);
2821
2822	subj = SLOT(cred->cr_label);
2823	obj = SLOT(dvplabel);
2824
2825	if (!biba_dominate_effective(obj, subj))
2826		return (EACCES);
2827
2828	return (0);
2829}
2830
2831static int
2832biba_vnode_check_mmap(struct ucred *cred, struct vnode *vp,
2833    struct label *vplabel, int prot, int flags)
2834{
2835	struct mac_biba *subj, *obj;
2836
2837	/*
2838	 * Rely on the use of open()-time protections to handle
2839	 * non-revocation cases.
2840	 */
2841	if (!biba_enabled || !revocation_enabled)
2842		return (0);
2843
2844	subj = SLOT(cred->cr_label);
2845	obj = SLOT(vplabel);
2846
2847	if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
2848		if (!biba_dominate_effective(obj, subj))
2849			return (EACCES);
2850	}
2851	if (((prot & VM_PROT_WRITE) != 0) && ((flags & MAP_SHARED) != 0)) {
2852		if (!biba_dominate_effective(subj, obj))
2853			return (EACCES);
2854	}
2855
2856	return (0);
2857}
2858
2859static int
2860biba_vnode_check_open(struct ucred *cred, struct vnode *vp,
2861    struct label *vplabel, int acc_mode)
2862{
2863	struct mac_biba *subj, *obj;
2864
2865	if (!biba_enabled)
2866		return (0);
2867
2868	subj = SLOT(cred->cr_label);
2869	obj = SLOT(vplabel);
2870
2871	/* XXX privilege override for admin? */
2872	if (acc_mode & (VREAD | VEXEC | VSTAT)) {
2873		if (!biba_dominate_effective(obj, subj))
2874			return (EACCES);
2875	}
2876	if (acc_mode & (VWRITE | VAPPEND | VADMIN)) {
2877		if (!biba_dominate_effective(subj, obj))
2878			return (EACCES);
2879	}
2880
2881	return (0);
2882}
2883
2884static int
2885biba_vnode_check_poll(struct ucred *active_cred, struct ucred *file_cred,
2886    struct vnode *vp, struct label *vplabel)
2887{
2888	struct mac_biba *subj, *obj;
2889
2890	if (!biba_enabled || !revocation_enabled)
2891		return (0);
2892
2893	subj = SLOT(active_cred->cr_label);
2894	obj = SLOT(vplabel);
2895
2896	if (!biba_dominate_effective(obj, subj))
2897		return (EACCES);
2898
2899	return (0);
2900}
2901
2902static int
2903biba_vnode_check_read(struct ucred *active_cred, struct ucred *file_cred,
2904    struct vnode *vp, struct label *vplabel)
2905{
2906	struct mac_biba *subj, *obj;
2907
2908	if (!biba_enabled || !revocation_enabled)
2909		return (0);
2910
2911	subj = SLOT(active_cred->cr_label);
2912	obj = SLOT(vplabel);
2913
2914	if (!biba_dominate_effective(obj, subj))
2915		return (EACCES);
2916
2917	return (0);
2918}
2919
2920static int
2921biba_vnode_check_readdir(struct ucred *cred, struct vnode *dvp,
2922    struct label *dvplabel)
2923{
2924	struct mac_biba *subj, *obj;
2925
2926	if (!biba_enabled)
2927		return (0);
2928
2929	subj = SLOT(cred->cr_label);
2930	obj = SLOT(dvplabel);
2931
2932	if (!biba_dominate_effective(obj, subj))
2933		return (EACCES);
2934
2935	return (0);
2936}
2937
2938static int
2939biba_vnode_check_readlink(struct ucred *cred, struct vnode *vp,
2940    struct label *vplabel)
2941{
2942	struct mac_biba *subj, *obj;
2943
2944	if (!biba_enabled)
2945		return (0);
2946
2947	subj = SLOT(cred->cr_label);
2948	obj = SLOT(vplabel);
2949
2950	if (!biba_dominate_effective(obj, subj))
2951		return (EACCES);
2952
2953	return (0);
2954}
2955
2956static int
2957biba_vnode_check_relabel(struct ucred *cred, struct vnode *vp,
2958    struct label *vplabel, struct label *newlabel)
2959{
2960	struct mac_biba *old, *new, *subj;
2961	int error;
2962
2963	old = SLOT(vplabel);
2964	new = SLOT(newlabel);
2965	subj = SLOT(cred->cr_label);
2966
2967	/*
2968	 * If there is a Biba label update for the vnode, it must be a
2969	 * effective label.
2970	 */
2971	error = biba_atmostflags(new, MAC_BIBA_FLAG_EFFECTIVE);
2972	if (error)
2973		return (error);
2974
2975	/*
2976	 * To perform a relabel of the vnode (Biba label or not), Biba must
2977	 * authorize the relabel.
2978	 */
2979	if (!biba_effective_in_range(old, subj))
2980		return (EPERM);
2981
2982	/*
2983	 * If the Biba label is to be changed, authorize as appropriate.
2984	 */
2985	if (new->mb_flags & MAC_BIBA_FLAG_EFFECTIVE) {
2986		/*
2987		 * To change the Biba label on a vnode, the new vnode label
2988		 * must be in the subject range.
2989		 */
2990		if (!biba_effective_in_range(new, subj))
2991			return (EPERM);
2992
2993		/*
2994		 * To change the Biba label on the vnode to be EQUAL, the
2995		 * subject must have appropriate privilege.
2996		 */
2997		if (biba_contains_equal(new)) {
2998			error = biba_subject_privileged(subj);
2999			if (error)
3000				return (error);
3001		}
3002	}
3003
3004	return (0);
3005}
3006
3007static int
3008biba_vnode_check_rename_from(struct ucred *cred, struct vnode *dvp,
3009    struct label *dvplabel, struct vnode *vp, struct label *vplabel,
3010    struct componentname *cnp)
3011{
3012	struct mac_biba *subj, *obj;
3013
3014	if (!biba_enabled)
3015		return (0);
3016
3017	subj = SLOT(cred->cr_label);
3018	obj = SLOT(dvplabel);
3019
3020	if (!biba_dominate_effective(subj, obj))
3021		return (EACCES);
3022
3023	obj = SLOT(vplabel);
3024
3025	if (!biba_dominate_effective(subj, obj))
3026		return (EACCES);
3027
3028	return (0);
3029}
3030
3031static int
3032biba_vnode_check_rename_to(struct ucred *cred, struct vnode *dvp,
3033    struct label *dvplabel, struct vnode *vp, struct label *vplabel,
3034    int samedir, struct componentname *cnp)
3035{
3036	struct mac_biba *subj, *obj;
3037
3038	if (!biba_enabled)
3039		return (0);
3040
3041	subj = SLOT(cred->cr_label);
3042	obj = SLOT(dvplabel);
3043
3044	if (!biba_dominate_effective(subj, obj))
3045		return (EACCES);
3046
3047	if (vp != NULL) {
3048		obj = SLOT(vplabel);
3049
3050		if (!biba_dominate_effective(subj, obj))
3051			return (EACCES);
3052	}
3053
3054	return (0);
3055}
3056
3057static int
3058biba_vnode_check_revoke(struct ucred *cred, struct vnode *vp,
3059    struct label *vplabel)
3060{
3061	struct mac_biba *subj, *obj;
3062
3063	if (!biba_enabled)
3064		return (0);
3065
3066	subj = SLOT(cred->cr_label);
3067	obj = SLOT(vplabel);
3068
3069	if (!biba_dominate_effective(subj, obj))
3070		return (EACCES);
3071
3072	return (0);
3073}
3074
3075static int
3076biba_vnode_check_setacl(struct ucred *cred, struct vnode *vp,
3077    struct label *vplabel, acl_type_t type, struct acl *acl)
3078{
3079	struct mac_biba *subj, *obj;
3080
3081	if (!biba_enabled)
3082		return (0);
3083
3084	subj = SLOT(cred->cr_label);
3085	obj = SLOT(vplabel);
3086
3087	if (!biba_dominate_effective(subj, obj))
3088		return (EACCES);
3089
3090	return (0);
3091}
3092
3093static int
3094biba_vnode_check_setextattr(struct ucred *cred, struct vnode *vp,
3095    struct label *vplabel, int attrnamespace, const char *name,
3096    struct uio *uio)
3097{
3098	struct mac_biba *subj, *obj;
3099
3100	if (!biba_enabled)
3101		return (0);
3102
3103	subj = SLOT(cred->cr_label);
3104	obj = SLOT(vplabel);
3105
3106	if (!biba_dominate_effective(subj, obj))
3107		return (EACCES);
3108
3109	/* XXX: protect the MAC EA in a special way? */
3110
3111	return (0);
3112}
3113
3114static int
3115biba_vnode_check_setflags(struct ucred *cred, struct vnode *vp,
3116    struct label *vplabel, u_long flags)
3117{
3118	struct mac_biba *subj, *obj;
3119
3120	if (!biba_enabled)
3121		return (0);
3122
3123	subj = SLOT(cred->cr_label);
3124	obj = SLOT(vplabel);
3125
3126	if (!biba_dominate_effective(subj, obj))
3127		return (EACCES);
3128
3129	return (0);
3130}
3131
3132static int
3133biba_vnode_check_setmode(struct ucred *cred, struct vnode *vp,
3134    struct label *vplabel, mode_t mode)
3135{
3136	struct mac_biba *subj, *obj;
3137
3138	if (!biba_enabled)
3139		return (0);
3140
3141	subj = SLOT(cred->cr_label);
3142	obj = SLOT(vplabel);
3143
3144	if (!biba_dominate_effective(subj, obj))
3145		return (EACCES);
3146
3147	return (0);
3148}
3149
3150static int
3151biba_vnode_check_setowner(struct ucred *cred, struct vnode *vp,
3152    struct label *vplabel, uid_t uid, gid_t gid)
3153{
3154	struct mac_biba *subj, *obj;
3155
3156	if (!biba_enabled)
3157		return (0);
3158
3159	subj = SLOT(cred->cr_label);
3160	obj = SLOT(vplabel);
3161
3162	if (!biba_dominate_effective(subj, obj))
3163		return (EACCES);
3164
3165	return (0);
3166}
3167
3168static int
3169biba_vnode_check_setutimes(struct ucred *cred, struct vnode *vp,
3170    struct label *vplabel, struct timespec atime, struct timespec mtime)
3171{
3172	struct mac_biba *subj, *obj;
3173
3174	if (!biba_enabled)
3175		return (0);
3176
3177	subj = SLOT(cred->cr_label);
3178	obj = SLOT(vplabel);
3179
3180	if (!biba_dominate_effective(subj, obj))
3181		return (EACCES);
3182
3183	return (0);
3184}
3185
3186static int
3187biba_vnode_check_stat(struct ucred *active_cred, struct ucred *file_cred,
3188    struct vnode *vp, struct label *vplabel)
3189{
3190	struct mac_biba *subj, *obj;
3191
3192	if (!biba_enabled)
3193		return (0);
3194
3195	subj = SLOT(active_cred->cr_label);
3196	obj = SLOT(vplabel);
3197
3198	if (!biba_dominate_effective(obj, subj))
3199		return (EACCES);
3200
3201	return (0);
3202}
3203
3204static int
3205biba_vnode_check_unlink(struct ucred *cred, struct vnode *dvp,
3206    struct label *dvplabel, struct vnode *vp, struct label *vplabel,
3207    struct componentname *cnp)
3208{
3209	struct mac_biba *subj, *obj;
3210
3211	if (!biba_enabled)
3212		return (0);
3213
3214	subj = SLOT(cred->cr_label);
3215	obj = SLOT(dvplabel);
3216
3217	if (!biba_dominate_effective(subj, obj))
3218		return (EACCES);
3219
3220	obj = SLOT(vplabel);
3221
3222	if (!biba_dominate_effective(subj, obj))
3223		return (EACCES);
3224
3225	return (0);
3226}
3227
3228static int
3229biba_vnode_check_write(struct ucred *active_cred,
3230    struct ucred *file_cred, struct vnode *vp, struct label *vplabel)
3231{
3232	struct mac_biba *subj, *obj;
3233
3234	if (!biba_enabled || !revocation_enabled)
3235		return (0);
3236
3237	subj = SLOT(active_cred->cr_label);
3238	obj = SLOT(vplabel);
3239
3240	if (!biba_dominate_effective(subj, obj))
3241		return (EACCES);
3242
3243	return (0);
3244}
3245
3246static void
3247biba_syncache_create(struct label *label, struct inpcb *inp)
3248{
3249	struct mac_biba *source, *dest;
3250
3251	source = SLOT(inp->inp_label);
3252	dest = SLOT(label);
3253	biba_copy_effective(source, dest);
3254}
3255
3256static void
3257biba_syncache_create_mbuf(struct label *sc_label, struct mbuf *m,
3258    struct label *mlabel)
3259{
3260	struct mac_biba *source, *dest;
3261
3262	source = SLOT(sc_label);
3263	dest = SLOT(mlabel);
3264	biba_copy_effective(source, dest);
3265}
3266
3267static struct mac_policy_ops mac_biba_ops =
3268{
3269	.mpo_init = biba_init,
3270	.mpo_bpfdesc_init_label = biba_init_label,
3271	.mpo_cred_init_label = biba_init_label,
3272	.mpo_devfs_init_label = biba_init_label,
3273	.mpo_ifnet_init_label = biba_init_label,
3274	.mpo_inpcb_init_label = biba_init_label_waitcheck,
3275	.mpo_syncache_init_label = biba_init_label_waitcheck,
3276	.mpo_sysvmsg_init_label = biba_init_label,
3277	.mpo_sysvmsq_init_label = biba_init_label,
3278	.mpo_sysvsem_init_label = biba_init_label,
3279	.mpo_sysvshm_init_label = biba_init_label,
3280	.mpo_ipq_init_label = biba_init_label_waitcheck,
3281	.mpo_mbuf_init_label = biba_init_label_waitcheck,
3282	.mpo_mount_init_label = biba_init_label,
3283	.mpo_pipe_init_label = biba_init_label,
3284	.mpo_posixsem_init_label = biba_init_label,
3285	.mpo_socket_init_label = biba_init_label_waitcheck,
3286	.mpo_socketpeer_init_label = biba_init_label_waitcheck,
3287	.mpo_syncache_create = biba_syncache_create,
3288	.mpo_vnode_init_label = biba_init_label,
3289	.mpo_bpfdesc_destroy_label = biba_destroy_label,
3290	.mpo_cred_destroy_label = biba_destroy_label,
3291	.mpo_devfs_destroy_label = biba_destroy_label,
3292	.mpo_ifnet_destroy_label = biba_destroy_label,
3293	.mpo_inpcb_destroy_label = biba_destroy_label,
3294	.mpo_syncache_destroy_label = biba_destroy_label,
3295	.mpo_sysvmsg_destroy_label = biba_destroy_label,
3296	.mpo_sysvmsq_destroy_label = biba_destroy_label,
3297	.mpo_sysvsem_destroy_label = biba_destroy_label,
3298	.mpo_sysvshm_destroy_label = biba_destroy_label,
3299	.mpo_ipq_destroy_label = biba_destroy_label,
3300	.mpo_mbuf_destroy_label = biba_destroy_label,
3301	.mpo_mount_destroy_label = biba_destroy_label,
3302	.mpo_pipe_destroy_label = biba_destroy_label,
3303	.mpo_posixsem_destroy_label = biba_destroy_label,
3304	.mpo_socket_destroy_label = biba_destroy_label,
3305	.mpo_socketpeer_destroy_label = biba_destroy_label,
3306	.mpo_vnode_destroy_label = biba_destroy_label,
3307	.mpo_cred_copy_label = biba_copy_label,
3308	.mpo_ifnet_copy_label = biba_copy_label,
3309	.mpo_mbuf_copy_label = biba_copy_label,
3310	.mpo_pipe_copy_label = biba_copy_label,
3311	.mpo_socket_copy_label = biba_copy_label,
3312	.mpo_vnode_copy_label = biba_copy_label,
3313	.mpo_cred_externalize_label = biba_externalize_label,
3314	.mpo_ifnet_externalize_label = biba_externalize_label,
3315	.mpo_pipe_externalize_label = biba_externalize_label,
3316	.mpo_socket_externalize_label = biba_externalize_label,
3317	.mpo_socketpeer_externalize_label = biba_externalize_label,
3318	.mpo_vnode_externalize_label = biba_externalize_label,
3319	.mpo_cred_internalize_label = biba_internalize_label,
3320	.mpo_ifnet_internalize_label = biba_internalize_label,
3321	.mpo_pipe_internalize_label = biba_internalize_label,
3322	.mpo_socket_internalize_label = biba_internalize_label,
3323	.mpo_vnode_internalize_label = biba_internalize_label,
3324	.mpo_devfs_create_device = biba_devfs_create_device,
3325	.mpo_devfs_create_directory = biba_devfs_create_directory,
3326	.mpo_devfs_create_symlink = biba_devfs_create_symlink,
3327	.mpo_mount_create = biba_mount_create,
3328	.mpo_vnode_relabel = biba_vnode_relabel,
3329	.mpo_devfs_update = biba_devfs_update,
3330	.mpo_devfs_vnode_associate = biba_devfs_vnode_associate,
3331	.mpo_vnode_associate_extattr = biba_vnode_associate_extattr,
3332	.mpo_vnode_associate_singlelabel = biba_vnode_associate_singlelabel,
3333	.mpo_vnode_create_extattr = biba_vnode_create_extattr,
3334	.mpo_vnode_setlabel_extattr = biba_vnode_setlabel_extattr,
3335	.mpo_socket_create_mbuf = biba_socket_create_mbuf,
3336	.mpo_syncache_create_mbuf = biba_syncache_create_mbuf,
3337	.mpo_pipe_create = biba_pipe_create,
3338	.mpo_posixsem_create = biba_posixsem_create,
3339	.mpo_socket_create = biba_socket_create,
3340	.mpo_socket_newconn = biba_socket_newconn,
3341	.mpo_pipe_relabel = biba_pipe_relabel,
3342	.mpo_socket_relabel = biba_socket_relabel,
3343	.mpo_socketpeer_set_from_mbuf = biba_socketpeer_set_from_mbuf,
3344	.mpo_socketpeer_set_from_socket = biba_socketpeer_set_from_socket,
3345	.mpo_bpfdesc_create = biba_bpfdesc_create,
3346	.mpo_ipq_reassemble = biba_ipq_reassemble,
3347	.mpo_netinet_fragment = biba_netinet_fragment,
3348	.mpo_ifnet_create = biba_ifnet_create,
3349	.mpo_inpcb_create = biba_inpcb_create,
3350	.mpo_sysvmsg_create = biba_sysvmsg_create,
3351	.mpo_sysvmsq_create = biba_sysvmsq_create,
3352	.mpo_sysvsem_create = biba_sysvsem_create,
3353	.mpo_sysvshm_create = biba_sysvshm_create,
3354	.mpo_ipq_create = biba_ipq_create,
3355	.mpo_inpcb_create_mbuf = biba_inpcb_create_mbuf,
3356	.mpo_bpfdesc_create_mbuf = biba_bpfdesc_create_mbuf,
3357	.mpo_ifnet_create_mbuf = biba_ifnet_create_mbuf,
3358	.mpo_mbuf_create_multicast_encap = biba_mbuf_create_multicast_encap,
3359	.mpo_mbuf_create_netlayer = biba_mbuf_create_netlayer,
3360	.mpo_ipq_match = biba_ipq_match,
3361	.mpo_ifnet_relabel = biba_ifnet_relabel,
3362	.mpo_ipq_update = biba_ipq_update,
3363	.mpo_inpcb_sosetlabel = biba_inpcb_sosetlabel,
3364	.mpo_proc_create_swapper = biba_proc_create_swapper,
3365	.mpo_proc_create_init = biba_proc_create_init,
3366	.mpo_proc_associate_nfsd = biba_proc_associate_nfsd,
3367	.mpo_cred_relabel = biba_cred_relabel,
3368	.mpo_sysvmsg_cleanup = biba_sysvmsg_cleanup,
3369	.mpo_sysvmsq_cleanup = biba_sysvmsq_cleanup,
3370	.mpo_sysvsem_cleanup = biba_sysvsem_cleanup,
3371	.mpo_sysvshm_cleanup = biba_sysvshm_cleanup,
3372	.mpo_bpfdesc_check_receive = biba_bpfdesc_check_receive,
3373	.mpo_cred_check_relabel = biba_cred_check_relabel,
3374	.mpo_cred_check_visible = biba_cred_check_visible,
3375	.mpo_ifnet_check_relabel = biba_ifnet_check_relabel,
3376	.mpo_ifnet_check_transmit = biba_ifnet_check_transmit,
3377	.mpo_inpcb_check_deliver = biba_inpcb_check_deliver,
3378	.mpo_sysvmsq_check_msgrcv = biba_sysvmsq_check_msgrcv,
3379	.mpo_sysvmsq_check_msgrmid = biba_sysvmsq_check_msgrmid,
3380	.mpo_sysvmsq_check_msqget = biba_sysvmsq_check_msqget,
3381	.mpo_sysvmsq_check_msqsnd = biba_sysvmsq_check_msqsnd,
3382	.mpo_sysvmsq_check_msqrcv = biba_sysvmsq_check_msqrcv,
3383	.mpo_sysvmsq_check_msqctl = biba_sysvmsq_check_msqctl,
3384	.mpo_sysvsem_check_semctl = biba_sysvsem_check_semctl,
3385	.mpo_sysvsem_check_semget = biba_sysvsem_check_semget,
3386	.mpo_sysvsem_check_semop = biba_sysvsem_check_semop,
3387	.mpo_sysvshm_check_shmat = biba_sysvshm_check_shmat,
3388	.mpo_sysvshm_check_shmctl = biba_sysvshm_check_shmctl,
3389	.mpo_sysvshm_check_shmget = biba_sysvshm_check_shmget,
3390	.mpo_kld_check_load = biba_kld_check_load,
3391	.mpo_mount_check_stat = biba_mount_check_stat,
3392	.mpo_pipe_check_ioctl = biba_pipe_check_ioctl,
3393	.mpo_pipe_check_poll = biba_pipe_check_poll,
3394	.mpo_pipe_check_read = biba_pipe_check_read,
3395	.mpo_pipe_check_relabel = biba_pipe_check_relabel,
3396	.mpo_pipe_check_stat = biba_pipe_check_stat,
3397	.mpo_pipe_check_write = biba_pipe_check_write,
3398	.mpo_posixsem_check_destroy = biba_posixsem_check_write,
3399	.mpo_posixsem_check_getvalue = biba_posixsem_check_rdonly,
3400	.mpo_posixsem_check_open = biba_posixsem_check_write,
3401	.mpo_posixsem_check_post = biba_posixsem_check_write,
3402	.mpo_posixsem_check_unlink = biba_posixsem_check_write,
3403	.mpo_posixsem_check_wait = biba_posixsem_check_write,
3404	.mpo_proc_check_debug = biba_proc_check_debug,
3405	.mpo_proc_check_sched = biba_proc_check_sched,
3406	.mpo_proc_check_signal = biba_proc_check_signal,
3407	.mpo_socket_check_deliver = biba_socket_check_deliver,
3408	.mpo_socket_check_relabel = biba_socket_check_relabel,
3409	.mpo_socket_check_visible = biba_socket_check_visible,
3410	.mpo_system_check_acct = biba_system_check_acct,
3411	.mpo_system_check_auditctl = biba_system_check_auditctl,
3412	.mpo_system_check_auditon = biba_system_check_auditon,
3413	.mpo_system_check_swapon = biba_system_check_swapon,
3414	.mpo_system_check_swapoff = biba_system_check_swapoff,
3415	.mpo_system_check_sysctl = biba_system_check_sysctl,
3416	.mpo_vnode_check_access = biba_vnode_check_open,
3417	.mpo_vnode_check_chdir = biba_vnode_check_chdir,
3418	.mpo_vnode_check_chroot = biba_vnode_check_chroot,
3419	.mpo_vnode_check_create = biba_vnode_check_create,
3420	.mpo_vnode_check_deleteacl = biba_vnode_check_deleteacl,
3421	.mpo_vnode_check_deleteextattr = biba_vnode_check_deleteextattr,
3422	.mpo_vnode_check_exec = biba_vnode_check_exec,
3423	.mpo_vnode_check_getacl = biba_vnode_check_getacl,
3424	.mpo_vnode_check_getextattr = biba_vnode_check_getextattr,
3425	.mpo_vnode_check_link = biba_vnode_check_link,
3426	.mpo_vnode_check_listextattr = biba_vnode_check_listextattr,
3427	.mpo_vnode_check_lookup = biba_vnode_check_lookup,
3428	.mpo_vnode_check_mmap = biba_vnode_check_mmap,
3429	.mpo_vnode_check_open = biba_vnode_check_open,
3430	.mpo_vnode_check_poll = biba_vnode_check_poll,
3431	.mpo_vnode_check_read = biba_vnode_check_read,
3432	.mpo_vnode_check_readdir = biba_vnode_check_readdir,
3433	.mpo_vnode_check_readlink = biba_vnode_check_readlink,
3434	.mpo_vnode_check_relabel = biba_vnode_check_relabel,
3435	.mpo_vnode_check_rename_from = biba_vnode_check_rename_from,
3436	.mpo_vnode_check_rename_to = biba_vnode_check_rename_to,
3437	.mpo_vnode_check_revoke = biba_vnode_check_revoke,
3438	.mpo_vnode_check_setacl = biba_vnode_check_setacl,
3439	.mpo_vnode_check_setextattr = biba_vnode_check_setextattr,
3440	.mpo_vnode_check_setflags = biba_vnode_check_setflags,
3441	.mpo_vnode_check_setmode = biba_vnode_check_setmode,
3442	.mpo_vnode_check_setowner = biba_vnode_check_setowner,
3443	.mpo_vnode_check_setutimes = biba_vnode_check_setutimes,
3444	.mpo_vnode_check_stat = biba_vnode_check_stat,
3445	.mpo_vnode_check_unlink = biba_vnode_check_unlink,
3446	.mpo_vnode_check_write = biba_vnode_check_write,
3447	.mpo_netatalk_aarp_send = biba_netatalk_aarp_send,
3448	.mpo_netinet_arp_send = biba_netinet_arp_send,
3449	.mpo_netinet_firewall_send = biba_netinet_firewall_send,
3450	.mpo_netinet_igmp_send = biba_netinet_igmp_send,
3451	.mpo_netinet6_nd6_send = biba_netinet6_nd6_send,
3452	.mpo_priv_check = biba_priv_check,
3453};
3454
3455MAC_POLICY_SET(&mac_biba_ops, mac_biba, "TrustedBSD MAC/Biba",
3456    MPC_LOADTIME_FLAG_NOTLATE | MPC_LOADTIME_FLAG_LABELMBUFS, &biba_slot);
3457