Skip to contentSkip to conversation

vfio/container: hold group_lock across backend ioctl passthrough

PATCH New Jeff Barnes 1 participant 1 message Nov 23

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

PATCH 1/1 vfio/container: hold group_lock across backend ioctl passthrough

vfio_fops_unl_ioctl() forwards unrecognized ioctls to the IOMMU backend by reading container->iommu_driver and container->iommu_data and calling driver->ops->ioctl() without holding container->group_lock.

When the last group is detached, vfio_group_detach_container() takes group_lock for write, releases the backend and clears both pointers. An ioctl running concurrently on the container fd can therefore use backend state that is being torn down. With the Type1 backend, a VFIO_IOMMU_GET_INFO racing with the last group detach walks iommu-> iova_list while vfio_iommu_type1_release() frees it, and faults on a poisoned list pointer (dead000000000100).

The other container entry points that use the backend, namely VFIO_CHECK_EXTENSION and VFIO_SET_IOMMU, already take group_lock. Do the same for the passthrough path and hold it for read across the backend call. This is consistent with the existing lock order, as backends only take their own locks (e.g. iommu->lock) below group_lock.

This patch was developed with the assistance of GitHub Copilot CLI (Claude Sonnet 5.5), which helped analyze the race, draft the fix and this description, and build-check the result. The author reviewed the change and tested it on a downstream 6.6 kernel.

Fixes cba3345cc494 ("vfio: VFIO core")
---
 drivers/vfio/container.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/vfio/container.c +3 -0
index 003281dbf8bc..38ef3b3dd0de 100644
@@ -346,11 +346,14 @@ static long vfio_fops_unl_ioctl(struct file *filep,
 		ret = vfio_ioctl_set_iommu(container, arg);
 		break;
 	default:
+		/* Keep the backend alive until the ioctl completes. */
+		down_read(&container->group_lock);
 		driver = container->iommu_driver;
 		data = container->iommu_data;
 
 		if (driver) /* passthrough all unrecognized ioctls */
 			ret = driver->ops->ioctl(data, cmd, arg);
+		up_read(&container->group_lock);
 	}
 
 	return ret;
Signature · 1 line
2.43.0