@@ -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
471148045. 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
51915285For KVM_EXIT_X86_RDMSR, the "index" field tells user space which MSR the guest
51925286wants 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
62656359accesses that would usually trigger a #GP by KVM into the guest will
62666360instead get bounced to user space through the KVM_EXIT_X86_RDMSR and
62676361KVM_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.
0 commit comments