Blame SOURCES/0283-calloc-Make-sure-we-always-have-an-overflow-checking.patch

9723a8
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
c294fc
From: Peter Jones <pjones@redhat.com>
c294fc
Date: Mon, 15 Jun 2020 12:15:29 -0400
9723a8
Subject: [PATCH] calloc: Make sure we always have an overflow-checking
c294fc
 calloc() available
c294fc
c294fc
This tries to make sure that everywhere in this source tree, we always have
c294fc
an appropriate version of calloc() (i.e. grub_calloc(), xcalloc(), etc.)
c294fc
available, and that they all safely check for overflow and return NULL when
c294fc
it would occur.
c294fc
c294fc
Signed-off-by: Peter Jones <pjones@redhat.com>
c294fc
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
c294fc
Upstream-commit-id: 79e51ab7a9a
c294fc
---
9723a8
 grub-core/kern/emu/misc.c          | 12 ++++++++++++
9723a8
 grub-core/kern/emu/mm.c            | 10 ++++++++++
9723a8
 grub-core/kern/mm.c                | 40 ++++++++++++++++++++++++++++++++++++++
9723a8
 grub-core/lib/libgcrypt_wrap/mem.c | 11 +++++++++--
9723a8
 grub-core/lib/posix_wrap/stdlib.h  |  8 +++++++-
c294fc
 include/grub/emu/misc.h            |  1 +
9723a8
 include/grub/mm.h                  |  6 ++++++
c294fc
 7 files changed, 85 insertions(+), 3 deletions(-)
c294fc
c294fc
diff --git a/grub-core/kern/emu/misc.c b/grub-core/kern/emu/misc.c
b71686
index 3d3a4a4a9..b40727673 100644
c294fc
--- a/grub-core/kern/emu/misc.c
c294fc
+++ b/grub-core/kern/emu/misc.c
c294fc
@@ -84,6 +84,18 @@ grub_util_error (const char *fmt, ...)
c294fc
   grub_exit (1);
c294fc
 }
c294fc
 
c294fc
+void *
c294fc
+xcalloc (grub_size_t nmemb, grub_size_t size)
c294fc
+{
c294fc
+  void *p;
c294fc
+
c294fc
+  p = calloc (nmemb, size);
c294fc
+  if (!p)
c294fc
+    grub_util_error ("%s", _("out of memory"));
c294fc
+
c294fc
+  return p;
c294fc
+}
c294fc
+
c294fc
 void *
c294fc
 xmalloc (grub_size_t size)
c294fc
 {
c294fc
diff --git a/grub-core/kern/emu/mm.c b/grub-core/kern/emu/mm.c
b71686
index f262e95e3..145b01d37 100644
c294fc
--- a/grub-core/kern/emu/mm.c
c294fc
+++ b/grub-core/kern/emu/mm.c
c294fc
@@ -25,6 +25,16 @@
c294fc
 #include <string.h>
c294fc
 #include <grub/i18n.h>
c294fc
 
c294fc
+void *
c294fc
+grub_calloc (grub_size_t nmemb, grub_size_t size)
c294fc
+{
c294fc
+  void *ret;
c294fc
+  ret = calloc (nmemb, size);
c294fc
+  if (!ret)
c294fc
+    grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
c294fc
+  return ret;
c294fc
+}
c294fc
+
c294fc
 void *
c294fc
 grub_malloc (grub_size_t size)
c294fc
 {
c294fc
diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c
b71686
index 002cbfa4f..80d0720d0 100644
c294fc
--- a/grub-core/kern/mm.c
c294fc
+++ b/grub-core/kern/mm.c
c294fc
@@ -67,8 +67,10 @@
c294fc
 #include <grub/dl.h>
c294fc
 #include <grub/i18n.h>
c294fc
 #include <grub/mm_private.h>
c294fc
+#include <grub/safemath.h>
c294fc
 
c294fc
 #ifdef MM_DEBUG
c294fc
+# undef grub_calloc
c294fc
 # undef grub_malloc
c294fc
 # undef grub_zalloc
c294fc
 # undef grub_realloc
c294fc
@@ -375,6 +377,30 @@ grub_memalign (grub_size_t align, grub_size_t size)
c294fc
   return 0;
c294fc
 }
c294fc
 
c294fc
+/*
c294fc
+ * Allocate NMEMB instances of SIZE bytes and return the pointer, or error on
c294fc
+ * integer overflow.
c294fc
+ */
c294fc
+void *
c294fc
+grub_calloc (grub_size_t nmemb, grub_size_t size)
c294fc
+{
c294fc
+  void *ret;
c294fc
+  grub_size_t sz = 0;
c294fc
+
c294fc
+  if (grub_mul (nmemb, size, &sz))
c294fc
+    {
c294fc
+      grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
c294fc
+      return NULL;
c294fc
+    }
c294fc
+
c294fc
+  ret = grub_memalign (0, sz);
c294fc
+  if (!ret)
c294fc
+    return NULL;
c294fc
+
c294fc
+  grub_memset (ret, 0, sz);
c294fc
+  return ret;
c294fc
+}
c294fc
+
c294fc
 /* Allocate SIZE bytes and return the pointer.  */
c294fc
 void *
c294fc
 grub_malloc (grub_size_t size)
c294fc
@@ -561,6 +587,20 @@ grub_mm_dump (unsigned lineno)
c294fc
   grub_printf ("\n");
c294fc
 }
