Commit Graph

288 Commits

Author SHA1 Message Date
Tobias Klauser
27bbf83dae windows: add race annotations to Windows ReadFile and WriteFile
Follow CL 392774 which changed Windows ReadFile and WriteFile in package
syscall.

For golang/go#51618

Change-Id: I0f8046adb0f8145bd0775a3b5399647897cf19ce
Reviewed-on: https://go-review.googlesource.com/c/sys/+/392814
Trust: Tobias Klauser <tobias.klauser@gmail.com>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2022-03-15 18:05:22 +00:00
Tobias Klauser
5a964db013 windows: add Get{Active,Maximum}ProcesorCount
Reference:
https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getactiveprocessorcount
https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getmaximumprocessorcount

Change-Id: Ifca711b82f112b395eac3b96266f63b5623881fc
Reviewed-on: https://go-review.googlesource.com/c/sys/+/374435
Trust: Tobias Klauser <tobias.klauser@gmail.com>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Patrik Nyblom <pnyb@google.com>
Trust: Patrik Nyblom <pnyb@google.com>
Run-TryBot: Patrik Nyblom <pnyb@google.com>
2022-01-11 09:28:08 +00:00
Jason A. Donenfeld
1d35b9e2eb windows/svc: use NtQuerySystemInformation in IsWindowsService
This brings the algorithm more exactly in line with what .NET does for
the identically named function. Specifically, instead of using
OpenProcess, which requires rights that restricted services might not
have, we use NtQuerySystemInformation(SYSTEM_PROCESS_INFORMATION) to
find the parent process image name and session ID.

Fixes golang/go#44921.

Change-Id: Ie2ad7521cf4c530037d086e61dbc2413e4e7777c
Reviewed-on: https://go-review.googlesource.com/c/sys/+/372554
Trust: Jason Donenfeld <Jason@zx2c4.com>
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Patrik Nyblom <pnyb@google.com>
Trust: Patrik Nyblom <pnyb@google.com>
Run-TryBot: Patrik Nyblom <pnyb@google.com>
2021-12-16 02:10:12 +00:00
Jason A. Donenfeld
3b038e5940 windows: set pipe explicitly inheritable in test for go 1.16
Go 1.16 didn't create inheritable pipes yet, because we didn't have safe
process inheritance semantics worked out then. So, for this test,
explicitly mark the pipe as inheritable, in case this is running on Go
1.16.

Fixes golang/go#50160.

Change-Id: I2e59cf430ff35e7f59315b11b73e79b9aaf3e6ce
Reviewed-on: https://go-review.googlesource.com/c/sys/+/371694
Trust: Jason Donenfeld <Jason@zx2c4.com>
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
2021-12-14 17:07:44 +00:00
Jason A. Donenfeld
03aa0b5f68 windows: allocate attribute list with LocalAlloc, not individual items
We didn't want to wind up with Go pointers mangled by win32, so we were
previously copying the contents into a LocalAlloc'd blob, and then
adding that to the attribute list. The problem is that recent changes to
the API broke this design convention, to the point where it expects 0x18
byte objects to be added using size 0x8. This seems like an unfortunate
oversight from Microsoft, but there's nothing we can do about it. So we
can work around it by instead LocalAlloc'ing the actual container, and
then using the exact pointer value that's passed into Update.

This commit also adds a test that both makes sure that these functions
actually work, and provokes a UaF that's successfully caught, were you
to comment out the line of this commit that reads `al.pointers =
append(al.pointers, value)`.

Fixes golang/go#50134.

Change-Id: Ib73346d2d6ca3db601cd236596cefb564d9dc8f1
Reviewed-on: https://go-review.googlesource.com/c/sys/+/371276
Trust: Jason Donenfeld <Jason@zx2c4.com>
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Patrik Nyblom <pnyb@google.com>
Trust: Patrik Nyblom <pnyb@google.com>
Run-TryBot: Patrik Nyblom <pnyb@google.com>
2021-12-13 22:30:07 +00:00
Tobias Klauser
94396e4217 windows: add ModuleEntry32 type and Module32{First,Next} wrappers
Reference:
https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/ns-tlhelp32-moduleentry32w
https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-module32firstw
https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-module32nextw

