CRED: Neuter sys_capset()
[linux-block.git] / security / commoncap.c
index 399bfdb9e2da99c4ef81fdd8b0391b1f5571c371..e3f36ef629fabd095269cf6a4fc8a1aef2ed69d1 100644 (file)
@@ -8,6 +8,7 @@
  */
 
 #include <linux/capability.h>
+#include <linux/audit.h>
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/kernel.h>
@@ -48,7 +49,7 @@ EXPORT_SYMBOL(cap_netlink_recv);
  * returns 0 when a task has a capability, but the kernel's capable()
  * returns 1 for this case.
  */
-int cap_capable (struct task_struct *tsk, int cap)
+int cap_capable(struct task_struct *tsk, int cap, int audit)
 {
        /* Derived from include/linux/sched.h:capable. */
        if (cap_raised(tsk->cap_effective, cap))
@@ -95,15 +96,6 @@ int cap_capget (struct task_struct *target, kernel_cap_t *effective,
 
 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
 
-static inline int cap_block_setpcap(struct task_struct *target)
-{
-       /*
-        * No support for remote process capability manipulation with
-        * filesystem capability support.
-        */
-       return (target != current);
-}
-
 static inline int cap_inh_is_capped(void)
 {
        /*
@@ -111,14 +103,13 @@ static inline int cap_inh_is_capped(void)
         * to the old permitted set. That is, if the current task
         * does *not* possess the CAP_SETPCAP capability.
         */
-       return (cap_capable(current, CAP_SETPCAP) != 0);
+       return (cap_capable(current, CAP_SETPCAP, SECURITY_CAP_AUDIT) != 0);
 }
 
 static inline int cap_limit_ptraced_target(void) { return 1; }
 
 #else /* ie., ndef CONFIG_SECURITY_FILE_CAPABILITIES */
 
-static inline int cap_block_setpcap(struct task_struct *t) { return 0; }
 static inline int cap_inh_is_capped(void) { return 1; }
 static inline int cap_limit_ptraced_target(void)
 {
@@ -127,21 +118,18 @@ static inline int cap_limit_ptraced_target(void)
 
 #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
 
-int cap_capset_check (struct task_struct *target, kernel_cap_t *effective,
+int cap_capset_check (kernel_cap_t *effective,
                      kernel_cap_t *inheritable, kernel_cap_t *permitted)
 {
-       if (cap_block_setpcap(target)) {
-               return -EPERM;
-       }
        if (cap_inh_is_capped()
            && !cap_issubset(*inheritable,
-                            cap_combine(target->cap_inheritable,
+                            cap_combine(current->cap_inheritable,
                                         current->cap_permitted))) {
                /* incapable of using this inheritable set */
                return -EPERM;
        }
        if (!cap_issubset(*inheritable,
-                          cap_combine(target->cap_inheritable,
+                          cap_combine(current->cap_inheritable,
                                       current->cap_bset))) {
                /* no new pI capabilities outside bounding set */
                return -EPERM;
@@ -149,7 +137,7 @@ int cap_capset_check (struct task_struct *target, kernel_cap_t *effective,
 
        /* verify restrictions on target's new Permitted set */
        if (!cap_issubset (*permitted,
-                          cap_combine (target->cap_permitted,
+                          cap_combine (current->cap_permitted,
                                        current->cap_permitted))) {
                return -EPERM;
        }
@@ -162,12 +150,12 @@ int cap_capset_check (struct task_struct *target, kernel_cap_t *effective,
        return 0;
 }
 
-void cap_capset_set (struct task_struct *target, kernel_cap_t *effective,
+void cap_capset_set (kernel_cap_t *effective,
                     kernel_cap_t *inheritable, kernel_cap_t *permitted)
 {
-       target->cap_effective = *effective;
-       target->cap_inheritable = *inheritable;
-       target->cap_permitted = *permitted;
+       current->cap_effective = *effective;
+       current->cap_inheritable = *inheritable;
+       current->cap_permitted = *permitted;
 }
 
 static inline void bprm_clear_caps(struct linux_binprm *bprm)
@@ -202,17 +190,70 @@ int cap_inode_killpriv(struct dentry *dentry)
        return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
 }
 
-static inline int cap_from_disk(struct vfs_cap_data *caps,
-                               struct linux_binprm *bprm, unsigned size)
+static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
+                                         struct linux_binprm *bprm)
+{
+       unsigned i;
+       int ret = 0;
+
+       if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
+               bprm->cap_effective = true;
+       else
+               bprm->cap_effective = false;
+
+       CAP_FOR_EACH_U32(i) {
+               __u32 permitted = caps->permitted.cap[i];
+               __u32 inheritable = caps->inheritable.cap[i];
+
+               /*
+                * pP' = (X & fP) | (pI & fI)
+                */
+               bprm->cap_post_exec_permitted.cap[i] =
+                       (current->cap_bset.cap[i] & permitted) |
+                       (current->cap_inheritable.cap[i] & inheritable);
+
+               if (permitted & ~bprm->cap_post_exec_permitted.cap[i]) {
+                       /*
+                        * insufficient to execute correctly
+                        */
+                       ret = -EPERM;
+               }
+       }
+
+       /*
+        * For legacy apps, with no internal support for recognizing they
+        * do not have enough capabilities, we return an error if they are
+        * missing some "forced" (aka file-permitted) capabilities.
+        */
+       return bprm->cap_effective ? ret : 0;
+}
+
+int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
 {
+       struct inode *inode = dentry->d_inode;
        __u32 magic_etc;
        unsigned tocopy, i;
-       int ret;
+       int size;
+       struct vfs_cap_data caps;
+
+       memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
+
+       if (!inode || !inode->i_op || !inode->i_op->getxattr)
+               return -ENODATA;
+
+       size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps,
+                                  XATTR_CAPS_SZ);
+       if (size == -ENODATA || size == -EOPNOTSUPP) {
+               /* no data, that's ok */
+               return -ENODATA;
+       }
+       if (size < 0)
+               return size;
 
        if (size < sizeof(magic_etc))
                return -EINVAL;
 
-       magic_etc = le32_to_cpu(caps->magic_etc);
+       cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
 
        switch ((magic_etc & VFS_CAP_REVISION_MASK)) {
        case VFS_CAP_REVISION_1:
@@ -229,46 +270,13 @@ static inline int cap_from_disk(struct vfs_cap_data *caps,
                return -EINVAL;
        }
 
-       if (magic_etc & VFS_CAP_FLAGS_EFFECTIVE) {
-               bprm->cap_effective = true;
-       } else {
-               bprm->cap_effective = false;
-       }
-
-       ret = 0;
-
        CAP_FOR_EACH_U32(i) {
-               __u32 value_cpu;
-
-               if (i >= tocopy) {
-                       /*
-                        * Legacy capability sets have no upper bits
-                        */
-                       bprm->cap_post_exec_permitted.cap[i] = 0;
-                       continue;
-               }
-               /*
-                * pP' = (X & fP) | (pI & fI)
-                */
-               value_cpu = le32_to_cpu(caps->data[i].permitted);
-               bprm->cap_post_exec_permitted.cap[i] =
-                       (current->cap_bset.cap[i] & value_cpu) |
-                       (current->cap_inheritable.cap[i] &
-                               le32_to_cpu(caps->data[i].inheritable));
-               if (value_cpu & ~bprm->cap_post_exec_permitted.cap[i]) {
-                       /*
-                        * insufficient to execute correctly
-                        */
-                       ret = -EPERM;
-               }
+               if (i >= tocopy)
+                       break;
+               cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted);
+               cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);
        }
-
-       /*
-        * For legacy apps, with no internal support for recognizing they
-        * do not have enough capabilities, we return an error if they are
-        * missing some "forced" (aka file-permitted) capabilities.
-        */
-       return bprm->cap_effective ? ret : 0;
+       return 0;
 }
 
 /* Locate any VFS capabilities: */
@@ -276,33 +284,29 @@ static int get_file_caps(struct linux_binprm *bprm)
 {
        struct dentry *dentry;
        int rc = 0;
-       struct vfs_cap_data vcaps;
-       struct inode *inode;
+       struct cpu_vfs_cap_data vcaps;
 
-       if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID) {
-               bprm_clear_caps(bprm);
+       bprm_clear_caps(bprm);
+
+       if (!file_caps_enabled)
+               return 0;
+
+       if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)
                return 0;
-       }
 
        dentry = dget(bprm->file->f_dentry);
-       inode = dentry->d_inode;
-       if (!inode->i_op || !inode->i_op->getxattr)
-               goto out;
 
-       rc = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, &vcaps,
-                                  XATTR_CAPS_SZ);
-       if (rc == -ENODATA || rc == -EOPNOTSUPP) {
-               /* no data, that's ok */
-               rc = 0;
+       rc = get_vfs_caps_from_disk(dentry, &vcaps);
+       if (rc < 0) {
+               if (rc == -EINVAL)
+                       printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n",
+                               __func__, rc, bprm->filename);
+               else if (rc == -ENODATA)
+                       rc = 0;
                goto out;
        }
-       if (rc < 0)
-               goto out;
 
-       rc = cap_from_disk(&vcaps, bprm, rc);
-       if (rc == -EINVAL)
-               printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
-                      __func__, rc, bprm->filename);
+       rc = bprm_caps_from_vfs_caps(&vcaps, bprm);
 
 out:
        dput(dentry);
@@ -345,7 +349,7 @@ int cap_bprm_set_security (struct linux_binprm *bprm)
                 * If only the real uid is 0, we do not set the effective
                 * bit.
                 */
-               if (bprm->e_uid == 0 || current->uid == 0) {
+               if (bprm->e_uid == 0 || current_uid() == 0) {
                        /* pP' = (cap_bset & ~0) | (pI & ~0) */
                        bprm->cap_post_exec_permitted = cap_combine(
                                current->cap_bset, current->cap_inheritable
@@ -360,7 +364,14 @@ int cap_bprm_set_security (struct linux_binprm *bprm)
 
 void cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe)
 {
-       if (bprm->e_uid != current->uid || bprm->e_gid != current->gid ||
+       kernel_cap_t pP = current->cap_permitted;
+       kernel_cap_t pE = current->cap_effective;
+       uid_t uid;
+       gid_t gid;
+
+       current_uid_gid(&uid, &gid);
+
+       if (bprm->e_uid != uid || bprm->e_gid != gid ||
            !cap_issubset(bprm->cap_post_exec_permitted,
                          current->cap_permitted)) {
                set_dumpable(current->mm, suid_dumpable);
@@ -368,8 +379,8 @@ void cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe)
 
                if (unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
                        if (!capable(CAP_SETUID)) {
-                               bprm->e_uid = current->uid;
-                               bprm->e_gid = current->gid;
+                               bprm->e_uid = uid;
+                               bprm->e_gid = gid;
                        }
                        if (cap_limit_ptraced_target()) {
                                bprm->cap_post_exec_permitted = cap_intersect(
@@ -393,22 +404,39 @@ void cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe)
                        cap_clear(current->cap_effective);
        }
 
-       /* AUD: Audit candidate if current->cap_effective is set */
+       /*
+        * Audit candidate if current->cap_effective is set
+        *
+        * We do not bother to audit if 3 things are true:
+        *   1) cap_effective has all caps
+        *   2) we are root
+        *   3) root is supposed to have all caps (SECURE_NOROOT)
+        * Since this is just a normal root execing a process.
+        *
+        * Number 1 above might fail if you don't have a full bset, but I think
+        * that is interesting information to audit.
+        */
+       if (!cap_isclear(current->cap_effective)) {
+               if (!cap_issubset(CAP_FULL_SET, current->cap_effective) ||
+                   (bprm->e_uid != 0) || (current->uid != 0) ||
+                   issecure(SECURE_NOROOT))
+                       audit_log_bprm_fcaps(bprm, &pP, &pE);
+       }
 
        current->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
 }
 
 int cap_bprm_secureexec (struct linux_binprm *bprm)
 {
-       if (current->uid != 0) {
+       if (current_uid() != 0) {
                if (bprm->cap_effective)
                        return 1;
                if (!cap_isclear(bprm->cap_post_exec_permitted))
                        return 1;
        }
 
-       return (current->euid != current->uid ||
-               current->egid != current->gid);
+       return (current_euid() != current_uid() ||
+               current_egid() != current_gid());
 }
 
 int cap_inode_setxattr(struct dentry *dentry, const char *name,
@@ -471,16 +499,18 @@ int cap_inode_removexattr(struct dentry *dentry, const char *name)
 static inline void cap_emulate_setxuid (int old_ruid, int old_euid,
                                        int old_suid)
 {
+       uid_t euid = current_euid();
+
        if ((old_ruid == 0 || old_euid == 0 || old_suid == 0) &&
-           (current->uid != 0 && current->euid != 0 && current->suid != 0) &&
+           (current_uid()  != 0 && euid != 0 && current_suid() != 0) &&
            !issecure(SECURE_KEEP_CAPS)) {
                cap_clear (current->cap_permitted);
                cap_clear (current->cap_effective);
        }
-       if (old_euid == 0 && current->euid != 0) {
+       if (old_euid == 0 && euid != 0) {
                cap_clear (current->cap_effective);
        }
-       if (old_euid != 0 && current->euid == 0) {
+       if (old_euid != 0 && euid == 0) {
                current->cap_effective = current->cap_permitted;
        }
 }
@@ -509,12 +539,12 @@ int cap_task_post_setuid (uid_t old_ruid, uid_t old_euid, uid_t old_suid,
                         */
 
                        if (!issecure (SECURE_NO_SETUID_FIXUP)) {
-                               if (old_fsuid == 0 && current->fsuid != 0) {
+                               if (old_fsuid == 0 && current_fsuid() != 0) {
                                        current->cap_effective =
                                                cap_drop_fs_set(
                                                    current->cap_effective);
                                }
-                               if (old_fsuid != 0 && current->fsuid == 0) {
+                               if (old_fsuid != 0 && current_fsuid() == 0) {
                                        current->cap_effective =
                                                cap_raise_fs_set(
                                                    current->cap_effective,
@@ -640,7 +670,7 @@ int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
                    || ((current->securebits & SECURE_ALL_LOCKS
                         & ~arg2))                                    /*[2]*/
                    || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
-                   || (cap_capable(current, CAP_SETPCAP) != 0)) {    /*[4]*/
+                   || (cap_capable(current, CAP_SETPCAP, SECURITY_CAP_AUDIT) != 0)) { /*[4]*/
                        /*
                         * [1] no changing of bits that are locked
                         * [2] no unlocking of locks
@@ -705,7 +735,7 @@ int cap_vm_enough_memory(struct mm_struct *mm, long pages)
 {
        int cap_sys_admin = 0;
 
-       if (cap_capable(current, CAP_SYS_ADMIN) == 0)
+       if (cap_capable(current, CAP_SYS_ADMIN, SECURITY_CAP_NOAUDIT) == 0)
                cap_sys_admin = 1;
        return __vm_enough_memory(mm, pages, cap_sys_admin);
 }