Commit Graph

263 Commits

Author SHA1 Message Date
Jason A. Donenfeld
0f9fa26af8 windows: fix constant value of WTD_STATEACTION_VERIFY
A 0x01 was mistakenly changed to 0x10. This commit rectifies the issue,
while also adding a test to ensure this functionality is behaving as
expected.

Fixes golang/go#46906

Change-Id: I833fe03e69a71283b4f7abb6cf640574d03f5d24
Reviewed-on: https://go-review.googlesource.com/c/sys/+/331010
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2021-06-30 00:52:30 +00:00
Jason A. Donenfeld
7ecdf8ef09 windows: change CreateProcessAsUser from kernel32 to advapi32
While kernel32.dll works as a target, the documentation says advapi32,
so go with that to be safe:

https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessasuserw

Change-Id: I8e4a3693dad3759c3a094fa2476b89fb3d38b6bd
Reviewed-on: https://go-review.googlesource.com/c/sys/+/323789
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2021-06-01 08:02:50 +00:00
Jason A. Donenfeld
35b2ab0089 windows: add CreateProcessAsUser
The syscall package already has this, but this one does not, so add this
simple companion to CreateProcess.

Change-Id: I8533f91245630dcf39705ae56a22c1217871f968
Reviewed-on: https://go-review.googlesource.com/c/sys/+/322489
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2021-05-25 14:32:21 +00:00
Jason A. Donenfeld
6ca3eb03df windows: use Go-managed pointer list for ProcThreadAttributeList
It turns out that if you write Go pointers to Go memory, the Go compiler
must be involved so that it generates various calls to the GC in the
process. Letting Windows write Go pointers to Go memory violated this.

We fix this by having all the Windows-managed memory be just a boring
[]byte blob. Then, in order to prevent the GC from prematurely cleaning
up the pointers referenced by that []byte blob, or in the future moving
memory and attempting to fix up pointers, we copy the data to the
Windows heap and then maintain a little array of pointers that have been
used. Every time the Update function is called with a new pointer, we
make a copy and append it to the list.  Then, on Delete, we free the
pointers from the Windows heap.

Updates golang/go#44900.

Change-Id: I42340a93fd9f6b8d10340634cf833fd4559a5f4f
Reviewed-on: https://go-review.googlesource.com/c/sys/+/300369
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2021-05-21 09:01:06 +00:00
Jason A. Donenfeld
e8d321eab0 windows: add command line escaping wrappers around EscapeArg and CommandLineToArgv
DecomposeCommandLine makes CommandLineToArgv usable in an ordinary way.
There's actually a pure-Go version of this available as the private
os.commandLineToArgv function, which we could copy, but given this is
x/sys/windows, it seems best to stick to the actual Windows primitives
which will always remain current. Then, ComposeCommandLine is just a
simple wrapper around EscapeArg (which has no native win32 substitute).

Change-Id: Ia2c7ca2ded9e5713b281dade34639dfeacf1171c
Reviewed-on: https://go-review.googlesource.com/c/sys/+/319229
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2021-05-14 08:44:01 +00:00
Tobias Klauser
977fb72620 windows: add //go:build line to empty.s
Change-Id: I715846360d6f889c64b59348e3483ecf8a99f428
Reviewed-on: https://go-review.googlesource.com/c/sys/+/318212
Trust: Tobias Klauser <tobias.klauser@gmail.com>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2021-05-10 12:01:38 +00:00
Song Lim
8803ae5d13 windows/svc/mgr: fix misspelled word in service.go
fix misspelled word "service" in line 62

