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

spi/xilinx: Fix access invalid memory on xilinx_spi_tx

On 1 and 2 bytes per word, the transfer of the 3 last bytes will access
memory outside tx_ptr.

Although this has not trigger any error on real hardware, we should
better fix this.

Fixes: 24ba5e593f391507 (Remove rx_fn and tx_fn pointer)
Signed-off-by: Ricardo Ribalda Delgado <ricardo.ribalda@gmail.com>
Signed-off-by: Mark Brown <broonie@kernel.org>

authored by

Ricardo Ribalda Delgado and committed by
Mark Brown
34093cb9 0635287a

+16 -1
+16 -1
drivers/spi/spi-xilinx.c
··· 117 117 118 118 static void xilinx_spi_tx(struct xilinx_spi *xspi) 119 119 { 120 + u32 data = 0; 121 + 120 122 if (!xspi->tx_ptr) { 121 123 xspi->write_fn(0, xspi->regs + XSPI_TXD_OFFSET); 122 124 return; 123 125 } 124 - xspi->write_fn(*(u32 *)(xspi->tx_ptr), xspi->regs + XSPI_TXD_OFFSET); 126 + 127 + switch (xspi->bytes_per_word) { 128 + case 1: 129 + data = *(u8 *)(xspi->tx_ptr); 130 + break; 131 + case 2: 132 + data = *(u16 *)(xspi->tx_ptr); 133 + break; 134 + case 4: 135 + data = *(u32 *)(xspi->tx_ptr); 136 + break; 137 + } 138 + 139 + xspi->write_fn(data, xspi->regs + XSPI_TXD_OFFSET); 125 140 xspi->tx_ptr += xspi->bytes_per_word; 126 141 } 127 142