vfio/cdx: Fix NULL pointer dereference in interrupt trigger path
Subsystems (5)
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.
--- drivers/vfio/cdx/intr.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/vfio/cdx/intr.c +4 -0
@@ -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
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.
--- drivers/cdx/cdx.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/cdx/cdx.c +5 -2
@@ -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
Quoted 11 lines · Prasanna Kumar T S M
Add validation to ensure MSI is configured before accessing
cdx_irqsarray invfio_cdx_set_msi_trigger(). Without this check, userspace can trigger a NULL pointer dereference by callingVFIO_DEVICE_SET_IRQSwithVFIO_IRQ_SET_DATA_BOOLorVFIO_IRQ_SET_DATA_NONEflags before ever setting up interrupts viaVFIO_IRQ_SET_DATA_EVENTFD.The
vfio_cdx_msi_enable()function allocates thecdx_irqsarray and setsconfig_msito 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()checksirq_is()before the trigger loop.Fixes 848e447e000c ("vfio/cdx: add interrupt support")Signed-off-by Prasanna Kumar T S M <[email protected]>
Re: [PATCH 2/2] cdx: Fix double free when sysfs file creation fails
In reply to Prasanna Kumar T S MQuoted 9 lines · Prasanna Kumar T S M
In
cdx_create_res_attr(), ifsysfs_create_bin_file()fails, the code freesres_attrbut doesn't set cdx_dev->res_attr[num] to NULL. This leaves a dangling pointer in the array. Thencdx_destroy_res_attr()frees the already-freed memory. Fix the double free by initializing cdx_dev->res_attr[num] aftersysfs_create_bin_file()completes.Fixes aeda33ab8160 ("cdx: create sysfs bin files for cdx resources")Signed-off-by Prasanna Kumar T S M <[email protected]>
Alex Williamson
maintainerQuoted 12 lines · Nipun
Quoted 11 lines · Prasanna Kumar T S M
Add validation to ensure MSI is configured before accessing
cdx_irqsarray invfio_cdx_set_msi_trigger(). Without this check, userspace can trigger a NULL pointer dereference by callingVFIO_DEVICE_SET_IRQSwithVFIO_IRQ_SET_DATA_BOOLorVFIO_IRQ_SET_DATA_NONEflags before ever setting up interrupts viaVFIO_IRQ_SET_DATA_EVENTFD.The
vfio_cdx_msi_enable()function allocates thecdx_irqsarray and setsconfig_msito 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()checksirq_is()before the trigger loop.Fixes 848e447e000c ("vfio/cdx: add interrupt support")Signed-off-by Prasanna Kumar T S M <[email protected]>Acked-by Nipun Gupta <[email protected]>
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
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_irqsarray invfio_cdx_set_msi_trigger(). Without this check, userspace can trigger a NULL pointer dereference by callingVFIO_DEVICE_SET_IRQSwithVFIO_IRQ_SET_DATA_BOOLorVFIO_IRQ_SET_DATA_NONEflags before ever setting up interrupts viaVFIO_IRQ_SET_DATA_EVENTFD.The
vfio_cdx_msi_enable()function allocates thecdx_irqsarray and setsconfig_msito 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()checksirq_is()before the trigger loop.Fixes 848e447e000c ("vfio/cdx: add interrupt support")Signed-off-by Prasanna Kumar T S M <[email protected]>Acked-by Nipun Gupta <[email protected]>
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_devicethat is acquired with a guard invfio_cdx_set_irqs_ioctl(). That would makeconfig_msistable 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
Alex Williamson
maintainerQuoted 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_irqsarray invfio_cdx_set_msi_trigger(). Without this check, userspace can trigger a NULL pointer dereference by callingVFIO_DEVICE_SET_IRQSwithVFIO_IRQ_SET_DATA_BOOLorVFIO_IRQ_SET_DATA_NONEflags before ever setting up interrupts viaVFIO_IRQ_SET_DATA_EVENTFD.The
vfio_cdx_msi_enable()function allocates thecdx_irqsarray and setsconfig_msito 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()checksirq_is()before the trigger loop.Fixes 848e447e000c ("vfio/cdx: add interrupt support")Signed-off-by Prasanna Kumar T S M <[email protected]>Acked-by Nipun Gupta <[email protected]>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_devicethat is acquired with a guard invfio_cdx_set_irqs_ioctl(). That would makeconfig_msistable 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
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_irqsarray invfio_cdx_set_msi_trigger(). Without this check, userspace can trigger a NULL pointer dereference by callingVFIO_DEVICE_SET_IRQSwithVFIO_IRQ_SET_DATA_BOOLorVFIO_IRQ_SET_DATA_NONEflags before ever setting up interrupts viaVFIO_IRQ_SET_DATA_EVENTFD.The
vfio_cdx_msi_enable()function allocates thecdx_irqsarray and setsconfig_msito 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()checksirq_is()before the trigger loop.Fixes 848e447e000c ("vfio/cdx: add interrupt support")Signed-off-by Prasanna Kumar T S M <[email protected]>Acked-by Nipun Gupta <[email protected]>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_devicethat is acquired with a guard invfio_cdx_set_irqs_ioctl(). That would makeconfig_msistable 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_msiis not set prior to the ioctl, but it doesn't consider concurrency whereconfig_msimay be set when tested, but race with a call tovfio_cdx_msi_disable(). I think the fix needs both the validation ofconfig_msiand 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
Re: [PATCH 2/2] cdx: Fix double free when sysfs file creation fails
In reply to Gupta, NipunHi Nipun,
Quoted 9 lines · Nipun
Quoted 6 lines · Prasanna Kumar T S M
In
cdx_create_res_attr(), ifsysfs_create_bin_file()fails, the code freesres_attrbut doesn't set cdx_dev->res_attr[num] to NULL. This leaves a dangling pointer in the array. Thencdx_destroy_res_attr()frees the already-freed memory. Fix the double free by initializing cdx_dev->res_attr[num] aftersysfs_create_bin_file()completes.
Quoted 5 lines
Quoted 3 lines
Fixes aeda33ab8160 ("cdx: create sysfs bin files for cdx resources")Signed-off-by Prasanna Kumar T S M <[email protected]>Acked-by Nipun Gupta <[email protected]>
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
Gupta, Nipun
maintainerRe: [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(), ifsysfs_create_bin_file()fails, the code freesres_attrbut doesn't set cdx_dev->res_attr[num] to NULL. This leaves a dangling pointer in the array. Thencdx_destroy_res_attr()frees the already-freed memory. Fix the double free by initializing cdx_dev->res_attr[num] aftersysfs_create_bin_file()completes.
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Fixes aeda33ab8160 ("cdx: create sysfs bin files for cdx resources")Signed-off-by Prasanna Kumar T S M <[email protected]>Acked-by Nipun Gupta <[email protected]>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