From 3c998971158cb41cbd24745131582589f2e6c949 Mon Sep 17 00:00:00 2001 From: pks <> Date: Tue, 21 Sep 2010 05:13:27 +0000 Subject: [PATCH] Missing file, QWORD compile fix, and thread error handling improvements. asm/ucontext.h will be used to implement a crash handler in msflinker, which should allow for easier debugging and development of msflinker and extension code. thread.c/h, fixes a bug if you thread_create(), but stop the thread before running it. Compilation fix for WSAGetLastError git-svn-id: file:///home/svn/framework3/trunk@10415 4d416f70-5f16-0410-b530-b9f4589650da --- .../libc/kernel/arch-x86/asm/ucontext.h | 23 +++++++++++++++ .../meterpreter/source/common/compat_types.h | 2 ++ .../source/meterpreter/source/common/thread.c | 29 ++++++++++++++++++- .../source/meterpreter/source/common/thread.h | 1 + 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 external/source/meterpreter/source/bionic/libc/kernel/arch-x86/asm/ucontext.h diff --git a/external/source/meterpreter/source/bionic/libc/kernel/arch-x86/asm/ucontext.h b/external/source/meterpreter/source/bionic/libc/kernel/arch-x86/asm/ucontext.h new file mode 100644 index 0000000000..b2a29d2d99 --- /dev/null +++ b/external/source/meterpreter/source/bionic/libc/kernel/arch-x86/asm/ucontext.h @@ -0,0 +1,23 @@ +/**************************************************************************** + **************************************************************************** + *** + *** This header was automatically generated from a Linux kernel header + *** of the same name, to make information necessary for userspace to + *** call into the kernel available to libc. It contains only constants, + *** structures, and macros generated from the original header, and thus, + *** contains no copyrightable information. + *** + **************************************************************************** + ****************************************************************************/ +#ifndef __ASM_X86_UCONTEXT_H +#define __ASM_X86_UCONTEXT_H + +struct ucontext { + unsigned long uc_flags; + struct ucontext *uc_link; + stack_t uc_stack; + struct sigcontext uc_mcontext; + sigset_t uc_sigmask; +}; + +#endif diff --git a/external/source/meterpreter/source/common/compat_types.h b/external/source/meterpreter/source/common/compat_types.h index ac2ad3f22a..aebdde7c67 100644 --- a/external/source/meterpreter/source/common/compat_types.h +++ b/external/source/meterpreter/source/common/compat_types.h @@ -189,6 +189,7 @@ typedef VOID * PVOID; typedef void * HMODULE; typedef short SHORT; typedef unsigned short USHORT; +typedef uint64_t QWORD; #ifndef TRUE #define TRUE (1) @@ -227,6 +228,7 @@ typedef unsigned short USHORT; int local_error; +#define WSAGetLastError() GetLastError() #define GetLastError() (local_error != -1 ? local_error : errno) #define SetLastError(x) (local_error = (x)) #define __declspec(x) diff --git a/external/source/meterpreter/source/common/thread.c b/external/source/meterpreter/source/common/thread.c index 8699500e58..02b1139c5c 100644 --- a/external/source/meterpreter/source/common/thread.c +++ b/external/source/meterpreter/source/common/thread.c @@ -307,6 +307,13 @@ void *__paused_thread(void *req) thread = tc->thread; free(tc); + if(event_poll(thread->sigterm, 0) == TRUE) { + /* + * In some cases, we might want to stop a thread before it does anything :/ + */ + return NULL; + } + return funk(thread); } #endif @@ -352,6 +359,8 @@ THREAD * thread_create( THREADFUNK funk, LPVOID param1, LPVOID param2 ) // PKS, this is fucky. // we need to use conditionals to implement this. + thread->thread_started = FALSE; + do { pthread_t pid; @@ -407,6 +416,8 @@ BOOL thread_run( THREAD * thread ) tc->engine_running = TRUE; pthread_mutex_unlock(&tc->suspend_mutex); pthread_cond_signal(&tc->suspend_cond); + + thread->thread_started = TRUE; #endif return TRUE; } @@ -417,10 +428,26 @@ BOOL thread_run( THREAD * thread ) */ BOOL thread_sigterm( THREAD * thread ) { + BOOL ret; + if( thread == NULL ) return FALSE; - return event_signal( thread->sigterm ); + ret = event_signal( thread->sigterm ); + +#ifndef _WIN32 + /* + * If we sig term a thread before it's started execution, we will leak memory / not be + * able to join on the thread, etc. + * + * Therefore, we need to start the thread executing before calling thread_join + */ + if(thread->thread_started != TRUE) { + thread_run(thread); + } +#endif + + return ret; } /* diff --git a/external/source/meterpreter/source/common/thread.h b/external/source/meterpreter/source/common/thread.h index b2aca5a9d1..6f3f2325f0 100644 --- a/external/source/meterpreter/source/common/thread.h +++ b/external/source/meterpreter/source/common/thread.h @@ -63,6 +63,7 @@ typedef struct _THREAD #ifndef _WIN32 void *suspend_thread_data; pthread_t pid; + int thread_started; #endif } THREAD, * LPTHREAD;