dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

Blame SOURCES/0314-linux-Fix-integer-overflows-in-initrd-size-handling.patch

80913e
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
80913e
From: Colin Watson <cjwatson@debian.org>
80913e
Date: Sat, 25 Jul 2020 12:15:37 +0100
80913e
Subject: [PATCH] linux: Fix integer overflows in initrd size handling
80913e
80913e
These could be triggered by a crafted filesystem with very large files.
80913e
80913e
Fixes: CVE-2020-15707
80913e
80913e
Signed-off-by: Colin Watson <cjwatson@debian.org>
80913e
Reviewed-by: Jan Setje-Eilers <jan.setjeeilers@oracle.com>
80913e
---
80913e
 grub-core/loader/linux.c | 74 +++++++++++++++++++++++++++++++++++-------------
80913e
 1 file changed, 54 insertions(+), 20 deletions(-)
80913e
80913e
diff --git a/grub-core/loader/linux.c b/grub-core/loader/linux.c
80913e
index 61a2e144db0..0953f6d3266 100644
80913e
--- a/grub-core/loader/linux.c
80913e
+++ b/grub-core/loader/linux.c
80913e
@@ -5,6 +5,7 @@
80913e
 #include <grub/file.h>
80913e
 #include <grub/mm.h>
80913e
 #include <grub/tpm.h>
80913e
+#include <grub/safemath.h>
80913e
 
80913e
 struct newc_head
80913e
 {
80913e
@@ -99,13 +100,13 @@ free_dir (struct dir *root)
80913e
   grub_free (root);
80913e
 }
80913e
 
80913e
-static grub_size_t
80913e
+static grub_err_t
80913e
 insert_dir (const char *name, struct dir **root,
80913e
-	    grub_uint8_t *ptr)
80913e
+	    grub_uint8_t *ptr, grub_size_t *size)
80913e
 {
80913e
   struct dir *cur, **head = root;
80913e
   const char *cb, *ce = name;
80913e
-  grub_size_t size = 0;
80913e
+  *size = 0;
80913e
   while (1)
80913e
     {
80913e
       for (cb = ce; *cb == '/'; cb++);
80913e
@@ -131,14 +132,22 @@ insert_dir (const char *name, struct dir **root,
80913e
 	      ptr = make_header (ptr, name, ce - name,
80913e
 				 040777, 0);
80913e
 	    }
80913e
-	  size += ALIGN_UP ((ce - (char *) name)
80913e
-			    + sizeof (struct newc_head), 4);
80913e
+	  if (grub_add (*size,
80913e
+		        ALIGN_UP ((ce - (char *) name)
80913e
+				  + sizeof (struct newc_head), 4),
80913e
+			size))
80913e
+	    {
80913e
+	      grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
80913e
+	      grub_free (n->name);
80913e
+	      grub_free (n);
80913e
+	      return grub_errno;
80913e
+	    }
80913e
 	  *head = n;
80913e
 	  cur = n;
80913e
 	}
80913e
       root = &cur->next;
80913e
     }
80913e
-  return size;
80913e
+  return GRUB_ERR_NONE;
80913e
 }
80913e
 
80913e
 grub_err_t
