Skip to contentSkip to conversation

Fix use after stack unwind in fzsync lib

PATCH Superseded Martin Doucha 3 participants 3 messages Mar 25, 2020

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix use after stack unwind in fzsync lib

tst_fzsync_pair_reset() passes a local variable to thread B which may be already unwinded by the time the thread wrapper function executes. If new variables get allocated and initialized on stack between pthread_create() and thread wrapper execution, thread B will segfault.

---
 include/tst_fuzzy_sync.h | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
include/tst_fuzzy_sync.h +11 -4
index c1d0b00f9..32b9859a0 100644
@@ -242,11 +242,14 @@ struct tst_fzsync_run_thread {
  */
 static void *tst_fzsync_thread_wrapper(void *run_thread)
 {
-       struct tst_fzsync_run_thread t = *(struct tst_fzsync_run_thread *)run_thread;
+       struct tst_fzsync_run_thread *t = run_thread;
+       void *ret;
 
        pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
        pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
-       return t.func(t.arg);
+       ret = t->func(t->arg);
+       free(t);
+       return ret;
 }
 
 /**
@@ -297,8 +300,12 @@ static void tst_fzsync_pair_reset(struct tst_fzsync_pair *pair,
 	pair->b_cntr = 0;
 	pair->exit = 0;
 	if (run_b) {
-		struct tst_fzsync_run_thread wrap_run_b = {.func = run_b, .arg = NULL};
-		SAFE_PTHREAD_CREATE(&pair->thread_b, 0, tst_fzsync_thread_wrapper, &wrap_run_b);
+		struct tst_fzsync_run_thread *wrap_run_b;
+
+		wrap_run_b = SAFE_MALLOC(sizeof(struct tst_fzsync_run_thread));
+		wrap_run_b->func = run_b;
+		wrap_run_b->arg = NULL;
+		SAFE_PTHREAD_CREATE(&pair->thread_b, 0, tst_fzsync_thread_wrapper, wrap_run_b);
 	}
 
 	pair->exec_time_start = (float)tst_timeout_remaining();
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/1 Fix use after stack unwind in fzsync lib

Hello,

Quoted 17 lines · Martin Doucha

tst_fzsync_pair_reset() passes a local variable to thread B which may be already unwinded by the time the thread wrapper function executes. If new variables get allocated and initialized on stack between pthread_create() and thread wrapper execution, thread B will segfault.

---
 include/tst_fuzzy_sync.h | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
include/tst_fuzzy_sync.h +0 -0
index c1d0b00f9..32b9859a0 100644
@@ -242,11 +242,14 @@ struct tst_fzsync_run_thread {
  */
 static void *tst_fzsync_thread_wrapper(void *run_thread)

include/tst_fuzzy_sync.h struct tst_fzsync_run_thread { · line 251

Quoted 10 lines
include/tst_fuzzy_sync.h +4 -2
 {
-       struct tst_fzsync_run_thread t = *(struct tst_fzsync_run_thread *)run_thread;
+       struct tst_fzsync_run_thread *t = run_thread;
+       void *ret;
 
        pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
        pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
-       return t.func(t.arg);
+       ret = t->func(t->arg);
+       free(t);

Why not use SAFE_FREE?

Quoted 7 lines
include/tst_fuzzy_sync.h +1 -0
+       return ret;
 }
 
 /**
@@ -297,8 +300,12 @@ static void tst_fzsync_pair_reset(struct tst_fzsync_pair *pair,
 	pair->b_cntr = 0;
 	pair->exit = 0;

include/tst_fuzzy_sync.h static void tst_fzsync_pair_reset(struct tst_fzsync_pair *pair, · line 309

Quoted 10 lines
include/tst_fuzzy_sync.h +6 -2
 	if (run_b) {
-		struct tst_fzsync_run_thread wrap_run_b = {.func = run_b, .arg = NULL};
-		SAFE_PTHREAD_CREATE(&pair->thread_b, 0, tst_fzsync_thread_wrapper, &wrap_run_b);
+		struct tst_fzsync_run_thread *wrap_run_b;
+
+		wrap_run_b = SAFE_MALLOC(sizeof(struct tst_fzsync_run_thread));
+		wrap_run_b->func = run_b;
+		wrap_run_b->arg = NULL;
+		SAFE_PTHREAD_CREATE(&pair->thread_b, 0,
 tst_fzsync_thread_wrapper, wrap_run_b);

I suppose there is a memory leak here if thread create fails, but we probably don't care because the test will exit shortly afterwards.

Quoted 3 lines
include/tst_fuzzy_sync.h +0 -0
 	}
 

pair->exec_time_start = (float)tst_timeout_remaining();

Signature · 2 lines
Thank you,
Richard.
#2 lore raw

PATCH 1/1 Fix use after stack unwind in fzsync lib

Hi!

Quoted 7 lines

include/tst_fuzzy_sync.h struct tst_fzsync_run_thread { · line 251

Quoted 5 lines
        pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
        pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
include/tst_fuzzy_sync.h +2 -1
-       return t.func(t.arg);
+       ret = t->func(t->arg);
+       free(t);

Why not use SAFE_FREE?

free() does not report errors, there is no such thing as SAFE_FREE()

Signature · 2 lines
Cyril Hrubis
[email protected]