Skip to content

Commit

Permalink
libct/system: ClearRlimitNofileCache for go 1.23
Browse files Browse the repository at this point in the history
Go 1.23 tightens access to internal symbols, and even puts runc into
"hall of shame" for using an internal symbol (recently added by commit
da68c8e). So, while not impossible, it becomes harder to access those
internal symbols, and it is a bad idea in general.

Assuming Go 1.23 comes with https://go.dev/cl/588076, we can clean the
internal rlimit cache by setting the RLIMIT_NOFILE for ourselves,
essentially disabling the rlimit cache.

NOTE this also relies on golang.org/x/sys/unix having https://go.dev/cl/476695.

Signed-off-by: Kir Kolyshkin <[email protected]>
  • Loading branch information
kolyshkin committed May 23, 2024
1 parent 68564ee commit 76a6a86
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 23 deletions.
16 changes: 9 additions & 7 deletions libcontainer/init_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,7 @@ func containerInit(t initType, config *initConfig, pipe *syncSocket, consoleSock

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

switch t {
case initSetns:
Expand Down Expand Up @@ -655,13 +653,17 @@ func setupRoute(config *configs.Config) error {
return nil
}

func containsRlimit(limits []configs.Rlimit, resource int) bool {
func maybeClearRlimitNofileCache(limits []configs.Rlimit) {
const res = unix.RLIMIT_NOFILE
for _, rlimit := range limits {
if rlimit.Type == resource {
return true
if rlimit.Type == res {
system.ClearRlimitNofileCache(&unix.Rlimit{
Cur: rlimit.Soft,
Max: rlimit.Hard,
})
return
}
}
return false
}

func setupRlimits(limits []configs.Rlimit, pid int) error {
Expand Down
16 changes: 0 additions & 16 deletions libcontainer/system/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,13 @@ import (
"io"
"os"
"strconv"
"sync/atomic"
"syscall"
"unsafe"

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

//go:linkname syscallOrigRlimitNofile syscall.origRlimitNofile
var syscallOrigRlimitNofile atomic.Pointer[syscall.Rlimit]

// ClearRlimitNofileCache is to clear go runtime's nofile rlimit cache.
func ClearRlimitNofileCache() {
// As reported in issue #4195, the new version of go runtime(since 1.19)
// 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 setting by the parent process for the container
// to become invalid. It can be solved by clearing this cache. But
// unfortunately, go stdlib doesn't provide such function, so we need to
// link to the private var `origRlimitNofile` in package syscall to hack.
syscallOrigRlimitNofile.Store(nil)
}

type ParentDeathSignal int

func (p ParentDeathSignal) Restore() error {
Expand Down
26 changes: 26 additions & 0 deletions libcontainer/system/rlimit_linux_go122.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//go:build !go1.23

package system

import (
"sync/atomic"
"syscall"

"golang.org/x/sys/unix"
)

//go:linkname syscallOrigRlimitNofile syscall.origRlimitNofile

Check failure on line 12 in libcontainer/system/rlimit_linux_go122.go

View workflow job for this annotation

GitHub Actions / compile-buildtags

//go:linkname only allowed in Go files that import "unsafe"

Check failure on line 12 in libcontainer/system/rlimit_linux_go122.go

View workflow job for this annotation

GitHub Actions / cross-i386 (runc_nodmz)

//go:linkname only allowed in Go files that import "unsafe"

Check failure on line 12 in libcontainer/system/rlimit_linux_go122.go

View workflow job for this annotation

GitHub Actions / cross-i386

//go:linkname only allowed in Go files that import "unsafe"

Check failure on line 12 in libcontainer/system/rlimit_linux_go122.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-20.04, 1.21.x, rootless)

//go:linkname only allowed in Go files that import "unsafe"

Check failure on line 12 in libcontainer/system/rlimit_linux_go122.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-20.04, 1.21.x)

//go:linkname only allowed in Go files that import "unsafe"

Check failure on line 12 in libcontainer/system/rlimit_linux_go122.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.20.x)

//go:linkname only allowed in Go files that import "unsafe"

Check failure on line 12 in libcontainer/system/rlimit_linux_go122.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.21.x)

//go:linkname only allowed in Go files that import "unsafe"

Check failure on line 12 in libcontainer/system/rlimit_linux_go122.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.21.x, rootless, -race)

//go:linkname only allowed in Go files that import "unsafe"

Check failure on line 12 in libcontainer/system/rlimit_linux_go122.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.21.x, runc_nodmz)

//go:linkname only allowed in Go files that import "unsafe"

Check failure on line 12 in libcontainer/system/rlimit_linux_go122.go

View workflow job for this annotation

GitHub Actions / lint

//go:linkname only allowed in Go files that import "unsafe") (typecheck)

Check failure on line 12 in libcontainer/system/rlimit_linux_go122.go

View workflow job for this annotation

GitHub Actions / lint

//go:linkname only allowed in Go files that import "unsafe") (typecheck)

Check failure on line 12 in libcontainer/system/rlimit_linux_go122.go

View workflow job for this annotation

GitHub Actions / lint

//go:linkname only allowed in Go files that import "unsafe") (typecheck)

Check failure on line 12 in libcontainer/system/rlimit_linux_go122.go

View workflow job for this annotation

GitHub Actions / lint

//go:linkname only allowed in Go files that import "unsafe" (typecheck)

Check failure on line 12 in libcontainer/system/rlimit_linux_go122.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.21.x, -race)

