Blame view

kernel/ucount.c 5.72 KB
dbec28460   Eric W. Biederman   userns: Add per u...
1
2
3
4
5
6
7
8
9
10
  /*
   *  This program is free software; you can redistribute it and/or
   *  modify it under the terms of the GNU General Public License as
   *  published by the Free Software Foundation, version 2 of the
   *  License.
   */
  
  #include <linux/stat.h>
  #include <linux/sysctl.h>
  #include <linux/slab.h>
5b825c3af   Ingo Molnar   sched/headers: Pr...
11
  #include <linux/cred.h>
f6b2db1a3   Eric W. Biederman   userns: Make the ...
12
  #include <linux/hash.h>
514c60324   Randy Dunlap   headers: untangle...
13
  #include <linux/kmemleak.h>
dbec28460   Eric W. Biederman   userns: Add per u...
14
  #include <linux/user_namespace.h>
f6b2db1a3   Eric W. Biederman   userns: Make the ...
15
16
17
18
19
20
21
22
23
  #define UCOUNTS_HASHTABLE_BITS 10
  static struct hlist_head ucounts_hashtable[(1 << UCOUNTS_HASHTABLE_BITS)];
  static DEFINE_SPINLOCK(ucounts_lock);
  
  #define ucounts_hashfn(ns, uid)						\
  	hash_long((unsigned long)__kuid_val(uid) + (unsigned long)(ns), \
  		  UCOUNTS_HASHTABLE_BITS)
  #define ucounts_hashentry(ns, uid)	\
  	(ucounts_hashtable + ucounts_hashfn(ns, uid))
dbec28460   Eric W. Biederman   userns: Add per u...
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  #ifdef CONFIG_SYSCTL
  static struct ctl_table_set *
  set_lookup(struct ctl_table_root *root)
  {
  	return &current_user_ns()->set;
  }
  
  static int set_is_seen(struct ctl_table_set *set)
  {
  	return &current_user_ns()->set == set;
  }
  
  static int set_permissions(struct ctl_table_header *head,
  				  struct ctl_table *table)
  {
  	struct user_namespace *user_ns =
  		container_of(head->set, struct user_namespace, set);
  	int mode;
  
  	/* Allow users with CAP_SYS_RESOURCE unrestrained access */
  	if (ns_capable(user_ns, CAP_SYS_RESOURCE))
  		mode = (table->mode & S_IRWXU) >> 6;
  	else
  	/* Allow all others at most read-only access */
  		mode = table->mode & S_IROTH;
  	return (mode << 6) | (mode << 3) | mode;
  }
  
  static struct ctl_table_root set_root = {
  	.lookup = set_lookup,
  	.permissions = set_permissions,
  };
b376c3e1b   Eric W. Biederman   userns: Add a lim...
56
57
  static int zero = 0;
  static int int_max = INT_MAX;
1cce1eea0   Nikolay Borisov   inotify: Convert ...
58
  #define UCOUNT_ENTRY(name)				\
25f9c0817   Eric W. Biederman   userns: Generaliz...
59
60
61
62
63
64
65
66
  	{						\
  		.procname	= name,			\
  		.maxlen		= sizeof(int),		\
  		.mode		= 0644,			\
  		.proc_handler	= proc_dointvec_minmax,	\
  		.extra1		= &zero,		\
  		.extra2		= &int_max,		\
  	}
