xref: /linux-6.15/kernel/ucount.c (revision b4dc0bee)
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,
14*b4dc0beeSSebastian Andrzej Siewior 	.count = RCUREF_INIT(1),
15905ae01cSAlexey Gladkov };
16905ae01cSAlexey Gladkov 
17f6b2db1aSEric W. Biederman #define UCOUNTS_HASHTABLE_BITS 10
185f01a22cSSebastian Andrzej Siewior #define UCOUNTS_HASHTABLE_ENTRIES (1 << UCOUNTS_HASHTABLE_BITS)
195f01a22cSSebastian Andrzej Siewior static struct hlist_nulls_head ucounts_hashtable[UCOUNTS_HASHTABLE_ENTRIES] = {
205f01a22cSSebastian Andrzej Siewior 	[0 ... UCOUNTS_HASHTABLE_ENTRIES - 1] = HLIST_NULLS_HEAD_INIT(0)
215f01a22cSSebastian Andrzej Siewior };
22f6b2db1aSEric W. Biederman static DEFINE_SPINLOCK(ucounts_lock);
23f6b2db1aSEric W. Biederman 
24f6b2db1aSEric W. Biederman #define ucounts_hashfn(ns, uid)						\
25f6b2db1aSEric W. Biederman 	hash_long((unsigned long)__kuid_val(uid) + (unsigned long)(ns), \
26f6b2db1aSEric W. Biederman 		  UCOUNTS_HASHTABLE_BITS)
27f6b2db1aSEric W. Biederman #define ucounts_hashentry(ns, uid)	\
28f6b2db1aSEric W. Biederman 	(ucounts_hashtable + ucounts_hashfn(ns, uid))
29f6b2db1aSEric W. Biederman 
30dbec2846SEric W. Biederman #ifdef CONFIG_SYSCTL
31dbec2846SEric W. Biederman static struct ctl_table_set *
set_lookup(struct ctl_table_root * root)32dbec2846SEric W. Biederman set_lookup(struct ctl_table_root *root)
33dbec2846SEric W. Biederman {
34dbec2846SEric W. Biederman 	return &current_user_ns()->set;
35dbec2846SEric W. Biederman }
36dbec2846SEric W. Biederman 
set_is_seen(struct ctl_table_set * set)37dbec2846SEric W. Biederman static int set_is_seen(struct ctl_table_set *set)
38dbec2846SEric W. Biederman {
39dbec2846SEric W. Biederman 	return &current_user_ns()->set == set;
40dbec2846SEric W. Biederman }
41dbec2846SEric W. Biederman 
set_permissions(struct ctl_table_header * head,const struct ctl_table * table)42dbec2846SEric W. Biederman static int set_permissions(struct ctl_table_header *head,
43795f90c6SThomas Weißschuh 			   const struct ctl_table *table)
44dbec2846SEric W. Biederman {
45dbec2846SEric W. Biederman 	struct user_namespace *user_ns =
46dbec2846SEric W. Biederman 		container_of(head->set, struct user_namespace, set);
47dbec2846SEric W. Biederman 	int mode;
48dbec2846SEric W. Biederman 
49dbec2846SEric W. Biederman 	/* Allow users with CAP_SYS_RESOURCE unrestrained access */
50dbec2846SEric W. Biederman 	if (ns_capable(user_ns, CAP_SYS_RESOURCE))
51dbec2846SEric W. Biederman 		mode = (table->mode & S_IRWXU) >> 6;
52dbec2846SEric W. Biederman 	else
53dbec2846SEric W. Biederman 	/* Allow all others at most read-only access */
54dbec2846SEric W. Biederman 		mode = table->mode & S_IROTH;
55dbec2846SEric W. Biederman 	return (mode << 6) | (mode << 3) | mode;
56dbec2846SEric W. Biederman }
57dbec2846SEric W. Biederman 
58dbec2846SEric W. Biederman static struct ctl_table_root set_root = {
59dbec2846SEric W. Biederman 	.lookup = set_lookup,
60dbec2846SEric W. Biederman 	.permissions = set_permissions,
61dbec2846SEric W. Biederman };
62dbec2846SEric W. Biederman 
63f153c224SSven Schnelle static long ue_zero = 0;
64f153c224SSven Schnelle static long ue_int_max = INT_MAX;
65f153c224SSven Schnelle 
6625f9c081SEric W. Biederman #define UCOUNT_ENTRY(name)					\
6725f9c081SEric W. Biederman 	{							\
6825f9c081SEric W. Biederman 		.procname	= name,				\
69f153c224SSven Schnelle 		.maxlen		= sizeof(long),			\
7025f9c081SEric W. Biederman 		.mode		= 0644,				\
71f153c224SSven Schnelle 		.proc_handler	= proc_doulongvec_minmax,	\
72f153c224SSven Schnelle 		.extra1		= &ue_zero,			\
73f153c224SSven Schnelle 		.extra2		= &ue_int_max,			\
7425f9c081SEric W. Biederman 	}
75ad45af93SThomas Weißschuh static const struct ctl_table user_table[] = {
7625f9c081SEric W. Biederman 	UCOUNT_ENTRY("max_user_namespaces"),
77f333c700SEric W. Biederman 	UCOUNT_ENTRY("max_pid_namespaces"),
78f7af3d1cSEric W. Biederman 	UCOUNT_ENTRY("max_uts_namespaces"),
79aba35661SEric W. Biederman 	UCOUNT_ENTRY("max_ipc_namespaces"),
8070328660SEric W. Biederman 	UCOUNT_ENTRY("max_net_namespaces"),
81537f7ccbSEric W. Biederman 	UCOUNT_ENTRY("max_mnt_namespaces"),
82d08311ddSEric W. Biederman 	UCOUNT_ENTRY("max_cgroup_namespaces"),
83eeec26d5SDmitry Safonov 	UCOUNT_ENTRY("max_time_namespaces"),
841cce1eeaSNikolay Borisov #ifdef CONFIG_INOTIFY_USER
851cce1eeaSNikolay Borisov 	UCOUNT_ENTRY("max_inotify_instances"),
861cce1eeaSNikolay Borisov 	UCOUNT_ENTRY("max_inotify_watches"),
871cce1eeaSNikolay Borisov #endif
885b8fea65SAmir Goldstein #ifdef CONFIG_FANOTIFY
895b8fea65SAmir Goldstein 	UCOUNT_ENTRY("max_fanotify_groups"),
905b8fea65SAmir Goldstein 	UCOUNT_ENTRY("max_fanotify_marks"),
915b8fea65SAmir Goldstein #endif
92dbec2846SEric W. Biederman };
93dbec2846SEric W. Biederman #endif /* CONFIG_SYSCTL */
94dbec2846SEric W. Biederman 
setup_userns_sysctls(struct user_namespace * ns)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 
10011a92190SJoel Granados 	BUILD_BUG_ON(ARRAY_SIZE(user_table) != UCOUNT_COUNTS);
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 		}
108bff97cf1SJoel Granados 		ns->sysctls = __register_sysctl_table(&ns->set, "user", tbl,
109bff97cf1SJoel Granados 						      ARRAY_SIZE(user_table));
110dbec2846SEric W. Biederman 	}
111dbec2846SEric W. Biederman 	if (!ns->sysctls) {
112dbec2846SEric W. Biederman 		kfree(tbl);
113dbec2846SEric W. Biederman 		retire_sysctl_set(&ns->set);
114dbec2846SEric W. Biederman 		return false;
115dbec2846SEric W. Biederman 	}
116dbec2846SEric W. Biederman #endif
117dbec2846SEric W. Biederman 	return true;
118dbec2846SEric W. Biederman }
119dbec2846SEric W. Biederman 
retire_userns_sysctls(struct user_namespace * ns)120dbec2846SEric W. Biederman void retire_userns_sysctls(struct user_namespace *ns)
121dbec2846SEric W. Biederman {
122dbec2846SEric W. Biederman #ifdef CONFIG_SYSCTL
123bfa858f2SThomas Weißschuh 	const struct ctl_table *tbl;
124dbec2846SEric W. Biederman 
125dbec2846SEric W. Biederman 	tbl = ns->sysctls->ctl_table_arg;
126dbec2846SEric W. Biederman 	unregister_sysctl_table(ns->sysctls);
127dbec2846SEric W. Biederman 	retire_sysctl_set(&ns->set);
128dbec2846SEric W. Biederman 	kfree(tbl);
129dbec2846SEric W. Biederman #endif
130dbec2846SEric W. Biederman }
131dbec2846SEric W. Biederman 
find_ucounts(struct user_namespace * ns,kuid_t uid,struct hlist_nulls_head * hashent)1325f01a22cSSebastian Andrzej Siewior static struct ucounts *find_ucounts(struct user_namespace *ns, kuid_t uid,
1335f01a22cSSebastian Andrzej Siewior 				    struct hlist_nulls_head *hashent)
134f6b2db1aSEric W. Biederman {
135f6b2db1aSEric W. Biederman 	struct ucounts *ucounts;
1365f01a22cSSebastian Andrzej Siewior 	struct hlist_nulls_node *pos;
137f6b2db1aSEric W. Biederman 
1385f01a22cSSebastian Andrzej Siewior 	guard(rcu)();
1395f01a22cSSebastian Andrzej Siewior 	hlist_nulls_for_each_entry_rcu(ucounts, pos, hashent, node) {
1405f01a22cSSebastian Andrzej Siewior 		if (uid_eq(ucounts->uid, uid) && (ucounts->ns == ns)) {
141*b4dc0beeSSebastian Andrzej Siewior 			if (rcuref_get(&ucounts->count))
142f6b2db1aSEric W. Biederman 				return ucounts;
143f6b2db1aSEric W. Biederman 		}
1445f01a22cSSebastian Andrzej Siewior 	}
145f6b2db1aSEric W. Biederman 	return NULL;
146f6b2db1aSEric W. Biederman }
147f6b2db1aSEric W. Biederman 
hlist_add_ucounts(struct ucounts * ucounts)148905ae01cSAlexey Gladkov static void hlist_add_ucounts(struct ucounts *ucounts)
149905ae01cSAlexey Gladkov {
1505f01a22cSSebastian Andrzej Siewior 	struct hlist_nulls_head *hashent = ucounts_hashentry(ucounts->ns, ucounts->uid);
1515f01a22cSSebastian Andrzej Siewior 
152905ae01cSAlexey Gladkov 	spin_lock_irq(&ucounts_lock);
1535f01a22cSSebastian Andrzej Siewior 	hlist_nulls_add_head_rcu(&ucounts->node, hashent);
154905ae01cSAlexey Gladkov 	spin_unlock_irq(&ucounts_lock);
155905ae01cSAlexey Gladkov }
156905ae01cSAlexey Gladkov 
alloc_ucounts(struct user_namespace * ns,kuid_t uid)157905ae01cSAlexey Gladkov struct ucounts *alloc_ucounts(struct user_namespace *ns, kuid_t uid)
158f6b2db1aSEric W. Biederman {
1595f01a22cSSebastian Andrzej Siewior 	struct hlist_nulls_head *hashent = ucounts_hashentry(ns, uid);
1605f01a22cSSebastian Andrzej Siewior 	struct ucounts *ucounts, *new;
161f6b2db1aSEric W. Biederman 
162f6b2db1aSEric W. Biederman 	ucounts = find_ucounts(ns, uid, hashent);
1635f01a22cSSebastian Andrzej Siewior 	if (ucounts)
1645f01a22cSSebastian Andrzej Siewior 		return ucounts;
165f6b2db1aSEric W. Biederman 
166f6b2db1aSEric W. Biederman 	new = kzalloc(sizeof(*new), GFP_KERNEL);
167f6b2db1aSEric W. Biederman 	if (!new)
168f6b2db1aSEric W. Biederman 		return NULL;
169f6b2db1aSEric W. Biederman 
170f6b2db1aSEric W. Biederman 	new->ns = ns;
171f6b2db1aSEric W. Biederman 	new->uid = uid;
172*b4dc0beeSSebastian Andrzej Siewior 	rcuref_init(&new->count, 1);
173f6b2db1aSEric W. Biederman 
174880a3854SNikolay Borisov 	spin_lock_irq(&ucounts_lock);
175f6b2db1aSEric W. Biederman 	ucounts = find_ucounts(ns, uid, hashent);
1765f01a22cSSebastian Andrzej Siewior 	if (ucounts) {
1775f01a22cSSebastian Andrzej Siewior 		spin_unlock_irq(&ucounts_lock);
1785f01a22cSSebastian Andrzej Siewior 		kfree(new);
1795f01a22cSSebastian Andrzej Siewior 		return ucounts;
1805f01a22cSSebastian Andrzej Siewior 	}
1815f01a22cSSebastian Andrzej Siewior 
1825f01a22cSSebastian Andrzej Siewior 	hlist_nulls_add_head_rcu(&new->node, hashent);
183f9d87929SEric W. Biederman 	get_user_ns(new->ns);
184880a3854SNikolay Borisov 	spin_unlock_irq(&ucounts_lock);
185b6c33652SAlexey Gladkov 	return new;
186f6b2db1aSEric W. Biederman }
187f6b2db1aSEric W. Biederman 
put_ucounts(struct ucounts * ucounts)188905ae01cSAlexey Gladkov void put_ucounts(struct ucounts *ucounts)
189f6b2db1aSEric W. Biederman {
190880a3854SNikolay Borisov 	unsigned long flags;
191880a3854SNikolay Borisov 
192*b4dc0beeSSebastian Andrzej Siewior 	if (rcuref_put(&ucounts->count)) {
193*b4dc0beeSSebastian Andrzej Siewior 		spin_lock_irqsave(&ucounts_lock, flags);
1945f01a22cSSebastian Andrzej Siewior 		hlist_nulls_del_rcu(&ucounts->node);
195880a3854SNikolay Borisov 		spin_unlock_irqrestore(&ucounts_lock, flags);
1965f01a22cSSebastian Andrzej Siewior 
197f9d87929SEric W. Biederman 		put_user_ns(ucounts->ns);
1985f01a22cSSebastian Andrzej Siewior 		kfree_rcu(ucounts, rcu);
199f6b2db1aSEric W. Biederman 	}
200b6c33652SAlexey Gladkov }
201f6b2db1aSEric W. Biederman 
atomic_long_inc_below(atomic_long_t * v,int u)202f9c82a4eSAlexey Gladkov static inline bool atomic_long_inc_below(atomic_long_t *v, int u)
203b376c3e1SEric W. Biederman {
204f9c82a4eSAlexey Gladkov 	long c, old;
205f9c82a4eSAlexey Gladkov 	c = atomic_long_read(v);
206b376c3e1SEric W. Biederman 	for (;;) {
207b376c3e1SEric W. Biederman 		if (unlikely(c >= u))
208b376c3e1SEric W. Biederman 			return false;
209f9c82a4eSAlexey Gladkov 		old = atomic_long_cmpxchg(v, c, c+1);
210b376c3e1SEric W. Biederman 		if (likely(old == c))
211b376c3e1SEric W. Biederman 			return true;
212b376c3e1SEric W. Biederman 		c = old;
213b376c3e1SEric W. Biederman 	}
214b376c3e1SEric W. Biederman }
215b376c3e1SEric W. Biederman 
inc_ucount(struct user_namespace * ns,kuid_t uid,enum ucount_type type)21625f9c081SEric W. Biederman struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid,
21725f9c081SEric W. Biederman 			   enum ucount_type type)
218b376c3e1SEric W. Biederman {
219f6b2db1aSEric W. Biederman 	struct ucounts *ucounts, *iter, *bad;
220f6b2db1aSEric W. Biederman 	struct user_namespace *tns;
221905ae01cSAlexey Gladkov 	ucounts = alloc_ucounts(ns, uid);
222f6b2db1aSEric W. Biederman 	for (iter = ucounts; iter; iter = tns->ucounts) {
223f9c82a4eSAlexey Gladkov 		long max;
224f6b2db1aSEric W. Biederman 		tns = iter->ns;
22525f9c081SEric W. Biederman 		max = READ_ONCE(tns->ucount_max[type]);
226f9c82a4eSAlexey Gladkov 		if (!atomic_long_inc_below(&iter->ucount[type], max))
227b376c3e1SEric W. Biederman 			goto fail;
228b376c3e1SEric W. Biederman 	}
229f6b2db1aSEric W. Biederman 	return ucounts;
230b376c3e1SEric W. Biederman fail:
231f6b2db1aSEric W. Biederman 	bad = iter;
232f6b2db1aSEric W. Biederman 	for (iter = ucounts; iter != bad; iter = iter->ns->ucounts)
233f9c82a4eSAlexey Gladkov 		atomic_long_dec(&iter->ucount[type]);
234b376c3e1SEric W. Biederman 
235f6b2db1aSEric W. Biederman 	put_ucounts(ucounts);
236f6b2db1aSEric W. Biederman 	return NULL;
237b376c3e1SEric W. Biederman }
238b376c3e1SEric W. Biederman 
dec_ucount(struct ucounts * ucounts,enum ucount_type type)23925f9c081SEric W. Biederman void dec_ucount(struct ucounts *ucounts, enum ucount_type type)
240b376c3e1SEric W. Biederman {
241f6b2db1aSEric W. Biederman 	struct ucounts *iter;
242f6b2db1aSEric W. Biederman 	for (iter = ucounts; iter; iter = iter->ns->ucounts) {
243f9c82a4eSAlexey Gladkov 		long dec = atomic_long_dec_if_positive(&iter->ucount[type]);
244b376c3e1SEric W. Biederman 		WARN_ON_ONCE(dec < 0);
245b376c3e1SEric W. Biederman 	}
246f6b2db1aSEric W. Biederman 	put_ucounts(ucounts);
247b376c3e1SEric W. Biederman }
248b376c3e1SEric W. Biederman 
inc_rlimit_ucounts(struct ucounts * ucounts,enum rlimit_type type,long v)249de399236SAlexey Gladkov long inc_rlimit_ucounts(struct ucounts *ucounts, enum rlimit_type type, long v)
25021d1c5e3SAlexey Gladkov {
25121d1c5e3SAlexey Gladkov 	struct ucounts *iter;
25259ec7157SAlexey Gladkov 	long max = LONG_MAX;
25321d1c5e3SAlexey Gladkov 	long ret = 0;
25421d1c5e3SAlexey Gladkov 
25521d1c5e3SAlexey Gladkov 	for (iter = ucounts; iter; iter = iter->ns->ucounts) {
256de399236SAlexey Gladkov 		long new = atomic_long_add_return(v, &iter->rlimit[type]);
25721d1c5e3SAlexey Gladkov 		if (new < 0 || new > max)
25821d1c5e3SAlexey Gladkov 			ret = LONG_MAX;
25921d1c5e3SAlexey Gladkov 		else if (iter == ucounts)
26021d1c5e3SAlexey Gladkov 			ret = new;
261de399236SAlexey Gladkov 		max = get_userns_rlimit_max(iter->ns, type);
26221d1c5e3SAlexey Gladkov 	}
26321d1c5e3SAlexey Gladkov 	return ret;
26421d1c5e3SAlexey Gladkov }
26521d1c5e3SAlexey Gladkov 
dec_rlimit_ucounts(struct ucounts * ucounts,enum rlimit_type type,long v)266de399236SAlexey Gladkov bool dec_rlimit_ucounts(struct ucounts *ucounts, enum rlimit_type type, long v)
26721d1c5e3SAlexey Gladkov {
26821d1c5e3SAlexey Gladkov 	struct ucounts *iter;
269f928ef68SEric W. Biederman 	long new = -1; /* Silence compiler warning */
27021d1c5e3SAlexey Gladkov 	for (iter = ucounts; iter; iter = iter->ns->ucounts) {
271de399236SAlexey Gladkov 		long dec = atomic_long_sub_return(v, &iter->rlimit[type]);
27221d1c5e3SAlexey Gladkov 		WARN_ON_ONCE(dec < 0);
27321d1c5e3SAlexey Gladkov 		if (iter == ucounts)
27421d1c5e3SAlexey Gladkov 			new = dec;
27521d1c5e3SAlexey Gladkov 	}
27621d1c5e3SAlexey Gladkov 	return (new == 0);
27721d1c5e3SAlexey Gladkov }
27821d1c5e3SAlexey Gladkov 
do_dec_rlimit_put_ucounts(struct ucounts * ucounts,struct ucounts * last,enum rlimit_type type)27915bc01efSEric W. Biederman static void do_dec_rlimit_put_ucounts(struct ucounts *ucounts,
280de399236SAlexey Gladkov 				struct ucounts *last, enum rlimit_type type)
28115bc01efSEric W. Biederman {
28215bc01efSEric W. Biederman 	struct ucounts *iter, *next;
28315bc01efSEric W. Biederman 	for (iter = ucounts; iter != last; iter = next) {
284de399236SAlexey Gladkov 		long dec = atomic_long_sub_return(1, &iter->rlimit[type]);
28515bc01efSEric W. Biederman 		WARN_ON_ONCE(dec < 0);
28615bc01efSEric W. Biederman 		next = iter->ns->ucounts;
28715bc01efSEric W. Biederman 		if (dec == 0)
28815bc01efSEric W. Biederman 			put_ucounts(iter);
28915bc01efSEric W. Biederman 	}
29015bc01efSEric W. Biederman }
29115bc01efSEric W. Biederman 
dec_rlimit_put_ucounts(struct ucounts * ucounts,enum rlimit_type type)292de399236SAlexey Gladkov void dec_rlimit_put_ucounts(struct ucounts *ucounts, enum rlimit_type type)
29315bc01efSEric W. Biederman {
29415bc01efSEric W. Biederman 	do_dec_rlimit_put_ucounts(ucounts, NULL, type);
29515bc01efSEric W. Biederman }
29615bc01efSEric W. Biederman 
inc_rlimit_get_ucounts(struct ucounts * ucounts,enum rlimit_type type,bool override_rlimit)2979e05e5c7SRoman Gushchin long inc_rlimit_get_ucounts(struct ucounts *ucounts, enum rlimit_type type,
2989e05e5c7SRoman Gushchin 			    bool override_rlimit)
29915bc01efSEric W. Biederman {
30015bc01efSEric W. Biederman 	/* Caller must hold a reference to ucounts */
30115bc01efSEric W. Biederman 	struct ucounts *iter;
30259ec7157SAlexey Gladkov 	long max = LONG_MAX;
30315bc01efSEric W. Biederman 	long dec, ret = 0;
30415bc01efSEric W. Biederman 
30515bc01efSEric W. Biederman 	for (iter = ucounts; iter; iter = iter->ns->ucounts) {
306de399236SAlexey Gladkov 		long new = atomic_long_add_return(1, &iter->rlimit[type]);
30715bc01efSEric W. Biederman 		if (new < 0 || new > max)
308432dc065SAndrei Vagin 			goto dec_unwind;
30915bc01efSEric W. Biederman 		if (iter == ucounts)
31015bc01efSEric W. Biederman 			ret = new;
3119e05e5c7SRoman Gushchin 		if (!override_rlimit)
312de399236SAlexey Gladkov 			max = get_userns_rlimit_max(iter->ns, type);
31315bc01efSEric W. Biederman 		/*
31415bc01efSEric W. Biederman 		 * Grab an extra ucount reference for the caller when
31515bc01efSEric W. Biederman 		 * the rlimit count was previously 0.
31615bc01efSEric W. Biederman 		 */
31715bc01efSEric W. Biederman 		if (new != 1)
31815bc01efSEric W. Biederman 			continue;
31915bc01efSEric W. Biederman 		if (!get_ucounts(iter))
32015bc01efSEric W. Biederman 			goto dec_unwind;
32115bc01efSEric W. Biederman 	}
32215bc01efSEric W. Biederman 	return ret;
32315bc01efSEric W. Biederman dec_unwind:
324de399236SAlexey Gladkov 	dec = atomic_long_sub_return(1, &iter->rlimit[type]);
32515bc01efSEric W. Biederman 	WARN_ON_ONCE(dec < 0);
32615bc01efSEric W. Biederman 	do_dec_rlimit_put_ucounts(ucounts, iter, type);
32715bc01efSEric W. Biederman 	return 0;
32815bc01efSEric W. Biederman }
32915bc01efSEric W. Biederman 
is_rlimit_overlimit(struct ucounts * ucounts,enum rlimit_type type,unsigned long rlimit)330de399236SAlexey Gladkov bool is_rlimit_overlimit(struct ucounts *ucounts, enum rlimit_type type, unsigned long rlimit)
33121d1c5e3SAlexey Gladkov {
33221d1c5e3SAlexey Gladkov 	struct ucounts *iter;
33359ec7157SAlexey Gladkov 	long max = rlimit;
33459ec7157SAlexey Gladkov 	if (rlimit > LONG_MAX)
33559ec7157SAlexey Gladkov 		max = LONG_MAX;
33621d1c5e3SAlexey Gladkov 	for (iter = ucounts; iter; iter = iter->ns->ucounts) {
337de399236SAlexey Gladkov 		long val = get_rlimit_value(iter, type);
3380cbae9e2SEric W. Biederman 		if (val < 0 || val > max)
33921d1c5e3SAlexey Gladkov 			return true;
340de399236SAlexey Gladkov 		max = get_userns_rlimit_max(iter->ns, type);
34121d1c5e3SAlexey Gladkov 	}
34221d1c5e3SAlexey Gladkov 	return false;
34321d1c5e3SAlexey Gladkov }
34421d1c5e3SAlexey Gladkov 
user_namespace_sysctl_init(void)345dbec2846SEric W. Biederman static __init int user_namespace_sysctl_init(void)
346dbec2846SEric W. Biederman {
347dbec2846SEric W. Biederman #ifdef CONFIG_SYSCTL
348f6b2db1aSEric W. Biederman 	static struct ctl_table_header *user_header;
349dbec2846SEric W. Biederman 	static struct ctl_table empty[1];
350dbec2846SEric W. Biederman 	/*
351f6b2db1aSEric W. Biederman 	 * It is necessary to register the user directory in the
352dbec2846SEric W. Biederman 	 * default set so that registrations in the child sets work
353dbec2846SEric W. Biederman 	 * properly.
354dbec2846SEric W. Biederman 	 */
3559edbfe92SJoel Granados 	user_header = register_sysctl_sz("user", empty, 0);
356ed5bd7dcSLuis R. Rodriguez 	kmemleak_ignore(user_header);
357f6b2db1aSEric W. Biederman 	BUG_ON(!user_header);
358dbec2846SEric W. Biederman 	BUG_ON(!setup_userns_sysctls(&init_user_ns));
359dbec2846SEric W. Biederman #endif
360905ae01cSAlexey Gladkov 	hlist_add_ucounts(&init_ucounts);
36121d1c5e3SAlexey Gladkov 	inc_rlimit_ucounts(&init_ucounts, UCOUNT_RLIMIT_NPROC, 1);
362dbec2846SEric W. Biederman 	return 0;
363dbec2846SEric W. Biederman }
364dbec2846SEric W. Biederman subsys_initcall(user_namespace_sysctl_init);
365