From 76a286530a8b5bdbab81c3851b851dea2da32114 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Mon, 13 Feb 2023 20:40:42 +0000 Subject: [image] Check delimiters when parsing command-line key-value arguments The Linux kernel bzImage image format and the CPIO archive constructor will parse the image command line for certain arguments of the form "key=value". This parsing is currently implemented using strstr() in a way that can cause a false positive suffix match. For example, a command line containing "highmem=" would erroneously be treated as containing a value for "mem=". Fix by centralising the logic used for parsing such arguments, and including a check that the argument immediately follows a whitespace delimiter (or is at the start of the string). Reported-by: Filippo Giunchedi Signed-off-by: Michael Brown --- src/core/image.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'src/core/image.c') diff --git a/src/core/image.c b/src/core/image.c index 3e236ca60..f6d3d8ddd 100644 --- a/src/core/image.c +++ b/src/core/image.c @@ -27,6 +27,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); #include #include #include +#include #include #include #include @@ -569,3 +570,33 @@ struct image * image_memory ( const char *name, userptr_t data, size_t len ) { err_alloc_image: return NULL; } + +/** + * Find argument within image command line + * + * @v image Image + * @v key Argument search key (including trailing delimiter) + * @ret value Argument value, or NULL if not found + */ +const char * image_argument ( struct image *image, const char *key ) { + const char *cmdline = image->cmdline; + const char *search; + const char *match; + const char *next; + + /* Find argument */ + for ( search = cmdline ; search ; search = next ) { + + /* Find next occurrence, if any */ + match = strstr ( search, key ); + if ( ! match ) + break; + next = ( match + strlen ( key ) ); + + /* Check preceding delimiter, if any */ + if ( ( match == cmdline ) || isspace ( match[-1] ) ) + return next; + } + + return NULL; +} -- cgit v1.2.3-55-g7522 From e51e7bbad7d043a6b369b0050ff4e1c0e62f3b5d Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Mon, 6 Mar 2023 16:55:54 +0000 Subject: [image] Consistently use for_each_image() to iterate over images Signed-off-by: Michael Brown --- src/core/image.c | 2 +- src/interface/efi/efi_file.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/core/image.c') diff --git a/src/core/image.c b/src/core/image.c index f6d3d8ddd..5a9aebc74 100644 --- a/src/core/image.c +++ b/src/core/image.c @@ -312,7 +312,7 @@ void unregister_image ( struct image *image ) { struct image * find_image ( const char *name ) { struct image *image; - list_for_each_entry ( image, &images, list ) { + for_each_image ( image ) { if ( strcmp ( image->name, name ) == 0 ) return image; } diff --git a/src/interface/efi/efi_file.c b/src/interface/efi/efi_file.c index 7bcf8d597..f2b44fa73 100644 --- a/src/interface/efi/efi_file.c +++ b/src/interface/efi/efi_file.c @@ -130,7 +130,7 @@ static struct image * efi_file_find ( const char *name ) { struct image *image; /* Find image */ - list_for_each_entry ( image, &images, list ) { + for_each_image ( image ) { if ( strcasecmp ( image->name, name ) == 0 ) return image; } -- cgit v1.2.3-55-g7522 From 9e1f7a3659071004f4b8c76f2593da6287f0d575 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Mon, 6 Mar 2023 16:28:48 +0000 Subject: [image] Always unregister currently executing image We unregister script images during their execution, to prevent a "boot" command from re-executing the containing script. This also has the side effect of preventing executing scripts from showing up within the Linux magic initrd image (or the Multiboot module list). Additional logic in bzimage.c and efi_file.c prevents a currently executing kernel from showing up within the magic initrd image. Similar logic in multiboot.c prevents the Multiboot kernel from showing up as a Multiboot module. This still leaves some corner cases that are not covered correctly. For example: when using a gzip-compressed kernel image, nothing will currently hide the original compressed image from the magic initrd. Fix by moving the logic that temporarily unregisters the current image from script_exec() to image_exec(), so that it applies to all image types, and simplify the magic initrd and Multiboot module list construction logic on the basis that no further filtering of the registered image list is necessary. This change has the side effect of hiding currently executing EFI images from the virtual filesystem exposed by iPXE. For example, when using iPXE to boot wimboot, the wimboot binary itself will no longer be visible within the virtual filesystem. Signed-off-by: Michael Brown --- src/arch/x86/image/bzimage.c | 8 -------- src/arch/x86/image/multiboot.c | 4 ---- src/core/image.c | 12 +++++++++--- src/image/script.c | 9 --------- src/interface/efi/efi_file.c | 6 +----- 5 files changed, 10 insertions(+), 29 deletions(-) (limited to 'src/core/image.c') diff --git a/src/arch/x86/image/bzimage.c b/src/arch/x86/image/bzimage.c index b40bb2d07..b15bd5563 100644 --- a/src/arch/x86/image/bzimage.c +++ b/src/arch/x86/image/bzimage.c @@ -355,10 +355,6 @@ static size_t bzimage_load_initrd ( struct image *image, size_t offset; size_t pad_len; - /* Do not include kernel image itself as an initrd */ - if ( initrd == image ) - return 0; - /* Create cpio header for non-prebuilt images */ offset = cpio_header ( initrd, &cpio ); @@ -406,10 +402,6 @@ static int bzimage_check_initrds ( struct image *image, /* Calculate total loaded length of initrds */ for_each_image ( initrd ) { - /* Skip kernel */ - if ( initrd == image ) - continue; - /* Calculate length */ len += bzimage_load_initrd ( image, initrd, UNULL ); len = bzimage_align ( len ); diff --git a/src/arch/x86/image/multiboot.c b/src/arch/x86/image/multiboot.c index 0c85df708..c1c63bc97 100644 --- a/src/arch/x86/image/multiboot.c +++ b/src/arch/x86/image/multiboot.c @@ -204,10 +204,6 @@ static int multiboot_add_modules ( struct image *image, physaddr_t start, break; } - /* Do not include kernel image itself as a module */ - if ( module_image == image ) - continue; - /* Page-align the module */ start = ( ( start + 0xfff ) & ~0xfff ); diff --git a/src/core/image.c b/src/core/image.c index 5a9aebc74..b280eb4d3 100644 --- a/src/core/image.c +++ b/src/core/image.c @@ -349,9 +349,8 @@ int image_exec ( struct image *image ) { /* Preserve record of any currently-running image */ saved_current_image = current_image; - /* Take out a temporary reference to the image. This allows - * the image to unregister itself if necessary, without - * automatically freeing itself. + /* Take out a temporary reference to the image, so that it + * does not get freed when temporarily unregistered. */ current_image = image_get ( image ); @@ -371,6 +370,9 @@ int image_exec ( struct image *image ) { /* Record boot attempt */ syslog ( LOG_NOTICE, "Executing \"%s\"\n", image->name ); + /* Temporarily unregister the image during its execution */ + unregister_image ( image ); + /* Try executing the image */ if ( ( rc = image->type->exec ( image ) ) != 0 ) { DBGC ( image, "IMAGE %s could not execute: %s\n", @@ -387,6 +389,10 @@ int image_exec ( struct image *image ) { image->name, strerror ( rc ) ); } + /* Re-register image (unless due to be replaced) */ + if ( ! image->replacement ) + register_image ( image ); + /* Pick up replacement image before we drop the original * image's temporary reference. The replacement image must * already be registered, so we don't need to hold a temporary diff --git a/src/image/script.c b/src/image/script.c index 28050868a..b34df1e21 100644 --- a/src/image/script.c +++ b/src/image/script.c @@ -197,11 +197,6 @@ static int script_exec ( struct image *image ) { size_t saved_offset; int rc; - /* Temporarily de-register image, so that a "boot" command - * doesn't throw us into an execution loop. - */ - unregister_image ( image ); - /* Preserve state of any currently-running script */ saved_offset = script_offset; @@ -212,10 +207,6 @@ static int script_exec ( struct image *image ) { /* Restore saved state */ script_offset = saved_offset; - /* Re-register image (unless we have been replaced) */ - if ( ! image->replacement ) - register_image ( image ); - return rc; } diff --git a/src/interface/efi/efi_file.c b/src/interface/efi/efi_file.c index f2b44fa73..a0bba1606 100644 --- a/src/interface/efi/efi_file.c +++ b/src/interface/efi/efi_file.c @@ -240,10 +240,6 @@ static size_t efi_file_read_initrd ( struct efi_file_reader *reader ) { len = 0; for_each_image ( image ) { - /* Ignore currently executing image */ - if ( image == current_image ) - continue; - /* Pad to alignment boundary */ pad_len = ( ( -reader->pos ) & ( INITRD_ALIGN - 1 ) ); if ( pad_len ) { @@ -1091,7 +1087,7 @@ int efi_file_install ( EFI_HANDLE handle ) { * instance only if the initrd is non-empty, since Linux does * not gracefully handle a zero-length initrd. */ - load = ( list_is_singular ( &images ) ? NULL : &efi_file_initrd.load ); + load = ( have_images() ? &efi_file_initrd.load : NULL ); if ( ( rc = efi_file_path_install ( &efi_file_initrd_path.vendor.Header, load ) ) != 0 ) { goto err_initrd; -- cgit v1.2.3-55-g7522