Skip to content

Commit 043248b

Browse files
committed
KVM: VMX: Forbid userspace MSR filters for x2APIC
Allowing userspace to intercept reads to x2APIC MSRs when APICV is fully enabled for the guest simply can't work. But more in general, the LAPIC could be set to in-kernel after the MSR filter is setup and allowing accesses by userspace would be very confusing. We could in principle allow userspace to intercept reads and writes to TPR, and writes to EOI and SELF_IPI, but while that could be made it work, it would still be silly. Cc: Alexander Graf <graf@amazon.com> Cc: Aaron Lewis <aaronlewis@google.com> Cc: Peter Xu <peterx@redhat.com> Cc: Sean Christopherson <sean.j.christopherson@intel.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 9389b9d commit 043248b

2 files changed

Lines changed: 18 additions & 9 deletions

File tree

‎Documentation/virt/kvm/api.rst‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4777,20 +4777,22 @@ specify whether a certain MSR access should be explicitly filtered for or not.
47774777
If this ioctl has never been invoked, MSR accesses are not guarded and the
47784778
default KVM in-kernel emulation behavior is fully preserved.
47794779

4780+
Calling this ioctl with an empty set of ranges (all nmsrs == 0) disables MSR
4781+
filtering. In that mode, ``KVM_MSR_FILTER_DEFAULT_DENY`` is invalid and causes
4782+
an error.
4783+
47804784
As soon as the filtering is in place, every MSR access is processed through
47814785
the filtering except for accesses to the x2APIC MSRs (from 0x800 to 0x8ff);
47824786
x2APIC MSRs are always allowed, independent of the ``default_allow`` setting,
47834787
and their behavior depends on the ``X2APIC_ENABLE`` bit of the APIC base
47844788
register.
47854789

4786-
If a bit is within one of the defined ranges, read and write
4787-
accesses are guarded by the bitmap's value for the MSR index. If it is not
4788-
defined in any range, whether MSR access is rejected is determined by the flags
4789-
field in the kvm_msr_filter struct: ``KVM_MSR_FILTER_DEFAULT_ALLOW`` and
4790-
``KVM_MSR_FILTER_DEFAULT_DENY``.
4791-
4792-
Calling this ioctl with an empty set of ranges (all nmsrs == 0) disables MSR
4793-
filtering. In that mode, KVM_MSR_FILTER_DEFAULT_DENY no longer has any effect.
4790+
If a bit is within one of the defined ranges, read and write accesses are
4791+
guarded by the bitmap's value for the MSR index if the kind of access
4792+
is included in the ``struct kvm_msr_filter_range`` flags. If no range
4793+
cover this particular access, the behavior is determined by the flags
4794+
field in the kvm_msr_filter struct: ``KVM_MSR_FILTER_DEFAULT_ALLOW``
4795+
and ``KVM_MSR_FILTER_DEFAULT_DENY``.
47944796

47954797
Each bitmap range specifies a range of MSRs to potentially allow access on.
47964798
The range goes from MSR index [base .. base+nmsrs]. The flags field

‎arch/x86/kvm/x86.c‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5252,14 +5252,21 @@ static int kvm_vm_ioctl_set_msr_filter(struct kvm *kvm, void __user *argp)
52525252
struct kvm_msr_filter filter;
52535253
bool default_allow;
52545254
int r = 0;
5255+
bool empty = true;
52555256
u32 i;
52565257

52575258
if (copy_from_user(&filter, user_msr_filter, sizeof(filter)))
52585259
return -EFAULT;
52595260

5260-
kvm_clear_msr_filter(kvm);
5261+
for (i = 0; i < ARRAY_SIZE(filter.ranges); i++)
5262+
empty &= !filter.ranges[i].nmsrs;
52615263

52625264
default_allow = !(filter.flags & KVM_MSR_FILTER_DEFAULT_DENY);
5265+
if (empty && !default_allow)
5266+
return -EINVAL;
5267+
5268+
kvm_clear_msr_filter(kvm);
5269+
52635270
kvm->arch.msr_filter.default_allow = default_allow;
52645271

52655272
/*

0 commit comments

Comments
 (0)