mac_process.c revision 102136
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 102136 2002-08-19 20:26:32Z 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"
49101173Srwatson
50100894Srwatson#include <sys/param.h>
51100979Srwatson#include <sys/extattr.h>
52100979Srwatson#include <sys/kernel.h>
53100979Srwatson#include <sys/lock.h>
54100979Srwatson#include <sys/mutex.h>
55100979Srwatson#include <sys/sx.h>
56100979Srwatson#include <sys/mac.h>
57101712Srwatson#include <sys/module.h>
58100979Srwatson#include <sys/proc.h>
59100979Srwatson#include <sys/systm.h>
60100894Srwatson#include <sys/sysproto.h>
61100894Srwatson#include <sys/sysent.h>
62100979Srwatson#include <sys/vnode.h>
63100979Srwatson#include <sys/mount.h>
64100979Srwatson#include <sys/file.h>
65100979Srwatson#include <sys/namei.h>
66100979Srwatson#include <sys/socket.h>
67100979Srwatson#include <sys/pipe.h>
68100979Srwatson#include <sys/socketvar.h>
69100979Srwatson#include <sys/sx.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/bpf.h>
82100979Srwatson#include <net/bpfdesc.h>
83100979Srwatson#include <net/if.h>
84100979Srwatson#include <net/if_var.h>
85100979Srwatson
86100979Srwatson#include <netinet/in.h>
87100979Srwatson#include <netinet/ip_var.h>
88100979Srwatson
89100979Srwatson#ifdef MAC
90100979Srwatson
91101712Srwatson/*
92101712Srwatson * Declare that the kernel provides MAC support, version 1.  This permits
93101712Srwatson * modules to refuse to be loaded if the necessary support isn't present,
94101712Srwatson * even if it's pre-boot.
95101712Srwatson */
96101712SrwatsonMODULE_VERSION(kernel_mac_support, 1);
97101712Srwatson
98100979SrwatsonSYSCTL_DECL(_security);
99100979Srwatson
100100979SrwatsonSYSCTL_NODE(_security, OID_AUTO, mac, CTLFLAG_RW, 0,
101100979Srwatson    "TrustedBSD MAC policy controls");
102100979SrwatsonSYSCTL_NODE(_security_mac, OID_AUTO, debug, CTLFLAG_RW, 0,
103100979Srwatson    "TrustedBSD MAC debug info");
104100979Srwatson
105100979Srwatsonstatic int	mac_debug_label_fallback = 0;
106100979SrwatsonSYSCTL_INT(_security_mac_debug, OID_AUTO, label_fallback, CTLFLAG_RW,
107100979Srwatson    &mac_debug_label_fallback, 0, "Filesystems should fall back to fs label"
108100979Srwatson    "when label is corrupted.");
109100979SrwatsonTUNABLE_INT("security.mac.debug_label_fallback",
110100979Srwatson    &mac_debug_label_fallback);
111100979Srwatson
112100979Srwatson#ifndef MAC_MAX_POLICIES
113100979Srwatson#define	MAC_MAX_POLICIES	8
114100979Srwatson#endif
115100979Srwatson#if MAC_MAX_POLICIES > 32
116100979Srwatson#error "MAC_MAX_POLICIES too large"
117100979Srwatson#endif
118100979Srwatsonstatic unsigned int mac_max_policies = MAC_MAX_POLICIES;
119100979Srwatsonstatic unsigned int mac_policy_offsets_free = (1 << MAC_MAX_POLICIES) - 1;
120100979SrwatsonSYSCTL_UINT(_security_mac, OID_AUTO, max_policies, CTLFLAG_RD,
121100979Srwatson    &mac_max_policies, 0, "");
122100979Srwatson
123100979Srwatsonstatic int	mac_late = 0;
124100979Srwatson
125100979Srwatsonstatic int	mac_enforce_fs = 1;
126100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, enforce_fs, CTLFLAG_RW,
127100979Srwatson    &mac_enforce_fs, 0, "Enforce MAC policy on file system objects");
128100979SrwatsonTUNABLE_INT("security.mac.enforce_fs", &mac_enforce_fs);
129100979Srwatson
130100979Srwatsonstatic int	mac_enforce_network = 1;
131100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, enforce_network, CTLFLAG_RW,
132100979Srwatson    &mac_enforce_network, 0, "Enforce MAC policy on network packets");
133100979SrwatsonTUNABLE_INT("security.mac.enforce_network", &mac_enforce_network);
134100979Srwatson
135100979Srwatsonstatic int	mac_enforce_process = 1;
136100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, enforce_process, CTLFLAG_RW,
137100979Srwatson    &mac_enforce_process, 0, "Enforce MAC policy on inter-process operations");
138100979SrwatsonTUNABLE_INT("security.mac.enforce_process", &mac_enforce_process);
139100979Srwatson
140100979Srwatsonstatic int	mac_enforce_socket = 1;
141100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, enforce_socket, CTLFLAG_RW,
142100979Srwatson    &mac_enforce_socket, 0, "Enforce MAC policy on socket operations");
143100979SrwatsonTUNABLE_INT("security.mac.enforce_socket", &mac_enforce_socket);
144100979Srwatson
145100979Srwatsonstatic int	mac_enforce_pipe = 1;
146100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, enforce_pipe, CTLFLAG_RW,
147100979Srwatson    &mac_enforce_pipe, 0, "Enforce MAC policy on pipe operations");
148100979Srwatson
149100979Srwatsonstatic int	mac_label_size = sizeof(struct mac);
150100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, label_size, CTLFLAG_RD,
151100979Srwatson    &mac_label_size, 0, "Pre-compiled MAC label size");
152100979Srwatson
153100979Srwatsonstatic int	mac_cache_fslabel_in_vnode = 1;
154100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, cache_fslabel_in_vnode, CTLFLAG_RW,
155100979Srwatson    &mac_cache_fslabel_in_vnode, 0, "Cache mount fslabel in vnode");
156100979SrwatsonTUNABLE_INT("security.mac.cache_fslabel_in_vnode",
157100979Srwatson    &mac_cache_fslabel_in_vnode);
158100979Srwatson
159100979Srwatsonstatic int	mac_vnode_label_cache_hits = 0;
160100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, vnode_label_cache_hits, CTLFLAG_RD,
161100979Srwatson    &mac_vnode_label_cache_hits, 0, "Cache hits on vnode labels");
162100979Srwatsonstatic int	mac_vnode_label_cache_misses = 0;
163100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, vnode_label_cache_misses, CTLFLAG_RD,
164100979Srwatson    &mac_vnode_label_cache_misses, 0, "Cache misses on vnode labels");
165101892Srwatsonstatic int	mac_mmap_revocation_via_cow = 0;
166100979SrwatsonSYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation_via_cow, CTLFLAG_RW,
167100979Srwatson    &mac_mmap_revocation_via_cow, 0, "Revoke mmap access to files via "
168100979Srwatson    "copy-on-write semantics, or by removing all write access");
169100979Srwatson
170101988Srwatson#ifdef MAC_DEBUG
171100979Srwatsonstatic unsigned int nmacmbufs, nmaccreds, nmacifnets, nmacbpfdescs,
172100979Srwatson    nmacsockets, nmacmounts, nmactemp, nmacvnodes, nmacdevfsdirents,
173100979Srwatson    nmacipqs, nmacpipes;
174100979SrwatsonSYSCTL_UINT(_security_mac_debug, OID_AUTO, mbufs, CTLFLAG_RD,
175100979Srwatson    &nmacmbufs, 0, "number of mbufs in use");
176100979SrwatsonSYSCTL_UINT(_security_mac_debug, OID_AUTO, creds, CTLFLAG_RD,
177100979Srwatson    &nmaccreds, 0, "number of ucreds in use");
178100979SrwatsonSYSCTL_UINT(_security_mac_debug, OID_AUTO, ifnets, CTLFLAG_RD,
179100979Srwatson    &nmacifnets, 0, "number of ifnets in use");
180100979SrwatsonSYSCTL_UINT(_security_mac_debug, OID_AUTO, ipqs, CTLFLAG_RD,
181100979Srwatson    &nmacipqs, 0, "number of ipqs in use");
182100979SrwatsonSYSCTL_UINT(_security_mac_debug, OID_AUTO, bpfdescs, CTLFLAG_RD,
183100979Srwatson    &nmacbpfdescs, 0, "number of bpfdescs in use");
184100979SrwatsonSYSCTL_UINT(_security_mac_debug, OID_AUTO, sockets, CTLFLAG_RD,
185100979Srwatson    &nmacsockets, 0, "number of sockets in use");
186100979SrwatsonSYSCTL_UINT(_security_mac_debug, OID_AUTO, pipes, CTLFLAG_RD,
187100979Srwatson    &nmacpipes, 0, "number of pipes in use");
188100979SrwatsonSYSCTL_UINT(_security_mac_debug, OID_AUTO, mounts, CTLFLAG_RD,
189100979Srwatson    &nmacmounts, 0, "number of mounts in use");
190100979SrwatsonSYSCTL_UINT(_security_mac_debug, OID_AUTO, temp, CTLFLAG_RD,
191100979Srwatson    &nmactemp, 0, "number of temporary labels in use");
192100979SrwatsonSYSCTL_UINT(_security_mac_debug, OID_AUTO, vnodes, CTLFLAG_RD,
193100979Srwatson    &nmacvnodes, 0, "number of vnodes in use");
194100979SrwatsonSYSCTL_UINT(_security_mac_debug, OID_AUTO, devfsdirents, CTLFLAG_RD,
195100979Srwatson    &nmacdevfsdirents, 0, "number of devfs dirents inuse");
196101988Srwatson#endif
197100979Srwatson
198100979Srwatsonstatic int	error_select(int error1, int error2);
199100979Srwatsonstatic int	mac_externalize(struct label *label, struct mac *mac);
200100979Srwatsonstatic int	mac_policy_register(struct mac_policy_conf *mpc);
201100979Srwatsonstatic int	mac_policy_unregister(struct mac_policy_conf *mpc);
202100979Srwatson
203100979Srwatsonstatic int	mac_stdcreatevnode_ea(struct vnode *vp);
204100979Srwatsonstatic void	mac_cred_mmapped_drop_perms(struct thread *td,
205100979Srwatson		    struct ucred *cred);
206100979Srwatsonstatic void	mac_cred_mmapped_drop_perms_recurse(struct thread *td,
207100979Srwatson		    struct ucred *cred, struct vm_map *map);
208100979Srwatson
209100979SrwatsonMALLOC_DEFINE(M_MACOPVEC, "macopvec", "MAC policy operation vector");
210100979SrwatsonMALLOC_DEFINE(M_MACPIPELABEL, "macpipelabel", "MAC labels for pipes");
211100979Srwatson
212100979Srwatson/*
213100979Srwatson * mac_policy_list_lock protects the consistency of 'mac_policy_list',
214100979Srwatson * the linked list of attached policy modules.  Read-only consumers of
215100979Srwatson * the list must acquire a shared lock for the duration of their use;
216100979Srwatson * writers must acquire an exclusive lock.  Note that for compound
217100979Srwatson * operations, locks should be held for the entire compound operation,
218100979Srwatson * and that this is not yet done for relabel requests.
219100979Srwatson */
220100979Srwatsonstatic struct mtx mac_policy_list_lock;
221100979Srwatsonstatic LIST_HEAD(, mac_policy_conf) mac_policy_list;
222100979Srwatsonstatic int mac_policy_list_busy;
223100979Srwatson#define	MAC_POLICY_LIST_LOCKINIT()	mtx_init(&mac_policy_list_lock,	\
224100979Srwatson	"mac_policy_list_lock", NULL, MTX_DEF);
225100979Srwatson#define	MAC_POLICY_LIST_LOCK()	mtx_lock(&mac_policy_list_lock);
226100979Srwatson#define	MAC_POLICY_LIST_UNLOCK()	mtx_unlock(&mac_policy_list_lock);
227100979Srwatson
228100979Srwatson#define	MAC_POLICY_LIST_BUSY() do {					\
229100979Srwatson	MAC_POLICY_LIST_LOCK();						\
230100979Srwatson	mac_policy_list_busy++;						\
231100979Srwatson	MAC_POLICY_LIST_UNLOCK();					\
232100979Srwatson} while (0)
233100979Srwatson
234100979Srwatson#define	MAC_POLICY_LIST_UNBUSY() do {					\
235100979Srwatson	MAC_POLICY_LIST_LOCK();						\
236100979Srwatson	mac_policy_list_busy--;						\
237100979Srwatson	if (mac_policy_list_busy < 0)					\
238100979Srwatson		panic("Extra mac_policy_list_busy--");			\
239100979Srwatson	MAC_POLICY_LIST_UNLOCK();					\
240100979Srwatson} while (0)
241100979Srwatson
242100979Srwatson/*
243100979Srwatson * MAC_CHECK performs the designated check by walking the policy
244100979Srwatson * module list and checking with each as to how it feels about the
245100979Srwatson * request.  Note that it returns its value via 'error' in the scope
246100979Srwatson * of the caller.
247100979Srwatson */
248100979Srwatson#define	MAC_CHECK(check, args...) do {					\
249100979Srwatson	struct mac_policy_conf *mpc;					\
250100979Srwatson									\
251100979Srwatson	error = 0;							\
252100979Srwatson	MAC_POLICY_LIST_BUSY();						\
253100979Srwatson	LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {			\
254100979Srwatson		if (mpc->mpc_ops->mpo_ ## check != NULL)		\
255100979Srwatson			error = error_select(				\
256100979Srwatson			    mpc->mpc_ops->mpo_ ## check (args),		\
257100979Srwatson			    error);					\
258100979Srwatson	}								\
259100979Srwatson	MAC_POLICY_LIST_UNBUSY();					\
260100979Srwatson} while (0)
261100979Srwatson
262100979Srwatson/*
263100979Srwatson * MAC_BOOLEAN performs the designated boolean composition by walking
264100979Srwatson * the module list, invoking each instance of the operation, and
265100979Srwatson * combining the results using the passed C operator.  Note that it
266100979Srwatson * returns its value via 'result' in the scope of the caller, which
267100979Srwatson * should be initialized by the caller in a meaningful way to get
268100979Srwatson * a meaningful result.
269100979Srwatson */
270100979Srwatson#define	MAC_BOOLEAN(operation, composition, args...) do {		\
271100979Srwatson	struct mac_policy_conf *mpc;					\
272100979Srwatson									\
273100979Srwatson	MAC_POLICY_LIST_BUSY();						\
274100979Srwatson	LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {			\
275100979Srwatson		if (mpc->mpc_ops->mpo_ ## operation != NULL)		\
276100979Srwatson			result = result composition			\
277100979Srwatson			    mpc->mpc_ops->mpo_ ## operation (args);	\
278100979Srwatson	}								\
279100979Srwatson	MAC_POLICY_LIST_UNBUSY();					\
280100979Srwatson} while (0)
281100979Srwatson
282100979Srwatson/*
283100979Srwatson * MAC_PERFORM performs the designated operation by walking the policy
284100979Srwatson * module list and invoking that operation for each policy.
285100979Srwatson */
286100979Srwatson#define	MAC_PERFORM(operation, args...) do {				\
287100979Srwatson	struct mac_policy_conf *mpc;					\
288100979Srwatson									\
289100979Srwatson	MAC_POLICY_LIST_BUSY();						\
290100979Srwatson	LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {			\
291100979Srwatson		if (mpc->mpc_ops->mpo_ ## operation != NULL)		\
292100979Srwatson			mpc->mpc_ops->mpo_ ## operation (args);		\
293100979Srwatson	}								\
294100979Srwatson	MAC_POLICY_LIST_UNBUSY();					\
295100979Srwatson} while (0)
296100979Srwatson
297100979Srwatson/*
298100979Srwatson * Initialize the MAC subsystem, including appropriate SMP locks.
299100979Srwatson */
300100979Srwatsonstatic void
301100979Srwatsonmac_init(void)
302100979Srwatson{
303100979Srwatson
304100979Srwatson	LIST_INIT(&mac_policy_list);
305100979Srwatson	MAC_POLICY_LIST_LOCKINIT();
306100979Srwatson}
307100979Srwatson
308100979Srwatson/*
309100979Srwatson * For the purposes of modules that want to know if they were loaded
310100979Srwatson * "early", set the mac_late flag once we've processed modules either
311100979Srwatson * linked into the kernel, or loaded before the kernel startup.
312100979Srwatson */
313100979Srwatsonstatic void
314100979Srwatsonmac_late_init(void)
315100979Srwatson{
316100979Srwatson
317100979Srwatson	mac_late = 1;
318100979Srwatson}
319100979Srwatson
320100979Srwatson/*
321100979Srwatson * Allow MAC policy modules to register during boot, etc.
322100979Srwatson */
323100894Srwatsonint
324100979Srwatsonmac_policy_modevent(module_t mod, int type, void *data)
325100979Srwatson{
326100979Srwatson	struct mac_policy_conf *mpc;
327100979Srwatson	int error;
328100979Srwatson
329100979Srwatson	error = 0;
330100979Srwatson	mpc = (struct mac_policy_conf *) data;
331100979Srwatson
332100979Srwatson	switch (type) {
333100979Srwatson	case MOD_LOAD:
334100979Srwatson		if (mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_NOTLATE &&
335100979Srwatson		    mac_late) {
336100979Srwatson			printf("mac_policy_modevent: can't load %s policy "
337100979Srwatson			    "after booting\n", mpc->mpc_name);
338100979Srwatson			error = EBUSY;
339100979Srwatson			break;
340100979Srwatson		}
341100979Srwatson		error = mac_policy_register(mpc);
342100979Srwatson		break;
343100979Srwatson	case MOD_UNLOAD:
344100979Srwatson		/* Don't unregister the module if it was never registered. */
345100979Srwatson		if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED)
346100979Srwatson		    != 0)
347100979Srwatson			error = mac_policy_unregister(mpc);
348100979Srwatson		else
349100979Srwatson			error = 0;
350100979Srwatson		break;
351100979Srwatson	default:
352100979Srwatson		break;
353100979Srwatson	}
354100979Srwatson
355100979Srwatson	return (error);
356100979Srwatson}
357100979Srwatson
358100979Srwatsonstatic int
359100979Srwatsonmac_policy_register(struct mac_policy_conf *mpc)
360100979Srwatson{
361100979Srwatson	struct mac_policy_conf *tmpc;
362100979Srwatson	struct mac_policy_ops *ops;
363100979Srwatson	struct mac_policy_op_entry *mpe;
364100979Srwatson	int slot;
365100979Srwatson
366100979Srwatson	MALLOC(mpc->mpc_ops, struct mac_policy_ops *, sizeof(*ops), M_MACOPVEC,
367100979Srwatson	    M_WAITOK | M_ZERO);
368100979Srwatson	for (mpe = mpc->mpc_entries; mpe->mpe_constant != MAC_OP_LAST; mpe++) {
369100979Srwatson		switch (mpe->mpe_constant) {
370100979Srwatson		case MAC_OP_LAST:
371100979Srwatson			/*
372100979Srwatson			 * Doesn't actually happen, but this allows checking
373100979Srwatson			 * that all enumerated values are handled.
374100979Srwatson			 */
375100979Srwatson			break;
376100979Srwatson		case MAC_DESTROY:
377100979Srwatson			mpc->mpc_ops->mpo_destroy =
378100979Srwatson			    mpe->mpe_function;
379100979Srwatson			break;
380100979Srwatson		case MAC_INIT:
381100979Srwatson			mpc->mpc_ops->mpo_init =
382100979Srwatson			    mpe->mpe_function;
383100979Srwatson			break;
384102123Srwatson		case MAC_SYSCALL:
385102123Srwatson			mpc->mpc_ops->mpo_syscall =
386102123Srwatson			    mpe->mpe_function;
387102123Srwatson			break;
388100979Srwatson		case MAC_INIT_BPFDESC:
389100979Srwatson			mpc->mpc_ops->mpo_init_bpfdesc =
390100979Srwatson			    mpe->mpe_function;
391100979Srwatson			break;
392100979Srwatson		case MAC_INIT_CRED:
393100979Srwatson			mpc->mpc_ops->mpo_init_cred =
394100979Srwatson			    mpe->mpe_function;
395100979Srwatson			break;
396100979Srwatson		case MAC_INIT_DEVFSDIRENT:
397100979Srwatson			mpc->mpc_ops->mpo_init_devfsdirent =
398100979Srwatson			    mpe->mpe_function;
399100979Srwatson			break;
400100979Srwatson		case MAC_INIT_IFNET:
401100979Srwatson			mpc->mpc_ops->mpo_init_ifnet =
402100979Srwatson			    mpe->mpe_function;
403100979Srwatson			break;
404100979Srwatson		case MAC_INIT_IPQ:
405100979Srwatson			mpc->mpc_ops->mpo_init_ipq =
406100979Srwatson			    mpe->mpe_function;
407100979Srwatson			break;
408100979Srwatson		case MAC_INIT_MBUF:
409100979Srwatson			mpc->mpc_ops->mpo_init_mbuf =
410100979Srwatson			    mpe->mpe_function;
411100979Srwatson			break;
412100979Srwatson		case MAC_INIT_MOUNT:
413100979Srwatson			mpc->mpc_ops->mpo_init_mount =
414100979Srwatson			    mpe->mpe_function;
415100979Srwatson			break;
416100979Srwatson		case MAC_INIT_PIPE:
417100979Srwatson			mpc->mpc_ops->mpo_init_pipe =
418100979Srwatson			    mpe->mpe_function;
419100979Srwatson			break;
420100979Srwatson		case MAC_INIT_SOCKET:
421100979Srwatson			mpc->mpc_ops->mpo_init_socket =
422100979Srwatson			    mpe->mpe_function;
423100979Srwatson			break;
424100979Srwatson		case MAC_INIT_TEMP:
425100979Srwatson			mpc->mpc_ops->mpo_init_temp =
426100979Srwatson			    mpe->mpe_function;
427100979Srwatson			break;
428100979Srwatson		case MAC_INIT_VNODE:
429100979Srwatson			mpc->mpc_ops->mpo_init_vnode =
430100979Srwatson			    mpe->mpe_function;
431100979Srwatson			break;
432100979Srwatson		case MAC_DESTROY_BPFDESC:
433100979Srwatson			mpc->mpc_ops->mpo_destroy_bpfdesc =
434100979Srwatson			    mpe->mpe_function;
435100979Srwatson			break;
436100979Srwatson		case MAC_DESTROY_CRED:
437100979Srwatson			mpc->mpc_ops->mpo_destroy_cred =
438100979Srwatson			    mpe->mpe_function;
439100979Srwatson			break;
440100979Srwatson		case MAC_DESTROY_DEVFSDIRENT:
441100979Srwatson			mpc->mpc_ops->mpo_destroy_devfsdirent =
442100979Srwatson			    mpe->mpe_function;
443100979Srwatson			break;
444100979Srwatson		case MAC_DESTROY_IFNET:
445100979Srwatson			mpc->mpc_ops->mpo_destroy_ifnet =
446100979Srwatson			    mpe->mpe_function;
447100979Srwatson			break;
448100979Srwatson		case MAC_DESTROY_IPQ:
449100979Srwatson			mpc->mpc_ops->mpo_destroy_ipq =
450100979Srwatson			    mpe->mpe_function;
451100979Srwatson			break;
452100979Srwatson		case MAC_DESTROY_MBUF:
453100979Srwatson			mpc->mpc_ops->mpo_destroy_mbuf =
454100979Srwatson			    mpe->mpe_function;
455100979Srwatson			break;
456100979Srwatson		case MAC_DESTROY_MOUNT:
457100979Srwatson			mpc->mpc_ops->mpo_destroy_mount =
458100979Srwatson			    mpe->mpe_function;
459100979Srwatson			break;
460100979Srwatson		case MAC_DESTROY_PIPE:
461100979Srwatson			mpc->mpc_ops->mpo_destroy_pipe =
462100979Srwatson			    mpe->mpe_function;
463100979Srwatson			break;
464100979Srwatson		case MAC_DESTROY_SOCKET:
465100979Srwatson			mpc->mpc_ops->mpo_destroy_socket =
466100979Srwatson			    mpe->mpe_function;
467100979Srwatson			break;
468100979Srwatson		case MAC_DESTROY_TEMP:
469100979Srwatson			mpc->mpc_ops->mpo_destroy_temp =
470100979Srwatson			    mpe->mpe_function;
471100979Srwatson			break;
472100979Srwatson		case MAC_DESTROY_VNODE:
473100979Srwatson			mpc->mpc_ops->mpo_destroy_vnode =
474100979Srwatson			    mpe->mpe_function;
475100979Srwatson			break;
476100979Srwatson		case MAC_EXTERNALIZE:
477100979Srwatson			mpc->mpc_ops->mpo_externalize =
478100979Srwatson			    mpe->mpe_function;
479100979Srwatson			break;
480100979Srwatson		case MAC_INTERNALIZE:
481100979Srwatson			mpc->mpc_ops->mpo_internalize =
482100979Srwatson			    mpe->mpe_function;
483100979Srwatson			break;
484100979Srwatson		case MAC_CREATE_DEVFS_DEVICE:
485100979Srwatson			mpc->mpc_ops->mpo_create_devfs_device =
486100979Srwatson			    mpe->mpe_function;
487100979Srwatson			break;
488100979Srwatson		case MAC_CREATE_DEVFS_DIRECTORY:
489100979Srwatson			mpc->mpc_ops->mpo_create_devfs_directory =
490100979Srwatson			    mpe->mpe_function;
491100979Srwatson			break;
492100979Srwatson		case MAC_CREATE_DEVFS_VNODE:
493100979Srwatson			mpc->mpc_ops->mpo_create_devfs_vnode =
494100979Srwatson			    mpe->mpe_function;
495100979Srwatson			break;
496100979Srwatson		case MAC_STDCREATEVNODE_EA:
497100979Srwatson			mpc->mpc_ops->mpo_stdcreatevnode_ea =
498100979Srwatson			    mpe->mpe_function;
499100979Srwatson			break;
500100979Srwatson		case MAC_CREATE_VNODE:
501100979Srwatson			mpc->mpc_ops->mpo_create_vnode =
502100979Srwatson			    mpe->mpe_function;
503100979Srwatson			break;
504100979Srwatson		case MAC_CREATE_MOUNT:
505100979Srwatson			mpc->mpc_ops->mpo_create_mount =
506100979Srwatson			    mpe->mpe_function;
507100979Srwatson			break;
508100979Srwatson		case MAC_CREATE_ROOT_MOUNT:
509100979Srwatson			mpc->mpc_ops->mpo_create_root_mount =
510100979Srwatson			    mpe->mpe_function;
511100979Srwatson			break;
512100979Srwatson		case MAC_RELABEL_VNODE:
513100979Srwatson			mpc->mpc_ops->mpo_relabel_vnode =
514100979Srwatson			    mpe->mpe_function;
515100979Srwatson			break;
516100979Srwatson		case MAC_UPDATE_DEVFSDIRENT:
517100979Srwatson			mpc->mpc_ops->mpo_update_devfsdirent =
518100979Srwatson			    mpe->mpe_function;
519100979Srwatson			break;
520100979Srwatson		case MAC_UPDATE_PROCFSVNODE:
521100979Srwatson			mpc->mpc_ops->mpo_update_procfsvnode =
522100979Srwatson			    mpe->mpe_function;
523100979Srwatson			break;
524100979Srwatson		case MAC_UPDATE_VNODE_FROM_EXTATTR:
525100979Srwatson			mpc->mpc_ops->mpo_update_vnode_from_extattr =
526100979Srwatson			    mpe->mpe_function;
527100979Srwatson			break;
528100979Srwatson		case MAC_UPDATE_VNODE_FROM_EXTERNALIZED:
529100979Srwatson			mpc->mpc_ops->mpo_update_vnode_from_externalized =
530100979Srwatson			    mpe->mpe_function;
531100979Srwatson			break;
532100979Srwatson		case MAC_UPDATE_VNODE_FROM_MOUNT:
533100979Srwatson			mpc->mpc_ops->mpo_update_vnode_from_mount =
534100979Srwatson			    mpe->mpe_function;
535100979Srwatson			break;
536100979Srwatson		case MAC_CREATE_MBUF_FROM_SOCKET:
537100979Srwatson			mpc->mpc_ops->mpo_create_mbuf_from_socket =
538100979Srwatson			    mpe->mpe_function;
539100979Srwatson			break;
540100979Srwatson		case MAC_CREATE_PIPE:
541100979Srwatson			mpc->mpc_ops->mpo_create_pipe =
542100979Srwatson			    mpe->mpe_function;
543100979Srwatson			break;
544100979Srwatson		case MAC_CREATE_SOCKET:
545100979Srwatson			mpc->mpc_ops->mpo_create_socket =
546100979Srwatson			    mpe->mpe_function;
547100979Srwatson			break;
548100979Srwatson		case MAC_CREATE_SOCKET_FROM_SOCKET:
549100979Srwatson			mpc->mpc_ops->mpo_create_socket_from_socket =
550100979Srwatson			    mpe->mpe_function;
551100979Srwatson			break;
552100979Srwatson		case MAC_RELABEL_PIPE:
553100979Srwatson			mpc->mpc_ops->mpo_relabel_pipe =
554100979Srwatson			    mpe->mpe_function;
555100979Srwatson			break;
556100979Srwatson		case MAC_RELABEL_SOCKET:
557100979Srwatson			mpc->mpc_ops->mpo_relabel_socket =
558100979Srwatson			    mpe->mpe_function;
559100979Srwatson			break;
560100979Srwatson		case MAC_SET_SOCKET_PEER_FROM_MBUF:
561100979Srwatson			mpc->mpc_ops->mpo_set_socket_peer_from_mbuf =
562100979Srwatson			    mpe->mpe_function;
563100979Srwatson			break;
564100979Srwatson		case MAC_SET_SOCKET_PEER_FROM_SOCKET:
565100979Srwatson			mpc->mpc_ops->mpo_set_socket_peer_from_socket =
566100979Srwatson			    mpe->mpe_function;
567100979Srwatson			break;
568100979Srwatson		case MAC_CREATE_BPFDESC:
569100979Srwatson			mpc->mpc_ops->mpo_create_bpfdesc =
570100979Srwatson			    mpe->mpe_function;
571100979Srwatson			break;
572100979Srwatson		case MAC_CREATE_DATAGRAM_FROM_IPQ:
573100979Srwatson			mpc->mpc_ops->mpo_create_datagram_from_ipq =
574100979Srwatson			    mpe->mpe_function;
575100979Srwatson			break;
576100979Srwatson		case MAC_CREATE_FRAGMENT:
577100979Srwatson			mpc->mpc_ops->mpo_create_fragment =
578100979Srwatson			    mpe->mpe_function;
579100979Srwatson			break;
580100979Srwatson		case MAC_CREATE_IFNET:
581100979Srwatson			mpc->mpc_ops->mpo_create_ifnet =
582100979Srwatson			    mpe->mpe_function;
583100979Srwatson			break;
584100979Srwatson		case MAC_CREATE_IPQ:
585100979Srwatson			mpc->mpc_ops->mpo_create_ipq =
586100979Srwatson			    mpe->mpe_function;
587100979Srwatson			break;
588100979Srwatson		case MAC_CREATE_MBUF_FROM_MBUF:
589100979Srwatson			mpc->mpc_ops->mpo_create_mbuf_from_mbuf =
590100979Srwatson			    mpe->mpe_function;
591100979Srwatson			break;
592100979Srwatson		case MAC_CREATE_MBUF_LINKLAYER:
593100979Srwatson			mpc->mpc_ops->mpo_create_mbuf_linklayer =
594100979Srwatson			    mpe->mpe_function;
595100979Srwatson			break;
596100979Srwatson		case MAC_CREATE_MBUF_FROM_BPFDESC:
597100979Srwatson			mpc->mpc_ops->mpo_create_mbuf_from_bpfdesc =
598100979Srwatson			    mpe->mpe_function;
599100979Srwatson			break;
600100979Srwatson		case MAC_CREATE_MBUF_FROM_IFNET:
601100979Srwatson			mpc->mpc_ops->mpo_create_mbuf_from_ifnet =
602100979Srwatson			    mpe->mpe_function;
603100979Srwatson			break;
604100979Srwatson		case MAC_CREATE_MBUF_MULTICAST_ENCAP:
605100979Srwatson			mpc->mpc_ops->mpo_create_mbuf_multicast_encap =
606100979Srwatson			    mpe->mpe_function;
607100979Srwatson			break;
608100979Srwatson		case MAC_CREATE_MBUF_NETLAYER:
609100979Srwatson			mpc->mpc_ops->mpo_create_mbuf_netlayer =
610100979Srwatson			    mpe->mpe_function;
611100979Srwatson			break;
612100979Srwatson		case MAC_FRAGMENT_MATCH:
613100979Srwatson			mpc->mpc_ops->mpo_fragment_match =
614100979Srwatson			    mpe->mpe_function;
615100979Srwatson			break;
616100979Srwatson		case MAC_RELABEL_IFNET:
617100979Srwatson			mpc->mpc_ops->mpo_relabel_ifnet =
618100979Srwatson			    mpe->mpe_function;
619100979Srwatson			break;
620100979Srwatson		case MAC_UPDATE_IPQ:
621100979Srwatson			mpc->mpc_ops->mpo_update_ipq =
622100979Srwatson			    mpe->mpe_function;
623100979Srwatson			break;
624100979Srwatson		case MAC_CREATE_CRED:
625100979Srwatson			mpc->mpc_ops->mpo_create_cred =
626100979Srwatson			    mpe->mpe_function;
627100979Srwatson			break;
628100979Srwatson		case MAC_EXECVE_TRANSITION:
629100979Srwatson			mpc->mpc_ops->mpo_execve_transition =
630100979Srwatson			    mpe->mpe_function;
631100979Srwatson			break;
632100979Srwatson		case MAC_EXECVE_WILL_TRANSITION:
633100979Srwatson			mpc->mpc_ops->mpo_execve_will_transition =
634100979Srwatson			    mpe->mpe_function;
635100979Srwatson			break;
636100979Srwatson		case MAC_CREATE_PROC0:
637100979Srwatson			mpc->mpc_ops->mpo_create_proc0 = mpe->mpe_function;
638100979Srwatson			break;
639100979Srwatson		case MAC_CREATE_PROC1:
640100979Srwatson			mpc->mpc_ops->mpo_create_proc1 = mpe->mpe_function;
641100979Srwatson			break;
642100979Srwatson		case MAC_RELABEL_CRED:
643100979Srwatson			mpc->mpc_ops->mpo_relabel_cred =
644100979Srwatson			    mpe->mpe_function;
645100979Srwatson			break;
646100979Srwatson		case MAC_CHECK_BPFDESC_RECEIVE:
647100979Srwatson			mpc->mpc_ops->mpo_check_bpfdesc_receive =
648100979Srwatson			    mpe->mpe_function;
649100979Srwatson			break;
650100979Srwatson		case MAC_CHECK_CRED_RELABEL:
651100979Srwatson			mpc->mpc_ops->mpo_check_cred_relabel =
652100979Srwatson			    mpe->mpe_function;
653100979Srwatson			break;
654100979Srwatson		case MAC_CHECK_CRED_VISIBLE:
655100979Srwatson			mpc->mpc_ops->mpo_check_cred_visible =
656100979Srwatson			    mpe->mpe_function;
657100979Srwatson			break;
658100979Srwatson		case MAC_CHECK_IFNET_RELABEL:
659100979Srwatson			mpc->mpc_ops->mpo_check_ifnet_relabel =
660100979Srwatson			    mpe->mpe_function;
661100979Srwatson			break;
662100979Srwatson		case MAC_CHECK_IFNET_TRANSMIT:
663100979Srwatson			mpc->mpc_ops->mpo_check_ifnet_transmit =
664100979Srwatson			    mpe->mpe_function;
665100979Srwatson			break;
666100979Srwatson		case MAC_CHECK_MOUNT_STAT:
667100979Srwatson			mpc->mpc_ops->mpo_check_mount_stat =
668100979Srwatson			    mpe->mpe_function;
669100979Srwatson			break;
670100979Srwatson		case MAC_CHECK_PIPE_IOCTL:
671100979Srwatson			mpc->mpc_ops->mpo_check_pipe_ioctl =
672100979Srwatson			    mpe->mpe_function;
673100979Srwatson			break;
674102115Srwatson		case MAC_CHECK_PIPE_POLL:
675102115Srwatson			mpc->mpc_ops->mpo_check_pipe_poll =
676100979Srwatson			    mpe->mpe_function;
677100979Srwatson			break;
678102115Srwatson		case MAC_CHECK_PIPE_READ:
679102115Srwatson			mpc->mpc_ops->mpo_check_pipe_read =
680102115Srwatson			    mpe->mpe_function;
681102115Srwatson			break;
682100979Srwatson		case MAC_CHECK_PIPE_RELABEL:
683100979Srwatson			mpc->mpc_ops->mpo_check_pipe_relabel =
684100979Srwatson			    mpe->mpe_function;
685100979Srwatson			break;
686102115Srwatson		case MAC_CHECK_PIPE_STAT:
687102115Srwatson			mpc->mpc_ops->mpo_check_pipe_stat =
688102115Srwatson			    mpe->mpe_function;
689102115Srwatson			break;
690102115Srwatson		case MAC_CHECK_PIPE_WRITE:
691102115Srwatson			mpc->mpc_ops->mpo_check_pipe_write =
692102115Srwatson			    mpe->mpe_function;
693102115Srwatson			break;
694100979Srwatson		case MAC_CHECK_PROC_DEBUG:
695100979Srwatson			mpc->mpc_ops->mpo_check_proc_debug =
696100979Srwatson			    mpe->mpe_function;
697100979Srwatson			break;
698100979Srwatson		case MAC_CHECK_PROC_SCHED:
699100979Srwatson			mpc->mpc_ops->mpo_check_proc_sched =
700100979Srwatson			    mpe->mpe_function;
701100979Srwatson			break;
702100979Srwatson		case MAC_CHECK_PROC_SIGNAL:
703100979Srwatson			mpc->mpc_ops->mpo_check_proc_signal =
704100979Srwatson			    mpe->mpe_function;
705100979Srwatson			break;
706100979Srwatson		case MAC_CHECK_SOCKET_BIND:
707100979Srwatson			mpc->mpc_ops->mpo_check_socket_bind =
708100979Srwatson			    mpe->mpe_function;
709100979Srwatson			break;
710100979Srwatson		case MAC_CHECK_SOCKET_CONNECT:
711100979Srwatson			mpc->mpc_ops->mpo_check_socket_connect =
712100979Srwatson			    mpe->mpe_function;
713100979Srwatson			break;
714101933Srwatson		case MAC_CHECK_SOCKET_DELIVER:
715101933Srwatson			mpc->mpc_ops->mpo_check_socket_deliver =
716101933Srwatson			    mpe->mpe_function;
717101933Srwatson			break;
718100979Srwatson		case MAC_CHECK_SOCKET_LISTEN:
719100979Srwatson			mpc->mpc_ops->mpo_check_socket_listen =
720100979Srwatson			    mpe->mpe_function;
721100979Srwatson			break;
722100979Srwatson		case MAC_CHECK_SOCKET_RELABEL:
723100979Srwatson			mpc->mpc_ops->mpo_check_socket_relabel =
724100979Srwatson			    mpe->mpe_function;
725100979Srwatson			break;
726100979Srwatson		case MAC_CHECK_SOCKET_VISIBLE:
727100979Srwatson			mpc->mpc_ops->mpo_check_socket_visible =
728100979Srwatson			    mpe->mpe_function;
729100979Srwatson			break;
730100979Srwatson		case MAC_CHECK_VNODE_ACCESS:
731100979Srwatson			mpc->mpc_ops->mpo_check_vnode_access =
732100979Srwatson			    mpe->mpe_function;
733100979Srwatson			break;
734100979Srwatson		case MAC_CHECK_VNODE_CHDIR:
735100979Srwatson			mpc->mpc_ops->mpo_check_vnode_chdir =
736100979Srwatson			    mpe->mpe_function;
737100979Srwatson			break;
738100979Srwatson		case MAC_CHECK_VNODE_CHROOT:
739100979Srwatson			mpc->mpc_ops->mpo_check_vnode_chroot =
740100979Srwatson			    mpe->mpe_function;
741100979Srwatson			break;
742100979Srwatson		case MAC_CHECK_VNODE_CREATE:
743100979Srwatson			mpc->mpc_ops->mpo_check_vnode_create =
744100979Srwatson			    mpe->mpe_function;
745100979Srwatson			break;
746100979Srwatson		case MAC_CHECK_VNODE_DELETE:
747100979Srwatson			mpc->mpc_ops->mpo_check_vnode_delete =
748100979Srwatson			    mpe->mpe_function;
749100979Srwatson			break;
750100979Srwatson		case MAC_CHECK_VNODE_DELETEACL:
751100979Srwatson			mpc->mpc_ops->mpo_check_vnode_deleteacl =
752100979Srwatson			    mpe->mpe_function;
753100979Srwatson			break;
754100979Srwatson		case MAC_CHECK_VNODE_EXEC:
755100979Srwatson			mpc->mpc_ops->mpo_check_vnode_exec =
756100979Srwatson			    mpe->mpe_function;
757100979Srwatson			break;
758100979Srwatson		case MAC_CHECK_VNODE_GETACL:
759100979Srwatson			mpc->mpc_ops->mpo_check_vnode_getacl =
760100979Srwatson			    mpe->mpe_function;
761100979Srwatson			break;
762100979Srwatson		case MAC_CHECK_VNODE_GETEXTATTR:
763100979Srwatson			mpc->mpc_ops->mpo_check_vnode_getextattr =
764100979Srwatson			    mpe->mpe_function;
765100979Srwatson			break;
766100979Srwatson		case MAC_CHECK_VNODE_LOOKUP:
767100979Srwatson			mpc->mpc_ops->mpo_check_vnode_lookup =
768100979Srwatson			    mpe->mpe_function;
769100979Srwatson			break;
770100979Srwatson		case MAC_CHECK_VNODE_MMAP_PERMS:
771100979Srwatson			mpc->mpc_ops->mpo_check_vnode_mmap_perms =
772100979Srwatson			    mpe->mpe_function;
773100979Srwatson			break;
774100979Srwatson		case MAC_CHECK_VNODE_OPEN:
775100979Srwatson			mpc->mpc_ops->mpo_check_vnode_open =
776100979Srwatson			    mpe->mpe_function;
777100979Srwatson			break;
778102112Srwatson		case MAC_CHECK_VNODE_POLL:
779102112Srwatson			mpc->mpc_ops->mpo_check_vnode_poll =
780102112Srwatson			    mpe->mpe_function;
781102112Srwatson			break;
782102112Srwatson		case MAC_CHECK_VNODE_READ:
783102112Srwatson			mpc->mpc_ops->mpo_check_vnode_read =
784102112Srwatson			    mpe->mpe_function;
785102112Srwatson			break;
786100979Srwatson		case MAC_CHECK_VNODE_READDIR:
787100979Srwatson			mpc->mpc_ops->mpo_check_vnode_readdir =
788100979Srwatson			    mpe->mpe_function;
789100979Srwatson			break;
790100979Srwatson		case MAC_CHECK_VNODE_READLINK:
791100979Srwatson			mpc->mpc_ops->mpo_check_vnode_readlink =
792100979Srwatson			    mpe->mpe_function;
793100979Srwatson			break;
794100979Srwatson		case MAC_CHECK_VNODE_RELABEL:
795100979Srwatson			mpc->mpc_ops->mpo_check_vnode_relabel =
796100979Srwatson			    mpe->mpe_function;
797100979Srwatson			break;
798100979Srwatson		case MAC_CHECK_VNODE_RENAME_FROM:
799100979Srwatson			mpc->mpc_ops->mpo_check_vnode_rename_from =
800100979Srwatson			    mpe->mpe_function;
801100979Srwatson			break;
802100979Srwatson		case MAC_CHECK_VNODE_RENAME_TO:
803100979Srwatson			mpc->mpc_ops->mpo_check_vnode_rename_to =
804100979Srwatson			    mpe->mpe_function;
805100979Srwatson			break;
806100979Srwatson		case MAC_CHECK_VNODE_REVOKE:
807100979Srwatson			mpc->mpc_ops->mpo_check_vnode_revoke =
808100979Srwatson			    mpe->mpe_function;
809100979Srwatson			break;
810100979Srwatson		case MAC_CHECK_VNODE_SETACL:
811100979Srwatson			mpc->mpc_ops->mpo_check_vnode_setacl =
812100979Srwatson			    mpe->mpe_function;
813100979Srwatson			break;
814100979Srwatson		case MAC_CHECK_VNODE_SETEXTATTR:
815100979Srwatson			mpc->mpc_ops->mpo_check_vnode_setextattr =
816100979Srwatson			    mpe->mpe_function;
817100979Srwatson			break;
818100979Srwatson		case MAC_CHECK_VNODE_SETFLAGS:
819100979Srwatson			mpc->mpc_ops->mpo_check_vnode_setflags =
820100979Srwatson			    mpe->mpe_function;
821100979Srwatson			break;
822100979Srwatson		case MAC_CHECK_VNODE_SETMODE:
823100979Srwatson			mpc->mpc_ops->mpo_check_vnode_setmode =
824100979Srwatson			    mpe->mpe_function;
825100979Srwatson			break;
826100979Srwatson		case MAC_CHECK_VNODE_SETOWNER:
827100979Srwatson			mpc->mpc_ops->mpo_check_vnode_setowner =
828100979Srwatson			    mpe->mpe_function;
829100979Srwatson			break;
830100979Srwatson		case MAC_CHECK_VNODE_SETUTIMES:
831100979Srwatson			mpc->mpc_ops->mpo_check_vnode_setutimes =
832100979Srwatson			    mpe->mpe_function;
833100979Srwatson			break;
834100979Srwatson		case MAC_CHECK_VNODE_STAT:
835100979Srwatson			mpc->mpc_ops->mpo_check_vnode_stat =
836100979Srwatson			    mpe->mpe_function;
837100979Srwatson			break;
838102112Srwatson		case MAC_CHECK_VNODE_WRITE:
839102112Srwatson			mpc->mpc_ops->mpo_check_vnode_write =
840102112Srwatson			    mpe->mpe_function;
841102112Srwatson			break;
842100979Srwatson/*
843100979Srwatson		default:
844100979Srwatson			printf("MAC policy `%s': unknown operation %d\n",
845100979Srwatson			    mpc->mpc_name, mpe->mpe_constant);
846100979Srwatson			return (EINVAL);
847100979Srwatson*/
848100979Srwatson		}
849100979Srwatson	}
850100979Srwatson	MAC_POLICY_LIST_LOCK();
851100979Srwatson	if (mac_policy_list_busy > 0) {
852100979Srwatson		MAC_POLICY_LIST_UNLOCK();
853100979Srwatson		FREE(mpc->mpc_ops, M_MACOPVEC);
854100979Srwatson		mpc->mpc_ops = NULL;
855100979Srwatson		return (EBUSY);
856100979Srwatson	}
857100979Srwatson	LIST_FOREACH(tmpc, &mac_policy_list, mpc_list) {
858100979Srwatson		if (strcmp(tmpc->mpc_name, mpc->mpc_name) == 0) {
859100979Srwatson			MAC_POLICY_LIST_UNLOCK();
860100979Srwatson			FREE(mpc->mpc_ops, M_MACOPVEC);
861100979Srwatson			mpc->mpc_ops = NULL;
862100979Srwatson			return (EEXIST);
863100979Srwatson		}
864100979Srwatson	}
865100979Srwatson	if (mpc->mpc_field_off != NULL) {
866100979Srwatson		slot = ffs(mac_policy_offsets_free);
867100979Srwatson		if (slot == 0) {
868100979Srwatson			MAC_POLICY_LIST_UNLOCK();
869100979Srwatson			FREE(mpc->mpc_ops, M_MACOPVEC);
870100979Srwatson			mpc->mpc_ops = NULL;
871100979Srwatson			return (ENOMEM);
872100979Srwatson		}
873100979Srwatson		slot--;
874100979Srwatson		mac_policy_offsets_free &= ~(1 << slot);
875100979Srwatson		*mpc->mpc_field_off = slot;
876100979Srwatson	}
877100979Srwatson	mpc->mpc_runtime_flags |= MPC_RUNTIME_FLAG_REGISTERED;
878100979Srwatson	LIST_INSERT_HEAD(&mac_policy_list, mpc, mpc_list);
879100979Srwatson
880100979Srwatson	/* Per-policy initialization. */
881100979Srwatson	if (mpc->mpc_ops->mpo_init != NULL)
882100979Srwatson		(*(mpc->mpc_ops->mpo_init))(mpc);
883100979Srwatson	MAC_POLICY_LIST_UNLOCK();
884100979Srwatson
885100979Srwatson	printf("Security policy loaded: %s (%s)\n", mpc->mpc_fullname,
886100979Srwatson	    mpc->mpc_name);
887100979Srwatson
888100979Srwatson	return (0);
889100979Srwatson}
890100979Srwatson
891100979Srwatsonstatic int
892100979Srwatsonmac_policy_unregister(struct mac_policy_conf *mpc)
893100979Srwatson{
894100979Srwatson
895100979Srwatson#if 0
896100979Srwatson	/*
897100979Srwatson	 * Don't allow unloading modules with private data.
898100979Srwatson	 */
899100979Srwatson	if (mpc->mpc_field_off != NULL)
900100979Srwatson		return (EBUSY);
901100979Srwatson#endif
902100979Srwatson	if ((mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK) == 0)
903100979Srwatson		return (EBUSY);
904100979Srwatson	MAC_POLICY_LIST_LOCK();
905100979Srwatson	if (mac_policy_list_busy > 0) {
906100979Srwatson		MAC_POLICY_LIST_UNLOCK();
907100979Srwatson		return (EBUSY);
908100979Srwatson	}
909100979Srwatson	if (mpc->mpc_ops->mpo_destroy != NULL)
910100979Srwatson		(*(mpc->mpc_ops->mpo_destroy))(mpc);
911100979Srwatson
912100979Srwatson	LIST_REMOVE(mpc, mpc_list);
913100979Srwatson	MAC_POLICY_LIST_UNLOCK();
914100979Srwatson
915100979Srwatson	FREE(mpc->mpc_ops, M_MACOPVEC);
916100979Srwatson	mpc->mpc_ops = NULL;
917100979Srwatson
918100979Srwatson	printf("Security policy unload: %s (%s)\n", mpc->mpc_fullname,
919100979Srwatson	    mpc->mpc_name);
920100979Srwatson
921100979Srwatson	return (0);
922100979Srwatson}
923100979Srwatson
924100979Srwatson/*
925100979Srwatson * Define an error value precedence, and given two arguments, selects the
926100979Srwatson * value with the higher precedence.
927100979Srwatson */
928100979Srwatsonstatic int
929100979Srwatsonerror_select(int error1, int error2)
930100979Srwatson{
931100979Srwatson
932100979Srwatson	/* Certain decision-making errors take top priority. */
933100979Srwatson	if (error1 == EDEADLK || error2 == EDEADLK)
934100979Srwatson		return (EDEADLK);
935100979Srwatson
936100979Srwatson	/* Invalid arguments should be reported where possible. */
937100979Srwatson	if (error1 == EINVAL || error2 == EINVAL)
938100979Srwatson		return (EINVAL);
939100979Srwatson
940100979Srwatson	/* Precedence goes to "visibility", with both process and file. */
941100979Srwatson	if (error1 == ESRCH || error2 == ESRCH)
942100979Srwatson		return (ESRCH);
943100979Srwatson
944100979Srwatson	if (error1 == ENOENT || error2 == ENOENT)
945100979Srwatson		return (ENOENT);
946100979Srwatson
947100979Srwatson	/* Precedence goes to DAC/MAC protections. */
948100979Srwatson	if (error1 == EACCES || error2 == EACCES)
949100979Srwatson		return (EACCES);
950100979Srwatson
951100979Srwatson	/* Precedence goes to privilege. */
952100979Srwatson	if (error1 == EPERM || error2 == EPERM)
953100979Srwatson		return (EPERM);
954100979Srwatson
955100979Srwatson	/* Precedence goes to error over success; otherwise, arbitrary. */
956100979Srwatson	if (error1 != 0)
957100979Srwatson		return (error1);
958100979Srwatson	return (error2);
959100979Srwatson}
960100979Srwatson
961100979Srwatsonvoid
962100979Srwatsonmac_update_devfsdirent(struct devfs_dirent *de, struct vnode *vp)
963100979Srwatson{
964100979Srwatson
965100979Srwatson	MAC_PERFORM(update_devfsdirent, de, &de->de_label, vp, &vp->v_label);
966100979Srwatson}
967100979Srwatson
968100979Srwatsonvoid
969100979Srwatsonmac_update_procfsvnode(struct vnode *vp, struct ucred *cred)
970100979Srwatson{
971100979Srwatson
972100979Srwatson	MAC_PERFORM(update_procfsvnode, vp, &vp->v_label, cred);
973100979Srwatson}
974100979Srwatson
975100979Srwatson/*
976100979Srwatson * Support callout for policies that manage their own externalization
977100979Srwatson * using extended attributes.
978100979Srwatson */
979100979Srwatsonstatic int
980100979Srwatsonmac_update_vnode_from_extattr(struct vnode *vp, struct mount *mp)
981100979Srwatson{
982100979Srwatson	int error;
983100979Srwatson
984100979Srwatson	MAC_CHECK(update_vnode_from_extattr, vp, &vp->v_label, mp,
985100979Srwatson	    &mp->mnt_fslabel);
986100979Srwatson
987100979Srwatson	return (error);
988100979Srwatson}
989100979Srwatson
990100979Srwatson/*
991100979Srwatson * Given an externalized mac label, internalize it and stamp it on a
992100979Srwatson * vnode.
993100979Srwatson */
994100979Srwatsonstatic int
995100979Srwatsonmac_update_vnode_from_externalized(struct vnode *vp, struct mac *extmac)
996100979Srwatson{
997100979Srwatson	int error;
998100979Srwatson
999100979Srwatson	MAC_CHECK(update_vnode_from_externalized, vp, &vp->v_label, extmac);
1000100979Srwatson
1001100979Srwatson	return (error);
1002100979Srwatson}
1003100979Srwatson
1004100979Srwatson/*
1005100979Srwatson * Call out to individual policies to update the label in a vnode from
1006100979Srwatson * the mountpoint.
1007100979Srwatson */
1008100979Srwatsonvoid
1009100979Srwatsonmac_update_vnode_from_mount(struct vnode *vp, struct mount *mp)
1010100979Srwatson{
1011100979Srwatson
1012100979Srwatson	MAC_PERFORM(update_vnode_from_mount, vp, &vp->v_label, mp,
1013100979Srwatson	    &mp->mnt_fslabel);
1014100979Srwatson
1015101308Sjeff	ASSERT_VOP_LOCKED(vp, "mac_update_vnode_from_mount");
1016100979Srwatson	if (mac_cache_fslabel_in_vnode)
1017101308Sjeff		vp->v_vflag |= VV_CACHEDLABEL;
1018100979Srwatson}
1019100979Srwatson
1020100979Srwatson/*
1021100979Srwatson * Implementation of VOP_REFRESHLABEL() that relies on extended attributes
1022100979Srwatson * to store label data.  Can be referenced by filesystems supporting
1023100979Srwatson * extended attributes.
1024100979Srwatson */
1025100979Srwatsonint
1026100979Srwatsonvop_stdrefreshlabel_ea(struct vop_refreshlabel_args *ap)
1027100979Srwatson{
1028100979Srwatson	struct vnode *vp = ap->a_vp;
1029100979Srwatson	struct mac extmac;
1030100979Srwatson	int buflen, error;
1031100979Srwatson
1032100979Srwatson	ASSERT_VOP_LOCKED(vp, "vop_stdrefreshlabel_ea");
1033100979Srwatson
1034100979Srwatson	/*
1035100979Srwatson	 * Call out to external policies first.  Order doesn't really
1036100979Srwatson	 * matter, as long as failure of one assures failure of all.
1037100979Srwatson	 */
1038100979Srwatson	error = mac_update_vnode_from_extattr(vp, vp->v_mount);
1039100979Srwatson	if (error)
1040100979Srwatson		return (error);
1041100979Srwatson
1042100979Srwatson	buflen = sizeof(extmac);
1043100979Srwatson	error = vn_extattr_get(vp, IO_NODELOCKED,
1044100979Srwatson	    FREEBSD_MAC_EXTATTR_NAMESPACE, FREEBSD_MAC_EXTATTR_NAME, &buflen,
1045100979Srwatson	    (char *)&extmac, curthread);
1046100979Srwatson	switch (error) {
1047100979Srwatson	case 0:
1048100979Srwatson		/* Got it */
1049100979Srwatson		break;
1050100979Srwatson
1051100979Srwatson	case ENOATTR:
1052100979Srwatson		/*
1053100979Srwatson		 * Use the label from the mount point.
1054100979Srwatson		 */
1055100979Srwatson		mac_update_vnode_from_mount(vp, vp->v_mount);
1056100979Srwatson		return (0);
1057100979Srwatson
1058100979Srwatson	case EOPNOTSUPP:
1059100979Srwatson	default:
1060100979Srwatson		/* Fail horribly. */
1061100979Srwatson		return (error);
1062100979Srwatson	}
1063100979Srwatson
1064100979Srwatson	if (buflen != sizeof(extmac))
1065100979Srwatson		error = EPERM;		/* Fail very closed. */
1066100979Srwatson	if (error == 0)
1067100979Srwatson		error = mac_update_vnode_from_externalized(vp, &extmac);
1068100979Srwatson	if (error == 0)
1069101308Sjeff		vp->v_vflag |= VV_CACHEDLABEL;
1070100979Srwatson	else {
1071100979Srwatson		struct vattr va;
1072100979Srwatson
1073100979Srwatson		printf("Corrupted label on %s",
1074100979Srwatson		    vp->v_mount->mnt_stat.f_mntonname);
1075100979Srwatson		if (VOP_GETATTR(vp, &va, curthread->td_ucred, curthread) == 0)
1076100979Srwatson			printf(" inum %ld", va.va_fileid);
1077100979Srwatson		if (mac_debug_label_fallback) {
1078100979Srwatson			printf(", falling back.\n");
1079100979Srwatson			mac_update_vnode_from_mount(vp, vp->v_mount);
1080100979Srwatson			error = 0;
1081100979Srwatson		} else {
1082100979Srwatson			printf(".\n");
1083100979Srwatson			error = EPERM;
1084100979Srwatson		}
1085100979Srwatson	}
1086100979Srwatson
1087100979Srwatson	return (error);
1088100979Srwatson}
1089100979Srwatson
1090100979Srwatson/*
1091100979Srwatson * Make sure the vnode label is up-to-date.  If EOPNOTSUPP, then we handle
1092100979Srwatson * the labeling activity outselves.  Filesystems should be careful not
1093100979Srwatson * to change their minds regarding whether they support vop_refreshlabel()
1094100979Srwatson * for a vnode or not.  Don't cache the vnode here, allow the file
1095100979Srwatson * system code to determine if it's safe to cache.  If we update from
1096100979Srwatson * the mount, don't cache since a change to the mount label should affect
1097100979Srwatson * all vnodes.
1098100979Srwatson */
1099100979Srwatsonstatic int
1100100979Srwatsonvn_refreshlabel(struct vnode *vp, struct ucred *cred)
1101100979Srwatson{
1102100979Srwatson	int error;
1103100979Srwatson
1104100979Srwatson	ASSERT_VOP_LOCKED(vp, "vn_refreshlabel");
1105100979Srwatson
1106100979Srwatson	if (vp->v_mount == NULL) {
1107100979Srwatson/*
1108100979Srwatson		Eventually, we probably want to special-case refreshing
1109100979Srwatson		of deadfs vnodes, and if there's a lock-free race somewhere,
1110100979Srwatson		that case might be handled here.
1111100979Srwatson
1112100979Srwatson		mac_update_vnode_deadfs(vp);
1113100979Srwatson		return (0);
1114100979Srwatson */
1115100979Srwatson		/* printf("vn_refreshlabel: null v_mount\n"); */
1116100979Srwatson		if (vp->v_tag != VT_NON)
1117100979Srwatson			printf(
1118100979Srwatson			    "vn_refreshlabel: null v_mount with non-VT_NON\n");
1119100979Srwatson		return (EBADF);
1120100979Srwatson	}
1121100979Srwatson
1122101308Sjeff	if (vp->v_vflag & VV_CACHEDLABEL) {
1123100979Srwatson		mac_vnode_label_cache_hits++;
1124100979Srwatson		return (0);
1125100979Srwatson	} else
1126100979Srwatson		mac_vnode_label_cache_misses++;
1127100979Srwatson
1128100979Srwatson	if ((vp->v_mount->mnt_flag & MNT_MULTILABEL) == 0) {
1129100979Srwatson		mac_update_vnode_from_mount(vp, vp->v_mount);
1130100979Srwatson		return (0);
1131100979Srwatson	}
1132100979Srwatson
1133100979Srwatson	error = VOP_REFRESHLABEL(vp, cred, curthread);
1134100979Srwatson	switch (error) {
1135100979Srwatson	case EOPNOTSUPP:
1136100979Srwatson		/*
1137100979Srwatson		 * If labels are not supported on this vnode, fall back to
1138100979Srwatson		 * the label in the mount and propagate it to the vnode.
1139100979Srwatson		 * There should probably be some sort of policy/flag/decision
1140100979Srwatson		 * about doing this.
1141100979Srwatson		 */
1142100979Srwatson		mac_update_vnode_from_mount(vp, vp->v_mount);
1143100979Srwatson		error = 0;
1144100979Srwatson	default:
1145100979Srwatson		return (error);
1146100979Srwatson	}
1147100979Srwatson}
1148100979Srwatson
1149100979Srwatson/*
1150100979Srwatson * Helper function for file systems using the vop_std*_ea() calls.  This
1151100979Srwatson * function must be called after EA service is available for the vnode,
1152100979Srwatson * but before it's hooked up to the namespace so that the node persists
1153100979Srwatson * if there's a crash, or before it can be accessed.  On successful
1154100979Srwatson * commit of the label to disk (etc), do cache the label.
1155100979Srwatson */
1156100979Srwatsonint
1157100979Srwatsonvop_stdcreatevnode_ea(struct vnode *dvp, struct vnode *tvp, struct ucred *cred)
1158100979Srwatson{
1159100979Srwatson	struct mac extmac;
1160100979Srwatson	int error;
1161100979Srwatson
1162101308Sjeff	ASSERT_VOP_LOCKED(tvp, "vop_stdcreatevnode_ea");
1163100979Srwatson	if ((dvp->v_mount->mnt_flag & MNT_MULTILABEL) == 0) {
1164100979Srwatson		mac_update_vnode_from_mount(tvp, tvp->v_mount);
1165100979Srwatson	} else {
1166100979Srwatson		error = vn_refreshlabel(dvp, cred);
1167100979Srwatson		if (error)
1168100979Srwatson			return (error);
1169100979Srwatson
1170100979Srwatson		/*
1171100979Srwatson		 * Stick the label in the vnode.  Then try to write to
1172100979Srwatson		 * disk.  If we fail, return a failure to abort the
1173100979Srwatson		 * create operation.  Really, this failure shouldn't
1174100979Srwatson		 * happen except in fairly unusual circumstances (out
1175100979Srwatson		 * of disk, etc).
1176100979Srwatson		 */
1177100979Srwatson		mac_create_vnode(cred, dvp, tvp);
1178100979Srwatson
1179100979Srwatson		error = mac_stdcreatevnode_ea(tvp);
1180100979Srwatson		if (error)
1181100979Srwatson			return (error);
1182100979Srwatson
1183100979Srwatson		/*
1184100979Srwatson		 * XXX: Eventually this will go away and all policies will
1185100979Srwatson		 * directly manage their extended attributes.
1186100979Srwatson		 */
1187100979Srwatson		error = mac_externalize(&tvp->v_label, &extmac);
1188100979Srwatson		if (error)
1189100979Srwatson			return (error);
1190100979Srwatson
1191100979Srwatson		error = vn_extattr_set(tvp, IO_NODELOCKED,
1192100979Srwatson		    FREEBSD_MAC_EXTATTR_NAMESPACE, FREEBSD_MAC_EXTATTR_NAME,
1193100979Srwatson		    sizeof(extmac), (char *)&extmac, curthread);
1194100979Srwatson		if (error == 0)
1195101308Sjeff			tvp->v_vflag |= VV_CACHEDLABEL;
1196100979Srwatson		else {
1197100979Srwatson#if 0
1198100979Srwatson			/*
1199100979Srwatson			 * In theory, we could have fall-back behavior here.
1200100979Srwatson			 * It would probably be incorrect.
1201100979Srwatson			 */
1202100979Srwatson#endif
1203100979Srwatson			return (error);
1204100979Srwatson		}
1205100979Srwatson	}
1206100979Srwatson
1207100979Srwatson	return (0);
1208100979Srwatson}
1209100979Srwatson
1210100979Srwatsonvoid
1211100979Srwatsonmac_execve_transition(struct ucred *old, struct ucred *new, struct vnode *vp)
1212100979Srwatson{
1213100979Srwatson	int error;
1214100979Srwatson
1215100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_execve_transition");
1216100979Srwatson
1217100979Srwatson	error = vn_refreshlabel(vp, old);
1218100979Srwatson	if (error) {
1219100979Srwatson		printf("mac_execve_transition: vn_refreshlabel returned %d\n",
1220100979Srwatson		    error);
1221100979Srwatson		printf("mac_execve_transition: using old vnode label\n");
1222100979Srwatson	}
1223100979Srwatson
1224100979Srwatson	MAC_PERFORM(execve_transition, old, new, vp, &vp->v_label);
1225100979Srwatson}
1226100979Srwatson
1227100979Srwatsonint
1228100979Srwatsonmac_execve_will_transition(struct ucred *old, struct vnode *vp)
1229100979Srwatson{
1230100979Srwatson	int error, result;
1231100979Srwatson
1232100979Srwatson	error = vn_refreshlabel(vp, old);
1233100979Srwatson	if (error)
1234100979Srwatson		return (error);
1235100979Srwatson
1236100979Srwatson	result = 0;
1237100979Srwatson	MAC_BOOLEAN(execve_will_transition, ||, old, vp, &vp->v_label);
1238100979Srwatson
1239100979Srwatson	return (result);
1240100979Srwatson}
1241100979Srwatson
1242100979Srwatsonstatic void
1243100979Srwatsonmac_init_label(struct label *label)
1244100979Srwatson{
1245100979Srwatson
1246100979Srwatson	bzero(label, sizeof(*label));
1247100979Srwatson	label->l_flags = MAC_FLAG_INITIALIZED;
1248100979Srwatson}
1249100979Srwatson
1250100979Srwatsonstatic void
1251100979Srwatsonmac_init_structmac(struct mac *mac)
1252100979Srwatson{
1253100979Srwatson
1254100979Srwatson	bzero(mac, sizeof(*mac));
1255100979Srwatson	mac->m_macflags = MAC_FLAG_INITIALIZED;
1256100979Srwatson}
1257100979Srwatson
1258100979Srwatsonstatic void
1259100979Srwatsonmac_destroy_label(struct label *label)
1260100979Srwatson{
1261100979Srwatson
1262100979Srwatson	KASSERT(label->l_flags & MAC_FLAG_INITIALIZED,
1263100979Srwatson	    ("destroying uninitialized label"));
1264100979Srwatson
1265100979Srwatson	bzero(label, sizeof(*label));
1266100979Srwatson	/* implicit: label->l_flags &= ~MAC_FLAG_INITIALIZED; */
1267100979Srwatson}
1268100979Srwatson
1269100979Srwatsonint
1270100979Srwatsonmac_init_mbuf(struct mbuf *m, int how)
1271100979Srwatson{
1272100979Srwatson	KASSERT(m->m_flags & M_PKTHDR, ("mac_init_mbuf on non-header mbuf"));
1273100979Srwatson
1274100979Srwatson	/* "how" is one of M_(TRY|DONT)WAIT */
1275100979Srwatson	mac_init_label(&m->m_pkthdr.label);
1276100979Srwatson	MAC_PERFORM(init_mbuf, m, how, &m->m_pkthdr.label);
1277101988Srwatson#ifdef MAC_DEBUG
1278100979Srwatson	atomic_add_int(&nmacmbufs, 1);
1279101988Srwatson#endif
1280100979Srwatson	return (0);
1281100979Srwatson}
1282100979Srwatson
1283100979Srwatsonvoid
1284100979Srwatsonmac_destroy_mbuf(struct mbuf *m)
1285100979Srwatson{
1286100979Srwatson
1287100979Srwatson	MAC_PERFORM(destroy_mbuf, m, &m->m_pkthdr.label);
1288100979Srwatson	mac_destroy_label(&m->m_pkthdr.label);
1289101988Srwatson#ifdef MAC_DEBUG
1290100979Srwatson	atomic_subtract_int(&nmacmbufs, 1);
1291101988Srwatson#endif
1292100979Srwatson}
1293100979Srwatson
1294100979Srwatsonvoid
1295100979Srwatsonmac_init_cred(struct ucred *cr)
1296100979Srwatson{
1297100979Srwatson
1298100979Srwatson	mac_init_label(&cr->cr_label);
1299100979Srwatson	MAC_PERFORM(init_cred, cr, &cr->cr_label);
1300101988Srwatson#ifdef MAC_DEBUG
1301100979Srwatson	atomic_add_int(&nmaccreds, 1);
1302101988Srwatson#endif
1303100979Srwatson}
1304100979Srwatson
1305100979Srwatsonvoid
1306100979Srwatsonmac_destroy_cred(struct ucred *cr)
1307100979Srwatson{
1308100979Srwatson
1309100979Srwatson	MAC_PERFORM(destroy_cred, cr, &cr->cr_label);
1310100979Srwatson	mac_destroy_label(&cr->cr_label);
1311101988Srwatson#ifdef MAC_DEBUG
1312100979Srwatson	atomic_subtract_int(&nmaccreds, 1);
1313101988Srwatson#endif
1314100979Srwatson}
1315100979Srwatson
1316100979Srwatsonvoid
1317100979Srwatsonmac_init_ifnet(struct ifnet *ifp)
1318100979Srwatson{
1319100979Srwatson
1320100979Srwatson	mac_init_label(&ifp->if_label);
1321100979Srwatson	MAC_PERFORM(init_ifnet, ifp, &ifp->if_label);
1322101988Srwatson#ifdef MAC_DEBUG
1323100979Srwatson	atomic_add_int(&nmacifnets, 1);
1324101988Srwatson#endif
1325100979Srwatson}
1326100979Srwatson
1327100979Srwatsonvoid
1328100979Srwatsonmac_destroy_ifnet(struct ifnet *ifp)
1329100979Srwatson{
1330100979Srwatson
1331100979Srwatson	MAC_PERFORM(destroy_ifnet, ifp, &ifp->if_label);
1332100979Srwatson	mac_destroy_label(&ifp->if_label);
1333101988Srwatson#ifdef MAC_DEBUG
1334100979Srwatson	atomic_subtract_int(&nmacifnets, 1);
1335101988Srwatson#endif
1336100979Srwatson}
1337100979Srwatson
1338100979Srwatsonvoid
1339100979Srwatsonmac_init_ipq(struct ipq *ipq)
1340100979Srwatson{
1341100979Srwatson
1342100979Srwatson	mac_init_label(&ipq->ipq_label);
1343100979Srwatson	MAC_PERFORM(init_ipq, ipq, &ipq->ipq_label);
1344101988Srwatson#ifdef MAC_DEBUG
1345100979Srwatson	atomic_add_int(&nmacipqs, 1);
1346101988Srwatson#endif
1347100979Srwatson}
1348100979Srwatson
1349100979Srwatsonvoid
1350100979Srwatsonmac_destroy_ipq(struct ipq *ipq)
1351100979Srwatson{
1352100979Srwatson
1353100979Srwatson	MAC_PERFORM(destroy_ipq, ipq, &ipq->ipq_label);
1354100979Srwatson	mac_destroy_label(&ipq->ipq_label);
1355101988Srwatson#ifdef MAC_DEBUG
1356100979Srwatson	atomic_subtract_int(&nmacipqs, 1);
1357101988Srwatson#endif
1358100979Srwatson}
1359100979Srwatson
1360100979Srwatsonvoid
1361100979Srwatsonmac_init_socket(struct socket *socket)
1362100979Srwatson{
1363100979Srwatson
1364100979Srwatson	mac_init_label(&socket->so_label);
1365100979Srwatson	mac_init_label(&socket->so_peerlabel);
1366100979Srwatson	MAC_PERFORM(init_socket, socket, &socket->so_label,
1367100979Srwatson	    &socket->so_peerlabel);
1368101988Srwatson#ifdef MAC_DEBUG
1369100979Srwatson	atomic_add_int(&nmacsockets, 1);
1370101988Srwatson#endif
1371100979Srwatson}
1372100979Srwatson
1373100979Srwatsonvoid
1374100979Srwatsonmac_destroy_socket(struct socket *socket)
1375100979Srwatson{
1376100979Srwatson
1377100979Srwatson	MAC_PERFORM(destroy_socket, socket, &socket->so_label,
1378100979Srwatson	    &socket->so_peerlabel);
1379100979Srwatson	mac_destroy_label(&socket->so_label);
1380100979Srwatson	mac_destroy_label(&socket->so_peerlabel);
1381101988Srwatson#ifdef MAC_DEBUG
1382100979Srwatson	atomic_subtract_int(&nmacsockets, 1);
1383101988Srwatson#endif
1384100979Srwatson}
1385100979Srwatson
1386100979Srwatsonvoid
1387100979Srwatsonmac_init_pipe(struct pipe *pipe)
1388100979Srwatson{
1389100979Srwatson	struct label *label;
1390100979Srwatson
1391100979Srwatson	label = malloc(sizeof(struct label), M_MACPIPELABEL, M_ZERO|M_WAITOK);
1392100979Srwatson	mac_init_label(label);
1393100979Srwatson	pipe->pipe_label = label;
1394100979Srwatson	pipe->pipe_peer->pipe_label = label;
1395100979Srwatson	MAC_PERFORM(init_pipe, pipe, pipe->pipe_label);
1396101988Srwatson#ifdef MAC_DEBUG
1397100979Srwatson	atomic_add_int(&nmacpipes, 1);
1398101988Srwatson#endif
1399100979Srwatson}
1400100979Srwatson
1401100979Srwatsonvoid
1402100979Srwatsonmac_destroy_pipe(struct pipe *pipe)
1403100979Srwatson{
1404100979Srwatson
1405100979Srwatson	MAC_PERFORM(destroy_pipe, pipe, pipe->pipe_label);
1406100979Srwatson	mac_destroy_label(pipe->pipe_label);
1407100979Srwatson	free(pipe->pipe_label, M_MACPIPELABEL);
1408101988Srwatson#ifdef MAC_DEBUG
1409100979Srwatson	atomic_subtract_int(&nmacpipes, 1);
1410101988Srwatson#endif
1411100979Srwatson}
1412100979Srwatson
1413100979Srwatsonvoid
1414100979Srwatsonmac_init_bpfdesc(struct bpf_d *bpf_d)
1415100979Srwatson{
1416100979Srwatson
1417100979Srwatson	mac_init_label(&bpf_d->bd_label);
1418100979Srwatson	MAC_PERFORM(init_bpfdesc, bpf_d, &bpf_d->bd_label);
1419101988Srwatson#ifdef MAC_DEBUG
1420100979Srwatson	atomic_add_int(&nmacbpfdescs, 1);
1421101988Srwatson#endif
1422100979Srwatson}
1423100979Srwatson
1424100979Srwatsonvoid
1425100979Srwatsonmac_destroy_bpfdesc(struct bpf_d *bpf_d)
1426100979Srwatson{
1427100979Srwatson
1428100979Srwatson	MAC_PERFORM(destroy_bpfdesc, bpf_d, &bpf_d->bd_label);
1429100979Srwatson	mac_destroy_label(&bpf_d->bd_label);
1430101988Srwatson#ifdef MAC_DEBUG
1431100979Srwatson	atomic_subtract_int(&nmacbpfdescs, 1);
1432101988Srwatson#endif
1433100979Srwatson}
1434100979Srwatson
1435100979Srwatsonvoid
1436100979Srwatsonmac_init_mount(struct mount *mp)
1437100979Srwatson{
1438100979Srwatson
1439100979Srwatson	mac_init_label(&mp->mnt_mntlabel);
1440100979Srwatson	mac_init_label(&mp->mnt_fslabel);
1441100979Srwatson	MAC_PERFORM(init_mount, mp, &mp->mnt_mntlabel, &mp->mnt_fslabel);
1442101988Srwatson#ifdef MAC_DEBUG
1443100979Srwatson	atomic_add_int(&nmacmounts, 1);
1444101988Srwatson#endif
1445100979Srwatson}
1446100979Srwatson
1447100979Srwatsonvoid
1448100979Srwatsonmac_destroy_mount(struct mount *mp)
1449100979Srwatson{
1450100979Srwatson
1451100979Srwatson	MAC_PERFORM(destroy_mount, mp, &mp->mnt_mntlabel, &mp->mnt_fslabel);
1452100979Srwatson	mac_destroy_label(&mp->mnt_fslabel);
1453100979Srwatson	mac_destroy_label(&mp->mnt_mntlabel);
1454101988Srwatson#ifdef MAC_DEBUG
1455100979Srwatson	atomic_subtract_int(&nmacmounts, 1);
1456101988Srwatson#endif
1457100979Srwatson}
1458100979Srwatson
1459100979Srwatsonstatic void
1460100979Srwatsonmac_init_temp(struct label *label)
1461100979Srwatson{
1462100979Srwatson
1463100979Srwatson	mac_init_label(label);
1464100979Srwatson	MAC_PERFORM(init_temp, label);
1465101988Srwatson#ifdef MAC_DEBUG
1466100979Srwatson	atomic_add_int(&nmactemp, 1);
1467101988Srwatson#endif
1468100979Srwatson}
1469100979Srwatson
1470100979Srwatsonstatic void
1471100979Srwatsonmac_destroy_temp(struct label *label)
1472100979Srwatson{
1473100979Srwatson
1474100979Srwatson	MAC_PERFORM(destroy_temp, label);
1475100979Srwatson	mac_destroy_label(label);
1476101988Srwatson#ifdef MAC_DEBUG
1477100979Srwatson	atomic_subtract_int(&nmactemp, 1);
1478101988Srwatson#endif
1479100979Srwatson}
1480100979Srwatson
1481100979Srwatsonvoid
1482100979Srwatsonmac_init_vnode(struct vnode *vp)
1483100979Srwatson{
1484100979Srwatson
1485100979Srwatson	mac_init_label(&vp->v_label);
1486100979Srwatson	MAC_PERFORM(init_vnode, vp, &vp->v_label);
1487101988Srwatson#ifdef MAC_DEBUG
1488100979Srwatson	atomic_add_int(&nmacvnodes, 1);
1489101988Srwatson#endif
1490100979Srwatson}
1491100979Srwatson
1492100979Srwatsonvoid
1493100979Srwatsonmac_destroy_vnode(struct vnode *vp)
1494100979Srwatson{
1495100979Srwatson
1496100979Srwatson	MAC_PERFORM(destroy_vnode, vp, &vp->v_label);
1497100979Srwatson	mac_destroy_label(&vp->v_label);
1498101988Srwatson#ifdef MAC_DEBUG
1499100979Srwatson	atomic_subtract_int(&nmacvnodes, 1);
1500101988Srwatson#endif
1501100979Srwatson}
1502100979Srwatson
1503100979Srwatsonvoid
1504100979Srwatsonmac_init_devfsdirent(struct devfs_dirent *de)
1505100979Srwatson{
1506100979Srwatson
1507100979Srwatson	mac_init_label(&de->de_label);
1508100979Srwatson	MAC_PERFORM(init_devfsdirent, de, &de->de_label);
1509101988Srwatson#ifdef MAC_DEBUG
1510100979Srwatson	atomic_add_int(&nmacdevfsdirents, 1);
1511101988Srwatson#endif
1512100979Srwatson}
1513100979Srwatson
1514100979Srwatsonvoid
1515100979Srwatsonmac_destroy_devfsdirent(struct devfs_dirent *de)
1516100979Srwatson{
1517100979Srwatson
1518100979Srwatson	MAC_PERFORM(destroy_devfsdirent, de, &de->de_label);
1519100979Srwatson	mac_destroy_label(&de->de_label);
1520101988Srwatson#ifdef MAC_DEBUG
1521100979Srwatson	atomic_subtract_int(&nmacdevfsdirents, 1);
1522101988Srwatson#endif
1523100979Srwatson}
1524100979Srwatson
1525100979Srwatsonstatic int
1526100979Srwatsonmac_externalize(struct label *label, struct mac *mac)
1527100979Srwatson{
1528100979Srwatson	int error;
1529100979Srwatson
1530100979Srwatson	mac_init_structmac(mac);
1531100979Srwatson	MAC_CHECK(externalize, label, mac);
1532100979Srwatson
1533100979Srwatson	return (error);
1534100979Srwatson}
1535100979Srwatson
1536100979Srwatsonstatic int
1537100979Srwatsonmac_internalize(struct label *label, struct mac *mac)
1538100979Srwatson{
1539100979Srwatson	int error;
1540100979Srwatson
1541100979Srwatson	mac_init_temp(label);
1542100979Srwatson	MAC_CHECK(internalize, label, mac);
1543100979Srwatson	if (error)
1544100979Srwatson		mac_destroy_temp(label);
1545100979Srwatson
1546100979Srwatson	return (error);
1547100979Srwatson}
1548100979Srwatson
1549100979Srwatson/*
1550100979Srwatson * Initialize MAC label for the first kernel process, from which other
1551100979Srwatson * kernel processes and threads are spawned.
1552100979Srwatson */
1553100979Srwatsonvoid
1554100979Srwatsonmac_create_proc0(struct ucred *cred)
1555100979Srwatson{
1556100979Srwatson
1557100979Srwatson	MAC_PERFORM(create_proc0, cred);
1558100979Srwatson}
1559100979Srwatson
1560100979Srwatson/*
1561100979Srwatson * Initialize MAC label for the first userland process, from which other
1562100979Srwatson * userland processes and threads are spawned.
1563100979Srwatson */
1564100979Srwatsonvoid
1565100979Srwatsonmac_create_proc1(struct ucred *cred)
1566100979Srwatson{
1567100979Srwatson
1568100979Srwatson	MAC_PERFORM(create_proc1, cred);
1569100979Srwatson}
1570100979Srwatson
1571100979Srwatson/*
1572100979Srwatson * When a new process is created, its label must be initialized.  Generally,
1573100979Srwatson * this involves inheritence from the parent process, modulo possible
1574100979Srwatson * deltas.  This function allows that processing to take place.
1575100979Srwatson */
1576100979Srwatsonvoid
1577100979Srwatsonmac_create_cred(struct ucred *parent_cred, struct ucred *child_cred)
1578100979Srwatson{
1579100979Srwatson
1580100979Srwatson	MAC_PERFORM(create_cred, parent_cred, child_cred);
1581100979Srwatson}
1582100979Srwatson
1583100979Srwatsonint
1584100979Srwatsonmac_check_vnode_access(struct ucred *cred, struct vnode *vp, int flags)
1585100979Srwatson{
1586100979Srwatson	int error;
1587100979Srwatson
1588100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_access");
1589100979Srwatson
1590100979Srwatson	if (!mac_enforce_fs)
1591100979Srwatson		return (0);
1592100979Srwatson
1593100979Srwatson	error = vn_refreshlabel(vp, cred);
1594100979Srwatson	if (error)
1595100979Srwatson		return (error);
1596100979Srwatson
1597100979Srwatson	MAC_CHECK(check_vnode_access, cred, vp, &vp->v_label, flags);
1598100979Srwatson	return (error);
1599100979Srwatson}
1600100979Srwatson
1601100979Srwatsonint
1602100979Srwatsonmac_check_vnode_chdir(struct ucred *cred, struct vnode *dvp)
1603100979Srwatson{
1604100979Srwatson	int error;
1605100979Srwatson
1606100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_chdir");
1607100979Srwatson
1608100979Srwatson	if (!mac_enforce_fs)
1609100979Srwatson		return (0);
1610100979Srwatson
1611100979Srwatson	error = vn_refreshlabel(dvp, cred);
1612100979Srwatson	if (error)
1613100979Srwatson		return (error);
1614100979Srwatson
1615100979Srwatson	MAC_CHECK(check_vnode_chdir, cred, dvp, &dvp->v_label);
1616100979Srwatson	return (error);
1617100979Srwatson}
1618100979Srwatson
1619100979Srwatsonint
1620100979Srwatsonmac_check_vnode_chroot(struct ucred *cred, struct vnode *dvp)
1621100979Srwatson{
1622100979Srwatson	int error;
1623100979Srwatson
1624100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_chroot");
1625100979Srwatson
1626100979Srwatson	if (!mac_enforce_fs)
1627100979Srwatson		return (0);
1628100979Srwatson
1629100979Srwatson	error = vn_refreshlabel(dvp, cred);
1630100979Srwatson	if (error)
1631100979Srwatson		return (error);
1632100979Srwatson
1633100979Srwatson	MAC_CHECK(check_vnode_chroot, cred, dvp, &dvp->v_label);
1634100979Srwatson	return (error);
1635100979Srwatson}
1636100979Srwatson
1637100979Srwatsonint
1638100979Srwatsonmac_check_vnode_create(struct ucred *cred, struct vnode *dvp,
1639100979Srwatson    struct componentname *cnp, struct vattr *vap)
1640100979Srwatson{
1641100979Srwatson	int error;
1642100979Srwatson
1643100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_create");
1644100979Srwatson
1645100979Srwatson	if (!mac_enforce_fs)
1646100979Srwatson		return (0);
1647100979Srwatson
1648100979Srwatson	error = vn_refreshlabel(dvp, cred);
1649100979Srwatson	if (error)
1650100979Srwatson		return (error);
1651100979Srwatson
1652100979Srwatson	MAC_CHECK(check_vnode_create, cred, dvp, &dvp->v_label, cnp, vap);
1653100979Srwatson	return (error);
1654100979Srwatson}
1655100979Srwatson
1656100979Srwatsonint
1657100979Srwatsonmac_check_vnode_delete(struct ucred *cred, struct vnode *dvp, struct vnode *vp,
1658100979Srwatson    struct componentname *cnp)
1659100979Srwatson{
1660100979Srwatson	int error;
1661100979Srwatson
1662100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_delete");
1663100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_delete");
1664100979Srwatson
1665100979Srwatson	if (!mac_enforce_fs)
1666100979Srwatson		return (0);
1667100979Srwatson
1668100979Srwatson	error = vn_refreshlabel(dvp, cred);
1669100979Srwatson	if (error)
1670100979Srwatson		return (error);
1671100979Srwatson	error = vn_refreshlabel(vp, cred);
1672100979Srwatson	if (error)
1673100979Srwatson		return (error);
1674100979Srwatson
1675100979Srwatson	MAC_CHECK(check_vnode_delete, cred, dvp, &dvp->v_label, vp,
1676100979Srwatson	    &vp->v_label, cnp);
1677100979Srwatson	return (error);
1678100979Srwatson}
1679100979Srwatson
1680100979Srwatsonint
1681100979Srwatsonmac_check_vnode_deleteacl(struct ucred *cred, struct vnode *vp,
1682100979Srwatson    acl_type_t type)
1683100979Srwatson{
1684100979Srwatson	int error;
1685100979Srwatson
1686100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_deleteacl");
1687100979Srwatson
1688100979Srwatson	if (!mac_enforce_fs)
1689100979Srwatson		return (0);
1690100979Srwatson
1691100979Srwatson	error = vn_refreshlabel(vp, cred);
1692100979Srwatson	if (error)
1693100979Srwatson		return (error);
1694100979Srwatson
1695100979Srwatson	MAC_CHECK(check_vnode_deleteacl, cred, vp, &vp->v_label, type);
1696100979Srwatson	return (error);
1697100979Srwatson}
1698100979Srwatson
1699100979Srwatsonint
1700100979Srwatsonmac_check_vnode_exec(struct ucred *cred, struct vnode *vp)
1701100979Srwatson{
1702100979Srwatson	int error;
1703100979Srwatson
1704102102Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_exec");
1705102102Srwatson
1706100979Srwatson	if (!mac_enforce_process && !mac_enforce_fs)
1707100979Srwatson		return (0);
1708100979Srwatson
1709100979Srwatson	error = vn_refreshlabel(vp, cred);
1710100979Srwatson	if (error)
1711100979Srwatson		return (error);
1712100979Srwatson	MAC_CHECK(check_vnode_exec, cred, vp, &vp->v_label);
1713100979Srwatson
1714100979Srwatson	return (error);
1715100979Srwatson}
1716100979Srwatson
1717100979Srwatsonint
1718100979Srwatsonmac_check_vnode_getacl(struct ucred *cred, struct vnode *vp, acl_type_t type)
1719100979Srwatson{
1720100979Srwatson	int error;
1721100979Srwatson
1722100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_getacl");
1723100979Srwatson
1724100979Srwatson	if (!mac_enforce_fs)
1725100979Srwatson		return (0);
1726100979Srwatson
1727100979Srwatson	error = vn_refreshlabel(vp, cred);
1728100979Srwatson	if (error)
1729100979Srwatson		return (error);
1730100979Srwatson
1731100979Srwatson	MAC_CHECK(check_vnode_getacl, cred, vp, &vp->v_label, type);
1732100979Srwatson	return (error);
1733100979Srwatson}
1734100979Srwatson
1735100979Srwatsonint
1736100979Srwatsonmac_check_vnode_getextattr(struct ucred *cred, struct vnode *vp,
1737100979Srwatson    int attrnamespace, const char *name, struct uio *uio)
1738100979Srwatson{
1739100979Srwatson	int error;
1740100979Srwatson
1741100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_getextattr");
1742100979Srwatson
1743100979Srwatson	if (!mac_enforce_fs)
1744100979Srwatson		return (0);
1745100979Srwatson
1746100979Srwatson	error = vn_refreshlabel(vp, cred);
1747100979Srwatson	if (error)
1748100979Srwatson		return (error);
1749100979Srwatson
1750100979Srwatson	MAC_CHECK(check_vnode_getextattr, cred, vp, &vp->v_label,
1751100979Srwatson	    attrnamespace, name, uio);
1752100979Srwatson	return (error);
1753100979Srwatson}
1754100979Srwatson
1755100979Srwatsonint
1756100979Srwatsonmac_check_vnode_lookup(struct ucred *cred, struct vnode *dvp,
1757100979Srwatson    struct componentname *cnp)
1758100979Srwatson{
1759100979Srwatson	int error;
1760100979Srwatson
1761100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_lookup");
1762100979Srwatson
1763100979Srwatson	if (!mac_enforce_fs)
1764100979Srwatson		return (0);
1765100979Srwatson
1766100979Srwatson	error = vn_refreshlabel(dvp, cred);
1767100979Srwatson	if (error)
1768100979Srwatson		return (error);
1769100979Srwatson
1770100979Srwatson	MAC_CHECK(check_vnode_lookup, cred, dvp, &dvp->v_label, cnp);
1771100979Srwatson	return (error);
1772100979Srwatson}
1773100979Srwatson
1774100979Srwatsonvm_prot_t
1775100979Srwatsonmac_check_vnode_mmap_prot(struct ucred *cred, struct vnode *vp, int newmapping)
1776100979Srwatson{
1777100979Srwatson	vm_prot_t result = VM_PROT_ALL;
1778100979Srwatson
1779100979Srwatson	/*
1780100979Srwatson	 * This should be some sort of MAC_BITWISE, maybe :)
1781100979Srwatson	 */
1782100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mmap_perms");
1783100979Srwatson	MAC_BOOLEAN(check_vnode_mmap_perms, &, cred, vp, &vp->v_label,
1784100979Srwatson	    newmapping);
1785100979Srwatson	return (result);
1786100979Srwatson}
1787100979Srwatson
1788100979Srwatsonint
1789102112Srwatsonmac_check_vnode_open(struct ucred *cred, struct vnode *vp, mode_t acc_mode)
1790100979Srwatson{
1791100979Srwatson	int error;
1792100979Srwatson
1793102112Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_open");
1794102112Srwatson
1795100979Srwatson	if (!mac_enforce_fs)
1796100979Srwatson		return (0);
1797100979Srwatson
1798102112Srwatson	error = vn_refreshlabel(vp, cred);
1799102112Srwatson	if (error)
1800102112Srwatson		return (error);
1801100979Srwatson
1802102112Srwatson	MAC_CHECK(check_vnode_open, cred, vp, &vp->v_label, acc_mode);
1803102112Srwatson	return (error);
1804102112Srwatson}
1805102112Srwatson
1806102112Srwatsonint
1807102129Srwatsonmac_check_vnode_poll(struct ucred *active_cred, struct ucred *file_cred,
1808102129Srwatson    struct vnode *vp)
1809102112Srwatson{
1810102112Srwatson	int error;
1811102112Srwatson
1812102112Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_poll");
1813102112Srwatson
1814102112Srwatson	if (!mac_enforce_fs)
1815102112Srwatson		return (0);
1816102112Srwatson
1817102129Srwatson	error = vn_refreshlabel(vp, active_cred);
1818100979Srwatson	if (error)
1819100979Srwatson		return (error);
1820100979Srwatson
1821102129Srwatson	MAC_CHECK(check_vnode_poll, active_cred, file_cred, vp,
1822102129Srwatson	    &vp->v_label);
1823100979Srwatson
1824100979Srwatson	return (error);
1825100979Srwatson}
1826100979Srwatson
1827100979Srwatsonint
1828102129Srwatsonmac_check_vnode_read(struct ucred *active_cred, struct ucred *file_cred,
1829102129Srwatson    struct vnode *vp)
1830100979Srwatson{
1831100979Srwatson	int error;
1832100979Srwatson
1833102112Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_read");
1834100979Srwatson
1835100979Srwatson	if (!mac_enforce_fs)
1836100979Srwatson		return (0);
1837100979Srwatson
1838102129Srwatson	error = vn_refreshlabel(vp, active_cred);
1839100979Srwatson	if (error)
1840100979Srwatson		return (error);
1841100979Srwatson
1842102129Srwatson	MAC_CHECK(check_vnode_read, active_cred, file_cred, vp,
1843102129Srwatson	    &vp->v_label);
1844102112Srwatson
1845100979Srwatson	return (error);
1846100979Srwatson}
1847100979Srwatson
1848100979Srwatsonint
1849100979Srwatsonmac_check_vnode_readdir(struct ucred *cred, struct vnode *dvp)
1850100979Srwatson{
1851100979Srwatson	int error;
1852100979Srwatson
1853100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_readdir");
1854100979Srwatson
1855100979Srwatson	if (!mac_enforce_fs)
1856100979Srwatson		return (0);
1857100979Srwatson
1858100979Srwatson	error = vn_refreshlabel(dvp, cred);
1859100979Srwatson	if (error)
1860100979Srwatson		return (error);
1861100979Srwatson
1862100979Srwatson	MAC_CHECK(check_vnode_readdir, cred, dvp, &dvp->v_label);
1863100979Srwatson	return (error);
1864100979Srwatson}
1865100979Srwatson
1866100979Srwatsonint
1867100979Srwatsonmac_check_vnode_readlink(struct ucred *cred, struct vnode *vp)
1868100979Srwatson{
1869100979Srwatson	int error;
1870100979Srwatson
1871100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_readlink");
1872100979Srwatson
1873100979Srwatson	if (!mac_enforce_fs)
1874100979Srwatson		return (0);
1875100979Srwatson
1876100979Srwatson	error = vn_refreshlabel(vp, cred);
1877100979Srwatson	if (error)
1878100979Srwatson		return (error);
1879100979Srwatson
1880100979Srwatson	MAC_CHECK(check_vnode_readlink, cred, vp, &vp->v_label);
1881100979Srwatson	return (error);
1882100979Srwatson}
1883100979Srwatson
1884100979Srwatsonstatic int
1885100979Srwatsonmac_check_vnode_relabel(struct ucred *cred, struct vnode *vp,
1886100979Srwatson    struct label *newlabel)
1887100979Srwatson{
1888100979Srwatson	int error;
1889100979Srwatson
1890100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_relabel");
1891100979Srwatson
1892100979Srwatson	error = vn_refreshlabel(vp, cred);
1893100979Srwatson	if (error)
1894100979Srwatson		return (error);
1895100979Srwatson
1896100979Srwatson	MAC_CHECK(check_vnode_relabel, cred, vp, &vp->v_label, newlabel);
1897100979Srwatson
1898100979Srwatson	return (error);
1899100979Srwatson}
1900100979Srwatson
1901100979Srwatsonint
1902100979Srwatsonmac_check_vnode_rename_from(struct ucred *cred, struct vnode *dvp,
1903100979Srwatson    struct vnode *vp, struct componentname *cnp)
1904100979Srwatson{
1905100979Srwatson	int error;
1906100979Srwatson
1907100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_rename_from");
1908100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_rename_from");
1909100979Srwatson
1910100979Srwatson	if (!mac_enforce_fs)
1911100979Srwatson		return (0);
1912100979Srwatson
1913100979Srwatson	error = vn_refreshlabel(dvp, cred);
1914100979Srwatson	if (error)
1915100979Srwatson		return (error);
1916100979Srwatson	error = vn_refreshlabel(vp, cred);
1917100979Srwatson	if (error)
1918100979Srwatson		return (error);
1919100979Srwatson
1920100979Srwatson	MAC_CHECK(check_vnode_rename_from, cred, dvp, &dvp->v_label, vp,
1921100979Srwatson	    &vp->v_label, cnp);
1922100979Srwatson	return (error);
1923100979Srwatson}
1924100979Srwatson
1925100979Srwatsonint
1926100979Srwatsonmac_check_vnode_rename_to(struct ucred *cred, struct vnode *dvp,
1927100979Srwatson    struct vnode *vp, int samedir, struct componentname *cnp)
1928100979Srwatson{
1929100979Srwatson	int error;
1930100979Srwatson
1931100979Srwatson	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_rename_to");
1932100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_rename_to");
1933100979Srwatson
1934100979Srwatson	if (!mac_enforce_fs)
1935100979Srwatson		return (0);
1936100979Srwatson
1937100979Srwatson	error = vn_refreshlabel(dvp, cred);
1938100979Srwatson	if (error)
1939100979Srwatson		return (error);
1940100979Srwatson	if (vp != NULL) {
1941100979Srwatson		error = vn_refreshlabel(vp, cred);
1942100979Srwatson		if (error)
1943100979Srwatson			return (error);
1944100979Srwatson	}
1945100979Srwatson	MAC_CHECK(check_vnode_rename_to, cred, dvp, &dvp->v_label, vp,
1946100979Srwatson	    vp != NULL ? &vp->v_label : NULL, samedir, cnp);
1947100979Srwatson	return (error);
1948100979Srwatson}
1949100979Srwatson
1950100979Srwatsonint
1951100979Srwatsonmac_check_vnode_revoke(struct ucred *cred, struct vnode *vp)
1952100979Srwatson{
1953100979Srwatson	int error;
1954100979Srwatson
1955100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_revoke");
1956100979Srwatson
1957100979Srwatson	if (!mac_enforce_fs)
1958100979Srwatson		return (0);
1959100979Srwatson
1960100979Srwatson	error = vn_refreshlabel(vp, cred);
1961100979Srwatson	if (error)
1962100979Srwatson		return (error);
1963100979Srwatson
1964100979Srwatson	MAC_CHECK(check_vnode_revoke, cred, vp, &vp->v_label);
1965100979Srwatson	return (error);
1966100979Srwatson}
1967100979Srwatson
1968100979Srwatsonint
1969100979Srwatsonmac_check_vnode_setacl(struct ucred *cred, struct vnode *vp, acl_type_t type,
1970100979Srwatson    struct acl *acl)
1971100979Srwatson{
1972100979Srwatson	int error;
1973100979Srwatson
1974100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setacl");
1975100979Srwatson
1976100979Srwatson	if (!mac_enforce_fs)
1977100979Srwatson		return (0);
1978100979Srwatson
1979100979Srwatson	error = vn_refreshlabel(vp, cred);
1980100979Srwatson	if (error)
1981100979Srwatson		return (error);
1982100979Srwatson
1983100979Srwatson	MAC_CHECK(check_vnode_setacl, cred, vp, &vp->v_label, type, acl);
1984100979Srwatson	return (error);
1985100979Srwatson}
1986100979Srwatson
1987100979Srwatsonint
1988100979Srwatsonmac_check_vnode_setextattr(struct ucred *cred, struct vnode *vp,
1989100979Srwatson    int attrnamespace, const char *name, struct uio *uio)
1990100979Srwatson{
1991100979Srwatson	int error;
1992100979Srwatson
1993100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setextattr");
1994100979Srwatson
1995100979Srwatson	if (!mac_enforce_fs)
1996100979Srwatson		return (0);
1997100979Srwatson
1998100979Srwatson	error = vn_refreshlabel(vp, cred);
1999100979Srwatson	if (error)
2000100979Srwatson		return (error);
2001100979Srwatson
2002100979Srwatson	MAC_CHECK(check_vnode_setextattr, cred, vp, &vp->v_label,
2003100979Srwatson	    attrnamespace, name, uio);
2004100979Srwatson	return (error);
2005100979Srwatson}
2006100979Srwatson
2007100979Srwatsonint
2008100979Srwatsonmac_check_vnode_setflags(struct ucred *cred, struct vnode *vp, u_long flags)
2009100979Srwatson{
2010100979Srwatson	int error;
2011100979Srwatson
2012100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setflags");
2013100979Srwatson
2014100979Srwatson	if (!mac_enforce_fs)
2015100979Srwatson		return (0);
2016100979Srwatson
2017100979Srwatson	error = vn_refreshlabel(vp, cred);
2018100979Srwatson	if (error)
2019100979Srwatson		return (error);
2020100979Srwatson
2021100979Srwatson	MAC_CHECK(check_vnode_setflags, cred, vp, &vp->v_label, flags);
2022100979Srwatson	return (error);
2023100979Srwatson}
2024100979Srwatson
2025100979Srwatsonint
2026100979Srwatsonmac_check_vnode_setmode(struct ucred *cred, struct vnode *vp, mode_t mode)
2027100979Srwatson{
2028100979Srwatson	int error;
2029100979Srwatson
2030100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setmode");
2031100979Srwatson
2032100979Srwatson	if (!mac_enforce_fs)
2033100979Srwatson		return (0);
2034100979Srwatson
2035100979Srwatson	error = vn_refreshlabel(vp, cred);
2036100979Srwatson	if (error)
2037100979Srwatson		return (error);
2038100979Srwatson
2039100979Srwatson	MAC_CHECK(check_vnode_setmode, cred, vp, &vp->v_label, mode);
2040100979Srwatson	return (error);
2041100979Srwatson}
2042100979Srwatson
2043100979Srwatsonint
2044100979Srwatsonmac_check_vnode_setowner(struct ucred *cred, struct vnode *vp, uid_t uid,
2045100979Srwatson    gid_t gid)
2046100979Srwatson{
2047100979Srwatson	int error;
2048100979Srwatson
2049100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setowner");
2050100979Srwatson
2051100979Srwatson	if (!mac_enforce_fs)
2052100979Srwatson		return (0);
2053100979Srwatson
2054100979Srwatson	error = vn_refreshlabel(vp, cred);
2055100979Srwatson	if (error)
2056100979Srwatson		return (error);
2057100979Srwatson
2058100979Srwatson	MAC_CHECK(check_vnode_setowner, cred, vp, &vp->v_label, uid, gid);
2059100979Srwatson	return (error);
2060100979Srwatson}
2061100979Srwatson
2062100979Srwatsonint
2063100979Srwatsonmac_check_vnode_setutimes(struct ucred *cred, struct vnode *vp,
2064100979Srwatson    struct timespec atime, struct timespec mtime)
2065100979Srwatson{
2066100979Srwatson	int error;
2067100979Srwatson
2068100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setutimes");
2069100979Srwatson
2070100979Srwatson	if (!mac_enforce_fs)
2071100979Srwatson		return (0);
2072100979Srwatson
2073100979Srwatson	error = vn_refreshlabel(vp, cred);
2074100979Srwatson	if (error)
2075100979Srwatson		return (error);
2076100979Srwatson
2077100979Srwatson	MAC_CHECK(check_vnode_setutimes, cred, vp, &vp->v_label, atime,
2078100979Srwatson	    mtime);
2079100979Srwatson	return (error);
2080100979Srwatson}
2081100979Srwatson
2082100979Srwatsonint
2083102129Srwatsonmac_check_vnode_stat(struct ucred *active_cred, struct ucred *file_cred,
2084102129Srwatson    struct vnode *vp)
2085100979Srwatson{
2086100979Srwatson	int error;
2087100979Srwatson
2088100979Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_stat");
2089100979Srwatson
2090100979Srwatson	if (!mac_enforce_fs)
2091100979Srwatson		return (0);
2092100979Srwatson
2093102129Srwatson	error = vn_refreshlabel(vp, active_cred);
2094100979Srwatson	if (error)
2095100979Srwatson		return (error);
2096100979Srwatson
2097102129Srwatson	MAC_CHECK(check_vnode_stat, active_cred, file_cred, vp,
2098102129Srwatson	    &vp->v_label);
2099100979Srwatson	return (error);
2100100979Srwatson}
2101100979Srwatson
2102102112Srwatsonint
2103102129Srwatsonmac_check_vnode_write(struct ucred *active_cred, struct ucred *file_cred,
2104102129Srwatson    struct vnode *vp)
2105102112Srwatson{
2106102112Srwatson	int error;
2107102112Srwatson
2108102112Srwatson	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_write");
2109102112Srwatson
2110102112Srwatson	if (!mac_enforce_fs)
2111102112Srwatson		return (0);
2112102112Srwatson
2113102129Srwatson	error = vn_refreshlabel(vp, active_cred);
2114102112Srwatson	if (error)
2115102112Srwatson		return (error);
2116102112Srwatson
2117102129Srwatson	MAC_CHECK(check_vnode_write, active_cred, file_cred, vp,
2118102129Srwatson	    &vp->v_label);
2119102112Srwatson
2120102112Srwatson	return (error);
2121102112Srwatson}
2122102112Srwatson
2123100979Srwatson/*
2124100979Srwatson * When relabeling a process, call out to the policies for the maximum
2125100979Srwatson * permission allowed for each object type we know about in its
2126100979Srwatson * memory space, and revoke access (in the least surprising ways we
2127100979Srwatson * know) when necessary.  The process lock is not held here.
2128100979Srwatson */
2129100979Srwatsonstatic void
2130100979Srwatsonmac_cred_mmapped_drop_perms(struct thread *td, struct ucred *cred)
2131100979Srwatson{
2132100979Srwatson
2133100979Srwatson	/* XXX freeze all other threads */
2134100979Srwatson	mtx_lock(&Giant);
2135100979Srwatson	mac_cred_mmapped_drop_perms_recurse(td, cred,
2136100979Srwatson	    &td->td_proc->p_vmspace->vm_map);
2137100979Srwatson	mtx_unlock(&Giant);
2138100979Srwatson	/* XXX allow other threads to continue */
2139100979Srwatson}
2140100979Srwatson
2141100979Srwatsonstatic __inline const char *
2142100979Srwatsonprot2str(vm_prot_t prot)
2143100979Srwatson{
2144100979Srwatson
2145100979Srwatson	switch (prot & VM_PROT_ALL) {
2146100979Srwatson	case VM_PROT_READ:
2147100979Srwatson		return ("r--");
2148100979Srwatson	case VM_PROT_READ | VM_PROT_WRITE:
2149100979Srwatson		return ("rw-");
2150100979Srwatson	case VM_PROT_READ | VM_PROT_EXECUTE:
2151100979Srwatson		return ("r-x");
2152100979Srwatson	case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
2153100979Srwatson		return ("rwx");
2154100979Srwatson	case VM_PROT_WRITE:
2155100979Srwatson		return ("-w-");
2156100979Srwatson	case VM_PROT_EXECUTE:
2157100979Srwatson		return ("--x");
2158100979Srwatson	case VM_PROT_WRITE | VM_PROT_EXECUTE:
2159100979Srwatson		return ("-wx");
2160100979Srwatson	default:
2161100979Srwatson		return ("---");
2162100979Srwatson	}
2163100979Srwatson}
2164100979Srwatson
2165100979Srwatsonstatic void
2166100979Srwatsonmac_cred_mmapped_drop_perms_recurse(struct thread *td, struct ucred *cred,
2167100979Srwatson    struct vm_map *map)
2168100979Srwatson{
2169100979Srwatson	struct vm_map_entry *vme;
2170100979Srwatson	vm_prot_t result, revokeperms;
2171100979Srwatson	vm_object_t object;
2172100979Srwatson	vm_ooffset_t offset;
2173100979Srwatson	struct vnode *vp;
2174100979Srwatson
2175100979Srwatson	vm_map_lock_read(map);
2176100979Srwatson	for (vme = map->header.next; vme != &map->header; vme = vme->next) {
2177100979Srwatson		if (vme->eflags & MAP_ENTRY_IS_SUB_MAP) {
2178100979Srwatson			mac_cred_mmapped_drop_perms_recurse(td, cred,
2179100979Srwatson			    vme->object.sub_map);
2180100979Srwatson			continue;
2181100979Srwatson		}
2182100979Srwatson		/*
2183100979Srwatson		 * Skip over entries that obviously are not shared.
2184100979Srwatson		 */
2185100979Srwatson		if (vme->eflags & (MAP_ENTRY_COW | MAP_ENTRY_NOSYNC) ||
2186100979Srwatson		    !vme->max_protection)
2187100979Srwatson			continue;
2188100979Srwatson		/*
2189100979Srwatson		 * Drill down to the deepest backing object.
2190100979Srwatson		 */
2191100979Srwatson		offset = vme->offset;
2192100979Srwatson		object = vme->object.vm_object;
2193100979Srwatson		if (object == NULL)
2194100979Srwatson			continue;
2195100979Srwatson		while (object->backing_object != NULL) {
2196100979Srwatson			object = object->backing_object;
2197100979Srwatson			offset += object->backing_object_offset;
2198100979Srwatson		}
2199100979Srwatson		/*
2200100979Srwatson		 * At the moment, vm_maps and objects aren't considered
2201100979Srwatson		 * by the MAC system, so only things with backing by a
2202100979Srwatson		 * normal object (read: vnodes) are checked.
2203100979Srwatson		 */
2204100979Srwatson		if (object->type != OBJT_VNODE)
2205100979Srwatson			continue;
2206100979Srwatson		vp = (struct vnode *)object->handle;
2207100979Srwatson		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2208100979Srwatson		result = mac_check_vnode_mmap_prot(cred, vp, 0);
2209100979Srwatson		VOP_UNLOCK(vp, 0, td);
2210100979Srwatson		/*
2211100979Srwatson		 * Find out what maximum protection we may be allowing
2212100979Srwatson		 * now but a policy needs to get removed.
2213100979Srwatson		 */
2214100979Srwatson		revokeperms = vme->max_protection & ~result;
2215100979Srwatson		if (!revokeperms)
2216100979Srwatson			continue;
2217100979Srwatson		printf("pid %d: revoking %s perms from %#lx:%d "
2218100979Srwatson		    "(max %s/cur %s)\n", td->td_proc->p_pid,
2219100979Srwatson		    prot2str(revokeperms), vme->start, vme->end - vme->start,
2220100979Srwatson		    prot2str(vme->max_protection), prot2str(vme->protection));
2221100979Srwatson		vm_map_lock_upgrade(map);
2222100979Srwatson		/*
2223100979Srwatson		 * This is the really simple case: if a map has more
2224100979Srwatson		 * max_protection than is allowed, but it's not being
2225100979Srwatson		 * actually used (that is, the current protection is
2226100979Srwatson		 * still allowed), we can just wipe it out and do
2227100979Srwatson		 * nothing more.
2228100979Srwatson		 */
2229100979Srwatson		if ((vme->protection & revokeperms) == 0) {
2230100979Srwatson			vme->max_protection -= revokeperms;
2231100979Srwatson		} else {
2232100979Srwatson			if (revokeperms & VM_PROT_WRITE) {
2233100979Srwatson				/*
2234100979Srwatson				 * In the more complicated case, flush out all
2235100979Srwatson				 * pending changes to the object then turn it
2236100979Srwatson				 * copy-on-write.
2237100979Srwatson				 */
2238100979Srwatson				vm_object_reference(object);
2239100979Srwatson				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2240100979Srwatson				vm_object_page_clean(object,
2241100979Srwatson				    OFF_TO_IDX(offset),
2242100979Srwatson				    OFF_TO_IDX(offset + vme->end - vme->start +
2243100979Srwatson					PAGE_MASK),
2244100979Srwatson				    OBJPC_SYNC);
2245100979Srwatson				VOP_UNLOCK(vp, 0, td);
2246100979Srwatson				vm_object_deallocate(object);
2247100979Srwatson				/*
2248100979Srwatson				 * Why bother if there's no read permissions
2249100979Srwatson				 * anymore?  For the rest, we need to leave
2250100979Srwatson				 * the write permissions on for COW, or
2251100979Srwatson				 * remove them entirely if configured to.
2252100979Srwatson				 */
2253100979Srwatson				if (!mac_mmap_revocation_via_cow) {
2254100979Srwatson					vme->max_protection &= ~VM_PROT_WRITE;
2255100979Srwatson					vme->protection &= ~VM_PROT_WRITE;
2256100979Srwatson				} if ((revokeperms & VM_PROT_READ) == 0)
2257100979Srwatson					vme->eflags |= MAP_ENTRY_COW |
2258100979Srwatson					    MAP_ENTRY_NEEDS_COPY;
2259100979Srwatson			}
2260100979Srwatson			if (revokeperms & VM_PROT_EXECUTE) {
2261100979Srwatson				vme->max_protection &= ~VM_PROT_EXECUTE;
2262100979Srwatson				vme->protection &= ~VM_PROT_EXECUTE;
2263100979Srwatson			}
2264100979Srwatson			if (revokeperms & VM_PROT_READ) {
2265100979Srwatson				vme->max_protection = 0;
2266100979Srwatson				vme->protection = 0;
2267100979Srwatson			}
2268100979Srwatson			pmap_protect(map->pmap, vme->start, vme->end,
2269100979Srwatson			    vme->protection & ~revokeperms);
2270100979Srwatson			vm_map_simplify_entry(map, vme);
2271100979Srwatson		}
2272100979Srwatson		vm_map_lock_downgrade(map);
2273100979Srwatson	}
2274100979Srwatson	vm_map_unlock_read(map);
2275100979Srwatson}
2276100979Srwatson
2277100979Srwatson/*
2278100979Srwatson * When the subject's label changes, it may require revocation of privilege
2279100979Srwatson * to mapped objects.  This can't be done on-the-fly later with a unified
2280100979Srwatson * buffer cache.
2281100979Srwatson */
2282100979Srwatsonstatic void
2283100979Srwatsonmac_relabel_cred(struct ucred *cred, struct label *newlabel)
2284100979Srwatson{
2285100979Srwatson
2286100979Srwatson	MAC_PERFORM(relabel_cred, cred, newlabel);
2287100979Srwatson}
2288100979Srwatson
2289100979Srwatsonvoid
2290100979Srwatsonmac_relabel_vnode(struct ucred *cred, struct vnode *vp, struct label *newlabel)
2291100979Srwatson{
2292100979Srwatson
2293100979Srwatson	MAC_PERFORM(relabel_vnode, cred, vp, &vp->v_label, newlabel);
2294100979Srwatson}
2295100979Srwatson
2296100979Srwatsonvoid
2297100979Srwatsonmac_create_ifnet(struct ifnet *ifnet)
2298100979Srwatson{
2299100979Srwatson
2300100979Srwatson	MAC_PERFORM(create_ifnet, ifnet, &ifnet->if_label);
2301100979Srwatson}
2302100979Srwatson
2303100979Srwatsonvoid
2304100979Srwatsonmac_create_bpfdesc(struct ucred *cred, struct bpf_d *bpf_d)
2305100979Srwatson{
2306100979Srwatson
2307100979Srwatson	MAC_PERFORM(create_bpfdesc, cred, bpf_d, &bpf_d->bd_label);
2308100979Srwatson}
2309100979Srwatson
2310100979Srwatsonvoid
2311100979Srwatsonmac_create_socket(struct ucred *cred, struct socket *socket)
2312100979Srwatson{
2313100979Srwatson
2314100979Srwatson	MAC_PERFORM(create_socket, cred, socket, &socket->so_label);
2315100979Srwatson}
2316100979Srwatson
2317100979Srwatsonvoid
2318100979Srwatsonmac_create_pipe(struct ucred *cred, struct pipe *pipe)
2319100979Srwatson{
2320100979Srwatson
2321100979Srwatson	MAC_PERFORM(create_pipe, cred, pipe, pipe->pipe_label);
2322100979Srwatson}
2323100979Srwatson
2324100979Srwatsonvoid
2325100979Srwatsonmac_create_socket_from_socket(struct socket *oldsocket,
2326100979Srwatson    struct socket *newsocket)
2327100979Srwatson{
2328100979Srwatson
2329100979Srwatson	MAC_PERFORM(create_socket_from_socket, oldsocket, &oldsocket->so_label,
2330100979Srwatson	    newsocket, &newsocket->so_label);
2331100979Srwatson}
2332100979Srwatson
2333100979Srwatsonstatic void
2334100979Srwatsonmac_relabel_socket(struct ucred *cred, struct socket *socket,
2335100979Srwatson    struct label *newlabel)
2336100979Srwatson{
2337100979Srwatson
2338100979Srwatson	MAC_PERFORM(relabel_socket, cred, socket, &socket->so_label, newlabel);
2339100979Srwatson}
2340100979Srwatson
2341100979Srwatsonstatic void
2342100979Srwatsonmac_relabel_pipe(struct ucred *cred, struct pipe *pipe, struct label *newlabel)
2343100979Srwatson{
2344100979Srwatson
2345100979Srwatson	MAC_PERFORM(relabel_pipe, cred, pipe, pipe->pipe_label, newlabel);
2346100979Srwatson}
2347100979Srwatson
2348100979Srwatsonvoid
2349100979Srwatsonmac_set_socket_peer_from_mbuf(struct mbuf *mbuf, struct socket *socket)
2350100979Srwatson{
2351100979Srwatson
2352100979Srwatson	MAC_PERFORM(set_socket_peer_from_mbuf, mbuf, &mbuf->m_pkthdr.label,
2353100979Srwatson	    socket, &socket->so_peerlabel);
2354100979Srwatson}
2355100979Srwatson
2356100979Srwatsonvoid
2357100979Srwatsonmac_set_socket_peer_from_socket(struct socket *oldsocket,
2358100979Srwatson    struct socket *newsocket)
2359100979Srwatson{
2360100979Srwatson
2361100979Srwatson	MAC_PERFORM(set_socket_peer_from_socket, oldsocket,
2362100979Srwatson	    &oldsocket->so_label, newsocket, &newsocket->so_peerlabel);
2363100979Srwatson}
2364100979Srwatson
2365100979Srwatsonvoid
2366100979Srwatsonmac_create_datagram_from_ipq(struct ipq *ipq, struct mbuf *datagram)
2367100979Srwatson{
2368100979Srwatson
2369100979Srwatson	MAC_PERFORM(create_datagram_from_ipq, ipq, &ipq->ipq_label,
2370100979Srwatson	    datagram, &datagram->m_pkthdr.label);
2371100979Srwatson}
2372100979Srwatson
2373100979Srwatsonvoid
2374100979Srwatsonmac_create_fragment(struct mbuf *datagram, struct mbuf *fragment)
2375100979Srwatson{
2376100979Srwatson
2377100979Srwatson	MAC_PERFORM(create_fragment, datagram, &datagram->m_pkthdr.label,
2378100979Srwatson	    fragment, &fragment->m_pkthdr.label);
2379100979Srwatson}
2380100979Srwatson
2381100979Srwatsonvoid
2382100979Srwatsonmac_create_ipq(struct mbuf *fragment, struct ipq *ipq)
2383100979Srwatson{
2384100979Srwatson
2385100979Srwatson	MAC_PERFORM(create_ipq, fragment, &fragment->m_pkthdr.label, ipq,
2386100979Srwatson	    &ipq->ipq_label);
2387100979Srwatson}
2388100979Srwatson
2389100979Srwatsonvoid
2390100979Srwatsonmac_create_mbuf_from_mbuf(struct mbuf *oldmbuf, struct mbuf *newmbuf)
2391100979Srwatson{
2392100979Srwatson
2393100979Srwatson	MAC_PERFORM(create_mbuf_from_mbuf, oldmbuf, &oldmbuf->m_pkthdr.label,
2394100979Srwatson	    newmbuf, &newmbuf->m_pkthdr.label);
2395100979Srwatson}
2396100979Srwatson
2397100979Srwatsonvoid
2398100979Srwatsonmac_create_mbuf_from_bpfdesc(struct bpf_d *bpf_d, struct mbuf *mbuf)
2399100979Srwatson{
2400100979Srwatson
2401100979Srwatson	MAC_PERFORM(create_mbuf_from_bpfdesc, bpf_d, &bpf_d->bd_label, mbuf,
2402100979Srwatson	    &mbuf->m_pkthdr.label);
2403100979Srwatson}
2404100979Srwatson
2405100979Srwatsonvoid
2406100979Srwatsonmac_create_mbuf_linklayer(struct ifnet *ifnet, struct mbuf *mbuf)
2407100979Srwatson{
2408100979Srwatson
2409100979Srwatson	MAC_PERFORM(create_mbuf_linklayer, ifnet, &ifnet->if_label, mbuf,
2410100979Srwatson	    &mbuf->m_pkthdr.label);
2411100979Srwatson}
2412100979Srwatson
2413100979Srwatsonvoid
2414100979Srwatsonmac_create_mbuf_from_ifnet(struct ifnet *ifnet, struct mbuf *mbuf)
2415100979Srwatson{
2416100979Srwatson
2417100979Srwatson	MAC_PERFORM(create_mbuf_from_ifnet, ifnet, &ifnet->if_label, mbuf,
2418100979Srwatson	    &mbuf->m_pkthdr.label);
2419100979Srwatson}
2420100979Srwatson
2421100979Srwatsonvoid
2422100979Srwatsonmac_create_mbuf_multicast_encap(struct mbuf *oldmbuf, struct ifnet *ifnet,
2423100979Srwatson    struct mbuf *newmbuf)
2424100979Srwatson{
2425100979Srwatson
2426100979Srwatson	MAC_PERFORM(create_mbuf_multicast_encap, oldmbuf,
2427100979Srwatson	    &oldmbuf->m_pkthdr.label, ifnet, &ifnet->if_label, newmbuf,
2428100979Srwatson	    &newmbuf->m_pkthdr.label);
2429100979Srwatson}
2430100979Srwatson
2431100979Srwatsonvoid
2432100979Srwatsonmac_create_mbuf_netlayer(struct mbuf *oldmbuf, struct mbuf *newmbuf)
2433100979Srwatson{
2434100979Srwatson
2435100979Srwatson	MAC_PERFORM(create_mbuf_netlayer, oldmbuf, &oldmbuf->m_pkthdr.label,
2436100979Srwatson	    newmbuf, &newmbuf->m_pkthdr.label);
2437100979Srwatson}
2438100979Srwatson
2439100979Srwatsonint
2440100979Srwatsonmac_fragment_match(struct mbuf *fragment, struct ipq *ipq)
2441100979Srwatson{
2442100979Srwatson	int result;
2443100979Srwatson
2444100979Srwatson	result = 1;
2445100979Srwatson	MAC_BOOLEAN(fragment_match, &&, fragment, &fragment->m_pkthdr.label,
2446100979Srwatson	    ipq, &ipq->ipq_label);
2447100979Srwatson
2448100979Srwatson	return (result);
2449100979Srwatson}
2450100979Srwatson
2451100979Srwatsonvoid
2452100979Srwatsonmac_update_ipq(struct mbuf *fragment, struct ipq *ipq)
2453100979Srwatson{
2454100979Srwatson
2455100979Srwatson	MAC_PERFORM(update_ipq, fragment, &fragment->m_pkthdr.label, ipq,
2456100979Srwatson	    &ipq->ipq_label);
2457100979Srwatson}
2458100979Srwatson
2459100979Srwatsonvoid
2460100979Srwatsonmac_create_mbuf_from_socket(struct socket *socket, struct mbuf *mbuf)
2461100979Srwatson{
2462100979Srwatson
2463100979Srwatson	MAC_PERFORM(create_mbuf_from_socket, socket, &socket->so_label, mbuf,
2464100979Srwatson	    &mbuf->m_pkthdr.label);
2465100979Srwatson}
2466100979Srwatson
2467100979Srwatsonvoid
2468100979Srwatsonmac_create_mount(struct ucred *cred, struct mount *mp)
2469100979Srwatson{
2470100979Srwatson
2471100979Srwatson	MAC_PERFORM(create_mount, cred, mp, &mp->mnt_mntlabel,
2472100979Srwatson	    &mp->mnt_fslabel);
2473100979Srwatson}
2474100979Srwatson
2475100979Srwatsonvoid
2476100979Srwatsonmac_create_root_mount(struct ucred *cred, struct mount *mp)
2477100979Srwatson{
2478100979Srwatson
2479100979Srwatson	MAC_PERFORM(create_root_mount, cred, mp, &mp->mnt_mntlabel,
2480100979Srwatson	    &mp->mnt_fslabel);
2481100979Srwatson}
2482100979Srwatson
2483100979Srwatsonint
2484100979Srwatsonmac_check_bpfdesc_receive(struct bpf_d *bpf_d, struct ifnet *ifnet)
2485100979Srwatson{
2486100979Srwatson	int error;
2487100979Srwatson
2488100979Srwatson	if (!mac_enforce_network)
2489100979Srwatson		return (0);
2490100979Srwatson
2491100979Srwatson	MAC_CHECK(check_bpfdesc_receive, bpf_d, &bpf_d->bd_label, ifnet,
2492100979Srwatson	    &ifnet->if_label);
2493100979Srwatson
2494100979Srwatson	return (error);
2495100979Srwatson}
2496100979Srwatson
2497100979Srwatsonstatic int
2498100979Srwatsonmac_check_cred_relabel(struct ucred *cred, struct label *newlabel)
2499100979Srwatson{
2500100979Srwatson	int error;
2501100979Srwatson
2502100979Srwatson	MAC_CHECK(check_cred_relabel, cred, newlabel);
2503100979Srwatson
2504100979Srwatson	return (error);
2505100979Srwatson}
2506100979Srwatson
2507100979Srwatsonint
2508100979Srwatsonmac_check_cred_visible(struct ucred *u1, struct ucred *u2)
2509100979Srwatson{
2510100979Srwatson	int error;
2511100979Srwatson
2512100979Srwatson	if (!mac_enforce_process)
2513100979Srwatson		return (0);
2514100979Srwatson
2515100979Srwatson	MAC_CHECK(check_cred_visible, u1, u2);
2516100979Srwatson
2517100979Srwatson	return (error);
2518100979Srwatson}
2519100979Srwatson
2520100979Srwatsonint
2521100979Srwatsonmac_check_ifnet_transmit(struct ifnet *ifnet, struct mbuf *mbuf)
2522100979Srwatson{
2523100979Srwatson	int error;
2524100979Srwatson
2525100979Srwatson	if (!mac_enforce_network)
2526100979Srwatson		return (0);
2527100979Srwatson
2528100979Srwatson	KASSERT(mbuf->m_flags & M_PKTHDR, ("packet has no pkthdr"));
2529100979Srwatson	if (!(mbuf->m_pkthdr.label.l_flags & MAC_FLAG_INITIALIZED))
2530100979Srwatson		printf("%s%d: not initialized\n", ifnet->if_name,
2531100979Srwatson		    ifnet->if_unit);
2532100979Srwatson
2533100979Srwatson	MAC_CHECK(check_ifnet_transmit, ifnet, &ifnet->if_label, mbuf,
2534100979Srwatson	    &mbuf->m_pkthdr.label);
2535100979Srwatson
2536100979Srwatson	return (error);
2537100979Srwatson}
2538100979Srwatson
2539100979Srwatsonint
2540100979Srwatsonmac_check_mount_stat(struct ucred *cred, struct mount *mount)
2541100979Srwatson{
2542100979Srwatson	int error;
2543100979Srwatson
2544100979Srwatson	if (!mac_enforce_fs)
2545100979Srwatson		return (0);
2546100979Srwatson
2547100979Srwatson	MAC_CHECK(check_mount_stat, cred, mount, &mount->mnt_mntlabel);
2548100979Srwatson
2549100979Srwatson	return (error);
2550100979Srwatson}
2551100979Srwatson
2552100979Srwatsonint
2553100979Srwatsonmac_check_pipe_ioctl(struct ucred *cred, struct pipe *pipe, unsigned long cmd,
2554100979Srwatson    void *data)
2555100979Srwatson{
2556100979Srwatson	int error;
2557100979Srwatson
2558100979Srwatson	MAC_CHECK(check_pipe_ioctl, cred, pipe, pipe->pipe_label, cmd, data);
2559100979Srwatson
2560100979Srwatson	return (error);
2561100979Srwatson}
2562100979Srwatson
2563100979Srwatsonint
2564102115Srwatsonmac_check_pipe_poll(struct ucred *cred, struct pipe *pipe)
2565100979Srwatson{
2566100979Srwatson	int error;
2567100979Srwatson
2568102115Srwatson	MAC_CHECK(check_pipe_poll, cred, pipe, pipe->pipe_label);
2569100979Srwatson
2570100979Srwatson	return (error);
2571100979Srwatson}
2572100979Srwatson
2573102115Srwatsonint
2574102115Srwatsonmac_check_pipe_read(struct ucred *cred, struct pipe *pipe)
2575102115Srwatson{
2576102115Srwatson	int error;
2577102115Srwatson
2578102115Srwatson	MAC_CHECK(check_pipe_read, cred, pipe, pipe->pipe_label);
2579102115Srwatson
2580102115Srwatson	return (error);
2581102115Srwatson}
2582102115Srwatson
2583100979Srwatsonstatic int
2584100979Srwatsonmac_check_pipe_relabel(struct ucred *cred, struct pipe *pipe,
2585100979Srwatson    struct label *newlabel)
2586100979Srwatson{
2587100979Srwatson	int error;
2588100979Srwatson
2589100979Srwatson	MAC_CHECK(check_pipe_relabel, cred, pipe, pipe->pipe_label, newlabel);
2590100979Srwatson
2591100979Srwatson	return (error);
2592100979Srwatson}
2593100979Srwatson
2594100979Srwatsonint
2595102115Srwatsonmac_check_pipe_stat(struct ucred *cred, struct pipe *pipe)
2596102115Srwatson{
2597102115Srwatson	int error;
2598102115Srwatson
2599102115Srwatson	MAC_CHECK(check_pipe_stat, cred, pipe, pipe->pipe_label);
2600102115Srwatson
2601102115Srwatson	return (error);
2602102115Srwatson}
2603102115Srwatson
2604102115Srwatsonint
2605102115Srwatsonmac_check_pipe_write(struct ucred *cred, struct pipe *pipe)
2606102115Srwatson{
2607102115Srwatson	int error;
2608102115Srwatson
2609102115Srwatson	MAC_CHECK(check_pipe_write, cred, pipe, pipe->pipe_label);
2610102115Srwatson
2611102115Srwatson	return (error);
2612102115Srwatson}
2613102115Srwatson
2614102115Srwatsonint
2615100979Srwatsonmac_check_proc_debug(struct ucred *cred, struct proc *proc)
2616100979Srwatson{
2617100979Srwatson	int error;
2618100979Srwatson
2619102103Srwatson	PROC_LOCK_ASSERT(proc, MA_OWNED);
2620102103Srwatson
2621100979Srwatson	if (!mac_enforce_process)
2622100979Srwatson		return (0);
2623100979Srwatson
2624100979Srwatson	MAC_CHECK(check_proc_debug, cred, proc);
2625100979Srwatson
2626100979Srwatson	return (error);
2627100979Srwatson}
2628100979Srwatson
2629100979Srwatsonint
2630100979Srwatsonmac_check_proc_sched(struct ucred *cred, struct proc *proc)
2631100979Srwatson{
2632100979Srwatson	int error;
2633100979Srwatson
2634102103Srwatson	PROC_LOCK_ASSERT(proc, MA_OWNED);
2635102103Srwatson
2636100979Srwatson	if (!mac_enforce_process)
2637100979Srwatson		return (0);
2638100979Srwatson
2639100979Srwatson	MAC_CHECK(check_proc_sched, cred, proc);
2640100979Srwatson
2641100979Srwatson	return (error);
2642100979Srwatson}
2643100979Srwatson
2644100979Srwatsonint
2645100979Srwatsonmac_check_proc_signal(struct ucred *cred, struct proc *proc, int signum)
2646100979Srwatson{
2647100979Srwatson	int error;
2648100979Srwatson
2649102103Srwatson	PROC_LOCK_ASSERT(proc, MA_OWNED);
2650102103Srwatson
2651100979Srwatson	if (!mac_enforce_process)
2652100979Srwatson		return (0);
2653100979Srwatson
2654100979Srwatson	MAC_CHECK(check_proc_signal, cred, proc, signum);
2655100979Srwatson
2656100979Srwatson	return (error);
2657100979Srwatson}
2658100979Srwatson
2659100979Srwatsonint
2660100979Srwatsonmac_check_socket_bind(struct ucred *ucred, struct socket *socket,
2661100979Srwatson    struct sockaddr *sockaddr)
2662100979Srwatson{
2663100979Srwatson	int error;
2664100979Srwatson
2665100979Srwatson	if (!mac_enforce_socket)
2666100979Srwatson		return (0);
2667100979Srwatson
2668100979Srwatson	MAC_CHECK(check_socket_bind, ucred, socket, &socket->so_label,
2669100979Srwatson	    sockaddr);
2670100979Srwatson
2671100979Srwatson	return (error);
2672100979Srwatson}
2673100979Srwatson
2674100979Srwatsonint
2675100979Srwatsonmac_check_socket_connect(struct ucred *cred, struct socket *socket,
2676100979Srwatson    struct sockaddr *sockaddr)
2677100979Srwatson{
2678100979Srwatson	int error;
2679100979Srwatson
2680100979Srwatson	if (!mac_enforce_socket)
2681100979Srwatson		return (0);
2682100979Srwatson
2683100979Srwatson	MAC_CHECK(check_socket_connect, cred, socket, &socket->so_label,
2684100979Srwatson	    sockaddr);
2685100979Srwatson
2686100979Srwatson	return (error);
2687100979Srwatson}
2688100979Srwatson
2689100979Srwatsonint
2690101933Srwatsonmac_check_socket_deliver(struct socket *socket, struct mbuf *mbuf)
2691100979Srwatson{
2692100979Srwatson	int error;
2693100979Srwatson
2694100979Srwatson	if (!mac_enforce_socket)
2695100979Srwatson		return (0);
2696100979Srwatson
2697101933Srwatson	MAC_CHECK(check_socket_deliver, socket, &socket->so_label, mbuf,
2698101933Srwatson	    &mbuf->m_pkthdr.label);
2699101933Srwatson
2700100979Srwatson	return (error);
2701100979Srwatson}
2702100979Srwatson
2703100979Srwatsonint
2704101933Srwatsonmac_check_socket_listen(struct ucred *cred, struct socket *socket)
2705100979Srwatson{
2706100979Srwatson	int error;
2707100979Srwatson
2708100979Srwatson	if (!mac_enforce_socket)
2709100979Srwatson		return (0);
2710100979Srwatson
2711101933Srwatson	MAC_CHECK(check_socket_listen, cred, socket, &socket->so_label);
2712100979Srwatson	return (error);
2713100979Srwatson}
2714100979Srwatson
2715100979Srwatsonstatic int
2716100979Srwatsonmac_check_socket_relabel(struct ucred *cred, struct socket *socket,
2717100979Srwatson    struct label *newlabel)
2718100979Srwatson{
2719100979Srwatson	int error;
2720100979Srwatson
2721100979Srwatson	MAC_CHECK(check_socket_relabel, cred, socket, &socket->so_label,
2722100979Srwatson	    newlabel);
2723100979Srwatson
2724100979Srwatson	return (error);
2725100979Srwatson}
2726100979Srwatson
2727100979Srwatsonint
2728100979Srwatsonmac_check_socket_visible(struct ucred *cred, struct socket *socket)
2729100979Srwatson{
2730100979Srwatson	int error;
2731100979Srwatson
2732100979Srwatson	if (!mac_enforce_socket)
2733100979Srwatson		return (0);
2734100979Srwatson
2735100979Srwatson	MAC_CHECK(check_socket_visible, cred, socket, &socket->so_label);
2736100979Srwatson
2737100979Srwatson	return (error);
2738100979Srwatson}
2739100979Srwatson
2740100979Srwatsonint
2741100979Srwatsonmac_ioctl_ifnet_get(struct ucred *cred, struct ifreq *ifr,
2742100979Srwatson    struct ifnet *ifnet)
2743100979Srwatson{
2744100979Srwatson	struct mac label;
2745100979Srwatson	int error;
2746100979Srwatson
2747100979Srwatson	error = mac_externalize(&ifnet->if_label, &label);
2748100979Srwatson	if (error)
2749100979Srwatson		return (error);
2750100979Srwatson
2751100979Srwatson	return (copyout(&label, ifr->ifr_ifru.ifru_data, sizeof(label)));
2752100979Srwatson}
2753100979Srwatson
2754100979Srwatsonint
2755100979Srwatsonmac_ioctl_ifnet_set(struct ucred *cred, struct ifreq *ifr,
2756100979Srwatson    struct ifnet *ifnet)
2757100979Srwatson{
2758100979Srwatson	struct mac newlabel;
2759100979Srwatson	struct label intlabel;
2760100979Srwatson	int error;
2761100979Srwatson
2762100979Srwatson	error = copyin(ifr->ifr_ifru.ifru_data, &newlabel, sizeof(newlabel));
2763100979Srwatson	if (error)
2764100979Srwatson		return (error);
2765100979Srwatson
2766100979Srwatson	error = mac_internalize(&intlabel, &newlabel);
2767100979Srwatson	if (error)
2768100979Srwatson		return (error);
2769100979Srwatson
2770100979Srwatson	/*
2771100979Srwatson	 * XXX: Note that this is a redundant privilege check, since
2772100979Srwatson	 * policies impose this check themselves if required by the
2773100979Srwatson	 * policy.  Eventually, this should go away.
2774100979Srwatson	 */
2775100979Srwatson	error = suser_cred(cred, 0);
2776100979Srwatson	if (error)
2777100979Srwatson		goto out;
2778100979Srwatson
2779100979Srwatson	MAC_CHECK(check_ifnet_relabel, cred, ifnet, &ifnet->if_label,
2780100979Srwatson	    &intlabel);
2781100979Srwatson	if (error)
2782100979Srwatson		goto out;
2783100979Srwatson
2784100979Srwatson	MAC_PERFORM(relabel_ifnet, cred, ifnet, &ifnet->if_label, &intlabel);
2785100979Srwatson
2786100979Srwatsonout:
2787100979Srwatson	mac_destroy_temp(&intlabel);
2788100979Srwatson	return (error);
2789100979Srwatson}
2790100979Srwatson
2791100979Srwatsonvoid
2792100979Srwatsonmac_create_devfs_vnode(struct devfs_dirent *de, struct vnode *vp)
2793100979Srwatson{
2794100979Srwatson
2795100979Srwatson	MAC_PERFORM(create_devfs_vnode, de, &de->de_label, vp, &vp->v_label);
2796100979Srwatson}
2797100979Srwatson
2798100979Srwatsonvoid
2799100979Srwatsonmac_create_devfs_device(dev_t dev, struct devfs_dirent *de)
2800100979Srwatson{
2801100979Srwatson
2802100979Srwatson	MAC_PERFORM(create_devfs_device, dev, de, &de->de_label);
2803100979Srwatson}
2804100979Srwatson
2805100979Srwatsonstatic int
2806100979Srwatsonmac_stdcreatevnode_ea(struct vnode *vp)
2807100979Srwatson{
2808100979Srwatson	int error;
2809100979Srwatson
2810100979Srwatson	MAC_CHECK(stdcreatevnode_ea, vp, &vp->v_label);
2811100979Srwatson
2812100979Srwatson	return (error);
2813100979Srwatson}
2814100979Srwatson
2815100979Srwatsonvoid
2816100979Srwatsonmac_create_devfs_directory(char *dirname, int dirnamelen,
2817100979Srwatson    struct devfs_dirent *de)
2818100979Srwatson{
2819100979Srwatson
2820100979Srwatson	MAC_PERFORM(create_devfs_directory, dirname, dirnamelen, de,
2821100979Srwatson	    &de->de_label);
2822100979Srwatson}
2823100979Srwatson
2824100979Srwatson/*
2825100979Srwatson * When a new vnode is created, this call will initialize its label.
2826100979Srwatson */
2827100979Srwatsonvoid
2828100979Srwatsonmac_create_vnode(struct ucred *cred, struct vnode *parent,
2829100979Srwatson    struct vnode *child)
2830100979Srwatson{
2831100979Srwatson	int error;
2832100979Srwatson
2833100979Srwatson	ASSERT_VOP_LOCKED(parent, "mac_create_vnode");
2834100979Srwatson	ASSERT_VOP_LOCKED(child, "mac_create_vnode");
2835100979Srwatson
2836100979Srwatson	error = vn_refreshlabel(parent, cred);
2837100979Srwatson	if (error) {
2838100979Srwatson		printf("mac_create_vnode: vn_refreshlabel returned %d\n",
2839100979Srwatson		    error);
2840100979Srwatson		printf("mac_create_vnode: using old vnode label\n");
2841100979Srwatson	}
2842100979Srwatson
2843100979Srwatson	MAC_PERFORM(create_vnode, cred, parent, &parent->v_label, child,
2844100979Srwatson	    &child->v_label);
2845100979Srwatson}
2846100979Srwatson
2847100979Srwatsonint
2848100979Srwatsonmac_setsockopt_label_set(struct ucred *cred, struct socket *so,
2849100979Srwatson    struct mac *extmac)
2850100979Srwatson{
2851100979Srwatson	struct label intlabel;
2852100979Srwatson	int error;
2853100979Srwatson
2854100979Srwatson	error = mac_internalize(&intlabel, extmac);
2855100979Srwatson	if (error)
2856100979Srwatson		return (error);
2857100979Srwatson
2858100979Srwatson	mac_check_socket_relabel(cred, so, &intlabel);
2859100979Srwatson	if (error) {
2860100979Srwatson		mac_destroy_temp(&intlabel);
2861100979Srwatson		return (error);
2862100979Srwatson	}
2863100979Srwatson
2864100979Srwatson	mac_relabel_socket(cred, so, &intlabel);
2865100979Srwatson
2866100979Srwatson	mac_destroy_temp(&intlabel);
2867100979Srwatson	return (0);
2868100979Srwatson}
2869100979Srwatson
2870100979Srwatsonint
2871100979Srwatsonmac_pipe_label_set(struct ucred *cred, struct pipe *pipe, struct label *label)
2872100979Srwatson{
2873100979Srwatson	int error;
2874100979Srwatson
2875100979Srwatson	error = mac_check_pipe_relabel(cred, pipe, label);
2876100979Srwatson	if (error)
2877100979Srwatson		return (error);
2878100979Srwatson
2879100979Srwatson	mac_relabel_pipe(cred, pipe, label);
2880100979Srwatson
2881100979Srwatson	return (0);
2882100979Srwatson}
2883100979Srwatson
2884100979Srwatsonint
2885100979Srwatsonmac_getsockopt_label_get(struct ucred *cred, struct socket *so,
2886100979Srwatson    struct mac *extmac)
2887100979Srwatson{
2888100979Srwatson
2889100979Srwatson	return (mac_externalize(&so->so_label, extmac));
2890100979Srwatson}
2891100979Srwatson
2892100979Srwatsonint
2893100979Srwatsonmac_getsockopt_peerlabel_get(struct ucred *cred, struct socket *so,
2894100979Srwatson    struct mac *extmac)
2895100979Srwatson{
2896100979Srwatson
2897100979Srwatson	return (mac_externalize(&so->so_peerlabel, extmac));
2898100979Srwatson}
2899100979Srwatson
2900100979Srwatson/*
2901100979Srwatson * Implementation of VOP_SETLABEL() that relies on extended attributes
2902100979Srwatson * to store label data.  Can be referenced by filesystems supporting
2903100979Srwatson * extended attributes.
2904100979Srwatson */
2905100979Srwatsonint
2906100979Srwatsonvop_stdsetlabel_ea(struct vop_setlabel_args *ap)
2907100979Srwatson{
2908100979Srwatson	struct vnode *vp = ap->a_vp;
2909100979Srwatson	struct label *intlabel = ap->a_label;
2910100979Srwatson	struct mac extmac;
2911100979Srwatson	int error;
2912100979Srwatson
2913100979Srwatson	ASSERT_VOP_LOCKED(vp, "vop_stdsetlabel_ea");
2914100979Srwatson
2915100979Srwatson	/*
2916100979Srwatson	 * XXX: Eventually call out to EA check/set calls here.
2917100979Srwatson	 * Be particularly careful to avoid race conditions,
2918100979Srwatson	 * consistency problems, and stability problems when
2919100979Srwatson	 * dealing with multiple EAs.  In particular, we require
2920100979Srwatson	 * the ability to write multiple EAs on the same file in
2921100979Srwatson	 * a single transaction, which the current EA interface
2922100979Srwatson	 * does not provide.
2923100979Srwatson	 */
2924100979Srwatson
2925100979Srwatson	error = mac_externalize(intlabel, &extmac);
2926100979Srwatson	if (error)
2927100979Srwatson		return (error);
2928100979Srwatson
2929100979Srwatson	error = vn_extattr_set(vp, IO_NODELOCKED,
2930100979Srwatson	    FREEBSD_MAC_EXTATTR_NAMESPACE, FREEBSD_MAC_EXTATTR_NAME,
2931100979Srwatson	    sizeof(extmac), (char *)&extmac, curthread);
2932100979Srwatson	if (error)
2933100979Srwatson		return (error);
2934100979Srwatson
2935100979Srwatson	mac_relabel_vnode(ap->a_cred, vp, intlabel);
2936100979Srwatson
2937101308Sjeff	vp->v_vflag |= VV_CACHEDLABEL;
2938100979Srwatson
2939100979Srwatson	return (0);
2940100979Srwatson}
2941100979Srwatson
2942100979Srwatsonstatic int
2943100979Srwatsonvn_setlabel(struct vnode *vp, struct label *intlabel, struct ucred *cred)
2944100979Srwatson{
2945100979Srwatson	int error;
2946100979Srwatson
2947100979Srwatson	if (vp->v_mount == NULL) {
2948100979Srwatson		/* printf("vn_setlabel: null v_mount\n"); */
2949100979Srwatson		if (vp->v_tag != VT_NON)
2950100979Srwatson			printf("vn_setlabel: null v_mount with non-VT_NON\n");
2951100979Srwatson		return (EBADF);
2952100979Srwatson	}
2953100979Srwatson
2954100979Srwatson	if ((vp->v_mount->mnt_flag & MNT_MULTILABEL) == 0)
2955100979Srwatson		return (EOPNOTSUPP);
2956100979Srwatson
2957100979Srwatson	/*
2958100979Srwatson	 * Multi-phase commit.  First check the policies to confirm the
2959100979Srwatson	 * change is OK.  Then commit via the filesystem.  Finally,
2960100979Srwatson	 * update the actual vnode label.  Question: maybe the filesystem
2961100979Srwatson	 * should update the vnode at the end as part of VOP_SETLABEL()?
2962100979Srwatson	 */
2963100979Srwatson	error = mac_check_vnode_relabel(cred, vp, intlabel);
2964100979Srwatson	if (error)
2965100979Srwatson		return (error);
2966100979Srwatson
2967100979Srwatson	/*
2968100979Srwatson	 * VADMIN provides the opportunity for the filesystem to make
2969100979Srwatson	 * decisions about who is and is not able to modify labels
2970100979Srwatson	 * and protections on files.  This might not be right.  We can't
2971100979Srwatson	 * assume VOP_SETLABEL() will do it, because we might implement
2972100979Srwatson	 * that as part of vop_stdsetlabel_ea().
2973100979Srwatson	 */
2974100979Srwatson	error = VOP_ACCESS(vp, VADMIN, cred, curthread);
2975100979Srwatson	if (error)
2976100979Srwatson		return (error);
2977100979Srwatson
2978100979Srwatson	error = VOP_SETLABEL(vp, intlabel, cred, curthread);
2979100979Srwatson	if (error)
2980100979Srwatson		return (error);
2981100979Srwatson
2982100979Srwatson	return (0);
2983100979Srwatson}
2984100979Srwatson
2985100979Srwatson/*
2986100979Srwatson * MPSAFE
2987100979Srwatson */
2988100979Srwatsonint
2989100894Srwatson__mac_get_proc(struct thread *td, struct __mac_get_proc_args *uap)
2990100894Srwatson{
2991100979Srwatson	struct mac extmac;
2992100979Srwatson	int error;
2993100894Srwatson
2994100979Srwatson	error = mac_externalize(&td->td_ucred->cr_label, &extmac);
2995100979Srwatson	if (error == 0)
2996100979Srwatson		error = copyout(&extmac, SCARG(uap, mac_p), sizeof(extmac));
2997100979Srwatson
2998100979Srwatson	return (error);
2999100979Srwatson}
3000100979Srwatson
3001100979Srwatson/*
3002100979Srwatson * MPSAFE
3003100979Srwatson */
3004100979Srwatsonint
3005100979Srwatson__mac_set_proc(struct thread *td, struct __mac_set_proc_args *uap)
3006100979Srwatson{
3007100979Srwatson	struct ucred *newcred, *oldcred;
3008100979Srwatson	struct proc *p;
3009100979Srwatson	struct mac extmac;
3010100979Srwatson	struct label intlabel;
3011100979Srwatson	int error;
3012100979Srwatson
3013100979Srwatson	error = copyin(SCARG(uap, mac_p), &extmac, sizeof(extmac));
3014100979Srwatson	if (error)
3015100979Srwatson		return (error);
3016100979Srwatson
3017100979Srwatson	error = mac_internalize(&intlabel, &extmac);
3018100979Srwatson	if (error)
3019100979Srwatson		return (error);
3020100979Srwatson
3021100979Srwatson	newcred = crget();
3022100979Srwatson
3023100979Srwatson	p = td->td_proc;
3024100979Srwatson	PROC_LOCK(p);
3025100979Srwatson	oldcred = p->p_ucred;
3026100979Srwatson
3027100979Srwatson	error = mac_check_cred_relabel(oldcred, &intlabel);
3028100979Srwatson	if (error) {
3029100979Srwatson		PROC_UNLOCK(p);
3030100979Srwatson		mac_destroy_temp(&intlabel);
3031100979Srwatson		crfree(newcred);
3032100979Srwatson		return (error);
3033100979Srwatson	}
3034100979Srwatson
3035100979Srwatson	setsugid(p);
3036100979Srwatson	crcopy(newcred, oldcred);
3037100979Srwatson	mac_relabel_cred(newcred, &intlabel);
3038102136Srwatson	p->p_ucred = newcred;
3039100979Srwatson
3040102136Srwatson	/*
3041102136Srwatson	 * Grab additional reference for use while revoking mmaps, prior
3042102136Srwatson	 * to releasing the proc lock and sharing the cred.
3043102136Srwatson	 */
3044102136Srwatson	crhold(newcred);
3045100979Srwatson	PROC_UNLOCK(p);
3046102136Srwatson
3047102136Srwatson	mac_cred_mmapped_drop_perms(td, newcred);
3048102136Srwatson
3049102136Srwatson	crfree(newcred);	/* Free revocation reference. */
3050100979Srwatson	crfree(oldcred);
3051100979Srwatson	mac_destroy_temp(&intlabel);
3052100979Srwatson	return (0);
3053100979Srwatson}
3054100979Srwatson
3055100979Srwatson/*
3056100979Srwatson * MPSAFE
3057100979Srwatson */
3058100979Srwatsonint
3059100979Srwatson__mac_get_fd(struct thread *td, struct __mac_get_fd_args *uap)
3060100979Srwatson{
3061100979Srwatson	struct file *fp;
3062100979Srwatson	struct mac extmac;
3063100979Srwatson	struct vnode *vp;
3064100979Srwatson	struct pipe *pipe;
3065100979Srwatson	int error;
3066100979Srwatson
3067100979Srwatson	mtx_lock(&Giant);
3068100979Srwatson
3069100979Srwatson	error = fget(td, SCARG(uap, fd), &fp);
3070100979Srwatson	if (error)
3071100979Srwatson		goto out;
3072100979Srwatson
3073100979Srwatson	switch (fp->f_type) {
3074100979Srwatson	case DTYPE_FIFO:
3075100979Srwatson	case DTYPE_VNODE:
3076100979Srwatson		vp = (struct vnode *)fp->f_data;
3077100979Srwatson
3078100979Srwatson		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3079100979Srwatson		error = vn_refreshlabel(vp, td->td_ucred);
3080100979Srwatson		if (error == 0)
3081100979Srwatson			error = mac_externalize(&vp->v_label, &extmac);
3082100979Srwatson		VOP_UNLOCK(vp, 0, td);
3083100979Srwatson		break;
3084100979Srwatson	case DTYPE_PIPE:
3085100979Srwatson		pipe = (struct pipe *)fp->f_data;
3086100979Srwatson		error = mac_externalize(pipe->pipe_label, &extmac);
3087100979Srwatson		break;
3088100979Srwatson	default:
3089100979Srwatson		error = EINVAL;
3090100979Srwatson	}
3091100979Srwatson
3092100979Srwatson	if (error == 0)
3093100979Srwatson		error = copyout(&extmac, SCARG(uap, mac_p), sizeof(extmac));
3094100979Srwatson
3095100979Srwatson	fdrop(fp, td);
3096100979Srwatson
3097100979Srwatsonout:
3098100979Srwatson	mtx_unlock(&Giant);
3099100979Srwatson	return (error);
3100100979Srwatson}
3101100979Srwatson
3102100979Srwatson/*
3103100979Srwatson * MPSAFE
3104100979Srwatson */
3105100979Srwatsonint
3106100979Srwatson__mac_get_file(struct thread *td, struct __mac_get_file_args *uap)
3107100979Srwatson{
3108100979Srwatson	struct nameidata nd;
3109100979Srwatson	struct mac extmac;
3110100979Srwatson	int error;
3111100979Srwatson
3112100979Srwatson	mtx_lock(&Giant);
3113100979Srwatson	NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_USERSPACE,
3114100979Srwatson	    SCARG(uap, path_p), td);
3115100979Srwatson	error = namei(&nd);
3116100979Srwatson	if (error)
3117100979Srwatson		goto out;
3118100979Srwatson
3119100979Srwatson	error = vn_refreshlabel(nd.ni_vp, td->td_ucred);
3120100979Srwatson	if (error == 0)
3121100979Srwatson		error = mac_externalize(&nd.ni_vp->v_label, &extmac);
3122100979Srwatson	NDFREE(&nd, 0);
3123100979Srwatson	if (error)
3124100979Srwatson		goto out;
3125100979Srwatson
3126100979Srwatson	error = copyout(&extmac, SCARG(uap, mac_p), sizeof(extmac));
3127100979Srwatson
3128100979Srwatsonout:
3129100979Srwatson	mtx_unlock(&Giant);
3130100979Srwatson	return (error);
3131100979Srwatson}
3132100979Srwatson
3133100979Srwatson/*
3134100979Srwatson * MPSAFE
3135100979Srwatson */
3136100979Srwatsonint
3137100979Srwatson__mac_set_fd(struct thread *td, struct __mac_set_fd_args *uap)
3138100979Srwatson{
3139100979Srwatson	struct file *fp;
3140100979Srwatson	struct mac extmac;
3141100979Srwatson	struct label intlabel;
3142100979Srwatson	struct mount *mp;
3143100979Srwatson	struct vnode *vp;
3144100979Srwatson	struct pipe *pipe;
3145100979Srwatson	int error;
3146100979Srwatson
3147100979Srwatson	mtx_lock(&Giant);
3148100979Srwatson	error = fget(td, SCARG(uap, fd), &fp);
3149100979Srwatson	if (error)
3150100979Srwatson		goto out1;
3151100979Srwatson
3152100979Srwatson	error = copyin(SCARG(uap, mac_p), &extmac, sizeof(extmac));
3153100979Srwatson	if (error)
3154100979Srwatson		goto out2;
3155100979Srwatson
3156100979Srwatson	error = mac_internalize(&intlabel, &extmac);
3157100979Srwatson	if (error)
3158100979Srwatson		goto out2;
3159100979Srwatson
3160100979Srwatson	switch (fp->f_type) {
3161100979Srwatson	case DTYPE_FIFO:
3162100979Srwatson	case DTYPE_VNODE:
3163100979Srwatson		vp = (struct vnode *)fp->f_data;
3164100979Srwatson		error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3165100979Srwatson		if (error != 0)
3166100979Srwatson			break;
3167100979Srwatson
3168100979Srwatson		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3169100979Srwatson		error = vn_setlabel(vp, &intlabel, td->td_ucred);
3170100979Srwatson		VOP_UNLOCK(vp, 0, td);
3171100979Srwatson		vn_finished_write(mp);
3172100979Srwatson		mac_destroy_temp(&intlabel);
3173100979Srwatson		break;
3174100979Srwatson	case DTYPE_PIPE:
3175100979Srwatson		pipe = (struct pipe *)fp->f_data;
3176100979Srwatson		error = mac_pipe_label_set(td->td_ucred, pipe, &intlabel);
3177100979Srwatson		break;
3178100979Srwatson	default:
3179100979Srwatson		error = EINVAL;
3180100979Srwatson	}
3181100979Srwatson
3182100979Srwatsonout2:
3183100979Srwatson	fdrop(fp, td);
3184100979Srwatsonout1:
3185100979Srwatson	mtx_unlock(&Giant);
3186100979Srwatson	return (error);
3187100979Srwatson}
3188100979Srwatson
3189100979Srwatson/*
3190100979Srwatson * MPSAFE
3191100979Srwatson */
3192100979Srwatsonint
3193100979Srwatson__mac_set_file(struct thread *td, struct __mac_set_file_args *uap)
3194100979Srwatson{
3195100979Srwatson	struct nameidata nd;
3196100979Srwatson	struct mac extmac;
3197100979Srwatson	struct label intlabel;
3198100979Srwatson	struct mount *mp;
3199100979Srwatson	int error;
3200100979Srwatson
3201100979Srwatson	mtx_lock(&Giant);
3202100979Srwatson
3203100979Srwatson	error = copyin(SCARG(uap, mac_p), &extmac, sizeof(extmac));
3204100979Srwatson	if (error)
3205100979Srwatson		goto out;
3206100979Srwatson
3207100979Srwatson	error = mac_internalize(&intlabel, &extmac);
3208100979Srwatson	if (error)
3209100979Srwatson		goto out;
3210100979Srwatson
3211100979Srwatson	NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_USERSPACE,
3212100979Srwatson	    SCARG(uap, path_p), td);
3213100979Srwatson	error = namei(&nd);
3214100979Srwatson	if (error)
3215100979Srwatson		goto out2;
3216100979Srwatson	error = vn_start_write(nd.ni_vp, &mp, V_WAIT | PCATCH);
3217100979Srwatson	if (error)
3218100979Srwatson		goto out2;
3219100979Srwatson
3220100979Srwatson	error = vn_setlabel(nd.ni_vp, &intlabel, td->td_ucred);
3221100979Srwatson
3222100979Srwatson	vn_finished_write(mp);
3223100979Srwatsonout2:
3224100979Srwatson	mac_destroy_temp(&intlabel);
3225100979Srwatson	NDFREE(&nd, 0);
3226100979Srwatsonout:
3227100979Srwatson	mtx_unlock(&Giant);
3228100979Srwatson	return (error);
3229100979Srwatson}
3230100979Srwatson
3231102123Srwatsonint
3232102123Srwatsonmac_syscall(struct thread *td, struct mac_syscall_args *uap)
3233102123Srwatson{
3234102123Srwatson	struct mac_policy_conf *mpc;
3235102123Srwatson	char target[MAC_MAX_POLICY_NAME];
3236102123Srwatson	int error;
3237102123Srwatson
3238102123Srwatson	error = copyinstr(SCARG(uap, policy), target, sizeof(target), NULL);
3239102123Srwatson	if (error)
3240102123Srwatson		return (error);
3241102123Srwatson
3242102123Srwatson	error = ENOSYS;
3243102123Srwatson	MAC_POLICY_LIST_BUSY();
3244102123Srwatson	LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {
3245102123Srwatson		if (strcmp(mpc->mpc_name, target) == 0 &&
3246102123Srwatson		    mpc->mpc_ops->mpo_syscall != NULL) {
3247102123Srwatson			error = mpc->mpc_ops->mpo_syscall(td,
3248102123Srwatson			    SCARG(uap, call), SCARG(uap, arg));
3249102123Srwatson			goto out;
3250102123Srwatson		}
3251102123Srwatson	}
3252102123Srwatson
3253102123Srwatsonout:
3254102123Srwatson	MAC_POLICY_LIST_UNBUSY();
3255102123Srwatson	return (error);
3256102123Srwatson}
3257102123Srwatson
3258100979SrwatsonSYSINIT(mac, SI_SUB_MAC, SI_ORDER_FIRST, mac_init, NULL);
3259100979SrwatsonSYSINIT(mac_late, SI_SUB_MAC_LATE, SI_ORDER_FIRST, mac_late_init, NULL);
3260100979Srwatson
3261100979Srwatson#else /* !MAC */
3262100979Srwatson
3263100979Srwatsonint
3264100979Srwatson__mac_get_proc(struct thread *td, struct __mac_get_proc_args *uap)
3265100979Srwatson{
3266100979Srwatson
3267100894Srwatson	return (ENOSYS);
3268100894Srwatson}
3269100894Srwatson
3270100894Srwatsonint
3271100894Srwatson__mac_set_proc(struct thread *td, struct __mac_set_proc_args *uap)
3272100894Srwatson{
3273100894Srwatson
3274100894Srwatson	return (ENOSYS);
3275100894Srwatson}
3276100894Srwatson
3277100894Srwatsonint
3278100894Srwatson__mac_get_fd(struct thread *td, struct __mac_get_fd_args *uap)
3279100894Srwatson{
3280100894Srwatson
3281100894Srwatson	return (ENOSYS);
3282100894Srwatson}
3283100894Srwatson
3284100894Srwatsonint
3285100894Srwatson__mac_get_file(struct thread *td, struct __mac_get_file_args *uap)
3286100894Srwatson{
3287100894Srwatson
3288100894Srwatson	return (ENOSYS);
3289100894Srwatson}
3290100894Srwatson
3291100894Srwatsonint
3292100894Srwatson__mac_set_fd(struct thread *td, struct __mac_set_fd_args *uap)
3293100894Srwatson{
3294100894Srwatson
3295100894Srwatson	return (ENOSYS);
3296100894Srwatson}
3297100894Srwatson
3298100894Srwatsonint
3299100894Srwatson__mac_set_file(struct thread *td, struct __mac_set_file_args *uap)
3300100894Srwatson{
3301100894Srwatson
3302100894Srwatson	return (ENOSYS);
3303100894Srwatson}
3304100979Srwatson
3305102123Srwatsonint
3306102123Srwatsonmac_syscall(struct thread *td, struct mac_syscall_args *uap)
3307102123Srwatson{
3308102123Srwatson
3309102123Srwatson	return (ENOSYS);
3310102123Srwatson}
3311102123Srwatson
3312100979Srwatson#endif /* !MAC */
3313