go: syscall: caching RLIMIT_NOFILE is wrong implementation
Go version
go version go1.20.13 linux/amd64
Output of go env in your module/workspace:
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.13"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1954436029=/tmp/go-build -gno-record-gcc-switches"
What did you do?
RLIMIT_NOFILE is cached in the go runtime, and RLIMIT_NOFILE in the cache is restored for the child process before exec.
Assume that the system’s default RLIMIT_NOFILE is 1024, and process A generates child process B through fork. B is a go application, so the go runtime will cache RLIMIT_NOFILE as 1024. When I set B’s RLIMIT_NOFILE to 10000 through prlimit in A, and use exec.Command to create process C in B, C’s RLIMIT_NOFILE is restored to 1024.
This behavior is incompatible with the operating system. It also caused problems with runc: https://github.com/opencontainers/runc/issues/4195
The problem originates from https://github.com/golang/go/commit/f5eef58e4381259cbd84b3f2074c79607fb5c821
What did you see happen?
Reference https://github.com/opencontainers/runc/issues/4195
What did you expect to see?
RLIMIT_NOFILE should be correctly inherited to child processes
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 35 (29 by maintainers)
Commits related to this issue
- syscall: Setrlimit: always clean rlimitNofileCache Since the introduction of origRlimitNofileCache in CL 476097 the only way to disable restoring RLIMIT_NOFILE before calling execve syscall (os.Start... — committed to golang/go by kolyshkin 2 years ago
- syscall: rm go:linkname from origRlimitNofile Since CL 588076 runc can do fine without the kludge. The code accessing the symbol is now guarded with `go:build !go1.23` in all supported runc branches ... — committed to golang/go by kolyshkin 2 years ago
- syscall: honor prlimit set by a different process On Linux one process can call prlimit to change the resource limit of another process. With this change we treat that as though the current process c... — committed to sthagen/golang-go by ianlancetaylor 2 years ago
I’m sorry that you don’t feel like a welcome member of the community. Certainly my goal is to work with tools like runc to find solutions that will work for the whole community.
Using
go:linknameto reach into the runtime is an example of something that simply doesn’t work for the whole community. That doesn’t mean that the problems can’t be solved. It means thatgo:linknameis the wrong solution.I stand by my claim that using
prlimitto change the rlimit of a different process is a very rare operation. As I’ve said, I am certainly open to figuring out a way to make this work for you. After all, this issue is still open. But I don’t think the answer is “the way that we do things is the only way that matters.” There are other cases that matter, whether we consider Go to be a systems language or not.For
RLIMIT_NOFILEthe Go runtime is trying to handle a real problem that real programs were running into in practice (#46279). The caching to reset the limit for child processes, also discussed on that issue, is also there to handle a real problem. And you also have a real problem. So let’s figure out a way to solve all those problems, rather than saying that some of them don’t matter.Go is advertised as a systems language and in 2013-ish the decision was made to write Docker in Go, which lead to a lot of large projects using Go (such as most of the container ecosystem – I would argue that Docker’s usage of Go helped Go’s popularity in the early days). Many of those projects use runc’s code directly or use runc (because runc is by far the most commonly used container runtime). Deprecating it wouldn’t make any sense.
The “maybe Go isn’t the right language for this” ship has long since sailed. If we could travel back to 2013, I would argue that Go is the wrong language to use – but at the time Go was the only language that made sense to use (Rust wasn’t ready, and writing Docker in C/C++ would’ve lead to more security issues because Docker is a privileged daemon that has to deal with far more things than runc).
FWIW my experience is that Go isn’t really a “systems language”, it’s more of a language for writing tools and web servers that don’t require a lot of fine-grained control that system languages provide. But that isn’t what it was advertised as in 2013, and even today Go is still sold as a “systems language” (it seems obvious now that that term means different things to different people).
When I submit bug reports as a runc maintainer, I’m more than aware that we are not seen as a welcome member of the Go community because we are always seen as “doing weird things” (such as being put on the “wall of shame” for trying to work around problems in the Go runtime that are not documented and took us ages to figure out how to fix). I try to avoid opening bug reports for these kinds of issues because I know that they will just be ignored. Being told “maybe you should just shut up shop and go somewhere else” is a little hurtful, to be honest.
Sure, but adding an additional basket of possible vulnerabilities isn’t a good thing. We have to do a lot of parsing of data and other operations where memory safety helps us avoid dumb security issues. I suspect that most programs could have a large number of security issues from non-memory-safety bugs, but I’m sure we agree that memory safe languages should be used when possible? Fewer possible bugs is better, right?
For this particular issue, it seems very strange to argue that a process doing
prlimiton your program is an “unusual” thing to happen for programs written in a “systems language”. But again, I guess we have different definitions of what a systems language is.@cyphar Thanks for the suggestion. What do you think of https://go.dev/cl/607516?
Quite agree.
I think cyphar’s original mean is that Runc maintainers always submit issues which are difficult to resolve in go community, and then we had to decide to using some weird ways to fix them in my own, for example
go:linkname. I think this is a figure of speech, not a real feeling. We know thatgo:linknameis not recommended, but we just only take it for a short term solution to give golang community more time to fix. Yes, we didn’t want to submit these issues for no reasons, because that are really problems met by runc. The goal of submitted them to golang community is to fetch some possible helps from others. I think golang maintainers gave us lots of solutions, though some of solutions are not perfect. We should say thanks to these nice persons. We understand that these issues filed by us are not critical for golang, golang maintainers have lots of more important things to do. So we should have a thorough discussion instead of rushing to fix it with imperfect methods, though these are not cared by other golang users.For this specific issue, I think the original implementation(#46279) is not recommended. This is the core reason of this discuss. I think that if we want to raise the soft limit of nofile, we should let the users to do this, we should not do it silently in runtime. It’s the responsibility of the user, but not the runtime. I think a language runtime should provide more and more controllable functions for the users, but not make a decision for them. Once we have a wrong beginning, we need to add more imperfect implementations to fix some unimportant issues, it’s not worth to do. My Suggest way: I think we should raise this soft limit in their own tools when they need to do it, for example:
cmd/gofmt,cmd/go, but not in go runtime. Thanks.@cyphar If
os/execcould configure the child’s rlimits before execing it (via some new fields insyscall.SysProcAttr), would that eliminate runc’s need to useprlimit?One other issue is that Go runtime’s caching of
RLIMIT_NOFILEis based on the assumption thatsetrlimitis the only way to set the rlimits of a process. However,prlimitlets you set limits from a different process, and doing this doesn’t update the cache because the Go runtime doesn’t have any way of knowing that it happened (this is what we do in runc).This causes several issues (one of which is the series of race conditions and logic issues we have now fixed in runc with stdlib workarounds by telling the Go runtime that we plan to change our
RLIMIT_NOFILEwith a dummySetRlimitcall) but the general takeaway I have is that this auto-adjustment logic isn’t appropriate for general-purpose Go programs.