Blame view

kernel/user_namespace.c 25.5 KB
acce292c8   Cedric Le Goater   user namespace: a...
1
2
3
4
5
6
  /*
   *  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.
   */
9984de1a5   Paul Gortmaker   kernel: Map most ...
7
  #include <linux/export.h>
acce292c8   Cedric Le Goater   user namespace: a...
8
  #include <linux/nsproxy.h>
1aeb272cf   Robert P. J. Day   kernel: explicitl...
9
  #include <linux/slab.h>
acce292c8   Cedric Le Goater   user namespace: a...
10
  #include <linux/user_namespace.h>
0bb80f240   David Howells   proc: Split the n...
11
  #include <linux/proc_ns.h>
5c1469de7   Eric W. Biederman   user_ns: Introduc...
12
  #include <linux/highuid.h>
18b6e0414   Serge Hallyn   User namespaces: ...
13
  #include <linux/cred.h>
973c59142   Eric W. Biederman   userns: Start out...
14
  #include <linux/securebits.h>
22d917d80   Eric W. Biederman   userns: Rework th...
15
16
17
18
19
20
21
  #include <linux/keyctl.h>
  #include <linux/key-type.h>
  #include <keys/user-type.h>
  #include <linux/seq_file.h>
  #include <linux/fs.h>
  #include <linux/uaccess.h>
  #include <linux/ctype.h>
f76d207a6   Eric W. Biederman   userns: Add kproj...
22
  #include <linux/projid.h>
e66eded83   Eric W. Biederman   userns: Don't all...
23
  #include <linux/fs_struct.h>
acce292c8   Cedric Le Goater   user namespace: a...
24

6164281ab   Pavel Emelyanov   user_ns: improve ...
25
  static struct kmem_cache *user_ns_cachep __read_mostly;
f0d62aec9   Eric W. Biederman   userns: Rename id...
26
  static DEFINE_MUTEX(userns_state_mutex);
6164281ab   Pavel Emelyanov   user_ns: improve ...
27

6708075f1   Eric W. Biederman   userns: Don't let...
28
29
  static bool new_idmap_permitted(const struct file *file,
  				struct user_namespace *ns, int cap_setid,
22d917d80   Eric W. Biederman   userns: Rework th...
30
  				struct uid_gid_map *map);
