[PATCH v2 06/10] spi: cadence-quadspi: Validate num-cs against CQSPI_MAX_CHIPSELECT
From: Lee Jones
Date: Fri Nov 27 2026 - 10:32:53 EST
cqspi_of_get_pdata() reads cqspi->num_chipselect from the "num-cs"
Device Tree property without checking that it fits within
CQSPI_MAX_CHIPSELECT. cqspi_setup_flash() then validates each child
node's chip select only against cqspi->num_chipselect before indexing
cqspi->f_pdata[CQSPI_MAX_CHIPSELECT], allowing an out-of-bounds write
if a DT specifies more than CQSPI_MAX_CHIPSELECT chip selects.
Reject "num-cs" values exceeding CQSPI_MAX_CHIPSELECT in
cqspi_of_get_pdata(), and store cs as u32 in cqspi_setup_flash() to
match of_property_read_u32().
Fixes: 0d62c64a8e48 ("spi: cadence-qspi: assert each subnode flash CS is valid")
Closes: https://sashiko.dev/#/bug/linux-cd463fc8-1b94-4bcf-8d06-fae21b88894c
Signed-off-by: Lee Jones <lee@xxxxxxxxxx>
---
v1 => v2:
- Reject num-cs values exceeding CQSPI_MAX_CHIPSELECT in cqspi_of_get_pdata() and store cs as u32 in cqspi_setup_flash(), per Theo Lebrun's review.
- Simplify the commit message and drop the stable tag, per Theo Lebrun's review.
drivers/spi/spi-cadence-quadspi.c | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)
diff --git a/drivers/spi/spi-cadence-quadspi.c b/drivers/spi/spi-cadence-quadspi.c
index 912470bdd10c..0f15250c51db 100644
--- a/drivers/spi/spi-cadence-quadspi.c
+++ b/drivers/spi/spi-cadence-quadspi.c
@@ -1613,8 +1613,13 @@ static int cqspi_of_get_pdata(struct cqspi_st *cqspi)
}
}
- if (of_property_read_u32(np, "num-cs", &cqspi->num_chipselect))
+ if (of_property_read_u32(np, "num-cs", &cqspi->num_chipselect)) {
cqspi->num_chipselect = CQSPI_MAX_CHIPSELECT;
+ } else if (cqspi->num_chipselect > CQSPI_MAX_CHIPSELECT) {
+ dev_err(dev, "num-cs (%u) exceeds maximum (%u)\n",
+ cqspi->num_chipselect, CQSPI_MAX_CHIPSELECT);
+ return -EINVAL;
+ }
cqspi->rclk_en = of_property_read_bool(np, "cdns,rclk-en");
@@ -1753,7 +1758,8 @@ static int cqspi_setup_flash(struct cqspi_st *cqspi)
struct platform_device *pdev = cqspi->pdev;
struct device *dev = &pdev->dev;
struct cqspi_flash_pdata *f_pdata;
- int ret, cs, max_cs = -1;
+ int ret, max_cs = -1;
+ u32 cs;
/* Get flash device data */
for_each_available_child_of_node_scoped(dev->of_node, np) {
@@ -1764,7 +1770,7 @@ static int cqspi_setup_flash(struct cqspi_st *cqspi)
}
if (cs >= cqspi->num_chipselect) {
- dev_err(dev, "Chip select %d out of range.\n", cs);
+ dev_err(dev, "Chip select %u out of range.\n", cs);
return -EINVAL;
}
--
2.56.1.839.g22a8290db9-goog