Commit e8def4047f for golang

commit e8def4047fd1089e0c01b244a3b61bc3093bc7cd
Author: Ayham Joumran <140757050+AyhamJo7@users.noreply.github.com>
Date:   Wed Nov 25 00:28:43 2026 +0000

    os: avoid kill(-1) when Signal races with Release

    When a Process has no handle, pidSignal checked p.Pid and the process
    status and then read p.Pid again for the call to kill. Release sets
    Pid to -1 without synchronization, so a Signal that raced with Release
    could pass the checks and then call kill(-1, sig), which signals every
    process that the caller has permission to signal.

    Read Pid once and use that value for the checks and for the call.
    A Signal that races with Release now either reports that the process
    was released or signals the original PID.

    Fixes #83341

    Change-Id: I5ff02b59ecf898775eb3a099a80a8f9ac0df4309
    GitHub-Last-Rev: 1a10c1be5045ee204fd479b6dff34f6d34f741e4
    GitHub-Pull-Request: golang/go#83365
    Reviewed-on: https://go-review.googlesource.com/c/go/+/867363
    Auto-Submit: Dmitri Shuralyov <dmitshur@google.com>
    Reviewed-by: David Chase <drchase@google.com>
    LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
    Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>

diff --git a/src/os/exec_unix.go b/src/os/exec_unix.go
index d2ceb0ceb5..d209c44936 100644
--- a/src/os/exec_unix.go
+++ b/src/os/exec_unix.go
@@ -91,10 +91,14 @@ func (p *Process) signal(sig Signal) error {
 }

 func (p *Process) pidSignal(s syscall.Signal) error {
-	if p.Pid == pidReleased {
+	// Read p.Pid only once. A concurrent Release sets it to pidReleased,
+	// and passing that value to kill would signal every process
+	// that we have permission to signal.
+	pid := p.Pid
+	if pid == pidReleased {
 		return errProcessReleased
 	}
-	if p.Pid == pidUnset {
+	if pid == pidUnset {
 		return errors.New("os: process not initialized")
 	}

@@ -108,7 +112,7 @@ func (p *Process) pidSignal(s syscall.Signal) error {
 		return errProcessReleased
 	}

-	return convertESRCH(syscall.Kill(p.Pid, s))
+	return convertESRCH(syscall.Kill(pid, s))
 }

 func convertESRCH(err error) error {
diff --git a/src/os/exec_unix_test.go b/src/os/exec_unix_test.go
index 960f5d8218..7280443fe3 100644
--- a/src/os/exec_unix_test.go
+++ b/src/os/exec_unix_test.go
@@ -8,12 +8,14 @@ package os_test

 import (
 	"errors"
+	"internal/race"
 	"internal/testenv"
 	"math"
 	. "os"
 	"runtime"
 	"syscall"
 	"testing"
+	"time"
 )

 func TestErrProcessDone(t *testing.T) {
@@ -87,3 +89,46 @@ func TestProcessBadPID(t *testing.T) {
 		t.Error("p.Signal succeeded unexpectedly")
 	}
 }
+
+// Test that a Signal that races with Release never passes the PID that
+// Release stores, -1, to kill: that signals every process that the
+// caller has permission to signal. See go.dev/issue/83341.
+func TestSignalReleaseRace(t *testing.T) {
+	if race.Enabled {
+		// Release writes the Pid field without synchronization.
+		// See the comment in Release.
+		t.Skip("skipping: Signal and Release race on Process.Pid")
+	}
+	t.Parallel()
+
+	// Signaling a nonexistent process fails, so Signal can only
+	// succeed if it signals some other PID. Signal 0 has no effect
+	// on the processes that kill(-1, 0) reaches.
+	// As in TestProcessAlreadyDone, assume that MaxInt32 is not in use.
+	//
+	// The Process must use the PID rather than a process handle,
+	// which FindProcess cannot be relied on to give us.
+	p := &Process{Pid: math.MaxInt32}
+
+	unlock := p.LockSignal()
+	errc := make(chan error, 1)
+	go func() {
+		errc <- p.Signal(syscall.Signal(0))
+	}()
+
+	// Give Signal time to check the PID and block.
+	// If it has not got that far the test passes without
+	// testing anything, but it cannot fail spuriously.
+	time.Sleep(10 * time.Millisecond)
+
+	// This is the first thing that Release does, and the only
+	// thing that it has done when a racing Signal goes wrong.
+	// Calling Release here would also mark the Process as released,
+	// which Signal checks after it has acquired the lock.
+	p.Pid = -1
+	unlock()
+
+	if err := <-errc; err == nil {
+		t.Errorf("Signal(0) of nonexistent process succeeded after Pid was set to -1")
+	}
+}
diff --git a/src/os/export_unix_test.go b/src/os/export_unix_test.go
index b8dcca0f8f..f99b86f757 100644
--- a/src/os/export_unix_test.go
+++ b/src/os/export_unix_test.go
@@ -7,3 +7,11 @@
 package os

 var SplitPath = splitPath
+
+// LockSignal blocks calls to p.Signal that use the PID, rather than a
+// process handle, after they have checked the PID and before they send
+// the signal. It returns a function that unblocks them.
+func (p *Process) LockSignal() (unlock func()) {
+	p.sigMu.Lock()
+	return p.sigMu.Unlock
+}