Change-Id: I646f073d5604c36775d27c478a7b4b007f70ff62
GitHub-Last-Rev: 79901a6ca3
GitHub-Pull-Request: golang/sys#107
Reviewed-on: https://go-review.googlesource.com/c/sys/+/314573
Trust: Alex Brainman <alex.brainman@gmail.com>
Trust: Emmanuel Odeke <emmanuel@orijtech.com>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2021-05-03 08:07:04 +00:00
Jason A. Donenfeld
5e06dd20ab windows: ensure SECURITY_DESCRIPTOR allocation is large enough
Self-relative security descriptors can sometimes be smaller than the
header size of the SECURITY_DESCRIPTOR struct, which is fine in C, but
checkptr complains about it, so instead, ensure that the allocation is
at least the size of the SECURITY_DESCRIPTOR struct.

This fixes this splat:

fatal error: checkptr: converted pointer straddles multiple allocations

goroutine 36 [running]:
runtime.throw(0x761c5f, 0x3a)
        /usr/lib/go/src/runtime/panic.go:1117 +0x79 fp=0xc00005dd90 sp=0xc00005dd60 pc=0x5fb8d9
runtime.checkptrAlignment(0xc0001a4020, 0x741860, 0x1)
        /usr/lib/go/src/runtime/checkptr.go:20 +0xc9 fp=0xc00005ddc0 sp=0xc00005dd90 pc=0x5c7729
golang.org/x/sys/windows.(*SECURITY_DESCRIPTOR).copySelfRelativeSecurityDescriptor(0x1b9ad638190, 0x2)
        /home/zx2c4/Projects/golang-dev/sys/windows/security_windows.go:1359 +0x1b7 fp=0xc00005de50 sp=0xc00005ddc0 pc=0x6f0077
golang.org/x/sys/windows.SecurityDescriptorFromString(0x754f2b, 0x2, 0x0, 0x0, 0x0)
        /home/zx2c4/Projects/golang-dev/sys/windows/security_windows.go:1371 +0xde fp=0xc00005deb8 sp=0xc00005de50 pc=0x6f019e

Change-Id: I552017a93d4ca6f6debc6f8f445dac5c6717fed1
Reviewed-on: https://go-review.googlesource.com/c/sys/+/307129
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Josh Bleecher Snyder <josharian@gmail.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
2021-04-03 16:11:42 +00:00
Jason A. Donenfeld
700132347e windows: allocate SECURITY_DESCRIPTOR with uintptr alignment
This struct has pointers in it, which means checkptr expects for it to
be aligned properly. When we're copying a Windows-allocated struct to a
Go-allocated one, make sure that the Go allocation is aligned to the
pointer size.

This fixes the following checkptr splat:

goroutine 29 [running]:
runtime.throw(0x4f8dd9, 0x3a)
	C:/hostedtoolcache/windows/go/1.16.2/x64/src/runtime/panic.go:1117 +0x79 fp=0xc000041c50 sp=0xc000041c20 pc=0x2f9879
runtime.checkptrAlignment(0xc00009c180, 0x4d8a00, 0x1)
	C:/hostedtoolcache/windows/go/1.16.2/x64/src/runtime/checkptr.go:20 +0xc9 fp=0xc000041c80 sp=0xc000041c50 pc=0x2c4d09
golang.org/x/sys/windows.(*SECURITY_DESCRIPTOR).copySelfRelativeSecurityDescriptor(0x227284caa00, 0x2)
	C:/Users/runneradmin/go/pkg/mod/golang.org/x/sys@v0.0.0-20210309040221-94ec62e08169/windows/security_windows.go:1347 +0x11f fp=0xc000041ce8 sp=0xc000041c80 pc=0x467f9f
golang.org/x/sys/windows.SecurityDescriptorFromString(0x4ebfb0, 0x2, 0x0, 0x0, 0x0)

Change-Id: I7eb9c07e7afb7f139473b660f82a23541663ec50
Reviewed-on: https://go-review.googlesource.com/c/sys/+/306889
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Josh Bleecher Snyder <josharian@gmail.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
2021-04-02 19:21:33 +00:00
Tobias Klauser
c6e025ad80 windows: add IP_HDRINCL, IP_PKTINFO, IPV6_PKTINFO
These will be used to replace the respective local definitions in
the golang.org/x/net package.

