[PATCH 02/13] mmc: cavium: Prevent slot_pdev[] out-of-bounds write during probe

From: Lee Jones

Date: Mon Nov 23 2026 - 14:17:41 EST


octeon_mmc_probe() and thunder_mmc_probe() iterate over Device Tree
child nodes and store each created slot platform device in
host->slot_pdev[i], which is sized to CAVIUM_MAX_MMC (4). Neither
function checks that i remains below CAVIUM_MAX_MMC before writing to
host->slot_pdev[i], and both increment i even for disabled or
non-mmc-slot child nodes. When a controller node has more than four
child nodes (or four valid mmc-slot nodes alongside another child node
on ThunderX), host->slot_pdev[i] writes past the end of the array and
corrupts the adjacent set_shared_power and acquire_bus callback
pointers in struct cvm_mmc_host.

Iterate only over available child nodes, advance the slot_pdev[] index
only when a slot platform device is created, and fail the probe with
-EINVAL if more than CAVIUM_MAX_MMC slots are present.

Fixes: 8fb83b142823 ("mmc: cavium: Fix probing race with regulator")
Fixes: 166bac38c3c5 ("mmc: cavium: Add MMC PCI driver for ThunderX SOCs")
Closes: https://sashiko.dev/#/bug/linux-022f7ab4-a7fe-4881-b87d-613d4b8f010f
Closes: https://sashiko.dev/#/bug/linux-04e9d7cc-573a-4ea4-ab4e-d65b9b0626ac
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Lee Jones <lee@xxxxxxxxxx>
---
drivers/mmc/host/cavium-octeon.c | 13 +++++++++----
drivers/mmc/host/cavium-thunderx.c | 11 +++++++++--
2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/drivers/mmc/host/cavium-octeon.c b/drivers/mmc/host/cavium-octeon.c
index 7b75b3dfd3e6..375d8f23b26e 100644
--- a/drivers/mmc/host/cavium-octeon.c
+++ b/drivers/mmc/host/cavium-octeon.c
@@ -262,13 +262,18 @@ static int octeon_mmc_probe(struct platform_device *pdev)
platform_set_drvdata(pdev, host);

i = 0;
- for_each_child_of_node_scoped(node, cn) {
+ for_each_available_child_of_node_scoped(node, cn) {
+ if (i >= CAVIUM_MAX_MMC) {
+ dev_err(&pdev->dev, "Too many slots\n");
+ ret = -EINVAL;
+ octeon_mmc_set_shared_power(host, 0);
+ goto error;
+ }
host->slot_pdev[i] =
of_platform_device_create(cn, NULL, &pdev->dev);
- if (!host->slot_pdev[i]) {
- i++;
+ if (!host->slot_pdev[i])
continue;
- }
+
ret = cvm_mmc_of_slot_probe(&host->slot_pdev[i]->dev, host);
if (ret) {
dev_err(&pdev->dev, "Error populating slots\n");
diff --git a/drivers/mmc/host/cavium-thunderx.c b/drivers/mmc/host/cavium-thunderx.c
index b8296b2de127..a5346fb96f9b 100644
--- a/drivers/mmc/host/cavium-thunderx.c
+++ b/drivers/mmc/host/cavium-thunderx.c
@@ -129,7 +129,7 @@ static int thunder_mmc_probe(struct pci_dev *pdev,
if (ret)
goto error;

- for_each_child_of_node(node, child_node) {
+ for_each_available_child_of_node(node, child_node) {
/*
* mmc_of_parse and devm* require one device per slot.
* Create a dummy device per slot and set the node pointer to
@@ -137,6 +137,13 @@ static int thunder_mmc_probe(struct pci_dev *pdev,
* of_platform_device_create.
*/
if (of_device_is_compatible(child_node, "mmc-slot")) {
+ if (i >= CAVIUM_MAX_MMC) {
+ dev_err(dev, "Too many slots\n");
+ of_node_put(child_node);
+ ret = -EINVAL;
+ goto error;
+ }
+
host->slot_pdev[i] = of_platform_device_create(child_node, NULL,
&pdev->dev);
if (!host->slot_pdev[i])
@@ -147,8 +154,8 @@ static int thunder_mmc_probe(struct pci_dev *pdev,
of_node_put(child_node);
goto error;
}
+ i++;
}
- i++;
}
dev_info(dev, "probed\n");
return 0;
--
2.56.1.750.gf70dc682cf-goog