grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] grub-core/loader/i386: Fix initrd maximum address overflow


From: Jeremy Szu
Subject: [PATCH] grub-core/loader/i386: Fix initrd maximum address overflow
Date: Sun, 4 Dec 2022 21:43:37 +0800

The current i386 initrd be limited under 1G memory and it works in most
compressed initrd. (also initrd_addr_max case reported by kernel)

addr = (addr_max - aligned_size) & ~0xFFF;

Above line to calculate the reasonable address to store the initrd.

However, if initrd size is greater than 1G (or initrd_addr_max), then it
will get overflow, especially in x86_64 arch.

Therefore, add a check point to prevent it overflow as well as having a
debug log for complex story of initrd addresses.

Signed-off-by: Jeremy Szu <jeremy.szu@canonical.com>
---
 grub-core/loader/i386/linux.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/grub-core/loader/i386/linux.c b/grub-core/loader/i386/linux.c
index edd6c2bb1..06d482481 100644
--- a/grub-core/loader/i386/linux.c
+++ b/grub-core/loader/i386/linux.c
@@ -1085,9 +1085,22 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ 
((unused)),
 
   addr_min = (grub_addr_t) prot_mode_target + prot_init_space;
 
+  /* Make sure the maximum address is able to store the initrd. */
+  if (addr_max < aligned_size)
+    {
+      grub_error (GRUB_ERR_OUT_OF_RANGE,
+                  "the size of initrd is bigger than addr_max.\n");
+      goto fail;
+    }
+
   /* Put the initrd as high as possible, 4KiB aligned.  */
   addr = (addr_max - aligned_size) & ~0xFFF;
 
+  grub_dprintf ("linux",
+                "Initrd at addr 0x%" PRIxGRUB_ADDR " which is expected in"
+                " ranger 0x%" PRIxGRUB_ADDR    " ~ 0x%" PRIxGRUB_ADDR "\n",
+                addr, addr_min, addr_max);
+
   if (addr < addr_min)
     {
       grub_error (GRUB_ERR_OUT_OF_RANGE, "the initrd is too big");
-- 
2.38.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]