All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] workqueue: Fix UAF report by KASAN in pwq_release_workfn()
@ 2023-09-02 11:50 Zqiang
  2023-09-05 18:51 ` Tejun Heo
  0 siblings, 1 reply; 10+ messages in thread
From: Zqiang @ 2023-09-02 11:50 UTC (permalink / raw)
  To: tj, jiangshanlai, qiang.zhang1211; +Cc: linux-kernel

Currently, for UNBOUND workqueue allocation failure, the
apply_wqattr_cleanup() will be called and use the pwq_release_worker
kthread to release resources asynchronously. however, the kfree(wq)
is invoked directly in failure path of alloc_workqueue(), this leads
to the following scenario:

BUG: KASAN: slab-use-after-free in pwq_release_workfn+0x339/0x380 kernel/workqueue.c:4124
Read of size 4 at addr ffff888027b831c0 by task pool_workqueue_/3

CPU: 0 PID: 3 Comm: pool_workqueue_ Not tainted 6.5.0-rc7-next-20230825-syzkaller #0
Hardware name: Google Compute Engine/Google Compute Engine, BIOS Google 07/26/2023
Call Trace:
 <TASK>
 __dump_stack lib/dump_stack.c:88 [inline]
 dump_stack_lvl+0xd9/0x1b0 lib/dump_stack.c:106
 print_address_description mm/kasan/report.c:364 [inline]
 print_report+0xc4/0x620 mm/kasan/report.c:475
 kasan_report+0xda/0x110 mm/kasan/report.c:588
 pwq_release_workfn+0x339/0x380 kernel/workqueue.c:4124
 kthread_worker_fn+0x2fc/0xa80 kernel/kthread.c:823
 kthread+0x33a/0x430 kernel/kthread.c:388
 ret_from_fork+0x45/0x80 arch/x86/kernel/process.c:147
 ret_from_fork_asm+0x11/0x20 arch/x86/entry/entry_64.S:304
 </TASK>

Allocated by task 5054:
 kasan_save_stack+0x33/0x50 mm/kasan/common.c:45
 kasan_set_track+0x25/0x30 mm/kasan/common.c:52
 ____kasan_kmalloc mm/kasan/common.c:374 [inline]
 __kasan_kmalloc+0xa2/0xb0 mm/kasan/common.c:383
 kmalloc include/linux/slab.h:599 [inline]
 kzalloc include/linux/slab.h:720 [inline]
 alloc_workqueue+0x16f/0x1490 kernel/workqueue.c:4684
 kvm_mmu_init_tdp_mmu+0x23/0x100 arch/x86/kvm/mmu/tdp_mmu.c:19
 kvm_mmu_init_vm+0x248/0x2e0 arch/x86/kvm/mmu/mmu.c:6180
 kvm_arch_init_vm+0x39/0x720 arch/x86/kvm/x86.c:12311
 kvm_create_vm arch/x86/kvm/../../../virt/kvm/kvm_main.c:1222 [inline]
 kvm_dev_ioctl_create_vm arch/x86/kvm/../../../virt/kvm/kvm_main.c:5089 [inline]
 kvm_dev_ioctl+0xa31/0x1c20 arch/x86/kvm/../../../virt/kvm/kvm_main.c:5131
 vfs_ioctl fs/ioctl.c:51 [inline]
 __do_sys_ioctl fs/ioctl.c:871 [inline]
 __se_sys_ioctl fs/ioctl.c:857 [inline]
 __x64_sys_ioctl+0x18f/0x210 fs/ioctl.c:857
 do_syscall_x64 arch/x86/entry/common.c:50 [inline]
 do_syscall_64+0x38/0xb0 arch/x86/entry/common.c:80
 entry_SYSCALL_64_after_hwframe+0x63/0xcd

Freed by task 5054:
 kasan_save_stack+0x33/0x50 mm/kasan/common.c:45
 kasan_set_track+0x25/0x30 mm/kasan/common.c:52
 kasan_save_free_info+0x2b/0x40 mm/kasan/generic.c:522
 ____kasan_slab_free mm/kasan/common.c:236 [inline]
 ____kasan_slab_free+0x15b/0x1b0 mm/kasan/common.c:200
 kasan_slab_free include/linux/kasan.h:164 [inline]
 slab_free_hook mm/slub.c:1800 [inline]
 slab_free_freelist_hook+0x114/0x1e0 mm/slub.c:1826
 slab_free mm/slub.c:3809 [inline]
 __kmem_cache_free+0xb8/0x2f0 mm/slub.c:3822
 alloc_workqueue+0xe76/0x1490 kernel/workqueue.c:4746
 kvm_mmu_init_tdp_mmu+0x23/0x100 arch/x86/kvm/mmu/tdp_mmu.c:19
 kvm_mmu_init_vm+0x248/0x2e0 arch/x86/kvm/mmu/mmu.c:6180
 kvm_arch_init_vm+0x39/0x720 arch/x86/kvm/x86.c:12311
 kvm_create_vm arch/x86/kvm/../../../virt/kvm/kvm_main.c:1222 [inline]
 kvm_dev_ioctl_create_vm arch/x86/kvm/../../../virt/kvm/kvm_main.c:5089 [inline]
 kvm_dev_ioctl+0xa31/0x1c20 arch/x86/kvm/../../../virt/kvm/kvm_main.c:5131
 vfs_ioctl fs/ioctl.c:51 [inline]
 __do_sys_ioctl fs/ioctl.c:871 [inline]
 __se_sys_ioctl fs/ioctl.c:857 [inline]
 __x64_sys_ioctl+0x18f/0x210 fs/ioctl.c:857
 do_syscall_x64 arch/x86/entry/common.c:50 [inline]
 do_syscall_64+0x38/0xb0 arch/x86/entry/common.c:80
 entry_SYSCALL_64_after_hwframe+0x63/0xcd

This commit therefore only direct release resources when the
!UNBOUND workqueue allocation failure.

Reported-by: syzbot+60db9f652c92d5bacba4@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=60db9f652c92d5bacba4
Signed-off-by: Zqiang <qiang.zhang1211@gmail.com>
---
 kernel/workqueue.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

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);
 	return -ENOMEM;
 }
 
@@ -4712,7 +4716,7 @@ struct workqueue_struct *alloc_workqueue(const char *fmt,
 	INIT_LIST_HEAD(&wq->list);
 
 	if (alloc_and_link_pwqs(wq) < 0)
-		goto err_unreg_lockdep;
+		return NULL;
 
 	if (wq_online && init_rescuer(wq) < 0)
 		goto err_destroy;
@@ -4738,11 +4742,7 @@ struct workqueue_struct *alloc_workqueue(const char *fmt,
 
 	return wq;
 
-err_unreg_lockdep:
-	wq_unregister_lockdep(wq);
-	wq_free_lockdep(wq);
 err_free_wq:
-	free_workqueue_attrs(wq->unbound_attrs);
 	kfree(wq);
 	return NULL;
 err_destroy:
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH] workqueue: Fix UAF report by KASAN in pwq_release_workfn()
  2023-09-02 11:50 [PATCH] workqueue: Fix UAF report by KASAN in pwq_release_workfn() Zqiang
@ 2023-09-05 18:51 ` Tejun Heo
  2023-09-06  2:12   ` Z qiang
  0 siblings, 1 reply; 10+ messages in thread
From: Tejun Heo @ 2023-09-05 18:51 UTC (permalink / raw)
  To: Zqiang; +Cc: jiangshanlai, linux-kernel

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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] workqueue: Fix UAF report by KASAN in pwq_release_workfn()
  2023-09-05 18:51 ` Tejun Heo
