nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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