Re: [PATCH] workqueue: Fix UAF report by KASAN in pwq_release_workfn()

From: Tejun Heo
Date: Tue Sep 05 2023 - 14:53:27 EST


Hello,

On Sat, Sep 02, 2023 at 07:50:26PM +0800, Zqiang wrote:
> diff --git a/kernel/workqueue.c b/kernel/workqueue.c
> index c85825e17df8..f3f9c9222070 100644
> --- a/kernel/workqueue.c
> +++ b/kernel/workqueue.c
> @@ -4609,6 +4609,10 @@ static int alloc_and_link_pwqs(struct workqueue_struct *wq)
> free_percpu(wq->cpu_pwq);
> wq->cpu_pwq = NULL;
> }
> + wq_unregister_lockdep(wq);
> + wq_free_lockdep(wq);
> + free_workqueue_attrs(wq->unbound_attrs);
> + kfree(wq);

So, this would fix the bug but it's quite confusing because we end up taking
two separate error handling paths and alloc_and_link_pwqs() ends up freeing
stuff allocated outside the function. Wouldn't it be enough to flush the
pwq_release_worker after apply_workqueue_attrs() failure so that the cleanup
operation ordering is maintained?

Thanks.

--
tejun