blob: 51855f5f6311211c785373d52e990f879ec959fe [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>
David Howells0bb80f22013-04-12 01:50:06 +010012#include <linux/proc_ns.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
64 /* The creator needs a mapping in the parent user namespace
65 * or else we won't be able to reasonably tell userspace who
66 * created a user_namespace.
67 */
68 if (!kuid_has_mapping(parent_ns, owner) ||
69 !kgid_has_mapping(parent_ns, group))
70 return -EPERM;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070071
Eric W. Biederman22d917d2011-11-17 00:11:58 -080072 ns = kmem_cache_zalloc(user_ns_cachep, GFP_KERNEL);
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070073 if (!ns)
Serge Hallyn18b6e042008-10-15 16:38:45 -050074 return -ENOMEM;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070075
Eric W. Biederman98f842e2011-06-15 10:21:48 -070076 ret = proc_alloc_inum(&ns->proc_inum);
77 if (ret) {
78 kmem_cache_free(user_ns_cachep, ns);
79 return ret;
80 }
81
Eric W. Biedermanc61a2812012-12-28 18:58:39 -080082 atomic_set(&ns->count, 1);
Eric W. Biedermancde19752012-07-26 06:24:06 -070083 /* Leave the new->user_ns reference with the new user namespace. */
Eric W. Biedermanaeb3ae92011-11-16 21:59:43 -080084 ns->parent = parent_ns;
Eric W. Biederman783291e2011-11-17 01:32:59 -080085 ns->owner = owner;
86 ns->group = group;
Eric W. Biederman22d917d2011-11-17 00:11:58 -080087
Eric W. Biedermancde19752012-07-26 06:24:06 -070088 set_cred_user_ns(new, ns);
Eric W. Biederman0093ccb2011-11-16 21:52:53 -080089
Serge Hallyn18b6e042008-10-15 16:38:45 -050090 return 0;
Cedric Le Goateracce2922007-07-15 23:40:59 -070091}
92
Eric W. Biedermanb2e0d9872012-07-26 05:15:35 -070093int unshare_userns(unsigned long unshare_flags, struct cred **new_cred)
94{
95 struct cred *cred;
96
97 if (!(unshare_flags & CLONE_NEWUSER))
98 return 0;
99
100 cred = prepare_creds();
101 if (!cred)
102 return -ENOMEM;
103
104 *new_cred = cred;
105 return create_user_ns(cred);
106}
107
Eric W. Biedermanc61a2812012-12-28 18:58:39 -0800108void free_user_ns(struct user_namespace *ns)
David Howells51708362009-02-27 14:03:03 -0800109{
Eric W. Biedermanc61a2812012-12-28 18:58:39 -0800110 struct user_namespace *parent;
David Howells51708362009-02-27 14:03:03 -0800111
Eric W. Biedermanc61a2812012-12-28 18:58:39 -0800112 do {
113 parent = ns->parent;
114 proc_free_inum(ns->proc_inum);
115 kmem_cache_free(user_ns_cachep, ns);
116 ns = parent;
117 } while (atomic_dec_and_test(&parent->count));
David Howells51708362009-02-27 14:03:03 -0800118}
Michael Halcrow6a3fd922008-04-29 00:59:52 -0700119EXPORT_SYMBOL(free_user_ns);
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000120
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800121static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count)
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000122{
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800123 unsigned idx, extents;
124 u32 first, last, id2;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000125
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800126 id2 = id + count - 1;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000127
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800128 /* Find the matching extent */
129 extents = map->nr_extents;
130 smp_read_barrier_depends();
131 for (idx = 0; idx < extents; idx++) {
132 first = map->extent[idx].first;
133 last = first + map->extent[idx].count - 1;
134 if (id >= first && id <= last &&
135 (id2 >= first && id2 <= last))
136 break;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000137 }
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800138 /* Map the id or note failure */
139 if (idx < extents)
140 id = (id - first) + map->extent[idx].lower_first;
141 else
142 id = (u32) -1;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000143
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800144 return id;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000145}
146
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800147static u32 map_id_down(struct uid_gid_map *map, u32 id)
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000148{
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800149 unsigned idx, extents;
150 u32 first, last;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000151
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800152 /* Find the matching extent */
153 extents = map->nr_extents;
154 smp_read_barrier_depends();
155 for (idx = 0; idx < extents; idx++) {
156 first = map->extent[idx].first;
157 last = first + map->extent[idx].count - 1;
158 if (id >= first && id <= last)
159 break;
160 }
161 /* Map the id or note failure */
162 if (idx < extents)
163 id = (id - first) + map->extent[idx].lower_first;
164 else
165 id = (u32) -1;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000166
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800167 return id;
168}
169
170static u32 map_id_up(struct uid_gid_map *map, u32 id)
171{
172 unsigned idx, extents;
173 u32 first, last;
174
175 /* Find the matching extent */
176 extents = map->nr_extents;
177 smp_read_barrier_depends();
178 for (idx = 0; idx < extents; idx++) {
179 first = map->extent[idx].lower_first;
180 last = first + map->extent[idx].count - 1;
181 if (id >= first && id <= last)
182 break;
183 }
184 /* Map the id or note failure */
185 if (idx < extents)
186 id = (id - first) + map->extent[idx].first;
187 else
188 id = (u32) -1;
189
190 return id;
191}
192
193/**
194 * make_kuid - Map a user-namespace uid pair into a kuid.
195 * @ns: User namespace that the uid is in
196 * @uid: User identifier
197 *
198 * Maps a user-namespace uid pair into a kernel internal kuid,
199 * and returns that kuid.
200 *
201 * When there is no mapping defined for the user-namespace uid
202 * pair INVALID_UID is returned. Callers are expected to test
203 * for and handle handle INVALID_UID being returned. INVALID_UID
204 * may be tested for using uid_valid().
205 */
206kuid_t make_kuid(struct user_namespace *ns, uid_t uid)
207{
208 /* Map the uid to a global kernel uid */
209 return KUIDT_INIT(map_id_down(&ns->uid_map, uid));
210}
211EXPORT_SYMBOL(make_kuid);
212
213/**
214 * from_kuid - Create a uid from a kuid user-namespace pair.
215 * @targ: The user namespace we want a uid in.
216 * @kuid: The kernel internal uid to start with.
217 *
218 * Map @kuid into the user-namespace specified by @targ and
219 * return the resulting uid.
220 *
221 * There is always a mapping into the initial user_namespace.
222 *
223 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
224 */
225uid_t from_kuid(struct user_namespace *targ, kuid_t kuid)
226{
227 /* Map the uid from a global kernel uid */
228 return map_id_up(&targ->uid_map, __kuid_val(kuid));
229}
230EXPORT_SYMBOL(from_kuid);
231
232/**
233 * from_kuid_munged - Create a uid from a kuid user-namespace pair.
234 * @targ: The user namespace we want a uid in.
235 * @kuid: The kernel internal uid to start with.
236 *
237 * Map @kuid into the user-namespace specified by @targ and
238 * return the resulting uid.
239 *
240 * There is always a mapping into the initial user_namespace.
241 *
242 * Unlike from_kuid from_kuid_munged never fails and always
243 * returns a valid uid. This makes from_kuid_munged appropriate
244 * for use in syscalls like stat and getuid where failing the
245 * system call and failing to provide a valid uid are not an
246 * options.
247 *
248 * If @kuid has no mapping in @targ overflowuid is returned.
249 */
250uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid)
251{
252 uid_t uid;
253 uid = from_kuid(targ, kuid);
254
255 if (uid == (uid_t) -1)
256 uid = overflowuid;
257 return uid;
258}
259EXPORT_SYMBOL(from_kuid_munged);
260
261/**
262 * make_kgid - Map a user-namespace gid pair into a kgid.
263 * @ns: User namespace that the gid is in
264 * @uid: group identifier
265 *
266 * Maps a user-namespace gid pair into a kernel internal kgid,
267 * and returns that kgid.
268 *
269 * When there is no mapping defined for the user-namespace gid
270 * pair INVALID_GID is returned. Callers are expected to test
271 * for and handle INVALID_GID being returned. INVALID_GID may be
272 * tested for using gid_valid().
273 */
274kgid_t make_kgid(struct user_namespace *ns, gid_t gid)
275{
276 /* Map the gid to a global kernel gid */
277 return KGIDT_INIT(map_id_down(&ns->gid_map, gid));
278}
279EXPORT_SYMBOL(make_kgid);
280
281/**
282 * from_kgid - Create a gid from a kgid user-namespace pair.
283 * @targ: The user namespace we want a gid in.
284 * @kgid: The kernel internal gid to start with.
285 *
286 * Map @kgid into the user-namespace specified by @targ and
287 * return the resulting gid.
288 *
289 * There is always a mapping into the initial user_namespace.
290 *
291 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
292 */
293gid_t from_kgid(struct user_namespace *targ, kgid_t kgid)
294{
295 /* Map the gid from a global kernel gid */
296 return map_id_up(&targ->gid_map, __kgid_val(kgid));
297}
298EXPORT_SYMBOL(from_kgid);
299
300/**
301 * from_kgid_munged - Create a gid from a kgid user-namespace pair.
302 * @targ: The user namespace we want a gid in.
303 * @kgid: The kernel internal gid to start with.
304 *
305 * Map @kgid into the user-namespace specified by @targ and
306 * return the resulting gid.
307 *
308 * There is always a mapping into the initial user_namespace.
309 *
310 * Unlike from_kgid from_kgid_munged never fails and always
311 * returns a valid gid. This makes from_kgid_munged appropriate
312 * for use in syscalls like stat and getgid where failing the
313 * system call and failing to provide a valid gid are not options.
314 *
315 * If @kgid has no mapping in @targ overflowgid is returned.
316 */
317gid_t from_kgid_munged(struct user_namespace *targ, kgid_t kgid)
318{
319 gid_t gid;
320 gid = from_kgid(targ, kgid);
321
322 if (gid == (gid_t) -1)
323 gid = overflowgid;
324 return gid;
325}
326EXPORT_SYMBOL(from_kgid_munged);
327
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700328/**
329 * make_kprojid - Map a user-namespace projid pair into a kprojid.
330 * @ns: User namespace that the projid is in
331 * @projid: Project identifier
332 *
333 * Maps a user-namespace uid pair into a kernel internal kuid,
334 * and returns that kuid.
335 *
336 * When there is no mapping defined for the user-namespace projid
337 * pair INVALID_PROJID is returned. Callers are expected to test
338 * for and handle handle INVALID_PROJID being returned. INVALID_PROJID
339 * may be tested for using projid_valid().
340 */
341kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid)
342{
343 /* Map the uid to a global kernel uid */
344 return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid));
345}
346EXPORT_SYMBOL(make_kprojid);
347
348/**
349 * from_kprojid - Create a projid from a kprojid user-namespace pair.
350 * @targ: The user namespace we want a projid in.
351 * @kprojid: The kernel internal project identifier to start with.
352 *
353 * Map @kprojid into the user-namespace specified by @targ and
354 * return the resulting projid.
355 *
356 * There is always a mapping into the initial user_namespace.
357 *
358 * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
359 */
360projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid)
361{
362 /* Map the uid from a global kernel uid */
363 return map_id_up(&targ->projid_map, __kprojid_val(kprojid));
364}
365EXPORT_SYMBOL(from_kprojid);
366
367/**
368 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
369 * @targ: The user namespace we want a projid in.
370 * @kprojid: The kernel internal projid to start with.
371 *
372 * Map @kprojid into the user-namespace specified by @targ and
373 * return the resulting projid.
374 *
375 * There is always a mapping into the initial user_namespace.
376 *
377 * Unlike from_kprojid from_kprojid_munged never fails and always
378 * returns a valid projid. This makes from_kprojid_munged
379 * appropriate for use in syscalls like stat and where
380 * failing the system call and failing to provide a valid projid are
381 * not an options.
382 *
383 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
384 */
385projid_t from_kprojid_munged(struct user_namespace *targ, kprojid_t kprojid)
386{
387 projid_t projid;
388 projid = from_kprojid(targ, kprojid);
389
390 if (projid == (projid_t) -1)
391 projid = OVERFLOW_PROJID;
392 return projid;
393}
394EXPORT_SYMBOL(from_kprojid_munged);
395
396
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800397static int uid_m_show(struct seq_file *seq, void *v)
398{
399 struct user_namespace *ns = seq->private;
400 struct uid_gid_extent *extent = v;
401 struct user_namespace *lower_ns;
402 uid_t lower;
403
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700404 lower_ns = seq_user_ns(seq);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800405 if ((lower_ns == ns) && lower_ns->parent)
406 lower_ns = lower_ns->parent;
407
408 lower = from_kuid(lower_ns, KUIDT_INIT(extent->lower_first));
409
410 seq_printf(seq, "%10u %10u %10u\n",
411 extent->first,
412 lower,
413 extent->count);
414
415 return 0;
416}
417
418static int gid_m_show(struct seq_file *seq, void *v)
419{
420 struct user_namespace *ns = seq->private;
421 struct uid_gid_extent *extent = v;
422 struct user_namespace *lower_ns;
423 gid_t lower;
424
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700425 lower_ns = seq_user_ns(seq);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800426 if ((lower_ns == ns) && lower_ns->parent)
427 lower_ns = lower_ns->parent;
428
429 lower = from_kgid(lower_ns, KGIDT_INIT(extent->lower_first));
430
431 seq_printf(seq, "%10u %10u %10u\n",
432 extent->first,
433 lower,
434 extent->count);
435
436 return 0;
437}
438
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700439static int projid_m_show(struct seq_file *seq, void *v)
440{
441 struct user_namespace *ns = seq->private;
442 struct uid_gid_extent *extent = v;
443 struct user_namespace *lower_ns;
444 projid_t lower;
445
446 lower_ns = seq_user_ns(seq);
447 if ((lower_ns == ns) && lower_ns->parent)
448 lower_ns = lower_ns->parent;
449
450 lower = from_kprojid(lower_ns, KPROJIDT_INIT(extent->lower_first));
451
452 seq_printf(seq, "%10u %10u %10u\n",
453 extent->first,
454 lower,
455 extent->count);
456
457 return 0;
458}
459
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800460static void *m_start(struct seq_file *seq, loff_t *ppos, struct uid_gid_map *map)
461{
462 struct uid_gid_extent *extent = NULL;
463 loff_t pos = *ppos;
464
465 if (pos < map->nr_extents)
466 extent = &map->extent[pos];
467
468 return extent;
469}
470
471static void *uid_m_start(struct seq_file *seq, loff_t *ppos)
472{
473 struct user_namespace *ns = seq->private;
474
475 return m_start(seq, ppos, &ns->uid_map);
476}
477
478static void *gid_m_start(struct seq_file *seq, loff_t *ppos)
479{
480 struct user_namespace *ns = seq->private;
481
482 return m_start(seq, ppos, &ns->gid_map);
483}
484
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700485static void *projid_m_start(struct seq_file *seq, loff_t *ppos)
486{
487 struct user_namespace *ns = seq->private;
488
489 return m_start(seq, ppos, &ns->projid_map);
490}
491
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800492static void *m_next(struct seq_file *seq, void *v, loff_t *pos)
493{
494 (*pos)++;
495 return seq->op->start(seq, pos);
496}
497
498static void m_stop(struct seq_file *seq, void *v)
499{
500 return;
501}
502
503struct seq_operations proc_uid_seq_operations = {
504 .start = uid_m_start,
505 .stop = m_stop,
506 .next = m_next,
507 .show = uid_m_show,
508};
509
510struct seq_operations proc_gid_seq_operations = {
511 .start = gid_m_start,
512 .stop = m_stop,
513 .next = m_next,
514 .show = gid_m_show,
515};
516
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700517struct seq_operations proc_projid_seq_operations = {
518 .start = projid_m_start,
519 .stop = m_stop,
520 .next = m_next,
521 .show = projid_m_show,
522};
523
Eric W. Biederman0bd14b42012-12-27 22:27:29 -0800524static bool mappings_overlap(struct uid_gid_map *new_map, struct uid_gid_extent *extent)
525{
526 u32 upper_first, lower_first, upper_last, lower_last;
527 unsigned idx;
528
529 upper_first = extent->first;
530 lower_first = extent->lower_first;
531 upper_last = upper_first + extent->count - 1;
532 lower_last = lower_first + extent->count - 1;
533
534 for (idx = 0; idx < new_map->nr_extents; idx++) {
535 u32 prev_upper_first, prev_lower_first;
536 u32 prev_upper_last, prev_lower_last;
537 struct uid_gid_extent *prev;
538
539 prev = &new_map->extent[idx];
540
541 prev_upper_first = prev->first;
542 prev_lower_first = prev->lower_first;
543 prev_upper_last = prev_upper_first + prev->count - 1;
544 prev_lower_last = prev_lower_first + prev->count - 1;
545
546 /* Does the upper range intersect a previous extent? */
547 if ((prev_upper_first <= upper_last) &&
548 (prev_upper_last >= upper_first))
549 return true;
550
551 /* Does the lower range intersect a previous extent? */
552 if ((prev_lower_first <= lower_last) &&
553 (prev_lower_last >= lower_first))
554 return true;
555 }
556 return false;
557}
558
559
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800560static DEFINE_MUTEX(id_map_mutex);
561
562static ssize_t map_write(struct file *file, const char __user *buf,
563 size_t count, loff_t *ppos,
564 int cap_setid,
565 struct uid_gid_map *map,
566 struct uid_gid_map *parent_map)
567{
568 struct seq_file *seq = file->private_data;
569 struct user_namespace *ns = seq->private;
570 struct uid_gid_map new_map;
571 unsigned idx;
Eric W. Biederman0bd14b42012-12-27 22:27:29 -0800572 struct uid_gid_extent *extent = NULL;
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800573 unsigned long page = 0;
574 char *kbuf, *pos, *next_line;
575 ssize_t ret = -EINVAL;
576
577 /*
578 * The id_map_mutex serializes all writes to any given map.
579 *
580 * Any map is only ever written once.
581 *
582 * An id map fits within 1 cache line on most architectures.
583 *
584 * On read nothing needs to be done unless you are on an
585 * architecture with a crazy cache coherency model like alpha.
586 *
587 * There is a one time data dependency between reading the
588 * count of the extents and the values of the extents. The
589 * desired behavior is to see the values of the extents that
590 * were written before the count of the extents.
591 *
592 * To achieve this smp_wmb() is used on guarantee the write
593 * order and smp_read_barrier_depends() is guaranteed that we
594 * don't have crazy architectures returning stale data.
595 *
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000596 */
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800597 mutex_lock(&id_map_mutex);
598
599 ret = -EPERM;
600 /* Only allow one successful write to the map */
601 if (map->nr_extents != 0)
602 goto out;
603
604 /* Require the appropriate privilege CAP_SETUID or CAP_SETGID
605 * over the user namespace in order to set the id mapping.
606 */
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700607 if (cap_valid(cap_setid) && !ns_capable(ns, cap_setid))
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800608 goto out;
609
610 /* Get a buffer */
611 ret = -ENOMEM;
612 page = __get_free_page(GFP_TEMPORARY);
613 kbuf = (char *) page;
614 if (!page)
615 goto out;
616
617 /* Only allow <= page size writes at the beginning of the file */
618 ret = -EINVAL;
619 if ((*ppos != 0) || (count >= PAGE_SIZE))
620 goto out;
621
622 /* Slurp in the user data */
623 ret = -EFAULT;
624 if (copy_from_user(kbuf, buf, count))
625 goto out;
626 kbuf[count] = '\0';
627
628 /* Parse the user data */
629 ret = -EINVAL;
630 pos = kbuf;
631 new_map.nr_extents = 0;
632 for (;pos; pos = next_line) {
633 extent = &new_map.extent[new_map.nr_extents];
634
635 /* Find the end of line and ensure I don't look past it */
636 next_line = strchr(pos, '\n');
637 if (next_line) {
638 *next_line = '\0';
639 next_line++;
640 if (*next_line == '\0')
641 next_line = NULL;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000642 }
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800643
644 pos = skip_spaces(pos);
645 extent->first = simple_strtoul(pos, &pos, 10);
646 if (!isspace(*pos))
647 goto out;
648
649 pos = skip_spaces(pos);
650 extent->lower_first = simple_strtoul(pos, &pos, 10);
651 if (!isspace(*pos))
652 goto out;
653
654 pos = skip_spaces(pos);
655 extent->count = simple_strtoul(pos, &pos, 10);
656 if (*pos && !isspace(*pos))
657 goto out;
658
659 /* Verify there is not trailing junk on the line */
660 pos = skip_spaces(pos);
661 if (*pos != '\0')
662 goto out;
663
664 /* Verify we have been given valid starting values */
665 if ((extent->first == (u32) -1) ||
666 (extent->lower_first == (u32) -1 ))
667 goto out;
668
669 /* Verify count is not zero and does not cause the extent to wrap */
670 if ((extent->first + extent->count) <= extent->first)
671 goto out;
672 if ((extent->lower_first + extent->count) <= extent->lower_first)
673 goto out;
674
Eric W. Biederman0bd14b42012-12-27 22:27:29 -0800675 /* Do the ranges in extent overlap any previous extents? */
676 if (mappings_overlap(&new_map, extent))
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800677 goto out;
678
679 new_map.nr_extents++;
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800680
681 /* Fail if the file contains too many extents */
682 if ((new_map.nr_extents == UID_GID_MAP_MAX_EXTENTS) &&
683 (next_line != NULL))
684 goto out;
685 }
686 /* Be very certaint the new map actually exists */
687 if (new_map.nr_extents == 0)
688 goto out;
689
690 ret = -EPERM;
691 /* Validate the user is allowed to use user id's mapped to. */
692 if (!new_idmap_permitted(ns, cap_setid, &new_map))
693 goto out;
694
695 /* Map the lower ids from the parent user namespace to the
696 * kernel global id space.
697 */
698 for (idx = 0; idx < new_map.nr_extents; idx++) {
699 u32 lower_first;
700 extent = &new_map.extent[idx];
701
702 lower_first = map_id_range_down(parent_map,
703 extent->lower_first,
704 extent->count);
705
706 /* Fail if we can not map the specified extent to
707 * the kernel global id space.
708 */
709 if (lower_first == (u32) -1)
710 goto out;
711
712 extent->lower_first = lower_first;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000713 }
714
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800715 /* Install the map */
716 memcpy(map->extent, new_map.extent,
717 new_map.nr_extents*sizeof(new_map.extent[0]));
718 smp_wmb();
719 map->nr_extents = new_map.nr_extents;
720
721 *ppos = count;
722 ret = count;
723out:
724 mutex_unlock(&id_map_mutex);
725 if (page)
726 free_page(page);
727 return ret;
728}
729
730ssize_t proc_uid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
731{
732 struct seq_file *seq = file->private_data;
733 struct user_namespace *ns = seq->private;
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700734 struct user_namespace *seq_ns = seq_user_ns(seq);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800735
736 if (!ns->parent)
737 return -EPERM;
738
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700739 if ((seq_ns != ns) && (seq_ns != ns->parent))
740 return -EPERM;
741
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800742 return map_write(file, buf, size, ppos, CAP_SETUID,
743 &ns->uid_map, &ns->parent->uid_map);
744}
745
746ssize_t proc_gid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
747{
748 struct seq_file *seq = file->private_data;
749 struct user_namespace *ns = seq->private;
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700750 struct user_namespace *seq_ns = seq_user_ns(seq);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800751
752 if (!ns->parent)
753 return -EPERM;
754
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700755 if ((seq_ns != ns) && (seq_ns != ns->parent))
756 return -EPERM;
757
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800758 return map_write(file, buf, size, ppos, CAP_SETGID,
759 &ns->gid_map, &ns->parent->gid_map);
760}
761
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700762ssize_t proc_projid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
763{
764 struct seq_file *seq = file->private_data;
765 struct user_namespace *ns = seq->private;
766 struct user_namespace *seq_ns = seq_user_ns(seq);
767
768 if (!ns->parent)
769 return -EPERM;
770
771 if ((seq_ns != ns) && (seq_ns != ns->parent))
772 return -EPERM;
773
774 /* Anyone can set any valid project id no capability needed */
775 return map_write(file, buf, size, ppos, -1,
776 &ns->projid_map, &ns->parent->projid_map);
777}
778
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800779static bool new_idmap_permitted(struct user_namespace *ns, int cap_setid,
780 struct uid_gid_map *new_map)
781{
Eric W. Biederman37657da2012-07-27 06:21:27 -0700782 /* Allow mapping to your own filesystem ids */
783 if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1)) {
784 u32 id = new_map->extent[0].lower_first;
785 if (cap_setid == CAP_SETUID) {
786 kuid_t uid = make_kuid(ns->parent, id);
787 if (uid_eq(uid, current_fsuid()))
788 return true;
789 }
790 else if (cap_setid == CAP_SETGID) {
791 kgid_t gid = make_kgid(ns->parent, id);
792 if (gid_eq(gid, current_fsgid()))
793 return true;
794 }
795 }
796
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700797 /* Allow anyone to set a mapping that doesn't require privilege */
798 if (!cap_valid(cap_setid))
799 return true;
800
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800801 /* Allow the specified ids if we have the appropriate capability
802 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
803 */
804 if (ns_capable(ns->parent, cap_setid))
805 return true;
806
807 return false;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000808}
Pavel Emelyanov61642812011-01-12 17:00:46 -0800809
Eric W. Biedermancde19752012-07-26 06:24:06 -0700810static void *userns_get(struct task_struct *task)
811{
812 struct user_namespace *user_ns;
813
814 rcu_read_lock();
815 user_ns = get_user_ns(__task_cred(task)->user_ns);
816 rcu_read_unlock();
817
818 return user_ns;
819}
820
821static void userns_put(void *ns)
822{
823 put_user_ns(ns);
824}
825
826static int userns_install(struct nsproxy *nsproxy, void *ns)
827{
828 struct user_namespace *user_ns = ns;
829 struct cred *cred;
830
831 /* Don't allow gaining capabilities by reentering
832 * the same user namespace.
833 */
834 if (user_ns == current_user_ns())
835 return -EINVAL;
836
Eric W. Biederman51550402012-12-09 17:19:52 -0800837 /* Threaded processes may not enter a different user namespace */
Eric W. Biedermancde19752012-07-26 06:24:06 -0700838 if (atomic_read(&current->mm->mm_users) > 1)
839 return -EINVAL;
840
Eric W. Biedermane66eded2013-03-13 11:51:49 -0700841 if (current->fs->users != 1)
842 return -EINVAL;
843
Eric W. Biedermancde19752012-07-26 06:24:06 -0700844 if (!ns_capable(user_ns, CAP_SYS_ADMIN))
845 return -EPERM;
846
847 cred = prepare_creds();
848 if (!cred)
849 return -ENOMEM;
850
851 put_user_ns(cred->user_ns);
852 set_cred_user_ns(cred, get_user_ns(user_ns));
853
854 return commit_creds(cred);
855}
856
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700857static unsigned int userns_inum(void *ns)
858{
859 struct user_namespace *user_ns = ns;
860 return user_ns->proc_inum;
861}
862
Eric W. Biedermancde19752012-07-26 06:24:06 -0700863const struct proc_ns_operations userns_operations = {
864 .name = "user",
865 .type = CLONE_NEWUSER,
866 .get = userns_get,
867 .put = userns_put,
868 .install = userns_install,
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700869 .inum = userns_inum,
Eric W. Biedermancde19752012-07-26 06:24:06 -0700870};
871
Pavel Emelyanov61642812011-01-12 17:00:46 -0800872static __init int user_namespaces_init(void)
873{
874 user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
875 return 0;
876}
877module_init(user_namespaces_init);