This source file includes following definitions.
- file_caps_disable
- warn_legacy_capability_use
- warn_deprecated_v2
- cap_validate_magic
- cap_get_target_pid
- SYSCALL_DEFINE2
- SYSCALL_DEFINE2
- has_ns_capability
- has_capability
- has_ns_capability_noaudit
- has_capability_noaudit
- ns_capable_common
- ns_capable
- ns_capable_noaudit
- ns_capable_setid
- capable
- file_ns_capable
- privileged_wrt_inode_uidgid
- capable_wrt_inode_uidgid
- ptracer_capable
1
2
3
4
5
6
7
8
9
10
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13 #include <linux/audit.h>
14 #include <linux/capability.h>
15 #include <linux/mm.h>
16 #include <linux/export.h>
17 #include <linux/security.h>
18 #include <linux/syscalls.h>
19 #include <linux/pid_namespace.h>
20 #include <linux/user_namespace.h>
21 #include <linux/uaccess.h>
22
23
24
25
26
27 const kernel_cap_t __cap_empty_set = CAP_EMPTY_SET;
28 EXPORT_SYMBOL(__cap_empty_set);
29
30 int file_caps_enabled = 1;
31
32 static 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);
38
39 #ifdef CONFIG_MULTIUSER
40
41
42
43
44
45
46 static void warn_legacy_capability_use(void)
47 {
48 char name[sizeof(current->comm)];
49
50 pr_info_once("warning: `%s' uses 32-bit capabilities (legacy support in use)\n",
51 get_task_comm(name, current));
52 }
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 static void warn_deprecated_v2(void)
71 {
72 char name[sizeof(current->comm)];
73
74 pr_info_once("warning: `%s' uses deprecated v2 capabilities in a way that may be insecure\n",
75 get_task_comm(name, current));
76 }
77
78
79
80
81
82 static int cap_validate_magic(cap_user_header_t header, unsigned *tocopy)
83 {
84 __u32 version;
85
86 if (get_user(version, &header->version))
87 return -EFAULT;
88
89 switch (version) {
90 case _LINUX_CAPABILITY_VERSION_1:
91 warn_legacy_capability_use();
92 *tocopy = _LINUX_CAPABILITY_U32S_1;
93 break;
94 case _LINUX_CAPABILITY_VERSION_2:
95 warn_deprecated_v2();
96
97 case _LINUX_CAPABILITY_VERSION_3:
98 *tocopy = _LINUX_CAPABILITY_U32S_3;
99 break;
100 default:
101 if (put_user((u32)_KERNEL_CAPABILITY_VERSION, &header->version))
102 return -EFAULT;
103 return -EINVAL;
104 }
105
106 return 0;
107 }
108
109
110
111
112
113
114
115
116 static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp,
117 kernel_cap_t *pIp, kernel_cap_t *pPp)
118 {
119 int ret;
120
121 if (pid && (pid != task_pid_vnr(current))) {
122 struct task_struct *target;
123
124 rcu_read_lock();
125
126 target = find_task_by_vpid(pid);
127 if (!target)
128 ret = -ESRCH;
129 else
130 ret = security_capget(target, pEp, pIp, pPp);
131
132 rcu_read_unlock();
133 } else
134 ret = security_capget(current, pEp, pIp, pPp);
135
136 return ret;
137 }
138
139
140
141
142
143
144
145
146
147
148 SYSCALL_DEFINE2(capget, cap_user_header_t, header, cap_user_data_t, dataptr)
149 {
150 int ret = 0;
151 pid_t pid;
152 unsigned tocopy;
153 kernel_cap_t pE, pI, pP;
154
155 ret = cap_validate_magic(header, &tocopy);
156 if ((dataptr == NULL) || (ret != 0))
157 return ((dataptr == NULL) && (ret == -EINVAL)) ? 0 : ret;
158
159 if (get_user(pid, &header->pid))
160 return -EFAULT;
161
162 if (pid < 0)
163 return -EINVAL;
164
165 ret = cap_get_target_pid(pid, &pE, &pI, &pP);
166 if (!ret) {
167 struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
168 unsigned i;
169
170 for (i = 0; i < tocopy; i++) {
171 kdata[i].effective = pE.cap[i];
172 kdata[i].permitted = pP.cap[i];
173 kdata[i].inheritable = pI.cap[i];
174 }
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195 if (copy_to_user(dataptr, kdata, tocopy
196 * sizeof(struct __user_cap_data_struct))) {
197 return -EFAULT;
198 }
199 }
200
201 return ret;
202 }
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222 SYSCALL_DEFINE2(capset, cap_user_header_t, header, const cap_user_data_t, data)
223 {
224 struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
225 unsigned i, tocopy, copybytes;
226 kernel_cap_t inheritable, permitted, effective;
227 struct cred *new;
228 int ret;
229 pid_t pid;
230
231 ret = cap_validate_magic(header, &tocopy);
232 if (ret != 0)
233 return ret;
234
235 if (get_user(pid, &header->pid))
236 return -EFAULT;
237
238
239 if (pid != 0 && pid != task_pid_vnr(current))
240 return -EPERM;
241
242 copybytes = tocopy * sizeof(struct __user_cap_data_struct);
243 if (copybytes > sizeof(kdata))
244 return -EFAULT;
245
246 if (copy_from_user(&kdata, data, copybytes))
247 return -EFAULT;
248
249 for (i = 0; i < tocopy; i++) {
250 effective.cap[i] = kdata[i].effective;
251 permitted.cap[i] = kdata[i].permitted;
252 inheritable.cap[i] = kdata[i].inheritable;
253 }
254 while (i < _KERNEL_CAPABILITY_U32S) {
255 effective.cap[i] = 0;
256 permitted.cap[i] = 0;
257 inheritable.cap[i] = 0;
258 i++;
259 }
260
261 effective.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
262 permitted.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
263 inheritable.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
264
265 new = prepare_creds();
266 if (!new)
267 return -ENOMEM;
268
269 ret = security_capset(new, current_cred(),
270 &effective, &inheritable, &permitted);
271 if (ret < 0)
272 goto error;
273
274 audit_log_capset(new, current_cred());
275
276 return commit_creds(new);
277
278 error:
279 abort_creds(new);
280 return ret;
281 }
282
283
284
285
286
287
288
289
290
291
292
293
294 bool has_ns_capability(struct task_struct *t,
295 struct user_namespace *ns, int cap)
296 {
297 int ret;
298
299 rcu_read_lock();
300 ret = security_capable(__task_cred(t), ns, cap, CAP_OPT_NONE);
301 rcu_read_unlock();
302
303 return (ret == 0);
304 }
305
306
307
308
309
310
311
312
313
314
315
316 bool has_capability(struct task_struct *t, int cap)
317 {
318 return has_ns_capability(t, &init_user_ns, cap);
319 }
320 EXPORT_SYMBOL(has_capability);
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335 bool has_ns_capability_noaudit(struct task_struct *t,
336 struct user_namespace *ns, int cap)
337 {
338 int ret;
339
340 rcu_read_lock();
341 ret = security_capable(__task_cred(t), ns, cap, CAP_OPT_NOAUDIT);
342 rcu_read_unlock();
343
344 return (ret == 0);
345 }
346
347
348
349
350
351
352
353
354
355
356
357
358
359 bool has_capability_noaudit(struct task_struct *t, int cap)
360 {
361 return has_ns_capability_noaudit(t, &init_user_ns, cap);
362 }
363
364 static bool ns_capable_common(struct user_namespace *ns,
365 int cap,
366 unsigned int opts)
367 {
368 int capable;
369
370 if (unlikely(!cap_valid(cap))) {
371 pr_crit("capable() called with invalid cap=%u\n", cap);
372 BUG();
373 }
374
375 capable = security_capable(current_cred(), ns, cap, opts);
376 if (capable == 0) {
377 current->flags |= PF_SUPERPRIV;
378 return true;
379 }
380 return false;
381 }
382
383
384
385
386
387
388
389
390
391
392
393
394 bool ns_capable(struct user_namespace *ns, int cap)
395 {
396 return ns_capable_common(ns, cap, CAP_OPT_NONE);
397 }
398 EXPORT_SYMBOL(ns_capable);
399
400
401
402
403
404
405
406
407
408
409
410
411
412 bool ns_capable_noaudit(struct user_namespace *ns, int cap)
413 {
414 return ns_capable_common(ns, cap, CAP_OPT_NOAUDIT);
415 }
416 EXPORT_SYMBOL(ns_capable_noaudit);
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431 bool ns_capable_setid(struct user_namespace *ns, int cap)
432 {
433 return ns_capable_common(ns, cap, CAP_OPT_INSETID);
434 }
435 EXPORT_SYMBOL(ns_capable_setid);
436
437
438
439
440
441
442
443
444
445
446
447 bool capable(int cap)
448 {
449 return ns_capable(&init_user_ns, cap);
450 }
451 EXPORT_SYMBOL(capable);
452 #endif
453
454
455
456
457
458
459
460
461
462
463
464
465
466 bool file_ns_capable(const struct file *file, struct user_namespace *ns,
467 int cap)
468 {
469
470 if (WARN_ON_ONCE(!cap_valid(cap)))
471 return false;
472
473 if (security_capable(file->f_cred, ns, cap, CAP_OPT_NONE) == 0)
474 return true;
475
476 return false;
477 }
478 EXPORT_SYMBOL(file_ns_capable);
479
480
481
482
483
484
485
486
487 bool privileged_wrt_inode_uidgid(struct user_namespace *ns, const struct inode *inode)
488 {
489 return kuid_has_mapping(ns, inode->i_uid) &&
490 kgid_has_mapping(ns, inode->i_gid);
491 }
492
493
494
495
496
497
498
499
500
501
502 bool capable_wrt_inode_uidgid(const struct inode *inode, int cap)
503 {
504 struct user_namespace *ns = current_user_ns();
505
506 return ns_capable(ns, cap) && privileged_wrt_inode_uidgid(ns, inode);
507 }
508 EXPORT_SYMBOL(capable_wrt_inode_uidgid);
509
510
511
512
513
514
515
516
517
518 bool ptracer_capable(struct task_struct *tsk, struct user_namespace *ns)
519 {
520 int ret = 0;
521 const struct cred *cred;
522
523 rcu_read_lock();
524 cred = rcu_dereference(tsk->ptracer_cred);
525 if (cred)
526 ret = security_capable(cred, ns, CAP_SYS_PTRACE,
527 CAP_OPT_NOAUDIT);
528 rcu_read_unlock();
529 return (ret == 0);
530 }