From f5eef58e4381259cbd84b3f2074c79607fb5c821 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Mon, 13 Mar 2023 20:59:14 -0700 Subject: syscall: restore original NOFILE rlimit in child process If we increased the NOFILE rlimit when starting the program, restore the original rlimit when forking a child process. For #46279 Change-Id: Ia5d2af9ef435e5932965c15eec2e428d2130d230 Reviewed-on: https://go-review.googlesource.com/c/go/+/476097 Reviewed-by: Bryan Mills Reviewed-by: Ian Lance Taylor TryBot-Bypass: Ian Lance Taylor --- src/syscall/exec_libc.go | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/syscall/exec_libc.go') diff --git a/src/syscall/exec_libc.go b/src/syscall/exec_libc.go index 0f8a7b5375..44557867eb 100644 --- a/src/syscall/exec_libc.go +++ b/src/syscall/exec_libc.go @@ -53,6 +53,7 @@ func getpid() (pid uintptr, err Errno) func ioctl(fd uintptr, req uintptr, arg uintptr) (err Errno) func setgid(gid uintptr) (err Errno) func setgroups1(ngid uintptr, gid uintptr) (err Errno) +func setrlimit1(which uintptr, lim unsafe.Pointer) (err Errno) func setsid() (pid uintptr, err Errno) func setuid(uid uintptr) (err Errno) func setpgid(pid uintptr, pgid uintptr) (err Errno) @@ -90,6 +91,8 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr ngroups, groups uintptr ) + rlim, rlimOK := origRlimitNofile.Load().(Rlimit) + // guard against side effects of shuffling fds below. // Make sure that nextfd is beyond any currently open files so // that we can't run the risk of overwriting any of them. @@ -292,6 +295,11 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr } } + // Restore original rlimit. + if rlimOK && rlim.Cur != 0 { + setrlimit1(RLIMIT_NOFILE, unsafe.Pointer(&rlim)) + } + // Time to exec. err1 = execve( uintptr(unsafe.Pointer(argv0)), -- cgit v1.3-5-g9baa