Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows: Search for DLLs in system paths only #5836

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions server/certstore/certstore_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,38 @@ var (
winAuthRootStore = winWide("AuthRoot")

// These DLLs must be available on all Windows hosts
winCrypt32 = windows.MustLoadDLL("crypt32.dll")
winNCrypt = windows.MustLoadDLL("ncrypt.dll")
winCrypt32 = windows.NewLazySystemDLL("crypt32.dll")
winNCrypt = windows.NewLazySystemDLL("ncrypt.dll")

winCertFindCertificateInStore = winCrypt32.MustFindProc("CertFindCertificateInStore")
winCryptAcquireCertificatePrivateKey = winCrypt32.MustFindProc("CryptAcquireCertificatePrivateKey")
winNCryptExportKey = winNCrypt.MustFindProc("NCryptExportKey")
winNCryptOpenStorageProvider = winNCrypt.MustFindProc("NCryptOpenStorageProvider")
winNCryptGetProperty = winNCrypt.MustFindProc("NCryptGetProperty")
winNCryptSignHash = winNCrypt.MustFindProc("NCryptSignHash")
winCertFindCertificateInStore = winCrypt32.NewProc("CertFindCertificateInStore")
winCryptAcquireCertificatePrivateKey = winCrypt32.NewProc("CryptAcquireCertificatePrivateKey")
winNCryptExportKey = winNCrypt.NewProc("NCryptExportKey")
winNCryptOpenStorageProvider = winNCrypt.NewProc("NCryptOpenStorageProvider")
winNCryptGetProperty = winNCrypt.NewProc("NCryptGetProperty")
winNCryptSignHash = winNCrypt.NewProc("NCryptSignHash")

winFnGetProperty = winGetProperty
)

func init() {
for _, d := range []*windows.LazyDLL{
winCrypt32, winNCrypt,
} {
if err := d.Load(); err != nil {
panic(err)
}
}
for _, p := range []*windows.LazyProc{
winCertFindCertificateInStore, winCryptAcquireCertificatePrivateKey,
winNCryptExportKey, winNCryptOpenStorageProvider,
winNCryptGetProperty, winNCryptSignHash,
} {
if err := p.Find(); err != nil {
panic(err)
}
}
}

type winPKCS1PaddingInfo struct {
pszAlgID *uint16
}
Expand Down
18 changes: 17 additions & 1 deletion server/pse/pse_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,33 @@ import (
"syscall"
"time"
"unsafe"

"golang.org/x/sys/windows"
)

var (
pdh = syscall.NewLazyDLL("pdh.dll")
pdh = windows.NewLazySystemDLL("pdh.dll")
winPdhOpenQuery = pdh.NewProc("PdhOpenQuery")
winPdhAddCounter = pdh.NewProc("PdhAddCounterW")
winPdhCollectQueryData = pdh.NewProc("PdhCollectQueryData")
winPdhGetFormattedCounterValue = pdh.NewProc("PdhGetFormattedCounterValue")
winPdhGetFormattedCounterArray = pdh.NewProc("PdhGetFormattedCounterArrayW")
)

func init() {
if err := pdh.Load(); err != nil {
panic(err)
}
for _, p := range []*windows.LazyProc{
winPdhOpenQuery, winPdhAddCounter, winPdhCollectQueryData,
winPdhGetFormattedCounterValue, winPdhGetFormattedCounterArray,
} {
if err := p.Find(); err != nil {
panic(err)
}
}
}

// global performance counter query handle and counters
var (
pcHandle PDH_HQUERY
Expand Down
25 changes: 15 additions & 10 deletions server/sysmem/mem_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,23 @@
package sysmem

import (
"syscall"
"unsafe"

"golang.org/x/sys/windows"
)

var winKernel32 = windows.NewLazySystemDLL("kernel32.dll")
var winGlobalMemoryStatusEx = winKernel32.NewProc("GlobalMemoryStatusEx")

func init() {
if err := winKernel32.Load(); err != nil {
panic(err)
}
if err := winGlobalMemoryStatusEx.Find(); err != nil {
panic(err)
}
}

// https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/ns-sysinfoapi-memorystatusex
type _memoryStatusEx struct {
dwLength uint32
Expand All @@ -30,16 +43,8 @@ type _memoryStatusEx struct {
}

func Memory() int64 {
kernel32, err := syscall.LoadDLL("kernel32.dll")
if err != nil {
return 0
}
globalMemoryStatusEx, err := kernel32.FindProc("GlobalMemoryStatusEx")
if err != nil {
return 0
}
msx := &_memoryStatusEx{dwLength: 64}
res, _, _ := globalMemoryStatusEx.Call(uintptr(unsafe.Pointer(msx)))
res, _, _ := winGlobalMemoryStatusEx.Call(uintptr(unsafe.Pointer(msx)))
if res == 0 {
return 0
}
Expand Down