Skip to contentSkip to conversation

i2c: qcom-geni: fix double free of the DMA bounce buffer

PATCH In review Thomas Fourier 2 participants 2 messages Oct 28 – Oct 29

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

PATCH 1/1 i2c: qcom-geni: fix double free of the DMA bounce buffer

geni_i2c_{rx,tx}_msg_cleanup() release gi2c->dma_buf through i2c_put_dma_safe_msg_buf(), which kfree()s the bounce buffer when the message buffer is not DMA safe, but leave the pointer set. gi2c->dma_buf is only updated when the DMA setup in geni_i2c_{rx,tx}_one_msg() succeeds. If the setup fails for a later message, the function clears only its local copy, so the cleanup helper sees the stale pointer from the previous transfer, unmaps it again and frees it a second time.

Clear gi2c->dma_buf once it has been released.

This bug was found statically and compile-tested only.

Fixes 357ee8841d0b ("i2c: qcom-geni: Store DMA mapping data in geni_i2c_dev struct")
---
 drivers/i2c/busses/i2c-qcom-geni.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/i2c/busses/i2c-qcom-geni.c +2 -0
index a431e98723ef..78701310936b 100644
@@ -500,6 +500,7 @@ static void geni_i2c_rx_msg_cleanup(struct geni_i2c_dev *gi2c,
 			geni_i2c_rx_fsm_rst(gi2c);
 		geni_se_rx_dma_unprep(&gi2c->se, gi2c->dma_addr, gi2c->xfer_len);
 		i2c_put_dma_safe_msg_buf(gi2c->dma_buf, cur, !gi2c->err);
+		gi2c->dma_buf = NULL;
 	}
 }
 
@@ -512,6 +513,7 @@ static void geni_i2c_tx_msg_cleanup(struct geni_i2c_dev *gi2c,
 			geni_i2c_tx_fsm_rst(gi2c);
 		geni_se_tx_dma_unprep(&gi2c->se, gi2c->dma_addr, gi2c->xfer_len);
 		i2c_put_dma_safe_msg_buf(gi2c->dma_buf, cur, !gi2c->err);
+		gi2c->dma_buf = NULL;
 	}
 }
 
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Thomas Fourier

geni_i2c_{rx,tx}_msg_cleanup() release gi2c->dma_buf through i2c_put_dma_safe_msg_buf(), which kfree()s the bounce buffer when the message buffer is not DMA safe, but leave the pointer set. gi2c->dma_buf is only updated when the DMA setup in geni_i2c_{rx,tx}_one_msg() succeeds. If the setup fails for a later message, the function clears only its local copy, so the cleanup helper sees the stale pointer from the previous transfer, unmaps it again and frees it a second time.

Quoted 7 lines

Clear gi2c->dma_buf once it has been released.

This bug was found statically and compile-tested only.

Fixes 357ee8841d0b ("i2c: qcom-geni: Store DMA mapping data in geni_i2c_dev struct")

---