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
8 #include <linux/module.h>
9 #include <linux/version.h>
10 #include <linux/nsproxy.h>
11 #include <linux/slab.h>
12 #include <linux/user_namespace.h>
15 * Clone a new ns copying an original user ns, setting refcount to 1
16 * @old_ns: namespace to clone
17 * Return NULL on error (failure to kmalloc), new ns otherwise
19 static struct user_namespace
*clone_user_ns(struct user_namespace
*old_ns
)
21 struct user_namespace
*ns
;
22 struct user_struct
*new_user
;
25 ns
= kmalloc(sizeof(struct user_namespace
), GFP_KERNEL
);
27 return ERR_PTR(-ENOMEM
);
31 for (n
= 0; n
< UIDHASH_SZ
; ++n
)
32 INIT_HLIST_HEAD(ns
->uidhash_table
+ n
);
34 /* Insert new root user. */
35 ns
->root_user
= alloc_uid(ns
, 0);
38 return ERR_PTR(-ENOMEM
);
41 /* Reset current->user with a new one */
42 new_user
= alloc_uid(ns
, current
->uid
);
44 free_uid(ns
->root_user
);
46 return ERR_PTR(-ENOMEM
);
53 struct user_namespace
* copy_user_ns(int flags
, struct user_namespace
*old_ns
)
55 struct user_namespace
*new_ns
;
60 if (!(flags
& CLONE_NEWUSER
))
63 new_ns
= clone_user_ns(old_ns
);
69 void free_user_ns(struct kref
*kref
)
71 struct user_namespace
*ns
;
73 ns
= container_of(kref
, struct user_namespace
, kref
);
77 EXPORT_SYMBOL(free_user_ns
);