* [PATCH] dm-pcache: fix use-after-free of cache segments on teardown
@ 2026-11-27 3:29 tongzhiwei
2026-11-30 13:56 ` Mikulas Patocka
0 siblings, 1 reply; 2+ messages in thread
From: tongzhiwei @ 2026-11-27 3:29 UTC (permalink / raw)
To: Dongsheng Yang, Zheng Gu
Cc: tongzhiwei, Mikulas Patocka, dm-devel, linux-kernel, stable
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
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] dm-pcache: fix use-after-free of cache segments on teardown
2026-11-27 3:29 [PATCH] dm-pcache: fix use-after-free of cache segments on teardown tongzhiwei
@ 2026-11-30 13:56 ` Mikulas Patocka
0 siblings, 0 replies; 2+ messages in thread
From: Mikulas Patocka @ 2026-11-30 13:56 UTC (permalink / raw)
To: tongzhiwei; +Cc: Dongsheng Yang, Zheng Gu, dm-devel, linux-kernel, stable
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
>
>
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-30 13:57 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27 3:29 [PATCH] dm-pcache: fix use-after-free of cache segments on teardown tongzhiwei
2026-11-30 13:56 ` Mikulas Patocka
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®