2006-10-02 16:18:14 +07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2004 IBM Corporation
|
|
|
|
*
|
|
|
|
* Author: Serge Hallyn <serue@us.ibm.com>
|
|
|
|
*
|
|
|
|
* 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 01:51:41 +07:00
|
|
|
#include <linux/export.h>
|
2006-10-02 16:18:14 +07:00
|
|
|
#include <linux/uts.h>
|
|
|
|
#include <linux/utsname.h>
|
2007-07-16 13:41:06 +07:00
|
|
|
#include <linux/err.h>
|
2008-04-29 14:59:25 +07:00
|
|
|
#include <linux/slab.h>
|
userns: add a user_namespace as creator/owner of uts_namespace
The expected course of development for user namespaces targeted
capabilities is laid out at https://wiki.ubuntu.com/UserNamespace.
Goals:
- Make it safe for an unprivileged user to unshare namespaces. They
will be privileged with respect to the new namespace, but this should
only include resources which the unprivileged user already owns.
- Provide separate limits and accounting for userids in different
namespaces.
Status:
Currently (as of 2.6.38) you can clone with the CLONE_NEWUSER flag to
get a new user namespace if you have the CAP_SYS_ADMIN, CAP_SETUID, and
CAP_SETGID capabilities. What this gets you is a whole new set of
userids, meaning that user 500 will have a different 'struct user' in
your namespace than in other namespaces. So any accounting information
stored in struct user will be unique to your namespace.
However, throughout the kernel there are checks which
- simply check for a capability. Since root in a child namespace
has all capabilities, this means that a child namespace is not
constrained.
- simply compare uid1 == uid2. Since these are the integer uids,
uid 500 in namespace 1 will be said to be equal to uid 500 in
namespace 2.
As a result, the lxc implementation at lxc.sf.net does not use user
namespaces. This is actually helpful because it leaves us free to
develop user namespaces in such a way that, for some time, user
namespaces may be unuseful.
Bugs aside, this patchset is supposed to not at all affect systems which
are not actively using user namespaces, and only restrict what tasks in
child user namespace can do. They begin to limit privilege to a user
namespace, so that root in a container cannot kill or ptrace tasks in the
parent user namespace, and can only get world access rights to files.
Since all files currently belong to the initila user namespace, that means
that child user namespaces can only get world access rights to *all*
files. While this temporarily makes user namespaces bad for system
containers, it starts to get useful for some sandboxing.
I've run the 'runltplite.sh' with and without this patchset and found no
difference.
This patch:
copy_process() handles CLONE_NEWUSER before the rest of the namespaces.
So in the case of clone(CLONE_NEWUSER|CLONE_NEWUTS) the new uts namespace
will have the new user namespace as its owner. That is what we want,
since we want root in that new userns to be able to have privilege over
it.
Changelog:
Feb 15: don't set uts_ns->user_ns if we didn't create
a new uts_ns.
Feb 23: Move extern init_user_ns declaration from
init/version.c to utsname.h.
Signed-off-by: Serge E. Hallyn <serge.hallyn@canonical.com>
Acked-by: "Eric W. Biederman" <ebiederm@xmission.com>
Acked-by: Daniel Lezcano <daniel.lezcano@free.fr>
Acked-by: David Howells <dhowells@redhat.com>
Cc: James Morris <jmorris@namei.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-03-24 06:43:16 +07:00
|
|
|
#include <linux/user_namespace.h>
|
2013-04-12 07:50:06 +07:00
|
|
|
#include <linux/proc_ns.h>
|
2006-10-02 16:18:14 +07:00
|
|
|
|
2009-06-18 06:27:54 +07:00
|
|
|
static struct uts_namespace *create_uts_ns(void)
|
|
|
|
{
|
|
|
|
struct uts_namespace *uts_ns;
|
|
|
|
|
|
|
|
uts_ns = kmalloc(sizeof(struct uts_namespace), GFP_KERNEL);
|
|
|
|
if (uts_ns)
|
|
|
|
kref_init(&uts_ns->kref);
|
|
|
|
return uts_ns;
|
|
|
|
}
|
|
|
|
|
2006-10-02 16:18:17 +07:00
|
|
|
/*
|
|
|
|
* Clone a new ns copying an original utsname, setting refcount to 1
|
|
|
|
* @old_ns: namespace to clone
|
2013-02-28 08:05:30 +07:00
|
|
|
* Return ERR_PTR(-ENOMEM) on error (failure to kmalloc), new ns otherwise
|
2006-10-02 16:18:17 +07:00
|
|
|
*/
|
2012-07-26 18:02:49 +07:00
|
|
|
static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns,
|
2011-03-24 06:43:18 +07:00
|
|
|
struct uts_namespace *old_ns)
|
2006-10-02 16:18:17 +07:00
|
|
|
{
|
|
|
|
struct uts_namespace *ns;
|
2011-06-16 00:21:48 +07:00
|
|
|
int err;
|
2006-10-02 16:18:17 +07:00
|
|
|
|
2009-06-18 06:27:54 +07:00
|
|
|
ns = create_uts_ns();
|
2007-07-16 13:41:06 +07:00
|
|
|
if (!ns)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
2011-06-16 00:21:48 +07:00
|
|
|
err = proc_alloc_inum(&ns->proc_inum);
|
|
|
|
if (err) {
|
|
|
|
kfree(ns);
|
|
|
|
return ERR_PTR(err);
|
|
|
|
}
|
|
|
|
|
2007-09-19 12:46:27 +07:00
|
|
|
down_read(&uts_sem);
|
2007-07-16 13:41:06 +07:00
|
|
|
memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
|
2012-07-26 18:02:49 +07:00
|
|
|
ns->user_ns = get_user_ns(user_ns);
|
2007-09-19 12:46:27 +07:00
|
|
|
up_read(&uts_sem);
|
2006-10-02 16:18:17 +07:00
|
|
|
return ns;
|
|
|
|
}
|
|
|
|
|
2006-10-02 16:18:14 +07:00
|
|
|
/*
|
|
|
|
* Copy task tsk's utsname namespace, or clone it if flags
|
|
|
|
* specifies CLONE_NEWUTS. In latter case, changes to the
|
|
|
|
* utsname of this process won't be seen by parent, and vice
|
|
|
|
* versa.
|
|
|
|
*/
|
2011-03-24 06:43:18 +07:00
|
|
|
struct uts_namespace *copy_utsname(unsigned long flags,
|
2012-07-26 18:02:49 +07:00
|
|
|
struct user_namespace *user_ns, struct uts_namespace *old_ns)
|
2006-10-02 16:18:14 +07:00
|
|
|
{
|
2006-10-02 16:18:17 +07:00
|
|
|
struct uts_namespace *new_ns;
|
2006-10-02 16:18:14 +07:00
|
|
|
|
2007-05-08 14:25:21 +07:00
|
|
|
BUG_ON(!old_ns);
|
2006-10-02 16:18:14 +07:00
|
|
|
get_uts_ns(old_ns);
|
|
|
|
|
2006-10-02 16:18:17 +07:00
|
|
|
if (!(flags & CLONE_NEWUTS))
|
2007-05-08 14:25:21 +07:00
|
|
|
return old_ns;
|
2006-10-02 16:18:17 +07:00
|
|
|
|
2012-07-26 18:02:49 +07:00
|
|
|
new_ns = clone_uts_ns(user_ns, old_ns);
|
2006-10-02 16:18:17 +07:00
|
|
|
|
|
|
|
put_uts_ns(old_ns);
|
2007-05-08 14:25:21 +07:00
|
|
|
return new_ns;
|
2006-10-02 16:18:14 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
void free_uts_ns(struct kref *kref)
|
|
|
|
{
|
|
|
|
struct uts_namespace *ns;
|
|
|
|
|
|
|
|
ns = container_of(kref, struct uts_namespace, kref);
|
userns: add a user_namespace as creator/owner of uts_namespace
The expected course of development for user namespaces targeted
capabilities is laid out at https://wiki.ubuntu.com/UserNamespace.
Goals:
- Make it safe for an unprivileged user to unshare namespaces. They
will be privileged with respect to the new namespace, but this should
only include resources which the unprivileged user already owns.
- Provide separate limits and accounting for userids in different
namespaces.
Status:
Currently (as of 2.6.38) you can clone with the CLONE_NEWUSER flag to
get a new user namespace if you have the CAP_SYS_ADMIN, CAP_SETUID, and
CAP_SETGID capabilities. What this gets you is a whole new set of
userids, meaning that user 500 will have a different 'struct user' in
your namespace than in other namespaces. So any accounting information
stored in struct user will be unique to your namespace.
However, throughout the kernel there are checks which
- simply check for a capability. Since root in a child namespace
has all capabilities, this means that a child namespace is not
constrained.
- simply compare uid1 == uid2. Since these are the integer uids,
uid 500 in namespace 1 will be said to be equal to uid 500 in
namespace 2.
As a result, the lxc implementation at lxc.sf.net does not use user
namespaces. This is actually helpful because it leaves us free to
develop user namespaces in such a way that, for some time, user
namespaces may be unuseful.
Bugs aside, this patchset is supposed to not at all affect systems which
are not actively using user namespaces, and only restrict what tasks in
child user namespace can do. They begin to limit privilege to a user
namespace, so that root in a container cannot kill or ptrace tasks in the
parent user namespace, and can only get world access rights to files.
Since all files currently belong to the initila user namespace, that means
that child user namespaces can only get world access rights to *all*
files. While this temporarily makes user namespaces bad for system
containers, it starts to get useful for some sandboxing.
I've run the 'runltplite.sh' with and without this patchset and found no
difference.
This patch:
copy_process() handles CLONE_NEWUSER before the rest of the namespaces.
So in the case of clone(CLONE_NEWUSER|CLONE_NEWUTS) the new uts namespace
will have the new user namespace as its owner. That is what we want,
since we want root in that new userns to be able to have privilege over
it.
Changelog:
Feb 15: don't set uts_ns->user_ns if we didn't create
a new uts_ns.
Feb 23: Move extern init_user_ns declaration from
init/version.c to utsname.h.
Signed-off-by: Serge E. Hallyn <serge.hallyn@canonical.com>
Acked-by: "Eric W. Biederman" <ebiederm@xmission.com>
Acked-by: Daniel Lezcano <daniel.lezcano@free.fr>
Acked-by: David Howells <dhowells@redhat.com>
Cc: James Morris <jmorris@namei.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-03-24 06:43:16 +07:00
|
|
|
put_user_ns(ns->user_ns);
|
2011-06-16 00:21:48 +07:00
|
|
|
proc_free_inum(ns->proc_inum);
|
2006-10-02 16:18:14 +07:00
|
|
|
kfree(ns);
|
|
|
|
}
|
2010-03-08 09:43:27 +07:00
|
|
|
|
|
|
|
static void *utsns_get(struct task_struct *task)
|
|
|
|
{
|
|
|
|
struct uts_namespace *ns = NULL;
|
|
|
|
struct nsproxy *nsproxy;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
nsproxy = task_nsproxy(task);
|
|
|
|
if (nsproxy) {
|
|
|
|
ns = nsproxy->uts_ns;
|
|
|
|
get_uts_ns(ns);
|
|
|
|
}
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
return ns;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void utsns_put(void *ns)
|
|
|
|
{
|
|
|
|
put_uts_ns(ns);
|
|
|
|
}
|
|
|
|
|
2012-07-26 15:13:20 +07:00
|
|
|
static int utsns_install(struct nsproxy *nsproxy, void *new)
|
2010-03-08 09:43:27 +07:00
|
|
|
{
|
2012-07-26 15:13:20 +07:00
|
|
|
struct uts_namespace *ns = new;
|
|
|
|
|
2012-12-14 22:55:36 +07:00
|
|
|
if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
|
2013-03-21 02:49:49 +07:00
|
|
|
!ns_capable(current_user_ns(), CAP_SYS_ADMIN))
|
2012-07-26 15:13:20 +07:00
|
|
|
return -EPERM;
|
|
|
|
|
2010-03-08 09:43:27 +07:00
|
|
|
get_uts_ns(ns);
|
|
|
|
put_uts_ns(nsproxy->uts_ns);
|
|
|
|
nsproxy->uts_ns = ns;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-16 00:21:48 +07:00
|
|
|
static unsigned int utsns_inum(void *vp)
|
|
|
|
{
|
|
|
|
struct uts_namespace *ns = vp;
|
|
|
|
|
|
|
|
return ns->proc_inum;
|
|
|
|
}
|
|
|
|
|
2010-03-08 09:43:27 +07:00
|
|
|
const struct proc_ns_operations utsns_operations = {
|
|
|
|
.name = "uts",
|
|
|
|
.type = CLONE_NEWUTS,
|
|
|
|
.get = utsns_get,
|
|
|
|
.put = utsns_put,
|
|
|
|
.install = utsns_install,
|
2011-06-16 00:21:48 +07:00
|
|
|
.inum = utsns_inum,
|
2010-03-08 09:43:27 +07:00
|
|
|
};
|