Skip to content

Commit 275edab

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 275edab

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-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/rlimit_g119.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//go:build go1.19
2+
3+
package system
4+
5+
import (
6+
"sync/atomic"
7+
"syscall"
8+
9+
_ "unsafe"
10+
)
11+
12+
//go:linkname syscallOrigRlimitNofile syscall.origRlimitNofile
13+
var syscallOrigRlimitNofile atomic.Pointer[syscall.Rlimit]
14+
15+
// As reported in issue #4195, the new version of go runtime(since 1.19)
16+
// will cache rlimit-nofile. Before executing execve, the rlimit-nofile
17+
// of the process will be restored with the cache. In runc, this will
18+
// cause the rlimit-nofile setting by the parent process for the container
19+
// to become invalid. It can be solved by clearing this cache. But
20+
// unfortunately, go stdlib doesn't provide such function, so we need to
21+
// link to the private var `origRlimitNofile` in package syscall to hack.
22+
func ClearRlimitNofileCache() {
23+
syscallOrigRlimitNofile.Store(nil)
24+
}

libcontainer/system/rlimit_stub.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//go:build !go1.19
2+
3+
package system
4+
5+
// As reported in issue #4195, the new version of go runtime(since 1.19)
6+
// will cache rlimit-nofile. Before executing execve, the rlimit-nofile
7+
// of the process will be restored with the cache. In runc, this will
8+
// cause the rlimit-nofile setting by the parent process for the container
9+
// to become invalid. It can be solved by clearing this cache. But
10+
// unfortunately, go stdlib doesn't provide such function, so we need to
11+
// link to the private var `origRlimitNofile` in package syscall to hack.
12+
func ClearRlimitNofileCache() {
13+
}

0 commit comments

Comments
 (0)