Blame SOURCES/0285-malloc-Use-overflow-checking-primitives-where-we-do-.patch

c294fc
From 05c062003cff8dbbf1b394fa836378e88522e29c Mon Sep 17 00:00:00 2001
c294fc
From: Peter Jones <pjones@redhat.com>
c294fc
Date: Mon, 15 Jun 2020 12:28:27 -0400
c294fc
Subject: [PATCH 285/314] malloc: Use overflow checking primitives where we do
c294fc
 complex allocations
c294fc
c294fc
This attempts to fix the places where we do the following where
c294fc
arithmetic_expr may include unvalidated data:
c294fc
c294fc
  X = grub_malloc(arithmetic_expr);
c294fc
c294fc
It accomplishes this by doing the arithmetic ahead of time using grub_add(),
c294fc
grub_sub(), grub_mul() and testing for overflow before proceeding.
c294fc
c294fc
Among other issues, this fixes:
c294fc
  - allocation of integer overflow in grub_video_bitmap_create()
c294fc
    reported by Chris Coulson,
c294fc
  - allocation of integer overflow in grub_png_decode_image_header()
c294fc
    reported by Chris Coulson,
c294fc
  - allocation of integer overflow in grub_squash_read_symlink()
c294fc
    reported by Chris Coulson,
c294fc
  - allocation of integer overflow in grub_ext2_read_symlink()
c294fc
    reported by Chris Coulson,
c294fc
  - allocation of integer overflow in read_section_as_string()
c294fc
    reported by Chris Coulson.
c294fc
c294fc
Fixes: CVE-2020-14309, CVE-2020-14310, CVE-2020-14311
c294fc
c294fc
Signed-off-by: Peter Jones <pjones@redhat.com>
c294fc
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
c294fc
Upstream-commit-id: 5fb2befbf04
c294fc
---
c294fc
 grub-core/commands/legacycfg.c | 29 +++++++++++++++----
c294fc
 grub-core/commands/wildcard.c  | 36 ++++++++++++++++++++----
c294fc
 grub-core/disk/ldm.c           | 32 +++++++++++++++------
c294fc
 grub-core/font/font.c          |  7 ++++-
c294fc
 grub-core/fs/btrfs.c           | 29 +++++++++++++------
c294fc
 grub-core/fs/ext2.c            | 10 ++++++-
c294fc
 grub-core/fs/iso9660.c         | 51 ++++++++++++++++++++++++----------
c294fc
 grub-core/fs/sfs.c             | 27 ++++++++++++++----
c294fc
 grub-core/fs/squash4.c         | 45 ++++++++++++++++++++++--------
c294fc
 grub-core/fs/udf.c             | 41 +++++++++++++++++----------
c294fc
 grub-core/fs/xfs.c             | 11 +++++---
c294fc
 grub-core/fs/zfs/zfs.c         | 22 ++++++++++-----
c294fc
 grub-core/fs/zfs/zfscrypt.c    |  7 ++++-
c294fc
 grub-core/lib/arg.c            | 20 +++++++++++--
c294fc
 grub-core/loader/i386/bsd.c    |  8 +++++-
c294fc
 grub-core/net/dns.c            |  9 +++++-
c294fc
 grub-core/normal/charset.c     | 10 +++++--
c294fc
 grub-core/normal/cmdline.c     | 14 ++++++++--
c294fc
 grub-core/normal/menu_entry.c  | 13 +++++++--
c294fc
 grub-core/script/argv.c        | 16 +++++++++--
c294fc
 grub-core/script/lexer.c       | 21 ++++++++++++--
c294fc
 grub-core/video/bitmap.c       | 25 +++++++++++------
c294fc
 grub-core/video/readers/png.c  | 13 +++++++--
c294fc
 23 files changed, 383 insertions(+), 113 deletions(-)
c294fc
c294fc
diff --git a/grub-core/commands/legacycfg.c b/grub-core/commands/legacycfg.c
c294fc
index da66a8927c4..0de070eacc4 100644
c294fc
--- a/grub-core/commands/legacycfg.c
c294fc
+++ b/grub-core/commands/legacycfg.c
c294fc
@@ -32,6 +32,7 @@
c294fc
 #include <grub/auth.h>
c294fc
 #include <grub/disk.h>
c294fc
 #include <grub/partition.h>
c294fc
+#include <grub/safemath.h>
c294fc
 
c294fc
 GRUB_MOD_LICENSE ("GPLv3+");
c294fc
 
c294fc
@@ -104,13 +105,22 @@ legacy_file (const char *filename)
c294fc
 	if (newsuffix)
