From e52845a7b7a1dfb5e6e97ed6f57076fe943aa20f Mon Sep 17 00:00:00 2001 From: sxul07 Date: Wed, 18 Mar 2020 08:26:01 +0800 Subject: [PATCH] fix error on windows server --- go.mod | 2 +- go.sum | 2 ++ main.go | 31 ++++++++++++++++++++++++++++++- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 73f713f..4d87eb5 100644 --- a/go.mod +++ b/go.mod @@ -17,5 +17,5 @@ require ( github.com/lxn/win v0.0.0-20181015143721-a7f87360b10e github.com/oxtoacart/bpool v0.0.0-20190227141107-8c4636f812cc // indirect github.com/stretchr/testify v1.3.0 // indirect - golang.org/x/sys v0.0.0-20190405154228-4b34438f7a67 + golang.org/x/sys v0.0.0-20200317113312-5766fd39f98d ) diff --git a/go.sum b/go.sum index 044b553..f40b444 100644 --- a/go.sum +++ b/go.sum @@ -35,3 +35,5 @@ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0 github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= golang.org/x/sys v0.0.0-20190405154228-4b34438f7a67 h1:1Fzlr8kkDLQwqMP8GxrhptBLqZG/EDpiATneiZHY998= golang.org/x/sys v0.0.0-20190405154228-4b34438f7a67/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200317113312-5766fd39f98d h1:62ap6LNOjDU6uGmKXHJbSfciMoV+FeI1sRXx/pLDL44= +golang.org/x/sys v0.0.0-20200317113312-5766fd39f98d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/main.go b/main.go index b74be44..73abdea 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,7 @@ import ( "net" "os" "os/signal" + "os/user" "reflect" "sync" "syscall" @@ -53,8 +54,34 @@ type copyDataStruct struct { lpData uintptr } +type SecurityAttributes struct { + Length uint32 + SecurityDescriptor uintptr + InheritHandle uint32 +} + var queryPageantMutex sync.Mutex +func makeInheritSaWithSid() *windows.SecurityAttributes { + var sa windows.SecurityAttributes + + u, err := user.Current() + + if err == nil { + sd, err := windows.SecurityDescriptorFromString("O:" + u.Uid) + if err == nil { + sa.SecurityDescriptor = sd + } + } + + sa.Length = uint32(unsafe.Sizeof(sa)) + + sa.InheritHandle = 1 + + return &sa + +} + func queryPageant(buf []byte) (result []byte, err error) { if len(buf) > agentMaxMessageLength { err = errors.New("Message too long") @@ -75,7 +102,9 @@ func queryPageant(buf []byte) (result []byte, err error) { mapName := fmt.Sprintf("WSLPageantRequest") queryPageantMutex.Lock() - fileMap, err := windows.CreateFileMapping(invalidHandleValue, nil, pageReadWrite, 0, agentMaxMessageLength, syscall.StringToUTF16Ptr(mapName)) + var sa = makeInheritSaWithSid() + + fileMap, err := windows.CreateFileMapping(invalidHandleValue, sa, pageReadWrite, 0, agentMaxMessageLength, syscall.StringToUTF16Ptr(mapName)) if err != nil { queryPageantMutex.Unlock() return