.kref = {
.refcount = ATOMIC_INIT(3),
},
- .creator = &root_user,
+ .owner = GLOBAL_ROOT_UID,
+ .group = GLOBAL_ROOT_GID,
};
EXPORT_SYMBOL_GPL(init_user_ns);
*/
static DEFINE_SPINLOCK(uidhash_lock);
-/* root_user.__count is 2, 1 for init task cred, 1 for init_user_ns->user_ns */
+/* root_user.__count is 1, for init task cred */
struct user_struct root_user = {
- .__count = ATOMIC_INIT(2),
+ .__count = ATOMIC_INIT(1),
.processes = ATOMIC_INIT(1),
.files = ATOMIC_INIT(0),
.sigpending = ATOMIC_INIT(0),
{
struct user_namespace *ns, *parent_ns = new->user_ns;
struct user_struct *root_user;
+ kuid_t owner = make_kuid(new->user_ns, new->euid);
+ kgid_t group = make_kgid(new->user_ns, new->egid);
+
+ /* The creator needs a mapping in the parent user namespace
+ * or else we won't be able to reasonably tell userspace who
+ * created a user_namespace.
+ */
+ if (!kuid_has_mapping(parent_ns, owner) ||
+ !kgid_has_mapping(parent_ns, group))
+ return -EPERM;
ns = kmem_cache_alloc(user_ns_cachep, GFP_KERNEL);
if (!ns)
/* set the new root user in the credentials under preparation */
ns->parent = parent_ns;
- ns->creator = new->user;
+ ns->owner = owner;
+ ns->group = group;
+ free_uid(new->user);
new->user = root_user;
new->uid = new->euid = new->suid = new->fsuid = 0;
new->gid = new->egid = new->sgid = new->fsgid = 0;
#endif
/* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
- /* Leave the reference to our user_ns with the new cred */
+ /* Leave the new->user_ns reference with the new user namespace. */
+ /* Leave the reference to our user_ns with the new cred. */
new->user_ns = ns;
return 0;
}
-/*
- * Deferred destructor for a user namespace. This is required because
- * free_user_ns() may be called with uidhash_lock held, but we need to call
- * back to free_uid() which will want to take the lock again.
- */
-static void free_user_ns_work(struct work_struct *work)
+void free_user_ns(struct kref *kref)
{
struct user_namespace *parent, *ns =
- container_of(work, struct user_namespace, destroyer);
+ container_of(kref, struct user_namespace, kref);
+
parent = ns->parent;
- free_uid(ns->creator);
kmem_cache_free(user_ns_cachep, ns);
put_user_ns(parent);
}
-
-void free_user_ns(struct kref *kref)
-{
- struct user_namespace *ns =
- container_of(kref, struct user_namespace, kref);
-
- INIT_WORK(&ns->destroyer, free_user_ns_work);
- schedule_work(&ns->destroyer);
-}
EXPORT_SYMBOL(free_user_ns);
uid_t user_ns_map_uid(struct user_namespace *to, const struct cred *cred, uid_t uid)
if (likely(to == cred->user_ns))
return uid;
-
/* Is cred->user the creator of the target user_ns
* or the creator of one of it's parents?
*/
for ( tmp = to; tmp != &init_user_ns; tmp = tmp->parent ) {
- if (cred->user == tmp->creator) {
+ if (uid_eq(cred->user->uid, tmp->owner)) {
return (uid_t)0;
}
}
* or the creator of one of it's parents?
*/
for ( tmp = to; tmp != &init_user_ns; tmp = tmp->parent ) {
- if (cred->user == tmp->creator) {
+ if (uid_eq(cred->user->uid, tmp->owner)) {
return (gid_t)0;
}
}