cde1975bc   Eric W. Biederman   userns: Implent p...
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  static void set_cred_user_ns(struct cred *cred, struct user_namespace *user_ns)
  {
  	/* Start with the same capabilities as init but useless for doing
  	 * anything as the capabilities are bound to the new user namespace.
  	 */
  	cred->securebits = SECUREBITS_DEFAULT;
  	cred->cap_inheritable = CAP_EMPTY_SET;
  	cred->cap_permitted = CAP_FULL_SET;
  	cred->cap_effective = CAP_FULL_SET;
  	cred->cap_bset = CAP_FULL_SET;
  #ifdef CONFIG_KEYS
  	key_put(cred->request_key_auth);
  	cred->request_key_auth = NULL;
  #endif
  	/* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
  	cred->user_ns = user_ns;
  }
77ec739d8   Serge E. Hallyn   user namespace: a...
48
  /*
18b6e0414   Serge Hallyn   User namespaces: ...
49
50
51
52
53
54
   * Create a new user namespace, deriving the creator from the user in the
   * passed credentials, and replacing that user with the new root user for the
   * new namespace.
   *
   * This is called by copy_creds(), which will finish setting the target task's
   * credentials.
77ec739d8   Serge E. Hallyn   user namespace: a...
55
   */
18b6e0414   Serge Hallyn   User namespaces: ...
56
  int create_user_ns(struct cred *new)
77ec739d8   Serge E. Hallyn   user namespace: a...
57
  {
0093ccb68   Eric W. Biederman   cred: Refcount th...
58
  	struct user_namespace *ns, *parent_ns = new->user_ns;
078de5f70   Eric W. Biederman   userns: Store uid...
59
60
  	kuid_t owner = new->euid;
  	kgid_t group = new->egid;
98f842e67   Eric W. Biederman   proc: Usable inod...
61
  	int ret;
783291e69   Eric W. Biederman   userns: Simplify ...
62

8742f229b   Oleg Nesterov   userns: limit the...
63
64
  	if (parent_ns->level > 32)
  		return -EUSERS;
3151527ee   Eric W. Biederman   userns: Don't al...
65
66
67
68
69
70
71
72
  	/*
  	 * Verify that we can not violate the policy of which files
  	 * may be accessed that is specified by the root directory,
  	 * by verifing that the root directory is at the root of the
  	 * mount namespace which allows all files to be accessed.
  	 */
  	if (current_chrooted())
  		return -EPERM;
783291e69   Eric W. Biederman   userns: Simplify ...
73
74
75
76
77
78
79
  	/* The creator needs a mapping in the parent user namespace
  	 * or else we won't be able to reasonably tell userspace who
  	 * created a user_namespace.
  	 */
  	if (!kuid_has_mapping(parent_ns, owner) ||
  	    !kgid_has_mapping(parent_ns, group))
  		return -EPERM;
77ec739d8   Serge E. Hallyn   user namespace: a...
80

22d917d80   Eric W. Biederman   userns: Rework th...
81
  	ns = kmem_cache_zalloc(user_ns_cachep, GFP_KERNEL);
77ec739d8   Serge E. Hallyn   user namespace: a...
82
  	if (!ns)
18b6e0414   Serge Hallyn   User namespaces: ...
83
  		return -ENOMEM;
77ec739d8   Serge E. Hallyn   user namespace: a...
84

6344c433a   Al Viro   new helpers: ns_a...
85
  	ret = ns_alloc_inum(&ns->ns);
98f842e67   Eric W. Biederman   proc: Usable inod...
86
87
88
89
  	if (ret) {
  		kmem_cache_free(user_ns_cachep, ns);
  		return ret;
  	}
33c429405   Al Viro   copy address of p...
90
  	ns->ns.ops = &userns_operations;
98f842e67   Eric W. Biederman   proc: Usable inod...
91

c61a2810a   Eric W. Biederman   userns: Avoid rec...
92
  	atomic_set(&ns->count, 1);
cde1975bc   Eric W. Biederman   userns: Implent p...
93
  	/* Leave the new->user_ns reference with the new user namespace. */
aeb3ae9da   Eric W. Biederman   userns: Add an ex...
94
  	ns->parent = parent_ns;
8742f229b   Oleg Nesterov   userns: limit the...
95
  	ns->level = parent_ns->level + 1;
783291e69   Eric W. Biederman   userns: Simplify ...
96
97
  	ns->owner = owner;
  	ns->group = group;
22d917d80   Eric W. Biederman   userns: Rework th...
98

9cc46516d   Eric W. Biederman   userns: Add a kno...
99
100
101
102
  	/* Inherit USERNS_SETGROUPS_ALLOWED from our parent */
  	mutex_lock(&userns_state_mutex);
  	ns->flags = parent_ns->flags;
  	mutex_unlock(&userns_state_mutex);
cde1975bc   Eric W. Biederman   userns: Implent p...
103
  	set_cred_user_ns(new, ns);
0093ccb68   Eric W. Biederman   cred: Refcount th...
104

f36f8c75a   David Howells   KEYS: Add per-use...
105
106
107
  #ifdef CONFIG_PERSISTENT_KEYRINGS
  	init_rwsem(&ns->persistent_keyring_register_sem);
  #endif
18b6e0414   Serge Hallyn   User namespaces: ...
108
  	return 0;
acce292c8   Cedric Le Goater   user namespace: a...
109
  }
b2e0d9870   Eric W. Biederman   userns: Implement...
110
111
112
  int unshare_userns(unsigned long unshare_flags, struct cred **new_cred)
  {
  	struct cred *cred;
6160968ce   Oleg Nesterov   userns: unshare_u...
113
  	int err = -ENOMEM;
b2e0d9870   Eric W. Biederman   userns: Implement...
114
115
116
117
118
  
  	if (!(unshare_flags & CLONE_NEWUSER))
  		return 0;
  
  	cred = prepare_creds();
6160968ce   Oleg Nesterov   userns: unshare_u...
119
120
121
122
123
124
125
  	if (cred) {
  		err = create_user_ns(cred);
  		if (err)
  			put_cred(cred);
  		else
  			*new_cred = cred;
  	}
b2e0d9870   Eric W. Biederman   userns: Implement...
126

6160968ce   Oleg Nesterov   userns: unshare_u...
127
  	return err;
b2e0d9870   Eric W. Biederman   userns: Implement...
128
  }
c61a2810a   Eric W. Biederman   userns: Avoid rec...
129
  void free_user_ns(struct user_namespace *ns)
acce292c8   Cedric Le Goater   user namespace: a...
130
  {
c61a2810a   Eric W. Biederman   userns: Avoid rec...
131
  	struct user_namespace *parent;
783291e69   Eric W. Biederman   userns: Simplify ...
132

c61a2810a   Eric W. Biederman   userns: Avoid rec...
133
134
  	do {
  		parent = ns->parent;
f36f8c75a   David Howells   KEYS: Add per-use...
135
136
137
  #ifdef CONFIG_PERSISTENT_KEYRINGS
  		key_put(ns->persistent_keyring_register);
  #endif
6344c433a   Al Viro   new helpers: ns_a...
138
  		ns_free_inum(&ns->ns);
c61a2810a   Eric W. Biederman   userns: Avoid rec...
139
140
141
  		kmem_cache_free(user_ns_cachep, ns);
  		ns = parent;
  	} while (atomic_dec_and_test(&parent->count));
acce292c8   Cedric Le Goater   user namespace: a...
142
  }
6a3fd92e7   Michael Halcrow   eCryptfs: make ke...
143
  EXPORT_SYMBOL(free_user_ns);
5c1469de7   Eric W. Biederman   user_ns: Introduc...
144

22d917d80   Eric W. Biederman   userns: Rework th...
145
  static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count)
5c1469de7   Eric W. Biederman   user_ns: Introduc...
146
  {
22d917d80   Eric W. Biederman   userns: Rework th...
147
148
  	unsigned idx, extents;
  	u32 first, last, id2;
5c1469de7   Eric W. Biederman   user_ns: Introduc...
149

22d917d80   Eric W. Biederman   userns: Rework th...
150
  	id2 = id + count - 1;
5c1469de7   Eric W. Biederman   user_ns: Introduc...
151

22d917d80   Eric W. Biederman   userns: Rework th...
152
153
  	/* Find the matching extent */
  	extents = map->nr_extents;
e79323bd8   Mikulas Patocka   user namespace: f...
154
  	smp_rmb();
22d917d80   Eric W. Biederman   userns: Rework th...
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
  	for (idx = 0; idx < extents; idx++) {
  		first = map->extent[idx].first;
  		last = first + map->extent[idx].count - 1;
  		if (id >= first && id <= last &&
  		    (id2 >= first && id2 <= last))
  			break;
  	}
  	/* Map the id or note failure */
  	if (idx < extents)
  		id = (id - first) + map->extent[idx].lower_first;
  	else
  		id = (u32) -1;
  
  	return id;
  }
  
  static u32 map_id_down(struct uid_gid_map *map, u32 id)
  {
  	unsigned idx, extents;
  	u32 first, last;
  
  	/* Find the matching extent */
  	extents = map->nr_extents;
e79323bd8   Mikulas Patocka   user namespace: f...
178
  	smp_rmb();
22d917d80   Eric W. Biederman   userns: Rework th...
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
  	for (idx = 0; idx < extents; idx++) {
  		first = map->extent[idx].first;
  		last = first + map->extent[idx].count - 1;
  		if (id >= first && id <= last)
  			break;
  	}
  	/* Map the id or note failure */
  	if (idx < extents)
  		id = (id - first) + map->extent[idx].lower_first;
  	else
  		id = (u32) -1;
  
  	return id;
  }
  
  static u32 map_id_up(struct uid_gid_map *map, u32 id)
  {
  	unsigned idx, extents;
  	u32 first, last;
  
  	/* Find the matching extent */
  	extents = map->nr_extents;
e79323bd8   Mikulas Patocka   user namespace: f...
201
  	smp_rmb();
22d917d80   Eric W. Biederman   userns: Rework th...
202
203
204
205
206
  	for (idx = 0; idx < extents; idx++) {
  		first = map->extent[idx].lower_first;
  		last = first + map->extent[idx].count - 1;
  		if (id >= first && id <= last)
  			break;
5c1469de7   Eric W. Biederman   user_ns: Introduc...
207
  	}
22d917d80   Eric W. Biederman   userns: Rework th...
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
  	/* Map the id or note failure */
  	if (idx < extents)
  		id = (id - first) + map->extent[idx].first;
  	else
  		id = (u32) -1;
  
  	return id;
  }
  
  /**
   *	make_kuid - Map a user-namespace uid pair into a kuid.
   *	@ns:  User namespace that the uid is in
   *	@uid: User identifier
   *
   *	Maps a user-namespace uid pair into a kernel internal kuid,
   *	and returns that kuid.
   *
   *	When there is no mapping defined for the user-namespace uid
   *	pair INVALID_UID is returned.  Callers are expected to test
b080e047a   Brian Campbell   user_namespace.c:...
227
   *	for and handle INVALID_UID being returned.  INVALID_UID
22d917d80   Eric W. Biederman   userns: Rework th...
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
   *	may be tested for using uid_valid().
   */
  kuid_t make_kuid(struct user_namespace *ns, uid_t uid)
  {
  	/* Map the uid to a global kernel uid */
  	return KUIDT_INIT(map_id_down(&ns->uid_map, uid));
  }
  EXPORT_SYMBOL(make_kuid);
  
  /**
   *	from_kuid - Create a uid from a kuid user-namespace pair.
   *	@targ: The user namespace we want a uid in.
   *	@kuid: The kernel internal uid to start with.
   *
   *	Map @kuid into the user-namespace specified by @targ and
   *	return the resulting uid.
   *
   *	There is always a mapping into the initial user_namespace.
   *
   *	If @kuid has no mapping in @targ (uid_t)-1 is returned.
   */
  uid_t from_kuid(struct user_namespace *targ, kuid_t kuid)
  {
  	/* Map the uid from a global kernel uid */
  	return map_id_up(&targ->uid_map, __kuid_val(kuid));
  }
  EXPORT_SYMBOL(from_kuid);
  
  /**
   *	from_kuid_munged - Create a uid from a kuid user-namespace pair.
   *	@targ: The user namespace we want a uid in.
   *	@kuid: The kernel internal uid to start with.
   *
   *	Map @kuid into the user-namespace specified by @targ and
   *	return the resulting uid.
   *
   *	There is always a mapping into the initial user_namespace.
   *
   *	Unlike from_kuid from_kuid_munged never fails and always
   *	returns a valid uid.  This makes from_kuid_munged appropriate
   *	for use in syscalls like stat and getuid where failing the
   *	system call and failing to provide a valid uid are not an
   *	options.
   *
   *	If @kuid has no mapping in @targ overflowuid is returned.
   */
  uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid)
  {
  	uid_t uid;
  	uid = from_kuid(targ, kuid);
  
  	if (uid == (uid_t) -1)
  		uid = overflowuid;
  	return uid;
  }
  EXPORT_SYMBOL(from_kuid_munged);
  
  /**
   *	make_kgid - Map a user-namespace gid pair into a kgid.
   *	@ns:  User namespace that the gid is in
68a9a435e   Fabian Frederick   kernel/user_names...
288
   *	@gid: group identifier
22d917d80   Eric W. Biederman   userns: Rework th...
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
   *
   *	Maps a user-namespace gid pair into a kernel internal kgid,
   *	and returns that kgid.
   *
   *	When there is no mapping defined for the user-namespace gid
   *	pair INVALID_GID is returned.  Callers are expected to test
   *	for and handle INVALID_GID being returned.  INVALID_GID may be
   *	tested for using gid_valid().
   */
  kgid_t make_kgid(struct user_namespace *ns, gid_t gid)
  {
  	/* Map the gid to a global kernel gid */
  	return KGIDT_INIT(map_id_down(&ns->gid_map, gid));
  }
  EXPORT_SYMBOL(make_kgid);
  
  /**
   *	from_kgid - Create a gid from a kgid user-namespace pair.
   *	@targ: The user namespace we want a gid in.
   *	@kgid: The kernel internal gid to start with.
   *
   *	Map @kgid into the user-namespace specified by @targ and
   *	return the resulting gid.
   *
   *	There is always a mapping into the initial user_namespace.
   *
   *	If @kgid has no mapping in @targ (gid_t)-1 is returned.
   */
  gid_t from_kgid(struct user_namespace *targ, kgid_t kgid)
  {
  	/* Map the gid from a global kernel gid */
  	return map_id_up(&targ->gid_map, __kgid_val(kgid));
  }
  EXPORT_SYMBOL(from_kgid);
  
  /**
   *	from_kgid_munged - Create a gid from a kgid user-namespace pair.
   *	@targ: The user namespace we want a gid in.
   *	@kgid: The kernel internal gid to start with.
   *
   *	Map @kgid into the user-namespace specified by @targ and
   *	return the resulting gid.
   *
   *	There is always a mapping into the initial user_namespace.
   *
   *	Unlike from_kgid from_kgid_munged never fails and always
   *	returns a valid gid.  This makes from_kgid_munged appropriate
   *	for use in syscalls like stat and getgid where failing the
   *	system call and failing to provide a valid gid are not options.
   *
   *	If @kgid has no mapping in @targ overflowgid is returned.
   */
  gid_t from_kgid_munged(struct user_namespace *targ, kgid_t kgid)
  {
  	gid_t gid;
  	gid = from_kgid(targ, kgid);
  
  	if (gid == (gid_t) -1)
  		gid = overflowgid;
  	return gid;
  }
  EXPORT_SYMBOL(from_kgid_munged);
