1
mirror of https://github.com/rapid7/metasploit-payloads synced 2025-03-24 18:16:24 +01:00

Revert "fix Linux threads to actually use allocated memory"

This reverts commit f95152dfc16da32e5b59abdee60ddab209c2a564.
This commit is contained in:
Brent Cook 2016-04-26 16:49:46 -04:00
parent 328bd0a93d
commit 84140c23ba
No known key found for this signature in database
GPG Key ID: 1FFAA0B24B708F96
2 changed files with 95 additions and 94 deletions
c/meterpreter/source/common

@ -21,16 +21,17 @@ int __futex_wake(volatile void *ftx, int count);
* when using CriticalSections with OpenSSL on some Windows systems. Mutex's are not as optimal * when using CriticalSections with OpenSSL on some Windows systems. Mutex's are not as optimal
* as CriticalSections but they appear to resolve the OpenSSL deadlock issue. * as CriticalSections but they appear to resolve the OpenSSL deadlock issue.
*/ */
LOCK * lock_create(VOID) LOCK * lock_create( VOID )
{ {
LOCK * lock = calloc(1, sizeof(LOCK)); LOCK * lock = (LOCK *)malloc( sizeof( LOCK ) );
if (lock != NULL) if( lock != NULL )
{ {
memset( lock, 0, sizeof( LOCK ) );
#ifdef _WIN32 #ifdef _WIN32
lock->handle = CreateMutex(NULL, FALSE, NULL); lock->handle = CreateMutex( NULL, FALSE, NULL );
#else #else
pthread_mutex_init(&lock->handle, NULL); pthread_mutex_init(lock->handle, NULL);
#endif #endif
} }
return lock; return lock;
@ -39,32 +40,32 @@ LOCK * lock_create(VOID)
/* /*
* Destroy a lock that is no longer required. * Destroy a lock that is no longer required.
*/ */
VOID lock_destroy(LOCK * lock) VOID lock_destroy( LOCK * lock )
{ {
if (lock != NULL ) if( lock != NULL )
{ {
lock_release(lock); lock_release( lock );
#ifdef _WIN32 #ifdef _WIN32
CloseHandle(lock->handle); CloseHandle( lock->handle );
#else #else
pthread_mutex_destroy(&lock->handle); pthread_mutex_destroy(lock->handle);
#endif #endif
free(lock); free( lock );
} }
} }
/* /*
* Acquire a lock and block untill it is acquired. * Acquire a lock and block untill it is acquired.
*/ */
VOID lock_acquire(LOCK * lock) VOID lock_acquire( LOCK * lock )
{ {
if (lock != NULL ) { if( lock != NULL ) {
#ifdef _WIN32 #ifdef _WIN32
WaitForSingleObject(lock->handle, INFINITE); WaitForSingleObject( lock->handle, INFINITE );
#else #else
pthread_mutex_lock(&lock->handle); pthread_mutex_lock(lock->handle);
#endif #endif
} }
} }
@ -72,13 +73,13 @@ VOID lock_acquire(LOCK * lock)
/* /*
* Release a lock previously held. * Release a lock previously held.
*/ */
VOID lock_release(LOCK * lock) VOID lock_release( LOCK * lock )
{ {
if (lock != NULL ) { if( lock != NULL ) {
#ifdef _WIN32 #ifdef _WIN32
ReleaseMutex(lock->handle); ReleaseMutex( lock->handle );
#else #else
pthread_mutex_unlock(&lock->handle); pthread_mutex_unlock(lock->handle);
#endif #endif
} }
} }
@ -88,21 +89,21 @@ VOID lock_release(LOCK * lock)
/* /*
* Create a new event which can be signaled/polled/and blocked on. * Create a new event which can be signaled/polled/and blocked on.
*/ */
EVENT * event_create(VOID) EVENT * event_create( VOID )
{ {
EVENT * event = NULL; EVENT * event = NULL;
event = (EVENT *)malloc(sizeof(EVENT)); event = (EVENT *)malloc( sizeof( EVENT ) );
if (event == NULL) if( event == NULL )
return NULL; return NULL;
memset(event, 0, sizeof(EVENT)); memset( event, 0, sizeof( EVENT ) );
#ifdef _WIN32 #ifdef _WIN32
event->handle = CreateEvent(NULL, FALSE, FALSE, NULL); event->handle = CreateEvent( NULL, FALSE, FALSE, NULL );
if (event->handle == NULL) if( event->handle == NULL )
{ {
free(event); free( event );
return NULL; return NULL;
} }
#endif #endif
@ -113,16 +114,16 @@ EVENT * event_create(VOID)
/* /*
* Destroy an event. * Destroy an event.
*/ */
BOOL event_destroy(EVENT * event) BOOL event_destroy( EVENT * event )
{ {
if (event == NULL) if( event == NULL )
return FALSE; return FALSE;
#ifdef _WIN32 #ifdef _WIN32
CloseHandle(event->handle); CloseHandle( event->handle );
#endif #endif
free(event); free( event );
return TRUE; return TRUE;
} }
@ -130,15 +131,15 @@ BOOL event_destroy(EVENT * event)
/* /*
* Signal an event. * Signal an event.
*/ */
BOOL event_signal(EVENT * event) BOOL event_signal( EVENT * event )
{ {
if (event == NULL) if( event == NULL )
return FALSE; return FALSE;
#ifdef _WIN32 #ifdef _WIN32
dprintf("Signalling 0x%x", event->handle); dprintf( "Signalling 0x%x", event->handle );
if (SetEvent(event->handle) == 0) { if( SetEvent( event->handle ) == 0 ) {
dprintf("Signalling 0x%x failed %u", event->handle, GetLastError()); dprintf( "Signalling 0x%x failed %u", event->handle, GetLastError() );
return FALSE; return FALSE;
} }
#else #else
@ -153,13 +154,13 @@ BOOL event_signal(EVENT * event)
* Poll an event to see if it has been signaled. Set timeout to -1 to block indefinatly. * Poll an event to see if it has been signaled. Set timeout to -1 to block indefinatly.
* If timeout is 0 this function does not block but returns immediately. * If timeout is 0 this function does not block but returns immediately.
*/ */
BOOL event_poll(EVENT * event, DWORD timeout) BOOL event_poll( EVENT * event, DWORD timeout )
{ {
#ifdef _WIN32 #ifdef _WIN32
if (event == NULL) if( event == NULL )
return FALSE; return FALSE;
if (WaitForSingleObject(event->handle, timeout) == WAIT_OBJECT_0) if( WaitForSingleObject( event->handle, timeout ) == WAIT_OBJECT_0 )
return TRUE; return TRUE;
return FALSE; return FALSE;
@ -169,13 +170,13 @@ BOOL event_poll(EVENT * event, DWORD timeout)
// DWORD WINAPI WaitForSingleObject( // DWORD WINAPI WaitForSingleObject(
// __in HANDLE hHandle, // __in HANDLE hHandle,
// __in DWORD dwMilliseconds // __in DWORD dwMilliseconds
//); // );
// http://msdn.microsoft.com/en-us/library/ms687032(VS.85).aspx // http://msdn.microsoft.com/en-us/library/ms687032(VS.85).aspx
if (event == NULL) if( event == NULL )
return FALSE; return FALSE;
if (timeout) { if(timeout) {
struct timespec ts; struct timespec ts;
// XXX, need to verify for -1. below modified from bionic/pthread.c // XXX, need to verify for -1. below modified from bionic/pthread.c
@ -197,7 +198,7 @@ BOOL event_poll(EVENT * event, DWORD timeout)
// We should behave like an auto-reset event // We should behave like an auto-reset event
result = event->handle ? TRUE : FALSE; result = event->handle ? TRUE : FALSE;
if (result) if( result )
event->handle = (HANDLE)0; event->handle = (HANDLE)0;
return result; return result;
@ -209,7 +210,7 @@ BOOL event_poll(EVENT * event, DWORD timeout)
/* /*
* Opens and create a THREAD item for the current/calling thread. * Opens and create a THREAD item for the current/calling thread.
*/ */
THREAD * thread_open(VOID) THREAD * thread_open( VOID )
{ {
THREAD * thread = NULL; THREAD * thread = NULL;
#ifdef _WIN32 #ifdef _WIN32
@ -217,10 +218,10 @@ THREAD * thread_open(VOID)
HMODULE hKernel32 = NULL; HMODULE hKernel32 = NULL;
thread = (THREAD *)malloc(sizeof(THREAD)); thread = (THREAD *)malloc( sizeof( THREAD ) );
if (thread != NULL) if( thread != NULL )
{ {
memset(thread, 0, sizeof(THREAD)); memset( thread, 0, sizeof(THREAD) );
thread->id = GetCurrentThreadId(); thread->id = GetCurrentThreadId();
thread->sigterm = event_create(); thread->sigterm = event_create();
@ -230,41 +231,41 @@ THREAD * thread_open(VOID)
// for now. // for now.
// First we try to use the normal OpenThread function, available on Windows 2000 and up... // First we try to use the normal OpenThread function, available on Windows 2000 and up...
hKernel32 = LoadLibrary("kernel32.dll"); hKernel32 = LoadLibrary( "kernel32.dll" );
pOpenThread = (OPENTHREAD)GetProcAddress(hKernel32, "OpenThread"); pOpenThread = (OPENTHREAD)GetProcAddress( hKernel32, "OpenThread" );
if (pOpenThread) if( pOpenThread )
{ {
thread->handle = pOpenThread(THREAD_TERMINATE|THREAD_SUSPEND_RESUME, FALSE, thread->id); thread->handle = pOpenThread( THREAD_TERMINATE|THREAD_SUSPEND_RESUME, FALSE, thread->id );
} }
else else
{ {
NTOPENTHREAD pNtOpenThread = NULL; NTOPENTHREAD pNtOpenThread = NULL;
// If we can't use OpenThread, we try the older NtOpenThread function as found on NT4 machines. // If we can't use OpenThread, we try the older NtOpenThread function as found on NT4 machines.
HMODULE hNtDll = LoadLibrary("ntdll.dll"); HMODULE hNtDll = LoadLibrary( "ntdll.dll" );
pNtOpenThread = (NTOPENTHREAD)GetProcAddress(hNtDll, "NtOpenThread"); pNtOpenThread = (NTOPENTHREAD)GetProcAddress( hNtDll, "NtOpenThread" );
if (pNtOpenThread) if( pNtOpenThread )
{ {
_OBJECT_ATTRIBUTES oa = {0}; _OBJECT_ATTRIBUTES oa = {0};
_CLIENT_ID cid = {0}; _CLIENT_ID cid = {0};
cid.UniqueThread = (PVOID)thread->id; cid.UniqueThread = (PVOID)thread->id;
pNtOpenThread(&thread->handle, THREAD_TERMINATE|THREAD_SUSPEND_RESUME, &oa, &cid); pNtOpenThread( &thread->handle, THREAD_TERMINATE|THREAD_SUSPEND_RESUME, &oa, &cid );
} }
FreeLibrary(hNtDll); FreeLibrary( hNtDll );
} }
FreeLibrary(hKernel32); FreeLibrary( hKernel32 );
} }
return thread; return thread;
#else #else
thread = (THREAD *)malloc(sizeof(THREAD)); thread = (THREAD *)malloc( sizeof( THREAD ) );
if (thread != NULL) if( thread != NULL )
{ {
memset(thread, 0, sizeof(THREAD)); memset( thread, 0, sizeof(THREAD) );
thread->id = gettid(); thread->id = gettid();
thread->sigterm = event_create(); thread->sigterm = event_create();
@ -319,7 +320,7 @@ void *__paused_thread(void *req)
thread = tc->thread; thread = tc->thread;
free(tc); free(tc);
if (event_poll(thread->sigterm, 0) == TRUE) { if(event_poll(thread->sigterm, 0) == TRUE) {
/* /*
* In some cases, we might want to stop a thread before it does anything :/ * In some cases, we might want to stop a thread before it does anything :/
*/ */
@ -333,23 +334,23 @@ void *__paused_thread(void *req)
/* /*
* Create a new thread in a suspended state. * Create a new thread in a suspended state.
*/ */
THREAD * thread_create(THREADFUNK funk, LPVOID param1, LPVOID param2, LPVOID param3) THREAD * thread_create( THREADFUNK funk, LPVOID param1, LPVOID param2, LPVOID param3 )
{ {
THREAD * thread = NULL; THREAD * thread = NULL;
if (funk == NULL) if( funk == NULL )
return NULL; return NULL;
thread = (THREAD *)malloc(sizeof(THREAD)); thread = (THREAD *)malloc( sizeof( THREAD ) );
if (thread == NULL) if( thread == NULL )
return NULL; return NULL;
memset(thread, 0, sizeof(THREAD)); memset( thread, 0, sizeof( THREAD ) );
thread->sigterm = event_create(); thread->sigterm = event_create();
if (thread->sigterm == NULL) if( thread->sigterm == NULL )
{ {
free(thread); free( thread );
return NULL; return NULL;
} }
@ -359,12 +360,12 @@ THREAD * thread_create(THREADFUNK funk, LPVOID param1, LPVOID param2, LPVOID par
thread->parameter3 = param3; thread->parameter3 = param3;
#ifdef _WIN32 #ifdef _WIN32
thread->handle = CreateThread(NULL, 0, funk, thread, CREATE_SUSPENDED, &thread->id); thread->handle = CreateThread( NULL, 0, funk, thread, CREATE_SUSPENDED, &thread->id );
if (thread->handle == NULL) if( thread->handle == NULL )
{ {
event_destroy(thread->sigterm); event_destroy( thread->sigterm );
free(thread); free( thread );
return NULL; return NULL;
} }
@ -380,13 +381,13 @@ THREAD * thread_create(THREADFUNK funk, LPVOID param1, LPVOID param2, LPVOID par
struct thread_conditional *tc; struct thread_conditional *tc;
tc = (struct thread_conditional *) malloc(sizeof(struct thread_conditional)); tc = (struct thread_conditional *) malloc(sizeof(struct thread_conditional));
if (tc == NULL) { if( tc == NULL ) {
event_destroy(thread->sigterm); event_destroy(thread->sigterm);
free(thread); free(thread);
return NULL; return NULL;
} }
memset(tc, 0, sizeof(struct thread_conditional)); memset( tc, 0, sizeof(struct thread_conditional));
pthread_mutex_init(&tc->suspend_mutex, NULL); pthread_mutex_init(&tc->suspend_mutex, NULL);
pthread_cond_init(&tc->suspend_cond, NULL); pthread_cond_init(&tc->suspend_cond, NULL);
@ -396,7 +397,7 @@ THREAD * thread_create(THREADFUNK funk, LPVOID param1, LPVOID param2, LPVOID par
thread->suspend_thread_data = (void *)(tc); thread->suspend_thread_data = (void *)(tc);
if (pthread_create(&(thread->pid), NULL, __paused_thread, tc) == -1) { if(pthread_create(&(thread->pid), NULL, __paused_thread, tc) == -1) {
free(tc); free(tc);
event_destroy(thread->sigterm); event_destroy(thread->sigterm);
free(thread); free(thread);
@ -413,13 +414,13 @@ THREAD * thread_create(THREADFUNK funk, LPVOID param1, LPVOID param2, LPVOID par
/* /*
* Run a thread. * Run a thread.
*/ */
BOOL thread_run(THREAD * thread) BOOL thread_run( THREAD * thread )
{ {
if (thread == NULL) if( thread == NULL )
return FALSE; return FALSE;
#ifdef _WIN32 #ifdef _WIN32
if (ResumeThread(thread->handle) < 0) if( ResumeThread( thread->handle ) < 0 )
return FALSE; return FALSE;
#else #else
@ -439,14 +440,14 @@ BOOL thread_run(THREAD * thread)
* Signals the thread to terminate. It is the responsibility of the thread to wait for and process this signal. * Signals the thread to terminate. It is the responsibility of the thread to wait for and process this signal.
* Should be used to signal the thread to terminate. * Should be used to signal the thread to terminate.
*/ */
BOOL thread_sigterm(THREAD * thread) BOOL thread_sigterm( THREAD * thread )
{ {
BOOL ret; BOOL ret;
if (thread == NULL) if( thread == NULL )
return FALSE; return FALSE;
ret = event_signal(thread->sigterm); ret = event_signal( thread->sigterm );
#ifndef _WIN32 #ifndef _WIN32
/* /*
@ -455,7 +456,7 @@ BOOL thread_sigterm(THREAD * thread)
* *
* Therefore, we need to start the thread executing before calling thread_join * Therefore, we need to start the thread executing before calling thread_join
*/ */
if (thread->thread_started != TRUE) { if(thread->thread_started != TRUE) {
thread_run(thread); thread_run(thread);
} }
#endif #endif
@ -466,13 +467,13 @@ BOOL thread_sigterm(THREAD * thread)
/* /*
* Terminate a thread. Use with caution! better to signal your thread to terminate and wait for it to do so. * Terminate a thread. Use with caution! better to signal your thread to terminate and wait for it to do so.
*/ */
BOOL thread_kill(THREAD * thread) BOOL thread_kill( THREAD * thread )
{ {
if (thread == NULL) if( thread == NULL )
return FALSE; return FALSE;
#ifdef _WIN32 #ifdef _WIN32
if (TerminateThread(thread->handle, -1) == 0) if( TerminateThread( thread->handle, -1 ) == 0 )
return FALSE; return FALSE;
return TRUE; return TRUE;
@ -497,18 +498,18 @@ BOOL thread_kill(THREAD * thread)
/* /*
* Blocks untill the thread has terminated. * Blocks untill the thread has terminated.
*/ */
BOOL thread_join(THREAD * thread) BOOL thread_join( THREAD * thread )
{ {
if (thread == NULL) if( thread == NULL )
return FALSE; return FALSE;
#ifdef _WIN32 #ifdef _WIN32
if (WaitForSingleObject(thread->handle, INFINITE) == WAIT_OBJECT_0) if( WaitForSingleObject( thread->handle, INFINITE ) == WAIT_OBJECT_0 )
return TRUE; return TRUE;
return FALSE; return FALSE;
#else #else
if (pthread_join(thread->pid, NULL) == 0) if(pthread_join(thread->pid, NULL) == 0)
return TRUE; return TRUE;
return FALSE; return FALSE;
@ -519,20 +520,20 @@ BOOL thread_join(THREAD * thread)
* Destroys a previously created thread. Note, this does not terminate the thread. You must signal your * Destroys a previously created thread. Note, this does not terminate the thread. You must signal your
* thread to terminate and wait for it to do so (via thread_signal/thread_join). * thread to terminate and wait for it to do so (via thread_signal/thread_join).
*/ */
BOOL thread_destroy(THREAD * thread) BOOL thread_destroy( THREAD * thread )
{ {
if (thread == NULL) if( thread == NULL )
return FALSE; return FALSE;
event_destroy(thread->sigterm); event_destroy( thread->sigterm );
#ifdef _WIN32 #ifdef _WIN32
CloseHandle(thread->handle); CloseHandle( thread->handle );
#else #else
pthread_detach(thread->pid); pthread_detach(thread->pid);
#endif #endif
free(thread); free( thread );
return TRUE; return TRUE;
} }

@ -44,7 +44,7 @@ typedef struct _LOCK
#ifdef _WIN32 #ifdef _WIN32
HANDLE handle; HANDLE handle;
#else #else
pthread_mutex_t handle; pthread_mutex_t *handle;
#endif // _WIN32 #endif // _WIN32
} LOCK, * LPLOCK; } LOCK, * LPLOCK;