[PATCH 6.19 202/220] mshv: Fix use-after-free in mshv_map_user_memory error path

Greg Kroah-Hartman
2026-03-23T13:59:52Z

From: Stanislav Kinsburskii <skinsburskii@linux.microsoft.com>

[ Upstream commit 6922db250422a0dfee34de322f86b7a73d713d33 ]

In the error path of mshv_map_user_memory(), calling vfree() directly on
the region leaves the MMU notifier registered. When userspace later unmaps
the memory, the notifier fires and accesses the freed region, causing a
use-after-free and potential kernel panic.

Replace vfree() with mshv_partition_put() to properly unregister
the MMU notifier before freeing the region.

Fixes: b9a66cd5ccbb9 ("mshv: Add support for movable memory regions")
Signed-off-by: Stanislav Kinsburskii <skinsburskii@linux.microsoft.com>
Signed-off-by: Wei Liu <wei.liu@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
drivers/hv/mshv_root_main.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/hv/mshv_root_main.c b/drivers/hv/mshv_root_main.c
index d3e8a66443ad6..45cf086ad430d 100644
--- a/drivers/hv/mshv_root_main.c
+++ b/drivers/hv/mshv_root_main.c
@@ -1334,7 +1334,7 @@ mshv_map_user_memory(struct mshv_partition *partition,
return 0;

errout:
- vfree(region);
+ mshv_region_put(region);
return ret;
}

--
2.51.0