blob: ce92f7e6290a9016fad84d014a81e6907eaa5ec5 [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>
Cedric Le Goateracce2922007-07-15 23:40:59 -070024
Pavel Emelyanov61642812011-01-12 17:00:46 -080025static struct kmem_cache *user_ns_cachep __read_mostly;
26
Eric W. Biederman22d917d2011-11-17 00:11:58 -080027static bool new_idmap_permitted(struct user_namespace *ns, int cap_setid,
28 struct uid_gid_map *map);
29
Eric W. Biedermancde19752012-07-26 06:24:06 -070030static void set_cred_user_ns(struct cred *cred, struct user_namespace *user_ns)
31{
32 /* Start with the same capabilities as init but useless for doing
33 * anything as the capabilities are bound to the new user namespace.
34 */
35 cred->securebits = SECUREBITS_DEFAULT;
36 cred->cap_inheritable = CAP_EMPTY_SET;
37 cred->cap_permitted = CAP_FULL_SET;
38 cred->cap_effective = CAP_FULL_SET;
39 cred->cap_bset = CAP_FULL_SET;
40#ifdef CONFIG_KEYS
41 key_put(cred->request_key_auth);
42 cred->request_key_auth = NULL;
43#endif
44 /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
45 cred->user_ns = user_ns;
46}
47
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070048/*
Serge Hallyn18b6e042008-10-15 16:38:45 -050049 * Create a new user namespace, deriving the creator from the user in the
50 * passed credentials, and replacing that user with the new root user for the
51 * new namespace.
52 *
53 * This is called by copy_creds(), which will finish setting the target task's
54 * credentials.
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070055 */
Serge Hallyn18b6e042008-10-15 16:38:45 -050056int create_user_ns(struct cred *new)
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070057{
Eric W. Biederman0093ccb2011-11-16 21:52:53 -080058 struct user_namespace *ns, *parent_ns = new->user_ns;
Eric W. Biederman078de5f2012-02-08 07:00:08 -080059 kuid_t owner = new->euid;
60 kgid_t group = new->egid;
Eric W. Biederman783291e2011-11-17 01:32:59 -080061
62 /* The creator needs a mapping in the parent user namespace
63 * or else we won't be able to reasonably tell userspace who
64 * created a user_namespace.
65 */
66 if (!kuid_has_mapping(parent_ns, owner) ||
67 !kgid_has_mapping(parent_ns, group))
68 return -EPERM;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070069
Eric W. Biederman22d917d2011-11-17 00:11:58 -080070 ns = kmem_cache_zalloc(user_ns_cachep, GFP_KERNEL);
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070071 if (!ns)
Serge Hallyn18b6e042008-10-15 16:38:45 -050072 return -ENOMEM;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070073
74 kref_init(&ns->kref);
Eric W. Biedermancde19752012-07-26 06:24:06 -070075 /* Leave the new->user_ns reference with the new user namespace. */
Eric W. Biedermanaeb3ae92011-11-16 21:59:43 -080076 ns->parent = parent_ns;
Eric W. Biederman783291e2011-11-17 01:32:59 -080077 ns->owner = owner;
78 ns->group = group;
Eric W. Biederman22d917d2011-11-17 00:11:58 -080079
Eric W. Biedermancde19752012-07-26 06:24:06 -070080 set_cred_user_ns(new, ns);
Eric W. Biederman0093ccb2011-11-16 21:52:53 -080081
Serge Hallyn18b6e042008-10-15 16:38:45 -050082 return 0;
Cedric Le Goateracce2922007-07-15 23:40:59 -070083}
84
Eric W. Biedermanb2e0d9872012-07-26 05:15:35 -070085int unshare_userns(unsigned long unshare_flags, struct cred **new_cred)
86{
87 struct cred *cred;
88
89 if (!(unshare_flags & CLONE_NEWUSER))
90 return 0;
91
92 cred = prepare_creds();
93 if (!cred)
94 return -ENOMEM;
95
96 *new_cred = cred;
97 return create_user_ns(cred);
98}
99
David Howells51708362009-02-27 14:03:03 -0800100void free_user_ns(struct kref *kref)
101{
Eric W. Biederman783291e2011-11-17 01:32:59 -0800102 struct user_namespace *parent, *ns =
David Howells51708362009-02-27 14:03:03 -0800103 container_of(kref, struct user_namespace, kref);
104
Eric W. Biederman783291e2011-11-17 01:32:59 -0800105 parent = ns->parent;
106 kmem_cache_free(user_ns_cachep, ns);
107 put_user_ns(parent);
David Howells51708362009-02-27 14:03:03 -0800108}
Michael Halcrow6a3fd922008-04-29 00:59:52 -0700109EXPORT_SYMBOL(free_user_ns);
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000110
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800111static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count)
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000112{
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800113 unsigned idx, extents;
114 u32 first, last, id2;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000115
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800116 id2 = id + count - 1;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000117
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800118 /* Find the matching extent */
119 extents = map->nr_extents;
120 smp_read_barrier_depends();
121 for (idx = 0; idx < extents; idx++) {
122 first = map->extent[idx].first;
123 last = first + map->extent[idx].count - 1;
124 if (id >= first && id <= last &&
125 (id2 >= first && id2 <= last))
126 break;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000127 }
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800128 /* Map the id or note failure */
129 if (idx < extents)
130 id = (id - first) + map->extent[idx].lower_first;
131 else
132 id = (u32) -1;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000133
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800134 return id;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000135}
136
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800137static u32 map_id_down(struct uid_gid_map *map, u32 id)
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000138{
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800139 unsigned idx, extents;
140 u32 first, last;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000141
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800142 /* Find the matching extent */
143 extents = map->nr_extents;
144 smp_read_barrier_depends();
145 for (idx = 0; idx < extents; idx++) {
146 first = map->extent[idx].first;
147 last = first + map->extent[idx].count - 1;
148 if (id >= first && id <= last)
149 break;
150 }
151 /* Map the id or note failure */
152 if (idx < extents)
153 id = (id - first) + map->extent[idx].lower_first;
154 else
155 id = (u32) -1;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000156
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800157 return id;
158}
159
160static u32 map_id_up(struct uid_gid_map *map, u32 id)
161{
162 unsigned idx, extents;
163 u32 first, last;
164
165 /* Find the matching extent */
166 extents = map->nr_extents;
167 smp_read_barrier_depends();
168 for (idx = 0; idx < extents; idx++) {
169 first = map->extent[idx].lower_first;
170 last = first + map->extent[idx].count - 1;
171 if (id >= first && id <= last)
172 break;
173 }
174 /* Map the id or note failure */
175 if (idx < extents)
176 id = (id - first) + map->extent[idx].first;
177 else
178 id = (u32) -1;
179
180 return id;
181}
182
183/**
184 * make_kuid - Map a user-namespace uid pair into a kuid.
185 * @ns: User namespace that the uid is in
186 * @uid: User identifier
187 *
188 * Maps a user-namespace uid pair into a kernel internal kuid,
189 * and returns that kuid.
190 *
191 * When there is no mapping defined for the user-namespace uid
192 * pair INVALID_UID is returned. Callers are expected to test
193 * for and handle handle INVALID_UID being returned. INVALID_UID
194 * may be tested for using uid_valid().
195 */
196kuid_t make_kuid(struct user_namespace *ns, uid_t uid)
197{
198 /* Map the uid to a global kernel uid */
199 return KUIDT_INIT(map_id_down(&ns->uid_map, uid));
200}
201EXPORT_SYMBOL(make_kuid);
202
203/**
204 * from_kuid - Create a uid from a kuid user-namespace pair.
205 * @targ: The user namespace we want a uid in.
206 * @kuid: The kernel internal uid to start with.
207 *
208 * Map @kuid into the user-namespace specified by @targ and
209 * return the resulting uid.
210 *
211 * There is always a mapping into the initial user_namespace.
212 *
213 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
214 */
215uid_t from_kuid(struct user_namespace *targ, kuid_t kuid)
216{
217 /* Map the uid from a global kernel uid */
218 return map_id_up(&targ->uid_map, __kuid_val(kuid));
219}
220EXPORT_SYMBOL(from_kuid);
221
222/**
223 * from_kuid_munged - 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 * Unlike from_kuid from_kuid_munged never fails and always
233 * returns a valid uid. This makes from_kuid_munged appropriate
234 * for use in syscalls like stat and getuid where failing the
235 * system call and failing to provide a valid uid are not an
236 * options.
237 *
238 * If @kuid has no mapping in @targ overflowuid is returned.
239 */
240uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid)
241{
242 uid_t uid;
243 uid = from_kuid(targ, kuid);
244
245 if (uid == (uid_t) -1)
246 uid = overflowuid;
247 return uid;
248}
249EXPORT_SYMBOL(from_kuid_munged);
250
251/**
252 * make_kgid - Map a user-namespace gid pair into a kgid.
253 * @ns: User namespace that the gid is in
254 * @uid: group identifier
255 *
256 * Maps a user-namespace gid pair into a kernel internal kgid,
257 * and returns that kgid.
258 *
259 * When there is no mapping defined for the user-namespace gid
260 * pair INVALID_GID is returned. Callers are expected to test
261 * for and handle INVALID_GID being returned. INVALID_GID may be
262 * tested for using gid_valid().
263 */
264kgid_t make_kgid(struct user_namespace *ns, gid_t gid)
265{
266 /* Map the gid to a global kernel gid */
267 return KGIDT_INIT(map_id_down(&ns->gid_map, gid));
268}
269EXPORT_SYMBOL(make_kgid);
270
271/**
272 * from_kgid - Create a gid from a kgid user-namespace pair.
273 * @targ: The user namespace we want a gid in.
274 * @kgid: The kernel internal gid to start with.
275 *
276 * Map @kgid into the user-namespace specified by @targ and
277 * return the resulting gid.
278 *
279 * There is always a mapping into the initial user_namespace.
280 *
281 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
282 */
283gid_t from_kgid(struct user_namespace *targ, kgid_t kgid)
284{
285 /* Map the gid from a global kernel gid */
286 return map_id_up(&targ->gid_map, __kgid_val(kgid));
287}
288EXPORT_SYMBOL(from_kgid);
289
290/**
291 * from_kgid_munged - 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 * Unlike from_kgid from_kgid_munged never fails and always
301 * returns a valid gid. This makes from_kgid_munged appropriate
302 * for use in syscalls like stat and getgid where failing the
303 * system call and failing to provide a valid gid are not options.
304 *
305 * If @kgid has no mapping in @targ overflowgid is returned.
306 */
307gid_t from_kgid_munged(struct user_namespace *targ, kgid_t kgid)
308{
309 gid_t gid;
310 gid = from_kgid(targ, kgid);
311
312 if (gid == (gid_t) -1)
313 gid = overflowgid;
314 return gid;
315}
316EXPORT_SYMBOL(from_kgid_munged);
317
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700318/**
319 * make_kprojid - Map a user-namespace projid pair into a kprojid.
320 * @ns: User namespace that the projid is in
321 * @projid: Project identifier
322 *
323 * Maps a user-namespace uid pair into a kernel internal kuid,
324 * and returns that kuid.
325 *
326 * When there is no mapping defined for the user-namespace projid
327 * pair INVALID_PROJID is returned. Callers are expected to test
328 * for and handle handle INVALID_PROJID being returned. INVALID_PROJID
329 * may be tested for using projid_valid().
330 */
331kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid)
332{
333 /* Map the uid to a global kernel uid */
334 return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid));
335}
336EXPORT_SYMBOL(make_kprojid);
337
338/**
339 * from_kprojid - Create a projid from a kprojid user-namespace pair.
340 * @targ: The user namespace we want a projid in.
341 * @kprojid: The kernel internal project identifier to start with.
342 *
343 * Map @kprojid into the user-namespace specified by @targ and
344 * return the resulting projid.
345 *
346 * There is always a mapping into the initial user_namespace.
347 *
348 * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
349 */
350projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid)
351{
352 /* Map the uid from a global kernel uid */
353 return map_id_up(&targ->projid_map, __kprojid_val(kprojid));
354}
355EXPORT_SYMBOL(from_kprojid);
356
357/**
358 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
359 * @targ: The user namespace we want a projid in.
360 * @kprojid: The kernel internal projid 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 * Unlike from_kprojid from_kprojid_munged never fails and always
368 * returns a valid projid. This makes from_kprojid_munged
369 * appropriate for use in syscalls like stat and where
370 * failing the system call and failing to provide a valid projid are
371 * not an options.
372 *
373 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
374 */
375projid_t from_kprojid_munged(struct user_namespace *targ, kprojid_t kprojid)
376{
377 projid_t projid;
378 projid = from_kprojid(targ, kprojid);
379
380 if (projid == (projid_t) -1)
381 projid = OVERFLOW_PROJID;
382 return projid;
383}
384EXPORT_SYMBOL(from_kprojid_munged);
385
386
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800387static int uid_m_show(struct seq_file *seq, void *v)
388{
389 struct user_namespace *ns = seq->private;
390 struct uid_gid_extent *extent = v;
391 struct user_namespace *lower_ns;
392 uid_t lower;
393
394 lower_ns = current_user_ns();
395 if ((lower_ns == ns) && lower_ns->parent)
396 lower_ns = lower_ns->parent;
397
398 lower = from_kuid(lower_ns, KUIDT_INIT(extent->lower_first));
399
400 seq_printf(seq, "%10u %10u %10u\n",
401 extent->first,
402 lower,
403 extent->count);
404
405 return 0;
406}
407
408static int gid_m_show(struct seq_file *seq, void *v)
409{
410 struct user_namespace *ns = seq->private;
411 struct uid_gid_extent *extent = v;
412 struct user_namespace *lower_ns;
413 gid_t lower;
414
415 lower_ns = current_user_ns();
416 if ((lower_ns == ns) && lower_ns->parent)
417 lower_ns = lower_ns->parent;
418
419 lower = from_kgid(lower_ns, KGIDT_INIT(extent->lower_first));
420
421 seq_printf(seq, "%10u %10u %10u\n",
422 extent->first,
423 lower,
424 extent->count);
425
426 return 0;
427}
428
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700429static int projid_m_show(struct seq_file *seq, void *v)
430{
431 struct user_namespace *ns = seq->private;
432 struct uid_gid_extent *extent = v;
433 struct user_namespace *lower_ns;
434 projid_t lower;
435
436 lower_ns = seq_user_ns(seq);
437 if ((lower_ns == ns) && lower_ns->parent)
438 lower_ns = lower_ns->parent;
439
440 lower = from_kprojid(lower_ns, KPROJIDT_INIT(extent->lower_first));
441
442 seq_printf(seq, "%10u %10u %10u\n",
443 extent->first,
444 lower,
445 extent->count);
446
447 return 0;
448}
449
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800450static void *m_start(struct seq_file *seq, loff_t *ppos, struct uid_gid_map *map)
451{
452 struct uid_gid_extent *extent = NULL;
453 loff_t pos = *ppos;
454
455 if (pos < map->nr_extents)
456 extent = &map->extent[pos];
457
458 return extent;
459}
460
461static void *uid_m_start(struct seq_file *seq, loff_t *ppos)
462{
463 struct user_namespace *ns = seq->private;
464
465 return m_start(seq, ppos, &ns->uid_map);
466}
467
468static void *gid_m_start(struct seq_file *seq, loff_t *ppos)
469{
470 struct user_namespace *ns = seq->private;
471
472 return m_start(seq, ppos, &ns->gid_map);
473}
474
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700475static void *projid_m_start(struct seq_file *seq, loff_t *ppos)
476{
477 struct user_namespace *ns = seq->private;
478
479 return m_start(seq, ppos, &ns->projid_map);
480}
481
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800482static void *m_next(struct seq_file *seq, void *v, loff_t *pos)
483{
484 (*pos)++;
485 return seq->op->start(seq, pos);
486}
487
488static void m_stop(struct seq_file *seq, void *v)
489{
490 return;
491}
492
493struct seq_operations proc_uid_seq_operations = {
494 .start = uid_m_start,
495 .stop = m_stop,
496 .next = m_next,
497 .show = uid_m_show,
498};
499
500struct seq_operations proc_gid_seq_operations = {
501 .start = gid_m_start,
502 .stop = m_stop,
503 .next = m_next,
504 .show = gid_m_show,
505};
506
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700507struct seq_operations proc_projid_seq_operations = {
508 .start = projid_m_start,
509 .stop = m_stop,
510 .next = m_next,
511 .show = projid_m_show,
512};
513
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800514static DEFINE_MUTEX(id_map_mutex);
515
516static ssize_t map_write(struct file *file, const char __user *buf,
517 size_t count, loff_t *ppos,
518 int cap_setid,
519 struct uid_gid_map *map,
520 struct uid_gid_map *parent_map)
521{
522 struct seq_file *seq = file->private_data;
523 struct user_namespace *ns = seq->private;
524 struct uid_gid_map new_map;
525 unsigned idx;
526 struct uid_gid_extent *extent, *last = NULL;
527 unsigned long page = 0;
528 char *kbuf, *pos, *next_line;
529 ssize_t ret = -EINVAL;
530
531 /*
532 * The id_map_mutex serializes all writes to any given map.
533 *
534 * Any map is only ever written once.
535 *
536 * An id map fits within 1 cache line on most architectures.
537 *
538 * On read nothing needs to be done unless you are on an
539 * architecture with a crazy cache coherency model like alpha.
540 *
541 * There is a one time data dependency between reading the
542 * count of the extents and the values of the extents. The
543 * desired behavior is to see the values of the extents that
544 * were written before the count of the extents.
545 *
546 * To achieve this smp_wmb() is used on guarantee the write
547 * order and smp_read_barrier_depends() is guaranteed that we
548 * don't have crazy architectures returning stale data.
549 *
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000550 */
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800551 mutex_lock(&id_map_mutex);
552
553 ret = -EPERM;
554 /* Only allow one successful write to the map */
555 if (map->nr_extents != 0)
556 goto out;
557
558 /* Require the appropriate privilege CAP_SETUID or CAP_SETGID
559 * over the user namespace in order to set the id mapping.
560 */
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700561 if (cap_valid(cap_setid) && !ns_capable(ns, cap_setid))
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800562 goto out;
563
564 /* Get a buffer */
565 ret = -ENOMEM;
566 page = __get_free_page(GFP_TEMPORARY);
567 kbuf = (char *) page;
568 if (!page)
569 goto out;
570
571 /* Only allow <= page size writes at the beginning of the file */
572 ret = -EINVAL;
573 if ((*ppos != 0) || (count >= PAGE_SIZE))
574 goto out;
575
576 /* Slurp in the user data */
577 ret = -EFAULT;
578 if (copy_from_user(kbuf, buf, count))
579 goto out;
580 kbuf[count] = '\0';
581
582 /* Parse the user data */
583 ret = -EINVAL;
584 pos = kbuf;
585 new_map.nr_extents = 0;
586 for (;pos; pos = next_line) {
587 extent = &new_map.extent[new_map.nr_extents];
588
589 /* Find the end of line and ensure I don't look past it */
590 next_line = strchr(pos, '\n');
591 if (next_line) {
592 *next_line = '\0';
593 next_line++;
594 if (*next_line == '\0')
595 next_line = NULL;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000596 }
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800597
598 pos = skip_spaces(pos);
599 extent->first = simple_strtoul(pos, &pos, 10);
600 if (!isspace(*pos))
601 goto out;
602
603 pos = skip_spaces(pos);
604 extent->lower_first = simple_strtoul(pos, &pos, 10);
605 if (!isspace(*pos))
606 goto out;
607
608 pos = skip_spaces(pos);
609 extent->count = simple_strtoul(pos, &pos, 10);
610 if (*pos && !isspace(*pos))
611 goto out;
612
613 /* Verify there is not trailing junk on the line */
614 pos = skip_spaces(pos);
615 if (*pos != '\0')
616 goto out;
617
618 /* Verify we have been given valid starting values */
619 if ((extent->first == (u32) -1) ||
620 (extent->lower_first == (u32) -1 ))
621 goto out;
622
623 /* Verify count is not zero and does not cause the extent to wrap */
624 if ((extent->first + extent->count) <= extent->first)
625 goto out;
626 if ((extent->lower_first + extent->count) <= extent->lower_first)
627 goto out;
628
629 /* For now only accept extents that are strictly in order */
630 if (last &&
631 (((last->first + last->count) > extent->first) ||
632 ((last->lower_first + last->count) > extent->lower_first)))
633 goto out;
634
635 new_map.nr_extents++;
636 last = extent;
637
638 /* Fail if the file contains too many extents */
639 if ((new_map.nr_extents == UID_GID_MAP_MAX_EXTENTS) &&
640 (next_line != NULL))
641 goto out;
642 }
643 /* Be very certaint the new map actually exists */
644 if (new_map.nr_extents == 0)
645 goto out;
646
647 ret = -EPERM;
648 /* Validate the user is allowed to use user id's mapped to. */
649 if (!new_idmap_permitted(ns, cap_setid, &new_map))
650 goto out;
651
652 /* Map the lower ids from the parent user namespace to the
653 * kernel global id space.
654 */
655 for (idx = 0; idx < new_map.nr_extents; idx++) {
656 u32 lower_first;
657 extent = &new_map.extent[idx];
658
659 lower_first = map_id_range_down(parent_map,
660 extent->lower_first,
661 extent->count);
662
663 /* Fail if we can not map the specified extent to
664 * the kernel global id space.
665 */
666 if (lower_first == (u32) -1)
667 goto out;
668
669 extent->lower_first = lower_first;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000670 }
671
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800672 /* Install the map */
673 memcpy(map->extent, new_map.extent,
674 new_map.nr_extents*sizeof(new_map.extent[0]));
675 smp_wmb();
676 map->nr_extents = new_map.nr_extents;
677
678 *ppos = count;
679 ret = count;
680out:
681 mutex_unlock(&id_map_mutex);
682 if (page)
683 free_page(page);
684 return ret;
685}
686
687ssize_t proc_uid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
688{
689 struct seq_file *seq = file->private_data;
690 struct user_namespace *ns = seq->private;
691
692 if (!ns->parent)
693 return -EPERM;
694
695 return map_write(file, buf, size, ppos, CAP_SETUID,
696 &ns->uid_map, &ns->parent->uid_map);
697}
698
699ssize_t proc_gid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
700{
701 struct seq_file *seq = file->private_data;
702 struct user_namespace *ns = seq->private;
703
704 if (!ns->parent)
705 return -EPERM;
706
707 return map_write(file, buf, size, ppos, CAP_SETGID,
708 &ns->gid_map, &ns->parent->gid_map);
709}
710
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700711ssize_t proc_projid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
712{
713 struct seq_file *seq = file->private_data;
714 struct user_namespace *ns = seq->private;
715 struct user_namespace *seq_ns = seq_user_ns(seq);
716
717 if (!ns->parent)
718 return -EPERM;
719
720 if ((seq_ns != ns) && (seq_ns != ns->parent))
721 return -EPERM;
722
723 /* Anyone can set any valid project id no capability needed */
724 return map_write(file, buf, size, ppos, -1,
725 &ns->projid_map, &ns->parent->projid_map);
726}
727
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800728static bool new_idmap_permitted(struct user_namespace *ns, int cap_setid,
729 struct uid_gid_map *new_map)
730{
Eric W. Biederman37657da2012-07-27 06:21:27 -0700731 /* Allow mapping to your own filesystem ids */
732 if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1)) {
733 u32 id = new_map->extent[0].lower_first;
734 if (cap_setid == CAP_SETUID) {
735 kuid_t uid = make_kuid(ns->parent, id);
736 if (uid_eq(uid, current_fsuid()))
737 return true;
738 }
739 else if (cap_setid == CAP_SETGID) {
740 kgid_t gid = make_kgid(ns->parent, id);
741 if (gid_eq(gid, current_fsgid()))
742 return true;
743 }
744 }
745
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700746 /* Allow anyone to set a mapping that doesn't require privilege */
747 if (!cap_valid(cap_setid))
748 return true;
749
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800750 /* Allow the specified ids if we have the appropriate capability
751 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
752 */
753 if (ns_capable(ns->parent, cap_setid))
754 return true;
755
756 return false;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000757}
Pavel Emelyanov61642812011-01-12 17:00:46 -0800758
Eric W. Biedermancde19752012-07-26 06:24:06 -0700759static void *userns_get(struct task_struct *task)
760{
761 struct user_namespace *user_ns;
762
763 rcu_read_lock();
764 user_ns = get_user_ns(__task_cred(task)->user_ns);
765 rcu_read_unlock();
766
767 return user_ns;
768}
769
770static void userns_put(void *ns)
771{
772 put_user_ns(ns);
773}
774
775static int userns_install(struct nsproxy *nsproxy, void *ns)
776{
777 struct user_namespace *user_ns = ns;
778 struct cred *cred;
779
780 /* Don't allow gaining capabilities by reentering
781 * the same user namespace.
782 */
783 if (user_ns == current_user_ns())
784 return -EINVAL;
785
786 /* Threaded many not enter a different user namespace */
787 if (atomic_read(&current->mm->mm_users) > 1)
788 return -EINVAL;
789
790 if (!ns_capable(user_ns, CAP_SYS_ADMIN))
791 return -EPERM;
792
793 cred = prepare_creds();
794 if (!cred)
795 return -ENOMEM;
796
797 put_user_ns(cred->user_ns);
798 set_cred_user_ns(cred, get_user_ns(user_ns));
799
800 return commit_creds(cred);
801}
802
803const struct proc_ns_operations userns_operations = {
804 .name = "user",
805 .type = CLONE_NEWUSER,
806 .get = userns_get,
807 .put = userns_put,
808 .install = userns_install,
809};
810
Pavel Emelyanov61642812011-01-12 17:00:46 -0800811static __init int user_namespaces_init(void)
812{
813 user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
814 return 0;
815}
816module_init(user_namespaces_init);