Skip to content

Commit

Permalink
libct: clean cached rlimit nofile in go runtime
Browse files Browse the repository at this point in the history
As reported in issue #4195, the new version of go runtime will
cache rlimit-nofile. Before executing exec, the rlimit-nofile
of the process will be restored with the cache. In runc, this will
cause the rlimit-nofile set by the parent process for the container
to become invalid. This can be solved by clearing the cache.

Signed-off-by: ls-ggg <[email protected]>
(cherry picked from commit f9f8abf)
Signed-off-by: lifubang <[email protected]>
  • Loading branch information
ls-ggg authored and lifubang committed May 7, 2024
1 parent 28a9681 commit 83fa2f7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
9 changes: 9 additions & 0 deletions libcontainer/init_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,15 @@ func setupRoute(config *configs.Config) error {
return nil
}

func containsRlimit(limits []configs.Rlimit, resource int) bool {
for _, rlimit := range limits {
if rlimit.Type == resource {
return true
}
}
return false
}

func setupRlimits(limits []configs.Rlimit, pid int) error {
for _, rlimit := range limits {
if err := unix.Prlimit(pid, rlimit.Type, &unix.Rlimit{Max: rlimit.Hard, Cur: rlimit.Soft}, nil); err != nil {
Expand Down
7 changes: 7 additions & 0 deletions libcontainer/setns_init_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ func (l *linuxSetnsInit) Init() error {
}
}
}

// Clean the RLIMIT_NOFILE cache in go runtime.
// Issue: https://github.com/opencontainers/runc/issues/4195
if containsRlimit(l.config.Rlimits, unix.RLIMIT_NOFILE) {
system.ClearRlimitNofileCache()
}

if l.config.CreateConsole {
if err := setupConsole(l.consoleSocket, l.config, false); err != nil {
return err
Expand Down
6 changes: 6 additions & 0 deletions libcontainer/standard_init_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ func (l *linuxStandardInit) Init() error {
}
}

// Clean the RLIMIT_NOFILE cache in go runtime.
// Issue: https://github.com/opencontainers/runc/issues/4195
if containsRlimit(l.config.Rlimits, unix.RLIMIT_NOFILE) {
system.ClearRlimitNofileCache()
}

if err := setupNetwork(l.config); err != nil {
return err
}
Expand Down
16 changes: 16 additions & 0 deletions libcontainer/system/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,29 @@ import (
"os"
"os/exec"
"strconv"
"sync/atomic"
"syscall"
"unsafe"

"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
)

//go:linkname syscallOrigRlimitNofile syscall.origRlimitNofile

// As reported in issue #4195, the new version of go runtime will
// cache rlimit-nofile. Before executing exec, the rlimit-nofile
// of the process will be restored with the cache. In runc, this will
// cause the rlimit-nofile set by the parent process for the container
// to become invalid. This can be solved by clearing the cache. But
// unfortunately, go stdlib doesn't provide such function, so we need to
// link to the private var `origRlimitNofile` in package syscall.
var syscallOrigRlimitNofile atomic.Pointer[syscall.Rlimit]

func ClearRlimitNofileCache() {
syscallOrigRlimitNofile.Store(nil)
}

type ParentDeathSignal int

func (p ParentDeathSignal) Restore() error {
Expand Down

0 comments on commit 83fa2f7

Please sign in to comment.