Re: [PATCH] mm: blk-cgroup: fix use-after-free in cgwb_release_workfn()
From: Dennis Zhou
Date: Mon Apr 13 2026 - 15:41:55 EST
Hello,
On Mon, Apr 13, 2026 at 03:09:19AM -0700, Breno Leitao wrote:
> cgwb_release_workfn() calls css_put(wb->blkcg_css) and then later
> accesses wb->blkcg_css again via blkcg_unpin_online(). If css_put()
> drops the last reference, the blkcg can be freed asynchronously
> (css_free_rwork_fn -> blkcg_css_free -> kfree) before blkcg_unpin_online()
> dereferences the pointer to access blkcg->online_pin, resulting in a
> use-after-free:
>
> BUG: KASAN: slab-use-after-free in blkcg_unpin_online (./include/linux/instrumented.h:112 ./include/linux/atomic/atomic-instrumented.h:400 ./include/linux/refcount.h:389 ./include/linux/refcount.h:432 ./include/linux/refcount.h:450 block/blk-cgroup.c:1367)
> Write of size 4 at addr ff11000117aa6160 by task kworker/71:1/531
> Workqueue: cgwb_release cgwb_release_workfn
> Call Trace:
> <TASK>
> blkcg_unpin_online (./include/linux/instrumented.h:112 ./include/linux/atomic/atomic-instrumented.h:400 ./include/linux/refcount.h:389 ./include/linux/refcount.h:432 ./include/linux/refcount.h:450 block/blk-cgroup.c:1367)
> cgwb_release_workfn (mm/backing-dev.c:629)
> process_scheduled_works (kernel/workqueue.c:3278 kernel/workqueue.c:3385)
>
> Freed by task 1016:
> kfree (./include/linux/kasan.h:235 mm/slub.c:2689 mm/slub.c:6246 mm/slub.c:6561)
> css_free_rwork_fn (kernel/cgroup/cgroup.c:5542)
> process_scheduled_works (kernel/workqueue.c:3302 kernel/workqueue.c:3385)
>
> ** Stack based on commit 66672af7a095 ("Add linux-next specific files
> for 20260410")
>
> I am seeing this crash sporadically in Meta fleet across multiple
> kernel versions. A full reproducer is available at:
> https://github.com/leitao/debug/blob/main/reproducers/repro_blkcg_uaf.sh
>
> (The race window is narrow. To make it easily reproducible, inject
> a msleep(100) between css_put() and blkcg_unpin_online() in
> cgwb_release_workfn(). With that delay and a KASAN-enabled kernel, the
> reproducer triggers the splat reliably in less than a second.)
>
> Fix this by moving blkcg_unpin_online() before css_put(), so the
> cgwb's CSS reference keeps the blkcg alive while blkcg_unpin_online()
> accesses it.
>
> Fixes: 59b57717fff8 ("blkcg: delay blkg destruction until after writeback has finished")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Breno Leitao <leitao@xxxxxxxxxx>
> ---
> mm/backing-dev.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/mm/backing-dev.c b/mm/backing-dev.c
> index 7a18fa6c72725..cecbcf9060a65 100644
> --- a/mm/backing-dev.c
> +++ b/mm/backing-dev.c
> @@ -618,12 +618,13 @@ static void cgwb_release_workfn(struct work_struct *work)
> wb_shutdown(wb);
>
> css_put(wb->memcg_css);
> - css_put(wb->blkcg_css);
> - mutex_unlock(&wb->bdi->cgwb_release_mutex);
>
> /* triggers blkg destruction if no online users left */
> blkcg_unpin_online(wb->blkcg_css);
>
> + css_put(wb->blkcg_css);
> + mutex_unlock(&wb->bdi->cgwb_release_mutex);
> +
I haven't been in this code for quite some time, but does this need to
be protected by cgwb_release_mutex? My understanding is that
cgwb_release_mutex serializes wb_shutdown() between
cgwb_bdi_unregister() and cgwb_release_workfn().
> fprop_local_destroy_percpu(&wb->memcg_completions);
>
> spin_lock_irq(&cgwb_lock);
>
> ---
> base-commit: 66672af7a095d89f082c5327f3b15bc2f93d558e
> change-id: 20260413-blkcg-9b82762430f4
>
> Best regards,
> --
> Breno Leitao <leitao@xxxxxxxxxx>
>
Whoops. I think I made a bad assumption that wb implied a blkg existed
but if it never created one yet, then there's no blkg pinning the blkcg.
Either way that is tougher / more wrong than just keeping the blkcg_css
ref.
Reviewed-by: Dennis Zhou <dennis@xxxxxxxxxx>
Thanks,
Dennis