Blame SOURCES/0512-loader-i386-efi-linux-Fix-a-memory-leak-in-the-initr.patch

bf0270
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
bf0270
From: Chris Coulson <chris.coulson@canonical.com>
bf0270
Date: Tue, 3 May 2022 09:47:35 +0200
bf0270
Subject: [PATCH] loader/i386/efi/linux: Fix a memory leak in the initrd
bf0270
 command
bf0270
bf0270
Subsequent invocations of the initrd command result in the previous
bf0270
initrd being leaked, so fix that.
bf0270
bf0270
Signed-off-by: Chris Coulson <chris.coulson@canonical.com>
bf0270
(cherry picked from commit d98af31ce1e31bb22163960d53f5eb28c66582a0)
bf0270
(cherry picked from commit 62234d6a00e6d1dd8e017ff161d359feb5234082)
bf0270
(cherry picked from commit bda5a10716dc9676400dce1374232452f46d0bc4)
bf0270
---
bf0270
 grub-core/loader/i386/efi/linux.c | 21 ++++++++++++---------
bf0270
 1 file changed, 12 insertions(+), 9 deletions(-)
bf0270
bf0270
diff --git a/grub-core/loader/i386/efi/linux.c b/grub-core/loader/i386/efi/linux.c
bf0270
index 77a0734786..8337191921 100644
bf0270
--- a/grub-core/loader/i386/efi/linux.c
bf0270
+++ b/grub-core/loader/i386/efi/linux.c
bf0270
@@ -209,6 +209,7 @@ grub_cmd_initrd (grub_command_t cmd, int argc, char *argv[])
bf0270
   grub_uint8_t *ptr;
bf0270
   struct grub_linuxefi_context *context = (struct grub_linuxefi_context *) cmd->data;
bf0270
   struct linux_kernel_params *params;
bf0270
+  void *initrd_mem = 0;
bf0270
 
bf0270
   if (argc == 0)
bf0270
     {
bf0270
@@ -242,19 +243,19 @@ grub_cmd_initrd (grub_command_t cmd, int argc, char *argv[])
bf0270
 	}
bf0270
     }
bf0270
 
bf0270
-  context->initrd_mem = kernel_alloc(size, N_("can't allocate initrd"));
bf0270
-  if (context->initrd_mem == NULL)
bf0270
+  initrd_mem = kernel_alloc(size, N_("can't allocate initrd"));
bf0270
+  if (initrd_mem == NULL)
bf0270
     goto fail;
bf0270
-  grub_dprintf ("linux", "initrd_mem = %p\n", context->initrd_mem);
bf0270
+  grub_dprintf ("linux", "initrd_mem = %p\n", initrd_mem);
bf0270
 
bf0270
   params->ramdisk_size = LOW_U32(size);
bf0270
-  params->ramdisk_image = LOW_U32(context->initrd_mem);
bf0270
+  params->ramdisk_image = LOW_U32(initrd_mem);
bf0270
 #if defined(__x86_64__)
bf0270
   params->ext_ramdisk_size = HIGH_U32(size);
bf0270
-  params->ext_ramdisk_image = HIGH_U32(context->initrd_mem);
bf0270
+  params->ext_ramdisk_image = HIGH_U32(initrd_mem);
bf0270
 #endif
bf0270
 
bf0270
-  ptr = context->initrd_mem;
bf0270
+  ptr = initrd_mem;
bf0270
 
bf0270
   for (i = 0; i < nfiles; i++)
bf0270
     {
bf0270
@@ -273,6 +274,9 @@ grub_cmd_initrd (grub_command_t cmd, int argc, char *argv[])
bf0270
       ptr += ALIGN_UP_OVERHEAD (cursize, 4);
bf0270
     }
bf0270
 
bf0270
+  kernel_free(context->initrd_mem, params->ramdisk_size);
bf0270
+
bf0270
+  context->initrd_mem = initrd_mem;
bf0270
   params->ramdisk_size = size;
bf0270
 
bf0270
  fail:
bf0270
@@ -280,9 +284,8 @@ grub_cmd_initrd (grub_command_t cmd, int argc, char *argv[])
bf0270
     grub_file_close (files[i]);
bf0270
   grub_free (files);
bf0270
 
bf0270
-  if (context->initrd_mem && grub_errno)
bf0270
-    grub_efi_free_pages ((grub_efi_physical_address_t)(grub_addr_t)context->initrd_mem,
bf0270
-			 BYTES_TO_PAGES(size));
bf0270
+  if (initrd_mem && grub_errno)
bf0270
+    kernel_free (initrd_mem, size);
bf0270
 
bf0270
   return grub_errno;
bf0270
 }