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 #ifndef MAC_MAX_POLICIES
103 #define MAC_MAX_POLICIES 8
105 #if MAC_MAX_POLICIES > 32
106 #error "MAC_MAX_POLICIES too large"
108 static unsigned int mac_max_policies = MAC_MAX_POLICIES;
109 static unsigned int mac_policy_offsets_free = (1 << MAC_MAX_POLICIES) - 1;
110 SYSCTL_UINT(_security_mac, OID_AUTO, max_policies, CTLFLAG_RD,
111 &mac_max_policies, 0, "");
113 static int mac_late = 0;
115 static int mac_enforce_fs = 1;
116 SYSCTL_INT(_security_mac, OID_AUTO, enforce_fs, CTLFLAG_RW,
117 &mac_enforce_fs, 0, "Enforce MAC policy on file system objects");
118 TUNABLE_INT("security.mac.enforce_fs", &mac_enforce_fs);
120 static int mac_enforce_network = 1;
121 SYSCTL_INT(_security_mac, OID_AUTO, enforce_network, CTLFLAG_RW,
122 &mac_enforce_network, 0, "Enforce MAC policy on network packets");
123 TUNABLE_INT("security.mac.enforce_network", &mac_enforce_network);
125 static int mac_enforce_pipe = 1;
126 SYSCTL_INT(_security_mac, OID_AUTO, enforce_pipe, CTLFLAG_RW,
127 &mac_enforce_pipe, 0, "Enforce MAC policy on pipe operations");
128 TUNABLE_INT("security.mac.enforce_pipe", &mac_enforce_pipe);
130 static int mac_enforce_process = 1;
131 SYSCTL_INT(_security_mac, OID_AUTO, enforce_process, CTLFLAG_RW,
132 &mac_enforce_process, 0, "Enforce MAC policy on inter-process operations");
133 TUNABLE_INT("security.mac.enforce_process", &mac_enforce_process);
135 static int mac_enforce_socket = 1;
136 SYSCTL_INT(_security_mac, OID_AUTO, enforce_socket, CTLFLAG_RW,
137 &mac_enforce_socket, 0, "Enforce MAC policy on socket operations");
138 TUNABLE_INT("security.mac.enforce_socket", &mac_enforce_socket);
140 static int mac_enforce_vm = 1;
141 SYSCTL_INT(_security_mac, OID_AUTO, enforce_vm, CTLFLAG_RW,
142 &mac_enforce_vm, 0, "Enforce MAC policy on vm operations");
143 TUNABLE_INT("security.mac.enforce_vm", &mac_enforce_vm);
145 static int mac_label_size = sizeof(struct mac);
146 SYSCTL_INT(_security_mac, OID_AUTO, label_size, CTLFLAG_RD,
147 &mac_label_size, 0, "Pre-compiled MAC label size");
149 static int mac_cache_fslabel_in_vnode = 1;
150 SYSCTL_INT(_security_mac, OID_AUTO, cache_fslabel_in_vnode, CTLFLAG_RW,
151 &mac_cache_fslabel_in_vnode, 0, "Cache mount fslabel in vnode");
152 TUNABLE_INT("security.mac.cache_fslabel_in_vnode",
153 &mac_cache_fslabel_in_vnode);
155 static int mac_vnode_label_cache_hits = 0;
156 SYSCTL_INT(_security_mac, OID_AUTO, vnode_label_cache_hits, CTLFLAG_RD,
157 &mac_vnode_label_cache_hits, 0, "Cache hits on vnode labels");
158 static int mac_vnode_label_cache_misses = 0;
159 SYSCTL_INT(_security_mac, OID_AUTO, vnode_label_cache_misses, CTLFLAG_RD,
160 &mac_vnode_label_cache_misses, 0, "Cache misses on vnode labels");
162 static int mac_mmap_revocation = 1;
163 SYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation, CTLFLAG_RW,
164 &mac_mmap_revocation, 0, "Revoke mmap access to files on subject "
166 static int mac_mmap_revocation_via_cow = 0;
167 SYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation_via_cow, CTLFLAG_RW,
168 &mac_mmap_revocation_via_cow, 0, "Revoke mmap access to files via "
169 "copy-on-write semantics, or by removing all write access");
172 SYSCTL_NODE(_security_mac, OID_AUTO, debug, CTLFLAG_RW, 0,
173 "TrustedBSD MAC debug info");
175 static int mac_debug_label_fallback = 0;
176 SYSCTL_INT(_security_mac_debug, OID_AUTO, label_fallback, CTLFLAG_RW,
177 &mac_debug_label_fallback, 0, "Filesystems should fall back to fs label"
178 "when label is corrupted.");
179 TUNABLE_INT("security.mac.debug_label_fallback",
180 &mac_debug_label_fallback);
182 SYSCTL_NODE(_security_mac_debug, OID_AUTO, counters, CTLFLAG_RW, 0,
183 "TrustedBSD MAC object counters");
185 static unsigned int nmacmbufs, nmaccreds, nmacifnets, nmacbpfdescs,
186 nmacsockets, nmacmounts, nmactemp, nmacvnodes, nmacdevfsdirents,
189 SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, mbufs, CTLFLAG_RD,
190 &nmacmbufs, 0, "number of mbufs in use");
191 SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, creds, CTLFLAG_RD,
192 &nmaccreds, 0, "number of ucreds in use");
193 SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, ifnets, CTLFLAG_RD,
194 &nmacifnets, 0, "number of ifnets in use");
195 SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, ipqs, CTLFLAG_RD,
196 &nmacipqs, 0, "number of ipqs in use");
197 SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, bpfdescs, CTLFLAG_RD,
198 &nmacbpfdescs, 0, "number of bpfdescs in use");
199 SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, sockets, CTLFLAG_RD,
200 &nmacsockets, 0, "number of sockets in use");
201 SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, pipes, CTLFLAG_RD,
202 &nmacpipes, 0, "number of pipes in use");
203 SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, mounts, CTLFLAG_RD,
204 &nmacmounts, 0, "number of mounts in use");
205 SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, temp, CTLFLAG_RD,
206 &nmactemp, 0, "number of temporary labels in use");
207 SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, vnodes, CTLFLAG_RD,
208 &nmacvnodes, 0, "number of vnodes in use");
209 SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, devfsdirents, CTLFLAG_RD,
210 &nmacdevfsdirents, 0, "number of devfs dirents inuse");
213 static int error_select(int error1, int error2);
214 static int mac_externalize(struct label *label, struct mac *mac);
215 static int mac_policy_register(struct mac_policy_conf *mpc);
216 static int mac_policy_unregister(struct mac_policy_conf *mpc);
218 static int mac_stdcreatevnode_ea(struct vnode *vp);
219 static void mac_cred_mmapped_drop_perms(struct thread *td,
221 static void mac_cred_mmapped_drop_perms_recurse(struct thread *td,
222 struct ucred *cred, struct vm_map *map);
224 MALLOC_DEFINE(M_MACOPVEC, "macopvec", "MAC policy operation vector");
225 MALLOC_DEFINE(M_MACPIPELABEL, "macpipelabel", "MAC labels for pipes");
228 * mac_policy_list_lock protects the consistency of 'mac_policy_list',
229 * the linked list of attached policy modules. Read-only consumers of
230 * the list must acquire a shared lock for the duration of their use;
231 * writers must acquire an exclusive lock. Note that for compound
232 * operations, locks should be held for the entire compound operation,
233 * and that this is not yet done for relabel requests.
235 static struct mtx mac_policy_list_lock;
236 static LIST_HEAD(, mac_policy_conf) mac_policy_list;
237 static int mac_policy_list_busy;
238 #define MAC_POLICY_LIST_LOCKINIT() mtx_init(&mac_policy_list_lock, \
239 "mac_policy_list_lock", NULL, MTX_DEF);
240 #define MAC_POLICY_LIST_LOCK() mtx_lock(&mac_policy_list_lock);
241 #define MAC_POLICY_LIST_UNLOCK() mtx_unlock(&mac_policy_list_lock);
243 #define MAC_POLICY_LIST_BUSY() do { \
244 MAC_POLICY_LIST_LOCK(); \
245 mac_policy_list_busy++; \
246 MAC_POLICY_LIST_UNLOCK(); \
249 #define MAC_POLICY_LIST_UNBUSY() do { \
250 MAC_POLICY_LIST_LOCK(); \
251 mac_policy_list_busy--; \
252 if (mac_policy_list_busy < 0) \
253 panic("Extra mac_policy_list_busy--"); \
254 MAC_POLICY_LIST_UNLOCK(); \
258 * MAC_CHECK performs the designated check by walking the policy
259 * module list and checking with each as to how it feels about the
260 * request. Note that it returns its value via 'error' in the scope
263 #define MAC_CHECK(check, args...) do { \
264 struct mac_policy_conf *mpc; \
267 MAC_POLICY_LIST_BUSY(); \
268 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \
269 if (mpc->mpc_ops->mpo_ ## check != NULL) \
270 error = error_select( \
271 mpc->mpc_ops->mpo_ ## check (args), \
274 MAC_POLICY_LIST_UNBUSY(); \
278 * MAC_BOOLEAN performs the designated boolean composition by walking
279 * the module list, invoking each instance of the operation, and
280 * combining the results using the passed C operator. Note that it
281 * returns its value via 'result' in the scope of the caller, which
282 * should be initialized by the caller in a meaningful way to get
283 * a meaningful result.
285 #define MAC_BOOLEAN(operation, composition, args...) do { \
286 struct mac_policy_conf *mpc; \
288 MAC_POLICY_LIST_BUSY(); \
289 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \
290 if (mpc->mpc_ops->mpo_ ## operation != NULL) \
291 result = result composition \
292 mpc->mpc_ops->mpo_ ## operation (args); \
294 MAC_POLICY_LIST_UNBUSY(); \
298 * MAC_PERFORM performs the designated operation by walking the policy
299 * module list and invoking that operation for each policy.
301 #define MAC_PERFORM(operation, args...) do { \
302 struct mac_policy_conf *mpc; \
304 MAC_POLICY_LIST_BUSY(); \
305 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \
306 if (mpc->mpc_ops->mpo_ ## operation != NULL) \
307 mpc->mpc_ops->mpo_ ## operation (args); \
309 MAC_POLICY_LIST_UNBUSY(); \
313 * Initialize the MAC subsystem, including appropriate SMP locks.
319 LIST_INIT(&mac_policy_list);
320 MAC_POLICY_LIST_LOCKINIT();
324 * For the purposes of modules that want to know if they were loaded
325 * "early", set the mac_late flag once we've processed modules either
326 * linked into the kernel, or loaded before the kernel startup.
336 * Allow MAC policy modules to register during boot, etc.
339 mac_policy_modevent(module_t mod, int type, void *data)
341 struct mac_policy_conf *mpc;
345 mpc = (struct mac_policy_conf *) data;
349 if (mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_NOTLATE &&
351 printf("mac_policy_modevent: can't load %s policy "
352 "after booting\n", mpc->mpc_name);
356 error = mac_policy_register(mpc);
359 /* Don't unregister the module if it was never registered. */
360 if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED)
362 error = mac_policy_unregister(mpc);
374 mac_policy_register(struct mac_policy_conf *mpc)
376 struct mac_policy_conf *tmpc;
377 struct mac_policy_op_entry *mpe;
380 MALLOC(mpc->mpc_ops, struct mac_policy_ops *, sizeof(*mpc->mpc_ops),
381 M_MACOPVEC, M_WAITOK | M_ZERO);
382 for (mpe = mpc->mpc_entries; mpe->mpe_constant != MAC_OP_LAST; mpe++) {
383 switch (mpe->mpe_constant) {
386 * Doesn't actually happen, but this allows checking
387 * that all enumerated values are handled.
391 mpc->mpc_ops->mpo_destroy =
395 mpc->mpc_ops->mpo_init =
399 mpc->mpc_ops->mpo_syscall =
402 case MAC_INIT_BPFDESC_LABEL:
403 mpc->mpc_ops->mpo_init_bpfdesc_label =
406 case MAC_INIT_CRED_LABEL:
407 mpc->mpc_ops->mpo_init_cred_label =
410 case MAC_INIT_DEVFSDIRENT_LABEL:
411 mpc->mpc_ops->mpo_init_devfsdirent_label =
414 case MAC_INIT_IFNET_LABEL:
415 mpc->mpc_ops->mpo_init_ifnet_label =
418 case MAC_INIT_IPQ_LABEL:
419 mpc->mpc_ops->mpo_init_ipq_label =
422 case MAC_INIT_MBUF_LABEL:
423 mpc->mpc_ops->mpo_init_mbuf_label =
426 case MAC_INIT_MOUNT_LABEL:
427 mpc->mpc_ops->mpo_init_mount_label =
430 case MAC_INIT_MOUNT_FS_LABEL:
431 mpc->mpc_ops->mpo_init_mount_fs_label =
434 case MAC_INIT_PIPE_LABEL:
435 mpc->mpc_ops->mpo_init_pipe_label =
438 case MAC_INIT_SOCKET_LABEL:
439 mpc->mpc_ops->mpo_init_socket_label =
442 case MAC_INIT_SOCKET_PEER_LABEL:
443 mpc->mpc_ops->mpo_init_socket_peer_label =
446 case MAC_INIT_TEMP_LABEL:
447 mpc->mpc_ops->mpo_init_temp_label =
450 case MAC_INIT_VNODE_LABEL:
451 mpc->mpc_ops->mpo_init_vnode_label =
454 case MAC_DESTROY_BPFDESC_LABEL:
455 mpc->mpc_ops->mpo_destroy_bpfdesc_label =
458 case MAC_DESTROY_CRED_LABEL:
459 mpc->mpc_ops->mpo_destroy_cred_label =
462 case MAC_DESTROY_DEVFSDIRENT_LABEL:
463 mpc->mpc_ops->mpo_destroy_devfsdirent_label =
466 case MAC_DESTROY_IFNET_LABEL:
467 mpc->mpc_ops->mpo_destroy_ifnet_label =
470 case MAC_DESTROY_IPQ_LABEL:
471 mpc->mpc_ops->mpo_destroy_ipq_label =
474 case MAC_DESTROY_MBUF_LABEL:
475 mpc->mpc_ops->mpo_destroy_mbuf_label =
478 case MAC_DESTROY_MOUNT_LABEL:
479 mpc->mpc_ops->mpo_destroy_mount_label =
482 case MAC_DESTROY_MOUNT_FS_LABEL:
483 mpc->mpc_ops->mpo_destroy_mount_fs_label =
486 case MAC_DESTROY_PIPE_LABEL:
487 mpc->mpc_ops->mpo_destroy_pipe_label =
490 case MAC_DESTROY_SOCKET_LABEL:
491 mpc->mpc_ops->mpo_destroy_socket_label =
494 case MAC_DESTROY_SOCKET_PEER_LABEL:
495 mpc->mpc_ops->mpo_destroy_socket_peer_label =
498 case MAC_DESTROY_TEMP_LABEL:
499 mpc->mpc_ops->mpo_destroy_temp_label =
502 case MAC_DESTROY_VNODE_LABEL:
503 mpc->mpc_ops->mpo_destroy_vnode_label =
506 case MAC_EXTERNALIZE:
507 mpc->mpc_ops->mpo_externalize =
510 case MAC_INTERNALIZE:
511 mpc->mpc_ops->mpo_internalize =
514 case MAC_CREATE_DEVFS_DEVICE:
515 mpc->mpc_ops->mpo_create_devfs_device =
518 case MAC_CREATE_DEVFS_DIRECTORY:
519 mpc->mpc_ops->mpo_create_devfs_directory =
522 case MAC_CREATE_DEVFS_VNODE:
523 mpc->mpc_ops->mpo_create_devfs_vnode =
526 case MAC_STDCREATEVNODE_EA:
527 mpc->mpc_ops->mpo_stdcreatevnode_ea =
530 case MAC_CREATE_VNODE:
531 mpc->mpc_ops->mpo_create_vnode =
534 case MAC_CREATE_MOUNT:
535 mpc->mpc_ops->mpo_create_mount =
538 case MAC_CREATE_ROOT_MOUNT:
539 mpc->mpc_ops->mpo_create_root_mount =
542 case MAC_RELABEL_VNODE:
543 mpc->mpc_ops->mpo_relabel_vnode =
546 case MAC_UPDATE_DEVFSDIRENT:
547 mpc->mpc_ops->mpo_update_devfsdirent =
550 case MAC_UPDATE_PROCFSVNODE:
551 mpc->mpc_ops->mpo_update_procfsvnode =
554 case MAC_UPDATE_VNODE_FROM_EXTATTR:
555 mpc->mpc_ops->mpo_update_vnode_from_extattr =
558 case MAC_UPDATE_VNODE_FROM_EXTERNALIZED:
559 mpc->mpc_ops->mpo_update_vnode_from_externalized =
562 case MAC_UPDATE_VNODE_FROM_MOUNT:
563 mpc->mpc_ops->mpo_update_vnode_from_mount =
566 case MAC_CREATE_MBUF_FROM_SOCKET:
567 mpc->mpc_ops->mpo_create_mbuf_from_socket =
570 case MAC_CREATE_PIPE:
571 mpc->mpc_ops->mpo_create_pipe =
574 case MAC_CREATE_SOCKET:
575 mpc->mpc_ops->mpo_create_socket =
578 case MAC_CREATE_SOCKET_FROM_SOCKET:
579 mpc->mpc_ops->mpo_create_socket_from_socket =
582 case MAC_RELABEL_PIPE:
583 mpc->mpc_ops->mpo_relabel_pipe =
586 case MAC_RELABEL_SOCKET:
587 mpc->mpc_ops->mpo_relabel_socket =
590 case MAC_SET_SOCKET_PEER_FROM_MBUF:
591 mpc->mpc_ops->mpo_set_socket_peer_from_mbuf =
594 case MAC_SET_SOCKET_PEER_FROM_SOCKET:
595 mpc->mpc_ops->mpo_set_socket_peer_from_socket =
598 case MAC_CREATE_BPFDESC:
599 mpc->mpc_ops->mpo_create_bpfdesc =
602 case MAC_CREATE_DATAGRAM_FROM_IPQ:
603 mpc->mpc_ops->mpo_create_datagram_from_ipq =
606 case MAC_CREATE_FRAGMENT:
607 mpc->mpc_ops->mpo_create_fragment =
610 case MAC_CREATE_IFNET:
611 mpc->mpc_ops->mpo_create_ifnet =
615 mpc->mpc_ops->mpo_create_ipq =
618 case MAC_CREATE_MBUF_FROM_MBUF:
619 mpc->mpc_ops->mpo_create_mbuf_from_mbuf =
622 case MAC_CREATE_MBUF_LINKLAYER:
623 mpc->mpc_ops->mpo_create_mbuf_linklayer =
626 case MAC_CREATE_MBUF_FROM_BPFDESC:
627 mpc->mpc_ops->mpo_create_mbuf_from_bpfdesc =
630 case MAC_CREATE_MBUF_FROM_IFNET:
631 mpc->mpc_ops->mpo_create_mbuf_from_ifnet =
634 case MAC_CREATE_MBUF_MULTICAST_ENCAP:
635 mpc->mpc_ops->mpo_create_mbuf_multicast_encap =
638 case MAC_CREATE_MBUF_NETLAYER:
639 mpc->mpc_ops->mpo_create_mbuf_netlayer =
642 case MAC_FRAGMENT_MATCH:
643 mpc->mpc_ops->mpo_fragment_match =
646 case MAC_RELABEL_IFNET:
647 mpc->mpc_ops->mpo_relabel_ifnet =
651 mpc->mpc_ops->mpo_update_ipq =
654 case MAC_CREATE_CRED:
655 mpc->mpc_ops->mpo_create_cred =
658 case MAC_EXECVE_TRANSITION:
659 mpc->mpc_ops->mpo_execve_transition =
662 case MAC_EXECVE_WILL_TRANSITION:
663 mpc->mpc_ops->mpo_execve_will_transition =
666 case MAC_CREATE_PROC0:
667 mpc->mpc_ops->mpo_create_proc0 = mpe->mpe_function;
669 case MAC_CREATE_PROC1:
670 mpc->mpc_ops->mpo_create_proc1 = mpe->mpe_function;
672 case MAC_RELABEL_CRED:
673 mpc->mpc_ops->mpo_relabel_cred =
676 case MAC_THREAD_USERRET:
677 mpc->mpc_ops->mpo_thread_userret =
680 case MAC_CHECK_BPFDESC_RECEIVE:
681 mpc->mpc_ops->mpo_check_bpfdesc_receive =
684 case MAC_CHECK_CRED_RELABEL:
685 mpc->mpc_ops->mpo_check_cred_relabel =
688 case MAC_CHECK_CRED_VISIBLE:
689 mpc->mpc_ops->mpo_check_cred_visible =
692 case MAC_CHECK_IFNET_RELABEL:
693 mpc->mpc_ops->mpo_check_ifnet_relabel =
696 case MAC_CHECK_IFNET_TRANSMIT:
697 mpc->mpc_ops->mpo_check_ifnet_transmit =
700 case MAC_CHECK_MOUNT_STAT:
701 mpc->mpc_ops->mpo_check_mount_stat =
704 case MAC_CHECK_PIPE_IOCTL:
705 mpc->mpc_ops->mpo_check_pipe_ioctl =
708 case MAC_CHECK_PIPE_POLL:
709 mpc->mpc_ops->mpo_check_pipe_poll =
712 case MAC_CHECK_PIPE_READ:
713 mpc->mpc_ops->mpo_check_pipe_read =
716 case MAC_CHECK_PIPE_RELABEL:
717 mpc->mpc_ops->mpo_check_pipe_relabel =
720 case MAC_CHECK_PIPE_STAT:
721 mpc->mpc_ops->mpo_check_pipe_stat =
724 case MAC_CHECK_PIPE_WRITE:
725 mpc->mpc_ops->mpo_check_pipe_write =
728 case MAC_CHECK_PROC_DEBUG:
729 mpc->mpc_ops->mpo_check_proc_debug =
732 case MAC_CHECK_PROC_SCHED:
733 mpc->mpc_ops->mpo_check_proc_sched =
736 case MAC_CHECK_PROC_SIGNAL:
737 mpc->mpc_ops->mpo_check_proc_signal =
740 case MAC_CHECK_SOCKET_BIND:
741 mpc->mpc_ops->mpo_check_socket_bind =
744 case MAC_CHECK_SOCKET_CONNECT:
745 mpc->mpc_ops->mpo_check_socket_connect =
748 case MAC_CHECK_SOCKET_DELIVER:
749 mpc->mpc_ops->mpo_check_socket_deliver =
752 case MAC_CHECK_SOCKET_LISTEN:
753 mpc->mpc_ops->mpo_check_socket_listen =
756 case MAC_CHECK_SOCKET_RELABEL:
757 mpc->mpc_ops->mpo_check_socket_relabel =
760 case MAC_CHECK_SOCKET_VISIBLE:
761 mpc->mpc_ops->mpo_check_socket_visible =
764 case MAC_CHECK_VNODE_ACCESS:
765 mpc->mpc_ops->mpo_check_vnode_access =
768 case MAC_CHECK_VNODE_CHDIR:
769 mpc->mpc_ops->mpo_check_vnode_chdir =
772 case MAC_CHECK_VNODE_CHROOT:
773 mpc->mpc_ops->mpo_check_vnode_chroot =
776 case MAC_CHECK_VNODE_CREATE:
777 mpc->mpc_ops->mpo_check_vnode_create =
780 case MAC_CHECK_VNODE_DELETE:
781 mpc->mpc_ops->mpo_check_vnode_delete =
784 case MAC_CHECK_VNODE_DELETEACL:
785 mpc->mpc_ops->mpo_check_vnode_deleteacl =
788 case MAC_CHECK_VNODE_EXEC:
789 mpc->mpc_ops->mpo_check_vnode_exec =
792 case MAC_CHECK_VNODE_GETACL:
793 mpc->mpc_ops->mpo_check_vnode_getacl =
796 case MAC_CHECK_VNODE_GETEXTATTR:
797 mpc->mpc_ops->mpo_check_vnode_getextattr =
800 case MAC_CHECK_VNODE_LOOKUP:
801 mpc->mpc_ops->mpo_check_vnode_lookup =
804 case MAC_CHECK_VNODE_MMAP_PERMS:
805 mpc->mpc_ops->mpo_check_vnode_mmap_perms =
808 case MAC_CHECK_VNODE_OPEN:
809 mpc->mpc_ops->mpo_check_vnode_open =
812 case MAC_CHECK_VNODE_POLL:
813 mpc->mpc_ops->mpo_check_vnode_poll =
816 case MAC_CHECK_VNODE_READ:
817 mpc->mpc_ops->mpo_check_vnode_read =
820 case MAC_CHECK_VNODE_READDIR:
821 mpc->mpc_ops->mpo_check_vnode_readdir =
824 case MAC_CHECK_VNODE_READLINK:
825 mpc->mpc_ops->mpo_check_vnode_readlink =
828 case MAC_CHECK_VNODE_RELABEL:
829 mpc->mpc_ops->mpo_check_vnode_relabel =
832 case MAC_CHECK_VNODE_RENAME_FROM:
833 mpc->mpc_ops->mpo_check_vnode_rename_from =
836 case MAC_CHECK_VNODE_RENAME_TO:
837 mpc->mpc_ops->mpo_check_vnode_rename_to =
840 case MAC_CHECK_VNODE_REVOKE:
841 mpc->mpc_ops->mpo_check_vnode_revoke =
844 case MAC_CHECK_VNODE_SETACL:
845 mpc->mpc_ops->mpo_check_vnode_setacl =
848 case MAC_CHECK_VNODE_SETEXTATTR:
849 mpc->mpc_ops->mpo_check_vnode_setextattr =
852 case MAC_CHECK_VNODE_SETFLAGS:
853 mpc->mpc_ops->mpo_check_vnode_setflags =
856 case MAC_CHECK_VNODE_SETMODE:
857 mpc->mpc_ops->mpo_check_vnode_setmode =
860 case MAC_CHECK_VNODE_SETOWNER:
861 mpc->mpc_ops->mpo_check_vnode_setowner =
864 case MAC_CHECK_VNODE_SETUTIMES:
865 mpc->mpc_ops->mpo_check_vnode_setutimes =
868 case MAC_CHECK_VNODE_STAT:
869 mpc->mpc_ops->mpo_check_vnode_stat =
872 case MAC_CHECK_VNODE_WRITE:
873 mpc->mpc_ops->mpo_check_vnode_write =
878 printf("MAC policy `%s': unknown operation %d\n",
879 mpc->mpc_name, mpe->mpe_constant);
884 MAC_POLICY_LIST_LOCK();
885 if (mac_policy_list_busy > 0) {
886 MAC_POLICY_LIST_UNLOCK();
887 FREE(mpc->mpc_ops, M_MACOPVEC);
891 LIST_FOREACH(tmpc, &mac_policy_list, mpc_list) {
892 if (strcmp(tmpc->mpc_name, mpc->mpc_name) == 0) {
893 MAC_POLICY_LIST_UNLOCK();
894 FREE(mpc->mpc_ops, M_MACOPVEC);
899 if (mpc->mpc_field_off != NULL) {
900 slot = ffs(mac_policy_offsets_free);
902 MAC_POLICY_LIST_UNLOCK();
903 FREE(mpc->mpc_ops, M_MACOPVEC);
908 mac_policy_offsets_free &= ~(1 << slot);
909 *mpc->mpc_field_off = slot;
911 mpc->mpc_runtime_flags |= MPC_RUNTIME_FLAG_REGISTERED;
912 LIST_INSERT_HEAD(&mac_policy_list, mpc, mpc_list);
914 /* Per-policy initialization. */
915 if (mpc->mpc_ops->mpo_init != NULL)
916 (*(mpc->mpc_ops->mpo_init))(mpc);
917 MAC_POLICY_LIST_UNLOCK();
919 printf("Security policy loaded: %s (%s)\n", mpc->mpc_fullname,
926 mac_policy_unregister(struct mac_policy_conf *mpc)
931 * Don't allow unloading modules with private data.
933 if (mpc->mpc_field_off != NULL)
936 if ((mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK) == 0)
938 MAC_POLICY_LIST_LOCK();
939 if (mac_policy_list_busy > 0) {
940 MAC_POLICY_LIST_UNLOCK();
943 if (mpc->mpc_ops->mpo_destroy != NULL)
944 (*(mpc->mpc_ops->mpo_destroy))(mpc);
946 LIST_REMOVE(mpc, mpc_list);
947 MAC_POLICY_LIST_UNLOCK();
949 FREE(mpc->mpc_ops, M_MACOPVEC);
952 printf("Security policy unload: %s (%s)\n", mpc->mpc_fullname,
959 * Define an error value precedence, and given two arguments, selects the
960 * value with the higher precedence.
963 error_select(int error1, int error2)
966 /* Certain decision-making errors take top priority. */
967 if (error1 == EDEADLK || error2 == EDEADLK)
970 /* Invalid arguments should be reported where possible. */
971 if (error1 == EINVAL || error2 == EINVAL)
974 /* Precedence goes to "visibility", with both process and file. */
975 if (error1 == ESRCH || error2 == ESRCH)
978 if (error1 == ENOENT || error2 == ENOENT)
981 /* Precedence goes to DAC/MAC protections. */
982 if (error1 == EACCES || error2 == EACCES)
985 /* Precedence goes to privilege. */
986 if (error1 == EPERM || error2 == EPERM)
989 /* Precedence goes to error over success; otherwise, arbitrary. */
996 mac_update_devfsdirent(struct devfs_dirent *de, struct vnode *vp)
999 MAC_PERFORM(update_devfsdirent, de, &de->de_label, vp, &vp->v_label);
1003 mac_update_procfsvnode(struct vnode *vp, struct ucred *cred)
1006 MAC_PERFORM(update_procfsvnode, vp, &vp->v_label, cred);
1010 * Support callout for policies that manage their own externalization
1011 * using extended attributes.
1014 mac_update_vnode_from_extattr(struct vnode *vp, struct mount *mp)
1018 MAC_CHECK(update_vnode_from_extattr, vp, &vp->v_label, mp,
1025 * Given an externalized mac label, internalize it and stamp it on a
1029 mac_update_vnode_from_externalized(struct vnode *vp, struct mac *extmac)
1033 MAC_CHECK(update_vnode_from_externalized, vp, &vp->v_label, extmac);
1039 * Call out to individual policies to update the label in a vnode from
1043 mac_update_vnode_from_mount(struct vnode *vp, struct mount *mp)
1046 MAC_PERFORM(update_vnode_from_mount, vp, &vp->v_label, mp,
1049 ASSERT_VOP_LOCKED(vp, "mac_update_vnode_from_mount");
1050 if (mac_cache_fslabel_in_vnode)
1051 vp->v_vflag |= VV_CACHEDLABEL;
1055 * Implementation of VOP_REFRESHLABEL() that relies on extended attributes
1056 * to store label data. Can be referenced by filesystems supporting
1057 * extended attributes.
1060 vop_stdrefreshlabel_ea(struct vop_refreshlabel_args *ap)
1062 struct vnode *vp = ap->a_vp;
1066 ASSERT_VOP_LOCKED(vp, "vop_stdrefreshlabel_ea");
1069 * Call out to external policies first. Order doesn't really
1070 * matter, as long as failure of one assures failure of all.
1072 error = mac_update_vnode_from_extattr(vp, vp->v_mount);
1076 buflen = sizeof(extmac);
1077 error = vn_extattr_get(vp, IO_NODELOCKED,
1078 FREEBSD_MAC_EXTATTR_NAMESPACE, FREEBSD_MAC_EXTATTR_NAME, &buflen,
1079 (char *)&extmac, curthread);
1087 * Use the label from the mount point.
1089 mac_update_vnode_from_mount(vp, vp->v_mount);
1094 /* Fail horribly. */
1098 if (buflen != sizeof(extmac))
1099 error = EPERM; /* Fail very closed. */
1101 error = mac_update_vnode_from_externalized(vp, &extmac);
1103 vp->v_vflag |= VV_CACHEDLABEL;
1107 printf("Corrupted label on %s",
1108 vp->v_mount->mnt_stat.f_mntonname);
1109 if (VOP_GETATTR(vp, &va, curthread->td_ucred, curthread) == 0)
1110 printf(" inum %ld", va.va_fileid);
1112 if (mac_debug_label_fallback) {
1113 printf(", falling back.\n");
1114 mac_update_vnode_from_mount(vp, vp->v_mount);
1129 * Make sure the vnode label is up-to-date. If EOPNOTSUPP, then we handle
1130 * the labeling activity outselves. Filesystems should be careful not
1131 * to change their minds regarding whether they support vop_refreshlabel()
1132 * for a vnode or not. Don't cache the vnode here, allow the file
1133 * system code to determine if it's safe to cache. If we update from
1134 * the mount, don't cache since a change to the mount label should affect
1138 vn_refreshlabel(struct vnode *vp, struct ucred *cred)
1142 ASSERT_VOP_LOCKED(vp, "vn_refreshlabel");
1144 if (vp->v_mount == NULL) {
1146 Eventually, we probably want to special-case refreshing
1147 of deadfs vnodes, and if there's a lock-free race somewhere,
1148 that case might be handled here.
1150 mac_update_vnode_deadfs(vp);
1153 /* printf("vn_refreshlabel: null v_mount\n"); */
1154 if (vp->v_type != VNON)
1156 "vn_refreshlabel: null v_mount with non-VNON\n");
1160 if (vp->v_vflag & VV_CACHEDLABEL) {
1161 mac_vnode_label_cache_hits++;
1164 mac_vnode_label_cache_misses++;
1166 if ((vp->v_mount->mnt_flag & MNT_MULTILABEL) == 0) {
1167 mac_update_vnode_from_mount(vp, vp->v_mount);
1171 error = VOP_REFRESHLABEL(vp, cred, curthread);
1175 * If labels are not supported on this vnode, fall back to
1176 * the label in the mount and propagate it to the vnode.
1177 * There should probably be some sort of policy/flag/decision
1180 mac_update_vnode_from_mount(vp, vp->v_mount);
1188 * Helper function for file systems using the vop_std*_ea() calls. This
1189 * function must be called after EA service is available for the vnode,
1190 * but before it's hooked up to the namespace so that the node persists
1191 * if there's a crash, or before it can be accessed. On successful
1192 * commit of the label to disk (etc), do cache the label.
1195 vop_stdcreatevnode_ea(struct vnode *dvp, struct vnode *tvp, struct ucred *cred)
1200 ASSERT_VOP_LOCKED(tvp, "vop_stdcreatevnode_ea");
1201 if ((dvp->v_mount->mnt_flag & MNT_MULTILABEL) == 0) {
1202 mac_update_vnode_from_mount(tvp, tvp->v_mount);
1204 error = vn_refreshlabel(dvp, cred);
1209 * Stick the label in the vnode. Then try to write to
1210 * disk. If we fail, return a failure to abort the
1211 * create operation. Really, this failure shouldn't
1212 * happen except in fairly unusual circumstances (out
1215 mac_create_vnode(cred, dvp, tvp);
1217 error = mac_stdcreatevnode_ea(tvp);
1222 * XXX: Eventually this will go away and all policies will
1223 * directly manage their extended attributes.
1225 error = mac_externalize(&tvp->v_label, &extmac);
1229 error = vn_extattr_set(tvp, IO_NODELOCKED,
1230 FREEBSD_MAC_EXTATTR_NAMESPACE, FREEBSD_MAC_EXTATTR_NAME,
1231 sizeof(extmac), (char *)&extmac, curthread);
1233 tvp->v_vflag |= VV_CACHEDLABEL;
1237 * In theory, we could have fall-back behavior here.
1238 * It would probably be incorrect.
1249 mac_execve_transition(struct ucred *old, struct ucred *new, struct vnode *vp)
1253 ASSERT_VOP_LOCKED(vp, "mac_execve_transition");
1255 error = vn_refreshlabel(vp, old);
1257 printf("mac_execve_transition: vn_refreshlabel returned %d\n",
1259 printf("mac_execve_transition: using old vnode label\n");
1262 MAC_PERFORM(execve_transition, old, new, vp, &vp->v_label);
1266 mac_execve_will_transition(struct ucred *old, struct vnode *vp)
1270 error = vn_refreshlabel(vp, old);
1275 MAC_BOOLEAN(execve_will_transition, ||, old, vp, &vp->v_label);
1281 mac_init_label(struct label *label)
1284 bzero(label, sizeof(*label));
1285 label->l_flags = MAC_FLAG_INITIALIZED;
1289 mac_init_structmac(struct mac *mac)
1292 bzero(mac, sizeof(*mac));
1293 mac->m_macflags = MAC_FLAG_INITIALIZED;
1297 mac_destroy_label(struct label *label)
1300 KASSERT(label->l_flags & MAC_FLAG_INITIALIZED,
1301 ("destroying uninitialized label"));
1303 bzero(label, sizeof(*label));
1304 /* implicit: label->l_flags &= ~MAC_FLAG_INITIALIZED; */
1308 mac_init_mbuf(struct mbuf *m, int how)
1310 KASSERT(m->m_flags & M_PKTHDR, ("mac_init_mbuf on non-header mbuf"));
1312 /* "how" is one of M_(TRY|DONT)WAIT */
1313 mac_init_label(&m->m_pkthdr.label);
1314 MAC_PERFORM(init_mbuf_label, &m->m_pkthdr.label, how);
1316 atomic_add_int(&nmacmbufs, 1);
1322 mac_destroy_mbuf(struct mbuf *m)
1325 MAC_PERFORM(destroy_mbuf_label, &m->m_pkthdr.label);
1326 mac_destroy_label(&m->m_pkthdr.label);
1328 atomic_subtract_int(&nmacmbufs, 1);
1333 mac_init_cred(struct ucred *cr)
1336 mac_init_label(&cr->cr_label);
1337 MAC_PERFORM(init_cred_label, &cr->cr_label);
1339 atomic_add_int(&nmaccreds, 1);
1344 mac_destroy_cred(struct ucred *cr)
1347 MAC_PERFORM(destroy_cred_label, &cr->cr_label);
1348 mac_destroy_label(&cr->cr_label);
1350 atomic_subtract_int(&nmaccreds, 1);
1355 mac_init_ifnet(struct ifnet *ifp)
1358 mac_init_label(&ifp->if_label);
1359 MAC_PERFORM(init_ifnet_label, &ifp->if_label);
1361 atomic_add_int(&nmacifnets, 1);
1366 mac_destroy_ifnet(struct ifnet *ifp)
1369 MAC_PERFORM(destroy_ifnet_label, &ifp->if_label);
1370 mac_destroy_label(&ifp->if_label);
1372 atomic_subtract_int(&nmacifnets, 1);
1377 mac_init_ipq(struct ipq *ipq)
1380 mac_init_label(&ipq->ipq_label);
1381 MAC_PERFORM(init_ipq_label, &ipq->ipq_label);
1383 atomic_add_int(&nmacipqs, 1);
1388 mac_destroy_ipq(struct ipq *ipq)
1391 MAC_PERFORM(destroy_ipq_label, &ipq->ipq_label);
1392 mac_destroy_label(&ipq->ipq_label);
1394 atomic_subtract_int(&nmacipqs, 1);
1399 mac_init_socket(struct socket *socket)
1402 mac_init_label(&socket->so_label);
1403 mac_init_label(&socket->so_peerlabel);
1404 MAC_PERFORM(init_socket_label, &socket->so_label);
1405 MAC_PERFORM(init_socket_peer_label, &socket->so_peerlabel);
1407 atomic_add_int(&nmacsockets, 1);
1412 mac_destroy_socket(struct socket *socket)
1415 MAC_PERFORM(destroy_socket_label, &socket->so_label);
1416 MAC_PERFORM(destroy_socket_peer_label, &socket->so_peerlabel);
1417 mac_destroy_label(&socket->so_label);
1418 mac_destroy_label(&socket->so_peerlabel);
1420 atomic_subtract_int(&nmacsockets, 1);
1425 mac_init_pipe(struct pipe *pipe)
1427 struct label *label;
1429 label = malloc(sizeof(struct label), M_MACPIPELABEL, M_ZERO|M_WAITOK);
1430 mac_init_label(label);
1431 pipe->pipe_label = label;
1432 pipe->pipe_peer->pipe_label = label;
1433 MAC_PERFORM(init_pipe_label, pipe->pipe_label);
1435 atomic_add_int(&nmacpipes, 1);
1440 mac_destroy_pipe(struct pipe *pipe)
1443 MAC_PERFORM(destroy_pipe_label, pipe->pipe_label);
1444 mac_destroy_label(pipe->pipe_label);
1445 free(pipe->pipe_label, M_MACPIPELABEL);
1447 atomic_subtract_int(&nmacpipes, 1);
1452 mac_init_bpfdesc(struct bpf_d *bpf_d)
1455 mac_init_label(&bpf_d->bd_label);
1456 MAC_PERFORM(init_bpfdesc_label, &bpf_d->bd_label);
1458 atomic_add_int(&nmacbpfdescs, 1);
1463 mac_destroy_bpfdesc(struct bpf_d *bpf_d)
1466 MAC_PERFORM(destroy_bpfdesc_label, &bpf_d->bd_label);
1467 mac_destroy_label(&bpf_d->bd_label);
1469 atomic_subtract_int(&nmacbpfdescs, 1);
1474 mac_init_mount(struct mount *mp)
1477 mac_init_label(&mp->mnt_mntlabel);
1478 mac_init_label(&mp->mnt_fslabel);
1479 MAC_PERFORM(init_mount_label, &mp->mnt_mntlabel);
1480 MAC_PERFORM(init_mount_fs_label, &mp->mnt_fslabel);
1482 atomic_add_int(&nmacmounts, 1);
1487 mac_destroy_mount(struct mount *mp)
1490 MAC_PERFORM(destroy_mount_label, &mp->mnt_mntlabel);
1491 MAC_PERFORM(destroy_mount_fs_label, &mp->mnt_fslabel);
1492 mac_destroy_label(&mp->mnt_fslabel);
1493 mac_destroy_label(&mp->mnt_mntlabel);
1495 atomic_subtract_int(&nmacmounts, 1);
1500 mac_init_temp(struct label *label)
1503 mac_init_label(label);
1504 MAC_PERFORM(init_temp_label, label);
1506 atomic_add_int(&nmactemp, 1);
1511 mac_destroy_temp(struct label *label)
1514 MAC_PERFORM(destroy_temp_label, label);
1515 mac_destroy_label(label);
1517 atomic_subtract_int(&nmactemp, 1);
1522 mac_init_vnode(struct vnode *vp)
1525 mac_init_label(&vp->v_label);
1526 MAC_PERFORM(init_vnode_label, &vp->v_label);
1528 atomic_add_int(&nmacvnodes, 1);
1533 mac_destroy_vnode(struct vnode *vp)
1536 MAC_PERFORM(destroy_vnode_label, &vp->v_label);
1537 mac_destroy_label(&vp->v_label);
1539 atomic_subtract_int(&nmacvnodes, 1);
1544 mac_init_devfsdirent(struct devfs_dirent *de)
1547 mac_init_label(&de->de_label);
1548 MAC_PERFORM(init_devfsdirent_label, &de->de_label);
1550 atomic_add_int(&nmacdevfsdirents, 1);
1555 mac_destroy_devfsdirent(struct devfs_dirent *de)
1558 MAC_PERFORM(destroy_devfsdirent_label, &de->de_label);
1559 mac_destroy_label(&de->de_label);
1561 atomic_subtract_int(&nmacdevfsdirents, 1);
1566 mac_externalize(struct label *label, struct mac *mac)
1570 mac_init_structmac(mac);
1571 MAC_CHECK(externalize, label, mac);
1577 mac_internalize(struct label *label, struct mac *mac)
1581 mac_init_temp(label);
1582 MAC_CHECK(internalize, label, mac);
1584 mac_destroy_temp(label);
1590 * Initialize MAC label for the first kernel process, from which other
1591 * kernel processes and threads are spawned.
1594 mac_create_proc0(struct ucred *cred)
1597 MAC_PERFORM(create_proc0, cred);
1601 * Initialize MAC label for the first userland process, from which other
1602 * userland processes and threads are spawned.
1605 mac_create_proc1(struct ucred *cred)
1608 MAC_PERFORM(create_proc1, cred);
1612 mac_thread_userret(struct thread *td)
1615 MAC_PERFORM(thread_userret, td);
1619 * When a new process is created, its label must be initialized. Generally,
1620 * this involves inheritence from the parent process, modulo possible
1621 * deltas. This function allows that processing to take place.
1624 mac_create_cred(struct ucred *parent_cred, struct ucred *child_cred)
1627 MAC_PERFORM(create_cred, parent_cred, child_cred);
1631 mac_check_vnode_access(struct ucred *cred, struct vnode *vp, int flags)
1635 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_access");
1637 if (!mac_enforce_fs)
1640 error = vn_refreshlabel(vp, cred);
1644 MAC_CHECK(check_vnode_access, cred, vp, &vp->v_label, flags);
1649 mac_check_vnode_chdir(struct ucred *cred, struct vnode *dvp)
1653 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_chdir");
1655 if (!mac_enforce_fs)
1658 error = vn_refreshlabel(dvp, cred);
1662 MAC_CHECK(check_vnode_chdir, cred, dvp, &dvp->v_label);
1667 mac_check_vnode_chroot(struct ucred *cred, struct vnode *dvp)
1671 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_chroot");
1673 if (!mac_enforce_fs)
1676 error = vn_refreshlabel(dvp, cred);
1680 MAC_CHECK(check_vnode_chroot, cred, dvp, &dvp->v_label);
1685 mac_check_vnode_create(struct ucred *cred, struct vnode *dvp,
1686 struct componentname *cnp, struct vattr *vap)
1690 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_create");
1692 if (!mac_enforce_fs)
1695 error = vn_refreshlabel(dvp, cred);
1699 MAC_CHECK(check_vnode_create, cred, dvp, &dvp->v_label, cnp, vap);
1704 mac_check_vnode_delete(struct ucred *cred, struct vnode *dvp, struct vnode *vp,
1705 struct componentname *cnp)
1709 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_delete");
1710 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_delete");
1712 if (!mac_enforce_fs)
1715 error = vn_refreshlabel(dvp, cred);
1718 error = vn_refreshlabel(vp, cred);
1722 MAC_CHECK(check_vnode_delete, cred, dvp, &dvp->v_label, vp,
1728 mac_check_vnode_deleteacl(struct ucred *cred, struct vnode *vp,
1733 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_deleteacl");
1735 if (!mac_enforce_fs)
1738 error = vn_refreshlabel(vp, cred);
1742 MAC_CHECK(check_vnode_deleteacl, cred, vp, &vp->v_label, type);
1747 mac_check_vnode_exec(struct ucred *cred, struct vnode *vp)
1751 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_exec");
1753 if (!mac_enforce_process && !mac_enforce_fs)
1756 error = vn_refreshlabel(vp, cred);
1759 MAC_CHECK(check_vnode_exec, cred, vp, &vp->v_label);
1765 mac_check_vnode_getacl(struct ucred *cred, struct vnode *vp, acl_type_t type)
1769 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_getacl");
1771 if (!mac_enforce_fs)
1774 error = vn_refreshlabel(vp, cred);
1778 MAC_CHECK(check_vnode_getacl, cred, vp, &vp->v_label, type);
1783 mac_check_vnode_getextattr(struct ucred *cred, struct vnode *vp,
1784 int attrnamespace, const char *name, struct uio *uio)
1788 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_getextattr");
1790 if (!mac_enforce_fs)
1793 error = vn_refreshlabel(vp, cred);
1797 MAC_CHECK(check_vnode_getextattr, cred, vp, &vp->v_label,
1798 attrnamespace, name, uio);
1803 mac_check_vnode_lookup(struct ucred *cred, struct vnode *dvp,
1804 struct componentname *cnp)
1808 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_lookup");
1810 if (!mac_enforce_fs)
1813 error = vn_refreshlabel(dvp, cred);
1817 MAC_CHECK(check_vnode_lookup, cred, dvp, &dvp->v_label, cnp);
1822 mac_check_vnode_mmap_prot(struct ucred *cred, struct vnode *vp, int newmapping)
1824 vm_prot_t result = VM_PROT_ALL;
1826 if (!mac_enforce_vm)
1830 * This should be some sort of MAC_BITWISE, maybe :)
1832 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mmap_perms");
1833 MAC_BOOLEAN(check_vnode_mmap_perms, &, cred, vp, &vp->v_label,
1839 mac_check_vnode_open(struct ucred *cred, struct vnode *vp, mode_t acc_mode)
1843 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_open");
1845 if (!mac_enforce_fs)
1848 error = vn_refreshlabel(vp, cred);
1852 MAC_CHECK(check_vnode_open, cred, vp, &vp->v_label, acc_mode);
1857 mac_check_vnode_poll(struct ucred *active_cred, struct ucred *file_cred,
1862 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_poll");
1864 if (!mac_enforce_fs)
1867 error = vn_refreshlabel(vp, active_cred);
1871 MAC_CHECK(check_vnode_poll, active_cred, file_cred, vp,
1878 mac_check_vnode_read(struct ucred *active_cred, struct ucred *file_cred,
1883 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_read");
1885 if (!mac_enforce_fs)
1888 error = vn_refreshlabel(vp, active_cred);
1892 MAC_CHECK(check_vnode_read, active_cred, file_cred, vp,
1899 mac_check_vnode_readdir(struct ucred *cred, struct vnode *dvp)
1903 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_readdir");
1905 if (!mac_enforce_fs)
1908 error = vn_refreshlabel(dvp, cred);
1912 MAC_CHECK(check_vnode_readdir, cred, dvp, &dvp->v_label);
1917 mac_check_vnode_readlink(struct ucred *cred, struct vnode *vp)
1921 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_readlink");
1923 if (!mac_enforce_fs)
1926 error = vn_refreshlabel(vp, cred);
1930 MAC_CHECK(check_vnode_readlink, cred, vp, &vp->v_label);
1935 mac_check_vnode_relabel(struct ucred *cred, struct vnode *vp,
1936 struct label *newlabel)
1940 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_relabel");
1942 error = vn_refreshlabel(vp, cred);
1946 MAC_CHECK(check_vnode_relabel, cred, vp, &vp->v_label, newlabel);
1952 mac_check_vnode_rename_from(struct ucred *cred, struct vnode *dvp,
1953 struct vnode *vp, struct componentname *cnp)
1957 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_rename_from");
1958 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_rename_from");
1960 if (!mac_enforce_fs)
1963 error = vn_refreshlabel(dvp, cred);
1966 error = vn_refreshlabel(vp, cred);
1970 MAC_CHECK(check_vnode_rename_from, cred, dvp, &dvp->v_label, vp,
1976 mac_check_vnode_rename_to(struct ucred *cred, struct vnode *dvp,
1977 struct vnode *vp, int samedir, struct componentname *cnp)
1981 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_rename_to");
1982 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_rename_to");
1984 if (!mac_enforce_fs)
1987 error = vn_refreshlabel(dvp, cred);
1991 error = vn_refreshlabel(vp, cred);
1995 MAC_CHECK(check_vnode_rename_to, cred, dvp, &dvp->v_label, vp,
1996 vp != NULL ? &vp->v_label : NULL, samedir, cnp);
2001 mac_check_vnode_revoke(struct ucred *cred, struct vnode *vp)
2005 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_revoke");
2007 if (!mac_enforce_fs)
2010 error = vn_refreshlabel(vp, cred);
2014 MAC_CHECK(check_vnode_revoke, cred, vp, &vp->v_label);
2019 mac_check_vnode_setacl(struct ucred *cred, struct vnode *vp, acl_type_t type,
2024 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setacl");
2026 if (!mac_enforce_fs)
2029 error = vn_refreshlabel(vp, cred);
2033 MAC_CHECK(check_vnode_setacl, cred, vp, &vp->v_label, type, acl);
2038 mac_check_vnode_setextattr(struct ucred *cred, struct vnode *vp,
2039 int attrnamespace, const char *name, struct uio *uio)
2043 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setextattr");
2045 if (!mac_enforce_fs)
2048 error = vn_refreshlabel(vp, cred);
2052 MAC_CHECK(check_vnode_setextattr, cred, vp, &vp->v_label,
2053 attrnamespace, name, uio);
2058 mac_check_vnode_setflags(struct ucred *cred, struct vnode *vp, u_long flags)
2062 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setflags");
2064 if (!mac_enforce_fs)
2067 error = vn_refreshlabel(vp, cred);
2071 MAC_CHECK(check_vnode_setflags, cred, vp, &vp->v_label, flags);
2076 mac_check_vnode_setmode(struct ucred *cred, struct vnode *vp, mode_t mode)
2080 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setmode");
2082 if (!mac_enforce_fs)
2085 error = vn_refreshlabel(vp, cred);
2089 MAC_CHECK(check_vnode_setmode, cred, vp, &vp->v_label, mode);
2094 mac_check_vnode_setowner(struct ucred *cred, struct vnode *vp, uid_t uid,
2099 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setowner");
2101 if (!mac_enforce_fs)
2104 error = vn_refreshlabel(vp, cred);
2108 MAC_CHECK(check_vnode_setowner, cred, vp, &vp->v_label, uid, gid);
2113 mac_check_vnode_setutimes(struct ucred *cred, struct vnode *vp,
2114 struct timespec atime, struct timespec mtime)
2118 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setutimes");
2120 if (!mac_enforce_fs)
2123 error = vn_refreshlabel(vp, cred);
2127 MAC_CHECK(check_vnode_setutimes, cred, vp, &vp->v_label, atime,
2133 mac_check_vnode_stat(struct ucred *active_cred, struct ucred *file_cred,
2138 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_stat");
2140 if (!mac_enforce_fs)
2143 error = vn_refreshlabel(vp, active_cred);
2147 MAC_CHECK(check_vnode_stat, active_cred, file_cred, vp,
2153 mac_check_vnode_write(struct ucred *active_cred, struct ucred *file_cred,
2158 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_write");
2160 if (!mac_enforce_fs)
2163 error = vn_refreshlabel(vp, active_cred);
2167 MAC_CHECK(check_vnode_write, active_cred, file_cred, vp,
2174 * When relabeling a process, call out to the policies for the maximum
2175 * permission allowed for each object type we know about in its
2176 * memory space, and revoke access (in the least surprising ways we
2177 * know) when necessary. The process lock is not held here.
2180 mac_cred_mmapped_drop_perms(struct thread *td, struct ucred *cred)
2183 /* XXX freeze all other threads */
2184 mac_cred_mmapped_drop_perms_recurse(td, cred,
2185 &td->td_proc->p_vmspace->vm_map);
2186 /* XXX allow other threads to continue */
2189 static __inline const char *
2190 prot2str(vm_prot_t prot)
2193 switch (prot & VM_PROT_ALL) {
2196 case VM_PROT_READ | VM_PROT_WRITE:
2198 case VM_PROT_READ | VM_PROT_EXECUTE:
2200 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
2204 case VM_PROT_EXECUTE:
2206 case VM_PROT_WRITE | VM_PROT_EXECUTE:
2214 mac_cred_mmapped_drop_perms_recurse(struct thread *td, struct ucred *cred,
2217 struct vm_map_entry *vme;
2218 vm_prot_t result, revokeperms;
2220 vm_ooffset_t offset;
2223 if (!mac_mmap_revocation)
2226 vm_map_lock_read(map);
2227 for (vme = map->header.next; vme != &map->header; vme = vme->next) {
2228 if (vme->eflags & MAP_ENTRY_IS_SUB_MAP) {
2229 mac_cred_mmapped_drop_perms_recurse(td, cred,
2230 vme->object.sub_map);
2234 * Skip over entries that obviously are not shared.
2236 if (vme->eflags & (MAP_ENTRY_COW | MAP_ENTRY_NOSYNC) ||
2237 !vme->max_protection)
2240 * Drill down to the deepest backing object.
2242 offset = vme->offset;
2243 object = vme->object.vm_object;
2246 while (object->backing_object != NULL) {
2247 object = object->backing_object;
2248 offset += object->backing_object_offset;
2251 * At the moment, vm_maps and objects aren't considered
2252 * by the MAC system, so only things with backing by a
2253 * normal object (read: vnodes) are checked.
2255 if (object->type != OBJT_VNODE)
2257 vp = (struct vnode *)object->handle;
2258 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2259 result = mac_check_vnode_mmap_prot(cred, vp, 0);
2260 VOP_UNLOCK(vp, 0, td);
2262 * Find out what maximum protection we may be allowing
2263 * now but a policy needs to get removed.
2265 revokeperms = vme->max_protection & ~result;
2268 printf("pid %ld: revoking %s perms from %#lx:%ld "
2269 "(max %s/cur %s)\n", (long)td->td_proc->p_pid,
2270 prot2str(revokeperms), (u_long)vme->start,
2271 (long)(vme->end - vme->start),
2272 prot2str(vme->max_protection), prot2str(vme->protection));
2273 vm_map_lock_upgrade(map);
2275 * This is the really simple case: if a map has more
2276 * max_protection than is allowed, but it's not being
2277 * actually used (that is, the current protection is
2278 * still allowed), we can just wipe it out and do
2281 if ((vme->protection & revokeperms) == 0) {
2282 vme->max_protection -= revokeperms;
2284 if (revokeperms & VM_PROT_WRITE) {
2286 * In the more complicated case, flush out all
2287 * pending changes to the object then turn it
2290 vm_object_reference(object);
2291 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2292 vm_object_page_clean(object,
2294 OFF_TO_IDX(offset + vme->end - vme->start +
2297 VOP_UNLOCK(vp, 0, td);
2298 vm_object_deallocate(object);
2300 * Why bother if there's no read permissions
2301 * anymore? For the rest, we need to leave
2302 * the write permissions on for COW, or
2303 * remove them entirely if configured to.
2305 if (!mac_mmap_revocation_via_cow) {
2306 vme->max_protection &= ~VM_PROT_WRITE;
2307 vme->protection &= ~VM_PROT_WRITE;
2308 } if ((revokeperms & VM_PROT_READ) == 0)
2309 vme->eflags |= MAP_ENTRY_COW |
2310 MAP_ENTRY_NEEDS_COPY;
2312 if (revokeperms & VM_PROT_EXECUTE) {
2313 vme->max_protection &= ~VM_PROT_EXECUTE;
2314 vme->protection &= ~VM_PROT_EXECUTE;
2316 if (revokeperms & VM_PROT_READ) {
2317 vme->max_protection = 0;
2318 vme->protection = 0;
2320 pmap_protect(map->pmap, vme->start, vme->end,
2321 vme->protection & ~revokeperms);
2322 vm_map_simplify_entry(map, vme);
2324 vm_map_lock_downgrade(map);
2326 vm_map_unlock_read(map);
2330 * When the subject's label changes, it may require revocation of privilege
2331 * to mapped objects. This can't be done on-the-fly later with a unified
2335 mac_relabel_cred(struct ucred *cred, struct label *newlabel)
2338 MAC_PERFORM(relabel_cred, cred, newlabel);
2342 mac_relabel_vnode(struct ucred *cred, struct vnode *vp, struct label *newlabel)
2345 MAC_PERFORM(relabel_vnode, cred, vp, &vp->v_label, newlabel);
2349 mac_create_ifnet(struct ifnet *ifnet)
2352 MAC_PERFORM(create_ifnet, ifnet, &ifnet->if_label);
2356 mac_create_bpfdesc(struct ucred *cred, struct bpf_d *bpf_d)
2359 MAC_PERFORM(create_bpfdesc, cred, bpf_d, &bpf_d->bd_label);
2363 mac_create_socket(struct ucred *cred, struct socket *socket)
2366 MAC_PERFORM(create_socket, cred, socket, &socket->so_label);
2370 mac_create_pipe(struct ucred *cred, struct pipe *pipe)
2373 MAC_PERFORM(create_pipe, cred, pipe, pipe->pipe_label);
2377 mac_create_socket_from_socket(struct socket *oldsocket,
2378 struct socket *newsocket)
2381 MAC_PERFORM(create_socket_from_socket, oldsocket, &oldsocket->so_label,
2382 newsocket, &newsocket->so_label);
2386 mac_relabel_socket(struct ucred *cred, struct socket *socket,
2387 struct label *newlabel)
2390 MAC_PERFORM(relabel_socket, cred, socket, &socket->so_label, newlabel);
2394 mac_relabel_pipe(struct ucred *cred, struct pipe *pipe, struct label *newlabel)
2397 MAC_PERFORM(relabel_pipe, cred, pipe, pipe->pipe_label, newlabel);
2401 mac_set_socket_peer_from_mbuf(struct mbuf *mbuf, struct socket *socket)
2404 MAC_PERFORM(set_socket_peer_from_mbuf, mbuf, &mbuf->m_pkthdr.label,
2405 socket, &socket->so_peerlabel);
2409 mac_set_socket_peer_from_socket(struct socket *oldsocket,
2410 struct socket *newsocket)
2413 MAC_PERFORM(set_socket_peer_from_socket, oldsocket,
2414 &oldsocket->so_label, newsocket, &newsocket->so_peerlabel);
2418 mac_create_datagram_from_ipq(struct ipq *ipq, struct mbuf *datagram)
2421 MAC_PERFORM(create_datagram_from_ipq, ipq, &ipq->ipq_label,
2422 datagram, &datagram->m_pkthdr.label);
2426 mac_create_fragment(struct mbuf *datagram, struct mbuf *fragment)
2429 MAC_PERFORM(create_fragment, datagram, &datagram->m_pkthdr.label,
2430 fragment, &fragment->m_pkthdr.label);
2434 mac_create_ipq(struct mbuf *fragment, struct ipq *ipq)
2437 MAC_PERFORM(create_ipq, fragment, &fragment->m_pkthdr.label, ipq,
2442 mac_create_mbuf_from_mbuf(struct mbuf *oldmbuf, struct mbuf *newmbuf)
2445 MAC_PERFORM(create_mbuf_from_mbuf, oldmbuf, &oldmbuf->m_pkthdr.label,
2446 newmbuf, &newmbuf->m_pkthdr.label);
2450 mac_create_mbuf_from_bpfdesc(struct bpf_d *bpf_d, struct mbuf *mbuf)
2453 MAC_PERFORM(create_mbuf_from_bpfdesc, bpf_d, &bpf_d->bd_label, mbuf,
2454 &mbuf->m_pkthdr.label);
2458 mac_create_mbuf_linklayer(struct ifnet *ifnet, struct mbuf *mbuf)
2461 MAC_PERFORM(create_mbuf_linklayer, ifnet, &ifnet->if_label, mbuf,
2462 &mbuf->m_pkthdr.label);
2466 mac_create_mbuf_from_ifnet(struct ifnet *ifnet, struct mbuf *mbuf)
2469 MAC_PERFORM(create_mbuf_from_ifnet, ifnet, &ifnet->if_label, mbuf,
2470 &mbuf->m_pkthdr.label);
2474 mac_create_mbuf_multicast_encap(struct mbuf *oldmbuf, struct ifnet *ifnet,
2475 struct mbuf *newmbuf)
2478 MAC_PERFORM(create_mbuf_multicast_encap, oldmbuf,
2479 &oldmbuf->m_pkthdr.label, ifnet, &ifnet->if_label, newmbuf,
2480 &newmbuf->m_pkthdr.label);
2484 mac_create_mbuf_netlayer(struct mbuf *oldmbuf, struct mbuf *newmbuf)
2487 MAC_PERFORM(create_mbuf_netlayer, oldmbuf, &oldmbuf->m_pkthdr.label,
2488 newmbuf, &newmbuf->m_pkthdr.label);
2492 mac_fragment_match(struct mbuf *fragment, struct ipq *ipq)
2497 MAC_BOOLEAN(fragment_match, &&, fragment, &fragment->m_pkthdr.label,
2498 ipq, &ipq->ipq_label);
2504 mac_update_ipq(struct mbuf *fragment, struct ipq *ipq)
2507 MAC_PERFORM(update_ipq, fragment, &fragment->m_pkthdr.label, ipq,
2512 mac_create_mbuf_from_socket(struct socket *socket, struct mbuf *mbuf)
2515 MAC_PERFORM(create_mbuf_from_socket, socket, &socket->so_label, mbuf,
2516 &mbuf->m_pkthdr.label);
2520 mac_create_mount(struct ucred *cred, struct mount *mp)
2523 MAC_PERFORM(create_mount, cred, mp, &mp->mnt_mntlabel,
2528 mac_create_root_mount(struct ucred *cred, struct mount *mp)
2531 MAC_PERFORM(create_root_mount, cred, mp, &mp->mnt_mntlabel,
2536 mac_check_bpfdesc_receive(struct bpf_d *bpf_d, struct ifnet *ifnet)
2540 if (!mac_enforce_network)
2543 MAC_CHECK(check_bpfdesc_receive, bpf_d, &bpf_d->bd_label, ifnet,
2550 mac_check_cred_relabel(struct ucred *cred, struct label *newlabel)
2554 MAC_CHECK(check_cred_relabel, cred, newlabel);
2560 mac_check_cred_visible(struct ucred *u1, struct ucred *u2)
2564 if (!mac_enforce_process)
2567 MAC_CHECK(check_cred_visible, u1, u2);
2573 mac_check_ifnet_transmit(struct ifnet *ifnet, struct mbuf *mbuf)
2577 if (!mac_enforce_network)
2580 KASSERT(mbuf->m_flags & M_PKTHDR, ("packet has no pkthdr"));
2581 if (!(mbuf->m_pkthdr.label.l_flags & MAC_FLAG_INITIALIZED))
2582 printf("%s%d: not initialized\n", ifnet->if_name,
2585 MAC_CHECK(check_ifnet_transmit, ifnet, &ifnet->if_label, mbuf,
2586 &mbuf->m_pkthdr.label);
2592 mac_check_mount_stat(struct ucred *cred, struct mount *mount)
2596 if (!mac_enforce_fs)
2599 MAC_CHECK(check_mount_stat, cred, mount, &mount->mnt_mntlabel);
2605 mac_check_pipe_ioctl(struct ucred *cred, struct pipe *pipe, unsigned long cmd,
2610 PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2612 if (!mac_enforce_pipe)
2615 MAC_CHECK(check_pipe_ioctl, cred, pipe, pipe->pipe_label, cmd, data);
2621 mac_check_pipe_poll(struct ucred *cred, struct pipe *pipe)
2625 PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2627 if (!mac_enforce_pipe)
2630 MAC_CHECK(check_pipe_poll, cred, pipe, pipe->pipe_label);
2636 mac_check_pipe_read(struct ucred *cred, struct pipe *pipe)
2640 PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2642 if (!mac_enforce_pipe)
2645 MAC_CHECK(check_pipe_read, cred, pipe, pipe->pipe_label);
2651 mac_check_pipe_relabel(struct ucred *cred, struct pipe *pipe,
2652 struct label *newlabel)
2656 PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2658 if (!mac_enforce_pipe)
2661 MAC_CHECK(check_pipe_relabel, cred, pipe, pipe->pipe_label, newlabel);
2667 mac_check_pipe_stat(struct ucred *cred, struct pipe *pipe)
2671 PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2673 if (!mac_enforce_pipe)
2676 MAC_CHECK(check_pipe_stat, cred, pipe, pipe->pipe_label);
2682 mac_check_pipe_write(struct ucred *cred, struct pipe *pipe)
2686 PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2688 if (!mac_enforce_pipe)
2691 MAC_CHECK(check_pipe_write, cred, pipe, pipe->pipe_label);
2697 mac_check_proc_debug(struct ucred *cred, struct proc *proc)
2701 PROC_LOCK_ASSERT(proc, MA_OWNED);
2703 if (!mac_enforce_process)
2706 MAC_CHECK(check_proc_debug, cred, proc);
2712 mac_check_proc_sched(struct ucred *cred, struct proc *proc)
2716 PROC_LOCK_ASSERT(proc, MA_OWNED);
2718 if (!mac_enforce_process)
2721 MAC_CHECK(check_proc_sched, cred, proc);
2727 mac_check_proc_signal(struct ucred *cred, struct proc *proc, int signum)
2731 PROC_LOCK_ASSERT(proc, MA_OWNED);
2733 if (!mac_enforce_process)
2736 MAC_CHECK(check_proc_signal, cred, proc, signum);
2742 mac_check_socket_bind(struct ucred *ucred, struct socket *socket,
2743 struct sockaddr *sockaddr)
2747 if (!mac_enforce_socket)
2750 MAC_CHECK(check_socket_bind, ucred, socket, &socket->so_label,
2757 mac_check_socket_connect(struct ucred *cred, struct socket *socket,
2758 struct sockaddr *sockaddr)
2762 if (!mac_enforce_socket)
2765 MAC_CHECK(check_socket_connect, cred, socket, &socket->so_label,
2772 mac_check_socket_deliver(struct socket *socket, struct mbuf *mbuf)
2776 if (!mac_enforce_socket)
2779 MAC_CHECK(check_socket_deliver, socket, &socket->so_label, mbuf,
2780 &mbuf->m_pkthdr.label);
2786 mac_check_socket_listen(struct ucred *cred, struct socket *socket)
2790 if (!mac_enforce_socket)
2793 MAC_CHECK(check_socket_listen, cred, socket, &socket->so_label);
2798 mac_check_socket_relabel(struct ucred *cred, struct socket *socket,
2799 struct label *newlabel)
2803 MAC_CHECK(check_socket_relabel, cred, socket, &socket->so_label,
2810 mac_check_socket_visible(struct ucred *cred, struct socket *socket)
2814 if (!mac_enforce_socket)
2817 MAC_CHECK(check_socket_visible, cred, socket, &socket->so_label);
2823 mac_ioctl_ifnet_get(struct ucred *cred, struct ifreq *ifr,
2824 struct ifnet *ifnet)
2829 error = mac_externalize(&ifnet->if_label, &label);
2833 return (copyout(&label, ifr->ifr_ifru.ifru_data, sizeof(label)));
2837 mac_ioctl_ifnet_set(struct ucred *cred, struct ifreq *ifr,
2838 struct ifnet *ifnet)
2840 struct mac newlabel;
2841 struct label intlabel;
2844 error = copyin(ifr->ifr_ifru.ifru_data, &newlabel, sizeof(newlabel));
2848 error = mac_internalize(&intlabel, &newlabel);
2853 * XXX: Note that this is a redundant privilege check, since
2854 * policies impose this check themselves if required by the
2855 * policy. Eventually, this should go away.
2857 error = suser_cred(cred, 0);
2861 MAC_CHECK(check_ifnet_relabel, cred, ifnet, &ifnet->if_label,
2866 MAC_PERFORM(relabel_ifnet, cred, ifnet, &ifnet->if_label, &intlabel);
2869 mac_destroy_temp(&intlabel);
2874 mac_create_devfs_vnode(struct devfs_dirent *de, struct vnode *vp)
2877 MAC_PERFORM(create_devfs_vnode, de, &de->de_label, vp, &vp->v_label);
2881 mac_create_devfs_device(dev_t dev, struct devfs_dirent *de)
2884 MAC_PERFORM(create_devfs_device, dev, de, &de->de_label);
2888 mac_stdcreatevnode_ea(struct vnode *vp)
2892 MAC_CHECK(stdcreatevnode_ea, vp, &vp->v_label);
2898 mac_create_devfs_directory(char *dirname, int dirnamelen,
2899 struct devfs_dirent *de)
2902 MAC_PERFORM(create_devfs_directory, dirname, dirnamelen, de,
2907 * When a new vnode is created, this call will initialize its label.
2910 mac_create_vnode(struct ucred *cred, struct vnode *parent,
2911 struct vnode *child)
2915 ASSERT_VOP_LOCKED(parent, "mac_create_vnode");
2916 ASSERT_VOP_LOCKED(child, "mac_create_vnode");
2918 error = vn_refreshlabel(parent, cred);
2920 printf("mac_create_vnode: vn_refreshlabel returned %d\n",
2922 printf("mac_create_vnode: using old vnode label\n");
2925 MAC_PERFORM(create_vnode, cred, parent, &parent->v_label, child,
2930 mac_setsockopt_label_set(struct ucred *cred, struct socket *so,
2933 struct label intlabel;
2936 error = mac_internalize(&intlabel, extmac);
2940 mac_check_socket_relabel(cred, so, &intlabel);
2942 mac_destroy_temp(&intlabel);
2946 mac_relabel_socket(cred, so, &intlabel);
2948 mac_destroy_temp(&intlabel);
2953 mac_pipe_label_set(struct ucred *cred, struct pipe *pipe, struct label *label)
2957 PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2959 error = mac_check_pipe_relabel(cred, pipe, label);
2963 mac_relabel_pipe(cred, pipe, label);
2969 mac_getsockopt_label_get(struct ucred *cred, struct socket *so,
2973 return (mac_externalize(&so->so_label, extmac));
2977 mac_getsockopt_peerlabel_get(struct ucred *cred, struct socket *so,
2981 return (mac_externalize(&so->so_peerlabel, extmac));
2985 * Implementation of VOP_SETLABEL() that relies on extended attributes
2986 * to store label data. Can be referenced by filesystems supporting
2987 * extended attributes.
2990 vop_stdsetlabel_ea(struct vop_setlabel_args *ap)
2992 struct vnode *vp = ap->a_vp;
2993 struct label *intlabel = ap->a_label;
2997 ASSERT_VOP_LOCKED(vp, "vop_stdsetlabel_ea");
3000 * XXX: Eventually call out to EA check/set calls here.
3001 * Be particularly careful to avoid race conditions,
3002 * consistency problems, and stability problems when
3003 * dealing with multiple EAs. In particular, we require
3004 * the ability to write multiple EAs on the same file in
3005 * a single transaction, which the current EA interface
3009 error = mac_externalize(intlabel, &extmac);
3013 error = vn_extattr_set(vp, IO_NODELOCKED,
3014 FREEBSD_MAC_EXTATTR_NAMESPACE, FREEBSD_MAC_EXTATTR_NAME,
3015 sizeof(extmac), (char *)&extmac, curthread);
3019 mac_relabel_vnode(ap->a_cred, vp, intlabel);
3021 vp->v_vflag |= VV_CACHEDLABEL;
3027 vn_setlabel(struct vnode *vp, struct label *intlabel, struct ucred *cred)
3031 if (vp->v_mount == NULL) {
3032 /* printf("vn_setlabel: null v_mount\n"); */
3033 if (vp->v_type != VNON)
3034 printf("vn_setlabel: null v_mount with non-VNON\n");
3038 if ((vp->v_mount->mnt_flag & MNT_MULTILABEL) == 0)
3039 return (EOPNOTSUPP);
3042 * Multi-phase commit. First check the policies to confirm the
3043 * change is OK. Then commit via the filesystem. Finally,
3044 * update the actual vnode label. Question: maybe the filesystem
3045 * should update the vnode at the end as part of VOP_SETLABEL()?
3047 error = mac_check_vnode_relabel(cred, vp, intlabel);
3052 * VADMIN provides the opportunity for the filesystem to make
3053 * decisions about who is and is not able to modify labels
3054 * and protections on files. This might not be right. We can't
3055 * assume VOP_SETLABEL() will do it, because we might implement
3056 * that as part of vop_stdsetlabel_ea().
3058 error = VOP_ACCESS(vp, VADMIN, cred, curthread);
3062 error = VOP_SETLABEL(vp, intlabel, cred, curthread);
3073 __mac_get_proc(struct thread *td, struct __mac_get_proc_args *uap)
3078 error = mac_externalize(&td->td_ucred->cr_label, &extmac);
3080 error = copyout(&extmac, SCARG(uap, mac_p), sizeof(extmac));
3089 __mac_set_proc(struct thread *td, struct __mac_set_proc_args *uap)
3091 struct ucred *newcred, *oldcred;
3094 struct label intlabel;
3097 error = copyin(SCARG(uap, mac_p), &extmac, sizeof(extmac));
3101 error = mac_internalize(&intlabel, &extmac);
3109 oldcred = p->p_ucred;
3111 error = mac_check_cred_relabel(oldcred, &intlabel);
3114 mac_destroy_temp(&intlabel);
3120 crcopy(newcred, oldcred);
3121 mac_relabel_cred(newcred, &intlabel);
3122 p->p_ucred = newcred;
3125 * Grab additional reference for use while revoking mmaps, prior
3126 * to releasing the proc lock and sharing the cred.
3132 mac_cred_mmapped_drop_perms(td, newcred);
3135 crfree(newcred); /* Free revocation reference. */
3137 mac_destroy_temp(&intlabel);
3145 __mac_get_fd(struct thread *td, struct __mac_get_fd_args *uap)
3155 error = fget(td, SCARG(uap, fd), &fp);
3159 switch (fp->f_type) {
3162 vp = (struct vnode *)fp->f_data;
3164 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3165 error = vn_refreshlabel(vp, td->td_ucred);
3167 error = mac_externalize(&vp->v_label, &extmac);
3168 VOP_UNLOCK(vp, 0, td);
3171 pipe = (struct pipe *)fp->f_data;
3172 error = mac_externalize(pipe->pipe_label, &extmac);
3179 error = copyout(&extmac, SCARG(uap, mac_p), sizeof(extmac));
3192 __mac_get_file(struct thread *td, struct __mac_get_file_args *uap)
3194 struct nameidata nd;
3199 NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_USERSPACE,
3200 SCARG(uap, path_p), td);
3205 error = vn_refreshlabel(nd.ni_vp, td->td_ucred);
3207 error = mac_externalize(&nd.ni_vp->v_label, &extmac);
3212 error = copyout(&extmac, SCARG(uap, mac_p), sizeof(extmac));
3223 __mac_set_fd(struct thread *td, struct __mac_set_fd_args *uap)
3227 struct label intlabel;
3234 error = fget(td, SCARG(uap, fd), &fp);
3238 error = copyin(SCARG(uap, mac_p), &extmac, sizeof(extmac));
3242 error = mac_internalize(&intlabel, &extmac);
3246 switch (fp->f_type) {
3249 vp = (struct vnode *)fp->f_data;
3250 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3254 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3255 error = vn_setlabel(vp, &intlabel, td->td_ucred);
3256 VOP_UNLOCK(vp, 0, td);
3257 vn_finished_write(mp);
3258 mac_destroy_temp(&intlabel);
3261 pipe = (struct pipe *)fp->f_data;
3263 error = mac_pipe_label_set(td->td_ucred, pipe, &intlabel);
3281 __mac_set_file(struct thread *td, struct __mac_set_file_args *uap)
3283 struct nameidata nd;
3285 struct label intlabel;
3291 error = copyin(SCARG(uap, mac_p), &extmac, sizeof(extmac));
3295 error = mac_internalize(&intlabel, &extmac);
3299 NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_USERSPACE,
3300 SCARG(uap, path_p), td);
3304 error = vn_start_write(nd.ni_vp, &mp, V_WAIT | PCATCH);
3308 error = vn_setlabel(nd.ni_vp, &intlabel, td->td_ucred);
3310 vn_finished_write(mp);
3312 mac_destroy_temp(&intlabel);
3320 mac_syscall(struct thread *td, struct mac_syscall_args *uap)
3322 struct mac_policy_conf *mpc;
3323 char target[MAC_MAX_POLICY_NAME];
3326 error = copyinstr(SCARG(uap, policy), target, sizeof(target), NULL);
3331 MAC_POLICY_LIST_BUSY();
3332 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {
3333 if (strcmp(mpc->mpc_name, target) == 0 &&
3334 mpc->mpc_ops->mpo_syscall != NULL) {
3335 error = mpc->mpc_ops->mpo_syscall(td,
3336 SCARG(uap, call), SCARG(uap, arg));
3342 MAC_POLICY_LIST_UNBUSY();
3346 SYSINIT(mac, SI_SUB_MAC, SI_ORDER_FIRST, mac_init, NULL);
3347 SYSINIT(mac_late, SI_SUB_MAC_LATE, SI_ORDER_FIRST, mac_late_init, NULL);
3352 __mac_get_proc(struct thread *td, struct __mac_get_proc_args *uap)
3359 __mac_set_proc(struct thread *td, struct __mac_set_proc_args *uap)
3366 __mac_get_fd(struct thread *td, struct __mac_get_fd_args *uap)
3373 __mac_get_file(struct thread *td, struct __mac_get_file_args *uap)
3380 __mac_set_fd(struct thread *td, struct __mac_set_fd_args *uap)
3387 __mac_set_file(struct thread *td, struct __mac_set_file_args *uap)
3394 mac_syscall(struct thread *td, struct mac_syscall_args *uap)