nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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