public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Chen Yufeng <chenyufeng@iie.ac.cn>
Cc: pbonzini@redhat.com, tglx@linutronix.de, mingo@redhat.com,
	bp@alien8.de,  dave.hansen@linux.intel.com, x86@kernel.org,
	hpa@zytor.com,  kvm@vger.kernel.org
Subject: Re: [PATCH] kvm: x86: Don't report guest userspace emulation error to userspace in kvm_task_switch()
Date: Wed, 16 Apr 2025 07:12:13 -0700	[thread overview]
Message-ID: <Z_-6veJq79R-H0EH@google.com> (raw)
In-Reply-To: <20250416134326.1342-1-chenyufeng@iie.ac.cn>

On Wed, Apr 16, 2025, Chen Yufeng wrote:
> This patch prevents that emulation failures which result from emulating
> task switch for an L2-Guest results in being reported to userspace.
> 
> Without this patch a malicious L2-Guest would be able to kill the L1 by 
> triggering a race-condition between an vmexit and the task switch emulator.

Only if L1 doesn't intercept task switches, which is only possible on SVM (they
are a mandatory intercept on VMX).  If L1 is deferring task switch emulation to
L0, then IMO L0 is well within its rights to exit to userspace if KVM can't
emulate the task switch.

So unless I'm missing something, I vote to keep the code as-is.

> This patch is smiliar to commit fc3a9157d314 ("KVM: X86: Don't report L2 
> emulation failures to user-space")

Generic emulation is different.  There are legitimate scenarios where KVM needs
to emulate L2 instructions, without L1's explicit consent, and so KVM needs to
guard against L2 playing games with its code stream.

Task switches are very different.  KVM doesn't fetch from the code stream, i.e.
L2 can't play TLB games, and I highly doubt there is a real world hypervisor
that doesn't intercept task switches.

> Fixes: 1051778f6e1e ("KVM: x86: Handle emulation failure directly in kvm_task_switch()")
> 
> Signed-off-by: Chen Yufeng <chenyufeng@iie.ac.cn>
> ---
>  arch/x86/kvm/x86.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 3712dde0bf9d..b22be88196ed 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -11874,9 +11874,11 @@ int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
>  	 */
>  	if (ret || vcpu->mmio_needed) {
>  		vcpu->mmio_needed = false;
> -		vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
> -		vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
> -		vcpu->run->internal.ndata = 0;
> +		if (!is_guest_mode(vcpu)) {
> +			vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
> +			vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
> +			vcpu->run->internal.ndata = 0;
> +		}
>  		return 0;
>  	}
>  
> -- 
> 2.34.1
> 

  reply	other threads:[~2025-04-16 14:12 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-16 13:43 [PATCH] kvm: x86: Don't report guest userspace emulation error to userspace in kvm_task_switch() Chen Yufeng
2025-04-16 14:12 ` Sean Christopherson [this message]
2025-04-17  3:55   ` Chen Yufeng

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=Z_-6veJq79R-H0EH@google.com \
    --to=seanjc@google.com \
    --cc=bp@alien8.de \
    --cc=chenyufeng@iie.ac.cn \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=kvm@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=tglx@linutronix.de \
    --cc=x86@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