ed1787d
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
ed1787d
From: Zhang Boyang <zhangboyang.id@gmail.com>
ed1787d
Date: Fri, 5 Aug 2022 01:58:27 +0800
ed1787d
Subject: [PATCH] font: Fix several integer overflows in
ed1787d
 grub_font_construct_glyph()
ed1787d
ed1787d
This patch fixes several integer overflows in grub_font_construct_glyph().
ed1787d
Glyphs of invalid size, zero or leading to an overflow, are rejected.
ed1787d
The inconsistency between "glyph" and "max_glyph_size" when grub_malloc()
ed1787d
returns NULL is fixed too.
ed1787d
ed1787d
Fixes: CVE-2022-2601
ed1787d
ed1787d
Reported-by: Zhang Boyang <zhangboyang.id@gmail.com>
ed1787d
Signed-off-by: Zhang Boyang <zhangboyang.id@gmail.com>
ed1787d
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
ed1787d
(cherry picked from commit b1805f251b31a9d3cfae5c3572ddfa630145dbbf)
ed1787d
---
ed1787d
 grub-core/font/font.c | 29 +++++++++++++++++------------
ed1787d
 1 file changed, 17 insertions(+), 12 deletions(-)
ed1787d
ed1787d
diff --git a/grub-core/font/font.c b/grub-core/font/font.c
ed1787d
index 6a3fbebbd8..1fa181d4ca 100644
ed1787d
--- a/grub-core/font/font.c
ed1787d
+++ b/grub-core/font/font.c
ed1787d
@@ -1517,6 +1517,7 @@ grub_font_construct_glyph (grub_font_t hinted_font,
ed1787d
   struct grub_video_signed_rect bounds;
ed1787d
   static struct grub_font_glyph *glyph = 0;
ed1787d
   static grub_size_t max_glyph_size = 0;
ed1787d
+  grub_size_t cur_glyph_size;
ed1787d
 
ed1787d
   ensure_comb_space (glyph_id);
ed1787d
 
ed1787d
@@ -1533,29 +1534,33 @@ grub_font_construct_glyph (grub_font_t hinted_font,
ed1787d
   if (!glyph_id->ncomb && !glyph_id->attributes)
ed1787d
     return main_glyph;
ed1787d
 
ed1787d
-  if (max_glyph_size < sizeof (*glyph) + (bounds.width * bounds.height + GRUB_CHAR_BIT - 1) / GRUB_CHAR_BIT)
ed1787d
+  if (grub_video_bitmap_calc_1bpp_bufsz (bounds.width, bounds.height, &cur_glyph_size) ||
ed1787d
+      grub_add (sizeof (*glyph), cur_glyph_size, &cur_glyph_size))
ed1787d
+    return main_glyph;
ed1787d
+
ed1787d
+  if (max_glyph_size < cur_glyph_size)
ed1787d
     {
ed1787d
       grub_free (glyph);
ed1787d
-      max_glyph_size = (sizeof (*glyph) + (bounds.width * bounds.height + GRUB_CHAR_BIT - 1) / GRUB_CHAR_BIT) * 2;
ed1787d
-      if (max_glyph_size < 8)
ed1787d
-	max_glyph_size = 8;
ed1787d
-      glyph = grub_malloc (max_glyph_size);
ed1787d
+      if (grub_mul (cur_glyph_size, 2, &max_glyph_size))
ed1787d
+	max_glyph_size = 0;
ed1787d
+      glyph = max_glyph_size > 0 ? grub_malloc (max_glyph_size) : NULL;
ed1787d
     }
ed1787d
   if (!glyph)
ed1787d
     {
ed1787d
+      max_glyph_size = 0;
ed1787d
       grub_errno = GRUB_ERR_NONE;
ed1787d
       return main_glyph;
ed1787d
     }
ed1787d
 
ed1787d
-  grub_memset (glyph, 0, sizeof (*glyph)
ed1787d
-	       + (bounds.width * bounds.height
ed1787d
-		  + GRUB_CHAR_BIT - 1) / GRUB_CHAR_BIT);
ed1787d
+  grub_memset (glyph, 0, cur_glyph_size);
ed1787d
 
ed1787d
   glyph->font = main_glyph->font;
ed1787d
-  glyph->width = bounds.width;
ed1787d
-  glyph->height = bounds.height;
ed1787d
-  glyph->offset_x = bounds.x;
ed1787d
-  glyph->offset_y = bounds.y;
ed1787d
+  if (bounds.width == 0 || bounds.height == 0 ||
ed1787d
+      grub_cast (bounds.width, &glyph->width) ||
ed1787d
+      grub_cast (bounds.height, &glyph->height) ||
ed1787d
+      grub_cast (bounds.x, &glyph->offset_x) ||
ed1787d
+      grub_cast (bounds.y, &glyph->offset_y))
ed1787d
+    return main_glyph;
ed1787d
 
ed1787d
   if (glyph_id->attributes & GRUB_UNICODE_GLYPH_ATTRIBUTE_MIRROR)
ed1787d
     grub_font_blit_glyph_mirror (glyph, main_glyph,