From: sashiko-bot@kernel.org
To: "Wei Hu" <weh@linux.microsoft.com>
Cc: linux-hyperv@vger.kernel.org
Subject: Re: [PATCH v2 10/13] mshv: unmap SNP memory before state teardown
Date: Mon, 10 Aug 2026 13:13:19 +0000 [thread overview]
Message-ID: <20260810131320.E54A81F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260810124527.1409634-11-weh@linux.microsoft.com>
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
New issues:
- [Critical] The `destroy_work` delayed work is never explicitly canceled during module unload, leading to a potential use-after-free and kernel panic.
- [High] Retrying `destroy_partition` on failure causes partial unmap aborts, permanently leaving remaining chunks of a memory region mapped.
- [Low] Intentional early return in `mshv_region_destroy` permanently leaks the `mshv_mem_region` kernel tracking struct.
Pre-existing issues:
- [High] Direct kfree(vp) without waiting for RCU grace period allows Use-After-Free in concurrent ISRs.
--
--- Patch [10]: [PATCH v2 10/13] mshv: unmap SNP memory before state teardown ---
commit fe90624ce2dba03d1ad11c9e5fd88e8f71d8c3d0
Author: Wei Liu <wei.liu@kernel.org>
mshv: unmap SNP memory before state teardown
Unmap memory regions before destroying SNP partition state, and only
release regions after SNP state is torn down. Dropping regions first
attempts to regain host access while the partition is still secure and
can leave pages inaccessible.
> diff --git a/drivers/hv/mshv_regions.c b/drivers/hv/mshv_regions.c
> index 6d65e5b421529..62c49b4d2a33e 100644
> --- a/drivers/hv/mshv_regions.c
> +++ b/drivers/hv/mshv_regions.c
[ ... ]
> @@ -337,7 +337,7 @@ static int mshv_region_chunk_unmap(struct mshv_mem_region *region,
> page_count, flags);
> }
>
> -static int mshv_region_unmap(struct mshv_mem_region *region)
> +int mshv_region_unmap(struct mshv_mem_region *region)
> {
> return mshv_region_process_range(region, 0,
[Severity: Low]
Does this error path leak the kernel tracking structure?
When tearing down an SNP encrypted partition and mshv_region_share()
fails, the function returns early to intentionally leave guest memory
pinned and prevent host crashes:
drivers/hv/mshv_regions.c:mshv_region_destroy() {
...
if (ret) {
pt_err(partition,
"Failed to regain access to memory... %d\n", ret);
return;
}
}
mshv_region_unmap(region);
mshv_region_invalidate(region);
vfree(region);
}
Since the mshv_mem_region structure is already removed from the partition
lists and its refcount is zero, could bypassing the vfree(region) call
result in permanently leaking the mreg_pages array and tracking structure?
[ ... ]
> diff --git a/drivers/hv/mshv_root_main.c b/drivers/hv/mshv_root_main.c
> index c32d3375af8c5..0dbf509351bb0 100644
> --- a/drivers/hv/mshv_root_main.c
> +++ b/drivers/hv/mshv_root_main.c
> @@ -2208,10 +2208,8 @@ static void destroy_partition(struct mshv_partition *partition)
> if (mshv_partition_encrypted(partition)) {
> hlist_for_each_entry_safe(region, n,
> &partition->pt_mem_regions,
> - hnode) {
> - hlist_del(®ion->hnode);
> - mshv_region_put(region);
> - }
> + hnode)
> + mshv_region_unmap(region);
>
> ret = destroy_snp_partition_state(partition);
> if (ret) {
[Severity: High]
Can this retry logic lead to a partial unmap regression when a memory region
is processed a second time?
If destroy_snp_partition_state() fails, the teardown is retried via
destroy_work:
drivers/hv/mshv_root_main.c:destroy_partition() {
...
ret = destroy_snp_partition_state(partition);
if (ret) {
...
schedule_delayed_work(&partition->destroy_work, HZ);
return;
}
}
On the subsequent attempt, mshv_region_unmap() will iterate through the
region chunks again starting from offset 0. Since mreg_pages isn't
cleared during the initial successful unmap, the hypervisor will likely
return an error when attempting to unmap the first chunk again.
mshv_region_process_range() aborts processing immediately on error:
drivers/hv/mshv_regions.c:mshv_region_process_range() {
...
ret = mshv_region_process_chunk(region, flags, page_offset, ...);
if (ret < 0)
return ret;
}
Will this completely bypass the unmapping of any remaining chunks that
were not reached in the previous pass?
[Severity: High]
Is it safe to schedule a delayed work task that lacks cancelation during module
unload?
If destroy_snp_partition_state() fails, the teardown is retried via
delayed work. If the partition file descriptor is closed, the module
reference count drops to 0, which allows the administrator to unload the
mshv module.
Looking at the exit path:
drivers/hv/mshv_root_main.c:mshv_parent_partition_exit() {
...
mshv_irqfd_wq_cleanup();
root_scheduler_deinit();
mshv_synic_exit();
}
Since mshv_parent_partition_exit() executes without calling
cancel_delayed_work_sync(&partition->destroy_work), can the timer fire
after the kernel module is unloaded and execute freed memory?
[Severity: High]
This is a pre-existing issue, but would this direct free cause a
use-after-free regression in concurrent interrupt service routines?
In destroy_partition(), vp structures are freed directly:
drivers/hv/mshv_root_main.c:destroy_partition() {
...
kfree(vp);
partition->pt_vp_array[i] = NULL;
...
}
Concurrently, the intercept ISR reads these pointers under an RCU lock:
drivers/hv/mshv_synic.c:mshv_intercept_isr() {
...
vp = partition->pt_vp_array[vp_index];
if (unlikely(!vp)) {
...
}
kick_vp(vp);
...
}
Since drain_all_vps() is skipped for non-root schedulers, intercept
messages can still arrive during partition teardown. If destroy_partition()
executes between the time mshv_intercept_isr() reads the vp pointer and
dereferences it in kick_vp(), could this result in accessing freed memory
because synchronize_rcu() or kfree_rcu() isn't used?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260810124527.1409634-1-weh@linux.microsoft.com?part=10
next prev parent reply other threads:[~2026-08-10 13:13 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-10 12:44 [PATCH v2 00/13] mshv: add SEV-SNP support for MSHV root partitions Wei Hu
2026-08-10 12:44 ` [PATCH v2 01/13] mshv: add SEV-SNP UAPI definitions Wei Hu
2026-08-10 12:59 ` sashiko-bot
2026-08-10 12:44 ` [PATCH v2 02/13] mshv: add SEV-SNP PSP request hypercall Wei Hu
2026-08-10 12:44 ` [PATCH v2 03/13] mshv: add SEV-SNP isolated page hypercalls Wei Hu
2026-08-10 12:58 ` sashiko-bot
2026-08-10 12:44 ` [PATCH v2 04/13] mshv: wire SEV-SNP partition ioctls Wei Hu
2026-08-10 13:07 ` sashiko-bot
2026-08-10 12:44 ` [PATCH v2 05/13] hyperv: fix hv_input_get_system_property layout for SNP status Wei Hu
2026-08-10 18:59 ` Wei Liu
2026-08-10 12:45 ` [PATCH v2 06/13] mshv: detect and report SEV-SNP support at init Wei Hu
2026-08-10 12:55 ` sashiko-bot
2026-08-10 18:53 ` Wei Liu
2026-08-10 12:45 ` [PATCH v2 07/13] mshv: default to safe partition CPU features Wei Hu
2026-08-10 12:57 ` sashiko-bot
2026-08-10 12:45 ` [PATCH v2 08/13] mshv: accept partial CPU feature banks Wei Hu
2026-08-10 12:45 ` [PATCH v2 09/13] mshv: define full processor and xsave feature masks Wei Hu
2026-08-10 12:45 ` [PATCH v2 10/13] mshv: unmap SNP memory before state teardown Wei Hu
2026-08-10 13:13 ` sashiko-bot [this message]
2026-08-10 12:45 ` [PATCH v2 11/13] mshv: unlock SNP pages on panic for crashdump collection Wei Hu
2026-08-10 13:10 ` sashiko-bot
2026-08-10 12:45 ` [PATCH v2 12/13] hyperv: add MSHV Dom0 root-partition boot enablement (EFI HvLoader) Wei Hu
2026-08-10 13:05 ` sashiko-bot
2026-08-10 18:50 ` Wei Liu
2026-08-10 12:45 ` [PATCH v2 13/13] mshv: set up own SynIC registers on a nested root partition Wei Hu
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=20260810131320.E54A81F000E9@smtp.kernel.org \
--to=sashiko-bot@kernel.org \
--cc=linux-hyperv@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
--cc=weh@linux.microsoft.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.