blob: 2a374d512ead46bb3caae521bf358a87531446bd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/capability.c
3 *
4 * Copyright (C) 1997 Andrew Main <zefram@fysh.org>
5 *
Andrew Morgan72c2d582007-10-18 03:05:59 -07006 * Integrated into 2.1.97+, Andrew G. Morgan <morgan@kernel.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 30 May 2002: Cleanup, Robert M. Love <rml@tech9.net>
Daniel Walker314f70f2007-10-18 03:06:08 -07008 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Eric Parise68b75a02008-11-11 21:48:22 +110010#include <linux/audit.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080011#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/mm.h>
13#include <linux/module.h>
14#include <linux/security.h>
15#include <linux/syscalls.h>
Serge E. Hallynb460cbc2007-10-18 23:39:52 -070016#include <linux/pid_namespace.h>
Serge E. Hallyn3486740a2011-03-23 16:43:17 -070017#include <linux/user_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20/*
Andrew Morgane338d262008-02-04 22:29:42 -080021 * Leveraged for setting/resetting capabilities
22 */
23
24const kernel_cap_t __cap_empty_set = CAP_EMPTY_SET;
25const kernel_cap_t __cap_full_set = CAP_FULL_SET;
Andrew Morgane338d262008-02-04 22:29:42 -080026
27EXPORT_SYMBOL(__cap_empty_set);
28EXPORT_SYMBOL(__cap_full_set);
Andrew Morgane338d262008-02-04 22:29:42 -080029
Serge E. Hallyn1f29fae2008-11-05 16:08:52 -060030int file_caps_enabled = 1;
31
32static int __init file_caps_disable(char *str)
33{
34 file_caps_enabled = 0;
35 return 1;
36}
37__setup("no_file_caps", file_caps_disable);
Serge E. Hallyn1f29fae2008-11-05 16:08:52 -060038
Andrew Morgane338d262008-02-04 22:29:42 -080039/*
40 * More recent versions of libcap are available from:
41 *
42 * http://www.kernel.org/pub/linux/libs/security/linux-privs/
43 */
44
45static void warn_legacy_capability_use(void)
46{
47 static int warned;
48 if (!warned) {
49 char name[sizeof(current->comm)];
50
51 printk(KERN_INFO "warning: `%s' uses 32-bit capabilities"
52 " (legacy support in use)\n",
53 get_task_comm(name, current));
54 warned = 1;
55 }
56}
57
58/*
Andrew G. Morganca05a992008-05-27 22:05:17 -070059 * 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.
67 *
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
71 * away.
72 */
73
74static void warn_deprecated_v2(void)
75{
76 static int warned;
77
78 if (!warned) {
79 char name[sizeof(current->comm)];
80
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));
84 warned = 1;
85 }
86}
87
88/*
89 * Version check. Return the number of u32s in each capability flag
90 * array, or a negative value on error.
91 */
92static int cap_validate_magic(cap_user_header_t header, unsigned *tocopy)
93{
94 __u32 version;
95
96 if (get_user(version, &header->version))
97 return -EFAULT;
98
99 switch (version) {
100 case _LINUX_CAPABILITY_VERSION_1:
101 warn_legacy_capability_use();
102 *tocopy = _LINUX_CAPABILITY_U32S_1;
103 break;
104 case _LINUX_CAPABILITY_VERSION_2:
105 warn_deprecated_v2();
106 /*
107 * fall through - v3 is otherwise equivalent to v2.
108 */
109 case _LINUX_CAPABILITY_VERSION_3:
110 *tocopy = _LINUX_CAPABILITY_U32S_3;
111 break;
112 default:
113 if (put_user((u32)_KERNEL_CAPABILITY_VERSION, &header->version))
114 return -EFAULT;
115 return -EINVAL;
116 }
117
118 return 0;
119}
120
Andrew G. Morganab763c72008-07-23 21:28:25 -0700121/*
David Howellsd84f4f92008-11-14 10:39:23 +1100122 * 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.
Andrew G. Morganab763c72008-07-23 21:28:25 -0700127 */
128static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp,
129 kernel_cap_t *pIp, kernel_cap_t *pPp)
130{
131 int ret;
132
133 if (pid && (pid != task_pid_vnr(current))) {
134 struct task_struct *target;
135
Thomas Gleixner86fc80f2009-12-09 17:13:31 +0100136 rcu_read_lock();
Andrew G. Morganab763c72008-07-23 21:28:25 -0700137
138 target = find_task_by_vpid(pid);
139 if (!target)
140 ret = -ESRCH;
141 else
142 ret = security_capget(target, pEp, pIp, pPp);
143
Thomas Gleixner86fc80f2009-12-09 17:13:31 +0100144 rcu_read_unlock();
Andrew G. Morganab763c72008-07-23 21:28:25 -0700145 } else
146 ret = security_capget(current, pEp, pIp, pPp);
147
148 return ret;
149}
150
Randy Dunlap207a7ba2005-07-27 11:45:10 -0700151/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 * sys_capget - get the capabilities of a given process.
Randy Dunlap207a7ba2005-07-27 11:45:10 -0700153 * @header: pointer to struct that contains capability version and
154 * target pid data
155 * @dataptr: pointer to struct that contains the effective, permitted,
156 * and inheritable capabilities that are returned
157 *
158 * Returns 0 on success and < 0 on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 */
Heiko Carstensb290ebe2009-01-14 14:14:06 +0100160SYSCALL_DEFINE2(capget, cap_user_header_t, header, cap_user_data_t, dataptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Daniel Walker314f70f2007-10-18 03:06:08 -0700162 int ret = 0;
163 pid_t pid;
Andrew Morgane338d262008-02-04 22:29:42 -0800164 unsigned tocopy;
165 kernel_cap_t pE, pI, pP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Andrew G. Morganca05a992008-05-27 22:05:17 -0700167 ret = cap_validate_magic(header, &tocopy);
Andrew G. Morganc4a5af52009-11-23 04:57:52 +0000168 if ((dataptr == NULL) || (ret != 0))
169 return ((dataptr == NULL) && (ret == -EINVAL)) ? 0 : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Daniel Walker314f70f2007-10-18 03:06:08 -0700171 if (get_user(pid, &header->pid))
172 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Daniel Walker314f70f2007-10-18 03:06:08 -0700174 if (pid < 0)
175 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Andrew G. Morganab763c72008-07-23 21:28:25 -0700177 ret = cap_get_target_pid(pid, &pE, &pI, &pP);
Andrew Morgane338d262008-02-04 22:29:42 -0800178 if (!ret) {
Andrew G. Morganca05a992008-05-27 22:05:17 -0700179 struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
Andrew Morgane338d262008-02-04 22:29:42 -0800180 unsigned i;
181
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];
186 }
187
188 /*
Andrew G. Morganca05a992008-05-27 22:05:17 -0700189 * Note, in the case, tocopy < _KERNEL_CAPABILITY_U32S,
Andrew Morgane338d262008-02-04 22:29:42 -0800190 * 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
194 * sequence.
195 *
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
199 * capabilities.
200 *
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
205 * fails.
206 */
Andrew Morgane338d262008-02-04 22:29:42 -0800207 if (copy_to_user(dataptr, kdata, tocopy
208 * sizeof(struct __user_cap_data_struct))) {
209 return -EFAULT;
210 }
211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Daniel Walker314f70f2007-10-18 03:06:08 -0700213 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Randy Dunlap207a7ba2005-07-27 11:45:10 -0700216/**
Andrew G. Morganab763c72008-07-23 21:28:25 -0700217 * sys_capset - set capabilities for a process or (*) a group of processes
Randy Dunlap207a7ba2005-07-27 11:45:10 -0700218 * @header: pointer to struct that contains capability version and
219 * target pid data
220 * @data: pointer to struct that contains the effective, permitted,
221 * and inheritable capabilities
222 *
David Howells1cdcbec2008-11-14 10:39:14 +1100223 * Set capabilities for the current process only. The ability to any other
224 * process(es) has been deprecated and removed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 *
226 * The restrictions on setting capabilities are specified as:
227 *
David Howells1cdcbec2008-11-14 10:39:14 +1100228 * 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
Randy Dunlap207a7ba2005-07-27 11:45:10 -0700231 *
232 * Returns 0 on success and < 0 on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 */
Heiko Carstensb290ebe2009-01-14 14:14:06 +0100234SYSCALL_DEFINE2(capset, cap_user_header_t, header, const cap_user_data_t, data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Andrew G. Morganca05a992008-05-27 22:05:17 -0700236 struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
Arjan van de Ven825332e2009-10-14 08:17:36 +1100237 unsigned i, tocopy, copybytes;
Daniel Walker314f70f2007-10-18 03:06:08 -0700238 kernel_cap_t inheritable, permitted, effective;
David Howellsd84f4f92008-11-14 10:39:23 +1100239 struct cred *new;
Daniel Walker314f70f2007-10-18 03:06:08 -0700240 int ret;
241 pid_t pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Andrew G. Morganca05a992008-05-27 22:05:17 -0700243 ret = cap_validate_magic(header, &tocopy);
244 if (ret != 0)
245 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Daniel Walker314f70f2007-10-18 03:06:08 -0700247 if (get_user(pid, &header->pid))
248 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
David Howells1cdcbec2008-11-14 10:39:14 +1100250 /* may only affect current now */
251 if (pid != 0 && pid != task_pid_vnr(current))
252 return -EPERM;
253
Arjan van de Ven825332e2009-10-14 08:17:36 +1100254 copybytes = tocopy * sizeof(struct __user_cap_data_struct);
255 if (copybytes > sizeof(kdata))
256 return -EFAULT;
257
258 if (copy_from_user(&kdata, data, copybytes))
Daniel Walker314f70f2007-10-18 03:06:08 -0700259 return -EFAULT;
Andrew Morgane338d262008-02-04 22:29:42 -0800260
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;
265 }
Andrew G. Morganca05a992008-05-27 22:05:17 -0700266 while (i < _KERNEL_CAPABILITY_U32S) {
Andrew Morgane338d262008-02-04 22:29:42 -0800267 effective.cap[i] = 0;
268 permitted.cap[i] = 0;
269 inheritable.cap[i] = 0;
270 i++;
271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
David Howellsd84f4f92008-11-14 10:39:23 +1100273 new = prepare_creds();
274 if (!new)
275 return -ENOMEM;
276
277 ret = security_capset(new, current_cred(),
278 &effective, &inheritable, &permitted);
279 if (ret < 0)
280 goto error;
281
Al Viro57f71a02009-01-04 14:52:57 -0500282 audit_log_capset(pid, new, current_cred());
Eric Parise68b75a02008-11-11 21:48:22 +1100283
David Howellsd84f4f92008-11-14 10:39:23 +1100284 return commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
David Howellsd84f4f92008-11-14 10:39:23 +1100286error:
287 abort_creds(new);
Daniel Walker314f70f2007-10-18 03:06:08 -0700288 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
Chris Wright12b59892006-03-25 03:07:41 -0800290
David Howells5cd9c582008-08-14 11:37:28 +0100291/**
Serge E. Hallyn32632452011-03-23 16:43:21 -0700292 * 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
295 *
296 * Return true if the specified task has the given superior capability
297 * currently in effect to the initial user namespace, false if not.
298 *
299 * Note that this does not set PF_SUPERPRIV on the task.
300 */
301bool has_capability(struct task_struct *t, int cap)
302{
303 int ret = security_real_capable(t, &init_user_ns, cap);
304
305 return (ret == 0);
306}
307
308/**
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
313 *
314 * Return true if the specified task has the given superior capability
315 * currently in effect to the specified user namespace, false if not.
316 *
317 * Note that this does not set PF_SUPERPRIV on the task.
318 */
319bool has_ns_capability(struct task_struct *t,
320 struct user_namespace *ns, int cap)
321{
322 int ret = security_real_capable(t, ns, cap);
323
324 return (ret == 0);
325}
326
327/**
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
331 *
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.
335 *
336 * Note that this does not set PF_SUPERPRIV on the task.
337 */
338bool has_capability_noaudit(struct task_struct *t, int cap)
339{
340 int ret = security_real_capable_noaudit(t, &init_user_ns, cap);
341
342 return (ret == 0);
343}
344
345/**
David Howells5cd9c582008-08-14 11:37:28 +0100346 * capable - Determine if the current task has a superior capability in effect
347 * @cap: The capability to be tested for
348 *
349 * Return true if the current task has the given superior capability currently
350 * available for use, false if not.
351 *
352 * This sets PF_SUPERPRIV on the task if the capability is available on the
353 * assumption that it's about to be used.
354 */
Serge E. Hallyn3486740a2011-03-23 16:43:17 -0700355bool capable(int cap)
356{
357 return ns_capable(&init_user_ns, cap);
358}
359EXPORT_SYMBOL(capable);
360
361/**
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
365 *
366 * Return true if the current task has the given superior capability currently
367 * available for use, false if not.
368 *
369 * This sets PF_SUPERPRIV on the task if the capability is available on the
370 * assumption that it's about to be used.
371 */
372bool ns_capable(struct user_namespace *ns, int cap)
Chris Wright12b59892006-03-25 03:07:41 -0800373{
Eric Paris637d32d2008-10-29 15:42:12 +1100374 if (unlikely(!cap_valid(cap))) {
375 printk(KERN_CRIT "capable() called with invalid cap=%u\n", cap);
376 BUG();
377 }
378
Serge E. Hallyn3486740a2011-03-23 16:43:17 -0700379 if (security_capable(ns, current_cred(), cap) == 0) {
David Howells5cd9c582008-08-14 11:37:28 +0100380 current->flags |= PF_SUPERPRIV;
Serge E. Hallyn3486740a2011-03-23 16:43:17 -0700381 return true;
Chris Wright12b59892006-03-25 03:07:41 -0800382 }
Serge E. Hallyn3486740a2011-03-23 16:43:17 -0700383 return false;
Chris Wright12b59892006-03-25 03:07:41 -0800384}
Serge E. Hallyn3486740a2011-03-23 16:43:17 -0700385EXPORT_SYMBOL(ns_capable);
386
387/**
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.
392 *
393 * Return true if it does, false otherwise.
394 */
395bool task_ns_capable(struct task_struct *t, int cap)
396{
397 return ns_capable(task_cred_xxx(t, user)->user_ns, cap);
398}
399EXPORT_SYMBOL(task_ns_capable);