f76d207a6   Eric W. Biederman   userns: Add kproj...
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
  /**
   *	make_kprojid - Map a user-namespace projid pair into a kprojid.
   *	@ns:  User namespace that the projid is in
   *	@projid: Project identifier
   *
   *	Maps a user-namespace uid pair into a kernel internal kuid,
   *	and returns that kuid.
   *
   *	When there is no mapping defined for the user-namespace projid
   *	pair INVALID_PROJID is returned.  Callers are expected to test
   *	for and handle handle INVALID_PROJID being returned.  INVALID_PROJID
   *	may be tested for using projid_valid().
   */
  kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid)
  {
  	/* Map the uid to a global kernel uid */
  	return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid));
  }
  EXPORT_SYMBOL(make_kprojid);
  
  /**
   *	from_kprojid - Create a projid from a kprojid user-namespace pair.
   *	@targ: The user namespace we want a projid in.
   *	@kprojid: The kernel internal project identifier to start with.
   *
   *	Map @kprojid into the user-namespace specified by @targ and
   *	return the resulting projid.
   *
   *	There is always a mapping into the initial user_namespace.
   *
   *	If @kprojid has no mapping in @targ (projid_t)-1 is returned.
   */
  projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid)
  {
  	/* Map the uid from a global kernel uid */
  	return map_id_up(&targ->projid_map, __kprojid_val(kprojid));
  }
  EXPORT_SYMBOL(from_kprojid);
  
  /**
   *	from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
   *	@targ: The user namespace we want a projid in.
   *	@kprojid: The kernel internal projid to start with.
   *
   *	Map @kprojid into the user-namespace specified by @targ and
   *	return the resulting projid.
   *
   *	There is always a mapping into the initial user_namespace.
   *
   *	Unlike from_kprojid from_kprojid_munged never fails and always
   *	returns a valid projid.  This makes from_kprojid_munged
   *	appropriate for use in syscalls like stat and where
   *	failing the system call and failing to provide a valid projid are
   *	not an options.
   *
   *	If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
   */
  projid_t from_kprojid_munged(struct user_namespace *targ, kprojid_t kprojid)
  {
  	projid_t projid;
  	projid = from_kprojid(targ, kprojid);
  
  	if (projid == (projid_t) -1)
  		projid = OVERFLOW_PROJID;
  	return projid;
  }
  EXPORT_SYMBOL(from_kprojid_munged);
22d917d80   Eric W. Biederman   userns: Rework th...
418
419
420
421
422
423
  static int uid_m_show(struct seq_file *seq, void *v)
  {
  	struct user_namespace *ns = seq->private;
  	struct uid_gid_extent *extent = v;
  	struct user_namespace *lower_ns;
  	uid_t lower;
5c1469de7   Eric W. Biederman   user_ns: Introduc...
424

c450f371d   Eric W. Biederman   userns: For /proc...
425
  	lower_ns = seq_user_ns(seq);
22d917d80   Eric W. Biederman   userns: Rework th...
426
427
428
429
430
431
432
433
434
435
436
437
  	if ((lower_ns == ns) && lower_ns->parent)
  		lower_ns = lower_ns->parent;
  
  	lower = from_kuid(lower_ns, KUIDT_INIT(extent->lower_first));
  
  	seq_printf(seq, "%10u %10u %10u
  ",
  		extent->first,
  		lower,
  		extent->count);
  
  	return 0;
5c1469de7   Eric W. Biederman   user_ns: Introduc...
438
  }
22d917d80   Eric W. Biederman   userns: Rework th...
439
  static int gid_m_show(struct seq_file *seq, void *v)
5c1469de7   Eric W. Biederman   user_ns: Introduc...
440
  {
22d917d80   Eric W. Biederman   userns: Rework th...
441
442
443
444
  	struct user_namespace *ns = seq->private;
  	struct uid_gid_extent *extent = v;
  	struct user_namespace *lower_ns;
  	gid_t lower;
5c1469de7   Eric W. Biederman   user_ns: Introduc...
445

c450f371d   Eric W. Biederman   userns: For /proc...
446
  	lower_ns = seq_user_ns(seq);
22d917d80   Eric W. Biederman   userns: Rework th...
447
448
  	if ((lower_ns == ns) && lower_ns->parent)
  		lower_ns = lower_ns->parent;
5c1469de7   Eric W. Biederman   user_ns: Introduc...
449

22d917d80   Eric W. Biederman   userns: Rework th...
450
451
452
453
454
455
456
457
458
459
  	lower = from_kgid(lower_ns, KGIDT_INIT(extent->lower_first));
  
  	seq_printf(seq, "%10u %10u %10u
  ",
  		extent->first,
  		lower,
  		extent->count);
  
  	return 0;
  }
f76d207a6   Eric W. Biederman   userns: Add kproj...
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
  static int projid_m_show(struct seq_file *seq, void *v)
  {
  	struct user_namespace *ns = seq->private;
  	struct uid_gid_extent *extent = v;
  	struct user_namespace *lower_ns;
  	projid_t lower;
  
  	lower_ns = seq_user_ns(seq);
  	if ((lower_ns == ns) && lower_ns->parent)
  		lower_ns = lower_ns->parent;
  
  	lower = from_kprojid(lower_ns, KPROJIDT_INIT(extent->lower_first));
  
  	seq_printf(seq, "%10u %10u %10u
  ",
  		extent->first,
  		lower,
  		extent->count);
  
  	return 0;
  }
68a9a435e   Fabian Frederick   kernel/user_names...
481
482
  static void *m_start(struct seq_file *seq, loff_t *ppos,
  		     struct uid_gid_map *map)
22d917d80   Eric W. Biederman   userns: Rework th...
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
  {
  	struct uid_gid_extent *extent = NULL;
  	loff_t pos = *ppos;
  
  	if (pos < map->nr_extents)
  		extent = &map->extent[pos];
  
  	return extent;
  }
  
  static void *uid_m_start(struct seq_file *seq, loff_t *ppos)
  {
  	struct user_namespace *ns = seq->private;
  
  	return m_start(seq, ppos, &ns->uid_map);
  }
  
  static void *gid_m_start(struct seq_file *seq, loff_t *ppos)
  {
  	struct user_namespace *ns = seq->private;
  
  	return m_start(seq, ppos, &ns->gid_map);
  }
