xref: /openbmc/linux/kernel/ucount.c (revision f9c82a4ea89c384d49ce03768ba88d049ed3f1f0)
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 
11f6b2db1aSEric W. Biederman #define UCOUNTS_HASHTABLE_BITS 10
12f6b2db1aSEric W. Biederman static struct hlist_head ucounts_hashtable[(1 << UCOUNTS_HASHTABLE_BITS)];
13f6b2db1aSEric W. Biederman static DEFINE_SPINLOCK(ucounts_lock);
14f6b2db1aSEric W. Biederman 
15f6b2db1aSEric W. Biederman #define ucounts_hashfn(ns, uid)						\
16f6b2db1aSEric W. Biederman 	hash_long((unsigned long)__kuid_val(uid) + (unsigned long)(ns), \
17f6b2db1aSEric W. Biederman 		  UCOUNTS_HASHTABLE_BITS)
18f6b2db1aSEric W. Biederman #define ucounts_hashentry(ns, uid)	\
19f6b2db1aSEric W. Biederman 	(ucounts_hashtable + ucounts_hashfn(ns, uid))
20f6b2db1aSEric W. Biederman 
21f6b2db1aSEric W. Biederman 
22dbec2846SEric W. Biederman #ifdef CONFIG_SYSCTL
23dbec2846SEric W. Biederman static struct ctl_table_set *
24dbec2846SEric W. Biederman set_lookup(struct ctl_table_root *root)
25dbec2846SEric W. Biederman {
26dbec2846SEric W. Biederman 	return &current_user_ns()->set;
27dbec2846SEric W. Biederman }
28dbec2846SEric W. Biederman 
29dbec2846SEric W. Biederman static int set_is_seen(struct ctl_table_set *set)
30dbec2846SEric W. Biederman {
31dbec2846SEric W. Biederman 	return &current_user_ns()->set == set;
32dbec2846SEric W. Biederman }
33dbec2846SEric W. Biederman 
34dbec2846SEric W. Biederman static int set_permissions(struct ctl_table_header *head,
35dbec2846SEric W. Biederman 				  struct ctl_table *table)
36dbec2846SEric W. Biederman {
37dbec2846SEric W. Biederman 	struct user_namespace *user_ns =
38dbec2846SEric W. Biederman 		container_of(head->set, struct user_namespace, set);
39dbec2846SEric W. Biederman 	int mode;
40dbec2846SEric W. Biederman 
41dbec2846SEric W. Biederman 	/* Allow users with CAP_SYS_RESOURCE unrestrained access */
42dbec2846SEric W. Biederman 	if (ns_capable(user_ns, CAP_SYS_RESOURCE))
43dbec2846SEric W. Biederman 		mode = (table->mode & S_IRWXU) >> 6;
44dbec2846SEric W. Biederman 	else
45dbec2846SEric W. Biederman 	/* Allow all others at most read-only access */
46dbec2846SEric W. Biederman 		mode = table->mode & S_IROTH;
47dbec2846SEric W. Biederman 	return (mode << 6) | (mode << 3) | mode;
48dbec2846SEric W. Biederman }
49dbec2846SEric W. Biederman 
50dbec2846SEric W. Biederman static struct ctl_table_root set_root = {
51dbec2846SEric W. Biederman 	.lookup = set_lookup,
52dbec2846SEric W. Biederman 	.permissions = set_permissions,
53dbec2846SEric W. Biederman };
54dbec2846SEric W. Biederman 
5525f9c081SEric W. Biederman #define UCOUNT_ENTRY(name)				\
5625f9c081SEric W. Biederman 	{						\
5725f9c081SEric W. Biederman 		.procname	= name,			\
5825f9c081SEric W. Biederman 		.maxlen		= sizeof(int),		\
5925f9c081SEric W. Biederman 		.mode		= 0644,			\
6025f9c081SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,	\
61eec4844fSMatteo Croce 		.extra1		= SYSCTL_ZERO,		\
62eec4844fSMatteo Croce 		.extra2		= SYSCTL_INT_MAX,	\
6325f9c081SEric W. Biederman 	}
64f6b2db1aSEric W. Biederman static struct ctl_table user_table[] = {
6525f9c081SEric W. Biederman 	UCOUNT_ENTRY("max_user_namespaces"),
66f333c700SEric W. Biederman 	UCOUNT_ENTRY("max_pid_namespaces"),
67f7af3d1cSEric W. Biederman 	UCOUNT_ENTRY("max_uts_namespaces"),
68aba35661SEric W. Biederman 	UCOUNT_ENTRY("max_ipc_namespaces"),
6970328660SEric W. Biederman 	UCOUNT_ENTRY("max_net_namespaces"),
70537f7ccbSEric W. Biederman 	UCOUNT_ENTRY("max_mnt_namespaces"),
71d08311ddSEric W. Biederman 	UCOUNT_ENTRY("max_cgroup_namespaces"),
72eeec26d5SDmitry Safonov 	UCOUNT_ENTRY("max_time_namespaces"),
731cce1eeaSNikolay Borisov #ifdef CONFIG_INOTIFY_USER
741cce1eeaSNikolay Borisov 	UCOUNT_ENTRY("max_inotify_instances"),
751cce1eeaSNikolay Borisov 	UCOUNT_ENTRY("max_inotify_watches"),
761cce1eeaSNikolay Borisov #endif
77dbec2846SEric W. Biederman 	{ }
78dbec2846SEric W. Biederman };
79dbec2846SEric W. Biederman #endif /* CONFIG_SYSCTL */
80dbec2846SEric W. Biederman 
81dbec2846SEric W. Biederman bool setup_userns_sysctls(struct user_namespace *ns)
82dbec2846SEric W. Biederman {
83dbec2846SEric W. Biederman #ifdef CONFIG_SYSCTL
84dbec2846SEric W. Biederman 	struct ctl_table *tbl;
850f538e3eSJan Kara 
860f538e3eSJan Kara 	BUILD_BUG_ON(ARRAY_SIZE(user_table) != UCOUNT_COUNTS + 1);
87dbec2846SEric W. Biederman 	setup_sysctl_set(&ns->set, &set_root, set_is_seen);
88f6b2db1aSEric W. Biederman 	tbl = kmemdup(user_table, sizeof(user_table), GFP_KERNEL);
89dbec2846SEric W. Biederman 	if (tbl) {
9025f9c081SEric W. Biederman 		int i;
9125f9c081SEric W. Biederman 		for (i = 0; i < UCOUNT_COUNTS; i++) {
9225f9c081SEric W. Biederman 			tbl[i].data = &ns->ucount_max[i];
9325f9c081SEric W. Biederman 		}
94f6b2db1aSEric W. Biederman 		ns->sysctls = __register_sysctl_table(&ns->set, "user", tbl);
95dbec2846SEric W. Biederman 	}
96dbec2846SEric W. Biederman 	if (!ns->sysctls) {
97dbec2846SEric W. Biederman 		kfree(tbl);
98dbec2846SEric W. Biederman 		retire_sysctl_set(&ns->set);
99dbec2846SEric W. Biederman 		return false;
100dbec2846SEric W. Biederman 	}
101dbec2846SEric W. Biederman #endif
102dbec2846SEric W. Biederman 	return true;
103dbec2846SEric W. Biederman }
104dbec2846SEric W. Biederman 
105dbec2846SEric W. Biederman void retire_userns_sysctls(struct user_namespace *ns)
106dbec2846SEric W. Biederman {
107dbec2846SEric W. Biederman #ifdef CONFIG_SYSCTL
108dbec2846SEric W. Biederman 	struct ctl_table *tbl;
109dbec2846SEric W. Biederman 
110dbec2846SEric W. Biederman 	tbl = ns->sysctls->ctl_table_arg;
111dbec2846SEric W. Biederman 	unregister_sysctl_table(ns->sysctls);
112dbec2846SEric W. Biederman 	retire_sysctl_set(&ns->set);
113dbec2846SEric W. Biederman 	kfree(tbl);
114dbec2846SEric W. Biederman #endif
115dbec2846SEric W. Biederman }
116dbec2846SEric W. Biederman 
117f6b2db1aSEric W. Biederman static struct ucounts *find_ucounts(struct user_namespace *ns, kuid_t uid, struct hlist_head *hashent)
118f6b2db1aSEric W. Biederman {
119f6b2db1aSEric W. Biederman 	struct ucounts *ucounts;
120f6b2db1aSEric W. Biederman 
121f6b2db1aSEric W. Biederman 	hlist_for_each_entry(ucounts, hashent, node) {
122f6b2db1aSEric W. Biederman 		if (uid_eq(ucounts->uid, uid) && (ucounts->ns == ns))
123f6b2db1aSEric W. Biederman 			return ucounts;
124f6b2db1aSEric W. Biederman 	}
125f6b2db1aSEric W. Biederman 	return NULL;
126f6b2db1aSEric W. Biederman }
127f6b2db1aSEric W. Biederman 
128f6b2db1aSEric W. Biederman static struct ucounts *get_ucounts(struct user_namespace *ns, kuid_t uid)
129f6b2db1aSEric W. Biederman {
130f6b2db1aSEric W. Biederman 	struct hlist_head *hashent = ucounts_hashentry(ns, uid);
131f6b2db1aSEric W. Biederman 	struct ucounts *ucounts, *new;
132f6b2db1aSEric W. Biederman 
133880a3854SNikolay Borisov 	spin_lock_irq(&ucounts_lock);
134f6b2db1aSEric W. Biederman 	ucounts = find_ucounts(ns, uid, hashent);
135f6b2db1aSEric W. Biederman 	if (!ucounts) {
136880a3854SNikolay Borisov 		spin_unlock_irq(&ucounts_lock);
137f6b2db1aSEric W. Biederman 
138f6b2db1aSEric W. Biederman 		new = kzalloc(sizeof(*new), GFP_KERNEL);
139f6b2db1aSEric W. Biederman 		if (!new)
140f6b2db1aSEric W. Biederman 			return NULL;
141f6b2db1aSEric W. Biederman 
142f6b2db1aSEric W. Biederman 		new->ns = ns;
143f6b2db1aSEric W. Biederman 		new->uid = uid;
144040757f7SEric W. Biederman 		new->count = 0;
145f6b2db1aSEric W. Biederman 
146880a3854SNikolay Borisov 		spin_lock_irq(&ucounts_lock);
147f6b2db1aSEric W. Biederman 		ucounts = find_ucounts(ns, uid, hashent);
148f6b2db1aSEric W. Biederman 		if (ucounts) {
149f6b2db1aSEric W. Biederman 			kfree(new);
150f6b2db1aSEric W. Biederman 		} else {
151f6b2db1aSEric W. Biederman 			hlist_add_head(&new->node, hashent);
152f6b2db1aSEric W. Biederman 			ucounts = new;
153f6b2db1aSEric W. Biederman 		}
154f6b2db1aSEric W. Biederman 	}
155040757f7SEric W. Biederman 	if (ucounts->count == INT_MAX)
156f6b2db1aSEric W. Biederman 		ucounts = NULL;
157040757f7SEric W. Biederman 	else
158040757f7SEric W. Biederman 		ucounts->count += 1;
159880a3854SNikolay Borisov 	spin_unlock_irq(&ucounts_lock);
160f6b2db1aSEric W. Biederman 	return ucounts;
161f6b2db1aSEric W. Biederman }
162f6b2db1aSEric W. Biederman 
163f6b2db1aSEric W. Biederman static void put_ucounts(struct ucounts *ucounts)
164f6b2db1aSEric W. Biederman {
165880a3854SNikolay Borisov 	unsigned long flags;
166880a3854SNikolay Borisov 
167880a3854SNikolay Borisov 	spin_lock_irqsave(&ucounts_lock, flags);
168040757f7SEric W. Biederman 	ucounts->count -= 1;
169040757f7SEric W. Biederman 	if (!ucounts->count)
170f6b2db1aSEric W. Biederman 		hlist_del_init(&ucounts->node);
171040757f7SEric W. Biederman 	else
172040757f7SEric W. Biederman 		ucounts = NULL;
173880a3854SNikolay Borisov 	spin_unlock_irqrestore(&ucounts_lock, flags);
174f6b2db1aSEric W. Biederman 
175f6b2db1aSEric W. Biederman 	kfree(ucounts);
176f6b2db1aSEric W. Biederman }
177f6b2db1aSEric W. Biederman 
178*f9c82a4eSAlexey Gladkov static inline bool atomic_long_inc_below(atomic_long_t *v, int u)
179b376c3e1SEric W. Biederman {
180*f9c82a4eSAlexey Gladkov 	long c, old;
181*f9c82a4eSAlexey Gladkov 	c = atomic_long_read(v);
182b376c3e1SEric W. Biederman 	for (;;) {
183b376c3e1SEric W. Biederman 		if (unlikely(c >= u))
184b376c3e1SEric W. Biederman 			return false;
185*f9c82a4eSAlexey Gladkov 		old = atomic_long_cmpxchg(v, c, c+1);
186b376c3e1SEric W. Biederman 		if (likely(old == c))
187b376c3e1SEric W. Biederman 			return true;
188b376c3e1SEric W. Biederman 		c = old;
189b376c3e1SEric W. Biederman 	}
190b376c3e1SEric W. Biederman }
191b376c3e1SEric W. Biederman 
19225f9c081SEric W. Biederman struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid,
19325f9c081SEric W. Biederman 			   enum ucount_type type)
194b376c3e1SEric W. Biederman {
195f6b2db1aSEric W. Biederman 	struct ucounts *ucounts, *iter, *bad;
196f6b2db1aSEric W. Biederman 	struct user_namespace *tns;
197f6b2db1aSEric W. Biederman 	ucounts = get_ucounts(ns, uid);
198f6b2db1aSEric W. Biederman 	for (iter = ucounts; iter; iter = tns->ucounts) {
199*f9c82a4eSAlexey Gladkov 		long max;
200f6b2db1aSEric W. Biederman 		tns = iter->ns;
20125f9c081SEric W. Biederman 		max = READ_ONCE(tns->ucount_max[type]);
202*f9c82a4eSAlexey Gladkov 		if (!atomic_long_inc_below(&iter->ucount[type], max))
203b376c3e1SEric W. Biederman 			goto fail;
204b376c3e1SEric W. Biederman 	}
205f6b2db1aSEric W. Biederman 	return ucounts;
206b376c3e1SEric W. Biederman fail:
207f6b2db1aSEric W. Biederman 	bad = iter;
208f6b2db1aSEric W. Biederman 	for (iter = ucounts; iter != bad; iter = iter->ns->ucounts)
209*f9c82a4eSAlexey Gladkov 		atomic_long_dec(&iter->ucount[type]);
210b376c3e1SEric W. Biederman 
211f6b2db1aSEric W. Biederman 	put_ucounts(ucounts);
212f6b2db1aSEric W. Biederman 	return NULL;
213b376c3e1SEric W. Biederman }
214b376c3e1SEric W. Biederman 
21525f9c081SEric W. Biederman void dec_ucount(struct ucounts *ucounts, enum ucount_type type)
216b376c3e1SEric W. Biederman {
217f6b2db1aSEric W. Biederman 	struct ucounts *iter;
218f6b2db1aSEric W. Biederman 	for (iter = ucounts; iter; iter = iter->ns->ucounts) {
219*f9c82a4eSAlexey Gladkov 		long dec = atomic_long_dec_if_positive(&iter->ucount[type]);
220b376c3e1SEric W. Biederman 		WARN_ON_ONCE(dec < 0);
221b376c3e1SEric W. Biederman 	}
222f6b2db1aSEric W. Biederman 	put_ucounts(ucounts);
223b376c3e1SEric W. Biederman }
224b376c3e1SEric W. Biederman 
225dbec2846SEric W. Biederman static __init int user_namespace_sysctl_init(void)
226dbec2846SEric W. Biederman {
227dbec2846SEric W. Biederman #ifdef CONFIG_SYSCTL
228f6b2db1aSEric W. Biederman 	static struct ctl_table_header *user_header;
229dbec2846SEric W. Biederman 	static struct ctl_table empty[1];
230dbec2846SEric W. Biederman 	/*
231f6b2db1aSEric W. Biederman 	 * It is necessary to register the user directory in the
232dbec2846SEric W. Biederman 	 * default set so that registrations in the child sets work
233dbec2846SEric W. Biederman 	 * properly.
234dbec2846SEric W. Biederman 	 */
235f6b2db1aSEric W. Biederman 	user_header = register_sysctl("user", empty);
236ed5bd7dcSLuis R. Rodriguez 	kmemleak_ignore(user_header);
237f6b2db1aSEric W. Biederman 	BUG_ON(!user_header);
238dbec2846SEric W. Biederman 	BUG_ON(!setup_userns_sysctls(&init_user_ns));
239dbec2846SEric W. Biederman #endif
240dbec2846SEric W. Biederman 	return 0;
241dbec2846SEric W. Biederman }
242dbec2846SEric W. Biederman subsys_initcall(user_namespace_sysctl_init);
243