Skip to content

Commit 1a15525

Browse files
agrafbonzini
authored andcommitted
KVM: x86: Introduce MSR filtering
It's not desireable to have all MSRs always handled by KVM kernel space. Some MSRs would be useful to handle in user space to either emulate behavior (like uCode updates) or differentiate whether they are valid based on the CPU model. To allow user space to specify which MSRs it wants to see handled by KVM, this patch introduces a new ioctl to push filter rules with bitmaps into KVM. Based on these bitmaps, KVM can then decide whether to reject MSR access. With the addition of KVM_CAP_X86_USER_SPACE_MSR it can also deflect the denied MSR events to user space to operate on. If no filter is populated, MSR handling stays identical to before. Signed-off-by: Alexander Graf <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 3eb9001 commit 1a15525

File tree

5 files changed

+289
-1
lines changed

5 files changed

+289
-1
lines changed

Documentation/virt/kvm/api.rst

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4707,6 +4707,99 @@ KVM_PV_VM_VERIFY
47074707
Verify the integrity of the unpacked image. Only if this succeeds,
47084708
KVM is allowed to start protected VCPUs.
47094709

4710+
4.126 KVM_X86_SET_MSR_FILTER
4711+
----------------------------
4712+
4713+
:Capability: KVM_X86_SET_MSR_FILTER
4714+
:Architectures: x86
4715+
:Type: vm ioctl
4716+
:Parameters: struct kvm_msr_filter
4717+
:Returns: 0 on success, < 0 on error
4718+
4719+
::
4720+
4721+
struct kvm_msr_filter_range {
4722+
#define KVM_MSR_FILTER_READ (1 << 0)
4723+
#define KVM_MSR_FILTER_WRITE (1 << 1)
4724+
__u32 flags;
4725+
__u32 nmsrs; /* number of msrs in bitmap */
4726+
__u32 base; /* MSR index the bitmap starts at */
4727+
__u8 *bitmap; /* a 1 bit allows the operations in flags, 0 denies */
4728+
};
4729+
4730+
#define KVM_MSR_FILTER_MAX_RANGES 16
4731+
struct kvm_msr_filter {
4732+
#define KVM_MSR_FILTER_DEFAULT_ALLOW (0 << 0)
4733+
#define KVM_MSR_FILTER_DEFAULT_DENY (1 << 0)
4734+
__u32 flags;
4735+
struct kvm_msr_filter_range ranges[KVM_MSR_FILTER_MAX_RANGES];
4736+
};
4737+
4738+
flags values for struct kvm_msr_filter_range:
4739+
4740+
KVM_MSR_FILTER_READ
4741+
4742+
Filter read accesses to MSRs using the given bitmap. A 0 in the bitmap
4743+
indicates that a read should immediately fail, while a 1 indicates that
4744+
a read for a particular MSR should be handled regardless of the default
4745+
filter action.
4746+
4747+
KVM_MSR_FILTER_WRITE
4748+
4749+
Filter write accesses to MSRs using the given bitmap. A 0 in the bitmap
4750+
indicates that a write should immediately fail, while a 1 indicates that
4751+
a write for a particular MSR should be handled regardless of the default
4752+
filter action.
4753+
4754+
KVM_MSR_FILTER_READ | KVM_MSR_FILTER_WRITE
4755+
4756+
Filter both read and write accesses to MSRs using the given bitmap. A 0
4757+
in the bitmap indicates that both reads and writes should immediately fail,
4758+
while a 1 indicates that reads and writes for a particular MSR are not
4759+
filtered by this range.
4760+
4761+
flags values for struct kvm_msr_filter:
4762+
4763+
KVM_MSR_FILTER_DEFAULT_ALLOW
4764+
4765+
If no filter range matches an MSR index that is getting accessed, KVM will
4766+
fall back to allowing access to the MSR.
4767+
4768+
KVM_MSR_FILTER_DEFAULT_DENY
4769+
4770+
If no filter range matches an MSR index that is getting accessed, KVM will
4771+
fall back to rejecting access to the MSR. In this mode, all MSRs that should
4772+
be processed by KVM need to explicitly be marked as allowed in the bitmaps.
4773+
4774+
This ioctl allows user space to define up to 16 bitmaps of MSR ranges to
4775+
specify whether a certain MSR access should be explicitly filtered for or not.
4776+
4777+
If this ioctl has never been invoked, MSR accesses are not guarded and the
4778+
old KVM in-kernel emulation behavior is fully preserved.
4779+
4780+
As soon as the filtering is in place, every MSR access is processed through
4781+
the filtering. If a bit is within one of the defined ranges, read and write
4782+
accesses are guarded by the bitmap's value for the MSR index. If it is not
4783+
defined in any range, whether MSR access is rejected is determined by the flags
4784+
field in the kvm_msr_filter struct: KVM_MSR_FILTER_DEFAULT_ALLOW and
4785+
KVM_MSR_FILTER_DEFAULT_DENY.
4786+
4787+
Calling this ioctl with an empty set of ranges (all nmsrs == 0) disables MSR
4788+
filtering. In that mode, KVM_MSR_FILTER_DEFAULT_DENY no longer has any effect.
4789+
4790+
Each bitmap range specifies a range of MSRs to potentially allow access on.
4791+
The range goes from MSR index [base .. base+nmsrs]. The flags field
4792+
indicates whether reads, writes or both reads and writes are filtered
4793+
by setting a 1 bit in the bitmap for the corresponding MSR index.
4794+
4795+
If an MSR access is not permitted through the filtering, it generates a
4796+
#GP inside the guest. When combined with KVM_CAP_X86_USER_SPACE_MSR, that
4797+
allows user space to deflect and potentially handle various MSR accesses
4798+
into user space.
4799+
4800+
If a vCPU is in running state while this ioctl is invoked, the vCPU may
4801+
experience inconsistent filtering behavior on MSR accesses.
4802+
47104803

