/* Common capabilities, needed by capability.o.1*2* This program is free software; you can redistribute it and/or modify3* it under the terms of the GNU General Public License as published by4* the Free Software Foundation; either version 2 of the License, or5* (at your option) any later version.6*7*/89#include <linux/capability.h>10#include <linux/audit.h>11#include <linux/module.h>12#include <linux/init.h>13#include <linux/kernel.h>14#include <linux/security.h>15#include <linux/file.h>16#include <linux/mm.h>17#include <linux/mman.h>18#include <linux/pagemap.h>19#include <linux/swap.h>20#include <linux/skbuff.h>21#include <linux/netlink.h>22#include <linux/ptrace.h>23#include <linux/xattr.h>24#include <linux/hugetlb.h>25#include <linux/mount.h>26#include <linux/sched.h>27#include <linux/prctl.h>28#include <linux/securebits.h>29#include <linux/user_namespace.h>3031/*32* If a non-root user executes a setuid-root binary in33* !secure(SECURE_NOROOT) mode, then we raise capabilities.34* However if fE is also set, then the intent is for only35* the file capabilities to be applied, and the setuid-root36* bit is left on either to change the uid (plausible) or37* to get full privilege on a kernel without file capabilities38* support. So in that case we do not raise capabilities.39*40* Warn if that happens, once per boot.41*/42static void warn_setuid_and_fcaps_mixed(const char *fname)43{44static int warned;45if (!warned) {46printk(KERN_INFO "warning: `%s' has both setuid-root and"47" effective capabilities. Therefore not raising all"48" capabilities.\n", fname);49warned = 1;50}51}5253int cap_netlink_send(struct sock *sk, struct sk_buff *skb)54{55return 0;56}5758int cap_netlink_recv(struct sk_buff *skb, int cap)59{60if (!cap_raised(current_cap(), cap))61return -EPERM;62return 0;63}64EXPORT_SYMBOL(cap_netlink_recv);6566/**67* cap_capable - Determine whether a task has a particular effective capability68* @tsk: The task to query69* @cred: The credentials to use70* @ns: The user namespace in which we need the capability71* @cap: The capability to check for72* @audit: Whether to write an audit message or not73*74* Determine whether the nominated task has the specified capability amongst75* its effective set, returning 0 if it does, -ve if it does not.76*77* NOTE WELL: cap_has_capability() cannot be used like the kernel's capable()78* and has_capability() functions. That is, it has the reverse semantics:79* cap_has_capability() returns 0 when a task has a capability, but the80* kernel's capable() and has_capability() returns 1 for this case.81*/82int cap_capable(struct task_struct *tsk, const struct cred *cred,83struct user_namespace *targ_ns, int cap, int audit)84{85for (;;) {86/* The creator of the user namespace has all caps. */87if (targ_ns != &init_user_ns && targ_ns->creator == cred->user)88return 0;8990/* Do we have the necessary capabilities? */91if (targ_ns == cred->user->user_ns)92return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;9394/* Have we tried all of the parent namespaces? */95if (targ_ns == &init_user_ns)96return -EPERM;9798/*99*If you have a capability in a parent user ns, then you have100* it over all children user namespaces as well.101*/102targ_ns = targ_ns->creator->user_ns;103}104105/* We never get here */106}107108/**109* cap_settime - Determine whether the current process may set the system clock110* @ts: The time to set111* @tz: The timezone to set112*113* Determine whether the current process may set the system clock and timezone114* information, returning 0 if permission granted, -ve if denied.115*/116int cap_settime(const struct timespec *ts, const struct timezone *tz)117{118if (!capable(CAP_SYS_TIME))119return -EPERM;120return 0;121}122123/**124* cap_ptrace_access_check - Determine whether the current process may access125* another126* @child: The process to be accessed127* @mode: The mode of attachment.128*129* If we are in the same or an ancestor user_ns and have all the target130* task's capabilities, then ptrace access is allowed.131* If we have the ptrace capability to the target user_ns, then ptrace132* access is allowed.133* Else denied.134*135* Determine whether a process may access another, returning 0 if permission136* granted, -ve if denied.137*/138int cap_ptrace_access_check(struct task_struct *child, unsigned int mode)139{140int ret = 0;141const struct cred *cred, *child_cred;142143rcu_read_lock();144cred = current_cred();145child_cred = __task_cred(child);146if (cred->user->user_ns == child_cred->user->user_ns &&147cap_issubset(child_cred->cap_permitted, cred->cap_permitted))148goto out;149if (ns_capable(child_cred->user->user_ns, CAP_SYS_PTRACE))150goto out;151ret = -EPERM;152out:153rcu_read_unlock();154return ret;155}156157/**158* cap_ptrace_traceme - Determine whether another process may trace the current159* @parent: The task proposed to be the tracer160*161* If parent is in the same or an ancestor user_ns and has all current's162* capabilities, then ptrace access is allowed.163* If parent has the ptrace capability to current's user_ns, then ptrace164* access is allowed.165* Else denied.166*167* Determine whether the nominated task is permitted to trace the current168* process, returning 0 if permission is granted, -ve if denied.169*/170int cap_ptrace_traceme(struct task_struct *parent)171{172int ret = 0;173const struct cred *cred, *child_cred;174175rcu_read_lock();176cred = __task_cred(parent);177child_cred = current_cred();178if (cred->user->user_ns == child_cred->user->user_ns &&179cap_issubset(child_cred->cap_permitted, cred->cap_permitted))180goto out;181if (has_ns_capability(parent, child_cred->user->user_ns, CAP_SYS_PTRACE))182goto out;183ret = -EPERM;184out:185rcu_read_unlock();186return ret;187}188189/**190* cap_capget - Retrieve a task's capability sets191* @target: The task from which to retrieve the capability sets192* @effective: The place to record the effective set193* @inheritable: The place to record the inheritable set194* @permitted: The place to record the permitted set195*196* This function retrieves the capabilities of the nominated task and returns197* them to the caller.198*/199int cap_capget(struct task_struct *target, kernel_cap_t *effective,200kernel_cap_t *inheritable, kernel_cap_t *permitted)201{202const struct cred *cred;203204/* Derived from kernel/capability.c:sys_capget. */205rcu_read_lock();206cred = __task_cred(target);207*effective = cred->cap_effective;208*inheritable = cred->cap_inheritable;209*permitted = cred->cap_permitted;210rcu_read_unlock();211return 0;212}213214/*215* Determine whether the inheritable capabilities are limited to the old216* permitted set. Returns 1 if they are limited, 0 if they are not.217*/218static inline int cap_inh_is_capped(void)219{220221/* they are so limited unless the current task has the CAP_SETPCAP222* capability223*/224if (cap_capable(current, current_cred(),225current_cred()->user->user_ns, CAP_SETPCAP,226SECURITY_CAP_AUDIT) == 0)227return 0;228return 1;229}230231/**232* cap_capset - Validate and apply proposed changes to current's capabilities233* @new: The proposed new credentials; alterations should be made here234* @old: The current task's current credentials235* @effective: A pointer to the proposed new effective capabilities set236* @inheritable: A pointer to the proposed new inheritable capabilities set237* @permitted: A pointer to the proposed new permitted capabilities set238*239* This function validates and applies a proposed mass change to the current240* process's capability sets. The changes are made to the proposed new241* credentials, and assuming no error, will be committed by the caller of LSM.242*/243int cap_capset(struct cred *new,244const struct cred *old,245const kernel_cap_t *effective,246const kernel_cap_t *inheritable,247const kernel_cap_t *permitted)248{249if (cap_inh_is_capped() &&250!cap_issubset(*inheritable,251cap_combine(old->cap_inheritable,252old->cap_permitted)))253/* incapable of using this inheritable set */254return -EPERM;255256if (!cap_issubset(*inheritable,257cap_combine(old->cap_inheritable,258old->cap_bset)))259/* no new pI capabilities outside bounding set */260return -EPERM;261262/* verify restrictions on target's new Permitted set */263if (!cap_issubset(*permitted, old->cap_permitted))264return -EPERM;265266/* verify the _new_Effective_ is a subset of the _new_Permitted_ */267if (!cap_issubset(*effective, *permitted))268return -EPERM;269270new->cap_effective = *effective;271new->cap_inheritable = *inheritable;272new->cap_permitted = *permitted;273return 0;274}275276/*277* Clear proposed capability sets for execve().278*/279static inline void bprm_clear_caps(struct linux_binprm *bprm)280{281cap_clear(bprm->cred->cap_permitted);282bprm->cap_effective = false;283}284285/**286* cap_inode_need_killpriv - Determine if inode change affects privileges287* @dentry: The inode/dentry in being changed with change marked ATTR_KILL_PRIV288*289* Determine if an inode having a change applied that's marked ATTR_KILL_PRIV290* affects the security markings on that inode, and if it is, should291* inode_killpriv() be invoked or the change rejected?292*293* Returns 0 if granted; +ve if granted, but inode_killpriv() is required; and294* -ve to deny the change.295*/296int cap_inode_need_killpriv(struct dentry *dentry)297{298struct inode *inode = dentry->d_inode;299int error;300301if (!inode->i_op->getxattr)302return 0;303304error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);305if (error <= 0)306return 0;307return 1;308}309310/**311* cap_inode_killpriv - Erase the security markings on an inode312* @dentry: The inode/dentry to alter313*314* Erase the privilege-enhancing security markings on an inode.315*316* Returns 0 if successful, -ve on error.317*/318int cap_inode_killpriv(struct dentry *dentry)319{320struct inode *inode = dentry->d_inode;321322if (!inode->i_op->removexattr)323return 0;324325return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);326}327328/*329* Calculate the new process capability sets from the capability sets attached330* to a file.331*/332static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,333struct linux_binprm *bprm,334bool *effective)335{336struct cred *new = bprm->cred;337unsigned i;338int ret = 0;339340if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)341*effective = true;342343CAP_FOR_EACH_U32(i) {344__u32 permitted = caps->permitted.cap[i];345__u32 inheritable = caps->inheritable.cap[i];346347/*348* pP' = (X & fP) | (pI & fI)349*/350new->cap_permitted.cap[i] =351(new->cap_bset.cap[i] & permitted) |352(new->cap_inheritable.cap[i] & inheritable);353354if (permitted & ~new->cap_permitted.cap[i])355/* insufficient to execute correctly */356ret = -EPERM;357}358359/*360* For legacy apps, with no internal support for recognizing they361* do not have enough capabilities, we return an error if they are362* missing some "forced" (aka file-permitted) capabilities.363*/364return *effective ? ret : 0;365}366367/*368* Extract the on-exec-apply capability sets for an executable file.369*/370int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)371{372struct inode *inode = dentry->d_inode;373__u32 magic_etc;374unsigned tocopy, i;375int size;376struct vfs_cap_data caps;377378memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));379380if (!inode || !inode->i_op->getxattr)381return -ENODATA;382383size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps,384XATTR_CAPS_SZ);385if (size == -ENODATA || size == -EOPNOTSUPP)386/* no data, that's ok */387return -ENODATA;388if (size < 0)389return size;390391if (size < sizeof(magic_etc))392return -EINVAL;393394cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);395396switch (magic_etc & VFS_CAP_REVISION_MASK) {397case VFS_CAP_REVISION_1:398if (size != XATTR_CAPS_SZ_1)399return -EINVAL;400tocopy = VFS_CAP_U32_1;401break;402case VFS_CAP_REVISION_2:403if (size != XATTR_CAPS_SZ_2)404return -EINVAL;405tocopy = VFS_CAP_U32_2;406break;407default:408return -EINVAL;409}410411CAP_FOR_EACH_U32(i) {412if (i >= tocopy)413break;414cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted);415cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);416}417418return 0;419}420421/*422* Attempt to get the on-exec apply capability sets for an executable file from423* its xattrs and, if present, apply them to the proposed credentials being424* constructed by execve().425*/426static int get_file_caps(struct linux_binprm *bprm, bool *effective)427{428struct dentry *dentry;429int rc = 0;430struct cpu_vfs_cap_data vcaps;431432bprm_clear_caps(bprm);433434if (!file_caps_enabled)435return 0;436437if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)438return 0;439440dentry = dget(bprm->file->f_dentry);441442rc = get_vfs_caps_from_disk(dentry, &vcaps);443if (rc < 0) {444if (rc == -EINVAL)445printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n",446__func__, rc, bprm->filename);447else if (rc == -ENODATA)448rc = 0;449goto out;450}451452rc = bprm_caps_from_vfs_caps(&vcaps, bprm, effective);453if (rc == -EINVAL)454printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",455__func__, rc, bprm->filename);456457out:458dput(dentry);459if (rc)460bprm_clear_caps(bprm);461462return rc;463}464465/**466* cap_bprm_set_creds - Set up the proposed credentials for execve().467* @bprm: The execution parameters, including the proposed creds468*469* Set up the proposed credentials for a new execution context being470* constructed by execve(). The proposed creds in @bprm->cred is altered,471* which won't take effect immediately. Returns 0 if successful, -ve on error.472*/473int cap_bprm_set_creds(struct linux_binprm *bprm)474{475const struct cred *old = current_cred();476struct cred *new = bprm->cred;477bool effective;478int ret;479480effective = false;481ret = get_file_caps(bprm, &effective);482if (ret < 0)483return ret;484485if (!issecure(SECURE_NOROOT)) {486/*487* If the legacy file capability is set, then don't set privs488* for a setuid root binary run by a non-root user. Do set it489* for a root user just to cause least surprise to an admin.490*/491if (effective && new->uid != 0 && new->euid == 0) {492warn_setuid_and_fcaps_mixed(bprm->filename);493goto skip;494}495/*496* To support inheritance of root-permissions and suid-root497* executables under compatibility mode, we override the498* capability sets for the file.499*500* If only the real uid is 0, we do not set the effective bit.501*/502if (new->euid == 0 || new->uid == 0) {503/* pP' = (cap_bset & ~0) | (pI & ~0) */504new->cap_permitted = cap_combine(old->cap_bset,505old->cap_inheritable);506}507if (new->euid == 0)508effective = true;509}510skip:511512/* Don't let someone trace a set[ug]id/setpcap binary with the revised513* credentials unless they have the appropriate permit514*/515if ((new->euid != old->uid ||516new->egid != old->gid ||517!cap_issubset(new->cap_permitted, old->cap_permitted)) &&518bprm->unsafe & ~LSM_UNSAFE_PTRACE_CAP) {519/* downgrade; they get no more than they had, and maybe less */520if (!capable(CAP_SETUID)) {521new->euid = new->uid;522new->egid = new->gid;523}524new->cap_permitted = cap_intersect(new->cap_permitted,525old->cap_permitted);526}527528new->suid = new->fsuid = new->euid;529new->sgid = new->fsgid = new->egid;530531if (effective)532new->cap_effective = new->cap_permitted;533else534cap_clear(new->cap_effective);535bprm->cap_effective = effective;536537/*538* Audit candidate if current->cap_effective is set539*540* We do not bother to audit if 3 things are true:541* 1) cap_effective has all caps542* 2) we are root543* 3) root is supposed to have all caps (SECURE_NOROOT)544* Since this is just a normal root execing a process.545*546* Number 1 above might fail if you don't have a full bset, but I think547* that is interesting information to audit.548*/549if (!cap_isclear(new->cap_effective)) {550if (!cap_issubset(CAP_FULL_SET, new->cap_effective) ||551new->euid != 0 || new->uid != 0 ||552issecure(SECURE_NOROOT)) {553ret = audit_log_bprm_fcaps(bprm, new, old);554if (ret < 0)555return ret;556}557}558559new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);560return 0;561}562563/**564* cap_bprm_secureexec - Determine whether a secure execution is required565* @bprm: The execution parameters566*567* Determine whether a secure execution is required, return 1 if it is, and 0568* if it is not.569*570* The credentials have been committed by this point, and so are no longer571* available through @bprm->cred.572*/573int cap_bprm_secureexec(struct linux_binprm *bprm)574{575const struct cred *cred = current_cred();576577if (cred->uid != 0) {578if (bprm->cap_effective)579return 1;580if (!cap_isclear(cred->cap_permitted))581return 1;582}583584return (cred->euid != cred->uid ||585cred->egid != cred->gid);586}587588/**589* cap_inode_setxattr - Determine whether an xattr may be altered590* @dentry: The inode/dentry being altered591* @name: The name of the xattr to be changed592* @value: The value that the xattr will be changed to593* @size: The size of value594* @flags: The replacement flag595*596* Determine whether an xattr may be altered or set on an inode, returning 0 if597* permission is granted, -ve if denied.598*599* This is used to make sure security xattrs don't get updated or set by those600* who aren't privileged to do so.601*/602int cap_inode_setxattr(struct dentry *dentry, const char *name,603const void *value, size_t size, int flags)604{605if (!strcmp(name, XATTR_NAME_CAPS)) {606if (!capable(CAP_SETFCAP))607return -EPERM;608return 0;609}610611if (!strncmp(name, XATTR_SECURITY_PREFIX,612sizeof(XATTR_SECURITY_PREFIX) - 1) &&613!capable(CAP_SYS_ADMIN))614return -EPERM;615return 0;616}617618/**619* cap_inode_removexattr - Determine whether an xattr may be removed620* @dentry: The inode/dentry being altered621* @name: The name of the xattr to be changed622*623* Determine whether an xattr may be removed from an inode, returning 0 if624* permission is granted, -ve if denied.625*626* This is used to make sure security xattrs don't get removed by those who627* aren't privileged to remove them.628*/629int cap_inode_removexattr(struct dentry *dentry, const char *name)630{631if (!strcmp(name, XATTR_NAME_CAPS)) {632if (!capable(CAP_SETFCAP))633return -EPERM;634return 0;635}636637if (!strncmp(name, XATTR_SECURITY_PREFIX,638sizeof(XATTR_SECURITY_PREFIX) - 1) &&639!capable(CAP_SYS_ADMIN))640return -EPERM;641return 0;642}643644/*645* cap_emulate_setxuid() fixes the effective / permitted capabilities of646* a process after a call to setuid, setreuid, or setresuid.647*648* 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of649* {r,e,s}uid != 0, the permitted and effective capabilities are650* cleared.651*652* 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective653* capabilities of the process are cleared.654*655* 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective656* capabilities are set to the permitted capabilities.657*658* fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should659* never happen.660*661* -astor662*663* cevans - New behaviour, Oct '99664* A process may, via prctl(), elect to keep its capabilities when it665* calls setuid() and switches away from uid==0. Both permitted and666* effective sets will be retained.667* Without this change, it was impossible for a daemon to drop only some668* of its privilege. The call to setuid(!=0) would drop all privileges!669* Keeping uid 0 is not an option because uid 0 owns too many vital670* files..671* Thanks to Olaf Kirch and Peter Benie for spotting this.672*/673static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old)674{675if ((old->uid == 0 || old->euid == 0 || old->suid == 0) &&676(new->uid != 0 && new->euid != 0 && new->suid != 0) &&677!issecure(SECURE_KEEP_CAPS)) {678cap_clear(new->cap_permitted);679cap_clear(new->cap_effective);680}681if (old->euid == 0 && new->euid != 0)682cap_clear(new->cap_effective);683if (old->euid != 0 && new->euid == 0)684new->cap_effective = new->cap_permitted;685}686687/**688* cap_task_fix_setuid - Fix up the results of setuid() call689* @new: The proposed credentials690* @old: The current task's current credentials691* @flags: Indications of what has changed692*693* Fix up the results of setuid() call before the credential changes are694* actually applied, returning 0 to grant the changes, -ve to deny them.695*/696int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags)697{698switch (flags) {699case LSM_SETID_RE:700case LSM_SETID_ID:701case LSM_SETID_RES:702/* juggle the capabilities to follow [RES]UID changes unless703* otherwise suppressed */704if (!issecure(SECURE_NO_SETUID_FIXUP))705cap_emulate_setxuid(new, old);706break;707708case LSM_SETID_FS:709/* juggle the capabilties to follow FSUID changes, unless710* otherwise suppressed711*712* FIXME - is fsuser used for all CAP_FS_MASK capabilities?713* if not, we might be a bit too harsh here.714*/715if (!issecure(SECURE_NO_SETUID_FIXUP)) {716if (old->fsuid == 0 && new->fsuid != 0)717new->cap_effective =718cap_drop_fs_set(new->cap_effective);719720if (old->fsuid != 0 && new->fsuid == 0)721new->cap_effective =722cap_raise_fs_set(new->cap_effective,723new->cap_permitted);724}725break;726727default:728return -EINVAL;729}730731return 0;732}733734/*735* Rationale: code calling task_setscheduler, task_setioprio, and736* task_setnice, assumes that737* . if capable(cap_sys_nice), then those actions should be allowed738* . if not capable(cap_sys_nice), but acting on your own processes,739* then those actions should be allowed740* This is insufficient now since you can call code without suid, but741* yet with increased caps.742* So we check for increased caps on the target process.743*/744static int cap_safe_nice(struct task_struct *p)745{746int is_subset;747748rcu_read_lock();749is_subset = cap_issubset(__task_cred(p)->cap_permitted,750current_cred()->cap_permitted);751rcu_read_unlock();752753if (!is_subset && !capable(CAP_SYS_NICE))754return -EPERM;755return 0;756}757758/**759* cap_task_setscheduler - Detemine if scheduler policy change is permitted760* @p: The task to affect761*762* Detemine if the requested scheduler policy change is permitted for the763* specified task, returning 0 if permission is granted, -ve if denied.764*/765int cap_task_setscheduler(struct task_struct *p)766{767return cap_safe_nice(p);768}769770/**771* cap_task_ioprio - Detemine if I/O priority change is permitted772* @p: The task to affect773* @ioprio: The I/O priority to set774*775* Detemine if the requested I/O priority change is permitted for the specified776* task, returning 0 if permission is granted, -ve if denied.777*/778int cap_task_setioprio(struct task_struct *p, int ioprio)779{780return cap_safe_nice(p);781}782783/**784* cap_task_ioprio - Detemine if task priority change is permitted785* @p: The task to affect786* @nice: The nice value to set787*788* Detemine if the requested task priority change is permitted for the789* specified task, returning 0 if permission is granted, -ve if denied.790*/791int cap_task_setnice(struct task_struct *p, int nice)792{793return cap_safe_nice(p);794}795796/*797* Implement PR_CAPBSET_DROP. Attempt to remove the specified capability from798* the current task's bounding set. Returns 0 on success, -ve on error.799*/800static long cap_prctl_drop(struct cred *new, unsigned long cap)801{802if (!capable(CAP_SETPCAP))803return -EPERM;804if (!cap_valid(cap))805return -EINVAL;806807cap_lower(new->cap_bset, cap);808return 0;809}810811/**812* cap_task_prctl - Implement process control functions for this security module813* @option: The process control function requested814* @arg2, @arg3, @arg4, @arg5: The argument data for this function815*816* Allow process control functions (sys_prctl()) to alter capabilities; may817* also deny access to other functions not otherwise implemented here.818*819* Returns 0 or +ve on success, -ENOSYS if this function is not implemented820* here, other -ve on error. If -ENOSYS is returned, sys_prctl() and other LSM821* modules will consider performing the function.822*/823int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,824unsigned long arg4, unsigned long arg5)825{826struct cred *new;827long error = 0;828829new = prepare_creds();830if (!new)831return -ENOMEM;832833switch (option) {834case PR_CAPBSET_READ:835error = -EINVAL;836if (!cap_valid(arg2))837goto error;838error = !!cap_raised(new->cap_bset, arg2);839goto no_change;840841case PR_CAPBSET_DROP:842error = cap_prctl_drop(new, arg2);843if (error < 0)844goto error;845goto changed;846847/*848* The next four prctl's remain to assist with transitioning a849* system from legacy UID=0 based privilege (when filesystem850* capabilities are not in use) to a system using filesystem851* capabilities only - as the POSIX.1e draft intended.852*853* Note:854*855* PR_SET_SECUREBITS =856* issecure_mask(SECURE_KEEP_CAPS_LOCKED)857* | issecure_mask(SECURE_NOROOT)858* | issecure_mask(SECURE_NOROOT_LOCKED)859* | issecure_mask(SECURE_NO_SETUID_FIXUP)860* | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED)861*862* will ensure that the current process and all of its863* children will be locked into a pure864* capability-based-privilege environment.865*/866case PR_SET_SECUREBITS:867error = -EPERM;868if ((((new->securebits & SECURE_ALL_LOCKS) >> 1)869& (new->securebits ^ arg2)) /*[1]*/870|| ((new->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/871|| (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/872|| (cap_capable(current, current_cred(),873current_cred()->user->user_ns, CAP_SETPCAP,874SECURITY_CAP_AUDIT) != 0) /*[4]*/875/*876* [1] no changing of bits that are locked877* [2] no unlocking of locks878* [3] no setting of unsupported bits879* [4] doing anything requires privilege (go read about880* the "sendmail capabilities bug")881*/882)883/* cannot change a locked bit */884goto error;885new->securebits = arg2;886goto changed;887888case PR_GET_SECUREBITS:889error = new->securebits;890goto no_change;891892case PR_GET_KEEPCAPS:893if (issecure(SECURE_KEEP_CAPS))894error = 1;895goto no_change;896897case PR_SET_KEEPCAPS:898error = -EINVAL;899if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */900goto error;901error = -EPERM;902if (issecure(SECURE_KEEP_CAPS_LOCKED))903goto error;904if (arg2)905new->securebits |= issecure_mask(SECURE_KEEP_CAPS);906else907new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);908goto changed;909910default:911/* No functionality available - continue with default */912error = -ENOSYS;913goto error;914}915916/* Functionality provided */917changed:918return commit_creds(new);919920no_change:921error:922abort_creds(new);923return error;924}925926/**927* cap_vm_enough_memory - Determine whether a new virtual mapping is permitted928* @mm: The VM space in which the new mapping is to be made929* @pages: The size of the mapping930*931* Determine whether the allocation of a new virtual mapping by the current932* task is permitted, returning 0 if permission is granted, -ve if not.933*/934int cap_vm_enough_memory(struct mm_struct *mm, long pages)935{936int cap_sys_admin = 0;937938if (cap_capable(current, current_cred(), &init_user_ns, CAP_SYS_ADMIN,939SECURITY_CAP_NOAUDIT) == 0)940cap_sys_admin = 1;941return __vm_enough_memory(mm, pages, cap_sys_admin);942}943944/*945* cap_file_mmap - check if able to map given addr946* @file: unused947* @reqprot: unused948* @prot: unused949* @flags: unused950* @addr: address attempting to be mapped951* @addr_only: unused952*953* If the process is attempting to map memory below dac_mmap_min_addr they need954* CAP_SYS_RAWIO. The other parameters to this function are unused by the955* capability security module. Returns 0 if this mapping should be allowed956* -EPERM if not.957*/958int cap_file_mmap(struct file *file, unsigned long reqprot,959unsigned long prot, unsigned long flags,960unsigned long addr, unsigned long addr_only)961{962int ret = 0;963964if (addr < dac_mmap_min_addr) {965ret = cap_capable(current, current_cred(), &init_user_ns, CAP_SYS_RAWIO,966SECURITY_CAP_AUDIT);967/* set PF_SUPERPRIV if it turns out we allow the low mmap */968if (ret == 0)969current->flags |= PF_SUPERPRIV;970}971return ret;972}973974975