ata: sata_dwc_460ex: Fix DMA channel leak in sata_dwc_port_start()
PATCH 1/1 ata: sata_dwc_460ex: Fix DMA channel leak in sata_dwc_port_start()
If phy_power_on() fails in sata_dwc_port_start(), the error path frees hsdevp without releasing the DMA channel acquired earlier. Add a cleanup label to release the DMA channel before freeing hsdevp on this failure path.
--- drivers/ata/sata_dwc_460ex.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/ata/sata_dwc_460ex.c +3 -1
@@ -853,7 +853,7 @@ static int sata_dwc_port_start(struct ata_port *ap) err =phy_power_on (hsdev->phy);if (err) - goto CLEANUP_ALLOC; + goto CLEANUP_DMA;for (i =0 ; i <SATA_DWC_QCMD_MAX ; i++) hsdevp->cmd_issued[i] =SATA_DWC_CMD_ISSUED_NOT ;
@@ -880,6 +880,8 @@ static int sata_dwc_port_start(struct ata_port *ap)dev_dbg (ap->dev,"%s: done\n" , __func__);return 0 ; +CLEANUP_DMA: +dma_release_channel (hsdevp->chan); CLEANUP_ALLOC:kfree (hsdevp); CLEANUP:
Signature · 1 line
2.25.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [Critical] ata: sata_dwc_460ex: use-after-free…
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Critical] ata: sata_dwc_460ex: use-after-free in sata_dwc_probe() [1] --
commit 7cbed05368af81c2d62d6074918f0d6f1dc52c91
Author: Haoxiang Li <[email protected]>
ata: sata_dwc_460ex: Fix DMA channel leak in sata_dwc_port_start()
This commit releases the DMA channel in sata_dwc_port_start() when phy_power_on() fails before freeing the port structure.
Quoted 14 lines
drivers/ata/sata_dwc_460ex.c +1
-1@@ -852,7 +852,7 @@ static int sata_dwc_port_start(struct ata_port *ap) err =phy_power_on (hsdev->phy);if (err) - goto CLEANUP_ALLOC; + goto CLEANUP_DMA;for (i =0 ; i <SATA_DWC_QCMD_MAX ; i++) hsdevp->cmd_issued[i] =SATA_DWC_CMD_ISSUED_NOT ;
drivers/ata/sata_dwc_460ex.c static int sata_dwc_port_start(struct ata_port *ap) · line 886
Quoted 8 lines
drivers/ata/sata_dwc_460ex.c +2
-0@@ -879,6 +879,8 @@ static int sata_dwc_port_start(struct ata_port *ap)dev_dbg (ap->dev,"%s: done\n" , __func__);return 0 ; +CLEANUP_DMA: +dma_release_channel (hsdevp->chan); CLEANUP_ALLOC:kfree (hsdevp); CLEANUP:
[Severity: Critical]
This problem wasn't introduced by this patch, but can a failure in sata_dwc_probe() after sata_dwc_dma_init_old() cause a use-after-free
[1] on the shared interrupt line?
drivers/ata/sata_dwc_460ex.c:sata_dwc_probe() {
...
#ifdef CONFIG_SATA_DWC_OLD_DMA
if (!of_property_present(dev->of_node, "dmas")) {
err = sata_dwc_dma_init_old(ofdev, hsdev);
if (err)
return err;
}#endif
hsdev->phy = devm_phy_optional_get(dev, "sata-phy"); if (IS_ERR(hsdev->phy)) return PTR_ERR(hsdev->phy); err = phy_init(hsdev->phy); if (err) goto error_out; ... error_out: phy_exit(hsdev->phy); return err; }
sata_dwc_dma_init_old() registers a DMA engine and requests a shared interrupt without devres, but the subsequent error paths in sata_dwc_probe() return without calling sata_dwc_dma_exit_old(). When probe fails, the driver core frees the devm-allocated device context while the shared interrupt remains registered, so a later interrupt on that line invokes the handler with freed memory.
[1] https://sashiko.dev/#/bug/linux-4f29918f-8b5f-48b5-ab4d-aa40ae6890e7
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 7 lines · Haoxiang Li
If
phy_power_on()fails insata_dwc_port_start(), the error path frees hsdevp without releasing the DMA channel acquired earlier. Add a cleanup label to release the DMA channel before freeing hsdevp on this failure path.Fixes 0f48debdb906 ("ata: sata_dwc_460ex: add phy support")Signed-off-by Haoxiang Li <[email protected]>
Looks good.
Signature · 2 lines
Damien Le Moal Western Digital Research
Quoted 6 lines · Haoxiang Li
If
phy_power_on()fails insata_dwc_port_start(), the error path frees hsdevp without releasing the DMA channel acquired earlier. Add a cleanup label to release the DMA channel before freeing hsdevp on this failure path.
Applied to libata/linux.git (for-7.5), thanks!
[1/1] ata: sata_dwc_460ex: Fix DMA channel leak in sata_dwc_port_start()
https://git.kernel.org/libata/linux/c/9397e8e9
Kind regards,
Niklas