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

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