Skip to content

Commit 7035c31

Browse files
WenhuaChanglsandov1
authored andcommitted
commands/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 a CD-ROM, the ESP is a VFAT image indexed by the El Torito boot catalog. The boot device is set to (cd0), corresponding to the CD-ROM image mounted as an ISO 9660 filesystem. 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: e0fa7dc (bli: Add a module for the Boot Loader Interface) Signed-off-by: Michael Chang <[email protected]> Reviewed-By: Oliver Steffen <[email protected]> Reviewed-by: Daniel Kiper <[email protected]>
1 parent a8d8bee commit 7035c31

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

grub-core/commands/bli.c

+19-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@ get_part_uuid (const char *device_name, char **part_uuid)
4848
if (device == NULL)
4949
return grub_error (grub_errno, N_("cannot open device: %s"), device_name);
5050

51+
if (device->disk == NULL)
52+
{
53+
grub_dprintf ("bli", "%s is not a disk device, partuuid skipped\n", device_name);
54+
*part_uuid = NULL;
55+
grub_device_close (device);
56+
return GRUB_ERR_NONE;
57+
}
58+
59+
if (device->disk->partition == NULL)
60+
{
61+
grub_dprintf ("bli", "%s has no partition, partuuid skipped\n", device_name);
62+
*part_uuid = NULL;
63+
grub_device_close (device);
64+
return GRUB_ERR_NONE;
65+
}
66+
5167
disk = grub_disk_open (device->disk->name);
5268
if (disk == NULL)
5369
{
@@ -99,7 +115,7 @@ set_loader_device_part_uuid (void)
99115

100116
status = get_part_uuid (device_name, &part_uuid);
101117

102-
if (status == GRUB_ERR_NONE)
118+
if (status == GRUB_ERR_NONE && part_uuid)
103119
status = grub_efi_set_variable_to_string ("LoaderDevicePartUUID", &bli_vendor_guid, part_uuid,
104120
GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS |
105121
GRUB_EFI_VARIABLE_RUNTIME_ACCESS);
@@ -117,4 +133,6 @@ GRUB_MOD_INIT (bli)
117133
GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS |
118134
GRUB_EFI_VARIABLE_RUNTIME_ACCESS);
119135
set_loader_device_part_uuid ();
136+
/* No error here is critical, other than being logged */
137+
grub_print_error ();
120138
}

0 commit comments

Comments
 (0)