Skip to content

Commit

Permalink
Windows: Search for DLLs in system paths only
Browse files Browse the repository at this point in the history
Reported-by: Trail of Bits <https://www.trailofbits.com>
Signed-off-by: Neil Twigg <neil@nats.io>
  • Loading branch information
neilalexander committed Aug 29, 2024
1 parent 3ffe823 commit aa9711c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 19 deletions.
35 changes: 27 additions & 8 deletions server/certstore/certstore_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,38 @@ var (
winMyStore = winWide("MY")

// 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

0 comments on commit aa9711c

Please sign in to comment.