Skip to content

Commit 11b64b4

Browse files
zx2c4dmitshur
authored andcommitted
[release-branch.go1.17] syscall: do not use handle lists on windows when NoInheritHandles is true
If NoInheritHandles is passed, then we shouldn't attempt to do anything with handle lists. Otherwise CreateProcess fails with invalid param, because it's being told both to not inherit handles and to inherit certain handles. This commit fixes that by using the same logic for handle lists as it does for enabling or disabling handle inheritance. It also adds a test to make sure this doesn't regress again. Updates #48040 Fixes #48075 Change-Id: I507261baeec263091738ab90157a991d917dc92f Reviewed-on: https://go-review.googlesource.com/c/go/+/350416 Trust: Jason A. Donenfeld <[email protected]> Run-TryBot: Jason A. Donenfeld <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Patrik Nyblom <[email protected]>
1 parent 2ac3bdf commit 11b64b4

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/os/exec/exec_windows_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package exec_test
1010
import (
1111
"io"
1212
"os"
13+
"os/exec"
1314
"strconv"
1415
"syscall"
1516
"testing"
@@ -41,3 +42,16 @@ func TestPipePassing(t *testing.T) {
4142
t.Error(err)
4243
}
4344
}
45+
46+
func TestNoInheritHandles(t *testing.T) {
47+
cmd := exec.Command("cmd", "/c exit 88")
48+
cmd.SysProcAttr = &syscall.SysProcAttr{NoInheritHandles: true}
49+
err := cmd.Run()
50+
exitError, ok := err.(*exec.ExitError)
51+
if !ok {
52+
t.Fatalf("got error %v; want ExitError", err)
53+
}
54+
if exitError.ExitCode() != 88 {
55+
t.Fatalf("got exit code %d; want 88", exitError.ExitCode())
56+
}
57+
}

src/syscall/exec_windows.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,10 @@ func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle
390390
}
391391
fd = fd[:j]
392392

393+
willInheritHandles := len(fd) > 0 && !sys.NoInheritHandles
394+
393395
// Do not accidentally inherit more than these handles.
394-
if len(fd) > 0 {
396+
if willInheritHandles {
395397
err = updateProcThreadAttribute(si.ProcThreadAttributeList, 0, _PROC_THREAD_ATTRIBUTE_HANDLE_LIST, unsafe.Pointer(&fd[0]), uintptr(len(fd))*unsafe.Sizeof(fd[0]), nil, nil)
396398
if err != nil {
397399
return 0, 0, err
@@ -401,9 +403,9 @@ func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle
401403
pi := new(ProcessInformation)
402404
flags := sys.CreationFlags | CREATE_UNICODE_ENVIRONMENT | _EXTENDED_STARTUPINFO_PRESENT
403405
if sys.Token != 0 {
404-
err = CreateProcessAsUser(sys.Token, argv0p, argvp, sys.ProcessAttributes, sys.ThreadAttributes, len(fd) > 0 && !sys.NoInheritHandles, flags, createEnvBlock(attr.Env), dirp, &si.StartupInfo, pi)
406+
err = CreateProcessAsUser(sys.Token, argv0p, argvp, sys.ProcessAttributes, sys.ThreadAttributes, willInheritHandles, flags, createEnvBlock(attr.Env), dirp, &si.StartupInfo, pi)
405407
} else {
406-
err = CreateProcess(argv0p, argvp, sys.ProcessAttributes, sys.ThreadAttributes, len(fd) > 0 && !sys.NoInheritHandles, flags, createEnvBlock(attr.Env), dirp, &si.StartupInfo, pi)
408+
err = CreateProcess(argv0p, argvp, sys.ProcessAttributes, sys.ThreadAttributes, willInheritHandles, flags, createEnvBlock(attr.Env), dirp, &si.StartupInfo, pi)
407409
}
408410
if err != nil {
409411
return 0, 0, err

0 commit comments

Comments
 (0)