blob: 24f8ec3b64d8623fc114880f49aca357e11411eb [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. Biederman98f842e2011-06-15 10:21:48 -070061 int ret;
Eric W. Biederman783291e2011-11-17 01:32:59 -080062
63 /* The creator needs a mapping in the parent user namespace
64 * or else we won't be able to reasonably tell userspace who
65 * created a user_namespace.
66 */
67 if (!kuid_has_mapping(parent_ns, owner) ||
68 !kgid_has_mapping(parent_ns, group))
69 return -EPERM;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070070
Eric W. Biederman22d917d2011-11-17 00:11:58 -080071 ns = kmem_cache_zalloc(user_ns_cachep, GFP_KERNEL);
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070072 if (!ns)
Serge Hallyn18b6e042008-10-15 16:38:45 -050073 return -ENOMEM;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070074
Eric W. Biederman98f842e2011-06-15 10:21:48 -070075 ret = proc_alloc_inum(&ns->proc_inum);
76 if (ret) {
77 kmem_cache_free(user_ns_cachep, ns);
78 return ret;
79 }
80
Eric W. Biedermanc61a2812012-12-28 18:58:39 -080081 atomic_set(&ns->count, 1);
Eric W. Biedermancde19752012-07-26 06:24:06 -070082 /* Leave the new->user_ns reference with the new user namespace. */
Eric W. Biedermanaeb3ae92011-11-16 21:59:43 -080083 ns->parent = parent_ns;
Eric W. Biederman783291e2011-11-17 01:32:59 -080084 ns->owner = owner;
85 ns->group = group;
Eric W. Biederman22d917d2011-11-17 00:11:58 -080086
Eric W. Biedermancde19752012-07-26 06:24:06 -070087 set_cred_user_ns(new, ns);
Eric W. Biederman0093ccb2011-11-16 21:52:53 -080088
Serge Hallyn18b6e042008-10-15 16:38:45 -050089 return 0;
Cedric Le Goateracce2922007-07-15 23:40:59 -070090}
91
Eric W. Biedermanb2e0d9872012-07-26 05:15:35 -070092int unshare_userns(unsigned long unshare_flags, struct cred **new_cred)
93{
94 struct cred *cred;
95
96 if (!(unshare_flags & CLONE_NEWUSER))
97 return 0;
98
99 cred = prepare_creds();
100 if (!cred)
101 return -ENOMEM;
102
103 *new_cred = cred;
104 return create_user_ns(cred);
105}
106
Eric W. Biedermanc61a2812012-12-28 18:58:39 -0800107void free_user_ns(struct user_namespace *ns)
David Howells51708362009-02-27 14:03:03 -0800108{
Eric W. Biedermanc61a2812012-12-28 18:58:39 -0800109 struct user_namespace *parent;
David Howells51708362009-02-27 14:03:03 -0800110
Eric W. Biedermanc61a2812012-12-28 18:58:39 -0800111 do {
112 parent = ns->parent;
113 proc_free_inum(ns->proc_inum);
114 kmem_cache_free(user_ns_cachep, ns);
115 ns = parent;
116 } while (atomic_dec_and_test(&parent->count));
David Howells51708362009-02-27 14:03:03 -0800117}
Michael Halcrow6a3fd922008-04-29 00:59:52 -0700118EXPORT_SYMBOL(free_user_ns);
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000119
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800120static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count)
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000121{
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800122 unsigned idx, extents;
123 u32 first, last, id2;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000124
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800125 id2 = id + count - 1;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000126
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800127 /* Find the matching extent */
128 extents = map->nr_extents;
129 smp_read_barrier_depends();
130 for (idx = 0; idx < extents; idx++) {
131 first = map->extent[idx].first;
132 last = first + map->extent[idx].count - 1;
133 if (id >= first && id <= last &&
134 (id2 >= first && id2 <= last))
135 break;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000136 }
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800137 /* Map the id or note failure */
138 if (idx < extents)
139 id = (id - first) + map->extent[idx].lower_first;
140 else
141 id = (u32) -1;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000142
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800143 return id;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000144}
145
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800146static u32 map_id_down(struct uid_gid_map *map, u32 id)
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000147{
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800148 unsigned idx, extents;
149 u32 first, last;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000150
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800151 /* Find the matching extent */
152 extents = map->nr_extents;
153 smp_read_barrier_depends();
154 for (idx = 0; idx < extents; idx++) {
155 first = map->extent[idx].first;
156 last = first + map->extent[idx].count - 1;
157 if (id >= first && id <= last)
158 break;
159 }
160 /* Map the id or note failure */
161 if (idx < extents)
162 id = (id - first) + map->extent[idx].lower_first;
163 else
164 id = (u32) -1;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000165
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800166 return id;
167}
168
169static u32 map_id_up(struct uid_gid_map *map, u32 id)
170{
171 unsigned idx, extents;
172 u32 first, last;
173
174 /* Find the matching extent */
175 extents = map->nr_extents;
176 smp_read_barrier_depends();
177 for (idx = 0; idx < extents; idx++) {
178 first = map->extent[idx].lower_first;
179 last = first + map->extent[idx].count - 1;
180 if (id >= first && id <= last)
181 break;
182 }
183 /* Map the id or note failure */
184 if (idx < extents)
185 id = (id - first) + map->extent[idx].first;
186 else
187 id = (u32) -1;
188
189 return id;
190}
191
192/**
193 * make_kuid - Map a user-namespace uid pair into a kuid.
194 * @ns: User namespace that the uid is in
195 * @uid: User identifier
196 *
197 * Maps a user-namespace uid pair into a kernel internal kuid,
198 * and returns that kuid.
199 *
200 * When there is no mapping defined for the user-namespace uid
201 * pair INVALID_UID is returned. Callers are expected to test
202 * for and handle handle INVALID_UID being returned. INVALID_UID
203 * may be tested for using uid_valid().
204 */
205kuid_t make_kuid(struct user_namespace *ns, uid_t uid)
206{
207 /* Map the uid to a global kernel uid */
208 return KUIDT_INIT(map_id_down(&ns->uid_map, uid));
209}
210EXPORT_SYMBOL(make_kuid);
211
212/**
213 * from_kuid - Create a uid from a kuid user-namespace pair.
214 * @targ: The user namespace we want a uid in.
215 * @kuid: The kernel internal uid to start with.
216 *
217 * Map @kuid into the user-namespace specified by @targ and
218 * return the resulting uid.
219 *
220 * There is always a mapping into the initial user_namespace.
221 *
222 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
223 */
224uid_t from_kuid(struct user_namespace *targ, kuid_t kuid)
225{
226 /* Map the uid from a global kernel uid */
227 return map_id_up(&targ->uid_map, __kuid_val(kuid));
228}
229EXPORT_SYMBOL(from_kuid);
230
231/**
232 * from_kuid_munged - Create a uid from a kuid user-namespace pair.
233 * @targ: The user namespace we want a uid in.
234 * @kuid: The kernel internal uid to start with.
235 *
236 * Map @kuid into the user-namespace specified by @targ and
237 * return the resulting uid.
238 *
239 * There is always a mapping into the initial user_namespace.
240 *
241 * Unlike from_kuid from_kuid_munged never fails and always
242 * returns a valid uid. This makes from_kuid_munged appropriate
243 * for use in syscalls like stat and getuid where failing the
244 * system call and failing to provide a valid uid are not an
245 * options.
246 *
247 * If @kuid has no mapping in @targ overflowuid is returned.
248 */
249uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid)
250{
251 uid_t uid;
252 uid = from_kuid(targ, kuid);
253
254 if (uid == (uid_t) -1)
255 uid = overflowuid;
256 return uid;
257}
258EXPORT_SYMBOL(from_kuid_munged);
259
260/**
261 * make_kgid - Map a user-namespace gid pair into a kgid.
262 * @ns: User namespace that the gid is in
263 * @uid: group identifier
264 *
265 * Maps a user-namespace gid pair into a kernel internal kgid,
266 * and returns that kgid.
267 *
268 * When there is no mapping defined for the user-namespace gid
269 * pair INVALID_GID is returned. Callers are expected to test
270 * for and handle INVALID_GID being returned. INVALID_GID may be
271 * tested for using gid_valid().
272 */
273kgid_t make_kgid(struct user_namespace *ns, gid_t gid)
274{
275 /* Map the gid to a global kernel gid */
276 return KGIDT_INIT(map_id_down(&ns->gid_map, gid));
277}
278EXPORT_SYMBOL(make_kgid);
279
280/**
281 * from_kgid - Create a gid from a kgid user-namespace pair.
282 * @targ: The user namespace we want a gid in.
283 * @kgid: The kernel internal gid to start with.
284 *
285 * Map @kgid into the user-namespace specified by @targ and
286 * return the resulting gid.
287 *
288 * There is always a mapping into the initial user_namespace.
289 *
290 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
291 */
292gid_t from_kgid(struct user_namespace *targ, kgid_t kgid)
293{
294 /* Map the gid from a global kernel gid */
295 return map_id_up(&targ->gid_map, __kgid_val(kgid));
296}
297EXPORT_SYMBOL(from_kgid);
298
299/**
300 * from_kgid_munged - Create a gid from a kgid user-namespace pair.
301 * @targ: The user namespace we want a gid in.
302 * @kgid: The kernel internal gid to start with.
303 *
304 * Map @kgid into the user-namespace specified by @targ and
305 * return the resulting gid.
306 *
307 * There is always a mapping into the initial user_namespace.
308 *
309 * Unlike from_kgid from_kgid_munged never fails and always
310 * returns a valid gid. This makes from_kgid_munged appropriate
311 * for use in syscalls like stat and getgid where failing the
312 * system call and failing to provide a valid gid are not options.
313 *
314 * If @kgid has no mapping in @targ overflowgid is returned.
315 */
316gid_t from_kgid_munged(struct user_namespace *targ, kgid_t kgid)
317{
318 gid_t gid;
319 gid = from_kgid(targ, kgid);
320
321 if (gid == (gid_t) -1)
322 gid = overflowgid;
323 return gid;
324}
325EXPORT_SYMBOL(from_kgid_munged);
326
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700327/**
328 * make_kprojid - Map a user-namespace projid pair into a kprojid.
329 * @ns: User namespace that the projid is in
330 * @projid: Project identifier
331 *
332 * Maps a user-namespace uid pair into a kernel internal kuid,
333 * and returns that kuid.
334 *
335 * When there is no mapping defined for the user-namespace projid
336 * pair INVALID_PROJID is returned. Callers are expected to test
337 * for and handle handle INVALID_PROJID being returned. INVALID_PROJID
338 * may be tested for using projid_valid().
339 */
340kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid)
341{
342 /* Map the uid to a global kernel uid */
343 return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid));
344}
345EXPORT_SYMBOL(make_kprojid);
346
347/**
348 * from_kprojid - Create a projid from a kprojid user-namespace pair.
349 * @targ: The user namespace we want a projid in.
350 * @kprojid: The kernel internal project identifier to start with.
351 *
352 * Map @kprojid into the user-namespace specified by @targ and
353 * return the resulting projid.
354 *
355 * There is always a mapping into the initial user_namespace.
356 *
357 * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
358 */
359projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid)
360{
361 /* Map the uid from a global kernel uid */
362 return map_id_up(&targ->projid_map, __kprojid_val(kprojid));
363}
364EXPORT_SYMBOL(from_kprojid);
365
366/**
367 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
368 * @targ: The user namespace we want a projid in.
369 * @kprojid: The kernel internal projid to start with.
370 *
371 * Map @kprojid into the user-namespace specified by @targ and
372 * return the resulting projid.
373 *
374 * There is always a mapping into the initial user_namespace.
375 *
376 * Unlike from_kprojid from_kprojid_munged never fails and always
377 * returns a valid projid. This makes from_kprojid_munged
378 * appropriate for use in syscalls like stat and where
379 * failing the system call and failing to provide a valid projid are
380 * not an options.
381 *
382 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
383 */
384projid_t from_kprojid_munged(struct user_namespace *targ, kprojid_t kprojid)
385{
386 projid_t projid;
387 projid = from_kprojid(targ, kprojid);
388
389 if (projid == (projid_t) -1)
390 projid = OVERFLOW_PROJID;
391 return projid;
392}
393EXPORT_SYMBOL(from_kprojid_munged);
394
395
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800396static int uid_m_show(struct seq_file *seq, void *v)
397{
398 struct user_namespace *ns = seq->private;
399 struct uid_gid_extent *extent = v;
400 struct user_namespace *lower_ns;
401 uid_t lower;
402
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700403 lower_ns = seq_user_ns(seq);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800404 if ((lower_ns == ns) && lower_ns->parent)
405 lower_ns = lower_ns->parent;
406
407 lower = from_kuid(lower_ns, KUIDT_INIT(extent->lower_first));
408
409 seq_printf(seq, "%10u %10u %10u\n",
410 extent->first,
411 lower,
412 extent->count);
413
414 return 0;
415}
416
417static int gid_m_show(struct seq_file *seq, void *v)
418{
419 struct user_namespace *ns = seq->private;
420 struct uid_gid_extent *extent = v;
421 struct user_namespace *lower_ns;
422 gid_t lower;
423
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700424 lower_ns = seq_user_ns(seq);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800425 if ((lower_ns == ns) && lower_ns->parent)
426 lower_ns = lower_ns->parent;
427
428 lower = from_kgid(lower_ns, KGIDT_INIT(extent->lower_first));
429
430 seq_printf(seq, "%10u %10u %10u\n",
431 extent->first,
432 lower,
433 extent->count);
434
435 return 0;
436}
437
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700438static int projid_m_show(struct seq_file *seq, void *v)
439{
440 struct user_namespace *ns = seq->private;
441 struct uid_gid_extent *extent = v;
442 struct user_namespace *lower_ns;
443 projid_t lower;
444
445 lower_ns = seq_user_ns(seq);
446 if ((lower_ns == ns) && lower_ns->parent)
447 lower_ns = lower_ns->parent;
448
449 lower = from_kprojid(lower_ns, KPROJIDT_INIT(extent->lower_first));
450
451 seq_printf(seq, "%10u %10u %10u\n",
452 extent->first,
453 lower,
454 extent->count);
455
456 return 0;
457}
458
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800459static void *m_start(struct seq_file *seq, loff_t *ppos, struct uid_gid_map *map)
460{
461 struct uid_gid_extent *extent = NULL;
462 loff_t pos = *ppos;
463
464 if (pos < map->nr_extents)
465 extent = &map->extent[pos];
466
467 return extent;
468}
469
470static void *uid_m_start(struct seq_file *seq, loff_t *ppos)
471{
472 struct user_namespace *ns = seq->private;
473
474 return m_start(seq, ppos, &ns->uid_map);
475}
476
477static void *gid_m_start(struct seq_file *seq, loff_t *ppos)
478{
479 struct user_namespace *ns = seq->private;
480
481 return m_start(seq, ppos, &ns->gid_map);
482}
483
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700484static void *projid_m_start(struct seq_file *seq, loff_t *ppos)
485{
486 struct user_namespace *ns = seq->private;
487
488 return m_start(seq, ppos, &ns->projid_map);
489}
490
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800491static void *m_next(struct seq_file *seq, void *v, loff_t *pos)
492{
493 (*pos)++;
494 return seq->op->start(seq, pos);
495}
496
497static void m_stop(struct seq_file *seq, void *v)
498{
499 return;
500}
501
502struct seq_operations proc_uid_seq_operations = {
503 .start = uid_m_start,
504 .stop = m_stop,
505 .next = m_next,
506 .show = uid_m_show,
507};
508
509struct seq_operations proc_gid_seq_operations = {
510 .start = gid_m_start,
511 .stop = m_stop,
512 .next = m_next,
513 .show = gid_m_show,
514};
515
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700516struct seq_operations proc_projid_seq_operations = {
517 .start = projid_m_start,
518 .stop = m_stop,
519 .next = m_next,
520 .show = projid_m_show,
521};
522
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800523static DEFINE_MUTEX(id_map_mutex);
524
525static ssize_t map_write(struct file *file, const char __user *buf,
526 size_t count, loff_t *ppos,
527 int cap_setid,
528 struct uid_gid_map *map,
529 struct uid_gid_map *parent_map)
530{
531 struct seq_file *seq = file->private_data;
532 struct user_namespace *ns = seq->private;
533 struct uid_gid_map new_map;
534 unsigned idx;
535 struct uid_gid_extent *extent, *last = NULL;
536 unsigned long page = 0;
537 char *kbuf, *pos, *next_line;
538 ssize_t ret = -EINVAL;
539
540 /*
541 * The id_map_mutex serializes all writes to any given map.
542 *
543 * Any map is only ever written once.
544 *
545 * An id map fits within 1 cache line on most architectures.
546 *
547 * On read nothing needs to be done unless you are on an
548 * architecture with a crazy cache coherency model like alpha.
549 *
550 * There is a one time data dependency between reading the
551 * count of the extents and the values of the extents. The
552 * desired behavior is to see the values of the extents that
553 * were written before the count of the extents.
554 *
555 * To achieve this smp_wmb() is used on guarantee the write
556 * order and smp_read_barrier_depends() is guaranteed that we
557 * don't have crazy architectures returning stale data.
558 *
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000559 */
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800560 mutex_lock(&id_map_mutex);
561
562 ret = -EPERM;
563 /* Only allow one successful write to the map */
564 if (map->nr_extents != 0)
565 goto out;
566
567 /* Require the appropriate privilege CAP_SETUID or CAP_SETGID
568 * over the user namespace in order to set the id mapping.
569 */
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700570 if (cap_valid(cap_setid) && !ns_capable(ns, cap_setid))
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800571 goto out;
572
573 /* Get a buffer */
574 ret = -ENOMEM;
575 page = __get_free_page(GFP_TEMPORARY);
576 kbuf = (char *) page;
577 if (!page)
578 goto out;
579
580 /* Only allow <= page size writes at the beginning of the file */
581 ret = -EINVAL;
582 if ((*ppos != 0) || (count >= PAGE_SIZE))
583 goto out;
584
585 /* Slurp in the user data */
586 ret = -EFAULT;
587 if (copy_from_user(kbuf, buf, count))
588 goto out;
589 kbuf[count] = '\0';
590
591 /* Parse the user data */
592 ret = -EINVAL;
593 pos = kbuf;
594 new_map.nr_extents = 0;
595 for (;pos; pos = next_line) {
596 extent = &new_map.extent[new_map.nr_extents];
597
598 /* Find the end of line and ensure I don't look past it */
599 next_line = strchr(pos, '\n');
600 if (next_line) {
601 *next_line = '\0';
602 next_line++;
603 if (*next_line == '\0')
604 next_line = NULL;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000605 }
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800606
607 pos = skip_spaces(pos);
608 extent->first = simple_strtoul(pos, &pos, 10);
609 if (!isspace(*pos))
610 goto out;
611
612 pos = skip_spaces(pos);
613 extent->lower_first = simple_strtoul(pos, &pos, 10);
614 if (!isspace(*pos))
615 goto out;
616
617 pos = skip_spaces(pos);
618 extent->count = simple_strtoul(pos, &pos, 10);
619 if (*pos && !isspace(*pos))
620 goto out;
621
622 /* Verify there is not trailing junk on the line */
623 pos = skip_spaces(pos);
624 if (*pos != '\0')
625 goto out;
626
627 /* Verify we have been given valid starting values */
628 if ((extent->first == (u32) -1) ||
629 (extent->lower_first == (u32) -1 ))
630 goto out;
631
632 /* Verify count is not zero and does not cause the extent to wrap */
633 if ((extent->first + extent->count) <= extent->first)
634 goto out;
635 if ((extent->lower_first + extent->count) <= extent->lower_first)
636 goto out;
637
638 /* For now only accept extents that are strictly in order */
639 if (last &&
640 (((last->first + last->count) > extent->first) ||
641 ((last->lower_first + last->count) > extent->lower_first)))
642 goto out;
643
644 new_map.nr_extents++;
645 last = extent;
646
647 /* Fail if the file contains too many extents */
648 if ((new_map.nr_extents == UID_GID_MAP_MAX_EXTENTS) &&
649 (next_line != NULL))
650 goto out;
651 }
652 /* Be very certaint the new map actually exists */
653 if (new_map.nr_extents == 0)
654 goto out;
655
656 ret = -EPERM;
657 /* Validate the user is allowed to use user id's mapped to. */
658 if (!new_idmap_permitted(ns, cap_setid, &new_map))
659 goto out;
660
661 /* Map the lower ids from the parent user namespace to the
662 * kernel global id space.
663 */
664 for (idx = 0; idx < new_map.nr_extents; idx++) {
665 u32 lower_first;
666 extent = &new_map.extent[idx];
667
668 lower_first = map_id_range_down(parent_map,
669 extent->lower_first,
670 extent->count);
671
672 /* Fail if we can not map the specified extent to
673 * the kernel global id space.
674 */
675 if (lower_first == (u32) -1)
676 goto out;
677
678 extent->lower_first = lower_first;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000679 }
680
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800681 /* Install the map */
682 memcpy(map->extent, new_map.extent,
683 new_map.nr_extents*sizeof(new_map.extent[0]));
684 smp_wmb();
685 map->nr_extents = new_map.nr_extents;
686
687 *ppos = count;
688 ret = count;
689out:
690 mutex_unlock(&id_map_mutex);
691 if (page)
692 free_page(page);
693 return ret;
694}
695
696ssize_t proc_uid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
697{
698 struct seq_file *seq = file->private_data;
699 struct user_namespace *ns = seq->private;
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700700 struct user_namespace *seq_ns = seq_user_ns(seq);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800701
702 if (!ns->parent)
703 return -EPERM;
704
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700705 if ((seq_ns != ns) && (seq_ns != ns->parent))
706 return -EPERM;
707
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800708 return map_write(file, buf, size, ppos, CAP_SETUID,
709 &ns->uid_map, &ns->parent->uid_map);
710}
711
712ssize_t proc_gid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
713{
714 struct seq_file *seq = file->private_data;
715 struct user_namespace *ns = seq->private;
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700716 struct user_namespace *seq_ns = seq_user_ns(seq);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800717
718 if (!ns->parent)
719 return -EPERM;
720
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700721 if ((seq_ns != ns) && (seq_ns != ns->parent))
722 return -EPERM;
723
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800724 return map_write(file, buf, size, ppos, CAP_SETGID,
725 &ns->gid_map, &ns->parent->gid_map);
726}
727
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700728ssize_t proc_projid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
729{
730 struct seq_file *seq = file->private_data;
731 struct user_namespace *ns = seq->private;
732 struct user_namespace *seq_ns = seq_user_ns(seq);
733
734 if (!ns->parent)
735 return -EPERM;
736
737 if ((seq_ns != ns) && (seq_ns != ns->parent))
738 return -EPERM;
739
740 /* Anyone can set any valid project id no capability needed */
741 return map_write(file, buf, size, ppos, -1,
742 &ns->projid_map, &ns->parent->projid_map);
743}
744
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800745static bool new_idmap_permitted(struct user_namespace *ns, int cap_setid,
746 struct uid_gid_map *new_map)
747{
Eric W. Biederman37657da2012-07-27 06:21:27 -0700748 /* Allow mapping to your own filesystem ids */
749 if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1)) {
750 u32 id = new_map->extent[0].lower_first;
751 if (cap_setid == CAP_SETUID) {
752 kuid_t uid = make_kuid(ns->parent, id);
753 if (uid_eq(uid, current_fsuid()))
754 return true;
755 }
756 else if (cap_setid == CAP_SETGID) {
757 kgid_t gid = make_kgid(ns->parent, id);
758 if (gid_eq(gid, current_fsgid()))
759 return true;
760 }
761 }
762
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700763 /* Allow anyone to set a mapping that doesn't require privilege */
764 if (!cap_valid(cap_setid))
765 return true;
766
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800767 /* Allow the specified ids if we have the appropriate capability
768 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
769 */
770 if (ns_capable(ns->parent, cap_setid))
771 return true;
772
773 return false;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000774}
Pavel Emelyanov61642812011-01-12 17:00:46 -0800775
Eric W. Biedermancde19752012-07-26 06:24:06 -0700776static void *userns_get(struct task_struct *task)
777{
778 struct user_namespace *user_ns;
779
780 rcu_read_lock();
781 user_ns = get_user_ns(__task_cred(task)->user_ns);
782 rcu_read_unlock();
783
784 return user_ns;
785}
786
787static void userns_put(void *ns)
788{
789 put_user_ns(ns);
790}
791
792static int userns_install(struct nsproxy *nsproxy, void *ns)
793{
794 struct user_namespace *user_ns = ns;
795 struct cred *cred;
796
797 /* Don't allow gaining capabilities by reentering
798 * the same user namespace.
799 */
800 if (user_ns == current_user_ns())
801 return -EINVAL;
802
Eric W. Biederman51550402012-12-09 17:19:52 -0800803 /* Threaded processes may not enter a different user namespace */
Eric W. Biedermancde19752012-07-26 06:24:06 -0700804 if (atomic_read(&current->mm->mm_users) > 1)
805 return -EINVAL;
806
807 if (!ns_capable(user_ns, CAP_SYS_ADMIN))
808 return -EPERM;
809
810 cred = prepare_creds();
811 if (!cred)
812 return -ENOMEM;
813
814 put_user_ns(cred->user_ns);
815 set_cred_user_ns(cred, get_user_ns(user_ns));
816
817 return commit_creds(cred);
818}
819
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700820static unsigned int userns_inum(void *ns)
821{
822 struct user_namespace *user_ns = ns;
823 return user_ns->proc_inum;
824}
825
Eric W. Biedermancde19752012-07-26 06:24:06 -0700826const struct proc_ns_operations userns_operations = {
827 .name = "user",
828 .type = CLONE_NEWUSER,
829 .get = userns_get,
830 .put = userns_put,
831 .install = userns_install,
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700832 .inum = userns_inum,
Eric W. Biedermancde19752012-07-26 06:24:06 -0700833};
834
Pavel Emelyanov61642812011-01-12 17:00:46 -0800835static __init int user_namespaces_init(void)
836{
837 user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
838 return 0;
839}
840module_init(user_namespaces_init);