Kernel KVM virtualization development
 help / color / mirror / Atom feed
* [PATCH] vfio/container: hold group_lock across backend ioctl passthrough
@ 2026-11-23 21:10 Jeff Barnes
  2026-12-04 23:16 ` Alex Williamson
  0 siblings, 1 reply; 2+ messages in thread
From: Jeff Barnes @ 2026-11-23 21:10 UTC (permalink / raw)
  To: Alex Williamson; +Cc: kvm, stable

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")
Cc: stable@vger.kernel.org
Signed-off-by: Jeff Barnes <jeffbarnes@linux.microsoft.com>
---
 drivers/vfio/container.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/vfio/container.c b/drivers/vfio/container.c
index 003281dbf8bc..38ef3b3dd0de 100644
--- a/drivers/vfio/container.c
+++ b/drivers/vfio/container.c
@@ -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;
-- 
2.43.0


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

* Re: [PATCH] vfio/container: hold group_lock across backend ioctl passthrough
  2026-11-23 21:10 [PATCH] vfio/container: hold group_lock across backend ioctl passthrough Jeff Barnes
@ 2026-12-04 23:16 ` Alex Williamson
  0 siblings, 0 replies; 2+ messages in thread
From: Alex Williamson @ 2026-12-04 23:16 UTC (permalink / raw)
  To: Jeff Barnes; +Cc: kvm, stable, alex

On Mon, 23 Nov 2026 16:10:25 -0500
Jeff Barnes <jeffbarnes@linux.microsoft.com> wrote:

> 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")
> Cc: stable@vger.kernel.org
> Signed-off-by: Jeff Barnes <jeffbarnes@linux.microsoft.com>
> ---
>  drivers/vfio/container.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/drivers/vfio/container.c b/drivers/vfio/container.c
> index 003281dbf8bc..38ef3b3dd0de 100644
> --- a/drivers/vfio/container.c
> +++ b/drivers/vfio/container.c
> @@ -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;

Applied to vfio next branch for v7.5.  Thanks,

Alex

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

end of thread, other threads:[~2026-12-04 23:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 21:10 [PATCH] vfio/container: hold group_lock across backend ioctl passthrough Jeff Barnes
2026-12-04 23:16 ` Alex Williamson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox