Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH] slimbus: qcom-ngd-ctrl: Fix potential DMA buffer leaks on teardown
@ 2026-11-18 14:16 Guangshuo Li
  2026-11-18 14:31 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Guangshuo Li @ 2026-11-18 14:16 UTC (permalink / raw)
  To: Srinivas Kandagatla; +Cc: linux-arm-msm, linux-sound, linux-kernel, stable

DMA teardown terminates transfers and releases the channels but leaves
the coherent message buffers allocated. A subsequent initialization
overwrites their addresses, and removal loses them permanently.

Free the buffers after terminating DMA. Clear channels released during
initialization failure so teardown cannot free the same resources again.

This issue was found by our static analysis tool under development.

Fixes: 917809e2280b ("slimbus: ngd: Add qcom SLIMBus NGD driver")
Cc: stable@vger.kernel.org
Signed-off-by: Guangshuo Li <liguangshuo@mail.sdu.edu.cn>
---
 drivers/slimbus/qcom-ngd-ctrl.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/drivers/slimbus/qcom-ngd-ctrl.c b/drivers/slimbus/qcom-ngd-ctrl.c
index eb983b6d1815..e604f4e2b762 100644
--- a/drivers/slimbus/qcom-ngd-ctrl.c
+++ b/drivers/slimbus/qcom-ngd-ctrl.c
@@ -706,9 +706,12 @@ static int qcom_slim_ngd_init_rx_msgq(struct qcom_slim_ngd_ctrl *ctrl)
 	return 0;
 
 rx_post_err:
+	dmaengine_terminate_sync(ctrl->dma_rx_channel);
 	dma_free_coherent(dev, size, ctrl->rx_base, ctrl->rx_phys_base);
+	ctrl->rx_base = NULL;
 rel_rx:
 	dma_release_channel(ctrl->dma_rx_channel);
+	ctrl->dma_rx_channel = NULL;
 	return ret;
 }
 
@@ -743,6 +746,7 @@ static int qcom_slim_ngd_init_tx_msgq(struct qcom_slim_ngd_ctrl *ctrl)
 	return 0;
 rel_tx:
 	dma_release_channel(ctrl->dma_tx_channel);
+	ctrl->dma_tx_channel = NULL;
 	return ret;
 }
 
@@ -1210,11 +1214,19 @@ static int qcom_slim_ngd_exit_dma(struct qcom_slim_ngd_ctrl *ctrl)
 {
 	if (ctrl->dma_rx_channel) {
 		dmaengine_terminate_sync(ctrl->dma_rx_channel);
+		dma_free_coherent(ctrl->dev,
+				  QCOM_SLIM_NGD_DESC_NUM * SLIM_MSGQ_BUF_LEN,
+				  ctrl->rx_base, ctrl->rx_phys_base);
+		ctrl->rx_base = NULL;
 		dma_release_channel(ctrl->dma_rx_channel);
 	}
 
 	if (ctrl->dma_tx_channel) {
 		dmaengine_terminate_sync(ctrl->dma_tx_channel);
+		dma_free_coherent(ctrl->dev,
+				  (QCOM_SLIM_NGD_DESC_NUM + 1) * SLIM_MSGQ_BUF_LEN,
+				  ctrl->tx_base, ctrl->tx_phys_base);
+		ctrl->tx_base = NULL;
 		dma_release_channel(ctrl->dma_tx_channel);
 	}
 

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] slimbus: qcom-ngd-ctrl: Fix potential DMA buffer leaks on teardown
  2026-11-18 14:16 [PATCH] slimbus: qcom-ngd-ctrl: Fix potential DMA buffer leaks on teardown Guangshuo Li
@ 2026-11-18 14:31 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-18 14:31 UTC (permalink / raw)
  To: Guangshuo Li; +Cc: linux-sound

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] slimbus: qcom-ngd-ctrl: NULL deref and UAF after
  qcom_slim_ngd_exit_dma()
--

commit 2b691a90537a94bfedb91b63d4fb5eae146f115d
Author: Guangshuo Li <liguangshuo@mail.sdu.edu.cn>

slimbus: qcom-ngd-ctrl: Fix potential DMA buffer leaks on teardown

This commit frees the coherent RX and TX DMA message buffers during DMA
teardown and clears the released DMA channel pointers on initialization
failure.

> diff --git a/drivers/slimbus/qcom-ngd-ctrl.c b/drivers/slimbus/qcom-ngd-ctrl.c
> index eb983b6d1815f..e604f4e2b7624 100644
> --- a/drivers/slimbus/qcom-ngd-ctrl.c
> +++ b/drivers/slimbus/qcom-ngd-ctrl.c

[ ... ]

> @@ -1210,11 +1214,19 @@ static int qcom_slim_ngd_exit_dma(struct qcom_slim_ngd_ctrl *ctrl)
>  {
>  	if (ctrl->dma_rx_channel) {
>  		dmaengine_terminate_sync(ctrl->dma_rx_channel);
> +		dma_free_coherent(ctrl->dev,
> +				  QCOM_SLIM_NGD_DESC_NUM * SLIM_MSGQ_BUF_LEN,
> +				  ctrl->rx_base, ctrl->rx_phys_base);
> +		ctrl->rx_base = NULL;
>  		dma_release_channel(ctrl->dma_rx_channel);
>  	}
>  
>  	if (ctrl->dma_tx_channel) {
>  		dmaengine_terminate_sync(ctrl->dma_tx_channel);
> +		dma_free_coherent(ctrl->dev,
> +				  (QCOM_SLIM_NGD_DESC_NUM + 1) * SLIM_MSGQ_BUF_LEN,
> +				  ctrl->tx_base, ctrl->tx_phys_base);
> +		ctrl->tx_base = NULL;
>  		dma_release_channel(ctrl->dma_tx_channel);
>  	}

[Severity: High]
Can freeing ctrl->tx_base and setting it to NULL here cause a NULL pointer
dereference or use-after-free in qcom_slim_ngd_xfer_msg()?

qcom_slim_ngd_tx_msg_get() computes the descriptor buffer address from
ctrl->tx_base without checking whether ctrl->tx_base is NULL:

drivers/slimbus/qcom-ngd-ctrl.c:qcom_slim_ngd_tx_msg_get() {
    ...
	desc  = &ctrl->txdesc[ctrl->tx_tail];
	desc->base = ctrl->tx_base + ctrl->tx_tail * SLIM_MSGQ_BUF_LEN;
	desc->comp = comp;
	ctrl->tx_tail = (ctrl->tx_tail + 1) % QCOM_SLIM_NGD_DESC_NUM;

	spin_unlock_irqrestore(&ctrl->tx_buf_lock, flags);

	return desc->base;
}

When ctrl->tx_base is NULL and ctrl->tx_tail is non-zero (or on the second
call after the first call advances ctrl->tx_tail from 0 to 1),
qcom_slim_ngd_tx_msg_get() returns a non-NULL small offset pointer, which
qcom_slim_ngd_xfer_msg() immediately dereferences at *pbuf = 0.

This is reachable when qcom_slim_ngd_runtime_suspend() calls
qcom_slim_ngd_exit_dma() and the subsequent qcom_slim_qmi_power_request()
fails (such as with -EBUSY), aborting suspend while ctrl->state remains
QCOM_SLIM_NGD_CTRL_IDLE with ctrl->tx_base set to NULL; or when
qcom_slim_ngd_runtime_resume() returns 0 after qcom_slim_ngd_power_up()
fails before reinitializing DMA.

In addition, qcom_slim_ngd_exit_dma() frees ctrl->tx_base without holding
ctrl->tx_buf_lock or ctrl->tx_lock and before ctrl->m_work is flushed, so a
concurrent transfer can also write into the freed coherent buffer:

CPU0                                  CPU1
qcom_slim_ngd_xfer_msg()
  pbuf = qcom_slim_ngd_tx_msg_get()
                                      qcom_slim_ngd_exit_dma()
                                        dma_free_coherent(..., ctrl->tx_base, ...)
                                        ctrl->tx_base = NULL;
  *pbuf = 0;  <-- use-after-free

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261118-324f82bd41-slimbus-qcom-0@mail.sdu.edu.cn?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-18 14:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18 14:16 [PATCH] slimbus: qcom-ngd-ctrl: Fix potential DMA buffer leaks on teardown Guangshuo Li
2026-11-18 14:31 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox