From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796109576; b=R+15pp4CTOJCousM3BdcRx4HI8pD+j2N22zBKI1HCUJD2nkAmzDgq+Wc5QhzSOga+pmF igygHKOEmrPoxcP8IlIdpkStlUm36rAXvr6UPLkgXDSjC13c10khXcF/yVO3Aq8mPlDJ/ 5S0TRkjXKQpZeUeMbj8gX108qb7yXcE/j9QA1XG6hF0WoQO55uEC7hyTY2/MvWvBKwtAb JebXHvA6oiSQCjjG/nVnaB26vszvjkBC8L0Pk7D1kXKsl3OiAN/MJsoht3tlQu3b+YOmT Pl6X88Y35yeg6TMTKzUZs9ij3whTPI6dMjBMODOI3NOHva5SpFeu4gXitni0BwhOvlQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796109576; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=l2Sk1QSRw5HO/Gg5v1RiYWZvnvsPmiwiGfvnM7jYtrY=; b=PG9+k945VkEROaBKwhkNPZnYyJSaLvK0tClSu3KUDLRKIS1c71Nx/jjmfwtMW0nUUaG3 ORsYJ4Z6MAMA/H2Not2H9CwwuQTCpvO4OD4YKR6uHHqjc1bppDT63LJn8h9VL7xzlJd5F eG0sUWknpdNv1+vF//F5q1iptwC/7LeeeCM9Mn4b8kiIsvf5r182hm8ILxt+8vQBwV5Qw xJGQukNwpnFysLrLue6CqBJgEIdq9wjak9/aTsSPQ0Bw9bP9yqXNPTAgAexey1rL2XJky 1lnCPaVobwDaUNBENTuUZcWUMEzGEym55Te3No+nl3dK5qT5fz3XFH5F5P85l6kPOxg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ljkqSKbJ; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A3A5E606A68A; Tue, 1 Dec 2026 07:19:36 +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: 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() Reply-To: sashiko-reviews@lists.linux.dev 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-Mailing-List: linux-rdma@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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