]> nv-tegra.nvidia Code Review - linux-2.6.git/blobdiff - security/commoncap.c
Fix install_process_keyring error handling
[linux-2.6.git] / security / commoncap.c
index e3f36ef629fabd095269cf6a4fc8a1aef2ed69d1..5e632b4857e443d8031eaa17c0e2bd7e877b3d14 100644 (file)
@@ -1,4 +1,4 @@
-/* Common capabilities, needed by capability.o and root_plug.o
+/* Common capabilities, needed by capability.o.
  *
  *     This program is free software; you can redistribute it and/or modify
  *     it under the terms of the GNU General Public License as published by
 #include <linux/sched.h>
 #include <linux/prctl.h>
 #include <linux/securebits.h>
+#include <linux/syslog.h>
+
+/*
+ * If a non-root user executes a setuid-root binary in
+ * !secure(SECURE_NOROOT) mode, then we raise capabilities.
+ * However if fE is also set, then the intent is for only
+ * the file capabilities to be applied, and the setuid-root
+ * bit is left on either to change the uid (plausible) or
+ * to get full privilege on a kernel without file capabilities
+ * support.  So in that case we do not raise capabilities.
+ *
+ * Warn if that happens, once per boot.
+ */
+static void warn_setuid_and_fcaps_mixed(const char *fname)
+{
+       static int warned;
+       if (!warned) {
+               printk(KERN_INFO "warning: `%s' has both setuid-root and"
+                       " effective capabilities. Therefore not raising all"
+                       " capabilities.\n", fname);
+               warned = 1;
+       }
+}
 
 int cap_netlink_send(struct sock *sk, struct sk_buff *skb)
 {
-       NETLINK_CB(skb).eff_cap = current->cap_effective;
+       NETLINK_CB(skb).eff_cap = current_cap();
        return 0;
 }
 
@@ -40,23 +63,37 @@ int cap_netlink_recv(struct sk_buff *skb, int cap)
                return -EPERM;
        return 0;
 }
-
 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.
+/**
+ * cap_capable - Determine whether a task has a particular effective capability
+ * @tsk: The task to query
+ * @cred: The credentials to use
+ * @cap: The capability to check for
+ * @audit: Whether to write an audit message or not
+ *
+ * Determine whether the nominated task has the specified capability amongst
+ * its effective set, returning 0 if it does, -ve if it does not.
+ *
+ * NOTE WELL: cap_has_capability() cannot be used like the kernel's capable()
+ * and has_capability() functions.  That is, it has the reverse semantics:
+ * cap_has_capability() returns 0 when a task has a capability, but the
+ * kernel's capable() and has_capability() returns 1 for this case.
  */
-int cap_capable(struct task_struct *tsk, int cap, int audit)
+int cap_capable(struct task_struct *tsk, const struct cred *cred, int cap,
+               int audit)
 {
-       /* Derived from include/linux/sched.h:capable. */
-       if (cap_raised(tsk->cap_effective, cap))
-               return 0;
-       return -EPERM;
+       return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
 }
 
+/**
+ * cap_settime - Determine whether the current process may set the system clock
+ * @ts: The time to set
+ * @tz: The timezone to set
+ *
+ * Determine whether the current process may set the system clock and timezone
+ * information, returning 0 if permission granted, -ve if denied.
+ */
 int cap_settime(struct timespec *ts, struct timezone *tz)
 {
        if (!capable(CAP_SYS_TIME))
@@ -64,114 +101,160 @@ int cap_settime(struct timespec *ts, struct timezone *tz)
        return 0;
 }
 
-int cap_ptrace_may_access(struct task_struct *child, unsigned int mode)
+/**
+ * cap_ptrace_access_check - Determine whether the current process may access
+ *                        another
+ * @child: The process to be accessed
+ * @mode: The mode of attachment.
+ *
+ * Determine whether a process may access another, returning 0 if permission
+ * granted, -ve if denied.
+ */
+int cap_ptrace_access_check(struct task_struct *child, unsigned int mode)
 {
-       /* Derived from arch/i386/kernel/ptrace.c:sys_ptrace. */
-       if (cap_issubset(child->cap_permitted, current->cap_permitted))
-               return 0;
-       if (capable(CAP_SYS_PTRACE))
-               return 0;
-       return -EPERM;
+       int ret = 0;
+
+       rcu_read_lock();
+       if (!cap_issubset(__task_cred(child)->cap_permitted,
+                         current_cred()->cap_permitted) &&
+           !capable(CAP_SYS_PTRACE))
+               ret = -EPERM;
+       rcu_read_unlock();
+       return ret;
 }
 
+/**
+ * cap_ptrace_traceme - Determine whether another process may trace the current
+ * @parent: The task proposed to be the tracer
+ *
+ * Determine whether the nominated task is permitted to trace the current
+ * process, returning 0 if permission is granted, -ve if denied.
+ */
 int cap_ptrace_traceme(struct task_struct *parent)
 {
-       /* Derived from arch/i386/kernel/ptrace.c:sys_ptrace. */
-       if (cap_issubset(current->cap_permitted, parent->cap_permitted))
-               return 0;
-       if (has_capability(parent, CAP_SYS_PTRACE))
-               return 0;
-       return -EPERM;
+       int ret = 0;
+
+       rcu_read_lock();
+       if (!cap_issubset(current_cred()->cap_permitted,
+                         __task_cred(parent)->cap_permitted) &&
+           !has_capability(parent, CAP_SYS_PTRACE))
+               ret = -EPERM;
+       rcu_read_unlock();
+       return ret;
 }
 
-int cap_capget (struct task_struct *target, kernel_cap_t *effective,
-               kernel_cap_t *inheritable, kernel_cap_t *permitted)
+/**
+ * cap_capget - Retrieve a task's capability sets
+ * @target: The task from which to retrieve the capability sets
+ * @effective: The place to record the effective set
+ * @inheritable: The place to record the inheritable set
+ * @permitted: The place to record the permitted set
+ *
+ * This function retrieves the capabilities of the nominated task and returns
+ * them to the caller.
+ */
+int cap_capget(struct task_struct *target, kernel_cap_t *effective,
+              kernel_cap_t *inheritable, kernel_cap_t *permitted)
 {
+       const struct cred *cred;
+
        /* Derived from kernel/capability.c:sys_capget. */
-       *effective = target->cap_effective;
-       *inheritable = target->cap_inheritable;
-       *permitted = target->cap_permitted;
+       rcu_read_lock();
+       cred = __task_cred(target);
+       *effective   = cred->cap_effective;
+       *inheritable = cred->cap_inheritable;
+       *permitted   = cred->cap_permitted;
+       rcu_read_unlock();
        return 0;
 }
 
-#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
-
+/*
+ * Determine whether the inheritable capabilities are limited to the old
+ * permitted set.  Returns 1 if they are limited, 0 if they are not.
+ */
 static inline int cap_inh_is_capped(void)
 {
-       /*
-        * 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, 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_inh_is_capped(void) { return 1; }
-static inline int cap_limit_ptraced_target(void)
-{
-       return !capable(CAP_SETPCAP);
+       /* they are so limited unless the current task has the CAP_SETPCAP
+        * capability
+        */
+       if (cap_capable(current, current_cred(), CAP_SETPCAP,
+                       SECURITY_CAP_AUDIT) == 0)
+               return 0;
+       return 1;
 }
 
-#endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
-
-int cap_capset_check (kernel_cap_t *effective,
-                     kernel_cap_t *inheritable, kernel_cap_t *permitted)
-{
-       if (cap_inh_is_capped()
-           && !cap_issubset(*inheritable,
-                            cap_combine(current->cap_inheritable,
-                                        current->cap_permitted))) {
+/**
+ * cap_capset - Validate and apply proposed changes to current's capabilities
+ * @new: The proposed new credentials; alterations should be made here
+ * @old: The current task's current credentials
+ * @effective: A pointer to the proposed new effective capabilities set
+ * @inheritable: A pointer to the proposed new inheritable capabilities set
+ * @permitted: A pointer to the proposed new permitted capabilities set
+ *
+ * This function validates and applies a proposed mass change to the current
+ * process's capability sets.  The changes are made to the proposed new
+ * credentials, and assuming no error, will be committed by the caller of LSM.
+ */
+int cap_capset(struct cred *new,
+              const struct cred *old,
+              const kernel_cap_t *effective,
+              const kernel_cap_t *inheritable,
+              const kernel_cap_t *permitted)
+{
+       if (cap_inh_is_capped() &&
+           !cap_issubset(*inheritable,
+                         cap_combine(old->cap_inheritable,
+                                     old->cap_permitted)))
                /* incapable of using this inheritable set */
                return -EPERM;
-       }
+
        if (!cap_issubset(*inheritable,
-                          cap_combine(current->cap_inheritable,
-                                      current->cap_bset))) {
+                         cap_combine(old->cap_inheritable,
+                                     old->cap_bset)))
                /* no new pI capabilities outside bounding set */
                return -EPERM;
-       }
 
        /* verify restrictions on target's new Permitted set */
-       if (!cap_issubset (*permitted,
-                          cap_combine (current->cap_permitted,
-                                       current->cap_permitted))) {
+       if (!cap_issubset(*permitted, old->cap_permitted))
                return -EPERM;
-       }
 
        /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
-       if (!cap_issubset (*effective, *permitted)) {
+       if (!cap_issubset(*effective, *permitted))
                return -EPERM;
-       }
 
+       new->cap_effective   = *effective;
+       new->cap_inheritable = *inheritable;
+       new->cap_permitted   = *permitted;
        return 0;
 }
 
