2019-06-01 11:08:55 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2006-10-02 13:18:14 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2004 IBM Corporation
|
|
|
|
*
|
|
|
|
* Author: Serge Hallyn <serue@us.ibm.com>
|
|
|
|
*/
|
|
|
|
|
2011-05-23 22:51:41 +04:00
|
|
|
#include <linux/export.h>
|
2006-10-02 13:18:14 +04:00
|
|
|
#include <linux/uts.h>
|
|
|
|
#include <linux/utsname.h>
|
2007-07-16 10:41:06 +04:00
|
|
|
#include <linux/err.h>
|
2008-04-29 11:59:25 +04:00
|
|
|
#include <linux/slab.h>
|
2017-02-02 19:54:15 +03: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-24 02:43:16 +03:00
|
|
|
#include <linux/user_namespace.h>
|
2013-04-12 04:50:06 +04:00
|
|
|
#include <linux/proc_ns.h>
|
2017-02-06 12:57:33 +03:00
|
|
|
#include <linux/sched/task.h>
|
2006-10-02 13:18:14 +04:00
|
|
|
|
2018-04-11 02:32:36 +03:00
|
|
|
static struct kmem_cache *uts_ns_cache __ro_after_init;
|
|
|
|
|
2016-08-08 22:11:25 +03: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-18 03:27:54 +04:00
|
|
|
static struct uts_namespace *create_uts_ns(void)
|
|
|
|
{
|
|
|
|
struct uts_namespace *uts_ns;
|
|
|
|
|
2018-04-11 02:32:36 +03:00
|
|
|
uts_ns = kmem_cache_alloc(uts_ns_cache, GFP_KERNEL);
|
2009-06-18 03:27:54 +04:00
|
|
|
if (uts_ns)
|
2020-08-03 13:16:21 +03:00
|
|
|
refcount_set(&uts_ns->ns.count, 1);
|
2009-06-18 03:27:54 +04:00
|
|
|
return uts_ns;
|
|
|
|
}
|
|
|
|
|
2006-10-02 13:18:17 +04:00
|
|
|
/*
|
|
|
|
* Clone a new ns copying an original utsname, setting refcount to 1
|
|
|
|
* @old_ns: namespace to clone
|
2018-04-11 02:32:36 +03:00
|
|
|
* Return ERR_PTR(-ENOMEM) on error (failure to allocate), new ns otherwise
|
2006-10-02 13:18:17 +04:00
|
|
|
*/
|
2012-07-26 15:02:49 +04:00
|
|
|
static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns,
|
2011-03-24 02:43:18 +03:00
|
|
|
struct uts_namespace *old_ns)
|
2006-10-02 13:18:17 +04:00
|
|
|
{
|
|
|
|
struct uts_namespace *ns;
|
2016-08-08 22:11:25 +03:00
|
|
|
struct ucounts *ucounts;
|
2011-06-15 21:21:48 +04:00
|
|
|
int err;
|
2006-10-02 13:18:17 +04:00
|
|
|
|
2016-09-22 21:08:36 +03:00
|
|
|
err = -ENOSPC;
|
2016-08-08 22:11:25 +03:00
|
|
|
ucounts = inc_uts_namespaces(user_ns);
|
|
|
|
if (!ucounts)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
err = -ENOMEM;
|
2009-06-18 03:27:54 +04:00
|
|
|
ns = create_uts_ns();
|
2007-07-16 10:41:06 +04:00
|
|
|
if (!ns)
|
2016-08-08 22:11:25 +03:00
|
|
|
goto fail_dec;
|
2007-07-16 10:41:06 +04:00
|
|
|
|
2014-11-01 07:45:45 +03:00
|
|
|
err = ns_alloc_inum(&ns->ns);
|
2016-08-08 22:11:25 +03:00
|
|
|
if (err)
|
|
|
|
goto fail_free;
|
2011-06-15 21:21:48 +04:00
|
|
|
|
2016-08-08 22:11:25 +03:00
|
|
|
ns->ucounts = ucounts;
|
2014-11-01 09:32:53 +03:00
|
|
|
ns->ns.ops = &utsns_operations;
|
|
|
|
|
2007-09-19 09:46:27 +04:00
|
|
|
down_read(&uts_sem);
|
2007-07-16 10:41:06 +04:00
|
|
|
memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
|
2012-07-26 15:02:49 +04:00
|
|
|
ns->user_ns = get_user_ns(user_ns);
|
2007-09-19 09:46:27 +04:00
|
|
|
up_read(&uts_sem);
|
2006-10-02 13:18:17 +04:00
|
|
|
return ns;
|
2016-08-08 22:11:25 +03:00
|
|
|
|
|
|
|
fail_free:
|
2018-04-11 02:32:36 +03:00
|
|
|
kmem_cache_free(uts_ns_cache, ns);
|
2016-08-08 22:11:25 +03:00
|
|
|
fail_dec:
|
|
|
|
dec_uts_namespaces(ucounts);
|
|
|
|
fail:
|
|
|
|
return ERR_PTR(err);
|
2006-10-02 13:18:17 +04:00
|
|
|
}
|
|
|
|
|
2006-10-02 13:18:14 +04: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 02:43:18 +03:00
|
|
|
struct uts_namespace *copy_utsname(unsigned long flags,
|
2012-07-26 15:02:49 +04:00
|
|
|
struct user_namespace *user_ns, struct uts_namespace *old_ns)
|
2006-10-02 13:18:14 +04:00
|
|
|
{
|
2006-10-02 13:18:17 +04:00
|
|
|
struct uts_namespace *new_ns;
|
2006-10-02 13:18:14 +04:00
|
|
|
|
2007-05-08 11:25:21 +04:00
|
|
|
BUG_ON(!old_ns);
|
2006-10-02 13:18:14 +04:00
|
|
|
get_uts_ns(old_ns);
|
|
|
|
|
2006-10-02 13:18:17 +04:00
|
|
|
if (!(flags & CLONE_NEWUTS))
|
2007-05-08 11:25:21 +04:00
|
|
|
return old_ns;
|
2006-10-02 13:18:17 +04:00
|
|
|
|
2012-07-26 15:02:49 +04:00
|
|
|
new_ns = clone_uts_ns(user_ns, old_ns);
|
2006-10-02 13:18:17 +04:00
|
|
|
|
|
|
|
put_uts_ns(old_ns);
|
2007-05-08 11:25:21 +04:00
|
|
|
return new_ns;
|
2006-10-02 13:18:14 +04:00
|
|
|
}
|
|
|
|
|
2020-08-03 13:16:21 +03:00
|
|
|
void free_uts_ns(struct uts_namespace *ns)
|
2006-10-02 13:18:14 +04:00
|
|
|
{
|
2016-08-08 22:11:25 +03: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-24 02:43:16 +03:00
|
|
|
put_user_ns(ns->user_ns);
|
2014-11-01 07:45:45 +03:00
|
|
|
ns_free_inum(&ns->ns);
|
2018-04-11 02:32:36 +03:00
|
|
|
kmem_cache_free(uts_ns_cache, ns);
|
2006-10-02 13:18:14 +04:00
|
|
|
}
|
2010-03-08 05:43:27 +03:00
|
|
|
|
2014-11-01 07:25:30 +03:00
|
|
|
static inline struct uts_namespace *to_uts_ns(struct ns_common *ns)
|
|
|
|
{
|
|
|
|
return container_of(ns, struct uts_namespace, ns);
|
|
|
|
}
|
|
|
|
|
2014-11-01 07:37:32 +03:00
|
|
|
static struct ns_common *utsns_get(struct task_struct *task)
|
2010-03-08 05:43:27 +03:00
|
|
|
{
|
|
|
|
struct uts_namespace *ns = NULL;
|
|
|
|
struct nsproxy *nsproxy;
|
|
|
|
|
2014-02-04 07:13:49 +04:00
|
|
|
task_lock(task);
|
|
|
|
nsproxy = task->nsproxy;
|
2010-03-08 05:43:27 +03:00
|
|
|
if (nsproxy) {
|
|
|
|
ns = nsproxy->uts_ns;
|
|
|
|
get_uts_ns(ns);
|
|
|
|
}
|
2014-02-04 07:13:49 +04:00
|
|
|
task_unlock(task);
|
2010-03-08 05:43:27 +03:00
|
|
|
|
2014-11-01 07:25:30 +03:00
|
|
|
return ns ? &ns->ns : NULL;
|
2010-03-08 05:43:27 +03:00
|
|
|
}
|
|
|
|
|
2014-11-01 07:37:32 +03:00
|
|
|
static void utsns_put(struct ns_common *ns)
|
2010-03-08 05:43:27 +03:00
|
|
|
{
|
2014-11-01 07:25:30 +03:00
|
|
|
put_uts_ns(to_uts_ns(ns));
|
2010-03-08 05:43:27 +03:00
|
|
|
}
|
|
|
|
|
2020-05-05 17:04:30 +03:00
|
|
|
static int utsns_install(struct nsset *nsset, struct ns_common *new)
|
2010-03-08 05:43:27 +03:00
|
|
|
{
|
2020-05-05 17:04:30 +03:00
|
|
|
struct nsproxy *nsproxy = nsset->nsproxy;
|
2014-11-01 07:25:30 +03:00
|
|
|
struct uts_namespace *ns = to_uts_ns(new);
|
2012-07-26 12:13:20 +04:00
|
|
|
|
2012-12-14 19:55:36 +04:00
|
|
|
if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
|
2020-05-05 17:04:30 +03:00
|
|
|
!ns_capable(nsset->cred->user_ns, CAP_SYS_ADMIN))
|
2012-07-26 12:13:20 +04:00
|
|
|
return -EPERM;
|
|
|
|
|
2010-03-08 05:43:27 +03:00
|
|
|
get_uts_ns(ns);
|
|
|
|
put_uts_ns(nsproxy->uts_ns);
|
|
|
|
nsproxy->uts_ns = ns;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-06 10:47:13 +03:00
|
|
|
static struct user_namespace *utsns_owner(struct ns_common *ns)
|
|
|
|
{
|
|
|
|
return to_uts_ns(ns)->user_ns;
|
|
|
|
}
|
|
|
|
|
2010-03-08 05:43:27 +03: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 10:47:13 +03:00
|
|
|
.owner = utsns_owner,
|
2010-03-08 05:43:27 +03:00
|
|
|
};
|
2018-04-11 02:32:36 +03:00
|
|
|
|
|
|
|
void __init uts_ns_init(void)
|
|
|
|
{
|
|
|
|
uts_ns_cache = kmem_cache_create_usercopy(
|
|
|
|
"uts_namespace", sizeof(struct uts_namespace), 0,
|
|
|
|
SLAB_PANIC|SLAB_ACCOUNT,
|
|
|
|
offsetof(struct uts_namespace, name),
|
|
|
|
sizeof_field(struct uts_namespace, name),
|
|
|
|
NULL);
|
|
|
|
}
|