2 * linux/kernel/capability.c
4 * Copyright (C) 1997 Andrew Main <zefram@fysh.org>
6 * Integrated into 2.1.97+, Andrew G. Morgan <morgan@kernel.org>
7 * 30 May 2002: Cleanup, Robert M. Love <rml@tech9.net>
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/audit.h>
13 #include <linux/capability.h>
15 #include <linux/export.h>
16 #include <linux/security.h>
17 #include <linux/syscalls.h>
18 #include <linux/pid_namespace.h>
19 #include <linux/user_namespace.h>
20 #include <asm/uaccess.h>
23 #include <linux/linux_on_dex.h>
27 * Leveraged for setting/resetting capabilities
30 const kernel_cap_t __cap_empty_set
= CAP_EMPTY_SET
;
31 EXPORT_SYMBOL(__cap_empty_set
);
33 int file_caps_enabled
= 1;
35 static int __init
file_caps_disable(char *str
)
37 file_caps_enabled
= 0;
40 __setup("no_file_caps", file_caps_disable
);
42 #ifdef CONFIG_MULTIUSER
44 * More recent versions of libcap are available from:
46 * http://www.kernel.org/pub/linux/libs/security/linux-privs/
49 static void warn_legacy_capability_use(void)
51 char name
[sizeof(current
->comm
)];
53 pr_info_once("warning: `%s' uses 32-bit capabilities (legacy support in use)\n",
54 get_task_comm(name
, current
));
58 * Version 2 capabilities worked fine, but the linux/capability.h file
59 * that accompanied their introduction encouraged their use without
60 * the necessary user-space source code changes. As such, we have
61 * created a version 3 with equivalent functionality to version 2, but
62 * with a header change to protect legacy source code from using
63 * version 2 when it wanted to use version 1. If your system has code
64 * that trips the following warning, it is using version 2 specific
65 * capabilities and may be doing so insecurely.
67 * The remedy is to either upgrade your version of libcap (to 2.10+,
68 * if the application is linked against it), or recompile your
69 * application with modern kernel headers and this warning will go
73 static void warn_deprecated_v2(void)
75 char name
[sizeof(current
->comm
)];
77 pr_info_once("warning: `%s' uses deprecated v2 capabilities in a way that may be insecure\n",
78 get_task_comm(name
, current
));
82 * Version check. Return the number of u32s in each capability flag
83 * array, or a negative value on error.
85 static int cap_validate_magic(cap_user_header_t header
, unsigned *tocopy
)
89 if (get_user(version
, &header
->version
))
93 case _LINUX_CAPABILITY_VERSION_1
:
94 warn_legacy_capability_use();
95 *tocopy
= _LINUX_CAPABILITY_U32S_1
;
97 case _LINUX_CAPABILITY_VERSION_2
:
100 * fall through - v3 is otherwise equivalent to v2.
102 case _LINUX_CAPABILITY_VERSION_3
:
103 *tocopy
= _LINUX_CAPABILITY_U32S_3
;
106 if (put_user((u32
)_KERNEL_CAPABILITY_VERSION
, &header
->version
))
115 * The only thing that can change the capabilities of the current
116 * process is the current process. As such, we can't be in this code
117 * at the same time as we are in the process of setting capabilities
118 * in this process. The net result is that we can limit our use of
119 * locks to when we are reading the caps of another process.
121 static inline int cap_get_target_pid(pid_t pid
, kernel_cap_t
*pEp
,
122 kernel_cap_t
*pIp
, kernel_cap_t
*pPp
)
126 if (pid
&& (pid
!= task_pid_vnr(current
))) {
127 struct task_struct
*target
;
131 target
= find_task_by_vpid(pid
);
135 ret
= security_capget(target
, pEp
, pIp
, pPp
);
139 ret
= security_capget(current
, pEp
, pIp
, pPp
);
145 * sys_capget - get the capabilities of a given process.
146 * @header: pointer to struct that contains capability version and
148 * @dataptr: pointer to struct that contains the effective, permitted,
149 * and inheritable capabilities that are returned
151 * Returns 0 on success and < 0 on error.
153 SYSCALL_DEFINE2(capget
, cap_user_header_t
, header
, cap_user_data_t
, dataptr
)
158 kernel_cap_t pE
, pI
, pP
;
160 ret
= cap_validate_magic(header
, &tocopy
);
161 if ((dataptr
== NULL
) || (ret
!= 0))
162 return ((dataptr
== NULL
) && (ret
== -EINVAL
)) ? 0 : ret
;
164 if (get_user(pid
, &header
->pid
))
170 ret
= cap_get_target_pid(pid
, &pE
, &pI
, &pP
);
172 struct __user_cap_data_struct kdata
[_KERNEL_CAPABILITY_U32S
];
175 for (i
= 0; i
< tocopy
; i
++) {
176 kdata
[i
].effective
= pE
.cap
[i
];
177 kdata
[i
].permitted
= pP
.cap
[i
];
178 kdata
[i
].inheritable
= pI
.cap
[i
];
182 * Note, in the case, tocopy < _KERNEL_CAPABILITY_U32S,
183 * we silently drop the upper capabilities here. This
184 * has the effect of making older libcap
185 * implementations implicitly drop upper capability
186 * bits when they perform a: capget/modify/capset
189 * This behavior is considered fail-safe
190 * behavior. Upgrading the application to a newer
191 * version of libcap will enable access to the newer
194 * An alternative would be to return an error here
195 * (-ERANGE), but that causes legacy applications to
196 * unexpectedly fail; the capget/modify/capset aborts
197 * before modification is attempted and the application
200 if (copy_to_user(dataptr
, kdata
, tocopy
201 * sizeof(struct __user_cap_data_struct
))) {
210 * sys_capset - set capabilities for a process or (*) a group of processes
211 * @header: pointer to struct that contains capability version and
213 * @data: pointer to struct that contains the effective, permitted,
214 * and inheritable capabilities
216 * Set capabilities for the current process only. The ability to any other
217 * process(es) has been deprecated and removed.
219 * The restrictions on setting capabilities are specified as:
221 * I: any raised capabilities must be a subset of the old permitted
222 * P: any raised capabilities must be a subset of the old permitted
223 * E: must be set to a subset of new permitted
225 * Returns 0 on success and < 0 on error.
227 SYSCALL_DEFINE2(capset
, cap_user_header_t
, header
, const cap_user_data_t
, data
)
229 struct __user_cap_data_struct kdata
[_KERNEL_CAPABILITY_U32S
];
230 unsigned i
, tocopy
, copybytes
;
231 kernel_cap_t inheritable
, permitted
, effective
;
236 ret
= cap_validate_magic(header
, &tocopy
);
240 if (get_user(pid
, &header
->pid
))
243 /* may only affect current now */
244 if (pid
!= 0 && pid
!= task_pid_vnr(current
))
247 copybytes
= tocopy
* sizeof(struct __user_cap_data_struct
);
248 if (copybytes
> sizeof(kdata
))
251 if (copy_from_user(&kdata
, data
, copybytes
))
254 for (i
= 0; i
< tocopy
; i
++) {
255 effective
.cap
[i
] = kdata
[i
].effective
;
256 permitted
.cap
[i
] = kdata
[i
].permitted
;
257 inheritable
.cap
[i
] = kdata
[i
].inheritable
;
259 while (i
< _KERNEL_CAPABILITY_U32S
) {
260 effective
.cap
[i
] = 0;
261 permitted
.cap
[i
] = 0;
262 inheritable
.cap
[i
] = 0;
266 effective
.cap
[CAP_LAST_U32
] &= CAP_LAST_U32_VALID_MASK
;
267 permitted
.cap
[CAP_LAST_U32
] &= CAP_LAST_U32_VALID_MASK
;
268 inheritable
.cap
[CAP_LAST_U32
] &= CAP_LAST_U32_VALID_MASK
;
270 new = prepare_creds();
274 ret
= security_capset(new, current_cred(),
275 &effective
, &inheritable
, &permitted
);
279 audit_log_capset(new, current_cred());
281 return commit_creds(new);
289 * has_ns_capability - Does a task have a capability in a specific user ns
290 * @t: The task in question
291 * @ns: target user namespace
292 * @cap: The capability to be tested for
294 * Return true if the specified task has the given superior capability
295 * currently in effect to the specified user namespace, false if not.
297 * Note that this does not set PF_SUPERPRIV on the task.
299 bool has_ns_capability(struct task_struct
*t
,
300 struct user_namespace
*ns
, int cap
)
305 ret
= security_capable(__task_cred(t
), ns
, cap
);
312 * has_capability - Does a task have a capability in init_user_ns
313 * @t: The task in question
314 * @cap: The capability to be tested for
316 * Return true if the specified task has the given superior capability
317 * currently in effect to the initial user namespace, false if not.
319 * Note that this does not set PF_SUPERPRIV on the task.
321 bool has_capability(struct task_struct
*t
, int cap
)
323 return has_ns_capability(t
, &init_user_ns
, cap
);
327 * has_ns_capability_noaudit - Does a task have a capability (unaudited)
328 * in a specific user ns.
329 * @t: The task in question
330 * @ns: target user namespace
331 * @cap: The capability to be tested for
333 * Return true if the specified task has the given superior capability
334 * currently in effect to the specified user namespace, false if not.
335 * Do not write an audit message for the check.
337 * Note that this does not set PF_SUPERPRIV on the task.
339 bool has_ns_capability_noaudit(struct task_struct
*t
,
340 struct user_namespace
*ns
, int cap
)
345 ret
= security_capable_noaudit(__task_cred(t
), ns
, cap
);
352 * has_capability_noaudit - Does a task have a capability (unaudited) in the
354 * @t: The task in question
355 * @cap: The capability to be tested for
357 * Return true if the specified task has the given superior capability
358 * currently in effect to init_user_ns, false if not. Don't write an
359 * audit message for the check.
361 * Note that this does not set PF_SUPERPRIV on the task.
363 bool has_capability_noaudit(struct task_struct
*t
, int cap
)
365 return has_ns_capability_noaudit(t
, &init_user_ns
, cap
);
368 static bool ns_capable_common(struct user_namespace
*ns
, int cap
, bool audit
)
372 if (unlikely(!cap_valid(cap
))) {
373 pr_crit("capable() called with invalid cap=%u\n", cap
);
377 capable
= audit
? security_capable(current_cred(), ns
, cap
) :
378 security_capable_noaudit(current_cred(), ns
, cap
);
380 current
->flags
|= PF_SUPERPRIV
;
387 * ns_capable - Determine if the current task has a superior capability in effect
388 * @ns: The usernamespace we want the capability in
389 * @cap: The capability to be tested for
391 * Return true if the current task has the given superior capability currently
392 * available for use, false if not.
394 * This sets PF_SUPERPRIV on the task if the capability is available on the
395 * assumption that it's about to be used.
397 bool ns_capable(struct user_namespace
*ns
, int cap
)
399 return ns_capable_common(ns
, cap
, true);
401 EXPORT_SYMBOL(ns_capable
);
404 * ns_capable_noaudit - Determine if the current task has a superior capability
405 * (unaudited) in effect
406 * @ns: The usernamespace we want the capability in
407 * @cap: The capability to be tested for
409 * Return true if the current task has the given superior capability currently
410 * available for use, false if not.
412 * This sets PF_SUPERPRIV on the task if the capability is available on the
413 * assumption that it's about to be used.
415 bool ns_capable_noaudit(struct user_namespace
*ns
, int cap
)
417 return ns_capable_common(ns
, cap
, false);
419 EXPORT_SYMBOL(ns_capable_noaudit
);
422 * capable - Determine if the current task has a superior capability in effect
423 * @cap: The capability to be tested for
425 * Return true if the current task has the given superior capability currently
426 * available for use, false if not.
428 * This sets PF_SUPERPRIV on the task if the capability is available on the
429 * assumption that it's about to be used.
431 bool capable(int cap
)
433 return ns_capable(&init_user_ns
, cap
);
435 EXPORT_SYMBOL(capable
);
436 #endif /* CONFIG_MULTIUSER */
439 * file_ns_capable - Determine if the file's opener had a capability in effect
440 * @file: The file we want to check
441 * @ns: The usernamespace we want the capability in
442 * @cap: The capability to be tested for
444 * Return true if task that opened the file had a capability in effect
445 * when the file was opened.
447 * This does not set PF_SUPERPRIV because the caller may not
448 * actually be privileged.
450 bool file_ns_capable(const struct file
*file
, struct user_namespace
*ns
,
453 if (WARN_ON_ONCE(!cap_valid(cap
)))
456 if (security_capable(file
->f_cred
, ns
, cap
) == 0)
461 EXPORT_SYMBOL(file_ns_capable
);
464 * privileged_wrt_inode_uidgid - Do capabilities in the namespace work over the inode?
465 * @ns: The user namespace in question
466 * @inode: The inode in question
468 * Return true if the inode uid and gid are within the namespace.
470 bool privileged_wrt_inode_uidgid(struct user_namespace
*ns
, const struct inode
*inode
)
472 return kuid_has_mapping(ns
, inode
->i_uid
) &&
473 kgid_has_mapping(ns
, inode
->i_gid
);
477 * capable_wrt_inode_uidgid - Check nsown_capable and uid and gid mapped
478 * @inode: The inode in question
479 * @cap: The capability in question
481 * Return true if the current task has the given capability targeted at
482 * its own user namespace and that the given inode's uid and gid are
483 * mapped into the current user namespace.
485 bool capable_wrt_inode_uidgid(const struct inode
*inode
, int cap
)
487 struct user_namespace
*ns
= current_user_ns();
489 //Never allow LOD container process access outside inodes
490 #ifdef CONFIG_LOD_SEC
491 if (current_is_LOD() && (!inode_is_LOD(inode
))){
492 #ifndef CONFIG_SAMSUNG_PRODUCT_SHIP
493 printk(KERN_ERR
"LOD capable_wrt_inode_uidgid: blocking CAP %d of PROC %s "
494 "PID %d PROC_UID %d INODE_UID %d\n", cap
, current
->comm
, current
->pid
,
495 current_cred()->uid
.val
, inode
->i_uid
.val
);
501 return ns_capable(ns
, cap
) && privileged_wrt_inode_uidgid(ns
, inode
);
503 EXPORT_SYMBOL(capable_wrt_inode_uidgid
);
506 * ptracer_capable - Determine if the ptracer holds CAP_SYS_PTRACE in the namespace
507 * @tsk: The task that may be ptraced
508 * @ns: The user namespace to search for CAP_SYS_PTRACE in
510 * Return true if the task that is ptracing the current task had CAP_SYS_PTRACE
511 * in the specified user namespace.
513 bool ptracer_capable(struct task_struct
*tsk
, struct user_namespace
*ns
)
515 int ret
= 0; /* An absent tracer adds no restrictions */
516 const struct cred
*cred
;
518 cred
= rcu_dereference(tsk
->ptracer_cred
);
520 ret
= security_capable_noaudit(cred
, ns
, CAP_SYS_PTRACE
);