9723a8
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
c294fc
From: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
c294fc
Date: Tue, 7 Jul 2020 15:12:25 -0400
9723a8
Subject: [PATCH] term: Fix overflow on user inputs
c294fc
c294fc
This requires a very weird input from the serial interface but can cause
c294fc
an overflow in input_buf (keys) overwriting the next variable (npending)
c294fc
with the user choice:
c294fc
c294fc
(pahole output)
c294fc
c294fc
struct grub_terminfo_input_state {
c294fc
        int                        input_buf[6];         /*     0    24 */
c294fc
        int                        npending;             /*    24     4 */ <- CORRUPT
c294fc
        ...snip...
c294fc
c294fc
The magic string requires causing this is "ESC,O,],0,1,2,q" and we overflow
c294fc
npending with "q" (aka increase npending to 161). The simplest fix is to
c294fc
just to disallow overwrites input_buf, which exactly what this patch does.
c294fc
c294fc
Fixes: CID 292449
c294fc
c294fc
Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
c294fc
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
c294fc
Upstream-commit-id: 98dfa546777
c294fc
---
c294fc
 grub-core/term/terminfo.c | 9 ++++++---
c294fc
 1 file changed, 6 insertions(+), 3 deletions(-)
c294fc
c294fc
diff --git a/grub-core/term/terminfo.c b/grub-core/term/terminfo.c
b71686
index 537a5c0cb..44d0b3b19 100644
c294fc
--- a/grub-core/term/terminfo.c
c294fc
+++ b/grub-core/term/terminfo.c
c294fc
@@ -398,7 +398,7 @@ grub_terminfo_getwh (struct grub_term_output *term)
c294fc
 }
c294fc
 
c294fc
 static void
c294fc
-grub_terminfo_readkey (struct grub_term_input *term, int *keys, int *len,
c294fc
+grub_terminfo_readkey (struct grub_term_input *term, int *keys, int *len, int max_len,
c294fc
 		       int (*readkey) (struct grub_term_input *term))
c294fc
 {
c294fc
   int c;
c294fc
@@ -414,6 +414,9 @@ grub_terminfo_readkey (struct grub_term_input *term, int *keys, int *len,
c294fc
     if (c == -1)						\
c294fc
       return;							\
c294fc
 								\
c294fc
+    if (*len >= max_len)                                       \
c294fc
+      return;                                                   \
c294fc
+                                                                \
c294fc
     keys[*len] = c;						\
c294fc
     (*len)++;							\
c294fc
   }
c294fc
@@ -602,8 +605,8 @@ grub_terminfo_getkey (struct grub_term_input *termi)
c294fc
       return ret;
c294fc
     }
c294fc
 
c294fc
-  grub_terminfo_readkey (termi, data->input_buf,
c294fc
-			 &data->npending, data->readkey);
c294fc
+  grub_terminfo_readkey (termi, data->input_buf, &data->npending,
c294fc
+			 GRUB_TERMINFO_READKEY_MAX_LEN, data->readkey);
c294fc
 
c294fc
 #if defined(__powerpc__) && defined(GRUB_MACHINE_IEEE1275)
c294fc
   if (data->npending == 1 && data->input_buf[0] == GRUB_TERM_ESC