Skip to content

Commit

Permalink
libct/cg/OpenFile: check cgroupFd on error
Browse files Browse the repository at this point in the history
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 it might be caused by
using the same process on the both sides of the container boundary).

Consider such use incorrect, but give a helpful hint as two what is
going on by enriching the Path component of the error with the fd
information.

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]>
  • Loading branch information
kolyshkin committed Jun 15, 2021
1 parent b53485c commit 2e55f8b
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions libcontainer/cgroups/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"os"
"path"
"strconv"
"strings"
"sync"

Expand Down Expand Up @@ -134,6 +135,17 @@ func openFile(dir, file string, flags int) (*os.File, error) {
Mode: uint64(mode),
})
if err != nil {
// Check if cgroupFd is still opened to cgroupfsDir.
fdStr := strconv.Itoa(cgroupFd)
fdDest, _ := os.Readlink("/proc/self/fd/" + fdStr)
if fdDest != cgroupfsDir {
// TODO: reopen cgroupFd and retry openat2.

// Enhance the Path in the error to contain the
// cgroupFd value and the directory it is opened to,
// for example: "@[fd 7:/!=/sys/fs/cgroup]/cpu.stat".
path = "@[fd " + fdStr + ":" + fdDest + "!=" + cgroupfsDir + "]/" + relPath
}
return nil, &os.PathError{Op: "openat2", Path: path, Err: err}
}

Expand Down

0 comments on commit 2e55f8b

Please sign in to comment.