mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-08-23 17:30:02 +00:00
![Vasily Averin](/assets/img/avatar_default.png)
Container admin can create new namespaces and force kernel to allocate up to several pages of memory for the namespaces and its associated structures. Net and uts namespaces have enabled accounting for such allocations. It makes sense to account for rest ones to restrict the host's memory consumption from inside the memcg-limited container. Link: https://lkml.kernel.org/r/5525bcbf-533e-da27-79b7-158686c64e13@virtuozzo.com Signed-off-by: Vasily Averin <vvs@virtuozzo.com> Acked-by: Serge Hallyn <serge@hallyn.com> Acked-by: Christian Brauner <christian.brauner@ubuntu.com> Acked-by: Kirill Tkhai <ktkhai@virtuozzo.com> Reviewed-by: Shakeel Butt <shakeelb@google.com> Cc: Alexander Viro <viro@zeniv.linux.org.uk> Cc: Alexey Dobriyan <adobriyan@gmail.com> Cc: Andrei Vagin <avagin@gmail.com> Cc: Borislav Petkov <bp@alien8.de> Cc: Borislav Petkov <bp@suse.de> Cc: Dmitry Safonov <0x7f454c46@gmail.com> Cc: "Eric W. Biederman" <ebiederm@xmission.com> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: "H. Peter Anvin" <hpa@zytor.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: "J. Bruce Fields" <bfields@fieldses.org> Cc: Jeff Layton <jlayton@kernel.org> Cc: Jens Axboe <axboe@kernel.dk> Cc: Jiri Slaby <jirislaby@kernel.org> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Michal Hocko <mhocko@kernel.org> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Roman Gushchin <guro@fb.com> Cc: Tejun Heo <tj@kernel.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Vladimir Davydov <vdavydov.dev@gmail.com> Cc: Yutian Yang <nglaive@gmail.com> Cc: Zefan Li <lizefan.x@bytedance.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
157 lines
3.4 KiB
C
157 lines
3.4 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
#include "cgroup-internal.h"
|
|
|
|
#include <linux/sched/task.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/nsproxy.h>
|
|
#include <linux/proc_ns.h>
|
|
|
|
|
|
/* cgroup namespaces */
|
|
|
|
static struct ucounts *inc_cgroup_namespaces(struct user_namespace *ns)
|
|
{
|
|
return inc_ucount(ns, current_euid(), UCOUNT_CGROUP_NAMESPACES);
|
|
}
|
|
|
|
static void dec_cgroup_namespaces(struct ucounts *ucounts)
|
|
{
|
|
dec_ucount(ucounts, UCOUNT_CGROUP_NAMESPACES);
|
|
}
|
|
|
|
static struct cgroup_namespace *alloc_cgroup_ns(void)
|
|
{
|
|
struct cgroup_namespace *new_ns;
|
|
int ret;
|
|
|
|
new_ns = kzalloc(sizeof(struct cgroup_namespace), GFP_KERNEL_ACCOUNT);
|
|
if (!new_ns)
|
|
return ERR_PTR(-ENOMEM);
|
|
ret = ns_alloc_inum(&new_ns->ns);
|
|
if (ret) {
|
|
kfree(new_ns);
|
|
return ERR_PTR(ret);
|
|
}
|
|
refcount_set(&new_ns->ns.count, 1);
|
|
new_ns->ns.ops = &cgroupns_operations;
|
|
return new_ns;
|
|
}
|
|
|
|
void free_cgroup_ns(struct cgroup_namespace *ns)
|
|
{
|
|
put_css_set(ns->root_cset);
|
|
dec_cgroup_namespaces(ns->ucounts);
|
|
put_user_ns(ns->user_ns);
|
|
ns_free_inum(&ns->ns);
|
|
kfree(ns);
|
|
}
|
|
EXPORT_SYMBOL(free_cgroup_ns);
|
|
|
|
struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,
|
|
struct user_namespace *user_ns,
|
|
struct cgroup_namespace *old_ns)
|
|
{
|
|
struct cgroup_namespace *new_ns;
|
|
struct ucounts *ucounts;
|
|
struct css_set *cset;
|
|
|
|
BUG_ON(!old_ns);
|
|
|
|
if (!(flags & CLONE_NEWCGROUP)) {
|
|
get_cgroup_ns(old_ns);
|
|
return old_ns;
|
|
}
|
|
|
|
/* Allow only sysadmin to create cgroup namespace. */
|
|
if (!ns_capable(user_ns, CAP_SYS_ADMIN))
|
|
return ERR_PTR(-EPERM);
|
|
|
|
ucounts = inc_cgroup_namespaces(user_ns);
|
|
if (!ucounts)
|
|
return ERR_PTR(-ENOSPC);
|
|
|
|
/* It is not safe to take cgroup_mutex here */
|
|
spin_lock_irq(&css_set_lock);
|
|
cset = task_css_set(current);
|
|
get_css_set(cset);
|
|
spin_unlock_irq(&css_set_lock);
|
|
|
|
new_ns = alloc_cgroup_ns();
|
|
if (IS_ERR(new_ns)) {
|
|
put_css_set(cset);
|
|
dec_cgroup_namespaces(ucounts);
|
|
return new_ns;
|
|
}
|
|
|
|
new_ns->user_ns = get_user_ns(user_ns);
|
|
new_ns->ucounts = ucounts;
|
|
new_ns->root_cset = cset;
|
|
|
|
return new_ns;
|
|
}
|
|
|
|
static inline struct cgroup_namespace *to_cg_ns(struct ns_common *ns)
|
|
{
|
|
return container_of(ns, struct cgroup_namespace, ns);
|
|
}
|
|
|
|
static int cgroupns_install(struct nsset *nsset, struct ns_common *ns)
|
|
{
|
|
struct nsproxy *nsproxy = nsset->nsproxy;
|
|
struct cgroup_namespace *cgroup_ns = to_cg_ns(ns);
|
|
|
|
if (!ns_capable(nsset->cred->user_ns, CAP_SYS_ADMIN) ||
|
|
!ns_capable(cgroup_ns->user_ns, CAP_SYS_ADMIN))
|
|
return -EPERM;
|
|
|
|
/* Don't need to do anything if we are attaching to our own cgroupns. */
|
|
if (cgroup_ns == nsproxy->cgroup_ns)
|
|
return 0;
|
|
|
|
get_cgroup_ns(cgroup_ns);
|
|
put_cgroup_ns(nsproxy->cgroup_ns);
|
|
nsproxy->cgroup_ns = cgroup_ns;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static struct ns_common *cgroupns_get(struct task_struct *task)
|
|
{
|
|
struct cgroup_namespace *ns = NULL;
|
|
struct nsproxy *nsproxy;
|
|
|
|
task_lock(task);
|
|
nsproxy = task->nsproxy;
|
|
if (nsproxy) {
|
|
ns = nsproxy->cgroup_ns;
|
|
get_cgroup_ns(ns);
|
|
}
|
|
task_unlock(task);
|
|
|
|
return ns ? &ns->ns : NULL;
|
|
}
|
|
|
|
static void cgroupns_put(struct ns_common *ns)
|
|
{
|
|
put_cgroup_ns(to_cg_ns(ns));
|
|
}
|
|
|
|
static struct user_namespace *cgroupns_owner(struct ns_common *ns)
|
|
{
|
|
return to_cg_ns(ns)->user_ns;
|
|
}
|
|
|
|
const struct proc_ns_operations cgroupns_operations = {
|
|
.name = "cgroup",
|
|
.type = CLONE_NEWCGROUP,
|
|
.get = cgroupns_get,
|
|
.put = cgroupns_put,
|
|
.install = cgroupns_install,
|
|
.owner = cgroupns_owner,
|
|
};
|
|
|
|
static __init int cgroup_namespaces_init(void)
|
|
{
|
|
return 0;
|
|
}
|
|
subsys_initcall(cgroup_namespaces_init);
|