@ 2023-09-06  2:12   ` Z qiang
  2023-09-06 17:01     ` Tejun Heo
  0 siblings, 1 reply; 10+ messages in thread
From: Z qiang @ 2023-09-06  2:12 UTC (permalink / raw)
  To: Tejun Heo; +Cc: jiangshanlai, linux-kernel

>
> 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?

Hello, Tejun

Flush the pwq_release_worker is insufficient, the call_rcu() is
invoked to release wq
in pwq_release_workfn(), this is also asynchronous.


Thanks
Zqiang

>
> Thanks.
>
> --
> tejun

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] workqueue: Fix UAF report by KASAN in pwq_release_workfn()
  2023-09-06  2:12   ` Z qiang
@ 2023-09-06 17:01     ` Tejun Heo
  2023-09-07  2:13       ` Z qiang
  0 siblings, 1 reply; 10+ messages in thread
From: Tejun Heo @ 2023-09-06 17:01 UTC (permalink / raw)
  To: Z qiang; +Cc: jiangshanlai, linux-kernel

On Wed, Sep 06, 2023 at 10:12:34AM +0800, Z qiang wrote:
> Flush the pwq_release_worker is insufficient, the call_rcu() is
> invoked to release wq
> in pwq_release_workfn(), this is also asynchronous.

But rcu_free_pwq() doesn't access wq or anything. The last access is from
the work function.

Thanks.

-- 
tejun

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] workqueue: Fix UAF report by KASAN in pwq_release_workfn()
  2023-09-06 17:01     ` Tejun Heo
