nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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