MSR filtering / exiting flag cleanup
v3 cover MSR filtering / exiting flag cleanup
Posting as an RFC to get feedback whether it's too late to protect the unused flag bits. My hope is this feature is still new enough, and not widely used enough, and this change is reasonable enough to be able to be corrected. These bits should have been protected from the start, but unfortunately they were not.
Other approaches to fixing this could be to fix it with a quirk, or the tried and true KVM method of adding a "2" (e.g. KVM_CAP_X86_USER_SPACE_MSR2). Both approaches, however, complicate the code more than it would otherwise be if the original feature could be patched.
For long term simplicity my hope is to be able to just patch the original change.
Note: Patch 1/4 does not change the ABI and patch 3/4 does not contain functional changes, so they are not labeled as RFCs.
v2 -> v3
- Added patch 1/4 to prevent the kernel from using the flag KVM_MSR_FILTER_DEFAULT_ALLOW.
- Cleaned up the selftest code based on feedback.
v1 -> v2
- Added valid masks KVM_MSR_FILTER_VALID_MASK and KVM_MSR_EXIT_REASON_VALID_MASK.
- Added patch 2/3 to add valid mask KVM_MSR_FILTER_RANGE_VALID_MASK, and use it.
- Added testing to demonstrate flag protection when calling the ioctl for KVM_X86_SET_MSR_FILTER or KVM_CAP_X86_USER_SPACE_MSR.
Aaron Lewis (4):
KVM: x86: Do not allow use of the MSR filter allow flag in the kernel
KVM: x86: Protect the unused bits in the MSR filtering / exiting flags
KVM: x86: Add a VALID_MASK for the flags in kvm_msr_filter_range
selftests: kvm/x86: Test the flags in MSR filtering / exiting
arch/x86/include/uapi/asm/kvm.h | 5 ++ arch/x86/kvm/x86.c | 8 +- include/uapi/linux/kvm.h | 3 + .../kvm/x86_64/userspace_msr_exit_test.c | 85 +++++++++++++++++++ 4 files changed, 100 insertions(+), 1 deletion(-)
Signature · 1 line
2.37.1.359.gd136c6c3e2-goog
PATCH v3 1/4 KVM: x86: Do not allow use of the MSR filter allow flag in the kernel
Protect the kernel from using the flag KVM_MSR_FILTER_DEFAULT_ALLOW. Its value is 0, and using it incorrectly could have unintended consequences. E.g. prevent someone in the kernel from writing something like this.
if (filter.flags & KVM_MSR_FILTER_DEFAULT_ALLOW)
<allow the MSR>
and getting confused when it doesn't work.
It would be more ideal to remove this flag altogether, but userspace may already be using it, so protecting the kernel is all that can reasonably be done at this point.
---
Google's VMM is already using this flag, so we *know* that dropping the flag entirely will break userspace. All we can do at this point is prevent the kernel from using it.
arch/x86/include/uapi/asm/kvm.h | 2 ++ 1 file changed, 2 insertions(+)
arch/x86/include/uapi/asm/kvm.h +2 -0
@@ -222,7 +222,9 @@ struct kvm_msr_filter_range {#define KVM_MSR_FILTER_MAX_RANGES 16struct kvm_msr_filter { +#ifndef __KERNEL__#define KVM_MSR_FILTER_DEFAULT_ALLOW (0 << 0) +#endif #define KVM_MSR_FILTER_DEFAULT_DENY (1 << 0)__u32 flags;struct kvm_msr_filter_range ranges[KVM_MSR_FILTER_MAX_RANGES ];
Signature · 1 line
2.37.1.359.gd136c6c3e2-goog
RFC PATCH v3 2/4 KVM: x86: Protect the unused bits in the MSR filtering / exiting flags
In reply to Aaron LewisThe flags used in KVM_CAP_X86_USER_SPACE_MSR and KVM_X86_SET_MSR_FILTER have no protection for their unused bits. Without protection, future development for these features will be difficult. Add the protection needed to make it possible to extend these features in the future.
--- arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/x86.c | 6 ++++++ include/uapi/linux/kvm.h | 3 +++ 3 files changed, 10 insertions(+)
arch/x86/include/uapi/asm/kvm.h +1 -0
@@ -226,6 +226,7 @@ struct kvm_msr_filter {#define KVM_MSR_FILTER_DEFAULT_ALLOW (0 << 0)#endif #define KVM_MSR_FILTER_DEFAULT_DENY (1 << 0) +#define KVM_MSR_FILTER_VALID_MASK (KVM_MSR_FILTER_DEFAULT_DENY)__u32 flags;struct kvm_msr_filter_range ranges[KVM_MSR_FILTER_MAX_RANGES ]; };
arch/x86/kvm/x86.c +6 -0
@@ -6171,6 +6171,9 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, r =0 ;break ;case KVM_CAP_X86_USER_SPACE_MSR : + r = -EINVAL ; +if (cap->args[0 ] & ~KVM_MSR_EXIT_REASON_VALID_MASK ) +break ; kvm->arch.user_space_msr_mask = cap->args[0 ]; r =0 ;break ;
@@ -6384,6 +6387,9 @@ static int kvm_vm_ioctl_set_msr_filter(struct kvm *kvm, void __user *argp)if (copy_from_user (&filter, user_msr_filter,sizeof (filter)))return -EFAULT ; +if (filter.flags & ~KVM_MSR_FILTER_VALID_MASK ) +return -EINVAL ; +for (i =0 ; i <ARRAY_SIZE (filter.ranges); i++) empty &= !filter.ranges[i].nmsrs;
include/uapi/linux/kvm.h +3 -0
@@ -484,6 +484,9 @@ struct kvm_run {#define KVM_MSR_EXIT_REASON_INVAL (1 << 0)#define KVM_MSR_EXIT_REASON_UNKNOWN (1 << 1)#define KVM_MSR_EXIT_REASON_FILTER (1 << 2) +#define KVM_MSR_EXIT_REASON_VALID_MASK (KVM_MSR_EXIT_REASON_INVAL | \ + KVM_MSR_EXIT_REASON_UNKNOWN | \ + KVM_MSR_EXIT_REASON_FILTER)__u32 reason;/* kernel -> user */ __u32 index;/* kernel -> user */ __u64 data;/* kernel <-> user */
Signature · 1 line
2.37.1.359.gd136c6c3e2-goog
PATCH v3 3/4 KVM: x86: Add a VALID_MASK for the flags in kvm_msr_filter_range
In reply to Aaron LewisAdd the mask KVM_MSR_FILTER_RANGE_VALID_MASK for the flags in the struct kvm_msr_filter_range. This simplifies checks that validate these flags, and makes it easier to introduce new flags in the future.
No functional change intended.
--- arch/x86/include/uapi/asm/kvm.h | 2 ++ arch/x86/kvm/x86.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-)
arch/x86/include/uapi/asm/kvm.h +2 -0
@@ -214,6 +214,8 @@ struct kvm_msr_list {struct kvm_msr_filter_range {#define KVM_MSR_FILTER_READ (1 << 0)#define KVM_MSR_FILTER_WRITE (1 << 1) +#define KVM_MSR_FILTER_RANGE_VALID_MASK (KVM_MSR_FILTER_READ | \ + KVM_MSR_FILTER_WRITE)__u32 flags;__u32 nmsrs;/* number of msrs in bitmap */ __u32 base;/* MSR index the bitmap starts at */
arch/x86/kvm/x86.c +1 -1
@@ -6349,7 +6349,7 @@ static int kvm_add_msr_filter(struct kvm_x86_msr_filter *msr_filter,if (!user_range->nmsrs)return 0 ; -if (user_range->flags & ~(KVM_MSR_FILTER_READ |KVM_MSR_FILTER_WRITE )) +if (user_range->flags & ~KVM_MSR_FILTER_RANGE_VALID_MASK )return -EINVAL ;if (!user_range->flags)
Signature · 1 line
2.37.1.359.gd136c6c3e2-goog
RFC PATCH v3 4/4 selftests: kvm/x86: Test the flags in MSR filtering / exiting
In reply to Aaron LewisWhen using the flags in KVM_X86_SET_MSR_FILTER and KVM_CAP_X86_USER_SPACE_MSR it is expected that an attempt to write to any of the unused bits will fail. Add testing to walk over every bit in each of the flag fields in MSR filtering / exiting to verify that happens.
--- .../kvm/x86_64/userspace_msr_exit_test.c | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+)
tools/testing/selftests/kvm/x86_64/userspace_msr_exit_test.c +85 -0
@@ -734,6 +734,89 @@ static void test_msr_permission_bitmap(void)kvm_vm_free (vm); } +#define test_user_exit_msr_ioctl (vm, cmd, arg, flag, valid_mask) \ +({ \ + int r = __vm_ioctl(vm, cmd, arg); \ + \ + if (flag & valid_mask) \ + TEST_ASSERT(!r, __KVM_IOCTL_ERROR(#cmd, r)); \ + else \ + TEST_ASSERT(r == -1 && errno == EINVAL, \ + "Wanted EINVAL for %s with flag = 0x%llx, got rc: %i errno: %i (%s)", \ + #cmd, flag, r, errno, strerror(errno)); \ +}) + +static void run_user_space_msr_flag_test (struct kvm_vm *vm) +{ +struct kvm_enable_cap cap = { .cap =KVM_CAP_X86_USER_SPACE_MSR }; +int nflags =sizeof (cap.args[0 ]) *BITS_PER_BYTE ; +int rc; +int i; + + rc =kvm_check_cap (KVM_CAP_X86_USER_SPACE_MSR ); +TEST_ASSERT (rc,"KVM_CAP_X86_USER_SPACE_MSR is available" ); + +for (i =0 ; i < nflags; i++) { + cap.args[0 ] =BIT_ULL (i); +test_user_exit_msr_ioctl (vm,KVM_ENABLE_CAP , &cap, +BIT_ULL (i),KVM_MSR_EXIT_REASON_VALID_MASK ); + } +} + +static void run_msr_filter_flag_test (struct kvm_vm *vm) +{ +u64 deny_bits =0 ; +struct kvm_msr_filter filter = { + .flags =KVM_MSR_FILTER_DEFAULT_ALLOW , + .ranges = { + { + .flags =KVM_MSR_FILTER_READ , + .nmsrs =1 , + .base =0 , + .bitmap = (uint8_t *)&deny_bits, + }, + }, + }; +int nflags; +int rc; +int i; + + rc =kvm_check_cap (KVM_CAP_X86_MSR_FILTER ); +TEST_ASSERT (rc,"KVM_CAP_X86_MSR_FILTER is available" ); + + nflags =sizeof (filter.flags) *BITS_PER_BYTE ; +for (i =0 ; i < nflags; i++) { + filter.flags =BIT_ULL (i); +test_user_exit_msr_ioctl (vm,KVM_X86_SET_MSR_FILTER , &filter, +BIT_ULL (i),KVM_MSR_FILTER_VALID_MASK ); + } + + filter.flags =KVM_MSR_FILTER_DEFAULT_ALLOW ; + nflags =sizeof (filter.ranges[0 ].flags) *BITS_PER_BYTE ; +for (i =0 ; i < nflags; i++) { + filter.ranges[0 ].flags =BIT_ULL (i); +test_user_exit_msr_ioctl (vm,KVM_X86_SET_MSR_FILTER , &filter, +BIT_ULL (i),KVM_MSR_FILTER_RANGE_VALID_MASK ); + } +} + +/* Test that attempts to write to the unused bits in a flag fails. */ +static void test_user_exit_msr_flags (void ) +{ +struct kvm_vcpu *vcpu; +struct kvm_vm *vm; + + vm =vm_create_with_one_vcpu (&vcpu,NULL ); + +/* Test flags for KVM_CAP_X86_USER_SPACE_MSR. */ +run_user_space_msr_flag_test (vm); + +/* Test flags and range flags for KVM_X86_SET_MSR_FILTER. */ +run_msr_filter_flag_test (vm); + +kvm_vm_free (vm); +} +int main (int argc,char *argv[]) {/* Tell stdout not to buffer its content */
@@ -745,5 +828,7 @@ int main(int argc, char *argv[])test_msr_permission_bitmap (); +test_user_exit_msr_flags (); +return 0 ; }
Signature · 1 line
2.37.1.359.gd136c6c3e2-goog