Skip to content

Commit

Permalink
cgroups: verify that we are actually running in cgroup namespace
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
  • Loading branch information
Christian Brauner committed Feb 26, 2021
1 parent c915eca commit 253990c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/lxc/cgroups/cgfsng.c
Expand Up @@ -1870,7 +1870,7 @@ __cgfsng_ops static bool cgfsng_mount(struct cgroup_ops *ops,
{
__do_close int dfd_mnt_cgroupfs = -EBADF, fd_fs = -EBADF;
__do_free char *cgroup_root = NULL;
bool has_cgns = false, wants_force_mount = false;
bool in_cgroup_ns = false, wants_force_mount = false;
struct lxc_conf *conf = handler->conf;
struct lxc_rootfs *rootfs = &conf->rootfs;
const char *rootfs_mnt = get_rootfs_mnt(rootfs);
Expand Down Expand Up @@ -1910,8 +1910,9 @@ __cgfsng_ops static bool cgfsng_mount(struct cgroup_ops *ops,
wants_force_mount = true;
}

has_cgns = cgns_supported();
if (has_cgns && !wants_force_mount)
if (cgns_supported() && container_uses_namespace(handler, CLONE_NEWCGROUP))
in_cgroup_ns = true;
if (in_cgroup_ns && !wants_force_mount)
return true;

if (type == LXC_AUTO_CGROUP_NOSPEC)
Expand All @@ -1929,7 +1930,7 @@ __cgfsng_ops static bool cgfsng_mount(struct cgroup_ops *ops,
return log_error_errno(-errno, errno, "Failed to open %d(%s)",
rootfs->dfd_mnt, DEFAULT_CGROUP_MOUNTPOINT_RELATIVE);

if (has_cgns && wants_force_mount) {
if (in_cgroup_ns && wants_force_mount) {
/*
* If cgroup namespaces are supported but the container
* will not have CAP_SYS_ADMIN after it has started we
Expand Down Expand Up @@ -1994,7 +1995,7 @@ __cgfsng_ops static bool cgfsng_mount(struct cgroup_ops *ops,
if (ret < 0)
return log_error_errno(false, errno, "Failed to create cgroup mountpoint %d(%s)", dfd_mnt_cgroupfs, controller);

if (has_cgns && wants_force_mount) {
if (in_cgroup_ns && wants_force_mount) {
/*
* If cgroup namespaces are supported but the container
* will not have CAP_SYS_ADMIN after it has started we
Expand Down
6 changes: 6 additions & 0 deletions src/lxc/start.h
Expand Up @@ -180,4 +180,10 @@ __hidden extern int __lxc_start(struct lxc_handler *, struct lxc_operations *, v
__hidden extern int resolve_clone_flags(struct lxc_handler *handler);
__hidden extern void lxc_expose_namespace_environment(const struct lxc_handler *handler);

static inline bool container_uses_namespace(const struct lxc_handler *handler,
unsigned int ns_flag)
{
return (handler->ns_clone_flags & ns_flag);
}

#endif

0 comments on commit 253990c

Please sign in to comment.