mac_process.c revision 107089
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 *
10106392Srwatson * This software was developed for the FreeBSD Project in part by Network
11106392Srwatson * Associates Laboratories, the Security Research Division of Network
12106392Srwatson * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
13106392Srwatson * as part of the DARPA 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 *
24100894Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25100894Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26100894Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27100894Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28100894Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29100894Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30100894Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31100894Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32100894Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33100894Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34100894Srwatson * SUCH DAMAGE.
35100894Srwatson *
36100894Srwatson * $FreeBSD: head/sys/security/mac/mac_process.c 107089 2002-11-19 22:12:42Z rwatson $
37100894Srwatson */
38100894Srwatson/*
39100894Srwatson * Developed by the TrustedBSD Project.
40100894Srwatson *
41100894Srwatson * Framework for extensible kernel access control.  Kernel and userland
42100894Srwatson * interface to the framework, policy registration and composition.
43100894Srwatson */
44100894Srwatson
45100894Srwatson#include "opt_mac.h"
46104300Sphk#include "opt_devfs.h"
47101173Srwatson
48100894Srwatson#include <sys/param.h>
49106856Srwatson#include <sys/condvar.h>
50100979Srwatson#include <sys/extattr.h>
51106468Srwatson#include <sys/imgact.h>
52100979Srwatson#include <sys/kernel.h>
53100979Srwatson#include <sys/lock.h>
54102949Sbde#include <sys/malloc.h>
55100979Srwatson#include <sys/mutex.h>
56100979Srwatson#include <sys/mac.h>
57101712Srwatson#include <sys/module.h>
58100979Srwatson#include <sys/proc.h>
59100979Srwatson#include <sys/systm.h>
60100894Srwatson#include <sys/sysproto.h>
61100894Srwatson#include <sys/sysent.h>
62100979Srwatson#include <sys/vnode.h>
63100979Srwatson#include <sys/mount.h>
64100979Srwatson#include <sys/file.h>
65100979Srwatson#include <sys/namei.h>
66100979Srwatson#include <sys/socket.h>
67100979Srwatson#include <sys/pipe.h>
68100979Srwatson#include <sys/socketvar.h>
69100979Srwatson#include <sys/sysctl.h>
70100894Srwatson
71100979Srwatson#include <vm/vm.h>
72100979Srwatson#include <vm/pmap.h>
73100979Srwatson#include <vm/vm_map.h>
74100979Srwatson#include <vm/vm_object.h>
75100979Srwatson
76100979Srwatson#include <sys/mac_policy.h>
77100979Srwatson
78100979Srwatson#include <fs/devfs/devfs.h>
79100979Srwatson
80100979Srwatson#include <net/bpfdesc.h>
81100979Srwatson#include <net/if.h>
82100979Srwatson#include <net/if_var.h>
83100979Srwatson
84100979Srwatson#include <netinet/in.h>
85100979Srwatson#include <netinet/ip_var.h>
86100979Srwatson
87100979Srwatson#ifdef MAC
88100979Srwatson
89101712Srwatson/*
90101712Srwatson * Declare that the kernel provides MAC support, version 1.  This permits
91101712Srwatson * modules to refuse to be loaded if the necessary support isn't present,
92101712Srwatson * even if it's pre-boot.
93101712Srwatson */
94101712SrwatsonMODULE_VERSION(kernel_mac_support, 1);
95101712Srwatson
96100979SrwatsonSYSCTL_DECL(_security);
97100979Srwatson
98100979SrwatsonSYSCTL_NODE(_security, OID_AUTO, mac, CTLFLAG_RW, 0,
99100979Srwatson    "TrustedBSD MAC policy controls");
100104517Srwatson
101100979Srwatson#if MAC_MAX_POLICIES > 32
102100979Srwatson#error "MAC_MAX_POLICIES too large"
103100979Srwatson#endif
104105497Srwatson
105100979Srwatsonstatic unsigned int mac_max_policies = MAC_MAX_POLICIES;
106100979Srwatsonstatic unsigned int mac_policy_offsets_free = (1 << MAC_MAX_POLICIES) - 1;
107100979SrwatsonSYSCTL_UINT(_security_mac, OID_AUTO, max_policies, CTLFLAG_RD,
108100979Srwatson    &mac_max_policies, 0, "");
109100979Srwatson
110105959Srwatson/*
111105959Srwatson * Has the kernel started generating labeled objects yet?  All read/write
112105959Srwatson * access to this variable is serialized during the boot process.  Following
113105959Srwatson * the end of serialization, we don't update this flag; no locking.
114105959Srwatson */
115100979Srwatsonstatic int	mac_late = 0;
116100979Srwatson
117105988Srwatson/*
118105988Srwatson * Warn about EA transactions only the first time they happen.
119105988Srwatson * Weak coherency, no locking.
120105988Srwatson */
121105988Srwatsonstatic int	ea_warn_once = 0;
122105988Srwatson
123100979Srwatsonstatic int	mac_enforce_fs = 1;
124100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, enforce_fs, CTLFLAG_RW,
125100979Srwatson    &mac_enforce_fs, 0, "Enforce MAC policy on file system objects");
126100979SrwatsonTUNABLE_INT("security.mac.enforce_fs", &mac_enforce_fs);
127100979Srwatson
128107089Srwatsonstatic int	mac_enforce_kld = 1;
129107089SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, enforce_kld, CTLFLAG_RW,
130107089Srwatson    &mac_enforce_kld, 0, "Enforce MAC policy on kld operations");
131107089SrwatsonTUNABLE_INT("security.mac.enforce_kld", &mac_enforce_kld);
132107089Srwatson
133100979Srwatsonstatic int	mac_enforce_network = 1;
134100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, enforce_network, CTLFLAG_RW,
135100979Srwatson    &mac_enforce_network, 0, "Enforce MAC policy on network packets");
136100979SrwatsonTUNABLE_INT("security.mac.enforce_network", &mac_enforce_network);
137100979Srwatson
138103513Srwatsonstatic int	mac_enforce_pipe = 1;
139103513SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, enforce_pipe, CTLFLAG_RW,
140103513Srwatson    &mac_enforce_pipe, 0, "Enforce MAC policy on pipe operations");
141104236SrwatsonTUNABLE_INT("security.mac.enforce_pipe", &mac_enforce_pipe);
142103513Srwatson
143100979Srwatsonstatic int	mac_enforce_process = 1;
144100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, enforce_process, CTLFLAG_RW,
145100979Srwatson    &mac_enforce_process, 0, "Enforce MAC policy on inter-process operations");
146100979SrwatsonTUNABLE_INT("security.mac.enforce_process", &mac_enforce_process);
147100979Srwatson
148100979Srwatsonstatic int	mac_enforce_socket = 1;
149100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, enforce_socket, CTLFLAG_RW,
150100979Srwatson    &mac_enforce_socket, 0, "Enforce MAC policy on socket operations");
151100979SrwatsonTUNABLE_INT("security.mac.enforce_socket", &mac_enforce_socket);
152100979Srwatson
153106045Srwatsonstatic int	mac_enforce_system = 1;
154106045SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, enforce_system, CTLFLAG_RW,
155106045Srwatson    &mac_enforce_system, 0, "Enforce MAC policy on system operations");
156106045SrwatsonTUNABLE_INT("security.mac.enforce_system", &mac_enforce_system);
157106025Srwatson
158106045Srwatsonstatic int	mac_enforce_vm = 1;
159103514SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, enforce_vm, CTLFLAG_RW,
160103514Srwatson    &mac_enforce_vm, 0, "Enforce MAC policy on vm operations");
161104236SrwatsonTUNABLE_INT("security.mac.enforce_vm", &mac_enforce_vm);
162103514Srwatson
163103136Srwatsonstatic int	mac_mmap_revocation = 1;
164103136SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation, CTLFLAG_RW,
165103136Srwatson    &mac_mmap_revocation, 0, "Revoke mmap access to files on subject "
166103136Srwatson    "relabel");
167101892Srwatsonstatic int	mac_mmap_revocation_via_cow = 0;
168100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation_via_cow, CTLFLAG_RW,
169100979Srwatson    &mac_mmap_revocation_via_cow, 0, "Revoke mmap access to files via "
170100979Srwatson    "copy-on-write semantics, or by removing all write access");
171100979Srwatson
172101988Srwatson#ifdef MAC_DEBUG
173104268SrwatsonSYSCTL_NODE(_security_mac, OID_AUTO, debug, CTLFLAG_RW, 0,
174104268Srwatson    "TrustedBSD MAC debug info");
175104268Srwatson
176104268Srwatsonstatic int	mac_debug_label_fallback = 0;
177104268SrwatsonSYSCTL_INT(_security_mac_debug, OID_AUTO, label_fallback, CTLFLAG_RW,
178104268Srwatson    &mac_debug_label_fallback, 0, "Filesystems should fall back to fs label"
179104268Srwatson    "when label is corrupted.");
180104268SrwatsonTUNABLE_INT("security.mac.debug_label_fallback",
181104268Srwatson    &mac_debug_label_fallback);
182104268Srwatson
183104517SrwatsonSYSCTL_NODE(_security_mac_debug, OID_AUTO, counters, CTLFLAG_RW, 0,
184104517Srwatson    "TrustedBSD MAC object counters");
185104517Srwatson
186100979Srwatsonstatic unsigned int nmacmbufs, nmaccreds, nmacifnets, nmacbpfdescs,
187100979Srwatson    nmacsockets, nmacmounts, nmactemp, nmacvnodes, nmacdevfsdirents,
188100979Srwatson    nmacipqs, nmacpipes;
189104517Srwatson
190104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, mbufs, CTLFLAG_RD,
191100979Srwatson    &nmacmbufs, 0, "number of mbufs in use");
192104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, creds, CTLFLAG_RD,
193100979Srwatson    &nmaccreds, 0, "number of ucreds in use");
194104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, ifnets, CTLFLAG_RD,
195100979Srwatson    &nmacifnets, 0, "number of ifnets in use");
196104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, ipqs, CTLFLAG_RD,
197100979Srwatson    &nmacipqs, 0, "number of ipqs in use");
198104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, bpfdescs, CTLFLAG_RD,
199100979Srwatson    &nmacbpfdescs, 0, "number of bpfdescs in use");
200104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, sockets, CTLFLAG_RD,
201100979Srwatson    &nmacsockets, 0, "number of sockets in use");
202104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, pipes, CTLFLAG_RD,
203100979Srwatson    &nmacpipes, 0, "number of pipes in use");
204104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, mounts, CTLFLAG_RD,
205100979Srwatson    &nmacmounts, 0, "number of mounts in use");
206104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, temp, CTLFLAG_RD,
207100979Srwatson    &nmactemp, 0, "number of temporary labels in use");
208104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, vnodes, CTLFLAG_RD,
209100979Srwatson    &nmacvnodes, 0, "number of vnodes in use");
210104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, devfsdirents, CTLFLAG_RD,
211100979Srwatson    &nmacdevfsdirents, 0, "number of devfs dirents inuse");
212101988Srwatson#endif
213100979Srwatson
214100979Srwatsonstatic int	error_select(int error1, int error2);
215100979Srwatsonstatic int	mac_policy_register(struct mac_policy_conf *mpc);
216100979Srwatsonstatic int	mac_policy_unregister(struct mac_policy_conf *mpc);
217100979Srwatson
218104546Srwatsonstatic void	mac_check_vnode_mmap_downgrade(struct ucred *cred,
219104546Srwatson		    struct vnode *vp, int *prot);
220100979Srwatsonstatic void	mac_cred_mmapped_drop_perms_recurse(struct thread *td,
221100979Srwatson		    struct ucred *cred, struct vm_map *map);
222100979Srwatson
223104541Srwatsonstatic void	mac_destroy_socket_label(struct label *label);
224104541Srwatson
225105988Srwatsonstatic int	mac_setlabel_vnode_extattr(struct ucred *cred,
226105988Srwatson		    struct vnode *vp, struct label *intlabel);
227105988Srwatson
228100979SrwatsonMALLOC_DEFINE(M_MACPIPELABEL, "macpipelabel", "MAC labels for pipes");
229105694SrwatsonMALLOC_DEFINE(M_MACTEMP, "mactemp", "MAC temporary label storage");
230100979Srwatson
231100979Srwatson/*
232106856Srwatson * mac_policy_list stores the list of active policies.  A busy count is
233106856Srwatson * maintained for the list, stored in mac_policy_busy.  The busy count
234106856Srwatson * is protected by mac_policy_list_lock; the list may be modified only
235106856Srwatson * while the busy count is 0, requiring that the lock be held to
236106856Srwatson * prevent new references to the list from being acquired.  For almost
237106856Srwatson * all operations, incrementing the busy count is sufficient to
238106856Srwatson * guarantee consistency, as the list cannot be modified while the
239106856Srwatson * busy count is elevated.  For a few special operations involving a
240106856Srwatson * change to the list of active policies, the lock itself must be held.
241106856Srwatson * A condition variable, mac_policy_list_not_busy, is used to signal
242106856Srwatson * potential exclusive consumers that they should try to acquire the
243106856Srwatson * lock if a first attempt at exclusive access fails.
244100979Srwatson */
245100979Srwatsonstatic struct mtx mac_policy_list_lock;
246106856Srwatsonstatic struct cv mac_policy_list_not_busy;
247100979Srwatsonstatic LIST_HEAD(, mac_policy_conf) mac_policy_list;
248100979Srwatsonstatic int mac_policy_list_busy;
249100979Srwatson
250106856Srwatson#define	MAC_POLICY_LIST_LOCKINIT() do {					\
251106856Srwatson	mtx_init(&mac_policy_list_lock, "mac_policy_list_lock", NULL,	\
252106856Srwatson	    MTX_DEF);							\
253106856Srwatson	cv_init(&mac_policy_list_not_busy, "mac_policy_list_not_busy");	\
254106856Srwatson} while (0)
255106856Srwatson
256106856Srwatson#define	MAC_POLICY_LIST_LOCK() do {					\
257106856Srwatson	mtx_lock(&mac_policy_list_lock);				\
258106856Srwatson} while (0)
259106856Srwatson
260106856Srwatson#define	MAC_POLICY_LIST_UNLOCK() do {					\
261106856Srwatson	mtx_unlock(&mac_policy_list_lock);				\
262106856Srwatson} while (0)
263106856Srwatson
264106856Srwatson/*
265106856Srwatson * We manually invoke WITNESS_SLEEP() to allow Witness to generate
266106856Srwatson * warnings even if we don't end up ever triggering the wait at
267106856Srwatson * run-time.  The consumer of the exclusive interface must not hold
268106856Srwatson * any locks (other than potentially Giant) since we may sleep for
269106856Srwatson * long (potentially indefinite) periods of time waiting for the
270106856Srwatson * framework to become quiescent so that a policy list change may
271106856Srwatson * be made.
272106856Srwatson */
273106856Srwatson#define	MAC_POLICY_LIST_EXCLUSIVE() do {				\
274106856Srwatson	WITNESS_SLEEP(1, NULL);						\
275106856Srwatson	mtx_lock(&mac_policy_list_lock);				\
276106856Srwatson	while (mac_policy_list_busy != 0)				\
277106856Srwatson		cv_wait(&mac_policy_list_not_busy,			\
278106856Srwatson		    &mac_policy_list_lock);				\
279106856Srwatson} while (0)
280106856Srwatson
281100979Srwatson#define	MAC_POLICY_LIST_BUSY() do {					\
282100979Srwatson	MAC_POLICY_LIST_LOCK();						\
283100979Srwatson	mac_policy_list_busy++;						\
284100979Srwatson	MAC_POLICY_LIST_UNLOCK();					\
285100979Srwatson} while (0)
286100979Srwatson
287100979Srwatson#define	MAC_POLICY_LIST_UNBUSY() do {					\
288100979Srwatson	MAC_POLICY_LIST_LOCK();						\
289100979Srwatson	mac_policy_list_busy--;						\
290106856Srwatson	KASSERT(mac_policy_list_busy >= 0, ("MAC_POLICY_LIST_LOCK"));	\
291106856Srwatson	if (mac_policy_list_busy == 0)					\
292106856Srwatson		cv_signal(&mac_policy_list_not_busy);			\
293100979Srwatson	MAC_POLICY_LIST_UNLOCK();					\
294100979Srwatson} while (0)
295100979Srwatson
296100979Srwatson/*
297100979Srwatson * MAC_CHECK performs the designated check by walking the policy
298100979Srwatson * module list and checking with each as to how it feels about the
299100979Srwatson * request.  Note that it returns its value via 'error' in the scope
300100979Srwatson * of the caller.
301100979Srwatson */
302100979Srwatson#define	MAC_CHECK(check, args...) do {					\
303100979Srwatson	struct mac_policy_conf *mpc;					\
304100979Srwatson									\
305100979Srwatson	error = 0;							\
306100979Srwatson	MAC_POLICY_LIST_BUSY();						\
307100979Srwatson	LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {			\
308100979Srwatson		if (mpc->mpc_ops->mpo_ ## check != NULL)		\
309100979Srwatson			error = error_select(				\
310100979Srwatson			    mpc->mpc_ops->mpo_ ## check (args),		\
311100979Srwatson			    error);					\
312100979Srwatson	}								\
313100979Srwatson	MAC_POLICY_LIST_UNBUSY();					\
314100979Srwatson} while (0)
315100979Srwatson
316100979Srwatson/*
317100979Srwatson * MAC_BOOLEAN performs the designated boolean composition by walking
318100979Srwatson * the module list, invoking each instance of the operation, and
319100979Srwatson * combining the results using the passed C operator.  Note that it
320100979Srwatson * returns its value via 'result' in the scope of the caller, which
321100979Srwatson * should be initialized by the caller in a meaningful way to get
322100979Srwatson * a meaningful result.
323100979Srwatson */
324100979Srwatson#define	MAC_BOOLEAN(operation, composition, args...) do {		\
325100979Srwatson	struct mac_policy_conf *mpc;					\
326100979Srwatson									\
327100979Srwatson	MAC_POLICY_LIST_BUSY();						\
328100979Srwatson	LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {			\
329100979Srwatson		if (mpc->mpc_ops->mpo_ ## operation != NULL)		\
330100979Srwatson			result = result composition			\
331100979Srwatson			    mpc->mpc_ops->mpo_ ## operation (args);	\
332100979Srwatson	}								\
333100979Srwatson	MAC_POLICY_LIST_UNBUSY();					\
334100979Srwatson} while (0)
335100979Srwatson
336105694Srwatson#define	MAC_EXTERNALIZE(type, label, elementlist, outbuf, 		\
337105694Srwatson    outbuflen) do {							\
338105694Srwatson	char *curptr, *curptr_start, *element_name, *element_temp;	\
339105694Srwatson	size_t left, left_start, len;					\
340105694Srwatson	int claimed, first, first_start, ignorenotfound;		\
341105694Srwatson									\
342105694Srwatson	error = 0;							\
343105694Srwatson	element_temp = elementlist;					\
344105694Srwatson	curptr = outbuf;						\
345105694Srwatson	curptr[0] = '\0';						\
346105694Srwatson	left = outbuflen;						\
347105694Srwatson	first = 1;							\
348105694Srwatson	while ((element_name = strsep(&element_temp, ",")) != NULL) {	\
349105694Srwatson		curptr_start = curptr;					\
350105694Srwatson		left_start = left;					\
351105694Srwatson		first_start = first;					\
352105694Srwatson		if (element_name[0] == '?') {				\
353105694Srwatson			element_name++;					\
354105694Srwatson			ignorenotfound = 1;				\
355105694Srwatson		} else							\
356105694Srwatson			ignorenotfound = 0;				\
357105694Srwatson		claimed = 0;						\
358105694Srwatson		if (first) {						\
359105694Srwatson			len = snprintf(curptr, left, "%s/",		\
360105694Srwatson			    element_name);				\
361105694Srwatson			first = 0;					\
362105694Srwatson		} else							\
363105694Srwatson			len = snprintf(curptr, left, ",%s/",		\
364105694Srwatson			    element_name);				\
365105694Srwatson		if (len >= left) {					\
366105694Srwatson			error = EINVAL;		/* XXXMAC: E2BIG */	\
367105694Srwatson			break;						\
368105694Srwatson		}							\
369105694Srwatson		curptr += len;						\
370105694Srwatson		left -= len;						\
371105694Srwatson									\
372105694Srwatson		MAC_CHECK(externalize_ ## type, label, element_name,	\
373105694Srwatson		    curptr, left, &len, &claimed);			\
374105694Srwatson		if (error)						\
375105694Srwatson			break;						\
376105694Srwatson		if (claimed == 1) {					\
377105694Srwatson			if (len >= outbuflen) {				\
378105694Srwatson				error = EINVAL;	/* XXXMAC: E2BIG */	\
379105694Srwatson				break;					\
380105694Srwatson			}						\
381105694Srwatson			curptr += len;					\
382105694Srwatson			left -= len;					\
383105694Srwatson		} else if (claimed == 0 && ignorenotfound) {		\
384105694Srwatson			/*						\
385105694Srwatson			 * Revert addition of the label element		\
386105694Srwatson			 * name.					\
387105694Srwatson			 */						\
388105694Srwatson			curptr = curptr_start;				\
389105694Srwatson			*curptr = '\0';					\
390105694Srwatson			left = left_start;				\
391105694Srwatson			first = first_start;				\
392105694Srwatson		} else {						\
393105694Srwatson			error = EINVAL;		/* XXXMAC: ENOLABEL */	\
394105694Srwatson			break;						\
395105694Srwatson		}							\
396105694Srwatson	}								\
397105694Srwatson} while (0)
398105694Srwatson
399105694Srwatson#define	MAC_INTERNALIZE(type, label, instring) do {			\
400105694Srwatson	char *element, *element_name, *element_data;			\
401105694Srwatson	int claimed;							\
402105694Srwatson									\
403105694Srwatson	error = 0;							\
404105694Srwatson	element = instring;						\
405105694Srwatson	while ((element_name = strsep(&element, ",")) != NULL) {	\
406105694Srwatson		element_data = element_name;				\
407105694Srwatson		element_name = strsep(&element_data, "/");		\
408105694Srwatson		if (element_data == NULL) {				\
409105694Srwatson			error = EINVAL;					\
410105694Srwatson			break;						\
411105694Srwatson		}							\
412105694Srwatson		claimed = 0;						\
413105694Srwatson		MAC_CHECK(internalize_ ## type, label, element_name,	\
414105694Srwatson		    element_data, &claimed);				\
415105694Srwatson		if (error)						\
416105694Srwatson			break;						\
417105694Srwatson		if (claimed != 1) {					\
418105694Srwatson			/* XXXMAC: Another error here? */		\
419105694Srwatson			error = EINVAL;					\
420105694Srwatson			break;						\
421105694Srwatson		}							\
422105694Srwatson	}								\
423105694Srwatson} while (0)
424105694Srwatson
425100979Srwatson/*
426100979Srwatson * MAC_PERFORM performs the designated operation by walking the policy
427100979Srwatson * module list and invoking that operation for each policy.
428100979Srwatson */
429100979Srwatson#define	MAC_PERFORM(operation, args...) do {				\
430100979Srwatson	struct mac_policy_conf *mpc;					\
431100979Srwatson									\
432100979Srwatson	MAC_POLICY_LIST_BUSY();						\
433100979Srwatson	LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {			\
434100979Srwatson		if (mpc->mpc_ops->mpo_ ## operation != NULL)		\
435100979Srwatson			mpc->mpc_ops->mpo_ ## operation (args);		\
436100979Srwatson	}								\
437100979Srwatson	MAC_POLICY_LIST_UNBUSY();					\
438100979Srwatson} while (0)
439100979Srwatson
440100979Srwatson/*
441100979Srwatson * Initialize the MAC subsystem, including appropriate SMP locks.
442100979Srwatson */
443100979Srwatsonstatic void
444100979Srwatsonmac_init(void)
445100979Srwatson{
446100979Srwatson
447100979Srwatson	LIST_INIT(&mac_policy_list);
448100979Srwatson	MAC_POLICY_LIST_LOCKINIT();
449100979Srwatson}
450100979Srwatson
451100979Srwatson/*
452100979Srwatson * For the purposes of modules that want to know if they were loaded
453100979Srwatson * "early", set the mac_late flag once we've processed modules either
454100979Srwatson * linked into the kernel, or loaded before the kernel startup.
455100979Srwatson */
456100979Srwatsonstatic void
457100979Srwatsonmac_late_init(void)
458100979Srwatson{
459100979Srwatson
460100979Srwatson	mac_late = 1;
461100979Srwatson}
462100979Srwatson
463100979Srwatson/*
464100979Srwatson * Allow MAC policy modules to register during boot, etc.
465100979Srwatson */
466100894Srwatsonint
467100979Srwatsonmac_policy_modevent(module_t mod, int type, void *data)
468100979Srwatson{
469100979Srwatson	struct mac_policy_conf *mpc;
470100979Srwatson	int error;
471100979Srwatson
472100979Srwatson	error = 0;
473100979Srwatson	mpc = (struct mac_policy_conf *) data;
474100979Srwatson
475100979Srwatson	switch (type) {
476100979Srwatson	case MOD_LOAD:
477100979Srwatson		if (mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_NOTLATE &&
478100979Srwatson		    mac_late) {
479100979Srwatson			printf("mac_policy_modevent: can't load %s policy "
480100979Srwatson			    "after booting\n", mpc->mpc_name);
481100979Srwatson			error = EBUSY;
482100979Srwatson			break;
483100979Srwatson		}
484100979Srwatson		error = mac_policy_register(mpc);
485100979Srwatson		break;
486100979Srwatson	case MOD_UNLOAD:
487100979Srwatson		/* Don't unregister the module if it was never registered. */
488100979Srwatson		if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED)
489100979Srwatson		    != 0)
490100979Srwatson			error = mac_policy_unregister(mpc);
491100979Srwatson		else
492100979Srwatson			error = 0;
493100979Srwatson		break;
494100979Srwatson	default:
495100979Srwatson		break;
496100979Srwatson	}
497100979Srwatson
498100979Srwatson	return (error);
499100979Srwatson}
500100979Srwatson
501100979Srwatsonstatic int
502100979Srwatsonmac_policy_register(struct mac_policy_conf *mpc)
503100979Srwatson{
504100979Srwatson	struct mac_policy_conf *tmpc;
505100979Srwatson	int slot;
506100979Srwatson
507106856Srwatson	MAC_POLICY_LIST_EXCLUSIVE();
508100979Srwatson	LIST_FOREACH(tmpc, &mac_policy_list, mpc_list) {
509100979Srwatson		if (strcmp(tmpc->mpc_name, mpc->mpc_name) == 0) {
510100979Srwatson			MAC_POLICY_LIST_UNLOCK();
511100979Srwatson			return (EEXIST);
512100979Srwatson		}
513100979Srwatson	}
514100979Srwatson	if (mpc->mpc_field_off != NULL) {
515100979Srwatson		slot = ffs(mac_policy_offsets_free);
516100979Srwatson		if (slot == 0) {
517100979Srwatson			MAC_POLICY_LIST_UNLOCK();
518100979Srwatson			return (ENOMEM);
519100979Srwatson		}
520100979Srwatson		slot--;
521100979Srwatson		mac_policy_offsets_free &= ~(1 << slot);
522100979Srwatson		*mpc->mpc_field_off = slot;
523100979Srwatson	}
524100979Srwatson	mpc->mpc_runtime_flags |= MPC_RUNTIME_FLAG_REGISTERED;
525100979Srwatson	LIST_INSERT_HEAD(&mac_policy_list, mpc, mpc_list);
526100979Srwatson
527100979Srwatson	/* Per-policy initialization. */
528100979Srwatson	if (mpc->mpc_ops->mpo_init != NULL)
529100979Srwatson		(*(mpc->mpc_ops->mpo_init))(mpc);
530100979Srwatson	MAC_POLICY_LIST_UNLOCK();
531100979Srwatson
532100979Srwatson	printf("Security policy loaded: %s (%s)\n", mpc->mpc_fullname,
533100979Srwatson	    mpc->mpc_name);
534100979Srwatson
535100979Srwatson	return (0);
536100979Srwatson}
537100979Srwatson
538100979Srwatsonstatic int
539100979Srwatsonmac_policy_unregister(struct mac_policy_conf *mpc)
540100979Srwatson{
541100979Srwatson
542104520Srwatson	/*
543104520Srwatson	 * If we fail the load, we may get a request to unload.  Check
544104520Srwatson	 * to see if we did the run-time registration, and if not,
545104520Srwatson	 * silently succeed.
546104520Srwatson	 */
547106856Srwatson	MAC_POLICY_LIST_EXCLUSIVE();
548104520Srwatson	if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED) == 0) {
549104520Srwatson		MAC_POLICY_LIST_UNLOCK();
550104520Srwatson		return (0);
551104520Srwatson	}
552100979Srwatson#if 0
553100979Srwatson	/*
554100979Srwatson	 * Don't allow unloading modules with private data.
555100979Srwatson	 */
556104520Srwatson	if (mpc->mpc_field_off != NULL) {
557104520Srwatson		MAC_POLICY_LIST_UNLOCK();
558100979Srwatson		return (EBUSY);
559104520Srwatson	}
560100979Srwatson#endif
561104520Srwatson	/*
562104520Srwatson	 * Only allow the unload to proceed if the module is unloadable
563104520Srwatson	 * by its own definition.
564104520Srwatson	 */
565104520Srwatson	if ((mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK) == 0) {
566104520Srwatson		MAC_POLICY_LIST_UNLOCK();
567100979Srwatson		return (EBUSY);
568104520Srwatson	}
569100979Srwatson	if (mpc->mpc_ops->mpo_destroy != NULL)
570100979Srwatson		(*(mpc->mpc_ops->mpo_destroy))(mpc);
571100979Srwatson
572100979Srwatson	LIST_REMOVE(mpc, mpc_list);
573106856Srwatson	mpc->mpc_runtime_flags &= ~MPC_RUNTIME_FLAG_REGISTERED;
574106856Srwatson
575100979Srwatson	MAC_POLICY_LIST_UNLOCK();
576100979Srwatson
577100979Srwatson	printf("Security policy unload: %s (%s)\n", mpc->mpc_fullname,
578100979Srwatson	    mpc->mpc_name);
579100979Srwatson
580100979Srwatson	return (0);
581100979Srwatson}
582100979Srwatson
583100979Srwatson/*
584100979Srwatson * Define an error value precedence, and given two arguments, selects the
585100979Srwatson * value with the higher precedence.
586100979Srwatson */
587100979Srwatsonstatic int
588100979Srwatsonerror_select(int error1, int error2)
589100979Srwatson{
590100979Srwatson
591100979Srwatson	/* Certain decision-making errors take top priority. */
592100979Srwatson	if (error1 == EDEADLK || error2 == EDEADLK)
593100979Srwatson		return (EDEADLK);
594100979Srwatson
595100979Srwatson	/* Invalid arguments should be reported where possible. */
596100979Srwatson	if (error1 == EINVAL || error2 == EINVAL)
597100979Srwatson		return (EINVAL);
598100979Srwatson
599100979Srwatson	/* Precedence goes to "visibility", with both process and file. */
600100979Srwatson	if (error1 == ESRCH || error2 == ESRCH)
601100979Srwatson		return (ESRCH);
602100979Srwatson
603100979Srwatson	if (error1 == ENOENT || error2 == ENOENT)
604100979Srwatson		return (ENOENT);
605100979Srwatson
606100979Srwatson	/* Precedence goes to DAC/MAC protections. */
607100979Srwatson	if (error1 == EACCES || error2 == EACCES)
608100979Srwatson		return (EACCES);
609100979Srwatson
610100979Srwatson	/* Precedence goes to privilege. */
611100979Srwatson	if (error1 == EPERM || error2 == EPERM)
612100979Srwatson		return (EPERM);
613100979Srwatson
614100979Srwatson	/* Precedence goes to error over success; otherwise, arbitrary. */
615100979Srwatson	if (error1 != 0)
616100979Srwatson		return (error1);
617100979Srwatson	return (error2);
618100979Srwatson}
619100979Srwatson
620104521Srwatsonstatic void
621104521Srwatsonmac_init_label(struct label *label)
622104521Srwatson{
623104521Srwatson
624104521Srwatson	bzero(label, sizeof(*label));
625104521Srwatson	label->l_flags = MAC_FLAG_INITIALIZED;
626104521Srwatson}
627104521Srwatson
628104521Srwatsonstatic void
629104521Srwatsonmac_destroy_label(struct label *label)
630104521Srwatson{
631104521Srwatson
632104521Srwatson	KASSERT(label->l_flags & MAC_FLAG_INITIALIZED,
633104521Srwatson	    ("destroying uninitialized label"));
634104521Srwatson
635104521Srwatson	bzero(label, sizeof(*label));
636104521Srwatson	/* implicit: label->l_flags &= ~MAC_FLAG_INITIALIZED; */
637104521Srwatson}
638104521Srwatson
639100979Srwatsonvoid
640104527Srwatsonmac_init_bpfdesc(struct bpf_d *bpf_d)
641104521Srwatson{
642104521Srwatson
643104527Srwatson	mac_init_label(&bpf_d->bd_label);
644104527Srwatson	MAC_PERFORM(init_bpfdesc_label, &bpf_d->bd_label);
645104521Srwatson#ifdef MAC_DEBUG
646104527Srwatson	atomic_add_int(&nmacbpfdescs, 1);
647104521Srwatson#endif
648104521Srwatson}
649104521Srwatson
650105694Srwatsonstatic void
651105694Srwatsonmac_init_cred_label(struct label *label)
652104521Srwatson{
653104521Srwatson
654105694Srwatson	mac_init_label(label);
655105694Srwatson	MAC_PERFORM(init_cred_label, label);
656104521Srwatson#ifdef MAC_DEBUG
657104521Srwatson	atomic_add_int(&nmaccreds, 1);
658104521Srwatson#endif
659104521Srwatson}
660104521Srwatson
661104521Srwatsonvoid
662105694Srwatsonmac_init_cred(struct ucred *cred)
663105694Srwatson{
664105694Srwatson
665105694Srwatson	mac_init_cred_label(&cred->cr_label);
666105694Srwatson}
667105694Srwatson
668105694Srwatsonvoid
669104527Srwatsonmac_init_devfsdirent(struct devfs_dirent *de)
670104521Srwatson{
671104521Srwatson
672104527Srwatson	mac_init_label(&de->de_label);
673104527Srwatson	MAC_PERFORM(init_devfsdirent_label, &de->de_label);
674104521Srwatson#ifdef MAC_DEBUG
675104527Srwatson	atomic_add_int(&nmacdevfsdirents, 1);
676104521Srwatson#endif
677104521Srwatson}
678104521Srwatson
679105694Srwatsonstatic void
680105694Srwatsonmac_init_ifnet_label(struct label *label)
681104521Srwatson{
682104521Srwatson
683105694Srwatson	mac_init_label(label);
684105694Srwatson	MAC_PERFORM(init_ifnet_label, label);
685104521Srwatson#ifdef MAC_DEBUG
686104521Srwatson	atomic_add_int(&nmacifnets, 1);
687104521Srwatson#endif
688104521Srwatson}
689104521Srwatson
690104521Srwatsonvoid
691105694Srwatsonmac_init_ifnet(struct ifnet *ifp)
692105694Srwatson{
693105694Srwatson
694105694Srwatson	mac_init_ifnet_label(&ifp->if_label);
695105694Srwatson}
696105694Srwatson
697105694Srwatsonvoid
698104527Srwatsonmac_init_ipq(struct ipq *ipq)
699104521Srwatson{
700104521Srwatson
701104527Srwatson	mac_init_label(&ipq->ipq_label);
702104527Srwatson	MAC_PERFORM(init_ipq_label, &ipq->ipq_label);
703104521Srwatson#ifdef MAC_DEBUG
704104527Srwatson	atomic_add_int(&nmacipqs, 1);
705104521Srwatson#endif
706104521Srwatson}
707104521Srwatson
708104527Srwatsonint
709104527Srwatsonmac_init_mbuf(struct mbuf *m, int flag)
710104527Srwatson{
711104528Srwatson	int error;
712104528Srwatson
713104527Srwatson	KASSERT(m->m_flags & M_PKTHDR, ("mac_init_mbuf on non-header mbuf"));
714104527Srwatson
715104527Srwatson	mac_init_label(&m->m_pkthdr.label);
716104527Srwatson
717104528Srwatson	MAC_CHECK(init_mbuf_label, &m->m_pkthdr.label, flag);
718104528Srwatson	if (error) {
719104528Srwatson		MAC_PERFORM(destroy_mbuf_label, &m->m_pkthdr.label);
720104528Srwatson		mac_destroy_label(&m->m_pkthdr.label);
721104528Srwatson	}
722104528Srwatson
723104527Srwatson#ifdef MAC_DEBUG
724104528Srwatson	if (error == 0)
725104528Srwatson		atomic_add_int(&nmacmbufs, 1);
726104527Srwatson#endif
727104528Srwatson	return (error);
728104527Srwatson}
729104527Srwatson
730104521Srwatsonvoid
731104527Srwatsonmac_init_mount(struct mount *mp)
732104521Srwatson{
733104521Srwatson
734104527Srwatson	mac_init_label(&mp->mnt_mntlabel);
735104527Srwatson	mac_init_label(&mp->mnt_fslabel);
736104527Srwatson	MAC_PERFORM(init_mount_label, &mp->mnt_mntlabel);
737104527Srwatson	MAC_PERFORM(init_mount_fs_label, &mp->mnt_fslabel);
738104521Srwatson#ifdef MAC_DEBUG
739104527Srwatson	atomic_add_int(&nmacmounts, 1);
740104521Srwatson#endif
741104521Srwatson}
742104521Srwatson
743105694Srwatsonstatic void
744105694Srwatsonmac_init_pipe_label(struct label *label)
745105694Srwatson{
746105694Srwatson
747105694Srwatson	mac_init_label(label);
748105694Srwatson	MAC_PERFORM(init_pipe_label, label);
749105694Srwatson#ifdef MAC_DEBUG
750105694Srwatson	atomic_add_int(&nmacpipes, 1);
751105694Srwatson#endif
752105694Srwatson}
753105694Srwatson
754104521Srwatsonvoid
755104527Srwatsonmac_init_pipe(struct pipe *pipe)
756104521Srwatson{
757104527Srwatson	struct label *label;
758104521Srwatson
759104527Srwatson	label = malloc(sizeof(struct label), M_MACPIPELABEL, M_ZERO|M_WAITOK);
760104527Srwatson	pipe->pipe_label = label;
761104527Srwatson	pipe->pipe_peer->pipe_label = label;
762105694Srwatson	mac_init_pipe_label(label);
763104521Srwatson}
764104521Srwatson
765104541Srwatsonstatic int
766104541Srwatsonmac_init_socket_label(struct label *label, int flag)
767104521Srwatson{
768104541Srwatson	int error;
769104521Srwatson
770104541Srwatson	mac_init_label(label);
771104541Srwatson
772104541Srwatson	MAC_CHECK(init_socket_label, label, flag);
773104541Srwatson	if (error) {
774104541Srwatson		MAC_PERFORM(destroy_socket_label, label);
775104541Srwatson		mac_destroy_label(label);
776104541Srwatson	}
777104541Srwatson
778104521Srwatson#ifdef MAC_DEBUG
779104541Srwatson	if (error == 0)
780104541Srwatson		atomic_add_int(&nmacsockets, 1);
781104521Srwatson#endif
782104541Srwatson
783104541Srwatson	return (error);
784104521Srwatson}
785104521Srwatson
786104541Srwatsonstatic int
787104541Srwatsonmac_init_socket_peer_label(struct label *label, int flag)
788104541Srwatson{
789104541Srwatson	int error;
790104541Srwatson
791104541Srwatson	mac_init_label(label);
792104541Srwatson
793104541Srwatson	MAC_CHECK(init_socket_peer_label, label, flag);
794104541Srwatson	if (error) {
795104541Srwatson		MAC_PERFORM(destroy_socket_label, label);
796104541Srwatson		mac_destroy_label(label);
797104541Srwatson	}
798104541Srwatson
799104541Srwatson	return (error);
800104541Srwatson}
801104541Srwatson
802104541Srwatsonint
803104541Srwatsonmac_init_socket(struct socket *socket, int flag)
804104541Srwatson{
805104541Srwatson	int error;
806104541Srwatson
807104541Srwatson	error = mac_init_socket_label(&socket->so_label, flag);
808104541Srwatson	if (error)
809104541Srwatson		return (error);
810104541Srwatson
811104541Srwatson	error = mac_init_socket_peer_label(&socket->so_peerlabel, flag);
812104541Srwatson	if (error)
813104541Srwatson		mac_destroy_socket_label(&socket->so_label);
814104541Srwatson
815104541Srwatson	return (error);
816104541Srwatson}
817104541Srwatson
818105988Srwatsonvoid
819105694Srwatsonmac_init_vnode_label(struct label *label)
820104521Srwatson{
821104521Srwatson
822104527Srwatson	mac_init_label(label);
823105694Srwatson	MAC_PERFORM(init_vnode_label, label);
824104521Srwatson#ifdef MAC_DEBUG
825105694Srwatson	atomic_add_int(&nmacvnodes, 1);
826104521Srwatson#endif
827104521Srwatson}
828104521Srwatson
829104521Srwatsonvoid
830104527Srwatsonmac_init_vnode(struct vnode *vp)
831104521Srwatson{
832104521Srwatson
833105694Srwatson	mac_init_vnode_label(&vp->v_label);
834104521Srwatson}
835104521Srwatson
836104521Srwatsonvoid
837104527Srwatsonmac_destroy_bpfdesc(struct bpf_d *bpf_d)
838104521Srwatson{
839104521Srwatson
840104527Srwatson	MAC_PERFORM(destroy_bpfdesc_label, &bpf_d->bd_label);
841104527Srwatson	mac_destroy_label(&bpf_d->bd_label);
842104521Srwatson#ifdef MAC_DEBUG
843104527Srwatson	atomic_subtract_int(&nmacbpfdescs, 1);
844104521Srwatson#endif
845104521Srwatson}
846104521Srwatson
847105694Srwatsonstatic void
848105694Srwatsonmac_destroy_cred_label(struct label *label)
849104521Srwatson{
850104521Srwatson
851105694Srwatson	MAC_PERFORM(destroy_cred_label, label);
852105694Srwatson	mac_destroy_label(label);
853104521Srwatson#ifdef MAC_DEBUG
854104527Srwatson	atomic_subtract_int(&nmaccreds, 1);
855104521Srwatson#endif
856104521Srwatson}
857104521Srwatson
858104521Srwatsonvoid
859105694Srwatsonmac_destroy_cred(struct ucred *cred)
860105694Srwatson{
861105694Srwatson
862105694Srwatson	mac_destroy_cred_label(&cred->cr_label);
863105694Srwatson}
864105694Srwatson
865105694Srwatsonvoid
866104527Srwatsonmac_destroy_devfsdirent(struct devfs_dirent *de)
867104521Srwatson{
868104521Srwatson
869104527Srwatson	MAC_PERFORM(destroy_devfsdirent_label, &de->de_label);
870104527Srwatson	mac_destroy_label(&de->de_label);
871104521Srwatson#ifdef MAC_DEBUG
872104527Srwatson	atomic_subtract_int(&nmacdevfsdirents, 1);
873104521Srwatson#endif
874104521Srwatson}
875104521Srwatson
876105694Srwatsonstatic void
877105694Srwatsonmac_destroy_ifnet_label(struct label *label)
878104521Srwatson{
879104521Srwatson
880105694Srwatson	MAC_PERFORM(destroy_ifnet_label, label);
881105694Srwatson	mac_destroy_label(label);
882104521Srwatson#ifdef MAC_DEBUG
883104527Srwatson	atomic_subtract_int(&nmacifnets, 1);
884104521Srwatson#endif
885104521Srwatson}
886104521Srwatson
887104521Srwatsonvoid
888105694Srwatsonmac_destroy_ifnet(struct ifnet *ifp)
889105694Srwatson{
890105694Srwatson
891105694Srwatson	mac_destroy_ifnet_label(&ifp->if_label);
892105694Srwatson}
893105694Srwatson
894105694Srwatsonvoid
895104527Srwatsonmac_destroy_ipq(struct ipq *ipq)
896104521Srwatson{
897104521Srwatson
898104527Srwatson	MAC_PERFORM(destroy_ipq_label, &ipq->ipq_label);
899104527Srwatson	mac_destroy_label(&ipq->ipq_label);
900104521Srwatson#ifdef MAC_DEBUG
901104527Srwatson	atomic_subtract_int(&nmacipqs, 1);
902104521Srwatson#endif
903104521Srwatson}
904104521Srwatson
905104527Srwatsonvoid
906104527Srwatsonmac_destroy_mbuf(struct mbuf *m)
907104521Srwatson{
908104521Srwatson
909104527Srwatson	MAC_PERFORM(destroy_mbuf_label, &m->m_pkthdr.label);
910104527Srwatson	mac_destroy_label(&m->m_pkthdr.label);
911104521Srwatson#ifdef MAC_DEBUG
912104527Srwatson	atomic_subtract_int(&nmacmbufs, 1);
913104521Srwatson#endif
914104521Srwatson}
915104521Srwatson
916104527Srwatsonvoid
917104527Srwatsonmac_destroy_mount(struct mount *mp)
918104521Srwatson{
919104521Srwatson
920104527Srwatson	MAC_PERFORM(destroy_mount_label, &mp->mnt_mntlabel);
921104527Srwatson	MAC_PERFORM(destroy_mount_fs_label, &mp->mnt_fslabel);
922104527Srwatson	mac_destroy_label(&mp->mnt_fslabel);
923104527Srwatson	mac_destroy_label(&mp->mnt_mntlabel);
924104521Srwatson#ifdef MAC_DEBUG
925104527Srwatson	atomic_subtract_int(&nmacmounts, 1);
926104521Srwatson#endif
927104521Srwatson}
928104521Srwatson
929105694Srwatsonstatic void
930105694Srwatsonmac_destroy_pipe_label(struct label *label)
931104521Srwatson{
932104521Srwatson
933105694Srwatson	MAC_PERFORM(destroy_pipe_label, label);
934105694Srwatson	mac_destroy_label(label);
935104521Srwatson#ifdef MAC_DEBUG
936104527Srwatson	atomic_subtract_int(&nmacpipes, 1);
937104521Srwatson#endif
938104521Srwatson}
939104521Srwatson
940105694Srwatsonvoid
941105694Srwatsonmac_destroy_pipe(struct pipe *pipe)
942105694Srwatson{
943105694Srwatson
944105694Srwatson	mac_destroy_pipe_label(pipe->pipe_label);
945105694Srwatson	free(pipe->pipe_label, M_MACPIPELABEL);
946105694Srwatson}
947105694Srwatson
948104541Srwatsonstatic void
949104541Srwatsonmac_destroy_socket_label(struct label *label)
950104521Srwatson{
951104521Srwatson
952104541Srwatson	MAC_PERFORM(destroy_socket_label, label);
953104541Srwatson	mac_destroy_label(label);
954104521Srwatson#ifdef MAC_DEBUG
955104527Srwatson	atomic_subtract_int(&nmacsockets, 1);
956104521Srwatson#endif
957104521Srwatson}
958104521Srwatson
959104527Srwatsonstatic void
960104541Srwatsonmac_destroy_socket_peer_label(struct label *label)
961104541Srwatson{
962104541Srwatson
963104541Srwatson	MAC_PERFORM(destroy_socket_peer_label, label);
964104541Srwatson	mac_destroy_label(label);
965104541Srwatson}
966104541Srwatson
967104541Srwatsonvoid
968104541Srwatsonmac_destroy_socket(struct socket *socket)
969104541Srwatson{
970104541Srwatson
971104541Srwatson	mac_destroy_socket_label(&socket->so_label);
972104541Srwatson	mac_destroy_socket_peer_label(&socket->so_peerlabel);
973104541Srwatson}
974104541Srwatson
975105988Srwatsonvoid
976105694Srwatsonmac_destroy_vnode_label(struct label *label)
977104521Srwatson{
978104521Srwatson
979105694Srwatson	MAC_PERFORM(destroy_vnode_label, label);
980104527Srwatson	mac_destroy_label(label);
981104521Srwatson#ifdef MAC_DEBUG
982105694Srwatson	atomic_subtract_int(&nmacvnodes, 1);
983104521Srwatson#endif
984104521Srwatson}
985104521Srwatson
986104521Srwatsonvoid
987104527Srwatsonmac_destroy_vnode(struct vnode *vp)
988104521Srwatson{
989104521Srwatson
990105694Srwatson	mac_destroy_vnode_label(&vp->v_label);
991104521Srwatson}
992104521Srwatson
993105694Srwatsonstatic void
994105694Srwatsonmac_copy_pipe_label(struct label *src, struct label *dest)
995105694Srwatson{
996105694Srwatson
997105694Srwatson	MAC_PERFORM(copy_pipe_label, src, dest);
998105694Srwatson}
999105694Srwatson
1000105988Srwatsonvoid
1001105694Srwatsonmac_copy_vnode_label(struct label *src, struct label *dest)
1002105694Srwatson{
1003105694Srwatson
1004105694Srwatson	MAC_PERFORM(copy_vnode_label, src, dest);
1005105694Srwatson}
1006105694Srwatson
1007104522Srwatsonstatic int
1008105694Srwatsonmac_check_structmac_consistent(struct mac *mac)
1009104522Srwatson{
1010105694Srwatson
1011105694Srwatson	if (mac->m_buflen > MAC_MAX_LABEL_BUF_LEN)
1012105694Srwatson		return (EINVAL);
1013105694Srwatson
1014105694Srwatson	return (0);
1015105694Srwatson}
1016105694Srwatson
1017105694Srwatsonstatic int
1018105694Srwatsonmac_externalize_cred_label(struct label *label, char *elements,
1019105694Srwatson    char *outbuf, size_t outbuflen, int flags)
1020105694Srwatson{
1021104522Srwatson	int error;
1022104522Srwatson
1023105694Srwatson	MAC_EXTERNALIZE(cred_label, label, elements, outbuf, outbuflen);
1024104522Srwatson
1025104522Srwatson	return (error);
1026104522Srwatson}
1027104522Srwatson
1028104522Srwatsonstatic int
1029105694Srwatsonmac_externalize_ifnet_label(struct label *label, char *elements,
1030105694Srwatson    char *outbuf, size_t outbuflen, int flags)
1031104522Srwatson{
1032104522Srwatson	int error;
1033104522Srwatson
1034105694Srwatson	MAC_EXTERNALIZE(ifnet_label, label, elements, outbuf, outbuflen);
1035104522Srwatson
1036104522Srwatson	return (error);
1037104522Srwatson}
1038104522Srwatson
1039105694Srwatsonstatic int
1040105694Srwatsonmac_externalize_pipe_label(struct label *label, char *elements,
1041105694Srwatson    char *outbuf, size_t outbuflen, int flags)
1042105694Srwatson{
1043105694Srwatson	int error;
1044105694Srwatson
1045105694Srwatson	MAC_EXTERNALIZE(pipe_label, label, elements, outbuf, outbuflen);
1046105694Srwatson
1047105694Srwatson	return (error);
1048105694Srwatson}
1049105694Srwatson
1050105694Srwatsonstatic int
1051105694Srwatsonmac_externalize_socket_label(struct label *label, char *elements,
1052105694Srwatson    char *outbuf, size_t outbuflen, int flags)
1053105694Srwatson{
1054105694Srwatson	int error;
1055105694Srwatson
1056105694Srwatson	MAC_EXTERNALIZE(socket_label, label, elements, outbuf, outbuflen);
1057105694Srwatson
1058105694Srwatson	return (error);
1059105694Srwatson}
1060105694Srwatson
1061105694Srwatsonstatic int
1062105694Srwatsonmac_externalize_socket_peer_label(struct label *label, char *elements,
1063105694Srwatson    char *outbuf, size_t outbuflen, int flags)
1064105694Srwatson{
1065105694Srwatson	int error;
1066105694Srwatson
1067105694Srwatson	MAC_EXTERNALIZE(socket_peer_label, label, elements, outbuf, outbuflen);
1068105694Srwatson
1069105694Srwatson	return (error);
1070105694Srwatson}
1071105694Srwatson
1072105694Srwatsonstatic int
1073105694Srwatsonmac_externalize_vnode_label(struct label *label, char *elements,
1074105694Srwatson    char *outbuf, size_t outbuflen, int flags)
1075105694Srwatson{
1076105694Srwatson	int error;
1077105694Srwatson
1078105694Srwatson	MAC_EXTERNALIZE(vnode_label, label, elements, outbuf, outbuflen);
1079105694Srwatson
1080105694Srwatson	return (error);
1081105694Srwatson}
1082105694Srwatson
1083105694Srwatsonstatic int
1084105694Srwatsonmac_internalize_cred_label(struct label *label, char *string)
1085105694Srwatson{
1086105694Srwatson	int error;
1087105694Srwatson
1088105694Srwatson	MAC_INTERNALIZE(cred_label, label, string);
1089105694Srwatson
1090105694Srwatson	return (error);
1091105694Srwatson}
1092105694Srwatson
1093105694Srwatsonstatic int
1094105694Srwatsonmac_internalize_ifnet_label(struct label *label, char *string)
1095105694Srwatson{
1096105694Srwatson	int error;
1097105694Srwatson
1098105694Srwatson	MAC_INTERNALIZE(ifnet_label, label, string);
1099105694Srwatson
1100105694Srwatson	return (error);
1101105694Srwatson}
1102105694Srwatson
1103105694Srwatsonstatic int
1104105694Srwatsonmac_internalize_pipe_label(struct label *label, char *string)
1105105694Srwatson{
1106105694Srwatson	int error;
1107105694Srwatson
1108105694Srwatson	MAC_INTERNALIZE(pipe_label, label, string);
1109105694Srwatson
1110105694Srwatson	return (error);
1111105694Srwatson}
1112105694Srwatson
1113105694Srwatsonstatic int
1114105694Srwatsonmac_internalize_socket_label(struct label *label, char *string)
1115105694Srwatson{
1116105694Srwatson	int error;
1117105694Srwatson
1118105694Srwatson	MAC_INTERNALIZE(socket_label, label, string);
1119105694Srwatson
1120105694Srwatson	return (error);
1121105694Srwatson}
1122105694Srwatson
1123105694Srwatsonstatic int
1124105694Srwatsonmac_internalize_vnode_label(struct label *label, char *string)
1125105694Srwatson{
1126105694Srwatson	int error;
1127105694Srwatson
1128105694Srwatson	MAC_INTERNALIZE(vnode_label, label, string);
1129105694Srwatson
1130105694Srwatson	return (error);
1131105694Srwatson}
1132105694Srwatson
1133104522Srwatson/*
1134104522Srwatson * Initialize MAC label for the first kernel process, from which other
1135104522Srwatson * kernel processes and threads are spawned.
1136104522Srwatson */
1137104521Srwatsonvoid
1138104522Srwatsonmac_create_proc0(struct ucred *cred)
1139104522Srwatson{
1140104522Srwatson
1141104522Srwatson	MAC_PERFORM(create_proc0, cred);
1142104522Srwatson}
1143104522Srwatson
1144104522Srwatson/*
1145104522Srwatson * Initialize MAC label for the first userland process, from which other
1146104522Srwatson * userland processes and threads are spawned.
1147104522Srwatson */
1148104522Srwatsonvoid
1149104522Srwatsonmac_create_proc1(struct ucred *cred)
1150104522Srwatson{
1151104522Srwatson
1152104522Srwatson	MAC_PERFORM(create_proc1, cred);
1153104522Srwatson}
1154104522Srwatson
1155104522Srwatsonvoid
1156104522Srwatsonmac_thread_userret(struct thread *td)
1157104522Srwatson{
1158104522Srwatson
1159104522Srwatson	MAC_PERFORM(thread_userret, td);
1160104522Srwatson}
1161104522Srwatson
1162104522Srwatson/*
1163104522Srwatson * When a new process is created, its label must be initialized.  Generally,
1164104522Srwatson * this involves inheritence from the parent process, modulo possible
1165104522Srwatson * deltas.  This function allows that processing to take place.
1166104522Srwatson */
1167104522Srwatsonvoid
1168104522Srwatsonmac_create_cred(struct ucred *parent_cred, struct ucred *child_cred)
1169104522Srwatson{
1170104522Srwatson
1171104522Srwatson	MAC_PERFORM(create_cred, parent_cred, child_cred);
1172104522Srwatson}
1173104522Srwatson
1174104522Srwatsonvoid
1175100979Srwatsonmac_update_devfsdirent(struct devfs_dirent *de, struct vnode *vp)
1176100979Srwatson{
1177100979Srwatson
1178100979Srwatson	MAC_PERFORM(update_devfsdirent, de, &de->de_label, vp, &vp->v_label);
1179100979Srwatson}
1180100979Srwatson
1181100979Srwatsonvoid
1182105988Srwatsonmac_associate_vnode_devfs(struct mount *mp, struct devfs_dirent *de,
1183105988Srwatson    struct vnode *vp)
1184100979Srwatson{
1185100979Srwatson
1186105988Srwatson	MAC_PERFORM(associate_vnode_devfs, mp, &mp->mnt_fslabel, de,
1187105988Srwatson	    &de->de_label, vp, &vp->v_label);
1188100979Srwatson}
1189100979Srwatson
1190105988Srwatsonint
1191105988Srwatsonmac_associate_vnode_extattr(struct mount *mp, struct vnode *vp)
1192100979Srwatson{
1193100979Srwatson	int error;
1194100979Srwatson
1195105988Srwatson	ASSERT_VOP_LOCKED(vp, "mac_associate_vnode_extattr");
1196100979Srwatson
1197105988Srwatson	MAC_CHECK(associate_vnode_extattr, mp, &mp->mnt_fslabel, vp,
1198105988Srwatson	    &vp->v_label);
1199100979Srwatson
1200100979Srwatson	return (error);
1201100979Srwatson}
1202100979Srwatson
1203100979Srwatsonvoid
1204105988Srwatsonmac_associate_vnode_singlelabel(struct mount *mp, struct vnode *vp)
1205100979Srwatson{
1206100979Srwatson
1207105988Srwatson	MAC_PERFORM(associate_vnode_singlelabel, mp, &mp->mnt_fslabel, vp,
1208105988Srwatson	    &vp->v_label);
1209100979Srwatson}
1210100979Srwatson
1211100979Srwatsonint
1212105988Srwatsonmac_create_vnode_extattr(struct ucred *cred, struct mount *mp,
1213105988Srwatson    struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
1214100979Srwatson{
1215105988Srwatson	int error;
1216100979Srwatson
1217105988Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_create_vnode_extattr");
1218105988Srwatson	ASSERT_VOP_LOCKED(vp, "mac_create_vnode_extattr");
1219100979Srwatson
1220105988Srwatson	error = VOP_OPENEXTATTR(vp, cred, curthread);
1221105988Srwatson	if (error == EOPNOTSUPP) {
1222105988Srwatson		/* XXX: Optionally abort if transactions not supported. */
1223105988Srwatson		if (ea_warn_once == 0) {
1224105988Srwatson			printf("Warning: transactions not supported "
1225105988Srwatson			    "in EA write.\n");
1226105988Srwatson			ea_warn_once = 1;
1227105988Srwatson		}
1228105988Srwatson	} else if (error)
1229100979Srwatson		return (error);
1230100979Srwatson
1231105988Srwatson	MAC_CHECK(create_vnode_extattr, cred, mp, &mp->mnt_fslabel,
1232105988Srwatson	    dvp, &dvp->v_label, vp, &vp->v_label, cnp);
1233100979Srwatson
1234105988Srwatson	if (error) {
1235105988Srwatson		VOP_CLOSEEXTATTR(vp, 0, NOCRED, curthread);
1236100979Srwatson		return (error);
1237100979Srwatson	}
1238100979Srwatson
1239105988Srwatson	error = VOP_CLOSEEXTATTR(vp, 1, NOCRED, curthread);
1240100979Srwatson
1241105988Srwatson	if (error == EOPNOTSUPP)
1242105988Srwatson		error = 0;				/* XXX */
1243100979Srwatson
1244100979Srwatson	return (error);
1245100979Srwatson}
1246100979Srwatson
1247100979Srwatsonstatic int
1248105988Srwatsonmac_setlabel_vnode_extattr(struct ucred *cred, struct vnode *vp,
1249105988Srwatson    struct label *intlabel)
1250100979Srwatson{
1251100979Srwatson	int error;
1252100979Srwatson
1253105988Srwatson	ASSERT_VOP_LOCKED(vp, "mac_setlabel_vnode_extattr");
1254100979Srwatson
1255105988Srwatson	error = VOP_OPENEXTATTR(vp, cred, curthread);
1256105988Srwatson	if (error == EOPNOTSUPP) {
1257105988Srwatson		/* XXX: Optionally abort if transactions not supported. */
1258105988Srwatson		if (ea_warn_once == 0) {
1259105988Srwatson			printf("Warning: transactions not supported "
1260105988Srwatson			    "in EA write.\n");
1261105988Srwatson			ea_warn_once = 1;
1262105988Srwatson		}
1263105988Srwatson	} else if (error)
1264105988Srwatson		return (error);
1265100979Srwatson
1266105988Srwatson	MAC_CHECK(setlabel_vnode_extattr, cred, vp, &vp->v_label, intlabel);
1267100979Srwatson
1268105988Srwatson	if (error) {
1269105988Srwatson		VOP_CLOSEEXTATTR(vp, 0, NOCRED, curthread);
1270100979Srwatson		return (error);
1271100979Srwatson	}
1272100979Srwatson
1273105988Srwatson	error = VOP_CLOSEEXTATTR(vp, 1, NOCRED, curthread);
1274100979Srwatson
1275105988Srwatson	if (error == EOPNOTSUPP)
1276105988Srwatson		error = 0;				/* XXX */
1277100979Srwatson
1278105988Srwatson	return (error);
1279100979Srwatson}
1280100979Srwatson
1281106468Srwatsonint
1282106468Srwatsonmac_execve_enter(struct image_params *imgp, struct mac *mac_p,
1283106468Srwatson    struct label *execlabelstorage)
1284106468Srwatson{
1285106468Srwatson	struct mac mac;
1286106468Srwatson	char *buffer;
1287106468Srwatson	int error;
1288106468Srwatson
1289106468Srwatson	if (mac_p == NULL)
1290106468Srwatson		return (0);
1291106468Srwatson
1292106468Srwatson	error = copyin(mac_p, &mac, sizeof(mac));
1293106468Srwatson	if (error)
1294106468Srwatson		return (error);
1295106468Srwatson
1296106468Srwatson	error = mac_check_structmac_consistent(&mac);
1297106468Srwatson	if (error)
1298106468Srwatson		return (error);
1299106468Srwatson
1300106468Srwatson	buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
1301106468Srwatson	error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
1302106468Srwatson	if (error) {
1303106468Srwatson		free(buffer, M_MACTEMP);
1304106468Srwatson		return (error);
1305106468Srwatson	}
1306106468Srwatson
1307106468Srwatson	mac_init_cred_label(execlabelstorage);
1308106468Srwatson	error = mac_internalize_cred_label(execlabelstorage, buffer);
1309106468Srwatson	free(buffer, M_MACTEMP);
1310106468Srwatson	if (error) {
1311106468Srwatson		mac_destroy_cred_label(execlabelstorage);
1312106468Srwatson		return (error);
1313106468Srwatson	}
1314106468Srwatson	imgp->execlabel = execlabelstorage;
1315106468Srwatson	return (0);
1316106468Srwatson}
1317106468Srwatson
1318100979Srwatsonvoid
1319106468Srwatsonmac_execve_exit(struct image_params *imgp)
1320100979Srwatson{
1321106468Srwatson	if (imgp->execlabel != NULL)
1322106468Srwatson		mac_destroy_cred_label(imgp->execlabel);
1323106468Srwatson}
1324100979Srwatson
1325106468Srwatsonvoid
1326106468Srwatsonmac_execve_transition(struct ucred *old, struct ucred *new, struct vnode *vp,
1327106468Srwatson    struct label *interpvnodelabel, struct image_params *imgp)
1328106468Srwatson{
1329106468Srwatson
1330100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_execve_transition");
1331100979Srwatson
1332106460Srwatson	if (!mac_enforce_process && !mac_enforce_fs)
1333106460Srwatson		return;
1334106460Srwatson
1335106468Srwatson	MAC_PERFORM(execve_transition, old, new, vp, &vp->v_label,
1336106647Srwatson	    interpvnodelabel, imgp, imgp->execlabel);
1337100979Srwatson}
1338100979Srwatson
1339100979Srwatsonint
1340106468Srwatsonmac_execve_will_transition(struct ucred *old, struct vnode *vp,
1341106468Srwatson    struct label *interpvnodelabel, struct image_params *imgp)
1342100979Srwatson{
1343105988Srwatson	int result;
1344100979Srwatson
1345106460Srwatson	ASSERT_VOP_LOCKED(vp, "mac_execve_will_transition");
1346106460Srwatson
1347106460Srwatson	if (!mac_enforce_process && !mac_enforce_fs)
1348106460Srwatson		return (0);
1349106460Srwatson
1350100979Srwatson	result = 0;
1351106468Srwatson	MAC_BOOLEAN(execve_will_transition, ||, old, vp, &vp->v_label,
1352106647Srwatson	    interpvnodelabel, imgp, imgp->execlabel);
1353100979Srwatson
1354100979Srwatson	return (result);
1355100979Srwatson}
1356100979Srwatson
1357100979Srwatsonint
1358106212Srwatsonmac_check_vnode_access(struct ucred *cred, struct vnode *vp, int acc_mode)
1359100979Srwatson{
1360100979Srwatson	int error;
1361100979Srwatson
1362100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_access");
1363100979Srwatson
1364100979Srwatson	if (!mac_enforce_fs)
1365100979Srwatson		return (0);
1366100979Srwatson
1367106212Srwatson	MAC_CHECK(check_vnode_access, cred, vp, &vp->v_label, acc_mode);
1368100979Srwatson	return (error);
1369100979Srwatson}
1370100979Srwatson
1371100979Srwatsonint
1372100979Srwatsonmac_check_vnode_chdir(struct ucred *cred, struct vnode *dvp)
1373100979Srwatson{
1374100979Srwatson	int error;
1375100979Srwatson
1376100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_chdir");
1377100979Srwatson
1378100979Srwatson	if (!mac_enforce_fs)
1379100979Srwatson		return (0);
1380100979Srwatson
1381100979Srwatson	MAC_CHECK(check_vnode_chdir, cred, dvp, &dvp->v_label);
1382100979Srwatson	return (error);
1383100979Srwatson}
1384100979Srwatson
1385100979Srwatsonint
1386100979Srwatsonmac_check_vnode_chroot(struct ucred *cred, struct vnode *dvp)
1387100979Srwatson{
1388100979Srwatson	int error;
1389100979Srwatson
1390100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_chroot");
1391100979Srwatson
1392100979Srwatson	if (!mac_enforce_fs)
1393100979Srwatson		return (0);
1394100979Srwatson
1395100979Srwatson	MAC_CHECK(check_vnode_chroot, cred, dvp, &dvp->v_label);
1396100979Srwatson	return (error);
1397100979Srwatson}
1398100979Srwatson
1399100979Srwatsonint
1400100979Srwatsonmac_check_vnode_create(struct ucred *cred, struct vnode *dvp,
1401100979Srwatson    struct componentname *cnp, struct vattr *vap)
1402100979Srwatson{
1403100979Srwatson	int error;
1404100979Srwatson
1405100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_create");
1406100979Srwatson
1407100979Srwatson	if (!mac_enforce_fs)
1408100979Srwatson		return (0);
1409100979Srwatson
1410100979Srwatson	MAC_CHECK(check_vnode_create, cred, dvp, &dvp->v_label, cnp, vap);
1411100979Srwatson	return (error);
1412100979Srwatson}
1413100979Srwatson
1414100979Srwatsonint
1415100979Srwatsonmac_check_vnode_delete(struct ucred *cred, struct vnode *dvp, struct vnode *vp,
1416100979Srwatson    struct componentname *cnp)
1417100979Srwatson{
1418100979Srwatson	int error;
1419100979Srwatson
1420100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_delete");
1421100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_delete");
1422100979Srwatson
1423100979Srwatson	if (!mac_enforce_fs)
1424100979Srwatson		return (0);
1425100979Srwatson
1426100979Srwatson	MAC_CHECK(check_vnode_delete, cred, dvp, &dvp->v_label, vp,
1427100979Srwatson	    &vp->v_label, cnp);
1428100979Srwatson	return (error);
1429100979Srwatson}
1430100979Srwatson
1431100979Srwatsonint
1432100979Srwatsonmac_check_vnode_deleteacl(struct ucred *cred, struct vnode *vp,
1433100979Srwatson    acl_type_t type)
1434100979Srwatson{
1435100979Srwatson	int error;
1436100979Srwatson
1437100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_deleteacl");
1438100979Srwatson
1439100979Srwatson	if (!mac_enforce_fs)
1440100979Srwatson		return (0);
1441100979Srwatson
1442100979Srwatson	MAC_CHECK(check_vnode_deleteacl, cred, vp, &vp->v_label, type);
1443100979Srwatson	return (error);
1444100979Srwatson}
1445100979Srwatson
1446100979Srwatsonint
1447106468Srwatsonmac_check_vnode_exec(struct ucred *cred, struct vnode *vp,
1448106468Srwatson    struct image_params *imgp)
1449100979Srwatson{
1450100979Srwatson	int error;
1451100979Srwatson
1452102102Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_exec");
1453102102Srwatson
1454100979Srwatson	if (!mac_enforce_process && !mac_enforce_fs)
1455100979Srwatson		return (0);
1456100979Srwatson
1457106647Srwatson	MAC_CHECK(check_vnode_exec, cred, vp, &vp->v_label, imgp,
1458106647Srwatson	    imgp->execlabel);
1459100979Srwatson
1460100979Srwatson	return (error);
1461100979Srwatson}
1462100979Srwatson
1463100979Srwatsonint
1464100979Srwatsonmac_check_vnode_getacl(struct ucred *cred, struct vnode *vp, acl_type_t type)
1465100979Srwatson{
1466100979Srwatson	int error;
1467100979Srwatson
1468100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_getacl");
1469100979Srwatson
1470100979Srwatson	if (!mac_enforce_fs)
1471100979Srwatson		return (0);
1472100979Srwatson
1473100979Srwatson	MAC_CHECK(check_vnode_getacl, cred, vp, &vp->v_label, type);
1474100979Srwatson	return (error);
1475100979Srwatson}
1476100979Srwatson
1477100979Srwatsonint
1478100979Srwatsonmac_check_vnode_getextattr(struct ucred *cred, struct vnode *vp,
1479100979Srwatson    int attrnamespace, const char *name, struct uio *uio)
1480100979Srwatson{
1481100979Srwatson	int error;
1482100979Srwatson
1483100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_getextattr");
1484100979Srwatson
1485100979Srwatson	if (!mac_enforce_fs)
1486100979Srwatson		return (0);
1487100979Srwatson
1488100979Srwatson	MAC_CHECK(check_vnode_getextattr, cred, vp, &vp->v_label,
1489100979Srwatson	    attrnamespace, name, uio);
1490100979Srwatson	return (error);
1491100979Srwatson}
1492100979Srwatson
1493100979Srwatsonint
1494104529Srwatsonmac_check_vnode_link(struct ucred *cred, struct vnode *dvp,
1495104529Srwatson    struct vnode *vp, struct componentname *cnp)
1496104529Srwatson{
1497104529Srwatson	int error;
1498104529Srwatson
1499104529Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_link");
1500104529Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_link");
1501104529Srwatson
1502104529Srwatson	if (!mac_enforce_fs)
1503104529Srwatson		return (0);
1504104529Srwatson
1505104529Srwatson	MAC_CHECK(check_vnode_link, cred, dvp, &dvp->v_label, vp,
1506104529Srwatson	    &vp->v_label, cnp);
1507104529Srwatson	return (error);
1508104529Srwatson}
1509104529Srwatson
1510104529Srwatsonint
1511100979Srwatsonmac_check_vnode_lookup(struct ucred *cred, struct vnode *dvp,
1512100979Srwatson    struct componentname *cnp)
1513100979Srwatson{
1514100979Srwatson	int error;
1515100979Srwatson
1516100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_lookup");
1517100979Srwatson
1518100979Srwatson	if (!mac_enforce_fs)
1519100979Srwatson		return (0);
1520100979Srwatson
1521100979Srwatson	MAC_CHECK(check_vnode_lookup, cred, dvp, &dvp->v_label, cnp);
1522100979Srwatson	return (error);
1523100979Srwatson}
1524100979Srwatson
1525104546Srwatsonint
1526104546Srwatsonmac_check_vnode_mmap(struct ucred *cred, struct vnode *vp, int prot)
1527100979Srwatson{
1528104546Srwatson	int error;
1529100979Srwatson
1530104546Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mmap");
1531103514Srwatson
1532104546Srwatson	if (!mac_enforce_fs || !mac_enforce_vm)
1533104546Srwatson		return (0);
1534104546Srwatson
1535104546Srwatson	MAC_CHECK(check_vnode_mmap, cred, vp, &vp->v_label, prot);
1536104546Srwatson	return (error);
1537100979Srwatson}
1538100979Srwatson
1539104546Srwatsonvoid
1540104546Srwatsonmac_check_vnode_mmap_downgrade(struct ucred *cred, struct vnode *vp, int *prot)
1541104546Srwatson{
1542104546Srwatson	int result = *prot;
1543104546Srwatson
1544104546Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mmap_downgrade");
1545104546Srwatson
1546104546Srwatson	if (!mac_enforce_fs || !mac_enforce_vm)
1547104546Srwatson		return;
1548104546Srwatson
1549104546Srwatson	MAC_PERFORM(check_vnode_mmap_downgrade, cred, vp, &vp->v_label,
1550104546Srwatson	    &result);
1551104546Srwatson
1552104546Srwatson	*prot = result;
1553104546Srwatson}
1554104546Srwatson
1555100979Srwatsonint
1556104546Srwatsonmac_check_vnode_mprotect(struct ucred *cred, struct vnode *vp, int prot)
1557104546Srwatson{
1558104546Srwatson	int error;
1559104546Srwatson
1560104546Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mprotect");
1561104546Srwatson
1562104546Srwatson	if (!mac_enforce_fs || !mac_enforce_vm)
1563104546Srwatson		return (0);
1564104546Srwatson
1565104546Srwatson	MAC_CHECK(check_vnode_mprotect, cred, vp, &vp->v_label, prot);
1566104546Srwatson	return (error);
1567104546Srwatson}
1568104546Srwatson
1569104546Srwatsonint
1570106212Srwatsonmac_check_vnode_open(struct ucred *cred, struct vnode *vp, int acc_mode)
1571100979Srwatson{
1572100979Srwatson	int error;
1573100979Srwatson
1574102112Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_open");
1575102112Srwatson
1576100979Srwatson	if (!mac_enforce_fs)
1577100979Srwatson		return (0);
1578100979Srwatson
1579102112Srwatson	MAC_CHECK(check_vnode_open, cred, vp, &vp->v_label, acc_mode);
1580102112Srwatson	return (error);
1581102112Srwatson}
1582102112Srwatson
1583102112Srwatsonint
1584102129Srwatsonmac_check_vnode_poll(struct ucred *active_cred, struct ucred *file_cred,
1585102129Srwatson    struct vnode *vp)
1586102112Srwatson{
1587102112Srwatson	int error;
1588102112Srwatson
1589102112Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_poll");
1590102112Srwatson
1591102112Srwatson	if (!mac_enforce_fs)
1592102112Srwatson		return (0);
1593102112Srwatson
1594102129Srwatson	MAC_CHECK(check_vnode_poll, active_cred, file_cred, vp,
1595102129Srwatson	    &vp->v_label);
1596100979Srwatson
1597100979Srwatson	return (error);
1598100979Srwatson}
1599100979Srwatson
1600100979Srwatsonint
1601102129Srwatsonmac_check_vnode_read(struct ucred *active_cred, struct ucred *file_cred,
1602102129Srwatson    struct vnode *vp)
1603100979Srwatson{
1604100979Srwatson	int error;
1605100979Srwatson
1606102112Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_read");
1607100979Srwatson
1608100979Srwatson	if (!mac_enforce_fs)
1609100979Srwatson		return (0);
1610100979Srwatson
1611102129Srwatson	MAC_CHECK(check_vnode_read, active_cred, file_cred, vp,
1612102129Srwatson	    &vp->v_label);
1613102112Srwatson
1614100979Srwatson	return (error);
1615100979Srwatson}
1616100979Srwatson
1617100979Srwatsonint
1618100979Srwatsonmac_check_vnode_readdir(struct ucred *cred, struct vnode *dvp)
1619100979Srwatson{
1620100979Srwatson	int error;
1621100979Srwatson
1622100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_readdir");
1623100979Srwatson
1624100979Srwatson	if (!mac_enforce_fs)
1625100979Srwatson		return (0);
1626100979Srwatson
1627100979Srwatson	MAC_CHECK(check_vnode_readdir, cred, dvp, &dvp->v_label);
1628100979Srwatson	return (error);
1629100979Srwatson}
1630100979Srwatson
1631100979Srwatsonint
1632100979Srwatsonmac_check_vnode_readlink(struct ucred *cred, struct vnode *vp)
1633100979Srwatson{
1634100979Srwatson	int error;
1635100979Srwatson
1636100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_readlink");
1637100979Srwatson
1638100979Srwatson	if (!mac_enforce_fs)
1639100979Srwatson		return (0);
1640100979Srwatson
1641100979Srwatson	MAC_CHECK(check_vnode_readlink, cred, vp, &vp->v_label);
1642100979Srwatson	return (error);
1643100979Srwatson}
1644100979Srwatson
1645100979Srwatsonstatic int
1646100979Srwatsonmac_check_vnode_relabel(struct ucred *cred, struct vnode *vp,
1647100979Srwatson    struct label *newlabel)
1648100979Srwatson{
1649100979Srwatson	int error;
1650100979Srwatson
1651100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_relabel");
1652100979Srwatson
1653100979Srwatson	MAC_CHECK(check_vnode_relabel, cred, vp, &vp->v_label, newlabel);
1654100979Srwatson
1655100979Srwatson	return (error);
1656100979Srwatson}
1657100979Srwatson
1658100979Srwatsonint
1659100979Srwatsonmac_check_vnode_rename_from(struct ucred *cred, struct vnode *dvp,
1660100979Srwatson    struct vnode *vp, struct componentname *cnp)
1661100979Srwatson{
1662100979Srwatson	int error;
1663100979Srwatson
1664100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_rename_from");
1665100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_rename_from");
1666100979Srwatson
1667100979Srwatson	if (!mac_enforce_fs)
1668100979Srwatson		return (0);
1669100979Srwatson
1670100979Srwatson	MAC_CHECK(check_vnode_rename_from, cred, dvp, &dvp->v_label, vp,
1671100979Srwatson	    &vp->v_label, cnp);
1672100979Srwatson	return (error);
1673100979Srwatson}
1674100979Srwatson
1675100979Srwatsonint
1676100979Srwatsonmac_check_vnode_rename_to(struct ucred *cred, struct vnode *dvp,
1677100979Srwatson    struct vnode *vp, int samedir, struct componentname *cnp)
1678100979Srwatson{
1679100979Srwatson	int error;
1680100979Srwatson
1681100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_rename_to");
1682100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_rename_to");
1683100979Srwatson
1684100979Srwatson	if (!mac_enforce_fs)
1685100979Srwatson		return (0);
1686100979Srwatson
1687100979Srwatson	MAC_CHECK(check_vnode_rename_to, cred, dvp, &dvp->v_label, vp,
1688100979Srwatson	    vp != NULL ? &vp->v_label : NULL, samedir, cnp);
1689100979Srwatson	return (error);
1690100979Srwatson}
1691100979Srwatson
1692100979Srwatsonint
1693100979Srwatsonmac_check_vnode_revoke(struct ucred *cred, struct vnode *vp)
1694100979Srwatson{
1695100979Srwatson	int error;
1696100979Srwatson
1697100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_revoke");
1698100979Srwatson
1699100979Srwatson	if (!mac_enforce_fs)
1700100979Srwatson		return (0);
1701100979Srwatson
1702100979Srwatson	MAC_CHECK(check_vnode_revoke, cred, vp, &vp->v_label);
1703100979Srwatson	return (error);
1704100979Srwatson}
1705100979Srwatson
1706100979Srwatsonint
1707100979Srwatsonmac_check_vnode_setacl(struct ucred *cred, struct vnode *vp, acl_type_t type,
1708100979Srwatson    struct acl *acl)
1709100979Srwatson{
1710100979Srwatson	int error;
1711100979Srwatson
1712100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setacl");
1713100979Srwatson
1714100979Srwatson	if (!mac_enforce_fs)
1715100979Srwatson		return (0);
1716100979Srwatson
1717100979Srwatson	MAC_CHECK(check_vnode_setacl, cred, vp, &vp->v_label, type, acl);
1718100979Srwatson	return (error);
1719100979Srwatson}
1720100979Srwatson
1721100979Srwatsonint
1722100979Srwatsonmac_check_vnode_setextattr(struct ucred *cred, struct vnode *vp,
1723100979Srwatson    int attrnamespace, const char *name, struct uio *uio)
1724100979Srwatson{
1725100979Srwatson	int error;
1726100979Srwatson
1727100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setextattr");
1728100979Srwatson
1729100979Srwatson	if (!mac_enforce_fs)
1730100979Srwatson		return (0);
1731100979Srwatson
1732100979Srwatson	MAC_CHECK(check_vnode_setextattr, cred, vp, &vp->v_label,
1733100979Srwatson	    attrnamespace, name, uio);
1734100979Srwatson	return (error);
1735100979Srwatson}
1736100979Srwatson
1737100979Srwatsonint
1738100979Srwatsonmac_check_vnode_setflags(struct ucred *cred, struct vnode *vp, u_long flags)
1739100979Srwatson{
1740100979Srwatson	int error;
1741100979Srwatson
1742100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setflags");
1743100979Srwatson
1744100979Srwatson	if (!mac_enforce_fs)
1745100979Srwatson		return (0);
1746100979Srwatson
1747100979Srwatson	MAC_CHECK(check_vnode_setflags, cred, vp, &vp->v_label, flags);
1748100979Srwatson	return (error);
1749100979Srwatson}
1750100979Srwatson
1751100979Srwatsonint
1752100979Srwatsonmac_check_vnode_setmode(struct ucred *cred, struct vnode *vp, mode_t mode)
1753100979Srwatson{
1754100979Srwatson	int error;
1755100979Srwatson
1756100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setmode");
1757100979Srwatson
1758100979Srwatson	if (!mac_enforce_fs)
1759100979Srwatson		return (0);
1760100979Srwatson
1761100979Srwatson	MAC_CHECK(check_vnode_setmode, cred, vp, &vp->v_label, mode);
1762100979Srwatson	return (error);
1763100979Srwatson}
1764100979Srwatson
1765100979Srwatsonint
1766100979Srwatsonmac_check_vnode_setowner(struct ucred *cred, struct vnode *vp, uid_t uid,
1767100979Srwatson    gid_t gid)
1768100979Srwatson{
1769100979Srwatson	int error;
1770100979Srwatson
1771100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setowner");
1772100979Srwatson
1773100979Srwatson	if (!mac_enforce_fs)
1774100979Srwatson		return (0);
1775100979Srwatson
1776100979Srwatson	MAC_CHECK(check_vnode_setowner, cred, vp, &vp->v_label, uid, gid);
1777100979Srwatson	return (error);
1778100979Srwatson}
1779100979Srwatson
1780100979Srwatsonint
1781100979Srwatsonmac_check_vnode_setutimes(struct ucred *cred, struct vnode *vp,
1782100979Srwatson    struct timespec atime, struct timespec mtime)
1783100979Srwatson{
1784100979Srwatson	int error;
1785100979Srwatson
1786100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setutimes");
1787100979Srwatson
1788100979Srwatson	if (!mac_enforce_fs)
1789100979Srwatson		return (0);
1790100979Srwatson
1791100979Srwatson	MAC_CHECK(check_vnode_setutimes, cred, vp, &vp->v_label, atime,
1792100979Srwatson	    mtime);
1793100979Srwatson	return (error);
1794100979Srwatson}
1795100979Srwatson
1796100979Srwatsonint
1797102129Srwatsonmac_check_vnode_stat(struct ucred *active_cred, struct ucred *file_cred,
1798102129Srwatson    struct vnode *vp)
1799100979Srwatson{
1800100979Srwatson	int error;
1801100979Srwatson
1802100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_stat");
1803100979Srwatson
1804100979Srwatson	if (!mac_enforce_fs)
1805100979Srwatson		return (0);
1806100979Srwatson
1807102129Srwatson	MAC_CHECK(check_vnode_stat, active_cred, file_cred, vp,
1808102129Srwatson	    &vp->v_label);
1809100979Srwatson	return (error);
1810100979Srwatson}
1811100979Srwatson
1812102112Srwatsonint
1813102129Srwatsonmac_check_vnode_write(struct ucred *active_cred, struct ucred *file_cred,
1814102129Srwatson    struct vnode *vp)
1815102112Srwatson{
1816102112Srwatson	int error;
1817102112Srwatson
1818102112Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_write");
1819102112Srwatson
1820102112Srwatson	if (!mac_enforce_fs)
1821102112Srwatson		return (0);
1822102112Srwatson
1823102129Srwatson	MAC_CHECK(check_vnode_write, active_cred, file_cred, vp,
1824102129Srwatson	    &vp->v_label);
1825102112Srwatson
1826102112Srwatson	return (error);
1827102112Srwatson}
1828102112Srwatson
1829100979Srwatson/*
1830100979Srwatson * When relabeling a process, call out to the policies for the maximum
1831100979Srwatson * permission allowed for each object type we know about in its
1832100979Srwatson * memory space, and revoke access (in the least surprising ways we
1833100979Srwatson * know) when necessary.  The process lock is not held here.
1834100979Srwatson */
1835100979Srwatsonstatic void
1836100979Srwatsonmac_cred_mmapped_drop_perms(struct thread *td, struct ucred *cred)
1837100979Srwatson{
1838100979Srwatson
1839100979Srwatson	/* XXX freeze all other threads */
1840100979Srwatson	mac_cred_mmapped_drop_perms_recurse(td, cred,
1841100979Srwatson	    &td->td_proc->p_vmspace->vm_map);
1842100979Srwatson	/* XXX allow other threads to continue */
1843100979Srwatson}
1844100979Srwatson
1845100979Srwatsonstatic __inline const char *
1846100979Srwatsonprot2str(vm_prot_t prot)
1847100979Srwatson{
1848100979Srwatson
1849100979Srwatson	switch (prot & VM_PROT_ALL) {
1850100979Srwatson	case VM_PROT_READ:
1851100979Srwatson		return ("r--");
1852100979Srwatson	case VM_PROT_READ | VM_PROT_WRITE:
1853100979Srwatson		return ("rw-");
1854100979Srwatson	case VM_PROT_READ | VM_PROT_EXECUTE:
1855100979Srwatson		return ("r-x");
1856100979Srwatson	case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
1857100979Srwatson		return ("rwx");
1858100979Srwatson	case VM_PROT_WRITE:
1859100979Srwatson		return ("-w-");
1860100979Srwatson	case VM_PROT_EXECUTE:
1861100979Srwatson		return ("--x");
1862100979Srwatson	case VM_PROT_WRITE | VM_PROT_EXECUTE:
1863100979Srwatson		return ("-wx");
1864100979Srwatson	default:
1865100979Srwatson		return ("---");
1866100979Srwatson	}
1867100979Srwatson}
1868100979Srwatson
1869100979Srwatsonstatic void
1870100979Srwatsonmac_cred_mmapped_drop_perms_recurse(struct thread *td, struct ucred *cred,
1871100979Srwatson    struct vm_map *map)
1872100979Srwatson{
1873100979Srwatson	struct vm_map_entry *vme;
1874104546Srwatson	int result;
1875104546Srwatson	vm_prot_t revokeperms;
1876100979Srwatson	vm_object_t object;
1877100979Srwatson	vm_ooffset_t offset;
1878100979Srwatson	struct vnode *vp;
1879100979Srwatson
1880103136Srwatson	if (!mac_mmap_revocation)
1881103136Srwatson		return;
1882103136Srwatson
1883100979Srwatson	vm_map_lock_read(map);
1884100979Srwatson	for (vme = map->header.next; vme != &map->header; vme = vme->next) {
1885100979Srwatson		if (vme->eflags & MAP_ENTRY_IS_SUB_MAP) {
1886100979Srwatson			mac_cred_mmapped_drop_perms_recurse(td, cred,
1887100979Srwatson			    vme->object.sub_map);
1888100979Srwatson			continue;
1889100979Srwatson		}
1890100979Srwatson		/*
1891100979Srwatson		 * Skip over entries that obviously are not shared.
1892100979Srwatson		 */
1893100979Srwatson		if (vme->eflags & (MAP_ENTRY_COW | MAP_ENTRY_NOSYNC) ||
1894100979Srwatson		    !vme->max_protection)
1895100979Srwatson			continue;
1896100979Srwatson		/*
1897100979Srwatson		 * Drill down to the deepest backing object.
1898100979Srwatson		 */
1899100979Srwatson		offset = vme->offset;
1900100979Srwatson		object = vme->object.vm_object;
1901100979Srwatson		if (object == NULL)
1902100979Srwatson			continue;
1903100979Srwatson		while (object->backing_object != NULL) {
1904100979Srwatson			object = object->backing_object;
1905100979Srwatson			offset += object->backing_object_offset;
1906100979Srwatson		}
1907100979Srwatson		/*
1908100979Srwatson		 * At the moment, vm_maps and objects aren't considered
1909100979Srwatson		 * by the MAC system, so only things with backing by a
1910100979Srwatson		 * normal object (read: vnodes) are checked.
1911100979Srwatson		 */
1912100979Srwatson		if (object->type != OBJT_VNODE)
1913100979Srwatson			continue;
1914100979Srwatson		vp = (struct vnode *)object->handle;
1915100979Srwatson		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1916104546Srwatson		result = vme->max_protection;
1917104546Srwatson		mac_check_vnode_mmap_downgrade(cred, vp, &result);
1918100979Srwatson		VOP_UNLOCK(vp, 0, td);
1919100979Srwatson		/*
1920100979Srwatson		 * Find out what maximum protection we may be allowing
1921100979Srwatson		 * now but a policy needs to get removed.
1922100979Srwatson		 */
1923100979Srwatson		revokeperms = vme->max_protection & ~result;
1924100979Srwatson		if (!revokeperms)
1925100979Srwatson			continue;
1926102949Sbde		printf("pid %ld: revoking %s perms from %#lx:%ld "
1927102949Sbde		    "(max %s/cur %s)\n", (long)td->td_proc->p_pid,
1928102949Sbde		    prot2str(revokeperms), (u_long)vme->start,
1929102949Sbde		    (long)(vme->end - vme->start),
1930100979Srwatson		    prot2str(vme->max_protection), prot2str(vme->protection));
1931100979Srwatson		vm_map_lock_upgrade(map);
1932100979Srwatson		/*
1933100979Srwatson		 * This is the really simple case: if a map has more
1934100979Srwatson		 * max_protection than is allowed, but it's not being
1935100979Srwatson		 * actually used (that is, the current protection is
1936100979Srwatson		 * still allowed), we can just wipe it out and do
1937100979Srwatson		 * nothing more.
1938100979Srwatson		 */
1939100979Srwatson		if ((vme->protection & revokeperms) == 0) {
1940100979Srwatson			vme->max_protection -= revokeperms;
1941100979Srwatson		} else {
1942100979Srwatson			if (revokeperms & VM_PROT_WRITE) {
1943100979Srwatson				/*
1944100979Srwatson				 * In the more complicated case, flush out all
1945100979Srwatson				 * pending changes to the object then turn it
1946100979Srwatson				 * copy-on-write.
1947100979Srwatson				 */
1948100979Srwatson				vm_object_reference(object);
1949100979Srwatson				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1950100979Srwatson				vm_object_page_clean(object,
1951100979Srwatson				    OFF_TO_IDX(offset),
1952100979Srwatson				    OFF_TO_IDX(offset + vme->end - vme->start +
1953100979Srwatson					PAGE_MASK),
1954100979Srwatson				    OBJPC_SYNC);
1955100979Srwatson				VOP_UNLOCK(vp, 0, td);
1956100979Srwatson				vm_object_deallocate(object);
1957100979Srwatson				/*
1958100979Srwatson				 * Why bother if there's no read permissions
1959100979Srwatson				 * anymore?  For the rest, we need to leave
1960100979Srwatson				 * the write permissions on for COW, or
1961100979Srwatson				 * remove them entirely if configured to.
1962100979Srwatson				 */
1963100979Srwatson				if (!mac_mmap_revocation_via_cow) {
1964100979Srwatson					vme->max_protection &= ~VM_PROT_WRITE;
1965100979Srwatson					vme->protection &= ~VM_PROT_WRITE;
1966100979Srwatson				} if ((revokeperms & VM_PROT_READ) == 0)
1967100979Srwatson					vme->eflags |= MAP_ENTRY_COW |
1968100979Srwatson					    MAP_ENTRY_NEEDS_COPY;
1969100979Srwatson			}
1970100979Srwatson			if (revokeperms & VM_PROT_EXECUTE) {
1971100979Srwatson				vme->max_protection &= ~VM_PROT_EXECUTE;
1972100979Srwatson				vme->protection &= ~VM_PROT_EXECUTE;
1973100979Srwatson			}
1974100979Srwatson			if (revokeperms & VM_PROT_READ) {
1975100979Srwatson				vme->max_protection = 0;
1976100979Srwatson				vme->protection = 0;
1977100979Srwatson			}
1978100979Srwatson			pmap_protect(map->pmap, vme->start, vme->end,
1979100979Srwatson			    vme->protection & ~revokeperms);
1980100979Srwatson			vm_map_simplify_entry(map, vme);
1981100979Srwatson		}
1982100979Srwatson		vm_map_lock_downgrade(map);
1983100979Srwatson	}
1984100979Srwatson	vm_map_unlock_read(map);
1985100979Srwatson}
1986100979Srwatson
1987100979Srwatson/*
1988100979Srwatson * When the subject's label changes, it may require revocation of privilege
1989100979Srwatson * to mapped objects.  This can't be done on-the-fly later with a unified
1990100979Srwatson * buffer cache.
1991100979Srwatson */
1992100979Srwatsonstatic void
1993100979Srwatsonmac_relabel_cred(struct ucred *cred, struct label *newlabel)
1994100979Srwatson{
1995100979Srwatson
1996100979Srwatson	MAC_PERFORM(relabel_cred, cred, newlabel);
1997100979Srwatson}
1998100979Srwatson
1999100979Srwatsonvoid
2000100979Srwatsonmac_relabel_vnode(struct ucred *cred, struct vnode *vp, struct label *newlabel)
2001100979Srwatson{
2002100979Srwatson
2003100979Srwatson	MAC_PERFORM(relabel_vnode, cred, vp, &vp->v_label, newlabel);
2004100979Srwatson}
2005100979Srwatson
2006100979Srwatsonvoid
2007100979Srwatsonmac_create_ifnet(struct ifnet *ifnet)
2008100979Srwatson{
2009100979Srwatson
2010100979Srwatson	MAC_PERFORM(create_ifnet, ifnet, &ifnet->if_label);
2011100979Srwatson}
2012100979Srwatson
2013100979Srwatsonvoid
2014100979Srwatsonmac_create_bpfdesc(struct ucred *cred, struct bpf_d *bpf_d)
2015100979Srwatson{
2016100979Srwatson
2017100979Srwatson	MAC_PERFORM(create_bpfdesc, cred, bpf_d, &bpf_d->bd_label);
2018100979Srwatson}
2019100979Srwatson
2020100979Srwatsonvoid
2021100979Srwatsonmac_create_socket(struct ucred *cred, struct socket *socket)
2022100979Srwatson{
2023100979Srwatson
2024100979Srwatson	MAC_PERFORM(create_socket, cred, socket, &socket->so_label);
2025100979Srwatson}
2026100979Srwatson
2027100979Srwatsonvoid
2028100979Srwatsonmac_create_pipe(struct ucred *cred, struct pipe *pipe)
2029100979Srwatson{
2030100979Srwatson
2031100979Srwatson	MAC_PERFORM(create_pipe, cred, pipe, pipe->pipe_label);
2032100979Srwatson}
2033100979Srwatson
2034100979Srwatsonvoid
2035100979Srwatsonmac_create_socket_from_socket(struct socket *oldsocket,
2036100979Srwatson    struct socket *newsocket)
2037100979Srwatson{
2038100979Srwatson
2039100979Srwatson	MAC_PERFORM(create_socket_from_socket, oldsocket, &oldsocket->so_label,
2040100979Srwatson	    newsocket, &newsocket->so_label);
2041100979Srwatson}
2042100979Srwatson
2043100979Srwatsonstatic void
2044100979Srwatsonmac_relabel_socket(struct ucred *cred, struct socket *socket,
2045100979Srwatson    struct label *newlabel)
2046100979Srwatson{
2047100979Srwatson
2048100979Srwatson	MAC_PERFORM(relabel_socket, cred, socket, &socket->so_label, newlabel);
2049100979Srwatson}
2050100979Srwatson
2051100979Srwatsonstatic void
2052100979Srwatsonmac_relabel_pipe(struct ucred *cred, struct pipe *pipe, struct label *newlabel)
2053100979Srwatson{
2054100979Srwatson
2055100979Srwatson	MAC_PERFORM(relabel_pipe, cred, pipe, pipe->pipe_label, newlabel);
2056100979Srwatson}
2057100979Srwatson
2058100979Srwatsonvoid
2059100979Srwatsonmac_set_socket_peer_from_mbuf(struct mbuf *mbuf, struct socket *socket)
2060100979Srwatson{
2061100979Srwatson
2062100979Srwatson	MAC_PERFORM(set_socket_peer_from_mbuf, mbuf, &mbuf->m_pkthdr.label,
2063100979Srwatson	    socket, &socket->so_peerlabel);
2064100979Srwatson}
2065100979Srwatson
2066100979Srwatsonvoid
2067100979Srwatsonmac_set_socket_peer_from_socket(struct socket *oldsocket,
2068100979Srwatson    struct socket *newsocket)
2069100979Srwatson{
2070100979Srwatson
2071100979Srwatson	MAC_PERFORM(set_socket_peer_from_socket, oldsocket,
2072100979Srwatson	    &oldsocket->so_label, newsocket, &newsocket->so_peerlabel);
2073100979Srwatson}
2074100979Srwatson
2075100979Srwatsonvoid
2076100979Srwatsonmac_create_datagram_from_ipq(struct ipq *ipq, struct mbuf *datagram)
2077100979Srwatson{
2078100979Srwatson
2079100979Srwatson	MAC_PERFORM(create_datagram_from_ipq, ipq, &ipq->ipq_label,
2080100979Srwatson	    datagram, &datagram->m_pkthdr.label);
2081100979Srwatson}
2082100979Srwatson
2083100979Srwatsonvoid
2084100979Srwatsonmac_create_fragment(struct mbuf *datagram, struct mbuf *fragment)
2085100979Srwatson{
2086100979Srwatson
2087100979Srwatson	MAC_PERFORM(create_fragment, datagram, &datagram->m_pkthdr.label,
2088100979Srwatson	    fragment, &fragment->m_pkthdr.label);
2089100979Srwatson}
2090100979Srwatson
2091100979Srwatsonvoid
2092100979Srwatsonmac_create_ipq(struct mbuf *fragment, struct ipq *ipq)
2093100979Srwatson{
2094100979Srwatson
2095100979Srwatson	MAC_PERFORM(create_ipq, fragment, &fragment->m_pkthdr.label, ipq,
2096100979Srwatson	    &ipq->ipq_label);
2097100979Srwatson}
2098100979Srwatson
2099100979Srwatsonvoid
2100100979Srwatsonmac_create_mbuf_from_mbuf(struct mbuf *oldmbuf, struct mbuf *newmbuf)
2101100979Srwatson{
2102100979Srwatson
2103100979Srwatson	MAC_PERFORM(create_mbuf_from_mbuf, oldmbuf, &oldmbuf->m_pkthdr.label,
2104100979Srwatson	    newmbuf, &newmbuf->m_pkthdr.label);
2105100979Srwatson}
2106100979Srwatson
2107100979Srwatsonvoid
2108100979Srwatsonmac_create_mbuf_from_bpfdesc(struct bpf_d *bpf_d, struct mbuf *mbuf)
2109100979Srwatson{
2110100979Srwatson
2111100979Srwatson	MAC_PERFORM(create_mbuf_from_bpfdesc, bpf_d, &bpf_d->bd_label, mbuf,
2112100979Srwatson	    &mbuf->m_pkthdr.label);
2113100979Srwatson}
2114100979Srwatson
2115100979Srwatsonvoid
2116100979Srwatsonmac_create_mbuf_linklayer(struct ifnet *ifnet, struct mbuf *mbuf)
2117100979Srwatson{
2118100979Srwatson
2119100979Srwatson	MAC_PERFORM(create_mbuf_linklayer, ifnet, &ifnet->if_label, mbuf,
2120100979Srwatson	    &mbuf->m_pkthdr.label);
2121100979Srwatson}
2122100979Srwatson
2123100979Srwatsonvoid
2124100979Srwatsonmac_create_mbuf_from_ifnet(struct ifnet *ifnet, struct mbuf *mbuf)
2125100979Srwatson{
2126100979Srwatson
2127100979Srwatson	MAC_PERFORM(create_mbuf_from_ifnet, ifnet, &ifnet->if_label, mbuf,
2128100979Srwatson	    &mbuf->m_pkthdr.label);
2129100979Srwatson}
2130100979Srwatson
2131100979Srwatsonvoid
2132100979Srwatsonmac_create_mbuf_multicast_encap(struct mbuf *oldmbuf, struct ifnet *ifnet,
2133100979Srwatson    struct mbuf *newmbuf)
2134100979Srwatson{
2135100979Srwatson
2136100979Srwatson	MAC_PERFORM(create_mbuf_multicast_encap, oldmbuf,
2137100979Srwatson	    &oldmbuf->m_pkthdr.label, ifnet, &ifnet->if_label, newmbuf,
2138100979Srwatson	    &newmbuf->m_pkthdr.label);
2139100979Srwatson}
2140100979Srwatson
2141100979Srwatsonvoid
2142100979Srwatsonmac_create_mbuf_netlayer(struct mbuf *oldmbuf, struct mbuf *newmbuf)
2143100979Srwatson{
2144100979Srwatson
2145100979Srwatson	MAC_PERFORM(create_mbuf_netlayer, oldmbuf, &oldmbuf->m_pkthdr.label,
2146100979Srwatson	    newmbuf, &newmbuf->m_pkthdr.label);
2147100979Srwatson}
2148100979Srwatson
2149100979Srwatsonint
2150100979Srwatsonmac_fragment_match(struct mbuf *fragment, struct ipq *ipq)
2151100979Srwatson{
2152100979Srwatson	int result;
2153100979Srwatson
2154100979Srwatson	result = 1;
2155100979Srwatson	MAC_BOOLEAN(fragment_match, &&, fragment, &fragment->m_pkthdr.label,
2156100979Srwatson	    ipq, &ipq->ipq_label);
2157100979Srwatson
2158100979Srwatson	return (result);
2159100979Srwatson}
2160100979Srwatson
2161100979Srwatsonvoid
2162100979Srwatsonmac_update_ipq(struct mbuf *fragment, struct ipq *ipq)
2163100979Srwatson{
2164100979Srwatson
2165100979Srwatson	MAC_PERFORM(update_ipq, fragment, &fragment->m_pkthdr.label, ipq,
2166100979Srwatson	    &ipq->ipq_label);
2167100979Srwatson}
2168100979Srwatson
2169100979Srwatsonvoid
2170100979Srwatsonmac_create_mbuf_from_socket(struct socket *socket, struct mbuf *mbuf)
2171100979Srwatson{
2172100979Srwatson
2173100979Srwatson	MAC_PERFORM(create_mbuf_from_socket, socket, &socket->so_label, mbuf,
2174100979Srwatson	    &mbuf->m_pkthdr.label);
2175100979Srwatson}
2176100979Srwatson
2177100979Srwatsonvoid
2178100979Srwatsonmac_create_mount(struct ucred *cred, struct mount *mp)
2179100979Srwatson{
2180100979Srwatson
2181100979Srwatson	MAC_PERFORM(create_mount, cred, mp, &mp->mnt_mntlabel,
2182100979Srwatson	    &mp->mnt_fslabel);
2183100979Srwatson}
2184100979Srwatson
2185100979Srwatsonvoid
2186100979Srwatsonmac_create_root_mount(struct ucred *cred, struct mount *mp)
2187100979Srwatson{
2188100979Srwatson
2189100979Srwatson	MAC_PERFORM(create_root_mount, cred, mp, &mp->mnt_mntlabel,
2190100979Srwatson	    &mp->mnt_fslabel);
2191100979Srwatson}
2192100979Srwatson
2193100979Srwatsonint
2194100979Srwatsonmac_check_bpfdesc_receive(struct bpf_d *bpf_d, struct ifnet *ifnet)
2195100979Srwatson{
2196100979Srwatson	int error;
2197100979Srwatson
2198100979Srwatson	if (!mac_enforce_network)
2199100979Srwatson		return (0);
2200100979Srwatson
2201100979Srwatson	MAC_CHECK(check_bpfdesc_receive, bpf_d, &bpf_d->bd_label, ifnet,
2202100979Srwatson	    &ifnet->if_label);
2203100979Srwatson
2204100979Srwatson	return (error);
2205100979Srwatson}
2206100979Srwatson
2207100979Srwatsonstatic int
2208100979Srwatsonmac_check_cred_relabel(struct ucred *cred, struct label *newlabel)
2209100979Srwatson{
2210100979Srwatson	int error;
2211100979Srwatson
2212100979Srwatson	MAC_CHECK(check_cred_relabel, cred, newlabel);
2213100979Srwatson
2214100979Srwatson	return (error);
2215100979Srwatson}
2216100979Srwatson
2217100979Srwatsonint
2218100979Srwatsonmac_check_cred_visible(struct ucred *u1, struct ucred *u2)
2219100979Srwatson{
2220100979Srwatson	int error;
2221100979Srwatson
2222100979Srwatson	if (!mac_enforce_process)
2223100979Srwatson		return (0);
2224100979Srwatson
2225100979Srwatson	MAC_CHECK(check_cred_visible, u1, u2);
2226100979Srwatson
2227100979Srwatson	return (error);
2228100979Srwatson}
2229100979Srwatson
2230100979Srwatsonint
2231100979Srwatsonmac_check_ifnet_transmit(struct ifnet *ifnet, struct mbuf *mbuf)
2232100979Srwatson{
2233100979Srwatson	int error;
2234100979Srwatson
2235100979Srwatson	if (!mac_enforce_network)
2236100979Srwatson		return (0);
2237100979Srwatson
2238100979Srwatson	KASSERT(mbuf->m_flags & M_PKTHDR, ("packet has no pkthdr"));
2239100979Srwatson	if (!(mbuf->m_pkthdr.label.l_flags & MAC_FLAG_INITIALIZED))
2240105598Sbrooks		if_printf(ifnet, "not initialized\n");
2241100979Srwatson
2242100979Srwatson	MAC_CHECK(check_ifnet_transmit, ifnet, &ifnet->if_label, mbuf,
2243100979Srwatson	    &mbuf->m_pkthdr.label);
2244100979Srwatson
2245100979Srwatson	return (error);
2246100979Srwatson}
2247100979Srwatson
2248100979Srwatsonint
2249106308Srwatsonmac_check_kenv_dump(struct ucred *cred)
2250106308Srwatson{
2251106308Srwatson	int error;
2252106308Srwatson
2253106308Srwatson	if (!mac_enforce_system)
2254106308Srwatson		return (0);
2255106308Srwatson
2256106308Srwatson	MAC_CHECK(check_kenv_dump, cred);
2257106308Srwatson
2258106308Srwatson	return (error);
2259106308Srwatson}
2260106308Srwatson
2261106308Srwatsonint
2262106308Srwatsonmac_check_kenv_get(struct ucred *cred, char *name)
2263106308Srwatson{
2264106308Srwatson	int error;
2265106308Srwatson
2266106308Srwatson	if (!mac_enforce_system)
2267106308Srwatson		return (0);
2268106308Srwatson
2269106308Srwatson	MAC_CHECK(check_kenv_get, cred, name);
2270106308Srwatson
2271106308Srwatson	return (error);
2272106308Srwatson}
2273106308Srwatson
2274106308Srwatsonint
2275106308Srwatsonmac_check_kenv_set(struct ucred *cred, char *name, char *value)
2276106308Srwatson{
2277106308Srwatson	int error;
2278106308Srwatson
2279106308Srwatson	if (!mac_enforce_system)
2280106308Srwatson		return (0);
2281106308Srwatson
2282106308Srwatson	MAC_CHECK(check_kenv_set, cred, name, value);
2283106308Srwatson
2284106308Srwatson	return (error);
2285106308Srwatson}
2286106308Srwatson
2287106308Srwatsonint
2288106308Srwatsonmac_check_kenv_unset(struct ucred *cred, char *name)
2289106308Srwatson{
2290106308Srwatson	int error;
2291106308Srwatson
2292106308Srwatson	if (!mac_enforce_system)
2293106308Srwatson		return (0);
2294106308Srwatson
2295106308Srwatson	MAC_CHECK(check_kenv_unset, cred, name);
2296106308Srwatson
2297106308Srwatson	return (error);
2298106308Srwatson}
2299106308Srwatson
2300106308Srwatsonint
2301107089Srwatsonmac_check_kld_load(struct ucred *cred, struct vnode *vp)
2302107089Srwatson{
2303107089Srwatson	int error;
2304107089Srwatson
2305107089Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_kld_load");
2306107089Srwatson
2307107089Srwatson	if (!mac_enforce_kld)
2308107089Srwatson		return (0);
2309107089Srwatson
2310107089Srwatson	MAC_CHECK(check_kld_load, cred, vp, &vp->v_label);
2311107089Srwatson
2312107089Srwatson	return (error);
2313107089Srwatson}
2314107089Srwatson
2315107089Srwatsonint
2316107089Srwatsonmac_check_kld_stat(struct ucred *cred)
2317107089Srwatson{
2318107089Srwatson	int error;
2319107089Srwatson
2320107089Srwatson	if (!mac_enforce_kld)
2321107089Srwatson		return (0);
2322107089Srwatson
2323107089Srwatson	MAC_CHECK(check_kld_stat, cred);
2324107089Srwatson
2325107089Srwatson	return (error);
2326107089Srwatson}
2327107089Srwatson
2328107089Srwatsonint
2329107089Srwatsonmac_check_kld_unload(struct ucred *cred)
2330107089Srwatson{
2331107089Srwatson	int error;
2332107089Srwatson
2333107089Srwatson	if (!mac_enforce_kld)
2334107089Srwatson		return (0);
2335107089Srwatson
2336107089Srwatson	MAC_CHECK(check_kld_unload, cred);
2337107089Srwatson
2338107089Srwatson	return (error);
2339107089Srwatson}
2340107089Srwatson
2341107089Srwatsonint
2342100979Srwatsonmac_check_mount_stat(struct ucred *cred, struct mount *mount)
2343100979Srwatson{
2344100979Srwatson	int error;
2345100979Srwatson
2346100979Srwatson	if (!mac_enforce_fs)
2347100979Srwatson		return (0);
2348100979Srwatson
2349100979Srwatson	MAC_CHECK(check_mount_stat, cred, mount, &mount->mnt_mntlabel);
2350100979Srwatson
2351100979Srwatson	return (error);
2352100979Srwatson}
2353100979Srwatson
2354100979Srwatsonint
2355100979Srwatsonmac_check_pipe_ioctl(struct ucred *cred, struct pipe *pipe, unsigned long cmd,
2356100979Srwatson    void *data)
2357100979Srwatson{
2358100979Srwatson	int error;
2359100979Srwatson
2360104269Srwatson	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2361104269Srwatson
2362104269Srwatson	if (!mac_enforce_pipe)
2363104269Srwatson		return (0);
2364104269Srwatson
2365100979Srwatson	MAC_CHECK(check_pipe_ioctl, cred, pipe, pipe->pipe_label, cmd, data);
2366100979Srwatson
2367100979Srwatson	return (error);
2368100979Srwatson}
2369100979Srwatson
2370100979Srwatsonint
2371102115Srwatsonmac_check_pipe_poll(struct ucred *cred, struct pipe *pipe)
2372100979Srwatson{
2373100979Srwatson	int error;
2374100979Srwatson
2375104269Srwatson	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2376104269Srwatson
2377104269Srwatson	if (!mac_enforce_pipe)
2378104269Srwatson		return (0);
2379104269Srwatson
2380102115Srwatson	MAC_CHECK(check_pipe_poll, cred, pipe, pipe->pipe_label);
2381100979Srwatson
2382100979Srwatson	return (error);
2383100979Srwatson}
2384100979Srwatson
2385102115Srwatsonint
2386102115Srwatsonmac_check_pipe_read(struct ucred *cred, struct pipe *pipe)
2387102115Srwatson{
2388102115Srwatson	int error;
2389102115Srwatson
2390104269Srwatson	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2391104269Srwatson
2392104269Srwatson	if (!mac_enforce_pipe)
2393104269Srwatson		return (0);
2394104269Srwatson
2395102115Srwatson	MAC_CHECK(check_pipe_read, cred, pipe, pipe->pipe_label);
2396102115Srwatson
2397102115Srwatson	return (error);
2398102115Srwatson}
2399102115Srwatson
2400100979Srwatsonstatic int
2401100979Srwatsonmac_check_pipe_relabel(struct ucred *cred, struct pipe *pipe,
2402100979Srwatson    struct label *newlabel)
2403100979Srwatson{
2404100979Srwatson	int error;
2405100979Srwatson
2406104269Srwatson	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2407104269Srwatson
2408104269Srwatson	if (!mac_enforce_pipe)
2409104269Srwatson		return (0);
2410104269Srwatson
2411100979Srwatson	MAC_CHECK(check_pipe_relabel, cred, pipe, pipe->pipe_label, newlabel);
2412100979Srwatson
2413100979Srwatson	return (error);
2414100979Srwatson}
2415100979Srwatson
2416100979Srwatsonint
2417102115Srwatsonmac_check_pipe_stat(struct ucred *cred, struct pipe *pipe)
2418102115Srwatson{
2419102115Srwatson	int error;
2420102115Srwatson
2421104269Srwatson	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2422104269Srwatson
2423104269Srwatson	if (!mac_enforce_pipe)
2424104269Srwatson		return (0);
2425104269Srwatson
2426102115Srwatson	MAC_CHECK(check_pipe_stat, cred, pipe, pipe->pipe_label);
2427102115Srwatson
2428102115Srwatson	return (error);
2429102115Srwatson}
2430102115Srwatson
2431102115Srwatsonint
2432102115Srwatsonmac_check_pipe_write(struct ucred *cred, struct pipe *pipe)
2433102115Srwatson{
2434102115Srwatson	int error;
2435102115Srwatson
2436104269Srwatson	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2437104269Srwatson
2438104269Srwatson	if (!mac_enforce_pipe)
2439104269Srwatson		return (0);
2440104269Srwatson
2441102115Srwatson	MAC_CHECK(check_pipe_write, cred, pipe, pipe->pipe_label);
2442102115Srwatson
2443102115Srwatson	return (error);
2444102115Srwatson}
2445102115Srwatson
2446102115Srwatsonint
2447100979Srwatsonmac_check_proc_debug(struct ucred *cred, struct proc *proc)
2448100979Srwatson{
2449100979Srwatson	int error;
2450100979Srwatson
2451102103Srwatson	PROC_LOCK_ASSERT(proc, MA_OWNED);
2452102103Srwatson
2453100979Srwatson	if (!mac_enforce_process)
2454100979Srwatson		return (0);
2455100979Srwatson
2456100979Srwatson	MAC_CHECK(check_proc_debug, cred, proc);
2457100979Srwatson
2458100979Srwatson	return (error);
2459100979Srwatson}
2460100979Srwatson
2461100979Srwatsonint
2462100979Srwatsonmac_check_proc_sched(struct ucred *cred, struct proc *proc)
2463100979Srwatson{
2464100979Srwatson	int error;
2465100979Srwatson
2466102103Srwatson	PROC_LOCK_ASSERT(proc, MA_OWNED);
2467102103Srwatson
2468100979Srwatson	if (!mac_enforce_process)
2469100979Srwatson		return (0);
2470100979Srwatson
2471100979Srwatson	MAC_CHECK(check_proc_sched, cred, proc);
2472100979Srwatson
2473100979Srwatson	return (error);
2474100979Srwatson}
2475100979Srwatson
2476100979Srwatsonint
2477100979Srwatsonmac_check_proc_signal(struct ucred *cred, struct proc *proc, int signum)
2478100979Srwatson{
2479100979Srwatson	int error;
2480100979Srwatson
2481102103Srwatson	PROC_LOCK_ASSERT(proc, MA_OWNED);
2482102103Srwatson
2483100979Srwatson	if (!mac_enforce_process)
2484100979Srwatson		return (0);
2485100979Srwatson
2486100979Srwatson	MAC_CHECK(check_proc_signal, cred, proc, signum);
2487100979Srwatson
2488100979Srwatson	return (error);
2489100979Srwatson}
2490100979Srwatson
2491100979Srwatsonint
2492100979Srwatsonmac_check_socket_bind(struct ucred *ucred, struct socket *socket,
2493100979Srwatson    struct sockaddr *sockaddr)
2494100979Srwatson{
2495100979Srwatson	int error;
2496100979Srwatson
2497100979Srwatson	if (!mac_enforce_socket)
2498100979Srwatson		return (0);
2499100979Srwatson
2500100979Srwatson	MAC_CHECK(check_socket_bind, ucred, socket, &socket->so_label,
2501100979Srwatson	    sockaddr);
2502100979Srwatson
2503100979Srwatson	return (error);
2504100979Srwatson}
2505100979Srwatson
2506100979Srwatsonint
2507100979Srwatsonmac_check_socket_connect(struct ucred *cred, struct socket *socket,
2508100979Srwatson    struct sockaddr *sockaddr)
2509100979Srwatson{
2510100979Srwatson	int error;
2511100979Srwatson
2512100979Srwatson	if (!mac_enforce_socket)
2513100979Srwatson		return (0);
2514100979Srwatson
2515100979Srwatson	MAC_CHECK(check_socket_connect, cred, socket, &socket->so_label,
2516100979Srwatson	    sockaddr);
2517100979Srwatson
2518100979Srwatson	return (error);
2519100979Srwatson}
2520100979Srwatson
2521100979Srwatsonint
2522101933Srwatsonmac_check_socket_deliver(struct socket *socket, struct mbuf *mbuf)
2523100979Srwatson{
2524100979Srwatson	int error;
2525100979Srwatson
2526100979Srwatson	if (!mac_enforce_socket)
2527100979Srwatson		return (0);
2528100979Srwatson
2529101933Srwatson	MAC_CHECK(check_socket_deliver, socket, &socket->so_label, mbuf,
2530101933Srwatson	    &mbuf->m_pkthdr.label);
2531101933Srwatson
2532100979Srwatson	return (error);
2533100979Srwatson}
2534100979Srwatson
2535100979Srwatsonint
2536101933Srwatsonmac_check_socket_listen(struct ucred *cred, struct socket *socket)
2537100979Srwatson{
2538100979Srwatson	int error;
2539100979Srwatson
2540100979Srwatson	if (!mac_enforce_socket)
2541100979Srwatson		return (0);
2542100979Srwatson
2543101933Srwatson	MAC_CHECK(check_socket_listen, cred, socket, &socket->so_label);
2544100979Srwatson	return (error);
2545100979Srwatson}
2546100979Srwatson
2547104571Srwatsonint
2548104571Srwatsonmac_check_socket_receive(struct ucred *cred, struct socket *so)
2549104571Srwatson{
2550104571Srwatson	int error;
2551104571Srwatson
2552104571Srwatson	if (!mac_enforce_socket)
2553104571Srwatson		return (0);
2554104571Srwatson
2555104571Srwatson	MAC_CHECK(check_socket_receive, cred, so, &so->so_label);
2556104571Srwatson
2557104571Srwatson	return (error);
2558104571Srwatson}
2559104571Srwatson
2560100979Srwatsonstatic int
2561100979Srwatsonmac_check_socket_relabel(struct ucred *cred, struct socket *socket,
2562100979Srwatson    struct label *newlabel)
2563100979Srwatson{
2564100979Srwatson	int error;
2565100979Srwatson
2566100979Srwatson	MAC_CHECK(check_socket_relabel, cred, socket, &socket->so_label,
2567100979Srwatson	    newlabel);
2568100979Srwatson
2569100979Srwatson	return (error);
2570100979Srwatson}
2571100979Srwatson
2572100979Srwatsonint
2573104571Srwatsonmac_check_socket_send(struct ucred *cred, struct socket *so)
2574104571Srwatson{
2575104571Srwatson	int error;
2576104571Srwatson
2577104571Srwatson	if (!mac_enforce_socket)
2578104571Srwatson		return (0);
2579104571Srwatson
2580104571Srwatson	MAC_CHECK(check_socket_send, cred, so, &so->so_label);
2581104571Srwatson
2582104571Srwatson	return (error);
2583104571Srwatson}
2584104571Srwatson
2585104571Srwatsonint
2586100979Srwatsonmac_check_socket_visible(struct ucred *cred, struct socket *socket)
2587100979Srwatson{
2588100979Srwatson	int error;
2589100979Srwatson
2590100979Srwatson	if (!mac_enforce_socket)
2591100979Srwatson		return (0);
2592105694Srwatson
2593100979Srwatson	MAC_CHECK(check_socket_visible, cred, socket, &socket->so_label);
2594105694Srwatson
2595100979Srwatson	return (error);
2596100979Srwatson}
2597100979Srwatson
2598100979Srwatsonint
2599106412Srwatsonmac_check_system_acct(struct ucred *cred, struct vnode *vp)
2600106412Srwatson{
2601106412Srwatson	int error;
2602106412Srwatson
2603106412Srwatson	if (vp != NULL) {
2604106412Srwatson		ASSERT_VOP_LOCKED(vp, "mac_check_system_acct");
2605106412Srwatson	}
2606106412Srwatson
2607106412Srwatson	if (!mac_enforce_system)
2608106412Srwatson		return (0);
2609106412Srwatson
2610106412Srwatson	MAC_CHECK(check_system_acct, cred, vp,
2611106412Srwatson	    vp != NULL ? &vp->v_label : NULL);
2612106412Srwatson
2613106412Srwatson	return (error);
2614106412Srwatson}
2615106412Srwatson
2616106412Srwatsonint
2617106412Srwatsonmac_check_system_nfsd(struct ucred *cred)
2618106412Srwatson{
2619106412Srwatson	int error;
2620106412Srwatson
2621106412Srwatson	if (!mac_enforce_system)
2622106412Srwatson		return (0);
2623106412Srwatson
2624106412Srwatson	MAC_CHECK(check_system_nfsd, cred);
2625106412Srwatson
2626106412Srwatson	return (error);
2627106412Srwatson}
2628106412Srwatson
2629106412Srwatsonint
2630106024Srwatsonmac_check_system_reboot(struct ucred *cred, int howto)
2631106024Srwatson{
2632106024Srwatson	int error;
2633106024Srwatson
2634106045Srwatson	if (!mac_enforce_system)
2635106024Srwatson		return (0);
2636106024Srwatson
2637106024Srwatson	MAC_CHECK(check_system_reboot, cred, howto);
2638106045Srwatson
2639106024Srwatson	return (error);
2640106024Srwatson}
2641106024Srwatson
2642106024Srwatsonint
2643106369Srwatsonmac_check_system_settime(struct ucred *cred)
2644106369Srwatson{
2645106369Srwatson	int error;
2646106369Srwatson
2647106369Srwatson	if (!mac_enforce_system)
2648106369Srwatson		return (0);
2649106369Srwatson
2650106369Srwatson	MAC_CHECK(check_system_settime, cred);
2651106369Srwatson
2652106369Srwatson	return (error);
2653106369Srwatson}
2654106369Srwatson
2655106369Srwatsonint
2656106023Srwatsonmac_check_system_swapon(struct ucred *cred, struct vnode *vp)
2657106023Srwatson{
2658106023Srwatson	int error;
2659106023Srwatson
2660106023Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_system_swapon");
2661106023Srwatson
2662106045Srwatson	if (!mac_enforce_system)
2663106023Srwatson		return (0);
2664106023Srwatson
2665106023Srwatson	MAC_CHECK(check_system_swapon, cred, vp, &vp->v_label);
2666106023Srwatson	return (error);
2667106023Srwatson}
2668106023Srwatson
2669106023Srwatsonint
2670106025Srwatsonmac_check_system_sysctl(struct ucred *cred, int *name, u_int namelen,
2671106025Srwatson    void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen)
2672106025Srwatson{
2673106025Srwatson	int error;
2674106025Srwatson
2675106025Srwatson	/*
2676106025Srwatson	 * XXXMAC: We're very much like to assert the SYSCTL_LOCK here,
2677106025Srwatson	 * but since it's not exported from kern_sysctl.c, we can't.
2678106025Srwatson	 */
2679106045Srwatson	if (!mac_enforce_system)
2680106025Srwatson		return (0);
2681106025Srwatson
2682106025Srwatson	MAC_CHECK(check_system_sysctl, cred, name, namelen, old, oldlenp,
2683106025Srwatson	    inkernel, new, newlen);
2684106025Srwatson
2685106025Srwatson	return (error);
2686106025Srwatson}
2687106025Srwatson
2688106025Srwatsonint
2689100979Srwatsonmac_ioctl_ifnet_get(struct ucred *cred, struct ifreq *ifr,
2690100979Srwatson    struct ifnet *ifnet)
2691100979Srwatson{
2692105694Srwatson	char *elements, *buffer;
2693105694Srwatson	struct mac mac;
2694100979Srwatson	int error;
2695100979Srwatson
2696105694Srwatson	error = copyin(ifr->ifr_ifru.ifru_data, &mac, sizeof(mac));
2697100979Srwatson	if (error)
2698100979Srwatson		return (error);
2699100979Srwatson
2700105694Srwatson	error = mac_check_structmac_consistent(&mac);
2701105694Srwatson	if (error)
2702105694Srwatson		return (error);
2703105694Srwatson
2704105694Srwatson	elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
2705105694Srwatson	error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
2706105694Srwatson	if (error) {
2707105694Srwatson		free(elements, M_MACTEMP);
2708105694Srwatson		return (error);
2709105694Srwatson	}
2710105694Srwatson
2711105694Srwatson	buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
2712105694Srwatson	error = mac_externalize_ifnet_label(&ifnet->if_label, elements,
2713105694Srwatson	    buffer, mac.m_buflen, M_WAITOK);
2714105694Srwatson	if (error == 0)
2715105694Srwatson		error = copyout(buffer, mac.m_string, strlen(buffer)+1);
2716105694Srwatson
2717105694Srwatson	free(buffer, M_MACTEMP);
2718105694Srwatson	free(elements, M_MACTEMP);
2719105694Srwatson
2720105694Srwatson	return (error);
2721100979Srwatson}
2722100979Srwatson
2723100979Srwatsonint
2724100979Srwatsonmac_ioctl_ifnet_set(struct ucred *cred, struct ifreq *ifr,
2725100979Srwatson    struct ifnet *ifnet)
2726100979Srwatson{
2727100979Srwatson	struct label intlabel;
2728105694Srwatson	struct mac mac;
2729105694Srwatson	char *buffer;
2730100979Srwatson	int error;
2731100979Srwatson
2732105694Srwatson	error = copyin(ifr->ifr_ifru.ifru_data, &mac, sizeof(mac));
2733100979Srwatson	if (error)
2734100979Srwatson		return (error);
2735100979Srwatson
2736105694Srwatson	error = mac_check_structmac_consistent(&mac);
2737100979Srwatson	if (error)
2738100979Srwatson		return (error);
2739100979Srwatson
2740105694Srwatson	buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
2741105694Srwatson	error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
2742105694Srwatson	if (error) {
2743105694Srwatson		free(buffer, M_MACTEMP);
2744105694Srwatson		return (error);
2745105694Srwatson	}
2746105694Srwatson
2747105694Srwatson	mac_init_ifnet_label(&intlabel);
2748105694Srwatson	error = mac_internalize_ifnet_label(&intlabel, buffer);
2749105694Srwatson	free(buffer, M_MACTEMP);
2750105694Srwatson	if (error) {
2751105694Srwatson		mac_destroy_ifnet_label(&intlabel);
2752105694Srwatson		return (error);
2753105694Srwatson	}
2754105694Srwatson
2755100979Srwatson	/*
2756100979Srwatson	 * XXX: Note that this is a redundant privilege check, since
2757100979Srwatson	 * policies impose this check themselves if required by the
2758100979Srwatson	 * policy.  Eventually, this should go away.
2759100979Srwatson	 */
2760100979Srwatson	error = suser_cred(cred, 0);
2761105694Srwatson	if (error) {
2762105694Srwatson		mac_destroy_ifnet_label(&intlabel);
2763105694Srwatson		return (error);
2764105694Srwatson	}
2765100979Srwatson
2766100979Srwatson	MAC_CHECK(check_ifnet_relabel, cred, ifnet, &ifnet->if_label,
2767100979Srwatson	    &intlabel);
2768105694Srwatson	if (error) {
2769105694Srwatson		mac_destroy_ifnet_label(&intlabel);
2770105694Srwatson		return (error);
2771105694Srwatson	}
2772100979Srwatson
2773100979Srwatson	MAC_PERFORM(relabel_ifnet, cred, ifnet, &ifnet->if_label, &intlabel);
2774100979Srwatson
2775105694Srwatson	mac_destroy_ifnet_label(&intlabel);
2776105694Srwatson	return (0);
2777100979Srwatson}
2778100979Srwatson
2779100979Srwatsonvoid
2780100979Srwatsonmac_create_devfs_device(dev_t dev, struct devfs_dirent *de)
2781100979Srwatson{
2782100979Srwatson
2783100979Srwatson	MAC_PERFORM(create_devfs_device, dev, de, &de->de_label);
2784100979Srwatson}
2785100979Srwatson
2786104533Srwatsonvoid
2787104533Srwatsonmac_create_devfs_symlink(struct ucred *cred, struct devfs_dirent *dd,
2788104533Srwatson    struct devfs_dirent *de)
2789104533Srwatson{
2790104533Srwatson
2791104533Srwatson	MAC_PERFORM(create_devfs_symlink, cred, dd, &dd->de_label, de,
2792104533Srwatson	    &de->de_label);
2793104533Srwatson}
2794104533Srwatson
2795100979Srwatsonvoid
2796100979Srwatsonmac_create_devfs_directory(char *dirname, int dirnamelen,
2797100979Srwatson    struct devfs_dirent *de)
2798100979Srwatson{
2799100979Srwatson
2800100979Srwatson	MAC_PERFORM(create_devfs_directory, dirname, dirnamelen, de,
2801100979Srwatson	    &de->de_label);
2802100979Srwatson}
2803100979Srwatson
2804100979Srwatsonint
2805100979Srwatsonmac_setsockopt_label_set(struct ucred *cred, struct socket *so,
2806105694Srwatson    struct mac *mac)
2807100979Srwatson{
2808100979Srwatson	struct label intlabel;
2809105694Srwatson	char *buffer;
2810100979Srwatson	int error;
2811100979Srwatson
2812105694Srwatson	error = mac_check_structmac_consistent(mac);
2813100979Srwatson	if (error)
2814100979Srwatson		return (error);
2815100979Srwatson
2816105694Srwatson	buffer = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK);
2817105694Srwatson	error = copyinstr(mac->m_string, buffer, mac->m_buflen, NULL);
2818105694Srwatson	if (error) {
2819105694Srwatson		free(buffer, M_MACTEMP);
2820105694Srwatson		return (error);
2821105694Srwatson	}
2822105694Srwatson
2823105694Srwatson	mac_init_socket_label(&intlabel, M_WAITOK);
2824105694Srwatson	error = mac_internalize_socket_label(&intlabel, buffer);
2825105694Srwatson	free(buffer, M_MACTEMP);
2826105694Srwatson	if (error) {
2827105694Srwatson		mac_destroy_socket_label(&intlabel);
2828105694Srwatson		return (error);
2829105694Srwatson	}
2830105694Srwatson
2831100979Srwatson	mac_check_socket_relabel(cred, so, &intlabel);
2832100979Srwatson	if (error) {
2833105694Srwatson		mac_destroy_socket_label(&intlabel);
2834100979Srwatson		return (error);
2835100979Srwatson	}
2836100979Srwatson
2837100979Srwatson	mac_relabel_socket(cred, so, &intlabel);
2838100979Srwatson
2839105694Srwatson	mac_destroy_socket_label(&intlabel);
2840100979Srwatson	return (0);
2841100979Srwatson}
2842100979Srwatson
2843100979Srwatsonint
2844100979Srwatsonmac_pipe_label_set(struct ucred *cred, struct pipe *pipe, struct label *label)
2845100979Srwatson{
2846100979Srwatson	int error;
2847100979Srwatson
2848104269Srwatson	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2849104269Srwatson
2850100979Srwatson	error = mac_check_pipe_relabel(cred, pipe, label);
2851100979Srwatson	if (error)
2852100979Srwatson		return (error);
2853100979Srwatson
2854100979Srwatson	mac_relabel_pipe(cred, pipe, label);
2855100979Srwatson
2856100979Srwatson	return (0);
2857100979Srwatson}
2858100979Srwatson
2859100979Srwatsonint
2860100979Srwatsonmac_getsockopt_label_get(struct ucred *cred, struct socket *so,
2861105694Srwatson    struct mac *mac)
2862100979Srwatson{
2863105694Srwatson	char *buffer, *elements;
2864105694Srwatson	int error;
2865100979Srwatson
2866105694Srwatson	error = mac_check_structmac_consistent(mac);
2867105694Srwatson	if (error)
2868105694Srwatson		return (error);
2869105694Srwatson
2870105694Srwatson	elements = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK);
2871105694Srwatson	error = copyinstr(mac->m_string, elements, mac->m_buflen, NULL);
2872105694Srwatson	if (error) {
2873105694Srwatson		free(elements, M_MACTEMP);
2874105694Srwatson		return (error);
2875105694Srwatson	}
2876105694Srwatson
2877105694Srwatson	buffer = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
2878105694Srwatson	error = mac_externalize_socket_label(&so->so_label, elements,
2879105694Srwatson	    buffer, mac->m_buflen, M_WAITOK);
2880105694Srwatson	if (error == 0)
2881105694Srwatson		error = copyout(buffer, mac->m_string, strlen(buffer)+1);
2882105694Srwatson
2883105694Srwatson	free(buffer, M_MACTEMP);
2884105694Srwatson	free(elements, M_MACTEMP);
2885105694Srwatson
2886105694Srwatson	return (error);
2887100979Srwatson}
2888100979Srwatson
2889100979Srwatsonint
2890100979Srwatsonmac_getsockopt_peerlabel_get(struct ucred *cred, struct socket *so,
2891105694Srwatson    struct mac *mac)
2892100979Srwatson{
2893105694Srwatson	char *elements, *buffer;
2894105694Srwatson	int error;
2895100979Srwatson
2896105694Srwatson	error = mac_check_structmac_consistent(mac);
2897105694Srwatson	if (error)
2898105694Srwatson		return (error);
2899105694Srwatson
2900105694Srwatson	elements = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK);
2901105694Srwatson	error = copyinstr(mac->m_string, elements, mac->m_buflen, NULL);
2902105694Srwatson	if (error) {
2903105694Srwatson		free(elements, M_MACTEMP);
2904105694Srwatson		return (error);
2905105694Srwatson	}
2906105694Srwatson
2907105694Srwatson	buffer = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
2908105694Srwatson	error = mac_externalize_socket_peer_label(&so->so_peerlabel,
2909105694Srwatson	    elements, buffer, mac->m_buflen, M_WAITOK);
2910105694Srwatson	if (error == 0)
2911105694Srwatson		error = copyout(buffer, mac->m_string, strlen(buffer)+1);
2912105694Srwatson
2913105694Srwatson	free(buffer, M_MACTEMP);
2914105694Srwatson	free(elements, M_MACTEMP);
2915105694Srwatson
2916105694Srwatson	return (error);
2917100979Srwatson}
2918100979Srwatson
2919100979Srwatson/*
2920100979Srwatson * Implementation of VOP_SETLABEL() that relies on extended attributes
2921100979Srwatson * to store label data.  Can be referenced by filesystems supporting
2922100979Srwatson * extended attributes.
2923100979Srwatson */
2924100979Srwatsonint
2925100979Srwatsonvop_stdsetlabel_ea(struct vop_setlabel_args *ap)
2926100979Srwatson{
2927100979Srwatson	struct vnode *vp = ap->a_vp;
2928100979Srwatson	struct label *intlabel = ap->a_label;
2929100979Srwatson	int error;
2930100979Srwatson
2931100979Srwatson	ASSERT_VOP_LOCKED(vp, "vop_stdsetlabel_ea");
2932100979Srwatson
2933105988Srwatson	if ((vp->v_mount->mnt_flag & MNT_MULTILABEL) == 0)
2934105988Srwatson		return (EOPNOTSUPP);
2935100979Srwatson
2936105988Srwatson	error = mac_setlabel_vnode_extattr(ap->a_cred, vp, intlabel);
2937100979Srwatson	if (error)
2938100979Srwatson		return (error);
2939100979Srwatson
2940100979Srwatson	mac_relabel_vnode(ap->a_cred, vp, intlabel);
2941100979Srwatson
2942100979Srwatson	return (0);
2943100979Srwatson}
2944100979Srwatson
2945100979Srwatsonstatic int
2946100979Srwatsonvn_setlabel(struct vnode *vp, struct label *intlabel, struct ucred *cred)
2947100979Srwatson{
2948100979Srwatson	int error;
2949100979Srwatson
2950100979Srwatson	if (vp->v_mount == NULL) {
2951100979Srwatson		/* printf("vn_setlabel: null v_mount\n"); */
2952103314Snjl		if (vp->v_type != VNON)
2953103314Snjl			printf("vn_setlabel: null v_mount with non-VNON\n");
2954100979Srwatson		return (EBADF);
2955100979Srwatson	}
2956100979Srwatson
2957100979Srwatson	if ((vp->v_mount->mnt_flag & MNT_MULTILABEL) == 0)
2958100979Srwatson		return (EOPNOTSUPP);
2959100979Srwatson
2960100979Srwatson	/*
2961100979Srwatson	 * Multi-phase commit.  First check the policies to confirm the
2962100979Srwatson	 * change is OK.  Then commit via the filesystem.  Finally,
2963100979Srwatson	 * update the actual vnode label.  Question: maybe the filesystem
2964100979Srwatson	 * should update the vnode at the end as part of VOP_SETLABEL()?
2965100979Srwatson	 */
2966100979Srwatson	error = mac_check_vnode_relabel(cred, vp, intlabel);
2967100979Srwatson	if (error)
2968100979Srwatson		return (error);
2969100979Srwatson
2970100979Srwatson	/*
2971100979Srwatson	 * VADMIN provides the opportunity for the filesystem to make
2972100979Srwatson	 * decisions about who is and is not able to modify labels
2973100979Srwatson	 * and protections on files.  This might not be right.  We can't
2974100979Srwatson	 * assume VOP_SETLABEL() will do it, because we might implement
2975100979Srwatson	 * that as part of vop_stdsetlabel_ea().
2976100979Srwatson	 */
2977100979Srwatson	error = VOP_ACCESS(vp, VADMIN, cred, curthread);
2978100979Srwatson	if (error)
2979100979Srwatson		return (error);
2980100979Srwatson
2981100979Srwatson	error = VOP_SETLABEL(vp, intlabel, cred, curthread);
2982100979Srwatson	if (error)
2983100979Srwatson		return (error);
2984100979Srwatson
2985100979Srwatson	return (0);
2986100979Srwatson}
2987100979Srwatson
2988105694Srwatsonint
2989105694Srwatson__mac_get_pid(struct thread *td, struct __mac_get_pid_args *uap)
2990105694Srwatson{
2991105694Srwatson	char *elements, *buffer;
2992105694Srwatson	struct mac mac;
2993105694Srwatson	struct proc *tproc;
2994105694Srwatson	struct ucred *tcred;
2995105694Srwatson	int error;
2996105694Srwatson
2997105694Srwatson	error = copyin(SCARG(uap, mac_p), &mac, sizeof(mac));
2998105694Srwatson	if (error)
2999105694Srwatson		return (error);
3000105694Srwatson
3001105694Srwatson	error = mac_check_structmac_consistent(&mac);
3002105694Srwatson	if (error)
3003105694Srwatson		return (error);
3004105694Srwatson
3005105694Srwatson	tproc = pfind(uap->pid);
3006105694Srwatson	if (tproc == NULL)
3007105694Srwatson		return (ESRCH);
3008105694Srwatson
3009105694Srwatson	tcred = NULL;				/* Satisfy gcc. */
3010105694Srwatson	error = p_cansee(td, tproc);
3011105694Srwatson	if (error == 0)
3012105694Srwatson		tcred = crhold(tproc->p_ucred);
3013105694Srwatson	PROC_UNLOCK(tproc);
3014105694Srwatson	if (error)
3015105694Srwatson		return (error);
3016105694Srwatson
3017105694Srwatson	elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
3018105694Srwatson	error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
3019105694Srwatson	if (error) {
3020105694Srwatson		free(elements, M_MACTEMP);
3021105694Srwatson		crfree(tcred);
3022105694Srwatson		return (error);
3023105694Srwatson	}
3024105694Srwatson
3025105694Srwatson	buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
3026105694Srwatson	error = mac_externalize_cred_label(&tcred->cr_label, elements,
3027105694Srwatson	    buffer, mac.m_buflen, M_WAITOK);
3028105694Srwatson	if (error == 0)
3029105694Srwatson		error = copyout(buffer, mac.m_string, strlen(buffer)+1);
3030105694Srwatson
3031105694Srwatson	free(buffer, M_MACTEMP);
3032105694Srwatson	free(elements, M_MACTEMP);
3033105694Srwatson	crfree(tcred);
3034105694Srwatson	return (error);
3035105694Srwatson}
3036105694Srwatson
3037100979Srwatson/*
3038100979Srwatson * MPSAFE
3039100979Srwatson */
3040100979Srwatsonint
3041100894Srwatson__mac_get_proc(struct thread *td, struct __mac_get_proc_args *uap)
3042100894Srwatson{
3043105694Srwatson	char *elements, *buffer;
3044105694Srwatson	struct mac mac;
3045100979Srwatson	int error;
3046100894Srwatson
3047105694Srwatson	error = copyin(uap->mac_p, &mac, sizeof(mac));
3048105694Srwatson	if (error)
3049105694Srwatson		return (error);
3050105694Srwatson
3051105694Srwatson	error = mac_check_structmac_consistent(&mac);
3052105694Srwatson	if (error)
3053105694Srwatson		return (error);
3054105694Srwatson
3055105694Srwatson	elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
3056105694Srwatson	error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
3057105694Srwatson	if (error) {
3058105694Srwatson		free(elements, M_MACTEMP);
3059105694Srwatson		return (error);
3060105694Srwatson	}
3061105694Srwatson
3062105694Srwatson	buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
3063105694Srwatson	error = mac_externalize_cred_label(&td->td_ucred->cr_label,
3064105694Srwatson	    elements, buffer, mac.m_buflen, M_WAITOK);
3065100979Srwatson	if (error == 0)
3066105694Srwatson		error = copyout(buffer, mac.m_string, strlen(buffer)+1);
3067100979Srwatson
3068105694Srwatson	free(buffer, M_MACTEMP);
3069105694Srwatson	free(elements, M_MACTEMP);
3070100979Srwatson	return (error);
3071100979Srwatson}
3072100979Srwatson
3073100979Srwatson/*
3074100979Srwatson * MPSAFE
3075100979Srwatson */
3076100979Srwatsonint
3077100979Srwatson__mac_set_proc(struct thread *td, struct __mac_set_proc_args *uap)
3078100979Srwatson{
3079100979Srwatson	struct ucred *newcred, *oldcred;
3080105694Srwatson	struct label intlabel;
3081100979Srwatson	struct proc *p;
3082105694Srwatson	struct mac mac;
3083105694Srwatson	char *buffer;
3084100979Srwatson	int error;
3085100979Srwatson
3086105694Srwatson	error = copyin(uap->mac_p, &mac, sizeof(mac));
3087100979Srwatson	if (error)
3088100979Srwatson		return (error);
3089100979Srwatson
3090105694Srwatson	error = mac_check_structmac_consistent(&mac);
3091100979Srwatson	if (error)
3092100979Srwatson		return (error);
3093100979Srwatson
3094105694Srwatson	buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
3095105694Srwatson	error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
3096105694Srwatson	if (error) {
3097105694Srwatson		free(buffer, M_MACTEMP);
3098105694Srwatson		return (error);
3099105694Srwatson	}
3100105694Srwatson
3101105694Srwatson	mac_init_cred_label(&intlabel);
3102105694Srwatson	error = mac_internalize_cred_label(&intlabel, buffer);
3103105694Srwatson	free(buffer, M_MACTEMP);
3104105694Srwatson	if (error) {
3105105694Srwatson		mac_destroy_cred_label(&intlabel);
3106105694Srwatson		return (error);
3107105694Srwatson	}
3108105694Srwatson
3109100979Srwatson	newcred = crget();
3110100979Srwatson
3111100979Srwatson	p = td->td_proc;
3112100979Srwatson	PROC_LOCK(p);
3113100979Srwatson	oldcred = p->p_ucred;
3114100979Srwatson
3115100979Srwatson	error = mac_check_cred_relabel(oldcred, &intlabel);
3116100979Srwatson	if (error) {
3117100979Srwatson		PROC_UNLOCK(p);
3118100979Srwatson		crfree(newcred);
3119105694Srwatson		goto out;
3120100979Srwatson	}
3121100979Srwatson
3122100979Srwatson	setsugid(p);
3123100979Srwatson	crcopy(newcred, oldcred);
3124100979Srwatson	mac_relabel_cred(newcred, &intlabel);
3125102136Srwatson	p->p_ucred = newcred;
3126100979Srwatson
3127102136Srwatson	/*
3128102136Srwatson	 * Grab additional reference for use while revoking mmaps, prior
3129102136Srwatson	 * to releasing the proc lock and sharing the cred.
3130102136Srwatson	 */
3131102136Srwatson	crhold(newcred);
3132100979Srwatson	PROC_UNLOCK(p);
3133102136Srwatson
3134105694Srwatson	if (mac_enforce_vm) {
3135105694Srwatson		mtx_lock(&Giant);
3136105694Srwatson		mac_cred_mmapped_drop_perms(td, newcred);
3137105694Srwatson		mtx_unlock(&Giant);
3138105694Srwatson	}
3139102136Srwatson
3140102136Srwatson	crfree(newcred);	/* Free revocation reference. */
3141100979Srwatson	crfree(oldcred);
3142105694Srwatson
3143105694Srwatsonout:
3144105694Srwatson	mac_destroy_cred_label(&intlabel);
3145105694Srwatson	return (error);
3146100979Srwatson}
3147100979Srwatson
3148100979Srwatson/*
3149100979Srwatson * MPSAFE
3150100979Srwatson */
3151100979Srwatsonint
3152100979Srwatson__mac_get_fd(struct thread *td, struct __mac_get_fd_args *uap)
3153100979Srwatson{
3154105694Srwatson	char *elements, *buffer;
3155105694Srwatson	struct label intlabel;
3156100979Srwatson	struct file *fp;
3157105694Srwatson	struct mac mac;
3158100979Srwatson	struct vnode *vp;
3159100979Srwatson	struct pipe *pipe;
3160105694Srwatson	short label_type;
3161100979Srwatson	int error;
3162100979Srwatson
3163105694Srwatson	error = copyin(uap->mac_p, &mac, sizeof(mac));
3164105694Srwatson	if (error)
3165105694Srwatson		return (error);
3166100979Srwatson
3167105694Srwatson	error = mac_check_structmac_consistent(&mac);
3168105694Srwatson	if (error)
3169105694Srwatson		return (error);
3170105694Srwatson
3171105694Srwatson	elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
3172105694Srwatson	error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
3173105694Srwatson	if (error) {
3174105694Srwatson		free(elements, M_MACTEMP);
3175105694Srwatson		return (error);
3176105694Srwatson	}
3177105694Srwatson
3178105694Srwatson	buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
3179105694Srwatson	mtx_lock(&Giant);				/* VFS */
3180100979Srwatson	error = fget(td, SCARG(uap, fd), &fp);
3181100979Srwatson	if (error)
3182100979Srwatson		goto out;
3183100979Srwatson
3184105694Srwatson	label_type = fp->f_type;
3185100979Srwatson	switch (fp->f_type) {
3186100979Srwatson	case DTYPE_FIFO:
3187100979Srwatson	case DTYPE_VNODE:
3188100979Srwatson		vp = (struct vnode *)fp->f_data;
3189100979Srwatson
3190105694Srwatson		mac_init_vnode_label(&intlabel);
3191105694Srwatson
3192100979Srwatson		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3193105988Srwatson		mac_copy_vnode_label(&vp->v_label, &intlabel);
3194100979Srwatson		VOP_UNLOCK(vp, 0, td);
3195105694Srwatson
3196100979Srwatson		break;
3197100979Srwatson	case DTYPE_PIPE:
3198100979Srwatson		pipe = (struct pipe *)fp->f_data;
3199105694Srwatson
3200105694Srwatson		mac_init_pipe_label(&intlabel);
3201105694Srwatson
3202105694Srwatson		PIPE_LOCK(pipe);
3203105694Srwatson		mac_copy_pipe_label(pipe->pipe_label, &intlabel);
3204105694Srwatson		PIPE_UNLOCK(pipe);
3205100979Srwatson		break;
3206100979Srwatson	default:
3207100979Srwatson		error = EINVAL;
3208105694Srwatson		fdrop(fp, td);
3209105694Srwatson		goto out;
3210100979Srwatson	}
3211105694Srwatson	fdrop(fp, td);
3212100979Srwatson
3213105694Srwatson	switch (label_type) {
3214105694Srwatson	case DTYPE_FIFO:
3215105694Srwatson	case DTYPE_VNODE:
3216105694Srwatson		if (error == 0)
3217105694Srwatson			error = mac_externalize_vnode_label(&intlabel,
3218105694Srwatson			    elements, buffer, mac.m_buflen, M_WAITOK);
3219105694Srwatson		mac_destroy_vnode_label(&intlabel);
3220105694Srwatson		break;
3221105694Srwatson	case DTYPE_PIPE:
3222105694Srwatson		error = mac_externalize_pipe_label(&intlabel, elements,
3223105694Srwatson		    buffer, mac.m_buflen, M_WAITOK);
3224105694Srwatson		mac_destroy_pipe_label(&intlabel);
3225105694Srwatson		break;
3226105694Srwatson	default:
3227105694Srwatson		panic("__mac_get_fd: corrupted label_type");
3228105694Srwatson	}
3229105694Srwatson
3230100979Srwatson	if (error == 0)
3231105694Srwatson		error = copyout(buffer, mac.m_string, strlen(buffer)+1);
3232100979Srwatson
3233105694Srwatsonout:
3234105694Srwatson	mtx_unlock(&Giant);				/* VFS */
3235105694Srwatson	free(buffer, M_MACTEMP);
3236105694Srwatson	free(elements, M_MACTEMP);
3237100979Srwatson
3238100979Srwatson	return (error);
3239100979Srwatson}
3240100979Srwatson
3241100979Srwatson/*
3242100979Srwatson * MPSAFE
3243100979Srwatson */
3244100979Srwatsonint
3245100979Srwatson__mac_get_file(struct thread *td, struct __mac_get_file_args *uap)
3246100979Srwatson{
3247105694Srwatson	char *elements, *buffer;
3248100979Srwatson	struct nameidata nd;
3249105694Srwatson	struct label intlabel;
3250105694Srwatson	struct mac mac;
3251100979Srwatson	int error;
3252100979Srwatson
3253105694Srwatson	error = copyin(uap->mac_p, &mac, sizeof(mac));
3254105694Srwatson	if (error)
3255105694Srwatson		return (error);
3256105694Srwatson
3257105694Srwatson	error = mac_check_structmac_consistent(&mac);
3258105694Srwatson	if (error)
3259105694Srwatson		return (error);
3260105694Srwatson
3261105694Srwatson	elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
3262105694Srwatson	error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
3263105694Srwatson	if (error) {
3264105694Srwatson		free(elements, M_MACTEMP);
3265105694Srwatson		return (error);
3266105694Srwatson	}
3267105694Srwatson
3268105694Srwatson	buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
3269105694Srwatson	mtx_lock(&Giant);				/* VFS */
3270105694Srwatson	NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_USERSPACE, uap->path_p,
3271105694Srwatson	    td);
3272100979Srwatson	error = namei(&nd);
3273100979Srwatson	if (error)
3274100979Srwatson		goto out;
3275100979Srwatson
3276105694Srwatson	mac_init_vnode_label(&intlabel);
3277105988Srwatson	mac_copy_vnode_label(&nd.ni_vp->v_label, &intlabel);
3278105988Srwatson	error = mac_externalize_vnode_label(&intlabel, elements, buffer,
3279105988Srwatson	    mac.m_buflen, M_WAITOK);
3280105694Srwatson
3281100979Srwatson	NDFREE(&nd, 0);
3282105694Srwatson	mac_destroy_vnode_label(&intlabel);
3283105694Srwatson
3284105694Srwatson	if (error == 0)
3285105694Srwatson		error = copyout(buffer, mac.m_string, strlen(buffer)+1);
3286105694Srwatson
3287105694Srwatsonout:
3288105694Srwatson	mtx_unlock(&Giant);				/* VFS */
3289105694Srwatson
3290105694Srwatson	free(buffer, M_MACTEMP);
3291105694Srwatson	free(elements, M_MACTEMP);
3292105694Srwatson
3293105694Srwatson	return (error);
3294105694Srwatson}
3295105694Srwatson
3296105694Srwatson/*
3297105694Srwatson * MPSAFE
3298105694Srwatson */
3299105694Srwatsonint
3300105694Srwatson__mac_get_link(struct thread *td, struct __mac_get_link_args *uap)
3301105694Srwatson{
3302105694Srwatson	char *elements, *buffer;
3303105694Srwatson	struct nameidata nd;
3304105694Srwatson	struct label intlabel;
3305105694Srwatson	struct mac mac;
3306105694Srwatson	int error;
3307105694Srwatson
3308105694Srwatson	error = copyin(uap->mac_p, &mac, sizeof(mac));
3309100979Srwatson	if (error)
3310105694Srwatson		return (error);
3311105694Srwatson
3312105694Srwatson	error = mac_check_structmac_consistent(&mac);
3313105694Srwatson	if (error)
3314105694Srwatson		return (error);
3315105694Srwatson
3316105694Srwatson	elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
3317105694Srwatson	error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
3318105694Srwatson	if (error) {
3319105694Srwatson		free(elements, M_MACTEMP);
3320105694Srwatson		return (error);
3321105694Srwatson	}
3322105694Srwatson
3323105694Srwatson	buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
3324105694Srwatson	mtx_lock(&Giant);				/* VFS */
3325105694Srwatson	NDINIT(&nd, LOOKUP, LOCKLEAF | NOFOLLOW, UIO_USERSPACE, uap->path_p,
3326105694Srwatson	    td);
3327105694Srwatson	error = namei(&nd);
3328105694Srwatson	if (error)
3329100979Srwatson		goto out;
3330100979Srwatson
3331105694Srwatson	mac_init_vnode_label(&intlabel);
3332105988Srwatson	mac_copy_vnode_label(&nd.ni_vp->v_label, &intlabel);
3333105988Srwatson	error = mac_externalize_vnode_label(&intlabel, elements, buffer,
3334105988Srwatson	    mac.m_buflen, M_WAITOK);
3335105694Srwatson	NDFREE(&nd, 0);
3336105694Srwatson	mac_destroy_vnode_label(&intlabel);
3337100979Srwatson
3338105694Srwatson	if (error == 0)
3339105694Srwatson		error = copyout(buffer, mac.m_string, strlen(buffer)+1);
3340105694Srwatson
3341100979Srwatsonout:
3342105694Srwatson	mtx_unlock(&Giant);				/* VFS */
3343105694Srwatson
3344105694Srwatson	free(buffer, M_MACTEMP);
3345105694Srwatson	free(elements, M_MACTEMP);
3346105694Srwatson
3347100979Srwatson	return (error);
3348100979Srwatson}
3349100979Srwatson
3350100979Srwatson/*
3351100979Srwatson * MPSAFE
3352100979Srwatson */
3353100979Srwatsonint
3354100979Srwatson__mac_set_fd(struct thread *td, struct __mac_set_fd_args *uap)
3355100979Srwatson{
3356105694Srwatson	struct label intlabel;
3357105694Srwatson	struct pipe *pipe;
3358100979Srwatson	struct file *fp;
3359100979Srwatson	struct mount *mp;
3360100979Srwatson	struct vnode *vp;
3361105694Srwatson	struct mac mac;
3362105694Srwatson	char *buffer;
3363100979Srwatson	int error;
3364100979Srwatson
3365105694Srwatson	error = copyin(uap->mac_p, &mac, sizeof(mac));
3366100979Srwatson	if (error)
3367105694Srwatson		return (error);
3368100979Srwatson
3369105694Srwatson	error = mac_check_structmac_consistent(&mac);
3370100979Srwatson	if (error)
3371105694Srwatson		return (error);
3372100979Srwatson
3373105694Srwatson	buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
3374105694Srwatson	error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
3375105694Srwatson	if (error) {
3376105694Srwatson		free(buffer, M_MACTEMP);
3377105694Srwatson		return (error);
3378105694Srwatson	}
3379105694Srwatson
3380105694Srwatson	mtx_lock(&Giant);				/* VFS */
3381105694Srwatson
3382105694Srwatson	error = fget(td, SCARG(uap, fd), &fp);
3383100979Srwatson	if (error)
3384105694Srwatson		goto out;
3385100979Srwatson
3386100979Srwatson	switch (fp->f_type) {
3387100979Srwatson	case DTYPE_FIFO:
3388100979Srwatson	case DTYPE_VNODE:
3389105694Srwatson		mac_init_vnode_label(&intlabel);
3390105694Srwatson		error = mac_internalize_vnode_label(&intlabel, buffer);
3391105694Srwatson		if (error) {
3392105694Srwatson			mac_destroy_vnode_label(&intlabel);
3393105694Srwatson			break;
3394105694Srwatson		}
3395105694Srwatson
3396100979Srwatson		vp = (struct vnode *)fp->f_data;
3397100979Srwatson		error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3398105694Srwatson		if (error != 0) {
3399105694Srwatson			mac_destroy_vnode_label(&intlabel);
3400100979Srwatson			break;
3401105694Srwatson		}
3402100979Srwatson
3403100979Srwatson		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3404100979Srwatson		error = vn_setlabel(vp, &intlabel, td->td_ucred);
3405100979Srwatson		VOP_UNLOCK(vp, 0, td);
3406100979Srwatson		vn_finished_write(mp);
3407105694Srwatson
3408105694Srwatson		mac_destroy_vnode_label(&intlabel);
3409100979Srwatson		break;
3410105694Srwatson
3411100979Srwatson	case DTYPE_PIPE:
3412105694Srwatson		mac_init_pipe_label(&intlabel);
3413105694Srwatson		error = mac_internalize_pipe_label(&intlabel, buffer);
3414105694Srwatson		if (error == 0) {
3415105694Srwatson			pipe = (struct pipe *)fp->f_data;
3416105694Srwatson			PIPE_LOCK(pipe);
3417105694Srwatson			error = mac_pipe_label_set(td->td_ucred, pipe,
3418105694Srwatson			    &intlabel);
3419105694Srwatson			PIPE_UNLOCK(pipe);
3420105694Srwatson		}
3421105694Srwatson
3422105694Srwatson		mac_destroy_pipe_label(&intlabel);
3423100979Srwatson		break;
3424105694Srwatson
3425100979Srwatson	default:
3426100979Srwatson		error = EINVAL;
3427100979Srwatson	}
3428100979Srwatson
3429100979Srwatson	fdrop(fp, td);
3430105694Srwatsonout:
3431105694Srwatson	mtx_unlock(&Giant);				/* VFS */
3432105694Srwatson
3433105694Srwatson	free(buffer, M_MACTEMP);
3434105694Srwatson
3435100979Srwatson	return (error);
3436100979Srwatson}
3437100979Srwatson
3438100979Srwatson/*
3439100979Srwatson * MPSAFE
3440100979Srwatson */
3441100979Srwatsonint
3442100979Srwatson__mac_set_file(struct thread *td, struct __mac_set_file_args *uap)
3443100979Srwatson{
3444105694Srwatson	struct label intlabel;
3445100979Srwatson	struct nameidata nd;
3446100979Srwatson	struct mount *mp;
3447105694Srwatson	struct mac mac;
3448105694Srwatson	char *buffer;
3449100979Srwatson	int error;
3450100979Srwatson
3451105694Srwatson	error = copyin(uap->mac_p, &mac, sizeof(mac));
3452100979Srwatson	if (error)
3453105694Srwatson		return (error);
3454100979Srwatson
3455105694Srwatson	error = mac_check_structmac_consistent(&mac);
3456100979Srwatson	if (error)
3457105694Srwatson		return (error);
3458100979Srwatson
3459105694Srwatson	buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
3460105694Srwatson	error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
3461105694Srwatson	if (error) {
3462105694Srwatson		free(buffer, M_MACTEMP);
3463105694Srwatson		return (error);
3464105694Srwatson	}
3465105694Srwatson
3466105694Srwatson	mac_init_vnode_label(&intlabel);
3467105694Srwatson	error = mac_internalize_vnode_label(&intlabel, buffer);
3468105694Srwatson	free(buffer, M_MACTEMP);
3469105694Srwatson	if (error) {
3470105694Srwatson		mac_destroy_vnode_label(&intlabel);
3471105694Srwatson		return (error);
3472105694Srwatson	}
3473105694Srwatson
3474105694Srwatson	mtx_lock(&Giant);				/* VFS */
3475105694Srwatson
3476105694Srwatson	NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_USERSPACE, uap->path_p,
3477105694Srwatson	    td);
3478100979Srwatson	error = namei(&nd);
3479105694Srwatson	if (error == 0) {
3480105694Srwatson		error = vn_start_write(nd.ni_vp, &mp, V_WAIT | PCATCH);
3481105694Srwatson		if (error == 0)
3482105694Srwatson			error = vn_setlabel(nd.ni_vp, &intlabel,
3483105694Srwatson			    td->td_ucred);
3484105694Srwatson		vn_finished_write(mp);
3485105694Srwatson	}
3486105694Srwatson
3487105694Srwatson	NDFREE(&nd, 0);
3488105694Srwatson	mtx_unlock(&Giant);				/* VFS */
3489105694Srwatson	mac_destroy_vnode_label(&intlabel);
3490105694Srwatson
3491105694Srwatson	return (error);
3492105694Srwatson}
3493105694Srwatson
3494105694Srwatson/*
3495105694Srwatson * MPSAFE
3496105694Srwatson */
3497105694Srwatsonint
3498105694Srwatson__mac_set_link(struct thread *td, struct __mac_set_link_args *uap)
3499105694Srwatson{
3500105694Srwatson	struct label intlabel;
3501105694Srwatson	struct nameidata nd;
3502105694Srwatson	struct mount *mp;
3503105694Srwatson	struct mac mac;
3504105694Srwatson	char *buffer;
3505105694Srwatson	int error;
3506105694Srwatson
3507105694Srwatson	error = copyin(uap->mac_p, &mac, sizeof(mac));
3508100979Srwatson	if (error)
3509105694Srwatson		return (error);
3510105694Srwatson
3511105694Srwatson	error = mac_check_structmac_consistent(&mac);
3512100979Srwatson	if (error)
3513105694Srwatson		return (error);
3514100979Srwatson
3515105694Srwatson	buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
3516105694Srwatson	error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
3517105694Srwatson	if (error) {
3518105694Srwatson		free(buffer, M_MACTEMP);
3519105694Srwatson		return (error);
3520105694Srwatson	}
3521105694Srwatson
3522105694Srwatson	mac_init_vnode_label(&intlabel);
3523105694Srwatson	error = mac_internalize_vnode_label(&intlabel, buffer);
3524105694Srwatson	free(buffer, M_MACTEMP);
3525105694Srwatson	if (error) {
3526105694Srwatson		mac_destroy_vnode_label(&intlabel);
3527105694Srwatson		return (error);
3528105694Srwatson	}
3529105694Srwatson
3530105694Srwatson	mtx_lock(&Giant);				/* VFS */
3531105694Srwatson
3532105694Srwatson	NDINIT(&nd, LOOKUP, LOCKLEAF | NOFOLLOW, UIO_USERSPACE, uap->path_p,
3533105694Srwatson	    td);
3534105694Srwatson	error = namei(&nd);
3535105694Srwatson	if (error == 0) {
3536105694Srwatson		error = vn_start_write(nd.ni_vp, &mp, V_WAIT | PCATCH);
3537105694Srwatson		if (error == 0)
3538105694Srwatson			error = vn_setlabel(nd.ni_vp, &intlabel,
3539105694Srwatson			    td->td_ucred);
3540105694Srwatson		vn_finished_write(mp);
3541105694Srwatson	}
3542105694Srwatson
3543100979Srwatson	NDFREE(&nd, 0);
3544105694Srwatson	mtx_unlock(&Giant);				/* VFS */
3545105694Srwatson	mac_destroy_vnode_label(&intlabel);
3546105694Srwatson
3547100979Srwatson	return (error);
3548100979Srwatson}
3549100979Srwatson
3550105694Srwatson/*
3551105694Srwatson * MPSAFE
3552105694Srwatson */
3553102123Srwatsonint
3554102123Srwatsonmac_syscall(struct thread *td, struct mac_syscall_args *uap)
3555102123Srwatson{
3556102123Srwatson	struct mac_policy_conf *mpc;
3557102123Srwatson	char target[MAC_MAX_POLICY_NAME];
3558102123Srwatson	int error;
3559102123Srwatson
3560102123Srwatson	error = copyinstr(SCARG(uap, policy), target, sizeof(target), NULL);
3561102123Srwatson	if (error)
3562102123Srwatson		return (error);
3563102123Srwatson
3564102123Srwatson	error = ENOSYS;
3565102123Srwatson	MAC_POLICY_LIST_BUSY();
3566102123Srwatson	LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {
3567102123Srwatson		if (strcmp(mpc->mpc_name, target) == 0 &&
3568102123Srwatson		    mpc->mpc_ops->mpo_syscall != NULL) {
3569102123Srwatson			error = mpc->mpc_ops->mpo_syscall(td,
3570102123Srwatson			    SCARG(uap, call), SCARG(uap, arg));
3571102123Srwatson			goto out;
3572102123Srwatson		}
3573102123Srwatson	}
3574102123Srwatson
3575102123Srwatsonout:
3576102123Srwatson	MAC_POLICY_LIST_UNBUSY();
3577102123Srwatson	return (error);
3578102123Srwatson}
3579102123Srwatson
3580100979SrwatsonSYSINIT(mac, SI_SUB_MAC, SI_ORDER_FIRST, mac_init, NULL);
3581100979SrwatsonSYSINIT(mac_late, SI_SUB_MAC_LATE, SI_ORDER_FIRST, mac_late_init, NULL);
3582100979Srwatson
3583100979Srwatson#else /* !MAC */
3584100979Srwatson
3585100979Srwatsonint
3586105694Srwatson__mac_get_pid(struct thread *td, struct __mac_get_pid_args *uap)
3587105694Srwatson{
3588105694Srwatson
3589105694Srwatson	return (ENOSYS);
3590105694Srwatson}
3591105694Srwatson
3592105694Srwatsonint
3593100979Srwatson__mac_get_proc(struct thread *td, struct __mac_get_proc_args *uap)
3594100979Srwatson{
3595100979Srwatson
3596100894Srwatson	return (ENOSYS);
3597100894Srwatson}
3598100894Srwatson
3599100894Srwatsonint
3600100894Srwatson__mac_set_proc(struct thread *td, struct __mac_set_proc_args *uap)
3601100894Srwatson{
3602100894Srwatson
3603100894Srwatson	return (ENOSYS);
3604100894Srwatson}
3605100894Srwatson
3606100894Srwatsonint
3607100894Srwatson__mac_get_fd(struct thread *td, struct __mac_get_fd_args *uap)
3608100894Srwatson{
3609100894Srwatson
3610100894Srwatson	return (ENOSYS);
3611100894Srwatson}
3612100894Srwatson
3613100894Srwatsonint
3614100894Srwatson__mac_get_file(struct thread *td, struct __mac_get_file_args *uap)
3615100894Srwatson{
3616100894Srwatson
3617100894Srwatson	return (ENOSYS);
3618100894Srwatson}
3619100894Srwatson
3620100894Srwatsonint
3621105694Srwatson__mac_get_link(struct thread *td, struct __mac_get_link_args *uap)
3622105694Srwatson{
3623105694Srwatson
3624105694Srwatson	return (ENOSYS);
3625105694Srwatson}
3626105694Srwatson
3627105694Srwatsonint
3628100894Srwatson__mac_set_fd(struct thread *td, struct __mac_set_fd_args *uap)
3629100894Srwatson{
3630100894Srwatson
3631100894Srwatson	return (ENOSYS);
3632100894Srwatson}
3633100894Srwatson
3634100894Srwatsonint
3635100894Srwatson__mac_set_file(struct thread *td, struct __mac_set_file_args *uap)
3636100894Srwatson{
3637100894Srwatson
3638100894Srwatson	return (ENOSYS);
3639100894Srwatson}
3640100979Srwatson
3641102123Srwatsonint
3642105694Srwatson__mac_set_link(struct thread *td, struct __mac_set_link_args *uap)
3643105694Srwatson{
3644105694Srwatson
3645105694Srwatson	return (ENOSYS);
3646105694Srwatson}
3647105694Srwatson
3648105694Srwatsonint
3649102123Srwatsonmac_syscall(struct thread *td, struct mac_syscall_args *uap)
3650102123Srwatson{
3651102123Srwatson
3652102123Srwatson	return (ENOSYS);
3653102123Srwatson}
3654102123Srwatson
3655105694Srwatson#endif
3656