2 * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson
3 * Copyright (c) 2001 Ilmar S. Habibulin
4 * Copyright (c) 2001, 2002 Networks Associates Technology, Inc.
7 * This software was developed by Robert Watson and Ilmar Habibulin for the
10 * This software was developed for the FreeBSD Project in part by NAI Labs,
11 * the Security Research Division of Network Associates, Inc. under
12 * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
13 * CHATS research program.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. The names of the authors may not be used to endorse or promote
24 * products derived from this software without specific prior written
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * Developed by the TrustedBSD Project.
44 * Framework for extensible kernel access control. Kernel and userland
45 * interface to the framework, policy registration and composition.
49 #include "opt_devfs.h"
51 #include <sys/param.h>
52 #include <sys/extattr.h>
53 #include <sys/kernel.h>
55 #include <sys/malloc.h>
56 #include <sys/mutex.h>
58 #include <sys/module.h>
60 #include <sys/systm.h>
61 #include <sys/sysproto.h>
62 #include <sys/sysent.h>
63 #include <sys/vnode.h>
64 #include <sys/mount.h>
66 #include <sys/namei.h>
67 #include <sys/socket.h>
69 #include <sys/socketvar.h>
70 #include <sys/sysctl.h>
74 #include <vm/vm_map.h>
75 #include <vm/vm_object.h>
77 #include <sys/mac_policy.h>
79 #include <fs/devfs/devfs.h>
81 #include <net/bpfdesc.h>
83 #include <net/if_var.h>
85 #include <netinet/in.h>
86 #include <netinet/ip_var.h>
91 * Declare that the kernel provides MAC support, version 1. This permits
92 * modules to refuse to be loaded if the necessary support isn't present,
93 * even if it's pre-boot.
95 MODULE_VERSION(kernel_mac_support, 1);
97 SYSCTL_DECL(_security);
99 SYSCTL_NODE(_security, OID_AUTO, mac, CTLFLAG_RW, 0,
100 "TrustedBSD MAC policy controls");
102 #if MAC_MAX_POLICIES > 32
103 #error "MAC_MAX_POLICIES too large"
106 static unsigned int mac_max_policies = MAC_MAX_POLICIES;
107 static unsigned int mac_policy_offsets_free = (1 << MAC_MAX_POLICIES) - 1;
108 SYSCTL_UINT(_security_mac, OID_AUTO, max_policies, CTLFLAG_RD,
109 &mac_max_policies, 0, "");
111 static int mac_late = 0;
113 static int mac_enforce_fs = 1;
114 SYSCTL_INT(_security_mac, OID_AUTO, enforce_fs, CTLFLAG_RW,
115 &mac_enforce_fs, 0, "Enforce MAC policy on file system objects");
116 TUNABLE_INT("security.mac.enforce_fs", &mac_enforce_fs);
118 static int mac_enforce_network = 1;
119 SYSCTL_INT(_security_mac, OID_AUTO, enforce_network, CTLFLAG_RW,
120 &mac_enforce_network, 0, "Enforce MAC policy on network packets");
121 TUNABLE_INT("security.mac.enforce_network", &mac_enforce_network);
123 static int mac_enforce_pipe = 1;
124 SYSCTL_INT(_security_mac, OID_AUTO, enforce_pipe, CTLFLAG_RW,
125 &mac_enforce_pipe, 0, "Enforce MAC policy on pipe operations");
126 TUNABLE_INT("security.mac.enforce_pipe", &mac_enforce_pipe);
128 static int mac_enforce_process = 1;
129 SYSCTL_INT(_security_mac, OID_AUTO, enforce_process, CTLFLAG_RW,
130 &mac_enforce_process, 0, "Enforce MAC policy on inter-process operations");
131 TUNABLE_INT("security.mac.enforce_process", &mac_enforce_process);
133 static int mac_enforce_socket = 1;
134 SYSCTL_INT(_security_mac, OID_AUTO, enforce_socket, CTLFLAG_RW,
135 &mac_enforce_socket, 0, "Enforce MAC policy on socket operations");
136 TUNABLE_INT("security.mac.enforce_socket", &mac_enforce_socket);
138 static int mac_enforce_vm = 1;
139 SYSCTL_INT(_security_mac, OID_AUTO, enforce_vm, CTLFLAG_RW,
140 &mac_enforce_vm, 0, "Enforce MAC policy on vm operations");
141 TUNABLE_INT("security.mac.enforce_vm", &mac_enforce_vm);
143 static int mac_label_size = sizeof(struct oldmac);
144 SYSCTL_INT(_security_mac, OID_AUTO, label_size, CTLFLAG_RD,
145 &mac_label_size, 0, "Pre-compiled MAC label size");
147 static int mac_cache_fslabel_in_vnode = 1;
148 SYSCTL_INT(_security_mac, OID_AUTO, cache_fslabel_in_vnode, CTLFLAG_RW,
149 &mac_cache_fslabel_in_vnode, 0, "Cache mount fslabel in vnode");
150 TUNABLE_INT("security.mac.cache_fslabel_in_vnode",
151 &mac_cache_fslabel_in_vnode);
153 static int mac_vnode_label_cache_hits = 0;
154 SYSCTL_INT(_security_mac, OID_AUTO, vnode_label_cache_hits, CTLFLAG_RD,
155 &mac_vnode_label_cache_hits, 0, "Cache hits on vnode labels");
156 static int mac_vnode_label_cache_misses = 0;
157 SYSCTL_INT(_security_mac, OID_AUTO, vnode_label_cache_misses, CTLFLAG_RD,
158 &mac_vnode_label_cache_misses, 0, "Cache misses on vnode labels");
160 static int mac_mmap_revocation = 1;
161 SYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation, CTLFLAG_RW,
162 &mac_mmap_revocation, 0, "Revoke mmap access to files on subject "
164 static int mac_mmap_revocation_via_cow = 0;
165 SYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation_via_cow, CTLFLAG_RW,
166 &mac_mmap_revocation_via_cow, 0, "Revoke mmap access to files via "
167 "copy-on-write semantics, or by removing all write access");
170 SYSCTL_NODE(_security_mac, OID_AUTO, debug, CTLFLAG_RW, 0,
171 "TrustedBSD MAC debug info");
173 static int mac_debug_label_fallback = 0;
174 SYSCTL_INT(_security_mac_debug, OID_AUTO, label_fallback, CTLFLAG_RW,
175 &mac_debug_label_fallback, 0, "Filesystems should fall back to fs label"
176 "when label is corrupted.");
177 TUNABLE_INT("security.mac.debug_label_fallback",
178 &mac_debug_label_fallback);
180 SYSCTL_NODE(_security_mac_debug, OID_AUTO, counters, CTLFLAG_RW, 0,
181 "TrustedBSD MAC object counters");
183 static unsigned int nmacmbufs, nmaccreds, nmacifnets, nmacbpfdescs,
184 nmacsockets, nmacmounts, nmactemp, nmacvnodes, nmacdevfsdirents,
187 SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, mbufs, CTLFLAG_RD,
188 &nmacmbufs, 0, "number of mbufs in use");
189 SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, creds, CTLFLAG_RD,
190 &nmaccreds, 0, "number of ucreds in use");
191 SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, ifnets, CTLFLAG_RD,
192 &nmacifnets, 0, "number of ifnets in use");
193 SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, ipqs, CTLFLAG_RD,
194 &nmacipqs, 0, "number of ipqs in use");
195 SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, bpfdescs, CTLFLAG_RD,
196 &nmacbpfdescs, 0, "number of bpfdescs in use");
197 SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, sockets, CTLFLAG_RD,
198 &nmacsockets, 0, "number of sockets in use");
199 SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, pipes, CTLFLAG_RD,
200 &nmacpipes, 0, "number of pipes in use");
201 SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, mounts, CTLFLAG_RD,
202 &nmacmounts, 0, "number of mounts in use");
203 SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, temp, CTLFLAG_RD,
204 &nmactemp, 0, "number of temporary labels in use");
205 SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, vnodes, CTLFLAG_RD,
206 &nmacvnodes, 0, "number of vnodes in use");
207 SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, devfsdirents, CTLFLAG_RD,
208 &nmacdevfsdirents, 0, "number of devfs dirents inuse");
211 static int error_select(int error1, int error2);
212 static int mac_externalize_vnode_oldmac(struct label *label,
213 struct oldmac *extmac);
214 static int mac_policy_register(struct mac_policy_conf *mpc);
215 static int mac_policy_unregister(struct mac_policy_conf *mpc);
217 static int mac_stdcreatevnode_ea(struct vnode *vp);
218 static void mac_check_vnode_mmap_downgrade(struct ucred *cred,
219 struct vnode *vp, int *prot);
220 static void mac_cred_mmapped_drop_perms_recurse(struct thread *td,
221 struct ucred *cred, struct vm_map *map);
223 static void mac_destroy_socket_label(struct label *label);
225 MALLOC_DEFINE(M_MACOPVEC, "macopvec", "MAC policy operation vector");
226 MALLOC_DEFINE(M_MACPIPELABEL, "macpipelabel", "MAC labels for pipes");
227 MALLOC_DEFINE(M_MACTEMP, "mactemp", "MAC temporary label storage");
230 * mac_policy_list_lock protects the consistency of 'mac_policy_list',
231 * the linked list of attached policy modules. Read-only consumers of
232 * the list must acquire a shared lock for the duration of their use;
233 * writers must acquire an exclusive lock. Note that for compound
234 * operations, locks should be held for the entire compound operation,
235 * and that this is not yet done for relabel requests.
237 static struct mtx mac_policy_list_lock;
238 static LIST_HEAD(, mac_policy_conf) mac_policy_list;
239 static int mac_policy_list_busy;
240 #define MAC_POLICY_LIST_LOCKINIT() mtx_init(&mac_policy_list_lock, \
241 "mac_policy_list_lock", NULL, MTX_DEF);
242 #define MAC_POLICY_LIST_LOCK() mtx_lock(&mac_policy_list_lock);
243 #define MAC_POLICY_LIST_UNLOCK() mtx_unlock(&mac_policy_list_lock);
245 #define MAC_POLICY_LIST_BUSY() do { \
246 MAC_POLICY_LIST_LOCK(); \
247 mac_policy_list_busy++; \
248 MAC_POLICY_LIST_UNLOCK(); \
251 #define MAC_POLICY_LIST_UNBUSY() do { \
252 MAC_POLICY_LIST_LOCK(); \
253 mac_policy_list_busy--; \
254 if (mac_policy_list_busy < 0) \
255 panic("Extra mac_policy_list_busy--"); \
256 MAC_POLICY_LIST_UNLOCK(); \
260 * MAC_CHECK performs the designated check by walking the policy
261 * module list and checking with each as to how it feels about the
262 * request. Note that it returns its value via 'error' in the scope
265 #define MAC_CHECK(check, args...) do { \
266 struct mac_policy_conf *mpc; \
269 MAC_POLICY_LIST_BUSY(); \
270 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \
271 if (mpc->mpc_ops->mpo_ ## check != NULL) \
272 error = error_select( \
273 mpc->mpc_ops->mpo_ ## check (args), \
276 MAC_POLICY_LIST_UNBUSY(); \
280 * MAC_BOOLEAN performs the designated boolean composition by walking
281 * the module list, invoking each instance of the operation, and
282 * combining the results using the passed C operator. Note that it
283 * returns its value via 'result' in the scope of the caller, which
284 * should be initialized by the caller in a meaningful way to get
285 * a meaningful result.
287 #define MAC_BOOLEAN(operation, composition, args...) do { \
288 struct mac_policy_conf *mpc; \
290 MAC_POLICY_LIST_BUSY(); \
291 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \
292 if (mpc->mpc_ops->mpo_ ## operation != NULL) \
293 result = result composition \
294 mpc->mpc_ops->mpo_ ## operation (args); \
296 MAC_POLICY_LIST_UNBUSY(); \
299 #define MAC_EXTERNALIZE(type, label, elementlist, outbuf, \
301 char *curptr, *curptr_start, *element_name, *element_temp; \
302 size_t left, left_start, len; \
303 int claimed, first, first_start, ignorenotfound; \
306 element_temp = elementlist; \
311 while ((element_name = strsep(&element_temp, ",")) != NULL) { \
312 curptr_start = curptr; \
314 first_start = first; \
315 if (element_name[0] == '?') { \
317 ignorenotfound = 1; \
319 ignorenotfound = 0; \
322 len = snprintf(curptr, left, "%s/", \
326 len = snprintf(curptr, left, ",%s/", \
329 error = EINVAL; /* XXXMAC: E2BIG */ \
335 MAC_CHECK(externalize_ ## type, label, element_name, \
336 curptr, left, &len, &claimed); \
339 if (claimed == 1) { \
340 if (len >= outbuflen) { \
341 error = EINVAL; /* XXXMAC: E2BIG */ \
346 } else if (claimed == 0 && ignorenotfound) { \
348 * Revert addition of the label element \
351 curptr = curptr_start; \
354 first = first_start; \
356 error = EINVAL; /* XXXMAC: ENOLABEL */ \
362 #define MAC_INTERNALIZE(type, label, instring) do { \
363 char *element, *element_name, *element_data; \
367 element = instring; \
368 while ((element_name = strsep(&element, ",")) != NULL) { \
369 element_data = element_name; \
370 element_name = strsep(&element_data, "/"); \
371 if (element_data == NULL) { \
376 MAC_CHECK(internalize_ ## type, label, element_name, \
377 element_data, &claimed); \
380 if (claimed != 1) { \
381 /* XXXMAC: Another error here? */ \
389 * MAC_PERFORM performs the designated operation by walking the policy
390 * module list and invoking that operation for each policy.
392 #define MAC_PERFORM(operation, args...) do { \
393 struct mac_policy_conf *mpc; \
395 MAC_POLICY_LIST_BUSY(); \
396 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \
397 if (mpc->mpc_ops->mpo_ ## operation != NULL) \
398 mpc->mpc_ops->mpo_ ## operation (args); \
400 MAC_POLICY_LIST_UNBUSY(); \
404 * Initialize the MAC subsystem, including appropriate SMP locks.
410 LIST_INIT(&mac_policy_list);
411 MAC_POLICY_LIST_LOCKINIT();
415 * For the purposes of modules that want to know if they were loaded
416 * "early", set the mac_late flag once we've processed modules either
417 * linked into the kernel, or loaded before the kernel startup.
427 * Allow MAC policy modules to register during boot, etc.
430 mac_policy_modevent(module_t mod, int type, void *data)
432 struct mac_policy_conf *mpc;
436 mpc = (struct mac_policy_conf *) data;
440 if (mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_NOTLATE &&
442 printf("mac_policy_modevent: can't load %s policy "
443 "after booting\n", mpc->mpc_name);
447 error = mac_policy_register(mpc);
450 /* Don't unregister the module if it was never registered. */
451 if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED)
453 error = mac_policy_unregister(mpc);
465 mac_policy_register(struct mac_policy_conf *mpc)
467 struct mac_policy_conf *tmpc;
468 struct mac_policy_op_entry *mpe;
471 MALLOC(mpc->mpc_ops, struct mac_policy_ops *, sizeof(*mpc->mpc_ops),
472 M_MACOPVEC, M_WAITOK | M_ZERO);
473 for (mpe = mpc->mpc_entries; mpe->mpe_constant != MAC_OP_LAST; mpe++) {
474 switch (mpe->mpe_constant) {
477 * Doesn't actually happen, but this allows checking
478 * that all enumerated values are handled.
482 mpc->mpc_ops->mpo_destroy =
486 mpc->mpc_ops->mpo_init =
490 mpc->mpc_ops->mpo_syscall =
493 case MAC_INIT_BPFDESC_LABEL:
494 mpc->mpc_ops->mpo_init_bpfdesc_label =
497 case MAC_INIT_CRED_LABEL:
498 mpc->mpc_ops->mpo_init_cred_label =
501 case MAC_INIT_DEVFSDIRENT_LABEL:
502 mpc->mpc_ops->mpo_init_devfsdirent_label =
505 case MAC_INIT_IFNET_LABEL:
506 mpc->mpc_ops->mpo_init_ifnet_label =
509 case MAC_INIT_IPQ_LABEL:
510 mpc->mpc_ops->mpo_init_ipq_label =
513 case MAC_INIT_MBUF_LABEL:
514 mpc->mpc_ops->mpo_init_mbuf_label =
517 case MAC_INIT_MOUNT_LABEL:
518 mpc->mpc_ops->mpo_init_mount_label =
521 case MAC_INIT_MOUNT_FS_LABEL:
522 mpc->mpc_ops->mpo_init_mount_fs_label =
525 case MAC_INIT_PIPE_LABEL:
526 mpc->mpc_ops->mpo_init_pipe_label =
529 case MAC_INIT_SOCKET_LABEL:
530 mpc->mpc_ops->mpo_init_socket_label =
533 case MAC_INIT_SOCKET_PEER_LABEL:
534 mpc->mpc_ops->mpo_init_socket_peer_label =
537 case MAC_INIT_VNODE_LABEL:
538 mpc->mpc_ops->mpo_init_vnode_label =
541 case MAC_DESTROY_BPFDESC_LABEL:
542 mpc->mpc_ops->mpo_destroy_bpfdesc_label =
545 case MAC_DESTROY_CRED_LABEL:
546 mpc->mpc_ops->mpo_destroy_cred_label =
549 case MAC_DESTROY_DEVFSDIRENT_LABEL:
550 mpc->mpc_ops->mpo_destroy_devfsdirent_label =
553 case MAC_DESTROY_IFNET_LABEL:
554 mpc->mpc_ops->mpo_destroy_ifnet_label =
557 case MAC_DESTROY_IPQ_LABEL:
558 mpc->mpc_ops->mpo_destroy_ipq_label =
561 case MAC_DESTROY_MBUF_LABEL:
562 mpc->mpc_ops->mpo_destroy_mbuf_label =
565 case MAC_DESTROY_MOUNT_LABEL:
566 mpc->mpc_ops->mpo_destroy_mount_label =
569 case MAC_DESTROY_MOUNT_FS_LABEL:
570 mpc->mpc_ops->mpo_destroy_mount_fs_label =
573 case MAC_DESTROY_PIPE_LABEL:
574 mpc->mpc_ops->mpo_destroy_pipe_label =
577 case MAC_DESTROY_SOCKET_LABEL:
578 mpc->mpc_ops->mpo_destroy_socket_label =
581 case MAC_DESTROY_SOCKET_PEER_LABEL:
582 mpc->mpc_ops->mpo_destroy_socket_peer_label =
585 case MAC_DESTROY_VNODE_LABEL:
586 mpc->mpc_ops->mpo_destroy_vnode_label =
589 case MAC_COPY_PIPE_LABEL:
590 mpc->mpc_ops->mpo_copy_pipe_label =
593 case MAC_COPY_VNODE_LABEL:
594 mpc->mpc_ops->mpo_copy_vnode_label =
597 case MAC_EXTERNALIZE_CRED_LABEL:
598 mpc->mpc_ops->mpo_externalize_cred_label =
601 case MAC_EXTERNALIZE_IFNET_LABEL:
602 mpc->mpc_ops->mpo_externalize_ifnet_label =
605 case MAC_EXTERNALIZE_PIPE_LABEL:
606 mpc->mpc_ops->mpo_externalize_pipe_label =
609 case MAC_EXTERNALIZE_SOCKET_LABEL:
610 mpc->mpc_ops->mpo_externalize_socket_label =
613 case MAC_EXTERNALIZE_SOCKET_PEER_LABEL:
614 mpc->mpc_ops->mpo_externalize_socket_peer_label =
617 case MAC_EXTERNALIZE_VNODE_LABEL:
618 mpc->mpc_ops->mpo_externalize_vnode_label =
621 case MAC_EXTERNALIZE_VNODE_OLDMAC:
622 mpc->mpc_ops->mpo_externalize_vnode_oldmac =
625 case MAC_INTERNALIZE_CRED_LABEL:
626 mpc->mpc_ops->mpo_internalize_cred_label =
629 case MAC_INTERNALIZE_IFNET_LABEL:
630 mpc->mpc_ops->mpo_internalize_ifnet_label =
633 case MAC_INTERNALIZE_PIPE_LABEL:
634 mpc->mpc_ops->mpo_internalize_pipe_label =
637 case MAC_INTERNALIZE_SOCKET_LABEL:
638 mpc->mpc_ops->mpo_internalize_socket_label =
641 case MAC_INTERNALIZE_VNODE_LABEL:
642 mpc->mpc_ops->mpo_internalize_vnode_label =
645 case MAC_CREATE_DEVFS_DEVICE:
646 mpc->mpc_ops->mpo_create_devfs_device =
649 case MAC_CREATE_DEVFS_DIRECTORY:
650 mpc->mpc_ops->mpo_create_devfs_directory =
653 case MAC_CREATE_DEVFS_SYMLINK:
654 mpc->mpc_ops->mpo_create_devfs_symlink =
657 case MAC_CREATE_DEVFS_VNODE:
658 mpc->mpc_ops->mpo_create_devfs_vnode =
661 case MAC_STDCREATEVNODE_EA:
662 mpc->mpc_ops->mpo_stdcreatevnode_ea =
665 case MAC_CREATE_VNODE:
666 mpc->mpc_ops->mpo_create_vnode =
669 case MAC_CREATE_MOUNT:
670 mpc->mpc_ops->mpo_create_mount =
673 case MAC_CREATE_ROOT_MOUNT:
674 mpc->mpc_ops->mpo_create_root_mount =
677 case MAC_RELABEL_VNODE:
678 mpc->mpc_ops->mpo_relabel_vnode =
681 case MAC_UPDATE_DEVFSDIRENT:
682 mpc->mpc_ops->mpo_update_devfsdirent =
685 case MAC_UPDATE_PROCFSVNODE:
686 mpc->mpc_ops->mpo_update_procfsvnode =
689 case MAC_UPDATE_VNODE_FROM_EXTATTR:
690 mpc->mpc_ops->mpo_update_vnode_from_extattr =
693 case MAC_UPDATE_VNODE_FROM_EXTERNALIZED:
694 mpc->mpc_ops->mpo_update_vnode_from_externalized =
697 case MAC_UPDATE_VNODE_FROM_MOUNT:
698 mpc->mpc_ops->mpo_update_vnode_from_mount =
701 case MAC_CREATE_MBUF_FROM_SOCKET:
702 mpc->mpc_ops->mpo_create_mbuf_from_socket =
705 case MAC_CREATE_PIPE:
706 mpc->mpc_ops->mpo_create_pipe =
709 case MAC_CREATE_SOCKET:
710 mpc->mpc_ops->mpo_create_socket =
713 case MAC_CREATE_SOCKET_FROM_SOCKET:
714 mpc->mpc_ops->mpo_create_socket_from_socket =
717 case MAC_RELABEL_PIPE:
718 mpc->mpc_ops->mpo_relabel_pipe =
721 case MAC_RELABEL_SOCKET:
722 mpc->mpc_ops->mpo_relabel_socket =
725 case MAC_SET_SOCKET_PEER_FROM_MBUF:
726 mpc->mpc_ops->mpo_set_socket_peer_from_mbuf =
729 case MAC_SET_SOCKET_PEER_FROM_SOCKET:
730 mpc->mpc_ops->mpo_set_socket_peer_from_socket =
733 case MAC_CREATE_BPFDESC:
734 mpc->mpc_ops->mpo_create_bpfdesc =
737 case MAC_CREATE_DATAGRAM_FROM_IPQ:
738 mpc->mpc_ops->mpo_create_datagram_from_ipq =
741 case MAC_CREATE_FRAGMENT:
742 mpc->mpc_ops->mpo_create_fragment =
745 case MAC_CREATE_IFNET:
746 mpc->mpc_ops->mpo_create_ifnet =
750 mpc->mpc_ops->mpo_create_ipq =
753 case MAC_CREATE_MBUF_FROM_MBUF:
754 mpc->mpc_ops->mpo_create_mbuf_from_mbuf =
757 case MAC_CREATE_MBUF_LINKLAYER:
758 mpc->mpc_ops->mpo_create_mbuf_linklayer =
761 case MAC_CREATE_MBUF_FROM_BPFDESC:
762 mpc->mpc_ops->mpo_create_mbuf_from_bpfdesc =
765 case MAC_CREATE_MBUF_FROM_IFNET:
766 mpc->mpc_ops->mpo_create_mbuf_from_ifnet =
769 case MAC_CREATE_MBUF_MULTICAST_ENCAP:
770 mpc->mpc_ops->mpo_create_mbuf_multicast_encap =
773 case MAC_CREATE_MBUF_NETLAYER:
774 mpc->mpc_ops->mpo_create_mbuf_netlayer =
777 case MAC_FRAGMENT_MATCH:
778 mpc->mpc_ops->mpo_fragment_match =
781 case MAC_RELABEL_IFNET:
782 mpc->mpc_ops->mpo_relabel_ifnet =
786 mpc->mpc_ops->mpo_update_ipq =
789 case MAC_CREATE_CRED:
790 mpc->mpc_ops->mpo_create_cred =
793 case MAC_EXECVE_TRANSITION:
794 mpc->mpc_ops->mpo_execve_transition =
797 case MAC_EXECVE_WILL_TRANSITION:
798 mpc->mpc_ops->mpo_execve_will_transition =
801 case MAC_CREATE_PROC0:
802 mpc->mpc_ops->mpo_create_proc0 =
805 case MAC_CREATE_PROC1:
806 mpc->mpc_ops->mpo_create_proc1 =
809 case MAC_RELABEL_CRED:
810 mpc->mpc_ops->mpo_relabel_cred =
813 case MAC_THREAD_USERRET:
814 mpc->mpc_ops->mpo_thread_userret =
817 case MAC_CHECK_BPFDESC_RECEIVE:
818 mpc->mpc_ops->mpo_check_bpfdesc_receive =
821 case MAC_CHECK_CRED_RELABEL:
822 mpc->mpc_ops->mpo_check_cred_relabel =
825 case MAC_CHECK_CRED_VISIBLE:
826 mpc->mpc_ops->mpo_check_cred_visible =
829 case MAC_CHECK_IFNET_RELABEL:
830 mpc->mpc_ops->mpo_check_ifnet_relabel =
833 case MAC_CHECK_IFNET_TRANSMIT:
834 mpc->mpc_ops->mpo_check_ifnet_transmit =
837 case MAC_CHECK_MOUNT_STAT:
838 mpc->mpc_ops->mpo_check_mount_stat =
841 case MAC_CHECK_PIPE_IOCTL:
842 mpc->mpc_ops->mpo_check_pipe_ioctl =
845 case MAC_CHECK_PIPE_POLL:
846 mpc->mpc_ops->mpo_check_pipe_poll =
849 case MAC_CHECK_PIPE_READ:
850 mpc->mpc_ops->mpo_check_pipe_read =
853 case MAC_CHECK_PIPE_RELABEL:
854 mpc->mpc_ops->mpo_check_pipe_relabel =
857 case MAC_CHECK_PIPE_STAT:
858 mpc->mpc_ops->mpo_check_pipe_stat =
861 case MAC_CHECK_PIPE_WRITE:
862 mpc->mpc_ops->mpo_check_pipe_write =
865 case MAC_CHECK_PROC_DEBUG:
866 mpc->mpc_ops->mpo_check_proc_debug =
869 case MAC_CHECK_PROC_SCHED:
870 mpc->mpc_ops->mpo_check_proc_sched =
873 case MAC_CHECK_PROC_SIGNAL:
874 mpc->mpc_ops->mpo_check_proc_signal =
877 case MAC_CHECK_SOCKET_BIND:
878 mpc->mpc_ops->mpo_check_socket_bind =
881 case MAC_CHECK_SOCKET_CONNECT:
882 mpc->mpc_ops->mpo_check_socket_connect =
885 case MAC_CHECK_SOCKET_DELIVER:
886 mpc->mpc_ops->mpo_check_socket_deliver =
889 case MAC_CHECK_SOCKET_LISTEN:
890 mpc->mpc_ops->mpo_check_socket_listen =
893 case MAC_CHECK_SOCKET_RECEIVE:
894 mpc->mpc_ops->mpo_check_socket_receive =
897 case MAC_CHECK_SOCKET_RELABEL:
898 mpc->mpc_ops->mpo_check_socket_relabel =
901 case MAC_CHECK_SOCKET_SEND:
902 mpc->mpc_ops->mpo_check_socket_send =
905 case MAC_CHECK_SOCKET_VISIBLE:
906 mpc->mpc_ops->mpo_check_socket_visible =
909 case MAC_CHECK_VNODE_ACCESS:
910 mpc->mpc_ops->mpo_check_vnode_access =
913 case MAC_CHECK_VNODE_CHDIR:
914 mpc->mpc_ops->mpo_check_vnode_chdir =
917 case MAC_CHECK_VNODE_CHROOT:
918 mpc->mpc_ops->mpo_check_vnode_chroot =
921 case MAC_CHECK_VNODE_CREATE:
922 mpc->mpc_ops->mpo_check_vnode_create =
925 case MAC_CHECK_VNODE_DELETE:
926 mpc->mpc_ops->mpo_check_vnode_delete =
929 case MAC_CHECK_VNODE_DELETEACL:
930 mpc->mpc_ops->mpo_check_vnode_deleteacl =
933 case MAC_CHECK_VNODE_EXEC:
934 mpc->mpc_ops->mpo_check_vnode_exec =
937 case MAC_CHECK_VNODE_GETACL:
938 mpc->mpc_ops->mpo_check_vnode_getacl =
941 case MAC_CHECK_VNODE_GETEXTATTR:
942 mpc->mpc_ops->mpo_check_vnode_getextattr =
945 case MAC_CHECK_VNODE_LINK:
946 mpc->mpc_ops->mpo_check_vnode_link =
949 case MAC_CHECK_VNODE_LOOKUP:
950 mpc->mpc_ops->mpo_check_vnode_lookup =
953 case MAC_CHECK_VNODE_MMAP:
954 mpc->mpc_ops->mpo_check_vnode_mmap =
957 case MAC_CHECK_VNODE_MMAP_DOWNGRADE:
958 mpc->mpc_ops->mpo_check_vnode_mmap_downgrade =
961 case MAC_CHECK_VNODE_MPROTECT:
962 mpc->mpc_ops->mpo_check_vnode_mprotect =
965 case MAC_CHECK_VNODE_OPEN:
966 mpc->mpc_ops->mpo_check_vnode_open =
969 case MAC_CHECK_VNODE_POLL:
970 mpc->mpc_ops->mpo_check_vnode_poll =
973 case MAC_CHECK_VNODE_READ:
974 mpc->mpc_ops->mpo_check_vnode_read =
977 case MAC_CHECK_VNODE_READDIR:
978 mpc->mpc_ops->mpo_check_vnode_readdir =
981 case MAC_CHECK_VNODE_READLINK:
982 mpc->mpc_ops->mpo_check_vnode_readlink =
985 case MAC_CHECK_VNODE_RELABEL:
986 mpc->mpc_ops->mpo_check_vnode_relabel =
989 case MAC_CHECK_VNODE_RENAME_FROM:
990 mpc->mpc_ops->mpo_check_vnode_rename_from =
993 case MAC_CHECK_VNODE_RENAME_TO:
994 mpc->mpc_ops->mpo_check_vnode_rename_to =
997 case MAC_CHECK_VNODE_REVOKE:
998 mpc->mpc_ops->mpo_check_vnode_revoke =
1001 case MAC_CHECK_VNODE_SETACL:
1002 mpc->mpc_ops->mpo_check_vnode_setacl =
1005 case MAC_CHECK_VNODE_SETEXTATTR:
1006 mpc->mpc_ops->mpo_check_vnode_setextattr =
1009 case MAC_CHECK_VNODE_SETFLAGS:
1010 mpc->mpc_ops->mpo_check_vnode_setflags =
1013 case MAC_CHECK_VNODE_SETMODE:
1014 mpc->mpc_ops->mpo_check_vnode_setmode =
1017 case MAC_CHECK_VNODE_SETOWNER:
1018 mpc->mpc_ops->mpo_check_vnode_setowner =
1021 case MAC_CHECK_VNODE_SETUTIMES:
1022 mpc->mpc_ops->mpo_check_vnode_setutimes =
1025 case MAC_CHECK_VNODE_STAT:
1026 mpc->mpc_ops->mpo_check_vnode_stat =
1029 case MAC_CHECK_VNODE_WRITE:
1030 mpc->mpc_ops->mpo_check_vnode_write =
1035 printf("MAC policy `%s': unknown operation %d\n",
1036 mpc->mpc_name, mpe->mpe_constant);
1041 MAC_POLICY_LIST_LOCK();
1042 if (mac_policy_list_busy > 0) {
1043 MAC_POLICY_LIST_UNLOCK();
1044 FREE(mpc->mpc_ops, M_MACOPVEC);
1045 mpc->mpc_ops = NULL;
1048 LIST_FOREACH(tmpc, &mac_policy_list, mpc_list) {
1049 if (strcmp(tmpc->mpc_name, mpc->mpc_name) == 0) {
1050 MAC_POLICY_LIST_UNLOCK();
1051 FREE(mpc->mpc_ops, M_MACOPVEC);
1052 mpc->mpc_ops = NULL;
1056 if (mpc->mpc_field_off != NULL) {
1057 slot = ffs(mac_policy_offsets_free);
1059 MAC_POLICY_LIST_UNLOCK();
1060 FREE(mpc->mpc_ops, M_MACOPVEC);
1061 mpc->mpc_ops = NULL;
1065 mac_policy_offsets_free &= ~(1 << slot);
1066 *mpc->mpc_field_off = slot;
1068 mpc->mpc_runtime_flags |= MPC_RUNTIME_FLAG_REGISTERED;
1069 LIST_INSERT_HEAD(&mac_policy_list, mpc, mpc_list);
1071 /* Per-policy initialization. */
1072 if (mpc->mpc_ops->mpo_init != NULL)
1073 (*(mpc->mpc_ops->mpo_init))(mpc);
1074 MAC_POLICY_LIST_UNLOCK();
1076 printf("Security policy loaded: %s (%s)\n", mpc->mpc_fullname,
1083 mac_policy_unregister(struct mac_policy_conf *mpc)
1087 * If we fail the load, we may get a request to unload. Check
1088 * to see if we did the run-time registration, and if not,
1091 MAC_POLICY_LIST_LOCK();
1092 if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED) == 0) {
1093 MAC_POLICY_LIST_UNLOCK();
1098 * Don't allow unloading modules with private data.
1100 if (mpc->mpc_field_off != NULL) {
1101 MAC_POLICY_LIST_UNLOCK();
1106 * Only allow the unload to proceed if the module is unloadable
1107 * by its own definition.
1109 if ((mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK) == 0) {
1110 MAC_POLICY_LIST_UNLOCK();
1114 * Right now, we EBUSY if the list is in use. In the future,
1115 * for reliability reasons, we might want to sleep and wakeup
1116 * later to try again.
1118 if (mac_policy_list_busy > 0) {
1119 MAC_POLICY_LIST_UNLOCK();
1122 if (mpc->mpc_ops->mpo_destroy != NULL)
1123 (*(mpc->mpc_ops->mpo_destroy))(mpc);
1125 LIST_REMOVE(mpc, mpc_list);
1126 MAC_POLICY_LIST_UNLOCK();
1128 FREE(mpc->mpc_ops, M_MACOPVEC);
1129 mpc->mpc_ops = NULL;
1130 mpc->mpc_runtime_flags &= ~MPC_RUNTIME_FLAG_REGISTERED;
1132 printf("Security policy unload: %s (%s)\n", mpc->mpc_fullname,
1139 * Define an error value precedence, and given two arguments, selects the
1140 * value with the higher precedence.
1143 error_select(int error1, int error2)
1146 /* Certain decision-making errors take top priority. */
1147 if (error1 == EDEADLK || error2 == EDEADLK)
1150 /* Invalid arguments should be reported where possible. */
1151 if (error1 == EINVAL || error2 == EINVAL)
1154 /* Precedence goes to "visibility", with both process and file. */
1155 if (error1 == ESRCH || error2 == ESRCH)
1158 if (error1 == ENOENT || error2 == ENOENT)
1161 /* Precedence goes to DAC/MAC protections. */
1162 if (error1 == EACCES || error2 == EACCES)
1165 /* Precedence goes to privilege. */
1166 if (error1 == EPERM || error2 == EPERM)
1169 /* Precedence goes to error over success; otherwise, arbitrary. */
1176 mac_init_label(struct label *label)
1179 bzero(label, sizeof(*label));
1180 label->l_flags = MAC_FLAG_INITIALIZED;
1184 mac_destroy_label(struct label *label)
1187 KASSERT(label->l_flags & MAC_FLAG_INITIALIZED,
1188 ("destroying uninitialized label"));
1190 bzero(label, sizeof(*label));
1191 /* implicit: label->l_flags &= ~MAC_FLAG_INITIALIZED; */
1195 mac_init_bpfdesc(struct bpf_d *bpf_d)
1198 mac_init_label(&bpf_d->bd_label);
1199 MAC_PERFORM(init_bpfdesc_label, &bpf_d->bd_label);
1201 atomic_add_int(&nmacbpfdescs, 1);
1206 mac_init_cred_label(struct label *label)
1209 mac_init_label(label);
1210 MAC_PERFORM(init_cred_label, label);
1212 atomic_add_int(&nmaccreds, 1);
1217 mac_init_cred(struct ucred *cred)
1220 mac_init_cred_label(&cred->cr_label);
1224 mac_init_devfsdirent(struct devfs_dirent *de)
1227 mac_init_label(&de->de_label);
1228 MAC_PERFORM(init_devfsdirent_label, &de->de_label);
1230 atomic_add_int(&nmacdevfsdirents, 1);
1235 mac_init_ifnet_label(struct label *label)
1238 mac_init_label(label);
1239 MAC_PERFORM(init_ifnet_label, label);
1241 atomic_add_int(&nmacifnets, 1);
1246 mac_init_ifnet(struct ifnet *ifp)
1249 mac_init_ifnet_label(&ifp->if_label);
1253 mac_init_ipq(struct ipq *ipq)
1256 mac_init_label(&ipq->ipq_label);
1257 MAC_PERFORM(init_ipq_label, &ipq->ipq_label);
1259 atomic_add_int(&nmacipqs, 1);
1264 mac_init_mbuf(struct mbuf *m, int flag)
1268 KASSERT(m->m_flags & M_PKTHDR, ("mac_init_mbuf on non-header mbuf"));
1270 mac_init_label(&m->m_pkthdr.label);
1272 MAC_CHECK(init_mbuf_label, &m->m_pkthdr.label, flag);
1274 MAC_PERFORM(destroy_mbuf_label, &m->m_pkthdr.label);
1275 mac_destroy_label(&m->m_pkthdr.label);
1280 atomic_add_int(&nmacmbufs, 1);
1286 mac_init_mount(struct mount *mp)
1289 mac_init_label(&mp->mnt_mntlabel);
1290 mac_init_label(&mp->mnt_fslabel);
1291 MAC_PERFORM(init_mount_label, &mp->mnt_mntlabel);
1292 MAC_PERFORM(init_mount_fs_label, &mp->mnt_fslabel);
1294 atomic_add_int(&nmacmounts, 1);
1299 mac_init_pipe_label(struct label *label)
1302 mac_init_label(label);
1303 MAC_PERFORM(init_pipe_label, label);
1305 atomic_add_int(&nmacpipes, 1);
1310 mac_init_pipe(struct pipe *pipe)
1312 struct label *label;
1314 label = malloc(sizeof(struct label), M_MACPIPELABEL, M_ZERO|M_WAITOK);
1315 pipe->pipe_label = label;
1316 pipe->pipe_peer->pipe_label = label;
1317 mac_init_pipe_label(label);
1321 mac_init_socket_label(struct label *label, int flag)
1325 mac_init_label(label);
1327 MAC_CHECK(init_socket_label, label, flag);
1329 MAC_PERFORM(destroy_socket_label, label);
1330 mac_destroy_label(label);
1335 atomic_add_int(&nmacsockets, 1);
1342 mac_init_socket_peer_label(struct label *label, int flag)
1346 mac_init_label(label);
1348 MAC_CHECK(init_socket_peer_label, label, flag);
1350 MAC_PERFORM(destroy_socket_label, label);
1351 mac_destroy_label(label);
1358 mac_init_socket(struct socket *socket, int flag)
1362 error = mac_init_socket_label(&socket->so_label, flag);
1366 error = mac_init_socket_peer_label(&socket->so_peerlabel, flag);
1368 mac_destroy_socket_label(&socket->so_label);
1374 mac_init_vnode_label(struct label *label)
1377 mac_init_label(label);
1378 MAC_PERFORM(init_vnode_label, label);
1380 atomic_add_int(&nmacvnodes, 1);
1385 mac_init_vnode(struct vnode *vp)
1388 mac_init_vnode_label(&vp->v_label);
1392 mac_destroy_bpfdesc(struct bpf_d *bpf_d)
1395 MAC_PERFORM(destroy_bpfdesc_label, &bpf_d->bd_label);
1396 mac_destroy_label(&bpf_d->bd_label);
1398 atomic_subtract_int(&nmacbpfdescs, 1);
1403 mac_destroy_cred_label(struct label *label)
1406 MAC_PERFORM(destroy_cred_label, label);
1407 mac_destroy_label(label);
1409 atomic_subtract_int(&nmaccreds, 1);
1414 mac_destroy_cred(struct ucred *cred)
1417 mac_destroy_cred_label(&cred->cr_label);
1421 mac_destroy_devfsdirent(struct devfs_dirent *de)
1424 MAC_PERFORM(destroy_devfsdirent_label, &de->de_label);
1425 mac_destroy_label(&de->de_label);
1427 atomic_subtract_int(&nmacdevfsdirents, 1);
1432 mac_destroy_ifnet_label(struct label *label)
1435 MAC_PERFORM(destroy_ifnet_label, label);
1436 mac_destroy_label(label);
1438 atomic_subtract_int(&nmacifnets, 1);
1443 mac_destroy_ifnet(struct ifnet *ifp)
1446 mac_destroy_ifnet_label(&ifp->if_label);
1450 mac_destroy_ipq(struct ipq *ipq)
1453 MAC_PERFORM(destroy_ipq_label, &ipq->ipq_label);
1454 mac_destroy_label(&ipq->ipq_label);
1456 atomic_subtract_int(&nmacipqs, 1);
1461 mac_destroy_mbuf(struct mbuf *m)
1464 MAC_PERFORM(destroy_mbuf_label, &m->m_pkthdr.label);
1465 mac_destroy_label(&m->m_pkthdr.label);
1467 atomic_subtract_int(&nmacmbufs, 1);
1472 mac_destroy_mount(struct mount *mp)
1475 MAC_PERFORM(destroy_mount_label, &mp->mnt_mntlabel);
1476 MAC_PERFORM(destroy_mount_fs_label, &mp->mnt_fslabel);
1477 mac_destroy_label(&mp->mnt_fslabel);
1478 mac_destroy_label(&mp->mnt_mntlabel);
1480 atomic_subtract_int(&nmacmounts, 1);
1485 mac_destroy_pipe_label(struct label *label)
1488 MAC_PERFORM(destroy_pipe_label, label);
1489 mac_destroy_label(label);
1491 atomic_subtract_int(&nmacpipes, 1);
1496 mac_destroy_pipe(struct pipe *pipe)
1499 mac_destroy_pipe_label(pipe->pipe_label);
1500 free(pipe->pipe_label, M_MACPIPELABEL);
1504 mac_destroy_socket_label(struct label *label)
1507 MAC_PERFORM(destroy_socket_label, label);
1508 mac_destroy_label(label);
1510 atomic_subtract_int(&nmacsockets, 1);
1515 mac_destroy_socket_peer_label(struct label *label)
1518 MAC_PERFORM(destroy_socket_peer_label, label);
1519 mac_destroy_label(label);
1523 mac_destroy_socket(struct socket *socket)
1526 mac_destroy_socket_label(&socket->so_label);
1527 mac_destroy_socket_peer_label(&socket->so_peerlabel);
1531 mac_destroy_vnode_label(struct label *label)
1534 MAC_PERFORM(destroy_vnode_label, label);
1535 mac_destroy_label(label);
1537 atomic_subtract_int(&nmacvnodes, 1);
1542 mac_destroy_vnode(struct vnode *vp)
1545 mac_destroy_vnode_label(&vp->v_label);
1549 mac_copy_pipe_label(struct label *src, struct label *dest)
1552 MAC_PERFORM(copy_pipe_label, src, dest);
1556 mac_copy_vnode_label(struct label *src, struct label *dest)
1559 MAC_PERFORM(copy_vnode_label, src, dest);
1563 mac_check_structmac_consistent(struct mac *mac)
1566 if (mac->m_buflen > MAC_MAX_LABEL_BUF_LEN)
1573 mac_externalize_cred_label(struct label *label, char *elements,
1574 char *outbuf, size_t outbuflen, int flags)
1578 MAC_EXTERNALIZE(cred_label, label, elements, outbuf, outbuflen);
1584 mac_externalize_ifnet_label(struct label *label, char *elements,
1585 char *outbuf, size_t outbuflen, int flags)
1589 MAC_EXTERNALIZE(ifnet_label, label, elements, outbuf, outbuflen);
1595 mac_externalize_pipe_label(struct label *label, char *elements,
1596 char *outbuf, size_t outbuflen, int flags)
1600 MAC_EXTERNALIZE(pipe_label, label, elements, outbuf, outbuflen);
1606 mac_externalize_socket_label(struct label *label, char *elements,
1607 char *outbuf, size_t outbuflen, int flags)
1611 MAC_EXTERNALIZE(socket_label, label, elements, outbuf, outbuflen);
1617 mac_externalize_socket_peer_label(struct label *label, char *elements,
1618 char *outbuf, size_t outbuflen, int flags)
1622 MAC_EXTERNALIZE(socket_peer_label, label, elements, outbuf, outbuflen);
1628 mac_externalize_vnode_label(struct label *label, char *elements,
1629 char *outbuf, size_t outbuflen, int flags)
1633 MAC_EXTERNALIZE(vnode_label, label, elements, outbuf, outbuflen);
1639 mac_externalize_vnode_oldmac(struct label *label, struct oldmac *extmac)
1643 MAC_CHECK(externalize_vnode_oldmac, label, extmac);
1649 mac_internalize_cred_label(struct label *label, char *string)
1653 MAC_INTERNALIZE(cred_label, label, string);
1659 mac_internalize_ifnet_label(struct label *label, char *string)
1663 MAC_INTERNALIZE(ifnet_label, label, string);
1669 mac_internalize_pipe_label(struct label *label, char *string)
1673 MAC_INTERNALIZE(pipe_label, label, string);
1679 mac_internalize_socket_label(struct label *label, char *string)
1683 MAC_INTERNALIZE(socket_label, label, string);
1689 mac_internalize_vnode_label(struct label *label, char *string)
1693 MAC_INTERNALIZE(vnode_label, label, string);
1699 * Initialize MAC label for the first kernel process, from which other
1700 * kernel processes and threads are spawned.
1703 mac_create_proc0(struct ucred *cred)
1706 MAC_PERFORM(create_proc0, cred);
1710 * Initialize MAC label for the first userland process, from which other
1711 * userland processes and threads are spawned.
1714 mac_create_proc1(struct ucred *cred)
1717 MAC_PERFORM(create_proc1, cred);
1721 mac_thread_userret(struct thread *td)
1724 MAC_PERFORM(thread_userret, td);
1728 * When a new process is created, its label must be initialized. Generally,
1729 * this involves inheritence from the parent process, modulo possible
1730 * deltas. This function allows that processing to take place.
1733 mac_create_cred(struct ucred *parent_cred, struct ucred *child_cred)
1736 MAC_PERFORM(create_cred, parent_cred, child_cred);
1740 mac_update_devfsdirent(struct devfs_dirent *de, struct vnode *vp)
1743 MAC_PERFORM(update_devfsdirent, de, &de->de_label, vp, &vp->v_label);
1747 mac_update_procfsvnode(struct vnode *vp, struct ucred *cred)
1750 MAC_PERFORM(update_procfsvnode, vp, &vp->v_label, cred);
1754 * Support callout for policies that manage their own externalization
1755 * using extended attributes.
1758 mac_update_vnode_from_extattr(struct vnode *vp, struct mount *mp)
1762 MAC_CHECK(update_vnode_from_extattr, vp, &vp->v_label, mp,
1769 * Given an externalized mac label, internalize it and stamp it on a
1773 mac_update_vnode_from_externalized(struct vnode *vp, struct oldmac *extmac)
1777 MAC_CHECK(update_vnode_from_externalized, vp, &vp->v_label, extmac);
1783 * Call out to individual policies to update the label in a vnode from
1787 mac_update_vnode_from_mount(struct vnode *vp, struct mount *mp)
1790 MAC_PERFORM(update_vnode_from_mount, vp, &vp->v_label, mp,
1793 ASSERT_VOP_LOCKED(vp, "mac_update_vnode_from_mount");
1794 if (mac_cache_fslabel_in_vnode)
1795 vp->v_vflag |= VV_CACHEDLABEL;
1799 * Implementation of VOP_REFRESHLABEL() that relies on extended attributes
1800 * to store label data. Can be referenced by filesystems supporting
1801 * extended attributes.
1804 vop_stdrefreshlabel_ea(struct vop_refreshlabel_args *ap)
1806 struct vnode *vp = ap->a_vp;
1807 struct oldmac extmac;
1810 ASSERT_VOP_LOCKED(vp, "vop_stdrefreshlabel_ea");
1813 * Call out to external policies first. Order doesn't really
1814 * matter, as long as failure of one assures failure of all.
1816 error = mac_update_vnode_from_extattr(vp, vp->v_mount);
1820 buflen = sizeof(extmac);
1821 error = vn_extattr_get(vp, IO_NODELOCKED,
1822 FREEBSD_MAC_EXTATTR_NAMESPACE, FREEBSD_MAC_EXTATTR_NAME, &buflen,
1823 (char *)&extmac, curthread);
1831 * Use the label from the mount point.
1833 mac_update_vnode_from_mount(vp, vp->v_mount);
1838 /* Fail horribly. */
1842 if (buflen != sizeof(extmac))
1843 error = EPERM; /* Fail very closed. */
1845 error = mac_update_vnode_from_externalized(vp, &extmac);
1847 vp->v_vflag |= VV_CACHEDLABEL;
1851 printf("Corrupted label on %s",
1852 vp->v_mount->mnt_stat.f_mntonname);
1853 if (VOP_GETATTR(vp, &va, curthread->td_ucred, curthread) == 0)
1854 printf(" inum %ld", va.va_fileid);
1856 if (mac_debug_label_fallback) {
1857 printf(", falling back.\n");
1858 mac_update_vnode_from_mount(vp, vp->v_mount);
1873 * Make sure the vnode label is up-to-date. If EOPNOTSUPP, then we handle
1874 * the labeling activity outselves. Filesystems should be careful not
1875 * to change their minds regarding whether they support vop_refreshlabel()
1876 * for a vnode or not. Don't cache the vnode here, allow the file
1877 * system code to determine if it's safe to cache. If we update from
1878 * the mount, don't cache since a change to the mount label should affect
1882 vn_refreshlabel(struct vnode *vp, struct ucred *cred)
1886 ASSERT_VOP_LOCKED(vp, "vn_refreshlabel");
1888 if (vp->v_mount == NULL) {
1890 Eventually, we probably want to special-case refreshing
1891 of deadfs vnodes, and if there's a lock-free race somewhere,
1892 that case might be handled here.
1894 mac_update_vnode_deadfs(vp);
1897 /* printf("vn_refreshlabel: null v_mount\n"); */
1898 if (vp->v_type != VNON)
1900 "vn_refreshlabel: null v_mount with non-VNON\n");
1904 if (vp->v_vflag & VV_CACHEDLABEL) {
1905 mac_vnode_label_cache_hits++;
1908 mac_vnode_label_cache_misses++;
1910 if ((vp->v_mount->mnt_flag & MNT_MULTILABEL) == 0) {
1911 mac_update_vnode_from_mount(vp, vp->v_mount);
1915 error = VOP_REFRESHLABEL(vp, cred, curthread);
1919 * If labels are not supported on this vnode, fall back to
1920 * the label in the mount and propagate it to the vnode.
1921 * There should probably be some sort of policy/flag/decision
1924 mac_update_vnode_from_mount(vp, vp->v_mount);
1932 * Helper function for file systems using the vop_std*_ea() calls. This
1933 * function must be called after EA service is available for the vnode,
1934 * but before it's hooked up to the namespace so that the node persists
1935 * if there's a crash, or before it can be accessed. On successful
1936 * commit of the label to disk (etc), do cache the label.
1939 vop_stdcreatevnode_ea(struct vnode *dvp, struct vnode *tvp, struct ucred *cred)
1941 struct oldmac extmac;
1944 ASSERT_VOP_LOCKED(tvp, "vop_stdcreatevnode_ea");
1945 if ((dvp->v_mount->mnt_flag & MNT_MULTILABEL) == 0) {
1946 mac_update_vnode_from_mount(tvp, tvp->v_mount);
1948 error = vn_refreshlabel(dvp, cred);
1953 * Stick the label in the vnode. Then try to write to
1954 * disk. If we fail, return a failure to abort the
1955 * create operation. Really, this failure shouldn't
1956 * happen except in fairly unusual circumstances (out
1959 mac_create_vnode(cred, dvp, tvp);
1961 error = mac_stdcreatevnode_ea(tvp);
1966 * XXX: Eventually this will go away and all policies will
1967 * directly manage their extended attributes.
1969 error = mac_externalize_vnode_oldmac(&tvp->v_label, &extmac);
1973 error = vn_extattr_set(tvp, IO_NODELOCKED,
1974 FREEBSD_MAC_EXTATTR_NAMESPACE, FREEBSD_MAC_EXTATTR_NAME,
1975 sizeof(extmac), (char *)&extmac, curthread);
1977 tvp->v_vflag |= VV_CACHEDLABEL;
1981 * In theory, we could have fall-back behavior here.
1982 * It would probably be incorrect.
1993 mac_execve_transition(struct ucred *old, struct ucred *new, struct vnode *vp)
1997 ASSERT_VOP_LOCKED(vp, "mac_execve_transition");
1999 error = vn_refreshlabel(vp, old);
2001 printf("mac_execve_transition: vn_refreshlabel returned %d\n",
2003 printf("mac_execve_transition: using old vnode label\n");
2006 MAC_PERFORM(execve_transition, old, new, vp, &vp->v_label);
2010 mac_execve_will_transition(struct ucred *old, struct vnode *vp)
2014 error = vn_refreshlabel(vp, old);
2019 MAC_BOOLEAN(execve_will_transition, ||, old, vp, &vp->v_label);
2025 mac_check_vnode_access(struct ucred *cred, struct vnode *vp, int flags)
2029 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_access");
2031 if (!mac_enforce_fs)
2034 error = vn_refreshlabel(vp, cred);
2038 MAC_CHECK(check_vnode_access, cred, vp, &vp->v_label, flags);
2043 mac_check_vnode_chdir(struct ucred *cred, struct vnode *dvp)
2047 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_chdir");
2049 if (!mac_enforce_fs)
2052 error = vn_refreshlabel(dvp, cred);
2056 MAC_CHECK(check_vnode_chdir, cred, dvp, &dvp->v_label);
2061 mac_check_vnode_chroot(struct ucred *cred, struct vnode *dvp)
2065 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_chroot");
2067 if (!mac_enforce_fs)
2070 error = vn_refreshlabel(dvp, cred);
2074 MAC_CHECK(check_vnode_chroot, cred, dvp, &dvp->v_label);
2079 mac_check_vnode_create(struct ucred *cred, struct vnode *dvp,
2080 struct componentname *cnp, struct vattr *vap)
2084 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_create");
2086 if (!mac_enforce_fs)
2089 error = vn_refreshlabel(dvp, cred);
2093 MAC_CHECK(check_vnode_create, cred, dvp, &dvp->v_label, cnp, vap);
2098 mac_check_vnode_delete(struct ucred *cred, struct vnode *dvp, struct vnode *vp,
2099 struct componentname *cnp)
2103 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_delete");
2104 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_delete");
2106 if (!mac_enforce_fs)
2109 error = vn_refreshlabel(dvp, cred);
2112 error = vn_refreshlabel(vp, cred);
2116 MAC_CHECK(check_vnode_delete, cred, dvp, &dvp->v_label, vp,
2122 mac_check_vnode_deleteacl(struct ucred *cred, struct vnode *vp,
2127 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_deleteacl");
2129 if (!mac_enforce_fs)
2132 error = vn_refreshlabel(vp, cred);
2136 MAC_CHECK(check_vnode_deleteacl, cred, vp, &vp->v_label, type);
2141 mac_check_vnode_exec(struct ucred *cred, struct vnode *vp)
2145 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_exec");
2147 if (!mac_enforce_process && !mac_enforce_fs)
2150 error = vn_refreshlabel(vp, cred);
2153 MAC_CHECK(check_vnode_exec, cred, vp, &vp->v_label);
2159 mac_check_vnode_getacl(struct ucred *cred, struct vnode *vp, acl_type_t type)
2163 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_getacl");
2165 if (!mac_enforce_fs)
2168 error = vn_refreshlabel(vp, cred);
2172 MAC_CHECK(check_vnode_getacl, cred, vp, &vp->v_label, type);
2177 mac_check_vnode_getextattr(struct ucred *cred, struct vnode *vp,
2178 int attrnamespace, const char *name, struct uio *uio)
2182 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_getextattr");
2184 if (!mac_enforce_fs)
2187 error = vn_refreshlabel(vp, cred);
2191 MAC_CHECK(check_vnode_getextattr, cred, vp, &vp->v_label,
2192 attrnamespace, name, uio);
2197 mac_check_vnode_link(struct ucred *cred, struct vnode *dvp,
2198 struct vnode *vp, struct componentname *cnp)
2202 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_link");
2203 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_link");
2205 if (!mac_enforce_fs)
2208 error = vn_refreshlabel(dvp, cred);
2212 error = vn_refreshlabel(vp, cred);
2216 MAC_CHECK(check_vnode_link, cred, dvp, &dvp->v_label, vp,
2222 mac_check_vnode_lookup(struct ucred *cred, struct vnode *dvp,
2223 struct componentname *cnp)
2227 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_lookup");
2229 if (!mac_enforce_fs)
2232 error = vn_refreshlabel(dvp, cred);
2236 MAC_CHECK(check_vnode_lookup, cred, dvp, &dvp->v_label, cnp);
2241 mac_check_vnode_mmap(struct ucred *cred, struct vnode *vp, int prot)
2245 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mmap");
2247 if (!mac_enforce_fs || !mac_enforce_vm)
2250 error = vn_refreshlabel(vp, cred);
2254 MAC_CHECK(check_vnode_mmap, cred, vp, &vp->v_label, prot);
2259 mac_check_vnode_mmap_downgrade(struct ucred *cred, struct vnode *vp, int *prot)
2263 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mmap_downgrade");
2265 if (!mac_enforce_fs || !mac_enforce_vm)
2268 MAC_PERFORM(check_vnode_mmap_downgrade, cred, vp, &vp->v_label,
2275 mac_check_vnode_mprotect(struct ucred *cred, struct vnode *vp, int prot)
2279 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mprotect");
2281 if (!mac_enforce_fs || !mac_enforce_vm)
2284 error = vn_refreshlabel(vp, cred);
2288 MAC_CHECK(check_vnode_mprotect, cred, vp, &vp->v_label, prot);
2293 mac_check_vnode_open(struct ucred *cred, struct vnode *vp, mode_t acc_mode)
2297 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_open");
2299 if (!mac_enforce_fs)
2302 error = vn_refreshlabel(vp, cred);
2306 MAC_CHECK(check_vnode_open, cred, vp, &vp->v_label, acc_mode);
2311 mac_check_vnode_poll(struct ucred *active_cred, struct ucred *file_cred,
2316 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_poll");
2318 if (!mac_enforce_fs)
2321 error = vn_refreshlabel(vp, active_cred);
2325 MAC_CHECK(check_vnode_poll, active_cred, file_cred, vp,
2332 mac_check_vnode_read(struct ucred *active_cred, struct ucred *file_cred,
2337 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_read");
2339 if (!mac_enforce_fs)
2342 error = vn_refreshlabel(vp, active_cred);
2346 MAC_CHECK(check_vnode_read, active_cred, file_cred, vp,
2353 mac_check_vnode_readdir(struct ucred *cred, struct vnode *dvp)
2357 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_readdir");
2359 if (!mac_enforce_fs)
2362 error = vn_refreshlabel(dvp, cred);
2366 MAC_CHECK(check_vnode_readdir, cred, dvp, &dvp->v_label);
2371 mac_check_vnode_readlink(struct ucred *cred, struct vnode *vp)
2375 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_readlink");
2377 if (!mac_enforce_fs)
2380 error = vn_refreshlabel(vp, cred);
2384 MAC_CHECK(check_vnode_readlink, cred, vp, &vp->v_label);
2389 mac_check_vnode_relabel(struct ucred *cred, struct vnode *vp,
2390 struct label *newlabel)
2394 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_relabel");
2396 error = vn_refreshlabel(vp, cred);
2400 MAC_CHECK(check_vnode_relabel, cred, vp, &vp->v_label, newlabel);
2406 mac_check_vnode_rename_from(struct ucred *cred, struct vnode *dvp,
2407 struct vnode *vp, struct componentname *cnp)
2411 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_rename_from");
2412 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_rename_from");
2414 if (!mac_enforce_fs)
2417 error = vn_refreshlabel(dvp, cred);
2420 error = vn_refreshlabel(vp, cred);
2424 MAC_CHECK(check_vnode_rename_from, cred, dvp, &dvp->v_label, vp,
2430 mac_check_vnode_rename_to(struct ucred *cred, struct vnode *dvp,
2431 struct vnode *vp, int samedir, struct componentname *cnp)
2435 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_rename_to");
2436 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_rename_to");
2438 if (!mac_enforce_fs)
2441 error = vn_refreshlabel(dvp, cred);
2445 error = vn_refreshlabel(vp, cred);
2449 MAC_CHECK(check_vnode_rename_to, cred, dvp, &dvp->v_label, vp,
2450 vp != NULL ? &vp->v_label : NULL, samedir, cnp);
2455 mac_check_vnode_revoke(struct ucred *cred, struct vnode *vp)
2459 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_revoke");
2461 if (!mac_enforce_fs)
2464 error = vn_refreshlabel(vp, cred);
2468 MAC_CHECK(check_vnode_revoke, cred, vp, &vp->v_label);
2473 mac_check_vnode_setacl(struct ucred *cred, struct vnode *vp, acl_type_t type,
2478 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setacl");
2480 if (!mac_enforce_fs)
2483 error = vn_refreshlabel(vp, cred);
2487 MAC_CHECK(check_vnode_setacl, cred, vp, &vp->v_label, type, acl);
2492 mac_check_vnode_setextattr(struct ucred *cred, struct vnode *vp,
2493 int attrnamespace, const char *name, struct uio *uio)
2497 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setextattr");
2499 if (!mac_enforce_fs)
2502 error = vn_refreshlabel(vp, cred);
2506 MAC_CHECK(check_vnode_setextattr, cred, vp, &vp->v_label,
2507 attrnamespace, name, uio);
2512 mac_check_vnode_setflags(struct ucred *cred, struct vnode *vp, u_long flags)
2516 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setflags");
2518 if (!mac_enforce_fs)
2521 error = vn_refreshlabel(vp, cred);
2525 MAC_CHECK(check_vnode_setflags, cred, vp, &vp->v_label, flags);
2530 mac_check_vnode_setmode(struct ucred *cred, struct vnode *vp, mode_t mode)
2534 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setmode");
2536 if (!mac_enforce_fs)
2539 error = vn_refreshlabel(vp, cred);
2543 MAC_CHECK(check_vnode_setmode, cred, vp, &vp->v_label, mode);
2548 mac_check_vnode_setowner(struct ucred *cred, struct vnode *vp, uid_t uid,
2553 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setowner");
2555 if (!mac_enforce_fs)
2558 error = vn_refreshlabel(vp, cred);
2562 MAC_CHECK(check_vnode_setowner, cred, vp, &vp->v_label, uid, gid);
2567 mac_check_vnode_setutimes(struct ucred *cred, struct vnode *vp,
2568 struct timespec atime, struct timespec mtime)
2572 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setutimes");
2574 if (!mac_enforce_fs)
2577 error = vn_refreshlabel(vp, cred);
2581 MAC_CHECK(check_vnode_setutimes, cred, vp, &vp->v_label, atime,
2587 mac_check_vnode_stat(struct ucred *active_cred, struct ucred *file_cred,
2592 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_stat");
2594 if (!mac_enforce_fs)
2597 error = vn_refreshlabel(vp, active_cred);
2601 MAC_CHECK(check_vnode_stat, active_cred, file_cred, vp,
2607 mac_check_vnode_write(struct ucred *active_cred, struct ucred *file_cred,
2612 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_write");
2614 if (!mac_enforce_fs)
2617 error = vn_refreshlabel(vp, active_cred);
2621 MAC_CHECK(check_vnode_write, active_cred, file_cred, vp,
2628 * When relabeling a process, call out to the policies for the maximum
2629 * permission allowed for each object type we know about in its
2630 * memory space, and revoke access (in the least surprising ways we
2631 * know) when necessary. The process lock is not held here.
2634 mac_cred_mmapped_drop_perms(struct thread *td, struct ucred *cred)
2637 /* XXX freeze all other threads */
2638 mac_cred_mmapped_drop_perms_recurse(td, cred,
2639 &td->td_proc->p_vmspace->vm_map);
2640 /* XXX allow other threads to continue */
2643 static __inline const char *
2644 prot2str(vm_prot_t prot)
2647 switch (prot & VM_PROT_ALL) {
2650 case VM_PROT_READ | VM_PROT_WRITE:
2652 case VM_PROT_READ | VM_PROT_EXECUTE:
2654 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
2658 case VM_PROT_EXECUTE:
2660 case VM_PROT_WRITE | VM_PROT_EXECUTE:
2668 mac_cred_mmapped_drop_perms_recurse(struct thread *td, struct ucred *cred,
2671 struct vm_map_entry *vme;
2673 vm_prot_t revokeperms;
2675 vm_ooffset_t offset;
2678 if (!mac_mmap_revocation)
2681 vm_map_lock_read(map);
2682 for (vme = map->header.next; vme != &map->header; vme = vme->next) {
2683 if (vme->eflags & MAP_ENTRY_IS_SUB_MAP) {
2684 mac_cred_mmapped_drop_perms_recurse(td, cred,
2685 vme->object.sub_map);
2689 * Skip over entries that obviously are not shared.
2691 if (vme->eflags & (MAP_ENTRY_COW | MAP_ENTRY_NOSYNC) ||
2692 !vme->max_protection)
2695 * Drill down to the deepest backing object.
2697 offset = vme->offset;
2698 object = vme->object.vm_object;
2701 while (object->backing_object != NULL) {
2702 object = object->backing_object;
2703 offset += object->backing_object_offset;
2706 * At the moment, vm_maps and objects aren't considered
2707 * by the MAC system, so only things with backing by a
2708 * normal object (read: vnodes) are checked.
2710 if (object->type != OBJT_VNODE)
2712 vp = (struct vnode *)object->handle;
2713 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2714 result = vme->max_protection;
2715 mac_check_vnode_mmap_downgrade(cred, vp, &result);
2716 VOP_UNLOCK(vp, 0, td);
2718 * Find out what maximum protection we may be allowing
2719 * now but a policy needs to get removed.
2721 revokeperms = vme->max_protection & ~result;
2724 printf("pid %ld: revoking %s perms from %#lx:%ld "
2725 "(max %s/cur %s)\n", (long)td->td_proc->p_pid,
2726 prot2str(revokeperms), (u_long)vme->start,
2727 (long)(vme->end - vme->start),
2728 prot2str(vme->max_protection), prot2str(vme->protection));
2729 vm_map_lock_upgrade(map);
2731 * This is the really simple case: if a map has more
2732 * max_protection than is allowed, but it's not being
2733 * actually used (that is, the current protection is
2734 * still allowed), we can just wipe it out and do
2737 if ((vme->protection & revokeperms) == 0) {
2738 vme->max_protection -= revokeperms;
2740 if (revokeperms & VM_PROT_WRITE) {
2742 * In the more complicated case, flush out all
2743 * pending changes to the object then turn it
2746 vm_object_reference(object);
2747 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2748 vm_object_page_clean(object,
2750 OFF_TO_IDX(offset + vme->end - vme->start +
2753 VOP_UNLOCK(vp, 0, td);
2754 vm_object_deallocate(object);
2756 * Why bother if there's no read permissions
2757 * anymore? For the rest, we need to leave
2758 * the write permissions on for COW, or
2759 * remove them entirely if configured to.
2761 if (!mac_mmap_revocation_via_cow) {
2762 vme->max_protection &= ~VM_PROT_WRITE;
2763 vme->protection &= ~VM_PROT_WRITE;
2764 } if ((revokeperms & VM_PROT_READ) == 0)
2765 vme->eflags |= MAP_ENTRY_COW |
2766 MAP_ENTRY_NEEDS_COPY;
2768 if (revokeperms & VM_PROT_EXECUTE) {
2769 vme->max_protection &= ~VM_PROT_EXECUTE;
2770 vme->protection &= ~VM_PROT_EXECUTE;
2772 if (revokeperms & VM_PROT_READ) {
2773 vme->max_protection = 0;
2774 vme->protection = 0;
2776 pmap_protect(map->pmap, vme->start, vme->end,
2777 vme->protection & ~revokeperms);
2778 vm_map_simplify_entry(map, vme);
2780 vm_map_lock_downgrade(map);
2782 vm_map_unlock_read(map);
2786 * When the subject's label changes, it may require revocation of privilege
2787 * to mapped objects. This can't be done on-the-fly later with a unified
2791 mac_relabel_cred(struct ucred *cred, struct label *newlabel)
2794 MAC_PERFORM(relabel_cred, cred, newlabel);
2798 mac_relabel_vnode(struct ucred *cred, struct vnode *vp, struct label *newlabel)
2801 MAC_PERFORM(relabel_vnode, cred, vp, &vp->v_label, newlabel);
2805 mac_create_ifnet(struct ifnet *ifnet)
2808 MAC_PERFORM(create_ifnet, ifnet, &ifnet->if_label);
2812 mac_create_bpfdesc(struct ucred *cred, struct bpf_d *bpf_d)
2815 MAC_PERFORM(create_bpfdesc, cred, bpf_d, &bpf_d->bd_label);
2819 mac_create_socket(struct ucred *cred, struct socket *socket)
2822 MAC_PERFORM(create_socket, cred, socket, &socket->so_label);
2826 mac_create_pipe(struct ucred *cred, struct pipe *pipe)
2829 MAC_PERFORM(create_pipe, cred, pipe, pipe->pipe_label);
2833 mac_create_socket_from_socket(struct socket *oldsocket,
2834 struct socket *newsocket)
2837 MAC_PERFORM(create_socket_from_socket, oldsocket, &oldsocket->so_label,
2838 newsocket, &newsocket->so_label);
2842 mac_relabel_socket(struct ucred *cred, struct socket *socket,
2843 struct label *newlabel)
2846 MAC_PERFORM(relabel_socket, cred, socket, &socket->so_label, newlabel);
2850 mac_relabel_pipe(struct ucred *cred, struct pipe *pipe, struct label *newlabel)
2853 MAC_PERFORM(relabel_pipe, cred, pipe, pipe->pipe_label, newlabel);
2857 mac_set_socket_peer_from_mbuf(struct mbuf *mbuf, struct socket *socket)
2860 MAC_PERFORM(set_socket_peer_from_mbuf, mbuf, &mbuf->m_pkthdr.label,
2861 socket, &socket->so_peerlabel);
2865 mac_set_socket_peer_from_socket(struct socket *oldsocket,
2866 struct socket *newsocket)
2869 MAC_PERFORM(set_socket_peer_from_socket, oldsocket,
2870 &oldsocket->so_label, newsocket, &newsocket->so_peerlabel);
2874 mac_create_datagram_from_ipq(struct ipq *ipq, struct mbuf *datagram)
2877 MAC_PERFORM(create_datagram_from_ipq, ipq, &ipq->ipq_label,
2878 datagram, &datagram->m_pkthdr.label);
2882 mac_create_fragment(struct mbuf *datagram, struct mbuf *fragment)
2885 MAC_PERFORM(create_fragment, datagram, &datagram->m_pkthdr.label,
2886 fragment, &fragment->m_pkthdr.label);
2890 mac_create_ipq(struct mbuf *fragment, struct ipq *ipq)
2893 MAC_PERFORM(create_ipq, fragment, &fragment->m_pkthdr.label, ipq,
2898 mac_create_mbuf_from_mbuf(struct mbuf *oldmbuf, struct mbuf *newmbuf)
2901 MAC_PERFORM(create_mbuf_from_mbuf, oldmbuf, &oldmbuf->m_pkthdr.label,
2902 newmbuf, &newmbuf->m_pkthdr.label);
2906 mac_create_mbuf_from_bpfdesc(struct bpf_d *bpf_d, struct mbuf *mbuf)
2909 MAC_PERFORM(create_mbuf_from_bpfdesc, bpf_d, &bpf_d->bd_label, mbuf,
2910 &mbuf->m_pkthdr.label);
2914 mac_create_mbuf_linklayer(struct ifnet *ifnet, struct mbuf *mbuf)
2917 MAC_PERFORM(create_mbuf_linklayer, ifnet, &ifnet->if_label, mbuf,
2918 &mbuf->m_pkthdr.label);
2922 mac_create_mbuf_from_ifnet(struct ifnet *ifnet, struct mbuf *mbuf)
2925 MAC_PERFORM(create_mbuf_from_ifnet, ifnet, &ifnet->if_label, mbuf,
2926 &mbuf->m_pkthdr.label);
2930 mac_create_mbuf_multicast_encap(struct mbuf *oldmbuf, struct ifnet *ifnet,
2931 struct mbuf *newmbuf)
2934 MAC_PERFORM(create_mbuf_multicast_encap, oldmbuf,
2935 &oldmbuf->m_pkthdr.label, ifnet, &ifnet->if_label, newmbuf,
2936 &newmbuf->m_pkthdr.label);
2940 mac_create_mbuf_netlayer(struct mbuf *oldmbuf, struct mbuf *newmbuf)
2943 MAC_PERFORM(create_mbuf_netlayer, oldmbuf, &oldmbuf->m_pkthdr.label,
2944 newmbuf, &newmbuf->m_pkthdr.label);
2948 mac_fragment_match(struct mbuf *fragment, struct ipq *ipq)
2953 MAC_BOOLEAN(fragment_match, &&, fragment, &fragment->m_pkthdr.label,
2954 ipq, &ipq->ipq_label);
2960 mac_update_ipq(struct mbuf *fragment, struct ipq *ipq)
2963 MAC_PERFORM(update_ipq, fragment, &fragment->m_pkthdr.label, ipq,
2968 mac_create_mbuf_from_socket(struct socket *socket, struct mbuf *mbuf)
2971 MAC_PERFORM(create_mbuf_from_socket, socket, &socket->so_label, mbuf,
2972 &mbuf->m_pkthdr.label);
2976 mac_create_mount(struct ucred *cred, struct mount *mp)
2979 MAC_PERFORM(create_mount, cred, mp, &mp->mnt_mntlabel,
2984 mac_create_root_mount(struct ucred *cred, struct mount *mp)
2987 MAC_PERFORM(create_root_mount, cred, mp, &mp->mnt_mntlabel,
2992 mac_check_bpfdesc_receive(struct bpf_d *bpf_d, struct ifnet *ifnet)
2996 if (!mac_enforce_network)
2999 MAC_CHECK(check_bpfdesc_receive, bpf_d, &bpf_d->bd_label, ifnet,
3006 mac_check_cred_relabel(struct ucred *cred, struct label *newlabel)
3010 MAC_CHECK(check_cred_relabel, cred, newlabel);
3016 mac_check_cred_visible(struct ucred *u1, struct ucred *u2)
3020 if (!mac_enforce_process)
3023 MAC_CHECK(check_cred_visible, u1, u2);
3029 mac_check_ifnet_transmit(struct ifnet *ifnet, struct mbuf *mbuf)
3033 if (!mac_enforce_network)
3036 KASSERT(mbuf->m_flags & M_PKTHDR, ("packet has no pkthdr"));
3037 if (!(mbuf->m_pkthdr.label.l_flags & MAC_FLAG_INITIALIZED))
3038 if_printf(ifnet, "not initialized\n");
3040 MAC_CHECK(check_ifnet_transmit, ifnet, &ifnet->if_label, mbuf,
3041 &mbuf->m_pkthdr.label);
3047 mac_check_mount_stat(struct ucred *cred, struct mount *mount)
3051 if (!mac_enforce_fs)
3054 MAC_CHECK(check_mount_stat, cred, mount, &mount->mnt_mntlabel);
3060 mac_check_pipe_ioctl(struct ucred *cred, struct pipe *pipe, unsigned long cmd,
3065 PIPE_LOCK_ASSERT(pipe, MA_OWNED);
3067 if (!mac_enforce_pipe)
3070 MAC_CHECK(check_pipe_ioctl, cred, pipe, pipe->pipe_label, cmd, data);
3076 mac_check_pipe_poll(struct ucred *cred, struct pipe *pipe)
3080 PIPE_LOCK_ASSERT(pipe, MA_OWNED);
3082 if (!mac_enforce_pipe)
3085 MAC_CHECK(check_pipe_poll, cred, pipe, pipe->pipe_label);
3091 mac_check_pipe_read(struct ucred *cred, struct pipe *pipe)
3095 PIPE_LOCK_ASSERT(pipe, MA_OWNED);
3097 if (!mac_enforce_pipe)
3100 MAC_CHECK(check_pipe_read, cred, pipe, pipe->pipe_label);
3106 mac_check_pipe_relabel(struct ucred *cred, struct pipe *pipe,
3107 struct label *newlabel)
3111 PIPE_LOCK_ASSERT(pipe, MA_OWNED);
3113 if (!mac_enforce_pipe)
3116 MAC_CHECK(check_pipe_relabel, cred, pipe, pipe->pipe_label, newlabel);
3122 mac_check_pipe_stat(struct ucred *cred, struct pipe *pipe)
3126 PIPE_LOCK_ASSERT(pipe, MA_OWNED);
3128 if (!mac_enforce_pipe)
3131 MAC_CHECK(check_pipe_stat, cred, pipe, pipe->pipe_label);
3137 mac_check_pipe_write(struct ucred *cred, struct pipe *pipe)
3141 PIPE_LOCK_ASSERT(pipe, MA_OWNED);
3143 if (!mac_enforce_pipe)
3146 MAC_CHECK(check_pipe_write, cred, pipe, pipe->pipe_label);
3152 mac_check_proc_debug(struct ucred *cred, struct proc *proc)
3156 PROC_LOCK_ASSERT(proc, MA_OWNED);
3158 if (!mac_enforce_process)
3161 MAC_CHECK(check_proc_debug, cred, proc);
3167 mac_check_proc_sched(struct ucred *cred, struct proc *proc)
3171 PROC_LOCK_ASSERT(proc, MA_OWNED);
3173 if (!mac_enforce_process)
3176 MAC_CHECK(check_proc_sched, cred, proc);
3182 mac_check_proc_signal(struct ucred *cred, struct proc *proc, int signum)
3186 PROC_LOCK_ASSERT(proc, MA_OWNED);
3188 if (!mac_enforce_process)
3191 MAC_CHECK(check_proc_signal, cred, proc, signum);
3197 mac_check_socket_bind(struct ucred *ucred, struct socket *socket,
3198 struct sockaddr *sockaddr)
3202 if (!mac_enforce_socket)
3205 MAC_CHECK(check_socket_bind, ucred, socket, &socket->so_label,
3212 mac_check_socket_connect(struct ucred *cred, struct socket *socket,
3213 struct sockaddr *sockaddr)
3217 if (!mac_enforce_socket)
3220 MAC_CHECK(check_socket_connect, cred, socket, &socket->so_label,
3227 mac_check_socket_deliver(struct socket *socket, struct mbuf *mbuf)
3231 if (!mac_enforce_socket)
3234 MAC_CHECK(check_socket_deliver, socket, &socket->so_label, mbuf,
3235 &mbuf->m_pkthdr.label);
3241 mac_check_socket_listen(struct ucred *cred, struct socket *socket)
3245 if (!mac_enforce_socket)
3248 MAC_CHECK(check_socket_listen, cred, socket, &socket->so_label);
3253 mac_check_socket_receive(struct ucred *cred, struct socket *so)
3257 if (!mac_enforce_socket)
3260 MAC_CHECK(check_socket_receive, cred, so, &so->so_label);
3266 mac_check_socket_relabel(struct ucred *cred, struct socket *socket,
3267 struct label *newlabel)
3271 MAC_CHECK(check_socket_relabel, cred, socket, &socket->so_label,
3278 mac_check_socket_send(struct ucred *cred, struct socket *so)
3282 if (!mac_enforce_socket)
3285 MAC_CHECK(check_socket_send, cred, so, &so->so_label);
3291 mac_check_socket_visible(struct ucred *cred, struct socket *socket)
3295 if (!mac_enforce_socket)
3298 MAC_CHECK(check_socket_visible, cred, socket, &socket->so_label);
3304 mac_ioctl_ifnet_get(struct ucred *cred, struct ifreq *ifr,
3305 struct ifnet *ifnet)
3307 char *elements, *buffer;
3311 error = copyin(ifr->ifr_ifru.ifru_data, &mac, sizeof(mac));
3315 error = mac_check_structmac_consistent(&mac);
3319 elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
3321 error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
3323 free(elements, M_MACTEMP);
3327 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
3329 error = mac_externalize_ifnet_label(&ifnet->if_label, elements,
3330 buffer, mac.m_buflen, M_WAITOK);
3332 error = copyout(buffer, mac.m_string, strlen(buffer)+1);
3334 free(buffer, M_MACTEMP);
3335 free(elements, M_MACTEMP);
3341 mac_ioctl_ifnet_set(struct ucred *cred, struct ifreq *ifr,
3342 struct ifnet *ifnet)
3344 struct label intlabel;
3349 error = copyin(ifr->ifr_ifru.ifru_data, &mac, sizeof(mac));
3353 error = mac_check_structmac_consistent(&mac);
3357 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
3359 error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
3361 free(buffer, M_MACTEMP);
3365 mac_init_ifnet_label(&intlabel);
3366 error = mac_internalize_ifnet_label(&intlabel, buffer);
3367 free(buffer, M_MACTEMP);
3369 mac_destroy_ifnet_label(&intlabel);
3374 * XXX: Note that this is a redundant privilege check, since
3375 * policies impose this check themselves if required by the
3376 * policy. Eventually, this should go away.
3378 error = suser_cred(cred, 0);
3380 mac_destroy_ifnet_label(&intlabel);
3384 MAC_CHECK(check_ifnet_relabel, cred, ifnet, &ifnet->if_label,
3387 mac_destroy_ifnet_label(&intlabel);
3391 MAC_PERFORM(relabel_ifnet, cred, ifnet, &ifnet->if_label, &intlabel);
3393 mac_destroy_ifnet_label(&intlabel);
3398 mac_create_devfs_vnode(struct devfs_dirent *de, struct vnode *vp)
3401 MAC_PERFORM(create_devfs_vnode, de, &de->de_label, vp, &vp->v_label);
3405 mac_create_devfs_device(dev_t dev, struct devfs_dirent *de)
3408 MAC_PERFORM(create_devfs_device, dev, de, &de->de_label);
3412 mac_create_devfs_symlink(struct ucred *cred, struct devfs_dirent *dd,
3413 struct devfs_dirent *de)
3416 MAC_PERFORM(create_devfs_symlink, cred, dd, &dd->de_label, de,
3421 mac_stdcreatevnode_ea(struct vnode *vp)
3425 MAC_CHECK(stdcreatevnode_ea, vp, &vp->v_label);
3431 mac_create_devfs_directory(char *dirname, int dirnamelen,
3432 struct devfs_dirent *de)
3435 MAC_PERFORM(create_devfs_directory, dirname, dirnamelen, de,
3440 * When a new vnode is created, this call will initialize its label.
3443 mac_create_vnode(struct ucred *cred, struct vnode *parent,
3444 struct vnode *child)
3448 ASSERT_VOP_LOCKED(parent, "mac_create_vnode");
3449 ASSERT_VOP_LOCKED(child, "mac_create_vnode");
3451 error = vn_refreshlabel(parent, cred);
3453 printf("mac_create_vnode: vn_refreshlabel returned %d\n",
3455 printf("mac_create_vnode: using old vnode label\n");
3458 MAC_PERFORM(create_vnode, cred, parent, &parent->v_label, child,
3463 mac_setsockopt_label_set(struct ucred *cred, struct socket *so,
3466 struct label intlabel;
3470 error = mac_check_structmac_consistent(mac);
3474 buffer = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK);
3476 error = copyinstr(mac->m_string, buffer, mac->m_buflen, NULL);
3478 free(buffer, M_MACTEMP);
3482 mac_init_socket_label(&intlabel, M_WAITOK);
3483 error = mac_internalize_socket_label(&intlabel, buffer);
3484 free(buffer, M_MACTEMP);
3486 mac_destroy_socket_label(&intlabel);
3490 mac_check_socket_relabel(cred, so, &intlabel);
3492 mac_destroy_socket_label(&intlabel);
3496 mac_relabel_socket(cred, so, &intlabel);
3498 mac_destroy_socket_label(&intlabel);
3503 mac_pipe_label_set(struct ucred *cred, struct pipe *pipe, struct label *label)
3507 PIPE_LOCK_ASSERT(pipe, MA_OWNED);
3509 error = mac_check_pipe_relabel(cred, pipe, label);
3513 mac_relabel_pipe(cred, pipe, label);
3519 mac_getsockopt_label_get(struct ucred *cred, struct socket *so,
3522 char *buffer, *elements;
3525 error = mac_check_structmac_consistent(mac);
3529 elements = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK);
3531 error = copyinstr(mac->m_string, elements, mac->m_buflen, NULL);
3533 free(elements, M_MACTEMP);
3537 buffer = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
3539 error = mac_externalize_socket_label(&so->so_label, elements,
3540 buffer, mac->m_buflen, M_WAITOK);
3542 error = copyout(buffer, mac->m_string, strlen(buffer)+1);
3544 free(buffer, M_MACTEMP);
3545 free(elements, M_MACTEMP);
3551 mac_getsockopt_peerlabel_get(struct ucred *cred, struct socket *so,
3554 char *elements, *buffer;
3557 error = mac_check_structmac_consistent(mac);
3561 elements = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK);
3563 error = copyinstr(mac->m_string, elements, mac->m_buflen, NULL);
3565 free(elements, M_MACTEMP);
3569 buffer = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
3571 error = mac_externalize_socket_peer_label(&so->so_peerlabel,
3572 elements, buffer, mac->m_buflen, M_WAITOK);
3574 error = copyout(buffer, mac->m_string, strlen(buffer)+1);
3576 free(buffer, M_MACTEMP);
3577 free(elements, M_MACTEMP);
3583 * Implementation of VOP_SETLABEL() that relies on extended attributes
3584 * to store label data. Can be referenced by filesystems supporting
3585 * extended attributes.
3588 vop_stdsetlabel_ea(struct vop_setlabel_args *ap)
3590 struct vnode *vp = ap->a_vp;
3591 struct label *intlabel = ap->a_label;
3592 struct oldmac extmac;
3595 ASSERT_VOP_LOCKED(vp, "vop_stdsetlabel_ea");
3598 * XXX: Eventually call out to EA check/set calls here.
3599 * Be particularly careful to avoid race conditions,
3600 * consistency problems, and stability problems when
3601 * dealing with multiple EAs. In particular, we require
3602 * the ability to write multiple EAs on the same file in
3603 * a single transaction, which the current EA interface
3607 error = mac_externalize_vnode_oldmac(intlabel, &extmac);
3611 error = vn_extattr_set(vp, IO_NODELOCKED,
3612 FREEBSD_MAC_EXTATTR_NAMESPACE, FREEBSD_MAC_EXTATTR_NAME,
3613 sizeof(extmac), (char *)&extmac, curthread);
3617 mac_relabel_vnode(ap->a_cred, vp, intlabel);
3619 vp->v_vflag |= VV_CACHEDLABEL;
3625 vn_setlabel(struct vnode *vp, struct label *intlabel, struct ucred *cred)
3629 if (vp->v_mount == NULL) {
3630 /* printf("vn_setlabel: null v_mount\n"); */
3631 if (vp->v_type != VNON)
3632 printf("vn_setlabel: null v_mount with non-VNON\n");
3636 if ((vp->v_mount->mnt_flag & MNT_MULTILABEL) == 0)
3637 return (EOPNOTSUPP);
3640 * Multi-phase commit. First check the policies to confirm the
3641 * change is OK. Then commit via the filesystem. Finally,
3642 * update the actual vnode label. Question: maybe the filesystem
3643 * should update the vnode at the end as part of VOP_SETLABEL()?
3645 error = mac_check_vnode_relabel(cred, vp, intlabel);
3650 * VADMIN provides the opportunity for the filesystem to make
3651 * decisions about who is and is not able to modify labels
3652 * and protections on files. This might not be right. We can't
3653 * assume VOP_SETLABEL() will do it, because we might implement
3654 * that as part of vop_stdsetlabel_ea().
3656 error = VOP_ACCESS(vp, VADMIN, cred, curthread);
3660 error = VOP_SETLABEL(vp, intlabel, cred, curthread);
3668 __mac_get_pid(struct thread *td, struct __mac_get_pid_args *uap)
3670 char *elements, *buffer;
3673 struct ucred *tcred;
3676 error = copyin(SCARG(uap, mac_p), &mac, sizeof(mac));
3680 error = mac_check_structmac_consistent(&mac);
3684 tproc = pfind(uap->pid);
3688 tcred = NULL; /* Satisfy gcc. */
3689 error = p_cansee(td, tproc);
3691 tcred = crhold(tproc->p_ucred);
3696 elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
3698 error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
3700 free(elements, M_MACTEMP);
3705 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
3707 error = mac_externalize_cred_label(&tcred->cr_label, elements,
3708 buffer, mac.m_buflen, M_WAITOK);
3710 error = copyout(buffer, mac.m_string, strlen(buffer)+1);
3712 free(buffer, M_MACTEMP);
3713 free(elements, M_MACTEMP);
3722 __mac_get_proc(struct thread *td, struct __mac_get_proc_args *uap)
3724 char *elements, *buffer;
3728 error = copyin(uap->mac_p, &mac, sizeof(mac));
3732 error = mac_check_structmac_consistent(&mac);
3736 elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
3738 error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
3740 free(elements, M_MACTEMP);
3744 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
3746 error = mac_externalize_cred_label(&td->td_ucred->cr_label,
3747 elements, buffer, mac.m_buflen, M_WAITOK);
3749 error = copyout(buffer, mac.m_string, strlen(buffer)+1);
3751 free(buffer, M_MACTEMP);
3752 free(elements, M_MACTEMP);
3760 __mac_set_proc(struct thread *td, struct __mac_set_proc_args *uap)
3762 struct ucred *newcred, *oldcred;
3763 struct label intlabel;
3769 error = copyin(uap->mac_p, &mac, sizeof(mac));
3773 error = mac_check_structmac_consistent(&mac);
3777 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
3779 error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
3781 free(buffer, M_MACTEMP);
3785 mac_init_cred_label(&intlabel);
3786 error = mac_internalize_cred_label(&intlabel, buffer);
3787 free(buffer, M_MACTEMP);
3789 mac_destroy_cred_label(&intlabel);
3797 oldcred = p->p_ucred;
3799 error = mac_check_cred_relabel(oldcred, &intlabel);
3807 crcopy(newcred, oldcred);
3808 mac_relabel_cred(newcred, &intlabel);
3809 p->p_ucred = newcred;
3812 * Grab additional reference for use while revoking mmaps, prior
3813 * to releasing the proc lock and sharing the cred.
3818 if (mac_enforce_vm) {
3820 mac_cred_mmapped_drop_perms(td, newcred);
3824 crfree(newcred); /* Free revocation reference. */
3828 mac_destroy_cred_label(&intlabel);
3836 __mac_get_fd(struct thread *td, struct __mac_get_fd_args *uap)
3838 char *elements, *buffer;
3839 struct label intlabel;
3847 error = copyin(uap->mac_p, &mac, sizeof(mac));
3851 error = mac_check_structmac_consistent(&mac);
3855 elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
3857 error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
3859 free(elements, M_MACTEMP);
3863 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
3865 mtx_lock(&Giant); /* VFS */
3866 error = fget(td, SCARG(uap, fd), &fp);
3870 label_type = fp->f_type;
3871 switch (fp->f_type) {
3874 vp = (struct vnode *)fp->f_data;
3876 mac_init_vnode_label(&intlabel);
3878 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3879 error = vn_refreshlabel(vp, td->td_ucred);
3881 mac_copy_vnode_label(&vp->v_label, &intlabel);
3882 VOP_UNLOCK(vp, 0, td);
3886 pipe = (struct pipe *)fp->f_data;
3888 mac_init_pipe_label(&intlabel);
3891 mac_copy_pipe_label(pipe->pipe_label, &intlabel);
3901 switch (label_type) {
3905 error = mac_externalize_vnode_label(&intlabel,
3906 elements, buffer, mac.m_buflen, M_WAITOK);
3907 mac_destroy_vnode_label(&intlabel);
3910 error = mac_externalize_pipe_label(&intlabel, elements,
3911 buffer, mac.m_buflen, M_WAITOK);
3912 mac_destroy_pipe_label(&intlabel);
3915 panic("__mac_get_fd: corrupted label_type");
3919 error = copyout(buffer, mac.m_string, strlen(buffer)+1);
3922 mtx_unlock(&Giant); /* VFS */
3923 free(buffer, M_MACTEMP);
3924 free(elements, M_MACTEMP);
3933 __mac_get_file(struct thread *td, struct __mac_get_file_args *uap)
3935 char *elements, *buffer;
3936 struct nameidata nd;
3937 struct label intlabel;
3941 error = copyin(uap->mac_p, &mac, sizeof(mac));
3945 error = mac_check_structmac_consistent(&mac);
3949 elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
3951 error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
3953 free(elements, M_MACTEMP);
3957 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
3959 mtx_lock(&Giant); /* VFS */
3960 NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_USERSPACE, uap->path_p,
3966 mac_init_vnode_label(&intlabel);
3967 error = vn_refreshlabel(nd.ni_vp, td->td_ucred);
3969 mac_copy_vnode_label(&nd.ni_vp->v_label, &intlabel);
3971 error = mac_externalize_vnode_label(&intlabel, elements,
3972 buffer, mac.m_buflen, M_WAITOK);
3975 mac_destroy_vnode_label(&intlabel);
3978 error = copyout(buffer, mac.m_string, strlen(buffer)+1);
3981 mtx_unlock(&Giant); /* VFS */
3983 free(buffer, M_MACTEMP);
3984 free(elements, M_MACTEMP);
3993 __mac_get_link(struct thread *td, struct __mac_get_link_args *uap)
3995 char *elements, *buffer;
3996 struct nameidata nd;
3997 struct label intlabel;
4001 error = copyin(uap->mac_p, &mac, sizeof(mac));
4005 error = mac_check_structmac_consistent(&mac);
4009 elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
4011 error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL);
4013 free(elements, M_MACTEMP);
4017 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO);
4019 mtx_lock(&Giant); /* VFS */
4020 NDINIT(&nd, LOOKUP, LOCKLEAF | NOFOLLOW, UIO_USERSPACE, uap->path_p,
4026 mac_init_vnode_label(&intlabel);
4027 error = vn_refreshlabel(nd.ni_vp, td->td_ucred);
4029 mac_copy_vnode_label(&nd.ni_vp->v_label, &intlabel);
4031 error = mac_externalize_vnode_label(&intlabel, elements,
4032 buffer, mac.m_buflen, M_WAITOK);
4034 mac_destroy_vnode_label(&intlabel);
4037 error = copyout(buffer, mac.m_string, strlen(buffer)+1);
4040 mtx_unlock(&Giant); /* VFS */
4042 free(buffer, M_MACTEMP);
4043 free(elements, M_MACTEMP);
4052 __mac_set_fd(struct thread *td, struct __mac_set_fd_args *uap)
4054 struct label intlabel;
4063 error = copyin(uap->mac_p, &mac, sizeof(mac));
4067 error = mac_check_structmac_consistent(&mac);
4071 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
4073 error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
4075 free(buffer, M_MACTEMP);
4079 mtx_lock(&Giant); /* VFS */
4081 error = fget(td, SCARG(uap, fd), &fp);
4085 switch (fp->f_type) {
4088 mac_init_vnode_label(&intlabel);
4089 error = mac_internalize_vnode_label(&intlabel, buffer);
4091 mac_destroy_vnode_label(&intlabel);
4095 vp = (struct vnode *)fp->f_data;
4096 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4098 mac_destroy_vnode_label(&intlabel);
4102 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4103 error = vn_setlabel(vp, &intlabel, td->td_ucred);
4104 VOP_UNLOCK(vp, 0, td);
4105 vn_finished_write(mp);
4107 mac_destroy_vnode_label(&intlabel);
4111 mac_init_pipe_label(&intlabel);
4112 error = mac_internalize_pipe_label(&intlabel, buffer);
4114 pipe = (struct pipe *)fp->f_data;
4116 error = mac_pipe_label_set(td->td_ucred, pipe,
4121 mac_destroy_pipe_label(&intlabel);
4130 mtx_unlock(&Giant); /* VFS */
4132 free(buffer, M_MACTEMP);
4141 __mac_set_file(struct thread *td, struct __mac_set_file_args *uap)
4143 struct label intlabel;
4144 struct nameidata nd;
4150 error = copyin(uap->mac_p, &mac, sizeof(mac));
4154 error = mac_check_structmac_consistent(&mac);
4158 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
4160 error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
4162 free(buffer, M_MACTEMP);
4166 mac_init_vnode_label(&intlabel);
4167 error = mac_internalize_vnode_label(&intlabel, buffer);
4168 free(buffer, M_MACTEMP);
4170 mac_destroy_vnode_label(&intlabel);
4174 mtx_lock(&Giant); /* VFS */
4176 NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_USERSPACE, uap->path_p,
4180 error = vn_start_write(nd.ni_vp, &mp, V_WAIT | PCATCH);
4182 error = vn_setlabel(nd.ni_vp, &intlabel,
4184 vn_finished_write(mp);
4188 mtx_unlock(&Giant); /* VFS */
4189 mac_destroy_vnode_label(&intlabel);
4198 __mac_set_link(struct thread *td, struct __mac_set_link_args *uap)
4200 struct label intlabel;
4201 struct nameidata nd;
4207 error = copyin(uap->mac_p, &mac, sizeof(mac));
4211 error = mac_check_structmac_consistent(&mac);
4215 buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK);
4217 error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL);
4219 free(buffer, M_MACTEMP);
4223 mac_init_vnode_label(&intlabel);
4224 error = mac_internalize_vnode_label(&intlabel, buffer);
4225 free(buffer, M_MACTEMP);
4227 mac_destroy_vnode_label(&intlabel);
4231 mtx_lock(&Giant); /* VFS */
4233 NDINIT(&nd, LOOKUP, LOCKLEAF | NOFOLLOW, UIO_USERSPACE, uap->path_p,
4237 error = vn_start_write(nd.ni_vp, &mp, V_WAIT | PCATCH);
4239 error = vn_setlabel(nd.ni_vp, &intlabel,
4241 vn_finished_write(mp);
4245 mtx_unlock(&Giant); /* VFS */
4246 mac_destroy_vnode_label(&intlabel);
4255 mac_syscall(struct thread *td, struct mac_syscall_args *uap)
4257 struct mac_policy_conf *mpc;
4258 char target[MAC_MAX_POLICY_NAME];
4261 error = copyinstr(SCARG(uap, policy), target, sizeof(target), NULL);
4266 MAC_POLICY_LIST_BUSY();
4267 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {
4268 if (strcmp(mpc->mpc_name, target) == 0 &&
4269 mpc->mpc_ops->mpo_syscall != NULL) {
4270 error = mpc->mpc_ops->mpo_syscall(td,
4271 SCARG(uap, call), SCARG(uap, arg));
4277 MAC_POLICY_LIST_UNBUSY();
4281 SYSINIT(mac, SI_SUB_MAC, SI_ORDER_FIRST, mac_init, NULL);
4282 SYSINIT(mac_late, SI_SUB_MAC_LATE, SI_ORDER_FIRST, mac_late_init, NULL);
4287 __mac_get_pid(struct thread *td, struct __mac_get_pid_args *uap)
4294 __mac_get_proc(struct thread *td, struct __mac_get_proc_args *uap)
4301 __mac_set_proc(struct thread *td, struct __mac_set_proc_args *uap)
4308 __mac_get_fd(struct thread *td, struct __mac_get_fd_args *uap)
4315 __mac_get_file(struct thread *td, struct __mac_get_file_args *uap)
4322 __mac_get_link(struct thread *td, struct __mac_get_link_args *uap)
4329 __mac_set_fd(struct thread *td, struct __mac_set_fd_args *uap)
4336 __mac_set_file(struct thread *td, struct __mac_set_file_args *uap)
4343 __mac_set_link(struct thread *td, struct __mac_set_link_args *uap)
4350 mac_syscall(struct thread *td, struct mac_syscall_args *uap)