Change-Id: I5455cabdbaa8e7e25490f869ca349e3c6f67a0cf
Reviewed-on: https://go-review.googlesource.com/c/sys/+/369174
Trust: Tobias Klauser <tobias.klauser@gmail.com>
Trust: Patrik Nyblom <pnyb@google.com>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
Run-TryBot: Patrik Nyblom <pnyb@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Patrik Nyblom <pnyb@google.com>
2021-12-04 12:00:58 +00:00
Jason A. Donenfeld
fe61309f88 windows: add SetupAPI and CfgMgr32 functions
Simon and I have worked on these on and off for a number of projects
over the last 3 years and by now it's quite stable and mature. Rather
than carrying this around privately, these have now been updated to be
in the style of x/sys/windows, in this case matching how the
security_windows.go file does things. Specifically, because these APIs
are kind of hard to work with, and quite lengthy, we split them off into
a setupapi_windows.go file, just like security_windows.go.  We already
had the setupapierrors_windows.go file, so that's been folded into the
new one that contains the additional definitions nad functions.

These APIs are among the most obtuse to work with in all of Win32, so
considerable attention has been spent trying to make these wrappers both
quite "raw" so that they match the win32, but also accessible from Go.
The fact that they're so old in Windows history makes for a few tricky
things, like struct padding that doesn't quite match Go's rules, but
we've been able to accommodate basically everything after quite a bit of
work.

Change-Id: I0c2dd85e4bb40eee10186ffc92558a858bdf8c6a
Reviewed-on: https://go-review.googlesource.com/c/sys/+/366654
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Patrik Nyblom <pnyb@google.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Simon Rozman <simon@rozman.si>
Reviewed-by: Patrik Nyblom <pnyb@google.com>
2021-11-24 21:15:45 +00:00
Jason A. Donenfeld
ef496fb156 windows: re-enable TestWinVerifyTrust with newly signed file
Rather than disabling this test, let's just not make it rely on
Microsoft files, whose signing validity period we can't depend on.
Instead, we include our own EV-signed artifact, with a Digicert
timestamp using a certificate valid for a decade.

Fixes golang/go#49651.
Fixes golang/go#49266.
For golang/go#46906.

Change-Id: Idadba346810017b8f769d6fac1ddd357d4dee93c
Reviewed-on: https://go-review.googlesource.com/c/sys/+/366655
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
2021-11-23 17:31:58 +00:00
Bryan C. Mills
dee7805ff2 windows: skip TestWinVerifyTrust
This test is failing (with different failure modes) on two different
builders. Adding a skip until it can be made more robust.

For golang/go#49651
For golang/go#49266
Updates golang/go#46906

