All of lore.kernel.org
 help / color / mirror / Atom feed
From: Lee Jones <lee@kernel.org>
To: lee@kernel.org, "Mark Brown" <broonie@kernel.org>,
	"Foon Tan" <lftan@altera.com>,
	"Graham Moore" <grmoore@opensource.altera.com>,
	"Murugan R" <vadivel.muruganx.ramuthevar@intel.com>,
	"Vignesh Raghavendra" <vigneshr@ti.com>,
	"Pratyush Yadav" <p.yadav@ti.com>, "Dhruva Gole" <d-gole@ti.com>,
	"Théo Lebrun" <theo.lebrun@bootlin.com>,
	linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2 06/10] spi: cadence-quadspi: Validate num-cs against CQSPI_MAX_CHIPSELECT
Date: Fri, 27 Nov 2026 15:31:09 +0000	[thread overview]
Message-ID: <20261127153158.2969573-7-lee@kernel.org> (raw)
In-Reply-To: <20261127153158.2969573-1-lee@kernel.org>

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@kernel.org>
---
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


  parent reply	other threads:[~2026-11-27 15:32 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 15:31 [PATCH v2 00/10] spi: Fix DMA, bounds, clock, and chip-select bugs Lee Jones
2026-11-27 15:31 ` [PATCH v2 01/10] spi: fsl-cpm: Map the zero page's address, not its struct page Lee Jones
2026-11-27 15:31 ` [PATCH v2 02/10] spi: bcm2835: Store native chip-select GPIO descriptor per device Lee Jones
2026-11-27 15:31 ` [PATCH v2 03/10] spi: ch341: Limit transfers to the hardware packet size Lee Jones
2026-11-27 15:31 ` [PATCH v2 04/10] spi: sprd-adi: Do not copy uninitialised data to rx_buf on read failure Lee Jones
2026-11-27 15:31 ` [PATCH v2 05/10] spi: topcliff-pch: Advance tx_buf between DMA chunks Lee Jones
2026-11-27 15:31 ` Lee Jones [this message]
2026-11-27 17:22   ` [PATCH v2 06/10] spi: cadence-quadspi: Validate num-cs against CQSPI_MAX_CHIPSELECT Théo Lebrun
2026-11-27 15:31 ` [PATCH v2 07/10] spi: cadence-xspi: Zero the tail word in m_iowriteq() Lee Jones
2026-11-27 15:31 ` [PATCH v2 08/10] spi: fsl-qspi: Propagate clock errors from fsl_qspi_select_mem() Lee Jones
2026-11-30  3:32   ` Bough Chen
2026-12-01  9:05   ` Frieder Schrempf
2026-11-27 15:31 ` [PATCH v2 09/10] spi: cadence-xspi: Advance tx pointer between Marvell b0 chunks Lee Jones
2026-11-27 15:31 ` [PATCH v2 10/10] spi: cadence-xspi: Skip tail read for qword-aligned Marvell b0 receives Lee Jones

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261127153158.2969573-7-lee@kernel.org \
    --to=lee@kernel.org \
    --cc=broonie@kernel.org \
    --cc=d-gole@ti.com \
    --cc=grmoore@opensource.altera.com \
    --cc=lftan@altera.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-spi@vger.kernel.org \
    --cc=p.yadav@ti.com \
    --cc=theo.lebrun@bootlin.com \
    --cc=vadivel.muruganx.ramuthevar@intel.com \
    --cc=vigneshr@ti.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.