mirror of
https://github.com/golang/go
synced 2024-11-11 18:11:40 -07:00
syscall: on exec failure, close pidfd
Fixes #69284 Change-Id: I6350209302778ba5e44fa03d0b9e680d2b4ec192 Reviewed-on: https://go-review.googlesource.com/c/go/+/611495 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: roger peppe <rogpeppe@gmail.com> Reviewed-by: Tim King <taking@google.com> Auto-Submit: Ian Lance Taylor <iant@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
This commit is contained in:
parent
cdca671855
commit
8926ca9c5e
@ -9,6 +9,7 @@ import (
|
||||
"internal/syscall/unix"
|
||||
"internal/testenv"
|
||||
"os"
|
||||
"os/exec"
|
||||
"syscall"
|
||||
"testing"
|
||||
)
|
||||
@ -89,3 +90,58 @@ func TestStartProcessWithPidfd(t *testing.T) {
|
||||
t.Errorf("SendSignal: got %v, want %v", err, syscall.ESRCH)
|
||||
}
|
||||
}
|
||||
|
||||
// Issue #69284
|
||||
func TestPidfdLeak(t *testing.T) {
|
||||
exe := testenv.Executable(t)
|
||||
|
||||
// Find the next 10 descriptors.
|
||||
// We need to get more than one descriptor in practice;
|
||||
// the pidfd winds up not being the next descriptor.
|
||||
const count = 10
|
||||
want := make([]int, count)
|
||||
for i := range count {
|
||||
var err error
|
||||
want[i], err = syscall.Open(exe, syscall.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Close the descriptors.
|
||||
for _, d := range want {
|
||||
syscall.Close(d)
|
||||
}
|
||||
|
||||
// Start a process 10 times.
|
||||
for range 10 {
|
||||
// For testing purposes this has to be an absolute path.
|
||||
// Otherwise we will fail finding the executable
|
||||
// and won't start a process at all.
|
||||
cmd := exec.Command("/noSuchExecutable")
|
||||
cmd.Run()
|
||||
}
|
||||
|
||||
// Open the next 10 descriptors again.
|
||||
got := make([]int, count)
|
||||
for i := range count {
|
||||
var err error
|
||||
got[i], err = syscall.Open(exe, syscall.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Close the descriptors
|
||||
for _, d := range got {
|
||||
syscall.Close(d)
|
||||
}
|
||||
|
||||
t.Logf("got %v", got)
|
||||
t.Logf("want %v", want)
|
||||
|
||||
// Allow some slack for runtime epoll descriptors and the like.
|
||||
if got[count-1] > want[count-1]+5 {
|
||||
t.Errorf("got descriptor %d, want %d", got[count-1], want[count-1])
|
||||
}
|
||||
}
|
||||
|
@ -293,3 +293,8 @@ childerror:
|
||||
RawSyscall(SYS_EXIT, 253, 0, 0)
|
||||
}
|
||||
}
|
||||
|
||||
// forkAndExecFailureCleanup cleans up after an exec failure.
|
||||
func forkAndExecFailureCleanup(attr *ProcAttr, sys *SysProcAttr) {
|
||||
// Nothing to do.
|
||||
}
|
||||
|
@ -317,3 +317,8 @@ childerror:
|
||||
RawSyscall(SYS_EXIT, 253, 0, 0)
|
||||
}
|
||||
}
|
||||
|
||||
// forkAndExecFailureCleanup cleans up after an exec failure.
|
||||
func forkAndExecFailureCleanup(attr *ProcAttr, sys *SysProcAttr) {
|
||||
// Nothing to do.
|
||||
}
|
||||
|
@ -314,6 +314,11 @@ childerror:
|
||||
}
|
||||
}
|
||||
|
||||
// forkAndExecFailureCleanup cleans up after an exec failure.
|
||||
func forkAndExecFailureCleanup(attr *ProcAttr, sys *SysProcAttr) {
|
||||
// Nothing to do.
|
||||
}
|
||||
|
||||
func ioctlPtr(fd, req uintptr, arg unsafe.Pointer) (err Errno) {
|
||||
return ioctl(fd, req, uintptr(arg))
|
||||
}
|
||||
|
@ -289,3 +289,8 @@ childerror:
|
||||
rawSyscall(abi.FuncPCABI0(libc_exit_trampoline), 253, 0, 0)
|
||||
}
|
||||
}
|
||||
|
||||
// forkAndExecFailureCleanup cleans up after an exec failure.
|
||||
func forkAndExecFailureCleanup(attr *ProcAttr, sys *SysProcAttr) {
|
||||
// Nothing to do.
|
||||
}
|
||||
|
@ -750,3 +750,11 @@ func writeUidGidMappings(pid int, sys *SysProcAttr) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// forkAndExecFailureCleanup cleans up after an exec failure.
|
||||
func forkAndExecFailureCleanup(attr *ProcAttr, sys *SysProcAttr) {
|
||||
if sys.PidFD != nil && *sys.PidFD != -1 {
|
||||
Close(*sys.PidFD)
|
||||
*sys.PidFD = -1
|
||||
}
|
||||
}
|
||||
|
@ -237,6 +237,10 @@ func forkExec(argv0 string, argv []string, attr *ProcAttr) (pid int, err error)
|
||||
for err1 == EINTR {
|
||||
_, err1 = Wait4(pid, &wstatus, 0, nil)
|
||||
}
|
||||
|
||||
// OS-specific cleanup on failure.
|
||||
forkAndExecFailureCleanup(attr, sys)
|
||||
|
||||
return 0, err
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user