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

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