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
27const kernel_cap_t __cap_empty_set = CAP_EMPTY_SET;
28EXPORT_SYMBOL(__cap_empty_set);
29
30int 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);
38
39#ifdef CONFIG_MULTIUSER
40
41
42
43
44
45
46static 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
70static 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
82static 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 fallthrough;
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
116static 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
148SYSCALL_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
222SYSCALL_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
278error:
279 abort_creds(new);
280 return ret;
281}
282
283
284
285
286
287
288
289
290
291
292
293
294bool 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
316bool has_capability(struct task_struct *t, int cap)
317{
318 return has_ns_capability(t, &init_user_ns, cap);
319}
320EXPORT_SYMBOL(has_capability);
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335bool 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
359bool has_capability_noaudit(struct task_struct *t, int cap)
360{
361 return has_ns_capability_noaudit(t, &init_user_ns, cap);
362}
363EXPORT_SYMBOL(has_capability_noaudit);
364
365static bool ns_capable_common(struct user_namespace *ns,
366 int cap,
367 unsigned int opts)
368{
369 int capable;
370
371 if (unlikely(!cap_valid(cap))) {
372 pr_crit("capable() called with invalid cap=%u\n", cap);
373 BUG();
374 }
375
376 capable = security_capable(current_cred(), ns, cap, opts);
377 if (capable == 0) {
378 current->flags |= PF_SUPERPRIV;
379 return true;
380 }
381 return false;
382}
383
384
385
386
387
388
389
390
391
392
393
394
395bool ns_capable(struct user_namespace *ns, int cap)
396{
397 return ns_capable_common(ns, cap, CAP_OPT_NONE);
398}
399EXPORT_SYMBOL(ns_capable);
400
401
402
403
404
405
406
407
408
409
410
411
412
413bool ns_capable_noaudit(struct user_namespace *ns, int cap)
414{
415 return ns_capable_common(ns, cap, CAP_OPT_NOAUDIT);
416}
417EXPORT_SYMBOL(ns_capable_noaudit);
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432bool ns_capable_setid(struct user_namespace *ns, int cap)
433{
434 return ns_capable_common(ns, cap, CAP_OPT_INSETID);
435}
436EXPORT_SYMBOL(ns_capable_setid);
437
438
439
440
441
442
443
444
445
446
447
448bool capable(int cap)
449{
450 return ns_capable(&init_user_ns, cap);
451}
452EXPORT_SYMBOL(capable);
453#endif
454
455
456
457
458
459
460
461
462
463
464
465
466
467bool file_ns_capable(const struct file *file, struct user_namespace *ns,
468 int cap)
469{
470
471 if (WARN_ON_ONCE(!cap_valid(cap)))
472 return false;
473
474 if (security_capable(file->f_cred, ns, cap, CAP_OPT_NONE) == 0)
475 return true;
476
477 return false;
478}
479EXPORT_SYMBOL(file_ns_capable);
480
481
482
483
484
485
486
487
488bool privileged_wrt_inode_uidgid(struct user_namespace *ns,
489 struct user_namespace *mnt_userns,
490 const struct inode *inode)
491{
492 return kuid_has_mapping(ns, i_uid_into_mnt(mnt_userns, inode)) &&
493 kgid_has_mapping(ns, i_gid_into_mnt(mnt_userns, inode));
494}
495
496
497
498
499
500
501
502
503
504
505bool capable_wrt_inode_uidgid(struct user_namespace *mnt_userns,
506 const struct inode *inode, int cap)
507{
508 struct user_namespace *ns = current_user_ns();
509
510 return ns_capable(ns, cap) &&
511 privileged_wrt_inode_uidgid(ns, mnt_userns, inode);
512}
513EXPORT_SYMBOL(capable_wrt_inode_uidgid);
514
515
516
517
518
519
520
521
522
523bool ptracer_capable(struct task_struct *tsk, struct user_namespace *ns)
524{
525 int ret = 0;
526 const struct cred *cred;
527
528 rcu_read_lock();
529 cred = rcu_dereference(tsk->ptracer_cred);
530 if (cred)
531 ret = security_capable(cred, ns, CAP_SYS_PTRACE,
532 CAP_OPT_NOAUDIT);
533 rcu_read_unlock();
534 return (ret == 0);
535}
536