Skip to contentSkip to conversation

xen/virtio: Fix use-after-free of grant DMA data after rebind

PATCH New Changyul Lee 1 participant 1 message Nov 19 Xen hypervisor interface

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/virtio: Fix use-after-free of grant DMA data after rebind

When the device is unbound, devres frees its grant DMA data but leaves the entry in xen_grant_dma_devices. On rebind, the remaining entry causes xen_grant_setup_dma_ops() to skip setup. Since x86 retains dev->dma_ops across unbind, subsequent DMA operations access the freed data.

Register a devres action to erase the entry before freeing the data.

Fixes 61367688f1fb ("xen/virtio: enable grant based virtio on x86")

---
Found with a static analysis tool we are developing.

Tested in QEMU with KVM Xen HVM emulation (xen-version=0x4000a) and CONFIG_XEN_VIRTIO_FORCE_GRANT=y, using a virtio-keyboard-pci device with iommu_platform=on. Unbinding and rebinding virtio-pci through sysfs triggers a KASAN slab-use-after-free in xen_grant_dma_alloc() before this patch and no report after it.

 drivers/xen/grant-dma-ops.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/xen/grant-dma-ops.c +12 -0
index 5fcafa4c344f..784413e6d282 100644
@@ -66,6 +66,15 @@ static int store_xen_grant_dma_data(struct device *dev,
 	return ret;
 }
 
+static void remove_xen_grant_dma_data(void *dev)
+{
+	unsigned long flags;
+
+	xa_lock_irqsave(&xen_grant_dma_devices, flags);
+	__xa_erase(&xen_grant_dma_devices, (unsigned long)dev);
+	xa_unlock_irqrestore(&xen_grant_dma_devices, flags);
+}
+
 /*
  * DMA ops for Xen frontends (e.g. virtio).
  *
@@ -396,6 +405,9 @@ static void xen_grant_setup_dma_ops(struct device *dev, domid_t backend_domid)
 		goto err;
 	}
 
+	if (devm_add_action_or_reset(dev, remove_xen_grant_dma_data, dev))
+		goto err;
+
 	dev->dma_ops = &xen_grant_dma_ops;
 
 	return;

base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3

Signature · 1 line
2.43.0