Skip to contentSkip to conversation

spi: bcm2835: fix NULL pointer deref for OOB transfers.

PATCH In review Dani Sanz 3 participants 4 messages Apr 12, 2023 – Apr 16, 2023

Subsystems (3)
lore
Share & tools
lore

Dani Sanz

author
#0 lore raw

PATCH 1/1 spi: bcm2835: fix NULL pointer deref for OOB transfers.

From: Dani Sanz <[email protected]>

There was a problem when calling ioctl(fd, SPI_IOC_ENABLE_OOB_MODE, oob_spi_setup) to use spidev in OOB mode after setting up the spidev.

The kernel crashed because on function bcm2835_spi_start_oob_transfer(struct spi_controller *ctlr, struct spi_oob_transfer *xfer), it tried to access bs->slv->prepare_cs, however bs->slv was NULL.

This patch sets the bs->slv value to the correct bcm2835_spidev object during bcm2835_spi_setup(struct spi_device *spi), so the NULL pointer dereference that happened later is avoided.

---
 drivers/spi/spi-bcm2835.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/spi/spi-bcm2835.c +2 -0
index 2626abfc0a5b..1051482df945 100644
@@ -1244,6 +1244,8 @@ static int bcm2835_spi_setup(struct spi_device *spi)
 
 		spi_set_ctldata(spi, slv);
 
+		bs->slv = slv;
+
 		ret = bcm2835_spi_setup_dma(ctlr, spi, bs, slv);
 		if (ret)
 			goto err_cleanup;
Signature · 1 line
2.25.1
#1 lore raw

drivers/spi/spi-bcm2835.c

Quoted 23 lines · Dani Sanz

From: Dani Sanz <[email protected]>

There was a problem when calling ioctl(fd, SPI_IOC_ENABLE_OOB_MODE, oob_spi_setup) to use spidev in OOB mode after setting up the spidev.

The kernel crashed because on function bcm2835_spi_start_oob_transfer(struct spi_controller *ctlr, struct spi_oob_transfer *xfer), it tried to access bs->slv->prepare_cs, however bs->slv was NULL.

This patch sets the bs->slv value to the correct bcm2835_spidev object during bcm2835_spi_setup(struct spi_device *spi), so the NULL pointer dereference that happened later is avoided.

---
 drivers/spi/spi-bcm2835.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/spi/spi-bcm2835.c +0 -0
index 2626abfc0a5b..1051482df945 100644

Hi!

Thanks for your patch, but his is the wrong mailing list for this issue. The patch should be submitted upstream (Linux) and we will fetch it during the next update cycle of Dovetail or IPIPE.

[1] should provide the necessary details on how to submit such a patch.

It's common practice to include the kernel stack trace in the commit message so others can search for the same issue and will hopefully be directed to your patch.

Best regards,
Florian

[1] https://www.kernel.org/doc/html/latest/process/submitting-patches.html

Quoted 9 lines
drivers/spi/spi-bcm2835.c +2 -0
@@ -1244,6 +1244,8 @@ static int bcm2835_spi_setup(struct spi_device *spi)
 
 		spi_set_ctldata(spi, slv);
 
+		bs->slv = slv;
+
 		ret = bcm2835_spi_setup_dma(ctlr, spi, bs, slv);
 		if (ret)
 			goto err_cleanup;
#2 lore raw
Quoted 27 lines · Florian Bezdeka

drivers/spi/spi-bcm2835.c

Quoted 23 lines · Dani Sanz

From: Dani Sanz <[email protected]>

There was a problem when calling ioctl(fd, SPI_IOC_ENABLE_OOB_MODE, oob_spi_setup) to use spidev in OOB mode after setting up the spidev.

The kernel crashed because on function bcm2835_spi_start_oob_transfer(struct spi_controller *ctlr, struct spi_oob_transfer *xfer), it tried to access bs->slv->prepare_cs, however bs->slv was NULL.

This patch sets the bs->slv value to the correct bcm2835_spidev object during bcm2835_spi_setup(struct spi_device *spi), so the NULL pointer dereference that happened later is avoided.

---
 drivers/spi/spi-bcm2835.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/spi/spi-bcm2835.c +0 -0
index 2626abfc0a5b..1051482df945 100644

Hi!

Quoted 4 lines

Thanks for your patch, but his is the wrong mailing list for this issue. The patch should be submitted upstream (Linux) and we will fetch it during the next update cycle of Dovetail or IPIPE.

Actually, this patch applies specifically to Dovetail, upstream refers to bs->slv only when TX via DMA is enabled, so there is no issue in this case. However, Dovetail extends this assumption to any transfer direction, but fails to init this field accordingly, hence the breakage.

Signature · 1 line
Philippe.
#3 lore raw
In reply to Dani Sanz
Quoted 24 lines · Dani Sanz

From: Dani Sanz <[email protected]>

There was a problem when calling ioctl(fd, SPI_IOC_ENABLE_OOB_MODE, oob_spi_setup) to use spidev in OOB mode after setting up the spidev.

The kernel crashed because on function bcm2835_spi_start_oob_transfer(struct spi_controller *ctlr, struct spi_oob_transfer *xfer), it tried to access bs->slv->prepare_cs, however bs->slv was NULL.

This patch sets the bs->slv value to the correct bcm2835_spidev object during bcm2835_spi_setup(struct spi_device *spi), so the NULL pointer dereference that happened later is avoided.

---
 drivers/spi/spi-bcm2835.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/spi/spi-bcm2835.c +0 -0
index 2626abfc0a5b..1051482df945 100644

drivers/spi/spi-bcm2835.c static int bcm2835_spi_setup(struct spi_device *spi) · line 1251

Quoted 8 lines
drivers/spi/spi-bcm2835.c +2 -0
@@ -1244,6 +1244,8 @@ static int bcm2835_spi_setup(struct spi_device *spi)
 
 		spi_set_ctldata(spi, slv);
 
+		bs->slv = slv;
+
 		ret = bcm2835_spi_setup_dma(ctlr, spi, bs, slv);
 		if (ret)
 			goto err_cleanup;

This bug has been there for quite some time it seems. Merged into 5.15.y, 6.1.y and 6.3, dovetail and evl trees. Thanks for looking into this.

Signature · 1 line
Philippe.