From: Xiaoyao Li <xiaoyao.li@intel.com>
To: Sean Christopherson <seanjc@google.com>,
Paolo Bonzini <pbonzini@redhat.com>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
Like Xu <like.xu.linux@gmail.com>
Subject: Re: [RFC PATCH 3/3] KVM: x86: Disallow writes to immutable feature MSRs after KVM_RUN
Date: Wed, 10 Aug 2022 14:29:47 +0800 [thread overview]
Message-ID: <40c9ecc1-e223-160b-4939-07e4f7200781@intel.com> (raw)
In-Reply-To: <20220805172945.35412-4-seanjc@google.com>
On 8/6/2022 1:29 AM, Sean Christopherson wrote:
> Disallow writes to feature MSRs after KVM_RUN to prevent userspace from
> changing the vCPU model after running the vCPU. Similar to guest CPUID,
> KVM uses feature MSRs to configure intercepts, determine what operations
> are/aren't allowed, etc. Changing the capabilities while the vCPU is
> active will at best yield unpredictable guest behavior, and at worst
> could be dangerous to KVM.
>
> Allow writing the current value, e.g. so that userspace can blindly set
> all MSRs when emulating RESET, and unconditionally allow writes to
> MSR_IA32_UCODE_REV so that userspace can emulate patch loads.
>
> Special case the VMX MSRs to keep the generic list small, i.e. so that
> KVM can do a linear walk of the generic list without incurring meaningful
> overhead.
>
> Cc: Like Xu <like.xu.linux@gmail.com>
> Signed-off-by: Sean Christopherson <seanjc@google.com>
> ---
> arch/x86/kvm/x86.c | 37 +++++++++++++++++++++++++++++++++++++
> 1 file changed, 37 insertions(+)
>
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index a1c65b77fb16..4da26a1f14c1 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -1541,6 +1541,26 @@ static u32 msr_based_features[ARRAY_SIZE(msr_based_features_all_except_vmx) +
> (KVM_LAST_EMULATED_VMX_MSR - KVM_FIRST_EMULATED_VMX_MSR + 1)];
> static unsigned int num_msr_based_features;
>
> +/*
> + * All feature MSRs except uCode revID, which tracks the currently loaded uCode
> + * patch, are immutable once the vCPU model is defined.
> + */
> +static bool kvm_is_immutable_feature_msr(u32 msr)
> +{
> + int i;
> +
> + if (msr >= KVM_FIRST_EMULATED_VMX_MSR && msr <= KVM_LAST_EMULATED_VMX_MSR)
> + return true;
> +
> + for (i = 0; i < ARRAY_SIZE(msr_based_features_all_except_vmx); i++) {
> + if (msr == msr_based_features_all_except_vmx[i])
> + return msr != MSR_IA32_UCODE_REV;
> + }
> +
> + return false;
> +}
> +
> +
> static u64 kvm_get_arch_capabilities(void)
> {
> u64 data = 0;
> @@ -2136,6 +2156,23 @@ static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
>
> static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
> {
> + u64 val;
> +
> + /*
> + * Disallow writes to immutable feature MSRs after KVM_RUN. KVM does
> + * not support modifying the guest vCPU model on the fly, e.g. changing
> + * the nVMX capabilities while L2 is running is nonsensical. Ignore
> + * writes of the same value, e.g. to allow userspace to blindly stuff
> + * all MSRs when emulating RESET.
> + */
> + if (vcpu->arch.last_vmentry_cpu != -1 &&
can we extract "vcpu->arch.last_vmentry_cpu != -1" into a function like
kvm_vcpu_has_runned() ?
> + kvm_is_immutable_feature_msr(index)) {
> + if (do_get_msr(vcpu, index, &val) || *data != val)
> + return -EINVAL;
> +
> + return 0;
> + }
> +
> return kvm_set_msr_ignored_check(vcpu, index, *data, true);
> }
>
next prev parent reply other threads:[~2022-08-10 6:30 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-05 17:29 [RFC PATCH 0/3] KVM: x86: Disallow writes to feature MSRs post-KVM_RUN Sean Christopherson
2022-08-05 17:29 ` [RFC PATCH 1/3] KVM: x86: Add macros to track first...last VMX feature MSRs Sean Christopherson
2022-08-05 17:29 ` [RFC PATCH 2/3] KVM: x86: Generate set of VMX feature MSRs using first/last definitions Sean Christopherson
2022-08-10 12:52 ` Paolo Bonzini
2022-08-10 14:40 ` Sean Christopherson
2022-08-10 19:16 ` Paolo Bonzini
2022-08-05 17:29 ` [RFC PATCH 3/3] KVM: x86: Disallow writes to immutable feature MSRs after KVM_RUN Sean Christopherson
2022-08-10 6:29 ` Xiaoyao Li [this message]
2022-08-10 14:45 ` Sean Christopherson
2022-08-10 12:52 ` [RFC PATCH 0/3] KVM: x86: Disallow writes to feature MSRs post-KVM_RUN 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=40c9ecc1-e223-160b-4939-07e4f7200781@intel.com \
--to=xiaoyao.li@intel.com \
--cc=kvm@vger.kernel.org \
--cc=like.xu.linux@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@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