-void cap_capset_set (kernel_cap_t *effective,
-                    kernel_cap_t *inheritable, kernel_cap_t *permitted)
-{
-       current->cap_effective = *effective;
-       current->cap_inheritable = *inheritable;
-       current->cap_permitted = *permitted;
-}
-
+/*
+ * Clear proposed capability sets for execve().
+ */
 static inline void bprm_clear_caps(struct linux_binprm *bprm)
 {
-       cap_clear(bprm->cap_post_exec_permitted);
+       cap_clear(bprm->cred->cap_permitted);
        bprm->cap_effective = false;
 }
 
-#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
-
+/**
+ * cap_inode_need_killpriv - Determine if inode change affects privileges
+ * @dentry: The inode/dentry in being changed with change marked ATTR_KILL_PRIV
+ *
+ * Determine if an inode having a change applied that's marked ATTR_KILL_PRIV
+ * affects the security markings on that inode, and if it is, should
+ * inode_killpriv() be invoked or the change rejected?
+ *
+ * Returns 0 if granted; +ve if granted, but inode_killpriv() is required; and
+ * -ve to deny the change.
+ */
 int cap_inode_need_killpriv(struct dentry *dentry)
 {
        struct inode *inode = dentry->d_inode;
        int error;
 
-       if (!inode->i_op || !inode->i_op->getxattr)
+       if (!inode->i_op->getxattr)
               return 0;
 
        error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);
@@ -180,26 +263,38 @@ int cap_inode_need_killpriv(struct dentry *dentry)
        return 1;
 }
 
+/**
+ * cap_inode_killpriv - Erase the security markings on an inode
+ * @dentry: The inode/dentry to alter
+ *
+ * Erase the privilege-enhancing security markings on an inode.
+ *
+ * Returns 0 if successful, -ve on error.
+ */
 int cap_inode_killpriv(struct dentry *dentry)
 {
        struct inode *inode = dentry->d_inode;
 
-       if (!inode->i_op || !inode->i_op->removexattr)
+       if (!inode->i_op->removexattr)
               return 0;
 
        return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
 }
 
+/*
+ * Calculate the new process capability sets from the capability sets attached
+ * to a file.
+ */
 static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
-                                         struct linux_binprm *bprm)
+                                         struct linux_binprm *bprm,
+                                         bool *effective)
 {
+       struct cred *new = bprm->cred;
        unsigned i;
        int ret = 0;
 
        if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
-               bprm->cap_effective = true;
-       else
-               bprm->cap_effective = false;
+               *effective = true;
 
        CAP_FOR_EACH_U32(i) {
                __u32 permitted = caps->permitted.cap[i];
@@ -208,16 +303,13 @@ static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
                /*
                 * pP' = (X & fP) | (pI & fI)
                 */
-               bprm->cap_post_exec_permitted.cap[i] =
-                       (current->cap_bset.cap[i] & permitted) |
-                       (current->cap_inheritable.cap[i] & inheritable);
+               new->cap_permitted.cap[i] =
+                       (new->cap_bset.cap[i] & permitted) |
+                       (new->cap_inheritable.cap[i] & inheritable);
 
-               if (permitted & ~bprm->cap_post_exec_permitted.cap[i]) {
-                       /*
-                        * insufficient to execute correctly
-                        */
+               if (permitted & ~new->cap_permitted.cap[i])
+                       /* insufficient to execute correctly */
                        ret = -EPERM;
-               }
        }
 
        /*
@@ -225,9 +317,12 @@ static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
         * 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 *effective ? ret : 0;
 }
 
+/*
+ * Extract the on-exec-apply capability sets for an executable file.
+ */
 int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
 {
        struct inode *inode = dentry->d_inode;
@@ -238,15 +333,14 @@ int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data
 
        memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
 
-       if (!inode || !inode->i_op || !inode->i_op->getxattr)
+       if (!inode || !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) {
+       if (size == -ENODATA || size == -EOPNOTSUPP)
                /* no data, that's ok */
                return -ENODATA;
-       }
        if (size < 0)
                return size;
 
@@ -255,7 +349,7 @@ int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data
 
        cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
 
-       switch ((magic_etc & VFS_CAP_REVISION_MASK)) {
+       switch (magic_etc & VFS_CAP_REVISION_MASK) {
        case VFS_CAP_REVISION_1:
                if (size != XATTR_CAPS_SZ_1)
                        return -EINVAL;
@@ -276,11 +370,16 @@ int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data
                cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted);
                cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);
        }
+
        return 0;
 }
 
-/* Locate any VFS capabilities: */
-static int get_file_caps(struct linux_binprm *bprm)
+/*
+ * Attempt to get the on-exec apply capability sets for an executable file from
+ * its xattrs and, if present, apply them to the proposed credentials being
+ * constructed by execve().
+ */
+static int get_file_caps(struct linux_binprm *bprm, bool *effective)
 {
        struct dentry *dentry;
        int rc = 0;
@@ -306,7 +405,10 @@ static int get_file_caps(struct linux_binprm *bprm)
                goto out;
        }
 
-       rc = bprm_caps_from_vfs_caps(&vcaps, bprm);
+       rc = bprm_caps_from_vfs_caps(&vcaps, bprm, effective);
+       if (rc == -EINVAL)
+               printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
+                      __func__, rc, bprm->filename);
 
 out:
        dput(dentry);
@@ -316,93 +418,82 @@ out:
        return rc;
 }
 
-#else
-int cap_inode_need_killpriv(struct dentry *dentry)
-{
-       return 0;
-}
-
-int cap_inode_killpriv(struct dentry *dentry)
-{
-       return 0;
-}
-
-static inline int get_file_caps(struct linux_binprm *bprm)
-{
-       bprm_clear_caps(bprm);
-       return 0;
-}
-#endif
-
-int cap_bprm_set_security (struct linux_binprm *bprm)
+/**
+ * cap_bprm_set_creds - Set up the proposed credentials for execve().
+ * @bprm: The execution parameters, including the proposed creds
+ *
+ * Set up the proposed credentials for a new execution context being
+ * constructed by execve().  The proposed creds in @bprm->cred is altered,
+ * which won't take effect immediately.  Returns 0 if successful, -ve on error.
+ */
+int cap_bprm_set_creds(struct linux_binprm *bprm)
 {
+       const struct cred *old = current_cred();
+       struct cred *new = bprm->cred;
+       bool effective;
        int ret;
 
-       ret = get_file_caps(bprm);
+       effective = false;
+       ret = get_file_caps(bprm, &effective);
+       if (ret < 0)
+               return ret;
 
        if (!issecure(SECURE_NOROOT)) {
+               /*
+                * If the legacy file capability is set, then don't set privs
+                * for a setuid root binary run by a non-root user.  Do set it
+                * for a root user just to cause least surprise to an admin.
+                */
+               if (effective && new->uid != 0 && new->euid == 0) {
+                       warn_setuid_and_fcaps_mixed(bprm->filename);
+                       goto skip;
+               }
                /*
                 * To support inheritance of root-permissions and suid-root
                 * executables under compatibility mode, we override the
                 * capability sets for the file.
                 *
-                * If only the real uid is 0, we do not set the effective
-                * bit.
+                * If only the real uid is 0, we do not set the effective bit.
                 */
-               if (bprm->e_uid == 0 || current_uid() == 0) {
+               if (new->euid == 0 || new->uid == 0) {
                        /* pP' = (cap_bset & ~0) | (pI & ~0) */
-                       bprm->cap_post_exec_permitted = cap_combine(
-                               current->cap_bset, current->cap_inheritable
-                               );
-                       bprm->cap_effective = (bprm->e_uid == 0);
-                       ret = 0;
+                       new->cap_permitted = cap_combine(old->cap_bset,
+                                                        old->cap_inheritable);
                }
+               if (new->euid == 0)
+                       effective = true;
        }
+skip:
 
-       return ret;
-}
-
-void cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe)
-{
-       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);
-               current->pdeath_signal = 0;
-
-               if (unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
-                       if (!capable(CAP_SETUID)) {
-                               bprm->e_uid = uid;
-                               bprm->e_gid = gid;
-                       }
-                       if (cap_limit_ptraced_target()) {
-                               bprm->cap_post_exec_permitted = cap_intersect(
-                                       bprm->cap_post_exec_permitted,
-                                       current->cap_permitted);
-                       }
+       /* Don't let someone trace a set[ug]id/setpcap binary with the revised
+        * credentials unless they have the appropriate permit
+        */
+       if ((new->euid != old->uid ||
+            new->egid != old->gid ||
+            !cap_issubset(new->cap_permitted, old->cap_permitted)) &&
+           bprm->unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
+               /* downgrade; they get no more than they had, and maybe less */
+               if (!capable(CAP_SETUID)) {
+                       new->euid = new->uid;
+                       new->egid = new->gid;
                }
+               new->cap_permitted = cap_intersect(new->cap_permitted,
+                                                  old->cap_permitted);
        }
 
-       current->suid = current->euid = current->fsuid = bprm->e_uid;
-       current->sgid = current->egid = current->fsgid = bprm->e_gid;
+       new->suid = new->fsuid = new->euid;
+       new->sgid = new->fsgid = new->egid;
 
-       /* For init, we want to retain the capabilities set
-        * in the init_task struct. Thus we skip the usual
-        * capability rules */
+       /* For init, we want to retain the capabilities set in the initial
+        * task.  Thus we skip the usual capability rules
+        */
        if (!is_global_init(current)) {
-               current->cap_permitted = bprm->cap_post_exec_permitted;
-               if (bprm->cap_effective)
-                       current->cap_effective = bprm->cap_post_exec_permitted;
+               if (effective)
+                       new->cap_effective = new->cap_permitted;
                else
-                       cap_clear(current->cap_effective);
+                       cap_clear(new->cap_effective);
        }
+       bprm->cap_effective = effective;
 
        /*
         * Audit candidate if current->cap_effective is set
@@ -416,29 +507,59 @@ void cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe)
         * 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);
+       if (!cap_isclear(new->cap_effective)) {
+               if (!cap_issubset(CAP_FULL_SET, new->cap_effective) ||
+                   new->euid != 0 || new->uid != 0 ||
+                   issecure(SECURE_NOROOT)) {
+                       ret = audit_log_bprm_fcaps(bprm, new, old);
+                       if (ret < 0)
+                               return ret;
+               }
        }
 
-       current->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
+       new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
+       return 0;
 }
 
-int cap_bprm_secureexec (struct linux_binprm *bprm)
+/**
+ * cap_bprm_secureexec - Determine whether a secure execution is required
+ * @bprm: The execution parameters
+ *
+ * Determine whether a secure execution is required, return 1 if it is, and 0
+ * if it is not.
+ *
+ * The credentials have been committed by this point, and so are no longer
+ * available through @bprm->cred.
+ */
+int cap_bprm_secureexec(struct linux_binprm *bprm)
 {
-       if (current_uid() != 0) {
+       const struct cred *cred = current_cred();
+
+       if (cred->uid != 0) {
                if (bprm->cap_effective)
                        return 1;
-               if (!cap_isclear(bprm->cap_post_exec_permitted))
+               if (!cap_isclear(cred->cap_permitted))
                        return 1;
        }
 
-       return (current_euid() != current_uid() ||
-               current_egid() != current_gid());
+       return (cred->euid != cred->uid ||
+               cred->egid != cred->gid);
 }
 
+/**
+ * cap_inode_setxattr - Determine whether an xattr may be altered
+ * @dentry: The inode/dentry being altered
+ * @name: The name of the xattr to be changed
+ * @value: The value that the xattr will be changed to
+ * @size: The size of value
+ * @flags: The replacement flag
+ *
+ * Determine whether an xattr may be altered or set on an inode, returning 0 if
+ * permission is granted, -ve if denied.
+ *
+ * This is used to make sure security xattrs don't get updated or set by those
+ * who aren't privileged to do so.
+ */
 int cap_inode_setxattr(struct dentry *dentry, const char *name,
                       const void *value, size_t size, int flags)
 {
@@ -446,28 +567,42 @@ int cap_inode_setxattr(struct dentry *dentry, const char *name,
                if (!capable(CAP_SETFCAP))
                        return -EPERM;
                return 0;
-       } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
-                    sizeof(XATTR_SECURITY_PREFIX) - 1)  &&
+       }
+
+       if (!strncmp(name, XATTR_SECURITY_PREFIX,
+                    sizeof(XATTR_SECURITY_PREFIX) - 1) &&
            !capable(CAP_SYS_ADMIN))
                return -EPERM;
        return 0;
 }
 
+/**
+ * cap_inode_removexattr - Determine whether an xattr may be removed
+ * @dentry: The inode/dentry being altered
+ * @name: The name of the xattr to be changed
+ *
+ * Determine whether an xattr may be removed from an inode, returning 0 if
+ * permission is granted, -ve if denied.
+ *
+ * This is used to make sure security xattrs don't get removed by those who
+ * aren't privileged to remove them.
+ */
 int cap_inode_removexattr(struct dentry *dentry, const char *name)
 {
        if (!strcmp(name, XATTR_NAME_CAPS)) {
                if (!capable(CAP_SETFCAP))
                        return -EPERM;
                return 0;
-       } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
-                    sizeof(XATTR_SECURITY_PREFIX) - 1)  &&
+       }
+
+       if (!strncmp(name, XATTR_SECURITY_PREFIX,
+                    sizeof(XATTR_SECURITY_PREFIX) - 1) &&
            !capable(CAP_SYS_ADMIN))
                return -EPERM;
        return 0;
 }
 
-/* moved from kernel/sys.c. */
-/* 
+/*
  * cap_emulate_setxuid() fixes the effective / permitted capabilities of
  * a process after a call to setuid, setreuid, or setresuid.
  *
@@ -481,10 +616,10 @@ int cap_inode_removexattr(struct dentry *dentry, const char *name)
  *  3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
  *  capabilities are set to the permitted capabilities.
  *
- *  fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should 
+ *  fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
  *  never happen.
  *
- *  -astor 
+ *  -astor
  *
  * cevans - New behaviour, Oct '99
  * A process may, via prctl(), elect to keep its capabilities when it
@@ -496,63 +631,60 @@ int cap_inode_removexattr(struct dentry *dentry, const char *name)
  * files..
  * Thanks to Olaf Kirch and Peter Benie for spotting this.
  */
-static inline void cap_emulate_setxuid (int old_ruid, int old_euid,
-                                       int old_suid)
+static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old)
 {
-       uid_t euid = current_euid();
-
-       if ((old_ruid == 0 || old_euid == 0 || old_suid == 0) &&
-           (current_uid()  != 0 && euid != 0 && current_suid() != 0) &&
+       if ((old->uid == 0 || old->euid == 0 || old->suid == 0) &&
+           (new->uid != 0 && new->euid != 0 && new->suid != 0) &&
            !issecure(SECURE_KEEP_CAPS)) {
-               cap_clear (current->cap_permitted);
-               cap_clear (current->cap_effective);
-       }
-       if (old_euid == 0 && euid != 0) {
-               cap_clear (current->cap_effective);
-       }
-       if (old_euid != 0 && euid == 0) {
-               current->cap_effective = current->cap_permitted;
+               cap_clear(new->cap_permitted);
+               cap_clear(new->cap_effective);
        }
+       if (old->euid == 0 && new->euid != 0)
+               cap_clear(new->cap_effective);
+       if (old->euid != 0 && new->euid == 0)
+               new->cap_effective = new->cap_permitted;
 }
 
-int cap_task_post_setuid (uid_t old_ruid, uid_t old_euid, uid_t old_suid,
-                         int flags)
+/**
+ * cap_task_fix_setuid - Fix up the results of setuid() call
+ * @new: The proposed credentials
+ * @old: The current task's current credentials
+ * @flags: Indications of what has changed
+ *
+ * Fix up the results of setuid() call before the credential changes are
+ * actually applied, returning 0 to grant the changes, -ve to deny them.
+ */
+int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags)
 {
        switch (flags) {
        case LSM_SETID_RE:
        case LSM_SETID_ID:
        case LSM_SETID_RES:
-               /* Copied from kernel/sys.c:setreuid/setuid/setresuid. */
-               if (!issecure (SECURE_NO_SETUID_FIXUP)) {
-                       cap_emulate_setxuid (old_ruid, old_euid, old_suid);
-               }
+               /* juggle the capabilities to follow [RES]UID changes unless
+                * otherwise suppressed */
+               if (!issecure(SECURE_NO_SETUID_FIXUP))
+                       cap_emulate_setxuid(new, old);
                break;
-       case LSM_SETID_FS:
-               {
-                       uid_t old_fsuid = old_ruid;
-
-                       /* Copied from kernel/sys.c:setfsuid. */
-
-                       /*
-                        * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
-                        *          if not, we might be a bit too harsh here.
-                        */
 
-                       if (!issecure (SECURE_NO_SETUID_FIXUP)) {
-                               if (old_fsuid == 0 && current_fsuid() != 0) {
-                                       current->cap_effective =
-                                               cap_drop_fs_set(
-                                                   current->cap_effective);
-                               }
-                               if (old_fsuid != 0 && current_fsuid() == 0) {
-                                       current->cap_effective =
-                                               cap_raise_fs_set(
-                                                   current->cap_effective,
-                                                   current->cap_permitted);
-                               }
-                       }
-                       break;
+       case LSM_SETID_FS:
+               /* juggle the capabilties to follow FSUID changes, unless
+                * otherwise suppressed
+                *
+                * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
+                *          if not, we might be a bit too harsh here.
+                */
+               if (!issecure(SECURE_NO_SETUID_FIXUP)) {
+                       if (old->fsuid == 0 && new->fsuid != 0)
+                               new->cap_effective =
+                                       cap_drop_fs_set(new->cap_effective);
+
+                       if (old->fsuid != 0 && new->fsuid == 0)
+                               new->cap_effective =
+                                       cap_raise_fs_set(new->cap_effective,
+                                                        new->cap_permitted);
                }
+               break;
+
        default:
                return -EINVAL;
        }
@@ -560,7 +692,6 @@ int cap_task_post_setuid (uid_t old_ruid, uid_t old_euid, uid_t old_suid,
        return 0;
 }
 
-#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
 /*
  * Rationale: code calling task_setscheduler, task_setioprio, and
  * task_setnice, assumes that
@@ -573,77 +704,106 @@ int cap_task_post_setuid (uid_t old_ruid, uid_t old_euid, uid_t old_suid,
  */
 static int cap_safe_nice(struct task_struct *p)
 {
-       if (!cap_issubset(p->cap_permitted, current->cap_permitted) &&
-           !capable(CAP_SYS_NICE))
+       int is_subset;
+
+       rcu_read_lock();
+       is_subset = cap_issubset(__task_cred(p)->cap_permitted,
+                                current_cred()->cap_permitted);
+       rcu_read_unlock();
+
+       if (!is_subset && !capable(CAP_SYS_NICE))
                return -EPERM;
        return 0;
 }
 
-int cap_task_setscheduler (struct task_struct *p, int policy,
-                          struct sched_param *lp)
+/**
+ * cap_task_setscheduler - Detemine if scheduler policy change is permitted
+ * @p: The task to affect
+ *
+ * Detemine if the requested scheduler policy change is permitted for the
+ * specified task, returning 0 if permission is granted, -ve if denied.
+ */
+int cap_task_setscheduler(struct task_struct *p)
 {
        return cap_safe_nice(p);
 }
 
-int cap_task_setioprio (struct task_struct *p, int ioprio)
+/**
+ * cap_task_ioprio - Detemine if I/O priority change is permitted
+ * @p: The task to affect
+ * @ioprio: The I/O priority to set
+ *
+ * Detemine if the requested I/O priority change is permitted for the specified
+ * task, returning 0 if permission is granted, -ve if denied.
+ */
+int cap_task_setioprio(struct task_struct *p, int ioprio)
 {
        return cap_safe_nice(p);
 }
 
-int cap_task_setnice (struct task_struct *p, int nice)
+/**
+ * cap_task_ioprio - Detemine if task priority change is permitted
+ * @p: The task to affect
+ * @nice: The nice value to set
+ *
+ * Detemine if the requested task priority change is permitted for the
+ * specified task, returning 0 if permission is granted, -ve if denied.
+ */
+int cap_task_setnice(struct task_struct *p, int nice)
 {
        return cap_safe_nice(p);
 }
 
 /*
- * called from kernel/sys.c for prctl(PR_CABSET_DROP)
- * done without task_capability_lock() because it introduces
- * no new races - i.e. only another task doing capget() on
- * this task could get inconsistent info.  There can be no
- * racing writer bc a task can only change its own caps.
+ * Implement PR_CAPBSET_DROP.  Attempt to remove the specified capability from
+ * the current task's bounding set.  Returns 0 on success, -ve on error.
  */
-static long cap_prctl_drop(unsigned long cap)
+static long cap_prctl_drop(struct cred *new, unsigned long cap)
 {
        if (!capable(CAP_SETPCAP))
                return -EPERM;
        if (!cap_valid(cap))
                return -EINVAL;
-       cap_lower(current->cap_bset, cap);
-       return 0;
-}
 
-#else
-int cap_task_setscheduler (struct task_struct *p, int policy,
-                          struct sched_param *lp)
-{
-       return 0;
-}
-int cap_task_setioprio (struct task_struct *p, int ioprio)
-{
-       return 0;
-}
-int cap_task_setnice (struct task_struct *p, int nice)
-{
+       cap_lower(new->cap_bset, cap);
        return 0;
 }
-#endif
 
+/**
+ * cap_task_prctl - Implement process control functions for this security module
+ * @option: The process control function requested
+ * @arg2, @arg3, @arg4, @arg5: The argument data for this function
+ *
+ * Allow process control functions (sys_prctl()) to alter capabilities; may
+ * also deny access to other functions not otherwise implemented here.
+ *
+ * Returns 0 or +ve on success, -ENOSYS if this function is not implemented
+ * here, other -ve on error.  If -ENOSYS is returned, sys_prctl() and other LSM
+ * modules will consider performing the function.
+ */
 int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
-                  unsigned long arg4, unsigned long arg5, long *rc_p)
+                  unsigned long arg4, unsigned long arg5)
 {
+       struct cred *new;
        long error = 0;
 
+       new = prepare_creds();
+       if (!new)
+               return -ENOMEM;
+
        switch (option) {
        case PR_CAPBSET_READ:
+               error = -EINVAL;
                if (!cap_valid(arg2))
-                       error = -EINVAL;
-               else
-                       error = !!cap_raised(current->cap_bset, arg2);
-               break;
-#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
+                       goto error;
+               error = !!cap_raised(new->cap_bset, arg2);
+               goto no_change;
+
        case PR_CAPBSET_DROP:
-               error = cap_prctl_drop(arg2);
-               break;
+               error = cap_prctl_drop(new, arg2);
+               if (error < 0)
+                       goto error;
+               goto changed;
 
        /*
         * The next four prctl's remain to assist with transitioning a
@@ -665,12 +825,13 @@ int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
         * capability-based-privilege environment.
         */
        case PR_SET_SECUREBITS:
-               if ((((current->securebits & SECURE_ALL_LOCKS) >> 1)
-                    & (current->securebits ^ arg2))                  /*[1]*/
-                   || ((current->securebits & SECURE_ALL_LOCKS
-                        & ~arg2))                                    /*[2]*/
-                   || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
-                   || (cap_capable(current, CAP_SETPCAP, SECURITY_CAP_AUDIT) != 0)) { /*[4]*/
+               error = -EPERM;
+               if ((((new->securebits & SECURE_ALL_LOCKS) >> 1)
+                    & (new->securebits ^ arg2))                        /*[1]*/
+                   || ((new->securebits & SECURE_ALL_LOCKS & ~arg2))   /*[2]*/
+                   || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS))   /*[3]*/
+                   || (cap_capable(current, current_cred(), CAP_SETPCAP,
+                                   SECURITY_CAP_AUDIT) != 0)           /*[4]*/
                        /*
                         * [1] no changing of bits that are locked
                         * [2] no unlocking of locks
@@ -678,65 +839,112 @@ int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
                         * [4] doing anything requires privilege (go read about
                         *     the "sendmail capabilities bug")
                         */
-                       error = -EPERM;  /* cannot change a locked bit */
-               } else {
-                       current->securebits = arg2;
-               }
-               break;
-       case PR_GET_SECUREBITS:
-               error = current->securebits;
-               break;
+                   )
+                       /* cannot change a locked bit */
+                       goto error;
+               new->securebits = arg2;
+               goto changed;
 
