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 /kernel/ucount.c | |
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>
Diffstat (limited to 'kernel/ucount.c')
-rw-r--r-- | kernel/ucount.c | 99 |
1 files changed, 99 insertions, 0 deletions
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 | |||