0017-font-Fix-several-integer-overflows-in-grub_font_cons.patch 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. From fefba72d17364d6212cfd3be2232f4ce0ba23b82 Mon Sep 17 00:00:00 2001
  2. From: Zhang Boyang <zhangboyang.id@gmail.com>
  3. Date: Fri, 5 Aug 2022 01:58:27 +0800
  4. Subject: [PATCH] font: Fix several integer overflows in
  5. grub_font_construct_glyph()
  6. This patch fixes several integer overflows in grub_font_construct_glyph().
  7. Glyphs of invalid size, zero or leading to an overflow, are rejected.
  8. The inconsistency between "glyph" and "max_glyph_size" when grub_malloc()
  9. returns NULL is fixed too.
  10. Fixes: CVE-2022-2601
  11. Reported-by: Zhang Boyang <zhangboyang.id@gmail.com>
  12. Signed-off-by: Zhang Boyang <zhangboyang.id@gmail.com>
  13. Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
  14. Upstream: 768e1ef2fc159f6e14e7246e4be09363708ac39e
  15. Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
  16. ---
  17. grub-core/font/font.c | 29 +++++++++++++++++------------
  18. 1 file changed, 17 insertions(+), 12 deletions(-)
  19. diff --git a/grub-core/font/font.c b/grub-core/font/font.c
  20. index 876b5b695..0ff552578 100644
  21. --- a/grub-core/font/font.c
  22. +++ b/grub-core/font/font.c
  23. @@ -1515,6 +1515,7 @@ grub_font_construct_glyph (grub_font_t hinted_font,
  24. struct grub_video_signed_rect bounds;
  25. static struct grub_font_glyph *glyph = 0;
  26. static grub_size_t max_glyph_size = 0;
  27. + grub_size_t cur_glyph_size;
  28. ensure_comb_space (glyph_id);
  29. @@ -1531,29 +1532,33 @@ grub_font_construct_glyph (grub_font_t hinted_font,
  30. if (!glyph_id->ncomb && !glyph_id->attributes)
  31. return main_glyph;
  32. - if (max_glyph_size < sizeof (*glyph) + (bounds.width * bounds.height + GRUB_CHAR_BIT - 1) / GRUB_CHAR_BIT)
  33. + if (grub_video_bitmap_calc_1bpp_bufsz (bounds.width, bounds.height, &cur_glyph_size) ||
  34. + grub_add (sizeof (*glyph), cur_glyph_size, &cur_glyph_size))
  35. + return main_glyph;
  36. +
  37. + if (max_glyph_size < cur_glyph_size)
  38. {
  39. grub_free (glyph);
  40. - max_glyph_size = (sizeof (*glyph) + (bounds.width * bounds.height + GRUB_CHAR_BIT - 1) / GRUB_CHAR_BIT) * 2;
  41. - if (max_glyph_size < 8)
  42. - max_glyph_size = 8;
  43. - glyph = grub_malloc (max_glyph_size);
  44. + if (grub_mul (cur_glyph_size, 2, &max_glyph_size))
  45. + max_glyph_size = 0;
  46. + glyph = max_glyph_size > 0 ? grub_malloc (max_glyph_size) : NULL;
  47. }
  48. if (!glyph)
  49. {
  50. + max_glyph_size = 0;
  51. grub_errno = GRUB_ERR_NONE;
  52. return main_glyph;
  53. }
  54. - grub_memset (glyph, 0, sizeof (*glyph)
  55. - + (bounds.width * bounds.height
  56. - + GRUB_CHAR_BIT - 1) / GRUB_CHAR_BIT);
  57. + grub_memset (glyph, 0, cur_glyph_size);
  58. glyph->font = main_glyph->font;
  59. - glyph->width = bounds.width;
  60. - glyph->height = bounds.height;
  61. - glyph->offset_x = bounds.x;
  62. - glyph->offset_y = bounds.y;
  63. + if (bounds.width == 0 || bounds.height == 0 ||
  64. + grub_cast (bounds.width, &glyph->width) ||
  65. + grub_cast (bounds.height, &glyph->height) ||
  66. + grub_cast (bounds.x, &glyph->offset_x) ||
  67. + grub_cast (bounds.y, &glyph->offset_y))
  68. + return main_glyph;
  69. if (glyph_id->attributes & GRUB_UNICODE_GLYPH_ATTRIBUTE_MIRROR)
  70. grub_font_blit_glyph_mirror (glyph, main_glyph,
  71. --
  72. 2.41.0