Change-Id: Ieae8908a87ee7df1ca43683e7e921128bff90b8d
Reviewed-on: https://go-review.googlesource.com/c/sys/+/301690
Trust: Tobias Klauser <tobias.klauser@gmail.com>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2021-03-15 16:08:23 +00:00
Jason A. Donenfeld
94ec62e081 windows: add SetNamedPipeHandleState
I added GetNamedPipeHandleState before but forgot the setter function,
which winds up being useful for namedpipe's tests.

Change-Id: Iefeabb2ac9584ec15e18e2ae7e4a72fbb46cbe7c
Reviewed-on: https://go-review.googlesource.com/c/sys/+/299209
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2021-03-09 04:02:21 +00:00
Jason A. Donenfeld
8fe3ee5dd7 windows: return ERROR_ALREADY_EXIST for non-nil handle in Create* functions
These functions sometimes return a non-nil value, while indicating
ERROR_ALREADY_EXISTS through GetLastError. In this case, return the
error along with the handle, to indicate the case.

Fixes golang/go#44539.

Change-Id: Id18e64724e57f62a34d6562511d9eafa8e6f9a3e
Reviewed-on: https://go-review.googlesource.com/c/sys/+/297332
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2021-03-05 23:01:14 +00:00
Jason A. Donenfeld
5cdee2b1b5 windows: correct NT definitions and add more resolution
Alex pointed out that some of these original types were incomplete, so
this commit fills things out a bit more. We also add more to the test
case.

Change-Id: Iff7bd9ea6e0d021ba997c6d7351fe7c4a46187a9
Reviewed-on: https://go-review.googlesource.com/c/sys/+/299189
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2021-03-05 21:54:15 +00:00
Alex Brainman
d6cf4ab887 windows/svc: use ioutil.WriteFile instead of os.WriteFile
os.WriteFile is not available on Go 1.15. But we still need to
support Go 1.15.

Fixes broken Go 1.15 build

https://build.golang.org/log/07b89b007d0aca2e97bc01cdf4c6a1d90e6732f9

Change-Id: I18433985ec82477160058ef96c160345304ba1ad
Reviewed-on: https://go-review.googlesource.com/c/sys/+/299309
Trust: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2021-03-05 21:05:45 +00:00
Jason A. Donenfeld
7844c3c200 windows/svc: rewrite IsWindowsService
Alex copied this from a temporary thing I had written for the runtime
package. In runtime, you can't really access other packages like
syscall, so everything has to be very manual. But in x/sys, we can do
things properly. So this reimplements the function in a more straight
forward way.

Change-Id: I1634904bb1e10f33252954ce02d4b17ae56592e5
Reviewed-on: https://go-review.googlesource.com/c/sys/+/298830
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2021-03-05 03:40:16 +00:00
Jason A. Donenfeld
61f932b4c6 windows: add QueryFullProcessImageName
This will be useful for reworking the IsWindowsService function in
svc/.

Change-Id: Ie1f7a560d7e81ee4ac3055e04748b6f9c8234edb
Reviewed-on: https://go-review.googlesource.com/c/sys/+/298829
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2021-03-05 03:01:59 +00:00
Jason A. Donenfeld
0d6cb8bd5a windows: add ole32 instantiation functions
These are basic functions for getting function pointers to instantiated
OLE objects.

Change-Id: I5a3d179d6da2af006b6e11def18280959bb9bd98
Reviewed-on: https://go-review.googlesource.com/c/sys/+/298469
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2021-03-05 02:34:07 +00:00
Jason A. Donenfeld
f9bc61c02a windows: add resource extraction functions
These functions make it possible to read executable resource information
at runtime.

