[PATCH 5.8 147/148] efi/libstub: Handle unterminated cmdline

From: Greg Kroah-Hartman
Date: Mon Aug 24 2020 - 05:52:38 EST


From: Arvind Sankar <nivedita@xxxxxxxxxxxx>

commit 8a8a3237a78cbc0557f0eb16a89f16d616323e99 upstream.

Make the command line parsing more robust, by handling the case it is
not NUL-terminated.

Use strnlen instead of strlen, and make sure that the temporary copy is
NUL-terminated before parsing.

Cc: <stable@xxxxxxxxxxxxxxx>
Signed-off-by: Arvind Sankar <nivedita@xxxxxxxxxxxx>
Link: https://lore.kernel.org/r/20200813185811.554051-4-nivedita@xxxxxxxxxxxx
Signed-off-by: Ard Biesheuvel <ardb@xxxxxxxxxx>
Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>

---
drivers/firmware/efi/libstub/efi-stub-helper.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)

--- a/drivers/firmware/efi/libstub/efi-stub-helper.c
+++ b/drivers/firmware/efi/libstub/efi-stub-helper.c
@@ -194,12 +194,14 @@ efi_status_t efi_parse_options(char cons
if (!cmdline)
return EFI_SUCCESS;

- len = strlen(cmdline) + 1;
+ len = strnlen(cmdline, COMMAND_LINE_SIZE - 1) + 1;
status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, len, (void **)&buf);
if (status != EFI_SUCCESS)
return status;

- str = skip_spaces(memcpy(buf, cmdline, len));
+ memcpy(buf, cmdline, len - 1);
+ buf[len - 1] = '\0';
+ str = skip_spaces(buf);

while (*str) {
char *param, *val;