Skip to contentSkip to conversation

bli: Fix crash in get_part_uuid

PATCH Superseded Michael Chang via Grub-devel 3 participants 5 messages Jul 16, 2024 – Jul 17, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bli: Fix crash in get_part_uuid

The get_part_uuid() function made an assumption that the target grub device is a partition device and accessed device->disk->partition without checking for NULL. There are four situations where this assumption is problematic:

1. The device is a net device instead of a disk.
2. The device is an abstraction device, like LVM, RAID, or CRYPTO, which is mostly logical "disk" ((lvmid/<UUID>) and so on).
3. Firmware RAID may present the ESP to grub as an EFI disk (hd0) device if it is contained within a Linux software RAID.
4. When booting from an ISO, the ESP is treated as an El Torito image in the boot catalog. It is therefore presented by firmware and interpreted by grub as a disk (cd0).

As a result, get_part_uuid() could lead to a NULL pointer dereference and trigger a synchronous exception during boot if the ESP falls into one of these categories. This patch fixes the problem by adding the necessary checks to handle cases where the ESP is not a partition device.

Additionally, to avoid disrupting the boot process, this patch relaxes the severity of the errors in this context to non-critical. Errors will be logged, but they will not prevent the boot process from continuing.

Fixes e0fa7dc84 (bli: Add a module for the Boot Loader Interface)
---
 grub-core/commands/bli.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
grub-core/commands/bli.c +19 -1
index e0d8a54f7..298c5f70a 100644
@@ -48,6 +48,22 @@ get_part_uuid (const char *device_name, char **part_uuid)
   if (device == NULL)
     return grub_error (grub_errno, N_("cannot open device: %s"), device_name);
 
+  if (device->disk == NULL)
+    {
+      grub_dprintf ("bli", "%s is not a disk device, partuuid skipped\n", device_name);
+      *part_uuid = NULL;
+      grub_device_close (device);
+      return GRUB_ERR_NONE;
+    }
+
+  if (device->disk->partition == NULL)
+    {
+      grub_dprintf ("bli", "%s has no partition, partuuid skipped\n", device_name);
+      *part_uuid = NULL;
+      grub_device_close (device);
+      return GRUB_ERR_NONE;
+    }
+
   disk = grub_disk_open (device->disk->name);
   if (disk == NULL)
     {
@@ -99,7 +115,7 @@ set_loader_device_part_uuid (void)
 
   status = get_part_uuid (device_name, &part_uuid);
 
-  if (status == GRUB_ERR_NONE)
+  if (status == GRUB_ERR_NONE && part_uuid)
     status = grub_efi_set_variable_to_string ("LoaderDevicePartUUID", &bli_vendor_guid, part_uuid,
 					      GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS |
 					      GRUB_EFI_VARIABLE_RUNTIME_ACCESS);
@@ -117,4 +133,6 @@ GRUB_MOD_INIT (bli)
 				   GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS |
 				   GRUB_EFI_VARIABLE_RUNTIME_ACCESS);
   set_loader_device_part_uuid ();
+  /* No error here is critical, other than being logged */
+  grub_print_error ();
 }
Signature · 7 lines
2.45.2


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

EFI El Torito image on ISO9660 (was: [PATCH] bli: Fix crash in get_part_uuid)

Quoted 4 lines · Michael Chang via Grub-devel

4. When booting from an ISO, the ESP is treated as an El Torito image in the boot catalog. It is therefore presented by firmware and interpreted by grub as a disk (cd0).

In my limited experience, (cd0) is the whole CD/DVD/BD and GRUB does not show the EFI El Torito image as such.

Tested with debian-12.6.0-amd64-netinst.iso on QEMU+OVMF and real hardware:

grub> echo $cmdpath
(cd0)/EFI/BOOT

grub> ls
(proc) (memdisk) (cd0) (cd0,apple2) (cd0,apple1) (cd0,msdos2)

grub> ls (cd0)
Device cd0: Filesystem type iso9660 - Label 'Debian 12.6.0 amd64 n' ...

grub> ls (cd0)/
/boot /css /debian /dists /doc /EFI ...

The /EFI directory in the ISO9660 filesystem contains a copy of the EFI El Torito image contents so that GRUB can find its config file (cd0)/EFI/debian/grub.cfg.

