mirror of
https://github.com/rwinkhart/sys.git
synced 2026-08-28 04:46:44 -04:00
windows: do not query library for inline functions
The GetCurrent*Token variety of functions are actually implemented as inline functions in the header files of the SDK. Attempting to call out to these as library functions is an error. This commit also adds a test to ensure that these work as expected. Change-Id: I105f1ca1a8936114fe61bc22188200c31f240a23 Reviewed-on: https://go-review.googlesource.com/c/sys/+/177840 Run-TryBot: Jason Donenfeld <Jason@zx2c4.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
committed by
Brad Fitzpatrick
parent
30999d67c8
commit
8097e1b27f
@@ -8,6 +8,7 @@ import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
@@ -104,3 +105,67 @@ func TestCreateWellKnownSid(t *testing.T) {
|
||||
t.Fatalf("Expecting administrators to be S-1-5-32-544, but found %s instead", sidStr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPseudoTokens(t *testing.T) {
|
||||
version, err := windows.GetVersion()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ((version&0xffff)>>8)|((version&0xff)<<8) < 0x0602 {
|
||||
return
|
||||
}
|
||||
|
||||
realProcessToken, err := windows.OpenCurrentProcessToken()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer realProcessToken.Close()
|
||||
realProcessUser, err := realProcessToken.GetTokenUser()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
pseudoProcessToken := windows.GetCurrentProcessToken()
|
||||
pseudoProcessUser, err := pseudoProcessToken.GetTokenUser()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !windows.EqualSid(realProcessUser.User.Sid, pseudoProcessUser.User.Sid) {
|
||||
t.Fatal("The real process token does not have the same as the pseudo process token")
|
||||
}
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
err = windows.RevertToSelf()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
pseudoThreadToken := windows.GetCurrentThreadToken()
|
||||
_, err = pseudoThreadToken.GetTokenUser()
|
||||
if err != windows.ERROR_NO_TOKEN {
|
||||
t.Fatal("Expected an empty thread token")
|
||||
}
|
||||
pseudoThreadEffectiveToken := windows.GetCurrentThreadEffectiveToken()
|
||||
pseudoThreadEffectiveUser, err := pseudoThreadEffectiveToken.GetTokenUser()
|
||||
if err != nil {
|
||||
t.Fatal(nil)
|
||||
}
|
||||
if !windows.EqualSid(realProcessUser.User.Sid, pseudoThreadEffectiveUser.User.Sid) {
|
||||
t.Fatal("The real process token does not have the same as the pseudo thread effective token, even though we aren't impersonating")
|
||||
}
|
||||
|
||||
err = windows.ImpersonateSelf(windows.SecurityImpersonation)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer windows.RevertToSelf()
|
||||
pseudoThreadUser, err := pseudoThreadToken.GetTokenUser()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !windows.EqualSid(realProcessUser.User.Sid, pseudoThreadUser.User.Sid) {
|
||||
t.Fatal("The real process token does not have the same as the pseudo thread token after impersonating self")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user