Skip to contentSkip to conversation

virt: acrn: Don't free a VM the hypervisor refused to destroy

PATCH In review Yuho Choi 2 participants 3 messages Nov 11 – Nov 24 Acrn hypervisor service module

lore
Share & tools
lore

Yuho Choi

author
#0 lore raw

PATCH 1/1 virt: acrn: Don't free a VM the hypervisor refused to destroy

acrn_dev_release() calls acrn_vm_destroy() and then frees the VM whether or not it was destroyed.

Since commit 4c4c1257b844 ("virt: acrn: Do hcall_destroy_vm() before resource release"), acrn_vm_destroy() returns early when hcall_destroy_vm() fails, so that the VM can be destroyed again later. The VM then stays on acrn_vm_list, and its ioreq clients and irqfds still point to it. That commit names a real-time VM as a case where the hypervisor refuses, since it can keep running without the Service VM. The hypervisor also only destroys a paused VM, so the same happens when the device model exits without pausing the VM.

Once the VM is freed, ioreq_dispatcher() still walks acrn_vm_list on the next I/O request notification and dereferences the freed VM.

Leak the VM instead when it could not be destroyed.

Fixes 4c4c1257b844 ("virt: acrn: Do hcall_destroy_vm() before resource release")

---
Compile-tested only (x86_64 defconfig with ACRN_HSM, W=1); I have no ACRN setup to test it on.

 drivers/virt/acrn/hsm.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/virt/acrn/hsm.c +7 -2
index f170ff4617fdd..8403859e0c868 100644
@@ -427,8 +427,13 @@ static int acrn_dev_release(struct inode *inode, struct file *filp)
 {
 	struct acrn_vm *vm = filp->private_data;
 
-	acrn_vm_destroy(vm);
-	kfree(vm);
+	/*
+	 * If the hypervisor refused to destroy the VM, it is still on
+	 * acrn_vm_list and its ioreq clients and irqfds still use it, so
+	 * leak it.
+	 */
+	if (!acrn_vm_destroy(vm))
+		kfree(vm);
 	return 0;
 }
 

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4

Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 22 lines

-----Original Message-----
From: Yuho Choi <[email protected]>
Sent: Wednesday, November 11, 2026 9:52 AM
To: Li, Fei1 <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>; Shuo Liu <[email protected]>; [email protected]; Yuho Choi <[email protected]>
Subject: [PATCH v1] virt: acrn: Don't free a VM the hypervisor refused to destroy

acrn_dev_release() calls acrn_vm_destroy() and then frees the VM whether or not it was destroyed.

Since commit 4c4c1257b844 ("virt: acrn: Do hcall_destroy_vm() before resource release"), acrn_vm_destroy() returns early when hcall_destroy_vm() fails, so that the VM can be destroyed again later. The VM then stays on acrn_vm_list, and its ioreq clients and irqfds still point to it. That commit names a real-time VM as a case where the hypervisor refuses, since it can keep running without the Service VM.
The hypervisor also only destroys a paused VM, so the same happens when the device model exits without pausing the VM.

Quoted 8 lines

Once the VM is freed, ioreq_dispatcher() still walks acrn_vm_list on the next I/O request notification and dereferences the freed VM.

Leak the VM instead when it could not be destroyed.

Fixes 4c4c1257b844 ("virt: acrn: Do hcall_destroy_vm() before resource

release")

Hi Yuho,

Thanks for fixing this UAF issue!

Besides your current change, I suggest:

1. In acrn_dev_release(), try calling hcall_pause_vm(vm->vmid) before acrn_vm_destroy(vm) when vm->vmid is valid and not yet marked as destroyed.

2. Call try_module_get(THIS_MODULE) in acrn_vm_create() (and roll it back on failure), paired with module_put(THIS_MODULE) when acrn_vm_destroy() succeeds. This keeps the module pinned when a VM cannot be destroyed and remains in memory, preventing rmmod from triggering a kernel crash via active ioeventfd kthreads or irqfd callbacks under CONFIG_ACRN_HSM=m.

Thanks.

Quoted 31 lines

---
Compile-tested only (x86_64 defconfig with ACRN_HSM, W=1); I have no ACRN setup to test it on.

 drivers/virt/acrn/hsm.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/virt/acrn/hsm.c index renamed from drivers/virt/acrn/hsm.c +0 -0

f170ff4617fdd..8403859e0c868 100644

drivers/virt/acrn/hsm.c +7 -2
@@ -427,8 +427,13 @@ static int acrn_dev_release(struct inode *inode,
 struct file *filp)  {
 	struct acrn_vm *vm = filp->private_data;
 
-	acrn_vm_destroy(vm);
-	kfree(vm);
+	/*
+	 * If the hypervisor refused to destroy the VM, it is still on
+	 * acrn_vm_list and its ioreq clients and irqfds still use it, so
+	 * leak it.
+	 */
+	if (!acrn_vm_destroy(vm))
+		kfree(vm);
 	return 0;
 }
 

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4 --
2.43.0

author
#2 lore raw
Quoted 3 lines · On Fri, Nov 20, 2026 at 08:01:14AM +0000, Li, Fei1 wrote:

Hi Yuho,

Thanks for fixing this UAF issue!

Thank you for the review and suggestions.

Quoted 5 lines

Besides your current change, I suggest:

1. In acrn_dev_release(), try calling hcall_pause_vm(vm->vmid) before acrn_vm_destroy(vm) when vm->vmid is valid and not yet marked as destroyed.

That makes sense. If the device model exits without pausing the VM, the hypervisor refuses to destroy it and the VM ends up leaked. Pausing it first in acrn_dev_release() would let it be cleaned up properly in that case.

I'll make the pause best-effort and still attempt to destroy the VM if it fails.
If the destroy also fails, the VM will be retained as in v1.

Quoted 6 lines

2. Call try_module_get(THIS_MODULE) in acrn_vm_create() (and roll it back on failure), paired with module_put(THIS_MODULE) when acrn_vm_destroy() succeeds. This keeps the module pinned when a VM cannot be destroyed and remains in memory, preventing rmmod from triggering a kernel crash via active ioeventfd kthreads or irqfd callbacks under CONFIG_ACRN_HSM=m.

Good point. I had missed the module unload case.

Since acrn_vm_create() is called through an ioctl and acrn_fops.owner is already set to THIS_MODULE, the caller holds a module reference. Could we instead use __module_get(THIS_MODULE) at the end of acrn_vm_create(), after successful initialization? That would avoid having to roll back the module reference on the creation failure paths.

Unless you prefer try_module_get(), I'll go with __module_get() and incorporate both changes in v2.