Skip to contentSkip to conversation

riscv: pi: validate early bootargs before string use

PATCH In review Pengpeng Hou 2 participants 2 messages Apr 3 – Nov 25 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: pi: validate early bootargs before string use

get_early_cmdline() fetches bootargs directly from the FDT and immediately passes the raw property pointer to strlen() before copying it into the early command-line buffer. Flat DT properties are external boot input, and this path does not prove that bootargs is NUL-terminated within its declared bounds.

Use fdt_stringlist_get() so malformed unterminated bootargs are rejected before any C-string helpers walk them.

---
 arch/riscv/kernel/pi/cmdline_early.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
arch/riscv/kernel/pi/cmdline_early.c +6 -5
index 389d086a0718..ac55f1c7ca70 100644
@@ -13,16 +13,17 @@ static char early_cmdline[COMMAND_LINE_SIZE];
 static char *get_early_cmdline(uintptr_t dtb_pa)
 {
 	const char *fdt_cmdline = NULL;
-	unsigned int fdt_cmdline_size = 0;
+	int fdt_cmdline_size = 0;
 	int chosen_node;
 
 	if (!IS_ENABLED(CONFIG_CMDLINE_FORCE)) {
 		chosen_node = fdt_path_offset((void *)dtb_pa, "/chosen");
 		if (chosen_node >= 0) {
-			fdt_cmdline = fdt_getprop((void *)dtb_pa, chosen_node,
-						  "bootargs", NULL);
-			if (fdt_cmdline) {
-				fdt_cmdline_size = strlen(fdt_cmdline);
+			fdt_cmdline = fdt_stringlist_get((void *)dtb_pa,
+							 chosen_node,
+							 "bootargs", 0,
+							 &fdt_cmdline_size);
+			if (fdt_cmdline && fdt_cmdline_size > 0) {
 				strscpy(early_cmdline, fdt_cmdline,
 					COMMAND_LINE_SIZE);
 			}
Signature · 1 line
2.50.1 (Apple Git-155)
maintainer
#1 lore raw
Quoted 10 lines · Pengpeng Hou

get_early_cmdline() fetches bootargs directly from the FDT and immediately passes the raw property pointer to strlen() before copying it into the early command-line buffer. Flat DT properties are external boot input, and this path does not prove that bootargs is NUL-terminated within its declared bounds.

Use fdt_stringlist_get() so malformed unterminated bootargs are rejected before any C-string helpers walk them.

Sashiko points out that this could cause a regression in the CONFIG_CMDLINE_FALLBACK-related code, later in get_early_cmdline(). The CONFIG_CMDLINE_FALLBACK-related code only tests for fdt_cmdline_size == 0, but fdt_stringlist_get() could pass back a negative error code in fdt_cmdline_size under some error conditions.

Care to fix that and resubmit?

thanks,

- Paul