diff options
author | Serge E. Hallyn <serge@hallyn.com> | 2011-03-23 19:43:17 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2011-03-23 22:47:02 -0400 |
commit | 3486740a4f32a6a466f5ac931654d154790ba648 (patch) | |
tree | ac5d968a66057fa84933b8f89fd3e916270dffed /kernel | |
parent | 59607db367c57f515183cb203642291bb14d9c40 (diff) |
userns: security: make capabilities relative to the user namespace
- Introduce ns_capable to test for a capability in a non-default
user namespace.
- Teach cap_capable to handle capabilities in a non-default
user namespace.
The motivation is to get to the unprivileged creation of new
namespaces. It looks like this gets us 90% of the way there, with
only potential uid confusion issues left.
I still need to handle getting all caps after creation but otherwise I
think I have a good starter patch that achieves all of your goals.
Changelog:
11/05/2010: [serge] add apparmor
12/14/2010: [serge] fix capabilities to created user namespaces
Without this, if user serge creates a user_ns, he won't have
capabilities to the user_ns he created. THis is because we
were first checking whether his effective caps had the caps
he needed and returning -EPERM if not, and THEN checking whether
he was the creator. Reverse those checks.
12/16/2010: [serge] security_real_capable needs ns argument in !security case
01/11/2011: [serge] add task_ns_capable helper
01/11/2011: [serge] add nsown_capable() helper per Bastian Blank suggestion
02/16/2011: [serge] fix a logic bug: the root user is always creator of
init_user_ns, but should not always have capabilities to
it! Fix the check in cap_capable().
02/21/2011: Add the required user_ns parameter to security_capable,
fixing a compile failure.
02/23/2011: Convert some macros to functions as per akpm comments. Some
couldn't be converted because we can't easily forward-declare
them (they are inline if !SECURITY, extern if SECURITY). Add
a current_user_ns function so we can use it in capability.h
without #including cred.h. Move all forward declarations
together to the top of the #ifdef __KERNEL__ section, and use
kernel-doc format.
02/23/2011: Per dhowells, clean up comment in cap_capable().
02/23/2011: Per akpm, remove unreachable 'return -EPERM' in cap_capable.
(Original written and signed off by Eric; latest, modified version
acked by him)
[akpm@linux-foundation.org: fix build]
[akpm@linux-foundation.org: export current_user_ns() for ecryptfs]
[serge.hallyn@canonical.com: remove unneeded extra argument in selinux's task_has_capability]
Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
Signed-off-by: Serge E. Hallyn <serge.hallyn@canonical.com>
Acked-by: "Eric W. Biederman" <ebiederm@xmission.com>
Acked-by: Daniel Lezcano <daniel.lezcano@free.fr>
Acked-by: David Howells <dhowells@redhat.com>
Cc: James Morris <jmorris@namei.org>
Signed-off-by: Serge E. Hallyn <serge.hallyn@canonical.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'kernel')
-rw-r--r-- | kernel/capability.c | 42 | ||||
-rw-r--r-- | kernel/cred.c | 6 |
2 files changed, 43 insertions, 5 deletions
diff --git a/kernel/capability.c b/kernel/capability.c index 9e9385f132c8..0a3d2c863a1c 100644 --- a/kernel/capability.c +++ b/kernel/capability.c | |||
@@ -14,6 +14,7 @@ | |||
14 | #include <linux/security.h> | 14 | #include <linux/security.h> |
15 | #include <linux/syscalls.h> | 15 | #include <linux/syscalls.h> |
16 | #include <linux/pid_namespace.h> | 16 | #include <linux/pid_namespace.h> |
17 | #include <linux/user_namespace.h> | ||
17 | #include <asm/uaccess.h> | 18 | #include <asm/uaccess.h> |
18 | 19 | ||
19 | /* | 20 | /* |
@@ -299,17 +300,48 @@ error: | |||
299 | * This sets PF_SUPERPRIV on the task if the capability is available on the | 300 | * This sets PF_SUPERPRIV on the task if the capability is available on the |
300 | * assumption that it's about to be used. | 301 | * assumption that it's about to be used. |
301 | */ | 302 | */ |
302 | int capable(int cap) | 303 | bool capable(int cap) |
304 | { | ||
305 | return ns_capable(&init_user_ns, cap); | ||
306 | } | ||
307 | EXPORT_SYMBOL(capable); | ||
308 | |||
309 | /** | ||
310 | * ns_capable - Determine if the current task has a superior capability in effect | ||
311 | * @ns: The usernamespace we want the capability in | ||
312 | * @cap: The capability to be tested for | ||
313 | * | ||
314 | * Return true if the current task has the given superior capability currently | ||
315 | * available for use, false if not. | ||
316 | * | ||
317 | * This sets PF_SUPERPRIV on the task if the capability is available on the | ||
318 | * assumption that it's about to be used. | ||
319 | */ | ||
320 | bool ns_capable(struct user_namespace *ns, int cap) | ||
303 | { | 321 | { |
304 | if (unlikely(!cap_valid(cap))) { | 322 | if (unlikely(!cap_valid(cap))) { |
305 | printk(KERN_CRIT "capable() called with invalid cap=%u\n", cap); | 323 | printk(KERN_CRIT "capable() called with invalid cap=%u\n", cap); |
306 | BUG(); | 324 | BUG(); |
307 | } | 325 | } |
308 | 326 | ||
309 | if (security_capable(current_cred(), cap) == 0) { | 327 | if (security_capable(ns, current_cred(), cap) == 0) { |
310 | current->flags |= PF_SUPERPRIV; | 328 | current->flags |= PF_SUPERPRIV; |
311 | return 1; | 329 | return true; |
312 | } | 330 | } |
313 | return 0; | 331 | return false; |
314 | } | 332 | } |
315 | EXPORT_SYMBOL(capable); | 333 | EXPORT_SYMBOL(ns_capable); |
334 | |||
335 | /** | ||
336 | * task_ns_capable - Determine whether current task has a superior | ||
337 | * capability targeted at a specific task's user namespace. | ||
338 | * @t: The task whose user namespace is targeted. | ||
339 | * @cap: The capability in question. | ||
340 | * | ||
341 | * Return true if it does, false otherwise. | ||
342 | */ | ||
343 | bool task_ns_capable(struct task_struct *t, int cap) | ||
344 | { | ||
345 | return ns_capable(task_cred_xxx(t, user)->user_ns, cap); | ||
346 | } | ||
347 | EXPORT_SYMBOL(task_ns_capable); | ||
diff --git a/kernel/cred.c b/kernel/cred.c index 2343c132c5a7..5557b55048df 100644 --- a/kernel/cred.c +++ b/kernel/cred.c | |||
@@ -741,6 +741,12 @@ int set_create_files_as(struct cred *new, struct inode *inode) | |||
741 | } | 741 | } |
742 | EXPORT_SYMBOL(set_create_files_as); | 742 | EXPORT_SYMBOL(set_create_files_as); |
743 | 743 | ||
744 | struct user_namespace *current_user_ns(void) | ||
745 | { | ||
746 | return _current_user_ns(); | ||
747 | } | ||
748 | EXPORT_SYMBOL(current_user_ns); | ||
749 | |||
744 | #ifdef CONFIG_DEBUG_CREDENTIALS | 750 | #ifdef CONFIG_DEBUG_CREDENTIALS |
745 | 751 | ||
746 | bool creds_are_invalid(const struct cred *cred) | 752 | bool creds_are_invalid(const struct cred *cred) |