[PATCH v2] riscv: pi: check the extent of early bootargs before copying

From: Pengpeng Hou

Date: Wed Dec 02 2026 - 22:45:33 EST


strscpy() limits the size of the early command-line destination, not the
size of the FDT property. Check that bootargs contains a terminator
within its own extent before using it as a string.

For absent or malformed bootargs, leave the copied length at zero so
CONFIG_CMDLINE_FALLBACK still selects the built-in command line. Keep
the existing treatment of a valid but truncated command line.

Signed-off-by: Pengpeng Hou <pengpeng@xxxxxxxxxxx>
---
Changes since v1:
Keep the property length separate from the copied length. A libfdt error
cannot become a destination offset or suppress fallback.
Previous version: https://lore.kernel.org/r/20260403143003.3-riscv-pi-bootargs-pengpeng@xxxxxxxxxxx

Codex assisted with patch preparation and the changelog.
Current review: Codex (Assisted-by: gpt 5).
Static review only; this revision was not built.

arch/riscv/kernel/pi/cmdline_early.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/arch/riscv/kernel/pi/cmdline_early.c b/arch/riscv/kernel/pi/cmdline_early.c
index 0afbe4077cb8ffba380e9b1862e711b6323468db..7fa2a9255ad5c524a95d908c69d63967db1e1c83 100644
--- a/arch/riscv/kernel/pi/cmdline_early.c
+++ b/arch/riscv/kernel/pi/cmdline_early.c
@@ -14,14 +14,15 @@ static char *get_early_cmdline(uintptr_t dtb_pa)
{
const char *fdt_cmdline;
ssize_t fdt_cmdline_size = 0;
- int chosen_node;
+ int chosen_node, len;

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) {
+ "bootargs", &len);
+ if (fdt_cmdline && len > 0 &&
+ memchr(fdt_cmdline, '\0', len)) {
fdt_cmdline_size = strscpy(early_cmdline, fdt_cmdline);
if (fdt_cmdline_size < 0)
return early_cmdline;

base-commit: 0810bd27d7bbccb4f579f1fbb1afc98687c4c7c3