From: Mikulas Patocka <mpatocka@redhat.com>
To: tongzhiwei <tongzhiwei@cmss.chinamobile.com>
Cc: Dongsheng Yang <dongsheng.yang@linux.dev>,
Zheng Gu <cengku@gmail.com>,
dm-devel@lists.linux.dev, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: Re: [PATCH] dm-pcache: fix use-after-free of cache segments on teardown
Date: Mon, 30 Nov 2026 14:56:53 +0100 (CET) [thread overview]
Message-ID: <267d1742-767a-fa5c-f6db-d27b8547d486@redhat.com> (raw)
In-Reply-To: <20261127032934.484-1-tongzhiwei@cmss.chinamobile.com>
Hi
I accepted the patch, but it is quite a bad practice to have two static
functions cache_exit that do the same thing. So, I made the function
cache_exit non-static and deleted the second definition.
Mikulas
On Fri, 27 Nov 2026, tongzhiwei wrote:
> NOTE: this fix is found by LLM but reviewed by human.
>
> pcache_cache_stop() (called from dm_pcache_dtr() BEFORE
> backing_dev_stop()) freed cache->segments via cache_exit(). An
> inflight KMEM writeback completion can still be in flight then: its
> late callback path
>
> req_complete_fn -> backing_dev_req_end -> writeback_end_req ->
> writeback_ctx_end -> cache_pos_advance
>
> dereferences dirty_tail.cache_seg (== &cache->segments[id]), already
> freed -> use-after-free. backing_dev_stop() is the authoritative
> drain (wait_event(inflight_reqs == 0) + flush_work(req_complete_work))
> but ran after the free.
>
> Drop the cache_exit() call from pcache_cache_stop() and free
> segments/seg_map from dm_pcache_dtr() instead, after backing_dev_stop().
> The free is a local static cache_exit() in dm_pcache.c (not exported);
> cache.c's static cache_exit() is left untouched for the start-failure
> error path (pcache_cache_start's cache_exit: label, reached only when
> cache_init() succeeded).
>
> The other resources the late completion touches (cache_dev mapping,
> req/bvec pools, task_wq, backing dm_dev) are already released after
> the drain, so relocating segments/seg_map closes the only UAF window.
>
> Fixes: 1d57628ff95b ("dm-pcache: add persistent cache target in device-mapper")
> Cc: stable@vger.kernel.org
> Signed-off-by: tongzhiwei <tongzhiwei@cmss.chinamobile.com>
> ---
> drivers/md/dm-pcache/cache.c | 2 +-
> drivers/md/dm-pcache/dm_pcache.c | 7 +++++++
> 2 files changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/md/dm-pcache/cache.c b/drivers/md/dm-pcache/cache.c
> index b0b3e21677de..c41f7b7d1d04 100644
> --- a/drivers/md/dm-pcache/cache.c
> +++ b/drivers/md/dm-pcache/cache.c
> @@ -453,7 +453,7 @@ void pcache_cache_stop(struct dm_pcache *pcache)
> if (cache->req_key_tree.n_subtrees)
> cache_destroy_req_keys(cache);
>
> - cache_exit(cache);
> + /* cache_exit() is called from dm_pcache_dtr() after backing_dev_stop(). */
> }
>
> struct workqueue_struct *cache_get_wq(struct pcache_cache *cache)
> diff --git a/drivers/md/dm-pcache/dm_pcache.c b/drivers/md/dm-pcache/dm_pcache.c
> index 645fc27d82ba..b7abe585098e 100644
> --- a/drivers/md/dm-pcache/dm_pcache.c
> +++ b/drivers/md/dm-pcache/dm_pcache.c
> @@ -351,6 +351,12 @@ static void defer_req_stop(struct dm_pcache *pcache)
> }
> }
>
> +static void cache_exit(struct pcache_cache *cache)
> +{
> + kvfree(cache->seg_map);
> + kvfree(cache->segments);
> +}
> +
> static void dm_pcache_dtr(struct dm_target *ti)
> {
> struct dm_pcache *pcache;
> @@ -364,6 +370,7 @@ static void dm_pcache_dtr(struct dm_target *ti)
>
> pcache_cache_stop(pcache);
> backing_dev_stop(pcache);
> + cache_exit(&pcache->cache);
> cache_dev_stop(pcache);
>
> pcache_destroy_args(pcache);
> --
> 2.43.0
>
>
>
prev parent reply other threads:[~2026-11-30 13:57 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-27 3:29 tongzhiwei
2026-11-30 13:56 ` Mikulas Patocka [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=267d1742-767a-fa5c-f6db-d27b8547d486@redhat.com \
--to=mpatocka@redhat.com \
--cc=cengku@gmail.com \
--cc=dm-devel@lists.linux.dev \
--cc=dongsheng.yang@linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=stable@vger.kernel.org \
--cc=tongzhiwei@cmss.chinamobile.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®