Change-Id: I00f260199ecda8daeb3417eaa9c02198663063b7
Reviewed-on: https://go-review.googlesource.com/c/sys/+/298173
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
2021-03-05 02:33:59 +00:00
Jason A. Donenfeld
7b4935edff windows: add pipe creation win32 APIs
These remaining APIs will make it possible to implement a win32 pipe
library using x/sys/windows.

Change-Id: If0cacedb7857c013c03e15cb6330b474cc010581
Reviewed-on: https://go-review.googlesource.com/c/sys/+/298172
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2021-03-04 20:38:40 +00:00
Jason A. Donenfeld
1243437a8e windows: add various NT APIs
In anticipation of the next commit which adds win32 pipe APIs, add some
of the foundational NT APIs for that, which will be required for making
a robust Go pipe library. Also add a simple test case.

Change-Id: I898bd6c5265a8939a7f05a24c4d9b22941dc56b7
Reviewed-on: https://go-review.googlesource.com/c/sys/+/298171
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2021-03-04 20:34:36 +00:00
Jason A. Donenfeld
444254391f windows: treat proc thread attribute lists as unsafe.Pointers
It turns out that the proc thread update function doesn't actually
allocate new memory for its arguments and instead just copies the
pointer values into the preallocated memory. Since we were allocating
that memory as []byte, the garbage collector didn't scan it for pointers
to Go allocations and freed them. We _could_ fix this by requiring that
all users of this use runtime.KeepAlive for everything they pass to the
update function, but that seems harder than necessary. Instead, we can
just do the allocation as []unsafe.Pointer, which means the GC can
operate as intended and not free these from beneath our feet. In order
to ensure this remains true, we also add a test for this.

Updates golang/go#44662.

Change-Id: Iaa8b694a6682cc1876879632c7ba068e47b8666d
Reviewed-on: https://go-review.googlesource.com/c/sys/+/297331
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Bryan C. Mills <bcmills@google.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2021-03-04 19:59:27 +00:00
Jason A. Donenfeld
afaa3650a9 windows: do not write LastStatus parameter of TEB when converting errors
The prior function updated the TEB's LastStatus member, which is not
what we want to be doing here. It's also not consistent with Microsoft's
own Go code for their pipe library, which properly uses the "NoTeb"
variant like this commit.

For good measure, we add a simple test case to make sure these paths are
being exercised.

Change-Id: I4080898f704bdc93a6048001b06ffce516fb412d
Reviewed-on: https://go-review.googlesource.com/c/sys/+/298169
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2021-03-04 15:22:09 +00:00
Jason A. Donenfeld
50617c2ba1 windows/mkwinsyscall: use "windows." prefix when referring to NTStatus
All references to "Errno" go through the syscalldot function, which
prepends "syscall." if the generation target is not in the syscall
package. Similarly so, we need a windowsdot function to prepend
"windows." to NTStatus.

Change-Id: I86affcf0155632ed5848ebabca97d175a041c6c3
Reviewed-on: https://go-review.googlesource.com/c/sys/+/298170
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2021-03-04 12:46:12 +00:00
Jason A. Donenfeld
78dc250343 windows/mkwinsyscall: deduplicate functions before printing variables
The same symbol name might be defined multiple times with different
function names or signatures. In that case, it's an error to redefine
the proc variable. So deduplicate these before printing.

Change-Id: I7a636cd44fb1ea6f51840ef3924d6316e266faa0
Reviewed-on: https://go-review.googlesource.com/c/sys/+/288332
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2021-03-04 12:45:23 +00:00
Jason A. Donenfeld
134d130e1a windows: add support for NTSTATUS values
The native NT API returns error values from a different namespace as the
usual Win32 one. This means it needs to be typed differently. This
commit adds broad support for using NTSTATUS values in a new type called
NTStatus.

First we add the type as a basic uint32. Then we add all of the
predefined constants from ntstatus.h, by augmenting mkerrors.bash to do
automatic extraction. There's a convenece way to convert an NT error to
a Win32 error, so we add the NTStatus.Errno() function. Since NTStatus
is an error type, we define an Error() function that returns a string by
asking ntdll.dll for its contents, in the exact same way that
syscall.Errno.Error() does, by calling FormatMessage. Since functions
need to actually use this, we add the rule that if a `//sys` declaration
returns an error value called "ntstatus", then the type underlying the
error interface is an NTStatus instead of an Errno. Finally we fix one
function that was returning an error interface of an Errno rather than
an NTStatus.

Change-Id: I06296b9563bbec526759d12a19f13ac6ad46dcc3
Reviewed-on: https://go-review.googlesource.com/c/sys/+/297330
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2021-03-03 07:41:36 +00:00
Jason A. Donenfeld
77cc2087c0 windows: refresh error constants to latest SDK errors
This is simply a re-run of mkerrors.bash with the newer Windows SDK.

Change-Id: I16077dce04e38ae23ffb938df1feed8fdadc96fa
Reviewed-on: https://go-review.googlesource.com/c/sys/+/297329
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2021-03-01 09:17:18 +00:00
Tobias Kohlbau
479acdf4ea windows: add support for CommTimeouts
CommTimeouts allows the user to set the timeout on Comm devices:

docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setcommtimeouts
docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getcommtimeouts

Change-Id: I756dc93b1b01412a496c6eccab22c9ff7e5f4b83
Reviewed-on: https://go-review.googlesource.com/c/sys/+/290209
Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
2021-02-28 01:22:17 +00:00
Alex Brainman
b0d1d43c01 windows/svc: make IsWindowsService handle parent exit
IsWindowsService fails when parent process has exited.

This change makes IsWindowsService return false instead.

Added a test to verify the change.

Change-Id: Ibb2aa9478e8afd9e35011bbdc0985bdf8f0af9cc
Reviewed-on: https://go-review.googlesource.com/c/sys/+/294729
TryBot-Result: Go Bot <gobot@golang.org>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Alex Brainman <alex.brainman@gmail.com>
2021-02-27 04:07:30 +00:00
Jason A. Donenfeld
f36f78243c windows: add definitions and functions for ProcThreadAttributeList
The bulk of the documentation for these is in:

https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-updateprocthreadattribute

This allows creating processes from scratch that use advanced options,
such as changing the process parent.

Fixes golang/go#44005.

Change-Id: Id5cc5400541e57710b9e888cd37ef4f925d510fe
Reviewed-on: https://go-review.googlesource.com/c/sys/+/288412
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2021-02-26 18:17:00 +00:00
Jason A. Donenfeld
683adc9d29 windows: do not overflow key memory in GetQueuedCompletionStatus
The third argument to GetQueuedCompletionStatus is a pointer to a
uintptr, not a uint32. Users of this functions have therefore been
corrupting their memory every time they used it. Either that memory
corruption was silent (dangerous), or their programs didn't work so they
chose a different API to use.

Updates golang/go#44538.

Change-Id: Ie1f66de11001cf9c8195afaa61f003a86f821a95
Reviewed-on: https://go-review.googlesource.com/c/sys/+/295174
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
2021-02-25 01:42:09 +00:00
Jason A. Donenfeld
eede4237b3 windows: add WSASocket
Simple function to complement the other ws2 functions we have:
https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsasocketw

Change-Id: Idf6b17360d84a8529e48ab94561c4ac7bee1d847
Reviewed-on: https://go-review.googlesource.com/c/sys/+/295175
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2021-02-23 21:21:15 +00:00
Bryan C. Mills
57ea560cfc windows: comment on fields omitted from the win32finddata1 struct
Updates golang/go#42637

