Skip to contentSkip to conversation

vfio/cdx: Fix NULL pointer dereference in interrupt trigger path

PATCH In review Prasanna Kumar T S M 3 participants 10 messages Mar 20 – Apr 28

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 vfio/cdx: Fix NULL pointer dereference in interrupt trigger path

Add validation to ensure MSI is configured before accessing cdx_irqs array in vfio_cdx_set_msi_trigger(). Without this check, userspace can trigger a NULL pointer dereference by calling VFIO_DEVICE_SET_IRQS with VFIO_IRQ_SET_DATA_BOOL or VFIO_IRQ_SET_DATA_NONE flags before ever setting up interrupts via VFIO_IRQ_SET_DATA_EVENTFD.

The vfio_cdx_msi_enable() function allocates the cdx_irqs array and sets config_msi to 1 only when called through the EVENTFD path. The trigger loop (for DATA_BOOL/DATA_NONE) assumed this had already been done, but there was no enforcement of this call ordering.

This matches the protection used in the PCI VFIO driver where vfio_pci_set_msi_trigger() checks irq_is() before the trigger loop.

Fixes 848e447e000c ("vfio/cdx: add interrupt support")
---
 drivers/vfio/cdx/intr.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/vfio/cdx/intr.c +4 -0
index 8f4402cec9c5..c0eed065e8ef 100644
@@ -175,6 +175,10 @@ static int vfio_cdx_set_msi_trigger(struct vfio_cdx_device *vdev,
 		return ret;
 	}
 
+	/* Ensure MSI is configured before accessing cdx_irqs */
+	if (!vdev->config_msi)
+		return -EINVAL;
+
 	for (i = start; i < start + count; i++) {
 		if (!vdev->cdx_irqs[i].trigger)
 			continue;
Signature · 1 line
2.49.0
#1 lore raw

PATCH 2/2 cdx: Fix double free when sysfs file creation fails

In cdx_create_res_attr(), if sysfs_create_bin_file() fails, the code frees res_attr but doesn't set cdx_dev->res_attr[num] to NULL. This leaves a dangling pointer in the array. Then cdx_destroy_res_attr() frees the already-freed memory. Fix the double free by initializing cdx_dev->res_attr[num] after sysfs_create_bin_file() completes.

Fixes aeda33ab8160 ("cdx: create sysfs bin files for cdx resources")
---
 drivers/cdx/cdx.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/cdx/cdx.c +5 -2
index 9196dc50a48d..a4e03fb07c4c 100644
@@ -768,7 +768,6 @@ static int cdx_create_res_attr(struct cdx_device *cdx_dev, int num)
 
 	sysfs_bin_attr_init(res_attr);
 
-	cdx_dev->res_attr[num] = res_attr;
 	sprintf(res_attr_name, "resource%d", num);
 
 	res_attr->mmap = cdx_mmap_resource;
@@ -777,8 +776,12 @@ static int cdx_create_res_attr(struct cdx_device *cdx_dev, int num)
 	res_attr->size = cdx_resource_len(cdx_dev, num);
 	res_attr->private = (void *)(unsigned long)num;
 	ret = sysfs_create_bin_file(&cdx_dev->dev.kobj, res_attr);
-	if (ret)
+	if (ret) {
 		kfree(res_attr);
+		return ret;
+	}
+
+	cdx_dev->res_attr[num] = res_attr;
 
 	return ret;
 }
Signature · 1 line
2.49.0
maintainer Acked-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] cdx: Fix double free when sysfs file creation fails

Quoted 9 lines · Prasanna Kumar T S M

In cdx_create_res_attr(), if sysfs_create_bin_file() fails, the code frees res_attr but doesn't set cdx_dev->res_attr[num] to NULL. This leaves a dangling pointer in the array. Then cdx_destroy_res_attr() frees the already-freed memory. Fix the double free by initializing cdx_dev->res_attr[num] after sysfs_create_bin_file() completes.

Fixes aeda33ab8160 ("cdx: create sysfs bin files for cdx resources")
#3 lore raw

Re: [PATCH 2/2] cdx: Fix double free when sysfs file creation fails

Hi Nipun,

Quoted 9 lines · Nipun
Quoted 6 lines · Prasanna Kumar T S M

In cdx_create_res_attr(), if sysfs_create_bin_file() fails, the code frees res_attr but doesn't set cdx_dev->res_attr[num] to NULL. This leaves a dangling pointer in the array. Then cdx_destroy_res_attr() frees the already-freed memory. Fix the double free by initializing cdx_dev->res_attr[num] after sysfs_create_bin_file() completes.

Quoted 5 lines
Quoted 3 lines
Fixes aeda33ab8160 ("cdx: create sysfs bin files for cdx resources")

I am not able to see this patch in linux-next, although I can see the other patch.

Is this patch still on track for merging?

Thanks,
Prasanna Kumar

maintainer
#4 lore raw

Re: [PATCH 2/2] cdx: Fix double free when sysfs file creation fails

Hi Prasanna,

Quoted 12 lines · Prasanna Kumar T S M

Hi Nipun,

Quoted 9 lines · Nipun
Quoted 6 lines · Prasanna Kumar T S M

In cdx_create_res_attr(), if sysfs_create_bin_file() fails, the code frees res_attr but doesn't set cdx_dev->res_attr[num] to NULL. This leaves a dangling pointer in the array. Then cdx_destroy_res_attr() frees the already-freed memory. Fix the double free by initializing cdx_dev->res_attr[num] after sysfs_create_bin_file() completes.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Fixes aeda33ab8160 ("cdx: create sysfs bin files for cdx resources")

I am not able to see this patch in linux-next, although I can see the other patch.

Can you please resend this patch as a separate individual patch so that Greg can pick it up. The other patch i.e. patch 1/2 of this was applied to vfio-next tree as a part of series https://lore.kernel.org/all/[email protected]/.

Thanks,
Nipun

maintainer Acked-by on v1 1/2
#5 lore raw
In reply to Prasanna Kumar T S M
Quoted 11 lines · Prasanna Kumar T S M

Add validation to ensure MSI is configured before accessing cdx_irqs array in vfio_cdx_set_msi_trigger(). Without this check, userspace can trigger a NULL pointer dereference by calling VFIO_DEVICE_SET_IRQS with VFIO_IRQ_SET_DATA_BOOL or VFIO_IRQ_SET_DATA_NONE flags before ever setting up interrupts via VFIO_IRQ_SET_DATA_EVENTFD.

The vfio_cdx_msi_enable() function allocates the cdx_irqs array and sets config_msi to 1 only when called through the EVENTFD path. The trigger loop (for DATA_BOOL/DATA_NONE) assumed this had already been done, but there was no enforcement of this call ordering.

Quoted 6 lines

This matches the protection used in the PCI VFIO driver where vfio_pci_set_msi_trigger() checks irq_is() before the trigger loop.

Fixes 848e447e000c ("vfio/cdx: add interrupt support")
maintainer
#6 lore raw
Quoted 12 lines · Nipun
Quoted 11 lines · Prasanna Kumar T S M

Add validation to ensure MSI is configured before accessing cdx_irqs array in vfio_cdx_set_msi_trigger(). Without this check, userspace can trigger a NULL pointer dereference by calling VFIO_DEVICE_SET_IRQS with VFIO_IRQ_SET_DATA_BOOL or VFIO_IRQ_SET_DATA_NONE flags before ever setting up interrupts via VFIO_IRQ_SET_DATA_EVENTFD.

The vfio_cdx_msi_enable() function allocates the cdx_irqs array and sets config_msi to 1 only when called through the EVENTFD path. The trigger loop (for DATA_BOOL/DATA_NONE) assumed this had already been done, but there was no enforcement of this call ordering.

Quoted 8 lines
Quoted 6 lines

This matches the protection used in the PCI VFIO driver where vfio_pci_set_msi_trigger() checks irq_is() before the trigger loop.

Fixes 848e447e000c ("vfio/cdx: add interrupt support")

It's an improvement, but I think it also highlights that interrupt setup for vfio-cdx devices is racy. I think it should adopt a mutex on the vfio_cdx_device that is acquired with a guard in vfio_cdx_set_irqs_ioctl(). That would make config_msi stable for this test. Thanks,

Alex

#7 lore raw
Quoted 24 lines · Alex Williamson
Quoted 12 lines · Nipun
Quoted 11 lines · Prasanna Kumar T S M

Add validation to ensure MSI is configured before accessing cdx_irqs array in vfio_cdx_set_msi_trigger(). Without this check, userspace can trigger a NULL pointer dereference by calling VFIO_DEVICE_SET_IRQS with VFIO_IRQ_SET_DATA_BOOL or VFIO_IRQ_SET_DATA_NONE flags before ever setting up interrupts via VFIO_IRQ_SET_DATA_EVENTFD.

The vfio_cdx_msi_enable() function allocates the cdx_irqs array and sets config_msi to 1 only when called through the EVENTFD path. The trigger loop (for DATA_BOOL/DATA_NONE) assumed this had already been done, but there was no enforcement of this call ordering.

Quoted 8 lines
Quoted 6 lines

This matches the protection used in the PCI VFIO driver where vfio_pci_set_msi_trigger() checks irq_is() before the trigger loop.

Fixes 848e447e000c ("vfio/cdx: add interrupt support")
Quoted 7 lines