The Apple and MSDOS partitions are caused by the hybrid structure of the Debian installation image. Partition (cd0,apple2) matches the position of the EFI El Torito image but would not exist on a "pure" ISO9660 image.

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

#2 lore raw

Re: EFI El Torito image on ISO9660 (was: [PATCH] bli: Fix crash in get_part_uuid)

Quoted 20 lines · Pascal Hambourg
Quoted 4 lines · Michael Chang via Grub-devel

4. When booting from an ISO, the ESP is treated as an El Torito image in the boot catalog. It is therefore presented by firmware and interpreted by grub as a disk (cd0).

In my limited experience, (cd0) is the whole CD/DVD/BD and GRUB does not show the EFI El Torito image as such.

Tested with debian-12.6.0-amd64-netinst.iso on QEMU+OVMF and real hardware:

grub> echo $cmdpath
(cd0)/EFI/BOOT

grub> ls
(proc) (memdisk) (cd0) (cd0,apple2) (cd0,apple1) (cd0,msdos2)

grub> ls (cd0)
Device cd0: Filesystem type iso9660 - Label 'Debian 12.6.0 amd64 n' ...

Quoted 6 lines

grub> ls (cd0)/
/boot /css /debian /dists /doc /EFI ...

The /EFI directory in the ISO9660 filesystem contains a copy of the EFI El Torito image contents so that GRUB can find its config file (cd0)/EFI/debian/grub.cfg.

Thank you for pointing this out. You're right, (cd0) is actually the whole cdrom image mounted by the iso9660 filesystem in grub, not the vfat EFI image. I'll fix the commit message in the next revision.

Quoted 4 lines

The Apple and MSDOS partitions are caused by the hybrid structure of the Debian installation image. Partition (cd0,apple2) matches the position of the EFI El Torito image but would not exist on a "pure" ISO9660 image.

It seems that saying "When booting from an ISO..." is ambiguous because it also includes booting hybrid disk ISO images. In this context, bli wouldn't crash since $cmdpath, the source of the boot location, points to a partition device. To be more precise, I'll rephrase it to "When booting from a cdrom...".

Thanks,
Michael

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

Reviewed-by on v1 1/1
#3 lore raw
In reply to Michael Chang via Grub-devel

Quoting Michael Chang via Grub-devel (2024-07-16 08:55:00)

Quoted 74 lines

The get_part_uuid() function made an assumption that the target grub device is a partition device and accessed device->disk->partition without checking for NULL. There are four situations where this assumption is problematic:

1. The device is a net device instead of a disk.
2. The device is an abstraction device, like LVM, RAID, or CRYPTO, which is mostly logical "disk" ((lvmid/<UUID>) and so on).
3. Firmware RAID may present the ESP to grub as an EFI disk (hd0) device if it is contained within a Linux software RAID.
4. When booting from an ISO, the ESP is treated as an El Torito image in the boot catalog. It is therefore presented by firmware and interpreted by grub as a disk (cd0).

As a result, get_part_uuid() could lead to a NULL pointer dereference and trigger a synchronous exception during boot if the ESP falls into one of these categories. This patch fixes the problem by adding the necessary checks to handle cases where the ESP is not a partition device.

Additionally, to avoid disrupting the boot process, this patch relaxes the severity of the errors in this context to non-critical. Errors will be logged, but they will not prevent the boot process from continuing.

Fixes e0fa7dc84 (bli: Add a module for the Boot Loader Interface)
---
 grub-core/commands/bli.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
grub-core/commands/bli.c +19 -2
index e0d8a54f7..298c5f70a 100644
@@ -48,6 +48,22 @@ get_part_uuid (const char *device_name, char **part_uuid)
   if (device == NULL)
     return grub_error (grub_errno, N_("cannot open device: %s"), device_name);
 
