blob: 0f1e42884577c93b891a7da6aa149733ba2d5141 [file] [log] [blame]
Cedric Le Goateracce2922007-07-15 23:40:59 -07001/*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License as
4 * published by the Free Software Foundation, version 2 of the
5 * License.
6 */
7
Paul Gortmaker9984de12011-05-23 14:51:41 -04008#include <linux/export.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -07009#include <linux/nsproxy.h>
Robert P. J. Day1aeb2722008-04-29 00:59:25 -070010#include <linux/slab.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -070011#include <linux/user_namespace.h>
Eric W. Biedermancde19752012-07-26 06:24:06 -070012#include <linux/proc_fs.h>
Eric W. Biederman5c1469d2010-06-13 03:28:03 +000013#include <linux/highuid.h>
Serge Hallyn18b6e042008-10-15 16:38:45 -050014#include <linux/cred.h>
Eric W. Biederman973c5912011-11-17 01:59:07 -080015#include <linux/securebits.h>
Eric W. Biederman22d917d2011-11-17 00:11:58 -080016#include <linux/keyctl.h>
17#include <linux/key-type.h>
18#include <keys/user-type.h>
19#include <linux/seq_file.h>
20#include <linux/fs.h>
21#include <linux/uaccess.h>
22#include <linux/ctype.h>
Eric W. Biedermanf76d2072012-08-30 01:24:05 -070023#include <linux/projid.h>
Eric W. Biedermane66eded2013-03-13 11:51:49 -070024#include <linux/fs_struct.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -070025
Pavel Emelyanov61642812011-01-12 17:00:46 -080026static struct kmem_cache *user_ns_cachep __read_mostly;
27
Eric W. Biederman22d917d2011-11-17 00:11:58 -080028static bool new_idmap_permitted(struct user_namespace *ns, int cap_setid,
29 struct uid_gid_map *map);
30
Eric W. Biedermancde19752012-07-26 06:24:06 -070031static void set_cred_user_ns(struct cred *cred, struct user_namespace *user_ns)
32{
33 /* Start with the same capabilities as init but useless for doing
34 * anything as the capabilities are bound to the new user namespace.
35 */
36 cred->securebits = SECUREBITS_DEFAULT;
37 cred->cap_inheritable = CAP_EMPTY_SET;
38 cred->cap_permitted = CAP_FULL_SET;
39 cred->cap_effective = CAP_FULL_SET;
40 cred->cap_bset = CAP_FULL_SET;
41#ifdef CONFIG_KEYS
42 key_put(cred->request_key_auth);
43 cred->request_key_auth = NULL;
44#endif
45 /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
46 cred->user_ns = user_ns;
47}
48
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070049/*
Serge Hallyn18b6e042008-10-15 16:38:45 -050050 * Create a new user namespace, deriving the creator from the user in the
51 * passed credentials, and replacing that user with the new root user for the
52 * new namespace.
53 *
54 * This is called by copy_creds(), which will finish setting the target task's
55 * credentials.
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070056 */
Serge Hallyn18b6e042008-10-15 16:38:45 -050057int create_user_ns(struct cred *new)
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070058{
Eric W. Biederman0093ccb2011-11-16 21:52:53 -080059 struct user_namespace *ns, *parent_ns = new->user_ns;
Eric W. Biederman078de5f2012-02-08 07:00:08 -080060 kuid_t owner = new->euid;
61 kgid_t group = new->egid;
Eric W. Biederman98f842e2011-06-15 10:21:48 -070062 int ret;
Eric W. Biederman783291e2011-11-17 01:32:59 -080063
Eric W. Biederman31515272013-03-15 01:45:51 -070064 /*
65 * Verify that we can not violate the policy of which files
66 * may be accessed that is specified by the root directory,
67 * by verifing that the root directory is at the root of the
68 * mount namespace which allows all files to be accessed.
69 */
70 if (current_chrooted())
71 return -EPERM;
72
Eric W. Biederman783291e2011-11-17 01:32:59 -080073 /* The creator needs a mapping in the parent user namespace
74 * or else we won't be able to reasonably tell userspace who
75 * created a user_namespace.
76 */
77 if (!kuid_has_mapping(parent_ns, owner) ||
78 !kgid_has_mapping(parent_ns, group))
79 return -EPERM;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070080
Eric W. Biederman22d917d2011-11-17 00:11:58 -080081 ns = kmem_cache_zalloc(user_ns_cachep, GFP_KERNEL);
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070082 if (!ns)
Serge Hallyn18b6e042008-10-15 16:38:45 -050083 return -ENOMEM;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070084
Eric W. Biederman98f842e2011-06-15 10:21:48 -070085 ret = proc_alloc_inum(&ns->proc_inum);
86 if (ret) {
87 kmem_cache_free(user_ns_cachep, ns);
88 return ret;
89 }
90
Eric W. Biedermanc61a2812012-12-28 18:58:39 -080091 atomic_set(&ns->count, 1);
Eric W. Biedermancde19752012-07-26 06:24:06 -070092 /* Leave the new->user_ns reference with the new user namespace. */
Eric W. Biedermanaeb3ae92011-11-16 21:59:43 -080093 ns->parent = parent_ns;
Eric W. Biederman783291e2011-11-17 01:32:59 -080094 ns->owner = owner;
95 ns->group = group;
Eric W. Biederman22d917d2011-11-17 00:11:58 -080096
Eric W. Biedermancde19752012-07-26 06:24:06 -070097 set_cred_user_ns(new, ns);
Eric W. Biederman0093ccb2011-11-16 21:52:53 -080098
Serge Hallyn18b6e042008-10-15 16:38:45 -050099 return 0;
Cedric Le Goateracce2922007-07-15 23:40:59 -0700100}
101
Eric W. Biedermanb2e0d9872012-07-26 05:15:35 -0700102int unshare_userns(unsigned long unshare_flags, struct cred **new_cred)
103{
104 struct cred *cred;
105
106 if (!(unshare_flags & CLONE_NEWUSER))
107 return 0;
108
109 cred = prepare_creds();
110 if (!cred)
111 return -ENOMEM;
112
113 *new_cred = cred;
114 return create_user_ns(cred);
115}
116
Eric W. Biedermanc61a2812012-12-28 18:58:39 -0800117void free_user_ns(struct user_namespace *ns)
David Howells51708362009-02-27 14:03:03 -0800118{
Eric W. Biedermanc61a2812012-12-28 18:58:39 -0800119 struct user_namespace *parent;
David Howells51708362009-02-27 14:03:03 -0800120
Eric W. Biedermanc61a2812012-12-28 18:58:39 -0800121 do {
122 parent = ns->parent;
123 proc_free_inum(ns->proc_inum);
124 kmem_cache_free(user_ns_cachep, ns);
125 ns = parent;
126 } while (atomic_dec_and_test(&parent->count));
David Howells51708362009-02-27 14:03:03 -0800127}
Michael Halcrow6a3fd922008-04-29 00:59:52 -0700128EXPORT_SYMBOL(free_user_ns);
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000129
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800130static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count)
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000131{
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800132 unsigned idx, extents;
133 u32 first, last, id2;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000134
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800135 id2 = id + count - 1;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000136
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800137 /* Find the matching extent */
138 extents = map->nr_extents;
139 smp_read_barrier_depends();
140 for (idx = 0; idx < extents; idx++) {
141 first = map->extent[idx].first;
142 last = first + map->extent[idx].count - 1;
143 if (id >= first && id <= last &&
144 (id2 >= first && id2 <= last))
145 break;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000146 }
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800147 /* Map the id or note failure */
148 if (idx < extents)
149 id = (id - first) + map->extent[idx].lower_first;
150 else
151 id = (u32) -1;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000152
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800153 return id;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000154}
155
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800156static u32 map_id_down(struct uid_gid_map *map, u32 id)
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000157{
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800158 unsigned idx, extents;
159 u32 first, last;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000160
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800161 /* Find the matching extent */
162 extents = map->nr_extents;
163 smp_read_barrier_depends();
164 for (idx = 0; idx < extents; idx++) {
165 first = map->extent[idx].first;
166 last = first + map->extent[idx].count - 1;
167 if (id >= first && id <= last)
168 break;
169 }
170 /* Map the id or note failure */
171 if (idx < extents)
172 id = (id - first) + map->extent[idx].lower_first;
173 else
174 id = (u32) -1;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000175
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800176 return id;
177}
178
179static u32 map_id_up(struct uid_gid_map *map, u32 id)
180{
181 unsigned idx, extents;
182 u32 first, last;
183
184 /* Find the matching extent */
185 extents = map->nr_extents;
186 smp_read_barrier_depends();
187 for (idx = 0; idx < extents; idx++) {
188 first = map->extent[idx].lower_first;
189 last = first + map->extent[idx].count - 1;
190 if (id >= first && id <= last)
191 break;
192 }
193 /* Map the id or note failure */
194 if (idx < extents)
195 id = (id - first) + map->extent[idx].first;
196 else
197 id = (u32) -1;
198
199 return id;
200}
201
202/**
203 * make_kuid - Map a user-namespace uid pair into a kuid.
204 * @ns: User namespace that the uid is in
205 * @uid: User identifier
206 *
207 * Maps a user-namespace uid pair into a kernel internal kuid,
208 * and returns that kuid.
209 *
210 * When there is no mapping defined for the user-namespace uid
211 * pair INVALID_UID is returned. Callers are expected to test
212 * for and handle handle INVALID_UID being returned. INVALID_UID
213 * may be tested for using uid_valid().
214 */
215kuid_t make_kuid(struct user_namespace *ns, uid_t uid)
216{
217 /* Map the uid to a global kernel uid */
218 return KUIDT_INIT(map_id_down(&ns->uid_map, uid));
219}
220EXPORT_SYMBOL(make_kuid);
221
222/**
223 * from_kuid - Create a uid from a kuid user-namespace pair.
224 * @targ: The user namespace we want a uid in.
225 * @kuid: The kernel internal uid to start with.
226 *
227 * Map @kuid into the user-namespace specified by @targ and
228 * return the resulting uid.
229 *
230 * There is always a mapping into the initial user_namespace.
231 *
232 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
233 */
234uid_t from_kuid(struct user_namespace *targ, kuid_t kuid)
235{
236 /* Map the uid from a global kernel uid */
237 return map_id_up(&targ->uid_map, __kuid_val(kuid));
238}
239EXPORT_SYMBOL(from_kuid);
240
241/**
242 * from_kuid_munged - Create a uid from a kuid user-namespace pair.
243 * @targ: The user namespace we want a uid in.
244 * @kuid: The kernel internal uid to start with.
245 *
246 * Map @kuid into the user-namespace specified by @targ and
247 * return the resulting uid.
248 *
249 * There is always a mapping into the initial user_namespace.
250 *
251 * Unlike from_kuid from_kuid_munged never fails and always
252 * returns a valid uid. This makes from_kuid_munged appropriate
253 * for use in syscalls like stat and getuid where failing the
254 * system call and failing to provide a valid uid are not an
255 * options.
256 *
257 * If @kuid has no mapping in @targ overflowuid is returned.
258 */
259uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid)
260{
261 uid_t uid;
262 uid = from_kuid(targ, kuid);
263
264 if (uid == (uid_t) -1)
265 uid = overflowuid;
266 return uid;
267}
268EXPORT_SYMBOL(from_kuid_munged);
269
270/**
271 * make_kgid - Map a user-namespace gid pair into a kgid.
272 * @ns: User namespace that the gid is in
273 * @uid: group identifier
274 *
275 * Maps a user-namespace gid pair into a kernel internal kgid,
276 * and returns that kgid.
277 *
278 * When there is no mapping defined for the user-namespace gid
279 * pair INVALID_GID is returned. Callers are expected to test
280 * for and handle INVALID_GID being returned. INVALID_GID may be
281 * tested for using gid_valid().
282 */
283kgid_t make_kgid(struct user_namespace *ns, gid_t gid)
284{
285 /* Map the gid to a global kernel gid */
286 return KGIDT_INIT(map_id_down(&ns->gid_map, gid));
287}
288EXPORT_SYMBOL(make_kgid);
289
290/**
291 * from_kgid - Create a gid from a kgid user-namespace pair.
292 * @targ: The user namespace we want a gid in.
293 * @kgid: The kernel internal gid to start with.
294 *
295 * Map @kgid into the user-namespace specified by @targ and
296 * return the resulting gid.
297 *
298 * There is always a mapping into the initial user_namespace.
299 *
300 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
301 */
302gid_t from_kgid(struct user_namespace *targ, kgid_t kgid)
303{
304 /* Map the gid from a global kernel gid */
305 return map_id_up(&targ->gid_map, __kgid_val(kgid));
306}
307EXPORT_SYMBOL(from_kgid);
308
309/**
310 * from_kgid_munged - Create a gid from a kgid user-namespace pair.
311 * @targ: The user namespace we want a gid in.
312 * @kgid: The kernel internal gid to start with.
313 *
314 * Map @kgid into the user-namespace specified by @targ and
315 * return the resulting gid.
316 *
317 * There is always a mapping into the initial user_namespace.
318 *
319 * Unlike from_kgid from_kgid_munged never fails and always
320 * returns a valid gid. This makes from_kgid_munged appropriate
321 * for use in syscalls like stat and getgid where failing the
322 * system call and failing to provide a valid gid are not options.
323 *
324 * If @kgid has no mapping in @targ overflowgid is returned.
325 */
326gid_t from_kgid_munged(struct user_namespace *targ, kgid_t kgid)
327{
328 gid_t gid;
329 gid = from_kgid(targ, kgid);
330
331 if (gid == (gid_t) -1)
332 gid = overflowgid;
333 return gid;
334}
335EXPORT_SYMBOL(from_kgid_munged);
336
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700337/**
338 * make_kprojid - Map a user-namespace projid pair into a kprojid.
339 * @ns: User namespace that the projid is in
340 * @projid: Project identifier
341 *
342 * Maps a user-namespace uid pair into a kernel internal kuid,
343 * and returns that kuid.
344 *
345 * When there is no mapping defined for the user-namespace projid
346 * pair INVALID_PROJID is returned. Callers are expected to test
347 * for and handle handle INVALID_PROJID being returned. INVALID_PROJID
348 * may be tested for using projid_valid().
349 */
350kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid)
351{
352 /* Map the uid to a global kernel uid */
353 return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid));
354}
355EXPORT_SYMBOL(make_kprojid);
356
357/**
358 * from_kprojid - Create a projid from a kprojid user-namespace pair.
359 * @targ: The user namespace we want a projid in.
360 * @kprojid: The kernel internal project identifier to start with.
361 *
362 * Map @kprojid into the user-namespace specified by @targ and
363 * return the resulting projid.
364 *
365 * There is always a mapping into the initial user_namespace.
366 *
367 * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
368 */
369projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid)
370{
371 /* Map the uid from a global kernel uid */
372 return map_id_up(&targ->projid_map, __kprojid_val(kprojid));
373}
374EXPORT_SYMBOL(from_kprojid);
375
376/**
377 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
378 * @targ: The user namespace we want a projid in.
379 * @kprojid: The kernel internal projid to start with.
380 *
381 * Map @kprojid into the user-namespace specified by @targ and
382 * return the resulting projid.
383 *
384 * There is always a mapping into the initial user_namespace.
385 *
386 * Unlike from_kprojid from_kprojid_munged never fails and always
387 * returns a valid projid. This makes from_kprojid_munged
388 * appropriate for use in syscalls like stat and where
389 * failing the system call and failing to provide a valid projid are
390 * not an options.
391 *
392 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
393 */
394projid_t from_kprojid_munged(struct user_namespace *targ, kprojid_t kprojid)
395{
396 projid_t projid;
397 projid = from_kprojid(targ, kprojid);
398
399 if (projid == (projid_t) -1)
400 projid = OVERFLOW_PROJID;
401 return projid;
402}
403EXPORT_SYMBOL(from_kprojid_munged);
404
405
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800406static int uid_m_show(struct seq_file *seq, void *v)
407{
408 struct user_namespace *ns = seq->private;
409 struct uid_gid_extent *extent = v;
410 struct user_namespace *lower_ns;
411 uid_t lower;
412
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700413 lower_ns = seq_user_ns(seq);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800414 if ((lower_ns == ns) && lower_ns->parent)
415 lower_ns = lower_ns->parent;
416
417 lower = from_kuid(lower_ns, KUIDT_INIT(extent->lower_first));
418
419 seq_printf(seq, "%10u %10u %10u\n",
420 extent->first,
421 lower,
422 extent->count);
423
424 return 0;
425}
426
427static int gid_m_show(struct seq_file *seq, void *v)
428{
429 struct user_namespace *ns = seq->private;
430 struct uid_gid_extent *extent = v;
431 struct user_namespace *lower_ns;
432 gid_t lower;
433
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700434 lower_ns = seq_user_ns(seq);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800435 if ((lower_ns == ns) && lower_ns->parent)
436 lower_ns = lower_ns->parent;
437
438 lower = from_kgid(lower_ns, KGIDT_INIT(extent->lower_first));
439
440 seq_printf(seq, "%10u %10u %10u\n",
441 extent->first,
442 lower,
443 extent->count);
444
445 return 0;
446}
447
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700448static int projid_m_show(struct seq_file *seq, void *v)
449{
450 struct user_namespace *ns = seq->private;
451 struct uid_gid_extent *extent = v;
452 struct user_namespace *lower_ns;
453 projid_t lower;
454
455 lower_ns = seq_user_ns(seq);
456 if ((lower_ns == ns) && lower_ns->parent)
457 lower_ns = lower_ns->parent;
458
459 lower = from_kprojid(lower_ns, KPROJIDT_INIT(extent->lower_first));
460
461 seq_printf(seq, "%10u %10u %10u\n",
462 extent->first,
463 lower,
464 extent->count);
465
466 return 0;
467}
468
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800469static void *m_start(struct seq_file *seq, loff_t *ppos, struct uid_gid_map *map)
470{
471 struct uid_gid_extent *extent = NULL;
472 loff_t pos = *ppos;
473
474 if (pos < map->nr_extents)
475 extent = &map->extent[pos];
476
477 return extent;
478}
479
480static void *uid_m_start(struct seq_file *seq, loff_t *ppos)
481{
482 struct user_namespace *ns = seq->private;
483
484 return m_start(seq, ppos, &ns->uid_map);
485}
486
487static void *gid_m_start(struct seq_file *seq, loff_t *ppos)
488{
489 struct user_namespace *ns = seq->private;
490
491 return m_start(seq, ppos, &ns->gid_map);
492}
493
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700494static void *projid_m_start(struct seq_file *seq, loff_t *ppos)
495{
496 struct user_namespace *ns = seq->private;
497
498 return m_start(seq, ppos, &ns->projid_map);
499}
500
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800501static void *m_next(struct seq_file *seq, void *v, loff_t *pos)
502{
503 (*pos)++;
504 return seq->op->start(seq, pos);
505}
506
507static void m_stop(struct seq_file *seq, void *v)
508{
509 return;
510}
511
512struct seq_operations proc_uid_seq_operations = {
513 .start = uid_m_start,
514 .stop = m_stop,
515 .next = m_next,
516 .show = uid_m_show,
517};
518
519struct seq_operations proc_gid_seq_operations = {
520 .start = gid_m_start,
521 .stop = m_stop,
522 .next = m_next,
523 .show = gid_m_show,
524};
525
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700526struct seq_operations proc_projid_seq_operations = {
527 .start = projid_m_start,
528 .stop = m_stop,
529 .next = m_next,
530 .show = projid_m_show,
531};
532
Eric W. Biederman0bd14b42012-12-27 22:27:29 -0800533static bool mappings_overlap(struct uid_gid_map *new_map, struct uid_gid_extent *extent)
534{
535 u32 upper_first, lower_first, upper_last, lower_last;
536 unsigned idx;
537
538 upper_first = extent->first;
539 lower_first = extent->lower_first;
540 upper_last = upper_first + extent->count - 1;
541 lower_last = lower_first + extent->count - 1;
542
543 for (idx = 0; idx < new_map->nr_extents; idx++) {
544 u32 prev_upper_first, prev_lower_first;
545 u32 prev_upper_last, prev_lower_last;
546 struct uid_gid_extent *prev;
547
548 prev = &new_map->extent[idx];
549
550 prev_upper_first = prev->first;
551 prev_lower_first = prev->lower_first;
552 prev_upper_last = prev_upper_first + prev->count - 1;
553 prev_lower_last = prev_lower_first + prev->count - 1;
554
555 /* Does the upper range intersect a previous extent? */
556 if ((prev_upper_first <= upper_last) &&
557 (prev_upper_last >= upper_first))
558 return true;
559
560 /* Does the lower range intersect a previous extent? */
561 if ((prev_lower_first <= lower_last) &&
562 (prev_lower_last >= lower_first))
563 return true;
564 }
565 return false;
566}
567
568
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800569static DEFINE_MUTEX(id_map_mutex);
570
571static ssize_t map_write(struct file *file, const char __user *buf,
572 size_t count, loff_t *ppos,
573 int cap_setid,
574 struct uid_gid_map *map,
575 struct uid_gid_map *parent_map)
576{
577 struct seq_file *seq = file->private_data;
578 struct user_namespace *ns = seq->private;
579 struct uid_gid_map new_map;
580 unsigned idx;
Eric W. Biederman0bd14b42012-12-27 22:27:29 -0800581 struct uid_gid_extent *extent = NULL;
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800582 unsigned long page = 0;
583 char *kbuf, *pos, *next_line;
584 ssize_t ret = -EINVAL;
585
586 /*
587 * The id_map_mutex serializes all writes to any given map.
588 *
589 * Any map is only ever written once.
590 *
591 * An id map fits within 1 cache line on most architectures.
592 *
593 * On read nothing needs to be done unless you are on an
594 * architecture with a crazy cache coherency model like alpha.
595 *
596 * There is a one time data dependency between reading the
597 * count of the extents and the values of the extents. The
598 * desired behavior is to see the values of the extents that
599 * were written before the count of the extents.
600 *
601 * To achieve this smp_wmb() is used on guarantee the write
602 * order and smp_read_barrier_depends() is guaranteed that we
603 * don't have crazy architectures returning stale data.
604 *
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000605 */
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800606 mutex_lock(&id_map_mutex);
607
608 ret = -EPERM;
609 /* Only allow one successful write to the map */
610 if (map->nr_extents != 0)
611 goto out;
612
613 /* Require the appropriate privilege CAP_SETUID or CAP_SETGID
614 * over the user namespace in order to set the id mapping.
615 */
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700616 if (cap_valid(cap_setid) && !ns_capable(ns, cap_setid))
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800617 goto out;
618
619 /* Get a buffer */
620 ret = -ENOMEM;
621 page = __get_free_page(GFP_TEMPORARY);
622 kbuf = (char *) page;
623 if (!page)
624 goto out;
625
626 /* Only allow <= page size writes at the beginning of the file */
627 ret = -EINVAL;
628 if ((*ppos != 0) || (count >= PAGE_SIZE))
629 goto out;
630
631 /* Slurp in the user data */
632 ret = -EFAULT;
633 if (copy_from_user(kbuf, buf, count))
634 goto out;
635 kbuf[count] = '\0';
636
637 /* Parse the user data */
638 ret = -EINVAL;
639 pos = kbuf;
640 new_map.nr_extents = 0;
641 for (;pos; pos = next_line) {
642 extent = &new_map.extent[new_map.nr_extents];
643
644 /* Find the end of line and ensure I don't look past it */
645 next_line = strchr(pos, '\n');
646 if (next_line) {
647 *next_line = '\0';
648 next_line++;
649 if (*next_line == '\0')
650 next_line = NULL;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000651 }
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800652
653 pos = skip_spaces(pos);
654 extent->first = simple_strtoul(pos, &pos, 10);
655 if (!isspace(*pos))
656 goto out;
657
658 pos = skip_spaces(pos);
659 extent->lower_first = simple_strtoul(pos, &pos, 10);
660 if (!isspace(*pos))
661 goto out;
662
663 pos = skip_spaces(pos);
664 extent->count = simple_strtoul(pos, &pos, 10);
665 if (*pos && !isspace(*pos))
666 goto out;
667
668 /* Verify there is not trailing junk on the line */
669 pos = skip_spaces(pos);
670 if (*pos != '\0')
671 goto out;
672
673 /* Verify we have been given valid starting values */
674 if ((extent->first == (u32) -1) ||
675 (extent->lower_first == (u32) -1 ))
676 goto out;
677
678 /* Verify count is not zero and does not cause the extent to wrap */
679 if ((extent->first + extent->count) <= extent->first)
680 goto out;
681 if ((extent->lower_first + extent->count) <= extent->lower_first)
682 goto out;
683
Eric W. Biederman0bd14b42012-12-27 22:27:29 -0800684 /* Do the ranges in extent overlap any previous extents? */
685 if (mappings_overlap(&new_map, extent))
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800686 goto out;
687
688 new_map.nr_extents++;
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800689
690 /* Fail if the file contains too many extents */
691 if ((new_map.nr_extents == UID_GID_MAP_MAX_EXTENTS) &&
692 (next_line != NULL))
693 goto out;
694 }
695 /* Be very certaint the new map actually exists */
696 if (new_map.nr_extents == 0)
697 goto out;
698
699 ret = -EPERM;
700 /* Validate the user is allowed to use user id's mapped to. */
701 if (!new_idmap_permitted(ns, cap_setid, &new_map))
702 goto out;
703
704 /* Map the lower ids from the parent user namespace to the
705 * kernel global id space.
706 */
707 for (idx = 0; idx < new_map.nr_extents; idx++) {
708 u32 lower_first;
709 extent = &new_map.extent[idx];
710
711 lower_first = map_id_range_down(parent_map,
712 extent->lower_first,
713 extent->count);
714
715 /* Fail if we can not map the specified extent to
716 * the kernel global id space.
717 */
718 if (lower_first == (u32) -1)
719 goto out;
720
721 extent->lower_first = lower_first;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000722 }
723
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800724 /* Install the map */
725 memcpy(map->extent, new_map.extent,
726 new_map.nr_extents*sizeof(new_map.extent[0]));
727 smp_wmb();
728 map->nr_extents = new_map.nr_extents;
729
730 *ppos = count;
731 ret = count;
732out:
733 mutex_unlock(&id_map_mutex);
734 if (page)
735 free_page(page);
736 return ret;
737}
738
739ssize_t proc_uid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
740{
741 struct seq_file *seq = file->private_data;
742 struct user_namespace *ns = seq->private;
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700743 struct user_namespace *seq_ns = seq_user_ns(seq);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800744
745 if (!ns->parent)
746 return -EPERM;
747
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700748 if ((seq_ns != ns) && (seq_ns != ns->parent))
749 return -EPERM;
750
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800751 return map_write(file, buf, size, ppos, CAP_SETUID,
752 &ns->uid_map, &ns->parent->uid_map);
753}
754
755ssize_t proc_gid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
756{
757 struct seq_file *seq = file->private_data;
758 struct user_namespace *ns = seq->private;
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700759 struct user_namespace *seq_ns = seq_user_ns(seq);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800760
761 if (!ns->parent)
762 return -EPERM;
763
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700764 if ((seq_ns != ns) && (seq_ns != ns->parent))
765 return -EPERM;
766
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800767 return map_write(file, buf, size, ppos, CAP_SETGID,
768 &ns->gid_map, &ns->parent->gid_map);
769}
770
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700771ssize_t proc_projid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
772{
773 struct seq_file *seq = file->private_data;
774 struct user_namespace *ns = seq->private;
775 struct user_namespace *seq_ns = seq_user_ns(seq);
776
777 if (!ns->parent)
778 return -EPERM;
779
780 if ((seq_ns != ns) && (seq_ns != ns->parent))
781 return -EPERM;
782
783 /* Anyone can set any valid project id no capability needed */
784 return map_write(file, buf, size, ppos, -1,
785 &ns->projid_map, &ns->parent->projid_map);
786}
787
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800788static bool new_idmap_permitted(struct user_namespace *ns, int cap_setid,
789 struct uid_gid_map *new_map)
790{
Eric W. Biederman37657da2012-07-27 06:21:27 -0700791 /* Allow mapping to your own filesystem ids */
792 if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1)) {
793 u32 id = new_map->extent[0].lower_first;
794 if (cap_setid == CAP_SETUID) {
795 kuid_t uid = make_kuid(ns->parent, id);
796 if (uid_eq(uid, current_fsuid()))
797 return true;
798 }
799 else if (cap_setid == CAP_SETGID) {
800 kgid_t gid = make_kgid(ns->parent, id);
801 if (gid_eq(gid, current_fsgid()))
802 return true;
803 }
804 }
805
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700806 /* Allow anyone to set a mapping that doesn't require privilege */
807 if (!cap_valid(cap_setid))
808 return true;
809
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800810 /* Allow the specified ids if we have the appropriate capability
811 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
812 */
813 if (ns_capable(ns->parent, cap_setid))
814 return true;
815
816 return false;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000817}
Pavel Emelyanov61642812011-01-12 17:00:46 -0800818
Eric W. Biedermancde19752012-07-26 06:24:06 -0700819static void *userns_get(struct task_struct *task)
820{
821 struct user_namespace *user_ns;
822
823 rcu_read_lock();
824 user_ns = get_user_ns(__task_cred(task)->user_ns);
825 rcu_read_unlock();
826
827 return user_ns;
828}
829
830static void userns_put(void *ns)
831{
832 put_user_ns(ns);
833}
834
835static int userns_install(struct nsproxy *nsproxy, void *ns)
836{
837 struct user_namespace *user_ns = ns;
838 struct cred *cred;
839
840 /* Don't allow gaining capabilities by reentering
841 * the same user namespace.
842 */
843 if (user_ns == current_user_ns())
844 return -EINVAL;
845
Eric W. Biederman51550402012-12-09 17:19:52 -0800846 /* Threaded processes may not enter a different user namespace */
Eric W. Biedermancde19752012-07-26 06:24:06 -0700847 if (atomic_read(&current->mm->mm_users) > 1)
848 return -EINVAL;
849
Eric W. Biedermane66eded2013-03-13 11:51:49 -0700850 if (current->fs->users != 1)
851 return -EINVAL;
852
Eric W. Biedermancde19752012-07-26 06:24:06 -0700853 if (!ns_capable(user_ns, CAP_SYS_ADMIN))
854 return -EPERM;
855
856 cred = prepare_creds();
857 if (!cred)
858 return -ENOMEM;
859
860 put_user_ns(cred->user_ns);
861 set_cred_user_ns(cred, get_user_ns(user_ns));
862
863 return commit_creds(cred);
864}
865
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700866static unsigned int userns_inum(void *ns)
867{
868 struct user_namespace *user_ns = ns;
869 return user_ns->proc_inum;
870}
871
Eric W. Biedermancde19752012-07-26 06:24:06 -0700872const struct proc_ns_operations userns_operations = {
873 .name = "user",
874 .type = CLONE_NEWUSER,
875 .get = userns_get,
876 .put = userns_put,
877 .install = userns_install,
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700878 .inum = userns_inum,
Eric W. Biedermancde19752012-07-26 06:24:06 -0700879};
880
Pavel Emelyanov61642812011-01-12 17:00:46 -0800881static __init int user_namespaces_init(void)
882{
883 user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
884 return 0;
885}
886module_init(user_namespaces_init);