f6b2db1a3   Eric W. Biederman   userns: Make the ...
67
  static struct ctl_table user_table[] = {
25f9c0817   Eric W. Biederman   userns: Generaliz...
68
  	UCOUNT_ENTRY("max_user_namespaces"),
f333c700c   Eric W. Biederman   pidns: Add a limi...
69
  	UCOUNT_ENTRY("max_pid_namespaces"),
f7af3d1c0   Eric W. Biederman   utsns: Add a limi...
70
  	UCOUNT_ENTRY("max_uts_namespaces"),
aba356616   Eric W. Biederman   ipcns: Add a lim...
71
  	UCOUNT_ENTRY("max_ipc_namespaces"),
703286608   Eric W. Biederman   netns: Add a limi...
72
  	UCOUNT_ENTRY("max_net_namespaces"),
537f7ccb3   Eric W. Biederman   mntns: Add a limi...
73
  	UCOUNT_ENTRY("max_mnt_namespaces"),
d08311dd6   Eric W. Biederman   cgroupns: Add a l...
74
  	UCOUNT_ENTRY("max_cgroup_namespaces"),
1cce1eea0   Nikolay Borisov   inotify: Convert ...
75
76
77
78
  #ifdef CONFIG_INOTIFY_USER
  	UCOUNT_ENTRY("max_inotify_instances"),
  	UCOUNT_ENTRY("max_inotify_watches"),
  #endif
dbec28460   Eric W. Biederman   userns: Add per u...
79
80
81
82
83
84
85
86
87
  	{ }
  };
  #endif /* CONFIG_SYSCTL */
  
  bool setup_userns_sysctls(struct user_namespace *ns)
  {
  #ifdef CONFIG_SYSCTL
  	struct ctl_table *tbl;
  	setup_sysctl_set(&ns->set, &set_root, set_is_seen);
f6b2db1a3   Eric W. Biederman   userns: Make the ...
88
  	tbl = kmemdup(user_table, sizeof(user_table), GFP_KERNEL);
dbec28460   Eric W. Biederman   userns: Add per u...
89
  	if (tbl) {
25f9c0817   Eric W. Biederman   userns: Generaliz...
90
91
92
93
  		int i;
  		for (i = 0; i < UCOUNT_COUNTS; i++) {
  			tbl[i].data = &ns->ucount_max[i];
  		}
f6b2db1a3   Eric W. Biederman   userns: Make the ...
94
  		ns->sysctls = __register_sysctl_table(&ns->set, "user", tbl);
dbec28460   Eric W. Biederman   userns: Add per u...
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  	}
  	if (!ns->sysctls) {
  		kfree(tbl);
  		retire_sysctl_set(&ns->set);
  		return false;
  	}
  #endif
  	return true;
  }
  
  void retire_userns_sysctls(struct user_namespace *ns)
  {
  #ifdef CONFIG_SYSCTL
  	struct ctl_table *tbl;
  
  	tbl = ns->sysctls->ctl_table_arg;
  	unregister_sysctl_table(ns->sysctls);
  	retire_sysctl_set(&ns->set);
  	kfree(tbl);
  #endif
  }
f6b2db1a3   Eric W. Biederman   userns: Make the ...
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  static struct ucounts *find_ucounts(struct user_namespace *ns, kuid_t uid, struct hlist_head *hashent)
  {
  	struct ucounts *ucounts;
  
  	hlist_for_each_entry(ucounts, hashent, node) {
  		if (uid_eq(ucounts->uid, uid) && (ucounts->ns == ns))
  			return ucounts;
  	}
  	return NULL;
  }
  
  static struct ucounts *get_ucounts(struct user_namespace *ns, kuid_t uid)
  {
  	struct hlist_head *hashent = ucounts_hashentry(ns, uid);
  	struct ucounts *ucounts, *new;
880a38547   Nikolay Borisov   userns: Make ucou...
131
  	spin_lock_irq(&ucounts_lock);
f6b2db1a3   Eric W. Biederman   userns: Make the ...
132
133
  	ucounts = find_ucounts(ns, uid, hashent);
  	if (!ucounts) {
880a38547   Nikolay Borisov   userns: Make ucou...
134
  		spin_unlock_irq(&ucounts_lock);
f6b2db1a3   Eric W. Biederman   userns: Make the ...
135
136
137
138
139
140
141
  
  		new = kzalloc(sizeof(*new), GFP_KERNEL);
  		if (!new)
  			return NULL;
  
  		new->ns = ns;
  		new->uid = uid;
040757f73   Eric W. Biederman   ucount: Remove th...
142
  		new->count = 0;
f6b2db1a3   Eric W. Biederman   userns: Make the ...
143

880a38547   Nikolay Borisov   userns: Make ucou...
144
  		spin_lock_irq(&ucounts_lock);
f6b2db1a3   Eric W. Biederman   userns: Make the ...
145
146
147
148
149
150
151
152
  		ucounts = find_ucounts(ns, uid, hashent);
  		if (ucounts) {
  			kfree(new);
  		} else {
  			hlist_add_head(&new->node, hashent);
  			ucounts = new;
  		}
  	}
040757f73   Eric W. Biederman   ucount: Remove th...
153
  	if (ucounts->count == INT_MAX)
f6b2db1a3   Eric W. Biederman   userns: Make the ...
154
  		ucounts = NULL;
040757f73   Eric W. Biederman   ucount: Remove th...
155
156
  	else
  		ucounts->count += 1;
880a38547   Nikolay Borisov   userns: Make ucou...
157
  	spin_unlock_irq(&ucounts_lock);
f6b2db1a3   Eric W. Biederman   userns: Make the ...
158
159
160
161
162
  	return ucounts;
  }
  
  static void put_ucounts(struct ucounts *ucounts)
  {
880a38547   Nikolay Borisov   userns: Make ucou...
163
  	unsigned long flags;
040757f73   Eric W. Biederman   ucount: Remove th...
164
165
166
  	spin_lock_irqsave(&ucounts_lock, flags);
  	ucounts->count -= 1;
  	if (!ucounts->count)
f6b2db1a3   Eric W. Biederman   userns: Make the ...
167
  		hlist_del_init(&ucounts->node);
040757f73   Eric W. Biederman   ucount: Remove th...
168
169
170
  	else
  		ucounts = NULL;
  	spin_unlock_irqrestore(&ucounts_lock, flags);
f6b2db1a3   Eric W. Biederman   userns: Make the ...
171

040757f73   Eric W. Biederman   ucount: Remove th...
172
  	kfree(ucounts);
f6b2db1a3   Eric W. Biederman   userns: Make the ...
173
  }
