From 98129a5cf4a0207f7e2202f458737deeaca0514a Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Sun, 22 Sep 2019 12:27:43 +0200 Subject: [PATCH] windows: use pseudo handle constants to implement GetCurrentProcess There's no point in adding a function call to retrieve a constant, or worse, a syscall to retrieve a constant. These are fixed and baked so deep into NT they'll never change. So let's benefit from the obvious optimization and make these constants. Go easily inlines the function calls as well. We also take the opportunity to sunset OpenCurrentProcessToken and restore its original behavior, since users should be invoking this deliberately with the correct access mask. Change-Id: I92f7de56c0fcf5b69b59f5a79d2828c7ddf3c8f6 Reviewed-on: https://go-review.googlesource.com/c/sys/+/196800 Run-TryBot: Jason A. Donenfeld TryBot-Result: Gobot Gobot Reviewed-by: Alex Brainman --- windows/security_windows.go | 17 ++++++++++------- windows/syscall_windows.go | 14 ++++++++++++-- windows/zsyscall_windows.go | 14 -------------- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/windows/security_windows.go b/windows/security_windows.go index 308d4847..f5e725a1 100644 --- a/windows/security_windows.go +++ b/windows/security_windows.go @@ -647,13 +647,16 @@ func (tml *Tokenmandatorylabel) Size() uint32 { // system-related operations on the local computer. type Token Handle -// OpenCurrentProcessToken opens the access token -// associated with current process. It is a real -// token that needs to be closed, unlike -// GetCurrentProcessToken. -func OpenCurrentProcessToken() (token Token, err error) { - err = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_DUPLICATE, &token) - return +// OpenCurrentProcessToken opens an access token associated with current +// process with TOKEN_QUERY access. It is a real token that needs to be closed. +// +// Deprecated: Explicitly call OpenProcessToken(GetCurrentProcess(), ...) +// with the desired access instead, or use GetCurrentProcessToken for a +// TOKEN_QUERY token. +func OpenCurrentProcessToken() (Token, error) { + var token Token + err := OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token) + return token, err } // GetCurrentProcessToken returns the access token associated with diff --git a/windows/syscall_windows.go b/windows/syscall_windows.go index 47bde878..5734645b 100644 --- a/windows/syscall_windows.go +++ b/windows/syscall_windows.go @@ -178,8 +178,6 @@ 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) -//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] @@ -309,6 +307,18 @@ func NewCallbackCDecl(fn interface{}) uintptr { // syscall interface implementation for other packages +// GetCurrentProcess returns the handle for the current process. +// It is a pseudo handle that does not need to be closed. +func GetCurrentProcess() Handle { + return Handle(^uintptr(1 - 1)) +} + +// GetCurrentThread returns the handle for the current thread. +// It is a pseudo handle that does not need to be closed. +func GetCurrentThread() Handle { + return Handle(^uintptr(2 - 1)) +} + // GetProcAddressByOrdinal retrieves the address of the exported // function from module by ordinal. func GetProcAddressByOrdinal(module Handle, ordinal uintptr) (proc uintptr, err error) { diff --git a/windows/zsyscall_windows.go b/windows/zsyscall_windows.go index d0b74bfc..8008c030 100644 --- a/windows/zsyscall_windows.go +++ b/windows/zsyscall_windows.go @@ -119,8 +119,6 @@ var ( procTerminateProcess = modkernel32.NewProc("TerminateProcess") procGetExitCodeProcess = modkernel32.NewProc("GetExitCodeProcess") procGetStartupInfoW = modkernel32.NewProc("GetStartupInfoW") - procGetCurrentProcess = modkernel32.NewProc("GetCurrentProcess") - procGetCurrentThread = modkernel32.NewProc("GetCurrentThread") procGetProcessTimes = modkernel32.NewProc("GetProcessTimes") procDuplicateHandle = modkernel32.NewProc("DuplicateHandle") procWaitForSingleObject = modkernel32.NewProc("WaitForSingleObject") @@ -1209,18 +1207,6 @@ func GetStartupInfo(startupInfo *StartupInfo) (err error) { return } -func GetCurrentProcess() (pseudoHandle Handle) { - r0, _, _ := syscall.Syscall(procGetCurrentProcess.Addr(), 0, 0, 0, 0) - pseudoHandle = Handle(r0) - return -} - -func GetCurrentThread() (pseudoHandle Handle) { - r0, _, _ := syscall.Syscall(procGetCurrentThread.Addr(), 0, 0, 0, 0) - pseudoHandle = Handle(r0) - return -} - func GetProcessTimes(handle Handle, creationTime *Filetime, exitTime *Filetime, kernelTime *Filetime, userTime *Filetime) (err error) { r1, _, e1 := syscall.Syscall6(procGetProcessTimes.Addr(), 5, uintptr(handle), uintptr(unsafe.Pointer(creationTime)), uintptr(unsafe.Pointer(exitTime)), uintptr(unsafe.Pointer(kernelTime)), uintptr(unsafe.Pointer(userTime)), 0) if r1 == 0 {