aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/user_namespace.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/user_namespace.c')
-rw-r--r--kernel/user_namespace.c153
1 files changed, 127 insertions, 26 deletions
diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c
index aa312b0dc3ec..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
26static struct kmem_cache *user_ns_cachep __read_mostly; 26static struct kmem_cache *user_ns_cachep __read_mostly;
27static DEFINE_MUTEX(userns_state_mutex);
27 28
28static bool new_idmap_permitted(const struct file *file, 29static bool new_idmap_permitted(const struct file *file,
29 struct user_namespace *ns, int cap_setid, 30 struct user_namespace *ns, int cap_setid,
@@ -86,11 +87,12 @@ int create_user_ns(struct cred *new)
86 if (!ns) 87 if (!ns)
87 return -ENOMEM; 88 return -ENOMEM;
88 89
89 ret = proc_alloc_inum(&ns->proc_inum); 90 ret = ns_alloc_inum(&ns->ns);
90 if (ret) { 91 if (ret) {
91 kmem_cache_free(user_ns_cachep, ns); 92 kmem_cache_free(user_ns_cachep, ns);
92 return ret; 93 return ret;
93 } 94 }
95 ns->ns.ops = &userns_operations;
94 96
95 atomic_set(&ns->count, 1); 97 atomic_set(&ns->count, 1);
96 /* Leave the new->user_ns reference with the new user namespace. */ 98 /* Leave the new->user_ns reference with the new user namespace. */
@@ -99,6 +101,11 @@ int create_user_ns(struct cred *new)
99 ns->owner = owner; 101 ns->owner = owner;
100 ns->group = group; 102 ns->group = group;
101 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
102 set_cred_user_ns(new, ns); 109 set_cred_user_ns(new, ns);
103 110
104#ifdef CONFIG_PERSISTENT_KEYRINGS 111#ifdef CONFIG_PERSISTENT_KEYRINGS
@@ -136,7 +143,7 @@ void free_user_ns(struct user_namespace *ns)
136#ifdef CONFIG_PERSISTENT_KEYRINGS 143#ifdef CONFIG_PERSISTENT_KEYRINGS
137 key_put(ns->persistent_keyring_register); 144 key_put(ns->persistent_keyring_register);
138#endif 145#endif
139 proc_free_inum(ns->proc_inum); 146 ns_free_inum(&ns->ns);
140 kmem_cache_free(user_ns_cachep, ns); 147 kmem_cache_free(user_ns_cachep, ns);
141 ns = parent; 148 ns = parent;
142 } while (atomic_dec_and_test(&parent->count)); 149 } while (atomic_dec_and_test(&parent->count));
@@ -583,9 +590,6 @@ static bool mappings_overlap(struct uid_gid_map *new_map,
583 return false; 590 return false;
584} 591}
585 592
586
587static DEFINE_MUTEX(id_map_mutex);
588
589static ssize_t map_write(struct file *file, const char __user *buf, 593static ssize_t map_write(struct file *file, const char __user *buf,
590 size_t count, loff_t *ppos, 594 size_t count, loff_t *ppos,
591 int cap_setid, 595 int cap_setid,
@@ -602,7 +606,7 @@ static ssize_t map_write(struct file *file, const char __user *buf,
602 ssize_t ret = -EINVAL; 606 ssize_t ret = -EINVAL;
603 607
604 /* 608 /*
605 * The id_map_mutex serializes all writes to any given map. 609 * The userns_state_mutex serializes all writes to any given map.
606 * 610 *
607 * Any map is only ever written once. 611 * Any map is only ever written once.
608 * 612 *
@@ -620,7 +624,7 @@ static ssize_t map_write(struct file *file, const char __user *buf,
620 * 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
621 * architectures returning stale data. 625 * architectures returning stale data.
622 */ 626 */
623 mutex_lock(&id_map_mutex); 627 mutex_lock(&userns_state_mutex);
624 628
625 ret = -EPERM; 629 ret = -EPERM;
626 /* Only allow one successful write to the map */ 630 /* Only allow one successful write to the map */
@@ -640,7 +644,7 @@ static ssize_t map_write(struct file *file, const char __user *buf,
640 if (!page) 644 if (!page)
641 goto out; 645 goto out;
642 646
643 /* Only allow <= page size writes at the beginning of the file */ 647 /* Only allow < page size writes at the beginning of the file */
644 ret = -EINVAL; 648 ret = -EINVAL;
645 if ((*ppos != 0) || (count >= PAGE_SIZE)) 649 if ((*ppos != 0) || (count >= PAGE_SIZE))
646 goto out; 650 goto out;
@@ -750,7 +754,7 @@ static ssize_t map_write(struct file *file, const char __user *buf,
750 *ppos = count; 754 *ppos = count;
751 ret = count; 755 ret = count;
752out: 756out:
753 mutex_unlock(&id_map_mutex); 757 mutex_unlock(&userns_state_mutex);
754 if (page) 758 if (page)
755 free_page(page); 759 free_page(page);
756 return ret; 760 return ret;
@@ -812,16 +816,21 @@ static bool new_idmap_permitted(const struct file *file,
812 struct user_namespace *ns, int cap_setid, 816 struct user_namespace *ns, int cap_setid,
813 struct uid_gid_map *new_map) 817 struct uid_gid_map *new_map)
814{ 818{
815 /* Allow mapping to your own filesystem ids */ 819 const struct cred *cred = file->f_cred;
816 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)) {
817 u32 id = new_map->extent[0].lower_first; 825 u32 id = new_map->extent[0].lower_first;
818 if (cap_setid == CAP_SETUID) { 826 if (cap_setid == CAP_SETUID) {
819 kuid_t uid = make_kuid(ns->parent, id); 827 kuid_t uid = make_kuid(ns->parent, id);
820 if (uid_eq(uid, file->f_cred->fsuid)) 828 if (uid_eq(uid, cred->euid))
821 return true; 829 return true;
822 } else if (cap_setid == CAP_SETGID) { 830 } else if (cap_setid == CAP_SETGID) {
823 kgid_t gid = make_kgid(ns->parent, id); 831 kgid_t gid = make_kgid(ns->parent, id);
824 if (gid_eq(gid, file->f_cred->fsgid)) 832 if (!(ns->flags & USERNS_SETGROUPS_ALLOWED) &&
833 gid_eq(gid, cred->egid))
825 return true; 834 return true;
826 } 835 }
827 } 836 }
@@ -841,7 +850,106 @@ static bool new_idmap_permitted(const struct file *file,
841 return false; 850 return false;
842} 851}
843 852
844static void *userns_get(struct task_struct *task) 853int 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
864ssize_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;
924out:
925 return ret;
926out_unlock:
927 mutex_unlock(&userns_state_mutex);
928 goto out;
929}
930
931bool 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
947static inline struct user_namespace *to_user_ns(struct ns_common *ns)
948{
949 return container_of(ns, struct user_namespace, ns);
950}
951
952static struct ns_common *userns_get(struct task_struct *task)
845{ 953{
846 struct user_namespace *user_ns; 954 struct user_namespace *user_ns;
847 955
@@ -849,17 +957,17 @@ static void *userns_get(struct task_struct *task)
849 user_ns = get_user_ns(__task_cred(task)->user_ns); 957 user_ns = get_user_ns(__task_cred(task)->user_ns);
850 rcu_read_unlock(); 958 rcu_read_unlock();
851 959
852 return user_ns; 960 return user_ns ? &user_ns->ns : NULL;
853} 961}
854 962
855static void userns_put(void *ns) 963static void userns_put(struct ns_common *ns)
856{ 964{
857 put_user_ns(ns); 965 put_user_ns(to_user_ns(ns));
858} 966}
859 967
860static int userns_install(struct nsproxy *nsproxy, void *ns) 968static int userns_install(struct nsproxy *nsproxy, struct ns_common *ns)
861{ 969{
862 struct user_namespace *user_ns = ns; 970 struct user_namespace *user_ns = to_user_ns(ns);
863 struct cred *cred; 971 struct cred *cred;
864 972
865 /* Don't allow gaining capabilities by reentering 973 /* Don't allow gaining capabilities by reentering
@@ -888,19 +996,12 @@ static int userns_install(struct nsproxy *nsproxy, void *ns)
888 return commit_creds(cred); 996 return commit_creds(cred);
889} 997}
890 998
891static unsigned int userns_inum(void *ns)
892{
893 struct user_namespace *user_ns = ns;
894 return user_ns->proc_inum;
895}
896
897const struct proc_ns_operations userns_operations = { 999const struct proc_ns_operations userns_operations = {
898 .name = "user", 1000 .name = "user",
899 .type = CLONE_NEWUSER, 1001 .type = CLONE_NEWUSER,
900 .get = userns_get, 1002 .get = userns_get,
901 .put = userns_put, 1003 .put = userns_put,
902 .install = userns_install, 1004 .install = userns_install,
903 .inum = userns_inum,
904}; 1005};
905 1006
906static __init int user_namespaces_init(void) 1007static __init int user_namespaces_init(void)