0018-font-Fix-an-integer-underflow-in-blit_comb.patch 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. From 79bd19e078c5053d800b1b4d3a901083da947e70 Mon Sep 17 00:00:00 2001
  2. From: Zhang Boyang <zhangboyang.id@gmail.com>
  3. Date: Mon, 24 Oct 2022 08:05:35 +0800
  4. Subject: [PATCH] font: Fix an integer underflow in blit_comb()
  5. The expression (ctx.bounds.height - combining_glyphs[i]->height) / 2 may
  6. evaluate to a very big invalid value even if both ctx.bounds.height and
  7. combining_glyphs[i]->height are small integers. For example, if
  8. ctx.bounds.height is 10 and combining_glyphs[i]->height is 12, this
  9. expression evaluates to 2147483647 (expected -1). This is because
  10. coordinates are allowed to be negative but ctx.bounds.height is an
  11. unsigned int. So, the subtraction operates on unsigned ints and
  12. underflows to a very big value. The division makes things even worse.
  13. The quotient is still an invalid value even if converted back to int.
  14. This patch fixes the problem by casting ctx.bounds.height to int. As
  15. a result the subtraction will operate on int and grub_uint16_t which
  16. will be promoted to an int. So, the underflow will no longer happen. Other
  17. uses of ctx.bounds.height (and ctx.bounds.width) are also casted to int,
  18. to ensure coordinates are always calculated on signed integers.
  19. Fixes: CVE-2022-3775
  20. Reported-by: Daniel Axtens <dja@axtens.net>
  21. Signed-off-by: Zhang Boyang <zhangboyang.id@gmail.com>
  22. Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
  23. Upstream: 992c06191babc1e109caf40d6a07ec6fdef427af
  24. Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
  25. ---
  26. grub-core/font/font.c | 16 ++++++++--------
  27. 1 file changed, 8 insertions(+), 8 deletions(-)
  28. diff --git a/grub-core/font/font.c b/grub-core/font/font.c
  29. index 0ff552578..7b1cbde07 100644
  30. --- a/grub-core/font/font.c
  31. +++ b/grub-core/font/font.c
  32. @@ -1206,12 +1206,12 @@ blit_comb (const struct grub_unicode_glyph *glyph_id,
  33. ctx.bounds.height = main_glyph->height;
  34. above_rightx = main_glyph->offset_x + main_glyph->width;
  35. - above_righty = ctx.bounds.y + ctx.bounds.height;
  36. + above_righty = ctx.bounds.y + (int) ctx.bounds.height;
  37. above_leftx = main_glyph->offset_x;
  38. - above_lefty = ctx.bounds.y + ctx.bounds.height;
  39. + above_lefty = ctx.bounds.y + (int) ctx.bounds.height;
  40. - below_rightx = ctx.bounds.x + ctx.bounds.width;
  41. + below_rightx = ctx.bounds.x + (int) ctx.bounds.width;
  42. below_righty = ctx.bounds.y;
  43. comb = grub_unicode_get_comb (glyph_id);
  44. @@ -1224,7 +1224,7 @@ blit_comb (const struct grub_unicode_glyph *glyph_id,
  45. if (!combining_glyphs[i])
  46. continue;
  47. - targetx = (ctx.bounds.width - combining_glyphs[i]->width) / 2 + ctx.bounds.x;
  48. + targetx = ((int) ctx.bounds.width - combining_glyphs[i]->width) / 2 + ctx.bounds.x;
  49. /* CGJ is to avoid diacritics reordering. */
  50. if (comb[i].code
  51. == GRUB_UNICODE_COMBINING_GRAPHEME_JOINER)
  52. @@ -1234,8 +1234,8 @@ blit_comb (const struct grub_unicode_glyph *glyph_id,
  53. case GRUB_UNICODE_COMB_OVERLAY:
  54. do_blit (combining_glyphs[i],
  55. targetx,
  56. - (ctx.bounds.height - combining_glyphs[i]->height) / 2
  57. - - (ctx.bounds.height + ctx.bounds.y), &ctx);
  58. + ((int) ctx.bounds.height - combining_glyphs[i]->height) / 2
  59. + - ((int) ctx.bounds.height + ctx.bounds.y), &ctx);
  60. if (min_devwidth < combining_glyphs[i]->width)
  61. min_devwidth = combining_glyphs[i]->width;
  62. break;
  63. @@ -1308,7 +1308,7 @@ blit_comb (const struct grub_unicode_glyph *glyph_id,
  64. /* Fallthrough. */
  65. case GRUB_UNICODE_STACK_ATTACHED_ABOVE:
  66. do_blit (combining_glyphs[i], targetx,
  67. - -(ctx.bounds.height + ctx.bounds.y + space
  68. + -((int) ctx.bounds.height + ctx.bounds.y + space
  69. + combining_glyphs[i]->height), &ctx);
  70. if (min_devwidth < combining_glyphs[i]->width)
  71. min_devwidth = combining_glyphs[i]->width;
  72. @@ -1316,7 +1316,7 @@ blit_comb (const struct grub_unicode_glyph *glyph_id,
  73. case GRUB_UNICODE_COMB_HEBREW_DAGESH:
  74. do_blit (combining_glyphs[i], targetx,
  75. - -(ctx.bounds.height / 2 + ctx.bounds.y
  76. + -((int) ctx.bounds.height / 2 + ctx.bounds.y
  77. + combining_glyphs[i]->height / 2), &ctx);
  78. if (min_devwidth < combining_glyphs[i]->width)
  79. min_devwidth = combining_glyphs[i]->width;
  80. --
  81. 2.41.0