-#endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
+       case PR_GET_SECUREBITS:
+               error = new->securebits;
+               goto no_change;
 
        case PR_GET_KEEPCAPS:
                if (issecure(SECURE_KEEP_CAPS))
                        error = 1;
-               break;
+               goto no_change;
+
        case PR_SET_KEEPCAPS:
+               error = -EINVAL;
                if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
-                       error = -EINVAL;
-               else if (issecure(SECURE_KEEP_CAPS_LOCKED))
-                       error = -EPERM;
-               else if (arg2)
-                       current->securebits |= issecure_mask(SECURE_KEEP_CAPS);
+                       goto error;
+               error = -EPERM;
+               if (issecure(SECURE_KEEP_CAPS_LOCKED))
+                       goto error;
+               if (arg2)
+                       new->securebits |= issecure_mask(SECURE_KEEP_CAPS);
                else
-                       current->securebits &=
-                               ~issecure_mask(SECURE_KEEP_CAPS);
-               break;
+                       new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
+               goto changed;
 
        default:
                /* No functionality available - continue with default */
-               return 0;
+               error = -ENOSYS;
+               goto error;
        }
 
        /* Functionality provided */
-       *rc_p = error;
-       return 1;
-}
+changed:
+       return commit_creds(new);
 
-void cap_task_reparent_to_init (struct task_struct *p)
-{
-       cap_set_init_eff(p->cap_effective);
-       cap_clear(p->cap_inheritable);
-       cap_set_full(p->cap_permitted);
-       p->securebits = SECUREBITS_DEFAULT;
-       return;
+no_change:
+error:
+       abort_creds(new);
+       return error;
 }
 
-int cap_syslog (int type)
+/**
+ * cap_syslog - Determine whether syslog function is permitted
+ * @type: Function requested
+ * @from_file: Whether this request came from an open file (i.e. /proc)
+ *
+ * Determine whether the current process is permitted to use a particular
+ * syslog function, returning 0 if permission is granted, -ve if not.
+ */
+int cap_syslog(int type, bool from_file)
 {
-       if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN))
+       if (type != SYSLOG_ACTION_OPEN && from_file)
+               return 0;
+       if ((type != SYSLOG_ACTION_READ_ALL &&
+            type != SYSLOG_ACTION_SIZE_BUFFER) && !capable(CAP_SYS_ADMIN))
                return -EPERM;
        return 0;
 }
 
+/**
+ * cap_vm_enough_memory - Determine whether a new virtual mapping is permitted
+ * @mm: The VM space in which the new mapping is to be made
+ * @pages: The size of the mapping
+ *
+ * Determine whether the allocation of a new virtual mapping by the current
+ * task is permitted, returning 0 if permission is granted, -ve if not.
+ */
 int cap_vm_enough_memory(struct mm_struct *mm, long pages)
 {
        int cap_sys_admin = 0;
 
-       if (cap_capable(current, CAP_SYS_ADMIN, SECURITY_CAP_NOAUDIT) == 0)
+       if (cap_capable(current, current_cred(), CAP_SYS_ADMIN,
+                       SECURITY_CAP_NOAUDIT) == 0)
                cap_sys_admin = 1;
        return __vm_enough_memory(mm, pages, cap_sys_admin);
 }
 
+/*
+ * cap_file_mmap - check if able to map given addr
+ * @file: unused
+ * @reqprot: unused
+ * @prot: unused
+ * @flags: unused
+ * @addr: address attempting to be mapped
+ * @addr_only: unused
+ *
+ * If the process is attempting to map memory below dac_mmap_min_addr they need
+ * CAP_SYS_RAWIO.  The other parameters to this function are unused by the
+ * capability security module.  Returns 0 if this mapping should be allowed
+ * -EPERM if not.
+ */
+int cap_file_mmap(struct file *file, unsigned long reqprot,
+                 unsigned long prot, unsigned long flags,
+                 unsigned long addr, unsigned long addr_only)
+{
+       int ret = 0;
+
+       if (addr < dac_mmap_min_addr) {
+               ret = cap_capable(current, current_cred(), CAP_SYS_RAWIO,
+                                 SECURITY_CAP_AUDIT);
+               /* set PF_SUPERPRIV if it turns out we allow the low mmap */
+               if (ret == 0)
+                       current->flags |= PF_SUPERPRIV;
+       }
+       return ret;
+}