mac_biba.c revision 106160
1/*-
2 * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson
3 * Copyright (c) 2001, 2002 Networks Associates Technology, Inc.
4 * All rights reserved.
5 *
6 * This software was developed by Robert Watson for the TrustedBSD Project.
7 *
8 * This software was developed for the FreeBSD Project in part by NAI Labs,
9 * the Security Research Division of Network Associates, Inc. under
10 * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
11 * CHATS research program.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 * 3. The names of the authors may not be used to endorse or promote
22 *    products derived from this software without specific prior written
23 *    permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * $FreeBSD: head/sys/security/mac_biba/mac_biba.c 106160 2002-10-29 19:14:16Z rwatson $
38 */
39
40/*
41 * Developed by the TrustedBSD Project.
42 * Biba fixed label mandatory integrity policy.
43 */
44
45#include <sys/types.h>
46#include <sys/param.h>
47#include <sys/acl.h>
48#include <sys/conf.h>
49#include <sys/extattr.h>
50#include <sys/kernel.h>
51#include <sys/mac.h>
52#include <sys/malloc.h>
53#include <sys/mount.h>
54#include <sys/proc.h>
55#include <sys/systm.h>
56#include <sys/sysproto.h>
57#include <sys/sysent.h>
58#include <sys/systm.h>
59#include <sys/vnode.h>
60#include <sys/file.h>
61#include <sys/socket.h>
62#include <sys/socketvar.h>
63#include <sys/pipe.h>
64#include <sys/sysctl.h>
65
66#include <fs/devfs/devfs.h>
67
68#include <net/bpfdesc.h>
69#include <net/if.h>
70#include <net/if_types.h>
71#include <net/if_var.h>
72
73#include <netinet/in.h>
74#include <netinet/ip_var.h>
75
76#include <vm/vm.h>
77
78#include <sys/mac_policy.h>
79
80#include <security/mac_biba/mac_biba.h>
81
82SYSCTL_DECL(_security_mac);
83
84SYSCTL_NODE(_security_mac, OID_AUTO, biba, CTLFLAG_RW, 0,
85    "TrustedBSD mac_biba policy controls");
86
87static int	mac_biba_label_size = sizeof(struct mac_biba);
88SYSCTL_INT(_security_mac_biba, OID_AUTO, label_size, CTLFLAG_RD,
89    &mac_biba_label_size, 0, "Size of struct mac_biba");
90
91static int	mac_biba_enabled = 0;
92SYSCTL_INT(_security_mac_biba, OID_AUTO, enabled, CTLFLAG_RW,
93    &mac_biba_enabled, 0, "Enforce MAC/Biba policy");
94TUNABLE_INT("security.mac.biba.enabled", &mac_biba_enabled);
95
96static int	destroyed_not_inited;
97SYSCTL_INT(_security_mac_biba, OID_AUTO, destroyed_not_inited, CTLFLAG_RD,
98    &destroyed_not_inited, 0, "Count of labels destroyed but not inited");
99
100static int	trust_all_interfaces = 0;
101SYSCTL_INT(_security_mac_biba, OID_AUTO, trust_all_interfaces, CTLFLAG_RD,
102    &trust_all_interfaces, 0, "Consider all interfaces 'trusted' by MAC/Biba");
103TUNABLE_INT("security.mac.biba.trust_all_interfaces", &trust_all_interfaces);
104
105static char	trusted_interfaces[128];
106SYSCTL_STRING(_security_mac_biba, OID_AUTO, trusted_interfaces, CTLFLAG_RD,
107    trusted_interfaces, 0, "Interfaces considered 'trusted' by MAC/Biba");
108TUNABLE_STR("security.mac.biba.trusted_interfaces", trusted_interfaces,
109    sizeof(trusted_interfaces));
110
111static int	max_compartments = MAC_BIBA_MAX_COMPARTMENTS;
112SYSCTL_INT(_security_mac_biba, OID_AUTO, max_compartments, CTLFLAG_RD,
113    &max_compartments, 0, "Maximum supported compartments");
114
115static int	ptys_equal = 0;
116SYSCTL_INT(_security_mac_biba, OID_AUTO, ptys_equal, CTLFLAG_RW,
117    &ptys_equal, 0, "Label pty devices as biba/equal on create");
118TUNABLE_INT("security.mac.biba.ptys_equal", &ptys_equal);
119
120static int	revocation_enabled = 0;
121SYSCTL_INT(_security_mac_biba, OID_AUTO, revocation_enabled, CTLFLAG_RW,
122    &revocation_enabled, 0, "Revoke access to objects on relabel");
123TUNABLE_INT("security.mac.biba.revocation_enabled", &revocation_enabled);
124
125static int	mac_biba_slot;
126#define	SLOT(l)	((struct mac_biba *)LABEL_TO_SLOT((l), mac_biba_slot).l_ptr)
127
128MALLOC_DEFINE(M_MACBIBA, "biba label", "MAC/Biba labels");
129
130static __inline int
131biba_bit_set_empty(u_char *set) {
132	int i;
133
134	for (i = 0; i < MAC_BIBA_MAX_COMPARTMENTS >> 3; i++)
135		if (set[i] != 0)
136			return (0);
137	return (1);
138}
139
140static struct mac_biba *
141biba_alloc(int flag)
142{
143	struct mac_biba *mac_biba;
144
145	mac_biba = malloc(sizeof(struct mac_biba), M_MACBIBA, M_ZERO | flag);
146
147	return (mac_biba);
148}
149
150static void
151biba_free(struct mac_biba *mac_biba)
152{
153
154	if (mac_biba != NULL)
155		free(mac_biba, M_MACBIBA);
156	else
157		atomic_add_int(&destroyed_not_inited, 1);
158}
159
160static int
161biba_atmostflags(struct mac_biba *mac_biba, int flags)
162{
163
164	if ((mac_biba->mb_flags & flags) != mac_biba->mb_flags)
165		return (EINVAL);
166	return (0);
167}
168
169static int
170mac_biba_dominate_element(struct mac_biba_element *a,
171    struct mac_biba_element *b)
172{
173	int bit;
174
175	switch (a->mbe_type) {
176	case MAC_BIBA_TYPE_EQUAL:
177	case MAC_BIBA_TYPE_HIGH:
178		return (1);
179
180	case MAC_BIBA_TYPE_LOW:
181		switch (b->mbe_type) {
182		case MAC_BIBA_TYPE_GRADE:
183		case MAC_BIBA_TYPE_HIGH:
184			return (0);
185
186		case MAC_BIBA_TYPE_EQUAL:
187		case MAC_BIBA_TYPE_LOW:
188			return (1);
189
190		default:
191			panic("mac_biba_dominate_element: b->mbe_type invalid");
192		}
193
194	case MAC_BIBA_TYPE_GRADE:
195		switch (b->mbe_type) {
196		case MAC_BIBA_TYPE_EQUAL:
197		case MAC_BIBA_TYPE_LOW:
198			return (1);
199
200		case MAC_BIBA_TYPE_HIGH:
201			return (0);
202
203		case MAC_BIBA_TYPE_GRADE:
204			for (bit = 1; bit <= MAC_BIBA_MAX_COMPARTMENTS; bit++)
205				if (!MAC_BIBA_BIT_TEST(bit,
206				    a->mbe_compartments) &&
207				    MAC_BIBA_BIT_TEST(bit, b->mbe_compartments))
208					return (0);
209			return (a->mbe_grade >= b->mbe_grade);
210
211		default:
212			panic("mac_biba_dominate_element: b->mbe_type invalid");
213		}
214
215	default:
216		panic("mac_biba_dominate_element: a->mbe_type invalid");
217	}
218
219	return (0);
220}
221
222static int
223mac_biba_subject_dominate_high(struct mac_biba *mac_biba)
224{
225	struct mac_biba_element *element;
226
227	KASSERT((mac_biba->mb_single->mb_flags & MAC_BIBA_FLAG_SINGLE) != 0,
228	    ("mac_biba_single_in_range: mac_biba not single"));
229	element = &mac_biba->mb_single;
230
231	return (element->mbe_type == MAC_BIBA_TYPE_EQUAL ||
232	    element->mbe_type == MAC_BIBA_TYPE_HIGH);
233}
234
235static int
236mac_biba_range_in_range(struct mac_biba *rangea, struct mac_biba *rangeb)
237{
238
239	return (mac_biba_dominate_element(&rangeb->mb_rangehigh,
240	    &rangea->mb_rangehigh) &&
241	    mac_biba_dominate_element(&rangea->mb_rangelow,
242	    &rangeb->mb_rangelow));
243}
244
245static int
246mac_biba_single_in_range(struct mac_biba *single, struct mac_biba *range)
247{
248
249	KASSERT((single->mb_flags & MAC_BIBA_FLAG_SINGLE) != 0,
250	    ("mac_biba_single_in_range: a not single"));
251	KASSERT((range->mb_flags & MAC_BIBA_FLAG_RANGE) != 0,
252	    ("mac_biba_single_in_range: b not range"));
253
254	return (mac_biba_dominate_element(&range->mb_rangehigh,
255	    &single->mb_single) &&
256	    mac_biba_dominate_element(&single->mb_single,
257	    &range->mb_rangelow));
258
259	return (1);
260}
261
262static int
263mac_biba_dominate_single(struct mac_biba *a, struct mac_biba *b)
264{
265	KASSERT((a->mb_flags & MAC_BIBA_FLAG_SINGLE) != 0,
266	    ("mac_biba_dominate_single: a not single"));
267	KASSERT((b->mb_flags & MAC_BIBA_FLAG_SINGLE) != 0,
268	    ("mac_biba_dominate_single: b not single"));
269
270	return (mac_biba_dominate_element(&a->mb_single, &b->mb_single));
271}
272
273static int
274mac_biba_equal_element(struct mac_biba_element *a, struct mac_biba_element *b)
275{
276
277	if (a->mbe_type == MAC_BIBA_TYPE_EQUAL ||
278	    b->mbe_type == MAC_BIBA_TYPE_EQUAL)
279		return (1);
280
281	return (a->mbe_type == b->mbe_type && a->mbe_grade == b->mbe_grade);
282}
283
284static int
285mac_biba_equal_single(struct mac_biba *a, struct mac_biba *b)
286{
287
288	KASSERT((a->mb_flags & MAC_BIBA_FLAG_SINGLE) != 0,
289	    ("mac_biba_equal_single: a not single"));
290	KASSERT((b->mb_flags & MAC_BIBA_FLAG_SINGLE) != 0,
291	    ("mac_biba_equal_single: b not single"));
292
293	return (mac_biba_equal_element(&a->mb_single, &b->mb_single));
294}
295
296static int
297mac_biba_contains_equal(struct mac_biba *mac_biba)
298{
299
300	if (mac_biba->mb_flags & MAC_BIBA_FLAG_SINGLE)
301		if (mac_biba->mb_single.mbe_type == MAC_BIBA_TYPE_EQUAL)
302			return (1);
303
304	if (mac_biba->mb_flags & MAC_BIBA_FLAG_RANGE) {
305		if (mac_biba->mb_rangelow.mbe_type == MAC_BIBA_TYPE_EQUAL)
306			return (1);
307		if (mac_biba->mb_rangehigh.mbe_type == MAC_BIBA_TYPE_EQUAL)
308			return (1);
309	}
310
311	return (0);
312}
313
314static int
315mac_biba_subject_privileged(struct mac_biba *mac_biba)
316{
317
318	KASSERT((mac_biba->mb_flags & MAC_BIBA_FLAGS_BOTH) ==
319	    MAC_BIBA_FLAGS_BOTH,
320	    ("mac_biba_subject_privileged: subject doesn't have both labels"));
321
322	/* If the single is EQUAL, it's ok. */
323	if (mac_biba->mb_single.mbe_type == MAC_BIBA_TYPE_EQUAL)
324		return (0);
325
326	/* If either range endpoint is EQUAL, it's ok. */
327	if (mac_biba->mb_rangelow.mbe_type == MAC_BIBA_TYPE_EQUAL ||
328	    mac_biba->mb_rangehigh.mbe_type == MAC_BIBA_TYPE_EQUAL)
329		return (0);
330
331	/* If the range is low-high, it's ok. */
332	if (mac_biba->mb_rangelow.mbe_type == MAC_BIBA_TYPE_LOW &&
333	    mac_biba->mb_rangehigh.mbe_type == MAC_BIBA_TYPE_HIGH)
334		return (0);
335
336	/* It's not ok. */
337	return (EPERM);
338}
339
340static int
341mac_biba_high_single(struct mac_biba *mac_biba)
342{
343
344	KASSERT((mac_biba->mb_flags & MAC_BIBA_FLAG_SINGLE) != 0,
345	    ("mac_biba_equal_single: mac_biba not single"));
346
347	return (mac_biba->mb_single.mbe_type == MAC_BIBA_TYPE_HIGH);
348}
349
350static int
351mac_biba_valid(struct mac_biba *mac_biba)
352{
353
354	if (mac_biba->mb_flags & MAC_BIBA_FLAG_SINGLE) {
355		switch (mac_biba->mb_single.mbe_type) {
356		case MAC_BIBA_TYPE_GRADE:
357			break;
358
359		case MAC_BIBA_TYPE_EQUAL:
360		case MAC_BIBA_TYPE_HIGH:
361		case MAC_BIBA_TYPE_LOW:
362			if (mac_biba->mb_single.mbe_grade != 0 ||
363			    !MAC_BIBA_BIT_SET_EMPTY(
364			    mac_biba->mb_single.mbe_compartments))
365				return (EINVAL);
366			break;
367
368		default:
369			return (EINVAL);
370		}
371	} else {
372		if (mac_biba->mb_single.mbe_type != MAC_BIBA_TYPE_UNDEF)
373			return (EINVAL);
374	}
375
376	if (mac_biba->mb_flags & MAC_BIBA_FLAG_RANGE) {
377		switch (mac_biba->mb_rangelow.mbe_type) {
378		case MAC_BIBA_TYPE_GRADE:
379			break;
380
381		case MAC_BIBA_TYPE_EQUAL:
382		case MAC_BIBA_TYPE_HIGH:
383		case MAC_BIBA_TYPE_LOW:
384			if (mac_biba->mb_rangelow.mbe_grade != 0 ||
385			    !MAC_BIBA_BIT_SET_EMPTY(
386			    mac_biba->mb_rangelow.mbe_compartments))
387				return (EINVAL);
388			break;
389
390		default:
391			return (EINVAL);
392		}
393
394		switch (mac_biba->mb_rangehigh.mbe_type) {
395		case MAC_BIBA_TYPE_GRADE:
396			break;
397
398		case MAC_BIBA_TYPE_EQUAL:
399		case MAC_BIBA_TYPE_HIGH:
400		case MAC_BIBA_TYPE_LOW:
401			if (mac_biba->mb_rangehigh.mbe_grade != 0 ||
402			    !MAC_BIBA_BIT_SET_EMPTY(
403			    mac_biba->mb_rangehigh.mbe_compartments))
404				return (EINVAL);
405			break;
406
407		default:
408			return (EINVAL);
409		}
410		if (!mac_biba_dominate_element(&mac_biba->mb_rangehigh,
411		    &mac_biba->mb_rangelow))
412			return (EINVAL);
413	} else {
414		if (mac_biba->mb_rangelow.mbe_type != MAC_BIBA_TYPE_UNDEF ||
415		    mac_biba->mb_rangehigh.mbe_type != MAC_BIBA_TYPE_UNDEF)
416			return (EINVAL);
417	}
418
419	return (0);
420}
421
422static void
423mac_biba_set_range(struct mac_biba *mac_biba, u_short typelow,
424    u_short gradelow, u_char *compartmentslow, u_short typehigh,
425    u_short gradehigh, u_char *compartmentshigh)
426{
427
428	mac_biba->mb_rangelow.mbe_type = typelow;
429	mac_biba->mb_rangelow.mbe_grade = gradelow;
430	if (compartmentslow != NULL)
431		memcpy(mac_biba->mb_rangelow.mbe_compartments,
432		    compartmentslow,
433		    sizeof(mac_biba->mb_rangelow.mbe_compartments));
434	mac_biba->mb_rangehigh.mbe_type = typehigh;
435	mac_biba->mb_rangehigh.mbe_grade = gradehigh;
436	if (compartmentshigh != NULL)
437		memcpy(mac_biba->mb_rangehigh.mbe_compartments,
438		    compartmentshigh,
439		    sizeof(mac_biba->mb_rangehigh.mbe_compartments));
440	mac_biba->mb_flags |= MAC_BIBA_FLAG_RANGE;
441}
442
443static void
444mac_biba_set_single(struct mac_biba *mac_biba, u_short type, u_short grade,
445    u_char *compartments)
446{
447
448	mac_biba->mb_single.mbe_type = type;
449	mac_biba->mb_single.mbe_grade = grade;
450	if (compartments != NULL)
451		memcpy(mac_biba->mb_single.mbe_compartments, compartments,
452		    sizeof(mac_biba->mb_single.mbe_compartments));
453	mac_biba->mb_flags |= MAC_BIBA_FLAG_SINGLE;
454}
455
456static void
457mac_biba_copy_range(struct mac_biba *labelfrom, struct mac_biba *labelto)
458{
459
460	KASSERT((labelfrom->mb_flags & MAC_BIBA_FLAG_RANGE) != 0,
461	    ("mac_biba_copy_range: labelfrom not range"));
462
463	labelto->mb_rangelow = labelfrom->mb_rangelow;
464	labelto->mb_rangehigh = labelfrom->mb_rangehigh;
465	labelto->mb_flags |= MAC_BIBA_FLAG_RANGE;
466}
467
468static void
469mac_biba_copy_single(struct mac_biba *labelfrom, struct mac_biba *labelto)
470{
471
472	KASSERT((labelfrom->mb_flags & MAC_BIBA_FLAG_SINGLE) != 0,
473	    ("mac_biba_copy_single: labelfrom not single"));
474
475	labelto->mb_single = labelfrom->mb_single;
476	labelto->mb_flags |= MAC_BIBA_FLAG_SINGLE;
477}
478
479static void
480mac_biba_copy(struct mac_biba *source, struct mac_biba *dest)
481{
482
483	if (source->mb_flags & MAC_BIBA_FLAG_SINGLE)
484		mac_biba_copy_single(source, dest);
485	if (source->mb_flags & MAC_BIBA_FLAG_RANGE)
486		mac_biba_copy_range(source, dest);
487}
488
489/*
490 * Policy module operations.
491 */
492static void
493mac_biba_destroy(struct mac_policy_conf *conf)
494{
495
496}
497
498static void
499mac_biba_init(struct mac_policy_conf *conf)
500{
501
502}
503
504/*
505 * Label operations.
506 */
507static void
508mac_biba_init_label(struct label *label)
509{
510
511	SLOT(label) = biba_alloc(M_WAITOK);
512}
513
514static int
515mac_biba_init_label_waitcheck(struct label *label, int flag)
516{
517
518	SLOT(label) = biba_alloc(flag);
519	if (SLOT(label) == NULL)
520		return (ENOMEM);
521
522	return (0);
523}
524
525static void
526mac_biba_destroy_label(struct label *label)
527{
528
529	biba_free(SLOT(label));
530	SLOT(label) = NULL;
531}
532
533/*
534 * mac_biba_element_to_string() is basically an snprintf wrapper with
535 * the same properties as snprintf().  It returns the length it would
536 * have added to the string in the event the string is too short.
537 */
538static size_t
539mac_biba_element_to_string(char *string, size_t size,
540    struct mac_biba_element *element)
541{
542	int pos, bit = 1;
543
544	switch (element->mbe_type) {
545	case MAC_BIBA_TYPE_HIGH:
546		return (snprintf(string, size, "high"));
547
548	case MAC_BIBA_TYPE_LOW:
549		return (snprintf(string, size, "low"));
550
551	case MAC_BIBA_TYPE_EQUAL:
552		return (snprintf(string, size, "equal"));
553
554	case MAC_BIBA_TYPE_GRADE:
555		pos = snprintf(string, size, "%d:", element->mbe_grade);
556		for (bit = 1; bit <= MAC_BIBA_MAX_COMPARTMENTS; bit++) {
557			if (MAC_BIBA_BIT_TEST(bit, element->mbe_compartments))
558				pos += snprintf(string + pos, size - pos,
559				    "%d+", bit);
560		}
561		if (string[pos - 1] == '+' || string[pos - 1] == ':')
562			string[--pos] = NULL;
563		return (pos);
564
565	default:
566		panic("mac_biba_element_to_string: invalid type (%d)",
567		    element->mbe_type);
568	}
569}
570
571static int
572mac_biba_to_string(char *string, size_t size, size_t *caller_len,
573    struct mac_biba *mac_biba)
574{
575	size_t left, len;
576	char *curptr;
577
578	bzero(string, size);
579	curptr = string;
580	left = size;
581
582	if (mac_biba->mb_flags & MAC_BIBA_FLAG_SINGLE) {
583		len = mac_biba_element_to_string(curptr, left,
584		    &mac_biba->mb_single);
585		if (len >= left)
586			return (EINVAL);
587		left -= len;
588		curptr += len;
589	}
590
591	if (mac_biba->mb_flags & MAC_BIBA_FLAG_RANGE) {
592		len = snprintf(curptr, left, "(");
593		if (len >= left)
594			return (EINVAL);
595		left -= len;
596		curptr += len;
597
598		len = mac_biba_element_to_string(curptr, left,
599		    &mac_biba->mb_rangelow);
600		if (len >= left)
601			return (EINVAL);
602		left -= len;
603		curptr += len;
604
605		len = snprintf(curptr, left, "-");
606		if (len >= left)
607			return (EINVAL);
608		left -= len;
609		curptr += len;
610
611		len = mac_biba_element_to_string(curptr, left,
612		    &mac_biba->mb_rangehigh);
613		if (len >= left)
614			return (EINVAL);
615		left -= len;
616		curptr += len;
617
618		len = snprintf(curptr, left, ")");
619		if (len >= left)
620			return (EINVAL);
621		left -= len;
622		curptr += len;
623	}
624
625	*caller_len = strlen(string);
626	return (0);
627}
628
629static int
630mac_biba_externalize_label(struct label *label, char *element_name,
631    char *element_data, size_t size, size_t *len, int *claimed)
632{
633	struct mac_biba *mac_biba;
634	int error;
635
636	if (strcmp(MAC_BIBA_LABEL_NAME, element_name) != 0)
637		return (0);
638
639	(*claimed)++;
640
641	mac_biba = SLOT(label);
642	error = mac_biba_to_string(element_data, size, len, mac_biba);
643	if (error)
644		return (error);
645
646	*len = strlen(element_data);
647	return (0);
648}
649
650static int
651mac_biba_parse_element(struct mac_biba_element *element, char *string)
652{
653
654	if (strcmp(string, "high") == 0 ||
655	    strcmp(string, "hi") == 0) {
656		element->mbe_type = MAC_BIBA_TYPE_HIGH;
657		element->mbe_grade = MAC_BIBA_TYPE_UNDEF;
658	} else if (strcmp(string, "low") == 0 ||
659	    strcmp(string, "lo") == 0) {
660		element->mbe_type = MAC_BIBA_TYPE_LOW;
661		element->mbe_grade = MAC_BIBA_TYPE_UNDEF;
662	} else if (strcmp(string, "equal") == 0 ||
663	    strcmp(string, "eq") == 0) {
664		element->mbe_type = MAC_BIBA_TYPE_EQUAL;
665		element->mbe_grade = MAC_BIBA_TYPE_UNDEF;
666	} else {
667		char *p0, *p1;
668		int d;
669
670		p0 = string;
671		d = strtol(p0, &p1, 10);
672
673		if (d < 0 || d > 65535)
674			return (EINVAL);
675		element->mbe_type = MAC_BIBA_TYPE_GRADE;
676		element->mbe_grade = d;
677
678		if (*p1 != ':')  {
679			if (p1 == p0 || *p1 != '\0')
680				return (EINVAL);
681			else
682				return (0);
683		}
684		else
685			if (*(p1 + 1) == '\0')
686				return (0);
687
688		while ((p0 = ++p1)) {
689			d = strtol(p0, &p1, 10);
690			if (d < 1 || d > MAC_BIBA_MAX_COMPARTMENTS)
691				return (EINVAL);
692
693			MAC_BIBA_BIT_SET(d, element->mbe_compartments);
694
695			if (*p1 == '\0')
696				break;
697			if (p1 == p0 || *p1 != '+')
698				return (EINVAL);
699		}
700	}
701
702	return (0);
703}
704
705/*
706 * Note: destructively consumes the string, make a local copy before
707 * calling if that's a problem.
708 */
709static int
710mac_biba_parse(struct mac_biba *mac_biba, char *string)
711{
712	char *range, *rangeend, *rangehigh, *rangelow, *single;
713	int error;
714
715	/* Do we have a range? */
716	single = string;
717	range = index(string, '(');
718	if (range == single)
719		single = NULL;
720	rangelow = rangehigh = NULL;
721	if (range != NULL) {
722		/* Nul terminate the end of the single string. */
723		*range = '\0';
724		range++;
725		rangelow = range;
726		rangehigh = index(rangelow, '-');
727		if (rangehigh == NULL)
728			return (EINVAL);
729		rangehigh++;
730		if (*rangelow == '\0' || *rangehigh == '\0')
731			return (EINVAL);
732		rangeend = index(rangehigh, ')');
733		if (rangeend == NULL)
734			return (EINVAL);
735		if (*(rangeend + 1) != '\0')
736			return (EINVAL);
737		/* Nul terminate the ends of the ranges. */
738		*(rangehigh - 1) = '\0';
739		*rangeend = '\0';
740	}
741	KASSERT((rangelow != NULL && rangehigh != NULL) ||
742	    (rangelow == NULL && rangehigh == NULL),
743	    ("mac_biba_internalize_label: range mismatch"));
744
745	bzero(mac_biba, sizeof(*mac_biba));
746	if (single != NULL) {
747		error = mac_biba_parse_element(&mac_biba->mb_single, single);
748		if (error)
749			return (error);
750		mac_biba->mb_flags |= MAC_BIBA_FLAG_SINGLE;
751	}
752
753	if (rangelow != NULL) {
754		error = mac_biba_parse_element(&mac_biba->mb_rangelow,
755		    rangelow);
756		if (error)
757			return (error);
758		error = mac_biba_parse_element(&mac_biba->mb_rangehigh,
759		    rangehigh);
760		if (error)
761			return (error);
762		mac_biba->mb_flags |= MAC_BIBA_FLAG_RANGE;
763	}
764
765	error = mac_biba_valid(mac_biba);
766	if (error)
767		return (error);
768
769	return (0);
770}
771
772static int
773mac_biba_internalize_label(struct label *label, char *element_name,
774    char *element_data, int *claimed)
775{
776	struct mac_biba *mac_biba, mac_biba_temp;
777	int error;
778
779	if (strcmp(MAC_BIBA_LABEL_NAME, element_name) != 0)
780		return (0);
781
782	(*claimed)++;
783
784	error = mac_biba_parse(&mac_biba_temp, element_data);
785	if (error)
786		return (error);
787
788	mac_biba = SLOT(label);
789	*mac_biba = mac_biba_temp;
790
791	return (0);
792}
793
794static void
795mac_biba_copy_label(struct label *src, struct label *dest)
796{
797
798	*SLOT(dest) = *SLOT(src);
799}
800
801/*
802 * Labeling event operations: file system objects, and things that look
803 * a lot like file system objects.
804 */
805static void
806mac_biba_create_devfs_device(dev_t dev, struct devfs_dirent *devfs_dirent,
807    struct label *label)
808{
809	struct mac_biba *mac_biba;
810	int biba_type;
811
812	mac_biba = SLOT(label);
813	if (strcmp(dev->si_name, "null") == 0 ||
814	    strcmp(dev->si_name, "zero") == 0 ||
815	    strcmp(dev->si_name, "random") == 0 ||
816	    strncmp(dev->si_name, "fd/", strlen("fd/")) == 0)
817		biba_type = MAC_BIBA_TYPE_EQUAL;
818	else if (ptys_equal &&
819	    (strncmp(dev->si_name, "ttyp", strlen("ttyp")) == 0 ||
820	    strncmp(dev->si_name, "ptyp", strlen("ptyp")) == 0))
821		biba_type = MAC_BIBA_TYPE_EQUAL;
822	else
823		biba_type = MAC_BIBA_TYPE_HIGH;
824	mac_biba_set_single(mac_biba, biba_type, 0, NULL);
825}
826
827static void
828mac_biba_create_devfs_directory(char *dirname, int dirnamelen,
829    struct devfs_dirent *devfs_dirent, struct label *label)
830{
831	struct mac_biba *mac_biba;
832
833	mac_biba = SLOT(label);
834	mac_biba_set_single(mac_biba, MAC_BIBA_TYPE_HIGH, 0, NULL);
835}
836
837static void
838mac_biba_create_devfs_symlink(struct ucred *cred, struct devfs_dirent *dd,
839    struct label *ddlabel, struct devfs_dirent *de, struct label *delabel)
840{
841	struct mac_biba *source, *dest;
842
843	source = SLOT(&cred->cr_label);
844	dest = SLOT(delabel);
845
846	mac_biba_copy_single(source, dest);
847}
848
849static void
850mac_biba_create_devfs_vnode(struct devfs_dirent *devfs_dirent,
851    struct label *direntlabel, struct vnode *vp, struct label *vnodelabel)
852{
853	struct mac_biba *source, *dest;
854
855	source = SLOT(direntlabel);
856	dest = SLOT(vnodelabel);
857	mac_biba_copy_single(source, dest);
858}
859
860static void
861mac_biba_create_mount(struct ucred *cred, struct mount *mp,
862    struct label *mntlabel, struct label *fslabel)
863{
864	struct mac_biba *source, *dest;
865
866	source = SLOT(&cred->cr_label);
867	dest = SLOT(mntlabel);
868	mac_biba_copy_single(source, dest);
869	dest = SLOT(fslabel);
870	mac_biba_copy_single(source, dest);
871}
872
873static void
874mac_biba_create_root_mount(struct ucred *cred, struct mount *mp,
875    struct label *mntlabel, struct label *fslabel)
876{
877	struct mac_biba *mac_biba;
878
879	/* Always mount root as high integrity. */
880	mac_biba = SLOT(fslabel);
881	mac_biba_set_single(mac_biba, MAC_BIBA_TYPE_HIGH, 0, NULL);
882	mac_biba = SLOT(mntlabel);
883	mac_biba_set_single(mac_biba, MAC_BIBA_TYPE_HIGH, 0, NULL);
884}
885
886static void
887mac_biba_relabel_vnode(struct ucred *cred, struct vnode *vp,
888    struct label *vnodelabel, struct label *label)
889{
890	struct mac_biba *source, *dest;
891
892	source = SLOT(label);
893	dest = SLOT(vnodelabel);
894
895	mac_biba_copy(source, dest);
896}
897
898static void
899mac_biba_update_devfsdirent(struct devfs_dirent *devfs_dirent,
900    struct label *direntlabel, struct vnode *vp, struct label *vnodelabel)
901{
902	struct mac_biba *source, *dest;
903
904	source = SLOT(vnodelabel);
905	dest = SLOT(direntlabel);
906
907	mac_biba_copy(source, dest);
908}
909
910static void
911mac_biba_associate_vnode_devfs(struct mount *mp, struct label *fslabel,
912    struct devfs_dirent *de, struct label *delabel, struct vnode *vp,
913    struct label *vlabel)
914{
915	struct mac_biba *source, *dest;
916
917	source = SLOT(delabel);
918	dest = SLOT(vlabel);
919
920	mac_biba_copy_single(source, dest);
921}
922
923static int
924mac_biba_associate_vnode_extattr(struct mount *mp, struct label *fslabel,
925    struct vnode *vp, struct label *vlabel)
926{
927	struct mac_biba temp, *source, *dest;
928	size_t buflen;
929	int error;
930
931	source = SLOT(fslabel);
932	dest = SLOT(vlabel);
933
934	buflen = sizeof(temp);
935	bzero(&temp, buflen);
936
937	error = vn_extattr_get(vp, IO_NODELOCKED, MAC_BIBA_EXTATTR_NAMESPACE,
938	    MAC_BIBA_EXTATTR_NAME, &buflen, (char *) &temp, curthread);
939	if (error == ENOATTR || error == EOPNOTSUPP) {
940		/* Fall back to the fslabel. */
941		mac_biba_copy_single(source, dest);
942		return (0);
943	} else if (error)
944		return (error);
945
946	if (buflen != sizeof(temp)) {
947		printf("mac_biba_associate_vnode_extattr: bad size %d\n",
948		    buflen);
949		return (EPERM);
950	}
951	if (mac_biba_valid(&temp) != 0) {
952		printf("mac_biba_associate_vnode_extattr: invalid\n");
953		return (EPERM);
954	}
955	if ((temp.mb_flags & MAC_BIBA_FLAGS_BOTH) != MAC_BIBA_FLAG_SINGLE) {
956		printf("mac_biba_associate_vnode_extattr: not single\n");
957		return (EPERM);
958	}
959
960	mac_biba_copy_single(&temp, dest);
961	return (0);
962}
963
964static void
965mac_biba_associate_vnode_singlelabel(struct mount *mp,
966    struct label *fslabel, struct vnode *vp, struct label *vlabel)
967{
968	struct mac_biba *source, *dest;
969
970	source = SLOT(fslabel);
971	dest = SLOT(vlabel);
972
973	mac_biba_copy_single(source, dest);
974}
975
976static int
977mac_biba_create_vnode_extattr(struct ucred *cred, struct mount *mp,
978    struct label *fslabel, struct vnode *dvp, struct label *dlabel,
979    struct vnode *vp, struct label *vlabel, struct componentname *cnp)
980{
981	struct mac_biba *source, *dest, temp;
982	size_t buflen;
983	int error;
984
985	buflen = sizeof(temp);
986	bzero(&temp, buflen);
987
988	source = SLOT(&cred->cr_label);
989	dest = SLOT(vlabel);
990	mac_biba_copy_single(source, &temp);
991
992	error = vn_extattr_set(vp, IO_NODELOCKED, MAC_BIBA_EXTATTR_NAMESPACE,
993	    MAC_BIBA_EXTATTR_NAME, buflen, (char *) &temp, curthread);
994	if (error == 0)
995		mac_biba_copy_single(source, dest);
996	return (error);
997}
998
999static int
1000mac_biba_setlabel_vnode_extattr(struct ucred *cred, struct vnode *vp,
1001    struct label *vlabel, struct label *intlabel)
1002{
1003	struct mac_biba *source, temp;
1004	size_t buflen;
1005	int error;
1006
1007	buflen = sizeof(temp);
1008	bzero(&temp, buflen);
1009
1010	source = SLOT(intlabel);
1011	if ((source->mb_flags & MAC_BIBA_FLAG_SINGLE) == 0)
1012		return (0);
1013
1014	mac_biba_copy_single(source, &temp);
1015
1016	error = vn_extattr_set(vp, IO_NODELOCKED, MAC_BIBA_EXTATTR_NAMESPACE,
1017	    MAC_BIBA_EXTATTR_NAME, buflen, (char *) &temp, curthread);
1018	return (error);
1019}
1020
1021/*
1022 * Labeling event operations: IPC object.
1023 */
1024static void
1025mac_biba_create_mbuf_from_socket(struct socket *so, struct label *socketlabel,
1026    struct mbuf *m, struct label *mbuflabel)
1027{
1028	struct mac_biba *source, *dest;
1029
1030	source = SLOT(socketlabel);
1031	dest = SLOT(mbuflabel);
1032
1033	mac_biba_copy_single(source, dest);
1034}
1035
1036static void
1037mac_biba_create_socket(struct ucred *cred, struct socket *socket,
1038    struct label *socketlabel)
1039{
1040	struct mac_biba *source, *dest;
1041
1042	source = SLOT(&cred->cr_label);
1043	dest = SLOT(socketlabel);
1044
1045	mac_biba_copy_single(source, dest);
1046}
1047
1048static void
1049mac_biba_create_pipe(struct ucred *cred, struct pipe *pipe,
1050    struct label *pipelabel)
1051{
1052	struct mac_biba *source, *dest;
1053
1054	source = SLOT(&cred->cr_label);
1055	dest = SLOT(pipelabel);
1056
1057	mac_biba_copy_single(source, dest);
1058}
1059
1060static void
1061mac_biba_create_socket_from_socket(struct socket *oldsocket,
1062    struct label *oldsocketlabel, struct socket *newsocket,
1063    struct label *newsocketlabel)
1064{
1065	struct mac_biba *source, *dest;
1066
1067	source = SLOT(oldsocketlabel);
1068	dest = SLOT(newsocketlabel);
1069
1070	mac_biba_copy_single(source, dest);
1071}
1072
1073static void
1074mac_biba_relabel_socket(struct ucred *cred, struct socket *socket,
1075    struct label *socketlabel, struct label *newlabel)
1076{
1077	struct mac_biba *source, *dest;
1078
1079	source = SLOT(newlabel);
1080	dest = SLOT(socketlabel);
1081
1082	mac_biba_copy(source, dest);
1083}
1084
1085static void
1086mac_biba_relabel_pipe(struct ucred *cred, struct pipe *pipe,
1087    struct label *pipelabel, struct label *newlabel)
1088{
1089	struct mac_biba *source, *dest;
1090
1091	source = SLOT(newlabel);
1092	dest = SLOT(pipelabel);
1093
1094	mac_biba_copy(source, dest);
1095}
1096
1097static void
1098mac_biba_set_socket_peer_from_mbuf(struct mbuf *mbuf, struct label *mbuflabel,
1099    struct socket *socket, struct label *socketpeerlabel)
1100{
1101	struct mac_biba *source, *dest;
1102
1103	source = SLOT(mbuflabel);
1104	dest = SLOT(socketpeerlabel);
1105
1106	mac_biba_copy_single(source, dest);
1107}
1108
1109/*
1110 * Labeling event operations: network objects.
1111 */
1112static void
1113mac_biba_set_socket_peer_from_socket(struct socket *oldsocket,
1114    struct label *oldsocketlabel, struct socket *newsocket,
1115    struct label *newsocketpeerlabel)
1116{
1117	struct mac_biba *source, *dest;
1118
1119	source = SLOT(oldsocketlabel);
1120	dest = SLOT(newsocketpeerlabel);
1121
1122	mac_biba_copy_single(source, dest);
1123}
1124
1125static void
1126mac_biba_create_bpfdesc(struct ucred *cred, struct bpf_d *bpf_d,
1127    struct label *bpflabel)
1128{
1129	struct mac_biba *source, *dest;
1130
1131	source = SLOT(&cred->cr_label);
1132	dest = SLOT(bpflabel);
1133
1134	mac_biba_copy_single(source, dest);
1135}
1136
1137static void
1138mac_biba_create_ifnet(struct ifnet *ifnet, struct label *ifnetlabel)
1139{
1140	char tifname[IFNAMSIZ], ifname[IFNAMSIZ], *p, *q;
1141	char tiflist[sizeof(trusted_interfaces)];
1142	struct mac_biba *dest;
1143	int len, grade;
1144
1145	dest = SLOT(ifnetlabel);
1146
1147	if (ifnet->if_type == IFT_LOOP) {
1148		grade = MAC_BIBA_TYPE_EQUAL;
1149		goto set;
1150	}
1151
1152	if (trust_all_interfaces) {
1153		grade = MAC_BIBA_TYPE_HIGH;
1154		goto set;
1155	}
1156
1157	grade = MAC_BIBA_TYPE_LOW;
1158
1159	if (trusted_interfaces[0] == '\0' ||
1160	    !strvalid(trusted_interfaces, sizeof(trusted_interfaces)))
1161		goto set;
1162
1163	bzero(tiflist, sizeof(tiflist));
1164	for (p = trusted_interfaces, q = tiflist; *p != '\0'; p++, q++)
1165		if(*p != ' ' && *p != '\t')
1166			*q = *p;
1167
1168	snprintf(ifname, IFNAMSIZ, "%s%d", ifnet->if_name, ifnet->if_unit);
1169
1170	for (p = q = tiflist;; p++) {
1171		if (*p == ',' || *p == '\0') {
1172			len = p - q;
1173			if (len < IFNAMSIZ) {
1174				bzero(tifname, sizeof(tifname));
1175				bcopy(q, tifname, len);
1176				if (strcmp(tifname, ifname) == 0) {
1177					grade = MAC_BIBA_TYPE_HIGH;
1178					break;
1179				}
1180			} else {
1181				*p = '\0';
1182				printf("mac_biba warning: interface name "
1183				    "\"%s\" is too long (must be < %d)\n",
1184				    q, IFNAMSIZ);
1185			}
1186			if (*p == '\0')
1187				break;
1188			q = p + 1;
1189		}
1190	}
1191set:
1192	mac_biba_set_single(dest, grade, 0, NULL);
1193	mac_biba_set_range(dest, grade, 0, NULL, grade, 0, NULL);
1194}
1195
1196static void
1197mac_biba_create_ipq(struct mbuf *fragment, struct label *fragmentlabel,
1198    struct ipq *ipq, struct label *ipqlabel)
1199{
1200	struct mac_biba *source, *dest;
1201
1202	source = SLOT(fragmentlabel);
1203	dest = SLOT(ipqlabel);
1204
1205	mac_biba_copy_single(source, dest);
1206}
1207
1208static void
1209mac_biba_create_datagram_from_ipq(struct ipq *ipq, struct label *ipqlabel,
1210    struct mbuf *datagram, struct label *datagramlabel)
1211{
1212	struct mac_biba *source, *dest;
1213
1214	source = SLOT(ipqlabel);
1215	dest = SLOT(datagramlabel);
1216
1217	/* Just use the head, since we require them all to match. */
1218	mac_biba_copy_single(source, dest);
1219}
1220
1221static void
1222mac_biba_create_fragment(struct mbuf *datagram, struct label *datagramlabel,
1223    struct mbuf *fragment, struct label *fragmentlabel)
1224{
1225	struct mac_biba *source, *dest;
1226
1227	source = SLOT(datagramlabel);
1228	dest = SLOT(fragmentlabel);
1229
1230	mac_biba_copy_single(source, dest);
1231}
1232
1233static void
1234mac_biba_create_mbuf_from_mbuf(struct mbuf *oldmbuf,
1235    struct label *oldmbuflabel, struct mbuf *newmbuf,
1236    struct label *newmbuflabel)
1237{
1238	struct mac_biba *source, *dest;
1239
1240	source = SLOT(oldmbuflabel);
1241	dest = SLOT(newmbuflabel);
1242
1243	/*
1244	 * Because the source mbuf may not yet have been "created",
1245	 * just initialized, we do a conditional copy.  Since we don't
1246	 * allow mbufs to have ranges, do a KASSERT to make sure that
1247	 * doesn't happen.
1248	 */
1249	KASSERT((source->mb_flags & MAC_BIBA_FLAG_RANGE) == 0,
1250	    ("mac_biba_create_mbuf_from_mbuf: source mbuf has range"));
1251	mac_biba_copy(source, dest);
1252}
1253
1254static void
1255mac_biba_create_mbuf_linklayer(struct ifnet *ifnet, struct label *ifnetlabel,
1256    struct mbuf *mbuf, struct label *mbuflabel)
1257{
1258	struct mac_biba *dest;
1259
1260	dest = SLOT(mbuflabel);
1261
1262	mac_biba_set_single(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
1263}
1264
1265static void
1266mac_biba_create_mbuf_from_bpfdesc(struct bpf_d *bpf_d, struct label *bpflabel,
1267    struct mbuf *mbuf, struct label *mbuflabel)
1268{
1269	struct mac_biba *source, *dest;
1270
1271	source = SLOT(bpflabel);
1272	dest = SLOT(mbuflabel);
1273
1274	mac_biba_copy_single(source, dest);
1275}
1276
1277static void
1278mac_biba_create_mbuf_from_ifnet(struct ifnet *ifnet, struct label *ifnetlabel,
1279    struct mbuf *m, struct label *mbuflabel)
1280{
1281	struct mac_biba *source, *dest;
1282
1283	source = SLOT(ifnetlabel);
1284	dest = SLOT(mbuflabel);
1285
1286	mac_biba_copy_single(source, dest);
1287}
1288
1289static void
1290mac_biba_create_mbuf_multicast_encap(struct mbuf *oldmbuf,
1291    struct label *oldmbuflabel, struct ifnet *ifnet, struct label *ifnetlabel,
1292    struct mbuf *newmbuf, struct label *newmbuflabel)
1293{
1294	struct mac_biba *source, *dest;
1295
1296	source = SLOT(oldmbuflabel);
1297	dest = SLOT(newmbuflabel);
1298
1299	mac_biba_copy_single(source, dest);
1300}
1301
1302static void
1303mac_biba_create_mbuf_netlayer(struct mbuf *oldmbuf, struct label *oldmbuflabel,
1304    struct mbuf *newmbuf, struct label *newmbuflabel)
1305{
1306	struct mac_biba *source, *dest;
1307
1308	source = SLOT(oldmbuflabel);
1309	dest = SLOT(newmbuflabel);
1310
1311	mac_biba_copy_single(source, dest);
1312}
1313
1314static int
1315mac_biba_fragment_match(struct mbuf *fragment, struct label *fragmentlabel,
1316    struct ipq *ipq, struct label *ipqlabel)
1317{
1318	struct mac_biba *a, *b;
1319
1320	a = SLOT(ipqlabel);
1321	b = SLOT(fragmentlabel);
1322
1323	return (mac_biba_equal_single(a, b));
1324}
1325
1326static void
1327mac_biba_relabel_ifnet(struct ucred *cred, struct ifnet *ifnet,
1328    struct label *ifnetlabel, struct label *newlabel)
1329{
1330	struct mac_biba *source, *dest;
1331
1332	source = SLOT(newlabel);
1333	dest = SLOT(ifnetlabel);
1334
1335	mac_biba_copy(source, dest);
1336}
1337
1338static void
1339mac_biba_update_ipq(struct mbuf *fragment, struct label *fragmentlabel,
1340    struct ipq *ipq, struct label *ipqlabel)
1341{
1342
1343	/* NOOP: we only accept matching labels, so no need to update */
1344}
1345
1346/*
1347 * Labeling event operations: processes.
1348 */
1349static void
1350mac_biba_create_cred(struct ucred *cred_parent, struct ucred *cred_child)
1351{
1352	struct mac_biba *source, *dest;
1353
1354	source = SLOT(&cred_parent->cr_label);
1355	dest = SLOT(&cred_child->cr_label);
1356
1357	mac_biba_copy_single(source, dest);
1358	mac_biba_copy_range(source, dest);
1359}
1360
1361static void
1362mac_biba_execve_transition(struct ucred *old, struct ucred *new,
1363    struct vnode *vp, struct mac *vnodelabel)
1364{
1365	struct mac_biba *source, *dest;
1366
1367	source = SLOT(&old->cr_label);
1368	dest = SLOT(&new->cr_label);
1369
1370	mac_biba_copy_single(source, dest);
1371	mac_biba_copy_range(source, dest);
1372}
1373
1374static int
1375mac_biba_execve_will_transition(struct ucred *old, struct vnode *vp,
1376    struct mac *vnodelabel)
1377{
1378
1379	return (0);
1380}
1381
1382static void
1383mac_biba_create_proc0(struct ucred *cred)
1384{
1385	struct mac_biba *dest;
1386
1387	dest = SLOT(&cred->cr_label);
1388
1389	mac_biba_set_single(dest, MAC_BIBA_TYPE_EQUAL, 0, NULL);
1390	mac_biba_set_range(dest, MAC_BIBA_TYPE_LOW, 0, NULL,
1391	    MAC_BIBA_TYPE_HIGH, 0, NULL);
1392}
1393
1394static void
1395mac_biba_create_proc1(struct ucred *cred)
1396{
1397	struct mac_biba *dest;
1398
1399	dest = SLOT(&cred->cr_label);
1400
1401	mac_biba_set_single(dest, MAC_BIBA_TYPE_HIGH, 0, NULL);
1402	mac_biba_set_range(dest, MAC_BIBA_TYPE_LOW, 0, NULL,
1403	    MAC_BIBA_TYPE_HIGH, 0, NULL);
1404}
1405
1406static void
1407mac_biba_relabel_cred(struct ucred *cred, struct label *newlabel)
1408{
1409	struct mac_biba *source, *dest;
1410
1411	source = SLOT(newlabel);
1412	dest = SLOT(&cred->cr_label);
1413
1414	mac_biba_copy(source, dest);
1415}
1416
1417/*
1418 * Access control checks.
1419 */
1420static int
1421mac_biba_check_bpfdesc_receive(struct bpf_d *bpf_d, struct label *bpflabel,
1422    struct ifnet *ifnet, struct label *ifnetlabel)
1423{
1424	struct mac_biba *a, *b;
1425
1426	if (!mac_biba_enabled)
1427		return (0);
1428
1429	a = SLOT(bpflabel);
1430	b = SLOT(ifnetlabel);
1431
1432	if (mac_biba_equal_single(a, b))
1433		return (0);
1434	return (EACCES);
1435}
1436
1437static int
1438mac_biba_check_cred_relabel(struct ucred *cred, struct label *newlabel)
1439{
1440	struct mac_biba *subj, *new;
1441	int error;
1442
1443	subj = SLOT(&cred->cr_label);
1444	new = SLOT(newlabel);
1445
1446	/*
1447	 * If there is a Biba label update for the credential, it may
1448	 * be an update of the single, range, or both.
1449	 */
1450	error = biba_atmostflags(new, MAC_BIBA_FLAGS_BOTH);
1451	if (error)
1452		return (error);
1453
1454	/*
1455	 * If the Biba label is to be changed, authorize as appropriate.
1456	 */
1457	if (new->mb_flags & MAC_BIBA_FLAGS_BOTH) {
1458		/*
1459		 * To change the Biba single label on a credential, the
1460		 * new single label must be in the current range.
1461		 */
1462		if (new->mb_flags & MAC_BIBA_FLAG_SINGLE &&
1463		    !mac_biba_single_in_range(new, subj))
1464			return (EPERM);
1465
1466		/*
1467		 * To change the Biba range on a credential, the new
1468		 * range label must be in the current range.
1469		 */
1470		if (new->mb_flags & MAC_BIBA_FLAG_RANGE &&
1471		    !mac_biba_range_in_range(new, subj))
1472			return (EPERM);
1473
1474		/*
1475		 * To have EQUAL in any component of the new credential
1476		 * Biba label, the subject must already have EQUAL in
1477		 * their label.
1478		 */
1479		if (mac_biba_contains_equal(new)) {
1480			error = mac_biba_subject_privileged(subj);
1481			if (error)
1482				return (error);
1483		}
1484
1485		/*
1486		 * XXXMAC: Additional consistency tests regarding the
1487		 * single and range of the new label might be performed
1488		 * here.
1489		 */
1490	}
1491
1492	return (0);
1493}
1494
1495static int
1496mac_biba_check_cred_visible(struct ucred *u1, struct ucred *u2)
1497{
1498	struct mac_biba *subj, *obj;
1499
1500	if (!mac_biba_enabled)
1501		return (0);
1502
1503	subj = SLOT(&u1->cr_label);
1504	obj = SLOT(&u2->cr_label);
1505
1506	/* XXX: range */
1507	if (!mac_biba_dominate_single(obj, subj))
1508		return (ESRCH);
1509
1510	return (0);
1511}
1512
1513static int
1514mac_biba_check_ifnet_relabel(struct ucred *cred, struct ifnet *ifnet,
1515    struct label *ifnetlabel, struct label *newlabel)
1516{
1517	struct mac_biba *subj, *new;
1518	int error;
1519
1520	subj = SLOT(&cred->cr_label);
1521	new = SLOT(newlabel);
1522
1523	/*
1524	 * If there is a Biba label update for the interface, it may
1525	 * be an update of the single, range, or both.
1526	 */
1527	error = biba_atmostflags(new, MAC_BIBA_FLAGS_BOTH);
1528	if (error)
1529		return (error);
1530
1531	/*
1532	 * Relabling network interfaces requires Biba privilege.
1533	 */
1534	error = mac_biba_subject_privileged(subj);
1535	if (error)
1536		return (error);
1537
1538	/*
1539	 * If the Biba label is to be changed, authorize as appropriate.
1540	 */
1541	if (new->mb_flags & MAC_BIBA_FLAGS_BOTH) {
1542		/*
1543		 * Rely on the traditional superuser status for the Biba
1544		 * interface relabel requirements.  XXXMAC: This will go
1545		 * away.
1546		 */
1547		error = suser_cred(cred, 0);
1548		if (error)
1549			return (EPERM);
1550
1551		/*
1552		 * XXXMAC: Additional consistency tests regarding the single
1553		 * and the range of the new label might be performed here.
1554		 */
1555	}
1556
1557	return (0);
1558}
1559
1560static int
1561mac_biba_check_ifnet_transmit(struct ifnet *ifnet, struct label *ifnetlabel,
1562    struct mbuf *m, struct label *mbuflabel)
1563{
1564	struct mac_biba *p, *i;
1565
1566	if (!mac_biba_enabled)
1567		return (0);
1568
1569	p = SLOT(mbuflabel);
1570	i = SLOT(ifnetlabel);
1571
1572	return (mac_biba_single_in_range(p, i) ? 0 : EACCES);
1573}
1574
1575static int
1576mac_biba_check_mount_stat(struct ucred *cred, struct mount *mp,
1577    struct label *mntlabel)
1578{
1579	struct mac_biba *subj, *obj;
1580
1581	if (!mac_biba_enabled)
1582		return (0);
1583
1584	subj = SLOT(&cred->cr_label);
1585	obj = SLOT(mntlabel);
1586
1587	if (!mac_biba_dominate_single(obj, subj))
1588		return (EACCES);
1589
1590	return (0);
1591}
1592
1593static int
1594mac_biba_check_pipe_ioctl(struct ucred *cred, struct pipe *pipe,
1595    struct label *pipelabel, unsigned long cmd, void /* caddr_t */ *data)
1596{
1597
1598	if(!mac_biba_enabled)
1599		return (0);
1600
1601	/* XXX: This will be implemented soon... */
1602
1603	return (0);
1604}
1605
1606static int
1607mac_biba_check_pipe_poll(struct ucred *cred, struct pipe *pipe,
1608    struct label *pipelabel)
1609{
1610	struct mac_biba *subj, *obj;
1611
1612	if (!mac_biba_enabled)
1613		return (0);
1614
1615	subj = SLOT(&cred->cr_label);
1616	obj = SLOT((pipelabel));
1617
1618	if (!mac_biba_dominate_single(obj, subj))
1619		return (EACCES);
1620
1621	return (0);
1622}
1623
1624static int
1625mac_biba_check_pipe_read(struct ucred *cred, struct pipe *pipe,
1626    struct label *pipelabel)
1627{
1628	struct mac_biba *subj, *obj;
1629
1630	if (!mac_biba_enabled)
1631		return (0);
1632
1633	subj = SLOT(&cred->cr_label);
1634	obj = SLOT((pipelabel));
1635
1636	if (!mac_biba_dominate_single(obj, subj))
1637		return (EACCES);
1638
1639	return (0);
1640}
1641
1642static int
1643mac_biba_check_pipe_relabel(struct ucred *cred, struct pipe *pipe,
1644    struct label *pipelabel, struct label *newlabel)
1645{
1646	struct mac_biba *subj, *obj, *new;
1647	int error;
1648
1649	new = SLOT(newlabel);
1650	subj = SLOT(&cred->cr_label);
1651	obj = SLOT(pipelabel);
1652
1653	/*
1654	 * If there is a Biba label update for a pipe, it must be a
1655	 * single update.
1656	 */
1657	error = biba_atmostflags(new, MAC_BIBA_FLAG_SINGLE);
1658	if (error)
1659		return (error);
1660
1661	/*
1662	 * To perform a relabel of a pipe (Biba label or not), Biba must
1663	 * authorize the relabel.
1664	 */
1665	if (!mac_biba_single_in_range(obj, subj))
1666		return (EPERM);
1667
1668	/*
1669	 * If the Biba label is to be changed, authorize as appropriate.
1670	 */
1671	if (new->mb_flags & MAC_BIBA_FLAG_SINGLE) {
1672		/*
1673		 * To change the Biba label on a pipe, the new pipe label
1674		 * must be in the subject range.
1675		 */
1676		if (!mac_biba_single_in_range(new, subj))
1677			return (EPERM);
1678
1679		/*
1680		 * To change the Biba label on a pipe to be EQUAL, the
1681		 * subject must have appropriate privilege.
1682		 */
1683		if (mac_biba_contains_equal(new)) {
1684			error = mac_biba_subject_privileged(subj);
1685			if (error)
1686				return (error);
1687		}
1688	}
1689
1690	return (0);
1691}
1692
1693static int
1694mac_biba_check_pipe_stat(struct ucred *cred, struct pipe *pipe,
1695    struct label *pipelabel)
1696{
1697	struct mac_biba *subj, *obj;
1698
1699	if (!mac_biba_enabled)
1700		return (0);
1701
1702	subj = SLOT(&cred->cr_label);
1703	obj = SLOT((pipelabel));
1704
1705	if (!mac_biba_dominate_single(obj, subj))
1706		return (EACCES);
1707
1708	return (0);
1709}
1710
1711static int
1712mac_biba_check_pipe_write(struct ucred *cred, struct pipe *pipe,
1713    struct label *pipelabel)
1714{
1715	struct mac_biba *subj, *obj;
1716
1717	if (!mac_biba_enabled)
1718		return (0);
1719
1720	subj = SLOT(&cred->cr_label);
1721	obj = SLOT((pipelabel));
1722
1723	if (!mac_biba_dominate_single(subj, obj))
1724		return (EACCES);
1725
1726	return (0);
1727}
1728
1729static int
1730mac_biba_check_proc_debug(struct ucred *cred, struct proc *proc)
1731{
1732	struct mac_biba *subj, *obj;
1733
1734	if (!mac_biba_enabled)
1735		return (0);
1736
1737	subj = SLOT(&cred->cr_label);
1738	obj = SLOT(&proc->p_ucred->cr_label);
1739
1740	/* XXX: range checks */
1741	if (!mac_biba_dominate_single(obj, subj))
1742		return (ESRCH);
1743	if (!mac_biba_dominate_single(subj, obj))
1744		return (EACCES);
1745
1746	return (0);
1747}
1748
1749static int
1750mac_biba_check_proc_sched(struct ucred *cred, struct proc *proc)
1751{
1752	struct mac_biba *subj, *obj;
1753
1754	if (!mac_biba_enabled)
1755		return (0);
1756
1757	subj = SLOT(&cred->cr_label);
1758	obj = SLOT(&proc->p_ucred->cr_label);
1759
1760	/* XXX: range checks */
1761	if (!mac_biba_dominate_single(obj, subj))
1762		return (ESRCH);
1763	if (!mac_biba_dominate_single(subj, obj))
1764		return (EACCES);
1765
1766	return (0);
1767}
1768
1769static int
1770mac_biba_check_proc_signal(struct ucred *cred, struct proc *proc, int signum)
1771{
1772	struct mac_biba *subj, *obj;
1773
1774	if (!mac_biba_enabled)
1775		return (0);
1776
1777	subj = SLOT(&cred->cr_label);
1778	obj = SLOT(&proc->p_ucred->cr_label);
1779
1780	/* XXX: range checks */
1781	if (!mac_biba_dominate_single(obj, subj))
1782		return (ESRCH);
1783	if (!mac_biba_dominate_single(subj, obj))
1784		return (EACCES);
1785
1786	return (0);
1787}
1788
1789static int
1790mac_biba_check_socket_deliver(struct socket *so, struct label *socketlabel,
1791    struct mbuf *m, struct label *mbuflabel)
1792{
1793	struct mac_biba *p, *s;
1794
1795	if (!mac_biba_enabled)
1796		return (0);
1797
1798	p = SLOT(mbuflabel);
1799	s = SLOT(socketlabel);
1800
1801	return (mac_biba_equal_single(p, s) ? 0 : EACCES);
1802}
1803
1804static int
1805mac_biba_check_socket_relabel(struct ucred *cred, struct socket *socket,
1806    struct label *socketlabel, struct label *newlabel)
1807{
1808	struct mac_biba *subj, *obj, *new;
1809	int error;
1810
1811	new = SLOT(newlabel);
1812	subj = SLOT(&cred->cr_label);
1813	obj = SLOT(socketlabel);
1814
1815	/*
1816	 * If there is a Biba label update for the socket, it may be
1817	 * an update of single.
1818	 */
1819	error = biba_atmostflags(new, MAC_BIBA_FLAG_SINGLE);
1820	if (error)
1821		return (error);
1822
1823	/*
1824	 * To relabel a socket, the old socket single must be in the subject
1825	 * range.
1826	 */
1827	if (!mac_biba_single_in_range(obj, subj))
1828		return (EPERM);
1829
1830	/*
1831	 * If the Biba label is to be changed, authorize as appropriate.
1832	 */
1833	if (new->mb_flags & MAC_BIBA_FLAG_SINGLE) {
1834		/*
1835		 * To relabel a socket, the new socket single must be in
1836		 * the subject range.
1837		 */
1838		if (!mac_biba_single_in_range(new, subj))
1839			return (EPERM);
1840
1841		/*
1842		 * To change the Biba label on the socket to contain EQUAL,
1843		 * the subject must have appropriate privilege.
1844		 */
1845		if (mac_biba_contains_equal(new)) {
1846			error = mac_biba_subject_privileged(subj);
1847			if (error)
1848				return (error);
1849		}
1850	}
1851
1852	return (0);
1853}
1854
1855static int
1856mac_biba_check_socket_visible(struct ucred *cred, struct socket *socket,
1857    struct label *socketlabel)
1858{
1859	struct mac_biba *subj, *obj;
1860
1861	if (!mac_biba_enabled)
1862		return (0);
1863
1864	subj = SLOT(&cred->cr_label);
1865	obj = SLOT(socketlabel);
1866
1867	if (!mac_biba_dominate_single(obj, subj))
1868		return (ENOENT);
1869
1870	return (0);
1871}
1872
1873static int
1874mac_biba_check_vnode_chdir(struct ucred *cred, struct vnode *dvp,
1875    struct label *dlabel)
1876{
1877	struct mac_biba *subj, *obj;
1878
1879	if (!mac_biba_enabled)
1880		return (0);
1881
1882	subj = SLOT(&cred->cr_label);
1883	obj = SLOT(dlabel);
1884
1885	if (!mac_biba_dominate_single(obj, subj))
1886		return (EACCES);
1887
1888	return (0);
1889}
1890
1891static int
1892mac_biba_check_vnode_chroot(struct ucred *cred, struct vnode *dvp,
1893    struct label *dlabel)
1894{
1895	struct mac_biba *subj, *obj;
1896
1897	if (!mac_biba_enabled)
1898		return (0);
1899
1900	subj = SLOT(&cred->cr_label);
1901	obj = SLOT(dlabel);
1902
1903	if (!mac_biba_dominate_single(obj, subj))
1904		return (EACCES);
1905
1906	return (0);
1907}
1908
1909static int
1910mac_biba_check_vnode_create(struct ucred *cred, struct vnode *dvp,
1911    struct label *dlabel, struct componentname *cnp, struct vattr *vap)
1912{
1913	struct mac_biba *subj, *obj;
1914
1915	if (!mac_biba_enabled)
1916		return (0);
1917
1918	subj = SLOT(&cred->cr_label);
1919	obj = SLOT(dlabel);
1920
1921	if (!mac_biba_dominate_single(subj, obj))
1922		return (EACCES);
1923
1924	return (0);
1925}
1926
1927static int
1928mac_biba_check_vnode_delete(struct ucred *cred, struct vnode *dvp,
1929    struct label *dlabel, struct vnode *vp, struct label *label,
1930    struct componentname *cnp)
1931{
1932	struct mac_biba *subj, *obj;
1933
1934	if (!mac_biba_enabled)
1935		return (0);
1936
1937	subj = SLOT(&cred->cr_label);
1938	obj = SLOT(dlabel);
1939
1940	if (!mac_biba_dominate_single(subj, obj))
1941		return (EACCES);
1942
1943	obj = SLOT(label);
1944
1945	if (!mac_biba_dominate_single(subj, obj))
1946		return (EACCES);
1947
1948	return (0);
1949}
1950
1951static int
1952mac_biba_check_vnode_deleteacl(struct ucred *cred, struct vnode *vp,
1953    struct label *label, acl_type_t type)
1954{
1955	struct mac_biba *subj, *obj;
1956
1957	if (!mac_biba_enabled)
1958		return (0);
1959
1960	subj = SLOT(&cred->cr_label);
1961	obj = SLOT(label);
1962
1963	if (!mac_biba_dominate_single(subj, obj))
1964		return (EACCES);
1965
1966	return (0);
1967}
1968
1969static int
1970mac_biba_check_vnode_exec(struct ucred *cred, struct vnode *vp,
1971    struct label *label)
1972{
1973	struct mac_biba *subj, *obj;
1974
1975	if (!mac_biba_enabled)
1976		return (0);
1977
1978	subj = SLOT(&cred->cr_label);
1979	obj = SLOT(label);
1980
1981	if (!mac_biba_dominate_single(obj, subj))
1982		return (EACCES);
1983
1984	return (0);
1985}
1986
1987static int
1988mac_biba_check_vnode_getacl(struct ucred *cred, struct vnode *vp,
1989    struct label *label, acl_type_t type)
1990{
1991	struct mac_biba *subj, *obj;
1992
1993	if (!mac_biba_enabled)
1994		return (0);
1995
1996	subj = SLOT(&cred->cr_label);
1997	obj = SLOT(label);
1998
1999	if (!mac_biba_dominate_single(obj, subj))
2000		return (EACCES);
2001
2002	return (0);
2003}
2004
2005static int
2006mac_biba_check_vnode_getextattr(struct ucred *cred, struct vnode *vp,
2007    struct label *label, int attrnamespace, const char *name, struct uio *uio)
2008{
2009	struct mac_biba *subj, *obj;
2010
2011	if (!mac_biba_enabled)
2012		return (0);
2013
2014	subj = SLOT(&cred->cr_label);
2015	obj = SLOT(label);
2016
2017	if (!mac_biba_dominate_single(obj, subj))
2018		return (EACCES);
2019
2020	return (0);
2021}
2022
2023static int
2024mac_biba_check_vnode_link(struct ucred *cred, struct vnode *dvp,
2025    struct label *dlabel, struct vnode *vp, struct label *label,
2026    struct componentname *cnp)
2027{
2028	struct mac_biba *subj, *obj;
2029
2030	if (!mac_biba_enabled)
2031		return (0);
2032
2033	subj = SLOT(&cred->cr_label);
2034	obj = SLOT(dlabel);
2035
2036	if (!mac_biba_dominate_single(subj, obj))
2037		return (EACCES);
2038
2039	obj = SLOT(label);
2040
2041	if (!mac_biba_dominate_single(subj, obj))
2042		return (EACCES);
2043
2044	return (0);
2045}
2046
2047static int
2048mac_biba_check_vnode_lookup(struct ucred *cred, struct vnode *dvp,
2049    struct label *dlabel, struct componentname *cnp)
2050{
2051	struct mac_biba *subj, *obj;
2052
2053	if (!mac_biba_enabled)
2054		return (0);
2055
2056	subj = SLOT(&cred->cr_label);
2057	obj = SLOT(dlabel);
2058
2059	if (!mac_biba_dominate_single(obj, subj))
2060		return (EACCES);
2061
2062	return (0);
2063}
2064
2065static int
2066mac_biba_check_vnode_mmap(struct ucred *cred, struct vnode *vp,
2067    struct label *label, int prot)
2068{
2069	struct mac_biba *subj, *obj;
2070
2071	/*
2072	 * Rely on the use of open()-time protections to handle
2073	 * non-revocation cases.
2074	 */
2075	if (!mac_biba_enabled || !revocation_enabled)
2076		return (0);
2077
2078	subj = SLOT(&cred->cr_label);
2079	obj = SLOT(label);
2080
2081	if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
2082		if (!mac_biba_dominate_single(obj, subj))
2083			return (EACCES);
2084	}
2085	if (prot & VM_PROT_WRITE) {
2086		if (!mac_biba_dominate_single(subj, obj))
2087			return (EACCES);
2088	}
2089
2090	return (0);
2091}
2092
2093static int
2094mac_biba_check_vnode_open(struct ucred *cred, struct vnode *vp,
2095    struct label *vnodelabel, mode_t acc_mode)
2096{
2097	struct mac_biba *subj, *obj;
2098
2099	if (!mac_biba_enabled)
2100		return (0);
2101
2102	subj = SLOT(&cred->cr_label);
2103	obj = SLOT(vnodelabel);
2104
2105	/* XXX privilege override for admin? */
2106	if (acc_mode & (VREAD | VEXEC | VSTAT)) {
2107		if (!mac_biba_dominate_single(obj, subj))
2108			return (EACCES);
2109	}
2110	if (acc_mode & (VWRITE | VAPPEND | VADMIN)) {
2111		if (!mac_biba_dominate_single(subj, obj))
2112			return (EACCES);
2113	}
2114
2115	return (0);
2116}
2117
2118static int
2119mac_biba_check_vnode_poll(struct ucred *active_cred, struct ucred *file_cred,
2120    struct vnode *vp, struct label *label)
2121{
2122	struct mac_biba *subj, *obj;
2123
2124	if (!mac_biba_enabled || !revocation_enabled)
2125		return (0);
2126
2127	subj = SLOT(&active_cred->cr_label);
2128	obj = SLOT(label);
2129
2130	if (!mac_biba_dominate_single(obj, subj))
2131		return (EACCES);
2132
2133	return (0);
2134}
2135
2136static int
2137mac_biba_check_vnode_read(struct ucred *active_cred, struct ucred *file_cred,
2138    struct vnode *vp, struct label *label)
2139{
2140	struct mac_biba *subj, *obj;
2141
2142	if (!mac_biba_enabled || !revocation_enabled)
2143		return (0);
2144
2145	subj = SLOT(&active_cred->cr_label);
2146	obj = SLOT(label);
2147
2148	if (!mac_biba_dominate_single(obj, subj))
2149		return (EACCES);
2150
2151	return (0);
2152}
2153
2154static int
2155mac_biba_check_vnode_readdir(struct ucred *cred, struct vnode *dvp,
2156    struct label *dlabel)
2157{
2158	struct mac_biba *subj, *obj;
2159
2160	if (!mac_biba_enabled)
2161		return (0);
2162
2163	subj = SLOT(&cred->cr_label);
2164	obj = SLOT(dlabel);
2165
2166	if (!mac_biba_dominate_single(obj, subj))
2167		return (EACCES);
2168
2169	return (0);
2170}
2171
2172static int
2173mac_biba_check_vnode_readlink(struct ucred *cred, struct vnode *vp,
2174    struct label *label)
2175{
2176	struct mac_biba *subj, *obj;
2177
2178	if (!mac_biba_enabled)
2179		return (0);
2180
2181	subj = SLOT(&cred->cr_label);
2182	obj = SLOT(label);
2183
2184	if (!mac_biba_dominate_single(obj, subj))
2185		return (EACCES);
2186
2187	return (0);
2188}
2189
2190static int
2191mac_biba_check_vnode_relabel(struct ucred *cred, struct vnode *vp,
2192    struct label *vnodelabel, struct label *newlabel)
2193{
2194	struct mac_biba *old, *new, *subj;
2195	int error;
2196
2197	old = SLOT(vnodelabel);
2198	new = SLOT(newlabel);
2199	subj = SLOT(&cred->cr_label);
2200
2201	/*
2202	 * If there is a Biba label update for the vnode, it must be a
2203	 * single label.
2204	 */
2205	error = biba_atmostflags(new, MAC_BIBA_FLAG_SINGLE);
2206	if (error)
2207		return (error);
2208
2209	/*
2210	 * To perform a relabel of the vnode (Biba label or not), Biba must
2211	 * authorize the relabel.
2212	 */
2213	if (!mac_biba_single_in_range(old, subj))
2214		return (EPERM);
2215
2216	/*
2217	 * If the Biba label is to be changed, authorize as appropriate.
2218	 */
2219	if (new->mb_flags & MAC_BIBA_FLAG_SINGLE) {
2220		/*
2221		 * To change the Biba label on a vnode, the new vnode label
2222		 * must be in the subject range.
2223		 */
2224		if (!mac_biba_single_in_range(new, subj))
2225			return (EPERM);
2226
2227		/*
2228		 * To change the Biba label on the vnode to be EQUAL,
2229		 * the subject must have appropriate privilege.
2230		 */
2231		if (mac_biba_contains_equal(new)) {
2232			error = mac_biba_subject_privileged(subj);
2233			if (error)
2234				return (error);
2235		}
2236	}
2237
2238	return (0);
2239}
2240
2241static int
2242mac_biba_check_vnode_rename_from(struct ucred *cred, struct vnode *dvp,
2243    struct label *dlabel, struct vnode *vp, struct label *label,
2244    struct componentname *cnp)
2245{
2246	struct mac_biba *subj, *obj;
2247
2248	if (!mac_biba_enabled)
2249		return (0);
2250
2251	subj = SLOT(&cred->cr_label);
2252	obj = SLOT(dlabel);
2253
2254	if (!mac_biba_dominate_single(subj, obj))
2255		return (EACCES);
2256
2257	obj = SLOT(label);
2258
2259	if (!mac_biba_dominate_single(subj, obj))
2260		return (EACCES);
2261
2262	return (0);
2263}
2264
2265static int
2266mac_biba_check_vnode_rename_to(struct ucred *cred, struct vnode *dvp,
2267    struct label *dlabel, struct vnode *vp, struct label *label, int samedir,
2268    struct componentname *cnp)
2269{
2270	struct mac_biba *subj, *obj;
2271
2272	if (!mac_biba_enabled)
2273		return (0);
2274
2275	subj = SLOT(&cred->cr_label);
2276	obj = SLOT(dlabel);
2277
2278	if (!mac_biba_dominate_single(subj, obj))
2279		return (EACCES);
2280
2281	if (vp != NULL) {
2282		obj = SLOT(label);
2283
2284		if (!mac_biba_dominate_single(subj, obj))
2285			return (EACCES);
2286	}
2287
2288	return (0);
2289}
2290
2291static int
2292mac_biba_check_vnode_revoke(struct ucred *cred, struct vnode *vp,
2293    struct label *label)
2294{
2295	struct mac_biba *subj, *obj;
2296
2297	if (!mac_biba_enabled)
2298		return (0);
2299
2300	subj = SLOT(&cred->cr_label);
2301	obj = SLOT(label);
2302
2303	if (!mac_biba_dominate_single(subj, obj))
2304		return (EACCES);
2305
2306	return (0);
2307}
2308
2309static int
2310mac_biba_check_vnode_setacl(struct ucred *cred, struct vnode *vp,
2311    struct label *label, acl_type_t type, struct acl *acl)
2312{
2313	struct mac_biba *subj, *obj;
2314
2315	if (!mac_biba_enabled)
2316		return (0);
2317
2318	subj = SLOT(&cred->cr_label);
2319	obj = SLOT(label);
2320
2321	if (!mac_biba_dominate_single(subj, obj))
2322		return (EACCES);
2323
2324	return (0);
2325}
2326
2327static int
2328mac_biba_check_vnode_setextattr(struct ucred *cred, struct vnode *vp,
2329    struct label *vnodelabel, int attrnamespace, const char *name,
2330    struct uio *uio)
2331{
2332	struct mac_biba *subj, *obj;
2333
2334	if (!mac_biba_enabled)
2335		return (0);
2336
2337	subj = SLOT(&cred->cr_label);
2338	obj = SLOT(vnodelabel);
2339
2340	if (!mac_biba_dominate_single(subj, obj))
2341		return (EACCES);
2342
2343	/* XXX: protect the MAC EA in a special way? */
2344
2345	return (0);
2346}
2347
2348static int
2349mac_biba_check_vnode_setflags(struct ucred *cred, struct vnode *vp,
2350    struct label *vnodelabel, u_long flags)
2351{
2352	struct mac_biba *subj, *obj;
2353
2354	if (!mac_biba_enabled)
2355		return (0);
2356
2357	subj = SLOT(&cred->cr_label);
2358	obj = SLOT(vnodelabel);
2359
2360	if (!mac_biba_dominate_single(subj, obj))
2361		return (EACCES);
2362
2363	return (0);
2364}
2365
2366static int
2367mac_biba_check_vnode_setmode(struct ucred *cred, struct vnode *vp,
2368    struct label *vnodelabel, mode_t mode)
2369{
2370	struct mac_biba *subj, *obj;
2371
2372	if (!mac_biba_enabled)
2373		return (0);
2374
2375	subj = SLOT(&cred->cr_label);
2376	obj = SLOT(vnodelabel);
2377
2378	if (!mac_biba_dominate_single(subj, obj))
2379		return (EACCES);
2380
2381	return (0);
2382}
2383
2384static int
2385mac_biba_check_vnode_setowner(struct ucred *cred, struct vnode *vp,
2386    struct label *vnodelabel, uid_t uid, gid_t gid)
2387{
2388	struct mac_biba *subj, *obj;
2389
2390	if (!mac_biba_enabled)
2391		return (0);
2392
2393	subj = SLOT(&cred->cr_label);
2394	obj = SLOT(vnodelabel);
2395
2396	if (!mac_biba_dominate_single(subj, obj))
2397		return (EACCES);
2398
2399	return (0);
2400}
2401
2402static int
2403mac_biba_check_vnode_setutimes(struct ucred *cred, struct vnode *vp,
2404    struct label *vnodelabel, struct timespec atime, struct timespec mtime)
2405{
2406	struct mac_biba *subj, *obj;
2407
2408	if (!mac_biba_enabled)
2409		return (0);
2410
2411	subj = SLOT(&cred->cr_label);
2412	obj = SLOT(vnodelabel);
2413
2414	if (!mac_biba_dominate_single(subj, obj))
2415		return (EACCES);
2416
2417	return (0);
2418}
2419
2420static int
2421mac_biba_check_vnode_stat(struct ucred *active_cred, struct ucred *file_cred,
2422    struct vnode *vp, struct label *vnodelabel)
2423{
2424	struct mac_biba *subj, *obj;
2425
2426	if (!mac_biba_enabled)
2427		return (0);
2428
2429	subj = SLOT(&active_cred->cr_label);
2430	obj = SLOT(vnodelabel);
2431
2432	if (!mac_biba_dominate_single(obj, subj))
2433		return (EACCES);
2434
2435	return (0);
2436}
2437
2438static int
2439mac_biba_check_vnode_write(struct ucred *active_cred,
2440    struct ucred *file_cred, struct vnode *vp, struct label *label)
2441{
2442	struct mac_biba *subj, *obj;
2443
2444	if (!mac_biba_enabled || !revocation_enabled)
2445		return (0);
2446
2447	subj = SLOT(&active_cred->cr_label);
2448	obj = SLOT(label);
2449
2450	if (!mac_biba_dominate_single(subj, obj))
2451		return (EACCES);
2452
2453	return (0);
2454}
2455
2456static struct mac_policy_op_entry mac_biba_ops[] =
2457{
2458	{ MAC_DESTROY,
2459	    (macop_t)mac_biba_destroy },
2460	{ MAC_INIT,
2461	    (macop_t)mac_biba_init },
2462	{ MAC_INIT_BPFDESC_LABEL,
2463	    (macop_t)mac_biba_init_label },
2464	{ MAC_INIT_CRED_LABEL,
2465	    (macop_t)mac_biba_init_label },
2466	{ MAC_INIT_DEVFSDIRENT_LABEL,
2467	    (macop_t)mac_biba_init_label },
2468	{ MAC_INIT_IFNET_LABEL,
2469	    (macop_t)mac_biba_init_label },
2470	{ MAC_INIT_IPQ_LABEL,
2471	    (macop_t)mac_biba_init_label },
2472	{ MAC_INIT_MBUF_LABEL,
2473	    (macop_t)mac_biba_init_label_waitcheck },
2474	{ MAC_INIT_MOUNT_LABEL,
2475	    (macop_t)mac_biba_init_label },
2476	{ MAC_INIT_MOUNT_FS_LABEL,
2477	    (macop_t)mac_biba_init_label },
2478	{ MAC_INIT_PIPE_LABEL,
2479	    (macop_t)mac_biba_init_label },
2480	{ MAC_INIT_SOCKET_LABEL,
2481	    (macop_t)mac_biba_init_label_waitcheck },
2482	{ MAC_INIT_SOCKET_PEER_LABEL,
2483	    (macop_t)mac_biba_init_label_waitcheck },
2484	{ MAC_INIT_VNODE_LABEL,
2485	    (macop_t)mac_biba_init_label },
2486	{ MAC_DESTROY_BPFDESC_LABEL,
2487	    (macop_t)mac_biba_destroy_label },
2488	{ MAC_DESTROY_CRED_LABEL,
2489	    (macop_t)mac_biba_destroy_label },
2490	{ MAC_DESTROY_DEVFSDIRENT_LABEL,
2491	    (macop_t)mac_biba_destroy_label },
2492	{ MAC_DESTROY_IFNET_LABEL,
2493	    (macop_t)mac_biba_destroy_label },
2494	{ MAC_DESTROY_IPQ_LABEL,
2495	    (macop_t)mac_biba_destroy_label },
2496	{ MAC_DESTROY_MBUF_LABEL,
2497	    (macop_t)mac_biba_destroy_label },
2498	{ MAC_DESTROY_MOUNT_LABEL,
2499	    (macop_t)mac_biba_destroy_label },
2500	{ MAC_DESTROY_MOUNT_FS_LABEL,
2501	    (macop_t)mac_biba_destroy_label },
2502	{ MAC_DESTROY_PIPE_LABEL,
2503	    (macop_t)mac_biba_destroy_label },
2504	{ MAC_DESTROY_SOCKET_LABEL,
2505	    (macop_t)mac_biba_destroy_label },
2506	{ MAC_DESTROY_SOCKET_PEER_LABEL,
2507	    (macop_t)mac_biba_destroy_label },
2508	{ MAC_DESTROY_VNODE_LABEL,
2509	    (macop_t)mac_biba_destroy_label },
2510	{ MAC_COPY_PIPE_LABEL,
2511	    (macop_t)mac_biba_copy_label },
2512	{ MAC_COPY_VNODE_LABEL,
2513	    (macop_t)mac_biba_copy_label },
2514	{ MAC_EXTERNALIZE_CRED_LABEL,
2515	    (macop_t)mac_biba_externalize_label },
2516	{ MAC_EXTERNALIZE_IFNET_LABEL,
2517	    (macop_t)mac_biba_externalize_label },
2518	{ MAC_EXTERNALIZE_PIPE_LABEL,
2519	    (macop_t)mac_biba_externalize_label },
2520	{ MAC_EXTERNALIZE_SOCKET_LABEL,
2521	    (macop_t)mac_biba_externalize_label },
2522	{ MAC_EXTERNALIZE_SOCKET_PEER_LABEL,
2523	    (macop_t)mac_biba_externalize_label },
2524	{ MAC_EXTERNALIZE_VNODE_LABEL,
2525	    (macop_t)mac_biba_externalize_label },
2526	{ MAC_INTERNALIZE_CRED_LABEL,
2527	    (macop_t)mac_biba_internalize_label },
2528	{ MAC_INTERNALIZE_IFNET_LABEL,
2529	    (macop_t)mac_biba_internalize_label },
2530	{ MAC_INTERNALIZE_PIPE_LABEL,
2531	    (macop_t)mac_biba_internalize_label },
2532	{ MAC_INTERNALIZE_SOCKET_LABEL,
2533	    (macop_t)mac_biba_internalize_label },
2534	{ MAC_INTERNALIZE_VNODE_LABEL,
2535	    (macop_t)mac_biba_internalize_label },
2536	{ MAC_CREATE_DEVFS_DEVICE,
2537	    (macop_t)mac_biba_create_devfs_device },
2538	{ MAC_CREATE_DEVFS_DIRECTORY,
2539	    (macop_t)mac_biba_create_devfs_directory },
2540	{ MAC_CREATE_DEVFS_SYMLINK,
2541	    (macop_t)mac_biba_create_devfs_symlink },
2542	{ MAC_CREATE_DEVFS_VNODE,
2543	    (macop_t)mac_biba_create_devfs_vnode },
2544	{ MAC_CREATE_MOUNT,
2545	    (macop_t)mac_biba_create_mount },
2546	{ MAC_CREATE_ROOT_MOUNT,
2547	    (macop_t)mac_biba_create_root_mount },
2548	{ MAC_RELABEL_VNODE,
2549	    (macop_t)mac_biba_relabel_vnode },
2550	{ MAC_UPDATE_DEVFSDIRENT,
2551	    (macop_t)mac_biba_update_devfsdirent },
2552	{ MAC_ASSOCIATE_VNODE_DEVFS,
2553	    (macop_t)mac_biba_associate_vnode_devfs },
2554	{ MAC_ASSOCIATE_VNODE_EXTATTR,
2555	    (macop_t)mac_biba_associate_vnode_extattr },
2556	{ MAC_ASSOCIATE_VNODE_SINGLELABEL,
2557	    (macop_t)mac_biba_associate_vnode_singlelabel },
2558	{ MAC_CREATE_VNODE_EXTATTR,
2559	    (macop_t)mac_biba_create_vnode_extattr },
2560	{ MAC_SETLABEL_VNODE_EXTATTR,
2561	    (macop_t)mac_biba_setlabel_vnode_extattr },
2562	{ MAC_CREATE_MBUF_FROM_SOCKET,
2563	    (macop_t)mac_biba_create_mbuf_from_socket },
2564	{ MAC_CREATE_PIPE,
2565	    (macop_t)mac_biba_create_pipe },
2566	{ MAC_CREATE_SOCKET,
2567	    (macop_t)mac_biba_create_socket },
2568	{ MAC_CREATE_SOCKET_FROM_SOCKET,
2569	    (macop_t)mac_biba_create_socket_from_socket },
2570	{ MAC_RELABEL_PIPE,
2571	    (macop_t)mac_biba_relabel_pipe },
2572	{ MAC_RELABEL_SOCKET,
2573	    (macop_t)mac_biba_relabel_socket },
2574	{ MAC_SET_SOCKET_PEER_FROM_MBUF,
2575	    (macop_t)mac_biba_set_socket_peer_from_mbuf },
2576	{ MAC_SET_SOCKET_PEER_FROM_SOCKET,
2577	    (macop_t)mac_biba_set_socket_peer_from_socket },
2578	{ MAC_CREATE_BPFDESC,
2579	    (macop_t)mac_biba_create_bpfdesc },
2580	{ MAC_CREATE_DATAGRAM_FROM_IPQ,
2581	    (macop_t)mac_biba_create_datagram_from_ipq },
2582	{ MAC_CREATE_FRAGMENT,
2583	    (macop_t)mac_biba_create_fragment },
2584	{ MAC_CREATE_IFNET,
2585	    (macop_t)mac_biba_create_ifnet },
2586	{ MAC_CREATE_IPQ,
2587	    (macop_t)mac_biba_create_ipq },
2588	{ MAC_CREATE_MBUF_FROM_MBUF,
2589	    (macop_t)mac_biba_create_mbuf_from_mbuf },
2590	{ MAC_CREATE_MBUF_LINKLAYER,
2591	    (macop_t)mac_biba_create_mbuf_linklayer },
2592	{ MAC_CREATE_MBUF_FROM_BPFDESC,
2593	    (macop_t)mac_biba_create_mbuf_from_bpfdesc },
2594	{ MAC_CREATE_MBUF_FROM_IFNET,
2595	    (macop_t)mac_biba_create_mbuf_from_ifnet },
2596	{ MAC_CREATE_MBUF_MULTICAST_ENCAP,
2597	    (macop_t)mac_biba_create_mbuf_multicast_encap },
2598	{ MAC_CREATE_MBUF_NETLAYER,
2599	    (macop_t)mac_biba_create_mbuf_netlayer },
2600	{ MAC_FRAGMENT_MATCH,
2601	    (macop_t)mac_biba_fragment_match },
2602	{ MAC_RELABEL_IFNET,
2603	    (macop_t)mac_biba_relabel_ifnet },
2604	{ MAC_UPDATE_IPQ,
2605	    (macop_t)mac_biba_update_ipq },
2606	{ MAC_CREATE_CRED,
2607	    (macop_t)mac_biba_create_cred },
2608	{ MAC_EXECVE_TRANSITION,
2609	    (macop_t)mac_biba_execve_transition },
2610	{ MAC_EXECVE_WILL_TRANSITION,
2611	    (macop_t)mac_biba_execve_will_transition },
2612	{ MAC_CREATE_PROC0,
2613	    (macop_t)mac_biba_create_proc0 },
2614	{ MAC_CREATE_PROC1,
2615	    (macop_t)mac_biba_create_proc1 },
2616	{ MAC_RELABEL_CRED,
2617	    (macop_t)mac_biba_relabel_cred },
2618	{ MAC_CHECK_BPFDESC_RECEIVE,
2619	    (macop_t)mac_biba_check_bpfdesc_receive },
2620	{ MAC_CHECK_CRED_RELABEL,
2621	    (macop_t)mac_biba_check_cred_relabel },
2622	{ MAC_CHECK_CRED_VISIBLE,
2623	    (macop_t)mac_biba_check_cred_visible },
2624	{ MAC_CHECK_IFNET_RELABEL,
2625	    (macop_t)mac_biba_check_ifnet_relabel },
2626	{ MAC_CHECK_IFNET_TRANSMIT,
2627	    (macop_t)mac_biba_check_ifnet_transmit },
2628	{ MAC_CHECK_MOUNT_STAT,
2629	    (macop_t)mac_biba_check_mount_stat },
2630	{ MAC_CHECK_PIPE_IOCTL,
2631	    (macop_t)mac_biba_check_pipe_ioctl },
2632	{ MAC_CHECK_PIPE_POLL,
2633	    (macop_t)mac_biba_check_pipe_poll },
2634	{ MAC_CHECK_PIPE_READ,
2635	    (macop_t)mac_biba_check_pipe_read },
2636	{ MAC_CHECK_PIPE_RELABEL,
2637	    (macop_t)mac_biba_check_pipe_relabel },
2638	{ MAC_CHECK_PIPE_STAT,
2639	    (macop_t)mac_biba_check_pipe_stat },
2640	{ MAC_CHECK_PIPE_WRITE,
2641	    (macop_t)mac_biba_check_pipe_write },
2642	{ MAC_CHECK_PROC_DEBUG,
2643	    (macop_t)mac_biba_check_proc_debug },
2644	{ MAC_CHECK_PROC_SCHED,
2645	    (macop_t)mac_biba_check_proc_sched },
2646	{ MAC_CHECK_PROC_SIGNAL,
2647	    (macop_t)mac_biba_check_proc_signal },
2648	{ MAC_CHECK_SOCKET_DELIVER,
2649	    (macop_t)mac_biba_check_socket_deliver },
2650	{ MAC_CHECK_SOCKET_RELABEL,
2651	    (macop_t)mac_biba_check_socket_relabel },
2652	{ MAC_CHECK_SOCKET_VISIBLE,
2653	    (macop_t)mac_biba_check_socket_visible },
2654	{ MAC_CHECK_VNODE_ACCESS,
2655	    (macop_t)mac_biba_check_vnode_open },
2656	{ MAC_CHECK_VNODE_CHDIR,
2657	    (macop_t)mac_biba_check_vnode_chdir },
2658	{ MAC_CHECK_VNODE_CHROOT,
2659	    (macop_t)mac_biba_check_vnode_chroot },
2660	{ MAC_CHECK_VNODE_CREATE,
2661	    (macop_t)mac_biba_check_vnode_create },
2662	{ MAC_CHECK_VNODE_DELETE,
2663	    (macop_t)mac_biba_check_vnode_delete },
2664	{ MAC_CHECK_VNODE_DELETEACL,
2665	    (macop_t)mac_biba_check_vnode_deleteacl },
2666	{ MAC_CHECK_VNODE_EXEC,
2667	    (macop_t)mac_biba_check_vnode_exec },
2668	{ MAC_CHECK_VNODE_GETACL,
2669	    (macop_t)mac_biba_check_vnode_getacl },
2670	{ MAC_CHECK_VNODE_GETEXTATTR,
2671	    (macop_t)mac_biba_check_vnode_getextattr },
2672	{ MAC_CHECK_VNODE_LINK,
2673	    (macop_t)mac_biba_check_vnode_link },
2674	{ MAC_CHECK_VNODE_LOOKUP,
2675	    (macop_t)mac_biba_check_vnode_lookup },
2676	{ MAC_CHECK_VNODE_MMAP,
2677	    (macop_t)mac_biba_check_vnode_mmap },
2678	{ MAC_CHECK_VNODE_MPROTECT,
2679	    (macop_t)mac_biba_check_vnode_mmap },
2680	{ MAC_CHECK_VNODE_OPEN,
2681	    (macop_t)mac_biba_check_vnode_open },
2682	{ MAC_CHECK_VNODE_POLL,
2683	    (macop_t)mac_biba_check_vnode_poll },
2684	{ MAC_CHECK_VNODE_READ,
2685	    (macop_t)mac_biba_check_vnode_read },
2686	{ MAC_CHECK_VNODE_READDIR,
2687	    (macop_t)mac_biba_check_vnode_readdir },
2688	{ MAC_CHECK_VNODE_READLINK,
2689	    (macop_t)mac_biba_check_vnode_readlink },
2690	{ MAC_CHECK_VNODE_RELABEL,
2691	    (macop_t)mac_biba_check_vnode_relabel },
2692	{ MAC_CHECK_VNODE_RENAME_FROM,
2693	    (macop_t)mac_biba_check_vnode_rename_from },
2694	{ MAC_CHECK_VNODE_RENAME_TO,
2695	    (macop_t)mac_biba_check_vnode_rename_to },
2696	{ MAC_CHECK_VNODE_REVOKE,
2697	    (macop_t)mac_biba_check_vnode_revoke },
2698	{ MAC_CHECK_VNODE_SETACL,
2699	    (macop_t)mac_biba_check_vnode_setacl },
2700	{ MAC_CHECK_VNODE_SETEXTATTR,
2701	    (macop_t)mac_biba_check_vnode_setextattr },
2702	{ MAC_CHECK_VNODE_SETFLAGS,
2703	    (macop_t)mac_biba_check_vnode_setflags },
2704	{ MAC_CHECK_VNODE_SETMODE,
2705	    (macop_t)mac_biba_check_vnode_setmode },
2706	{ MAC_CHECK_VNODE_SETOWNER,
2707	    (macop_t)mac_biba_check_vnode_setowner },
2708	{ MAC_CHECK_VNODE_SETUTIMES,
2709	    (macop_t)mac_biba_check_vnode_setutimes },
2710	{ MAC_CHECK_VNODE_STAT,
2711	    (macop_t)mac_biba_check_vnode_stat },
2712	{ MAC_CHECK_VNODE_WRITE,
2713	    (macop_t)mac_biba_check_vnode_write },
2714	{ MAC_OP_LAST, NULL }
2715};
2716
2717MAC_POLICY_SET(mac_biba_ops, trustedbsd_mac_biba, "TrustedBSD MAC/Biba",
2718    MPC_LOADTIME_FLAG_NOTLATE, &mac_biba_slot);
2719