Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/shaggy...
[pandora-kernel.git] / security / commoncap.c
index 43f9027..ea61bc7 100644 (file)
@@ -59,6 +59,12 @@ int cap_netlink_recv(struct sk_buff *skb, int cap)
 
 EXPORT_SYMBOL(cap_netlink_recv);
 
+/*
+ * NOTE WELL: cap_capable() cannot be used like the kernel's capable()
+ * function.  That is, it has the reverse semantics: cap_capable()
+ * 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)
 {
        /* Derived from include/linux/sched.h:capable. */
@@ -107,10 +113,11 @@ static inline int cap_block_setpcap(struct task_struct *target)
 static inline int cap_inh_is_capped(void)
 {
        /*
-        * return 1 if changes to the inheritable set are limited
-        * to the old permitted set.
+        * Return 1 if changes to the inheritable set are limited
+        * to the old permitted set. That is, if the current task
+        * does *not* possess the CAP_SETPCAP capability.
         */
-       return !cap_capable(current, CAP_SETPCAP);
+       return (cap_capable(current, CAP_SETPCAP) != 0);
 }
 
 #else /* ie., ndef CONFIG_SECURITY_FILE_CAPABILITIES */
@@ -190,7 +197,8 @@ int cap_inode_killpriv(struct dentry *dentry)
        return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
 }
 
-static inline int cap_from_disk(__le32 *caps, struct linux_binprm *bprm,
+static inline int cap_from_disk(struct vfs_cap_data *caps,
+                               struct linux_binprm *bprm,
                                int size)
 {
        __u32 magic_etc;
@@ -198,7 +206,7 @@ static inline int cap_from_disk(__le32 *caps, struct linux_binprm *bprm,
        if (size != XATTR_CAPS_SZ)
                return -EINVAL;
 
-       magic_etc = le32_to_cpu(caps[0]);
+       magic_etc = le32_to_cpu(caps->magic_etc);
 
        switch ((magic_etc & VFS_CAP_REVISION_MASK)) {
        case VFS_CAP_REVISION:
@@ -206,8 +214,8 @@ static inline int cap_from_disk(__le32 *caps, struct linux_binprm *bprm,
                        bprm->cap_effective = true;
                else
                        bprm->cap_effective = false;
-               bprm->cap_permitted = to_cap_t( le32_to_cpu(caps[1]) );
-               bprm->cap_inheritable = to_cap_t( le32_to_cpu(caps[2]) );
+               bprm->cap_permitted = to_cap_t(le32_to_cpu(caps->permitted));
+               bprm->cap_inheritable = to_cap_t(le32_to_cpu(caps->inheritable));
                return 0;
        default:
                return -EINVAL;
@@ -219,7 +227,7 @@ static int get_file_caps(struct linux_binprm *bprm)
 {
        struct dentry *dentry;
        int rc = 0;
-       __le32 v1caps[XATTR_CAPS_SZ];
+       struct vfs_cap_data incaps;
        struct inode *inode;
 
        if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID) {
@@ -232,8 +240,14 @@ static int get_file_caps(struct linux_binprm *bprm)
        if (!inode->i_op || !inode->i_op->getxattr)
                goto out;
 
-       rc = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, &v1caps,
-                                                       XATTR_CAPS_SZ);
+       rc = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);
+       if (rc > 0) {
+               if (rc == XATTR_CAPS_SZ)
+                       rc = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS,
+                                               &incaps, XATTR_CAPS_SZ);
+               else
+                       rc = -EINVAL;
+       }
        if (rc == -ENODATA || rc == -EOPNOTSUPP) {
                /* no data, that's ok */
                rc = 0;
@@ -242,7 +256,7 @@ static int get_file_caps(struct linux_binprm *bprm)
        if (rc < 0)
                goto out;
 
-       rc = cap_from_disk(v1caps, bprm, rc);
+       rc = cap_from_disk(&incaps, bprm, rc);
        if (rc)
                printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
                        __FUNCTION__, rc, bprm->filename);
@@ -519,6 +533,19 @@ int cap_task_kill(struct task_struct *p, struct siginfo *info,
        if (info != SEND_SIG_NOINFO && (is_si_special(info) || SI_FROMKERNEL(info)))
                return 0;
 
+       /*
+        * Running a setuid root program raises your capabilities.
+        * Killing your own setuid root processes was previously
+        * allowed.
+        * We must preserve legacy signal behavior in this case.
+        */
+       if (p->euid == 0 && p->uid == current->uid)
+               return 0;
+
+       /* sigcont is permitted within same session */
+       if (sig == SIGCONT && (task_session_nr(current) == task_session_nr(p)))
+               return 0;
+
        if (secid)
                /*
                 * Signal sent as a particular user.