blob: 5247cdb24e6235a6bdd609e43644fb5815ab400a [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;
Eric W. Biedermanf0d62ae2014-12-09 14:03:14 -060027static DEFINE_MUTEX(userns_state_mutex);
Pavel Emelyanov61642812011-01-12 17:00:46 -080028
Eric W. Biederman67080752013-04-14 13:47:02 -070029static bool new_idmap_permitted(const struct file *file,
30 struct user_namespace *ns, int cap_setid,
Eric W. Biederman22d917d2011-11-17 00:11:58 -080031 struct uid_gid_map *map);
Eric W. Biedermanb0321322016-07-30 13:53:37 -050032static void free_user_ns(struct work_struct *work);
Eric W. Biederman22d917d2011-11-17 00:11:58 -080033
Eric W. Biedermancde19752012-07-26 06:24:06 -070034static void set_cred_user_ns(struct cred *cred, struct user_namespace *user_ns)
35{
36 /* Start with the same capabilities as init but useless for doing
37 * anything as the capabilities are bound to the new user namespace.
38 */
39 cred->securebits = SECUREBITS_DEFAULT;
40 cred->cap_inheritable = CAP_EMPTY_SET;
41 cred->cap_permitted = CAP_FULL_SET;
42 cred->cap_effective = CAP_FULL_SET;
Andy Lutomirski58319052015-09-04 15:42:45 -070043 cred->cap_ambient = CAP_EMPTY_SET;
Eric W. Biedermancde19752012-07-26 06:24:06 -070044 cred->cap_bset = CAP_FULL_SET;
45#ifdef CONFIG_KEYS
46 key_put(cred->request_key_auth);
47 cred->request_key_auth = NULL;
48#endif
49 /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
50 cred->user_ns = user_ns;
51}
52
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070053/*
Serge Hallyn18b6e042008-10-15 16:38:45 -050054 * Create a new user namespace, deriving the creator from the user in the
55 * passed credentials, and replacing that user with the new root user for the
56 * new namespace.
57 *
58 * This is called by copy_creds(), which will finish setting the target task's
59 * credentials.
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070060 */
Serge Hallyn18b6e042008-10-15 16:38:45 -050061int create_user_ns(struct cred *new)
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070062{
Eric W. Biederman0093ccb2011-11-16 21:52:53 -080063 struct user_namespace *ns, *parent_ns = new->user_ns;
Eric W. Biederman078de5f2012-02-08 07:00:08 -080064 kuid_t owner = new->euid;
65 kgid_t group = new->egid;
Eric W. Biederman98f842e2011-06-15 10:21:48 -070066 int ret;
Eric W. Biederman783291e2011-11-17 01:32:59 -080067
Oleg Nesterov8742f222013-08-08 18:55:32 +020068 if (parent_ns->level > 32)
69 return -EUSERS;
70
Eric W. Biederman31515272013-03-15 01:45:51 -070071 /*
72 * Verify that we can not violate the policy of which files
73 * may be accessed that is specified by the root directory,
74 * by verifing that the root directory is at the root of the
75 * mount namespace which allows all files to be accessed.
76 */
77 if (current_chrooted())
78 return -EPERM;
79
Eric W. Biederman783291e2011-11-17 01:32:59 -080080 /* The creator needs a mapping in the parent user namespace
81 * or else we won't be able to reasonably tell userspace who
82 * created a user_namespace.
83 */
84 if (!kuid_has_mapping(parent_ns, owner) ||
85 !kgid_has_mapping(parent_ns, group))
86 return -EPERM;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070087
Eric W. Biederman22d917d2011-11-17 00:11:58 -080088 ns = kmem_cache_zalloc(user_ns_cachep, GFP_KERNEL);
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070089 if (!ns)
Serge Hallyn18b6e042008-10-15 16:38:45 -050090 return -ENOMEM;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070091
Al Viro6344c432014-11-01 00:45:45 -040092 ret = ns_alloc_inum(&ns->ns);
Eric W. Biederman98f842e2011-06-15 10:21:48 -070093 if (ret) {
94 kmem_cache_free(user_ns_cachep, ns);
95 return ret;
96 }
Al Viro33c42942014-11-01 02:32:53 -040097 ns->ns.ops = &userns_operations;
Eric W. Biederman98f842e2011-06-15 10:21:48 -070098
Eric W. Biedermanc61a2812012-12-28 18:58:39 -080099 atomic_set(&ns->count, 1);
Eric W. Biedermancde19752012-07-26 06:24:06 -0700100 /* Leave the new->user_ns reference with the new user namespace. */
Eric W. Biedermanaeb3ae92011-11-16 21:59:43 -0800101 ns->parent = parent_ns;
Oleg Nesterov8742f222013-08-08 18:55:32 +0200102 ns->level = parent_ns->level + 1;
Eric W. Biederman783291e2011-11-17 01:32:59 -0800103 ns->owner = owner;
104 ns->group = group;
Eric W. Biedermanb0321322016-07-30 13:53:37 -0500105 INIT_WORK(&ns->work, free_user_ns);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800106
Eric W. Biederman9cc46512014-12-02 12:27:26 -0600107 /* Inherit USERNS_SETGROUPS_ALLOWED from our parent */
108 mutex_lock(&userns_state_mutex);
109 ns->flags = parent_ns->flags;
110 mutex_unlock(&userns_state_mutex);
111
Eric W. Biedermancde19752012-07-26 06:24:06 -0700112 set_cred_user_ns(new, ns);
Eric W. Biederman0093ccb2011-11-16 21:52:53 -0800113
David Howellsf36f8c72013-09-24 10:35:19 +0100114#ifdef CONFIG_PERSISTENT_KEYRINGS
115 init_rwsem(&ns->persistent_keyring_register_sem);
116#endif
Serge Hallyn18b6e042008-10-15 16:38:45 -0500117 return 0;
Cedric Le Goateracce2922007-07-15 23:40:59 -0700118}
119
Eric W. Biedermanb2e0d9872012-07-26 05:15:35 -0700120int unshare_userns(unsigned long unshare_flags, struct cred **new_cred)
121{
122 struct cred *cred;
Oleg Nesterov61609682013-08-06 19:38:55 +0200123 int err = -ENOMEM;
Eric W. Biedermanb2e0d9872012-07-26 05:15:35 -0700124
125 if (!(unshare_flags & CLONE_NEWUSER))
126 return 0;
127
128 cred = prepare_creds();
Oleg Nesterov61609682013-08-06 19:38:55 +0200129 if (cred) {
130 err = create_user_ns(cred);
131 if (err)
132 put_cred(cred);
133 else
134 *new_cred = cred;
135 }
Eric W. Biedermanb2e0d9872012-07-26 05:15:35 -0700136
Oleg Nesterov61609682013-08-06 19:38:55 +0200137 return err;
Eric W. Biedermanb2e0d9872012-07-26 05:15:35 -0700138}
139
Eric W. Biedermanb0321322016-07-30 13:53:37 -0500140static void free_user_ns(struct work_struct *work)
David Howells51708362009-02-27 14:03:03 -0800141{
Eric W. Biedermanb0321322016-07-30 13:53:37 -0500142 struct user_namespace *parent, *ns =
143 container_of(work, struct user_namespace, work);
David Howells51708362009-02-27 14:03:03 -0800144
Eric W. Biedermanc61a2812012-12-28 18:58:39 -0800145 do {
146 parent = ns->parent;
David Howellsf36f8c72013-09-24 10:35:19 +0100147#ifdef CONFIG_PERSISTENT_KEYRINGS
148 key_put(ns->persistent_keyring_register);
149#endif
Al Viro6344c432014-11-01 00:45:45 -0400150 ns_free_inum(&ns->ns);
Eric W. Biedermanc61a2812012-12-28 18:58:39 -0800151 kmem_cache_free(user_ns_cachep, ns);
152 ns = parent;
153 } while (atomic_dec_and_test(&parent->count));
David Howells51708362009-02-27 14:03:03 -0800154}
Eric W. Biedermanb0321322016-07-30 13:53:37 -0500155
156void __put_user_ns(struct user_namespace *ns)
157{
158 schedule_work(&ns->work);
159}
160EXPORT_SYMBOL(__put_user_ns);
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000161
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800162static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count)
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000163{
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800164 unsigned idx, extents;
165 u32 first, last, id2;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000166
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800167 id2 = id + count - 1;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000168
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800169 /* Find the matching extent */
170 extents = map->nr_extents;
Mikulas Patockae79323b2014-04-14 16:58:55 -0400171 smp_rmb();
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800172 for (idx = 0; idx < extents; idx++) {
173 first = map->extent[idx].first;
174 last = first + map->extent[idx].count - 1;
175 if (id >= first && id <= last &&
176 (id2 >= first && id2 <= last))
177 break;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000178 }
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800179 /* Map the id or note failure */
180 if (idx < extents)
181 id = (id - first) + map->extent[idx].lower_first;
182 else
183 id = (u32) -1;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000184
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800185 return id;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000186}
187
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800188static u32 map_id_down(struct uid_gid_map *map, u32 id)
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000189{
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800190 unsigned idx, extents;
191 u32 first, last;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000192
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800193 /* Find the matching extent */
194 extents = map->nr_extents;
Mikulas Patockae79323b2014-04-14 16:58:55 -0400195 smp_rmb();
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800196 for (idx = 0; idx < extents; idx++) {
197 first = map->extent[idx].first;
198 last = first + map->extent[idx].count - 1;
199 if (id >= first && id <= last)
200 break;
201 }
202 /* Map the id or note failure */
203 if (idx < extents)
204 id = (id - first) + map->extent[idx].lower_first;
205 else
206 id = (u32) -1;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000207
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800208 return id;
209}
210
211static u32 map_id_up(struct uid_gid_map *map, u32 id)
212{
213 unsigned idx, extents;
214 u32 first, last;
215
216 /* Find the matching extent */
217 extents = map->nr_extents;
Mikulas Patockae79323b2014-04-14 16:58:55 -0400218 smp_rmb();
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800219 for (idx = 0; idx < extents; idx++) {
220 first = map->extent[idx].lower_first;
221 last = first + map->extent[idx].count - 1;
222 if (id >= first && id <= last)
223 break;
224 }
225 /* Map the id or note failure */
226 if (idx < extents)
227 id = (id - first) + map->extent[idx].first;
228 else
229 id = (u32) -1;
230
231 return id;
232}
233
234/**
235 * make_kuid - Map a user-namespace uid pair into a kuid.
236 * @ns: User namespace that the uid is in
237 * @uid: User identifier
238 *
239 * Maps a user-namespace uid pair into a kernel internal kuid,
240 * and returns that kuid.
241 *
242 * When there is no mapping defined for the user-namespace uid
243 * pair INVALID_UID is returned. Callers are expected to test
Brian Campbellb080e042014-02-16 22:58:12 -0500244 * for and handle INVALID_UID being returned. INVALID_UID
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800245 * may be tested for using uid_valid().
246 */
247kuid_t make_kuid(struct user_namespace *ns, uid_t uid)
248{
249 /* Map the uid to a global kernel uid */
250 return KUIDT_INIT(map_id_down(&ns->uid_map, uid));
251}
252EXPORT_SYMBOL(make_kuid);
253
254/**
255 * from_kuid - Create a uid from a kuid user-namespace pair.
256 * @targ: The user namespace we want a uid in.
257 * @kuid: The kernel internal uid to start with.
258 *
259 * Map @kuid into the user-namespace specified by @targ and
260 * return the resulting uid.
261 *
262 * There is always a mapping into the initial user_namespace.
263 *
264 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
265 */
266uid_t from_kuid(struct user_namespace *targ, kuid_t kuid)
267{
268 /* Map the uid from a global kernel uid */
269 return map_id_up(&targ->uid_map, __kuid_val(kuid));
270}
271EXPORT_SYMBOL(from_kuid);
272
273/**
274 * from_kuid_munged - Create a uid from a kuid user-namespace pair.
275 * @targ: The user namespace we want a uid in.
276 * @kuid: The kernel internal uid to start with.
277 *
278 * Map @kuid into the user-namespace specified by @targ and
279 * return the resulting uid.
280 *
281 * There is always a mapping into the initial user_namespace.
282 *
283 * Unlike from_kuid from_kuid_munged never fails and always
284 * returns a valid uid. This makes from_kuid_munged appropriate
285 * for use in syscalls like stat and getuid where failing the
286 * system call and failing to provide a valid uid are not an
287 * options.
288 *
289 * If @kuid has no mapping in @targ overflowuid is returned.
290 */
291uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid)
292{
293 uid_t uid;
294 uid = from_kuid(targ, kuid);
295
296 if (uid == (uid_t) -1)
297 uid = overflowuid;
298 return uid;
299}
300EXPORT_SYMBOL(from_kuid_munged);
301
302/**
303 * make_kgid - Map a user-namespace gid pair into a kgid.
304 * @ns: User namespace that the gid is in
Fabian Frederick68a9a432014-06-06 14:37:21 -0700305 * @gid: group identifier
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800306 *
307 * Maps a user-namespace gid pair into a kernel internal kgid,
308 * and returns that kgid.
309 *
310 * When there is no mapping defined for the user-namespace gid
311 * pair INVALID_GID is returned. Callers are expected to test
312 * for and handle INVALID_GID being returned. INVALID_GID may be
313 * tested for using gid_valid().
314 */
315kgid_t make_kgid(struct user_namespace *ns, gid_t gid)
316{
317 /* Map the gid to a global kernel gid */
318 return KGIDT_INIT(map_id_down(&ns->gid_map, gid));
319}
320EXPORT_SYMBOL(make_kgid);
321
322/**
323 * from_kgid - Create a gid from a kgid user-namespace pair.
324 * @targ: The user namespace we want a gid in.
325 * @kgid: The kernel internal gid to start with.
326 *
327 * Map @kgid into the user-namespace specified by @targ and
328 * return the resulting gid.
329 *
330 * There is always a mapping into the initial user_namespace.
331 *
332 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
333 */
334gid_t from_kgid(struct user_namespace *targ, kgid_t kgid)
335{
336 /* Map the gid from a global kernel gid */
337 return map_id_up(&targ->gid_map, __kgid_val(kgid));
338}
339EXPORT_SYMBOL(from_kgid);
340
341/**
342 * from_kgid_munged - Create a gid from a kgid user-namespace pair.
343 * @targ: The user namespace we want a gid in.
344 * @kgid: The kernel internal gid to start with.
345 *
346 * Map @kgid into the user-namespace specified by @targ and
347 * return the resulting gid.
348 *
349 * There is always a mapping into the initial user_namespace.
350 *
351 * Unlike from_kgid from_kgid_munged never fails and always
352 * returns a valid gid. This makes from_kgid_munged appropriate
353 * for use in syscalls like stat and getgid where failing the
354 * system call and failing to provide a valid gid are not options.
355 *
356 * If @kgid has no mapping in @targ overflowgid is returned.
357 */
358gid_t from_kgid_munged(struct user_namespace *targ, kgid_t kgid)
359{
360 gid_t gid;
361 gid = from_kgid(targ, kgid);
362
363 if (gid == (gid_t) -1)
364 gid = overflowgid;
365 return gid;
366}
367EXPORT_SYMBOL(from_kgid_munged);
368
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700369/**
370 * make_kprojid - Map a user-namespace projid pair into a kprojid.
371 * @ns: User namespace that the projid is in
372 * @projid: Project identifier
373 *
374 * Maps a user-namespace uid pair into a kernel internal kuid,
375 * and returns that kuid.
376 *
377 * When there is no mapping defined for the user-namespace projid
378 * pair INVALID_PROJID is returned. Callers are expected to test
379 * for and handle handle INVALID_PROJID being returned. INVALID_PROJID
380 * may be tested for using projid_valid().
381 */
382kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid)
383{
384 /* Map the uid to a global kernel uid */
385 return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid));
386}
387EXPORT_SYMBOL(make_kprojid);
388
389/**
390 * from_kprojid - Create a projid from a kprojid user-namespace pair.
391 * @targ: The user namespace we want a projid in.
392 * @kprojid: The kernel internal project identifier to start with.
393 *
394 * Map @kprojid into the user-namespace specified by @targ and
395 * return the resulting projid.
396 *
397 * There is always a mapping into the initial user_namespace.
398 *
399 * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
400 */
401projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid)
402{
403 /* Map the uid from a global kernel uid */
404 return map_id_up(&targ->projid_map, __kprojid_val(kprojid));
405}
406EXPORT_SYMBOL(from_kprojid);
407
408/**
409 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
410 * @targ: The user namespace we want a projid in.
411 * @kprojid: The kernel internal projid to start with.
412 *
413 * Map @kprojid into the user-namespace specified by @targ and
414 * return the resulting projid.
415 *
416 * There is always a mapping into the initial user_namespace.
417 *
418 * Unlike from_kprojid from_kprojid_munged never fails and always
419 * returns a valid projid. This makes from_kprojid_munged
420 * appropriate for use in syscalls like stat and where
421 * failing the system call and failing to provide a valid projid are
422 * not an options.
423 *
424 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
425 */
426projid_t from_kprojid_munged(struct user_namespace *targ, kprojid_t kprojid)
427{
428 projid_t projid;
429 projid = from_kprojid(targ, kprojid);
430
431 if (projid == (projid_t) -1)
432 projid = OVERFLOW_PROJID;
433 return projid;
434}
435EXPORT_SYMBOL(from_kprojid_munged);
436
437
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800438static int uid_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 uid_t lower;
444
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700445 lower_ns = seq_user_ns(seq);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800446 if ((lower_ns == ns) && lower_ns->parent)
447 lower_ns = lower_ns->parent;
448
449 lower = from_kuid(lower_ns, KUIDT_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
459static int gid_m_show(struct seq_file *seq, void *v)
460{
461 struct user_namespace *ns = seq->private;
462 struct uid_gid_extent *extent = v;
463 struct user_namespace *lower_ns;
464 gid_t lower;
465
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700466 lower_ns = seq_user_ns(seq);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800467 if ((lower_ns == ns) && lower_ns->parent)
468 lower_ns = lower_ns->parent;
469
470 lower = from_kgid(lower_ns, KGIDT_INIT(extent->lower_first));
471
472 seq_printf(seq, "%10u %10u %10u\n",
473 extent->first,
474 lower,
475 extent->count);
476
477 return 0;
478}
479
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700480static int projid_m_show(struct seq_file *seq, void *v)
481{
482 struct user_namespace *ns = seq->private;
483 struct uid_gid_extent *extent = v;
484 struct user_namespace *lower_ns;
485 projid_t lower;
486
487 lower_ns = seq_user_ns(seq);
488 if ((lower_ns == ns) && lower_ns->parent)
489 lower_ns = lower_ns->parent;
490
491 lower = from_kprojid(lower_ns, KPROJIDT_INIT(extent->lower_first));
492
493 seq_printf(seq, "%10u %10u %10u\n",
494 extent->first,
495 lower,
496 extent->count);
497
498 return 0;
499}
500
Fabian Frederick68a9a432014-06-06 14:37:21 -0700501static void *m_start(struct seq_file *seq, loff_t *ppos,
502 struct uid_gid_map *map)
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800503{
504 struct uid_gid_extent *extent = NULL;
505 loff_t pos = *ppos;
506
507 if (pos < map->nr_extents)
508 extent = &map->extent[pos];
509
510 return extent;
511}
512
513static void *uid_m_start(struct seq_file *seq, loff_t *ppos)
514{
515 struct user_namespace *ns = seq->private;
516
517 return m_start(seq, ppos, &ns->uid_map);
518}
519
520static void *gid_m_start(struct seq_file *seq, loff_t *ppos)
521{
522 struct user_namespace *ns = seq->private;
523
524 return m_start(seq, ppos, &ns->gid_map);
525}
526
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700527static void *projid_m_start(struct seq_file *seq, loff_t *ppos)
528{
529 struct user_namespace *ns = seq->private;
530
531 return m_start(seq, ppos, &ns->projid_map);
532}
533
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800534static void *m_next(struct seq_file *seq, void *v, loff_t *pos)
535{
536 (*pos)++;
537 return seq->op->start(seq, pos);
538}
539
540static void m_stop(struct seq_file *seq, void *v)
541{
542 return;
543}
544
Fabian Frederickccf94f12014-08-08 14:21:22 -0700545const struct seq_operations proc_uid_seq_operations = {
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800546 .start = uid_m_start,
547 .stop = m_stop,
548 .next = m_next,
549 .show = uid_m_show,
550};
551
Fabian Frederickccf94f12014-08-08 14:21:22 -0700552const struct seq_operations proc_gid_seq_operations = {
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800553 .start = gid_m_start,
554 .stop = m_stop,
555 .next = m_next,
556 .show = gid_m_show,
557};
558
Fabian Frederickccf94f12014-08-08 14:21:22 -0700559const struct seq_operations proc_projid_seq_operations = {
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700560 .start = projid_m_start,
561 .stop = m_stop,
562 .next = m_next,
563 .show = projid_m_show,
564};
565
Fabian Frederick68a9a432014-06-06 14:37:21 -0700566static bool mappings_overlap(struct uid_gid_map *new_map,
567 struct uid_gid_extent *extent)
Eric W. Biederman0bd14b42012-12-27 22:27:29 -0800568{
569 u32 upper_first, lower_first, upper_last, lower_last;
570 unsigned idx;
571
572 upper_first = extent->first;
573 lower_first = extent->lower_first;
574 upper_last = upper_first + extent->count - 1;
575 lower_last = lower_first + extent->count - 1;
576
577 for (idx = 0; idx < new_map->nr_extents; idx++) {
578 u32 prev_upper_first, prev_lower_first;
579 u32 prev_upper_last, prev_lower_last;
580 struct uid_gid_extent *prev;
581
582 prev = &new_map->extent[idx];
583
584 prev_upper_first = prev->first;
585 prev_lower_first = prev->lower_first;
586 prev_upper_last = prev_upper_first + prev->count - 1;
587 prev_lower_last = prev_lower_first + prev->count - 1;
588
589 /* Does the upper range intersect a previous extent? */
590 if ((prev_upper_first <= upper_last) &&
591 (prev_upper_last >= upper_first))
592 return true;
593
594 /* Does the lower range intersect a previous extent? */
595 if ((prev_lower_first <= lower_last) &&
596 (prev_lower_last >= lower_first))
597 return true;
598 }
599 return false;
600}
601
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800602static ssize_t map_write(struct file *file, const char __user *buf,
603 size_t count, loff_t *ppos,
604 int cap_setid,
605 struct uid_gid_map *map,
606 struct uid_gid_map *parent_map)
607{
608 struct seq_file *seq = file->private_data;
609 struct user_namespace *ns = seq->private;
610 struct uid_gid_map new_map;
611 unsigned idx;
Eric W. Biederman0bd14b42012-12-27 22:27:29 -0800612 struct uid_gid_extent *extent = NULL;
Al Viro70f6cbb2015-12-24 00:13:10 -0500613 char *kbuf = NULL, *pos, *next_line;
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800614 ssize_t ret = -EINVAL;
615
616 /*
Eric W. Biedermanf0d62ae2014-12-09 14:03:14 -0600617 * The userns_state_mutex serializes all writes to any given map.
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800618 *
619 * Any map is only ever written once.
620 *
621 * An id map fits within 1 cache line on most architectures.
622 *
623 * On read nothing needs to be done unless you are on an
624 * architecture with a crazy cache coherency model like alpha.
625 *
626 * There is a one time data dependency between reading the
627 * count of the extents and the values of the extents. The
628 * desired behavior is to see the values of the extents that
629 * were written before the count of the extents.
630 *
631 * To achieve this smp_wmb() is used on guarantee the write
Mikulas Patockae79323b2014-04-14 16:58:55 -0400632 * order and smp_rmb() is guaranteed that we don't have crazy
633 * architectures returning stale data.
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000634 */
Eric W. Biedermanf0d62ae2014-12-09 14:03:14 -0600635 mutex_lock(&userns_state_mutex);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800636
637 ret = -EPERM;
638 /* Only allow one successful write to the map */
639 if (map->nr_extents != 0)
640 goto out;
641
Andy Lutomirski41c21e32013-04-14 11:44:04 -0700642 /*
643 * Adjusting namespace settings requires capabilities on the target.
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800644 */
Andy Lutomirski41c21e32013-04-14 11:44:04 -0700645 if (cap_valid(cap_setid) && !file_ns_capable(file, ns, CAP_SYS_ADMIN))
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800646 goto out;
647
Eric W. Biederman36476be2014-12-05 20:03:28 -0600648 /* Only allow < page size writes at the beginning of the file */
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800649 ret = -EINVAL;
650 if ((*ppos != 0) || (count >= PAGE_SIZE))
651 goto out;
652
653 /* Slurp in the user data */
Al Viro70f6cbb2015-12-24 00:13:10 -0500654 kbuf = memdup_user_nul(buf, count);
655 if (IS_ERR(kbuf)) {
656 ret = PTR_ERR(kbuf);
657 kbuf = NULL;
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800658 goto out;
Al Viro70f6cbb2015-12-24 00:13:10 -0500659 }
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800660
661 /* Parse the user data */
662 ret = -EINVAL;
663 pos = kbuf;
664 new_map.nr_extents = 0;
Fabian Frederick68a9a432014-06-06 14:37:21 -0700665 for (; pos; pos = next_line) {
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800666 extent = &new_map.extent[new_map.nr_extents];
667
668 /* Find the end of line and ensure I don't look past it */
669 next_line = strchr(pos, '\n');
670 if (next_line) {
671 *next_line = '\0';
672 next_line++;
673 if (*next_line == '\0')
674 next_line = NULL;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000675 }
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800676
677 pos = skip_spaces(pos);
678 extent->first = simple_strtoul(pos, &pos, 10);
679 if (!isspace(*pos))
680 goto out;
681
682 pos = skip_spaces(pos);
683 extent->lower_first = simple_strtoul(pos, &pos, 10);
684 if (!isspace(*pos))
685 goto out;
686
687 pos = skip_spaces(pos);
688 extent->count = simple_strtoul(pos, &pos, 10);
689 if (*pos && !isspace(*pos))
690 goto out;
691
692 /* Verify there is not trailing junk on the line */
693 pos = skip_spaces(pos);
694 if (*pos != '\0')
695 goto out;
696
697 /* Verify we have been given valid starting values */
698 if ((extent->first == (u32) -1) ||
Fabian Frederick68a9a432014-06-06 14:37:21 -0700699 (extent->lower_first == (u32) -1))
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800700 goto out;
701
Fabian Frederick68a9a432014-06-06 14:37:21 -0700702 /* Verify count is not zero and does not cause the
703 * extent to wrap
704 */
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800705 if ((extent->first + extent->count) <= extent->first)
706 goto out;
Fabian Frederick68a9a432014-06-06 14:37:21 -0700707 if ((extent->lower_first + extent->count) <=
708 extent->lower_first)
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800709 goto out;
710
Eric W. Biederman0bd14b42012-12-27 22:27:29 -0800711 /* Do the ranges in extent overlap any previous extents? */
712 if (mappings_overlap(&new_map, extent))
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800713 goto out;
714
715 new_map.nr_extents++;
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800716
717 /* Fail if the file contains too many extents */
718 if ((new_map.nr_extents == UID_GID_MAP_MAX_EXTENTS) &&
719 (next_line != NULL))
720 goto out;
721 }
722 /* Be very certaint the new map actually exists */
723 if (new_map.nr_extents == 0)
724 goto out;
725
726 ret = -EPERM;
727 /* Validate the user is allowed to use user id's mapped to. */
Eric W. Biederman67080752013-04-14 13:47:02 -0700728 if (!new_idmap_permitted(file, ns, cap_setid, &new_map))
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800729 goto out;
730
731 /* Map the lower ids from the parent user namespace to the
732 * kernel global id space.
733 */
734 for (idx = 0; idx < new_map.nr_extents; idx++) {
735 u32 lower_first;
736 extent = &new_map.extent[idx];
737
738 lower_first = map_id_range_down(parent_map,
739 extent->lower_first,
740 extent->count);
741
742 /* Fail if we can not map the specified extent to
743 * the kernel global id space.
744 */
745 if (lower_first == (u32) -1)
746 goto out;
747
748 extent->lower_first = lower_first;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000749 }
750
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800751 /* Install the map */
752 memcpy(map->extent, new_map.extent,
753 new_map.nr_extents*sizeof(new_map.extent[0]));
754 smp_wmb();
755 map->nr_extents = new_map.nr_extents;
756
757 *ppos = count;
758 ret = count;
759out:
Eric W. Biedermanf0d62ae2014-12-09 14:03:14 -0600760 mutex_unlock(&userns_state_mutex);
Al Viro70f6cbb2015-12-24 00:13:10 -0500761 kfree(kbuf);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800762 return ret;
763}
764
Fabian Frederick68a9a432014-06-06 14:37:21 -0700765ssize_t proc_uid_map_write(struct file *file, const char __user *buf,
766 size_t size, loff_t *ppos)
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800767{
768 struct seq_file *seq = file->private_data;
769 struct user_namespace *ns = seq->private;
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700770 struct user_namespace *seq_ns = seq_user_ns(seq);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800771
772 if (!ns->parent)
773 return -EPERM;
774
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700775 if ((seq_ns != ns) && (seq_ns != ns->parent))
776 return -EPERM;
777
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800778 return map_write(file, buf, size, ppos, CAP_SETUID,
779 &ns->uid_map, &ns->parent->uid_map);
780}
781
Fabian Frederick68a9a432014-06-06 14:37:21 -0700782ssize_t proc_gid_map_write(struct file *file, const char __user *buf,
783 size_t size, loff_t *ppos)
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800784{
785 struct seq_file *seq = file->private_data;
786 struct user_namespace *ns = seq->private;
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700787 struct user_namespace *seq_ns = seq_user_ns(seq);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800788
789 if (!ns->parent)
790 return -EPERM;
791
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700792 if ((seq_ns != ns) && (seq_ns != ns->parent))
793 return -EPERM;
794
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800795 return map_write(file, buf, size, ppos, CAP_SETGID,
796 &ns->gid_map, &ns->parent->gid_map);
797}
798
Fabian Frederick68a9a432014-06-06 14:37:21 -0700799ssize_t proc_projid_map_write(struct file *file, const char __user *buf,
800 size_t size, loff_t *ppos)
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700801{
802 struct seq_file *seq = file->private_data;
803 struct user_namespace *ns = seq->private;
804 struct user_namespace *seq_ns = seq_user_ns(seq);
805
806 if (!ns->parent)
807 return -EPERM;
808
809 if ((seq_ns != ns) && (seq_ns != ns->parent))
810 return -EPERM;
811
812 /* Anyone can set any valid project id no capability needed */
813 return map_write(file, buf, size, ppos, -1,
814 &ns->projid_map, &ns->parent->projid_map);
815}
816
Fabian Frederick68a9a432014-06-06 14:37:21 -0700817static bool new_idmap_permitted(const struct file *file,
Eric W. Biederman67080752013-04-14 13:47:02 -0700818 struct user_namespace *ns, int cap_setid,
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800819 struct uid_gid_map *new_map)
820{
Eric W. Biedermanf95d7912014-11-26 23:22:14 -0600821 const struct cred *cred = file->f_cred;
Eric W. Biederman0542f172014-12-05 17:51:47 -0600822 /* Don't allow mappings that would allow anything that wouldn't
823 * be allowed without the establishment of unprivileged mappings.
824 */
Eric W. Biedermanf95d7912014-11-26 23:22:14 -0600825 if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1) &&
826 uid_eq(ns->owner, cred->euid)) {
Eric W. Biederman37657da2012-07-27 06:21:27 -0700827 u32 id = new_map->extent[0].lower_first;
828 if (cap_setid == CAP_SETUID) {
829 kuid_t uid = make_kuid(ns->parent, id);
Eric W. Biedermanf95d7912014-11-26 23:22:14 -0600830 if (uid_eq(uid, cred->euid))
Eric W. Biederman37657da2012-07-27 06:21:27 -0700831 return true;
Fabian Frederick68a9a432014-06-06 14:37:21 -0700832 } else if (cap_setid == CAP_SETGID) {
Eric W. Biederman37657da2012-07-27 06:21:27 -0700833 kgid_t gid = make_kgid(ns->parent, id);
Eric W. Biederman66d2f332014-12-05 19:36:04 -0600834 if (!(ns->flags & USERNS_SETGROUPS_ALLOWED) &&
835 gid_eq(gid, cred->egid))
Eric W. Biederman37657da2012-07-27 06:21:27 -0700836 return true;
837 }
838 }
839
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700840 /* Allow anyone to set a mapping that doesn't require privilege */
841 if (!cap_valid(cap_setid))
842 return true;
843
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800844 /* Allow the specified ids if we have the appropriate capability
845 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
Eric W. Biederman67080752013-04-14 13:47:02 -0700846 * And the opener of the id file also had the approprpiate capability.
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800847 */
Eric W. Biederman67080752013-04-14 13:47:02 -0700848 if (ns_capable(ns->parent, cap_setid) &&
849 file_ns_capable(file, ns->parent, cap_setid))
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800850 return true;
851
852 return false;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000853}
Pavel Emelyanov61642812011-01-12 17:00:46 -0800854
Eric W. Biederman9cc46512014-12-02 12:27:26 -0600855int proc_setgroups_show(struct seq_file *seq, void *v)
856{
857 struct user_namespace *ns = seq->private;
858 unsigned long userns_flags = ACCESS_ONCE(ns->flags);
859
860 seq_printf(seq, "%s\n",
861 (userns_flags & USERNS_SETGROUPS_ALLOWED) ?
862 "allow" : "deny");
863 return 0;
864}
865
866ssize_t proc_setgroups_write(struct file *file, const char __user *buf,
867 size_t count, loff_t *ppos)
868{
869 struct seq_file *seq = file->private_data;
870 struct user_namespace *ns = seq->private;
871 char kbuf[8], *pos;
872 bool setgroups_allowed;
873 ssize_t ret;
874
875 /* Only allow a very narrow range of strings to be written */
876 ret = -EINVAL;
877 if ((*ppos != 0) || (count >= sizeof(kbuf)))
878 goto out;
879
880 /* What was written? */
881 ret = -EFAULT;
882 if (copy_from_user(kbuf, buf, count))
883 goto out;
884 kbuf[count] = '\0';
885 pos = kbuf;
886
887 /* What is being requested? */
888 ret = -EINVAL;
889 if (strncmp(pos, "allow", 5) == 0) {
890 pos += 5;
891 setgroups_allowed = true;
892 }
893 else if (strncmp(pos, "deny", 4) == 0) {
894 pos += 4;
895 setgroups_allowed = false;
896 }
897 else
898 goto out;
899
900 /* Verify there is not trailing junk on the line */
901 pos = skip_spaces(pos);
902 if (*pos != '\0')
903 goto out;
904
905 ret = -EPERM;
906 mutex_lock(&userns_state_mutex);
907 if (setgroups_allowed) {
908 /* Enabling setgroups after setgroups has been disabled
909 * is not allowed.
910 */
911 if (!(ns->flags & USERNS_SETGROUPS_ALLOWED))
912 goto out_unlock;
913 } else {
914 /* Permanently disabling setgroups after setgroups has
915 * been enabled by writing the gid_map is not allowed.
916 */
917 if (ns->gid_map.nr_extents != 0)
918 goto out_unlock;
919 ns->flags &= ~USERNS_SETGROUPS_ALLOWED;
920 }
921 mutex_unlock(&userns_state_mutex);
922
923 /* Report a successful write */
924 *ppos = count;
925 ret = count;
926out:
927 return ret;
928out_unlock:
929 mutex_unlock(&userns_state_mutex);
930 goto out;
931}
932
Eric W. Biederman273d2c62014-12-05 18:01:11 -0600933bool userns_may_setgroups(const struct user_namespace *ns)
934{
935 bool allowed;
936
Eric W. Biedermanf0d62ae2014-12-09 14:03:14 -0600937 mutex_lock(&userns_state_mutex);
Eric W. Biederman273d2c62014-12-05 18:01:11 -0600938 /* It is not safe to use setgroups until a gid mapping in
939 * the user namespace has been established.
940 */
941 allowed = ns->gid_map.nr_extents != 0;
Eric W. Biederman9cc46512014-12-02 12:27:26 -0600942 /* Is setgroups allowed? */
943 allowed = allowed && (ns->flags & USERNS_SETGROUPS_ALLOWED);
Eric W. Biedermanf0d62ae2014-12-09 14:03:14 -0600944 mutex_unlock(&userns_state_mutex);
Eric W. Biederman273d2c62014-12-05 18:01:11 -0600945
946 return allowed;
947}
948
Seth Forsheed07b8462015-09-23 15:16:04 -0500949/*
950 * Returns true if @ns is the same namespace as or a descendant of
951 * @target_ns.
952 */
953bool current_in_userns(const struct user_namespace *target_ns)
954{
955 struct user_namespace *ns;
956 for (ns = current_user_ns(); ns; ns = ns->parent) {
957 if (ns == target_ns)
958 return true;
959 }
960 return false;
961}
962
Al Viro3c041182014-11-01 00:25:30 -0400963static inline struct user_namespace *to_user_ns(struct ns_common *ns)
964{
965 return container_of(ns, struct user_namespace, ns);
966}
967
Al Viro64964522014-11-01 00:37:32 -0400968static struct ns_common *userns_get(struct task_struct *task)
Eric W. Biedermancde19752012-07-26 06:24:06 -0700969{
970 struct user_namespace *user_ns;
971
972 rcu_read_lock();
973 user_ns = get_user_ns(__task_cred(task)->user_ns);
974 rcu_read_unlock();
975
Al Viro3c041182014-11-01 00:25:30 -0400976 return user_ns ? &user_ns->ns : NULL;
Eric W. Biedermancde19752012-07-26 06:24:06 -0700977}
978
Al Viro64964522014-11-01 00:37:32 -0400979static void userns_put(struct ns_common *ns)
Eric W. Biedermancde19752012-07-26 06:24:06 -0700980{
Al Viro3c041182014-11-01 00:25:30 -0400981 put_user_ns(to_user_ns(ns));
Eric W. Biedermancde19752012-07-26 06:24:06 -0700982}
983
Al Viro64964522014-11-01 00:37:32 -0400984static int userns_install(struct nsproxy *nsproxy, struct ns_common *ns)
Eric W. Biedermancde19752012-07-26 06:24:06 -0700985{
Al Viro3c041182014-11-01 00:25:30 -0400986 struct user_namespace *user_ns = to_user_ns(ns);
Eric W. Biedermancde19752012-07-26 06:24:06 -0700987 struct cred *cred;
988
989 /* Don't allow gaining capabilities by reentering
990 * the same user namespace.
991 */
992 if (user_ns == current_user_ns())
993 return -EINVAL;
994
Eric W. Biedermanfaf00da2015-08-10 18:25:44 -0500995 /* Tasks that share a thread group must share a user namespace */
996 if (!thread_group_empty(current))
Eric W. Biedermancde19752012-07-26 06:24:06 -0700997 return -EINVAL;
998
Eric W. Biedermane66eded2013-03-13 11:51:49 -0700999 if (current->fs->users != 1)
1000 return -EINVAL;
1001
Eric W. Biedermancde19752012-07-26 06:24:06 -07001002 if (!ns_capable(user_ns, CAP_SYS_ADMIN))
1003 return -EPERM;
1004
1005 cred = prepare_creds();
1006 if (!cred)
1007 return -ENOMEM;
1008
1009 put_user_ns(cred->user_ns);
1010 set_cred_user_ns(cred, get_user_ns(user_ns));
1011
1012 return commit_creds(cred);
1013}
1014
1015const struct proc_ns_operations userns_operations = {
1016 .name = "user",
1017 .type = CLONE_NEWUSER,
1018 .get = userns_get,
1019 .put = userns_put,
1020 .install = userns_install,
1021};
1022
Pavel Emelyanov61642812011-01-12 17:00:46 -08001023static __init int user_namespaces_init(void)
1024{
1025 user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
1026 return 0;
1027}
Paul Gortmakerc96d6662014-04-03 14:48:35 -07001028subsys_initcall(user_namespaces_init);