kvm: sev: Fix issues reported by Sashiko
Subsystems (5)
cover kvm: sev: Fix issues reported by Sashiko
From: Joerg Roedel <[email protected]>
Hi,
On the post of my direct-VMSA patch-set Sashiko reported a few real pre-existing issues in the SEV-SNP launch_update code. This patch-set addresses three of them:
* Fix user-triggerable WARN_ON on LAUNCH_UPDATE path.
* Check that CPUID pages are writable before writing error information to it.
* Fix kunmap_local() order.
Please review.
-Joerg
Joerg Roedel (4):
kvm: sev: Fix user-space triggerable WARN_ON on snp_launch_update path
kvm: sev: Unmap pages in correct order in sev_gmem_post_populate()
KVM: guest_memfd: Add write parameter to kvm_gmem_populate()
kvm: sev: Acquire a writeable page reference for CPUID pages
arch/x86/kvm/svm/sev.c | 15 +++++++++++++-- arch/x86/kvm/vmx/tdx.c | 2 +- include/linux/kvm_host.h | 4 +++- virt/kvm/guest_memfd.c | 4 ++-- 4 files changed, 19 insertions(+), 6 deletions(-)
Signature · 1 line
2.53.0
PATCH 1/4 kvm: sev: Fix user-space triggerable WARN_ON on snp_launch_update path
From: Joerg Roedel <[email protected]>
Sashiko reported on an unrelated patch:
[Severity: High]
This is a pre-existing issue, but can a host userspace process trigger a kernel warning by passing a NULL user address (uaddr = 0) here?
If params.uaddr is 0, src becomes NULL and passes the PAGE_ALIGNED(src) check. kvm_gmem_populate() skips fetching the user page and passes src_page = NULL to sev_gmem_post_populate().
That function then unconditionally evaluates:
WARN_ON_ONCE(sev_populate_args->type != KVM_SEV_SNP_PAGE_TYPE_ZERO &&
!src_page)
Since the type isn't ZERO, won't this allow an unprivileged user to spam the kernel log?
The assessment is correct, so check for this condition earlier in the snp_launch_update() path to avoid the WARN_ON_ONCE.
--- arch/x86/kvm/svm/sev.c | 7 +++++++ 1 file changed, 7 insertions(+)
arch/x86/kvm/svm/sev.c +7 -0
@@ -2438,6 +2438,13 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp)if (!PAGE_ALIGNED (src))return -EINVAL ; +/* +* Make sure user-mode did not pass NULL as src with +* type != KVM_SEV_SNP_PAGE_TYPE_ZERO. +*/ +if (src ==NULL && params.type !=KVM_SEV_SNP_PAGE_TYPE_ZERO ) +return -EINVAL ; + npages = params.len /PAGE_SIZE ;/*
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH 1/4] kvm: sev: Fix user-space triggerable WARN_ON on snp_launch_update path
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] snp_launch_update unconditionally returns -EIO when kvm_gmem_populate fails, discarding critical error codes like -EINTR, -EFAULT, and -ENOMEM.
- [High] sev_gmem_post_populate unmaps kmap_local pointers in the wrong order in the CPUID firmware error handling block.
--
commit 9048d7bc73d4ef4c2ccbf4550c4b2feff1f1822d
Author: Joerg Roedel <[email protected]>
kvm: sev: Fix user-space triggerable WARN_ON on snp_launch_update path
This commit avoids a WARN_ON_ONCE in the snp_launch_update() path by explicitly checking if a user-space process passed a NULL source address when the page type is not KVM_SEV_SNP_PAGE_TYPE_ZERO.
Quoted 6 lines
arch/x86/kvm/svm/sev.c +0
-0@@ -2438,6 +2438,13 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp)if (!PAGE_ALIGNED (src))
arch/x86/kvm/svm/sev.c static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2448
Quoted 10 lines
arch/x86/kvm/svm/sev.c +7
-0return -EINVAL ; +/* +* Make sure user-mode did not pass NULL as src with +* type != KVM_SEV_SNP_PAGE_TYPE_ZERO. +*/ +if (src ==NULL && params.type !=KVM_SEV_SNP_PAGE_TYPE_ZERO ) +return -EINVAL ; + npages = params.len /PAGE_SIZE ;
[Severity: High]
This is a pre-existing issue, but does snp_launch_update() unconditionally return -EIO when kvm_gmem_populate() fails?
arch/x86/kvm/svm/sev.c:snp_launch_update() {
...
count = kvm_gmem_populate(kvm, params.gfn_start, src, npages, write,
sev_gmem_post_populate, &sev_populate_args);
if (count < 0) {
argp->error = sev_populate_args.fw_error;
pr_debug("%s: kvm_gmem_populate failed, ret %ld (fw_error %d)\n",
__func__, count, argp->error);
return -EIO;
}...
}
Returning -EIO unconditionally here discards important error codes like -EINTR, -EFAULT, and -ENOMEM from kvm_gmem_populate().
A userspace process could receive a signal (e.g. SIGALRM) during the ioctl execution, causing kvm_gmem_populate() to return -EINTR, which gets overwritten with -EIO.
This breaks standard Linux signal restart semantics and could cause VM creation to fail under signal pressure.
[Severity: High]
This is also a pre-existing issue, but are the kmap_local pointers unmapped in the wrong order in sev_gmem_post_populate()?
arch/x86/kvm/svm/sev.c:sev_gmem_post_populate() {
...
if (ret && !snp_page_reclaim(kvm, pfn) &&
sev_populate_args->type == KVM_SEV_SNP_PAGE_TYPE_CPUID &&
sev_populate_args->fw_error == SEV_RET_INVALID_PARAM) {
void *src_vaddr = kmap_local_page(src_page);
void *dst_vaddr = kmap_local_pfn(pfn);
memcpy(src_vaddr, dst_vaddr, PAGE_SIZE);
kunmap_local(src_vaddr);
kunmap_local(dst_vaddr);
}...
}
kunmap_local() requires pointers to be unmapped in strictly reverse LIFO order.
Here, src_vaddr is mapped first, then dst_vaddr, but src_vaddr is also unmapped first.
This violates the API requirement and can trigger kernel panics via BUG_ON() when CONFIG_DEBUG_KMAP_LOCAL is enabled.
While an earlier patch in this series ('kvm: sev: Unmap pages in correct order in sev_gmem_post_populate()') fixed this in the first error block of the function, it looks like this second error block was missed.
This can be triggered by a userspace VMM passing invalid CPUID data to KVM_SEV_SNP_LAUNCH_UPDATE, causing firmware to reject the page.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Sean Christopherson
maintainerRe: [PATCH 1/4] kvm: sev: Fix user-space triggerable WARN_ON on snp_launch_update path
In reply to Jörg RödelPlease capitalize the scope, i.e. "KVM: SEV:".
Quoted 12 lines · Jörg Rödel
From: Joerg Roedel <[email protected]>
Sashiko reported on an unrelated patch:
[Severity: High]
This is a pre-existing issue, but can a host userspace process trigger a kernel warning by passing a NULL user address (uaddr = 0) here?If params.uaddr is 0, src becomes NULL and passes the
PAGE_ALIGNED(src)check.kvm_gmem_populate()skips fetching the user page and passessrc_page= NULL tosev_gmem_post_populate().
Quoted 7 lines
That function then unconditionally evaluates:
WARN_ON_ONCE(sev_populate_args->type !=
KVM_SEV_SNP_PAGE_TYPE_ZERO&&!src_page)Since the type isn't ZERO, won't this allow an unprivileged user to spam the kernel log?
Use Reported-by: + Closes to capture Sashiko's effecitve bug report instead of copy+pasting the finding. There's no reason to treat Sashiko any differently than any other bot.
Quoted 14 lines
The assessment is correct, so check for this condition earlier in the
snp_launch_update()path to avoid theWARN_ON_ONCE.Fixes dee5a47cc7a45 ("KVM: SEV: Add KVM_SEV_SNP_LAUNCH_UPDATE command")Signed-off-by Joerg Roedel <[email protected]>--- arch/x86/kvm/svm/sev.c | 7 +++++++ 1 file changed, 7 insertions(+)arch/x86/kvm/svm/sev.c +0
-0
arch/x86/kvm/svm/sev.c static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2443
Quoted 6 lines
arch/x86/kvm/svm/sev.c +3
-0@@ -2438,6 +2438,13 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp)if (!PAGE_ALIGNED (src))return -EINVAL ; +/* +* Make sure user-mode did not pass NULL as src with +* type != KVM_SEV_SNP_PAGE_TYPE_ZERO.
Meh, that's pretty obvious from the code.
arch/x86/kvm/svm/sev.c static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2445
Quoted 2 lines
arch/x86/kvm/svm/sev.c +2
-0+*/ +if (src ==NULL && params.type !=KVM_SEV_SNP_PAGE_TYPE_ZERO )
I think I'd prefer this over checking for KVM_SEV_SNP_PAGE_TYPE_ZERO twice, especially since the PAGE_ALIGNED() check for the NULL pointer case is rather weird.
if (params.type == KVM_SEV_SNP_PAGE_TYPE_ZERO) src = NULL; else if (!params.uaddr || !PAGE_ALIGNED(params.uaddr)) return -EINVAL; else src = u64_to_user_ptr(params.uaddr);
arch/x86/kvm/svm/sev.c static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2446
Quoted 1 line
arch/x86/kvm/svm/sev.c +1
-0+return -EINVAL ;
Gah, we created quite the mess for ourselves. TDX returns -EOPNOTSUPP instead of -EINVAL, I guess as a placeholder for in-place conversion? I don't care which error code is returned, but I do want KVM to be consistent.
We should also adjust TDX to pre-check the source address, because checking only in the post-populate flow subtly relies on tdx_vcpu_init_mem_region() returning immediately on error. If that weren't the case (ignoring for the moment that continuing on would be nonsensical), KVM would advace the address by PAGE_SIZE and suddenly a NULL userspace address becomes non-NULL.
I also think it makes sense to drop the WARN in sev_gmem_post_populate(), it's completely redundant once this bug is fixed.
Ugh, and both SNP and TDX fail to account for tags, and fail to check for striding into kernel space. Which I suppose is fine, since gup() handles those correctly. And I don't see a strong argument for disallowing tagged addresses, because unlike the userspace address for memslots, KVM doesn't keep the address around long-term.
So over two patches, the below? I can send a v2, I've already got changelogs written (I was fiddling around with extracting and reusing kvm_set_memory_region()'s checks on the userspace address+size, but as above, convinced myself that KVM should continue to allow tagged addresses for SNP and TDX).
--- arch/x86/kvm/svm/sev.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
arch/x86/kvm/svm/sev.c +5 -6
@@ -2330,9 +2330,6 @@ static int sev_gmem_post_populate(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn,int level;int ret; -if (WARN_ON_ONCE (sev_populate_args->type !=KVM_SEV_SNP_PAGE_TYPE_ZERO && !src_page)) -return -EINVAL ; - ret =snp_lookup_rmpentry ((u64 )pfn, &assigned, &level);if (ret || assigned) { pr_debug("%s: Failed to ensure GFN 0x%llx RMP entry is initial shared state, ret: %d assigned: %d\n",
@@ -2421,10 +2418,12 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) params.type != KVM_SEV_SNP_PAGE_TYPE_CPUID))return -EINVAL ; - src = params.type ==KVM_SEV_SNP_PAGE_TYPE_ZERO ?NULL :u64_to_user_ptr (params.uaddr); - -if (!PAGE_ALIGNED (src)) +if (params.type ==KVM_SEV_SNP_PAGE_TYPE_ZERO ) + src =NULL ; +else if (!params.uaddr || !PAGE_ALIGNED (params.uaddr))return -EINVAL ; +else + src =u64_to_user_ptr (params.uaddr); npages = params.len /PAGE_SIZE ;
--- arch/x86/kvm/vmx/tdx.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx/tdx.c +2 -5
@@ -3198,9 +3198,6 @@ static int tdx_gmem_post_populate(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn,if (KVM_BUG_ON (kvm_tdx->page_add_src, kvm))return -EIO ; -if (!src_page) -return -EOPNOTSUPP ; - kvm_tdx->page_add_src = src_page; ret =kvm_tdp_mmu_map_private_pfn (arg->vcpu, gfn, pfn); kvm_tdx->page_add_src =NULL ;
@@ -3247,8 +3244,8 @@ static int tdx_vcpu_init_mem_region(struct kvm_vcpu *vcpu, struct kvm_tdx_cmd *cif (copy_from_user (®ion,u64_to_user_ptr (cmd->data),sizeof (region)))return -EFAULT ; -if (!PAGE_ALIGNED (region.source_addr) || !PAGE_ALIGNED (region.gpa) || - !region.nr_pages || +if (!PAGE_ALIGNED (region.source_addr) || !region.source_addr || + !PAGE_ALIGNED (region.gpa) || !region.nr_pages || region.gpa + (region.nr_pages <<PAGE_SHIFT ) <= region.gpa || !vt_is_tdx_private_gpa (kvm, region.gpa) || !vt_is_tdx_private_gpa (kvm, region.gpa + (region.nr_pages <<PAGE_SHIFT ) -1 ))
Re: [PATCH 1/4] kvm: sev: Fix user-space triggerable WARN_ON on snp_launch_update path
In reply to Jörg RödelQuoted 12 lines · Jörg Rödel
From: Joerg Roedel <[email protected]>
Sashiko reported on an unrelated patch:
[Severity: High]
This is a pre-existing issue, but can a host userspace process trigger a kernel warning by passing a NULL user address (uaddr = 0) here?If params.uaddr is 0, src becomes NULL and passes the
PAGE_ALIGNED(src)check.kvm_gmem_populate()skips fetching the user page and passessrc_page= NULL tosev_gmem_post_populate().
Quoted 7 lines
That function then unconditionally evaluates:
WARN_ON_ONCE(sev_populate_args->type !=
KVM_SEV_SNP_PAGE_TYPE_ZERO&&!src_page)Since the type isn't ZERO, won't this allow an unprivileged user to spam the kernel log?
It would only be one warning that is emitted, "spam the kernel log" sounds like you could fill it with warnings. And I would say that the severity is only "High" should the kernel be configured with PANIC_ON_WARN.
Quoted 3 lines
The assessment is correct, so check for this condition earlier in the
snp_launch_update()path to avoid theWARN_ON_ONCE.
I'm not positive, but isn't it technically possible that the userspace virtual address can be 0? In which case, should this be fixed in the kvm_gmem_populate() API with maybe a new parameter that indicates whether src is valid or not?
Thanks,
Tom
Quoted 25 lines
Fixes dee5a47cc7a45 ("KVM: SEV: Add KVM_SEV_SNP_LAUNCH_UPDATE command")Signed-off-by Joerg Roedel <[email protected]>--- arch/x86/kvm/svm/sev.c | 7 +++++++ 1 file changed, 7 insertions(+)arch/x86/kvm/svm/sev.c +7
-0@@ -2438,6 +2438,13 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp)if (!PAGE_ALIGNED (src))return -EINVAL ; +/* +* Make sure user-mode did not pass NULL as src with +* type != KVM_SEV_SNP_PAGE_TYPE_ZERO. +*/ +if (src ==NULL && params.type !=KVM_SEV_SNP_PAGE_TYPE_ZERO ) +return -EINVAL ; + npages = params.len /PAGE_SIZE ;/*
Sean Christopherson
maintainerRe: [PATCH 1/4] kvm: sev: Fix user-space triggerable WARN_ON on snp_launch_update path
Quoted 18 lines · Tom Lendacky
Quoted 17 lines · Jörg Rödel
From: Joerg Roedel <[email protected]>
Sashiko reported on an unrelated patch:
[Severity: High]
This is a pre-existing issue, but can a host userspace process trigger a kernel warning by passing a NULL user address (uaddr = 0) here?If params.uaddr is 0, src becomes NULL and passes the
PAGE_ALIGNED(src)check.kvm_gmem_populate()skips fetching the user page and passessrc_page= NULL tosev_gmem_post_populate().That function then unconditionally evaluates:
WARN_ON_ONCE(sev_populate_args->type !=
KVM_SEV_SNP_PAGE_TYPE_ZERO&&!src_page)
Quoted 6 lines
Quoted 2 lines
Since the type isn't ZERO, won't this allow an unprivileged user to spam the kernel log?
It would only be one warning that is emitted, "spam the kernel log" sounds like you could fill it with warnings. And I would say that the severity is only "High" should the kernel be configured with
PANIC_ON_WARN.
Yeah, ignore any and all complaints about panic_on_warn=1 leading to DoS.
https://lore.kernel.org/all/CABgObfZJV5hU_7WoPWLRH3-EvKts%[email protected]
Quoted 5 lines
Quoted 2 lines
The assessment is correct, so check for this condition earlier in the
snp_launch_update()path to avoid theWARN_ON_ONCE.I'm not positive, but isn't it technically possible that the userspace virtual address can be 0?
Yep, though it requires an explicit opt-in.
config DEFAULT_MMAP_MIN_ADDR
int "Low address space to protect from user allocation" depends on MMU
default 4096
help
This is the portion of low virtual memory which should be protected from userspace allocation. Keeping a user from writing to low pages can help reduce the impact of kernel NULL pointer bugs.
For most ppc64 and x86 users with lots of address space a value of 65536 is reasonable and should cause no problems. On arm and other archs it should not be higher than 32768. Programs which use vm86 functionality or have some need to map this low address space will need CAP_SYS_RAWIO or disable this protection by setting the value to 0.
This value can be changed after boot using the /proc/sys/vm/mmap_min_addr tunable.
Quoted 2 lines
In which case, should this be fixed in the
kvm_gmem_populate()API with maybe a new parameter that indicates whether src is valid or not?
Nah, treating 0/NULL as invalid is perfectly acceptable. It doesn't work today, i.e. there's no risk of breaking anyone, and just because userspace can use VA=0 for other things doesn't mean KVM needs to support that for all of its uAPI surface.
FWIW, I was initially opposed to using 0/NULL to mean "invalid", and even called it ridiculous, but David W. convinced me that accepting 0/NULL in modern KVM uAPI would violate the principle of least surprise. So now I get to claim using NULL to mean invalid as my own original idea ;-)
PATCH 2/4 kvm: sev: Unmap pages in correct order in sev_gmem_post_populate()
In reply to Jörg RödelFrom: Joerg Roedel <[email protected]>
The kmap_local() interface requires unmapping of pages in reverse order of mapping.
--- arch/x86/kvm/svm/sev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/svm/sev.c +1 -1
@@ -2360,8 +2360,8 @@ static int sev_gmem_post_populate(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn,memcpy (dst_vaddr, src_vaddr,PAGE_SIZE ); -kunmap_local (src_vaddr);kunmap_local (dst_vaddr); +kunmap_local (src_vaddr); } ret = rmp_make_private(pfn, gfn << PAGE_SHIFT, PG_LEVEL_4K,
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH 2/4] kvm: sev: Unmap pages in correct order in sev_gmem_post_populate()
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] The patch corrects the kunmap_local() LIFO unmapping order in the initial setup path but leaves an identical unmapping violation in the CPUID firmware error-handling path of sev_gmem_post_populate(). --
commit 6fe2af9a226230e3ae153d850afdecbdb7d9f3cc
Author: Joerg Roedel <[email protected]>
kvm: sev: Unmap pages in correct order in sev_gmem_post_populate()
This commit corrects the unmapping order of kmap_local() pages in the initial setup path of sev_gmem_post_populate(), ensuring strict LIFO ordering.
Quoted 5 lines
arch/x86/kvm/svm/sev.c +0
-0
arch/x86/kvm/svm/sev.c static int sev_gmem_post_populate(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, · line 2367
Quoted 9 lines
arch/x86/kvm/svm/sev.c +1
-1@@ -2360,8 +2360,8 @@ static int sev_gmem_post_populate(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn,memcpy (dst_vaddr, src_vaddr,PAGE_SIZE ); -kunmap_local (src_vaddr);kunmap_local (dst_vaddr); +kunmap_local (src_vaddr); } ret = rmp_make_private(pfn, gfn << PAGE_SHIFT, PG_LEVEL_4K,
[Severity: High]
This is a pre-existing issue, but while this hunk fixes the unmapping order in the initial setup path, does the identical LIFO unmapping violation still exist further down in the CPUID firmware error-handling path of sev_gmem_post_populate()?
If an untrusted userspace process supplies invalid CPUID data during KVM_SEV_SNP_LAUNCH_UPDATE, the firmware returns SEV_RET_INVALID_PARAM.
This directly reaches the error path where src_vaddr and dst_vaddr are mapped, but unmapped out of order:
arch/x86/kvm/svm/sev.c:sev_gmem_post_populate() {
...
if (ret && !snp_page_reclaim(kvm, pfn) &&
sev_populate_args->type == KVM_SEV_SNP_PAGE_TYPE_CPUID &&
sev_populate_args->fw_error == SEV_RET_INVALID_PARAM) {
void *src_vaddr = kmap_local_page(src_page);
void *dst_vaddr = kmap_local_pfn(pfn);
memcpy(src_vaddr, dst_vaddr, PAGE_SIZE);
kunmap_local(src_vaddr);
kunmap_local(dst_vaddr);
}
...
}Should this second instance be updated to reverse the unmap order as well to prevent kmap stack corruption?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH 2/4] kvm: sev: Unmap pages in correct order in sev_gmem_post_populate()
Quoted 2 lines · On Tue, Jun 23, 2026 at 09:24:16AM +0000, [email protected] wrote:
Should this second instance be updated to reverse the unmap order as well to prevent kmap stack corruption?
Yes, it should. Updated for v2.
Sean Christopherson
maintainerRe: [PATCH 2/4] kvm: sev: Unmap pages in correct order in sev_gmem_post_populate()
Quoted 5 lines · Jörg Rödel
Quoted 2 lines · On Tue, Jun 23, 2026 at 09:24:16AM +0000, [email protected] wrote:
Should this second instance be updated to reverse the unmap order as well to prevent kmap stack corruption?
Yes, it should. Updated for v2.
Already fixed, commit 138f5f9cbe37 ("KVM: SEV: Unmap local kmaps in LIFO order, per highmem requirements").
PATCH 3/4 KVM: guest_memfd: Add `write` parameter to kvm_gmem_populate()
In reply to Jörg RödelFrom: Joerg Roedel <[email protected]>
The call-path of kvm_gmem_populate() might subsequently write to the page provided by user-space. This is used to provide detailed error information in case the page population failed.
But since kvm_gmem_populate() only acquires a read-only reference to the user-space page via get_user_pages_fast(), the error information might be written to a read-only page later on.
Add a parameter to kvm_gmem_populate() to optionally acquire a writeable reference to the source page to make sure page permissions can be enforced.
--- arch/x86/kvm/svm/sev.c | 2 +- arch/x86/kvm/vmx/tdx.c | 2 +- include/linux/kvm_host.h | 4 +++- virt/kvm/guest_memfd.c | 4 ++-- 4 files changed, 7 insertions(+), 5 deletions(-)
arch/x86/kvm/svm/sev.c +1 -1
@@ -2475,7 +2475,7 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) sev_populate_args.sev_fd = argp->sev_fd; sev_populate_args.type = params.type; - count =kvm_gmem_populate (kvm, params.gfn_start, src, npages, + count =kvm_gmem_populate (kvm, params.gfn_start, src, npages,0 , sev_gmem_post_populate, &sev_populate_args);if (count <0 ) { argp->error = sev_populate_args.fw_error;
arch/x86/kvm/vmx/tdx.c +1 -1
@@ -3185,7 +3185,7 @@ static int tdx_vcpu_init_mem_region(struct kvm_vcpu *vcpu, struct kvm_tdx_cmd *c }; gmem_ret =kvm_gmem_populate (kvm,gpa_to_gfn (region.gpa),u64_to_user_ptr (region.source_addr), -1 , tdx_gmem_post_populate, &arg); +1 ,0 , tdx_gmem_post_populate, &arg);if (gmem_ret <0 ) { ret = gmem_ret;break ;
include/linux/kvm_host.h +3 -1
@@ -2581,6 +2581,8 @@ int kvm_arch_gmem_prepare(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, int max_ord* (passed to @post_populate, and incremented on each iteration * if not NULL). Must be page-aligned. * @npages: number of pages to copy from userspace-buffer +* @write: user-space provided buffer must be writable. The function +* will acquire a writable reference when set to 1. * @post_populate: callback to issue for each gmem page that backs the GPA * range * @opaque: opaque data to pass to @post_populate callback
@@ -2597,7 +2599,7 @@ typedef int (*kvm_gmem_populate_cb)(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, struct page *page, void *opaque);long kvm_gmem_populate (struct kvm *kvm,gfn_t gfn,void __user *src,long npages, -kvm_gmem_populate_cb post_populate,void *opaque); +int write,kvm_gmem_populate_cb post_populate,void *opaque);#endif #ifdef CONFIG_HAVE_KVM_ARCH_GMEM_INVALIDATE
virt/kvm/guest_memfd.c +2 -2
@@ -859,7 +859,7 @@ static long __kvm_gmem_populate(struct kvm *kvm, struct kvm_memory_slot *slot, }long kvm_gmem_populate (struct kvm *kvm,gfn_t start_gfn,void __user *src,long npages, -kvm_gmem_populate_cb post_populate,void *opaque) +int write,kvm_gmem_populate_cb post_populate,void *opaque) {struct kvm_memory_slot *slot;int ret =0 ;
@@ -893,7 +893,7 @@ long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src, longif (src) {unsigned long uaddr = (unsigned long )src + i *PAGE_SIZE ; - ret =get_user_pages_fast (uaddr,1 ,0 , &src_page); + ret =get_user_pages_fast (uaddr,1 , write, &src_page);if (ret <0 )break ;if (ret !=1 ) {
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH 3/4] KVM: guest_memfd: Add `write` parameter to kvm_gmem_populate()
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [Low] The integer write parameter is passed directly as the gup_flags bitmask to get_user_pages_fast(), treating a bitmask as a boolean.
Pre-existing issues:
- [Medium] Modified user pages acquired via get_user_pages_fast() are not marked dirty before their reference is dropped, leading to potential data loss. --
commit 49441863078cd5114f78eb8c75f1d31489417864
Author: Joerg Roedel <[email protected]>
KVM: guest_memfd: Add write parameter to kvm_gmem_populate()
This commit adds a write parameter to kvm_gmem_populate() to optionally acquire a writable reference to the user-space page via get_user_pages_fast(). This ensures page permissions are enforced when KVM needs to write error details back to userspace.
Quoted 4 lines
virt/kvm/guest_memfd.c +0
-0
[ ... ]
virt/kvm/guest_memfd.c long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src, long · line 896
Quoted 6 lines
virt/kvm/guest_memfd.c +1
-1@@ -893,7 +893,7 @@ long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src, longif (src) {unsigned long uaddr = (unsigned long )src + i *PAGE_SIZE ; - ret =get_user_pages_fast (uaddr,1 ,0 , &src_page); + ret =get_user_pages_fast (uaddr,1 , write, &src_page);
[Severity: Low]
Does this pass an integer parameter directly into the gup_flags argument? While the integer value 1 coincidentally evaluates to FOLL_WRITE (1 << 0), is it safe to rely on the numeric value of an internal macro definition here? Would it be more robust to explicitly pass FOLL_WRITE based on the write parameter?
[Severity: Medium]
This is a pre-existing issue, but since we are explicitly handling writable pages here, are we missing a dirty page notification before the reference is dropped?
If a user-provided CPUID page fails validation during a SEV-SNP VM launch, sev_gmem_post_populate() modifies the page directly via memcpy() bypassing the user page tables. However, kvm_gmem_populate() drops the reference shortly after:
virt/kvm/guest_memfd.c:kvm_gmem_populate() {
...
ret = __kvm_gmem_populate(...);
if (src_page)
put_page(src_page);
...
}Does this drop the reference without calling set_page_dirty_lock() or an equivalent dirtying function? If so, could the memory management subsystem reclaim the unmarked page and cause userspace to read stale data instead of the detailed error information?
Quoted 3 lines
virt/kvm/guest_memfd.c +0
-0if (ret <0 )break ;if (ret !=1 ) {
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Sean Christopherson
maintainerRe: [PATCH 3/4] KVM: guest_memfd: Add `write` parameter to kvm_gmem_populate()
In reply to Jörg RödelQuoted 6 lines · Jörg Rödel
From: Joerg Roedel <[email protected]>
The call-path of
kvm_gmem_populate()might subsequently write to the page provided by user-space. This is used to provide detailed error information in case the page population failed.
Quoted 7 lines
But since
kvm_gmem_populate()only acquires a read-only reference to the user-space page viaget_user_pages_fast(), the error information might be written to a read-only page later on.Add a parameter to
kvm_gmem_populate()to optionally acquire a writeable reference to the source page to make sure page permissions can be enforced.
Already fixed, commit f13e90059908 ("KVM: SEV: Pin source page for write when adding CPUID data for SNP guest").
PATCH 4/4 kvm: sev: Acquire a writeable page reference for CPUID pages
In reply to Jörg RödelFrom: Joerg Roedel <[email protected]>
When the PSP checks on a user-provided CPUID page fail KVM will write back the detailed error information to the user-provided buffer.
Make sure this buffer is actually writable to not write the errors to a read-only page.
--- arch/x86/kvm/svm/sev.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/sev.c +5 -1
@@ -2415,6 +2415,7 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp)struct kvm_memory_slot *memslot;long npages, count;void __user *src; +int write;if (!sev_snp_guest (kvm) || !sev->snp_context)return -EINVAL ;
@@ -2475,7 +2476,10 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) sev_populate_args.sev_fd = argp->sev_fd; sev_populate_args.type = params.type; - count =kvm_gmem_populate (kvm, params.gfn_start, src, npages,0 , +/* Acquire a write-reference for CPUID pages as kernel might write to it */ + write = params.type ==KVM_SEV_SNP_PAGE_TYPE_CPUID ; + + count =kvm_gmem_populate (kvm, params.gfn_start, src, npages, write, sev_gmem_post_populate, &sev_populate_args);if (count <0 ) { argp->error = sev_populate_args.fw_error;
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH 4/4] kvm: sev: Acquire a writeable page reference for CPUID pages
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Pre-existing issues:
- [Critical] Incorrect unmap order for kmap_local mappings in the CPUID error reporting path leads to host kernel crashes.
- [High] Missing dirty tracking for user pages modified by the kernel, leading to potential silent data loss.
- [High] Data race and Use-After-Free on kvm_memory_slot between memslot teardown and kvm_gmem_error_folio. --
commit 53ff0fbc410035f0e257e032a520eacbc8269a5c
Author: Joerg Roedel <[email protected]>
kvm: sev: Acquire a writeable page reference for CPUID pages
When the PSP checks on a user-provided CPUID page fail KVM will write back the detailed error information to the user-provided buffer.
Make sure this buffer is actually writable to not write the errors to a read-only page.
Quoted 11 lines
arch/x86/kvm/svm/sev.c +1
-0@@ -2415,6 +2415,7 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp)struct kvm_memory_slot *memslot;long npages, count;void __user *src; +int write;if (!sev_snp_guest (kvm) || !sev->snp_context)return -EINVAL ;
[ ... ]
arch/x86/kvm/svm/sev.c static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2483
Quoted 10 lines
arch/x86/kvm/svm/sev.c +4
-1@@ -2475,7 +2476,10 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) sev_populate_args.sev_fd = argp->sev_fd; sev_populate_args.type = params.type; - count =kvm_gmem_populate (kvm, params.gfn_start, src, npages,0 , +/* Acquire a write-reference for CPUID pages as kernel might write to it */ + write = params.type ==KVM_SEV_SNP_PAGE_TYPE_CPUID ; + + count =kvm_gmem_populate (kvm, params.gfn_start, src, npages, write, sev_gmem_post_populate, &sev_populate_args);
[Severity: High]
This isn't a bug introduced by this patch, but does kvm_gmem_populate() handle dirtying the page correctly after sev_gmem_post_populate() modifies it?
When a user-provided CPUID page triggers SEV_RET_INVALID_PARAM, the error path modifies the page directly via memcpy():
arch/x86/kvm/svm/sev.c:sev_gmem_post_populate() {
...
memcpy(src_vaddr, dst_vaddr, PAGE_SIZE);
...
}Since this bypasses hardware PTE dirty tracking, and kvm_gmem_populate() releases the page without marking it dirty:
virt/kvm/guest_memfd.c:kvm_gmem_populate() {
...
if (src_page)
put_page(src_page);
...
}Could this result in the userspace VMM permanently losing the error details if the memory is file-backed or pushed to swap and discarded without writeback?
[Severity: Critical]
This is a pre-existing issue, but does sev_gmem_post_populate() unmap pages in the correct order?
If __sev_issue_cmd() fails, it maps src_vaddr and dst_vaddr, but appears to unmap them in FIFO order:
arch/x86/kvm/svm/sev.c:sev_gmem_post_populate() {
...
kunmap_local(src_vaddr);
kunmap_local(dst_vaddr);
...
}Since the kmap_local API strictly requires LIFO unmapping, could an untrusted VMM intentionally passing an invalid CPUID page trigger a host kernel crash or corrupt the per-CPU kmap_ctrl stack?
[Severity: High]
This is a pre-existing issue, but is there a data race and use-after-free risk on kvm_memory_slot between memslot teardown and kvm_gmem_error_folio()?
When KVM destroys a memslot, kvm_gmem_unbind() fast-paths the teardown if there is no file:
virt/kvm/guest_memfd.c:kvm_gmem_unbind() {
if (!file) {
__kvm_gmem_unbind(slot, slot->gmem.file->private_data);
return;
}
...
}This deliberately skips acquiring filemap_invalidate_lock(file->f_mapping).
If a hardware memory failure concurrently triggers kvm_gmem_error_folio(), it iterates over f->bindings holding only filemap_invalidate_lock_shared().
Because xa_for_each_range() drops the internal RCU lock for each element, could it yield the slot pointer right as kvm_gmem_unbind() completes and frees it, leading to a dereference of freed memory here?
virt/kvm/guest_memfd.c:__kvm_gmem_invalidate_begin() {
...
.start = slot->base_gfn + max(pgoff, start) - pgoff,
...
}Quoted 2 lines
arch/x86/kvm/svm/sev.c +0
-0if (count <0 ) { argp->error = sev_populate_args.fw_error;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH 4/4] kvm: sev: Acquire a writeable page reference for CPUID pages
Quoted 3 lines · [email protected] writes:
[...snip...]
Thanks Jorg, commit f13e90059908 ("KVM: SEV: Pin source page for write when adding CPUID data for SNP guest") is a combination of this patch and the previous, so this is fixed too.
This report caught my attention:
Quoted 15 lines
[Severity: High]
This is a pre-existing issue, but is there a data race and use-after-free risk onkvm_memory_slotbetween memslot teardown andkvm_gmem_error_folio()?When KVM destroys a memslot,
kvm_gmem_unbind()fast-paths the teardown if there is no file:virt/kvm/guest_memfd.c:kvm_gmem_unbind() { if (!file) { __kvm_gmem_unbind(slot, slot->gmem.file->private_data); return; } ... }
Quoted 2 lines
This deliberately skips acquiring
filemap_invalidate_lock(file->f_mapping).
Here's how kvm_gmem_unbind() relates to kvm_gmem_release():
This unbinding without filemap_invalidate_lock() covers the case where
+ During VM destruction, the memslots are being destroyed:
kvm_gmem_unbind() is reached without holding slots_lock
and
+ There's a concurrent guest_memfd kvm_gmem_release()
slot->gmem.file is not NULL (yet, kvm_gmem_release() would set that), but the file is already closed, hence kvm_gmem_get_file() returns NULL. (See CLASS(gmem_get_file, file)(slot);).
Since kvm_gmem_get_file() returns NULL, there's no way to safely take the filemap_invalidate_lock(), but we must still do the unbinding. Without unbinding, kvm_gmem_release() would iterate f->bindings and use slot after free.
Quoted 3 lines
If a hardware memory failure concurrently triggers
kvm_gmem_error_folio(), it iterates overf->bindingsholding onlyfilemap_invalidate_lock_shared().
To get to the above unbinding without holding filemap_invalidate_lock(), the file would have to already be closed. If the file is closed, the inode (and folios) can still be alive. Memory failure can look up the folios and hence the inode.
But to reach __kvm_gmem_invalidate_begin() from kvm_gmem_error_folio(), we'd have to iterate the files associated with the inode (kvm_gmem_for_each_file() in kvm_gmem_invalidate()). Since the file is already closed to unbind without filemap_invalidate_lock(), the kvm_gmem_error_folio() side won't see the file.
Quoted 4 lines
Because
xa_for_each_range()drops the internal RCU lock for each element, could it yield the slot pointer right askvm_gmem_unbind()completes and frees it, leading to a dereference of freed memory here?
No issue here. Phew.
Quoted 11 lines
virt/kvm/guest_memfd.c:__kvm_gmem_invalidate_begin() { ... .start = slot->base_gfn + max(pgoff, start) - pgoff, ... }arch/x86/kvm/svm/sev.c static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2485
Quoted 2 lines
arch/x86/kvm/svm/sev.c +0
-0if (count <0 ) { argp->error = sev_populate_args.fw_error;--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4