f76d207a6   Eric W. Biederman   userns: Add kproj...
506
507
508
509
510
511
  static void *projid_m_start(struct seq_file *seq, loff_t *ppos)
  {
  	struct user_namespace *ns = seq->private;
  
  	return m_start(seq, ppos, &ns->projid_map);
  }
22d917d80   Eric W. Biederman   userns: Rework th...
512
513
514
515
516
517
518
519
520
521
  static void *m_next(struct seq_file *seq, void *v, loff_t *pos)
  {
  	(*pos)++;
  	return seq->op->start(seq, pos);
  }
  
  static void m_stop(struct seq_file *seq, void *v)
  {
  	return;
  }
ccf94f1b4   Fabian Frederick   proc: constify se...
522
  const struct seq_operations proc_uid_seq_operations = {
22d917d80   Eric W. Biederman   userns: Rework th...
523
524
525
526
527
  	.start = uid_m_start,
  	.stop = m_stop,
  	.next = m_next,
  	.show = uid_m_show,
  };
ccf94f1b4   Fabian Frederick   proc: constify se...
528
  const struct seq_operations proc_gid_seq_operations = {
22d917d80   Eric W. Biederman   userns: Rework th...
529
530
531
532
533
  	.start = gid_m_start,
  	.stop = m_stop,
  	.next = m_next,
  	.show = gid_m_show,
  };
ccf94f1b4   Fabian Frederick   proc: constify se...
534
  const struct seq_operations proc_projid_seq_operations = {
f76d207a6   Eric W. Biederman   userns: Add kproj...
535
536
537
538
539
  	.start = projid_m_start,
  	.stop = m_stop,
  	.next = m_next,
  	.show = projid_m_show,
  };
68a9a435e   Fabian Frederick   kernel/user_names...
540
541
  static bool mappings_overlap(struct uid_gid_map *new_map,
  			     struct uid_gid_extent *extent)
0bd14b4fd   Eric W. Biederman   userns: Allow any...
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
  {
  	u32 upper_first, lower_first, upper_last, lower_last;
  	unsigned idx;
  
  	upper_first = extent->first;
  	lower_first = extent->lower_first;
  	upper_last = upper_first + extent->count - 1;
  	lower_last = lower_first + extent->count - 1;
  
  	for (idx = 0; idx < new_map->nr_extents; idx++) {
  		u32 prev_upper_first, prev_lower_first;
  		u32 prev_upper_last, prev_lower_last;
  		struct uid_gid_extent *prev;
  
  		prev = &new_map->extent[idx];
  
  		prev_upper_first = prev->first;
  		prev_lower_first = prev->lower_first;
  		prev_upper_last = prev_upper_first + prev->count - 1;
  		prev_lower_last = prev_lower_first + prev->count - 1;
  
  		/* Does the upper range intersect a previous extent? */
  		if ((prev_upper_first <= upper_last) &&
  		    (prev_upper_last >= upper_first))
  			return true;
  
  		/* Does the lower range intersect a previous extent? */
  		if ((prev_lower_first <= lower_last) &&
  		    (prev_lower_last >= lower_first))
  			return true;
  	}
  	return false;
  }
