Skip to contentSkip to conversation

dma-buf: fix integer overflow in fill_sg_entry() for buffers >= 8GiB

PATCH Applied Alex Mastro 4 participants 5 messages Nov 26, 2025 – Nov 28, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dma-buf: fix integer overflow in fill_sg_entry() for buffers >= 8GiB

fill_sg_entry() splits large DMA buffers into multiple scatter-gather entries, each holding up to UINT_MAX bytes. When calculating the DMA address for entries beyond the second one, the expression (i * UINT_MAX) causes integer overflow due to 32-bit arithmetic.

This manifests when the input arg length >= 8 GiB results in looping for i >= 2.

Fix by casting i to dma_addr_t before multiplication.

Fixes 3aa31a8bb11e ("dma-buf: provide phys_vec to scatter-gather mapping routine")

---
More color about how I discovered this in [1] for the commit at [2]:

[1] https://lore.kernel.org/all/[email protected]
[2] https://lore.kernel.org/all/[email protected]

---
 drivers/dma-buf/dma-buf-mapping.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/dma-buf/dma-buf-mapping.c +1 -1
index b4819811a64a..b7352e609fbd 100644
@@ -24,7 +24,7 @@ static struct scatterlist *fill_sg_entry(struct scatterlist *sgl, size_t length,
 		 * does not require the CPU list for mapping or unmapping.
 		 */
 		sg_set_page(sgl, NULL, 0, 0);
-		sg_dma_address(sgl) = addr + i * UINT_MAX;
+		sg_dma_address(sgl) = addr + (dma_addr_t)i * UINT_MAX;
 		sg_dma_len(sgl) = len;
 		sgl = sg_next(sgl);
 	}

---
base-commit: 5415d887db0e059920cb5673a32cc4d66daa280f
change-id: 20251125-dma-buf-overflow-e3253f108e36

Best regards,

Signature · 1 line
Alex Mastro <[email protected]>
Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · Alex Mastro

fill_sg_entry() splits large DMA buffers into multiple scatter-gather entries, each holding up to UINT_MAX bytes. When calculating the DMA address for entries beyond the second one, the expression (i * UINT_MAX) causes integer overflow due to 32-bit arithmetic.

This manifests when the input arg length >= 8 GiB results in looping for i >= 2.

Fix by casting i to dma_addr_t before multiplication.

Fixes 3aa31a8bb11e ("dma-buf: provide phys_vec to scatter-gather mapping routine")
Quoted 9 lines

---
More color about how I discovered this in [1] for the commit at [2]:

[1] https://lore.kernel.org/all/[email protected]
[2] https://lore.kernel.org/all/[email protected]

---
 drivers/dma-buf/dma-buf-mapping.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Thanks,

Reviewed-by on v1 1/1
#2 lore raw
In reply to Alex Mastro
Quoted 12 lines · Alex Mastro

fill_sg_entry() splits large DMA buffers into multiple scatter-gather entries, each holding up to UINT_MAX bytes. When calculating the DMA address for entries beyond the second one, the expression (i * UINT_MAX) causes integer overflow due to 32-bit arithmetic.

This manifests when the input arg length >= 8 GiB results in looping for i >= 2.

Fix by casting i to dma_addr_t before multiplication.

Fixes 3aa31a8bb11e ("dma-buf: provide phys_vec to scatter-gather mapping routine")
Quoted 8 lines

---
More color about how I discovered this in [1] for the commit at [2]:

[1] https://lore.kernel.org/all/[email protected]
[2] https://lore.kernel.org/all/[email protected]

---
 drivers/dma-buf/dma-buf-mapping.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

AlexW, can you pick this up?

Thanks,
Jason

#3 lore raw
Quoted 22 lines · On Wed, 26 Nov 2025 14:01:07 -0400 Jason Gunthorpe <[email protected]> wrote:
Quoted 12 lines · Alex Mastro

fill_sg_entry() splits large DMA buffers into multiple scatter-gather entries, each holding up to UINT_MAX bytes. When calculating the DMA address for entries beyond the second one, the expression (i * UINT_MAX) causes integer overflow due to 32-bit arithmetic.

This manifests when the input arg length >= 8 GiB results in looping for i >= 2.

Fix by casting i to dma_addr_t before multiplication.

Fixes 3aa31a8bb11e ("dma-buf: provide phys_vec to scatter-gather mapping routine")
Quoted 8 lines

---
More color about how I discovered this in [1] for the commit at [2]:

[1] https://lore.kernel.org/all/[email protected]
[2] https://lore.kernel.org/all/[email protected]

---
 drivers/dma-buf/dma-buf-mapping.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)  
Quoted 3 lines

AlexW, can you pick this up?

Yes, I'm planning to. Thanks,

Alex

#4 lore raw
In reply to Alex Mastro
Quoted 27 lines · On Tue, 25 Nov 2025 17:11:18 -0800 Alex Mastro <[email protected]> wrote:

fill_sg_entry() splits large DMA buffers into multiple scatter-gather entries, each holding up to UINT_MAX bytes. When calculating the DMA address for entries beyond the second one, the expression (i * UINT_MAX) causes integer overflow due to 32-bit arithmetic.

This manifests when the input arg length >= 8 GiB results in looping for i >= 2.

Fix by casting i to dma_addr_t before multiplication.

Fixes 3aa31a8bb11e ("dma-buf: provide phys_vec to scatter-gather mapping routine")

---
More color about how I discovered this in [1] for the commit at [2]:

[1] https://lore.kernel.org/all/[email protected]
[2] https://lore.kernel.org/all/[email protected]

---
 drivers/dma-buf/dma-buf-mapping.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/dma-buf/dma-buf-mapping.c +0 -0
index b4819811a64a..b7352e609fbd 100644

drivers/dma-buf/dma-buf-mapping.c static struct scatterlist *fill_sg_entry(struct scatterlist *sgl, size_t length, · line 30

Quoted 8 lines
drivers/dma-buf/dma-buf-mapping.c +1 -1
@@ -24,7 +24,7 @@ static struct scatterlist *fill_sg_entry(struct scatterlist *sgl, size_t length,
 		 * does not require the CPU list for mapping or unmapping.
 		 */
 		sg_set_page(sgl, NULL, 0, 0);
-		sg_dma_address(sgl) = addr + i * UINT_MAX;
+		sg_dma_address(sgl) = addr + (dma_addr_t)i * UINT_MAX;
 		sg_dma_len(sgl) = len;
 		sgl = sg_next(sgl);
 	}

Applied to vfio next branch for v6.19. Thanks,

Alex