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 c91fe9aeba ("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 <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin
2026-02-05 17:41:55 -08:00
parent 1d030fab7d
commit 6c07a37a58
+28 -5
View File
@@ -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,