blob: 0d9c51d67333f7a445afb8b936520a3bcd25dc33 [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
8#include <linux/module.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>
12
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070013/*
14 * Clone a new ns copying an original user ns, setting refcount to 1
15 * @old_ns: namespace to clone
16 * Return NULL on error (failure to kmalloc), new ns otherwise
17 */
18static struct user_namespace *clone_user_ns(struct user_namespace *old_ns)
19{
20 struct user_namespace *ns;
21 struct user_struct *new_user;
David Howellsd84f4f92008-11-14 10:39:23 +110022 struct cred *new;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070023 int n;
24
25 ns = kmalloc(sizeof(struct user_namespace), GFP_KERNEL);
26 if (!ns)
Cedric Le Goater467e9f42007-07-15 23:41:06 -070027 return ERR_PTR(-ENOMEM);
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070028
29 kref_init(&ns->kref);
30
31 for (n = 0; n < UIDHASH_SZ; ++n)
Pavel Emelyanov735de222007-09-18 22:46:44 -070032 INIT_HLIST_HEAD(ns->uidhash_table + n);
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070033
34 /* Insert new root user. */
35 ns->root_user = alloc_uid(ns, 0);
36 if (!ns->root_user) {
37 kfree(ns);
Cedric Le Goater467e9f42007-07-15 23:41:06 -070038 return ERR_PTR(-ENOMEM);
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070039 }
40
41 /* Reset current->user with a new one */
David Howells76aac0e2008-11-14 10:39:12 +110042 new_user = alloc_uid(ns, current_uid());
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070043 if (!new_user) {
44 free_uid(ns->root_user);
45 kfree(ns);
Cedric Le Goater467e9f42007-07-15 23:41:06 -070046 return ERR_PTR(-ENOMEM);
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070047 }
48
David Howellsd84f4f92008-11-14 10:39:23 +110049 /* Install the new user */
50 new = prepare_creds();
51 if (!new) {
52 free_uid(new_user);
53 free_uid(ns->root_user);
54 kfree(ns);
55 }
56 free_uid(new->user);
57 new->user = new_user;
58 commit_creds(new);
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070059 return ns;
60}
61
Cedric Le Goateracce2922007-07-15 23:40:59 -070062struct user_namespace * copy_user_ns(int flags, struct user_namespace *old_ns)
63{
64 struct user_namespace *new_ns;
65
66 BUG_ON(!old_ns);
67 get_user_ns(old_ns);
68
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070069 if (!(flags & CLONE_NEWUSER))
70 return old_ns;
71
72 new_ns = clone_user_ns(old_ns);
73
74 put_user_ns(old_ns);
Cedric Le Goateracce2922007-07-15 23:40:59 -070075 return new_ns;
76}
77
78void free_user_ns(struct kref *kref)
79{
80 struct user_namespace *ns;
81
82 ns = container_of(kref, struct user_namespace, kref);
Pavel Emelyanov28f300d2007-09-18 22:46:45 -070083 release_uids(ns);
Cedric Le Goateracce2922007-07-15 23:40:59 -070084 kfree(ns);
85}
Michael Halcrow6a3fd922008-04-29 00:59:52 -070086EXPORT_SYMBOL(free_user_ns);