Skip to content

Commit f8aa1b9

Browse files
l0kodshuahkh
authored andcommitted
kunit: Fix kthread reference
There is a race condition when a kthread finishes after the deadline and before the call to kthread_stop(), which may lead to use after free. Cc: Brendan Higgins <[email protected]> Cc: Shuah Khan <[email protected]> Reviewed-by: Kees Cook <[email protected]> Fixes: adf5054 ("kunit: fix UAF when run kfence test case test_gfpzero") Reviewed-by: David Gow <[email protected]> Reviewed-by: Rae Moar <[email protected]> Signed-off-by: Mickaël Salaün <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Shuah Khan <[email protected]>
1 parent cde5e1b commit f8aa1b9

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

lib/kunit/try-catch.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/completion.h>
1212
#include <linux/kernel.h>
1313
#include <linux/kthread.h>
14+
#include <linux/sched/task.h>
1415

1516
#include "try-catch-impl.h"
1617

@@ -65,14 +66,15 @@ void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)
6566
try_catch->context = context;
6667
try_catch->try_completion = &try_completion;
6768
try_catch->try_result = 0;
68-
task_struct = kthread_run(kunit_generic_run_threadfn_adapter,
69-
try_catch,
70-
"kunit_try_catch_thread");
69+
task_struct = kthread_create(kunit_generic_run_threadfn_adapter,
70+
try_catch, "kunit_try_catch_thread");
7171
if (IS_ERR(task_struct)) {
7272
try_catch->try_result = PTR_ERR(task_struct);
7373
try_catch->catch(try_catch->context);
7474
return;
7575
}
76+
get_task_struct(task_struct);
77+
wake_up_process(task_struct);
7678

7779
time_remaining = wait_for_completion_timeout(&try_completion,
7880
kunit_test_timeout());
@@ -82,6 +84,7 @@ void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)
8284
kthread_stop(task_struct);
8385
}
8486

87+
put_task_struct(task_struct);
8588
exit_code = try_catch->try_result;
8689

8790
if (!exit_code)

0 commit comments

Comments
 (0)