From 2dccfee4fd3e23e10ba17b9d696cf2388c28764b Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Sat, 21 Sep 2019 21:22:07 +0200 Subject: [PATCH] windows: do not return invalid error for psuedo-handle functions GetCurrentProcess and GetCurrentThread return -1 and -2 respectively. We could arguably hard code those values, but MSDN cautions not to; I'm sure this advice is old now, given that the psuedo handles for tokens (not processes/threads) are now implemented with inline functions in the headers for Windows 8, but anyway, we'll follow Microsoft's advice. However, regardless of that, these functions never ever return an error. MSDN doesn't indicate that they do, reverse engineering the functions doesn't indicate that they do, and checking against 0 is just plain wrong, considering 0!=INVALID_HANDLE_VALUE; however INVALID_HANDLE_VALUE==-1, so that's not correct either. In fact, checking any value and returning any error does not make sense. Incidently having to check code for the pseudo handle is more verbose too. In order to make this function do the correct thing and meet the spec, remove the error value from the return. Change-Id: If03c9dab001be3bf5a04999aef20dbfcf8a4f405 Reviewed-on: https://go-review.googlesource.com/c/sys/+/196798 Run-TryBot: Jason A. Donenfeld TryBot-Result: Gobot Gobot Reviewed-by: Alex Brainman --- windows/security_windows.go | 14 +++----------- windows/syscall_windows.go | 4 ++-- windows/syscall_windows_test.go | 3 +-- windows/zsyscall_windows.go | 22 ++++------------------ 4 files changed, 10 insertions(+), 33 deletions(-) diff --git a/windows/security_windows.go b/windows/security_windows.go index 5b2f6ba1..8e2971df 100644 --- a/windows/security_windows.go +++ b/windows/security_windows.go @@ -652,17 +652,9 @@ type Token Handle // associated with current process. It is a real // token that needs to be closed, unlike // GetCurrentProcessToken. -func OpenCurrentProcessToken() (Token, error) { - p, e := GetCurrentProcess() - if e != nil { - return 0, e - } - var t Token - e = OpenProcessToken(p, TOKEN_QUERY|TOKEN_DUPLICATE, &t) - if e != nil { - return 0, e - } - return t, nil +func OpenCurrentProcessToken() (token Token, err error) { + err = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_DUPLICATE, &token) + return } // GetCurrentProcessToken returns the access token associated with diff --git a/windows/syscall_windows.go b/windows/syscall_windows.go index ed36134b..47bde878 100644 --- a/windows/syscall_windows.go +++ b/windows/syscall_windows.go @@ -178,8 +178,8 @@ func NewCallbackCDecl(fn interface{}) uintptr { //sys TerminateProcess(handle Handle, exitcode uint32) (err error) //sys GetExitCodeProcess(handle Handle, exitcode *uint32) (err error) //sys GetStartupInfo(startupInfo *StartupInfo) (err error) = GetStartupInfoW -//sys GetCurrentProcess() (pseudoHandle Handle, err error) -//sys GetCurrentThread() (pseudoHandle Handle, err error) +//sys GetCurrentProcess() (pseudoHandle Handle) +//sys GetCurrentThread() (pseudoHandle Handle) //sys GetProcessTimes(handle Handle, creationTime *Filetime, exitTime *Filetime, kernelTime *Filetime, userTime *Filetime) (err error) //sys DuplicateHandle(hSourceProcessHandle Handle, hSourceHandle Handle, hTargetProcessHandle Handle, lpTargetHandle *Handle, dwDesiredAccess uint32, bInheritHandle bool, dwOptions uint32) (err error) //sys WaitForSingleObject(handle Handle, waitMilliseconds uint32) (event uint32, err error) [failretval==0xffffffff] diff --git a/windows/syscall_windows_test.go b/windows/syscall_windows_test.go index db261ddb..b8004096 100644 --- a/windows/syscall_windows_test.go +++ b/windows/syscall_windows_test.go @@ -250,8 +250,7 @@ func TestGetNamedSecurityInfo(t *testing.T) { } func TestGetSecurityInfo(t *testing.T) { - process, _ := windows.GetCurrentProcess() - sd, err := windows.GetSecurityInfo(process, windows.SE_KERNEL_OBJECT, windows.DACL_SECURITY_INFORMATION) + sd, err := windows.GetSecurityInfo(windows.GetCurrentProcess(), windows.SE_KERNEL_OBJECT, windows.DACL_SECURITY_INFORMATION) if err != nil { t.Fatal(err) } diff --git a/windows/zsyscall_windows.go b/windows/zsyscall_windows.go index 407d2709..d0b74bfc 100644 --- a/windows/zsyscall_windows.go +++ b/windows/zsyscall_windows.go @@ -1209,29 +1209,15 @@ func GetStartupInfo(startupInfo *StartupInfo) (err error) { return } -func GetCurrentProcess() (pseudoHandle Handle, err error) { - r0, _, e1 := syscall.Syscall(procGetCurrentProcess.Addr(), 0, 0, 0, 0) +func GetCurrentProcess() (pseudoHandle Handle) { + r0, _, _ := syscall.Syscall(procGetCurrentProcess.Addr(), 0, 0, 0, 0) pseudoHandle = Handle(r0) - if pseudoHandle == 0 { - if e1 != 0 { - err = errnoErr(e1) - } else { - err = syscall.EINVAL - } - } return } -func GetCurrentThread() (pseudoHandle Handle, err error) { - r0, _, e1 := syscall.Syscall(procGetCurrentThread.Addr(), 0, 0, 0, 0) +func GetCurrentThread() (pseudoHandle Handle) { + r0, _, _ := syscall.Syscall(procGetCurrentThread.Addr(), 0, 0, 0, 0) pseudoHandle = Handle(r0) - if pseudoHandle == 0 { - if e1 != 0 { - err = errnoErr(e1) - } else { - err = syscall.EINVAL - } - } return }