// SPDX-License-Identifier: GPL-2.01/*2* linux/kernel/capability.c3*4* Copyright (C) 1997 Andrew Main <[email protected]>5*6* Integrated into 2.1.97+, Andrew G. Morgan <[email protected]>7* 30 May 2002: Cleanup, Robert M. Love <[email protected]>8*/910#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt1112#include <linux/audit.h>13#include <linux/capability.h>14#include <linux/mm.h>15#include <linux/export.h>16#include <linux/security.h>17#include <linux/syscalls.h>18#include <linux/pid_namespace.h>19#include <linux/user_namespace.h>20#include <linux/uaccess.h>2122int file_caps_enabled = 1;2324static int __init file_caps_disable(char *str)25{26file_caps_enabled = 0;27return 1;28}29__setup("no_file_caps", file_caps_disable);3031#ifdef CONFIG_MULTIUSER32/*33* More recent versions of libcap are available from:34*35* http://www.kernel.org/pub/linux/libs/security/linux-privs/36*/3738static void warn_legacy_capability_use(void)39{40pr_info_once("warning: `%s' uses 32-bit capabilities (legacy support in use)\n",41current->comm);42}4344/*45* Version 2 capabilities worked fine, but the linux/capability.h file46* that accompanied their introduction encouraged their use without47* the necessary user-space source code changes. As such, we have48* created a version 3 with equivalent functionality to version 2, but49* with a header change to protect legacy source code from using50* version 2 when it wanted to use version 1. If your system has code51* that trips the following warning, it is using version 2 specific52* capabilities and may be doing so insecurely.53*54* The remedy is to either upgrade your version of libcap (to 2.10+,55* if the application is linked against it), or recompile your56* application with modern kernel headers and this warning will go57* away.58*/5960static void warn_deprecated_v2(void)61{62pr_info_once("warning: `%s' uses deprecated v2 capabilities in a way that may be insecure\n",63current->comm);64}6566/*67* Version check. Return the number of u32s in each capability flag68* array, or a negative value on error.69*/70static int cap_validate_magic(cap_user_header_t header, unsigned *tocopy)71{72__u32 version;7374if (get_user(version, &header->version))75return -EFAULT;7677switch (version) {78case _LINUX_CAPABILITY_VERSION_1:79warn_legacy_capability_use();80*tocopy = _LINUX_CAPABILITY_U32S_1;81break;82case _LINUX_CAPABILITY_VERSION_2:83warn_deprecated_v2();84fallthrough; /* v3 is otherwise equivalent to v2 */85case _LINUX_CAPABILITY_VERSION_3:86*tocopy = _LINUX_CAPABILITY_U32S_3;87break;88default:89if (put_user((u32)_KERNEL_CAPABILITY_VERSION, &header->version))90return -EFAULT;91return -EINVAL;92}9394return 0;95}9697/*98* The only thing that can change the capabilities of the current99* process is the current process. As such, we can't be in this code100* at the same time as we are in the process of setting capabilities101* in this process. The net result is that we can limit our use of102* locks to when we are reading the caps of another process.103*/104static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp,105kernel_cap_t *pIp, kernel_cap_t *pPp)106{107int ret;108109if (pid && (pid != task_pid_vnr(current))) {110const struct task_struct *target;111112rcu_read_lock();113114target = find_task_by_vpid(pid);115if (!target)116ret = -ESRCH;117else118ret = security_capget(target, pEp, pIp, pPp);119120rcu_read_unlock();121} else122ret = security_capget(current, pEp, pIp, pPp);123124return ret;125}126127/**128* sys_capget - get the capabilities of a given process.129* @header: pointer to struct that contains capability version and130* target pid data131* @dataptr: pointer to struct that contains the effective, permitted,132* and inheritable capabilities that are returned133*134* Returns 0 on success and < 0 on error.135*/136SYSCALL_DEFINE2(capget, cap_user_header_t, header, cap_user_data_t, dataptr)137{138int ret = 0;139pid_t pid;140unsigned tocopy;141kernel_cap_t pE, pI, pP;142struct __user_cap_data_struct kdata[2];143144ret = cap_validate_magic(header, &tocopy);145if ((dataptr == NULL) || (ret != 0))146return ((dataptr == NULL) && (ret == -EINVAL)) ? 0 : ret;147148if (get_user(pid, &header->pid))149return -EFAULT;150151if (pid < 0)152return -EINVAL;153154ret = cap_get_target_pid(pid, &pE, &pI, &pP);155if (ret)156return ret;157158/*159* Annoying legacy format with 64-bit capabilities exposed160* as two sets of 32-bit fields, so we need to split the161* capability values up.162*/163kdata[0].effective = pE.val; kdata[1].effective = pE.val >> 32;164kdata[0].permitted = pP.val; kdata[1].permitted = pP.val >> 32;165kdata[0].inheritable = pI.val; kdata[1].inheritable = pI.val >> 32;166167/*168* Note, in the case, tocopy < _KERNEL_CAPABILITY_U32S,169* we silently drop the upper capabilities here. This170* has the effect of making older libcap171* implementations implicitly drop upper capability172* bits when they perform a: capget/modify/capset173* sequence.174*175* This behavior is considered fail-safe176* behavior. Upgrading the application to a newer177* version of libcap will enable access to the newer178* capabilities.179*180* An alternative would be to return an error here181* (-ERANGE), but that causes legacy applications to182* unexpectedly fail; the capget/modify/capset aborts183* before modification is attempted and the application184* fails.185*/186if (copy_to_user(dataptr, kdata, tocopy * sizeof(kdata[0])))187return -EFAULT;188189return 0;190}191192static kernel_cap_t mk_kernel_cap(u32 low, u32 high)193{194return (kernel_cap_t) { (low | ((u64)high << 32)) & CAP_VALID_MASK };195}196197/**198* sys_capset - set capabilities for a process or (*) a group of processes199* @header: pointer to struct that contains capability version and200* target pid data201* @data: pointer to struct that contains the effective, permitted,202* and inheritable capabilities203*204* Set capabilities for the current process only. The ability to any other205* process(es) has been deprecated and removed.206*207* The restrictions on setting capabilities are specified as:208*209* I: any raised capabilities must be a subset of the old permitted210* P: any raised capabilities must be a subset of the old permitted211* E: must be set to a subset of new permitted212*213* Returns 0 on success and < 0 on error.214*/215SYSCALL_DEFINE2(capset, cap_user_header_t, header, const cap_user_data_t, data)216{217struct __user_cap_data_struct kdata[2] = { { 0, }, };218unsigned tocopy, copybytes;219kernel_cap_t inheritable, permitted, effective;220struct cred *new;221int ret;222pid_t pid;223224ret = cap_validate_magic(header, &tocopy);225if (ret != 0)226return ret;227228if (get_user(pid, &header->pid))229return -EFAULT;230231/* may only affect current now */232if (pid != 0 && pid != task_pid_vnr(current))233return -EPERM;234235copybytes = tocopy * sizeof(struct __user_cap_data_struct);236if (copybytes > sizeof(kdata))237return -EFAULT;238239if (copy_from_user(&kdata, data, copybytes))240return -EFAULT;241242effective = mk_kernel_cap(kdata[0].effective, kdata[1].effective);243permitted = mk_kernel_cap(kdata[0].permitted, kdata[1].permitted);244inheritable = mk_kernel_cap(kdata[0].inheritable, kdata[1].inheritable);245246new = prepare_creds();247if (!new)248return -ENOMEM;249250ret = security_capset(new, current_cred(),251&effective, &inheritable, &permitted);252if (ret < 0)253goto error;254255audit_log_capset(new, current_cred());256257return commit_creds(new);258259error:260abort_creds(new);261return ret;262}263264/**265* has_ns_capability - Does a task have a capability in a specific user ns266* @t: The task in question267* @ns: target user namespace268* @cap: The capability to be tested for269*270* Return true if the specified task has the given superior capability271* currently in effect to the specified user namespace, false if not.272*273* Note that this does not set PF_SUPERPRIV on the task.274*/275bool has_ns_capability(struct task_struct *t,276struct user_namespace *ns, int cap)277{278int ret;279280rcu_read_lock();281ret = security_capable(__task_cred(t), ns, cap, CAP_OPT_NONE);282rcu_read_unlock();283284return (ret == 0);285}286287/**288* has_ns_capability_noaudit - Does a task have a capability (unaudited)289* in a specific user ns.290* @t: The task in question291* @ns: target user namespace292* @cap: The capability to be tested for293*294* Return true if the specified task has the given superior capability295* currently in effect to the specified user namespace, false if not.296* Do not write an audit message for the check.297*298* Note that this does not set PF_SUPERPRIV on the task.299*/300bool has_ns_capability_noaudit(struct task_struct *t,301struct user_namespace *ns, int cap)302{303int ret;304305rcu_read_lock();306ret = security_capable(__task_cred(t), ns, cap, CAP_OPT_NOAUDIT);307rcu_read_unlock();308309return (ret == 0);310}311312/**313* has_capability_noaudit - Does a task have a capability (unaudited) in the314* initial user ns315* @t: The task in question316* @cap: The capability to be tested for317*318* Return true if the specified task has the given superior capability319* currently in effect to init_user_ns, false if not. Don't write an320* audit message for the check.321*322* Note that this does not set PF_SUPERPRIV on the task.323*/324bool has_capability_noaudit(struct task_struct *t, int cap)325{326return has_ns_capability_noaudit(t, &init_user_ns, cap);327}328EXPORT_SYMBOL(has_capability_noaudit);329330static bool ns_capable_common(struct user_namespace *ns,331int cap,332unsigned int opts)333{334int capable;335336if (unlikely(!cap_valid(cap))) {337pr_crit("capable() called with invalid cap=%u\n", cap);338BUG();339}340341capable = security_capable(current_cred(), ns, cap, opts);342if (capable == 0) {343current->flags |= PF_SUPERPRIV;344return true;345}346return false;347}348349/**350* ns_capable - Determine if the current task has a superior capability in effect351* @ns: The usernamespace we want the capability in352* @cap: The capability to be tested for353*354* Return true if the current task has the given superior capability currently355* available for use, false if not.356*357* This sets PF_SUPERPRIV on the task if the capability is available on the358* assumption that it's about to be used.359*/360bool ns_capable(struct user_namespace *ns, int cap)361{362return ns_capable_common(ns, cap, CAP_OPT_NONE);363}364EXPORT_SYMBOL(ns_capable);365366/**367* ns_capable_noaudit - Determine if the current task has a superior capability368* (unaudited) in effect369* @ns: The usernamespace we want the capability in370* @cap: The capability to be tested for371*372* Return true if the current task has the given superior capability currently373* available for use, false if not.374*375* This sets PF_SUPERPRIV on the task if the capability is available on the376* assumption that it's about to be used.377*/378bool ns_capable_noaudit(struct user_namespace *ns, int cap)379{380return ns_capable_common(ns, cap, CAP_OPT_NOAUDIT);381}382EXPORT_SYMBOL(ns_capable_noaudit);383384/**385* ns_capable_setid - Determine if the current task has a superior capability386* in effect, while signalling that this check is being done from within a387* setid or setgroups syscall.388* @ns: The usernamespace we want the capability in389* @cap: The capability to be tested for390*391* Return true if the current task has the given superior capability currently392* available for use, false if not.393*394* This sets PF_SUPERPRIV on the task if the capability is available on the395* assumption that it's about to be used.396*/397bool ns_capable_setid(struct user_namespace *ns, int cap)398{399return ns_capable_common(ns, cap, CAP_OPT_INSETID);400}401EXPORT_SYMBOL(ns_capable_setid);402403/**404* capable - Determine if the current task has a superior capability in effect405* @cap: The capability to be tested for406*407* Return true if the current task has the given superior capability currently408* available for use, false if not.409*410* This sets PF_SUPERPRIV on the task if the capability is available on the411* assumption that it's about to be used.412*/413bool capable(int cap)414{415return ns_capable(&init_user_ns, cap);416}417EXPORT_SYMBOL(capable);418#endif /* CONFIG_MULTIUSER */419420/**421* file_ns_capable - Determine if the file's opener had a capability in effect422* @file: The file we want to check423* @ns: The usernamespace we want the capability in424* @cap: The capability to be tested for425*426* Return true if task that opened the file had a capability in effect427* when the file was opened.428*429* This does not set PF_SUPERPRIV because the caller may not430* actually be privileged.431*/432bool file_ns_capable(const struct file *file, struct user_namespace *ns,433int cap)434{435436if (WARN_ON_ONCE(!cap_valid(cap)))437return false;438439if (security_capable(file->f_cred, ns, cap, CAP_OPT_NONE) == 0)440return true;441442return false;443}444EXPORT_SYMBOL(file_ns_capable);445446/**447* privileged_wrt_inode_uidgid - Do capabilities in the namespace work over the inode?448* @ns: The user namespace in question449* @idmap: idmap of the mount @inode was found from450* @inode: The inode in question451*452* Return true if the inode uid and gid are within the namespace.453*/454bool privileged_wrt_inode_uidgid(struct user_namespace *ns,455struct mnt_idmap *idmap,456const struct inode *inode)457{458return vfsuid_has_mapping(ns, i_uid_into_vfsuid(idmap, inode)) &&459vfsgid_has_mapping(ns, i_gid_into_vfsgid(idmap, inode));460}461462/**463* capable_wrt_inode_uidgid - Check nsown_capable and uid and gid mapped464* @idmap: idmap of the mount @inode was found from465* @inode: The inode in question466* @cap: The capability in question467*468* Return true if the current task has the given capability targeted at469* its own user namespace and that the given inode's uid and gid are470* mapped into the current user namespace.471*/472bool capable_wrt_inode_uidgid(struct mnt_idmap *idmap,473const struct inode *inode, int cap)474{475struct user_namespace *ns = current_user_ns();476477return ns_capable(ns, cap) &&478privileged_wrt_inode_uidgid(ns, idmap, inode);479}480EXPORT_SYMBOL(capable_wrt_inode_uidgid);481482/**483* ptracer_capable - Determine if the ptracer holds CAP_SYS_PTRACE in the namespace484* @tsk: The task that may be ptraced485* @ns: The user namespace to search for CAP_SYS_PTRACE in486*487* Return true if the task that is ptracing the current task had CAP_SYS_PTRACE488* in the specified user namespace.489*/490bool ptracer_capable(struct task_struct *tsk, struct user_namespace *ns)491{492int ret = 0; /* An absent tracer adds no restrictions */493const struct cred *cred;494495rcu_read_lock();496cred = rcu_dereference(tsk->ptracer_cred);497if (cred)498ret = security_capable(cred, ns, CAP_SYS_PTRACE,499CAP_OPT_NOAUDIT);500rcu_read_unlock();501return (ret == 0);502}503504505