blob: 2f05303715a5c4066a04b128251ebeec08d3779b [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19/*
Andrew Morgane338d262008-02-04 22:29:42 -080020 * Leveraged for setting/resetting capabilities
21 */
22
23const kernel_cap_t __cap_empty_set = CAP_EMPTY_SET;
24const kernel_cap_t __cap_full_set = CAP_FULL_SET;
25const kernel_cap_t __cap_init_eff_set = CAP_INIT_EFF_SET;
26
27EXPORT_SYMBOL(__cap_empty_set);
28EXPORT_SYMBOL(__cap_full_set);
29EXPORT_SYMBOL(__cap_init_eff_set);
30
Serge E. Hallyn1f29fae2008-11-05 16:08:52 -060031int file_caps_enabled = 1;
32
33static int __init file_caps_disable(char *str)
34{
35 file_caps_enabled = 0;
36 return 1;
37}
38__setup("no_file_caps", file_caps_disable);
Serge E. Hallyn1f29fae2008-11-05 16:08:52 -060039
Andrew Morgane338d262008-02-04 22:29:42 -080040/*
41 * More recent versions of libcap are available from:
42 *
43 * http://www.kernel.org/pub/linux/libs/security/linux-privs/
44 */
45
46static void warn_legacy_capability_use(void)
47{
48 static int warned;
49 if (!warned) {
50 char name[sizeof(current->comm)];
51
52 printk(KERN_INFO "warning: `%s' uses 32-bit capabilities"
53 " (legacy support in use)\n",
54 get_task_comm(name, current));
55 warned = 1;
56 }
57}
58
59/*
Andrew G. Morganca05a992008-05-27 22:05:17 -070060 * Version 2 capabilities worked fine, but the linux/capability.h file
61 * that accompanied their introduction encouraged their use without
62 * the necessary user-space source code changes. As such, we have
63 * created a version 3 with equivalent functionality to version 2, but
64 * with a header change to protect legacy source code from using
65 * version 2 when it wanted to use version 1. If your system has code
66 * that trips the following warning, it is using version 2 specific
67 * capabilities and may be doing so insecurely.
68 *
69 * The remedy is to either upgrade your version of libcap (to 2.10+,
70 * if the application is linked against it), or recompile your
71 * application with modern kernel headers and this warning will go
72 * away.
73 */
74
75static void warn_deprecated_v2(void)
76{
77 static int warned;
78
79 if (!warned) {
80 char name[sizeof(current->comm)];
81
82 printk(KERN_INFO "warning: `%s' uses deprecated v2"
83 " capabilities in a way that may be insecure.\n",
84 get_task_comm(name, current));
85 warned = 1;
86 }
87}
88
89/*
90 * Version check. Return the number of u32s in each capability flag
91 * array, or a negative value on error.
92 */
93static int cap_validate_magic(cap_user_header_t header, unsigned *tocopy)
94{
95 __u32 version;
96
97 if (get_user(version, &header->version))
98 return -EFAULT;
99
100 switch (version) {
101 case _LINUX_CAPABILITY_VERSION_1:
102 warn_legacy_capability_use();
103 *tocopy = _LINUX_CAPABILITY_U32S_1;
104 break;
105 case _LINUX_CAPABILITY_VERSION_2:
106 warn_deprecated_v2();
107 /*
108 * fall through - v3 is otherwise equivalent to v2.
109 */
110 case _LINUX_CAPABILITY_VERSION_3:
111 *tocopy = _LINUX_CAPABILITY_U32S_3;
112 break;
113 default:
114 if (put_user((u32)_KERNEL_CAPABILITY_VERSION, &header->version))
115 return -EFAULT;
116 return -EINVAL;
117 }
118
119 return 0;
120}
121
Andrew G. Morganab763c72008-07-23 21:28:25 -0700122/*
David Howellsd84f4f92008-11-14 10:39:23 +1100123 * The only thing that can change the capabilities of the current
124 * process is the current process. As such, we can't be in this code
125 * at the same time as we are in the process of setting capabilities
126 * in this process. The net result is that we can limit our use of
127 * locks to when we are reading the caps of another process.
Andrew G. Morganab763c72008-07-23 21:28:25 -0700128 */
129static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp,
130 kernel_cap_t *pIp, kernel_cap_t *pPp)
131{
132 int ret;
133
134 if (pid && (pid != task_pid_vnr(current))) {
135 struct task_struct *target;
136
Thomas Gleixner86fc80f2009-12-09 17:13:31 +0100137 rcu_read_lock();
Andrew G. Morganab763c72008-07-23 21:28:25 -0700138
139 target = find_task_by_vpid(pid);
140 if (!target)
141 ret = -ESRCH;
142 else
143 ret = security_capget(target, pEp, pIp, pPp);
144
Thomas Gleixner86fc80f2009-12-09 17:13:31 +0100145 rcu_read_unlock();
Andrew G. Morganab763c72008-07-23 21:28:25 -0700146 } else
147 ret = security_capget(current, pEp, pIp, pPp);
148
149 return ret;
150}
151
Randy Dunlap207a7ba2005-07-27 11:45:10 -0700152/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 * sys_capget - get the capabilities of a given process.
Randy Dunlap207a7ba2005-07-27 11:45:10 -0700154 * @header: pointer to struct that contains capability version and
155 * target pid data
156 * @dataptr: pointer to struct that contains the effective, permitted,
157 * and inheritable capabilities that are returned
158 *
159 * Returns 0 on success and < 0 on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 */
Heiko Carstensb290ebe2009-01-14 14:14:06 +0100161SYSCALL_DEFINE2(capget, cap_user_header_t, header, cap_user_data_t, dataptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Daniel Walker314f70f2007-10-18 03:06:08 -0700163 int ret = 0;
164 pid_t pid;
Andrew Morgane338d262008-02-04 22:29:42 -0800165 unsigned tocopy;
166 kernel_cap_t pE, pI, pP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Andrew G. Morganca05a992008-05-27 22:05:17 -0700168 ret = cap_validate_magic(header, &tocopy);
Andrew G. Morganc4a5af52009-11-23 04:57:52 +0000169 if ((dataptr == NULL) || (ret != 0))
170 return ((dataptr == NULL) && (ret == -EINVAL)) ? 0 : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Daniel Walker314f70f2007-10-18 03:06:08 -0700172 if (get_user(pid, &header->pid))
173 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Daniel Walker314f70f2007-10-18 03:06:08 -0700175 if (pid < 0)
176 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Andrew G. Morganab763c72008-07-23 21:28:25 -0700178 ret = cap_get_target_pid(pid, &pE, &pI, &pP);
Andrew Morgane338d262008-02-04 22:29:42 -0800179 if (!ret) {
Andrew G. Morganca05a992008-05-27 22:05:17 -0700180 struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
Andrew Morgane338d262008-02-04 22:29:42 -0800181 unsigned i;
182
183 for (i = 0; i < tocopy; i++) {
184 kdata[i].effective = pE.cap[i];
185 kdata[i].permitted = pP.cap[i];
186 kdata[i].inheritable = pI.cap[i];
187 }
188
189 /*
Andrew G. Morganca05a992008-05-27 22:05:17 -0700190 * Note, in the case, tocopy < _KERNEL_CAPABILITY_U32S,
Andrew Morgane338d262008-02-04 22:29:42 -0800191 * we silently drop the upper capabilities here. This
192 * has the effect of making older libcap
193 * implementations implicitly drop upper capability
194 * bits when they perform a: capget/modify/capset
195 * sequence.
196 *
197 * This behavior is considered fail-safe
198 * behavior. Upgrading the application to a newer
199 * version of libcap will enable access to the newer
200 * capabilities.
201 *
202 * An alternative would be to return an error here
203 * (-ERANGE), but that causes legacy applications to
204 * unexpectidly fail; the capget/modify/capset aborts
205 * before modification is attempted and the application
206 * fails.
207 */
Andrew Morgane338d262008-02-04 22:29:42 -0800208 if (copy_to_user(dataptr, kdata, tocopy
209 * sizeof(struct __user_cap_data_struct))) {
210 return -EFAULT;
211 }
212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Daniel Walker314f70f2007-10-18 03:06:08 -0700214 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Randy Dunlap207a7ba2005-07-27 11:45:10 -0700217/**
Andrew G. Morganab763c72008-07-23 21:28:25 -0700218 * sys_capset - set capabilities for a process or (*) a group of processes
Randy Dunlap207a7ba2005-07-27 11:45:10 -0700219 * @header: pointer to struct that contains capability version and
220 * target pid data
221 * @data: pointer to struct that contains the effective, permitted,
222 * and inheritable capabilities
223 *
David Howells1cdcbec2008-11-14 10:39:14 +1100224 * Set capabilities for the current process only. The ability to any other
225 * process(es) has been deprecated and removed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 *
227 * The restrictions on setting capabilities are specified as:
228 *
David Howells1cdcbec2008-11-14 10:39:14 +1100229 * I: any raised capabilities must be a subset of the old permitted
230 * P: any raised capabilities must be a subset of the old permitted
231 * E: must be set to a subset of new permitted
Randy Dunlap207a7ba2005-07-27 11:45:10 -0700232 *
233 * Returns 0 on success and < 0 on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 */
Heiko Carstensb290ebe2009-01-14 14:14:06 +0100235SYSCALL_DEFINE2(capset, cap_user_header_t, header, const cap_user_data_t, data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Andrew G. Morganca05a992008-05-27 22:05:17 -0700237 struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
Arjan van de Ven825332e2009-10-14 08:17:36 +1100238 unsigned i, tocopy, copybytes;
Daniel Walker314f70f2007-10-18 03:06:08 -0700239 kernel_cap_t inheritable, permitted, effective;
David Howellsd84f4f92008-11-14 10:39:23 +1100240 struct cred *new;
Daniel Walker314f70f2007-10-18 03:06:08 -0700241 int ret;
242 pid_t pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Andrew G. Morganca05a992008-05-27 22:05:17 -0700244 ret = cap_validate_magic(header, &tocopy);
245 if (ret != 0)
246 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Daniel Walker314f70f2007-10-18 03:06:08 -0700248 if (get_user(pid, &header->pid))
249 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
David Howells1cdcbec2008-11-14 10:39:14 +1100251 /* may only affect current now */
252 if (pid != 0 && pid != task_pid_vnr(current))
253 return -EPERM;
254
Arjan van de Ven825332e2009-10-14 08:17:36 +1100255 copybytes = tocopy * sizeof(struct __user_cap_data_struct);
256 if (copybytes > sizeof(kdata))
257 return -EFAULT;
258
259 if (copy_from_user(&kdata, data, copybytes))
Daniel Walker314f70f2007-10-18 03:06:08 -0700260 return -EFAULT;
Andrew Morgane338d262008-02-04 22:29:42 -0800261
262 for (i = 0; i < tocopy; i++) {
263 effective.cap[i] = kdata[i].effective;
264 permitted.cap[i] = kdata[i].permitted;
265 inheritable.cap[i] = kdata[i].inheritable;
266 }
Andrew G. Morganca05a992008-05-27 22:05:17 -0700267 while (i < _KERNEL_CAPABILITY_U32S) {
Andrew Morgane338d262008-02-04 22:29:42 -0800268 effective.cap[i] = 0;
269 permitted.cap[i] = 0;
270 inheritable.cap[i] = 0;
271 i++;
272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
David Howellsd84f4f92008-11-14 10:39:23 +1100274 new = prepare_creds();
275 if (!new)
276 return -ENOMEM;
277
278 ret = security_capset(new, current_cred(),
279 &effective, &inheritable, &permitted);
280 if (ret < 0)
281 goto error;
282
Al Viro57f71a02009-01-04 14:52:57 -0500283 audit_log_capset(pid, new, current_cred());
Eric Parise68b75a02008-11-11 21:48:22 +1100284
David Howellsd84f4f92008-11-14 10:39:23 +1100285 return commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
David Howellsd84f4f92008-11-14 10:39:23 +1100287error:
288 abort_creds(new);
Daniel Walker314f70f2007-10-18 03:06:08 -0700289 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
Chris Wright12b59892006-03-25 03:07:41 -0800291
David Howells5cd9c582008-08-14 11:37:28 +0100292/**
293 * capable - Determine if the current task has a superior capability in effect
294 * @cap: The capability to be tested for
295 *
296 * Return true if the current task has the given superior capability currently
297 * available for use, false if not.
298 *
299 * This sets PF_SUPERPRIV on the task if the capability is available on the
300 * assumption that it's about to be used.
301 */
302int capable(int cap)
Chris Wright12b59892006-03-25 03:07:41 -0800303{
Eric Paris637d32d2008-10-29 15:42:12 +1100304 if (unlikely(!cap_valid(cap))) {
305 printk(KERN_CRIT "capable() called with invalid cap=%u\n", cap);
306 BUG();
307 }
308
David Howells3699c532009-01-06 22:27:01 +0000309 if (security_capable(cap) == 0) {
David Howells5cd9c582008-08-14 11:37:28 +0100310 current->flags |= PF_SUPERPRIV;
Chris Wright12b59892006-03-25 03:07:41 -0800311 return 1;
312 }
313 return 0;
314}
Chris Wright12b59892006-03-25 03:07:41 -0800315EXPORT_SYMBOL(capable);