Skip to content

Commit

Permalink
Fixed mutiple process/thread handle mismatches and reworked tid2handl…
Browse files Browse the repository at this point in the history
…er function.
  • Loading branch information
yetmorecode authored and radare committed Aug 13, 2015
1 parent 2e347da commit 4b55af7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 39 deletions.
29 changes: 14 additions & 15 deletions libr/debug/p/debug_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -483,15 +483,15 @@ static int windows_reg_read (RDebug *dbg, int type, ut8 *buf, int size) {
type = -type;
}

HANDLE hProcess=tid2handler (pid, tid);
HANDLE thread = w32_open_thread (pid, tid);
CONTEXT ctx __attribute__ ((aligned (16)));
ctx.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
if (!GetThreadContext (hProcess, &ctx)) {
if (!GetThreadContext (thread, &ctx)) {
eprintf ("GetThreadContext: %x\n", (int)GetLastError ());
CloseHandle(hProcess);
CloseHandle(thread);
return R_FALSE;
}
CloseHandle(hProcess);
CloseHandle(thread);
if (type==R_REG_TYPE_FPU || type==R_REG_TYPE_MMX || type==R_REG_TYPE_XMM) {
#if __MINGW64__
eprintf ("TODO: r_debug_native_reg_read fpu/mmx/xmm\n");
Expand Down Expand Up @@ -845,13 +845,13 @@ static int r_debug_native_reg_write(RDebug *dbg, int type, const ut8* buf, int s
int tid = dbg->tid;
int pid = dbg->pid;
BOOL ret;
HANDLE hProcess;
HANDLE thread;
CONTEXT ctx __attribute__((aligned (16)));
memcpy (&ctx, buf, sizeof (CONTEXT));
ctx.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
hProcess=tid2handler (pid, tid);
ret=SetThreadContext (hProcess, &ctx)? R_TRUE: R_FALSE;
CloseHandle(hProcess);
thread = w32_open_thread (pid, tid);
ret=SetThreadContext (thread, &ctx)? R_TRUE: R_FALSE;
CloseHandle(thread);
return ret;
#endif
return R_FALSE;
Expand All @@ -863,14 +863,13 @@ static int r_debug_native_reg_write(RDebug *dbg, int type, const ut8* buf, int s
if (type == R_REG_TYPE_GPR) {
#if __WINDOWS__ && !__CYGWIN__
BOOL ret;
HANDLE hProcess;
CONTEXT ctx __attribute__((aligned (16)));
memcpy (&ctx, buf, sizeof (CONTEXT));
ctx.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
// eprintf ("EFLAGS =%x\n", ctx.EFlags);
hProcess = tid2handler (dbg->pid, dbg->tid);
ret=SetThreadContext (hProcess, &ctx)? R_TRUE: R_FALSE;
CloseHandle (hProcess);
HANDLE thread = w32_open_thread (dbg->pid, dbg->tid);
ret = SetThreadContext (thread, &ctx)? R_TRUE: R_FALSE;
CloseHandle (thread);
return ret;
#elif __linux__
int ret = ptrace (PTRACE_SETREGS, dbg->pid,
Expand Down Expand Up @@ -944,7 +943,7 @@ static RDebugMap* r_debug_native_map_alloc(RDebug *dbg, ut64 addr, int size) {
#elif __WINDOWS__ && !__CYGWIN__
RDebugMap *map = NULL;
LPVOID base = NULL;
HANDLE process = tid2handler (dbg->pid, dbg->tid);
HANDLE process = w32_open_process (PROCESS_ALL_ACCESS, FALSE, dbg->pid);
if (process == INVALID_HANDLE_VALUE) {
return map;
}
Expand All @@ -969,7 +968,7 @@ static int r_debug_native_map_dealloc(RDebug *dbg, ut64 addr, int size) {
return xnu_map_dealloc (dbg, addr, size);

#elif __WINDOWS__ && !__CYGWIN__
HANDLE process = tid2handler (dbg->pid, dbg->tid);
HANDLE process = w32_open_process (PROCESS_ALL_ACCESS, FALSE, dbg->tid);
if (process == INVALID_HANDLE_VALUE) {
return R_FALSE;
}
Expand Down Expand Up @@ -1411,7 +1410,7 @@ static RList *r_debug_desc_native_list (int pid) {
static int r_debug_native_map_protect (RDebug *dbg, ut64 addr, int size, int perms) {
#if __WINDOWS__ && !__CYGWIN__
DWORD old;
HANDLE process = tid2handler (dbg->pid, dbg->tid);
HANDLE process = w32_open_process (PROCESS_ALL_ACCESS, FALSE, dbg->pid);
// TODO: align pointers
BOOL ret = VirtualProtectEx (WIN32_PI (process), (LPVOID)(UINT)addr, size, perms, &old);
CloseHandle (process);
Expand Down
33 changes: 9 additions & 24 deletions libr/debug/p/native/w32.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include <psapi.h>
#include <tchar.h>

static HANDLE tid2handler(int pid, int tid);

// XXX remove
#define WIN32_PI(x) x
#if 0
Expand Down Expand Up @@ -415,13 +413,12 @@ static int w32_dbg_wait(RDebug *dbg, int pid) {
print_lasterr ((char *)__FUNCTION__, "WaitForDebugEvent");
return -1;
}
/* save thread id */
code = de.dwDebugEventCode;
tid = de.dwThreadId;
//pid = de.dwProcessId;
pid = de.dwProcessId;
dbg->tid = tid;
code = de.dwDebugEventCode;
/* Ctrl-C? */
/* get kind of event */
dbg->pid = pid;
/* TODO: DEBUG_CONTROL_C */
switch (code) {
case CREATE_PROCESS_DEBUG_EVENT:
eprintf ("(%d) created process (%d:%p)\n",
Expand Down Expand Up @@ -512,24 +509,12 @@ static inline int CheckValidPE(unsigned char * PeHeader) {
return 0;
}

static HANDLE tid2handler(int pid, int tid) {
HANDLE th = CreateToolhelp32Snapshot (TH32CS_SNAPTHREAD, pid);
THREADENTRY32 te32 = { .dwSize = sizeof (THREADENTRY32) };
if (th == INVALID_HANDLE_VALUE)
return NULL;
if (!Thread32First (th, &te32)) {
CloseHandle (th);
return NULL;
static HANDLE w32_open_thread (int pid, int tid) {
HANDLE thread = w32_openthread (THREAD_ALL_ACCESS, 0, tid);
if (thread == INVALID_HANDLE_VALUE) {
print_lasterr((char *)__FUNCTION__, "OpenThread");
}
do {
if (te32.th32OwnerProcessID == pid && te32.th32ThreadID == tid) {
CloseHandle (th);
return w32_openthread (THREAD_ALL_ACCESS, 0,
te32.th32ThreadID);
}
} while (Thread32Next (th, &te32));
CloseHandle (th);
return NULL;
return thread;
}

RList *w32_thread_list (int pid, RList *list) {
Expand Down

0 comments on commit 4b55af7

Please sign in to comment.