blob: cfbe44299488c18187d80acbe06d9e03c5dc8a83 [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
Randy.Dunlapc59ede72006-01-11 12:17:46 -080010#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/mm.h>
12#include <linux/module.h>
13#include <linux/security.h>
14#include <linux/syscalls.h>
Serge E. Hallynb460cbc2007-10-18 23:39:52 -070015#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/uaccess.h>
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018/*
19 * This lock protects task->cap_* for all tasks including current.
20 * Locking rule: acquire this prior to tasklist_lock.
21 */
22static DEFINE_SPINLOCK(task_capability_lock);
23
24/*
Andrew Morgane338d262008-02-04 22:29:42 -080025 * Leveraged for setting/resetting capabilities
26 */
27
28const kernel_cap_t __cap_empty_set = CAP_EMPTY_SET;
29const kernel_cap_t __cap_full_set = CAP_FULL_SET;
30const kernel_cap_t __cap_init_eff_set = CAP_INIT_EFF_SET;
31
32EXPORT_SYMBOL(__cap_empty_set);
33EXPORT_SYMBOL(__cap_full_set);
34EXPORT_SYMBOL(__cap_init_eff_set);
35
36/*
37 * More recent versions of libcap are available from:
38 *
39 * http://www.kernel.org/pub/linux/libs/security/linux-privs/
40 */
41
42static void warn_legacy_capability_use(void)
43{
44 static int warned;
45 if (!warned) {
46 char name[sizeof(current->comm)];
47
48 printk(KERN_INFO "warning: `%s' uses 32-bit capabilities"
49 " (legacy support in use)\n",
50 get_task_comm(name, current));
51 warned = 1;
52 }
53}
54
55/*
Andrew G. Morganca05a992008-05-27 22:05:17 -070056 * Version 2 capabilities worked fine, but the linux/capability.h file
57 * that accompanied their introduction encouraged their use without
58 * the necessary user-space source code changes. As such, we have
59 * created a version 3 with equivalent functionality to version 2, but
60 * with a header change to protect legacy source code from using
61 * version 2 when it wanted to use version 1. If your system has code
62 * that trips the following warning, it is using version 2 specific
63 * 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 your
67 * application with modern kernel headers and this warning will go
68 * away.
69 */
70
71static void warn_deprecated_v2(void)
72{
73 static int warned;
74
75 if (!warned) {
76 char name[sizeof(current->comm)];
77
78 printk(KERN_INFO "warning: `%s' uses deprecated v2"
79 " capabilities in a way that may be insecure.\n",
80 get_task_comm(name, current));
81 warned = 1;
82 }
83}
84
85/*
86 * Version check. Return the number of u32s in each capability flag
87 * array, or a negative value on error.
88 */
89static int cap_validate_magic(cap_user_header_t header, unsigned *tocopy)
90{
91 __u32 version;
92
93 if (get_user(version, &header->version))
94 return -EFAULT;
95
96 switch (version) {
97 case _LINUX_CAPABILITY_VERSION_1:
98 warn_legacy_capability_use();
99 *tocopy = _LINUX_CAPABILITY_U32S_1;
100 break;
101 case _LINUX_CAPABILITY_VERSION_2:
102 warn_deprecated_v2();
103 /*
104 * fall through - v3 is otherwise equivalent to v2.
105 */
106 case _LINUX_CAPABILITY_VERSION_3:
107 *tocopy = _LINUX_CAPABILITY_U32S_3;
108 break;
109 default:
110 if (put_user((u32)_KERNEL_CAPABILITY_VERSION, &header->version))
111 return -EFAULT;
112 return -EINVAL;
113 }
114
115 return 0;
116}
117
118/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 * For sys_getproccap() and sys_setproccap(), any of the three
120 * capability set pointers may be NULL -- indicating that that set is
121 * uninteresting and/or not to be changed.
122 */
123
Randy Dunlap207a7ba2005-07-27 11:45:10 -0700124/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 * sys_capget - get the capabilities of a given process.
Randy Dunlap207a7ba2005-07-27 11:45:10 -0700126 * @header: pointer to struct that contains capability version and
127 * target pid data
128 * @dataptr: pointer to struct that contains the effective, permitted,
129 * and inheritable capabilities that are returned
130 *
131 * Returns 0 on success and < 0 on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 */
133asmlinkage long sys_capget(cap_user_header_t header, cap_user_data_t dataptr)
134{
Daniel Walker314f70f2007-10-18 03:06:08 -0700135 int ret = 0;
136 pid_t pid;
Daniel Walker314f70f2007-10-18 03:06:08 -0700137 struct task_struct *target;
Andrew Morgane338d262008-02-04 22:29:42 -0800138 unsigned tocopy;
139 kernel_cap_t pE, pI, pP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Andrew G. Morganca05a992008-05-27 22:05:17 -0700141 ret = cap_validate_magic(header, &tocopy);
142 if (ret != 0)
143 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Daniel Walker314f70f2007-10-18 03:06:08 -0700145 if (get_user(pid, &header->pid))
146 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Daniel Walker314f70f2007-10-18 03:06:08 -0700148 if (pid < 0)
149 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Daniel Walker314f70f2007-10-18 03:06:08 -0700151 spin_lock(&task_capability_lock);
152 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700154 if (pid && pid != task_pid_vnr(current)) {
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700155 target = find_task_by_vpid(pid);
Daniel Walker314f70f2007-10-18 03:06:08 -0700156 if (!target) {
157 ret = -ESRCH;
158 goto out;
159 }
160 } else
161 target = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Andrew Morgane338d262008-02-04 22:29:42 -0800163 ret = security_capget(target, &pE, &pI, &pP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165out:
Daniel Walker314f70f2007-10-18 03:06:08 -0700166 read_unlock(&tasklist_lock);
167 spin_unlock(&task_capability_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Andrew Morgane338d262008-02-04 22:29:42 -0800169 if (!ret) {
Andrew G. Morganca05a992008-05-27 22:05:17 -0700170 struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
Andrew Morgane338d262008-02-04 22:29:42 -0800171 unsigned i;
172
173 for (i = 0; i < tocopy; i++) {
174 kdata[i].effective = pE.cap[i];
175 kdata[i].permitted = pP.cap[i];
176 kdata[i].inheritable = pI.cap[i];
177 }
178
179 /*
Andrew G. Morganca05a992008-05-27 22:05:17 -0700180 * Note, in the case, tocopy < _KERNEL_CAPABILITY_U32S,
Andrew Morgane338d262008-02-04 22:29:42 -0800181 * we silently drop the upper capabilities here. This
182 * has the effect of making older libcap
183 * implementations implicitly drop upper capability
184 * bits when they perform a: capget/modify/capset
185 * sequence.
186 *
187 * This behavior is considered fail-safe
188 * behavior. Upgrading the application to a newer
189 * version of libcap will enable access to the newer
190 * capabilities.
191 *
192 * An alternative would be to return an error here
193 * (-ERANGE), but that causes legacy applications to
194 * unexpectidly fail; the capget/modify/capset aborts
195 * before modification is attempted and the application
196 * fails.
197 */
198
199 if (copy_to_user(dataptr, kdata, tocopy
200 * sizeof(struct __user_cap_data_struct))) {
201 return -EFAULT;
202 }
203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Daniel Walker314f70f2007-10-18 03:06:08 -0700205 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
208/*
209 * cap_set_pg - set capabilities for all processes in a given process
210 * group. We call this holding task_capability_lock and tasklist_lock.
211 */
Eric W. Biederman41487c62007-02-12 00:53:01 -0800212static inline int cap_set_pg(int pgrp_nr, kernel_cap_t *effective,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 kernel_cap_t *inheritable,
214 kernel_cap_t *permitted)
215{
Ingo Molnar36c8b582006-07-03 00:25:41 -0700216 struct task_struct *g, *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 int ret = -EPERM;
218 int found = 0;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800219 struct pid *pgrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Pavel Emelyanov89905712007-10-18 23:40:19 -0700221 pgrp = find_vpid(pgrp_nr);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800222 do_each_pid_task(pgrp, PIDTYPE_PGID, g) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 target = g;
224 while_each_thread(g, target) {
225 if (!security_capset_check(target, effective,
226 inheritable,
227 permitted)) {
228 security_capset_set(target, effective,
229 inheritable,
230 permitted);
231 ret = 0;
232 }
233 found = 1;
234 }
Eric W. Biederman41487c62007-02-12 00:53:01 -0800235 } while_each_pid_task(pgrp, PIDTYPE_PGID, g);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 if (!found)
Daniel Walker314f70f2007-10-18 03:06:08 -0700238 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return ret;
240}
241
242/*
243 * cap_set_all - set capabilities for all processes other than init
244 * and self. We call this holding task_capability_lock and tasklist_lock.
245 */
246static inline int cap_set_all(kernel_cap_t *effective,
247 kernel_cap_t *inheritable,
248 kernel_cap_t *permitted)
249{
Ingo Molnar36c8b582006-07-03 00:25:41 -0700250 struct task_struct *g, *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 int ret = -EPERM;
252 int found = 0;
253
254 do_each_thread(g, target) {
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700255 if (target == current || is_container_init(target->group_leader))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 continue;
257 found = 1;
258 if (security_capset_check(target, effective, inheritable,
259 permitted))
260 continue;
261 ret = 0;
262 security_capset_set(target, effective, inheritable, permitted);
263 } while_each_thread(g, target);
264
265 if (!found)
266 ret = 0;
267 return ret;
268}
269
Randy Dunlap207a7ba2005-07-27 11:45:10 -0700270/**
271 * sys_capset - set capabilities for a process or a group of processes
272 * @header: pointer to struct that contains capability version and
273 * target pid data
274 * @data: pointer to struct that contains the effective, permitted,
275 * and inheritable capabilities
276 *
277 * Set capabilities for a given process, all processes, or all
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 * processes in a given process group.
279 *
280 * The restrictions on setting capabilities are specified as:
281 *
282 * [pid is for the 'target' task. 'current' is the calling task.]
283 *
284 * I: any raised capabilities must be a subset of the (old current) permitted
285 * P: any raised capabilities must be a subset of the (old current) permitted
286 * E: must be set to a subset of (new target) permitted
Randy Dunlap207a7ba2005-07-27 11:45:10 -0700287 *
288 * Returns 0 on success and < 0 on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 */
290asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data)
291{
Andrew G. Morganca05a992008-05-27 22:05:17 -0700292 struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
Andrew Morgane338d262008-02-04 22:29:42 -0800293 unsigned i, tocopy;
Daniel Walker314f70f2007-10-18 03:06:08 -0700294 kernel_cap_t inheritable, permitted, effective;
Daniel Walker314f70f2007-10-18 03:06:08 -0700295 struct task_struct *target;
296 int ret;
297 pid_t pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Andrew G. Morganca05a992008-05-27 22:05:17 -0700299 ret = cap_validate_magic(header, &tocopy);
300 if (ret != 0)
301 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Daniel Walker314f70f2007-10-18 03:06:08 -0700303 if (get_user(pid, &header->pid))
304 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700306 if (pid && pid != task_pid_vnr(current) && !capable(CAP_SETPCAP))
Daniel Walker314f70f2007-10-18 03:06:08 -0700307 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Andrew Morgane338d262008-02-04 22:29:42 -0800309 if (copy_from_user(&kdata, data, tocopy
310 * sizeof(struct __user_cap_data_struct))) {
Daniel Walker314f70f2007-10-18 03:06:08 -0700311 return -EFAULT;
Andrew Morgane338d262008-02-04 22:29:42 -0800312 }
313
314 for (i = 0; i < tocopy; i++) {
315 effective.cap[i] = kdata[i].effective;
316 permitted.cap[i] = kdata[i].permitted;
317 inheritable.cap[i] = kdata[i].inheritable;
318 }
Andrew G. Morganca05a992008-05-27 22:05:17 -0700319 while (i < _KERNEL_CAPABILITY_U32S) {
Andrew Morgane338d262008-02-04 22:29:42 -0800320 effective.cap[i] = 0;
321 permitted.cap[i] = 0;
322 inheritable.cap[i] = 0;
323 i++;
324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Daniel Walker314f70f2007-10-18 03:06:08 -0700326 spin_lock(&task_capability_lock);
327 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700329 if (pid > 0 && pid != task_pid_vnr(current)) {
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700330 target = find_task_by_vpid(pid);
Daniel Walker314f70f2007-10-18 03:06:08 -0700331 if (!target) {
332 ret = -ESRCH;
333 goto out;
334 }
335 } else
336 target = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Daniel Walker314f70f2007-10-18 03:06:08 -0700338 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Daniel Walker314f70f2007-10-18 03:06:08 -0700340 /* having verified that the proposed changes are legal,
341 we now put them into effect. */
342 if (pid < 0) {
343 if (pid == -1) /* all procs other than current and init */
344 ret = cap_set_all(&effective, &inheritable, &permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Daniel Walker314f70f2007-10-18 03:06:08 -0700346 else /* all procs in process group */
347 ret = cap_set_pg(-pid, &effective, &inheritable,
348 &permitted);
349 } else {
350 ret = security_capset_check(target, &effective, &inheritable,
351 &permitted);
352 if (!ret)
353 security_capset_set(target, &effective, &inheritable,
354 &permitted);
355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357out:
Daniel Walker314f70f2007-10-18 03:06:08 -0700358 read_unlock(&tasklist_lock);
359 spin_unlock(&task_capability_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Daniel Walker314f70f2007-10-18 03:06:08 -0700361 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
Chris Wright12b59892006-03-25 03:07:41 -0800363
364int __capable(struct task_struct *t, int cap)
365{
366 if (security_capable(t, cap) == 0) {
367 t->flags |= PF_SUPERPRIV;
368 return 1;
369 }
370 return 0;
371}
Chris Wright12b59892006-03-25 03:07:41 -0800372
373int capable(int cap)
374{
375 return __capable(current, cap);
376}
377EXPORT_SYMBOL(capable);