22d917d80   Eric W. Biederman   userns: Rework th...
575
576
577
578
579
580
581
582
583
584
  static ssize_t map_write(struct file *file, const char __user *buf,
  			 size_t count, loff_t *ppos,
  			 int cap_setid,
  			 struct uid_gid_map *map,
  			 struct uid_gid_map *parent_map)
  {
  	struct seq_file *seq = file->private_data;
  	struct user_namespace *ns = seq->private;
  	struct uid_gid_map new_map;
  	unsigned idx;
0bd14b4fd   Eric W. Biederman   userns: Allow any...
585
  	struct uid_gid_extent *extent = NULL;
22d917d80   Eric W. Biederman   userns: Rework th...
586
587
588
589
590
  	unsigned long page = 0;
  	char *kbuf, *pos, *next_line;
  	ssize_t ret = -EINVAL;
  
  	/*
f0d62aec9   Eric W. Biederman   userns: Rename id...
591
  	 * The userns_state_mutex serializes all writes to any given map.
22d917d80   Eric W. Biederman   userns: Rework th...
592
593
594
595
596
597
598
599
600
601
602
603
604
605
  	 *
  	 * Any map is only ever written once.
  	 *
  	 * An id map fits within 1 cache line on most architectures.
  	 *
  	 * On read nothing needs to be done unless you are on an
  	 * architecture with a crazy cache coherency model like alpha.
  	 *
  	 * There is a one time data dependency between reading the
  	 * count of the extents and the values of the extents.  The
  	 * desired behavior is to see the values of the extents that
  	 * were written before the count of the extents.
  	 *
  	 * To achieve this smp_wmb() is used on guarantee the write
e79323bd8   Mikulas Patocka   user namespace: f...
606
607
  	 * order and smp_rmb() is guaranteed that we don't have crazy
  	 * architectures returning stale data.
22d917d80   Eric W. Biederman   userns: Rework th...
608
  	 */
f0d62aec9   Eric W. Biederman   userns: Rename id...
609
  	mutex_lock(&userns_state_mutex);
22d917d80   Eric W. Biederman   userns: Rework th...
610
611
612
613
614
  
  	ret = -EPERM;
  	/* Only allow one successful write to the map */
  	if (map->nr_extents != 0)
  		goto out;
41c21e351   Andy Lutomirski   userns: Changing ...
615
616
  	/*
  	 * Adjusting namespace settings requires capabilities on the target.
5c1469de7   Eric W. Biederman   user_ns: Introduc...
617
  	 */
41c21e351   Andy Lutomirski   userns: Changing ...
618
  	if (cap_valid(cap_setid) && !file_ns_capable(file, ns, CAP_SYS_ADMIN))
22d917d80   Eric W. Biederman   userns: Rework th...
619
620
621
622
623
624
625
626
  		goto out;
  
  	/* Get a buffer */
  	ret = -ENOMEM;
  	page = __get_free_page(GFP_TEMPORARY);
  	kbuf = (char *) page;
  	if (!page)
  		goto out;
36476beac   Eric W. Biederman   userns; Correct t...
627
  	/* Only allow < page size writes at the beginning of the file */
22d917d80   Eric W. Biederman   userns: Rework th...
628
629
630
631
632
633
634
635
636
637
638
639
640
641
  	ret = -EINVAL;
  	if ((*ppos != 0) || (count >= PAGE_SIZE))
  		goto out;
  
  	/* Slurp in the user data */
  	ret = -EFAULT;
  	if (copy_from_user(kbuf, buf, count))
  		goto out;
  	kbuf[count] = '\0';
  
  	/* Parse the user data */
  	ret = -EINVAL;
  	pos = kbuf;
  	new_map.nr_extents = 0;
68a9a435e   Fabian Frederick   kernel/user_names...
642
  	for (; pos; pos = next_line) {
22d917d80   Eric W. Biederman   userns: Rework th...
643
644
645
646
647
648
649
650
651
652
  		extent = &new_map.extent[new_map.nr_extents];
  
  		/* Find the end of line and ensure I don't look past it */
  		next_line = strchr(pos, '
  ');
  		if (next_line) {
  			*next_line = '\0';
  			next_line++;
  			if (*next_line == '\0')
  				next_line = NULL;
5c1469de7   Eric W. Biederman   user_ns: Introduc...
653
  		}
22d917d80   Eric W. Biederman   userns: Rework th...
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
  
  		pos = skip_spaces(pos);
  		extent->first = simple_strtoul(pos, &pos, 10);
  		if (!isspace(*pos))
  			goto out;
  
  		pos = skip_spaces(pos);
  		extent->lower_first = simple_strtoul(pos, &pos, 10);
  		if (!isspace(*pos))
  			goto out;
  
  		pos = skip_spaces(pos);
  		extent->count = simple_strtoul(pos, &pos, 10);
  		if (*pos && !isspace(*pos))
  			goto out;
  
  		/* Verify there is not trailing junk on the line */
  		pos = skip_spaces(pos);
  		if (*pos != '\0')
  			goto out;
  
  		/* Verify we have been given valid starting values */
  		if ((extent->first == (u32) -1) ||
68a9a435e   Fabian Frederick   kernel/user_names...
677
  		    (extent->lower_first == (u32) -1))
22d917d80   Eric W. Biederman   userns: Rework th...
678
  			goto out;
68a9a435e   Fabian Frederick   kernel/user_names...
679
680
681
  		/* Verify count is not zero and does not cause the
  		 * extent to wrap
  		 */
22d917d80   Eric W. Biederman   userns: Rework th...
682
683
  		if ((extent->first + extent->count) <= extent->first)
  			goto out;
68a9a435e   Fabian Frederick   kernel/user_names...
684
685
  		if ((extent->lower_first + extent->count) <=
  		     extent->lower_first)
22d917d80   Eric W. Biederman   userns: Rework th...
686
  			goto out;
0bd14b4fd   Eric W. Biederman   userns: Allow any...
687
688
  		/* Do the ranges in extent overlap any previous extents? */
  		if (mappings_overlap(&new_map, extent))
22d917d80   Eric W. Biederman   userns: Rework th...
689
690
691
  			goto out;
  
  		new_map.nr_extents++;
22d917d80   Eric W. Biederman   userns: Rework th...
692
693
694
695
696
  
  		/* Fail if the file contains too many extents */
  		if ((new_map.nr_extents == UID_GID_MAP_MAX_EXTENTS) &&
  		    (next_line != NULL))
  			goto out;
5c1469de7   Eric W. Biederman   user_ns: Introduc...
697
  	}
22d917d80   Eric W. Biederman   userns: Rework th...
698
699
700
701
702
703
  	/* Be very certaint the new map actually exists */
  	if (new_map.nr_extents == 0)
  		goto out;
  
  	ret = -EPERM;
  	/* Validate the user is allowed to use user id's mapped to. */
6708075f1   Eric W. Biederman   userns: Don't let...
704
  	if (!new_idmap_permitted(file, ns, cap_setid, &new_map))
22d917d80   Eric W. Biederman   userns: Rework th...
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
  		goto out;
  
  	/* Map the lower ids from the parent user namespace to the
  	 * kernel global id space.
  	 */
  	for (idx = 0; idx < new_map.nr_extents; idx++) {
  		u32 lower_first;
  		extent = &new_map.extent[idx];
  
  		lower_first = map_id_range_down(parent_map,
  						extent->lower_first,
  						extent->count);
  
  		/* Fail if we can not map the specified extent to
  		 * the kernel global id space.
  		 */
  		if (lower_first == (u32) -1)
  			goto out;
  
  		extent->lower_first = lower_first;
  	}
  
  	/* Install the map */
  	memcpy(map->extent, new_map.extent,
  		new_map.nr_extents*sizeof(new_map.extent[0]));
  	smp_wmb();
  	map->nr_extents = new_map.nr_extents;
  
  	*ppos = count;
  	ret = count;
  out:
f0d62aec9   Eric W. Biederman   userns: Rename id...
736
  	mutex_unlock(&userns_state_mutex);
22d917d80   Eric W. Biederman   userns: Rework th...
737
738
739
740
  	if (page)
  		free_page(page);
  	return ret;
  }
68a9a435e   Fabian Frederick   kernel/user_names...
741
742
  ssize_t proc_uid_map_write(struct file *file, const char __user *buf,
  			   size_t size, loff_t *ppos)
22d917d80   Eric W. Biederman   userns: Rework th...
743
744
745
  {
  	struct seq_file *seq = file->private_data;
  	struct user_namespace *ns = seq->private;
c450f371d   Eric W. Biederman   userns: For /proc...
746
  	struct user_namespace *seq_ns = seq_user_ns(seq);
22d917d80   Eric W. Biederman   userns: Rework th...
747
748
749
  
  	if (!ns->parent)
  		return -EPERM;
c450f371d   Eric W. Biederman   userns: For /proc...
750
751
  	if ((seq_ns != ns) && (seq_ns != ns->parent))
  		return -EPERM;
22d917d80   Eric W. Biederman   userns: Rework th...
752
753
754
  	return map_write(file, buf, size, ppos, CAP_SETUID,
  			 &ns->uid_map, &ns->parent->uid_map);
  }
68a9a435e   Fabian Frederick   kernel/user_names...
755
756
  ssize_t proc_gid_map_write(struct file *file, const char __user *buf,
  			   size_t size, loff_t *ppos)
22d917d80   Eric W. Biederman   userns: Rework th...
757
758
759
  {
  	struct seq_file *seq = file->private_data;
  	struct user_namespace *ns = seq->private;
c450f371d   Eric W. Biederman   userns: For /proc...
760
  	struct user_namespace *seq_ns = seq_user_ns(seq);
22d917d80   Eric W. Biederman   userns: Rework th...
761
762
763
  
  	if (!ns->parent)
  		return -EPERM;
c450f371d   Eric W. Biederman   userns: For /proc...
764
765
  	if ((seq_ns != ns) && (seq_ns != ns->parent))
  		return -EPERM;
22d917d80   Eric W. Biederman   userns: Rework th...
766
767
768
  	return map_write(file, buf, size, ppos, CAP_SETGID,
  			 &ns->gid_map, &ns->parent->gid_map);
  }
68a9a435e   Fabian Frederick   kernel/user_names...
769
770
  ssize_t proc_projid_map_write(struct file *file, const char __user *buf,
  			      size_t size, loff_t *ppos)
f76d207a6   Eric W. Biederman   userns: Add kproj...
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
  {
  	struct seq_file *seq = file->private_data;
  	struct user_namespace *ns = seq->private;
  	struct user_namespace *seq_ns = seq_user_ns(seq);
  
  	if (!ns->parent)
  		return -EPERM;
  
  	if ((seq_ns != ns) && (seq_ns != ns->parent))
  		return -EPERM;
  
  	/* Anyone can set any valid project id no capability needed */
  	return map_write(file, buf, size, ppos, -1,
  			 &ns->projid_map, &ns->parent->projid_map);
  }
68a9a435e   Fabian Frederick   kernel/user_names...
786
  static bool new_idmap_permitted(const struct file *file,
6708075f1   Eric W. Biederman   userns: Don't let...
787
  				struct user_namespace *ns, int cap_setid,
22d917d80   Eric W. Biederman   userns: Rework th...
788
789
  				struct uid_gid_map *new_map)
  {
f95d7918b   Eric W. Biederman   userns: Only allo...
790
  	const struct cred *cred = file->f_cred;
0542f17bf   Eric W. Biederman   userns: Document ...
791
792
793
  	/* Don't allow mappings that would allow anything that wouldn't
  	 * be allowed without the establishment of unprivileged mappings.
  	 */
f95d7918b   Eric W. Biederman   userns: Only allo...
794
795
  	if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1) &&
  	    uid_eq(ns->owner, cred->euid)) {
37657da3c   Eric W. Biederman   userns: Allow set...
796
797
798
  		u32 id = new_map->extent[0].lower_first;
  		if (cap_setid == CAP_SETUID) {
  			kuid_t uid = make_kuid(ns->parent, id);
f95d7918b   Eric W. Biederman   userns: Only allo...
799
  			if (uid_eq(uid, cred->euid))
37657da3c   Eric W. Biederman   userns: Allow set...
800
  				return true;
68a9a435e   Fabian Frederick   kernel/user_names...
801
  		} else if (cap_setid == CAP_SETGID) {
37657da3c   Eric W. Biederman   userns: Allow set...
802
  			kgid_t gid = make_kgid(ns->parent, id);
66d2f338e   Eric W. Biederman   userns: Allow set...
803
804
  			if (!(ns->flags & USERNS_SETGROUPS_ALLOWED) &&
  			    gid_eq(gid, cred->egid))
37657da3c   Eric W. Biederman   userns: Allow set...
805
806
807
  				return true;
  		}
  	}
