ed1787d
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
ed1787d
From: Zhang Boyang <zhangboyang.id@gmail.com>
ed1787d
Date: Fri, 28 Oct 2022 21:31:39 +0800
ed1787d
Subject: [PATCH] normal/charset: Fix an integer overflow in
ed1787d
 grub_unicode_aglomerate_comb()
ed1787d
ed1787d
The out->ncomb is a bit-field of 8 bits. So, the max possible value is 255.
ed1787d
However, code in grub_unicode_aglomerate_comb() doesn't check for an
ed1787d
overflow when incrementing out->ncomb. If out->ncomb is already 255,
ed1787d
after incrementing it will get 0 instead of 256, and cause illegal
ed1787d
memory access in subsequent processing.
ed1787d
ed1787d
This patch introduces GRUB_UNICODE_NCOMB_MAX to represent the max
ed1787d
acceptable value of ncomb. The code now checks for this limit and
ed1787d
ignores additional combining characters when limit is reached.
ed1787d
ed1787d
Reported-by: Daniel Axtens <dja@axtens.net>
ed1787d
Signed-off-by: Zhang Boyang <zhangboyang.id@gmail.com>
ed1787d
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
ed1787d
(cherry picked from commit da90d62316a3b105d2fbd7334d6521936bd6dcf6)
ed1787d
---
ed1787d
 grub-core/normal/charset.c | 3 +++
ed1787d
 include/grub/unicode.h     | 2 ++
ed1787d
 2 files changed, 5 insertions(+)
ed1787d
ed1787d
diff --git a/grub-core/normal/charset.c b/grub-core/normal/charset.c
ed1787d
index 7a5a7c153c..c243ca6dae 100644
ed1787d
--- a/grub-core/normal/charset.c
ed1787d
+++ b/grub-core/normal/charset.c
ed1787d
@@ -472,6 +472,9 @@ grub_unicode_aglomerate_comb (const grub_uint32_t *in, grub_size_t inlen,
ed1787d
 	  if (!haveout)
ed1787d
 	    continue;
ed1787d
 
ed1787d
+	  if (out->ncomb == GRUB_UNICODE_NCOMB_MAX)
ed1787d
+	    continue;
ed1787d
+
ed1787d
 	  if (comb_type == GRUB_UNICODE_COMB_MC
ed1787d
 	      || comb_type == GRUB_UNICODE_COMB_ME
ed1787d
 	      || comb_type == GRUB_UNICODE_COMB_MN)
ed1787d
diff --git a/include/grub/unicode.h b/include/grub/unicode.h
ed1787d
index 4de986a857..c4f6fca043 100644
ed1787d
--- a/include/grub/unicode.h
ed1787d
+++ b/include/grub/unicode.h
ed1787d
@@ -147,7 +147,9 @@ struct grub_unicode_glyph
ed1787d
   grub_uint8_t bidi_level:6; /* minimum: 6 */
ed1787d
   enum grub_bidi_type bidi_type:5; /* minimum: :5 */
ed1787d
 
ed1787d
+#define GRUB_UNICODE_NCOMB_MAX ((1 << 8) - 1)
ed1787d
   unsigned ncomb:8;
ed1787d
+
ed1787d
   /* Hint by unicode subsystem how wide this character usually is.
ed1787d
      Real width is determined by font. Set only in UTF-8 stream.  */
ed1787d
   int estimated_width:8;