diff options
author | Eric W. Biederman <ebiederm@xmission.com> | 2016-07-30 14:58:49 -0400 |
---|---|---|
committer | Eric W. Biederman <ebiederm@xmission.com> | 2016-08-08 14:18:58 -0400 |
commit | dbec28460a89aa7c02c3301e9e108d98272549d2 (patch) | |
tree | bf4b71c69e072bb6fdbe680674ab84bd79143e45 | |
parent | b032132c3c218f4a09e9499b3674299a752581c6 (diff) |
userns: Add per user namespace sysctls.
Limit per userns sysctls to only be opened for write by a holder
of CAP_SYS_RESOURCE.
Add all of the necessary boilerplate for having per user namespace
sysctls.
Acked-by: Kees Cook <keescook@chromium.org>
Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
-rw-r--r-- | include/linux/user_namespace.h | 4 | ||||
-rw-r--r-- | kernel/Makefile | 2 | ||||
-rw-r--r-- | kernel/ucount.c | 99 | ||||
-rw-r--r-- | kernel/user_namespace.c | 18 |
4 files changed, 120 insertions, 3 deletions
diff --git a/include/linux/user_namespace.h b/include/linux/user_namespace.h index 4e79b3c64dee..e5697eaf6bf9 100644 --- a/include/linux/user_namespace.h +++ b/include/linux/user_namespace.h | |||
@@ -40,6 +40,10 @@ struct user_namespace { | |||
40 | struct rw_semaphore persistent_keyring_register_sem; | 40 | struct rw_semaphore persistent_keyring_register_sem; |
41 | #endif | 41 | #endif |
42 | struct work_struct work; | 42 | struct work_struct work; |
43 | #ifdef CONFIG_SYSCTL | ||
44 | struct ctl_table_set set; | ||
45 | struct ctl_table_header *sysctls; | ||
46 | #endif | ||
43 | }; | 47 | }; |
44 | 48 | ||
45 | extern struct user_namespace init_user_ns; | 49 | extern struct user_namespace init_user_ns; |
diff --git a/kernel/Makefile b/kernel/Makefile index e2ec54e2b952..eb26e12c6c2a 100644 --- a/kernel/Makefile +++ b/kernel/Makefile | |||
@@ -9,7 +9,7 @@ obj-y = fork.o exec_domain.o panic.o \ | |||
9 | extable.o params.o \ | 9 | extable.o params.o \ |
10 | kthread.o sys_ni.o nsproxy.o \ | 10 | kthread.o sys_ni.o nsproxy.o \ |
11 | notifier.o ksysfs.o cred.o reboot.o \ | 11 | notifier.o ksysfs.o cred.o reboot.o \ |
12 | async.o range.o smpboot.o | 12 | async.o range.o smpboot.o ucount.o |
13 | 13 | ||
14 | obj-$(CONFIG_MULTIUSER) += groups.o | 14 | obj-$(CONFIG_MULTIUSER) += groups.o |
15 | 15 | ||
diff --git a/kernel/ucount.c b/kernel/ucount.c new file mode 100644 index 000000000000..cbde1dc87851 --- /dev/null +++ b/kernel/ucount.c | |||
@@ -0,0 +1,99 @@ | |||
1 | /* | ||
2 | * This program is free software; you can redistribute it and/or | ||
3 | * modify it under the terms of the GNU General Public License as | ||
4 | * published by the Free Software Foundation, version 2 of the | ||
5 | * License. | ||
6 | */ | ||
7 | |||
8 | #include <linux/stat.h> | ||
9 | #include <linux/sysctl.h> | ||
10 | #include <linux/slab.h> | ||
11 | #include <linux/user_namespace.h> | ||
12 | |||
13 | #ifdef CONFIG_SYSCTL | ||
14 | static struct ctl_table_set * | ||
15 | set_lookup(struct ctl_table_root *root) | ||
16 | { | ||
17 | return ¤t_user_ns()->set; | ||
18 | } | ||
19 | |||
20 | static int set_is_seen(struct ctl_table_set *set) | ||
21 | { | ||
22 | return ¤t_user_ns()->set == set; | ||
23 | } | ||
24 | |||
25 | static int set_permissions(struct ctl_table_header *head, | ||
26 | struct ctl_table *table) | ||
27 | { | ||
28 | struct user_namespace *user_ns = | ||
29 | container_of(head->set, struct user_namespace, set); | ||
30 | int mode; | ||
31 | |||
32 | /* Allow users with CAP_SYS_RESOURCE unrestrained access */ | ||
33 | if (ns_capable(user_ns, CAP_SYS_RESOURCE)) | ||
34 | mode = (table->mode & S_IRWXU) >> 6; | ||
35 | else | ||
36 | /* Allow all others at most read-only access */ | ||
37 | mode = table->mode & S_IROTH; | ||
38 | return (mode << 6) | (mode << 3) | mode; | ||
39 | } | ||
40 | |||
41 | static struct ctl_table_root set_root = { | ||
42 | .lookup = set_lookup, | ||
43 | .permissions = set_permissions, | ||
44 | }; | ||
45 | |||
46 | static struct ctl_table userns_table[] = { | ||
47 | { } | ||
48 | }; | ||
49 | #endif /* CONFIG_SYSCTL */ | ||
50 | |||
51 | bool setup_userns_sysctls(struct user_namespace *ns) | ||
52 | { | ||
53 | #ifdef CONFIG_SYSCTL | ||
54 | struct ctl_table *tbl; | ||
55 | setup_sysctl_set(&ns->set, &set_root, set_is_seen); | ||
56 | tbl = kmemdup(userns_table, sizeof(userns_table), GFP_KERNEL); | ||
57 | if (tbl) { | ||
58 | ns->sysctls = __register_sysctl_table(&ns->set, "userns", tbl); | ||
59 | } | ||
60 | if (!ns->sysctls) { | ||
61 | kfree(tbl); | ||
62 | retire_sysctl_set(&ns->set); | ||
63 | return false; | ||
64 | } | ||
65 | #endif | ||
66 | return true; | ||
67 | } | ||
68 | |||
69 | void retire_userns_sysctls(struct user_namespace *ns) | ||
70 | { | ||
71 | #ifdef CONFIG_SYSCTL | ||
72 | struct ctl_table *tbl; | ||
73 | |||
74 | tbl = ns->sysctls->ctl_table_arg; | ||
75 | unregister_sysctl_table(ns->sysctls); | ||
76 | retire_sysctl_set(&ns->set); | ||
77 | kfree(tbl); | ||
78 | #endif | ||
79 | } | ||
80 | |||
81 | static __init int user_namespace_sysctl_init(void) | ||
82 | { | ||
83 | #ifdef CONFIG_SYSCTL | ||
84 | static struct ctl_table_header *userns_header; | ||
85 | static struct ctl_table empty[1]; | ||
86 | /* | ||
87 | * It is necessary to register the userns directory in the | ||
88 | * default set so that registrations in the child sets work | ||
89 | * properly. | ||
90 | */ | ||
91 | userns_header = register_sysctl("userns", empty); | ||
92 | BUG_ON(!userns_header); | ||
93 | BUG_ON(!setup_userns_sysctls(&init_user_ns)); | ||
94 | #endif | ||
95 | return 0; | ||
96 | } | ||
97 | subsys_initcall(user_namespace_sysctl_init); | ||
98 | |||
99 | |||
diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c index 5247cdb24e62..a63332253c7e 100644 --- a/kernel/user_namespace.c +++ b/kernel/user_namespace.c | |||
@@ -23,6 +23,9 @@ | |||
23 | #include <linux/projid.h> | 23 | #include <linux/projid.h> |
24 | #include <linux/fs_struct.h> | 24 | #include <linux/fs_struct.h> |
25 | 25 | ||
26 | extern bool setup_userns_sysctls(struct user_namespace *ns); | ||
27 | extern void retire_userns_sysctls(struct user_namespace *ns); | ||
28 | |||
26 | static struct kmem_cache *user_ns_cachep __read_mostly; | 29 | static struct kmem_cache *user_ns_cachep __read_mostly; |
27 | static DEFINE_MUTEX(userns_state_mutex); | 30 | static DEFINE_MUTEX(userns_state_mutex); |
28 | 31 | ||
@@ -109,12 +112,22 @@ int create_user_ns(struct cred *new) | |||
109 | ns->flags = parent_ns->flags; | 112 | ns->flags = parent_ns->flags; |
110 | mutex_unlock(&userns_state_mutex); | 113 | mutex_unlock(&userns_state_mutex); |
111 | 114 | ||
112 | set_cred_user_ns(new, ns); | ||
113 | |||
114 | #ifdef CONFIG_PERSISTENT_KEYRINGS | 115 | #ifdef CONFIG_PERSISTENT_KEYRINGS |
115 | init_rwsem(&ns->persistent_keyring_register_sem); | 116 | init_rwsem(&ns->persistent_keyring_register_sem); |
116 | #endif | 117 | #endif |
118 | ret = -ENOMEM; | ||
119 | if (!setup_userns_sysctls(ns)) | ||
120 | goto fail_keyring; | ||
121 | |||
122 | set_cred_user_ns(new, ns); | ||
117 | return 0; | 123 | return 0; |
124 | fail_keyring: | ||
125 | #ifdef CONFIG_PERSISTENT_KEYRINGS | ||
126 | key_put(ns->persistent_keyring_register); | ||
127 | #endif | ||
128 | ns_free_inum(&ns->ns); | ||
129 | kmem_cache_free(user_ns_cachep, ns); | ||
130 | return ret; | ||
118 | } | 131 | } |
119 | 132 | ||
120 | int unshare_userns(unsigned long unshare_flags, struct cred **new_cred) | 133 | int unshare_userns(unsigned long unshare_flags, struct cred **new_cred) |
@@ -144,6 +157,7 @@ static void free_user_ns(struct work_struct *work) | |||
144 | 157 | ||
145 | do { | 158 | do { |
146 | parent = ns->parent; | 159 | parent = ns->parent; |
160 | retire_userns_sysctls(ns); | ||
147 | #ifdef CONFIG_PERSISTENT_KEYRINGS | 161 | #ifdef CONFIG_PERSISTENT_KEYRINGS |
148 | key_put(ns->persistent_keyring_register); | 162 | key_put(ns->persistent_keyring_register); |
149 | #endif | 163 | #endif |