From 041e6131d1444d07d13ade89349ab4334b61d18c Mon Sep 17 00:00:00 2001 From: Marc Hartmayer Date: Tue, 21 Nov 2023 09:54:37 +0000 Subject: [PATCH] genprotimg/boot: stage3b: check cmdline for null-termination Add a check to the stage3b that the kernel cmdline is always null-terminated. While at it, ensure the coding style is consistent. Reviewed-by: Steffen Eiden Signed-off-by: Marc Hartmayer Signed-off-by: Steffen Eiden --- genprotimg/boot/stage3b.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/genprotimg/boot/stage3b.c b/genprotimg/boot/stage3b.c index 89f17bec..99bceff6 100644 --- a/genprotimg/boot/stage3b.c +++ b/genprotimg/boot/stage3b.c @@ -61,13 +61,17 @@ void __noreturn start(void) if (cmdline->size > get_kernel_cmdline_size()) panic(EINTERNAL, "Command line is too large\n"); - /* move the kernel cmdline */ - memmove((void *)COMMAND_LINE, - (void *)cmdline->src, - cmdline->size); + if (cmdline->size > 0) { + /* make sure the cmdline is a null-terminated string */ + if (((char *)cmdline->src)[cmdline->size - 1] != '\0') + panic(EINTERNAL, "Command line needs to be null-terminated\n"); + + /* move the kernel cmdline */ + memmove((void *)COMMAND_LINE, (void *)cmdline->src, cmdline->size); + } /* the initrd does not need to be moved */ - if (initrd->size != 0) { + if (initrd->size > 0) { /* copy initrd start address and size into new kernel space */ *(unsigned long long *)INITRD_START = initrd->src; *(unsigned long long *)INITRD_SIZE = initrd->size;