xref: /freebsd/sys/security/mac_lomac/mac_lomac.c (revision 7d4c0fac8c7db9c5741ba98a8b3ce3c43feb1cf4)
1db2661ceSRobert Watson /*-
23de40469SRobert Watson  * Copyright (c) 1999-2002, 2007-2009 Robert N. M. Watson
3c77cf2b1SRobert Watson  * Copyright (c) 2001-2005 Networks Associates Technology, Inc.
430d239bcSRobert Watson  * Copyright (c) 2006 SPARTA, Inc.
5db2661ceSRobert Watson  * All rights reserved.
6db2661ceSRobert Watson  *
7db2661ceSRobert Watson  * This software was developed by Robert Watson for the TrustedBSD Project.
8db2661ceSRobert Watson  *
9db2661ceSRobert Watson  * This software was developed for the FreeBSD Project in part by NAI Labs,
10db2661ceSRobert Watson  * the Security Research Division of Network Associates, Inc. under
11db2661ceSRobert Watson  * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
12db2661ceSRobert Watson  * CHATS research program.
13db2661ceSRobert Watson  *
1430d239bcSRobert Watson  * This software was enhanced by SPARTA ISSO under SPAWAR contract
1530d239bcSRobert Watson  * N66001-04-C-6019 ("SEFOS").
1630d239bcSRobert Watson  *
17db2661ceSRobert Watson  * Redistribution and use in source and binary forms, with or without
18db2661ceSRobert Watson  * modification, are permitted provided that the following conditions
19db2661ceSRobert Watson  * are met:
20db2661ceSRobert Watson  * 1. Redistributions of source code must retain the above copyright
21db2661ceSRobert Watson  *    notice, this list of conditions and the following disclaimer.
22db2661ceSRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
23db2661ceSRobert Watson  *    notice, this list of conditions and the following disclaimer in the
24db2661ceSRobert Watson  *    documentation and/or other materials provided with the distribution.
25db2661ceSRobert Watson  *
26db2661ceSRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27db2661ceSRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28db2661ceSRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29db2661ceSRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30db2661ceSRobert Watson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31db2661ceSRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32db2661ceSRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33db2661ceSRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34db2661ceSRobert Watson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35db2661ceSRobert Watson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36db2661ceSRobert Watson  * SUCH DAMAGE.
37db2661ceSRobert Watson  */
38db2661ceSRobert Watson 
39db2661ceSRobert Watson /*
40db2661ceSRobert Watson  * Developed by the TrustedBSD Project.
41c14d15aeSRobert Watson  *
42db2661ceSRobert Watson  * Low-watermark floating label mandatory integrity policy.
43db2661ceSRobert Watson  */
44db2661ceSRobert Watson 
45db2661ceSRobert Watson #include <sys/types.h>
46db2661ceSRobert Watson #include <sys/param.h>
47db2661ceSRobert Watson #include <sys/acl.h>
48db2661ceSRobert Watson #include <sys/conf.h>
49db2661ceSRobert Watson #include <sys/extattr.h>
50db2661ceSRobert Watson #include <sys/kernel.h>
51db2661ceSRobert Watson #include <sys/malloc.h>
52c92163dcSChristian S.J. Peron #include <sys/mman.h>
53db2661ceSRobert Watson #include <sys/mount.h>
54acd3428bSRobert Watson #include <sys/priv.h>
55db2661ceSRobert Watson #include <sys/proc.h>
56f51e5803SRobert Watson #include <sys/sbuf.h>
57db2661ceSRobert Watson #include <sys/systm.h>
58db2661ceSRobert Watson #include <sys/sysproto.h>
59db2661ceSRobert Watson #include <sys/sysent.h>
60db2661ceSRobert Watson #include <sys/systm.h>
61db2661ceSRobert Watson #include <sys/vnode.h>
62db2661ceSRobert Watson #include <sys/file.h>
63db2661ceSRobert Watson #include <sys/socket.h>
64db2661ceSRobert Watson #include <sys/socketvar.h>
6536422989SPoul-Henning Kamp #include <sys/sx.h>
66db2661ceSRobert Watson #include <sys/pipe.h>
67db2661ceSRobert Watson #include <sys/sysctl.h>
68db2661ceSRobert Watson #include <sys/syslog.h>
69db2661ceSRobert Watson 
70db2661ceSRobert Watson #include <fs/devfs/devfs.h>
71db2661ceSRobert Watson 
72db2661ceSRobert Watson #include <net/bpfdesc.h>
73db2661ceSRobert Watson #include <net/if.h>
74db2661ceSRobert Watson #include <net/if_types.h>
75db2661ceSRobert Watson #include <net/if_var.h>
76db2661ceSRobert Watson 
77db2661ceSRobert Watson #include <netinet/in.h>
78a557af22SRobert Watson #include <netinet/in_pcb.h>
79db2661ceSRobert Watson #include <netinet/ip_var.h>
80db2661ceSRobert Watson 
81db2661ceSRobert Watson #include <vm/vm.h>
82db2661ceSRobert Watson 
830efd6615SRobert Watson #include <security/mac/mac_policy.h>
84aed55708SRobert Watson #include <security/mac/mac_framework.h>
85db2661ceSRobert Watson #include <security/mac_lomac/mac_lomac.h>
86db2661ceSRobert Watson 
87db2661ceSRobert Watson struct mac_lomac_proc {
88db2661ceSRobert Watson 	struct mac_lomac mac_lomac;
89db2661ceSRobert Watson 	struct mtx mtx;
90db2661ceSRobert Watson };
91db2661ceSRobert Watson 
927029da5cSPawel Biernacki static SYSCTL_NODE(_security_mac, OID_AUTO, lomac,
937029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
94db2661ceSRobert Watson     "TrustedBSD mac_lomac policy controls");
95db2661ceSRobert Watson 
963f1a7a90SRobert Watson static int	lomac_label_size = sizeof(struct mac_lomac);
97db2661ceSRobert Watson SYSCTL_INT(_security_mac_lomac, OID_AUTO, label_size, CTLFLAG_RD,
983f1a7a90SRobert Watson     &lomac_label_size, 0, "Size of struct mac_lomac");
99db2661ceSRobert Watson 
1003f1a7a90SRobert Watson static int	lomac_enabled = 1;
101af3b2549SHans Petter Selasky SYSCTL_INT(_security_mac_lomac, OID_AUTO, enabled, CTLFLAG_RWTUN,
1023f1a7a90SRobert Watson     &lomac_enabled, 0, "Enforce MAC/LOMAC policy");
103db2661ceSRobert Watson 
104db2661ceSRobert Watson static int	destroyed_not_inited;
105db2661ceSRobert Watson SYSCTL_INT(_security_mac_lomac, OID_AUTO, destroyed_not_inited, CTLFLAG_RD,
106db2661ceSRobert Watson     &destroyed_not_inited, 0, "Count of labels destroyed but not inited");
107db2661ceSRobert Watson 
108db2661ceSRobert Watson static int	trust_all_interfaces = 0;
109af3b2549SHans Petter Selasky SYSCTL_INT(_security_mac_lomac, OID_AUTO, trust_all_interfaces, CTLFLAG_RDTUN,
110db2661ceSRobert Watson     &trust_all_interfaces, 0, "Consider all interfaces 'trusted' by MAC/LOMAC");
111db2661ceSRobert Watson 
112db2661ceSRobert Watson static char	trusted_interfaces[128];
113af3b2549SHans Petter Selasky SYSCTL_STRING(_security_mac_lomac, OID_AUTO, trusted_interfaces, CTLFLAG_RDTUN,
114*7d4c0facSZhenlei Huang     trusted_interfaces, sizeof(trusted_interfaces),
115*7d4c0facSZhenlei Huang     "Interfaces considered 'trusted' by MAC/LOMAC");
116db2661ceSRobert Watson 
117db2661ceSRobert Watson static int	ptys_equal = 0;
118af3b2549SHans Petter Selasky SYSCTL_INT(_security_mac_lomac, OID_AUTO, ptys_equal, CTLFLAG_RWTUN,
119db2661ceSRobert Watson     &ptys_equal, 0, "Label pty devices as lomac/equal on create");
120db2661ceSRobert Watson 
121db2661ceSRobert Watson static int	revocation_enabled = 1;
122af3b2549SHans Petter Selasky SYSCTL_INT(_security_mac_lomac, OID_AUTO, revocation_enabled, CTLFLAG_RWTUN,
123db2661ceSRobert Watson     &revocation_enabled, 0, "Revoke access to objects on relabel");
124db2661ceSRobert Watson 
1253f1a7a90SRobert Watson static int	lomac_slot;
1263f1a7a90SRobert Watson #define	SLOT(l)	((struct mac_lomac *)mac_label_get((l), lomac_slot))
1273f1a7a90SRobert Watson #define	SLOT_SET(l, val) mac_label_set((l), lomac_slot, (uintptr_t)(val))
128db2661ceSRobert Watson #define	PSLOT(l) ((struct mac_lomac_proc *)				\
1293f1a7a90SRobert Watson     mac_label_get((l), lomac_slot))
1303f1a7a90SRobert Watson #define	PSLOT_SET(l, val) mac_label_set((l), lomac_slot, (uintptr_t)(val))
131db2661ceSRobert Watson 
132d745c852SEd Schouten static MALLOC_DEFINE(M_LOMAC, "mac_lomac_label", "MAC/LOMAC labels");
133db2661ceSRobert Watson 
134db2661ceSRobert Watson static struct mac_lomac *
lomac_alloc(int flag)135db2661ceSRobert Watson lomac_alloc(int flag)
136db2661ceSRobert Watson {
1373f1a7a90SRobert Watson 	struct mac_lomac *ml;
138db2661ceSRobert Watson 
1393f1a7a90SRobert Watson 	ml = malloc(sizeof(*ml), M_LOMAC, M_ZERO | flag);
140db2661ceSRobert Watson 
1413f1a7a90SRobert Watson 	return (ml);
142db2661ceSRobert Watson }
143db2661ceSRobert Watson 
144db2661ceSRobert Watson static void
lomac_free(struct mac_lomac * ml)1453f1a7a90SRobert Watson lomac_free(struct mac_lomac *ml)
146db2661ceSRobert Watson {
147db2661ceSRobert Watson 
1483f1a7a90SRobert Watson 	if (ml != NULL)
1493f1a7a90SRobert Watson 		free(ml, M_LOMAC);
150db2661ceSRobert Watson 	else
151db2661ceSRobert Watson 		atomic_add_int(&destroyed_not_inited, 1);
152db2661ceSRobert Watson }
153db2661ceSRobert Watson 
154db2661ceSRobert Watson static int
lomac_atmostflags(struct mac_lomac * ml,int flags)1553f1a7a90SRobert Watson lomac_atmostflags(struct mac_lomac *ml, int flags)
156db2661ceSRobert Watson {
157db2661ceSRobert Watson 
1583f1a7a90SRobert Watson 	if ((ml->ml_flags & flags) != ml->ml_flags)
159db2661ceSRobert Watson 		return (EINVAL);
160db2661ceSRobert Watson 	return (0);
161db2661ceSRobert Watson }
162db2661ceSRobert Watson 
163db2661ceSRobert Watson static int
lomac_dominate_element(struct mac_lomac_element * a,struct mac_lomac_element * b)1643f1a7a90SRobert Watson lomac_dominate_element(struct mac_lomac_element *a,
165db2661ceSRobert Watson     struct mac_lomac_element *b)
166db2661ceSRobert Watson {
167db2661ceSRobert Watson 
168db2661ceSRobert Watson 	switch (a->mle_type) {
169db2661ceSRobert Watson 	case MAC_LOMAC_TYPE_EQUAL:
170db2661ceSRobert Watson 	case MAC_LOMAC_TYPE_HIGH:
171db2661ceSRobert Watson 		return (1);
172db2661ceSRobert Watson 
173db2661ceSRobert Watson 	case MAC_LOMAC_TYPE_LOW:
174db2661ceSRobert Watson 		switch (b->mle_type) {
175db2661ceSRobert Watson 		case MAC_LOMAC_TYPE_GRADE:
176db2661ceSRobert Watson 		case MAC_LOMAC_TYPE_HIGH:
177db2661ceSRobert Watson 			return (0);
178db2661ceSRobert Watson 
179db2661ceSRobert Watson 		case MAC_LOMAC_TYPE_EQUAL:
180db2661ceSRobert Watson 		case MAC_LOMAC_TYPE_LOW:
181db2661ceSRobert Watson 			return (1);
182db2661ceSRobert Watson 
183db2661ceSRobert Watson 		default:
1843f1a7a90SRobert Watson 			panic("lomac_dominate_element: b->mle_type invalid");
185db2661ceSRobert Watson 		}
186db2661ceSRobert Watson 
187db2661ceSRobert Watson 	case MAC_LOMAC_TYPE_GRADE:
188db2661ceSRobert Watson 		switch (b->mle_type) {
189db2661ceSRobert Watson 		case MAC_LOMAC_TYPE_EQUAL:
190db2661ceSRobert Watson 		case MAC_LOMAC_TYPE_LOW:
191db2661ceSRobert Watson 			return (1);
192db2661ceSRobert Watson 
193db2661ceSRobert Watson 		case MAC_LOMAC_TYPE_HIGH:
194db2661ceSRobert Watson 			return (0);
195db2661ceSRobert Watson 
196db2661ceSRobert Watson 		case MAC_LOMAC_TYPE_GRADE:
197db2661ceSRobert Watson 			return (a->mle_grade >= b->mle_grade);
198db2661ceSRobert Watson 
199db2661ceSRobert Watson 		default:
2003f1a7a90SRobert Watson 			panic("lomac_dominate_element: b->mle_type invalid");
201db2661ceSRobert Watson 		}
202db2661ceSRobert Watson 
203db2661ceSRobert Watson 	default:
2043f1a7a90SRobert Watson 		panic("lomac_dominate_element: a->mle_type invalid");
205db2661ceSRobert Watson 	}
206db2661ceSRobert Watson }
207db2661ceSRobert Watson 
208db2661ceSRobert Watson static int
lomac_range_in_range(struct mac_lomac * rangea,struct mac_lomac * rangeb)2093f1a7a90SRobert Watson lomac_range_in_range(struct mac_lomac *rangea, struct mac_lomac *rangeb)
210db2661ceSRobert Watson {
211db2661ceSRobert Watson 
2123f1a7a90SRobert Watson 	return (lomac_dominate_element(&rangeb->ml_rangehigh,
213db2661ceSRobert Watson 	    &rangea->ml_rangehigh) &&
2143f1a7a90SRobert Watson 	    lomac_dominate_element(&rangea->ml_rangelow,
215db2661ceSRobert Watson 	    &rangeb->ml_rangelow));
216db2661ceSRobert Watson }
217db2661ceSRobert Watson 
218db2661ceSRobert Watson static int
lomac_single_in_range(struct mac_lomac * single,struct mac_lomac * range)2193f1a7a90SRobert Watson lomac_single_in_range(struct mac_lomac *single, struct mac_lomac *range)
220db2661ceSRobert Watson {
221db2661ceSRobert Watson 
222db2661ceSRobert Watson 	KASSERT((single->ml_flags & MAC_LOMAC_FLAG_SINGLE) != 0,
2233f1a7a90SRobert Watson 	    ("lomac_single_in_range: a not single"));
224db2661ceSRobert Watson 	KASSERT((range->ml_flags & MAC_LOMAC_FLAG_RANGE) != 0,
2253f1a7a90SRobert Watson 	    ("lomac_single_in_range: b not range"));
226db2661ceSRobert Watson 
2273f1a7a90SRobert Watson 	return (lomac_dominate_element(&range->ml_rangehigh,
2283f1a7a90SRobert Watson 	    &single->ml_single) && lomac_dominate_element(&single->ml_single,
229db2661ceSRobert Watson 	    &range->ml_rangelow));
230db2661ceSRobert Watson }
231db2661ceSRobert Watson 
232db2661ceSRobert Watson static int
lomac_auxsingle_in_range(struct mac_lomac * single,struct mac_lomac * range)2333f1a7a90SRobert Watson lomac_auxsingle_in_range(struct mac_lomac *single, struct mac_lomac *range)
234db2661ceSRobert Watson {
235db2661ceSRobert Watson 
236db2661ceSRobert Watson 	KASSERT((single->ml_flags & MAC_LOMAC_FLAG_AUX) != 0,
2373f1a7a90SRobert Watson 	    ("lomac_single_in_range: a not auxsingle"));
238db2661ceSRobert Watson 	KASSERT((range->ml_flags & MAC_LOMAC_FLAG_RANGE) != 0,
2393f1a7a90SRobert Watson 	    ("lomac_single_in_range: b not range"));
240db2661ceSRobert Watson 
2413f1a7a90SRobert Watson 	return (lomac_dominate_element(&range->ml_rangehigh,
242db2661ceSRobert Watson 	    &single->ml_auxsingle) &&
2433f1a7a90SRobert Watson 	    lomac_dominate_element(&single->ml_auxsingle,
244db2661ceSRobert Watson 	    &range->ml_rangelow));
245db2661ceSRobert Watson }
246db2661ceSRobert Watson 
247db2661ceSRobert Watson static int
lomac_dominate_single(struct mac_lomac * a,struct mac_lomac * b)2483f1a7a90SRobert Watson lomac_dominate_single(struct mac_lomac *a, struct mac_lomac *b)
249db2661ceSRobert Watson {
250db2661ceSRobert Watson 	KASSERT((a->ml_flags & MAC_LOMAC_FLAG_SINGLE) != 0,
2513f1a7a90SRobert Watson 	    ("lomac_dominate_single: a not single"));
252db2661ceSRobert Watson 	KASSERT((b->ml_flags & MAC_LOMAC_FLAG_SINGLE) != 0,
2533f1a7a90SRobert Watson 	    ("lomac_dominate_single: b not single"));
254db2661ceSRobert Watson 
2553f1a7a90SRobert Watson 	return (lomac_dominate_element(&a->ml_single, &b->ml_single));
256db2661ceSRobert Watson }
257db2661ceSRobert Watson 
258db2661ceSRobert Watson static int
lomac_subject_dominate(struct mac_lomac * a,struct mac_lomac * b)2593f1a7a90SRobert Watson lomac_subject_dominate(struct mac_lomac *a, struct mac_lomac *b)
260db2661ceSRobert Watson {
261db2661ceSRobert Watson 	KASSERT((~a->ml_flags &
262db2661ceSRobert Watson 	    (MAC_LOMAC_FLAG_SINGLE | MAC_LOMAC_FLAG_RANGE)) == 0,
2633f1a7a90SRobert Watson 	    ("lomac_dominate_single: a not subject"));
264db2661ceSRobert Watson 	KASSERT((b->ml_flags & MAC_LOMAC_FLAG_SINGLE) != 0,
2653f1a7a90SRobert Watson 	    ("lomac_dominate_single: b not single"));
266db2661ceSRobert Watson 
2673f1a7a90SRobert Watson 	return (lomac_dominate_element(&a->ml_rangehigh, &b->ml_single));
268db2661ceSRobert Watson }
269db2661ceSRobert Watson 
270db2661ceSRobert Watson static int
lomac_equal_element(struct mac_lomac_element * a,struct mac_lomac_element * b)2713f1a7a90SRobert Watson lomac_equal_element(struct mac_lomac_element *a, struct mac_lomac_element *b)
272db2661ceSRobert Watson {
273db2661ceSRobert Watson 
274db2661ceSRobert Watson 	if (a->mle_type == MAC_LOMAC_TYPE_EQUAL ||
275db2661ceSRobert Watson 	    b->mle_type == MAC_LOMAC_TYPE_EQUAL)
276db2661ceSRobert Watson 		return (1);
277db2661ceSRobert Watson 
278db2661ceSRobert Watson 	return (a->mle_type == b->mle_type && a->mle_grade == b->mle_grade);
279db2661ceSRobert Watson }
280db2661ceSRobert Watson 
281db2661ceSRobert Watson static int
lomac_equal_single(struct mac_lomac * a,struct mac_lomac * b)2823f1a7a90SRobert Watson lomac_equal_single(struct mac_lomac *a, struct mac_lomac *b)
283db2661ceSRobert Watson {
284db2661ceSRobert Watson 
285db2661ceSRobert Watson 	KASSERT((a->ml_flags & MAC_LOMAC_FLAG_SINGLE) != 0,
2863f1a7a90SRobert Watson 	    ("lomac_equal_single: a not single"));
287db2661ceSRobert Watson 	KASSERT((b->ml_flags & MAC_LOMAC_FLAG_SINGLE) != 0,
2883f1a7a90SRobert Watson 	    ("lomac_equal_single: b not single"));
289db2661ceSRobert Watson 
2903f1a7a90SRobert Watson 	return (lomac_equal_element(&a->ml_single, &b->ml_single));
291db2661ceSRobert Watson }
292db2661ceSRobert Watson 
293db2661ceSRobert Watson static int
lomac_contains_equal(struct mac_lomac * ml)2943f1a7a90SRobert Watson lomac_contains_equal(struct mac_lomac *ml)
295db2661ceSRobert Watson {
296db2661ceSRobert Watson 
2973f1a7a90SRobert Watson 	if (ml->ml_flags & MAC_LOMAC_FLAG_SINGLE)
2983f1a7a90SRobert Watson 		if (ml->ml_single.mle_type == MAC_LOMAC_TYPE_EQUAL)
299db2661ceSRobert Watson 			return (1);
3003f1a7a90SRobert Watson 	if (ml->ml_flags & MAC_LOMAC_FLAG_AUX)
3013f1a7a90SRobert Watson 		if (ml->ml_auxsingle.mle_type == MAC_LOMAC_TYPE_EQUAL)
302db2661ceSRobert Watson 			return (1);
303db2661ceSRobert Watson 
3043f1a7a90SRobert Watson 	if (ml->ml_flags & MAC_LOMAC_FLAG_RANGE) {
3053f1a7a90SRobert Watson 		if (ml->ml_rangelow.mle_type == MAC_LOMAC_TYPE_EQUAL)
306db2661ceSRobert Watson 			return (1);
3073f1a7a90SRobert Watson 		if (ml->ml_rangehigh.mle_type == MAC_LOMAC_TYPE_EQUAL)
308db2661ceSRobert Watson 			return (1);
309db2661ceSRobert Watson 	}
310db2661ceSRobert Watson 
311db2661ceSRobert Watson 	return (0);
312db2661ceSRobert Watson }
313db2661ceSRobert Watson 
314db2661ceSRobert Watson static int
lomac_subject_privileged(struct mac_lomac * ml)3153f1a7a90SRobert Watson lomac_subject_privileged(struct mac_lomac *ml)
316db2661ceSRobert Watson {
317db2661ceSRobert Watson 
3183f1a7a90SRobert Watson 	KASSERT((ml->ml_flags & MAC_LOMAC_FLAGS_BOTH) ==
319db2661ceSRobert Watson 	    MAC_LOMAC_FLAGS_BOTH,
3203f1a7a90SRobert Watson 	    ("lomac_subject_privileged: subject doesn't have both labels"));
321db2661ceSRobert Watson 
322db2661ceSRobert Watson 	/* If the single is EQUAL, it's ok. */
3233f1a7a90SRobert Watson 	if (ml->ml_single.mle_type == MAC_LOMAC_TYPE_EQUAL)
324db2661ceSRobert Watson 		return (0);
325db2661ceSRobert Watson 
326db2661ceSRobert Watson 	/* If either range endpoint is EQUAL, it's ok. */
3273f1a7a90SRobert Watson 	if (ml->ml_rangelow.mle_type == MAC_LOMAC_TYPE_EQUAL ||
3283f1a7a90SRobert Watson 	    ml->ml_rangehigh.mle_type == MAC_LOMAC_TYPE_EQUAL)
329db2661ceSRobert Watson 		return (0);
330db2661ceSRobert Watson 
331db2661ceSRobert Watson 	/* If the range is low-high, it's ok. */
3323f1a7a90SRobert Watson 	if (ml->ml_rangelow.mle_type == MAC_LOMAC_TYPE_LOW &&
3333f1a7a90SRobert Watson 	    ml->ml_rangehigh.mle_type == MAC_LOMAC_TYPE_HIGH)
334db2661ceSRobert Watson 		return (0);
335db2661ceSRobert Watson 
336db2661ceSRobert Watson 	/* It's not ok. */
337db2661ceSRobert Watson 	return (EPERM);
338db2661ceSRobert Watson }
339db2661ceSRobert Watson 
340db2661ceSRobert Watson static int
lomac_high_single(struct mac_lomac * ml)3413f1a7a90SRobert Watson lomac_high_single(struct mac_lomac *ml)
342db2661ceSRobert Watson {
343db2661ceSRobert Watson 
3443f1a7a90SRobert Watson 	KASSERT((ml->ml_flags & MAC_LOMAC_FLAG_SINGLE) != 0,
3453f1a7a90SRobert Watson 	    ("lomac_high_single: mac_lomac not single"));
346db2661ceSRobert Watson 
3473f1a7a90SRobert Watson 	return (ml->ml_single.mle_type == MAC_LOMAC_TYPE_HIGH);
348db2661ceSRobert Watson }
349db2661ceSRobert Watson 
350db2661ceSRobert Watson static int
lomac_valid(struct mac_lomac * ml)3513f1a7a90SRobert Watson lomac_valid(struct mac_lomac *ml)
352db2661ceSRobert Watson {
353db2661ceSRobert Watson 
3543f1a7a90SRobert Watson 	if (ml->ml_flags & MAC_LOMAC_FLAG_SINGLE) {
3553f1a7a90SRobert Watson 		switch (ml->ml_single.mle_type) {
356db2661ceSRobert Watson 		case MAC_LOMAC_TYPE_GRADE:
357db2661ceSRobert Watson 		case MAC_LOMAC_TYPE_EQUAL:
358db2661ceSRobert Watson 		case MAC_LOMAC_TYPE_HIGH:
359db2661ceSRobert Watson 		case MAC_LOMAC_TYPE_LOW:
360db2661ceSRobert Watson 			break;
361db2661ceSRobert Watson 
362db2661ceSRobert Watson 		default:
363db2661ceSRobert Watson 			return (EINVAL);
364db2661ceSRobert Watson 		}
365db2661ceSRobert Watson 	} else {
3663f1a7a90SRobert Watson 		if (ml->ml_single.mle_type != MAC_LOMAC_TYPE_UNDEF)
367db2661ceSRobert Watson 			return (EINVAL);
368db2661ceSRobert Watson 	}
369db2661ceSRobert Watson 
3703f1a7a90SRobert Watson 	if (ml->ml_flags & MAC_LOMAC_FLAG_AUX) {
3713f1a7a90SRobert Watson 		switch (ml->ml_auxsingle.mle_type) {
372db2661ceSRobert Watson 		case MAC_LOMAC_TYPE_GRADE:
373db2661ceSRobert Watson 		case MAC_LOMAC_TYPE_EQUAL:
374db2661ceSRobert Watson 		case MAC_LOMAC_TYPE_HIGH:
375db2661ceSRobert Watson 		case MAC_LOMAC_TYPE_LOW:
376db2661ceSRobert Watson 			break;
377db2661ceSRobert Watson 
378db2661ceSRobert Watson 		default:
379db2661ceSRobert Watson 			return (EINVAL);
380db2661ceSRobert Watson 		}
381db2661ceSRobert Watson 	} else {
3823f1a7a90SRobert Watson 		if (ml->ml_auxsingle.mle_type != MAC_LOMAC_TYPE_UNDEF)
383db2661ceSRobert Watson 			return (EINVAL);
384db2661ceSRobert Watson 	}
385db2661ceSRobert Watson 
3863f1a7a90SRobert Watson 	if (ml->ml_flags & MAC_LOMAC_FLAG_RANGE) {
3873f1a7a90SRobert Watson 		switch (ml->ml_rangelow.mle_type) {
388db2661ceSRobert Watson 		case MAC_LOMAC_TYPE_GRADE:
389db2661ceSRobert Watson 		case MAC_LOMAC_TYPE_EQUAL:
390db2661ceSRobert Watson 		case MAC_LOMAC_TYPE_HIGH:
391db2661ceSRobert Watson 		case MAC_LOMAC_TYPE_LOW:
392db2661ceSRobert Watson 			break;
393db2661ceSRobert Watson 
394db2661ceSRobert Watson 		default:
395db2661ceSRobert Watson 			return (EINVAL);
396db2661ceSRobert Watson 		}
397db2661ceSRobert Watson 
3983f1a7a90SRobert Watson 		switch (ml->ml_rangehigh.mle_type) {
399db2661ceSRobert Watson 		case MAC_LOMAC_TYPE_GRADE:
400db2661ceSRobert Watson 		case MAC_LOMAC_TYPE_EQUAL:
401db2661ceSRobert Watson 		case MAC_LOMAC_TYPE_HIGH:
402db2661ceSRobert Watson 		case MAC_LOMAC_TYPE_LOW:
403db2661ceSRobert Watson 			break;
404db2661ceSRobert Watson 
405db2661ceSRobert Watson 		default:
406db2661ceSRobert Watson 			return (EINVAL);
407db2661ceSRobert Watson 		}
4083f1a7a90SRobert Watson 		if (!lomac_dominate_element(&ml->ml_rangehigh,
4093f1a7a90SRobert Watson 		    &ml->ml_rangelow))
410db2661ceSRobert Watson 			return (EINVAL);
411db2661ceSRobert Watson 	} else {
4123f1a7a90SRobert Watson 		if (ml->ml_rangelow.mle_type != MAC_LOMAC_TYPE_UNDEF ||
4133f1a7a90SRobert Watson 		    ml->ml_rangehigh.mle_type != MAC_LOMAC_TYPE_UNDEF)
414db2661ceSRobert Watson 			return (EINVAL);
415db2661ceSRobert Watson 	}
416db2661ceSRobert Watson 
417db2661ceSRobert Watson 	return (0);
418db2661ceSRobert Watson }
419db2661ceSRobert Watson 
420db2661ceSRobert Watson static void
lomac_set_range(struct mac_lomac * ml,u_short typelow,u_short gradelow,u_short typehigh,u_short gradehigh)4213f1a7a90SRobert Watson lomac_set_range(struct mac_lomac *ml, u_short typelow, u_short gradelow,
4223f1a7a90SRobert Watson     u_short typehigh, u_short gradehigh)
423db2661ceSRobert Watson {
424db2661ceSRobert Watson 
4253f1a7a90SRobert Watson 	ml->ml_rangelow.mle_type = typelow;
4263f1a7a90SRobert Watson 	ml->ml_rangelow.mle_grade = gradelow;
4273f1a7a90SRobert Watson 	ml->ml_rangehigh.mle_type = typehigh;
4283f1a7a90SRobert Watson 	ml->ml_rangehigh.mle_grade = gradehigh;
4293f1a7a90SRobert Watson 	ml->ml_flags |= MAC_LOMAC_FLAG_RANGE;
430db2661ceSRobert Watson }
431db2661ceSRobert Watson 
432db2661ceSRobert Watson static void
lomac_set_single(struct mac_lomac * ml,u_short type,u_short grade)4333f1a7a90SRobert Watson lomac_set_single(struct mac_lomac *ml, u_short type, u_short grade)
434db2661ceSRobert Watson {
435db2661ceSRobert Watson 
4363f1a7a90SRobert Watson 	ml->ml_single.mle_type = type;
4373f1a7a90SRobert Watson 	ml->ml_single.mle_grade = grade;
4383f1a7a90SRobert Watson 	ml->ml_flags |= MAC_LOMAC_FLAG_SINGLE;
439db2661ceSRobert Watson }
440db2661ceSRobert Watson 
441db2661ceSRobert Watson static void
lomac_copy_range(struct mac_lomac * labelfrom,struct mac_lomac * labelto)4423f1a7a90SRobert Watson lomac_copy_range(struct mac_lomac *labelfrom, struct mac_lomac *labelto)
443db2661ceSRobert Watson {
444db2661ceSRobert Watson 
445db2661ceSRobert Watson 	KASSERT((labelfrom->ml_flags & MAC_LOMAC_FLAG_RANGE) != 0,
4463f1a7a90SRobert Watson 	    ("lomac_copy_range: labelfrom not range"));
447db2661ceSRobert Watson 
448db2661ceSRobert Watson 	labelto->ml_rangelow = labelfrom->ml_rangelow;
449db2661ceSRobert Watson 	labelto->ml_rangehigh = labelfrom->ml_rangehigh;
450db2661ceSRobert Watson 	labelto->ml_flags |= MAC_LOMAC_FLAG_RANGE;
451db2661ceSRobert Watson }
452db2661ceSRobert Watson 
453db2661ceSRobert Watson static void
lomac_copy_single(struct mac_lomac * labelfrom,struct mac_lomac * labelto)4543f1a7a90SRobert Watson lomac_copy_single(struct mac_lomac *labelfrom, struct mac_lomac *labelto)
455db2661ceSRobert Watson {
456db2661ceSRobert Watson 
457db2661ceSRobert Watson 	KASSERT((labelfrom->ml_flags & MAC_LOMAC_FLAG_SINGLE) != 0,
4583f1a7a90SRobert Watson 	    ("lomac_copy_single: labelfrom not single"));
459db2661ceSRobert Watson 
460db2661ceSRobert Watson 	labelto->ml_single = labelfrom->ml_single;
461db2661ceSRobert Watson 	labelto->ml_flags |= MAC_LOMAC_FLAG_SINGLE;
462db2661ceSRobert Watson }
463db2661ceSRobert Watson 
464db2661ceSRobert Watson static void
lomac_copy_auxsingle(struct mac_lomac * labelfrom,struct mac_lomac * labelto)4653f1a7a90SRobert Watson lomac_copy_auxsingle(struct mac_lomac *labelfrom, struct mac_lomac *labelto)
466db2661ceSRobert Watson {
467db2661ceSRobert Watson 
468db2661ceSRobert Watson 	KASSERT((labelfrom->ml_flags & MAC_LOMAC_FLAG_AUX) != 0,
4693f1a7a90SRobert Watson 	    ("lomac_copy_auxsingle: labelfrom not auxsingle"));
470db2661ceSRobert Watson 
471db2661ceSRobert Watson 	labelto->ml_auxsingle = labelfrom->ml_auxsingle;
472db2661ceSRobert Watson 	labelto->ml_flags |= MAC_LOMAC_FLAG_AUX;
473db2661ceSRobert Watson }
474db2661ceSRobert Watson 
475db2661ceSRobert Watson static void
lomac_copy(struct mac_lomac * source,struct mac_lomac * dest)4763f1a7a90SRobert Watson lomac_copy(struct mac_lomac *source, struct mac_lomac *dest)
477db2661ceSRobert Watson {
478db2661ceSRobert Watson 
479db2661ceSRobert Watson 	if (source->ml_flags & MAC_LOMAC_FLAG_SINGLE)
4803f1a7a90SRobert Watson 		lomac_copy_single(source, dest);
481db2661ceSRobert Watson 	if (source->ml_flags & MAC_LOMAC_FLAG_AUX)
4823f1a7a90SRobert Watson 		lomac_copy_auxsingle(source, dest);
483db2661ceSRobert Watson 	if (source->ml_flags & MAC_LOMAC_FLAG_RANGE)
4843f1a7a90SRobert Watson 		lomac_copy_range(source, dest);
485db2661ceSRobert Watson }
486db2661ceSRobert Watson 
4873f1a7a90SRobert Watson static int	lomac_to_string(struct sbuf *sb, struct mac_lomac *ml);
488db2661ceSRobert Watson 
489db2661ceSRobert Watson static int
maybe_demote(struct mac_lomac * subjlabel,struct mac_lomac * objlabel,const char * actionname,const char * objname,struct vnode * vp)490db2661ceSRobert Watson maybe_demote(struct mac_lomac *subjlabel, struct mac_lomac *objlabel,
49178007886SRobert Watson     const char *actionname, const char *objname, struct vnode *vp)
492db2661ceSRobert Watson {
493f51e5803SRobert Watson 	struct sbuf subjlabel_sb, subjtext_sb, objlabel_sb;
494f51e5803SRobert Watson 	char *subjlabeltext, *objlabeltext, *subjtext;
495f51e5803SRobert Watson 	struct mac_lomac cached_subjlabel;
496f51e5803SRobert Watson 	struct mac_lomac_proc *subj;
497db2661ceSRobert Watson 	struct vattr va;
498db2661ceSRobert Watson 	struct proc *p;
499db2661ceSRobert Watson 	pid_t pgid;
500db2661ceSRobert Watson 
501eca8a663SRobert Watson 	subj = PSLOT(curthread->td_proc->p_label);
502f51e5803SRobert Watson 
503db2661ceSRobert Watson 	p = curthread->td_proc;
504db2661ceSRobert Watson 	mtx_lock(&subj->mtx);
505db2661ceSRobert Watson         if (subj->mac_lomac.ml_flags & MAC_LOMAC_FLAG_UPDATE) {
506db2661ceSRobert Watson 		/*
5073f1a7a90SRobert Watson 		 * Check to see if the pending demotion would be more or less
5083f1a7a90SRobert Watson 		 * severe than this one, and keep the more severe.  This can
5093f1a7a90SRobert Watson 		 * only happen for a multi-threaded application.
510db2661ceSRobert Watson 		 */
5113f1a7a90SRobert Watson 		if (lomac_dominate_single(objlabel, &subj->mac_lomac)) {
512db2661ceSRobert Watson 			mtx_unlock(&subj->mtx);
513db2661ceSRobert Watson 			return (0);
514db2661ceSRobert Watson 		}
515db2661ceSRobert Watson 	}
516db2661ceSRobert Watson 	bzero(&subj->mac_lomac, sizeof(subj->mac_lomac));
517db2661ceSRobert Watson 	/*
518db2661ceSRobert Watson 	 * Always demote the single label.
519db2661ceSRobert Watson 	 */
5203f1a7a90SRobert Watson 	lomac_copy_single(objlabel, &subj->mac_lomac);
521db2661ceSRobert Watson 	/*
5223f1a7a90SRobert Watson 	 * Start with the original range, then minimize each side of the
5233f1a7a90SRobert Watson 	 * range to the point of not dominating the object.  The high side
5243f1a7a90SRobert Watson 	 * will always be demoted, of course.
525db2661ceSRobert Watson 	 */
5263f1a7a90SRobert Watson 	lomac_copy_range(subjlabel, &subj->mac_lomac);
5273f1a7a90SRobert Watson 	if (!lomac_dominate_element(&objlabel->ml_single,
528db2661ceSRobert Watson 	    &subj->mac_lomac.ml_rangelow))
529db2661ceSRobert Watson 		subj->mac_lomac.ml_rangelow = objlabel->ml_single;
530db2661ceSRobert Watson 	subj->mac_lomac.ml_rangehigh = objlabel->ml_single;
531db2661ceSRobert Watson 	subj->mac_lomac.ml_flags |= MAC_LOMAC_FLAG_UPDATE;
532c6d31b83SKonstantin Belousov 	ast_sched(curthread, TDA_MAC);
533f51e5803SRobert Watson 
534f51e5803SRobert Watson 	/*
5353f1a7a90SRobert Watson 	 * Avoid memory allocation while holding a mutex; cache the label.
536f51e5803SRobert Watson 	 */
5373f1a7a90SRobert Watson 	lomac_copy_single(&subj->mac_lomac, &cached_subjlabel);
538db2661ceSRobert Watson 	mtx_unlock(&subj->mtx);
539f51e5803SRobert Watson 
540f51e5803SRobert Watson 	sbuf_new(&subjlabel_sb, NULL, 0, SBUF_AUTOEXTEND);
5413f1a7a90SRobert Watson 	lomac_to_string(&subjlabel_sb, subjlabel);
542f51e5803SRobert Watson 	sbuf_finish(&subjlabel_sb);
543f51e5803SRobert Watson 	subjlabeltext = sbuf_data(&subjlabel_sb);
544f51e5803SRobert Watson 
545f51e5803SRobert Watson 	sbuf_new(&subjtext_sb, NULL, 0, SBUF_AUTOEXTEND);
5463f1a7a90SRobert Watson 	lomac_to_string(&subjtext_sb, &subj->mac_lomac);
547f51e5803SRobert Watson 	sbuf_finish(&subjtext_sb);
548f51e5803SRobert Watson 	subjtext = sbuf_data(&subjtext_sb);
549f51e5803SRobert Watson 
550f51e5803SRobert Watson 	sbuf_new(&objlabel_sb, NULL, 0, SBUF_AUTOEXTEND);
5513f1a7a90SRobert Watson 	lomac_to_string(&objlabel_sb, objlabel);
552f51e5803SRobert Watson 	sbuf_finish(&objlabel_sb);
553f51e5803SRobert Watson 	objlabeltext = sbuf_data(&objlabel_sb);
554f51e5803SRobert Watson 
555db2661ceSRobert Watson 	pgid = p->p_pgrp->pg_id;		/* XXX could be stale? */
5560359a12eSAttilio Rao 	if (vp != NULL && VOP_GETATTR(vp, &va, curthread->td_ucred) == 0) {
557db2661ceSRobert Watson 		log(LOG_INFO, "LOMAC: level-%s subject p%dg%du%d:%s demoted to"
558dde58752SGleb Kurtsou 		    " level %s after %s a level-%s %s (inode=%ju, "
559db2661ceSRobert Watson 		    "mountpount=%s)\n",
560db2661ceSRobert Watson 		    subjlabeltext, p->p_pid, pgid, curthread->td_ucred->cr_uid,
561db2661ceSRobert Watson 		    p->p_comm, subjtext, actionname, objlabeltext, objname,
562dde58752SGleb Kurtsou 		    (uintmax_t)va.va_fileid, vp->v_mount->mnt_stat.f_mntonname);
563db2661ceSRobert Watson 	} else {
564db2661ceSRobert Watson 		log(LOG_INFO, "LOMAC: level-%s subject p%dg%du%d:%s demoted to"
565db2661ceSRobert Watson 		    " level %s after %s a level-%s %s\n",
566db2661ceSRobert Watson 		    subjlabeltext, p->p_pid, pgid, curthread->td_ucred->cr_uid,
567db2661ceSRobert Watson 		    p->p_comm, subjtext, actionname, objlabeltext, objname);
568db2661ceSRobert Watson 	}
569db2661ceSRobert Watson 
570f51e5803SRobert Watson 	sbuf_delete(&subjlabel_sb);
571f51e5803SRobert Watson 	sbuf_delete(&subjtext_sb);
572f51e5803SRobert Watson 	sbuf_delete(&objlabel_sb);
573f51e5803SRobert Watson 
574db2661ceSRobert Watson 	return (0);
575db2661ceSRobert Watson }
576db2661ceSRobert Watson 
577db2661ceSRobert Watson /*
5783f1a7a90SRobert Watson  * Relabel "to" to "from" only if "from" is a valid label (contains at least
5793f1a7a90SRobert Watson  * a single), as for a relabel operation which may or may not involve a
5803f1a7a90SRobert Watson  * relevant label.
581db2661ceSRobert Watson  */
5827496ed81SRobert Watson static void
try_relabel(struct mac_lomac * from,struct mac_lomac * to)583db2661ceSRobert Watson try_relabel(struct mac_lomac *from, struct mac_lomac *to)
584db2661ceSRobert Watson {
585db2661ceSRobert Watson 
586db2661ceSRobert Watson 	if (from->ml_flags & MAC_LOMAC_FLAG_SINGLE) {
587db2661ceSRobert Watson 		bzero(to, sizeof(*to));
5883f1a7a90SRobert Watson 		lomac_copy(from, to);
589db2661ceSRobert Watson 	}
590db2661ceSRobert Watson }
591db2661ceSRobert Watson 
592c6d31b83SKonstantin Belousov static void
ast_mac(struct thread * td,int tda __unused)593c6d31b83SKonstantin Belousov ast_mac(struct thread *td, int tda __unused)
594c6d31b83SKonstantin Belousov {
595c6d31b83SKonstantin Belousov 	mac_thread_userret(td);
596c6d31b83SKonstantin Belousov }
597c6d31b83SKonstantin Belousov 
598db2661ceSRobert Watson /*
599db2661ceSRobert Watson  * Policy module operations.
600db2661ceSRobert Watson  */
601db2661ceSRobert Watson static void
lomac_init(struct mac_policy_conf * conf __unused)602c6d31b83SKonstantin Belousov lomac_init(struct mac_policy_conf *conf __unused)
603db2661ceSRobert Watson {
604c6d31b83SKonstantin Belousov 	ast_register(TDA_MAC, ASTR_ASTF_REQUIRED, 0, ast_mac);
605c6d31b83SKonstantin Belousov }
606db2661ceSRobert Watson 
607c6d31b83SKonstantin Belousov static void
lomac_fini(struct mac_policy_conf * conf __unused)608c6d31b83SKonstantin Belousov lomac_fini(struct mac_policy_conf *conf __unused)
609c6d31b83SKonstantin Belousov {
610c6d31b83SKonstantin Belousov 	ast_deregister(TDA_MAC);
611db2661ceSRobert Watson }
612db2661ceSRobert Watson 
613db2661ceSRobert Watson /*
614db2661ceSRobert Watson  * Label operations.
615db2661ceSRobert Watson  */
616db2661ceSRobert Watson static void
lomac_init_label(struct label * label)6173f1a7a90SRobert Watson lomac_init_label(struct label *label)
618db2661ceSRobert Watson {
619db2661ceSRobert Watson 
6201477f588SAlexander Kabaev 	SLOT_SET(label, lomac_alloc(M_WAITOK));
621db2661ceSRobert Watson }
622db2661ceSRobert Watson 
623db2661ceSRobert Watson static int
lomac_init_label_waitcheck(struct label * label,int flag)6243f1a7a90SRobert Watson lomac_init_label_waitcheck(struct label *label, int flag)
625db2661ceSRobert Watson {
626db2661ceSRobert Watson 
6271477f588SAlexander Kabaev 	SLOT_SET(label, lomac_alloc(flag));
628db2661ceSRobert Watson 	if (SLOT(label) == NULL)
629db2661ceSRobert Watson 		return (ENOMEM);
630db2661ceSRobert Watson 
631db2661ceSRobert Watson 	return (0);
632db2661ceSRobert Watson }
633db2661ceSRobert Watson 
634db2661ceSRobert Watson static void
lomac_destroy_label(struct label * label)6353f1a7a90SRobert Watson lomac_destroy_label(struct label *label)
636db2661ceSRobert Watson {
637db2661ceSRobert Watson 
638db2661ceSRobert Watson 	lomac_free(SLOT(label));
6391477f588SAlexander Kabaev 	SLOT_SET(label, NULL);
640db2661ceSRobert Watson }
641db2661ceSRobert Watson 
642f51e5803SRobert Watson static int
lomac_element_to_string(struct sbuf * sb,struct mac_lomac_element * element)6433f1a7a90SRobert Watson lomac_element_to_string(struct sbuf *sb, struct mac_lomac_element *element)
644db2661ceSRobert Watson {
645db2661ceSRobert Watson 
646db2661ceSRobert Watson 	switch (element->mle_type) {
647db2661ceSRobert Watson 	case MAC_LOMAC_TYPE_HIGH:
648f51e5803SRobert Watson 		return (sbuf_printf(sb, "high"));
649db2661ceSRobert Watson 
650db2661ceSRobert Watson 	case MAC_LOMAC_TYPE_LOW:
651f51e5803SRobert Watson 		return (sbuf_printf(sb, "low"));
652db2661ceSRobert Watson 
653db2661ceSRobert Watson 	case MAC_LOMAC_TYPE_EQUAL:
654f51e5803SRobert Watson 		return (sbuf_printf(sb, "equal"));
655db2661ceSRobert Watson 
656db2661ceSRobert Watson 	case MAC_LOMAC_TYPE_GRADE:
657f51e5803SRobert Watson 		return (sbuf_printf(sb, "%d", element->mle_grade));
658db2661ceSRobert Watson 
659db2661ceSRobert Watson 	default:
6603f1a7a90SRobert Watson 		panic("lomac_element_to_string: invalid type (%d)",
661db2661ceSRobert Watson 		    element->mle_type);
662db2661ceSRobert Watson 	}
663db2661ceSRobert Watson }
664db2661ceSRobert Watson 
665db2661ceSRobert Watson static int
lomac_to_string(struct sbuf * sb,struct mac_lomac * ml)6663f1a7a90SRobert Watson lomac_to_string(struct sbuf *sb, struct mac_lomac *ml)
667db2661ceSRobert Watson {
668db2661ceSRobert Watson 
6693f1a7a90SRobert Watson 	if (ml->ml_flags & MAC_LOMAC_FLAG_SINGLE) {
6703f1a7a90SRobert Watson 		if (lomac_element_to_string(sb, &ml->ml_single) == -1)
671f51e5803SRobert Watson 			return (EINVAL);
672db2661ceSRobert Watson 	}
673db2661ceSRobert Watson 
6743f1a7a90SRobert Watson 	if (ml->ml_flags & MAC_LOMAC_FLAG_AUX) {
675f51e5803SRobert Watson 		if (sbuf_putc(sb, '[') == -1)
676f51e5803SRobert Watson 			return (EINVAL);
677db2661ceSRobert Watson 
6783f1a7a90SRobert Watson 		if (lomac_element_to_string(sb, &ml->ml_auxsingle) == -1)
679f51e5803SRobert Watson 			return (EINVAL);
680db2661ceSRobert Watson 
681f51e5803SRobert Watson 		if (sbuf_putc(sb, ']') == -1)
682f51e5803SRobert Watson 			return (EINVAL);
683db2661ceSRobert Watson 	}
684db2661ceSRobert Watson 
6853f1a7a90SRobert Watson 	if (ml->ml_flags & MAC_LOMAC_FLAG_RANGE) {
686f51e5803SRobert Watson 		if (sbuf_putc(sb, '(') == -1)
687f51e5803SRobert Watson 			return (EINVAL);
688db2661ceSRobert Watson 
6893f1a7a90SRobert Watson 		if (lomac_element_to_string(sb, &ml->ml_rangelow) == -1)
690f51e5803SRobert Watson 			return (EINVAL);
691db2661ceSRobert Watson 
692f51e5803SRobert Watson 		if (sbuf_putc(sb, '-') == -1)
693f51e5803SRobert Watson 			return (EINVAL);
694db2661ceSRobert Watson 
6953f1a7a90SRobert Watson 		if (lomac_element_to_string(sb, &ml->ml_rangehigh) == -1)
696f51e5803SRobert Watson 			return (EINVAL);
697db2661ceSRobert Watson 
6988a4b86b9SRobert Watson 		if (sbuf_putc(sb, ')') == -1)
699f51e5803SRobert Watson 			return (EINVAL);
700db2661ceSRobert Watson 	}
701db2661ceSRobert Watson 
702db2661ceSRobert Watson 	return (0);
703db2661ceSRobert Watson }
704db2661ceSRobert Watson 
705db2661ceSRobert Watson static int
lomac_externalize_label(struct label * label,char * element_name,struct sbuf * sb,int * claimed)7063f1a7a90SRobert Watson lomac_externalize_label(struct label *label, char *element_name,
707f51e5803SRobert Watson     struct sbuf *sb, int *claimed)
708db2661ceSRobert Watson {
7093f1a7a90SRobert Watson 	struct mac_lomac *ml;
710db2661ceSRobert Watson 
711db2661ceSRobert Watson 	if (strcmp(MAC_LOMAC_LABEL_NAME, element_name) != 0)
712db2661ceSRobert Watson 		return (0);
713db2661ceSRobert Watson 
714db2661ceSRobert Watson 	(*claimed)++;
715db2661ceSRobert Watson 
7163f1a7a90SRobert Watson 	ml = SLOT(label);
717db2661ceSRobert Watson 
7183f1a7a90SRobert Watson 	return (lomac_to_string(sb, ml));
719db2661ceSRobert Watson }
720db2661ceSRobert Watson 
721db2661ceSRobert Watson static int
lomac_parse_element(struct mac_lomac_element * element,char * string)7223f1a7a90SRobert Watson lomac_parse_element(struct mac_lomac_element *element, char *string)
723db2661ceSRobert Watson {
724db2661ceSRobert Watson 
7253f1a7a90SRobert Watson 	if (strcmp(string, "high") == 0 || strcmp(string, "hi") == 0) {
726db2661ceSRobert Watson 		element->mle_type = MAC_LOMAC_TYPE_HIGH;
727db2661ceSRobert Watson 		element->mle_grade = MAC_LOMAC_TYPE_UNDEF;
7283f1a7a90SRobert Watson 	} else if (strcmp(string, "low") == 0 || strcmp(string, "lo") == 0) {
729db2661ceSRobert Watson 		element->mle_type = MAC_LOMAC_TYPE_LOW;
730db2661ceSRobert Watson 		element->mle_grade = MAC_LOMAC_TYPE_UNDEF;
73195b85ca3SRobert Watson 	} else if (strcmp(string, "equal") == 0 ||
73295b85ca3SRobert Watson 	    strcmp(string, "eq") == 0) {
733db2661ceSRobert Watson 		element->mle_type = MAC_LOMAC_TYPE_EQUAL;
734db2661ceSRobert Watson 		element->mle_grade = MAC_LOMAC_TYPE_UNDEF;
735db2661ceSRobert Watson 	} else {
736db2661ceSRobert Watson 		char *p0, *p1;
737db2661ceSRobert Watson 		int d;
738db2661ceSRobert Watson 
739db2661ceSRobert Watson 		p0 = string;
740db2661ceSRobert Watson 		d = strtol(p0, &p1, 10);
741db2661ceSRobert Watson 
742db2661ceSRobert Watson 		if (d < 0 || d > 65535)
743db2661ceSRobert Watson 			return (EINVAL);
744db2661ceSRobert Watson 		element->mle_type = MAC_LOMAC_TYPE_GRADE;
745db2661ceSRobert Watson 		element->mle_grade = d;
746db2661ceSRobert Watson 
747db2661ceSRobert Watson 		if (p1 == p0 || *p1 != '\0')
748db2661ceSRobert Watson 			return (EINVAL);
749db2661ceSRobert Watson 	}
750db2661ceSRobert Watson 
751db2661ceSRobert Watson 	return (0);
752db2661ceSRobert Watson }
753db2661ceSRobert Watson 
754db2661ceSRobert Watson /*
7553f1a7a90SRobert Watson  * Note: destructively consumes the string, make a local copy before calling
7563f1a7a90SRobert Watson  * if that's a problem.
757db2661ceSRobert Watson  */
758db2661ceSRobert Watson static int
lomac_parse(struct mac_lomac * ml,char * string)7593f1a7a90SRobert Watson lomac_parse(struct mac_lomac *ml, char *string)
760db2661ceSRobert Watson {
761db2661ceSRobert Watson 	char *range, *rangeend, *rangehigh, *rangelow, *single, *auxsingle,
762db2661ceSRobert Watson 	    *auxsingleend;
763db2661ceSRobert Watson 	int error;
764db2661ceSRobert Watson 
765db2661ceSRobert Watson 	/* Do we have a range? */
766db2661ceSRobert Watson 	single = string;
767dc15eac0SEd Schouten 	range = strchr(string, '(');
768db2661ceSRobert Watson 	if (range == single)
769db2661ceSRobert Watson 		single = NULL;
770dc15eac0SEd Schouten 	auxsingle = strchr(string, '[');
771db2661ceSRobert Watson 	if (auxsingle == single)
772db2661ceSRobert Watson 		single = NULL;
773db2661ceSRobert Watson 	if (range != NULL && auxsingle != NULL)
774db2661ceSRobert Watson 		return (EINVAL);
775db2661ceSRobert Watson 	rangelow = rangehigh = NULL;
776db2661ceSRobert Watson 	if (range != NULL) {
777db2661ceSRobert Watson 		/* Nul terminate the end of the single string. */
778db2661ceSRobert Watson 		*range = '\0';
779db2661ceSRobert Watson 		range++;
780db2661ceSRobert Watson 		rangelow = range;
781dc15eac0SEd Schouten 		rangehigh = strchr(rangelow, '-');
782db2661ceSRobert Watson 		if (rangehigh == NULL)
783db2661ceSRobert Watson 			return (EINVAL);
784db2661ceSRobert Watson 		rangehigh++;
785db2661ceSRobert Watson 		if (*rangelow == '\0' || *rangehigh == '\0')
786db2661ceSRobert Watson 			return (EINVAL);
787dc15eac0SEd Schouten 		rangeend = strchr(rangehigh, ')');
788db2661ceSRobert Watson 		if (rangeend == NULL)
789db2661ceSRobert Watson 			return (EINVAL);
790db2661ceSRobert Watson 		if (*(rangeend + 1) != '\0')
791db2661ceSRobert Watson 			return (EINVAL);
792db2661ceSRobert Watson 		/* Nul terminate the ends of the ranges. */
793db2661ceSRobert Watson 		*(rangehigh - 1) = '\0';
794db2661ceSRobert Watson 		*rangeend = '\0';
795db2661ceSRobert Watson 	}
796db2661ceSRobert Watson 	KASSERT((rangelow != NULL && rangehigh != NULL) ||
797db2661ceSRobert Watson 	    (rangelow == NULL && rangehigh == NULL),
7983f1a7a90SRobert Watson 	    ("lomac_internalize_label: range mismatch"));
799db2661ceSRobert Watson 	if (auxsingle != NULL) {
800db2661ceSRobert Watson 		/* Nul terminate the end of the single string. */
801db2661ceSRobert Watson 		*auxsingle = '\0';
802db2661ceSRobert Watson 		auxsingle++;
803dc15eac0SEd Schouten 		auxsingleend = strchr(auxsingle, ']');
804db2661ceSRobert Watson 		if (auxsingleend == NULL)
805db2661ceSRobert Watson 			return (EINVAL);
806db2661ceSRobert Watson 		if (*(auxsingleend + 1) != '\0')
807db2661ceSRobert Watson 			return (EINVAL);
808db2661ceSRobert Watson 		/* Nul terminate the end of the auxsingle. */
809db2661ceSRobert Watson 		*auxsingleend = '\0';
810db2661ceSRobert Watson 	}
811db2661ceSRobert Watson 
8123f1a7a90SRobert Watson 	bzero(ml, sizeof(*ml));
813db2661ceSRobert Watson 	if (single != NULL) {
8143f1a7a90SRobert Watson 		error = lomac_parse_element(&ml->ml_single, single);
815db2661ceSRobert Watson 		if (error)
816db2661ceSRobert Watson 			return (error);
8173f1a7a90SRobert Watson 		ml->ml_flags |= MAC_LOMAC_FLAG_SINGLE;
818db2661ceSRobert Watson 	}
819db2661ceSRobert Watson 
820db2661ceSRobert Watson 	if (auxsingle != NULL) {
82195b85ca3SRobert Watson 		error = lomac_parse_element(&ml->ml_auxsingle, auxsingle);
822db2661ceSRobert Watson 		if (error)
823db2661ceSRobert Watson 			return (error);
8243f1a7a90SRobert Watson 		ml->ml_flags |= MAC_LOMAC_FLAG_AUX;
825db2661ceSRobert Watson 	}
826db2661ceSRobert Watson 
827db2661ceSRobert Watson 	if (rangelow != NULL) {
8283f1a7a90SRobert Watson 		error = lomac_parse_element(&ml->ml_rangelow, rangelow);
829db2661ceSRobert Watson 		if (error)
830db2661ceSRobert Watson 			return (error);
8313f1a7a90SRobert Watson 		error = lomac_parse_element(&ml->ml_rangehigh, rangehigh);
832db2661ceSRobert Watson 		if (error)
833db2661ceSRobert Watson 			return (error);
8343f1a7a90SRobert Watson 		ml->ml_flags |= MAC_LOMAC_FLAG_RANGE;
835db2661ceSRobert Watson 	}
836db2661ceSRobert Watson 
8373f1a7a90SRobert Watson 	error = lomac_valid(ml);
838db2661ceSRobert Watson 	if (error)
839db2661ceSRobert Watson 		return (error);
840db2661ceSRobert Watson 
841db2661ceSRobert Watson 	return (0);
842db2661ceSRobert Watson }
843db2661ceSRobert Watson 
844db2661ceSRobert Watson static int
lomac_internalize_label(struct label * label,char * element_name,char * element_data,int * claimed)8453f1a7a90SRobert Watson lomac_internalize_label(struct label *label, char *element_name,
846db2661ceSRobert Watson     char *element_data, int *claimed)
847db2661ceSRobert Watson {
8483f1a7a90SRobert Watson 	struct mac_lomac *ml, ml_temp;
849db2661ceSRobert Watson 	int error;
850db2661ceSRobert Watson 
851db2661ceSRobert Watson 	if (strcmp(MAC_LOMAC_LABEL_NAME, element_name) != 0)
852db2661ceSRobert Watson 		return (0);
853db2661ceSRobert Watson 
854db2661ceSRobert Watson 	(*claimed)++;
855db2661ceSRobert Watson 
8563f1a7a90SRobert Watson 	error = lomac_parse(&ml_temp, element_data);
857db2661ceSRobert Watson 	if (error)
858db2661ceSRobert Watson 		return (error);
859db2661ceSRobert Watson 
8603f1a7a90SRobert Watson 	ml = SLOT(label);
8613f1a7a90SRobert Watson 	*ml = ml_temp;
862db2661ceSRobert Watson 
863db2661ceSRobert Watson 	return (0);
864db2661ceSRobert Watson }
865db2661ceSRobert Watson 
866db2661ceSRobert Watson static void
lomac_copy_label(struct label * src,struct label * dest)8673f1a7a90SRobert Watson lomac_copy_label(struct label *src, struct label *dest)
868db2661ceSRobert Watson {
869db2661ceSRobert Watson 
870db2661ceSRobert Watson 	*SLOT(dest) = *SLOT(src);
871db2661ceSRobert Watson }
872db2661ceSRobert Watson 
873db2661ceSRobert Watson /*
874eb320b0eSRobert Watson  * Object-specific entry point implementations are sorted alphabetically by
875eb320b0eSRobert Watson  * object type name and then by operation.
876db2661ceSRobert Watson  */
877db2661ceSRobert Watson static int
lomac_bpfdesc_check_receive(struct bpf_d * d,struct label * dlabel,struct ifnet * ifp,struct label * ifplabel)8783f1a7a90SRobert Watson lomac_bpfdesc_check_receive(struct bpf_d *d, struct label *dlabel,
87978007886SRobert Watson     struct ifnet *ifp, struct label *ifplabel)
880db2661ceSRobert Watson {
881db2661ceSRobert Watson 	struct mac_lomac *a, *b;
882db2661ceSRobert Watson 
8833f1a7a90SRobert Watson 	if (!lomac_enabled)
884db2661ceSRobert Watson 		return (0);
885db2661ceSRobert Watson 
88678007886SRobert Watson 	a = SLOT(dlabel);
88778007886SRobert Watson 	b = SLOT(ifplabel);
888db2661ceSRobert Watson 
8893f1a7a90SRobert Watson 	if (lomac_equal_single(a, b))
890db2661ceSRobert Watson 		return (0);
891db2661ceSRobert Watson 	return (EACCES);
892db2661ceSRobert Watson }
893db2661ceSRobert Watson 
894eb320b0eSRobert Watson static void
lomac_bpfdesc_create(struct ucred * cred,struct bpf_d * d,struct label * dlabel)895eb320b0eSRobert Watson lomac_bpfdesc_create(struct ucred *cred, struct bpf_d *d,
896eb320b0eSRobert Watson     struct label *dlabel)
897eb320b0eSRobert Watson {
898eb320b0eSRobert Watson 	struct mac_lomac *source, *dest;
899eb320b0eSRobert Watson 
900eb320b0eSRobert Watson 	source = SLOT(cred->cr_label);
901eb320b0eSRobert Watson 	dest = SLOT(dlabel);
902eb320b0eSRobert Watson 
903eb320b0eSRobert Watson 	lomac_copy_single(source, dest);
904eb320b0eSRobert Watson }
905eb320b0eSRobert Watson 
906eb320b0eSRobert Watson static void
lomac_bpfdesc_create_mbuf(struct bpf_d * d,struct label * dlabel,struct mbuf * m,struct label * mlabel)907eb320b0eSRobert Watson lomac_bpfdesc_create_mbuf(struct bpf_d *d, struct label *dlabel,
908eb320b0eSRobert Watson     struct mbuf *m, struct label *mlabel)
909eb320b0eSRobert Watson {
910eb320b0eSRobert Watson 	struct mac_lomac *source, *dest;
911eb320b0eSRobert Watson 
912eb320b0eSRobert Watson 	source = SLOT(dlabel);
913eb320b0eSRobert Watson 	dest = SLOT(mlabel);
914eb320b0eSRobert Watson 
915eb320b0eSRobert Watson 	lomac_copy_single(source, dest);
916eb320b0eSRobert Watson }
917eb320b0eSRobert Watson 
918db2661ceSRobert Watson static int
lomac_cred_check_relabel(struct ucred * cred,struct label * newlabel)9193f1a7a90SRobert Watson lomac_cred_check_relabel(struct ucred *cred, struct label *newlabel)
920db2661ceSRobert Watson {
921db2661ceSRobert Watson 	struct mac_lomac *subj, *new;
922db2661ceSRobert Watson 	int error;
923db2661ceSRobert Watson 
924eca8a663SRobert Watson 	subj = SLOT(cred->cr_label);
925db2661ceSRobert Watson 	new = SLOT(newlabel);
926db2661ceSRobert Watson 
927db2661ceSRobert Watson 	/*
9283f1a7a90SRobert Watson 	 * If there is a LOMAC label update for the credential, it may be an
9293f1a7a90SRobert Watson 	 * update of the single, range, or both.
930db2661ceSRobert Watson 	 */
931db2661ceSRobert Watson 	error = lomac_atmostflags(new, MAC_LOMAC_FLAGS_BOTH);
932db2661ceSRobert Watson 	if (error)
933db2661ceSRobert Watson 		return (error);
934db2661ceSRobert Watson 
935db2661ceSRobert Watson 	/*
936db2661ceSRobert Watson 	 * If the LOMAC label is to be changed, authorize as appropriate.
937db2661ceSRobert Watson 	 */
938db2661ceSRobert Watson 	if (new->ml_flags & MAC_LOMAC_FLAGS_BOTH) {
939db2661ceSRobert Watson 		/*
94084bdb083SRobert Watson 		 * Fill in the missing parts from the previous label.
941db2661ceSRobert Watson 		 */
94284bdb083SRobert Watson 		if ((new->ml_flags & MAC_LOMAC_FLAG_SINGLE) == 0)
9433f1a7a90SRobert Watson 			lomac_copy_single(subj, new);
94484bdb083SRobert Watson 		if ((new->ml_flags & MAC_LOMAC_FLAG_RANGE) == 0)
9453f1a7a90SRobert Watson 			lomac_copy_range(subj, new);
946db2661ceSRobert Watson 
947db2661ceSRobert Watson 		/*
9483f1a7a90SRobert Watson 		 * To change the LOMAC range on a credential, the new range
9493f1a7a90SRobert Watson 		 * label must be in the current range.
950db2661ceSRobert Watson 		 */
9513f1a7a90SRobert Watson 		if (!lomac_range_in_range(new, subj))
95284bdb083SRobert Watson 			return (EPERM);
95384bdb083SRobert Watson 
95484bdb083SRobert Watson 		/*
9553f1a7a90SRobert Watson 		 * To change the LOMAC single label on a credential, the new
9563f1a7a90SRobert Watson 		 * single label must be in the new range.  Implicitly from
9573f1a7a90SRobert Watson 		 * the previous check, the new single is in the old range.
95884bdb083SRobert Watson 		 */
9593f1a7a90SRobert Watson 		if (!lomac_single_in_range(new, new))
960db2661ceSRobert Watson 			return (EPERM);
961db2661ceSRobert Watson 
962db2661ceSRobert Watson 		/*
9633f1a7a90SRobert Watson 		 * To have EQUAL in any component of the new credential LOMAC
9643f1a7a90SRobert Watson 		 * label, the subject must already have EQUAL in their label.
965db2661ceSRobert Watson 		 */
9663f1a7a90SRobert Watson 		if (lomac_contains_equal(new)) {
9673f1a7a90SRobert Watson 			error = lomac_subject_privileged(subj);
968db2661ceSRobert Watson 			if (error)
969db2661ceSRobert Watson 				return (error);
970db2661ceSRobert Watson 		}
971db2661ceSRobert Watson 
972db2661ceSRobert Watson 		/*
9733f1a7a90SRobert Watson 		 * XXXMAC: Additional consistency tests regarding the single
9743f1a7a90SRobert Watson 		 * and range of the new label might be performed here.
975db2661ceSRobert Watson 		 */
976db2661ceSRobert Watson 	}
977db2661ceSRobert Watson 
978db2661ceSRobert Watson 	return (0);
979db2661ceSRobert Watson }
980db2661ceSRobert Watson 
981db2661ceSRobert Watson static int
lomac_cred_check_visible(struct ucred * cr1,struct ucred * cr2)9823f1a7a90SRobert Watson lomac_cred_check_visible(struct ucred *cr1, struct ucred *cr2)
983db2661ceSRobert Watson {
984db2661ceSRobert Watson 	struct mac_lomac *subj, *obj;
985db2661ceSRobert Watson 
9863f1a7a90SRobert Watson 	if (!lomac_enabled)
987db2661ceSRobert Watson 		return (0);
988db2661ceSRobert Watson 
98978007886SRobert Watson 	subj = SLOT(cr1->cr_label);
99078007886SRobert Watson 	obj = SLOT(cr2->cr_label);
991db2661ceSRobert Watson 
992db2661ceSRobert Watson 	/* XXX: range */
9933f1a7a90SRobert Watson 	if (!lomac_dominate_single(obj, subj))
994db2661ceSRobert Watson 		return (ESRCH);
995db2661ceSRobert Watson 
996db2661ceSRobert Watson 	return (0);
997db2661ceSRobert Watson }
998212ab0cfSRobert Watson 
999212ab0cfSRobert Watson static void
lomac_cred_create_init(struct ucred * cred)1000212ab0cfSRobert Watson lomac_cred_create_init(struct ucred *cred)
1001212ab0cfSRobert Watson {
1002212ab0cfSRobert Watson 	struct mac_lomac *dest;
1003212ab0cfSRobert Watson 
1004212ab0cfSRobert Watson 	dest = SLOT(cred->cr_label);
1005212ab0cfSRobert Watson 
1006212ab0cfSRobert Watson 	lomac_set_single(dest, MAC_LOMAC_TYPE_HIGH, 0);
1007212ab0cfSRobert Watson 	lomac_set_range(dest, MAC_LOMAC_TYPE_LOW, 0, MAC_LOMAC_TYPE_HIGH, 0);
1008212ab0cfSRobert Watson }
1009212ab0cfSRobert Watson 
1010212ab0cfSRobert Watson static void
lomac_cred_create_swapper(struct ucred * cred)1011212ab0cfSRobert Watson lomac_cred_create_swapper(struct ucred *cred)
1012212ab0cfSRobert Watson {
1013212ab0cfSRobert Watson 	struct mac_lomac *dest;
1014212ab0cfSRobert Watson 
1015212ab0cfSRobert Watson 	dest = SLOT(cred->cr_label);
1016212ab0cfSRobert Watson 
1017212ab0cfSRobert Watson 	lomac_set_single(dest, MAC_LOMAC_TYPE_EQUAL, 0);
1018212ab0cfSRobert Watson 	lomac_set_range(dest, MAC_LOMAC_TYPE_LOW, 0, MAC_LOMAC_TYPE_HIGH, 0);
1019212ab0cfSRobert Watson }
1020212ab0cfSRobert Watson 
1021eb320b0eSRobert Watson static void
lomac_cred_relabel(struct ucred * cred,struct label * newlabel)1022eb320b0eSRobert Watson lomac_cred_relabel(struct ucred *cred, struct label *newlabel)
1023eb320b0eSRobert Watson {
1024eb320b0eSRobert Watson 	struct mac_lomac *source, *dest;
1025eb320b0eSRobert Watson 
1026eb320b0eSRobert Watson 	source = SLOT(newlabel);
1027eb320b0eSRobert Watson 	dest = SLOT(cred->cr_label);
1028eb320b0eSRobert Watson 
1029eb320b0eSRobert Watson 	try_relabel(source, dest);
1030eb320b0eSRobert Watson }
1031eb320b0eSRobert Watson 
1032eb320b0eSRobert Watson static void
lomac_devfs_create_device(struct ucred * cred,struct mount * mp,struct cdev * dev,struct devfs_dirent * de,struct label * delabel)1033eb320b0eSRobert Watson lomac_devfs_create_device(struct ucred *cred, struct mount *mp,
1034eb320b0eSRobert Watson     struct cdev *dev, struct devfs_dirent *de, struct label *delabel)
1035eb320b0eSRobert Watson {
1036eb320b0eSRobert Watson 	struct mac_lomac *ml;
10377870adb6SEd Schouten 	const char *dn;
1038eb320b0eSRobert Watson 	int lomac_type;
1039eb320b0eSRobert Watson 
1040eb320b0eSRobert Watson 	ml = SLOT(delabel);
10417870adb6SEd Schouten 	dn = devtoname(dev);
10427870adb6SEd Schouten 	if (strcmp(dn, "null") == 0 ||
10437870adb6SEd Schouten 	    strcmp(dn, "zero") == 0 ||
10447870adb6SEd Schouten 	    strcmp(dn, "random") == 0 ||
10457870adb6SEd Schouten 	    strncmp(dn, "fd/", strlen("fd/")) == 0 ||
10467870adb6SEd Schouten 	    strncmp(dn, "ttyv", strlen("ttyv")) == 0)
1047eb320b0eSRobert Watson 		lomac_type = MAC_LOMAC_TYPE_EQUAL;
1048eb320b0eSRobert Watson 	else if (ptys_equal &&
10497870adb6SEd Schouten 	    (strncmp(dn, "ttyp", strlen("ttyp")) == 0 ||
10507870adb6SEd Schouten 	    strncmp(dn, "pts/", strlen("pts/")) == 0 ||
10517870adb6SEd Schouten 	    strncmp(dn, "ptyp", strlen("ptyp")) == 0))
1052eb320b0eSRobert Watson 		lomac_type = MAC_LOMAC_TYPE_EQUAL;
1053eb320b0eSRobert Watson 	else
1054eb320b0eSRobert Watson 		lomac_type = MAC_LOMAC_TYPE_HIGH;
1055eb320b0eSRobert Watson 	lomac_set_single(ml, lomac_type, 0);
1056eb320b0eSRobert Watson }
1057eb320b0eSRobert Watson 
1058eb320b0eSRobert Watson static void
lomac_devfs_create_directory(struct mount * mp,char * dirname,int dirnamelen,struct devfs_dirent * de,struct label * delabel)1059eb320b0eSRobert Watson lomac_devfs_create_directory(struct mount *mp, char *dirname, int dirnamelen,
1060eb320b0eSRobert Watson     struct devfs_dirent *de, struct label *delabel)
1061eb320b0eSRobert Watson {
1062eb320b0eSRobert Watson 	struct mac_lomac *ml;
1063eb320b0eSRobert Watson 
1064eb320b0eSRobert Watson 	ml = SLOT(delabel);
1065eb320b0eSRobert Watson 	lomac_set_single(ml, MAC_LOMAC_TYPE_HIGH, 0);
1066eb320b0eSRobert Watson }
1067eb320b0eSRobert Watson 
1068eb320b0eSRobert Watson static void
lomac_devfs_create_symlink(struct ucred * cred,struct mount * mp,struct devfs_dirent * dd,struct label * ddlabel,struct devfs_dirent * de,struct label * delabel)1069eb320b0eSRobert Watson lomac_devfs_create_symlink(struct ucred *cred, struct mount *mp,
1070eb320b0eSRobert Watson     struct devfs_dirent *dd, struct label *ddlabel, struct devfs_dirent *de,
1071eb320b0eSRobert Watson     struct label *delabel)
1072eb320b0eSRobert Watson {
1073eb320b0eSRobert Watson 	struct mac_lomac *source, *dest;
1074eb320b0eSRobert Watson 
1075eb320b0eSRobert Watson 	source = SLOT(cred->cr_label);
1076eb320b0eSRobert Watson 	dest = SLOT(delabel);
1077eb320b0eSRobert Watson 
1078eb320b0eSRobert Watson 	lomac_copy_single(source, dest);
1079eb320b0eSRobert Watson }
1080eb320b0eSRobert Watson 
1081eb320b0eSRobert Watson static void
lomac_devfs_update(struct mount * mp,struct devfs_dirent * de,struct label * delabel,struct vnode * vp,struct label * vplabel)1082eb320b0eSRobert Watson lomac_devfs_update(struct mount *mp, struct devfs_dirent *de,
1083eb320b0eSRobert Watson     struct label *delabel, struct vnode *vp, struct label *vplabel)
1084eb320b0eSRobert Watson {
1085eb320b0eSRobert Watson 	struct mac_lomac *source, *dest;
1086eb320b0eSRobert Watson 
1087eb320b0eSRobert Watson 	source = SLOT(vplabel);
1088eb320b0eSRobert Watson 	dest = SLOT(delabel);
1089eb320b0eSRobert Watson 
1090eb320b0eSRobert Watson 	lomac_copy(source, dest);
1091eb320b0eSRobert Watson }
1092eb320b0eSRobert Watson 
1093eb320b0eSRobert Watson static void
lomac_devfs_vnode_associate(struct mount * mp,struct label * mplabel,struct devfs_dirent * de,struct label * delabel,struct vnode * vp,struct label * vplabel)1094eb320b0eSRobert Watson lomac_devfs_vnode_associate(struct mount *mp, struct label *mplabel,
1095eb320b0eSRobert Watson     struct devfs_dirent *de, struct label *delabel, struct vnode *vp,
1096eb320b0eSRobert Watson     struct label *vplabel)
1097eb320b0eSRobert Watson {
1098eb320b0eSRobert Watson 	struct mac_lomac *source, *dest;
1099eb320b0eSRobert Watson 
1100eb320b0eSRobert Watson 	source = SLOT(delabel);
1101eb320b0eSRobert Watson 	dest = SLOT(vplabel);
1102eb320b0eSRobert Watson 
1103eb320b0eSRobert Watson 	lomac_copy_single(source, dest);
1104eb320b0eSRobert Watson }
1105db2661ceSRobert Watson 
1106db2661ceSRobert Watson static int
lomac_ifnet_check_relabel(struct ucred * cred,struct ifnet * ifp,struct label * ifplabel,struct label * newlabel)11073f1a7a90SRobert Watson lomac_ifnet_check_relabel(struct ucred *cred, struct ifnet *ifp,
110878007886SRobert Watson     struct label *ifplabel, struct label *newlabel)
1109db2661ceSRobert Watson {
1110db2661ceSRobert Watson 	struct mac_lomac *subj, *new;
1111db2661ceSRobert Watson 	int error;
1112db2661ceSRobert Watson 
1113eca8a663SRobert Watson 	subj = SLOT(cred->cr_label);
1114db2661ceSRobert Watson 	new = SLOT(newlabel);
1115db2661ceSRobert Watson 
1116db2661ceSRobert Watson 	/*
11173f1a7a90SRobert Watson 	 * If there is a LOMAC label update for the interface, it may be an
11183f1a7a90SRobert Watson 	 * update of the single, range, or both.
1119db2661ceSRobert Watson 	 */
1120db2661ceSRobert Watson 	error = lomac_atmostflags(new, MAC_LOMAC_FLAGS_BOTH);
1121db2661ceSRobert Watson 	if (error)
1122db2661ceSRobert Watson 		return (error);
1123db2661ceSRobert Watson 
1124db2661ceSRobert Watson 	/*
1125db2661ceSRobert Watson 	 * Relabling network interfaces requires LOMAC privilege.
1126db2661ceSRobert Watson 	 */
11273f1a7a90SRobert Watson 	error = lomac_subject_privileged(subj);
1128db2661ceSRobert Watson 	if (error)
1129db2661ceSRobert Watson 		return (error);
1130db2661ceSRobert Watson 
1131db2661ceSRobert Watson 	/*
1132db2661ceSRobert Watson 	 * If the LOMAC label is to be changed, authorize as appropriate.
1133db2661ceSRobert Watson 	 */
1134db2661ceSRobert Watson 	if (new->ml_flags & MAC_LOMAC_FLAGS_BOTH) {
1135db2661ceSRobert Watson 		/*
113684bdb083SRobert Watson 		 * Fill in the missing parts from the previous label.
113784bdb083SRobert Watson 		 */
113884bdb083SRobert Watson 		if ((new->ml_flags & MAC_LOMAC_FLAG_SINGLE) == 0)
11393f1a7a90SRobert Watson 			lomac_copy_single(subj, new);
114084bdb083SRobert Watson 		if ((new->ml_flags & MAC_LOMAC_FLAG_RANGE) == 0)
11413f1a7a90SRobert Watson 			lomac_copy_range(subj, new);
114284bdb083SRobert Watson 
114384bdb083SRobert Watson 		/*
1144db2661ceSRobert Watson 		 * Rely on the traditional superuser status for the LOMAC
1145db2661ceSRobert Watson 		 * interface relabel requirements.  XXXMAC: This will go
1146db2661ceSRobert Watson 		 * away.
1147acd3428bSRobert Watson 		 *
1148acd3428bSRobert Watson 		 * XXXRW: This is also redundant to a higher layer check.
1149db2661ceSRobert Watson 		 */
1150cc426dd3SMateusz Guzik 		error = priv_check_cred(cred, PRIV_NET_SETIFMAC);
1151db2661ceSRobert Watson 		if (error)
1152db2661ceSRobert Watson 			return (EPERM);
1153db2661ceSRobert Watson 
1154db2661ceSRobert Watson 		/*
1155db2661ceSRobert Watson 		 * XXXMAC: Additional consistency tests regarding the single
1156db2661ceSRobert Watson 		 * and the range of the new label might be performed here.
1157db2661ceSRobert Watson 		 */
1158db2661ceSRobert Watson 	}
1159db2661ceSRobert Watson 
1160db2661ceSRobert Watson 	return (0);
1161db2661ceSRobert Watson }
1162db2661ceSRobert Watson 
1163db2661ceSRobert Watson static int
lomac_ifnet_check_transmit(struct ifnet * ifp,struct label * ifplabel,struct mbuf * m,struct label * mlabel)11643f1a7a90SRobert Watson lomac_ifnet_check_transmit(struct ifnet *ifp, struct label *ifplabel,
116578007886SRobert Watson     struct mbuf *m, struct label *mlabel)
1166db2661ceSRobert Watson {
1167db2661ceSRobert Watson 	struct mac_lomac *p, *i;
1168db2661ceSRobert Watson 
11693f1a7a90SRobert Watson 	if (!lomac_enabled)
1170db2661ceSRobert Watson 		return (0);
1171db2661ceSRobert Watson 
117278007886SRobert Watson 	p = SLOT(mlabel);
117378007886SRobert Watson 	i = SLOT(ifplabel);
1174db2661ceSRobert Watson 
11753f1a7a90SRobert Watson 	return (lomac_single_in_range(p, i) ? 0 : EACCES);
1176db2661ceSRobert Watson }
1177db2661ceSRobert Watson 
1178eb320b0eSRobert Watson static void
lomac_ifnet_create(struct ifnet * ifp,struct label * ifplabel)1179eb320b0eSRobert Watson lomac_ifnet_create(struct ifnet *ifp, struct label *ifplabel)
1180eb320b0eSRobert Watson {
1181eb320b0eSRobert Watson 	char tifname[IFNAMSIZ], *p, *q;
1182eb320b0eSRobert Watson 	char tiflist[sizeof(trusted_interfaces)];
1183eb320b0eSRobert Watson 	struct mac_lomac *dest;
1184eb320b0eSRobert Watson 	int len, grade;
1185eb320b0eSRobert Watson 
1186eb320b0eSRobert Watson 	dest = SLOT(ifplabel);
1187eb320b0eSRobert Watson 
118830af2c13SJustin Hibbits 	if (if_gettype(ifp) == IFT_LOOP) {
1189eb320b0eSRobert Watson 		grade = MAC_LOMAC_TYPE_EQUAL;
1190eb320b0eSRobert Watson 		goto set;
1191eb320b0eSRobert Watson 	}
1192eb320b0eSRobert Watson 
1193eb320b0eSRobert Watson 	if (trust_all_interfaces) {
1194eb320b0eSRobert Watson 		grade = MAC_LOMAC_TYPE_HIGH;
1195eb320b0eSRobert Watson 		goto set;
1196eb320b0eSRobert Watson 	}
1197eb320b0eSRobert Watson 
1198eb320b0eSRobert Watson 	grade = MAC_LOMAC_TYPE_LOW;
1199eb320b0eSRobert Watson 
1200eb320b0eSRobert Watson 	if (trusted_interfaces[0] == '\0' ||
1201eb320b0eSRobert Watson 	    !strvalid(trusted_interfaces, sizeof(trusted_interfaces)))
1202eb320b0eSRobert Watson 		goto set;
1203eb320b0eSRobert Watson 
1204eb320b0eSRobert Watson 	bzero(tiflist, sizeof(tiflist));
1205eb320b0eSRobert Watson 	for (p = trusted_interfaces, q = tiflist; *p != '\0'; p++, q++)
1206eb320b0eSRobert Watson 		if(*p != ' ' && *p != '\t')
1207eb320b0eSRobert Watson 			*q = *p;
1208eb320b0eSRobert Watson 
1209eb320b0eSRobert Watson 	for (p = q = tiflist;; p++) {
1210eb320b0eSRobert Watson 		if (*p == ',' || *p == '\0') {
1211eb320b0eSRobert Watson 			len = p - q;
1212eb320b0eSRobert Watson 			if (len < IFNAMSIZ) {
1213eb320b0eSRobert Watson 				bzero(tifname, sizeof(tifname));
1214eb320b0eSRobert Watson 				bcopy(q, tifname, len);
121530af2c13SJustin Hibbits 				if (strcmp(tifname, if_name(ifp)) == 0) {
1216eb320b0eSRobert Watson 					grade = MAC_LOMAC_TYPE_HIGH;
1217eb320b0eSRobert Watson 					break;
1218eb320b0eSRobert Watson 				}
1219eb320b0eSRobert Watson 			}
1220eb320b0eSRobert Watson 			else {
1221eb320b0eSRobert Watson 				*p = '\0';
1222eb320b0eSRobert Watson 				printf("MAC/LOMAC warning: interface name "
1223eb320b0eSRobert Watson 				    "\"%s\" is too long (must be < %d)\n",
1224eb320b0eSRobert Watson 				    q, IFNAMSIZ);
1225eb320b0eSRobert Watson 			}
1226eb320b0eSRobert Watson 			if (*p == '\0')
1227eb320b0eSRobert Watson 				break;
1228eb320b0eSRobert Watson 			q = p + 1;
1229eb320b0eSRobert Watson 		}
1230eb320b0eSRobert Watson 	}
1231eb320b0eSRobert Watson set:
1232eb320b0eSRobert Watson 	lomac_set_single(dest, grade, 0);
1233eb320b0eSRobert Watson 	lomac_set_range(dest, grade, 0, grade, 0);
1234eb320b0eSRobert Watson }
1235eb320b0eSRobert Watson 
1236eb320b0eSRobert Watson static void
lomac_ifnet_create_mbuf(struct ifnet * ifp,struct label * ifplabel,struct mbuf * m,struct label * mlabel)1237eb320b0eSRobert Watson lomac_ifnet_create_mbuf(struct ifnet *ifp, struct label *ifplabel,
1238eb320b0eSRobert Watson     struct mbuf *m, struct label *mlabel)
1239eb320b0eSRobert Watson {
1240eb320b0eSRobert Watson 	struct mac_lomac *source, *dest;
1241eb320b0eSRobert Watson 
1242eb320b0eSRobert Watson 	source = SLOT(ifplabel);
1243eb320b0eSRobert Watson 	dest = SLOT(mlabel);
1244eb320b0eSRobert Watson 
1245eb320b0eSRobert Watson 	lomac_copy_single(source, dest);
1246eb320b0eSRobert Watson }
1247eb320b0eSRobert Watson 
1248eb320b0eSRobert Watson static void
lomac_ifnet_relabel(struct ucred * cred,struct ifnet * ifp,struct label * ifplabel,struct label * newlabel)1249eb320b0eSRobert Watson lomac_ifnet_relabel(struct ucred *cred, struct ifnet *ifp,
1250eb320b0eSRobert Watson     struct label *ifplabel, struct label *newlabel)
1251eb320b0eSRobert Watson {
1252eb320b0eSRobert Watson 	struct mac_lomac *source, *dest;
1253eb320b0eSRobert Watson 
1254eb320b0eSRobert Watson 	source = SLOT(newlabel);
1255eb320b0eSRobert Watson 	dest = SLOT(ifplabel);
1256eb320b0eSRobert Watson 
1257eb320b0eSRobert Watson 	try_relabel(source, dest);
1258eb320b0eSRobert Watson }
1259eb320b0eSRobert Watson 
1260db2661ceSRobert Watson static int
lomac_inpcb_check_deliver(struct inpcb * inp,struct label * inplabel,struct mbuf * m,struct label * mlabel)12613f1a7a90SRobert Watson lomac_inpcb_check_deliver(struct inpcb *inp, struct label *inplabel,
1262a557af22SRobert Watson     struct mbuf *m, struct label *mlabel)
1263a557af22SRobert Watson {
1264a557af22SRobert Watson 	struct mac_lomac *p, *i;
1265a557af22SRobert Watson 
12663f1a7a90SRobert Watson 	if (!lomac_enabled)
1267a557af22SRobert Watson 		return (0);
1268a557af22SRobert Watson 
1269a557af22SRobert Watson 	p = SLOT(mlabel);
1270a557af22SRobert Watson 	i = SLOT(inplabel);
1271a557af22SRobert Watson 
12723f1a7a90SRobert Watson 	return (lomac_equal_single(p, i) ? 0 : EACCES);
1273a557af22SRobert Watson }
1274a557af22SRobert Watson 
12757fb179baSBjoern A. Zeeb static int
lomac_inpcb_check_visible(struct ucred * cred,struct inpcb * inp,struct label * inplabel)12767fb179baSBjoern A. Zeeb lomac_inpcb_check_visible(struct ucred *cred, struct inpcb *inp,
12777fb179baSBjoern A. Zeeb     struct label *inplabel)
12787fb179baSBjoern A. Zeeb {
12797fb179baSBjoern A. Zeeb 	struct mac_lomac *subj, *obj;
12807fb179baSBjoern A. Zeeb 
12817fb179baSBjoern A. Zeeb 	if (!lomac_enabled)
12827fb179baSBjoern A. Zeeb 		return (0);
12837fb179baSBjoern A. Zeeb 
12847fb179baSBjoern A. Zeeb 	subj = SLOT(cred->cr_label);
12857fb179baSBjoern A. Zeeb 	obj = SLOT(inplabel);
12867fb179baSBjoern A. Zeeb 
12877fb179baSBjoern A. Zeeb 	if (!lomac_dominate_single(obj, subj))
12887fb179baSBjoern A. Zeeb 		return (ENOENT);
12897fb179baSBjoern A. Zeeb 
12907fb179baSBjoern A. Zeeb 	return (0);
12917fb179baSBjoern A. Zeeb }
12927fb179baSBjoern A. Zeeb 
1293eb320b0eSRobert Watson static void
lomac_inpcb_create(struct socket * so,struct label * solabel,struct inpcb * inp,struct label * inplabel)1294eb320b0eSRobert Watson lomac_inpcb_create(struct socket *so, struct label *solabel,
1295eb320b0eSRobert Watson     struct inpcb *inp, struct label *inplabel)
1296eb320b0eSRobert Watson {
1297eb320b0eSRobert Watson 	struct mac_lomac *source, *dest;
1298eb320b0eSRobert Watson 
1299eb320b0eSRobert Watson 	source = SLOT(solabel);
1300eb320b0eSRobert Watson 	dest = SLOT(inplabel);
1301eb320b0eSRobert Watson 
1302eb320b0eSRobert Watson 	lomac_copy_single(source, dest);
1303eb320b0eSRobert Watson }
1304eb320b0eSRobert Watson 
1305eb320b0eSRobert Watson static void
lomac_inpcb_create_mbuf(struct inpcb * inp,struct label * inplabel,struct mbuf * m,struct label * mlabel)1306eb320b0eSRobert Watson lomac_inpcb_create_mbuf(struct inpcb *inp, struct label *inplabel,
1307eb320b0eSRobert Watson     struct mbuf *m, struct label *mlabel)
1308eb320b0eSRobert Watson {
1309eb320b0eSRobert Watson 	struct mac_lomac *source, *dest;
1310eb320b0eSRobert Watson 
1311eb320b0eSRobert Watson 	source = SLOT(inplabel);
1312eb320b0eSRobert Watson 	dest = SLOT(mlabel);
1313eb320b0eSRobert Watson 
1314eb320b0eSRobert Watson 	lomac_copy_single(source, dest);
1315eb320b0eSRobert Watson }
1316eb320b0eSRobert Watson 
1317eb320b0eSRobert Watson static void
lomac_inpcb_sosetlabel(struct socket * so,struct label * solabel,struct inpcb * inp,struct label * inplabel)1318eb320b0eSRobert Watson lomac_inpcb_sosetlabel(struct socket *so, struct label *solabel,
1319eb320b0eSRobert Watson     struct inpcb *inp, struct label *inplabel)
1320eb320b0eSRobert Watson {
1321eb320b0eSRobert Watson 	struct mac_lomac *source, *dest;
1322eb320b0eSRobert Watson 
13233de40469SRobert Watson 	SOCK_LOCK_ASSERT(so);
13243de40469SRobert Watson 
1325eb320b0eSRobert Watson 	source = SLOT(solabel);
1326eb320b0eSRobert Watson 	dest = SLOT(inplabel);
1327eb320b0eSRobert Watson 
1328eb320b0eSRobert Watson 	lomac_copy_single(source, dest);
1329eb320b0eSRobert Watson }
1330eb320b0eSRobert Watson 
1331eb320b0eSRobert Watson static void
lomac_ip6q_create(struct mbuf * m,struct label * mlabel,struct ip6q * q6,struct label * q6label)1332048e1287SRobert Watson lomac_ip6q_create(struct mbuf *m, struct label *mlabel, struct ip6q *q6,
1333048e1287SRobert Watson     struct label *q6label)
1334048e1287SRobert Watson {
1335048e1287SRobert Watson 	struct mac_lomac *source, *dest;
1336048e1287SRobert Watson 
1337048e1287SRobert Watson 	source = SLOT(mlabel);
1338048e1287SRobert Watson 	dest = SLOT(q6label);
1339048e1287SRobert Watson 
1340048e1287SRobert Watson 	lomac_copy_single(source, dest);
1341048e1287SRobert Watson }
1342048e1287SRobert Watson 
1343048e1287SRobert Watson static int
lomac_ip6q_match(struct mbuf * m,struct label * mlabel,struct ip6q * q6,struct label * q6label)1344048e1287SRobert Watson lomac_ip6q_match(struct mbuf *m, struct label *mlabel, struct ip6q *q6,
1345048e1287SRobert Watson     struct label *q6label)
1346048e1287SRobert Watson {
1347048e1287SRobert Watson 	struct mac_lomac *a, *b;
1348048e1287SRobert Watson 
1349048e1287SRobert Watson 	a = SLOT(q6label);
1350048e1287SRobert Watson 	b = SLOT(mlabel);
1351048e1287SRobert Watson 
1352048e1287SRobert Watson 	return (lomac_equal_single(a, b));
1353048e1287SRobert Watson }
1354048e1287SRobert Watson 
1355048e1287SRobert Watson static void
lomac_ip6q_reassemble(struct ip6q * q6,struct label * q6label,struct mbuf * m,struct label * mlabel)1356048e1287SRobert Watson lomac_ip6q_reassemble(struct ip6q *q6, struct label *q6label, struct mbuf *m,
1357048e1287SRobert Watson     struct label *mlabel)
1358048e1287SRobert Watson {
1359048e1287SRobert Watson 	struct mac_lomac *source, *dest;
1360048e1287SRobert Watson 
1361048e1287SRobert Watson 	source = SLOT(q6label);
1362048e1287SRobert Watson 	dest = SLOT(mlabel);
1363048e1287SRobert Watson 
1364048e1287SRobert Watson 	/* Just use the head, since we require them all to match. */
1365048e1287SRobert Watson 	lomac_copy_single(source, dest);
1366048e1287SRobert Watson }
1367048e1287SRobert Watson 
1368048e1287SRobert Watson static void
lomac_ip6q_update(struct mbuf * m,struct label * mlabel,struct ip6q * q6,struct label * q6label)1369048e1287SRobert Watson lomac_ip6q_update(struct mbuf *m, struct label *mlabel, struct ip6q *q6,
1370048e1287SRobert Watson     struct label *q6label)
1371048e1287SRobert Watson {
1372048e1287SRobert Watson 
1373048e1287SRobert Watson 	/* NOOP: we only accept matching labels, so no need to update */
1374048e1287SRobert Watson }
1375048e1287SRobert Watson 
1376048e1287SRobert Watson static void
lomac_ipq_create(struct mbuf * m,struct label * mlabel,struct ipq * q,struct label * qlabel)137737f44cb4SRobert Watson lomac_ipq_create(struct mbuf *m, struct label *mlabel, struct ipq *q,
137837f44cb4SRobert Watson     struct label *qlabel)
1379eb320b0eSRobert Watson {
1380eb320b0eSRobert Watson 	struct mac_lomac *source, *dest;
1381eb320b0eSRobert Watson 
1382eb320b0eSRobert Watson 	source = SLOT(mlabel);
138337f44cb4SRobert Watson 	dest = SLOT(qlabel);
1384eb320b0eSRobert Watson 
1385eb320b0eSRobert Watson 	lomac_copy_single(source, dest);
1386eb320b0eSRobert Watson }
1387eb320b0eSRobert Watson 
1388eb320b0eSRobert Watson static int
lomac_ipq_match(struct mbuf * m,struct label * mlabel,struct ipq * q,struct label * qlabel)138937f44cb4SRobert Watson lomac_ipq_match(struct mbuf *m, struct label *mlabel, struct ipq *q,
139037f44cb4SRobert Watson     struct label *qlabel)
1391eb320b0eSRobert Watson {
1392eb320b0eSRobert Watson 	struct mac_lomac *a, *b;
1393eb320b0eSRobert Watson 
139437f44cb4SRobert Watson 	a = SLOT(qlabel);
1395eb320b0eSRobert Watson 	b = SLOT(mlabel);
1396eb320b0eSRobert Watson 
1397eb320b0eSRobert Watson 	return (lomac_equal_single(a, b));
1398eb320b0eSRobert Watson }
1399eb320b0eSRobert Watson 
1400eb320b0eSRobert Watson static void
lomac_ipq_reassemble(struct ipq * q,struct label * qlabel,struct mbuf * m,struct label * mlabel)140137f44cb4SRobert Watson lomac_ipq_reassemble(struct ipq *q, struct label *qlabel, struct mbuf *m,
140237f44cb4SRobert Watson     struct label *mlabel)
1403eb320b0eSRobert Watson {
1404eb320b0eSRobert Watson 	struct mac_lomac *source, *dest;
1405eb320b0eSRobert Watson 
140637f44cb4SRobert Watson 	source = SLOT(qlabel);
1407eb320b0eSRobert Watson 	dest = SLOT(mlabel);
1408eb320b0eSRobert Watson 
1409eb320b0eSRobert Watson 	/* Just use the head, since we require them all to match. */
1410eb320b0eSRobert Watson 	lomac_copy_single(source, dest);
1411eb320b0eSRobert Watson }
1412eb320b0eSRobert Watson 
1413eb320b0eSRobert Watson static void
lomac_ipq_update(struct mbuf * m,struct label * mlabel,struct ipq * q,struct label * qlabel)141437f44cb4SRobert Watson lomac_ipq_update(struct mbuf *m, struct label *mlabel, struct ipq *q,
141537f44cb4SRobert Watson     struct label *qlabel)
1416eb320b0eSRobert Watson {
1417eb320b0eSRobert Watson 
1418eb320b0eSRobert Watson 	/* NOOP: we only accept matching labels, so no need to update */
1419eb320b0eSRobert Watson }
1420eb320b0eSRobert Watson 
1421a557af22SRobert Watson static int
lomac_kld_check_load(struct ucred * cred,struct vnode * vp,struct label * vplabel)14223f1a7a90SRobert Watson lomac_kld_check_load(struct ucred *cred, struct vnode *vp,
142378007886SRobert Watson     struct label *vplabel)
1424db2661ceSRobert Watson {
1425db2661ceSRobert Watson 	struct mac_lomac *subj, *obj;
1426db2661ceSRobert Watson 
14273f1a7a90SRobert Watson 	if (!lomac_enabled)
1428db2661ceSRobert Watson 		return (0);
1429db2661ceSRobert Watson 
1430eca8a663SRobert Watson 	subj = SLOT(cred->cr_label);
143178007886SRobert Watson 	obj = SLOT(vplabel);
1432db2661ceSRobert Watson 
14333f1a7a90SRobert Watson 	if (lomac_subject_privileged(subj))
1434db2661ceSRobert Watson 		return (EPERM);
1435db2661ceSRobert Watson 
14363f1a7a90SRobert Watson 	if (!lomac_high_single(obj))
1437db2661ceSRobert Watson 		return (EACCES);
1438db2661ceSRobert Watson 
1439db2661ceSRobert Watson 	return (0);
1440db2661ceSRobert Watson }
1441db2661ceSRobert Watson 
1442eb320b0eSRobert Watson static void
lomac_mount_create(struct ucred * cred,struct mount * mp,struct label * mplabel)1443eb320b0eSRobert Watson lomac_mount_create(struct ucred *cred, struct mount *mp,
1444eb320b0eSRobert Watson     struct label *mplabel)
1445eb320b0eSRobert Watson {
1446eb320b0eSRobert Watson 	struct mac_lomac *source, *dest;
1447eb320b0eSRobert Watson 
1448eb320b0eSRobert Watson 	source = SLOT(cred->cr_label);
1449eb320b0eSRobert Watson 	dest = SLOT(mplabel);
1450eb320b0eSRobert Watson 	lomac_copy_single(source, dest);
1451eb320b0eSRobert Watson }
1452eb320b0eSRobert Watson 
1453eb320b0eSRobert Watson static void
lomac_netinet_arp_send(struct ifnet * ifp,struct label * ifplabel,struct mbuf * m,struct label * mlabel)1454eb320b0eSRobert Watson lomac_netinet_arp_send(struct ifnet *ifp, struct label *ifplabel,
1455eb320b0eSRobert Watson     struct mbuf *m, struct label *mlabel)
1456eb320b0eSRobert Watson {
1457eb320b0eSRobert Watson 	struct mac_lomac *dest;
1458eb320b0eSRobert Watson 
1459eb320b0eSRobert Watson 	dest = SLOT(mlabel);
1460eb320b0eSRobert Watson 
1461eb320b0eSRobert Watson 	lomac_set_single(dest, MAC_LOMAC_TYPE_EQUAL, 0);
1462eb320b0eSRobert Watson }
1463eb320b0eSRobert Watson 
1464eb320b0eSRobert Watson static void
lomac_netinet_firewall_reply(struct mbuf * mrecv,struct label * mrecvlabel,struct mbuf * msend,struct label * msendlabel)1465eb320b0eSRobert Watson lomac_netinet_firewall_reply(struct mbuf *mrecv, struct label *mrecvlabel,
1466eb320b0eSRobert Watson     struct mbuf *msend, struct label *msendlabel)
1467eb320b0eSRobert Watson {
1468eb320b0eSRobert Watson 	struct mac_lomac *source, *dest;
1469eb320b0eSRobert Watson 
1470eb320b0eSRobert Watson 	source = SLOT(mrecvlabel);
1471eb320b0eSRobert Watson 	dest = SLOT(msendlabel);
1472eb320b0eSRobert Watson 
1473eb320b0eSRobert Watson 	lomac_copy_single(source, dest);
1474eb320b0eSRobert Watson }
1475eb320b0eSRobert Watson 
1476eb320b0eSRobert Watson static void
lomac_netinet_firewall_send(struct mbuf * m,struct label * mlabel)1477eb320b0eSRobert Watson lomac_netinet_firewall_send(struct mbuf *m, struct label *mlabel)
1478eb320b0eSRobert Watson {
1479eb320b0eSRobert Watson 	struct mac_lomac *dest;
1480eb320b0eSRobert Watson 
1481eb320b0eSRobert Watson 	dest = SLOT(mlabel);
1482eb320b0eSRobert Watson 
1483bc5ade0dSPedro F. Giffuni 	/* XXX: where is the label for the firewall really coming from? */
1484eb320b0eSRobert Watson 	lomac_set_single(dest, MAC_LOMAC_TYPE_EQUAL, 0);
1485eb320b0eSRobert Watson }
1486eb320b0eSRobert Watson 
1487eb320b0eSRobert Watson static void
lomac_netinet_fragment(struct mbuf * m,struct label * mlabel,struct mbuf * frag,struct label * fraglabel)1488eb320b0eSRobert Watson lomac_netinet_fragment(struct mbuf *m, struct label *mlabel,
1489eb320b0eSRobert Watson     struct mbuf *frag, struct label *fraglabel)
1490eb320b0eSRobert Watson {
1491eb320b0eSRobert Watson 	struct mac_lomac *source, *dest;
1492eb320b0eSRobert Watson 
1493eb320b0eSRobert Watson 	source = SLOT(mlabel);
1494eb320b0eSRobert Watson 	dest = SLOT(fraglabel);
1495eb320b0eSRobert Watson 
1496eb320b0eSRobert Watson 	lomac_copy_single(source, dest);
1497eb320b0eSRobert Watson }
1498eb320b0eSRobert Watson 
1499eb320b0eSRobert Watson static void
lomac_netinet_icmp_reply(struct mbuf * mrecv,struct label * mrecvlabel,struct mbuf * msend,struct label * msendlabel)1500eb320b0eSRobert Watson lomac_netinet_icmp_reply(struct mbuf *mrecv, struct label *mrecvlabel,
1501eb320b0eSRobert Watson     struct mbuf *msend, struct label *msendlabel)
1502eb320b0eSRobert Watson {
1503eb320b0eSRobert Watson 	struct mac_lomac *source, *dest;
1504eb320b0eSRobert Watson 
1505eb320b0eSRobert Watson 	source = SLOT(mrecvlabel);
1506eb320b0eSRobert Watson 	dest = SLOT(msendlabel);
1507eb320b0eSRobert Watson 
1508eb320b0eSRobert Watson 	lomac_copy_single(source, dest);
1509eb320b0eSRobert Watson }
1510eb320b0eSRobert Watson 
1511eb320b0eSRobert Watson static void
lomac_netinet_igmp_send(struct ifnet * ifp,struct label * ifplabel,struct mbuf * m,struct label * mlabel)1512eb320b0eSRobert Watson lomac_netinet_igmp_send(struct ifnet *ifp, struct label *ifplabel,
1513eb320b0eSRobert Watson     struct mbuf *m, struct label *mlabel)
1514eb320b0eSRobert Watson {
1515eb320b0eSRobert Watson 	struct mac_lomac *dest;
1516eb320b0eSRobert Watson 
1517eb320b0eSRobert Watson 	dest = SLOT(mlabel);
1518eb320b0eSRobert Watson 
1519eb320b0eSRobert Watson 	lomac_set_single(dest, MAC_LOMAC_TYPE_EQUAL, 0);
1520eb320b0eSRobert Watson }
1521eb320b0eSRobert Watson 
1522eb320b0eSRobert Watson static void
lomac_netinet6_nd6_send(struct ifnet * ifp,struct label * ifplabel,struct mbuf * m,struct label * mlabel)1523eb320b0eSRobert Watson lomac_netinet6_nd6_send(struct ifnet *ifp, struct label *ifplabel,
1524eb320b0eSRobert Watson     struct mbuf *m, struct label *mlabel)
1525eb320b0eSRobert Watson {
1526eb320b0eSRobert Watson 	struct mac_lomac *dest;
1527eb320b0eSRobert Watson 
1528eb320b0eSRobert Watson 	dest = SLOT(mlabel);
1529eb320b0eSRobert Watson 
1530eb320b0eSRobert Watson 	lomac_set_single(dest, MAC_LOMAC_TYPE_EQUAL, 0);
1531eb320b0eSRobert Watson }
1532eb320b0eSRobert Watson 
1533db2661ceSRobert Watson static int
lomac_pipe_check_ioctl(struct ucred * cred,struct pipepair * pp,struct label * pplabel,unsigned long cmd,void * data)15343f1a7a90SRobert Watson lomac_pipe_check_ioctl(struct ucred *cred, struct pipepair *pp,
153578007886SRobert Watson     struct label *pplabel, unsigned long cmd, void /* caddr_t */ *data)
1536db2661ceSRobert Watson {
1537db2661ceSRobert Watson 
15383f1a7a90SRobert Watson 	if (!lomac_enabled)
1539db2661ceSRobert Watson 		return (0);
1540db2661ceSRobert Watson 
1541db2661ceSRobert Watson 	/* XXX: This will be implemented soon... */
1542db2661ceSRobert Watson 
1543db2661ceSRobert Watson 	return (0);
1544db2661ceSRobert Watson }
1545db2661ceSRobert Watson 
1546db2661ceSRobert Watson static int
lomac_pipe_check_read(struct ucred * cred,struct pipepair * pp,struct label * pplabel)15473f1a7a90SRobert Watson lomac_pipe_check_read(struct ucred *cred, struct pipepair *pp,
154878007886SRobert Watson     struct label *pplabel)
1549db2661ceSRobert Watson {
1550db2661ceSRobert Watson 	struct mac_lomac *subj, *obj;
1551db2661ceSRobert Watson 
15523f1a7a90SRobert Watson 	if (!lomac_enabled)
1553db2661ceSRobert Watson 		return (0);
1554db2661ceSRobert Watson 
1555eca8a663SRobert Watson 	subj = SLOT(cred->cr_label);
155678007886SRobert Watson 	obj = SLOT(pplabel);
1557db2661ceSRobert Watson 
15583f1a7a90SRobert Watson 	if (!lomac_dominate_single(obj, subj))
1559db2661ceSRobert Watson 		return (maybe_demote(subj, obj, "reading", "pipe", NULL));
1560db2661ceSRobert Watson 
1561db2661ceSRobert Watson 	return (0);
1562db2661ceSRobert Watson }
1563db2661ceSRobert Watson 
1564db2661ceSRobert Watson static int
lomac_pipe_check_relabel(struct ucred * cred,struct pipepair * pp,struct label * pplabel,struct label * newlabel)15653f1a7a90SRobert Watson lomac_pipe_check_relabel(struct ucred *cred, struct pipepair *pp,
156678007886SRobert Watson     struct label *pplabel, struct label *newlabel)
1567db2661ceSRobert Watson {
1568db2661ceSRobert Watson 	struct mac_lomac *subj, *obj, *new;
1569db2661ceSRobert Watson 	int error;
1570db2661ceSRobert Watson 
1571db2661ceSRobert Watson 	new = SLOT(newlabel);
1572eca8a663SRobert Watson 	subj = SLOT(cred->cr_label);
157378007886SRobert Watson 	obj = SLOT(pplabel);
1574db2661ceSRobert Watson 
1575db2661ceSRobert Watson 	/*
15763f1a7a90SRobert Watson 	 * If there is a LOMAC label update for a pipe, it must be a single
15773f1a7a90SRobert Watson 	 * update.
1578db2661ceSRobert Watson 	 */
1579db2661ceSRobert Watson 	error = lomac_atmostflags(new, MAC_LOMAC_FLAG_SINGLE);
1580db2661ceSRobert Watson 	if (error)
1581db2661ceSRobert Watson 		return (error);
1582db2661ceSRobert Watson 
1583db2661ceSRobert Watson 	/*
1584db2661ceSRobert Watson 	 * To perform a relabel of a pipe (LOMAC label or not), LOMAC must
1585db2661ceSRobert Watson 	 * authorize the relabel.
1586db2661ceSRobert Watson 	 */
15873f1a7a90SRobert Watson 	if (!lomac_single_in_range(obj, subj))
1588db2661ceSRobert Watson 		return (EPERM);
1589db2661ceSRobert Watson 
1590db2661ceSRobert Watson 	/*
1591db2661ceSRobert Watson 	 * If the LOMAC label is to be changed, authorize as appropriate.
1592db2661ceSRobert Watson 	 */
1593db2661ceSRobert Watson 	if (new->ml_flags & MAC_LOMAC_FLAG_SINGLE) {
1594db2661ceSRobert Watson 		/*
1595db2661ceSRobert Watson 		 * To change the LOMAC label on a pipe, the new pipe label
1596db2661ceSRobert Watson 		 * must be in the subject range.
1597db2661ceSRobert Watson 		 */
15983f1a7a90SRobert Watson 		if (!lomac_single_in_range(new, subj))
1599db2661ceSRobert Watson 			return (EPERM);
1600db2661ceSRobert Watson 
1601db2661ceSRobert Watson 		/*
1602db2661ceSRobert Watson 		 * To change the LOMAC label on a pipe to be EQUAL, the
1603db2661ceSRobert Watson 		 * subject must have appropriate privilege.
1604db2661ceSRobert Watson 		 */
16053f1a7a90SRobert Watson 		if (lomac_contains_equal(new)) {
16063f1a7a90SRobert Watson 			error = lomac_subject_privileged(subj);
1607db2661ceSRobert Watson 			if (error)
1608db2661ceSRobert Watson 				return (error);
1609db2661ceSRobert Watson 		}
1610db2661ceSRobert Watson 	}
1611db2661ceSRobert Watson 
1612db2661ceSRobert Watson 	return (0);
1613db2661ceSRobert Watson }
1614db2661ceSRobert Watson 
1615db2661ceSRobert Watson static int
lomac_pipe_check_write(struct ucred * cred,struct pipepair * pp,struct label * pplabel)16163f1a7a90SRobert Watson lomac_pipe_check_write(struct ucred *cred, struct pipepair *pp,
161778007886SRobert Watson     struct label *pplabel)
1618db2661ceSRobert Watson {
1619db2661ceSRobert Watson 	struct mac_lomac *subj, *obj;
1620db2661ceSRobert Watson 
16213f1a7a90SRobert Watson 	if (!lomac_enabled)
1622db2661ceSRobert Watson 		return (0);
1623db2661ceSRobert Watson 
1624eca8a663SRobert Watson 	subj = SLOT(cred->cr_label);
162578007886SRobert Watson 	obj = SLOT(pplabel);
1626db2661ceSRobert Watson 
16273f1a7a90SRobert Watson 	if (!lomac_subject_dominate(subj, obj))
1628db2661ceSRobert Watson 		return (EACCES);
1629db2661ceSRobert Watson 
1630db2661ceSRobert Watson 	return (0);
1631db2661ceSRobert Watson }
1632db2661ceSRobert Watson 
1633eb320b0eSRobert Watson static void
lomac_pipe_create(struct ucred * cred,struct pipepair * pp,struct label * pplabel)1634eb320b0eSRobert Watson lomac_pipe_create(struct ucred *cred, struct pipepair *pp,
1635eb320b0eSRobert Watson     struct label *pplabel)
1636db2661ceSRobert Watson {
1637eb320b0eSRobert Watson 	struct mac_lomac *source, *dest;
1638db2661ceSRobert Watson 
1639eb320b0eSRobert Watson 	source = SLOT(cred->cr_label);
1640eb320b0eSRobert Watson 	dest = SLOT(pplabel);
1641db2661ceSRobert Watson 
1642eb320b0eSRobert Watson 	lomac_copy_single(source, dest);
1643db2661ceSRobert Watson }
1644db2661ceSRobert Watson 
1645eb320b0eSRobert Watson static void
lomac_pipe_relabel(struct ucred * cred,struct pipepair * pp,struct label * pplabel,struct label * newlabel)1646eb320b0eSRobert Watson lomac_pipe_relabel(struct ucred *cred, struct pipepair *pp,
1647eb320b0eSRobert Watson     struct label *pplabel, struct label *newlabel)
1648db2661ceSRobert Watson {
1649eb320b0eSRobert Watson 	struct mac_lomac *source, *dest;
1650db2661ceSRobert Watson 
1651eb320b0eSRobert Watson 	source = SLOT(newlabel);
1652eb320b0eSRobert Watson 	dest = SLOT(pplabel);
1653db2661ceSRobert Watson 
1654eb320b0eSRobert Watson 	try_relabel(source, dest);
1655db2661ceSRobert Watson }
1656db2661ceSRobert Watson 
1657c14d15aeSRobert Watson /*
1658c14d15aeSRobert Watson  * Some system privileges are allowed regardless of integrity grade; others
1659c14d15aeSRobert Watson  * are allowed only when running with privilege with respect to the LOMAC
1660c14d15aeSRobert Watson  * policy as they might otherwise allow bypassing of the integrity policy.
1661c14d15aeSRobert Watson  */
1662c14d15aeSRobert Watson static int
lomac_priv_check(struct ucred * cred,int priv)16633f1a7a90SRobert Watson lomac_priv_check(struct ucred *cred, int priv)
1664c14d15aeSRobert Watson {
1665c14d15aeSRobert Watson 	struct mac_lomac *subj;
1666c14d15aeSRobert Watson 	int error;
1667c14d15aeSRobert Watson 
16683f1a7a90SRobert Watson 	if (!lomac_enabled)
1669c14d15aeSRobert Watson 		return (0);
1670c14d15aeSRobert Watson 
1671c14d15aeSRobert Watson 	/*
1672c14d15aeSRobert Watson 	 * Exempt only specific privileges from the LOMAC integrity policy.
1673c14d15aeSRobert Watson 	 */
1674c14d15aeSRobert Watson 	switch (priv) {
1675c14d15aeSRobert Watson 	case PRIV_KTRACE:
1676c14d15aeSRobert Watson 	case PRIV_MSGBUF:
1677c14d15aeSRobert Watson 
1678c14d15aeSRobert Watson 	/*
1679c14d15aeSRobert Watson 	 * Allow processes to manipulate basic process audit properties, and
1680c14d15aeSRobert Watson 	 * to submit audit records.
1681c14d15aeSRobert Watson 	 */
1682c14d15aeSRobert Watson 	case PRIV_AUDIT_GETAUDIT:
1683c14d15aeSRobert Watson 	case PRIV_AUDIT_SETAUDIT:
1684c14d15aeSRobert Watson 	case PRIV_AUDIT_SUBMIT:
1685c14d15aeSRobert Watson 
1686c14d15aeSRobert Watson 	/*
1687c14d15aeSRobert Watson 	 * Allow processes to manipulate their regular UNIX credentials.
1688c14d15aeSRobert Watson 	 */
1689c14d15aeSRobert Watson 	case PRIV_CRED_SETUID:
1690c14d15aeSRobert Watson 	case PRIV_CRED_SETEUID:
1691c14d15aeSRobert Watson 	case PRIV_CRED_SETGID:
1692c14d15aeSRobert Watson 	case PRIV_CRED_SETEGID:
1693c14d15aeSRobert Watson 	case PRIV_CRED_SETGROUPS:
1694c14d15aeSRobert Watson 	case PRIV_CRED_SETREUID:
1695c14d15aeSRobert Watson 	case PRIV_CRED_SETREGID:
1696c14d15aeSRobert Watson 	case PRIV_CRED_SETRESUID:
1697c14d15aeSRobert Watson 	case PRIV_CRED_SETRESGID:
1698c14d15aeSRobert Watson 
1699c14d15aeSRobert Watson 	/*
1700c14d15aeSRobert Watson 	 * Allow processes to perform system monitoring.
1701c14d15aeSRobert Watson 	 */
1702c14d15aeSRobert Watson 	case PRIV_SEEOTHERGIDS:
1703c14d15aeSRobert Watson 	case PRIV_SEEOTHERUIDS:
17047974ca1cSOlivier Certner 	case PRIV_SEEJAILPROC:
1705c14d15aeSRobert Watson 		break;
1706c14d15aeSRobert Watson 
1707c14d15aeSRobert Watson 	/*
1708c14d15aeSRobert Watson 	 * Allow access to general process debugging facilities.  We
1709c14d15aeSRobert Watson 	 * separately control debugging based on MAC label.
1710c14d15aeSRobert Watson 	 */
1711c14d15aeSRobert Watson 	case PRIV_DEBUG_DIFFCRED:
1712c14d15aeSRobert Watson 	case PRIV_DEBUG_SUGID:
1713c14d15aeSRobert Watson 	case PRIV_DEBUG_UNPRIV:
1714c14d15aeSRobert Watson 
1715c14d15aeSRobert Watson 	/*
1716c14d15aeSRobert Watson 	 * Allow manipulating jails.
1717c14d15aeSRobert Watson 	 */
1718c14d15aeSRobert Watson 	case PRIV_JAIL_ATTACH:
1719c14d15aeSRobert Watson 
1720c14d15aeSRobert Watson 	/*
1721c14d15aeSRobert Watson 	 * Allow privilege with respect to the Partition policy, but not the
1722c14d15aeSRobert Watson 	 * Privs policy.
1723c14d15aeSRobert Watson 	 */
1724c14d15aeSRobert Watson 	case PRIV_MAC_PARTITION:
1725c14d15aeSRobert Watson 
1726c14d15aeSRobert Watson 	/*
1727c14d15aeSRobert Watson 	 * Allow privilege with respect to process resource limits and login
1728c14d15aeSRobert Watson 	 * context.
1729c14d15aeSRobert Watson 	 */
1730c14d15aeSRobert Watson 	case PRIV_PROC_LIMIT:
1731c14d15aeSRobert Watson 	case PRIV_PROC_SETLOGIN:
1732c14d15aeSRobert Watson 	case PRIV_PROC_SETRLIMIT:
1733c14d15aeSRobert Watson 
1734c14d15aeSRobert Watson 	/*
1735c14d15aeSRobert Watson 	 * Allow System V and POSIX IPC privileges.
1736c14d15aeSRobert Watson 	 */
1737c14d15aeSRobert Watson 	case PRIV_IPC_READ:
1738c14d15aeSRobert Watson 	case PRIV_IPC_WRITE:
1739c14d15aeSRobert Watson 	case PRIV_IPC_ADMIN:
1740c14d15aeSRobert Watson 	case PRIV_IPC_MSGSIZE:
1741c14d15aeSRobert Watson 	case PRIV_MQ_ADMIN:
1742c14d15aeSRobert Watson 
1743c14d15aeSRobert Watson 	/*
1744c14d15aeSRobert Watson 	 * Allow certain scheduler manipulations -- possibly this should be
1745c14d15aeSRobert Watson 	 * controlled by more fine-grained policy, as potentially low
1746c14d15aeSRobert Watson 	 * integrity processes can deny CPU to higher integrity ones.
1747c14d15aeSRobert Watson 	 */
1748c14d15aeSRobert Watson 	case PRIV_SCHED_DIFFCRED:
1749c14d15aeSRobert Watson 	case PRIV_SCHED_SETPRIORITY:
1750c14d15aeSRobert Watson 	case PRIV_SCHED_RTPRIO:
1751c14d15aeSRobert Watson 	case PRIV_SCHED_SETPOLICY:
1752c14d15aeSRobert Watson 	case PRIV_SCHED_SET:
1753c14d15aeSRobert Watson 	case PRIV_SCHED_SETPARAM:
1754a20a2450SFlorian Walpen 	case PRIV_SCHED_IDPRIO:
1755c14d15aeSRobert Watson 
1756c14d15aeSRobert Watson 	/*
1757c14d15aeSRobert Watson 	 * More IPC privileges.
1758c14d15aeSRobert Watson 	 */
1759c14d15aeSRobert Watson 	case PRIV_SEM_WRITE:
1760c14d15aeSRobert Watson 
1761c14d15aeSRobert Watson 	/*
1762c14d15aeSRobert Watson 	 * Allow signaling privileges subject to integrity policy.
1763c14d15aeSRobert Watson 	 */
1764c14d15aeSRobert Watson 	case PRIV_SIGNAL_DIFFCRED:
1765c14d15aeSRobert Watson 	case PRIV_SIGNAL_SUGID:
1766c14d15aeSRobert Watson 
1767c14d15aeSRobert Watson 	/*
1768c14d15aeSRobert Watson 	 * Allow access to only limited sysctls from lower integrity levels;
1769c14d15aeSRobert Watson 	 * piggy-back on the Jail definition.
1770c14d15aeSRobert Watson 	 */
1771c14d15aeSRobert Watson 	case PRIV_SYSCTL_WRITEJAIL:
1772c14d15aeSRobert Watson 
1773c14d15aeSRobert Watson 	/*
1774c14d15aeSRobert Watson 	 * Allow TTY-based privileges, subject to general device access using
1775c14d15aeSRobert Watson 	 * labels on TTY device nodes, but not console privilege.
1776c14d15aeSRobert Watson 	 */
1777c14d15aeSRobert Watson 	case PRIV_TTY_DRAINWAIT:
1778c14d15aeSRobert Watson 	case PRIV_TTY_DTRWAIT:
1779c14d15aeSRobert Watson 	case PRIV_TTY_EXCLUSIVE:
1780c14d15aeSRobert Watson 	case PRIV_TTY_STI:
1781c14d15aeSRobert Watson 	case PRIV_TTY_SETA:
1782c14d15aeSRobert Watson 
1783c14d15aeSRobert Watson 	/*
1784c14d15aeSRobert Watson 	 * Grant most VFS privileges, as almost all are in practice bounded
1785c14d15aeSRobert Watson 	 * by more specific checks using labels.
1786c14d15aeSRobert Watson 	 */
1787c14d15aeSRobert Watson 	case PRIV_VFS_READ:
1788c14d15aeSRobert Watson 	case PRIV_VFS_WRITE:
1789c14d15aeSRobert Watson 	case PRIV_VFS_ADMIN:
1790c14d15aeSRobert Watson 	case PRIV_VFS_EXEC:
1791c14d15aeSRobert Watson 	case PRIV_VFS_LOOKUP:
1792c14d15aeSRobert Watson 	case PRIV_VFS_CHFLAGS_DEV:
1793c14d15aeSRobert Watson 	case PRIV_VFS_CHOWN:
1794c14d15aeSRobert Watson 	case PRIV_VFS_CHROOT:
1795c14d15aeSRobert Watson 	case PRIV_VFS_RETAINSUGID:
1796c14d15aeSRobert Watson 	case PRIV_VFS_EXCEEDQUOTA:
1797c14d15aeSRobert Watson 	case PRIV_VFS_FCHROOT:
1798c14d15aeSRobert Watson 	case PRIV_VFS_FHOPEN:
1799c14d15aeSRobert Watson 	case PRIV_VFS_FHSTATFS:
1800c14d15aeSRobert Watson 	case PRIV_VFS_GENERATION:
1801c14d15aeSRobert Watson 	case PRIV_VFS_GETFH:
1802c14d15aeSRobert Watson 	case PRIV_VFS_GETQUOTA:
1803c14d15aeSRobert Watson 	case PRIV_VFS_LINK:
1804c14d15aeSRobert Watson 	case PRIV_VFS_MOUNT:
1805c14d15aeSRobert Watson 	case PRIV_VFS_MOUNT_OWNER:
1806c14d15aeSRobert Watson 	case PRIV_VFS_MOUNT_PERM:
1807c14d15aeSRobert Watson 	case PRIV_VFS_MOUNT_SUIDDIR:
1808c14d15aeSRobert Watson 	case PRIV_VFS_MOUNT_NONUSER:
1809c14d15aeSRobert Watson 	case PRIV_VFS_SETGID:
1810c14d15aeSRobert Watson 	case PRIV_VFS_STICKYFILE:
1811c14d15aeSRobert Watson 	case PRIV_VFS_SYSFLAGS:
1812c14d15aeSRobert Watson 	case PRIV_VFS_UNMOUNT:
1813c14d15aeSRobert Watson 
1814c14d15aeSRobert Watson 	/*
1815c14d15aeSRobert Watson 	 * Allow VM privileges; it would be nice if these were subject to
1816c14d15aeSRobert Watson 	 * resource limits.
1817c14d15aeSRobert Watson 	 */
1818c14d15aeSRobert Watson 	case PRIV_VM_MADV_PROTECT:
1819c14d15aeSRobert Watson 	case PRIV_VM_MLOCK:
1820c14d15aeSRobert Watson 	case PRIV_VM_MUNLOCK:
18213364c323SKonstantin Belousov 	case PRIV_VM_SWAP_NOQUOTA:
18223364c323SKonstantin Belousov 	case PRIV_VM_SWAP_NORLIMIT:
1823c14d15aeSRobert Watson 
1824c14d15aeSRobert Watson 	/*
1825c14d15aeSRobert Watson 	 * Allow some but not all network privileges.  In general, dont allow
1826c14d15aeSRobert Watson 	 * reconfiguring the network stack, just normal use.
1827c14d15aeSRobert Watson 	 */
1828c14d15aeSRobert Watson 	case PRIV_NETINET_RESERVEDPORT:
1829c14d15aeSRobert Watson 	case PRIV_NETINET_RAW:
1830c14d15aeSRobert Watson 	case PRIV_NETINET_REUSEPORT:
1831c14d15aeSRobert Watson 		break;
1832c14d15aeSRobert Watson 
1833c14d15aeSRobert Watson 	/*
1834c14d15aeSRobert Watson 	 * All remaining system privileges are allow only if the process
1835c14d15aeSRobert Watson 	 * holds privilege with respect to the LOMAC policy.
1836c14d15aeSRobert Watson 	 */
1837c14d15aeSRobert Watson 	default:
1838c14d15aeSRobert Watson 		subj = SLOT(cred->cr_label);
18393f1a7a90SRobert Watson 		error = lomac_subject_privileged(subj);
1840c14d15aeSRobert Watson 		if (error)
1841c14d15aeSRobert Watson 			return (error);
1842c14d15aeSRobert Watson 	}
1843c14d15aeSRobert Watson 	return (0);
1844c14d15aeSRobert Watson }
1845c14d15aeSRobert Watson 
1846eb320b0eSRobert Watson static int
lomac_proc_check_debug(struct ucred * cred,struct proc * p)1847eb320b0eSRobert Watson lomac_proc_check_debug(struct ucred *cred, struct proc *p)
1848eb320b0eSRobert Watson {
1849eb320b0eSRobert Watson 	struct mac_lomac *subj, *obj;
1850eb320b0eSRobert Watson 
1851eb320b0eSRobert Watson 	if (!lomac_enabled)
1852eb320b0eSRobert Watson 		return (0);
1853eb320b0eSRobert Watson 
1854eb320b0eSRobert Watson 	subj = SLOT(cred->cr_label);
1855eb320b0eSRobert Watson 	obj = SLOT(p->p_ucred->cr_label);
1856eb320b0eSRobert Watson 
1857eb320b0eSRobert Watson 	/* XXX: range checks */
1858eb320b0eSRobert Watson 	if (!lomac_dominate_single(obj, subj))
1859eb320b0eSRobert Watson 		return (ESRCH);
1860eb320b0eSRobert Watson 	if (!lomac_subject_dominate(subj, obj))
1861eb320b0eSRobert Watson 		return (EACCES);
1862eb320b0eSRobert Watson 
1863eb320b0eSRobert Watson 	return (0);
1864eb320b0eSRobert Watson }
1865eb320b0eSRobert Watson 
1866eb320b0eSRobert Watson static int
lomac_proc_check_sched(struct ucred * cred,struct proc * p)1867eb320b0eSRobert Watson lomac_proc_check_sched(struct ucred *cred, struct proc *p)
1868eb320b0eSRobert Watson {
1869eb320b0eSRobert Watson 	struct mac_lomac *subj, *obj;
1870eb320b0eSRobert Watson 
1871eb320b0eSRobert Watson 	if (!lomac_enabled)
1872eb320b0eSRobert Watson 		return (0);
1873eb320b0eSRobert Watson 
1874eb320b0eSRobert Watson 	subj = SLOT(cred->cr_label);
1875eb320b0eSRobert Watson 	obj = SLOT(p->p_ucred->cr_label);
1876eb320b0eSRobert Watson 
1877eb320b0eSRobert Watson 	/* XXX: range checks */
1878eb320b0eSRobert Watson 	if (!lomac_dominate_single(obj, subj))
1879eb320b0eSRobert Watson 		return (ESRCH);
1880eb320b0eSRobert Watson 	if (!lomac_subject_dominate(subj, obj))
1881eb320b0eSRobert Watson 		return (EACCES);
1882eb320b0eSRobert Watson 
1883eb320b0eSRobert Watson 	return (0);
1884eb320b0eSRobert Watson }
1885eb320b0eSRobert Watson 
1886eb320b0eSRobert Watson static int
lomac_proc_check_signal(struct ucred * cred,struct proc * p,int signum)1887eb320b0eSRobert Watson lomac_proc_check_signal(struct ucred *cred, struct proc *p, int signum)
1888eb320b0eSRobert Watson {
1889eb320b0eSRobert Watson 	struct mac_lomac *subj, *obj;
1890eb320b0eSRobert Watson 
1891eb320b0eSRobert Watson 	if (!lomac_enabled)
1892eb320b0eSRobert Watson 		return (0);
1893eb320b0eSRobert Watson 
1894eb320b0eSRobert Watson 	subj = SLOT(cred->cr_label);
1895eb320b0eSRobert Watson 	obj = SLOT(p->p_ucred->cr_label);
1896eb320b0eSRobert Watson 
1897eb320b0eSRobert Watson 	/* XXX: range checks */
1898eb320b0eSRobert Watson 	if (!lomac_dominate_single(obj, subj))
1899eb320b0eSRobert Watson 		return (ESRCH);
1900eb320b0eSRobert Watson 	if (!lomac_subject_dominate(subj, obj))
1901eb320b0eSRobert Watson 		return (EACCES);
1902eb320b0eSRobert Watson 
1903eb320b0eSRobert Watson 	return (0);
1904eb320b0eSRobert Watson }
1905eb320b0eSRobert Watson 
1906eb320b0eSRobert Watson static void
lomac_proc_destroy_label(struct label * label)1907eb320b0eSRobert Watson lomac_proc_destroy_label(struct label *label)
1908eb320b0eSRobert Watson {
1909eb320b0eSRobert Watson 
1910eb320b0eSRobert Watson 	mtx_destroy(&PSLOT(label)->mtx);
19111ede983cSDag-Erling Smørgrav 	free(PSLOT(label), M_LOMAC);
1912eb320b0eSRobert Watson 	PSLOT_SET(label, NULL);
1913eb320b0eSRobert Watson }
1914eb320b0eSRobert Watson 
1915eb320b0eSRobert Watson static void
lomac_proc_init_label(struct label * label)1916eb320b0eSRobert Watson lomac_proc_init_label(struct label *label)
1917eb320b0eSRobert Watson {
1918eb320b0eSRobert Watson 
1919eb320b0eSRobert Watson 	PSLOT_SET(label, malloc(sizeof(struct mac_lomac_proc), M_LOMAC,
1920eb320b0eSRobert Watson 	    M_ZERO | M_WAITOK));
1921eb320b0eSRobert Watson 	mtx_init(&PSLOT(label)->mtx, "MAC/Lomac proc lock", NULL, MTX_DEF);
1922eb320b0eSRobert Watson }
1923eb320b0eSRobert Watson 
1924eb320b0eSRobert Watson static int
lomac_socket_check_deliver(struct socket * so,struct label * solabel,struct mbuf * m,struct label * mlabel)1925eb320b0eSRobert Watson lomac_socket_check_deliver(struct socket *so, struct label *solabel,
1926eb320b0eSRobert Watson     struct mbuf *m, struct label *mlabel)
1927eb320b0eSRobert Watson {
1928eb320b0eSRobert Watson 	struct mac_lomac *p, *s;
19293de40469SRobert Watson 	int error;
1930eb320b0eSRobert Watson 
1931eb320b0eSRobert Watson 	if (!lomac_enabled)
1932eb320b0eSRobert Watson 		return (0);
1933eb320b0eSRobert Watson 
1934eb320b0eSRobert Watson 	p = SLOT(mlabel);
1935eb320b0eSRobert Watson 	s = SLOT(solabel);
1936eb320b0eSRobert Watson 
19373de40469SRobert Watson 	SOCK_LOCK(so);
19383de40469SRobert Watson 	error = lomac_equal_single(p, s) ? 0 : EACCES;
19393de40469SRobert Watson 	SOCK_UNLOCK(so);
19403de40469SRobert Watson 	return (error);
1941eb320b0eSRobert Watson }
1942eb320b0eSRobert Watson 
1943eb320b0eSRobert Watson static int
lomac_socket_check_relabel(struct ucred * cred,struct socket * so,struct label * solabel,struct label * newlabel)1944eb320b0eSRobert Watson lomac_socket_check_relabel(struct ucred *cred, struct socket *so,
1945eb320b0eSRobert Watson     struct label *solabel, struct label *newlabel)
1946eb320b0eSRobert Watson {
1947eb320b0eSRobert Watson 	struct mac_lomac *subj, *obj, *new;
1948eb320b0eSRobert Watson 	int error;
1949eb320b0eSRobert Watson 
19503de40469SRobert Watson 	SOCK_LOCK_ASSERT(so);
19513de40469SRobert Watson 
1952eb320b0eSRobert Watson 	new = SLOT(newlabel);
1953eb320b0eSRobert Watson 	subj = SLOT(cred->cr_label);
1954eb320b0eSRobert Watson 	obj = SLOT(solabel);
1955eb320b0eSRobert Watson 
1956eb320b0eSRobert Watson 	/*
1957eb320b0eSRobert Watson 	 * If there is a LOMAC label update for the socket, it may be an
1958eb320b0eSRobert Watson 	 * update of single.
1959eb320b0eSRobert Watson 	 */
1960eb320b0eSRobert Watson 	error = lomac_atmostflags(new, MAC_LOMAC_FLAG_SINGLE);
1961eb320b0eSRobert Watson 	if (error)
1962eb320b0eSRobert Watson 		return (error);
1963eb320b0eSRobert Watson 
1964eb320b0eSRobert Watson 	/*
1965eb320b0eSRobert Watson 	 * To relabel a socket, the old socket single must be in the subject
1966eb320b0eSRobert Watson 	 * range.
1967eb320b0eSRobert Watson 	 */
1968eb320b0eSRobert Watson 	if (!lomac_single_in_range(obj, subj))
1969eb320b0eSRobert Watson 		return (EPERM);
1970eb320b0eSRobert Watson 
1971eb320b0eSRobert Watson 	/*
1972eb320b0eSRobert Watson 	 * If the LOMAC label is to be changed, authorize as appropriate.
1973eb320b0eSRobert Watson 	 */
1974eb320b0eSRobert Watson 	if (new->ml_flags & MAC_LOMAC_FLAG_SINGLE) {
1975eb320b0eSRobert Watson 		/*
1976eb320b0eSRobert Watson 		 * To relabel a socket, the new socket single must be in the
1977eb320b0eSRobert Watson 		 * subject range.
1978eb320b0eSRobert Watson 		 */
1979eb320b0eSRobert Watson 		if (!lomac_single_in_range(new, subj))
1980eb320b0eSRobert Watson 			return (EPERM);
1981eb320b0eSRobert Watson 
1982eb320b0eSRobert Watson 		/*
1983eb320b0eSRobert Watson 		 * To change the LOMAC label on the socket to contain EQUAL,
1984eb320b0eSRobert Watson 		 * the subject must have appropriate privilege.
1985eb320b0eSRobert Watson 		 */
1986eb320b0eSRobert Watson 		if (lomac_contains_equal(new)) {
1987eb320b0eSRobert Watson 			error = lomac_subject_privileged(subj);
1988eb320b0eSRobert Watson 			if (error)
1989eb320b0eSRobert Watson 				return (error);
1990eb320b0eSRobert Watson 		}
1991eb320b0eSRobert Watson 	}
1992eb320b0eSRobert Watson 
1993eb320b0eSRobert Watson 	return (0);
1994eb320b0eSRobert Watson }
1995eb320b0eSRobert Watson 
1996eb320b0eSRobert Watson static int
lomac_socket_check_visible(struct ucred * cred,struct socket * so,struct label * solabel)1997eb320b0eSRobert Watson lomac_socket_check_visible(struct ucred *cred, struct socket *so,
1998eb320b0eSRobert Watson     struct label *solabel)
1999eb320b0eSRobert Watson {
2000eb320b0eSRobert Watson 	struct mac_lomac *subj, *obj;
2001eb320b0eSRobert Watson 
2002eb320b0eSRobert Watson 	if (!lomac_enabled)
2003eb320b0eSRobert Watson 		return (0);
2004eb320b0eSRobert Watson 
2005eb320b0eSRobert Watson 	subj = SLOT(cred->cr_label);
2006eb320b0eSRobert Watson 	obj = SLOT(solabel);
2007eb320b0eSRobert Watson 
20083de40469SRobert Watson 	SOCK_LOCK(so);
20093de40469SRobert Watson 	if (!lomac_dominate_single(obj, subj)) {
20103de40469SRobert Watson 		SOCK_UNLOCK(so);
2011eb320b0eSRobert Watson 		return (ENOENT);
20123de40469SRobert Watson 	}
20133de40469SRobert Watson 	SOCK_UNLOCK(so);
2014eb320b0eSRobert Watson 
2015eb320b0eSRobert Watson 	return (0);
2016eb320b0eSRobert Watson }
2017eb320b0eSRobert Watson 
2018eb320b0eSRobert Watson static void
lomac_socket_create(struct ucred * cred,struct socket * so,struct label * solabel)2019eb320b0eSRobert Watson lomac_socket_create(struct ucred *cred, struct socket *so,
2020eb320b0eSRobert Watson     struct label *solabel)
2021eb320b0eSRobert Watson {
2022eb320b0eSRobert Watson 	struct mac_lomac *source, *dest;
2023eb320b0eSRobert Watson 
2024eb320b0eSRobert Watson 	source = SLOT(cred->cr_label);
2025eb320b0eSRobert Watson 	dest = SLOT(solabel);
2026eb320b0eSRobert Watson 
2027eb320b0eSRobert Watson 	lomac_copy_single(source, dest);
2028eb320b0eSRobert Watson }
2029eb320b0eSRobert Watson 
2030eb320b0eSRobert Watson static void
lomac_socket_create_mbuf(struct socket * so,struct label * solabel,struct mbuf * m,struct label * mlabel)2031eb320b0eSRobert Watson lomac_socket_create_mbuf(struct socket *so, struct label *solabel,
2032eb320b0eSRobert Watson     struct mbuf *m, struct label *mlabel)
2033eb320b0eSRobert Watson {
2034eb320b0eSRobert Watson 	struct mac_lomac *source, *dest;
2035eb320b0eSRobert Watson 
2036eb320b0eSRobert Watson 	source = SLOT(solabel);
2037eb320b0eSRobert Watson 	dest = SLOT(mlabel);
2038eb320b0eSRobert Watson 
20393de40469SRobert Watson 	SOCK_LOCK(so);
2040eb320b0eSRobert Watson 	lomac_copy_single(source, dest);
20413de40469SRobert Watson 	SOCK_UNLOCK(so);
2042eb320b0eSRobert Watson }
2043eb320b0eSRobert Watson 
2044eb320b0eSRobert Watson static void
lomac_socket_newconn(struct socket * oldso,struct label * oldsolabel,struct socket * newso,struct label * newsolabel)2045eb320b0eSRobert Watson lomac_socket_newconn(struct socket *oldso, struct label *oldsolabel,
2046eb320b0eSRobert Watson     struct socket *newso, struct label *newsolabel)
2047eb320b0eSRobert Watson {
20483de40469SRobert Watson 	struct mac_lomac source, *dest;
2049eb320b0eSRobert Watson 
20503de40469SRobert Watson 	SOCK_LOCK(oldso);
20513de40469SRobert Watson 	source = *SLOT(oldsolabel);
20523de40469SRobert Watson 	SOCK_UNLOCK(oldso);
20533de40469SRobert Watson 
2054eb320b0eSRobert Watson 	dest = SLOT(newsolabel);
2055eb320b0eSRobert Watson 
20563de40469SRobert Watson 	SOCK_LOCK(newso);
20573de40469SRobert Watson 	lomac_copy_single(&source, dest);
20583de40469SRobert Watson 	SOCK_UNLOCK(newso);
2059eb320b0eSRobert Watson }
2060eb320b0eSRobert Watson 
2061eb320b0eSRobert Watson static void
lomac_socket_relabel(struct ucred * cred,struct socket * so,struct label * solabel,struct label * newlabel)2062eb320b0eSRobert Watson lomac_socket_relabel(struct ucred *cred, struct socket *so,
2063eb320b0eSRobert Watson     struct label *solabel, struct label *newlabel)
2064eb320b0eSRobert Watson {
2065eb320b0eSRobert Watson 	struct mac_lomac *source, *dest;
2066eb320b0eSRobert Watson 
20673de40469SRobert Watson 	SOCK_LOCK_ASSERT(so);
20683de40469SRobert Watson 
2069eb320b0eSRobert Watson 	source = SLOT(newlabel);
2070eb320b0eSRobert Watson 	dest = SLOT(solabel);
2071eb320b0eSRobert Watson 
2072eb320b0eSRobert Watson 	try_relabel(source, dest);
2073eb320b0eSRobert Watson }
2074eb320b0eSRobert Watson 
2075eb320b0eSRobert Watson static void
lomac_socketpeer_set_from_mbuf(struct mbuf * m,struct label * mlabel,struct socket * so,struct label * sopeerlabel)2076eb320b0eSRobert Watson lomac_socketpeer_set_from_mbuf(struct mbuf *m, struct label *mlabel,
2077eb320b0eSRobert Watson     struct socket *so, struct label *sopeerlabel)
2078eb320b0eSRobert Watson {
2079eb320b0eSRobert Watson 	struct mac_lomac *source, *dest;
2080eb320b0eSRobert Watson 
2081eb320b0eSRobert Watson 	source = SLOT(mlabel);
2082eb320b0eSRobert Watson 	dest = SLOT(sopeerlabel);
2083eb320b0eSRobert Watson 
20843de40469SRobert Watson 	SOCK_LOCK(so);
2085eb320b0eSRobert Watson 	lomac_copy_single(source, dest);
20863de40469SRobert Watson 	SOCK_UNLOCK(so);
2087eb320b0eSRobert Watson }
2088eb320b0eSRobert Watson 
2089eb320b0eSRobert Watson static void
lomac_socketpeer_set_from_socket(struct socket * oldso,struct label * oldsolabel,struct socket * newso,struct label * newsopeerlabel)2090eb320b0eSRobert Watson lomac_socketpeer_set_from_socket(struct socket *oldso,
2091eb320b0eSRobert Watson     struct label *oldsolabel, struct socket *newso,
2092eb320b0eSRobert Watson     struct label *newsopeerlabel)
2093eb320b0eSRobert Watson {
20943de40469SRobert Watson 	struct mac_lomac source, *dest;
2095eb320b0eSRobert Watson 
20963de40469SRobert Watson 	SOCK_LOCK(oldso);
20973de40469SRobert Watson 	source = *SLOT(oldsolabel);
20983de40469SRobert Watson 	SOCK_UNLOCK(oldso);
20993de40469SRobert Watson 
2100eb320b0eSRobert Watson 	dest = SLOT(newsopeerlabel);
2101eb320b0eSRobert Watson 
21023de40469SRobert Watson 	SOCK_LOCK(newso);
21033de40469SRobert Watson 	lomac_copy_single(&source, dest);
21043de40469SRobert Watson 	SOCK_UNLOCK(newso);
2105eb320b0eSRobert Watson }
2106eb320b0eSRobert Watson 
2107eb320b0eSRobert Watson static void
lomac_syncache_create(struct label * label,struct inpcb * inp)2108eb320b0eSRobert Watson lomac_syncache_create(struct label *label, struct inpcb *inp)
2109eb320b0eSRobert Watson {
2110eb320b0eSRobert Watson 	struct mac_lomac *source, *dest;
2111eb320b0eSRobert Watson 
2112eb320b0eSRobert Watson 	source = SLOT(inp->inp_label);
2113eb320b0eSRobert Watson 	dest = SLOT(label);
2114eb320b0eSRobert Watson 	lomac_copy(source, dest);
2115eb320b0eSRobert Watson }
2116eb320b0eSRobert Watson 
2117eb320b0eSRobert Watson static void
lomac_syncache_create_mbuf(struct label * sc_label,struct mbuf * m,struct label * mlabel)2118eb320b0eSRobert Watson lomac_syncache_create_mbuf(struct label *sc_label, struct mbuf *m,
2119eb320b0eSRobert Watson     struct label *mlabel)
2120eb320b0eSRobert Watson {
2121eb320b0eSRobert Watson 	struct mac_lomac *source, *dest;
2122eb320b0eSRobert Watson 
2123eb320b0eSRobert Watson 	source = SLOT(sc_label);
2124eb320b0eSRobert Watson 	dest = SLOT(mlabel);
2125eb320b0eSRobert Watson 	lomac_copy(source, dest);
2126eb320b0eSRobert Watson }
2127c14d15aeSRobert Watson 
2128db2661ceSRobert Watson static int
lomac_system_check_acct(struct ucred * cred,struct vnode * vp,struct label * vplabel)21293f1a7a90SRobert Watson lomac_system_check_acct(struct ucred *cred, struct vnode *vp,
213078007886SRobert Watson     struct label *vplabel)
213118717f69SRobert Watson {
213218717f69SRobert Watson 	struct mac_lomac *subj, *obj;
213318717f69SRobert Watson 
21343f1a7a90SRobert Watson 	if (!lomac_enabled)
213518717f69SRobert Watson 		return (0);
213618717f69SRobert Watson 
213718717f69SRobert Watson 	subj = SLOT(cred->cr_label);
213878007886SRobert Watson 	obj = SLOT(vplabel);
213918717f69SRobert Watson 
21403f1a7a90SRobert Watson 	if (lomac_subject_privileged(subj))
214118717f69SRobert Watson 		return (EPERM);
214218717f69SRobert Watson 
21433f1a7a90SRobert Watson 	if (!lomac_high_single(obj))
214418717f69SRobert Watson 		return (EACCES);
214518717f69SRobert Watson 
214618717f69SRobert Watson 	return (0);
214718717f69SRobert Watson }
214818717f69SRobert Watson 
214918717f69SRobert Watson static int
lomac_system_check_auditctl(struct ucred * cred,struct vnode * vp,struct label * vplabel)21503f1a7a90SRobert Watson lomac_system_check_auditctl(struct ucred *cred, struct vnode *vp,
215178007886SRobert Watson     struct label *vplabel)
215218717f69SRobert Watson {
215318717f69SRobert Watson 	struct mac_lomac *subj, *obj;
215418717f69SRobert Watson 
21553f1a7a90SRobert Watson 	if (!lomac_enabled)
215618717f69SRobert Watson 		return (0);
215718717f69SRobert Watson 
215818717f69SRobert Watson 	subj = SLOT(cred->cr_label);
215978007886SRobert Watson 	obj = SLOT(vplabel);
216018717f69SRobert Watson 
21613f1a7a90SRobert Watson 	if (lomac_subject_privileged(subj))
216218717f69SRobert Watson 		return (EPERM);
216318717f69SRobert Watson 
21643f1a7a90SRobert Watson 	if (!lomac_high_single(obj))
216518717f69SRobert Watson 		return (EACCES);
216618717f69SRobert Watson 
216718717f69SRobert Watson 	return (0);
216818717f69SRobert Watson }
216918717f69SRobert Watson 
217018717f69SRobert Watson static int
lomac_system_check_swapoff(struct ucred * cred,struct vnode * vp,struct label * vplabel)21713f1a7a90SRobert Watson lomac_system_check_swapoff(struct ucred *cred, struct vnode *vp,
217278007886SRobert Watson     struct label *vplabel)
217318717f69SRobert Watson {
217418717f69SRobert Watson 	struct mac_lomac *subj;
217518717f69SRobert Watson 
21763f1a7a90SRobert Watson 	if (!lomac_enabled)
217718717f69SRobert Watson 		return (0);
217818717f69SRobert Watson 
217918717f69SRobert Watson 	subj = SLOT(cred->cr_label);
218018717f69SRobert Watson 
21813f1a7a90SRobert Watson 	if (lomac_subject_privileged(subj))
218218717f69SRobert Watson 		return (EPERM);
218318717f69SRobert Watson 
218418717f69SRobert Watson 	return (0);
218518717f69SRobert Watson }
218618717f69SRobert Watson 
218718717f69SRobert Watson static int
lomac_system_check_swapon(struct ucred * cred,struct vnode * vp,struct label * vplabel)21883f1a7a90SRobert Watson lomac_system_check_swapon(struct ucred *cred, struct vnode *vp,
218978007886SRobert Watson     struct label *vplabel)
2190db2661ceSRobert Watson {
2191db2661ceSRobert Watson 	struct mac_lomac *subj, *obj;
2192db2661ceSRobert Watson 
21933f1a7a90SRobert Watson 	if (!lomac_enabled)
2194db2661ceSRobert Watson 		return (0);
2195db2661ceSRobert Watson 
2196eca8a663SRobert Watson 	subj = SLOT(cred->cr_label);
219778007886SRobert Watson 	obj = SLOT(vplabel);
2198db2661ceSRobert Watson 
21993f1a7a90SRobert Watson 	if (lomac_subject_privileged(subj))
2200db2661ceSRobert Watson 		return (EPERM);
2201db2661ceSRobert Watson 
22023f1a7a90SRobert Watson 	if (!lomac_high_single(obj))
2203db2661ceSRobert Watson 		return (EACCES);
2204db2661ceSRobert Watson 
2205db2661ceSRobert Watson 	return (0);
2206db2661ceSRobert Watson }
2207db2661ceSRobert Watson 
2208db2661ceSRobert Watson static int
lomac_system_check_sysctl(struct ucred * cred,struct sysctl_oid * oidp,void * arg1,int arg2,struct sysctl_req * req)22093f1a7a90SRobert Watson lomac_system_check_sysctl(struct ucred *cred, struct sysctl_oid *oidp,
221063dba32bSPawel Jakub Dawidek     void *arg1, int arg2, struct sysctl_req *req)
2211db2661ceSRobert Watson {
2212db2661ceSRobert Watson 	struct mac_lomac *subj;
2213db2661ceSRobert Watson 
22143f1a7a90SRobert Watson 	if (!lomac_enabled)
2215db2661ceSRobert Watson 		return (0);
2216db2661ceSRobert Watson 
2217eca8a663SRobert Watson 	subj = SLOT(cred->cr_label);
2218db2661ceSRobert Watson 
2219db2661ceSRobert Watson 	/*
22203f1a7a90SRobert Watson 	 * Treat sysctl variables without CTLFLAG_ANYBODY flag as lomac/high,
22213f1a7a90SRobert Watson 	 * but also require privilege to change them.
2222db2661ceSRobert Watson 	 */
222363dba32bSPawel Jakub Dawidek 	if (req->newptr != NULL && (oidp->oid_kind & CTLFLAG_ANYBODY) == 0) {
2224db2661ceSRobert Watson #ifdef notdef
22253f1a7a90SRobert Watson 		if (!lomac_subject_dominate_high(subj))
2226db2661ceSRobert Watson 			return (EACCES);
2227db2661ceSRobert Watson #endif
2228db2661ceSRobert Watson 
22293f1a7a90SRobert Watson 		if (lomac_subject_privileged(subj))
2230db2661ceSRobert Watson 			return (EPERM);
2231db2661ceSRobert Watson 	}
2232db2661ceSRobert Watson 
2233db2661ceSRobert Watson 	return (0);
2234db2661ceSRobert Watson }
2235db2661ceSRobert Watson 
2236eb320b0eSRobert Watson static void
lomac_thread_userret(struct thread * td)2237eb320b0eSRobert Watson lomac_thread_userret(struct thread *td)
2238eb320b0eSRobert Watson {
2239eb320b0eSRobert Watson 	struct proc *p = td->td_proc;
2240eb320b0eSRobert Watson 	struct mac_lomac_proc *subj = PSLOT(p->p_label);
2241eb320b0eSRobert Watson 	struct ucred *newcred, *oldcred;
2242eb320b0eSRobert Watson 	int dodrop;
2243eb320b0eSRobert Watson 
2244eb320b0eSRobert Watson 	mtx_lock(&subj->mtx);
2245eb320b0eSRobert Watson 	if (subj->mac_lomac.ml_flags & MAC_LOMAC_FLAG_UPDATE) {
2246eb320b0eSRobert Watson 		dodrop = 0;
2247eb320b0eSRobert Watson 		mtx_unlock(&subj->mtx);
2248eb320b0eSRobert Watson 		newcred = crget();
2249eb320b0eSRobert Watson 		PROC_LOCK(p);
2250eb320b0eSRobert Watson 		mtx_lock(&subj->mtx);
2251eb320b0eSRobert Watson 		/*
2252eb320b0eSRobert Watson 		 * Check if we lost the race while allocating the cred.
2253eb320b0eSRobert Watson 		 */
2254eb320b0eSRobert Watson 		if ((subj->mac_lomac.ml_flags & MAC_LOMAC_FLAG_UPDATE) == 0) {
2255eb320b0eSRobert Watson 			crfree(newcred);
2256eb320b0eSRobert Watson 			goto out;
2257eb320b0eSRobert Watson 		}
2258eb320b0eSRobert Watson 		oldcred = p->p_ucred;
2259eb320b0eSRobert Watson 		crcopy(newcred, oldcred);
2260eb320b0eSRobert Watson 		crhold(newcred);
2261eb320b0eSRobert Watson 		lomac_copy(&subj->mac_lomac, SLOT(newcred->cr_label));
2262daf63fd2SMateusz Guzik 		proc_set_cred(p, newcred);
2263eb320b0eSRobert Watson 		crfree(oldcred);
2264eb320b0eSRobert Watson 		dodrop = 1;
2265eb320b0eSRobert Watson 	out:
2266eb320b0eSRobert Watson 		mtx_unlock(&subj->mtx);
2267eb320b0eSRobert Watson 		PROC_UNLOCK(p);
2268eb320b0eSRobert Watson 		if (dodrop)
22699215889dSRobert Watson 			mac_proc_vm_revoke(curthread);
2270eb320b0eSRobert Watson 	} else {
2271eb320b0eSRobert Watson 		mtx_unlock(&subj->mtx);
2272eb320b0eSRobert Watson 	}
2273eb320b0eSRobert Watson }
2274eb320b0eSRobert Watson 
2275eb320b0eSRobert Watson static int
lomac_vnode_associate_extattr(struct mount * mp,struct label * mplabel,struct vnode * vp,struct label * vplabel)2276eb320b0eSRobert Watson lomac_vnode_associate_extattr(struct mount *mp, struct label *mplabel,
2277eb320b0eSRobert Watson     struct vnode *vp, struct label *vplabel)
2278eb320b0eSRobert Watson {
2279eb320b0eSRobert Watson 	struct mac_lomac ml_temp, *source, *dest;
2280eb320b0eSRobert Watson 	int buflen, error;
2281eb320b0eSRobert Watson 
2282eb320b0eSRobert Watson 	source = SLOT(mplabel);
2283eb320b0eSRobert Watson 	dest = SLOT(vplabel);
2284eb320b0eSRobert Watson 
2285eb320b0eSRobert Watson 	buflen = sizeof(ml_temp);
2286eb320b0eSRobert Watson 	bzero(&ml_temp, buflen);
2287eb320b0eSRobert Watson 
2288eb320b0eSRobert Watson 	error = vn_extattr_get(vp, IO_NODELOCKED, MAC_LOMAC_EXTATTR_NAMESPACE,
2289eb320b0eSRobert Watson 	    MAC_LOMAC_EXTATTR_NAME, &buflen, (char *)&ml_temp, curthread);
2290eb320b0eSRobert Watson 	if (error == ENOATTR || error == EOPNOTSUPP) {
2291eb320b0eSRobert Watson 		/* Fall back to the mntlabel. */
2292eb320b0eSRobert Watson 		lomac_copy_single(source, dest);
2293eb320b0eSRobert Watson 		return (0);
2294eb320b0eSRobert Watson 	} else if (error)
2295eb320b0eSRobert Watson 		return (error);
2296eb320b0eSRobert Watson 
2297eb320b0eSRobert Watson 	if (buflen != sizeof(ml_temp)) {
2298eb320b0eSRobert Watson 		if (buflen != sizeof(ml_temp) - sizeof(ml_temp.ml_auxsingle)) {
2299eb320b0eSRobert Watson 			printf("lomac_vnode_associate_extattr: bad size %d\n",
2300eb320b0eSRobert Watson 			    buflen);
2301eb320b0eSRobert Watson 			return (EPERM);
2302eb320b0eSRobert Watson 		}
2303eb320b0eSRobert Watson 		bzero(&ml_temp.ml_auxsingle, sizeof(ml_temp.ml_auxsingle));
2304eb320b0eSRobert Watson 		buflen = sizeof(ml_temp);
2305eb320b0eSRobert Watson 		(void)vn_extattr_set(vp, IO_NODELOCKED,
2306eb320b0eSRobert Watson 		    MAC_LOMAC_EXTATTR_NAMESPACE, MAC_LOMAC_EXTATTR_NAME,
2307eb320b0eSRobert Watson 		    buflen, (char *)&ml_temp, curthread);
2308eb320b0eSRobert Watson 	}
2309eb320b0eSRobert Watson 	if (lomac_valid(&ml_temp) != 0) {
2310eb320b0eSRobert Watson 		printf("lomac_vnode_associate_extattr: invalid\n");
2311eb320b0eSRobert Watson 		return (EPERM);
2312eb320b0eSRobert Watson 	}
2313eb320b0eSRobert Watson 	if ((ml_temp.ml_flags & MAC_LOMAC_FLAGS_BOTH) !=
2314eb320b0eSRobert Watson 	    MAC_LOMAC_FLAG_SINGLE) {
2315eb320b0eSRobert Watson 		printf("lomac_vnode_associate_extattr: not single\n");
2316eb320b0eSRobert Watson 		return (EPERM);
2317eb320b0eSRobert Watson 	}
2318eb320b0eSRobert Watson 
2319eb320b0eSRobert Watson 	lomac_copy_single(&ml_temp, dest);
2320eb320b0eSRobert Watson 	return (0);
2321eb320b0eSRobert Watson }
2322eb320b0eSRobert Watson 
2323eb320b0eSRobert Watson static void
lomac_vnode_associate_singlelabel(struct mount * mp,struct label * mplabel,struct vnode * vp,struct label * vplabel)2324eb320b0eSRobert Watson lomac_vnode_associate_singlelabel(struct mount *mp, struct label *mplabel,
2325eb320b0eSRobert Watson     struct vnode *vp, struct label *vplabel)
2326eb320b0eSRobert Watson {
2327eb320b0eSRobert Watson 	struct mac_lomac *source, *dest;
2328eb320b0eSRobert Watson 
2329eb320b0eSRobert Watson 	source = SLOT(mplabel);
2330eb320b0eSRobert Watson 	dest = SLOT(vplabel);
2331eb320b0eSRobert Watson 
2332eb320b0eSRobert Watson 	lomac_copy_single(source, dest);
2333eb320b0eSRobert Watson }
2334eb320b0eSRobert Watson 
2335db2661ceSRobert Watson static int
lomac_vnode_check_create(struct ucred * cred,struct vnode * dvp,struct label * dvplabel,struct componentname * cnp,struct vattr * vap)23363f1a7a90SRobert Watson lomac_vnode_check_create(struct ucred *cred, struct vnode *dvp,
233778007886SRobert Watson     struct label *dvplabel, struct componentname *cnp, struct vattr *vap)
2338db2661ceSRobert Watson {
2339db2661ceSRobert Watson 	struct mac_lomac *subj, *obj;
2340db2661ceSRobert Watson 
23413f1a7a90SRobert Watson 	if (!lomac_enabled)
2342db2661ceSRobert Watson 		return (0);
2343db2661ceSRobert Watson 
2344eca8a663SRobert Watson 	subj = SLOT(cred->cr_label);
234578007886SRobert Watson 	obj = SLOT(dvplabel);
2346db2661ceSRobert Watson 
23473f1a7a90SRobert Watson 	if (!lomac_subject_dominate(subj, obj))
2348db2661ceSRobert Watson 		return (EACCES);
2349db2661ceSRobert Watson 	if (obj->ml_flags & MAC_LOMAC_FLAG_AUX &&
23503f1a7a90SRobert Watson 	    !lomac_dominate_element(&subj->ml_single, &obj->ml_auxsingle))
2351db2661ceSRobert Watson 		return (EACCES);
2352db2661ceSRobert Watson 
2353db2661ceSRobert Watson 	return (0);
2354db2661ceSRobert Watson }
2355db2661ceSRobert Watson 
2356db2661ceSRobert Watson static int
lomac_vnode_check_deleteacl(struct ucred * cred,struct vnode * vp,struct label * vplabel,acl_type_t type)23573f1a7a90SRobert Watson lomac_vnode_check_deleteacl(struct ucred *cred, struct vnode *vp,
235878007886SRobert Watson     struct label *vplabel, acl_type_t type)
2359db2661ceSRobert Watson {
2360db2661ceSRobert Watson 	struct mac_lomac *subj, *obj;
2361db2661ceSRobert Watson 
23623f1a7a90SRobert Watson 	if (!lomac_enabled)
2363db2661ceSRobert Watson 		return (0);
2364db2661ceSRobert Watson 
2365eca8a663SRobert Watson 	subj = SLOT(cred->cr_label);
236678007886SRobert Watson 	obj = SLOT(vplabel);
2367db2661ceSRobert Watson 
23683f1a7a90SRobert Watson 	if (!lomac_subject_dominate(subj, obj))
2369db2661ceSRobert Watson 		return (EACCES);
2370db2661ceSRobert Watson 
2371db2661ceSRobert Watson 	return (0);
2372db2661ceSRobert Watson }
2373db2661ceSRobert Watson 
2374db2661ceSRobert Watson static int
lomac_vnode_check_link(struct ucred * cred,struct vnode * dvp,struct label * dvplabel,struct vnode * vp,struct label * vplabel,struct componentname * cnp)23753f1a7a90SRobert Watson lomac_vnode_check_link(struct ucred *cred, struct vnode *dvp,
237678007886SRobert Watson     struct label *dvplabel, struct vnode *vp, struct label *vplabel,
2377db2661ceSRobert Watson     struct componentname *cnp)
2378db2661ceSRobert Watson {
2379db2661ceSRobert Watson 	struct mac_lomac *subj, *obj;
2380db2661ceSRobert Watson 
23813f1a7a90SRobert Watson 	if (!lomac_enabled)
2382db2661ceSRobert Watson 		return (0);
2383db2661ceSRobert Watson 
2384eca8a663SRobert Watson 	subj = SLOT(cred->cr_label);
238578007886SRobert Watson 	obj = SLOT(dvplabel);
2386db2661ceSRobert Watson 
23873f1a7a90SRobert Watson 	if (!lomac_subject_dominate(subj, obj))
2388db2661ceSRobert Watson 		return (EACCES);
2389db2661ceSRobert Watson 
239078007886SRobert Watson 	obj = SLOT(vplabel);
2391db2661ceSRobert Watson 
23923f1a7a90SRobert Watson 	if (!lomac_subject_dominate(subj, obj))
2393db2661ceSRobert Watson 		return (EACCES);
2394db2661ceSRobert Watson 
2395db2661ceSRobert Watson 	return (0);
2396db2661ceSRobert Watson }
2397db2661ceSRobert Watson 
2398db2661ceSRobert Watson static int
lomac_vnode_check_mmap(struct ucred * cred,struct vnode * vp,struct label * vplabel,int prot,int flags)23993f1a7a90SRobert Watson lomac_vnode_check_mmap(struct ucred *cred, struct vnode *vp,
240078007886SRobert Watson     struct label *vplabel, int prot, int flags)
2401db2661ceSRobert Watson {
2402db2661ceSRobert Watson 	struct mac_lomac *subj, *obj;
2403db2661ceSRobert Watson 
2404db2661ceSRobert Watson 	/*
2405db2661ceSRobert Watson 	 * Rely on the use of open()-time protections to handle
2406db2661ceSRobert Watson 	 * non-revocation cases.
2407db2661ceSRobert Watson 	 */
24083f1a7a90SRobert Watson 	if (!lomac_enabled)
2409db2661ceSRobert Watson 		return (0);
2410db2661ceSRobert Watson 
2411eca8a663SRobert Watson 	subj = SLOT(cred->cr_label);
241278007886SRobert Watson 	obj = SLOT(vplabel);
2413db2661ceSRobert Watson 
2414c92163dcSChristian S.J. Peron 	if (((prot & VM_PROT_WRITE) != 0) && ((flags & MAP_SHARED) != 0)) {
24153f1a7a90SRobert Watson 		if (!lomac_subject_dominate(subj, obj))
2416db2661ceSRobert Watson 			return (EACCES);
2417db2661ceSRobert Watson 	}
2418db2661ceSRobert Watson 	if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
24193f1a7a90SRobert Watson 		if (!lomac_dominate_single(obj, subj))
2420db2661ceSRobert Watson 			return (maybe_demote(subj, obj, "mapping", "file", vp));
2421db2661ceSRobert Watson 	}
2422db2661ceSRobert Watson 
2423db2661ceSRobert Watson 	return (0);
2424db2661ceSRobert Watson }
2425db2661ceSRobert Watson 
2426db2661ceSRobert Watson static void
lomac_vnode_check_mmap_downgrade(struct ucred * cred,struct vnode * vp,struct label * vplabel,int * prot)24273f1a7a90SRobert Watson lomac_vnode_check_mmap_downgrade(struct ucred *cred, struct vnode *vp,
242878007886SRobert Watson     struct label *vplabel, /* XXX vm_prot_t */ int *prot)
2429db2661ceSRobert Watson {
2430db2661ceSRobert Watson 	struct mac_lomac *subj, *obj;
2431db2661ceSRobert Watson 
2432db2661ceSRobert Watson 	/*
2433db2661ceSRobert Watson 	 * Rely on the use of open()-time protections to handle
2434db2661ceSRobert Watson 	 * non-revocation cases.
2435db2661ceSRobert Watson 	 */
24363f1a7a90SRobert Watson 	if (!lomac_enabled || !revocation_enabled)
2437db2661ceSRobert Watson 		return;
2438db2661ceSRobert Watson 
2439eca8a663SRobert Watson 	subj = SLOT(cred->cr_label);
244078007886SRobert Watson 	obj = SLOT(vplabel);
2441db2661ceSRobert Watson 
24423f1a7a90SRobert Watson 	if (!lomac_subject_dominate(subj, obj))
2443db2661ceSRobert Watson 		*prot &= ~VM_PROT_WRITE;
2444db2661ceSRobert Watson }
2445db2661ceSRobert Watson 
2446db2661ceSRobert Watson static int
lomac_vnode_check_open(struct ucred * cred,struct vnode * vp,struct label * vplabel,accmode_t accmode)24473f1a7a90SRobert Watson lomac_vnode_check_open(struct ucred *cred, struct vnode *vp,
244815bc6b2bSEdward Tomasz Napierala     struct label *vplabel, accmode_t accmode)
2449db2661ceSRobert Watson {
2450db2661ceSRobert Watson 	struct mac_lomac *subj, *obj;
2451db2661ceSRobert Watson 
24523f1a7a90SRobert Watson 	if (!lomac_enabled)
2453db2661ceSRobert Watson 		return (0);
2454db2661ceSRobert Watson 
2455eca8a663SRobert Watson 	subj = SLOT(cred->cr_label);
245678007886SRobert Watson 	obj = SLOT(vplabel);
2457db2661ceSRobert Watson 
2458db2661ceSRobert Watson 	/* XXX privilege override for admin? */
24593a597bfcSEdward Tomasz Napierala 	if (accmode & VMODIFY_PERMS) {
24603f1a7a90SRobert Watson 		if (!lomac_subject_dominate(subj, obj))
2461db2661ceSRobert Watson 			return (EACCES);
2462db2661ceSRobert Watson 	}
2463db2661ceSRobert Watson 
2464db2661ceSRobert Watson 	return (0);
2465db2661ceSRobert Watson }
2466db2661ceSRobert Watson 
2467db2661ceSRobert Watson static int
lomac_vnode_check_read(struct ucred * active_cred,struct ucred * file_cred,struct vnode * vp,struct label * vplabel)24683f1a7a90SRobert Watson lomac_vnode_check_read(struct ucred *active_cred, struct ucred *file_cred,
246978007886SRobert Watson     struct vnode *vp, struct label *vplabel)
2470db2661ceSRobert Watson {
2471db2661ceSRobert Watson 	struct mac_lomac *subj, *obj;
2472db2661ceSRobert Watson 
24733f1a7a90SRobert Watson 	if (!lomac_enabled || !revocation_enabled)
2474db2661ceSRobert Watson 		return (0);
2475db2661ceSRobert Watson 
2476eca8a663SRobert Watson 	subj = SLOT(active_cred->cr_label);
247778007886SRobert Watson 	obj = SLOT(vplabel);
2478db2661ceSRobert Watson 
24793f1a7a90SRobert Watson 	if (!lomac_dominate_single(obj, subj))
2480db2661ceSRobert Watson 		return (maybe_demote(subj, obj, "reading", "file", vp));
2481db2661ceSRobert Watson 
2482db2661ceSRobert Watson 	return (0);
2483db2661ceSRobert Watson }
2484db2661ceSRobert Watson 
2485db2661ceSRobert Watson static int
lomac_vnode_check_relabel(struct ucred * cred,struct vnode * vp,struct label * vplabel,struct label * newlabel)24863f1a7a90SRobert Watson lomac_vnode_check_relabel(struct ucred *cred, struct vnode *vp,
248778007886SRobert Watson     struct label *vplabel, struct label *newlabel)
2488db2661ceSRobert Watson {
2489db2661ceSRobert Watson 	struct mac_lomac *old, *new, *subj;
2490db2661ceSRobert Watson 	int error;
2491db2661ceSRobert Watson 
249278007886SRobert Watson 	old = SLOT(vplabel);
2493db2661ceSRobert Watson 	new = SLOT(newlabel);
2494eca8a663SRobert Watson 	subj = SLOT(cred->cr_label);
2495db2661ceSRobert Watson 
2496db2661ceSRobert Watson 	/*
2497db2661ceSRobert Watson 	 * If there is a LOMAC label update for the vnode, it must be a
2498db2661ceSRobert Watson 	 * single label, with an optional explicit auxiliary single.
2499db2661ceSRobert Watson 	 */
2500db2661ceSRobert Watson 	error = lomac_atmostflags(new,
2501db2661ceSRobert Watson 	    MAC_LOMAC_FLAG_SINGLE | MAC_LOMAC_FLAG_AUX);
2502db2661ceSRobert Watson 	if (error)
2503db2661ceSRobert Watson 		return (error);
2504db2661ceSRobert Watson 
2505db2661ceSRobert Watson 	/*
2506db2661ceSRobert Watson 	 * To perform a relabel of the vnode (LOMAC label or not), LOMAC must
2507db2661ceSRobert Watson 	 * authorize the relabel.
2508db2661ceSRobert Watson 	 */
25093f1a7a90SRobert Watson 	if (!lomac_single_in_range(old, subj))
2510db2661ceSRobert Watson 		return (EPERM);
2511db2661ceSRobert Watson 
2512db2661ceSRobert Watson 	/*
2513db2661ceSRobert Watson 	 * If the LOMAC label is to be changed, authorize as appropriate.
2514db2661ceSRobert Watson 	 */
2515db2661ceSRobert Watson 	if (new->ml_flags & MAC_LOMAC_FLAG_SINGLE) {
2516db2661ceSRobert Watson 		/*
2517db2661ceSRobert Watson 		 * To change the LOMAC label on a vnode, the new vnode label
2518db2661ceSRobert Watson 		 * must be in the subject range.
2519db2661ceSRobert Watson 		 */
25203f1a7a90SRobert Watson 		if (!lomac_single_in_range(new, subj))
2521db2661ceSRobert Watson 			return (EPERM);
2522db2661ceSRobert Watson 
2523db2661ceSRobert Watson 		/*
25243f1a7a90SRobert Watson 		 * To change the LOMAC label on the vnode to be EQUAL, the
25253f1a7a90SRobert Watson 		 * subject must have appropriate privilege.
2526db2661ceSRobert Watson 		 */
25273f1a7a90SRobert Watson 		if (lomac_contains_equal(new)) {
25283f1a7a90SRobert Watson 			error = lomac_subject_privileged(subj);
2529db2661ceSRobert Watson 			if (error)
2530db2661ceSRobert Watson 				return (error);
2531db2661ceSRobert Watson 		}
2532db2661ceSRobert Watson 	}
2533db2661ceSRobert Watson 	if (new->ml_flags & MAC_LOMAC_FLAG_AUX) {
2534db2661ceSRobert Watson 		/*
253584bdb083SRobert Watson 		 * Fill in the missing parts from the previous label.
253684bdb083SRobert Watson 		 */
253784bdb083SRobert Watson 		if ((new->ml_flags & MAC_LOMAC_FLAG_SINGLE) == 0)
25383f1a7a90SRobert Watson 			lomac_copy_single(subj, new);
253984bdb083SRobert Watson 
254084bdb083SRobert Watson 		/*
2541db2661ceSRobert Watson 		 * To change the auxiliary LOMAC label on a vnode, the new
2542db2661ceSRobert Watson 		 * vnode label must be in the subject range.
2543db2661ceSRobert Watson 		 */
25443f1a7a90SRobert Watson 		if (!lomac_auxsingle_in_range(new, subj))
2545db2661ceSRobert Watson 			return (EPERM);
2546db2661ceSRobert Watson 
2547db2661ceSRobert Watson 		/*
2548db2661ceSRobert Watson 		 * To change the auxiliary LOMAC label on the vnode to be
2549db2661ceSRobert Watson 		 * EQUAL, the subject must have appropriate privilege.
2550db2661ceSRobert Watson 		 */
25513f1a7a90SRobert Watson 		if (lomac_contains_equal(new)) {
25523f1a7a90SRobert Watson 			error = lomac_subject_privileged(subj);
2553db2661ceSRobert Watson 			if (error)
2554db2661ceSRobert Watson 				return (error);
2555db2661ceSRobert Watson 		}
2556db2661ceSRobert Watson 	}
2557db2661ceSRobert Watson 
2558db2661ceSRobert Watson 	return (0);
2559db2661ceSRobert Watson }
2560db2661ceSRobert Watson 
2561db2661ceSRobert Watson static int
lomac_vnode_check_rename_from(struct ucred * cred,struct vnode * dvp,struct label * dvplabel,struct vnode * vp,struct label * vplabel,struct componentname * cnp)25623f1a7a90SRobert Watson lomac_vnode_check_rename_from(struct ucred *cred, struct vnode *dvp,
256378007886SRobert Watson     struct label *dvplabel, struct vnode *vp, struct label *vplabel,
2564db2661ceSRobert Watson     struct componentname *cnp)
2565db2661ceSRobert Watson {
2566db2661ceSRobert Watson 	struct mac_lomac *subj, *obj;
2567db2661ceSRobert Watson 
25683f1a7a90SRobert Watson 	if (!lomac_enabled)
2569db2661ceSRobert Watson 		return (0);
2570db2661ceSRobert Watson 
2571eca8a663SRobert Watson 	subj = SLOT(cred->cr_label);
257278007886SRobert Watson 	obj = SLOT(dvplabel);
2573db2661ceSRobert Watson 
25743f1a7a90SRobert Watson 	if (!lomac_subject_dominate(subj, obj))
2575db2661ceSRobert Watson 		return (EACCES);
2576db2661ceSRobert Watson 
257778007886SRobert Watson 	obj = SLOT(vplabel);
2578db2661ceSRobert Watson 
25793f1a7a90SRobert Watson 	if (!lomac_subject_dominate(subj, obj))
2580db2661ceSRobert Watson 		return (EACCES);
2581db2661ceSRobert Watson 
2582db2661ceSRobert Watson 	return (0);
2583db2661ceSRobert Watson }
2584db2661ceSRobert Watson 
2585db2661ceSRobert Watson static int
lomac_vnode_check_rename_to(struct ucred * cred,struct vnode * dvp,struct label * dvplabel,struct vnode * vp,struct label * vplabel,int samedir,struct componentname * cnp)25863f1a7a90SRobert Watson lomac_vnode_check_rename_to(struct ucred *cred, struct vnode *dvp,
258778007886SRobert Watson     struct label *dvplabel, struct vnode *vp, struct label *vplabel,
258878007886SRobert Watson     int samedir, struct componentname *cnp)
2589db2661ceSRobert Watson {
2590db2661ceSRobert Watson 	struct mac_lomac *subj, *obj;
2591db2661ceSRobert Watson 
25923f1a7a90SRobert Watson 	if (!lomac_enabled)
2593db2661ceSRobert Watson 		return (0);
2594db2661ceSRobert Watson 
2595eca8a663SRobert Watson 	subj = SLOT(cred->cr_label);
259678007886SRobert Watson 	obj = SLOT(dvplabel);
2597db2661ceSRobert Watson 
25983f1a7a90SRobert Watson 	if (!lomac_subject_dominate(subj, obj))
2599db2661ceSRobert Watson 		return (EACCES);
2600db2661ceSRobert Watson 
2601db2661ceSRobert Watson 	if (vp != NULL) {
260278007886SRobert Watson 		obj = SLOT(vplabel);
2603db2661ceSRobert Watson 
26043f1a7a90SRobert Watson 		if (!lomac_subject_dominate(subj, obj))
2605db2661ceSRobert Watson 			return (EACCES);
2606db2661ceSRobert Watson 	}
2607db2661ceSRobert Watson 
2608db2661ceSRobert Watson 	return (0);
2609db2661ceSRobert Watson }
2610db2661ceSRobert Watson 
2611db2661ceSRobert Watson static int
lomac_vnode_check_revoke(struct ucred * cred,struct vnode * vp,struct label * vplabel)26123f1a7a90SRobert Watson lomac_vnode_check_revoke(struct ucred *cred, struct vnode *vp,
261378007886SRobert Watson     struct label *vplabel)
2614db2661ceSRobert Watson {
2615db2661ceSRobert Watson 	struct mac_lomac *subj, *obj;
2616db2661ceSRobert Watson 
26173f1a7a90SRobert Watson 	if (!lomac_enabled)
2618db2661ceSRobert Watson 		return (0);
2619db2661ceSRobert Watson 
2620eca8a663SRobert Watson 	subj = SLOT(cred->cr_label);
262178007886SRobert Watson 	obj = SLOT(vplabel);
2622db2661ceSRobert Watson 
26233f1a7a90SRobert Watson 	if (!lomac_subject_dominate(subj, obj))
2624db2661ceSRobert Watson 		return (EACCES);
2625db2661ceSRobert Watson 
2626db2661ceSRobert Watson 	return (0);
2627db2661ceSRobert Watson }
2628db2661ceSRobert Watson 
2629db2661ceSRobert Watson static int
lomac_vnode_check_setacl(struct ucred * cred,struct vnode * vp,struct label * vplabel,acl_type_t type,struct acl * acl)26303f1a7a90SRobert Watson lomac_vnode_check_setacl(struct ucred *cred, struct vnode *vp,
263178007886SRobert Watson     struct label *vplabel, acl_type_t type, struct acl *acl)
2632db2661ceSRobert Watson {
2633db2661ceSRobert Watson 	struct mac_lomac *subj, *obj;
2634db2661ceSRobert Watson 
26353f1a7a90SRobert Watson 	if (!lomac_enabled)
2636db2661ceSRobert Watson 		return (0);
2637db2661ceSRobert Watson 
2638eca8a663SRobert Watson 	subj = SLOT(cred->cr_label);
263978007886SRobert Watson 	obj = SLOT(vplabel);
2640db2661ceSRobert Watson 
26413f1a7a90SRobert Watson 	if (!lomac_subject_dominate(subj, obj))
2642db2661ceSRobert Watson 		return (EACCES);
2643db2661ceSRobert Watson 
2644db2661ceSRobert Watson 	return (0);
2645db2661ceSRobert Watson }
2646db2661ceSRobert Watson 
2647db2661ceSRobert Watson static int
lomac_vnode_check_setextattr(struct ucred * cred,struct vnode * vp,struct label * vplabel,int attrnamespace,const char * name)26483f1a7a90SRobert Watson lomac_vnode_check_setextattr(struct ucred *cred, struct vnode *vp,
2649fefd0ac8SRobert Watson     struct label *vplabel, int attrnamespace, const char *name)
2650db2661ceSRobert Watson {
2651db2661ceSRobert Watson 	struct mac_lomac *subj, *obj;
2652db2661ceSRobert Watson 
26533f1a7a90SRobert Watson 	if (!lomac_enabled)
2654db2661ceSRobert Watson 		return (0);
2655db2661ceSRobert Watson 
2656eca8a663SRobert Watson 	subj = SLOT(cred->cr_label);
265778007886SRobert Watson 	obj = SLOT(vplabel);
2658db2661ceSRobert Watson 
26593f1a7a90SRobert Watson 	if (!lomac_subject_dominate(subj, obj))
2660db2661ceSRobert Watson 		return (EACCES);
2661db2661ceSRobert Watson 
2662db2661ceSRobert Watson 	/* XXX: protect the MAC EA in a special way? */
2663db2661ceSRobert Watson 
2664db2661ceSRobert Watson 	return (0);
2665db2661ceSRobert Watson }
2666db2661ceSRobert Watson 
2667db2661ceSRobert Watson static int
lomac_vnode_check_setflags(struct ucred * cred,struct vnode * vp,struct label * vplabel,u_long flags)26683f1a7a90SRobert Watson lomac_vnode_check_setflags(struct ucred *cred, struct vnode *vp,
266978007886SRobert Watson     struct label *vplabel, u_long flags)
2670db2661ceSRobert Watson {
2671db2661ceSRobert Watson 	struct mac_lomac *subj, *obj;
2672db2661ceSRobert Watson 
26733f1a7a90SRobert Watson 	if (!lomac_enabled)
2674db2661ceSRobert Watson 		return (0);
2675db2661ceSRobert Watson 
2676eca8a663SRobert Watson 	subj = SLOT(cred->cr_label);
267778007886SRobert Watson 	obj = SLOT(vplabel);
2678db2661ceSRobert Watson 
26793f1a7a90SRobert Watson 	if (!lomac_subject_dominate(subj, obj))
2680db2661ceSRobert Watson 		return (EACCES);
2681db2661ceSRobert Watson 
2682db2661ceSRobert Watson 	return (0);
2683db2661ceSRobert Watson }
2684db2661ceSRobert Watson 
2685db2661ceSRobert Watson static int
lomac_vnode_check_setmode(struct ucred * cred,struct vnode * vp,struct label * vplabel,mode_t mode)26863f1a7a90SRobert Watson lomac_vnode_check_setmode(struct ucred *cred, struct vnode *vp,
268778007886SRobert Watson     struct label *vplabel, mode_t mode)
2688db2661ceSRobert Watson {
2689db2661ceSRobert Watson 	struct mac_lomac *subj, *obj;
2690db2661ceSRobert Watson 
26913f1a7a90SRobert Watson 	if (!lomac_enabled)
2692db2661ceSRobert Watson 		return (0);
2693db2661ceSRobert Watson 
2694eca8a663SRobert Watson 	subj = SLOT(cred->cr_label);
269578007886SRobert Watson 	obj = SLOT(vplabel);
2696db2661ceSRobert Watson 
26973f1a7a90SRobert Watson 	if (!lomac_subject_dominate(subj, obj))
2698db2661ceSRobert Watson 		return (EACCES);
2699db2661ceSRobert Watson 
2700db2661ceSRobert Watson 	return (0);
2701db2661ceSRobert Watson }
2702db2661ceSRobert Watson 
2703db2661ceSRobert Watson static int
lomac_vnode_check_setowner(struct ucred * cred,struct vnode * vp,struct label * vplabel,uid_t uid,gid_t gid)27043f1a7a90SRobert Watson lomac_vnode_check_setowner(struct ucred *cred, struct vnode *vp,
270578007886SRobert Watson     struct label *vplabel, uid_t uid, gid_t gid)
2706db2661ceSRobert Watson {
2707db2661ceSRobert Watson 	struct mac_lomac *subj, *obj;
2708db2661ceSRobert Watson 
27093f1a7a90SRobert Watson 	if (!lomac_enabled)
2710db2661ceSRobert Watson 		return (0);
2711db2661ceSRobert Watson 
2712eca8a663SRobert Watson 	subj = SLOT(cred->cr_label);
271378007886SRobert Watson 	obj = SLOT(vplabel);
2714db2661ceSRobert Watson 
27153f1a7a90SRobert Watson 	if (!lomac_subject_dominate(subj, obj))
2716db2661ceSRobert Watson 		return (EACCES);
2717db2661ceSRobert Watson 
2718db2661ceSRobert Watson 	return (0);
2719db2661ceSRobert Watson }
2720db2661ceSRobert Watson 
2721db2661ceSRobert Watson static int
lomac_vnode_check_setutimes(struct ucred * cred,struct vnode * vp,struct label * vplabel,struct timespec atime,struct timespec mtime)27223f1a7a90SRobert Watson lomac_vnode_check_setutimes(struct ucred *cred, struct vnode *vp,
272378007886SRobert Watson     struct label *vplabel, struct timespec atime, struct timespec mtime)
2724db2661ceSRobert Watson {
2725db2661ceSRobert Watson 	struct mac_lomac *subj, *obj;
2726db2661ceSRobert Watson 
27273f1a7a90SRobert Watson 	if (!lomac_enabled)
2728db2661ceSRobert Watson 		return (0);
2729db2661ceSRobert Watson 
2730eca8a663SRobert Watson 	subj = SLOT(cred->cr_label);
273178007886SRobert Watson 	obj = SLOT(vplabel);
2732db2661ceSRobert Watson 
27333f1a7a90SRobert Watson 	if (!lomac_subject_dominate(subj, obj))
2734db2661ceSRobert Watson 		return (EACCES);
2735db2661ceSRobert Watson 
2736db2661ceSRobert Watson 	return (0);
2737db2661ceSRobert Watson }
2738db2661ceSRobert Watson 
2739db2661ceSRobert Watson static int
lomac_vnode_check_unlink(struct ucred * cred,struct vnode * dvp,struct label * dvplabel,struct vnode * vp,struct label * vplabel,struct componentname * cnp)27403f1a7a90SRobert Watson lomac_vnode_check_unlink(struct ucred *cred, struct vnode *dvp,
274145e0f3d6SRobert Watson     struct label *dvplabel, struct vnode *vp, struct label *vplabel,
274245e0f3d6SRobert Watson     struct componentname *cnp)
274345e0f3d6SRobert Watson {
274445e0f3d6SRobert Watson 	struct mac_lomac *subj, *obj;
274545e0f3d6SRobert Watson 
27463f1a7a90SRobert Watson 	if (!lomac_enabled)
274745e0f3d6SRobert Watson 		return (0);
274845e0f3d6SRobert Watson 
274945e0f3d6SRobert Watson 	subj = SLOT(cred->cr_label);
275045e0f3d6SRobert Watson 	obj = SLOT(dvplabel);
275145e0f3d6SRobert Watson 
27523f1a7a90SRobert Watson 	if (!lomac_subject_dominate(subj, obj))
275345e0f3d6SRobert Watson 		return (EACCES);
275445e0f3d6SRobert Watson 
275545e0f3d6SRobert Watson 	obj = SLOT(vplabel);
275645e0f3d6SRobert Watson 
27573f1a7a90SRobert Watson 	if (!lomac_subject_dominate(subj, obj))
275845e0f3d6SRobert Watson 		return (EACCES);
275945e0f3d6SRobert Watson 
276045e0f3d6SRobert Watson 	return (0);
276145e0f3d6SRobert Watson }
276245e0f3d6SRobert Watson 
276345e0f3d6SRobert Watson static int
lomac_vnode_check_write(struct ucred * active_cred,struct ucred * file_cred,struct vnode * vp,struct label * vplabel)27643f1a7a90SRobert Watson lomac_vnode_check_write(struct ucred *active_cred,
276578007886SRobert Watson     struct ucred *file_cred, struct vnode *vp, struct label *vplabel)
2766db2661ceSRobert Watson {
2767db2661ceSRobert Watson 	struct mac_lomac *subj, *obj;
2768db2661ceSRobert Watson 
27693f1a7a90SRobert Watson 	if (!lomac_enabled || !revocation_enabled)
2770db2661ceSRobert Watson 		return (0);
2771db2661ceSRobert Watson 
2772eca8a663SRobert Watson 	subj = SLOT(active_cred->cr_label);
277378007886SRobert Watson 	obj = SLOT(vplabel);
2774db2661ceSRobert Watson 
27753f1a7a90SRobert Watson 	if (!lomac_subject_dominate(subj, obj))
2776db2661ceSRobert Watson 		return (EACCES);
2777db2661ceSRobert Watson 
2778db2661ceSRobert Watson 	return (0);
2779db2661ceSRobert Watson }
2780db2661ceSRobert Watson 
2781eb320b0eSRobert Watson static int
lomac_vnode_create_extattr(struct ucred * cred,struct mount * mp,struct label * mplabel,struct vnode * dvp,struct label * dvplabel,struct vnode * vp,struct label * vplabel,struct componentname * cnp)2782eb320b0eSRobert Watson lomac_vnode_create_extattr(struct ucred *cred, struct mount *mp,
2783eb320b0eSRobert Watson     struct label *mplabel, struct vnode *dvp, struct label *dvplabel,
2784eb320b0eSRobert Watson     struct vnode *vp, struct label *vplabel, struct componentname *cnp)
2785db2661ceSRobert Watson {
2786eb320b0eSRobert Watson 	struct mac_lomac *source, *dest, *dir, temp;
2787eb320b0eSRobert Watson 	size_t buflen;
2788eb320b0eSRobert Watson 	int error;
2789db2661ceSRobert Watson 
2790eb320b0eSRobert Watson 	buflen = sizeof(temp);
2791eb320b0eSRobert Watson 	bzero(&temp, buflen);
2792eb320b0eSRobert Watson 
2793eb320b0eSRobert Watson 	source = SLOT(cred->cr_label);
2794eb320b0eSRobert Watson 	dest = SLOT(vplabel);
2795eb320b0eSRobert Watson 	dir = SLOT(dvplabel);
2796eb320b0eSRobert Watson 	if (dir->ml_flags & MAC_LOMAC_FLAG_AUX) {
2797eb320b0eSRobert Watson 		lomac_copy_auxsingle(dir, &temp);
2798eb320b0eSRobert Watson 		lomac_set_single(&temp, dir->ml_auxsingle.mle_type,
2799eb320b0eSRobert Watson 		    dir->ml_auxsingle.mle_grade);
2800db2661ceSRobert Watson 	} else {
2801eb320b0eSRobert Watson 		lomac_copy_single(source, &temp);
2802db2661ceSRobert Watson 	}
2803eb320b0eSRobert Watson 
2804eb320b0eSRobert Watson 	error = vn_extattr_set(vp, IO_NODELOCKED, MAC_LOMAC_EXTATTR_NAMESPACE,
2805eb320b0eSRobert Watson 	    MAC_LOMAC_EXTATTR_NAME, buflen, (char *)&temp, curthread);
2806eb320b0eSRobert Watson 	if (error == 0)
2807eb320b0eSRobert Watson 		lomac_copy(&temp, dest);
2808eb320b0eSRobert Watson 	return (error);
2809eb320b0eSRobert Watson }
2810eb320b0eSRobert Watson 
2811eb320b0eSRobert Watson static void
lomac_vnode_execve_transition(struct ucred * old,struct ucred * new,struct vnode * vp,struct label * vplabel,struct label * interpvplabel,struct image_params * imgp,struct label * execlabel)2812eb320b0eSRobert Watson lomac_vnode_execve_transition(struct ucred *old, struct ucred *new,
2813eb320b0eSRobert Watson     struct vnode *vp, struct label *vplabel, struct label *interpvplabel,
2814eb320b0eSRobert Watson     struct image_params *imgp, struct label *execlabel)
2815eb320b0eSRobert Watson {
2816eb320b0eSRobert Watson 	struct mac_lomac *source, *dest, *obj, *robj;
2817eb320b0eSRobert Watson 
2818eb320b0eSRobert Watson 	source = SLOT(old->cr_label);
2819eb320b0eSRobert Watson 	dest = SLOT(new->cr_label);
2820eb320b0eSRobert Watson 	obj = SLOT(vplabel);
2821eb320b0eSRobert Watson 	robj = interpvplabel != NULL ? SLOT(interpvplabel) : obj;
2822eb320b0eSRobert Watson 
2823eb320b0eSRobert Watson 	lomac_copy(source, dest);
2824eb320b0eSRobert Watson 	/*
2825eb320b0eSRobert Watson 	 * If there's an auxiliary label on the real object, respect it and
2826eb320b0eSRobert Watson 	 * assume that this level should be assumed immediately if a higher
2827eb320b0eSRobert Watson 	 * level is currently in place.
2828eb320b0eSRobert Watson 	 */
2829eb320b0eSRobert Watson 	if (robj->ml_flags & MAC_LOMAC_FLAG_AUX &&
2830eb320b0eSRobert Watson 	    !lomac_dominate_element(&robj->ml_auxsingle, &dest->ml_single)
2831eb320b0eSRobert Watson 	    && lomac_auxsingle_in_range(robj, dest))
2832eb320b0eSRobert Watson 		lomac_set_single(dest, robj->ml_auxsingle.mle_type,
2833eb320b0eSRobert Watson 		    robj->ml_auxsingle.mle_grade);
2834eb320b0eSRobert Watson 	/*
2835eb320b0eSRobert Watson 	 * Restructuring to use the execve transitioning mechanism instead of
2836eb320b0eSRobert Watson 	 * the normal demotion mechanism here would be difficult, so just
2837eb320b0eSRobert Watson 	 * copy the label over and perform standard demotion.  This is also
2838eb320b0eSRobert Watson 	 * non-optimal because it will result in the intermediate label "new"
2839eb320b0eSRobert Watson 	 * being created and immediately recycled.
2840eb320b0eSRobert Watson 	 */
2841eb320b0eSRobert Watson 	if (lomac_enabled && revocation_enabled &&
2842eb320b0eSRobert Watson 	    !lomac_dominate_single(obj, source))
2843eb320b0eSRobert Watson 		(void)maybe_demote(source, obj, "executing", "file", vp);
2844eb320b0eSRobert Watson }
2845eb320b0eSRobert Watson 
2846eb320b0eSRobert Watson static int
lomac_vnode_execve_will_transition(struct ucred * old,struct vnode * vp,struct label * vplabel,struct label * interpvplabel,struct image_params * imgp,struct label * execlabel)2847eb320b0eSRobert Watson lomac_vnode_execve_will_transition(struct ucred *old, struct vnode *vp,
2848eb320b0eSRobert Watson     struct label *vplabel, struct label *interpvplabel,
2849eb320b0eSRobert Watson     struct image_params *imgp, struct label *execlabel)
2850eb320b0eSRobert Watson {
2851eb320b0eSRobert Watson 	struct mac_lomac *subj, *obj, *robj;
2852eb320b0eSRobert Watson 
2853eb320b0eSRobert Watson 	if (!lomac_enabled || !revocation_enabled)
2854eb320b0eSRobert Watson 		return (0);
2855eb320b0eSRobert Watson 
2856eb320b0eSRobert Watson 	subj = SLOT(old->cr_label);
2857eb320b0eSRobert Watson 	obj = SLOT(vplabel);
2858eb320b0eSRobert Watson 	robj = interpvplabel != NULL ? SLOT(interpvplabel) : obj;
2859eb320b0eSRobert Watson 
2860eb320b0eSRobert Watson 	return ((robj->ml_flags & MAC_LOMAC_FLAG_AUX &&
2861eb320b0eSRobert Watson 	    !lomac_dominate_element(&robj->ml_auxsingle, &subj->ml_single)
2862eb320b0eSRobert Watson 	    && lomac_auxsingle_in_range(robj, subj)) ||
2863eb320b0eSRobert Watson 	    !lomac_dominate_single(obj, subj));
2864eb320b0eSRobert Watson }
2865eb320b0eSRobert Watson 
2866eb320b0eSRobert Watson static void
lomac_vnode_relabel(struct ucred * cred,struct vnode * vp,struct label * vplabel,struct label * newlabel)2867eb320b0eSRobert Watson lomac_vnode_relabel(struct ucred *cred, struct vnode *vp,
2868eb320b0eSRobert Watson     struct label *vplabel, struct label *newlabel)
2869eb320b0eSRobert Watson {
2870eb320b0eSRobert Watson 	struct mac_lomac *source, *dest;
2871eb320b0eSRobert Watson 
2872eb320b0eSRobert Watson 	source = SLOT(newlabel);
2873eb320b0eSRobert Watson 	dest = SLOT(vplabel);
2874eb320b0eSRobert Watson 
2875eb320b0eSRobert Watson 	try_relabel(source, dest);
2876eb320b0eSRobert Watson }
2877eb320b0eSRobert Watson 
2878eb320b0eSRobert Watson static int
lomac_vnode_setlabel_extattr(struct ucred * cred,struct vnode * vp,struct label * vplabel,struct label * intlabel)2879eb320b0eSRobert Watson lomac_vnode_setlabel_extattr(struct ucred *cred, struct vnode *vp,
2880eb320b0eSRobert Watson     struct label *vplabel, struct label *intlabel)
2881eb320b0eSRobert Watson {
2882eb320b0eSRobert Watson 	struct mac_lomac *source, temp;
2883eb320b0eSRobert Watson 	size_t buflen;
2884eb320b0eSRobert Watson 	int error;
2885eb320b0eSRobert Watson 
2886eb320b0eSRobert Watson 	buflen = sizeof(temp);
2887eb320b0eSRobert Watson 	bzero(&temp, buflen);
2888eb320b0eSRobert Watson 
2889eb320b0eSRobert Watson 	source = SLOT(intlabel);
2890eb320b0eSRobert Watson 	if ((source->ml_flags & MAC_LOMAC_FLAG_SINGLE) == 0)
2891eb320b0eSRobert Watson 		return (0);
2892eb320b0eSRobert Watson 
2893eb320b0eSRobert Watson 	lomac_copy_single(source, &temp);
2894eb320b0eSRobert Watson 	error = vn_extattr_set(vp, IO_NODELOCKED, MAC_LOMAC_EXTATTR_NAMESPACE,
2895eb320b0eSRobert Watson 	    MAC_LOMAC_EXTATTR_NAME, buflen, (char *)&temp, curthread);
2896eb320b0eSRobert Watson 	return (error);
2897db2661ceSRobert Watson }
2898db2661ceSRobert Watson 
28993f1a7a90SRobert Watson static struct mac_policy_ops lomac_ops =
2900db2661ceSRobert Watson {
29013f1a7a90SRobert Watson 	.mpo_init = lomac_init,
2902c6d31b83SKonstantin Belousov 	.mpo_destroy = lomac_fini,
2903eb320b0eSRobert Watson 
2904eb320b0eSRobert Watson 	.mpo_bpfdesc_check_receive = lomac_bpfdesc_check_receive,
2905eb320b0eSRobert Watson 	.mpo_bpfdesc_create = lomac_bpfdesc_create,
2906eb320b0eSRobert Watson 	.mpo_bpfdesc_create_mbuf = lomac_bpfdesc_create_mbuf,
29073f1a7a90SRobert Watson 	.mpo_bpfdesc_destroy_label = lomac_destroy_label,
2908eb320b0eSRobert Watson 	.mpo_bpfdesc_init_label = lomac_init_label,
2909eb320b0eSRobert Watson 
2910eb320b0eSRobert Watson 	.mpo_cred_check_relabel = lomac_cred_check_relabel,
2911eb320b0eSRobert Watson 	.mpo_cred_check_visible = lomac_cred_check_visible,
29123f1a7a90SRobert Watson 	.mpo_cred_copy_label = lomac_copy_label,
2913212ab0cfSRobert Watson 	.mpo_cred_create_swapper = lomac_cred_create_swapper,
2914212ab0cfSRobert Watson 	.mpo_cred_create_init = lomac_cred_create_init,
2915eb320b0eSRobert Watson 	.mpo_cred_destroy_label = lomac_destroy_label,
29163f1a7a90SRobert Watson 	.mpo_cred_externalize_label = lomac_externalize_label,
2917eb320b0eSRobert Watson 	.mpo_cred_init_label = lomac_init_label,
29183f1a7a90SRobert Watson 	.mpo_cred_internalize_label = lomac_internalize_label,
2919eb320b0eSRobert Watson 	.mpo_cred_relabel = lomac_cred_relabel,
2920eb320b0eSRobert Watson 
29213f1a7a90SRobert Watson 	.mpo_devfs_create_device = lomac_devfs_create_device,
29223f1a7a90SRobert Watson 	.mpo_devfs_create_directory = lomac_devfs_create_directory,
29233f1a7a90SRobert Watson 	.mpo_devfs_create_symlink = lomac_devfs_create_symlink,
2924eb320b0eSRobert Watson 	.mpo_devfs_destroy_label = lomac_destroy_label,
2925eb320b0eSRobert Watson 	.mpo_devfs_init_label = lomac_init_label,
29263f1a7a90SRobert Watson 	.mpo_devfs_update = lomac_devfs_update,
29273f1a7a90SRobert Watson 	.mpo_devfs_vnode_associate = lomac_devfs_vnode_associate,
2928eb320b0eSRobert Watson 
29293f1a7a90SRobert Watson 	.mpo_ifnet_check_relabel = lomac_ifnet_check_relabel,
29303f1a7a90SRobert Watson 	.mpo_ifnet_check_transmit = lomac_ifnet_check_transmit,
2931eb320b0eSRobert Watson 	.mpo_ifnet_copy_label = lomac_copy_label,
2932eb320b0eSRobert Watson 	.mpo_ifnet_create = lomac_ifnet_create,
2933eb320b0eSRobert Watson 	.mpo_ifnet_create_mbuf = lomac_ifnet_create_mbuf,
2934eb320b0eSRobert Watson 	.mpo_ifnet_destroy_label = lomac_destroy_label,
2935eb320b0eSRobert Watson 	.mpo_ifnet_externalize_label = lomac_externalize_label,
2936eb320b0eSRobert Watson 	.mpo_ifnet_init_label = lomac_init_label,
2937eb320b0eSRobert Watson 	.mpo_ifnet_internalize_label = lomac_internalize_label,
2938eb320b0eSRobert Watson 	.mpo_ifnet_relabel = lomac_ifnet_relabel,
2939eb320b0eSRobert Watson 
2940eb320b0eSRobert Watson 	.mpo_syncache_create = lomac_syncache_create,
2941eb320b0eSRobert Watson 	.mpo_syncache_destroy_label = lomac_destroy_label,
2942eb320b0eSRobert Watson 	.mpo_syncache_init_label = lomac_init_label_waitcheck,
2943eb320b0eSRobert Watson 
29443f1a7a90SRobert Watson 	.mpo_inpcb_check_deliver = lomac_inpcb_check_deliver,
29457fb179baSBjoern A. Zeeb 	.mpo_inpcb_check_visible = lomac_inpcb_check_visible,
2946eb320b0eSRobert Watson 	.mpo_inpcb_create = lomac_inpcb_create,
2947eb320b0eSRobert Watson 	.mpo_inpcb_create_mbuf = lomac_inpcb_create_mbuf,
2948eb320b0eSRobert Watson 	.mpo_inpcb_destroy_label = lomac_destroy_label,
2949eb320b0eSRobert Watson 	.mpo_inpcb_init_label = lomac_init_label_waitcheck,
2950eb320b0eSRobert Watson 	.mpo_inpcb_sosetlabel = lomac_inpcb_sosetlabel,
2951eb320b0eSRobert Watson 
2952048e1287SRobert Watson 	.mpo_ip6q_create = lomac_ip6q_create,
2953048e1287SRobert Watson 	.mpo_ip6q_destroy_label = lomac_destroy_label,
2954048e1287SRobert Watson 	.mpo_ip6q_init_label = lomac_init_label_waitcheck,
2955048e1287SRobert Watson 	.mpo_ip6q_match = lomac_ip6q_match,
2956048e1287SRobert Watson 	.mpo_ip6q_reassemble = lomac_ip6q_reassemble,
2957048e1287SRobert Watson 	.mpo_ip6q_update = lomac_ip6q_update,
2958048e1287SRobert Watson 
2959eb320b0eSRobert Watson 	.mpo_ipq_create = lomac_ipq_create,
2960eb320b0eSRobert Watson 	.mpo_ipq_destroy_label = lomac_destroy_label,
2961eb320b0eSRobert Watson 	.mpo_ipq_init_label = lomac_init_label_waitcheck,
2962eb320b0eSRobert Watson 	.mpo_ipq_match = lomac_ipq_match,
2963eb320b0eSRobert Watson 	.mpo_ipq_reassemble = lomac_ipq_reassemble,
2964eb320b0eSRobert Watson 	.mpo_ipq_update = lomac_ipq_update,
2965eb320b0eSRobert Watson 
29663f1a7a90SRobert Watson 	.mpo_kld_check_load = lomac_kld_check_load,
2967eb320b0eSRobert Watson 
2968eb320b0eSRobert Watson 	.mpo_mbuf_copy_label = lomac_copy_label,
2969eb320b0eSRobert Watson 	.mpo_mbuf_destroy_label = lomac_destroy_label,
2970eb320b0eSRobert Watson 	.mpo_mbuf_init_label = lomac_init_label_waitcheck,
2971eb320b0eSRobert Watson 
2972eb320b0eSRobert Watson 	.mpo_mount_create = lomac_mount_create,
2973eb320b0eSRobert Watson 	.mpo_mount_destroy_label = lomac_destroy_label,
2974eb320b0eSRobert Watson 	.mpo_mount_init_label = lomac_init_label,
2975eb320b0eSRobert Watson 
2976eb320b0eSRobert Watson 	.mpo_netinet_arp_send = lomac_netinet_arp_send,
2977eb320b0eSRobert Watson 	.mpo_netinet_firewall_reply = lomac_netinet_firewall_reply,
2978eb320b0eSRobert Watson 	.mpo_netinet_firewall_send = lomac_netinet_firewall_send,
2979eb320b0eSRobert Watson 	.mpo_netinet_fragment = lomac_netinet_fragment,
2980eb320b0eSRobert Watson 	.mpo_netinet_icmp_reply = lomac_netinet_icmp_reply,
2981eb320b0eSRobert Watson 	.mpo_netinet_igmp_send = lomac_netinet_igmp_send,
2982eb320b0eSRobert Watson 
2983eb320b0eSRobert Watson 	.mpo_netinet6_nd6_send = lomac_netinet6_nd6_send,
2984eb320b0eSRobert Watson 
29853f1a7a90SRobert Watson 	.mpo_pipe_check_ioctl = lomac_pipe_check_ioctl,
29863f1a7a90SRobert Watson 	.mpo_pipe_check_read = lomac_pipe_check_read,
29873f1a7a90SRobert Watson 	.mpo_pipe_check_relabel = lomac_pipe_check_relabel,
29883f1a7a90SRobert Watson 	.mpo_pipe_check_write = lomac_pipe_check_write,
2989eb320b0eSRobert Watson 	.mpo_pipe_copy_label = lomac_copy_label,
2990eb320b0eSRobert Watson 	.mpo_pipe_create = lomac_pipe_create,
2991eb320b0eSRobert Watson 	.mpo_pipe_destroy_label = lomac_destroy_label,
2992eb320b0eSRobert Watson 	.mpo_pipe_externalize_label = lomac_externalize_label,
2993eb320b0eSRobert Watson 	.mpo_pipe_init_label = lomac_init_label,
2994eb320b0eSRobert Watson 	.mpo_pipe_internalize_label = lomac_internalize_label,
2995eb320b0eSRobert Watson 	.mpo_pipe_relabel = lomac_pipe_relabel,
2996eb320b0eSRobert Watson 
2997eb320b0eSRobert Watson 	.mpo_priv_check = lomac_priv_check,
2998eb320b0eSRobert Watson 
29993f1a7a90SRobert Watson 	.mpo_proc_check_debug = lomac_proc_check_debug,
30003f1a7a90SRobert Watson 	.mpo_proc_check_sched = lomac_proc_check_sched,
30013f1a7a90SRobert Watson 	.mpo_proc_check_signal = lomac_proc_check_signal,
3002eb320b0eSRobert Watson 	.mpo_proc_destroy_label = lomac_proc_destroy_label,
3003eb320b0eSRobert Watson 	.mpo_proc_init_label = lomac_proc_init_label,
3004eb320b0eSRobert Watson 
30053f1a7a90SRobert Watson 	.mpo_socket_check_deliver = lomac_socket_check_deliver,
30063f1a7a90SRobert Watson 	.mpo_socket_check_relabel = lomac_socket_check_relabel,
30073f1a7a90SRobert Watson 	.mpo_socket_check_visible = lomac_socket_check_visible,
3008eb320b0eSRobert Watson 	.mpo_socket_copy_label = lomac_copy_label,
3009eb320b0eSRobert Watson 	.mpo_socket_create = lomac_socket_create,
3010eb320b0eSRobert Watson 	.mpo_socket_create_mbuf = lomac_socket_create_mbuf,
3011eb320b0eSRobert Watson 	.mpo_socket_destroy_label = lomac_destroy_label,
3012eb320b0eSRobert Watson 	.mpo_socket_externalize_label = lomac_externalize_label,
3013eb320b0eSRobert Watson 	.mpo_socket_init_label = lomac_init_label_waitcheck,
3014eb320b0eSRobert Watson 	.mpo_socket_internalize_label = lomac_internalize_label,
3015eb320b0eSRobert Watson 	.mpo_socket_newconn = lomac_socket_newconn,
3016eb320b0eSRobert Watson 	.mpo_socket_relabel = lomac_socket_relabel,
3017eb320b0eSRobert Watson 
3018eb320b0eSRobert Watson 	.mpo_socketpeer_destroy_label = lomac_destroy_label,
3019eb320b0eSRobert Watson 	.mpo_socketpeer_externalize_label = lomac_externalize_label,
3020eb320b0eSRobert Watson 	.mpo_socketpeer_init_label = lomac_init_label_waitcheck,
3021eb320b0eSRobert Watson 	.mpo_socketpeer_set_from_mbuf = lomac_socketpeer_set_from_mbuf,
3022eb320b0eSRobert Watson 	.mpo_socketpeer_set_from_socket = lomac_socketpeer_set_from_socket,
3023eb320b0eSRobert Watson 
3024eb320b0eSRobert Watson 	.mpo_syncache_create_mbuf = lomac_syncache_create_mbuf,
3025eb320b0eSRobert Watson 
30263f1a7a90SRobert Watson 	.mpo_system_check_acct = lomac_system_check_acct,
30273f1a7a90SRobert Watson 	.mpo_system_check_auditctl = lomac_system_check_auditctl,
30283f1a7a90SRobert Watson 	.mpo_system_check_swapoff = lomac_system_check_swapoff,
30293f1a7a90SRobert Watson 	.mpo_system_check_swapon = lomac_system_check_swapon,
30303f1a7a90SRobert Watson 	.mpo_system_check_sysctl = lomac_system_check_sysctl,
3031eb320b0eSRobert Watson 
3032eb320b0eSRobert Watson 	.mpo_thread_userret = lomac_thread_userret,
3033eb320b0eSRobert Watson 
3034eb320b0eSRobert Watson 	.mpo_vnode_associate_extattr = lomac_vnode_associate_extattr,
3035eb320b0eSRobert Watson 	.mpo_vnode_associate_singlelabel = lomac_vnode_associate_singlelabel,
30363f1a7a90SRobert Watson 	.mpo_vnode_check_access = lomac_vnode_check_open,
30373f1a7a90SRobert Watson 	.mpo_vnode_check_create = lomac_vnode_check_create,
30383f1a7a90SRobert Watson 	.mpo_vnode_check_deleteacl = lomac_vnode_check_deleteacl,
30393f1a7a90SRobert Watson 	.mpo_vnode_check_link = lomac_vnode_check_link,
30403f1a7a90SRobert Watson 	.mpo_vnode_check_mmap = lomac_vnode_check_mmap,
30413f1a7a90SRobert Watson 	.mpo_vnode_check_mmap_downgrade = lomac_vnode_check_mmap_downgrade,
30423f1a7a90SRobert Watson 	.mpo_vnode_check_open = lomac_vnode_check_open,
30433f1a7a90SRobert Watson 	.mpo_vnode_check_read = lomac_vnode_check_read,
30443f1a7a90SRobert Watson 	.mpo_vnode_check_relabel = lomac_vnode_check_relabel,
30453f1a7a90SRobert Watson 	.mpo_vnode_check_rename_from = lomac_vnode_check_rename_from,
30463f1a7a90SRobert Watson 	.mpo_vnode_check_rename_to = lomac_vnode_check_rename_to,
30473f1a7a90SRobert Watson 	.mpo_vnode_check_revoke = lomac_vnode_check_revoke,
30483f1a7a90SRobert Watson 	.mpo_vnode_check_setacl = lomac_vnode_check_setacl,
30493f1a7a90SRobert Watson 	.mpo_vnode_check_setextattr = lomac_vnode_check_setextattr,
30503f1a7a90SRobert Watson 	.mpo_vnode_check_setflags = lomac_vnode_check_setflags,
30513f1a7a90SRobert Watson 	.mpo_vnode_check_setmode = lomac_vnode_check_setmode,
30523f1a7a90SRobert Watson 	.mpo_vnode_check_setowner = lomac_vnode_check_setowner,
30533f1a7a90SRobert Watson 	.mpo_vnode_check_setutimes = lomac_vnode_check_setutimes,
30543f1a7a90SRobert Watson 	.mpo_vnode_check_unlink = lomac_vnode_check_unlink,
30553f1a7a90SRobert Watson 	.mpo_vnode_check_write = lomac_vnode_check_write,
3056eb320b0eSRobert Watson 	.mpo_vnode_copy_label = lomac_copy_label,
3057eb320b0eSRobert Watson 	.mpo_vnode_create_extattr = lomac_vnode_create_extattr,
3058eb320b0eSRobert Watson 	.mpo_vnode_destroy_label = lomac_destroy_label,
3059eb320b0eSRobert Watson 	.mpo_vnode_execve_transition = lomac_vnode_execve_transition,
3060eb320b0eSRobert Watson 	.mpo_vnode_execve_will_transition = lomac_vnode_execve_will_transition,
3061eb320b0eSRobert Watson 	.mpo_vnode_externalize_label = lomac_externalize_label,
3062eb320b0eSRobert Watson 	.mpo_vnode_init_label = lomac_init_label,
3063eb320b0eSRobert Watson 	.mpo_vnode_internalize_label = lomac_internalize_label,
3064eb320b0eSRobert Watson 	.mpo_vnode_relabel = lomac_vnode_relabel,
3065eb320b0eSRobert Watson 	.mpo_vnode_setlabel_extattr = lomac_vnode_setlabel_extattr,
3066db2661ceSRobert Watson };
3067db2661ceSRobert Watson 
30683f1a7a90SRobert Watson MAC_POLICY_SET(&lomac_ops, mac_lomac, "TrustedBSD MAC/LOMAC",
30699162f64bSRobert Watson     MPC_LOADTIME_FLAG_NOTLATE, &lomac_slot);
3070