Re: [PATCH] mm/memcg: fix UAF in drain_all_stock() async work during offline
From: Andrew Morton
Date: Thu Aug 27 2026 - 18:35:42 EST
On Thu, 27 Aug 2026 12:42:11 -0400 Rik van Riel <riel@xxxxxxxxxxx> wrote:
> drain_all_stock() queues drain work on remote CPUs via
> schedule_drain_work() -> queue_work_on(memcg_wq) and returns
> immediately without waiting. The worker, drain_local_memcg_stock()
> / drain_local_obj_stock(), dereferences per-CPU stock caches with
> READ_ONCE(stock->cached[i]) and does css_put() / obj_cgroup_put().
>
> mem_cgroup_css_offline() calls drain_all_stock(memcg) to
> optimize reclamation latency, but never flushes memcg_wq. If
> that races with cgroup removal, free can happen while workers
> are still pending, causing UAF. The drain work could also have
> been queued by somebody else before offline started (e.g. high
> throttling), not just by the offline path itself.
>
> Timeline illustrating the race:
>
> ...
>
> Fix by having the offline path wait for the workqueue to be
> done with the memcg, before freeing the memcg.
>
> Found through a code audit with kres.
>
> Fixes: 591edfb10a94 ("mm: drain memcg stocks on css offlining")
> Cc: stable@xxxxxxxxxxxxxxx
> Assisted-by: Hermes:muse-spark-1.2 kres
Sashiko might have found another thing in there:
https://sashiko.dev/#/patchset/20260827124211.3b94b103@fangorn