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