Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/kernel/capability.c
26243 views
1
// SPDX-License-Identifier: GPL-2.0
2
/*
3
* linux/kernel/capability.c
4
*
5
* Copyright (C) 1997 Andrew Main <[email protected]>
6
*
7
* Integrated into 2.1.97+, Andrew G. Morgan <[email protected]>
8
* 30 May 2002: Cleanup, Robert M. Love <[email protected]>
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
int file_caps_enabled = 1;
24
25
static int __init file_caps_disable(char *str)
26
{
27
file_caps_enabled = 0;
28
return 1;
29
}
30
__setup("no_file_caps", file_caps_disable);
31
32
#ifdef CONFIG_MULTIUSER
33
/*
34
* More recent versions of libcap are available from:
35
*
36
* http://www.kernel.org/pub/linux/libs/security/linux-privs/
37
*/
38
39
static void warn_legacy_capability_use(void)
40
{
41
pr_info_once("warning: `%s' uses 32-bit capabilities (legacy support in use)\n",
42
current->comm);
43
}
44
45
/*
46
* Version 2 capabilities worked fine, but the linux/capability.h file
47
* that accompanied their introduction encouraged their use without
48
* the necessary user-space source code changes. As such, we have
49
* created a version 3 with equivalent functionality to version 2, but
50
* with a header change to protect legacy source code from using
51
* version 2 when it wanted to use version 1. If your system has code
52
* that trips the following warning, it is using version 2 specific
53
* capabilities and may be doing so insecurely.
54
*
55
* The remedy is to either upgrade your version of libcap (to 2.10+,
56
* if the application is linked against it), or recompile your
57
* application with modern kernel headers and this warning will go
58
* away.
59
*/
60
61
static void warn_deprecated_v2(void)
62
{
63
pr_info_once("warning: `%s' uses deprecated v2 capabilities in a way that may be insecure\n",
64
current->comm);
65
}
66
67
/*
68
* Version check. Return the number of u32s in each capability flag
69
* array, or a negative value on error.
70
*/
71
static int cap_validate_magic(cap_user_header_t header, unsigned *tocopy)
72
{
73
__u32 version;
74
75
if (get_user(version, &header->version))
76
return -EFAULT;
77
78
switch (version) {
79
case _LINUX_CAPABILITY_VERSION_1:
80
warn_legacy_capability_use();
81
*tocopy = _LINUX_CAPABILITY_U32S_1;
82
break;
83
case _LINUX_CAPABILITY_VERSION_2:
84
warn_deprecated_v2();
85
fallthrough; /* v3 is otherwise equivalent to v2 */
86
case _LINUX_CAPABILITY_VERSION_3:
87
*tocopy = _LINUX_CAPABILITY_U32S_3;
88
break;
89
default:
90
if (put_user((u32)_KERNEL_CAPABILITY_VERSION, &header->version))
91
return -EFAULT;
92
return -EINVAL;
93
}
94
95
return 0;
96
}
97
98
/*
99
* The only thing that can change the capabilities of the current
100
* process is the current process. As such, we can't be in this code
101
* at the same time as we are in the process of setting capabilities
102
* in this process. The net result is that we can limit our use of
103
* locks to when we are reading the caps of another process.
104
*/
105
static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp,
106
kernel_cap_t *pIp, kernel_cap_t *pPp)
107
{
108
int ret;
109
110
if (pid && (pid != task_pid_vnr(current))) {
111
const struct task_struct *target;
112
113
rcu_read_lock();
114
115
target = find_task_by_vpid(pid);
116
if (!target)
117
ret = -ESRCH;
118
else
119
ret = security_capget(target, pEp, pIp, pPp);
120
121
rcu_read_unlock();
122
} else
123
ret = security_capget(current, pEp, pIp, pPp);
124
125
return ret;
126
}
127
128
/**
129
* sys_capget - get the capabilities of a given process.
130
* @header: pointer to struct that contains capability version and
131
* target pid data
132
* @dataptr: pointer to struct that contains the effective, permitted,
133
* and inheritable capabilities that are returned
134
*
135
* Returns 0 on success and < 0 on error.
136
*/
137
SYSCALL_DEFINE2(capget, cap_user_header_t, header, cap_user_data_t, dataptr)
138
{
139
int ret = 0;
140
pid_t pid;
141
unsigned tocopy;
142
kernel_cap_t pE, pI, pP;
143
struct __user_cap_data_struct kdata[2];
144
145
ret = cap_validate_magic(header, &tocopy);
146
if ((dataptr == NULL) || (ret != 0))
147
return ((dataptr == NULL) && (ret == -EINVAL)) ? 0 : ret;
148
149
if (get_user(pid, &header->pid))
150
return -EFAULT;
151
152
if (pid < 0)
153
return -EINVAL;
154
155
ret = cap_get_target_pid(pid, &pE, &pI, &pP);
156
if (ret)
157
return ret;
158
159
/*
160
* Annoying legacy format with 64-bit capabilities exposed
161
* as two sets of 32-bit fields, so we need to split the
162
* capability values up.
163
*/
164
kdata[0].effective = pE.val; kdata[1].effective = pE.val >> 32;
165
kdata[0].permitted = pP.val; kdata[1].permitted = pP.val >> 32;
166
kdata[0].inheritable = pI.val; kdata[1].inheritable = pI.val >> 32;
167
168
/*
169
* Note, in the case, tocopy < _KERNEL_CAPABILITY_U32S,
170
* we silently drop the upper capabilities here. This
171
* has the effect of making older libcap
172
* implementations implicitly drop upper capability
173
* bits when they perform a: capget/modify/capset
174
* sequence.
175
*
176
* This behavior is considered fail-safe
177
* behavior. Upgrading the application to a newer
178
* version of libcap will enable access to the newer
179
* capabilities.
180
*
181
* An alternative would be to return an error here
182
* (-ERANGE), but that causes legacy applications to
183
* unexpectedly fail; the capget/modify/capset aborts
184
* before modification is attempted and the application
185
* fails.
186
*/
187
if (copy_to_user(dataptr, kdata, tocopy * sizeof(kdata[0])))
188
return -EFAULT;
189
190
return 0;
191
}
192
193
static kernel_cap_t mk_kernel_cap(u32 low, u32 high)
194
{
195
return (kernel_cap_t) { (low | ((u64)high << 32)) & CAP_VALID_MASK };
196
}
197
198
/**
199
* sys_capset - set capabilities for a process or (*) a group of processes
200
* @header: pointer to struct that contains capability version and
201
* target pid data
202
* @data: pointer to struct that contains the effective, permitted,
203
* and inheritable capabilities
204
*
205
* Set capabilities for the current process only. The ability to any other
206
* process(es) has been deprecated and removed.
207
*
208
* The restrictions on setting capabilities are specified as:
209
*
210
* I: any raised capabilities must be a subset of the old permitted
211
* P: any raised capabilities must be a subset of the old permitted
212
* E: must be set to a subset of new permitted
213
*
214
* Returns 0 on success and < 0 on error.
215
*/
216
SYSCALL_DEFINE2(capset, cap_user_header_t, header, const cap_user_data_t, data)
217
{
218
struct __user_cap_data_struct kdata[2] = { { 0, }, };
219
unsigned tocopy, copybytes;
220
kernel_cap_t inheritable, permitted, effective;
221
struct cred *new;
222
int ret;
223
pid_t pid;
224
225
ret = cap_validate_magic(header, &tocopy);
226
if (ret != 0)
227
return ret;
228
229
if (get_user(pid, &header->pid))
230
return -EFAULT;
231
232
/* may only affect current now */
233
if (pid != 0 && pid != task_pid_vnr(current))
234
return -EPERM;
235
236
copybytes = tocopy * sizeof(struct __user_cap_data_struct);
237
if (copybytes > sizeof(kdata))
238
return -EFAULT;
239
240
if (copy_from_user(&kdata, data, copybytes))
241
return -EFAULT;
242
243
effective = mk_kernel_cap(kdata[0].effective, kdata[1].effective);
244
permitted = mk_kernel_cap(kdata[0].permitted, kdata[1].permitted);
245
inheritable = mk_kernel_cap(kdata[0].inheritable, kdata[1].inheritable);
246
247
new = prepare_creds();
248
if (!new)
249
return -ENOMEM;
250
251
ret = security_capset(new, current_cred(),
252
&effective, &inheritable, &permitted);
253
if (ret < 0)
254
goto error;
255
256
audit_log_capset(new, current_cred());
257
258
return commit_creds(new);
259
260
error:
261
abort_creds(new);
262
return ret;
263
}
264
265
/**
266
* has_ns_capability - Does a task have a capability in a specific user ns
267
* @t: The task in question
268
* @ns: target user namespace
269
* @cap: The capability to be tested for
270
*
271
* Return true if the specified task has the given superior capability
272
* currently in effect to the specified user namespace, false if not.
273
*
274
* Note that this does not set PF_SUPERPRIV on the task.
275
*/
276
bool has_ns_capability(struct task_struct *t,
277
struct user_namespace *ns, int cap)
278
{
279
int ret;
280
281
rcu_read_lock();
282
ret = security_capable(__task_cred(t), ns, cap, CAP_OPT_NONE);
283
rcu_read_unlock();
284
285
return (ret == 0);
286
}
287
288
/**
289
* has_ns_capability_noaudit - Does a task have a capability (unaudited)
290
* in a specific user ns.
291
* @t: The task in question
292
* @ns: target user namespace
293
* @cap: The capability to be tested for
294
*
295
* Return true if the specified task has the given superior capability
296
* currently in effect to the specified user namespace, false if not.
297
* Do not write an audit message for the check.
298
*
299
* Note that this does not set PF_SUPERPRIV on the task.
300
*/
301
bool has_ns_capability_noaudit(struct task_struct *t,
302
struct user_namespace *ns, int cap)
303
{
304
int ret;
305
306
rcu_read_lock();
307
ret = security_capable(__task_cred(t), ns, cap, CAP_OPT_NOAUDIT);
308
rcu_read_unlock();
309
310
return (ret == 0);
311
}
312
313
/**
314
* has_capability_noaudit - Does a task have a capability (unaudited) in the
315
* initial user ns
316
* @t: The task in question
317
* @cap: The capability to be tested for
318
*
319
* Return true if the specified task has the given superior capability
320
* currently in effect to init_user_ns, false if not. Don't write an
321
* audit message for the check.
322
*
323
* Note that this does not set PF_SUPERPRIV on the task.
324
*/
325
bool has_capability_noaudit(struct task_struct *t, int cap)
326
{
327
return has_ns_capability_noaudit(t, &init_user_ns, cap);
328
}
329
EXPORT_SYMBOL(has_capability_noaudit);
330
331
static bool ns_capable_common(struct user_namespace *ns,
332
int cap,
333
unsigned int opts)
334
{
335
int capable;
336
337
if (unlikely(!cap_valid(cap))) {
338
pr_crit("capable() called with invalid cap=%u\n", cap);
339
BUG();
340
}
341
342
capable = security_capable(current_cred(), ns, cap, opts);
343
if (capable == 0) {
344
current->flags |= PF_SUPERPRIV;
345
return true;
346
}
347
return false;
348
}
349
350
/**
351
* ns_capable - Determine if the current task has a superior capability in effect
352
* @ns: The usernamespace we want the capability in
353
* @cap: The capability to be tested for
354
*
355
* Return true if the current task has the given superior capability currently
356
* available for use, false if not.
357
*
358
* This sets PF_SUPERPRIV on the task if the capability is available on the
359
* assumption that it's about to be used.
360
*/
361
bool ns_capable(struct user_namespace *ns, int cap)
362
{
363
return ns_capable_common(ns, cap, CAP_OPT_NONE);
364
}
365
EXPORT_SYMBOL(ns_capable);
366
367
/**
368
* ns_capable_noaudit - Determine if the current task has a superior capability
369
* (unaudited) in effect
370
* @ns: The usernamespace we want the capability in
371
* @cap: The capability to be tested for
372
*
373
* Return true if the current task has the given superior capability currently
374
* available for use, false if not.
375
*
376
* This sets PF_SUPERPRIV on the task if the capability is available on the
377
* assumption that it's about to be used.
378
*/
379
bool ns_capable_noaudit(struct user_namespace *ns, int cap)
380
{
381
return ns_capable_common(ns, cap, CAP_OPT_NOAUDIT);
382
}
383
EXPORT_SYMBOL(ns_capable_noaudit);
384
385
/**
386
* ns_capable_setid - Determine if the current task has a superior capability
387
* in effect, while signalling that this check is being done from within a
388
* setid or setgroups syscall.
389
* @ns: The usernamespace we want the capability in
390
* @cap: The capability to be tested for
391
*
392
* Return true if the current task has the given superior capability currently
393
* available for use, false if not.
394
*
395
* This sets PF_SUPERPRIV on the task if the capability is available on the
396
* assumption that it's about to be used.
397
*/
398
bool ns_capable_setid(struct user_namespace *ns, int cap)
399
{
400
return ns_capable_common(ns, cap, CAP_OPT_INSETID);
401
}
402
EXPORT_SYMBOL(ns_capable_setid);
403
404
/**
405
* capable - Determine if the current task has a superior capability in effect
406
* @cap: The capability to be tested for
407
*
408
* Return true if the current task has the given superior capability currently
409
* available for use, false if not.
410
*
411
* This sets PF_SUPERPRIV on the task if the capability is available on the
412
* assumption that it's about to be used.
413
*/
414
bool capable(int cap)
415
{
416
return ns_capable(&init_user_ns, cap);
417
}
418
EXPORT_SYMBOL(capable);
419
#endif /* CONFIG_MULTIUSER */
420
421
/**
422
* file_ns_capable - Determine if the file's opener had a capability in effect
423
* @file: The file we want to check
424
* @ns: The usernamespace we want the capability in
425
* @cap: The capability to be tested for
426
*
427
* Return true if task that opened the file had a capability in effect
428
* when the file was opened.
429
*
430
* This does not set PF_SUPERPRIV because the caller may not
431
* actually be privileged.
432
*/
433
bool file_ns_capable(const struct file *file, struct user_namespace *ns,
434
int cap)
435
{
436
437
if (WARN_ON_ONCE(!cap_valid(cap)))
438
return false;
439
440
if (security_capable(file->f_cred, ns, cap, CAP_OPT_NONE) == 0)
441
return true;
442
443
return false;
444
}
445
EXPORT_SYMBOL(file_ns_capable);
446
447
/**
448
* privileged_wrt_inode_uidgid - Do capabilities in the namespace work over the inode?
449
* @ns: The user namespace in question
450
* @idmap: idmap of the mount @inode was found from
451
* @inode: The inode in question
452
*
453
* Return true if the inode uid and gid are within the namespace.
454
*/
455
bool privileged_wrt_inode_uidgid(struct user_namespace *ns,
456
struct mnt_idmap *idmap,
457
const struct inode *inode)
458
{
459
return vfsuid_has_mapping(ns, i_uid_into_vfsuid(idmap, inode)) &&
460
vfsgid_has_mapping(ns, i_gid_into_vfsgid(idmap, inode));
461
}
462
463
/**
464
* capable_wrt_inode_uidgid - Check nsown_capable and uid and gid mapped
465
* @idmap: idmap of the mount @inode was found from
466
* @inode: The inode in question
467
* @cap: The capability in question
468
*
469
* Return true if the current task has the given capability targeted at
470
* its own user namespace and that the given inode's uid and gid are
471
* mapped into the current user namespace.
472
*/
473
bool capable_wrt_inode_uidgid(struct mnt_idmap *idmap,
474
const struct inode *inode, int cap)
475
{
476
struct user_namespace *ns = current_user_ns();
477
478
return ns_capable(ns, cap) &&
479
privileged_wrt_inode_uidgid(ns, idmap, inode);
480
}
481
EXPORT_SYMBOL(capable_wrt_inode_uidgid);
482
483
/**
484
* ptracer_capable - Determine if the ptracer holds CAP_SYS_PTRACE in the namespace
485
* @tsk: The task that may be ptraced
486
* @ns: The user namespace to search for CAP_SYS_PTRACE in
487
*
488
* Return true if the task that is ptracing the current task had CAP_SYS_PTRACE
489
* in the specified user namespace.
490
*/
491
bool ptracer_capable(struct task_struct *tsk, struct user_namespace *ns)
492
{
493
int ret = 0; /* An absent tracer adds no restrictions */
494
const struct cred *cred;
495
496
rcu_read_lock();
497
cred = rcu_dereference(tsk->ptracer_cred);
498
if (cred)
499
ret = security_capable(cred, ns, CAP_SYS_PTRACE,
500
CAP_OPT_NOAUDIT);
501
rcu_read_unlock();
502
return (ret == 0);
503
}
504
505