Change-Id: I2e8c95af3cf2172c55bda6a15a749afcc6c45581
Reviewed-on: https://go-review.googlesource.com/c/sys/+/284172
Trust: Bryan C. Mills <bcmills@google.com>
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2021-02-19 17:28:41 +00:00
Boshi LIAN
3351caf150 windows: add CertFindCertificateInStore, CertFindChainInStore and CryptAcquireCertificatePrivateKey
add cert loading related syscall
CertFindCertificateInStore [1]
CertFindChainInStore [2]
CryptAcquireCertificatePrivateKey [3]

[1] https://docs.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-certfindcertificateinstore
[2] https://docs.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-certfindchaininstore
[3] https://docs.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-cryptacquirecertificateprivatekey

Change-Id: I179bffd06d714d729f3afd83687336edecae6b37
GitHub-Last-Rev: 58a8c666c8
GitHub-Pull-Request: golang/sys#94
Reviewed-on: https://go-review.googlesource.com/c/sys/+/281012
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
2021-02-19 17:27:56 +00:00
Jason A. Donenfeld
beda7e5e15 windows: port to ARM64
This also fixes up some spaces->tabs issues and moves build tags from
the file contents to the file name.

Updates golang/go#36439.

Change-Id: Iafa03e26575603a802832e587bb18ebd16fd176b
Reviewed-on: https://go-review.googlesource.com/c/sys/+/289449
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Russ Cox <rsc@golang.org>
2021-02-18 14:52:45 +00:00
Jason A. Donenfeld
22da62e12c windows: correct struct name and bool type
This fixes the name of a struct to match Microsoft's documentation.

It also reverts a change I made in the last patchset of CL 285714. The
idea there was that a Go bool would suffice for a win32 BOOL, because of
little endian. But in fact, a value of 0xff000000 would be treated as
false by Go but true by C, which is a problem. So this changes to using
a vanilla int32 type, which matches the C "int" used in the Microsoft
headers.

Change-Id: Id7cd306e916b3754e8dfe32bf11ec30cad3a13b4
Reviewed-on: https://go-review.googlesource.com/c/sys/+/285717
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2021-01-24 15:45:48 +00:00
Jason A. Donenfeld
1d476976d1 windows: add support for DPAPI
DPAPI allows machine or user-based blob encryption, using APIs from
crypt32:

https://docs.microsoft.com/en-us/windows/win32/api/dpapi/nf-dpapi-cryptunprotectdata
https://docs.microsoft.com/en-us/windows/win32/api/dpapi/nf-dpapi-cryptprotectdata

Change-Id: I57fb4f1877a5fb7629809e79111cbfa02b11cca3
Reviewed-on: https://go-review.googlesource.com/c/sys/+/285716
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2021-01-23 23:11:50 +00:00
Jason A. Donenfeld
07034700bc windows: add WinVerifyTrustEx function
This commit adds the function and the required structs for it.  This is
the same as the WinVerifyTrust function but has the more correct
signature.

https://docs.microsoft.com/en-us/windows/win32/api/wintrust/nf-wintrust-winverifytrustex

Change-Id: I43ae20302ba85a6ae1fc32ad4c34b59bee0a6a35
Reviewed-on: https://go-review.googlesource.com/c/sys/+/285715
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2021-01-23 23:11:35 +00:00
Jason A. Donenfeld
4df39788e6 windows: add missing crypt/cert API functions for examining certificate objects
This adds these functions and the various enums and constants required
to use them:
https://docs.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-cryptdecodeobject
https://docs.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-cryptqueryobject
https://docs.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-certgetnamestringw
https://docs.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-certfindextension

We also use this opportunity to clean up formatting of the previous
crypt APIs added: a few lines slipped in that used "//sys   ", with
spaces, instead of "//sys\t" with a tab. This fixes that up for
consistency.

Change-Id: I925d3d29b5255f70ee2368e8906c45b8b22115ce
Reviewed-on: https://go-review.googlesource.com/c/sys/+/285714
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
2021-01-23 23:10:56 +00:00
Jason A. Donenfeld
a8b976e07c windows: add Find*ChangeNotification APIs for file and directory monitoring
https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findfirstchangenotificationw
https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findnextchangenotification
https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findclosechangenotification

