nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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