Skip to contentSkip to conversation

loader/efi/linux: Correctly terminate LoadOptions field

PATCH Superseded Frediano Ziglio via Grub-devel 1 participant 3 messages Jun 23, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 loader/efi/linux: Correctly terminate LoadOptions field

If a simple string for arguments are passed it should be NUL terminated. This is true for other code but not for "linux" command.

---
 grub-core/loader/efi/linux.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
grub-core/loader/efi/linux.c +5 -4
index 78ea07ca8..afda6ef8f 100644
@@ -190,7 +190,7 @@ grub_arch_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char *args)
   grub_efi_boot_services_t *b;
   grub_efi_status_t status;
   grub_efi_loaded_image_t *loaded_image;
-  int len;
+  grub_size_t len;
 
   mempath = grub_malloc (2 * sizeof (grub_efi_memory_mapped_device_path_t));
   if (!mempath)
@@ -230,9 +230,10 @@ grub_arch_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char *args)
   if (!loaded_image->load_options)
     return grub_errno;
 
-  loaded_image->load_options_size =
-    2 * grub_utf8_to_utf16 (loaded_image->load_options, len,
-			    (grub_uint8_t *) args, len, NULL);
+  len = grub_utf8_to_utf16 (loaded_image->load_options, len,
+			    (grub_uint8_t *) args, (grub_size_t) -1, NULL);
+  ((grub_efi_char16_t*)loaded_image->load_options)[len++] = 0; /* NUL terminate */
+  loaded_image->load_options_size = len * sizeof (grub_efi_char16_t);
 
   grub_dprintf ("linux", "starting image %p\n", image_handle);
   status = b->start_image (image_handle, 0, NULL);
Signature · 7 lines
2.49.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#1 lore raw
Quoted 25 lines · Frediano Ziglio via Grub-devel

If a simple string for arguments are passed it should be NUL terminated. This is true for other code but not for "linux" command.

---
 grub-core/loader/efi/linux.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
grub-core/loader/efi/linux.c +1 -1
index 78ea07ca8..afda6ef8f 100644
@@ -190,7 +190,7 @@ grub_arch_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char *args)
   grub_efi_boot_services_t *b;
   grub_efi_status_t status;
   grub_efi_loaded_image_t *loaded_image;
-  int len;
+  grub_size_t len;
 
   mempath = grub_malloc (2 * sizeof (grub_efi_memory_mapped_device_path_t));
   if (!mempath)
@@ -230,9 +230,10 @@ grub_arch_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char *args)
   if (!loaded_image->load_options)

grub-core/loader/efi/linux.c grub_arch_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char *args) · line 237

Quoted 10 lines
grub-core/loader/efi/linux.c +4 -3
     return grub_errno;
 
-  loaded_image->load_options_size =
-    2 * grub_utf8_to_utf16 (loaded_image->load_options, len,
-                           (grub_uint8_t *) args, len, NULL);
+  len = grub_utf8_to_utf16 (loaded_image->load_options, len,
+                           (grub_uint8_t *) args, (grub_size_t) -1, NULL);
+  ((grub_efi_char16_t*)loaded_image->load_options)[len++] = 0; /* NUL terminate */
+  loaded_image->load_options_size = len * sizeof (grub_efi_char16_t);
 

According to the description, grub_utf8_to_utf16 may return -1 which would be a problem here since it is then used as an array index.

The assignment of loaded_image->load_options_size just above (out of the patch context) seems unnecessary now.

Ross

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#2 lore raw
Quoted 30 lines · Ross Lagerwall
Quoted 27 lines · Frediano Ziglio via Grub-devel

If a simple string for arguments are passed it should be NUL terminated. This is true for other code but not for "linux" command.

---
 grub-core/loader/efi/linux.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
grub-core/loader/efi/linux.c +1 -1
index 78ea07ca8..afda6ef8f 100644
@@ -190,7 +190,7 @@ grub_arch_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char *args)
   grub_efi_boot_services_t *b;
   grub_efi_status_t status;
   grub_efi_loaded_image_t *loaded_image;
-  int len;
+  grub_size_t len;
 
   mempath = grub_malloc (2 * sizeof (grub_efi_memory_mapped_device_path_t));
   if (!mempath)
@@ -230,9 +230,10 @@ grub_arch_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char *args)
   if (!loaded_image->load_options)
     return grub_errno;
 
Quoted 12 lines

grub-core/loader/efi/linux.c grub_arch_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char *args) · line 237

Quoted 8 lines
grub-core/loader/efi/linux.c +4 -3
-  loaded_image->load_options_size =
-    2 * grub_utf8_to_utf16 (loaded_image->load_options, len,
-                           (grub_uint8_t *) args, len, NULL);
+  len = grub_utf8_to_utf16 (loaded_image->load_options, len,
+                           (grub_uint8_t *) args, (grub_size_t) -1, NULL);
+  ((grub_efi_char16_t*)loaded_image->load_options)[len++] = 0; /* NUL terminate */
+  loaded_image->load_options_size = len * sizeof (grub_efi_char16_t);
 

According to the description, grub_utf8_to_utf16 may return -1 which would be a problem here since it is then used as an array index.

Looking at the function implementation it looks like the comment is obsolete, the code replaces any invalid encoding with a question mark. Should I update the function comment?

Quoted 3 lines

The assignment of loaded_image->load_options_size just above (out of the patch context) seems unnecessary now.

Not a regression, but I can change it.

Quoted 1 line

Ross

Frediano

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel