Blame SOURCES/0295-relocator-Protect-grub_relocator_alloc_chunk_addr-in.patch

80913e
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
80913e
From: Alexey Makhalov <amakhalov@vmware.com>
80913e
Date: Wed, 15 Jul 2020 06:42:37 +0000
80913e
Subject: [PATCH] relocator: Protect grub_relocator_alloc_chunk_addr() input
80913e
 args against integer underflow/overflow
80913e
80913e
Use arithmetic macros from safemath.h to accomplish it. In this commit,
80913e
I didn't want to be too paranoid to check every possible math equation
80913e
for overflow/underflow. Only obvious places (with non zero chance of
80913e
overflow/underflow) were refactored.
80913e
80913e
Signed-off-by: Alexey Makhalov <amakhalov@vmware.com>
80913e
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
80913e
Upstream-commit-id: ebb15735f10
80913e
---
80913e
 grub-core/loader/i386/linux.c    |  9 +++++++--
80913e
 grub-core/loader/i386/pc/linux.c |  9 +++++++--
80913e
 grub-core/loader/i386/xen.c      | 12 ++++++++++--
80913e
 grub-core/loader/xnu.c           | 11 +++++++----
80913e
 4 files changed, 31 insertions(+), 10 deletions(-)
80913e
80913e
diff --git a/grub-core/loader/i386/linux.c b/grub-core/loader/i386/linux.c
80913e
index 76304f05700..b4a30f607fa 100644
80913e
--- a/grub-core/loader/i386/linux.c
80913e
+++ b/grub-core/loader/i386/linux.c
80913e
@@ -37,6 +37,7 @@
80913e
 #include <grub/linux.h>
80913e
 #include <grub/efi/sb.h>
80913e
 #include <grub/tpm.h>
80913e
+#include <grub/safemath.h>
80913e
 
80913e
 GRUB_MOD_LICENSE ("GPLv3+");
80913e
 
80913e
@@ -593,9 +594,13 @@ grub_linux_boot (void)
80913e
 
80913e
   {
80913e
     grub_relocator_chunk_t ch;
80913e
+    grub_size_t sz;
80913e
+
80913e
+    if (grub_add (ctx.real_size, efi_mmap_size, &sz))
80913e
+      return GRUB_ERR_OUT_OF_RANGE;
80913e
+
80913e
     err = grub_relocator_alloc_chunk_addr (relocator, &ch,
80913e
-					   ctx.real_mode_target,
80913e
-					   (ctx.real_size + efi_mmap_size));
80913e
+					   ctx.real_mode_target, sz);
80913e
     if (err)
80913e
      return err;
80913e
     real_mode_mem = get_virtual_current_address (ch);
80913e
diff --git a/grub-core/loader/i386/pc/linux.c b/grub-core/loader/i386/pc/linux.c
80913e
index 783a3cd93bc..540891371f9 100644
80913e
--- a/grub-core/loader/i386/pc/linux.c
80913e
+++ b/grub-core/loader/i386/pc/linux.c
80913e
@@ -36,6 +36,7 @@
80913e
 #include <grub/lib/cmdline.h>
80913e
 #include <grub/linux.h>
80913e
 #include <grub/efi/sb.h>
80913e
+#include <grub/safemath.h>
80913e
 
80913e
 GRUB_MOD_LICENSE ("GPLv3+");
80913e
 
80913e
@@ -231,8 +232,12 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
80913e
     setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS;
80913e
 
80913e
   real_size = setup_sects << GRUB_DISK_SECTOR_BITS;
80913e
-  grub_linux16_prot_size = grub_file_size (file)
80913e
-    - real_size - GRUB_DISK_SECTOR_SIZE;
80913e
+  if (grub_sub (grub_file_size (file), real_size, &grub_linux16_prot_size) ||
80913e
+      grub_sub (grub_linux16_prot_size, GRUB_DISK_SECTOR_SIZE, &grub_linux16_prot_size))
80913e
+    {
80913e
+      grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
80913e
+      goto fail;
80913e
+    }
80913e
 
