nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0400-fs-fshelp-Catch-impermissibly-large-block-sizes-in-r.patch

b1bcb2
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
b1bcb2
From: Daniel Axtens <dja@axtens.net>
b1bcb2
Date: Mon, 18 Jan 2021 11:46:39 +1100
b1bcb2
Subject: [PATCH] fs/fshelp: Catch impermissibly large block sizes in read
b1bcb2
 helper
b1bcb2
b1bcb2
A fuzzed HFS+ filesystem had log2blocksize = 22. This gave
b1bcb2
log2blocksize + GRUB_DISK_SECTOR_BITS = 31. 1 << 31 = 0x80000000,
b1bcb2
which is -1 as an int. This caused some wacky behavior later on in
b1bcb2
the function, leading to out-of-bounds writes on the destination buffer.
b1bcb2
b1bcb2
Catch log2blocksize + GRUB_DISK_SECTOR_BITS >= 31. We could be stricter,
b1bcb2
but this is the minimum that will prevent integer size weirdness.
b1bcb2
b1bcb2
Signed-off-by: Daniel Axtens <dja@axtens.net>
b1bcb2
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
b1bcb2
---
b1bcb2
 grub-core/fs/fshelp.c | 19 +++++++++++++++++++
b1bcb2
 1 file changed, 19 insertions(+)
b1bcb2
b1bcb2
diff --git a/grub-core/fs/fshelp.c b/grub-core/fs/fshelp.c
b1bcb2
index 42bd542bbc3..7e7387e86e1 100644
b1bcb2
--- a/grub-core/fs/fshelp.c
b1bcb2
+++ b/grub-core/fs/fshelp.c
b1bcb2
@@ -252,6 +252,25 @@ grub_fshelp_read_file (grub_disk_t disk, grub_fshelp_node_t node,
b1bcb2
   grub_disk_addr_t i, blockcnt;
b1bcb2
   int blocksize = 1 << (log2blocksize + GRUB_DISK_SECTOR_BITS);
b1bcb2
 
b1bcb2
+  /*
b1bcb2
+   * Catch blatantly invalid log2blocksize. We could be a lot stricter, but
b1bcb2
+   * this is the most permissive we can be before we start to see integer
b1bcb2
+   * overflow/underflow issues.
b1bcb2
+   */
b1bcb2
+  if (log2blocksize + GRUB_DISK_SECTOR_BITS >= 31)
b1bcb2
+    {
b1bcb2
+      grub_error (GRUB_ERR_OUT_OF_RANGE,
b1bcb2
+		  N_("blocksize too large"));
b1bcb2
+      return -1;
b1bcb2
+    }
b1bcb2
+
b1bcb2
+  if (pos > filesize)
b1bcb2
+    {
b1bcb2
+      grub_error (GRUB_ERR_OUT_OF_RANGE,
b1bcb2
+		  N_("attempt to read past the end of file"));
b1bcb2
+      return -1;
b1bcb2
+    }
b1bcb2
+
b1bcb2
   /* Adjust LEN so it we can't read past the end of the file.  */
b1bcb2
   if (pos + len > filesize)
b1bcb2
     len = filesize - pos;