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

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