Skip to content

Commit ba342a6

Browse files
committed
memmod: do not use IsBadReadPtr
It should be enough to check for the trailing zero name. Signed-off-by: Jason A. Donenfeld <[email protected]>
1 parent f9a2178 commit ba342a6

File tree

4 files changed

+11
-61
lines changed

4 files changed

+11
-61
lines changed

tun/wintun/memmod/memmod_windows.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,16 @@ func (module *Module) performBaseRelocation(delta uintptr) (relocated bool, err
304304
return true, nil
305305
}
306306

307+
func isBadPtr(p, l uintptr) bool {
308+
if l == 0 {
309+
return false
310+
}
311+
if p == 0 {
312+
return true
313+
}
314+
return p + l - 1 >= p //TODO: maybe compare signed?
315+
}
316+
307317
func (module *Module) buildImportTable() error {
308318
directory := module.headerDirectory(IMAGE_DIRECTORY_ENTRY_IMPORT)
309319
if directory.Size == 0 {
@@ -312,7 +322,7 @@ func (module *Module) buildImportTable() error {
312322

313323
module.modules = make([]windows.Handle, 0, 16)
314324
importDesc := (*IMAGE_IMPORT_DESCRIPTOR)(a2p(module.codeBase + uintptr(directory.VirtualAddress)))
315-
for !isBadReadPtr(uintptr(unsafe.Pointer(importDesc)), unsafe.Sizeof(*importDesc)) && importDesc.Name != 0 {
325+
for importDesc.Name != 0 {
316326
handle, err := windows.LoadLibraryEx(windows.BytePtrToString((*byte)(a2p(module.codeBase+uintptr(importDesc.Name)))), 0, windows.LOAD_LIBRARY_SEARCH_SYSTEM32)
317327
if err != nil {
318328
return fmt.Errorf("Error loading module: %w", err)

tun/wintun/memmod/mksyscall.go

Lines changed: 0 additions & 8 deletions
This file was deleted.

tun/wintun/memmod/syscall_windows.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,6 @@ const (
324324
DLL_PROCESS_DETACH = 0
325325
)
326326

327-
//sys isBadReadPtr(addr uintptr, ucb uintptr) (ret bool) = kernel32.IsBadReadPtr
328-
329327
type SYSTEM_INFO struct {
330328
ProcessorArchitecture uint16
331329
Reserved uint16

tun/wintun/memmod/zsyscall_windows.go

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)