nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0465-kern-parser-Fix-a-stack-buffer-overflow.patch

9723a8
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
9723a8
From: Chris Coulson <chris.coulson@canonical.com>
9723a8
Date: Thu, 7 Jan 2021 19:21:03 +0000
9723a8
Subject: [PATCH] kern/parser: Fix a stack buffer overflow
9723a8
9723a8
grub_parser_split_cmdline() expands variable names present in the supplied
9723a8
command line in to their corresponding variable contents and uses a 1 kiB
9723a8
stack buffer for temporary storage without sufficient bounds checking. If
9723a8
the function is called with a command line that references a variable with
9723a8
a sufficiently large payload, it is possible to overflow the stack
9723a8
buffer via tab completion, corrupt the stack frame and potentially
9723a8
control execution.
9723a8
9723a8
Fixes: CVE-2020-27749
9723a8
9723a8
Reported-by: Chris Coulson <chris.coulson@canonical.com>
9723a8
Signed-off-by: Chris Coulson <chris.coulson@canonical.com>
9723a8
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
9723a8
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
9723a8
---
9723a8
 grub-core/kern/parser.c | 110 +++++++++++++++++++++++++++++-------------------
9723a8
 1 file changed, 67 insertions(+), 43 deletions(-)
9723a8
9723a8
diff --git a/grub-core/kern/parser.c b/grub-core/kern/parser.c
b71686
index e010eaa1f..6ab7aa427 100644
9723a8
--- a/grub-core/kern/parser.c
9723a8
+++ b/grub-core/kern/parser.c
9723a8
@@ -18,6 +18,7 @@
9723a8
  */
9723a8
 
9723a8
 #include <grub/parser.h>
9723a8
+#include <grub/buffer.h>
9723a8
 #include <grub/env.h>
9723a8
 #include <grub/misc.h>
9723a8
 #include <grub/mm.h>
9723a8
@@ -107,8 +108,8 @@ check_varstate (grub_parser_state_t s)
9723a8
 }
9723a8
 
9723a8
 
