Re: [PATCH 06/10] spi: cadence-quadspi: Bound chip select against f_pdata array size

From: Théo Lebrun

Date: Thu Nov 19 2026 - 05:16:16 EST


Hello Lee,

On Thu Nov 19, 2026 at 10:17 AM CET, Lee Jones wrote:
> cqspi_setup_flash() uses the chip select taken from each flash child
> node's reg property to index the fixed-size cqspi->f_pdata[] array,
> which has CQSPI_MAX_CHIPSELECT entries.
>
> The range check now compares the chip select against num_chipselect,
> which comes straight from the num-cs property and is never clamped to
> CQSPI_MAX_CHIPSELECT. A device tree which specifies num-cs greater than
> CQSPI_MAX_CHIPSELECT together with a flash node at a correspondingly
> high reg value therefore causes writes beyond the end of f_pdata[].
>
> Reject chip selects which do not fit in f_pdata[] as well as those which
> exceed num-cs. While at it, store the chip select in a u32, matching
> of_property_read_u32(), so that values with the top bit set cannot
> become negative and slip past the bounds checks.

This commit message was hard to read! Can we do better?
The issue at hand is super simple.

Repeating the last sentence:

> While at it, store the chip select in a u32, matching
> of_property_read_u32(), so that values with the top bit set cannot
> become negative and slip past the bounds checks.

Come on! 2147483648 chip selects?

> Fixes: 0d62c64a8e48 ("spi: cadence-qspi: assert each subnode flash CS is valid")
> Closes: https://sashiko.dev/#/bug/linux-cd463fc8-1b94-4bcf-8d06-fae21b88894c
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Lee Jones <lee@xxxxxxxxxx>

Is this worthy of backporting to all stable releases? It is a indeed
a bug fix but which can only occur with a broken DT, probably during
development. It sounds like a lot of churn.

> ---
> drivers/spi/spi-cadence-quadspi.c | 8 +++++---
> 1 file changed, 5 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/spi/spi-cadence-quadspi.c b/drivers/spi/spi-cadence-quadspi.c
> index 844df97111d7..fbb917c104b0 100644
> --- a/drivers/spi/spi-cadence-quadspi.c
> +++ b/drivers/spi/spi-cadence-quadspi.c
> @@ -1751,7 +1751,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) {
> @@ -1761,8 +1762,9 @@ static int cqspi_setup_flash(struct cqspi_st *cqspi)
> return ret;
> }
>
> - if (cs >= cqspi->num_chipselect) {
> - dev_err(dev, "Chip select %d out of range.\n", cs);
> + if (cs >= cqspi->num_chipselect ||
> + cs >= CQSPI_MAX_CHIPSELECT) {
> + dev_err(dev, "Chip select %u out of range.\n", cs);
> return -EINVAL;
> }

OK this does fix the bug. Here is another proposal though:
ensure at probe that cqspi->num_chipselect <= CQSPI_MAX_CHIPSELECT.

Thanks,

--
Théo Lebrun, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com