All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Sasha Levin <sashal@kernel.org>
Cc: Gavin Guo <gavinguo@igalia.com>,
	stable@vger.kernel.org, mhal@rbox.co,  haoyuwu254@gmail.com,
	pbonzini@redhat.com
Subject: Re: [PATCH 6.6] KVM: x86: Make x2APIC ID 100% readonly
Date: Mon, 6 Jan 2025 12:27:44 -0800	[thread overview]
Message-ID: <Z3w8wPRvjNyDXSQS@google.com> (raw)
In-Reply-To: <Z3AWJjUDmfCnD99S@lappy>

On Sat, Dec 28, 2024, Sasha Levin wrote:
> On Thu, Dec 26, 2024 at 11:38:47AM +0800, Gavin Guo wrote:
> > From: Sean Christopherson <seanjc@google.com>
> > 
> > [ Upstream commit 4b7c3f6d04bd53f2e5b228b6821fb8f5d1ba3071 ]
> > 
> > Ignore the userspace provided x2APIC ID when fixing up APIC state for
> > KVM_SET_LAPIC, i.e. make the x2APIC fully readonly in KVM.  Commit
> > a92e2543d6a8 ("KVM: x86: use hardware-compatible format for APIC ID
> > register"), which added the fixup, didn't intend to allow userspace to
> > modify the x2APIC ID.  In fact, that commit is when KVM first started
> > treating the x2APIC ID as readonly, apparently to fix some race:
> > 
> > static inline u32 kvm_apic_id(struct kvm_lapic *apic)
> > {
> > -       return (kvm_lapic_get_reg(apic, APIC_ID) >> 24) & 0xff;
> > +       /* To avoid a race between apic_base and following APIC_ID update when
> > +        * switching to x2apic_mode, the x2apic mode returns initial x2apic id.
> > +        */
> > +       if (apic_x2apic_mode(apic))
> > +               return apic->vcpu->vcpu_id;
> > +
> > +       return kvm_lapic_get_reg(apic, APIC_ID) >> 24;
> > }
> > 
> > Furthermore, KVM doesn't support delivering interrupts to vCPUs with a
> > modified x2APIC ID, but KVM *does* return the modified value on a guest
> > RDMSR and for KVM_GET_LAPIC.  I.e. no remotely sane setup can actually
> > work with a modified x2APIC ID.
> > 
> > Making the x2APIC ID fully readonly fixes a WARN in KVM's optimized map
> > calculation, which expects the LDR to align with the x2APIC ID.
> > 
> >  WARNING: CPU: 2 PID: 958 at arch/x86/kvm/lapic.c:331 kvm_recalculate_apic_map+0x609/0xa00 [kvm]
> >  CPU: 2 PID: 958 Comm: recalc_apic_map Not tainted 6.4.0-rc3-vanilla+ #35
> >  Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Arch Linux 1.16.2-1-1 04/01/2014
> >  RIP: 0010:kvm_recalculate_apic_map+0x609/0xa00 [kvm]
> >  Call Trace:
> >   <TASK>
> >   kvm_apic_set_state+0x1cf/0x5b0 [kvm]
> >   kvm_arch_vcpu_ioctl+0x1806/0x2100 [kvm]
> >   kvm_vcpu_ioctl+0x663/0x8a0 [kvm]
> >   __x64_sys_ioctl+0xb8/0xf0
> >   do_syscall_64+0x56/0x80
> >   entry_SYSCALL_64_after_hwframe+0x46/0xb0
> >  RIP: 0033:0x7fade8b9dd6f
> > 
> > Unfortunately, the WARN can still trigger for other CPUs than the current
> > one by racing against KVM_SET_LAPIC, so remove it completely.
> > 
> > Reported-by: Michal Luczaj <mhal@rbox.co>
> > Closes: https://lore.kernel.org/all/814baa0c-1eaa-4503-129f-059917365e80@rbox.co
> > Reported-by: Haoyu Wu <haoyuwu254@gmail.com>
> > Closes: https://lore.kernel.org/all/20240126161633.62529-1-haoyuwu254@gmail.com
> > Reported-by: syzbot+545f1326f405db4e1c3e@syzkaller.appspotmail.com
> > Closes: https://lore.kernel.org/all/000000000000c2a6b9061cbca3c3@google.com
> > Signed-off-by: Sean Christopherson <seanjc@google.com>
> > Message-ID: <20240802202941.344889-2-seanjc@google.com>
> > Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> > Signed-off-by: Gavin Guo <gavinguo@igalia.com>
> 
> As this one isn't tagged for stable, the KVM maintainers should ack the
> backport before we take it.

What's the motivation for applying this to 6.6?  AFAIK, there's no real world use
case that benefits from the patch, the fix is purely to plug a hole where fuzzers,
e.g. syzkaller, can trip a WARN.

That said, this is essentially a prerequisite for "KVM: x86: Re-split x2APIC ICR
into ICR+ICR2 for AMD (x2AVIC)"[*], and it's relatively low risk, so I'm not
opposed to landing it in 6.6.

[*] https://lore.kernel.org/all/2024100123-unreached-enrage-2cb1@gregkh

  reply	other threads:[~2025-01-06 20:27 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-26  3:38 [PATCH 6.6] KVM: x86: Make x2APIC ID 100% readonly Gavin Guo
2024-12-26 16:44 ` Sasha Levin
2024-12-28 15:15 ` Sasha Levin
2025-01-06 20:27   ` Sean Christopherson [this message]
2025-01-09 11:57     ` Gavin Guo
2025-01-17 18:55       ` Sean Christopherson

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=Z3w8wPRvjNyDXSQS@google.com \
    --to=seanjc@google.com \
    --cc=gavinguo@igalia.com \
    --cc=haoyuwu254@gmail.com \
    --cc=mhal@rbox.co \
    --cc=pbonzini@redhat.com \
    --cc=sashal@kernel.org \
    --cc=stable@vger.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 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.