From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=117.135.210.9 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796107205; b=jBGLY57FOCiQLGfOuofGVc2449zqvBP8XBkr4Xh84TKeWUaQPl0+2lpjk7y2vJ5KxcIx V/3oimpVaj/ych3wxtma/6eZgcHUHLa9sXGi2/vJN2XiL2NF4HOC8e58De+aNj3PYrIOz r5ZstIUZqrwFvyxsXDdGUtRUdryOHpXSAl31A43TbkrKhKTuEjHNmJ/IgZxN0tQs/l1YM WqNifwevuH2ppm+C216SWY0BxNwLHz/AYJ1G9kq+Mc+SsBpfmg4fl0mzzxUM7CrQWSD9S VT9iCILrTVnrHscT3BTfUZayVG8TkES7ukvf1B4RhvffP9aTDWDCVrlT/QWEoTU/A3Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796107205; h=DMARC-Filter:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID: Content-Type:Content-Transfer-Encoding:MIME-Version:X-CM-TRANSID: X-Coremail-Antispam:X-CM-SenderInfo; bh=q9vP0stD6dzSRxbGWiSjw35cUrVCd7in6MpbSyDFgXc=; b=ZB3ZgWKwRke4p729Cfw8E5P1DijC/pYy/gvGBzkgFn8+nueLbDyoI4Rk48k4vA4bPmk8 T+La0H8xaTGH0sM6zrL+FI0qQXZZZ83EP75I94cjuLNM/Cpk6IC05NOB8+XjNrMfwSF4E cH2/ueDupt3e2nFTcmSLquOAb7zxbv0oYIFlaHRbMCIznsZRWgSuK+e45UVP8W1bv7ZhK GblP6aaCfV2vHnva0jMyOo5j6mFcbd/mtckZCPgJvJ9w0tvu/6WdsFintbu7BOdEOP1CT NFVgK2NeErzwi1yZ3cW97bd2M2UFCMAbr6u2x98gzLQ1WUwN5iUfKGqtlD8OoLDNhrA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=126.com; spf=pass smtp.mailfrom=126.com; dkim=pass header.d=126.com header.i=@126.com header.a=rsa-sha256 header.s=s110527 header.b=puytQUBY; arc=none smtp.remote-ip=117.135.210.9 Received: from m16.mail.126.com (m16.mail.126.com [117.135.210.9]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 48A69606A31A for ; Tue, 1 Dec 2026 06:40:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 48A69606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=126.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=126.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=126.com header.i=@126.com header.a=rsa-sha256 header.s=s110527 header.b=puytQUBY DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:To:Subject:Date:Message-ID:Content-Type: MIME-Version; bh=q9vP0stD6dzSRxbGWiSjw35cUrVCd7in6MpbSyDFgXc=; b=puytQUBYEnzwhdswSVTRKEuBzOk1qbi6ZMQhhqjy3jdNxlGdWGW/JOkk8uLNhq lqUrUsBmQINQ5kI0wxh7zrHkEb9eiMd0zep6X93zT+EyV1+fMQn2r2WPUR1SZ2W/ YLmyK3IxDmW3I4ymqujt2ci3JS3klI9T9F3NfQpq4c9XI= From: Bilal Hashim To: Keith Busch , Jens Axboe , Christoph Hellwig , Sagi Grimberg , Pavel Begunkov 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 Message-ID: <179610715173.42980.4278125034253504647@126.com> Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-CM-TRANSID:_____wDnxwSMaw5r4ER3EQ--.25738S3 X-Coremail-Antispam: 1Uf129KBjvJXoWxZw45CrWDWrWfCr4ktF48Crg_yoW5XFy7pF 4fXr12yrWSqr17JayrAwsxJ3Z0y3yfKF18ur43Kws09w4Utry5XFnFgFyFyFyDCF4kXFyF yFWDtrn8uFW5AF7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07j8nYwUUUUU= X-CM-SenderInfo: 5dhh2vbf6rjloofrz/xtbBnxBd92sOa5ApBgAA3U 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 --- 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;