The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH v2] iommufd: only hold references to files memfd_pin_folios() can pin
@ 2026-11-17  1:32 Jiahao Xu
  2026-11-17  1:58 ` Samiullah Khawaja
  0 siblings, 1 reply; 2+ messages in thread
From: Jiahao Xu @ 2026-11-17  1:32 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Kevin Tian, Joerg Roedel (AMD), Will Deacon, Robin Murphy, iommu,
	linux-kernel, Christian Brauner, syzbot+7666ed2c3d42196bf7e3,
	Jiahao Xu

iopt_map_file_pages() stores a reference to the file passed to
IOMMU_IOAS_MAP_FILE in the iopt_pages of the new mapping:

	iopt_map_file_pages()
	  iopt_alloc_file_pages()
	    pages->file = get_file(file)

iopt_release_pages() drops that reference only when the iopt_pages is
destroyed, which for a file mapping happens while the iommufd file is
being torn down.  Nothing restricts which file can be passed, so
userspace can map the iommufd file itself, or a vfio device fd bound to
the same iommufd, both of which own an iommufd_ctx reference.  The
reference graph then contains a cycle and the file, the iopt_pages, the
IOAS and the iommufd_ctx are all leaked; kmemleak reports it as
"memory leak in path_openat" with the holder taken through
iopt_map_file_pages().

Reverse the test instead of trying to enumerate the files that own an
iommufd_ctx: accept only the files that memfd_pin_folios() is able to
pin, which is exactly the set of files the mapping can be built from.
memfd_pin_folios() already enforces

	if (!shmem_file(memfd) && !is_file_hugepages(memfd))
		return -EINVAL;

and both predicates are inline helpers, so the same check can run here
before the reference is handed to iopt_pages.  dma-bufs are unaffected:
they are taken by the dma_buf_get() branch above and never reach the
file branch, and iopt_pages holds the dma_buf there, not a struct file.

Fixes: f4986a72d6e4 ("iommufd: Add IOMMU_IOAS_MAP_FILE")
Reported-by: syzbot+7666ed2c3d42196bf7e3@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=7666ed2c3d42196bf7e3
Link: https://lore.kernel.org/all/20261112105945.285526-1-xujiahao@cmss.chinamobile.com/
Signed-off-by: Jiahao Xu <xujiahao@cmss.chinamobile.com>
---
v2:
 - Replace the iommufd_fops blocklist with the allowlist of files that
   memfd_pin_folios() can pin, as Jason asked for.  This covers a vfio
   device fd bound to the same iommufd, and any other fd that owns an
   iommufd_ctx, instead of only the iommufd file itself.
 - dma-buf mappings are untouched: they are taken by the dma_buf_get()
   branch and iopt_pages holds the dma_buf there, not a struct file.

 drivers/iommu/iommufd/io_pagetable.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/drivers/iommu/iommufd/io_pagetable.c b/drivers/iommu/iommufd/io_pagetable.c
index 5ac736cf5..4d10b2253 100644
--- a/drivers/iommu/iommufd/io_pagetable.c
+++ b/drivers/iommu/iommufd/io_pagetable.c
@@ -15,7 +15,9 @@
 #include <linux/iommu.h>
 #include <linux/iommufd.h>
 #include <linux/lockdep.h>
+#include <linux/hugetlb.h>
 #include <linux/sched/mm.h>
+#include <linux/shmem_fs.h>
 #include <linux/slab.h>
 #include <uapi/linux/iommufd.h>
 
@@ -515,6 +517,18 @@ int iopt_map_file_pages(struct iommufd_ctx *ictx, struct io_pagetable *iopt,
 		if (!file)
 			return -EBADF;
 
+		/*
+		 * iopt_pages keeps a reference to the file until the mapping is
+		 * destroyed, so only accept the files memfd_pin_folios() is able
+		 * to pin.  Any other fd, in particular one that itself owns an
+		 * iommufd_ctx (iommufd, vfio, ...), would put the file into a
+		 * reference cycle with this iommufd and never be released.
+		 */
+		if (!shmem_file(file) && !is_file_hugepages(file)) {
+			fput(file);
+			return -EINVAL;
+		}
+
 		pages = iopt_alloc_file_pages(file, start_byte, start, length,
 					      iommu_prot & IOMMU_WRITE);
 		fput(file);
-- 
2.43.0



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

* Re: [PATCH v2] iommufd: only hold references to files memfd_pin_folios() can pin
  2026-11-17  1:32 [PATCH v2] iommufd: only hold references to files memfd_pin_folios() can pin Jiahao Xu
@ 2026-11-17  1:58 ` Samiullah Khawaja
  0 siblings, 0 replies; 2+ messages in thread
