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