Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

spi: restore rx/tx_buf in case of unset CONFIG_HAS_DMA

The case where spi_master sets the flags SPI_MASTER_MUST_RX/TX while
CONFIG_HAS_DMA is unset (which is unlikley) together with a driver
that reuses spi_messages with rx/tx_buff set to NULL, can result in:
* data disclosure over the SPI (for tx_buf == NULL)
* memory corruption (for rx_buf == NULL)

This happenes when dummy_rx/dummy_tx are changing address due to krealloc
or free and an allocation of the memory by a different part of the kernel.

Signed-off-by: Martin Sperl <kernel@martin.sperl.org>
Signed-off-by: Mark Brown <broonie@kernel.org>

authored by

Martin Sperl and committed by
Mark Brown
4b786458 8e76ef88

+22 -12
+22 -12
drivers/spi/spi.c
··· 571 571 return 0; 572 572 } 573 573 574 - static int spi_unmap_msg(struct spi_master *master, struct spi_message *msg) 574 + static int __spi_unmap_msg(struct spi_master *master, struct spi_message *msg) 575 575 { 576 576 struct spi_transfer *xfer; 577 577 struct device *tx_dev, *rx_dev; ··· 583 583 rx_dev = master->dma_rx->device->dev; 584 584 585 585 list_for_each_entry(xfer, &msg->transfers, transfer_list) { 586 - /* 587 - * Restore the original value of tx_buf or rx_buf if they are 588 - * NULL. 589 - */ 590 - if (xfer->tx_buf == master->dummy_tx) 591 - xfer->tx_buf = NULL; 592 - if (xfer->rx_buf == master->dummy_rx) 593 - xfer->rx_buf = NULL; 594 - 595 586 if (!master->can_dma(master, msg->spi, xfer)) 596 587 continue; 597 588 ··· 599 608 return 0; 600 609 } 601 610 602 - static inline int spi_unmap_msg(struct spi_master *master, 603 - struct spi_message *msg) 611 + static inline int __spi_unmap_msg(struct spi_master *master, 612 + struct spi_message *msg) 604 613 { 605 614 return 0; 606 615 } 607 616 #endif /* !CONFIG_HAS_DMA */ 617 + 618 + static inline int spi_unmap_msg(struct spi_master *master, 619 + struct spi_message *msg) 620 + { 621 + struct spi_transfer *xfer; 622 + 623 + list_for_each_entry(xfer, &msg->transfers, transfer_list) { 624 + /* 625 + * Restore the original value of tx_buf or rx_buf if they are 626 + * NULL. 627 + */ 628 + if (xfer->tx_buf == master->dummy_tx) 629 + xfer->tx_buf = NULL; 630 + if (xfer->rx_buf == master->dummy_rx) 631 + xfer->rx_buf = NULL; 632 + } 633 + 634 + return __spi_unmap_msg(master, msg); 635 + } 608 636 609 637 static int spi_map_msg(struct spi_master *master, struct spi_message *msg) 610 638 {