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