Skip to contentSkip to conversation

dmaengine: at_hdmac: fix memory leak in atc_prep_slave_sg

PATCH In review Rosen Penev 3 participants 3 messages Jul 23 – Sep 30

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dmaengine: at_hdmac: fix memory leak in atc_prep_slave_sg

The default case in the direction switch returns NULL without freeing the descriptor allocated earlier via kzalloc_flex(). Add the missing kfree(desc) before returning.

Fixes 808347f6a317 ("dmaengine: at_hdmac: add DMA slave transfers")
Assisted-by opencode:big-pickle
---
 drivers/dma/at_hdmac.c | 1 +
 1 file changed, 1 insertion(+)
drivers/dma/at_hdmac.c +1 -0
index f9c336630fa2..c06f70dbc4c5 100644
@@ -1381,6 +1381,7 @@ atc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
 		}
 		break;
 	default:
+		kfree(desc);
 		return NULL;
 	}
 
Signature · 1 line
2.55.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] NULL Pointer Dereference in atdma_desc_free during descriptor preparation error paths. --

--- Patch [1]: [PATCH] dmaengine: at_hdmac: fix memory leak in atc_prep_slave_sg --- commit 59e07e1618be88a67e6deaa08218ec2ae653a007
Author: Rosen Penev <[email protected]>

dmaengine: at_hdmac: fix memory leak in atc_prep_slave_sg

This commit fixes a memory leak in the default switch case of the slave scatter-gather preparation function. It adds a missing free for the allocated descriptor before returning on an invalid direction.

drivers/dma/at_hdmac.c atc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl, · line 1386

Quoted 11 lines
drivers/dma/at_hdmac.c +1 -0
index e5b30a57c477a..7763a94d3f998 100644
@@ -1381,6 +1381,7 @@ atc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
 		}
 		break;
 	default:
+		kfree(desc);
 		return NULL;
 	}

[Severity: High]
This is a pre-existing issue, but while reviewing this error handling I noticed a potential NULL pointer dereference in the other error paths of this function.

If dma_pool_alloc() fails or an SG list has a zero-length entry, the code jumps to the error path:

err_desc_get:

        dev_err(chan2dev(chan), "not enough descriptors available\n");
err:
        atdma_desc_free(&desc->vd);
        return NULL;

Inside atdma_desc_free(), it immediately evaluates the device from the channel:

static void atdma_desc_free(struct virt_dma_desc *vd)

{
        struct at_dma *atdma = to_at_dma(vd->tx.chan->device);
        ...
}

Since desc was just allocated with kzalloc_flex() which zeroes memory, and vd->tx.chan is not initialized until vchan_tx_prep() is called at the end of atc_prep_slave_sg(), won't vd->tx.chan still be NULL during this cleanup?

Does this code trigger a panic when attempting to dereference the NULL channel pointer?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by on v1 1/1
#2 lore raw
In reply to Rosen Penev
Quoted 8 lines · Rosen Penev

The default case in the direction switch returns NULL without freeing the descriptor allocated earlier via kzalloc_flex(). Add the missing kfree(desc) before returning.

Fixes 808347f6a317 ("dmaengine: at_hdmac: add DMA slave transfers")
Assisted-by opencode:big-pickle

---

Quoted 18 lines
 drivers/dma/at_hdmac.c | 1 +
 1 file changed, 1 insertion(+)
drivers/dma/at_hdmac.c +1 -1
index f9c336630fa2..c06f70dbc4c5 100644
@@ -1381,6 +1381,7 @@ atc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
 		}
 		break;
 	default:
+		kfree(desc);
 		return NULL;
 	}
 
--

2.55.0