Skip to contentSkip to conversation

riscv: cpuinfo: Fix NULL dereference and inconsistent error code

PATCH In review Mohamed Ayman 2 participants 4 messages May 2 – May 3 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: cpuinfo: Fix NULL dereference and inconsistent error code

Fix a potential NULL pointer dereference in c_show() by ensuring the device tree node returned by of_get_cpu_node() is checked before use.

Also replace a non-standard return value (-1) in riscv_of_parent_hartid() with -ENODEV to follow Linux kernel conventions for "not found" error returns.

Both issues improve robustness and consistency with existing RISC-V CPU info handling code.

v2: Add missing Signed-off-by

---
 arch/riscv/kernel/cpu.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
arch/riscv/kernel/cpu.c +7 -6
index 3dbc8cc557dd..e0bc7526861f 100644
@@ -135,7 +135,7 @@ int riscv_of_parent_hartid(struct device_node *node, unsigned long *hartid)
 		}
 	}
 
-	return -1;
+	return -ENODEV;
 }
 
 unsigned long __init riscv_get_marchid(void)
@@ -348,12 +348,13 @@ static int c_show(struct seq_file *m, void *v)
 
 	if (acpi_disabled) {
 		node = of_get_cpu_node(cpu_id, NULL);
+		if (node) {
+			if (!of_property_read_string(node, "compatible", &compat) &&
+				strcmp(compat, "riscv"))
+				seq_printf(m, "uarch\t\t: %s\n", compat);
 
-		if (!of_property_read_string(node, "compatible", &compat) &&
-		    strcmp(compat, "riscv"))
-			seq_printf(m, "uarch\t\t: %s\n", compat);
-
-		of_node_put(node);
+			of_node_put(node);
+		}
 	}
 
 	seq_printf(m, "mvendorid\t: 0x%lx\n", ci->mvendorid);
Signature · 1 line
2.34.1
#1 lore raw
Quoted 2 lines · Mohamed Ayman

Fix a potential NULL pointer dereference in c_show() by ensuring the device tree node returned by of_get_cpu_node() is checked before use.

Where is the NULL pointer deref?

of_property_read_string() can cope with a NULL np, it just returns an error.

of_node_put() also handles a NULL np.

Quoted 3 lines

Also replace a non-standard return value (-1) in riscv_of_parent_hartid() with -ENODEV to follow Linux kernel conventions for "not found" error returns.

That should be a separate patch. It does look like a good change. At least some of the callers of riscv_of_parent_hartid() pass the return value up to their callers, so -1 could be misinterpreted somewhere as EPERM.

cheers

#2 lore raw

Thanks for the review Michael.

You're right, both of_property_read_string() and of_node_put() handle NULL np internally, so the c_show() hunk is unnecessary. I'll drop it in v3.

I'll also send the -ENODEV fix as a standalone patch as you suggested. :)

Quoted 21 lines · Michael Ellerman
Quoted 2 lines · Mohamed Ayman

Fix a potential NULL pointer dereference in c_show() by ensuring the device tree node returned by of_get_cpu_node() is checked before use.

Where is the NULL pointer deref?

of_property_read_string() can cope with a NULL np, it just returns an error.

of_node_put() also handles a NULL np.

Quoted 3 lines

Also replace a non-standard return value (-1) in riscv_of_parent_hartid() with -ENODEV to follow Linux kernel conventions for "not found" error returns.

That should be a separate patch. It does look like a good change. At least some of the callers of riscv_of_parent_hartid() pass the return value up to their callers, so -1 could be misinterpreted somewhere as EPERM.

cheers

#3 lore raw

PATCH v3 1/1 riscv: Replace non-standard -1 return with -ENODEV in riscv_of_parent_hartid()

In reply to Mohamed Ayman

riscv_of_parent_hartid() returns -1 when no valid hart ID is found among the parent nodes. This is non-standard; Linux kernel convention for a "not found" condition is -ENODEV. Additionally, since several callers propagate this return value up the call chain, a raw -1 could be misinterpreted as -EPERM by code inspecting errno values.

Replace the -1 with -ENODEV for clarity and consistency with the rest of the RISC-V CPU handling code.

---
 arch/riscv/kernel/cpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/cpu.c +1 -1
index 3dbc8cc55..414cd55df 100644
@@ -135,7 +135,7 @@ int riscv_of_parent_hartid(struct device_node *node, unsigned long *hartid)
 		}
 	}
 
-	return -1;
+	return -ENODEV;
 }
 
 unsigned long __init riscv_get_marchid(void)
Signature · 1 line
2.34.1