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

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