dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

Blame SOURCES/0304-Fix-a-regression-caused-by-efi-fix-some-malformed-de.patch

9723a8
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
c294fc
From: Chris Coulson <chris.coulson@canonical.com>
c294fc
Date: Wed, 22 Jul 2020 17:06:04 +0100
9723a8
Subject: [PATCH] Fix a regression caused by "efi: fix some malformed device
9723a8
 path arithmetic errors"
c294fc
c294fc
This commit introduced a bogus check inside copy_file_path to
c294fc
determine whether the destination grub_efi_file_path_device_path_t
c294fc
was valid before anything was copied to it. Depending on the
c294fc
contents of the heap buffer, this check could fail which would
c294fc
result in copy_file_path returning early.
c294fc
c294fc
Without any error propagated to the caller, make_file_path would
c294fc
then try to advance the invalid device path node with
c294fc
GRUB_EFI_NEXT_DEVICE_PATH, which would also fail, returning a NULL
c294fc
pointer that would subsequently be dereferenced.
c294fc
c294fc
Remove the bogus check, and also propagate errors from copy_file_path.
c294fc
---
c294fc
 grub-core/loader/efi/chainloader.c | 26 ++++++++++++++------------
c294fc
 1 file changed, 14 insertions(+), 12 deletions(-)
c294fc
c294fc
diff --git a/grub-core/loader/efi/chainloader.c b/grub-core/loader/efi/chainloader.c
c294fc
index c2411b6dab2..8b99cf23e9d 100644
c294fc
--- a/grub-core/loader/efi/chainloader.c
c294fc
+++ b/grub-core/loader/efi/chainloader.c
c294fc
@@ -115,7 +115,7 @@ grub_chainloader_boot (void)
c294fc
   return grub_errno;
c294fc
 }
c294fc
 
c294fc
-static void
c294fc
+static grub_err_t
c294fc
 copy_file_path (grub_efi_file_path_device_path_t *fp,
c294fc
 		const char *str, grub_efi_uint16_t len)
c294fc
 {
c294fc
@@ -125,15 +125,9 @@ copy_file_path (grub_efi_file_path_device_path_t *fp,
c294fc
   fp->header.type = GRUB_EFI_MEDIA_DEVICE_PATH_TYPE;
c294fc
   fp->header.subtype = GRUB_EFI_FILE_PATH_DEVICE_PATH_SUBTYPE;
c294fc
 
c294fc
-  if (!GRUB_EFI_DEVICE_PATH_VALID ((grub_efi_device_path_t *)fp))
c294fc
-    {
c294fc
-      grub_error (GRUB_ERR_BAD_ARGUMENT, "EFI Device Path is invalid");
c294fc
-      return;
c294fc
-    }
c294fc
-
c294fc
   path_name = grub_calloc (len, GRUB_MAX_UTF16_PER_UTF8 * sizeof (*path_name));
c294fc
   if (!path_name)
c294fc
-    return;
c294fc
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY, "failed to allocate path buffer");
c294fc
 
c294fc
   size = grub_utf8_to_utf16 (path_name, len * GRUB_MAX_UTF16_PER_UTF8,
c294fc
 			     (const grub_uint8_t *) str, len, 0);
c294fc
@@ -145,6 +139,8 @@ copy_file_path (grub_efi_file_path_device_path_t *fp,
c294fc
   /* File Path is NULL terminated */
c294fc
   fp->path_name[size++] = '\0';
c294fc
   fp->header.length = size * sizeof (grub_efi_char16_t) + sizeof (*fp);
c294fc
+  grub_free (path_name);
c294fc
+  return GRUB_ERR_NONE;
c294fc
 }
c294fc
 
c294fc
 static grub_efi_device_path_t *
c294fc
@@ -202,13 +198,19 @@ make_file_path (grub_efi_device_path_t *dp, const char *filename)
c294fc
   /* Fill the file path for the directory.  */
c294fc
   d = (grub_efi_device_path_t *) ((char *) file_path
c294fc
 				  + ((char *) d - (char *) dp));
c294fc
-  copy_file_path ((grub_efi_file_path_device_path_t *) d,
c294fc
-		  dir_start, dir_end - dir_start);
c294fc
+  if (copy_file_path ((grub_efi_file_path_device_path_t *) d,
c294fc
+		      dir_start, dir_end - dir_start) != GRUB_ERR_NONE)
c294fc
+    {
c294fc
+    fail:
c294fc
+      grub_free (file_path);
c294fc
+      return 0;
c294fc
+    }
c294fc
 
c294fc
   /* Fill the file path for the file.  */
c294fc
   d = GRUB_EFI_NEXT_DEVICE_PATH (d);
c294fc
-  copy_file_path ((grub_efi_file_path_device_path_t *) d,
c294fc
-		  dir_end + 1, grub_strlen (dir_end + 1));
c294fc
+  if (copy_file_path ((grub_efi_file_path_device_path_t *) d,
c294fc
+		      dir_end + 1, grub_strlen (dir_end + 1)) != GRUB_ERR_NONE)
c294fc
+    goto fail;
c294fc
 
c294fc
   /* Fill the end of device path nodes.  */
c294fc
   d = GRUB_EFI_NEXT_DEVICE_PATH (d);