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

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