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

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