Skip to content

Commit bd50e7c

Browse files
committed
libct/cg/OpenFile: check cgroupFd on error
opencontainers/runc issue 3026 describes a scenario in which OpenFile failed to open a legitimate existing cgroupfs file. Added debug (similar to what this commit does) shown that cgroupFd is no longer opened to "/sys/fs/cgroup", but to "/" (it's not clear what caused it, and the source code is not available, but they might be using the same process on the both sides of the container/chroot/pivot_root/mntns boundary, or remounting /sys/fs/cgroup). Consider such use incorrect, but give a helpful hint as two what is going on by wrapping the error in a more useful message. NB: this can potentially be fixed by reopening the cgroupFd once we detected that it's screwed, and retrying openat2. Alas I do not have a test case for this, so left this as a TODO suggestion. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent c2d9668 commit bd50e7c

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

libcontainer/cgroups/file.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"os"
88
"path"
9+
"strconv"
910
"strings"
1011
"sync"
1112

@@ -137,7 +138,23 @@ func openFile(dir, file string, flags int) (*os.File, error) {
137138
Mode: uint64(mode),
138139
})
139140
if err != nil {
140-
return nil, &os.PathError{Op: "openat2", Path: path, Err: err}
141+
err = &os.PathError{Op: "openat2", Path: path, Err: err}
142+
// Check if cgroupFd is still opened to cgroupfsDir
143+
// (happens when this package is incorrectly used
144+
// across the chroot/pivot_root/mntns boundary, or
145+
// when /sys/fs/cgroup is remounted).
146+
//
147+
// TODO: if such usage will ever be common, amend this
148+
// to reopen cgroupFd and retry openat2.
149+
fdStr := strconv.Itoa(cgroupFd)
150+
fdDest, _ := os.Readlink("/proc/self/fd/" + fdStr)
151+
if fdDest != cgroupfsDir {
152+
// Wrap the error so it is clear that cgroupFd
153+
// is opened to an unexpected/wrong directory.
154+
err = fmt.Errorf("cgroupFd %s unexpectedly opened to %s != %s: %w",
155+
fdStr, fdDest, cgroupfsDir, err)
156+
}
157+
return nil, err
141158
}
142159

143160
return os.NewFile(uintptr(fd), path), nil

0 commit comments

Comments
 (0)