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 mac);
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(struct label *label, struct mac *mac);
213 static int mac_policy_register(struct mac_policy_conf *mpc);
214 static int mac_policy_unregister(struct mac_policy_conf *mpc);
216 static int mac_stdcreatevnode_ea(struct vnode *vp);
217 static void mac_check_vnode_mmap_downgrade(struct ucred *cred,
218 struct vnode *vp, int *prot);
219 static void mac_cred_mmapped_drop_perms_recurse(struct thread *td,
220 struct ucred *cred, struct vm_map *map);
222 static void mac_destroy_socket_label(struct label *label);
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_SYMLINK:
523 mpc->mpc_ops->mpo_create_devfs_symlink =
526 case MAC_CREATE_DEVFS_VNODE:
527 mpc->mpc_ops->mpo_create_devfs_vnode =
530 case MAC_STDCREATEVNODE_EA:
531 mpc->mpc_ops->mpo_stdcreatevnode_ea =
534 case MAC_CREATE_VNODE:
535 mpc->mpc_ops->mpo_create_vnode =
538 case MAC_CREATE_MOUNT:
539 mpc->mpc_ops->mpo_create_mount =
542 case MAC_CREATE_ROOT_MOUNT:
543 mpc->mpc_ops->mpo_create_root_mount =
546 case MAC_RELABEL_VNODE:
547 mpc->mpc_ops->mpo_relabel_vnode =
550 case MAC_UPDATE_DEVFSDIRENT:
551 mpc->mpc_ops->mpo_update_devfsdirent =
554 case MAC_UPDATE_PROCFSVNODE:
555 mpc->mpc_ops->mpo_update_procfsvnode =
558 case MAC_UPDATE_VNODE_FROM_EXTATTR:
559 mpc->mpc_ops->mpo_update_vnode_from_extattr =
562 case MAC_UPDATE_VNODE_FROM_EXTERNALIZED:
563 mpc->mpc_ops->mpo_update_vnode_from_externalized =
566 case MAC_UPDATE_VNODE_FROM_MOUNT:
567 mpc->mpc_ops->mpo_update_vnode_from_mount =
570 case MAC_CREATE_MBUF_FROM_SOCKET:
571 mpc->mpc_ops->mpo_create_mbuf_from_socket =
574 case MAC_CREATE_PIPE:
575 mpc->mpc_ops->mpo_create_pipe =
578 case MAC_CREATE_SOCKET:
579 mpc->mpc_ops->mpo_create_socket =
582 case MAC_CREATE_SOCKET_FROM_SOCKET:
583 mpc->mpc_ops->mpo_create_socket_from_socket =
586 case MAC_RELABEL_PIPE:
587 mpc->mpc_ops->mpo_relabel_pipe =
590 case MAC_RELABEL_SOCKET:
591 mpc->mpc_ops->mpo_relabel_socket =
594 case MAC_SET_SOCKET_PEER_FROM_MBUF:
595 mpc->mpc_ops->mpo_set_socket_peer_from_mbuf =
598 case MAC_SET_SOCKET_PEER_FROM_SOCKET:
599 mpc->mpc_ops->mpo_set_socket_peer_from_socket =
602 case MAC_CREATE_BPFDESC:
603 mpc->mpc_ops->mpo_create_bpfdesc =
606 case MAC_CREATE_DATAGRAM_FROM_IPQ:
607 mpc->mpc_ops->mpo_create_datagram_from_ipq =
610 case MAC_CREATE_FRAGMENT:
611 mpc->mpc_ops->mpo_create_fragment =
614 case MAC_CREATE_IFNET:
615 mpc->mpc_ops->mpo_create_ifnet =
619 mpc->mpc_ops->mpo_create_ipq =
622 case MAC_CREATE_MBUF_FROM_MBUF:
623 mpc->mpc_ops->mpo_create_mbuf_from_mbuf =
626 case MAC_CREATE_MBUF_LINKLAYER:
627 mpc->mpc_ops->mpo_create_mbuf_linklayer =
630 case MAC_CREATE_MBUF_FROM_BPFDESC:
631 mpc->mpc_ops->mpo_create_mbuf_from_bpfdesc =
634 case MAC_CREATE_MBUF_FROM_IFNET:
635 mpc->mpc_ops->mpo_create_mbuf_from_ifnet =
638 case MAC_CREATE_MBUF_MULTICAST_ENCAP:
639 mpc->mpc_ops->mpo_create_mbuf_multicast_encap =
642 case MAC_CREATE_MBUF_NETLAYER:
643 mpc->mpc_ops->mpo_create_mbuf_netlayer =
646 case MAC_FRAGMENT_MATCH:
647 mpc->mpc_ops->mpo_fragment_match =
650 case MAC_RELABEL_IFNET:
651 mpc->mpc_ops->mpo_relabel_ifnet =
655 mpc->mpc_ops->mpo_update_ipq =
658 case MAC_CREATE_CRED:
659 mpc->mpc_ops->mpo_create_cred =
662 case MAC_EXECVE_TRANSITION:
663 mpc->mpc_ops->mpo_execve_transition =
666 case MAC_EXECVE_WILL_TRANSITION:
667 mpc->mpc_ops->mpo_execve_will_transition =
670 case MAC_CREATE_PROC0:
671 mpc->mpc_ops->mpo_create_proc0 =
674 case MAC_CREATE_PROC1:
675 mpc->mpc_ops->mpo_create_proc1 =
678 case MAC_RELABEL_CRED:
679 mpc->mpc_ops->mpo_relabel_cred =
682 case MAC_THREAD_USERRET:
683 mpc->mpc_ops->mpo_thread_userret =
686 case MAC_CHECK_BPFDESC_RECEIVE:
687 mpc->mpc_ops->mpo_check_bpfdesc_receive =
690 case MAC_CHECK_CRED_RELABEL:
691 mpc->mpc_ops->mpo_check_cred_relabel =
694 case MAC_CHECK_CRED_VISIBLE:
695 mpc->mpc_ops->mpo_check_cred_visible =
698 case MAC_CHECK_IFNET_RELABEL:
699 mpc->mpc_ops->mpo_check_ifnet_relabel =
702 case MAC_CHECK_IFNET_TRANSMIT:
703 mpc->mpc_ops->mpo_check_ifnet_transmit =
706 case MAC_CHECK_MOUNT_STAT:
707 mpc->mpc_ops->mpo_check_mount_stat =
710 case MAC_CHECK_PIPE_IOCTL:
711 mpc->mpc_ops->mpo_check_pipe_ioctl =
714 case MAC_CHECK_PIPE_POLL:
715 mpc->mpc_ops->mpo_check_pipe_poll =
718 case MAC_CHECK_PIPE_READ:
719 mpc->mpc_ops->mpo_check_pipe_read =
722 case MAC_CHECK_PIPE_RELABEL:
723 mpc->mpc_ops->mpo_check_pipe_relabel =
726 case MAC_CHECK_PIPE_STAT:
727 mpc->mpc_ops->mpo_check_pipe_stat =
730 case MAC_CHECK_PIPE_WRITE:
731 mpc->mpc_ops->mpo_check_pipe_write =
734 case MAC_CHECK_PROC_DEBUG:
735 mpc->mpc_ops->mpo_check_proc_debug =
738 case MAC_CHECK_PROC_SCHED:
739 mpc->mpc_ops->mpo_check_proc_sched =
742 case MAC_CHECK_PROC_SIGNAL:
743 mpc->mpc_ops->mpo_check_proc_signal =
746 case MAC_CHECK_SOCKET_BIND:
747 mpc->mpc_ops->mpo_check_socket_bind =
750 case MAC_CHECK_SOCKET_CONNECT:
751 mpc->mpc_ops->mpo_check_socket_connect =
754 case MAC_CHECK_SOCKET_DELIVER:
755 mpc->mpc_ops->mpo_check_socket_deliver =
758 case MAC_CHECK_SOCKET_LISTEN:
759 mpc->mpc_ops->mpo_check_socket_listen =
762 case MAC_CHECK_SOCKET_RECEIVE:
763 mpc->mpc_ops->mpo_check_socket_receive =
766 case MAC_CHECK_SOCKET_RELABEL:
767 mpc->mpc_ops->mpo_check_socket_relabel =
770 case MAC_CHECK_SOCKET_SEND:
771 mpc->mpc_ops->mpo_check_socket_send =
774 case MAC_CHECK_SOCKET_VISIBLE:
775 mpc->mpc_ops->mpo_check_socket_visible =
778 case MAC_CHECK_VNODE_ACCESS:
779 mpc->mpc_ops->mpo_check_vnode_access =
782 case MAC_CHECK_VNODE_CHDIR:
783 mpc->mpc_ops->mpo_check_vnode_chdir =
786 case MAC_CHECK_VNODE_CHROOT:
787 mpc->mpc_ops->mpo_check_vnode_chroot =
790 case MAC_CHECK_VNODE_CREATE:
791 mpc->mpc_ops->mpo_check_vnode_create =
794 case MAC_CHECK_VNODE_DELETE:
795 mpc->mpc_ops->mpo_check_vnode_delete =
798 case MAC_CHECK_VNODE_DELETEACL:
799 mpc->mpc_ops->mpo_check_vnode_deleteacl =
802 case MAC_CHECK_VNODE_EXEC:
803 mpc->mpc_ops->mpo_check_vnode_exec =
806 case MAC_CHECK_VNODE_GETACL:
807 mpc->mpc_ops->mpo_check_vnode_getacl =
810 case MAC_CHECK_VNODE_GETEXTATTR:
811 mpc->mpc_ops->mpo_check_vnode_getextattr =
814 case MAC_CHECK_VNODE_LINK:
815 mpc->mpc_ops->mpo_check_vnode_link =
818 case MAC_CHECK_VNODE_LOOKUP:
819 mpc->mpc_ops->mpo_check_vnode_lookup =
822 case MAC_CHECK_VNODE_MMAP:
823 mpc->mpc_ops->mpo_check_vnode_mmap =
826 case MAC_CHECK_VNODE_MMAP_DOWNGRADE:
827 mpc->mpc_ops->mpo_check_vnode_mmap_downgrade =
830 case MAC_CHECK_VNODE_MPROTECT:
831 mpc->mpc_ops->mpo_check_vnode_mprotect =
834 case MAC_CHECK_VNODE_OPEN:
835 mpc->mpc_ops->mpo_check_vnode_open =
838 case MAC_CHECK_VNODE_POLL:
839 mpc->mpc_ops->mpo_check_vnode_poll =
842 case MAC_CHECK_VNODE_READ:
843 mpc->mpc_ops->mpo_check_vnode_read =
846 case MAC_CHECK_VNODE_READDIR:
847 mpc->mpc_ops->mpo_check_vnode_readdir =
850 case MAC_CHECK_VNODE_READLINK:
851 mpc->mpc_ops->mpo_check_vnode_readlink =
854 case MAC_CHECK_VNODE_RELABEL:
855 mpc->mpc_ops->mpo_check_vnode_relabel =
858 case MAC_CHECK_VNODE_RENAME_FROM:
859 mpc->mpc_ops->mpo_check_vnode_rename_from =
862 case MAC_CHECK_VNODE_RENAME_TO:
863 mpc->mpc_ops->mpo_check_vnode_rename_to =
866 case MAC_CHECK_VNODE_REVOKE:
867 mpc->mpc_ops->mpo_check_vnode_revoke =
870 case MAC_CHECK_VNODE_SETACL:
871 mpc->mpc_ops->mpo_check_vnode_setacl =
874 case MAC_CHECK_VNODE_SETEXTATTR:
875 mpc->mpc_ops->mpo_check_vnode_setextattr =
878 case MAC_CHECK_VNODE_SETFLAGS:
879 mpc->mpc_ops->mpo_check_vnode_setflags =
882 case MAC_CHECK_VNODE_SETMODE:
883 mpc->mpc_ops->mpo_check_vnode_setmode =
886 case MAC_CHECK_VNODE_SETOWNER:
887 mpc->mpc_ops->mpo_check_vnode_setowner =
890 case MAC_CHECK_VNODE_SETUTIMES:
891 mpc->mpc_ops->mpo_check_vnode_setutimes =
894 case MAC_CHECK_VNODE_STAT:
895 mpc->mpc_ops->mpo_check_vnode_stat =
898 case MAC_CHECK_VNODE_WRITE:
899 mpc->mpc_ops->mpo_check_vnode_write =
904 printf("MAC policy `%s': unknown operation %d\n",
905 mpc->mpc_name, mpe->mpe_constant);
910 MAC_POLICY_LIST_LOCK();
911 if (mac_policy_list_busy > 0) {
912 MAC_POLICY_LIST_UNLOCK();
913 FREE(mpc->mpc_ops, M_MACOPVEC);
917 LIST_FOREACH(tmpc, &mac_policy_list, mpc_list) {
918 if (strcmp(tmpc->mpc_name, mpc->mpc_name) == 0) {
919 MAC_POLICY_LIST_UNLOCK();
920 FREE(mpc->mpc_ops, M_MACOPVEC);
925 if (mpc->mpc_field_off != NULL) {
926 slot = ffs(mac_policy_offsets_free);
928 MAC_POLICY_LIST_UNLOCK();
929 FREE(mpc->mpc_ops, M_MACOPVEC);
934 mac_policy_offsets_free &= ~(1 << slot);
935 *mpc->mpc_field_off = slot;
937 mpc->mpc_runtime_flags |= MPC_RUNTIME_FLAG_REGISTERED;
938 LIST_INSERT_HEAD(&mac_policy_list, mpc, mpc_list);
940 /* Per-policy initialization. */
941 if (mpc->mpc_ops->mpo_init != NULL)
942 (*(mpc->mpc_ops->mpo_init))(mpc);
943 MAC_POLICY_LIST_UNLOCK();
945 printf("Security policy loaded: %s (%s)\n", mpc->mpc_fullname,
952 mac_policy_unregister(struct mac_policy_conf *mpc)
956 * If we fail the load, we may get a request to unload. Check
957 * to see if we did the run-time registration, and if not,
960 MAC_POLICY_LIST_LOCK();
961 if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED) == 0) {
962 MAC_POLICY_LIST_UNLOCK();
967 * Don't allow unloading modules with private data.
969 if (mpc->mpc_field_off != NULL) {
970 MAC_POLICY_LIST_UNLOCK();
975 * Only allow the unload to proceed if the module is unloadable
976 * by its own definition.
978 if ((mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK) == 0) {
979 MAC_POLICY_LIST_UNLOCK();
983 * Right now, we EBUSY if the list is in use. In the future,
984 * for reliability reasons, we might want to sleep and wakeup
985 * later to try again.
987 if (mac_policy_list_busy > 0) {
988 MAC_POLICY_LIST_UNLOCK();
991 if (mpc->mpc_ops->mpo_destroy != NULL)
992 (*(mpc->mpc_ops->mpo_destroy))(mpc);
994 LIST_REMOVE(mpc, mpc_list);
995 MAC_POLICY_LIST_UNLOCK();
997 FREE(mpc->mpc_ops, M_MACOPVEC);
999 mpc->mpc_runtime_flags &= ~MPC_RUNTIME_FLAG_REGISTERED;
1001 printf("Security policy unload: %s (%s)\n", mpc->mpc_fullname,
1008 * Define an error value precedence, and given two arguments, selects the
1009 * value with the higher precedence.
1012 error_select(int error1, int error2)
1015 /* Certain decision-making errors take top priority. */
1016 if (error1 == EDEADLK || error2 == EDEADLK)
1019 /* Invalid arguments should be reported where possible. */
1020 if (error1 == EINVAL || error2 == EINVAL)
1023 /* Precedence goes to "visibility", with both process and file. */
1024 if (error1 == ESRCH || error2 == ESRCH)
1027 if (error1 == ENOENT || error2 == ENOENT)
1030 /* Precedence goes to DAC/MAC protections. */
1031 if (error1 == EACCES || error2 == EACCES)
1034 /* Precedence goes to privilege. */
1035 if (error1 == EPERM || error2 == EPERM)
1038 /* Precedence goes to error over success; otherwise, arbitrary. */
1045 mac_init_label(struct label *label)
1048 bzero(label, sizeof(*label));
1049 label->l_flags = MAC_FLAG_INITIALIZED;
1053 mac_destroy_label(struct label *label)
1056 KASSERT(label->l_flags & MAC_FLAG_INITIALIZED,
1057 ("destroying uninitialized label"));
1059 bzero(label, sizeof(*label));
1060 /* implicit: label->l_flags &= ~MAC_FLAG_INITIALIZED; */
1064 mac_init_structmac(struct mac *mac)
1067 bzero(mac, sizeof(*mac));
1068 mac->m_macflags = MAC_FLAG_INITIALIZED;
1072 mac_init_bpfdesc(struct bpf_d *bpf_d)
1075 mac_init_label(&bpf_d->bd_label);
1076 MAC_PERFORM(init_bpfdesc_label, &bpf_d->bd_label);
1078 atomic_add_int(&nmacbpfdescs, 1);
1083 mac_init_cred(struct ucred *cr)
1086 mac_init_label(&cr->cr_label);
1087 MAC_PERFORM(init_cred_label, &cr->cr_label);
1089 atomic_add_int(&nmaccreds, 1);
1094 mac_init_devfsdirent(struct devfs_dirent *de)
1097 mac_init_label(&de->de_label);
1098 MAC_PERFORM(init_devfsdirent_label, &de->de_label);
1100 atomic_add_int(&nmacdevfsdirents, 1);
1105 mac_init_ifnet(struct ifnet *ifp)
1108 mac_init_label(&ifp->if_label);
1109 MAC_PERFORM(init_ifnet_label, &ifp->if_label);
1111 atomic_add_int(&nmacifnets, 1);
1116 mac_init_ipq(struct ipq *ipq)
1119 mac_init_label(&ipq->ipq_label);
1120 MAC_PERFORM(init_ipq_label, &ipq->ipq_label);
1122 atomic_add_int(&nmacipqs, 1);
1127 mac_init_mbuf(struct mbuf *m, int flag)
1131 KASSERT(m->m_flags & M_PKTHDR, ("mac_init_mbuf on non-header mbuf"));
1133 mac_init_label(&m->m_pkthdr.label);
1135 MAC_CHECK(init_mbuf_label, &m->m_pkthdr.label, flag);
1137 MAC_PERFORM(destroy_mbuf_label, &m->m_pkthdr.label);
1138 mac_destroy_label(&m->m_pkthdr.label);
1143 atomic_add_int(&nmacmbufs, 1);
1149 mac_init_mount(struct mount *mp)
1152 mac_init_label(&mp->mnt_mntlabel);
1153 mac_init_label(&mp->mnt_fslabel);
1154 MAC_PERFORM(init_mount_label, &mp->mnt_mntlabel);
1155 MAC_PERFORM(init_mount_fs_label, &mp->mnt_fslabel);
1157 atomic_add_int(&nmacmounts, 1);
1162 mac_init_pipe(struct pipe *pipe)
1164 struct label *label;
1166 label = malloc(sizeof(struct label), M_MACPIPELABEL, M_ZERO|M_WAITOK);
1167 mac_init_label(label);
1168 pipe->pipe_label = label;
1169 pipe->pipe_peer->pipe_label = label;
1170 MAC_PERFORM(init_pipe_label, pipe->pipe_label);
1172 atomic_add_int(&nmacpipes, 1);
1177 mac_init_socket_label(struct label *label, int flag)
1181 mac_init_label(label);
1183 MAC_CHECK(init_socket_label, label, flag);
1185 MAC_PERFORM(destroy_socket_label, label);
1186 mac_destroy_label(label);
1191 atomic_add_int(&nmacsockets, 1);
1198 mac_init_socket_peer_label(struct label *label, int flag)
1202 mac_init_label(label);
1204 MAC_CHECK(init_socket_peer_label, label, flag);
1206 MAC_PERFORM(destroy_socket_label, label);
1207 mac_destroy_label(label);
1214 mac_init_socket(struct socket *socket, int flag)
1218 error = mac_init_socket_label(&socket->so_label, flag);
1222 error = mac_init_socket_peer_label(&socket->so_peerlabel, flag);
1224 mac_destroy_socket_label(&socket->so_label);
1230 mac_init_temp(struct label *label)
1233 mac_init_label(label);
1234 MAC_PERFORM(init_temp_label, label);
1236 atomic_add_int(&nmactemp, 1);
1241 mac_init_vnode(struct vnode *vp)
1244 mac_init_label(&vp->v_label);
1245 MAC_PERFORM(init_vnode_label, &vp->v_label);
1247 atomic_add_int(&nmacvnodes, 1);
1252 mac_destroy_bpfdesc(struct bpf_d *bpf_d)
1255 MAC_PERFORM(destroy_bpfdesc_label, &bpf_d->bd_label);
1256 mac_destroy_label(&bpf_d->bd_label);
1258 atomic_subtract_int(&nmacbpfdescs, 1);
1263 mac_destroy_cred(struct ucred *cr)
1266 MAC_PERFORM(destroy_cred_label, &cr->cr_label);
1267 mac_destroy_label(&cr->cr_label);
1269 atomic_subtract_int(&nmaccreds, 1);
1274 mac_destroy_devfsdirent(struct devfs_dirent *de)
1277 MAC_PERFORM(destroy_devfsdirent_label, &de->de_label);
1278 mac_destroy_label(&de->de_label);
1280 atomic_subtract_int(&nmacdevfsdirents, 1);
1285 mac_destroy_ifnet(struct ifnet *ifp)
1288 MAC_PERFORM(destroy_ifnet_label, &ifp->if_label);
1289 mac_destroy_label(&ifp->if_label);
1291 atomic_subtract_int(&nmacifnets, 1);
1296 mac_destroy_ipq(struct ipq *ipq)
1299 MAC_PERFORM(destroy_ipq_label, &ipq->ipq_label);
1300 mac_destroy_label(&ipq->ipq_label);
1302 atomic_subtract_int(&nmacipqs, 1);
1307 mac_destroy_mbuf(struct mbuf *m)
1310 MAC_PERFORM(destroy_mbuf_label, &m->m_pkthdr.label);
1311 mac_destroy_label(&m->m_pkthdr.label);
1313 atomic_subtract_int(&nmacmbufs, 1);
1318 mac_destroy_mount(struct mount *mp)
1321 MAC_PERFORM(destroy_mount_label, &mp->mnt_mntlabel);
1322 MAC_PERFORM(destroy_mount_fs_label, &mp->mnt_fslabel);
1323 mac_destroy_label(&mp->mnt_fslabel);
1324 mac_destroy_label(&mp->mnt_mntlabel);
1326 atomic_subtract_int(&nmacmounts, 1);
1331 mac_destroy_pipe(struct pipe *pipe)
1334 MAC_PERFORM(destroy_pipe_label, pipe->pipe_label);
1335 mac_destroy_label(pipe->pipe_label);
1336 free(pipe->pipe_label, M_MACPIPELABEL);
1338 atomic_subtract_int(&nmacpipes, 1);
1343 mac_destroy_socket_label(struct label *label)
1346 MAC_PERFORM(destroy_socket_label, label);
1347 mac_destroy_label(label);
1349 atomic_subtract_int(&nmacsockets, 1);
1354 mac_destroy_socket_peer_label(struct label *label)
1357 MAC_PERFORM(destroy_socket_peer_label, label);
1358 mac_destroy_label(label);
1362 mac_destroy_socket(struct socket *socket)
1365 mac_destroy_socket_label(&socket->so_label);
1366 mac_destroy_socket_peer_label(&socket->so_peerlabel);
1370 mac_destroy_temp(struct label *label)
1373 MAC_PERFORM(destroy_temp_label, label);
1374 mac_destroy_label(label);
1376 atomic_subtract_int(&nmactemp, 1);
1381 mac_destroy_vnode(struct vnode *vp)
1384 MAC_PERFORM(destroy_vnode_label, &vp->v_label);
1385 mac_destroy_label(&vp->v_label);
1387 atomic_subtract_int(&nmacvnodes, 1);
1392 mac_externalize(struct label *label, struct mac *mac)
1396 mac_init_structmac(mac);
1397 MAC_CHECK(externalize, label, mac);
1403 mac_internalize(struct label *label, struct mac *mac)
1407 mac_init_temp(label);
1408 MAC_CHECK(internalize, label, mac);
1410 mac_destroy_temp(label);
1416 * Initialize MAC label for the first kernel process, from which other
1417 * kernel processes and threads are spawned.
1420 mac_create_proc0(struct ucred *cred)
1423 MAC_PERFORM(create_proc0, cred);
1427 * Initialize MAC label for the first userland process, from which other
1428 * userland processes and threads are spawned.
1431 mac_create_proc1(struct ucred *cred)
1434 MAC_PERFORM(create_proc1, cred);
1438 mac_thread_userret(struct thread *td)
1441 MAC_PERFORM(thread_userret, td);
1445 * When a new process is created, its label must be initialized. Generally,
1446 * this involves inheritence from the parent process, modulo possible
1447 * deltas. This function allows that processing to take place.
1450 mac_create_cred(struct ucred *parent_cred, struct ucred *child_cred)
1453 MAC_PERFORM(create_cred, parent_cred, child_cred);
1457 mac_update_devfsdirent(struct devfs_dirent *de, struct vnode *vp)
1460 MAC_PERFORM(update_devfsdirent, de, &de->de_label, vp, &vp->v_label);
1464 mac_update_procfsvnode(struct vnode *vp, struct ucred *cred)
1467 MAC_PERFORM(update_procfsvnode, vp, &vp->v_label, cred);
1471 * Support callout for policies that manage their own externalization
1472 * using extended attributes.
1475 mac_update_vnode_from_extattr(struct vnode *vp, struct mount *mp)
1479 MAC_CHECK(update_vnode_from_extattr, vp, &vp->v_label, mp,
1486 * Given an externalized mac label, internalize it and stamp it on a
1490 mac_update_vnode_from_externalized(struct vnode *vp, struct mac *extmac)
1494 MAC_CHECK(update_vnode_from_externalized, vp, &vp->v_label, extmac);
1500 * Call out to individual policies to update the label in a vnode from
1504 mac_update_vnode_from_mount(struct vnode *vp, struct mount *mp)
1507 MAC_PERFORM(update_vnode_from_mount, vp, &vp->v_label, mp,
1510 ASSERT_VOP_LOCKED(vp, "mac_update_vnode_from_mount");
1511 if (mac_cache_fslabel_in_vnode)
1512 vp->v_vflag |= VV_CACHEDLABEL;
1516 * Implementation of VOP_REFRESHLABEL() that relies on extended attributes
1517 * to store label data. Can be referenced by filesystems supporting
1518 * extended attributes.
1521 vop_stdrefreshlabel_ea(struct vop_refreshlabel_args *ap)
1523 struct vnode *vp = ap->a_vp;
1527 ASSERT_VOP_LOCKED(vp, "vop_stdrefreshlabel_ea");
1530 * Call out to external policies first. Order doesn't really
1531 * matter, as long as failure of one assures failure of all.
1533 error = mac_update_vnode_from_extattr(vp, vp->v_mount);
1537 buflen = sizeof(extmac);
1538 error = vn_extattr_get(vp, IO_NODELOCKED,
1539 FREEBSD_MAC_EXTATTR_NAMESPACE, FREEBSD_MAC_EXTATTR_NAME, &buflen,
1540 (char *)&extmac, curthread);
1548 * Use the label from the mount point.
1550 mac_update_vnode_from_mount(vp, vp->v_mount);
1555 /* Fail horribly. */
1559 if (buflen != sizeof(extmac))
1560 error = EPERM; /* Fail very closed. */
1562 error = mac_update_vnode_from_externalized(vp, &extmac);
1564 vp->v_vflag |= VV_CACHEDLABEL;
1568 printf("Corrupted label on %s",
1569 vp->v_mount->mnt_stat.f_mntonname);
1570 if (VOP_GETATTR(vp, &va, curthread->td_ucred, curthread) == 0)
1571 printf(" inum %ld", va.va_fileid);
1573 if (mac_debug_label_fallback) {
1574 printf(", falling back.\n");
1575 mac_update_vnode_from_mount(vp, vp->v_mount);
1590 * Make sure the vnode label is up-to-date. If EOPNOTSUPP, then we handle
1591 * the labeling activity outselves. Filesystems should be careful not
1592 * to change their minds regarding whether they support vop_refreshlabel()
1593 * for a vnode or not. Don't cache the vnode here, allow the file
1594 * system code to determine if it's safe to cache. If we update from
1595 * the mount, don't cache since a change to the mount label should affect
1599 vn_refreshlabel(struct vnode *vp, struct ucred *cred)
1603 ASSERT_VOP_LOCKED(vp, "vn_refreshlabel");
1605 if (vp->v_mount == NULL) {
1607 Eventually, we probably want to special-case refreshing
1608 of deadfs vnodes, and if there's a lock-free race somewhere,
1609 that case might be handled here.
1611 mac_update_vnode_deadfs(vp);
1614 /* printf("vn_refreshlabel: null v_mount\n"); */
1615 if (vp->v_type != VNON)
1617 "vn_refreshlabel: null v_mount with non-VNON\n");
1621 if (vp->v_vflag & VV_CACHEDLABEL) {
1622 mac_vnode_label_cache_hits++;
1625 mac_vnode_label_cache_misses++;
1627 if ((vp->v_mount->mnt_flag & MNT_MULTILABEL) == 0) {
1628 mac_update_vnode_from_mount(vp, vp->v_mount);
1632 error = VOP_REFRESHLABEL(vp, cred, curthread);
1636 * If labels are not supported on this vnode, fall back to
1637 * the label in the mount and propagate it to the vnode.
1638 * There should probably be some sort of policy/flag/decision
1641 mac_update_vnode_from_mount(vp, vp->v_mount);
1649 * Helper function for file systems using the vop_std*_ea() calls. This
1650 * function must be called after EA service is available for the vnode,
1651 * but before it's hooked up to the namespace so that the node persists
1652 * if there's a crash, or before it can be accessed. On successful
1653 * commit of the label to disk (etc), do cache the label.
1656 vop_stdcreatevnode_ea(struct vnode *dvp, struct vnode *tvp, struct ucred *cred)
1661 ASSERT_VOP_LOCKED(tvp, "vop_stdcreatevnode_ea");
1662 if ((dvp->v_mount->mnt_flag & MNT_MULTILABEL) == 0) {
1663 mac_update_vnode_from_mount(tvp, tvp->v_mount);
1665 error = vn_refreshlabel(dvp, cred);
1670 * Stick the label in the vnode. Then try to write to
1671 * disk. If we fail, return a failure to abort the
1672 * create operation. Really, this failure shouldn't
1673 * happen except in fairly unusual circumstances (out
1676 mac_create_vnode(cred, dvp, tvp);
1678 error = mac_stdcreatevnode_ea(tvp);
1683 * XXX: Eventually this will go away and all policies will
1684 * directly manage their extended attributes.
1686 error = mac_externalize(&tvp->v_label, &extmac);
1690 error = vn_extattr_set(tvp, IO_NODELOCKED,
1691 FREEBSD_MAC_EXTATTR_NAMESPACE, FREEBSD_MAC_EXTATTR_NAME,
1692 sizeof(extmac), (char *)&extmac, curthread);
1694 tvp->v_vflag |= VV_CACHEDLABEL;
1698 * In theory, we could have fall-back behavior here.
1699 * It would probably be incorrect.
1710 mac_execve_transition(struct ucred *old, struct ucred *new, struct vnode *vp)
1714 ASSERT_VOP_LOCKED(vp, "mac_execve_transition");
1716 error = vn_refreshlabel(vp, old);
1718 printf("mac_execve_transition: vn_refreshlabel returned %d\n",
1720 printf("mac_execve_transition: using old vnode label\n");
1723 MAC_PERFORM(execve_transition, old, new, vp, &vp->v_label);
1727 mac_execve_will_transition(struct ucred *old, struct vnode *vp)
1731 error = vn_refreshlabel(vp, old);
1736 MAC_BOOLEAN(execve_will_transition, ||, old, vp, &vp->v_label);
1742 mac_check_vnode_access(struct ucred *cred, struct vnode *vp, int flags)
1746 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_access");
1748 if (!mac_enforce_fs)
1751 error = vn_refreshlabel(vp, cred);
1755 MAC_CHECK(check_vnode_access, cred, vp, &vp->v_label, flags);
1760 mac_check_vnode_chdir(struct ucred *cred, struct vnode *dvp)
1764 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_chdir");
1766 if (!mac_enforce_fs)
1769 error = vn_refreshlabel(dvp, cred);
1773 MAC_CHECK(check_vnode_chdir, cred, dvp, &dvp->v_label);
1778 mac_check_vnode_chroot(struct ucred *cred, struct vnode *dvp)
1782 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_chroot");
1784 if (!mac_enforce_fs)
1787 error = vn_refreshlabel(dvp, cred);
1791 MAC_CHECK(check_vnode_chroot, cred, dvp, &dvp->v_label);
1796 mac_check_vnode_create(struct ucred *cred, struct vnode *dvp,
1797 struct componentname *cnp, struct vattr *vap)
1801 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_create");
1803 if (!mac_enforce_fs)
1806 error = vn_refreshlabel(dvp, cred);
1810 MAC_CHECK(check_vnode_create, cred, dvp, &dvp->v_label, cnp, vap);
1815 mac_check_vnode_delete(struct ucred *cred, struct vnode *dvp, struct vnode *vp,
1816 struct componentname *cnp)
1820 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_delete");
1821 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_delete");
1823 if (!mac_enforce_fs)
1826 error = vn_refreshlabel(dvp, cred);
1829 error = vn_refreshlabel(vp, cred);
1833 MAC_CHECK(check_vnode_delete, cred, dvp, &dvp->v_label, vp,
1839 mac_check_vnode_deleteacl(struct ucred *cred, struct vnode *vp,
1844 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_deleteacl");
1846 if (!mac_enforce_fs)
1849 error = vn_refreshlabel(vp, cred);
1853 MAC_CHECK(check_vnode_deleteacl, cred, vp, &vp->v_label, type);
1858 mac_check_vnode_exec(struct ucred *cred, struct vnode *vp)
1862 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_exec");
1864 if (!mac_enforce_process && !mac_enforce_fs)
1867 error = vn_refreshlabel(vp, cred);
1870 MAC_CHECK(check_vnode_exec, cred, vp, &vp->v_label);
1876 mac_check_vnode_getacl(struct ucred *cred, struct vnode *vp, acl_type_t type)
1880 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_getacl");
1882 if (!mac_enforce_fs)
1885 error = vn_refreshlabel(vp, cred);
1889 MAC_CHECK(check_vnode_getacl, cred, vp, &vp->v_label, type);
1894 mac_check_vnode_getextattr(struct ucred *cred, struct vnode *vp,
1895 int attrnamespace, const char *name, struct uio *uio)
1899 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_getextattr");
1901 if (!mac_enforce_fs)
1904 error = vn_refreshlabel(vp, cred);
1908 MAC_CHECK(check_vnode_getextattr, cred, vp, &vp->v_label,
1909 attrnamespace, name, uio);
1914 mac_check_vnode_link(struct ucred *cred, struct vnode *dvp,
1915 struct vnode *vp, struct componentname *cnp)
1920 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_link");
1921 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_link");
1923 if (!mac_enforce_fs)
1926 error = vn_refreshlabel(dvp, cred);
1930 error = vn_refreshlabel(vp, cred);
1934 MAC_CHECK(check_vnode_link, cred, dvp, &dvp->v_label, vp,
1940 mac_check_vnode_lookup(struct ucred *cred, struct vnode *dvp,
1941 struct componentname *cnp)
1945 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_lookup");
1947 if (!mac_enforce_fs)
1950 error = vn_refreshlabel(dvp, cred);
1954 MAC_CHECK(check_vnode_lookup, cred, dvp, &dvp->v_label, cnp);
1959 mac_check_vnode_mmap(struct ucred *cred, struct vnode *vp, int prot)
1963 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mmap");
1965 if (!mac_enforce_fs || !mac_enforce_vm)
1968 error = vn_refreshlabel(vp, cred);
1972 MAC_CHECK(check_vnode_mmap, cred, vp, &vp->v_label, prot);
1977 mac_check_vnode_mmap_downgrade(struct ucred *cred, struct vnode *vp, int *prot)
1981 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mmap_downgrade");
1983 if (!mac_enforce_fs || !mac_enforce_vm)
1986 MAC_PERFORM(check_vnode_mmap_downgrade, cred, vp, &vp->v_label,
1993 mac_check_vnode_mprotect(struct ucred *cred, struct vnode *vp, int prot)
1997 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mprotect");
1999 if (!mac_enforce_fs || !mac_enforce_vm)
2002 error = vn_refreshlabel(vp, cred);
2006 MAC_CHECK(check_vnode_mprotect, cred, vp, &vp->v_label, prot);
2011 mac_check_vnode_open(struct ucred *cred, struct vnode *vp, mode_t acc_mode)
2015 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_open");
2017 if (!mac_enforce_fs)
2020 error = vn_refreshlabel(vp, cred);
2024 MAC_CHECK(check_vnode_open, cred, vp, &vp->v_label, acc_mode);
2029 mac_check_vnode_poll(struct ucred *active_cred, struct ucred *file_cred,
2034 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_poll");
2036 if (!mac_enforce_fs)
2039 error = vn_refreshlabel(vp, active_cred);
2043 MAC_CHECK(check_vnode_poll, active_cred, file_cred, vp,
2050 mac_check_vnode_read(struct ucred *active_cred, struct ucred *file_cred,
2055 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_read");
2057 if (!mac_enforce_fs)
2060 error = vn_refreshlabel(vp, active_cred);
2064 MAC_CHECK(check_vnode_read, active_cred, file_cred, vp,
2071 mac_check_vnode_readdir(struct ucred *cred, struct vnode *dvp)
2075 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_readdir");
2077 if (!mac_enforce_fs)
2080 error = vn_refreshlabel(dvp, cred);
2084 MAC_CHECK(check_vnode_readdir, cred, dvp, &dvp->v_label);
2089 mac_check_vnode_readlink(struct ucred *cred, struct vnode *vp)
2093 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_readlink");
2095 if (!mac_enforce_fs)
2098 error = vn_refreshlabel(vp, cred);
2102 MAC_CHECK(check_vnode_readlink, cred, vp, &vp->v_label);
2107 mac_check_vnode_relabel(struct ucred *cred, struct vnode *vp,
2108 struct label *newlabel)
2112 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_relabel");
2114 error = vn_refreshlabel(vp, cred);
2118 MAC_CHECK(check_vnode_relabel, cred, vp, &vp->v_label, newlabel);
2124 mac_check_vnode_rename_from(struct ucred *cred, struct vnode *dvp,
2125 struct vnode *vp, struct componentname *cnp)
2129 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_rename_from");
2130 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_rename_from");
2132 if (!mac_enforce_fs)
2135 error = vn_refreshlabel(dvp, cred);
2138 error = vn_refreshlabel(vp, cred);
2142 MAC_CHECK(check_vnode_rename_from, cred, dvp, &dvp->v_label, vp,
2148 mac_check_vnode_rename_to(struct ucred *cred, struct vnode *dvp,
2149 struct vnode *vp, int samedir, struct componentname *cnp)
2153 ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_rename_to");
2154 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_rename_to");
2156 if (!mac_enforce_fs)
2159 error = vn_refreshlabel(dvp, cred);
2163 error = vn_refreshlabel(vp, cred);
2167 MAC_CHECK(check_vnode_rename_to, cred, dvp, &dvp->v_label, vp,
2168 vp != NULL ? &vp->v_label : NULL, samedir, cnp);
2173 mac_check_vnode_revoke(struct ucred *cred, struct vnode *vp)
2177 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_revoke");
2179 if (!mac_enforce_fs)
2182 error = vn_refreshlabel(vp, cred);
2186 MAC_CHECK(check_vnode_revoke, cred, vp, &vp->v_label);
2191 mac_check_vnode_setacl(struct ucred *cred, struct vnode *vp, acl_type_t type,
2196 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setacl");
2198 if (!mac_enforce_fs)
2201 error = vn_refreshlabel(vp, cred);
2205 MAC_CHECK(check_vnode_setacl, cred, vp, &vp->v_label, type, acl);
2210 mac_check_vnode_setextattr(struct ucred *cred, struct vnode *vp,
2211 int attrnamespace, const char *name, struct uio *uio)
2215 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setextattr");
2217 if (!mac_enforce_fs)
2220 error = vn_refreshlabel(vp, cred);
2224 MAC_CHECK(check_vnode_setextattr, cred, vp, &vp->v_label,
2225 attrnamespace, name, uio);
2230 mac_check_vnode_setflags(struct ucred *cred, struct vnode *vp, u_long flags)
2234 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setflags");
2236 if (!mac_enforce_fs)
2239 error = vn_refreshlabel(vp, cred);
2243 MAC_CHECK(check_vnode_setflags, cred, vp, &vp->v_label, flags);
2248 mac_check_vnode_setmode(struct ucred *cred, struct vnode *vp, mode_t mode)
2252 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setmode");
2254 if (!mac_enforce_fs)
2257 error = vn_refreshlabel(vp, cred);
2261 MAC_CHECK(check_vnode_setmode, cred, vp, &vp->v_label, mode);
2266 mac_check_vnode_setowner(struct ucred *cred, struct vnode *vp, uid_t uid,
2271 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setowner");
2273 if (!mac_enforce_fs)
2276 error = vn_refreshlabel(vp, cred);
2280 MAC_CHECK(check_vnode_setowner, cred, vp, &vp->v_label, uid, gid);
2285 mac_check_vnode_setutimes(struct ucred *cred, struct vnode *vp,
2286 struct timespec atime, struct timespec mtime)
2290 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setutimes");
2292 if (!mac_enforce_fs)
2295 error = vn_refreshlabel(vp, cred);
2299 MAC_CHECK(check_vnode_setutimes, cred, vp, &vp->v_label, atime,
2305 mac_check_vnode_stat(struct ucred *active_cred, struct ucred *file_cred,
2310 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_stat");
2312 if (!mac_enforce_fs)
2315 error = vn_refreshlabel(vp, active_cred);
2319 MAC_CHECK(check_vnode_stat, active_cred, file_cred, vp,
2325 mac_check_vnode_write(struct ucred *active_cred, struct ucred *file_cred,
2330 ASSERT_VOP_LOCKED(vp, "mac_check_vnode_write");
2332 if (!mac_enforce_fs)
2335 error = vn_refreshlabel(vp, active_cred);
2339 MAC_CHECK(check_vnode_write, active_cred, file_cred, vp,
2346 * When relabeling a process, call out to the policies for the maximum
2347 * permission allowed for each object type we know about in its
2348 * memory space, and revoke access (in the least surprising ways we
2349 * know) when necessary. The process lock is not held here.
2352 mac_cred_mmapped_drop_perms(struct thread *td, struct ucred *cred)
2355 /* XXX freeze all other threads */
2356 mac_cred_mmapped_drop_perms_recurse(td, cred,
2357 &td->td_proc->p_vmspace->vm_map);
2358 /* XXX allow other threads to continue */
2361 static __inline const char *
2362 prot2str(vm_prot_t prot)
2365 switch (prot & VM_PROT_ALL) {
2368 case VM_PROT_READ | VM_PROT_WRITE:
2370 case VM_PROT_READ | VM_PROT_EXECUTE:
2372 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
2376 case VM_PROT_EXECUTE:
2378 case VM_PROT_WRITE | VM_PROT_EXECUTE:
2386 mac_cred_mmapped_drop_perms_recurse(struct thread *td, struct ucred *cred,
2389 struct vm_map_entry *vme;
2391 vm_prot_t revokeperms;
2393 vm_ooffset_t offset;
2396 if (!mac_mmap_revocation)
2399 vm_map_lock_read(map);
2400 for (vme = map->header.next; vme != &map->header; vme = vme->next) {
2401 if (vme->eflags & MAP_ENTRY_IS_SUB_MAP) {
2402 mac_cred_mmapped_drop_perms_recurse(td, cred,
2403 vme->object.sub_map);
2407 * Skip over entries that obviously are not shared.
2409 if (vme->eflags & (MAP_ENTRY_COW | MAP_ENTRY_NOSYNC) ||
2410 !vme->max_protection)
2413 * Drill down to the deepest backing object.
2415 offset = vme->offset;
2416 object = vme->object.vm_object;
2419 while (object->backing_object != NULL) {
2420 object = object->backing_object;
2421 offset += object->backing_object_offset;
2424 * At the moment, vm_maps and objects aren't considered
2425 * by the MAC system, so only things with backing by a
2426 * normal object (read: vnodes) are checked.
2428 if (object->type != OBJT_VNODE)
2430 vp = (struct vnode *)object->handle;
2431 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2432 result = vme->max_protection;
2433 mac_check_vnode_mmap_downgrade(cred, vp, &result);
2434 VOP_UNLOCK(vp, 0, td);
2436 * Find out what maximum protection we may be allowing
2437 * now but a policy needs to get removed.
2439 revokeperms = vme->max_protection & ~result;
2442 printf("pid %ld: revoking %s perms from %#lx:%ld "
2443 "(max %s/cur %s)\n", (long)td->td_proc->p_pid,
2444 prot2str(revokeperms), (u_long)vme->start,
2445 (long)(vme->end - vme->start),
2446 prot2str(vme->max_protection), prot2str(vme->protection));
2447 vm_map_lock_upgrade(map);
2449 * This is the really simple case: if a map has more
2450 * max_protection than is allowed, but it's not being
2451 * actually used (that is, the current protection is
2452 * still allowed), we can just wipe it out and do
2455 if ((vme->protection & revokeperms) == 0) {
2456 vme->max_protection -= revokeperms;
2458 if (revokeperms & VM_PROT_WRITE) {
2460 * In the more complicated case, flush out all
2461 * pending changes to the object then turn it
2464 vm_object_reference(object);
2465 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2466 vm_object_page_clean(object,
2468 OFF_TO_IDX(offset + vme->end - vme->start +
2471 VOP_UNLOCK(vp, 0, td);
2472 vm_object_deallocate(object);
2474 * Why bother if there's no read permissions
2475 * anymore? For the rest, we need to leave
2476 * the write permissions on for COW, or
2477 * remove them entirely if configured to.
2479 if (!mac_mmap_revocation_via_cow) {
2480 vme->max_protection &= ~VM_PROT_WRITE;
2481 vme->protection &= ~VM_PROT_WRITE;
2482 } if ((revokeperms & VM_PROT_READ) == 0)
2483 vme->eflags |= MAP_ENTRY_COW |
2484 MAP_ENTRY_NEEDS_COPY;
2486 if (revokeperms & VM_PROT_EXECUTE) {
2487 vme->max_protection &= ~VM_PROT_EXECUTE;
2488 vme->protection &= ~VM_PROT_EXECUTE;
2490 if (revokeperms & VM_PROT_READ) {
2491 vme->max_protection = 0;
2492 vme->protection = 0;
2494 pmap_protect(map->pmap, vme->start, vme->end,
2495 vme->protection & ~revokeperms);
2496 vm_map_simplify_entry(map, vme);
2498 vm_map_lock_downgrade(map);
2500 vm_map_unlock_read(map);
2504 * When the subject's label changes, it may require revocation of privilege
2505 * to mapped objects. This can't be done on-the-fly later with a unified
2509 mac_relabel_cred(struct ucred *cred, struct label *newlabel)
2512 MAC_PERFORM(relabel_cred, cred, newlabel);
2516 mac_relabel_vnode(struct ucred *cred, struct vnode *vp, struct label *newlabel)
2519 MAC_PERFORM(relabel_vnode, cred, vp, &vp->v_label, newlabel);
2523 mac_create_ifnet(struct ifnet *ifnet)
2526 MAC_PERFORM(create_ifnet, ifnet, &ifnet->if_label);
2530 mac_create_bpfdesc(struct ucred *cred, struct bpf_d *bpf_d)
2533 MAC_PERFORM(create_bpfdesc, cred, bpf_d, &bpf_d->bd_label);
2537 mac_create_socket(struct ucred *cred, struct socket *socket)
2540 MAC_PERFORM(create_socket, cred, socket, &socket->so_label);
2544 mac_create_pipe(struct ucred *cred, struct pipe *pipe)
2547 MAC_PERFORM(create_pipe, cred, pipe, pipe->pipe_label);
2551 mac_create_socket_from_socket(struct socket *oldsocket,
2552 struct socket *newsocket)
2555 MAC_PERFORM(create_socket_from_socket, oldsocket, &oldsocket->so_label,
2556 newsocket, &newsocket->so_label);
2560 mac_relabel_socket(struct ucred *cred, struct socket *socket,
2561 struct label *newlabel)
2564 MAC_PERFORM(relabel_socket, cred, socket, &socket->so_label, newlabel);
2568 mac_relabel_pipe(struct ucred *cred, struct pipe *pipe, struct label *newlabel)
2571 MAC_PERFORM(relabel_pipe, cred, pipe, pipe->pipe_label, newlabel);
2575 mac_set_socket_peer_from_mbuf(struct mbuf *mbuf, struct socket *socket)
2578 MAC_PERFORM(set_socket_peer_from_mbuf, mbuf, &mbuf->m_pkthdr.label,
2579 socket, &socket->so_peerlabel);
2583 mac_set_socket_peer_from_socket(struct socket *oldsocket,
2584 struct socket *newsocket)
2587 MAC_PERFORM(set_socket_peer_from_socket, oldsocket,
2588 &oldsocket->so_label, newsocket, &newsocket->so_peerlabel);
2592 mac_create_datagram_from_ipq(struct ipq *ipq, struct mbuf *datagram)
2595 MAC_PERFORM(create_datagram_from_ipq, ipq, &ipq->ipq_label,
2596 datagram, &datagram->m_pkthdr.label);
2600 mac_create_fragment(struct mbuf *datagram, struct mbuf *fragment)
2603 MAC_PERFORM(create_fragment, datagram, &datagram->m_pkthdr.label,
2604 fragment, &fragment->m_pkthdr.label);
2608 mac_create_ipq(struct mbuf *fragment, struct ipq *ipq)
2611 MAC_PERFORM(create_ipq, fragment, &fragment->m_pkthdr.label, ipq,
2616 mac_create_mbuf_from_mbuf(struct mbuf *oldmbuf, struct mbuf *newmbuf)
2619 MAC_PERFORM(create_mbuf_from_mbuf, oldmbuf, &oldmbuf->m_pkthdr.label,
2620 newmbuf, &newmbuf->m_pkthdr.label);
2624 mac_create_mbuf_from_bpfdesc(struct bpf_d *bpf_d, struct mbuf *mbuf)
2627 MAC_PERFORM(create_mbuf_from_bpfdesc, bpf_d, &bpf_d->bd_label, mbuf,
2628 &mbuf->m_pkthdr.label);
2632 mac_create_mbuf_linklayer(struct ifnet *ifnet, struct mbuf *mbuf)
2635 MAC_PERFORM(create_mbuf_linklayer, ifnet, &ifnet->if_label, mbuf,
2636 &mbuf->m_pkthdr.label);
2640 mac_create_mbuf_from_ifnet(struct ifnet *ifnet, struct mbuf *mbuf)
2643 MAC_PERFORM(create_mbuf_from_ifnet, ifnet, &ifnet->if_label, mbuf,
2644 &mbuf->m_pkthdr.label);
2648 mac_create_mbuf_multicast_encap(struct mbuf *oldmbuf, struct ifnet *ifnet,
2649 struct mbuf *newmbuf)
2652 MAC_PERFORM(create_mbuf_multicast_encap, oldmbuf,
2653 &oldmbuf->m_pkthdr.label, ifnet, &ifnet->if_label, newmbuf,
2654 &newmbuf->m_pkthdr.label);
2658 mac_create_mbuf_netlayer(struct mbuf *oldmbuf, struct mbuf *newmbuf)
2661 MAC_PERFORM(create_mbuf_netlayer, oldmbuf, &oldmbuf->m_pkthdr.label,
2662 newmbuf, &newmbuf->m_pkthdr.label);
2666 mac_fragment_match(struct mbuf *fragment, struct ipq *ipq)
2671 MAC_BOOLEAN(fragment_match, &&, fragment, &fragment->m_pkthdr.label,
2672 ipq, &ipq->ipq_label);
2678 mac_update_ipq(struct mbuf *fragment, struct ipq *ipq)
2681 MAC_PERFORM(update_ipq, fragment, &fragment->m_pkthdr.label, ipq,
2686 mac_create_mbuf_from_socket(struct socket *socket, struct mbuf *mbuf)
2689 MAC_PERFORM(create_mbuf_from_socket, socket, &socket->so_label, mbuf,
2690 &mbuf->m_pkthdr.label);
2694 mac_create_mount(struct ucred *cred, struct mount *mp)
2697 MAC_PERFORM(create_mount, cred, mp, &mp->mnt_mntlabel,
2702 mac_create_root_mount(struct ucred *cred, struct mount *mp)
2705 MAC_PERFORM(create_root_mount, cred, mp, &mp->mnt_mntlabel,
2710 mac_check_bpfdesc_receive(struct bpf_d *bpf_d, struct ifnet *ifnet)
2714 if (!mac_enforce_network)
2717 MAC_CHECK(check_bpfdesc_receive, bpf_d, &bpf_d->bd_label, ifnet,
2724 mac_check_cred_relabel(struct ucred *cred, struct label *newlabel)
2728 MAC_CHECK(check_cred_relabel, cred, newlabel);
2734 mac_check_cred_visible(struct ucred *u1, struct ucred *u2)
2738 if (!mac_enforce_process)
2741 MAC_CHECK(check_cred_visible, u1, u2);
2747 mac_check_ifnet_transmit(struct ifnet *ifnet, struct mbuf *mbuf)
2751 if (!mac_enforce_network)
2754 KASSERT(mbuf->m_flags & M_PKTHDR, ("packet has no pkthdr"));
2755 if (!(mbuf->m_pkthdr.label.l_flags & MAC_FLAG_INITIALIZED))
2756 printf("%s%d: not initialized\n", ifnet->if_name,
2759 MAC_CHECK(check_ifnet_transmit, ifnet, &ifnet->if_label, mbuf,
2760 &mbuf->m_pkthdr.label);
2766 mac_check_mount_stat(struct ucred *cred, struct mount *mount)
2770 if (!mac_enforce_fs)
2773 MAC_CHECK(check_mount_stat, cred, mount, &mount->mnt_mntlabel);
2779 mac_check_pipe_ioctl(struct ucred *cred, struct pipe *pipe, unsigned long cmd,
2784 PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2786 if (!mac_enforce_pipe)
2789 MAC_CHECK(check_pipe_ioctl, cred, pipe, pipe->pipe_label, cmd, data);
2795 mac_check_pipe_poll(struct ucred *cred, struct pipe *pipe)
2799 PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2801 if (!mac_enforce_pipe)
2804 MAC_CHECK(check_pipe_poll, cred, pipe, pipe->pipe_label);
2810 mac_check_pipe_read(struct ucred *cred, struct pipe *pipe)
2814 PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2816 if (!mac_enforce_pipe)
2819 MAC_CHECK(check_pipe_read, cred, pipe, pipe->pipe_label);
2825 mac_check_pipe_relabel(struct ucred *cred, struct pipe *pipe,
2826 struct label *newlabel)
2830 PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2832 if (!mac_enforce_pipe)
2835 MAC_CHECK(check_pipe_relabel, cred, pipe, pipe->pipe_label, newlabel);
2841 mac_check_pipe_stat(struct ucred *cred, struct pipe *pipe)
2845 PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2847 if (!mac_enforce_pipe)
2850 MAC_CHECK(check_pipe_stat, cred, pipe, pipe->pipe_label);
2856 mac_check_pipe_write(struct ucred *cred, struct pipe *pipe)
2860 PIPE_LOCK_ASSERT(pipe, MA_OWNED);
2862 if (!mac_enforce_pipe)
2865 MAC_CHECK(check_pipe_write, cred, pipe, pipe->pipe_label);
2871 mac_check_proc_debug(struct ucred *cred, struct proc *proc)
2875 PROC_LOCK_ASSERT(proc, MA_OWNED);
2877 if (!mac_enforce_process)
2880 MAC_CHECK(check_proc_debug, cred, proc);
2886 mac_check_proc_sched(struct ucred *cred, struct proc *proc)
2890 PROC_LOCK_ASSERT(proc, MA_OWNED);
2892 if (!mac_enforce_process)
2895 MAC_CHECK(check_proc_sched, cred, proc);
2901 mac_check_proc_signal(struct ucred *cred, struct proc *proc, int signum)
2905 PROC_LOCK_ASSERT(proc, MA_OWNED);
2907 if (!mac_enforce_process)
2910 MAC_CHECK(check_proc_signal, cred, proc, signum);
2916 mac_check_socket_bind(struct ucred *ucred, struct socket *socket,
2917 struct sockaddr *sockaddr)
2921 if (!mac_enforce_socket)
2924 MAC_CHECK(check_socket_bind, ucred, socket, &socket->so_label,
2931 mac_check_socket_connect(struct ucred *cred, struct socket *socket,
2932 struct sockaddr *sockaddr)
2936 if (!mac_enforce_socket)
2939 MAC_CHECK(check_socket_connect, cred, socket, &socket->so_label,
2946 mac_check_socket_deliver(struct socket *socket, struct mbuf *mbuf)
2950 if (!mac_enforce_socket)
2953 MAC_CHECK(check_socket_deliver, socket, &socket->so_label, mbuf,
2954 &mbuf->m_pkthdr.label);
2960 mac_check_socket_listen(struct ucred *cred, struct socket *socket)
2964 if (!mac_enforce_socket)
2967 MAC_CHECK(check_socket_listen, cred, socket, &socket->so_label);
2972 mac_check_socket_receive(struct ucred *cred, struct socket *so)
2976 if (!mac_enforce_socket)
2979 MAC_CHECK(check_socket_receive, cred, so, &so->so_label);
2985 mac_check_socket_relabel(struct ucred *cred, struct socket *socket,
2986 struct label *newlabel)
2990 MAC_CHECK(check_socket_relabel, cred, socket, &socket->so_label,
2997 mac_check_socket_send(struct ucred *cred, struct socket *so)
3001 if (!mac_enforce_socket)
3004 MAC_CHECK(check_socket_send, cred, so, &so->so_label);
3010 mac_check_socket_visible(struct ucred *cred, struct socket *socket)
3014 if (!mac_enforce_socket)
3017 MAC_CHECK(check_socket_visible, cred, socket, &socket->so_label);
3023 mac_ioctl_ifnet_get(struct ucred *cred, struct ifreq *ifr,
3024 struct ifnet *ifnet)
3029 error = mac_externalize(&ifnet->if_label, &label);
3033 return (copyout(&label, ifr->ifr_ifru.ifru_data, sizeof(label)));
3037 mac_ioctl_ifnet_set(struct ucred *cred, struct ifreq *ifr,
3038 struct ifnet *ifnet)
3040 struct mac newlabel;
3041 struct label intlabel;
3044 error = copyin(ifr->ifr_ifru.ifru_data, &newlabel, sizeof(newlabel));
3048 error = mac_internalize(&intlabel, &newlabel);
3053 * XXX: Note that this is a redundant privilege check, since
3054 * policies impose this check themselves if required by the
3055 * policy. Eventually, this should go away.
3057 error = suser_cred(cred, 0);
3061 MAC_CHECK(check_ifnet_relabel, cred, ifnet, &ifnet->if_label,
3066 MAC_PERFORM(relabel_ifnet, cred, ifnet, &ifnet->if_label, &intlabel);
3069 mac_destroy_temp(&intlabel);
3074 mac_create_devfs_vnode(struct devfs_dirent *de, struct vnode *vp)
3077 MAC_PERFORM(create_devfs_vnode, de, &de->de_label, vp, &vp->v_label);
3081 mac_create_devfs_device(dev_t dev, struct devfs_dirent *de)
3084 MAC_PERFORM(create_devfs_device, dev, de, &de->de_label);
3088 mac_create_devfs_symlink(struct ucred *cred, struct devfs_dirent *dd,
3089 struct devfs_dirent *de)
3092 MAC_PERFORM(create_devfs_symlink, cred, dd, &dd->de_label, de,
3097 mac_stdcreatevnode_ea(struct vnode *vp)
3101 MAC_CHECK(stdcreatevnode_ea, vp, &vp->v_label);
3107 mac_create_devfs_directory(char *dirname, int dirnamelen,
3108 struct devfs_dirent *de)
3111 MAC_PERFORM(create_devfs_directory, dirname, dirnamelen, de,
3116 * When a new vnode is created, this call will initialize its label.
3119 mac_create_vnode(struct ucred *cred, struct vnode *parent,
3120 struct vnode *child)
3124 ASSERT_VOP_LOCKED(parent, "mac_create_vnode");
3125 ASSERT_VOP_LOCKED(child, "mac_create_vnode");
3127 error = vn_refreshlabel(parent, cred);
3129 printf("mac_create_vnode: vn_refreshlabel returned %d\n",
3131 printf("mac_create_vnode: using old vnode label\n");
3134 MAC_PERFORM(create_vnode, cred, parent, &parent->v_label, child,
3139 mac_setsockopt_label_set(struct ucred *cred, struct socket *so,
3142 struct label intlabel;
3145 error = mac_internalize(&intlabel, extmac);
3149 mac_check_socket_relabel(cred, so, &intlabel);
3151 mac_destroy_temp(&intlabel);
3155 mac_relabel_socket(cred, so, &intlabel);
3157 mac_destroy_temp(&intlabel);
3162 mac_pipe_label_set(struct ucred *cred, struct pipe *pipe, struct label *label)
3166 PIPE_LOCK_ASSERT(pipe, MA_OWNED);
3168 error = mac_check_pipe_relabel(cred, pipe, label);
3172 mac_relabel_pipe(cred, pipe, label);
3178 mac_getsockopt_label_get(struct ucred *cred, struct socket *so,
3182 return (mac_externalize(&so->so_label, extmac));
3186 mac_getsockopt_peerlabel_get(struct ucred *cred, struct socket *so,
3190 return (mac_externalize(&so->so_peerlabel, extmac));
3194 * Implementation of VOP_SETLABEL() that relies on extended attributes
3195 * to store label data. Can be referenced by filesystems supporting
3196 * extended attributes.
3199 vop_stdsetlabel_ea(struct vop_setlabel_args *ap)
3201 struct vnode *vp = ap->a_vp;
3202 struct label *intlabel = ap->a_label;
3206 ASSERT_VOP_LOCKED(vp, "vop_stdsetlabel_ea");
3209 * XXX: Eventually call out to EA check/set calls here.
3210 * Be particularly careful to avoid race conditions,
3211 * consistency problems, and stability problems when
3212 * dealing with multiple EAs. In particular, we require
3213 * the ability to write multiple EAs on the same file in
3214 * a single transaction, which the current EA interface
3218 error = mac_externalize(intlabel, &extmac);
3222 error = vn_extattr_set(vp, IO_NODELOCKED,
3223 FREEBSD_MAC_EXTATTR_NAMESPACE, FREEBSD_MAC_EXTATTR_NAME,
3224 sizeof(extmac), (char *)&extmac, curthread);
3228 mac_relabel_vnode(ap->a_cred, vp, intlabel);
3230 vp->v_vflag |= VV_CACHEDLABEL;
3236 vn_setlabel(struct vnode *vp, struct label *intlabel, struct ucred *cred)
3240 if (vp->v_mount == NULL) {
3241 /* printf("vn_setlabel: null v_mount\n"); */
3242 if (vp->v_type != VNON)
3243 printf("vn_setlabel: null v_mount with non-VNON\n");
3247 if ((vp->v_mount->mnt_flag & MNT_MULTILABEL) == 0)
3248 return (EOPNOTSUPP);
3251 * Multi-phase commit. First check the policies to confirm the
3252 * change is OK. Then commit via the filesystem. Finally,
3253 * update the actual vnode label. Question: maybe the filesystem
3254 * should update the vnode at the end as part of VOP_SETLABEL()?
3256 error = mac_check_vnode_relabel(cred, vp, intlabel);
3261 * VADMIN provides the opportunity for the filesystem to make
3262 * decisions about who is and is not able to modify labels
3263 * and protections on files. This might not be right. We can't
3264 * assume VOP_SETLABEL() will do it, because we might implement
3265 * that as part of vop_stdsetlabel_ea().
3267 error = VOP_ACCESS(vp, VADMIN, cred, curthread);
3271 error = VOP_SETLABEL(vp, intlabel, cred, curthread);
3282 __mac_get_proc(struct thread *td, struct __mac_get_proc_args *uap)
3287 error = mac_externalize(&td->td_ucred->cr_label, &extmac);
3289 error = copyout(&extmac, SCARG(uap, mac_p), sizeof(extmac));
3298 __mac_set_proc(struct thread *td, struct __mac_set_proc_args *uap)
3300 struct ucred *newcred, *oldcred;
3303 struct label intlabel;
3306 error = copyin(SCARG(uap, mac_p), &extmac, sizeof(extmac));
3310 error = mac_internalize(&intlabel, &extmac);
3318 oldcred = p->p_ucred;
3320 error = mac_check_cred_relabel(oldcred, &intlabel);
3323 mac_destroy_temp(&intlabel);
3329 crcopy(newcred, oldcred);
3330 mac_relabel_cred(newcred, &intlabel);
3331 p->p_ucred = newcred;
3334 * Grab additional reference for use while revoking mmaps, prior
3335 * to releasing the proc lock and sharing the cred.
3341 mac_cred_mmapped_drop_perms(td, newcred);
3344 crfree(newcred); /* Free revocation reference. */
3346 mac_destroy_temp(&intlabel);
3354 __mac_get_fd(struct thread *td, struct __mac_get_fd_args *uap)
3364 error = fget(td, SCARG(uap, fd), &fp);
3368 switch (fp->f_type) {
3371 vp = (struct vnode *)fp->f_data;
3373 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3374 error = vn_refreshlabel(vp, td->td_ucred);
3376 error = mac_externalize(&vp->v_label, &extmac);
3377 VOP_UNLOCK(vp, 0, td);
3380 pipe = (struct pipe *)fp->f_data;
3381 error = mac_externalize(pipe->pipe_label, &extmac);
3388 error = copyout(&extmac, SCARG(uap, mac_p), sizeof(extmac));
3401 __mac_get_file(struct thread *td, struct __mac_get_file_args *uap)
3403 struct nameidata nd;
3408 NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_USERSPACE,
3409 SCARG(uap, path_p), td);
3414 error = vn_refreshlabel(nd.ni_vp, td->td_ucred);
3416 error = mac_externalize(&nd.ni_vp->v_label, &extmac);
3421 error = copyout(&extmac, SCARG(uap, mac_p), sizeof(extmac));
3432 __mac_set_fd(struct thread *td, struct __mac_set_fd_args *uap)
3436 struct label intlabel;
3443 error = fget(td, SCARG(uap, fd), &fp);
3447 error = copyin(SCARG(uap, mac_p), &extmac, sizeof(extmac));
3451 error = mac_internalize(&intlabel, &extmac);
3455 switch (fp->f_type) {
3458 vp = (struct vnode *)fp->f_data;
3459 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3463 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3464 error = vn_setlabel(vp, &intlabel, td->td_ucred);
3465 VOP_UNLOCK(vp, 0, td);
3466 vn_finished_write(mp);
3467 mac_destroy_temp(&intlabel);
3470 pipe = (struct pipe *)fp->f_data;
3472 error = mac_pipe_label_set(td->td_ucred, pipe, &intlabel);
3490 __mac_set_file(struct thread *td, struct __mac_set_file_args *uap)
3492 struct nameidata nd;
3494 struct label intlabel;
3500 error = copyin(SCARG(uap, mac_p), &extmac, sizeof(extmac));
3504 error = mac_internalize(&intlabel, &extmac);
3508 NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_USERSPACE,
3509 SCARG(uap, path_p), td);
3513 error = vn_start_write(nd.ni_vp, &mp, V_WAIT | PCATCH);
3517 error = vn_setlabel(nd.ni_vp, &intlabel, td->td_ucred);
3519 vn_finished_write(mp);
3521 mac_destroy_temp(&intlabel);
3529 mac_syscall(struct thread *td, struct mac_syscall_args *uap)
3531 struct mac_policy_conf *mpc;
3532 char target[MAC_MAX_POLICY_NAME];
3535 error = copyinstr(SCARG(uap, policy), target, sizeof(target), NULL);
3540 MAC_POLICY_LIST_BUSY();
3541 LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {
3542 if (strcmp(mpc->mpc_name, target) == 0 &&
3543 mpc->mpc_ops->mpo_syscall != NULL) {
3544 error = mpc->mpc_ops->mpo_syscall(td,
3545 SCARG(uap, call), SCARG(uap, arg));
3551 MAC_POLICY_LIST_UNBUSY();
3555 SYSINIT(mac, SI_SUB_MAC, SI_ORDER_FIRST, mac_init, NULL);
3556 SYSINIT(mac_late, SI_SUB_MAC_LATE, SI_ORDER_FIRST, mac_late_init, NULL);
3561 __mac_get_proc(struct thread *td, struct __mac_get_proc_args *uap)
3568 __mac_set_proc(struct thread *td, struct __mac_set_proc_args *uap)
3575 __mac_get_fd(struct thread *td, struct __mac_get_fd_args *uap)
3582 __mac_get_file(struct thread *td, struct __mac_get_file_args *uap)
3589 __mac_set_fd(struct thread *td, struct __mac_set_fd_args *uap)
3596 __mac_set_file(struct thread *td, struct __mac_set_file_args *uap)
3603 mac_syscall(struct thread *td, struct mac_syscall_args *uap)