linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 1/2] vfio: Prevent open_count decrement to negative
@ 2025-06-03 15:23 Jacob Pan
  2025-06-03 15:23 ` [PATCH v2 2/2] vfio: Fix unbalanced vfio_df_close call in no-iommu mode Jacob Pan
  2025-06-13 22:31 ` [PATCH v2 1/2] vfio: Prevent open_count decrement to negative Alex Williamson
  0 siblings, 2 replies; 14+ messages in thread
From: Jacob Pan @ 2025-06-03 15:23 UTC (permalink / raw)
  To: linux-kernel, iommu@lists.linux.dev, Alex Williamson, Liu, Yi L,
	jgg@nvidia.com, Jacob Pan
  Cc: Zhang Yu, Easwar Hariharan, Saurabh Sengar

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.

Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Reviewed-by: Yi Liu <yi.l.liu@intel.com>
Signed-off-by: Jacob Pan <jacob.pan@linux.microsoft.com>
---
v2: Added Reviewed-by tags
---
 drivers/vfio/vfio_main.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
index 1fd261efc582..5046cae05222 100644
--- a/drivers/vfio/vfio_main.c
+++ b/drivers/vfio/vfio_main.c
@@ -583,7 +583,8 @@ void vfio_df_close(struct vfio_device_file *df)
 
 	lockdep_assert_held(&device->dev_set->lock);
 
-	vfio_assert_device_open(device);
+	if (!vfio_assert_device_open(device))
+		return;
 	if (device->open_count == 1)
 		vfio_df_device_last_close(df);
 	device->open_count--;
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2025-06-18 23:26 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-03 15:23 [PATCH v2 1/2] vfio: Prevent open_count decrement to negative Jacob Pan
2025-06-03 15:23 ` [PATCH v2 2/2] vfio: Fix unbalanced vfio_df_close call in no-iommu mode Jacob Pan
2025-06-13 22:31   ` Alex Williamson
2025-06-14  0:15     ` Jason Gunthorpe
2025-06-16 14:47       ` Alex Williamson
2025-06-16 15:34         ` Jason Gunthorpe
2025-06-16 19:40           ` Alex Williamson
2025-06-16 20:05             ` Jason Gunthorpe
2025-06-18 23:11               ` Jacob Pan
2025-06-18 23:25                 ` Jason Gunthorpe
2025-06-13 22:31 ` [PATCH v2 1/2] vfio: Prevent open_count decrement to negative Alex Williamson
2025-06-14  0:09   ` Jason Gunthorpe
2025-06-16 14:40     ` Alex Williamson
2025-06-18 23:08       ` Jacob Pan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).