Skip to content

Commit 982ddd5

Browse files
jacobpanawilliam
authored andcommitted
vfio: Prevent open_count decrement to negative
When vfio_df_close() is called with open_count=0, it triggers a warning in vfio_assert_device_open() but still decrements open_count to -1. This allows a subsequent open to incorrectly pass the open_count == 0 check, leading to unintended behavior, such as setting df->access_granted = true. For example, running an IOMMUFD compat no-IOMMU device with VFIO tests (https://github.com/awilliam/tests/blob/master/vfio-noiommu-pci-device-open.c) results in a warning and a failed VFIO_GROUP_GET_DEVICE_FD ioctl on the first run, but the second run succeeds incorrectly. Add checks to avoid decrementing open_count below zero. Fixes: 05f37e1 ("vfio: Pass struct vfio_device_file * to vfio_device_open/close()") Reviewed-by: Jason Gunthorpe <[email protected]> Reviewed-by: Yi Liu <[email protected]> Signed-off-by: Jacob Pan <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alex Williamson <[email protected]>
1 parent b25e271 commit 982ddd5

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

drivers/vfio/vfio_main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,8 @@ void vfio_df_close(struct vfio_device_file *df)
583583

584584
lockdep_assert_held(&device->dev_set->lock);
585585

586-
vfio_assert_device_open(device);
586+
if (!vfio_assert_device_open(device))
587+
return;
587588
if (device->open_count == 1)
588589
vfio_df_device_last_close(df);
589590
device->open_count--;

0 commit comments

Comments
 (0)