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