80913e
@@ -175,26 +184,33 @@ grub_initrd_init (int argc, char *argv[],
80913e
 	  if (eptr)
80913e
 	    {
80913e
 	      grub_file_filter_disable_compression ();
80913e
+	      grub_size_t dir_size, name_len;
80913e
+
80913e
 	      initrd_ctx->components[i].newc_name = grub_strndup (ptr, eptr - ptr);
80913e
-	      if (!initrd_ctx->components[i].newc_name)
80913e
+	      if (!initrd_ctx->components[i].newc_name ||
80913e
+		  insert_dir (initrd_ctx->components[i].newc_name, &root, 0,
80913e
+			      &dir_size))
80913e
 		{
80913e
 		  grub_initrd_close (initrd_ctx);
80913e
 		  return grub_errno;
80913e
 		}
80913e
-	      initrd_ctx->size
80913e
-		+= ALIGN_UP (sizeof (struct newc_head)
80913e
-			    + grub_strlen (initrd_ctx->components[i].newc_name),
80913e
-			     4);
80913e
-	      initrd_ctx->size += insert_dir (initrd_ctx->components[i].newc_name,
80913e
-					      &root, 0);
80913e
+	      name_len = grub_strlen (initrd_ctx->components[i].newc_name);
80913e
+	      if (grub_add (initrd_ctx->size,
80913e
+			    ALIGN_UP (sizeof (struct newc_head) + name_len, 4),
80913e
+			    &initrd_ctx->size) ||
80913e
+		  grub_add (initrd_ctx->size, dir_size, &initrd_ctx->size))
80913e
+		goto overflow;
80913e
 	      newc = 1;
80913e
 	      fname = eptr + 1;
80913e
 	    }
80913e
 	}
80913e
       else if (newc)
80913e
 	{
80913e
-	  initrd_ctx->size += ALIGN_UP (sizeof (struct newc_head)
80913e
-					+ sizeof ("TRAILER!!!") - 1, 4);
80913e
+	  if (grub_add (initrd_ctx->size,
80913e
+			ALIGN_UP (sizeof (struct newc_head)
80913e
+				  + sizeof ("TRAILER!!!") - 1, 4),
80913e
+			&initrd_ctx->size))
80913e
+	    goto overflow;
80913e
 	  free_dir (root);
80913e
 	  root = 0;
80913e
 	  newc = 0;
80913e
@@ -209,19 +225,29 @@ grub_initrd_init (int argc, char *argv[],
80913e
       initrd_ctx->nfiles++;
80913e
       initrd_ctx->components[i].size
80913e
 	= grub_file_size (initrd_ctx->components[i].file);
80913e
-      initrd_ctx->size += initrd_ctx->components[i].size;
80913e
+      if (grub_add (initrd_ctx->size, initrd_ctx->components[i].size,
80913e
+		    &initrd_ctx->size))
80913e
+	goto overflow;
80913e
     }
80913e
 
80913e
   if (newc)
80913e
     {
80913e
       initrd_ctx->size = ALIGN_UP (initrd_ctx->size, 4);
80913e
-      initrd_ctx->size += ALIGN_UP (sizeof (struct newc_head)
80913e
-				    + sizeof ("TRAILER!!!") - 1, 4);
80913e
+      if (grub_add (initrd_ctx->size,
80913e
+		    ALIGN_UP (sizeof (struct newc_head)
80913e
+			      + sizeof ("TRAILER!!!") - 1, 4),
80913e
+		    &initrd_ctx->size))
80913e
+	goto overflow;
80913e
       free_dir (root);
80913e
       root = 0;
80913e
     }
80913e
   
80913e
   return GRUB_ERR_NONE;
80913e
+
80913e
+overflow:
80913e
+  free_dir (root);
80913e
+  grub_initrd_close (initrd_ctx);
80913e
+  return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
80913e
 }
80913e
 
80913e
 grub_size_t
80913e
@@ -262,8 +288,16 @@ grub_initrd_load (struct grub_linux_initrd_context *initrd_ctx,
80913e
 
80913e
       if (initrd_ctx->components[i].newc_name)
80913e
 	{
80913e
-	  ptr += insert_dir (initrd_ctx->components[i].newc_name,
80913e
-			     &root, ptr);
80913e
+	  grub_size_t dir_size;
80913e
+
80913e
+	  if (insert_dir (initrd_ctx->components[i].newc_name, &root, ptr,
80913e
+			  &dir_size))
80913e
+	    {
80913e
+	      free_dir (root);
80913e
+	      grub_initrd_close (initrd_ctx);
80913e
+	      return grub_errno;
80913e
+	    }
80913e
+	  ptr += dir_size;
80913e
 	  ptr = make_header (ptr, initrd_ctx->components[i].newc_name,
80913e
 			     grub_strlen (initrd_ctx->components[i].newc_name),
80913e
 			     0100777,