]> git.saurik.com Git - apple/xnu.git/blobdiff - security/mac_base.c
xnu-2050.7.9.tar.gz
[apple/xnu.git] / security / mac_base.c
index 8b2eabff248a915ae304f02f0fef62cd0dcbf51b..ae808e27796654e5ec484e6b767785569f1ce6de 100644 (file)
@@ -88,6 +88,7 @@
 #include <sys/kauth.h>
 #include <sys/sysproto.h>
 
+#include <mach/exception_types.h>
 #include <mach/vm_types.h>
 #include <mach/vm_prot.h>
 
@@ -123,7 +124,11 @@ SYSCTL_NODE(, OID_AUTO, security, CTLFLAG_RW|CTLFLAG_LOCKED, 0,
 SYSCTL_NODE(_security, OID_AUTO, mac, CTLFLAG_RW|CTLFLAG_LOCKED, 0,
     "TrustedBSD MAC policy controls");
 
-
+#if DEBUG
+#define SECURITY_MAC_CTLFLAGS CTLFLAG_RW | CTLFLAG_LOCKED
+#else
+#define SECURITY_MAC_CTLFLAGS CTLFLAG_RD | CTLFLAG_LOCKED
+#endif
 
 /*
  * Declare that the kernel provides MAC support, version 1.  This permits
@@ -140,7 +145,7 @@ MODULE_VERSION(kernel_mac_support, 1);
 
 static unsigned int mac_max_slots = MAC_MAX_SLOTS;
 static unsigned int mac_slot_offsets_free = (1 << MAC_MAX_SLOTS) - 1;
-SYSCTL_UINT(_security_mac, OID_AUTO, max_slots, CTLFLAG_RD,
+SYSCTL_UINT(_security_mac, OID_AUTO, max_slots, CTLFLAG_RD | CTLFLAG_LOCKED,
     &mac_max_slots, 0, "");
 
 /*
@@ -163,13 +168,10 @@ int       mac_late = 0;
  */
 #if CONFIG_MACF_NET
 unsigned int mac_label_mbufs   = 1;
-SYSCTL_UINT(_security_mac, OID_AUTO, label_mbufs, CTLFLAG_RW,
+SYSCTL_UINT(_security_mac, OID_AUTO, label_mbufs, SECURITY_MAC_CTLFLAGS,
        &mac_label_mbufs, 0, "Label all MBUFs");
 #endif
 
-#if !defined(CONFIG_MACF_ALWAYS_LABEL_MBUF) && 0
-static int     mac_labelmbufs = 0;
-#endif
 
 /*
  * Flag to indicate whether or not we should allocate label storage for
@@ -183,87 +185,68 @@ static int        mac_labelmbufs = 0;
  * be a problem.
  */
 unsigned int   mac_label_vnodes = 0;
-SYSCTL_UINT(_security_mac, OID_AUTO, labelvnodes, CTLFLAG_RW,
+SYSCTL_UINT(_security_mac, OID_AUTO, labelvnodes, SECURITY_MAC_CTLFLAGS,
     &mac_label_vnodes, 0, "Label all vnodes");
 
 
 unsigned int   mac_mmap_revocation = 0;
-SYSCTL_UINT(_security_mac, OID_AUTO, mmap_revocation, CTLFLAG_RW,
+SYSCTL_UINT(_security_mac, OID_AUTO, mmap_revocation, SECURITY_MAC_CTLFLAGS,
     &mac_mmap_revocation, 0, "Revoke mmap access to files on subject "
     "relabel");
 
 unsigned int   mac_mmap_revocation_via_cow = 0;
-SYSCTL_UINT(_security_mac, OID_AUTO, mmap_revocation_via_cow, CTLFLAG_RW,
+SYSCTL_UINT(_security_mac, OID_AUTO, mmap_revocation_via_cow, SECURITY_MAC_CTLFLAGS,
     &mac_mmap_revocation_via_cow, 0, "Revoke mmap access to files via "
     "copy-on-write semantics, or by removing all write access");
 
 unsigned int mac_device_enforce = 1;
-SYSCTL_UINT(_security_mac, OID_AUTO, device_enforce, CTLFLAG_RW,
+SYSCTL_UINT(_security_mac, OID_AUTO, device_enforce, SECURITY_MAC_CTLFLAGS,
           &mac_device_enforce, 0, "Enforce MAC policy on device operations");
 
-unsigned int mac_file_enforce = 0;
-SYSCTL_UINT(_security_mac, OID_AUTO, file_enforce, CTLFLAG_RW,
-       &mac_file_enforce, 0, "Enforce MAC policy on file operations");
-
-unsigned int mac_iokit_enforce = 0;
-SYSCTL_UINT(_security_mac, OID_AUTO, iokit_enforce, CTLFLAG_RW,
-       &mac_file_enforce, 0, "Enforce MAC policy on IOKit operations");
-
 unsigned int   mac_pipe_enforce = 1;
-SYSCTL_UINT(_security_mac, OID_AUTO, pipe_enforce, CTLFLAG_RW,
+SYSCTL_UINT(_security_mac, OID_AUTO, pipe_enforce, SECURITY_MAC_CTLFLAGS,
     &mac_pipe_enforce, 0, "Enforce MAC policy on pipe operations");
 
 unsigned int   mac_posixsem_enforce = 1;
-SYSCTL_UINT(_security_mac, OID_AUTO, posixsem_enforce, CTLFLAG_RW,
+SYSCTL_UINT(_security_mac, OID_AUTO, posixsem_enforce, SECURITY_MAC_CTLFLAGS,
     &mac_posixsem_enforce, 0, "Enforce MAC policy on POSIX semaphores");
 
 unsigned int mac_posixshm_enforce = 1;
-SYSCTL_UINT(_security_mac, OID_AUTO, posixshm_enforce, CTLFLAG_RW,
+SYSCTL_UINT(_security_mac, OID_AUTO, posixshm_enforce, SECURITY_MAC_CTLFLAGS,
     &mac_posixshm_enforce, 0, "Enforce MAC policy on Posix Shared Memory");
 
 unsigned int   mac_proc_enforce = 1;
-SYSCTL_UINT(_security_mac, OID_AUTO, proc_enforce, CTLFLAG_RW,
+SYSCTL_UINT(_security_mac, OID_AUTO, proc_enforce, SECURITY_MAC_CTLFLAGS,
           &mac_proc_enforce, 0, "Enforce MAC policy on process operations");
 
 unsigned int mac_socket_enforce = 1;
-SYSCTL_UINT(_security_mac, OID_AUTO, socket_enforce, CTLFLAG_RW,
+SYSCTL_UINT(_security_mac, OID_AUTO, socket_enforce, SECURITY_MAC_CTLFLAGS,
        &mac_socket_enforce, 0, "Enforce MAC policy on socket operations");
 
 unsigned int   mac_system_enforce = 1;
-SYSCTL_UINT(_security_mac, OID_AUTO, system_enforce, CTLFLAG_RW,
+SYSCTL_UINT(_security_mac, OID_AUTO, system_enforce, SECURITY_MAC_CTLFLAGS,
     &mac_system_enforce, 0, "Enforce MAC policy on system-wide interfaces");
 
 unsigned int   mac_sysvmsg_enforce = 1;
-SYSCTL_UINT(_security_mac, OID_AUTO, sysvmsg_enforce, CTLFLAG_RW,
+SYSCTL_UINT(_security_mac, OID_AUTO, sysvmsg_enforce, SECURITY_MAC_CTLFLAGS,
     &mac_sysvmsg_enforce, 0, "Enforce MAC policy on System V IPC message queues");
 
 unsigned int   mac_sysvsem_enforce = 1;
-SYSCTL_UINT(_security_mac, OID_AUTO, sysvsem_enforce, CTLFLAG_RW,
+SYSCTL_UINT(_security_mac, OID_AUTO, sysvsem_enforce, SECURITY_MAC_CTLFLAGS,
     &mac_sysvsem_enforce, 0, "Enforce MAC policy on System V IPC semaphores");
 
 unsigned int   mac_sysvshm_enforce = 1;
-SYSCTL_INT(_security_mac, OID_AUTO, sysvshm_enforce, CTLFLAG_RW,
+SYSCTL_INT(_security_mac, OID_AUTO, sysvshm_enforce, SECURITY_MAC_CTLFLAGS,
     &mac_sysvshm_enforce, 0, "Enforce MAC policy on System V Shared Memory");
 
 unsigned int   mac_vm_enforce = 1;
-SYSCTL_INT(_security_mac, OID_AUTO, vm_enforce, CTLFLAG_RW,
+SYSCTL_INT(_security_mac, OID_AUTO, vm_enforce, SECURITY_MAC_CTLFLAGS,
           &mac_vm_enforce, 0, "Enforce MAC policy on VM operations");
 
 unsigned int   mac_vnode_enforce = 1;
-SYSCTL_UINT(_security_mac, OID_AUTO, vnode_enforce, CTLFLAG_RW,
+SYSCTL_UINT(_security_mac, OID_AUTO, vnode_enforce, SECURITY_MAC_CTLFLAGS,
           &mac_vnode_enforce, 0, "Enforce MAC policy on vnode operations");
 
-
-#if CONFIG_MACF_MACH
-unsigned int   mac_port_enforce = 0;
-SYSCTL_UINT(_security_mac, OID_AUTO, port_enforce, CTLFLAG_RW,
-    &mac_port_enforce, 0, "Enforce MAC policy on Mach port operations");
-
-unsigned int   mac_task_enforce = 0;
-SYSCTL_UINT(_security_mac, OID_AUTO, task_enforce, CTLFLAG_RW,
-    &mac_task_enforce, 0, "Enforce MAC policy on Mach task operations");
-#endif
-
 #if CONFIG_AUDIT
 /*
  * mac_audit_data_zone is the zone used for data pushed into the audit
@@ -744,26 +727,6 @@ mac_policy_removefrom_labellist(mac_policy_handle_t handle)
 static void
 mac_policy_updateflags(void)
 {
-#if !defined(CONFIG_MACF_ALWAYS_LABEL_MBUF) && 0 /* port to new list style */
-
-       struct mac_policy_conf *tmpc;
-       int labelmbufs;
-
-       mac_policy_assert_exclusive();
-
-       labelmbufs = 0;
-
-       /* XXX - convert to new list structure */
-       LIST_FOREACH(tmpc, &mac_static_policy_list, mpc_list) {
-               if (tmpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_LABELMBUFS)
-                       labelmbufs++;
-       }
-       LIST_FOREACH(tmpc, &mac_policy_list, mpc_list) {
-               if (tmpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_LABELMBUFS)
-                       labelmbufs++;
-       }
-       mac_labelmbufs = (labelmbufs != 0);
-#endif
 }
 
 static __inline void
@@ -1346,12 +1309,15 @@ __mac_get_pid(struct proc *p, struct __mac_get_pid_args *uap, int *ret __unused)
 
        AUDIT_ARG(pid, uap->pid);
        if (IS_64BIT_PROCESS(p)) {
-               error = copyin(uap->mac_p, &mac, sizeof(mac));
+               struct user64_mac mac64;
+               error = copyin(uap->mac_p, &mac64, sizeof(mac64));
+               mac.m_buflen = mac64.m_buflen;
+               mac.m_string = mac64.m_string;
        } else {
-               struct mac mac32;
+               struct user32_mac mac32;
                error = copyin(uap->mac_p, &mac32, sizeof(mac32));
                mac.m_buflen = mac32.m_buflen;
-               mac.m_string = CAST_USER_ADDR_T(mac32.m_string);
+               mac.m_string = mac32.m_string;
        }
        if (error)
                return (error);
@@ -1397,12 +1363,15 @@ __mac_get_proc(proc_t p, struct __mac_get_proc_args *uap, int *ret __unused)
        size_t ulen;
 
        if (IS_64BIT_PROCESS(p)) {
-               error = copyin(uap->mac_p, &mac, sizeof(mac));
+               struct user64_mac mac64;
+               error = copyin(uap->mac_p, &mac64, sizeof(mac64));
+               mac.m_buflen = mac64.m_buflen;
+               mac.m_string = mac64.m_string;
        } else {
-               struct mac mac32;
+               struct user32_mac mac32;
                error = copyin(uap->mac_p, &mac32, sizeof(mac32));
                mac.m_buflen = mac32.m_buflen;
-               mac.m_string = CAST_USER_ADDR_T(mac32.m_string);
+               mac.m_string = mac32.m_string;
        }
        if (error)
                return (error);
@@ -1444,12 +1413,15 @@ __mac_set_proc(proc_t p, struct __mac_set_proc_args *uap, int *ret __unused)
        size_t ulen;
 
        if (IS_64BIT_PROCESS(p)) {
-               error = copyin(uap->mac_p, &mac, sizeof(mac));
+               struct user64_mac mac64;
+               error = copyin(uap->mac_p, &mac64, sizeof(mac64));
+               mac.m_buflen = mac64.m_buflen;
+               mac.m_string = mac64.m_string;
        } else {
-               struct mac mac32;
+               struct user32_mac mac32;
                error = copyin(uap->mac_p, &mac32, sizeof(mac32));
                mac.m_buflen = mac32.m_buflen;
-               mac.m_string = CAST_USER_ADDR_T(mac32.m_string);
+               mac.m_string = mac32.m_string;
        }
        if (error)
                return (error);
@@ -1526,12 +1498,15 @@ __mac_get_lcid(proc_t p, struct __mac_get_lcid_args *uap, int *ret __unused)
 
        AUDIT_ARG(value32, uap->lcid);
        if (IS_64BIT_PROCESS(p)) {
-               error = copyin(uap->mac_p, &mac, sizeof(mac));
+               struct user64_mac mac64;
+               error = copyin(uap->mac_p, &mac64, sizeof(mac64));
+               mac.m_buflen = mac64.m_buflen;
+               mac.m_string = mac64.m_string;
        } else {
-               struct mac mac32;
+               struct user32_mac mac32;
                error = copyin(uap->mac_p, &mac32, sizeof(mac32));
                mac.m_buflen = mac32.m_buflen;
-               mac.m_string = CAST_USER_ADDR_T(mac32.m_string);
+               mac.m_string = mac32.m_string;
        }
 
        if (error)
@@ -1590,12 +1565,15 @@ __mac_get_lctx(proc_t p, struct __mac_get_lctx_args *uap, int *ret __unused)
        size_t ulen;
 
        if (IS_64BIT_PROCESS(p)) {
-               error = copyin(uap->mac_p, &mac, sizeof(mac));
+               struct user64_mac mac64;
+               error = copyin(uap->mac_p, &mac64, sizeof(mac64));
+               mac.m_buflen = mac64.m_buflen;
+               mac.m_string = mac64.m_string;
        } else {
-               struct mac mac32;
+               struct user32_mac mac32;
                error = copyin(uap->mac_p, &mac32, sizeof(mac32));
                mac.m_buflen = mac32.m_buflen;
-               mac.m_string = CAST_USER_ADDR_T(mac32.m_string);
+               mac.m_string = mac32.m_string;
        }
 
        if (error)
@@ -1643,12 +1621,15 @@ __mac_set_lctx(proc_t p, struct __mac_set_lctx_args *uap, int *ret __unused)
        size_t ulen;
 
        if (IS_64BIT_PROCESS(p)) {
-               error = copyin(uap->mac_p, &mac, sizeof(mac));
+               struct user64_mac mac64;
+               error = copyin(uap->mac_p, &mac64, sizeof(mac64));
+               mac.m_buflen = mac64.m_buflen;
+               mac.m_string = mac64.m_string;
        } else {
-               struct mac mac32;
+               struct user32_mac mac32;
                error = copyin(uap->mac_p, &mac32, sizeof(mac32));
                mac.m_buflen = mac32.m_buflen;
-               mac.m_string = CAST_USER_ADDR_T(mac32.m_string);
+               mac.m_string = mac32.m_string;
        }
        if (error)
                return (error);
@@ -1732,12 +1713,15 @@ __mac_get_fd(proc_t p, struct __mac_get_fd_args *uap, int *ret __unused)
        AUDIT_ARG(fd, uap->fd);
 
        if (IS_64BIT_PROCESS(p)) {
-               error = copyin(uap->mac_p, &mac, sizeof(mac));
+               struct user64_mac mac64;
+               error = copyin(uap->mac_p, &mac64, sizeof(mac64));
+               mac.m_buflen = mac64.m_buflen;
+               mac.m_string = mac64.m_string;
        } else {
-               struct mac mac32;
+               struct user32_mac mac32;
                error = copyin(uap->mac_p, &mac32, sizeof(mac32));
                mac.m_buflen = mac32.m_buflen;
-               mac.m_string = CAST_USER_ADDR_T(mac32.m_string);
+               mac.m_string = mac32.m_string;
        }
 
        if (error) 
@@ -1834,12 +1818,15 @@ mac_get_filelink(proc_t p, user_addr_t mac_p, user_addr_t path_p, int follow)
        size_t ulen;
 
        if (IS_64BIT_PROCESS(p)) {
-               error = copyin(mac_p, &mac, sizeof(mac));
+               struct user64_mac mac64;
+               error = copyin(mac_p, &mac64, sizeof(mac64));
+               mac.m_buflen = mac64.m_buflen;
+               mac.m_string = mac64.m_string;
        } else {
-               struct mac mac32;
+               struct user32_mac mac32;
                error = copyin(mac_p, &mac32, sizeof(mac32));
                mac.m_buflen = mac32.m_buflen;
-               mac.m_string = CAST_USER_ADDR_T(mac32.m_string);
+               mac.m_string = mac32.m_string;
        }
 
        if (error)
@@ -1862,7 +1849,7 @@ mac_get_filelink(proc_t p, user_addr_t mac_p, user_addr_t path_p, int follow)
 
        ctx = vfs_context_current();
 
-       NDINIT(&nd, LOOKUP,
+       NDINIT(&nd, LOOKUP, OP_LOOKUP,
                LOCKLEAF | (follow ? FOLLOW : NOFOLLOW) | AUDITVNPATH1,
                UIO_USERSPACE, path_p, ctx);
        error = namei(&nd);
@@ -1926,12 +1913,15 @@ __mac_set_fd(proc_t p, struct __mac_set_fd_args *uap, int *ret __unused)
        AUDIT_ARG(fd, uap->fd);
 
        if (IS_64BIT_PROCESS(p)) {
-               error = copyin(uap->mac_p, &mac, sizeof(mac));
+               struct user64_mac mac64;
+               error = copyin(uap->mac_p, &mac64, sizeof(mac64));
+               mac.m_buflen = mac64.m_buflen;
+               mac.m_string = mac64.m_string;
        } else {
-               struct mac mac32;
+               struct user32_mac mac32;
                error = copyin(uap->mac_p, &mac32, sizeof(mac32));
                mac.m_buflen = mac32.m_buflen;
-               mac.m_string = CAST_USER_ADDR_T(mac32.m_string);
+               mac.m_string = mac32.m_string;
        }
        if (error) 
                return (error);
@@ -2034,12 +2024,15 @@ mac_set_filelink(proc_t p, user_addr_t mac_p, user_addr_t path_p,
                return ENOSYS;
 
        if (IS_64BIT_PROCESS(p)) {
-               error = copyin(mac_p, &mac, sizeof(mac));
+               struct user64_mac mac64;
+               error = copyin(mac_p, &mac64, sizeof(mac64));
+               mac.m_buflen = mac64.m_buflen;
+               mac.m_string = mac64.m_string;
        } else {
-               struct mac mac32;
+               struct user32_mac mac32;
                error = copyin(mac_p, &mac32, sizeof(mac32));
                mac.m_buflen = mac32.m_buflen;
-               mac.m_string = CAST_USER_ADDR_T(mac32.m_string);
+               mac.m_string = mac32.m_string;
        }
        if (error)
                return (error);
@@ -2066,7 +2059,7 @@ mac_set_filelink(proc_t p, user_addr_t mac_p, user_addr_t path_p,
                return (error);
        }
 
-       NDINIT(&nd, LOOKUP,
+       NDINIT(&nd, LOOKUP, OP_LOOKUP,
                LOCKLEAF | (follow ? FOLLOW : NOFOLLOW) | AUDITVNPATH1,
                UIO_USERSPACE, path_p, ctx);
        error = namei(&nd);
@@ -2175,12 +2168,15 @@ mac_mount_label_get(struct mount *mp, user_addr_t mac_p)
        size_t ulen;
 
        if (IS_64BIT_PROCESS(current_proc())) {
-               error = copyin(mac_p, &mac, sizeof(mac));
+               struct user64_mac mac64;
+               error = copyin(mac_p, &mac64, sizeof(mac64));
+               mac.m_buflen = mac64.m_buflen;
+               mac.m_string = mac64.m_string;
        } else {
-               struct mac mac32;
+               struct user32_mac mac32;
                error = copyin(mac_p, &mac32, sizeof(mac32));
                mac.m_buflen = mac32.m_buflen;
-               mac.m_string = CAST_USER_ADDR_T(mac32.m_string);
+               mac.m_string = mac32.m_string;
        }
        if (error)
                return (error);
@@ -2232,7 +2228,7 @@ __mac_get_mount(proc_t p __unused, struct __mac_get_mount_args *uap,
        struct mount *mp;
        int error;
 
-       NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNPATH1,
+       NDINIT(&nd, LOOKUP, OP_LOOKUP, FOLLOW | AUDITVNPATH1,
                UIO_USERSPACE, uap->path, ctx);
        error = namei(&nd);
        if (error) {
@@ -2244,6 +2240,61 @@ __mac_get_mount(proc_t p __unused, struct __mac_get_mount_args *uap,
        return mac_mount_label_get(mp, uap->mac_p);
 }
 
+/*
+ * mac_schedule_userret()
+ *
+ * Schedule a callback to the mpo_thread_userret hook. The mpo_thread_userret
+ * hook is called just before the thread exit from the kernel in ast_taken().
+ *
+ * Returns:     0              Success
+ *             !0              Not successful
+ */
+int
+mac_schedule_userret(void)
+{
+
+       act_set_astmacf(current_thread());
+       return (0);
+}
+
+/*
+ * mac_do_machexc()
+ *
+ * Do a Mach exception.  This should only be done in the mpo_thread_userret
+ * callback.
+ *
+ * params:     code            exception code
+ *             subcode         exception subcode
+ *             flags           flags:
+ *                             MAC_DOEXCF_TRACED  Only do exception if being
+ *                                                ptrace()'ed.
+ *
+ *
+ * Returns:     0              Success
+ *             !0              Not successful
+ */
+int
+mac_do_machexc(int64_t code, int64_t subcode, uint32_t flags)
+{
+       mach_exception_data_type_t  codes[EXCEPTION_CODE_MAX];
+       proc_t p = current_proc();
+
+       /* Only allow execption codes in MACF's reserved range. */
+       if ((code < EXC_MACF_MIN) || (code > EXC_MACF_MAX))
+               return (1);
+
+       if (flags & MAC_DOEXCF_TRACED &&
+           !(p->p_lflag & P_LTRACED && (p->p_lflag & P_LPPWAIT) == 0))
+               return (0);
+
+
+       /* Send the Mach exception */
+       codes[0] = (mach_exception_data_type_t)code;
+       codes[1] = (mach_exception_data_type_t)subcode;
+
+       return (bsd_exception(EXC_SOFTWARE, codes, 2) != KERN_SUCCESS);
+}
+
 #else /* MAC */
 
 int
@@ -2394,4 +2445,18 @@ __mac_get_mount(proc_t p __unused,
 
        return (ENOSYS);
 }
+
+int
+mac_schedule_userret(void)
+{
+
+       return (1);
+}
+
+int
+mac_do_machexc(int64_t code __unused, int64_t subcode __unused, uint32_t flags __unused)
+{
+
+       return (1);
+}
 #endif /* !MAC */