aboutsummaryrefslogtreecommitdiff
path: root/src/os
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2024-09-06 12:19:01 -0700
committerGopher Robot <gobot@golang.org>2024-09-10 22:35:31 +0000
commit8926ca9c5ec3ea0b51e413e87f737aeb1422ea48 (patch)
tree2000b0ed00d376c91303d4dfbd9563b9f82a4cc1 /src/os
parentcdca67185573ca94734249584afd6cd2d3d58a84 (diff)
downloadgo-8926ca9c5ec3ea0b51e413e87f737aeb1422ea48.tar.xz
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>
Diffstat (limited to 'src/os')
-rw-r--r--src/os/pidfd_linux_test.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/os/pidfd_linux_test.go b/src/os/pidfd_linux_test.go
index fa0877037b..6b10798dd4 100644
--- a/src/os/pidfd_linux_test.go
+++ b/src/os/pidfd_linux_test.go
@@ -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])
+ }
+}