+  if (device->disk == NULL)
+    {
+      grub_dprintf ("bli", "%s is not a disk device, partuuid skipped\n", device_name);
+      *part_uuid = NULL;
+      grub_device_close (device);
+      return GRUB_ERR_NONE;
+    }
+
+  if (device->disk->partition == NULL)
+    {
+      grub_dprintf ("bli", "%s has no partition, partuuid skipped\n", device_name);
+      *part_uuid = NULL;
+      grub_device_close (device);
+      return GRUB_ERR_NONE;
+    }
+
   disk = grub_disk_open (device->disk->name);
   if (disk == NULL)
     {
@@ -99,7 +115,7 @@ set_loader_device_part_uuid (void)
 
   status = get_part_uuid (device_name, &part_uuid);
 
-  if (status == GRUB_ERR_NONE)
+  if (status == GRUB_ERR_NONE && part_uuid)
     status = grub_efi_set_variable_to_string ("LoaderDevicePartUUID", &bli_vendor_guid, part_uuid,
                                              GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS |
                                              GRUB_EFI_VARIABLE_RUNTIME_ACCESS);
@@ -117,4 +133,6 @@ GRUB_MOD_INIT (bli)
                                   GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS |
                                   GRUB_EFI_VARIABLE_RUNTIME_ACCESS);
   set_loader_device_part_uuid ();
+  /* No error here is critical, other than being logged */
+  grub_print_error ();
 }
--
Quoted 3 lines

2.45.2

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

#4 lore raw
Quoted 72 lines · Oliver Steffen

Quoting Michael Chang via Grub-devel (2024-07-16 08:55:00)

Quoted 71 lines

The get_part_uuid() function made an assumption that the target grub device is a partition device and accessed device->disk->partition without checking for NULL. There are four situations where this assumption is problematic:

1. The device is a net device instead of a disk.
2. The device is an abstraction device, like LVM, RAID, or CRYPTO, which is mostly logical "disk" ((lvmid/<UUID>) and so on).
3. Firmware RAID may present the ESP to grub as an EFI disk (hd0) device if it is contained within a Linux software RAID.
4. When booting from an ISO, the ESP is treated as an El Torito image in the boot catalog. It is therefore presented by firmware and interpreted by grub as a disk (cd0).

As a result, get_part_uuid() could lead to a NULL pointer dereference and trigger a synchronous exception during boot if the ESP falls into one of these categories. This patch fixes the problem by adding the necessary checks to handle cases where the ESP is not a partition device.

Additionally, to avoid disrupting the boot process, this patch relaxes the severity of the errors in this context to non-critical. Errors will be logged, but they will not prevent the boot process from continuing.

Fixes e0fa7dc84 (bli: Add a module for the Boot Loader Interface)
---
 grub-core/commands/bli.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
grub-core/commands/bli.c +18 -1
index e0d8a54f7..298c5f70a 100644
@@ -48,6 +48,22 @@ get_part_uuid (const char *device_name, char **part_uuid)
   if (device == NULL)
     return grub_error (grub_errno, N_("cannot open device: %s"), device_name);
 
+  if (device->disk == NULL)
+    {
+      grub_dprintf ("bli", "%s is not a disk device, partuuid skipped\n", device_name);
+      *part_uuid = NULL;
+      grub_device_close (device);
+      return GRUB_ERR_NONE;
+    }
+
+  if (device->disk->partition == NULL)
+    {
+      grub_dprintf ("bli", "%s has no partition, partuuid skipped\n", device_name);
+      *part_uuid = NULL;
+      grub_device_close (device);
+      return GRUB_ERR_NONE;
+    }
+
   disk = grub_disk_open (device->disk->name);
   if (disk == NULL)
     {
@@ -99,7 +115,7 @@ set_loader_device_part_uuid (void)
 
   status = get_part_uuid (device_name, &part_uuid);
 
-  if (status == GRUB_ERR_NONE)
+  if (status == GRUB_ERR_NONE && part_uuid)
     status = grub_efi_set_variable_to_string ("LoaderDevicePartUUID", &bli_vendor_guid, part_uuid,
                                              GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS |
                                              GRUB_EFI_VARIABLE_RUNTIME_ACCESS);
@@ -117,4 +133,6 @@ GRUB_MOD_INIT (bli)
                                   GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS |
                                   GRUB_EFI_VARIABLE_RUNTIME_ACCESS);
   set_loader_device_part_uuid ();
+  /* No error here is critical, other than being logged */
Quoted 8 lines
Quoted 6 lines
grub-core/commands/bli.c +1 -1
+  grub_print_error ();
 }
--

2.45.2

Thank you! I'll add your Reviewed-By in next revision.

Regards,
Michael

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