Skip to contentSkip to conversation

KVM: Delete guest-triggerable (in theory) BUG_ON() in ioeventfd datamatch

PATCH In review Sean Christopherson 2 participants 3 messages May 27 KVM

lore
Share & tools
lore

Sean Christopherson

author reviewer
#0 lore raw

PATCH 1/1 KVM: Delete guest-triggerable (in theory) BUG_ON() in ioeventfd datamatch

Drop a BUG_ON() that has been reachable since it was first added, way back in 2009.

For a given store, KVM tracks the entire value in the destination operand, x86_emulate_ctxt.dst. If the destination is memory, and the target splits multiple pages and/or is emulated MMIO, then KVM handles each fragment independently. E.g. on a page split starting at page offset 0xffc, KVM writes 4 bytes to the first page, then the remaining bytes to the second page, using ctxt->dst as the source for both (with appropriate offsets).

If the destination splits a page *and* hits emulated MMIO on the second page, then KVM will complete the write to the first page, then emulate the MMIO access to the second page. If there is a datamatch-enabled ioeventfd at offset 0 of the second page, then KVM will process the remainder of the store as a potential ioeventfd signal.

Putting it all together, if the guest emits a store that splits a page starting at page offset N, and the second page has a datamatch-enabled ioeventfd at offset 0, then KVM will check for datamatch using &dst.valptr[N] as the source. Due to dst (and thus dst.valptr) being 32-byte aligned, if N is not aligned to @len, the BUG_ON() fires.