47114804
5. The kvm_run structure
47124805
========================
@@ -5187,6 +5280,7 @@ ENABLE_CAP. Currently valid exit reasons are:
51875280

51885281
KVM_MSR_EXIT_REASON_UNKNOWN - access to MSR that is unknown to KVM
51895282
KVM_MSR_EXIT_REASON_INVAL - access to invalid MSRs or reserved bits
5283+
KVM_MSR_EXIT_REASON_FILTER - access blocked by KVM_X86_SET_MSR_FILTER
51905284

51915285
For KVM_EXIT_X86_RDMSR, the "index" field tells user space which MSR the guest
51925286
wants to read. To respond to this request with a successful read, user space
@@ -6265,3 +6359,17 @@ writes to user space. It can be enabled on a VM level. If enabled, MSR
62656359
accesses that would usually trigger a #GP by KVM into the guest will
62666360
instead get bounced to user space through the KVM_EXIT_X86_RDMSR and
62676361
KVM_EXIT_X86_WRMSR exit notifications.
6362+
6363+
8.25 KVM_X86_SET_MSR_FILTER
6364+
---------------------------
6365+
6366+
:Architectures: x86
6367+
6368+
This capability indicates that KVM supports that accesses to user defined MSRs
6369+
may be rejected. With this capability exposed, KVM exports new VM ioctl
6370+
KVM_X86_SET_MSR_FILTER which user space can call to specify bitmaps of MSR
6371+
ranges that KVM should reject access to.
6372+
6373+
In combination with KVM_CAP_X86_USER_SPACE_MSR, this allows user space to
6374+
trap and emulate MSRs that are outside of the scope of KVM as well as
6375+
limit the attack surface on KVM's MSR emulation code.

arch/x86/include/asm/kvm_host.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
#define KVM_REQ_HV_TLB_FLUSH \
8888
KVM_ARCH_REQ_FLAGS(27, KVM_REQUEST_NO_WAKEUP)
8989
#define KVM_REQ_APF_READY KVM_ARCH_REQ(28)
90+
#define KVM_REQ_MSR_FILTER_CHANGED KVM_ARCH_REQ(29)
9091

9192
#define CR0_RESERVED_BITS \
9293
(~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
@@ -860,6 +861,13 @@ struct kvm_hv {
860861
struct kvm_hv_syndbg hv_syndbg;
861862
};
862863

864+
struct msr_bitmap_range {
865+
u32 flags;
866+
u32 nmsrs;
867+
u32 base;
868+
unsigned long *bitmap;
869+
};
870+
863871
enum kvm_irqchip_mode {
864872
KVM_IRQCHIP_NONE,
865873
KVM_IRQCHIP_KERNEL, /* created with KVM_CREATE_IRQCHIP */
@@ -964,6 +972,12 @@ struct kvm_arch {
964972
/* Deflect RDMSR and WRMSR to user space when they trigger a #GP */
965973
u32 user_space_msr_mask;
966974

975+
struct {
976+
u8 count;
977+
bool default_allow:1;
978+
struct msr_bitmap_range ranges[16];
979+
} msr_filter;
980+
967981
struct kvm_pmu_event_filter *pmu_event_filter;
968982
struct task_struct *nx_lpage_recovery_thread;
969983
};

arch/x86/include/uapi/asm/kvm.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,26 @@ struct kvm_msr_list {
192192
__u32 indices[0];
193193
};
194194

195+
/* Maximum size of any access bitmap in bytes */
196+
#define KVM_MSR_FILTER_MAX_BITMAP_SIZE 0x600
197+
198+
/* for KVM_X86_SET_MSR_FILTER */
199+
struct kvm_msr_filter_range {
195200
#define KVM_MSR_FILTER_READ (1 << 0)
196201
#define KVM_MSR_FILTER_WRITE (1 << 1)
202+
__u32 flags;
203+
__u32 nmsrs; /* number of msrs in bitmap */
204+
__u32 base; /* MSR index the bitmap starts at */
205+
__u8 *bitmap; /* a 1 bit allows the operations in flags, 0 denies */
206+
};
207+
208+
#define KVM_MSR_FILTER_MAX_RANGES 16
209+
struct kvm_msr_filter {
210+
#define KVM_MSR_FILTER_DEFAULT_ALLOW (0 << 0)
211+
#define KVM_MSR_FILTER_DEFAULT_DENY (1 << 0)
212+
__u32 flags;
213+
struct kvm_msr_filter_range ranges[KVM_MSR_FILTER_MAX_RANGES];
214+
};
197215

198216
struct kvm_cpuid_entry {
199217
__u32 function;

0 commit comments

Comments
 (0)