From b6303091fc8c11cf86b92e9c4287c0ba74e77e87 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Fri, 27 Nov 2020 13:13:45 +0100 Subject: memmod: fix import loading function usage Signed-off-by: Jason A. Donenfeld --- tun/wintun/memmod/memmod_windows.go | 6 +++--- tun/wintun/memmod/syscall_windows.go | 2 -- tun/wintun/memmod/zsyscall_windows.go | 22 +--------------------- 3 files changed, 4 insertions(+), 26 deletions(-) (limited to 'tun/wintun') diff --git a/tun/wintun/memmod/memmod_windows.go b/tun/wintun/memmod/memmod_windows.go index 13f28ef..b9d7783 100644 --- a/tun/wintun/memmod/memmod_windows.go +++ b/tun/wintun/memmod/memmod_windows.go @@ -313,7 +313,7 @@ func (module *Module) buildImportTable() error { module.modules = make([]windows.Handle, 0, 16) importDesc := (*IMAGE_IMPORT_DESCRIPTOR)(a2p(module.codeBase + uintptr(directory.VirtualAddress))) for !isBadReadPtr(uintptr(unsafe.Pointer(importDesc)), unsafe.Sizeof(*importDesc)) && importDesc.Name != 0 { - handle, err := loadLibraryA((*byte)(a2p(module.codeBase + uintptr(importDesc.Name)))) + handle, err := windows.LoadLibraryEx(windows.BytePtrToString((*byte)(a2p(module.codeBase + uintptr(importDesc.Name)))), 0, windows.LOAD_LIBRARY_SEARCH_SYSTEM32) if err != nil { return fmt.Errorf("Error loading module: %w", err) } @@ -328,10 +328,10 @@ func (module *Module) buildImportTable() error { } for *thunkRef != 0 { if IMAGE_SNAP_BY_ORDINAL(*thunkRef) { - *funcRef, err = getProcAddress(handle, (*byte)(a2p(IMAGE_ORDINAL(*thunkRef)))) + *funcRef, err = windows.GetProcAddressByOrdinal(handle, IMAGE_ORDINAL(*thunkRef)) } else { thunkData := (*IMAGE_IMPORT_BY_NAME)(a2p(module.codeBase + *thunkRef)) - *funcRef, err = getProcAddress(handle, &thunkData.Name[0]) + *funcRef, err = windows.GetProcAddress(handle, windows.BytePtrToString(&thunkData.Name[0])) } if err != nil { windows.FreeLibrary(handle) diff --git a/tun/wintun/memmod/syscall_windows.go b/tun/wintun/memmod/syscall_windows.go index beae415..0b29cb9 100644 --- a/tun/wintun/memmod/syscall_windows.go +++ b/tun/wintun/memmod/syscall_windows.go @@ -324,8 +324,6 @@ const ( DLL_PROCESS_DETACH = 0 ) -//sys loadLibraryA(libFileName *byte) (module windows.Handle, err error) = kernel32.LoadLibraryA -//sys getProcAddress(module windows.Handle, procName *byte) (addr uintptr, err error) = kernel32.GetProcAddress //sys isBadReadPtr(addr uintptr, ucb uintptr) (ret bool) = kernel32.IsBadReadPtr type SYSTEM_INFO struct { diff --git a/tun/wintun/memmod/zsyscall_windows.go b/tun/wintun/memmod/zsyscall_windows.go index c56b650..6a5b76f 100644 --- a/tun/wintun/memmod/zsyscall_windows.go +++ b/tun/wintun/memmod/zsyscall_windows.go @@ -40,31 +40,11 @@ func errnoErr(e syscall.Errno) error { var ( modkernel32 = windows.NewLazySystemDLL("kernel32.dll") - procGetProcAddress = modkernel32.NewProc("GetProcAddress") - procIsBadReadPtr = modkernel32.NewProc("IsBadReadPtr") - procLoadLibraryA = modkernel32.NewProc("LoadLibraryA") + procIsBadReadPtr = modkernel32.NewProc("IsBadReadPtr") ) -func getProcAddress(module windows.Handle, procName *byte) (addr uintptr, err error) { - r0, _, e1 := syscall.Syscall(procGetProcAddress.Addr(), 2, uintptr(module), uintptr(unsafe.Pointer(procName)), 0) - addr = uintptr(r0) - if addr == 0 { - err = errnoErr(e1) - } - return -} - func isBadReadPtr(addr uintptr, ucb uintptr) (ret bool) { r0, _, _ := syscall.Syscall(procIsBadReadPtr.Addr(), 2, uintptr(addr), uintptr(ucb), 0) ret = r0 != 0 return } - -func loadLibraryA(libFileName *byte) (module windows.Handle, err error) { - r0, _, e1 := syscall.Syscall(procLoadLibraryA.Addr(), 1, uintptr(unsafe.Pointer(libFileName)), 0, 0) - module = windows.Handle(r0) - if module == 0 { - err = errnoErr(e1) - } - return -} -- cgit v1.2.3-59-g8ed1b