[PATCH] spi: dln2: validate frame sizes reported by device
From: Zhiqi Zhang
Date: Thu Nov 26 2026 - 01:27:37 EST
dln2_spi_get_supported_frame_sizes() builds the SPI bits_per_word_mask
directly from the frame sizes reported by the DLN2 device. The response
length is checked and the count is bounded, but an empty list and each
advertised frame size are not validated.
A zero count leaves bits_per_word_mask empty, which makes the SPI core
fall back to treating the controller as if it had no explicit mask. A
frame size of 0 makes BIT(rx->frame_sizes[i] - 1) evaluate a negative
shift count. Values greater than 32 are outside the u32
bits_per_word_mask representation used by the SPI core and can also
trigger an out-of-bounds shift.
Reject an empty frame-size list and frame sizes outside 1..32 with
-EPROTO before adding them to the mask, and use SPI_BPW_MASK() to make
the mask construction match the SPI core convention.
I found this while reviewing DLN2 capability parsing. Codex modified the
code, and I reviewed the change.
Fixes: 3d8c0d749da3 ("spi: add support for DLN-2 USB-SPI adapter")
Signed-off-by: Zhiqi Zhang <zhiqizhangg@xxxxxxxxx>
---
Tested with ARCH=arm64 W=1 drivers/spi/spi-dln2.o and CONFIG_SPI_DLN2=m
in a Docker gcc:16 environment.
I do not have DLN2 hardware for runtime testing.
drivers/spi/spi-dln2.c | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)
diff --git a/drivers/spi/spi-dln2.c b/drivers/spi/spi-dln2.c
index 60085ed04a..ec673c650f 100644
--- a/drivers/spi/spi-dln2.c
+++ b/drivers/spi/spi-dln2.c
@@ -71,6 +71,7 @@
#define DLN2_SPI_GET_MAX_DELAY_BETWEEN_FRAMES DLN2_SPI_CMD(0x4C)
#define DLN2_SPI_MAX_XFER_SIZE 256
+#define DLN2_SPI_MAX_FRAME_SIZE 32
#define DLN2_SPI_BUF_SIZE (DLN2_SPI_MAX_XFER_SIZE + 16)
#define DLN2_SPI_ATTR_LEAVE_SS_LOW BIT(0)
#define DLN2_TRANSFERS_WAIT_COMPLETE 1
@@ -336,12 +337,17 @@ static int dln2_spi_get_supported_frame_sizes(struct dln2_spi *dln2,
return ret;
if (rx_len < sizeof(*rx))
return -EPROTO;
- if (rx->count > ARRAY_SIZE(rx->frame_sizes))
+ if (!rx->count || rx->count > ARRAY_SIZE(rx->frame_sizes))
return -EPROTO;
*bpw_mask = 0;
- for (i = 0; i < rx->count; i++)
- *bpw_mask |= BIT(rx->frame_sizes[i] - 1);
+ for (i = 0; i < rx->count; i++) {
+ if (!rx->frame_sizes[i] ||
+ rx->frame_sizes[i] > DLN2_SPI_MAX_FRAME_SIZE)
+ return -EPROTO;
+
+ *bpw_mask |= SPI_BPW_MASK(rx->frame_sizes[i]);
+ }
dev_dbg(&dln2->pdev->dev, "bpw_mask = 0x%X\n", *bpw_mask);
base-commit: 7ccb044c27720d48175577080b851f047ddcfd1c
--
2.54.0 (Apple Git-157)