9723a8
-static void
9723a8
-add_var (char *varname, char **bp, char **vp,
9723a8
+static grub_err_t
9723a8
+add_var (grub_buffer_t varname, grub_buffer_t buf,
9723a8
 	 grub_parser_state_t state, grub_parser_state_t newstate)
9723a8
 {
9723a8
   const char *val;
9723a8
@@ -116,31 +117,41 @@ add_var (char *varname, char **bp, char **vp,
9723a8
   /* Check if a variable was being read in and the end of the name
9723a8
      was reached.  */
9723a8
   if (!(check_varstate (state) && !check_varstate (newstate)))
9723a8
-    return;
9723a8
+    return GRUB_ERR_NONE;
9723a8
 
9723a8
-  *((*vp)++) = '\0';
9723a8
-  val = grub_env_get (varname);
9723a8
-  *vp = varname;
9723a8
+  if (grub_buffer_append_char (varname, '\0') != GRUB_ERR_NONE)
9723a8
+    return grub_errno;
9723a8
+
9723a8
+  val = grub_env_get ((const char *) grub_buffer_peek_data (varname));
9723a8
+  grub_buffer_reset (varname);
9723a8
   if (!val)
9723a8
-    return;
9723a8
+    return GRUB_ERR_NONE;
9723a8
 
9723a8
   /* Insert the contents of the variable in the buffer.  */
9723a8
-  for (; *val; val++)
9723a8
-    *((*bp)++) = *val;
9723a8
+  return grub_buffer_append_data (buf, val, grub_strlen (val));
9723a8
 }
9723a8
 
9723a8
-static void
9723a8
-terminate_arg (char *buffer, char **bp, int *argc)
9723a8
+static grub_err_t
9723a8
+terminate_arg (grub_buffer_t buffer, int *argc)
9723a8
 {
9723a8
-  if (*bp != buffer && *((*bp) - 1) != '\0')
9723a8
-    {
9723a8
-      *((*bp)++) = '\0';
9723a8
-      (*argc)++;
9723a8
-    }
9723a8
+  grub_size_t unread = grub_buffer_get_unread_bytes (buffer);
9723a8
+
9723a8
+  if (unread == 0)
9723a8
+    return GRUB_ERR_NONE;
9723a8
+
9723a8
+  if (*(const char *) grub_buffer_peek_data_at (buffer, unread - 1) == '\0')
9723a8
+    return GRUB_ERR_NONE;
9723a8
+
9723a8
+  if (grub_buffer_append_char (buffer, '\0') != GRUB_ERR_NONE)
9723a8
+    return grub_errno;
9723a8
+
9723a8
+  (*argc)++;
9723a8
+
9723a8
+  return GRUB_ERR_NONE;
9723a8
 }
9723a8
 
9723a8
 static grub_err_t
9723a8
-process_char (char c, char *buffer, char **bp, char *varname, char **vp,
9723a8
+process_char (char c, grub_buffer_t buffer, grub_buffer_t varname,
9723a8
 	      grub_parser_state_t state, int *argc,
9723a8
 	      grub_parser_state_t *newstate)
9723a8
 {
9723a8
@@ -153,12 +164,13 @@ process_char (char c, char *buffer, char **bp, char *varname, char **vp,
9723a8
    * not describe the variable anymore, write the variable to
9723a8
    * the buffer.
9723a8
    */
9723a8
-  add_var (varname, bp, vp, state, *newstate);
9723a8
+  if (add_var (varname, buffer, state, *newstate) != GRUB_ERR_NONE)
9723a8
+    return grub_errno;
9723a8
 
9723a8
   if (check_varstate (*newstate))
9723a8
     {
9723a8
       if (use)
9723a8
-	*((*vp)++) = use;
9723a8
+        return grub_buffer_append_char (varname, use);
9723a8
     }
9723a8
   else if (*newstate == GRUB_PARSER_STATE_TEXT &&
9723a8
 	   state != GRUB_PARSER_STATE_ESC && grub_isspace (use))
9723a8
@@ -167,10 +179,10 @@ process_char (char c, char *buffer, char **bp, char *varname, char **vp,
9723a8
        * Don't add more than one argument if multiple
9723a8
        * spaces are used.
9723a8
        */
9723a8
-      terminate_arg (buffer, bp, argc);
9723a8
+      return terminate_arg (buffer, argc);
9723a8
     }
9723a8
   else if (use)
9723a8
-    *((*bp)++) = use;
9723a8
+    return grub_buffer_append_char (buffer, use);
9723a8
 
9723a8
   return GRUB_ERR_NONE;
9723a8
 }
9723a8
@@ -181,19 +193,22 @@ grub_parser_split_cmdline (const char *cmdline,
9723a8
 			   int *argc, char ***argv)
9723a8
 {
9723a8
   grub_parser_state_t state = GRUB_PARSER_STATE_TEXT;
9723a8
-  /* XXX: Fixed size buffer, perhaps this buffer should be dynamically
9723a8
-     allocated.  */
9723a8
-  char buffer[1024];
9723a8
-  char *bp = buffer;
9723a8
+  grub_buffer_t buffer, varname;
9723a8
   char *rd = (char *) cmdline;
9723a8
   char *rp = rd;
9723a8
-  char varname[200];
9723a8
-  char *vp = varname;
9723a8
-  char *args;
9723a8
   int i;
9723a8
 
9723a8
   *argc = 0;
9723a8
   *argv = NULL;
9723a8
+
9723a8
+  buffer = grub_buffer_new (1024);
9723a8
+  if (buffer == NULL)
9723a8
+    return grub_errno;
9723a8
+
9723a8
+  varname = grub_buffer_new (200);
9723a8
+  if (varname == NULL)
9723a8
+    goto fail;
9723a8
+
9723a8
   do
9723a8
     {
9723a8
       if (rp == NULL || *rp == '\0')
9723a8
@@ -219,7 +234,7 @@ grub_parser_split_cmdline (const char *cmdline,
9723a8
 	{
9723a8
 	  grub_parser_state_t newstate;
9723a8
 
9723a8
-	  if (process_char (*rp, buffer, &bp, varname, &vp, state, argc,
9723a8
+	  if (process_char (*rp, buffer, varname, state, argc,
9723a8
 			    &newstate) != GRUB_ERR_NONE)
9723a8
 	    goto fail;
9723a8
 
9723a8
@@ -230,10 +245,12 @@ grub_parser_split_cmdline (const char *cmdline,
9723a8
 
9723a8
   /* A special case for when the last character was part of a
9723a8
      variable.  */
9723a8
-  add_var (varname, &bp, &vp, state, GRUB_PARSER_STATE_TEXT);
9723a8
+  if (add_var (varname, buffer, state, GRUB_PARSER_STATE_TEXT) != GRUB_ERR_NONE)
9723a8
+    goto fail;
9723a8
 
9723a8
   /* Ensure that the last argument is terminated. */
9723a8
-  terminate_arg (buffer, &bp, argc);
9723a8
+  if (terminate_arg (buffer, argc) != GRUB_ERR_NONE)
9723a8
+    goto fail;
9723a8
 
9723a8
   /* If there are no args, then we're done. */
9723a8
   if (!*argc)
9723a8
@@ -242,38 +259,45 @@ grub_parser_split_cmdline (const char *cmdline,
9723a8
       goto out;
9723a8
     }
9723a8
 
9723a8
-  /* Reserve memory for the return values.  */
9723a8
-  args = grub_malloc (bp - buffer);
9723a8
-  if (!args)
9723a8
-    goto fail;
9723a8
-  grub_memcpy (args, buffer, bp - buffer);
9723a8
-
9723a8
   *argv = grub_calloc (*argc + 1, sizeof (char *));
9723a8
   if (!*argv)
9723a8
     goto fail;
9723a8
 
9723a8
   /* The arguments are separated with 0's, setup argv so it points to
9723a8
      the right values.  */
9723a8
-  bp = args;
9723a8
   for (i = 0; i < *argc; i++)
9723a8
     {
9723a8
-      (*argv)[i] = bp;
9723a8
-      while (*bp)
9723a8
-	bp++;
9723a8
-      bp++;
9723a8
+      char *arg;
9723a8
+
9723a8
+      if (i > 0)
9723a8
+	{
9723a8
+	  if (grub_buffer_advance_read_pos (buffer, 1) != GRUB_ERR_NONE)
9723a8
+	    goto fail;
9723a8
+	}
9723a8
+
9723a8
+      arg = (char *) grub_buffer_peek_data (buffer);
9723a8
+      if (arg == NULL ||
9723a8
+	  grub_buffer_advance_read_pos (buffer, grub_strlen (arg)) != GRUB_ERR_NONE)
9723a8
+	goto fail;
9723a8
+
9723a8
+      (*argv)[i] = arg;
9723a8
     }
9723a8
 
9723a8
+  /* Keep memory for the return values. */
9723a8
+  grub_buffer_take_data (buffer);
9723a8
+
9723a8
   grub_errno = GRUB_ERR_NONE;
9723a8
 
9723a8
  out:
9723a8
   if (rd != cmdline)
9723a8
     grub_free (rd);
9723a8
+  grub_buffer_free (buffer);
9723a8
+  grub_buffer_free (varname);
9723a8
 
9723a8
   return grub_errno;
9723a8
 
9723a8
  fail:
9723a8
   grub_free (*argv);
9723a8
-  grub_free (args);
9723a8
   goto out;
9723a8
 }
9723a8