| Message ID | 20260611063631.96566-1-zenghongling@kylinos.cn (mailing list archive) |
|---|---|
| State | New |
| Headers |
Return-Path: <linux-sunxi+bounces-23778-sunxi=pue.re@lists.linux.dev> X-Original-To: noreply@patchwork.local Delivered-To: noreply@patchwork.local Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mxe881.netcup.net (Postfix) with ESMTPS id 187551C160C for <noreply@patchwork.local>; Thu, 11 Jun 2026 08:38:27 +0200 (CEST) Authentication-Results: mxe881; spf=pass (sender IP is 172.234.253.10) smtp.mailfrom=linux-sunxi+bounces-23778-noreply=patchwork.local@lists.linux.dev smtp.helo=sea.lore.kernel.org Received-SPF: pass (mxe881: domain of lists.linux.dev designates 172.234.253.10 as permitted sender) client-ip=172.234.253.10; envelope-from=linux-sunxi+bounces-23778-noreply=patchwork.local@lists.linux.dev; helo=sea.lore.kernel.org; Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.90.174.1]) by sea.lore.kernel.org (Postfix) with ESMTP id 6F617313F74A for <noreply@patchwork.local>; Thu, 11 Jun 2026 06:36:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6FC16364935; Thu, 11 Jun 2026 06:36:50 +0000 (UTC) X-Original-To: linux-sunxi@lists.linux.dev Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 483E92BEFE8 for <linux-sunxi@lists.linux.dev>; Thu, 11 Jun 2026 06:36:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=124.126.103.232 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781159810; cv=none; b=AOUdptP+CTLdrepH81AihiSudwhQcWh9YXN9cPXycxtvLxY20iHFiSqvp6NyfaXLK09xNof/6jdbjxsjvITHg5aEPlj0DiWubOWlx1mxfiZd1EmQQkfEDX59Ml/cQTLOrUZSSeMw5cdQJziOFjL9coKPSW1TCm2yPkDMoASFrGM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781159810; c=relaxed/simple; bh=9dvBrkdJfrYfPXnBGL9jqbmpMFwrVe4wBgElwU/t46E=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=s2e3X4Btbr4Fn27X0nT+0FF0F5XbTrryDIEIXWu44TeSzo9Dj0IgK5+GpY5IpppesVk1aoh6Uc3FMNXZ2UVXvIbhA650QMvhVhDP0PGvAb7sV/fMCsNbOZg4KvUmgi7D3AOB0q0eLr7wKW9OjOwSvrcgVrtL24hkxvgx/O1QH20= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn; spf=pass smtp.mailfrom=kylinos.cn; arc=none smtp.client-ip=124.126.103.232 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylinos.cn X-UUID: e4893022655f11f1aa26b74ffac11d73-20260611 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.12,REQID:dd46af78-ef48-42df-b0aa-f7aa64b8b7cc,IP:0,U RL:0,TC:0,Content:0,EDM:25,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTION :release,TS:25 X-CID-META: VersionHash:e7bac3a,CLOUDID:01b8394cc7936b409385b5ef72af8116,BulkI D:nil,BulkQuantity:0,Recheck:0,SF:102|850|865|898,TC:nil,Content:0|15|50,E DM:5,IP:nil,URL:0,File:nil,RT:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA: 0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0,ARC:0 X-CID-BVR: 2,SSN|SDN X-CID-BAS: 2,SSN|SDN,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-CID-RHF: D41D8CD98F00B204E9800998ECF8427E X-UUID: e4893022655f11f1aa26b74ffac11d73-20260611 X-User: zenghongling@kylinos.cn Received: from localhost.localdomain [(10.44.16.150)] by mailgw.kylinos.cn (envelope-from <zenghongling@kylinos.cn>) (Generic MTA with TLSv1.3 TLS_AES_256_GCM_SHA384 256/256) with ESMTP id 1353217616; Thu, 11 Jun 2026 14:36:35 +0800 From: Hongling Zeng <zenghongling@kylinos.cn> To: vkoul@kernel.org, Frank.Li@kernel.org, wens@kernel.org, jernej.skrabec@gmail.com, samuel@sholland.org, mripard@kernel.org, arnd@arndb.de Cc: dmaengine@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev, linux-kernel@vger.kernel.org, zhongling0719@126.com, Hongling Zeng <zenghongling@kylinos.cn> Subject: [PATCH] dmaengine: sun6i-dma: Fix use-after-free in error handling paths Date: Thu, 11 Jun 2026 14:36:31 +0800 Message-Id: <20260611063631.96566-1-zenghongling@kylinos.cn> X-Mailer: git-send-email 2.25.1 Precedence: bulk X-Mailing-List: linux-sunxi@lists.linux.dev List-Id: <linux-sunxi.lists.linux.dev> List-Subscribe: <mailto:linux-sunxi+subscribe@lists.linux.dev> List-Unsubscribe: <mailto:linux-sunxi+unsubscribe@lists.linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspamd-worker-8404 X-Spamd-Result: default: False [-0.66 / 15.00]; BAYES_HAM(-5.50)[100.00%]; RBL_SENDERSCORE(2.00)[172.234.253.10:from]; SUSPICIOUS_RECIPS(1.50)[]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; MAILLIST(-0.15)[generic]; BAD_REP_POLICIES(0.10)[]; MIME_GOOD(-0.10)[text/plain]; HAS_LIST_UNSUB(-0.01)[]; DMARC_NA(0.00)[kylinos.cn]; RCPT_COUNT_TWELVE(0.00)[13]; DBL_BLOCKED_OPENRESOLVER(0.00)[sea.lore.kernel.org:rdns,sea.lore.kernel.org:helo,kylinos.cn:email]; TAGGED_RCPT(0.00)[]; FREEMAIL_CC(0.00)[vger.kernel.org,lists.infradead.org,lists.linux.dev,126.com,kylinos.cn]; FUZZY_BLOCKED(0.00)[rspamd.com]; R_SPF_ALLOW(0.00)[+ip4:172.234.253.10]; PRECEDENCE_BULK(0.00)[]; FORGED_SENDER_MAILLIST(0.00)[]; RCVD_TLS_LAST(0.00)[]; MIME_TRACE(0.00)[0:+]; FORGED_RECIPIENTS_MAILLIST(0.00)[]; FREEMAIL_TO(0.00)[kernel.org,gmail.com,sholland.org,arndb.de]; FROM_HAS_DN(0.00)[]; ARC_ALLOW(0.00)[subspace.kernel.org:s=arc-20240116:i=1]; TAGGED_FROM(0.00)[bounces-23778-noreply=patchwork.local]; RCVD_COUNT_FIVE(0.00)[5]; R_DKIM_NA(0.00)[]; FROM_NEQ_ENVFROM(0.00)[zenghongling@kylinos.cn,linux-sunxi@lists.linux.dev]; ASN(0.00)[asn:63949, ipnet:172.234.224.0/19, country:SG]; TO_DN_SOME(0.00)[] X-Rspamd-Queue-Id: 187551C160C X-MORS-Enabled: yes X-MORS-DOMAIN: patchwork.local X-MORS-HOSTING: hosting172546 X-MORS-USER: hosting172546 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= |
| Series |
dmaengine: sun6i-dma: Fix use-after-free in error handling paths
|
|
Commit Message
Hongling Zeng
June 11, 2026, 6:36 a.m. UTC
In error handling paths, the for loop frees v_lli in the loop body,
then accesses v_lli->v_lli_next and v_lli->p_lli_next in the
increment expression, which is use-after-free.
Fix by saving both the next virtual and physical pointers before
freeing the current node.
Fixes: 555859308723 ("dmaengine: Add driver for Allwinner sun6i DMA")
Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
---
drivers/dma/sun6i-dma.c | 20 ++++++++++++++++----
1 file changed, 16 insertions(+), 4 deletions(-)
Comments
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
Pre-existing issues:
- [High] Missing `sg_len` validation in `sun6i_dma_prep_slave_sg` leads to NULL pointer dereference and hardware memory corruption.
- [High] Missing bounds validation in `sun6i_dma_prep_dma_cyclic` causes a kernel panic via division by zero or NULL pointer dereference.
- [High] Deadlock risk due to incompatible lock contexts for `sdev->lock`.
- [High] TOCTOU race condition in `sun6i_dma_interrupt` leads to NULL pointer dereference if raced against `sun6i_dma_terminate_all`.
--
commit ca6b94283f2abd1205fd392ecfced542cac0978a
Author: Hongling Zeng <zenghongling@kylinos.cn>
dmaengine: sun6i-dma: Fix use-after-free in error handling paths
This commit fixes a use-after-free issue in the error handling paths of the
DMA preparation functions. It saves the next physical and virtual pointers
before freeing the current linked list node.
> diff --git a/drivers/dma/sun6i-dma.c b/drivers/dma/sun6i-dma.c
> index a9a254dbf8cb9..eb9c4ae87ac8e 100644
> --- a/drivers/dma/sun6i-dma.c
> +++ b/drivers/dma/sun6i-dma.c
> @@ -788,9 +788,15 @@ static struct dma_async_tx_descriptor *sun6i_dma_prep_slave_sg(
[Severity: High]
This isn't a bug introduced by this patch, but missing sg_len validation in
sun6i_dma_prep_slave_sg() can lead to a NULL pointer dereference and
hardware memory corruption.
If a 0-length scatterlist is provided, the loop is completely bypassed:
for_each_sg(sgl, sg, sg_len, i) {
v_lli = dma_pool_alloc(sdev->pool, GFP_DMA32 | GFP_NOWAIT, &p_lli);
This leaves txd->v_lli as NULL and txd->p_lli as 0. When this descriptor is
later submitted in sun6i_dma_start_desc():
writel(pchan->desc->p_lli, pchan->base + DMA_CHAN_LLI_ADDR);
writel(DMA_CHAN_ENABLE_START, pchan->base + DMA_CHAN_ENABLE);
The uninitialized physical address 0 is fed directly to the DMA hardware
registers. Does this code need an explicit sg_len > 0 check to prevent
invalid memory accesses?
> return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
>
> err_lli_free:
> - for (p_lli = txd->p_lli, v_lli = txd->v_lli; v_lli;
> - p_lli = v_lli->p_lli_next, v_lli = v_lli->v_lli_next)
> + p_lli = txd->p_lli;
> + v_lli = txd->v_lli;
> + while (v_lli) {
> + struct sun6i_dma_lli *next_v_lli = v_lli->v_lli_next;
> + dma_addr_t next_p_lli = v_lli->p_lli_next;
> dma_pool_free(sdev->pool, v_lli, p_lli);
> + v_lli = next_v_lli;
> + p_lli = next_p_lli;
> + }
> kfree(txd);
> return NULL;
> }
[ ... ]
> @@ -869,9 +875,15 @@ static struct dma_async_tx_descriptor *sun6i_dma_prep_dma_cyclic(
[Severity: High]
This is a pre-existing issue, but missing bounds validation in
sun6i_dma_prep_dma_cyclic() can cause a kernel panic via division by zero or
a NULL pointer dereference.
The function computes the number of periods without checking if period_len
is zero:
unsigned int i, periods = buf_len / period_len;
If buf_len is less than period_len, periods evaluates to 0, which skips the
allocation loop and leaves prev as NULL, resulting in an immediate NULL
pointer dereference here:
prev->p_lli_next = txd->p_lli; /* cyclic list */
Could this be triggered by a caller passing invalid lengths, and should we
return an error early if period_len is zero or greater than buf_len?
> return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
>
> err_lli_free:
> - for (p_lli = txd->p_lli, v_lli = txd->v_lli; v_lli;
> - p_lli = v_lli->p_lli_next, v_lli = v_lli->v_lli_next)
> + p_lli = txd->p_lli;
> + v_lli = txd->v_lli;
> + while (v_lli) {
> + struct sun6i_dma_lli *next_v_lli = v_lli->v_lli_next;
> + dma_addr_t next_p_lli = v_lli->p_lli_next;
> dma_pool_free(sdev->pool, v_lli, p_lli);
> + v_lli = next_v_lli;
> + p_lli = next_p_lli;
> + }
> kfree(txd);
> return NULL;
> }
[Severity: High]
This isn't a bug introduced by this patch, but is there a deadlock risk due
to incompatible lock contexts for sdev->lock?
The spinlock sdev->lock is taken by sun6i_dma_tasklet() in softirq context:
spin_lock_irq(&sdev->lock);
However, sun6i_dma_pause() and sun6i_dma_terminate_all() acquire the lock
without disabling softirqs:
spin_lock(&sdev->lock);
list_del_init(&vchan->node);
spin_unlock(&sdev->lock);
If a process thread holds the lock and a hardware interrupt schedules the
tasklet, could the tasklet execute on the same CPU, attempt to acquire
sdev->lock with spin_lock_irq(), and spin forever because the preempted
process still holds it?
[Severity: High]
This is a pre-existing issue, but does a TOCTOU race condition exist
between sun6i_dma_interrupt() and sun6i_dma_terminate_all()?
In sun6i_dma_terminate_all(), the channel is stopped and pchan->desc is set
to NULL concurrently under vchan->vc.lock:
vchan->phy = NULL;
pchan->vchan = NULL;
pchan->desc = NULL;
pchan->done = NULL;
Meanwhile, the interrupt handler sun6i_dma_interrupt() processes the
completion:
if (vchan->cyclic) {
vchan_cyclic_callback(&pchan->desc->vd);
} else {
spin_lock(&vchan->vc.lock);
vchan_cookie_complete(&pchan->desc->vd);
In the cyclic case, the handler dereferences pchan->desc->vd locklessly. In
the non-cyclic case, it waits on vchan->vc.lock but doesn't re-validate
pchan->desc after acquiring the lock. Could this lead to a NULL pointer
dereference if the descriptor is cleared by sun6i_dma_terminate_all() on
another CPU?
Dne četrtek, 11. junij 2026 ob 08:36:31 Srednjeevropski poletni čas je Hongling Zeng napisal(a): > In error handling paths, the for loop frees v_lli in the loop body, > then accesses v_lli->v_lli_next and v_lli->p_lli_next in the > increment expression, which is use-after-free. > > Fix by saving both the next virtual and physical pointers before > freeing the current node. > > Fixes: 555859308723 ("dmaengine: Add driver for Allwinner sun6i DMA") > Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn> > --- > drivers/dma/sun6i-dma.c | 20 ++++++++++++++++---- > 1 file changed, 16 insertions(+), 4 deletions(-) > > diff --git a/drivers/dma/sun6i-dma.c b/drivers/dma/sun6i-dma.c > index a9a254dbf8cb..eb9c4ae87ac8 100644 > --- a/drivers/dma/sun6i-dma.c > +++ b/drivers/dma/sun6i-dma.c > @@ -788,9 +788,15 @@ static struct dma_async_tx_descriptor *sun6i_dma_prep_slave_sg( > return vchan_tx_prep(&vchan->vc, &txd->vd, flags); > > err_lli_free: > - for (p_lli = txd->p_lli, v_lli = txd->v_lli; v_lli; > - p_lli = v_lli->p_lli_next, v_lli = v_lli->v_lli_next) > + p_lli = txd->p_lli; > + v_lli = txd->v_lli; > + while (v_lli) { > + struct sun6i_dma_lli *next_v_lli = v_lli->v_lli_next; > + dma_addr_t next_p_lli = v_lli->p_lli_next; > dma_pool_free(sdev->pool, v_lli, p_lli); > + v_lli = next_v_lli; > + p_lli = next_p_lli; > + } > kfree(txd); > return NULL; > } > @@ -869,9 +875,15 @@ static struct dma_async_tx_descriptor *sun6i_dma_prep_dma_cyclic( > return vchan_tx_prep(&vchan->vc, &txd->vd, flags); > > err_lli_free: > - for (p_lli = txd->p_lli, v_lli = txd->v_lli; v_lli; > - p_lli = v_lli->p_lli_next, v_lli = v_lli->v_lli_next) > + p_lli = txd->p_lli; > + v_lli = txd->v_lli; > + while (v_lli) { > + struct sun6i_dma_lli *next_v_lli = v_lli->v_lli_next; > + dma_addr_t next_p_lli = v_lli->p_lli_next; > dma_pool_free(sdev->pool, v_lli, p_lli); > + v_lli = next_v_lli; > + p_lli = next_p_lli; > + } > kfree(txd); > return NULL; > } > This is certainly a valid fix, but it's replicating what sun6i_dma_free_desc() is already doing. It would be better to split code to accept struct sun6i_desc *txd parameter and call that instead from all places. Best regards, Jernej
diff --git a/drivers/dma/sun6i-dma.c b/drivers/dma/sun6i-dma.c index a9a254dbf8cb..eb9c4ae87ac8 100644 --- a/drivers/dma/sun6i-dma.c +++ b/drivers/dma/sun6i-dma.c @@ -788,9 +788,15 @@ static struct dma_async_tx_descriptor *sun6i_dma_prep_slave_sg( return vchan_tx_prep(&vchan->vc, &txd->vd, flags); err_lli_free: - for (p_lli = txd->p_lli, v_lli = txd->v_lli; v_lli; - p_lli = v_lli->p_lli_next, v_lli = v_lli->v_lli_next) + p_lli = txd->p_lli; + v_lli = txd->v_lli; + while (v_lli) { + struct sun6i_dma_lli *next_v_lli = v_lli->v_lli_next; + dma_addr_t next_p_lli = v_lli->p_lli_next; dma_pool_free(sdev->pool, v_lli, p_lli); + v_lli = next_v_lli; + p_lli = next_p_lli; + } kfree(txd); return NULL; } @@ -869,9 +875,15 @@ static struct dma_async_tx_descriptor *sun6i_dma_prep_dma_cyclic( return vchan_tx_prep(&vchan->vc, &txd->vd, flags); err_lli_free: - for (p_lli = txd->p_lli, v_lli = txd->v_lli; v_lli; - p_lli = v_lli->p_lli_next, v_lli = v_lli->v_lli_next) + p_lli = txd->p_lli; + v_lli = txd->v_lli; + while (v_lli) { + struct sun6i_dma_lli *next_v_lli = v_lli->v_lli_next; + dma_addr_t next_p_lli = v_lli->p_lli_next; dma_pool_free(sdev->pool, v_lli, p_lli); + v_lli = next_v_lli; + p_lli = next_p_lli; + } kfree(txd); return NULL; }