-
Notifications
You must be signed in to change notification settings - Fork 0
/
subprocess_windows.go
421 lines (377 loc) · 11.1 KB
/
subprocess_windows.go
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package jasper
import (
"fmt"
"syscall"
"time"
"unsafe"
)
// TODO: needs some documentation.
const (
// Constants for error codes
ERROR_SUCCESS syscall.Errno = 0
ERROR_FILE_NOT_FOUND syscall.Errno = 2
ERROR_ACCESS_DENIED syscall.Errno = 5
ERROR_INVALID_HANDLE syscall.Errno = 6
ERROR_INVALID_PARAMETER syscall.Errno = 87
DELETE = 0x00010000
READ_CONTROL = 0x00020000
WRITE_DAC = 0x00040000
WRITE_OWNER = 0x00080000
SYNCHRONIZE = 0x00100000
STANDARD_RIGHTS_REQUIRED = 0x000F0000
STANDARD_RIGHTS_READ = READ_CONTROL
STANDARD_RIGHTS_WRITE = READ_CONTROL
STANDARD_RIGHTS_EXECUTE = READ_CONTROL
STANDARD_RIGHTS_ALL = 0x001F0000
SPECIFIC_RIGHTS_ALL = 0x0000FFFF
ACCESS_SYSTEM_SECURITY = 0x01000000
MAXIMUM_ALLOWED = 0x02000000
// Constants for process permissions
PROCESS_TERMINATE = 0x0001
PROCESS_CREATE_THREAD = 0x0002
PROCESS_SET_SESSIONID = 0x0004
PROCESS_VM_OPERATION = 0x0008
PROCESS_VM_READ = 0x0010
PROCESS_VM_WRITE = 0x0020
PROCESS_DUP_HANDLE = 0x0040
PROCESS_CREATE_PROCESS = 0x0080
PROCESS_SET_QUOTA = 0x0100
PROCESS_SET_INFORMATION = 0x0200
PROCESS_QUERY_INFORMATION = 0x0400
PROCESS_SUSPEND_RESUME = 0x0800
PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
PROCESS_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0xFFFF
// Constants for job object limits
JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE = 0x2000
JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION = 0x400
JOB_OBJECT_LIMIT_ACTIVE_PROCESS = 8
JOB_OBJECT_LIMIT_JOB_MEMORY = 0x200
JOB_OBJECT_LIMIT_JOB_TIME = 4
JOB_OBJECT_LIMIT_PROCESS_MEMORY = 0x100
JOB_OBJECT_LIMIT_PROCESS_TIME = 2
JOB_OBJECT_LIMIT_WORKINGSET = 1
JOB_OBJECT_LIMIT_AFFINITY = 0x00000010
JobObjectInfoClassNameBasicProcessIdList = 3
JobObjectInfoClassNameExtendedLimitInformation = 9
// Constants for exit codes
STILL_ACTIVE = 0x103
// Constants for access rights for event objects
EVENT_ALL_ACCESS = 0x1F0003
EVENT_MODIFY_STATE = 0x0002
// Constants representing the wait return value
WAIT_OBJECT_0 uint32 = 0x00000000
WAIT_ABANDONED uint32 = 0x00000080
WAIT_FAILED uint32 = 0xFFFFFFFF
WAIT_TIMEOUT uint32 = 0x00000102
// Max allowed length of process ID list
MAX_PROCESS_ID_LIST_LENGTH = 1000
)
var (
modkernel32 = syscall.NewLazyDLL("kernel32.dll")
procAssignProcessToJobObject = modkernel32.NewProc("AssignProcessToJobObject")
procCloseHandle = modkernel32.NewProc("CloseHandle")
procCreateJobObjectW = modkernel32.NewProc("CreateJobObjectW")
procCreateEventW = modkernel32.NewProc("CreateEventW")
procOpenEventW = modkernel32.NewProc("OpenEventW")
procSetEvent = modkernel32.NewProc("SetEvent")
procGetExitCodeProcess = modkernel32.NewProc("GetExitCodeProcess")
procOpenProcess = modkernel32.NewProc("OpenProcess")
procTerminateProcess = modkernel32.NewProc("TerminateProcess")
procQueryInformationJobObject = modkernel32.NewProc("QueryInformationJobObject")
procTerminateJobObject = modkernel32.NewProc("TerminateJobObject")
procSetInformationJobObject = modkernel32.NewProc("SetInformationJobObject")
procWaitForSingleObject = modkernel32.NewProc("WaitForSingleObject")
)
type JobObject struct {
handle syscall.Handle
}
func NewWindowsJobObject(name string) (*JobObject, error) {
utf16Name, err := syscall.UTF16PtrFromString(name)
if err != nil {
return nil, NewWindowsError("UTF16PtrFromString", err)
}
hJob, err := CreateJobObject(utf16Name)
if err != nil {
return nil, NewWindowsError("CreateJobObject", err)
}
if err := SetInformationJobObjectExtended(hJob, JobObjectExtendedLimitInformation{
BasicLimitInformation: JobObjectBasicLimitInformation{
LimitFlags: JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE,
},
}); err != nil {
return nil, NewWindowsError("SetInformationJobObject", err)
}
return &JobObject{handle: hJob}, nil
}
func (j *JobObject) AssignProcess(pid uint) error {
hProcess, err := OpenProcess(PROCESS_ALL_ACCESS, false, uint32(pid))
if err != nil {
return NewWindowsError("OpenProcess", err)
}
defer CloseHandle(hProcess)
if err := AssignProcessToJobObject(j.handle, hProcess); err != nil {
return NewWindowsError("AssignProcessToJobObject", err)
}
return nil
}
func (j *JobObject) Terminate(exitCode uint) error {
if err := TerminateJobObject(j.handle, uint32(exitCode)); err != nil {
return NewWindowsError("TerminateJobObject", err)
}
return nil
}
func (j *JobObject) Close() error {
if j.handle != 0 {
if err := CloseHandle(j.handle); err != nil {
return NewWindowsError("CloseHandle", err)
}
j.handle = 0
}
return nil
}
///////////////////////////////////////////////////////////////////////////////////////////
//
// All the methods below are boilerplate functions for accessing the Windows syscalls.
//
///////////////////////////////////////////////////////////////////////////////////////////
type IoCounters struct {
ReadOperationCount uint64
WriteOperationCount uint64
OtherOperationCount uint64
ReadTransferCount uint64
WriteTransferCount uint64
OtherTransferCount uint64
}
type JobObjectBasicLimitInformation struct {
PerProcessUserTimeLimit uint64
PerJobUserTimeLimit uint64
LimitFlags uint32
MinimumWorkingSetSize uintptr
MaximumWorkingSetSize uintptr
ActiveProcessLimit uint32
Affinity uintptr
PriorityClass uint32
SchedulingClass uint32
}
type JobObjectExtendedLimitInformation struct {
BasicLimitInformation JobObjectBasicLimitInformation
IoInfo IoCounters
ProcessMemoryLimit uintptr
JobMemoryLimit uintptr
PeakProcessMemoryUsed uintptr
PeakJobMemoryUsed uintptr
}
func OpenProcess(desiredAccess uint32, inheritHandle bool, processId uint32) (syscall.Handle, error) {
var inheritHandleRaw int32
if inheritHandle {
inheritHandleRaw = 1
} else {
inheritHandleRaw = 0
}
r1, _, e1 := procOpenProcess.Call(
uintptr(desiredAccess),
uintptr(inheritHandleRaw),
uintptr(processId))
if r1 == 0 {
if e1 != ERROR_SUCCESS {
return 0, e1
} else {
return 0, syscall.EINVAL
}
}
return syscall.Handle(r1), nil
}
func GetExitCodeProcess(handle syscall.Handle, exitCode *uint32) error {
r1, _, e1 := procGetExitCodeProcess.Call(uintptr(handle), uintptr(unsafe.Pointer(exitCode)))
if r1 == 0 {
if e1 != ERROR_SUCCESS {
return e1
} else {
return syscall.EINVAL
}
}
return nil
}
func TerminateProcess(handle syscall.Handle, exitCode uint32) error {
r1, _, e1 := procTerminateProcess.Call(uintptr(handle), uintptr(exitCode))
if r1 == 0 {
if e1 != ERROR_SUCCESS {
return e1
} else {
return syscall.EINVAL
}
}
return nil
}
type SecurityAttributes struct {
Length uint32
SecurityDescriptor uintptr
InheritHandle uint32
}
func CreateJobObject(name *uint16) (syscall.Handle, error) {
jobAttributes := &SecurityAttributes{}
r1, _, e1 := procCreateJobObjectW.Call(
uintptr(unsafe.Pointer(jobAttributes)),
uintptr(unsafe.Pointer(name)))
if r1 == 0 {
if e1 != ERROR_SUCCESS {
return 0, e1
} else {
return 0, syscall.EINVAL
}
}
return syscall.Handle(r1), nil
}
func AssignProcessToJobObject(job syscall.Handle, process syscall.Handle) error {
r1, _, e1 := procAssignProcessToJobObject.Call(uintptr(job), uintptr(process))
if r1 == 0 {
if e1 != ERROR_SUCCESS {
return e1
} else {
return syscall.EINVAL
}
}
return nil
}
func QueryInformationJobObjectProcessIdList(job syscall.Handle) (*JobObjectBasicProcessIdList, error) {
info := JobObjectBasicProcessIdList{}
_, err := QueryInformationJobObject(
job,
JobObjectInfoClassNameBasicProcessIdList,
unsafe.Pointer(&info),
uint32(unsafe.Sizeof(info)),
)
if err != nil {
return nil, err
}
return &info, nil
}
func QueryInformationJobObject(job syscall.Handle, infoClass uint32, info unsafe.Pointer, length uint32) (uint32, error) {
var nLength uint32
r1, _, e1 := procQueryInformationJobObject.Call(
uintptr(job),
uintptr(infoClass),
uintptr(info),
uintptr(length),
uintptr(unsafe.Pointer(&nLength)),
)
if r1 == 0 {
if e1 != ERROR_SUCCESS {
return 0, e1
} else {
return 0, syscall.EINVAL
}
}
return nLength, nil
}
func SetInformationJobObjectExtended(job syscall.Handle, info JobObjectExtendedLimitInformation) error {
r1, _, e1 := procSetInformationJobObject.Call(uintptr(job), JobObjectInfoClassNameExtendedLimitInformation,
uintptr(unsafe.Pointer(&info)),
uintptr(uint32(unsafe.Sizeof(info))))
if r1 == 0 {
if e1 != ERROR_SUCCESS {
return e1
} else {
return syscall.EINVAL
}
}
return nil
}
func WaitForSingleObject(object syscall.Handle, timeout time.Duration) (uint32, error) {
timeoutMillis := int64(timeout * time.Millisecond)
r1, _, e1 := procWaitForSingleObject.Call(
uintptr(object),
uintptr(uint32(timeoutMillis)),
)
waitStatus := uint32(r1)
if waitStatus == WAIT_FAILED {
if e1 != ERROR_SUCCESS {
return waitStatus, e1
} else {
return waitStatus, syscall.EINVAL
}
}
return waitStatus, nil
}
func CreateEvent(name *uint16) (syscall.Handle, error) {
r1, _, e1 := procCreateEventW.Call(
uintptr(unsafe.Pointer(nil)),
uintptr(1),
uintptr(0),
uintptr(unsafe.Pointer(name)),
)
handle := syscall.Handle(r1)
if r1 == 0 {
if e1 != ERROR_SUCCESS {
return handle, e1
} else {
return handle, syscall.EINVAL
}
}
return handle, nil
}
func OpenEvent(name *uint16) (syscall.Handle, error) {
r1, _, e1 := procOpenEventW.Call(
uintptr(EVENT_MODIFY_STATE),
uintptr(0),
uintptr(unsafe.Pointer(name)),
)
handle := syscall.Handle(r1)
if r1 == 0 {
if e1 != ERROR_SUCCESS {
return handle, e1
} else {
return handle, syscall.EINVAL
}
}
return handle, nil
}
func SetEvent(event syscall.Handle) error {
r1, _, e1 := procSetEvent.Call(uintptr(event))
if r1 == 0 {
if e1 != ERROR_SUCCESS {
return e1
} else {
return syscall.EINVAL
}
}
return nil
}
func TerminateJobObject(job syscall.Handle, exitCode uint32) error {
r1, _, e1 := procTerminateJobObject.Call(uintptr(job), uintptr(exitCode))
if r1 == 0 {
if e1 != ERROR_SUCCESS {
return e1
} else {
return syscall.EINVAL
}
}
return nil
}
func CloseHandle(object syscall.Handle) error {
r1, _, e1 := procCloseHandle.Call(uintptr(object))
if r1 == 0 {
if e1 != ERROR_SUCCESS {
return e1
} else {
return syscall.EINVAL
}
}
return nil
}
type WindowsError struct {
functionName string
innerError error
}
func NewWindowsError(functionName string, innerError error) *WindowsError {
return &WindowsError{functionName, innerError}
}
func (self *WindowsError) FunctionName() string {
return self.functionName
}
func (self *WindowsError) InnerError() error {
return self.innerError
}
func (self *WindowsError) Error() string {
return fmt.Sprintf("gowin32: %s failed: %v", self.functionName, self.innerError)
}