nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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