1From 829329bddb2c3e623270cc634cc9ab32e6455fe7 Mon Sep 17 00:00:00 2001
2From: Daniel Axtens <dja@axtens.net>
3Date: Fri, 15 Jan 2021 20:03:20 +1100
4Subject: [PATCH] term/gfxterm: Don't set up a font with glyphs that are too
5 big
6
7Catch the case where we have a font so big that it causes the number of
8rows or columns to be 0. Currently we continue and allocate a
9virtual_screen.text_buffer of size 0. We then try to use that for glpyhs
10and things go badly.
11
12On the emu platform, malloc() may give us a valid pointer, in which case
13we'll access heap memory which we shouldn't. Alternatively, it may give us
14NULL, in which case we'll crash. For other platforms, if I understand
15grub_memalign() correctly, we will receive a valid but small allocation
16that we will very likely later overrun.
17
18Prevent the creation of a virtual screen that isn't at least 40 cols
19by 12 rows. This is arbitrary, but it seems that if your width or height
20is half a standard 80x24 terminal, you're probably going to struggle to
21read anything anyway.
22
23Signed-off-by: Daniel Axtens <dja@axtens.net>
24Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
25Signed-off-by: Stefan Sørensen <stefan.sorensen@spectralink.com>
26---
27 grub-core/term/gfxterm.c | 9 +++++++++
28 1 file changed, 9 insertions(+)
29
30diff --git a/grub-core/term/gfxterm.c b/grub-core/term/gfxterm.c
31index af7c090..b40fcce 100644
32--- a/grub-core/term/gfxterm.c
33+++ b/grub-core/term/gfxterm.c
34@@ -232,6 +232,15 @@ grub_virtual_screen_setup (unsigned int x, unsigned int y,
35   virtual_screen.columns = virtual_screen.width / virtual_screen.normal_char_width;
36   virtual_screen.rows = virtual_screen.height / virtual_screen.normal_char_height;
37
38+  /*
39+   * There must be a minimum number of rows and columns for the screen to
40+   * make sense. Arbitrarily pick half of 80x24. If either dimensions is 0
41+   * we would allocate 0 bytes for the text_buffer.
42+   */
43+  if (virtual_screen.columns < 40 || virtual_screen.rows < 12)
44+    return grub_error (GRUB_ERR_BAD_FONT,
45+		       "font: glyphs too large to fit on screen");
46+
47   /* Allocate memory for text buffer.  */
48   virtual_screen.text_buffer =
49     (struct grub_colored_char *) grub_malloc (virtual_screen.columns
50--
512.14.2
52
53