c294fc
 
c294fc
+void *
c294fc
+grub_debug_calloc (const char *file, int line, grub_size_t nmemb, grub_size_t size)
c294fc
+{
c294fc
+  void *ptr;
c294fc
+
c294fc
+  if (grub_mm_debug)
c294fc
+    grub_printf ("%s:%d: calloc (0x%" PRIxGRUB_SIZE ", 0x%" PRIxGRUB_SIZE ") = ",
c294fc
+		 file, line, size);
c294fc
+  ptr = grub_calloc (nmemb, size);
c294fc
+  if (grub_mm_debug)
c294fc
+    grub_printf ("%p\n", ptr);
c294fc
+  return ptr;
c294fc
+}
c294fc
+
c294fc
 void *
c294fc
 grub_debug_malloc (const char *file, int line, grub_size_t size)
c294fc
 {
c294fc
diff --git a/grub-core/lib/libgcrypt_wrap/mem.c b/grub-core/lib/libgcrypt_wrap/mem.c
b71686
index beeb661a3..74c6eafe5 100644
c294fc
--- a/grub-core/lib/libgcrypt_wrap/mem.c
c294fc
+++ b/grub-core/lib/libgcrypt_wrap/mem.c
c294fc
@@ -4,6 +4,7 @@
c294fc
 #include <grub/crypto.h>
c294fc
 #include <grub/dl.h>
c294fc
 #include <grub/env.h>
c294fc
+#include <grub/safemath.h>
c294fc
 
c294fc
 GRUB_MOD_LICENSE ("GPLv3+");
c294fc
 
c294fc
@@ -36,7 +37,10 @@ void *
c294fc
 gcry_xcalloc (size_t n, size_t m)
c294fc
 {
c294fc
   void *ret;
c294fc
-  ret = grub_zalloc (n * m);
c294fc
+  size_t sz;
c294fc
+  if (grub_mul (n, m, &sz))
c294fc
+    grub_fatal ("gcry_xcalloc would overflow");
c294fc
+  ret = grub_zalloc (sz);
c294fc
   if (!ret)
c294fc
     grub_fatal ("gcry_xcalloc failed");
c294fc
   return ret;
c294fc
@@ -56,7 +60,10 @@ void *
c294fc
 gcry_xcalloc_secure (size_t n, size_t m)
c294fc
 {
c294fc
   void *ret;
c294fc
-  ret = grub_zalloc (n * m);
c294fc
+  size_t sz;
c294fc
+  if (grub_mul (n, m, &sz))
c294fc
+    grub_fatal ("gcry_xcalloc would overflow");
c294fc
+  ret = grub_zalloc (sz);
c294fc
   if (!ret)
c294fc
     grub_fatal ("gcry_xcalloc failed");
c294fc
   return ret;
c294fc
diff --git a/grub-core/lib/posix_wrap/stdlib.h b/grub-core/lib/posix_wrap/stdlib.h
b71686
index 3b46f47ff..7a8d385e9 100644
c294fc
--- a/grub-core/lib/posix_wrap/stdlib.h
c294fc
+++ b/grub-core/lib/posix_wrap/stdlib.h
c294fc
@@ -21,6 +21,7 @@
c294fc
 
c294fc
 #include <grub/mm.h>
c294fc
 #include <grub/misc.h>
c294fc
+#include <grub/safemath.h>
c294fc
 
c294fc
 static inline void 
c294fc
 free (void *ptr)
c294fc
@@ -37,7 +38,12 @@ malloc (grub_size_t size)
c294fc
 static inline void *
c294fc
 calloc (grub_size_t size, grub_size_t nelem)
c294fc
 {
c294fc
-  return grub_zalloc (size * nelem);
c294fc
+  grub_size_t sz;
c294fc
+
c294fc
+  if (grub_mul (size, nelem, &sz))
c294fc
+    return NULL;
c294fc
+
c294fc
+  return grub_zalloc (sz);
c294fc
 }
c294fc
 
c294fc
 static inline void *
c294fc
diff --git a/include/grub/emu/misc.h b/include/grub/emu/misc.h
b71686
index a653132e3..09e1f1065 100644
c294fc
--- a/include/grub/emu/misc.h
c294fc
+++ b/include/grub/emu/misc.h
c294fc
@@ -51,6 +51,7 @@ grub_util_device_is_mapped (const char *dev);
c294fc
 #define GRUB_HOST_PRIxLONG_LONG "llx"
c294fc
 #endif
c294fc
 
c294fc
+void * EXPORT_FUNC(xcalloc) (grub_size_t nmemb, grub_size_t size) WARN_UNUSED_RESULT;
c294fc
 void * EXPORT_FUNC(xmalloc) (grub_size_t size) WARN_UNUSED_RESULT;
c294fc
 void * EXPORT_FUNC(xrealloc) (void *ptr, grub_size_t size) WARN_UNUSED_RESULT;
c294fc
 char * EXPORT_FUNC(xstrdup) (const char *str) WARN_UNUSED_RESULT;
c294fc
diff --git a/include/grub/mm.h b/include/grub/mm.h
b71686
index 28e2e53eb..9c38dd3ca 100644
c294fc
--- a/include/grub/mm.h
c294fc
+++ b/include/grub/mm.h
c294fc
@@ -29,6 +29,7 @@
c294fc
 #endif
c294fc
 
c294fc
 void grub_mm_init_region (void *addr, grub_size_t size);
c294fc
+void *EXPORT_FUNC(grub_calloc) (grub_size_t nmemb, grub_size_t size);
c294fc
 void *EXPORT_FUNC(grub_malloc) (grub_size_t size);
c294fc
 void *EXPORT_FUNC(grub_zalloc) (grub_size_t size);
c294fc
 void EXPORT_FUNC(grub_free) (void *ptr);
c294fc
@@ -48,6 +49,9 @@ extern int EXPORT_VAR(grub_mm_debug);
c294fc
 void grub_mm_dump_free (void);
c294fc
 void grub_mm_dump (unsigned lineno);
c294fc
 
c294fc
+#define grub_calloc(nmemb, size)	\
c294fc
+  grub_debug_calloc (GRUB_FILE, __LINE__, nmemb, size)
c294fc
+
c294fc
 #define grub_malloc(size)	\
c294fc
   grub_debug_malloc (GRUB_FILE, __LINE__, size)
c294fc
 
c294fc
@@ -63,6 +67,8 @@ void grub_mm_dump (unsigned lineno);
c294fc
 #define grub_free(ptr)	\
c294fc
   grub_debug_free (GRUB_FILE, __LINE__, ptr)
c294fc
 
c294fc
+void *EXPORT_FUNC(grub_debug_calloc) (const char *file, int line,
c294fc
+				      grub_size_t nmemb, grub_size_t size);
c294fc
 void *EXPORT_FUNC(grub_debug_malloc) (const char *file, int line,
c294fc
 				      grub_size_t size);
c294fc
 void *EXPORT_FUNC(grub_debug_zalloc) (const char *file, int line,