MSR Filtering updates
Subsystems (4)
cover MSR Filtering updates
Fix and update documentation for MSR Filtering. Then, add a commit to prevent MSRs that are not allow to be filtered from being sent to userspace.
Aaron Lewis (3):
KVM: x86: fix documentation for KVM_X86_SET_MSR_FILTER
KVM: x86: update documentation for MSR filtering
KVM: x86: Don't deflect MSRs to userspace that can't be filtered
Documentation/virt/kvm/api.rst | 132 +++++++-------------------------- arch/x86/kvm/x86.c | 16 +++- 2 files changed, 39 insertions(+), 109 deletions(-)
Signature · 1 line
2.37.0.rc0.161.g10f37bed90-goog
PATCH 1/3 KVM: x86: fix documentation for KVM_X86_SET_MSR_FILTER
Two copies of KVM_X86_SET_MSR_FILTER somehow managed to make it's way into the documentation. Remove one copy and merge the difference from the removed copy into the copy that's being kept.
--- Documentation/virt/kvm/api.rst | 115 +++------------------------------ 1 file changed, 8 insertions(+), 107 deletions(-)
Documentation/virt/kvm/api.rst +8 -107
@@ -4074,7 +4074,7 @@ Queues an SMI on the thread's vcpu. 4.97 KVM_X86_SET_MSR_FILTER ---------------------------- -:Capability: KVM_X86_SET_MSR_FILTER +:Capability: KVM_CAP_X86_MSR_FILTER :Architectures: x86 :Type: vm ioctl :Parameters: struct kvm_msr_filter
@@ -4173,8 +4173,12 @@ If an MSR access is not permitted through the filtering, it generates a allows user space to deflect and potentially handle various MSR accesses into user space. -If a vCPU is in running state while this ioctl is invoked, the vCPU may -experience inconsistent filtering behavior on MSR accesses. +Note, invoking this ioctl while a vCPU is running is inherently racy. However, +KVM does guarantee that vCPUs will see either the previous filter or the new +filter, e.g. MSRs with identical settings in both the old and new filter will +have deterministic behavior. + + 4.98 KVM_CREATE_SPAPR_TCE_64 ----------------------------
@@ -5287,110 +5291,7 @@ KVM_PV_DUMP authentication tag all of which are needed to decrypt the dump at a later time. - -4.126 KVM_X86_SET_MSR_FILTER ----------------------------- - -:Capability: KVM_CAP_X86_MSR_FILTER -:Architectures: x86 -:Type: vm ioctl -:Parameters: struct kvm_msr_filter -:Returns: 0 on success, < 0 on error - -:: - - struct kvm_msr_filter_range { - #define KVM_MSR_FILTER_READ (1 << 0) - #define KVM_MSR_FILTER_WRITE (1 << 1) - __u32 flags; - __u32 nmsrs; /* number of msrs in bitmap */ - __u32 base; /* MSR index the bitmap starts at */ - __u8 *bitmap; /* a 1 bit allows the operations in flags, 0 denies */ - }; - - #define KVM_MSR_FILTER_MAX_RANGES 16 - struct kvm_msr_filter { - #define KVM_MSR_FILTER_DEFAULT_ALLOW (0 << 0) - #define KVM_MSR_FILTER_DEFAULT_DENY (1 << 0) - __u32 flags; - struct kvm_msr_filter_range ranges[KVM_MSR_FILTER_MAX_RANGES]; - }; - -flags values for ``struct kvm_msr_filter_range``: - -``KVM_MSR_FILTER_READ`` - - Filter read accesses to MSRs using the given bitmap. A 0 in the bitmap - indicates that a read should immediately fail, while a 1 indicates that - a read for a particular MSR should be handled regardless of the default - filter action. - -``KVM_MSR_FILTER_WRITE`` - - Filter write accesses to MSRs using the given bitmap. A 0 in the bitmap - indicates that a write should immediately fail, while a 1 indicates that - a write for a particular MSR should be handled regardless of the default - filter action. - -``KVM_MSR_FILTER_READ | KVM_MSR_FILTER_WRITE`` - - Filter both read and write accesses to MSRs using the given bitmap. A 0 - in the bitmap indicates that both reads and writes should immediately fail, - while a 1 indicates that reads and writes for a particular MSR are not - filtered by this range. - -flags values for ``struct kvm_msr_filter``: - -``KVM_MSR_FILTER_DEFAULT_ALLOW`` - - If no filter range matches an MSR index that is getting accessed, KVM will - fall back to allowing access to the MSR. - -``KVM_MSR_FILTER_DEFAULT_DENY`` - - If no filter range matches an MSR index that is getting accessed, KVM will - fall back to rejecting access to the MSR. In this mode, all MSRs that should - be processed by KVM need to explicitly be marked as allowed in the bitmaps. - -This ioctl allows user space to define up to 16 bitmaps of MSR ranges to -specify whether a certain MSR access should be explicitly filtered for or not. - -If this ioctl has never been invoked, MSR accesses are not guarded and the -default KVM in-kernel emulation behavior is fully preserved. - -Calling this ioctl with an empty set of ranges (all nmsrs == 0) disables MSR -filtering. In that mode, ``KVM_MSR_FILTER_DEFAULT_DENY`` is invalid and causes -an error. - -As soon as the filtering is in place, every MSR access is processed through -the filtering except for accesses to the x2APIC MSRs (from 0x800 to 0x8ff); -x2APIC MSRs are always allowed, independent of the ``default_allow`` setting, -and their behavior depends on the ``X2APIC_ENABLE`` bit of the APIC base -register. - -If a bit is within one of the defined ranges, read and write accesses are -guarded by the bitmap's value for the MSR index if the kind of access -is included in the ``struct kvm_msr_filter_range`` flags. If no range -cover this particular access, the behavior is determined by the flags -field in the kvm_msr_filter struct: ``KVM_MSR_FILTER_DEFAULT_ALLOW`` -and ``KVM_MSR_FILTER_DEFAULT_DENY``. - -Each bitmap range specifies a range of MSRs to potentially allow access on. -The range goes from MSR index [base .. base+nmsrs]. The flags field -indicates whether reads, writes or both reads and writes are filtered -by setting a 1 bit in the bitmap for the corresponding MSR index. - -If an MSR access is not permitted through the filtering, it generates a -#GP inside the guest. When combined with KVM_CAP_X86_USER_SPACE_MSR, that -allows user space to deflect and potentially handle various MSR accesses -into user space. - -Note, invoking this ioctl with a vCPU is running is inherently racy. However, -KVM does guarantee that vCPUs will see either the previous filter or the new -filter, e.g. MSRs with identical settings in both the old and new filter will -have deterministic behavior. - -4.127 KVM_XEN_HVM_SET_ATTR +4.126 KVM_XEN_HVM_SET_ATTR -------------------------- :Capability: KVM_CAP_XEN_HVM / KVM_XEN_HVM_CONFIG_SHARED_INFO
Signature · 1 line
2.37.0.rc0.161.g10f37bed90-goog
PATCH 2/3 KVM: x86: update documentation for MSR filtering
In reply to Aaron LewisUpdate the documentation to ensure best practices are used by VMM developers when using KVM_X86_SET_MSR_FILTER and KVM_CAP_X86_USER_SPACE_MSR.
--- Documentation/virt/kvm/api.rst | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
Documentation/virt/kvm/api.rst +17 -0
@@ -4178,7 +4178,14 @@ KVM does guarantee that vCPUs will see either the previous filter or the new filter, e.g. MSRs with identical settings in both the old and new filter will have deterministic behavior. +When using filtering for the purpose of deflecting MSR accesses to userspace, +exiting[1] **must** be enabled for the lifetime of filtering. That is to say, +exiting needs to be enabled before filtering is enabled, and exiting needs to +remain enabled until after filtering has been disabled. Doing so avoids the +case where when an MSR access is filtered, instead of deflecting it to +userspace as intended a #GP is injected in the guest. +[1] KVM_CAP_X86_USER_SPACE_MSR set with exit reason KVM_MSR_EXIT_REASON_FILTER. 4.98 KVM_CREATE_SPAPR_TCE_64 ----------------------------
@@ -7191,6 +7198,16 @@ KVM_EXIT_X86_RDMSR and KVM_EXIT_X86_WRMSR exit notifications which user space can then handle to implement model specific MSR handling and/or user notifications to inform a user that an MSR was not handled. +When using filtering[1] for the purpose of deflecting MSR accesses to +userspace, exiting[2] **must** be enabled for the lifetime of filtering. That +is to say, exiting needs to be enabled before filtering is enabled, and exiting +needs to remain enabled until after filtering has been disabled. Doing so +avoids the case where when an MSR access is filtered, instead of deflecting it +to userspace as intended a #GP is injected in the guest. + +[1] Using KVM_X86_SET_MSR_FILTER +[2] KVM_CAP_X86_USER_SPACE_MSR set with exit reason KVM_MSR_EXIT_REASON_FILTER. + 7.22 KVM_CAP_X86_BUS_LOCK_EXIT -------------------------------
Signature · 1 line
2.37.0.rc0.161.g10f37bed90-goog
PATCH 3/3 KVM: x86: Don't deflect MSRs to userspace that can't be filtered
In reply to Aaron LewisIf an MSR is not permitted to be filtered and deflected to userspace, don't then allow it to be deflected to userspace by other means. If an MSR that cannot be filtered #GP's, and KVM is configured to send all MSRs that #GP to userspace, that MSR will be sent to userspace as well. Prevent that from happening by filtering out disallowed MSRs from being deflected to userspace.
--- arch/x86/kvm/x86.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +14 -2
@@ -1712,6 +1712,15 @@ void kvm_enable_efer_bits(u64 mask) }EXPORT_SYMBOL_GPL (kvm_enable_efer_bits); +bool kvm_msr_filtering_disallowed (u32 index) +{ +/* x2APIC MSRs do not support filtering. */ +if (index >=0x800 && index <=0x8ff ) +return true; + +return false; +} +bool kvm_msr_allowed (struct kvm_vcpu *vcpu,u32 index,u32 type) {struct kvm_x86_msr_filter *msr_filter;
@@ -1721,8 +1730,8 @@ bool kvm_msr_allowed(struct kvm_vcpu *vcpu, u32 index, u32 type)int idx;u32 i; -/* x2APIC MSRs do not support filtering. */ -if (index >=0x800 && index <=0x8ff ) +/* Prevent certain MSRs from using MSR Filtering. */ +if (kvm_msr_filtering_disallowed (index))return true; idx =srcu_read_lock (&kvm->srcu);
@@ -1962,6 +1971,9 @@ static int kvm_msr_user_space(struct kvm_vcpu *vcpu, u32 index,if (!(vcpu->kvm->arch.user_space_msr_mask & msr_reason))return 0 ; +if (kvm_msr_filtering_disallowed (index)) +return 0 ; + vcpu->run->exit_reason = exit_reason; vcpu->run->msr.error =0 ;memset (vcpu->run->msr.pad,0 ,sizeof (vcpu->run->msr.pad));
Signature · 1 line
2.37.0.rc0.161.g10f37bed90-goog
kernel test robot
build botRe: [PATCH 3/3] KVM: x86: Don't deflect MSRs to userspace that can't be filtered
Thank you for the patch! Perhaps something to improve:
Hi Aaron,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on kvm/queue]
[also build test WARNING on next-20220701]
[cannot apply to mst-vhost/linux-next linus/master v5.19-rc4] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/intel-lab-lkp/linux/commits/Aaron-Lewis/MSR-Filtering-updates/20220704-031727 base: https://git.kernel.org/pub/scm/virt/kvm/kvm.git queue config: i386-allyesconfig (https://download.01.org/0day-ci/archive/20220704/[email protected]/config) compiler: gcc-11 (Debian 11.3.0-3) 11.3.0 reproduce (this is a W=1 build): # https://github.com/intel-lab-lkp/linux/commit/0c12a0d47fb511592df45bf2030cc200b5bab5ef git remote add linux-review https://github.com/intel-lab-lkp/linux git fetch --no-tags linux-review Aaron-Lewis/MSR-Filtering-updates/20220704-031727 git checkout 0c12a0d47fb511592df45bf2030cc200b5bab5ef # save the config file mkdir build_dir && cp config build_dir/.config make W=1 O=build_dir ARCH=i386 SHELL=/bin/bash arch/x86/kvm/
If you fix the issue, kindly add following tag where applicable
All warnings (new ones prefixed by >>):
>> arch/x86/kvm/x86.c:1715:6: warning: no previous prototype for 'kvm_msr_filtering_disallowed' [-Wmissing-prototypes]
1715 | bool kvm_msr_filtering_disallowed(u32 index)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
vim +/kvm_msr_filtering_disallowed +1715 arch/x86/kvm/x86.c
1714
> 1715 bool kvm_msr_filtering_disallowed(u32 index)
1716 {
1717 /* x2APIC MSRs do not support filtering. */
1718 if (index >= 0x800 && index <= 0x8ff)
1719 return true;
1720
1721 return false;
1722 }
1723
Signature · 2 lines
0-DAY CI Kernel Test Service https://01.org/lkp
kernel test robot
build botRe: [PATCH 3/3] KVM: x86: Don't deflect MSRs to userspace that can't be filtered
In reply to Aaron LewisThank you for the patch! Perhaps something to improve:
Hi Aaron,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on kvm/queue]
[also build test WARNING on next-20220701]
[cannot apply to mst-vhost/linux-next linus/master v5.19-rc4] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/intel-lab-lkp/linux/commits/Aaron-Lewis/MSR-Filtering-updates/20220704-031727 base: https://git.kernel.org/pub/scm/virt/kvm/kvm.git queue config: i386-randconfig-a011 (https://download.01.org/0day-ci/archive/20220704/[email protected]/config) compiler: clang version 15.0.0 (https://github.com/llvm/llvm-project 5d787689b14574fe58ba9798563f4a6df6059fbf)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/0c12a0d47fb511592df45bf2030cc200b5bab5ef
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Aaron-Lewis/MSR-Filtering-updates/20220704-031727
git checkout 0c12a0d47fb511592df45bf2030cc200b5bab5ef
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 SHELL=/bin/bash arch/x86/kvm/
If you fix the issue, kindly add following tag where applicable
All warnings (new ones prefixed by >>):
>> arch/x86/kvm/x86.c:1715:6: warning: no previous prototype for function 'kvm_msr_filtering_disallowed' [-Wmissing-prototypes]
bool kvm_msr_filtering_disallowed(u32 index)
^ arch/x86/kvm/x86.c:1715:1: note: declare 'static' if the function is not intended to be used outside of this translation unit bool kvm_msr_filtering_disallowed(u32 index)
^
static
1 warning generated.
vim +/kvm_msr_filtering_disallowed +1715 arch/x86/kvm/x86.c
1714
> 1715 bool kvm_msr_filtering_disallowed(u32 index)
1716 {
1717 /* x2APIC MSRs do not support filtering. */
1718 if (index >= 0x800 && index <= 0x8ff)
1719 return true;
1720
1721 return false;
1722 }
1723
Signature · 2 lines
0-DAY CI Kernel Test Service https://01.org/lkp
kernel test robot
build botRe: [PATCH 3/3] KVM: x86: Don't deflect MSRs to userspace that can't be filtered
In reply to Aaron LewisThank you for the patch! Yet something to improve:
Hi Aaron,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on kvm/queue]
[also build test ERROR on next-20220701]
[cannot apply to mst-vhost/linux-next linus/master v5.19-rc5] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/intel-lab-lkp/linux/commits/Aaron-Lewis/MSR-Filtering-updates/20220704-031727 base: https://git.kernel.org/pub/scm/virt/kvm/kvm.git queue config: i386-randconfig-a002 (https://download.01.org/0day-ci/archive/20220704/[email protected]/config) compiler: clang version 15.0.0 (https://github.com/llvm/llvm-project 5d787689b14574fe58ba9798563f4a6df6059fbf)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/0c12a0d47fb511592df45bf2030cc200b5bab5ef
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Aaron-Lewis/MSR-Filtering-updates/20220704-031727 git checkout 0c12a0d47fb511592df45bf2030cc200b5bab5ef
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 SHELL=/bin/bash
If you fix the issue, kindly add following tag where applicable
All errors (new ones prefixed by >>):
>> arch/x86/kvm/x86.c:1715:6: error: no previous prototype for function 'kvm_msr_filtering_disallowed' [-Werror,-Wmissing-prototypes]
bool kvm_msr_filtering_disallowed(u32 index)
^ arch/x86/kvm/x86.c:1715:1: note: declare 'static' if the function is not intended to be used outside of this translation unit bool kvm_msr_filtering_disallowed(u32 index)
^
static
1 error generated.
vim +/kvm_msr_filtering_disallowed +1715 arch/x86/kvm/x86.c
1714
> 1715 bool kvm_msr_filtering_disallowed(u32 index)
1716 {
1717 /* x2APIC MSRs do not support filtering. */
1718 if (index >= 0x800 && index <= 0x8ff)
1719 return true;
1720
1721 return false;
1722 }
1723
Signature · 2 lines
0-DAY CI Kernel Test Service https://01.org/lkp
Sean Christopherson
maintainerRe: [PATCH 3/3] KVM: x86: Don't deflect MSRs to userspace that can't be filtered
In reply to Aaron LewisQuoted 6 lines · Aaron Lewis
If an MSR is not permitted to be filtered and deflected to userspace, don't then allow it to be deflected to userspace by other means. If an MSR that cannot be filtered #GP's, and KVM is configured to send all MSRs that #GP to userspace, that MSR will be sent to userspace as well. Prevent that from happening by filtering out disallowed MSRs from being deflected to userspace.
Why? Honest question. KVM doesn't allow filtering x2APIC accesses because supporting that would be messy, and there's no sane use case for intercepting x2APIC accesses if userspace has enabled the in-kernel local APIC.
I can't think of a meaningful use case for intercepting faults on x2APIC MSRs, but I also don't see anything inherently broken with allowing userspace to intercept such faults.
Quoted 10 lines
Signed-off-by Aaron Lewis <[email protected]>--- arch/x86/kvm/x86.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)arch/x86/kvm/x86.c +0
-0
arch/x86/kvm/x86.c void kvm_enable_efer_bits(u64 mask) · line 1715
Quoted 4 lines
arch/x86/kvm/x86.c +1
-0@@ -1712,6 +1712,15 @@ void kvm_enable_efer_bits(u64 mask) }EXPORT_SYMBOL_GPL (kvm_enable_efer_bits); +bool kvm_msr_filtering_disallowed (u32 index)
Should be static, per the test bot.
Quoted 35 lines
arch/x86/kvm/x86.c +13
-2+{ +/* x2APIC MSRs do not support filtering. */ +if (index >=0x800 && index <=0x8ff ) +return true; + +return false; +} +bool kvm_msr_allowed (struct kvm_vcpu *vcpu,u32 index,u32 type) {struct kvm_x86_msr_filter *msr_filter;@@ -1721,8 +1730,8 @@ bool kvm_msr_allowed(struct kvm_vcpu *vcpu, u32 index, u32 type)int idx;u32 i; -/* x2APIC MSRs do not support filtering. */ -if (index >=0x800 && index <=0x8ff ) +/* Prevent certain MSRs from using MSR Filtering. */ +if (kvm_msr_filtering_disallowed (index))return true; idx =srcu_read_lock (&kvm->srcu);@@ -1962,6 +1971,9 @@ static int kvm_msr_user_space(struct kvm_vcpu *vcpu, u32 index,if (!(vcpu->kvm->arch.user_space_msr_mask & msr_reason))return 0 ; +if (kvm_msr_filtering_disallowed (index)) +return 0 ; + vcpu->run->exit_reason = exit_reason; vcpu->run->msr.error =0 ;memset (vcpu->run->msr.pad,0 ,sizeof (vcpu->run->msr.pad));Signature · 1 line
2.37.0.rc0.161.g10f37bed90-goog
Re: [PATCH 3/3] KVM: x86: Don't deflect MSRs to userspace that can't be filtered
Quoted 9 lines · Sean Christopherson
Quoted 6 lines · Aaron Lewis
If an MSR is not permitted to be filtered and deflected to userspace, don't then allow it to be deflected to userspace by other means. If an MSR that cannot be filtered #GP's, and KVM is configured to send all MSRs that #GP to userspace, that MSR will be sent to userspace as well. Prevent that from happening by filtering out disallowed MSRs from being deflected to userspace.
Quoted 7 lines
Why? Honest question. KVM doesn't allow filtering x2APIC accesses because supporting that would be messy, and there's no sane use case for intercepting x2APIC accesses if userspace has enabled the in-kernel local APIC.
I can't think of a meaningful use case for intercepting faults on x2APIC MSRs, but I also don't see anything inherently broken with allowing userspace to intercept such faults.
Ack. I'll drop it in v2.
Quoted 53 lines
Quoted 11 lines
Signed-off-by Aaron Lewis <[email protected]>--- arch/x86/kvm/x86.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)arch/x86/kvm/x86.c +0
-0arch/x86/kvm/x86.c void kvm_enable_efer_bits(u64 mask) · line 1715
Quoted 4 lines
arch/x86/kvm/x86.c +1
-0@@ -1712,6 +1712,15 @@ void kvm_enable_efer_bits(u64 mask) }EXPORT_SYMBOL_GPL (kvm_enable_efer_bits); +bool kvm_msr_filtering_disallowed (u32 index)Should be static, per the test bot.
Quoted 35 lines
arch/x86/kvm/x86.c +13
-3+{ +/* x2APIC MSRs do not support filtering. */ +if (index >=0x800 && index <=0x8ff ) +return true; + +return false; +} +bool kvm_msr_allowed (struct kvm_vcpu *vcpu,u32 index,u32 type) {struct kvm_x86_msr_filter *msr_filter;@@ -1721,8 +1730,8 @@ bool kvm_msr_allowed(struct kvm_vcpu *vcpu, u32 index, u32 type)int idx;u32 i; -/* x2APIC MSRs do not support filtering. */ -if (index >=0x800 && index <=0x8ff ) +/* Prevent certain MSRs from using MSR Filtering. */ +if (kvm_msr_filtering_disallowed (index))return true; idx =srcu_read_lock (&kvm->srcu);@@ -1962,6 +1971,9 @@ static int kvm_msr_user_space(struct kvm_vcpu *vcpu, u32 index,if (!(vcpu->kvm->arch.user_space_msr_mask & msr_reason))return 0 ; +if (kvm_msr_filtering_disallowed (index)) +return 0 ; + vcpu->run->exit_reason = exit_reason; vcpu->run->msr.error =0 ;memset (vcpu->run->msr.pad,0 ,sizeof (vcpu->run->msr.pad)); --2.37.0.rc0.161.g10f37bed90-goog