c294fc
 	  {
c294fc
 	    char *t;
c294fc
-	    
c294fc
+	    grub_size_t sz;
c294fc
+
c294fc
+	    if (grub_add (grub_strlen (suffix), grub_strlen (newsuffix), &sz) ||
c294fc
+		grub_add (sz, 1, &sz))
c294fc
+	      {
c294fc
+		grub_errno = GRUB_ERR_OUT_OF_RANGE;
c294fc
+		goto fail_0;
c294fc
+	      }
c294fc
+
c294fc
 	    t = suffix;
c294fc
-	    suffix = grub_realloc (suffix, grub_strlen (suffix)
c294fc
-				   + grub_strlen (newsuffix) + 1);
c294fc
+	    suffix = grub_realloc (suffix, sz);
c294fc
 	    if (!suffix)
c294fc
 	      {
c294fc
 		grub_free (t);
c294fc
+
c294fc
+ fail_0:
c294fc
 		grub_free (entrysrc);
c294fc
 		grub_free (parsed);
c294fc
 		grub_free (newsuffix);
c294fc
@@ -154,13 +164,22 @@ legacy_file (const char *filename)
c294fc
 	  else
c294fc
 	    {
c294fc
 	      char *t;
c294fc
+	      grub_size_t sz;
c294fc
+
c294fc
+	      if (grub_add (grub_strlen (entrysrc), grub_strlen (parsed), &sz) ||
c294fc
+		  grub_add (sz, 1, &sz))
c294fc
+		{
c294fc
+		  grub_errno = GRUB_ERR_OUT_OF_RANGE;
c294fc
+		  goto fail_1;
c294fc
+		}
c294fc
 
c294fc
 	      t = entrysrc;
c294fc
-	      entrysrc = grub_realloc (entrysrc, grub_strlen (entrysrc)
c294fc
-				       + grub_strlen (parsed) + 1);
c294fc
+	      entrysrc = grub_realloc (entrysrc, sz);
c294fc
 	      if (!entrysrc)
c294fc
 		{
c294fc
 		  grub_free (t);
c294fc
+
c294fc
+ fail_1:
c294fc
 		  grub_free (parsed);
c294fc
 		  grub_free (suffix);
c294fc
 		  return grub_errno;
c294fc
diff --git a/grub-core/commands/wildcard.c b/grub-core/commands/wildcard.c
c294fc
index 02c46f9fdfa..c223411371e 100644
c294fc
--- a/grub-core/commands/wildcard.c
c294fc
+++ b/grub-core/commands/wildcard.c
c294fc
@@ -23,6 +23,7 @@
c294fc
 #include <grub/file.h>
c294fc
 #include <grub/device.h>
c294fc
 #include <grub/script_sh.h>
c294fc
+#include <grub/safemath.h>
c294fc
 
c294fc
 #include <regex.h>
c294fc
 
c294fc
@@ -48,6 +49,7 @@ merge (char **dest, char **ps)
c294fc
   int i;
c294fc
   int j;
c294fc
   char **p;
c294fc
+  grub_size_t sz;
c294fc
 
c294fc
   if (! dest)
c294fc
     return ps;
c294fc
@@ -60,7 +62,12 @@ merge (char **dest, char **ps)
c294fc
   for (j = 0; ps[j]; j++)
c294fc
     ;
c294fc
 
c294fc
-  p = grub_realloc (dest, sizeof (char*) * (i + j + 1));
c294fc
+  if (grub_add (i, j, &sz) ||
c294fc
+      grub_add (sz, 1, &sz) ||
c294fc
+      grub_mul (sz, sizeof (char *), &sz))
c294fc
+    return dest;
c294fc
+
c294fc
+  p = grub_realloc (dest, sz);
c294fc
   if (! p)
c294fc
     {
c294fc
       grub_free (dest);
c294fc
@@ -115,8 +122,15 @@ make_regex (const char *start, const char *end, regex_t *regexp)
c294fc
   char ch;
c294fc
   int i = 0;
c294fc
   unsigned len = end - start;
c294fc
-  char *buffer = grub_malloc (len * 2 + 2 + 1); /* worst case size. */
c294fc
+  char *buffer;
c294fc
+  grub_size_t sz;
c294fc
 
c294fc
+  /* Worst case size is (len * 2 + 2 + 1). */
c294fc
+  if (grub_mul (len, 2, &sz) ||
c294fc
+      grub_add (sz, 3, &sz))
c294fc
+    return 1;
c294fc
+
c294fc
+  buffer = grub_malloc (sz);
c294fc
   if (! buffer)
c294fc
     return 1;
c294fc
 
c294fc
@@ -226,6 +240,7 @@ match_devices_iter (const char *name, void *data)
c294fc
   struct match_devices_ctx *ctx = data;
c294fc
   char **t;
c294fc
   char *buffer;
c294fc
+  grub_size_t sz;
c294fc
 
c294fc
   /* skip partitions if asked to. */
c294fc
   if (ctx->noparts && grub_strchr (name, ','))
c294fc
@@ -239,11 +254,16 @@ match_devices_iter (const char *name, void *data)
c294fc
   if (regexec (ctx->regexp, buffer, 0, 0, 0))
c294fc
     {
c294fc
       grub_dprintf ("expand", "not matched\n");
c294fc
+ fail:
c294fc
       grub_free (buffer);
c294fc
       return 0;
c294fc
     }
c294fc
 
c294fc
-  t = grub_realloc (ctx->devs, sizeof (char*) * (ctx->ndev + 2));
c294fc
+  if (grub_add (ctx->ndev, 2, &sz) ||
c294fc
+      grub_mul (sz, sizeof (char *), &sz))
c294fc
+    goto fail;
c294fc
+
c294fc
+  t = grub_realloc (ctx->devs, sz);
c294fc
   if (! t)
c294fc
     {
c294fc
       grub_free (buffer);
c294fc
@@ -300,6 +320,7 @@ match_files_iter (const char *name,
c294fc
   struct match_files_ctx *ctx = data;
c294fc
   char **t;
c294fc
   char *buffer;
c294fc
+  grub_size_t sz;
c294fc
 
c294fc
   /* skip . and .. names */
c294fc
   if (grub_strcmp(".", name) == 0 || grub_strcmp("..", name) == 0)
c294fc
@@ -315,9 +336,14 @@ match_files_iter (const char *name,
c294fc
   if (! buffer)
c294fc
     return 1;
c294fc
 
c294fc
-  t = grub_realloc (ctx->files, sizeof (char*) * (ctx->nfile + 2));
c294fc
-  if (! t)
c294fc
+  if (grub_add (ctx->nfile, 2, &sz) ||
c294fc
+      grub_mul (sz, sizeof (char *), &sz))
c294fc
+    goto fail;
c294fc
+
c294fc
+  t = grub_realloc (ctx->files, sz);
c294fc
+  if (!t)
c294fc
     {
c294fc
+ fail:
c294fc
       grub_free (buffer);
c294fc
       return 1;
c294fc
     }
c294fc
diff --git a/grub-core/disk/ldm.c b/grub-core/disk/ldm.c
c294fc
index e6323701ab3..58f8a53e1ab 100644
c294fc
--- a/grub-core/disk/ldm.c
c294fc
+++ b/grub-core/disk/ldm.c
c294fc
@@ -25,6 +25,7 @@
c294fc
 #include <grub/msdos_partition.h>
c294fc
 #include <grub/gpt_partition.h>
c294fc
 #include <grub/i18n.h>
c294fc
+#include <grub/safemath.h>
c294fc
 
c294fc
 #ifdef GRUB_UTIL
c294fc
 #include <grub/emu/misc.h>
c294fc
@@ -289,6 +290,7 @@ make_vg (grub_disk_t disk,
c294fc
       struct grub_ldm_vblk vblk[GRUB_DISK_SECTOR_SIZE
c294fc
 				/ sizeof (struct grub_ldm_vblk)];
c294fc
       unsigned i;
c294fc
+      grub_size_t sz;
c294fc
       err = grub_disk_read (disk, cursec, 0,
c294fc
 			    sizeof(vblk), &vblk);
c294fc
       if (err)
c294fc
@@ -350,7 +352,13 @@ make_vg (grub_disk_t disk,
c294fc
 	      grub_free (lv);
c294fc
 	      goto fail2;
c294fc
 	    }
c294fc
-	  lv->name = grub_malloc (*ptr + 1);
c294fc
+	  if (grub_add (*ptr, 1, &sz))
c294fc
+	    {
c294fc
+	      grub_free (lv->internal_id);
c294fc
+	      grub_free (lv);
c294fc
+	      goto fail2;
c294fc
+	    }
c294fc
+	  lv->name = grub_malloc (sz);
c294fc
 	  if (!lv->name)
c294fc
 	    {
c294fc
 	      grub_free (lv->internal_id);
c294fc
@@ -599,10 +607,13 @@ make_vg (grub_disk_t disk,
c294fc
 	  if (lv->segments->node_alloc == lv->segments->node_count)
c294fc
 	    {
c294fc
 	      void *t;
c294fc
-	      lv->segments->node_alloc *= 2; 
c294fc
-	      t = grub_realloc (lv->segments->nodes,
c294fc
-				sizeof (*lv->segments->nodes)
c294fc
-				* lv->segments->node_alloc);
c294fc
+	      grub_size_t sz;
c294fc
+
c294fc
+	      if (grub_mul (lv->segments->node_alloc, 2, &lv->segments->node_alloc) ||
c294fc
+		  grub_mul (lv->segments->node_alloc, sizeof (*lv->segments->nodes), &sz))
c294fc
+		goto fail2;
c294fc
+
c294fc
+	      t = grub_realloc (lv->segments->nodes, sz);
c294fc
 	      if (!t)
c294fc
 		goto fail2;
c294fc
 	      lv->segments->nodes = t;
c294fc
@@ -723,10 +734,13 @@ make_vg (grub_disk_t disk,
c294fc
 	      if (comp->segment_alloc == comp->segment_count)
c294fc
 		{
c294fc
 		  void *t;
c294fc
-		  comp->segment_alloc *= 2;
c294fc
-		  t = grub_realloc (comp->segments,
c294fc
-				    comp->segment_alloc
c294fc
-				    * sizeof (*comp->segments));
c294fc
+		  grub_size_t sz;
c294fc
+
c294fc
+		  if (grub_mul (comp->segment_alloc, 2, &comp->segment_alloc) ||
c294fc
+		      grub_mul (comp->segment_alloc, sizeof (*comp->segments), &sz))
c294fc
+		    goto fail2;
c294fc
+
c294fc
+		  t = grub_realloc (comp->segments, sz);
c294fc
 		  if (!t)
c294fc
 		    goto fail2;
c294fc
 		  comp->segments = t;
c294fc
diff --git a/grub-core/font/font.c b/grub-core/font/font.c
c294fc
index 68967dc1c2b..d63354fb51b 100644
c294fc
--- a/grub-core/font/font.c
c294fc
+++ b/grub-core/font/font.c
c294fc
@@ -30,6 +30,7 @@
c294fc
 #include <grub/unicode.h>
c294fc
 #include <grub/fontformat.h>
c294fc
 #include <grub/env.h>
c294fc
+#include <grub/safemath.h>
c294fc
 
c294fc
 GRUB_MOD_LICENSE ("GPLv3+");
c294fc
 
c294fc
@@ -360,9 +361,13 @@ static char *
c294fc
 read_section_as_string (struct font_file_section *section)
c294fc
 {
c294fc
   char *str;
c294fc
+  grub_size_t sz;
c294fc
   grub_ssize_t ret;
c294fc
 
c294fc
-  str = grub_malloc (section->length + 1);
c294fc
+  if (grub_add (section->length, 1, &sz))
c294fc
+    return NULL;
c294fc
+
c294fc
+  str = grub_malloc (sz);
c294fc
   if (!str)
c294fc
     return 0;
c294fc
 
c294fc
diff --git a/grub-core/fs/btrfs.c b/grub-core/fs/btrfs.c
c294fc
index 8c8aa9c3103..1d801f6c9ee 100644
c294fc
--- a/grub-core/fs/btrfs.c
c294fc
+++ b/grub-core/fs/btrfs.c
c294fc
@@ -33,6 +33,7 @@
c294fc
 #include <grub/env.h>
c294fc
 #include <grub/extcmd.h>
c294fc
 #include <grub/list.h>
c294fc
+#include <grub/safemath.h>
c294fc
 
c294fc
 GRUB_MOD_LICENSE ("GPLv3+");
c294fc
 
c294fc
@@ -321,9 +322,13 @@ save_ref (struct grub_btrfs_leaf_descriptor *desc,
c294fc
   if (desc->allocated < desc->depth)
c294fc
     {
c294fc
       void *newdata;
c294fc
-      desc->allocated *= 2;
c294fc
-      newdata = grub_realloc (desc->data, sizeof (desc->data[0])
c294fc
-			      * desc->allocated);
c294fc
+      grub_size_t sz;
c294fc
+
c294fc
+      if (grub_mul (desc->allocated, 2, &desc->allocated) ||
c294fc
+	  grub_mul (desc->allocated, sizeof (desc->data[0]), &sz))
c294fc
+	return GRUB_ERR_OUT_OF_RANGE;
c294fc
+
c294fc
+      newdata = grub_realloc (desc->data, sz);
c294fc
       if (!newdata)
c294fc
 	return grub_errno;
c294fc
       desc->data = newdata;
c294fc
@@ -618,15 +623,21 @@ find_device (struct grub_btrfs_data *data, grub_uint64_t id, int do_rescan)
c294fc
   if (data->n_devices_attached > data->n_devices_allocated)
c294fc
     {
c294fc
       void *tmp;
c294fc
-      data->n_devices_allocated = 2 * data->n_devices_attached + 1;
c294fc
-      data->devices_attached
c294fc
-	= grub_realloc (tmp = data->devices_attached,
c294fc
-			data->n_devices_allocated
c294fc
-			* sizeof (data->devices_attached[0]));
c294fc
+      grub_size_t sz;
c294fc
+
c294fc
+      if (grub_mul (data->n_devices_attached, 2, &data->n_devices_allocated) ||
c294fc
+	  grub_add (data->n_devices_allocated, 1, &data->n_devices_allocated) ||
c294fc
+	  grub_mul (data->n_devices_allocated, sizeof (data->devices_attached[0]), &sz))
c294fc
+	goto fail;
c294fc
+
c294fc
+      data->devices_attached = grub_realloc (tmp = data->devices_attached, sz);
c294fc
       if (!data->devices_attached)
c294fc
 	{
c294fc
-	  grub_device_close (ctx.dev_found);
c294fc
 	  data->devices_attached = tmp;
c294fc
+
c294fc
+ fail:
c294fc
+	  if (ctx.dev_found)
c294fc
+	    grub_device_close (ctx.dev_found);
c294fc
 	  return NULL;
c294fc
 	}
c294fc
     }
c294fc
diff --git a/grub-core/fs/ext2.c b/grub-core/fs/ext2.c
c294fc
index b8ad75a0ff7..b4bd019f49a 100644
c294fc
--- a/grub-core/fs/ext2.c
c294fc
+++ b/grub-core/fs/ext2.c
c294fc
@@ -46,6 +46,7 @@
c294fc
 #include <grub/dl.h>
c294fc
 #include <grub/types.h>
c294fc
 #include <grub/fshelp.h>
c294fc
+#include <grub/safemath.h>
c294fc
 
c294fc
 GRUB_MOD_LICENSE ("GPLv3+");
c294fc
 
c294fc
@@ -703,6 +704,7 @@ grub_ext2_read_symlink (grub_fshelp_node_t node)
c294fc
 {
c294fc
   char *symlink;
c294fc
   struct grub_fshelp_node *diro = node;
c294fc
+  grub_size_t sz;
c294fc
 
c294fc
   if (! diro->inode_read)
c294fc
     {
c294fc
@@ -717,7 +719,13 @@ grub_ext2_read_symlink (grub_fshelp_node_t node)
c294fc
        }
c294fc
     }
c294fc
 
c294fc
-  symlink = grub_malloc (grub_le_to_cpu32 (diro->inode.size) + 1);
c294fc
+  if (grub_add (grub_le_to_cpu32 (diro->inode.size), 1, &sz))
c294fc
+    {
c294fc
+      grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
c294fc
+      return NULL;
c294fc
+    }
c294fc
+
c294fc
+  symlink = grub_malloc (sz);
c294fc
   if (! symlink)
c294fc
     return 0;
c294fc
 
c294fc
diff --git a/grub-core/fs/iso9660.c b/grub-core/fs/iso9660.c
c294fc
index 092b8f409ec..f45841e2b47 100644
c294fc
--- a/grub-core/fs/iso9660.c
c294fc
+++ b/grub-core/fs/iso9660.c
c294fc
@@ -28,6 +28,7 @@
c294fc
 #include <grub/fshelp.h>
c294fc
 #include <grub/charset.h>
c294fc
 #include <grub/datetime.h>
c294fc
+#include <grub/safemath.h>
c294fc
 
c294fc
 GRUB_MOD_LICENSE ("GPLv3+");
c294fc
 
c294fc
@@ -531,8 +532,13 @@ add_part (struct iterate_dir_ctx *ctx,
c294fc
 	  int len2)
c294fc
 {
c294fc
   int size = ctx->symlink ? grub_strlen (ctx->symlink) : 0;
c294fc
+  grub_size_t sz;
c294fc
 
c294fc
-  ctx->symlink = grub_realloc (ctx->symlink, size + len2 + 1);
c294fc
+  if (grub_add (size, len2, &sz) ||
c294fc
+      grub_add (sz, 1, &sz))
c294fc
+    return;
c294fc
+
c294fc
+  ctx->symlink = grub_realloc (ctx->symlink, sz);
c294fc
   if (! ctx->symlink)
c294fc
     return;
c294fc
 
c294fc
@@ -560,17 +566,24 @@ susp_iterate_dir (struct grub_iso9660_susp_entry *entry,
c294fc
 	{
c294fc
 	  grub_size_t off = 0, csize = 1;
c294fc
 	  char *old;
c294fc
+	  grub_size_t sz;
c294fc
+
c294fc
 	  csize = entry->len - 5;
c294fc
 	  old = ctx->filename;
c294fc
 	  if (ctx->filename_alloc)
c294fc
 	    {
c294fc
 	      off = grub_strlen (ctx->filename);
c294fc
-	      ctx->filename = grub_realloc (ctx->filename, csize + off + 1);
c294fc
+	      if (grub_add (csize, off, &sz) ||
c294fc
+		  grub_add (sz, 1, &sz))
c294fc
+		return GRUB_ERR_OUT_OF_RANGE;
c294fc
+	      ctx->filename = grub_realloc (ctx->filename, sz);
c294fc
 	    }
c294fc
 	  else
c294fc
 	    {
c294fc
 	      off = 0;
c294fc
-	      ctx->filename = grub_zalloc (csize + 1);
c294fc
+	      if (grub_add (csize, 1, &sz))
c294fc
+		return GRUB_ERR_OUT_OF_RANGE;
c294fc
+	      ctx->filename = grub_zalloc (sz);
c294fc
 	    }
c294fc
 	  if (!ctx->filename)
c294fc
 	    {
c294fc
@@ -776,14 +789,18 @@ grub_iso9660_iterate_dir (grub_fshelp_node_t dir,
c294fc
 	    if (node->have_dirents >= node->alloc_dirents)
c294fc
 	      {
c294fc
 		struct grub_fshelp_node *new_node;
c294fc
-		node->alloc_dirents *= 2;
c294fc
-		new_node = grub_realloc (node, 
c294fc
-					 sizeof (struct grub_fshelp_node)
c294fc
-					 + ((node->alloc_dirents
c294fc
-					     - ARRAY_SIZE (node->dirents))
c294fc
-					    * sizeof (node->dirents[0])));
c294fc
+		grub_size_t sz;
c294fc
+
c294fc
+		if (grub_mul (node->alloc_dirents, 2, &node->alloc_dirents) ||
c294fc
+		    grub_sub (node->alloc_dirents, ARRAY_SIZE (node->dirents), &sz) ||
c294fc
+		    grub_mul (sz, sizeof (node->dirents[0]), &sz) ||
c294fc
+		    grub_add (sz, sizeof (struct grub_fshelp_node), &sz))
c294fc
+		  goto fail_0;
c294fc
+
c294fc
+		new_node = grub_realloc (node, sz);
c294fc
 		if (!new_node)
c294fc
 		  {
c294fc
+ fail_0:
c294fc
 		    if (ctx.filename_alloc)
c294fc
 		      grub_free (ctx.filename);
c294fc
 		    grub_free (node);
c294fc
@@ -799,14 +816,18 @@ grub_iso9660_iterate_dir (grub_fshelp_node_t dir,
c294fc
 		* sizeof (node->dirents[0]) < grub_strlen (ctx.symlink) + 1)
c294fc
 	      {
c294fc
 		struct grub_fshelp_node *new_node;
c294fc
-		new_node = grub_realloc (node,
c294fc
-					 sizeof (struct grub_fshelp_node)
c294fc
-					 + ((node->alloc_dirents
c294fc
-					     - ARRAY_SIZE (node->dirents))
c294fc
-					    * sizeof (node->dirents[0]))
c294fc
-					 + grub_strlen (ctx.symlink) + 1);
c294fc
+		grub_size_t sz;
c294fc
+
c294fc
+		if (grub_sub (node->alloc_dirents, ARRAY_SIZE (node->dirents), &sz) ||
c294fc
+		    grub_mul (sz, sizeof (node->dirents[0]), &sz) ||
c294fc
+		    grub_add (sz, sizeof (struct grub_fshelp_node) + 1, &sz) ||
c294fc
+		    grub_add (sz, grub_strlen (ctx.symlink), &sz))
c294fc
+		  goto fail_1;
c294fc
+
c294fc
+		new_node = grub_realloc (node, sz);
c294fc
 		if (!new_node)
c294fc
 		  {
c294fc
+ fail_1:
c294fc
 		    if (ctx.filename_alloc)
c294fc
 		      grub_free (ctx.filename);
c294fc
 		    grub_free (node);
c294fc
diff --git a/grub-core/fs/sfs.c b/grub-core/fs/sfs.c
c294fc
index 663931717fd..3ddc6b5e287 100644
c294fc
--- a/grub-core/fs/sfs.c
c294fc
+++ b/grub-core/fs/sfs.c
c294fc
@@ -26,6 +26,7 @@
c294fc
 #include <grub/types.h>
c294fc
 #include <grub/fshelp.h>
c294fc
 #include <grub/charset.h>
c294fc
+#include <grub/safemath.h>
c294fc
 
c294fc
 GRUB_MOD_LICENSE ("GPLv3+");
c294fc
 
c294fc
@@ -307,10 +308,15 @@ grub_sfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
c294fc
       if (node->cache && node->cache_size >= node->cache_allocated)
c294fc
 	{
c294fc
 	  struct cache_entry *e = node->cache;
c294fc
-	  e = grub_realloc (node->cache,node->cache_allocated * 2
c294fc
-			    * sizeof (e[0]));
c294fc
+	  grub_size_t sz;
c294fc
+
c294fc
+	  if (grub_mul (node->cache_allocated, 2 * sizeof (e[0]), &sz))
c294fc
+	    goto fail;
c294fc
+
c294fc
+	  e = grub_realloc (node->cache, sz);
c294fc
 	  if (!e)
c294fc
 	    {
c294fc
+ fail:
c294fc
 	      grub_errno = 0;
c294fc
 	      grub_free (node->cache);
c294fc
 	      node->cache = 0;
c294fc
@@ -477,10 +483,16 @@ grub_sfs_create_node (struct grub_fshelp_node **node,
c294fc
   grub_size_t len = grub_strlen (name);
c294fc
   grub_uint8_t *name_u8;
c294fc
   int ret;
c294fc
+  grub_size_t sz;
c294fc
+
c294fc
+  if (grub_mul (len, GRUB_MAX_UTF8_PER_LATIN1, &sz) ||
c294fc
+      grub_add (sz, 1, &sz))
c294fc
+    return 1;
c294fc
+
c294fc
   *node = grub_malloc (sizeof (**node));
c294fc
   if (!*node)
c294fc
     return 1;
c294fc
-  name_u8 = grub_malloc (len * GRUB_MAX_UTF8_PER_LATIN1 + 1);
c294fc
+  name_u8 = grub_malloc (sz);
c294fc
   if (!name_u8)
c294fc
     {
c294fc
       grub_free (*node);
c294fc
@@ -724,8 +736,13 @@ grub_sfs_label (grub_device_t device, char **label)
c294fc
   data = grub_sfs_mount (disk);
c294fc
   if (data)
c294fc
     {
c294fc
-      grub_size_t len = grub_strlen (data->label);
c294fc
-      *label = grub_malloc (len * GRUB_MAX_UTF8_PER_LATIN1 + 1);
c294fc
+      grub_size_t sz, len = grub_strlen (data->label);
c294fc
+
c294fc
+      if (grub_mul (len, GRUB_MAX_UTF8_PER_LATIN1, &sz) ||
c294fc
+	  grub_add (sz, 1, &sz))
c294fc
+	return GRUB_ERR_OUT_OF_RANGE;
c294fc
+
c294fc
+      *label = grub_malloc (sz);
c294fc
       if (*label)
c294fc
 	*grub_latin1_to_utf8 ((grub_uint8_t *) *label,
c294fc
 			      (const grub_uint8_t *) data->label,
c294fc
diff --git a/grub-core/fs/squash4.c b/grub-core/fs/squash4.c
c294fc
index 2c967c65a42..f9bef38fc42 100644
c294fc
--- a/grub-core/fs/squash4.c
c294fc
+++ b/grub-core/fs/squash4.c
c294fc
@@ -26,6 +26,7 @@
c294fc
 #include <grub/types.h>
c294fc
 #include <grub/fshelp.h>
c294fc
 #include <grub/deflate.h>
c294fc
+#include <grub/safemath.h>
c294fc
 #include <minilzo.h>
c294fc
 
c294fc
 #include "xz.h"
c294fc
@@ -459,7 +460,17 @@ grub_squash_read_symlink (grub_fshelp_node_t node)
c294fc
 {
c294fc
   char *ret;
c294fc
   grub_err_t err;
c294fc
-  ret = grub_malloc (grub_le_to_cpu32 (node->ino.symlink.namelen) + 1);
c294fc
+  grub_size_t sz;
c294fc
+
c294fc
+  if (grub_add (grub_le_to_cpu32 (node->ino.symlink.namelen), 1, &sz))
c294fc
+    {
c294fc
+      grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
c294fc
+      return NULL;
c294fc
+    }
c294fc
+
c294fc
+  ret = grub_malloc (sz);
c294fc
+  if (!ret)
c294fc
+    return NULL;
c294fc
 
c294fc
   err = read_chunk (node->data, ret,
c294fc
 		    grub_le_to_cpu32 (node->ino.symlink.namelen),
c294fc
@@ -506,11 +517,16 @@ grub_squash_iterate_dir (grub_fshelp_node_t dir,
c294fc
 
c294fc
   {
c294fc
     grub_fshelp_node_t node;
c294fc
-    node = grub_malloc (sizeof (*node) + dir->stsize * sizeof (dir->stack[0]));
c294fc
+    grub_size_t sz;
c294fc
+
c294fc
+    if (grub_mul (dir->stsize, sizeof (dir->stack[0]), &sz) ||
c294fc
+	grub_add (sz, sizeof (*node), &sz))
c294fc
+      return 0;
c294fc
+
c294fc
+    node = grub_malloc (sz);
c294fc
     if (!node)
c294fc
       return 0;
c294fc
-    grub_memcpy (node, dir,
c294fc
-		 sizeof (*node) + dir->stsize * sizeof (dir->stack[0]));
c294fc
+    grub_memcpy (node, dir, sz);
c294fc
     if (hook (".", GRUB_FSHELP_DIR, node, hook_data))
c294fc
       return 1;
c294fc
 
c294fc
@@ -518,12 +534,15 @@ grub_squash_iterate_dir (grub_fshelp_node_t dir,
c294fc
       {
c294fc
 	grub_err_t err;
c294fc
 
c294fc
-	node = grub_malloc (sizeof (*node) + dir->stsize * sizeof (dir->stack[0]));
c294fc
+	if (grub_mul (dir->stsize, sizeof (dir->stack[0]), &sz) ||
c294fc
+	    grub_add (sz, sizeof (*node), &sz))
c294fc
+	  return 0;
c294fc
+
c294fc
+	node = grub_malloc (sz);
c294fc
 	if (!node)
c294fc
 	  return 0;
c294fc
 
c294fc
-	grub_memcpy (node, dir,
c294fc
-		     sizeof (*node) + dir->stsize * sizeof (dir->stack[0]));
c294fc
+	grub_memcpy (node, dir, sz);
c294fc
 
c294fc
 	node->stsize--;
c294fc
 	err = read_chunk (dir->data, &node->ino, sizeof (node->ino),
c294fc
@@ -557,6 +576,7 @@ grub_squash_iterate_dir (grub_fshelp_node_t dir,
c294fc
 	  enum grub_fshelp_filetype filetype = GRUB_FSHELP_REG;
c294fc
 	  struct grub_squash_dirent di;
c294fc
 	  struct grub_squash_inode ino;
c294fc
+	  grub_size_t sz;
c294fc
 
c294fc
 	  err = read_chunk (dir->data, &di, sizeof (di),
c294fc
 			    grub_le_to_cpu64 (dir->data->sb.diroffset)
c294fc
@@ -589,13 +609,16 @@ grub_squash_iterate_dir (grub_fshelp_node_t dir,
c294fc
 	  if (grub_le_to_cpu16 (di.type) == SQUASH_TYPE_SYMLINK)
c294fc
 	    filetype = GRUB_FSHELP_SYMLINK;
c294fc
 
c294fc
-	  node = grub_malloc (sizeof (*node)
c294fc
-			      + (dir->stsize + 1) * sizeof (dir->stack[0]));
c294fc
+	  if (grub_add (dir->stsize, 1, &sz) ||
c294fc
+	      grub_mul (sz, sizeof (dir->stack[0]), &sz) ||
c294fc
+	      grub_add (sz, sizeof (*node), &sz))
c294fc
+	    return 0;
c294fc
+
c294fc
+	  node = grub_malloc (sz);
c294fc
 	  if (! node)
c294fc
 	    return 0;
c294fc
 
c294fc
-	  grub_memcpy (node, dir,
c294fc
-		       sizeof (*node) + dir->stsize * sizeof (dir->stack[0]));
c294fc
+	  grub_memcpy (node, dir, sz - sizeof(dir->stack[0]));
c294fc
 
c294fc
 	  node->ino = ino;
c294fc
 	  node->stack[node->stsize].ino_chunk = grub_le_to_cpu32 (dh.ino_chunk);
c294fc
diff --git a/grub-core/fs/udf.c b/grub-core/fs/udf.c
c294fc
index 44481da7c6d..be41b48f913 100644
c294fc
--- a/grub-core/fs/udf.c
c294fc
+++ b/grub-core/fs/udf.c
c294fc
@@ -28,6 +28,7 @@
c294fc
 #include <grub/charset.h>
c294fc
 #include <grub/datetime.h>
c294fc
 #include <grub/udf.h>
c294fc
+#include <grub/safemath.h>
c294fc
 
c294fc
 GRUB_MOD_LICENSE ("GPLv3+");
c294fc
 
c294fc
@@ -890,9 +891,19 @@ read_string (const grub_uint8_t *raw, grub_size_t sz, char *outbuf)
c294fc
 	utf16[i] = (raw[2 * i + 1] << 8) | raw[2*i + 2];
c294fc
     }
c294fc
   if (!outbuf)
c294fc
-    outbuf = grub_malloc (utf16len * GRUB_MAX_UTF8_PER_UTF16 + 1);
c294fc
+    {
c294fc
+      grub_size_t size;
c294fc
+
c294fc
+      if (grub_mul (utf16len, GRUB_MAX_UTF8_PER_UTF16, &size) ||
c294fc
+	  grub_add (size, 1, &size))
c294fc
+	goto fail;
c294fc
+
c294fc
+      outbuf = grub_malloc (size);
c294fc
+    }
c294fc
   if (outbuf)
c294fc
     *grub_utf16_to_utf8 ((grub_uint8_t *) outbuf, utf16, utf16len) = '\0';
c294fc
+
c294fc
+ fail:
c294fc
   grub_free (utf16);
c294fc
   return outbuf;
c294fc
 }
c294fc
@@ -1005,7 +1016,7 @@ grub_udf_read_symlink (grub_fshelp_node_t node)
c294fc
   grub_size_t sz = U64 (node->block.fe.file_size);
c294fc
   grub_uint8_t *raw;
c294fc
   const grub_uint8_t *ptr;
c294fc
-  char *out, *optr;
c294fc
+  char *out = NULL, *optr;
c294fc
 
c294fc
   if (sz < 4)
c294fc
     return NULL;
c294fc
@@ -1013,14 +1024,16 @@ grub_udf_read_symlink (grub_fshelp_node_t node)
c294fc
   if (!raw)
c294fc
     return NULL;
c294fc
   if (grub_udf_read_file (node, NULL, NULL, 0, sz, (char *) raw) < 0)
c294fc
-    {
c294fc
-      grub_free (raw);
c294fc
-      return NULL;
c294fc
-    }
c294fc
+    goto fail_1;
c294fc
 
c294fc
-  out = grub_malloc (sz * 2 + 1);
c294fc
+  if (grub_mul (sz, 2, &sz) ||
c294fc
+      grub_add (sz, 1, &sz))
c294fc
+    goto fail_0;
c294fc
+
c294fc
+  out = grub_malloc (sz);
c294fc
   if (!out)
c294fc
     {
c294fc
+ fail_0:
c294fc
       grub_free (raw);
c294fc
       return NULL;
c294fc
     }
c294fc
@@ -1031,17 +1044,17 @@ grub_udf_read_symlink (grub_fshelp_node_t node)
c294fc
     {
c294fc
       grub_size_t s;
c294fc
       if ((grub_size_t) (ptr - raw + 4) > sz)
c294fc
-	goto fail;
c294fc
+	goto fail_1;
c294fc
       if (!(ptr[2] == 0 && ptr[3] == 0))
c294fc
-	goto fail;
c294fc
+	goto fail_1;
c294fc
       s = 4 + ptr[1];
c294fc
       if ((grub_size_t) (ptr - raw + s) > sz)
c294fc
-	goto fail;
c294fc
+	goto fail_1;
c294fc
       switch (*ptr)
c294fc
 	{
c294fc
 	case 1:
c294fc
 	  if (ptr[1])
c294fc
-	    goto fail;
c294fc
+	    goto fail_1;
c294fc
 	  /* Fallthrough.  */
c294fc
 	case 2:
c294fc
 	  /* in 4 bytes. out: 1 byte.  */
c294fc
@@ -1066,11 +1079,11 @@ grub_udf_read_symlink (grub_fshelp_node_t node)
c294fc
 	  if (optr != out)
c294fc
 	    *optr++ = '/';
c294fc
 	  if (!read_string (ptr + 4, s - 4, optr))
c294fc
-	    goto fail;
c294fc
+	    goto fail_1;
c294fc
 	  optr += grub_strlen (optr);
c294fc
 	  break;
c294fc
 	default:
c294fc
-	  goto fail;
c294fc
+	  goto fail_1;
c294fc
 	}
c294fc
       ptr += s;
c294fc
     }
c294fc
@@ -1078,7 +1091,7 @@ grub_udf_read_symlink (grub_fshelp_node_t node)
c294fc
   grub_free (raw);
c294fc
   return out;
c294fc
 
c294fc
- fail:
c294fc
+ fail_1:
c294fc
   grub_free (raw);
c294fc
   grub_free (out);
c294fc
   grub_error (GRUB_ERR_BAD_FS, "invalid symlink");
c294fc
diff --git a/grub-core/fs/xfs.c b/grub-core/fs/xfs.c
c294fc
index 3b00c744e23..66e66dd58cc 100644
c294fc
--- a/grub-core/fs/xfs.c
c294fc
+++ b/grub-core/fs/xfs.c
c294fc
@@ -25,6 +25,7 @@
c294fc
 #include <grub/dl.h>
c294fc
 #include <grub/types.h>
c294fc
 #include <grub/fshelp.h>
c294fc
+#include <grub/safemath.h>
c294fc
 
c294fc
 GRUB_MOD_LICENSE ("GPLv3+");
c294fc
 
c294fc
@@ -899,6 +900,7 @@ static struct grub_xfs_data *
c294fc
 grub_xfs_mount (grub_disk_t disk)
c294fc
 {
c294fc
   struct grub_xfs_data *data = 0;
c294fc
+  grub_size_t sz;
c294fc
 
c294fc
   data = grub_zalloc (sizeof (struct grub_xfs_data));
c294fc
   if (!data)
c294fc
@@ -913,10 +915,11 @@ grub_xfs_mount (grub_disk_t disk)
c294fc
   if (!grub_xfs_sb_valid(data))
c294fc
     goto fail;
c294fc
 
c294fc
-  data = grub_realloc (data,
c294fc
-		       sizeof (struct grub_xfs_data)
c294fc
-		       - sizeof (struct grub_xfs_inode)
c294fc
-		       + grub_xfs_inode_size(data) + 1);
c294fc
+  if (grub_add (grub_xfs_inode_size (data),
c294fc
+      sizeof (struct grub_xfs_data) - sizeof (struct grub_xfs_inode) + 1, &sz))
c294fc
+    goto fail;
c294fc
+
c294fc
+  data = grub_realloc (data, sz);
c294fc
 
c294fc
   if (! data)
c294fc
     goto fail;
c294fc
diff --git a/grub-core/fs/zfs/zfs.c b/grub-core/fs/zfs/zfs.c
c294fc
index f6b95d4fb02..c6204367e78 100644
c294fc
--- a/grub-core/fs/zfs/zfs.c
c294fc
+++ b/grub-core/fs/zfs/zfs.c
c294fc
@@ -55,6 +55,7 @@
c294fc
 #include <grub/deflate.h>
c294fc
 #include <grub/crypto.h>
c294fc
 #include <grub/i18n.h>
c294fc
+#include <grub/safemath.h>
c294fc
 
c294fc
 GRUB_MOD_LICENSE ("GPLv3+");
c294fc
 
c294fc
@@ -773,11 +774,14 @@ fill_vdev_info (struct grub_zfs_data *data,
c294fc
   if (data->n_devices_attached > data->n_devices_allocated)
c294fc
     {
c294fc
       void *tmp;
c294fc
-      data->n_devices_allocated = 2 * data->n_devices_attached + 1;
c294fc
-      data->devices_attached
c294fc
-	= grub_realloc (tmp = data->devices_attached,
c294fc
-			data->n_devices_allocated
c294fc
-			* sizeof (data->devices_attached[0]));
c294fc
+      grub_size_t sz;
c294fc
+
c294fc
+      if (grub_mul (data->n_devices_attached, 2, &data->n_devices_allocated) ||
c294fc
+	  grub_add (data->n_devices_allocated, 1, &data->n_devices_allocated) ||
c294fc
+	  grub_mul (data->n_devices_allocated, sizeof (data->devices_attached[0]), &sz))
c294fc
+	return GRUB_ERR_OUT_OF_RANGE;
c294fc
+
c294fc
+      data->devices_attached = grub_realloc (tmp = data->devices_attached, sz);
c294fc
       if (!data->devices_attached)
c294fc
 	{
c294fc
 	  data->devices_attached = tmp;
c294fc
@@ -3468,14 +3472,18 @@ grub_zfs_nvlist_lookup_nvlist (const char *nvlist, const char *name)
c294fc
 {
c294fc
   char *nvpair;
c294fc
   char *ret;
c294fc
-  grub_size_t size;
c294fc
+  grub_size_t size, sz;
c294fc
   int found;
c294fc
 
c294fc
   found = nvlist_find_value (nvlist, name, DATA_TYPE_NVLIST, &nvpair,
c294fc
 			     &size, 0);
c294fc
   if (!found)
c294fc
     return 0;
c294fc
-  ret = grub_zalloc (size + 3 * sizeof (grub_uint32_t));
c294fc
+
c294fc
+  if (grub_add (size, 3 * sizeof (grub_uint32_t), &sz))
c294fc
+      return 0;
c294fc
+
c294fc
+  ret = grub_zalloc (sz);
c294fc
   if (!ret)
c294fc
     return 0;
c294fc
   grub_memcpy (ret, nvlist, sizeof (grub_uint32_t));
c294fc
diff --git a/grub-core/fs/zfs/zfscrypt.c b/grub-core/fs/zfs/zfscrypt.c
c294fc
index 87eef621d6d..f8488c35344 100644
c294fc
--- a/grub-core/fs/zfs/zfscrypt.c
c294fc
+++ b/grub-core/fs/zfs/zfscrypt.c
c294fc
@@ -22,6 +22,7 @@
c294fc
 #include <grub/misc.h>
c294fc
 #include <grub/disk.h>
c294fc
 #include <grub/partition.h>
c294fc
+#include <grub/safemath.h>
c294fc
 #include <grub/dl.h>
c294fc
 #include <grub/types.h>
c294fc
 #include <grub/zfs/zfs.h>
c294fc
@@ -82,9 +83,13 @@ grub_zfs_add_key (grub_uint8_t *key_in,
c294fc
 		  int passphrase)
c294fc
 {
c294fc
   struct grub_zfs_wrap_key *key;
c294fc
+  grub_size_t sz;
c294fc
+
c294fc
   if (!passphrase && keylen > 32)
c294fc
     keylen = 32;
c294fc
-  key = grub_malloc (sizeof (*key) + keylen);
c294fc
+  if (grub_add (sizeof (*key), keylen, &sz))
c294fc
+    return GRUB_ERR_OUT_OF_RANGE;
c294fc
+  key = grub_malloc (sz);
c294fc
   if (!key)
c294fc
     return grub_errno;
c294fc
   key->is_passphrase = passphrase;
c294fc
diff --git a/grub-core/lib/arg.c b/grub-core/lib/arg.c
c294fc
index fd7744a6ff6..3288609a5e1 100644
c294fc
--- a/grub-core/lib/arg.c
c294fc
+++ b/grub-core/lib/arg.c
c294fc
@@ -23,6 +23,7 @@
c294fc
 #include <grub/term.h>
c294fc
 #include <grub/extcmd.h>
c294fc
 #include <grub/i18n.h>
c294fc
+#include <grub/safemath.h>
c294fc
 
c294fc
 /* Built-in parser for default options.  */
c294fc
 static const struct grub_arg_option help_options[] =
c294fc
@@ -216,7 +217,13 @@ static inline grub_err_t
c294fc
 add_arg (char ***argl, int *num, char *s)
c294fc
 {
c294fc
   char **p = *argl;
c294fc
-  *argl = grub_realloc (*argl, (++(*num) + 1) * sizeof (char *));
c294fc
+  grub_size_t sz;
c294fc
+
c294fc
+  if (grub_add (++(*num), 1, &sz) ||
c294fc
+      grub_mul (sz, sizeof (char *), &sz))
c294fc
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
c294fc
+
c294fc
+  *argl = grub_realloc (*argl, sz);
c294fc
   if (! *argl)
c294fc
     {
c294fc
       grub_free (p);
c294fc
@@ -431,6 +438,7 @@ grub_arg_list_alloc(grub_extcmd_t extcmd, int argc,
c294fc
   grub_size_t argcnt;
c294fc
   struct grub_arg_list *list;
c294fc
   const struct grub_arg_option *options;
c294fc
+  grub_size_t sz0, sz1;
c294fc
 
c294fc
   options = extcmd->options;
c294fc
   if (! options)
c294fc
@@ -443,7 +451,15 @@ grub_arg_list_alloc(grub_extcmd_t extcmd, int argc,
c294fc
 	argcnt += ((grub_size_t) argc + 1) / 2 + 1; /* max possible for any option */
c294fc
     }
c294fc
 
c294fc
-  list = grub_zalloc (sizeof (*list) * i + sizeof (char*) * argcnt);
c294fc
+  if (grub_mul (sizeof (*list), i, &sz0) ||
c294fc
+      grub_mul (sizeof (char *), argcnt, &sz1) ||
c294fc
+      grub_add (sz0, sz1, &sz0))
c294fc
+    {
c294fc
+      grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
c294fc
+      return 0;
c294fc
+    }
c294fc
+
c294fc
+  list = grub_zalloc (sz0);
c294fc
   if (! list)
c294fc
     return 0;
c294fc
 
c294fc
diff --git a/grub-core/loader/i386/bsd.c b/grub-core/loader/i386/bsd.c
c294fc
index 87709aa23e8..0f317632a3b 100644
c294fc
--- a/grub-core/loader/i386/bsd.c
c294fc
+++ b/grub-core/loader/i386/bsd.c
c294fc
@@ -35,6 +35,7 @@
c294fc
 #include <grub/ns8250.h>
c294fc
 #include <grub/bsdlabel.h>
c294fc
 #include <grub/crypto.h>
c294fc
+#include <grub/safemath.h>
c294fc
 #ifdef GRUB_MACHINE_PCBIOS
c294fc
 #include <grub/machine/int.h>
c294fc
 #endif
c294fc
@@ -1007,11 +1008,16 @@ grub_netbsd_add_modules (void)
c294fc
   struct grub_netbsd_btinfo_modules *mods;
c294fc
   unsigned i;
c294fc
   grub_err_t err;
c294fc
+  grub_size_t sz;
c294fc
 
c294fc
   for (mod = netbsd_mods; mod; mod = mod->next)
c294fc
     modcnt++;
c294fc
 
c294fc
-  mods = grub_malloc (sizeof (*mods) + sizeof (mods->mods[0]) * modcnt);
c294fc
+  if (grub_mul (modcnt, sizeof (mods->mods[0]), &sz) ||
c294fc
+      grub_add (sz, sizeof (*mods), &sz))
c294fc
+    return GRUB_ERR_OUT_OF_RANGE;
c294fc
+
c294fc
+  mods = grub_malloc (sz);
c294fc
   if (!mods)
c294fc
     return grub_errno;
c294fc
 
c294fc
diff --git a/grub-core/net/dns.c b/grub-core/net/dns.c
c294fc
index e332d5eb4a4..906ec7d6782 100644
c294fc
--- a/grub-core/net/dns.c
c294fc
+++ b/grub-core/net/dns.c
c294fc
@@ -22,6 +22,7 @@
c294fc
 #include <grub/i18n.h>
c294fc
 #include <grub/err.h>
c294fc
 #include <grub/time.h>
c294fc
+#include <grub/safemath.h>
c294fc
 
c294fc
 struct dns_cache_element
c294fc
 {
c294fc
@@ -51,9 +52,15 @@ grub_net_add_dns_server (const struct grub_net_network_level_address *s)
c294fc
     {
c294fc
       int na = dns_servers_alloc * 2;
c294fc
       struct grub_net_network_level_address *ns;
c294fc
+      grub_size_t sz;
c294fc
+
c294fc
       if (na < 8)
c294fc
 	na = 8;
c294fc
-      ns = grub_realloc (dns_servers, na * sizeof (ns[0]));
c294fc
+
c294fc
+      if (grub_mul (na, sizeof (ns[0]), &sz))
c294fc
+	return GRUB_ERR_OUT_OF_RANGE;
c294fc
+
c294fc
+      ns = grub_realloc (dns_servers, sz);
c294fc
       if (!ns)
c294fc
 	return grub_errno;
c294fc
       dns_servers_alloc = na;
c294fc
diff --git a/grub-core/normal/charset.c b/grub-core/normal/charset.c
c294fc
index d57fb72faa8..4dfcc31078d 100644
c294fc
--- a/grub-core/normal/charset.c
c294fc
+++ b/grub-core/normal/charset.c
c294fc
@@ -48,6 +48,7 @@
c294fc
 #include <grub/unicode.h>
c294fc
 #include <grub/term.h>
c294fc
 #include <grub/normal.h>
c294fc
+#include <grub/safemath.h>
c294fc
 
c294fc
 #if HAVE_FONT_SOURCE
c294fc
 #include "widthspec.h"
c294fc
@@ -464,6 +465,7 @@ grub_unicode_aglomerate_comb (const grub_uint32_t *in, grub_size_t inlen,
c294fc
 	{
c294fc
 	  struct grub_unicode_combining *n;
c294fc
 	  unsigned j;
c294fc
+	  grub_size_t sz;
c294fc
 
c294fc
 	  if (!haveout)
c294fc
 	    continue;
c294fc
@@ -477,10 +479,14 @@ grub_unicode_aglomerate_comb (const grub_uint32_t *in, grub_size_t inlen,
c294fc
 	    n = out->combining_inline;
c294fc
 	  else if (out->ncomb > (int) ARRAY_SIZE (out->combining_inline))
c294fc
 	    {
c294fc
-	      n = grub_realloc (out->combining_ptr,
c294fc
-				sizeof (n[0]) * (out->ncomb + 1));
c294fc
+	      if (grub_add (out->ncomb, 1, &sz) ||
c294fc
+		  grub_mul (sz, sizeof (n[0]), &sz))
c294fc
+		goto fail;
c294fc
+
c294fc
+	      n = grub_realloc (out->combining_ptr, sz);
c294fc
 	      if (!n)
c294fc
 		{
c294fc
+ fail:
c294fc
 		  grub_errno = GRUB_ERR_NONE;
c294fc
 		  continue;
c294fc
 		}
c294fc
diff --git a/grub-core/normal/cmdline.c b/grub-core/normal/cmdline.c
c294fc
index c57242e2ea9..de03fe63b3d 100644
c294fc
--- a/grub-core/normal/cmdline.c
c294fc
+++ b/grub-core/normal/cmdline.c
c294fc
@@ -28,6 +28,7 @@
c294fc
 #include <grub/env.h>
c294fc
 #include <grub/i18n.h>
c294fc
 #include <grub/charset.h>
c294fc
+#include <grub/safemath.h>
c294fc
 
c294fc
 static grub_uint32_t *kill_buf;
c294fc
 
c294fc
@@ -307,12 +308,21 @@ cl_insert (struct cmdline_term *cl_terms, unsigned nterms,
c294fc
   if (len + (*llen) >= (*max_len))
c294fc
     {
c294fc
       grub_uint32_t *nbuf;
c294fc
-      (*max_len) *= 2;
c294fc
-      nbuf = grub_realloc ((*buf), sizeof (grub_uint32_t) * (*max_len));
c294fc
+      grub_size_t sz;
c294fc
+
c294fc
+      if (grub_mul (*max_len, 2, max_len) ||
c294fc
+	  grub_mul (*max_len, sizeof (grub_uint32_t), &sz))
c294fc
+	{
c294fc
+	  grub_errno = GRUB_ERR_OUT_OF_RANGE;
c294fc
+	  goto fail;
c294fc
+	}
c294fc
+
c294fc
+      nbuf = grub_realloc ((*buf), sz);
c294fc
       if (nbuf)
c294fc
 	(*buf) = nbuf;
c294fc
       else
c294fc
 	{
c294fc
+ fail:
c294fc
 	  grub_print_error ();
c294fc
 	  grub_errno = GRUB_ERR_NONE;
c294fc
 	  (*max_len) /= 2;
c294fc
diff --git a/grub-core/normal/menu_entry.c b/grub-core/normal/menu_entry.c
c294fc
index f31487c1f58..de64a367c4e 100644
c294fc
--- a/grub-core/normal/menu_entry.c
c294fc
+++ b/grub-core/normal/menu_entry.c
c294fc
@@ -27,6 +27,7 @@
c294fc
 #include <grub/auth.h>
c294fc
 #include <grub/i18n.h>
c294fc
 #include <grub/charset.h>
c294fc
+#include <grub/safemath.h>
c294fc
 
c294fc
 enum update_mode
c294fc
   {
c294fc
@@ -113,10 +114,18 @@ ensure_space (struct line *linep, int extra)
c294fc
 {
c294fc
   if (linep->max_len < linep->len + extra)
c294fc
     {
c294fc
-      linep->max_len = 2 * (linep->len + extra);
c294fc
-      linep->buf = grub_realloc (linep->buf, (linep->max_len + 1) * sizeof (linep->buf[0]));
c294fc
+      grub_size_t sz0, sz1;
c294fc
+
c294fc
+      if (grub_add (linep->len, extra, &sz0) ||
c294fc
+	  grub_mul (sz0, 2, &sz0) ||
c294fc
+	  grub_add (sz0, 1, &sz1) ||
c294fc
+	  grub_mul (sz1, sizeof (linep->buf[0]), &sz1))
c294fc
+	return 0;
c294fc
+
c294fc
+      linep->buf = grub_realloc (linep->buf, sz1);
c294fc
       if (! linep->buf)
c294fc
 	return 0;
c294fc
+      linep->max_len = sz0;
c294fc
     }
c294fc
 
c294fc
   return 1;
c294fc
diff --git a/grub-core/script/argv.c b/grub-core/script/argv.c
c294fc
index 217ec5d1e1b..5751fdd5708 100644
c294fc
--- a/grub-core/script/argv.c
c294fc
+++ b/grub-core/script/argv.c
c294fc
@@ -20,6 +20,7 @@
c294fc
 #include <grub/mm.h>
c294fc
 #include <grub/misc.h>
c294fc
 #include <grub/script_sh.h>
c294fc
+#include <grub/safemath.h>
c294fc
 
c294fc
 /* Return nearest power of two that is >= v.  */
c294fc
 static unsigned
c294fc
@@ -81,11 +82,16 @@ int
c294fc
 grub_script_argv_next (struct grub_script_argv *argv)
c294fc
 {
c294fc
   char **p = argv->args;
c294fc
+  grub_size_t sz;
c294fc
 
c294fc
   if (argv->args && argv->argc && argv->args[argv->argc - 1] == 0)
c294fc
     return 0;
c294fc
 
c294fc
-  p = grub_realloc (p, round_up_exp ((argv->argc + 2) * sizeof (char *)));
c294fc
+  if (grub_add (argv->argc, 2, &sz) ||
c294fc
+      grub_mul (sz, sizeof (char *), &sz))
c294fc
+    return 1;
c294fc
+
c294fc
+  p = grub_realloc (p, round_up_exp (sz));
c294fc
   if (! p)
c294fc
     return 1;
c294fc
 
c294fc
@@ -105,13 +111,19 @@ grub_script_argv_append (struct grub_script_argv *argv, const char *s,
c294fc
 {
c294fc
   grub_size_t a;
c294fc
   char *p = argv->args[argv->argc - 1];
c294fc
+  grub_size_t sz;
c294fc
 
c294fc
   if (! s)
c294fc
     return 0;
c294fc
 
c294fc
   a = p ? grub_strlen (p) : 0;
c294fc
 
c294fc
-  p = grub_realloc (p, round_up_exp ((a + slen + 1) * sizeof (char)));
c294fc
+  if (grub_add (a, slen, &sz) ||
c294fc
+      grub_add (sz, 1, &sz) ||
c294fc
+      grub_mul (sz, sizeof (char), &sz))
c294fc
+    return 1;
c294fc
+
c294fc
+  p = grub_realloc (p, round_up_exp (sz));
c294fc
   if (! p)
c294fc
     return 1;
c294fc
 
c294fc
diff --git a/grub-core/script/lexer.c b/grub-core/script/lexer.c
c294fc
index c6bd3172fab..5fb0cbd0bc9 100644
c294fc
--- a/grub-core/script/lexer.c
c294fc
+++ b/grub-core/script/lexer.c
c294fc
@@ -24,6 +24,7 @@
c294fc
 #include <grub/mm.h>
c294fc
 #include <grub/script_sh.h>
c294fc
 #include <grub/i18n.h>
c294fc
+#include <grub/safemath.h>
c294fc
 
c294fc
 #define yytext_ptr char *
c294fc
 #include "grub_script.tab.h"
c294fc
@@ -110,10 +111,14 @@ grub_script_lexer_record (struct grub_parser_param *parser, char *str)
c294fc
       old = lexer->recording;
c294fc
       if (lexer->recordlen < len)
c294fc
 	lexer->recordlen = len;
c294fc
-      lexer->recordlen *= 2;
c294fc
+
c294fc
+      if (grub_mul (lexer->recordlen, 2, &lexer->recordlen))
c294fc
+	goto fail;
c294fc
+
c294fc
       lexer->recording = grub_realloc (lexer->recording, lexer->recordlen);
c294fc
       if (!lexer->recording)
c294fc
 	{
c294fc
+ fail:
c294fc
 	  grub_free (old);
c294fc
 	  lexer->recordpos = 0;
c294fc
 	  lexer->recordlen = 0;
c294fc
@@ -130,7 +135,7 @@ int
c294fc
 grub_script_lexer_yywrap (struct grub_parser_param *parserstate,
c294fc
 			  const char *input)
c294fc
 {
c294fc
-  grub_size_t len = 0;
c294fc
+  grub_size_t len = 0, sz;
c294fc
   char *p = 0;
c294fc
   char *line = 0;
c294fc
   YY_BUFFER_STATE buffer;
c294fc
@@ -168,12 +173,22 @@ grub_script_lexer_yywrap (struct grub_parser_param *parserstate,
c294fc
     }
c294fc
   else if (len && line[len - 1] != '\n')
c294fc
     {
c294fc
-      p = grub_realloc (line, len + 2);
c294fc
+      if (grub_add (len, 2, &sz))
c294fc
+	{
c294fc
+	  grub_free (line);
c294fc
+	  grub_script_yyerror (parserstate, N_("overflow is detected"));
c294fc
+	  return 1;
c294fc
+	}
c294fc
+
c294fc
+      p = grub_realloc (line, sz);
c294fc
       if (p)
c294fc
 	{
c294fc
 	  p[len++] = '\n';
c294fc
 	  p[len] = '\0';
c294fc
 	}
c294fc
+      else
c294fc
+	grub_free (line);
c294fc
+
c294fc
       line = p;
c294fc
     }
c294fc
 
c294fc
diff --git a/grub-core/video/bitmap.c b/grub-core/video/bitmap.c
c294fc
index b2e0315665b..6256e209a6b 100644
c294fc
--- a/grub-core/video/bitmap.c
c294fc
+++ b/grub-core/video/bitmap.c
c294fc
@@ -23,6 +23,7 @@
c294fc
 #include <grub/mm.h>
c294fc
 #include <grub/misc.h>
c294fc
 #include <grub/i18n.h>
c294fc
+#include <grub/safemath.h>
c294fc
 
c294fc
 GRUB_MOD_LICENSE ("GPLv3+");
c294fc
 
c294fc
@@ -58,7 +59,7 @@ grub_video_bitmap_create (struct grub_video_bitmap **bitmap,
c294fc
                           enum grub_video_blit_format blit_format)
c294fc
 {
c294fc
   struct grub_video_mode_info *mode_info;
c294fc
-  unsigned int size;
c294fc
+  grub_size_t size;
c294fc
 
c294fc
   if (!bitmap)
c294fc
     return grub_error (GRUB_ERR_BUG, "invalid argument");
c294fc
@@ -137,19 +138,25 @@ grub_video_bitmap_create (struct grub_video_bitmap **bitmap,
c294fc
 
c294fc
   mode_info->pitch = width * mode_info->bytes_per_pixel;
c294fc
 
c294fc
-  /* Calculate size needed for the data.  */
c294fc
-  size = (width * mode_info->bytes_per_pixel) * height;
c294fc
+  /* Calculate size needed for the data. */
c294fc
+  if (grub_mul (width, mode_info->bytes_per_pixel, &size) ||
c294fc
+      grub_mul (size, height, &size))
c294fc
+    {
c294fc
+      grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
c294fc
+      goto fail;
c294fc
+    }
c294fc
 
c294fc
   (*bitmap)->data = grub_zalloc (size);
c294fc
   if (! (*bitmap)->data)
c294fc
-    {
c294fc
-      grub_free (*bitmap);
c294fc
-      *bitmap = 0;
c294fc
-
c294fc
-      return grub_errno;
c294fc
-    }
c294fc
+    goto fail;
c294fc
 
c294fc
   return GRUB_ERR_NONE;
c294fc
+
c294fc
+ fail:
c294fc
+  grub_free (*bitmap);
c294fc
+  *bitmap = NULL;
c294fc
+
c294fc
+  return grub_errno;
c294fc
 }
c294fc
 
c294fc
 /* Frees all resources allocated by bitmap.  */
c294fc
diff --git a/grub-core/video/readers/png.c b/grub-core/video/readers/png.c
c294fc
index e85df3c1b04..719e647e44f 100644
c294fc
--- a/grub-core/video/readers/png.c
c294fc
+++ b/grub-core/video/readers/png.c
c294fc
@@ -23,6 +23,7 @@
c294fc
 #include <grub/mm.h>
c294fc
 #include <grub/misc.h>
c294fc
 #include <grub/bufio.h>
c294fc
+#include <grub/safemath.h>
c294fc
 
c294fc
 GRUB_MOD_LICENSE ("GPLv3+");
c294fc
 
c294fc
@@ -301,9 +302,17 @@ grub_png_decode_image_header (struct grub_png_data *data)
c294fc
       data->bpp <<= 1;
c294fc
 
c294fc
   data->color_bits = color_bits;
c294fc
-  data->row_bytes = data->image_width * data->bpp;
c294fc
+
c294fc
+  if (grub_mul (data->image_width, data->bpp, &data->row_bytes))
c294fc
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
c294fc
+
c294fc
   if (data->color_bits <= 4)
c294fc
-    data->row_bytes = (data->image_width * data->color_bits + 7) / 8;
c294fc
+    {
c294fc
+      if (grub_mul (data->image_width, data->color_bits + 7, &data->row_bytes))
c294fc
+	return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
c294fc
+
c294fc
+      data->row_bytes >>= 3;
c294fc
+    }
c294fc
 
c294fc
 #ifndef GRUB_CPU_WORDS_BIGENDIAN
c294fc
   if (data->is_16bit || data->is_gray || data->is_palette)
c294fc
-- 
c294fc
2.26.2
c294fc