xref: /linux/kernel/ucount.c (revision e4aebf06695c32d49f1007f9d252f97b5b2998a7)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include <linux/stat.h>
4 #include <linux/sysctl.h>
5 #include <linux/slab.h>
6 #include <linux/cred.h>
7 #include <linux/hash.h>
8 #include <linux/kmemleak.h>
9 #include <linux/user_namespace.h>
10 
11 struct ucounts init_ucounts = {
12 	.ns    = &init_user_ns,
13 	.uid   = GLOBAL_ROOT_UID,
14 	.count = ATOMIC_INIT(1),
15 };
16 
17 #define UCOUNTS_HASHTABLE_BITS 10
18 static struct hlist_head ucounts_hashtable[(1 << UCOUNTS_HASHTABLE_BITS)];
19 static DEFINE_SPINLOCK(ucounts_lock);
20 
21 #define ucounts_hashfn(ns, uid)						\
22 	hash_long((unsigned long)__kuid_val(uid) + (unsigned long)(ns), \
23 		  UCOUNTS_HASHTABLE_BITS)
24 #define ucounts_hashentry(ns, uid)	\
25 	(ucounts_hashtable + ucounts_hashfn(ns, uid))
26 
27 
28 #ifdef CONFIG_SYSCTL
29 static struct ctl_table_set *
30 set_lookup(struct ctl_table_root *root)
31 {
32 	return &current_user_ns()->set;
33 }
34 
35 static int set_is_seen(struct ctl_table_set *set)
36 {
37 	return &current_user_ns()->set == set;
38 }
39 
40 static int set_permissions(struct ctl_table_header *head,
41 				  struct ctl_table *table)
42 {
43 	struct user_namespace *user_ns =
44 		container_of(head->set, struct user_namespace, set);
45 	int mode;
46 
47 	/* Allow users with CAP_SYS_RESOURCE unrestrained access */
48 	if (ns_capable(user_ns, CAP_SYS_RESOURCE))
49 		mode = (table->mode & S_IRWXU) >> 6;
50 	else
51 	/* Allow all others at most read-only access */
52 		mode = table->mode & S_IROTH;
53 	return (mode << 6) | (mode << 3) | mode;
54 }
55 
56 static struct ctl_table_root set_root = {
57 	.lookup = set_lookup,
58 	.permissions = set_permissions,
59 };
60 
61 #define UCOUNT_ENTRY(name)				\
62 	{						\
63 		.procname	= name,			\
64 		.maxlen		= sizeof(int),		\
65 		.mode		= 0644,			\
66 		.proc_handler	= proc_dointvec_minmax,	\
67 		.extra1		= SYSCTL_ZERO,		\
68 		.extra2		= SYSCTL_INT_MAX,	\
69 	}
70 static struct ctl_table user_table[] = {
71 	UCOUNT_ENTRY("max_user_namespaces"),
72 	UCOUNT_ENTRY("max_pid_namespaces"),
73 	UCOUNT_ENTRY("max_uts_namespaces"),
74 	UCOUNT_ENTRY("max_ipc_namespaces"),
75 	UCOUNT_ENTRY("max_net_namespaces"),
76 	UCOUNT_ENTRY("max_mnt_namespaces"),
77 	UCOUNT_ENTRY("max_cgroup_namespaces"),
78 	UCOUNT_ENTRY("max_time_namespaces"),
79 #ifdef CONFIG_INOTIFY_USER
80 	UCOUNT_ENTRY("max_inotify_instances"),
81 	UCOUNT_ENTRY("max_inotify_watches"),
82 #endif
83 	{ },
84 	{ },
85 	{ },
86 	{ },
87 	{ }
88 };
89 #endif /* CONFIG_SYSCTL */
90 
91 bool setup_userns_sysctls(struct user_namespace *ns)
92 {
93 #ifdef CONFIG_SYSCTL
94 	struct ctl_table *tbl;
95 
96 	BUILD_BUG_ON(ARRAY_SIZE(user_table) != UCOUNT_COUNTS + 1);
97 	setup_sysctl_set(&ns->set, &set_root, set_is_seen);
98 	tbl = kmemdup(user_table, sizeof(user_table), GFP_KERNEL);
99 	if (tbl) {
100 		int i;
101 		for (i = 0; i < UCOUNT_COUNTS; i++) {
102 			tbl[i].data = &ns->ucount_max[i];
103 		}
104 		ns->sysctls = __register_sysctl_table(&ns->set, "user", tbl);
105 	}
106 	if (!ns->sysctls) {
107 		kfree(tbl);
108 		retire_sysctl_set(&ns->set);
109 		return false;
110 	}
111 #endif
112 	return true;
113 }
114 
115 void retire_userns_sysctls(struct user_namespace *ns)
116 {
117 #ifdef CONFIG_SYSCTL
118 	struct ctl_table *tbl;
119 
120 	tbl = ns->sysctls->ctl_table_arg;
121 	unregister_sysctl_table(ns->sysctls);
122 	retire_sysctl_set(&ns->set);
123 	kfree(tbl);
124 #endif
125 }
126 
127 static struct ucounts *find_ucounts(struct user_namespace *ns, kuid_t uid, struct hlist_head *hashent)
128 {
129 	struct ucounts *ucounts;
130 
131 	hlist_for_each_entry(ucounts, hashent, node) {
132 		if (uid_eq(ucounts->uid, uid) && (ucounts->ns == ns))
133 			return ucounts;
134 	}
135 	return NULL;
136 }
137 
138 static void hlist_add_ucounts(struct ucounts *ucounts)
139 {
140 	struct hlist_head *hashent = ucounts_hashentry(ucounts->ns, ucounts->uid);
141 	spin_lock_irq(&ucounts_lock);
142 	hlist_add_head(&ucounts->node, hashent);
143 	spin_unlock_irq(&ucounts_lock);
144 }
145 
146 struct ucounts *get_ucounts(struct ucounts *ucounts)
147 {
148 	if (ucounts && atomic_add_negative(1, &ucounts->count)) {
149 		put_ucounts(ucounts);
150 		ucounts = NULL;
151 	}
152 	return ucounts;
153 }
154 
155 struct ucounts *alloc_ucounts(struct user_namespace *ns, kuid_t uid)
156 {
157 	struct hlist_head *hashent = ucounts_hashentry(ns, uid);
158 	struct ucounts *ucounts, *new;
159 
160 	spin_lock_irq(&ucounts_lock);
161 	ucounts = find_ucounts(ns, uid, hashent);
162 	if (!ucounts) {
163 		spin_unlock_irq(&ucounts_lock);
164 
165 		new = kzalloc(sizeof(*new), GFP_KERNEL);
166 		if (!new)
167 			return NULL;
168 
169 		new->ns = ns;
170 		new->uid = uid;
171 		atomic_set(&new->count, 1);
172 
173 		spin_lock_irq(&ucounts_lock);
174 		ucounts = find_ucounts(ns, uid, hashent);
175 		if (ucounts) {
176 			kfree(new);
177 		} else {
178 			hlist_add_head(&new->node, hashent);
179 			spin_unlock_irq(&ucounts_lock);
180 			return new;
181 		}
182 	}
183 	spin_unlock_irq(&ucounts_lock);
184 	ucounts = get_ucounts(ucounts);
185 	return ucounts;
186 }
187 
188 void put_ucounts(struct ucounts *ucounts)
189 {
190 	unsigned long flags;
191 
192 	if (atomic_dec_and_test(&ucounts->count)) {
193 		spin_lock_irqsave(&ucounts_lock, flags);
194 		hlist_del_init(&ucounts->node);
195 		spin_unlock_irqrestore(&ucounts_lock, flags);
196 		kfree(ucounts);
197 	}
198 }
199 
200 static inline bool atomic_long_inc_below(atomic_long_t *v, int u)
201 {
202 	long c, old;
203 	c = atomic_long_read(v);
204 	for (;;) {
205 		if (unlikely(c >= u))
206 			return false;
207 		old = atomic_long_cmpxchg(v, c, c+1);
208 		if (likely(old == c))
209 			return true;
210 		c = old;
211 	}
212 }
213 
214 struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid,
215 			   enum ucount_type type)
216 {
217 	struct ucounts *ucounts, *iter, *bad;
218 	struct user_namespace *tns;
219 	ucounts = alloc_ucounts(ns, uid);
220 	for (iter = ucounts; iter; iter = tns->ucounts) {
221 		long max;
222 		tns = iter->ns;
223 		max = READ_ONCE(tns->ucount_max[type]);
224 		if (!atomic_long_inc_below(&iter->ucount[type], max))
225 			goto fail;
226 	}
227 	return ucounts;
228 fail:
229 	bad = iter;
230 	for (iter = ucounts; iter != bad; iter = iter->ns->ucounts)
231 		atomic_long_dec(&iter->ucount[type]);
232 
233 	put_ucounts(ucounts);
234 	return NULL;
235 }
236 
237 void dec_ucount(struct ucounts *ucounts, enum ucount_type type)
238 {
239 	struct ucounts *iter;
240 	for (iter = ucounts; iter; iter = iter->ns->ucounts) {
241 		long dec = atomic_long_dec_if_positive(&iter->ucount[type]);
242 		WARN_ON_ONCE(dec < 0);
243 	}
244 	put_ucounts(ucounts);
245 }
246 
247 long inc_rlimit_ucounts(struct ucounts *ucounts, enum ucount_type type, long v)
248 {
249 	struct ucounts *iter;
250 	long ret = 0;
251 
252 	for (iter = ucounts; iter; iter = iter->ns->ucounts) {
253 		long max = READ_ONCE(iter->ns->ucount_max[type]);
254 		long new = atomic_long_add_return(v, &iter->ucount[type]);
255 		if (new < 0 || new > max)
256 			ret = LONG_MAX;
257 		else if (iter == ucounts)
258 			ret = new;
259 	}
260 	return ret;
261 }
262 
263 bool dec_rlimit_ucounts(struct ucounts *ucounts, enum ucount_type type, long v)
264 {
265 	struct ucounts *iter;
266 	long new;
267 	for (iter = ucounts; iter; iter = iter->ns->ucounts) {
268 		long dec = atomic_long_add_return(-v, &iter->ucount[type]);
269 		WARN_ON_ONCE(dec < 0);
270 		if (iter == ucounts)
271 			new = dec;
272 	}
273 	return (new == 0);
274 }
275 
276 bool is_ucounts_overlimit(struct ucounts *ucounts, enum ucount_type type, unsigned long max)
277 {
278 	struct ucounts *iter;
279 	if (get_ucounts_value(ucounts, type) > max)
280 		return true;
281 	for (iter = ucounts; iter; iter = iter->ns->ucounts) {
282 		max = READ_ONCE(iter->ns->ucount_max[type]);
283 		if (get_ucounts_value(iter, type) > max)
284 			return true;
285 	}
286 	return false;
287 }
288 
289 static __init int user_namespace_sysctl_init(void)
290 {
291 #ifdef CONFIG_SYSCTL
292 	static struct ctl_table_header *user_header;
293 	static struct ctl_table empty[1];
294 	/*
295 	 * It is necessary to register the user directory in the
296 	 * default set so that registrations in the child sets work
297 	 * properly.
298 	 */
299 	user_header = register_sysctl("user", empty);
300 	kmemleak_ignore(user_header);
301 	BUG_ON(!user_header);
302 	BUG_ON(!setup_userns_sysctls(&init_user_ns));
303 #endif
304 	hlist_add_ucounts(&init_ucounts);
305 	inc_rlimit_ucounts(&init_ucounts, UCOUNT_RLIMIT_NPROC, 1);
306 	return 0;
307 }
308 subsys_initcall(user_namespace_sysctl_init);
309