qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: YiFei Zhu <zhuyifei@google.com>, qemu-devel@nongnu.org
Cc: Zhao Liu <zhao1.liu@intel.com>,
	Richard Henderson <richard.henderson@linaro.org>,
	Eduardo Habkost <eduardo@habkost.net>,
	qemu-stable@nongnu.org, unvariant.winter@gmail.com,
	YiFei Zhu <zhuyifei1999@gmail.com>
Subject: Re: [PATCH 1/2] i386/cpu: Prevent delivering SIPI during SMM in TCG mode
Date: Sat, 11 Oct 2025 09:19:47 +0200	[thread overview]
Message-ID: <2f1ab909-d220-495e-bb14-c231a0e0bb49@redhat.com> (raw)
In-Reply-To: <ca9b26db036fe39ffcb2ebbf7b8629b08632b8c2.1758794468.git.zhuyifei@google.com>

On 9/25/25 12:30, YiFei Zhu wrote:
> A malicious kernel may control the instruction pointer in SMM in a
> multi-processor VM by sending a sequence of IPIs via APIC:
> 
> CPU0			CPU1
> IPI(CPU1, MODE_INIT)
> 			x86_cpu_exec_reset()
> 			apic_init_reset()
> 			s->wait_for_sipi = true
> IPI(CPU1, MODE_SMI)
> 			do_smm_enter()
> 			env->hflags |= HF_SMM_MASK;
> IPI(CPU1, MODE_STARTUP, vector)
> 			do_cpu_sipi()
> 			apic_sipi()
> 			/* s->wait_for_sipi check passes */
> 			cpu_x86_load_seg_cache_sipi(vector)
> 
> A different sequence, SMI INIT SIPI, is also buggy in TCG because
> INIT is not blocked or latched during SMM. However, it is not
> vulnerable to an instruction pointer control in the same way because
> x86_cpu_exec_reset clears env->hflags, exiting SMM.

Thanks for the reports!  For this bug, I prefer to have the CPU eat the 
SIPI instead of latching them:

diff --git a/hw/intc/apic.c b/hw/intc/apic.c
index 6d7859640c2..c7680338563 100644
--- a/hw/intc/apic.c
+++ b/hw/intc/apic.c
@@ -646,8 +646,6 @@ void apic_sipi(DeviceState *dev)
  {
      APICCommonState *s = APIC(dev);

-    cpu_reset_interrupt(CPU(s->cpu), CPU_INTERRUPT_SIPI);
-
      if (!s->wait_for_sipi)
          return;
      cpu_x86_load_seg_cache_sipi(s->cpu, s->sipi_vector);
diff --git a/target/i386/helper.c b/target/i386/helper.c
index 651041ccfa6..a96834c4457 100644
--- a/target/i386/helper.c
+++ b/target/i386/helper.c
@@ -621,6 +621,9 @@ void do_cpu_init(X86CPU *cpu)

  void do_cpu_sipi(X86CPU *cpu)
  {
+    if (env->hflags & HF_SMM_MASK) {
+        return;
+    }
      apic_sipi(cpu->apic_state);
  }

diff --git a/target/i386/tcg/system/seg_helper.c 
b/target/i386/tcg/system/seg_helper.c
index 38072e51d72..8c7856be81e 100644
--- a/target/i386/tcg/system/seg_helper.c
+++ b/target/i386/tcg/system/seg_helper.c
@@ -182,6 +182,7 @@ bool x86_cpu_exec_interrupt(
          apic_poll_irq(cpu->apic_state);
          break;
      case CPU_INTERRUPT_SIPI:
+        cpu_reset_interrupt(cs, CPU_INTERRUPT_SIPI);
          do_cpu_sipi(cpu);
          break;
      case CPU_INTERRUPT_SMI:


Fixing INIT is harder, because it requires splitting CPU_INTERRUPT_INIT 
and CPU_INTERRUPT_RESET, but I'll take a look.

Paolo

> Fixes: a9bad65d2c1f ("target-i386: wake up processors that receive an SMI")
> Signed-off-by: YiFei Zhu <zhuyifei@google.com>
> ---
>   target/i386/cpu.c | 3 ++-
>   1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/target/i386/cpu.c b/target/i386/cpu.c
> index 6d85149e6e..697cc4e63b 100644
> --- a/target/i386/cpu.c
> +++ b/target/i386/cpu.c
> @@ -9762,7 +9762,8 @@ int x86_cpu_pending_interrupt(CPUState *cs, int interrupt_request)
>       if (interrupt_request & CPU_INTERRUPT_POLL) {
>           return CPU_INTERRUPT_POLL;
>       }
> -    if (interrupt_request & CPU_INTERRUPT_SIPI) {
> +    if ((interrupt_request & CPU_INTERRUPT_SIPI) &&
> +        !(env->hflags & HF_SMM_MASK)) {
>           return CPU_INTERRUPT_SIPI;
>       }
>   




  reply	other threads:[~2025-10-11  7:21 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-25 10:30 [PATCH 0/2] i386/tcg: Protect SMM against malicious kernel via IPI & DR YiFei Zhu
2025-09-25 10:30 ` [PATCH 1/2] i386/cpu: Prevent delivering SIPI during SMM in TCG mode YiFei Zhu
2025-10-11  7:19   ` Paolo Bonzini [this message]
2025-10-11  7:48     ` YiFei Zhu
2025-09-25 10:30 ` [PATCH 2/2] i386/tcg/smm_helper: Properly apply DR values on SMM entry / exit YiFei Zhu
2025-10-11  7:22   ` Paolo Bonzini

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=2f1ab909-d220-495e-bb14-c231a0e0bb49@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=eduardo@habkost.net \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-stable@nongnu.org \
    --cc=richard.henderson@linaro.org \
    --cc=unvariant.winter@gmail.com \
    --cc=zhao1.liu@intel.com \
    --cc=zhuyifei1999@gmail.com \
    --cc=zhuyifei@google.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).