f76d207a6   Eric W. Biederman   userns: Add kproj...
808
809
810
  	/* Allow anyone to set a mapping that doesn't require privilege */
  	if (!cap_valid(cap_setid))
  		return true;
22d917d80   Eric W. Biederman   userns: Rework th...
811
812
  	/* Allow the specified ids if we have the appropriate capability
  	 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
6708075f1   Eric W. Biederman   userns: Don't let...
813
  	 * And the opener of the id file also had the approprpiate capability.
22d917d80   Eric W. Biederman   userns: Rework th...
814
  	 */
6708075f1   Eric W. Biederman   userns: Don't let...
815
816
  	if (ns_capable(ns->parent, cap_setid) &&
  	    file_ns_capable(file, ns->parent, cap_setid))
22d917d80   Eric W. Biederman   userns: Rework th...
817
  		return true;
5c1469de7   Eric W. Biederman   user_ns: Introduc...
818

22d917d80   Eric W. Biederman   userns: Rework th...
819
  	return false;
5c1469de7   Eric W. Biederman   user_ns: Introduc...
820
  }
6164281ab   Pavel Emelyanov   user_ns: improve ...
821

9cc46516d   Eric W. Biederman   userns: Add a kno...
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
  int proc_setgroups_show(struct seq_file *seq, void *v)
  {
  	struct user_namespace *ns = seq->private;
  	unsigned long userns_flags = ACCESS_ONCE(ns->flags);
  
  	seq_printf(seq, "%s
  ",
  		   (userns_flags & USERNS_SETGROUPS_ALLOWED) ?
  		   "allow" : "deny");
  	return 0;
  }
  
  ssize_t proc_setgroups_write(struct file *file, const char __user *buf,
  			     size_t count, loff_t *ppos)
  {
  	struct seq_file *seq = file->private_data;
  	struct user_namespace *ns = seq->private;
  	char kbuf[8], *pos;
  	bool setgroups_allowed;
  	ssize_t ret;
  
  	/* Only allow a very narrow range of strings to be written */
  	ret = -EINVAL;
  	if ((*ppos != 0) || (count >= sizeof(kbuf)))
  		goto out;
  
  	/* What was written? */
  	ret = -EFAULT;
  	if (copy_from_user(kbuf, buf, count))
  		goto out;
  	kbuf[count] = '\0';
  	pos = kbuf;
  
  	/* What is being requested? */
  	ret = -EINVAL;
  	if (strncmp(pos, "allow", 5) == 0) {
  		pos += 5;
  		setgroups_allowed = true;
  	}
  	else if (strncmp(pos, "deny", 4) == 0) {
  		pos += 4;
  		setgroups_allowed = false;
  	}
  	else
  		goto out;
  
  	/* Verify there is not trailing junk on the line */
  	pos = skip_spaces(pos);
  	if (*pos != '\0')
  		goto out;
  
  	ret = -EPERM;
  	mutex_lock(&userns_state_mutex);
  	if (setgroups_allowed) {
  		/* Enabling setgroups after setgroups has been disabled
  		 * is not allowed.
  		 */
  		if (!(ns->flags & USERNS_SETGROUPS_ALLOWED))
  			goto out_unlock;
  	} else {
  		/* Permanently disabling setgroups after setgroups has
  		 * been enabled by writing the gid_map is not allowed.
  		 */
  		if (ns->gid_map.nr_extents != 0)
  			goto out_unlock;
  		ns->flags &= ~USERNS_SETGROUPS_ALLOWED;
  	}
  	mutex_unlock(&userns_state_mutex);
  
  	/* Report a successful write */
  	*ppos = count;
  	ret = count;
  out:
  	return ret;
  out_unlock:
  	mutex_unlock(&userns_state_mutex);
  	goto out;
  }