//go:linkname only allowed in Go files that import "unsafe"

Check failure on line 12 in libcontainer/system/rlimit_linux_go122.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.20.x, rootless)

//go:linkname only allowed in Go files that import "unsafe"

Check failure on line 12 in libcontainer/system/rlimit_linux_go122.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-20.04, 1.21.x, -race)

//go:linkname only allowed in Go files that import "unsafe"

Check failure on line 12 in libcontainer/system/rlimit_linux_go122.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.20.x, rootless, -race)

//go:linkname only allowed in Go files that import "unsafe"

Check failure on line 12 in libcontainer/system/rlimit_linux_go122.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.20.x, -race)

//go:linkname only allowed in Go files that import "unsafe"

Check failure on line 12 in libcontainer/system/rlimit_linux_go122.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-20.04, 1.21.x, rootless, -race)

//go:linkname only allowed in Go files that import "unsafe"

Check failure on line 12 in libcontainer/system/rlimit_linux_go122.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-20.04, 1.21.x, criu-dev)

//go:linkname only allowed in Go files that import "unsafe"

Check failure on line 12 in libcontainer/system/rlimit_linux_go122.go

View workflow job for this annotation

GitHub Actions / test (actuated-arm64-6cpu-8gb, 1.21.x)

//go:linkname only allowed in Go files that import "unsafe"

Check failure on line 12 in libcontainer/system/rlimit_linux_go122.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.21.x, rootless)

//go:linkname only allowed in Go files that import "unsafe"

Check failure on line 12 in libcontainer/system/rlimit_linux_go122.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.21.x, criu-dev)

//go:linkname only allowed in Go files that import "unsafe"

Check failure on line 12 in libcontainer/system/rlimit_linux_go122.go

View workflow job for this annotation

GitHub Actions / test (actuated-arm64-6cpu-8gb, 1.21.x, rootless)

//go:linkname only allowed in Go files that import "unsafe"

Check failure on line 12 in libcontainer/system/rlimit_linux_go122.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-20.04, 1.20.x)

//go:linkname only allowed in Go files that import "unsafe"

Check failure on line 12 in libcontainer/system/rlimit_linux_go122.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-20.04, 1.20.x, rootless)

//go:linkname only allowed in Go files that import "unsafe"

Check failure on line 12 in libcontainer/system/rlimit_linux_go122.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-20.04, 1.20.x, rootless, -race)

//go:linkname only allowed in Go files that import "unsafe"

Check failure on line 12 in libcontainer/system/rlimit_linux_go122.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-20.04, 1.20.x, -race)

//go:linkname only allowed in Go files that import "unsafe"
var syscallOrigRlimitNofile atomic.Pointer[syscall.Rlimit]

// ClearRlimitNofileCache clears go runtime's nofile rlimit cache.
// The argument is process RLIMIT_NOFILE values.
func ClearRlimitNofileCache(_ *unix.Rlimit) {
// As reported in issue #4195, the new version of go runtime(since 1.19)
// 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 setting by the parent process for the container
// to become invalid. It can be solved by clearing this cache. But
// unfortunately, go stdlib doesn't provide such function, so we need to
// link to the private var `origRlimitNofile` in package syscall to hack.
syscallOrigRlimitNofile.Store(nil)
}
20 changes: 20 additions & 0 deletions libcontainer/system/rlimit_linux_go123.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//go:build go1.23

package system

import (
"sync/atomic"
"syscall"

"golang.org/x/sys/unix"
)

// CleanRlimitNofileCache sets RLIMIT_NOFILE for the current process. This is
// not needed per se, but rather to clean the origRlimitNofile cache in Go.
//
// The implementation relies on go.dev/cl/588076.
func ClearRlimitNofileCache(lim *unix.Rlimit) {
// Ignore the return values since we only need to clean the cache,
// the limit is going to be set via unix.Prlimit elsewhere.
_ = syscall.Setrlimit(syscall.RLIMIT_NOFILE, lim)
}

0 comments on commit 76a6a86

Please sign in to comment.