From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (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 8811F606A688 for ; Tue, 1 Dec 2026 07:19:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ljkqSKbJ Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 0210710E10C; Tue, 1 Dec 2026 07:19:43 +0000 (UTC) Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by gabe.freedesktop.org (Postfix) with ESMTPS id DB5DD10E164; Tue, 1 Dec 2026 07:19:36 +0000 (UTC) Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 986BE44238; Tue, 1 Dec 2026 07:19:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3D04D1F000C1; Tue, 1 Dec 2026 07:19:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796109576; bh=l2Sk1QSRw5HO/Gg5v1RiYWZvnvsPmiwiGfvnM7jYtrY=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=ljkqSKbJdm6zOp6vFrX4o+MIzTa3UTVgS7/LRDI3SCBWEJHdv565cLY2Q9ee1p1gD 20CfzrkZud37qI+dmYHjVaokW0NA5PhdzKpKpAGXTXl6hnCQ5AtevAUeB2Egwrctqi IpfyVwKAGhNOwgxvCQPwr5my+FTJl/89PR2WJ2rwdhAB0Vk9AwHJxH0neYKIK0LkIs EBgLV+l57l62x5Jb5URPyZlZaUXJs0mx7LrTMz0+MBf+Jrtr6Jwu4PgSQqw4y6xCAB bmCtCzHujCnnQulXRXfx8p40oG4XPFjdhu1fKliwsu0cdnnO+M7VDFBFPJQcCVv6Yg hEGw0ct5XVJHA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH RFC 04/16] dma-shared-buf: Add dma_addr_list_to_sgt() To: "Alistair Popple" Cc: dri-devel@lists.freedesktop.org, intel-xe@lists.freedesktop.org, linux-rdma@vger.kernel.org, kvm@vger.kernel.org, media-ci@linuxtv.org In-Reply-To: <20261201-dmabuf_shared_buf_v1_xe_rc5-v1-4-b672717c7fc9@nvidia.com> References: <20261201-dmabuf_shared_buf_v1_xe_rc5-v1-0-b672717c7fc9@nvidia.com> <20261201-dmabuf_shared_buf_v1_xe_rc5-v1-4-b672717c7fc9@nvidia.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Tue, 01 Dec 2026 07:19:35 +0000 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: sashiko-reviews@lists.linux.dev Sender: "dri-devel" X-Spam-Status: No, score=-10.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 X-Archived-By: Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] dma-shared-buf: integer overflow in dma_addr_list_to_sgt() nents calculation -- commit d053fd21323b983e9a0997bdd63898a8c3072b3f Author: Alistair Popple dma-shared-buf: Add dma_addr_list_to_sgt() This commit adds dma_addr_list_to_sgt() and dma_addr_list_free_sgt() to bui= ld and free a DMA-only sg_table over a struct dma_addr_list. > diff --git a/drivers/dma-buf/dma-shared-buf.c b/drivers/dma-buf/dma-share= d-buf.c > index d5d179b771f22..c346d4c05de8e 100644 > --- a/drivers/dma-buf/dma-shared-buf.c > +++ b/drivers/dma-buf/dma-shared-buf.c [ ... ] > @@ -207,3 +209,87 @@ int dma_shared_buf_iommu_map(const struct dma_shared= _buf *shared_buf, > iommu_unmap(domain, start_iova, iova - start_iova); > return ret; > } > + > +#define MAX_SG_ENT_SZ ALIGN_DOWN(UINT_MAX, PAGE_SIZE) > + > +static struct scatterlist *fill_sg_entry(struct scatterlist *sgl, size_t= length, > + dma_addr_t addr) > +{ > + size_t len; > + > + while (length) { > + len =3D min(length, MAX_SG_ENT_SZ); > + length -=3D len; > + /* > + * The scatterlist has no CPU list, only the DMA list. Always > + * set the page related values to NULL to ensure nothing > + * handed the table can use them. The phys_addr based DMA API > + * does not require the CPU list for mapping or unmapping. > + */ > + sg_set_page(sgl, NULL, 0, 0); > + sg_dma_address(sgl) =3D addr; > + sg_dma_len(sgl) =3D len; > + addr +=3D len; > + /* Unconditionally advance. On last segment, this becomes NULL */ > + sgl =3D sg_next(sgl); > + } > + > + return sgl; > +} > + > +/** > + * dma_addr_list_to_sgt - Build an sg_table over the DMA addresses of a = mapped > + * dma_shared_buf > + * @sgt: The table to build > + * @addrs: The DMA addresses dma_shared_buf_map() returned > + * > + * For a caller which has to present the mapping as an sg_table. The tab= le > + * carries the DMA side only. The CPU side is left empty so that nothing= handed > + * the table can reach a struct page. The table does not own the mapping: > + * @addrs must stay mapped while the table is in use and is still releas= ed with > + * dma_shared_buf_unmap(). > + * > + * Returns: 0 on success, in which case the table is freed with > + * dma_addr_list_free_sgt(), or a negative error value. > + */ > +int dma_addr_list_to_sgt(struct sg_table *sgt, > + const struct dma_addr_list *addrs) > +{ > + struct scatterlist *sgl; > + unsigned int nents =3D 0; > + unsigned int i; > + int ret; > + > + for (i =3D 0; i < addrs->nr_ranges; i++) { > + unsigned int added =3D DIV_ROUND_UP(addrs->ranges[i].len, > + MAX_SG_ENT_SZ); > + > + if (check_add_overflow(nents, added, &nents)) > + return -E2BIG; > + } [Severity: High] Can DIV_ROUND_UP(addrs->ranges[i].len, MAX_SG_ENT_SZ) overflow when computi= ng added? MAX_SG_ENT_SZ is UINT_MAX - PAGE_SIZE + 1. On 32-bit builds, both size_t a= nd unsigned long are 32-bit, so the numerator addrs->ranges[i].len + MAX_SG_ENT_SZ - 1 wraps modulo 2^32 for any range with len > PAGE_SIZE and evaluates to added =3D=3D 0. If addrs mixes a single-page range (added =3D= =3D 1) and a multi-page range (added =3D=3D 0), sg_alloc_table() allocates too few ent= ries and fill_sg_entry() advances sgl to NULL via sg_next(sgl), causing a NULL pointer dereference in sg_set_page(). On 64-bit builds, assigning the quotient to unsigned int added also truncat= es the upper 32 bits before check_add_overflow(). Using DIV_ROUND_UP_ULL() in= to a u64 before check_add_overflow(nents, added, &nents) avoids both issues. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261201-dmabuf_sha= red_buf_v1_xe_rc5-v1-0-b672717c7fc9@nvidia.com?part=3D4