@ 2023-09-07  2:13       ` Z qiang
  2023-09-18  9:18         ` Z qiang
  2023-09-18 18:06         ` Tejun Heo
  0 siblings, 2 replies; 10+ messages in thread
From: Z qiang @ 2023-09-07  2:13 UTC (permalink / raw)
  To: Tejun Heo; +Cc: jiangshanlai, linux-kernel

>
> On Wed, Sep 06, 2023 at 10:12:34AM +0800, Z qiang wrote:
> > Flush the pwq_release_worker is insufficient, the call_rcu() is
> > invoked to release wq
> > in pwq_release_workfn(), this is also asynchronous.
>
> But rcu_free_pwq() doesn't access wq or anything. The last access is from
> the work function.

The rcu_free_wq() will access wq->cpu_pwq or unbound_attrs,
but  at this time, the kfree(wq) may have been called in alloc_workqueue().

Thanks
Zqiang


>
> Thanks.
>
> --
> tejun

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] workqueue: Fix UAF report by KASAN in pwq_release_workfn()
  2023-09-07  2:13       ` Z qiang
@ 2023-09-18  9:18         ` Z qiang
  2023-09-18 18:06         ` Tejun Heo
  1 sibling, 0 replies; 10+ messages in thread
From: Z qiang @ 2023-09-18  9:18 UTC (permalink / raw)
  To: Tejun Heo; +Cc: jiangshanlai, linux-kernel

>
> >
> > On Wed, Sep 06, 2023 at 10:12:34AM +0800, Z qiang wrote:
> > > Flush the pwq_release_worker is insufficient, the call_rcu() is
> > > invoked to release wq
> > > in pwq_release_workfn(), this is also asynchronous.
> >
> > But rcu_free_pwq() doesn't access wq or anything. The last access is from
> > the work function.
>
> The rcu_free_wq() will access wq->cpu_pwq or unbound_attrs,
> but  at this time, the kfree(wq) may have been called in alloc_workqueue().
>

Friendly ping.

Thanks
Zqiang

> Thanks
> Zqiang
>
>
> >
> > Thanks.
> >
> > --
> > tejun

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] workqueue: Fix UAF report by KASAN in pwq_release_workfn()
  2023-09-07  2:13       ` Z qiang
  2023-09-18  9:18         ` Z qiang
@ 2023-09-18 18:06         ` Tejun Heo
  2023-09-19  2:14           ` Z qiang
  1 sibling, 1 reply; 10+ messages in thread
From: Tejun Heo @ 2023-09-18 18:06 UTC (permalink / raw)
  To: Z qiang; +Cc: jiangshanlai, linux-kernel

On Thu, Sep 07, 2023 at 10:13:23AM +0800, Z qiang wrote:
> >
> > On Wed, Sep 06, 2023 at 10:12:34AM +0800, Z qiang wrote:
> > > Flush the pwq_release_worker is insufficient, the call_rcu() is
> > > invoked to release wq
> > > in pwq_release_workfn(), this is also asynchronous.
> >
> > But rcu_free_pwq() doesn't access wq or anything. The last access is from
> > the work function.
> 
> The rcu_free_wq() will access wq->cpu_pwq or unbound_attrs,
> but  at this time, the kfree(wq) may have been called in alloc_workqueue().

