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

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