nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0570-font-Fix-an-integer-underflow-in-blit_comb.patch

530103
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
530103
From: Zhang Boyang <zhangboyang.id@gmail.com>
530103
Date: Mon, 24 Oct 2022 08:05:35 +0800
530103
Subject: [PATCH] font: Fix an integer underflow in blit_comb()
530103
530103
The expression (ctx.bounds.height - combining_glyphs[i]->height) / 2 may
530103
evaluate to a very big invalid value even if both ctx.bounds.height and
530103
combining_glyphs[i]->height are small integers. For example, if
530103
ctx.bounds.height is 10 and combining_glyphs[i]->height is 12, this
530103
expression evaluates to 2147483647 (expected -1). This is because
530103
coordinates are allowed to be negative but ctx.bounds.height is an
530103
unsigned int. So, the subtraction operates on unsigned ints and
530103
underflows to a very big value. The division makes things even worse.
530103
The quotient is still an invalid value even if converted back to int.
530103
530103
This patch fixes the problem by casting ctx.bounds.height to int. As
530103
a result the subtraction will operate on int and grub_uint16_t which
530103
will be promoted to an int. So, the underflow will no longer happen. Other
530103
uses of ctx.bounds.height (and ctx.bounds.width) are also casted to int,
530103
to ensure coordinates are always calculated on signed integers.
530103
530103
Fixes: CVE-2022-3775
530103
530103
Reported-by: Daniel Axtens <dja@axtens.net>
530103
Signed-off-by: Zhang Boyang <zhangboyang.id@gmail.com>
530103
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
530103
(cherry picked from commit 6d2668dea3774ed74c4cd1eadd146f1b846bc3d4)
530103
(cherry picked from commit 05e532fb707bbf79aa4e1efbde4d208d7da89d6b)
530103
(cherry picked from commit 0b2592fbb245d53c5c42885d695ece03ddb0eb12)
530103
---
530103
 grub-core/font/font.c | 16 ++++++++--------
530103
 1 file changed, 8 insertions(+), 8 deletions(-)
530103
530103
diff --git a/grub-core/font/font.c b/grub-core/font/font.c
530103
index 31786ab339..fc9d92fce4 100644
530103
--- a/grub-core/font/font.c
530103
+++ b/grub-core/font/font.c
530103
@@ -1203,12 +1203,12 @@ blit_comb (const struct grub_unicode_glyph *glyph_id,
530103
   ctx.bounds.height = main_glyph->height;
530103
 
530103
   above_rightx = main_glyph->offset_x + main_glyph->width;
530103
-  above_righty = ctx.bounds.y + ctx.bounds.height;
530103
+  above_righty = ctx.bounds.y + (int) ctx.bounds.height;
530103
 
530103
   above_leftx = main_glyph->offset_x;
530103
-  above_lefty = ctx.bounds.y + ctx.bounds.height;
530103
+  above_lefty = ctx.bounds.y + (int) ctx.bounds.height;
530103
 
530103
-  below_rightx = ctx.bounds.x + ctx.bounds.width;
530103
+  below_rightx = ctx.bounds.x + (int) ctx.bounds.width;
530103
   below_righty = ctx.bounds.y;
530103
 
530103
   comb = grub_unicode_get_comb (glyph_id);
530103
@@ -1221,7 +1221,7 @@ blit_comb (const struct grub_unicode_glyph *glyph_id,
530103
 
530103
       if (!combining_glyphs[i])
530103
 	continue;
530103
-      targetx = (ctx.bounds.width - combining_glyphs[i]->width) / 2 + ctx.bounds.x;
530103
+      targetx = ((int) ctx.bounds.width - combining_glyphs[i]->width) / 2 + ctx.bounds.x;
530103
       /* CGJ is to avoid diacritics reordering. */
530103
       if (comb[i].code
530103
 	  == GRUB_UNICODE_COMBINING_GRAPHEME_JOINER)
530103
@@ -1231,8 +1231,8 @@ blit_comb (const struct grub_unicode_glyph *glyph_id,
530103
 	case GRUB_UNICODE_COMB_OVERLAY:
530103
 	  do_blit (combining_glyphs[i],
530103
 		   targetx,
530103
-		   (ctx.bounds.height - combining_glyphs[i]->height) / 2
530103
-		   - (ctx.bounds.height + ctx.bounds.y), &ctx;;
530103
+		   ((int) ctx.bounds.height - combining_glyphs[i]->height) / 2
530103
+		   - ((int) ctx.bounds.height + ctx.bounds.y), &ctx;;
530103
 	  if (min_devwidth < combining_glyphs[i]->width)
530103
 	    min_devwidth = combining_glyphs[i]->width;
530103
 	  break;
530103
@@ -1305,7 +1305,7 @@ blit_comb (const struct grub_unicode_glyph *glyph_id,
530103
 	  /* Fallthrough.  */
530103
 	case GRUB_UNICODE_STACK_ATTACHED_ABOVE:
530103
 	  do_blit (combining_glyphs[i], targetx,
530103
-		   -(ctx.bounds.height + ctx.bounds.y + space
530103
+		   -((int) ctx.bounds.height + ctx.bounds.y + space
530103
 		     + combining_glyphs[i]->height), &ctx;;
530103
 	  if (min_devwidth < combining_glyphs[i]->width)
530103
 	    min_devwidth = combining_glyphs[i]->width;
530103
@@ -1313,7 +1313,7 @@ blit_comb (const struct grub_unicode_glyph *glyph_id,
530103
 
530103
 	case GRUB_UNICODE_COMB_HEBREW_DAGESH:
530103
 	  do_blit (combining_glyphs[i], targetx,
530103
-		   -(ctx.bounds.height / 2 + ctx.bounds.y
530103
+		   -((int) ctx.bounds.height / 2 + ctx.bounds.y
530103
 		     + combining_glyphs[i]->height / 2), &ctx;;
530103
 	  if (min_devwidth < combining_glyphs[i]->width)
530103
 	    min_devwidth = combining_glyphs[i]->width;