kvm: reject unknown flags in device and dirty-log ioctls
cover kvm: reject unknown flags in device and dirty-log ioctls
Three generic KVM paths silently ignored flag bits that uapi or Documentation/virt/kvm/api.rst treat as reserved or zero-only:
- KVM_CREATE_DEVICE (only KVM_CREATE_DEVICE_TEST is defined)
- KVM_{SET,GET,HAS}_DEVICE_ATTR (kvm_device_attr.flags)
- KVM_ENABLE_CAP for KVM_CAP_DIRTY_LOG_RING{,_ACQ_REL} (cap->flags)
Reject with -EINVAL, matching dma-heap and other strict KVM ioctls. Adds ioctl_flag_validation_test to tools/testing/selftests/kvm.
Tested: ioctl_flag_validation_test fails on the host kernel without this patch (KVM_CREATE_DEVICE accepts flags=0x2); requires reloading the patched kvm module to pass.
Iván Ezequiel Rodriguez (1):
kvm: reject unknown flags in device and dirty-log ioctls
tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/ioctl_flag_validation_test.c | 104 +++++++++++++++++++++ virt/kvm/kvm_main.c | 9 ++ 3 files changed, 114 insertions(+)
Signature · 1 line
2.43.0
PATCH 1/1 kvm: reject unknown flags in device and dirty-log ioctls
KVM_CREATE_DEVICE only defines KVM_CREATE_DEVICE_TEST, but unknown flag bits were silently ignored. kvm_device_attr.flags is documented as unused yet was never checked centrally. KVM_ENABLE_CAP for KVM_CAP_DIRTY_LOG_RING and KVM_CAP_DIRTY_LOG_RING_ACQ_REL requires cap->flags to be zero per api.rst, but the generic handler did not enforce it.
Reject unknown or non-zero flags with -EINVAL, consistent with other KVM ioctls and dma-heap flag validation. Add a selftest covering all three paths.
--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../kvm/ioctl_flag_validation_test.c | 104 ++++++++++++++++++ virt/kvm/kvm_main.c | 9 ++ 3 files changed, 114 insertions(+) create mode 100644 tools/testing/selftests/kvm/ioctl_flag_validation_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
@@ -59,6 +59,7 @@ TEST_PROGS_x86 += x86/nx_huge_pages_test.sh TEST_GEN_PROGS_COMMON = demand_paging_test TEST_GEN_PROGS_COMMON += dirty_log_test TEST_GEN_PROGS_COMMON += guest_print_test +TEST_GEN_PROGS_COMMON += ioctl_flag_validation_test TEST_GEN_PROGS_COMMON += irqfd_test TEST_GEN_PROGS_COMMON += kvm_binary_stats_test TEST_GEN_PROGS_COMMON += kvm_create_max_vcpus
tools/testing/selftests/kvm/ioctl_flag_validation_test.c new file +104 -0
104 lines not on this page · raw message
virt/kvm/kvm_main.c +9 -0
@@ -4719,6 +4719,9 @@ static int kvm_device_ioctl_attr(struct kvm_device *dev,if (copy_from_user (&attr, (void __user *)arg,sizeof (attr)))return -EFAULT ; +if (attr.flags) +return -EINVAL ; +return accessor (dev, &attr); }
@@ -4811,6 +4814,9 @@ static int kvm_ioctl_create_device(struct kvm *kvm,int type;int ret; +if (cd->flags & ~KVM_CREATE_DEVICE_TEST ) +return -EINVAL ; +if (cd->type >=ARRAY_SIZE (kvm_device_ops_table))return -ENODEV ;
@@ -5061,6 +5067,9 @@ static int kvm_vm_ioctl_enable_cap_generic(struct kvm *kvm,if (!kvm_vm_ioctl_check_extension_generic (kvm, cap->cap))return -EINVAL ; +if (cap->flags) +return -EINVAL ; +return kvm_vm_ioctl_enable_dirty_log_ring (kvm, cap->args[0 ]);case KVM_CAP_DIRTY_LOG_RING_WITH_BITMAP : {int r = -EINVAL ;
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [Critical] Adding strict flag validation to previously ignored ioctl parameters breaks backwards compatibility for existing userspace applications.
Pre-existing issues:
- [Medium] The validation for kvm_device_attr.flags is incomplete and misses the VM, VCPU, and System ioctl paths. --
commit c3cf74adec51cd659e1e4d1a49a36240977087b9
Author: Iván Ezequiel Rodriguez <[email protected]>
kvm: reject unknown flags in device and dirty-log ioctls
KVM_CREATE_DEVICE only defines KVM_CREATE_DEVICE_TEST, but unknown flag bits were silently ignored. kvm_device_attr.flags is documented as unused yet was never checked centrally. KVM_ENABLE_CAP for KVM_CAP_DIRTY_LOG_RING and KVM_CAP_DIRTY_LOG_RING_ACQ_REL requires cap->flags to be zero per api.rst, but the generic handler did not enforce it.
virt/kvm/kvm_main.c static int kvm_device_ioctl_attr(struct kvm_device *dev, · line 4723
Quoted 10 lines
virt/kvm/kvm_main.c +2
-0@@ -4719,6 +4719,9 @@ static int kvm_device_ioctl_attr(struct kvm_device *dev,if (copy_from_user (&attr, (void __user *)arg,sizeof (attr)))return -EFAULT ; +if (attr.flags) +return -EINVAL ;
[Severity: Critical]
Does adding strict flag validation here break backwards compatibility for existing userspace applications? If userspace previously allocated struct kvm_device_attr on the stack without explicitly zero-initializing the flags field, won't it now fail with -EINVAL since the kernel used to silently ignore it?
[Severity: Medium]
This is a pre-existing issue, but the commit message states kvm_device_attr.flags is now checked "centrally". However, doesn't kvm_device_ioctl_attr() only handle device file descriptors?
The same device attribute ioctls (KVM_HAS_DEVICE_ATTR, KVM_GET_DEVICE_ATTR, KVM_SET_DEVICE_ATTR) can be called on VM or VCPU file descriptors, which are handled in architecture-specific code like kvm_vcpu_ioctl_device_attr() in arch/x86/kvm/x86.c or kvm_arch_vcpu_ioctl() in arch/arm64/kvm/arm.c.
Doesn't this bypass the new validation entirely?
virt/kvm/kvm_main.c static int kvm_ioctl_create_device(struct kvm *kvm, · line 4818
Quoted 10 lines
virt/kvm/kvm_main.c +3
-0+return accessor (dev, &attr); }@@ -4811,6 +4814,9 @@ static int kvm_ioctl_create_device(struct kvm *kvm,int type;int ret; +if (cd->flags & ~KVM_CREATE_DEVICE_TEST ) +return -EINVAL ;
[Severity: Critical]
Similar to the issue above, does rejecting unknown flags retroactively break applications that pass uninitialized stack memory in cd->flags when calling KVM_CREATE_DEVICE?
Quoted 4 lines
virt/kvm/kvm_main.c +1
-0+if (cd->type >=ARRAY_SIZE (kvm_device_ops_table))return -ENODEV ;
[ ... ]
virt/kvm/kvm_main.c static int kvm_vm_ioctl_enable_cap_generic(struct kvm *kvm, · line 5071
Quoted 6 lines
virt/kvm/kvm_main.c +2
-0@@ -5061,6 +5067,9 @@ static int kvm_vm_ioctl_enable_cap_generic(struct kvm *kvm,if (!kvm_vm_ioctl_check_extension_generic (kvm, cap->cap))return -EINVAL ; +if (cap->flags) +return -EINVAL ;
[Severity: Critical]
Will this retroactively break backwards compatibility for applications enabling KVM_CAP_DIRTY_LOG_RING or KVM_CAP_DIRTY_LOG_RING_ACQ_REL if they haven't explicitly zero-initialized cap->flags?
Quoted 4 lines
virt/kvm/kvm_main.c +1
-0+return kvm_vm_ioctl_enable_dirty_log_ring (kvm, cap->args[0 ]);case KVM_CAP_DIRTY_LOG_RING_WITH_BITMAP : {int r = -EINVAL ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1