mac_process.c revision 104546
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 104546 2002-10-06 02:46:26Z 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);
219104546Srwatsonstatic void	mac_check_vnode_mmap_downgrade(struct ucred *cred,
220104546Srwatson		    struct vnode *vp, int *prot);
221100979Srwatsonstatic void	mac_cred_mmapped_drop_perms_recurse(struct thread *td,
222100979Srwatson		    struct ucred *cred, struct vm_map *map);
223100979Srwatson
224104541Srwatsonstatic void	mac_destroy_socket_label(struct label *label);
225104541Srwatson
226100979SrwatsonMALLOC_DEFINE(M_MACOPVEC, "macopvec", "MAC policy operation vector");
227100979SrwatsonMALLOC_DEFINE(M_MACPIPELABEL, "macpipelabel", "MAC labels for pipes");
228100979Srwatson
229100979Srwatson/*
230100979Srwatson * mac_policy_list_lock protects the consistency of 'mac_policy_list',
231100979Srwatson * the linked list of attached policy modules.  Read-only consumers of
232100979Srwatson * the list must acquire a shared lock for the duration of their use;
233100979Srwatson * writers must acquire an exclusive lock.  Note that for compound
234100979Srwatson * operations, locks should be held for the entire compound operation,
235100979Srwatson * and that this is not yet done for relabel requests.
236100979Srwatson */
237100979Srwatsonstatic struct mtx mac_policy_list_lock;
238100979Srwatsonstatic LIST_HEAD(, mac_policy_conf) mac_policy_list;
239100979Srwatsonstatic int mac_policy_list_busy;
240100979Srwatson#define	MAC_POLICY_LIST_LOCKINIT()	mtx_init(&mac_policy_list_lock,	\
241100979Srwatson	"mac_policy_list_lock", NULL, MTX_DEF);
242100979Srwatson#define	MAC_POLICY_LIST_LOCK()	mtx_lock(&mac_policy_list_lock);
243100979Srwatson#define	MAC_POLICY_LIST_UNLOCK()	mtx_unlock(&mac_policy_list_lock);
244100979Srwatson
245100979Srwatson#define	MAC_POLICY_LIST_BUSY() do {					\
246100979Srwatson	MAC_POLICY_LIST_LOCK();						\
247100979Srwatson	mac_policy_list_busy++;						\
248100979Srwatson	MAC_POLICY_LIST_UNLOCK();					\
249100979Srwatson} while (0)
250100979Srwatson
251100979Srwatson#define	MAC_POLICY_LIST_UNBUSY() do {					\
252100979Srwatson	MAC_POLICY_LIST_LOCK();						\
253100979Srwatson	mac_policy_list_busy--;						\
254100979Srwatson	if (mac_policy_list_busy < 0)					\
255100979Srwatson		panic("Extra mac_policy_list_busy--");			\
256100979Srwatson	MAC_POLICY_LIST_UNLOCK();					\
257100979Srwatson} while (0)
258100979Srwatson
259100979Srwatson/*
260100979Srwatson * MAC_CHECK performs the designated check by walking the policy
261100979Srwatson * module list and checking with each as to how it feels about the
262100979Srwatson * request.  Note that it returns its value via 'error' in the scope
263100979Srwatson * of the caller.
264100979Srwatson */
265100979Srwatson#define	MAC_CHECK(check, args...) do {					\
266100979Srwatson	struct mac_policy_conf *mpc;					\
267100979Srwatson									\
268100979Srwatson	error = 0;							\
269100979Srwatson	MAC_POLICY_LIST_BUSY();						\
270100979Srwatson	LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {			\
271100979Srwatson		if (mpc->mpc_ops->mpo_ ## check != NULL)		\
272100979Srwatson			error = error_select(				\
273100979Srwatson			    mpc->mpc_ops->mpo_ ## check (args),		\
274100979Srwatson			    error);					\
275100979Srwatson	}								\
276100979Srwatson	MAC_POLICY_LIST_UNBUSY();					\
277100979Srwatson} while (0)
278100979Srwatson
279100979Srwatson/*
280100979Srwatson * MAC_BOOLEAN performs the designated boolean composition by walking
281100979Srwatson * the module list, invoking each instance of the operation, and
282100979Srwatson * combining the results using the passed C operator.  Note that it
283100979Srwatson * returns its value via 'result' in the scope of the caller, which
284100979Srwatson * should be initialized by the caller in a meaningful way to get
285100979Srwatson * a meaningful result.
286100979Srwatson */
287100979Srwatson#define	MAC_BOOLEAN(operation, composition, args...) do {		\
288100979Srwatson	struct mac_policy_conf *mpc;					\
289100979Srwatson									\
290100979Srwatson	MAC_POLICY_LIST_BUSY();						\
291100979Srwatson	LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {			\
292100979Srwatson		if (mpc->mpc_ops->mpo_ ## operation != NULL)		\
293100979Srwatson			result = result composition			\
294100979Srwatson			    mpc->mpc_ops->mpo_ ## operation (args);	\
295100979Srwatson	}								\
296100979Srwatson	MAC_POLICY_LIST_UNBUSY();					\
297100979Srwatson} while (0)
298100979Srwatson
299100979Srwatson/*
300100979Srwatson * MAC_PERFORM performs the designated operation by walking the policy
301100979Srwatson * module list and invoking that operation for each policy.
302100979Srwatson */
303100979Srwatson#define	MAC_PERFORM(operation, args...) do {				\
304100979Srwatson	struct mac_policy_conf *mpc;					\
305100979Srwatson									\
306100979Srwatson	MAC_POLICY_LIST_BUSY();						\
307100979Srwatson	LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {			\
308100979Srwatson		if (mpc->mpc_ops->mpo_ ## operation != NULL)		\
309100979Srwatson			mpc->mpc_ops->mpo_ ## operation (args);		\
310100979Srwatson	}								\
311100979Srwatson	MAC_POLICY_LIST_UNBUSY();					\
312100979Srwatson} while (0)
313100979Srwatson
314100979Srwatson/*
315100979Srwatson * Initialize the MAC subsystem, including appropriate SMP locks.
316100979Srwatson */
317100979Srwatsonstatic void
318100979Srwatsonmac_init(void)
319100979Srwatson{
320100979Srwatson
321100979Srwatson	LIST_INIT(&mac_policy_list);
322100979Srwatson	MAC_POLICY_LIST_LOCKINIT();
323100979Srwatson}
324100979Srwatson
325100979Srwatson/*
326100979Srwatson * For the purposes of modules that want to know if they were loaded
327100979Srwatson * "early", set the mac_late flag once we've processed modules either
328100979Srwatson * linked into the kernel, or loaded before the kernel startup.
329100979Srwatson */
330100979Srwatsonstatic void
331100979Srwatsonmac_late_init(void)
332100979Srwatson{
333100979Srwatson
334100979Srwatson	mac_late = 1;
335100979Srwatson}
336100979Srwatson
337100979Srwatson/*
338100979Srwatson * Allow MAC policy modules to register during boot, etc.
339100979Srwatson */
340100894Srwatsonint
341100979Srwatsonmac_policy_modevent(module_t mod, int type, void *data)
342100979Srwatson{
343100979Srwatson	struct mac_policy_conf *mpc;
344100979Srwatson	int error;
345100979Srwatson
346100979Srwatson	error = 0;
347100979Srwatson	mpc = (struct mac_policy_conf *) data;
348100979Srwatson
349100979Srwatson	switch (type) {
350100979Srwatson	case MOD_LOAD:
351100979Srwatson		if (mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_NOTLATE &&
352100979Srwatson		    mac_late) {
353100979Srwatson			printf("mac_policy_modevent: can't load %s policy "
354100979Srwatson			    "after booting\n", mpc->mpc_name);
355100979Srwatson			error = EBUSY;
356100979Srwatson			break;
357100979Srwatson		}
358100979Srwatson		error = mac_policy_register(mpc);
359100979Srwatson		break;
360100979Srwatson	case MOD_UNLOAD:
361100979Srwatson		/* Don't unregister the module if it was never registered. */
362100979Srwatson		if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED)
363100979Srwatson		    != 0)
364100979Srwatson			error = mac_policy_unregister(mpc);
365100979Srwatson		else
366100979Srwatson			error = 0;
367100979Srwatson		break;
368100979Srwatson	default:
369100979Srwatson		break;
370100979Srwatson	}
371100979Srwatson
372100979Srwatson	return (error);
373100979Srwatson}
374100979Srwatson
375100979Srwatsonstatic int
376100979Srwatsonmac_policy_register(struct mac_policy_conf *mpc)
377100979Srwatson{
378100979Srwatson	struct mac_policy_conf *tmpc;
379100979Srwatson	struct mac_policy_op_entry *mpe;
380100979Srwatson	int slot;
381100979Srwatson
382103570Srwatson	MALLOC(mpc->mpc_ops, struct mac_policy_ops *, sizeof(*mpc->mpc_ops),
383103570Srwatson	    M_MACOPVEC, M_WAITOK | M_ZERO);
384100979Srwatson	for (mpe = mpc->mpc_entries; mpe->mpe_constant != MAC_OP_LAST; mpe++) {
385100979Srwatson		switch (mpe->mpe_constant) {
386100979Srwatson		case MAC_OP_LAST:
387100979Srwatson			/*
388100979Srwatson			 * Doesn't actually happen, but this allows checking
389100979Srwatson			 * that all enumerated values are handled.
390100979Srwatson			 */
391100979Srwatson			break;
392100979Srwatson		case MAC_DESTROY:
393100979Srwatson			mpc->mpc_ops->mpo_destroy =
394100979Srwatson			    mpe->mpe_function;
395100979Srwatson			break;
396100979Srwatson		case MAC_INIT:
397100979Srwatson			mpc->mpc_ops->mpo_init =
398100979Srwatson			    mpe->mpe_function;
399100979Srwatson			break;
400102123Srwatson		case MAC_SYSCALL:
401102123Srwatson			mpc->mpc_ops->mpo_syscall =
402102123Srwatson			    mpe->mpe_function;
403102123Srwatson			break;
404104514Srwatson		case MAC_INIT_BPFDESC_LABEL:
405104514Srwatson			mpc->mpc_ops->mpo_init_bpfdesc_label =
406100979Srwatson			    mpe->mpe_function;
407100979Srwatson			break;
408104514Srwatson		case MAC_INIT_CRED_LABEL:
409104514Srwatson			mpc->mpc_ops->mpo_init_cred_label =
410100979Srwatson			    mpe->mpe_function;
411100979Srwatson			break;
412104514Srwatson		case MAC_INIT_DEVFSDIRENT_LABEL:
413104514Srwatson			mpc->mpc_ops->mpo_init_devfsdirent_label =
414100979Srwatson			    mpe->mpe_function;
415100979Srwatson			break;
416104514Srwatson		case MAC_INIT_IFNET_LABEL:
417104514Srwatson			mpc->mpc_ops->mpo_init_ifnet_label =
418100979Srwatson			    mpe->mpe_function;
419100979Srwatson			break;
420104514Srwatson		case MAC_INIT_IPQ_LABEL:
421104514Srwatson			mpc->mpc_ops->mpo_init_ipq_label =
422100979Srwatson			    mpe->mpe_function;
423100979Srwatson			break;
424104514Srwatson		case MAC_INIT_MBUF_LABEL:
425104514Srwatson			mpc->mpc_ops->mpo_init_mbuf_label =
426100979Srwatson			    mpe->mpe_function;
427100979Srwatson			break;
428104514Srwatson		case MAC_INIT_MOUNT_LABEL:
429104514Srwatson			mpc->mpc_ops->mpo_init_mount_label =
430100979Srwatson			    mpe->mpe_function;
431100979Srwatson			break;
432104514Srwatson		case MAC_INIT_MOUNT_FS_LABEL:
433104514Srwatson			mpc->mpc_ops->mpo_init_mount_fs_label =
434100979Srwatson			    mpe->mpe_function;
435100979Srwatson			break;
436104514Srwatson		case MAC_INIT_PIPE_LABEL:
437104514Srwatson			mpc->mpc_ops->mpo_init_pipe_label =
438100979Srwatson			    mpe->mpe_function;
439100979Srwatson			break;
440104514Srwatson		case MAC_INIT_SOCKET_LABEL:
441104514Srwatson			mpc->mpc_ops->mpo_init_socket_label =
442100979Srwatson			    mpe->mpe_function;
443100979Srwatson			break;
444104514Srwatson		case MAC_INIT_SOCKET_PEER_LABEL:
445104514Srwatson			mpc->mpc_ops->mpo_init_socket_peer_label =
446100979Srwatson			    mpe->mpe_function;
447100979Srwatson			break;
448104514Srwatson		case MAC_INIT_TEMP_LABEL:
449104514Srwatson			mpc->mpc_ops->mpo_init_temp_label =
450100979Srwatson			    mpe->mpe_function;
451100979Srwatson			break;
452104514Srwatson		case MAC_INIT_VNODE_LABEL:
453104514Srwatson			mpc->mpc_ops->mpo_init_vnode_label =
454100979Srwatson			    mpe->mpe_function;
455100979Srwatson			break;
456104514Srwatson		case MAC_DESTROY_BPFDESC_LABEL:
457104514Srwatson			mpc->mpc_ops->mpo_destroy_bpfdesc_label =
458100979Srwatson			    mpe->mpe_function;
459100979Srwatson			break;
460104514Srwatson		case MAC_DESTROY_CRED_LABEL:
461104514Srwatson			mpc->mpc_ops->mpo_destroy_cred_label =
462100979Srwatson			    mpe->mpe_function;
463100979Srwatson			break;
464104514Srwatson		case MAC_DESTROY_DEVFSDIRENT_LABEL:
465104514Srwatson			mpc->mpc_ops->mpo_destroy_devfsdirent_label =
466100979Srwatson			    mpe->mpe_function;
467100979Srwatson			break;
468104514Srwatson		case MAC_DESTROY_IFNET_LABEL:
469104514Srwatson			mpc->mpc_ops->mpo_destroy_ifnet_label =
470100979Srwatson			    mpe->mpe_function;
471100979Srwatson			break;
472104514Srwatson		case MAC_DESTROY_IPQ_LABEL:
473104514Srwatson			mpc->mpc_ops->mpo_destroy_ipq_label =
474100979Srwatson			    mpe->mpe_function;
475100979Srwatson			break;
476104514Srwatson		case MAC_DESTROY_MBUF_LABEL:
477104514Srwatson			mpc->mpc_ops->mpo_destroy_mbuf_label =
478100979Srwatson			    mpe->mpe_function;
479100979Srwatson			break;
480104514Srwatson		case MAC_DESTROY_MOUNT_LABEL:
481104514Srwatson			mpc->mpc_ops->mpo_destroy_mount_label =
482100979Srwatson			    mpe->mpe_function;
483100979Srwatson			break;
484104514Srwatson		case MAC_DESTROY_MOUNT_FS_LABEL:
485104514Srwatson			mpc->mpc_ops->mpo_destroy_mount_fs_label =
486100979Srwatson			    mpe->mpe_function;
487100979Srwatson			break;
488104514Srwatson		case MAC_DESTROY_PIPE_LABEL:
489104514Srwatson			mpc->mpc_ops->mpo_destroy_pipe_label =
490100979Srwatson			    mpe->mpe_function;
491100979Srwatson			break;
492104514Srwatson		case MAC_DESTROY_SOCKET_LABEL:
493104514Srwatson			mpc->mpc_ops->mpo_destroy_socket_label =
494104514Srwatson			    mpe->mpe_function;
495104514Srwatson			break;
496104514Srwatson		case MAC_DESTROY_SOCKET_PEER_LABEL:
497104514Srwatson			mpc->mpc_ops->mpo_destroy_socket_peer_label =
498104514Srwatson			    mpe->mpe_function;
499104514Srwatson			break;
500104514Srwatson		case MAC_DESTROY_TEMP_LABEL:
501104514Srwatson			mpc->mpc_ops->mpo_destroy_temp_label =
502104514Srwatson			    mpe->mpe_function;
503104514Srwatson			break;
504104514Srwatson		case MAC_DESTROY_VNODE_LABEL:
505104514Srwatson			mpc->mpc_ops->mpo_destroy_vnode_label =
506104514Srwatson			    mpe->mpe_function;
507104514Srwatson			break;
508100979Srwatson		case MAC_EXTERNALIZE:
509100979Srwatson			mpc->mpc_ops->mpo_externalize =
510100979Srwatson			    mpe->mpe_function;
511100979Srwatson			break;
512100979Srwatson		case MAC_INTERNALIZE:
513100979Srwatson			mpc->mpc_ops->mpo_internalize =
514100979Srwatson			    mpe->mpe_function;
515100979Srwatson			break;
516100979Srwatson		case MAC_CREATE_DEVFS_DEVICE:
517100979Srwatson			mpc->mpc_ops->mpo_create_devfs_device =
518100979Srwatson			    mpe->mpe_function;
519100979Srwatson			break;
520100979Srwatson		case MAC_CREATE_DEVFS_DIRECTORY:
521100979Srwatson			mpc->mpc_ops->mpo_create_devfs_directory =
522100979Srwatson			    mpe->mpe_function;
523100979Srwatson			break;
524104533Srwatson		case MAC_CREATE_DEVFS_SYMLINK:
525104533Srwatson			mpc->mpc_ops->mpo_create_devfs_symlink =
526104533Srwatson			    mpe->mpe_function;
527104533Srwatson			break;
528100979Srwatson		case MAC_CREATE_DEVFS_VNODE:
529100979Srwatson			mpc->mpc_ops->mpo_create_devfs_vnode =
530100979Srwatson			    mpe->mpe_function;
531100979Srwatson			break;
532100979Srwatson		case MAC_STDCREATEVNODE_EA:
533100979Srwatson			mpc->mpc_ops->mpo_stdcreatevnode_ea =
534100979Srwatson			    mpe->mpe_function;
535100979Srwatson			break;
536100979Srwatson		case MAC_CREATE_VNODE:
537100979Srwatson			mpc->mpc_ops->mpo_create_vnode =
538100979Srwatson			    mpe->mpe_function;
539100979Srwatson			break;
540100979Srwatson		case MAC_CREATE_MOUNT:
541100979Srwatson			mpc->mpc_ops->mpo_create_mount =
542100979Srwatson			    mpe->mpe_function;
543100979Srwatson			break;
544100979Srwatson		case MAC_CREATE_ROOT_MOUNT:
545100979Srwatson			mpc->mpc_ops->mpo_create_root_mount =
546100979Srwatson			    mpe->mpe_function;
547100979Srwatson			break;
548100979Srwatson		case MAC_RELABEL_VNODE:
549100979Srwatson			mpc->mpc_ops->mpo_relabel_vnode =
550100979Srwatson			    mpe->mpe_function;
551100979Srwatson			break;
552100979Srwatson		case MAC_UPDATE_DEVFSDIRENT:
553100979Srwatson			mpc->mpc_ops->mpo_update_devfsdirent =
554100979Srwatson			    mpe->mpe_function;
555100979Srwatson			break;
556100979Srwatson		case MAC_UPDATE_PROCFSVNODE:
557100979Srwatson			mpc->mpc_ops->mpo_update_procfsvnode =
558100979Srwatson			    mpe->mpe_function;
559100979Srwatson			break;
560100979Srwatson		case MAC_UPDATE_VNODE_FROM_EXTATTR:
561100979Srwatson			mpc->mpc_ops->mpo_update_vnode_from_extattr =
562100979Srwatson			    mpe->mpe_function;
563100979Srwatson			break;
564100979Srwatson		case MAC_UPDATE_VNODE_FROM_EXTERNALIZED:
565100979Srwatson			mpc->mpc_ops->mpo_update_vnode_from_externalized =
566100979Srwatson			    mpe->mpe_function;
567100979Srwatson			break;
568100979Srwatson		case MAC_UPDATE_VNODE_FROM_MOUNT:
569100979Srwatson			mpc->mpc_ops->mpo_update_vnode_from_mount =
570100979Srwatson			    mpe->mpe_function;
571100979Srwatson			break;
572100979Srwatson		case MAC_CREATE_MBUF_FROM_SOCKET:
573100979Srwatson			mpc->mpc_ops->mpo_create_mbuf_from_socket =
574100979Srwatson			    mpe->mpe_function;
575100979Srwatson			break;
576100979Srwatson		case MAC_CREATE_PIPE:
577100979Srwatson			mpc->mpc_ops->mpo_create_pipe =
578100979Srwatson			    mpe->mpe_function;
579100979Srwatson			break;
580100979Srwatson		case MAC_CREATE_SOCKET:
581100979Srwatson			mpc->mpc_ops->mpo_create_socket =
582100979Srwatson			    mpe->mpe_function;
583100979Srwatson			break;
584100979Srwatson		case MAC_CREATE_SOCKET_FROM_SOCKET:
585100979Srwatson			mpc->mpc_ops->mpo_create_socket_from_socket =
586100979Srwatson			    mpe->mpe_function;
587100979Srwatson			break;
588100979Srwatson		case MAC_RELABEL_PIPE:
589100979Srwatson			mpc->mpc_ops->mpo_relabel_pipe =
590100979Srwatson			    mpe->mpe_function;
591100979Srwatson			break;
592100979Srwatson		case MAC_RELABEL_SOCKET:
593100979Srwatson			mpc->mpc_ops->mpo_relabel_socket =
594100979Srwatson			    mpe->mpe_function;
595100979Srwatson			break;
596100979Srwatson		case MAC_SET_SOCKET_PEER_FROM_MBUF:
597100979Srwatson			mpc->mpc_ops->mpo_set_socket_peer_from_mbuf =
598100979Srwatson			    mpe->mpe_function;
599100979Srwatson			break;
600100979Srwatson		case MAC_SET_SOCKET_PEER_FROM_SOCKET:
601100979Srwatson			mpc->mpc_ops->mpo_set_socket_peer_from_socket =
602100979Srwatson			    mpe->mpe_function;
603100979Srwatson			break;
604100979Srwatson		case MAC_CREATE_BPFDESC:
605100979Srwatson			mpc->mpc_ops->mpo_create_bpfdesc =
606100979Srwatson			    mpe->mpe_function;
607100979Srwatson			break;
608100979Srwatson		case MAC_CREATE_DATAGRAM_FROM_IPQ:
609100979Srwatson			mpc->mpc_ops->mpo_create_datagram_from_ipq =
610100979Srwatson			    mpe->mpe_function;
611100979Srwatson			break;
612100979Srwatson		case MAC_CREATE_FRAGMENT:
613100979Srwatson			mpc->mpc_ops->mpo_create_fragment =
614100979Srwatson			    mpe->mpe_function;
615100979Srwatson			break;
616100979Srwatson		case MAC_CREATE_IFNET:
617100979Srwatson			mpc->mpc_ops->mpo_create_ifnet =
618100979Srwatson			    mpe->mpe_function;
619100979Srwatson			break;
620100979Srwatson		case MAC_CREATE_IPQ:
621100979Srwatson			mpc->mpc_ops->mpo_create_ipq =
622100979Srwatson			    mpe->mpe_function;
623100979Srwatson			break;
624100979Srwatson		case MAC_CREATE_MBUF_FROM_MBUF:
625100979Srwatson			mpc->mpc_ops->mpo_create_mbuf_from_mbuf =
626100979Srwatson			    mpe->mpe_function;
627100979Srwatson			break;
628100979Srwatson		case MAC_CREATE_MBUF_LINKLAYER:
629100979Srwatson			mpc->mpc_ops->mpo_create_mbuf_linklayer =
630100979Srwatson			    mpe->mpe_function;
631100979Srwatson			break;
632100979Srwatson		case MAC_CREATE_MBUF_FROM_BPFDESC:
633100979Srwatson			mpc->mpc_ops->mpo_create_mbuf_from_bpfdesc =
634100979Srwatson			    mpe->mpe_function;
635100979Srwatson			break;
636100979Srwatson		case MAC_CREATE_MBUF_FROM_IFNET:
637100979Srwatson			mpc->mpc_ops->mpo_create_mbuf_from_ifnet =
638100979Srwatson			    mpe->mpe_function;
639100979Srwatson			break;
640100979Srwatson		case MAC_CREATE_MBUF_MULTICAST_ENCAP:
641100979Srwatson			mpc->mpc_ops->mpo_create_mbuf_multicast_encap =
642100979Srwatson			    mpe->mpe_function;
643100979Srwatson			break;
644100979Srwatson		case MAC_CREATE_MBUF_NETLAYER:
645100979Srwatson			mpc->mpc_ops->mpo_create_mbuf_netlayer =
646100979Srwatson			    mpe->mpe_function;
647100979Srwatson			break;
648100979Srwatson		case MAC_FRAGMENT_MATCH:
649100979Srwatson			mpc->mpc_ops->mpo_fragment_match =
650100979Srwatson			    mpe->mpe_function;
651100979Srwatson			break;
652100979Srwatson		case MAC_RELABEL_IFNET:
653100979Srwatson			mpc->mpc_ops->mpo_relabel_ifnet =
654100979Srwatson			    mpe->mpe_function;
655100979Srwatson			break;
656100979Srwatson		case MAC_UPDATE_IPQ:
657100979Srwatson			mpc->mpc_ops->mpo_update_ipq =
658100979Srwatson			    mpe->mpe_function;
659100979Srwatson			break;
660100979Srwatson		case MAC_CREATE_CRED:
661100979Srwatson			mpc->mpc_ops->mpo_create_cred =
662100979Srwatson			    mpe->mpe_function;
663100979Srwatson			break;
664100979Srwatson		case MAC_EXECVE_TRANSITION:
665100979Srwatson			mpc->mpc_ops->mpo_execve_transition =
666100979Srwatson			    mpe->mpe_function;
667100979Srwatson			break;
668100979Srwatson		case MAC_EXECVE_WILL_TRANSITION:
669100979Srwatson			mpc->mpc_ops->mpo_execve_will_transition =
670100979Srwatson			    mpe->mpe_function;
671100979Srwatson			break;
672100979Srwatson		case MAC_CREATE_PROC0:
673104518Srwatson			mpc->mpc_ops->mpo_create_proc0 =
674104518Srwatson			    mpe->mpe_function;
675100979Srwatson			break;
676100979Srwatson		case MAC_CREATE_PROC1:
677104518Srwatson			mpc->mpc_ops->mpo_create_proc1 =
678104518Srwatson			    mpe->mpe_function;
679100979Srwatson			break;
680100979Srwatson		case MAC_RELABEL_CRED:
681100979Srwatson			mpc->mpc_ops->mpo_relabel_cred =
682100979Srwatson			    mpe->mpe_function;
683100979Srwatson			break;
684104338Srwatson		case MAC_THREAD_USERRET:
685104338Srwatson			mpc->mpc_ops->mpo_thread_userret =
686104338Srwatson			    mpe->mpe_function;
687104338Srwatson			break;
688100979Srwatson		case MAC_CHECK_BPFDESC_RECEIVE:
689100979Srwatson			mpc->mpc_ops->mpo_check_bpfdesc_receive =
690100979Srwatson			    mpe->mpe_function;
691100979Srwatson			break;
692100979Srwatson		case MAC_CHECK_CRED_RELABEL:
693100979Srwatson			mpc->mpc_ops->mpo_check_cred_relabel =
694100979Srwatson			    mpe->mpe_function;
695100979Srwatson			break;
696100979Srwatson		case MAC_CHECK_CRED_VISIBLE:
697100979Srwatson			mpc->mpc_ops->mpo_check_cred_visible =
698100979Srwatson			    mpe->mpe_function;
699100979Srwatson			break;
700100979Srwatson		case MAC_CHECK_IFNET_RELABEL:
701100979Srwatson			mpc->mpc_ops->mpo_check_ifnet_relabel =
702100979Srwatson			    mpe->mpe_function;
703100979Srwatson			break;
704100979Srwatson		case MAC_CHECK_IFNET_TRANSMIT:
705100979Srwatson			mpc->mpc_ops->mpo_check_ifnet_transmit =
706100979Srwatson			    mpe->mpe_function;
707100979Srwatson			break;
708100979Srwatson		case MAC_CHECK_MOUNT_STAT:
709100979Srwatson			mpc->mpc_ops->mpo_check_mount_stat =
710100979Srwatson			    mpe->mpe_function;
711100979Srwatson			break;
712100979Srwatson		case MAC_CHECK_PIPE_IOCTL:
713100979Srwatson			mpc->mpc_ops->mpo_check_pipe_ioctl =
714100979Srwatson			    mpe->mpe_function;
715100979Srwatson			break;
716102115Srwatson		case MAC_CHECK_PIPE_POLL:
717102115Srwatson			mpc->mpc_ops->mpo_check_pipe_poll =
718100979Srwatson			    mpe->mpe_function;
719100979Srwatson			break;
720102115Srwatson		case MAC_CHECK_PIPE_READ:
721102115Srwatson			mpc->mpc_ops->mpo_check_pipe_read =
722102115Srwatson			    mpe->mpe_function;
723102115Srwatson			break;
724100979Srwatson		case MAC_CHECK_PIPE_RELABEL:
725100979Srwatson			mpc->mpc_ops->mpo_check_pipe_relabel =
726100979Srwatson			    mpe->mpe_function;
727100979Srwatson			break;
728102115Srwatson		case MAC_CHECK_PIPE_STAT:
729102115Srwatson			mpc->mpc_ops->mpo_check_pipe_stat =
730102115Srwatson			    mpe->mpe_function;
731102115Srwatson			break;
732102115Srwatson		case MAC_CHECK_PIPE_WRITE:
733102115Srwatson			mpc->mpc_ops->mpo_check_pipe_write =
734102115Srwatson			    mpe->mpe_function;
735102115Srwatson			break;
736100979Srwatson		case MAC_CHECK_PROC_DEBUG:
737100979Srwatson			mpc->mpc_ops->mpo_check_proc_debug =
738100979Srwatson			    mpe->mpe_function;
739100979Srwatson			break;
740100979Srwatson		case MAC_CHECK_PROC_SCHED:
741100979Srwatson			mpc->mpc_ops->mpo_check_proc_sched =
742100979Srwatson			    mpe->mpe_function;
743100979Srwatson			break;
744100979Srwatson		case MAC_CHECK_PROC_SIGNAL:
745100979Srwatson			mpc->mpc_ops->mpo_check_proc_signal =
746100979Srwatson			    mpe->mpe_function;
747100979Srwatson			break;
748100979Srwatson		case MAC_CHECK_SOCKET_BIND:
749100979Srwatson			mpc->mpc_ops->mpo_check_socket_bind =
750100979Srwatson			    mpe->mpe_function;
751100979Srwatson			break;
752100979Srwatson		case MAC_CHECK_SOCKET_CONNECT:
753100979Srwatson			mpc->mpc_ops->mpo_check_socket_connect =
754100979Srwatson			    mpe->mpe_function;
755100979Srwatson			break;
756101933Srwatson		case MAC_CHECK_SOCKET_DELIVER:
757101933Srwatson			mpc->mpc_ops->mpo_check_socket_deliver =
758101933Srwatson			    mpe->mpe_function;
759101933Srwatson			break;
760100979Srwatson		case MAC_CHECK_SOCKET_LISTEN:
761100979Srwatson			mpc->mpc_ops->mpo_check_socket_listen =
762100979Srwatson			    mpe->mpe_function;
763100979Srwatson			break;
764100979Srwatson		case MAC_CHECK_SOCKET_RELABEL:
765100979Srwatson			mpc->mpc_ops->mpo_check_socket_relabel =
766100979Srwatson			    mpe->mpe_function;
767100979Srwatson			break;
768100979Srwatson		case MAC_CHECK_SOCKET_VISIBLE:
769100979Srwatson			mpc->mpc_ops->mpo_check_socket_visible =
770100979Srwatson			    mpe->mpe_function;
771100979Srwatson			break;
772100979Srwatson		case MAC_CHECK_VNODE_ACCESS:
773100979Srwatson			mpc->mpc_ops->mpo_check_vnode_access =
774100979Srwatson			    mpe->mpe_function;
775100979Srwatson			break;
776100979Srwatson		case MAC_CHECK_VNODE_CHDIR:
777100979Srwatson			mpc->mpc_ops->mpo_check_vnode_chdir =
778100979Srwatson			    mpe->mpe_function;
779100979Srwatson			break;
780100979Srwatson		case MAC_CHECK_VNODE_CHROOT:
781100979Srwatson			mpc->mpc_ops->mpo_check_vnode_chroot =
782100979Srwatson			    mpe->mpe_function;
783100979Srwatson			break;
784100979Srwatson		case MAC_CHECK_VNODE_CREATE:
785100979Srwatson			mpc->mpc_ops->mpo_check_vnode_create =
786100979Srwatson			    mpe->mpe_function;
787100979Srwatson			break;
788100979Srwatson		case MAC_CHECK_VNODE_DELETE:
789100979Srwatson			mpc->mpc_ops->mpo_check_vnode_delete =
790100979Srwatson			    mpe->mpe_function;
791100979Srwatson			break;
792100979Srwatson		case MAC_CHECK_VNODE_DELETEACL:
793100979Srwatson			mpc->mpc_ops->mpo_check_vnode_deleteacl =
794100979Srwatson			    mpe->mpe_function;
795100979Srwatson			break;
796100979Srwatson		case MAC_CHECK_VNODE_EXEC:
797100979Srwatson			mpc->mpc_ops->mpo_check_vnode_exec =
798100979Srwatson			    mpe->mpe_function;
799100979Srwatson			break;
800100979Srwatson		case MAC_CHECK_VNODE_GETACL:
801100979Srwatson			mpc->mpc_ops->mpo_check_vnode_getacl =
802100979Srwatson			    mpe->mpe_function;
803100979Srwatson			break;
804100979Srwatson		case MAC_CHECK_VNODE_GETEXTATTR:
805100979Srwatson			mpc->mpc_ops->mpo_check_vnode_getextattr =
806100979Srwatson			    mpe->mpe_function;
807100979Srwatson			break;
808104529Srwatson		case MAC_CHECK_VNODE_LINK:
809104529Srwatson			mpc->mpc_ops->mpo_check_vnode_link =
810104529Srwatson			    mpe->mpe_function;
811104529Srwatson			break;
812100979Srwatson		case MAC_CHECK_VNODE_LOOKUP:
813100979Srwatson			mpc->mpc_ops->mpo_check_vnode_lookup =
814100979Srwatson			    mpe->mpe_function;
815100979Srwatson			break;
816104546Srwatson		case MAC_CHECK_VNODE_MMAP:
817104546Srwatson			mpc->mpc_ops->mpo_check_vnode_mmap =
818100979Srwatson			    mpe->mpe_function;
819100979Srwatson			break;
820104546Srwatson		case MAC_CHECK_VNODE_MMAP_DOWNGRADE:
821104546Srwatson			mpc->mpc_ops->mpo_check_vnode_mmap_downgrade =
822104546Srwatson			    mpe->mpe_function;
823104546Srwatson			break;
824104546Srwatson		case MAC_CHECK_VNODE_MPROTECT:
825104546Srwatson			mpc->mpc_ops->mpo_check_vnode_mprotect =
826104546Srwatson			    mpe->mpe_function;
827104546Srwatson			break;
828100979Srwatson		case MAC_CHECK_VNODE_OPEN:
829100979Srwatson			mpc->mpc_ops->mpo_check_vnode_open =
830100979Srwatson			    mpe->mpe_function;
831100979Srwatson			break;
832102112Srwatson		case MAC_CHECK_VNODE_POLL:
833102112Srwatson			mpc->mpc_ops->mpo_check_vnode_poll =
834102112Srwatson			    mpe->mpe_function;
835102112Srwatson			break;
836102112Srwatson		case MAC_CHECK_VNODE_READ:
837102112Srwatson			mpc->mpc_ops->mpo_check_vnode_read =
838102112Srwatson			    mpe->mpe_function;
839102112Srwatson			break;
840100979Srwatson		case MAC_CHECK_VNODE_READDIR:
841100979Srwatson			mpc->mpc_ops->mpo_check_vnode_readdir =
842100979Srwatson			    mpe->mpe_function;
843100979Srwatson			break;
844100979Srwatson		case MAC_CHECK_VNODE_READLINK:
845100979Srwatson			mpc->mpc_ops->mpo_check_vnode_readlink =
846100979Srwatson			    mpe->mpe_function;
847100979Srwatson			break;
848100979Srwatson		case MAC_CHECK_VNODE_RELABEL:
849100979Srwatson			mpc->mpc_ops->mpo_check_vnode_relabel =
850100979Srwatson			    mpe->mpe_function;
851100979Srwatson			break;
852100979Srwatson		case MAC_CHECK_VNODE_RENAME_FROM:
853100979Srwatson			mpc->mpc_ops->mpo_check_vnode_rename_from =
854100979Srwatson			    mpe->mpe_function;
855100979Srwatson			break;
856100979Srwatson		case MAC_CHECK_VNODE_RENAME_TO:
857100979Srwatson			mpc->mpc_ops->mpo_check_vnode_rename_to =
858100979Srwatson			    mpe->mpe_function;
859100979Srwatson			break;
860100979Srwatson		case MAC_CHECK_VNODE_REVOKE:
861100979Srwatson			mpc->mpc_ops->mpo_check_vnode_revoke =
862100979Srwatson			    mpe->mpe_function;
863100979Srwatson			break;
864100979Srwatson		case MAC_CHECK_VNODE_SETACL:
865100979Srwatson			mpc->mpc_ops->mpo_check_vnode_setacl =
866100979Srwatson			    mpe->mpe_function;
867100979Srwatson			break;
868100979Srwatson		case MAC_CHECK_VNODE_SETEXTATTR:
869100979Srwatson			mpc->mpc_ops->mpo_check_vnode_setextattr =
870100979Srwatson			    mpe->mpe_function;
871100979Srwatson			break;
872100979Srwatson		case MAC_CHECK_VNODE_SETFLAGS:
873100979Srwatson			mpc->mpc_ops->mpo_check_vnode_setflags =
874100979Srwatson			    mpe->mpe_function;
875100979Srwatson			break;
876100979Srwatson		case MAC_CHECK_VNODE_SETMODE:
877100979Srwatson			mpc->mpc_ops->mpo_check_vnode_setmode =
878100979Srwatson			    mpe->mpe_function;
879100979Srwatson			break;
880100979Srwatson		case MAC_CHECK_VNODE_SETOWNER:
881100979Srwatson			mpc->mpc_ops->mpo_check_vnode_setowner =
882100979Srwatson			    mpe->mpe_function;
883100979Srwatson			break;
884100979Srwatson		case MAC_CHECK_VNODE_SETUTIMES:
885100979Srwatson			mpc->mpc_ops->mpo_check_vnode_setutimes =
886100979Srwatson			    mpe->mpe_function;
887100979Srwatson			break;
888100979Srwatson		case MAC_CHECK_VNODE_STAT:
889100979Srwatson			mpc->mpc_ops->mpo_check_vnode_stat =
890100979Srwatson			    mpe->mpe_function;
891100979Srwatson			break;
892102112Srwatson		case MAC_CHECK_VNODE_WRITE:
893102112Srwatson			mpc->mpc_ops->mpo_check_vnode_write =
894102112Srwatson			    mpe->mpe_function;
895102112Srwatson			break;
896100979Srwatson/*
897100979Srwatson		default:
898100979Srwatson			printf("MAC policy `%s': unknown operation %d\n",
899100979Srwatson			    mpc->mpc_name, mpe->mpe_constant);
900100979Srwatson			return (EINVAL);
901100979Srwatson*/
902100979Srwatson		}
903100979Srwatson	}
904100979Srwatson	MAC_POLICY_LIST_LOCK();
905100979Srwatson	if (mac_policy_list_busy > 0) {
906100979Srwatson		MAC_POLICY_LIST_UNLOCK();
907100979Srwatson		FREE(mpc->mpc_ops, M_MACOPVEC);
908100979Srwatson		mpc->mpc_ops = NULL;
909100979Srwatson		return (EBUSY);
910100979Srwatson	}
911100979Srwatson	LIST_FOREACH(tmpc, &mac_policy_list, mpc_list) {
912100979Srwatson		if (strcmp(tmpc->mpc_name, mpc->mpc_name) == 0) {
913100979Srwatson			MAC_POLICY_LIST_UNLOCK();
914100979Srwatson			FREE(mpc->mpc_ops, M_MACOPVEC);
915100979Srwatson			mpc->mpc_ops = NULL;
916100979Srwatson			return (EEXIST);
917100979Srwatson		}
918100979Srwatson	}
919100979Srwatson	if (mpc->mpc_field_off != NULL) {
920100979Srwatson		slot = ffs(mac_policy_offsets_free);
921100979Srwatson		if (slot == 0) {
922100979Srwatson			MAC_POLICY_LIST_UNLOCK();
923100979Srwatson			FREE(mpc->mpc_ops, M_MACOPVEC);
924100979Srwatson			mpc->mpc_ops = NULL;
925100979Srwatson			return (ENOMEM);
926100979Srwatson		}
927100979Srwatson		slot--;
928100979Srwatson		mac_policy_offsets_free &= ~(1 << slot);
929100979Srwatson		*mpc->mpc_field_off = slot;
930100979Srwatson	}
931100979Srwatson	mpc->mpc_runtime_flags |= MPC_RUNTIME_FLAG_REGISTERED;
932100979Srwatson	LIST_INSERT_HEAD(&mac_policy_list, mpc, mpc_list);
933100979Srwatson
934100979Srwatson	/* Per-policy initialization. */
935100979Srwatson	if (mpc->mpc_ops->mpo_init != NULL)
936100979Srwatson		(*(mpc->mpc_ops->mpo_init))(mpc);
937100979Srwatson	MAC_POLICY_LIST_UNLOCK();
938100979Srwatson
939100979Srwatson	printf("Security policy loaded: %s (%s)\n", mpc->mpc_fullname,
940100979Srwatson	    mpc->mpc_name);
941100979Srwatson
942100979Srwatson	return (0);
943100979Srwatson}
944100979Srwatson
945100979Srwatsonstatic int
946100979Srwatsonmac_policy_unregister(struct mac_policy_conf *mpc)
947100979Srwatson{
948100979Srwatson
949104520Srwatson	/*
950104520Srwatson	 * If we fail the load, we may get a request to unload.  Check
951104520Srwatson	 * to see if we did the run-time registration, and if not,
952104520Srwatson	 * silently succeed.
953104520Srwatson	 */
954104520Srwatson	MAC_POLICY_LIST_LOCK();
955104520Srwatson	if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED) == 0) {
956104520Srwatson		MAC_POLICY_LIST_UNLOCK();
957104520Srwatson		return (0);
958104520Srwatson	}
959100979Srwatson#if 0
960100979Srwatson	/*
961100979Srwatson	 * Don't allow unloading modules with private data.
962100979Srwatson	 */
963104520Srwatson	if (mpc->mpc_field_off != NULL) {
964104520Srwatson		MAC_POLICY_LIST_UNLOCK();
965100979Srwatson		return (EBUSY);
966104520Srwatson	}
967100979Srwatson#endif
968104520Srwatson	/*
969104520Srwatson	 * Only allow the unload to proceed if the module is unloadable
970104520Srwatson	 * by its own definition.
971104520Srwatson	 */
972104520Srwatson	if ((mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK) == 0) {
973104520Srwatson		MAC_POLICY_LIST_UNLOCK();
974100979Srwatson		return (EBUSY);
975104520Srwatson	}
976104520Srwatson	/*
977104520Srwatson	 * Right now, we EBUSY if the list is in use.  In the future,
978104520Srwatson	 * for reliability reasons, we might want to sleep and wakeup
979104520Srwatson	 * later to try again.
980104520Srwatson	 */
981100979Srwatson	if (mac_policy_list_busy > 0) {
982100979Srwatson		MAC_POLICY_LIST_UNLOCK();
983100979Srwatson		return (EBUSY);
984100979Srwatson	}
985100979Srwatson	if (mpc->mpc_ops->mpo_destroy != NULL)
986100979Srwatson		(*(mpc->mpc_ops->mpo_destroy))(mpc);
987100979Srwatson
988100979Srwatson	LIST_REMOVE(mpc, mpc_list);
989100979Srwatson	MAC_POLICY_LIST_UNLOCK();
990100979Srwatson
991100979Srwatson	FREE(mpc->mpc_ops, M_MACOPVEC);
992100979Srwatson	mpc->mpc_ops = NULL;
993100979Srwatson
994100979Srwatson	printf("Security policy unload: %s (%s)\n", mpc->mpc_fullname,
995100979Srwatson	    mpc->mpc_name);
996100979Srwatson
997100979Srwatson	return (0);
998100979Srwatson}
999100979Srwatson
1000100979Srwatson/*
1001100979Srwatson * Define an error value precedence, and given two arguments, selects the
1002100979Srwatson * value with the higher precedence.
1003100979Srwatson */
1004100979Srwatsonstatic int
1005100979Srwatsonerror_select(int error1, int error2)
1006100979Srwatson{
1007100979Srwatson
1008100979Srwatson	/* Certain decision-making errors take top priority. */
1009100979Srwatson	if (error1 == EDEADLK || error2 == EDEADLK)
1010100979Srwatson		return (EDEADLK);
1011100979Srwatson
1012100979Srwatson	/* Invalid arguments should be reported where possible. */
1013100979Srwatson	if (error1 == EINVAL || error2 == EINVAL)
1014100979Srwatson		return (EINVAL);
1015100979Srwatson
1016100979Srwatson	/* Precedence goes to "visibility", with both process and file. */
1017100979Srwatson	if (error1 == ESRCH || error2 == ESRCH)
1018100979Srwatson		return (ESRCH);
1019100979Srwatson
1020100979Srwatson	if (error1 == ENOENT || error2 == ENOENT)
1021100979Srwatson		return (ENOENT);
1022100979Srwatson
1023100979Srwatson	/* Precedence goes to DAC/MAC protections. */
1024100979Srwatson	if (error1 == EACCES || error2 == EACCES)
1025100979Srwatson		return (EACCES);
1026100979Srwatson
1027100979Srwatson	/* Precedence goes to privilege. */
1028100979Srwatson	if (error1 == EPERM || error2 == EPERM)
1029100979Srwatson		return (EPERM);
1030100979Srwatson
1031100979Srwatson	/* Precedence goes to error over success; otherwise, arbitrary. */
1032100979Srwatson	if (error1 != 0)
1033100979Srwatson		return (error1);
1034100979Srwatson	return (error2);
1035100979Srwatson}
1036100979Srwatson
1037104521Srwatsonstatic void
1038104521Srwatsonmac_init_label(struct label *label)
1039104521Srwatson{
1040104521Srwatson
1041104521Srwatson	bzero(label, sizeof(*label));
1042104521Srwatson	label->l_flags = MAC_FLAG_INITIALIZED;
1043104521Srwatson}
1044104521Srwatson
1045104521Srwatsonstatic void
1046104521Srwatsonmac_destroy_label(struct label *label)
1047104521Srwatson{
1048104521Srwatson
1049104521Srwatson	KASSERT(label->l_flags & MAC_FLAG_INITIALIZED,
1050104521Srwatson	    ("destroying uninitialized label"));
1051104521Srwatson
1052104521Srwatson	bzero(label, sizeof(*label));
1053104521Srwatson	/* implicit: label->l_flags &= ~MAC_FLAG_INITIALIZED; */
1054104521Srwatson}
1055104521Srwatson
1056104521Srwatsonstatic void
1057104521Srwatsonmac_init_structmac(struct mac *mac)
1058104521Srwatson{
1059104521Srwatson
1060104521Srwatson	bzero(mac, sizeof(*mac));
1061104521Srwatson	mac->m_macflags = MAC_FLAG_INITIALIZED;
1062104521Srwatson}
1063104521Srwatson
1064100979Srwatsonvoid
1065104527Srwatsonmac_init_bpfdesc(struct bpf_d *bpf_d)
1066104521Srwatson{
1067104521Srwatson
1068104527Srwatson	mac_init_label(&bpf_d->bd_label);
1069104527Srwatson	MAC_PERFORM(init_bpfdesc_label, &bpf_d->bd_label);
1070104521Srwatson#ifdef MAC_DEBUG
1071104527Srwatson	atomic_add_int(&nmacbpfdescs, 1);
1072104521Srwatson#endif
1073104521Srwatson}
1074104521Srwatson
1075104521Srwatsonvoid
1076104521Srwatsonmac_init_cred(struct ucred *cr)
1077104521Srwatson{
1078104521Srwatson
1079104521Srwatson	mac_init_label(&cr->cr_label);
1080104521Srwatson	MAC_PERFORM(init_cred_label, &cr->cr_label);
1081104521Srwatson#ifdef MAC_DEBUG
1082104521Srwatson	atomic_add_int(&nmaccreds, 1);
1083104521Srwatson#endif
1084104521Srwatson}
1085104521Srwatson
1086104521Srwatsonvoid
1087104527Srwatsonmac_init_devfsdirent(struct devfs_dirent *de)
1088104521Srwatson{
1089104521Srwatson
1090104527Srwatson	mac_init_label(&de->de_label);
1091104527Srwatson	MAC_PERFORM(init_devfsdirent_label, &de->de_label);
1092104521Srwatson#ifdef MAC_DEBUG
1093104527Srwatson	atomic_add_int(&nmacdevfsdirents, 1);
1094104521Srwatson#endif
1095104521Srwatson}
1096104521Srwatson
1097104521Srwatsonvoid
1098104521Srwatsonmac_init_ifnet(struct ifnet *ifp)
1099104521Srwatson{
1100104521Srwatson
1101104521Srwatson	mac_init_label(&ifp->if_label);
1102104521Srwatson	MAC_PERFORM(init_ifnet_label, &ifp->if_label);
1103104521Srwatson#ifdef MAC_DEBUG
1104104521Srwatson	atomic_add_int(&nmacifnets, 1);
1105104521Srwatson#endif
1106104521Srwatson}
1107104521Srwatson
1108104521Srwatsonvoid
1109104527Srwatsonmac_init_ipq(struct ipq *ipq)
1110104521Srwatson{
1111104521Srwatson
1112104527Srwatson	mac_init_label(&ipq->ipq_label);
1113104527Srwatson	MAC_PERFORM(init_ipq_label, &ipq->ipq_label);
1114104521Srwatson#ifdef MAC_DEBUG
1115104527Srwatson	atomic_add_int(&nmacipqs, 1);
1116104521Srwatson#endif
1117104521Srwatson}
1118104521Srwatson
1119104527Srwatsonint
1120104527Srwatsonmac_init_mbuf(struct mbuf *m, int flag)
1121104527Srwatson{
1122104528Srwatson	int error;
1123104528Srwatson
1124104527Srwatson	KASSERT(m->m_flags & M_PKTHDR, ("mac_init_mbuf on non-header mbuf"));
1125104527Srwatson
1126104527Srwatson	mac_init_label(&m->m_pkthdr.label);
1127104527Srwatson
1128104528Srwatson	MAC_CHECK(init_mbuf_label, &m->m_pkthdr.label, flag);
1129104528Srwatson	if (error) {
1130104528Srwatson		MAC_PERFORM(destroy_mbuf_label, &m->m_pkthdr.label);
1131104528Srwatson		mac_destroy_label(&m->m_pkthdr.label);
1132104528Srwatson	}
1133104528Srwatson
1134104527Srwatson#ifdef MAC_DEBUG
1135104528Srwatson	if (error == 0)
1136104528Srwatson		atomic_add_int(&nmacmbufs, 1);
1137104527Srwatson#endif
1138104528Srwatson	return (error);
1139104527Srwatson}
1140104527Srwatson
1141104521Srwatsonvoid
1142104527Srwatsonmac_init_mount(struct mount *mp)
1143104521Srwatson{
1144104521Srwatson
1145104527Srwatson	mac_init_label(&mp->mnt_mntlabel);
1146104527Srwatson	mac_init_label(&mp->mnt_fslabel);
1147104527Srwatson	MAC_PERFORM(init_mount_label, &mp->mnt_mntlabel);
1148104527Srwatson	MAC_PERFORM(init_mount_fs_label, &mp->mnt_fslabel);
1149104521Srwatson#ifdef MAC_DEBUG
1150104527Srwatson	atomic_add_int(&nmacmounts, 1);
1151104521Srwatson#endif
1152104521Srwatson}
1153104521Srwatson
1154104521Srwatsonvoid
1155104527Srwatsonmac_init_pipe(struct pipe *pipe)
1156104521Srwatson{
1157104527Srwatson	struct label *label;
1158104521Srwatson
1159104527Srwatson	label = malloc(sizeof(struct label), M_MACPIPELABEL, M_ZERO|M_WAITOK);
1160104527Srwatson	mac_init_label(label);
1161104527Srwatson	pipe->pipe_label = label;
1162104527Srwatson	pipe->pipe_peer->pipe_label = label;
1163104527Srwatson	MAC_PERFORM(init_pipe_label, pipe->pipe_label);
1164104521Srwatson#ifdef MAC_DEBUG
1165104527Srwatson	atomic_add_int(&nmacpipes, 1);
1166104521Srwatson#endif
1167104521Srwatson}
1168104521Srwatson
1169104541Srwatsonstatic int
1170104541Srwatsonmac_init_socket_label(struct label *label, int flag)
1171104521Srwatson{
1172104541Srwatson	int error;
1173104521Srwatson
1174104541Srwatson	mac_init_label(label);
1175104541Srwatson
1176104541Srwatson	MAC_CHECK(init_socket_label, label, flag);
1177104541Srwatson	if (error) {
1178104541Srwatson		MAC_PERFORM(destroy_socket_label, label);
1179104541Srwatson		mac_destroy_label(label);
1180104541Srwatson	}
1181104541Srwatson
1182104521Srwatson#ifdef MAC_DEBUG
1183104541Srwatson	if (error == 0)
1184104541Srwatson		atomic_add_int(&nmacsockets, 1);
1185104521Srwatson#endif
1186104541Srwatson
1187104541Srwatson	return (error);
1188104521Srwatson}
1189104521Srwatson
1190104541Srwatsonstatic int
1191104541Srwatsonmac_init_socket_peer_label(struct label *label, int flag)
1192104541Srwatson{
1193104541Srwatson	int error;
1194104541Srwatson
1195104541Srwatson	mac_init_label(label);
1196104541Srwatson
1197104541Srwatson	MAC_CHECK(init_socket_peer_label, label, flag);
1198104541Srwatson	if (error) {
1199104541Srwatson		MAC_PERFORM(destroy_socket_label, label);
1200104541Srwatson		mac_destroy_label(label);
1201104541Srwatson	}
1202104541Srwatson
1203104541Srwatson	return (error);
1204104541Srwatson}
1205104541Srwatson
1206104541Srwatsonint
1207104541Srwatsonmac_init_socket(struct socket *socket, int flag)
1208104541Srwatson{
1209104541Srwatson	int error;
1210104541Srwatson
1211104541Srwatson	error = mac_init_socket_label(&socket->so_label, flag);
1212104541Srwatson	if (error)
1213104541Srwatson		return (error);
1214104541Srwatson
1215104541Srwatson	error = mac_init_socket_peer_label(&socket->so_peerlabel, flag);
1216104541Srwatson	if (error)
1217104541Srwatson		mac_destroy_socket_label(&socket->so_label);
1218104541Srwatson
1219104541Srwatson	return (error);
1220104541Srwatson}
1221104541Srwatson
1222104527Srwatsonstatic void
1223104527Srwatsonmac_init_temp(struct label *label)
1224104521Srwatson{
1225104521Srwatson
1226104527Srwatson	mac_init_label(label);
1227104527Srwatson	MAC_PERFORM(init_temp_label, label);
1228104521Srwatson#ifdef MAC_DEBUG
1229104527Srwatson	atomic_add_int(&nmactemp, 1);
1230104521Srwatson#endif
1231104521Srwatson}
1232104521Srwatson
1233104521Srwatsonvoid
1234104527Srwatsonmac_init_vnode(struct vnode *vp)
1235104521Srwatson{
1236104521Srwatson
1237104527Srwatson	mac_init_label(&vp->v_label);
1238104527Srwatson	MAC_PERFORM(init_vnode_label, &vp->v_label);
1239104521Srwatson#ifdef MAC_DEBUG
1240104527Srwatson	atomic_add_int(&nmacvnodes, 1);
1241104521Srwatson#endif
1242104521Srwatson}
1243104521Srwatson
1244104521Srwatsonvoid
1245104527Srwatsonmac_destroy_bpfdesc(struct bpf_d *bpf_d)
1246104521Srwatson{
1247104521Srwatson
1248104527Srwatson	MAC_PERFORM(destroy_bpfdesc_label, &bpf_d->bd_label);
1249104527Srwatson	mac_destroy_label(&bpf_d->bd_label);
1250104521Srwatson#ifdef MAC_DEBUG
1251104527Srwatson	atomic_subtract_int(&nmacbpfdescs, 1);
1252104521Srwatson#endif
1253104521Srwatson}
1254104521Srwatson
1255104521Srwatsonvoid
1256104527Srwatsonmac_destroy_cred(struct ucred *cr)
1257104521Srwatson{
1258104521Srwatson
1259104527Srwatson	MAC_PERFORM(destroy_cred_label, &cr->cr_label);
1260104527Srwatson	mac_destroy_label(&cr->cr_label);
1261104521Srwatson#ifdef MAC_DEBUG
1262104527Srwatson	atomic_subtract_int(&nmaccreds, 1);
1263104521Srwatson#endif
1264104521Srwatson}
1265104521Srwatson
1266104521Srwatsonvoid
1267104527Srwatsonmac_destroy_devfsdirent(struct devfs_dirent *de)
1268104521Srwatson{
1269104521Srwatson
1270104527Srwatson	MAC_PERFORM(destroy_devfsdirent_label, &de->de_label);
1271104527Srwatson	mac_destroy_label(&de->de_label);
1272104521Srwatson#ifdef MAC_DEBUG
1273104527Srwatson	atomic_subtract_int(&nmacdevfsdirents, 1);
1274104521Srwatson#endif
1275104521Srwatson}
1276104521Srwatson
1277104521Srwatsonvoid
1278104527Srwatsonmac_destroy_ifnet(struct ifnet *ifp)
1279104521Srwatson{
1280104521Srwatson
1281104527Srwatson	MAC_PERFORM(destroy_ifnet_label, &ifp->if_label);
1282104527Srwatson	mac_destroy_label(&ifp->if_label);
1283104521Srwatson#ifdef MAC_DEBUG
1284104527Srwatson	atomic_subtract_int(&nmacifnets, 1);
1285104521Srwatson#endif
1286104521Srwatson}
1287104521Srwatson
1288104521Srwatsonvoid
1289104527Srwatsonmac_destroy_ipq(struct ipq *ipq)
1290104521Srwatson{
1291104521Srwatson
1292104527Srwatson	MAC_PERFORM(destroy_ipq_label, &ipq->ipq_label);
1293104527Srwatson	mac_destroy_label(&ipq->ipq_label);
1294104521Srwatson#ifdef MAC_DEBUG
1295104527Srwatson	atomic_subtract_int(&nmacipqs, 1);
1296104521Srwatson#endif
1297104521Srwatson}
1298104521Srwatson
1299104527Srwatsonvoid
1300104527Srwatsonmac_destroy_mbuf(struct mbuf *m)
1301104521Srwatson{
1302104521Srwatson
1303104527Srwatson	MAC_PERFORM(destroy_mbuf_label, &m->m_pkthdr.label);
1304104527Srwatson	mac_destroy_label(&m->m_pkthdr.label);
1305104521Srwatson#ifdef MAC_DEBUG
1306104527Srwatson	atomic_subtract_int(&nmacmbufs, 1);
1307104521Srwatson#endif
1308104521Srwatson}
1309104521Srwatson
1310104527Srwatsonvoid
1311104527Srwatsonmac_destroy_mount(struct mount *mp)
1312104521Srwatson{
1313104521Srwatson
1314104527Srwatson	MAC_PERFORM(destroy_mount_label, &mp->mnt_mntlabel);
1315104527Srwatson	MAC_PERFORM(destroy_mount_fs_label, &mp->mnt_fslabel);
1316104527Srwatson	mac_destroy_label(&mp->mnt_fslabel);
1317104527Srwatson	mac_destroy_label(&mp->mnt_mntlabel);
1318104521Srwatson#ifdef MAC_DEBUG
1319104527Srwatson	atomic_subtract_int(&nmacmounts, 1);
1320104521Srwatson#endif
1321104521Srwatson}
1322104521Srwatson
1323104521Srwatsonvoid
1324104527Srwatsonmac_destroy_pipe(struct pipe *pipe)
1325104521Srwatson{
1326104521Srwatson
1327104527Srwatson	MAC_PERFORM(destroy_pipe_label, pipe->pipe_label);
1328104527Srwatson	mac_destroy_label(pipe->pipe_label);
1329104527Srwatson	free(pipe->pipe_label, M_MACPIPELABEL);
1330104521Srwatson#ifdef MAC_DEBUG
1331104527Srwatson	atomic_subtract_int(&nmacpipes, 1);
1332104521Srwatson#endif
1333104521Srwatson}
1334104521Srwatson
1335104541Srwatsonstatic void
1336104541Srwatsonmac_destroy_socket_label(struct label *label)
1337104521Srwatson{
1338104521Srwatson
1339104541Srwatson	MAC_PERFORM(destroy_socket_label, label);
1340104541Srwatson	mac_destroy_label(label);
1341104521Srwatson#ifdef MAC_DEBUG
1342104527Srwatson	atomic_subtract_int(&nmacsockets, 1);
1343104521Srwatson#endif
1344104521Srwatson}
1345104521Srwatson
1346104527Srwatsonstatic void
1347104541Srwatsonmac_destroy_socket_peer_label(struct label *label)
1348104541Srwatson{
1349104541Srwatson
1350104541Srwatson	MAC_PERFORM(destroy_socket_peer_label, label);
1351104541Srwatson	mac_destroy_label(label);
1352104541Srwatson}
1353104541Srwatson
1354104541Srwatsonvoid
1355104541Srwatsonmac_destroy_socket(struct socket *socket)
1356104541Srwatson{
1357104541Srwatson
1358104541Srwatson	mac_destroy_socket_label(&socket->so_label);
1359104541Srwatson	mac_destroy_socket_peer_label(&socket->so_peerlabel);
1360104541Srwatson}
1361104541Srwatson
1362104541Srwatsonstatic void
1363104527Srwatsonmac_destroy_temp(struct label *label)
1364104521Srwatson{
1365104521Srwatson
1366104527Srwatson	MAC_PERFORM(destroy_temp_label, label);
1367104527Srwatson	mac_destroy_label(label);
1368104521Srwatson#ifdef MAC_DEBUG
1369104527Srwatson	atomic_subtract_int(&nmactemp, 1);
1370104521Srwatson#endif
1371104521Srwatson}
1372104521Srwatson
1373104521Srwatsonvoid
1374104527Srwatsonmac_destroy_vnode(struct vnode *vp)
1375104521Srwatson{
1376104521Srwatson
1377104527Srwatson	MAC_PERFORM(destroy_vnode_label, &vp->v_label);
1378104527Srwatson	mac_destroy_label(&vp->v_label);
1379104521Srwatson#ifdef MAC_DEBUG
1380104527Srwatson	atomic_subtract_int(&nmacvnodes, 1);
1381104521Srwatson#endif
1382104521Srwatson}
1383104521Srwatson
1384104522Srwatsonstatic int
1385104522Srwatsonmac_externalize(struct label *label, struct mac *mac)
1386104522Srwatson{
1387104522Srwatson	int error;
1388104522Srwatson
1389104522Srwatson	mac_init_structmac(mac);
1390104522Srwatson	MAC_CHECK(externalize, label, mac);
1391104522Srwatson
1392104522Srwatson	return (error);
1393104522Srwatson}
1394104522Srwatson
1395104522Srwatsonstatic int
1396104522Srwatsonmac_internalize(struct label *label, struct mac *mac)
1397104522Srwatson{
1398104522Srwatson	int error;
1399104522Srwatson
1400104522Srwatson	mac_init_temp(label);
1401104522Srwatson	MAC_CHECK(internalize, label, mac);
1402104522Srwatson	if (error)
1403104522Srwatson		mac_destroy_temp(label);
1404104522Srwatson
1405104522Srwatson	return (error);
1406104522Srwatson}
1407104522Srwatson
1408104522Srwatson/*
1409104522Srwatson * Initialize MAC label for the first kernel process, from which other
1410104522Srwatson * kernel processes and threads are spawned.
1411104522Srwatson */
1412104521Srwatsonvoid
1413104522Srwatsonmac_create_proc0(struct ucred *cred)
1414104522Srwatson{
1415104522Srwatson
1416104522Srwatson	MAC_PERFORM(create_proc0, cred);
1417104522Srwatson}
1418104522Srwatson
1419104522Srwatson/*
1420104522Srwatson * Initialize MAC label for the first userland process, from which other
1421104522Srwatson * userland processes and threads are spawned.
1422104522Srwatson */
1423104522Srwatsonvoid
1424104522Srwatsonmac_create_proc1(struct ucred *cred)
1425104522Srwatson{
1426104522Srwatson
1427104522Srwatson	MAC_PERFORM(create_proc1, cred);
1428104522Srwatson}
1429104522Srwatson
1430104522Srwatsonvoid
1431104522Srwatsonmac_thread_userret(struct thread *td)
1432104522Srwatson{
1433104522Srwatson
1434104522Srwatson	MAC_PERFORM(thread_userret, td);
1435104522Srwatson}
1436104522Srwatson
1437104522Srwatson/*
1438104522Srwatson * When a new process is created, its label must be initialized.  Generally,
1439104522Srwatson * this involves inheritence from the parent process, modulo possible
1440104522Srwatson * deltas.  This function allows that processing to take place.
1441104522Srwatson */
1442104522Srwatsonvoid
1443104522Srwatsonmac_create_cred(struct ucred *parent_cred, struct ucred *child_cred)
1444104522Srwatson{
1445104522Srwatson
1446104522Srwatson	MAC_PERFORM(create_cred, parent_cred, child_cred);
1447104522Srwatson}
1448104522Srwatson
1449104522Srwatsonvoid
1450100979Srwatsonmac_update_devfsdirent(struct devfs_dirent *de, struct vnode *vp)
1451100979Srwatson{
1452100979Srwatson
1453100979Srwatson	MAC_PERFORM(update_devfsdirent, de, &de->de_label, vp, &vp->v_label);
1454100979Srwatson}
1455100979Srwatson
1456100979Srwatsonvoid
1457100979Srwatsonmac_update_procfsvnode(struct vnode *vp, struct ucred *cred)
1458100979Srwatson{
1459100979Srwatson
1460100979Srwatson	MAC_PERFORM(update_procfsvnode, vp, &vp->v_label, cred);
1461100979Srwatson}
1462100979Srwatson
1463100979Srwatson/*
1464100979Srwatson * Support callout for policies that manage their own externalization
1465100979Srwatson * using extended attributes.
1466100979Srwatson */
1467100979Srwatsonstatic int
1468100979Srwatsonmac_update_vnode_from_extattr(struct vnode *vp, struct mount *mp)
1469100979Srwatson{
1470100979Srwatson	int error;
1471100979Srwatson
1472100979Srwatson	MAC_CHECK(update_vnode_from_extattr, vp, &vp->v_label, mp,
1473100979Srwatson	    &mp->mnt_fslabel);
1474100979Srwatson
1475100979Srwatson	return (error);
1476100979Srwatson}
1477100979Srwatson
1478100979Srwatson/*
1479100979Srwatson * Given an externalized mac label, internalize it and stamp it on a
1480100979Srwatson * vnode.
1481100979Srwatson */
1482100979Srwatsonstatic int
1483100979Srwatsonmac_update_vnode_from_externalized(struct vnode *vp, struct mac *extmac)
1484100979Srwatson{
1485100979Srwatson	int error;
1486100979Srwatson
1487100979Srwatson	MAC_CHECK(update_vnode_from_externalized, vp, &vp->v_label, extmac);
1488100979Srwatson
1489100979Srwatson	return (error);
1490100979Srwatson}
1491100979Srwatson
1492100979Srwatson/*
1493100979Srwatson * Call out to individual policies to update the label in a vnode from
1494100979Srwatson * the mountpoint.
1495100979Srwatson */
1496100979Srwatsonvoid
1497100979Srwatsonmac_update_vnode_from_mount(struct vnode *vp, struct mount *mp)
1498100979Srwatson{
1499100979Srwatson
1500100979Srwatson	MAC_PERFORM(update_vnode_from_mount, vp, &vp->v_label, mp,
1501100979Srwatson	    &mp->mnt_fslabel);
1502100979Srwatson
1503101308Sjeff	ASSERT_VOP_LOCKED(vp, "mac_update_vnode_from_mount");
1504100979Srwatson	if (mac_cache_fslabel_in_vnode)
1505101308Sjeff		vp->v_vflag |= VV_CACHEDLABEL;
1506100979Srwatson}
1507100979Srwatson
1508100979Srwatson/*
1509100979Srwatson * Implementation of VOP_REFRESHLABEL() that relies on extended attributes
1510100979Srwatson * to store label data.  Can be referenced by filesystems supporting
1511100979Srwatson * extended attributes.
1512100979Srwatson */
1513100979Srwatsonint
1514100979Srwatsonvop_stdrefreshlabel_ea(struct vop_refreshlabel_args *ap)
1515100979Srwatson{
1516100979Srwatson	struct vnode *vp = ap->a_vp;
1517100979Srwatson	struct mac extmac;
1518100979Srwatson	int buflen, error;
1519100979Srwatson
1520100979Srwatson	ASSERT_VOP_LOCKED(vp, "vop_stdrefreshlabel_ea");
1521100979Srwatson
1522100979Srwatson	/*
1523100979Srwatson	 * Call out to external policies first.  Order doesn't really
1524100979Srwatson	 * matter, as long as failure of one assures failure of all.
1525100979Srwatson	 */
1526100979Srwatson	error = mac_update_vnode_from_extattr(vp, vp->v_mount);
1527100979Srwatson	if (error)
1528100979Srwatson		return (error);
1529100979Srwatson
1530100979Srwatson	buflen = sizeof(extmac);
1531100979Srwatson	error = vn_extattr_get(vp, IO_NODELOCKED,
1532100979Srwatson	    FREEBSD_MAC_EXTATTR_NAMESPACE, FREEBSD_MAC_EXTATTR_NAME, &buflen,
1533100979Srwatson	    (char *)&extmac, curthread);
1534100979Srwatson	switch (error) {
1535100979Srwatson	case 0:
1536100979Srwatson		/* Got it */
1537100979Srwatson		break;
1538100979Srwatson
1539100979Srwatson	case ENOATTR:
1540100979Srwatson		/*
1541100979Srwatson		 * Use the label from the mount point.
1542100979Srwatson		 */
1543100979Srwatson		mac_update_vnode_from_mount(vp, vp->v_mount);
1544100979Srwatson		return (0);
1545100979Srwatson
1546100979Srwatson	case EOPNOTSUPP:
1547100979Srwatson	default:
1548100979Srwatson		/* Fail horribly. */
1549100979Srwatson		return (error);
1550100979Srwatson	}
1551100979Srwatson
1552100979Srwatson	if (buflen != sizeof(extmac))
1553100979Srwatson		error = EPERM;		/* Fail very closed. */
1554100979Srwatson	if (error == 0)
1555100979Srwatson		error = mac_update_vnode_from_externalized(vp, &extmac);
1556100979Srwatson	if (error == 0)
1557101308Sjeff		vp->v_vflag |= VV_CACHEDLABEL;
1558100979Srwatson	else {
1559100979Srwatson		struct vattr va;
1560100979Srwatson
1561100979Srwatson		printf("Corrupted label on %s",
1562100979Srwatson		    vp->v_mount->mnt_stat.f_mntonname);
1563100979Srwatson		if (VOP_GETATTR(vp, &va, curthread->td_ucred, curthread) == 0)
1564100979Srwatson			printf(" inum %ld", va.va_fileid);
1565104268Srwatson#ifdef MAC_DEBUG
1566100979Srwatson		if (mac_debug_label_fallback) {
1567100979Srwatson			printf(", falling back.\n");
1568100979Srwatson			mac_update_vnode_from_mount(vp, vp->v_mount);
1569100979Srwatson			error = 0;
1570100979Srwatson		} else {
1571104268Srwatson#endif
1572100979Srwatson			printf(".\n");
1573100979Srwatson			error = EPERM;
1574104268Srwatson#ifdef MAC_DEBUG
1575100979Srwatson		}
1576104268Srwatson#endif
1577100979Srwatson	}
1578100979Srwatson
1579100979Srwatson	return (error);
1580100979Srwatson}
1581100979Srwatson
1582100979Srwatson/*
1583100979Srwatson * Make sure the vnode label is up-to-date.  If EOPNOTSUPP, then we handle
1584100979Srwatson * the labeling activity outselves.  Filesystems should be careful not
1585100979Srwatson * to change their minds regarding whether they support vop_refreshlabel()
1586100979Srwatson * for a vnode or not.  Don't cache the vnode here, allow the file
1587100979Srwatson * system code to determine if it's safe to cache.  If we update from
1588100979Srwatson * the mount, don't cache since a change to the mount label should affect
1589100979Srwatson * all vnodes.
1590100979Srwatson */
1591100979Srwatsonstatic int
1592100979Srwatsonvn_refreshlabel(struct vnode *vp, struct ucred *cred)
1593100979Srwatson{
1594100979Srwatson	int error;
1595100979Srwatson
1596100979Srwatson	ASSERT_VOP_LOCKED(vp, "vn_refreshlabel");
1597100979Srwatson
1598100979Srwatson	if (vp->v_mount == NULL) {
1599100979Srwatson/*
1600100979Srwatson		Eventually, we probably want to special-case refreshing
1601100979Srwatson		of deadfs vnodes, and if there's a lock-free race somewhere,
1602100979Srwatson		that case might be handled here.
1603100979Srwatson
1604100979Srwatson		mac_update_vnode_deadfs(vp);
1605100979Srwatson		return (0);
1606100979Srwatson */
1607100979Srwatson		/* printf("vn_refreshlabel: null v_mount\n"); */
1608103314Snjl		if (vp->v_type != VNON)
1609100979Srwatson			printf(
1610103314Snjl			    "vn_refreshlabel: null v_mount with non-VNON\n");
1611100979Srwatson		return (EBADF);
1612100979Srwatson	}
1613100979Srwatson
1614101308Sjeff	if (vp->v_vflag & VV_CACHEDLABEL) {
1615100979Srwatson		mac_vnode_label_cache_hits++;
1616100979Srwatson		return (0);
1617100979Srwatson	} else
1618100979Srwatson		mac_vnode_label_cache_misses++;
1619100979Srwatson
1620100979Srwatson	if ((vp->v_mount->mnt_flag & MNT_MULTILABEL) == 0) {
1621100979Srwatson		mac_update_vnode_from_mount(vp, vp->v_mount);
1622100979Srwatson		return (0);
1623100979Srwatson	}
1624100979Srwatson
1625100979Srwatson	error = VOP_REFRESHLABEL(vp, cred, curthread);
1626100979Srwatson	switch (error) {
1627100979Srwatson	case EOPNOTSUPP:
1628100979Srwatson		/*
1629100979Srwatson		 * If labels are not supported on this vnode, fall back to
1630100979Srwatson		 * the label in the mount and propagate it to the vnode.
1631100979Srwatson		 * There should probably be some sort of policy/flag/decision
1632100979Srwatson		 * about doing this.
1633100979Srwatson		 */
1634100979Srwatson		mac_update_vnode_from_mount(vp, vp->v_mount);
1635100979Srwatson		error = 0;
1636100979Srwatson	default:
1637100979Srwatson		return (error);
1638100979Srwatson	}
1639100979Srwatson}
1640100979Srwatson
1641100979Srwatson/*
1642100979Srwatson * Helper function for file systems using the vop_std*_ea() calls.  This
1643100979Srwatson * function must be called after EA service is available for the vnode,
1644100979Srwatson * but before it's hooked up to the namespace so that the node persists
1645100979Srwatson * if there's a crash, or before it can be accessed.  On successful
1646100979Srwatson * commit of the label to disk (etc), do cache the label.
1647100979Srwatson */
1648100979Srwatsonint
1649100979Srwatsonvop_stdcreatevnode_ea(struct vnode *dvp, struct vnode *tvp, struct ucred *cred)
1650100979Srwatson{
1651100979Srwatson	struct mac extmac;
1652100979Srwatson	int error;
1653100979Srwatson
1654101308Sjeff	ASSERT_VOP_LOCKED(tvp, "vop_stdcreatevnode_ea");
1655100979Srwatson	if ((dvp->v_mount->mnt_flag & MNT_MULTILABEL) == 0) {
1656100979Srwatson		mac_update_vnode_from_mount(tvp, tvp->v_mount);
1657100979Srwatson	} else {
1658100979Srwatson		error = vn_refreshlabel(dvp, cred);
1659100979Srwatson		if (error)
1660100979Srwatson			return (error);
1661100979Srwatson
1662100979Srwatson		/*
1663100979Srwatson		 * Stick the label in the vnode.  Then try to write to
1664100979Srwatson		 * disk.  If we fail, return a failure to abort the
1665100979Srwatson		 * create operation.  Really, this failure shouldn't
1666100979Srwatson		 * happen except in fairly unusual circumstances (out
1667100979Srwatson		 * of disk, etc).
1668100979Srwatson		 */
1669100979Srwatson		mac_create_vnode(cred, dvp, tvp);
1670100979Srwatson
1671100979Srwatson		error = mac_stdcreatevnode_ea(tvp);
1672100979Srwatson		if (error)
1673100979Srwatson			return (error);
1674100979Srwatson
1675100979Srwatson		/*
1676100979Srwatson		 * XXX: Eventually this will go away and all policies will
1677100979Srwatson		 * directly manage their extended attributes.
1678100979Srwatson		 */
1679100979Srwatson		error = mac_externalize(&tvp->v_label, &extmac);
1680100979Srwatson		if (error)
1681100979Srwatson			return (error);
1682100979Srwatson
1683100979Srwatson		error = vn_extattr_set(tvp, IO_NODELOCKED,
1684100979Srwatson		    FREEBSD_MAC_EXTATTR_NAMESPACE, FREEBSD_MAC_EXTATTR_NAME,
1685100979Srwatson		    sizeof(extmac), (char *)&extmac, curthread);
1686100979Srwatson		if (error == 0)
1687101308Sjeff			tvp->v_vflag |= VV_CACHEDLABEL;
1688100979Srwatson		else {
1689100979Srwatson#if 0
1690100979Srwatson			/*
1691100979Srwatson			 * In theory, we could have fall-back behavior here.
1692100979Srwatson			 * It would probably be incorrect.
1693100979Srwatson			 */
1694100979Srwatson#endif
1695100979Srwatson			return (error);
1696100979Srwatson		}
1697100979Srwatson	}
1698100979Srwatson
1699100979Srwatson	return (0);
1700100979Srwatson}
1701100979Srwatson
1702100979Srwatsonvoid
1703100979Srwatsonmac_execve_transition(struct ucred *old, struct ucred *new, struct vnode *vp)
1704100979Srwatson{
1705100979Srwatson	int error;
1706100979Srwatson
1707100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_execve_transition");
1708100979Srwatson
1709100979Srwatson	error = vn_refreshlabel(vp, old);
1710100979Srwatson	if (error) {
1711100979Srwatson		printf("mac_execve_transition: vn_refreshlabel returned %d\n",
1712100979Srwatson		    error);
1713100979Srwatson		printf("mac_execve_transition: using old vnode label\n");
1714100979Srwatson	}
1715100979Srwatson
1716100979Srwatson	MAC_PERFORM(execve_transition, old, new, vp, &vp->v_label);
1717100979Srwatson}
1718100979Srwatson
1719100979Srwatsonint
1720100979Srwatsonmac_execve_will_transition(struct ucred *old, struct vnode *vp)
1721100979Srwatson{
1722100979Srwatson	int error, result;
1723100979Srwatson
1724100979Srwatson	error = vn_refreshlabel(vp, old);
1725100979Srwatson	if (error)
1726100979Srwatson		return (error);
1727100979Srwatson
1728100979Srwatson	result = 0;
1729100979Srwatson	MAC_BOOLEAN(execve_will_transition, ||, old, vp, &vp->v_label);
1730100979Srwatson
1731100979Srwatson	return (result);
1732100979Srwatson}
1733100979Srwatson
1734100979Srwatsonint
1735100979Srwatsonmac_check_vnode_access(struct ucred *cred, struct vnode *vp, int flags)
1736100979Srwatson{
1737100979Srwatson	int error;
1738100979Srwatson
1739100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_access");
1740100979Srwatson
1741100979Srwatson	if (!mac_enforce_fs)
1742100979Srwatson		return (0);
1743100979Srwatson
1744100979Srwatson	error = vn_refreshlabel(vp, cred);
1745100979Srwatson	if (error)
1746100979Srwatson		return (error);
1747100979Srwatson
1748100979Srwatson	MAC_CHECK(check_vnode_access, cred, vp, &vp->v_label, flags);
1749100979Srwatson	return (error);
1750100979Srwatson}
1751100979Srwatson
1752100979Srwatsonint
1753100979Srwatsonmac_check_vnode_chdir(struct ucred *cred, struct vnode *dvp)
1754100979Srwatson{
1755100979Srwatson	int error;
1756100979Srwatson
1757100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_chdir");
1758100979Srwatson
1759100979Srwatson	if (!mac_enforce_fs)
1760100979Srwatson		return (0);
1761100979Srwatson
1762100979Srwatson	error = vn_refreshlabel(dvp, cred);
1763100979Srwatson	if (error)
1764100979Srwatson		return (error);
1765100979Srwatson
1766100979Srwatson	MAC_CHECK(check_vnode_chdir, cred, dvp, &dvp->v_label);
1767100979Srwatson	return (error);
1768100979Srwatson}
1769100979Srwatson
1770100979Srwatsonint
1771100979Srwatsonmac_check_vnode_chroot(struct ucred *cred, struct vnode *dvp)
1772100979Srwatson{
1773100979Srwatson	int error;
1774100979Srwatson
1775100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_chroot");
1776100979Srwatson
1777100979Srwatson	if (!mac_enforce_fs)
1778100979Srwatson		return (0);
1779100979Srwatson
1780100979Srwatson	error = vn_refreshlabel(dvp, cred);
1781100979Srwatson	if (error)
1782100979Srwatson		return (error);
1783100979Srwatson
1784100979Srwatson	MAC_CHECK(check_vnode_chroot, cred, dvp, &dvp->v_label);
1785100979Srwatson	return (error);
1786100979Srwatson}
1787100979Srwatson
1788100979Srwatsonint
1789100979Srwatsonmac_check_vnode_create(struct ucred *cred, struct vnode *dvp,
1790100979Srwatson    struct componentname *cnp, struct vattr *vap)
1791100979Srwatson{
1792100979Srwatson	int error;
1793100979Srwatson
1794100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_create");
1795100979Srwatson
1796100979Srwatson	if (!mac_enforce_fs)
1797100979Srwatson		return (0);
1798100979Srwatson
1799100979Srwatson	error = vn_refreshlabel(dvp, cred);
1800100979Srwatson	if (error)
1801100979Srwatson		return (error);
1802100979Srwatson
1803100979Srwatson	MAC_CHECK(check_vnode_create, cred, dvp, &dvp->v_label, cnp, vap);
1804100979Srwatson	return (error);
1805100979Srwatson}
1806100979Srwatson
1807100979Srwatsonint
1808100979Srwatsonmac_check_vnode_delete(struct ucred *cred, struct vnode *dvp, struct vnode *vp,
1809100979Srwatson    struct componentname *cnp)
1810100979Srwatson{
1811100979Srwatson	int error;
1812100979Srwatson
1813100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_delete");
1814100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_delete");
1815100979Srwatson
1816100979Srwatson	if (!mac_enforce_fs)
1817100979Srwatson		return (0);
1818100979Srwatson
1819100979Srwatson	error = vn_refreshlabel(dvp, cred);
1820100979Srwatson	if (error)
1821100979Srwatson		return (error);
1822100979Srwatson	error = vn_refreshlabel(vp, cred);
1823100979Srwatson	if (error)
1824100979Srwatson		return (error);
1825100979Srwatson
1826100979Srwatson	MAC_CHECK(check_vnode_delete, cred, dvp, &dvp->v_label, vp,
1827100979Srwatson	    &vp->v_label, cnp);
1828100979Srwatson	return (error);
1829100979Srwatson}
1830100979Srwatson
1831100979Srwatsonint
1832100979Srwatsonmac_check_vnode_deleteacl(struct ucred *cred, struct vnode *vp,
1833100979Srwatson    acl_type_t type)
1834100979Srwatson{
1835100979Srwatson	int error;
1836100979Srwatson
1837100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_deleteacl");
1838100979Srwatson
1839100979Srwatson	if (!mac_enforce_fs)
1840100979Srwatson		return (0);
1841100979Srwatson
1842100979Srwatson	error = vn_refreshlabel(vp, cred);
1843100979Srwatson	if (error)
1844100979Srwatson		return (error);
1845100979Srwatson
1846100979Srwatson	MAC_CHECK(check_vnode_deleteacl, cred, vp, &vp->v_label, type);
1847100979Srwatson	return (error);
1848100979Srwatson}
1849100979Srwatson
1850100979Srwatsonint
1851100979Srwatsonmac_check_vnode_exec(struct ucred *cred, struct vnode *vp)
1852100979Srwatson{
1853100979Srwatson	int error;
1854100979Srwatson
1855102102Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_exec");
1856102102Srwatson
1857100979Srwatson	if (!mac_enforce_process && !mac_enforce_fs)
1858100979Srwatson		return (0);
1859100979Srwatson
1860100979Srwatson	error = vn_refreshlabel(vp, cred);
1861100979Srwatson	if (error)
1862100979Srwatson		return (error);
1863100979Srwatson	MAC_CHECK(check_vnode_exec, cred, vp, &vp->v_label);
1864100979Srwatson
1865100979Srwatson	return (error);
1866100979Srwatson}
1867100979Srwatson
1868100979Srwatsonint
1869100979Srwatsonmac_check_vnode_getacl(struct ucred *cred, struct vnode *vp, acl_type_t type)
1870100979Srwatson{
1871100979Srwatson	int error;
1872100979Srwatson
1873100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_getacl");
1874100979Srwatson
1875100979Srwatson	if (!mac_enforce_fs)
1876100979Srwatson		return (0);
1877100979Srwatson
1878100979Srwatson	error = vn_refreshlabel(vp, cred);
1879100979Srwatson	if (error)
1880100979Srwatson		return (error);
1881100979Srwatson
1882100979Srwatson	MAC_CHECK(check_vnode_getacl, cred, vp, &vp->v_label, type);
1883100979Srwatson	return (error);
1884100979Srwatson}
1885100979Srwatson
1886100979Srwatsonint
1887100979Srwatsonmac_check_vnode_getextattr(struct ucred *cred, struct vnode *vp,
1888100979Srwatson    int attrnamespace, const char *name, struct uio *uio)
1889100979Srwatson{
1890100979Srwatson	int error;
1891100979Srwatson
1892100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_getextattr");
1893100979Srwatson
1894100979Srwatson	if (!mac_enforce_fs)
1895100979Srwatson		return (0);
1896100979Srwatson
1897100979Srwatson	error = vn_refreshlabel(vp, cred);
1898100979Srwatson	if (error)
1899100979Srwatson		return (error);
1900100979Srwatson
1901100979Srwatson	MAC_CHECK(check_vnode_getextattr, cred, vp, &vp->v_label,
1902100979Srwatson	    attrnamespace, name, uio);
1903100979Srwatson	return (error);
1904100979Srwatson}
1905100979Srwatson
1906100979Srwatsonint
1907104529Srwatsonmac_check_vnode_link(struct ucred *cred, struct vnode *dvp,
1908104529Srwatson    struct vnode *vp, struct componentname *cnp)
1909104529Srwatson{
1910104529Srwatson
1911104529Srwatson	int error;
1912104529Srwatson
1913104529Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_link");
1914104529Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_link");
1915104529Srwatson
1916104529Srwatson	if (!mac_enforce_fs)
1917104529Srwatson		return (0);
1918104529Srwatson
1919104529Srwatson	error = vn_refreshlabel(dvp, cred);
1920104529Srwatson	if (error)
1921104529Srwatson		return (error);
1922104529Srwatson
1923104529Srwatson	error = vn_refreshlabel(vp, cred);
1924104529Srwatson	if (error)
1925104529Srwatson		return (error);
1926104529Srwatson
1927104529Srwatson	MAC_CHECK(check_vnode_link, cred, dvp, &dvp->v_label, vp,
1928104529Srwatson	    &vp->v_label, cnp);
1929104529Srwatson	return (error);
1930104529Srwatson}
1931104529Srwatson
1932104529Srwatsonint
1933100979Srwatsonmac_check_vnode_lookup(struct ucred *cred, struct vnode *dvp,
1934100979Srwatson    struct componentname *cnp)
1935100979Srwatson{
1936100979Srwatson	int error;
1937100979Srwatson
1938100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_lookup");
1939100979Srwatson
1940100979Srwatson	if (!mac_enforce_fs)
1941100979Srwatson		return (0);
1942100979Srwatson
1943100979Srwatson	error = vn_refreshlabel(dvp, cred);
1944100979Srwatson	if (error)
1945100979Srwatson		return (error);
1946100979Srwatson
1947100979Srwatson	MAC_CHECK(check_vnode_lookup, cred, dvp, &dvp->v_label, cnp);
1948100979Srwatson	return (error);
1949100979Srwatson}
1950100979Srwatson
1951104546Srwatsonint
1952104546Srwatsonmac_check_vnode_mmap(struct ucred *cred, struct vnode *vp, int prot)
1953100979Srwatson{
1954104546Srwatson	int error;
1955100979Srwatson
1956104546Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mmap");
1957103514Srwatson
1958104546Srwatson	if (!mac_enforce_fs || !mac_enforce_vm)
1959104546Srwatson		return (0);
1960104546Srwatson
1961104546Srwatson	error = vn_refreshlabel(vp, cred);
1962104546Srwatson	if (error)
1963104546Srwatson		return (error);
1964104546Srwatson
1965104546Srwatson	MAC_CHECK(check_vnode_mmap, cred, vp, &vp->v_label, prot);
1966104546Srwatson	return (error);
1967100979Srwatson}
1968100979Srwatson
1969104546Srwatsonvoid
1970104546Srwatsonmac_check_vnode_mmap_downgrade(struct ucred *cred, struct vnode *vp, int *prot)
1971104546Srwatson{
1972104546Srwatson	int result = *prot;
1973104546Srwatson
1974104546Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mmap_downgrade");
1975104546Srwatson
1976104546Srwatson	if (!mac_enforce_fs || !mac_enforce_vm)
1977104546Srwatson		return;
1978104546Srwatson
1979104546Srwatson	MAC_PERFORM(check_vnode_mmap_downgrade, cred, vp, &vp->v_label,
1980104546Srwatson	    &result);
1981104546Srwatson
1982104546Srwatson	*prot = result;
1983104546Srwatson}
1984104546Srwatson
1985100979Srwatsonint
1986104546Srwatsonmac_check_vnode_mprotect(struct ucred *cred, struct vnode *vp, int prot)
1987104546Srwatson{
1988104546Srwatson	int error;
1989104546Srwatson
1990104546Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mprotect");
1991104546Srwatson
1992104546Srwatson	if (!mac_enforce_fs || !mac_enforce_vm)
1993104546Srwatson		return (0);
1994104546Srwatson
1995104546Srwatson	error = vn_refreshlabel(vp, cred);
1996104546Srwatson	if (error)
1997104546Srwatson		return (error);
1998104546Srwatson
1999104546Srwatson	MAC_CHECK(check_vnode_mprotect, cred, vp, &vp->v_label, prot);
2000104546Srwatson	return (error);
2001104546Srwatson}
2002104546Srwatson
2003104546Srwatsonint
2004102112Srwatsonmac_check_vnode_open(struct ucred *cred, struct vnode *vp, mode_t acc_mode)
2005100979Srwatson{
2006100979Srwatson	int error;
2007100979Srwatson
2008102112Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_open");
2009102112Srwatson
2010100979Srwatson	if (!mac_enforce_fs)
2011100979Srwatson		return (0);
2012100979Srwatson
2013102112Srwatson	error = vn_refreshlabel(vp, cred);
2014102112Srwatson	if (error)
2015102112Srwatson		return (error);
2016100979Srwatson
2017102112Srwatson	MAC_CHECK(check_vnode_open, cred, vp, &vp->v_label, acc_mode);
2018102112Srwatson	return (error);
2019102112Srwatson}
2020102112Srwatson
2021102112Srwatsonint
2022102129Srwatsonmac_check_vnode_poll(struct ucred *active_cred, struct ucred *file_cred,
2023102129Srwatson    struct vnode *vp)
2024102112Srwatson{
2025102112Srwatson	int error;
2026102112Srwatson
2027102112Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_poll");
2028102112Srwatson
2029102112Srwatson	if (!mac_enforce_fs)
2030102112Srwatson		return (0);
2031102112Srwatson
2032102129Srwatson	error = vn_refreshlabel(vp, active_cred);
2033100979Srwatson	if (error)
2034100979Srwatson		return (error);
2035100979Srwatson
2036102129Srwatson	MAC_CHECK(check_vnode_poll, active_cred, file_cred, vp,
2037102129Srwatson	    &vp->v_label);
2038100979Srwatson
2039100979Srwatson	return (error);
2040100979Srwatson}
2041100979Srwatson
2042100979Srwatsonint
2043102129Srwatsonmac_check_vnode_read(struct ucred *active_cred, struct ucred *file_cred,
2044102129Srwatson    struct vnode *vp)
2045100979Srwatson{
2046100979Srwatson	int error;
2047100979Srwatson
2048102112Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_read");
2049100979Srwatson
2050100979Srwatson	if (!mac_enforce_fs)
2051100979Srwatson		return (0);
2052100979Srwatson
2053102129Srwatson	error = vn_refreshlabel(vp, active_cred);
2054100979Srwatson	if (error)
2055100979Srwatson		return (error);
2056100979Srwatson
2057102129Srwatson	MAC_CHECK(check_vnode_read, active_cred, file_cred, vp,
2058102129Srwatson	    &vp->v_label);
2059102112Srwatson
2060100979Srwatson	return (error);
2061100979Srwatson}
2062100979Srwatson
2063100979Srwatsonint
2064100979Srwatsonmac_check_vnode_readdir(struct ucred *cred, struct vnode *dvp)
2065100979Srwatson{
2066100979Srwatson	int error;
2067100979Srwatson
2068100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_readdir");
2069100979Srwatson
2070100979Srwatson	if (!mac_enforce_fs)
2071100979Srwatson		return (0);
2072100979Srwatson
2073100979Srwatson	error = vn_refreshlabel(dvp, cred);
2074100979Srwatson	if (error)
2075100979Srwatson		return (error);
2076100979Srwatson
2077100979Srwatson	MAC_CHECK(check_vnode_readdir, cred, dvp, &dvp->v_label);
2078100979Srwatson	return (error);
2079100979Srwatson}
2080100979Srwatson
2081100979Srwatsonint
2082100979Srwatsonmac_check_vnode_readlink(struct ucred *cred, struct vnode *vp)
2083100979Srwatson{
2084100979Srwatson	int error;
2085100979Srwatson
2086100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_readlink");
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_readlink, cred, vp, &vp->v_label);
2096100979Srwatson	return (error);
2097100979Srwatson}
2098100979Srwatson
2099100979Srwatsonstatic int
2100100979Srwatsonmac_check_vnode_relabel(struct ucred *cred, struct vnode *vp,
2101100979Srwatson    struct label *newlabel)
2102100979Srwatson{
2103100979Srwatson	int error;
2104100979Srwatson
2105100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_relabel");
2106100979Srwatson
2107100979Srwatson	error = vn_refreshlabel(vp, cred);
2108100979Srwatson	if (error)
2109100979Srwatson		return (error);
2110100979Srwatson
2111100979Srwatson	MAC_CHECK(check_vnode_relabel, cred, vp, &vp->v_label, newlabel);
2112100979Srwatson
2113100979Srwatson	return (error);
2114100979Srwatson}
2115100979Srwatson
2116100979Srwatsonint
2117100979Srwatsonmac_check_vnode_rename_from(struct ucred *cred, struct vnode *dvp,
2118100979Srwatson    struct vnode *vp, struct componentname *cnp)
2119100979Srwatson{
2120100979Srwatson	int error;
2121100979Srwatson
2122100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_rename_from");
2123100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_rename_from");
2124100979Srwatson
2125100979Srwatson	if (!mac_enforce_fs)
2126100979Srwatson		return (0);
2127100979Srwatson
2128100979Srwatson	error = vn_refreshlabel(dvp, cred);
2129100979Srwatson	if (error)
2130100979Srwatson		return (error);
2131100979Srwatson	error = vn_refreshlabel(vp, cred);
2132100979Srwatson	if (error)
2133100979Srwatson		return (error);
2134100979Srwatson
2135100979Srwatson	MAC_CHECK(check_vnode_rename_from, cred, dvp, &dvp->v_label, vp,
2136100979Srwatson	    &vp->v_label, cnp);
2137100979Srwatson	return (error);
2138100979Srwatson}
2139100979Srwatson
2140100979Srwatsonint
2141100979Srwatsonmac_check_vnode_rename_to(struct ucred *cred, struct vnode *dvp,
2142100979Srwatson    struct vnode *vp, int samedir, struct componentname *cnp)
2143100979Srwatson{
2144100979Srwatson	int error;
2145100979Srwatson
2146100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_rename_to");
2147100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_rename_to");
2148100979Srwatson
2149100979Srwatson	if (!mac_enforce_fs)
2150100979Srwatson		return (0);
2151100979Srwatson
2152100979Srwatson	error = vn_refreshlabel(dvp, cred);
2153100979Srwatson	if (error)
2154100979Srwatson		return (error);
2155100979Srwatson	if (vp != NULL) {
2156100979Srwatson		error = vn_refreshlabel(vp, cred);
2157100979Srwatson		if (error)
2158100979Srwatson			return (error);
2159100979Srwatson	}
2160100979Srwatson	MAC_CHECK(check_vnode_rename_to, cred, dvp, &dvp->v_label, vp,
2161100979Srwatson	    vp != NULL ? &vp->v_label : NULL, samedir, cnp);
2162100979Srwatson	return (error);
2163100979Srwatson}
2164100979Srwatson
2165100979Srwatsonint
2166100979Srwatsonmac_check_vnode_revoke(struct ucred *cred, struct vnode *vp)
2167100979Srwatson{
2168100979Srwatson	int error;
2169100979Srwatson
2170100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_revoke");
2171100979Srwatson
2172100979Srwatson	if (!mac_enforce_fs)
2173100979Srwatson		return (0);
2174100979Srwatson
2175100979Srwatson	error = vn_refreshlabel(vp, cred);
2176100979Srwatson	if (error)
2177100979Srwatson		return (error);
2178100979Srwatson
2179100979Srwatson	MAC_CHECK(check_vnode_revoke, cred, vp, &vp->v_label);
2180100979Srwatson	return (error);
2181100979Srwatson}
2182100979Srwatson
2183100979Srwatsonint
2184100979Srwatsonmac_check_vnode_setacl(struct ucred *cred, struct vnode *vp, acl_type_t type,
2185100979Srwatson    struct acl *acl)
2186100979Srwatson{
2187100979Srwatson	int error;
2188100979Srwatson
2189100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setacl");
2190100979Srwatson
2191100979Srwatson	if (!mac_enforce_fs)
2192100979Srwatson		return (0);
2193100979Srwatson
2194100979Srwatson	error = vn_refreshlabel(vp, cred);
2195100979Srwatson	if (error)
2196100979Srwatson		return (error);
2197100979Srwatson
2198100979Srwatson	MAC_CHECK(check_vnode_setacl, cred, vp, &vp->v_label, type, acl);
2199100979Srwatson	return (error);
2200100979Srwatson}
2201100979Srwatson
2202100979Srwatsonint
2203100979Srwatsonmac_check_vnode_setextattr(struct ucred *cred, struct vnode *vp,
2204100979Srwatson    int attrnamespace, const char *name, struct uio *uio)
2205100979Srwatson{
2206100979Srwatson	int error;
2207100979Srwatson
2208100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setextattr");
2209100979Srwatson
2210100979Srwatson	if (!mac_enforce_fs)
2211100979Srwatson		return (0);
2212100979Srwatson
2213100979Srwatson	error = vn_refreshlabel(vp, cred);
2214100979Srwatson	if (error)
2215100979Srwatson		return (error);
2216100979Srwatson
2217100979Srwatson	MAC_CHECK(check_vnode_setextattr, cred, vp, &vp->v_label,
2218100979Srwatson	    attrnamespace, name, uio);
2219100979Srwatson	return (error);
2220100979Srwatson}
2221100979Srwatson
2222100979Srwatsonint
2223100979Srwatsonmac_check_vnode_setflags(struct ucred *cred, struct vnode *vp, u_long flags)
2224100979Srwatson{
2225100979Srwatson	int error;
2226100979Srwatson
2227100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setflags");
2228100979Srwatson
2229100979Srwatson	if (!mac_enforce_fs)
2230100979Srwatson		return (0);
2231100979Srwatson
2232100979Srwatson	error = vn_refreshlabel(vp, cred);
2233100979Srwatson	if (error)
2234100979Srwatson		return (error);
2235100979Srwatson
2236100979Srwatson	MAC_CHECK(check_vnode_setflags, cred, vp, &vp->v_label, flags);
2237100979Srwatson	return (error);
2238100979Srwatson}
2239100979Srwatson
2240100979Srwatsonint
2241100979Srwatsonmac_check_vnode_setmode(struct ucred *cred, struct vnode *vp, mode_t mode)
2242100979Srwatson{
2243100979Srwatson	int error;
2244100979Srwatson
2245100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setmode");
2246100979Srwatson
2247100979Srwatson	if (!mac_enforce_fs)
2248100979Srwatson		return (0);
2249100979Srwatson
2250100979Srwatson	error = vn_refreshlabel(vp, cred);
2251100979Srwatson	if (error)
2252100979Srwatson		return (error);
2253100979Srwatson
2254100979Srwatson	MAC_CHECK(check_vnode_setmode, cred, vp, &vp->v_label, mode);
2255100979Srwatson	return (error);
2256100979Srwatson}
2257100979Srwatson
2258100979Srwatsonint
2259100979Srwatsonmac_check_vnode_setowner(struct ucred *cred, struct vnode *vp, uid_t uid,
2260100979Srwatson    gid_t gid)
2261100979Srwatson{
2262100979Srwatson	int error;
2263100979Srwatson
2264100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setowner");
2265100979Srwatson
2266100979Srwatson	if (!mac_enforce_fs)
2267100979Srwatson		return (0);
2268100979Srwatson
2269100979Srwatson	error = vn_refreshlabel(vp, cred);
2270100979Srwatson	if (error)
2271100979Srwatson		return (error);
2272100979Srwatson
2273100979Srwatson	MAC_CHECK(check_vnode_setowner, cred, vp, &vp->v_label, uid, gid);
2274100979Srwatson	return (error);
2275100979Srwatson}
2276100979Srwatson
2277100979Srwatsonint
2278100979Srwatsonmac_check_vnode_setutimes(struct ucred *cred, struct vnode *vp,
2279100979Srwatson    struct timespec atime, struct timespec mtime)
2280100979Srwatson{
2281100979Srwatson	int error;
2282100979Srwatson
2283100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setutimes");
2284100979Srwatson
2285100979Srwatson	if (!mac_enforce_fs)
2286100979Srwatson		return (0);
2287100979Srwatson
2288100979Srwatson	error = vn_refreshlabel(vp, cred);
2289100979Srwatson	if (error)
2290100979Srwatson		return (error);
2291100979Srwatson
2292100979Srwatson	MAC_CHECK(check_vnode_setutimes, cred, vp, &vp->v_label, atime,
2293100979Srwatson	    mtime);
2294100979Srwatson	return (error);
2295100979Srwatson}
2296100979Srwatson
2297100979Srwatsonint
2298102129Srwatsonmac_check_vnode_stat(struct ucred *active_cred, struct ucred *file_cred,
2299102129Srwatson    struct vnode *vp)
2300100979Srwatson{
2301100979Srwatson	int error;
2302100979Srwatson
2303100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_stat");
2304100979Srwatson
2305100979Srwatson	if (!mac_enforce_fs)
2306100979Srwatson		return (0);
2307100979Srwatson
2308102129Srwatson	error = vn_refreshlabel(vp, active_cred);
2309100979Srwatson	if (error)
2310100979Srwatson		return (error);
2311100979Srwatson
2312102129Srwatson	MAC_CHECK(check_vnode_stat, active_cred, file_cred, vp,
2313102129Srwatson	    &vp->v_label);
2314100979Srwatson	return (error);
2315100979Srwatson}
2316100979Srwatson
2317102112Srwatsonint
2318102129Srwatsonmac_check_vnode_write(struct ucred *active_cred, struct ucred *file_cred,
2319102129Srwatson    struct vnode *vp)
2320102112Srwatson{
2321102112Srwatson	int error;
2322102112Srwatson
2323102112Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_write");
2324102112Srwatson
2325102112Srwatson	if (!mac_enforce_fs)
2326102112Srwatson		return (0);
2327102112Srwatson
2328102129Srwatson	error = vn_refreshlabel(vp, active_cred);
2329102112Srwatson	if (error)
2330102112Srwatson		return (error);
2331102112Srwatson
2332102129Srwatson	MAC_CHECK(check_vnode_write, active_cred, file_cred, vp,
2333102129Srwatson	    &vp->v_label);
2334102112Srwatson
2335102112Srwatson	return (error);
2336102112Srwatson}
2337102112Srwatson
2338100979Srwatson/*
2339100979Srwatson * When relabeling a process, call out to the policies for the maximum
2340100979Srwatson * permission allowed for each object type we know about in its
2341100979Srwatson * memory space, and revoke access (in the least surprising ways we
2342100979Srwatson * know) when necessary.  The process lock is not held here.
2343100979Srwatson */
2344100979Srwatsonstatic void
2345100979Srwatsonmac_cred_mmapped_drop_perms(struct thread *td, struct ucred *cred)
2346100979Srwatson{
2347100979Srwatson
2348100979Srwatson	/* XXX freeze all other threads */
2349100979Srwatson	mac_cred_mmapped_drop_perms_recurse(td, cred,
2350100979Srwatson	    &td->td_proc->p_vmspace->vm_map);
2351100979Srwatson	/* XXX allow other threads to continue */
2352100979Srwatson}
2353100979Srwatson
2354100979Srwatsonstatic __inline const char *
2355100979Srwatsonprot2str(vm_prot_t prot)
2356100979Srwatson{
2357100979Srwatson
2358100979Srwatson	switch (prot & VM_PROT_ALL) {
2359100979Srwatson	case VM_PROT_READ:
2360100979Srwatson		return ("r--");
2361100979Srwatson	case VM_PROT_READ | VM_PROT_WRITE:
2362100979Srwatson		return ("rw-");
2363100979Srwatson	case VM_PROT_READ | VM_PROT_EXECUTE:
2364100979Srwatson		return ("r-x");
2365100979Srwatson	case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
2366100979Srwatson		return ("rwx");
2367100979Srwatson	case VM_PROT_WRITE:
2368100979Srwatson		return ("-w-");
2369100979Srwatson	case VM_PROT_EXECUTE:
2370100979Srwatson		return ("--x");
2371100979Srwatson	case VM_PROT_WRITE | VM_PROT_EXECUTE:
2372100979Srwatson		return ("-wx");
2373100979Srwatson	default:
2374100979Srwatson		return ("---");
2375100979Srwatson	}
2376100979Srwatson}
2377100979Srwatson
2378100979Srwatsonstatic void
2379100979Srwatsonmac_cred_mmapped_drop_perms_recurse(struct thread *td, struct ucred *cred,
2380100979Srwatson    struct vm_map *map)
2381100979Srwatson{
2382100979Srwatson	struct vm_map_entry *vme;
2383104546Srwatson	int result;
2384104546Srwatson	vm_prot_t revokeperms;
2385100979Srwatson	vm_object_t object;
2386100979Srwatson	vm_ooffset_t offset;
2387100979Srwatson	struct vnode *vp;
2388100979Srwatson
2389103136Srwatson	if (!mac_mmap_revocation)
2390103136Srwatson		return;
2391103136Srwatson
2392100979Srwatson	vm_map_lock_read(map);
2393100979Srwatson	for (vme = map->header.next; vme != &map->header; vme = vme->next) {
2394100979Srwatson		if (vme->eflags & MAP_ENTRY_IS_SUB_MAP) {
2395100979Srwatson			mac_cred_mmapped_drop_perms_recurse(td, cred,
2396100979Srwatson			    vme->object.sub_map);
2397100979Srwatson			continue;
2398100979Srwatson		}
2399100979Srwatson		/*
2400100979Srwatson		 * Skip over entries that obviously are not shared.
2401100979Srwatson		 */
2402100979Srwatson		if (vme->eflags & (MAP_ENTRY_COW | MAP_ENTRY_NOSYNC) ||
2403100979Srwatson		    !vme->max_protection)
2404100979Srwatson			continue;
2405100979Srwatson		/*
2406100979Srwatson		 * Drill down to the deepest backing object.
2407100979Srwatson		 */
2408100979Srwatson		offset = vme->offset;
2409100979Srwatson		object = vme->object.vm_object;
2410100979Srwatson		if (object == NULL)
2411100979Srwatson			continue;
2412100979Srwatson		while (object->backing_object != NULL) {
2413100979Srwatson			object = object->backing_object;
2414100979Srwatson			offset += object->backing_object_offset;
2415100979Srwatson		}
2416100979Srwatson		/*
2417100979Srwatson		 * At the moment, vm_maps and objects aren't considered
2418100979Srwatson		 * by the MAC system, so only things with backing by a
2419100979Srwatson		 * normal object (read: vnodes) are checked.
2420100979Srwatson		 */
2421100979Srwatson		if (object->type != OBJT_VNODE)
2422100979Srwatson			continue;
2423100979Srwatson		vp = (struct vnode *)object->handle;
2424100979Srwatson		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2425104546Srwatson		result = vme->max_protection;
2426104546Srwatson		mac_check_vnode_mmap_downgrade(cred, vp, &result);
2427100979Srwatson		VOP_UNLOCK(vp, 0, td);
2428100979Srwatson		/*
2429100979Srwatson		 * Find out what maximum protection we may be allowing
2430100979Srwatson		 * now but a policy needs to get removed.
2431100979Srwatson		 */
2432100979Srwatson		revokeperms = vme->max_protection & ~result;
2433100979Srwatson		if (!revokeperms)
2434100979Srwatson			continue;
2435102949Sbde		printf("pid %ld: revoking %s perms from %#lx:%ld "
2436102949Sbde		    "(max %s/cur %s)\n", (long)td->td_proc->p_pid,
2437102949Sbde		    prot2str(revokeperms), (u_long)vme->start,
2438102949Sbde		    (long)(vme->end - vme->start),
2439100979Srwatson		    prot2str(vme->max_protection), prot2str(vme->protection));
2440100979Srwatson		vm_map_lock_upgrade(map);
2441100979Srwatson		/*
2442100979Srwatson		 * This is the really simple case: if a map has more
2443100979Srwatson		 * max_protection than is allowed, but it's not being
2444100979Srwatson		 * actually used (that is, the current protection is
2445100979Srwatson		 * still allowed), we can just wipe it out and do
2446100979Srwatson		 * nothing more.
2447100979Srwatson		 */
2448100979Srwatson		if ((vme->protection & revokeperms) == 0) {
2449100979Srwatson			vme->max_protection -= revokeperms;
2450100979Srwatson		} else {
2451100979Srwatson			if (revokeperms & VM_PROT_WRITE) {
2452100979Srwatson				/*
2453100979Srwatson				 * In the more complicated case, flush out all
2454100979Srwatson				 * pending changes to the object then turn it
2455100979Srwatson				 * copy-on-write.
2456100979Srwatson				 */
2457100979Srwatson				vm_object_reference(object);
2458100979Srwatson				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2459100979Srwatson				vm_object_page_clean(object,
2460100979Srwatson				    OFF_TO_IDX(offset),
2461100979Srwatson				    OFF_TO_IDX(offset + vme->end - vme->start +
2462100979Srwatson					PAGE_MASK),
2463100979Srwatson				    OBJPC_SYNC);
2464100979Srwatson				VOP_UNLOCK(vp, 0, td);
2465100979Srwatson				vm_object_deallocate(object);
2466100979Srwatson				/*
2467100979Srwatson				 * Why bother if there's no read permissions
2468100979Srwatson				 * anymore?  For the rest, we need to leave
2469100979Srwatson				 * the write permissions on for COW, or
2470100979Srwatson				 * remove them entirely if configured to.
2471100979Srwatson				 */
2472100979Srwatson				if (!mac_mmap_revocation_via_cow) {
2473100979Srwatson					vme->max_protection &= ~VM_PROT_WRITE;
2474100979Srwatson					vme->protection &= ~VM_PROT_WRITE;
2475100979Srwatson				} if ((revokeperms & VM_PROT_READ) == 0)
2476100979Srwatson					vme->eflags |= MAP_ENTRY_COW |
2477100979Srwatson					    MAP_ENTRY_NEEDS_COPY;
2478100979Srwatson			}
2479100979Srwatson			if (revokeperms & VM_PROT_EXECUTE) {
2480100979Srwatson				vme->max_protection &= ~VM_PROT_EXECUTE;
2481100979Srwatson				vme->protection &= ~VM_PROT_EXECUTE;
2482100979Srwatson			}
2483100979Srwatson			if (revokeperms & VM_PROT_READ) {
2484100979Srwatson				vme->max_protection = 0;
2485100979Srwatson				vme->protection = 0;
2486100979Srwatson			}
2487100979Srwatson			pmap_protect(map->pmap, vme->start, vme->end,
2488100979Srwatson			    vme->protection & ~revokeperms);
2489100979Srwatson			vm_map_simplify_entry(map, vme);
2490100979Srwatson		}
2491100979Srwatson		vm_map_lock_downgrade(map);
2492100979Srwatson	}
2493100979Srwatson	vm_map_unlock_read(map);
2494100979Srwatson}
2495100979Srwatson
2496100979Srwatson/*
2497100979Srwatson * When the subject's label changes, it may require revocation of privilege
2498100979Srwatson * to mapped objects.  This can't be done on-the-fly later with a unified
2499100979Srwatson * buffer cache.
2500100979Srwatson */
2501100979Srwatsonstatic void
2502100979Srwatsonmac_relabel_cred(struct ucred *cred, struct label *newlabel)
2503100979Srwatson{
2504100979Srwatson
2505100979Srwatson	MAC_PERFORM(relabel_cred, cred, newlabel);
2506100979Srwatson}
2507100979Srwatson
2508100979Srwatsonvoid
2509100979Srwatsonmac_relabel_vnode(struct ucred *cred, struct vnode *vp, struct label *newlabel)
2510100979Srwatson{
2511100979Srwatson
2512100979Srwatson	MAC_PERFORM(relabel_vnode, cred, vp, &vp->v_label, newlabel);
2513100979Srwatson}
2514100979Srwatson
2515100979Srwatsonvoid
2516100979Srwatsonmac_create_ifnet(struct ifnet *ifnet)
2517100979Srwatson{
2518100979Srwatson
2519100979Srwatson	MAC_PERFORM(create_ifnet, ifnet, &ifnet->if_label);
2520100979Srwatson}
2521100979Srwatson
2522100979Srwatsonvoid
2523100979Srwatsonmac_create_bpfdesc(struct ucred *cred, struct bpf_d *bpf_d)
2524100979Srwatson{
2525100979Srwatson
2526100979Srwatson	MAC_PERFORM(create_bpfdesc, cred, bpf_d, &bpf_d->bd_label);
2527100979Srwatson}
2528100979Srwatson
2529100979Srwatsonvoid
2530100979Srwatsonmac_create_socket(struct ucred *cred, struct socket *socket)
2531100979Srwatson{
2532100979Srwatson
2533100979Srwatson	MAC_PERFORM(create_socket, cred, socket, &socket->so_label);
2534100979Srwatson}
2535100979Srwatson
2536100979Srwatsonvoid
2537100979Srwatsonmac_create_pipe(struct ucred *cred, struct pipe *pipe)
2538100979Srwatson{
2539100979Srwatson
2540100979Srwatson	MAC_PERFORM(create_pipe, cred, pipe, pipe->pipe_label);
2541100979Srwatson}
2542100979Srwatson
2543100979Srwatsonvoid
2544100979Srwatsonmac_create_socket_from_socket(struct socket *oldsocket,
2545100979Srwatson    struct socket *newsocket)
2546100979Srwatson{
2547100979Srwatson
2548100979Srwatson	MAC_PERFORM(create_socket_from_socket, oldsocket, &oldsocket->so_label,
2549100979Srwatson	    newsocket, &newsocket->so_label);
2550100979Srwatson}
2551100979Srwatson
2552100979Srwatsonstatic void
2553100979Srwatsonmac_relabel_socket(struct ucred *cred, struct socket *socket,
2554100979Srwatson    struct label *newlabel)
2555100979Srwatson{
2556100979Srwatson
2557100979Srwatson	MAC_PERFORM(relabel_socket, cred, socket, &socket->so_label, newlabel);
2558100979Srwatson}
2559100979Srwatson
2560100979Srwatsonstatic void
2561100979Srwatsonmac_relabel_pipe(struct ucred *cred, struct pipe *pipe, struct label *newlabel)
2562100979Srwatson{
2563100979Srwatson
2564100979Srwatson	MAC_PERFORM(relabel_pipe, cred, pipe, pipe->pipe_label, newlabel);
2565100979Srwatson}
2566100979Srwatson
2567100979Srwatsonvoid
2568100979Srwatsonmac_set_socket_peer_from_mbuf(struct mbuf *mbuf, struct socket *socket)
2569100979Srwatson{
2570100979Srwatson
2571100979Srwatson	MAC_PERFORM(set_socket_peer_from_mbuf, mbuf, &mbuf->m_pkthdr.label,
2572100979Srwatson	    socket, &socket->so_peerlabel);
2573100979Srwatson}
2574100979Srwatson
2575100979Srwatsonvoid
2576100979Srwatsonmac_set_socket_peer_from_socket(struct socket *oldsocket,
2577100979Srwatson    struct socket *newsocket)
2578100979Srwatson{
2579100979Srwatson
2580100979Srwatson	MAC_PERFORM(set_socket_peer_from_socket, oldsocket,
2581100979Srwatson	    &oldsocket->so_label, newsocket, &newsocket->so_peerlabel);
2582100979Srwatson}
2583100979Srwatson
2584100979Srwatsonvoid
2585100979Srwatsonmac_create_datagram_from_ipq(struct ipq *ipq, struct mbuf *datagram)
2586100979Srwatson{
2587100979Srwatson
2588100979Srwatson	MAC_PERFORM(create_datagram_from_ipq, ipq, &ipq->ipq_label,
2589100979Srwatson	    datagram, &datagram->m_pkthdr.label);
2590100979Srwatson}
2591100979Srwatson
2592100979Srwatsonvoid
2593100979Srwatsonmac_create_fragment(struct mbuf *datagram, struct mbuf *fragment)
2594100979Srwatson{
2595100979Srwatson
2596100979Srwatson	MAC_PERFORM(create_fragment, datagram, &datagram->m_pkthdr.label,
2597100979Srwatson	    fragment, &fragment->m_pkthdr.label);
2598100979Srwatson}
2599100979Srwatson
2600100979Srwatsonvoid
2601100979Srwatsonmac_create_ipq(struct mbuf *fragment, struct ipq *ipq)
2602100979Srwatson{
2603100979Srwatson
2604100979Srwatson	MAC_PERFORM(create_ipq, fragment, &fragment->m_pkthdr.label, ipq,
2605100979Srwatson	    &ipq->ipq_label);
2606100979Srwatson}
2607100979Srwatson
2608100979Srwatsonvoid
2609100979Srwatsonmac_create_mbuf_from_mbuf(struct mbuf *oldmbuf, struct mbuf *newmbuf)
2610100979Srwatson{
2611100979Srwatson
2612100979Srwatson	MAC_PERFORM(create_mbuf_from_mbuf, oldmbuf, &oldmbuf->m_pkthdr.label,
2613100979Srwatson	    newmbuf, &newmbuf->m_pkthdr.label);
2614100979Srwatson}
2615100979Srwatson
2616100979Srwatsonvoid
2617100979Srwatsonmac_create_mbuf_from_bpfdesc(struct bpf_d *bpf_d, struct mbuf *mbuf)
2618100979Srwatson{
2619100979Srwatson
2620100979Srwatson	MAC_PERFORM(create_mbuf_from_bpfdesc, bpf_d, &bpf_d->bd_label, mbuf,
2621100979Srwatson	    &mbuf->m_pkthdr.label);
2622100979Srwatson}
2623100979Srwatson
2624100979Srwatsonvoid
2625100979Srwatsonmac_create_mbuf_linklayer(struct ifnet *ifnet, struct mbuf *mbuf)
2626100979Srwatson{
2627100979Srwatson
2628100979Srwatson	MAC_PERFORM(create_mbuf_linklayer, ifnet, &ifnet->if_label, mbuf,
2629100979Srwatson	    &mbuf->m_pkthdr.label);
2630100979Srwatson}
2631100979Srwatson
2632100979Srwatsonvoid
2633100979Srwatsonmac_create_mbuf_from_ifnet(struct ifnet *ifnet, struct mbuf *mbuf)
2634100979Srwatson{
2635100979Srwatson
2636100979Srwatson	MAC_PERFORM(create_mbuf_from_ifnet, ifnet, &ifnet->if_label, mbuf,
2637100979Srwatson	    &mbuf->m_pkthdr.label);
2638100979Srwatson}
2639100979Srwatson
2640100979Srwatsonvoid
2641100979Srwatsonmac_create_mbuf_multicast_encap(struct mbuf *oldmbuf, struct ifnet *ifnet,
2642100979Srwatson    struct mbuf *newmbuf)
2643100979Srwatson{
2644100979Srwatson
2645100979Srwatson	MAC_PERFORM(create_mbuf_multicast_encap, oldmbuf,
2646100979Srwatson	    &oldmbuf->m_pkthdr.label, ifnet, &ifnet->if_label, newmbuf,
2647100979Srwatson	    &newmbuf->m_pkthdr.label);
2648100979Srwatson}
2649100979Srwatson
2650100979Srwatsonvoid
2651100979Srwatsonmac_create_mbuf_netlayer(struct mbuf *oldmbuf, struct mbuf *newmbuf)
2652100979Srwatson{
2653100979Srwatson
2654100979Srwatson	MAC_PERFORM(create_mbuf_netlayer, oldmbuf, &oldmbuf->m_pkthdr.label,
2655100979Srwatson	    newmbuf, &newmbuf->m_pkthdr.label);
2656100979Srwatson}
2657100979Srwatson
2658100979Srwatsonint
2659100979Srwatsonmac_fragment_match(struct mbuf *fragment, struct ipq *ipq)
2660100979Srwatson{
2661100979Srwatson	int result;
2662100979Srwatson
2663100979Srwatson	result = 1;
2664100979Srwatson	MAC_BOOLEAN(fragment_match, &&, fragment, &fragment->m_pkthdr.label,
2665100979Srwatson	    ipq, &ipq->ipq_label);
2666100979Srwatson
2667100979Srwatson	return (result);
2668100979Srwatson}
2669100979Srwatson
2670100979Srwatsonvoid
2671100979Srwatsonmac_update_ipq(struct mbuf *fragment, struct ipq *ipq)
2672100979Srwatson{
2673100979Srwatson
2674100979Srwatson	MAC_PERFORM(update_ipq, fragment, &fragment->m_pkthdr.label, ipq,
2675100979Srwatson	    &ipq->ipq_label);
2676100979Srwatson}
2677100979Srwatson
2678100979Srwatsonvoid
2679100979Srwatsonmac_create_mbuf_from_socket(struct socket *socket, struct mbuf *mbuf)
2680100979Srwatson{
2681100979Srwatson
2682100979Srwatson	MAC_PERFORM(create_mbuf_from_socket, socket, &socket->so_label, mbuf,
2683100979Srwatson	    &mbuf->m_pkthdr.label);
2684100979Srwatson}
2685100979Srwatson
2686100979Srwatsonvoid
2687100979Srwatsonmac_create_mount(struct ucred *cred, struct mount *mp)
2688100979Srwatson{
2689100979Srwatson
2690100979Srwatson	MAC_PERFORM(create_mount, cred, mp, &mp->mnt_mntlabel,
2691100979Srwatson	    &mp->mnt_fslabel);
2692100979Srwatson}
2693100979Srwatson
2694100979Srwatsonvoid
2695100979Srwatsonmac_create_root_mount(struct ucred *cred, struct mount *mp)
2696100979Srwatson{
2697100979Srwatson
2698100979Srwatson	MAC_PERFORM(create_root_mount, cred, mp, &mp->mnt_mntlabel,
2699100979Srwatson	    &mp->mnt_fslabel);
2700100979Srwatson}
2701100979Srwatson
2702100979Srwatsonint
2703100979Srwatsonmac_check_bpfdesc_receive(struct bpf_d *bpf_d, struct ifnet *ifnet)
2704100979Srwatson{
2705100979Srwatson	int error;
2706100979Srwatson
2707100979Srwatson	if (!mac_enforce_network)
2708100979Srwatson		return (0);
2709100979Srwatson
2710100979Srwatson	MAC_CHECK(check_bpfdesc_receive, bpf_d, &bpf_d->bd_label, ifnet,
2711100979Srwatson	    &ifnet->if_label);
2712100979Srwatson
2713100979Srwatson	return (error);
2714100979Srwatson}
2715100979Srwatson
2716100979Srwatsonstatic int
2717100979Srwatsonmac_check_cred_relabel(struct ucred *cred, struct label *newlabel)
2718100979Srwatson{
2719100979Srwatson	int error;
2720100979Srwatson
2721100979Srwatson	MAC_CHECK(check_cred_relabel, cred, newlabel);
2722100979Srwatson
2723100979Srwatson	return (error);
2724100979Srwatson}
2725100979Srwatson
2726100979Srwatsonint
2727100979Srwatsonmac_check_cred_visible(struct ucred *u1, struct ucred *u2)
2728100979Srwatson{
2729100979Srwatson	int error;
2730100979Srwatson
2731100979Srwatson	if (!mac_enforce_process)
2732100979Srwatson		return (0);
2733100979Srwatson
2734100979Srwatson	MAC_CHECK(check_cred_visible, u1, u2);
2735100979Srwatson
2736100979Srwatson	return (error);
2737100979Srwatson}
2738100979Srwatson
2739100979Srwatsonint
2740100979Srwatsonmac_check_ifnet_transmit(struct ifnet *ifnet, struct mbuf *mbuf)
2741100979Srwatson{
2742100979Srwatson	int error;
2743100979Srwatson
2744100979Srwatson	if (!mac_enforce_network)
2745100979Srwatson		return (0);
2746100979Srwatson
2747100979Srwatson	KASSERT(mbuf->m_flags & M_PKTHDR, ("packet has no pkthdr"));
2748100979Srwatson	if (!(mbuf->m_pkthdr.label.l_flags & MAC_FLAG_INITIALIZED))
2749100979Srwatson		printf("%s%d: not initialized\n", ifnet->if_name,
2750100979Srwatson		    ifnet->if_unit);
2751100979Srwatson
2752100979Srwatson	MAC_CHECK(check_ifnet_transmit, ifnet, &ifnet->if_label, mbuf,
2753100979Srwatson	    &mbuf->m_pkthdr.label);
2754100979Srwatson
2755100979Srwatson	return (error);
2756100979Srwatson}
2757100979Srwatson
2758100979Srwatsonint
2759100979Srwatsonmac_check_mount_stat(struct ucred *cred, struct mount *mount)
2760100979Srwatson{
2761100979Srwatson	int error;
2762100979Srwatson
2763100979Srwatson	if (!mac_enforce_fs)
2764100979Srwatson		return (0);
2765100979Srwatson
2766100979Srwatson	MAC_CHECK(check_mount_stat, cred, mount, &mount->mnt_mntlabel);
2767100979Srwatson
2768100979Srwatson	return (error);
2769100979Srwatson}
2770100979Srwatson
2771100979Srwatsonint
2772100979Srwatsonmac_check_pipe_ioctl(struct ucred *cred, struct pipe *pipe, unsigned long cmd,
2773100979Srwatson    void *data)
2774100979Srwatson{
2775100979Srwatson	int error;
2776100979Srwatson
2777104269Srwatson	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2778104269Srwatson
2779104269Srwatson	if (!mac_enforce_pipe)
2780104269Srwatson		return (0);
2781104269Srwatson
2782100979Srwatson	MAC_CHECK(check_pipe_ioctl, cred, pipe, pipe->pipe_label, cmd, data);
2783100979Srwatson
2784100979Srwatson	return (error);
2785100979Srwatson}
2786100979Srwatson
2787100979Srwatsonint
2788102115Srwatsonmac_check_pipe_poll(struct ucred *cred, struct pipe *pipe)
2789100979Srwatson{
2790100979Srwatson	int error;
2791100979Srwatson
2792104269Srwatson	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2793104269Srwatson
2794104269Srwatson	if (!mac_enforce_pipe)
2795104269Srwatson		return (0);
2796104269Srwatson
2797102115Srwatson	MAC_CHECK(check_pipe_poll, cred, pipe, pipe->pipe_label);
2798100979Srwatson
2799100979Srwatson	return (error);
2800100979Srwatson}
2801100979Srwatson
2802102115Srwatsonint
2803102115Srwatsonmac_check_pipe_read(struct ucred *cred, struct pipe *pipe)
2804102115Srwatson{
2805102115Srwatson	int error;
2806102115Srwatson
2807104269Srwatson	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2808104269Srwatson
2809104269Srwatson	if (!mac_enforce_pipe)
2810104269Srwatson		return (0);
2811104269Srwatson
2812102115Srwatson	MAC_CHECK(check_pipe_read, cred, pipe, pipe->pipe_label);
2813102115Srwatson
2814102115Srwatson	return (error);
2815102115Srwatson}
2816102115Srwatson
2817100979Srwatsonstatic int
2818100979Srwatsonmac_check_pipe_relabel(struct ucred *cred, struct pipe *pipe,
2819100979Srwatson    struct label *newlabel)
2820100979Srwatson{
2821100979Srwatson	int error;
2822100979Srwatson
2823104269Srwatson	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2824104269Srwatson
2825104269Srwatson	if (!mac_enforce_pipe)
2826104269Srwatson		return (0);
2827104269Srwatson
2828100979Srwatson	MAC_CHECK(check_pipe_relabel, cred, pipe, pipe->pipe_label, newlabel);
2829100979Srwatson
2830100979Srwatson	return (error);
2831100979Srwatson}
2832100979Srwatson
2833100979Srwatsonint
2834102115Srwatsonmac_check_pipe_stat(struct ucred *cred, struct pipe *pipe)
2835102115Srwatson{
2836102115Srwatson	int error;
2837102115Srwatson
2838104269Srwatson	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2839104269Srwatson
2840104269Srwatson	if (!mac_enforce_pipe)
2841104269Srwatson		return (0);
2842104269Srwatson
2843102115Srwatson	MAC_CHECK(check_pipe_stat, cred, pipe, pipe->pipe_label);
2844102115Srwatson
2845102115Srwatson	return (error);
2846102115Srwatson}
2847102115Srwatson
2848102115Srwatsonint
2849102115Srwatsonmac_check_pipe_write(struct ucred *cred, struct pipe *pipe)
2850102115Srwatson{
2851102115Srwatson	int error;
2852102115Srwatson
2853104269Srwatson	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2854104269Srwatson
2855104269Srwatson	if (!mac_enforce_pipe)
2856104269Srwatson		return (0);
2857104269Srwatson
2858102115Srwatson	MAC_CHECK(check_pipe_write, cred, pipe, pipe->pipe_label);
2859102115Srwatson
2860102115Srwatson	return (error);
2861102115Srwatson}
2862102115Srwatson
2863102115Srwatsonint
2864100979Srwatsonmac_check_proc_debug(struct ucred *cred, struct proc *proc)
2865100979Srwatson{
2866100979Srwatson	int error;
2867100979Srwatson
2868102103Srwatson	PROC_LOCK_ASSERT(proc, MA_OWNED);
2869102103Srwatson
2870100979Srwatson	if (!mac_enforce_process)
2871100979Srwatson		return (0);
2872100979Srwatson
2873100979Srwatson	MAC_CHECK(check_proc_debug, cred, proc);
2874100979Srwatson
2875100979Srwatson	return (error);
2876100979Srwatson}
2877100979Srwatson
2878100979Srwatsonint
2879100979Srwatsonmac_check_proc_sched(struct ucred *cred, struct proc *proc)
2880100979Srwatson{
2881100979Srwatson	int error;
2882100979Srwatson
2883102103Srwatson	PROC_LOCK_ASSERT(proc, MA_OWNED);
2884102103Srwatson
2885100979Srwatson	if (!mac_enforce_process)
2886100979Srwatson		return (0);
2887100979Srwatson
2888100979Srwatson	MAC_CHECK(check_proc_sched, cred, proc);
2889100979Srwatson
2890100979Srwatson	return (error);
2891100979Srwatson}
2892100979Srwatson
2893100979Srwatsonint
2894100979Srwatsonmac_check_proc_signal(struct ucred *cred, struct proc *proc, int signum)
2895100979Srwatson{
2896100979Srwatson	int error;
2897100979Srwatson
2898102103Srwatson	PROC_LOCK_ASSERT(proc, MA_OWNED);
2899102103Srwatson
2900100979Srwatson	if (!mac_enforce_process)
2901100979Srwatson		return (0);
2902100979Srwatson
2903100979Srwatson	MAC_CHECK(check_proc_signal, cred, proc, signum);
2904100979Srwatson
2905100979Srwatson	return (error);
2906100979Srwatson}
2907100979Srwatson
2908100979Srwatsonint
2909100979Srwatsonmac_check_socket_bind(struct ucred *ucred, struct socket *socket,
2910100979Srwatson    struct sockaddr *sockaddr)
2911100979Srwatson{
2912100979Srwatson	int error;
2913100979Srwatson
2914100979Srwatson	if (!mac_enforce_socket)
2915100979Srwatson		return (0);
2916100979Srwatson
2917100979Srwatson	MAC_CHECK(check_socket_bind, ucred, socket, &socket->so_label,
2918100979Srwatson	    sockaddr);
2919100979Srwatson
2920100979Srwatson	return (error);
2921100979Srwatson}
2922100979Srwatson
2923100979Srwatsonint
2924100979Srwatsonmac_check_socket_connect(struct ucred *cred, struct socket *socket,
2925100979Srwatson    struct sockaddr *sockaddr)
2926100979Srwatson{
2927100979Srwatson	int error;
2928100979Srwatson
2929100979Srwatson	if (!mac_enforce_socket)
2930100979Srwatson		return (0);
2931100979Srwatson
2932100979Srwatson	MAC_CHECK(check_socket_connect, cred, socket, &socket->so_label,
2933100979Srwatson	    sockaddr);
2934100979Srwatson
2935100979Srwatson	return (error);
2936100979Srwatson}
2937100979Srwatson
2938100979Srwatsonint
2939101933Srwatsonmac_check_socket_deliver(struct socket *socket, struct mbuf *mbuf)
2940100979Srwatson{
2941100979Srwatson	int error;
2942100979Srwatson
2943100979Srwatson	if (!mac_enforce_socket)
2944100979Srwatson		return (0);
2945100979Srwatson
2946101933Srwatson	MAC_CHECK(check_socket_deliver, socket, &socket->so_label, mbuf,
2947101933Srwatson	    &mbuf->m_pkthdr.label);
2948101933Srwatson
2949100979Srwatson	return (error);
2950100979Srwatson}
2951100979Srwatson
2952100979Srwatsonint
2953101933Srwatsonmac_check_socket_listen(struct ucred *cred, struct socket *socket)
2954100979Srwatson{
2955100979Srwatson	int error;
2956100979Srwatson
2957100979Srwatson	if (!mac_enforce_socket)
2958100979Srwatson		return (0);
2959100979Srwatson
2960101933Srwatson	MAC_CHECK(check_socket_listen, cred, socket, &socket->so_label);
2961100979Srwatson	return (error);
2962100979Srwatson}
2963100979Srwatson
2964100979Srwatsonstatic int
2965100979Srwatsonmac_check_socket_relabel(struct ucred *cred, struct socket *socket,
2966100979Srwatson    struct label *newlabel)
2967100979Srwatson{
2968100979Srwatson	int error;
2969100979Srwatson
2970100979Srwatson	MAC_CHECK(check_socket_relabel, cred, socket, &socket->so_label,
2971100979Srwatson	    newlabel);
2972100979Srwatson
2973100979Srwatson	return (error);
2974100979Srwatson}
2975100979Srwatson
2976100979Srwatsonint
2977100979Srwatsonmac_check_socket_visible(struct ucred *cred, struct socket *socket)
2978100979Srwatson{
2979100979Srwatson	int error;
2980100979Srwatson
2981100979Srwatson	if (!mac_enforce_socket)
2982100979Srwatson		return (0);
2983100979Srwatson
2984100979Srwatson	MAC_CHECK(check_socket_visible, cred, socket, &socket->so_label);
2985100979Srwatson
2986100979Srwatson	return (error);
2987100979Srwatson}
2988100979Srwatson
2989100979Srwatsonint
2990100979Srwatsonmac_ioctl_ifnet_get(struct ucred *cred, struct ifreq *ifr,
2991100979Srwatson    struct ifnet *ifnet)
2992100979Srwatson{
2993100979Srwatson	struct mac label;
2994100979Srwatson	int error;
2995100979Srwatson
2996100979Srwatson	error = mac_externalize(&ifnet->if_label, &label);
2997100979Srwatson	if (error)
2998100979Srwatson		return (error);
2999100979Srwatson
3000100979Srwatson	return (copyout(&label, ifr->ifr_ifru.ifru_data, sizeof(label)));
3001100979Srwatson}
3002100979Srwatson
3003100979Srwatsonint
3004100979Srwatsonmac_ioctl_ifnet_set(struct ucred *cred, struct ifreq *ifr,
3005100979Srwatson    struct ifnet *ifnet)
3006100979Srwatson{
3007100979Srwatson	struct mac newlabel;
3008100979Srwatson	struct label intlabel;
3009100979Srwatson	int error;
3010100979Srwatson
3011100979Srwatson	error = copyin(ifr->ifr_ifru.ifru_data, &newlabel, sizeof(newlabel));
3012100979Srwatson	if (error)
3013100979Srwatson		return (error);
3014100979Srwatson
3015100979Srwatson	error = mac_internalize(&intlabel, &newlabel);
3016100979Srwatson	if (error)
3017100979Srwatson		return (error);
3018100979Srwatson
3019100979Srwatson	/*
3020100979Srwatson	 * XXX: Note that this is a redundant privilege check, since
3021100979Srwatson	 * policies impose this check themselves if required by the
3022100979Srwatson	 * policy.  Eventually, this should go away.
3023100979Srwatson	 */
3024100979Srwatson	error = suser_cred(cred, 0);
3025100979Srwatson	if (error)
3026100979Srwatson		goto out;
3027100979Srwatson
3028100979Srwatson	MAC_CHECK(check_ifnet_relabel, cred, ifnet, &ifnet->if_label,
3029100979Srwatson	    &intlabel);
3030100979Srwatson	if (error)
3031100979Srwatson		goto out;
3032100979Srwatson
3033100979Srwatson	MAC_PERFORM(relabel_ifnet, cred, ifnet, &ifnet->if_label, &intlabel);
3034100979Srwatson
3035100979Srwatsonout:
3036100979Srwatson	mac_destroy_temp(&intlabel);
3037100979Srwatson	return (error);
3038100979Srwatson}
3039100979Srwatson
3040100979Srwatsonvoid
3041100979Srwatsonmac_create_devfs_vnode(struct devfs_dirent *de, struct vnode *vp)
3042100979Srwatson{
3043100979Srwatson
3044100979Srwatson	MAC_PERFORM(create_devfs_vnode, de, &de->de_label, vp, &vp->v_label);
3045100979Srwatson}
3046100979Srwatson
3047100979Srwatsonvoid
3048100979Srwatsonmac_create_devfs_device(dev_t dev, struct devfs_dirent *de)
3049100979Srwatson{
3050100979Srwatson
3051100979Srwatson	MAC_PERFORM(create_devfs_device, dev, de, &de->de_label);
3052100979Srwatson}
3053100979Srwatson
3054104533Srwatsonvoid
3055104533Srwatsonmac_create_devfs_symlink(struct ucred *cred, struct devfs_dirent *dd,
3056104533Srwatson    struct devfs_dirent *de)
3057104533Srwatson{
3058104533Srwatson
3059104533Srwatson	MAC_PERFORM(create_devfs_symlink, cred, dd, &dd->de_label, de,
3060104533Srwatson	    &de->de_label);
3061104533Srwatson}
3062104533Srwatson
3063100979Srwatsonstatic int
3064100979Srwatsonmac_stdcreatevnode_ea(struct vnode *vp)
3065100979Srwatson{
3066100979Srwatson	int error;
3067100979Srwatson
3068100979Srwatson	MAC_CHECK(stdcreatevnode_ea, vp, &vp->v_label);
3069100979Srwatson
3070100979Srwatson	return (error);
3071100979Srwatson}
3072100979Srwatson
3073100979Srwatsonvoid
3074100979Srwatsonmac_create_devfs_directory(char *dirname, int dirnamelen,
3075100979Srwatson    struct devfs_dirent *de)
3076100979Srwatson{
3077100979Srwatson
3078100979Srwatson	MAC_PERFORM(create_devfs_directory, dirname, dirnamelen, de,
3079100979Srwatson	    &de->de_label);
3080100979Srwatson}
3081100979Srwatson
3082100979Srwatson/*
3083100979Srwatson * When a new vnode is created, this call will initialize its label.
3084100979Srwatson */
3085100979Srwatsonvoid
3086100979Srwatsonmac_create_vnode(struct ucred *cred, struct vnode *parent,
3087100979Srwatson    struct vnode *child)
3088100979Srwatson{
3089100979Srwatson	int error;
3090100979Srwatson
3091100979Srwatson	ASSERT_VOP_LOCKED(parent, "mac_create_vnode");
3092100979Srwatson	ASSERT_VOP_LOCKED(child, "mac_create_vnode");
3093100979Srwatson
3094100979Srwatson	error = vn_refreshlabel(parent, cred);
3095100979Srwatson	if (error) {
3096100979Srwatson		printf("mac_create_vnode: vn_refreshlabel returned %d\n",
3097100979Srwatson		    error);
3098100979Srwatson		printf("mac_create_vnode: using old vnode label\n");
3099100979Srwatson	}
3100100979Srwatson
3101100979Srwatson	MAC_PERFORM(create_vnode, cred, parent, &parent->v_label, child,
3102100979Srwatson	    &child->v_label);
3103100979Srwatson}
3104100979Srwatson
3105100979Srwatsonint
3106100979Srwatsonmac_setsockopt_label_set(struct ucred *cred, struct socket *so,
3107100979Srwatson    struct mac *extmac)
3108100979Srwatson{
3109100979Srwatson	struct label intlabel;
3110100979Srwatson	int error;
3111100979Srwatson
3112100979Srwatson	error = mac_internalize(&intlabel, extmac);
3113100979Srwatson	if (error)
3114100979Srwatson		return (error);
3115100979Srwatson
3116100979Srwatson	mac_check_socket_relabel(cred, so, &intlabel);
3117100979Srwatson	if (error) {
3118100979Srwatson		mac_destroy_temp(&intlabel);
3119100979Srwatson		return (error);
3120100979Srwatson	}
3121100979Srwatson
3122100979Srwatson	mac_relabel_socket(cred, so, &intlabel);
3123100979Srwatson
3124100979Srwatson	mac_destroy_temp(&intlabel);
3125100979Srwatson	return (0);
3126100979Srwatson}
3127100979Srwatson
3128100979Srwatsonint
3129100979Srwatsonmac_pipe_label_set(struct ucred *cred, struct pipe *pipe, struct label *label)
3130100979Srwatson{
3131100979Srwatson	int error;
3132100979Srwatson
3133104269Srwatson	PIPE_LOCK_ASSERT(pipe, MA_OWNED);
3134104269Srwatson
3135100979Srwatson	error = mac_check_pipe_relabel(cred, pipe, label);
3136100979Srwatson	if (error)
3137100979Srwatson		return (error);
3138100979Srwatson
3139100979Srwatson	mac_relabel_pipe(cred, pipe, label);
3140100979Srwatson
3141100979Srwatson	return (0);
3142100979Srwatson}
3143100979Srwatson
3144100979Srwatsonint
3145100979Srwatsonmac_getsockopt_label_get(struct ucred *cred, struct socket *so,
3146100979Srwatson    struct mac *extmac)
3147100979Srwatson{
3148100979Srwatson
3149100979Srwatson	return (mac_externalize(&so->so_label, extmac));
3150100979Srwatson}
3151100979Srwatson
3152100979Srwatsonint
3153100979Srwatsonmac_getsockopt_peerlabel_get(struct ucred *cred, struct socket *so,
3154100979Srwatson    struct mac *extmac)
3155100979Srwatson{
3156100979Srwatson
3157100979Srwatson	return (mac_externalize(&so->so_peerlabel, extmac));
3158100979Srwatson}
3159100979Srwatson
3160100979Srwatson/*
3161100979Srwatson * Implementation of VOP_SETLABEL() that relies on extended attributes
3162100979Srwatson * to store label data.  Can be referenced by filesystems supporting
3163100979Srwatson * extended attributes.
3164100979Srwatson */
3165100979Srwatsonint
3166100979Srwatsonvop_stdsetlabel_ea(struct vop_setlabel_args *ap)
3167100979Srwatson{
3168100979Srwatson	struct vnode *vp = ap->a_vp;
3169100979Srwatson	struct label *intlabel = ap->a_label;
3170100979Srwatson	struct mac extmac;
3171100979Srwatson	int error;
3172100979Srwatson
3173100979Srwatson	ASSERT_VOP_LOCKED(vp, "vop_stdsetlabel_ea");
3174100979Srwatson
3175100979Srwatson	/*
3176100979Srwatson	 * XXX: Eventually call out to EA check/set calls here.
3177100979Srwatson	 * Be particularly careful to avoid race conditions,
3178100979Srwatson	 * consistency problems, and stability problems when
3179100979Srwatson	 * dealing with multiple EAs.  In particular, we require
3180100979Srwatson	 * the ability to write multiple EAs on the same file in
3181100979Srwatson	 * a single transaction, which the current EA interface
3182100979Srwatson	 * does not provide.
3183100979Srwatson	 */
3184100979Srwatson
3185100979Srwatson	error = mac_externalize(intlabel, &extmac);
3186100979Srwatson	if (error)
3187100979Srwatson		return (error);
3188100979Srwatson
3189100979Srwatson	error = vn_extattr_set(vp, IO_NODELOCKED,
3190100979Srwatson	    FREEBSD_MAC_EXTATTR_NAMESPACE, FREEBSD_MAC_EXTATTR_NAME,
3191100979Srwatson	    sizeof(extmac), (char *)&extmac, curthread);
3192100979Srwatson	if (error)
3193100979Srwatson		return (error);
3194100979Srwatson
3195100979Srwatson	mac_relabel_vnode(ap->a_cred, vp, intlabel);
3196100979Srwatson
3197101308Sjeff	vp->v_vflag |= VV_CACHEDLABEL;
3198100979Srwatson
3199100979Srwatson	return (0);
3200100979Srwatson}
3201100979Srwatson
3202100979Srwatsonstatic int
3203100979Srwatsonvn_setlabel(struct vnode *vp, struct label *intlabel, struct ucred *cred)
3204100979Srwatson{
3205100979Srwatson	int error;
3206100979Srwatson
3207100979Srwatson	if (vp->v_mount == NULL) {
3208100979Srwatson		/* printf("vn_setlabel: null v_mount\n"); */
3209103314Snjl		if (vp->v_type != VNON)
3210103314Snjl			printf("vn_setlabel: null v_mount with non-VNON\n");
3211100979Srwatson		return (EBADF);
3212100979Srwatson	}
3213100979Srwatson
3214100979Srwatson	if ((vp->v_mount->mnt_flag & MNT_MULTILABEL) == 0)
3215100979Srwatson		return (EOPNOTSUPP);
3216100979Srwatson
3217100979Srwatson	/*
3218100979Srwatson	 * Multi-phase commit.  First check the policies to confirm the
3219100979Srwatson	 * change is OK.  Then commit via the filesystem.  Finally,
3220100979Srwatson	 * update the actual vnode label.  Question: maybe the filesystem
3221100979Srwatson	 * should update the vnode at the end as part of VOP_SETLABEL()?
3222100979Srwatson	 */
3223100979Srwatson	error = mac_check_vnode_relabel(cred, vp, intlabel);
3224100979Srwatson	if (error)
3225100979Srwatson		return (error);
3226100979Srwatson
3227100979Srwatson	/*
3228100979Srwatson	 * VADMIN provides the opportunity for the filesystem to make
3229100979Srwatson	 * decisions about who is and is not able to modify labels
3230100979Srwatson	 * and protections on files.  This might not be right.  We can't
3231100979Srwatson	 * assume VOP_SETLABEL() will do it, because we might implement
3232100979Srwatson	 * that as part of vop_stdsetlabel_ea().
3233100979Srwatson	 */
3234100979Srwatson	error = VOP_ACCESS(vp, VADMIN, cred, curthread);
3235100979Srwatson	if (error)
3236100979Srwatson		return (error);
3237100979Srwatson
3238100979Srwatson	error = VOP_SETLABEL(vp, intlabel, cred, curthread);
3239100979Srwatson	if (error)
3240100979Srwatson		return (error);
3241100979Srwatson
3242100979Srwatson	return (0);
3243100979Srwatson}
3244100979Srwatson
3245100979Srwatson/*
3246100979Srwatson * MPSAFE
3247100979Srwatson */
3248100979Srwatsonint
3249100894Srwatson__mac_get_proc(struct thread *td, struct __mac_get_proc_args *uap)
3250100894Srwatson{
3251100979Srwatson	struct mac extmac;
3252100979Srwatson	int error;
3253100894Srwatson
3254100979Srwatson	error = mac_externalize(&td->td_ucred->cr_label, &extmac);
3255100979Srwatson	if (error == 0)
3256100979Srwatson		error = copyout(&extmac, SCARG(uap, mac_p), sizeof(extmac));
3257100979Srwatson
3258100979Srwatson	return (error);
3259100979Srwatson}
3260100979Srwatson
3261100979Srwatson/*
3262100979Srwatson * MPSAFE
3263100979Srwatson */
3264100979Srwatsonint
3265100979Srwatson__mac_set_proc(struct thread *td, struct __mac_set_proc_args *uap)
3266100979Srwatson{
3267100979Srwatson	struct ucred *newcred, *oldcred;
3268100979Srwatson	struct proc *p;
3269100979Srwatson	struct mac extmac;
3270100979Srwatson	struct label intlabel;
3271100979Srwatson	int error;
3272100979Srwatson
3273100979Srwatson	error = copyin(SCARG(uap, mac_p), &extmac, sizeof(extmac));
3274100979Srwatson	if (error)
3275100979Srwatson		return (error);
3276100979Srwatson
3277100979Srwatson	error = mac_internalize(&intlabel, &extmac);
3278100979Srwatson	if (error)
3279100979Srwatson		return (error);
3280100979Srwatson
3281100979Srwatson	newcred = crget();
3282100979Srwatson
3283100979Srwatson	p = td->td_proc;
3284100979Srwatson	PROC_LOCK(p);
3285100979Srwatson	oldcred = p->p_ucred;
3286100979Srwatson
3287100979Srwatson	error = mac_check_cred_relabel(oldcred, &intlabel);
3288100979Srwatson	if (error) {
3289100979Srwatson		PROC_UNLOCK(p);
3290100979Srwatson		mac_destroy_temp(&intlabel);
3291100979Srwatson		crfree(newcred);
3292100979Srwatson		return (error);
3293100979Srwatson	}
3294100979Srwatson
3295100979Srwatson	setsugid(p);
3296100979Srwatson	crcopy(newcred, oldcred);
3297100979Srwatson	mac_relabel_cred(newcred, &intlabel);
3298102136Srwatson	p->p_ucred = newcred;
3299100979Srwatson
3300102136Srwatson	/*
3301102136Srwatson	 * Grab additional reference for use while revoking mmaps, prior
3302102136Srwatson	 * to releasing the proc lock and sharing the cred.
3303102136Srwatson	 */
3304102136Srwatson	crhold(newcred);
3305100979Srwatson	PROC_UNLOCK(p);
3306102136Srwatson
3307103135Srwatson	mtx_lock(&Giant);
3308102136Srwatson	mac_cred_mmapped_drop_perms(td, newcred);
3309103135Srwatson	mtx_unlock(&Giant);
3310102136Srwatson
3311102136Srwatson	crfree(newcred);	/* Free revocation reference. */
3312100979Srwatson	crfree(oldcred);
3313100979Srwatson	mac_destroy_temp(&intlabel);
3314100979Srwatson	return (0);
3315100979Srwatson}
3316100979Srwatson
3317100979Srwatson/*
3318100979Srwatson * MPSAFE
3319100979Srwatson */
3320100979Srwatsonint
3321100979Srwatson__mac_get_fd(struct thread *td, struct __mac_get_fd_args *uap)
3322100979Srwatson{
3323100979Srwatson	struct file *fp;
3324100979Srwatson	struct mac extmac;
3325100979Srwatson	struct vnode *vp;
3326100979Srwatson	struct pipe *pipe;
3327100979Srwatson	int error;
3328100979Srwatson
3329100979Srwatson	mtx_lock(&Giant);
3330100979Srwatson
3331100979Srwatson	error = fget(td, SCARG(uap, fd), &fp);
3332100979Srwatson	if (error)
3333100979Srwatson		goto out;
3334100979Srwatson
3335100979Srwatson	switch (fp->f_type) {
3336100979Srwatson	case DTYPE_FIFO:
3337100979Srwatson	case DTYPE_VNODE:
3338100979Srwatson		vp = (struct vnode *)fp->f_data;
3339100979Srwatson
3340100979Srwatson		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3341100979Srwatson		error = vn_refreshlabel(vp, td->td_ucred);
3342100979Srwatson		if (error == 0)
3343100979Srwatson			error = mac_externalize(&vp->v_label, &extmac);
3344100979Srwatson		VOP_UNLOCK(vp, 0, td);
3345100979Srwatson		break;
3346100979Srwatson	case DTYPE_PIPE:
3347100979Srwatson		pipe = (struct pipe *)fp->f_data;
3348100979Srwatson		error = mac_externalize(pipe->pipe_label, &extmac);
3349100979Srwatson		break;
3350100979Srwatson	default:
3351100979Srwatson		error = EINVAL;
3352100979Srwatson	}
3353100979Srwatson
3354100979Srwatson	if (error == 0)
3355100979Srwatson		error = copyout(&extmac, SCARG(uap, mac_p), sizeof(extmac));
3356100979Srwatson
3357100979Srwatson	fdrop(fp, td);
3358100979Srwatson
3359100979Srwatsonout:
3360100979Srwatson	mtx_unlock(&Giant);
3361100979Srwatson	return (error);
3362100979Srwatson}
3363100979Srwatson
3364100979Srwatson/*
3365100979Srwatson * MPSAFE
3366100979Srwatson */
3367100979Srwatsonint
3368100979Srwatson__mac_get_file(struct thread *td, struct __mac_get_file_args *uap)
3369100979Srwatson{
3370100979Srwatson	struct nameidata nd;
3371100979Srwatson	struct mac extmac;
3372100979Srwatson	int error;
3373100979Srwatson
3374100979Srwatson	mtx_lock(&Giant);
3375100979Srwatson	NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_USERSPACE,
3376100979Srwatson	    SCARG(uap, path_p), td);
3377100979Srwatson	error = namei(&nd);
3378100979Srwatson	if (error)
3379100979Srwatson		goto out;
3380100979Srwatson
3381100979Srwatson	error = vn_refreshlabel(nd.ni_vp, td->td_ucred);
3382100979Srwatson	if (error == 0)
3383100979Srwatson		error = mac_externalize(&nd.ni_vp->v_label, &extmac);
3384100979Srwatson	NDFREE(&nd, 0);
3385100979Srwatson	if (error)
3386100979Srwatson		goto out;
3387100979Srwatson
3388100979Srwatson	error = copyout(&extmac, SCARG(uap, mac_p), sizeof(extmac));
3389100979Srwatson
3390100979Srwatsonout:
3391100979Srwatson	mtx_unlock(&Giant);
3392100979Srwatson	return (error);
3393100979Srwatson}
3394100979Srwatson
3395100979Srwatson/*
3396100979Srwatson * MPSAFE
3397100979Srwatson */
3398100979Srwatsonint
3399100979Srwatson__mac_set_fd(struct thread *td, struct __mac_set_fd_args *uap)
3400100979Srwatson{
3401100979Srwatson	struct file *fp;
3402100979Srwatson	struct mac extmac;
3403100979Srwatson	struct label intlabel;
3404100979Srwatson	struct mount *mp;
3405100979Srwatson	struct vnode *vp;
3406100979Srwatson	struct pipe *pipe;
3407100979Srwatson	int error;
3408100979Srwatson
3409100979Srwatson	mtx_lock(&Giant);
3410100979Srwatson	error = fget(td, SCARG(uap, fd), &fp);
3411100979Srwatson	if (error)
3412100979Srwatson		goto out1;
3413100979Srwatson
3414100979Srwatson	error = copyin(SCARG(uap, mac_p), &extmac, sizeof(extmac));
3415100979Srwatson	if (error)
3416100979Srwatson		goto out2;
3417100979Srwatson
3418100979Srwatson	error = mac_internalize(&intlabel, &extmac);
3419100979Srwatson	if (error)
3420100979Srwatson		goto out2;
3421100979Srwatson
3422100979Srwatson	switch (fp->f_type) {
3423100979Srwatson	case DTYPE_FIFO:
3424100979Srwatson	case DTYPE_VNODE:
3425100979Srwatson		vp = (struct vnode *)fp->f_data;
3426100979Srwatson		error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3427100979Srwatson		if (error != 0)
3428100979Srwatson			break;
3429100979Srwatson
3430100979Srwatson		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3431100979Srwatson		error = vn_setlabel(vp, &intlabel, td->td_ucred);
3432100979Srwatson		VOP_UNLOCK(vp, 0, td);
3433100979Srwatson		vn_finished_write(mp);
3434100979Srwatson		mac_destroy_temp(&intlabel);
3435100979Srwatson		break;
3436100979Srwatson	case DTYPE_PIPE:
3437100979Srwatson		pipe = (struct pipe *)fp->f_data;
3438104269Srwatson		PIPE_LOCK(pipe);
3439100979Srwatson		error = mac_pipe_label_set(td->td_ucred, pipe, &intlabel);
3440104269Srwatson		PIPE_UNLOCK(pipe);
3441100979Srwatson		break;
3442100979Srwatson	default:
3443100979Srwatson		error = EINVAL;
3444100979Srwatson	}
3445100979Srwatson
3446100979Srwatsonout2:
3447100979Srwatson	fdrop(fp, td);
3448100979Srwatsonout1:
3449100979Srwatson	mtx_unlock(&Giant);
3450100979Srwatson	return (error);
3451100979Srwatson}
3452100979Srwatson
3453100979Srwatson/*
3454100979Srwatson * MPSAFE
3455100979Srwatson */
3456100979Srwatsonint
3457100979Srwatson__mac_set_file(struct thread *td, struct __mac_set_file_args *uap)
3458100979Srwatson{
3459100979Srwatson	struct nameidata nd;
3460100979Srwatson	struct mac extmac;
3461100979Srwatson	struct label intlabel;
3462100979Srwatson	struct mount *mp;
3463100979Srwatson	int error;
3464100979Srwatson
3465100979Srwatson	mtx_lock(&Giant);
3466100979Srwatson
3467100979Srwatson	error = copyin(SCARG(uap, mac_p), &extmac, sizeof(extmac));
3468100979Srwatson	if (error)
3469100979Srwatson		goto out;
3470100979Srwatson
3471100979Srwatson	error = mac_internalize(&intlabel, &extmac);
3472100979Srwatson	if (error)
3473100979Srwatson		goto out;
3474100979Srwatson
3475100979Srwatson	NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_USERSPACE,
3476100979Srwatson	    SCARG(uap, path_p), td);
3477100979Srwatson	error = namei(&nd);
3478100979Srwatson	if (error)
3479100979Srwatson		goto out2;
3480100979Srwatson	error = vn_start_write(nd.ni_vp, &mp, V_WAIT | PCATCH);
3481100979Srwatson	if (error)
3482100979Srwatson		goto out2;
3483100979Srwatson
3484100979Srwatson	error = vn_setlabel(nd.ni_vp, &intlabel, td->td_ucred);
3485100979Srwatson
3486100979Srwatson	vn_finished_write(mp);
3487100979Srwatsonout2:
3488100979Srwatson	mac_destroy_temp(&intlabel);
3489100979Srwatson	NDFREE(&nd, 0);
3490100979Srwatsonout:
3491100979Srwatson	mtx_unlock(&Giant);
3492100979Srwatson	return (error);
3493100979Srwatson}
3494100979Srwatson
3495102123Srwatsonint
3496102123Srwatsonmac_syscall(struct thread *td, struct mac_syscall_args *uap)
3497102123Srwatson{
3498102123Srwatson	struct mac_policy_conf *mpc;
3499102123Srwatson	char target[MAC_MAX_POLICY_NAME];
3500102123Srwatson	int error;
3501102123Srwatson
3502102123Srwatson	error = copyinstr(SCARG(uap, policy), target, sizeof(target), NULL);
3503102123Srwatson	if (error)
3504102123Srwatson		return (error);
3505102123Srwatson
3506102123Srwatson	error = ENOSYS;
3507102123Srwatson	MAC_POLICY_LIST_BUSY();
3508102123Srwatson	LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {
3509102123Srwatson		if (strcmp(mpc->mpc_name, target) == 0 &&
3510102123Srwatson		    mpc->mpc_ops->mpo_syscall != NULL) {
3511102123Srwatson			error = mpc->mpc_ops->mpo_syscall(td,
3512102123Srwatson			    SCARG(uap, call), SCARG(uap, arg));
3513102123Srwatson			goto out;
3514102123Srwatson		}
3515102123Srwatson	}
3516102123Srwatson
3517102123Srwatsonout:
3518102123Srwatson	MAC_POLICY_LIST_UNBUSY();
3519102123Srwatson	return (error);
3520102123Srwatson}
3521102123Srwatson
3522100979SrwatsonSYSINIT(mac, SI_SUB_MAC, SI_ORDER_FIRST, mac_init, NULL);
3523100979SrwatsonSYSINIT(mac_late, SI_SUB_MAC_LATE, SI_ORDER_FIRST, mac_late_init, NULL);
3524100979Srwatson
3525100979Srwatson#else /* !MAC */
3526100979Srwatson
3527100979Srwatsonint
3528100979Srwatson__mac_get_proc(struct thread *td, struct __mac_get_proc_args *uap)
3529100979Srwatson{
3530100979Srwatson
3531100894Srwatson	return (ENOSYS);
3532100894Srwatson}
3533100894Srwatson
3534100894Srwatsonint
3535100894Srwatson__mac_set_proc(struct thread *td, struct __mac_set_proc_args *uap)
3536100894Srwatson{
3537100894Srwatson
3538100894Srwatson	return (ENOSYS);
3539100894Srwatson}
3540100894Srwatson
3541100894Srwatsonint
3542100894Srwatson__mac_get_fd(struct thread *td, struct __mac_get_fd_args *uap)
3543100894Srwatson{
3544100894Srwatson
3545100894Srwatson	return (ENOSYS);
3546100894Srwatson}
3547100894Srwatson
3548100894Srwatsonint
3549100894Srwatson__mac_get_file(struct thread *td, struct __mac_get_file_args *uap)
3550100894Srwatson{
3551100894Srwatson
3552100894Srwatson	return (ENOSYS);
3553100894Srwatson}
3554100894Srwatson
3555100894Srwatsonint
3556100894Srwatson__mac_set_fd(struct thread *td, struct __mac_set_fd_args *uap)
3557100894Srwatson{
3558100894Srwatson
3559100894Srwatson	return (ENOSYS);
3560100894Srwatson}
3561100894Srwatson
3562100894Srwatsonint
3563100894Srwatson__mac_set_file(struct thread *td, struct __mac_set_file_args *uap)
3564100894Srwatson{
3565100894Srwatson
3566100894Srwatson	return (ENOSYS);
3567100894Srwatson}
3568100979Srwatson
3569102123Srwatsonint
3570102123Srwatsonmac_syscall(struct thread *td, struct mac_syscall_args *uap)
3571102123Srwatson{
3572102123Srwatson
3573102123Srwatson	return (ENOSYS);
3574102123Srwatson}
3575102123Srwatson
3576100979Srwatson#endif /* !MAC */
3577