1
2
3
4
5
6
7
8
9
10#include <linux/capability.h>
11#include <linux/mm.h>
12#include <linux/module.h>
13#include <linux/security.h>
14#include <linux/syscalls.h>
15#include <linux/pid_namespace.h>
16#include <asm/uaccess.h>
17
18
19
20
21
22static DEFINE_SPINLOCK(task_capability_lock);
23
24
25
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
38
39
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
56
57
58
59
60
61
62
63
64
65
66
67
68
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
87
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
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#ifndef CONFIG_SECURITY_FILE_CAPABILITIES
119
120
121
122
123
124static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp,
125 kernel_cap_t *pIp, kernel_cap_t *pPp)
126{
127 struct task_struct *target;
128 int ret;
129
130 spin_lock(&task_capability_lock);
131 read_lock(&tasklist_lock);
132
133 if (pid && pid != task_pid_vnr(current)) {
134 target = find_task_by_vpid(pid);
135 if (!target) {
136 ret = -ESRCH;
137 goto out;
138 }
139 } else
140 target = current;
141
142 ret = security_capget(target, pEp, pIp, pPp);
143
144out:
145 read_unlock(&tasklist_lock);
146 spin_unlock(&task_capability_lock);
147
148 return ret;
149}
150
151
152
153
154
155static inline int cap_set_pg(int pgrp_nr, kernel_cap_t *effective,
156 kernel_cap_t *inheritable,
157 kernel_cap_t *permitted)
158{
159 struct task_struct *g, *target;
160 int ret = -EPERM;
161 int found = 0;
162 struct pid *pgrp;
163
164 spin_lock(&task_capability_lock);
165 read_lock(&tasklist_lock);
166
167 pgrp = find_vpid(pgrp_nr);
168 do_each_pid_task(pgrp, PIDTYPE_PGID, g) {
169 target = g;
170 while_each_thread(g, target) {
171 if (!security_capset_check(target, effective,
172 inheritable, permitted)) {
173 security_capset_set(target, effective,
174 inheritable, permitted);
175 ret = 0;
176 }
177 found = 1;
178 }
179 } while_each_pid_task(pgrp, PIDTYPE_PGID, g);
180
181 read_unlock(&tasklist_lock);
182 spin_unlock(&task_capability_lock);
183
184 if (!found)
185 ret = 0;
186 return ret;
187}
188
189
190
191
192
193static inline int cap_set_all(kernel_cap_t *effective,
194 kernel_cap_t *inheritable,
195 kernel_cap_t *permitted)
196{
197 struct task_struct *g, *target;
198 int ret = -EPERM;
199 int found = 0;
200
201 spin_lock(&task_capability_lock);
202 read_lock(&tasklist_lock);
203
204 do_each_thread(g, target) {
205 if (target == current
206 || is_container_init(target->group_leader))
207 continue;
208 found = 1;
209 if (security_capset_check(target, effective, inheritable,
210 permitted))
211 continue;
212 ret = 0;
213 security_capset_set(target, effective, inheritable, permitted);
214 } while_each_thread(g, target);
215
216 read_unlock(&tasklist_lock);
217 spin_unlock(&task_capability_lock);
218
219 if (!found)
220 ret = 0;
221
222 return ret;
223}
224
225
226
227
228
229
230static inline int do_sys_capset_other_tasks(pid_t pid, kernel_cap_t *effective,
231 kernel_cap_t *inheritable,
232 kernel_cap_t *permitted)
233{
234 struct task_struct *target;
235 int ret;
236
237 if (!capable(CAP_SETPCAP))
238 return -EPERM;
239
240 if (pid == -1)
241 return cap_set_all(effective, inheritable, permitted);
242
243 else if (pid < 0)
244 return cap_set_pg(-pid, effective, inheritable, permitted);
245
246
247 spin_lock(&task_capability_lock);
248 read_lock(&tasklist_lock);
249
250 target = find_task_by_vpid(pid);
251 if (!target)
252 ret = -ESRCH;
253 else {
254 ret = security_capset_check(target, effective, inheritable,
255 permitted);
256
257
258
259 if (!ret)
260 security_capset_set(target, effective, inheritable,
261 permitted);
262 }
263
264 read_unlock(&tasklist_lock);
265 spin_unlock(&task_capability_lock);
266
267 return ret;
268}
269
270#else
271
272
273
274
275
276
277
278
279
280static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp,
281 kernel_cap_t *pIp, kernel_cap_t *pPp)
282{
283 int ret;
284
285 if (pid && (pid != task_pid_vnr(current))) {
286 struct task_struct *target;
287
288 spin_lock(&task_capability_lock);
289 read_lock(&tasklist_lock);
290
291 target = find_task_by_vpid(pid);
292 if (!target)
293 ret = -ESRCH;
294 else
295 ret = security_capget(target, pEp, pIp, pPp);
296
297 read_unlock(&tasklist_lock);
298 spin_unlock(&task_capability_lock);
299 } else
300 ret = security_capget(current, pEp, pIp, pPp);
301
302 return ret;
303}
304
305
306
307
308
309
310
311static inline int do_sys_capset_other_tasks(pid_t pid,
312 kernel_cap_t *effective,
313 kernel_cap_t *inheritable,
314 kernel_cap_t *permitted)
315{
316 return -EPERM;
317}
318
319#endif
320
321
322
323
324
325
326kernel_cap_t cap_set_effective(const kernel_cap_t pE_new)
327{
328 kernel_cap_t pE_old;
329
330 spin_lock(&task_capability_lock);
331
332 pE_old = current->cap_effective;
333 current->cap_effective = pE_new;
334
335 spin_unlock(&task_capability_lock);
336
337 return pE_old;
338}
339
340EXPORT_SYMBOL(cap_set_effective);
341
342
343
344
345
346
347
348
349
350
351SYSCALL_DEFINE2(capget, cap_user_header_t, header, cap_user_data_t, dataptr)
352{
353 int ret = 0;
354 pid_t pid;
355 unsigned tocopy;
356 kernel_cap_t pE, pI, pP;
357
358 ret = cap_validate_magic(header, &tocopy);
359 if (ret != 0)
360 return ret;
361
362 if (get_user(pid, &header->pid))
363 return -EFAULT;
364
365 if (pid < 0)
366 return -EINVAL;
367
368 ret = cap_get_target_pid(pid, &pE, &pI, &pP);
369
370 if (!ret) {
371 struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
372 unsigned i;
373
374 for (i = 0; i < tocopy; i++) {
375 kdata[i].effective = pE.cap[i];
376 kdata[i].permitted = pP.cap[i];
377 kdata[i].inheritable = pI.cap[i];
378 }
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399 if (copy_to_user(dataptr, kdata, tocopy
400 * sizeof(struct __user_cap_data_struct))) {
401 return -EFAULT;
402 }
403 }
404
405 return ret;
406}
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428SYSCALL_DEFINE2(capset, cap_user_header_t, header, const cap_user_data_t, data)
429{
430 struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
431 unsigned i, tocopy;
432 kernel_cap_t inheritable, permitted, effective;
433 int ret;
434 pid_t pid;
435
436 ret = cap_validate_magic(header, &tocopy);
437 if (ret != 0)
438 return ret;
439
440 if (get_user(pid, &header->pid))
441 return -EFAULT;
442
443 if (copy_from_user(&kdata, data, tocopy
444 * sizeof(struct __user_cap_data_struct))) {
445 return -EFAULT;
446 }
447
448 for (i = 0; i < tocopy; i++) {
449 effective.cap[i] = kdata[i].effective;
450 permitted.cap[i] = kdata[i].permitted;
451 inheritable.cap[i] = kdata[i].inheritable;
452 }
453 while (i < _KERNEL_CAPABILITY_U32S) {
454 effective.cap[i] = 0;
455 permitted.cap[i] = 0;
456 inheritable.cap[i] = 0;
457 i++;
458 }
459
460 if (pid && (pid != task_pid_vnr(current)))
461 ret = do_sys_capset_other_tasks(pid, &effective, &inheritable,
462 &permitted);
463 else {
464
465
466
467
468
469
470
471 spin_lock(&task_capability_lock);
472
473 ret = security_capset_check(current, &effective, &inheritable,
474 &permitted);
475
476
477
478
479 if (!ret)
480 security_capset_set(current, &effective, &inheritable,
481 &permitted);
482 spin_unlock(&task_capability_lock);
483 }
484
485
486 return ret;
487}
488
489
490
491
492
493
494
495
496
497
498
499int capable(int cap)
500{
501 if (has_capability(current, cap)) {
502 current->flags |= PF_SUPERPRIV;
503 return 1;
504 }
505 return 0;
506}
507EXPORT_SYMBOL(capable);
508