Blame SOURCES/0563-font-Fix-size-overflow-in-grub_font_get_glyph_intern.patch

a9bbe0
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
a9bbe0
From: Zhang Boyang <zhangboyang.id@gmail.com>
a9bbe0
Date: Fri, 5 Aug 2022 00:51:20 +0800
a9bbe0
Subject: [PATCH] font: Fix size overflow in grub_font_get_glyph_internal()
a9bbe0
a9bbe0
The length of memory allocation and file read may overflow. This patch
a9bbe0
fixes the problem by using safemath macros.
a9bbe0
a9bbe0
There is a lot of code repetition like "(x * y + 7) / 8". It is unsafe
a9bbe0
if overflow happens. This patch introduces grub_video_bitmap_calc_1bpp_bufsz().
a9bbe0
It is safe replacement for such code. It has safemath-like prototype.
a9bbe0
a9bbe0
This patch also introduces grub_cast(value, pointer), it casts value to
a9bbe0
typeof(*pointer) then store the value to *pointer. It returns true when
a9bbe0
overflow occurs or false if there is no overflow. The semantics of arguments
a9bbe0
and return value are designed to be consistent with other safemath macros.
a9bbe0
a9bbe0
Signed-off-by: Zhang Boyang <zhangboyang.id@gmail.com>
a9bbe0
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
a9bbe0
(cherry picked from commit 941d10ad6f1dcbd12fb613002249e29ba035f985)
a9bbe0
(cherry picked from commit 6bca9693878bdf61dd62b8c784862a48e75f569a)
a9bbe0
(cherry picked from commit edbbda5486cf8c3dc2b68fbd3dead822ab448022)
a9bbe0
---
a9bbe0
 grub-core/font/font.c   | 17 +++++++++++++----
a9bbe0
 include/grub/bitmap.h   | 18 ++++++++++++++++++
a9bbe0
 include/grub/safemath.h |  2 ++
a9bbe0
 3 files changed, 33 insertions(+), 4 deletions(-)
a9bbe0
a9bbe0
diff --git a/grub-core/font/font.c b/grub-core/font/font.c
a9bbe0
index 8d1a990401..d6df79602d 100644
a9bbe0
--- a/grub-core/font/font.c
a9bbe0
+++ b/grub-core/font/font.c
a9bbe0
@@ -739,7 +739,8 @@ grub_font_get_glyph_internal (grub_font_t font, grub_uint32_t code)
a9bbe0
       grub_int16_t xoff;
a9bbe0
       grub_int16_t yoff;
a9bbe0
       grub_int16_t dwidth;
a9bbe0
-      int len;
a9bbe0
+      grub_ssize_t len;
a9bbe0
+      grub_size_t sz;
a9bbe0
 
a9bbe0
       if (index_entry->glyph)
a9bbe0
 	/* Return cached glyph.  */
a9bbe0
@@ -768,9 +769,17 @@ grub_font_get_glyph_internal (grub_font_t font, grub_uint32_t code)
a9bbe0
 	  return 0;
a9bbe0
 	}
a9bbe0
 
a9bbe0
-      len = (width * height + 7) / 8;
a9bbe0
-      glyph = grub_malloc (sizeof (struct grub_font_glyph) + len);
a9bbe0
-      if (!glyph)
a9bbe0
+      /* Calculate real struct size of current glyph. */
a9bbe0
+      if (grub_video_bitmap_calc_1bpp_bufsz (width, height, &len) ||
a9bbe0
+	  grub_add (sizeof (struct grub_font_glyph), len, &sz))
a9bbe0
+	{
a9bbe0
+	  remove_font (font);
a9bbe0
+	  return 0;
a9bbe0
+	}
a9bbe0
+
a9bbe0
+      /* Allocate and initialize the glyph struct. */
a9bbe0
+      glyph = grub_malloc (sz);
a9bbe0
+      if (glyph == NULL)
a9bbe0
 	{
a9bbe0
 	  remove_font (font);
a9bbe0
 	  return 0;
a9bbe0
diff --git a/include/grub/bitmap.h b/include/grub/bitmap.h
a9bbe0
index 5728f8ca3a..0d9603f619 100644
a9bbe0
--- a/include/grub/bitmap.h
a9bbe0
+++ b/include/grub/bitmap.h
a9bbe0
@@ -23,6 +23,7 @@
a9bbe0
 #include <grub/symbol.h>
a9bbe0
 #include <grub/types.h>
a9bbe0
 #include <grub/video.h>
a9bbe0
+#include <grub/safemath.h>
a9bbe0
 
a9bbe0
 struct grub_video_bitmap
a9bbe0
 {
a9bbe0
@@ -79,6 +80,23 @@ grub_video_bitmap_get_height (struct grub_video_bitmap *bitmap)
a9bbe0
   return bitmap->mode_info.height;
a9bbe0
 }
a9bbe0
 
a9bbe0
+/*
a9bbe0
+ * Calculate and store the size of data buffer of 1bit bitmap in result.
a9bbe0
+ * Equivalent to "*result = (width * height + 7) / 8" if no overflow occurs.
a9bbe0
+ * Return true when overflow occurs or false if there is no overflow.
a9bbe0
+ * This function is intentionally implemented as a macro instead of
a9bbe0
+ * an inline function. Although a bit awkward, it preserves data types for
a9bbe0
+ * safemath macros and reduces macro side effects as much as possible.
a9bbe0
+ *
a9bbe0
+ * XXX: Will report false overflow if width * height > UINT64_MAX.
a9bbe0
+ */
a9bbe0
+#define grub_video_bitmap_calc_1bpp_bufsz(width, height, result) \
a9bbe0
+({ \
a9bbe0
+  grub_uint64_t _bitmap_pixels; \
a9bbe0
+  grub_mul ((width), (height), &_bitmap_pixels) ? 1 : \
a9bbe0
+    grub_cast (_bitmap_pixels / GRUB_CHAR_BIT + !!(_bitmap_pixels % GRUB_CHAR_BIT), (result)); \
a9bbe0
+})
a9bbe0
+
a9bbe0
 void EXPORT_FUNC (grub_video_bitmap_get_mode_info) (struct grub_video_bitmap *bitmap,
a9bbe0
 						    struct grub_video_mode_info *mode_info);
a9bbe0
 
a9bbe0
diff --git a/include/grub/safemath.h b/include/grub/safemath.h
a9bbe0
index 1ccac276b5..30800ad6a1 100644
a9bbe0
--- a/include/grub/safemath.h
a9bbe0
+++ b/include/grub/safemath.h
a9bbe0
@@ -30,6 +30,8 @@
a9bbe0
 #define grub_sub(a, b, res)	__builtin_sub_overflow(a, b, res)
a9bbe0
 #define grub_mul(a, b, res)	__builtin_mul_overflow(a, b, res)
a9bbe0
 
a9bbe0
+#define grub_cast(a, res)	grub_add ((a), 0, (res))
a9bbe0
+
a9bbe0
 #else
a9bbe0
 /*
a9bbe0
  * Copyright 2020 Rasmus Villemoes