Skip to content

Commit

Permalink
Handle invalid hook addresses gracefully
Browse files Browse the repository at this point in the history
Closes #37
  • Loading branch information
ndrewh committed Aug 26, 2024
1 parent 8c54fdb commit 76f7285
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pyda_core/pyda_core_py.c
Original file line number Diff line number Diff line change
Expand Up @@ -559,9 +559,16 @@ PydaProcess_register_hook(PyObject *self, PyObject *args) {

#ifdef PYDA_DYNAMORIO_CLIENT
DEBUG_PRINTF("register_hook: %llx\n", addr);
#endif // PYDA_DYNAMORIO_CLIENT
if (!dr_memory_is_readable((app_pc)addr, 1)) {
char buf[100];
snprintf(buf, sizeof(buf), "Hooked PC %" PRIxPTR " is invalid.", (uintptr_t)addr);
PyErr_SetString(PyExc_RuntimeError, buf);
return NULL;
}

pyda_add_hook(p->main_thread->proc, addr, callback);

#endif // PYDA_DYNAMORIO_CLIENT
Py_INCREF(Py_None);
return Py_None;
}
Expand Down
18 changes: 18 additions & 0 deletions tests/err_invalidhook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from pyda import *
from pwnlib.elf.elf import ELF
from pwnlib.util.packing import u64
import string
import sys, time

p = process()

e = ELF(p.exe_path)
e.address = p.maps[p.exe_path].base

counter = 0
def lib_hook(p):
global counter
counter += 1

p.hook(0x1337133713371337, lib_hook)
p.run()
8 changes: 8 additions & 0 deletions tests/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,14 @@ def no_warnings_or_errors(stdout: bytes, stderr: bytes) -> bool:
lambda o, e: o.count(b"pass\n") == 1,
]
)),

("err_invalidhook", "simple.c", "err_invalidhook.py", RunOpts(), ExpectedResult(
retcode=0,
checkers=[
output_checker,
lambda o, e: e.count(b"RuntimeError: Hooked PC 1337133713371337 is invalid.") == 1,
]
)),
]

def main():
Expand Down

0 comments on commit 76f7285

Please sign in to comment.