aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/go-patches/0006-syscall-introduce-SysProcAttr.ParentProcess-on-Windo.patch
blob: b7453be012b793fc38ad1b601c5e17425704ee75 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
From fd146509dda08eca30b2145deddb669b789da6e0 Mon Sep 17 00:00:00 2001
From: "Jason A. Donenfeld" <Jason@zx2c4.com>
Date: Sun, 31 Jan 2021 18:14:56 +0100
Subject: [PATCH 6/6] syscall: introduce SysProcAttr.ParentProcess on Windows

This allows users to specify which process should be used as the parent
process when creating a new process.

Note that this doesn't just trivially pass the handle onward to
PROC_THREAD_ATTRIBUTE_PARENT_PROCESS, because inherited handles must be
valid in the parent process, so if we're changing the destination
process, then we must also change the origin of the parent handles. And,
the StartProcess function must clean up these handles successfully when
exiting, regardless of where the duplication happened. So, we take care
in this commit to use DuplicateHandle for both duplicating and for
closing the inherited handles.

The test was taken originally from CL 288272 and adjusted for use here.

Fixes #44011.

Change-Id: Ib3b132028dcab1aded3dc0e65126c8abebfa35eb
---
 src/syscall/exec_windows.go      | 17 ++++++--
 src/syscall/exec_windows_test.go | 73 ++++++++++++++++++++++++++++++++
 2 files changed, 87 insertions(+), 3 deletions(-)

diff --git a/src/syscall/exec_windows.go b/src/syscall/exec_windows.go
index 958b11260f..89257be0a4 100644
--- a/src/syscall/exec_windows.go
+++ b/src/syscall/exec_windows.go
@@ -243,6 +243,7 @@ type SysProcAttr struct {
 	ThreadAttributes           *SecurityAttributes // if set, applies these security attributes as the descriptor for the main thread of the new process
 	NoInheritHandles           bool                // if set, each inheritable handle in the calling process is not inherited by the new process
 	AdditionalInheritedHandles []Handle            // a list of additional handles, already marked as inheritable, that will be inherited by the new process
+	ParentProcess              Handle              // if non-zero, the new process regards the process given by this handle as its parent process
 }
 
 var zeroProcAttr ProcAttr
@@ -312,18 +313,22 @@ func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle
 	}
 
 	p, _ := GetCurrentProcess()
+	parentProcess := p
+	if sys.ParentProcess != 0 {
+		parentProcess = sys.ParentProcess
+	}
 	fd := make([]Handle, len(attr.Files))
 	for i := range attr.Files {
 		if attr.Files[i] > 0 {
-			err := DuplicateHandle(p, Handle(attr.Files[i]), p, &fd[i], 0, true, DUPLICATE_SAME_ACCESS)
+			err := DuplicateHandle(p, Handle(attr.Files[i]), parentProcess, &fd[i], 0, true, DUPLICATE_SAME_ACCESS)
 			if err != nil {
 				return 0, 0, err
 			}
-			defer CloseHandle(Handle(fd[i]))
+			defer DuplicateHandle(parentProcess, fd[i], 0, nil, 0, false, DUPLICATE_CLOSE_SOURCE)
 		}
 	}
 	si := new(_STARTUPINFOEXW)
-	si.ProcThreadAttributeList, err = newProcThreadAttributeList(1)
+	si.ProcThreadAttributeList, err = newProcThreadAttributeList(2)
 	if err != nil {
 		return 0, 0, err
 	}
@@ -333,6 +338,12 @@ func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle
 		si.Flags |= STARTF_USESHOWWINDOW
 		si.ShowWindow = SW_HIDE
 	}
+	if sys.ParentProcess != 0 {
+		err = si.ProcThreadAttributeList.Add(_PROC_THREAD_ATTRIBUTE_PARENT_PROCESS, unsafe.Pointer(&sys.ParentProcess), unsafe.Sizeof(sys.ParentProcess))
+		if err != nil {
+			return 0, 0, err
+		}
+	}
 	si.StdInput = fd[0]
 	si.StdOutput = fd[1]
 	si.StdErr = fd[2]
diff --git a/src/syscall/exec_windows_test.go b/src/syscall/exec_windows_test.go
index eda1d36877..8a1c2ceaae 100644
--- a/src/syscall/exec_windows_test.go
+++ b/src/syscall/exec_windows_test.go
@@ -5,8 +5,14 @@
 package syscall_test
 
 import (
+	"fmt"
+	"io/ioutil"
+	"os"
+	"os/exec"
+	"path/filepath"
 	"syscall"
 	"testing"
+	"time"
 )
 
 func TestEscapeArg(t *testing.T) {
@@ -41,3 +47,70 @@ func TestEscapeArg(t *testing.T) {
 		}
 	}
 }
+
+func TestChangingProcessParent(t *testing.T) {
+	if os.Getenv("GO_WANT_HELPER_PROCESS") == "parent" {
+		// in parent process
+
+		// Parent does nothign. It is just used as a parent of a child process.
+		time.Sleep(time.Minute)
+		os.Exit(0)
+	}
+
+	if os.Getenv("GO_WANT_HELPER_PROCESS") == "child" {
+		// in child process
+		dumpPath := os.Getenv("GO_WANT_HELPER_PROCESS_FILE")
+		if dumpPath == "" {
+			fmt.Fprintf(os.Stderr, "Dump file path cannot be blank.")
+			os.Exit(1)
+		}
+		err := os.WriteFile(dumpPath, []byte(fmt.Sprintf("%d", os.Getppid())), 0644)
+		if err != nil {
+			fmt.Fprintf(os.Stderr, "Error writing dump file: %v", err)
+			os.Exit(2)
+		}
+		os.Exit(0)
+	}
+
+	// run parent process
+
+	parent := exec.Command(os.Args[0], "-test.run=TestChangingProcessParent")
+	parent.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=parent")
+	err := parent.Start()
+	if err != nil {
+		t.Fatal(err)
+	}
+	defer func() {
+		parent.Process.Kill()
+		parent.Wait()
+	}()
+
+	// run child process
+
+	const _PROCESS_CREATE_PROCESS = 0x0080
+	const _PROCESS_DUP_HANDLE = 0x0040
+	childDumpPath := filepath.Join(t.TempDir(), "ppid.txt")
+	ph, err := syscall.OpenProcess(_PROCESS_CREATE_PROCESS|_PROCESS_DUP_HANDLE|syscall.PROCESS_QUERY_INFORMATION,
+		false, uint32(parent.Process.Pid))
+	if err != nil {
+		t.Fatal(err)
+	}
+	defer syscall.CloseHandle(ph)
+
+	child := exec.Command(os.Args[0], "-test.run=TestChangingProcessParent")
+	child.Env = append(os.Environ(),
+		"GO_WANT_HELPER_PROCESS=child",
+		"GO_WANT_HELPER_PROCESS_FILE="+childDumpPath)
+	child.SysProcAttr = &syscall.SysProcAttr{ParentProcess: ph}
+	childOutput, err := child.CombinedOutput()
+	if err != nil {
+		t.Errorf("child failed: %v: %v", err, string(childOutput))
+	}
+	childOutput, err = ioutil.ReadFile(childDumpPath)
+	if err != nil {
+		t.Fatalf("reading child ouput failed: %v", err)
+	}
+	if got, want := string(childOutput), fmt.Sprintf("%d", parent.Process.Pid); got != want {
+		t.Fatalf("child output: want %q, got %q", want, got)
+	}
+}
-- 
2.30.0