From 6c07a37a585db26a3117683456c9c06f97dc7485 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Thu, 5 Feb 2026 17:41:55 -0800 Subject: [PATCH] libct: prepareCgroupFD: fall back to container init cgroup Previously, when prepareCgroupFD would not open container's cgroup (as configured in config.json and saved to state.json), it returned a fatal error, as we presumed a container can't exist without its own cgroup. Apparently, it can. In a case when container is configured without cgroupns (i.e. it uses hosts cgroups), and /sys/fs/cgroup is mounted read-write, a rootful container's init can move itself to an entirely different cgroup (even a new one that it just created), and then the original container cgroup is removed by the kernel (or systemd?) as it has no processes left. By the way, from the systemd point of view the container is gone. And yet it is still there, and users want runc exec to work! And it worked, thanks to the "let's try container init's cgroup" fallback as added by commit c91fe9aebac83 ("cgroup2: exec: join the cgroup of the init process on EBUSY"). The fallback was added for the entirely different reason, but it happened to work in this very case, too. This behavior was broken with the introduction of CLONE_INTO_CGROUP support. While it is debatable whether this is a valid scenario when a container moves itself into a different cgroup, this very setup is used by e.g. buildkitd running in a privileged kubernetes container (see issue 5089). To restore the way things are expected to work, add the same "try container init's cgroup" fallback into prepareCgroupFD. While at it, simplify the code flow. Signed-off-by: Kir Kolyshkin --- libcontainer/process_linux.go | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/libcontainer/process_linux.go b/libcontainer/process_linux.go index f4fb8f039..439c7c734 100644 --- a/libcontainer/process_linux.go +++ b/libcontainer/process_linux.go @@ -366,6 +366,8 @@ func (p *setnsProcess) addIntoCgroup() error { // to join cgroup early, in p.cmd.Start. Returns an *os.File which // must be closed by the caller after p.Cmd.Start return. func (p *setnsProcess) prepareCgroupFD() (*os.File, error) { + const openFlags = unix.O_PATH | unix.O_DIRECTORY | unix.O_CLOEXEC + if !cgroups.IsCgroup2UnifiedMode() { return nil, nil } @@ -383,14 +385,28 @@ func (p *setnsProcess) prepareCgroupFD() (*os.File, error) { return nil, fmt.Errorf("bad sub cgroup path: %s", sub) } - fd, err := cgroups.OpenFile(base, sub, unix.O_PATH|unix.O_DIRECTORY|unix.O_CLOEXEC) + fd, err := cgroups.OpenFile(base, sub, openFlags) + if err == nil { + goto success + } + // Failed to open the configured cgroup. Fall back to container init's cgroup + // unless sub-cgroup is explicitly requested. The fallback logic should be + // the same as in addIntoCgroupV2. + if sub != "" { + goto fail + } + cgroup = p.initProcessCgroupPath() + if cgroup == "" { + goto fail + } + logrus.Debugf("failed to open configured cgroup (%v), will open container init cgroup %q", err, cgroup) + // NOTE: path is not guaranteed to exist because we didn't pause the container. + fd, err = cgroups.OpenFile(cgroup, "", openFlags) if err != nil { - if p.rootlessCgroups { - return nil, nil - } - return nil, fmt.Errorf("can't open cgroup: %w", err) + goto fail } +success: logrus.Debugf("using CLONE_INTO_CGROUP %q", cgroup) if p.cmd.SysProcAttr == nil { p.cmd.SysProcAttr = &syscall.SysProcAttr{} @@ -399,6 +415,13 @@ func (p *setnsProcess) prepareCgroupFD() (*os.File, error) { p.cmd.SysProcAttr.CgroupFD = int(fd.Fd()) return fd, nil + +fail: + // Ignore cgroup join error for rootless. + if p.rootlessCgroups { + return nil, nil + } + return nil, fmt.Errorf("can't open cgroup: %w", err) } // startWithCgroupFD starts a process via clone3 with CLONE_INTO_CGROUP,