Change-Id: I0fdd0e6f729c37e234b62b65abc53003eb8834f0
Reviewed-on: https://go-review.googlesource.com/c/sys/+/364794
Trust: Bryan C. Mills <bcmills@google.com>
Trust: Patrik Nyblom <pnyb@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Patrik Nyblom <pnyb@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
2021-11-17 18:06:35 +00:00
Bryan C. Mills
99a53858aa all: add missing //go:build comments
These were apparently overlooked in CL 357329, CL 294490, CL 296889,
and other various updates to this module. (I noticed them via gopls
while investigating golang/go#49466.)

Updates golang/go#41184

Change-Id: Id042bb6fe5282e6d528e9315acf2ad29d0df58ba
Reviewed-on: https://go-review.googlesource.com/c/sys/+/362577
Trust: Bryan C. Mills <bcmills@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
2021-11-10 15:43:04 +00:00
Bryan C. Mills
51b60fd695 windows/registry: lock OS thread while enumerating keys
CL 361154 updated the standard syscall package to document that
successive calls to syscall.RegEnumKeyEx must occur on the same OS
thread (after that requirement was empirically discovered in
golang/go#49320).

This use in x/sys needs to be updated to comply with the
newly-discovered requirement.

Fixes golang/go#49466.

Change-Id: Idc45d91f175e1db25c215213fcaa45982c2f5e6e
Reviewed-on: https://go-review.googlesource.com/c/sys/+/362576
Trust: Bryan C. Mills <bcmills@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2021-11-09 18:48:56 +00:00
Tobias Klauser
c75c47738b unix, windows: copy arrays by assignment instead of looping
Follow CL 360602 which did the same in package syscall.

Change-Id: Ibf6d86dea1cb118e869618c66da72da281adb2d7
Reviewed-on: https://go-review.googlesource.com/c/sys/+/361574
Trust: Tobias Klauser <tobias.klauser@gmail.com>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2021-11-05 18:34:46 +00:00
Hao Mou
611d5d6438 windows: fix test causing checkptr failure
TestNtCreateFileAndNtSetInformationFile is failing checkptr. Fix by
using replacing [:] with [:n:n].

Fixes golang/go#49217

Change-Id: I08e7ed41b33452c096cf40cdbf9d73989b769bdb
Reviewed-on: https://go-review.googlesource.com/c/sys/+/359496
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Trust: Alex Brainman <alex.brainman@gmail.com>
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
2021-10-31 06:41:16 +00:00
Jason A. Donenfeld
6e7872819d windows/svc: allow querying service start reason
The QueryServiceDynamicInformation API makes it possible to determine
why a service started. This is declared as an optional function because
the API is Win8+.

Reference:
https://docs.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-queryservicedynamicinformation

Change-Id: I18e9a95b35f8c37d94c9900626c1785f425701dc
Reviewed-on: https://go-review.googlesource.com/c/sys/+/358394
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-10-29 16:52:21 +00:00
Takuto Ikuta
c1bf0bb051 windows: regen zsyscall_windows.go
It seems mksyscall sorts Windows APIs bit differently now.

Change-Id: I2703b12addefbbac00ab06f62500bc7db2b8cba6
Reviewed-on: https://go-review.googlesource.com/c/sys/+/359657
Trust: Tobias Klauser <tobias.klauser@gmail.com>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com>
2021-10-29 16:29:42 +00:00
Hao Mou
d6a326fbbf windows: add NtSetInformationFile
Added NtSetInformationFile and some const values related to it.

The doc for the function and the values of the file information class
can be found here:
  https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/ntifs/nf-ntifs-ntsetinformationfile
The values of the flags in the individual FILE_INFORMATION_CLASS can be
found here:
  FILE_RENAME_INFORMATION - https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/ntifs/ns-ntifs-_file_rename_information
  FILE_DISPOSITION_INFORMATION_EX - https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/ntddk/ns-ntddk-_file_disposition_information_ex
  FILE_CASE_SENSITIVE_INFORMATION - https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/ntifs/ns-ntifs-_file_case_sensitive_information
  FILE_LINK_INFORMATION - https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/ntifs/ns-ntifs-_file_link_information
The other file information classes do not have flag values.

Fixes golang/go#48933

Change-Id: I917ff4c8df132f8584fd6d924cf5a9626a065092
Reviewed-on: https://go-review.googlesource.com/c/sys/+/355495
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-10-23 08:55:30 +00:00
Jason A. Donenfeld
9d61738499 windows: add dynamic exception function table manipulators
These are useful for loading new code into the address space at runtime
and having exceptions be handled.

https://docs.microsoft.com/en-us/windows/win32/api/winnt/nf-winnt-rtladdfunctiontable
https://docs.microsoft.com/en-us/windows/win32/api/winnt/nf-winnt-rtldeletefunctiontable

Change-Id: I4fce4d7003d87cf48486d6e09d8543c2f1325554
Reviewed-on: https://go-review.googlesource.com/c/sys/+/357409
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-10-20 17:42:00 +00:00
Jason A. Donenfeld
153b154469 windows: add system info and version related functions
This adds various version and system info functions and adds a test that
prints the version of all installed drivers.

Change-Id: I73a2b0a35dcedf88206979ec6f1a56552dc80899
Reviewed-on: https://go-review.googlesource.com/c/sys/+/357149
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2021-10-20 17:18:05 +00:00
Brad Fitzpatrick
fcb26fe61c windows: gofmt to add go:build lines
Change-Id: I71b9023fcb6c9860ea35ba0d2cf77a6eed5176b9
Reviewed-on: https://go-review.googlesource.com/c/sys/+/357329
Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com>
Reviewed-by: David Crawshaw <crawshaw@golang.org>
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: David Crawshaw <crawshaw@golang.org>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
2021-10-20 15:40:33 +00:00
Jason A. Donenfeld
25fac919d9 windows/svc: do not close service handle
The documentation [1] says "The service status handle does not have to
be closed." That makes it seem like it's optional. But actually, closing
it raises a user exception, because it's not a legitimate handle to
close.

This commit also adds the right go build comments with a `go fmt` pass.

[1] https://docs.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-registerservicectrlhandlerexa

Change-Id: Ibf67c0e8d94c87068720472009b24cecdfda6519
Reviewed-on: https://go-review.googlesource.com/c/sys/+/357250
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-10-20 15:30:01 +00:00
Jason A. Donenfeld
0ec99a608a windows/svc: rewrite in Go
The old service management code was written in assembly and communicated
over Windows events, which resulted in non-obvious control flow.
NewCallback makes it possible to rewrite all of this in vanilla Go. This
also enables the service test on the Go builders, as modifying system
services shouldn't be an issue there.

Change-Id: I8003b57d11d4469f762058c648a4b7733530eeb8
Reviewed-on: https://go-review.googlesource.com/c/sys/+/330010
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-10-20 06:40:51 +00:00
Hao Mou
97ac67df71 windows: add const values
When I was working with NtCreateFile and DeviceIoControl, I found a lot
of the constant values are missing, so I added them.

The access masks I added for NtCreateFile are described here: https://docs.microsoft.com/en-us/windows/win32/api/winternl/nf-winternl-ntcreatefile
Their values are listed here: https://docs.microsoft.com/en-us/windows/win32/fileio/file-access-rights-constants
The FSCTL control codes I added for DeviceIoControl are listed here: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/4dc02779-9d95-43f8-bba4-8d4ce4961458

This seems trivial so I did not create an issue.

Change-Id: I0c1b72ac1b34413a6ed7420026df46ff94cefe5f
Reviewed-on: https://go-review.googlesource.com/c/sys/+/355350
Trust: Alex Brainman <alex.brainman@gmail.com>
Trust: Meng Zhuo <mzh@golangcn.org>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2021-10-13 07:50:03 +00:00
Weilu Jia
d3039528d8 windows: add low level memory related syscalls
Adds VirtualQuery, VirtualQueryEx, VirtualProtectEx, ReadProcessMemory, and WriteProcessMemory.

VirtualProtect already exists.

https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualquery
https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualqueryex
https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualprotectex
https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-readprocessmemory
https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-writeprocessmemory

Change-Id: I609a09641c502d8d3802036123e0784b7c76e99b
GitHub-Last-Rev: 368f8c7b62
GitHub-Pull-Request: golang/sys#117
Reviewed-on: https://go-review.googlesource.com/c/sys/+/352249
Trust: Alex Brainman <alex.brainman@gmail.com>
Trust: Emmanuel Odeke <emmanuel@orijtech.com>
Trust: Benny Siegert <bsiegert@gmail.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-10-07 07:53:35 +00:00
Weilu Jia
92d5a993a6 windows: add process module related syscalls
Add EnumProcessModules, EnumProcessModulesEx, GetModuleInformation, GetModuleFileNameEx and GetModuleBaseName.

https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-enumprocessmodules
https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-enumprocessmodulesex
https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-getmoduleinformation
https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-getmodulefilenameexw
https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-getmodulebasenamew

Change-Id: I75b062daec7a2cc9685e803dfa851825acee32b6
GitHub-Last-Rev: 7c208d6d3e
GitHub-Pull-Request: golang/sys#116
Reviewed-on: https://go-review.googlesource.com/c/sys/+/350870
Trust: Alex Brainman <alex.brainman@gmail.com>
Trust: Minux Ma <minux@golang.org>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2021-09-25 03:26:02 +00:00
David Crawshaw
44f02d92fd windows: add WTSGetActiveConsoleSessionId
https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-wtsgetactiveconsolesessionid

Change-Id: I3c180bbb3a768beac6633e78514fac4e3837b69d
Reviewed-on: https://go-review.googlesource.com/c/sys/+/331909
Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Brad Fitzpatrick <bradfitz@golang.org>
Trust: Dmitri Shuralyov <dmitshur@golang.org>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
2021-08-19 07:20:04 +00:00
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