mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Bilal Hashim <AeeSec@126.com>
To: Keith Busch <kbusch@kernel.org>, Jens Axboe <axboe@kernel.dk>,
	Christoph Hellwig <hch@lst.de>, Sagi Grimberg <sagi@grimberg.me>,
	Pavel Begunkov <asml.silence@gmail.com>
Cc: linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org
Subject: [PATCH] nvme-pci: validate dmabuf size and sg_table bounds in nvme_dma_buf_io_map()
Date: Tue, 01 Dec 2026 14:39:11 +0800	[thread overview]
Message-ID: <179610715173.42980.4278125034253504647@126.com> (raw)

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;


             reply	other threads:[~2026-12-01  6:40 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  6:39 Bilal Hashim [this message]
2026-12-01  7:58 ` Christoph Hellwig

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=179610715173.42980.4278125034253504647@126.com \
    --to=aeesec@126.com \
    --cc=asml.silence@gmail.com \
    --cc=axboe@kernel.dk \
    --cc=hch@lst.de \
    --cc=kbusch@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=sagi@grimberg.me \
    /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

all inboxes | Powered by JetHome®