xref: /openbmc/linux/kernel/ucount.c (revision de399236e240743ad2dd10d719c37b97ddf31996)
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 
61f153c224SSven Schnelle static long ue_zero = 0;
62f153c224SSven Schnelle static long ue_int_max = INT_MAX;
63f153c224SSven Schnelle 
6425f9c081SEric W. Biederman #define UCOUNT_ENTRY(name)					\
6525f9c081SEric W. Biederman 	{							\
6625f9c081SEric W. Biederman 		.procname	= name,				\
67f153c224SSven Schnelle 		.maxlen		= sizeof(long),			\
6825f9c081SEric W. Biederman 		.mode		= 0644,				\
69f153c224SSven Schnelle 		.proc_handler	= proc_doulongvec_minmax,	\
70f153c224SSven Schnelle 		.extra1		= &ue_zero,			\
71f153c224SSven Schnelle 		.extra2		= &ue_int_max,			\
7225f9c081SEric W. Biederman 	}
73f6b2db1aSEric W. Biederman static struct ctl_table user_table[] = {
7425f9c081SEric W. Biederman 	UCOUNT_ENTRY("max_user_namespaces"),
75f333c700SEric W. Biederman 	UCOUNT_ENTRY("max_pid_namespaces"),
76f7af3d1cSEric W. Biederman 	UCOUNT_ENTRY("max_uts_namespaces"),
77aba35661SEric W. Biederman 	UCOUNT_ENTRY("max_ipc_namespaces"),
7870328660SEric W. Biederman 	UCOUNT_ENTRY("max_net_namespaces"),
79537f7ccbSEric W. Biederman 	UCOUNT_ENTRY("max_mnt_namespaces"),
80d08311ddSEric W. Biederman 	UCOUNT_ENTRY("max_cgroup_namespaces"),
81eeec26d5SDmitry Safonov 	UCOUNT_ENTRY("max_time_namespaces"),
821cce1eeaSNikolay Borisov #ifdef CONFIG_INOTIFY_USER
831cce1eeaSNikolay Borisov 	UCOUNT_ENTRY("max_inotify_instances"),
841cce1eeaSNikolay Borisov 	UCOUNT_ENTRY("max_inotify_watches"),
851cce1eeaSNikolay Borisov #endif
865b8fea65SAmir Goldstein #ifdef CONFIG_FANOTIFY
875b8fea65SAmir Goldstein 	UCOUNT_ENTRY("max_fanotify_groups"),
885b8fea65SAmir Goldstein 	UCOUNT_ENTRY("max_fanotify_marks"),
895b8fea65SAmir Goldstein #endif
90dbec2846SEric W. Biederman 	{ }
91dbec2846SEric W. Biederman };
92dbec2846SEric W. Biederman #endif /* CONFIG_SYSCTL */
93dbec2846SEric W. Biederman 
94dbec2846SEric W. Biederman bool setup_userns_sysctls(struct user_namespace *ns)
95dbec2846SEric W. Biederman {
96dbec2846SEric W. Biederman #ifdef CONFIG_SYSCTL
97dbec2846SEric W. Biederman 	struct ctl_table *tbl;
980f538e3eSJan Kara 
990f538e3eSJan Kara 	BUILD_BUG_ON(ARRAY_SIZE(user_table) != UCOUNT_COUNTS + 1);
100dbec2846SEric W. Biederman 	setup_sysctl_set(&ns->set, &set_root, set_is_seen);
101f6b2db1aSEric W. Biederman 	tbl = kmemdup(user_table, sizeof(user_table), GFP_KERNEL);
102dbec2846SEric W. Biederman 	if (tbl) {
10325f9c081SEric W. Biederman 		int i;
10425f9c081SEric W. Biederman 		for (i = 0; i < UCOUNT_COUNTS; i++) {
10525f9c081SEric W. Biederman 			tbl[i].data = &ns->ucount_max[i];
10625f9c081SEric W. Biederman 		}
107f6b2db1aSEric W. Biederman 		ns->sysctls = __register_sysctl_table(&ns->set, "user", tbl);
108dbec2846SEric W. Biederman 	}
109dbec2846SEric W. Biederman 	if (!ns->sysctls) {
110dbec2846SEric W. Biederman 		kfree(tbl);
111dbec2846SEric W. Biederman 		retire_sysctl_set(&ns->set);
112dbec2846SEric W. Biederman 		return false;
113dbec2846SEric W. Biederman 	}
114dbec2846SEric W. Biederman #endif
115dbec2846SEric W. Biederman 	return true;
116dbec2846SEric W. Biederman }
117dbec2846SEric W. Biederman 
118dbec2846SEric W. Biederman void retire_userns_sysctls(struct user_namespace *ns)
119dbec2846SEric W. Biederman {
120dbec2846SEric W. Biederman #ifdef CONFIG_SYSCTL
121dbec2846SEric W. Biederman 	struct ctl_table *tbl;
122dbec2846SEric W. Biederman 
123dbec2846SEric W. Biederman 	tbl = ns->sysctls->ctl_table_arg;
124dbec2846SEric W. Biederman 	unregister_sysctl_table(ns->sysctls);
125dbec2846SEric W. Biederman 	retire_sysctl_set(&ns->set);
126dbec2846SEric W. Biederman 	kfree(tbl);
127dbec2846SEric W. Biederman #endif
128dbec2846SEric W. Biederman }
129dbec2846SEric W. Biederman 
130f6b2db1aSEric W. Biederman static struct ucounts *find_ucounts(struct user_namespace *ns, kuid_t uid, struct hlist_head *hashent)
131f6b2db1aSEric W. Biederman {
132f6b2db1aSEric W. Biederman 	struct ucounts *ucounts;
133f6b2db1aSEric W. Biederman 
134f6b2db1aSEric W. Biederman 	hlist_for_each_entry(ucounts, hashent, node) {
135f6b2db1aSEric W. Biederman 		if (uid_eq(ucounts->uid, uid) && (ucounts->ns == ns))
136f6b2db1aSEric W. Biederman 			return ucounts;
137f6b2db1aSEric W. Biederman 	}
138f6b2db1aSEric W. Biederman 	return NULL;
139f6b2db1aSEric W. Biederman }
140f6b2db1aSEric W. Biederman 
141905ae01cSAlexey Gladkov static void hlist_add_ucounts(struct ucounts *ucounts)
142905ae01cSAlexey Gladkov {
143905ae01cSAlexey Gladkov 	struct hlist_head *hashent = ucounts_hashentry(ucounts->ns, ucounts->uid);
144905ae01cSAlexey Gladkov 	spin_lock_irq(&ucounts_lock);
145905ae01cSAlexey Gladkov 	hlist_add_head(&ucounts->node, hashent);
146905ae01cSAlexey Gladkov 	spin_unlock_irq(&ucounts_lock);
147905ae01cSAlexey Gladkov }
148905ae01cSAlexey Gladkov 
149da70d310SEric W. Biederman static inline bool get_ucounts_or_wrap(struct ucounts *ucounts)
150da70d310SEric W. Biederman {
151da70d310SEric W. Biederman 	/* Returns true on a successful get, false if the count wraps. */
152da70d310SEric W. Biederman 	return !atomic_add_negative(1, &ucounts->count);
153da70d310SEric W. Biederman }
154da70d310SEric W. Biederman 
155b6c33652SAlexey Gladkov struct ucounts *get_ucounts(struct ucounts *ucounts)
156b6c33652SAlexey Gladkov {
157da70d310SEric W. Biederman 	if (!get_ucounts_or_wrap(ucounts)) {
158b6c33652SAlexey Gladkov 		put_ucounts(ucounts);
159b6c33652SAlexey Gladkov 		ucounts = NULL;
160b6c33652SAlexey Gladkov 	}
161b6c33652SAlexey Gladkov 	return ucounts;
162b6c33652SAlexey Gladkov }
163b6c33652SAlexey Gladkov 
164905ae01cSAlexey Gladkov struct ucounts *alloc_ucounts(struct user_namespace *ns, kuid_t uid)
165f6b2db1aSEric W. Biederman {
166f6b2db1aSEric W. Biederman 	struct hlist_head *hashent = ucounts_hashentry(ns, uid);
167f6b2db1aSEric W. Biederman 	struct ucounts *ucounts, *new;
168da70d310SEric W. Biederman 	bool wrapped;
169f6b2db1aSEric W. Biederman 
170880a3854SNikolay Borisov 	spin_lock_irq(&ucounts_lock);
171f6b2db1aSEric W. Biederman 	ucounts = find_ucounts(ns, uid, hashent);
172f6b2db1aSEric W. Biederman 	if (!ucounts) {
173880a3854SNikolay Borisov 		spin_unlock_irq(&ucounts_lock);
174f6b2db1aSEric W. Biederman 
175f6b2db1aSEric W. Biederman 		new = kzalloc(sizeof(*new), GFP_KERNEL);
176f6b2db1aSEric W. Biederman 		if (!new)
177f6b2db1aSEric W. Biederman 			return NULL;
178f6b2db1aSEric W. Biederman 
179f6b2db1aSEric W. Biederman 		new->ns = ns;
180f6b2db1aSEric W. Biederman 		new->uid = uid;
181b6c33652SAlexey Gladkov 		atomic_set(&new->count, 1);
182f6b2db1aSEric W. Biederman 
183880a3854SNikolay Borisov 		spin_lock_irq(&ucounts_lock);
184f6b2db1aSEric W. Biederman 		ucounts = find_ucounts(ns, uid, hashent);
185f6b2db1aSEric W. Biederman 		if (ucounts) {
186f6b2db1aSEric W. Biederman 			kfree(new);
187f6b2db1aSEric W. Biederman 		} else {
188f6b2db1aSEric W. Biederman 			hlist_add_head(&new->node, hashent);
189f9d87929SEric W. Biederman 			get_user_ns(new->ns);
190880a3854SNikolay Borisov 			spin_unlock_irq(&ucounts_lock);
191b6c33652SAlexey Gladkov 			return new;
192f6b2db1aSEric W. Biederman 		}
193905ae01cSAlexey Gladkov 	}
194da70d310SEric W. Biederman 	wrapped = !get_ucounts_or_wrap(ucounts);
195b6c33652SAlexey Gladkov 	spin_unlock_irq(&ucounts_lock);
196da70d310SEric W. Biederman 	if (wrapped) {
197345daff2SAlexey Gladkov 		put_ucounts(ucounts);
198345daff2SAlexey Gladkov 		return NULL;
199345daff2SAlexey Gladkov 	}
200f6b2db1aSEric W. Biederman 	return ucounts;
201f6b2db1aSEric W. Biederman }
202f6b2db1aSEric W. Biederman 
203905ae01cSAlexey Gladkov void put_ucounts(struct ucounts *ucounts)
204f6b2db1aSEric W. Biederman {
205880a3854SNikolay Borisov 	unsigned long flags;
206880a3854SNikolay Borisov 
207345daff2SAlexey Gladkov 	if (atomic_dec_and_lock_irqsave(&ucounts->count, &ucounts_lock, flags)) {
208f6b2db1aSEric W. Biederman 		hlist_del_init(&ucounts->node);
209880a3854SNikolay Borisov 		spin_unlock_irqrestore(&ucounts_lock, flags);
210f9d87929SEric W. Biederman 		put_user_ns(ucounts->ns);
211f6b2db1aSEric W. Biederman 		kfree(ucounts);
212f6b2db1aSEric W. Biederman 	}
213b6c33652SAlexey Gladkov }
214f6b2db1aSEric W. Biederman 
215f9c82a4eSAlexey Gladkov static inline bool atomic_long_inc_below(atomic_long_t *v, int u)
216b376c3e1SEric W. Biederman {
217f9c82a4eSAlexey Gladkov 	long c, old;
218f9c82a4eSAlexey Gladkov 	c = atomic_long_read(v);
219b376c3e1SEric W. Biederman 	for (;;) {
220b376c3e1SEric W. Biederman 		if (unlikely(c >= u))
221b376c3e1SEric W. Biederman 			return false;
222f9c82a4eSAlexey Gladkov 		old = atomic_long_cmpxchg(v, c, c+1);
223b376c3e1SEric W. Biederman 		if (likely(old == c))
224b376c3e1SEric W. Biederman 			return true;
225b376c3e1SEric W. Biederman 		c = old;
226b376c3e1SEric W. Biederman 	}
227b376c3e1SEric W. Biederman }
228b376c3e1SEric W. Biederman 
22925f9c081SEric W. Biederman struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid,
23025f9c081SEric W. Biederman 			   enum ucount_type type)
231b376c3e1SEric W. Biederman {
232f6b2db1aSEric W. Biederman 	struct ucounts *ucounts, *iter, *bad;
233f6b2db1aSEric W. Biederman 	struct user_namespace *tns;
234905ae01cSAlexey Gladkov 	ucounts = alloc_ucounts(ns, uid);
235f6b2db1aSEric W. Biederman 	for (iter = ucounts; iter; iter = tns->ucounts) {
236f9c82a4eSAlexey Gladkov 		long max;
237f6b2db1aSEric W. Biederman 		tns = iter->ns;
23825f9c081SEric W. Biederman 		max = READ_ONCE(tns->ucount_max[type]);
239f9c82a4eSAlexey Gladkov 		if (!atomic_long_inc_below(&iter->ucount[type], max))
240b376c3e1SEric W. Biederman 			goto fail;
241b376c3e1SEric W. Biederman 	}
242f6b2db1aSEric W. Biederman 	return ucounts;
243b376c3e1SEric W. Biederman fail:
244f6b2db1aSEric W. Biederman 	bad = iter;
245f6b2db1aSEric W. Biederman 	for (iter = ucounts; iter != bad; iter = iter->ns->ucounts)
246f9c82a4eSAlexey Gladkov 		atomic_long_dec(&iter->ucount[type]);
247b376c3e1SEric W. Biederman 
248f6b2db1aSEric W. Biederman 	put_ucounts(ucounts);
249f6b2db1aSEric W. Biederman 	return NULL;
250b376c3e1SEric W. Biederman }
251b376c3e1SEric W. Biederman 
25225f9c081SEric W. Biederman void dec_ucount(struct ucounts *ucounts, enum ucount_type type)
253b376c3e1SEric W. Biederman {
254f6b2db1aSEric W. Biederman 	struct ucounts *iter;
255f6b2db1aSEric W. Biederman 	for (iter = ucounts; iter; iter = iter->ns->ucounts) {
256f9c82a4eSAlexey Gladkov 		long dec = atomic_long_dec_if_positive(&iter->ucount[type]);
257b376c3e1SEric W. Biederman 		WARN_ON_ONCE(dec < 0);
258b376c3e1SEric W. Biederman 	}
259f6b2db1aSEric W. Biederman 	put_ucounts(ucounts);
260b376c3e1SEric W. Biederman }
261b376c3e1SEric W. Biederman 
262*de399236SAlexey Gladkov long inc_rlimit_ucounts(struct ucounts *ucounts, enum rlimit_type type, long v)
26321d1c5e3SAlexey Gladkov {
26421d1c5e3SAlexey Gladkov 	struct ucounts *iter;
26559ec7157SAlexey Gladkov 	long max = LONG_MAX;
26621d1c5e3SAlexey Gladkov 	long ret = 0;
26721d1c5e3SAlexey Gladkov 
26821d1c5e3SAlexey Gladkov 	for (iter = ucounts; iter; iter = iter->ns->ucounts) {
269*de399236SAlexey Gladkov 		long new = atomic_long_add_return(v, &iter->rlimit[type]);
27021d1c5e3SAlexey Gladkov 		if (new < 0 || new > max)
27121d1c5e3SAlexey Gladkov 			ret = LONG_MAX;
27221d1c5e3SAlexey Gladkov 		else if (iter == ucounts)
27321d1c5e3SAlexey Gladkov 			ret = new;
274*de399236SAlexey Gladkov 		max = get_userns_rlimit_max(iter->ns, type);
27521d1c5e3SAlexey Gladkov 	}
27621d1c5e3SAlexey Gladkov 	return ret;
27721d1c5e3SAlexey Gladkov }
27821d1c5e3SAlexey Gladkov 
279*de399236SAlexey Gladkov bool dec_rlimit_ucounts(struct ucounts *ucounts, enum rlimit_type type, long v)
28021d1c5e3SAlexey Gladkov {
28121d1c5e3SAlexey Gladkov 	struct ucounts *iter;
282f928ef68SEric W. Biederman 	long new = -1; /* Silence compiler warning */
28321d1c5e3SAlexey Gladkov 	for (iter = ucounts; iter; iter = iter->ns->ucounts) {
284*de399236SAlexey Gladkov 		long dec = atomic_long_sub_return(v, &iter->rlimit[type]);
28521d1c5e3SAlexey Gladkov 		WARN_ON_ONCE(dec < 0);
28621d1c5e3SAlexey Gladkov 		if (iter == ucounts)
28721d1c5e3SAlexey Gladkov 			new = dec;
28821d1c5e3SAlexey Gladkov 	}
28921d1c5e3SAlexey Gladkov 	return (new == 0);
29021d1c5e3SAlexey Gladkov }
29121d1c5e3SAlexey Gladkov 
29215bc01efSEric W. Biederman static void do_dec_rlimit_put_ucounts(struct ucounts *ucounts,
293*de399236SAlexey Gladkov 				struct ucounts *last, enum rlimit_type type)
29415bc01efSEric W. Biederman {
29515bc01efSEric W. Biederman 	struct ucounts *iter, *next;
29615bc01efSEric W. Biederman 	for (iter = ucounts; iter != last; iter = next) {
297*de399236SAlexey Gladkov 		long dec = atomic_long_sub_return(1, &iter->rlimit[type]);
29815bc01efSEric W. Biederman 		WARN_ON_ONCE(dec < 0);
29915bc01efSEric W. Biederman 		next = iter->ns->ucounts;
30015bc01efSEric W. Biederman 		if (dec == 0)
30115bc01efSEric W. Biederman 			put_ucounts(iter);
30215bc01efSEric W. Biederman 	}
30315bc01efSEric W. Biederman }
30415bc01efSEric W. Biederman 
305*de399236SAlexey Gladkov void dec_rlimit_put_ucounts(struct ucounts *ucounts, enum rlimit_type type)
30615bc01efSEric W. Biederman {
30715bc01efSEric W. Biederman 	do_dec_rlimit_put_ucounts(ucounts, NULL, type);
30815bc01efSEric W. Biederman }
30915bc01efSEric W. Biederman 
310*de399236SAlexey Gladkov long inc_rlimit_get_ucounts(struct ucounts *ucounts, enum rlimit_type type)
31115bc01efSEric W. Biederman {
31215bc01efSEric W. Biederman 	/* Caller must hold a reference to ucounts */
31315bc01efSEric W. Biederman 	struct ucounts *iter;
31459ec7157SAlexey Gladkov 	long max = LONG_MAX;
31515bc01efSEric W. Biederman 	long dec, ret = 0;
31615bc01efSEric W. Biederman 
31715bc01efSEric W. Biederman 	for (iter = ucounts; iter; iter = iter->ns->ucounts) {
318*de399236SAlexey Gladkov 		long new = atomic_long_add_return(1, &iter->rlimit[type]);
31915bc01efSEric W. Biederman 		if (new < 0 || new > max)
32015bc01efSEric W. Biederman 			goto unwind;
32115bc01efSEric W. Biederman 		if (iter == ucounts)
32215bc01efSEric W. Biederman 			ret = new;
323*de399236SAlexey Gladkov 		max = get_userns_rlimit_max(iter->ns, type);
32415bc01efSEric W. Biederman 		/*
32515bc01efSEric W. Biederman 		 * Grab an extra ucount reference for the caller when
32615bc01efSEric W. Biederman 		 * the rlimit count was previously 0.
32715bc01efSEric W. Biederman 		 */
32815bc01efSEric W. Biederman 		if (new != 1)
32915bc01efSEric W. Biederman 			continue;
33015bc01efSEric W. Biederman 		if (!get_ucounts(iter))
33115bc01efSEric W. Biederman 			goto dec_unwind;
33215bc01efSEric W. Biederman 	}
33315bc01efSEric W. Biederman 	return ret;
33415bc01efSEric W. Biederman dec_unwind:
335*de399236SAlexey Gladkov 	dec = atomic_long_sub_return(1, &iter->rlimit[type]);
33615bc01efSEric W. Biederman 	WARN_ON_ONCE(dec < 0);
33715bc01efSEric W. Biederman unwind:
33815bc01efSEric W. Biederman 	do_dec_rlimit_put_ucounts(ucounts, iter, type);
33915bc01efSEric W. Biederman 	return 0;
34015bc01efSEric W. Biederman }
34115bc01efSEric W. Biederman 
342*de399236SAlexey Gladkov bool is_rlimit_overlimit(struct ucounts *ucounts, enum rlimit_type type, unsigned long rlimit)
34321d1c5e3SAlexey Gladkov {
34421d1c5e3SAlexey Gladkov 	struct ucounts *iter;
34559ec7157SAlexey Gladkov 	long max = rlimit;
34659ec7157SAlexey Gladkov 	if (rlimit > LONG_MAX)
34759ec7157SAlexey Gladkov 		max = LONG_MAX;
34821d1c5e3SAlexey Gladkov 	for (iter = ucounts; iter; iter = iter->ns->ucounts) {
349*de399236SAlexey Gladkov 		long val = get_rlimit_value(iter, type);
3500cbae9e2SEric W. Biederman 		if (val < 0 || val > max)
35121d1c5e3SAlexey Gladkov 			return true;
352*de399236SAlexey Gladkov 		max = get_userns_rlimit_max(iter->ns, type);
35321d1c5e3SAlexey Gladkov 	}
35421d1c5e3SAlexey Gladkov 	return false;
35521d1c5e3SAlexey Gladkov }
35621d1c5e3SAlexey Gladkov 
357dbec2846SEric W. Biederman static __init int user_namespace_sysctl_init(void)
358dbec2846SEric W. Biederman {
359dbec2846SEric W. Biederman #ifdef CONFIG_SYSCTL
360f6b2db1aSEric W. Biederman 	static struct ctl_table_header *user_header;
361dbec2846SEric W. Biederman 	static struct ctl_table empty[1];
362dbec2846SEric W. Biederman 	/*
363f6b2db1aSEric W. Biederman 	 * It is necessary to register the user directory in the
364dbec2846SEric W. Biederman 	 * default set so that registrations in the child sets work
365dbec2846SEric W. Biederman 	 * properly.
366dbec2846SEric W. Biederman 	 */
367f6b2db1aSEric W. Biederman 	user_header = register_sysctl("user", empty);
368ed5bd7dcSLuis R. Rodriguez 	kmemleak_ignore(user_header);
369f6b2db1aSEric W. Biederman 	BUG_ON(!user_header);
370dbec2846SEric W. Biederman 	BUG_ON(!setup_userns_sysctls(&init_user_ns));
371dbec2846SEric W. Biederman #endif
372905ae01cSAlexey Gladkov 	hlist_add_ucounts(&init_ucounts);
37321d1c5e3SAlexey Gladkov 	inc_rlimit_ucounts(&init_ucounts, UCOUNT_RLIMIT_NPROC, 1);
374dbec2846SEric W. Biederman 	return 0;
375dbec2846SEric W. Biederman }
376dbec2846SEric W. Biederman subsys_initcall(user_namespace_sysctl_init);
377