* [PATCH] dma-buf: fix integer overflow in fill_sg_entry() for buffers >= 8GiB
@ 2025-11-26 1:11 Alex Mastro
2025-11-26 7:18 ` Leon Romanovsky
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Alex Mastro @ 2025-11-26 1:11 UTC (permalink / raw)
To: Sumit Semwal, Christian König, Alex Williamson
Cc: Leon Romanovsky, Jason Gunthorpe, Kevin Tian, Nicolin Chen,
linux-media, dri-devel, linaro-mm-sig, linux-kernel, kvm,
Ankit Agrawal, 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")
Signed-off-by: Alex Mastro <amastro@fb.com>
---
More color about how I discovered this in [1] for the commit at [2]:
[1] https://lore.kernel.org/all/aSZHO6otK0Heh+Qj@devgpu015.cco6.facebook.com
[2] https://lore.kernel.org/all/20251120-dmabuf-vfio-v9-6-d7f71607f371@nvidia.com
---
drivers/dma-buf/dma-buf-mapping.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/dma-buf/dma-buf-mapping.c b/drivers/dma-buf/dma-buf-mapping.c
index b4819811a64a..b7352e609fbd 100644
--- a/drivers/dma-buf/dma-buf-mapping.c
+++ b/drivers/dma-buf/dma-buf-mapping.c
@@ -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,
--
Alex Mastro <amastro@fb.com>
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] dma-buf: fix integer overflow in fill_sg_entry() for buffers >= 8GiB
2025-11-26 1:11 [PATCH] dma-buf: fix integer overflow in fill_sg_entry() for buffers >= 8GiB Alex Mastro
@ 2025-11-26 7:18 ` Leon Romanovsky
2025-11-26 18:01 ` Jason Gunthorpe
2025-11-28 19:44 ` Alex Williamson
2 siblings, 0 replies; 5+ messages in thread
From: Leon Romanovsky @ 2025-11-26 7:18 UTC (permalink / raw)
To: Alex Mastro
Cc: Sumit Semwal, Christian König, Alex Williamson,
Jason Gunthorpe, Kevin Tian, Nicolin Chen, linux-media, dri-devel,
linaro-mm-sig, linux-kernel, kvm, Ankit Agrawal
On Tue, Nov 25, 2025 at 05:11:18PM -0800, Alex Mastro 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")
> Signed-off-by: Alex Mastro <amastro@fb.com>
> ---
> More color about how I discovered this in [1] for the commit at [2]:
>
> [1] https://lore.kernel.org/all/aSZHO6otK0Heh+Qj@devgpu015.cco6.facebook.com
> [2] https://lore.kernel.org/all/20251120-dmabuf-vfio-v9-6-d7f71607f371@nvidia.com
> ---
> drivers/dma-buf/dma-buf-mapping.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
Thanks,
Reviewed-by: Leon Romanovsky <leon@kernel.org>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] dma-buf: fix integer overflow in fill_sg_entry() for buffers >= 8GiB
2025-11-26 1:11 [PATCH] dma-buf: fix integer overflow in fill_sg_entry() for buffers >= 8GiB Alex Mastro
2025-11-26 7:18 ` Leon Romanovsky
@ 2025-11-26 18:01 ` Jason Gunthorpe
2025-11-26 18:55 ` Alex Williamson
2025-11-28 19:44 ` Alex Williamson
2 siblings, 1 reply; 5+ messages in thread
From: Jason Gunthorpe @ 2025-11-26 18:01 UTC (permalink / raw)
To: Alex Mastro, Alex Williamson
Cc: Sumit Semwal, Christian König, Leon Romanovsky, Kevin Tian,
Nicolin Chen, linux-media, dri-devel, linaro-mm-sig, linux-kernel,
kvm, Ankit Agrawal
On Tue, Nov 25, 2025 at 05:11:18PM -0800, Alex Mastro 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")
> Signed-off-by: Alex Mastro <amastro@fb.com>
> ---
> More color about how I discovered this in [1] for the commit at [2]:
>
> [1] https://lore.kernel.org/all/aSZHO6otK0Heh+Qj@devgpu015.cco6.facebook.com
> [2] https://lore.kernel.org/all/20251120-dmabuf-vfio-v9-6-d7f71607f371@nvidia.com
> ---
> drivers/dma-buf/dma-buf-mapping.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
AlexW, can you pick this up?
Thanks,
Jason
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] dma-buf: fix integer overflow in fill_sg_entry() for buffers >= 8GiB
2025-11-26 18:01 ` Jason Gunthorpe
@ 2025-11-26 18:55 ` Alex Williamson
0 siblings, 0 replies; 5+ messages in thread
From: Alex Williamson @ 2025-11-26 18:55 UTC (permalink / raw)
To: Jason Gunthorpe
Cc: Alex Mastro, Sumit Semwal, Christian König, Leon Romanovsky,
Kevin Tian, Nicolin Chen, linux-media, dri-devel, linaro-mm-sig,
linux-kernel, kvm, Ankit Agrawal
On Wed, 26 Nov 2025 14:01:07 -0400
Jason Gunthorpe <jgg@ziepe.ca> wrote:
> On Tue, Nov 25, 2025 at 05:11:18PM -0800, Alex Mastro 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")
> > Signed-off-by: Alex Mastro <amastro@fb.com>
> > ---
> > More color about how I discovered this in [1] for the commit at [2]:
> >
> > [1] https://lore.kernel.org/all/aSZHO6otK0Heh+Qj@devgpu015.cco6.facebook.com
> > [2] https://lore.kernel.org/all/20251120-dmabuf-vfio-v9-6-d7f71607f371@nvidia.com
> > ---
> > drivers/dma-buf/dma-buf-mapping.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
>
> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
>
> AlexW, can you pick this up?
Yes, I'm planning to. Thanks,
Alex
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] dma-buf: fix integer overflow in fill_sg_entry() for buffers >= 8GiB
2025-11-26 1:11 [PATCH] dma-buf: fix integer overflow in fill_sg_entry() for buffers >= 8GiB Alex Mastro
2025-11-26 7:18 ` Leon Romanovsky
2025-11-26 18:01 ` Jason Gunthorpe
@ 2025-11-28 19:44 ` Alex Williamson
2 siblings, 0 replies; 5+ messages in thread
From: Alex Williamson @ 2025-11-28 19:44 UTC (permalink / raw)
To: Alex Mastro
Cc: Sumit Semwal, Christian König, Leon Romanovsky,
Jason Gunthorpe, Kevin Tian, Nicolin Chen, linux-media, dri-devel,
linaro-mm-sig, linux-kernel, kvm, Ankit Agrawal
On Tue, 25 Nov 2025 17:11:18 -0800
Alex Mastro <amastro@fb.com> 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")
> Signed-off-by: Alex Mastro <amastro@fb.com>
> ---
> More color about how I discovered this in [1] for the commit at [2]:
>
> [1] https://lore.kernel.org/all/aSZHO6otK0Heh+Qj@devgpu015.cco6.facebook.com
> [2] https://lore.kernel.org/all/20251120-dmabuf-vfio-v9-6-d7f71607f371@nvidia.com
> ---
> drivers/dma-buf/dma-buf-mapping.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/dma-buf/dma-buf-mapping.c b/drivers/dma-buf/dma-buf-mapping.c
> index b4819811a64a..b7352e609fbd 100644
> --- a/drivers/dma-buf/dma-buf-mapping.c
> +++ b/drivers/dma-buf/dma-buf-mapping.c
> @@ -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
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2025-11-28 19:45 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-26 1:11 [PATCH] dma-buf: fix integer overflow in fill_sg_entry() for buffers >= 8GiB Alex Mastro
2025-11-26 7:18 ` Leon Romanovsky
2025-11-26 18:01 ` Jason Gunthorpe
2025-11-26 18:55 ` Alex Williamson
2025-11-28 19:44 ` Alex Williamson
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox