2007-07-16 14:40:59 +08:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation, version 2 of the
|
|
|
|
* License.
|
|
|
|
*/
|
|
|
|
|
2011-05-24 02:51:41 +08:00
|
|
|
#include <linux/export.h>
|
2007-07-16 14:40:59 +08:00
|
|
|
#include <linux/nsproxy.h>
|
2008-04-29 15:59:25 +08:00
|
|
|
#include <linux/slab.h>
|
2007-07-16 14:40:59 +08:00
|
|
|
#include <linux/user_namespace.h>
|
2010-06-13 11:28:03 +08:00
|
|
|
#include <linux/highuid.h>
|
2008-10-16 05:38:45 +08:00
|
|
|
#include <linux/cred.h>
|
2011-11-17 17:59:07 +08:00
|
|
|
#include <linux/securebits.h>
|
2007-07-16 14:40:59 +08:00
|
|
|
|
2011-01-13 09:00:46 +08:00
|
|
|
static struct kmem_cache *user_ns_cachep __read_mostly;
|
|
|
|
|
2007-07-16 14:41:01 +08:00
|
|
|
/*
|
2008-10-16 05:38:45 +08:00
|
|
|
* Create a new user namespace, deriving the creator from the user in the
|
|
|
|
* passed credentials, and replacing that user with the new root user for the
|
|
|
|
* new namespace.
|
|
|
|
*
|
|
|
|
* This is called by copy_creds(), which will finish setting the target task's
|
|
|
|
* credentials.
|
2007-07-16 14:41:01 +08:00
|
|
|
*/
|
2008-10-16 05:38:45 +08:00
|
|
|
int create_user_ns(struct cred *new)
|
2007-07-16 14:41:01 +08:00
|
|
|
{
|
2011-11-17 13:52:53 +08:00
|
|
|
struct user_namespace *ns, *parent_ns = new->user_ns;
|
2008-10-16 05:38:45 +08:00
|
|
|
struct user_struct *root_user;
|
2011-11-17 17:32:59 +08:00
|
|
|
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;
|
2007-07-16 14:41:01 +08:00
|
|
|
|
2011-01-13 09:00:46 +08:00
|
|
|
ns = kmem_cache_alloc(user_ns_cachep, GFP_KERNEL);
|
2007-07-16 14:41:01 +08:00
|
|
|
if (!ns)
|
2008-10-16 05:38:45 +08:00
|
|
|
return -ENOMEM;
|
2007-07-16 14:41:01 +08:00
|
|
|
|
|
|
|
kref_init(&ns->kref);
|
|
|
|
|
2008-10-16 05:38:45 +08:00
|
|
|
/* Alloc new root user. */
|
2011-11-17 15:20:58 +08:00
|
|
|
root_user = alloc_uid(make_kuid(ns, 0));
|
2008-10-16 05:38:45 +08:00
|
|
|
if (!root_user) {
|
2011-01-13 09:00:46 +08:00
|
|
|
kmem_cache_free(user_ns_cachep, ns);
|
2008-10-16 05:38:45 +08:00
|
|
|
return -ENOMEM;
|
2007-07-16 14:41:01 +08:00
|
|
|
}
|
|
|
|
|
2008-10-16 05:38:45 +08:00
|
|
|
/* set the new root user in the credentials under preparation */
|
2011-11-17 13:59:43 +08:00
|
|
|
ns->parent = parent_ns;
|
2011-11-17 17:32:59 +08:00
|
|
|
ns->owner = owner;
|
|
|
|
ns->group = group;
|
|
|
|
free_uid(new->user);
|
2008-10-16 05:38:45 +08:00
|
|
|
new->user = root_user;
|
|
|
|
new->uid = new->euid = new->suid = new->fsuid = 0;
|
|
|
|
new->gid = new->egid = new->sgid = new->fsgid = 0;
|
|
|
|
put_group_info(new->group_info);
|
|
|
|
new->group_info = get_group_info(&init_groups);
|
2011-11-17 17:59:07 +08:00
|
|
|
/* Start with the same capabilities as init but useless for doing
|
|
|
|
* anything as the capabilities are bound to the new user namespace.
|
|
|
|
*/
|
|
|
|
new->securebits = SECUREBITS_DEFAULT;
|
|
|
|
new->cap_inheritable = CAP_EMPTY_SET;
|
|
|
|
new->cap_permitted = CAP_FULL_SET;
|
|
|
|
new->cap_effective = CAP_FULL_SET;
|
|
|
|
new->cap_bset = CAP_FULL_SET;
|
2008-10-16 05:38:45 +08:00
|
|
|
#ifdef CONFIG_KEYS
|
|
|
|
key_put(new->request_key_auth);
|
|
|
|
new->request_key_auth = NULL;
|
|
|
|
#endif
|
|
|
|
/* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
|
2007-07-16 14:41:01 +08:00
|
|
|
|
2011-11-17 17:32:59 +08:00
|
|
|
/* Leave the new->user_ns reference with the new user namespace. */
|
|
|
|
/* Leave the reference to our user_ns with the new cred. */
|
2011-11-17 13:52:53 +08:00
|
|
|
new->user_ns = ns;
|
|
|
|
|
2008-10-16 05:38:45 +08:00
|
|
|
return 0;
|
2007-07-16 14:40:59 +08:00
|
|
|
}
|
|
|
|
|
2011-11-17 17:32:59 +08:00
|
|
|
void free_user_ns(struct kref *kref)
|
2007-07-16 14:40:59 +08:00
|
|
|
{
|
2011-11-17 13:59:43 +08:00
|
|
|
struct user_namespace *parent, *ns =
|
2011-11-17 17:32:59 +08:00
|
|
|
container_of(kref, struct user_namespace, kref);
|
|
|
|
|
2011-11-17 13:59:43 +08:00
|
|
|
parent = ns->parent;
|
2011-01-13 09:00:46 +08:00
|
|
|
kmem_cache_free(user_ns_cachep, ns);
|
2011-11-17 13:59:43 +08:00
|
|
|
put_user_ns(parent);
|
2007-07-16 14:40:59 +08:00
|
|
|
}
|
2008-04-29 15:59:52 +08:00
|
|
|
EXPORT_SYMBOL(free_user_ns);
|
2010-06-13 11:28:03 +08:00
|
|
|
|
|
|
|
uid_t user_ns_map_uid(struct user_namespace *to, const struct cred *cred, uid_t uid)
|
|
|
|
{
|
|
|
|
struct user_namespace *tmp;
|
|
|
|
|
2011-11-17 15:15:31 +08:00
|
|
|
if (likely(to == cred->user_ns))
|
2010-06-13 11:28:03 +08:00
|
|
|
return uid;
|
|
|
|
|
|
|
|
/* Is cred->user the creator of the target user_ns
|
|
|
|
* or the creator of one of it's parents?
|
|
|
|
*/
|
2011-11-17 13:59:43 +08:00
|
|
|
for ( tmp = to; tmp != &init_user_ns; tmp = tmp->parent ) {
|
2011-11-17 17:32:59 +08:00
|
|
|
if (uid_eq(cred->user->uid, tmp->owner)) {
|
2010-06-13 11:28:03 +08:00
|
|
|
return (uid_t)0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No useful relationship so no mapping */
|
|
|
|
return overflowuid;
|
|
|
|
}
|
|
|
|
|
|
|
|
gid_t user_ns_map_gid(struct user_namespace *to, const struct cred *cred, gid_t gid)
|
|
|
|
{
|
|
|
|
struct user_namespace *tmp;
|
|
|
|
|
2011-11-17 15:15:31 +08:00
|
|
|
if (likely(to == cred->user_ns))
|
2010-06-13 11:28:03 +08:00
|
|
|
return gid;
|
|
|
|
|
|
|
|
/* Is cred->user the creator of the target user_ns
|
|
|
|
* or the creator of one of it's parents?
|
|
|
|
*/
|
2011-11-17 13:59:43 +08:00
|
|
|
for ( tmp = to; tmp != &init_user_ns; tmp = tmp->parent ) {
|
2011-11-17 17:32:59 +08:00
|
|
|
if (uid_eq(cred->user->uid, tmp->owner)) {
|
2010-06-13 11:28:03 +08:00
|
|
|
return (gid_t)0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No useful relationship so no mapping */
|
|
|
|
return overflowgid;
|
|
|
|
}
|
2011-01-13 09:00:46 +08:00
|
|
|
|
|
|
|
static __init int user_namespaces_init(void)
|
|
|
|
{
|
|
|
|
user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
module_init(user_namespaces_init);
|