Skip to contentSkip to conversation

spi: amd: validate FIFO usage before executing message

PATCH In review Weibin Liu 3 participants 3 messages Oct 27 – Nov 2

Subsystems (2)
lore
Share & tools
lore

Weibin Liu

author
#0 lore raw

PATCH 1/1 spi: amd: validate FIFO usage before executing message

The RX FIFO bound is checked in the drain loop, after amd_spi_execute_opcode(). A message whose cumulative RX length exceeds AMD_SPI_FIFO_SIZE can therefore issue an SPI transaction before being rejected.

Check tx_len + rx_len while walking the message and drop the drain-loop check. This validates the complete message before executing the command.

Tested with a user-space replica covering TX, RX and mixed messages at the 70/71-byte boundary. Oversized split-RX messages are now rejected without executing the command. No AMD SPI hardware was available.

Assisted-by LLM
Fixes e2f4edfc061e ("spi: amd: fix len==0 underflow and FIFO overflow")
---
 drivers/spi/spi-amd.c | 24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)
drivers/spi/spi-amd.c +10 -14
index ddf89e5353c5..d3953de255be 100644
@@ -331,13 +331,6 @@ static inline int amd_spi_fifo_xfer(struct amd_spi *amd_spi,
 		else
 			amd_set_spi_freq(amd_spi, spi->max_speed_hz);
 
-		/* Reject transfers that would overflow the FIFO window */
-		if (fifo_pos + xfer->len >
-		    AMD_SPI_FIFO_BASE + AMD_SPI_FIFO_SIZE) {
-			message->status = -EMSGSIZE;
-			goto fin_msg;
-		}
-
 		if (xfer->tx_buf) {
 			buf = (u8 *)xfer->tx_buf;
 			if (!tx_len) {
@@ -346,6 +339,10 @@ static inline int amd_spi_fifo_xfer(struct amd_spi *amd_spi,
 				xfer->len--;
 			}
 			tx_len += xfer->len;
+			if (tx_len + rx_len > AMD_SPI_FIFO_SIZE) {
+				message->status = -EMSGSIZE;
+				goto fin_msg;
+			}
 
 			/* Write data into the FIFO. */
 			for (i = 0; i < xfer->len; i++)
@@ -355,8 +352,13 @@ static inline int amd_spi_fifo_xfer(struct amd_spi *amd_spi,
 		}
 
 		/* Store no. of bytes to be received from FIFO */
-		if (xfer->rx_buf)
+		if (xfer->rx_buf) {
 			rx_len += xfer->len;
+			if (tx_len + rx_len > AMD_SPI_FIFO_SIZE) {
+				message->status = -EMSGSIZE;
+				goto fin_msg;
+			}
+		}
 	}
 
 	if (!buf) {
@@ -380,12 +382,6 @@ static inline int amd_spi_fifo_xfer(struct amd_spi *amd_spi,
 
 		list_for_each_entry(xfer, &message->transfers, transfer_list)
 			if (xfer->rx_buf) {
-				/* Reject transfers that would overflow the FIFO window */
-				if (fifo_pos + xfer->len >
-				    AMD_SPI_FIFO_BASE + AMD_SPI_FIFO_SIZE) {
-					message->status = -EMSGSIZE;
-					goto fin_msg;
-				}
 				buf = (u8 *)xfer->rx_buf;
 				/* Read data from FIFO to receive buffer */
 				for (i = 0; i < xfer->len; i++)

base-commit: dc5ca34f5e8dc10c566ef39aa3b1a201a17ca734

Signature · 1 line
2.50.1
maintainer Reviewed-by on v1 1/1
#1 lore raw

Hi,

Quoted 12 lines · Weibin Liu

The RX FIFO bound is checked in the drain loop, after amd_spi_execute_opcode(). A message whose cumulative RX length exceeds AMD_SPI_FIFO_SIZE can therefore issue an SPI transaction before being rejected.

Check tx_len + rx_len while walking the message and drop the drain-loop check. This validates the complete message before executing the command.

Tested with a user-space replica covering TX, RX and mixed messages at the 70/71-byte boundary. Oversized split-RX messages are now rejected without executing the command. No AMD SPI hardware was available.

Quoted 5 lines
Assisted-by LLM
Fixes e2f4edfc061e ("spi: amd: fix len==0 underflow and FIFO overflow")

Thanks, patch looks good to me:

Regards,
Krishna

maintainer
#2 lore raw
In reply to Weibin Liu
Quoted 1 line · Weibin Liu

spi: amd: validate FIFO usage before executing message

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git for-7.4

Thanks!

[1/1] spi: amd: validate FIFO usage before executing message

      https://git.kernel.org/broonie/spi/c/aed0c7475d6a

All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying to this mail.

Thanks,
Mark