dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

Blame SOURCES/0298-script-Avoid-a-use-after-free-when-redefining-a-func.patch

80913e
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
80913e
From: Chris Coulson <chris.coulson@canonical.com>
80913e
Date: Fri, 10 Jul 2020 14:41:45 +0100
80913e
Subject: [PATCH] script: Avoid a use-after-free when redefining a function
80913e
 during execution
80913e
80913e
Defining a new function with the same name as a previously defined
80913e
function causes the grub_script and associated resources for the
80913e
previous function to be freed. If the previous function is currently
80913e
executing when a function with the same name is defined, this results
80913e
in use-after-frees when processing subsequent commands in the original
80913e
function.
80913e
80913e
Instead, reject a new function definition if it has the same name as
80913e
a previously defined function, and that function is currently being
80913e
executed. Although a behavioural change, this should be backwards
80913e
compatible with existing configurations because they can't be
80913e
dependent on the current behaviour without being broken.
80913e
80913e
Signed-off-by: Chris Coulson <chris.coulson@canonical.com>
80913e
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
80913e
Upstream-commit-id: f6253a1f540
80913e
---
80913e
 grub-core/script/execute.c  |  2 ++
80913e
 grub-core/script/function.c | 16 +++++++++++++---
80913e
 include/grub/script_sh.h    |  2 ++
80913e
 grub-core/script/parser.y   |  3 ++-
80913e
 4 files changed, 19 insertions(+), 4 deletions(-)
80913e
80913e
diff --git a/grub-core/script/execute.c b/grub-core/script/execute.c
80913e
index 528ddfd36f0..a1aadb9ee05 100644
80913e
--- a/grub-core/script/execute.c
80913e
+++ b/grub-core/script/execute.c
80913e
@@ -872,7 +872,9 @@ grub_script_function_call (grub_script_function_t func, int argc, char **args)
80913e
   old_scope = scope;
80913e
   scope = &new_scope;
80913e
 
80913e
+  func->executing++;
80913e
   ret = grub_script_execute (func->func);
80913e
+  func->executing--;
80913e
 
80913e
   function_return = 0;
80913e
   active_loops = loops;
80913e
diff --git a/grub-core/script/function.c b/grub-core/script/function.c
80913e
index d36655e510f..3aad04bf9dd 100644
80913e
--- a/grub-core/script/function.c
80913e
+++ b/grub-core/script/function.c
80913e
@@ -34,6 +34,7 @@ grub_script_function_create (struct grub_script_arg *functionname_arg,
80913e
   func = (grub_script_function_t) grub_malloc (sizeof (*func));
80913e
   if (! func)
80913e
     return 0;
80913e
+  func->executing = 0;
80913e
 
80913e
   func->name = grub_strdup (functionname_arg->str);
80913e
   if (! func->name)
80913e
@@ -60,10 +61,19 @@ grub_script_function_create (struct grub_script_arg *functionname_arg,
80913e
       grub_script_function_t q;
80913e
 
80913e
       q = *p;
80913e
-      grub_script_free (q->func);
80913e
-      q->func = cmd;
80913e
       grub_free (func);
80913e
-      func = q;
80913e
+      if (q->executing > 0)
80913e
+        {
80913e
+          grub_error (GRUB_ERR_BAD_ARGUMENT,
80913e
+		      N_("attempt to redefine a function being executed"));
80913e
+          func = NULL;
80913e
+        }
80913e
+      else
80913e
+        {
80913e
+          grub_script_free (q->func);
80913e
+          q->func = cmd;
80913e
+          func = q;
80913e
+        }
80913e
     }
80913e
   else
80913e
     {
80913e
diff --git a/include/grub/script_sh.h b/include/grub/script_sh.h
80913e
index b382bcf09bc..6c48e075122 100644
80913e
--- a/include/grub/script_sh.h
80913e
+++ b/include/grub/script_sh.h
80913e
@@ -361,6 +361,8 @@ struct grub_script_function
80913e
 
80913e
   /* The next element.  */
80913e
   struct grub_script_function *next;
80913e
+
80913e
+  unsigned executing;
80913e
 };
80913e
 typedef struct grub_script_function *grub_script_function_t;
80913e
 
80913e
diff --git a/grub-core/script/parser.y b/grub-core/script/parser.y
80913e
index 4f0ab8319e3..f80b86b6f15 100644
80913e
--- a/grub-core/script/parser.y
80913e
+++ b/grub-core/script/parser.y
80913e
@@ -289,7 +289,8 @@ function: "function" "name"
80913e
 	      grub_script_mem_free (state->func_mem);
80913e
 	    else {
80913e
 	      script->children = state->scripts;
80913e
-	      grub_script_function_create ($2, script);
80913e
+	      if (!grub_script_function_create ($2, script))
80913e
+		grub_script_free (script);
80913e
 	    }
80913e
 
80913e
 	    state->scripts = $<scripts>3;