80913e
   if (! grub_linux_is_bzimage
80913e
       && GRUB_LINUX_ZIMAGE_ADDR + grub_linux16_prot_size
80913e
diff --git a/grub-core/loader/i386/xen.c b/grub-core/loader/i386/xen.c
80913e
index 3073f64d5e5..85b93347b25 100644
80913e
--- a/grub-core/loader/i386/xen.c
80913e
+++ b/grub-core/loader/i386/xen.c
80913e
@@ -40,6 +40,7 @@
80913e
 #include <grub/xen_file.h>
80913e
 #include <grub/linux.h>
80913e
 #include <grub/i386/memory.h>
80913e
+#include <grub/safemath.h>
80913e
 
80913e
 GRUB_MOD_LICENSE ("GPLv3+");
80913e
 
80913e
@@ -635,6 +636,7 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
80913e
   grub_relocator_chunk_t ch;
80913e
   grub_addr_t kern_start;
80913e
   grub_addr_t kern_end;
80913e
+  grub_size_t sz;
80913e
 
80913e
   if (argc == 0)
80913e
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
80913e
@@ -699,8 +701,14 @@ grub_cmd_xen (grub_command_t cmd __attribute__ ((unused)),
80913e
 
80913e
   xen_state.max_addr = ALIGN_UP (kern_end, PAGE_SIZE);
80913e
 
80913e
-  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch, kern_start,
80913e
-					 kern_end - kern_start);
80913e
+
80913e
+  if (grub_sub (kern_end, kern_start, &sz))
80913e
+    {
80913e
+      err = GRUB_ERR_OUT_OF_RANGE;
80913e
+      goto fail;
80913e
+    }
80913e
+
80913e
+  err = grub_relocator_alloc_chunk_addr (xen_state.relocator, &ch, kern_start, sz);
80913e
   if (err)
80913e
     goto fail;
80913e
   kern_chunk_src = get_virtual_current_address (ch);
80913e
diff --git a/grub-core/loader/xnu.c b/grub-core/loader/xnu.c
80913e
index dc7d5409e1e..2bf02489bad 100644
80913e
--- a/grub-core/loader/xnu.c
80913e
+++ b/grub-core/loader/xnu.c
80913e
@@ -34,6 +34,7 @@
80913e
 #include <grub/env.h>
80913e
 #include <grub/i18n.h>
80913e
 #include <grub/efi/sb.h>
80913e
+#include <grub/safemath.h>
80913e
 
80913e
 GRUB_MOD_LICENSE ("GPLv3+");
80913e
 
80913e
@@ -59,15 +60,17 @@ grub_xnu_heap_malloc (int size, void **src, grub_addr_t *target)
80913e
 {
80913e
   grub_err_t err;
80913e
   grub_relocator_chunk_t ch;
80913e
+  grub_addr_t tgt;
80913e
+
80913e
+  if (grub_add (grub_xnu_heap_target_start, grub_xnu_heap_size, &tgt))
80913e
+    return GRUB_ERR_OUT_OF_RANGE;
80913e
   
80913e
-  err = grub_relocator_alloc_chunk_addr (grub_xnu_relocator, &ch,
80913e
-					 grub_xnu_heap_target_start
80913e
-					 + grub_xnu_heap_size, size);
80913e
+  err = grub_relocator_alloc_chunk_addr (grub_xnu_relocator, &ch, tgt, size);
80913e
   if (err)
80913e
     return err;
80913e
 
80913e
   *src = get_virtual_current_address (ch);
80913e
-  *target = grub_xnu_heap_target_start + grub_xnu_heap_size;
80913e
+  *target = tgt;
80913e
   grub_xnu_heap_size += size;
80913e
   grub_dprintf ("xnu", "val=%p\n", *src);
80913e
   return GRUB_ERR_NONE;