mac_cred.c revision 106023
1100894Srwatson/*-
2100894Srwatson * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson
3100894Srwatson * Copyright (c) 2001 Ilmar S. Habibulin
4100894Srwatson * Copyright (c) 2001, 2002 Networks Associates Technology, Inc.
5100894Srwatson * All rights reserved.
6100894Srwatson *
7100894Srwatson * This software was developed by Robert Watson and Ilmar Habibulin for the
8100894Srwatson * TrustedBSD Project.
9100894Srwatson *
10100894Srwatson * This software was developed for the FreeBSD Project in part by NAI Labs,
11100894Srwatson * the Security Research Division of Network Associates, Inc. under
12100894Srwatson * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
13100894Srwatson * CHATS research program.
14100894Srwatson *
15100894Srwatson * Redistribution and use in source and binary forms, with or without
16100894Srwatson * modification, are permitted provided that the following conditions
17100894Srwatson * are met:
18100894Srwatson * 1. Redistributions of source code must retain the above copyright
19100894Srwatson *    notice, this list of conditions and the following disclaimer.
20100894Srwatson * 2. Redistributions in binary form must reproduce the above copyright
21100894Srwatson *    notice, this list of conditions and the following disclaimer in the
22100894Srwatson *    documentation and/or other materials provided with the distribution.
23100894Srwatson * 3. The names of the authors may not be used to endorse or promote
24100894Srwatson *    products derived from this software without specific prior written
25100894Srwatson *    permission.
26100894Srwatson *
27100894Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
28100894Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29100894Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30100894Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
31100894Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32100894Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33100894Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34100894Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35100894Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36100894Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37100894Srwatson * SUCH DAMAGE.
38100894Srwatson *
39100894Srwatson * $FreeBSD: head/sys/security/mac/mac_process.c 106023 2002-10-27 06:54:06Z rwatson $
40100894Srwatson */
41100894Srwatson/*
42100894Srwatson * Developed by the TrustedBSD Project.
43100894Srwatson *
44100894Srwatson * Framework for extensible kernel access control.  Kernel and userland
45100894Srwatson * interface to the framework, policy registration and composition.
46100894Srwatson */
47100894Srwatson
48100894Srwatson#include "opt_mac.h"
49104300Sphk#include "opt_devfs.h"
50101173Srwatson
51100894Srwatson#include <sys/param.h>
52100979Srwatson#include <sys/extattr.h>
53100979Srwatson#include <sys/kernel.h>
54100979Srwatson#include <sys/lock.h>
55102949Sbde#include <sys/malloc.h>
56100979Srwatson#include <sys/mutex.h>
57100979Srwatson#include <sys/mac.h>
58101712Srwatson#include <sys/module.h>
59100979Srwatson#include <sys/proc.h>
60100979Srwatson#include <sys/systm.h>
61100894Srwatson#include <sys/sysproto.h>
62100894Srwatson#include <sys/sysent.h>
63100979Srwatson#include <sys/vnode.h>
64100979Srwatson#include <sys/mount.h>
65100979Srwatson#include <sys/file.h>
66100979Srwatson#include <sys/namei.h>
67100979Srwatson#include <sys/socket.h>
68100979Srwatson#include <sys/pipe.h>
69100979Srwatson#include <sys/socketvar.h>
70100979Srwatson#include <sys/sysctl.h>
71100894Srwatson
72100979Srwatson#include <vm/vm.h>
73100979Srwatson#include <vm/pmap.h>
74100979Srwatson#include <vm/vm_map.h>
75100979Srwatson#include <vm/vm_object.h>
76100979Srwatson
77100979Srwatson#include <sys/mac_policy.h>
78100979Srwatson
79100979Srwatson#include <fs/devfs/devfs.h>
80100979Srwatson
81100979Srwatson#include <net/bpfdesc.h>
82100979Srwatson#include <net/if.h>
83100979Srwatson#include <net/if_var.h>
84100979Srwatson
85100979Srwatson#include <netinet/in.h>
86100979Srwatson#include <netinet/ip_var.h>
87100979Srwatson
88100979Srwatson#ifdef MAC
89100979Srwatson
90101712Srwatson/*
91101712Srwatson * Declare that the kernel provides MAC support, version 1.  This permits
92101712Srwatson * modules to refuse to be loaded if the necessary support isn't present,
93101712Srwatson * even if it's pre-boot.
94101712Srwatson */
95101712SrwatsonMODULE_VERSION(kernel_mac_support, 1);
96101712Srwatson
97100979SrwatsonSYSCTL_DECL(_security);
98100979Srwatson
99100979SrwatsonSYSCTL_NODE(_security, OID_AUTO, mac, CTLFLAG_RW, 0,
100100979Srwatson    "TrustedBSD MAC policy controls");
101104517Srwatson
102100979Srwatson#if MAC_MAX_POLICIES > 32
103100979Srwatson#error "MAC_MAX_POLICIES too large"
104100979Srwatson#endif
105105497Srwatson
106100979Srwatsonstatic unsigned int mac_max_policies = MAC_MAX_POLICIES;
107100979Srwatsonstatic unsigned int mac_policy_offsets_free = (1 << MAC_MAX_POLICIES) - 1;
108100979SrwatsonSYSCTL_UINT(_security_mac, OID_AUTO, max_policies, CTLFLAG_RD,
109100979Srwatson    &mac_max_policies, 0, "");
110100979Srwatson
111105959Srwatson/*
112105959Srwatson * Has the kernel started generating labeled objects yet?  All read/write
113105959Srwatson * access to this variable is serialized during the boot process.  Following
114105959Srwatson * the end of serialization, we don't update this flag; no locking.
115105959Srwatson */
116100979Srwatsonstatic int	mac_late = 0;
117100979Srwatson
118105988Srwatson/*
119105988Srwatson * Warn about EA transactions only the first time they happen.
120105988Srwatson * Weak coherency, no locking.
121105988Srwatson */
122105988Srwatsonstatic int	ea_warn_once = 0;
123105988Srwatson
124100979Srwatsonstatic int	mac_enforce_fs = 1;
125100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, enforce_fs, CTLFLAG_RW,
126100979Srwatson    &mac_enforce_fs, 0, "Enforce MAC policy on file system objects");
127100979SrwatsonTUNABLE_INT("security.mac.enforce_fs", &mac_enforce_fs);
128100979Srwatson
129100979Srwatsonstatic int	mac_enforce_network = 1;
130100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, enforce_network, CTLFLAG_RW,
131100979Srwatson    &mac_enforce_network, 0, "Enforce MAC policy on network packets");
132100979SrwatsonTUNABLE_INT("security.mac.enforce_network", &mac_enforce_network);
133100979Srwatson
134103513Srwatsonstatic int	mac_enforce_pipe = 1;
135103513SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, enforce_pipe, CTLFLAG_RW,
136103513Srwatson    &mac_enforce_pipe, 0, "Enforce MAC policy on pipe operations");
137104236SrwatsonTUNABLE_INT("security.mac.enforce_pipe", &mac_enforce_pipe);
138103513Srwatson
139100979Srwatsonstatic int	mac_enforce_process = 1;
140100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, enforce_process, CTLFLAG_RW,
141100979Srwatson    &mac_enforce_process, 0, "Enforce MAC policy on inter-process operations");
142100979SrwatsonTUNABLE_INT("security.mac.enforce_process", &mac_enforce_process);
143100979Srwatson
144100979Srwatsonstatic int	mac_enforce_socket = 1;
145100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, enforce_socket, CTLFLAG_RW,
146100979Srwatson    &mac_enforce_socket, 0, "Enforce MAC policy on socket operations");
147100979SrwatsonTUNABLE_INT("security.mac.enforce_socket", &mac_enforce_socket);
148100979Srwatson
149103514Srwatsonstatic int     mac_enforce_vm = 1;
150103514SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, enforce_vm, CTLFLAG_RW,
151103514Srwatson    &mac_enforce_vm, 0, "Enforce MAC policy on vm operations");
152104236SrwatsonTUNABLE_INT("security.mac.enforce_vm", &mac_enforce_vm);
153103514Srwatson
154100979Srwatsonstatic int	mac_cache_fslabel_in_vnode = 1;
155100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, cache_fslabel_in_vnode, CTLFLAG_RW,
156100979Srwatson    &mac_cache_fslabel_in_vnode, 0, "Cache mount fslabel in vnode");
157100979SrwatsonTUNABLE_INT("security.mac.cache_fslabel_in_vnode",
158100979Srwatson    &mac_cache_fslabel_in_vnode);
159100979Srwatson
160103136Srwatsonstatic int	mac_mmap_revocation = 1;
161103136SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation, CTLFLAG_RW,
162103136Srwatson    &mac_mmap_revocation, 0, "Revoke mmap access to files on subject "
163103136Srwatson    "relabel");
164101892Srwatsonstatic int	mac_mmap_revocation_via_cow = 0;
165100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation_via_cow, CTLFLAG_RW,
166100979Srwatson    &mac_mmap_revocation_via_cow, 0, "Revoke mmap access to files via "
167100979Srwatson    "copy-on-write semantics, or by removing all write access");
168100979Srwatson
169101988Srwatson#ifdef MAC_DEBUG
170104268SrwatsonSYSCTL_NODE(_security_mac, OID_AUTO, debug, CTLFLAG_RW, 0,
171104268Srwatson    "TrustedBSD MAC debug info");
172104268Srwatson
173104268Srwatsonstatic int	mac_debug_label_fallback = 0;
174104268SrwatsonSYSCTL_INT(_security_mac_debug, OID_AUTO, label_fallback, CTLFLAG_RW,
175104268Srwatson    &mac_debug_label_fallback, 0, "Filesystems should fall back to fs label"
176104268Srwatson    "when label is corrupted.");
177104268SrwatsonTUNABLE_INT("security.mac.debug_label_fallback",
178104268Srwatson    &mac_debug_label_fallback);
179104268Srwatson
180104517SrwatsonSYSCTL_NODE(_security_mac_debug, OID_AUTO, counters, CTLFLAG_RW, 0,
181104517Srwatson    "TrustedBSD MAC object counters");
182104517Srwatson
183100979Srwatsonstatic unsigned int nmacmbufs, nmaccreds, nmacifnets, nmacbpfdescs,
184100979Srwatson    nmacsockets, nmacmounts, nmactemp, nmacvnodes, nmacdevfsdirents,
185100979Srwatson    nmacipqs, nmacpipes;
186104517Srwatson
187104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, mbufs, CTLFLAG_RD,
188100979Srwatson    &nmacmbufs, 0, "number of mbufs in use");
189104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, creds, CTLFLAG_RD,
190100979Srwatson    &nmaccreds, 0, "number of ucreds in use");
191104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, ifnets, CTLFLAG_RD,
192100979Srwatson    &nmacifnets, 0, "number of ifnets in use");
193104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, ipqs, CTLFLAG_RD,
194100979Srwatson    &nmacipqs, 0, "number of ipqs in use");
195104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, bpfdescs, CTLFLAG_RD,
196100979Srwatson    &nmacbpfdescs, 0, "number of bpfdescs in use");
197104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, sockets, CTLFLAG_RD,
198100979Srwatson    &nmacsockets, 0, "number of sockets in use");
199104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, pipes, CTLFLAG_RD,
200100979Srwatson    &nmacpipes, 0, "number of pipes in use");
201104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, mounts, CTLFLAG_RD,
202100979Srwatson    &nmacmounts, 0, "number of mounts in use");
203104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, temp, CTLFLAG_RD,
204100979Srwatson    &nmactemp, 0, "number of temporary labels in use");
205104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, vnodes, CTLFLAG_RD,
206100979Srwatson    &nmacvnodes, 0, "number of vnodes in use");
207104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, devfsdirents, CTLFLAG_RD,
208100979Srwatson    &nmacdevfsdirents, 0, "number of devfs dirents inuse");
209101988Srwatson#endif
210100979Srwatson
211100979Srwatsonstatic int	error_select(int error1, int error2);
212100979Srwatsonstatic int	mac_policy_register(struct mac_policy_conf *mpc);
213100979Srwatsonstatic int	mac_policy_unregister(struct mac_policy_conf *mpc);
214100979Srwatson
215104546Srwatsonstatic void	mac_check_vnode_mmap_downgrade(struct ucred *cred,
216104546Srwatson		    struct vnode *vp, int *prot);
217100979Srwatsonstatic void	mac_cred_mmapped_drop_perms_recurse(struct thread *td,
218100979Srwatson		    struct ucred *cred, struct vm_map *map);
219100979Srwatson
220104541Srwatsonstatic void	mac_destroy_socket_label(struct label *label);
221104541Srwatson
222105988Srwatsonstatic int	mac_setlabel_vnode_extattr(struct ucred *cred,
223105988Srwatson		    struct vnode *vp, struct label *intlabel);
224105988Srwatson
225105988Srwatson
226100979SrwatsonMALLOC_DEFINE(M_MACOPVEC, "macopvec", "MAC policy operation vector");
227100979SrwatsonMALLOC_DEFINE(M_MACPIPELABEL, "macpipelabel", "MAC labels for pipes");
228105694SrwatsonMALLOC_DEFINE(M_MACTEMP, "mactemp", "MAC temporary label storage");
229100979Srwatson
230100979Srwatson/*
231100979Srwatson * mac_policy_list_lock protects the consistency of 'mac_policy_list',
232100979Srwatson * the linked list of attached policy modules.  Read-only consumers of
233100979Srwatson * the list must acquire a shared lock for the duration of their use;
234100979Srwatson * writers must acquire an exclusive lock.  Note that for compound
235100979Srwatson * operations, locks should be held for the entire compound operation,
236100979Srwatson * and that this is not yet done for relabel requests.
237100979Srwatson */
238100979Srwatsonstatic struct mtx mac_policy_list_lock;
239100979Srwatsonstatic LIST_HEAD(, mac_policy_conf) mac_policy_list;
240100979Srwatsonstatic int mac_policy_list_busy;
241100979Srwatson#define	MAC_POLICY_LIST_LOCKINIT()	mtx_init(&mac_policy_list_lock,	\
242100979Srwatson	"mac_policy_list_lock", NULL, MTX_DEF);
243100979Srwatson#define	MAC_POLICY_LIST_LOCK()	mtx_lock(&mac_policy_list_lock);
244100979Srwatson#define	MAC_POLICY_LIST_UNLOCK()	mtx_unlock(&mac_policy_list_lock);
245100979Srwatson
246100979Srwatson#define	MAC_POLICY_LIST_BUSY() do {					\
247100979Srwatson	MAC_POLICY_LIST_LOCK();						\
248100979Srwatson	mac_policy_list_busy++;						\
249100979Srwatson	MAC_POLICY_LIST_UNLOCK();					\
250100979Srwatson} while (0)
251100979Srwatson
252100979Srwatson#define	MAC_POLICY_LIST_UNBUSY() do {					\
253100979Srwatson	MAC_POLICY_LIST_LOCK();						\
254100979Srwatson	mac_policy_list_busy--;						\
255100979Srwatson	if (mac_policy_list_busy < 0)					\
256100979Srwatson		panic("Extra mac_policy_list_busy--");			\
257100979Srwatson	MAC_POLICY_LIST_UNLOCK();					\
258100979Srwatson} while (0)
259100979Srwatson
260100979Srwatson/*
261100979Srwatson * MAC_CHECK performs the designated check by walking the policy
262100979Srwatson * module list and checking with each as to how it feels about the
263100979Srwatson * request.  Note that it returns its value via 'error' in the scope
264100979Srwatson * of the caller.
265100979Srwatson */
266100979Srwatson#define	MAC_CHECK(check, args...) do {					\
267100979Srwatson	struct mac_policy_conf *mpc;					\
268100979Srwatson									\
269100979Srwatson	error = 0;							\
270100979Srwatson	MAC_POLICY_LIST_BUSY();						\
271100979Srwatson	LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {			\
272100979Srwatson		if (mpc->mpc_ops->mpo_ ## check != NULL)		\
273100979Srwatson			error = error_select(				\
274100979Srwatson			    mpc->mpc_ops->mpo_ ## check (args),		\
275100979Srwatson			    error);					\
276100979Srwatson	}								\
277100979Srwatson	MAC_POLICY_LIST_UNBUSY();					\
278100979Srwatson} while (0)
279100979Srwatson
280100979Srwatson/*
281100979Srwatson * MAC_BOOLEAN performs the designated boolean composition by walking
282100979Srwatson * the module list, invoking each instance of the operation, and
283100979Srwatson * combining the results using the passed C operator.  Note that it
284100979Srwatson * returns its value via 'result' in the scope of the caller, which
285100979Srwatson * should be initialized by the caller in a meaningful way to get
286100979Srwatson * a meaningful result.
287100979Srwatson */
288100979Srwatson#define	MAC_BOOLEAN(operation, composition, args...) do {		\
289100979Srwatson	struct mac_policy_conf *mpc;					\
290100979Srwatson									\
291100979Srwatson	MAC_POLICY_LIST_BUSY();						\
292100979Srwatson	LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {			\
293100979Srwatson		if (mpc->mpc_ops->mpo_ ## operation != NULL)		\
294100979Srwatson			result = result composition			\
295100979Srwatson			    mpc->mpc_ops->mpo_ ## operation (args);	\
296100979Srwatson	}								\
297100979Srwatson	MAC_POLICY_LIST_UNBUSY();					\
298100979Srwatson} while (0)
299100979Srwatson
300105694Srwatson#define	MAC_EXTERNALIZE(type, label, elementlist, outbuf, 		\
301105694Srwatson    outbuflen) do {							\
302105694Srwatson	char *curptr, *curptr_start, *element_name, *element_temp;	\
303105694Srwatson	size_t left, left_start, len;					\
304105694Srwatson	int claimed, first, first_start, ignorenotfound;		\
305105694Srwatson									\
306105694Srwatson	error = 0;							\
307105694Srwatson	element_temp = elementlist;					\
308105694Srwatson	curptr = outbuf;						\
309105694Srwatson	curptr[0] = '\0';						\
310105694Srwatson	left = outbuflen;						\
311105694Srwatson	first = 1;							\
312105694Srwatson	while ((element_name = strsep(&element_temp, ",")) != NULL) {	\
313105694Srwatson		curptr_start = curptr;					\
314105694Srwatson		left_start = left;					\
315105694Srwatson		first_start = first;					\
316105694Srwatson		if (element_name[0] == '?') {				\
317105694Srwatson			element_name++;					\
318105694Srwatson			ignorenotfound = 1;				\
319105694Srwatson		} else							\
320105694Srwatson			ignorenotfound = 0;				\
321105694Srwatson		claimed = 0;						\
322105694Srwatson		if (first) {						\
323105694Srwatson			len = snprintf(curptr, left, "%s/",		\
324105694Srwatson			    element_name);				\
325105694Srwatson			first = 0;					\
326105694Srwatson		} else							\
327105694Srwatson			len = snprintf(curptr, left, ",%s/",		\
328105694Srwatson			    element_name);				\
329105694Srwatson		if (len >= left) {					\
330105694Srwatson			error = EINVAL;		/* XXXMAC: E2BIG */	\
331105694Srwatson			break;						\
332105694Srwatson		}							\
333105694Srwatson		curptr += len;						\
334105694Srwatson		left -= len;						\
335105694Srwatson									\
336105694Srwatson		MAC_CHECK(externalize_ ## type, label, element_name,	\
337105694Srwatson		    curptr, left, &len, &claimed);			\
338105694Srwatson		if (error)						\
339105694Srwatson			break;						\
340105694Srwatson		if (claimed == 1) {					\
341105694Srwatson			if (len >= outbuflen) {				\
342105694Srwatson				error = EINVAL;	/* XXXMAC: E2BIG */	\
343105694Srwatson				break;					\
344105694Srwatson			}						\
345105694Srwatson			curptr += len;					\
346105694Srwatson			left -= len;					\
347105694Srwatson		} else if (claimed == 0 && ignorenotfound) {		\
348105694Srwatson			/*						\
349105694Srwatson			 * Revert addition of the label element		\
350105694Srwatson			 * name.					\
351105694Srwatson			 */						\
352105694Srwatson			curptr = curptr_start;				\
353105694Srwatson			*curptr = '\0';					\
354105694Srwatson			left = left_start;				\
355105694Srwatson			first = first_start;				\
356105694Srwatson		} else {						\
357105694Srwatson			error = EINVAL;		/* XXXMAC: ENOLABEL */	\
358105694Srwatson			break;						\
359105694Srwatson		}							\
360105694Srwatson	}								\
361105694Srwatson} while (0)
362105694Srwatson
363105694Srwatson#define	MAC_INTERNALIZE(type, label, instring) do {			\
364105694Srwatson	char *element, *element_name, *element_data;			\
365105694Srwatson	int claimed;							\
366105694Srwatson									\
367105694Srwatson	error = 0;							\
368105694Srwatson	element = instring;						\
369105694Srwatson	while ((element_name = strsep(&element, ",")) != NULL) {	\
370105694Srwatson		element_data = element_name;				\
371105694Srwatson		element_name = strsep(&element_data, "/");		\
372105694Srwatson		if (element_data == NULL) {				\
373105694Srwatson			error = EINVAL;					\
374105694Srwatson			break;						\
375105694Srwatson		}							\
376105694Srwatson		claimed = 0;						\
377105694Srwatson		MAC_CHECK(internalize_ ## type, label, element_name,	\
378105694Srwatson		    element_data, &claimed);				\
379105694Srwatson		if (error)						\
380105694Srwatson			break;						\
381105694Srwatson		if (claimed != 1) {					\
382105694Srwatson			/* XXXMAC: Another error here? */		\
383105694Srwatson			error = EINVAL;					\
384105694Srwatson			break;						\
385105694Srwatson		}							\
386105694Srwatson	}								\
387105694Srwatson} while (0)
388105694Srwatson
389100979Srwatson/*
390100979Srwatson * MAC_PERFORM performs the designated operation by walking the policy
391100979Srwatson * module list and invoking that operation for each policy.
392100979Srwatson */
393100979Srwatson#define	MAC_PERFORM(operation, args...) do {				\
394100979Srwatson	struct mac_policy_conf *mpc;					\
395100979Srwatson									\
396100979Srwatson	MAC_POLICY_LIST_BUSY();						\
397100979Srwatson	LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {			\
398100979Srwatson		if (mpc->mpc_ops->mpo_ ## operation != NULL)		\
399100979Srwatson			mpc->mpc_ops->mpo_ ## operation (args);		\
400100979Srwatson	}								\
401100979Srwatson	MAC_POLICY_LIST_UNBUSY();					\
402100979Srwatson} while (0)
403100979Srwatson
404100979Srwatson/*
405100979Srwatson * Initialize the MAC subsystem, including appropriate SMP locks.
406100979Srwatson */
407100979Srwatsonstatic void
408100979Srwatsonmac_init(void)
409100979Srwatson{
410100979Srwatson
411100979Srwatson	LIST_INIT(&mac_policy_list);
412100979Srwatson	MAC_POLICY_LIST_LOCKINIT();
413100979Srwatson}
414100979Srwatson
415100979Srwatson/*
416100979Srwatson * For the purposes of modules that want to know if they were loaded
417100979Srwatson * "early", set the mac_late flag once we've processed modules either
418100979Srwatson * linked into the kernel, or loaded before the kernel startup.
419100979Srwatson */
420100979Srwatsonstatic void
421100979Srwatsonmac_late_init(void)
422100979Srwatson{
423100979Srwatson
424100979Srwatson	mac_late = 1;
425100979Srwatson}
426100979Srwatson
427100979Srwatson/*
428100979Srwatson * Allow MAC policy modules to register during boot, etc.
429100979Srwatson */
430100894Srwatsonint
431100979Srwatsonmac_policy_modevent(module_t mod, int type, void *data)
432100979Srwatson{
433100979Srwatson	struct mac_policy_conf *mpc;
434100979Srwatson	int error;
435100979Srwatson
436100979Srwatson	error = 0;
437100979Srwatson	mpc = (struct mac_policy_conf *) data;
438100979Srwatson
439100979Srwatson	switch (type) {
440100979Srwatson	case MOD_LOAD:
441100979Srwatson		if (mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_NOTLATE &&
442100979Srwatson		    mac_late) {
443100979Srwatson			printf("mac_policy_modevent: can't load %s policy "
444100979Srwatson			    "after booting\n", mpc->mpc_name);
445100979Srwatson			error = EBUSY;
446100979Srwatson			break;
447100979Srwatson		}
448100979Srwatson		error = mac_policy_register(mpc);
449100979Srwatson		break;
450100979Srwatson	case MOD_UNLOAD:
451100979Srwatson		/* Don't unregister the module if it was never registered. */
452100979Srwatson		if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED)
453100979Srwatson		    != 0)
454100979Srwatson			error = mac_policy_unregister(mpc);
455100979Srwatson		else
456100979Srwatson			error = 0;
457100979Srwatson		break;
458100979Srwatson	default:
459100979Srwatson		break;
460100979Srwatson	}
461100979Srwatson
462100979Srwatson	return (error);
463100979Srwatson}
464100979Srwatson
465100979Srwatsonstatic int
466100979Srwatsonmac_policy_register(struct mac_policy_conf *mpc)
467100979Srwatson{
468100979Srwatson	struct mac_policy_conf *tmpc;
469100979Srwatson	struct mac_policy_op_entry *mpe;
470100979Srwatson	int slot;
471100979Srwatson
472103570Srwatson	MALLOC(mpc->mpc_ops, struct mac_policy_ops *, sizeof(*mpc->mpc_ops),
473103570Srwatson	    M_MACOPVEC, M_WAITOK | M_ZERO);
474100979Srwatson	for (mpe = mpc->mpc_entries; mpe->mpe_constant != MAC_OP_LAST; mpe++) {
475100979Srwatson		switch (mpe->mpe_constant) {
476100979Srwatson		case MAC_OP_LAST:
477100979Srwatson			/*
478100979Srwatson			 * Doesn't actually happen, but this allows checking
479100979Srwatson			 * that all enumerated values are handled.
480100979Srwatson			 */
481100979Srwatson			break;
482100979Srwatson		case MAC_DESTROY:
483100979Srwatson			mpc->mpc_ops->mpo_destroy =
484100979Srwatson			    mpe->mpe_function;
485100979Srwatson			break;
486100979Srwatson		case MAC_INIT:
487100979Srwatson			mpc->mpc_ops->mpo_init =
488100979Srwatson			    mpe->mpe_function;
489100979Srwatson			break;
490102123Srwatson		case MAC_SYSCALL:
491102123Srwatson			mpc->mpc_ops->mpo_syscall =
492102123Srwatson			    mpe->mpe_function;
493102123Srwatson			break;
494104514Srwatson		case MAC_INIT_BPFDESC_LABEL:
495104514Srwatson			mpc->mpc_ops->mpo_init_bpfdesc_label =
496100979Srwatson			    mpe->mpe_function;
497100979Srwatson			break;
498104514Srwatson		case MAC_INIT_CRED_LABEL:
499104514Srwatson			mpc->mpc_ops->mpo_init_cred_label =
500100979Srwatson			    mpe->mpe_function;
501100979Srwatson			break;
502104514Srwatson		case MAC_INIT_DEVFSDIRENT_LABEL:
503104514Srwatson			mpc->mpc_ops->mpo_init_devfsdirent_label =
504100979Srwatson			    mpe->mpe_function;
505100979Srwatson			break;
506104514Srwatson		case MAC_INIT_IFNET_LABEL:
507104514Srwatson			mpc->mpc_ops->mpo_init_ifnet_label =
508100979Srwatson			    mpe->mpe_function;
509100979Srwatson			break;
510104514Srwatson		case MAC_INIT_IPQ_LABEL:
511104514Srwatson			mpc->mpc_ops->mpo_init_ipq_label =
512100979Srwatson			    mpe->mpe_function;
513100979Srwatson			break;
514104514Srwatson		case MAC_INIT_MBUF_LABEL:
515104514Srwatson			mpc->mpc_ops->mpo_init_mbuf_label =
516100979Srwatson			    mpe->mpe_function;
517100979Srwatson			break;
518104514Srwatson		case MAC_INIT_MOUNT_LABEL:
519104514Srwatson			mpc->mpc_ops->mpo_init_mount_label =
520100979Srwatson			    mpe->mpe_function;
521100979Srwatson			break;
522104514Srwatson		case MAC_INIT_MOUNT_FS_LABEL:
523104514Srwatson			mpc->mpc_ops->mpo_init_mount_fs_label =
524100979Srwatson			    mpe->mpe_function;
525100979Srwatson			break;
526104514Srwatson		case MAC_INIT_PIPE_LABEL:
527104514Srwatson			mpc->mpc_ops->mpo_init_pipe_label =
528100979Srwatson			    mpe->mpe_function;
529100979Srwatson			break;
530104514Srwatson		case MAC_INIT_SOCKET_LABEL:
531104514Srwatson			mpc->mpc_ops->mpo_init_socket_label =
532100979Srwatson			    mpe->mpe_function;
533100979Srwatson			break;
534104514Srwatson		case MAC_INIT_SOCKET_PEER_LABEL:
535104514Srwatson			mpc->mpc_ops->mpo_init_socket_peer_label =
536100979Srwatson			    mpe->mpe_function;
537100979Srwatson			break;
538104514Srwatson		case MAC_INIT_VNODE_LABEL:
539104514Srwatson			mpc->mpc_ops->mpo_init_vnode_label =
540100979Srwatson			    mpe->mpe_function;
541100979Srwatson			break;
542104514Srwatson		case MAC_DESTROY_BPFDESC_LABEL:
543104514Srwatson			mpc->mpc_ops->mpo_destroy_bpfdesc_label =
544100979Srwatson			    mpe->mpe_function;
545100979Srwatson			break;
546104514Srwatson		case MAC_DESTROY_CRED_LABEL:
547104514Srwatson			mpc->mpc_ops->mpo_destroy_cred_label =
548100979Srwatson			    mpe->mpe_function;
549100979Srwatson			break;
550104514Srwatson		case MAC_DESTROY_DEVFSDIRENT_LABEL:
551104514Srwatson			mpc->mpc_ops->mpo_destroy_devfsdirent_label =
552100979Srwatson			    mpe->mpe_function;
553100979Srwatson			break;
554104514Srwatson		case MAC_DESTROY_IFNET_LABEL:
555104514Srwatson			mpc->mpc_ops->mpo_destroy_ifnet_label =
556100979Srwatson			    mpe->mpe_function;
557100979Srwatson			break;
558104514Srwatson		case MAC_DESTROY_IPQ_LABEL:
559104514Srwatson			mpc->mpc_ops->mpo_destroy_ipq_label =
560100979Srwatson			    mpe->mpe_function;
561100979Srwatson			break;
562104514Srwatson		case MAC_DESTROY_MBUF_LABEL:
563104514Srwatson			mpc->mpc_ops->mpo_destroy_mbuf_label =
564100979Srwatson			    mpe->mpe_function;
565100979Srwatson			break;
566104514Srwatson		case MAC_DESTROY_MOUNT_LABEL:
567104514Srwatson			mpc->mpc_ops->mpo_destroy_mount_label =
568100979Srwatson			    mpe->mpe_function;
569100979Srwatson			break;
570104514Srwatson		case MAC_DESTROY_MOUNT_FS_LABEL:
571104514Srwatson			mpc->mpc_ops->mpo_destroy_mount_fs_label =
572100979Srwatson			    mpe->mpe_function;
573100979Srwatson			break;
574104514Srwatson		case MAC_DESTROY_PIPE_LABEL:
575104514Srwatson			mpc->mpc_ops->mpo_destroy_pipe_label =
576100979Srwatson			    mpe->mpe_function;
577100979Srwatson			break;
578104514Srwatson		case MAC_DESTROY_SOCKET_LABEL:
579104514Srwatson			mpc->mpc_ops->mpo_destroy_socket_label =
580104514Srwatson			    mpe->mpe_function;
581104514Srwatson			break;
582104514Srwatson		case MAC_DESTROY_SOCKET_PEER_LABEL:
583104514Srwatson			mpc->mpc_ops->mpo_destroy_socket_peer_label =
584104514Srwatson			    mpe->mpe_function;
585104514Srwatson			break;
586104514Srwatson		case MAC_DESTROY_VNODE_LABEL:
587104514Srwatson			mpc->mpc_ops->mpo_destroy_vnode_label =
588104514Srwatson			    mpe->mpe_function;
589104514Srwatson			break;
590105694Srwatson		case MAC_COPY_PIPE_LABEL:
591105694Srwatson			mpc->mpc_ops->mpo_copy_pipe_label =
592100979Srwatson			    mpe->mpe_function;
593100979Srwatson			break;
594105694Srwatson		case MAC_COPY_VNODE_LABEL:
595105694Srwatson			mpc->mpc_ops->mpo_copy_vnode_label =
596100979Srwatson			    mpe->mpe_function;
597100979Srwatson			break;
598105694Srwatson		case MAC_EXTERNALIZE_CRED_LABEL:
599105694Srwatson			mpc->mpc_ops->mpo_externalize_cred_label =
600105694Srwatson			    mpe->mpe_function;
601105694Srwatson			break;
602105694Srwatson		case MAC_EXTERNALIZE_IFNET_LABEL:
603105694Srwatson			mpc->mpc_ops->mpo_externalize_ifnet_label =
604105694Srwatson			    mpe->mpe_function;
605105694Srwatson			break;
606105694Srwatson		case MAC_EXTERNALIZE_PIPE_LABEL:
607105694Srwatson			mpc->mpc_ops->mpo_externalize_pipe_label =
608105694Srwatson			    mpe->mpe_function;
609105694Srwatson			break;
610105694Srwatson		case MAC_EXTERNALIZE_SOCKET_LABEL:
611105694Srwatson			mpc->mpc_ops->mpo_externalize_socket_label =
612105694Srwatson			    mpe->mpe_function;
613105694Srwatson			break;
614105694Srwatson		case MAC_EXTERNALIZE_SOCKET_PEER_LABEL:
615105694Srwatson			mpc->mpc_ops->mpo_externalize_socket_peer_label =
616105694Srwatson			    mpe->mpe_function;
617105694Srwatson			break;
618105694Srwatson		case MAC_EXTERNALIZE_VNODE_LABEL:
619105694Srwatson			mpc->mpc_ops->mpo_externalize_vnode_label =
620105694Srwatson			    mpe->mpe_function;
621105694Srwatson			break;
622105694Srwatson		case MAC_INTERNALIZE_CRED_LABEL:
623105694Srwatson			mpc->mpc_ops->mpo_internalize_cred_label =
624105694Srwatson			    mpe->mpe_function;
625105694Srwatson			break;
626105694Srwatson		case MAC_INTERNALIZE_IFNET_LABEL:
627105694Srwatson			mpc->mpc_ops->mpo_internalize_ifnet_label =
628105694Srwatson			    mpe->mpe_function;
629105694Srwatson			break;
630105694Srwatson		case MAC_INTERNALIZE_PIPE_LABEL:
631105694Srwatson			mpc->mpc_ops->mpo_internalize_pipe_label =
632105694Srwatson			    mpe->mpe_function;
633105694Srwatson			break;
634105694Srwatson		case MAC_INTERNALIZE_SOCKET_LABEL:
635105694Srwatson			mpc->mpc_ops->mpo_internalize_socket_label =
636105694Srwatson			    mpe->mpe_function;
637105694Srwatson			break;
638105694Srwatson		case MAC_INTERNALIZE_VNODE_LABEL:
639105694Srwatson			mpc->mpc_ops->mpo_internalize_vnode_label =
640105694Srwatson			    mpe->mpe_function;
641105694Srwatson			break;
642100979Srwatson		case MAC_CREATE_DEVFS_DEVICE:
643100979Srwatson			mpc->mpc_ops->mpo_create_devfs_device =
644100979Srwatson			    mpe->mpe_function;
645100979Srwatson			break;
646100979Srwatson		case MAC_CREATE_DEVFS_DIRECTORY:
647100979Srwatson			mpc->mpc_ops->mpo_create_devfs_directory =
648100979Srwatson			    mpe->mpe_function;
649100979Srwatson			break;
650104533Srwatson		case MAC_CREATE_DEVFS_SYMLINK:
651104533Srwatson			mpc->mpc_ops->mpo_create_devfs_symlink =
652104533Srwatson			    mpe->mpe_function;
653104533Srwatson			break;
654100979Srwatson		case MAC_CREATE_DEVFS_VNODE:
655100979Srwatson			mpc->mpc_ops->mpo_create_devfs_vnode =
656100979Srwatson			    mpe->mpe_function;
657100979Srwatson			break;
658100979Srwatson		case MAC_CREATE_MOUNT:
659100979Srwatson			mpc->mpc_ops->mpo_create_mount =
660100979Srwatson			    mpe->mpe_function;
661100979Srwatson			break;
662100979Srwatson		case MAC_CREATE_ROOT_MOUNT:
663100979Srwatson			mpc->mpc_ops->mpo_create_root_mount =
664100979Srwatson			    mpe->mpe_function;
665100979Srwatson			break;
666100979Srwatson		case MAC_RELABEL_VNODE:
667100979Srwatson			mpc->mpc_ops->mpo_relabel_vnode =
668100979Srwatson			    mpe->mpe_function;
669100979Srwatson			break;
670100979Srwatson		case MAC_UPDATE_DEVFSDIRENT:
671100979Srwatson			mpc->mpc_ops->mpo_update_devfsdirent =
672100979Srwatson			    mpe->mpe_function;
673100979Srwatson			break;
674105988Srwatson		case MAC_ASSOCIATE_VNODE_DEVFS:
675105988Srwatson			mpc->mpc_ops->mpo_associate_vnode_devfs =
676100979Srwatson			    mpe->mpe_function;
677100979Srwatson			break;
678105988Srwatson		case MAC_ASSOCIATE_VNODE_EXTATTR:
679105988Srwatson			mpc->mpc_ops->mpo_associate_vnode_extattr =
680100979Srwatson			    mpe->mpe_function;
681100979Srwatson			break;
682105988Srwatson		case MAC_ASSOCIATE_VNODE_SINGLELABEL:
683105988Srwatson			mpc->mpc_ops->mpo_associate_vnode_singlelabel =
684100979Srwatson			    mpe->mpe_function;
685100979Srwatson			break;
686105988Srwatson		case MAC_CREATE_VNODE_EXTATTR:
687105988Srwatson			mpc->mpc_ops->mpo_create_vnode_extattr =
688100979Srwatson			    mpe->mpe_function;
689100979Srwatson			break;
690105988Srwatson		case MAC_SETLABEL_VNODE_EXTATTR:
691105988Srwatson			mpc->mpc_ops->mpo_setlabel_vnode_extattr =
692105988Srwatson			    mpe->mpe_function;
693105988Srwatson			break;
694100979Srwatson		case MAC_CREATE_MBUF_FROM_SOCKET:
695100979Srwatson			mpc->mpc_ops->mpo_create_mbuf_from_socket =
696100979Srwatson			    mpe->mpe_function;
697100979Srwatson			break;
698100979Srwatson		case MAC_CREATE_PIPE:
699100979Srwatson			mpc->mpc_ops->mpo_create_pipe =
700100979Srwatson			    mpe->mpe_function;
701100979Srwatson			break;
702100979Srwatson		case MAC_CREATE_SOCKET:
703100979Srwatson			mpc->mpc_ops->mpo_create_socket =
704100979Srwatson			    mpe->mpe_function;
705100979Srwatson			break;
706100979Srwatson		case MAC_CREATE_SOCKET_FROM_SOCKET:
707100979Srwatson			mpc->mpc_ops->mpo_create_socket_from_socket =
708100979Srwatson			    mpe->mpe_function;
709100979Srwatson			break;
710100979Srwatson		case MAC_RELABEL_PIPE:
711100979Srwatson			mpc->mpc_ops->mpo_relabel_pipe =
712100979Srwatson			    mpe->mpe_function;
713100979Srwatson			break;
714100979Srwatson		case MAC_RELABEL_SOCKET:
715100979Srwatson			mpc->mpc_ops->mpo_relabel_socket =
716100979Srwatson			    mpe->mpe_function;
717100979Srwatson			break;
718100979Srwatson		case MAC_SET_SOCKET_PEER_FROM_MBUF:
719100979Srwatson			mpc->mpc_ops->mpo_set_socket_peer_from_mbuf =
720100979Srwatson			    mpe->mpe_function;
721100979Srwatson			break;
722100979Srwatson		case MAC_SET_SOCKET_PEER_FROM_SOCKET:
723100979Srwatson			mpc->mpc_ops->mpo_set_socket_peer_from_socket =
724100979Srwatson			    mpe->mpe_function;
725100979Srwatson			break;
726100979Srwatson		case MAC_CREATE_BPFDESC:
727100979Srwatson			mpc->mpc_ops->mpo_create_bpfdesc =
728100979Srwatson			    mpe->mpe_function;
729100979Srwatson			break;
730100979Srwatson		case MAC_CREATE_DATAGRAM_FROM_IPQ:
731100979Srwatson			mpc->mpc_ops->mpo_create_datagram_from_ipq =
732100979Srwatson			    mpe->mpe_function;
733100979Srwatson			break;
734100979Srwatson		case MAC_CREATE_FRAGMENT:
735100979Srwatson			mpc->mpc_ops->mpo_create_fragment =
736100979Srwatson			    mpe->mpe_function;
737100979Srwatson			break;
738100979Srwatson		case MAC_CREATE_IFNET:
739100979Srwatson			mpc->mpc_ops->mpo_create_ifnet =
740100979Srwatson			    mpe->mpe_function;
741100979Srwatson			break;
742100979Srwatson		case MAC_CREATE_IPQ:
743100979Srwatson			mpc->mpc_ops->mpo_create_ipq =
744100979Srwatson			    mpe->mpe_function;
745100979Srwatson			break;
746100979Srwatson		case MAC_CREATE_MBUF_FROM_MBUF:
747100979Srwatson			mpc->mpc_ops->mpo_create_mbuf_from_mbuf =
748100979Srwatson			    mpe->mpe_function;
749100979Srwatson			break;
750100979Srwatson		case MAC_CREATE_MBUF_LINKLAYER:
751100979Srwatson			mpc->mpc_ops->mpo_create_mbuf_linklayer =
752100979Srwatson			    mpe->mpe_function;
753100979Srwatson			break;
754100979Srwatson		case MAC_CREATE_MBUF_FROM_BPFDESC:
755100979Srwatson			mpc->mpc_ops->mpo_create_mbuf_from_bpfdesc =
756100979Srwatson			    mpe->mpe_function;
757100979Srwatson			break;
758100979Srwatson		case MAC_CREATE_MBUF_FROM_IFNET:
759100979Srwatson			mpc->mpc_ops->mpo_create_mbuf_from_ifnet =
760100979Srwatson			    mpe->mpe_function;
761100979Srwatson			break;
762100979Srwatson		case MAC_CREATE_MBUF_MULTICAST_ENCAP:
763100979Srwatson			mpc->mpc_ops->mpo_create_mbuf_multicast_encap =
764100979Srwatson			    mpe->mpe_function;
765100979Srwatson			break;
766100979Srwatson		case MAC_CREATE_MBUF_NETLAYER:
767100979Srwatson			mpc->mpc_ops->mpo_create_mbuf_netlayer =
768100979Srwatson			    mpe->mpe_function;
769100979Srwatson			break;
770100979Srwatson		case MAC_FRAGMENT_MATCH:
771100979Srwatson			mpc->mpc_ops->mpo_fragment_match =
772100979Srwatson			    mpe->mpe_function;
773100979Srwatson			break;
774100979Srwatson		case MAC_RELABEL_IFNET:
775100979Srwatson			mpc->mpc_ops->mpo_relabel_ifnet =
776100979Srwatson			    mpe->mpe_function;
777100979Srwatson			break;
778100979Srwatson		case MAC_UPDATE_IPQ:
779100979Srwatson			mpc->mpc_ops->mpo_update_ipq =
780100979Srwatson			    mpe->mpe_function;
781100979Srwatson			break;
782100979Srwatson		case MAC_CREATE_CRED:
783100979Srwatson			mpc->mpc_ops->mpo_create_cred =
784100979Srwatson			    mpe->mpe_function;
785100979Srwatson			break;
786100979Srwatson		case MAC_EXECVE_TRANSITION:
787100979Srwatson			mpc->mpc_ops->mpo_execve_transition =
788100979Srwatson			    mpe->mpe_function;
789100979Srwatson			break;
790100979Srwatson		case MAC_EXECVE_WILL_TRANSITION:
791100979Srwatson			mpc->mpc_ops->mpo_execve_will_transition =
792100979Srwatson			    mpe->mpe_function;
793100979Srwatson			break;
794100979Srwatson		case MAC_CREATE_PROC0:
795104518Srwatson			mpc->mpc_ops->mpo_create_proc0 =
796104518Srwatson			    mpe->mpe_function;
797100979Srwatson			break;
798100979Srwatson		case MAC_CREATE_PROC1:
799104518Srwatson			mpc->mpc_ops->mpo_create_proc1 =
800104518Srwatson			    mpe->mpe_function;
801100979Srwatson			break;
802100979Srwatson		case MAC_RELABEL_CRED:
803100979Srwatson			mpc->mpc_ops->mpo_relabel_cred =
804100979Srwatson			    mpe->mpe_function;
805100979Srwatson			break;
806104338Srwatson		case MAC_THREAD_USERRET:
807104338Srwatson			mpc->mpc_ops->mpo_thread_userret =
808104338Srwatson			    mpe->mpe_function;
809104338Srwatson			break;
810100979Srwatson		case MAC_CHECK_BPFDESC_RECEIVE:
811100979Srwatson			mpc->mpc_ops->mpo_check_bpfdesc_receive =
812100979Srwatson			    mpe->mpe_function;
813100979Srwatson			break;
814100979Srwatson		case MAC_CHECK_CRED_RELABEL:
815100979Srwatson			mpc->mpc_ops->mpo_check_cred_relabel =
816100979Srwatson			    mpe->mpe_function;
817100979Srwatson			break;
818100979Srwatson		case MAC_CHECK_CRED_VISIBLE:
819100979Srwatson			mpc->mpc_ops->mpo_check_cred_visible =
820100979Srwatson			    mpe->mpe_function;
821100979Srwatson			break;
822100979Srwatson		case MAC_CHECK_IFNET_RELABEL:
823100979Srwatson			mpc->mpc_ops->mpo_check_ifnet_relabel =
824100979Srwatson			    mpe->mpe_function;
825100979Srwatson			break;
826100979Srwatson		case MAC_CHECK_IFNET_TRANSMIT:
827100979Srwatson			mpc->mpc_ops->mpo_check_ifnet_transmit =
828100979Srwatson			    mpe->mpe_function;
829100979Srwatson			break;
830100979Srwatson		case MAC_CHECK_MOUNT_STAT:
831100979Srwatson			mpc->mpc_ops->mpo_check_mount_stat =
832100979Srwatson			    mpe->mpe_function;
833100979Srwatson			break;
834100979Srwatson		case MAC_CHECK_PIPE_IOCTL:
835100979Srwatson			mpc->mpc_ops->mpo_check_pipe_ioctl =
836100979Srwatson			    mpe->mpe_function;
837100979Srwatson			break;
838102115Srwatson		case MAC_CHECK_PIPE_POLL:
839102115Srwatson			mpc->mpc_ops->mpo_check_pipe_poll =
840100979Srwatson			    mpe->mpe_function;
841100979Srwatson			break;
842102115Srwatson		case MAC_CHECK_PIPE_READ:
843102115Srwatson			mpc->mpc_ops->mpo_check_pipe_read =
844102115Srwatson			    mpe->mpe_function;
845102115Srwatson			break;
846100979Srwatson		case MAC_CHECK_PIPE_RELABEL:
847100979Srwatson			mpc->mpc_ops->mpo_check_pipe_relabel =
848100979Srwatson			    mpe->mpe_function;
849100979Srwatson			break;
850102115Srwatson		case MAC_CHECK_PIPE_STAT:
851102115Srwatson			mpc->mpc_ops->mpo_check_pipe_stat =
852102115Srwatson			    mpe->mpe_function;
853102115Srwatson			break;
854102115Srwatson		case MAC_CHECK_PIPE_WRITE:
855102115Srwatson			mpc->mpc_ops->mpo_check_pipe_write =
856102115Srwatson			    mpe->mpe_function;
857102115Srwatson			break;
858100979Srwatson		case MAC_CHECK_PROC_DEBUG:
859100979Srwatson			mpc->mpc_ops->mpo_check_proc_debug =
860100979Srwatson			    mpe->mpe_function;
861100979Srwatson			break;
862100979Srwatson		case MAC_CHECK_PROC_SCHED:
863100979Srwatson			mpc->mpc_ops->mpo_check_proc_sched =
864100979Srwatson			    mpe->mpe_function;
865100979Srwatson			break;
866100979Srwatson		case MAC_CHECK_PROC_SIGNAL:
867100979Srwatson			mpc->mpc_ops->mpo_check_proc_signal =
868100979Srwatson			    mpe->mpe_function;
869100979Srwatson			break;
870100979Srwatson		case MAC_CHECK_SOCKET_BIND:
871100979Srwatson			mpc->mpc_ops->mpo_check_socket_bind =
872100979Srwatson			    mpe->mpe_function;
873100979Srwatson			break;
874100979Srwatson		case MAC_CHECK_SOCKET_CONNECT:
875100979Srwatson			mpc->mpc_ops->mpo_check_socket_connect =
876100979Srwatson			    mpe->mpe_function;
877100979Srwatson			break;
878101933Srwatson		case MAC_CHECK_SOCKET_DELIVER:
879101933Srwatson			mpc->mpc_ops->mpo_check_socket_deliver =
880101933Srwatson			    mpe->mpe_function;
881101933Srwatson			break;
882100979Srwatson		case MAC_CHECK_SOCKET_LISTEN:
883100979Srwatson			mpc->mpc_ops->mpo_check_socket_listen =
884100979Srwatson			    mpe->mpe_function;
885100979Srwatson			break;
886104571Srwatson		case MAC_CHECK_SOCKET_RECEIVE:
887104571Srwatson			mpc->mpc_ops->mpo_check_socket_receive =
888104571Srwatson			    mpe->mpe_function;
889104571Srwatson			break;
890100979Srwatson		case MAC_CHECK_SOCKET_RELABEL:
891100979Srwatson			mpc->mpc_ops->mpo_check_socket_relabel =
892100979Srwatson			    mpe->mpe_function;
893100979Srwatson			break;
894104571Srwatson		case MAC_CHECK_SOCKET_SEND:
895104571Srwatson			mpc->mpc_ops->mpo_check_socket_send =
896104571Srwatson			    mpe->mpe_function;
897104571Srwatson			break;
898100979Srwatson		case MAC_CHECK_SOCKET_VISIBLE:
899100979Srwatson			mpc->mpc_ops->mpo_check_socket_visible =
900100979Srwatson			    mpe->mpe_function;
901100979Srwatson			break;
902106023Srwatson		case MAC_CHECK_SYSTEM_SWAPON:
903106023Srwatson			mpc->mpc_ops->mpo_check_system_swapon =
904106023Srwatson			    mpe->mpe_function;
905106023Srwatson			break;
906100979Srwatson		case MAC_CHECK_VNODE_ACCESS:
907100979Srwatson			mpc->mpc_ops->mpo_check_vnode_access =
908100979Srwatson			    mpe->mpe_function;
909100979Srwatson			break;
910100979Srwatson		case MAC_CHECK_VNODE_CHDIR:
911100979Srwatson			mpc->mpc_ops->mpo_check_vnode_chdir =
912100979Srwatson			    mpe->mpe_function;
913100979Srwatson			break;
914100979Srwatson		case MAC_CHECK_VNODE_CHROOT:
915100979Srwatson			mpc->mpc_ops->mpo_check_vnode_chroot =
916100979Srwatson			    mpe->mpe_function;
917100979Srwatson			break;
918100979Srwatson		case MAC_CHECK_VNODE_CREATE:
919100979Srwatson			mpc->mpc_ops->mpo_check_vnode_create =
920100979Srwatson			    mpe->mpe_function;
921100979Srwatson			break;
922100979Srwatson		case MAC_CHECK_VNODE_DELETE:
923100979Srwatson			mpc->mpc_ops->mpo_check_vnode_delete =
924100979Srwatson			    mpe->mpe_function;
925100979Srwatson			break;
926100979Srwatson		case MAC_CHECK_VNODE_DELETEACL:
927100979Srwatson			mpc->mpc_ops->mpo_check_vnode_deleteacl =
928100979Srwatson			    mpe->mpe_function;
929100979Srwatson			break;
930100979Srwatson		case MAC_CHECK_VNODE_EXEC:
931100979Srwatson			mpc->mpc_ops->mpo_check_vnode_exec =
932100979Srwatson			    mpe->mpe_function;
933100979Srwatson			break;
934100979Srwatson		case MAC_CHECK_VNODE_GETACL:
935100979Srwatson			mpc->mpc_ops->mpo_check_vnode_getacl =
936100979Srwatson			    mpe->mpe_function;
937100979Srwatson			break;
938100979Srwatson		case MAC_CHECK_VNODE_GETEXTATTR:
939100979Srwatson			mpc->mpc_ops->mpo_check_vnode_getextattr =
940100979Srwatson			    mpe->mpe_function;
941100979Srwatson			break;
942104529Srwatson		case MAC_CHECK_VNODE_LINK:
943104529Srwatson			mpc->mpc_ops->mpo_check_vnode_link =
944104529Srwatson			    mpe->mpe_function;
945104529Srwatson			break;
946100979Srwatson		case MAC_CHECK_VNODE_LOOKUP:
947100979Srwatson			mpc->mpc_ops->mpo_check_vnode_lookup =
948100979Srwatson			    mpe->mpe_function;
949100979Srwatson			break;
950104546Srwatson		case MAC_CHECK_VNODE_MMAP:
951104546Srwatson			mpc->mpc_ops->mpo_check_vnode_mmap =
952100979Srwatson			    mpe->mpe_function;
953100979Srwatson			break;
954104546Srwatson		case MAC_CHECK_VNODE_MMAP_DOWNGRADE:
955104546Srwatson			mpc->mpc_ops->mpo_check_vnode_mmap_downgrade =
956104546Srwatson			    mpe->mpe_function;
957104546Srwatson			break;
958104546Srwatson		case MAC_CHECK_VNODE_MPROTECT:
959104546Srwatson			mpc->mpc_ops->mpo_check_vnode_mprotect =
960104546Srwatson			    mpe->mpe_function;
961104546Srwatson			break;
962100979Srwatson		case MAC_CHECK_VNODE_OPEN:
963100979Srwatson			mpc->mpc_ops->mpo_check_vnode_open =
964100979Srwatson			    mpe->mpe_function;
965100979Srwatson			break;
966102112Srwatson		case MAC_CHECK_VNODE_POLL:
967102112Srwatson			mpc->mpc_ops->mpo_check_vnode_poll =
968102112Srwatson			    mpe->mpe_function;
969102112Srwatson			break;
970102112Srwatson		case MAC_CHECK_VNODE_READ:
971102112Srwatson			mpc->mpc_ops->mpo_check_vnode_read =
972102112Srwatson			    mpe->mpe_function;
973102112Srwatson			break;
974100979Srwatson		case MAC_CHECK_VNODE_READDIR:
975100979Srwatson			mpc->mpc_ops->mpo_check_vnode_readdir =
976100979Srwatson			    mpe->mpe_function;
977100979Srwatson			break;
978100979Srwatson		case MAC_CHECK_VNODE_READLINK:
979100979Srwatson			mpc->mpc_ops->mpo_check_vnode_readlink =
980100979Srwatson			    mpe->mpe_function;
981100979Srwatson			break;
982100979Srwatson		case MAC_CHECK_VNODE_RELABEL:
983100979Srwatson			mpc->mpc_ops->mpo_check_vnode_relabel =
984100979Srwatson			    mpe->mpe_function;
985100979Srwatson			break;
986100979Srwatson		case MAC_CHECK_VNODE_RENAME_FROM:
987100979Srwatson			mpc->mpc_ops->mpo_check_vnode_rename_from =
988100979Srwatson			    mpe->mpe_function;
989100979Srwatson			break;
990100979Srwatson		case MAC_CHECK_VNODE_RENAME_TO:
991100979Srwatson			mpc->mpc_ops->mpo_check_vnode_rename_to =
992100979Srwatson			    mpe->mpe_function;
993100979Srwatson			break;
994100979Srwatson		case MAC_CHECK_VNODE_REVOKE:
995100979Srwatson			mpc->mpc_ops->mpo_check_vnode_revoke =
996100979Srwatson			    mpe->mpe_function;
997100979Srwatson			break;
998100979Srwatson		case MAC_CHECK_VNODE_SETACL:
999100979Srwatson			mpc->mpc_ops->mpo_check_vnode_setacl =
1000100979Srwatson			    mpe->mpe_function;
1001100979Srwatson			break;
1002100979Srwatson		case MAC_CHECK_VNODE_SETEXTATTR:
1003100979Srwatson			mpc->mpc_ops->mpo_check_vnode_setextattr =
1004100979Srwatson			    mpe->mpe_function;
1005100979Srwatson			break;
1006100979Srwatson		case MAC_CHECK_VNODE_SETFLAGS:
1007100979Srwatson			mpc->mpc_ops->mpo_check_vnode_setflags =
1008100979Srwatson			    mpe->mpe_function;
1009100979Srwatson			break;
1010100979Srwatson		case MAC_CHECK_VNODE_SETMODE:
1011100979Srwatson			mpc->mpc_ops->mpo_check_vnode_setmode =
1012100979Srwatson			    mpe->mpe_function;
1013100979Srwatson			break;
1014100979Srwatson		case MAC_CHECK_VNODE_SETOWNER:
1015100979Srwatson			mpc->mpc_ops->mpo_check_vnode_setowner =
1016100979Srwatson			    mpe->mpe_function;
1017100979Srwatson			break;
1018100979Srwatson		case MAC_CHECK_VNODE_SETUTIMES:
1019100979Srwatson			mpc->mpc_ops->mpo_check_vnode_setutimes =
1020100979Srwatson			    mpe->mpe_function;
1021100979Srwatson			break;
1022100979Srwatson		case MAC_CHECK_VNODE_STAT:
1023100979Srwatson			mpc->mpc_ops->mpo_check_vnode_stat =
1024100979Srwatson			    mpe->mpe_function;
1025100979Srwatson			break;
1026102112Srwatson		case MAC_CHECK_VNODE_WRITE:
1027102112Srwatson			mpc->mpc_ops->mpo_check_vnode_write =
1028102112Srwatson			    mpe->mpe_function;
1029102112Srwatson			break;
1030100979Srwatson/*
1031100979Srwatson		default:
1032100979Srwatson			printf("MAC policy `%s': unknown operation %d\n",
1033100979Srwatson			    mpc->mpc_name, mpe->mpe_constant);
1034100979Srwatson			return (EINVAL);
1035100979Srwatson*/
1036100979Srwatson		}
1037100979Srwatson	}
1038100979Srwatson	MAC_POLICY_LIST_LOCK();
1039100979Srwatson	if (mac_policy_list_busy > 0) {
1040100979Srwatson		MAC_POLICY_LIST_UNLOCK();
1041100979Srwatson		FREE(mpc->mpc_ops, M_MACOPVEC);
1042100979Srwatson		mpc->mpc_ops = NULL;
1043100979Srwatson		return (EBUSY);
1044100979Srwatson	}
1045100979Srwatson	LIST_FOREACH(tmpc, &mac_policy_list, mpc_list) {
1046100979Srwatson		if (strcmp(tmpc->mpc_name, mpc->mpc_name) == 0) {
1047100979Srwatson			MAC_POLICY_LIST_UNLOCK();
1048100979Srwatson			FREE(mpc->mpc_ops, M_MACOPVEC);
1049100979Srwatson			mpc->mpc_ops = NULL;
1050100979Srwatson			return (EEXIST);
1051100979Srwatson		}
1052100979Srwatson	}
1053100979Srwatson	if (mpc->mpc_field_off != NULL) {
1054100979Srwatson		slot = ffs(mac_policy_offsets_free);
1055100979Srwatson		if (slot == 0) {
1056100979Srwatson			MAC_POLICY_LIST_UNLOCK();
1057100979Srwatson			FREE(mpc->mpc_ops, M_MACOPVEC);
1058100979Srwatson			mpc->mpc_ops = NULL;
1059100979Srwatson			return (ENOMEM);
1060100979Srwatson		}
1061100979Srwatson		slot--;
1062100979Srwatson		mac_policy_offsets_free &= ~(1 << slot);
1063100979Srwatson		*mpc->mpc_field_off = slot;
1064100979Srwatson	}
1065100979Srwatson	mpc->mpc_runtime_flags |= MPC_RUNTIME_FLAG_REGISTERED;
1066100979Srwatson	LIST_INSERT_HEAD(&mac_policy_list, mpc, mpc_list);
1067100979Srwatson
1068100979Srwatson	/* Per-policy initialization. */
1069100979Srwatson	if (mpc->mpc_ops->mpo_init != NULL)
1070100979Srwatson		(*(mpc->mpc_ops->mpo_init))(mpc);
1071100979Srwatson	MAC_POLICY_LIST_UNLOCK();
1072100979Srwatson
1073100979Srwatson	printf("Security policy loaded: %s (%s)\n", mpc->mpc_fullname,
1074100979Srwatson	    mpc->mpc_name);
1075100979Srwatson
1076100979Srwatson	return (0);
1077100979Srwatson}
1078100979Srwatson
1079100979Srwatsonstatic int
1080100979Srwatsonmac_policy_unregister(struct mac_policy_conf *mpc)
1081100979Srwatson{
1082100979Srwatson
1083104520Srwatson	/*
1084104520Srwatson	 * If we fail the load, we may get a request to unload.  Check
1085104520Srwatson	 * to see if we did the run-time registration, and if not,
1086104520Srwatson	 * silently succeed.
1087104520Srwatson	 */
1088104520Srwatson	MAC_POLICY_LIST_LOCK();
1089104520Srwatson	if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED) == 0) {
1090104520Srwatson		MAC_POLICY_LIST_UNLOCK();
1091104520Srwatson		return (0);
1092104520Srwatson	}
1093100979Srwatson#if 0
1094100979Srwatson	/*
1095100979Srwatson	 * Don't allow unloading modules with private data.
1096100979Srwatson	 */
1097104520Srwatson	if (mpc->mpc_field_off != NULL) {
1098104520Srwatson		MAC_POLICY_LIST_UNLOCK();
1099100979Srwatson		return (EBUSY);
1100104520Srwatson	}
1101100979Srwatson#endif
1102104520Srwatson	/*
1103104520Srwatson	 * Only allow the unload to proceed if the module is unloadable
1104104520Srwatson	 * by its own definition.
1105104520Srwatson	 */
1106104520Srwatson	if ((mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK) == 0) {
1107104520Srwatson		MAC_POLICY_LIST_UNLOCK();
1108100979Srwatson		return (EBUSY);
1109104520Srwatson	}
1110104520Srwatson	/*
1111104520Srwatson	 * Right now, we EBUSY if the list is in use.  In the future,
1112104520Srwatson	 * for reliability reasons, we might want to sleep and wakeup
1113104520Srwatson	 * later to try again.
1114104520Srwatson	 */
1115100979Srwatson	if (mac_policy_list_busy > 0) {
1116100979Srwatson		MAC_POLICY_LIST_UNLOCK();
1117100979Srwatson		return (EBUSY);
1118100979Srwatson	}
1119100979Srwatson	if (mpc->mpc_ops->mpo_destroy != NULL)
1120100979Srwatson		(*(mpc->mpc_ops->mpo_destroy))(mpc);
1121100979Srwatson
1122100979Srwatson	LIST_REMOVE(mpc, mpc_list);
1123100979Srwatson	MAC_POLICY_LIST_UNLOCK();
1124100979Srwatson
1125100979Srwatson	FREE(mpc->mpc_ops, M_MACOPVEC);
1126100979Srwatson	mpc->mpc_ops = NULL;
1127105474Srwatson	mpc->mpc_runtime_flags &= ~MPC_RUNTIME_FLAG_REGISTERED;
1128100979Srwatson
1129100979Srwatson	printf("Security policy unload: %s (%s)\n", mpc->mpc_fullname,
1130100979Srwatson	    mpc->mpc_name);
1131100979Srwatson
1132100979Srwatson	return (0);
1133100979Srwatson}
1134100979Srwatson
1135100979Srwatson/*
1136100979Srwatson * Define an error value precedence, and given two arguments, selects the
1137100979Srwatson * value with the higher precedence.
1138100979Srwatson */
1139100979Srwatsonstatic int
1140100979Srwatsonerror_select(int error1, int error2)
1141100979Srwatson{
1142100979Srwatson
1143100979Srwatson	/* Certain decision-making errors take top priority. */
1144100979Srwatson	if (error1 == EDEADLK || error2 == EDEADLK)
1145100979Srwatson		return (EDEADLK);
1146100979Srwatson
1147100979Srwatson	/* Invalid arguments should be reported where possible. */
1148100979Srwatson	if (error1 == EINVAL || error2 == EINVAL)
1149100979Srwatson		return (EINVAL);
1150100979Srwatson
1151100979Srwatson	/* Precedence goes to "visibility", with both process and file. */
1152100979Srwatson	if (error1 == ESRCH || error2 == ESRCH)
1153100979Srwatson		return (ESRCH);
1154100979Srwatson
1155100979Srwatson	if (error1 == ENOENT || error2 == ENOENT)
1156100979Srwatson		return (ENOENT);
1157100979Srwatson
1158100979Srwatson	/* Precedence goes to DAC/MAC protections. */
1159100979Srwatson	if (error1 == EACCES || error2 == EACCES)
1160100979Srwatson		return (EACCES);
1161100979Srwatson
1162100979Srwatson	/* Precedence goes to privilege. */
1163100979Srwatson	if (error1 == EPERM || error2 == EPERM)
1164100979Srwatson		return (EPERM);
1165100979Srwatson
1166100979Srwatson	/* Precedence goes to error over success; otherwise, arbitrary. */
1167100979Srwatson	if (error1 != 0)
1168100979Srwatson		return (error1);
1169100979Srwatson	return (error2);
1170100979Srwatson}
1171100979Srwatson
1172104521Srwatsonstatic void
1173104521Srwatsonmac_init_label(struct label *label)
1174104521Srwatson{
1175104521Srwatson
1176104521Srwatson	bzero(label, sizeof(*label));
1177104521Srwatson	label->l_flags = MAC_FLAG_INITIALIZED;
1178104521Srwatson}
1179104521Srwatson
1180104521Srwatsonstatic void
1181104521Srwatsonmac_destroy_label(struct label *label)
1182104521Srwatson{
1183104521Srwatson
1184104521Srwatson	KASSERT(label->l_flags & MAC_FLAG_INITIALIZED,
1185104521Srwatson	    ("destroying uninitialized label"));
1186104521Srwatson
1187104521Srwatson	bzero(label, sizeof(*label));
1188104521Srwatson	/* implicit: label->l_flags &= ~MAC_FLAG_INITIALIZED; */
1189104521Srwatson}
1190104521Srwatson
1191100979Srwatsonvoid
1192104527Srwatsonmac_init_bpfdesc(struct bpf_d *bpf_d)
1193104521Srwatson{
1194104521Srwatson
1195104527Srwatson	mac_init_label(&bpf_d->bd_label);
1196104527Srwatson	MAC_PERFORM(init_bpfdesc_label, &bpf_d->bd_label);
1197104521Srwatson#ifdef MAC_DEBUG
1198104527Srwatson	atomic_add_int(&nmacbpfdescs, 1);
1199104521Srwatson#endif
1200104521Srwatson}
1201104521Srwatson
1202105694Srwatsonstatic void
1203105694Srwatsonmac_init_cred_label(struct label *label)
1204104521Srwatson{
1205104521Srwatson
1206105694Srwatson	mac_init_label(label);
1207105694Srwatson	MAC_PERFORM(init_cred_label, label);
1208104521Srwatson#ifdef MAC_DEBUG
1209104521Srwatson	atomic_add_int(&nmaccreds, 1);
1210104521Srwatson#endif
1211104521Srwatson}
1212104521Srwatson
1213104521Srwatsonvoid
1214105694Srwatsonmac_init_cred(struct ucred *cred)
1215105694Srwatson{
1216105694Srwatson
1217105694Srwatson	mac_init_cred_label(&cred->cr_label);
1218105694Srwatson}
1219105694Srwatson
1220105694Srwatsonvoid
1221104527Srwatsonmac_init_devfsdirent(struct devfs_dirent *de)
1222104521Srwatson{
1223104521Srwatson
1224104527Srwatson	mac_init_label(&de->de_label);
1225104527Srwatson	MAC_PERFORM(init_devfsdirent_label, &de->de_label);
1226104521Srwatson#ifdef MAC_DEBUG
1227104527Srwatson	atomic_add_int(&nmacdevfsdirents, 1);
1228104521Srwatson#endif
1229104521Srwatson}
1230104521Srwatson
1231105694Srwatsonstatic void
1232105694Srwatsonmac_init_ifnet_label(struct label *label)
1233104521Srwatson{
1234104521Srwatson
1235105694Srwatson	mac_init_label(label);
1236105694Srwatson	MAC_PERFORM(init_ifnet_label, label);
1237104521Srwatson#ifdef MAC_DEBUG
1238104521Srwatson	atomic_add_int(&nmacifnets, 1);
1239104521Srwatson#endif
1240104521Srwatson}
1241104521Srwatson
1242104521Srwatsonvoid
1243105694Srwatsonmac_init_ifnet(struct ifnet *ifp)
1244105694Srwatson{
1245105694Srwatson
1246105694Srwatson	mac_init_ifnet_label(&ifp->if_label);
1247105694Srwatson}
1248105694Srwatson
1249105694Srwatsonvoid
1250104527Srwatsonmac_init_ipq(struct ipq *ipq)
1251104521Srwatson{
1252104521Srwatson
1253104527Srwatson	mac_init_label(&ipq->ipq_label);
1254104527Srwatson	MAC_PERFORM(init_ipq_label, &ipq->ipq_label);
1255104521Srwatson#ifdef MAC_DEBUG
1256104527Srwatson	atomic_add_int(&nmacipqs, 1);
1257104521Srwatson#endif
1258104521Srwatson}
1259104521Srwatson
1260104527Srwatsonint
1261104527Srwatsonmac_init_mbuf(struct mbuf *m, int flag)
1262104527Srwatson{
1263104528Srwatson	int error;
1264104528Srwatson
1265104527Srwatson	KASSERT(m->m_flags & M_PKTHDR, ("mac_init_mbuf on non-header mbuf"));
1266104527Srwatson
1267104527Srwatson	mac_init_label(&m->m_pkthdr.label);
1268104527Srwatson
1269104528Srwatson	MAC_CHECK(init_mbuf_label, &m->m_pkthdr.label, flag);
1270104528Srwatson	if (error) {
1271104528Srwatson		MAC_PERFORM(destroy_mbuf_label, &m->m_pkthdr.label);
1272104528Srwatson		mac_destroy_label(&m->m_pkthdr.label);
1273104528Srwatson	}
1274104528Srwatson
1275104527Srwatson#ifdef MAC_DEBUG
1276104528Srwatson	if (error == 0)
1277104528Srwatson		atomic_add_int(&nmacmbufs, 1);
1278104527Srwatson#endif
1279104528Srwatson	return (error);
1280104527Srwatson}
1281104527Srwatson
1282104521Srwatsonvoid
1283104527Srwatsonmac_init_mount(struct mount *mp)
1284104521Srwatson{
1285104521Srwatson
1286104527Srwatson	mac_init_label(&mp->mnt_mntlabel);
1287104527Srwatson	mac_init_label(&mp->mnt_fslabel);
1288104527Srwatson	MAC_PERFORM(init_mount_label, &mp->mnt_mntlabel);
1289104527Srwatson	MAC_PERFORM(init_mount_fs_label, &mp->mnt_fslabel);
1290104521Srwatson#ifdef MAC_DEBUG
1291104527Srwatson	atomic_add_int(&nmacmounts, 1);
1292104521Srwatson#endif
1293104521Srwatson}
1294104521Srwatson
1295105694Srwatsonstatic void
1296105694Srwatsonmac_init_pipe_label(struct label *label)
1297105694Srwatson{
1298105694Srwatson
1299105694Srwatson	mac_init_label(label);
1300105694Srwatson	MAC_PERFORM(init_pipe_label, label);
1301105694Srwatson#ifdef MAC_DEBUG
1302105694Srwatson	atomic_add_int(&nmacpipes, 1);
1303105694Srwatson#endif
1304105694Srwatson}
1305105694Srwatson
1306104521Srwatsonvoid
1307104527Srwatsonmac_init_pipe(struct pipe *pipe)
1308104521Srwatson{
1309104527Srwatson	struct label *label;
1310104521Srwatson
1311104527Srwatson	label = malloc(sizeof(struct label), M_MACPIPELABEL, M_ZERO|M_WAITOK);
1312104527Srwatson	pipe->pipe_label = label;
1313104527Srwatson	pipe->pipe_peer->pipe_label = label;
1314105694Srwatson	mac_init_pipe_label(label);
1315104521Srwatson}
1316104521Srwatson
1317104541Srwatsonstatic int
1318104541Srwatsonmac_init_socket_label(struct label *label, int flag)
1319104521Srwatson{
1320104541Srwatson	int error;
1321104521Srwatson
1322104541Srwatson	mac_init_label(label);
1323104541Srwatson
1324104541Srwatson	MAC_CHECK(init_socket_label, label, flag);
1325104541Srwatson	if (error) {
1326104541Srwatson		MAC_PERFORM(destroy_socket_label, label);
1327104541Srwatson		mac_destroy_label(label);
1328104541Srwatson	}
1329104541Srwatson
1330104521Srwatson#ifdef MAC_DEBUG
1331104541Srwatson	if (error == 0)
1332104541Srwatson		atomic_add_int(&nmacsockets, 1);
1333104521Srwatson#endif
1334104541Srwatson
1335104541Srwatson	return (error);
1336104521Srwatson}
1337104521Srwatson
1338104541Srwatsonstatic int
1339104541Srwatsonmac_init_socket_peer_label(struct label *label, int flag)
1340104541Srwatson{
1341104541Srwatson	int error;
1342104541Srwatson
1343104541Srwatson	mac_init_label(label);
1344104541Srwatson
1345104541Srwatson	MAC_CHECK(init_socket_peer_label, label, flag);
1346104541Srwatson	if (error) {
1347104541Srwatson		MAC_PERFORM(destroy_socket_label, label);
1348104541Srwatson		mac_destroy_label(label);
1349104541Srwatson	}
1350104541Srwatson
1351104541Srwatson	return (error);
1352104541Srwatson}
1353104541Srwatson
1354104541Srwatsonint
1355104541Srwatsonmac_init_socket(struct socket *socket, int flag)
1356104541Srwatson{
1357104541Srwatson	int error;
1358104541Srwatson
1359104541Srwatson	error = mac_init_socket_label(&socket->so_label, flag);
1360104541Srwatson	if (error)
1361104541Srwatson		return (error);
1362104541Srwatson
1363104541Srwatson	error = mac_init_socket_peer_label(&socket->so_peerlabel, flag);
1364104541Srwatson	if (error)
1365104541Srwatson		mac_destroy_socket_label(&socket->so_label);
1366104541Srwatson
1367104541Srwatson	return (error);
1368104541Srwatson}
1369104541Srwatson
1370105988Srwatsonvoid
1371105694Srwatsonmac_init_vnode_label(struct label *label)
1372104521Srwatson{
1373104521Srwatson
1374104527Srwatson	mac_init_label(label);
1375105694Srwatson	MAC_PERFORM(init_vnode_label, label);
1376104521Srwatson#ifdef MAC_DEBUG
1377105694Srwatson	atomic_add_int(&nmacvnodes, 1);
1378104521Srwatson#endif
1379104521Srwatson}
1380104521Srwatson
1381104521Srwatsonvoid
1382104527Srwatsonmac_init_vnode(struct vnode *vp)
1383104521Srwatson{
1384104521Srwatson
1385105694Srwatson	mac_init_vnode_label(&vp->v_label);
1386104521Srwatson}
1387104521Srwatson
1388104521Srwatsonvoid
1389104527Srwatsonmac_destroy_bpfdesc(struct bpf_d *bpf_d)
1390104521Srwatson{
1391104521Srwatson
1392104527Srwatson	MAC_PERFORM(destroy_bpfdesc_label, &bpf_d->bd_label);
1393104527Srwatson	mac_destroy_label(&bpf_d->bd_label);
1394104521Srwatson#ifdef MAC_DEBUG
1395104527Srwatson	atomic_subtract_int(&nmacbpfdescs, 1);
1396104521Srwatson#endif
1397104521Srwatson}
1398104521Srwatson
1399105694Srwatsonstatic void
1400105694Srwatsonmac_destroy_cred_label(struct label *label)
1401104521Srwatson{
1402104521Srwatson
1403105694Srwatson	MAC_PERFORM(destroy_cred_label, label);
1404105694Srwatson	mac_destroy_label(label);
1405104521Srwatson#ifdef MAC_DEBUG
1406104527Srwatson	atomic_subtract_int(&nmaccreds, 1);
1407104521Srwatson#endif
1408104521Srwatson}
1409104521Srwatson
1410104521Srwatsonvoid
1411105694Srwatsonmac_destroy_cred(struct ucred *cred)
1412105694Srwatson{
1413105694Srwatson
1414105694Srwatson	mac_destroy_cred_label(&cred->cr_label);
1415105694Srwatson}
1416105694Srwatson
1417105694Srwatsonvoid
1418104527Srwatsonmac_destroy_devfsdirent(struct devfs_dirent *de)
1419104521Srwatson{
1420104521Srwatson
1421104527Srwatson	MAC_PERFORM(destroy_devfsdirent_label, &de->de_label);
1422104527Srwatson	mac_destroy_label(&de->de_label);
1423104521Srwatson#ifdef MAC_DEBUG
1424104527Srwatson	atomic_subtract_int(&nmacdevfsdirents, 1);
1425104521Srwatson#endif
1426104521Srwatson}
1427104521Srwatson
1428105694Srwatsonstatic void
1429105694Srwatsonmac_destroy_ifnet_label(struct label *label)
1430104521Srwatson{
1431104521Srwatson
1432105694Srwatson	MAC_PERFORM(destroy_ifnet_label, label);
1433105694Srwatson	mac_destroy_label(label);
1434104521Srwatson#ifdef MAC_DEBUG
1435104527Srwatson	atomic_subtract_int(&nmacifnets, 1);
1436104521Srwatson#endif
1437104521Srwatson}
1438104521Srwatson
1439104521Srwatsonvoid
1440105694Srwatsonmac_destroy_ifnet(struct ifnet *ifp)
1441105694Srwatson{
1442105694Srwatson
1443105694Srwatson	mac_destroy_ifnet_label(&ifp->if_label);
1444105694Srwatson}
1445105694Srwatson
1446105694Srwatsonvoid
1447104527Srwatsonmac_destroy_ipq(struct ipq *ipq)
1448104521Srwatson{
1449104521Srwatson
1450104527Srwatson	MAC_PERFORM(destroy_ipq_label, &ipq->ipq_label);
1451104527Srwatson	mac_destroy_label(&ipq->ipq_label);
1452104521Srwatson#ifdef MAC_DEBUG
1453104527Srwatson	atomic_subtract_int(&nmacipqs, 1);
1454104521Srwatson#endif
1455104521Srwatson}
1456104521Srwatson
1457104527Srwatsonvoid
1458104527Srwatsonmac_destroy_mbuf(struct mbuf *m)
1459104521Srwatson{
1460104521Srwatson
1461104527Srwatson	MAC_PERFORM(destroy_mbuf_label, &m->m_pkthdr.label);
1462104527Srwatson	mac_destroy_label(&m->m_pkthdr.label);
1463104521Srwatson#ifdef MAC_DEBUG
1464104527Srwatson	atomic_subtract_int(&nmacmbufs, 1);
1465104521Srwatson#endif
1466104521Srwatson}
1467104521Srwatson
1468104527Srwatsonvoid
1469104527Srwatsonmac_destroy_mount(struct mount *mp)
1470104521Srwatson{
1471104521Srwatson
1472104527Srwatson	MAC_PERFORM(destroy_mount_label, &mp->mnt_mntlabel);
1473104527Srwatson	MAC_PERFORM(destroy_mount_fs_label, &mp->mnt_fslabel);
1474104527Srwatson	mac_destroy_label(&mp->mnt_fslabel);
1475104527Srwatson	mac_destroy_label(&mp->mnt_mntlabel);
1476104521Srwatson#ifdef MAC_DEBUG
1477104527Srwatson	atomic_subtract_int(&nmacmounts, 1);
1478104521Srwatson#endif
1479104521Srwatson}
1480104521Srwatson
1481105694Srwatsonstatic void
1482105694Srwatsonmac_destroy_pipe_label(struct label *label)
1483104521Srwatson{
1484104521Srwatson
1485105694Srwatson	MAC_PERFORM(destroy_pipe_label, label);
1486105694Srwatson	mac_destroy_label(label);
1487104521Srwatson#ifdef MAC_DEBUG
1488104527Srwatson	atomic_subtract_int(&nmacpipes, 1);
1489104521Srwatson#endif
1490104521Srwatson}
1491104521Srwatson
1492105694Srwatsonvoid
1493105694Srwatsonmac_destroy_pipe(struct pipe *pipe)
1494105694Srwatson{
1495105694Srwatson
1496105694Srwatson	mac_destroy_pipe_label(pipe->pipe_label);
1497105694Srwatson	free(pipe->pipe_label, M_MACPIPELABEL);
1498105694Srwatson}
1499105694Srwatson
1500104541Srwatsonstatic void
1501104541Srwatsonmac_destroy_socket_label(struct label *label)
1502104521Srwatson{
1503104521Srwatson
1504104541Srwatson	MAC_PERFORM(destroy_socket_label, label);
1505104541Srwatson	mac_destroy_label(label);
1506104521Srwatson#ifdef MAC_DEBUG
1507104527Srwatson	atomic_subtract_int(&nmacsockets, 1);
1508104521Srwatson#endif
1509104521Srwatson}
1510104521Srwatson
1511104527Srwatsonstatic void
1512104541Srwatsonmac_destroy_socket_peer_label(struct label *label)
1513104541Srwatson{
1514104541Srwatson
1515104541Srwatson	MAC_PERFORM(destroy_socket_peer_label, label);
1516104541Srwatson	mac_destroy_label(label);
1517104541Srwatson}
1518104541Srwatson
1519104541Srwatsonvoid
1520104541Srwatsonmac_destroy_socket(struct socket *socket)
1521104541Srwatson{
1522104541Srwatson
1523104541Srwatson	mac_destroy_socket_label(&socket->so_label);
1524104541Srwatson	mac_destroy_socket_peer_label(&socket->so_peerlabel);
1525104541Srwatson}
1526104541Srwatson
1527105988Srwatsonvoid
1528105694Srwatsonmac_destroy_vnode_label(struct label *label)
1529104521Srwatson{
1530104521Srwatson
1531105694Srwatson	MAC_PERFORM(destroy_vnode_label, label);
1532104527Srwatson	mac_destroy_label(label);
1533104521Srwatson#ifdef MAC_DEBUG
1534105694Srwatson	atomic_subtract_int(&nmacvnodes, 1);
1535104521Srwatson#endif
1536104521Srwatson}
1537104521Srwatson
1538104521Srwatsonvoid
1539104527Srwatsonmac_destroy_vnode(struct vnode *vp)
1540104521Srwatson{
1541104521Srwatson
1542105694Srwatson	mac_destroy_vnode_label(&vp->v_label);
1543104521Srwatson}
1544104521Srwatson
1545105694Srwatsonstatic void
1546105694Srwatsonmac_copy_pipe_label(struct label *src, struct label *dest)
1547105694Srwatson{
1548105694Srwatson
1549105694Srwatson	MAC_PERFORM(copy_pipe_label, src, dest);
1550105694Srwatson}
1551105694Srwatson
1552105988Srwatsonvoid
1553105694Srwatsonmac_copy_vnode_label(struct label *src, struct label *dest)
1554105694Srwatson{
1555105694Srwatson
1556105694Srwatson	MAC_PERFORM(copy_vnode_label, src, dest);
1557105694Srwatson}
1558105694Srwatson
1559104522Srwatsonstatic int
1560105694Srwatsonmac_check_structmac_consistent(struct mac *mac)
1561104522Srwatson{
1562105694Srwatson
1563105694Srwatson	if (mac->m_buflen > MAC_MAX_LABEL_BUF_LEN)
1564105694Srwatson		return (EINVAL);
1565105694Srwatson
1566105694Srwatson	return (0);
1567105694Srwatson}
1568105694Srwatson
1569105694Srwatsonstatic int
1570105694Srwatsonmac_externalize_cred_label(struct label *label, char *elements,
1571105694Srwatson    char *outbuf, size_t outbuflen, int flags)
1572105694Srwatson{
1573104522Srwatson	int error;
1574104522Srwatson
1575105694Srwatson	MAC_EXTERNALIZE(cred_label, label, elements, outbuf, outbuflen);
1576104522Srwatson
1577104522Srwatson	return (error);
1578104522Srwatson}
1579104522Srwatson
1580104522Srwatsonstatic int
1581105694Srwatsonmac_externalize_ifnet_label(struct label *label, char *elements,
1582105694Srwatson    char *outbuf, size_t outbuflen, int flags)
1583104522Srwatson{
1584104522Srwatson	int error;
1585104522Srwatson
1586105694Srwatson	MAC_EXTERNALIZE(ifnet_label, label, elements, outbuf, outbuflen);
1587104522Srwatson
1588104522Srwatson	return (error);
1589104522Srwatson}
1590104522Srwatson
1591105694Srwatsonstatic int
1592105694Srwatsonmac_externalize_pipe_label(struct label *label, char *elements,
1593105694Srwatson    char *outbuf, size_t outbuflen, int flags)
1594105694Srwatson{
1595105694Srwatson	int error;
1596105694Srwatson
1597105694Srwatson	MAC_EXTERNALIZE(pipe_label, label, elements, outbuf, outbuflen);
1598105694Srwatson
1599105694Srwatson	return (error);
1600105694Srwatson}
1601105694Srwatson
1602105694Srwatsonstatic int
1603105694Srwatsonmac_externalize_socket_label(struct label *label, char *elements,
1604105694Srwatson    char *outbuf, size_t outbuflen, int flags)
1605105694Srwatson{
1606105694Srwatson	int error;
1607105694Srwatson
1608105694Srwatson	MAC_EXTERNALIZE(socket_label, label, elements, outbuf, outbuflen);
1609105694Srwatson
1610105694Srwatson	return (error);
1611105694Srwatson}
1612105694Srwatson
1613105694Srwatsonstatic int
1614105694Srwatsonmac_externalize_socket_peer_label(struct label *label, char *elements,
1615105694Srwatson    char *outbuf, size_t outbuflen, int flags)
1616105694Srwatson{
1617105694Srwatson	int error;
1618105694Srwatson
1619105694Srwatson	MAC_EXTERNALIZE(socket_peer_label, label, elements, outbuf, outbuflen);
1620105694Srwatson
1621105694Srwatson	return (error);
1622105694Srwatson}
1623105694Srwatson
1624105694Srwatsonstatic int
1625105694Srwatsonmac_externalize_vnode_label(struct label *label, char *elements,
1626105694Srwatson    char *outbuf, size_t outbuflen, int flags)
1627105694Srwatson{
1628105694Srwatson	int error;
1629105694Srwatson
1630105694Srwatson	MAC_EXTERNALIZE(vnode_label, label, elements, outbuf, outbuflen);
1631105694Srwatson
1632105694Srwatson	return (error);
1633105694Srwatson}
1634105694Srwatson
1635105694Srwatsonstatic int
1636105694Srwatsonmac_internalize_cred_label(struct label *label, char *string)
1637105694Srwatson{
1638105694Srwatson	int error;
1639105694Srwatson
1640105694Srwatson	MAC_INTERNALIZE(cred_label, label, string);
1641105694Srwatson
1642105694Srwatson	return (error);
1643105694Srwatson}
1644105694Srwatson
1645105694Srwatsonstatic int
1646105694Srwatsonmac_internalize_ifnet_label(struct label *label, char *string)
1647105694Srwatson{
1648105694Srwatson	int error;
1649105694Srwatson
1650105694Srwatson	MAC_INTERNALIZE(ifnet_label, label, string);
1651105694Srwatson
1652105694Srwatson	return (error);
1653105694Srwatson}
1654105694Srwatson
1655105694Srwatsonstatic int
1656105694Srwatsonmac_internalize_pipe_label(struct label *label, char *string)
1657105694Srwatson{
1658105694Srwatson	int error;
1659105694Srwatson
1660105694Srwatson	MAC_INTERNALIZE(pipe_label, label, string);
1661105694Srwatson
1662105694Srwatson	return (error);
1663105694Srwatson}
1664105694Srwatson
1665105694Srwatsonstatic int
1666105694Srwatsonmac_internalize_socket_label(struct label *label, char *string)
1667105694Srwatson{
1668105694Srwatson	int error;
1669105694Srwatson
1670105694Srwatson	MAC_INTERNALIZE(socket_label, label, string);
1671105694Srwatson
1672105694Srwatson	return (error);
1673105694Srwatson}
1674105694Srwatson
1675105694Srwatsonstatic int
1676105694Srwatsonmac_internalize_vnode_label(struct label *label, char *string)
1677105694Srwatson{
1678105694Srwatson	int error;
1679105694Srwatson
1680105694Srwatson	MAC_INTERNALIZE(vnode_label, label, string);
1681105694Srwatson
1682105694Srwatson	return (error);
1683105694Srwatson}
1684105694Srwatson
1685104522Srwatson/*
1686104522Srwatson * Initialize MAC label for the first kernel process, from which other
1687104522Srwatson * kernel processes and threads are spawned.
1688104522Srwatson */
1689104521Srwatsonvoid
1690104522Srwatsonmac_create_proc0(struct ucred *cred)
1691104522Srwatson{
1692104522Srwatson
1693104522Srwatson	MAC_PERFORM(create_proc0, cred);
1694104522Srwatson}
1695104522Srwatson
1696104522Srwatson/*
1697104522Srwatson * Initialize MAC label for the first userland process, from which other
1698104522Srwatson * userland processes and threads are spawned.
1699104522Srwatson */
1700104522Srwatsonvoid
1701104522Srwatsonmac_create_proc1(struct ucred *cred)
1702104522Srwatson{
1703104522Srwatson
1704104522Srwatson	MAC_PERFORM(create_proc1, cred);
1705104522Srwatson}
1706104522Srwatson
1707104522Srwatsonvoid
1708104522Srwatsonmac_thread_userret(struct thread *td)
1709104522Srwatson{
1710104522Srwatson
1711104522Srwatson	MAC_PERFORM(thread_userret, td);
1712104522Srwatson}
1713104522Srwatson
1714104522Srwatson/*
1715104522Srwatson * When a new process is created, its label must be initialized.  Generally,
1716104522Srwatson * this involves inheritence from the parent process, modulo possible
1717104522Srwatson * deltas.  This function allows that processing to take place.
1718104522Srwatson */
1719104522Srwatsonvoid
1720104522Srwatsonmac_create_cred(struct ucred *parent_cred, struct ucred *child_cred)
1721104522Srwatson{
1722104522Srwatson
1723104522Srwatson	MAC_PERFORM(create_cred, parent_cred, child_cred);
1724104522Srwatson}
1725104522Srwatson
1726104522Srwatsonvoid
1727100979Srwatsonmac_update_devfsdirent(struct devfs_dirent *de, struct vnode *vp)
1728100979Srwatson{
1729100979Srwatson
1730100979Srwatson	MAC_PERFORM(update_devfsdirent, de, &de->de_label, vp, &vp->v_label);
1731100979Srwatson}
1732100979Srwatson
1733100979Srwatsonvoid
1734105988Srwatsonmac_associate_vnode_devfs(struct mount *mp, struct devfs_dirent *de,
1735105988Srwatson    struct vnode *vp)
1736100979Srwatson{
1737100979Srwatson
1738105988Srwatson	MAC_PERFORM(associate_vnode_devfs, mp, &mp->mnt_fslabel, de,
1739105988Srwatson	    &de->de_label, vp, &vp->v_label);
1740100979Srwatson}
1741100979Srwatson
1742105988Srwatsonint
1743105988Srwatsonmac_associate_vnode_extattr(struct mount *mp, struct vnode *vp)
1744100979Srwatson{
1745100979Srwatson	int error;
1746100979Srwatson
1747105988Srwatson	ASSERT_VOP_LOCKED(vp, "mac_associate_vnode_extattr");
1748100979Srwatson
1749105988Srwatson	MAC_CHECK(associate_vnode_extattr, mp, &mp->mnt_fslabel, vp,
1750105988Srwatson	    &vp->v_label);
1751100979Srwatson
1752100979Srwatson	return (error);
1753100979Srwatson}
1754100979Srwatson
1755100979Srwatsonvoid
1756105988Srwatsonmac_associate_vnode_singlelabel(struct mount *mp, struct vnode *vp)
1757100979Srwatson{
1758100979Srwatson
1759105988Srwatson	MAC_PERFORM(associate_vnode_singlelabel, mp, &mp->mnt_fslabel, vp,
1760105988Srwatson	    &vp->v_label);
1761100979Srwatson}
1762100979Srwatson
1763100979Srwatsonint
1764105988Srwatsonmac_create_vnode_extattr(struct ucred *cred, struct mount *mp,
1765105988Srwatson    struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
1766100979Srwatson{
1767105988Srwatson	int error;
1768100979Srwatson
1769105988Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_create_vnode_extattr");
1770105988Srwatson	ASSERT_VOP_LOCKED(vp, "mac_create_vnode_extattr");
1771100979Srwatson
1772105988Srwatson	error = VOP_OPENEXTATTR(vp, cred, curthread);
1773105988Srwatson	if (error == EOPNOTSUPP) {
1774105988Srwatson		/* XXX: Optionally abort if transactions not supported. */
1775105988Srwatson		if (ea_warn_once == 0) {
1776105988Srwatson			printf("Warning: transactions not supported "
1777105988Srwatson			    "in EA write.\n");
1778105988Srwatson			ea_warn_once = 1;
1779105988Srwatson		}
1780105988Srwatson	} else if (error)
1781100979Srwatson		return (error);
1782100979Srwatson
1783105988Srwatson	MAC_CHECK(create_vnode_extattr, cred, mp, &mp->mnt_fslabel,
1784105988Srwatson	    dvp, &dvp->v_label, vp, &vp->v_label, cnp);
1785100979Srwatson
1786105988Srwatson	if (error) {
1787105988Srwatson		VOP_CLOSEEXTATTR(vp, 0, NOCRED, curthread);
1788100979Srwatson		return (error);
1789100979Srwatson	}
1790100979Srwatson
1791105988Srwatson	error = VOP_CLOSEEXTATTR(vp, 1, NOCRED, curthread);
1792100979Srwatson
1793105988Srwatson	if (error == EOPNOTSUPP)
1794105988Srwatson		error = 0;				/* XXX */
1795100979Srwatson
1796100979Srwatson	return (error);
1797100979Srwatson}
1798100979Srwatson
1799100979Srwatsonstatic int
1800105988Srwatsonmac_setlabel_vnode_extattr(struct ucred *cred, struct vnode *vp,
1801105988Srwatson    struct label *intlabel)
1802100979Srwatson{
1803100979Srwatson	int error;
1804100979Srwatson
1805105988Srwatson	ASSERT_VOP_LOCKED(vp, "mac_setlabel_vnode_extattr");
1806100979Srwatson
1807105988Srwatson	error = VOP_OPENEXTATTR(vp, cred, curthread);
1808105988Srwatson	if (error == EOPNOTSUPP) {
1809105988Srwatson		/* XXX: Optionally abort if transactions not supported. */
1810105988Srwatson		if (ea_warn_once == 0) {
1811105988Srwatson			printf("Warning: transactions not supported "
1812105988Srwatson			    "in EA write.\n");
1813105988Srwatson			ea_warn_once = 1;
1814105988Srwatson		}
1815105988Srwatson	} else if (error)
1816105988Srwatson		return (error);
1817100979Srwatson
1818105988Srwatson	MAC_CHECK(setlabel_vnode_extattr, cred, vp, &vp->v_label, intlabel);
1819100979Srwatson
1820105988Srwatson	if (error) {
1821105988Srwatson		VOP_CLOSEEXTATTR(vp, 0, NOCRED, curthread);
1822100979Srwatson		return (error);
1823100979Srwatson	}
1824100979Srwatson
1825105988Srwatson	error = VOP_CLOSEEXTATTR(vp, 1, NOCRED, curthread);
1826100979Srwatson
1827105988Srwatson	if (error == EOPNOTSUPP)
1828105988Srwatson		error = 0;				/* XXX */
1829100979Srwatson
1830105988Srwatson	return (error);
1831100979Srwatson}
1832100979Srwatson
1833100979Srwatsonvoid
1834100979Srwatsonmac_execve_transition(struct ucred *old, struct ucred *new, struct vnode *vp)
1835100979Srwatson{
1836100979Srwatson
1837100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_execve_transition");
1838100979Srwatson
1839100979Srwatson	MAC_PERFORM(execve_transition, old, new, vp, &vp->v_label);
1840100979Srwatson}
1841100979Srwatson
1842100979Srwatsonint
1843100979Srwatsonmac_execve_will_transition(struct ucred *old, struct vnode *vp)
1844100979Srwatson{
1845105988Srwatson	int result;
1846100979Srwatson
1847100979Srwatson	result = 0;
1848100979Srwatson	MAC_BOOLEAN(execve_will_transition, ||, old, vp, &vp->v_label);
1849100979Srwatson
1850100979Srwatson	return (result);
1851100979Srwatson}
1852100979Srwatson
1853100979Srwatsonint
1854100979Srwatsonmac_check_vnode_access(struct ucred *cred, struct vnode *vp, int flags)
1855100979Srwatson{
1856100979Srwatson	int error;
1857100979Srwatson
1858100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_access");
1859100979Srwatson
1860100979Srwatson	if (!mac_enforce_fs)
1861100979Srwatson		return (0);
1862100979Srwatson
1863100979Srwatson	MAC_CHECK(check_vnode_access, cred, vp, &vp->v_label, flags);
1864100979Srwatson	return (error);
1865100979Srwatson}
1866100979Srwatson
1867100979Srwatsonint
1868100979Srwatsonmac_check_vnode_chdir(struct ucred *cred, struct vnode *dvp)
1869100979Srwatson{
1870100979Srwatson	int error;
1871100979Srwatson
1872100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_chdir");
1873100979Srwatson
1874100979Srwatson	if (!mac_enforce_fs)
1875100979Srwatson		return (0);
1876100979Srwatson
1877100979Srwatson	MAC_CHECK(check_vnode_chdir, cred, dvp, &dvp->v_label);
1878100979Srwatson	return (error);
1879100979Srwatson}
1880100979Srwatson
1881100979Srwatsonint
1882100979Srwatsonmac_check_vnode_chroot(struct ucred *cred, struct vnode *dvp)
1883100979Srwatson{
1884100979Srwatson	int error;
1885100979Srwatson
1886100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_chroot");
1887100979Srwatson
1888100979Srwatson	if (!mac_enforce_fs)
1889100979Srwatson		return (0);
1890100979Srwatson
1891100979Srwatson	MAC_CHECK(check_vnode_chroot, cred, dvp, &dvp->v_label);
1892100979Srwatson	return (error);
1893100979Srwatson}
1894100979Srwatson
1895100979Srwatsonint
1896100979Srwatsonmac_check_vnode_create(struct ucred *cred, struct vnode *dvp,
1897100979Srwatson    struct componentname *cnp, struct vattr *vap)
1898100979Srwatson{
1899100979Srwatson	int error;
1900100979Srwatson
1901100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_create");
1902100979Srwatson
1903100979Srwatson	if (!mac_enforce_fs)
1904100979Srwatson		return (0);
1905100979Srwatson
1906100979Srwatson	MAC_CHECK(check_vnode_create, cred, dvp, &dvp->v_label, cnp, vap);
1907100979Srwatson	return (error);
1908100979Srwatson}
1909100979Srwatson
1910100979Srwatsonint
1911100979Srwatsonmac_check_vnode_delete(struct ucred *cred, struct vnode *dvp, struct vnode *vp,
1912100979Srwatson    struct componentname *cnp)
1913100979Srwatson{
1914100979Srwatson	int error;
1915100979Srwatson
1916100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_delete");
1917100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_delete");
1918100979Srwatson
1919100979Srwatson	if (!mac_enforce_fs)
1920100979Srwatson		return (0);
1921100979Srwatson
1922100979Srwatson	MAC_CHECK(check_vnode_delete, cred, dvp, &dvp->v_label, vp,
1923100979Srwatson	    &vp->v_label, cnp);
1924100979Srwatson	return (error);
1925100979Srwatson}
1926100979Srwatson
1927100979Srwatsonint
1928100979Srwatsonmac_check_vnode_deleteacl(struct ucred *cred, struct vnode *vp,
1929100979Srwatson    acl_type_t type)
1930100979Srwatson{
1931100979Srwatson	int error;
1932100979Srwatson
1933100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_deleteacl");
1934100979Srwatson
1935100979Srwatson	if (!mac_enforce_fs)
1936100979Srwatson		return (0);
1937100979Srwatson
1938100979Srwatson	MAC_CHECK(check_vnode_deleteacl, cred, vp, &vp->v_label, type);
1939100979Srwatson	return (error);
1940100979Srwatson}
1941100979Srwatson
1942100979Srwatsonint
1943100979Srwatsonmac_check_vnode_exec(struct ucred *cred, struct vnode *vp)
1944100979Srwatson{
1945100979Srwatson	int error;
1946100979Srwatson
1947102102Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_exec");
1948102102Srwatson
1949100979Srwatson	if (!mac_enforce_process && !mac_enforce_fs)
1950100979Srwatson		return (0);
1951100979Srwatson
1952100979Srwatson	MAC_CHECK(check_vnode_exec, cred, vp, &vp->v_label);
1953100979Srwatson
1954100979Srwatson	return (error);
1955100979Srwatson}
1956100979Srwatson
1957100979Srwatsonint
1958100979Srwatsonmac_check_vnode_getacl(struct ucred *cred, struct vnode *vp, acl_type_t type)
1959100979Srwatson{
1960100979Srwatson	int error;
1961100979Srwatson
1962100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_getacl");
1963100979Srwatson
1964100979Srwatson	if (!mac_enforce_fs)
1965100979Srwatson		return (0);
1966100979Srwatson
1967100979Srwatson	MAC_CHECK(check_vnode_getacl, cred, vp, &vp->v_label, type);
1968100979Srwatson	return (error);
1969100979Srwatson}
1970100979Srwatson
1971100979Srwatsonint
1972100979Srwatsonmac_check_vnode_getextattr(struct ucred *cred, struct vnode *vp,
1973100979Srwatson    int attrnamespace, const char *name, struct uio *uio)
1974100979Srwatson{
1975100979Srwatson	int error;
1976100979Srwatson
1977100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_getextattr");
1978100979Srwatson
1979100979Srwatson	if (!mac_enforce_fs)
1980100979Srwatson		return (0);
1981100979Srwatson
1982100979Srwatson	MAC_CHECK(check_vnode_getextattr, cred, vp, &vp->v_label,
1983100979Srwatson	    attrnamespace, name, uio);
1984100979Srwatson	return (error);
1985100979Srwatson}
1986100979Srwatson
1987100979Srwatsonint
1988104529Srwatsonmac_check_vnode_link(struct ucred *cred, struct vnode *dvp,
1989104529Srwatson    struct vnode *vp, struct componentname *cnp)
1990104529Srwatson{
1991104529Srwatson	int error;
1992104529Srwatson
1993104529Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_link");
1994104529Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_link");
1995104529Srwatson
1996104529Srwatson	if (!mac_enforce_fs)
1997104529Srwatson		return (0);
1998104529Srwatson
1999104529Srwatson	MAC_CHECK(check_vnode_link, cred, dvp, &dvp->v_label, vp,
2000104529Srwatson	    &vp->v_label, cnp);
2001104529Srwatson	return (error);
2002104529Srwatson}
2003104529Srwatson
2004104529Srwatsonint
2005100979Srwatsonmac_check_vnode_lookup(struct ucred *cred, struct vnode *dvp,
2006100979Srwatson    struct componentname *cnp)
2007100979Srwatson{
2008100979Srwatson	int error;
2009100979Srwatson
2010100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_lookup");
2011100979Srwatson
2012100979Srwatson	if (!mac_enforce_fs)
2013100979Srwatson		return (0);
2014100979Srwatson
2015100979Srwatson	MAC_CHECK(check_vnode_lookup, cred, dvp, &dvp->v_label, cnp);
2016100979Srwatson	return (error);
2017100979Srwatson}
2018100979Srwatson
2019104546Srwatsonint
2020104546Srwatsonmac_check_vnode_mmap(struct ucred *cred, struct vnode *vp, int prot)
2021100979Srwatson{
2022104546Srwatson	int error;
2023100979Srwatson
2024104546Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mmap");
2025103514Srwatson
2026104546Srwatson	if (!mac_enforce_fs || !mac_enforce_vm)
2027104546Srwatson		return (0);
2028104546Srwatson
2029104546Srwatson	MAC_CHECK(check_vnode_mmap, cred, vp, &vp->v_label, prot);
2030104546Srwatson	return (error);
2031100979Srwatson}
2032100979Srwatson
2033104546Srwatsonvoid
2034104546Srwatsonmac_check_vnode_mmap_downgrade(struct ucred *cred, struct vnode *vp, int *prot)
2035104546Srwatson{
2036104546Srwatson	int result = *prot;
2037104546Srwatson
2038104546Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mmap_downgrade");
2039104546Srwatson
2040104546Srwatson	if (!mac_enforce_fs || !mac_enforce_vm)
2041104546Srwatson		return;
2042104546Srwatson
2043104546Srwatson	MAC_PERFORM(check_vnode_mmap_downgrade, cred, vp, &vp->v_label,
2044104546Srwatson	    &result);
2045104546Srwatson
2046104546Srwatson	*prot = result;
2047104546Srwatson}
2048104546Srwatson
2049100979Srwatsonint
2050104546Srwatsonmac_check_vnode_mprotect(struct ucred *cred, struct vnode *vp, int prot)
2051104546Srwatson{
2052104546Srwatson	int error;
2053104546Srwatson
2054104546Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mprotect");
2055104546Srwatson
2056104546Srwatson	if (!mac_enforce_fs || !mac_enforce_vm)
2057104546Srwatson		return (0);
2058104546Srwatson
2059104546Srwatson	MAC_CHECK(check_vnode_mprotect, cred, vp, &vp->v_label, prot);
2060104546Srwatson	return (error);
2061104546Srwatson}
2062104546Srwatson
2063104546Srwatsonint
2064102112Srwatsonmac_check_vnode_open(struct ucred *cred, struct vnode *vp, mode_t acc_mode)
2065100979Srwatson{
2066100979Srwatson	int error;
2067100979Srwatson
2068102112Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_open");
2069102112Srwatson
2070100979Srwatson	if (!mac_enforce_fs)
2071100979Srwatson		return (0);
2072100979Srwatson
2073102112Srwatson	MAC_CHECK(check_vnode_open, cred, vp, &vp->v_label, acc_mode);
2074102112Srwatson	return (error);
2075102112Srwatson}
2076102112Srwatson
2077102112Srwatsonint
2078102129Srwatsonmac_check_vnode_poll(struct ucred *active_cred, struct ucred *file_cred,
2079102129Srwatson    struct vnode *vp)
2080102112Srwatson{
2081102112Srwatson	int error;
2082102112Srwatson
2083102112Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_poll");
2084102112Srwatson
2085102112Srwatson	if (!mac_enforce_fs)
2086102112Srwatson		return (0);
2087102112Srwatson
2088102129Srwatson	MAC_CHECK(check_vnode_poll, active_cred, file_cred, vp,
2089102129Srwatson	    &vp->v_label);
2090100979Srwatson
2091100979Srwatson	return (error);
2092100979Srwatson}
2093100979Srwatson
2094100979Srwatsonint
2095102129Srwatsonmac_check_vnode_read(struct ucred *active_cred, struct ucred *file_cred,
2096102129Srwatson    struct vnode *vp)
2097100979Srwatson{
2098100979Srwatson	int error;
2099100979Srwatson
2100102112Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_read");
2101100979Srwatson
2102100979Srwatson	if (!mac_enforce_fs)
2103100979Srwatson		return (0);
2104100979Srwatson
2105102129Srwatson	MAC_CHECK(check_vnode_read, active_cred, file_cred, vp,
2106102129Srwatson	    &vp->v_label);
2107102112Srwatson
2108100979Srwatson	return (error);
2109100979Srwatson}
2110100979Srwatson
2111100979Srwatsonint
2112100979Srwatsonmac_check_vnode_readdir(struct ucred *cred, struct vnode *dvp)
2113100979Srwatson{
2114100979Srwatson	int error;
2115100979Srwatson
2116100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_readdir");
2117100979Srwatson
2118100979Srwatson	if (!mac_enforce_fs)
2119100979Srwatson		return (0);
2120100979Srwatson
2121100979Srwatson	MAC_CHECK(check_vnode_readdir, cred, dvp, &dvp->v_label);
2122100979Srwatson	return (error);
2123100979Srwatson}
2124100979Srwatson
2125100979Srwatsonint
2126100979Srwatsonmac_check_vnode_readlink(struct ucred *cred, struct vnode *vp)
2127100979Srwatson{
2128100979Srwatson	int error;
2129100979Srwatson
2130100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_readlink");
2131100979Srwatson
2132100979Srwatson	if (!mac_enforce_fs)
2133100979Srwatson		return (0);
2134100979Srwatson
2135100979Srwatson	MAC_CHECK(check_vnode_readlink, cred, vp, &vp->v_label);
2136100979Srwatson	return (error);
2137100979Srwatson}
2138100979Srwatson
2139100979Srwatsonstatic int
2140100979Srwatsonmac_check_vnode_relabel(struct ucred *cred, struct vnode *vp,
2141100979Srwatson    struct label *newlabel)
2142100979Srwatson{
2143100979Srwatson	int error;
2144100979Srwatson
2145100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_relabel");
2146100979Srwatson
2147100979Srwatson	MAC_CHECK(check_vnode_relabel, cred, vp, &vp->v_label, newlabel);
2148100979Srwatson
2149100979Srwatson	return (error);
2150100979Srwatson}
2151100979Srwatson
2152100979Srwatsonint
2153100979Srwatsonmac_check_vnode_rename_from(struct ucred *cred, struct vnode *dvp,
2154100979Srwatson    struct vnode *vp, struct componentname *cnp)
2155100979Srwatson{
2156100979Srwatson	int error;
2157100979Srwatson
2158100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_rename_from");
2159100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_rename_from");
2160100979Srwatson
2161100979Srwatson	if (!mac_enforce_fs)
2162100979Srwatson		return (0);
2163100979Srwatson
2164100979Srwatson	MAC_CHECK(check_vnode_rename_from, cred, dvp, &dvp->v_label, vp,
2165100979Srwatson	    &vp->v_label, cnp);
2166100979Srwatson	return (error);
2167100979Srwatson}
2168100979Srwatson
2169100979Srwatsonint
2170100979Srwatsonmac_check_vnode_rename_to(struct ucred *cred, struct vnode *dvp,
2171100979Srwatson    struct vnode *vp, int samedir, struct componentname *cnp)
2172100979Srwatson{
2173100979Srwatson	int error;
2174100979Srwatson
2175100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_rename_to");
2176100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_rename_to");
2177100979Srwatson
2178100979Srwatson	if (!mac_enforce_fs)
2179100979Srwatson		return (0);
2180100979Srwatson
2181100979Srwatson	MAC_CHECK(check_vnode_rename_to, cred, dvp, &dvp->v_label, vp,
2182100979Srwatson	    vp != NULL ? &vp->v_label : NULL, samedir, cnp);
2183100979Srwatson	return (error);
2184100979Srwatson}
2185100979Srwatson
2186100979Srwatsonint
2187100979Srwatsonmac_check_vnode_revoke(struct ucred *cred, struct vnode *vp)
2188100979Srwatson{
2189100979Srwatson	int error;
2190100979Srwatson
2191100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_revoke");
2192100979Srwatson
2193100979Srwatson	if (!mac_enforce_fs)
2194100979Srwatson		return (0);
2195100979Srwatson
2196100979Srwatson	MAC_CHECK(check_vnode_revoke, cred, vp, &vp->v_label);
2197100979Srwatson	return (error);
2198100979Srwatson}
2199100979Srwatson
2200100979Srwatsonint
2201100979Srwatsonmac_check_vnode_setacl(struct ucred *cred, struct vnode *vp, acl_type_t type,
2202100979Srwatson    struct acl *acl)
2203100979Srwatson{
2204100979Srwatson	int error;
2205100979Srwatson
2206100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setacl");
2207100979Srwatson
2208100979Srwatson	if (!mac_enforce_fs)
2209100979Srwatson		return (0);
2210100979Srwatson
2211100979Srwatson	MAC_CHECK(check_vnode_setacl, cred, vp, &vp->v_label, type, acl);
2212100979Srwatson	return (error);
2213100979Srwatson}
2214100979Srwatson
2215100979Srwatsonint
2216100979Srwatsonmac_check_vnode_setextattr(struct ucred *cred, struct vnode *vp,
2217100979Srwatson    int attrnamespace, const char *name, struct uio *uio)
2218100979Srwatson{
2219100979Srwatson	int error;
2220100979Srwatson
2221100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setextattr");
2222100979Srwatson
2223100979Srwatson	if (!mac_enforce_fs)
2224100979Srwatson		return (0);
2225100979Srwatson
2226100979Srwatson	MAC_CHECK(check_vnode_setextattr, cred, vp, &vp->v_label,
2227100979Srwatson	    attrnamespace, name, uio);
2228100979Srwatson	return (error);
2229100979Srwatson}
2230100979Srwatson
2231100979Srwatsonint
2232100979Srwatsonmac_check_vnode_setflags(struct ucred *cred, struct vnode *vp, u_long flags)
2233100979Srwatson{
2234100979Srwatson	int error;
2235100979Srwatson
2236100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setflags");
2237100979Srwatson
2238100979Srwatson	if (!mac_enforce_fs)
2239100979Srwatson		return (0);
2240100979Srwatson
2241100979Srwatson	MAC_CHECK(check_vnode_setflags, cred, vp, &vp->v_label, flags);
2242100979Srwatson	return (error);
2243100979Srwatson}
2244100979Srwatson
2245100979Srwatsonint
2246100979Srwatsonmac_check_vnode_setmode(struct ucred *cred, struct vnode *vp, mode_t mode)
2247100979Srwatson{
2248100979Srwatson	int error;
2249100979Srwatson
2250100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setmode");
2251100979Srwatson
2252100979Srwatson	if (!mac_enforce_fs)
2253100979Srwatson		return (0);
2254100979Srwatson
2255100979Srwatson	MAC_CHECK(check_vnode_setmode, cred, vp, &vp->v_label, mode);
2256100979Srwatson	return (error);
2257100979Srwatson}
2258100979Srwatson
2259100979Srwatsonint
2260100979Srwatsonmac_check_vnode_setowner(struct ucred *cred, struct vnode *vp, uid_t uid,
2261100979Srwatson    gid_t gid)
2262100979Srwatson{
2263100979Srwatson	int error;
2264100979Srwatson
2265100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setowner");
2266100979Srwatson
2267100979Srwatson	if (!mac_enforce_fs)
2268100979Srwatson		return (0);
2269100979Srwatson
2270100979Srwatson	MAC_CHECK(check_vnode_setowner, cred, vp, &vp->v_label, uid, gid);
2271100979Srwatson	return (error);
2272100979Srwatson}
2273100979Srwatson
2274100979Srwatsonint
2275100979Srwatsonmac_check_vnode_setutimes(struct ucred *cred, struct vnode *vp,
2276100979Srwatson    struct timespec atime, struct timespec mtime)
2277100979Srwatson{
2278100979Srwatson	int error;
2279100979Srwatson
2280100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setutimes");
2281100979Srwatson
2282100979Srwatson	if (!mac_enforce_fs)
2283100979Srwatson		return (0);
2284100979Srwatson
2285100979Srwatson	MAC_CHECK(check_vnode_setutimes, cred, vp, &vp->v_label, atime,
2286100979Srwatson	    mtime);
2287100979Srwatson	return (error);
2288100979Srwatson}
2289100979Srwatson
2290100979Srwatsonint
2291102129Srwatsonmac_check_vnode_stat(struct ucred *active_cred, struct ucred *file_cred,
2292102129Srwatson    struct vnode *vp)
2293100979Srwatson{
2294100979Srwatson	int error;
2295100979Srwatson
2296100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_stat");
2297100979Srwatson
2298100979Srwatson	if (!mac_enforce_fs)
2299100979Srwatson		return (0);
2300100979Srwatson
2301102129Srwatson	MAC_CHECK(check_vnode_stat, active_cred, file_cred, vp,
2302102129Srwatson	    &vp->v_label);
2303100979Srwatson	return (error);
2304100979Srwatson}
2305100979Srwatson
2306102112Srwatsonint
2307102129Srwatsonmac_check_vnode_write(struct ucred *active_cred, struct ucred *file_cred,
2308102129Srwatson    struct vnode *vp)
2309102112Srwatson{
2310102112Srwatson	int error;
2311102112Srwatson
2312102112Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_write");
2313102112Srwatson
2314102112Srwatson	if (!mac_enforce_fs)
2315102112Srwatson		return (0);
2316102112Srwatson
2317102129Srwatson	MAC_CHECK(check_vnode_write, active_cred, file_cred, vp,
2318102129Srwatson	    &vp->v_label);
2319102112Srwatson
2320102112Srwatson	return (error);
2321102112Srwatson}
2322102112Srwatson
2323100979Srwatson/*
2324100979Srwatson * When relabeling a process, call out to the policies for the maximum
2325100979Srwatson * permission allowed for each object type we know about in its
2326100979Srwatson * memory space, and revoke access (in the least surprising ways we
2327100979Srwatson * know) when necessary.  The process lock is not held here.
2328100979Srwatson */
2329100979Srwatsonstatic void
2330100979Srwatsonmac_cred_mmapped_drop_perms(struct thread *td, struct ucred *cred)
2331100979Srwatson{
2332100979Srwatson
2333100979Srwatson	/* XXX freeze all other threads */
2334100979Srwatson	mac_cred_mmapped_drop_perms_recurse(td, cred,
2335100979Srwatson	    &td->td_proc->p_vmspace->vm_map);
2336100979Srwatson	/* XXX allow other threads to continue */
2337100979Srwatson}
2338100979Srwatson
2339100979Srwatsonstatic __inline const char *
2340100979Srwatsonprot2str(vm_prot_t prot)
2341100979Srwatson{
2342100979Srwatson
2343100979Srwatson	switch (prot & VM_PROT_ALL) {
2344100979Srwatson	case VM_PROT_READ:
2345100979Srwatson		return ("r--");
2346100979Srwatson	case VM_PROT_READ | VM_PROT_WRITE:
2347100979Srwatson		return ("rw-");
2348100979Srwatson	case VM_PROT_READ | VM_PROT_EXECUTE:
2349100979Srwatson		return ("r-x");
2350100979Srwatson	case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
2351100979Srwatson		return ("rwx");
2352100979Srwatson	case VM_PROT_WRITE:
2353100979Srwatson		return ("-w-");
2354100979Srwatson	case VM_PROT_EXECUTE:
2355100979Srwatson		return ("--x");
2356100979Srwatson	case VM_PROT_WRITE | VM_PROT_EXECUTE:
2357100979Srwatson		return ("-wx");
2358100979Srwatson	default:
2359100979Srwatson		return ("---");
2360100979Srwatson	}
2361100979Srwatson}
2362100979Srwatson
2363100979Srwatsonstatic void
2364100979Srwatsonmac_cred_mmapped_drop_perms_recurse(struct thread *td, struct ucred *cred,
2365100979Srwatson    struct vm_map *map)
2366100979Srwatson{
2367100979Srwatson	struct vm_map_entry *vme;
2368104546Srwatson	int result;
2369104546Srwatson	vm_prot_t revokeperms;
2370100979Srwatson	vm_object_t object;
2371100979Srwatson	vm_ooffset_t offset;
2372100979Srwatson	struct vnode *vp;
2373100979Srwatson
2374103136Srwatson	if (!mac_mmap_revocation)
2375103136Srwatson		return;
2376103136Srwatson
2377100979Srwatson	vm_map_lock_read(map);
2378100979Srwatson	for (vme = map->header.next; vme != &map->header; vme = vme->next) {
2379100979Srwatson		if (vme->eflags & MAP_ENTRY_IS_SUB_MAP) {
2380100979Srwatson			mac_cred_mmapped_drop_perms_recurse(td, cred,
2381100979Srwatson			    vme->object.sub_map);
2382100979Srwatson			continue;
2383100979Srwatson		}
2384100979Srwatson		/*
2385100979Srwatson		 * Skip over entries that obviously are not shared.
2386100979Srwatson		 */
2387100979Srwatson		if (vme->eflags & (MAP_ENTRY_COW | MAP_ENTRY_NOSYNC) ||
2388100979Srwatson		    !vme->max_protection)
2389100979Srwatson			continue;
2390100979Srwatson		/*
2391100979Srwatson		 * Drill down to the deepest backing object.
2392100979Srwatson		 */
2393100979Srwatson		offset = vme->offset;
2394100979Srwatson		object = vme->object.vm_object;
2395100979Srwatson		if (object == NULL)
2396100979Srwatson			continue;
2397100979Srwatson		while (object->backing_object != NULL) {
2398100979Srwatson			object = object->backing_object;
2399100979Srwatson			offset += object->backing_object_offset;
2400100979Srwatson		}
2401100979Srwatson		/*
2402100979Srwatson		 * At the moment, vm_maps and objects aren't considered
2403100979Srwatson		 * by the MAC system, so only things with backing by a
2404100979Srwatson		 * normal object (read: vnodes) are checked.
2405100979Srwatson		 */
2406100979Srwatson		if (object->type != OBJT_VNODE)
2407100979Srwatson			continue;
2408100979Srwatson		vp = (struct vnode *)object->handle;
2409100979Srwatson		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2410104546Srwatson		result = vme->max_protection;
2411104546Srwatson		mac_check_vnode_mmap_downgrade(cred, vp, &result);
2412100979Srwatson		VOP_UNLOCK(vp, 0, td);
2413100979Srwatson		/*
2414100979Srwatson		 * Find out what maximum protection we may be allowing
2415100979Srwatson		 * now but a policy needs to get removed.
2416100979Srwatson		 */
2417100979Srwatson		revokeperms = vme->max_protection & ~result;
2418100979Srwatson		if (!revokeperms)
2419100979Srwatson			continue;
2420102949Sbde		printf("pid %ld: revoking %s perms from %#lx:%ld "
2421102949Sbde		    "(max %s/cur %s)\n", (long)td->td_proc->p_pid,
2422102949Sbde		    prot2str(revokeperms), (u_long)vme->start,
2423102949Sbde		    (long)(vme->end - vme->start),
2424100979Srwatson		    prot2str(vme->max_protection), prot2str(vme->protection));
2425100979Srwatson		vm_map_lock_upgrade(map);
2426100979Srwatson		/*
2427100979Srwatson		 * This is the really simple case: if a map has more
2428100979Srwatson		 * max_protection than is allowed, but it's not being
2429100979Srwatson		 * actually used (that is, the current protection is
2430100979Srwatson		 * still allowed), we can just wipe it out and do
2431100979Srwatson		 * nothing more.
2432100979Srwatson		 */
2433100979Srwatson		if ((vme->protection & revokeperms) == 0) {
2434100979Srwatson			vme->max_protection -= revokeperms;
2435100979Srwatson		} else {
2436100979Srwatson			if (revokeperms & VM_PROT_WRITE) {
2437100979Srwatson				/*
2438100979Srwatson				 * In the more complicated case, flush out all
2439100979Srwatson				 * pending changes to the object then turn it
2440100979Srwatson				 * copy-on-write.
2441100979Srwatson				 */
2442100979Srwatson				vm_object_reference(object);
2443100979Srwatson				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2444100979Srwatson				vm_object_page_clean(object,
2445100979Srwatson				    OFF_TO_IDX(offset),
2446100979Srwatson				    OFF_TO_IDX(offset + vme->end - vme->start +
2447100979Srwatson					PAGE_MASK),
2448100979Srwatson				    OBJPC_SYNC);
2449100979Srwatson				VOP_UNLOCK(vp, 0, td);
2450100979Srwatson				vm_object_deallocate(object);
2451100979Srwatson				/*
2452100979Srwatson				 * Why bother if there's no read permissions
2453100979Srwatson				 * anymore?  For the rest, we need to leave
2454100979Srwatson				 * the write permissions on for COW, or
2455100979Srwatson				 * remove them entirely if configured to.
2456100979Srwatson				 */
2457100979Srwatson				if (!mac_mmap_revocation_via_cow) {
2458100979Srwatson					vme->max_protection &= ~VM_PROT_WRITE;
2459100979Srwatson					vme->protection &= ~VM_PROT_WRITE;
2460100979Srwatson				} if ((revokeperms & VM_PROT_READ) == 0)
2461100979Srwatson					vme->eflags |= MAP_ENTRY_COW |
2462100979Srwatson					    MAP_ENTRY_NEEDS_COPY;
2463100979Srwatson			}
2464100979Srwatson			if (revokeperms & VM_PROT_EXECUTE) {
2465100979Srwatson				vme->max_protection &= ~VM_PROT_EXECUTE;
2466100979Srwatson				vme->protection &= ~VM_PROT_EXECUTE;
2467100979Srwatson			}
2468100979Srwatson			if (revokeperms & VM_PROT_READ) {
2469100979Srwatson				vme->max_protection = 0;
2470100979Srwatson				vme->protection = 0;
2471100979Srwatson			}
2472100979Srwatson			pmap_protect(map->pmap, vme->start, vme->end,
2473100979Srwatson			    vme->protection & ~revokeperms);
2474100979Srwatson			vm_map_simplify_entry(map, vme);
2475100979Srwatson		}
2476100979Srwatson		vm_map_lock_downgrade(map);
2477100979Srwatson	}
2478100979Srwatson	vm_map_unlock_read(map);
2479100979Srwatson}
2480100979Srwatson
2481100979Srwatson/*
2482100979Srwatson * When the subject's label changes, it may require revocation of privilege
2483100979Srwatson * to mapped objects.  This can't be done on-the-fly later with a unified
2484100979Srwatson * buffer cache.
2485100979Srwatson */
2486100979Srwatsonstatic void
2487100979Srwatsonmac_relabel_cred(struct ucred *cred, struct label *newlabel)
2488100979Srwatson{
2489100979Srwatson
2490100979Srwatson	MAC_PERFORM(relabel_cred, cred, newlabel);
2491100979Srwatson}
2492100979Srwatson
2493100979Srwatsonvoid
2494100979Srwatsonmac_relabel_vnode(struct ucred *cred, struct vnode *vp, struct label *newlabel)
2495100979Srwatson{
2496100979Srwatson
2497100979Srwatson	MAC_PERFORM(relabel_vnode, cred, vp, &vp->v_label, newlabel);
2498100979Srwatson}
2499100979Srwatson
2500100979Srwatsonvoid
2501100979Srwatsonmac_create_ifnet(struct ifnet *ifnet)
2502100979Srwatson{
2503100979Srwatson
2504100979Srwatson	MAC_PERFORM(create_ifnet, ifnet, &ifnet->if_label);
2505100979Srwatson}
2506100979Srwatson
2507100979Srwatsonvoid
2508100979Srwatsonmac_create_bpfdesc(struct ucred *cred, struct bpf_d *bpf_d)
2509100979Srwatson{
2510100979Srwatson
2511100979Srwatson	MAC_PERFORM(create_bpfdesc, cred, bpf_d, &bpf_d->bd_label);
2512100979Srwatson}
2513100979Srwatson
2514100979Srwatsonvoid
2515100979Srwatsonmac_create_socket(struct ucred *cred, struct socket *socket)
2516100979Srwatson{
2517100979Srwatson
2518100979Srwatson	MAC_PERFORM(create_socket, cred, socket, &socket->so_label);
2519100979Srwatson}
2520100979Srwatson
2521100979Srwatsonvoid
2522100979Srwatsonmac_create_pipe(struct ucred *cred, struct pipe *pipe)
2523100979Srwatson{
2524100979Srwatson
2525100979Srwatson	MAC_PERFORM(create_pipe, cred, pipe, pipe->pipe_label);
2526100979Srwatson}
2527100979Srwatson
2528100979Srwatsonvoid
2529100979Srwatsonmac_create_socket_from_socket(struct socket *oldsocket,
2530100979Srwatson    struct socket *newsocket)
2531100979Srwatson{
2532100979Srwatson
2533100979Srwatson	MAC_PERFORM(create_socket_from_socket, oldsocket, &oldsocket->so_label,
2534100979Srwatson	    newsocket, &newsocket->so_label);
2535100979Srwatson}
2536100979Srwatson
2537100979Srwatsonstatic void
2538100979Srwatsonmac_relabel_socket(struct ucred *cred, struct socket *socket,
2539100979Srwatson    struct label *newlabel)
2540100979Srwatson{
2541100979Srwatson
2542100979Srwatson	MAC_PERFORM(relabel_socket, cred, socket, &socket->so_label, newlabel);
2543100979Srwatson}
2544100979Srwatson
2545100979Srwatsonstatic void
2546100979Srwatsonmac_relabel_pipe(struct ucred *cred, struct pipe *pipe, struct label *newlabel)
2547100979Srwatson{
2548100979Srwatson
2549100979Srwatson	MAC_PERFORM(relabel_pipe, cred, pipe, pipe->pipe_label, newlabel);
2550100979Srwatson}
2551100979Srwatson
2552100979Srwatsonvoid
2553100979Srwatsonmac_set_socket_peer_from_mbuf(struct mbuf *mbuf, struct socket *socket)
2554100979Srwatson{
2555100979Srwatson
2556100979Srwatson	MAC_PERFORM(set_socket_peer_from_mbuf, mbuf, &mbuf->m_pkthdr.label,
2557100979Srwatson	    socket, &socket->so_peerlabel);
2558100979Srwatson}
2559100979Srwatson
2560100979Srwatsonvoid
2561100979Srwatsonmac_set_socket_peer_from_socket(struct socket *oldsocket,
2562100979Srwatson    struct socket *newsocket)
2563100979Srwatson{
2564100979Srwatson
2565100979Srwatson	MAC_PERFORM(set_socket_peer_from_socket, oldsocket,
2566100979Srwatson	    &oldsocket->so_label, newsocket, &newsocket->so_peerlabel);
2567100979Srwatson}
2568100979Srwatson
2569100979Srwatsonvoid
2570100979Srwatsonmac_create_datagram_from_ipq(struct ipq *ipq, struct mbuf *datagram)
2571100979Srwatson{
2572100979Srwatson
2573100979Srwatson	MAC_PERFORM(create_datagram_from_ipq, ipq, &ipq->ipq_label,
2574100979Srwatson	    datagram, &datagram->m_pkthdr.label);
2575100979Srwatson}
2576100979Srwatson
2577100979Srwatsonvoid
2578100979Srwatsonmac_create_fragment(struct mbuf *datagram, struct mbuf *fragment)
2579100979Srwatson{
2580100979Srwatson
2581100979Srwatson	MAC_PERFORM(create_fragment, datagram, &datagram->m_pkthdr.label,
2582100979Srwatson	    fragment, &fragment->m_pkthdr.label);
2583100979Srwatson}
2584100979Srwatson
2585100979Srwatsonvoid
2586100979Srwatsonmac_create_ipq(struct mbuf *fragment, struct ipq *ipq)
2587100979Srwatson{
2588100979Srwatson
2589100979Srwatson	MAC_PERFORM(create_ipq, fragment, &fragment->m_pkthdr.label, ipq,
2590100979Srwatson	    &ipq->ipq_label);
2591100979Srwatson}
2592100979Srwatson
2593100979Srwatsonvoid
2594100979Srwatsonmac_create_mbuf_from_mbuf(struct mbuf *oldmbuf, struct mbuf *newmbuf)
2595100979Srwatson{
2596100979Srwatson
2597100979Srwatson	MAC_PERFORM(create_mbuf_from_mbuf, oldmbuf, &oldmbuf->m_pkthdr.label,
2598100979Srwatson	    newmbuf, &newmbuf->m_pkthdr.label);
2599100979Srwatson}
2600100979Srwatson
2601100979Srwatsonvoid
2602100979Srwatsonmac_create_mbuf_from_bpfdesc(struct bpf_d *bpf_d, struct mbuf *mbuf)
2603100979Srwatson{
2604100979Srwatson
2605100979Srwatson	MAC_PERFORM(create_mbuf_from_bpfdesc, bpf_d, &bpf_d->bd_label, mbuf,
2606100979Srwatson	    &mbuf->m_pkthdr.label);
2607100979Srwatson}
2608100979Srwatson
2609100979Srwatsonvoid
2610100979Srwatsonmac_create_mbuf_linklayer(struct ifnet *ifnet, struct mbuf *mbuf)
2611100979Srwatson{
2612100979Srwatson
2613100979Srwatson	MAC_PERFORM(create_mbuf_linklayer, ifnet, &ifnet->if_label, mbuf,
2614100979Srwatson	    &mbuf->m_pkthdr.label);
2615100979Srwatson}
2616100979Srwatson
2617100979Srwatsonvoid
2618100979Srwatsonmac_create_mbuf_from_ifnet(struct ifnet *ifnet, struct mbuf *mbuf)
2619100979Srwatson{
2620100979Srwatson
2621100979Srwatson	MAC_PERFORM(create_mbuf_from_ifnet, ifnet, &ifnet->if_label, mbuf,
2622100979Srwatson	    &mbuf->m_pkthdr.label);
2623100979Srwatson}
2624100979Srwatson
2625100979Srwatsonvoid
2626100979Srwatsonmac_create_mbuf_multicast_encap(struct mbuf *oldmbuf, struct ifnet *ifnet,
2627100979Srwatson    struct mbuf *newmbuf)
2628100979Srwatson{
2629100979Srwatson
2630100979Srwatson	MAC_PERFORM(create_mbuf_multicast_encap, oldmbuf,
2631100979Srwatson	    &oldmbuf->m_pkthdr.label, ifnet, &ifnet->if_label, newmbuf,
2632100979Srwatson	    &newmbuf->m_pkthdr.label);
2633100979Srwatson}
2634100979Srwatson
2635100979Srwatsonvoid
2636100979Srwatsonmac_create_mbuf_netlayer(struct mbuf *oldmbuf, struct mbuf *newmbuf)
2637100979Srwatson{
2638100979Srwatson
2639100979Srwatson	MAC_PERFORM(create_mbuf_netlayer, oldmbuf, &oldmbuf->m_pkthdr.label,
2640100979Srwatson	    newmbuf, &newmbuf->m_pkthdr.label);
2641100979Srwatson}
2642100979Srwatson
2643100979Srwatsonint
2644100979Srwatsonmac_fragment_match(struct mbuf *fragment, struct ipq *ipq)
2645100979Srwatson{
2646100979Srwatson	int result;
2647100979Srwatson
2648100979Srwatson	result = 1;
2649100979Srwatson	MAC_BOOLEAN(fragment_match, &&, fragment, &fragment->m_pkthdr.label,
2650100979Srwatson	    ipq, &ipq->ipq_label);
2651100979Srwatson
2652100979Srwatson	return (result);
2653100979Srwatson}
2654100979Srwatson
2655100979Srwatsonvoid
2656100979Srwatsonmac_update_ipq(struct mbuf *fragment, struct ipq *ipq)
2657100979Srwatson{
2658100979Srwatson
2659100979Srwatson	MAC_PERFORM(update_ipq, fragment, &fragment->m_pkthdr.label, ipq,
2660100979Srwatson	    &ipq->ipq_label);
2661100979Srwatson}
2662100979Srwatson
2663100979Srwatsonvoid
2664100979Srwatsonmac_create_mbuf_from_socket(struct socket *socket, struct mbuf *mbuf)
2665100979Srwatson{
2666100979Srwatson
2667100979Srwatson	MAC_PERFORM(create_mbuf_from_socket, socket, &socket->so_label, mbuf,
2668100979Srwatson	    &mbuf->m_pkthdr.label);
2669100979Srwatson}
2670100979Srwatson
2671100979Srwatsonvoid
2672100979Srwatsonmac_create_mount(struct ucred *cred, struct mount *mp)
2673100979Srwatson{
2674100979Srwatson
2675100979Srwatson	MAC_PERFORM(create_mount, cred, mp, &mp->mnt_mntlabel,
2676100979Srwatson	    &mp->mnt_fslabel);
2677100979Srwatson}
2678100979Srwatson
2679100979Srwatsonvoid
2680100979Srwatsonmac_create_root_mount(struct ucred *cred, struct mount *mp)
2681100979Srwatson{
2682100979Srwatson
2683100979Srwatson	MAC_PERFORM(create_root_mount, cred, mp, &mp->mnt_mntlabel,
2684100979Srwatson	    &mp->mnt_fslabel);
2685100979Srwatson}
2686100979Srwatson
2687100979Srwatsonint
2688100979Srwatsonmac_check_bpfdesc_receive(struct bpf_d *bpf_d, struct ifnet *ifnet)
2689100979Srwatson{
2690100979Srwatson	int error;
2691100979Srwatson
2692100979Srwatson	if (!mac_enforce_network)
2693100979Srwatson		return (0);
2694100979Srwatson
2695100979Srwatson	MAC_CHECK(check_bpfdesc_receive, bpf_d, &bpf_d->bd_label, ifnet,
2696100979Srwatson	    &ifnet->if_label);
2697100979Srwatson
2698100979Srwatson	return (error);
2699100979Srwatson}
2700100979Srwatson
2701100979Srwatsonstatic int
2702100979Srwatsonmac_check_cred_relabel(struct ucred *cred, struct label *newlabel)
2703100979Srwatson{
2704100979Srwatson	int error;
2705100979Srwatson
2706100979Srwatson	MAC_CHECK(check_cred_relabel, cred, newlabel);
2707100979Srwatson
2708100979Srwatson	return (error);
2709100979Srwatson}
2710100979Srwatson
2711100979Srwatsonint
2712100979Srwatsonmac_check_cred_visible(struct ucred *u1, struct ucred *u2)
2713100979Srwatson{
2714100979Srwatson	int error;
2715100979Srwatson
2716100979Srwatson	if (!mac_enforce_process)
2717100979Srwatson		return (0);
2718100979Srwatson
2719100979Srwatson	MAC_CHECK(check_cred_visible, u1, u2);
2720100979Srwatson
2721100979Srwatson	return (error);
2722100979Srwatson}
2723100979Srwatson
2724100979Srwatsonint
2725100979Srwatsonmac_check_ifnet_transmit(struct ifnet *ifnet, struct mbuf *mbuf)
2726100979Srwatson{
2727100979Srwatson	int error;
2728100979Srwatson
2729100979Srwatson	if (!mac_enforce_network)
2730100979Srwatson		return (0);
2731100979Srwatson
2732100979Srwatson	KASSERT(mbuf->m_flags & M_PKTHDR, ("packet has no pkthdr"));
2733100979Srwatson	if (!(mbuf->m_pkthdr.label.l_flags & MAC_FLAG_INITIALIZED))
2734105598Sbrooks		if_printf(ifnet, "not initialized\n");
2735100979Srwatson
2736100979Srwatson	MAC_CHECK(check_ifnet_transmit, ifnet, &ifnet->if_label, mbuf,
2737100979Srwatson	    &mbuf->m_pkthdr.label);
2738100979Srwatson
2739100979Srwatson	return (error);
2740100979Srwatson}
2741100979Srwatson
2742100979Srwatsonint
2743100979Srwatsonmac_check_mount_stat(struct ucred *cred, struct mount *mount)
2744100979Srwatson{
2745100979Srwatson	int error;
2746100979Srwatson
2747100979Srwatson	if (!mac_enforce_fs)
2748100979Srwatson		return (0);
2749100979Srwatson
2750100979Srwatson	MAC_CHECK(check_mount_stat, cred, mount, &mount->mnt_mntlabel);
2751100979Srwatson
2752100979Srwatson	return (error);
2753100979Srwatson}
2754100979Srwatson
2755100979Srwatsonint
2756100979Srwatsonmac_check_pipe_ioctl(struct ucred *cred, struct pipe *pipe, unsigned long cmd,
2757100979Srwatson    void *data)
2758100979Srwatson{
2759100979Srwatson	int error;
2760100979Srwatson
2761104269Srwatson	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2762104269Srwatson
2763104269Srwatson	if (!mac_enforce_pipe)
2764104269Srwatson		return (0);
2765104269Srwatson
2766100979Srwatson	MAC_CHECK(check_pipe_ioctl, cred, pipe, pipe->pipe_label, cmd, data);
2767100979Srwatson
2768100979Srwatson	return (error);
2769100979Srwatson}
2770100979Srwatson
2771100979Srwatsonint
2772102115Srwatsonmac_check_pipe_poll(struct ucred *cred, struct pipe *pipe)
2773100979Srwatson{
2774100979Srwatson	int error;
2775100979Srwatson
2776104269Srwatson	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2777104269Srwatson
2778104269Srwatson	if (!mac_enforce_pipe)
2779104269Srwatson		return (0);
2780104269Srwatson
2781102115Srwatson	MAC_CHECK(check_pipe_poll, cred, pipe, pipe->pipe_label);
2782100979Srwatson
2783100979Srwatson	return (error);
2784100979Srwatson}
2785100979Srwatson
2786102115Srwatsonint
2787102115Srwatsonmac_check_pipe_read(struct ucred *cred, struct pipe *pipe)
2788102115Srwatson{
2789102115Srwatson	int error;
2790102115Srwatson
2791104269Srwatson	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2792104269Srwatson
2793104269Srwatson	if (!mac_enforce_pipe)
2794104269Srwatson		return (0);
2795104269Srwatson
2796102115Srwatson	MAC_CHECK(check_pipe_read, cred, pipe, pipe->pipe_label);
2797102115Srwatson
2798102115Srwatson	return (error);
2799102115Srwatson}
2800102115Srwatson
2801100979Srwatsonstatic int
2802100979Srwatsonmac_check_pipe_relabel(struct ucred *cred, struct pipe *pipe,
2803100979Srwatson    struct label *newlabel)
2804100979Srwatson{
2805100979Srwatson	int error;
2806100979Srwatson
2807104269Srwatson	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2808104269Srwatson
2809104269Srwatson	if (!mac_enforce_pipe)
2810104269Srwatson		return (0);
2811104269Srwatson
2812100979Srwatson	MAC_CHECK(check_pipe_relabel, cred, pipe, pipe->pipe_label, newlabel);
2813100979Srwatson
2814100979Srwatson	return (error);
2815100979Srwatson}
2816100979Srwatson
2817100979Srwatsonint
2818102115Srwatsonmac_check_pipe_stat(struct ucred *cred, struct pipe *pipe)
2819102115Srwatson{
2820102115Srwatson	int error;
2821102115Srwatson
2822104269Srwatson	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2823104269Srwatson
2824104269Srwatson	if (!mac_enforce_pipe)
2825104269Srwatson		return (0);
2826104269Srwatson
2827102115Srwatson	MAC_CHECK(check_pipe_stat, cred, pipe, pipe->pipe_label);
2828102115Srwatson
2829102115Srwatson	return (error);
2830102115Srwatson}
2831102115Srwatson
2832102115Srwatsonint
2833102115Srwatsonmac_check_pipe_write(struct ucred *cred, struct pipe *pipe)
2834102115Srwatson{
2835102115Srwatson	int error;
2836102115Srwatson
2837104269Srwatson	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2838104269Srwatson
2839104269Srwatson	if (!mac_enforce_pipe)
2840104269Srwatson		return (0);
2841104269Srwatson
2842102115Srwatson	MAC_CHECK(check_pipe_write, cred, pipe, pipe->pipe_label);
2843102115Srwatson
2844102115Srwatson	return (error);
2845102115Srwatson}
2846102115Srwatson
2847102115Srwatsonint
2848100979Srwatsonmac_check_proc_debug(struct ucred *cred, struct proc *proc)
2849100979Srwatson{
2850100979Srwatson	int error;
2851100979Srwatson
2852102103Srwatson	PROC_LOCK_ASSERT(proc, MA_OWNED);
2853102103Srwatson
2854100979Srwatson	if (!mac_enforce_process)
2855100979Srwatson		return (0);
2856100979Srwatson
2857100979Srwatson	MAC_CHECK(check_proc_debug, cred, proc);
2858100979Srwatson
2859100979Srwatson	return (error);
2860100979Srwatson}
2861100979Srwatson
2862100979Srwatsonint
2863100979Srwatsonmac_check_proc_sched(struct ucred *cred, struct proc *proc)
2864100979Srwatson{
2865100979Srwatson	int error;
2866100979Srwatson
2867102103Srwatson	PROC_LOCK_ASSERT(proc, MA_OWNED);
2868102103Srwatson
2869100979Srwatson	if (!mac_enforce_process)
2870100979Srwatson		return (0);
2871100979Srwatson
2872100979Srwatson	MAC_CHECK(check_proc_sched, cred, proc);
2873100979Srwatson
2874100979Srwatson	return (error);
2875100979Srwatson}
2876100979Srwatson
2877100979Srwatsonint
2878100979Srwatsonmac_check_proc_signal(struct ucred *cred, struct proc *proc, int signum)
2879100979Srwatson{
2880100979Srwatson	int error;
2881100979Srwatson
2882102103Srwatson	PROC_LOCK_ASSERT(proc, MA_OWNED);
2883102103Srwatson
2884100979Srwatson	if (!mac_enforce_process)
2885100979Srwatson		return (0);
2886100979Srwatson
2887100979Srwatson	MAC_CHECK(check_proc_signal, cred, proc, signum);
2888100979Srwatson
2889100979Srwatson	return (error);
2890100979Srwatson}
2891100979Srwatson
2892100979Srwatsonint
2893100979Srwatsonmac_check_socket_bind(struct ucred *ucred, struct socket *socket,
2894100979Srwatson    struct sockaddr *sockaddr)
2895100979Srwatson{
2896100979Srwatson	int error;
2897100979Srwatson
2898100979Srwatson	if (!mac_enforce_socket)
2899100979Srwatson		return (0);
2900100979Srwatson
2901100979Srwatson	MAC_CHECK(check_socket_bind, ucred, socket, &socket->so_label,
2902100979Srwatson	    sockaddr);
2903100979Srwatson
2904100979Srwatson	return (error);
2905100979Srwatson}
2906100979Srwatson
2907100979Srwatsonint
2908100979Srwatsonmac_check_socket_connect(struct ucred *cred, struct socket *socket,
2909100979Srwatson    struct sockaddr *sockaddr)
2910100979Srwatson{
2911100979Srwatson	int error;
2912100979Srwatson
2913100979Srwatson	if (!mac_enforce_socket)
2914100979Srwatson		return (0);
2915100979Srwatson
2916100979Srwatson	MAC_CHECK(check_socket_connect, cred, socket, &socket->so_label,
2917100979Srwatson	    sockaddr);
2918100979Srwatson
2919100979Srwatson	return (error);
2920100979Srwatson}
2921100979Srwatson
2922100979Srwatsonint
2923101933Srwatsonmac_check_socket_deliver(struct socket *socket, struct mbuf *mbuf)
2924100979Srwatson{
2925100979Srwatson	int error;
2926100979Srwatson
2927100979Srwatson	if (!mac_enforce_socket)
2928100979Srwatson		return (0);
2929100979Srwatson
2930101933Srwatson	MAC_CHECK(check_socket_deliver, socket, &socket->so_label, mbuf,
2931101933Srwatson	    &mbuf->m_pkthdr.label);
2932101933Srwatson
2933100979Srwatson	return (error);
2934100979Srwatson}
2935100979Srwatson
2936100979Srwatsonint
2937101933Srwatsonmac_check_socket_listen(struct ucred *cred, struct socket *socket)
2938100979Srwatson{
2939100979Srwatson	int error;
2940100979Srwatson
2941100979Srwatson	if (!mac_enforce_socket)
2942100979Srwatson		return (0);
2943100979Srwatson
2944101933Srwatson	MAC_CHECK(check_socket_listen, cred, socket, &socket->so_label);
2945100979Srwatson	return (error);
2946100979Srwatson}
2947100979Srwatson
2948104571Srwatsonint
2949104571Srwatsonmac_check_socket_receive(struct ucred *cred, struct socket *so)
2950104571Srwatson{
2951104571Srwatson	int error;
2952104571Srwatson
2953104571Srwatson	if (!mac_enforce_socket)
2954104571Srwatson		return (0);
2955104571Srwatson
2956104571Srwatson	MAC_CHECK(check_socket_receive, cred, so, &so->so_label);
2957104571Srwatson
2958104571Srwatson	return (error);
2959104571Srwatson}
2960104571Srwatson
2961100979Srwatsonstatic int
2962100979Srwatsonmac_check_socket_relabel(struct ucred *cred, struct socket *socket,
2963100979Srwatson    struct label *newlabel)
2964100979Srwatson{
2965100979Srwatson	int error;
2966100979Srwatson
2967100979Srwatson	MAC_CHECK(check_socket_relabel, cred, socket, &socket->so_label,
2968100979Srwatson	    newlabel);
2969100979Srwatson
2970100979Srwatson	return (error);
2971100979Srwatson}
2972100979Srwatson
2973100979Srwatsonint
2974104571Srwatsonmac_check_socket_send(struct ucred *cred, struct socket *so)
2975104571Srwatson{
2976104571Srwatson	int error;
2977104571Srwatson
2978104571Srwatson	if (!mac_enforce_socket)
2979104571Srwatson		return (0);
2980104571Srwatson
2981104571Srwatson	MAC_CHECK(check_socket_send, cred, so, &so->so_label);
2982104571Srwatson
2983104571Srwatson	return (error);
2984104571Srwatson}
2985104571Srwatson
2986104571Srwatsonint
2987100979Srwatsonmac_check_socket_visible(struct ucred *cred, struct socket *socket)
2988100979Srwatson{
2989100979Srwatson	int error;
2990100979Srwatson
2991100979Srwatson	if (!mac_enforce_socket)
2992100979Srwatson		return (0);
2993105694Srwatson
2994100979Srwatson	MAC_CHECK(check_socket_visible, cred, socket, &socket->so_label);
2995105694Srwatson
2996100979Srwatson	return (error);
2997100979Srwatson}
2998100979Srwatson
2999100979Srwatsonint
3000106023Srwatsonmac_check_system_swapon(struct ucred *cred, struct vnode *vp)
3001106023Srwatson{
3002106023Srwatson	int error;
3003106023Srwatson
3004106023Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_system_swapon");
3005106023Srwatson
3006106023Srwatson	if (!mac_enforce_fs)
3007106023Srwatson		return (0);
3008106023Srwatson
3009106023Srwatson	MAC_CHECK(check_system_swapon, cred, vp, &vp->v_label);
3010106023Srwatson	return (error);
3011106023Srwatson}
3012106023Srwatson
3013106023Srwatsonint
3014100979Srwatsonmac_ioctl_ifnet_get(struct ucred *cred, struct ifreq *ifr,
3015100979Srwatson    struct ifnet *ifnet)
3016100979Srwatson{
3017105694Srwatson	char *elements, *buffer;
3018105694Srwatson	struct mac mac;
3019100979Srwatson	int error;
3020100979Srwatson
3021105694Srwatson	error = copyin(ifr->ifr_ifru.ifru_data, &mac, sizeof(mac));
3022100979Srwatson	if (error)
3023100979Srwatson		return (error);
3024100979Srwatson
3025105694Srwatson	error = mac_check_structmac_consistent(&mac);
3026105694Srwatson	if (error)
3027105694Srwatson		return (error);
3028105694Srwatson
3029105694Srwatson	elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
3030105694Srwatson	error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
3031105694Srwatson	if (error) {
3032105694Srwatson		free(elements, M_MACTEMP);
3033105694Srwatson		return (error);
3034105694Srwatson	}
3035105694Srwatson
3036105694Srwatson	buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
3037105694Srwatson	error = mac_externalize_ifnet_label(&ifnet->if_label, elements,
3038105694Srwatson	    buffer, mac.m_buflen, M_WAITOK);
3039105694Srwatson	if (error == 0)
3040105694Srwatson		error = copyout(buffer, mac.m_string, strlen(buffer)+1);
3041105694Srwatson
3042105694Srwatson	free(buffer, M_MACTEMP);
3043105694Srwatson	free(elements, M_MACTEMP);
3044105694Srwatson
3045105694Srwatson	return (error);
3046100979Srwatson}
3047100979Srwatson
3048100979Srwatsonint
3049100979Srwatsonmac_ioctl_ifnet_set(struct ucred *cred, struct ifreq *ifr,
3050100979Srwatson    struct ifnet *ifnet)
3051100979Srwatson{
3052100979Srwatson	struct label intlabel;
3053105694Srwatson	struct mac mac;
3054105694Srwatson	char *buffer;
3055100979Srwatson	int error;
3056100979Srwatson
3057105694Srwatson	error = copyin(ifr->ifr_ifru.ifru_data, &mac, sizeof(mac));
3058100979Srwatson	if (error)
3059100979Srwatson		return (error);
3060100979Srwatson
3061105694Srwatson	error = mac_check_structmac_consistent(&mac);
3062100979Srwatson	if (error)
3063100979Srwatson		return (error);
3064100979Srwatson
3065105694Srwatson	buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
3066105694Srwatson	error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
3067105694Srwatson	if (error) {
3068105694Srwatson		free(buffer, M_MACTEMP);
3069105694Srwatson		return (error);
3070105694Srwatson	}
3071105694Srwatson
3072105694Srwatson	mac_init_ifnet_label(&intlabel);
3073105694Srwatson	error = mac_internalize_ifnet_label(&intlabel, buffer);
3074105694Srwatson	free(buffer, M_MACTEMP);
3075105694Srwatson	if (error) {
3076105694Srwatson		mac_destroy_ifnet_label(&intlabel);
3077105694Srwatson		return (error);
3078105694Srwatson	}
3079105694Srwatson
3080100979Srwatson	/*
3081100979Srwatson	 * XXX: Note that this is a redundant privilege check, since
3082100979Srwatson	 * policies impose this check themselves if required by the
3083100979Srwatson	 * policy.  Eventually, this should go away.
3084100979Srwatson	 */
3085100979Srwatson	error = suser_cred(cred, 0);
3086105694Srwatson	if (error) {
3087105694Srwatson		mac_destroy_ifnet_label(&intlabel);
3088105694Srwatson		return (error);
3089105694Srwatson	}
3090100979Srwatson
3091100979Srwatson	MAC_CHECK(check_ifnet_relabel, cred, ifnet, &ifnet->if_label,
3092100979Srwatson	    &intlabel);
3093105694Srwatson	if (error) {
3094105694Srwatson		mac_destroy_ifnet_label(&intlabel);
3095105694Srwatson		return (error);
3096105694Srwatson	}
3097100979Srwatson
3098100979Srwatson	MAC_PERFORM(relabel_ifnet, cred, ifnet, &ifnet->if_label, &intlabel);
3099100979Srwatson
3100105694Srwatson	mac_destroy_ifnet_label(&intlabel);
3101105694Srwatson	return (0);
3102100979Srwatson}
3103100979Srwatson
3104100979Srwatsonvoid
3105100979Srwatsonmac_create_devfs_vnode(struct devfs_dirent *de, struct vnode *vp)
3106100979Srwatson{
3107100979Srwatson
3108100979Srwatson	MAC_PERFORM(create_devfs_vnode, de, &de->de_label, vp, &vp->v_label);
3109100979Srwatson}
3110100979Srwatson
3111100979Srwatsonvoid
3112100979Srwatsonmac_create_devfs_device(dev_t dev, struct devfs_dirent *de)
3113100979Srwatson{
3114100979Srwatson
3115100979Srwatson	MAC_PERFORM(create_devfs_device, dev, de, &de->de_label);
3116100979Srwatson}
3117100979Srwatson
3118104533Srwatsonvoid
3119104533Srwatsonmac_create_devfs_symlink(struct ucred *cred, struct devfs_dirent *dd,
3120104533Srwatson    struct devfs_dirent *de)
3121104533Srwatson{
3122104533Srwatson
3123104533Srwatson	MAC_PERFORM(create_devfs_symlink, cred, dd, &dd->de_label, de,
3124104533Srwatson	    &de->de_label);
3125104533Srwatson}
3126104533Srwatson
3127100979Srwatsonvoid
3128100979Srwatsonmac_create_devfs_directory(char *dirname, int dirnamelen,
3129100979Srwatson    struct devfs_dirent *de)
3130100979Srwatson{
3131100979Srwatson
3132100979Srwatson	MAC_PERFORM(create_devfs_directory, dirname, dirnamelen, de,
3133100979Srwatson	    &de->de_label);
3134100979Srwatson}
3135100979Srwatson
3136100979Srwatsonint
3137100979Srwatsonmac_setsockopt_label_set(struct ucred *cred, struct socket *so,
3138105694Srwatson    struct mac *mac)
3139100979Srwatson{
3140100979Srwatson	struct label intlabel;
3141105694Srwatson	char *buffer;
3142100979Srwatson	int error;
3143100979Srwatson
3144105694Srwatson	error = mac_check_structmac_consistent(mac);
3145100979Srwatson	if (error)
3146100979Srwatson		return (error);
3147100979Srwatson
3148105694Srwatson	buffer = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK);
3149105694Srwatson	error = copyinstr(mac->m_string, buffer, mac->m_buflen, NULL);
3150105694Srwatson	if (error) {
3151105694Srwatson		free(buffer, M_MACTEMP);
3152105694Srwatson		return (error);
3153105694Srwatson	}
3154105694Srwatson
3155105694Srwatson	mac_init_socket_label(&intlabel, M_WAITOK);
3156105694Srwatson	error = mac_internalize_socket_label(&intlabel, buffer);
3157105694Srwatson	free(buffer, M_MACTEMP);
3158105694Srwatson	if (error) {
3159105694Srwatson		mac_destroy_socket_label(&intlabel);
3160105694Srwatson		return (error);
3161105694Srwatson	}
3162105694Srwatson
3163100979Srwatson	mac_check_socket_relabel(cred, so, &intlabel);
3164100979Srwatson	if (error) {
3165105694Srwatson		mac_destroy_socket_label(&intlabel);
3166100979Srwatson		return (error);
3167100979Srwatson	}
3168100979Srwatson
3169100979Srwatson	mac_relabel_socket(cred, so, &intlabel);
3170100979Srwatson
3171105694Srwatson	mac_destroy_socket_label(&intlabel);
3172100979Srwatson	return (0);
3173100979Srwatson}
3174100979Srwatson
3175100979Srwatsonint
3176100979Srwatsonmac_pipe_label_set(struct ucred *cred, struct pipe *pipe, struct label *label)
3177100979Srwatson{
3178100979Srwatson	int error;
3179100979Srwatson
3180104269Srwatson	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
3181104269Srwatson
3182100979Srwatson	error = mac_check_pipe_relabel(cred, pipe, label);
3183100979Srwatson	if (error)
3184100979Srwatson		return (error);
3185100979Srwatson
3186100979Srwatson	mac_relabel_pipe(cred, pipe, label);
3187100979Srwatson
3188100979Srwatson	return (0);
3189100979Srwatson}
3190100979Srwatson
3191100979Srwatsonint
3192100979Srwatsonmac_getsockopt_label_get(struct ucred *cred, struct socket *so,
3193105694Srwatson    struct mac *mac)
3194100979Srwatson{
3195105694Srwatson	char *buffer, *elements;
3196105694Srwatson	int error;
3197100979Srwatson
3198105694Srwatson	error = mac_check_structmac_consistent(mac);
3199105694Srwatson	if (error)
3200105694Srwatson		return (error);
3201105694Srwatson
3202105694Srwatson	elements = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK);
3203105694Srwatson	error = copyinstr(mac->m_string, elements, mac->m_buflen, NULL);
3204105694Srwatson	if (error) {
3205105694Srwatson		free(elements, M_MACTEMP);
3206105694Srwatson		return (error);
3207105694Srwatson	}
3208105694Srwatson
3209105694Srwatson	buffer = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
3210105694Srwatson	error = mac_externalize_socket_label(&so->so_label, elements,
3211105694Srwatson	    buffer, mac->m_buflen, M_WAITOK);
3212105694Srwatson	if (error == 0)
3213105694Srwatson		error = copyout(buffer, mac->m_string, strlen(buffer)+1);
3214105694Srwatson
3215105694Srwatson	free(buffer, M_MACTEMP);
3216105694Srwatson	free(elements, M_MACTEMP);
3217105694Srwatson
3218105694Srwatson	return (error);
3219100979Srwatson}
3220100979Srwatson
3221100979Srwatsonint
3222100979Srwatsonmac_getsockopt_peerlabel_get(struct ucred *cred, struct socket *so,
3223105694Srwatson    struct mac *mac)
3224100979Srwatson{
3225105694Srwatson	char *elements, *buffer;
3226105694Srwatson	int error;
3227100979Srwatson
3228105694Srwatson	error = mac_check_structmac_consistent(mac);
3229105694Srwatson	if (error)
3230105694Srwatson		return (error);
3231105694Srwatson
3232105694Srwatson	elements = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK);
3233105694Srwatson	error = copyinstr(mac->m_string, elements, mac->m_buflen, NULL);
3234105694Srwatson	if (error) {
3235105694Srwatson		free(elements, M_MACTEMP);
3236105694Srwatson		return (error);
3237105694Srwatson	}
3238105694Srwatson
3239105694Srwatson	buffer = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
3240105694Srwatson	error = mac_externalize_socket_peer_label(&so->so_peerlabel,
3241105694Srwatson	    elements, buffer, mac->m_buflen, M_WAITOK);
3242105694Srwatson	if (error == 0)
3243105694Srwatson		error = copyout(buffer, mac->m_string, strlen(buffer)+1);
3244105694Srwatson
3245105694Srwatson	free(buffer, M_MACTEMP);
3246105694Srwatson	free(elements, M_MACTEMP);
3247105694Srwatson
3248105694Srwatson	return (error);
3249100979Srwatson}
3250100979Srwatson
3251100979Srwatson/*
3252100979Srwatson * Implementation of VOP_SETLABEL() that relies on extended attributes
3253100979Srwatson * to store label data.  Can be referenced by filesystems supporting
3254100979Srwatson * extended attributes.
3255100979Srwatson */
3256100979Srwatsonint
3257100979Srwatsonvop_stdsetlabel_ea(struct vop_setlabel_args *ap)
3258100979Srwatson{
3259100979Srwatson	struct vnode *vp = ap->a_vp;
3260100979Srwatson	struct label *intlabel = ap->a_label;
3261100979Srwatson	int error;
3262100979Srwatson
3263100979Srwatson	ASSERT_VOP_LOCKED(vp, "vop_stdsetlabel_ea");
3264100979Srwatson
3265105988Srwatson	if ((vp->v_mount->mnt_flag & MNT_MULTILABEL) == 0)
3266105988Srwatson		return (EOPNOTSUPP);
3267100979Srwatson
3268105988Srwatson	error = mac_setlabel_vnode_extattr(ap->a_cred, vp, intlabel);
3269100979Srwatson	if (error)
3270100979Srwatson		return (error);
3271100979Srwatson
3272100979Srwatson	mac_relabel_vnode(ap->a_cred, vp, intlabel);
3273100979Srwatson
3274100979Srwatson	return (0);
3275100979Srwatson}
3276100979Srwatson
3277100979Srwatsonstatic int
3278100979Srwatsonvn_setlabel(struct vnode *vp, struct label *intlabel, struct ucred *cred)
3279100979Srwatson{
3280100979Srwatson	int error;
3281100979Srwatson
3282100979Srwatson	if (vp->v_mount == NULL) {
3283100979Srwatson		/* printf("vn_setlabel: null v_mount\n"); */
3284103314Snjl		if (vp->v_type != VNON)
3285103314Snjl			printf("vn_setlabel: null v_mount with non-VNON\n");
3286100979Srwatson		return (EBADF);
3287100979Srwatson	}
3288100979Srwatson
3289100979Srwatson	if ((vp->v_mount->mnt_flag & MNT_MULTILABEL) == 0)
3290100979Srwatson		return (EOPNOTSUPP);
3291100979Srwatson
3292100979Srwatson	/*
3293100979Srwatson	 * Multi-phase commit.  First check the policies to confirm the
3294100979Srwatson	 * change is OK.  Then commit via the filesystem.  Finally,
3295100979Srwatson	 * update the actual vnode label.  Question: maybe the filesystem
3296100979Srwatson	 * should update the vnode at the end as part of VOP_SETLABEL()?
3297100979Srwatson	 */
3298100979Srwatson	error = mac_check_vnode_relabel(cred, vp, intlabel);
3299100979Srwatson	if (error)
3300100979Srwatson		return (error);
3301100979Srwatson
3302100979Srwatson	/*
3303100979Srwatson	 * VADMIN provides the opportunity for the filesystem to make
3304100979Srwatson	 * decisions about who is and is not able to modify labels
3305100979Srwatson	 * and protections on files.  This might not be right.  We can't
3306100979Srwatson	 * assume VOP_SETLABEL() will do it, because we might implement
3307100979Srwatson	 * that as part of vop_stdsetlabel_ea().
3308100979Srwatson	 */
3309100979Srwatson	error = VOP_ACCESS(vp, VADMIN, cred, curthread);
3310100979Srwatson	if (error)
3311100979Srwatson		return (error);
3312100979Srwatson
3313100979Srwatson	error = VOP_SETLABEL(vp, intlabel, cred, curthread);
3314100979Srwatson	if (error)
3315100979Srwatson		return (error);
3316100979Srwatson
3317100979Srwatson	return (0);
3318100979Srwatson}
3319100979Srwatson
3320105694Srwatsonint
3321105694Srwatson__mac_get_pid(struct thread *td, struct __mac_get_pid_args *uap)
3322105694Srwatson{
3323105694Srwatson	char *elements, *buffer;
3324105694Srwatson	struct mac mac;
3325105694Srwatson	struct proc *tproc;
3326105694Srwatson	struct ucred *tcred;
3327105694Srwatson	int error;
3328105694Srwatson
3329105694Srwatson	error = copyin(SCARG(uap, mac_p), &mac, sizeof(mac));
3330105694Srwatson	if (error)
3331105694Srwatson		return (error);
3332105694Srwatson
3333105694Srwatson	error = mac_check_structmac_consistent(&mac);
3334105694Srwatson	if (error)
3335105694Srwatson		return (error);
3336105694Srwatson
3337105694Srwatson	tproc = pfind(uap->pid);
3338105694Srwatson	if (tproc == NULL)
3339105694Srwatson		return (ESRCH);
3340105694Srwatson
3341105694Srwatson	tcred = NULL;				/* Satisfy gcc. */
3342105694Srwatson	error = p_cansee(td, tproc);
3343105694Srwatson	if (error == 0)
3344105694Srwatson		tcred = crhold(tproc->p_ucred);
3345105694Srwatson	PROC_UNLOCK(tproc);
3346105694Srwatson	if (error)
3347105694Srwatson		return (error);
3348105694Srwatson
3349105694Srwatson	elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
3350105694Srwatson	error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
3351105694Srwatson	if (error) {
3352105694Srwatson		free(elements, M_MACTEMP);
3353105694Srwatson		crfree(tcred);
3354105694Srwatson		return (error);
3355105694Srwatson	}
3356105694Srwatson
3357105694Srwatson	buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
3358105694Srwatson	error = mac_externalize_cred_label(&tcred->cr_label, elements,
3359105694Srwatson	    buffer, mac.m_buflen, M_WAITOK);
3360105694Srwatson	if (error == 0)
3361105694Srwatson		error = copyout(buffer, mac.m_string, strlen(buffer)+1);
3362105694Srwatson
3363105694Srwatson	free(buffer, M_MACTEMP);
3364105694Srwatson	free(elements, M_MACTEMP);
3365105694Srwatson	crfree(tcred);
3366105694Srwatson	return (error);
3367105694Srwatson}
3368105694Srwatson
3369100979Srwatson/*
3370100979Srwatson * MPSAFE
3371100979Srwatson */
3372100979Srwatsonint
3373100894Srwatson__mac_get_proc(struct thread *td, struct __mac_get_proc_args *uap)
3374100894Srwatson{
3375105694Srwatson	char *elements, *buffer;
3376105694Srwatson	struct mac mac;
3377100979Srwatson	int error;
3378100894Srwatson
3379105694Srwatson	error = copyin(uap->mac_p, &mac, sizeof(mac));
3380105694Srwatson	if (error)
3381105694Srwatson		return (error);
3382105694Srwatson
3383105694Srwatson	error = mac_check_structmac_consistent(&mac);
3384105694Srwatson	if (error)
3385105694Srwatson		return (error);
3386105694Srwatson
3387105694Srwatson	elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
3388105694Srwatson	error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
3389105694Srwatson	if (error) {
3390105694Srwatson		free(elements, M_MACTEMP);
3391105694Srwatson		return (error);
3392105694Srwatson	}
3393105694Srwatson
3394105694Srwatson	buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
3395105694Srwatson	error = mac_externalize_cred_label(&td->td_ucred->cr_label,
3396105694Srwatson	    elements, buffer, mac.m_buflen, M_WAITOK);
3397100979Srwatson	if (error == 0)
3398105694Srwatson		error = copyout(buffer, mac.m_string, strlen(buffer)+1);
3399100979Srwatson
3400105694Srwatson	free(buffer, M_MACTEMP);
3401105694Srwatson	free(elements, M_MACTEMP);
3402100979Srwatson	return (error);
3403100979Srwatson}
3404100979Srwatson
3405100979Srwatson/*
3406100979Srwatson * MPSAFE
3407100979Srwatson */
3408100979Srwatsonint
3409100979Srwatson__mac_set_proc(struct thread *td, struct __mac_set_proc_args *uap)
3410100979Srwatson{
3411100979Srwatson	struct ucred *newcred, *oldcred;
3412105694Srwatson	struct label intlabel;
3413100979Srwatson	struct proc *p;
3414105694Srwatson	struct mac mac;
3415105694Srwatson	char *buffer;
3416100979Srwatson	int error;
3417100979Srwatson
3418105694Srwatson	error = copyin(uap->mac_p, &mac, sizeof(mac));
3419100979Srwatson	if (error)
3420100979Srwatson		return (error);
3421100979Srwatson
3422105694Srwatson	error = mac_check_structmac_consistent(&mac);
3423100979Srwatson	if (error)
3424100979Srwatson		return (error);
3425100979Srwatson
3426105694Srwatson	buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
3427105694Srwatson	error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
3428105694Srwatson	if (error) {
3429105694Srwatson		free(buffer, M_MACTEMP);
3430105694Srwatson		return (error);
3431105694Srwatson	}
3432105694Srwatson
3433105694Srwatson	mac_init_cred_label(&intlabel);
3434105694Srwatson	error = mac_internalize_cred_label(&intlabel, buffer);
3435105694Srwatson	free(buffer, M_MACTEMP);
3436105694Srwatson	if (error) {
3437105694Srwatson		mac_destroy_cred_label(&intlabel);
3438105694Srwatson		return (error);
3439105694Srwatson	}
3440105694Srwatson
3441100979Srwatson	newcred = crget();
3442100979Srwatson
3443100979Srwatson	p = td->td_proc;
3444100979Srwatson	PROC_LOCK(p);
3445100979Srwatson	oldcred = p->p_ucred;
3446100979Srwatson
3447100979Srwatson	error = mac_check_cred_relabel(oldcred, &intlabel);
3448100979Srwatson	if (error) {
3449100979Srwatson		PROC_UNLOCK(p);
3450100979Srwatson		crfree(newcred);
3451105694Srwatson		goto out;
3452100979Srwatson	}
3453100979Srwatson
3454100979Srwatson	setsugid(p);
3455100979Srwatson	crcopy(newcred, oldcred);
3456100979Srwatson	mac_relabel_cred(newcred, &intlabel);
3457102136Srwatson	p->p_ucred = newcred;
3458100979Srwatson
3459102136Srwatson	/*
3460102136Srwatson	 * Grab additional reference for use while revoking mmaps, prior
3461102136Srwatson	 * to releasing the proc lock and sharing the cred.
3462102136Srwatson	 */
3463102136Srwatson	crhold(newcred);
3464100979Srwatson	PROC_UNLOCK(p);
3465102136Srwatson
3466105694Srwatson	if (mac_enforce_vm) {
3467105694Srwatson		mtx_lock(&Giant);
3468105694Srwatson		mac_cred_mmapped_drop_perms(td, newcred);
3469105694Srwatson		mtx_unlock(&Giant);
3470105694Srwatson	}
3471102136Srwatson
3472102136Srwatson	crfree(newcred);	/* Free revocation reference. */
3473100979Srwatson	crfree(oldcred);
3474105694Srwatson
3475105694Srwatsonout:
3476105694Srwatson	mac_destroy_cred_label(&intlabel);
3477105694Srwatson	return (error);
3478100979Srwatson}
3479100979Srwatson
3480100979Srwatson/*
3481100979Srwatson * MPSAFE
3482100979Srwatson */
3483100979Srwatsonint
3484100979Srwatson__mac_get_fd(struct thread *td, struct __mac_get_fd_args *uap)
3485100979Srwatson{
3486105694Srwatson	char *elements, *buffer;
3487105694Srwatson	struct label intlabel;
3488100979Srwatson	struct file *fp;
3489105694Srwatson	struct mac mac;
3490100979Srwatson	struct vnode *vp;
3491100979Srwatson	struct pipe *pipe;
3492105694Srwatson	short label_type;
3493100979Srwatson	int error;
3494100979Srwatson
3495105694Srwatson	error = copyin(uap->mac_p, &mac, sizeof(mac));
3496105694Srwatson	if (error)
3497105694Srwatson		return (error);
3498100979Srwatson
3499105694Srwatson	error = mac_check_structmac_consistent(&mac);
3500105694Srwatson	if (error)
3501105694Srwatson		return (error);
3502105694Srwatson
3503105694Srwatson	elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
3504105694Srwatson	error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
3505105694Srwatson	if (error) {
3506105694Srwatson		free(elements, M_MACTEMP);
3507105694Srwatson		return (error);
3508105694Srwatson	}
3509105694Srwatson
3510105694Srwatson	buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
3511105694Srwatson	mtx_lock(&Giant);				/* VFS */
3512100979Srwatson	error = fget(td, SCARG(uap, fd), &fp);
3513100979Srwatson	if (error)
3514100979Srwatson		goto out;
3515100979Srwatson
3516105694Srwatson	label_type = fp->f_type;
3517100979Srwatson	switch (fp->f_type) {
3518100979Srwatson	case DTYPE_FIFO:
3519100979Srwatson	case DTYPE_VNODE:
3520100979Srwatson		vp = (struct vnode *)fp->f_data;
3521100979Srwatson
3522105694Srwatson		mac_init_vnode_label(&intlabel);
3523105694Srwatson
3524100979Srwatson		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3525105988Srwatson		mac_copy_vnode_label(&vp->v_label, &intlabel);
3526100979Srwatson		VOP_UNLOCK(vp, 0, td);
3527105694Srwatson
3528100979Srwatson		break;
3529100979Srwatson	case DTYPE_PIPE:
3530100979Srwatson		pipe = (struct pipe *)fp->f_data;
3531105694Srwatson
3532105694Srwatson		mac_init_pipe_label(&intlabel);
3533105694Srwatson
3534105694Srwatson		PIPE_LOCK(pipe);
3535105694Srwatson		mac_copy_pipe_label(pipe->pipe_label, &intlabel);
3536105694Srwatson		PIPE_UNLOCK(pipe);
3537100979Srwatson		break;
3538100979Srwatson	default:
3539100979Srwatson		error = EINVAL;
3540105694Srwatson		fdrop(fp, td);
3541105694Srwatson		goto out;
3542100979Srwatson	}
3543105694Srwatson	fdrop(fp, td);
3544100979Srwatson
3545105694Srwatson	switch (label_type) {
3546105694Srwatson	case DTYPE_FIFO:
3547105694Srwatson	case DTYPE_VNODE:
3548105694Srwatson		if (error == 0)
3549105694Srwatson			error = mac_externalize_vnode_label(&intlabel,
3550105694Srwatson			    elements, buffer, mac.m_buflen, M_WAITOK);
3551105694Srwatson		mac_destroy_vnode_label(&intlabel);
3552105694Srwatson		break;
3553105694Srwatson	case DTYPE_PIPE:
3554105694Srwatson		error = mac_externalize_pipe_label(&intlabel, elements,
3555105694Srwatson		    buffer, mac.m_buflen, M_WAITOK);
3556105694Srwatson		mac_destroy_pipe_label(&intlabel);
3557105694Srwatson		break;
3558105694Srwatson	default:
3559105694Srwatson		panic("__mac_get_fd: corrupted label_type");
3560105694Srwatson	}
3561105694Srwatson
3562100979Srwatson	if (error == 0)
3563105694Srwatson		error = copyout(buffer, mac.m_string, strlen(buffer)+1);
3564100979Srwatson
3565105694Srwatsonout:
3566105694Srwatson	mtx_unlock(&Giant);				/* VFS */
3567105694Srwatson	free(buffer, M_MACTEMP);
3568105694Srwatson	free(elements, M_MACTEMP);
3569100979Srwatson
3570100979Srwatson	return (error);
3571100979Srwatson}
3572100979Srwatson
3573100979Srwatson/*
3574100979Srwatson * MPSAFE
3575100979Srwatson */
3576100979Srwatsonint
3577100979Srwatson__mac_get_file(struct thread *td, struct __mac_get_file_args *uap)
3578100979Srwatson{
3579105694Srwatson	char *elements, *buffer;
3580100979Srwatson	struct nameidata nd;
3581105694Srwatson	struct label intlabel;
3582105694Srwatson	struct mac mac;
3583100979Srwatson	int error;
3584100979Srwatson
3585105694Srwatson	error = copyin(uap->mac_p, &mac, sizeof(mac));
3586105694Srwatson	if (error)
3587105694Srwatson		return (error);
3588105694Srwatson
3589105694Srwatson	error = mac_check_structmac_consistent(&mac);
3590105694Srwatson	if (error)
3591105694Srwatson		return (error);
3592105694Srwatson
3593105694Srwatson	elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
3594105694Srwatson	error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
3595105694Srwatson	if (error) {
3596105694Srwatson		free(elements, M_MACTEMP);
3597105694Srwatson		return (error);
3598105694Srwatson	}
3599105694Srwatson
3600105694Srwatson	buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
3601105694Srwatson	mtx_lock(&Giant);				/* VFS */
3602105694Srwatson	NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_USERSPACE, uap->path_p,
3603105694Srwatson	    td);
3604100979Srwatson	error = namei(&nd);
3605100979Srwatson	if (error)
3606100979Srwatson		goto out;
3607100979Srwatson
3608105694Srwatson	mac_init_vnode_label(&intlabel);
3609105988Srwatson	mac_copy_vnode_label(&nd.ni_vp->v_label, &intlabel);
3610105988Srwatson	error = mac_externalize_vnode_label(&intlabel, elements, buffer,
3611105988Srwatson	    mac.m_buflen, M_WAITOK);
3612105694Srwatson
3613100979Srwatson	NDFREE(&nd, 0);
3614105694Srwatson	mac_destroy_vnode_label(&intlabel);
3615105694Srwatson
3616105694Srwatson	if (error == 0)
3617105694Srwatson		error = copyout(buffer, mac.m_string, strlen(buffer)+1);
3618105694Srwatson
3619105694Srwatsonout:
3620105694Srwatson	mtx_unlock(&Giant);				/* VFS */
3621105694Srwatson
3622105694Srwatson	free(buffer, M_MACTEMP);
3623105694Srwatson	free(elements, M_MACTEMP);
3624105694Srwatson
3625105694Srwatson	return (error);
3626105694Srwatson}
3627105694Srwatson
3628105694Srwatson/*
3629105694Srwatson * MPSAFE
3630105694Srwatson */
3631105694Srwatsonint
3632105694Srwatson__mac_get_link(struct thread *td, struct __mac_get_link_args *uap)
3633105694Srwatson{
3634105694Srwatson	char *elements, *buffer;
3635105694Srwatson	struct nameidata nd;
3636105694Srwatson	struct label intlabel;
3637105694Srwatson	struct mac mac;
3638105694Srwatson	int error;
3639105694Srwatson
3640105694Srwatson	error = copyin(uap->mac_p, &mac, sizeof(mac));
3641100979Srwatson	if (error)
3642105694Srwatson		return (error);
3643105694Srwatson
3644105694Srwatson	error = mac_check_structmac_consistent(&mac);
3645105694Srwatson	if (error)
3646105694Srwatson		return (error);
3647105694Srwatson
3648105694Srwatson	elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
3649105694Srwatson	error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
3650105694Srwatson	if (error) {
3651105694Srwatson		free(elements, M_MACTEMP);
3652105694Srwatson		return (error);
3653105694Srwatson	}
3654105694Srwatson
3655105694Srwatson	buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
3656105694Srwatson	mtx_lock(&Giant);				/* VFS */
3657105694Srwatson	NDINIT(&nd, LOOKUP, LOCKLEAF | NOFOLLOW, UIO_USERSPACE, uap->path_p,
3658105694Srwatson	    td);
3659105694Srwatson	error = namei(&nd);
3660105694Srwatson	if (error)
3661100979Srwatson		goto out;
3662100979Srwatson
3663105694Srwatson	mac_init_vnode_label(&intlabel);
3664105988Srwatson	mac_copy_vnode_label(&nd.ni_vp->v_label, &intlabel);
3665105988Srwatson	error = mac_externalize_vnode_label(&intlabel, elements, buffer,
3666105988Srwatson	    mac.m_buflen, M_WAITOK);
3667105694Srwatson	NDFREE(&nd, 0);
3668105694Srwatson	mac_destroy_vnode_label(&intlabel);
3669100979Srwatson
3670105694Srwatson	if (error == 0)
3671105694Srwatson		error = copyout(buffer, mac.m_string, strlen(buffer)+1);
3672105694Srwatson
3673100979Srwatsonout:
3674105694Srwatson	mtx_unlock(&Giant);				/* VFS */
3675105694Srwatson
3676105694Srwatson	free(buffer, M_MACTEMP);
3677105694Srwatson	free(elements, M_MACTEMP);
3678105694Srwatson
3679100979Srwatson	return (error);
3680100979Srwatson}
3681100979Srwatson
3682100979Srwatson/*
3683100979Srwatson * MPSAFE
3684100979Srwatson */
3685100979Srwatsonint
3686100979Srwatson__mac_set_fd(struct thread *td, struct __mac_set_fd_args *uap)
3687100979Srwatson{
3688105694Srwatson	struct label intlabel;
3689105694Srwatson	struct pipe *pipe;
3690100979Srwatson	struct file *fp;
3691100979Srwatson	struct mount *mp;
3692100979Srwatson	struct vnode *vp;
3693105694Srwatson	struct mac mac;
3694105694Srwatson	char *buffer;
3695100979Srwatson	int error;
3696100979Srwatson
3697105694Srwatson	error = copyin(uap->mac_p, &mac, sizeof(mac));
3698100979Srwatson	if (error)
3699105694Srwatson		return (error);
3700100979Srwatson
3701105694Srwatson	error = mac_check_structmac_consistent(&mac);
3702100979Srwatson	if (error)
3703105694Srwatson		return (error);
3704100979Srwatson
3705105694Srwatson	buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
3706105694Srwatson	error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
3707105694Srwatson	if (error) {
3708105694Srwatson		free(buffer, M_MACTEMP);
3709105694Srwatson		return (error);
3710105694Srwatson	}
3711105694Srwatson
3712105694Srwatson	mtx_lock(&Giant);				/* VFS */
3713105694Srwatson
3714105694Srwatson	error = fget(td, SCARG(uap, fd), &fp);
3715100979Srwatson	if (error)
3716105694Srwatson		goto out;
3717100979Srwatson
3718100979Srwatson	switch (fp->f_type) {
3719100979Srwatson	case DTYPE_FIFO:
3720100979Srwatson	case DTYPE_VNODE:
3721105694Srwatson		mac_init_vnode_label(&intlabel);
3722105694Srwatson		error = mac_internalize_vnode_label(&intlabel, buffer);
3723105694Srwatson		if (error) {
3724105694Srwatson			mac_destroy_vnode_label(&intlabel);
3725105694Srwatson			break;
3726105694Srwatson		}
3727105694Srwatson
3728100979Srwatson		vp = (struct vnode *)fp->f_data;
3729100979Srwatson		error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3730105694Srwatson		if (error != 0) {
3731105694Srwatson			mac_destroy_vnode_label(&intlabel);
3732100979Srwatson			break;
3733105694Srwatson		}
3734100979Srwatson
3735100979Srwatson		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3736100979Srwatson		error = vn_setlabel(vp, &intlabel, td->td_ucred);
3737100979Srwatson		VOP_UNLOCK(vp, 0, td);
3738100979Srwatson		vn_finished_write(mp);
3739105694Srwatson
3740105694Srwatson		mac_destroy_vnode_label(&intlabel);
3741100979Srwatson		break;
3742105694Srwatson
3743100979Srwatson	case DTYPE_PIPE:
3744105694Srwatson		mac_init_pipe_label(&intlabel);
3745105694Srwatson		error = mac_internalize_pipe_label(&intlabel, buffer);
3746105694Srwatson		if (error == 0) {
3747105694Srwatson			pipe = (struct pipe *)fp->f_data;
3748105694Srwatson			PIPE_LOCK(pipe);
3749105694Srwatson			error = mac_pipe_label_set(td->td_ucred, pipe,
3750105694Srwatson			    &intlabel);
3751105694Srwatson			PIPE_UNLOCK(pipe);
3752105694Srwatson		}
3753105694Srwatson
3754105694Srwatson		mac_destroy_pipe_label(&intlabel);
3755100979Srwatson		break;
3756105694Srwatson
3757100979Srwatson	default:
3758100979Srwatson		error = EINVAL;
3759100979Srwatson	}
3760100979Srwatson
3761100979Srwatson	fdrop(fp, td);
3762105694Srwatsonout:
3763105694Srwatson	mtx_unlock(&Giant);				/* VFS */
3764105694Srwatson
3765105694Srwatson	free(buffer, M_MACTEMP);
3766105694Srwatson
3767100979Srwatson	return (error);
3768100979Srwatson}
3769100979Srwatson
3770100979Srwatson/*
3771100979Srwatson * MPSAFE
3772100979Srwatson */
3773100979Srwatsonint
3774100979Srwatson__mac_set_file(struct thread *td, struct __mac_set_file_args *uap)
3775100979Srwatson{
3776105694Srwatson	struct label intlabel;
3777100979Srwatson	struct nameidata nd;
3778100979Srwatson	struct mount *mp;
3779105694Srwatson	struct mac mac;
3780105694Srwatson	char *buffer;
3781100979Srwatson	int error;
3782100979Srwatson
3783105694Srwatson	error = copyin(uap->mac_p, &mac, sizeof(mac));
3784100979Srwatson	if (error)
3785105694Srwatson		return (error);
3786100979Srwatson
3787105694Srwatson	error = mac_check_structmac_consistent(&mac);
3788100979Srwatson	if (error)
3789105694Srwatson		return (error);
3790100979Srwatson
3791105694Srwatson	buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
3792105694Srwatson	error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
3793105694Srwatson	if (error) {
3794105694Srwatson		free(buffer, M_MACTEMP);
3795105694Srwatson		return (error);
3796105694Srwatson	}
3797105694Srwatson
3798105694Srwatson	mac_init_vnode_label(&intlabel);
3799105694Srwatson	error = mac_internalize_vnode_label(&intlabel, buffer);
3800105694Srwatson	free(buffer, M_MACTEMP);
3801105694Srwatson	if (error) {
3802105694Srwatson		mac_destroy_vnode_label(&intlabel);
3803105694Srwatson		return (error);
3804105694Srwatson	}
3805105694Srwatson
3806105694Srwatson	mtx_lock(&Giant);				/* VFS */
3807105694Srwatson
3808105694Srwatson	NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_USERSPACE, uap->path_p,
3809105694Srwatson	    td);
3810100979Srwatson	error = namei(&nd);
3811105694Srwatson	if (error == 0) {
3812105694Srwatson		error = vn_start_write(nd.ni_vp, &mp, V_WAIT | PCATCH);
3813105694Srwatson		if (error == 0)
3814105694Srwatson			error = vn_setlabel(nd.ni_vp, &intlabel,
3815105694Srwatson			    td->td_ucred);
3816105694Srwatson		vn_finished_write(mp);
3817105694Srwatson	}
3818105694Srwatson
3819105694Srwatson	NDFREE(&nd, 0);
3820105694Srwatson	mtx_unlock(&Giant);				/* VFS */
3821105694Srwatson	mac_destroy_vnode_label(&intlabel);
3822105694Srwatson
3823105694Srwatson	return (error);
3824105694Srwatson}
3825105694Srwatson
3826105694Srwatson/*
3827105694Srwatson * MPSAFE
3828105694Srwatson */
3829105694Srwatsonint
3830105694Srwatson__mac_set_link(struct thread *td, struct __mac_set_link_args *uap)
3831105694Srwatson{
3832105694Srwatson	struct label intlabel;
3833105694Srwatson	struct nameidata nd;
3834105694Srwatson	struct mount *mp;
3835105694Srwatson	struct mac mac;
3836105694Srwatson	char *buffer;
3837105694Srwatson	int error;
3838105694Srwatson
3839105694Srwatson	error = copyin(uap->mac_p, &mac, sizeof(mac));
3840100979Srwatson	if (error)
3841105694Srwatson		return (error);
3842105694Srwatson
3843105694Srwatson	error = mac_check_structmac_consistent(&mac);
3844100979Srwatson	if (error)
3845105694Srwatson		return (error);
3846100979Srwatson
3847105694Srwatson	buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
3848105694Srwatson	error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
3849105694Srwatson	if (error) {
3850105694Srwatson		free(buffer, M_MACTEMP);
3851105694Srwatson		return (error);
3852105694Srwatson	}
3853105694Srwatson
3854105694Srwatson	mac_init_vnode_label(&intlabel);
3855105694Srwatson	error = mac_internalize_vnode_label(&intlabel, buffer);
3856105694Srwatson	free(buffer, M_MACTEMP);
3857105694Srwatson	if (error) {
3858105694Srwatson		mac_destroy_vnode_label(&intlabel);
3859105694Srwatson		return (error);
3860105694Srwatson	}
3861105694Srwatson
3862105694Srwatson	mtx_lock(&Giant);				/* VFS */
3863105694Srwatson
3864105694Srwatson	NDINIT(&nd, LOOKUP, LOCKLEAF | NOFOLLOW, UIO_USERSPACE, uap->path_p,
3865105694Srwatson	    td);
3866105694Srwatson	error = namei(&nd);
3867105694Srwatson	if (error == 0) {
3868105694Srwatson		error = vn_start_write(nd.ni_vp, &mp, V_WAIT | PCATCH);
3869105694Srwatson		if (error == 0)
3870105694Srwatson			error = vn_setlabel(nd.ni_vp, &intlabel,
3871105694Srwatson			    td->td_ucred);
3872105694Srwatson		vn_finished_write(mp);
3873105694Srwatson	}
3874105694Srwatson
3875100979Srwatson	NDFREE(&nd, 0);
3876105694Srwatson	mtx_unlock(&Giant);				/* VFS */
3877105694Srwatson	mac_destroy_vnode_label(&intlabel);
3878105694Srwatson
3879100979Srwatson	return (error);
3880100979Srwatson}
3881100979Srwatson
3882105694Srwatson/*
3883105694Srwatson * MPSAFE
3884105694Srwatson */
3885102123Srwatsonint
3886102123Srwatsonmac_syscall(struct thread *td, struct mac_syscall_args *uap)
3887102123Srwatson{
3888102123Srwatson	struct mac_policy_conf *mpc;
3889102123Srwatson	char target[MAC_MAX_POLICY_NAME];
3890102123Srwatson	int error;
3891102123Srwatson
3892102123Srwatson	error = copyinstr(SCARG(uap, policy), target, sizeof(target), NULL);
3893102123Srwatson	if (error)
3894102123Srwatson		return (error);
3895102123Srwatson
3896102123Srwatson	error = ENOSYS;
3897102123Srwatson	MAC_POLICY_LIST_BUSY();
3898102123Srwatson	LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {
3899102123Srwatson		if (strcmp(mpc->mpc_name, target) == 0 &&
3900102123Srwatson		    mpc->mpc_ops->mpo_syscall != NULL) {
3901102123Srwatson			error = mpc->mpc_ops->mpo_syscall(td,
3902102123Srwatson			    SCARG(uap, call), SCARG(uap, arg));
3903102123Srwatson			goto out;
3904102123Srwatson		}
3905102123Srwatson	}
3906102123Srwatson
3907102123Srwatsonout:
3908102123Srwatson	MAC_POLICY_LIST_UNBUSY();
3909102123Srwatson	return (error);
3910102123Srwatson}
3911102123Srwatson
3912100979SrwatsonSYSINIT(mac, SI_SUB_MAC, SI_ORDER_FIRST, mac_init, NULL);
3913100979SrwatsonSYSINIT(mac_late, SI_SUB_MAC_LATE, SI_ORDER_FIRST, mac_late_init, NULL);
3914100979Srwatson
3915100979Srwatson#else /* !MAC */
3916100979Srwatson
3917100979Srwatsonint
3918105694Srwatson__mac_get_pid(struct thread *td, struct __mac_get_pid_args *uap)
3919105694Srwatson{
3920105694Srwatson
3921105694Srwatson	return (ENOSYS);
3922105694Srwatson}
3923105694Srwatson
3924105694Srwatsonint
3925100979Srwatson__mac_get_proc(struct thread *td, struct __mac_get_proc_args *uap)
3926100979Srwatson{
3927100979Srwatson
3928100894Srwatson	return (ENOSYS);
3929100894Srwatson}
3930100894Srwatson
3931100894Srwatsonint
3932100894Srwatson__mac_set_proc(struct thread *td, struct __mac_set_proc_args *uap)
3933100894Srwatson{
3934100894Srwatson
3935100894Srwatson	return (ENOSYS);
3936100894Srwatson}
3937100894Srwatson
3938100894Srwatsonint
3939100894Srwatson__mac_get_fd(struct thread *td, struct __mac_get_fd_args *uap)
3940100894Srwatson{
3941100894Srwatson
3942100894Srwatson	return (ENOSYS);
3943100894Srwatson}
3944100894Srwatson
3945100894Srwatsonint
3946100894Srwatson__mac_get_file(struct thread *td, struct __mac_get_file_args *uap)
3947100894Srwatson{
3948100894Srwatson
3949100894Srwatson	return (ENOSYS);
3950100894Srwatson}
3951100894Srwatson
3952100894Srwatsonint
3953105694Srwatson__mac_get_link(struct thread *td, struct __mac_get_link_args *uap)
3954105694Srwatson{
3955105694Srwatson
3956105694Srwatson	return (ENOSYS);
3957105694Srwatson}
3958105694Srwatson
3959105694Srwatsonint
3960100894Srwatson__mac_set_fd(struct thread *td, struct __mac_set_fd_args *uap)
3961100894Srwatson{
3962100894Srwatson
3963100894Srwatson	return (ENOSYS);
3964100894Srwatson}
3965100894Srwatson
3966100894Srwatsonint
3967100894Srwatson__mac_set_file(struct thread *td, struct __mac_set_file_args *uap)
3968100894Srwatson{
3969100894Srwatson
3970100894Srwatson	return (ENOSYS);
3971100894Srwatson}
3972100979Srwatson
3973102123Srwatsonint
3974105694Srwatson__mac_set_link(struct thread *td, struct __mac_set_link_args *uap)
3975105694Srwatson{
3976105694Srwatson
3977105694Srwatson	return (ENOSYS);
3978105694Srwatson}
3979105694Srwatson
3980105694Srwatsonint
3981102123Srwatsonmac_syscall(struct thread *td, struct mac_syscall_args *uap)
3982102123Srwatson{
3983102123Srwatson
3984102123Srwatson	return (ENOSYS);
3985102123Srwatson}
3986102123Srwatson
3987105694Srwatson#endif
3988