mac_cred.c revision 104529
1100894Srwatson/*-
2100894Srwatson * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson
3100894Srwatson * Copyright (c) 2001 Ilmar S. Habibulin
4100894Srwatson * Copyright (c) 2001, 2002 Networks Associates Technology, Inc.
5100894Srwatson * All rights reserved.
6100894Srwatson *
7100894Srwatson * This software was developed by Robert Watson and Ilmar Habibulin for the
8100894Srwatson * TrustedBSD Project.
9100894Srwatson *
10100894Srwatson * This software was developed for the FreeBSD Project in part by NAI Labs,
11100894Srwatson * the Security Research Division of Network Associates, Inc. under
12100894Srwatson * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
13100894Srwatson * CHATS research program.
14100894Srwatson *
15100894Srwatson * Redistribution and use in source and binary forms, with or without
16100894Srwatson * modification, are permitted provided that the following conditions
17100894Srwatson * are met:
18100894Srwatson * 1. Redistributions of source code must retain the above copyright
19100894Srwatson *    notice, this list of conditions and the following disclaimer.
20100894Srwatson * 2. Redistributions in binary form must reproduce the above copyright
21100894Srwatson *    notice, this list of conditions and the following disclaimer in the
22100894Srwatson *    documentation and/or other materials provided with the distribution.
23100894Srwatson * 3. The names of the authors may not be used to endorse or promote
24100894Srwatson *    products derived from this software without specific prior written
25100894Srwatson *    permission.
26100894Srwatson *
27100894Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
28100894Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29100894Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30100894Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
31100894Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32100894Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33100894Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34100894Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35100894Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36100894Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37100894Srwatson * SUCH DAMAGE.
38100894Srwatson *
39100894Srwatson * $FreeBSD: head/sys/security/mac/mac_process.c 104529 2002-10-05 18:11:36Z rwatson $
40100894Srwatson */
41100894Srwatson/*
42100894Srwatson * Developed by the TrustedBSD Project.
43100894Srwatson *
44100894Srwatson * Framework for extensible kernel access control.  Kernel and userland
45100894Srwatson * interface to the framework, policy registration and composition.
46100894Srwatson */
47100894Srwatson
48100894Srwatson#include "opt_mac.h"
49104300Sphk#include "opt_devfs.h"
50101173Srwatson
51100894Srwatson#include <sys/param.h>
52100979Srwatson#include <sys/extattr.h>
53100979Srwatson#include <sys/kernel.h>
54100979Srwatson#include <sys/lock.h>
55102949Sbde#include <sys/malloc.h>
56100979Srwatson#include <sys/mutex.h>
57100979Srwatson#include <sys/mac.h>
58101712Srwatson#include <sys/module.h>
59100979Srwatson#include <sys/proc.h>
60100979Srwatson#include <sys/systm.h>
61100894Srwatson#include <sys/sysproto.h>
62100894Srwatson#include <sys/sysent.h>
63100979Srwatson#include <sys/vnode.h>
64100979Srwatson#include <sys/mount.h>
65100979Srwatson#include <sys/file.h>
66100979Srwatson#include <sys/namei.h>
67100979Srwatson#include <sys/socket.h>
68100979Srwatson#include <sys/pipe.h>
69100979Srwatson#include <sys/socketvar.h>
70100979Srwatson#include <sys/sysctl.h>
71100894Srwatson
72100979Srwatson#include <vm/vm.h>
73100979Srwatson#include <vm/pmap.h>
74100979Srwatson#include <vm/vm_map.h>
75100979Srwatson#include <vm/vm_object.h>
76100979Srwatson
77100979Srwatson#include <sys/mac_policy.h>
78100979Srwatson
79100979Srwatson#include <fs/devfs/devfs.h>
80100979Srwatson
81100979Srwatson#include <net/bpfdesc.h>
82100979Srwatson#include <net/if.h>
83100979Srwatson#include <net/if_var.h>
84100979Srwatson
85100979Srwatson#include <netinet/in.h>
86100979Srwatson#include <netinet/ip_var.h>
87100979Srwatson
88100979Srwatson#ifdef MAC
89100979Srwatson
90101712Srwatson/*
91101712Srwatson * Declare that the kernel provides MAC support, version 1.  This permits
92101712Srwatson * modules to refuse to be loaded if the necessary support isn't present,
93101712Srwatson * even if it's pre-boot.
94101712Srwatson */
95101712SrwatsonMODULE_VERSION(kernel_mac_support, 1);
96101712Srwatson
97100979SrwatsonSYSCTL_DECL(_security);
98100979Srwatson
99100979SrwatsonSYSCTL_NODE(_security, OID_AUTO, mac, CTLFLAG_RW, 0,
100100979Srwatson    "TrustedBSD MAC policy controls");
101104517Srwatson
102100979Srwatson#ifndef MAC_MAX_POLICIES
103100979Srwatson#define	MAC_MAX_POLICIES	8
104100979Srwatson#endif
105100979Srwatson#if MAC_MAX_POLICIES > 32
106100979Srwatson#error "MAC_MAX_POLICIES too large"
107100979Srwatson#endif
108100979Srwatsonstatic unsigned int mac_max_policies = MAC_MAX_POLICIES;
109100979Srwatsonstatic unsigned int mac_policy_offsets_free = (1 << MAC_MAX_POLICIES) - 1;
110100979SrwatsonSYSCTL_UINT(_security_mac, OID_AUTO, max_policies, CTLFLAG_RD,
111100979Srwatson    &mac_max_policies, 0, "");
112100979Srwatson
113100979Srwatsonstatic int	mac_late = 0;
114100979Srwatson
115100979Srwatsonstatic int	mac_enforce_fs = 1;
116100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, enforce_fs, CTLFLAG_RW,
117100979Srwatson    &mac_enforce_fs, 0, "Enforce MAC policy on file system objects");
118100979SrwatsonTUNABLE_INT("security.mac.enforce_fs", &mac_enforce_fs);
119100979Srwatson
120100979Srwatsonstatic int	mac_enforce_network = 1;
121100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, enforce_network, CTLFLAG_RW,
122100979Srwatson    &mac_enforce_network, 0, "Enforce MAC policy on network packets");
123100979SrwatsonTUNABLE_INT("security.mac.enforce_network", &mac_enforce_network);
124100979Srwatson
125103513Srwatsonstatic int	mac_enforce_pipe = 1;
126103513SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, enforce_pipe, CTLFLAG_RW,
127103513Srwatson    &mac_enforce_pipe, 0, "Enforce MAC policy on pipe operations");
128104236SrwatsonTUNABLE_INT("security.mac.enforce_pipe", &mac_enforce_pipe);
129103513Srwatson
130100979Srwatsonstatic int	mac_enforce_process = 1;
131100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, enforce_process, CTLFLAG_RW,
132100979Srwatson    &mac_enforce_process, 0, "Enforce MAC policy on inter-process operations");
133100979SrwatsonTUNABLE_INT("security.mac.enforce_process", &mac_enforce_process);
134100979Srwatson
135100979Srwatsonstatic int	mac_enforce_socket = 1;
136100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, enforce_socket, CTLFLAG_RW,
137100979Srwatson    &mac_enforce_socket, 0, "Enforce MAC policy on socket operations");
138100979SrwatsonTUNABLE_INT("security.mac.enforce_socket", &mac_enforce_socket);
139100979Srwatson
140103514Srwatsonstatic int     mac_enforce_vm = 1;
141103514SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, enforce_vm, CTLFLAG_RW,
142103514Srwatson    &mac_enforce_vm, 0, "Enforce MAC policy on vm operations");
143104236SrwatsonTUNABLE_INT("security.mac.enforce_vm", &mac_enforce_vm);
144103514Srwatson
145100979Srwatsonstatic int	mac_label_size = sizeof(struct mac);
146100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, label_size, CTLFLAG_RD,
147100979Srwatson    &mac_label_size, 0, "Pre-compiled MAC label size");
148100979Srwatson
149100979Srwatsonstatic int	mac_cache_fslabel_in_vnode = 1;
150100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, cache_fslabel_in_vnode, CTLFLAG_RW,
151100979Srwatson    &mac_cache_fslabel_in_vnode, 0, "Cache mount fslabel in vnode");
152100979SrwatsonTUNABLE_INT("security.mac.cache_fslabel_in_vnode",
153100979Srwatson    &mac_cache_fslabel_in_vnode);
154100979Srwatson
155100979Srwatsonstatic int	mac_vnode_label_cache_hits = 0;
156100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, vnode_label_cache_hits, CTLFLAG_RD,
157100979Srwatson    &mac_vnode_label_cache_hits, 0, "Cache hits on vnode labels");
158100979Srwatsonstatic int	mac_vnode_label_cache_misses = 0;
159100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, vnode_label_cache_misses, CTLFLAG_RD,
160100979Srwatson    &mac_vnode_label_cache_misses, 0, "Cache misses on vnode labels");
161103136Srwatson
162103136Srwatsonstatic int	mac_mmap_revocation = 1;
163103136SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation, CTLFLAG_RW,
164103136Srwatson    &mac_mmap_revocation, 0, "Revoke mmap access to files on subject "
165103136Srwatson    "relabel");
166101892Srwatsonstatic int	mac_mmap_revocation_via_cow = 0;
167100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation_via_cow, CTLFLAG_RW,
168100979Srwatson    &mac_mmap_revocation_via_cow, 0, "Revoke mmap access to files via "
169100979Srwatson    "copy-on-write semantics, or by removing all write access");
170100979Srwatson
171101988Srwatson#ifdef MAC_DEBUG
172104268SrwatsonSYSCTL_NODE(_security_mac, OID_AUTO, debug, CTLFLAG_RW, 0,
173104268Srwatson    "TrustedBSD MAC debug info");
174104268Srwatson
175104268Srwatsonstatic int	mac_debug_label_fallback = 0;
176104268SrwatsonSYSCTL_INT(_security_mac_debug, OID_AUTO, label_fallback, CTLFLAG_RW,
177104268Srwatson    &mac_debug_label_fallback, 0, "Filesystems should fall back to fs label"
178104268Srwatson    "when label is corrupted.");
179104268SrwatsonTUNABLE_INT("security.mac.debug_label_fallback",
180104268Srwatson    &mac_debug_label_fallback);
181104268Srwatson
182104517SrwatsonSYSCTL_NODE(_security_mac_debug, OID_AUTO, counters, CTLFLAG_RW, 0,
183104517Srwatson    "TrustedBSD MAC object counters");
184104517Srwatson
185100979Srwatsonstatic unsigned int nmacmbufs, nmaccreds, nmacifnets, nmacbpfdescs,
186100979Srwatson    nmacsockets, nmacmounts, nmactemp, nmacvnodes, nmacdevfsdirents,
187100979Srwatson    nmacipqs, nmacpipes;
188104517Srwatson
189104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, mbufs, CTLFLAG_RD,
190100979Srwatson    &nmacmbufs, 0, "number of mbufs in use");
191104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, creds, CTLFLAG_RD,
192100979Srwatson    &nmaccreds, 0, "number of ucreds in use");
193104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, ifnets, CTLFLAG_RD,
194100979Srwatson    &nmacifnets, 0, "number of ifnets in use");
195104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, ipqs, CTLFLAG_RD,
196100979Srwatson    &nmacipqs, 0, "number of ipqs in use");
197104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, bpfdescs, CTLFLAG_RD,
198100979Srwatson    &nmacbpfdescs, 0, "number of bpfdescs in use");
199104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, sockets, CTLFLAG_RD,
200100979Srwatson    &nmacsockets, 0, "number of sockets in use");
201104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, pipes, CTLFLAG_RD,
202100979Srwatson    &nmacpipes, 0, "number of pipes in use");
203104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, mounts, CTLFLAG_RD,
204100979Srwatson    &nmacmounts, 0, "number of mounts in use");
205104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, temp, CTLFLAG_RD,
206100979Srwatson    &nmactemp, 0, "number of temporary labels in use");
207104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, vnodes, CTLFLAG_RD,
208100979Srwatson    &nmacvnodes, 0, "number of vnodes in use");
209104517SrwatsonSYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, devfsdirents, CTLFLAG_RD,
210100979Srwatson    &nmacdevfsdirents, 0, "number of devfs dirents inuse");
211101988Srwatson#endif
212100979Srwatson
213100979Srwatsonstatic int	error_select(int error1, int error2);
214100979Srwatsonstatic int	mac_externalize(struct label *label, struct mac *mac);
215100979Srwatsonstatic int	mac_policy_register(struct mac_policy_conf *mpc);
216100979Srwatsonstatic int	mac_policy_unregister(struct mac_policy_conf *mpc);
217100979Srwatson
218100979Srwatsonstatic int	mac_stdcreatevnode_ea(struct vnode *vp);
219100979Srwatsonstatic void	mac_cred_mmapped_drop_perms(struct thread *td,
220100979Srwatson		    struct ucred *cred);
221100979Srwatsonstatic void	mac_cred_mmapped_drop_perms_recurse(struct thread *td,
222100979Srwatson		    struct ucred *cred, struct vm_map *map);
223100979Srwatson
224100979SrwatsonMALLOC_DEFINE(M_MACOPVEC, "macopvec", "MAC policy operation vector");
225100979SrwatsonMALLOC_DEFINE(M_MACPIPELABEL, "macpipelabel", "MAC labels for pipes");
226100979Srwatson
227100979Srwatson/*
228100979Srwatson * mac_policy_list_lock protects the consistency of 'mac_policy_list',
229100979Srwatson * the linked list of attached policy modules.  Read-only consumers of
230100979Srwatson * the list must acquire a shared lock for the duration of their use;
231100979Srwatson * writers must acquire an exclusive lock.  Note that for compound
232100979Srwatson * operations, locks should be held for the entire compound operation,
233100979Srwatson * and that this is not yet done for relabel requests.
234100979Srwatson */
235100979Srwatsonstatic struct mtx mac_policy_list_lock;
236100979Srwatsonstatic LIST_HEAD(, mac_policy_conf) mac_policy_list;
237100979Srwatsonstatic int mac_policy_list_busy;
238100979Srwatson#define	MAC_POLICY_LIST_LOCKINIT()	mtx_init(&mac_policy_list_lock,	\
239100979Srwatson	"mac_policy_list_lock", NULL, MTX_DEF);
240100979Srwatson#define	MAC_POLICY_LIST_LOCK()	mtx_lock(&mac_policy_list_lock);
241100979Srwatson#define	MAC_POLICY_LIST_UNLOCK()	mtx_unlock(&mac_policy_list_lock);
242100979Srwatson
243100979Srwatson#define	MAC_POLICY_LIST_BUSY() do {					\
244100979Srwatson	MAC_POLICY_LIST_LOCK();						\
245100979Srwatson	mac_policy_list_busy++;						\
246100979Srwatson	MAC_POLICY_LIST_UNLOCK();					\
247100979Srwatson} while (0)
248100979Srwatson
249100979Srwatson#define	MAC_POLICY_LIST_UNBUSY() do {					\
250100979Srwatson	MAC_POLICY_LIST_LOCK();						\
251100979Srwatson	mac_policy_list_busy--;						\
252100979Srwatson	if (mac_policy_list_busy < 0)					\
253100979Srwatson		panic("Extra mac_policy_list_busy--");			\
254100979Srwatson	MAC_POLICY_LIST_UNLOCK();					\
255100979Srwatson} while (0)
256100979Srwatson
257100979Srwatson/*
258100979Srwatson * MAC_CHECK performs the designated check by walking the policy
259100979Srwatson * module list and checking with each as to how it feels about the
260100979Srwatson * request.  Note that it returns its value via 'error' in the scope
261100979Srwatson * of the caller.
262100979Srwatson */
263100979Srwatson#define	MAC_CHECK(check, args...) do {					\
264100979Srwatson	struct mac_policy_conf *mpc;					\
265100979Srwatson									\
266100979Srwatson	error = 0;							\
267100979Srwatson	MAC_POLICY_LIST_BUSY();						\
268100979Srwatson	LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {			\
269100979Srwatson		if (mpc->mpc_ops->mpo_ ## check != NULL)		\
270100979Srwatson			error = error_select(				\
271100979Srwatson			    mpc->mpc_ops->mpo_ ## check (args),		\
272100979Srwatson			    error);					\
273100979Srwatson	}								\
274100979Srwatson	MAC_POLICY_LIST_UNBUSY();					\
275100979Srwatson} while (0)
276100979Srwatson
277100979Srwatson/*
278100979Srwatson * MAC_BOOLEAN performs the designated boolean composition by walking
279100979Srwatson * the module list, invoking each instance of the operation, and
280100979Srwatson * combining the results using the passed C operator.  Note that it
281100979Srwatson * returns its value via 'result' in the scope of the caller, which
282100979Srwatson * should be initialized by the caller in a meaningful way to get
283100979Srwatson * a meaningful result.
284100979Srwatson */
285100979Srwatson#define	MAC_BOOLEAN(operation, composition, args...) do {		\
286100979Srwatson	struct mac_policy_conf *mpc;					\
287100979Srwatson									\
288100979Srwatson	MAC_POLICY_LIST_BUSY();						\
289100979Srwatson	LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {			\
290100979Srwatson		if (mpc->mpc_ops->mpo_ ## operation != NULL)		\
291100979Srwatson			result = result composition			\
292100979Srwatson			    mpc->mpc_ops->mpo_ ## operation (args);	\
293100979Srwatson	}								\
294100979Srwatson	MAC_POLICY_LIST_UNBUSY();					\
295100979Srwatson} while (0)
296100979Srwatson
297100979Srwatson/*
298100979Srwatson * MAC_PERFORM performs the designated operation by walking the policy
299100979Srwatson * module list and invoking that operation for each policy.
300100979Srwatson */
301100979Srwatson#define	MAC_PERFORM(operation, args...) do {				\
302100979Srwatson	struct mac_policy_conf *mpc;					\
303100979Srwatson									\
304100979Srwatson	MAC_POLICY_LIST_BUSY();						\
305100979Srwatson	LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {			\
306100979Srwatson		if (mpc->mpc_ops->mpo_ ## operation != NULL)		\
307100979Srwatson			mpc->mpc_ops->mpo_ ## operation (args);		\
308100979Srwatson	}								\
309100979Srwatson	MAC_POLICY_LIST_UNBUSY();					\
310100979Srwatson} while (0)
311100979Srwatson
312100979Srwatson/*
313100979Srwatson * Initialize the MAC subsystem, including appropriate SMP locks.
314100979Srwatson */
315100979Srwatsonstatic void
316100979Srwatsonmac_init(void)
317100979Srwatson{
318100979Srwatson
319100979Srwatson	LIST_INIT(&mac_policy_list);
320100979Srwatson	MAC_POLICY_LIST_LOCKINIT();
321100979Srwatson}
322100979Srwatson
323100979Srwatson/*
324100979Srwatson * For the purposes of modules that want to know if they were loaded
325100979Srwatson * "early", set the mac_late flag once we've processed modules either
326100979Srwatson * linked into the kernel, or loaded before the kernel startup.
327100979Srwatson */
328100979Srwatsonstatic void
329100979Srwatsonmac_late_init(void)
330100979Srwatson{
331100979Srwatson
332100979Srwatson	mac_late = 1;
333100979Srwatson}
334100979Srwatson
335100979Srwatson/*
336100979Srwatson * Allow MAC policy modules to register during boot, etc.
337100979Srwatson */
338100894Srwatsonint
339100979Srwatsonmac_policy_modevent(module_t mod, int type, void *data)
340100979Srwatson{
341100979Srwatson	struct mac_policy_conf *mpc;
342100979Srwatson	int error;
343100979Srwatson
344100979Srwatson	error = 0;
345100979Srwatson	mpc = (struct mac_policy_conf *) data;
346100979Srwatson
347100979Srwatson	switch (type) {
348100979Srwatson	case MOD_LOAD:
349100979Srwatson		if (mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_NOTLATE &&
350100979Srwatson		    mac_late) {
351100979Srwatson			printf("mac_policy_modevent: can't load %s policy "
352100979Srwatson			    "after booting\n", mpc->mpc_name);
353100979Srwatson			error = EBUSY;
354100979Srwatson			break;
355100979Srwatson		}
356100979Srwatson		error = mac_policy_register(mpc);
357100979Srwatson		break;
358100979Srwatson	case MOD_UNLOAD:
359100979Srwatson		/* Don't unregister the module if it was never registered. */
360100979Srwatson		if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED)
361100979Srwatson		    != 0)
362100979Srwatson			error = mac_policy_unregister(mpc);
363100979Srwatson		else
364100979Srwatson			error = 0;
365100979Srwatson		break;
366100979Srwatson	default:
367100979Srwatson		break;
368100979Srwatson	}
369100979Srwatson
370100979Srwatson	return (error);
371100979Srwatson}
372100979Srwatson
373100979Srwatsonstatic int
374100979Srwatsonmac_policy_register(struct mac_policy_conf *mpc)
375100979Srwatson{
376100979Srwatson	struct mac_policy_conf *tmpc;
377100979Srwatson	struct mac_policy_op_entry *mpe;
378100979Srwatson	int slot;
379100979Srwatson
380103570Srwatson	MALLOC(mpc->mpc_ops, struct mac_policy_ops *, sizeof(*mpc->mpc_ops),
381103570Srwatson	    M_MACOPVEC, M_WAITOK | M_ZERO);
382100979Srwatson	for (mpe = mpc->mpc_entries; mpe->mpe_constant != MAC_OP_LAST; mpe++) {
383100979Srwatson		switch (mpe->mpe_constant) {
384100979Srwatson		case MAC_OP_LAST:
385100979Srwatson			/*
386100979Srwatson			 * Doesn't actually happen, but this allows checking
387100979Srwatson			 * that all enumerated values are handled.
388100979Srwatson			 */
389100979Srwatson			break;
390100979Srwatson		case MAC_DESTROY:
391100979Srwatson			mpc->mpc_ops->mpo_destroy =
392100979Srwatson			    mpe->mpe_function;
393100979Srwatson			break;
394100979Srwatson		case MAC_INIT:
395100979Srwatson			mpc->mpc_ops->mpo_init =
396100979Srwatson			    mpe->mpe_function;
397100979Srwatson			break;
398102123Srwatson		case MAC_SYSCALL:
399102123Srwatson			mpc->mpc_ops->mpo_syscall =
400102123Srwatson			    mpe->mpe_function;
401102123Srwatson			break;
402104514Srwatson		case MAC_INIT_BPFDESC_LABEL:
403104514Srwatson			mpc->mpc_ops->mpo_init_bpfdesc_label =
404100979Srwatson			    mpe->mpe_function;
405100979Srwatson			break;
406104514Srwatson		case MAC_INIT_CRED_LABEL:
407104514Srwatson			mpc->mpc_ops->mpo_init_cred_label =
408100979Srwatson			    mpe->mpe_function;
409100979Srwatson			break;
410104514Srwatson		case MAC_INIT_DEVFSDIRENT_LABEL:
411104514Srwatson			mpc->mpc_ops->mpo_init_devfsdirent_label =
412100979Srwatson			    mpe->mpe_function;
413100979Srwatson			break;
414104514Srwatson		case MAC_INIT_IFNET_LABEL:
415104514Srwatson			mpc->mpc_ops->mpo_init_ifnet_label =
416100979Srwatson			    mpe->mpe_function;
417100979Srwatson			break;
418104514Srwatson		case MAC_INIT_IPQ_LABEL:
419104514Srwatson			mpc->mpc_ops->mpo_init_ipq_label =
420100979Srwatson			    mpe->mpe_function;
421100979Srwatson			break;
422104514Srwatson		case MAC_INIT_MBUF_LABEL:
423104514Srwatson			mpc->mpc_ops->mpo_init_mbuf_label =
424100979Srwatson			    mpe->mpe_function;
425100979Srwatson			break;
426104514Srwatson		case MAC_INIT_MOUNT_LABEL:
427104514Srwatson			mpc->mpc_ops->mpo_init_mount_label =
428100979Srwatson			    mpe->mpe_function;
429100979Srwatson			break;
430104514Srwatson		case MAC_INIT_MOUNT_FS_LABEL:
431104514Srwatson			mpc->mpc_ops->mpo_init_mount_fs_label =
432100979Srwatson			    mpe->mpe_function;
433100979Srwatson			break;
434104514Srwatson		case MAC_INIT_PIPE_LABEL:
435104514Srwatson			mpc->mpc_ops->mpo_init_pipe_label =
436100979Srwatson			    mpe->mpe_function;
437100979Srwatson			break;
438104514Srwatson		case MAC_INIT_SOCKET_LABEL:
439104514Srwatson			mpc->mpc_ops->mpo_init_socket_label =
440100979Srwatson			    mpe->mpe_function;
441100979Srwatson			break;
442104514Srwatson		case MAC_INIT_SOCKET_PEER_LABEL:
443104514Srwatson			mpc->mpc_ops->mpo_init_socket_peer_label =
444100979Srwatson			    mpe->mpe_function;
445100979Srwatson			break;
446104514Srwatson		case MAC_INIT_TEMP_LABEL:
447104514Srwatson			mpc->mpc_ops->mpo_init_temp_label =
448100979Srwatson			    mpe->mpe_function;
449100979Srwatson			break;
450104514Srwatson		case MAC_INIT_VNODE_LABEL:
451104514Srwatson			mpc->mpc_ops->mpo_init_vnode_label =
452100979Srwatson			    mpe->mpe_function;
453100979Srwatson			break;
454104514Srwatson		case MAC_DESTROY_BPFDESC_LABEL:
455104514Srwatson			mpc->mpc_ops->mpo_destroy_bpfdesc_label =
456100979Srwatson			    mpe->mpe_function;
457100979Srwatson			break;
458104514Srwatson		case MAC_DESTROY_CRED_LABEL:
459104514Srwatson			mpc->mpc_ops->mpo_destroy_cred_label =
460100979Srwatson			    mpe->mpe_function;
461100979Srwatson			break;
462104514Srwatson		case MAC_DESTROY_DEVFSDIRENT_LABEL:
463104514Srwatson			mpc->mpc_ops->mpo_destroy_devfsdirent_label =
464100979Srwatson			    mpe->mpe_function;
465100979Srwatson			break;
466104514Srwatson		case MAC_DESTROY_IFNET_LABEL:
467104514Srwatson			mpc->mpc_ops->mpo_destroy_ifnet_label =
468100979Srwatson			    mpe->mpe_function;
469100979Srwatson			break;
470104514Srwatson		case MAC_DESTROY_IPQ_LABEL:
471104514Srwatson			mpc->mpc_ops->mpo_destroy_ipq_label =
472100979Srwatson			    mpe->mpe_function;
473100979Srwatson			break;
474104514Srwatson		case MAC_DESTROY_MBUF_LABEL:
475104514Srwatson			mpc->mpc_ops->mpo_destroy_mbuf_label =
476100979Srwatson			    mpe->mpe_function;
477100979Srwatson			break;
478104514Srwatson		case MAC_DESTROY_MOUNT_LABEL:
479104514Srwatson			mpc->mpc_ops->mpo_destroy_mount_label =
480100979Srwatson			    mpe->mpe_function;
481100979Srwatson			break;
482104514Srwatson		case MAC_DESTROY_MOUNT_FS_LABEL:
483104514Srwatson			mpc->mpc_ops->mpo_destroy_mount_fs_label =
484100979Srwatson			    mpe->mpe_function;
485100979Srwatson			break;
486104514Srwatson		case MAC_DESTROY_PIPE_LABEL:
487104514Srwatson			mpc->mpc_ops->mpo_destroy_pipe_label =
488100979Srwatson			    mpe->mpe_function;
489100979Srwatson			break;
490104514Srwatson		case MAC_DESTROY_SOCKET_LABEL:
491104514Srwatson			mpc->mpc_ops->mpo_destroy_socket_label =
492104514Srwatson			    mpe->mpe_function;
493104514Srwatson			break;
494104514Srwatson		case MAC_DESTROY_SOCKET_PEER_LABEL:
495104514Srwatson			mpc->mpc_ops->mpo_destroy_socket_peer_label =
496104514Srwatson			    mpe->mpe_function;
497104514Srwatson			break;
498104514Srwatson		case MAC_DESTROY_TEMP_LABEL:
499104514Srwatson			mpc->mpc_ops->mpo_destroy_temp_label =
500104514Srwatson			    mpe->mpe_function;
501104514Srwatson			break;
502104514Srwatson		case MAC_DESTROY_VNODE_LABEL:
503104514Srwatson			mpc->mpc_ops->mpo_destroy_vnode_label =
504104514Srwatson			    mpe->mpe_function;
505104514Srwatson			break;
506100979Srwatson		case MAC_EXTERNALIZE:
507100979Srwatson			mpc->mpc_ops->mpo_externalize =
508100979Srwatson			    mpe->mpe_function;
509100979Srwatson			break;
510100979Srwatson		case MAC_INTERNALIZE:
511100979Srwatson			mpc->mpc_ops->mpo_internalize =
512100979Srwatson			    mpe->mpe_function;
513100979Srwatson			break;
514100979Srwatson		case MAC_CREATE_DEVFS_DEVICE:
515100979Srwatson			mpc->mpc_ops->mpo_create_devfs_device =
516100979Srwatson			    mpe->mpe_function;
517100979Srwatson			break;
518100979Srwatson		case MAC_CREATE_DEVFS_DIRECTORY:
519100979Srwatson			mpc->mpc_ops->mpo_create_devfs_directory =
520100979Srwatson			    mpe->mpe_function;
521100979Srwatson			break;
522100979Srwatson		case MAC_CREATE_DEVFS_VNODE:
523100979Srwatson			mpc->mpc_ops->mpo_create_devfs_vnode =
524100979Srwatson			    mpe->mpe_function;
525100979Srwatson			break;
526100979Srwatson		case MAC_STDCREATEVNODE_EA:
527100979Srwatson			mpc->mpc_ops->mpo_stdcreatevnode_ea =
528100979Srwatson			    mpe->mpe_function;
529100979Srwatson			break;
530100979Srwatson		case MAC_CREATE_VNODE:
531100979Srwatson			mpc->mpc_ops->mpo_create_vnode =
532100979Srwatson			    mpe->mpe_function;
533100979Srwatson			break;
534100979Srwatson		case MAC_CREATE_MOUNT:
535100979Srwatson			mpc->mpc_ops->mpo_create_mount =
536100979Srwatson			    mpe->mpe_function;
537100979Srwatson			break;
538100979Srwatson		case MAC_CREATE_ROOT_MOUNT:
539100979Srwatson			mpc->mpc_ops->mpo_create_root_mount =
540100979Srwatson			    mpe->mpe_function;
541100979Srwatson			break;
542100979Srwatson		case MAC_RELABEL_VNODE:
543100979Srwatson			mpc->mpc_ops->mpo_relabel_vnode =
544100979Srwatson			    mpe->mpe_function;
545100979Srwatson			break;
546100979Srwatson		case MAC_UPDATE_DEVFSDIRENT:
547100979Srwatson			mpc->mpc_ops->mpo_update_devfsdirent =
548100979Srwatson			    mpe->mpe_function;
549100979Srwatson			break;
550100979Srwatson		case MAC_UPDATE_PROCFSVNODE:
551100979Srwatson			mpc->mpc_ops->mpo_update_procfsvnode =
552100979Srwatson			    mpe->mpe_function;
553100979Srwatson			break;
554100979Srwatson		case MAC_UPDATE_VNODE_FROM_EXTATTR:
555100979Srwatson			mpc->mpc_ops->mpo_update_vnode_from_extattr =
556100979Srwatson			    mpe->mpe_function;
557100979Srwatson			break;
558100979Srwatson		case MAC_UPDATE_VNODE_FROM_EXTERNALIZED:
559100979Srwatson			mpc->mpc_ops->mpo_update_vnode_from_externalized =
560100979Srwatson			    mpe->mpe_function;
561100979Srwatson			break;
562100979Srwatson		case MAC_UPDATE_VNODE_FROM_MOUNT:
563100979Srwatson			mpc->mpc_ops->mpo_update_vnode_from_mount =
564100979Srwatson			    mpe->mpe_function;
565100979Srwatson			break;
566100979Srwatson		case MAC_CREATE_MBUF_FROM_SOCKET:
567100979Srwatson			mpc->mpc_ops->mpo_create_mbuf_from_socket =
568100979Srwatson			    mpe->mpe_function;
569100979Srwatson			break;
570100979Srwatson		case MAC_CREATE_PIPE:
571100979Srwatson			mpc->mpc_ops->mpo_create_pipe =
572100979Srwatson			    mpe->mpe_function;
573100979Srwatson			break;
574100979Srwatson		case MAC_CREATE_SOCKET:
575100979Srwatson			mpc->mpc_ops->mpo_create_socket =
576100979Srwatson			    mpe->mpe_function;
577100979Srwatson			break;
578100979Srwatson		case MAC_CREATE_SOCKET_FROM_SOCKET:
579100979Srwatson			mpc->mpc_ops->mpo_create_socket_from_socket =
580100979Srwatson			    mpe->mpe_function;
581100979Srwatson			break;
582100979Srwatson		case MAC_RELABEL_PIPE:
583100979Srwatson			mpc->mpc_ops->mpo_relabel_pipe =
584100979Srwatson			    mpe->mpe_function;
585100979Srwatson			break;
586100979Srwatson		case MAC_RELABEL_SOCKET:
587100979Srwatson			mpc->mpc_ops->mpo_relabel_socket =
588100979Srwatson			    mpe->mpe_function;
589100979Srwatson			break;
590100979Srwatson		case MAC_SET_SOCKET_PEER_FROM_MBUF:
591100979Srwatson			mpc->mpc_ops->mpo_set_socket_peer_from_mbuf =
592100979Srwatson			    mpe->mpe_function;
593100979Srwatson			break;
594100979Srwatson		case MAC_SET_SOCKET_PEER_FROM_SOCKET:
595100979Srwatson			mpc->mpc_ops->mpo_set_socket_peer_from_socket =
596100979Srwatson			    mpe->mpe_function;
597100979Srwatson			break;
598100979Srwatson		case MAC_CREATE_BPFDESC:
599100979Srwatson			mpc->mpc_ops->mpo_create_bpfdesc =
600100979Srwatson			    mpe->mpe_function;
601100979Srwatson			break;
602100979Srwatson		case MAC_CREATE_DATAGRAM_FROM_IPQ:
603100979Srwatson			mpc->mpc_ops->mpo_create_datagram_from_ipq =
604100979Srwatson			    mpe->mpe_function;
605100979Srwatson			break;
606100979Srwatson		case MAC_CREATE_FRAGMENT:
607100979Srwatson			mpc->mpc_ops->mpo_create_fragment =
608100979Srwatson			    mpe->mpe_function;
609100979Srwatson			break;
610100979Srwatson		case MAC_CREATE_IFNET:
611100979Srwatson			mpc->mpc_ops->mpo_create_ifnet =
612100979Srwatson			    mpe->mpe_function;
613100979Srwatson			break;
614100979Srwatson		case MAC_CREATE_IPQ:
615100979Srwatson			mpc->mpc_ops->mpo_create_ipq =
616100979Srwatson			    mpe->mpe_function;
617100979Srwatson			break;
618100979Srwatson		case MAC_CREATE_MBUF_FROM_MBUF:
619100979Srwatson			mpc->mpc_ops->mpo_create_mbuf_from_mbuf =
620100979Srwatson			    mpe->mpe_function;
621100979Srwatson			break;
622100979Srwatson		case MAC_CREATE_MBUF_LINKLAYER:
623100979Srwatson			mpc->mpc_ops->mpo_create_mbuf_linklayer =
624100979Srwatson			    mpe->mpe_function;
625100979Srwatson			break;
626100979Srwatson		case MAC_CREATE_MBUF_FROM_BPFDESC:
627100979Srwatson			mpc->mpc_ops->mpo_create_mbuf_from_bpfdesc =
628100979Srwatson			    mpe->mpe_function;
629100979Srwatson			break;
630100979Srwatson		case MAC_CREATE_MBUF_FROM_IFNET:
631100979Srwatson			mpc->mpc_ops->mpo_create_mbuf_from_ifnet =
632100979Srwatson			    mpe->mpe_function;
633100979Srwatson			break;
634100979Srwatson		case MAC_CREATE_MBUF_MULTICAST_ENCAP:
635100979Srwatson			mpc->mpc_ops->mpo_create_mbuf_multicast_encap =
636100979Srwatson			    mpe->mpe_function;
637100979Srwatson			break;
638100979Srwatson		case MAC_CREATE_MBUF_NETLAYER:
639100979Srwatson			mpc->mpc_ops->mpo_create_mbuf_netlayer =
640100979Srwatson			    mpe->mpe_function;
641100979Srwatson			break;
642100979Srwatson		case MAC_FRAGMENT_MATCH:
643100979Srwatson			mpc->mpc_ops->mpo_fragment_match =
644100979Srwatson			    mpe->mpe_function;
645100979Srwatson			break;
646100979Srwatson		case MAC_RELABEL_IFNET:
647100979Srwatson			mpc->mpc_ops->mpo_relabel_ifnet =
648100979Srwatson			    mpe->mpe_function;
649100979Srwatson			break;
650100979Srwatson		case MAC_UPDATE_IPQ:
651100979Srwatson			mpc->mpc_ops->mpo_update_ipq =
652100979Srwatson			    mpe->mpe_function;
653100979Srwatson			break;
654100979Srwatson		case MAC_CREATE_CRED:
655100979Srwatson			mpc->mpc_ops->mpo_create_cred =
656100979Srwatson			    mpe->mpe_function;
657100979Srwatson			break;
658100979Srwatson		case MAC_EXECVE_TRANSITION:
659100979Srwatson			mpc->mpc_ops->mpo_execve_transition =
660100979Srwatson			    mpe->mpe_function;
661100979Srwatson			break;
662100979Srwatson		case MAC_EXECVE_WILL_TRANSITION:
663100979Srwatson			mpc->mpc_ops->mpo_execve_will_transition =
664100979Srwatson			    mpe->mpe_function;
665100979Srwatson			break;
666100979Srwatson		case MAC_CREATE_PROC0:
667104518Srwatson			mpc->mpc_ops->mpo_create_proc0 =
668104518Srwatson			    mpe->mpe_function;
669100979Srwatson			break;
670100979Srwatson		case MAC_CREATE_PROC1:
671104518Srwatson			mpc->mpc_ops->mpo_create_proc1 =
672104518Srwatson			    mpe->mpe_function;
673100979Srwatson			break;
674100979Srwatson		case MAC_RELABEL_CRED:
675100979Srwatson			mpc->mpc_ops->mpo_relabel_cred =
676100979Srwatson			    mpe->mpe_function;
677100979Srwatson			break;
678104338Srwatson		case MAC_THREAD_USERRET:
679104338Srwatson			mpc->mpc_ops->mpo_thread_userret =
680104338Srwatson			    mpe->mpe_function;
681104338Srwatson			break;
682100979Srwatson		case MAC_CHECK_BPFDESC_RECEIVE:
683100979Srwatson			mpc->mpc_ops->mpo_check_bpfdesc_receive =
684100979Srwatson			    mpe->mpe_function;
685100979Srwatson			break;
686100979Srwatson		case MAC_CHECK_CRED_RELABEL:
687100979Srwatson			mpc->mpc_ops->mpo_check_cred_relabel =
688100979Srwatson			    mpe->mpe_function;
689100979Srwatson			break;
690100979Srwatson		case MAC_CHECK_CRED_VISIBLE:
691100979Srwatson			mpc->mpc_ops->mpo_check_cred_visible =
692100979Srwatson			    mpe->mpe_function;
693100979Srwatson			break;
694100979Srwatson		case MAC_CHECK_IFNET_RELABEL:
695100979Srwatson			mpc->mpc_ops->mpo_check_ifnet_relabel =
696100979Srwatson			    mpe->mpe_function;
697100979Srwatson			break;
698100979Srwatson		case MAC_CHECK_IFNET_TRANSMIT:
699100979Srwatson			mpc->mpc_ops->mpo_check_ifnet_transmit =
700100979Srwatson			    mpe->mpe_function;
701100979Srwatson			break;
702100979Srwatson		case MAC_CHECK_MOUNT_STAT:
703100979Srwatson			mpc->mpc_ops->mpo_check_mount_stat =
704100979Srwatson			    mpe->mpe_function;
705100979Srwatson			break;
706100979Srwatson		case MAC_CHECK_PIPE_IOCTL:
707100979Srwatson			mpc->mpc_ops->mpo_check_pipe_ioctl =
708100979Srwatson			    mpe->mpe_function;
709100979Srwatson			break;
710102115Srwatson		case MAC_CHECK_PIPE_POLL:
711102115Srwatson			mpc->mpc_ops->mpo_check_pipe_poll =
712100979Srwatson			    mpe->mpe_function;
713100979Srwatson			break;
714102115Srwatson		case MAC_CHECK_PIPE_READ:
715102115Srwatson			mpc->mpc_ops->mpo_check_pipe_read =
716102115Srwatson			    mpe->mpe_function;
717102115Srwatson			break;
718100979Srwatson		case MAC_CHECK_PIPE_RELABEL:
719100979Srwatson			mpc->mpc_ops->mpo_check_pipe_relabel =
720100979Srwatson			    mpe->mpe_function;
721100979Srwatson			break;
722102115Srwatson		case MAC_CHECK_PIPE_STAT:
723102115Srwatson			mpc->mpc_ops->mpo_check_pipe_stat =
724102115Srwatson			    mpe->mpe_function;
725102115Srwatson			break;
726102115Srwatson		case MAC_CHECK_PIPE_WRITE:
727102115Srwatson			mpc->mpc_ops->mpo_check_pipe_write =
728102115Srwatson			    mpe->mpe_function;
729102115Srwatson			break;
730100979Srwatson		case MAC_CHECK_PROC_DEBUG:
731100979Srwatson			mpc->mpc_ops->mpo_check_proc_debug =
732100979Srwatson			    mpe->mpe_function;
733100979Srwatson			break;
734100979Srwatson		case MAC_CHECK_PROC_SCHED:
735100979Srwatson			mpc->mpc_ops->mpo_check_proc_sched =
736100979Srwatson			    mpe->mpe_function;
737100979Srwatson			break;
738100979Srwatson		case MAC_CHECK_PROC_SIGNAL:
739100979Srwatson			mpc->mpc_ops->mpo_check_proc_signal =
740100979Srwatson			    mpe->mpe_function;
741100979Srwatson			break;
742100979Srwatson		case MAC_CHECK_SOCKET_BIND:
743100979Srwatson			mpc->mpc_ops->mpo_check_socket_bind =
744100979Srwatson			    mpe->mpe_function;
745100979Srwatson			break;
746100979Srwatson		case MAC_CHECK_SOCKET_CONNECT:
747100979Srwatson			mpc->mpc_ops->mpo_check_socket_connect =
748100979Srwatson			    mpe->mpe_function;
749100979Srwatson			break;
750101933Srwatson		case MAC_CHECK_SOCKET_DELIVER:
751101933Srwatson			mpc->mpc_ops->mpo_check_socket_deliver =
752101933Srwatson			    mpe->mpe_function;
753101933Srwatson			break;
754100979Srwatson		case MAC_CHECK_SOCKET_LISTEN:
755100979Srwatson			mpc->mpc_ops->mpo_check_socket_listen =
756100979Srwatson			    mpe->mpe_function;
757100979Srwatson			break;
758100979Srwatson		case MAC_CHECK_SOCKET_RELABEL:
759100979Srwatson			mpc->mpc_ops->mpo_check_socket_relabel =
760100979Srwatson			    mpe->mpe_function;
761100979Srwatson			break;
762100979Srwatson		case MAC_CHECK_SOCKET_VISIBLE:
763100979Srwatson			mpc->mpc_ops->mpo_check_socket_visible =
764100979Srwatson			    mpe->mpe_function;
765100979Srwatson			break;
766100979Srwatson		case MAC_CHECK_VNODE_ACCESS:
767100979Srwatson			mpc->mpc_ops->mpo_check_vnode_access =
768100979Srwatson			    mpe->mpe_function;
769100979Srwatson			break;
770100979Srwatson		case MAC_CHECK_VNODE_CHDIR:
771100979Srwatson			mpc->mpc_ops->mpo_check_vnode_chdir =
772100979Srwatson			    mpe->mpe_function;
773100979Srwatson			break;
774100979Srwatson		case MAC_CHECK_VNODE_CHROOT:
775100979Srwatson			mpc->mpc_ops->mpo_check_vnode_chroot =
776100979Srwatson			    mpe->mpe_function;
777100979Srwatson			break;
778100979Srwatson		case MAC_CHECK_VNODE_CREATE:
779100979Srwatson			mpc->mpc_ops->mpo_check_vnode_create =
780100979Srwatson			    mpe->mpe_function;
781100979Srwatson			break;
782100979Srwatson		case MAC_CHECK_VNODE_DELETE:
783100979Srwatson			mpc->mpc_ops->mpo_check_vnode_delete =
784100979Srwatson			    mpe->mpe_function;
785100979Srwatson			break;
786100979Srwatson		case MAC_CHECK_VNODE_DELETEACL:
787100979Srwatson			mpc->mpc_ops->mpo_check_vnode_deleteacl =
788100979Srwatson			    mpe->mpe_function;
789100979Srwatson			break;
790100979Srwatson		case MAC_CHECK_VNODE_EXEC:
791100979Srwatson			mpc->mpc_ops->mpo_check_vnode_exec =
792100979Srwatson			    mpe->mpe_function;
793100979Srwatson			break;
794100979Srwatson		case MAC_CHECK_VNODE_GETACL:
795100979Srwatson			mpc->mpc_ops->mpo_check_vnode_getacl =
796100979Srwatson			    mpe->mpe_function;
797100979Srwatson			break;
798100979Srwatson		case MAC_CHECK_VNODE_GETEXTATTR:
799100979Srwatson			mpc->mpc_ops->mpo_check_vnode_getextattr =
800100979Srwatson			    mpe->mpe_function;
801100979Srwatson			break;
802104529Srwatson		case MAC_CHECK_VNODE_LINK:
803104529Srwatson			mpc->mpc_ops->mpo_check_vnode_link =
804104529Srwatson			    mpe->mpe_function;
805104529Srwatson			break;
806100979Srwatson		case MAC_CHECK_VNODE_LOOKUP:
807100979Srwatson			mpc->mpc_ops->mpo_check_vnode_lookup =
808100979Srwatson			    mpe->mpe_function;
809100979Srwatson			break;
810100979Srwatson		case MAC_CHECK_VNODE_MMAP_PERMS:
811100979Srwatson			mpc->mpc_ops->mpo_check_vnode_mmap_perms =
812100979Srwatson			    mpe->mpe_function;
813100979Srwatson			break;
814100979Srwatson		case MAC_CHECK_VNODE_OPEN:
815100979Srwatson			mpc->mpc_ops->mpo_check_vnode_open =
816100979Srwatson			    mpe->mpe_function;
817100979Srwatson			break;
818102112Srwatson		case MAC_CHECK_VNODE_POLL:
819102112Srwatson			mpc->mpc_ops->mpo_check_vnode_poll =
820102112Srwatson			    mpe->mpe_function;
821102112Srwatson			break;
822102112Srwatson		case MAC_CHECK_VNODE_READ:
823102112Srwatson			mpc->mpc_ops->mpo_check_vnode_read =
824102112Srwatson			    mpe->mpe_function;
825102112Srwatson			break;
826100979Srwatson		case MAC_CHECK_VNODE_READDIR:
827100979Srwatson			mpc->mpc_ops->mpo_check_vnode_readdir =
828100979Srwatson			    mpe->mpe_function;
829100979Srwatson			break;
830100979Srwatson		case MAC_CHECK_VNODE_READLINK:
831100979Srwatson			mpc->mpc_ops->mpo_check_vnode_readlink =
832100979Srwatson			    mpe->mpe_function;
833100979Srwatson			break;
834100979Srwatson		case MAC_CHECK_VNODE_RELABEL:
835100979Srwatson			mpc->mpc_ops->mpo_check_vnode_relabel =
836100979Srwatson			    mpe->mpe_function;
837100979Srwatson			break;
838100979Srwatson		case MAC_CHECK_VNODE_RENAME_FROM:
839100979Srwatson			mpc->mpc_ops->mpo_check_vnode_rename_from =
840100979Srwatson			    mpe->mpe_function;
841100979Srwatson			break;
842100979Srwatson		case MAC_CHECK_VNODE_RENAME_TO:
843100979Srwatson			mpc->mpc_ops->mpo_check_vnode_rename_to =
844100979Srwatson			    mpe->mpe_function;
845100979Srwatson			break;
846100979Srwatson		case MAC_CHECK_VNODE_REVOKE:
847100979Srwatson			mpc->mpc_ops->mpo_check_vnode_revoke =
848100979Srwatson			    mpe->mpe_function;
849100979Srwatson			break;
850100979Srwatson		case MAC_CHECK_VNODE_SETACL:
851100979Srwatson			mpc->mpc_ops->mpo_check_vnode_setacl =
852100979Srwatson			    mpe->mpe_function;
853100979Srwatson			break;
854100979Srwatson		case MAC_CHECK_VNODE_SETEXTATTR:
855100979Srwatson			mpc->mpc_ops->mpo_check_vnode_setextattr =
856100979Srwatson			    mpe->mpe_function;
857100979Srwatson			break;
858100979Srwatson		case MAC_CHECK_VNODE_SETFLAGS:
859100979Srwatson			mpc->mpc_ops->mpo_check_vnode_setflags =
860100979Srwatson			    mpe->mpe_function;
861100979Srwatson			break;
862100979Srwatson		case MAC_CHECK_VNODE_SETMODE:
863100979Srwatson			mpc->mpc_ops->mpo_check_vnode_setmode =
864100979Srwatson			    mpe->mpe_function;
865100979Srwatson			break;
866100979Srwatson		case MAC_CHECK_VNODE_SETOWNER:
867100979Srwatson			mpc->mpc_ops->mpo_check_vnode_setowner =
868100979Srwatson			    mpe->mpe_function;
869100979Srwatson			break;
870100979Srwatson		case MAC_CHECK_VNODE_SETUTIMES:
871100979Srwatson			mpc->mpc_ops->mpo_check_vnode_setutimes =
872100979Srwatson			    mpe->mpe_function;
873100979Srwatson			break;
874100979Srwatson		case MAC_CHECK_VNODE_STAT:
875100979Srwatson			mpc->mpc_ops->mpo_check_vnode_stat =
876100979Srwatson			    mpe->mpe_function;
877100979Srwatson			break;
878102112Srwatson		case MAC_CHECK_VNODE_WRITE:
879102112Srwatson			mpc->mpc_ops->mpo_check_vnode_write =
880102112Srwatson			    mpe->mpe_function;
881102112Srwatson			break;
882100979Srwatson/*
883100979Srwatson		default:
884100979Srwatson			printf("MAC policy `%s': unknown operation %d\n",
885100979Srwatson			    mpc->mpc_name, mpe->mpe_constant);
886100979Srwatson			return (EINVAL);
887100979Srwatson*/
888100979Srwatson		}
889100979Srwatson	}
890100979Srwatson	MAC_POLICY_LIST_LOCK();
891100979Srwatson	if (mac_policy_list_busy > 0) {
892100979Srwatson		MAC_POLICY_LIST_UNLOCK();
893100979Srwatson		FREE(mpc->mpc_ops, M_MACOPVEC);
894100979Srwatson		mpc->mpc_ops = NULL;
895100979Srwatson		return (EBUSY);
896100979Srwatson	}
897100979Srwatson	LIST_FOREACH(tmpc, &mac_policy_list, mpc_list) {
898100979Srwatson		if (strcmp(tmpc->mpc_name, mpc->mpc_name) == 0) {
899100979Srwatson			MAC_POLICY_LIST_UNLOCK();
900100979Srwatson			FREE(mpc->mpc_ops, M_MACOPVEC);
901100979Srwatson			mpc->mpc_ops = NULL;
902100979Srwatson			return (EEXIST);
903100979Srwatson		}
904100979Srwatson	}
905100979Srwatson	if (mpc->mpc_field_off != NULL) {
906100979Srwatson		slot = ffs(mac_policy_offsets_free);
907100979Srwatson		if (slot == 0) {
908100979Srwatson			MAC_POLICY_LIST_UNLOCK();
909100979Srwatson			FREE(mpc->mpc_ops, M_MACOPVEC);
910100979Srwatson			mpc->mpc_ops = NULL;
911100979Srwatson			return (ENOMEM);
912100979Srwatson		}
913100979Srwatson		slot--;
914100979Srwatson		mac_policy_offsets_free &= ~(1 << slot);
915100979Srwatson		*mpc->mpc_field_off = slot;
916100979Srwatson	}
917100979Srwatson	mpc->mpc_runtime_flags |= MPC_RUNTIME_FLAG_REGISTERED;
918100979Srwatson	LIST_INSERT_HEAD(&mac_policy_list, mpc, mpc_list);
919100979Srwatson
920100979Srwatson	/* Per-policy initialization. */
921100979Srwatson	if (mpc->mpc_ops->mpo_init != NULL)
922100979Srwatson		(*(mpc->mpc_ops->mpo_init))(mpc);
923100979Srwatson	MAC_POLICY_LIST_UNLOCK();
924100979Srwatson
925100979Srwatson	printf("Security policy loaded: %s (%s)\n", mpc->mpc_fullname,
926100979Srwatson	    mpc->mpc_name);
927100979Srwatson
928100979Srwatson	return (0);
929100979Srwatson}
930100979Srwatson
931100979Srwatsonstatic int
932100979Srwatsonmac_policy_unregister(struct mac_policy_conf *mpc)
933100979Srwatson{
934100979Srwatson
935104520Srwatson	/*
936104520Srwatson	 * If we fail the load, we may get a request to unload.  Check
937104520Srwatson	 * to see if we did the run-time registration, and if not,
938104520Srwatson	 * silently succeed.
939104520Srwatson	 */
940104520Srwatson	MAC_POLICY_LIST_LOCK();
941104520Srwatson	if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED) == 0) {
942104520Srwatson		MAC_POLICY_LIST_UNLOCK();
943104520Srwatson		return (0);
944104520Srwatson	}
945100979Srwatson#if 0
946100979Srwatson	/*
947100979Srwatson	 * Don't allow unloading modules with private data.
948100979Srwatson	 */
949104520Srwatson	if (mpc->mpc_field_off != NULL) {
950104520Srwatson		MAC_POLICY_LIST_UNLOCK();
951100979Srwatson		return (EBUSY);
952104520Srwatson	}
953100979Srwatson#endif
954104520Srwatson	/*
955104520Srwatson	 * Only allow the unload to proceed if the module is unloadable
956104520Srwatson	 * by its own definition.
957104520Srwatson	 */
958104520Srwatson	if ((mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK) == 0) {
959104520Srwatson		MAC_POLICY_LIST_UNLOCK();
960100979Srwatson		return (EBUSY);
961104520Srwatson	}
962104520Srwatson	/*
963104520Srwatson	 * Right now, we EBUSY if the list is in use.  In the future,
964104520Srwatson	 * for reliability reasons, we might want to sleep and wakeup
965104520Srwatson	 * later to try again.
966104520Srwatson	 */
967100979Srwatson	if (mac_policy_list_busy > 0) {
968100979Srwatson		MAC_POLICY_LIST_UNLOCK();
969100979Srwatson		return (EBUSY);
970100979Srwatson	}
971100979Srwatson	if (mpc->mpc_ops->mpo_destroy != NULL)
972100979Srwatson		(*(mpc->mpc_ops->mpo_destroy))(mpc);
973100979Srwatson
974100979Srwatson	LIST_REMOVE(mpc, mpc_list);
975100979Srwatson	MAC_POLICY_LIST_UNLOCK();
976100979Srwatson
977100979Srwatson	FREE(mpc->mpc_ops, M_MACOPVEC);
978100979Srwatson	mpc->mpc_ops = NULL;
979100979Srwatson
980100979Srwatson	printf("Security policy unload: %s (%s)\n", mpc->mpc_fullname,
981100979Srwatson	    mpc->mpc_name);
982100979Srwatson
983100979Srwatson	return (0);
984100979Srwatson}
985100979Srwatson
986100979Srwatson/*
987100979Srwatson * Define an error value precedence, and given two arguments, selects the
988100979Srwatson * value with the higher precedence.
989100979Srwatson */
990100979Srwatsonstatic int
991100979Srwatsonerror_select(int error1, int error2)
992100979Srwatson{
993100979Srwatson
994100979Srwatson	/* Certain decision-making errors take top priority. */
995100979Srwatson	if (error1 == EDEADLK || error2 == EDEADLK)
996100979Srwatson		return (EDEADLK);
997100979Srwatson
998100979Srwatson	/* Invalid arguments should be reported where possible. */
999100979Srwatson	if (error1 == EINVAL || error2 == EINVAL)
1000100979Srwatson		return (EINVAL);
1001100979Srwatson
1002100979Srwatson	/* Precedence goes to "visibility", with both process and file. */
1003100979Srwatson	if (error1 == ESRCH || error2 == ESRCH)
1004100979Srwatson		return (ESRCH);
1005100979Srwatson
1006100979Srwatson	if (error1 == ENOENT || error2 == ENOENT)
1007100979Srwatson		return (ENOENT);
1008100979Srwatson
1009100979Srwatson	/* Precedence goes to DAC/MAC protections. */
1010100979Srwatson	if (error1 == EACCES || error2 == EACCES)
1011100979Srwatson		return (EACCES);
1012100979Srwatson
1013100979Srwatson	/* Precedence goes to privilege. */
1014100979Srwatson	if (error1 == EPERM || error2 == EPERM)
1015100979Srwatson		return (EPERM);
1016100979Srwatson
1017100979Srwatson	/* Precedence goes to error over success; otherwise, arbitrary. */
1018100979Srwatson	if (error1 != 0)
1019100979Srwatson		return (error1);
1020100979Srwatson	return (error2);
1021100979Srwatson}
1022100979Srwatson
1023104521Srwatsonstatic void
1024104521Srwatsonmac_init_label(struct label *label)
1025104521Srwatson{
1026104521Srwatson
1027104521Srwatson	bzero(label, sizeof(*label));
1028104521Srwatson	label->l_flags = MAC_FLAG_INITIALIZED;
1029104521Srwatson}
1030104521Srwatson
1031104521Srwatsonstatic void
1032104521Srwatsonmac_destroy_label(struct label *label)
1033104521Srwatson{
1034104521Srwatson
1035104521Srwatson	KASSERT(label->l_flags & MAC_FLAG_INITIALIZED,
1036104521Srwatson	    ("destroying uninitialized label"));
1037104521Srwatson
1038104521Srwatson	bzero(label, sizeof(*label));
1039104521Srwatson	/* implicit: label->l_flags &= ~MAC_FLAG_INITIALIZED; */
1040104521Srwatson}
1041104521Srwatson
1042104521Srwatsonstatic void
1043104521Srwatsonmac_init_structmac(struct mac *mac)
1044104521Srwatson{
1045104521Srwatson
1046104521Srwatson	bzero(mac, sizeof(*mac));
1047104521Srwatson	mac->m_macflags = MAC_FLAG_INITIALIZED;
1048104521Srwatson}
1049104521Srwatson
1050100979Srwatsonvoid
1051104527Srwatsonmac_init_bpfdesc(struct bpf_d *bpf_d)
1052104521Srwatson{
1053104521Srwatson
1054104527Srwatson	mac_init_label(&bpf_d->bd_label);
1055104527Srwatson	MAC_PERFORM(init_bpfdesc_label, &bpf_d->bd_label);
1056104521Srwatson#ifdef MAC_DEBUG
1057104527Srwatson	atomic_add_int(&nmacbpfdescs, 1);
1058104521Srwatson#endif
1059104521Srwatson}
1060104521Srwatson
1061104521Srwatsonvoid
1062104521Srwatsonmac_init_cred(struct ucred *cr)
1063104521Srwatson{
1064104521Srwatson
1065104521Srwatson	mac_init_label(&cr->cr_label);
1066104521Srwatson	MAC_PERFORM(init_cred_label, &cr->cr_label);
1067104521Srwatson#ifdef MAC_DEBUG
1068104521Srwatson	atomic_add_int(&nmaccreds, 1);
1069104521Srwatson#endif
1070104521Srwatson}
1071104521Srwatson
1072104521Srwatsonvoid
1073104527Srwatsonmac_init_devfsdirent(struct devfs_dirent *de)
1074104521Srwatson{
1075104521Srwatson
1076104527Srwatson	mac_init_label(&de->de_label);
1077104527Srwatson	MAC_PERFORM(init_devfsdirent_label, &de->de_label);
1078104521Srwatson#ifdef MAC_DEBUG
1079104527Srwatson	atomic_add_int(&nmacdevfsdirents, 1);
1080104521Srwatson#endif
1081104521Srwatson}
1082104521Srwatson
1083104521Srwatsonvoid
1084104521Srwatsonmac_init_ifnet(struct ifnet *ifp)
1085104521Srwatson{
1086104521Srwatson
1087104521Srwatson	mac_init_label(&ifp->if_label);
1088104521Srwatson	MAC_PERFORM(init_ifnet_label, &ifp->if_label);
1089104521Srwatson#ifdef MAC_DEBUG
1090104521Srwatson	atomic_add_int(&nmacifnets, 1);
1091104521Srwatson#endif
1092104521Srwatson}
1093104521Srwatson
1094104521Srwatsonvoid
1095104527Srwatsonmac_init_ipq(struct ipq *ipq)
1096104521Srwatson{
1097104521Srwatson
1098104527Srwatson	mac_init_label(&ipq->ipq_label);
1099104527Srwatson	MAC_PERFORM(init_ipq_label, &ipq->ipq_label);
1100104521Srwatson#ifdef MAC_DEBUG
1101104527Srwatson	atomic_add_int(&nmacipqs, 1);
1102104521Srwatson#endif
1103104521Srwatson}
1104104521Srwatson
1105104527Srwatsonint
1106104527Srwatsonmac_init_mbuf(struct mbuf *m, int flag)
1107104527Srwatson{
1108104528Srwatson	int error;
1109104528Srwatson
1110104527Srwatson	KASSERT(m->m_flags & M_PKTHDR, ("mac_init_mbuf on non-header mbuf"));
1111104527Srwatson
1112104527Srwatson	mac_init_label(&m->m_pkthdr.label);
1113104527Srwatson
1114104528Srwatson	MAC_CHECK(init_mbuf_label, &m->m_pkthdr.label, flag);
1115104528Srwatson	if (error) {
1116104528Srwatson		MAC_PERFORM(destroy_mbuf_label, &m->m_pkthdr.label);
1117104528Srwatson		mac_destroy_label(&m->m_pkthdr.label);
1118104528Srwatson	}
1119104528Srwatson
1120104527Srwatson#ifdef MAC_DEBUG
1121104528Srwatson	if (error == 0)
1122104528Srwatson		atomic_add_int(&nmacmbufs, 1);
1123104527Srwatson#endif
1124104528Srwatson	return (error);
1125104527Srwatson}
1126104527Srwatson
1127104521Srwatsonvoid
1128104527Srwatsonmac_init_mount(struct mount *mp)
1129104521Srwatson{
1130104521Srwatson
1131104527Srwatson	mac_init_label(&mp->mnt_mntlabel);
1132104527Srwatson	mac_init_label(&mp->mnt_fslabel);
1133104527Srwatson	MAC_PERFORM(init_mount_label, &mp->mnt_mntlabel);
1134104527Srwatson	MAC_PERFORM(init_mount_fs_label, &mp->mnt_fslabel);
1135104521Srwatson#ifdef MAC_DEBUG
1136104527Srwatson	atomic_add_int(&nmacmounts, 1);
1137104521Srwatson#endif
1138104521Srwatson}
1139104521Srwatson
1140104521Srwatsonvoid
1141104527Srwatsonmac_init_pipe(struct pipe *pipe)
1142104521Srwatson{
1143104527Srwatson	struct label *label;
1144104521Srwatson
1145104527Srwatson	label = malloc(sizeof(struct label), M_MACPIPELABEL, M_ZERO|M_WAITOK);
1146104527Srwatson	mac_init_label(label);
1147104527Srwatson	pipe->pipe_label = label;
1148104527Srwatson	pipe->pipe_peer->pipe_label = label;
1149104527Srwatson	MAC_PERFORM(init_pipe_label, pipe->pipe_label);
1150104521Srwatson#ifdef MAC_DEBUG
1151104527Srwatson	atomic_add_int(&nmacpipes, 1);
1152104521Srwatson#endif
1153104521Srwatson}
1154104521Srwatson
1155104521Srwatsonvoid
1156104521Srwatsonmac_init_socket(struct socket *socket)
1157104521Srwatson{
1158104521Srwatson
1159104521Srwatson	mac_init_label(&socket->so_label);
1160104521Srwatson	mac_init_label(&socket->so_peerlabel);
1161104521Srwatson	MAC_PERFORM(init_socket_label, &socket->so_label);
1162104521Srwatson	MAC_PERFORM(init_socket_peer_label, &socket->so_peerlabel);
1163104521Srwatson#ifdef MAC_DEBUG
1164104521Srwatson	atomic_add_int(&nmacsockets, 1);
1165104521Srwatson#endif
1166104521Srwatson}
1167104521Srwatson
1168104527Srwatsonstatic void
1169104527Srwatsonmac_init_temp(struct label *label)
1170104521Srwatson{
1171104521Srwatson
1172104527Srwatson	mac_init_label(label);
1173104527Srwatson	MAC_PERFORM(init_temp_label, label);
1174104521Srwatson#ifdef MAC_DEBUG
1175104527Srwatson	atomic_add_int(&nmactemp, 1);
1176104521Srwatson#endif
1177104521Srwatson}
1178104521Srwatson
1179104521Srwatsonvoid
1180104527Srwatsonmac_init_vnode(struct vnode *vp)
1181104521Srwatson{
1182104521Srwatson
1183104527Srwatson	mac_init_label(&vp->v_label);
1184104527Srwatson	MAC_PERFORM(init_vnode_label, &vp->v_label);
1185104521Srwatson#ifdef MAC_DEBUG
1186104527Srwatson	atomic_add_int(&nmacvnodes, 1);
1187104521Srwatson#endif
1188104521Srwatson}
1189104521Srwatson
1190104521Srwatsonvoid
1191104527Srwatsonmac_destroy_bpfdesc(struct bpf_d *bpf_d)
1192104521Srwatson{
1193104521Srwatson
1194104527Srwatson	MAC_PERFORM(destroy_bpfdesc_label, &bpf_d->bd_label);
1195104527Srwatson	mac_destroy_label(&bpf_d->bd_label);
1196104521Srwatson#ifdef MAC_DEBUG
1197104527Srwatson	atomic_subtract_int(&nmacbpfdescs, 1);
1198104521Srwatson#endif
1199104521Srwatson}
1200104521Srwatson
1201104521Srwatsonvoid
1202104527Srwatsonmac_destroy_cred(struct ucred *cr)
1203104521Srwatson{
1204104521Srwatson
1205104527Srwatson	MAC_PERFORM(destroy_cred_label, &cr->cr_label);
1206104527Srwatson	mac_destroy_label(&cr->cr_label);
1207104521Srwatson#ifdef MAC_DEBUG
1208104527Srwatson	atomic_subtract_int(&nmaccreds, 1);
1209104521Srwatson#endif
1210104521Srwatson}
1211104521Srwatson
1212104521Srwatsonvoid
1213104527Srwatsonmac_destroy_devfsdirent(struct devfs_dirent *de)
1214104521Srwatson{
1215104521Srwatson
1216104527Srwatson	MAC_PERFORM(destroy_devfsdirent_label, &de->de_label);
1217104527Srwatson	mac_destroy_label(&de->de_label);
1218104521Srwatson#ifdef MAC_DEBUG
1219104527Srwatson	atomic_subtract_int(&nmacdevfsdirents, 1);
1220104521Srwatson#endif
1221104521Srwatson}
1222104521Srwatson
1223104521Srwatsonvoid
1224104527Srwatsonmac_destroy_ifnet(struct ifnet *ifp)
1225104521Srwatson{
1226104521Srwatson
1227104527Srwatson	MAC_PERFORM(destroy_ifnet_label, &ifp->if_label);
1228104527Srwatson	mac_destroy_label(&ifp->if_label);
1229104521Srwatson#ifdef MAC_DEBUG
1230104527Srwatson	atomic_subtract_int(&nmacifnets, 1);
1231104521Srwatson#endif
1232104521Srwatson}
1233104521Srwatson
1234104521Srwatsonvoid
1235104527Srwatsonmac_destroy_ipq(struct ipq *ipq)
1236104521Srwatson{
1237104521Srwatson
1238104527Srwatson	MAC_PERFORM(destroy_ipq_label, &ipq->ipq_label);
1239104527Srwatson	mac_destroy_label(&ipq->ipq_label);
1240104521Srwatson#ifdef MAC_DEBUG
1241104527Srwatson	atomic_subtract_int(&nmacipqs, 1);
1242104521Srwatson#endif
1243104521Srwatson}
1244104521Srwatson
1245104527Srwatsonvoid
1246104527Srwatsonmac_destroy_mbuf(struct mbuf *m)
1247104521Srwatson{
1248104521Srwatson
1249104527Srwatson	MAC_PERFORM(destroy_mbuf_label, &m->m_pkthdr.label);
1250104527Srwatson	mac_destroy_label(&m->m_pkthdr.label);
1251104521Srwatson#ifdef MAC_DEBUG
1252104527Srwatson	atomic_subtract_int(&nmacmbufs, 1);
1253104521Srwatson#endif
1254104521Srwatson}
1255104521Srwatson
1256104527Srwatsonvoid
1257104527Srwatsonmac_destroy_mount(struct mount *mp)
1258104521Srwatson{
1259104521Srwatson
1260104527Srwatson	MAC_PERFORM(destroy_mount_label, &mp->mnt_mntlabel);
1261104527Srwatson	MAC_PERFORM(destroy_mount_fs_label, &mp->mnt_fslabel);
1262104527Srwatson	mac_destroy_label(&mp->mnt_fslabel);
1263104527Srwatson	mac_destroy_label(&mp->mnt_mntlabel);
1264104521Srwatson#ifdef MAC_DEBUG
1265104527Srwatson	atomic_subtract_int(&nmacmounts, 1);
1266104521Srwatson#endif
1267104521Srwatson}
1268104521Srwatson
1269104521Srwatsonvoid
1270104527Srwatsonmac_destroy_pipe(struct pipe *pipe)
1271104521Srwatson{
1272104521Srwatson
1273104527Srwatson	MAC_PERFORM(destroy_pipe_label, pipe->pipe_label);
1274104527Srwatson	mac_destroy_label(pipe->pipe_label);
1275104527Srwatson	free(pipe->pipe_label, M_MACPIPELABEL);
1276104521Srwatson#ifdef MAC_DEBUG
1277104527Srwatson	atomic_subtract_int(&nmacpipes, 1);
1278104521Srwatson#endif
1279104521Srwatson}
1280104521Srwatson
1281104521Srwatsonvoid
1282104527Srwatsonmac_destroy_socket(struct socket *socket)
1283104521Srwatson{
1284104521Srwatson
1285104527Srwatson	MAC_PERFORM(destroy_socket_label, &socket->so_label);
1286104527Srwatson	MAC_PERFORM(destroy_socket_peer_label, &socket->so_peerlabel);
1287104527Srwatson	mac_destroy_label(&socket->so_label);
1288104527Srwatson	mac_destroy_label(&socket->so_peerlabel);
1289104521Srwatson#ifdef MAC_DEBUG
1290104527Srwatson	atomic_subtract_int(&nmacsockets, 1);
1291104521Srwatson#endif
1292104521Srwatson}
1293104521Srwatson
1294104527Srwatsonstatic void
1295104527Srwatsonmac_destroy_temp(struct label *label)
1296104521Srwatson{
1297104521Srwatson
1298104527Srwatson	MAC_PERFORM(destroy_temp_label, label);
1299104527Srwatson	mac_destroy_label(label);
1300104521Srwatson#ifdef MAC_DEBUG
1301104527Srwatson	atomic_subtract_int(&nmactemp, 1);
1302104521Srwatson#endif
1303104521Srwatson}
1304104521Srwatson
1305104521Srwatsonvoid
1306104527Srwatsonmac_destroy_vnode(struct vnode *vp)
1307104521Srwatson{
1308104521Srwatson
1309104527Srwatson	MAC_PERFORM(destroy_vnode_label, &vp->v_label);
1310104527Srwatson	mac_destroy_label(&vp->v_label);
1311104521Srwatson#ifdef MAC_DEBUG
1312104527Srwatson	atomic_subtract_int(&nmacvnodes, 1);
1313104521Srwatson#endif
1314104521Srwatson}
1315104521Srwatson
1316104522Srwatsonstatic int
1317104522Srwatsonmac_externalize(struct label *label, struct mac *mac)
1318104522Srwatson{
1319104522Srwatson	int error;
1320104522Srwatson
1321104522Srwatson	mac_init_structmac(mac);
1322104522Srwatson	MAC_CHECK(externalize, label, mac);
1323104522Srwatson
1324104522Srwatson	return (error);
1325104522Srwatson}
1326104522Srwatson
1327104522Srwatsonstatic int
1328104522Srwatsonmac_internalize(struct label *label, struct mac *mac)
1329104522Srwatson{
1330104522Srwatson	int error;
1331104522Srwatson
1332104522Srwatson	mac_init_temp(label);
1333104522Srwatson	MAC_CHECK(internalize, label, mac);
1334104522Srwatson	if (error)
1335104522Srwatson		mac_destroy_temp(label);
1336104522Srwatson
1337104522Srwatson	return (error);
1338104522Srwatson}
1339104522Srwatson
1340104522Srwatson/*
1341104522Srwatson * Initialize MAC label for the first kernel process, from which other
1342104522Srwatson * kernel processes and threads are spawned.
1343104522Srwatson */
1344104521Srwatsonvoid
1345104522Srwatsonmac_create_proc0(struct ucred *cred)
1346104522Srwatson{
1347104522Srwatson
1348104522Srwatson	MAC_PERFORM(create_proc0, cred);
1349104522Srwatson}
1350104522Srwatson
1351104522Srwatson/*
1352104522Srwatson * Initialize MAC label for the first userland process, from which other
1353104522Srwatson * userland processes and threads are spawned.
1354104522Srwatson */
1355104522Srwatsonvoid
1356104522Srwatsonmac_create_proc1(struct ucred *cred)
1357104522Srwatson{
1358104522Srwatson
1359104522Srwatson	MAC_PERFORM(create_proc1, cred);
1360104522Srwatson}
1361104522Srwatson
1362104522Srwatsonvoid
1363104522Srwatsonmac_thread_userret(struct thread *td)
1364104522Srwatson{
1365104522Srwatson
1366104522Srwatson	MAC_PERFORM(thread_userret, td);
1367104522Srwatson}
1368104522Srwatson
1369104522Srwatson/*
1370104522Srwatson * When a new process is created, its label must be initialized.  Generally,
1371104522Srwatson * this involves inheritence from the parent process, modulo possible
1372104522Srwatson * deltas.  This function allows that processing to take place.
1373104522Srwatson */
1374104522Srwatsonvoid
1375104522Srwatsonmac_create_cred(struct ucred *parent_cred, struct ucred *child_cred)
1376104522Srwatson{
1377104522Srwatson
1378104522Srwatson	MAC_PERFORM(create_cred, parent_cred, child_cred);
1379104522Srwatson}
1380104522Srwatson
1381104522Srwatsonvoid
1382100979Srwatsonmac_update_devfsdirent(struct devfs_dirent *de, struct vnode *vp)
1383100979Srwatson{
1384100979Srwatson
1385100979Srwatson	MAC_PERFORM(update_devfsdirent, de, &de->de_label, vp, &vp->v_label);
1386100979Srwatson}
1387100979Srwatson
1388100979Srwatsonvoid
1389100979Srwatsonmac_update_procfsvnode(struct vnode *vp, struct ucred *cred)
1390100979Srwatson{
1391100979Srwatson
1392100979Srwatson	MAC_PERFORM(update_procfsvnode, vp, &vp->v_label, cred);
1393100979Srwatson}
1394100979Srwatson
1395100979Srwatson/*
1396100979Srwatson * Support callout for policies that manage their own externalization
1397100979Srwatson * using extended attributes.
1398100979Srwatson */
1399100979Srwatsonstatic int
1400100979Srwatsonmac_update_vnode_from_extattr(struct vnode *vp, struct mount *mp)
1401100979Srwatson{
1402100979Srwatson	int error;
1403100979Srwatson
1404100979Srwatson	MAC_CHECK(update_vnode_from_extattr, vp, &vp->v_label, mp,
1405100979Srwatson	    &mp->mnt_fslabel);
1406100979Srwatson
1407100979Srwatson	return (error);
1408100979Srwatson}
1409100979Srwatson
1410100979Srwatson/*
1411100979Srwatson * Given an externalized mac label, internalize it and stamp it on a
1412100979Srwatson * vnode.
1413100979Srwatson */
1414100979Srwatsonstatic int
1415100979Srwatsonmac_update_vnode_from_externalized(struct vnode *vp, struct mac *extmac)
1416100979Srwatson{
1417100979Srwatson	int error;
1418100979Srwatson
1419100979Srwatson	MAC_CHECK(update_vnode_from_externalized, vp, &vp->v_label, extmac);
1420100979Srwatson
1421100979Srwatson	return (error);
1422100979Srwatson}
1423100979Srwatson
1424100979Srwatson/*
1425100979Srwatson * Call out to individual policies to update the label in a vnode from
1426100979Srwatson * the mountpoint.
1427100979Srwatson */
1428100979Srwatsonvoid
1429100979Srwatsonmac_update_vnode_from_mount(struct vnode *vp, struct mount *mp)
1430100979Srwatson{
1431100979Srwatson
1432100979Srwatson	MAC_PERFORM(update_vnode_from_mount, vp, &vp->v_label, mp,
1433100979Srwatson	    &mp->mnt_fslabel);
1434100979Srwatson
1435101308Sjeff	ASSERT_VOP_LOCKED(vp, "mac_update_vnode_from_mount");
1436100979Srwatson	if (mac_cache_fslabel_in_vnode)
1437101308Sjeff		vp->v_vflag |= VV_CACHEDLABEL;
1438100979Srwatson}
1439100979Srwatson
1440100979Srwatson/*
1441100979Srwatson * Implementation of VOP_REFRESHLABEL() that relies on extended attributes
1442100979Srwatson * to store label data.  Can be referenced by filesystems supporting
1443100979Srwatson * extended attributes.
1444100979Srwatson */
1445100979Srwatsonint
1446100979Srwatsonvop_stdrefreshlabel_ea(struct vop_refreshlabel_args *ap)
1447100979Srwatson{
1448100979Srwatson	struct vnode *vp = ap->a_vp;
1449100979Srwatson	struct mac extmac;
1450100979Srwatson	int buflen, error;
1451100979Srwatson
1452100979Srwatson	ASSERT_VOP_LOCKED(vp, "vop_stdrefreshlabel_ea");
1453100979Srwatson
1454100979Srwatson	/*
1455100979Srwatson	 * Call out to external policies first.  Order doesn't really
1456100979Srwatson	 * matter, as long as failure of one assures failure of all.
1457100979Srwatson	 */
1458100979Srwatson	error = mac_update_vnode_from_extattr(vp, vp->v_mount);
1459100979Srwatson	if (error)
1460100979Srwatson		return (error);
1461100979Srwatson
1462100979Srwatson	buflen = sizeof(extmac);
1463100979Srwatson	error = vn_extattr_get(vp, IO_NODELOCKED,
1464100979Srwatson	    FREEBSD_MAC_EXTATTR_NAMESPACE, FREEBSD_MAC_EXTATTR_NAME, &buflen,
1465100979Srwatson	    (char *)&extmac, curthread);
1466100979Srwatson	switch (error) {
1467100979Srwatson	case 0:
1468100979Srwatson		/* Got it */
1469100979Srwatson		break;
1470100979Srwatson
1471100979Srwatson	case ENOATTR:
1472100979Srwatson		/*
1473100979Srwatson		 * Use the label from the mount point.
1474100979Srwatson		 */
1475100979Srwatson		mac_update_vnode_from_mount(vp, vp->v_mount);
1476100979Srwatson		return (0);
1477100979Srwatson
1478100979Srwatson	case EOPNOTSUPP:
1479100979Srwatson	default:
1480100979Srwatson		/* Fail horribly. */
1481100979Srwatson		return (error);
1482100979Srwatson	}
1483100979Srwatson
1484100979Srwatson	if (buflen != sizeof(extmac))
1485100979Srwatson		error = EPERM;		/* Fail very closed. */
1486100979Srwatson	if (error == 0)
1487100979Srwatson		error = mac_update_vnode_from_externalized(vp, &extmac);
1488100979Srwatson	if (error == 0)
1489101308Sjeff		vp->v_vflag |= VV_CACHEDLABEL;
1490100979Srwatson	else {
1491100979Srwatson		struct vattr va;
1492100979Srwatson
1493100979Srwatson		printf("Corrupted label on %s",
1494100979Srwatson		    vp->v_mount->mnt_stat.f_mntonname);
1495100979Srwatson		if (VOP_GETATTR(vp, &va, curthread->td_ucred, curthread) == 0)
1496100979Srwatson			printf(" inum %ld", va.va_fileid);
1497104268Srwatson#ifdef MAC_DEBUG
1498100979Srwatson		if (mac_debug_label_fallback) {
1499100979Srwatson			printf(", falling back.\n");
1500100979Srwatson			mac_update_vnode_from_mount(vp, vp->v_mount);
1501100979Srwatson			error = 0;
1502100979Srwatson		} else {
1503104268Srwatson#endif
1504100979Srwatson			printf(".\n");
1505100979Srwatson			error = EPERM;
1506104268Srwatson#ifdef MAC_DEBUG
1507100979Srwatson		}
1508104268Srwatson#endif
1509100979Srwatson	}
1510100979Srwatson
1511100979Srwatson	return (error);
1512100979Srwatson}
1513100979Srwatson
1514100979Srwatson/*
1515100979Srwatson * Make sure the vnode label is up-to-date.  If EOPNOTSUPP, then we handle
1516100979Srwatson * the labeling activity outselves.  Filesystems should be careful not
1517100979Srwatson * to change their minds regarding whether they support vop_refreshlabel()
1518100979Srwatson * for a vnode or not.  Don't cache the vnode here, allow the file
1519100979Srwatson * system code to determine if it's safe to cache.  If we update from
1520100979Srwatson * the mount, don't cache since a change to the mount label should affect
1521100979Srwatson * all vnodes.
1522100979Srwatson */
1523100979Srwatsonstatic int
1524100979Srwatsonvn_refreshlabel(struct vnode *vp, struct ucred *cred)
1525100979Srwatson{
1526100979Srwatson	int error;
1527100979Srwatson
1528100979Srwatson	ASSERT_VOP_LOCKED(vp, "vn_refreshlabel");
1529100979Srwatson
1530100979Srwatson	if (vp->v_mount == NULL) {
1531100979Srwatson/*
1532100979Srwatson		Eventually, we probably want to special-case refreshing
1533100979Srwatson		of deadfs vnodes, and if there's a lock-free race somewhere,
1534100979Srwatson		that case might be handled here.
1535100979Srwatson
1536100979Srwatson		mac_update_vnode_deadfs(vp);
1537100979Srwatson		return (0);
1538100979Srwatson */
1539100979Srwatson		/* printf("vn_refreshlabel: null v_mount\n"); */
1540103314Snjl		if (vp->v_type != VNON)
1541100979Srwatson			printf(
1542103314Snjl			    "vn_refreshlabel: null v_mount with non-VNON\n");
1543100979Srwatson		return (EBADF);
1544100979Srwatson	}
1545100979Srwatson
1546101308Sjeff	if (vp->v_vflag & VV_CACHEDLABEL) {
1547100979Srwatson		mac_vnode_label_cache_hits++;
1548100979Srwatson		return (0);
1549100979Srwatson	} else
1550100979Srwatson		mac_vnode_label_cache_misses++;
1551100979Srwatson
1552100979Srwatson	if ((vp->v_mount->mnt_flag & MNT_MULTILABEL) == 0) {
1553100979Srwatson		mac_update_vnode_from_mount(vp, vp->v_mount);
1554100979Srwatson		return (0);
1555100979Srwatson	}
1556100979Srwatson
1557100979Srwatson	error = VOP_REFRESHLABEL(vp, cred, curthread);
1558100979Srwatson	switch (error) {
1559100979Srwatson	case EOPNOTSUPP:
1560100979Srwatson		/*
1561100979Srwatson		 * If labels are not supported on this vnode, fall back to
1562100979Srwatson		 * the label in the mount and propagate it to the vnode.
1563100979Srwatson		 * There should probably be some sort of policy/flag/decision
1564100979Srwatson		 * about doing this.
1565100979Srwatson		 */
1566100979Srwatson		mac_update_vnode_from_mount(vp, vp->v_mount);
1567100979Srwatson		error = 0;
1568100979Srwatson	default:
1569100979Srwatson		return (error);
1570100979Srwatson	}
1571100979Srwatson}
1572100979Srwatson
1573100979Srwatson/*
1574100979Srwatson * Helper function for file systems using the vop_std*_ea() calls.  This
1575100979Srwatson * function must be called after EA service is available for the vnode,
1576100979Srwatson * but before it's hooked up to the namespace so that the node persists
1577100979Srwatson * if there's a crash, or before it can be accessed.  On successful
1578100979Srwatson * commit of the label to disk (etc), do cache the label.
1579100979Srwatson */
1580100979Srwatsonint
1581100979Srwatsonvop_stdcreatevnode_ea(struct vnode *dvp, struct vnode *tvp, struct ucred *cred)
1582100979Srwatson{
1583100979Srwatson	struct mac extmac;
1584100979Srwatson	int error;
1585100979Srwatson
1586101308Sjeff	ASSERT_VOP_LOCKED(tvp, "vop_stdcreatevnode_ea");
1587100979Srwatson	if ((dvp->v_mount->mnt_flag & MNT_MULTILABEL) == 0) {
1588100979Srwatson		mac_update_vnode_from_mount(tvp, tvp->v_mount);
1589100979Srwatson	} else {
1590100979Srwatson		error = vn_refreshlabel(dvp, cred);
1591100979Srwatson		if (error)
1592100979Srwatson			return (error);
1593100979Srwatson
1594100979Srwatson		/*
1595100979Srwatson		 * Stick the label in the vnode.  Then try to write to
1596100979Srwatson		 * disk.  If we fail, return a failure to abort the
1597100979Srwatson		 * create operation.  Really, this failure shouldn't
1598100979Srwatson		 * happen except in fairly unusual circumstances (out
1599100979Srwatson		 * of disk, etc).
1600100979Srwatson		 */
1601100979Srwatson		mac_create_vnode(cred, dvp, tvp);
1602100979Srwatson
1603100979Srwatson		error = mac_stdcreatevnode_ea(tvp);
1604100979Srwatson		if (error)
1605100979Srwatson			return (error);
1606100979Srwatson
1607100979Srwatson		/*
1608100979Srwatson		 * XXX: Eventually this will go away and all policies will
1609100979Srwatson		 * directly manage their extended attributes.
1610100979Srwatson		 */
1611100979Srwatson		error = mac_externalize(&tvp->v_label, &extmac);
1612100979Srwatson		if (error)
1613100979Srwatson			return (error);
1614100979Srwatson
1615100979Srwatson		error = vn_extattr_set(tvp, IO_NODELOCKED,
1616100979Srwatson		    FREEBSD_MAC_EXTATTR_NAMESPACE, FREEBSD_MAC_EXTATTR_NAME,
1617100979Srwatson		    sizeof(extmac), (char *)&extmac, curthread);
1618100979Srwatson		if (error == 0)
1619101308Sjeff			tvp->v_vflag |= VV_CACHEDLABEL;
1620100979Srwatson		else {
1621100979Srwatson#if 0
1622100979Srwatson			/*
1623100979Srwatson			 * In theory, we could have fall-back behavior here.
1624100979Srwatson			 * It would probably be incorrect.
1625100979Srwatson			 */
1626100979Srwatson#endif
1627100979Srwatson			return (error);
1628100979Srwatson		}
1629100979Srwatson	}
1630100979Srwatson
1631100979Srwatson	return (0);
1632100979Srwatson}
1633100979Srwatson
1634100979Srwatsonvoid
1635100979Srwatsonmac_execve_transition(struct ucred *old, struct ucred *new, struct vnode *vp)
1636100979Srwatson{
1637100979Srwatson	int error;
1638100979Srwatson
1639100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_execve_transition");
1640100979Srwatson
1641100979Srwatson	error = vn_refreshlabel(vp, old);
1642100979Srwatson	if (error) {
1643100979Srwatson		printf("mac_execve_transition: vn_refreshlabel returned %d\n",
1644100979Srwatson		    error);
1645100979Srwatson		printf("mac_execve_transition: using old vnode label\n");
1646100979Srwatson	}
1647100979Srwatson
1648100979Srwatson	MAC_PERFORM(execve_transition, old, new, vp, &vp->v_label);
1649100979Srwatson}
1650100979Srwatson
1651100979Srwatsonint
1652100979Srwatsonmac_execve_will_transition(struct ucred *old, struct vnode *vp)
1653100979Srwatson{
1654100979Srwatson	int error, result;
1655100979Srwatson
1656100979Srwatson	error = vn_refreshlabel(vp, old);
1657100979Srwatson	if (error)
1658100979Srwatson		return (error);
1659100979Srwatson
1660100979Srwatson	result = 0;
1661100979Srwatson	MAC_BOOLEAN(execve_will_transition, ||, old, vp, &vp->v_label);
1662100979Srwatson
1663100979Srwatson	return (result);
1664100979Srwatson}
1665100979Srwatson
1666100979Srwatsonint
1667100979Srwatsonmac_check_vnode_access(struct ucred *cred, struct vnode *vp, int flags)
1668100979Srwatson{
1669100979Srwatson	int error;
1670100979Srwatson
1671100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_access");
1672100979Srwatson
1673100979Srwatson	if (!mac_enforce_fs)
1674100979Srwatson		return (0);
1675100979Srwatson
1676100979Srwatson	error = vn_refreshlabel(vp, cred);
1677100979Srwatson	if (error)
1678100979Srwatson		return (error);
1679100979Srwatson
1680100979Srwatson	MAC_CHECK(check_vnode_access, cred, vp, &vp->v_label, flags);
1681100979Srwatson	return (error);
1682100979Srwatson}
1683100979Srwatson
1684100979Srwatsonint
1685100979Srwatsonmac_check_vnode_chdir(struct ucred *cred, struct vnode *dvp)
1686100979Srwatson{
1687100979Srwatson	int error;
1688100979Srwatson
1689100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_chdir");
1690100979Srwatson
1691100979Srwatson	if (!mac_enforce_fs)
1692100979Srwatson		return (0);
1693100979Srwatson
1694100979Srwatson	error = vn_refreshlabel(dvp, cred);
1695100979Srwatson	if (error)
1696100979Srwatson		return (error);
1697100979Srwatson
1698100979Srwatson	MAC_CHECK(check_vnode_chdir, cred, dvp, &dvp->v_label);
1699100979Srwatson	return (error);
1700100979Srwatson}
1701100979Srwatson
1702100979Srwatsonint
1703100979Srwatsonmac_check_vnode_chroot(struct ucred *cred, struct vnode *dvp)
1704100979Srwatson{
1705100979Srwatson	int error;
1706100979Srwatson
1707100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_chroot");
1708100979Srwatson
1709100979Srwatson	if (!mac_enforce_fs)
1710100979Srwatson		return (0);
1711100979Srwatson
1712100979Srwatson	error = vn_refreshlabel(dvp, cred);
1713100979Srwatson	if (error)
1714100979Srwatson		return (error);
1715100979Srwatson
1716100979Srwatson	MAC_CHECK(check_vnode_chroot, cred, dvp, &dvp->v_label);
1717100979Srwatson	return (error);
1718100979Srwatson}
1719100979Srwatson
1720100979Srwatsonint
1721100979Srwatsonmac_check_vnode_create(struct ucred *cred, struct vnode *dvp,
1722100979Srwatson    struct componentname *cnp, struct vattr *vap)
1723100979Srwatson{
1724100979Srwatson	int error;
1725100979Srwatson
1726100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_create");
1727100979Srwatson
1728100979Srwatson	if (!mac_enforce_fs)
1729100979Srwatson		return (0);
1730100979Srwatson
1731100979Srwatson	error = vn_refreshlabel(dvp, cred);
1732100979Srwatson	if (error)
1733100979Srwatson		return (error);
1734100979Srwatson
1735100979Srwatson	MAC_CHECK(check_vnode_create, cred, dvp, &dvp->v_label, cnp, vap);
1736100979Srwatson	return (error);
1737100979Srwatson}
1738100979Srwatson
1739100979Srwatsonint
1740100979Srwatsonmac_check_vnode_delete(struct ucred *cred, struct vnode *dvp, struct vnode *vp,
1741100979Srwatson    struct componentname *cnp)
1742100979Srwatson{
1743100979Srwatson	int error;
1744100979Srwatson
1745100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_delete");
1746100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_delete");
1747100979Srwatson
1748100979Srwatson	if (!mac_enforce_fs)
1749100979Srwatson		return (0);
1750100979Srwatson
1751100979Srwatson	error = vn_refreshlabel(dvp, cred);
1752100979Srwatson	if (error)
1753100979Srwatson		return (error);
1754100979Srwatson	error = vn_refreshlabel(vp, cred);
1755100979Srwatson	if (error)
1756100979Srwatson		return (error);
1757100979Srwatson
1758100979Srwatson	MAC_CHECK(check_vnode_delete, cred, dvp, &dvp->v_label, vp,
1759100979Srwatson	    &vp->v_label, cnp);
1760100979Srwatson	return (error);
1761100979Srwatson}
1762100979Srwatson
1763100979Srwatsonint
1764100979Srwatsonmac_check_vnode_deleteacl(struct ucred *cred, struct vnode *vp,
1765100979Srwatson    acl_type_t type)
1766100979Srwatson{
1767100979Srwatson	int error;
1768100979Srwatson
1769100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_deleteacl");
1770100979Srwatson
1771100979Srwatson	if (!mac_enforce_fs)
1772100979Srwatson		return (0);
1773100979Srwatson
1774100979Srwatson	error = vn_refreshlabel(vp, cred);
1775100979Srwatson	if (error)
1776100979Srwatson		return (error);
1777100979Srwatson
1778100979Srwatson	MAC_CHECK(check_vnode_deleteacl, cred, vp, &vp->v_label, type);
1779100979Srwatson	return (error);
1780100979Srwatson}
1781100979Srwatson
1782100979Srwatsonint
1783100979Srwatsonmac_check_vnode_exec(struct ucred *cred, struct vnode *vp)
1784100979Srwatson{
1785100979Srwatson	int error;
1786100979Srwatson
1787102102Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_exec");
1788102102Srwatson
1789100979Srwatson	if (!mac_enforce_process && !mac_enforce_fs)
1790100979Srwatson		return (0);
1791100979Srwatson
1792100979Srwatson	error = vn_refreshlabel(vp, cred);
1793100979Srwatson	if (error)
1794100979Srwatson		return (error);
1795100979Srwatson	MAC_CHECK(check_vnode_exec, cred, vp, &vp->v_label);
1796100979Srwatson
1797100979Srwatson	return (error);
1798100979Srwatson}
1799100979Srwatson
1800100979Srwatsonint
1801100979Srwatsonmac_check_vnode_getacl(struct ucred *cred, struct vnode *vp, acl_type_t type)
1802100979Srwatson{
1803100979Srwatson	int error;
1804100979Srwatson
1805100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_getacl");
1806100979Srwatson
1807100979Srwatson	if (!mac_enforce_fs)
1808100979Srwatson		return (0);
1809100979Srwatson
1810100979Srwatson	error = vn_refreshlabel(vp, cred);
1811100979Srwatson	if (error)
1812100979Srwatson		return (error);
1813100979Srwatson
1814100979Srwatson	MAC_CHECK(check_vnode_getacl, cred, vp, &vp->v_label, type);
1815100979Srwatson	return (error);
1816100979Srwatson}
1817100979Srwatson
1818100979Srwatsonint
1819100979Srwatsonmac_check_vnode_getextattr(struct ucred *cred, struct vnode *vp,
1820100979Srwatson    int attrnamespace, const char *name, struct uio *uio)
1821100979Srwatson{
1822100979Srwatson	int error;
1823100979Srwatson
1824100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_getextattr");
1825100979Srwatson
1826100979Srwatson	if (!mac_enforce_fs)
1827100979Srwatson		return (0);
1828100979Srwatson
1829100979Srwatson	error = vn_refreshlabel(vp, cred);
1830100979Srwatson	if (error)
1831100979Srwatson		return (error);
1832100979Srwatson
1833100979Srwatson	MAC_CHECK(check_vnode_getextattr, cred, vp, &vp->v_label,
1834100979Srwatson	    attrnamespace, name, uio);
1835100979Srwatson	return (error);
1836100979Srwatson}
1837100979Srwatson
1838100979Srwatsonint
1839104529Srwatsonmac_check_vnode_link(struct ucred *cred, struct vnode *dvp,
1840104529Srwatson    struct vnode *vp, struct componentname *cnp)
1841104529Srwatson{
1842104529Srwatson
1843104529Srwatson	int error;
1844104529Srwatson
1845104529Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_link");
1846104529Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_link");
1847104529Srwatson
1848104529Srwatson	if (!mac_enforce_fs)
1849104529Srwatson		return (0);
1850104529Srwatson
1851104529Srwatson	error = vn_refreshlabel(dvp, cred);
1852104529Srwatson	if (error)
1853104529Srwatson		return (error);
1854104529Srwatson
1855104529Srwatson	error = vn_refreshlabel(vp, cred);
1856104529Srwatson	if (error)
1857104529Srwatson		return (error);
1858104529Srwatson
1859104529Srwatson	MAC_CHECK(check_vnode_link, cred, dvp, &dvp->v_label, vp,
1860104529Srwatson	    &vp->v_label, cnp);
1861104529Srwatson	return (error);
1862104529Srwatson}
1863104529Srwatson
1864104529Srwatsonint
1865100979Srwatsonmac_check_vnode_lookup(struct ucred *cred, struct vnode *dvp,
1866100979Srwatson    struct componentname *cnp)
1867100979Srwatson{
1868100979Srwatson	int error;
1869100979Srwatson
1870100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_lookup");
1871100979Srwatson
1872100979Srwatson	if (!mac_enforce_fs)
1873100979Srwatson		return (0);
1874100979Srwatson
1875100979Srwatson	error = vn_refreshlabel(dvp, cred);
1876100979Srwatson	if (error)
1877100979Srwatson		return (error);
1878100979Srwatson
1879100979Srwatson	MAC_CHECK(check_vnode_lookup, cred, dvp, &dvp->v_label, cnp);
1880100979Srwatson	return (error);
1881100979Srwatson}
1882100979Srwatson
1883100979Srwatsonvm_prot_t
1884100979Srwatsonmac_check_vnode_mmap_prot(struct ucred *cred, struct vnode *vp, int newmapping)
1885100979Srwatson{
1886100979Srwatson	vm_prot_t result = VM_PROT_ALL;
1887100979Srwatson
1888103514Srwatson	if (!mac_enforce_vm)
1889103514Srwatson		return (result);
1890103514Srwatson
1891100979Srwatson	/*
1892100979Srwatson	 * This should be some sort of MAC_BITWISE, maybe :)
1893100979Srwatson	 */
1894100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mmap_perms");
1895100979Srwatson	MAC_BOOLEAN(check_vnode_mmap_perms, &, cred, vp, &vp->v_label,
1896100979Srwatson	    newmapping);
1897100979Srwatson	return (result);
1898100979Srwatson}
1899100979Srwatson
1900100979Srwatsonint
1901102112Srwatsonmac_check_vnode_open(struct ucred *cred, struct vnode *vp, mode_t acc_mode)
1902100979Srwatson{
1903100979Srwatson	int error;
1904100979Srwatson
1905102112Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_open");
1906102112Srwatson
1907100979Srwatson	if (!mac_enforce_fs)
1908100979Srwatson		return (0);
1909100979Srwatson
1910102112Srwatson	error = vn_refreshlabel(vp, cred);
1911102112Srwatson	if (error)
1912102112Srwatson		return (error);
1913100979Srwatson
1914102112Srwatson	MAC_CHECK(check_vnode_open, cred, vp, &vp->v_label, acc_mode);
1915102112Srwatson	return (error);
1916102112Srwatson}
1917102112Srwatson
1918102112Srwatsonint
1919102129Srwatsonmac_check_vnode_poll(struct ucred *active_cred, struct ucred *file_cred,
1920102129Srwatson    struct vnode *vp)
1921102112Srwatson{
1922102112Srwatson	int error;
1923102112Srwatson
1924102112Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_poll");
1925102112Srwatson
1926102112Srwatson	if (!mac_enforce_fs)
1927102112Srwatson		return (0);
1928102112Srwatson
1929102129Srwatson	error = vn_refreshlabel(vp, active_cred);
1930100979Srwatson	if (error)
1931100979Srwatson		return (error);
1932100979Srwatson
1933102129Srwatson	MAC_CHECK(check_vnode_poll, active_cred, file_cred, vp,
1934102129Srwatson	    &vp->v_label);
1935100979Srwatson
1936100979Srwatson	return (error);
1937100979Srwatson}
1938100979Srwatson
1939100979Srwatsonint
1940102129Srwatsonmac_check_vnode_read(struct ucred *active_cred, struct ucred *file_cred,
1941102129Srwatson    struct vnode *vp)
1942100979Srwatson{
1943100979Srwatson	int error;
1944100979Srwatson
1945102112Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_read");
1946100979Srwatson
1947100979Srwatson	if (!mac_enforce_fs)
1948100979Srwatson		return (0);
1949100979Srwatson
1950102129Srwatson	error = vn_refreshlabel(vp, active_cred);
1951100979Srwatson	if (error)
1952100979Srwatson		return (error);
1953100979Srwatson
1954102129Srwatson	MAC_CHECK(check_vnode_read, active_cred, file_cred, vp,
1955102129Srwatson	    &vp->v_label);
1956102112Srwatson
1957100979Srwatson	return (error);
1958100979Srwatson}
1959100979Srwatson
1960100979Srwatsonint
1961100979Srwatsonmac_check_vnode_readdir(struct ucred *cred, struct vnode *dvp)
1962100979Srwatson{
1963100979Srwatson	int error;
1964100979Srwatson
1965100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_readdir");
1966100979Srwatson
1967100979Srwatson	if (!mac_enforce_fs)
1968100979Srwatson		return (0);
1969100979Srwatson
1970100979Srwatson	error = vn_refreshlabel(dvp, cred);
1971100979Srwatson	if (error)
1972100979Srwatson		return (error);
1973100979Srwatson
1974100979Srwatson	MAC_CHECK(check_vnode_readdir, cred, dvp, &dvp->v_label);
1975100979Srwatson	return (error);
1976100979Srwatson}
1977100979Srwatson
1978100979Srwatsonint
1979100979Srwatsonmac_check_vnode_readlink(struct ucred *cred, struct vnode *vp)
1980100979Srwatson{
1981100979Srwatson	int error;
1982100979Srwatson
1983100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_readlink");
1984100979Srwatson
1985100979Srwatson	if (!mac_enforce_fs)
1986100979Srwatson		return (0);
1987100979Srwatson
1988100979Srwatson	error = vn_refreshlabel(vp, cred);
1989100979Srwatson	if (error)
1990100979Srwatson		return (error);
1991100979Srwatson
1992100979Srwatson	MAC_CHECK(check_vnode_readlink, cred, vp, &vp->v_label);
1993100979Srwatson	return (error);
1994100979Srwatson}
1995100979Srwatson
1996100979Srwatsonstatic int
1997100979Srwatsonmac_check_vnode_relabel(struct ucred *cred, struct vnode *vp,
1998100979Srwatson    struct label *newlabel)
1999100979Srwatson{
2000100979Srwatson	int error;
2001100979Srwatson
2002100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_relabel");
2003100979Srwatson
2004100979Srwatson	error = vn_refreshlabel(vp, cred);
2005100979Srwatson	if (error)
2006100979Srwatson		return (error);
2007100979Srwatson
2008100979Srwatson	MAC_CHECK(check_vnode_relabel, cred, vp, &vp->v_label, newlabel);
2009100979Srwatson
2010100979Srwatson	return (error);
2011100979Srwatson}
2012100979Srwatson
2013100979Srwatsonint
2014100979Srwatsonmac_check_vnode_rename_from(struct ucred *cred, struct vnode *dvp,
2015100979Srwatson    struct vnode *vp, struct componentname *cnp)
2016100979Srwatson{
2017100979Srwatson	int error;
2018100979Srwatson
2019100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_rename_from");
2020100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_rename_from");
2021100979Srwatson
2022100979Srwatson	if (!mac_enforce_fs)
2023100979Srwatson		return (0);
2024100979Srwatson
2025100979Srwatson	error = vn_refreshlabel(dvp, cred);
2026100979Srwatson	if (error)
2027100979Srwatson		return (error);
2028100979Srwatson	error = vn_refreshlabel(vp, cred);
2029100979Srwatson	if (error)
2030100979Srwatson		return (error);
2031100979Srwatson
2032100979Srwatson	MAC_CHECK(check_vnode_rename_from, cred, dvp, &dvp->v_label, vp,
2033100979Srwatson	    &vp->v_label, cnp);
2034100979Srwatson	return (error);
2035100979Srwatson}
2036100979Srwatson
2037100979Srwatsonint
2038100979Srwatsonmac_check_vnode_rename_to(struct ucred *cred, struct vnode *dvp,
2039100979Srwatson    struct vnode *vp, int samedir, struct componentname *cnp)
2040100979Srwatson{
2041100979Srwatson	int error;
2042100979Srwatson
2043100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_rename_to");
2044100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_rename_to");
2045100979Srwatson
2046100979Srwatson	if (!mac_enforce_fs)
2047100979Srwatson		return (0);
2048100979Srwatson
2049100979Srwatson	error = vn_refreshlabel(dvp, cred);
2050100979Srwatson	if (error)
2051100979Srwatson		return (error);
2052100979Srwatson	if (vp != NULL) {
2053100979Srwatson		error = vn_refreshlabel(vp, cred);
2054100979Srwatson		if (error)
2055100979Srwatson			return (error);
2056100979Srwatson	}
2057100979Srwatson	MAC_CHECK(check_vnode_rename_to, cred, dvp, &dvp->v_label, vp,
2058100979Srwatson	    vp != NULL ? &vp->v_label : NULL, samedir, cnp);
2059100979Srwatson	return (error);
2060100979Srwatson}
2061100979Srwatson
2062100979Srwatsonint
2063100979Srwatsonmac_check_vnode_revoke(struct ucred *cred, struct vnode *vp)
2064100979Srwatson{
2065100979Srwatson	int error;
2066100979Srwatson
2067100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_revoke");
2068100979Srwatson
2069100979Srwatson	if (!mac_enforce_fs)
2070100979Srwatson		return (0);
2071100979Srwatson
2072100979Srwatson	error = vn_refreshlabel(vp, cred);
2073100979Srwatson	if (error)
2074100979Srwatson		return (error);
2075100979Srwatson
2076100979Srwatson	MAC_CHECK(check_vnode_revoke, cred, vp, &vp->v_label);
2077100979Srwatson	return (error);
2078100979Srwatson}
2079100979Srwatson
2080100979Srwatsonint
2081100979Srwatsonmac_check_vnode_setacl(struct ucred *cred, struct vnode *vp, acl_type_t type,
2082100979Srwatson    struct acl *acl)
2083100979Srwatson{
2084100979Srwatson	int error;
2085100979Srwatson
2086100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setacl");
2087100979Srwatson
2088100979Srwatson	if (!mac_enforce_fs)
2089100979Srwatson		return (0);
2090100979Srwatson
2091100979Srwatson	error = vn_refreshlabel(vp, cred);
2092100979Srwatson	if (error)
2093100979Srwatson		return (error);
2094100979Srwatson
2095100979Srwatson	MAC_CHECK(check_vnode_setacl, cred, vp, &vp->v_label, type, acl);
2096100979Srwatson	return (error);
2097100979Srwatson}
2098100979Srwatson
2099100979Srwatsonint
2100100979Srwatsonmac_check_vnode_setextattr(struct ucred *cred, struct vnode *vp,
2101100979Srwatson    int attrnamespace, const char *name, struct uio *uio)
2102100979Srwatson{
2103100979Srwatson	int error;
2104100979Srwatson
2105100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setextattr");
2106100979Srwatson
2107100979Srwatson	if (!mac_enforce_fs)
2108100979Srwatson		return (0);
2109100979Srwatson
2110100979Srwatson	error = vn_refreshlabel(vp, cred);
2111100979Srwatson	if (error)
2112100979Srwatson		return (error);
2113100979Srwatson
2114100979Srwatson	MAC_CHECK(check_vnode_setextattr, cred, vp, &vp->v_label,
2115100979Srwatson	    attrnamespace, name, uio);
2116100979Srwatson	return (error);
2117100979Srwatson}
2118100979Srwatson
2119100979Srwatsonint
2120100979Srwatsonmac_check_vnode_setflags(struct ucred *cred, struct vnode *vp, u_long flags)
2121100979Srwatson{
2122100979Srwatson	int error;
2123100979Srwatson
2124100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setflags");
2125100979Srwatson
2126100979Srwatson	if (!mac_enforce_fs)
2127100979Srwatson		return (0);
2128100979Srwatson
2129100979Srwatson	error = vn_refreshlabel(vp, cred);
2130100979Srwatson	if (error)
2131100979Srwatson		return (error);
2132100979Srwatson
2133100979Srwatson	MAC_CHECK(check_vnode_setflags, cred, vp, &vp->v_label, flags);
2134100979Srwatson	return (error);
2135100979Srwatson}
2136100979Srwatson
2137100979Srwatsonint
2138100979Srwatsonmac_check_vnode_setmode(struct ucred *cred, struct vnode *vp, mode_t mode)
2139100979Srwatson{
2140100979Srwatson	int error;
2141100979Srwatson
2142100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setmode");
2143100979Srwatson
2144100979Srwatson	if (!mac_enforce_fs)
2145100979Srwatson		return (0);
2146100979Srwatson
2147100979Srwatson	error = vn_refreshlabel(vp, cred);
2148100979Srwatson	if (error)
2149100979Srwatson		return (error);
2150100979Srwatson
2151100979Srwatson	MAC_CHECK(check_vnode_setmode, cred, vp, &vp->v_label, mode);
2152100979Srwatson	return (error);
2153100979Srwatson}
2154100979Srwatson
2155100979Srwatsonint
2156100979Srwatsonmac_check_vnode_setowner(struct ucred *cred, struct vnode *vp, uid_t uid,
2157100979Srwatson    gid_t gid)
2158100979Srwatson{
2159100979Srwatson	int error;
2160100979Srwatson
2161100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setowner");
2162100979Srwatson
2163100979Srwatson	if (!mac_enforce_fs)
2164100979Srwatson		return (0);
2165100979Srwatson
2166100979Srwatson	error = vn_refreshlabel(vp, cred);
2167100979Srwatson	if (error)
2168100979Srwatson		return (error);
2169100979Srwatson
2170100979Srwatson	MAC_CHECK(check_vnode_setowner, cred, vp, &vp->v_label, uid, gid);
2171100979Srwatson	return (error);
2172100979Srwatson}
2173100979Srwatson
2174100979Srwatsonint
2175100979Srwatsonmac_check_vnode_setutimes(struct ucred *cred, struct vnode *vp,
2176100979Srwatson    struct timespec atime, struct timespec mtime)
2177100979Srwatson{
2178100979Srwatson	int error;
2179100979Srwatson
2180100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setutimes");
2181100979Srwatson
2182100979Srwatson	if (!mac_enforce_fs)
2183100979Srwatson		return (0);
2184100979Srwatson
2185100979Srwatson	error = vn_refreshlabel(vp, cred);
2186100979Srwatson	if (error)
2187100979Srwatson		return (error);
2188100979Srwatson
2189100979Srwatson	MAC_CHECK(check_vnode_setutimes, cred, vp, &vp->v_label, atime,
2190100979Srwatson	    mtime);
2191100979Srwatson	return (error);
2192100979Srwatson}
2193100979Srwatson
2194100979Srwatsonint
2195102129Srwatsonmac_check_vnode_stat(struct ucred *active_cred, struct ucred *file_cred,
2196102129Srwatson    struct vnode *vp)
2197100979Srwatson{
2198100979Srwatson	int error;
2199100979Srwatson
2200100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_stat");
2201100979Srwatson
2202100979Srwatson	if (!mac_enforce_fs)
2203100979Srwatson		return (0);
2204100979Srwatson
2205102129Srwatson	error = vn_refreshlabel(vp, active_cred);
2206100979Srwatson	if (error)
2207100979Srwatson		return (error);
2208100979Srwatson
2209102129Srwatson	MAC_CHECK(check_vnode_stat, active_cred, file_cred, vp,
2210102129Srwatson	    &vp->v_label);
2211100979Srwatson	return (error);
2212100979Srwatson}
2213100979Srwatson
2214102112Srwatsonint
2215102129Srwatsonmac_check_vnode_write(struct ucred *active_cred, struct ucred *file_cred,
2216102129Srwatson    struct vnode *vp)
2217102112Srwatson{
2218102112Srwatson	int error;
2219102112Srwatson
2220102112Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_write");
2221102112Srwatson
2222102112Srwatson	if (!mac_enforce_fs)
2223102112Srwatson		return (0);
2224102112Srwatson
2225102129Srwatson	error = vn_refreshlabel(vp, active_cred);
2226102112Srwatson	if (error)
2227102112Srwatson		return (error);
2228102112Srwatson
2229102129Srwatson	MAC_CHECK(check_vnode_write, active_cred, file_cred, vp,
2230102129Srwatson	    &vp->v_label);
2231102112Srwatson
2232102112Srwatson	return (error);
2233102112Srwatson}
2234102112Srwatson
2235100979Srwatson/*
2236100979Srwatson * When relabeling a process, call out to the policies for the maximum
2237100979Srwatson * permission allowed for each object type we know about in its
2238100979Srwatson * memory space, and revoke access (in the least surprising ways we
2239100979Srwatson * know) when necessary.  The process lock is not held here.
2240100979Srwatson */
2241100979Srwatsonstatic void
2242100979Srwatsonmac_cred_mmapped_drop_perms(struct thread *td, struct ucred *cred)
2243100979Srwatson{
2244100979Srwatson
2245100979Srwatson	/* XXX freeze all other threads */
2246100979Srwatson	mac_cred_mmapped_drop_perms_recurse(td, cred,
2247100979Srwatson	    &td->td_proc->p_vmspace->vm_map);
2248100979Srwatson	/* XXX allow other threads to continue */
2249100979Srwatson}
2250100979Srwatson
2251100979Srwatsonstatic __inline const char *
2252100979Srwatsonprot2str(vm_prot_t prot)
2253100979Srwatson{
2254100979Srwatson
2255100979Srwatson	switch (prot & VM_PROT_ALL) {
2256100979Srwatson	case VM_PROT_READ:
2257100979Srwatson		return ("r--");
2258100979Srwatson	case VM_PROT_READ | VM_PROT_WRITE:
2259100979Srwatson		return ("rw-");
2260100979Srwatson	case VM_PROT_READ | VM_PROT_EXECUTE:
2261100979Srwatson		return ("r-x");
2262100979Srwatson	case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
2263100979Srwatson		return ("rwx");
2264100979Srwatson	case VM_PROT_WRITE:
2265100979Srwatson		return ("-w-");
2266100979Srwatson	case VM_PROT_EXECUTE:
2267100979Srwatson		return ("--x");
2268100979Srwatson	case VM_PROT_WRITE | VM_PROT_EXECUTE:
2269100979Srwatson		return ("-wx");
2270100979Srwatson	default:
2271100979Srwatson		return ("---");
2272100979Srwatson	}
2273100979Srwatson}
2274100979Srwatson
2275100979Srwatsonstatic void
2276100979Srwatsonmac_cred_mmapped_drop_perms_recurse(struct thread *td, struct ucred *cred,
2277100979Srwatson    struct vm_map *map)
2278100979Srwatson{
2279100979Srwatson	struct vm_map_entry *vme;
2280100979Srwatson	vm_prot_t result, revokeperms;
2281100979Srwatson	vm_object_t object;
2282100979Srwatson	vm_ooffset_t offset;
2283100979Srwatson	struct vnode *vp;
2284100979Srwatson
2285103136Srwatson	if (!mac_mmap_revocation)
2286103136Srwatson		return;
2287103136Srwatson
2288100979Srwatson	vm_map_lock_read(map);
2289100979Srwatson	for (vme = map->header.next; vme != &map->header; vme = vme->next) {
2290100979Srwatson		if (vme->eflags & MAP_ENTRY_IS_SUB_MAP) {
2291100979Srwatson			mac_cred_mmapped_drop_perms_recurse(td, cred,
2292100979Srwatson			    vme->object.sub_map);
2293100979Srwatson			continue;
2294100979Srwatson		}
2295100979Srwatson		/*
2296100979Srwatson		 * Skip over entries that obviously are not shared.
2297100979Srwatson		 */
2298100979Srwatson		if (vme->eflags & (MAP_ENTRY_COW | MAP_ENTRY_NOSYNC) ||
2299100979Srwatson		    !vme->max_protection)
2300100979Srwatson			continue;
2301100979Srwatson		/*
2302100979Srwatson		 * Drill down to the deepest backing object.
2303100979Srwatson		 */
2304100979Srwatson		offset = vme->offset;
2305100979Srwatson		object = vme->object.vm_object;
2306100979Srwatson		if (object == NULL)
2307100979Srwatson			continue;
2308100979Srwatson		while (object->backing_object != NULL) {
2309100979Srwatson			object = object->backing_object;
2310100979Srwatson			offset += object->backing_object_offset;
2311100979Srwatson		}
2312100979Srwatson		/*
2313100979Srwatson		 * At the moment, vm_maps and objects aren't considered
2314100979Srwatson		 * by the MAC system, so only things with backing by a
2315100979Srwatson		 * normal object (read: vnodes) are checked.
2316100979Srwatson		 */
2317100979Srwatson		if (object->type != OBJT_VNODE)
2318100979Srwatson			continue;
2319100979Srwatson		vp = (struct vnode *)object->handle;
2320100979Srwatson		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2321100979Srwatson		result = mac_check_vnode_mmap_prot(cred, vp, 0);
2322100979Srwatson		VOP_UNLOCK(vp, 0, td);
2323100979Srwatson		/*
2324100979Srwatson		 * Find out what maximum protection we may be allowing
2325100979Srwatson		 * now but a policy needs to get removed.
2326100979Srwatson		 */
2327100979Srwatson		revokeperms = vme->max_protection & ~result;
2328100979Srwatson		if (!revokeperms)
2329100979Srwatson			continue;
2330102949Sbde		printf("pid %ld: revoking %s perms from %#lx:%ld "
2331102949Sbde		    "(max %s/cur %s)\n", (long)td->td_proc->p_pid,
2332102949Sbde		    prot2str(revokeperms), (u_long)vme->start,
2333102949Sbde		    (long)(vme->end - vme->start),
2334100979Srwatson		    prot2str(vme->max_protection), prot2str(vme->protection));
2335100979Srwatson		vm_map_lock_upgrade(map);
2336100979Srwatson		/*
2337100979Srwatson		 * This is the really simple case: if a map has more
2338100979Srwatson		 * max_protection than is allowed, but it's not being
2339100979Srwatson		 * actually used (that is, the current protection is
2340100979Srwatson		 * still allowed), we can just wipe it out and do
2341100979Srwatson		 * nothing more.
2342100979Srwatson		 */
2343100979Srwatson		if ((vme->protection & revokeperms) == 0) {
2344100979Srwatson			vme->max_protection -= revokeperms;
2345100979Srwatson		} else {
2346100979Srwatson			if (revokeperms & VM_PROT_WRITE) {
2347100979Srwatson				/*
2348100979Srwatson				 * In the more complicated case, flush out all
2349100979Srwatson				 * pending changes to the object then turn it
2350100979Srwatson				 * copy-on-write.
2351100979Srwatson				 */
2352100979Srwatson				vm_object_reference(object);
2353100979Srwatson				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2354100979Srwatson				vm_object_page_clean(object,
2355100979Srwatson				    OFF_TO_IDX(offset),
2356100979Srwatson				    OFF_TO_IDX(offset + vme->end - vme->start +
2357100979Srwatson					PAGE_MASK),
2358100979Srwatson				    OBJPC_SYNC);
2359100979Srwatson				VOP_UNLOCK(vp, 0, td);
2360100979Srwatson				vm_object_deallocate(object);
2361100979Srwatson				/*
2362100979Srwatson				 * Why bother if there's no read permissions
2363100979Srwatson				 * anymore?  For the rest, we need to leave
2364100979Srwatson				 * the write permissions on for COW, or
2365100979Srwatson				 * remove them entirely if configured to.
2366100979Srwatson				 */
2367100979Srwatson				if (!mac_mmap_revocation_via_cow) {
2368100979Srwatson					vme->max_protection &= ~VM_PROT_WRITE;
2369100979Srwatson					vme->protection &= ~VM_PROT_WRITE;
2370100979Srwatson				} if ((revokeperms & VM_PROT_READ) == 0)
2371100979Srwatson					vme->eflags |= MAP_ENTRY_COW |
2372100979Srwatson					    MAP_ENTRY_NEEDS_COPY;
2373100979Srwatson			}
2374100979Srwatson			if (revokeperms & VM_PROT_EXECUTE) {
2375100979Srwatson				vme->max_protection &= ~VM_PROT_EXECUTE;
2376100979Srwatson				vme->protection &= ~VM_PROT_EXECUTE;
2377100979Srwatson			}
2378100979Srwatson			if (revokeperms & VM_PROT_READ) {
2379100979Srwatson				vme->max_protection = 0;
2380100979Srwatson				vme->protection = 0;
2381100979Srwatson			}
2382100979Srwatson			pmap_protect(map->pmap, vme->start, vme->end,
2383100979Srwatson			    vme->protection & ~revokeperms);
2384100979Srwatson			vm_map_simplify_entry(map, vme);
2385100979Srwatson		}
2386100979Srwatson		vm_map_lock_downgrade(map);
2387100979Srwatson	}
2388100979Srwatson	vm_map_unlock_read(map);
2389100979Srwatson}
2390100979Srwatson
2391100979Srwatson/*
2392100979Srwatson * When the subject's label changes, it may require revocation of privilege
2393100979Srwatson * to mapped objects.  This can't be done on-the-fly later with a unified
2394100979Srwatson * buffer cache.
2395100979Srwatson */
2396100979Srwatsonstatic void
2397100979Srwatsonmac_relabel_cred(struct ucred *cred, struct label *newlabel)
2398100979Srwatson{
2399100979Srwatson
2400100979Srwatson	MAC_PERFORM(relabel_cred, cred, newlabel);
2401100979Srwatson}
2402100979Srwatson
2403100979Srwatsonvoid
2404100979Srwatsonmac_relabel_vnode(struct ucred *cred, struct vnode *vp, struct label *newlabel)
2405100979Srwatson{
2406100979Srwatson
2407100979Srwatson	MAC_PERFORM(relabel_vnode, cred, vp, &vp->v_label, newlabel);
2408100979Srwatson}
2409100979Srwatson
2410100979Srwatsonvoid
2411100979Srwatsonmac_create_ifnet(struct ifnet *ifnet)
2412100979Srwatson{
2413100979Srwatson
2414100979Srwatson	MAC_PERFORM(create_ifnet, ifnet, &ifnet->if_label);
2415100979Srwatson}
2416100979Srwatson
2417100979Srwatsonvoid
2418100979Srwatsonmac_create_bpfdesc(struct ucred *cred, struct bpf_d *bpf_d)
2419100979Srwatson{
2420100979Srwatson
2421100979Srwatson	MAC_PERFORM(create_bpfdesc, cred, bpf_d, &bpf_d->bd_label);
2422100979Srwatson}
2423100979Srwatson
2424100979Srwatsonvoid
2425100979Srwatsonmac_create_socket(struct ucred *cred, struct socket *socket)
2426100979Srwatson{
2427100979Srwatson
2428100979Srwatson	MAC_PERFORM(create_socket, cred, socket, &socket->so_label);
2429100979Srwatson}
2430100979Srwatson
2431100979Srwatsonvoid
2432100979Srwatsonmac_create_pipe(struct ucred *cred, struct pipe *pipe)
2433100979Srwatson{
2434100979Srwatson
2435100979Srwatson	MAC_PERFORM(create_pipe, cred, pipe, pipe->pipe_label);
2436100979Srwatson}
2437100979Srwatson
2438100979Srwatsonvoid
2439100979Srwatsonmac_create_socket_from_socket(struct socket *oldsocket,
2440100979Srwatson    struct socket *newsocket)
2441100979Srwatson{
2442100979Srwatson
2443100979Srwatson	MAC_PERFORM(create_socket_from_socket, oldsocket, &oldsocket->so_label,
2444100979Srwatson	    newsocket, &newsocket->so_label);
2445100979Srwatson}
2446100979Srwatson
2447100979Srwatsonstatic void
2448100979Srwatsonmac_relabel_socket(struct ucred *cred, struct socket *socket,
2449100979Srwatson    struct label *newlabel)
2450100979Srwatson{
2451100979Srwatson
2452100979Srwatson	MAC_PERFORM(relabel_socket, cred, socket, &socket->so_label, newlabel);
2453100979Srwatson}
2454100979Srwatson
2455100979Srwatsonstatic void
2456100979Srwatsonmac_relabel_pipe(struct ucred *cred, struct pipe *pipe, struct label *newlabel)
2457100979Srwatson{
2458100979Srwatson
2459100979Srwatson	MAC_PERFORM(relabel_pipe, cred, pipe, pipe->pipe_label, newlabel);
2460100979Srwatson}
2461100979Srwatson
2462100979Srwatsonvoid
2463100979Srwatsonmac_set_socket_peer_from_mbuf(struct mbuf *mbuf, struct socket *socket)
2464100979Srwatson{
2465100979Srwatson
2466100979Srwatson	MAC_PERFORM(set_socket_peer_from_mbuf, mbuf, &mbuf->m_pkthdr.label,
2467100979Srwatson	    socket, &socket->so_peerlabel);
2468100979Srwatson}
2469100979Srwatson
2470100979Srwatsonvoid
2471100979Srwatsonmac_set_socket_peer_from_socket(struct socket *oldsocket,
2472100979Srwatson    struct socket *newsocket)
2473100979Srwatson{
2474100979Srwatson
2475100979Srwatson	MAC_PERFORM(set_socket_peer_from_socket, oldsocket,
2476100979Srwatson	    &oldsocket->so_label, newsocket, &newsocket->so_peerlabel);
2477100979Srwatson}
2478100979Srwatson
2479100979Srwatsonvoid
2480100979Srwatsonmac_create_datagram_from_ipq(struct ipq *ipq, struct mbuf *datagram)
2481100979Srwatson{
2482100979Srwatson
2483100979Srwatson	MAC_PERFORM(create_datagram_from_ipq, ipq, &ipq->ipq_label,
2484100979Srwatson	    datagram, &datagram->m_pkthdr.label);
2485100979Srwatson}
2486100979Srwatson
2487100979Srwatsonvoid
2488100979Srwatsonmac_create_fragment(struct mbuf *datagram, struct mbuf *fragment)
2489100979Srwatson{
2490100979Srwatson
2491100979Srwatson	MAC_PERFORM(create_fragment, datagram, &datagram->m_pkthdr.label,
2492100979Srwatson	    fragment, &fragment->m_pkthdr.label);
2493100979Srwatson}
2494100979Srwatson
2495100979Srwatsonvoid
2496100979Srwatsonmac_create_ipq(struct mbuf *fragment, struct ipq *ipq)
2497100979Srwatson{
2498100979Srwatson
2499100979Srwatson	MAC_PERFORM(create_ipq, fragment, &fragment->m_pkthdr.label, ipq,
2500100979Srwatson	    &ipq->ipq_label);
2501100979Srwatson}
2502100979Srwatson
2503100979Srwatsonvoid
2504100979Srwatsonmac_create_mbuf_from_mbuf(struct mbuf *oldmbuf, struct mbuf *newmbuf)
2505100979Srwatson{
2506100979Srwatson
2507100979Srwatson	MAC_PERFORM(create_mbuf_from_mbuf, oldmbuf, &oldmbuf->m_pkthdr.label,
2508100979Srwatson	    newmbuf, &newmbuf->m_pkthdr.label);
2509100979Srwatson}
2510100979Srwatson
2511100979Srwatsonvoid
2512100979Srwatsonmac_create_mbuf_from_bpfdesc(struct bpf_d *bpf_d, struct mbuf *mbuf)
2513100979Srwatson{
2514100979Srwatson
2515100979Srwatson	MAC_PERFORM(create_mbuf_from_bpfdesc, bpf_d, &bpf_d->bd_label, mbuf,
2516100979Srwatson	    &mbuf->m_pkthdr.label);
2517100979Srwatson}
2518100979Srwatson
2519100979Srwatsonvoid
2520100979Srwatsonmac_create_mbuf_linklayer(struct ifnet *ifnet, struct mbuf *mbuf)
2521100979Srwatson{
2522100979Srwatson
2523100979Srwatson	MAC_PERFORM(create_mbuf_linklayer, ifnet, &ifnet->if_label, mbuf,
2524100979Srwatson	    &mbuf->m_pkthdr.label);
2525100979Srwatson}
2526100979Srwatson
2527100979Srwatsonvoid
2528100979Srwatsonmac_create_mbuf_from_ifnet(struct ifnet *ifnet, struct mbuf *mbuf)
2529100979Srwatson{
2530100979Srwatson
2531100979Srwatson	MAC_PERFORM(create_mbuf_from_ifnet, ifnet, &ifnet->if_label, mbuf,
2532100979Srwatson	    &mbuf->m_pkthdr.label);
2533100979Srwatson}
2534100979Srwatson
2535100979Srwatsonvoid
2536100979Srwatsonmac_create_mbuf_multicast_encap(struct mbuf *oldmbuf, struct ifnet *ifnet,
2537100979Srwatson    struct mbuf *newmbuf)
2538100979Srwatson{
2539100979Srwatson
2540100979Srwatson	MAC_PERFORM(create_mbuf_multicast_encap, oldmbuf,
2541100979Srwatson	    &oldmbuf->m_pkthdr.label, ifnet, &ifnet->if_label, newmbuf,
2542100979Srwatson	    &newmbuf->m_pkthdr.label);
2543100979Srwatson}
2544100979Srwatson
2545100979Srwatsonvoid
2546100979Srwatsonmac_create_mbuf_netlayer(struct mbuf *oldmbuf, struct mbuf *newmbuf)
2547100979Srwatson{
2548100979Srwatson
2549100979Srwatson	MAC_PERFORM(create_mbuf_netlayer, oldmbuf, &oldmbuf->m_pkthdr.label,
2550100979Srwatson	    newmbuf, &newmbuf->m_pkthdr.label);
2551100979Srwatson}
2552100979Srwatson
2553100979Srwatsonint
2554100979Srwatsonmac_fragment_match(struct mbuf *fragment, struct ipq *ipq)
2555100979Srwatson{
2556100979Srwatson	int result;
2557100979Srwatson
2558100979Srwatson	result = 1;
2559100979Srwatson	MAC_BOOLEAN(fragment_match, &&, fragment, &fragment->m_pkthdr.label,
2560100979Srwatson	    ipq, &ipq->ipq_label);
2561100979Srwatson
2562100979Srwatson	return (result);
2563100979Srwatson}
2564100979Srwatson
2565100979Srwatsonvoid
2566100979Srwatsonmac_update_ipq(struct mbuf *fragment, struct ipq *ipq)
2567100979Srwatson{
2568100979Srwatson
2569100979Srwatson	MAC_PERFORM(update_ipq, fragment, &fragment->m_pkthdr.label, ipq,
2570100979Srwatson	    &ipq->ipq_label);
2571100979Srwatson}
2572100979Srwatson
2573100979Srwatsonvoid
2574100979Srwatsonmac_create_mbuf_from_socket(struct socket *socket, struct mbuf *mbuf)
2575100979Srwatson{
2576100979Srwatson
2577100979Srwatson	MAC_PERFORM(create_mbuf_from_socket, socket, &socket->so_label, mbuf,
2578100979Srwatson	    &mbuf->m_pkthdr.label);
2579100979Srwatson}
2580100979Srwatson
2581100979Srwatsonvoid
2582100979Srwatsonmac_create_mount(struct ucred *cred, struct mount *mp)
2583100979Srwatson{
2584100979Srwatson
2585100979Srwatson	MAC_PERFORM(create_mount, cred, mp, &mp->mnt_mntlabel,
2586100979Srwatson	    &mp->mnt_fslabel);
2587100979Srwatson}
2588100979Srwatson
2589100979Srwatsonvoid
2590100979Srwatsonmac_create_root_mount(struct ucred *cred, struct mount *mp)
2591100979Srwatson{
2592100979Srwatson
2593100979Srwatson	MAC_PERFORM(create_root_mount, cred, mp, &mp->mnt_mntlabel,
2594100979Srwatson	    &mp->mnt_fslabel);
2595100979Srwatson}
2596100979Srwatson
2597100979Srwatsonint
2598100979Srwatsonmac_check_bpfdesc_receive(struct bpf_d *bpf_d, struct ifnet *ifnet)
2599100979Srwatson{
2600100979Srwatson	int error;
2601100979Srwatson
2602100979Srwatson	if (!mac_enforce_network)
2603100979Srwatson		return (0);
2604100979Srwatson
2605100979Srwatson	MAC_CHECK(check_bpfdesc_receive, bpf_d, &bpf_d->bd_label, ifnet,
2606100979Srwatson	    &ifnet->if_label);
2607100979Srwatson
2608100979Srwatson	return (error);
2609100979Srwatson}
2610100979Srwatson
2611100979Srwatsonstatic int
2612100979Srwatsonmac_check_cred_relabel(struct ucred *cred, struct label *newlabel)
2613100979Srwatson{
2614100979Srwatson	int error;
2615100979Srwatson
2616100979Srwatson	MAC_CHECK(check_cred_relabel, cred, newlabel);
2617100979Srwatson
2618100979Srwatson	return (error);
2619100979Srwatson}
2620100979Srwatson
2621100979Srwatsonint
2622100979Srwatsonmac_check_cred_visible(struct ucred *u1, struct ucred *u2)
2623100979Srwatson{
2624100979Srwatson	int error;
2625100979Srwatson
2626100979Srwatson	if (!mac_enforce_process)
2627100979Srwatson		return (0);
2628100979Srwatson
2629100979Srwatson	MAC_CHECK(check_cred_visible, u1, u2);
2630100979Srwatson
2631100979Srwatson	return (error);
2632100979Srwatson}
2633100979Srwatson
2634100979Srwatsonint
2635100979Srwatsonmac_check_ifnet_transmit(struct ifnet *ifnet, struct mbuf *mbuf)
2636100979Srwatson{
2637100979Srwatson	int error;
2638100979Srwatson
2639100979Srwatson	if (!mac_enforce_network)
2640100979Srwatson		return (0);
2641100979Srwatson
2642100979Srwatson	KASSERT(mbuf->m_flags & M_PKTHDR, ("packet has no pkthdr"));
2643100979Srwatson	if (!(mbuf->m_pkthdr.label.l_flags & MAC_FLAG_INITIALIZED))
2644100979Srwatson		printf("%s%d: not initialized\n", ifnet->if_name,
2645100979Srwatson		    ifnet->if_unit);
2646100979Srwatson
2647100979Srwatson	MAC_CHECK(check_ifnet_transmit, ifnet, &ifnet->if_label, mbuf,
2648100979Srwatson	    &mbuf->m_pkthdr.label);
2649100979Srwatson
2650100979Srwatson	return (error);
2651100979Srwatson}
2652100979Srwatson
2653100979Srwatsonint
2654100979Srwatsonmac_check_mount_stat(struct ucred *cred, struct mount *mount)
2655100979Srwatson{
2656100979Srwatson	int error;
2657100979Srwatson
2658100979Srwatson	if (!mac_enforce_fs)
2659100979Srwatson		return (0);
2660100979Srwatson
2661100979Srwatson	MAC_CHECK(check_mount_stat, cred, mount, &mount->mnt_mntlabel);
2662100979Srwatson
2663100979Srwatson	return (error);
2664100979Srwatson}
2665100979Srwatson
2666100979Srwatsonint
2667100979Srwatsonmac_check_pipe_ioctl(struct ucred *cred, struct pipe *pipe, unsigned long cmd,
2668100979Srwatson    void *data)
2669100979Srwatson{
2670100979Srwatson	int error;
2671100979Srwatson
2672104269Srwatson	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2673104269Srwatson
2674104269Srwatson	if (!mac_enforce_pipe)
2675104269Srwatson		return (0);
2676104269Srwatson
2677100979Srwatson	MAC_CHECK(check_pipe_ioctl, cred, pipe, pipe->pipe_label, cmd, data);
2678100979Srwatson
2679100979Srwatson	return (error);
2680100979Srwatson}
2681100979Srwatson
2682100979Srwatsonint
2683102115Srwatsonmac_check_pipe_poll(struct ucred *cred, struct pipe *pipe)
2684100979Srwatson{
2685100979Srwatson	int error;
2686100979Srwatson
2687104269Srwatson	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2688104269Srwatson
2689104269Srwatson	if (!mac_enforce_pipe)
2690104269Srwatson		return (0);
2691104269Srwatson
2692102115Srwatson	MAC_CHECK(check_pipe_poll, cred, pipe, pipe->pipe_label);
2693100979Srwatson
2694100979Srwatson	return (error);
2695100979Srwatson}
2696100979Srwatson
2697102115Srwatsonint
2698102115Srwatsonmac_check_pipe_read(struct ucred *cred, struct pipe *pipe)
2699102115Srwatson{
2700102115Srwatson	int error;
2701102115Srwatson
2702104269Srwatson	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2703104269Srwatson
2704104269Srwatson	if (!mac_enforce_pipe)
2705104269Srwatson		return (0);
2706104269Srwatson
2707102115Srwatson	MAC_CHECK(check_pipe_read, cred, pipe, pipe->pipe_label);
2708102115Srwatson
2709102115Srwatson	return (error);
2710102115Srwatson}
2711102115Srwatson
2712100979Srwatsonstatic int
2713100979Srwatsonmac_check_pipe_relabel(struct ucred *cred, struct pipe *pipe,
2714100979Srwatson    struct label *newlabel)
2715100979Srwatson{
2716100979Srwatson	int error;
2717100979Srwatson
2718104269Srwatson	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2719104269Srwatson
2720104269Srwatson	if (!mac_enforce_pipe)
2721104269Srwatson		return (0);
2722104269Srwatson
2723100979Srwatson	MAC_CHECK(check_pipe_relabel, cred, pipe, pipe->pipe_label, newlabel);
2724100979Srwatson
2725100979Srwatson	return (error);
2726100979Srwatson}
2727100979Srwatson
2728100979Srwatsonint
2729102115Srwatsonmac_check_pipe_stat(struct ucred *cred, struct pipe *pipe)
2730102115Srwatson{
2731102115Srwatson	int error;
2732102115Srwatson
2733104269Srwatson	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2734104269Srwatson
2735104269Srwatson	if (!mac_enforce_pipe)
2736104269Srwatson		return (0);
2737104269Srwatson
2738102115Srwatson	MAC_CHECK(check_pipe_stat, cred, pipe, pipe->pipe_label);
2739102115Srwatson
2740102115Srwatson	return (error);
2741102115Srwatson}
2742102115Srwatson
2743102115Srwatsonint
2744102115Srwatsonmac_check_pipe_write(struct ucred *cred, struct pipe *pipe)
2745102115Srwatson{
2746102115Srwatson	int error;
2747102115Srwatson
2748104269Srwatson	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2749104269Srwatson
2750104269Srwatson	if (!mac_enforce_pipe)
2751104269Srwatson		return (0);
2752104269Srwatson
2753102115Srwatson	MAC_CHECK(check_pipe_write, cred, pipe, pipe->pipe_label);
2754102115Srwatson
2755102115Srwatson	return (error);
2756102115Srwatson}
2757102115Srwatson
2758102115Srwatsonint
2759100979Srwatsonmac_check_proc_debug(struct ucred *cred, struct proc *proc)
2760100979Srwatson{
2761100979Srwatson	int error;
2762100979Srwatson
2763102103Srwatson	PROC_LOCK_ASSERT(proc, MA_OWNED);
2764102103Srwatson
2765100979Srwatson	if (!mac_enforce_process)
2766100979Srwatson		return (0);
2767100979Srwatson
2768100979Srwatson	MAC_CHECK(check_proc_debug, cred, proc);
2769100979Srwatson
2770100979Srwatson	return (error);
2771100979Srwatson}
2772100979Srwatson
2773100979Srwatsonint
2774100979Srwatsonmac_check_proc_sched(struct ucred *cred, struct proc *proc)
2775100979Srwatson{
2776100979Srwatson	int error;
2777100979Srwatson
2778102103Srwatson	PROC_LOCK_ASSERT(proc, MA_OWNED);
2779102103Srwatson
2780100979Srwatson	if (!mac_enforce_process)
2781100979Srwatson		return (0);
2782100979Srwatson
2783100979Srwatson	MAC_CHECK(check_proc_sched, cred, proc);
2784100979Srwatson
2785100979Srwatson	return (error);
2786100979Srwatson}
2787100979Srwatson
2788100979Srwatsonint
2789100979Srwatsonmac_check_proc_signal(struct ucred *cred, struct proc *proc, int signum)
2790100979Srwatson{
2791100979Srwatson	int error;
2792100979Srwatson
2793102103Srwatson	PROC_LOCK_ASSERT(proc, MA_OWNED);
2794102103Srwatson
2795100979Srwatson	if (!mac_enforce_process)
2796100979Srwatson		return (0);
2797100979Srwatson
2798100979Srwatson	MAC_CHECK(check_proc_signal, cred, proc, signum);
2799100979Srwatson
2800100979Srwatson	return (error);
2801100979Srwatson}
2802100979Srwatson
2803100979Srwatsonint
2804100979Srwatsonmac_check_socket_bind(struct ucred *ucred, struct socket *socket,
2805100979Srwatson    struct sockaddr *sockaddr)
2806100979Srwatson{
2807100979Srwatson	int error;
2808100979Srwatson
2809100979Srwatson	if (!mac_enforce_socket)
2810100979Srwatson		return (0);
2811100979Srwatson
2812100979Srwatson	MAC_CHECK(check_socket_bind, ucred, socket, &socket->so_label,
2813100979Srwatson	    sockaddr);
2814100979Srwatson
2815100979Srwatson	return (error);
2816100979Srwatson}
2817100979Srwatson
2818100979Srwatsonint
2819100979Srwatsonmac_check_socket_connect(struct ucred *cred, struct socket *socket,
2820100979Srwatson    struct sockaddr *sockaddr)
2821100979Srwatson{
2822100979Srwatson	int error;
2823100979Srwatson
2824100979Srwatson	if (!mac_enforce_socket)
2825100979Srwatson		return (0);
2826100979Srwatson
2827100979Srwatson	MAC_CHECK(check_socket_connect, cred, socket, &socket->so_label,
2828100979Srwatson	    sockaddr);
2829100979Srwatson
2830100979Srwatson	return (error);
2831100979Srwatson}
2832100979Srwatson
2833100979Srwatsonint
2834101933Srwatsonmac_check_socket_deliver(struct socket *socket, struct mbuf *mbuf)
2835100979Srwatson{
2836100979Srwatson	int error;
2837100979Srwatson
2838100979Srwatson	if (!mac_enforce_socket)
2839100979Srwatson		return (0);
2840100979Srwatson
2841101933Srwatson	MAC_CHECK(check_socket_deliver, socket, &socket->so_label, mbuf,
2842101933Srwatson	    &mbuf->m_pkthdr.label);
2843101933Srwatson
2844100979Srwatson	return (error);
2845100979Srwatson}
2846100979Srwatson
2847100979Srwatsonint
2848101933Srwatsonmac_check_socket_listen(struct ucred *cred, struct socket *socket)
2849100979Srwatson{
2850100979Srwatson	int error;
2851100979Srwatson
2852100979Srwatson	if (!mac_enforce_socket)
2853100979Srwatson		return (0);
2854100979Srwatson
2855101933Srwatson	MAC_CHECK(check_socket_listen, cred, socket, &socket->so_label);
2856100979Srwatson	return (error);
2857100979Srwatson}
2858100979Srwatson
2859100979Srwatsonstatic int
2860100979Srwatsonmac_check_socket_relabel(struct ucred *cred, struct socket *socket,
2861100979Srwatson    struct label *newlabel)
2862100979Srwatson{
2863100979Srwatson	int error;
2864100979Srwatson
2865100979Srwatson	MAC_CHECK(check_socket_relabel, cred, socket, &socket->so_label,
2866100979Srwatson	    newlabel);
2867100979Srwatson
2868100979Srwatson	return (error);
2869100979Srwatson}
2870100979Srwatson
2871100979Srwatsonint
2872100979Srwatsonmac_check_socket_visible(struct ucred *cred, struct socket *socket)
2873100979Srwatson{
2874100979Srwatson	int error;
2875100979Srwatson
2876100979Srwatson	if (!mac_enforce_socket)
2877100979Srwatson		return (0);
2878100979Srwatson
2879100979Srwatson	MAC_CHECK(check_socket_visible, cred, socket, &socket->so_label);
2880100979Srwatson
2881100979Srwatson	return (error);
2882100979Srwatson}
2883100979Srwatson
2884100979Srwatsonint
2885100979Srwatsonmac_ioctl_ifnet_get(struct ucred *cred, struct ifreq *ifr,
2886100979Srwatson    struct ifnet *ifnet)
2887100979Srwatson{
2888100979Srwatson	struct mac label;
2889100979Srwatson	int error;
2890100979Srwatson
2891100979Srwatson	error = mac_externalize(&ifnet->if_label, &label);
2892100979Srwatson	if (error)
2893100979Srwatson		return (error);
2894100979Srwatson
2895100979Srwatson	return (copyout(&label, ifr->ifr_ifru.ifru_data, sizeof(label)));
2896100979Srwatson}
2897100979Srwatson
2898100979Srwatsonint
2899100979Srwatsonmac_ioctl_ifnet_set(struct ucred *cred, struct ifreq *ifr,
2900100979Srwatson    struct ifnet *ifnet)
2901100979Srwatson{
2902100979Srwatson	struct mac newlabel;
2903100979Srwatson	struct label intlabel;
2904100979Srwatson	int error;
2905100979Srwatson
2906100979Srwatson	error = copyin(ifr->ifr_ifru.ifru_data, &newlabel, sizeof(newlabel));
2907100979Srwatson	if (error)
2908100979Srwatson		return (error);
2909100979Srwatson
2910100979Srwatson	error = mac_internalize(&intlabel, &newlabel);
2911100979Srwatson	if (error)
2912100979Srwatson		return (error);
2913100979Srwatson
2914100979Srwatson	/*
2915100979Srwatson	 * XXX: Note that this is a redundant privilege check, since
2916100979Srwatson	 * policies impose this check themselves if required by the
2917100979Srwatson	 * policy.  Eventually, this should go away.
2918100979Srwatson	 */
2919100979Srwatson	error = suser_cred(cred, 0);
2920100979Srwatson	if (error)
2921100979Srwatson		goto out;
2922100979Srwatson
2923100979Srwatson	MAC_CHECK(check_ifnet_relabel, cred, ifnet, &ifnet->if_label,
2924100979Srwatson	    &intlabel);
2925100979Srwatson	if (error)
2926100979Srwatson		goto out;
2927100979Srwatson
2928100979Srwatson	MAC_PERFORM(relabel_ifnet, cred, ifnet, &ifnet->if_label, &intlabel);
2929100979Srwatson
2930100979Srwatsonout:
2931100979Srwatson	mac_destroy_temp(&intlabel);
2932100979Srwatson	return (error);
2933100979Srwatson}
2934100979Srwatson
2935100979Srwatsonvoid
2936100979Srwatsonmac_create_devfs_vnode(struct devfs_dirent *de, struct vnode *vp)
2937100979Srwatson{
2938100979Srwatson
2939100979Srwatson	MAC_PERFORM(create_devfs_vnode, de, &de->de_label, vp, &vp->v_label);
2940100979Srwatson}
2941100979Srwatson
2942100979Srwatsonvoid
2943100979Srwatsonmac_create_devfs_device(dev_t dev, struct devfs_dirent *de)
2944100979Srwatson{
2945100979Srwatson
2946100979Srwatson	MAC_PERFORM(create_devfs_device, dev, de, &de->de_label);
2947100979Srwatson}
2948100979Srwatson
2949100979Srwatsonstatic int
2950100979Srwatsonmac_stdcreatevnode_ea(struct vnode *vp)
2951100979Srwatson{
2952100979Srwatson	int error;
2953100979Srwatson
2954100979Srwatson	MAC_CHECK(stdcreatevnode_ea, vp, &vp->v_label);
2955100979Srwatson
2956100979Srwatson	return (error);
2957100979Srwatson}
2958100979Srwatson
2959100979Srwatsonvoid
2960100979Srwatsonmac_create_devfs_directory(char *dirname, int dirnamelen,
2961100979Srwatson    struct devfs_dirent *de)
2962100979Srwatson{
2963100979Srwatson
2964100979Srwatson	MAC_PERFORM(create_devfs_directory, dirname, dirnamelen, de,
2965100979Srwatson	    &de->de_label);
2966100979Srwatson}
2967100979Srwatson
2968100979Srwatson/*
2969100979Srwatson * When a new vnode is created, this call will initialize its label.
2970100979Srwatson */
2971100979Srwatsonvoid
2972100979Srwatsonmac_create_vnode(struct ucred *cred, struct vnode *parent,
2973100979Srwatson    struct vnode *child)
2974100979Srwatson{
2975100979Srwatson	int error;
2976100979Srwatson
2977100979Srwatson	ASSERT_VOP_LOCKED(parent, "mac_create_vnode");
2978100979Srwatson	ASSERT_VOP_LOCKED(child, "mac_create_vnode");
2979100979Srwatson
2980100979Srwatson	error = vn_refreshlabel(parent, cred);
2981100979Srwatson	if (error) {
2982100979Srwatson		printf("mac_create_vnode: vn_refreshlabel returned %d\n",
2983100979Srwatson		    error);
2984100979Srwatson		printf("mac_create_vnode: using old vnode label\n");
2985100979Srwatson	}
2986100979Srwatson
2987100979Srwatson	MAC_PERFORM(create_vnode, cred, parent, &parent->v_label, child,
2988100979Srwatson	    &child->v_label);
2989100979Srwatson}
2990100979Srwatson
2991100979Srwatsonint
2992100979Srwatsonmac_setsockopt_label_set(struct ucred *cred, struct socket *so,
2993100979Srwatson    struct mac *extmac)
2994100979Srwatson{
2995100979Srwatson	struct label intlabel;
2996100979Srwatson	int error;
2997100979Srwatson
2998100979Srwatson	error = mac_internalize(&intlabel, extmac);
2999100979Srwatson	if (error)
3000100979Srwatson		return (error);
3001100979Srwatson
3002100979Srwatson	mac_check_socket_relabel(cred, so, &intlabel);
3003100979Srwatson	if (error) {
3004100979Srwatson		mac_destroy_temp(&intlabel);
3005100979Srwatson		return (error);
3006100979Srwatson	}
3007100979Srwatson
3008100979Srwatson	mac_relabel_socket(cred, so, &intlabel);
3009100979Srwatson
3010100979Srwatson	mac_destroy_temp(&intlabel);
3011100979Srwatson	return (0);
3012100979Srwatson}
3013100979Srwatson
3014100979Srwatsonint
3015100979Srwatsonmac_pipe_label_set(struct ucred *cred, struct pipe *pipe, struct label *label)
3016100979Srwatson{
3017100979Srwatson	int error;
3018100979Srwatson
3019104269Srwatson	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
3020104269Srwatson
3021100979Srwatson	error = mac_check_pipe_relabel(cred, pipe, label);
3022100979Srwatson	if (error)
3023100979Srwatson		return (error);
3024100979Srwatson
3025100979Srwatson	mac_relabel_pipe(cred, pipe, label);
3026100979Srwatson
3027100979Srwatson	return (0);
3028100979Srwatson}
3029100979Srwatson
3030100979Srwatsonint
3031100979Srwatsonmac_getsockopt_label_get(struct ucred *cred, struct socket *so,
3032100979Srwatson    struct mac *extmac)
3033100979Srwatson{
3034100979Srwatson
3035100979Srwatson	return (mac_externalize(&so->so_label, extmac));
3036100979Srwatson}
3037100979Srwatson
3038100979Srwatsonint
3039100979Srwatsonmac_getsockopt_peerlabel_get(struct ucred *cred, struct socket *so,
3040100979Srwatson    struct mac *extmac)
3041100979Srwatson{
3042100979Srwatson
3043100979Srwatson	return (mac_externalize(&so->so_peerlabel, extmac));
3044100979Srwatson}
3045100979Srwatson
3046100979Srwatson/*
3047100979Srwatson * Implementation of VOP_SETLABEL() that relies on extended attributes
3048100979Srwatson * to store label data.  Can be referenced by filesystems supporting
3049100979Srwatson * extended attributes.
3050100979Srwatson */
3051100979Srwatsonint
3052100979Srwatsonvop_stdsetlabel_ea(struct vop_setlabel_args *ap)
3053100979Srwatson{
3054100979Srwatson	struct vnode *vp = ap->a_vp;
3055100979Srwatson	struct label *intlabel = ap->a_label;
3056100979Srwatson	struct mac extmac;
3057100979Srwatson	int error;
3058100979Srwatson
3059100979Srwatson	ASSERT_VOP_LOCKED(vp, "vop_stdsetlabel_ea");
3060100979Srwatson
3061100979Srwatson	/*
3062100979Srwatson	 * XXX: Eventually call out to EA check/set calls here.
3063100979Srwatson	 * Be particularly careful to avoid race conditions,
3064100979Srwatson	 * consistency problems, and stability problems when
3065100979Srwatson	 * dealing with multiple EAs.  In particular, we require
3066100979Srwatson	 * the ability to write multiple EAs on the same file in
3067100979Srwatson	 * a single transaction, which the current EA interface
3068100979Srwatson	 * does not provide.
3069100979Srwatson	 */
3070100979Srwatson
3071100979Srwatson	error = mac_externalize(intlabel, &extmac);
3072100979Srwatson	if (error)
3073100979Srwatson		return (error);
3074100979Srwatson
3075100979Srwatson	error = vn_extattr_set(vp, IO_NODELOCKED,
3076100979Srwatson	    FREEBSD_MAC_EXTATTR_NAMESPACE, FREEBSD_MAC_EXTATTR_NAME,
3077100979Srwatson	    sizeof(extmac), (char *)&extmac, curthread);
3078100979Srwatson	if (error)
3079100979Srwatson		return (error);
3080100979Srwatson
3081100979Srwatson	mac_relabel_vnode(ap->a_cred, vp, intlabel);
3082100979Srwatson
3083101308Sjeff	vp->v_vflag |= VV_CACHEDLABEL;
3084100979Srwatson
3085100979Srwatson	return (0);
3086100979Srwatson}
3087100979Srwatson
3088100979Srwatsonstatic int
3089100979Srwatsonvn_setlabel(struct vnode *vp, struct label *intlabel, struct ucred *cred)
3090100979Srwatson{
3091100979Srwatson	int error;
3092100979Srwatson
3093100979Srwatson	if (vp->v_mount == NULL) {
3094100979Srwatson		/* printf("vn_setlabel: null v_mount\n"); */
3095103314Snjl		if (vp->v_type != VNON)
3096103314Snjl			printf("vn_setlabel: null v_mount with non-VNON\n");
3097100979Srwatson		return (EBADF);
3098100979Srwatson	}
3099100979Srwatson
3100100979Srwatson	if ((vp->v_mount->mnt_flag & MNT_MULTILABEL) == 0)
3101100979Srwatson		return (EOPNOTSUPP);
3102100979Srwatson
3103100979Srwatson	/*
3104100979Srwatson	 * Multi-phase commit.  First check the policies to confirm the
3105100979Srwatson	 * change is OK.  Then commit via the filesystem.  Finally,
3106100979Srwatson	 * update the actual vnode label.  Question: maybe the filesystem
3107100979Srwatson	 * should update the vnode at the end as part of VOP_SETLABEL()?
3108100979Srwatson	 */
3109100979Srwatson	error = mac_check_vnode_relabel(cred, vp, intlabel);
3110100979Srwatson	if (error)
3111100979Srwatson		return (error);
3112100979Srwatson
3113100979Srwatson	/*
3114100979Srwatson	 * VADMIN provides the opportunity for the filesystem to make
3115100979Srwatson	 * decisions about who is and is not able to modify labels
3116100979Srwatson	 * and protections on files.  This might not be right.  We can't
3117100979Srwatson	 * assume VOP_SETLABEL() will do it, because we might implement
3118100979Srwatson	 * that as part of vop_stdsetlabel_ea().
3119100979Srwatson	 */
3120100979Srwatson	error = VOP_ACCESS(vp, VADMIN, cred, curthread);
3121100979Srwatson	if (error)
3122100979Srwatson		return (error);
3123100979Srwatson
3124100979Srwatson	error = VOP_SETLABEL(vp, intlabel, cred, curthread);
3125100979Srwatson	if (error)
3126100979Srwatson		return (error);
3127100979Srwatson
3128100979Srwatson	return (0);
3129100979Srwatson}
3130100979Srwatson
3131100979Srwatson/*
3132100979Srwatson * MPSAFE
3133100979Srwatson */
3134100979Srwatsonint
3135100894Srwatson__mac_get_proc(struct thread *td, struct __mac_get_proc_args *uap)
3136100894Srwatson{
3137100979Srwatson	struct mac extmac;
3138100979Srwatson	int error;
3139100894Srwatson
3140100979Srwatson	error = mac_externalize(&td->td_ucred->cr_label, &extmac);
3141100979Srwatson	if (error == 0)
3142100979Srwatson		error = copyout(&extmac, SCARG(uap, mac_p), sizeof(extmac));
3143100979Srwatson
3144100979Srwatson	return (error);
3145100979Srwatson}
3146100979Srwatson
3147100979Srwatson/*
3148100979Srwatson * MPSAFE
3149100979Srwatson */
3150100979Srwatsonint
3151100979Srwatson__mac_set_proc(struct thread *td, struct __mac_set_proc_args *uap)
3152100979Srwatson{
3153100979Srwatson	struct ucred *newcred, *oldcred;
3154100979Srwatson	struct proc *p;
3155100979Srwatson	struct mac extmac;
3156100979Srwatson	struct label intlabel;
3157100979Srwatson	int error;
3158100979Srwatson
3159100979Srwatson	error = copyin(SCARG(uap, mac_p), &extmac, sizeof(extmac));
3160100979Srwatson	if (error)
3161100979Srwatson		return (error);
3162100979Srwatson
3163100979Srwatson	error = mac_internalize(&intlabel, &extmac);
3164100979Srwatson	if (error)
3165100979Srwatson		return (error);
3166100979Srwatson
3167100979Srwatson	newcred = crget();
3168100979Srwatson
3169100979Srwatson	p = td->td_proc;
3170100979Srwatson	PROC_LOCK(p);
3171100979Srwatson	oldcred = p->p_ucred;
3172100979Srwatson
3173100979Srwatson	error = mac_check_cred_relabel(oldcred, &intlabel);
3174100979Srwatson	if (error) {
3175100979Srwatson		PROC_UNLOCK(p);
3176100979Srwatson		mac_destroy_temp(&intlabel);
3177100979Srwatson		crfree(newcred);
3178100979Srwatson		return (error);
3179100979Srwatson	}
3180100979Srwatson
3181100979Srwatson	setsugid(p);
3182100979Srwatson	crcopy(newcred, oldcred);
3183100979Srwatson	mac_relabel_cred(newcred, &intlabel);
3184102136Srwatson	p->p_ucred = newcred;
3185100979Srwatson
3186102136Srwatson	/*
3187102136Srwatson	 * Grab additional reference for use while revoking mmaps, prior
3188102136Srwatson	 * to releasing the proc lock and sharing the cred.
3189102136Srwatson	 */
3190102136Srwatson	crhold(newcred);
3191100979Srwatson	PROC_UNLOCK(p);
3192102136Srwatson
3193103135Srwatson	mtx_lock(&Giant);
3194102136Srwatson	mac_cred_mmapped_drop_perms(td, newcred);
3195103135Srwatson	mtx_unlock(&Giant);
3196102136Srwatson
3197102136Srwatson	crfree(newcred);	/* Free revocation reference. */
3198100979Srwatson	crfree(oldcred);
3199100979Srwatson	mac_destroy_temp(&intlabel);
3200100979Srwatson	return (0);
3201100979Srwatson}
3202100979Srwatson
3203100979Srwatson/*
3204100979Srwatson * MPSAFE
3205100979Srwatson */
3206100979Srwatsonint
3207100979Srwatson__mac_get_fd(struct thread *td, struct __mac_get_fd_args *uap)
3208100979Srwatson{
3209100979Srwatson	struct file *fp;
3210100979Srwatson	struct mac extmac;
3211100979Srwatson	struct vnode *vp;
3212100979Srwatson	struct pipe *pipe;
3213100979Srwatson	int error;
3214100979Srwatson
3215100979Srwatson	mtx_lock(&Giant);
3216100979Srwatson
3217100979Srwatson	error = fget(td, SCARG(uap, fd), &fp);
3218100979Srwatson	if (error)
3219100979Srwatson		goto out;
3220100979Srwatson
3221100979Srwatson	switch (fp->f_type) {
3222100979Srwatson	case DTYPE_FIFO:
3223100979Srwatson	case DTYPE_VNODE:
3224100979Srwatson		vp = (struct vnode *)fp->f_data;
3225100979Srwatson
3226100979Srwatson		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3227100979Srwatson		error = vn_refreshlabel(vp, td->td_ucred);
3228100979Srwatson		if (error == 0)
3229100979Srwatson			error = mac_externalize(&vp->v_label, &extmac);
3230100979Srwatson		VOP_UNLOCK(vp, 0, td);
3231100979Srwatson		break;
3232100979Srwatson	case DTYPE_PIPE:
3233100979Srwatson		pipe = (struct pipe *)fp->f_data;
3234100979Srwatson		error = mac_externalize(pipe->pipe_label, &extmac);
3235100979Srwatson		break;
3236100979Srwatson	default:
3237100979Srwatson		error = EINVAL;
3238100979Srwatson	}
3239100979Srwatson
3240100979Srwatson	if (error == 0)
3241100979Srwatson		error = copyout(&extmac, SCARG(uap, mac_p), sizeof(extmac));
3242100979Srwatson
3243100979Srwatson	fdrop(fp, td);
3244100979Srwatson
3245100979Srwatsonout:
3246100979Srwatson	mtx_unlock(&Giant);
3247100979Srwatson	return (error);
3248100979Srwatson}
3249100979Srwatson
3250100979Srwatson/*
3251100979Srwatson * MPSAFE
3252100979Srwatson */
3253100979Srwatsonint
3254100979Srwatson__mac_get_file(struct thread *td, struct __mac_get_file_args *uap)
3255100979Srwatson{
3256100979Srwatson	struct nameidata nd;
3257100979Srwatson	struct mac extmac;
3258100979Srwatson	int error;
3259100979Srwatson
3260100979Srwatson	mtx_lock(&Giant);
3261100979Srwatson	NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_USERSPACE,
3262100979Srwatson	    SCARG(uap, path_p), td);
3263100979Srwatson	error = namei(&nd);
3264100979Srwatson	if (error)
3265100979Srwatson		goto out;
3266100979Srwatson
3267100979Srwatson	error = vn_refreshlabel(nd.ni_vp, td->td_ucred);
3268100979Srwatson	if (error == 0)
3269100979Srwatson		error = mac_externalize(&nd.ni_vp->v_label, &extmac);
3270100979Srwatson	NDFREE(&nd, 0);
3271100979Srwatson	if (error)
3272100979Srwatson		goto out;
3273100979Srwatson
3274100979Srwatson	error = copyout(&extmac, SCARG(uap, mac_p), sizeof(extmac));
3275100979Srwatson
3276100979Srwatsonout:
3277100979Srwatson	mtx_unlock(&Giant);
3278100979Srwatson	return (error);
3279100979Srwatson}
3280100979Srwatson
3281100979Srwatson/*
3282100979Srwatson * MPSAFE
3283100979Srwatson */
3284100979Srwatsonint
3285100979Srwatson__mac_set_fd(struct thread *td, struct __mac_set_fd_args *uap)
3286100979Srwatson{
3287100979Srwatson	struct file *fp;
3288100979Srwatson	struct mac extmac;
3289100979Srwatson	struct label intlabel;
3290100979Srwatson	struct mount *mp;
3291100979Srwatson	struct vnode *vp;
3292100979Srwatson	struct pipe *pipe;
3293100979Srwatson	int error;
3294100979Srwatson
3295100979Srwatson	mtx_lock(&Giant);
3296100979Srwatson	error = fget(td, SCARG(uap, fd), &fp);
3297100979Srwatson	if (error)
3298100979Srwatson		goto out1;
3299100979Srwatson
3300100979Srwatson	error = copyin(SCARG(uap, mac_p), &extmac, sizeof(extmac));
3301100979Srwatson	if (error)
3302100979Srwatson		goto out2;
3303100979Srwatson
3304100979Srwatson	error = mac_internalize(&intlabel, &extmac);
3305100979Srwatson	if (error)
3306100979Srwatson		goto out2;
3307100979Srwatson
3308100979Srwatson	switch (fp->f_type) {
3309100979Srwatson	case DTYPE_FIFO:
3310100979Srwatson	case DTYPE_VNODE:
3311100979Srwatson		vp = (struct vnode *)fp->f_data;
3312100979Srwatson		error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3313100979Srwatson		if (error != 0)
3314100979Srwatson			break;
3315100979Srwatson
3316100979Srwatson		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3317100979Srwatson		error = vn_setlabel(vp, &intlabel, td->td_ucred);
3318100979Srwatson		VOP_UNLOCK(vp, 0, td);
3319100979Srwatson		vn_finished_write(mp);
3320100979Srwatson		mac_destroy_temp(&intlabel);
3321100979Srwatson		break;
3322100979Srwatson	case DTYPE_PIPE:
3323100979Srwatson		pipe = (struct pipe *)fp->f_data;
3324104269Srwatson		PIPE_LOCK(pipe);
3325100979Srwatson		error = mac_pipe_label_set(td->td_ucred, pipe, &intlabel);
3326104269Srwatson		PIPE_UNLOCK(pipe);
3327100979Srwatson		break;
3328100979Srwatson	default:
3329100979Srwatson		error = EINVAL;
3330100979Srwatson	}
3331100979Srwatson
3332100979Srwatsonout2:
3333100979Srwatson	fdrop(fp, td);
3334100979Srwatsonout1:
3335100979Srwatson	mtx_unlock(&Giant);
3336100979Srwatson	return (error);
3337100979Srwatson}
3338100979Srwatson
3339100979Srwatson/*
3340100979Srwatson * MPSAFE
3341100979Srwatson */
3342100979Srwatsonint
3343100979Srwatson__mac_set_file(struct thread *td, struct __mac_set_file_args *uap)
3344100979Srwatson{
3345100979Srwatson	struct nameidata nd;
3346100979Srwatson	struct mac extmac;
3347100979Srwatson	struct label intlabel;
3348100979Srwatson	struct mount *mp;
3349100979Srwatson	int error;
3350100979Srwatson
3351100979Srwatson	mtx_lock(&Giant);
3352100979Srwatson
3353100979Srwatson	error = copyin(SCARG(uap, mac_p), &extmac, sizeof(extmac));
3354100979Srwatson	if (error)
3355100979Srwatson		goto out;
3356100979Srwatson
3357100979Srwatson	error = mac_internalize(&intlabel, &extmac);
3358100979Srwatson	if (error)
3359100979Srwatson		goto out;
3360100979Srwatson
3361100979Srwatson	NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_USERSPACE,
3362100979Srwatson	    SCARG(uap, path_p), td);
3363100979Srwatson	error = namei(&nd);
3364100979Srwatson	if (error)
3365100979Srwatson		goto out2;
3366100979Srwatson	error = vn_start_write(nd.ni_vp, &mp, V_WAIT | PCATCH);
3367100979Srwatson	if (error)
3368100979Srwatson		goto out2;
3369100979Srwatson
3370100979Srwatson	error = vn_setlabel(nd.ni_vp, &intlabel, td->td_ucred);
3371100979Srwatson
3372100979Srwatson	vn_finished_write(mp);
3373100979Srwatsonout2:
3374100979Srwatson	mac_destroy_temp(&intlabel);
3375100979Srwatson	NDFREE(&nd, 0);
3376100979Srwatsonout:
3377100979Srwatson	mtx_unlock(&Giant);
3378100979Srwatson	return (error);
3379100979Srwatson}
3380100979Srwatson
3381102123Srwatsonint
3382102123Srwatsonmac_syscall(struct thread *td, struct mac_syscall_args *uap)
3383102123Srwatson{
3384102123Srwatson	struct mac_policy_conf *mpc;
3385102123Srwatson	char target[MAC_MAX_POLICY_NAME];
3386102123Srwatson	int error;
3387102123Srwatson
3388102123Srwatson	error = copyinstr(SCARG(uap, policy), target, sizeof(target), NULL);
3389102123Srwatson	if (error)
3390102123Srwatson		return (error);
3391102123Srwatson
3392102123Srwatson	error = ENOSYS;
3393102123Srwatson	MAC_POLICY_LIST_BUSY();
3394102123Srwatson	LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {
3395102123Srwatson		if (strcmp(mpc->mpc_name, target) == 0 &&
3396102123Srwatson		    mpc->mpc_ops->mpo_syscall != NULL) {
3397102123Srwatson			error = mpc->mpc_ops->mpo_syscall(td,
3398102123Srwatson			    SCARG(uap, call), SCARG(uap, arg));
3399102123Srwatson			goto out;
3400102123Srwatson		}
3401102123Srwatson	}
3402102123Srwatson
3403102123Srwatsonout:
3404102123Srwatson	MAC_POLICY_LIST_UNBUSY();
3405102123Srwatson	return (error);
3406102123Srwatson}
3407102123Srwatson
3408100979SrwatsonSYSINIT(mac, SI_SUB_MAC, SI_ORDER_FIRST, mac_init, NULL);
3409100979SrwatsonSYSINIT(mac_late, SI_SUB_MAC_LATE, SI_ORDER_FIRST, mac_late_init, NULL);
3410100979Srwatson
3411100979Srwatson#else /* !MAC */
3412100979Srwatson
3413100979Srwatsonint
3414100979Srwatson__mac_get_proc(struct thread *td, struct __mac_get_proc_args *uap)
3415100979Srwatson{
3416100979Srwatson
3417100894Srwatson	return (ENOSYS);
3418100894Srwatson}
3419100894Srwatson
3420100894Srwatsonint
3421100894Srwatson__mac_set_proc(struct thread *td, struct __mac_set_proc_args *uap)
3422100894Srwatson{
3423100894Srwatson
3424100894Srwatson	return (ENOSYS);
3425100894Srwatson}
3426100894Srwatson
3427100894Srwatsonint
3428100894Srwatson__mac_get_fd(struct thread *td, struct __mac_get_fd_args *uap)
3429100894Srwatson{
3430100894Srwatson
3431100894Srwatson	return (ENOSYS);
3432100894Srwatson}
3433100894Srwatson
3434100894Srwatsonint
3435100894Srwatson__mac_get_file(struct thread *td, struct __mac_get_file_args *uap)
3436100894Srwatson{
3437100894Srwatson
3438100894Srwatson	return (ENOSYS);
3439100894Srwatson}
3440100894Srwatson
3441100894Srwatsonint
3442100894Srwatson__mac_set_fd(struct thread *td, struct __mac_set_fd_args *uap)
3443100894Srwatson{
3444100894Srwatson
3445100894Srwatson	return (ENOSYS);
3446100894Srwatson}
3447100894Srwatson
3448100894Srwatsonint
3449100894Srwatson__mac_set_file(struct thread *td, struct __mac_set_file_args *uap)
3450100894Srwatson{
3451100894Srwatson
3452100894Srwatson	return (ENOSYS);
3453100894Srwatson}
3454100979Srwatson
3455102123Srwatsonint
3456102123Srwatsonmac_syscall(struct thread *td, struct mac_syscall_args *uap)
3457102123Srwatson{
3458102123Srwatson
3459102123Srwatson	return (ENOSYS);
3460102123Srwatson}
3461102123Srwatson
3462100979Srwatson#endif /* !MAC */
3463