xref: /openbmc/linux/kernel/ucount.c (revision 345daff2e994ee844d6a609c37f085695fbb4c4d)
1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2dbec2846SEric W. Biederman 
3dbec2846SEric W. Biederman #include <linux/stat.h>
4dbec2846SEric W. Biederman #include <linux/sysctl.h>
5dbec2846SEric W. Biederman #include <linux/slab.h>
65b825c3aSIngo Molnar #include <linux/cred.h>
7f6b2db1aSEric W. Biederman #include <linux/hash.h>
8514c6032SRandy Dunlap #include <linux/kmemleak.h>
9dbec2846SEric W. Biederman #include <linux/user_namespace.h>
10dbec2846SEric W. Biederman 
11905ae01cSAlexey Gladkov struct ucounts init_ucounts = {
12905ae01cSAlexey Gladkov 	.ns    = &init_user_ns,
13905ae01cSAlexey Gladkov 	.uid   = GLOBAL_ROOT_UID,
14b6c33652SAlexey Gladkov 	.count = ATOMIC_INIT(1),
15905ae01cSAlexey Gladkov };
16905ae01cSAlexey Gladkov 
17f6b2db1aSEric W. Biederman #define UCOUNTS_HASHTABLE_BITS 10
18f6b2db1aSEric W. Biederman static struct hlist_head ucounts_hashtable[(1 << UCOUNTS_HASHTABLE_BITS)];
19f6b2db1aSEric W. Biederman static DEFINE_SPINLOCK(ucounts_lock);
20f6b2db1aSEric W. Biederman 
21f6b2db1aSEric W. Biederman #define ucounts_hashfn(ns, uid)						\
22f6b2db1aSEric W. Biederman 	hash_long((unsigned long)__kuid_val(uid) + (unsigned long)(ns), \
23f6b2db1aSEric W. Biederman 		  UCOUNTS_HASHTABLE_BITS)
24f6b2db1aSEric W. Biederman #define ucounts_hashentry(ns, uid)	\
25f6b2db1aSEric W. Biederman 	(ucounts_hashtable + ucounts_hashfn(ns, uid))
26f6b2db1aSEric W. Biederman 
27f6b2db1aSEric W. Biederman 
28dbec2846SEric W. Biederman #ifdef CONFIG_SYSCTL
29dbec2846SEric W. Biederman static struct ctl_table_set *
30dbec2846SEric W. Biederman set_lookup(struct ctl_table_root *root)
31dbec2846SEric W. Biederman {
32dbec2846SEric W. Biederman 	return &current_user_ns()->set;
33dbec2846SEric W. Biederman }
34dbec2846SEric W. Biederman 
35dbec2846SEric W. Biederman static int set_is_seen(struct ctl_table_set *set)
36dbec2846SEric W. Biederman {
37dbec2846SEric W. Biederman 	return &current_user_ns()->set == set;
38dbec2846SEric W. Biederman }
39dbec2846SEric W. Biederman 
40dbec2846SEric W. Biederman static int set_permissions(struct ctl_table_header *head,
41dbec2846SEric W. Biederman 				  struct ctl_table *table)
42dbec2846SEric W. Biederman {
43dbec2846SEric W. Biederman 	struct user_namespace *user_ns =
44dbec2846SEric W. Biederman 		container_of(head->set, struct user_namespace, set);
45dbec2846SEric W. Biederman 	int mode;
46dbec2846SEric W. Biederman 
47dbec2846SEric W. Biederman 	/* Allow users with CAP_SYS_RESOURCE unrestrained access */
48dbec2846SEric W. Biederman 	if (ns_capable(user_ns, CAP_SYS_RESOURCE))
49dbec2846SEric W. Biederman 		mode = (table->mode & S_IRWXU) >> 6;
50dbec2846SEric W. Biederman 	else
51dbec2846SEric W. Biederman 	/* Allow all others at most read-only access */
52dbec2846SEric W. Biederman 		mode = table->mode & S_IROTH;
53dbec2846SEric W. Biederman 	return (mode << 6) | (mode << 3) | mode;
54dbec2846SEric W. Biederman }
55dbec2846SEric W. Biederman 
56dbec2846SEric W. Biederman static struct ctl_table_root set_root = {
57dbec2846SEric W. Biederman 	.lookup = set_lookup,
58dbec2846SEric W. Biederman 	.permissions = set_permissions,
59dbec2846SEric W. Biederman };
60dbec2846SEric W. Biederman 
6125f9c081SEric W. Biederman #define UCOUNT_ENTRY(name)				\
6225f9c081SEric W. Biederman 	{						\
6325f9c081SEric W. Biederman 		.procname	= name,			\
6425f9c081SEric W. Biederman 		.maxlen		= sizeof(int),		\
6525f9c081SEric W. Biederman 		.mode		= 0644,			\
6625f9c081SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,	\
67eec4844fSMatteo Croce 		.extra1		= SYSCTL_ZERO,		\
68eec4844fSMatteo Croce 		.extra2		= SYSCTL_INT_MAX,	\
6925f9c081SEric W. Biederman 	}
70f6b2db1aSEric W. Biederman static struct ctl_table user_table[] = {
7125f9c081SEric W. Biederman 	UCOUNT_ENTRY("max_user_namespaces"),
72f333c700SEric W. Biederman 	UCOUNT_ENTRY("max_pid_namespaces"),
73f7af3d1cSEric W. Biederman 	UCOUNT_ENTRY("max_uts_namespaces"),
74aba35661SEric W. Biederman 	UCOUNT_ENTRY("max_ipc_namespaces"),
7570328660SEric W. Biederman 	UCOUNT_ENTRY("max_net_namespaces"),
76537f7ccbSEric W. Biederman 	UCOUNT_ENTRY("max_mnt_namespaces"),
77d08311ddSEric W. Biederman 	UCOUNT_ENTRY("max_cgroup_namespaces"),
78eeec26d5SDmitry Safonov 	UCOUNT_ENTRY("max_time_namespaces"),
791cce1eeaSNikolay Borisov #ifdef CONFIG_INOTIFY_USER
801cce1eeaSNikolay Borisov 	UCOUNT_ENTRY("max_inotify_instances"),
811cce1eeaSNikolay Borisov 	UCOUNT_ENTRY("max_inotify_watches"),
821cce1eeaSNikolay Borisov #endif
835b8fea65SAmir Goldstein #ifdef CONFIG_FANOTIFY
845b8fea65SAmir Goldstein 	UCOUNT_ENTRY("max_fanotify_groups"),
855b8fea65SAmir Goldstein 	UCOUNT_ENTRY("max_fanotify_marks"),
865b8fea65SAmir Goldstein #endif
8721d1c5e3SAlexey Gladkov 	{ },
886e52a9f0SAlexey Gladkov 	{ },
89d6469690SAlexey Gladkov 	{ },
90d7c9e99aSAlexey Gladkov 	{ },
91dbec2846SEric W. Biederman 	{ }
92dbec2846SEric W. Biederman };
93dbec2846SEric W. Biederman #endif /* CONFIG_SYSCTL */
94dbec2846SEric W. Biederman 
95dbec2846SEric W. Biederman bool setup_userns_sysctls(struct user_namespace *ns)
96dbec2846SEric W. Biederman {
97dbec2846SEric W. Biederman #ifdef CONFIG_SYSCTL
98dbec2846SEric W. Biederman 	struct ctl_table *tbl;
990f538e3eSJan Kara 
1000f538e3eSJan Kara 	BUILD_BUG_ON(ARRAY_SIZE(user_table) != UCOUNT_COUNTS + 1);
101dbec2846SEric W. Biederman 	setup_sysctl_set(&ns->set, &set_root, set_is_seen);
102f6b2db1aSEric W. Biederman 	tbl = kmemdup(user_table, sizeof(user_table), GFP_KERNEL);
103dbec2846SEric W. Biederman 	if (tbl) {
10425f9c081SEric W. Biederman 		int i;
10525f9c081SEric W. Biederman 		for (i = 0; i < UCOUNT_COUNTS; i++) {
10625f9c081SEric W. Biederman 			tbl[i].data = &ns->ucount_max[i];
10725f9c081SEric W. Biederman 		}
108f6b2db1aSEric W. Biederman 		ns->sysctls = __register_sysctl_table(&ns->set, "user", tbl);
109dbec2846SEric W. Biederman 	}
110dbec2846SEric W. Biederman 	if (!ns->sysctls) {
111dbec2846SEric W. Biederman 		kfree(tbl);
112dbec2846SEric W. Biederman 		retire_sysctl_set(&ns->set);
113dbec2846SEric W. Biederman 		return false;
114dbec2846SEric W. Biederman 	}
115dbec2846SEric W. Biederman #endif
116dbec2846SEric W. Biederman 	return true;
117dbec2846SEric W. Biederman }
118dbec2846SEric W. Biederman 
119dbec2846SEric W. Biederman void retire_userns_sysctls(struct user_namespace *ns)
120dbec2846SEric W. Biederman {
121dbec2846SEric W. Biederman #ifdef CONFIG_SYSCTL
122dbec2846SEric W. Biederman 	struct ctl_table *tbl;
123dbec2846SEric W. Biederman 
124dbec2846SEric W. Biederman 	tbl = ns->sysctls->ctl_table_arg;
125dbec2846SEric W. Biederman 	unregister_sysctl_table(ns->sysctls);
126dbec2846SEric W. Biederman 	retire_sysctl_set(&ns->set);
127dbec2846SEric W. Biederman 	kfree(tbl);
128dbec2846SEric W. Biederman #endif
129dbec2846SEric W. Biederman }
130dbec2846SEric W. Biederman 
131f6b2db1aSEric W. Biederman static struct ucounts *find_ucounts(struct user_namespace *ns, kuid_t uid, struct hlist_head *hashent)
132f6b2db1aSEric W. Biederman {
133f6b2db1aSEric W. Biederman 	struct ucounts *ucounts;
134f6b2db1aSEric W. Biederman 
135f6b2db1aSEric W. Biederman 	hlist_for_each_entry(ucounts, hashent, node) {
136f6b2db1aSEric W. Biederman 		if (uid_eq(ucounts->uid, uid) && (ucounts->ns == ns))
137f6b2db1aSEric W. Biederman 			return ucounts;
138f6b2db1aSEric W. Biederman 	}
139f6b2db1aSEric W. Biederman 	return NULL;
140f6b2db1aSEric W. Biederman }
141f6b2db1aSEric W. Biederman 
142905ae01cSAlexey Gladkov static void hlist_add_ucounts(struct ucounts *ucounts)
143905ae01cSAlexey Gladkov {
144905ae01cSAlexey Gladkov 	struct hlist_head *hashent = ucounts_hashentry(ucounts->ns, ucounts->uid);
145905ae01cSAlexey Gladkov 	spin_lock_irq(&ucounts_lock);
146905ae01cSAlexey Gladkov 	hlist_add_head(&ucounts->node, hashent);
147905ae01cSAlexey Gladkov 	spin_unlock_irq(&ucounts_lock);
148905ae01cSAlexey Gladkov }
149905ae01cSAlexey Gladkov 
150b6c33652SAlexey Gladkov struct ucounts *get_ucounts(struct ucounts *ucounts)
151b6c33652SAlexey Gladkov {
152b6c33652SAlexey Gladkov 	if (ucounts && atomic_add_negative(1, &ucounts->count)) {
153b6c33652SAlexey Gladkov 		put_ucounts(ucounts);
154b6c33652SAlexey Gladkov 		ucounts = NULL;
155b6c33652SAlexey Gladkov 	}
156b6c33652SAlexey Gladkov 	return ucounts;
157b6c33652SAlexey Gladkov }
158b6c33652SAlexey Gladkov 
159905ae01cSAlexey Gladkov struct ucounts *alloc_ucounts(struct user_namespace *ns, kuid_t uid)
160f6b2db1aSEric W. Biederman {
161f6b2db1aSEric W. Biederman 	struct hlist_head *hashent = ucounts_hashentry(ns, uid);
162f6b2db1aSEric W. Biederman 	struct ucounts *ucounts, *new;
163*345daff2SAlexey Gladkov 	long overflow;
164f6b2db1aSEric W. Biederman 
165880a3854SNikolay Borisov 	spin_lock_irq(&ucounts_lock);
166f6b2db1aSEric W. Biederman 	ucounts = find_ucounts(ns, uid, hashent);
167f6b2db1aSEric W. Biederman 	if (!ucounts) {
168880a3854SNikolay Borisov 		spin_unlock_irq(&ucounts_lock);
169f6b2db1aSEric W. Biederman 
170f6b2db1aSEric W. Biederman 		new = kzalloc(sizeof(*new), GFP_KERNEL);
171f6b2db1aSEric W. Biederman 		if (!new)
172f6b2db1aSEric W. Biederman 			return NULL;
173f6b2db1aSEric W. Biederman 
174f6b2db1aSEric W. Biederman 		new->ns = ns;
175f6b2db1aSEric W. Biederman 		new->uid = uid;
176b6c33652SAlexey Gladkov 		atomic_set(&new->count, 1);
177f6b2db1aSEric W. Biederman 
178880a3854SNikolay Borisov 		spin_lock_irq(&ucounts_lock);
179f6b2db1aSEric W. Biederman 		ucounts = find_ucounts(ns, uid, hashent);
180f6b2db1aSEric W. Biederman 		if (ucounts) {
181f6b2db1aSEric W. Biederman 			kfree(new);
182f6b2db1aSEric W. Biederman 		} else {
183f6b2db1aSEric W. Biederman 			hlist_add_head(&new->node, hashent);
184880a3854SNikolay Borisov 			spin_unlock_irq(&ucounts_lock);
185b6c33652SAlexey Gladkov 			return new;
186f6b2db1aSEric W. Biederman 		}
187905ae01cSAlexey Gladkov 	}
188*345daff2SAlexey Gladkov 	overflow = atomic_add_negative(1, &ucounts->count);
189b6c33652SAlexey Gladkov 	spin_unlock_irq(&ucounts_lock);
190*345daff2SAlexey Gladkov 	if (overflow) {
191*345daff2SAlexey Gladkov 		put_ucounts(ucounts);
192*345daff2SAlexey Gladkov 		return NULL;
193*345daff2SAlexey Gladkov 	}
194f6b2db1aSEric W. Biederman 	return ucounts;
195f6b2db1aSEric W. Biederman }
196f6b2db1aSEric W. Biederman 
197905ae01cSAlexey Gladkov void put_ucounts(struct ucounts *ucounts)
198f6b2db1aSEric W. Biederman {
199880a3854SNikolay Borisov 	unsigned long flags;
200880a3854SNikolay Borisov 
201*345daff2SAlexey Gladkov 	if (atomic_dec_and_lock_irqsave(&ucounts->count, &ucounts_lock, flags)) {
202f6b2db1aSEric W. Biederman 		hlist_del_init(&ucounts->node);
203880a3854SNikolay Borisov 		spin_unlock_irqrestore(&ucounts_lock, flags);
204f6b2db1aSEric W. Biederman 		kfree(ucounts);
205f6b2db1aSEric W. Biederman 	}
206b6c33652SAlexey Gladkov }
207f6b2db1aSEric W. Biederman 
208f9c82a4eSAlexey Gladkov static inline bool atomic_long_inc_below(atomic_long_t *v, int u)
209b376c3e1SEric W. Biederman {
210f9c82a4eSAlexey Gladkov 	long c, old;
211f9c82a4eSAlexey Gladkov 	c = atomic_long_read(v);
212b376c3e1SEric W. Biederman 	for (;;) {
213b376c3e1SEric W. Biederman 		if (unlikely(c >= u))
214b376c3e1SEric W. Biederman 			return false;
215f9c82a4eSAlexey Gladkov 		old = atomic_long_cmpxchg(v, c, c+1);
216b376c3e1SEric W. Biederman 		if (likely(old == c))
217b376c3e1SEric W. Biederman 			return true;
218b376c3e1SEric W. Biederman 		c = old;
219b376c3e1SEric W. Biederman 	}
220b376c3e1SEric W. Biederman }
221b376c3e1SEric W. Biederman 
22225f9c081SEric W. Biederman struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid,
22325f9c081SEric W. Biederman 			   enum ucount_type type)
224b376c3e1SEric W. Biederman {
225f6b2db1aSEric W. Biederman 	struct ucounts *ucounts, *iter, *bad;
226f6b2db1aSEric W. Biederman 	struct user_namespace *tns;
227905ae01cSAlexey Gladkov 	ucounts = alloc_ucounts(ns, uid);
228f6b2db1aSEric W. Biederman 	for (iter = ucounts; iter; iter = tns->ucounts) {
229f9c82a4eSAlexey Gladkov 		long max;
230f6b2db1aSEric W. Biederman 		tns = iter->ns;
23125f9c081SEric W. Biederman 		max = READ_ONCE(tns->ucount_max[type]);
232f9c82a4eSAlexey Gladkov 		if (!atomic_long_inc_below(&iter->ucount[type], max))
233b376c3e1SEric W. Biederman 			goto fail;
234b376c3e1SEric W. Biederman 	}
235f6b2db1aSEric W. Biederman 	return ucounts;
236b376c3e1SEric W. Biederman fail:
237f6b2db1aSEric W. Biederman 	bad = iter;
238f6b2db1aSEric W. Biederman 	for (iter = ucounts; iter != bad; iter = iter->ns->ucounts)
239f9c82a4eSAlexey Gladkov 		atomic_long_dec(&iter->ucount[type]);
240b376c3e1SEric W. Biederman 
241f6b2db1aSEric W. Biederman 	put_ucounts(ucounts);
242f6b2db1aSEric W. Biederman 	return NULL;
243b376c3e1SEric W. Biederman }
244b376c3e1SEric W. Biederman 
24525f9c081SEric W. Biederman void dec_ucount(struct ucounts *ucounts, enum ucount_type type)
246b376c3e1SEric W. Biederman {
247f6b2db1aSEric W. Biederman 	struct ucounts *iter;
248f6b2db1aSEric W. Biederman 	for (iter = ucounts; iter; iter = iter->ns->ucounts) {
249f9c82a4eSAlexey Gladkov 		long dec = atomic_long_dec_if_positive(&iter->ucount[type]);
250b376c3e1SEric W. Biederman 		WARN_ON_ONCE(dec < 0);
251b376c3e1SEric W. Biederman 	}
252f6b2db1aSEric W. Biederman 	put_ucounts(ucounts);
253b376c3e1SEric W. Biederman }
254b376c3e1SEric W. Biederman 
25521d1c5e3SAlexey Gladkov long inc_rlimit_ucounts(struct ucounts *ucounts, enum ucount_type type, long v)
25621d1c5e3SAlexey Gladkov {
25721d1c5e3SAlexey Gladkov 	struct ucounts *iter;
25821d1c5e3SAlexey Gladkov 	long ret = 0;
25921d1c5e3SAlexey Gladkov 
26021d1c5e3SAlexey Gladkov 	for (iter = ucounts; iter; iter = iter->ns->ucounts) {
26121d1c5e3SAlexey Gladkov 		long max = READ_ONCE(iter->ns->ucount_max[type]);
26221d1c5e3SAlexey Gladkov 		long new = atomic_long_add_return(v, &iter->ucount[type]);
26321d1c5e3SAlexey Gladkov 		if (new < 0 || new > max)
26421d1c5e3SAlexey Gladkov 			ret = LONG_MAX;
26521d1c5e3SAlexey Gladkov 		else if (iter == ucounts)
26621d1c5e3SAlexey Gladkov 			ret = new;
26721d1c5e3SAlexey Gladkov 	}
26821d1c5e3SAlexey Gladkov 	return ret;
26921d1c5e3SAlexey Gladkov }
27021d1c5e3SAlexey Gladkov 
27121d1c5e3SAlexey Gladkov bool dec_rlimit_ucounts(struct ucounts *ucounts, enum ucount_type type, long v)
27221d1c5e3SAlexey Gladkov {
27321d1c5e3SAlexey Gladkov 	struct ucounts *iter;
274f928ef68SEric W. Biederman 	long new = -1; /* Silence compiler warning */
27521d1c5e3SAlexey Gladkov 	for (iter = ucounts; iter; iter = iter->ns->ucounts) {
27621d1c5e3SAlexey Gladkov 		long dec = atomic_long_add_return(-v, &iter->ucount[type]);
27721d1c5e3SAlexey Gladkov 		WARN_ON_ONCE(dec < 0);
27821d1c5e3SAlexey Gladkov 		if (iter == ucounts)
27921d1c5e3SAlexey Gladkov 			new = dec;
28021d1c5e3SAlexey Gladkov 	}
28121d1c5e3SAlexey Gladkov 	return (new == 0);
28221d1c5e3SAlexey Gladkov }
28321d1c5e3SAlexey Gladkov 
28421d1c5e3SAlexey Gladkov bool is_ucounts_overlimit(struct ucounts *ucounts, enum ucount_type type, unsigned long max)
28521d1c5e3SAlexey Gladkov {
28621d1c5e3SAlexey Gladkov 	struct ucounts *iter;
28721d1c5e3SAlexey Gladkov 	if (get_ucounts_value(ucounts, type) > max)
28821d1c5e3SAlexey Gladkov 		return true;
28921d1c5e3SAlexey Gladkov 	for (iter = ucounts; iter; iter = iter->ns->ucounts) {
29021d1c5e3SAlexey Gladkov 		max = READ_ONCE(iter->ns->ucount_max[type]);
29121d1c5e3SAlexey Gladkov 		if (get_ucounts_value(iter, type) > max)
29221d1c5e3SAlexey Gladkov 			return true;
29321d1c5e3SAlexey Gladkov 	}
29421d1c5e3SAlexey Gladkov 	return false;
29521d1c5e3SAlexey Gladkov }
29621d1c5e3SAlexey Gladkov 
297dbec2846SEric W. Biederman static __init int user_namespace_sysctl_init(void)
298dbec2846SEric W. Biederman {
299dbec2846SEric W. Biederman #ifdef CONFIG_SYSCTL
300f6b2db1aSEric W. Biederman 	static struct ctl_table_header *user_header;
301dbec2846SEric W. Biederman 	static struct ctl_table empty[1];
302dbec2846SEric W. Biederman 	/*
303f6b2db1aSEric W. Biederman 	 * It is necessary to register the user directory in the
304dbec2846SEric W. Biederman 	 * default set so that registrations in the child sets work
305dbec2846SEric W. Biederman 	 * properly.
306dbec2846SEric W. Biederman 	 */
307f6b2db1aSEric W. Biederman 	user_header = register_sysctl("user", empty);
308ed5bd7dcSLuis R. Rodriguez 	kmemleak_ignore(user_header);
309f6b2db1aSEric W. Biederman 	BUG_ON(!user_header);
310dbec2846SEric W. Biederman 	BUG_ON(!setup_userns_sysctls(&init_user_ns));
311dbec2846SEric W. Biederman #endif
312905ae01cSAlexey Gladkov 	hlist_add_ucounts(&init_ucounts);
31321d1c5e3SAlexey Gladkov 	inc_rlimit_ucounts(&init_ucounts, UCOUNT_RLIMIT_NPROC, 1);
314dbec2846SEric W. Biederman 	return 0;
315dbec2846SEric W. Biederman }
316dbec2846SEric W. Biederman subsys_initcall(user_namespace_sysctl_init);
317