Skip to content

Commit

Permalink
libcontainer:clean cached rlimit nofile in go runtime
Browse files Browse the repository at this point in the history
As reported in issue opencontainers#4195, the new version of go runtime will
cache rlimit-nofile. before executing exec, the rlimit-nofile
of the process will be updated 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 <335814617@qq.com>
  • Loading branch information
ls-ggg committed Apr 2, 2024
1 parent 4641f17 commit d0dbf90
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions libcontainer/setns_init_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ func (l *linuxSetnsInit) Init() error {
}
}
}

if err := utils.CleanRlimitNoFileCache(); err != nil {
return err
}

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

if err := utils.CleanRlimitNoFileCache(); err != nil {
return err
}

if err := setupNetwork(l.config); err != nil {
return err
}
Expand Down
11 changes: 11 additions & 0 deletions libcontainer/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"strings"
"syscall"
"unsafe"

"golang.org/x/sys/unix"
Expand Down Expand Up @@ -129,3 +130,13 @@ func Annotations(labels []string) (bundle string, userAnnotations map[string]str
}
return
}

// Clean the cache of RLIMIT_NOFILE in go runtime
// https://github.com/golang/go/commit/f5eef58e4381259cbd84b3f2074c79607fb5c821#diff-ec665e9789f8cf5cd1828ad7fa9f0ff4ebc1f5b5dd0fc82a296da5c07da7ece6
func CleanRlimitNoFileCache() error {
rlimit := syscall.Rlimit{}
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlimit); err != nil {
return err
}
return syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlimit)
}
22 changes: 22 additions & 0 deletions tests/integration/resources.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bats

load helpers

function setup() {
setup_busybox
update_config '.process.args = ["/bin/sh", "-c", "ulimit -n"]'
}

function teardown() {
teardown_bundle
}

@test "runc run with RLIMIT_NOFILE" {
update_config '.process.capabilities.bounding = ["CAP_RESOURCE"]'
update_config '.process.rlimits = [{"type": "RLIMIT_NOFILE", "hard": 10000, "soft": 10000}]'

runc run test_hello
[ "$status" -eq 0 ]

[[ "${output}" == *"10000"* ]]
}

0 comments on commit d0dbf90

Please sign in to comment.