273d2c67c   Eric W. Biederman   userns: Don't all...
900
901
902
  bool userns_may_setgroups(const struct user_namespace *ns)
  {
  	bool allowed;
f0d62aec9   Eric W. Biederman   userns: Rename id...
903
  	mutex_lock(&userns_state_mutex);
273d2c67c   Eric W. Biederman   userns: Don't all...
904
905
906
907
  	/* It is not safe to use setgroups until a gid mapping in
  	 * the user namespace has been established.
  	 */
  	allowed = ns->gid_map.nr_extents != 0;
9cc46516d   Eric W. Biederman   userns: Add a kno...
908
909
  	/* Is setgroups allowed? */
  	allowed = allowed && (ns->flags & USERNS_SETGROUPS_ALLOWED);
f0d62aec9   Eric W. Biederman   userns: Rename id...
910
  	mutex_unlock(&userns_state_mutex);
273d2c67c   Eric W. Biederman   userns: Don't all...
911
912
913
  
  	return allowed;
  }
3c0411846   Al Viro   switch the rest o...
914
915
916
917
  static inline struct user_namespace *to_user_ns(struct ns_common *ns)
  {
  	return container_of(ns, struct user_namespace, ns);
  }
64964528b   Al Viro   make proc_ns_oper...
918
  static struct ns_common *userns_get(struct task_struct *task)
cde1975bc   Eric W. Biederman   userns: Implent p...
919
920
921
922
923
924
  {
  	struct user_namespace *user_ns;
  
  	rcu_read_lock();
  	user_ns = get_user_ns(__task_cred(task)->user_ns);
  	rcu_read_unlock();
3c0411846   Al Viro   switch the rest o...
925
  	return user_ns ? &user_ns->ns : NULL;
cde1975bc   Eric W. Biederman   userns: Implent p...
926
  }
64964528b   Al Viro   make proc_ns_oper...
927
  static void userns_put(struct ns_common *ns)
cde1975bc   Eric W. Biederman   userns: Implent p...
928
  {
3c0411846   Al Viro   switch the rest o...
929
  	put_user_ns(to_user_ns(ns));
cde1975bc   Eric W. Biederman   userns: Implent p...
930
  }
64964528b   Al Viro   make proc_ns_oper...
931
  static int userns_install(struct nsproxy *nsproxy, struct ns_common *ns)
cde1975bc   Eric W. Biederman   userns: Implent p...
932
  {
3c0411846   Al Viro   switch the rest o...
933
  	struct user_namespace *user_ns = to_user_ns(ns);
cde1975bc   Eric W. Biederman   userns: Implent p...
934
935
936
937
938
939
940
  	struct cred *cred;
  
  	/* Don't allow gaining capabilities by reentering
  	 * the same user namespace.
  	 */
  	if (user_ns == current_user_ns())
  		return -EINVAL;
5155040ed   Eric W. Biederman   userns: Fix typo ...
941
  	/* Threaded processes may not enter a different user namespace */
cde1975bc   Eric W. Biederman   userns: Implent p...
942
943
  	if (atomic_read(&current->mm->mm_users) > 1)
  		return -EINVAL;
e66eded83   Eric W. Biederman   userns: Don't all...
944
945
  	if (current->fs->users != 1)
  		return -EINVAL;
cde1975bc   Eric W. Biederman   userns: Implent p...
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
  	if (!ns_capable(user_ns, CAP_SYS_ADMIN))
  		return -EPERM;
  
  	cred = prepare_creds();
  	if (!cred)
  		return -ENOMEM;
  
  	put_user_ns(cred->user_ns);
  	set_cred_user_ns(cred, get_user_ns(user_ns));
  
  	return commit_creds(cred);
  }
  
  const struct proc_ns_operations userns_operations = {
  	.name		= "user",
  	.type		= CLONE_NEWUSER,
  	.get		= userns_get,
  	.put		= userns_put,
  	.install	= userns_install,
  };
6164281ab   Pavel Emelyanov   user_ns: improve ...
966
967
968
969
970
  static __init int user_namespaces_init(void)
  {
  	user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
  	return 0;
  }
c96d6660d   Paul Gortmaker   kernel: audit/fix...
971
  subsys_initcall(user_namespaces_init);