* [BUG] iommufd: batch_to_domain() can map the wrong PFN at ULONG_MAX
@ 2026-12-01 11:20 James Widner
2026-12-02 2:52 ` Samiullah Khawaja
0 siblings, 1 reply; 4+ messages in thread
From: James Widner @ 2026-12-01 11:20 UTC (permalink / raw)
To: jgg, kevin.tian; +Cc: iommu
[-- Attachment #1: Type: text/plain, Size: 800 bytes --]
Hello,
I found an unsigned-wrap issue in iommufd’s `batch_to_domain()`
handling when an IOAS mapping ends at `ULONG_MAX`. Under the affected
mapping paths, the first PFN run can cover the following IOVA and
leave the final IOVA mapped to a page that was not requested by
userspace.
The attached report explains the trigger, affected paths, current and
stable A/B results, limitations, and proposed fix. I have also
attached the proposed source diff for convenient review.
I would appreciate your assessment of the finding and proposed fix.
Please retain reporter attribution to me in any resulting commit.
This finding was researched and validated with Codex Daybreak and
DeepSeek 4.1 Heretic. I have reviewed the submission and take
responsibility for it.
Thanks,
-JWW
[-- Attachment #2: PROPOSED-FIX.diff --]
[-- Type: application/octet-stream, Size: 1725 bytes --]
diff --git a/drivers/iommu/iommufd/pages.c b/drivers/iommu/iommufd/pages.c
index 70b55fe1a..749ced470 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
[-- Attachment #3: R18-MAINTAINER-REPORT.pdf --]
[-- Type: application/pdf, Size: 155104 bytes --]
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [BUG] iommufd: batch_to_domain() can map the wrong PFN at ULONG_MAX
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 ` [PATCH] iommufd: avoid unsigned wrap at the top of the address space James Widner
0 siblings, 1 reply; 4+ messages in thread
From: Samiullah Khawaja @ 2026-12-02 2:52 UTC (permalink / raw)
To: James Widner; +Cc: jgg, kevin.tian, iommu
On Tue, Dec 01, 2026 at 06:20:47AM -0500, James Widner wrote:
>Hello,
>
>I found an unsigned-wrap issue in iommufd’s `batch_to_domain()`
>handling when an IOAS mapping ends at `ULONG_MAX`. Under the affected
>mapping paths, the first PFN run can cover the following IOVA and
>leave the final IOVA mapped to a page that was not requested by
>userspace.
>
>The attached report explains the trigger, affected paths, current and
>stable A/B results, limitations, and proposed fix. I have also
>attached the proposed source diff for convenient review.
>
>I would appreciate your assessment of the finding and proposed fix.
>Please retain reporter attribution to me in any resulting commit.
>
>This finding was researched and validated with Codex Daybreak and
>DeepSeek 4.1 Heretic. I have reviewed the submission and take
>responsibility for it.
Hi,
I was working on a fix for this one and was planning to send it today.
Do you want to send the proposed fix or should I send my fix with you
mentioned in Reported-by?
Thanks,
Sami
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH] iommufd: avoid unsigned wrap at the top of the address space
2026-12-02 2:52 ` Samiullah Khawaja
@ 2026-12-02 13:14 ` James Widner
2026-12-02 18:25 ` Samiullah Khawaja
0 siblings, 1 reply; 4+ messages in thread
From: James Widner @ 2026-12-02 13:14 UTC (permalink / raw)
To: Jason Gunthorpe, Kevin Tian
Cc: James Widner, Samiullah Khawaja, Joerg Roedel, Will Deacon,
Robin Murphy, iommu, stable
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
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] iommufd: avoid unsigned wrap at the top of the address space
2026-12-02 13:14 ` [PATCH] iommufd: avoid unsigned wrap at the top of the address space James Widner
@ 2026-12-02 18:25 ` Samiullah Khawaja
0 siblings, 0 replies; 4+ messages in thread
From: Samiullah Khawaja @ 2026-12-02 18:25 UTC (permalink / raw)
To: James Widner
Cc: Jason Gunthorpe, Kevin Tian, Joerg Roedel, Will Deacon,
Robin Murphy, iommu, stable
On Wed, Dec 02, 2026 at 01:14:00PM +0000, James Widner wrote:
>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(-)
>
Reviewed-by: Samiullah Khawaja <skhawaja@google.com>
Tested-by: Samiullah Khawaja <skhawaja@google.com>
Thanks,
Sami
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-02 18:25 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH] iommufd: avoid unsigned wrap at the top of the address space James Widner
2026-12-02 18:25 ` Samiullah Khawaja
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.