Change-Id: If7abfd63cd7a60e7f95ffad9aca2b7b7b74c94f0
Reviewed-on: https://go-review.googlesource.com/c/sys/+/285713
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2021-01-22 23:57:52 +00:00
Jason A. Donenfeld
04d7465088 windows: correct signature of recently added GetWindowThreadProcessId
This function returns 0 and sets last error on failure. While this isn't
said explicitly on MSDN, there's no PID 0, and trivial reverse
engineering shows that this is the case. For example:

.text:00000001800035ED loc_1800035ED:                          ; CODE XREF: GetWindowThreadProcessId+23↑j
.text:00000001800035ED                                         ; GetWindowThreadProcessId+3D↑j ...
.text:00000001800035ED                 mov     ecx, 578h       ; LastError
.text:00000001800035F2                 call    cs:__imp_RtlSetLastWin32Error
.text:00000001800035F9                 nop     dword ptr [rax+rax+00h]
.text:00000001800035FE
.text:00000001800035FE loc_1800035FE:                          ; CODE XREF: GetWindowThreadProcessId+65↑j
.text:00000001800035FE                 xor     eax, eax
.text:0000000180003600                 jmp     short loc_1800035AA

This function was also just added by a user who is likely its only
consumer, so this error is not too late to fix.

Change-Id: I5dd24e78c006686bb8f1288ad0fe63cd67df56a6
Reviewed-on: https://go-review.googlesource.com/c/sys/+/285272
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2021-01-22 09:31:01 +00:00
Alex Brainman
59c308dcf3 windows: use HWND type in MessageBox, GetShellWindow and GetWindowThreadProcessId
This matches usual win32api conventions. While we're at it, we
group together user32.dll functions together.

This CL was based on CL 282634 with all but MessageBox, GetShellWindow
and GetWindowThreadProcessId changes removed to prevent compatibility
break.

Change-Id: I7e17c581723c41580a49c5612cabc7a5c13c0f15
Reviewed-on: https://go-review.googlesource.com/c/sys/+/282972
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Trust: Alex Brainman <alex.brainman@gmail.com>
2021-01-12 09:13:31 +00:00
Alex Brainman
0df2131ae3 windows: add GetShellWindow and GetWindowThreadProcessId
I am trying to implement

https://devblogs.microsoft.com/oldnewthing/20190425-00/?p=102443

so I need these functions.

Change-Id: Id5082e4cc450569ffd021f4a300d56de325e4952
Reviewed-on: https://go-review.googlesource.com/c/sys/+/280717
Trust: Alex Brainman <alex.brainman@gmail.com>
Trust: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2021-01-08 17:29:13 +00:00
Boshi LIAN
2d18734c60 windows: add PFXImportCertStore and CertDuplicateCertificateContext
add 2 new cert related dll call
PFXImportCertStore [1]
CertDuplicateCertificateContext [2]

also, add missing flags for CertCloseStore [3]

[1] https://docs.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-pfximportcertstore
[2] https://docs.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-certduplicatecertificatecontext
[3] https://docs.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-certclosestore

Change-Id: Ia44100ddb2cac1c2a817932c859926e8183dcda0
GitHub-Last-Rev: f3cd41859d
GitHub-Pull-Request: golang/sys#93
Reviewed-on: https://go-review.googlesource.com/c/sys/+/273907
Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
2020-12-31 18:44:35 +00:00
Meng Zhuo
8ad439b19e windows: remove ErrorControl value check in CreateService
ErrorIgnore is 0 which means we shouldn't rely on zero value checking.

Fixes golang/go#43094

Change-Id: I7af3132cfe58d73c7abab92203fb02a0ada15218
Reviewed-on: https://go-review.googlesource.com/c/sys/+/276572
Trust: Meng Zhuo <mzh@golangcn.org>
Trust: Alex Brainman <alex.brainman@gmail.com>
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Run-TryBot: Meng Zhuo <mzh@golangcn.org>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2020-12-11 09:08:39 +00:00
Josh Bleecher Snyder
1f0c578a6b windows: add RegNotifyChangeKeyValue notifyFilter flags
Copied with permission (and CLA) from

https://git.zx2c4.com/wireguard-go/tree/tun/wintun/registry/registry_windows.go?id=5ca1218a5c16fb9b5e99b61c0b5758f66087e2e4

Change-Id: I42c1657b603f8494dadedacc8f7d719d39df9dc0
Reviewed-on: https://go-review.googlesource.com/c/sys/+/277153
Trust: Josh Bleecher Snyder <josharian@gmail.com>
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-12-11 00:26:50 +00:00
Josh Bleecher Snyder
7e3030f880 windows: add RegNotifyChangeKeyValue syscall
Change-Id: If1a1b6ad1935bfaacb9e6a99fb9ade056169ccb2
Reviewed-on: https://go-review.googlesource.com/c/sys/+/277152
Trust: Josh Bleecher Snyder <josharian@gmail.com>
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2020-12-10 22:38:39 +00:00
Jason A. Donenfeld
ed752295db windows/svc/mgr: report exit status when querying services
One thing the newer notification API does not do is automatically
provide the service exit code in its notifier response. It requires
querying manually. Unfortunately, we weren't propagating this
information up from the lower level struct, so this commit copys that
information over.

Change-Id: I70c683007ce34ffab6196329acefc8443f921ebe
Reviewed-on: https://go-review.googlesource.com/c/sys/+/274577
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Trust: Alex Brainman <alex.brainman@gmail.com>
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
2020-12-04 22:54:14 +00:00
Jason A. Donenfeld
c9906e3070 windows: add SubscribeServiceChangeNotifications for service tracking
Windows 8+ introduced SubscribeServiceChangeNotifications for simpler
and more reliable service event tracking. This commit adds the function
and related constants:

https://docs.microsoft.com/en-us/windows/win32/services/subscribeservicechangenotifications

Since these functions are only available on Windows 8+, and Go supports
Windows 7+, we mark them with "?" so that the caller can handle graceful
degradation.

Change-Id: Ibb9708bbe797408701c70b4b1c715381f33cb8ee
Reviewed-on: https://go-review.googlesource.com/c/sys/+/274576
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Trust: Alex Brainman <alex.brainman@gmail.com>
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
2020-12-04 22:54:10 +00:00
Jason A. Donenfeld
9a0d5dbcff windows/mkwinsyscall: account for non-"err" return values when processing "?"
The "?" code assumed that the error value was always called "err", when
in reality it might be called something different (like "ret") or even
entirely absent. This commit makes the templating robust to that. At the
same time, we move a lot of the complexity out of the actual templates
and into helper functions, so that this remains easy to read.

Change-Id: I939d56413a24f0e3e1bbf13da5adf13e9401747a
Reviewed-on: https://go-review.googlesource.com/c/sys/+/275472
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2020-12-04 22:54:03 +00:00
Boshi Lian
111129e158 windows: correct CertOpenStore to expect a 0 return value on failure
According to [1], this function returns NULL when it errors, rather than
INVALID_HANDLE_VALUE, which other Win32 functions return. Quick reversing
indicates that [1] is correct, as there's a `xor eax, eax` in the error
paths of the function just before jumping to the epilogue.

[1] https://docs.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-certopenstore#return-value

Change-Id: Iafe3b9c78e92c9c5adb8d14a434e11b58f59f19b
GitHub-Last-Rev: 5fcd21d889
GitHub-Pull-Request: golang/sys#92
Reviewed-on: https://go-review.googlesource.com/c/sys/+/273446
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Trust: Alex Brainman <alex.brainman@gmail.com>
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-30 07:27:48 +00:00