From: Samiullah Khawaja @ 2026-11-17  1:58 UTC (permalink / raw)
  To: Jiahao Xu
  Cc: Jason Gunthorpe, Kevin Tian, Joerg Roedel (AMD), Will Deacon,
	Robin Murphy, iommu, linux-kernel, Christian Brauner,
	syzbot+7666ed2c3d42196bf7e3

On Tue, Nov 17, 2026 at 09:32:44AM +0800, Jiahao Xu wrote:
>iopt_map_file_pages() stores a reference to the file passed to
>IOMMU_IOAS_MAP_FILE in the iopt_pages of the new mapping:
>
>	iopt_map_file_pages()
>	  iopt_alloc_file_pages()
>	    pages->file = get_file(file)
>
>iopt_release_pages() drops that reference only when the iopt_pages is
>destroyed, which for a file mapping happens while the iommufd file is
>being torn down.  Nothing restricts which file can be passed, so
>userspace can map the iommufd file itself, or a vfio device fd bound to
>the same iommufd, both of which own an iommufd_ctx reference.  The
>reference graph then contains a cycle and the file, the iopt_pages, the
>IOAS and the iommufd_ctx are all leaked; kmemleak reports it as
>"memory leak in path_openat" with the holder taken through
>iopt_map_file_pages().
>
>Reverse the test instead of trying to enumerate the files that own an
>iommufd_ctx: accept only the files that memfd_pin_folios() is able to
>pin, which is exactly the set of files the mapping can be built from.
>memfd_pin_folios() already enforces
>
>	if (!shmem_file(memfd) && !is_file_hugepages(memfd))
>		return -EINVAL;
>
>and both predicates are inline helpers, so the same check can run here
>before the reference is handed to iopt_pages.  dma-bufs are unaffected:
>they are taken by the dma_buf_get() branch above and never reach the
>file branch, and iopt_pages holds the dma_buf there, not a struct file.
>
>Fixes: f4986a72d6e4 ("iommufd: Add IOMMU_IOAS_MAP_FILE")
>Reported-by: syzbot+7666ed2c3d42196bf7e3@syzkaller.appspotmail.com
>Closes: https://syzkaller.appspot.com/bug?extid=7666ed2c3d42196bf7e3
>Link: https://lore.kernel.org/all/20261112105945.285526-1-xujiahao@cmss.chinamobile.com/
>Signed-off-by: Jiahao Xu <xujiahao@cmss.chinamobile.com>
>---
>v2:
> - Replace the iommufd_fops blocklist with the allowlist of files that
>   memfd_pin_folios() can pin, as Jason asked for.  This covers a vfio
>   device fd bound to the same iommufd, and any other fd that owns an
>   iommufd_ctx, instead of only the iommufd file itself.
> - dma-buf mappings are untouched: they are taken by the dma_buf_get()
>   branch and iopt_pages holds the dma_buf there, not a struct file.
>
> drivers/iommu/iommufd/io_pagetable.c | 14 ++++++++++++++
> 1 file changed, 14 insertions(+)
>
>diff --git a/drivers/iommu/iommufd/io_pagetable.c b/drivers/iommu/iommufd/io_pagetable.c
>index 5ac736cf5..4d10b2253 100644
>--- a/drivers/iommu/iommufd/io_pagetable.c
>+++ b/drivers/iommu/iommufd/io_pagetable.c
>@@ -15,7 +15,9 @@
> #include <linux/iommu.h>
> #include <linux/iommufd.h>
> #include <linux/lockdep.h>
>+#include <linux/hugetlb.h>
> #include <linux/sched/mm.h>
>+#include <linux/shmem_fs.h>
> #include <linux/slab.h>
> #include <uapi/linux/iommufd.h>
>
>@@ -515,6 +517,18 @@ int iopt_map_file_pages(struct iommufd_ctx *ictx, struct io_pagetable *iopt,
> 		if (!file)
> 			return -EBADF;
>
>+		/*
>+		 * iopt_pages keeps a reference to the file until the mapping is
>+		 * destroyed, so only accept the files memfd_pin_folios() is able
>+		 * to pin.  Any other fd, in particular one that itself owns an
>+		 * iommufd_ctx (iommufd, vfio, ...), would put the file into a
>+		 * reference cycle with this iommufd and never be released.
>+		 */
>+		if (!shmem_file(file) && !is_file_hugepages(file)) {
>+			fput(file);
>+			return -EINVAL;
>+		}
>+
> 		pages = iopt_alloc_file_pages(file, start_byte, start, length,
> 					      iommu_prot & IOMMU_WRITE);
> 		fput(file);
>-- 
>2.43.0
>

Reviewed-by: Samiullah Khawaja <skhawaja@google.com>

Thanks,
Sami

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

end of thread, other threads:[~2026-11-17  1:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17  1:32 [PATCH v2] iommufd: only hold references to files memfd_pin_folios() can pin Jiahao Xu
2026-11-17  1:58 ` Samiullah Khawaja

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