Re: BUG: KASAN: null-ptr-deref in _raw_spin_lock_irq next-20240410

From: Naresh Kamboju
Date: Thu Apr 11 2024 - 10:44:27 EST


On Thu, 11 Apr 2024 at 09:55, David Gow <davidgow@xxxxxxxxxx> wrote:
>
> On Wed, 10 Apr 2024 at 23:23, Will Deacon <will@xxxxxxxxxx> wrote:
> >
> > On Wed, Apr 10, 2024 at 03:57:10PM +0530, Naresh Kamboju wrote:
> > > Following kernel crash noticed on Linux next-20240410 tag while running
> > > kunit testing on qemu-arm64 and qemu-x86_64.
> > >
> > > Reported-by: Linux Kernel Functional Testing <lkft@xxxxxxxxxx>
> > >

<trim>

> >
> > Ok, so 'task_struct->vfork_done' is NULL. Looks like this code was added
> > recently, so adding Mickaël to cc.
> >
>
> Thanks. This looks like a race condition where the KUnit test kthread
> can terminate before we wait on it.
>
> Mickaël, does this seem like a correct fix to you?
> ---
> From: David Gow <davidgow@xxxxxxxxxx>
> Date: Thu, 11 Apr 2024 12:07:47 +0800
> Subject: [PATCH] kunit: Fix race condition in try-catch completion
>
> KUnit's try-catch infrastructure now uses vfork_done, which is always
> set to a valid completion when a kthread is crated, but which is set to
> NULL once the thread terminates. This creates a race condition, where
> the kthread exits before we can wait on it.
>
> Keep a copy of vfork_done, which is taken before we wake_up_process()
> and so valid, and wait on that instead.
>
> Fixes: 4de2a8e4cca4 ("kunit: Handle test faults")
> Reported-by: Linux Kernel Functional Testing <lkft@xxxxxxxxxx>
> Signed-off-by: David Gow <davidgow@xxxxxxxxxx>

This patch tested on top of Linux next and reported issues fixed.

Tested-by: Linux Kernel Functional Testing <lkft@xxxxxxxxxx>

> ---
> lib/kunit/try-catch.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/lib/kunit/try-catch.c b/lib/kunit/try-catch.c
> index fa687278ccc9..fc6cd4d7e80f 100644
> --- a/lib/kunit/try-catch.c
> +++ b/lib/kunit/try-catch.c
> @@ -63,6 +63,7 @@ void kunit_try_catch_run(struct kunit_try_catch
> *try_catch, void *context)
> {
> struct kunit *test = try_catch->test;
> struct task_struct *task_struct;
> + struct completion *task_done;
> int exit_code, time_remaining;
>
> try_catch->context = context;
> @@ -75,13 +76,14 @@ void kunit_try_catch_run(struct kunit_try_catch
> *try_catch, void *context)
> return;
> }
> get_task_struct(task_struct);
> + task_done = task_struct->vfork_done;
> wake_up_process(task_struct);
> /*
> * As for a vfork(2), task_struct->vfork_done (pointing to the
> * underlying kthread->exited) can be used to wait for the end of a
> * kernel thread.
> */
> - time_remaining = wait_for_completion_timeout(task_struct->vfork_done,
> + time_remaining = wait_for_completion_timeout(task_done,
> kunit_test_timeout());
> if (time_remaining == 0) {
> try_catch->try_result = -ETIMEDOUT;
> --

--
Linaro LKFT
https://lkft.linaro.org