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