nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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