Linux-PHY Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] phy: berlin-sata: count available child nodes
@ 2026-11-19  8:15 Wang Haoran
  2026-11-19  8:23 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Wang Haoran @ 2026-11-19  8:15 UTC (permalink / raw)
  To: vkoul; +Cc: neil.armstrong, mani, linux-phy, linux-kernel, wanghr2026

phy_berlin_sata_probe() sizes priv->nphys with of_get_child_count(), which
counts every child, but fills priv->phys[] only from available children. A
disabled child therefore leaves trailing NULL entries, and
phy_berlin_sata_phy_xlate() dereferences priv->phys[i]->index for them. A
device tree that disables a PHY but still points a consumer at it crashes
with a NULL dereference in the generic ahci_platform path during probe.

Count available children instead.

Signed-off-by: Wang Haoran <wanghr2026@mail.zgclab.edu.cn>
Fixes: 942a31b52191 ("phy: add a driver for the Berlin SATA PHY")
---

diff --git a/drivers/phy/marvell/phy-berlin-sata.c b/drivers/phy/marvell/phy-berlin-sata.c
index 084671ee7d56..6ed9c8e2b6f4 100644
--- a/drivers/phy/marvell/phy-berlin-sata.c
+++ b/drivers/phy/marvell/phy-berlin-sata.c
@@ -212,7 +212,7 @@ static int phy_berlin_sata_probe(struct platform_device *pdev)
 	if (IS_ERR(priv->clk))
 		return PTR_ERR(priv->clk);
 
-	priv->nphys = of_get_child_count(dev->of_node);
+	priv->nphys = of_get_available_child_count(dev->of_node);
 	if (priv->nphys == 0)
 		return -ENODEV;
 

---

PoC reproduction (7.4.0-rc2, arm64 QEMU virt, unmodified kernel):

Device tree injection, spliced into the QEMU virt machine DT:

    osc25m: clock-25m { compatible = "fixed-clock"; #clock-cells = <0>;
        clock-frequency = <25000000>; };

    reserved-memory { #address-cells = <2>; #size-cells = <2>; ranges;
        phy-regs@7f100000 { no-map; reg = <0x0 0x7f100000 0x0 0x1000>; };
        ahci-regs@7f110000 { no-map; reg = <0x0 0x7f110000 0x0 0x1000>; }; };

    sata_phy: sata-phy@7f100000 { compatible = "marvell,berlin2q-sata-phy";
        reg = <0x0 0x7f100000 0x0 0x1000>; clocks = <&osc25m>;
        #phy-cells = <1>;
        phy@0 { reg = <0>; };
        phy@1 { reg = <1>; status = "disabled"; }; };

    sata@7f110000 { compatible = "generic-ahci"; reg = <0x0 0x7f110000 0x0 0x1000>;
        interrupt-parent = <&gpio0>; interrupts = <5 8>; phys = <&sata_phy 1>; };

phy@1 is disabled but the generic-ahci consumer still points at it via
phys = <&sata_phy 1>, so xlate() walks a NULL slot.

Trigger:

    modprobe phy-berlin-sata
    modprobe ahci_platform

Result:

    Unable to handle kernel NULL pointer dereference at virtual address 000000000000000c
    pc : phy_berlin_sata_phy_xlate+0x30/0x60 [phy_berlin_sata]
    Call trace: ahci_platform_get_phy then ahci_probe

Note: the PoC was generated with AI assistance and the vulnerability was
found by static analysis. The device tree is trusted input (a bad DT
requires root or physical access), so this is not a security fix, but it
prevents a potential resource leak or system failure on a failed probe or
an old or malformed DT.


-- 
linux-phy mailing list
linux-phy@lists.infradead.org
https://lists.infradead.org/mailman/listinfo/linux-phy

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] phy: berlin-sata: count available child nodes
  2026-11-19  8:15 [PATCH] phy: berlin-sata: count available child nodes Wang Haoran
@ 2026-11-19  8:23 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-19  8:23 UTC (permalink / raw)
  To: Wang Haoran; +Cc: linux-phy, vkoul, neil.armstrong, olteanv

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] phy: berlin-sata: wrong bounds check in
  phy_berlin_sata_phy_xlate()
--

commit 0529df9ae940f51aa712838077eb6b3ead50665d
Author: Wang Haoran <wanghr2026@mail.zgclab.edu.cn>

phy: berlin-sata: count available child nodes

This commit sizes priv->nphys with of_get_available_child_count() so that
priv->phys[] matches the number of available child nodes populated during
probe.

> diff --git a/drivers/phy/marvell/phy-berlin-sata.c b/drivers/phy/marvell/phy-berlin-sata.c
> index c90e2867900c3..1d3afc2eb9535 100644
> --- a/drivers/phy/marvell/phy-berlin-sata.c
> +++ b/drivers/phy/marvell/phy-berlin-sata.c
> @@ -212,7 +212,7 @@ static int phy_berlin_sata_probe(struct platform_device *pdev)
>  	if (IS_ERR(priv->clk))
>  		return PTR_ERR(priv->clk);
>  
> -	priv->nphys = of_get_child_count(dev->of_node);
> +	priv->nphys = of_get_available_child_count(dev->of_node);
>  	if (priv->nphys == 0)
>  		return -ENODEV;

[Severity: Medium]
Does shrinking priv->nphys to the available child count break lookups of
PHY 1 when PHY 0 is disabled?

drivers/phy/marvell/phy-berlin-sata.c:phy_berlin_sata_phy_xlate() {
    ...
	if (WARN_ON(args->args[0] >= priv->nphys))
		return ERR_PTR(-ENODEV);

	for (i = 0; i < priv->nphys; i++) {
		if (priv->phys[i]->index == args->args[0])
			break;
	}
    ...
}

args->args[0] is the hardware PHY index (0 or 1), which the loop matches
against priv->phys[i]->index.  When PHY 0 is disabled and PHY 1 is enabled,
priv->nphys is 1 and priv->phys[0]->index is 1, so requesting PHY 1 trips
WARN_ON(args->args[0] >= priv->nphys) and fails with -ENODEV before reaching
the lookup loop.

Should phy_berlin_sata_phy_xlate() bound-check args->args[0] against
ARRAY_SIZE(phy_berlin_power_down_bits), matching phy_berlin_sata_probe()?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/1795076106646-11-85fca065@mail.zgclab.edu.cn?part=1

-- 
linux-phy mailing list
linux-phy@lists.infradead.org
https://lists.infradead.org/mailman/listinfo/linux-phy

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-19  8:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19  8:15 [PATCH] phy: berlin-sata: count available child nodes Wang Haoran
2026-11-19  8:23 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox