nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0281-font-Fix-several-integer-overflows-in-grub_font_cons.patch

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