2016-03-01 22:57:46 +00:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
2011-10-15 17:29:25 +11:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package syscall
|
|
|
|
|
|
|
|
|
|
import (
|
2016-03-25 06:40:58 +00:00
|
|
|
"internal/syscall/windows/sysdll"
|
2011-10-15 17:29:25 +11:00
|
|
|
"sync"
|
2012-11-16 12:06:48 +04:00
|
|
|
"sync/atomic"
|
|
|
|
|
"unsafe"
|
2011-10-15 17:29:25 +11:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// DLLError describes reasons for DLL load failures.
|
|
|
|
|
type DLLError struct {
|
2011-11-13 22:44:52 -05:00
|
|
|
Err error
|
2011-10-15 17:29:25 +11:00
|
|
|
ObjName string
|
|
|
|
|
Msg string
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-01 22:05:34 -04:00
|
|
|
func (e *DLLError) Error() string { return e.Msg }
|
2011-10-15 17:29:25 +11:00
|
|
|
|
2020-11-13 15:48:05 +01:00
|
|
|
func (e *DLLError) Unwrap() error { return e.Err }
|
|
|
|
|
|
2014-09-14 21:25:44 -04:00
|
|
|
// Implemented in ../runtime/syscall_windows.go.
|
2020-06-04 11:16:43 -04:00
|
|
|
|
2021-07-22 17:50:42 +02:00
|
|
|
// Deprecated: Use SyscallN instead.
|
2011-11-13 22:44:52 -05:00
|
|
|
func Syscall(trap, nargs, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
|
2021-07-22 17:50:42 +02:00
|
|
|
|
|
|
|
|
// Deprecated: Use SyscallN instead.
|
2011-11-13 22:44:52 -05:00
|
|
|
func Syscall6(trap, nargs, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
|
2021-07-22 17:50:42 +02:00
|
|
|
|
|
|
|
|
// Deprecated: Use SyscallN instead.
|
2011-11-13 22:44:52 -05:00
|
|
|
func Syscall9(trap, nargs, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr, err Errno)
|
2021-07-22 17:50:42 +02:00
|
|
|
|
|
|
|
|
// Deprecated: Use SyscallN instead.
|
2011-11-13 22:44:52 -05:00
|
|
|
func Syscall12(trap, nargs, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12 uintptr) (r1, r2 uintptr, err Errno)
|
2021-07-22 17:50:42 +02:00
|
|
|
|
|
|
|
|
// Deprecated: Use SyscallN instead.
|
2011-11-29 10:24:19 +11:00
|
|
|
func Syscall15(trap, nargs, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15 uintptr) (r1, r2 uintptr, err Errno)
|
2021-07-22 17:50:42 +02:00
|
|
|
|
|
|
|
|
// Deprecated: Use SyscallN instead.
|
2018-11-03 02:19:47 +09:00
|
|
|
func Syscall18(trap, nargs, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18 uintptr) (r1, r2 uintptr, err Errno)
|
2021-07-22 17:50:42 +02:00
|
|
|
|
|
|
|
|
func SyscallN(trap uintptr, args ...uintptr) (r1, r2 uintptr, err Errno)
|
2012-06-27 18:21:48 -05:00
|
|
|
func loadlibrary(filename *uint16) (handle uintptr, err Errno)
|
2019-03-06 19:26:29 +01:00
|
|
|
func loadsystemlibrary(filename *uint16, absoluteFilepath *uint16) (handle uintptr, err Errno)
|
2011-11-13 22:44:52 -05:00
|
|
|
func getprocaddress(handle uintptr, procname *uint8) (proc uintptr, err Errno)
|
2011-10-15 17:29:25 +11:00
|
|
|
|
|
|
|
|
// A DLL implements access to a single DLL.
|
|
|
|
|
type DLL struct {
|
|
|
|
|
Name string
|
|
|
|
|
Handle Handle
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-06 19:26:29 +01:00
|
|
|
// We use this for computing the absolute path for system DLLs on systems
|
|
|
|
|
// where SEARCH_SYSTEM32 is not available.
|
|
|
|
|
var systemDirectoryPrefix string
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
n := uint32(MAX_PATH)
|
|
|
|
|
for {
|
|
|
|
|
b := make([]uint16, n)
|
|
|
|
|
l, e := getSystemDirectory(&b[0], n)
|
|
|
|
|
if e != nil {
|
|
|
|
|
panic("Unable to determine system directory: " + e.Error())
|
|
|
|
|
}
|
|
|
|
|
if l <= n {
|
|
|
|
|
systemDirectoryPrefix = UTF16ToString(b[:l]) + "\\"
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
n = l
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-25 06:40:58 +00:00
|
|
|
// LoadDLL loads the named DLL file into memory.
|
2016-04-02 00:20:13 +00:00
|
|
|
//
|
|
|
|
|
// If name is not an absolute path and is not a known system DLL used by
|
|
|
|
|
// Go, Windows will search for the named DLL in many locations, causing
|
|
|
|
|
// potential DLL preloading attacks.
|
|
|
|
|
//
|
|
|
|
|
// Use LazyDLL in golang.org/x/sys/windows for a secure way to
|
|
|
|
|
// load system DLLs.
|
2016-03-25 06:40:58 +00:00
|
|
|
func LoadDLL(name string) (*DLL, error) {
|
syscall: return EINVAL when string arguments have NUL characters
Since NUL usually terminates strings in underlying syscalls, allowing
it when converting string arguments is a security risk, especially
when dealing with filenames. For example, a program might reason that
filename like "/root/..\x00/" is a subdirectory or "/root/" and allow
access to it, while underlying syscall will treat "\x00" as an end of
that string and the actual filename will be "/root/..", which might
be unexpected. Returning EINVAL when string arguments have NUL in
them makes sure this attack vector is unusable.
R=golang-dev, r, bradfitz, fullung, rsc, minux.ma
CC=golang-dev
https://golang.org/cl/6458050
2012-08-05 17:24:32 -04:00
|
|
|
namep, err := UTF16PtrFromString(name)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2016-03-25 06:40:58 +00:00
|
|
|
var h uintptr
|
|
|
|
|
var e Errno
|
|
|
|
|
if sysdll.IsSystemDLL[name] {
|
2019-03-06 19:26:29 +01:00
|
|
|
absoluteFilepathp, err := UTF16PtrFromString(systemDirectoryPrefix + name)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
h, e = loadsystemlibrary(namep, absoluteFilepathp)
|
2016-03-25 06:40:58 +00:00
|
|
|
} else {
|
|
|
|
|
h, e = loadlibrary(namep)
|
|
|
|
|
}
|
2011-10-15 17:29:25 +11:00
|
|
|
if e != 0 {
|
|
|
|
|
return nil, &DLLError{
|
2011-11-13 22:44:52 -05:00
|
|
|
Err: e,
|
2011-10-15 17:29:25 +11:00
|
|
|
ObjName: name,
|
2011-11-13 22:44:52 -05:00
|
|
|
Msg: "Failed to load " + name + ": " + e.Error(),
|
2011-10-15 17:29:25 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
d := &DLL{
|
|
|
|
|
Name: name,
|
|
|
|
|
Handle: Handle(h),
|
|
|
|
|
}
|
|
|
|
|
return d, nil
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-11 16:49:38 +03:00
|
|
|
// MustLoadDLL is like LoadDLL but panics if load operation fails.
|
2011-10-15 17:29:25 +11:00
|
|
|
func MustLoadDLL(name string) *DLL {
|
|
|
|
|
d, e := LoadDLL(name)
|
|
|
|
|
if e != nil {
|
|
|
|
|
panic(e)
|
|
|
|
|
}
|
|
|
|
|
return d
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FindProc searches DLL d for procedure named name and returns *Proc
|
|
|
|
|
// if found. It returns an error if search fails.
|
2011-11-01 22:12:41 -04:00
|
|
|
func (d *DLL) FindProc(name string) (proc *Proc, err error) {
|
syscall: return EINVAL when string arguments have NUL characters
Since NUL usually terminates strings in underlying syscalls, allowing
it when converting string arguments is a security risk, especially
when dealing with filenames. For example, a program might reason that
filename like "/root/..\x00/" is a subdirectory or "/root/" and allow
access to it, while underlying syscall will treat "\x00" as an end of
that string and the actual filename will be "/root/..", which might
be unexpected. Returning EINVAL when string arguments have NUL in
them makes sure this attack vector is unusable.
R=golang-dev, r, bradfitz, fullung, rsc, minux.ma
CC=golang-dev
https://golang.org/cl/6458050
2012-08-05 17:24:32 -04:00
|
|
|
namep, err := BytePtrFromString(name)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
a, e := getprocaddress(uintptr(d.Handle), namep)
|
2011-10-15 17:29:25 +11:00
|
|
|
if e != 0 {
|
|
|
|
|
return nil, &DLLError{
|
2011-11-13 22:44:52 -05:00
|
|
|
Err: e,
|
2011-10-15 17:29:25 +11:00
|
|
|
ObjName: name,
|
2011-11-13 22:44:52 -05:00
|
|
|
Msg: "Failed to find " + name + " procedure in " + d.Name + ": " + e.Error(),
|
2011-10-15 17:29:25 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
p := &Proc{
|
|
|
|
|
Dll: d,
|
|
|
|
|
Name: name,
|
|
|
|
|
addr: a,
|
|
|
|
|
}
|
|
|
|
|
return p, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MustFindProc is like FindProc but panics if search fails.
|
|
|
|
|
func (d *DLL) MustFindProc(name string) *Proc {
|
|
|
|
|
p, e := d.FindProc(name)
|
|
|
|
|
if e != nil {
|
|
|
|
|
panic(e)
|
|
|
|
|
}
|
|
|
|
|
return p
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Release unloads DLL d from memory.
|
2011-11-13 22:44:52 -05:00
|
|
|
func (d *DLL) Release() (err error) {
|
|
|
|
|
return FreeLibrary(d.Handle)
|
2011-10-15 17:29:25 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A Proc implements access to a procedure inside a DLL.
|
|
|
|
|
type Proc struct {
|
|
|
|
|
Dll *DLL
|
|
|
|
|
Name string
|
|
|
|
|
addr uintptr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Addr returns the address of the procedure represented by p.
|
|
|
|
|
// The return value can be passed to Syscall to run the procedure.
|
|
|
|
|
func (p *Proc) Addr() uintptr {
|
|
|
|
|
return p.addr
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-28 14:19:27 -07:00
|
|
|
//go:uintptrescapes
|
|
|
|
|
|
2021-07-22 17:50:42 +02:00
|
|
|
// Call executes procedure p with arguments a.
|
2013-02-03 01:42:17 +08:00
|
|
|
//
|
|
|
|
|
// The returned error is always non-nil, constructed from the result of GetLastError.
|
|
|
|
|
// Callers must inspect the primary return value to decide whether an error occurred
|
|
|
|
|
// (according to the semantics of the specific function being called) before consulting
|
2020-06-04 11:17:49 -04:00
|
|
|
// the error. The error always has type syscall.Errno.
|
|
|
|
|
//
|
|
|
|
|
// On amd64, Call can pass and return floating-point values. To pass
|
|
|
|
|
// an argument x with C type "float", use
|
|
|
|
|
// uintptr(math.Float32bits(x)). To pass an argument with C type
|
|
|
|
|
// "double", use uintptr(math.Float64bits(x)). Floating-point return
|
|
|
|
|
// values are returned in r2. The return value for C type "float" is
|
|
|
|
|
// math.Float32frombits(uint32(r2)). For C type "double", it is
|
|
|
|
|
// math.Float64frombits(uint64(r2)).
|
2021-07-22 17:50:42 +02:00
|
|
|
func (p *Proc) Call(a ...uintptr) (uintptr, uintptr, error) {
|
|
|
|
|
return SyscallN(p.Addr(), a...)
|
2011-10-15 17:29:25 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A LazyDLL implements access to a single DLL.
|
|
|
|
|
// It will delay the load of the DLL until the first
|
|
|
|
|
// call to its Handle method or to one of its
|
|
|
|
|
// LazyProc's Addr method.
|
2016-04-02 00:20:13 +00:00
|
|
|
//
|
|
|
|
|
// LazyDLL is subject to the same DLL preloading attacks as documented
|
|
|
|
|
// on LoadDLL.
|
|
|
|
|
//
|
|
|
|
|
// Use LazyDLL in golang.org/x/sys/windows for a secure way to
|
|
|
|
|
// load system DLLs.
|
2011-10-15 17:29:25 +11:00
|
|
|
type LazyDLL struct {
|
|
|
|
|
mu sync.Mutex
|
|
|
|
|
dll *DLL // non nil once DLL is loaded
|
|
|
|
|
Name string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load loads DLL file d.Name into memory. It returns an error if fails.
|
|
|
|
|
// Load will not try to load DLL, if it is already loaded into memory.
|
2011-11-01 22:12:41 -04:00
|
|
|
func (d *LazyDLL) Load() error {
|
2012-11-16 12:06:48 +04:00
|
|
|
// Non-racy version of:
|
|
|
|
|
// if d.dll == nil {
|
|
|
|
|
if atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&d.dll))) == nil {
|
2011-10-15 17:29:25 +11:00
|
|
|
d.mu.Lock()
|
|
|
|
|
defer d.mu.Unlock()
|
|
|
|
|
if d.dll == nil {
|
|
|
|
|
dll, e := LoadDLL(d.Name)
|
|
|
|
|
if e != nil {
|
|
|
|
|
return e
|
|
|
|
|
}
|
2012-11-16 12:06:48 +04:00
|
|
|
// Non-racy version of:
|
|
|
|
|
// d.dll = dll
|
|
|
|
|
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&d.dll)), unsafe.Pointer(dll))
|
2011-10-15 17:29:25 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// mustLoad is like Load but panics if search fails.
|
|
|
|
|
func (d *LazyDLL) mustLoad() {
|
|
|
|
|
e := d.Load()
|
|
|
|
|
if e != nil {
|
|
|
|
|
panic(e)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle returns d's module handle.
|
|
|
|
|
func (d *LazyDLL) Handle() uintptr {
|
|
|
|
|
d.mustLoad()
|
|
|
|
|
return uintptr(d.dll.Handle)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewProc returns a LazyProc for accessing the named procedure in the DLL d.
|
|
|
|
|
func (d *LazyDLL) NewProc(name string) *LazyProc {
|
|
|
|
|
return &LazyProc{l: d, Name: name}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewLazyDLL creates new LazyDLL associated with DLL file.
|
|
|
|
|
func NewLazyDLL(name string) *LazyDLL {
|
|
|
|
|
return &LazyDLL{Name: name}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A LazyProc implements access to a procedure inside a LazyDLL.
|
2019-06-07 13:07:32 -04:00
|
|
|
// It delays the lookup until the Addr, Call, or Find method is called.
|
2011-10-15 17:29:25 +11:00
|
|
|
type LazyProc struct {
|
|
|
|
|
mu sync.Mutex
|
|
|
|
|
Name string
|
|
|
|
|
l *LazyDLL
|
|
|
|
|
proc *Proc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find searches DLL for procedure named p.Name. It returns
|
|
|
|
|
// an error if search fails. Find will not search procedure,
|
|
|
|
|
// if it is already found and loaded into memory.
|
2011-11-01 22:12:41 -04:00
|
|
|
func (p *LazyProc) Find() error {
|
2012-11-16 12:06:48 +04:00
|
|
|
// Non-racy version of:
|
|
|
|
|
// if p.proc == nil {
|
|
|
|
|
if atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&p.proc))) == nil {
|
2011-10-15 17:29:25 +11:00
|
|
|
p.mu.Lock()
|
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
|
if p.proc == nil {
|
|
|
|
|
e := p.l.Load()
|
|
|
|
|
if e != nil {
|
|
|
|
|
return e
|
|
|
|
|
}
|
|
|
|
|
proc, e := p.l.dll.FindProc(p.Name)
|
|
|
|
|
if e != nil {
|
|
|
|
|
return e
|
|
|
|
|
}
|
2012-11-16 12:06:48 +04:00
|
|
|
// Non-racy version of:
|
|
|
|
|
// p.proc = proc
|
|
|
|
|
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&p.proc)), unsafe.Pointer(proc))
|
2011-10-15 17:29:25 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// mustFind is like Find but panics if search fails.
|
|
|
|
|
func (p *LazyProc) mustFind() {
|
|
|
|
|
e := p.Find()
|
|
|
|
|
if e != nil {
|
|
|
|
|
panic(e)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Addr returns the address of the procedure represented by p.
|
|
|
|
|
// The return value can be passed to Syscall to run the procedure.
|
|
|
|
|
func (p *LazyProc) Addr() uintptr {
|
|
|
|
|
p.mustFind()
|
|
|
|
|
return p.proc.Addr()
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-28 14:19:27 -07:00
|
|
|
//go:uintptrescapes
|
|
|
|
|
|
2019-06-07 13:07:32 -04:00
|
|
|
// Call executes procedure p with arguments a. See the documentation of
|
|
|
|
|
// Proc.Call for more information.
|
2013-02-03 01:42:17 +08:00
|
|
|
func (p *LazyProc) Call(a ...uintptr) (r1, r2 uintptr, lastErr error) {
|
2011-10-15 17:29:25 +11:00
|
|
|
p.mustFind()
|
|
|
|
|
return p.proc.Call(a...)
|
|
|
|
|
}
|