E.g. with a 16-byte store at page offset 0xffc, to an ioeventfd of len 8, all initial checks in ioeventfd_in_range() will succeed, and the BUG_ON() fires due to @val being 4-byte aligned, but not 8-byte aligned.

  ------------[ cut here ]------------
  kernel BUG at arch/x86/kvm/../../../virt/kvm/eventfd.c:783!
  Oops: invalid opcode: 0000 [#1] SMP
  CPU: 0 UID: 1000 PID: 615 Comm: repro Not tainted 7.1.0-rc2-ff238429d1ea #365 PREEMPT
  Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015
  RIP: 0010:ioeventfd_write+0x6c/0x70 [kvm]
  Call Trace:
   <TASK>
   __kvm_io_bus_write+0x85/0xb0 [kvm]
   kvm_io_bus_write+0x53/0x80 [kvm]
   vcpu_mmio_write+0x66/0xf0 [kvm]
   emulator_read_write_onepage+0x12a/0x540 [kvm]
   emulator_read_write+0x109/0x2b0 [kvm]
   x86_emulate_insn+0x4f8/0xfb0 [kvm]
   x86_emulate_instruction+0x181/0x790 [kvm]
   kvm_mmu_page_fault+0x313/0x630 [kvm]
   vmx_handle_exit+0x18a/0x590 [kvm_intel]
   kvm_arch_vcpu_ioctl_run+0xc81/0x1c90 [kvm]
   kvm_vcpu_ioctl+0x2d5/0x970 [kvm]
   __x64_sys_ioctl+0x8a/0xd0
   do_syscall_64+0xb7/0x890
   entry_SYSCALL_64_after_hwframe+0x4b/0x53
  RIP: 0033:0x7f19c931a9bf
   </TASK>
  Modules linked in: kvm_intel kvm irqbypass
  ---[ end trace 0000000000000000 ]---

Simply delete the BUG_ON(), as KVM x86 doesn't perform alignment checks on "normal" memory accesses at CPL0, i.e. the BUG_ON() doesn't actually guard against any badness (presumably it was a sanity check).

Fixes d34e6b175e61 ("KVM: add ioeventfd support")
---
 virt/kvm/eventfd.c | 3 ---
 1 file changed, 3 deletions(-)
virt/kvm/eventfd.c +0 -3
index 0e8b8a2c5b79..d631a86103bb 100644
@@ -779,9 +779,6 @@ ioeventfd_in_range(struct _ioeventfd *p, gpa_t addr, int len, const void *val)
 		return true;
 
 	/* otherwise, we have to actually compare the data */
-
-	BUG_ON(!IS_ALIGNED((unsigned long)val, len));
-
 	switch (len) {
 	case 1:
 		_val = *(u8 *)val;

base-commit: 66939c1603bd5579e63278f9dc72cba5b79da9b5

Signature · 1 line
2.54.0.823.g6e5bcc1fc9-goog
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Removing the `BUG_ON()` alignment guard exposes direct pointer dereferences of potentially…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Removing the BUG_ON() alignment guard exposes direct pointer dereferences of potentially unaligned memory, leading to C undefined behavior and potential alignment faults on architectures with strict alignment requirements. --

commit 811fc6dc622ae4cbc57a596cae2d0500853c54b9
Author: Sean Christopherson <[email protected]>

KVM: Delete guest-triggerable (in theory) BUG_ON() in ioeventfd datamatch

This commit removes a BUG_ON() in the ioeventfd datamatch path that can be triggered by a guest performing a page-split store. The patch removes the alignment check to prevent the guest from causing a host kernel panic.

Quoted 13 lines
virt/kvm/eventfd.c +0 -3
@@ -779,9 +779,6 @@ ioeventfd_in_range(struct _ioeventfd *p, gpa_t addr, int len, const void *val)
 		return true;
 
 	/* otherwise, we have to actually compare the data */
-
-	BUG_ON(!IS_ALIGNED((unsigned long)val, len));
-
 	switch (len) {
 	case 1:
 		_val = *(u8 *)val;

virt/kvm/eventfd.c

Quoted 6 lines
virt/kvm/eventfd.c +0 -0
 	case 2:
 		_val = *(u16 *)val;
 	case 4:
 		_val = *(u32 *)val;
 	case 8:
 		_val = *(u64 *)val;

[Severity: High]
With the alignment check removed, can val now be an unaligned pointer when it reaches these casts?

Because dereferencing an unaligned pointer directly is undefined behavior in C, could this lead to alignment faults on architectures with strict alignment requirements, given that this is in architecture-independent code?

If a system is configured with CONFIG_UBSAN_ALIGNMENT=y and panic_on_warn, wouldn't an unaligned access still allow a guest to trigger a host panic, leaving the original issue partially unresolved?

Does it make sense to use the get_unaligned() helpers here instead of direct casts?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author reviewer
#2 lore raw
Quoted 16 lines · On Wed, May 27, 2026, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Quoted 15 lines
virt/kvm/eventfd.c +0 -3
@@ -779,9 +779,6 @@ ioeventfd_in_range(struct _ioeventfd *p, gpa_t addr, int len, const void *val)
 		return true;
 
 	/* otherwise, we have to actually compare the data */
-
-	BUG_ON(!IS_ALIGNED((unsigned long)val, len));
-
 	switch (len) {
 	case 1:
 		_val = *(u8 *)val;
virt/kvm/eventfd.c +0 -0
 	case 2:
 		_val = *(u16 *)val;
Quoted 8 lines

virt/kvm/eventfd.c

Quoted 4 lines
virt/kvm/eventfd.c +0 -0
 	case 4:
 		_val = *(u32 *)val;
 	case 8:
 		_val = *(u64 *)val;

[Severity: High]
With the alignment check removed, can val now be an unaligned pointer when it reaches these casts?

Obviously.

Quoted 2 lines

Because dereferencing an unaligned pointer directly is undefined behavior in C,

Ugh, C99 ruins all the fun.

Quoted 6 lines

could this lead to alignment faults on architectures with strict alignment requirements, given that this is in architecture-independent code?

If a system is configured with CONFIG_UBSAN_ALIGNMENT=y and panic_on_warn, wouldn't an unaligned access still allow a guest to trigger a host panic, leaving the original issue partially unresolved?

LOL, good luck booting with CONFIG_UBSAN_ALIGNMENT=y and panic_on_warn=1.

Quoted 2 lines

Does it make sense to use the get_unaligned() helpers here instead of direct casts?

That does seem like the easiest solution, as this generates identical code before and after, at least on x86. The other easy fix would be to use memcmp(), but I like not actually changing the functionality in any meaningful way.

kvm/eventfd.c +5 -7
index 0e8b8a2c5b79..93ad2ebc963f 100644
@@ -24,6 +24,7 @@
 #include <linux/slab.h>
 #include <linux/seqlock.h>
 #include <linux/irqbypass.h>
+#include <linux/unaligned.h>
 #include <trace/events/kvm.h>
 
 #include <kvm/iodev.h>
@@ -779,21 +780,18 @@ ioeventfd_in_range(struct _ioeventfd *p, gpa_t addr, int len, const void *val)
                return true;
 
        /* otherwise, we have to actually compare the data */
-
-       BUG_ON(!IS_ALIGNED((unsigned long)val, len));
-
        switch (len) {
        case 1:
-               _val = *(u8 *)val;
+               _val = get_unaligned((u8 *)val);
                break;
        case 2:
-               _val = *(u16 *)val;
+               _val = get_unaligned((u16 *)val);
                break;
        case 4:
-               _val = *(u32 *)val;
+               _val = get_unaligned((u32 *)val);
                break;
        case 8:
-               _val = *(u64 *)val;
+               _val = get_unaligned((u64 *)val);
                break;
        default:
                return false;