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/user_namespace.h>
13 struct user_namespace init_user_ns
= {
15 .refcount
= ATOMIC_INIT(2),
17 .root_user
= &root_user
,
20 EXPORT_SYMBOL_GPL(init_user_ns
);
25 * Clone a new ns copying an original user ns, setting refcount to 1
26 * @old_ns: namespace to clone
27 * Return NULL on error (failure to kmalloc), new ns otherwise
29 static struct user_namespace
*clone_user_ns(struct user_namespace
*old_ns
)
31 struct user_namespace
*ns
;
32 struct user_struct
*new_user
;
35 ns
= kmalloc(sizeof(struct user_namespace
), GFP_KERNEL
);
37 return ERR_PTR(-ENOMEM
);
41 for (n
= 0; n
< UIDHASH_SZ
; ++n
)
42 INIT_HLIST_HEAD(ns
->uidhash_table
+ n
);
44 /* Insert new root user. */
45 ns
->root_user
= alloc_uid(ns
, 0);
48 return ERR_PTR(-ENOMEM
);
51 /* Reset current->user with a new one */
52 new_user
= alloc_uid(ns
, current
->uid
);
54 free_uid(ns
->root_user
);
56 return ERR_PTR(-ENOMEM
);
63 struct user_namespace
* copy_user_ns(int flags
, struct user_namespace
*old_ns
)
65 struct user_namespace
*new_ns
;
70 if (!(flags
& CLONE_NEWUSER
))
73 new_ns
= clone_user_ns(old_ns
);
79 void free_user_ns(struct kref
*kref
)
81 struct user_namespace
*ns
;
83 ns
= container_of(kref
, struct user_namespace
, kref
);
88 #endif /* CONFIG_USER_NS */