/*1* linux/kernel/capability.c2*3* Copyright (C) 1997 Andrew Main <[email protected]>4*5* Integrated into 2.1.97+, Andrew G. Morgan <[email protected]>6* 30 May 2002: Cleanup, Robert M. Love <[email protected]>7*/89#include <linux/audit.h>10#include <linux/capability.h>11#include <linux/mm.h>12#include <linux/module.h>13#include <linux/security.h>14#include <linux/syscalls.h>15#include <linux/pid_namespace.h>16#include <linux/user_namespace.h>17#include <asm/uaccess.h>1819/*20* Leveraged for setting/resetting capabilities21*/2223const kernel_cap_t __cap_empty_set = CAP_EMPTY_SET;2425EXPORT_SYMBOL(__cap_empty_set);2627int file_caps_enabled = 1;2829static int __init file_caps_disable(char *str)30{31file_caps_enabled = 0;32return 1;33}34__setup("no_file_caps", file_caps_disable);3536/*37* More recent versions of libcap are available from:38*39* http://www.kernel.org/pub/linux/libs/security/linux-privs/40*/4142static void warn_legacy_capability_use(void)43{44static int warned;45if (!warned) {46char name[sizeof(current->comm)];4748printk(KERN_INFO "warning: `%s' uses 32-bit capabilities"49" (legacy support in use)\n",50get_task_comm(name, current));51warned = 1;52}53}5455/*56* Version 2 capabilities worked fine, but the linux/capability.h file57* that accompanied their introduction encouraged their use without58* the necessary user-space source code changes. As such, we have59* created a version 3 with equivalent functionality to version 2, but60* with a header change to protect legacy source code from using61* version 2 when it wanted to use version 1. If your system has code62* that trips the following warning, it is using version 2 specific63* capabilities and may be doing so insecurely.64*65* The remedy is to either upgrade your version of libcap (to 2.10+,66* if the application is linked against it), or recompile your67* application with modern kernel headers and this warning will go68* away.69*/7071static void warn_deprecated_v2(void)72{73static int warned;7475if (!warned) {76char name[sizeof(current->comm)];7778printk(KERN_INFO "warning: `%s' uses deprecated v2"79" capabilities in a way that may be insecure.\n",80get_task_comm(name, current));81warned = 1;82}83}8485/*86* Version check. Return the number of u32s in each capability flag87* array, or a negative value on error.88*/89static int cap_validate_magic(cap_user_header_t header, unsigned *tocopy)90{91__u32 version;9293if (get_user(version, &header->version))94return -EFAULT;9596switch (version) {97case _LINUX_CAPABILITY_VERSION_1:98warn_legacy_capability_use();99*tocopy = _LINUX_CAPABILITY_U32S_1;100break;101case _LINUX_CAPABILITY_VERSION_2:102warn_deprecated_v2();103/*104* fall through - v3 is otherwise equivalent to v2.105*/106case _LINUX_CAPABILITY_VERSION_3:107*tocopy = _LINUX_CAPABILITY_U32S_3;108break;109default:110if (put_user((u32)_KERNEL_CAPABILITY_VERSION, &header->version))111return -EFAULT;112return -EINVAL;113}114115return 0;116}117118/*119* The only thing that can change the capabilities of the current120* process is the current process. As such, we can't be in this code121* at the same time as we are in the process of setting capabilities122* in this process. The net result is that we can limit our use of123* locks to when we are reading the caps of another process.124*/125static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp,126kernel_cap_t *pIp, kernel_cap_t *pPp)127{128int ret;129130if (pid && (pid != task_pid_vnr(current))) {131struct task_struct *target;132133rcu_read_lock();134135target = find_task_by_vpid(pid);136if (!target)137ret = -ESRCH;138else139ret = security_capget(target, pEp, pIp, pPp);140141rcu_read_unlock();142} else143ret = security_capget(current, pEp, pIp, pPp);144145return ret;146}147148/**149* sys_capget - get the capabilities of a given process.150* @header: pointer to struct that contains capability version and151* target pid data152* @dataptr: pointer to struct that contains the effective, permitted,153* and inheritable capabilities that are returned154*155* Returns 0 on success and < 0 on error.156*/157SYSCALL_DEFINE2(capget, cap_user_header_t, header, cap_user_data_t, dataptr)158{159int ret = 0;160pid_t pid;161unsigned tocopy;162kernel_cap_t pE, pI, pP;163164ret = cap_validate_magic(header, &tocopy);165if ((dataptr == NULL) || (ret != 0))166return ((dataptr == NULL) && (ret == -EINVAL)) ? 0 : ret;167168if (get_user(pid, &header->pid))169return -EFAULT;170171if (pid < 0)172return -EINVAL;173174ret = cap_get_target_pid(pid, &pE, &pI, &pP);175if (!ret) {176struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];177unsigned i;178179for (i = 0; i < tocopy; i++) {180kdata[i].effective = pE.cap[i];181kdata[i].permitted = pP.cap[i];182kdata[i].inheritable = pI.cap[i];183}184185/*186* Note, in the case, tocopy < _KERNEL_CAPABILITY_U32S,187* we silently drop the upper capabilities here. This188* has the effect of making older libcap189* implementations implicitly drop upper capability190* bits when they perform a: capget/modify/capset191* sequence.192*193* This behavior is considered fail-safe194* behavior. Upgrading the application to a newer195* version of libcap will enable access to the newer196* capabilities.197*198* An alternative would be to return an error here199* (-ERANGE), but that causes legacy applications to200* unexpectidly fail; the capget/modify/capset aborts201* before modification is attempted and the application202* fails.203*/204if (copy_to_user(dataptr, kdata, tocopy205* sizeof(struct __user_cap_data_struct))) {206return -EFAULT;207}208}209210return ret;211}212213/**214* sys_capset - set capabilities for a process or (*) a group of processes215* @header: pointer to struct that contains capability version and216* target pid data217* @data: pointer to struct that contains the effective, permitted,218* and inheritable capabilities219*220* Set capabilities for the current process only. The ability to any other221* process(es) has been deprecated and removed.222*223* The restrictions on setting capabilities are specified as:224*225* I: any raised capabilities must be a subset of the old permitted226* P: any raised capabilities must be a subset of the old permitted227* E: must be set to a subset of new permitted228*229* Returns 0 on success and < 0 on error.230*/231SYSCALL_DEFINE2(capset, cap_user_header_t, header, const cap_user_data_t, data)232{233struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];234unsigned i, tocopy, copybytes;235kernel_cap_t inheritable, permitted, effective;236struct cred *new;237int ret;238pid_t pid;239240ret = cap_validate_magic(header, &tocopy);241if (ret != 0)242return ret;243244if (get_user(pid, &header->pid))245return -EFAULT;246247/* may only affect current now */248if (pid != 0 && pid != task_pid_vnr(current))249return -EPERM;250251copybytes = tocopy * sizeof(struct __user_cap_data_struct);252if (copybytes > sizeof(kdata))253return -EFAULT;254255if (copy_from_user(&kdata, data, copybytes))256return -EFAULT;257258for (i = 0; i < tocopy; i++) {259effective.cap[i] = kdata[i].effective;260permitted.cap[i] = kdata[i].permitted;261inheritable.cap[i] = kdata[i].inheritable;262}263while (i < _KERNEL_CAPABILITY_U32S) {264effective.cap[i] = 0;265permitted.cap[i] = 0;266inheritable.cap[i] = 0;267i++;268}269270new = prepare_creds();271if (!new)272return -ENOMEM;273274ret = security_capset(new, current_cred(),275&effective, &inheritable, &permitted);276if (ret < 0)277goto error;278279audit_log_capset(pid, new, current_cred());280281return commit_creds(new);282283error:284abort_creds(new);285return ret;286}287288/**289* has_capability - Does a task have a capability in init_user_ns290* @t: The task in question291* @cap: The capability to be tested for292*293* Return true if the specified task has the given superior capability294* currently in effect to the initial user namespace, false if not.295*296* Note that this does not set PF_SUPERPRIV on the task.297*/298bool has_capability(struct task_struct *t, int cap)299{300int ret = security_real_capable(t, &init_user_ns, cap);301302return (ret == 0);303}304305/**306* has_capability - Does a task have a capability in a specific user ns307* @t: The task in question308* @ns: target user namespace309* @cap: The capability to be tested for310*311* Return true if the specified task has the given superior capability312* currently in effect to the specified user namespace, false if not.313*314* Note that this does not set PF_SUPERPRIV on the task.315*/316bool has_ns_capability(struct task_struct *t,317struct user_namespace *ns, int cap)318{319int ret = security_real_capable(t, ns, cap);320321return (ret == 0);322}323324/**325* has_capability_noaudit - Does a task have a capability (unaudited)326* @t: The task in question327* @cap: The capability to be tested for328*329* Return true if the specified task has the given superior capability330* currently in effect to init_user_ns, false if not. Don't write an331* audit message for the check.332*333* Note that this does not set PF_SUPERPRIV on the task.334*/335bool has_capability_noaudit(struct task_struct *t, int cap)336{337int ret = security_real_capable_noaudit(t, &init_user_ns, cap);338339return (ret == 0);340}341342/**343* capable - Determine if the current task has a superior capability in effect344* @cap: The capability to be tested for345*346* Return true if the current task has the given superior capability currently347* available for use, false if not.348*349* This sets PF_SUPERPRIV on the task if the capability is available on the350* assumption that it's about to be used.351*/352bool capable(int cap)353{354return ns_capable(&init_user_ns, cap);355}356EXPORT_SYMBOL(capable);357358/**359* ns_capable - Determine if the current task has a superior capability in effect360* @ns: The usernamespace we want the capability in361* @cap: The capability to be tested for362*363* Return true if the current task has the given superior capability currently364* available for use, false if not.365*366* This sets PF_SUPERPRIV on the task if the capability is available on the367* assumption that it's about to be used.368*/369bool ns_capable(struct user_namespace *ns, int cap)370{371if (unlikely(!cap_valid(cap))) {372printk(KERN_CRIT "capable() called with invalid cap=%u\n", cap);373BUG();374}375376if (security_capable(ns, current_cred(), cap) == 0) {377current->flags |= PF_SUPERPRIV;378return true;379}380return false;381}382EXPORT_SYMBOL(ns_capable);383384/**385* task_ns_capable - Determine whether current task has a superior386* capability targeted at a specific task's user namespace.387* @t: The task whose user namespace is targeted.388* @cap: The capability in question.389*390* Return true if it does, false otherwise.391*/392bool task_ns_capable(struct task_struct *t, int cap)393{394return ns_capable(task_cred_xxx(t, user)->user_ns, cap);395}396EXPORT_SYMBOL(task_ns_capable);397398/**399* nsown_capable - Check superior capability to one's own user_ns400* @cap: The capability in question401*402* Return true if the current task has the given superior capability403* targeted at its own user namespace.404*/405bool nsown_capable(int cap)406{407return ns_capable(current_user_ns(), cap);408}409410411