From: Robin Murphy <robin.murphy@arm.com>
To: Krzysztof Karas <krzysztof.karas@intel.com>,
intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org,
iommu@lists.linux.dev
Cc: "Andi Shyti" <andi.shyti@linux.intel.com>,
"Jason Gunthorpe" <jgg@ziepe.ca>,
"Michał Grzelak" <michal.grzelak@intel.com>,
"Janusz Krzysztofik" <janusz.krzysztofik@linux.intel.com>,
"Sebastian Brzezinka" <sebastian.brzezinka@intel.com>,
"Krzysztof Niemiec" <krzysztof.niemiec@intel.com>
Subject: Re: [PATCH v5 3/6] iommu/dma: Catch scatterlist length overflows
Date: Tue, 18 Aug 2026 13:10:52 +0100 [thread overview]
Message-ID: <8c9066f6-e460-458b-bad4-26d441a742ab@arm.com> (raw)
In-Reply-To: <20260817095648.2438192-4-krzysztof.karas@intel.com>
On 17/08/2026 10:56 am, Krzysztof Karas wrote:
> It is possible, when a very large mapping uses only one
> scatterlist, that padding overflows scatterlist's length field.
> This results in:
> 1) silently wrapping the value
> 2) smaller than desired mappings produced by iommu_map_sg
> 3) leaving mapped bytes in memory (no iommu_unmap)
>
> Address this issue by adding overflow detection for scatterlist
> length field.
>
> Fixes: 809eac54cdd6 ("iommu/dma: Implement scatterlist segment merging")
> Signed-off-by: Krzysztof Karas <krzysztof.karas@intel.com>
> ---
> v5 (sashiko review):
> * Included overflow checking on addition, previously silently
> omitted inside iova_align arguments.
> * Added check for zero-mapping via temporary variable comparison.
> * Added overflows_type on s->length to satisfy 64 bit systems.
>
> drivers/iommu/dma-iommu.c | 32 ++++++++++++++++++++++++++++++--
> 1 file changed, 30 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c
> index 9abaec0703ef..0bd5b13f006f 100644
> --- a/drivers/iommu/dma-iommu.c
> +++ b/drivers/iommu/dma-iommu.c
> @@ -21,6 +21,7 @@
> #include <linux/iommu-dma.h>
> #include <linux/iova.h>
> #include <linux/irq.h>
> +#include <linux/limits.h>
> #include <linux/list_sort.h>
> #include <linux/memremap.h>
> #include <linux/mm.h>
> @@ -1445,6 +1446,7 @@ int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
> for_each_sg(sg, s, nents, i) {
> size_t s_iova_off = iova_offset(iovad, s->offset);
> size_t s_length = s->length;
> + size_t s_length_tmp;
> size_t pad_len = (mask - iova_len + 1) & mask;
>
> switch (pci_p2pdma_state(&p2pdma_state, dev, sg_page(s))) {
> @@ -1476,7 +1478,22 @@ int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
> sg_dma_address(s) = s_iova_off;
> sg_dma_len(s) = s_length;
> s->offset -= s_iova_off;
> - s_length = iova_align(iovad, s_length + s_iova_off);
> +
> + if (check_add_overflow(s_length, s_iova_off, &s_length_tmp)) {
> + ret = -EOVERFLOW;
> + goto out_restore_sg;
> + }
This is unnecessary - s_iova_off will always be <= sg->offset, and since
the maximum possible segment boundary mask is 32 bits, if sg->offset +
sg->length would overflow then by definition it must cross a segment
boundary, so the segment is malformed to begin with and all bets are off.
> + s_length = iova_align(iovad, s_length_tmp);
> +
> + if (s_length_tmp != 0 && s_length == 0) {
> + ret = -EOVERFLOW;
> + goto out_restore_sg;
> + }
Similarly, a 0-length segment wouldn't be valid either (consider in the
simple case it would end up with sg_dma_len() == 0 which means "end of
mapping")
Thanks,
Robin.
> +
> + if (overflows_type(s_length, s->length)) {
> + ret = -EOVERFLOW;
> + goto out_restore_sg;
> + }
> s->length = s_length;
>
> /*
> @@ -1493,7 +1510,18 @@ int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
> * time through here (i.e. before it has a meaningful value).
> */
> if (pad_len && pad_len < s_length - 1) {
> - prev->length += pad_len;
> + unsigned int new_prev_len;
> + /*
> + * For large mappings spanning multiple GBs we
> + * may not be able to fit all needed padding into
> + * sg->length.
> + */
> + if (check_add_overflow(prev->length, pad_len, &new_prev_len)) {
> + ret = -EOVERFLOW;
> + goto out_restore_sg;
> + }
> +
> + prev->length = new_prev_len;
> iova_len += pad_len;
> }
>
next prev parent reply other threads:[~2026-08-18 12:11 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-17 9:56 [PATCH v5 0/6] drivers: Improve memory management for large object allocations when i915/shmem is used with iommu Krzysztof Karas
2026-08-17 9:56 ` [PATCH v5 1/6] drm/i915/gem: Count mapped pages in a folio Krzysztof Karas
2026-08-19 11:56 ` Andi Shyti
2026-08-19 11:56 ` Janusz Krzysztofik
2026-08-17 9:56 ` [PATCH 2/6] drm/i915/gem: Free sg table on failure to acquire second folio Krzysztof Karas
2026-08-17 9:56 ` [PATCH v5 3/6] iommu/dma: Catch scatterlist length overflows Krzysztof Karas
2026-08-17 10:11 ` sashiko-bot
2026-08-18 12:10 ` Robin Murphy [this message]
2026-08-17 9:56 ` [PATCH v5 4/6] drm/i915/gem: Pull out size validation into a separate function Krzysztof Karas
2026-08-17 9:56 ` [PATCH v5 5/6] drm/i915/gem: Read and shrink memory in " Krzysztof Karas
2026-08-17 9:56 ` [PATCH v5 6/6] drm/i915/gem: Remove iterator and use while loop Krzysztof Karas
2026-08-17 15:01 ` ✓ i915.CI.BAT: success for drivers: Improve memory management for large object allocations when i915/shmem is used with iommu Patchwork
2026-08-18 3:30 ` ✓ i915.CI.Full: " Patchwork
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=8c9066f6-e460-458b-bad4-26d441a742ab@arm.com \
--to=robin.murphy@arm.com \
--cc=andi.shyti@linux.intel.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=intel-gfx@lists.freedesktop.org \
--cc=iommu@lists.linux.dev \
--cc=janusz.krzysztofik@linux.intel.com \
--cc=jgg@ziepe.ca \
--cc=krzysztof.karas@intel.com \
--cc=krzysztof.niemiec@intel.com \
--cc=michal.grzelak@intel.com \
--cc=sebastian.brzezinka@intel.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox