1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * linux/kernel/capability.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Copyright (C) 1997 Andrew Main <[email protected]>
61da177e4SLinus Torvalds *
772c2d582SAndrew Morgan * Integrated into 2.1.97+, Andrew G. Morgan <[email protected]>
81da177e4SLinus Torvalds * 30 May 2002: Cleanup, Robert M. Love <[email protected]>
91da177e4SLinus Torvalds */
101da177e4SLinus Torvalds
11f5645d35SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12f5645d35SJoe Perches
13e68b75a0SEric Paris #include <linux/audit.h>
14c59ede7bSRandy.Dunlap #include <linux/capability.h>
151da177e4SLinus Torvalds #include <linux/mm.h>
169984de1aSPaul Gortmaker #include <linux/export.h>
171da177e4SLinus Torvalds #include <linux/security.h>
181da177e4SLinus Torvalds #include <linux/syscalls.h>
19b460cbc5SSerge E. Hallyn #include <linux/pid_namespace.h>
203486740aSSerge E. Hallyn #include <linux/user_namespace.h>
217c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
221da177e4SLinus Torvalds
231f29fae2SSerge E. Hallyn int file_caps_enabled = 1;
241f29fae2SSerge E. Hallyn
file_caps_disable(char * str)251f29fae2SSerge E. Hallyn static int __init file_caps_disable(char *str)
261f29fae2SSerge E. Hallyn {
271f29fae2SSerge E. Hallyn file_caps_enabled = 0;
281f29fae2SSerge E. Hallyn return 1;
291f29fae2SSerge E. Hallyn }
301f29fae2SSerge E. Hallyn __setup("no_file_caps", file_caps_disable);
311f29fae2SSerge E. Hallyn
322813893fSIulia Manda #ifdef CONFIG_MULTIUSER
33e338d263SAndrew Morgan /*
34e338d263SAndrew Morgan * More recent versions of libcap are available from:
35e338d263SAndrew Morgan *
36e338d263SAndrew Morgan * http://www.kernel.org/pub/linux/libs/security/linux-privs/
37e338d263SAndrew Morgan */
38e338d263SAndrew Morgan
warn_legacy_capability_use(void)39e338d263SAndrew Morgan static void warn_legacy_capability_use(void)
40e338d263SAndrew Morgan {
41f5645d35SJoe Perches pr_info_once("warning: `%s' uses 32-bit capabilities (legacy support in use)\n",
42*b6dcdb06SYafang Shao current->comm);
43e338d263SAndrew Morgan }
44e338d263SAndrew Morgan
45e338d263SAndrew Morgan /*
46ca05a99aSAndrew G. Morgan * Version 2 capabilities worked fine, but the linux/capability.h file
47ca05a99aSAndrew G. Morgan * that accompanied their introduction encouraged their use without
48ca05a99aSAndrew G. Morgan * the necessary user-space source code changes. As such, we have
49ca05a99aSAndrew G. Morgan * created a version 3 with equivalent functionality to version 2, but
50ca05a99aSAndrew G. Morgan * with a header change to protect legacy source code from using
51ca05a99aSAndrew G. Morgan * version 2 when it wanted to use version 1. If your system has code
52ca05a99aSAndrew G. Morgan * that trips the following warning, it is using version 2 specific
53ca05a99aSAndrew G. Morgan * capabilities and may be doing so insecurely.
54ca05a99aSAndrew G. Morgan *
55ca05a99aSAndrew G. Morgan * The remedy is to either upgrade your version of libcap (to 2.10+,
56ca05a99aSAndrew G. Morgan * if the application is linked against it), or recompile your
57ca05a99aSAndrew G. Morgan * application with modern kernel headers and this warning will go
58ca05a99aSAndrew G. Morgan * away.
59ca05a99aSAndrew G. Morgan */
60ca05a99aSAndrew G. Morgan
warn_deprecated_v2(void)61ca05a99aSAndrew G. Morgan static void warn_deprecated_v2(void)
62ca05a99aSAndrew G. Morgan {
63f5645d35SJoe Perches pr_info_once("warning: `%s' uses deprecated v2 capabilities in a way that may be insecure\n",
64*b6dcdb06SYafang Shao current->comm);
65ca05a99aSAndrew G. Morgan }
66ca05a99aSAndrew G. Morgan
67ca05a99aSAndrew G. Morgan /*
68ca05a99aSAndrew G. Morgan * Version check. Return the number of u32s in each capability flag
69ca05a99aSAndrew G. Morgan * array, or a negative value on error.
70ca05a99aSAndrew G. Morgan */
cap_validate_magic(cap_user_header_t header,unsigned * tocopy)71ca05a99aSAndrew G. Morgan static int cap_validate_magic(cap_user_header_t header, unsigned *tocopy)
72ca05a99aSAndrew G. Morgan {
73ca05a99aSAndrew G. Morgan __u32 version;
74ca05a99aSAndrew G. Morgan
75ca05a99aSAndrew G. Morgan if (get_user(version, &header->version))
76ca05a99aSAndrew G. Morgan return -EFAULT;
77ca05a99aSAndrew G. Morgan
78ca05a99aSAndrew G. Morgan switch (version) {
79ca05a99aSAndrew G. Morgan case _LINUX_CAPABILITY_VERSION_1:
80ca05a99aSAndrew G. Morgan warn_legacy_capability_use();
81ca05a99aSAndrew G. Morgan *tocopy = _LINUX_CAPABILITY_U32S_1;
82ca05a99aSAndrew G. Morgan break;
83ca05a99aSAndrew G. Morgan case _LINUX_CAPABILITY_VERSION_2:
84ca05a99aSAndrew G. Morgan warn_deprecated_v2();
85df561f66SGustavo A. R. Silva fallthrough; /* v3 is otherwise equivalent to v2 */
86ca05a99aSAndrew G. Morgan case _LINUX_CAPABILITY_VERSION_3:
87ca05a99aSAndrew G. Morgan *tocopy = _LINUX_CAPABILITY_U32S_3;
88ca05a99aSAndrew G. Morgan break;
89ca05a99aSAndrew G. Morgan default:
90ca05a99aSAndrew G. Morgan if (put_user((u32)_KERNEL_CAPABILITY_VERSION, &header->version))
91ca05a99aSAndrew G. Morgan return -EFAULT;
92ca05a99aSAndrew G. Morgan return -EINVAL;
93ca05a99aSAndrew G. Morgan }
94ca05a99aSAndrew G. Morgan
95ca05a99aSAndrew G. Morgan return 0;
96ca05a99aSAndrew G. Morgan }
97ca05a99aSAndrew G. Morgan
98ab763c71SAndrew G. Morgan /*
99d84f4f99SDavid Howells * The only thing that can change the capabilities of the current
100d84f4f99SDavid Howells * process is the current process. As such, we can't be in this code
101d84f4f99SDavid Howells * at the same time as we are in the process of setting capabilities
102d84f4f99SDavid Howells * in this process. The net result is that we can limit our use of
103d84f4f99SDavid Howells * locks to when we are reading the caps of another process.
104ab763c71SAndrew G. Morgan */
cap_get_target_pid(pid_t pid,kernel_cap_t * pEp,kernel_cap_t * pIp,kernel_cap_t * pPp)105ab763c71SAndrew G. Morgan static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp,
106ab763c71SAndrew G. Morgan kernel_cap_t *pIp, kernel_cap_t *pPp)
107ab763c71SAndrew G. Morgan {
108ab763c71SAndrew G. Morgan int ret;
109ab763c71SAndrew G. Morgan
110ab763c71SAndrew G. Morgan if (pid && (pid != task_pid_vnr(current))) {
1116672efbbSKhadija Kamran const struct task_struct *target;
112ab763c71SAndrew G. Morgan
11386fc80f1SThomas Gleixner rcu_read_lock();
114ab763c71SAndrew G. Morgan
115ab763c71SAndrew G. Morgan target = find_task_by_vpid(pid);
116ab763c71SAndrew G. Morgan if (!target)
117ab763c71SAndrew G. Morgan ret = -ESRCH;
118ab763c71SAndrew G. Morgan else
119ab763c71SAndrew G. Morgan ret = security_capget(target, pEp, pIp, pPp);
120ab763c71SAndrew G. Morgan
12186fc80f1SThomas Gleixner rcu_read_unlock();
122ab763c71SAndrew G. Morgan } else
123ab763c71SAndrew G. Morgan ret = security_capget(current, pEp, pIp, pPp);
124ab763c71SAndrew G. Morgan
125ab763c71SAndrew G. Morgan return ret;
126ab763c71SAndrew G. Morgan }
127ab763c71SAndrew G. Morgan
128207a7ba8SRandy Dunlap /**
1291da177e4SLinus Torvalds * sys_capget - get the capabilities of a given process.
130207a7ba8SRandy Dunlap * @header: pointer to struct that contains capability version and
131207a7ba8SRandy Dunlap * target pid data
132207a7ba8SRandy Dunlap * @dataptr: pointer to struct that contains the effective, permitted,
133207a7ba8SRandy Dunlap * and inheritable capabilities that are returned
134207a7ba8SRandy Dunlap *
135207a7ba8SRandy Dunlap * Returns 0 on success and < 0 on error.
1361da177e4SLinus Torvalds */
SYSCALL_DEFINE2(capget,cap_user_header_t,header,cap_user_data_t,dataptr)137b290ebe2SHeiko Carstens SYSCALL_DEFINE2(capget, cap_user_header_t, header, cap_user_data_t, dataptr)
1381da177e4SLinus Torvalds {
1391da177e4SLinus Torvalds int ret = 0;
1401da177e4SLinus Torvalds pid_t pid;
141e338d263SAndrew Morgan unsigned tocopy;
142e338d263SAndrew Morgan kernel_cap_t pE, pI, pP;
143f122a08bSLinus Torvalds struct __user_cap_data_struct kdata[2];
1441da177e4SLinus Torvalds
145ca05a99aSAndrew G. Morgan ret = cap_validate_magic(header, &tocopy);
146c4a5af54SAndrew G. Morgan if ((dataptr == NULL) || (ret != 0))
147c4a5af54SAndrew G. Morgan return ((dataptr == NULL) && (ret == -EINVAL)) ? 0 : ret;
1481da177e4SLinus Torvalds
1491da177e4SLinus Torvalds if (get_user(pid, &header->pid))
1501da177e4SLinus Torvalds return -EFAULT;
1511da177e4SLinus Torvalds
1521da177e4SLinus Torvalds if (pid < 0)
1531da177e4SLinus Torvalds return -EINVAL;
1541da177e4SLinus Torvalds
155ab763c71SAndrew G. Morgan ret = cap_get_target_pid(pid, &pE, &pI, &pP);
156f122a08bSLinus Torvalds if (ret)
157f122a08bSLinus Torvalds return ret;
158e338d263SAndrew Morgan
159f122a08bSLinus Torvalds /*
160f122a08bSLinus Torvalds * Annoying legacy format with 64-bit capabilities exposed
161f122a08bSLinus Torvalds * as two sets of 32-bit fields, so we need to split the
162f122a08bSLinus Torvalds * capability values up.
163f122a08bSLinus Torvalds */
164f122a08bSLinus Torvalds kdata[0].effective = pE.val; kdata[1].effective = pE.val >> 32;
165f122a08bSLinus Torvalds kdata[0].permitted = pP.val; kdata[1].permitted = pP.val >> 32;
166f122a08bSLinus Torvalds kdata[0].inheritable = pI.val; kdata[1].inheritable = pI.val >> 32;
167e338d263SAndrew Morgan
168e338d263SAndrew Morgan /*
169ca05a99aSAndrew G. Morgan * Note, in the case, tocopy < _KERNEL_CAPABILITY_U32S,
170e338d263SAndrew Morgan * we silently drop the upper capabilities here. This
171e338d263SAndrew Morgan * has the effect of making older libcap
172e338d263SAndrew Morgan * implementations implicitly drop upper capability
173e338d263SAndrew Morgan * bits when they perform a: capget/modify/capset
174e338d263SAndrew Morgan * sequence.
175e338d263SAndrew Morgan *
176e338d263SAndrew Morgan * This behavior is considered fail-safe
177e338d263SAndrew Morgan * behavior. Upgrading the application to a newer
178e338d263SAndrew Morgan * version of libcap will enable access to the newer
179e338d263SAndrew Morgan * capabilities.
180e338d263SAndrew Morgan *
181e338d263SAndrew Morgan * An alternative would be to return an error here
182e338d263SAndrew Morgan * (-ERANGE), but that causes legacy applications to
183a6c8c690SFabian Frederick * unexpectedly fail; the capget/modify/capset aborts
184e338d263SAndrew Morgan * before modification is attempted and the application
185e338d263SAndrew Morgan * fails.
186e338d263SAndrew Morgan */
187f122a08bSLinus Torvalds if (copy_to_user(dataptr, kdata, tocopy * sizeof(kdata[0])))
1881da177e4SLinus Torvalds return -EFAULT;
189f122a08bSLinus Torvalds
190f122a08bSLinus Torvalds return 0;
191e338d263SAndrew Morgan }
1921da177e4SLinus Torvalds
mk_kernel_cap(u32 low,u32 high)193f122a08bSLinus Torvalds static kernel_cap_t mk_kernel_cap(u32 low, u32 high)
194f122a08bSLinus Torvalds {
195f122a08bSLinus Torvalds return (kernel_cap_t) { (low | ((u64)high << 32)) & CAP_VALID_MASK };
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds
198207a7ba8SRandy Dunlap /**
199ab763c71SAndrew G. Morgan * sys_capset - set capabilities for a process or (*) a group of processes
200207a7ba8SRandy Dunlap * @header: pointer to struct that contains capability version and
201207a7ba8SRandy Dunlap * target pid data
202207a7ba8SRandy Dunlap * @data: pointer to struct that contains the effective, permitted,
203207a7ba8SRandy Dunlap * and inheritable capabilities
204207a7ba8SRandy Dunlap *
2051cdcbec1SDavid Howells * Set capabilities for the current process only. The ability to any other
2061cdcbec1SDavid Howells * process(es) has been deprecated and removed.
2071da177e4SLinus Torvalds *
2081da177e4SLinus Torvalds * The restrictions on setting capabilities are specified as:
2091da177e4SLinus Torvalds *
2101cdcbec1SDavid Howells * I: any raised capabilities must be a subset of the old permitted
2111cdcbec1SDavid Howells * P: any raised capabilities must be a subset of the old permitted
2121cdcbec1SDavid Howells * E: must be set to a subset of new permitted
213207a7ba8SRandy Dunlap *
214207a7ba8SRandy Dunlap * Returns 0 on success and < 0 on error.
2151da177e4SLinus Torvalds */
SYSCALL_DEFINE2(capset,cap_user_header_t,header,const cap_user_data_t,data)216b290ebe2SHeiko Carstens SYSCALL_DEFINE2(capset, cap_user_header_t, header, const cap_user_data_t, data)
2171da177e4SLinus Torvalds {
218f122a08bSLinus Torvalds struct __user_cap_data_struct kdata[2] = { { 0, }, };
219f122a08bSLinus Torvalds unsigned tocopy, copybytes;
2201da177e4SLinus Torvalds kernel_cap_t inheritable, permitted, effective;
221d84f4f99SDavid Howells struct cred *new;
2221da177e4SLinus Torvalds int ret;
2231da177e4SLinus Torvalds pid_t pid;
2241da177e4SLinus Torvalds
225ca05a99aSAndrew G. Morgan ret = cap_validate_magic(header, &tocopy);
226ca05a99aSAndrew G. Morgan if (ret != 0)
227ca05a99aSAndrew G. Morgan return ret;
2281da177e4SLinus Torvalds
2291da177e4SLinus Torvalds if (get_user(pid, &header->pid))
2301da177e4SLinus Torvalds return -EFAULT;
2311da177e4SLinus Torvalds
2321cdcbec1SDavid Howells /* may only affect current now */
2331cdcbec1SDavid Howells if (pid != 0 && pid != task_pid_vnr(current))
2341cdcbec1SDavid Howells return -EPERM;
2351cdcbec1SDavid Howells
236825332e4SArjan van de Ven copybytes = tocopy * sizeof(struct __user_cap_data_struct);
237825332e4SArjan van de Ven if (copybytes > sizeof(kdata))
238825332e4SArjan van de Ven return -EFAULT;
239825332e4SArjan van de Ven
240825332e4SArjan van de Ven if (copy_from_user(&kdata, data, copybytes))
2411da177e4SLinus Torvalds return -EFAULT;
242e338d263SAndrew Morgan
243f122a08bSLinus Torvalds effective = mk_kernel_cap(kdata[0].effective, kdata[1].effective);
244f122a08bSLinus Torvalds permitted = mk_kernel_cap(kdata[0].permitted, kdata[1].permitted);
245f122a08bSLinus Torvalds inheritable = mk_kernel_cap(kdata[0].inheritable, kdata[1].inheritable);
2467d8b6c63SEric Paris
247d84f4f99SDavid Howells new = prepare_creds();
248d84f4f99SDavid Howells if (!new)
249d84f4f99SDavid Howells return -ENOMEM;
250d84f4f99SDavid Howells
251d84f4f99SDavid Howells ret = security_capset(new, current_cred(),
252d84f4f99SDavid Howells &effective, &inheritable, &permitted);
253d84f4f99SDavid Howells if (ret < 0)
254d84f4f99SDavid Howells goto error;
255d84f4f99SDavid Howells
256ca24a23eSEric W. Biederman audit_log_capset(new, current_cred());
257e68b75a0SEric Paris
258d84f4f99SDavid Howells return commit_creds(new);
2591da177e4SLinus Torvalds
260d84f4f99SDavid Howells error:
261d84f4f99SDavid Howells abort_creds(new);
2621da177e4SLinus Torvalds return ret;
2631da177e4SLinus Torvalds }
26412b5989bSChris Wright
2655cd9c58fSDavid Howells /**
26625e75703SEric Paris * has_ns_capability - Does a task have a capability in a specific user ns
2673263245dSSerge E. Hallyn * @t: The task in question
2683263245dSSerge E. Hallyn * @ns: target user namespace
2693263245dSSerge E. Hallyn * @cap: The capability to be tested for
2703263245dSSerge E. Hallyn *
2713263245dSSerge E. Hallyn * Return true if the specified task has the given superior capability
2723263245dSSerge E. Hallyn * currently in effect to the specified user namespace, false if not.
2733263245dSSerge E. Hallyn *
2743263245dSSerge E. Hallyn * Note that this does not set PF_SUPERPRIV on the task.
2753263245dSSerge E. Hallyn */
has_ns_capability(struct task_struct * t,struct user_namespace * ns,int cap)2763263245dSSerge E. Hallyn bool has_ns_capability(struct task_struct *t,
2773263245dSSerge E. Hallyn struct user_namespace *ns, int cap)
2783263245dSSerge E. Hallyn {
2792920a840SEric Paris int ret;
2802920a840SEric Paris
2812920a840SEric Paris rcu_read_lock();
282c1a85a00SMicah Morton ret = security_capable(__task_cred(t), ns, cap, CAP_OPT_NONE);
2832920a840SEric Paris rcu_read_unlock();
2843263245dSSerge E. Hallyn
2853263245dSSerge E. Hallyn return (ret == 0);
2863263245dSSerge E. Hallyn }
2873263245dSSerge E. Hallyn
2883263245dSSerge E. Hallyn /**
2897b61d648SEric Paris * has_ns_capability_noaudit - Does a task have a capability (unaudited)
2907b61d648SEric Paris * in a specific user ns.
2917b61d648SEric Paris * @t: The task in question
2927b61d648SEric Paris * @ns: target user namespace
2937b61d648SEric Paris * @cap: The capability to be tested for
2947b61d648SEric Paris *
2957b61d648SEric Paris * Return true if the specified task has the given superior capability
2967b61d648SEric Paris * currently in effect to the specified user namespace, false if not.
2977b61d648SEric Paris * Do not write an audit message for the check.
2987b61d648SEric Paris *
2997b61d648SEric Paris * Note that this does not set PF_SUPERPRIV on the task.
3007b61d648SEric Paris */
has_ns_capability_noaudit(struct task_struct * t,struct user_namespace * ns,int cap)3017b61d648SEric Paris bool has_ns_capability_noaudit(struct task_struct *t,
3027b61d648SEric Paris struct user_namespace *ns, int cap)
3037b61d648SEric Paris {
3047b61d648SEric Paris int ret;
3057b61d648SEric Paris
3067b61d648SEric Paris rcu_read_lock();
307c1a85a00SMicah Morton ret = security_capable(__task_cred(t), ns, cap, CAP_OPT_NOAUDIT);
3087b61d648SEric Paris rcu_read_unlock();
3097b61d648SEric Paris
3107b61d648SEric Paris return (ret == 0);
3117b61d648SEric Paris }
3127b61d648SEric Paris
3137b61d648SEric Paris /**
3147b61d648SEric Paris * has_capability_noaudit - Does a task have a capability (unaudited) in the
3157b61d648SEric Paris * initial user ns
3163263245dSSerge E. Hallyn * @t: The task in question
3173263245dSSerge E. Hallyn * @cap: The capability to be tested for
3183263245dSSerge E. Hallyn *
3193263245dSSerge E. Hallyn * Return true if the specified task has the given superior capability
3203263245dSSerge E. Hallyn * currently in effect to init_user_ns, false if not. Don't write an
3213263245dSSerge E. Hallyn * audit message for the check.
3223263245dSSerge E. Hallyn *
3233263245dSSerge E. Hallyn * Note that this does not set PF_SUPERPRIV on the task.
3243263245dSSerge E. Hallyn */
has_capability_noaudit(struct task_struct * t,int cap)3253263245dSSerge E. Hallyn bool has_capability_noaudit(struct task_struct *t, int cap)
3263263245dSSerge E. Hallyn {
3277b61d648SEric Paris return has_ns_capability_noaudit(t, &init_user_ns, cap);
3283263245dSSerge E. Hallyn }
329eba0549bSDarrick J. Wong EXPORT_SYMBOL(has_capability_noaudit);
3303263245dSSerge E. Hallyn
ns_capable_common(struct user_namespace * ns,int cap,unsigned int opts)331c1a85a00SMicah Morton static bool ns_capable_common(struct user_namespace *ns,
332c1a85a00SMicah Morton int cap,
333c1a85a00SMicah Morton unsigned int opts)
33498f368e9STyler Hicks {
33598f368e9STyler Hicks int capable;
33698f368e9STyler Hicks
33798f368e9STyler Hicks if (unlikely(!cap_valid(cap))) {
33898f368e9STyler Hicks pr_crit("capable() called with invalid cap=%u\n", cap);
33998f368e9STyler Hicks BUG();
34098f368e9STyler Hicks }
34198f368e9STyler Hicks
342c1a85a00SMicah Morton capable = security_capable(current_cred(), ns, cap, opts);
34398f368e9STyler Hicks if (capable == 0) {
34498f368e9STyler Hicks current->flags |= PF_SUPERPRIV;
34598f368e9STyler Hicks return true;
34698f368e9STyler Hicks }
34798f368e9STyler Hicks return false;
34898f368e9STyler Hicks }
34998f368e9STyler Hicks
3503263245dSSerge E. Hallyn /**
3513486740aSSerge E. Hallyn * ns_capable - Determine if the current task has a superior capability in effect
3523486740aSSerge E. Hallyn * @ns: The usernamespace we want the capability in
3533486740aSSerge E. Hallyn * @cap: The capability to be tested for
3543486740aSSerge E. Hallyn *
3553486740aSSerge E. Hallyn * Return true if the current task has the given superior capability currently
3563486740aSSerge E. Hallyn * available for use, false if not.
3573486740aSSerge E. Hallyn *
3583486740aSSerge E. Hallyn * This sets PF_SUPERPRIV on the task if the capability is available on the
3593486740aSSerge E. Hallyn * assumption that it's about to be used.
3603486740aSSerge E. Hallyn */
ns_capable(struct user_namespace * ns,int cap)3613486740aSSerge E. Hallyn bool ns_capable(struct user_namespace *ns, int cap)
36212b5989bSChris Wright {
363c1a85a00SMicah Morton return ns_capable_common(ns, cap, CAP_OPT_NONE);
36412b5989bSChris Wright }
3653486740aSSerge E. Hallyn EXPORT_SYMBOL(ns_capable);
3663486740aSSerge E. Hallyn
36798f368e9STyler Hicks /**
36898f368e9STyler Hicks * ns_capable_noaudit - Determine if the current task has a superior capability
36998f368e9STyler Hicks * (unaudited) in effect
37098f368e9STyler Hicks * @ns: The usernamespace we want the capability in
37198f368e9STyler Hicks * @cap: The capability to be tested for
37298f368e9STyler Hicks *
37398f368e9STyler Hicks * Return true if the current task has the given superior capability currently
37498f368e9STyler Hicks * available for use, false if not.
37598f368e9STyler Hicks *
37698f368e9STyler Hicks * This sets PF_SUPERPRIV on the task if the capability is available on the
37798f368e9STyler Hicks * assumption that it's about to be used.
37898f368e9STyler Hicks */
ns_capable_noaudit(struct user_namespace * ns,int cap)37998f368e9STyler Hicks bool ns_capable_noaudit(struct user_namespace *ns, int cap)
38098f368e9STyler Hicks {
381c1a85a00SMicah Morton return ns_capable_common(ns, cap, CAP_OPT_NOAUDIT);
38298f368e9STyler Hicks }
38398f368e9STyler Hicks EXPORT_SYMBOL(ns_capable_noaudit);
3842813893fSIulia Manda
3852813893fSIulia Manda /**
38640852275SMicah Morton * ns_capable_setid - Determine if the current task has a superior capability
38740852275SMicah Morton * in effect, while signalling that this check is being done from within a
388111767c1SThomas Cedeno * setid or setgroups syscall.
38940852275SMicah Morton * @ns: The usernamespace we want the capability in
39040852275SMicah Morton * @cap: The capability to be tested for
39140852275SMicah Morton *
39240852275SMicah Morton * Return true if the current task has the given superior capability currently
39340852275SMicah Morton * available for use, false if not.
39440852275SMicah Morton *
39540852275SMicah Morton * This sets PF_SUPERPRIV on the task if the capability is available on the
39640852275SMicah Morton * assumption that it's about to be used.
39740852275SMicah Morton */
ns_capable_setid(struct user_namespace * ns,int cap)39840852275SMicah Morton bool ns_capable_setid(struct user_namespace *ns, int cap)
39940852275SMicah Morton {
40040852275SMicah Morton return ns_capable_common(ns, cap, CAP_OPT_INSETID);
40140852275SMicah Morton }
40240852275SMicah Morton EXPORT_SYMBOL(ns_capable_setid);
40340852275SMicah Morton
40440852275SMicah Morton /**
4052813893fSIulia Manda * capable - Determine if the current task has a superior capability in effect
4062813893fSIulia Manda * @cap: The capability to be tested for
4072813893fSIulia Manda *
4082813893fSIulia Manda * Return true if the current task has the given superior capability currently
4092813893fSIulia Manda * available for use, false if not.
4102813893fSIulia Manda *
4112813893fSIulia Manda * This sets PF_SUPERPRIV on the task if the capability is available on the
4122813893fSIulia Manda * assumption that it's about to be used.
4132813893fSIulia Manda */
capable(int cap)4142813893fSIulia Manda bool capable(int cap)
4152813893fSIulia Manda {
4162813893fSIulia Manda return ns_capable(&init_user_ns, cap);
4172813893fSIulia Manda }
4182813893fSIulia Manda EXPORT_SYMBOL(capable);
4192813893fSIulia Manda #endif /* CONFIG_MULTIUSER */
4202813893fSIulia Manda
4213486740aSSerge E. Hallyn /**
422935d8aabSLinus Torvalds * file_ns_capable - Determine if the file's opener had a capability in effect
423935d8aabSLinus Torvalds * @file: The file we want to check
424935d8aabSLinus Torvalds * @ns: The usernamespace we want the capability in
425935d8aabSLinus Torvalds * @cap: The capability to be tested for
426935d8aabSLinus Torvalds *
427935d8aabSLinus Torvalds * Return true if task that opened the file had a capability in effect
428935d8aabSLinus Torvalds * when the file was opened.
429935d8aabSLinus Torvalds *
430935d8aabSLinus Torvalds * This does not set PF_SUPERPRIV because the caller may not
431935d8aabSLinus Torvalds * actually be privileged.
432935d8aabSLinus Torvalds */
file_ns_capable(const struct file * file,struct user_namespace * ns,int cap)433a6c8c690SFabian Frederick bool file_ns_capable(const struct file *file, struct user_namespace *ns,
434a6c8c690SFabian Frederick int cap)
435935d8aabSLinus Torvalds {
436c1a85a00SMicah Morton
437935d8aabSLinus Torvalds if (WARN_ON_ONCE(!cap_valid(cap)))
438935d8aabSLinus Torvalds return false;
439935d8aabSLinus Torvalds
440c1a85a00SMicah Morton if (security_capable(file->f_cred, ns, cap, CAP_OPT_NONE) == 0)
441935d8aabSLinus Torvalds return true;
442935d8aabSLinus Torvalds
443935d8aabSLinus Torvalds return false;
444935d8aabSLinus Torvalds }
445935d8aabSLinus Torvalds EXPORT_SYMBOL(file_ns_capable);
446935d8aabSLinus Torvalds
447935d8aabSLinus Torvalds /**
448f84df2a6SEric W. Biederman * privileged_wrt_inode_uidgid - Do capabilities in the namespace work over the inode?
449f84df2a6SEric W. Biederman * @ns: The user namespace in question
450a495108eSGaosheng Cui * @idmap: idmap of the mount @inode was found from
451f84df2a6SEric W. Biederman * @inode: The inode in question
452f84df2a6SEric W. Biederman *
453f84df2a6SEric W. Biederman * Return true if the inode uid and gid are within the namespace.
454f84df2a6SEric W. Biederman */
privileged_wrt_inode_uidgid(struct user_namespace * ns,struct mnt_idmap * idmap,const struct inode * inode)4550558c1bfSChristian Brauner bool privileged_wrt_inode_uidgid(struct user_namespace *ns,
4569452e93eSChristian Brauner struct mnt_idmap *idmap,
4570558c1bfSChristian Brauner const struct inode *inode)
458f84df2a6SEric W. Biederman {
459e67fe633SChristian Brauner return vfsuid_has_mapping(ns, i_uid_into_vfsuid(idmap, inode)) &&
460e67fe633SChristian Brauner vfsgid_has_mapping(ns, i_gid_into_vfsgid(idmap, inode));
461f84df2a6SEric W. Biederman }
462f84df2a6SEric W. Biederman
463f84df2a6SEric W. Biederman /**
46423adbe12SAndy Lutomirski * capable_wrt_inode_uidgid - Check nsown_capable and uid and gid mapped
465a495108eSGaosheng Cui * @idmap: idmap of the mount @inode was found from
4661a48e2acSEric W. Biederman * @inode: The inode in question
4671a48e2acSEric W. Biederman * @cap: The capability in question
4681a48e2acSEric W. Biederman *
46923adbe12SAndy Lutomirski * Return true if the current task has the given capability targeted at
47023adbe12SAndy Lutomirski * its own user namespace and that the given inode's uid and gid are
47123adbe12SAndy Lutomirski * mapped into the current user namespace.
4721a48e2acSEric W. Biederman */
capable_wrt_inode_uidgid(struct mnt_idmap * idmap,const struct inode * inode,int cap)4739452e93eSChristian Brauner bool capable_wrt_inode_uidgid(struct mnt_idmap *idmap,
4740558c1bfSChristian Brauner const struct inode *inode, int cap)
4751a48e2acSEric W. Biederman {
4761a48e2acSEric W. Biederman struct user_namespace *ns = current_user_ns();
4771a48e2acSEric W. Biederman
4780558c1bfSChristian Brauner return ns_capable(ns, cap) &&
4799452e93eSChristian Brauner privileged_wrt_inode_uidgid(ns, idmap, inode);
4801a48e2acSEric W. Biederman }
48123adbe12SAndy Lutomirski EXPORT_SYMBOL(capable_wrt_inode_uidgid);
48264b875f7SEric W. Biederman
48364b875f7SEric W. Biederman /**
48464b875f7SEric W. Biederman * ptracer_capable - Determine if the ptracer holds CAP_SYS_PTRACE in the namespace
48564b875f7SEric W. Biederman * @tsk: The task that may be ptraced
48664b875f7SEric W. Biederman * @ns: The user namespace to search for CAP_SYS_PTRACE in
48764b875f7SEric W. Biederman *
48864b875f7SEric W. Biederman * Return true if the task that is ptracing the current task had CAP_SYS_PTRACE
48964b875f7SEric W. Biederman * in the specified user namespace.
49064b875f7SEric W. Biederman */
ptracer_capable(struct task_struct * tsk,struct user_namespace * ns)49164b875f7SEric W. Biederman bool ptracer_capable(struct task_struct *tsk, struct user_namespace *ns)
49264b875f7SEric W. Biederman {
49364b875f7SEric W. Biederman int ret = 0; /* An absent tracer adds no restrictions */
49464b875f7SEric W. Biederman const struct cred *cred;
495c1a85a00SMicah Morton
49664b875f7SEric W. Biederman rcu_read_lock();
49764b875f7SEric W. Biederman cred = rcu_dereference(tsk->ptracer_cred);
49864b875f7SEric W. Biederman if (cred)
499c1a85a00SMicah Morton ret = security_capable(cred, ns, CAP_SYS_PTRACE,
500c1a85a00SMicah Morton CAP_OPT_NOAUDIT);
50164b875f7SEric W. Biederman rcu_read_unlock();
50264b875f7SEric W. Biederman return (ret == 0);
50364b875f7SEric W. Biederman }
504