Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] nvme-pci: validate dmabuf size and sg_table bounds in nvme_dma_buf_io_map()
@ 2026-12-01  6:39 Bilal Hashim
  2026-12-01  6:44 ` sashiko-bot
  2026-12-01  7:58 ` Christoph Hellwig
  0 siblings, 2 replies; 3+ messages in thread
From: Bilal Hashim @ 2026-12-01  6:39 UTC (permalink / raw)
  To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
	Pavel Begunkov
  Cc: linux-nvme, linux-kernel

In nvme_dma_buf_io_map():

1. ctx->dmabuf->size (size_t) is divided by NVME_CTRL_PAGE_SIZE and
   stored into a 32-bit unsigned nr_entries without checking for zero or
   overflow, and nvme_rq_setup_dmabuf_map() indexes the buffer via the
   32-bit bio->bi_iter.bi_offset. Reject empty dma-bufs or dma-bufs
   larger than UINT_MAX.
2. When populating map->dma_list[] from the scatter-gather table, there
   is no bounds check against nr_entries. If the sg_table total DMA
   length exceeds nr_entries * NVME_CTRL_PAGE_SIZE, map->dma_list[i++]
   overflows the allocated buffer. Conversely, if the sg_table total DMA
   length is smaller than nr_entries * NVME_CTRL_PAGE_SIZE, trailing
   entries in map->dma_list[] remain uninitialized (kvmalloc_flex() does
   not zero-initialize) and could be programmed as arbitrary NVMe DMA
   addresses.

Validate ctx->dmabuf->size before computing nr_entries, bounds-check
each write into map->dma_list[], and verify that the entire dma_list was
populated.

Found by manual code inspection.

Signed-off-by: Bilal Hashim <AeeSec@126.com>
---
 drivers/nvme/host/pci-dmabuf.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/drivers/nvme/host/pci-dmabuf.c b/drivers/nvme/host/pci-dmabuf.c
index 161e4ff..3bba66d 100644
--- a/drivers/nvme/host/pci-dmabuf.c
+++ b/drivers/nvme/host/pci-dmabuf.c
@@ -22,20 +22,23 @@ static const struct dma_buf_attach_ops nvme_dmabuf_importer_ops = {
 
 static struct dma_buf_io_map *nvme_dma_buf_io_map(struct dma_buf_io_ctx *ctx)
 {
-	unsigned nr_entries = ctx->dmabuf->size / NVME_CTRL_PAGE_SIZE;
 	struct nvme_dma_buf_io_ctx *nvme_ctx = ctx->dev_priv;
 	struct dma_buf_attachment *attach = nvme_ctx->attach;
 	unsigned long tmp, i = 0;
 	struct nvme_dmabuf_map *map;
 	struct scatterlist *sg;
 	struct sg_table *sgt;
+	unsigned int nr_entries;
 	int ret;
 
 	dma_resv_assert_held(ctx->dmabuf->resv);
 
-	if (ctx->dmabuf->size % NVME_CTRL_PAGE_SIZE)
+	if (!ctx->dmabuf->size || ctx->dmabuf->size > UINT_MAX ||
+	    ctx->dmabuf->size % NVME_CTRL_PAGE_SIZE)
 		return ERR_PTR(-EINVAL);
 
+	nr_entries = ctx->dmabuf->size / NVME_CTRL_PAGE_SIZE;
+
 	map = kvmalloc_flex(*map, dma_list, nr_entries);
 	if (!map)
 		return ERR_PTR(-ENOMEM);
@@ -58,12 +61,21 @@ static struct dma_buf_io_map *nvme_dma_buf_io_map(struct dma_buf_io_ctx *ctx)
 			goto err;
 		}
 		while (sg_len) {
+			if (i >= nr_entries) {
+				ret = -EINVAL;
+				goto err;
+			}
 			map->dma_list[i++] = dma_addr;
 			dma_addr += NVME_CTRL_PAGE_SIZE;
 			sg_len -= NVME_CTRL_PAGE_SIZE;
 		}
 	}
 
+	if (i != nr_entries) {
+		ret = -EINVAL;
+		goto err;
+	}
+
 	ret = dma_buf_io_init_map(ctx, &map->base, sgt);
 	if (ret)
 		goto err;



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] nvme-pci: validate dmabuf size and sg_table bounds in nvme_dma_buf_io_map()
  2026-12-01  6:39 [PATCH] nvme-pci: validate dmabuf size and sg_table bounds in nvme_dma_buf_io_map() Bilal Hashim
@ 2026-12-01  6:44 ` sashiko-bot
  2026-12-01  7:58 ` Christoph Hellwig
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-12-01  6:44 UTC (permalink / raw)
  To: Bilal Hashim; +Cc: linux-nvme

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] nvme-pci: missing Fixes trailer for nvme_dma_buf_io_map() bounds
  fix
--

commit 0f211451ecf8fb8b6438a1ca1c4bb449732f7069
Author: Bilal Hashim <AeeSec@126.com>

> nvme-pci: validate dmabuf size and sg_table bounds in nvme_dma_buf_io_map()
>
> In nvme_dma_buf_io_map():
>
> 1. ctx->dmabuf->size (size_t) is divided by NVME_CTRL_PAGE_SIZE and
>    stored into a 32-bit unsigned nr_entries without checking for zero or
>    overflow, and nvme_rq_setup_dmabuf_map() indexes the buffer via the
>    32-bit bio->bi_iter.bi_offset. Reject empty dma-bufs or dma-bufs
>    larger than UINT_MAX.
> 2. When populating map->dma_list[] from the scatter-gather table, there
>    is no bounds check against nr_entries. If the sg_table total DMA
>    length exceeds nr_entries * NVME_CTRL_PAGE_SIZE, map->dma_list[i++]
>    overflows the allocated buffer. Conversely, if the sg_table total DMA
>    length is smaller than nr_entries * NVME_CTRL_PAGE_SIZE, trailing
>    entries in map->dma_list[] remain uninitialized (kvmalloc_flex() does
>    not zero-initialize) and could be programmed as arbitrary NVMe DMA
>    addresses.
>
> Validate ctx->dmabuf->size before computing nr_entries, bounds-check
> each write into map->dma_list[], and verify that the entire dma_list was
> populated.
>
> Found by manual code inspection.
>
> Signed-off-by: Bilal Hashim <AeeSec@126.com>

[Severity: Low]
This fixes missing size and scatter-gather bounds checks in
nvme_dma_buf_io_map().  Should this include:

  Fixes: ce5e386c1108 ("nvme-pci: implement dma-buf backed requests")

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/179610715173.42980.4278125034253504647@126.com?part=1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] nvme-pci: validate dmabuf size and sg_table bounds in nvme_dma_buf_io_map()
  2026-12-01  6:39 [PATCH] nvme-pci: validate dmabuf size and sg_table bounds in nvme_dma_buf_io_map() Bilal Hashim
  2026-12-01  6:44 ` sashiko-bot
@ 2026-12-01  7:58 ` Christoph Hellwig
  1 sibling, 0 replies; 3+ messages in thread
From: Christoph Hellwig @ 2026-12-01  7:58 UTC (permalink / raw)
  To: Bilal Hashim
  Cc: Keith Busch, Jens Axboe, Sagi Grimberg, Pavel Begunkov,
	linux-nvme, linux-kernel

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-01  7:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01  6:39 [PATCH] nvme-pci: validate dmabuf size and sg_table bounds in nvme_dma_buf_io_map() Bilal Hashim
2026-12-01  6:44 ` sashiko-bot
2026-12-01  7:58 ` Christoph Hellwig

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox