diff options
-rw-r--r-- | sys/kern/kern_mac.c | 2761 | ||||
-rw-r--r-- | sys/security/mac/mac_framework.c | 2761 | ||||
-rw-r--r-- | sys/security/mac/mac_syscalls.c | 2761 |
3 files changed, 15 insertions, 8268 deletions
diff --git a/sys/kern/kern_mac.c b/sys/kern/kern_mac.c index 35c9c9b..aae8a24 100644 --- a/sys/kern/kern_mac.c +++ b/sys/kern/kern_mac.c @@ -85,6 +85,8 @@ __FBSDID("$FreeBSD$"); #include <netinet/in.h> #include <netinet/ip_var.h> +#include <security/mac/mac_internal.h> + #ifdef MAC /* @@ -94,8 +96,6 @@ __FBSDID("$FreeBSD$"); */ MODULE_VERSION(kernel_mac_support, 1); -SYSCTL_DECL(_security); - SYSCTL_NODE(_security, OID_AUTO, mac, CTLFLAG_RW, 0, "TrustedBSD MAC policy controls"); @@ -130,119 +130,20 @@ int mac_late = 0; int mac_labelmbufs = 0; #endif -static int mac_enforce_fs = 1; -SYSCTL_INT(_security_mac, OID_AUTO, enforce_fs, CTLFLAG_RW, - &mac_enforce_fs, 0, "Enforce MAC policy on file system objects"); -TUNABLE_INT("security.mac.enforce_fs", &mac_enforce_fs); - -static int mac_enforce_kld = 1; -SYSCTL_INT(_security_mac, OID_AUTO, enforce_kld, CTLFLAG_RW, - &mac_enforce_kld, 0, "Enforce MAC policy on kld operations"); -TUNABLE_INT("security.mac.enforce_kld", &mac_enforce_kld); - -static int mac_enforce_network = 1; -SYSCTL_INT(_security_mac, OID_AUTO, enforce_network, CTLFLAG_RW, - &mac_enforce_network, 0, "Enforce MAC policy on network packets"); -TUNABLE_INT("security.mac.enforce_network", &mac_enforce_network); - -static int mac_enforce_pipe = 1; -SYSCTL_INT(_security_mac, OID_AUTO, enforce_pipe, CTLFLAG_RW, - &mac_enforce_pipe, 0, "Enforce MAC policy on pipe operations"); -TUNABLE_INT("security.mac.enforce_pipe", &mac_enforce_pipe); - -static int mac_enforce_process = 1; -SYSCTL_INT(_security_mac, OID_AUTO, enforce_process, CTLFLAG_RW, - &mac_enforce_process, 0, "Enforce MAC policy on inter-process operations"); -TUNABLE_INT("security.mac.enforce_process", &mac_enforce_process); - -static int mac_enforce_socket = 1; -SYSCTL_INT(_security_mac, OID_AUTO, enforce_socket, CTLFLAG_RW, - &mac_enforce_socket, 0, "Enforce MAC policy on socket operations"); -TUNABLE_INT("security.mac.enforce_socket", &mac_enforce_socket); - -static int mac_enforce_system = 1; -SYSCTL_INT(_security_mac, OID_AUTO, enforce_system, CTLFLAG_RW, - &mac_enforce_system, 0, "Enforce MAC policy on system operations"); -TUNABLE_INT("security.mac.enforce_system", &mac_enforce_system); - -static int mac_enforce_vm = 1; -SYSCTL_INT(_security_mac, OID_AUTO, enforce_vm, CTLFLAG_RW, - &mac_enforce_vm, 0, "Enforce MAC policy on vm operations"); -TUNABLE_INT("security.mac.enforce_vm", &mac_enforce_vm); - -static int mac_mmap_revocation = 1; -SYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation, CTLFLAG_RW, - &mac_mmap_revocation, 0, "Revoke mmap access to files on subject " - "relabel"); -static int mac_mmap_revocation_via_cow = 0; -SYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation_via_cow, CTLFLAG_RW, - &mac_mmap_revocation_via_cow, 0, "Revoke mmap access to files via " - "copy-on-write semantics, or by removing all write access"); - #ifdef MAC_DEBUG SYSCTL_NODE(_security_mac, OID_AUTO, debug, CTLFLAG_RW, 0, "TrustedBSD MAC debug info"); - -static int mac_debug_label_fallback = 0; -SYSCTL_INT(_security_mac_debug, OID_AUTO, label_fallback, CTLFLAG_RW, - &mac_debug_label_fallback, 0, "Filesystems should fall back to fs label" - "when label is corrupted."); -TUNABLE_INT("security.mac.debug_label_fallback", - &mac_debug_label_fallback); - SYSCTL_NODE(_security_mac_debug, OID_AUTO, counters, CTLFLAG_RW, 0, "TrustedBSD MAC object counters"); -static unsigned int nmacmbufs, nmaccreds, nmacifnets, nmacbpfdescs, - nmacsockets, nmacmounts, nmactemp, nmacvnodes, nmacdevfsdirents, - nmacipqs, nmacpipes, nmacprocs; - -#define MAC_DEBUG_COUNTER_INC(x) atomic_add_int(x, 1); -#define MAC_DEBUG_COUNTER_DEC(x) atomic_subtract_int(x, 1); - -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, mbufs, CTLFLAG_RD, - &nmacmbufs, 0, "number of mbufs in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, creds, CTLFLAG_RD, - &nmaccreds, 0, "number of ucreds in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, ifnets, CTLFLAG_RD, - &nmacifnets, 0, "number of ifnets in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, ipqs, CTLFLAG_RD, - &nmacipqs, 0, "number of ipqs in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, bpfdescs, CTLFLAG_RD, - &nmacbpfdescs, 0, "number of bpfdescs in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, sockets, CTLFLAG_RD, - &nmacsockets, 0, "number of sockets in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, pipes, CTLFLAG_RD, - &nmacpipes, 0, "number of pipes in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, procs, CTLFLAG_RD, - &nmacprocs, 0, "number of procs in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, mounts, CTLFLAG_RD, - &nmacmounts, 0, "number of mounts in use"); +static unsigned int nmactemp; SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, temp, CTLFLAG_RD, &nmactemp, 0, "number of temporary labels in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, vnodes, CTLFLAG_RD, - &nmacvnodes, 0, "number of vnodes in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, devfsdirents, CTLFLAG_RD, - &nmacdevfsdirents, 0, "number of devfs dirents inuse"); -#else -#define MAC_DEBUG_COUNTER_INC(x) -#define MAC_DEBUG_COUNTER_DEC(x) #endif static int mac_policy_register(struct mac_policy_conf *mpc); static int mac_policy_unregister(struct mac_policy_conf *mpc); -static void mac_check_vnode_mmap_downgrade(struct ucred *cred, - struct vnode *vp, int *prot); -static void mac_cred_mmapped_drop_perms_recurse(struct thread *td, - struct ucred *cred, struct vm_map *map); - -static void mac_destroy_socket_label(struct label *label); - -static int mac_setlabel_vnode_extattr(struct ucred *cred, - struct vnode *vp, struct label *intlabel); - -MALLOC_DEFINE(M_MACPIPELABEL, "macpipelabel", "MAC labels for pipes"); MALLOC_DEFINE(M_MACTEMP, "mactemp", "MAC temporary label storage"); /* @@ -341,151 +242,6 @@ mac_policy_list_unbusy(void) } /* - * MAC_CHECK performs the designated check by walking the policy - * module list and checking with each as to how it feels about the - * request. Note that it returns its value via 'error' in the scope - * of the caller. - */ -#define MAC_CHECK(check, args...) do { \ - struct mac_policy_conf *mpc; \ - int entrycount; \ - \ - error = 0; \ - LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ - if (mpc->mpc_ops->mpo_ ## check != NULL) \ - error = mac_error_select( \ - mpc->mpc_ops->mpo_ ## check (args), \ - error); \ - } \ - if ((entrycount = mac_policy_list_conditional_busy()) != 0) { \ - LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ - if (mpc->mpc_ops->mpo_ ## check != NULL) \ - error = mac_error_select( \ - mpc->mpc_ops->mpo_ ## check (args), \ - error); \ - } \ - mac_policy_list_unbusy(); \ - } \ -} while (0) - -/* - * MAC_BOOLEAN performs the designated boolean composition by walking - * the module list, invoking each instance of the operation, and - * combining the results using the passed C operator. Note that it - * returns its value via 'result' in the scope of the caller, which - * should be initialized by the caller in a meaningful way to get - * a meaningful result. - */ -#define MAC_BOOLEAN(operation, composition, args...) do { \ - struct mac_policy_conf *mpc; \ - int entrycount; \ - \ - LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ - if (mpc->mpc_ops->mpo_ ## operation != NULL) \ - result = result composition \ - mpc->mpc_ops->mpo_ ## operation (args); \ - } \ - if ((entrycount = mac_policy_list_conditional_busy()) != 0) { \ - LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ - if (mpc->mpc_ops->mpo_ ## operation != NULL) \ - result = result composition \ - mpc->mpc_ops->mpo_ ## operation \ - (args); \ - } \ - mac_policy_list_unbusy(); \ - } \ -} while (0) - -#define MAC_EXTERNALIZE(type, label, elementlist, outbuf, \ - outbuflen) do { \ - int claimed, first, ignorenotfound, savedlen; \ - char *element_name, *element_temp; \ - struct sbuf sb; \ - \ - error = 0; \ - first = 1; \ - sbuf_new(&sb, outbuf, outbuflen, SBUF_FIXEDLEN); \ - element_temp = elementlist; \ - while ((element_name = strsep(&element_temp, ",")) != NULL) { \ - if (element_name[0] == '?') { \ - element_name++; \ - ignorenotfound = 1; \ - } else \ - ignorenotfound = 0; \ - savedlen = sbuf_len(&sb); \ - if (first) { \ - error = sbuf_printf(&sb, "%s/", element_name); \ - first = 0; \ - } else \ - error = sbuf_printf(&sb, ",%s/", element_name); \ - if (error == -1) { \ - error = EINVAL; /* XXX: E2BIG? */ \ - break; \ - } \ - claimed = 0; \ - MAC_CHECK(externalize_ ## type, label, element_name, \ - &sb, &claimed); \ - if (error) \ - break; \ - if (claimed == 0 && ignorenotfound) { \ - /* Revert last label name. */ \ - sbuf_setpos(&sb, savedlen); \ - } else if (claimed != 1) { \ - error = EINVAL; /* XXX: ENOLABEL? */ \ - break; \ - } \ - } \ - sbuf_finish(&sb); \ -} while (0) - -#define MAC_INTERNALIZE(type, label, instring) do { \ - char *element, *element_name, *element_data; \ - int claimed; \ - \ - error = 0; \ - element = instring; \ - while ((element_name = strsep(&element, ",")) != NULL) { \ - element_data = element_name; \ - element_name = strsep(&element_data, "/"); \ - if (element_data == NULL) { \ - error = EINVAL; \ - break; \ - } \ - claimed = 0; \ - MAC_CHECK(internalize_ ## type, label, element_name, \ - element_data, &claimed); \ - if (error) \ - break; \ - if (claimed != 1) { \ - /* XXXMAC: Another error here? */ \ - error = EINVAL; \ - break; \ - } \ - } \ -} while (0) - -/* - * MAC_PERFORM performs the designated operation by walking the policy - * module list and invoking that operation for each policy. - */ -#define MAC_PERFORM(operation, args...) do { \ - struct mac_policy_conf *mpc; \ - int entrycount; \ - \ - LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ - if (mpc->mpc_ops->mpo_ ## operation != NULL) \ - mpc->mpc_ops->mpo_ ## operation (args); \ - } \ - if ((entrycount = mac_policy_list_conditional_busy()) != 0) { \ - LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ - if (mpc->mpc_ops->mpo_ ## operation != NULL) \ - mpc->mpc_ops->mpo_ ## operation (args); \ - } \ - mac_policy_list_unbusy(); \ - } \ -} while (0) - -/* * Initialize the MAC subsystem, including appropriate SMP locks. */ static void @@ -735,19 +491,7 @@ mac_error_select(int error1, int error2) return (error2); } -static struct label * -mbuf_to_label(struct mbuf *mbuf) -{ - struct m_tag *tag; - struct label *label; - - tag = m_tag_find(mbuf, PACKET_TAG_MACLABEL, NULL); - label = (struct label *)(tag+1); - - return (label); -} - -static void +void mac_init_label(struct label *label) { @@ -755,7 +499,7 @@ mac_init_label(struct label *label) label->l_flags = MAC_FLAG_INITIALIZED; } -static void +void mac_destroy_label(struct label *label) { @@ -766,406 +510,7 @@ mac_destroy_label(struct label *label) /* implicit: label->l_flags &= ~MAC_FLAG_INITIALIZED; */ } -void -mac_init_bpfdesc(struct bpf_d *bpf_d) -{ - - mac_init_label(&bpf_d->bd_label); - MAC_PERFORM(init_bpfdesc_label, &bpf_d->bd_label); - MAC_DEBUG_COUNTER_INC(&nmacbpfdescs); -} - -static void -mac_init_cred_label(struct label *label) -{ - - mac_init_label(label); - MAC_PERFORM(init_cred_label, label); - MAC_DEBUG_COUNTER_INC(&nmaccreds); -} - -void -mac_init_cred(struct ucred *cred) -{ - - mac_init_cred_label(&cred->cr_label); -} - -void -mac_init_devfsdirent(struct devfs_dirent *de) -{ - - mac_init_label(&de->de_label); - MAC_PERFORM(init_devfsdirent_label, &de->de_label); - MAC_DEBUG_COUNTER_INC(&nmacdevfsdirents); -} - -static void -mac_init_ifnet_label(struct label *label) -{ - - mac_init_label(label); - MAC_PERFORM(init_ifnet_label, label); - MAC_DEBUG_COUNTER_INC(&nmacifnets); -} - -void -mac_init_ifnet(struct ifnet *ifp) -{ - - mac_init_ifnet_label(&ifp->if_label); -} - -int -mac_init_ipq(struct ipq *ipq, int flag) -{ - int error; - - mac_init_label(&ipq->ipq_label); - - MAC_CHECK(init_ipq_label, &ipq->ipq_label, flag); - if (error) { - MAC_PERFORM(destroy_ipq_label, &ipq->ipq_label); - mac_destroy_label(&ipq->ipq_label); - } else { - MAC_DEBUG_COUNTER_INC(&nmacipqs); - } - return (error); -} - -int -mac_init_mbuf_tag(struct m_tag *tag, int flag) -{ - struct label *label; - int error; - - label = (struct label *) (tag + 1); - mac_init_label(label); - - MAC_CHECK(init_mbuf_label, label, flag); - if (error) { - MAC_PERFORM(destroy_mbuf_label, label); - mac_destroy_label(label); - } else { - MAC_DEBUG_COUNTER_INC(&nmacmbufs); - } - return (error); -} - -int -mac_init_mbuf(struct mbuf *m, int flag) -{ - struct m_tag *tag; - int error; - - M_ASSERTPKTHDR(m); - -#ifndef MAC_ALWAYS_LABEL_MBUF - /* - * If conditionally allocating mbuf labels, don't allocate unless - * they are required. - */ - if (!mac_labelmbufs) - return (0); -#endif - tag = m_tag_get(PACKET_TAG_MACLABEL, sizeof(struct label), - flag); - if (tag == NULL) - return (ENOMEM); - error = mac_init_mbuf_tag(tag, flag); - if (error) { - m_tag_free(tag); - return (error); - } - m_tag_prepend(m, tag); - return (0); -} - -void -mac_init_mount(struct mount *mp) -{ - - mac_init_label(&mp->mnt_mntlabel); - mac_init_label(&mp->mnt_fslabel); - MAC_PERFORM(init_mount_label, &mp->mnt_mntlabel); - MAC_PERFORM(init_mount_fs_label, &mp->mnt_fslabel); - MAC_DEBUG_COUNTER_INC(&nmacmounts); -} - -static void -mac_init_pipe_label(struct label *label) -{ - - mac_init_label(label); - MAC_PERFORM(init_pipe_label, label); - MAC_DEBUG_COUNTER_INC(&nmacpipes); -} - -void -mac_init_pipe(struct pipe *pipe) -{ - struct label *label; - - label = malloc(sizeof(struct label), M_MACPIPELABEL, M_ZERO|M_WAITOK); - pipe->pipe_label = label; - pipe->pipe_peer->pipe_label = label; - mac_init_pipe_label(label); -} - -void -mac_init_proc(struct proc *p) -{ - - mac_init_label(&p->p_label); - MAC_PERFORM(init_proc_label, &p->p_label); - MAC_DEBUG_COUNTER_INC(&nmacprocs); -} - -static int -mac_init_socket_label(struct label *label, int flag) -{ - int error; - - mac_init_label(label); - - MAC_CHECK(init_socket_label, label, flag); - if (error) { - MAC_PERFORM(destroy_socket_label, label); - mac_destroy_label(label); - } else { - MAC_DEBUG_COUNTER_INC(&nmacsockets); - } - - return (error); -} - -static int -mac_init_socket_peer_label(struct label *label, int flag) -{ - int error; - - mac_init_label(label); - - MAC_CHECK(init_socket_peer_label, label, flag); - if (error) { - MAC_PERFORM(destroy_socket_label, label); - mac_destroy_label(label); - } - - return (error); -} - int -mac_init_socket(struct socket *socket, int flag) -{ - int error; - - error = mac_init_socket_label(&socket->so_label, flag); - if (error) - return (error); - - error = mac_init_socket_peer_label(&socket->so_peerlabel, flag); - if (error) - mac_destroy_socket_label(&socket->so_label); - - return (error); -} - -void -mac_init_vnode_label(struct label *label) -{ - - mac_init_label(label); - MAC_PERFORM(init_vnode_label, label); - MAC_DEBUG_COUNTER_INC(&nmacvnodes); -} - -void -mac_init_vnode(struct vnode *vp) -{ - - mac_init_vnode_label(&vp->v_label); -} - -void -mac_destroy_bpfdesc(struct bpf_d *bpf_d) -{ - - MAC_PERFORM(destroy_bpfdesc_label, &bpf_d->bd_label); - mac_destroy_label(&bpf_d->bd_label); - MAC_DEBUG_COUNTER_DEC(&nmacbpfdescs); -} - -static void -mac_destroy_cred_label(struct label *label) -{ - - MAC_PERFORM(destroy_cred_label, label); - mac_destroy_label(label); - MAC_DEBUG_COUNTER_DEC(&nmaccreds); -} - -void -mac_destroy_cred(struct ucred *cred) -{ - - mac_destroy_cred_label(&cred->cr_label); -} - -void -mac_destroy_devfsdirent(struct devfs_dirent *de) -{ - - MAC_PERFORM(destroy_devfsdirent_label, &de->de_label); - mac_destroy_label(&de->de_label); - MAC_DEBUG_COUNTER_DEC(&nmacdevfsdirents); -} - -static void -mac_destroy_ifnet_label(struct label *label) -{ - - MAC_PERFORM(destroy_ifnet_label, label); - mac_destroy_label(label); - MAC_DEBUG_COUNTER_DEC(&nmacifnets); -} - -void -mac_destroy_ifnet(struct ifnet *ifp) -{ - - mac_destroy_ifnet_label(&ifp->if_label); -} - -void -mac_destroy_ipq(struct ipq *ipq) -{ - - MAC_PERFORM(destroy_ipq_label, &ipq->ipq_label); - mac_destroy_label(&ipq->ipq_label); - MAC_DEBUG_COUNTER_DEC(&nmacipqs); -} - -void -mac_destroy_mbuf_tag(struct m_tag *tag) -{ - struct label *label; - - label = (struct label *)(tag+1); - - MAC_PERFORM(destroy_mbuf_label, label); - mac_destroy_label(label); - MAC_DEBUG_COUNTER_DEC(&nmacmbufs); -} - -void -mac_destroy_mount(struct mount *mp) -{ - - MAC_PERFORM(destroy_mount_label, &mp->mnt_mntlabel); - MAC_PERFORM(destroy_mount_fs_label, &mp->mnt_fslabel); - mac_destroy_label(&mp->mnt_fslabel); - mac_destroy_label(&mp->mnt_mntlabel); - MAC_DEBUG_COUNTER_DEC(&nmacmounts); -} - -static void -mac_destroy_pipe_label(struct label *label) -{ - - MAC_PERFORM(destroy_pipe_label, label); - mac_destroy_label(label); - MAC_DEBUG_COUNTER_DEC(&nmacpipes); -} - -void -mac_destroy_pipe(struct pipe *pipe) -{ - - mac_destroy_pipe_label(pipe->pipe_label); - free(pipe->pipe_label, M_MACPIPELABEL); -} - -void -mac_destroy_proc(struct proc *p) -{ - - MAC_PERFORM(destroy_proc_label, &p->p_label); - mac_destroy_label(&p->p_label); - MAC_DEBUG_COUNTER_DEC(&nmacprocs); -} - -static void -mac_destroy_socket_label(struct label *label) -{ - - MAC_PERFORM(destroy_socket_label, label); - mac_destroy_label(label); - MAC_DEBUG_COUNTER_DEC(&nmacsockets); -} - -static void -mac_destroy_socket_peer_label(struct label *label) -{ - - MAC_PERFORM(destroy_socket_peer_label, label); - mac_destroy_label(label); -} - -void -mac_destroy_socket(struct socket *socket) -{ - - mac_destroy_socket_label(&socket->so_label); - mac_destroy_socket_peer_label(&socket->so_peerlabel); -} - -void -mac_destroy_vnode_label(struct label *label) -{ - - MAC_PERFORM(destroy_vnode_label, label); - mac_destroy_label(label); - MAC_DEBUG_COUNTER_DEC(&nmacvnodes); -} - -void -mac_destroy_vnode(struct vnode *vp) -{ - - mac_destroy_vnode_label(&vp->v_label); -} - -void -mac_copy_mbuf_tag(struct m_tag *src, struct m_tag *dest) -{ - struct label *src_label, *dest_label; - - src_label = (struct label *)(src+1); - dest_label = (struct label *)(dest+1); - - /* - * mac_init_mbuf_tag() is called on the target tag in - * m_tag_copy(), so we don't need to call it here. - */ - MAC_PERFORM(copy_mbuf_label, src_label, dest_label); -} - -static void -mac_copy_pipe_label(struct label *src, struct label *dest) -{ - - MAC_PERFORM(copy_pipe_label, src, dest); -} - -void -mac_copy_vnode_label(struct label *src, struct label *dest) -{ - - MAC_PERFORM(copy_vnode_label, src, dest); -} - -static int mac_check_structmac_consistent(struct mac *mac) { @@ -1176,2102 +521,6 @@ mac_check_structmac_consistent(struct mac *mac) return (0); } -static int -mac_externalize_cred_label(struct label *label, char *elements, - char *outbuf, size_t outbuflen, int flags) -{ - int error; - - MAC_EXTERNALIZE(cred_label, label, elements, outbuf, outbuflen); - - return (error); -} - -static int -mac_externalize_ifnet_label(struct label *label, char *elements, - char *outbuf, size_t outbuflen, int flags) -{ - int error; - - MAC_EXTERNALIZE(ifnet_label, label, elements, outbuf, outbuflen); - - return (error); -} - -static int -mac_externalize_pipe_label(struct label *label, char *elements, - char *outbuf, size_t outbuflen, int flags) -{ - int error; - - MAC_EXTERNALIZE(pipe_label, label, elements, outbuf, outbuflen); - - return (error); -} - -static int -mac_externalize_socket_label(struct label *label, char *elements, - char *outbuf, size_t outbuflen, int flags) -{ - int error; - - MAC_EXTERNALIZE(socket_label, label, elements, outbuf, outbuflen); - - return (error); -} - -static int -mac_externalize_socket_peer_label(struct label *label, char *elements, - char *outbuf, size_t outbuflen, int flags) -{ - int error; - - MAC_EXTERNALIZE(socket_peer_label, label, elements, outbuf, outbuflen); - - return (error); -} - -static int -mac_externalize_vnode_label(struct label *label, char *elements, - char *outbuf, size_t outbuflen, int flags) -{ - int error; - - MAC_EXTERNALIZE(vnode_label, label, elements, outbuf, outbuflen); - - return (error); -} - -static int -mac_internalize_cred_label(struct label *label, char *string) -{ - int error; - - MAC_INTERNALIZE(cred_label, label, string); - - return (error); -} - -static int -mac_internalize_ifnet_label(struct label *label, char *string) -{ - int error; - - MAC_INTERNALIZE(ifnet_label, label, string); - - return (error); -} - -static int -mac_internalize_pipe_label(struct label *label, char *string) -{ - int error; - - MAC_INTERNALIZE(pipe_label, label, string); - - return (error); -} - -static int -mac_internalize_socket_label(struct label *label, char *string) -{ - int error; - - MAC_INTERNALIZE(socket_label, label, string); - - return (error); -} - -static int -mac_internalize_vnode_label(struct label *label, char *string) -{ - int error; - - MAC_INTERNALIZE(vnode_label, label, string); - - return (error); -} - -/* - * Initialize MAC label for the first kernel process, from which other - * kernel processes and threads are spawned. - */ -void -mac_create_proc0(struct ucred *cred) -{ - - MAC_PERFORM(create_proc0, cred); -} - -/* - * Initialize MAC label for the first userland process, from which other - * userland processes and threads are spawned. - */ -void -mac_create_proc1(struct ucred *cred) -{ - - MAC_PERFORM(create_proc1, cred); -} - -void -mac_thread_userret(struct thread *td) -{ - - MAC_PERFORM(thread_userret, td); -} - -/* - * When a new process is created, its label must be initialized. Generally, - * this involves inheritence from the parent process, modulo possible - * deltas. This function allows that processing to take place. - */ -void -mac_create_cred(struct ucred *parent_cred, struct ucred *child_cred) -{ - - MAC_PERFORM(create_cred, parent_cred, child_cred); -} - -void -mac_update_devfsdirent(struct mount *mp, struct devfs_dirent *de, - struct vnode *vp) -{ - - MAC_PERFORM(update_devfsdirent, mp, de, &de->de_label, vp, - &vp->v_label); -} - -void -mac_associate_vnode_devfs(struct mount *mp, struct devfs_dirent *de, - struct vnode *vp) -{ - - MAC_PERFORM(associate_vnode_devfs, mp, &mp->mnt_fslabel, de, - &de->de_label, vp, &vp->v_label); -} - -int -mac_associate_vnode_extattr(struct mount *mp, struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_associate_vnode_extattr"); - - MAC_CHECK(associate_vnode_extattr, mp, &mp->mnt_fslabel, vp, - &vp->v_label); - - return (error); -} - -void -mac_associate_vnode_singlelabel(struct mount *mp, struct vnode *vp) -{ - - MAC_PERFORM(associate_vnode_singlelabel, mp, &mp->mnt_fslabel, vp, - &vp->v_label); -} - -int -mac_create_vnode_extattr(struct ucred *cred, struct mount *mp, - struct vnode *dvp, struct vnode *vp, struct componentname *cnp) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_create_vnode_extattr"); - ASSERT_VOP_LOCKED(vp, "mac_create_vnode_extattr"); - - error = VOP_OPENEXTATTR(vp, cred, curthread); - if (error == EOPNOTSUPP) { - /* XXX: Optionally abort if transactions not supported. */ - if (ea_warn_once == 0) { - printf("Warning: transactions not supported " - "in EA write.\n"); - ea_warn_once = 1; - } - } else if (error) - return (error); - - MAC_CHECK(create_vnode_extattr, cred, mp, &mp->mnt_fslabel, - dvp, &dvp->v_label, vp, &vp->v_label, cnp); - - if (error) { - VOP_CLOSEEXTATTR(vp, 0, NOCRED, curthread); - return (error); - } - - error = VOP_CLOSEEXTATTR(vp, 1, NOCRED, curthread); - - if (error == EOPNOTSUPP) - error = 0; /* XXX */ - - return (error); -} - -static int -mac_setlabel_vnode_extattr(struct ucred *cred, struct vnode *vp, - struct label *intlabel) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_setlabel_vnode_extattr"); - - error = VOP_OPENEXTATTR(vp, cred, curthread); - if (error == EOPNOTSUPP) { - /* XXX: Optionally abort if transactions not supported. */ - if (ea_warn_once == 0) { - printf("Warning: transactions not supported " - "in EA write.\n"); - ea_warn_once = 1; - } - } else if (error) - return (error); - - MAC_CHECK(setlabel_vnode_extattr, cred, vp, &vp->v_label, intlabel); - - if (error) { - VOP_CLOSEEXTATTR(vp, 0, NOCRED, curthread); - return (error); - } - - error = VOP_CLOSEEXTATTR(vp, 1, NOCRED, curthread); - - if (error == EOPNOTSUPP) - error = 0; /* XXX */ - - return (error); -} - -int -mac_execve_enter(struct image_params *imgp, struct mac *mac_p, - struct label *execlabelstorage) -{ - struct mac mac; - char *buffer; - int error; - - if (mac_p == NULL) - return (0); - - error = copyin(mac_p, &mac, sizeof(mac)); - if (error) - return (error); - - error = mac_check_structmac_consistent(&mac); - if (error) - return (error); - - buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK); - error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL); - if (error) { - free(buffer, M_MACTEMP); - return (error); - } - - mac_init_cred_label(execlabelstorage); - error = mac_internalize_cred_label(execlabelstorage, buffer); - free(buffer, M_MACTEMP); - if (error) { - mac_destroy_cred_label(execlabelstorage); - return (error); - } - imgp->execlabel = execlabelstorage; - return (0); -} - -void -mac_execve_exit(struct image_params *imgp) -{ - if (imgp->execlabel != NULL) - mac_destroy_cred_label(imgp->execlabel); -} - -void -mac_execve_transition(struct ucred *old, struct ucred *new, struct vnode *vp, - struct label *interpvnodelabel, struct image_params *imgp) -{ - - ASSERT_VOP_LOCKED(vp, "mac_execve_transition"); - - if (!mac_enforce_process && !mac_enforce_fs) - return; - - MAC_PERFORM(execve_transition, old, new, vp, &vp->v_label, - interpvnodelabel, imgp, imgp->execlabel); -} - -int -mac_execve_will_transition(struct ucred *old, struct vnode *vp, - struct label *interpvnodelabel, struct image_params *imgp) -{ - int result; - - ASSERT_VOP_LOCKED(vp, "mac_execve_will_transition"); - - if (!mac_enforce_process && !mac_enforce_fs) - return (0); - - result = 0; - MAC_BOOLEAN(execve_will_transition, ||, old, vp, &vp->v_label, - interpvnodelabel, imgp, imgp->execlabel); - - return (result); -} - -int -mac_check_vnode_access(struct ucred *cred, struct vnode *vp, int acc_mode) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_access"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_access, cred, vp, &vp->v_label, acc_mode); - return (error); -} - -int -mac_check_vnode_chdir(struct ucred *cred, struct vnode *dvp) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_chdir"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_chdir, cred, dvp, &dvp->v_label); - return (error); -} - -int -mac_check_vnode_chroot(struct ucred *cred, struct vnode *dvp) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_chroot"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_chroot, cred, dvp, &dvp->v_label); - return (error); -} - -int -mac_check_vnode_create(struct ucred *cred, struct vnode *dvp, - struct componentname *cnp, struct vattr *vap) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_create"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_create, cred, dvp, &dvp->v_label, cnp, vap); - return (error); -} - -int -mac_check_vnode_delete(struct ucred *cred, struct vnode *dvp, struct vnode *vp, - struct componentname *cnp) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_delete"); - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_delete"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_delete, cred, dvp, &dvp->v_label, vp, - &vp->v_label, cnp); - return (error); -} - -int -mac_check_vnode_deleteacl(struct ucred *cred, struct vnode *vp, - acl_type_t type) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_deleteacl"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_deleteacl, cred, vp, &vp->v_label, type); - return (error); -} - -int -mac_check_vnode_deleteextattr(struct ucred *cred, struct vnode *vp, - int attrnamespace, const char *name) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_deleteextattr"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_deleteextattr, cred, vp, &vp->v_label, - attrnamespace, name); - return (error); -} - -int -mac_check_vnode_exec(struct ucred *cred, struct vnode *vp, - struct image_params *imgp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_exec"); - - if (!mac_enforce_process && !mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_exec, cred, vp, &vp->v_label, imgp, - imgp->execlabel); - - return (error); -} - -int -mac_check_vnode_getacl(struct ucred *cred, struct vnode *vp, acl_type_t type) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_getacl"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_getacl, cred, vp, &vp->v_label, type); - return (error); -} - -int -mac_check_vnode_getextattr(struct ucred *cred, struct vnode *vp, - int attrnamespace, const char *name, struct uio *uio) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_getextattr"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_getextattr, cred, vp, &vp->v_label, - attrnamespace, name, uio); - return (error); -} - -int -mac_check_vnode_link(struct ucred *cred, struct vnode *dvp, - struct vnode *vp, struct componentname *cnp) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_link"); - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_link"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_link, cred, dvp, &dvp->v_label, vp, - &vp->v_label, cnp); - return (error); -} - -int -mac_check_vnode_listextattr(struct ucred *cred, struct vnode *vp, - int attrnamespace) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_listextattr"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_listextattr, cred, vp, &vp->v_label, - attrnamespace); - return (error); -} - -int -mac_check_vnode_lookup(struct ucred *cred, struct vnode *dvp, - struct componentname *cnp) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_lookup"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_lookup, cred, dvp, &dvp->v_label, cnp); - return (error); -} - -int -mac_check_vnode_mmap(struct ucred *cred, struct vnode *vp, int prot) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mmap"); - - if (!mac_enforce_fs || !mac_enforce_vm) - return (0); - - MAC_CHECK(check_vnode_mmap, cred, vp, &vp->v_label, prot); - return (error); -} - -void -mac_check_vnode_mmap_downgrade(struct ucred *cred, struct vnode *vp, int *prot) -{ - int result = *prot; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mmap_downgrade"); - - if (!mac_enforce_fs || !mac_enforce_vm) - return; - - MAC_PERFORM(check_vnode_mmap_downgrade, cred, vp, &vp->v_label, - &result); - - *prot = result; -} - -int -mac_check_vnode_mprotect(struct ucred *cred, struct vnode *vp, int prot) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mprotect"); - - if (!mac_enforce_fs || !mac_enforce_vm) - return (0); - - MAC_CHECK(check_vnode_mprotect, cred, vp, &vp->v_label, prot); - return (error); -} - -int -mac_check_vnode_open(struct ucred *cred, struct vnode *vp, int acc_mode) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_open"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_open, cred, vp, &vp->v_label, acc_mode); - return (error); -} - -int -mac_check_vnode_poll(struct ucred *active_cred, struct ucred *file_cred, - struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_poll"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_poll, active_cred, file_cred, vp, - &vp->v_label); - - return (error); -} - -int -mac_check_vnode_read(struct ucred *active_cred, struct ucred *file_cred, - struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_read"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_read, active_cred, file_cred, vp, - &vp->v_label); - - return (error); -} - -int -mac_check_vnode_readdir(struct ucred *cred, struct vnode *dvp) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_readdir"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_readdir, cred, dvp, &dvp->v_label); - return (error); -} - -int -mac_check_vnode_readlink(struct ucred *cred, struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_readlink"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_readlink, cred, vp, &vp->v_label); - return (error); -} - -static int -mac_check_vnode_relabel(struct ucred *cred, struct vnode *vp, - struct label *newlabel) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_relabel"); - - MAC_CHECK(check_vnode_relabel, cred, vp, &vp->v_label, newlabel); - - return (error); -} - -int -mac_check_vnode_rename_from(struct ucred *cred, struct vnode *dvp, - struct vnode *vp, struct componentname *cnp) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_rename_from"); - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_rename_from"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_rename_from, cred, dvp, &dvp->v_label, vp, - &vp->v_label, cnp); - return (error); -} - -int -mac_check_vnode_rename_to(struct ucred *cred, struct vnode *dvp, - struct vnode *vp, int samedir, struct componentname *cnp) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_rename_to"); - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_rename_to"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_rename_to, cred, dvp, &dvp->v_label, vp, - vp != NULL ? &vp->v_label : NULL, samedir, cnp); - return (error); -} - -int -mac_check_vnode_revoke(struct ucred *cred, struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_revoke"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_revoke, cred, vp, &vp->v_label); - return (error); -} - -int -mac_check_vnode_setacl(struct ucred *cred, struct vnode *vp, acl_type_t type, - struct acl *acl) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setacl"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_setacl, cred, vp, &vp->v_label, type, acl); - return (error); -} - -int -mac_check_vnode_setextattr(struct ucred *cred, struct vnode *vp, - int attrnamespace, const char *name, struct uio *uio) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setextattr"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_setextattr, cred, vp, &vp->v_label, - attrnamespace, name, uio); - return (error); -} - -int -mac_check_vnode_setflags(struct ucred *cred, struct vnode *vp, u_long flags) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setflags"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_setflags, cred, vp, &vp->v_label, flags); - return (error); -} - -int -mac_check_vnode_setmode(struct ucred *cred, struct vnode *vp, mode_t mode) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setmode"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_setmode, cred, vp, &vp->v_label, mode); - return (error); -} - -int -mac_check_vnode_setowner(struct ucred *cred, struct vnode *vp, uid_t uid, - gid_t gid) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setowner"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_setowner, cred, vp, &vp->v_label, uid, gid); - return (error); -} - -int -mac_check_vnode_setutimes(struct ucred *cred, struct vnode *vp, - struct timespec atime, struct timespec mtime) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setutimes"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_setutimes, cred, vp, &vp->v_label, atime, - mtime); - return (error); -} - -int -mac_check_vnode_stat(struct ucred *active_cred, struct ucred *file_cred, - struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_stat"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_stat, active_cred, file_cred, vp, - &vp->v_label); - return (error); -} - -int -mac_check_vnode_write(struct ucred *active_cred, struct ucred *file_cred, - struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_write"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_write, active_cred, file_cred, vp, - &vp->v_label); - - return (error); -} - -/* - * When relabeling a process, call out to the policies for the maximum - * permission allowed for each object type we know about in its - * memory space, and revoke access (in the least surprising ways we - * know) when necessary. The process lock is not held here. - */ -void -mac_cred_mmapped_drop_perms(struct thread *td, struct ucred *cred) -{ - - /* XXX freeze all other threads */ - mac_cred_mmapped_drop_perms_recurse(td, cred, - &td->td_proc->p_vmspace->vm_map); - /* XXX allow other threads to continue */ -} - -static __inline const char * -prot2str(vm_prot_t prot) -{ - - switch (prot & VM_PROT_ALL) { - case VM_PROT_READ: - return ("r--"); - case VM_PROT_READ | VM_PROT_WRITE: - return ("rw-"); - case VM_PROT_READ | VM_PROT_EXECUTE: - return ("r-x"); - case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE: - return ("rwx"); - case VM_PROT_WRITE: - return ("-w-"); - case VM_PROT_EXECUTE: - return ("--x"); - case VM_PROT_WRITE | VM_PROT_EXECUTE: - return ("-wx"); - default: - return ("---"); - } -} - -static void -mac_cred_mmapped_drop_perms_recurse(struct thread *td, struct ucred *cred, - struct vm_map *map) -{ - struct vm_map_entry *vme; - int result; - vm_prot_t revokeperms; - vm_object_t object; - vm_ooffset_t offset; - struct vnode *vp; - - if (!mac_mmap_revocation) - return; - - vm_map_lock_read(map); - for (vme = map->header.next; vme != &map->header; vme = vme->next) { - if (vme->eflags & MAP_ENTRY_IS_SUB_MAP) { - mac_cred_mmapped_drop_perms_recurse(td, cred, - vme->object.sub_map); - continue; - } - /* - * Skip over entries that obviously are not shared. - */ - if (vme->eflags & (MAP_ENTRY_COW | MAP_ENTRY_NOSYNC) || - !vme->max_protection) - continue; - /* - * Drill down to the deepest backing object. - */ - offset = vme->offset; - object = vme->object.vm_object; - if (object == NULL) - continue; - while (object->backing_object != NULL) { - object = object->backing_object; - offset += object->backing_object_offset; - } - /* - * At the moment, vm_maps and objects aren't considered - * by the MAC system, so only things with backing by a - * normal object (read: vnodes) are checked. - */ - if (object->type != OBJT_VNODE) - continue; - vp = (struct vnode *)object->handle; - vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); - result = vme->max_protection; - mac_check_vnode_mmap_downgrade(cred, vp, &result); - VOP_UNLOCK(vp, 0, td); - /* - * Find out what maximum protection we may be allowing - * now but a policy needs to get removed. - */ - revokeperms = vme->max_protection & ~result; - if (!revokeperms) - continue; - printf("pid %ld: revoking %s perms from %#lx:%ld " - "(max %s/cur %s)\n", (long)td->td_proc->p_pid, - prot2str(revokeperms), (u_long)vme->start, - (long)(vme->end - vme->start), - prot2str(vme->max_protection), prot2str(vme->protection)); - vm_map_lock_upgrade(map); - /* - * This is the really simple case: if a map has more - * max_protection than is allowed, but it's not being - * actually used (that is, the current protection is - * still allowed), we can just wipe it out and do - * nothing more. - */ - if ((vme->protection & revokeperms) == 0) { - vme->max_protection -= revokeperms; - } else { - if (revokeperms & VM_PROT_WRITE) { - /* - * In the more complicated case, flush out all - * pending changes to the object then turn it - * copy-on-write. - */ - vm_object_reference(object); - vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); - VM_OBJECT_LOCK(object); - vm_object_page_clean(object, - OFF_TO_IDX(offset), - OFF_TO_IDX(offset + vme->end - vme->start + - PAGE_MASK), - OBJPC_SYNC); - VM_OBJECT_UNLOCK(object); - VOP_UNLOCK(vp, 0, td); - vm_object_deallocate(object); - /* - * Why bother if there's no read permissions - * anymore? For the rest, we need to leave - * the write permissions on for COW, or - * remove them entirely if configured to. - */ - if (!mac_mmap_revocation_via_cow) { - vme->max_protection &= ~VM_PROT_WRITE; - vme->protection &= ~VM_PROT_WRITE; - } if ((revokeperms & VM_PROT_READ) == 0) - vme->eflags |= MAP_ENTRY_COW | - MAP_ENTRY_NEEDS_COPY; - } - if (revokeperms & VM_PROT_EXECUTE) { - vme->max_protection &= ~VM_PROT_EXECUTE; - vme->protection &= ~VM_PROT_EXECUTE; - } - if (revokeperms & VM_PROT_READ) { - vme->max_protection = 0; - vme->protection = 0; - } - pmap_protect(map->pmap, vme->start, vme->end, - vme->protection & ~revokeperms); - vm_map_simplify_entry(map, vme); - } - vm_map_lock_downgrade(map); - } - vm_map_unlock_read(map); -} - -/* - * When the subject's label changes, it may require revocation of privilege - * to mapped objects. This can't be done on-the-fly later with a unified - * buffer cache. - */ -static void -mac_relabel_cred(struct ucred *cred, struct label *newlabel) -{ - - MAC_PERFORM(relabel_cred, cred, newlabel); -} - -void -mac_relabel_vnode(struct ucred *cred, struct vnode *vp, struct label *newlabel) -{ - - MAC_PERFORM(relabel_vnode, cred, vp, &vp->v_label, newlabel); -} - -void -mac_create_ifnet(struct ifnet *ifnet) -{ - - MAC_PERFORM(create_ifnet, ifnet, &ifnet->if_label); -} - -void -mac_create_bpfdesc(struct ucred *cred, struct bpf_d *bpf_d) -{ - - MAC_PERFORM(create_bpfdesc, cred, bpf_d, &bpf_d->bd_label); -} - -void -mac_create_socket(struct ucred *cred, struct socket *socket) -{ - - MAC_PERFORM(create_socket, cred, socket, &socket->so_label); -} - -void -mac_create_pipe(struct ucred *cred, struct pipe *pipe) -{ - - MAC_PERFORM(create_pipe, cred, pipe, pipe->pipe_label); -} - -void -mac_create_socket_from_socket(struct socket *oldsocket, - struct socket *newsocket) -{ - - MAC_PERFORM(create_socket_from_socket, oldsocket, &oldsocket->so_label, - newsocket, &newsocket->so_label); -} - -static void -mac_relabel_socket(struct ucred *cred, struct socket *socket, - struct label *newlabel) -{ - - MAC_PERFORM(relabel_socket, cred, socket, &socket->so_label, newlabel); -} - -static void -mac_relabel_pipe(struct ucred *cred, struct pipe *pipe, struct label *newlabel) -{ - - MAC_PERFORM(relabel_pipe, cred, pipe, pipe->pipe_label, newlabel); -} - -void -mac_set_socket_peer_from_mbuf(struct mbuf *mbuf, struct socket *socket) -{ - struct label *label; - - label = mbuf_to_label(mbuf); - - MAC_PERFORM(set_socket_peer_from_mbuf, mbuf, label, socket, - &socket->so_peerlabel); -} - -void -mac_set_socket_peer_from_socket(struct socket *oldsocket, - struct socket *newsocket) -{ - - MAC_PERFORM(set_socket_peer_from_socket, oldsocket, - &oldsocket->so_label, newsocket, &newsocket->so_peerlabel); -} - -void -mac_create_datagram_from_ipq(struct ipq *ipq, struct mbuf *datagram) -{ - struct label *label; - - label = mbuf_to_label(datagram); - - MAC_PERFORM(create_datagram_from_ipq, ipq, &ipq->ipq_label, - datagram, label); -} - -void -mac_create_fragment(struct mbuf *datagram, struct mbuf *fragment) -{ - struct label *datagramlabel, *fragmentlabel; - - datagramlabel = mbuf_to_label(datagram); - fragmentlabel = mbuf_to_label(fragment); - - MAC_PERFORM(create_fragment, datagram, datagramlabel, fragment, - fragmentlabel); -} - -void -mac_create_ipq(struct mbuf *fragment, struct ipq *ipq) -{ - struct label *label; - - label = mbuf_to_label(fragment); - - MAC_PERFORM(create_ipq, fragment, label, ipq, &ipq->ipq_label); -} - -void -mac_create_mbuf_from_mbuf(struct mbuf *oldmbuf, struct mbuf *newmbuf) -{ - struct label *oldmbuflabel, *newmbuflabel; - - oldmbuflabel = mbuf_to_label(oldmbuf); - newmbuflabel = mbuf_to_label(newmbuf); - - MAC_PERFORM(create_mbuf_from_mbuf, oldmbuf, oldmbuflabel, newmbuf, - newmbuflabel); -} - -void -mac_create_mbuf_from_bpfdesc(struct bpf_d *bpf_d, struct mbuf *mbuf) -{ - struct label *label; - - label = mbuf_to_label(mbuf); - - MAC_PERFORM(create_mbuf_from_bpfdesc, bpf_d, &bpf_d->bd_label, mbuf, - label); -} - -void -mac_create_mbuf_linklayer(struct ifnet *ifnet, struct mbuf *mbuf) -{ - struct label *label; - - label = mbuf_to_label(mbuf); - - MAC_PERFORM(create_mbuf_linklayer, ifnet, &ifnet->if_label, mbuf, - label); -} - -void -mac_create_mbuf_from_ifnet(struct ifnet *ifnet, struct mbuf *mbuf) -{ - struct label *label; - - label = mbuf_to_label(mbuf); - - MAC_PERFORM(create_mbuf_from_ifnet, ifnet, &ifnet->if_label, mbuf, - label); -} - -void -mac_create_mbuf_multicast_encap(struct mbuf *oldmbuf, struct ifnet *ifnet, - struct mbuf *newmbuf) -{ - struct label *oldmbuflabel, *newmbuflabel; - - oldmbuflabel = mbuf_to_label(oldmbuf); - newmbuflabel = mbuf_to_label(newmbuf); - - MAC_PERFORM(create_mbuf_multicast_encap, oldmbuf, oldmbuflabel, - ifnet, &ifnet->if_label, newmbuf, newmbuflabel); -} - -void -mac_create_mbuf_netlayer(struct mbuf *oldmbuf, struct mbuf *newmbuf) -{ - struct label *oldmbuflabel, *newmbuflabel; - - oldmbuflabel = mbuf_to_label(oldmbuf); - newmbuflabel = mbuf_to_label(newmbuf); - - MAC_PERFORM(create_mbuf_netlayer, oldmbuf, oldmbuflabel, newmbuf, - newmbuflabel); -} - -int -mac_fragment_match(struct mbuf *fragment, struct ipq *ipq) -{ - struct label *label; - int result; - - label = mbuf_to_label(fragment); - - result = 1; - MAC_BOOLEAN(fragment_match, &&, fragment, label, ipq, - &ipq->ipq_label); - - return (result); -} - -void -mac_reflect_mbuf_icmp(struct mbuf *m) -{ - struct label *label; - - label = mbuf_to_label(m); - - MAC_PERFORM(reflect_mbuf_icmp, m, label); -} -void -mac_reflect_mbuf_tcp(struct mbuf *m) -{ - struct label *label; - - label = mbuf_to_label(m); - - MAC_PERFORM(reflect_mbuf_tcp, m, label); -} - -void -mac_update_ipq(struct mbuf *fragment, struct ipq *ipq) -{ - struct label *label; - - label = mbuf_to_label(fragment); - - MAC_PERFORM(update_ipq, fragment, label, ipq, &ipq->ipq_label); -} - -void -mac_create_mbuf_from_socket(struct socket *socket, struct mbuf *mbuf) -{ - struct label *label; - - label = mbuf_to_label(mbuf); - - MAC_PERFORM(create_mbuf_from_socket, socket, &socket->so_label, mbuf, - label); -} - -void -mac_create_mount(struct ucred *cred, struct mount *mp) -{ - - MAC_PERFORM(create_mount, cred, mp, &mp->mnt_mntlabel, - &mp->mnt_fslabel); -} - -void -mac_create_root_mount(struct ucred *cred, struct mount *mp) -{ - - MAC_PERFORM(create_root_mount, cred, mp, &mp->mnt_mntlabel, - &mp->mnt_fslabel); -} - -int -mac_check_bpfdesc_receive(struct bpf_d *bpf_d, struct ifnet *ifnet) -{ - int error; - - if (!mac_enforce_network) - return (0); - - MAC_CHECK(check_bpfdesc_receive, bpf_d, &bpf_d->bd_label, ifnet, - &ifnet->if_label); - - return (error); -} - -static int -mac_check_cred_relabel(struct ucred *cred, struct label *newlabel) -{ - int error; - - MAC_CHECK(check_cred_relabel, cred, newlabel); - - return (error); -} - -int -mac_check_cred_visible(struct ucred *u1, struct ucred *u2) -{ - int error; - - if (!mac_enforce_process) - return (0); - - MAC_CHECK(check_cred_visible, u1, u2); - - return (error); -} - -int -mac_check_ifnet_transmit(struct ifnet *ifnet, struct mbuf *mbuf) -{ - struct label *label; - int error; - - M_ASSERTPKTHDR(mbuf); - - if (!mac_enforce_network) - return (0); - - label = mbuf_to_label(mbuf); - - MAC_CHECK(check_ifnet_transmit, ifnet, &ifnet->if_label, mbuf, - label); - - return (error); -} - -int -mac_check_kenv_dump(struct ucred *cred) -{ - int error; - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_kenv_dump, cred); - - return (error); -} - -int -mac_check_kenv_get(struct ucred *cred, char *name) -{ - int error; - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_kenv_get, cred, name); - - return (error); -} - -int -mac_check_kenv_set(struct ucred *cred, char *name, char *value) -{ - int error; - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_kenv_set, cred, name, value); - - return (error); -} - -int -mac_check_kenv_unset(struct ucred *cred, char *name) -{ - int error; - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_kenv_unset, cred, name); - - return (error); -} - -int -mac_check_kld_load(struct ucred *cred, struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_kld_load"); - - if (!mac_enforce_kld) - return (0); - - MAC_CHECK(check_kld_load, cred, vp, &vp->v_label); - - return (error); -} - -int -mac_check_kld_stat(struct ucred *cred) -{ - int error; - - if (!mac_enforce_kld) - return (0); - - MAC_CHECK(check_kld_stat, cred); - - return (error); -} - -int -mac_check_kld_unload(struct ucred *cred) -{ - int error; - - if (!mac_enforce_kld) - return (0); - - MAC_CHECK(check_kld_unload, cred); - - return (error); -} - -int -mac_check_mount_stat(struct ucred *cred, struct mount *mount) -{ - int error; - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_mount_stat, cred, mount, &mount->mnt_mntlabel); - - return (error); -} - -int -mac_check_pipe_ioctl(struct ucred *cred, struct pipe *pipe, unsigned long cmd, - void *data) -{ - int error; - - PIPE_LOCK_ASSERT(pipe, MA_OWNED); - - if (!mac_enforce_pipe) - return (0); - - MAC_CHECK(check_pipe_ioctl, cred, pipe, pipe->pipe_label, cmd, data); - - return (error); -} - -int -mac_check_pipe_poll(struct ucred *cred, struct pipe *pipe) -{ - int error; - - PIPE_LOCK_ASSERT(pipe, MA_OWNED); - - if (!mac_enforce_pipe) - return (0); - - MAC_CHECK(check_pipe_poll, cred, pipe, pipe->pipe_label); - - return (error); -} - -int -mac_check_pipe_read(struct ucred *cred, struct pipe *pipe) -{ - int error; - - PIPE_LOCK_ASSERT(pipe, MA_OWNED); - - if (!mac_enforce_pipe) - return (0); - - MAC_CHECK(check_pipe_read, cred, pipe, pipe->pipe_label); - - return (error); -} - -static int -mac_check_pipe_relabel(struct ucred *cred, struct pipe *pipe, - struct label *newlabel) -{ - int error; - - PIPE_LOCK_ASSERT(pipe, MA_OWNED); - - if (!mac_enforce_pipe) - return (0); - - MAC_CHECK(check_pipe_relabel, cred, pipe, pipe->pipe_label, newlabel); - - return (error); -} - -int -mac_check_pipe_stat(struct ucred *cred, struct pipe *pipe) -{ - int error; - - PIPE_LOCK_ASSERT(pipe, MA_OWNED); - - if (!mac_enforce_pipe) - return (0); - - MAC_CHECK(check_pipe_stat, cred, pipe, pipe->pipe_label); - - return (error); -} - -int -mac_check_pipe_write(struct ucred *cred, struct pipe *pipe) -{ - int error; - - PIPE_LOCK_ASSERT(pipe, MA_OWNED); - - if (!mac_enforce_pipe) - return (0); - - MAC_CHECK(check_pipe_write, cred, pipe, pipe->pipe_label); - - return (error); -} - -int -mac_check_proc_debug(struct ucred *cred, struct proc *proc) -{ - int error; - - PROC_LOCK_ASSERT(proc, MA_OWNED); - - if (!mac_enforce_process) - return (0); - - MAC_CHECK(check_proc_debug, cred, proc); - - return (error); -} - -int -mac_check_proc_sched(struct ucred *cred, struct proc *proc) -{ - int error; - - PROC_LOCK_ASSERT(proc, MA_OWNED); - - if (!mac_enforce_process) - return (0); - - MAC_CHECK(check_proc_sched, cred, proc); - - return (error); -} - -int -mac_check_proc_signal(struct ucred *cred, struct proc *proc, int signum) -{ - int error; - - PROC_LOCK_ASSERT(proc, MA_OWNED); - - if (!mac_enforce_process) - return (0); - - MAC_CHECK(check_proc_signal, cred, proc, signum); - - return (error); -} - -int -mac_check_socket_bind(struct ucred *ucred, struct socket *socket, - struct sockaddr *sockaddr) -{ - int error; - - if (!mac_enforce_socket) - return (0); - - MAC_CHECK(check_socket_bind, ucred, socket, &socket->so_label, - sockaddr); - - return (error); -} - -int -mac_check_socket_connect(struct ucred *cred, struct socket *socket, - struct sockaddr *sockaddr) -{ - int error; - - if (!mac_enforce_socket) - return (0); - - MAC_CHECK(check_socket_connect, cred, socket, &socket->so_label, - sockaddr); - - return (error); -} - -int -mac_check_socket_deliver(struct socket *socket, struct mbuf *mbuf) -{ - struct label *label; - int error; - - if (!mac_enforce_socket) - return (0); - - label = mbuf_to_label(mbuf); - - MAC_CHECK(check_socket_deliver, socket, &socket->so_label, mbuf, - label); - - return (error); -} - -int -mac_check_socket_listen(struct ucred *cred, struct socket *socket) -{ - int error; - - if (!mac_enforce_socket) - return (0); - - MAC_CHECK(check_socket_listen, cred, socket, &socket->so_label); - return (error); -} - -int -mac_check_socket_receive(struct ucred *cred, struct socket *so) -{ - int error; - - if (!mac_enforce_socket) - return (0); - - MAC_CHECK(check_socket_receive, cred, so, &so->so_label); - - return (error); -} - -static int -mac_check_socket_relabel(struct ucred *cred, struct socket *socket, - struct label *newlabel) -{ - int error; - - MAC_CHECK(check_socket_relabel, cred, socket, &socket->so_label, - newlabel); - - return (error); -} - -int -mac_check_socket_send(struct ucred *cred, struct socket *so) -{ - int error; - - if (!mac_enforce_socket) - return (0); - - MAC_CHECK(check_socket_send, cred, so, &so->so_label); - - return (error); -} - -int -mac_check_socket_visible(struct ucred *cred, struct socket *socket) -{ - int error; - - if (!mac_enforce_socket) - return (0); - - MAC_CHECK(check_socket_visible, cred, socket, &socket->so_label); - - return (error); -} - -int -mac_check_sysarch_ioperm(struct ucred *cred) -{ - int error; - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_sysarch_ioperm, cred); - return (error); -} - -int -mac_check_system_acct(struct ucred *cred, struct vnode *vp) -{ - int error; - - if (vp != NULL) { - ASSERT_VOP_LOCKED(vp, "mac_check_system_acct"); - } - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_system_acct, cred, vp, - vp != NULL ? &vp->v_label : NULL); - - return (error); -} - -int -mac_check_system_nfsd(struct ucred *cred) -{ - int error; - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_system_nfsd, cred); - - return (error); -} - -int -mac_check_system_reboot(struct ucred *cred, int howto) -{ - int error; - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_system_reboot, cred, howto); - - return (error); -} - -int -mac_check_system_settime(struct ucred *cred) -{ - int error; - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_system_settime, cred); - - return (error); -} - -int -mac_check_system_swapon(struct ucred *cred, struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_system_swapon"); - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_system_swapon, cred, vp, &vp->v_label); - return (error); -} - -int -mac_check_system_swapoff(struct ucred *cred, struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_system_swapoff"); - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_system_swapoff, cred, vp, &vp->v_label); - return (error); -} - -int -mac_check_system_sysctl(struct ucred *cred, int *name, u_int namelen, - void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen) -{ - int error; - - /* - * XXXMAC: We're very much like to assert the SYSCTL_LOCK here, - * but since it's not exported from kern_sysctl.c, we can't. - */ - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_system_sysctl, cred, name, namelen, old, oldlenp, - inkernel, new, newlen); - - return (error); -} - -int -mac_ioctl_ifnet_get(struct ucred *cred, struct ifreq *ifr, - struct ifnet *ifnet) -{ - char *elements, *buffer; - struct mac mac; - int error; - - error = copyin(ifr->ifr_ifru.ifru_data, &mac, sizeof(mac)); - if (error) - return (error); - - error = mac_check_structmac_consistent(&mac); - if (error) - return (error); - - elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK); - error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL); - if (error) { - free(elements, M_MACTEMP); - return (error); - } - - buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO); - error = mac_externalize_ifnet_label(&ifnet->if_label, elements, - buffer, mac.m_buflen, M_WAITOK); - if (error == 0) - error = copyout(buffer, mac.m_string, strlen(buffer)+1); - - free(buffer, M_MACTEMP); - free(elements, M_MACTEMP); - - return (error); -} - -int -mac_ioctl_ifnet_set(struct ucred *cred, struct ifreq *ifr, - struct ifnet *ifnet) -{ - struct label intlabel; - struct mac mac; - char *buffer; - int error; - - error = copyin(ifr->ifr_ifru.ifru_data, &mac, sizeof(mac)); - if (error) - return (error); - - error = mac_check_structmac_consistent(&mac); - if (error) - return (error); - - buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK); - error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL); - if (error) { - free(buffer, M_MACTEMP); - return (error); - } - - mac_init_ifnet_label(&intlabel); - error = mac_internalize_ifnet_label(&intlabel, buffer); - free(buffer, M_MACTEMP); - if (error) { - mac_destroy_ifnet_label(&intlabel); - return (error); - } - - /* - * XXX: Note that this is a redundant privilege check, since - * policies impose this check themselves if required by the - * policy. Eventually, this should go away. - */ - error = suser_cred(cred, 0); - if (error) { - mac_destroy_ifnet_label(&intlabel); - return (error); - } - - MAC_CHECK(check_ifnet_relabel, cred, ifnet, &ifnet->if_label, - &intlabel); - if (error) { - mac_destroy_ifnet_label(&intlabel); - return (error); - } - - MAC_PERFORM(relabel_ifnet, cred, ifnet, &ifnet->if_label, &intlabel); - - mac_destroy_ifnet_label(&intlabel); - return (0); -} - -void -mac_create_devfs_device(struct mount *mp, dev_t dev, struct devfs_dirent *de) -{ - - MAC_PERFORM(create_devfs_device, mp, dev, de, &de->de_label); -} - -void -mac_create_devfs_symlink(struct ucred *cred, struct mount *mp, - struct devfs_dirent *dd, struct devfs_dirent *de) -{ - - MAC_PERFORM(create_devfs_symlink, cred, mp, dd, &dd->de_label, de, - &de->de_label); -} - -void -mac_create_devfs_directory(struct mount *mp, char *dirname, int dirnamelen, - struct devfs_dirent *de) -{ - - MAC_PERFORM(create_devfs_directory, mp, dirname, dirnamelen, de, - &de->de_label); -} - -int -mac_setsockopt_label_set(struct ucred *cred, struct socket *so, - struct mac *mac) -{ - struct label intlabel; - char *buffer; - int error; - - error = mac_check_structmac_consistent(mac); - if (error) - return (error); - - buffer = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK); - error = copyinstr(mac->m_string, buffer, mac->m_buflen, NULL); - if (error) { - free(buffer, M_MACTEMP); - return (error); - } - - mac_init_socket_label(&intlabel, M_WAITOK); - error = mac_internalize_socket_label(&intlabel, buffer); - free(buffer, M_MACTEMP); - if (error) { - mac_destroy_socket_label(&intlabel); - return (error); - } - - mac_check_socket_relabel(cred, so, &intlabel); - if (error) { - mac_destroy_socket_label(&intlabel); - return (error); - } - - mac_relabel_socket(cred, so, &intlabel); - - mac_destroy_socket_label(&intlabel); - return (0); -} - -int -mac_pipe_label_set(struct ucred *cred, struct pipe *pipe, struct label *label) -{ - int error; - - PIPE_LOCK_ASSERT(pipe, MA_OWNED); - - error = mac_check_pipe_relabel(cred, pipe, label); - if (error) - return (error); - - mac_relabel_pipe(cred, pipe, label); - - return (0); -} - -int -mac_getsockopt_label_get(struct ucred *cred, struct socket *so, - struct mac *mac) -{ - char *buffer, *elements; - int error; - - error = mac_check_structmac_consistent(mac); - if (error) - return (error); - - elements = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK); - error = copyinstr(mac->m_string, elements, mac->m_buflen, NULL); - if (error) { - free(elements, M_MACTEMP); - return (error); - } - - buffer = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK | M_ZERO); - error = mac_externalize_socket_label(&so->so_label, elements, - buffer, mac->m_buflen, M_WAITOK); - if (error == 0) - error = copyout(buffer, mac->m_string, strlen(buffer)+1); - - free(buffer, M_MACTEMP); - free(elements, M_MACTEMP); - - return (error); -} - -int -mac_getsockopt_peerlabel_get(struct ucred *cred, struct socket *so, - struct mac *mac) -{ - char *elements, *buffer; - int error; - - error = mac_check_structmac_consistent(mac); - if (error) - return (error); - - elements = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK); - error = copyinstr(mac->m_string, elements, mac->m_buflen, NULL); - if (error) { - free(elements, M_MACTEMP); - return (error); - } - - buffer = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK | M_ZERO); - error = mac_externalize_socket_peer_label(&so->so_peerlabel, - elements, buffer, mac->m_buflen, M_WAITOK); - if (error == 0) - error = copyout(buffer, mac->m_string, strlen(buffer)+1); - - free(buffer, M_MACTEMP); - free(elements, M_MACTEMP); - - return (error); -} - -/* - * Implementation of VOP_SETLABEL() that relies on extended attributes - * to store label data. Can be referenced by filesystems supporting - * extended attributes. - */ -int -vop_stdsetlabel_ea(struct vop_setlabel_args *ap) -{ - struct vnode *vp = ap->a_vp; - struct label *intlabel = ap->a_label; - int error; - - ASSERT_VOP_LOCKED(vp, "vop_stdsetlabel_ea"); - - if ((vp->v_mount->mnt_flag & MNT_MULTILABEL) == 0) - return (EOPNOTSUPP); - - error = mac_setlabel_vnode_extattr(ap->a_cred, vp, intlabel); - if (error) - return (error); - - mac_relabel_vnode(ap->a_cred, vp, intlabel); - - return (0); -} - -static int -vn_setlabel(struct vnode *vp, struct label *intlabel, struct ucred *cred) -{ - int error; - - if (vp->v_mount == NULL) { - /* printf("vn_setlabel: null v_mount\n"); */ - if (vp->v_type != VNON) - printf("vn_setlabel: null v_mount with non-VNON\n"); - return (EBADF); - } - - if ((vp->v_mount->mnt_flag & MNT_MULTILABEL) == 0) - return (EOPNOTSUPP); - - /* - * Multi-phase commit. First check the policies to confirm the - * change is OK. Then commit via the filesystem. Finally, - * update the actual vnode label. Question: maybe the filesystem - * should update the vnode at the end as part of VOP_SETLABEL()? - */ - error = mac_check_vnode_relabel(cred, vp, intlabel); - if (error) - return (error); - - /* - * VADMIN provides the opportunity for the filesystem to make - * decisions about who is and is not able to modify labels - * and protections on files. This might not be right. We can't - * assume VOP_SETLABEL() will do it, because we might implement - * that as part of vop_stdsetlabel_ea(). - */ - error = VOP_ACCESS(vp, VADMIN, cred, curthread); - if (error) - return (error); - - error = VOP_SETLABEL(vp, intlabel, cred, curthread); - if (error) - return (error); - - return (0); -} - int __mac_get_pid(struct thread *td, struct __mac_get_pid_args *uap) { diff --git a/sys/security/mac/mac_framework.c b/sys/security/mac/mac_framework.c index 35c9c9b..aae8a24 100644 --- a/sys/security/mac/mac_framework.c +++ b/sys/security/mac/mac_framework.c @@ -85,6 +85,8 @@ __FBSDID("$FreeBSD$"); #include <netinet/in.h> #include <netinet/ip_var.h> +#include <security/mac/mac_internal.h> + #ifdef MAC /* @@ -94,8 +96,6 @@ __FBSDID("$FreeBSD$"); */ MODULE_VERSION(kernel_mac_support, 1); -SYSCTL_DECL(_security); - SYSCTL_NODE(_security, OID_AUTO, mac, CTLFLAG_RW, 0, "TrustedBSD MAC policy controls"); @@ -130,119 +130,20 @@ int mac_late = 0; int mac_labelmbufs = 0; #endif -static int mac_enforce_fs = 1; -SYSCTL_INT(_security_mac, OID_AUTO, enforce_fs, CTLFLAG_RW, - &mac_enforce_fs, 0, "Enforce MAC policy on file system objects"); -TUNABLE_INT("security.mac.enforce_fs", &mac_enforce_fs); - -static int mac_enforce_kld = 1; -SYSCTL_INT(_security_mac, OID_AUTO, enforce_kld, CTLFLAG_RW, - &mac_enforce_kld, 0, "Enforce MAC policy on kld operations"); -TUNABLE_INT("security.mac.enforce_kld", &mac_enforce_kld); - -static int mac_enforce_network = 1; -SYSCTL_INT(_security_mac, OID_AUTO, enforce_network, CTLFLAG_RW, - &mac_enforce_network, 0, "Enforce MAC policy on network packets"); -TUNABLE_INT("security.mac.enforce_network", &mac_enforce_network); - -static int mac_enforce_pipe = 1; -SYSCTL_INT(_security_mac, OID_AUTO, enforce_pipe, CTLFLAG_RW, - &mac_enforce_pipe, 0, "Enforce MAC policy on pipe operations"); -TUNABLE_INT("security.mac.enforce_pipe", &mac_enforce_pipe); - -static int mac_enforce_process = 1; -SYSCTL_INT(_security_mac, OID_AUTO, enforce_process, CTLFLAG_RW, - &mac_enforce_process, 0, "Enforce MAC policy on inter-process operations"); -TUNABLE_INT("security.mac.enforce_process", &mac_enforce_process); - -static int mac_enforce_socket = 1; -SYSCTL_INT(_security_mac, OID_AUTO, enforce_socket, CTLFLAG_RW, - &mac_enforce_socket, 0, "Enforce MAC policy on socket operations"); -TUNABLE_INT("security.mac.enforce_socket", &mac_enforce_socket); - -static int mac_enforce_system = 1; -SYSCTL_INT(_security_mac, OID_AUTO, enforce_system, CTLFLAG_RW, - &mac_enforce_system, 0, "Enforce MAC policy on system operations"); -TUNABLE_INT("security.mac.enforce_system", &mac_enforce_system); - -static int mac_enforce_vm = 1; -SYSCTL_INT(_security_mac, OID_AUTO, enforce_vm, CTLFLAG_RW, - &mac_enforce_vm, 0, "Enforce MAC policy on vm operations"); -TUNABLE_INT("security.mac.enforce_vm", &mac_enforce_vm); - -static int mac_mmap_revocation = 1; -SYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation, CTLFLAG_RW, - &mac_mmap_revocation, 0, "Revoke mmap access to files on subject " - "relabel"); -static int mac_mmap_revocation_via_cow = 0; -SYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation_via_cow, CTLFLAG_RW, - &mac_mmap_revocation_via_cow, 0, "Revoke mmap access to files via " - "copy-on-write semantics, or by removing all write access"); - #ifdef MAC_DEBUG SYSCTL_NODE(_security_mac, OID_AUTO, debug, CTLFLAG_RW, 0, "TrustedBSD MAC debug info"); - -static int mac_debug_label_fallback = 0; -SYSCTL_INT(_security_mac_debug, OID_AUTO, label_fallback, CTLFLAG_RW, - &mac_debug_label_fallback, 0, "Filesystems should fall back to fs label" - "when label is corrupted."); -TUNABLE_INT("security.mac.debug_label_fallback", - &mac_debug_label_fallback); - SYSCTL_NODE(_security_mac_debug, OID_AUTO, counters, CTLFLAG_RW, 0, "TrustedBSD MAC object counters"); -static unsigned int nmacmbufs, nmaccreds, nmacifnets, nmacbpfdescs, - nmacsockets, nmacmounts, nmactemp, nmacvnodes, nmacdevfsdirents, - nmacipqs, nmacpipes, nmacprocs; - -#define MAC_DEBUG_COUNTER_INC(x) atomic_add_int(x, 1); -#define MAC_DEBUG_COUNTER_DEC(x) atomic_subtract_int(x, 1); - -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, mbufs, CTLFLAG_RD, - &nmacmbufs, 0, "number of mbufs in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, creds, CTLFLAG_RD, - &nmaccreds, 0, "number of ucreds in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, ifnets, CTLFLAG_RD, - &nmacifnets, 0, "number of ifnets in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, ipqs, CTLFLAG_RD, - &nmacipqs, 0, "number of ipqs in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, bpfdescs, CTLFLAG_RD, - &nmacbpfdescs, 0, "number of bpfdescs in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, sockets, CTLFLAG_RD, - &nmacsockets, 0, "number of sockets in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, pipes, CTLFLAG_RD, - &nmacpipes, 0, "number of pipes in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, procs, CTLFLAG_RD, - &nmacprocs, 0, "number of procs in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, mounts, CTLFLAG_RD, - &nmacmounts, 0, "number of mounts in use"); +static unsigned int nmactemp; SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, temp, CTLFLAG_RD, &nmactemp, 0, "number of temporary labels in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, vnodes, CTLFLAG_RD, - &nmacvnodes, 0, "number of vnodes in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, devfsdirents, CTLFLAG_RD, - &nmacdevfsdirents, 0, "number of devfs dirents inuse"); -#else -#define MAC_DEBUG_COUNTER_INC(x) -#define MAC_DEBUG_COUNTER_DEC(x) #endif static int mac_policy_register(struct mac_policy_conf *mpc); static int mac_policy_unregister(struct mac_policy_conf *mpc); -static void mac_check_vnode_mmap_downgrade(struct ucred *cred, - struct vnode *vp, int *prot); -static void mac_cred_mmapped_drop_perms_recurse(struct thread *td, - struct ucred *cred, struct vm_map *map); - -static void mac_destroy_socket_label(struct label *label); - -static int mac_setlabel_vnode_extattr(struct ucred *cred, - struct vnode *vp, struct label *intlabel); - -MALLOC_DEFINE(M_MACPIPELABEL, "macpipelabel", "MAC labels for pipes"); MALLOC_DEFINE(M_MACTEMP, "mactemp", "MAC temporary label storage"); /* @@ -341,151 +242,6 @@ mac_policy_list_unbusy(void) } /* - * MAC_CHECK performs the designated check by walking the policy - * module list and checking with each as to how it feels about the - * request. Note that it returns its value via 'error' in the scope - * of the caller. - */ -#define MAC_CHECK(check, args...) do { \ - struct mac_policy_conf *mpc; \ - int entrycount; \ - \ - error = 0; \ - LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ - if (mpc->mpc_ops->mpo_ ## check != NULL) \ - error = mac_error_select( \ - mpc->mpc_ops->mpo_ ## check (args), \ - error); \ - } \ - if ((entrycount = mac_policy_list_conditional_busy()) != 0) { \ - LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ - if (mpc->mpc_ops->mpo_ ## check != NULL) \ - error = mac_error_select( \ - mpc->mpc_ops->mpo_ ## check (args), \ - error); \ - } \ - mac_policy_list_unbusy(); \ - } \ -} while (0) - -/* - * MAC_BOOLEAN performs the designated boolean composition by walking - * the module list, invoking each instance of the operation, and - * combining the results using the passed C operator. Note that it - * returns its value via 'result' in the scope of the caller, which - * should be initialized by the caller in a meaningful way to get - * a meaningful result. - */ -#define MAC_BOOLEAN(operation, composition, args...) do { \ - struct mac_policy_conf *mpc; \ - int entrycount; \ - \ - LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ - if (mpc->mpc_ops->mpo_ ## operation != NULL) \ - result = result composition \ - mpc->mpc_ops->mpo_ ## operation (args); \ - } \ - if ((entrycount = mac_policy_list_conditional_busy()) != 0) { \ - LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ - if (mpc->mpc_ops->mpo_ ## operation != NULL) \ - result = result composition \ - mpc->mpc_ops->mpo_ ## operation \ - (args); \ - } \ - mac_policy_list_unbusy(); \ - } \ -} while (0) - -#define MAC_EXTERNALIZE(type, label, elementlist, outbuf, \ - outbuflen) do { \ - int claimed, first, ignorenotfound, savedlen; \ - char *element_name, *element_temp; \ - struct sbuf sb; \ - \ - error = 0; \ - first = 1; \ - sbuf_new(&sb, outbuf, outbuflen, SBUF_FIXEDLEN); \ - element_temp = elementlist; \ - while ((element_name = strsep(&element_temp, ",")) != NULL) { \ - if (element_name[0] == '?') { \ - element_name++; \ - ignorenotfound = 1; \ - } else \ - ignorenotfound = 0; \ - savedlen = sbuf_len(&sb); \ - if (first) { \ - error = sbuf_printf(&sb, "%s/", element_name); \ - first = 0; \ - } else \ - error = sbuf_printf(&sb, ",%s/", element_name); \ - if (error == -1) { \ - error = EINVAL; /* XXX: E2BIG? */ \ - break; \ - } \ - claimed = 0; \ - MAC_CHECK(externalize_ ## type, label, element_name, \ - &sb, &claimed); \ - if (error) \ - break; \ - if (claimed == 0 && ignorenotfound) { \ - /* Revert last label name. */ \ - sbuf_setpos(&sb, savedlen); \ - } else if (claimed != 1) { \ - error = EINVAL; /* XXX: ENOLABEL? */ \ - break; \ - } \ - } \ - sbuf_finish(&sb); \ -} while (0) - -#define MAC_INTERNALIZE(type, label, instring) do { \ - char *element, *element_name, *element_data; \ - int claimed; \ - \ - error = 0; \ - element = instring; \ - while ((element_name = strsep(&element, ",")) != NULL) { \ - element_data = element_name; \ - element_name = strsep(&element_data, "/"); \ - if (element_data == NULL) { \ - error = EINVAL; \ - break; \ - } \ - claimed = 0; \ - MAC_CHECK(internalize_ ## type, label, element_name, \ - element_data, &claimed); \ - if (error) \ - break; \ - if (claimed != 1) { \ - /* XXXMAC: Another error here? */ \ - error = EINVAL; \ - break; \ - } \ - } \ -} while (0) - -/* - * MAC_PERFORM performs the designated operation by walking the policy - * module list and invoking that operation for each policy. - */ -#define MAC_PERFORM(operation, args...) do { \ - struct mac_policy_conf *mpc; \ - int entrycount; \ - \ - LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ - if (mpc->mpc_ops->mpo_ ## operation != NULL) \ - mpc->mpc_ops->mpo_ ## operation (args); \ - } \ - if ((entrycount = mac_policy_list_conditional_busy()) != 0) { \ - LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ - if (mpc->mpc_ops->mpo_ ## operation != NULL) \ - mpc->mpc_ops->mpo_ ## operation (args); \ - } \ - mac_policy_list_unbusy(); \ - } \ -} while (0) - -/* * Initialize the MAC subsystem, including appropriate SMP locks. */ static void @@ -735,19 +491,7 @@ mac_error_select(int error1, int error2) return (error2); } -static struct label * -mbuf_to_label(struct mbuf *mbuf) -{ - struct m_tag *tag; - struct label *label; - - tag = m_tag_find(mbuf, PACKET_TAG_MACLABEL, NULL); - label = (struct label *)(tag+1); - - return (label); -} - -static void +void mac_init_label(struct label *label) { @@ -755,7 +499,7 @@ mac_init_label(struct label *label) label->l_flags = MAC_FLAG_INITIALIZED; } -static void +void mac_destroy_label(struct label *label) { @@ -766,406 +510,7 @@ mac_destroy_label(struct label *label) /* implicit: label->l_flags &= ~MAC_FLAG_INITIALIZED; */ } -void -mac_init_bpfdesc(struct bpf_d *bpf_d) -{ - - mac_init_label(&bpf_d->bd_label); - MAC_PERFORM(init_bpfdesc_label, &bpf_d->bd_label); - MAC_DEBUG_COUNTER_INC(&nmacbpfdescs); -} - -static void -mac_init_cred_label(struct label *label) -{ - - mac_init_label(label); - MAC_PERFORM(init_cred_label, label); - MAC_DEBUG_COUNTER_INC(&nmaccreds); -} - -void -mac_init_cred(struct ucred *cred) -{ - - mac_init_cred_label(&cred->cr_label); -} - -void -mac_init_devfsdirent(struct devfs_dirent *de) -{ - - mac_init_label(&de->de_label); - MAC_PERFORM(init_devfsdirent_label, &de->de_label); - MAC_DEBUG_COUNTER_INC(&nmacdevfsdirents); -} - -static void -mac_init_ifnet_label(struct label *label) -{ - - mac_init_label(label); - MAC_PERFORM(init_ifnet_label, label); - MAC_DEBUG_COUNTER_INC(&nmacifnets); -} - -void -mac_init_ifnet(struct ifnet *ifp) -{ - - mac_init_ifnet_label(&ifp->if_label); -} - -int -mac_init_ipq(struct ipq *ipq, int flag) -{ - int error; - - mac_init_label(&ipq->ipq_label); - - MAC_CHECK(init_ipq_label, &ipq->ipq_label, flag); - if (error) { - MAC_PERFORM(destroy_ipq_label, &ipq->ipq_label); - mac_destroy_label(&ipq->ipq_label); - } else { - MAC_DEBUG_COUNTER_INC(&nmacipqs); - } - return (error); -} - -int -mac_init_mbuf_tag(struct m_tag *tag, int flag) -{ - struct label *label; - int error; - - label = (struct label *) (tag + 1); - mac_init_label(label); - - MAC_CHECK(init_mbuf_label, label, flag); - if (error) { - MAC_PERFORM(destroy_mbuf_label, label); - mac_destroy_label(label); - } else { - MAC_DEBUG_COUNTER_INC(&nmacmbufs); - } - return (error); -} - -int -mac_init_mbuf(struct mbuf *m, int flag) -{ - struct m_tag *tag; - int error; - - M_ASSERTPKTHDR(m); - -#ifndef MAC_ALWAYS_LABEL_MBUF - /* - * If conditionally allocating mbuf labels, don't allocate unless - * they are required. - */ - if (!mac_labelmbufs) - return (0); -#endif - tag = m_tag_get(PACKET_TAG_MACLABEL, sizeof(struct label), - flag); - if (tag == NULL) - return (ENOMEM); - error = mac_init_mbuf_tag(tag, flag); - if (error) { - m_tag_free(tag); - return (error); - } - m_tag_prepend(m, tag); - return (0); -} - -void -mac_init_mount(struct mount *mp) -{ - - mac_init_label(&mp->mnt_mntlabel); - mac_init_label(&mp->mnt_fslabel); - MAC_PERFORM(init_mount_label, &mp->mnt_mntlabel); - MAC_PERFORM(init_mount_fs_label, &mp->mnt_fslabel); - MAC_DEBUG_COUNTER_INC(&nmacmounts); -} - -static void -mac_init_pipe_label(struct label *label) -{ - - mac_init_label(label); - MAC_PERFORM(init_pipe_label, label); - MAC_DEBUG_COUNTER_INC(&nmacpipes); -} - -void -mac_init_pipe(struct pipe *pipe) -{ - struct label *label; - - label = malloc(sizeof(struct label), M_MACPIPELABEL, M_ZERO|M_WAITOK); - pipe->pipe_label = label; - pipe->pipe_peer->pipe_label = label; - mac_init_pipe_label(label); -} - -void -mac_init_proc(struct proc *p) -{ - - mac_init_label(&p->p_label); - MAC_PERFORM(init_proc_label, &p->p_label); - MAC_DEBUG_COUNTER_INC(&nmacprocs); -} - -static int -mac_init_socket_label(struct label *label, int flag) -{ - int error; - - mac_init_label(label); - - MAC_CHECK(init_socket_label, label, flag); - if (error) { - MAC_PERFORM(destroy_socket_label, label); - mac_destroy_label(label); - } else { - MAC_DEBUG_COUNTER_INC(&nmacsockets); - } - - return (error); -} - -static int -mac_init_socket_peer_label(struct label *label, int flag) -{ - int error; - - mac_init_label(label); - - MAC_CHECK(init_socket_peer_label, label, flag); - if (error) { - MAC_PERFORM(destroy_socket_label, label); - mac_destroy_label(label); - } - - return (error); -} - int -mac_init_socket(struct socket *socket, int flag) -{ - int error; - - error = mac_init_socket_label(&socket->so_label, flag); - if (error) - return (error); - - error = mac_init_socket_peer_label(&socket->so_peerlabel, flag); - if (error) - mac_destroy_socket_label(&socket->so_label); - - return (error); -} - -void -mac_init_vnode_label(struct label *label) -{ - - mac_init_label(label); - MAC_PERFORM(init_vnode_label, label); - MAC_DEBUG_COUNTER_INC(&nmacvnodes); -} - -void -mac_init_vnode(struct vnode *vp) -{ - - mac_init_vnode_label(&vp->v_label); -} - -void -mac_destroy_bpfdesc(struct bpf_d *bpf_d) -{ - - MAC_PERFORM(destroy_bpfdesc_label, &bpf_d->bd_label); - mac_destroy_label(&bpf_d->bd_label); - MAC_DEBUG_COUNTER_DEC(&nmacbpfdescs); -} - -static void -mac_destroy_cred_label(struct label *label) -{ - - MAC_PERFORM(destroy_cred_label, label); - mac_destroy_label(label); - MAC_DEBUG_COUNTER_DEC(&nmaccreds); -} - -void -mac_destroy_cred(struct ucred *cred) -{ - - mac_destroy_cred_label(&cred->cr_label); -} - -void -mac_destroy_devfsdirent(struct devfs_dirent *de) -{ - - MAC_PERFORM(destroy_devfsdirent_label, &de->de_label); - mac_destroy_label(&de->de_label); - MAC_DEBUG_COUNTER_DEC(&nmacdevfsdirents); -} - -static void -mac_destroy_ifnet_label(struct label *label) -{ - - MAC_PERFORM(destroy_ifnet_label, label); - mac_destroy_label(label); - MAC_DEBUG_COUNTER_DEC(&nmacifnets); -} - -void -mac_destroy_ifnet(struct ifnet *ifp) -{ - - mac_destroy_ifnet_label(&ifp->if_label); -} - -void -mac_destroy_ipq(struct ipq *ipq) -{ - - MAC_PERFORM(destroy_ipq_label, &ipq->ipq_label); - mac_destroy_label(&ipq->ipq_label); - MAC_DEBUG_COUNTER_DEC(&nmacipqs); -} - -void -mac_destroy_mbuf_tag(struct m_tag *tag) -{ - struct label *label; - - label = (struct label *)(tag+1); - - MAC_PERFORM(destroy_mbuf_label, label); - mac_destroy_label(label); - MAC_DEBUG_COUNTER_DEC(&nmacmbufs); -} - -void -mac_destroy_mount(struct mount *mp) -{ - - MAC_PERFORM(destroy_mount_label, &mp->mnt_mntlabel); - MAC_PERFORM(destroy_mount_fs_label, &mp->mnt_fslabel); - mac_destroy_label(&mp->mnt_fslabel); - mac_destroy_label(&mp->mnt_mntlabel); - MAC_DEBUG_COUNTER_DEC(&nmacmounts); -} - -static void -mac_destroy_pipe_label(struct label *label) -{ - - MAC_PERFORM(destroy_pipe_label, label); - mac_destroy_label(label); - MAC_DEBUG_COUNTER_DEC(&nmacpipes); -} - -void -mac_destroy_pipe(struct pipe *pipe) -{ - - mac_destroy_pipe_label(pipe->pipe_label); - free(pipe->pipe_label, M_MACPIPELABEL); -} - -void -mac_destroy_proc(struct proc *p) -{ - - MAC_PERFORM(destroy_proc_label, &p->p_label); - mac_destroy_label(&p->p_label); - MAC_DEBUG_COUNTER_DEC(&nmacprocs); -} - -static void -mac_destroy_socket_label(struct label *label) -{ - - MAC_PERFORM(destroy_socket_label, label); - mac_destroy_label(label); - MAC_DEBUG_COUNTER_DEC(&nmacsockets); -} - -static void -mac_destroy_socket_peer_label(struct label *label) -{ - - MAC_PERFORM(destroy_socket_peer_label, label); - mac_destroy_label(label); -} - -void -mac_destroy_socket(struct socket *socket) -{ - - mac_destroy_socket_label(&socket->so_label); - mac_destroy_socket_peer_label(&socket->so_peerlabel); -} - -void -mac_destroy_vnode_label(struct label *label) -{ - - MAC_PERFORM(destroy_vnode_label, label); - mac_destroy_label(label); - MAC_DEBUG_COUNTER_DEC(&nmacvnodes); -} - -void -mac_destroy_vnode(struct vnode *vp) -{ - - mac_destroy_vnode_label(&vp->v_label); -} - -void -mac_copy_mbuf_tag(struct m_tag *src, struct m_tag *dest) -{ - struct label *src_label, *dest_label; - - src_label = (struct label *)(src+1); - dest_label = (struct label *)(dest+1); - - /* - * mac_init_mbuf_tag() is called on the target tag in - * m_tag_copy(), so we don't need to call it here. - */ - MAC_PERFORM(copy_mbuf_label, src_label, dest_label); -} - -static void -mac_copy_pipe_label(struct label *src, struct label *dest) -{ - - MAC_PERFORM(copy_pipe_label, src, dest); -} - -void -mac_copy_vnode_label(struct label *src, struct label *dest) -{ - - MAC_PERFORM(copy_vnode_label, src, dest); -} - -static int mac_check_structmac_consistent(struct mac *mac) { @@ -1176,2102 +521,6 @@ mac_check_structmac_consistent(struct mac *mac) return (0); } -static int -mac_externalize_cred_label(struct label *label, char *elements, - char *outbuf, size_t outbuflen, int flags) -{ - int error; - - MAC_EXTERNALIZE(cred_label, label, elements, outbuf, outbuflen); - - return (error); -} - -static int -mac_externalize_ifnet_label(struct label *label, char *elements, - char *outbuf, size_t outbuflen, int flags) -{ - int error; - - MAC_EXTERNALIZE(ifnet_label, label, elements, outbuf, outbuflen); - - return (error); -} - -static int -mac_externalize_pipe_label(struct label *label, char *elements, - char *outbuf, size_t outbuflen, int flags) -{ - int error; - - MAC_EXTERNALIZE(pipe_label, label, elements, outbuf, outbuflen); - - return (error); -} - -static int -mac_externalize_socket_label(struct label *label, char *elements, - char *outbuf, size_t outbuflen, int flags) -{ - int error; - - MAC_EXTERNALIZE(socket_label, label, elements, outbuf, outbuflen); - - return (error); -} - -static int -mac_externalize_socket_peer_label(struct label *label, char *elements, - char *outbuf, size_t outbuflen, int flags) -{ - int error; - - MAC_EXTERNALIZE(socket_peer_label, label, elements, outbuf, outbuflen); - - return (error); -} - -static int -mac_externalize_vnode_label(struct label *label, char *elements, - char *outbuf, size_t outbuflen, int flags) -{ - int error; - - MAC_EXTERNALIZE(vnode_label, label, elements, outbuf, outbuflen); - - return (error); -} - -static int -mac_internalize_cred_label(struct label *label, char *string) -{ - int error; - - MAC_INTERNALIZE(cred_label, label, string); - - return (error); -} - -static int -mac_internalize_ifnet_label(struct label *label, char *string) -{ - int error; - - MAC_INTERNALIZE(ifnet_label, label, string); - - return (error); -} - -static int -mac_internalize_pipe_label(struct label *label, char *string) -{ - int error; - - MAC_INTERNALIZE(pipe_label, label, string); - - return (error); -} - -static int -mac_internalize_socket_label(struct label *label, char *string) -{ - int error; - - MAC_INTERNALIZE(socket_label, label, string); - - return (error); -} - -static int -mac_internalize_vnode_label(struct label *label, char *string) -{ - int error; - - MAC_INTERNALIZE(vnode_label, label, string); - - return (error); -} - -/* - * Initialize MAC label for the first kernel process, from which other - * kernel processes and threads are spawned. - */ -void -mac_create_proc0(struct ucred *cred) -{ - - MAC_PERFORM(create_proc0, cred); -} - -/* - * Initialize MAC label for the first userland process, from which other - * userland processes and threads are spawned. - */ -void -mac_create_proc1(struct ucred *cred) -{ - - MAC_PERFORM(create_proc1, cred); -} - -void -mac_thread_userret(struct thread *td) -{ - - MAC_PERFORM(thread_userret, td); -} - -/* - * When a new process is created, its label must be initialized. Generally, - * this involves inheritence from the parent process, modulo possible - * deltas. This function allows that processing to take place. - */ -void -mac_create_cred(struct ucred *parent_cred, struct ucred *child_cred) -{ - - MAC_PERFORM(create_cred, parent_cred, child_cred); -} - -void -mac_update_devfsdirent(struct mount *mp, struct devfs_dirent *de, - struct vnode *vp) -{ - - MAC_PERFORM(update_devfsdirent, mp, de, &de->de_label, vp, - &vp->v_label); -} - -void -mac_associate_vnode_devfs(struct mount *mp, struct devfs_dirent *de, - struct vnode *vp) -{ - - MAC_PERFORM(associate_vnode_devfs, mp, &mp->mnt_fslabel, de, - &de->de_label, vp, &vp->v_label); -} - -int -mac_associate_vnode_extattr(struct mount *mp, struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_associate_vnode_extattr"); - - MAC_CHECK(associate_vnode_extattr, mp, &mp->mnt_fslabel, vp, - &vp->v_label); - - return (error); -} - -void -mac_associate_vnode_singlelabel(struct mount *mp, struct vnode *vp) -{ - - MAC_PERFORM(associate_vnode_singlelabel, mp, &mp->mnt_fslabel, vp, - &vp->v_label); -} - -int -mac_create_vnode_extattr(struct ucred *cred, struct mount *mp, - struct vnode *dvp, struct vnode *vp, struct componentname *cnp) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_create_vnode_extattr"); - ASSERT_VOP_LOCKED(vp, "mac_create_vnode_extattr"); - - error = VOP_OPENEXTATTR(vp, cred, curthread); - if (error == EOPNOTSUPP) { - /* XXX: Optionally abort if transactions not supported. */ - if (ea_warn_once == 0) { - printf("Warning: transactions not supported " - "in EA write.\n"); - ea_warn_once = 1; - } - } else if (error) - return (error); - - MAC_CHECK(create_vnode_extattr, cred, mp, &mp->mnt_fslabel, - dvp, &dvp->v_label, vp, &vp->v_label, cnp); - - if (error) { - VOP_CLOSEEXTATTR(vp, 0, NOCRED, curthread); - return (error); - } - - error = VOP_CLOSEEXTATTR(vp, 1, NOCRED, curthread); - - if (error == EOPNOTSUPP) - error = 0; /* XXX */ - - return (error); -} - -static int -mac_setlabel_vnode_extattr(struct ucred *cred, struct vnode *vp, - struct label *intlabel) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_setlabel_vnode_extattr"); - - error = VOP_OPENEXTATTR(vp, cred, curthread); - if (error == EOPNOTSUPP) { - /* XXX: Optionally abort if transactions not supported. */ - if (ea_warn_once == 0) { - printf("Warning: transactions not supported " - "in EA write.\n"); - ea_warn_once = 1; - } - } else if (error) - return (error); - - MAC_CHECK(setlabel_vnode_extattr, cred, vp, &vp->v_label, intlabel); - - if (error) { - VOP_CLOSEEXTATTR(vp, 0, NOCRED, curthread); - return (error); - } - - error = VOP_CLOSEEXTATTR(vp, 1, NOCRED, curthread); - - if (error == EOPNOTSUPP) - error = 0; /* XXX */ - - return (error); -} - -int -mac_execve_enter(struct image_params *imgp, struct mac *mac_p, - struct label *execlabelstorage) -{ - struct mac mac; - char *buffer; - int error; - - if (mac_p == NULL) - return (0); - - error = copyin(mac_p, &mac, sizeof(mac)); - if (error) - return (error); - - error = mac_check_structmac_consistent(&mac); - if (error) - return (error); - - buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK); - error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL); - if (error) { - free(buffer, M_MACTEMP); - return (error); - } - - mac_init_cred_label(execlabelstorage); - error = mac_internalize_cred_label(execlabelstorage, buffer); - free(buffer, M_MACTEMP); - if (error) { - mac_destroy_cred_label(execlabelstorage); - return (error); - } - imgp->execlabel = execlabelstorage; - return (0); -} - -void -mac_execve_exit(struct image_params *imgp) -{ - if (imgp->execlabel != NULL) - mac_destroy_cred_label(imgp->execlabel); -} - -void -mac_execve_transition(struct ucred *old, struct ucred *new, struct vnode *vp, - struct label *interpvnodelabel, struct image_params *imgp) -{ - - ASSERT_VOP_LOCKED(vp, "mac_execve_transition"); - - if (!mac_enforce_process && !mac_enforce_fs) - return; - - MAC_PERFORM(execve_transition, old, new, vp, &vp->v_label, - interpvnodelabel, imgp, imgp->execlabel); -} - -int -mac_execve_will_transition(struct ucred *old, struct vnode *vp, - struct label *interpvnodelabel, struct image_params *imgp) -{ - int result; - - ASSERT_VOP_LOCKED(vp, "mac_execve_will_transition"); - - if (!mac_enforce_process && !mac_enforce_fs) - return (0); - - result = 0; - MAC_BOOLEAN(execve_will_transition, ||, old, vp, &vp->v_label, - interpvnodelabel, imgp, imgp->execlabel); - - return (result); -} - -int -mac_check_vnode_access(struct ucred *cred, struct vnode *vp, int acc_mode) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_access"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_access, cred, vp, &vp->v_label, acc_mode); - return (error); -} - -int -mac_check_vnode_chdir(struct ucred *cred, struct vnode *dvp) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_chdir"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_chdir, cred, dvp, &dvp->v_label); - return (error); -} - -int -mac_check_vnode_chroot(struct ucred *cred, struct vnode *dvp) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_chroot"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_chroot, cred, dvp, &dvp->v_label); - return (error); -} - -int -mac_check_vnode_create(struct ucred *cred, struct vnode *dvp, - struct componentname *cnp, struct vattr *vap) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_create"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_create, cred, dvp, &dvp->v_label, cnp, vap); - return (error); -} - -int -mac_check_vnode_delete(struct ucred *cred, struct vnode *dvp, struct vnode *vp, - struct componentname *cnp) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_delete"); - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_delete"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_delete, cred, dvp, &dvp->v_label, vp, - &vp->v_label, cnp); - return (error); -} - -int -mac_check_vnode_deleteacl(struct ucred *cred, struct vnode *vp, - acl_type_t type) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_deleteacl"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_deleteacl, cred, vp, &vp->v_label, type); - return (error); -} - -int -mac_check_vnode_deleteextattr(struct ucred *cred, struct vnode *vp, - int attrnamespace, const char *name) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_deleteextattr"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_deleteextattr, cred, vp, &vp->v_label, - attrnamespace, name); - return (error); -} - -int -mac_check_vnode_exec(struct ucred *cred, struct vnode *vp, - struct image_params *imgp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_exec"); - - if (!mac_enforce_process && !mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_exec, cred, vp, &vp->v_label, imgp, - imgp->execlabel); - - return (error); -} - -int -mac_check_vnode_getacl(struct ucred *cred, struct vnode *vp, acl_type_t type) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_getacl"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_getacl, cred, vp, &vp->v_label, type); - return (error); -} - -int -mac_check_vnode_getextattr(struct ucred *cred, struct vnode *vp, - int attrnamespace, const char *name, struct uio *uio) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_getextattr"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_getextattr, cred, vp, &vp->v_label, - attrnamespace, name, uio); - return (error); -} - -int -mac_check_vnode_link(struct ucred *cred, struct vnode *dvp, - struct vnode *vp, struct componentname *cnp) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_link"); - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_link"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_link, cred, dvp, &dvp->v_label, vp, - &vp->v_label, cnp); - return (error); -} - -int -mac_check_vnode_listextattr(struct ucred *cred, struct vnode *vp, - int attrnamespace) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_listextattr"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_listextattr, cred, vp, &vp->v_label, - attrnamespace); - return (error); -} - -int -mac_check_vnode_lookup(struct ucred *cred, struct vnode *dvp, - struct componentname *cnp) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_lookup"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_lookup, cred, dvp, &dvp->v_label, cnp); - return (error); -} - -int -mac_check_vnode_mmap(struct ucred *cred, struct vnode *vp, int prot) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mmap"); - - if (!mac_enforce_fs || !mac_enforce_vm) - return (0); - - MAC_CHECK(check_vnode_mmap, cred, vp, &vp->v_label, prot); - return (error); -} - -void -mac_check_vnode_mmap_downgrade(struct ucred *cred, struct vnode *vp, int *prot) -{ - int result = *prot; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mmap_downgrade"); - - if (!mac_enforce_fs || !mac_enforce_vm) - return; - - MAC_PERFORM(check_vnode_mmap_downgrade, cred, vp, &vp->v_label, - &result); - - *prot = result; -} - -int -mac_check_vnode_mprotect(struct ucred *cred, struct vnode *vp, int prot) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mprotect"); - - if (!mac_enforce_fs || !mac_enforce_vm) - return (0); - - MAC_CHECK(check_vnode_mprotect, cred, vp, &vp->v_label, prot); - return (error); -} - -int -mac_check_vnode_open(struct ucred *cred, struct vnode *vp, int acc_mode) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_open"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_open, cred, vp, &vp->v_label, acc_mode); - return (error); -} - -int -mac_check_vnode_poll(struct ucred *active_cred, struct ucred *file_cred, - struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_poll"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_poll, active_cred, file_cred, vp, - &vp->v_label); - - return (error); -} - -int -mac_check_vnode_read(struct ucred *active_cred, struct ucred *file_cred, - struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_read"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_read, active_cred, file_cred, vp, - &vp->v_label); - - return (error); -} - -int -mac_check_vnode_readdir(struct ucred *cred, struct vnode *dvp) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_readdir"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_readdir, cred, dvp, &dvp->v_label); - return (error); -} - -int -mac_check_vnode_readlink(struct ucred *cred, struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_readlink"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_readlink, cred, vp, &vp->v_label); - return (error); -} - -static int -mac_check_vnode_relabel(struct ucred *cred, struct vnode *vp, - struct label *newlabel) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_relabel"); - - MAC_CHECK(check_vnode_relabel, cred, vp, &vp->v_label, newlabel); - - return (error); -} - -int -mac_check_vnode_rename_from(struct ucred *cred, struct vnode *dvp, - struct vnode *vp, struct componentname *cnp) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_rename_from"); - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_rename_from"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_rename_from, cred, dvp, &dvp->v_label, vp, - &vp->v_label, cnp); - return (error); -} - -int -mac_check_vnode_rename_to(struct ucred *cred, struct vnode *dvp, - struct vnode *vp, int samedir, struct componentname *cnp) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_rename_to"); - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_rename_to"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_rename_to, cred, dvp, &dvp->v_label, vp, - vp != NULL ? &vp->v_label : NULL, samedir, cnp); - return (error); -} - -int -mac_check_vnode_revoke(struct ucred *cred, struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_revoke"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_revoke, cred, vp, &vp->v_label); - return (error); -} - -int -mac_check_vnode_setacl(struct ucred *cred, struct vnode *vp, acl_type_t type, - struct acl *acl) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setacl"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_setacl, cred, vp, &vp->v_label, type, acl); - return (error); -} - -int -mac_check_vnode_setextattr(struct ucred *cred, struct vnode *vp, - int attrnamespace, const char *name, struct uio *uio) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setextattr"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_setextattr, cred, vp, &vp->v_label, - attrnamespace, name, uio); - return (error); -} - -int -mac_check_vnode_setflags(struct ucred *cred, struct vnode *vp, u_long flags) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setflags"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_setflags, cred, vp, &vp->v_label, flags); - return (error); -} - -int -mac_check_vnode_setmode(struct ucred *cred, struct vnode *vp, mode_t mode) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setmode"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_setmode, cred, vp, &vp->v_label, mode); - return (error); -} - -int -mac_check_vnode_setowner(struct ucred *cred, struct vnode *vp, uid_t uid, - gid_t gid) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setowner"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_setowner, cred, vp, &vp->v_label, uid, gid); - return (error); -} - -int -mac_check_vnode_setutimes(struct ucred *cred, struct vnode *vp, - struct timespec atime, struct timespec mtime) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setutimes"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_setutimes, cred, vp, &vp->v_label, atime, - mtime); - return (error); -} - -int -mac_check_vnode_stat(struct ucred *active_cred, struct ucred *file_cred, - struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_stat"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_stat, active_cred, file_cred, vp, - &vp->v_label); - return (error); -} - -int -mac_check_vnode_write(struct ucred *active_cred, struct ucred *file_cred, - struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_write"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_write, active_cred, file_cred, vp, - &vp->v_label); - - return (error); -} - -/* - * When relabeling a process, call out to the policies for the maximum - * permission allowed for each object type we know about in its - * memory space, and revoke access (in the least surprising ways we - * know) when necessary. The process lock is not held here. - */ -void -mac_cred_mmapped_drop_perms(struct thread *td, struct ucred *cred) -{ - - /* XXX freeze all other threads */ - mac_cred_mmapped_drop_perms_recurse(td, cred, - &td->td_proc->p_vmspace->vm_map); - /* XXX allow other threads to continue */ -} - -static __inline const char * -prot2str(vm_prot_t prot) -{ - - switch (prot & VM_PROT_ALL) { - case VM_PROT_READ: - return ("r--"); - case VM_PROT_READ | VM_PROT_WRITE: - return ("rw-"); - case VM_PROT_READ | VM_PROT_EXECUTE: - return ("r-x"); - case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE: - return ("rwx"); - case VM_PROT_WRITE: - return ("-w-"); - case VM_PROT_EXECUTE: - return ("--x"); - case VM_PROT_WRITE | VM_PROT_EXECUTE: - return ("-wx"); - default: - return ("---"); - } -} - -static void -mac_cred_mmapped_drop_perms_recurse(struct thread *td, struct ucred *cred, - struct vm_map *map) -{ - struct vm_map_entry *vme; - int result; - vm_prot_t revokeperms; - vm_object_t object; - vm_ooffset_t offset; - struct vnode *vp; - - if (!mac_mmap_revocation) - return; - - vm_map_lock_read(map); - for (vme = map->header.next; vme != &map->header; vme = vme->next) { - if (vme->eflags & MAP_ENTRY_IS_SUB_MAP) { - mac_cred_mmapped_drop_perms_recurse(td, cred, - vme->object.sub_map); - continue; - } - /* - * Skip over entries that obviously are not shared. - */ - if (vme->eflags & (MAP_ENTRY_COW | MAP_ENTRY_NOSYNC) || - !vme->max_protection) - continue; - /* - * Drill down to the deepest backing object. - */ - offset = vme->offset; - object = vme->object.vm_object; - if (object == NULL) - continue; - while (object->backing_object != NULL) { - object = object->backing_object; - offset += object->backing_object_offset; - } - /* - * At the moment, vm_maps and objects aren't considered - * by the MAC system, so only things with backing by a - * normal object (read: vnodes) are checked. - */ - if (object->type != OBJT_VNODE) - continue; - vp = (struct vnode *)object->handle; - vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); - result = vme->max_protection; - mac_check_vnode_mmap_downgrade(cred, vp, &result); - VOP_UNLOCK(vp, 0, td); - /* - * Find out what maximum protection we may be allowing - * now but a policy needs to get removed. - */ - revokeperms = vme->max_protection & ~result; - if (!revokeperms) - continue; - printf("pid %ld: revoking %s perms from %#lx:%ld " - "(max %s/cur %s)\n", (long)td->td_proc->p_pid, - prot2str(revokeperms), (u_long)vme->start, - (long)(vme->end - vme->start), - prot2str(vme->max_protection), prot2str(vme->protection)); - vm_map_lock_upgrade(map); - /* - * This is the really simple case: if a map has more - * max_protection than is allowed, but it's not being - * actually used (that is, the current protection is - * still allowed), we can just wipe it out and do - * nothing more. - */ - if ((vme->protection & revokeperms) == 0) { - vme->max_protection -= revokeperms; - } else { - if (revokeperms & VM_PROT_WRITE) { - /* - * In the more complicated case, flush out all - * pending changes to the object then turn it - * copy-on-write. - */ - vm_object_reference(object); - vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); - VM_OBJECT_LOCK(object); - vm_object_page_clean(object, - OFF_TO_IDX(offset), - OFF_TO_IDX(offset + vme->end - vme->start + - PAGE_MASK), - OBJPC_SYNC); - VM_OBJECT_UNLOCK(object); - VOP_UNLOCK(vp, 0, td); - vm_object_deallocate(object); - /* - * Why bother if there's no read permissions - * anymore? For the rest, we need to leave - * the write permissions on for COW, or - * remove them entirely if configured to. - */ - if (!mac_mmap_revocation_via_cow) { - vme->max_protection &= ~VM_PROT_WRITE; - vme->protection &= ~VM_PROT_WRITE; - } if ((revokeperms & VM_PROT_READ) == 0) - vme->eflags |= MAP_ENTRY_COW | - MAP_ENTRY_NEEDS_COPY; - } - if (revokeperms & VM_PROT_EXECUTE) { - vme->max_protection &= ~VM_PROT_EXECUTE; - vme->protection &= ~VM_PROT_EXECUTE; - } - if (revokeperms & VM_PROT_READ) { - vme->max_protection = 0; - vme->protection = 0; - } - pmap_protect(map->pmap, vme->start, vme->end, - vme->protection & ~revokeperms); - vm_map_simplify_entry(map, vme); - } - vm_map_lock_downgrade(map); - } - vm_map_unlock_read(map); -} - -/* - * When the subject's label changes, it may require revocation of privilege - * to mapped objects. This can't be done on-the-fly later with a unified - * buffer cache. - */ -static void -mac_relabel_cred(struct ucred *cred, struct label *newlabel) -{ - - MAC_PERFORM(relabel_cred, cred, newlabel); -} - -void -mac_relabel_vnode(struct ucred *cred, struct vnode *vp, struct label *newlabel) -{ - - MAC_PERFORM(relabel_vnode, cred, vp, &vp->v_label, newlabel); -} - -void -mac_create_ifnet(struct ifnet *ifnet) -{ - - MAC_PERFORM(create_ifnet, ifnet, &ifnet->if_label); -} - -void -mac_create_bpfdesc(struct ucred *cred, struct bpf_d *bpf_d) -{ - - MAC_PERFORM(create_bpfdesc, cred, bpf_d, &bpf_d->bd_label); -} - -void -mac_create_socket(struct ucred *cred, struct socket *socket) -{ - - MAC_PERFORM(create_socket, cred, socket, &socket->so_label); -} - -void -mac_create_pipe(struct ucred *cred, struct pipe *pipe) -{ - - MAC_PERFORM(create_pipe, cred, pipe, pipe->pipe_label); -} - -void -mac_create_socket_from_socket(struct socket *oldsocket, - struct socket *newsocket) -{ - - MAC_PERFORM(create_socket_from_socket, oldsocket, &oldsocket->so_label, - newsocket, &newsocket->so_label); -} - -static void -mac_relabel_socket(struct ucred *cred, struct socket *socket, - struct label *newlabel) -{ - - MAC_PERFORM(relabel_socket, cred, socket, &socket->so_label, newlabel); -} - -static void -mac_relabel_pipe(struct ucred *cred, struct pipe *pipe, struct label *newlabel) -{ - - MAC_PERFORM(relabel_pipe, cred, pipe, pipe->pipe_label, newlabel); -} - -void -mac_set_socket_peer_from_mbuf(struct mbuf *mbuf, struct socket *socket) -{ - struct label *label; - - label = mbuf_to_label(mbuf); - - MAC_PERFORM(set_socket_peer_from_mbuf, mbuf, label, socket, - &socket->so_peerlabel); -} - -void -mac_set_socket_peer_from_socket(struct socket *oldsocket, - struct socket *newsocket) -{ - - MAC_PERFORM(set_socket_peer_from_socket, oldsocket, - &oldsocket->so_label, newsocket, &newsocket->so_peerlabel); -} - -void -mac_create_datagram_from_ipq(struct ipq *ipq, struct mbuf *datagram) -{ - struct label *label; - - label = mbuf_to_label(datagram); - - MAC_PERFORM(create_datagram_from_ipq, ipq, &ipq->ipq_label, - datagram, label); -} - -void -mac_create_fragment(struct mbuf *datagram, struct mbuf *fragment) -{ - struct label *datagramlabel, *fragmentlabel; - - datagramlabel = mbuf_to_label(datagram); - fragmentlabel = mbuf_to_label(fragment); - - MAC_PERFORM(create_fragment, datagram, datagramlabel, fragment, - fragmentlabel); -} - -void -mac_create_ipq(struct mbuf *fragment, struct ipq *ipq) -{ - struct label *label; - - label = mbuf_to_label(fragment); - - MAC_PERFORM(create_ipq, fragment, label, ipq, &ipq->ipq_label); -} - -void -mac_create_mbuf_from_mbuf(struct mbuf *oldmbuf, struct mbuf *newmbuf) -{ - struct label *oldmbuflabel, *newmbuflabel; - - oldmbuflabel = mbuf_to_label(oldmbuf); - newmbuflabel = mbuf_to_label(newmbuf); - - MAC_PERFORM(create_mbuf_from_mbuf, oldmbuf, oldmbuflabel, newmbuf, - newmbuflabel); -} - -void -mac_create_mbuf_from_bpfdesc(struct bpf_d *bpf_d, struct mbuf *mbuf) -{ - struct label *label; - - label = mbuf_to_label(mbuf); - - MAC_PERFORM(create_mbuf_from_bpfdesc, bpf_d, &bpf_d->bd_label, mbuf, - label); -} - -void -mac_create_mbuf_linklayer(struct ifnet *ifnet, struct mbuf *mbuf) -{ - struct label *label; - - label = mbuf_to_label(mbuf); - - MAC_PERFORM(create_mbuf_linklayer, ifnet, &ifnet->if_label, mbuf, - label); -} - -void -mac_create_mbuf_from_ifnet(struct ifnet *ifnet, struct mbuf *mbuf) -{ - struct label *label; - - label = mbuf_to_label(mbuf); - - MAC_PERFORM(create_mbuf_from_ifnet, ifnet, &ifnet->if_label, mbuf, - label); -} - -void -mac_create_mbuf_multicast_encap(struct mbuf *oldmbuf, struct ifnet *ifnet, - struct mbuf *newmbuf) -{ - struct label *oldmbuflabel, *newmbuflabel; - - oldmbuflabel = mbuf_to_label(oldmbuf); - newmbuflabel = mbuf_to_label(newmbuf); - - MAC_PERFORM(create_mbuf_multicast_encap, oldmbuf, oldmbuflabel, - ifnet, &ifnet->if_label, newmbuf, newmbuflabel); -} - -void -mac_create_mbuf_netlayer(struct mbuf *oldmbuf, struct mbuf *newmbuf) -{ - struct label *oldmbuflabel, *newmbuflabel; - - oldmbuflabel = mbuf_to_label(oldmbuf); - newmbuflabel = mbuf_to_label(newmbuf); - - MAC_PERFORM(create_mbuf_netlayer, oldmbuf, oldmbuflabel, newmbuf, - newmbuflabel); -} - -int -mac_fragment_match(struct mbuf *fragment, struct ipq *ipq) -{ - struct label *label; - int result; - - label = mbuf_to_label(fragment); - - result = 1; - MAC_BOOLEAN(fragment_match, &&, fragment, label, ipq, - &ipq->ipq_label); - - return (result); -} - -void -mac_reflect_mbuf_icmp(struct mbuf *m) -{ - struct label *label; - - label = mbuf_to_label(m); - - MAC_PERFORM(reflect_mbuf_icmp, m, label); -} -void -mac_reflect_mbuf_tcp(struct mbuf *m) -{ - struct label *label; - - label = mbuf_to_label(m); - - MAC_PERFORM(reflect_mbuf_tcp, m, label); -} - -void -mac_update_ipq(struct mbuf *fragment, struct ipq *ipq) -{ - struct label *label; - - label = mbuf_to_label(fragment); - - MAC_PERFORM(update_ipq, fragment, label, ipq, &ipq->ipq_label); -} - -void -mac_create_mbuf_from_socket(struct socket *socket, struct mbuf *mbuf) -{ - struct label *label; - - label = mbuf_to_label(mbuf); - - MAC_PERFORM(create_mbuf_from_socket, socket, &socket->so_label, mbuf, - label); -} - -void -mac_create_mount(struct ucred *cred, struct mount *mp) -{ - - MAC_PERFORM(create_mount, cred, mp, &mp->mnt_mntlabel, - &mp->mnt_fslabel); -} - -void -mac_create_root_mount(struct ucred *cred, struct mount *mp) -{ - - MAC_PERFORM(create_root_mount, cred, mp, &mp->mnt_mntlabel, - &mp->mnt_fslabel); -} - -int -mac_check_bpfdesc_receive(struct bpf_d *bpf_d, struct ifnet *ifnet) -{ - int error; - - if (!mac_enforce_network) - return (0); - - MAC_CHECK(check_bpfdesc_receive, bpf_d, &bpf_d->bd_label, ifnet, - &ifnet->if_label); - - return (error); -} - -static int -mac_check_cred_relabel(struct ucred *cred, struct label *newlabel) -{ - int error; - - MAC_CHECK(check_cred_relabel, cred, newlabel); - - return (error); -} - -int -mac_check_cred_visible(struct ucred *u1, struct ucred *u2) -{ - int error; - - if (!mac_enforce_process) - return (0); - - MAC_CHECK(check_cred_visible, u1, u2); - - return (error); -} - -int -mac_check_ifnet_transmit(struct ifnet *ifnet, struct mbuf *mbuf) -{ - struct label *label; - int error; - - M_ASSERTPKTHDR(mbuf); - - if (!mac_enforce_network) - return (0); - - label = mbuf_to_label(mbuf); - - MAC_CHECK(check_ifnet_transmit, ifnet, &ifnet->if_label, mbuf, - label); - - return (error); -} - -int -mac_check_kenv_dump(struct ucred *cred) -{ - int error; - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_kenv_dump, cred); - - return (error); -} - -int -mac_check_kenv_get(struct ucred *cred, char *name) -{ - int error; - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_kenv_get, cred, name); - - return (error); -} - -int -mac_check_kenv_set(struct ucred *cred, char *name, char *value) -{ - int error; - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_kenv_set, cred, name, value); - - return (error); -} - -int -mac_check_kenv_unset(struct ucred *cred, char *name) -{ - int error; - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_kenv_unset, cred, name); - - return (error); -} - -int -mac_check_kld_load(struct ucred *cred, struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_kld_load"); - - if (!mac_enforce_kld) - return (0); - - MAC_CHECK(check_kld_load, cred, vp, &vp->v_label); - - return (error); -} - -int -mac_check_kld_stat(struct ucred *cred) -{ - int error; - - if (!mac_enforce_kld) - return (0); - - MAC_CHECK(check_kld_stat, cred); - - return (error); -} - -int -mac_check_kld_unload(struct ucred *cred) -{ - int error; - - if (!mac_enforce_kld) - return (0); - - MAC_CHECK(check_kld_unload, cred); - - return (error); -} - -int -mac_check_mount_stat(struct ucred *cred, struct mount *mount) -{ - int error; - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_mount_stat, cred, mount, &mount->mnt_mntlabel); - - return (error); -} - -int -mac_check_pipe_ioctl(struct ucred *cred, struct pipe *pipe, unsigned long cmd, - void *data) -{ - int error; - - PIPE_LOCK_ASSERT(pipe, MA_OWNED); - - if (!mac_enforce_pipe) - return (0); - - MAC_CHECK(check_pipe_ioctl, cred, pipe, pipe->pipe_label, cmd, data); - - return (error); -} - -int -mac_check_pipe_poll(struct ucred *cred, struct pipe *pipe) -{ - int error; - - PIPE_LOCK_ASSERT(pipe, MA_OWNED); - - if (!mac_enforce_pipe) - return (0); - - MAC_CHECK(check_pipe_poll, cred, pipe, pipe->pipe_label); - - return (error); -} - -int -mac_check_pipe_read(struct ucred *cred, struct pipe *pipe) -{ - int error; - - PIPE_LOCK_ASSERT(pipe, MA_OWNED); - - if (!mac_enforce_pipe) - return (0); - - MAC_CHECK(check_pipe_read, cred, pipe, pipe->pipe_label); - - return (error); -} - -static int -mac_check_pipe_relabel(struct ucred *cred, struct pipe *pipe, - struct label *newlabel) -{ - int error; - - PIPE_LOCK_ASSERT(pipe, MA_OWNED); - - if (!mac_enforce_pipe) - return (0); - - MAC_CHECK(check_pipe_relabel, cred, pipe, pipe->pipe_label, newlabel); - - return (error); -} - -int -mac_check_pipe_stat(struct ucred *cred, struct pipe *pipe) -{ - int error; - - PIPE_LOCK_ASSERT(pipe, MA_OWNED); - - if (!mac_enforce_pipe) - return (0); - - MAC_CHECK(check_pipe_stat, cred, pipe, pipe->pipe_label); - - return (error); -} - -int -mac_check_pipe_write(struct ucred *cred, struct pipe *pipe) -{ - int error; - - PIPE_LOCK_ASSERT(pipe, MA_OWNED); - - if (!mac_enforce_pipe) - return (0); - - MAC_CHECK(check_pipe_write, cred, pipe, pipe->pipe_label); - - return (error); -} - -int -mac_check_proc_debug(struct ucred *cred, struct proc *proc) -{ - int error; - - PROC_LOCK_ASSERT(proc, MA_OWNED); - - if (!mac_enforce_process) - return (0); - - MAC_CHECK(check_proc_debug, cred, proc); - - return (error); -} - -int -mac_check_proc_sched(struct ucred *cred, struct proc *proc) -{ - int error; - - PROC_LOCK_ASSERT(proc, MA_OWNED); - - if (!mac_enforce_process) - return (0); - - MAC_CHECK(check_proc_sched, cred, proc); - - return (error); -} - -int -mac_check_proc_signal(struct ucred *cred, struct proc *proc, int signum) -{ - int error; - - PROC_LOCK_ASSERT(proc, MA_OWNED); - - if (!mac_enforce_process) - return (0); - - MAC_CHECK(check_proc_signal, cred, proc, signum); - - return (error); -} - -int -mac_check_socket_bind(struct ucred *ucred, struct socket *socket, - struct sockaddr *sockaddr) -{ - int error; - - if (!mac_enforce_socket) - return (0); - - MAC_CHECK(check_socket_bind, ucred, socket, &socket->so_label, - sockaddr); - - return (error); -} - -int -mac_check_socket_connect(struct ucred *cred, struct socket *socket, - struct sockaddr *sockaddr) -{ - int error; - - if (!mac_enforce_socket) - return (0); - - MAC_CHECK(check_socket_connect, cred, socket, &socket->so_label, - sockaddr); - - return (error); -} - -int -mac_check_socket_deliver(struct socket *socket, struct mbuf *mbuf) -{ - struct label *label; - int error; - - if (!mac_enforce_socket) - return (0); - - label = mbuf_to_label(mbuf); - - MAC_CHECK(check_socket_deliver, socket, &socket->so_label, mbuf, - label); - - return (error); -} - -int -mac_check_socket_listen(struct ucred *cred, struct socket *socket) -{ - int error; - - if (!mac_enforce_socket) - return (0); - - MAC_CHECK(check_socket_listen, cred, socket, &socket->so_label); - return (error); -} - -int -mac_check_socket_receive(struct ucred *cred, struct socket *so) -{ - int error; - - if (!mac_enforce_socket) - return (0); - - MAC_CHECK(check_socket_receive, cred, so, &so->so_label); - - return (error); -} - -static int -mac_check_socket_relabel(struct ucred *cred, struct socket *socket, - struct label *newlabel) -{ - int error; - - MAC_CHECK(check_socket_relabel, cred, socket, &socket->so_label, - newlabel); - - return (error); -} - -int -mac_check_socket_send(struct ucred *cred, struct socket *so) -{ - int error; - - if (!mac_enforce_socket) - return (0); - - MAC_CHECK(check_socket_send, cred, so, &so->so_label); - - return (error); -} - -int -mac_check_socket_visible(struct ucred *cred, struct socket *socket) -{ - int error; - - if (!mac_enforce_socket) - return (0); - - MAC_CHECK(check_socket_visible, cred, socket, &socket->so_label); - - return (error); -} - -int -mac_check_sysarch_ioperm(struct ucred *cred) -{ - int error; - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_sysarch_ioperm, cred); - return (error); -} - -int -mac_check_system_acct(struct ucred *cred, struct vnode *vp) -{ - int error; - - if (vp != NULL) { - ASSERT_VOP_LOCKED(vp, "mac_check_system_acct"); - } - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_system_acct, cred, vp, - vp != NULL ? &vp->v_label : NULL); - - return (error); -} - -int -mac_check_system_nfsd(struct ucred *cred) -{ - int error; - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_system_nfsd, cred); - - return (error); -} - -int -mac_check_system_reboot(struct ucred *cred, int howto) -{ - int error; - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_system_reboot, cred, howto); - - return (error); -} - -int -mac_check_system_settime(struct ucred *cred) -{ - int error; - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_system_settime, cred); - - return (error); -} - -int -mac_check_system_swapon(struct ucred *cred, struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_system_swapon"); - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_system_swapon, cred, vp, &vp->v_label); - return (error); -} - -int -mac_check_system_swapoff(struct ucred *cred, struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_system_swapoff"); - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_system_swapoff, cred, vp, &vp->v_label); - return (error); -} - -int -mac_check_system_sysctl(struct ucred *cred, int *name, u_int namelen, - void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen) -{ - int error; - - /* - * XXXMAC: We're very much like to assert the SYSCTL_LOCK here, - * but since it's not exported from kern_sysctl.c, we can't. - */ - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_system_sysctl, cred, name, namelen, old, oldlenp, - inkernel, new, newlen); - - return (error); -} - -int -mac_ioctl_ifnet_get(struct ucred *cred, struct ifreq *ifr, - struct ifnet *ifnet) -{ - char *elements, *buffer; - struct mac mac; - int error; - - error = copyin(ifr->ifr_ifru.ifru_data, &mac, sizeof(mac)); - if (error) - return (error); - - error = mac_check_structmac_consistent(&mac); - if (error) - return (error); - - elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK); - error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL); - if (error) { - free(elements, M_MACTEMP); - return (error); - } - - buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO); - error = mac_externalize_ifnet_label(&ifnet->if_label, elements, - buffer, mac.m_buflen, M_WAITOK); - if (error == 0) - error = copyout(buffer, mac.m_string, strlen(buffer)+1); - - free(buffer, M_MACTEMP); - free(elements, M_MACTEMP); - - return (error); -} - -int -mac_ioctl_ifnet_set(struct ucred *cred, struct ifreq *ifr, - struct ifnet *ifnet) -{ - struct label intlabel; - struct mac mac; - char *buffer; - int error; - - error = copyin(ifr->ifr_ifru.ifru_data, &mac, sizeof(mac)); - if (error) - return (error); - - error = mac_check_structmac_consistent(&mac); - if (error) - return (error); - - buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK); - error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL); - if (error) { - free(buffer, M_MACTEMP); - return (error); - } - - mac_init_ifnet_label(&intlabel); - error = mac_internalize_ifnet_label(&intlabel, buffer); - free(buffer, M_MACTEMP); - if (error) { - mac_destroy_ifnet_label(&intlabel); - return (error); - } - - /* - * XXX: Note that this is a redundant privilege check, since - * policies impose this check themselves if required by the - * policy. Eventually, this should go away. - */ - error = suser_cred(cred, 0); - if (error) { - mac_destroy_ifnet_label(&intlabel); - return (error); - } - - MAC_CHECK(check_ifnet_relabel, cred, ifnet, &ifnet->if_label, - &intlabel); - if (error) { - mac_destroy_ifnet_label(&intlabel); - return (error); - } - - MAC_PERFORM(relabel_ifnet, cred, ifnet, &ifnet->if_label, &intlabel); - - mac_destroy_ifnet_label(&intlabel); - return (0); -} - -void -mac_create_devfs_device(struct mount *mp, dev_t dev, struct devfs_dirent *de) -{ - - MAC_PERFORM(create_devfs_device, mp, dev, de, &de->de_label); -} - -void -mac_create_devfs_symlink(struct ucred *cred, struct mount *mp, - struct devfs_dirent *dd, struct devfs_dirent *de) -{ - - MAC_PERFORM(create_devfs_symlink, cred, mp, dd, &dd->de_label, de, - &de->de_label); -} - -void -mac_create_devfs_directory(struct mount *mp, char *dirname, int dirnamelen, - struct devfs_dirent *de) -{ - - MAC_PERFORM(create_devfs_directory, mp, dirname, dirnamelen, de, - &de->de_label); -} - -int -mac_setsockopt_label_set(struct ucred *cred, struct socket *so, - struct mac *mac) -{ - struct label intlabel; - char *buffer; - int error; - - error = mac_check_structmac_consistent(mac); - if (error) - return (error); - - buffer = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK); - error = copyinstr(mac->m_string, buffer, mac->m_buflen, NULL); - if (error) { - free(buffer, M_MACTEMP); - return (error); - } - - mac_init_socket_label(&intlabel, M_WAITOK); - error = mac_internalize_socket_label(&intlabel, buffer); - free(buffer, M_MACTEMP); - if (error) { - mac_destroy_socket_label(&intlabel); - return (error); - } - - mac_check_socket_relabel(cred, so, &intlabel); - if (error) { - mac_destroy_socket_label(&intlabel); - return (error); - } - - mac_relabel_socket(cred, so, &intlabel); - - mac_destroy_socket_label(&intlabel); - return (0); -} - -int -mac_pipe_label_set(struct ucred *cred, struct pipe *pipe, struct label *label) -{ - int error; - - PIPE_LOCK_ASSERT(pipe, MA_OWNED); - - error = mac_check_pipe_relabel(cred, pipe, label); - if (error) - return (error); - - mac_relabel_pipe(cred, pipe, label); - - return (0); -} - -int -mac_getsockopt_label_get(struct ucred *cred, struct socket *so, - struct mac *mac) -{ - char *buffer, *elements; - int error; - - error = mac_check_structmac_consistent(mac); - if (error) - return (error); - - elements = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK); - error = copyinstr(mac->m_string, elements, mac->m_buflen, NULL); - if (error) { - free(elements, M_MACTEMP); - return (error); - } - - buffer = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK | M_ZERO); - error = mac_externalize_socket_label(&so->so_label, elements, - buffer, mac->m_buflen, M_WAITOK); - if (error == 0) - error = copyout(buffer, mac->m_string, strlen(buffer)+1); - - free(buffer, M_MACTEMP); - free(elements, M_MACTEMP); - - return (error); -} - -int -mac_getsockopt_peerlabel_get(struct ucred *cred, struct socket *so, - struct mac *mac) -{ - char *elements, *buffer; - int error; - - error = mac_check_structmac_consistent(mac); - if (error) - return (error); - - elements = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK); - error = copyinstr(mac->m_string, elements, mac->m_buflen, NULL); - if (error) { - free(elements, M_MACTEMP); - return (error); - } - - buffer = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK | M_ZERO); - error = mac_externalize_socket_peer_label(&so->so_peerlabel, - elements, buffer, mac->m_buflen, M_WAITOK); - if (error == 0) - error = copyout(buffer, mac->m_string, strlen(buffer)+1); - - free(buffer, M_MACTEMP); - free(elements, M_MACTEMP); - - return (error); -} - -/* - * Implementation of VOP_SETLABEL() that relies on extended attributes - * to store label data. Can be referenced by filesystems supporting - * extended attributes. - */ -int -vop_stdsetlabel_ea(struct vop_setlabel_args *ap) -{ - struct vnode *vp = ap->a_vp; - struct label *intlabel = ap->a_label; - int error; - - ASSERT_VOP_LOCKED(vp, "vop_stdsetlabel_ea"); - - if ((vp->v_mount->mnt_flag & MNT_MULTILABEL) == 0) - return (EOPNOTSUPP); - - error = mac_setlabel_vnode_extattr(ap->a_cred, vp, intlabel); - if (error) - return (error); - - mac_relabel_vnode(ap->a_cred, vp, intlabel); - - return (0); -} - -static int -vn_setlabel(struct vnode *vp, struct label *intlabel, struct ucred *cred) -{ - int error; - - if (vp->v_mount == NULL) { - /* printf("vn_setlabel: null v_mount\n"); */ - if (vp->v_type != VNON) - printf("vn_setlabel: null v_mount with non-VNON\n"); - return (EBADF); - } - - if ((vp->v_mount->mnt_flag & MNT_MULTILABEL) == 0) - return (EOPNOTSUPP); - - /* - * Multi-phase commit. First check the policies to confirm the - * change is OK. Then commit via the filesystem. Finally, - * update the actual vnode label. Question: maybe the filesystem - * should update the vnode at the end as part of VOP_SETLABEL()? - */ - error = mac_check_vnode_relabel(cred, vp, intlabel); - if (error) - return (error); - - /* - * VADMIN provides the opportunity for the filesystem to make - * decisions about who is and is not able to modify labels - * and protections on files. This might not be right. We can't - * assume VOP_SETLABEL() will do it, because we might implement - * that as part of vop_stdsetlabel_ea(). - */ - error = VOP_ACCESS(vp, VADMIN, cred, curthread); - if (error) - return (error); - - error = VOP_SETLABEL(vp, intlabel, cred, curthread); - if (error) - return (error); - - return (0); -} - int __mac_get_pid(struct thread *td, struct __mac_get_pid_args *uap) { diff --git a/sys/security/mac/mac_syscalls.c b/sys/security/mac/mac_syscalls.c index 35c9c9b..aae8a24 100644 --- a/sys/security/mac/mac_syscalls.c +++ b/sys/security/mac/mac_syscalls.c @@ -85,6 +85,8 @@ __FBSDID("$FreeBSD$"); #include <netinet/in.h> #include <netinet/ip_var.h> +#include <security/mac/mac_internal.h> + #ifdef MAC /* @@ -94,8 +96,6 @@ __FBSDID("$FreeBSD$"); */ MODULE_VERSION(kernel_mac_support, 1); -SYSCTL_DECL(_security); - SYSCTL_NODE(_security, OID_AUTO, mac, CTLFLAG_RW, 0, "TrustedBSD MAC policy controls"); @@ -130,119 +130,20 @@ int mac_late = 0; int mac_labelmbufs = 0; #endif -static int mac_enforce_fs = 1; -SYSCTL_INT(_security_mac, OID_AUTO, enforce_fs, CTLFLAG_RW, - &mac_enforce_fs, 0, "Enforce MAC policy on file system objects"); -TUNABLE_INT("security.mac.enforce_fs", &mac_enforce_fs); - -static int mac_enforce_kld = 1; -SYSCTL_INT(_security_mac, OID_AUTO, enforce_kld, CTLFLAG_RW, - &mac_enforce_kld, 0, "Enforce MAC policy on kld operations"); -TUNABLE_INT("security.mac.enforce_kld", &mac_enforce_kld); - -static int mac_enforce_network = 1; -SYSCTL_INT(_security_mac, OID_AUTO, enforce_network, CTLFLAG_RW, - &mac_enforce_network, 0, "Enforce MAC policy on network packets"); -TUNABLE_INT("security.mac.enforce_network", &mac_enforce_network); - -static int mac_enforce_pipe = 1; -SYSCTL_INT(_security_mac, OID_AUTO, enforce_pipe, CTLFLAG_RW, - &mac_enforce_pipe, 0, "Enforce MAC policy on pipe operations"); -TUNABLE_INT("security.mac.enforce_pipe", &mac_enforce_pipe); - -static int mac_enforce_process = 1; -SYSCTL_INT(_security_mac, OID_AUTO, enforce_process, CTLFLAG_RW, - &mac_enforce_process, 0, "Enforce MAC policy on inter-process operations"); -TUNABLE_INT("security.mac.enforce_process", &mac_enforce_process); - -static int mac_enforce_socket = 1; -SYSCTL_INT(_security_mac, OID_AUTO, enforce_socket, CTLFLAG_RW, - &mac_enforce_socket, 0, "Enforce MAC policy on socket operations"); -TUNABLE_INT("security.mac.enforce_socket", &mac_enforce_socket); - -static int mac_enforce_system = 1; -SYSCTL_INT(_security_mac, OID_AUTO, enforce_system, CTLFLAG_RW, - &mac_enforce_system, 0, "Enforce MAC policy on system operations"); -TUNABLE_INT("security.mac.enforce_system", &mac_enforce_system); - -static int mac_enforce_vm = 1; -SYSCTL_INT(_security_mac, OID_AUTO, enforce_vm, CTLFLAG_RW, - &mac_enforce_vm, 0, "Enforce MAC policy on vm operations"); -TUNABLE_INT("security.mac.enforce_vm", &mac_enforce_vm); - -static int mac_mmap_revocation = 1; -SYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation, CTLFLAG_RW, - &mac_mmap_revocation, 0, "Revoke mmap access to files on subject " - "relabel"); -static int mac_mmap_revocation_via_cow = 0; -SYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation_via_cow, CTLFLAG_RW, - &mac_mmap_revocation_via_cow, 0, "Revoke mmap access to files via " - "copy-on-write semantics, or by removing all write access"); - #ifdef MAC_DEBUG SYSCTL_NODE(_security_mac, OID_AUTO, debug, CTLFLAG_RW, 0, "TrustedBSD MAC debug info"); - -static int mac_debug_label_fallback = 0; -SYSCTL_INT(_security_mac_debug, OID_AUTO, label_fallback, CTLFLAG_RW, - &mac_debug_label_fallback, 0, "Filesystems should fall back to fs label" - "when label is corrupted."); -TUNABLE_INT("security.mac.debug_label_fallback", - &mac_debug_label_fallback); - SYSCTL_NODE(_security_mac_debug, OID_AUTO, counters, CTLFLAG_RW, 0, "TrustedBSD MAC object counters"); -static unsigned int nmacmbufs, nmaccreds, nmacifnets, nmacbpfdescs, - nmacsockets, nmacmounts, nmactemp, nmacvnodes, nmacdevfsdirents, - nmacipqs, nmacpipes, nmacprocs; - -#define MAC_DEBUG_COUNTER_INC(x) atomic_add_int(x, 1); -#define MAC_DEBUG_COUNTER_DEC(x) atomic_subtract_int(x, 1); - -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, mbufs, CTLFLAG_RD, - &nmacmbufs, 0, "number of mbufs in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, creds, CTLFLAG_RD, - &nmaccreds, 0, "number of ucreds in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, ifnets, CTLFLAG_RD, - &nmacifnets, 0, "number of ifnets in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, ipqs, CTLFLAG_RD, - &nmacipqs, 0, "number of ipqs in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, bpfdescs, CTLFLAG_RD, - &nmacbpfdescs, 0, "number of bpfdescs in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, sockets, CTLFLAG_RD, - &nmacsockets, 0, "number of sockets in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, pipes, CTLFLAG_RD, - &nmacpipes, 0, "number of pipes in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, procs, CTLFLAG_RD, - &nmacprocs, 0, "number of procs in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, mounts, CTLFLAG_RD, - &nmacmounts, 0, "number of mounts in use"); +static unsigned int nmactemp; SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, temp, CTLFLAG_RD, &nmactemp, 0, "number of temporary labels in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, vnodes, CTLFLAG_RD, - &nmacvnodes, 0, "number of vnodes in use"); -SYSCTL_UINT(_security_mac_debug_counters, OID_AUTO, devfsdirents, CTLFLAG_RD, - &nmacdevfsdirents, 0, "number of devfs dirents inuse"); -#else -#define MAC_DEBUG_COUNTER_INC(x) -#define MAC_DEBUG_COUNTER_DEC(x) #endif static int mac_policy_register(struct mac_policy_conf *mpc); static int mac_policy_unregister(struct mac_policy_conf *mpc); -static void mac_check_vnode_mmap_downgrade(struct ucred *cred, - struct vnode *vp, int *prot); -static void mac_cred_mmapped_drop_perms_recurse(struct thread *td, - struct ucred *cred, struct vm_map *map); - -static void mac_destroy_socket_label(struct label *label); - -static int mac_setlabel_vnode_extattr(struct ucred *cred, - struct vnode *vp, struct label *intlabel); - -MALLOC_DEFINE(M_MACPIPELABEL, "macpipelabel", "MAC labels for pipes"); MALLOC_DEFINE(M_MACTEMP, "mactemp", "MAC temporary label storage"); /* @@ -341,151 +242,6 @@ mac_policy_list_unbusy(void) } /* - * MAC_CHECK performs the designated check by walking the policy - * module list and checking with each as to how it feels about the - * request. Note that it returns its value via 'error' in the scope - * of the caller. - */ -#define MAC_CHECK(check, args...) do { \ - struct mac_policy_conf *mpc; \ - int entrycount; \ - \ - error = 0; \ - LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ - if (mpc->mpc_ops->mpo_ ## check != NULL) \ - error = mac_error_select( \ - mpc->mpc_ops->mpo_ ## check (args), \ - error); \ - } \ - if ((entrycount = mac_policy_list_conditional_busy()) != 0) { \ - LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ - if (mpc->mpc_ops->mpo_ ## check != NULL) \ - error = mac_error_select( \ - mpc->mpc_ops->mpo_ ## check (args), \ - error); \ - } \ - mac_policy_list_unbusy(); \ - } \ -} while (0) - -/* - * MAC_BOOLEAN performs the designated boolean composition by walking - * the module list, invoking each instance of the operation, and - * combining the results using the passed C operator. Note that it - * returns its value via 'result' in the scope of the caller, which - * should be initialized by the caller in a meaningful way to get - * a meaningful result. - */ -#define MAC_BOOLEAN(operation, composition, args...) do { \ - struct mac_policy_conf *mpc; \ - int entrycount; \ - \ - LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ - if (mpc->mpc_ops->mpo_ ## operation != NULL) \ - result = result composition \ - mpc->mpc_ops->mpo_ ## operation (args); \ - } \ - if ((entrycount = mac_policy_list_conditional_busy()) != 0) { \ - LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ - if (mpc->mpc_ops->mpo_ ## operation != NULL) \ - result = result composition \ - mpc->mpc_ops->mpo_ ## operation \ - (args); \ - } \ - mac_policy_list_unbusy(); \ - } \ -} while (0) - -#define MAC_EXTERNALIZE(type, label, elementlist, outbuf, \ - outbuflen) do { \ - int claimed, first, ignorenotfound, savedlen; \ - char *element_name, *element_temp; \ - struct sbuf sb; \ - \ - error = 0; \ - first = 1; \ - sbuf_new(&sb, outbuf, outbuflen, SBUF_FIXEDLEN); \ - element_temp = elementlist; \ - while ((element_name = strsep(&element_temp, ",")) != NULL) { \ - if (element_name[0] == '?') { \ - element_name++; \ - ignorenotfound = 1; \ - } else \ - ignorenotfound = 0; \ - savedlen = sbuf_len(&sb); \ - if (first) { \ - error = sbuf_printf(&sb, "%s/", element_name); \ - first = 0; \ - } else \ - error = sbuf_printf(&sb, ",%s/", element_name); \ - if (error == -1) { \ - error = EINVAL; /* XXX: E2BIG? */ \ - break; \ - } \ - claimed = 0; \ - MAC_CHECK(externalize_ ## type, label, element_name, \ - &sb, &claimed); \ - if (error) \ - break; \ - if (claimed == 0 && ignorenotfound) { \ - /* Revert last label name. */ \ - sbuf_setpos(&sb, savedlen); \ - } else if (claimed != 1) { \ - error = EINVAL; /* XXX: ENOLABEL? */ \ - break; \ - } \ - } \ - sbuf_finish(&sb); \ -} while (0) - -#define MAC_INTERNALIZE(type, label, instring) do { \ - char *element, *element_name, *element_data; \ - int claimed; \ - \ - error = 0; \ - element = instring; \ - while ((element_name = strsep(&element, ",")) != NULL) { \ - element_data = element_name; \ - element_name = strsep(&element_data, "/"); \ - if (element_data == NULL) { \ - error = EINVAL; \ - break; \ - } \ - claimed = 0; \ - MAC_CHECK(internalize_ ## type, label, element_name, \ - element_data, &claimed); \ - if (error) \ - break; \ - if (claimed != 1) { \ - /* XXXMAC: Another error here? */ \ - error = EINVAL; \ - break; \ - } \ - } \ -} while (0) - -/* - * MAC_PERFORM performs the designated operation by walking the policy - * module list and invoking that operation for each policy. - */ -#define MAC_PERFORM(operation, args...) do { \ - struct mac_policy_conf *mpc; \ - int entrycount; \ - \ - LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { \ - if (mpc->mpc_ops->mpo_ ## operation != NULL) \ - mpc->mpc_ops->mpo_ ## operation (args); \ - } \ - if ((entrycount = mac_policy_list_conditional_busy()) != 0) { \ - LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { \ - if (mpc->mpc_ops->mpo_ ## operation != NULL) \ - mpc->mpc_ops->mpo_ ## operation (args); \ - } \ - mac_policy_list_unbusy(); \ - } \ -} while (0) - -/* * Initialize the MAC subsystem, including appropriate SMP locks. */ static void @@ -735,19 +491,7 @@ mac_error_select(int error1, int error2) return (error2); } -static struct label * -mbuf_to_label(struct mbuf *mbuf) -{ - struct m_tag *tag; - struct label *label; - - tag = m_tag_find(mbuf, PACKET_TAG_MACLABEL, NULL); - label = (struct label *)(tag+1); - - return (label); -} - -static void +void mac_init_label(struct label *label) { @@ -755,7 +499,7 @@ mac_init_label(struct label *label) label->l_flags = MAC_FLAG_INITIALIZED; } -static void +void mac_destroy_label(struct label *label) { @@ -766,406 +510,7 @@ mac_destroy_label(struct label *label) /* implicit: label->l_flags &= ~MAC_FLAG_INITIALIZED; */ } -void -mac_init_bpfdesc(struct bpf_d *bpf_d) -{ - - mac_init_label(&bpf_d->bd_label); - MAC_PERFORM(init_bpfdesc_label, &bpf_d->bd_label); - MAC_DEBUG_COUNTER_INC(&nmacbpfdescs); -} - -static void -mac_init_cred_label(struct label *label) -{ - - mac_init_label(label); - MAC_PERFORM(init_cred_label, label); - MAC_DEBUG_COUNTER_INC(&nmaccreds); -} - -void -mac_init_cred(struct ucred *cred) -{ - - mac_init_cred_label(&cred->cr_label); -} - -void -mac_init_devfsdirent(struct devfs_dirent *de) -{ - - mac_init_label(&de->de_label); - MAC_PERFORM(init_devfsdirent_label, &de->de_label); - MAC_DEBUG_COUNTER_INC(&nmacdevfsdirents); -} - -static void -mac_init_ifnet_label(struct label *label) -{ - - mac_init_label(label); - MAC_PERFORM(init_ifnet_label, label); - MAC_DEBUG_COUNTER_INC(&nmacifnets); -} - -void -mac_init_ifnet(struct ifnet *ifp) -{ - - mac_init_ifnet_label(&ifp->if_label); -} - -int -mac_init_ipq(struct ipq *ipq, int flag) -{ - int error; - - mac_init_label(&ipq->ipq_label); - - MAC_CHECK(init_ipq_label, &ipq->ipq_label, flag); - if (error) { - MAC_PERFORM(destroy_ipq_label, &ipq->ipq_label); - mac_destroy_label(&ipq->ipq_label); - } else { - MAC_DEBUG_COUNTER_INC(&nmacipqs); - } - return (error); -} - -int -mac_init_mbuf_tag(struct m_tag *tag, int flag) -{ - struct label *label; - int error; - - label = (struct label *) (tag + 1); - mac_init_label(label); - - MAC_CHECK(init_mbuf_label, label, flag); - if (error) { - MAC_PERFORM(destroy_mbuf_label, label); - mac_destroy_label(label); - } else { - MAC_DEBUG_COUNTER_INC(&nmacmbufs); - } - return (error); -} - -int -mac_init_mbuf(struct mbuf *m, int flag) -{ - struct m_tag *tag; - int error; - - M_ASSERTPKTHDR(m); - -#ifndef MAC_ALWAYS_LABEL_MBUF - /* - * If conditionally allocating mbuf labels, don't allocate unless - * they are required. - */ - if (!mac_labelmbufs) - return (0); -#endif - tag = m_tag_get(PACKET_TAG_MACLABEL, sizeof(struct label), - flag); - if (tag == NULL) - return (ENOMEM); - error = mac_init_mbuf_tag(tag, flag); - if (error) { - m_tag_free(tag); - return (error); - } - m_tag_prepend(m, tag); - return (0); -} - -void -mac_init_mount(struct mount *mp) -{ - - mac_init_label(&mp->mnt_mntlabel); - mac_init_label(&mp->mnt_fslabel); - MAC_PERFORM(init_mount_label, &mp->mnt_mntlabel); - MAC_PERFORM(init_mount_fs_label, &mp->mnt_fslabel); - MAC_DEBUG_COUNTER_INC(&nmacmounts); -} - -static void -mac_init_pipe_label(struct label *label) -{ - - mac_init_label(label); - MAC_PERFORM(init_pipe_label, label); - MAC_DEBUG_COUNTER_INC(&nmacpipes); -} - -void -mac_init_pipe(struct pipe *pipe) -{ - struct label *label; - - label = malloc(sizeof(struct label), M_MACPIPELABEL, M_ZERO|M_WAITOK); - pipe->pipe_label = label; - pipe->pipe_peer->pipe_label = label; - mac_init_pipe_label(label); -} - -void -mac_init_proc(struct proc *p) -{ - - mac_init_label(&p->p_label); - MAC_PERFORM(init_proc_label, &p->p_label); - MAC_DEBUG_COUNTER_INC(&nmacprocs); -} - -static int -mac_init_socket_label(struct label *label, int flag) -{ - int error; - - mac_init_label(label); - - MAC_CHECK(init_socket_label, label, flag); - if (error) { - MAC_PERFORM(destroy_socket_label, label); - mac_destroy_label(label); - } else { - MAC_DEBUG_COUNTER_INC(&nmacsockets); - } - - return (error); -} - -static int -mac_init_socket_peer_label(struct label *label, int flag) -{ - int error; - - mac_init_label(label); - - MAC_CHECK(init_socket_peer_label, label, flag); - if (error) { - MAC_PERFORM(destroy_socket_label, label); - mac_destroy_label(label); - } - - return (error); -} - int -mac_init_socket(struct socket *socket, int flag) -{ - int error; - - error = mac_init_socket_label(&socket->so_label, flag); - if (error) - return (error); - - error = mac_init_socket_peer_label(&socket->so_peerlabel, flag); - if (error) - mac_destroy_socket_label(&socket->so_label); - - return (error); -} - -void -mac_init_vnode_label(struct label *label) -{ - - mac_init_label(label); - MAC_PERFORM(init_vnode_label, label); - MAC_DEBUG_COUNTER_INC(&nmacvnodes); -} - -void -mac_init_vnode(struct vnode *vp) -{ - - mac_init_vnode_label(&vp->v_label); -} - -void -mac_destroy_bpfdesc(struct bpf_d *bpf_d) -{ - - MAC_PERFORM(destroy_bpfdesc_label, &bpf_d->bd_label); - mac_destroy_label(&bpf_d->bd_label); - MAC_DEBUG_COUNTER_DEC(&nmacbpfdescs); -} - -static void -mac_destroy_cred_label(struct label *label) -{ - - MAC_PERFORM(destroy_cred_label, label); - mac_destroy_label(label); - MAC_DEBUG_COUNTER_DEC(&nmaccreds); -} - -void -mac_destroy_cred(struct ucred *cred) -{ - - mac_destroy_cred_label(&cred->cr_label); -} - -void -mac_destroy_devfsdirent(struct devfs_dirent *de) -{ - - MAC_PERFORM(destroy_devfsdirent_label, &de->de_label); - mac_destroy_label(&de->de_label); - MAC_DEBUG_COUNTER_DEC(&nmacdevfsdirents); -} - -static void -mac_destroy_ifnet_label(struct label *label) -{ - - MAC_PERFORM(destroy_ifnet_label, label); - mac_destroy_label(label); - MAC_DEBUG_COUNTER_DEC(&nmacifnets); -} - -void -mac_destroy_ifnet(struct ifnet *ifp) -{ - - mac_destroy_ifnet_label(&ifp->if_label); -} - -void -mac_destroy_ipq(struct ipq *ipq) -{ - - MAC_PERFORM(destroy_ipq_label, &ipq->ipq_label); - mac_destroy_label(&ipq->ipq_label); - MAC_DEBUG_COUNTER_DEC(&nmacipqs); -} - -void -mac_destroy_mbuf_tag(struct m_tag *tag) -{ - struct label *label; - - label = (struct label *)(tag+1); - - MAC_PERFORM(destroy_mbuf_label, label); - mac_destroy_label(label); - MAC_DEBUG_COUNTER_DEC(&nmacmbufs); -} - -void -mac_destroy_mount(struct mount *mp) -{ - - MAC_PERFORM(destroy_mount_label, &mp->mnt_mntlabel); - MAC_PERFORM(destroy_mount_fs_label, &mp->mnt_fslabel); - mac_destroy_label(&mp->mnt_fslabel); - mac_destroy_label(&mp->mnt_mntlabel); - MAC_DEBUG_COUNTER_DEC(&nmacmounts); -} - -static void -mac_destroy_pipe_label(struct label *label) -{ - - MAC_PERFORM(destroy_pipe_label, label); - mac_destroy_label(label); - MAC_DEBUG_COUNTER_DEC(&nmacpipes); -} - -void -mac_destroy_pipe(struct pipe *pipe) -{ - - mac_destroy_pipe_label(pipe->pipe_label); - free(pipe->pipe_label, M_MACPIPELABEL); -} - -void -mac_destroy_proc(struct proc *p) -{ - - MAC_PERFORM(destroy_proc_label, &p->p_label); - mac_destroy_label(&p->p_label); - MAC_DEBUG_COUNTER_DEC(&nmacprocs); -} - -static void -mac_destroy_socket_label(struct label *label) -{ - - MAC_PERFORM(destroy_socket_label, label); - mac_destroy_label(label); - MAC_DEBUG_COUNTER_DEC(&nmacsockets); -} - -static void -mac_destroy_socket_peer_label(struct label *label) -{ - - MAC_PERFORM(destroy_socket_peer_label, label); - mac_destroy_label(label); -} - -void -mac_destroy_socket(struct socket *socket) -{ - - mac_destroy_socket_label(&socket->so_label); - mac_destroy_socket_peer_label(&socket->so_peerlabel); -} - -void -mac_destroy_vnode_label(struct label *label) -{ - - MAC_PERFORM(destroy_vnode_label, label); - mac_destroy_label(label); - MAC_DEBUG_COUNTER_DEC(&nmacvnodes); -} - -void -mac_destroy_vnode(struct vnode *vp) -{ - - mac_destroy_vnode_label(&vp->v_label); -} - -void -mac_copy_mbuf_tag(struct m_tag *src, struct m_tag *dest) -{ - struct label *src_label, *dest_label; - - src_label = (struct label *)(src+1); - dest_label = (struct label *)(dest+1); - - /* - * mac_init_mbuf_tag() is called on the target tag in - * m_tag_copy(), so we don't need to call it here. - */ - MAC_PERFORM(copy_mbuf_label, src_label, dest_label); -} - -static void -mac_copy_pipe_label(struct label *src, struct label *dest) -{ - - MAC_PERFORM(copy_pipe_label, src, dest); -} - -void -mac_copy_vnode_label(struct label *src, struct label *dest) -{ - - MAC_PERFORM(copy_vnode_label, src, dest); -} - -static int mac_check_structmac_consistent(struct mac *mac) { @@ -1176,2102 +521,6 @@ mac_check_structmac_consistent(struct mac *mac) return (0); } -static int -mac_externalize_cred_label(struct label *label, char *elements, - char *outbuf, size_t outbuflen, int flags) -{ - int error; - - MAC_EXTERNALIZE(cred_label, label, elements, outbuf, outbuflen); - - return (error); -} - -static int -mac_externalize_ifnet_label(struct label *label, char *elements, - char *outbuf, size_t outbuflen, int flags) -{ - int error; - - MAC_EXTERNALIZE(ifnet_label, label, elements, outbuf, outbuflen); - - return (error); -} - -static int -mac_externalize_pipe_label(struct label *label, char *elements, - char *outbuf, size_t outbuflen, int flags) -{ - int error; - - MAC_EXTERNALIZE(pipe_label, label, elements, outbuf, outbuflen); - - return (error); -} - -static int -mac_externalize_socket_label(struct label *label, char *elements, - char *outbuf, size_t outbuflen, int flags) -{ - int error; - - MAC_EXTERNALIZE(socket_label, label, elements, outbuf, outbuflen); - - return (error); -} - -static int -mac_externalize_socket_peer_label(struct label *label, char *elements, - char *outbuf, size_t outbuflen, int flags) -{ - int error; - - MAC_EXTERNALIZE(socket_peer_label, label, elements, outbuf, outbuflen); - - return (error); -} - -static int -mac_externalize_vnode_label(struct label *label, char *elements, - char *outbuf, size_t outbuflen, int flags) -{ - int error; - - MAC_EXTERNALIZE(vnode_label, label, elements, outbuf, outbuflen); - - return (error); -} - -static int -mac_internalize_cred_label(struct label *label, char *string) -{ - int error; - - MAC_INTERNALIZE(cred_label, label, string); - - return (error); -} - -static int -mac_internalize_ifnet_label(struct label *label, char *string) -{ - int error; - - MAC_INTERNALIZE(ifnet_label, label, string); - - return (error); -} - -static int -mac_internalize_pipe_label(struct label *label, char *string) -{ - int error; - - MAC_INTERNALIZE(pipe_label, label, string); - - return (error); -} - -static int -mac_internalize_socket_label(struct label *label, char *string) -{ - int error; - - MAC_INTERNALIZE(socket_label, label, string); - - return (error); -} - -static int -mac_internalize_vnode_label(struct label *label, char *string) -{ - int error; - - MAC_INTERNALIZE(vnode_label, label, string); - - return (error); -} - -/* - * Initialize MAC label for the first kernel process, from which other - * kernel processes and threads are spawned. - */ -void -mac_create_proc0(struct ucred *cred) -{ - - MAC_PERFORM(create_proc0, cred); -} - -/* - * Initialize MAC label for the first userland process, from which other - * userland processes and threads are spawned. - */ -void -mac_create_proc1(struct ucred *cred) -{ - - MAC_PERFORM(create_proc1, cred); -} - -void -mac_thread_userret(struct thread *td) -{ - - MAC_PERFORM(thread_userret, td); -} - -/* - * When a new process is created, its label must be initialized. Generally, - * this involves inheritence from the parent process, modulo possible - * deltas. This function allows that processing to take place. - */ -void -mac_create_cred(struct ucred *parent_cred, struct ucred *child_cred) -{ - - MAC_PERFORM(create_cred, parent_cred, child_cred); -} - -void -mac_update_devfsdirent(struct mount *mp, struct devfs_dirent *de, - struct vnode *vp) -{ - - MAC_PERFORM(update_devfsdirent, mp, de, &de->de_label, vp, - &vp->v_label); -} - -void -mac_associate_vnode_devfs(struct mount *mp, struct devfs_dirent *de, - struct vnode *vp) -{ - - MAC_PERFORM(associate_vnode_devfs, mp, &mp->mnt_fslabel, de, - &de->de_label, vp, &vp->v_label); -} - -int -mac_associate_vnode_extattr(struct mount *mp, struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_associate_vnode_extattr"); - - MAC_CHECK(associate_vnode_extattr, mp, &mp->mnt_fslabel, vp, - &vp->v_label); - - return (error); -} - -void -mac_associate_vnode_singlelabel(struct mount *mp, struct vnode *vp) -{ - - MAC_PERFORM(associate_vnode_singlelabel, mp, &mp->mnt_fslabel, vp, - &vp->v_label); -} - -int -mac_create_vnode_extattr(struct ucred *cred, struct mount *mp, - struct vnode *dvp, struct vnode *vp, struct componentname *cnp) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_create_vnode_extattr"); - ASSERT_VOP_LOCKED(vp, "mac_create_vnode_extattr"); - - error = VOP_OPENEXTATTR(vp, cred, curthread); - if (error == EOPNOTSUPP) { - /* XXX: Optionally abort if transactions not supported. */ - if (ea_warn_once == 0) { - printf("Warning: transactions not supported " - "in EA write.\n"); - ea_warn_once = 1; - } - } else if (error) - return (error); - - MAC_CHECK(create_vnode_extattr, cred, mp, &mp->mnt_fslabel, - dvp, &dvp->v_label, vp, &vp->v_label, cnp); - - if (error) { - VOP_CLOSEEXTATTR(vp, 0, NOCRED, curthread); - return (error); - } - - error = VOP_CLOSEEXTATTR(vp, 1, NOCRED, curthread); - - if (error == EOPNOTSUPP) - error = 0; /* XXX */ - - return (error); -} - -static int -mac_setlabel_vnode_extattr(struct ucred *cred, struct vnode *vp, - struct label *intlabel) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_setlabel_vnode_extattr"); - - error = VOP_OPENEXTATTR(vp, cred, curthread); - if (error == EOPNOTSUPP) { - /* XXX: Optionally abort if transactions not supported. */ - if (ea_warn_once == 0) { - printf("Warning: transactions not supported " - "in EA write.\n"); - ea_warn_once = 1; - } - } else if (error) - return (error); - - MAC_CHECK(setlabel_vnode_extattr, cred, vp, &vp->v_label, intlabel); - - if (error) { - VOP_CLOSEEXTATTR(vp, 0, NOCRED, curthread); - return (error); - } - - error = VOP_CLOSEEXTATTR(vp, 1, NOCRED, curthread); - - if (error == EOPNOTSUPP) - error = 0; /* XXX */ - - return (error); -} - -int -mac_execve_enter(struct image_params *imgp, struct mac *mac_p, - struct label *execlabelstorage) -{ - struct mac mac; - char *buffer; - int error; - - if (mac_p == NULL) - return (0); - - error = copyin(mac_p, &mac, sizeof(mac)); - if (error) - return (error); - - error = mac_check_structmac_consistent(&mac); - if (error) - return (error); - - buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK); - error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL); - if (error) { - free(buffer, M_MACTEMP); - return (error); - } - - mac_init_cred_label(execlabelstorage); - error = mac_internalize_cred_label(execlabelstorage, buffer); - free(buffer, M_MACTEMP); - if (error) { - mac_destroy_cred_label(execlabelstorage); - return (error); - } - imgp->execlabel = execlabelstorage; - return (0); -} - -void -mac_execve_exit(struct image_params *imgp) -{ - if (imgp->execlabel != NULL) - mac_destroy_cred_label(imgp->execlabel); -} - -void -mac_execve_transition(struct ucred *old, struct ucred *new, struct vnode *vp, - struct label *interpvnodelabel, struct image_params *imgp) -{ - - ASSERT_VOP_LOCKED(vp, "mac_execve_transition"); - - if (!mac_enforce_process && !mac_enforce_fs) - return; - - MAC_PERFORM(execve_transition, old, new, vp, &vp->v_label, - interpvnodelabel, imgp, imgp->execlabel); -} - -int -mac_execve_will_transition(struct ucred *old, struct vnode *vp, - struct label *interpvnodelabel, struct image_params *imgp) -{ - int result; - - ASSERT_VOP_LOCKED(vp, "mac_execve_will_transition"); - - if (!mac_enforce_process && !mac_enforce_fs) - return (0); - - result = 0; - MAC_BOOLEAN(execve_will_transition, ||, old, vp, &vp->v_label, - interpvnodelabel, imgp, imgp->execlabel); - - return (result); -} - -int -mac_check_vnode_access(struct ucred *cred, struct vnode *vp, int acc_mode) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_access"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_access, cred, vp, &vp->v_label, acc_mode); - return (error); -} - -int -mac_check_vnode_chdir(struct ucred *cred, struct vnode *dvp) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_chdir"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_chdir, cred, dvp, &dvp->v_label); - return (error); -} - -int -mac_check_vnode_chroot(struct ucred *cred, struct vnode *dvp) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_chroot"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_chroot, cred, dvp, &dvp->v_label); - return (error); -} - -int -mac_check_vnode_create(struct ucred *cred, struct vnode *dvp, - struct componentname *cnp, struct vattr *vap) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_create"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_create, cred, dvp, &dvp->v_label, cnp, vap); - return (error); -} - -int -mac_check_vnode_delete(struct ucred *cred, struct vnode *dvp, struct vnode *vp, - struct componentname *cnp) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_delete"); - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_delete"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_delete, cred, dvp, &dvp->v_label, vp, - &vp->v_label, cnp); - return (error); -} - -int -mac_check_vnode_deleteacl(struct ucred *cred, struct vnode *vp, - acl_type_t type) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_deleteacl"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_deleteacl, cred, vp, &vp->v_label, type); - return (error); -} - -int -mac_check_vnode_deleteextattr(struct ucred *cred, struct vnode *vp, - int attrnamespace, const char *name) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_deleteextattr"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_deleteextattr, cred, vp, &vp->v_label, - attrnamespace, name); - return (error); -} - -int -mac_check_vnode_exec(struct ucred *cred, struct vnode *vp, - struct image_params *imgp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_exec"); - - if (!mac_enforce_process && !mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_exec, cred, vp, &vp->v_label, imgp, - imgp->execlabel); - - return (error); -} - -int -mac_check_vnode_getacl(struct ucred *cred, struct vnode *vp, acl_type_t type) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_getacl"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_getacl, cred, vp, &vp->v_label, type); - return (error); -} - -int -mac_check_vnode_getextattr(struct ucred *cred, struct vnode *vp, - int attrnamespace, const char *name, struct uio *uio) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_getextattr"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_getextattr, cred, vp, &vp->v_label, - attrnamespace, name, uio); - return (error); -} - -int -mac_check_vnode_link(struct ucred *cred, struct vnode *dvp, - struct vnode *vp, struct componentname *cnp) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_link"); - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_link"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_link, cred, dvp, &dvp->v_label, vp, - &vp->v_label, cnp); - return (error); -} - -int -mac_check_vnode_listextattr(struct ucred *cred, struct vnode *vp, - int attrnamespace) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_listextattr"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_listextattr, cred, vp, &vp->v_label, - attrnamespace); - return (error); -} - -int -mac_check_vnode_lookup(struct ucred *cred, struct vnode *dvp, - struct componentname *cnp) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_lookup"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_lookup, cred, dvp, &dvp->v_label, cnp); - return (error); -} - -int -mac_check_vnode_mmap(struct ucred *cred, struct vnode *vp, int prot) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mmap"); - - if (!mac_enforce_fs || !mac_enforce_vm) - return (0); - - MAC_CHECK(check_vnode_mmap, cred, vp, &vp->v_label, prot); - return (error); -} - -void -mac_check_vnode_mmap_downgrade(struct ucred *cred, struct vnode *vp, int *prot) -{ - int result = *prot; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mmap_downgrade"); - - if (!mac_enforce_fs || !mac_enforce_vm) - return; - - MAC_PERFORM(check_vnode_mmap_downgrade, cred, vp, &vp->v_label, - &result); - - *prot = result; -} - -int -mac_check_vnode_mprotect(struct ucred *cred, struct vnode *vp, int prot) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mprotect"); - - if (!mac_enforce_fs || !mac_enforce_vm) - return (0); - - MAC_CHECK(check_vnode_mprotect, cred, vp, &vp->v_label, prot); - return (error); -} - -int -mac_check_vnode_open(struct ucred *cred, struct vnode *vp, int acc_mode) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_open"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_open, cred, vp, &vp->v_label, acc_mode); - return (error); -} - -int -mac_check_vnode_poll(struct ucred *active_cred, struct ucred *file_cred, - struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_poll"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_poll, active_cred, file_cred, vp, - &vp->v_label); - - return (error); -} - -int -mac_check_vnode_read(struct ucred *active_cred, struct ucred *file_cred, - struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_read"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_read, active_cred, file_cred, vp, - &vp->v_label); - - return (error); -} - -int -mac_check_vnode_readdir(struct ucred *cred, struct vnode *dvp) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_readdir"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_readdir, cred, dvp, &dvp->v_label); - return (error); -} - -int -mac_check_vnode_readlink(struct ucred *cred, struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_readlink"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_readlink, cred, vp, &vp->v_label); - return (error); -} - -static int -mac_check_vnode_relabel(struct ucred *cred, struct vnode *vp, - struct label *newlabel) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_relabel"); - - MAC_CHECK(check_vnode_relabel, cred, vp, &vp->v_label, newlabel); - - return (error); -} - -int -mac_check_vnode_rename_from(struct ucred *cred, struct vnode *dvp, - struct vnode *vp, struct componentname *cnp) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_rename_from"); - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_rename_from"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_rename_from, cred, dvp, &dvp->v_label, vp, - &vp->v_label, cnp); - return (error); -} - -int -mac_check_vnode_rename_to(struct ucred *cred, struct vnode *dvp, - struct vnode *vp, int samedir, struct componentname *cnp) -{ - int error; - - ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_rename_to"); - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_rename_to"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_rename_to, cred, dvp, &dvp->v_label, vp, - vp != NULL ? &vp->v_label : NULL, samedir, cnp); - return (error); -} - -int -mac_check_vnode_revoke(struct ucred *cred, struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_revoke"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_revoke, cred, vp, &vp->v_label); - return (error); -} - -int -mac_check_vnode_setacl(struct ucred *cred, struct vnode *vp, acl_type_t type, - struct acl *acl) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setacl"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_setacl, cred, vp, &vp->v_label, type, acl); - return (error); -} - -int -mac_check_vnode_setextattr(struct ucred *cred, struct vnode *vp, - int attrnamespace, const char *name, struct uio *uio) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setextattr"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_setextattr, cred, vp, &vp->v_label, - attrnamespace, name, uio); - return (error); -} - -int -mac_check_vnode_setflags(struct ucred *cred, struct vnode *vp, u_long flags) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setflags"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_setflags, cred, vp, &vp->v_label, flags); - return (error); -} - -int -mac_check_vnode_setmode(struct ucred *cred, struct vnode *vp, mode_t mode) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setmode"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_setmode, cred, vp, &vp->v_label, mode); - return (error); -} - -int -mac_check_vnode_setowner(struct ucred *cred, struct vnode *vp, uid_t uid, - gid_t gid) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setowner"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_setowner, cred, vp, &vp->v_label, uid, gid); - return (error); -} - -int -mac_check_vnode_setutimes(struct ucred *cred, struct vnode *vp, - struct timespec atime, struct timespec mtime) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setutimes"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_setutimes, cred, vp, &vp->v_label, atime, - mtime); - return (error); -} - -int -mac_check_vnode_stat(struct ucred *active_cred, struct ucred *file_cred, - struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_stat"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_stat, active_cred, file_cred, vp, - &vp->v_label); - return (error); -} - -int -mac_check_vnode_write(struct ucred *active_cred, struct ucred *file_cred, - struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_vnode_write"); - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_vnode_write, active_cred, file_cred, vp, - &vp->v_label); - - return (error); -} - -/* - * When relabeling a process, call out to the policies for the maximum - * permission allowed for each object type we know about in its - * memory space, and revoke access (in the least surprising ways we - * know) when necessary. The process lock is not held here. - */ -void -mac_cred_mmapped_drop_perms(struct thread *td, struct ucred *cred) -{ - - /* XXX freeze all other threads */ - mac_cred_mmapped_drop_perms_recurse(td, cred, - &td->td_proc->p_vmspace->vm_map); - /* XXX allow other threads to continue */ -} - -static __inline const char * -prot2str(vm_prot_t prot) -{ - - switch (prot & VM_PROT_ALL) { - case VM_PROT_READ: - return ("r--"); - case VM_PROT_READ | VM_PROT_WRITE: - return ("rw-"); - case VM_PROT_READ | VM_PROT_EXECUTE: - return ("r-x"); - case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE: - return ("rwx"); - case VM_PROT_WRITE: - return ("-w-"); - case VM_PROT_EXECUTE: - return ("--x"); - case VM_PROT_WRITE | VM_PROT_EXECUTE: - return ("-wx"); - default: - return ("---"); - } -} - -static void -mac_cred_mmapped_drop_perms_recurse(struct thread *td, struct ucred *cred, - struct vm_map *map) -{ - struct vm_map_entry *vme; - int result; - vm_prot_t revokeperms; - vm_object_t object; - vm_ooffset_t offset; - struct vnode *vp; - - if (!mac_mmap_revocation) - return; - - vm_map_lock_read(map); - for (vme = map->header.next; vme != &map->header; vme = vme->next) { - if (vme->eflags & MAP_ENTRY_IS_SUB_MAP) { - mac_cred_mmapped_drop_perms_recurse(td, cred, - vme->object.sub_map); - continue; - } - /* - * Skip over entries that obviously are not shared. - */ - if (vme->eflags & (MAP_ENTRY_COW | MAP_ENTRY_NOSYNC) || - !vme->max_protection) - continue; - /* - * Drill down to the deepest backing object. - */ - offset = vme->offset; - object = vme->object.vm_object; - if (object == NULL) - continue; - while (object->backing_object != NULL) { - object = object->backing_object; - offset += object->backing_object_offset; - } - /* - * At the moment, vm_maps and objects aren't considered - * by the MAC system, so only things with backing by a - * normal object (read: vnodes) are checked. - */ - if (object->type != OBJT_VNODE) - continue; - vp = (struct vnode *)object->handle; - vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); - result = vme->max_protection; - mac_check_vnode_mmap_downgrade(cred, vp, &result); - VOP_UNLOCK(vp, 0, td); - /* - * Find out what maximum protection we may be allowing - * now but a policy needs to get removed. - */ - revokeperms = vme->max_protection & ~result; - if (!revokeperms) - continue; - printf("pid %ld: revoking %s perms from %#lx:%ld " - "(max %s/cur %s)\n", (long)td->td_proc->p_pid, - prot2str(revokeperms), (u_long)vme->start, - (long)(vme->end - vme->start), - prot2str(vme->max_protection), prot2str(vme->protection)); - vm_map_lock_upgrade(map); - /* - * This is the really simple case: if a map has more - * max_protection than is allowed, but it's not being - * actually used (that is, the current protection is - * still allowed), we can just wipe it out and do - * nothing more. - */ - if ((vme->protection & revokeperms) == 0) { - vme->max_protection -= revokeperms; - } else { - if (revokeperms & VM_PROT_WRITE) { - /* - * In the more complicated case, flush out all - * pending changes to the object then turn it - * copy-on-write. - */ - vm_object_reference(object); - vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); - VM_OBJECT_LOCK(object); - vm_object_page_clean(object, - OFF_TO_IDX(offset), - OFF_TO_IDX(offset + vme->end - vme->start + - PAGE_MASK), - OBJPC_SYNC); - VM_OBJECT_UNLOCK(object); - VOP_UNLOCK(vp, 0, td); - vm_object_deallocate(object); - /* - * Why bother if there's no read permissions - * anymore? For the rest, we need to leave - * the write permissions on for COW, or - * remove them entirely if configured to. - */ - if (!mac_mmap_revocation_via_cow) { - vme->max_protection &= ~VM_PROT_WRITE; - vme->protection &= ~VM_PROT_WRITE; - } if ((revokeperms & VM_PROT_READ) == 0) - vme->eflags |= MAP_ENTRY_COW | - MAP_ENTRY_NEEDS_COPY; - } - if (revokeperms & VM_PROT_EXECUTE) { - vme->max_protection &= ~VM_PROT_EXECUTE; - vme->protection &= ~VM_PROT_EXECUTE; - } - if (revokeperms & VM_PROT_READ) { - vme->max_protection = 0; - vme->protection = 0; - } - pmap_protect(map->pmap, vme->start, vme->end, - vme->protection & ~revokeperms); - vm_map_simplify_entry(map, vme); - } - vm_map_lock_downgrade(map); - } - vm_map_unlock_read(map); -} - -/* - * When the subject's label changes, it may require revocation of privilege - * to mapped objects. This can't be done on-the-fly later with a unified - * buffer cache. - */ -static void -mac_relabel_cred(struct ucred *cred, struct label *newlabel) -{ - - MAC_PERFORM(relabel_cred, cred, newlabel); -} - -void -mac_relabel_vnode(struct ucred *cred, struct vnode *vp, struct label *newlabel) -{ - - MAC_PERFORM(relabel_vnode, cred, vp, &vp->v_label, newlabel); -} - -void -mac_create_ifnet(struct ifnet *ifnet) -{ - - MAC_PERFORM(create_ifnet, ifnet, &ifnet->if_label); -} - -void -mac_create_bpfdesc(struct ucred *cred, struct bpf_d *bpf_d) -{ - - MAC_PERFORM(create_bpfdesc, cred, bpf_d, &bpf_d->bd_label); -} - -void -mac_create_socket(struct ucred *cred, struct socket *socket) -{ - - MAC_PERFORM(create_socket, cred, socket, &socket->so_label); -} - -void -mac_create_pipe(struct ucred *cred, struct pipe *pipe) -{ - - MAC_PERFORM(create_pipe, cred, pipe, pipe->pipe_label); -} - -void -mac_create_socket_from_socket(struct socket *oldsocket, - struct socket *newsocket) -{ - - MAC_PERFORM(create_socket_from_socket, oldsocket, &oldsocket->so_label, - newsocket, &newsocket->so_label); -} - -static void -mac_relabel_socket(struct ucred *cred, struct socket *socket, - struct label *newlabel) -{ - - MAC_PERFORM(relabel_socket, cred, socket, &socket->so_label, newlabel); -} - -static void -mac_relabel_pipe(struct ucred *cred, struct pipe *pipe, struct label *newlabel) -{ - - MAC_PERFORM(relabel_pipe, cred, pipe, pipe->pipe_label, newlabel); -} - -void -mac_set_socket_peer_from_mbuf(struct mbuf *mbuf, struct socket *socket) -{ - struct label *label; - - label = mbuf_to_label(mbuf); - - MAC_PERFORM(set_socket_peer_from_mbuf, mbuf, label, socket, - &socket->so_peerlabel); -} - -void -mac_set_socket_peer_from_socket(struct socket *oldsocket, - struct socket *newsocket) -{ - - MAC_PERFORM(set_socket_peer_from_socket, oldsocket, - &oldsocket->so_label, newsocket, &newsocket->so_peerlabel); -} - -void -mac_create_datagram_from_ipq(struct ipq *ipq, struct mbuf *datagram) -{ - struct label *label; - - label = mbuf_to_label(datagram); - - MAC_PERFORM(create_datagram_from_ipq, ipq, &ipq->ipq_label, - datagram, label); -} - -void -mac_create_fragment(struct mbuf *datagram, struct mbuf *fragment) -{ - struct label *datagramlabel, *fragmentlabel; - - datagramlabel = mbuf_to_label(datagram); - fragmentlabel = mbuf_to_label(fragment); - - MAC_PERFORM(create_fragment, datagram, datagramlabel, fragment, - fragmentlabel); -} - -void -mac_create_ipq(struct mbuf *fragment, struct ipq *ipq) -{ - struct label *label; - - label = mbuf_to_label(fragment); - - MAC_PERFORM(create_ipq, fragment, label, ipq, &ipq->ipq_label); -} - -void -mac_create_mbuf_from_mbuf(struct mbuf *oldmbuf, struct mbuf *newmbuf) -{ - struct label *oldmbuflabel, *newmbuflabel; - - oldmbuflabel = mbuf_to_label(oldmbuf); - newmbuflabel = mbuf_to_label(newmbuf); - - MAC_PERFORM(create_mbuf_from_mbuf, oldmbuf, oldmbuflabel, newmbuf, - newmbuflabel); -} - -void -mac_create_mbuf_from_bpfdesc(struct bpf_d *bpf_d, struct mbuf *mbuf) -{ - struct label *label; - - label = mbuf_to_label(mbuf); - - MAC_PERFORM(create_mbuf_from_bpfdesc, bpf_d, &bpf_d->bd_label, mbuf, - label); -} - -void -mac_create_mbuf_linklayer(struct ifnet *ifnet, struct mbuf *mbuf) -{ - struct label *label; - - label = mbuf_to_label(mbuf); - - MAC_PERFORM(create_mbuf_linklayer, ifnet, &ifnet->if_label, mbuf, - label); -} - -void -mac_create_mbuf_from_ifnet(struct ifnet *ifnet, struct mbuf *mbuf) -{ - struct label *label; - - label = mbuf_to_label(mbuf); - - MAC_PERFORM(create_mbuf_from_ifnet, ifnet, &ifnet->if_label, mbuf, - label); -} - -void -mac_create_mbuf_multicast_encap(struct mbuf *oldmbuf, struct ifnet *ifnet, - struct mbuf *newmbuf) -{ - struct label *oldmbuflabel, *newmbuflabel; - - oldmbuflabel = mbuf_to_label(oldmbuf); - newmbuflabel = mbuf_to_label(newmbuf); - - MAC_PERFORM(create_mbuf_multicast_encap, oldmbuf, oldmbuflabel, - ifnet, &ifnet->if_label, newmbuf, newmbuflabel); -} - -void -mac_create_mbuf_netlayer(struct mbuf *oldmbuf, struct mbuf *newmbuf) -{ - struct label *oldmbuflabel, *newmbuflabel; - - oldmbuflabel = mbuf_to_label(oldmbuf); - newmbuflabel = mbuf_to_label(newmbuf); - - MAC_PERFORM(create_mbuf_netlayer, oldmbuf, oldmbuflabel, newmbuf, - newmbuflabel); -} - -int -mac_fragment_match(struct mbuf *fragment, struct ipq *ipq) -{ - struct label *label; - int result; - - label = mbuf_to_label(fragment); - - result = 1; - MAC_BOOLEAN(fragment_match, &&, fragment, label, ipq, - &ipq->ipq_label); - - return (result); -} - -void -mac_reflect_mbuf_icmp(struct mbuf *m) -{ - struct label *label; - - label = mbuf_to_label(m); - - MAC_PERFORM(reflect_mbuf_icmp, m, label); -} -void -mac_reflect_mbuf_tcp(struct mbuf *m) -{ - struct label *label; - - label = mbuf_to_label(m); - - MAC_PERFORM(reflect_mbuf_tcp, m, label); -} - -void -mac_update_ipq(struct mbuf *fragment, struct ipq *ipq) -{ - struct label *label; - - label = mbuf_to_label(fragment); - - MAC_PERFORM(update_ipq, fragment, label, ipq, &ipq->ipq_label); -} - -void -mac_create_mbuf_from_socket(struct socket *socket, struct mbuf *mbuf) -{ - struct label *label; - - label = mbuf_to_label(mbuf); - - MAC_PERFORM(create_mbuf_from_socket, socket, &socket->so_label, mbuf, - label); -} - -void -mac_create_mount(struct ucred *cred, struct mount *mp) -{ - - MAC_PERFORM(create_mount, cred, mp, &mp->mnt_mntlabel, - &mp->mnt_fslabel); -} - -void -mac_create_root_mount(struct ucred *cred, struct mount *mp) -{ - - MAC_PERFORM(create_root_mount, cred, mp, &mp->mnt_mntlabel, - &mp->mnt_fslabel); -} - -int -mac_check_bpfdesc_receive(struct bpf_d *bpf_d, struct ifnet *ifnet) -{ - int error; - - if (!mac_enforce_network) - return (0); - - MAC_CHECK(check_bpfdesc_receive, bpf_d, &bpf_d->bd_label, ifnet, - &ifnet->if_label); - - return (error); -} - -static int -mac_check_cred_relabel(struct ucred *cred, struct label *newlabel) -{ - int error; - - MAC_CHECK(check_cred_relabel, cred, newlabel); - - return (error); -} - -int -mac_check_cred_visible(struct ucred *u1, struct ucred *u2) -{ - int error; - - if (!mac_enforce_process) - return (0); - - MAC_CHECK(check_cred_visible, u1, u2); - - return (error); -} - -int -mac_check_ifnet_transmit(struct ifnet *ifnet, struct mbuf *mbuf) -{ - struct label *label; - int error; - - M_ASSERTPKTHDR(mbuf); - - if (!mac_enforce_network) - return (0); - - label = mbuf_to_label(mbuf); - - MAC_CHECK(check_ifnet_transmit, ifnet, &ifnet->if_label, mbuf, - label); - - return (error); -} - -int -mac_check_kenv_dump(struct ucred *cred) -{ - int error; - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_kenv_dump, cred); - - return (error); -} - -int -mac_check_kenv_get(struct ucred *cred, char *name) -{ - int error; - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_kenv_get, cred, name); - - return (error); -} - -int -mac_check_kenv_set(struct ucred *cred, char *name, char *value) -{ - int error; - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_kenv_set, cred, name, value); - - return (error); -} - -int -mac_check_kenv_unset(struct ucred *cred, char *name) -{ - int error; - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_kenv_unset, cred, name); - - return (error); -} - -int -mac_check_kld_load(struct ucred *cred, struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_kld_load"); - - if (!mac_enforce_kld) - return (0); - - MAC_CHECK(check_kld_load, cred, vp, &vp->v_label); - - return (error); -} - -int -mac_check_kld_stat(struct ucred *cred) -{ - int error; - - if (!mac_enforce_kld) - return (0); - - MAC_CHECK(check_kld_stat, cred); - - return (error); -} - -int -mac_check_kld_unload(struct ucred *cred) -{ - int error; - - if (!mac_enforce_kld) - return (0); - - MAC_CHECK(check_kld_unload, cred); - - return (error); -} - -int -mac_check_mount_stat(struct ucred *cred, struct mount *mount) -{ - int error; - - if (!mac_enforce_fs) - return (0); - - MAC_CHECK(check_mount_stat, cred, mount, &mount->mnt_mntlabel); - - return (error); -} - -int -mac_check_pipe_ioctl(struct ucred *cred, struct pipe *pipe, unsigned long cmd, - void *data) -{ - int error; - - PIPE_LOCK_ASSERT(pipe, MA_OWNED); - - if (!mac_enforce_pipe) - return (0); - - MAC_CHECK(check_pipe_ioctl, cred, pipe, pipe->pipe_label, cmd, data); - - return (error); -} - -int -mac_check_pipe_poll(struct ucred *cred, struct pipe *pipe) -{ - int error; - - PIPE_LOCK_ASSERT(pipe, MA_OWNED); - - if (!mac_enforce_pipe) - return (0); - - MAC_CHECK(check_pipe_poll, cred, pipe, pipe->pipe_label); - - return (error); -} - -int -mac_check_pipe_read(struct ucred *cred, struct pipe *pipe) -{ - int error; - - PIPE_LOCK_ASSERT(pipe, MA_OWNED); - - if (!mac_enforce_pipe) - return (0); - - MAC_CHECK(check_pipe_read, cred, pipe, pipe->pipe_label); - - return (error); -} - -static int -mac_check_pipe_relabel(struct ucred *cred, struct pipe *pipe, - struct label *newlabel) -{ - int error; - - PIPE_LOCK_ASSERT(pipe, MA_OWNED); - - if (!mac_enforce_pipe) - return (0); - - MAC_CHECK(check_pipe_relabel, cred, pipe, pipe->pipe_label, newlabel); - - return (error); -} - -int -mac_check_pipe_stat(struct ucred *cred, struct pipe *pipe) -{ - int error; - - PIPE_LOCK_ASSERT(pipe, MA_OWNED); - - if (!mac_enforce_pipe) - return (0); - - MAC_CHECK(check_pipe_stat, cred, pipe, pipe->pipe_label); - - return (error); -} - -int -mac_check_pipe_write(struct ucred *cred, struct pipe *pipe) -{ - int error; - - PIPE_LOCK_ASSERT(pipe, MA_OWNED); - - if (!mac_enforce_pipe) - return (0); - - MAC_CHECK(check_pipe_write, cred, pipe, pipe->pipe_label); - - return (error); -} - -int -mac_check_proc_debug(struct ucred *cred, struct proc *proc) -{ - int error; - - PROC_LOCK_ASSERT(proc, MA_OWNED); - - if (!mac_enforce_process) - return (0); - - MAC_CHECK(check_proc_debug, cred, proc); - - return (error); -} - -int -mac_check_proc_sched(struct ucred *cred, struct proc *proc) -{ - int error; - - PROC_LOCK_ASSERT(proc, MA_OWNED); - - if (!mac_enforce_process) - return (0); - - MAC_CHECK(check_proc_sched, cred, proc); - - return (error); -} - -int -mac_check_proc_signal(struct ucred *cred, struct proc *proc, int signum) -{ - int error; - - PROC_LOCK_ASSERT(proc, MA_OWNED); - - if (!mac_enforce_process) - return (0); - - MAC_CHECK(check_proc_signal, cred, proc, signum); - - return (error); -} - -int -mac_check_socket_bind(struct ucred *ucred, struct socket *socket, - struct sockaddr *sockaddr) -{ - int error; - - if (!mac_enforce_socket) - return (0); - - MAC_CHECK(check_socket_bind, ucred, socket, &socket->so_label, - sockaddr); - - return (error); -} - -int -mac_check_socket_connect(struct ucred *cred, struct socket *socket, - struct sockaddr *sockaddr) -{ - int error; - - if (!mac_enforce_socket) - return (0); - - MAC_CHECK(check_socket_connect, cred, socket, &socket->so_label, - sockaddr); - - return (error); -} - -int -mac_check_socket_deliver(struct socket *socket, struct mbuf *mbuf) -{ - struct label *label; - int error; - - if (!mac_enforce_socket) - return (0); - - label = mbuf_to_label(mbuf); - - MAC_CHECK(check_socket_deliver, socket, &socket->so_label, mbuf, - label); - - return (error); -} - -int -mac_check_socket_listen(struct ucred *cred, struct socket *socket) -{ - int error; - - if (!mac_enforce_socket) - return (0); - - MAC_CHECK(check_socket_listen, cred, socket, &socket->so_label); - return (error); -} - -int -mac_check_socket_receive(struct ucred *cred, struct socket *so) -{ - int error; - - if (!mac_enforce_socket) - return (0); - - MAC_CHECK(check_socket_receive, cred, so, &so->so_label); - - return (error); -} - -static int -mac_check_socket_relabel(struct ucred *cred, struct socket *socket, - struct label *newlabel) -{ - int error; - - MAC_CHECK(check_socket_relabel, cred, socket, &socket->so_label, - newlabel); - - return (error); -} - -int -mac_check_socket_send(struct ucred *cred, struct socket *so) -{ - int error; - - if (!mac_enforce_socket) - return (0); - - MAC_CHECK(check_socket_send, cred, so, &so->so_label); - - return (error); -} - -int -mac_check_socket_visible(struct ucred *cred, struct socket *socket) -{ - int error; - - if (!mac_enforce_socket) - return (0); - - MAC_CHECK(check_socket_visible, cred, socket, &socket->so_label); - - return (error); -} - -int -mac_check_sysarch_ioperm(struct ucred *cred) -{ - int error; - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_sysarch_ioperm, cred); - return (error); -} - -int -mac_check_system_acct(struct ucred *cred, struct vnode *vp) -{ - int error; - - if (vp != NULL) { - ASSERT_VOP_LOCKED(vp, "mac_check_system_acct"); - } - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_system_acct, cred, vp, - vp != NULL ? &vp->v_label : NULL); - - return (error); -} - -int -mac_check_system_nfsd(struct ucred *cred) -{ - int error; - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_system_nfsd, cred); - - return (error); -} - -int -mac_check_system_reboot(struct ucred *cred, int howto) -{ - int error; - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_system_reboot, cred, howto); - - return (error); -} - -int -mac_check_system_settime(struct ucred *cred) -{ - int error; - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_system_settime, cred); - - return (error); -} - -int -mac_check_system_swapon(struct ucred *cred, struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_system_swapon"); - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_system_swapon, cred, vp, &vp->v_label); - return (error); -} - -int -mac_check_system_swapoff(struct ucred *cred, struct vnode *vp) -{ - int error; - - ASSERT_VOP_LOCKED(vp, "mac_check_system_swapoff"); - - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_system_swapoff, cred, vp, &vp->v_label); - return (error); -} - -int -mac_check_system_sysctl(struct ucred *cred, int *name, u_int namelen, - void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen) -{ - int error; - - /* - * XXXMAC: We're very much like to assert the SYSCTL_LOCK here, - * but since it's not exported from kern_sysctl.c, we can't. - */ - if (!mac_enforce_system) - return (0); - - MAC_CHECK(check_system_sysctl, cred, name, namelen, old, oldlenp, - inkernel, new, newlen); - - return (error); -} - -int -mac_ioctl_ifnet_get(struct ucred *cred, struct ifreq *ifr, - struct ifnet *ifnet) -{ - char *elements, *buffer; - struct mac mac; - int error; - - error = copyin(ifr->ifr_ifru.ifru_data, &mac, sizeof(mac)); - if (error) - return (error); - - error = mac_check_structmac_consistent(&mac); - if (error) - return (error); - - elements = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK); - error = copyinstr(mac.m_string, elements, mac.m_buflen, NULL); - if (error) { - free(elements, M_MACTEMP); - return (error); - } - - buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK | M_ZERO); - error = mac_externalize_ifnet_label(&ifnet->if_label, elements, - buffer, mac.m_buflen, M_WAITOK); - if (error == 0) - error = copyout(buffer, mac.m_string, strlen(buffer)+1); - - free(buffer, M_MACTEMP); - free(elements, M_MACTEMP); - - return (error); -} - -int -mac_ioctl_ifnet_set(struct ucred *cred, struct ifreq *ifr, - struct ifnet *ifnet) -{ - struct label intlabel; - struct mac mac; - char *buffer; - int error; - - error = copyin(ifr->ifr_ifru.ifru_data, &mac, sizeof(mac)); - if (error) - return (error); - - error = mac_check_structmac_consistent(&mac); - if (error) - return (error); - - buffer = malloc(mac.m_buflen, M_MACTEMP, M_WAITOK); - error = copyinstr(mac.m_string, buffer, mac.m_buflen, NULL); - if (error) { - free(buffer, M_MACTEMP); - return (error); - } - - mac_init_ifnet_label(&intlabel); - error = mac_internalize_ifnet_label(&intlabel, buffer); - free(buffer, M_MACTEMP); - if (error) { - mac_destroy_ifnet_label(&intlabel); - return (error); - } - - /* - * XXX: Note that this is a redundant privilege check, since - * policies impose this check themselves if required by the - * policy. Eventually, this should go away. - */ - error = suser_cred(cred, 0); - if (error) { - mac_destroy_ifnet_label(&intlabel); - return (error); - } - - MAC_CHECK(check_ifnet_relabel, cred, ifnet, &ifnet->if_label, - &intlabel); - if (error) { - mac_destroy_ifnet_label(&intlabel); - return (error); - } - - MAC_PERFORM(relabel_ifnet, cred, ifnet, &ifnet->if_label, &intlabel); - - mac_destroy_ifnet_label(&intlabel); - return (0); -} - -void -mac_create_devfs_device(struct mount *mp, dev_t dev, struct devfs_dirent *de) -{ - - MAC_PERFORM(create_devfs_device, mp, dev, de, &de->de_label); -} - -void -mac_create_devfs_symlink(struct ucred *cred, struct mount *mp, - struct devfs_dirent *dd, struct devfs_dirent *de) -{ - - MAC_PERFORM(create_devfs_symlink, cred, mp, dd, &dd->de_label, de, - &de->de_label); -} - -void -mac_create_devfs_directory(struct mount *mp, char *dirname, int dirnamelen, - struct devfs_dirent *de) -{ - - MAC_PERFORM(create_devfs_directory, mp, dirname, dirnamelen, de, - &de->de_label); -} - -int -mac_setsockopt_label_set(struct ucred *cred, struct socket *so, - struct mac *mac) -{ - struct label intlabel; - char *buffer; - int error; - - error = mac_check_structmac_consistent(mac); - if (error) - return (error); - - buffer = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK); - error = copyinstr(mac->m_string, buffer, mac->m_buflen, NULL); - if (error) { - free(buffer, M_MACTEMP); - return (error); - } - - mac_init_socket_label(&intlabel, M_WAITOK); - error = mac_internalize_socket_label(&intlabel, buffer); - free(buffer, M_MACTEMP); - if (error) { - mac_destroy_socket_label(&intlabel); - return (error); - } - - mac_check_socket_relabel(cred, so, &intlabel); - if (error) { - mac_destroy_socket_label(&intlabel); - return (error); - } - - mac_relabel_socket(cred, so, &intlabel); - - mac_destroy_socket_label(&intlabel); - return (0); -} - -int -mac_pipe_label_set(struct ucred *cred, struct pipe *pipe, struct label *label) -{ - int error; - - PIPE_LOCK_ASSERT(pipe, MA_OWNED); - - error = mac_check_pipe_relabel(cred, pipe, label); - if (error) - return (error); - - mac_relabel_pipe(cred, pipe, label); - - return (0); -} - -int -mac_getsockopt_label_get(struct ucred *cred, struct socket *so, - struct mac *mac) -{ - char *buffer, *elements; - int error; - - error = mac_check_structmac_consistent(mac); - if (error) - return (error); - - elements = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK); - error = copyinstr(mac->m_string, elements, mac->m_buflen, NULL); - if (error) { - free(elements, M_MACTEMP); - return (error); - } - - buffer = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK | M_ZERO); - error = mac_externalize_socket_label(&so->so_label, elements, - buffer, mac->m_buflen, M_WAITOK); - if (error == 0) - error = copyout(buffer, mac->m_string, strlen(buffer)+1); - - free(buffer, M_MACTEMP); - free(elements, M_MACTEMP); - - return (error); -} - -int -mac_getsockopt_peerlabel_get(struct ucred *cred, struct socket *so, - struct mac *mac) -{ - char *elements, *buffer; - int error; - - error = mac_check_structmac_consistent(mac); - if (error) - return (error); - - elements = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK); - error = copyinstr(mac->m_string, elements, mac->m_buflen, NULL); - if (error) { - free(elements, M_MACTEMP); - return (error); - } - - buffer = malloc(mac->m_buflen, M_MACTEMP, M_WAITOK | M_ZERO); - error = mac_externalize_socket_peer_label(&so->so_peerlabel, - elements, buffer, mac->m_buflen, M_WAITOK); - if (error == 0) - error = copyout(buffer, mac->m_string, strlen(buffer)+1); - - free(buffer, M_MACTEMP); - free(elements, M_MACTEMP); - - return (error); -} - -/* - * Implementation of VOP_SETLABEL() that relies on extended attributes - * to store label data. Can be referenced by filesystems supporting - * extended attributes. - */ -int -vop_stdsetlabel_ea(struct vop_setlabel_args *ap) -{ - struct vnode *vp = ap->a_vp; - struct label *intlabel = ap->a_label; - int error; - - ASSERT_VOP_LOCKED(vp, "vop_stdsetlabel_ea"); - - if ((vp->v_mount->mnt_flag & MNT_MULTILABEL) == 0) - return (EOPNOTSUPP); - - error = mac_setlabel_vnode_extattr(ap->a_cred, vp, intlabel); - if (error) - return (error); - - mac_relabel_vnode(ap->a_cred, vp, intlabel); - - return (0); -} - -static int -vn_setlabel(struct vnode *vp, struct label *intlabel, struct ucred *cred) -{ - int error; - - if (vp->v_mount == NULL) { - /* printf("vn_setlabel: null v_mount\n"); */ - if (vp->v_type != VNON) - printf("vn_setlabel: null v_mount with non-VNON\n"); - return (EBADF); - } - - if ((vp->v_mount->mnt_flag & MNT_MULTILABEL) == 0) - return (EOPNOTSUPP); - - /* - * Multi-phase commit. First check the policies to confirm the - * change is OK. Then commit via the filesystem. Finally, - * update the actual vnode label. Question: maybe the filesystem - * should update the vnode at the end as part of VOP_SETLABEL()? - */ - error = mac_check_vnode_relabel(cred, vp, intlabel); - if (error) - return (error); - - /* - * VADMIN provides the opportunity for the filesystem to make - * decisions about who is and is not able to modify labels - * and protections on files. This might not be right. We can't - * assume VOP_SETLABEL() will do it, because we might implement - * that as part of vop_stdsetlabel_ea(). - */ - error = VOP_ACCESS(vp, VADMIN, cred, curthread); - if (error) - return (error); - - error = VOP_SETLABEL(vp, intlabel, cred, curthread); - if (error) - return (error); - - return (0); -} - int __mac_get_pid(struct thread *td, struct __mac_get_pid_args *uap) { |