From: James Widner <jameswidner2@gmail.com>
To: Jason Gunthorpe <jgg@ziepe.ca>, Kevin Tian <kevin.tian@intel.com>
Cc: James Widner <jameswidner2@gmail.com>,
Samiullah Khawaja <skhawaja@google.com>,
Joerg Roedel <joro@8bytes.org>, Will Deacon <will@kernel.org>,
Robin Murphy <robin.murphy@arm.com>,
iommu@lists.linux.dev, stable@vger.kernel.org
Subject: [PATCH] iommufd: avoid unsigned wrap at the top of the address space
Date: Wed, 2 Dec 2026 13:14:00 +0000 [thread overview]
Message-ID: <20261202131400.2295591-1-jameswidner2@gmail.com> (raw)
In-Reply-To: <aw-G094ik8fz3XSO@google.com>
batch_to_domain() computes a PFN run's exclusive IOVA end using
last_iova + 1 even though last_iova is the inclusive area end. When an
area ends at ULONG_MAX this wraps to zero. The first PFN run can then be
extended over the remaining area while later runs are submitted with a
zero length. On paths that accept the zero-length continuation, the map
succeeds with translations that do not match the pinned PFN runs.
Teardown reads PFNs back from the domain. It may consequently put the
unintended page installed by the extended first run while retaining the
pin on the intended later page.
Compute each run size from the remaining inclusive distance instead. Stop
after mapping the terminal byte so the IOVA cursor is never advanced past
ULONG_MAX.
The final change passed an instrumented current-source mock-domain A/B. A
semantically equivalent Linux 6.12 backport passed a separate emulated
device-DMA A/B. One million randomized arithmetic-model cases also passed.
This finding, its tests, this fix, and this changelog were prepared with
substantial assistance from language-model-based coding tools.
Fixes: f394576eb11d ("iommufd: PFN handling for iopt_pages")
Cc: stable@vger.kernel.org
Signed-off-by: James Widner <jameswidner2@gmail.com>
---
drivers/iommu/iommufd/pages.c | 20 +++++++++++---------
1 file changed, 11 insertions(+), 9 deletions(-)
diff --git a/drivers/iommu/iommufd/pages.c b/drivers/iommu/iommufd/pages.c
index fcdbde007a9a..e867cb824164 100644
--- a/drivers/iommu/iommufd/pages.c
+++ b/drivers/iommu/iommufd/pages.c
@@ -512,7 +512,7 @@ static int batch_to_domain(struct pfn_batch *batch, struct iommu_domain *domain,
int iommu_prot = area->iommu_prot;
unsigned int page_offset = 0;
unsigned long start_iova;
- unsigned long next_iova;
+ unsigned long size;
unsigned int cur = 0;
unsigned long iova;
int rc;
@@ -525,25 +525,27 @@ static int batch_to_domain(struct pfn_batch *batch, struct iommu_domain *domain,
/* The first index might be a partial page */
if (start_index == iopt_area_index(area))
page_offset = area->page_offset;
- next_iova = iova = start_iova =
- iopt_area_index_to_iova(area, start_index);
+ start_iova = iopt_area_index_to_iova(area, start_index);
+ iova = start_iova;
while (cur < batch->end) {
- next_iova = min(last_iova + 1,
- next_iova + batch->npfns[cur] * PAGE_SIZE -
- page_offset);
+ size = batch->npfns[cur] * PAGE_SIZE - page_offset;
+ /* Avoid representing ULONG_MAX + 1 as an exclusive end. */
+ size = min(size - 1, last_iova - iova) + 1;
if (disable_large_pages)
rc = batch_iommu_map_small(
domain, iova,
PFN_PHYS(batch->pfns[cur]) + page_offset,
- next_iova - iova, iommu_prot);
+ size, iommu_prot);
else
rc = iommu_map(domain, iova,
PFN_PHYS(batch->pfns[cur]) + page_offset,
- next_iova - iova, iommu_prot,
+ size, iommu_prot,
GFP_KERNEL_ACCOUNT);
if (rc)
goto err_unmap;
- iova = next_iova;
+ if (size - 1 == last_iova - iova)
+ return 0;
+ iova += size;
page_offset = 0;
cur++;
}
--
2.43.0
prev parent reply other threads:[~2026-12-02 13:14 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 11:20 [BUG] iommufd: batch_to_domain() can map the wrong PFN at ULONG_MAX James Widner
2026-12-02 2:52 ` Samiullah Khawaja
2026-12-02 13:14 ` James Widner [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261202131400.2295591-1-jameswidner2@gmail.com \
--to=jameswidner2@gmail.com \
--cc=iommu@lists.linux.dev \
--cc=jgg@ziepe.ca \
--cc=joro@8bytes.org \
--cc=kevin.tian@intel.com \
--cc=robin.murphy@arm.com \
--cc=skhawaja@google.com \
--cc=stable@vger.kernel.org \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.