Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Yosry Ahmed" <yosry@kernel.org>
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH v2 05/10] KVM: selftests: Reuse GPR switching logic for nVMX
Date: Thu, 04 Jun 2026 20:52:03 +0000	[thread overview]
Message-ID: <20260604205203.F09181F00893@smtp.kernel.org> (raw)
In-Reply-To: <20260604203546.365658-6-yosry@kernel.org>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The `guest_regs` array is sized for 15 elements, but 16 elements are accessed, causing an out-of-bounds read/write to the `.bss` section.
- [Medium] `vmlaunch()` and `vmresume()` behavior for guest GPRs diverges based on the `enable_evmcs` flag, silently failing tests that rely on the new GPR switching logic under Enlightened VMCS.
--

commit 496a5916183241f499c38e4f427dd5cbecd4dc37
Author: Yosry Ahmed <yosry@kernel.org>

    KVM: selftests: Reuse GPR switching logic for nVMX
    
    Reuse the GPR switching logic for nVMX by defining VMX_SWITCH_GPRS_ASM,
    which is essentially the same as SVM_SWITCH_GPRS_ASM but also switches
    RAX, replacing the push/pop of a subset of the registers.
    
    The long clobber list of registers is no longer needed as registers are
    saved and restored appropriately (and not clobbered by L2).

> diff --git a/tools/testing/selftests/kvm/include/x86/vmx.h b/tools/testing/selftests/kvm/include/x86/vmx.h
> index 90fffaf915958..64996c310fe4c 100644
> --- a/tools/testing/selftests/kvm/include/x86/vmx.h
> +++ b/tools/testing/selftests/kvm/include/x86/vmx.h
> @@ -363,9 +363,23 @@ static inline u64 vmptrstz(void)
>  	return value;
>  }
>  
> -/*
> - * No guest state (e.g. GPRs) is established by this vmlaunch.
> - */
> +#define VMX_SWITCH_GPRS_ASM \
> +	GUEST_SWITCH_GPR_ASM(rax, GUEST_REGS_RAX) \
> +	GUEST_SWITCH_GPR_ASM(rbx, GUEST_REGS_RBX) \
> +	GUEST_SWITCH_GPR_ASM(rcx, GUEST_REGS_RCX) \
> +	GUEST_SWITCH_GPR_ASM(rdx, GUEST_REGS_RDX) \
> +	GUEST_SWITCH_GPR_ASM(rbp, GUEST_REGS_RBP) \
> +	GUEST_SWITCH_GPR_ASM(rsi, GUEST_REGS_RSI) \
> +	GUEST_SWITCH_GPR_ASM(rdi, GUEST_REGS_RDI) \
> +	GUEST_SWITCH_GPR_ASM(r8,  GUEST_REGS_R8)  \
> +	GUEST_SWITCH_GPR_ASM(r9,  GUEST_REGS_R9)  \
> +	GUEST_SWITCH_GPR_ASM(r10, GUEST_REGS_R10) \
> +	GUEST_SWITCH_GPR_ASM(r11, GUEST_REGS_R11) \
> +	GUEST_SWITCH_GPR_ASM(r12, GUEST_REGS_R12) \
> +	GUEST_SWITCH_GPR_ASM(r13, GUEST_REGS_R13) \
> +	GUEST_SWITCH_GPR_ASM(r14, GUEST_REGS_R14) \
> +	GUEST_SWITCH_GPR_ASM(r15, GUEST_REGS_R15)

[Severity: High]
Does this access past the end of the guest_regs array?

GUEST_REGS_R15 evaluates to 15, and the array is defined in
tools/testing/selftests/kvm/include/x86/processor.h as:

#define NR_GUEST_REGS	GUEST_REGS_R15
extern u64 guest_regs[NR_GUEST_REGS];

Since NR_GUEST_REGS is 15, the array only holds 15 elements (indices 0 to 14).
Calling GUEST_SWITCH_GPR_ASM(r15, GUEST_REGS_R15) will read and write the 16th
element at index 15, resulting in an out-of-bounds access that corrupts memory
in the .bss section.

> +
>  static inline int vmlaunch(void)
>  {
>  	int ret;
> @@ -373,34 +387,23 @@ static inline int vmlaunch(void)
>  	if (enable_evmcs)
>  		return evmcs_vmlaunch();

[Severity: Medium]
Will this break EVMCS test configurations?

If enable_evmcs is true, the code redirects to evmcs_vmlaunch() which wasn't
updated to use VMX_SWITCH_GPRS_ASM.  It still uses the old inline assembly
that completely ignores guest_regs:

tools/testing/selftests/kvm/include/x86/evmcs.h:evmcs_vmlaunch() {
    ...
    __asm__ __volatile__("push %%rbp;"
                 "push %%rcx;"
                 "push %%rdx;"
                 "push %%rsi;"
                 "push %%rdi;"
                 "push $0;"
    ...
}

Any KVM selftest that relies on guest_regs to establish L2 state before VM
entry will now silently execute with uninitialized L2 GPRs when run with EVMCS
enabled.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260604203546.365658-1-yosry@kernel.org?part=5

  reply	other threads:[~2026-06-04 20:52 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-04 20:35 [PATCH v2 00/10] KVM: selftests: Stress save+restore and #PF (ft. nested) Yosry Ahmed
2026-06-04 20:35 ` [PATCH v2 01/10] KVM: selftests: Move STR() and XSTR() definitions to test_util.h Yosry Ahmed
2026-06-04 20:35 ` [PATCH v2 02/10] KVM: selftests: Fix RAX and RFLAGS VMCB offsets when running L2 Yosry Ahmed
2026-06-04 20:35 ` [PATCH v2 03/10] KVM: selftests: Use an array for guest_regs (and fix offsets) Yosry Ahmed
2026-06-04 20:44   ` sashiko-bot
2026-06-04 20:49     ` Yosry Ahmed
2026-06-04 21:37       ` Yosry Ahmed
2026-06-04 20:35 ` [PATCH v2 04/10] KVM: selftests: Move GPR load/save definitions outside of nSVM code Yosry Ahmed
2026-06-04 20:47   ` sashiko-bot
2026-06-04 20:35 ` [PATCH v2 05/10] KVM: selftests: Reuse GPR switching logic for nVMX Yosry Ahmed
2026-06-04 20:52   ` sashiko-bot [this message]
2026-06-04 20:35 ` [PATCH v2 06/10] KVM: selftests: Drop HORRIFIC_L2_UCALL_CLOBBER_HACK Yosry Ahmed
2026-06-04 20:50   ` sashiko-bot
2026-06-04 21:11     ` Yosry Ahmed
2026-06-04 20:35 ` [PATCH v2 07/10] KVM: selftests: Add basic stress test for save+restore and #PF handling Yosry Ahmed
2026-06-05 16:31   ` Yosry Ahmed
2026-06-04 20:35 ` [PATCH v2 08/10] KVM: selftests: Trigger save+restore randomly in the #PF stress test Yosry Ahmed
2026-06-04 20:49   ` sashiko-bot
2026-06-04 20:55     ` Yosry Ahmed
2026-06-04 20:35 ` [PATCH v2 09/10] KVM: selftests: Support running stress save+restore and #PF test in L2 Yosry Ahmed
2026-06-04 20:35 ` [PATCH v2 10/10] KVM: selftests: Trigger L2->L1 exits stress save+restore and #PF test Yosry Ahmed

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=20260604205203.F09181F00893@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=yosry@kernel.org \
    /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