It's an improvement, but I think it also highlights that interrupt setup for vfio-cdx devices is racy. I think it should adopt a mutex on the vfio_cdx_device that is acquired with a guard in vfio_cdx_set_irqs_ioctl(). That would make config_msi stable for this test. Thanks,

Alex

This patch is fixing a specific problem. User space can make VFIO_* calls in a specific order to trigger NULL pointer access. This will not get fixed with a mutex.

Regards,
Prasanna Kumar

maintainer
#8 lore raw
Quoted 31 lines · On Thu, 2 Apr 2026 10:14:24 +0530 Prasanna Kumar T S M…
Quoted 30 lines · Alex Williamson
Quoted 12 lines · Nipun
Quoted 11 lines · Prasanna Kumar T S M

Add validation to ensure MSI is configured before accessing cdx_irqs array in vfio_cdx_set_msi_trigger(). Without this check, userspace can trigger a NULL pointer dereference by calling VFIO_DEVICE_SET_IRQS with VFIO_IRQ_SET_DATA_BOOL or VFIO_IRQ_SET_DATA_NONE flags before ever setting up interrupts via VFIO_IRQ_SET_DATA_EVENTFD.

The vfio_cdx_msi_enable() function allocates the cdx_irqs array and sets config_msi to 1 only when called through the EVENTFD path. The trigger loop (for DATA_BOOL/DATA_NONE) assumed this had already been done, but there was no enforcement of this call ordering.

Quoted 8 lines
Quoted 6 lines

This matches the protection used in the PCI VFIO driver where vfio_pci_set_msi_trigger() checks irq_is() before the trigger loop.

Fixes 848e447e000c ("vfio/cdx: add interrupt support")

It's an improvement, but I think it also highlights that interrupt setup for vfio-cdx devices is racy. I think it should adopt a mutex on the vfio_cdx_device that is acquired with a guard in vfio_cdx_set_irqs_ioctl(). That would make config_msi stable for this test. Thanks,

Quoted 5 lines
Quoted 1 line

Alex

This patch is fixing a specific problem. User space can make VFIO_* calls in a specific order to trigger NULL pointer access. This will not get fixed with a mutex.

I'm not saying the fix is wrong, I'm saying it's incomplete. This fixes the specific case where config_msi is not set prior to the ioctl, but it doesn't consider concurrency where config_msi may be set when tested, but race with a call to vfio_cdx_msi_disable(). I think the fix needs both the validation of config_msi and serialization via a mutex such that the test remains valid through the whole ioctl path. Thanks,

Alex

#9 lore raw
Quoted 40 lines · Alex Williamson
Quoted 31 lines · On Thu, 2 Apr 2026 10:14:24 +0530 Prasanna Kumar T S M…
Quoted 30 lines · Alex Williamson
Quoted 12 lines · Nipun
Quoted 11 lines · Prasanna Kumar T S M

Add validation to ensure MSI is configured before accessing cdx_irqs array in vfio_cdx_set_msi_trigger(). Without this check, userspace can trigger a NULL pointer dereference by calling VFIO_DEVICE_SET_IRQS with VFIO_IRQ_SET_DATA_BOOL or VFIO_IRQ_SET_DATA_NONE flags before ever setting up interrupts via VFIO_IRQ_SET_DATA_EVENTFD.

The vfio_cdx_msi_enable() function allocates the cdx_irqs array and sets config_msi to 1 only when called through the EVENTFD path. The trigger loop (for DATA_BOOL/DATA_NONE) assumed this had already been done, but there was no enforcement of this call ordering.

Quoted 8 lines
Quoted 6 lines

This matches the protection used in the PCI VFIO driver where vfio_pci_set_msi_trigger() checks irq_is() before the trigger loop.

Fixes 848e447e000c ("vfio/cdx: add interrupt support")

It's an improvement, but I think it also highlights that interrupt setup for vfio-cdx devices is racy. I think it should adopt a mutex on the vfio_cdx_device that is acquired with a guard in vfio_cdx_set_irqs_ioctl(). That would make config_msi stable for this test. Thanks,

Quoted 5 lines
Quoted 1 line

Alex

This patch is fixing a specific problem. User space can make VFIO_* calls in a specific order to trigger NULL pointer access. This will not get fixed with a mutex.

Quoted 8 lines

I'm not saying the fix is wrong, I'm saying it's incomplete. This fixes the specific case where config_msi is not set prior to the ioctl, but it doesn't consider concurrency where config_msi may be set when tested, but race with a call to vfio_cdx_msi_disable(). I think the fix needs both the validation of config_msi and serialization via a mutex such that the test remains valid through the whole ioctl path. Thanks,

Alex

Thanks for the suggestion. Yes, the concurrency issue exists. It exists even before this patch and it has to be fixed.

The CDX folks will be able to do this faster than me as they have deep expertise on this.

Thanks,
Prasanna Kumar