From: Sean Christopherson <seanjc@google.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
Will Deacon <will@kernel.org>, Michal Luczaj <mhal@rbox.co>,
Alexander Potapenko <glider@google.com>,
Marc Zyngier <maz@kernel.org>,
Oliver Upton <oliver.upton@linux.dev>
Subject: Re: [PATCH 4/6] Revert "KVM: Fix vcpu_array[0] races"
Date: Mon, 16 Dec 2024 15:05:45 -0800 [thread overview]
Message-ID: <Z2CySZTaW8CXPmEP@google.com> (raw)
In-Reply-To: <8889dc3b-d672-41c3-8d11-e88861b7b38e@redhat.com>
On Sun, Oct 20, 2024, Paolo Bonzini wrote:
> On 10/10/24 19:48, Sean Christopherson wrote:
> > On Thu, Oct 10, 2024, Paolo Bonzini wrote:
...
> > > > @@ -4298,12 +4299,7 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, unsigned long id)
> > > > kvm_get_kvm(kvm);
> > > > r = create_vcpu_fd(vcpu);
> > > > if (r < 0)
> > > > - goto kvm_put_xa_release;
> > > > -
> > > > - if (KVM_BUG_ON(xa_store(&kvm->vcpu_array, vcpu->vcpu_idx, vcpu, 0), kvm)) {
> > > > - r = -EINVAL;
> > > > - goto kvm_put_xa_release;
> > > > - }
> > > > + goto kvm_put_xa_erase;
> > >
> > > I also find it a bit jarring though that we have to undo the insertion. This
> > > is a chicken-and-egg situation where you are pick one operation B that will
> > > have to undo operation A if it fails. But what xa_store is doing, is
> > > breaking this deadlock.
> > >
> > > The code is a bit longer, sure, but I don't see the point in complicating
> > > the vcpu_array invariants and letting an entry disappear.
> >
> > But we only need one rule: vcpu_array[x] is valid if and only if 'x' is less than
> > online_vcpus. And that rule is necessary regardless of whether or not vcpu_array[x]
> > is filled before success is guaranteed.
>
> Even if the invariant is explainable I still find xa_erase to be uglier than
> xa_release, but maybe it's just me.
It's uglier, but has the distinct advantage of not being broken :-D
And while uglier, IMO there's value in having a way for fuzzers to test KVM's
online_vcpus logic. As evidenced by patches 1-3, accessing vcpu_array[] without
first checking online_vcpus is dangerous regardless of how vcpu_array[] is populated.
Allowing fuzzers to trigger removal vcpu_array[] in KASAN kernels provides meaningful
coverage for that code (see Michal's original bug report). While well-intentioned,
Michal's change in afb2acb2e3a3 simply blamed the wrong code. Denying ourselves that
coverage and carrying flawed code just because the correct code isn't the prettiest
doesn't seem like a good tradeoff.
> The reason I'm not fully convinced by the explanation is that...
>
> > I'm not concerned about the code length, it's that we need to do _something_ if
> > xa_store() fails. Yeah, it should never happen, but knowingly doing nothing feels
> > all kinds of wrong.
>
> ... it seems to me that this is not just an issue in KVM code; it should
> apply to other uses of xa_reserve()/xa_store() as well. If xa_store() fails
> after xa_reserve(), you're pretty much using the xarray API incorrectly...
> and then, just make it a BUG()? I know that BUG() is frowned upon, but if
> the API causes invalid memory accesses when used incorrectly, one might as
> well fail as early as possible and before the invalid memory access becomes
> exploitable.
>
> > I don't like BUG(), because it's obviously very doable to
> > gracefully handle failure.
>
> Yes, you can by using a different API. But the point is that in the
> reserve/store case the insert failure becomes a reserve failure, never a
> store failure.
>
> Maybe there should be an xa_store_reserved() that BUGs on failure, I don't
> know.
I agree a version of xa_store() that guarantees success would be nice to have,
but I'm not exactly eager to potentially start a fight Willy *and* Linus :-)
next prev parent reply other threads:[~2024-12-16 23:05 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-09 15:04 [PATCH 0/6] KVM: Fix bugs in vCPUs xarray usage Sean Christopherson
2024-10-09 15:04 ` [PATCH 1/6] KVM: Explicitly verify target vCPU is online in kvm_get_vcpu() Sean Christopherson
2024-10-10 5:31 ` Gupta, Pankaj
2024-10-10 15:54 ` Sean Christopherson
2024-10-10 16:33 ` Gupta, Pankaj
2024-10-09 15:04 ` [PATCH 2/6] KVM: Verify there's at least one online vCPU when iterating over all vCPUs Sean Christopherson
2024-10-09 15:04 ` [PATCH 3/6] KVM: Grab vcpu->mutex across installing the vCPU's fd and bumping online_vcpus Sean Christopherson
2024-10-09 15:04 ` [PATCH 4/6] Revert "KVM: Fix vcpu_array[0] races" Sean Christopherson
2024-10-10 12:46 ` Paolo Bonzini
2024-10-10 17:48 ` Sean Christopherson
2024-10-20 11:23 ` Paolo Bonzini
2024-12-16 23:05 ` Sean Christopherson [this message]
2024-10-09 15:04 ` [PATCH 5/6] KVM: Don't BUG() the kernel if xa_insert() fails with -EBUSY Sean Christopherson
2024-10-10 5:33 ` Gupta, Pankaj
2024-10-09 15:04 ` [PATCH 6/6] KVM: Drop hack that "manually" informs lockdep of kvm->lock vs. vcpu->mutex Sean Christopherson
2024-10-29 14:18 ` [PATCH 0/6] KVM: Fix bugs in vCPUs xarray usage Will Deacon
2024-12-19 2:40 ` Sean Christopherson
2024-12-19 14:18 ` 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=Z2CySZTaW8CXPmEP@google.com \
--to=seanjc@google.com \
--cc=glider@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=maz@kernel.org \
--cc=mhal@rbox.co \
--cc=oliver.upton@linux.dev \
--cc=pbonzini@redhat.com \
--cc=will@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