Replace Windows-specific NewLazyDLL with NewLazySystemDLL

This will only search Windows System directory for the DLL if name is a base
name (like "advapi32.dll"), which prevents DLL preloading attacks.

To get access to NewLazySystemDLL imports of syscall needs to be swapped with
golang.org/x/sys/windows.
This commit is contained in:
albertony 2025-01-07 15:40:14 +01:00
parent bfb7b88371
commit 7692ef289f
2 changed files with 7 additions and 7 deletions

View File

@ -5,18 +5,18 @@ package local
import ( import (
"context" "context"
"fmt" "fmt"
"syscall"
"unsafe" "unsafe"
"github.com/rclone/rclone/fs" "github.com/rclone/rclone/fs"
"golang.org/x/sys/windows"
) )
var getFreeDiskSpace = syscall.NewLazyDLL("kernel32.dll").NewProc("GetDiskFreeSpaceExW") var getFreeDiskSpace = windows.NewLazySystemDLL("kernel32.dll").NewProc("GetDiskFreeSpaceExW")
// About gets quota information // About gets quota information
func (f *Fs) About(ctx context.Context) (*fs.Usage, error) { func (f *Fs) About(ctx context.Context) (*fs.Usage, error) {
var available, total, free int64 var available, total, free int64
root, e := syscall.UTF16PtrFromString(f.root) root, e := windows.UTF16PtrFromString(f.root)
if e != nil { if e != nil {
return nil, fmt.Errorf("failed to read disk usage: %w", e) return nil, fmt.Errorf("failed to read disk usage: %w", e)
} }
@ -26,7 +26,7 @@ func (f *Fs) About(ctx context.Context) (*fs.Usage, error) {
uintptr(unsafe.Pointer(&total)), // lpTotalNumberOfBytes uintptr(unsafe.Pointer(&total)), // lpTotalNumberOfBytes
uintptr(unsafe.Pointer(&free)), // lpTotalNumberOfFreeBytes uintptr(unsafe.Pointer(&free)), // lpTotalNumberOfFreeBytes
) )
if e1 != syscall.Errno(0) { if e1 != windows.Errno(0) {
return nil, fmt.Errorf("failed to read disk usage: %w", e1) return nil, fmt.Errorf("failed to read disk usage: %w", e1)
} }
usage := &fs.Usage{ usage := &fs.Usage{

View File

@ -3,13 +3,13 @@
package terminal package terminal
import ( import (
"syscall" "golang.org/x/sys/windows"
) )
// HideConsole hides the console window and activates another window // HideConsole hides the console window and activates another window
func HideConsole() { func HideConsole() {
getConsoleWindow := syscall.NewLazyDLL("kernel32.dll").NewProc("GetConsoleWindow") getConsoleWindow := windows.NewLazySystemDLL("kernel32.dll").NewProc("GetConsoleWindow")
showWindow := syscall.NewLazyDLL("user32.dll").NewProc("ShowWindow") showWindow := windows.NewLazySystemDLL("user32.dll").NewProc("ShowWindow")
if getConsoleWindow.Find() == nil && showWindow.Find() == nil { if getConsoleWindow.Find() == nil && showWindow.Find() == nil {
hwnd, _, _ := getConsoleWindow.Call() hwnd, _, _ := getConsoleWindow.Call()
if hwnd != 0 { if hwnd != 0 {