LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH AUTOSEL 5.10 01/20] dmaengine: xilinx_dma: Fix read-after-free bug when terminating transfers
@ 2021-08-10 14:15 Sasha Levin
  2021-08-10 14:15 ` [PATCH AUTOSEL 5.10 02/20] dmaengine: usb-dmac: Fix PM reference leak in usb_dmac_probe() Sasha Levin
                   ` (17 more replies)
  0 siblings, 18 replies; 19+ messages in thread
From: Sasha Levin @ 2021-08-10 14:15 UTC (permalink / raw)
  To: linux-kernel, stable
  Cc: Adrian Larumbe, Vinod Koul, Sasha Levin, dmaengine, linux-arm-kernel

From: Adrian Larumbe <adrian.martinezlarumbe@imgtec.com>

[ Upstream commit 7dd2dd4ff9f3abda601f22b9d01441a0869d20d7 ]

When user calls dmaengine_terminate_sync, the driver will clean up any
remaining descriptors for all the pending or active transfers that had
previously been submitted. However, this might happen whilst the tasklet is
invoking the DMA callback for the last finished transfer, so by the time it
returns and takes over the channel's spinlock, the list of completed
descriptors it was traversing is no longer valid. This leads to a
read-after-free situation.

Fix it by signalling whether a user-triggered termination has happened by
means of a boolean variable.

Signed-off-by: Adrian Larumbe <adrian.martinezlarumbe@imgtec.com>
Link: https://lore.kernel.org/r/20210706234338.7696-3-adrian.martinezlarumbe@imgtec.com
Signed-off-by: Vinod Koul <vkoul@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 drivers/dma/xilinx/xilinx_dma.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/drivers/dma/xilinx/xilinx_dma.c b/drivers/dma/xilinx/xilinx_dma.c
index 79777550a6ff..9ffdbeec436b 100644
--- a/drivers/dma/xilinx/xilinx_dma.c
+++ b/drivers/dma/xilinx/xilinx_dma.c
@@ -394,6 +394,7 @@ struct xilinx_dma_tx_descriptor {
  * @genlock: Support genlock mode
  * @err: Channel has errors
  * @idle: Check for channel idle
+ * @terminating: Check for channel being synchronized by user
  * @tasklet: Cleanup work after irq
  * @config: Device configuration info
  * @flush_on_fsync: Flush on Frame sync
@@ -431,6 +432,7 @@ struct xilinx_dma_chan {
 	bool genlock;
 	bool err;
 	bool idle;
+	bool terminating;
 	struct tasklet_struct tasklet;
 	struct xilinx_vdma_config config;
 	bool flush_on_fsync;
@@ -1049,6 +1051,13 @@ static void xilinx_dma_chan_desc_cleanup(struct xilinx_dma_chan *chan)
 		/* Run any dependencies, then free the descriptor */
 		dma_run_dependencies(&desc->async_tx);
 		xilinx_dma_free_tx_descriptor(chan, desc);
+
+		/*
+		 * While we ran a callback the user called a terminate function,
+		 * which takes care of cleaning up any remaining descriptors
+		 */
+		if (chan->terminating)
+			break;
 	}
 
 	spin_unlock_irqrestore(&chan->lock, flags);
@@ -1965,6 +1974,8 @@ static dma_cookie_t xilinx_dma_tx_submit(struct dma_async_tx_descriptor *tx)
 	if (desc->cyclic)
 		chan->cyclic = true;
 
+	chan->terminating = false;
+
 	spin_unlock_irqrestore(&chan->lock, flags);
 
 	return cookie;
@@ -2436,6 +2447,7 @@ static int xilinx_dma_terminate_all(struct dma_chan *dchan)
 
 	xilinx_dma_chan_reset(chan);
 	/* Remove and free all of the descriptors in the lists */
+	chan->terminating = true;
 	xilinx_dma_free_descriptors(chan);
 	chan->idle = true;
 
-- 
2.30.2


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

end of thread, other threads:[~2021-08-10 14:18 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-10 14:15 [PATCH AUTOSEL 5.10 01/20] dmaengine: xilinx_dma: Fix read-after-free bug when terminating transfers Sasha Levin
2021-08-10 14:15 ` [PATCH AUTOSEL 5.10 02/20] dmaengine: usb-dmac: Fix PM reference leak in usb_dmac_probe() Sasha Levin
2021-08-10 14:15 ` [PATCH AUTOSEL 5.10 03/20] spi: spi-mux: Add module info needed for autoloading Sasha Levin
2021-08-10 14:15 ` [PATCH AUTOSEL 5.10 04/20] net: xfrm: Fix end of loop tests for list_for_each_entry Sasha Levin
2021-08-10 14:15 ` [PATCH AUTOSEL 5.10 05/20] ARM: dts: am43x-epos-evm: Reduce i2c0 bus speed for tps65218 Sasha Levin
2021-08-10 14:15 ` [PATCH AUTOSEL 5.10 06/20] dmaengine: of-dma: router_xlate to return -EPROBE_DEFER if controller is not yet available Sasha Levin
2021-08-10 14:15 ` [PATCH AUTOSEL 5.10 07/20] scsi: pm80xx: Fix TMF task completion race condition Sasha Levin
2021-08-10 14:15 ` [PATCH AUTOSEL 5.10 08/20] scsi: megaraid_mm: Fix end of loop tests for list_for_each_entry() Sasha Levin
2021-08-10 14:15 ` [PATCH AUTOSEL 5.10 09/20] scsi: scsi_dh_rdac: Avoid crash during rdac_bus_attach() Sasha Levin
2021-08-10 14:15 ` [PATCH AUTOSEL 5.10 10/20] scsi: core: Avoid printing an error if target_alloc() returns -ENXIO Sasha Levin
2021-08-10 14:15 ` [PATCH AUTOSEL 5.10 11/20] scsi: core: Fix capacity set to zero after offlinining device Sasha Levin
2021-08-10 14:15 ` [PATCH AUTOSEL 5.10 12/20] drm/amdgpu: fix the doorbell missing when in CGPG issue for renoir Sasha Levin
2021-08-10 14:15 ` [PATCH AUTOSEL 5.10 13/20] qede: fix crash in rmmod qede while automatic debug collection Sasha Levin
2021-08-10 14:15 ` [PATCH AUTOSEL 5.10 14/20] ARM: dts: nomadik: Fix up interrupt controller node names Sasha Levin
2021-08-10 14:15 ` [PATCH AUTOSEL 5.10 16/20] Revert "ACPICA: Fix memory leak caused by _CID repair function" Sasha Levin
2021-08-10 14:15 ` [PATCH AUTOSEL 5.10 17/20] net: usb: pegasus: Check the return value of get_geristers() and friends; Sasha Levin
2021-08-10 14:15 ` [PATCH AUTOSEL 5.10 18/20] net: usb: lan78xx: don't modify phy_device state concurrently Sasha Levin
2021-08-10 14:15 ` [PATCH AUTOSEL 5.10 19/20] drm/amd/display: Fix Dynamic bpp issue with 8K30 with Navi 1X Sasha Levin
2021-08-10 14:15 ` [PATCH AUTOSEL 5.10 20/20] drm/amd/display: workaround for hard hang on HPD on native DP Sasha Levin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).