I'm not following. The only way alloc_and_link fails is if
apply_wqattrs_prepare() fails and if prepare fails, none of the pwq's are
installed and pwq_unbound_release_workfn() won't try to free the wq as the
pwq's don't have any reference on it. So, if you flush the pwq release work
items, there can be no rcu_free_wq() in flight. Can you please try to see
whether the problem is reproducible with flushing?

Thanks.

-- 
tejun

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] workqueue: Fix UAF report by KASAN in pwq_release_workfn()
  2023-09-18 18:06         ` Tejun Heo
@ 2023-09-19  2:14           ` Z qiang
  2023-12-08  7:32             ` Xuewen Yan
  0 siblings, 1 reply; 10+ messages in thread
From: Z qiang @ 2023-09-19  2:14 UTC (permalink / raw)
  To: Tejun Heo; +Cc: jiangshanlai, linux-kernel

>
> On Thu, Sep 07, 2023 at 10:13:23AM +0800, Z qiang wrote:
> > >
> > > On Wed, Sep 06, 2023 at 10:12:34AM +0800, Z qiang wrote:
> > > > Flush the pwq_release_worker is insufficient, the call_rcu() is
> > > > invoked to release wq
> > > > in pwq_release_workfn(), this is also asynchronous.
> > >
> > > But rcu_free_pwq() doesn't access wq or anything. The last access is from
> > > the work function.
> >
> > The rcu_free_wq() will access wq->cpu_pwq or unbound_attrs,
> > but  at this time, the kfree(wq) may have been called in alloc_workqueue().
>
> I'm not following. The only way alloc_and_link fails is if
> apply_wqattrs_prepare() fails and if prepare fails, none of the pwq's are
> installed and pwq_unbound_release_workfn() won't try to free the wq as the
> pwq's don't have any reference on it. So, if you flush the pwq release work
> items, there can be no rcu_free_wq() in flight. Can you please try to see
> whether the problem is reproducible with flushing?
>

you are right .  sorry,  I ignore if apply_wqattrs_prepare() fails,
none of the pwq is installed,
the install_unbound_pwq() is not invoked. I will resend v2 and test.

Thanks
Zqiang

>
> Thanks.
>
> --
> tejun

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] workqueue: Fix UAF report by KASAN in pwq_release_workfn()
  2023-09-19  2:14           ` Z qiang
@ 2023-12-08  7:32             ` Xuewen Yan
  2023-12-08  8:18               ` Z qiang
  0 siblings, 1 reply; 10+ messages in thread
From: Xuewen Yan @ 2023-12-08  7:32 UTC (permalink / raw)
  To: Z qiang; +Cc: Tejun Heo, jiangshanlai, linux-kernel, guohua.yan

Hi qiang && Tejun

On Tue, Sep 19, 2023 at 7:30 PM Z qiang <qiang.zhang1211@gmail.com> wrote:
>
> >
> > On Thu, Sep 07, 2023 at 10:13:23AM +0800, Z qiang wrote:
> > > >
> > > > On Wed, Sep 06, 2023 at 10:12:34AM +0800, Z qiang wrote:
> > > > > Flush the pwq_release_worker is insufficient, the call_rcu() is
> > > > > invoked to release wq
> > > > > in pwq_release_workfn(), this is also asynchronous.
> > > >
> > > > But rcu_free_pwq() doesn't access wq or anything. The last access is from
> > > > the work function.
> > >
> > > The rcu_free_wq() will access wq->cpu_pwq or unbound_attrs,
> > > but  at this time, the kfree(wq) may have been called in alloc_workqueue().
> >
> > I'm not following. The only way alloc_and_link fails is if
> > apply_wqattrs_prepare() fails and if prepare fails, none of the pwq's are
> > installed and pwq_unbound_release_workfn() won't try to free the wq as the
> > pwq's don't have any reference on it. So, if you flush the pwq release work
> > items, there can be no rcu_free_wq() in flight. Can you please try to see
> > whether the problem is reproducible with flushing?
> >
>
> you are right .  sorry,  I ignore if apply_wqattrs_prepare() fails,
> none of the pwq is installed,
> the install_unbound_pwq() is not invoked. I will resend v2 and test.

I want to ask a question, why delete the v1?
Although add the flush_work, the release work would release the lockdep key,
and at the last of alloc_workqueue, it would also unregister_lockdep_key twice.

And other question, why not free lockdep and just unregister_lockdep?

4136         if (is_last) {
4137                 wq_unregister_lockdep(wq);
4138                 call_rcu(&wq->rcu, rcu_free_wq);
4139         }

I would really appreciate it if you could help explain it.

Thanks!
BR



>
> Thanks
> Zqiang
>
> >
> > Thanks.
> >
> > --
> > tejun

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] workqueue: Fix UAF report by KASAN in pwq_release_workfn()
  2023-12-08  7:32             ` Xuewen Yan
@ 2023-12-08  8:18               ` Z qiang
  0 siblings, 0 replies; 10+ messages in thread
From: Z qiang @ 2023-12-08  8:18 UTC (permalink / raw)
  To: Xuewen Yan; +Cc: Tejun Heo, jiangshanlai, linux-kernel, guohua.yan

>
> Hi qiang && Tejun
>
> On Tue, Sep 19, 2023 at 7:30 PM Z qiang <qiang.zhang1211@gmail.com> wrote:
> >
> > >
> > > On Thu, Sep 07, 2023 at 10:13:23AM +0800, Z qiang wrote:
> > > > >
> > > > > On Wed, Sep 06, 2023 at 10:12:34AM +0800, Z qiang wrote:
> > > > > > Flush the pwq_release_worker is insufficient, the call_rcu() is
> > > > > > invoked to release wq
> > > > > > in pwq_release_workfn(), this is also asynchronous.
> > > > >
> > > > > But rcu_free_pwq() doesn't access wq or anything. The last access is from
> > > > > the work function.
> > > >
> > > > The rcu_free_wq() will access wq->cpu_pwq or unbound_attrs,
> > > > but  at this time, the kfree(wq) may have been called in alloc_workqueue().
> > >
> > > I'm not following. The only way alloc_and_link fails is if
> > > apply_wqattrs_prepare() fails and if prepare fails, none of the pwq's are
> > > installed and pwq_unbound_release_workfn() won't try to free the wq as the
> > > pwq's don't have any reference on it. So, if you flush the pwq release work
> > > items, there can be no rcu_free_wq() in flight. Can you please try to see
> > > whether the problem is reproducible with flushing?
> > >
> >
> > you are right .  sorry,  I ignore if apply_wqattrs_prepare() fails,
> > none of the pwq is installed,
> > the install_unbound_pwq() is not invoked. I will resend v2 and test.
>
> I want to ask a question, why delete the v1?
> Although add the flush_work, the release work would release the lockdep key,
> and at the last of alloc_workqueue, it would also unregister_lockdep_key twice.
>

For unbound wq,  if apply_workqueue_attrs() return error,  the link_pwq() will
not be invoked, that means will not insert pwq->pwqs_node to wq->pwqs list,
that also means the is_last is false in pwq_release_workfn().



> And other question, why not free lockdep and just unregister_lockdep?
>
> 4136         if (is_last) {
> 4137                 wq_unregister_lockdep(wq);
> 4138                 call_rcu(&wq->rcu, rcu_free_wq);
> 4139         }

free lockdep in rcu_free_wq();

Thanks
Zqiang

>
> I would really appreciate it if you could help explain it.
>
> Thanks!
> BR
>
>
>
> >
> > Thanks
> > Zqiang
> >
> > >
> > > Thanks.
> > >
> > > --
> > > tejun

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2023-12-08  8:18 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-02 11:50 [PATCH] workqueue: Fix UAF report by KASAN in pwq_release_workfn() Zqiang
2023-09-05 18:51 ` Tejun Heo
2023-09-06  2:12   ` Z qiang
2023-09-06 17:01     ` Tejun Heo
2023-09-07  2:13       ` Z qiang
2023-09-18  9:18         ` Z qiang
2023-09-18 18:06         ` Tejun Heo
2023-09-19  2:14           ` Z qiang
2023-12-08  7:32             ` Xuewen Yan
2023-12-08  8:18               ` Z qiang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.