Skip to contentSkip to conversation

fdt: Fix bounds check in devfdt_get_addr_index

PATCH In review Samuel Holland 2 participants 5 messages Oct 31, 2022 – Nov 18, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fdt: Fix bounds check in devfdt_get_addr_index

reg must contain enough cells for the entire next address/size pair after skipping index pairs. The previous code allows an out-of-bounds read when na + ns > 1.

Fixes 69b41388ba45 ("dm: core: Add a new api to get indexed device address")

---

 drivers/core/fdtaddr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/core/fdtaddr.c +1 -1
index 91bcd1a2c2..50ea05263e 100644
@@ -43,7 +43,7 @@ fdt_addr_t devfdt_get_addr_index(const struct udevice *dev, int index)
 		}
 
 		reg = fdt_getprop(gd->fdt_blob, offset, "reg", &len);
-		if (!reg || (len <= (index * sizeof(fdt32_t) * (na + ns)))) {
+		if (!reg || (len < ((index + 1) * sizeof(fdt32_t) * (na + ns)))) {
 			debug("Req index out of range\n");
 			return FDT_ADDR_T_NONE;
 		}
Signature · 1 line
2.37.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Samuel Holland

reg must contain enough cells for the entire next address/size pair after skipping index pairs. The previous code allows an out-of-bounds read when na + ns > 1.

Fixes 69b41388ba45 ("dm: core: Add a new api to get indexed device address")

---

 drivers/core/fdtaddr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
#2 lore raw

Hi Samuel,

Quoted 7 lines · Simon Glass
Quoted 5 lines · Samuel Holland

reg must contain enough cells for the entire next address/size pair after skipping index pairs. The previous code allows an out-of-bounds read when na + ns > 1.

Quoted 8 lines
Quoted 6 lines
Fixes 69b41388ba45 ("dm: core: Add a new api to get indexed device address")

---

 drivers/core/fdtaddr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Somehow this break PPC in CI:

https://source.denx.de/u-boot/custodians/u-boot-dm/-/jobs/524776

Can you please take a look?

Regards,
SImon

#3 lore raw

Hi Simon,

Quoted 17 lines · Simon Glass

Hi Samuel,

Quoted 14 lines · Simon Glass
Quoted 11 lines · Samuel Holland

reg must contain enough cells for the entire next address/size pair after skipping index pairs. The previous code allows an out-of-bounds read when na + ns > 1.

Fixes 69b41388ba45 ("dm: core: Add a new api to get indexed device address")

---

 drivers/core/fdtaddr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 7 lines
Quoted 1 line

Somehow this break PPC in CI:

https://source.denx.de/u-boot/custodians/u-boot-dm/-/jobs/524776

Can you please take a look?

The cause is the call to lists_bind_fdt() inside serial_check_stdout(), which sets the UART's parent device to gd->dm_root, when the real parent should be a simple-bus node with a ranges property.

Then when we go to probe the UART, devfdt_get_addr_index() does:

	int parent = dev_of_offset(dev->parent);
	// ...
	na = fdt_address_cells(gd->fdt_blob, parent);

So devfdt_get_addr_index() sees the wrong number of address/size cells (2 instead of 1) and the check fails. This only worked previously because the check in devfdt_get_addr_index() would always succeed for index == 0.

This patch fixes things, by setting the UART's parent device correctly:

drivers/serial/ns16550.c +0 -2
@@ -623,9 +623,7 @@
 	.priv_auto	= sizeof(struct ns16550),
 	.probe = ns16550_serial_probe,
 	.ops	= &ns16550_serial_ops,
-#if !CONFIG_IS_ENABLED(OF_CONTROL)
 	.flags	= DM_FLAG_PRE_RELOC,
-#endif
 };
 
 DM_DRIVER_ALIAS(ns16550_serial, ti_da830_uart)

Or maybe devfdt_get_addr_index() should be looking at the FDT node parent, instead of the DM parent. This also fixes things:

drivers/core/fdtaddr.c +1 -1
@@ -22,7 +22,7 @@
 {
 #if CONFIG_IS_ENABLED(OF_REAL)
 	int offset = dev_of_offset(dev);
-	int parent = dev_of_offset(dev->parent);
+	int parent = fdt_parent_offset(gd->fdt_blob, offset);
 	fdt_addr_t addr;
 
 	if (CONFIG_IS_ENABLED(OF_TRANSLATE)) {

Regards,
Samuel

#4 lore raw

Hi Samuel,

Quoted 49 lines · Samuel Holland

Hi Simon,

Quoted 17 lines · Simon Glass

Hi Samuel,

Quoted 14 lines · Simon Glass
Quoted 11 lines · Samuel Holland

reg must contain enough cells for the entire next address/size pair after skipping index pairs. The previous code allows an out-of-bounds read when na + ns > 1.

Fixes 69b41388ba45 ("dm: core: Add a new api to get indexed device address")

---

 drivers/core/fdtaddr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 7 lines
Quoted 1 line

Somehow this break PPC in CI:

https://source.denx.de/u-boot/custodians/u-boot-dm/-/jobs/524776

Can you please take a look?

The cause is the call to lists_bind_fdt() inside serial_check_stdout(), which sets the UART's parent device to gd->dm_root, when the real parent should be a simple-bus node with a ranges property.

Then when we go to probe the UART, devfdt_get_addr_index() does:

        int parent = dev_of_offset(dev->parent);
        // ...
        na = fdt_address_cells(gd->fdt_blob, parent);

So devfdt_get_addr_index() sees the wrong number of address/size cells (2 instead of 1) and the check fails. This only worked previously because the check in devfdt_get_addr_index() would always succeed for index == 0.

This patch fixes things, by setting the UART's parent device correctly:

drivers/serial/ns16550.c line 628

Quoted 6 lines
drivers/serial/ns16550.c +0 -2
@@ -623,9 +623,7 @@
        .priv_auto      = sizeof(struct ns16550),
        .probe = ns16550_serial_probe,
        .ops    = &ns16550_serial_ops,
-#if !CONFIG_IS_ENABLED(OF_CONTROL)
        .flags  = DM_FLAG_PRE_RELOC,
-#endif

We should put the dm tag in the device tree node instead.

Quoted 10 lines
drivers/serial/ns16550.c +0 -0
 };
 
 DM_DRIVER_ALIAS(ns16550_serial, ti_da830_uart)

Or maybe devfdt_get_addr_index() should be looking at the FDT node parent, instead of the DM parent. This also fixes things:

drivers/core/fdtaddr.c line 25

Quoted 5 lines
drivers/core/fdtaddr.c +1 -1
@@ -22,7 +22,7 @@
 {
 #if CONFIG_IS_ENABLED(OF_REAL)
        int offset = dev_of_offset(dev);
-       int parent = dev_of_offset(dev->parent);
+       int parent = fdt_parent_offset(gd->fdt_blob, offset);

That is slow, best avoided.

drivers/core/fdtaddr.c line 28

Quoted 3 lines
drivers/core/fdtaddr.c +0 -0
        fdt_addr_t addr;
 
        if (CONFIG_IS_ENABLED(OF_TRANSLATE)) {

Regards,
Simon