2 * linux/kernel/capability.c
4 * Copyright (C) 1997 Andrew Main <zefram@fysh.org>
6 * Integrated into 2.1.97+, Andrew G. Morgan <morgan@kernel.org>
7 * 30 May 2002: Cleanup, Robert M. Love <rml@tech9.net>
10 #include <linux/audit.h>
11 #include <linux/capability.h>
13 #include <linux/module.h>
14 #include <linux/security.h>
15 #include <linux/syscalls.h>
16 #include <linux/pid_namespace.h>
17 #include <linux/user_namespace.h>
18 #include <asm/uaccess.h>
21 * Leveraged for setting/resetting capabilities
24 const kernel_cap_t __cap_empty_set = CAP_EMPTY_SET;
25 const kernel_cap_t __cap_full_set = CAP_FULL_SET;
27 EXPORT_SYMBOL(__cap_empty_set);
28 EXPORT_SYMBOL(__cap_full_set);
30 int file_caps_enabled = 1;
32 static int __init file_caps_disable(char *str)
34 file_caps_enabled = 0;
37 __setup("no_file_caps", file_caps_disable);
40 * More recent versions of libcap are available from:
42 * http://www.kernel.org/pub/linux/libs/security/linux-privs/
45 static void warn_legacy_capability_use(void)
49 char name[sizeof(current->comm)];
51 printk(KERN_INFO "warning: `%s' uses 32-bit capabilities"
52 " (legacy support in use)\n",
53 get_task_comm(name, current));
59 * Version 2 capabilities worked fine, but the linux/capability.h file
60 * that accompanied their introduction encouraged their use without
61 * the necessary user-space source code changes. As such, we have
62 * created a version 3 with equivalent functionality to version 2, but
63 * with a header change to protect legacy source code from using
64 * version 2 when it wanted to use version 1. If your system has code
65 * that trips the following warning, it is using version 2 specific
66 * capabilities and may be doing so insecurely.
68 * The remedy is to either upgrade your version of libcap (to 2.10+,
69 * if the application is linked against it), or recompile your
70 * application with modern kernel headers and this warning will go
74 static void warn_deprecated_v2(void)
79 char name[sizeof(current->comm)];
81 printk(KERN_INFO "warning: `%s' uses deprecated v2"
82 " capabilities in a way that may be insecure.\n",
83 get_task_comm(name, current));
89 * Version check. Return the number of u32s in each capability flag
90 * array, or a negative value on error.
92 static int cap_validate_magic(cap_user_header_t header, unsigned *tocopy)
96 if (get_user(version, &header->version))
100 case _LINUX_CAPABILITY_VERSION_1:
101 warn_legacy_capability_use();
102 *tocopy = _LINUX_CAPABILITY_U32S_1;
104 case _LINUX_CAPABILITY_VERSION_2:
105 warn_deprecated_v2();
107 * fall through - v3 is otherwise equivalent to v2.
109 case _LINUX_CAPABILITY_VERSION_3:
110 *tocopy = _LINUX_CAPABILITY_U32S_3;
113 if (put_user((u32)_KERNEL_CAPABILITY_VERSION, &header->version))
122 * The only thing that can change the capabilities of the current
123 * process is the current process. As such, we can't be in this code
124 * at the same time as we are in the process of setting capabilities
125 * in this process. The net result is that we can limit our use of
126 * locks to when we are reading the caps of another process.
128 static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp,
129 kernel_cap_t *pIp, kernel_cap_t *pPp)
133 if (pid && (pid != task_pid_vnr(current))) {
134 struct task_struct *target;
138 target = find_task_by_vpid(pid);
142 ret = security_capget(target, pEp, pIp, pPp);
146 ret = security_capget(current, pEp, pIp, pPp);
152 * sys_capget - get the capabilities of a given process.
153 * @header: pointer to struct that contains capability version and
155 * @dataptr: pointer to struct that contains the effective, permitted,
156 * and inheritable capabilities that are returned
158 * Returns 0 on success and < 0 on error.
160 SYSCALL_DEFINE2(capget, cap_user_header_t, header, cap_user_data_t, dataptr)
165 kernel_cap_t pE, pI, pP;
167 ret = cap_validate_magic(header, &tocopy);
168 if ((dataptr == NULL) || (ret != 0))
169 return ((dataptr == NULL) && (ret == -EINVAL)) ? 0 : ret;
171 if (get_user(pid, &header->pid))
177 ret = cap_get_target_pid(pid, &pE, &pI, &pP);
179 struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
182 for (i = 0; i < tocopy; i++) {
183 kdata[i].effective = pE.cap[i];
184 kdata[i].permitted = pP.cap[i];
185 kdata[i].inheritable = pI.cap[i];
189 * Note, in the case, tocopy < _KERNEL_CAPABILITY_U32S,
190 * we silently drop the upper capabilities here. This
191 * has the effect of making older libcap
192 * implementations implicitly drop upper capability
193 * bits when they perform a: capget/modify/capset
196 * This behavior is considered fail-safe
197 * behavior. Upgrading the application to a newer
198 * version of libcap will enable access to the newer
201 * An alternative would be to return an error here
202 * (-ERANGE), but that causes legacy applications to
203 * unexpectidly fail; the capget/modify/capset aborts
204 * before modification is attempted and the application
207 if (copy_to_user(dataptr, kdata, tocopy
208 * sizeof(struct __user_cap_data_struct))) {
217 * sys_capset - set capabilities for a process or (*) a group of processes
218 * @header: pointer to struct that contains capability version and
220 * @data: pointer to struct that contains the effective, permitted,
221 * and inheritable capabilities
223 * Set capabilities for the current process only. The ability to any other
224 * process(es) has been deprecated and removed.
226 * The restrictions on setting capabilities are specified as:
228 * I: any raised capabilities must be a subset of the old permitted
229 * P: any raised capabilities must be a subset of the old permitted
230 * E: must be set to a subset of new permitted
232 * Returns 0 on success and < 0 on error.
234 SYSCALL_DEFINE2(capset, cap_user_header_t, header, const cap_user_data_t, data)
236 struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
237 unsigned i, tocopy, copybytes;
238 kernel_cap_t inheritable, permitted, effective;
243 ret = cap_validate_magic(header, &tocopy);
247 if (get_user(pid, &header->pid))
250 /* may only affect current now */
251 if (pid != 0 && pid != task_pid_vnr(current))
254 copybytes = tocopy * sizeof(struct __user_cap_data_struct);
255 if (copybytes > sizeof(kdata))
258 if (copy_from_user(&kdata, data, copybytes))
261 for (i = 0; i < tocopy; i++) {
262 effective.cap[i] = kdata[i].effective;
263 permitted.cap[i] = kdata[i].permitted;
264 inheritable.cap[i] = kdata[i].inheritable;
266 while (i < _KERNEL_CAPABILITY_U32S) {
267 effective.cap[i] = 0;
268 permitted.cap[i] = 0;
269 inheritable.cap[i] = 0;
273 new = prepare_creds();
277 ret = security_capset(new, current_cred(),
278 &effective, &inheritable, &permitted);
282 audit_log_capset(pid, new, current_cred());
284 return commit_creds(new);
292 * has_capability - Does a task have a capability in init_user_ns
293 * @t: The task in question
294 * @cap: The capability to be tested for
296 * Return true if the specified task has the given superior capability
297 * currently in effect to the initial user namespace, false if not.
299 * Note that this does not set PF_SUPERPRIV on the task.
301 bool has_capability(struct task_struct *t, int cap)
303 int ret = security_real_capable(t, &init_user_ns, cap);
309 * has_capability - Does a task have a capability in a specific user ns
310 * @t: The task in question
311 * @ns: target user namespace
312 * @cap: The capability to be tested for
314 * Return true if the specified task has the given superior capability
315 * currently in effect to the specified user namespace, false if not.
317 * Note that this does not set PF_SUPERPRIV on the task.
319 bool has_ns_capability(struct task_struct *t,
320 struct user_namespace *ns, int cap)
322 int ret = security_real_capable(t, ns, cap);
328 * has_capability_noaudit - Does a task have a capability (unaudited)
329 * @t: The task in question
330 * @cap: The capability to be tested for
332 * Return true if the specified task has the given superior capability
333 * currently in effect to init_user_ns, false if not. Don't write an
334 * audit message for the check.
336 * Note that this does not set PF_SUPERPRIV on the task.
338 bool has_capability_noaudit(struct task_struct *t, int cap)
340 int ret = security_real_capable_noaudit(t, &init_user_ns, cap);
346 * capable - Determine if the current task has a superior capability in effect
347 * @cap: The capability to be tested for
349 * Return true if the current task has the given superior capability currently
350 * available for use, false if not.
352 * This sets PF_SUPERPRIV on the task if the capability is available on the
353 * assumption that it's about to be used.
355 bool capable(int cap)
357 return ns_capable(&init_user_ns, cap);
359 EXPORT_SYMBOL(capable);
362 * ns_capable - Determine if the current task has a superior capability in effect
363 * @ns: The usernamespace we want the capability in
364 * @cap: The capability to be tested for
366 * Return true if the current task has the given superior capability currently
367 * available for use, false if not.
369 * This sets PF_SUPERPRIV on the task if the capability is available on the
370 * assumption that it's about to be used.
372 bool ns_capable(struct user_namespace *ns, int cap)
374 if (unlikely(!cap_valid(cap))) {
375 printk(KERN_CRIT "capable() called with invalid cap=%u\n", cap);
379 if (security_capable(ns, current_cred(), cap) == 0) {
380 current->flags |= PF_SUPERPRIV;
385 EXPORT_SYMBOL(ns_capable);
388 * task_ns_capable - Determine whether current task has a superior
389 * capability targeted at a specific task's user namespace.
390 * @t: The task whose user namespace is targeted.
391 * @cap: The capability in question.
393 * Return true if it does, false otherwise.
395 bool task_ns_capable(struct task_struct *t, int cap)
397 return ns_capable(task_cred_xxx(t, user)->user_ns, cap);
399 EXPORT_SYMBOL(task_ns_capable);