diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2014-12-17 15:31:40 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2014-12-17 15:31:40 -0500 |
commit | 87c31b39abcb6fb6bd7d111200c9627a594bf6a9 (patch) | |
tree | ab2e5331fea9b823cb92719d0954a9141451c931 /kernel | |
parent | f045bbb9fa1bf6f507ad4de12d4e3471d8f672f1 (diff) | |
parent | db86da7cb76f797a1a8b445166a15cb922c6ff85 (diff) |
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiederm/user-namespace
Pull user namespace related fixes from Eric Biederman:
"As these are bug fixes almost all of thes changes are marked for
backporting to stable.
The first change (implicitly adding MNT_NODEV on remount) addresses a
regression that was created when security issues with unprivileged
remount were closed. I go on to update the remount test to make it
easy to detect if this issue reoccurs.
Then there are a handful of mount and umount related fixes.
Then half of the changes deal with the a recently discovered design
bug in the permission checks of gid_map. Unix since the beginning has
allowed setting group permissions on files to less than the user and
other permissions (aka ---rwx---rwx). As the unix permission checks
stop as soon as a group matches, and setgroups allows setting groups
that can not later be dropped, results in a situtation where it is
possible to legitimately use a group to assign fewer privileges to a
process. Which means dropping a group can increase a processes
privileges.
The fix I have adopted is that gid_map is now no longer writable
without privilege unless the new file /proc/self/setgroups has been
set to permanently disable setgroups.
The bulk of user namespace using applications even the applications
using applications using user namespaces without privilege remain
unaffected by this change. Unfortunately this ix breaks a couple user
space applications, that were relying on the problematic behavior (one
of which was tools/selftests/mount/unprivileged-remount-test.c).
To hopefully prevent needing a regression fix on top of my security
fix I rounded folks who work with the container implementations mostly
like to be affected and encouraged them to test the changes.
> So far nothing broke on my libvirt-lxc test bed. :-)
> Tested with openSUSE 13.2 and libvirt 1.2.9.
> Tested-by: Richard Weinberger <richard@nod.at>
> Tested on Fedora20 with libvirt 1.2.11, works fine.
> Tested-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com>
> Ok, thanks - yes, unprivileged lxc is working fine with your kernels.
> Just to be sure I was testing the right thing I also tested using
> my unprivileged nsexec testcases, and they failed on setgroup/setgid
> as now expected, and succeeded there without your patches.
> Tested-by: Serge Hallyn <serge.hallyn@ubuntu.com>
> I tested this with Sandstorm. It breaks as is and it works if I add
> the setgroups thing.
> Tested-by: Andy Lutomirski <luto@amacapital.net> # breaks things as designed :("
* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiederm/user-namespace:
userns: Unbreak the unprivileged remount tests
userns; Correct the comment in map_write
userns: Allow setting gid_maps without privilege when setgroups is disabled
userns: Add a knob to disable setgroups on a per user namespace basis
userns: Rename id_map_mutex to userns_state_mutex
userns: Only allow the creator of the userns unprivileged mappings
userns: Check euid no fsuid when establishing an unprivileged uid mapping
userns: Don't allow unprivileged creation of gid mappings
userns: Don't allow setgroups until a gid mapping has been setablished
userns: Document what the invariant required for safe unprivileged mappings.
groups: Consolidate the setgroups permission checks
mnt: Clear mnt_expire during pivot_root
mnt: Carefully set CL_UNPRIVILEGED in clone_mnt
mnt: Move the clear of MNT_LOCKED from copy_tree to it's callers.
umount: Do not allow unmounting rootfs.
umount: Disallow unprivileged mount force
mnt: Update unprivileged remount test
mnt: Implicitly add MNT_NODEV on remount when it was implicitly added by mount
Diffstat (limited to 'kernel')
-rw-r--r-- | kernel/groups.c | 11 | ||||
-rw-r--r-- | kernel/uid16.c | 2 | ||||
-rw-r--r-- | kernel/user.c | 1 | ||||
-rw-r--r-- | kernel/user_namespace.c | 124 |
4 files changed, 125 insertions, 13 deletions
diff --git a/kernel/groups.c b/kernel/groups.c index 451698f86cfa..664411f171b5 100644 --- a/kernel/groups.c +++ b/kernel/groups.c | |||
@@ -6,6 +6,7 @@ | |||
6 | #include <linux/slab.h> | 6 | #include <linux/slab.h> |
7 | #include <linux/security.h> | 7 | #include <linux/security.h> |
8 | #include <linux/syscalls.h> | 8 | #include <linux/syscalls.h> |
9 | #include <linux/user_namespace.h> | ||
9 | #include <asm/uaccess.h> | 10 | #include <asm/uaccess.h> |
10 | 11 | ||
11 | /* init to 2 - one for init_task, one to ensure it is never freed */ | 12 | /* init to 2 - one for init_task, one to ensure it is never freed */ |
@@ -213,6 +214,14 @@ out: | |||
213 | return i; | 214 | return i; |
214 | } | 215 | } |
215 | 216 | ||
217 | bool may_setgroups(void) | ||
218 | { | ||
219 | struct user_namespace *user_ns = current_user_ns(); | ||
220 | |||
221 | return ns_capable(user_ns, CAP_SETGID) && | ||
222 | userns_may_setgroups(user_ns); | ||
223 | } | ||
224 | |||
216 | /* | 225 | /* |
217 | * SMP: Our groups are copy-on-write. We can set them safely | 226 | * SMP: Our groups are copy-on-write. We can set them safely |
218 | * without another task interfering. | 227 | * without another task interfering. |
@@ -223,7 +232,7 @@ SYSCALL_DEFINE2(setgroups, int, gidsetsize, gid_t __user *, grouplist) | |||
223 | struct group_info *group_info; | 232 | struct group_info *group_info; |
224 | int retval; | 233 | int retval; |
225 | 234 | ||
226 | if (!ns_capable(current_user_ns(), CAP_SETGID)) | 235 | if (!may_setgroups()) |
227 | return -EPERM; | 236 | return -EPERM; |
228 | if ((unsigned)gidsetsize > NGROUPS_MAX) | 237 | if ((unsigned)gidsetsize > NGROUPS_MAX) |
229 | return -EINVAL; | 238 | return -EINVAL; |
diff --git a/kernel/uid16.c b/kernel/uid16.c index 602e5bbbceff..d58cc4d8f0d1 100644 --- a/kernel/uid16.c +++ b/kernel/uid16.c | |||
@@ -176,7 +176,7 @@ SYSCALL_DEFINE2(setgroups16, int, gidsetsize, old_gid_t __user *, grouplist) | |||
176 | struct group_info *group_info; | 176 | struct group_info *group_info; |
177 | int retval; | 177 | int retval; |
178 | 178 | ||
179 | if (!ns_capable(current_user_ns(), CAP_SETGID)) | 179 | if (!may_setgroups()) |
180 | return -EPERM; | 180 | return -EPERM; |
181 | if ((unsigned)gidsetsize > NGROUPS_MAX) | 181 | if ((unsigned)gidsetsize > NGROUPS_MAX) |
182 | return -EINVAL; | 182 | return -EINVAL; |
diff --git a/kernel/user.c b/kernel/user.c index 69b800aebf13..b069ccbfb0b0 100644 --- a/kernel/user.c +++ b/kernel/user.c | |||
@@ -54,6 +54,7 @@ struct user_namespace init_user_ns = { | |||
54 | #ifdef CONFIG_USER_NS | 54 | #ifdef CONFIG_USER_NS |
55 | .ns.ops = &userns_operations, | 55 | .ns.ops = &userns_operations, |
56 | #endif | 56 | #endif |
57 | .flags = USERNS_INIT_FLAGS, | ||
57 | #ifdef CONFIG_PERSISTENT_KEYRINGS | 58 | #ifdef CONFIG_PERSISTENT_KEYRINGS |
58 | .persistent_keyring_register_sem = | 59 | .persistent_keyring_register_sem = |
59 | __RWSEM_INITIALIZER(init_user_ns.persistent_keyring_register_sem), | 60 | __RWSEM_INITIALIZER(init_user_ns.persistent_keyring_register_sem), |
diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c index 1491ad00388f..4109f8320684 100644 --- a/kernel/user_namespace.c +++ b/kernel/user_namespace.c | |||
@@ -24,6 +24,7 @@ | |||
24 | #include <linux/fs_struct.h> | 24 | #include <linux/fs_struct.h> |
25 | 25 | ||
26 | static struct kmem_cache *user_ns_cachep __read_mostly; | 26 | static struct kmem_cache *user_ns_cachep __read_mostly; |
27 | static DEFINE_MUTEX(userns_state_mutex); | ||
27 | 28 | ||
28 | static bool new_idmap_permitted(const struct file *file, | 29 | static bool new_idmap_permitted(const struct file *file, |
29 | struct user_namespace *ns, int cap_setid, | 30 | struct user_namespace *ns, int cap_setid, |
@@ -100,6 +101,11 @@ int create_user_ns(struct cred *new) | |||
100 | ns->owner = owner; | 101 | ns->owner = owner; |
101 | ns->group = group; | 102 | ns->group = group; |
102 | 103 | ||
104 | /* Inherit USERNS_SETGROUPS_ALLOWED from our parent */ | ||
105 | mutex_lock(&userns_state_mutex); | ||
106 | ns->flags = parent_ns->flags; | ||
107 | mutex_unlock(&userns_state_mutex); | ||
108 | |||
103 | set_cred_user_ns(new, ns); | 109 | set_cred_user_ns(new, ns); |
104 | 110 | ||
105 | #ifdef CONFIG_PERSISTENT_KEYRINGS | 111 | #ifdef CONFIG_PERSISTENT_KEYRINGS |
@@ -584,9 +590,6 @@ static bool mappings_overlap(struct uid_gid_map *new_map, | |||
584 | return false; | 590 | return false; |
585 | } | 591 | } |
586 | 592 | ||
587 | |||
588 | static DEFINE_MUTEX(id_map_mutex); | ||
589 | |||
590 | static ssize_t map_write(struct file *file, const char __user *buf, | 593 | static ssize_t map_write(struct file *file, const char __user *buf, |
591 | size_t count, loff_t *ppos, | 594 | size_t count, loff_t *ppos, |
592 | int cap_setid, | 595 | int cap_setid, |
@@ -603,7 +606,7 @@ static ssize_t map_write(struct file *file, const char __user *buf, | |||
603 | ssize_t ret = -EINVAL; | 606 | ssize_t ret = -EINVAL; |
604 | 607 | ||
605 | /* | 608 | /* |
606 | * The id_map_mutex serializes all writes to any given map. | 609 | * The userns_state_mutex serializes all writes to any given map. |
607 | * | 610 | * |
608 | * Any map is only ever written once. | 611 | * Any map is only ever written once. |
609 | * | 612 | * |
@@ -621,7 +624,7 @@ static ssize_t map_write(struct file *file, const char __user *buf, | |||
621 | * order and smp_rmb() is guaranteed that we don't have crazy | 624 | * order and smp_rmb() is guaranteed that we don't have crazy |
622 | * architectures returning stale data. | 625 | * architectures returning stale data. |
623 | */ | 626 | */ |
624 | mutex_lock(&id_map_mutex); | 627 | mutex_lock(&userns_state_mutex); |
625 | 628 | ||
626 | ret = -EPERM; | 629 | ret = -EPERM; |
627 | /* Only allow one successful write to the map */ | 630 | /* Only allow one successful write to the map */ |
@@ -641,7 +644,7 @@ static ssize_t map_write(struct file *file, const char __user *buf, | |||
641 | if (!page) | 644 | if (!page) |
642 | goto out; | 645 | goto out; |
643 | 646 | ||
644 | /* Only allow <= page size writes at the beginning of the file */ | 647 | /* Only allow < page size writes at the beginning of the file */ |
645 | ret = -EINVAL; | 648 | ret = -EINVAL; |
646 | if ((*ppos != 0) || (count >= PAGE_SIZE)) | 649 | if ((*ppos != 0) || (count >= PAGE_SIZE)) |
647 | goto out; | 650 | goto out; |
@@ -751,7 +754,7 @@ static ssize_t map_write(struct file *file, const char __user *buf, | |||
751 | *ppos = count; | 754 | *ppos = count; |
752 | ret = count; | 755 | ret = count; |
753 | out: | 756 | out: |
754 | mutex_unlock(&id_map_mutex); | 757 | mutex_unlock(&userns_state_mutex); |
755 | if (page) | 758 | if (page) |
756 | free_page(page); | 759 | free_page(page); |
757 | return ret; | 760 | return ret; |
@@ -813,16 +816,21 @@ static bool new_idmap_permitted(const struct file *file, | |||
813 | struct user_namespace *ns, int cap_setid, | 816 | struct user_namespace *ns, int cap_setid, |
814 | struct uid_gid_map *new_map) | 817 | struct uid_gid_map *new_map) |
815 | { | 818 | { |
816 | /* Allow mapping to your own filesystem ids */ | 819 | const struct cred *cred = file->f_cred; |
817 | if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1)) { | 820 | /* Don't allow mappings that would allow anything that wouldn't |
821 | * be allowed without the establishment of unprivileged mappings. | ||
822 | */ | ||
823 | if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1) && | ||
824 | uid_eq(ns->owner, cred->euid)) { | ||
818 | u32 id = new_map->extent[0].lower_first; | 825 | u32 id = new_map->extent[0].lower_first; |
819 | if (cap_setid == CAP_SETUID) { | 826 | if (cap_setid == CAP_SETUID) { |
820 | kuid_t uid = make_kuid(ns->parent, id); | 827 | kuid_t uid = make_kuid(ns->parent, id); |
821 | if (uid_eq(uid, file->f_cred->fsuid)) | 828 | if (uid_eq(uid, cred->euid)) |
822 | return true; | 829 | return true; |
823 | } else if (cap_setid == CAP_SETGID) { | 830 | } else if (cap_setid == CAP_SETGID) { |
824 | kgid_t gid = make_kgid(ns->parent, id); | 831 | kgid_t gid = make_kgid(ns->parent, id); |
825 | if (gid_eq(gid, file->f_cred->fsgid)) | 832 | if (!(ns->flags & USERNS_SETGROUPS_ALLOWED) && |
833 | gid_eq(gid, cred->egid)) | ||
826 | return true; | 834 | return true; |
827 | } | 835 | } |
828 | } | 836 | } |
@@ -842,6 +850,100 @@ static bool new_idmap_permitted(const struct file *file, | |||
842 | return false; | 850 | return false; |
843 | } | 851 | } |
844 | 852 | ||
853 | int proc_setgroups_show(struct seq_file *seq, void *v) | ||
854 | { | ||
855 | struct user_namespace *ns = seq->private; | ||
856 | unsigned long userns_flags = ACCESS_ONCE(ns->flags); | ||
857 | |||
858 | seq_printf(seq, "%s\n", | ||
859 | (userns_flags & USERNS_SETGROUPS_ALLOWED) ? | ||
860 | "allow" : "deny"); | ||
861 | return 0; | ||
862 | } | ||
863 | |||
864 | ssize_t proc_setgroups_write(struct file *file, const char __user *buf, | ||
865 | size_t count, loff_t *ppos) | ||
866 | { | ||
867 | struct seq_file *seq = file->private_data; | ||
868 | struct user_namespace *ns = seq->private; | ||
869 | char kbuf[8], *pos; | ||
870 | bool setgroups_allowed; | ||
871 | ssize_t ret; | ||
872 | |||
873 | /* Only allow a very narrow range of strings to be written */ | ||
874 | ret = -EINVAL; | ||
875 | if ((*ppos != 0) || (count >= sizeof(kbuf))) | ||
876 | goto out; | ||
877 | |||
878 | /* What was written? */ | ||
879 | ret = -EFAULT; | ||
880 | if (copy_from_user(kbuf, buf, count)) | ||
881 | goto out; | ||
882 | kbuf[count] = '\0'; | ||
883 | pos = kbuf; | ||
884 | |||
885 | /* What is being requested? */ | ||
886 | ret = -EINVAL; | ||
887 | if (strncmp(pos, "allow", 5) == 0) { | ||
888 | pos += 5; | ||
889 | setgroups_allowed = true; | ||
890 | } | ||
891 | else if (strncmp(pos, "deny", 4) == 0) { | ||
892 | pos += 4; | ||
893 | setgroups_allowed = false; | ||
894 | } | ||
895 | else | ||
896 | goto out; | ||
897 | |||
898 | /* Verify there is not trailing junk on the line */ | ||
899 | pos = skip_spaces(pos); | ||
900 | if (*pos != '\0') | ||
901 | goto out; | ||
902 | |||
903 | ret = -EPERM; | ||
904 | mutex_lock(&userns_state_mutex); | ||
905 | if (setgroups_allowed) { | ||
906 | /* Enabling setgroups after setgroups has been disabled | ||
907 | * is not allowed. | ||
908 | */ | ||
909 | if (!(ns->flags & USERNS_SETGROUPS_ALLOWED)) | ||
910 | goto out_unlock; | ||
911 | } else { | ||
912 | /* Permanently disabling setgroups after setgroups has | ||
913 | * been enabled by writing the gid_map is not allowed. | ||
914 | */ | ||
915 | if (ns->gid_map.nr_extents != 0) | ||
916 | goto out_unlock; | ||
917 | ns->flags &= ~USERNS_SETGROUPS_ALLOWED; | ||
918 | } | ||
919 | mutex_unlock(&userns_state_mutex); | ||
920 | |||
921 | /* Report a successful write */ | ||
922 | *ppos = count; | ||
923 | ret = count; | ||
924 | out: | ||
925 | return ret; | ||
926 | out_unlock: | ||
927 | mutex_unlock(&userns_state_mutex); | ||
928 | goto out; | ||
929 | } | ||
930 | |||
931 | bool userns_may_setgroups(const struct user_namespace *ns) | ||
932 | { | ||
933 | bool allowed; | ||
934 | |||
935 | mutex_lock(&userns_state_mutex); | ||
936 | /* It is not safe to use setgroups until a gid mapping in | ||
937 | * the user namespace has been established. | ||
938 | */ | ||
939 | allowed = ns->gid_map.nr_extents != 0; | ||
940 | /* Is setgroups allowed? */ | ||
941 | allowed = allowed && (ns->flags & USERNS_SETGROUPS_ALLOWED); | ||
942 | mutex_unlock(&userns_state_mutex); | ||
943 | |||
944 | return allowed; | ||
945 | } | ||
946 | |||
845 | static inline struct user_namespace *to_user_ns(struct ns_common *ns) | 947 | static inline struct user_namespace *to_user_ns(struct ns_common *ns) |
846 | { | 948 | { |
847 | return container_of(ns, struct user_namespace, ns); | 949 | return container_of(ns, struct user_namespace, ns); |