xref: /openbmc/linux/kernel/ucount.c (revision 905ae01c4ae2ae3df05bb141801b1db4b7d83c61)
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 
11*905ae01cSAlexey Gladkov struct ucounts init_ucounts = {
12*905ae01cSAlexey Gladkov 	.ns    = &init_user_ns,
13*905ae01cSAlexey Gladkov 	.uid   = GLOBAL_ROOT_UID,
14*905ae01cSAlexey Gladkov 	.count = 1,
15*905ae01cSAlexey Gladkov };
16*905ae01cSAlexey 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
83dbec2846SEric W. Biederman 	{ }
84dbec2846SEric W. Biederman };
85dbec2846SEric W. Biederman #endif /* CONFIG_SYSCTL */
86dbec2846SEric W. Biederman 
87dbec2846SEric W. Biederman bool setup_userns_sysctls(struct user_namespace *ns)
88dbec2846SEric W. Biederman {
89dbec2846SEric W. Biederman #ifdef CONFIG_SYSCTL
90dbec2846SEric W. Biederman 	struct ctl_table *tbl;
910f538e3eSJan Kara 
920f538e3eSJan Kara 	BUILD_BUG_ON(ARRAY_SIZE(user_table) != UCOUNT_COUNTS + 1);
93dbec2846SEric W. Biederman 	setup_sysctl_set(&ns->set, &set_root, set_is_seen);
94f6b2db1aSEric W. Biederman 	tbl = kmemdup(user_table, sizeof(user_table), GFP_KERNEL);
95dbec2846SEric W. Biederman 	if (tbl) {
9625f9c081SEric W. Biederman 		int i;
9725f9c081SEric W. Biederman 		for (i = 0; i < UCOUNT_COUNTS; i++) {
9825f9c081SEric W. Biederman 			tbl[i].data = &ns->ucount_max[i];
9925f9c081SEric W. Biederman 		}
100f6b2db1aSEric W. Biederman 		ns->sysctls = __register_sysctl_table(&ns->set, "user", tbl);
101dbec2846SEric W. Biederman 	}
102dbec2846SEric W. Biederman 	if (!ns->sysctls) {
103dbec2846SEric W. Biederman 		kfree(tbl);
104dbec2846SEric W. Biederman 		retire_sysctl_set(&ns->set);
105dbec2846SEric W. Biederman 		return false;
106dbec2846SEric W. Biederman 	}
107dbec2846SEric W. Biederman #endif
108dbec2846SEric W. Biederman 	return true;
109dbec2846SEric W. Biederman }
110dbec2846SEric W. Biederman 
111dbec2846SEric W. Biederman void retire_userns_sysctls(struct user_namespace *ns)
112dbec2846SEric W. Biederman {
113dbec2846SEric W. Biederman #ifdef CONFIG_SYSCTL
114dbec2846SEric W. Biederman 	struct ctl_table *tbl;
115dbec2846SEric W. Biederman 
116dbec2846SEric W. Biederman 	tbl = ns->sysctls->ctl_table_arg;
117dbec2846SEric W. Biederman 	unregister_sysctl_table(ns->sysctls);
118dbec2846SEric W. Biederman 	retire_sysctl_set(&ns->set);
119dbec2846SEric W. Biederman 	kfree(tbl);
120dbec2846SEric W. Biederman #endif
121dbec2846SEric W. Biederman }
122dbec2846SEric W. Biederman 
123f6b2db1aSEric W. Biederman static struct ucounts *find_ucounts(struct user_namespace *ns, kuid_t uid, struct hlist_head *hashent)
124f6b2db1aSEric W. Biederman {
125f6b2db1aSEric W. Biederman 	struct ucounts *ucounts;
126f6b2db1aSEric W. Biederman 
127f6b2db1aSEric W. Biederman 	hlist_for_each_entry(ucounts, hashent, node) {
128f6b2db1aSEric W. Biederman 		if (uid_eq(ucounts->uid, uid) && (ucounts->ns == ns))
129f6b2db1aSEric W. Biederman 			return ucounts;
130f6b2db1aSEric W. Biederman 	}
131f6b2db1aSEric W. Biederman 	return NULL;
132f6b2db1aSEric W. Biederman }
133f6b2db1aSEric W. Biederman 
134*905ae01cSAlexey Gladkov static void hlist_add_ucounts(struct ucounts *ucounts)
135*905ae01cSAlexey Gladkov {
136*905ae01cSAlexey Gladkov 	struct hlist_head *hashent = ucounts_hashentry(ucounts->ns, ucounts->uid);
137*905ae01cSAlexey Gladkov 	spin_lock_irq(&ucounts_lock);
138*905ae01cSAlexey Gladkov 	hlist_add_head(&ucounts->node, hashent);
139*905ae01cSAlexey Gladkov 	spin_unlock_irq(&ucounts_lock);
140*905ae01cSAlexey Gladkov }
141*905ae01cSAlexey Gladkov 
142*905ae01cSAlexey Gladkov struct ucounts *alloc_ucounts(struct user_namespace *ns, kuid_t uid)
143f6b2db1aSEric W. Biederman {
144f6b2db1aSEric W. Biederman 	struct hlist_head *hashent = ucounts_hashentry(ns, uid);
145f6b2db1aSEric W. Biederman 	struct ucounts *ucounts, *new;
146f6b2db1aSEric W. Biederman 
147880a3854SNikolay Borisov 	spin_lock_irq(&ucounts_lock);
148f6b2db1aSEric W. Biederman 	ucounts = find_ucounts(ns, uid, hashent);
149f6b2db1aSEric W. Biederman 	if (!ucounts) {
150880a3854SNikolay Borisov 		spin_unlock_irq(&ucounts_lock);
151f6b2db1aSEric W. Biederman 
152f6b2db1aSEric W. Biederman 		new = kzalloc(sizeof(*new), GFP_KERNEL);
153f6b2db1aSEric W. Biederman 		if (!new)
154f6b2db1aSEric W. Biederman 			return NULL;
155f6b2db1aSEric W. Biederman 
156f6b2db1aSEric W. Biederman 		new->ns = ns;
157f6b2db1aSEric W. Biederman 		new->uid = uid;
158040757f7SEric W. Biederman 		new->count = 0;
159f6b2db1aSEric W. Biederman 
160880a3854SNikolay Borisov 		spin_lock_irq(&ucounts_lock);
161f6b2db1aSEric W. Biederman 		ucounts = find_ucounts(ns, uid, hashent);
162f6b2db1aSEric W. Biederman 		if (ucounts) {
163f6b2db1aSEric W. Biederman 			kfree(new);
164f6b2db1aSEric W. Biederman 		} else {
165f6b2db1aSEric W. Biederman 			hlist_add_head(&new->node, hashent);
166f6b2db1aSEric W. Biederman 			ucounts = new;
167f6b2db1aSEric W. Biederman 		}
168f6b2db1aSEric W. Biederman 	}
169040757f7SEric W. Biederman 	if (ucounts->count == INT_MAX)
170f6b2db1aSEric W. Biederman 		ucounts = NULL;
171040757f7SEric W. Biederman 	else
172040757f7SEric W. Biederman 		ucounts->count += 1;
173880a3854SNikolay Borisov 	spin_unlock_irq(&ucounts_lock);
174f6b2db1aSEric W. Biederman 	return ucounts;
175f6b2db1aSEric W. Biederman }
176f6b2db1aSEric W. Biederman 
177*905ae01cSAlexey Gladkov struct ucounts *get_ucounts(struct ucounts *ucounts)
178*905ae01cSAlexey Gladkov {
179*905ae01cSAlexey Gladkov 	unsigned long flags;
180*905ae01cSAlexey Gladkov 
181*905ae01cSAlexey Gladkov 	if (!ucounts)
182*905ae01cSAlexey Gladkov 		return NULL;
183*905ae01cSAlexey Gladkov 
184*905ae01cSAlexey Gladkov 	spin_lock_irqsave(&ucounts_lock, flags);
185*905ae01cSAlexey Gladkov 	if (ucounts->count == INT_MAX) {
186*905ae01cSAlexey Gladkov 		WARN_ONCE(1, "ucounts: counter has reached its maximum value");
187*905ae01cSAlexey Gladkov 		ucounts = NULL;
188*905ae01cSAlexey Gladkov 	} else {
189*905ae01cSAlexey Gladkov 		ucounts->count += 1;
190*905ae01cSAlexey Gladkov 	}
191*905ae01cSAlexey Gladkov 	spin_unlock_irqrestore(&ucounts_lock, flags);
192*905ae01cSAlexey Gladkov 
193*905ae01cSAlexey Gladkov 	return ucounts;
194*905ae01cSAlexey Gladkov }
195*905ae01cSAlexey Gladkov 
196*905ae01cSAlexey Gladkov void put_ucounts(struct ucounts *ucounts)
197f6b2db1aSEric W. Biederman {
198880a3854SNikolay Borisov 	unsigned long flags;
199880a3854SNikolay Borisov 
200880a3854SNikolay Borisov 	spin_lock_irqsave(&ucounts_lock, flags);
201040757f7SEric W. Biederman 	ucounts->count -= 1;
202040757f7SEric W. Biederman 	if (!ucounts->count)
203f6b2db1aSEric W. Biederman 		hlist_del_init(&ucounts->node);
204040757f7SEric W. Biederman 	else
205040757f7SEric W. Biederman 		ucounts = NULL;
206880a3854SNikolay Borisov 	spin_unlock_irqrestore(&ucounts_lock, flags);
207f6b2db1aSEric W. Biederman 
208f6b2db1aSEric W. Biederman 	kfree(ucounts);
209f6b2db1aSEric W. Biederman }
210f6b2db1aSEric W. Biederman 
211f9c82a4eSAlexey Gladkov static inline bool atomic_long_inc_below(atomic_long_t *v, int u)
212b376c3e1SEric W. Biederman {
213f9c82a4eSAlexey Gladkov 	long c, old;
214f9c82a4eSAlexey Gladkov 	c = atomic_long_read(v);
215b376c3e1SEric W. Biederman 	for (;;) {
216b376c3e1SEric W. Biederman 		if (unlikely(c >= u))
217b376c3e1SEric W. Biederman 			return false;
218f9c82a4eSAlexey Gladkov 		old = atomic_long_cmpxchg(v, c, c+1);
219b376c3e1SEric W. Biederman 		if (likely(old == c))
220b376c3e1SEric W. Biederman 			return true;
221b376c3e1SEric W. Biederman 		c = old;
222b376c3e1SEric W. Biederman 	}
223b376c3e1SEric W. Biederman }
224b376c3e1SEric W. Biederman 
22525f9c081SEric W. Biederman struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid,
22625f9c081SEric W. Biederman 			   enum ucount_type type)
227b376c3e1SEric W. Biederman {
228f6b2db1aSEric W. Biederman 	struct ucounts *ucounts, *iter, *bad;
229f6b2db1aSEric W. Biederman 	struct user_namespace *tns;
230*905ae01cSAlexey Gladkov 	ucounts = alloc_ucounts(ns, uid);
231f6b2db1aSEric W. Biederman 	for (iter = ucounts; iter; iter = tns->ucounts) {
232f9c82a4eSAlexey Gladkov 		long max;
233f6b2db1aSEric W. Biederman 		tns = iter->ns;
23425f9c081SEric W. Biederman 		max = READ_ONCE(tns->ucount_max[type]);
235f9c82a4eSAlexey Gladkov 		if (!atomic_long_inc_below(&iter->ucount[type], max))
236b376c3e1SEric W. Biederman 			goto fail;
237b376c3e1SEric W. Biederman 	}
238f6b2db1aSEric W. Biederman 	return ucounts;
239b376c3e1SEric W. Biederman fail:
240f6b2db1aSEric W. Biederman 	bad = iter;
241f6b2db1aSEric W. Biederman 	for (iter = ucounts; iter != bad; iter = iter->ns->ucounts)
242f9c82a4eSAlexey Gladkov 		atomic_long_dec(&iter->ucount[type]);
243b376c3e1SEric W. Biederman 
244f6b2db1aSEric W. Biederman 	put_ucounts(ucounts);
245f6b2db1aSEric W. Biederman 	return NULL;
246b376c3e1SEric W. Biederman }
247b376c3e1SEric W. Biederman 
24825f9c081SEric W. Biederman void dec_ucount(struct ucounts *ucounts, enum ucount_type type)
249b376c3e1SEric W. Biederman {
250f6b2db1aSEric W. Biederman 	struct ucounts *iter;
251f6b2db1aSEric W. Biederman 	for (iter = ucounts; iter; iter = iter->ns->ucounts) {
252f9c82a4eSAlexey Gladkov 		long dec = atomic_long_dec_if_positive(&iter->ucount[type]);
253b376c3e1SEric W. Biederman 		WARN_ON_ONCE(dec < 0);
254b376c3e1SEric W. Biederman 	}
255f6b2db1aSEric W. Biederman 	put_ucounts(ucounts);
256b376c3e1SEric W. Biederman }
257b376c3e1SEric W. Biederman 
258dbec2846SEric W. Biederman static __init int user_namespace_sysctl_init(void)
259dbec2846SEric W. Biederman {
260dbec2846SEric W. Biederman #ifdef CONFIG_SYSCTL
261f6b2db1aSEric W. Biederman 	static struct ctl_table_header *user_header;
262dbec2846SEric W. Biederman 	static struct ctl_table empty[1];
263dbec2846SEric W. Biederman 	/*
264f6b2db1aSEric W. Biederman 	 * It is necessary to register the user directory in the
265dbec2846SEric W. Biederman 	 * default set so that registrations in the child sets work
266dbec2846SEric W. Biederman 	 * properly.
267dbec2846SEric W. Biederman 	 */
268f6b2db1aSEric W. Biederman 	user_header = register_sysctl("user", empty);
269ed5bd7dcSLuis R. Rodriguez 	kmemleak_ignore(user_header);
270f6b2db1aSEric W. Biederman 	BUG_ON(!user_header);
271dbec2846SEric W. Biederman 	BUG_ON(!setup_userns_sysctls(&init_user_ns));
272dbec2846SEric W. Biederman #endif
273*905ae01cSAlexey Gladkov 	hlist_add_ucounts(&init_ucounts);
274dbec2846SEric W. Biederman 	return 0;
275dbec2846SEric W. Biederman }
276dbec2846SEric W. Biederman subsys_initcall(user_namespace_sysctl_init);
277