b376c3e1b   Eric W. Biederman   userns: Add a lim...
174
175
176
177
178
179
180
181
182
183
184
185
186
  static inline bool atomic_inc_below(atomic_t *v, int u)
  {
  	int c, old;
  	c = atomic_read(v);
  	for (;;) {
  		if (unlikely(c >= u))
  			return false;
  		old = atomic_cmpxchg(v, c, c+1);
  		if (likely(old == c))
  			return true;
  		c = old;
  	}
  }
25f9c0817   Eric W. Biederman   userns: Generaliz...
187
188
  struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid,
  			   enum ucount_type type)
b376c3e1b   Eric W. Biederman   userns: Add a lim...
189
  {
f6b2db1a3   Eric W. Biederman   userns: Make the ...
190
191
192
193
194
195
  	struct ucounts *ucounts, *iter, *bad;
  	struct user_namespace *tns;
  	ucounts = get_ucounts(ns, uid);
  	for (iter = ucounts; iter; iter = tns->ucounts) {
  		int max;
  		tns = iter->ns;
25f9c0817   Eric W. Biederman   userns: Generaliz...
196
197
  		max = READ_ONCE(tns->ucount_max[type]);
  		if (!atomic_inc_below(&iter->ucount[type], max))
b376c3e1b   Eric W. Biederman   userns: Add a lim...
198
199
  			goto fail;
  	}
f6b2db1a3   Eric W. Biederman   userns: Make the ...
200
  	return ucounts;
b376c3e1b   Eric W. Biederman   userns: Add a lim...
201
  fail:
f6b2db1a3   Eric W. Biederman   userns: Make the ...
202
203
  	bad = iter;
  	for (iter = ucounts; iter != bad; iter = iter->ns->ucounts)
25f9c0817   Eric W. Biederman   userns: Generaliz...
204
  		atomic_dec(&iter->ucount[type]);
b376c3e1b   Eric W. Biederman   userns: Add a lim...
205

f6b2db1a3   Eric W. Biederman   userns: Make the ...
206
207
  	put_ucounts(ucounts);
  	return NULL;
b376c3e1b   Eric W. Biederman   userns: Add a lim...
208
  }
25f9c0817   Eric W. Biederman   userns: Generaliz...
209
  void dec_ucount(struct ucounts *ucounts, enum ucount_type type)
b376c3e1b   Eric W. Biederman   userns: Add a lim...
210
  {
f6b2db1a3   Eric W. Biederman   userns: Make the ...
211
212
  	struct ucounts *iter;
  	for (iter = ucounts; iter; iter = iter->ns->ucounts) {
25f9c0817   Eric W. Biederman   userns: Generaliz...
213
  		int dec = atomic_dec_if_positive(&iter->ucount[type]);
b376c3e1b   Eric W. Biederman   userns: Add a lim...
214
215
  		WARN_ON_ONCE(dec < 0);
  	}
f6b2db1a3   Eric W. Biederman   userns: Make the ...
216
  	put_ucounts(ucounts);
b376c3e1b   Eric W. Biederman   userns: Add a lim...
217
  }
dbec28460   Eric W. Biederman   userns: Add per u...
218
219
220
  static __init int user_namespace_sysctl_init(void)
  {
  #ifdef CONFIG_SYSCTL
f6b2db1a3   Eric W. Biederman   userns: Make the ...
221
  	static struct ctl_table_header *user_header;
dbec28460   Eric W. Biederman   userns: Add per u...
222
223
  	static struct ctl_table empty[1];
  	/*
f6b2db1a3   Eric W. Biederman   userns: Make the ...
224
  	 * It is necessary to register the user directory in the
dbec28460   Eric W. Biederman   userns: Add per u...
225
226
227
  	 * default set so that registrations in the child sets work
  	 * properly.
  	 */
f6b2db1a3   Eric W. Biederman   userns: Make the ...
228
  	user_header = register_sysctl("user", empty);
ed5bd7dc8   Luis R. Rodriguez   kernel/ucount.c: ...
229
  	kmemleak_ignore(user_header);
f6b2db1a3   Eric W. Biederman   userns: Make the ...
230
  	BUG_ON(!user_header);
dbec28460   Eric W. Biederman   userns: Add per u...
231
232
233
234
235
  	BUG_ON(!setup_userns_sysctls(&init_user_ns));
  #endif
  	return 0;
  }
  subsys_initcall(user_namespace_sysctl_init);