2006-10-02 03:18:14 -06: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-23 12:51:41 -06:00
|
|
|
#include <linux/export.h>
|
2006-10-02 03:18:14 -06:00
|
|
|
#include <linux/uts.h>
|
|
|
|
#include <linux/utsname.h>
|
2007-07-16 00:41:06 -06:00
|
|
|
#include <linux/err.h>
|
2008-04-29 01:59:25 -06:00
|
|
|
#include <linux/slab.h>
|
2017-02-02 09:54:15 -07:00
|
|
|
#include <linux/cred.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-23 17:43:16 -06:00
|
|
|
#include <linux/user_namespace.h>
|
2013-04-11 18:50:06 -06:00
|
|
|
#include <linux/proc_ns.h>
|
2017-02-06 02:57:33 -07:00
|
|
|
#include <linux/sched/task.h>
|
2006-10-02 03:18:14 -06:00
|
|
|
|
2016-08-08 13:11:25 -06:00
|
|
|
static struct ucounts *inc_uts_namespaces(struct user_namespace *ns)
|
|
|
|
{
|
|
|
|
return inc_ucount(ns, current_euid(), UCOUNT_UTS_NAMESPACES);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dec_uts_namespaces(struct ucounts *ucounts)
|
|
|
|
{
|
|
|
|
dec_ucount(ucounts, UCOUNT_UTS_NAMESPACES);
|
|
|
|
}
|
|
|
|
|
2009-06-17 17:27:54 -06: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 03:18:17 -06:00
|
|
|
/*
|
|
|
|
* Clone a new ns copying an original utsname, setting refcount to 1
|
|
|
|
* @old_ns: namespace to clone
|
2013-02-27 18:05:30 -07:00
|
|
|
* Return ERR_PTR(-ENOMEM) on error (failure to kmalloc), new ns otherwise
|
2006-10-02 03:18:17 -06:00
|
|
|
*/
|
2012-07-26 05:02:49 -06:00
|
|
|
static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns,
|
2011-03-23 17:43:18 -06:00
|
|
|
struct uts_namespace *old_ns)
|
2006-10-02 03:18:17 -06:00
|
|
|
{
|
|
|
|
struct uts_namespace *ns;
|
2016-08-08 13:11:25 -06:00
|
|
|
struct ucounts *ucounts;
|
2011-06-15 11:21:48 -06:00
|
|
|
int err;
|
2006-10-02 03:18:17 -06:00
|
|
|
|
2016-09-22 12:08:36 -06:00
|
|
|
err = -ENOSPC;
|
2016-08-08 13:11:25 -06:00
|
|
|
ucounts = inc_uts_namespaces(user_ns);
|
|
|
|
if (!ucounts)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
err = -ENOMEM;
|
2009-06-17 17:27:54 -06:00
|
|
|
ns = create_uts_ns();
|
2007-07-16 00:41:06 -06:00
|
|
|
if (!ns)
|
2016-08-08 13:11:25 -06:00
|
|
|
goto fail_dec;
|
2007-07-16 00:41:06 -06:00
|
|
|
|
2014-10-31 22:45:45 -06:00
|
|
|
err = ns_alloc_inum(&ns->ns);
|
2016-08-08 13:11:25 -06:00
|
|
|
if (err)
|
|
|
|
goto fail_free;
|
2011-06-15 11:21:48 -06:00
|
|
|
|
2016-08-08 13:11:25 -06:00
|
|
|
ns->ucounts = ucounts;
|
2014-11-01 00:32:53 -06:00
|
|
|
ns->ns.ops = &utsns_operations;
|
|
|
|
|
2007-09-18 23:46:27 -06:00
|
|
|
down_read(&uts_sem);
|
2007-07-16 00:41:06 -06:00
|
|
|
memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
|
2012-07-26 05:02:49 -06:00
|
|
|
ns->user_ns = get_user_ns(user_ns);
|
2007-09-18 23:46:27 -06:00
|
|
|
up_read(&uts_sem);
|
2006-10-02 03:18:17 -06:00
|
|
|
return ns;
|
2016-08-08 13:11:25 -06:00
|
|
|
|
|
|
|
fail_free:
|
|
|
|
kfree(ns);
|
|
|
|
fail_dec:
|
|
|
|
dec_uts_namespaces(ucounts);
|
|
|
|
fail:
|
|
|
|
return ERR_PTR(err);
|
2006-10-02 03:18:17 -06:00
|
|
|
}
|
|
|
|
|
2006-10-02 03:18:14 -06: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-23 17:43:18 -06:00
|
|
|
struct uts_namespace *copy_utsname(unsigned long flags,
|
2012-07-26 05:02:49 -06:00
|
|
|
struct user_namespace *user_ns, struct uts_namespace *old_ns)
|
2006-10-02 03:18:14 -06:00
|
|
|
{
|
2006-10-02 03:18:17 -06:00
|
|
|
struct uts_namespace *new_ns;
|
2006-10-02 03:18:14 -06:00
|
|
|
|
2007-05-08 01:25:21 -06:00
|
|
|
BUG_ON(!old_ns);
|
2006-10-02 03:18:14 -06:00
|
|
|
get_uts_ns(old_ns);
|
|
|
|
|
2006-10-02 03:18:17 -06:00
|
|
|
if (!(flags & CLONE_NEWUTS))
|
2007-05-08 01:25:21 -06:00
|
|
|
return old_ns;
|
2006-10-02 03:18:17 -06:00
|
|
|
|
2012-07-26 05:02:49 -06:00
|
|
|
new_ns = clone_uts_ns(user_ns, old_ns);
|
2006-10-02 03:18:17 -06:00
|
|
|
|
|
|
|
put_uts_ns(old_ns);
|
2007-05-08 01:25:21 -06:00
|
|
|
return new_ns;
|
2006-10-02 03:18:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void free_uts_ns(struct kref *kref)
|
|
|
|
{
|
|
|
|
struct uts_namespace *ns;
|
|
|
|
|
|
|
|
ns = container_of(kref, struct uts_namespace, kref);
|
2016-08-08 13:11:25 -06:00
|
|
|
dec_uts_namespaces(ns->ucounts);
|
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-23 17:43:16 -06:00
|
|
|
put_user_ns(ns->user_ns);
|
2014-10-31 22:45:45 -06:00
|
|
|
ns_free_inum(&ns->ns);
|
2006-10-02 03:18:14 -06:00
|
|
|
kfree(ns);
|
|
|
|
}
|
2010-03-07 19:43:27 -07:00
|
|
|
|
2014-10-31 22:25:30 -06:00
|
|
|
static inline struct uts_namespace *to_uts_ns(struct ns_common *ns)
|
|
|
|
{
|
|
|
|
return container_of(ns, struct uts_namespace, ns);
|
|
|
|
}
|
|
|
|
|
2014-10-31 22:37:32 -06:00
|
|
|
static struct ns_common *utsns_get(struct task_struct *task)
|
2010-03-07 19:43:27 -07:00
|
|
|
{
|
|
|
|
struct uts_namespace *ns = NULL;
|
|
|
|
struct nsproxy *nsproxy;
|
|
|
|
|
2014-02-03 20:13:49 -07:00
|
|
|
task_lock(task);
|
|
|
|
nsproxy = task->nsproxy;
|
2010-03-07 19:43:27 -07:00
|
|
|
if (nsproxy) {
|
|
|
|
ns = nsproxy->uts_ns;
|
|
|
|
get_uts_ns(ns);
|
|
|
|
}
|
2014-02-03 20:13:49 -07:00
|
|
|
task_unlock(task);
|
2010-03-07 19:43:27 -07:00
|
|
|
|
2014-10-31 22:25:30 -06:00
|
|
|
return ns ? &ns->ns : NULL;
|
2010-03-07 19:43:27 -07:00
|
|
|
}
|
|
|
|
|
2014-10-31 22:37:32 -06:00
|
|
|
static void utsns_put(struct ns_common *ns)
|
2010-03-07 19:43:27 -07:00
|
|
|
{
|
2014-10-31 22:25:30 -06:00
|
|
|
put_uts_ns(to_uts_ns(ns));
|
2010-03-07 19:43:27 -07:00
|
|
|
}
|
|
|
|
|
2014-10-31 22:37:32 -06:00
|
|
|
static int utsns_install(struct nsproxy *nsproxy, struct ns_common *new)
|
2010-03-07 19:43:27 -07:00
|
|
|
{
|
2014-10-31 22:25:30 -06:00
|
|
|
struct uts_namespace *ns = to_uts_ns(new);
|
2012-07-26 02:13:20 -06:00
|
|
|
|
2012-12-14 08:55:36 -07:00
|
|
|
if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
|
2013-03-20 13:49:49 -06:00
|
|
|
!ns_capable(current_user_ns(), CAP_SYS_ADMIN))
|
2012-07-26 02:13:20 -06:00
|
|
|
return -EPERM;
|
|
|
|
|
2010-03-07 19:43:27 -07:00
|
|
|
get_uts_ns(ns);
|
|
|
|
put_uts_ns(nsproxy->uts_ns);
|
|
|
|
nsproxy->uts_ns = ns;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-06 01:47:13 -06:00
|
|
|
static struct user_namespace *utsns_owner(struct ns_common *ns)
|
|
|
|
{
|
|
|
|
return to_uts_ns(ns)->user_ns;
|
|
|
|
}
|
|
|
|
|
2010-03-07 19: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,
|
2016-09-06 01:47:13 -06:00
|
|
|
.owner = utsns_owner,
|
2010-03-07 19:43:27 -07:00
|
|
|
};
|