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 | |
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>
-rw-r--r-- | drivers/pci/pci-sysfs.c | 2 | ||||
-rw-r--r-- | include/linux/capability.h | 36 | ||||
-rw-r--r-- | include/linux/cred.h | 4 | ||||
-rw-r--r-- | include/linux/security.h | 28 | ||||
-rw-r--r-- | kernel/capability.c | 42 | ||||
-rw-r--r-- | kernel/cred.c | 6 | ||||
-rw-r--r-- | security/apparmor/lsm.c | 5 | ||||
-rw-r--r-- | security/commoncap.c | 38 | ||||
-rw-r--r-- | security/security.c | 16 | ||||
-rw-r--r-- | security/selinux/hooks.c | 13 |
10 files changed, 144 insertions, 46 deletions
diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index c85438a367d..a8a277a2e0d 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c | |||
@@ -369,7 +369,7 @@ pci_read_config(struct file *filp, struct kobject *kobj, | |||
369 | u8 *data = (u8*) buf; | 369 | u8 *data = (u8*) buf; |
370 | 370 | ||
371 | /* Several chips lock up trying to read undefined config space */ | 371 | /* Several chips lock up trying to read undefined config space */ |
372 | if (security_capable(filp->f_cred, CAP_SYS_ADMIN) == 0) { | 372 | if (security_capable(&init_user_ns, filp->f_cred, CAP_SYS_ADMIN) == 0) { |
373 | size = dev->cfg_size; | 373 | size = dev->cfg_size; |
374 | } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) { | 374 | } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) { |
375 | size = 128; | 375 | size = 128; |
diff --git a/include/linux/capability.h b/include/linux/capability.h index fb16a3699b9..7c9c8290301 100644 --- a/include/linux/capability.h +++ b/include/linux/capability.h | |||
@@ -368,6 +368,17 @@ struct cpu_vfs_cap_data { | |||
368 | 368 | ||
369 | #ifdef __KERNEL__ | 369 | #ifdef __KERNEL__ |
370 | 370 | ||
371 | struct dentry; | ||
372 | struct user_namespace; | ||
373 | |||
374 | extern struct user_namespace init_user_ns; | ||
375 | |||
376 | struct user_namespace *current_user_ns(void); | ||
377 | |||
378 | extern const kernel_cap_t __cap_empty_set; | ||
379 | extern const kernel_cap_t __cap_full_set; | ||
380 | extern const kernel_cap_t __cap_init_eff_set; | ||
381 | |||
371 | /* | 382 | /* |
372 | * Internal kernel functions only | 383 | * Internal kernel functions only |
373 | */ | 384 | */ |
@@ -530,10 +541,6 @@ static inline kernel_cap_t cap_raise_nfsd_set(const kernel_cap_t a, | |||
530 | cap_intersect(permitted, __cap_nfsd_set)); | 541 | cap_intersect(permitted, __cap_nfsd_set)); |
531 | } | 542 | } |
532 | 543 | ||
533 | extern const kernel_cap_t __cap_empty_set; | ||
534 | extern const kernel_cap_t __cap_full_set; | ||
535 | extern const kernel_cap_t __cap_init_eff_set; | ||
536 | |||
537 | /** | 544 | /** |
538 | * has_capability - Determine if a task has a superior capability available | 545 | * has_capability - Determine if a task has a superior capability available |
539 | * @t: The task in question | 546 | * @t: The task in question |
@@ -544,7 +551,7 @@ extern const kernel_cap_t __cap_init_eff_set; | |||
544 | * | 551 | * |
545 | * Note that this does not set PF_SUPERPRIV on the task. | 552 | * Note that this does not set PF_SUPERPRIV on the task. |
546 | */ | 553 | */ |
547 | #define has_capability(t, cap) (security_real_capable((t), (cap)) == 0) | 554 | #define has_capability(t, cap) (security_real_capable((t), &init_user_ns, (cap)) == 0) |
548 | 555 | ||
549 | /** | 556 | /** |
550 | * has_capability_noaudit - Determine if a task has a superior capability available (unaudited) | 557 | * has_capability_noaudit - Determine if a task has a superior capability available (unaudited) |
@@ -558,12 +565,25 @@ extern const kernel_cap_t __cap_init_eff_set; | |||
558 | * Note that this does not set PF_SUPERPRIV on the task. | 565 | * Note that this does not set PF_SUPERPRIV on the task. |
559 | */ | 566 | */ |
560 | #define has_capability_noaudit(t, cap) \ | 567 | #define has_capability_noaudit(t, cap) \ |
561 | (security_real_capable_noaudit((t), (cap)) == 0) | 568 | (security_real_capable_noaudit((t), &init_user_ns, (cap)) == 0) |
562 | 569 | ||
563 | extern int capable(int cap); | 570 | extern bool capable(int cap); |
571 | extern bool ns_capable(struct user_namespace *ns, int cap); | ||
572 | extern bool task_ns_capable(struct task_struct *t, int cap); | ||
573 | |||
574 | /** | ||
575 | * nsown_capable - Check superior capability to one's own user_ns | ||
576 | * @cap: The capability in question | ||
577 | * | ||
578 | * Return true if the current task has the given superior capability | ||
579 | * targeted at its own user namespace. | ||
580 | */ | ||
581 | static inline bool nsown_capable(int cap) | ||
582 | { | ||
583 | return ns_capable(current_user_ns(), cap); | ||
584 | } | ||
564 | 585 | ||
565 | /* audit system wants to get cap info from files as well */ | 586 | /* audit system wants to get cap info from files as well */ |
566 | struct dentry; | ||
567 | extern int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps); | 587 | extern int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps); |
568 | 588 | ||
569 | #endif /* __KERNEL__ */ | 589 | #endif /* __KERNEL__ */ |
diff --git a/include/linux/cred.h b/include/linux/cred.h index 4aaeab37644..9aeeb0ba200 100644 --- a/include/linux/cred.h +++ b/include/linux/cred.h | |||
@@ -354,9 +354,11 @@ static inline void put_cred(const struct cred *_cred) | |||
354 | #define current_fsgid() (current_cred_xxx(fsgid)) | 354 | #define current_fsgid() (current_cred_xxx(fsgid)) |
355 | #define current_cap() (current_cred_xxx(cap_effective)) | 355 | #define current_cap() (current_cred_xxx(cap_effective)) |
356 | #define current_user() (current_cred_xxx(user)) | 356 | #define current_user() (current_cred_xxx(user)) |
357 | #define current_user_ns() (current_cred_xxx(user)->user_ns) | 357 | #define _current_user_ns() (current_cred_xxx(user)->user_ns) |
358 | #define current_security() (current_cred_xxx(security)) | 358 | #define current_security() (current_cred_xxx(security)) |
359 | 359 | ||
360 | extern struct user_namespace *current_user_ns(void); | ||
361 | |||
360 | #define current_uid_gid(_uid, _gid) \ | 362 | #define current_uid_gid(_uid, _gid) \ |
361 | do { \ | 363 | do { \ |
362 | const struct cred *__cred; \ | 364 | const struct cred *__cred; \ |
diff --git a/include/linux/security.h b/include/linux/security.h index 56cac520d01..ca02f171673 100644 --- a/include/linux/security.h +++ b/include/linux/security.h | |||
@@ -47,13 +47,14 @@ | |||
47 | 47 | ||
48 | struct ctl_table; | 48 | struct ctl_table; |
49 | struct audit_krule; | 49 | struct audit_krule; |
50 | struct user_namespace; | ||
50 | 51 | ||
51 | /* | 52 | /* |
52 | * These functions are in security/capability.c and are used | 53 | * These functions are in security/capability.c and are used |
53 | * as the default capabilities functions | 54 | * as the default capabilities functions |
54 | */ | 55 | */ |
55 | extern int cap_capable(struct task_struct *tsk, const struct cred *cred, | 56 | extern int cap_capable(struct task_struct *tsk, const struct cred *cred, |
56 | int cap, int audit); | 57 | struct user_namespace *ns, int cap, int audit); |
57 | extern int cap_settime(const struct timespec *ts, const struct timezone *tz); | 58 | extern int cap_settime(const struct timespec *ts, const struct timezone *tz); |
58 | extern int cap_ptrace_access_check(struct task_struct *child, unsigned int mode); | 59 | extern int cap_ptrace_access_check(struct task_struct *child, unsigned int mode); |
59 | extern int cap_ptrace_traceme(struct task_struct *parent); | 60 | extern int cap_ptrace_traceme(struct task_struct *parent); |
@@ -1262,6 +1263,7 @@ static inline void security_free_mnt_opts(struct security_mnt_opts *opts) | |||
1262 | * credentials. | 1263 | * credentials. |
1263 | * @tsk contains the task_struct for the process. | 1264 | * @tsk contains the task_struct for the process. |
1264 | * @cred contains the credentials to use. | 1265 | * @cred contains the credentials to use. |
1266 | * @ns contains the user namespace we want the capability in | ||
1265 | * @cap contains the capability <include/linux/capability.h>. | 1267 | * @cap contains the capability <include/linux/capability.h>. |
1266 | * @audit: Whether to write an audit message or not | 1268 | * @audit: Whether to write an audit message or not |
1267 | * Return 0 if the capability is granted for @tsk. | 1269 | * Return 0 if the capability is granted for @tsk. |
@@ -1384,7 +1386,7 @@ struct security_operations { | |||
1384 | const kernel_cap_t *inheritable, | 1386 | const kernel_cap_t *inheritable, |
1385 | const kernel_cap_t *permitted); | 1387 | const kernel_cap_t *permitted); |
1386 | int (*capable) (struct task_struct *tsk, const struct cred *cred, | 1388 | int (*capable) (struct task_struct *tsk, const struct cred *cred, |
1387 | int cap, int audit); | 1389 | struct user_namespace *ns, int cap, int audit); |
1388 | int (*quotactl) (int cmds, int type, int id, struct super_block *sb); | 1390 | int (*quotactl) (int cmds, int type, int id, struct super_block *sb); |
1389 | int (*quota_on) (struct dentry *dentry); | 1391 | int (*quota_on) (struct dentry *dentry); |
1390 | int (*syslog) (int type); | 1392 | int (*syslog) (int type); |
@@ -1665,9 +1667,12 @@ int security_capset(struct cred *new, const struct cred *old, | |||
1665 | const kernel_cap_t *effective, | 1667 | const kernel_cap_t *effective, |
1666 | const kernel_cap_t *inheritable, | 1668 | const kernel_cap_t *inheritable, |
1667 | const kernel_cap_t *permitted); | 1669 | const kernel_cap_t *permitted); |
1668 | int security_capable(const struct cred *cred, int cap); | 1670 | int security_capable(struct user_namespace *ns, const struct cred *cred, |
1669 | int security_real_capable(struct task_struct *tsk, int cap); | 1671 | int cap); |
1670 | int security_real_capable_noaudit(struct task_struct *tsk, int cap); | 1672 | int security_real_capable(struct task_struct *tsk, struct user_namespace *ns, |
1673 | int cap); | ||
1674 | int security_real_capable_noaudit(struct task_struct *tsk, | ||
1675 | struct user_namespace *ns, int cap); | ||
1671 | int security_quotactl(int cmds, int type, int id, struct super_block *sb); | 1676 | int security_quotactl(int cmds, int type, int id, struct super_block *sb); |
1672 | int security_quota_on(struct dentry *dentry); | 1677 | int security_quota_on(struct dentry *dentry); |
1673 | int security_syslog(int type); | 1678 | int security_syslog(int type); |
@@ -1860,28 +1865,29 @@ static inline int security_capset(struct cred *new, | |||
1860 | return cap_capset(new, old, effective, inheritable, permitted); | 1865 | return cap_capset(new, old, effective, inheritable, permitted); |
1861 | } | 1866 | } |
1862 | 1867 | ||
1863 | static inline int security_capable(const struct cred *cred, int cap) | 1868 | static inline int security_capable(struct user_namespace *ns, |
1869 | const struct cred *cred, int cap) | ||
1864 | { | 1870 | { |
1865 | return cap_capable(current, cred, cap, SECURITY_CAP_AUDIT); | 1871 | return cap_capable(current, cred, ns, cap, SECURITY_CAP_AUDIT); |
1866 | } | 1872 | } |
1867 | 1873 | ||
1868 | static inline int security_real_capable(struct task_struct *tsk, int cap) | 1874 | static inline int security_real_capable(struct task_struct *tsk, struct user_namespace *ns, int cap) |
1869 | { | 1875 | { |
1870 | int ret; | 1876 | int ret; |
1871 | 1877 | ||
1872 | rcu_read_lock(); | 1878 | rcu_read_lock(); |
1873 | ret = cap_capable(tsk, __task_cred(tsk), cap, SECURITY_CAP_AUDIT); | 1879 | ret = cap_capable(tsk, __task_cred(tsk), ns, cap, SECURITY_CAP_AUDIT); |
1874 | rcu_read_unlock(); | 1880 | rcu_read_unlock(); |
1875 | return ret; | 1881 | return ret; |
1876 | } | 1882 | } |
1877 | 1883 | ||
1878 | static inline | 1884 | static inline |
1879 | int security_real_capable_noaudit(struct task_struct *tsk, int cap) | 1885 | int security_real_capable_noaudit(struct task_struct *tsk, struct user_namespace *ns, int cap) |
1880 | { | 1886 | { |
1881 | int ret; | 1887 | int ret; |
1882 | 1888 | ||
1883 | rcu_read_lock(); | 1889 | rcu_read_lock(); |
1884 | ret = cap_capable(tsk, __task_cred(tsk), cap, | 1890 | ret = cap_capable(tsk, __task_cred(tsk), ns, cap, |
1885 | SECURITY_CAP_NOAUDIT); | 1891 | SECURITY_CAP_NOAUDIT); |
1886 | rcu_read_unlock(); | 1892 | rcu_read_unlock(); |
1887 | return ret; | 1893 | return ret; |
diff --git a/kernel/capability.c b/kernel/capability.c index 9e9385f132c..0a3d2c863a1 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 2343c132c5a..5557b55048d 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) |
diff --git a/security/apparmor/lsm.c b/security/apparmor/lsm.c index d21a427a35a..ae3a698415e 100644 --- a/security/apparmor/lsm.c +++ b/security/apparmor/lsm.c | |||
@@ -22,6 +22,7 @@ | |||
22 | #include <linux/ctype.h> | 22 | #include <linux/ctype.h> |
23 | #include <linux/sysctl.h> | 23 | #include <linux/sysctl.h> |
24 | #include <linux/audit.h> | 24 | #include <linux/audit.h> |
25 | #include <linux/user_namespace.h> | ||
25 | #include <net/sock.h> | 26 | #include <net/sock.h> |
26 | 27 | ||
27 | #include "include/apparmor.h" | 28 | #include "include/apparmor.h" |
@@ -136,11 +137,11 @@ static int apparmor_capget(struct task_struct *target, kernel_cap_t *effective, | |||
136 | } | 137 | } |
137 | 138 | ||
138 | static int apparmor_capable(struct task_struct *task, const struct cred *cred, | 139 | static int apparmor_capable(struct task_struct *task, const struct cred *cred, |
139 | int cap, int audit) | 140 | struct user_namespace *ns, int cap, int audit) |
140 | { | 141 | { |
141 | struct aa_profile *profile; | 142 | struct aa_profile *profile; |
142 | /* cap_capable returns 0 on success, else -EPERM */ | 143 | /* cap_capable returns 0 on success, else -EPERM */ |
143 | int error = cap_capable(task, cred, cap, audit); | 144 | int error = cap_capable(task, cred, ns, cap, audit); |
144 | if (!error) { | 145 | if (!error) { |
145 | profile = aa_cred_profile(cred); | 146 | profile = aa_cred_profile(cred); |
146 | if (!unconfined(profile)) | 147 | if (!unconfined(profile)) |
diff --git a/security/commoncap.c b/security/commoncap.c index 49c57fd60ae..43a205bc7d7 100644 --- a/security/commoncap.c +++ b/security/commoncap.c | |||
@@ -27,6 +27,7 @@ | |||
27 | #include <linux/sched.h> | 27 | #include <linux/sched.h> |
28 | #include <linux/prctl.h> | 28 | #include <linux/prctl.h> |
29 | #include <linux/securebits.h> | 29 | #include <linux/securebits.h> |
30 | #include <linux/user_namespace.h> | ||
30 | 31 | ||
31 | /* | 32 | /* |
32 | * If a non-root user executes a setuid-root binary in | 33 | * If a non-root user executes a setuid-root binary in |
@@ -67,6 +68,7 @@ EXPORT_SYMBOL(cap_netlink_recv); | |||
67 | * cap_capable - Determine whether a task has a particular effective capability | 68 | * cap_capable - Determine whether a task has a particular effective capability |
68 | * @tsk: The task to query | 69 | * @tsk: The task to query |
69 | * @cred: The credentials to use | 70 | * @cred: The credentials to use |
71 | * @ns: The user namespace in which we need the capability | ||
70 | * @cap: The capability to check for | 72 | * @cap: The capability to check for |
71 | * @audit: Whether to write an audit message or not | 73 | * @audit: Whether to write an audit message or not |
72 | * | 74 | * |
@@ -78,10 +80,30 @@ EXPORT_SYMBOL(cap_netlink_recv); | |||
78 | * cap_has_capability() returns 0 when a task has a capability, but the | 80 | * cap_has_capability() returns 0 when a task has a capability, but the |
79 | * kernel's capable() and has_capability() returns 1 for this case. | 81 | * kernel's capable() and has_capability() returns 1 for this case. |
80 | */ | 82 | */ |
81 | int cap_capable(struct task_struct *tsk, const struct cred *cred, int cap, | 83 | int cap_capable(struct task_struct *tsk, const struct cred *cred, |
82 | int audit) | 84 | struct user_namespace *targ_ns, int cap, int audit) |
83 | { | 85 | { |
84 | return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM; | 86 | for (;;) { |
87 | /* The creator of the user namespace has all caps. */ | ||
88 | if (targ_ns != &init_user_ns && targ_ns->creator == cred->user) | ||
89 | return 0; | ||
90 | |||
91 | /* Do we have the necessary capabilities? */ | ||
92 | if (targ_ns == cred->user->user_ns) | ||
93 | return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM; | ||
94 | |||
95 | /* Have we tried all of the parent namespaces? */ | ||
96 | if (targ_ns == &init_user_ns) | ||
97 | return -EPERM; | ||
98 | |||
99 | /* | ||
100 | *If you have a capability in a parent user ns, then you have | ||
101 | * it over all children user namespaces as well. | ||
102 | */ | ||
103 | targ_ns = targ_ns->creator->user_ns; | ||
104 | } | ||
105 | |||
106 | /* We never get here */ | ||
85 | } | 107 | } |
86 | 108 | ||
87 | /** | 109 | /** |
@@ -176,7 +198,8 @@ static inline int cap_inh_is_capped(void) | |||
176 | /* they are so limited unless the current task has the CAP_SETPCAP | 198 | /* they are so limited unless the current task has the CAP_SETPCAP |
177 | * capability | 199 | * capability |
178 | */ | 200 | */ |
179 | if (cap_capable(current, current_cred(), CAP_SETPCAP, | 201 | if (cap_capable(current, current_cred(), |
202 | current_cred()->user->user_ns, CAP_SETPCAP, | ||
180 | SECURITY_CAP_AUDIT) == 0) | 203 | SECURITY_CAP_AUDIT) == 0) |
181 | return 0; | 204 | return 0; |
182 | return 1; | 205 | return 1; |
@@ -828,7 +851,8 @@ int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3, | |||
828 | & (new->securebits ^ arg2)) /*[1]*/ | 851 | & (new->securebits ^ arg2)) /*[1]*/ |
829 | || ((new->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/ | 852 | || ((new->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/ |
830 | || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/ | 853 | || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/ |
831 | || (cap_capable(current, current_cred(), CAP_SETPCAP, | 854 | || (cap_capable(current, current_cred(), |
855 | current_cred()->user->user_ns, CAP_SETPCAP, | ||
832 | SECURITY_CAP_AUDIT) != 0) /*[4]*/ | 856 | SECURITY_CAP_AUDIT) != 0) /*[4]*/ |
833 | /* | 857 | /* |
834 | * [1] no changing of bits that are locked | 858 | * [1] no changing of bits that are locked |
@@ -893,7 +917,7 @@ int cap_vm_enough_memory(struct mm_struct *mm, long pages) | |||
893 | { | 917 | { |
894 | int cap_sys_admin = 0; | 918 | int cap_sys_admin = 0; |
895 | 919 | ||
896 | if (cap_capable(current, current_cred(), CAP_SYS_ADMIN, | 920 | if (cap_capable(current, current_cred(), &init_user_ns, CAP_SYS_ADMIN, |
897 | SECURITY_CAP_NOAUDIT) == 0) | 921 | SECURITY_CAP_NOAUDIT) == 0) |
898 | cap_sys_admin = 1; | 922 | cap_sys_admin = 1; |
899 | return __vm_enough_memory(mm, pages, cap_sys_admin); | 923 | return __vm_enough_memory(mm, pages, cap_sys_admin); |
@@ -920,7 +944,7 @@ int cap_file_mmap(struct file *file, unsigned long reqprot, | |||
920 | int ret = 0; | 944 | int ret = 0; |
921 | 945 | ||
922 | if (addr < dac_mmap_min_addr) { | 946 | if (addr < dac_mmap_min_addr) { |
923 | ret = cap_capable(current, current_cred(), CAP_SYS_RAWIO, | 947 | ret = cap_capable(current, current_cred(), &init_user_ns, CAP_SYS_RAWIO, |
924 | SECURITY_CAP_AUDIT); | 948 | SECURITY_CAP_AUDIT); |
925 | /* set PF_SUPERPRIV if it turns out we allow the low mmap */ | 949 | /* set PF_SUPERPRIV if it turns out we allow the low mmap */ |
926 | if (ret == 0) | 950 | if (ret == 0) |
diff --git a/security/security.c b/security/security.c index 9187665a3fd..101142369db 100644 --- a/security/security.c +++ b/security/security.c | |||
@@ -154,29 +154,33 @@ int security_capset(struct cred *new, const struct cred *old, | |||
154 | effective, inheritable, permitted); | 154 | effective, inheritable, permitted); |
155 | } | 155 | } |
156 | 156 | ||
157 | int security_capable(const struct cred *cred, int cap) | 157 | int security_capable(struct user_namespace *ns, const struct cred *cred, |
158 | int cap) | ||
158 | { | 159 | { |
159 | return security_ops->capable(current, cred, cap, SECURITY_CAP_AUDIT); | 160 | return security_ops->capable(current, cred, ns, cap, |
161 | SECURITY_CAP_AUDIT); | ||
160 | } | 162 | } |
161 | 163 | ||
162 | int security_real_capable(struct task_struct *tsk, int cap) | 164 | int security_real_capable(struct task_struct *tsk, struct user_namespace *ns, |
165 | int cap) | ||
163 | { | 166 | { |
164 | const struct cred *cred; | 167 | const struct cred *cred; |
165 | int ret; | 168 | int ret; |
166 | 169 | ||
167 | cred = get_task_cred(tsk); | 170 | cred = get_task_cred(tsk); |
168 | ret = security_ops->capable(tsk, cred, cap, SECURITY_CAP_AUDIT); | 171 | ret = security_ops->capable(tsk, cred, ns, cap, SECURITY_CAP_AUDIT); |
169 | put_cred(cred); | 172 | put_cred(cred); |
170 | return ret; | 173 | return ret; |
171 | } | 174 | } |
172 | 175 | ||
173 | int security_real_capable_noaudit(struct task_struct *tsk, int cap) | 176 | int security_real_capable_noaudit(struct task_struct *tsk, |
177 | struct user_namespace *ns, int cap) | ||
174 | { | 178 | { |
175 | const struct cred *cred; | 179 | const struct cred *cred; |
176 | int ret; | 180 | int ret; |
177 | 181 | ||
178 | cred = get_task_cred(tsk); | 182 | cred = get_task_cred(tsk); |
179 | ret = security_ops->capable(tsk, cred, cap, SECURITY_CAP_NOAUDIT); | 183 | ret = security_ops->capable(tsk, cred, ns, cap, SECURITY_CAP_NOAUDIT); |
180 | put_cred(cred); | 184 | put_cred(cred); |
181 | return ret; | 185 | return ret; |
182 | } | 186 | } |
diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index 6475e1f0223..c67f863d354 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c | |||
@@ -79,6 +79,7 @@ | |||
79 | #include <linux/mutex.h> | 79 | #include <linux/mutex.h> |
80 | #include <linux/posix-timers.h> | 80 | #include <linux/posix-timers.h> |
81 | #include <linux/syslog.h> | 81 | #include <linux/syslog.h> |
82 | #include <linux/user_namespace.h> | ||
82 | 83 | ||
83 | #include "avc.h" | 84 | #include "avc.h" |
84 | #include "objsec.h" | 85 | #include "objsec.h" |
@@ -1846,11 +1847,11 @@ static int selinux_capset(struct cred *new, const struct cred *old, | |||
1846 | */ | 1847 | */ |
1847 | 1848 | ||
1848 | static int selinux_capable(struct task_struct *tsk, const struct cred *cred, | 1849 | static int selinux_capable(struct task_struct *tsk, const struct cred *cred, |
1849 | int cap, int audit) | 1850 | struct user_namespace *ns, int cap, int audit) |
1850 | { | 1851 | { |
1851 | int rc; | 1852 | int rc; |
1852 | 1853 | ||
1853 | rc = cap_capable(tsk, cred, cap, audit); | 1854 | rc = cap_capable(tsk, cred, ns, cap, audit); |
1854 | if (rc) | 1855 | if (rc) |
1855 | return rc; | 1856 | return rc; |
1856 | 1857 | ||
@@ -1931,7 +1932,8 @@ static int selinux_vm_enough_memory(struct mm_struct *mm, long pages) | |||
1931 | { | 1932 | { |
1932 | int rc, cap_sys_admin = 0; | 1933 | int rc, cap_sys_admin = 0; |
1933 | 1934 | ||
1934 | rc = selinux_capable(current, current_cred(), CAP_SYS_ADMIN, | 1935 | rc = selinux_capable(current, current_cred(), |
1936 | &init_user_ns, CAP_SYS_ADMIN, | ||
1935 | SECURITY_CAP_NOAUDIT); | 1937 | SECURITY_CAP_NOAUDIT); |
1936 | if (rc == 0) | 1938 | if (rc == 0) |
1937 | cap_sys_admin = 1; | 1939 | cap_sys_admin = 1; |
@@ -2834,7 +2836,8 @@ static int selinux_inode_getsecurity(const struct inode *inode, const char *name | |||
2834 | * and lack of permission just means that we fall back to the | 2836 | * and lack of permission just means that we fall back to the |
2835 | * in-core context value, not a denial. | 2837 | * in-core context value, not a denial. |
2836 | */ | 2838 | */ |
2837 | error = selinux_capable(current, current_cred(), CAP_MAC_ADMIN, | 2839 | error = selinux_capable(current, current_cred(), |
2840 | &init_user_ns, CAP_MAC_ADMIN, | ||
2838 | SECURITY_CAP_NOAUDIT); | 2841 | SECURITY_CAP_NOAUDIT); |
2839 | if (!error) | 2842 | if (!error) |
2840 | error = security_sid_to_context_force(isec->sid, &context, | 2843 | error = security_sid_to_context_force(isec->sid, &context, |
@@ -2968,7 +2971,7 @@ static int selinux_file_ioctl(struct file *file, unsigned int cmd, | |||
2968 | case KDSKBENT: | 2971 | case KDSKBENT: |
2969 | case KDSKBSENT: | 2972 | case KDSKBSENT: |
2970 | error = task_has_capability(current, cred, CAP_SYS_TTY_CONFIG, | 2973 | error = task_has_capability(current, cred, CAP_SYS_TTY_CONFIG, |
2971 | SECURITY_CAP_AUDIT); | 2974 | SECURITY_CAP_AUDIT); |
2972 | break; | 2975 | break; |
2973 | 2976 | ||
2974 | /* default case assumes that the command will go | 2977 | /* default case assumes that the command will go |