Re: [PATCH 2/4] KVM: x86: Harden _regs accesses to guard against buggy input

From: Sean Christopherson
Date: Thu May 26 2022 - 11:49:48 EST


On Thu, May 26, 2022, Vitaly Kuznetsov wrote:
> Sean Christopherson <seanjc@xxxxxxxxxx> writes:
> > ---
> > arch/x86/kvm/emulate.c | 6 ++++++
> > 1 file changed, 6 insertions(+)
> >
> > diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
> > index 7226a127ccb4..c58366ae4da2 100644
> > --- a/arch/x86/kvm/emulate.c
> > +++ b/arch/x86/kvm/emulate.c
> > @@ -247,6 +247,9 @@ enum x86_transfer_type {
> >
> > static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
> > {
> > + if (WARN_ON_ONCE(nr >= 16))
> > + nr &= 16 - 1;
>
> As the result of this is unlikely to match the expectation (and I'm
> unsure what's the expectation here in the first place :-), why not use
> KVM_BUG_ON() here instead?

ctxt->vcpu is a 'void *' due to the (IMO futile) separation of the emulator from
regular KVM. I.e. this doesn't have access to the 'kvm'.