kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jim Mattson <jmattson@google.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: kvm list <kvm@vger.kernel.org>, Liran Alon <liran.alon@oracle.com>
Subject: Re: [PATCH] kvm: nVMX: VMWRITE checks VMCS-link pointer before VMCS field
Date: Thu, 5 Dec 2019 13:30:40 -0800	[thread overview]
Message-ID: <CALMp9eTyhRwqsriLGg1xoO2sOPkgnKK1hV1U3C733xCjW7+VCA@mail.gmail.com> (raw)
In-Reply-To: <CALMp9eRbiKnH15NBFk0hrh8udcqZvu6RHm0Nrfh4TikQ3xF6OA@mail.gmail.com>

On Thu, Dec 5, 2019 at 5:11 AM Jim Mattson <jmattson@google.com> wrote:
>
> On Thu, Dec 5, 2019 at 3:46 AM Paolo Bonzini <pbonzini@redhat.com> wrote:
> >
> > On 04/12/19 22:40, Jim Mattson wrote:
> > > According to the SDM, a VMWRITE in VMX non-root operation with an
> > > invalid VMCS-link pointer results in VMfailInvalid before the validity
> > > of the VMCS field in the secondary source operand is checked.
> > >
> > > Fixes: 6d894f498f5d1 ("KVM: nVMX: vmread/vmwrite: Use shadow vmcs12 if running L2")
> > > Signed-off-by: Jim Mattson <jmattson@google.com>
> > > Cc: Liran Alon <liran.alon@oracle.com>
> > > ---
> > >  arch/x86/kvm/vmx/nested.c | 38 +++++++++++++++++++-------------------
> > >  1 file changed, 19 insertions(+), 19 deletions(-)
> >
> > As Vitaly pointed out, the test must be split in two, like this:
>
> Right. Odd that no kvm-unit-tests noticed.
>
> > ---------------- 8< -----------------------
> > From 3b9d87060e800ffae2bd19da94ede05018066c87 Mon Sep 17 00:00:00 2001
> > From: Paolo Bonzini <pbonzini@redhat.com>
> > Date: Thu, 5 Dec 2019 12:39:07 +0100
> > Subject: [PATCH] kvm: nVMX: VMWRITE checks VMCS-link pointer before VMCS field
> >
> > According to the SDM, a VMWRITE in VMX non-root operation with an
> > invalid VMCS-link pointer results in VMfailInvalid before the validity
> > of the VMCS field in the secondary source operand is checked.
> >
> > While cleaning up handle_vmwrite, make the code of handle_vmread look
> > the same, too.
>
> Okay.
>
> > Fixes: 6d894f498f5d1 ("KVM: nVMX: vmread/vmwrite: Use shadow vmcs12 if running L2")
> > Signed-off-by: Jim Mattson <jmattson@google.com>
> > Cc: Liran Alon <liran.alon@oracle.com>
> >
> > diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
> > index 4aea7d304beb..c080a879b95d 100644
> > --- a/arch/x86/kvm/vmx/nested.c
> > +++ b/arch/x86/kvm/vmx/nested.c
> > @@ -4767,14 +4767,13 @@ static int handle_vmread(struct kvm_vcpu *vcpu)
> >         if (to_vmx(vcpu)->nested.current_vmptr == -1ull)
> >                 return nested_vmx_failInvalid(vcpu);
> >
> > -       if (!is_guest_mode(vcpu))
> > -               vmcs12 = get_vmcs12(vcpu);
> > -       else {
> > +       vmcs12 = get_vmcs12(vcpu);
> > +       if (is_guest_mode(vcpu)) {
> >                 /*
> >                  * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
> >                  * to shadowed-field sets the ALU flags for VMfailInvalid.
> >                  */
> > -               if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)
> > +               if (vmcs12->vmcs_link_pointer == -1ull)
> >                         return nested_vmx_failInvalid(vcpu);
> >                 vmcs12 = get_shadow_vmcs12(vcpu);
> >         }
> > @@ -4878,8 +4877,19 @@ static int handle_vmwrite(struct kvm_vcpu *vcpu)
> >                 }
> >         }
> >
> > +       vmcs12 = get_vmcs12(vcpu);
> > +       if (is_guest_mode(vcpu)) {
> > +               /*
> > +                * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
> > +                * to shadowed-field sets the ALU flags for VMfailInvalid.
> > +                */
> > +               if (vmcs12->vmcs_link_pointer == -1ull)
> > +                       return nested_vmx_failInvalid(vcpu);
> > +               vmcs12 = get_shadow_vmcs12(vcpu);
> > +       }
> >
> >         field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
> > +
> >         /*
> >          * If the vCPU supports "VMWRITE to any supported field in the
> >          * VMCS," then the "read-only" fields are actually read/write.
> > @@ -4889,24 +4899,12 @@ static int handle_vmwrite(struct kvm_vcpu *vcpu)
> >                 return nested_vmx_failValid(vcpu,
> >                         VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
> >
> > -       if (!is_guest_mode(vcpu)) {
> > -               vmcs12 = get_vmcs12(vcpu);
> > -
> > -               /*
> > -                * Ensure vmcs12 is up-to-date before any VMWRITE that dirties
> > -                * vmcs12, else we may crush a field or consume a stale value.
> > -                */
> > -               if (!is_shadow_field_rw(field))
> > -                       copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
> > -       } else {
> > -               /*
> > -                * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
> > -                * to shadowed-field sets the ALU flags for VMfailInvalid.
> > -                */
> > -               if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)
> > -                       return nested_vmx_failInvalid(vcpu);
> > -               vmcs12 = get_shadow_vmcs12(vcpu);
> > -       }
> > +       /*
> > +        * Ensure vmcs12 is up-to-date before any VMWRITE that dirties
> > +        * vmcs12, else we may crush a field or consume a stale value.
> > +        */
> > +       if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field))
> > +               copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
> >
> >         offset = vmcs_field_to_offset(field);
> >         if (offset < 0)
> >
> >
> > ... and also, do you have a matching kvm-unit-tests patch?
>
> I'll put one together, along with a test that shows the current
> priority inversion between read-only and unsupported VMCS fields.

I can't figure out how to clear IA32_VMX_MISC[bit 29] in qemu, so I'm
going to add the test to tools/testing/selftests/kvm instead.

> > Thanks,
> >
> > Paolo
> >

  reply	other threads:[~2019-12-05 21:30 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-04 21:40 [PATCH] kvm: nVMX: VMWRITE checks VMCS-link pointer before VMCS field Jim Mattson
2019-12-05 11:28 ` Vitaly Kuznetsov
2019-12-05 11:45 ` Paolo Bonzini
2019-12-05 13:11   ` Jim Mattson
2019-12-05 21:30     ` Jim Mattson [this message]
2019-12-05 21:54       ` Liran Alon
2019-12-05 22:11         ` Jim Mattson
2019-12-09 15:28         ` Nadav Amit
2019-12-09 22:58           ` Liran Alon
2019-12-10 22:22             ` Nadav Amit
2019-12-09 16:12       ` Paolo Bonzini
2019-12-09 23:34         ` Jim Mattson
2019-12-10  8:39           ` 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=CALMp9eTyhRwqsriLGg1xoO2sOPkgnKK1hV1U3C733xCjW7+VCA@mail.gmail.com \
    --to=jmattson@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=liran.alon@oracle.com \
    --cc=pbonzini@redhat.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).