Skip to content

Commit c9893a3

Browse files
ls-ggglifubang
authored andcommitted
libct: clean cached rlimit nofile in go runtime
As reported in issue opencontainers#4195, the new version(since 1.19) of go runtime will cache rlimit-nofile. Before executing execve, 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. It 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]> (cherry picked from commit da68c8e) Signed-off-by: lifubang <[email protected]>
1 parent ebc0f65 commit c9893a3

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

libcontainer/init_linux.go

+16
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ func newContainerInit(t initType, pipe *os.File, consoleSocket *os.File, fifoFd,
8484
if err := populateProcessEnvironment(config.Env); err != nil {
8585
return nil, err
8686
}
87+
88+
// Clean the RLIMIT_NOFILE cache in go runtime.
89+
// Issue: https://github.com/opencontainers/runc/issues/4195
90+
if containsRlimit(config.Rlimits, unix.RLIMIT_NOFILE) {
91+
system.ClearRlimitNofileCache()
92+
}
93+
8794
switch t {
8895
case initSetns:
8996
// mountFds must be nil in this case. We don't mount while doing runc exec.
@@ -518,6 +525,15 @@ func setupRoute(config *configs.Config) error {
518525
return nil
519526
}
520527

528+
func containsRlimit(limits []configs.Rlimit, resource int) bool {
529+
for _, rlimit := range limits {
530+
if rlimit.Type == resource {
531+
return true
532+
}
533+
}
534+
return false
535+
}
536+
521537
func setupRlimits(limits []configs.Rlimit, pid int) error {
522538
for _, rlimit := range limits {
523539
if err := unix.Prlimit(pid, rlimit.Type, &unix.Rlimit{Max: rlimit.Hard, Cur: rlimit.Soft}, nil); err != nil {

libcontainer/setns_init_linux.go

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func (l *linuxSetnsInit) Init() error {
4848
}
4949
}
5050
}
51+
5152
if l.config.CreateConsole {
5253
if err := setupConsole(l.consoleSocket, l.config, false); err != nil {
5354
return err

libcontainer/system/linux.go

+16
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,27 @@ package system
55

66
import (
77
"os"
8+
"sync/atomic"
9+
"syscall"
810
"unsafe"
911

1012
"golang.org/x/sys/unix"
1113
)
1214

15+
//go:linkname syscallOrigRlimitNofile syscall.origRlimitNofile
16+
var syscallOrigRlimitNofile atomic.Pointer[syscall.Rlimit]
17+
18+
// As reported in issue #4195, the new version of go runtime(since 1.19)
19+
// will cache rlimit-nofile. Before executing execve, the rlimit-nofile
20+
// of the process will be restored with the cache. In runc, this will
21+
// cause the rlimit-nofile setting by the parent process for the container
22+
// to become invalid. It can be solved by clearing this cache. But
23+
// unfortunately, go stdlib doesn't provide such function, so we need to
24+
// link to the private var `origRlimitNofile` in package syscall to hack.
25+
func ClearRlimitNofileCache() {
26+
syscallOrigRlimitNofile.Store(nil)
27+
}
28+
1329
type ParentDeathSignal int
1430

1531
func (p ParentDeathSignal) Restore() error {

0 commit comments

Comments
 (0)