420527
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
420527
From: Peter Jones <pjones@redhat.com>
420527
Date: Fri, 19 Oct 2018 10:57:52 -0400
420527
Subject: [PATCH] Fix menu entry selection based on ID and title
420527
420527
Currently if grub_strtoul(saved_entry_value, NULL, 0) does not return an
420527
error, we assume the value it has produced is a correct index into our
420527
menu entry list, and do not try to interpret the value as the "id" or
420527
"title" .  In cases where "id" or "title" start with a numeral, this
420527
makes them impossible to use as selection criteria.
420527
420527
This patch splits the search into three phases - matching id, matching
420527
title, and only once those have been exhausted, trying to interpret the
420527
ID as a numeral.  In that case, we also require that the entire string
420527
is numeric, not merely a string with leading numeric characters.
420527
420527
Resolves: rhbz#1640979
420527
---
420527
 grub-core/normal/menu.c | 146 +++++++++++++++++++++++++-----------------------
420527
 1 file changed, 75 insertions(+), 71 deletions(-)
420527
420527
diff --git a/grub-core/normal/menu.c b/grub-core/normal/menu.c
420527
index 6cb2a071490..95f7abaf2fd 100644
420527
--- a/grub-core/normal/menu.c
420527
+++ b/grub-core/normal/menu.c
420527
@@ -164,12 +164,12 @@ grub_menu_set_timeout (int timeout)
420527
 }
420527
 
420527
 static int
420527
-menuentry_eq (const char *id, const char *spec)
420527
+menuentry_eq (const char *id, const char *spec, int limit)
420527
 {
420527
   const char *ptr1, *ptr2;
420527
   ptr1 = id;
420527
   ptr2 = spec;
420527
-  while (1)
420527
+  while (limit == -1 || ptr1 - id <= limit)
420527
     {
420527
       if (*ptr2 == '>' && ptr2[1] != '>' && *ptr1 == 0)
420527
 	return ptr2 - spec;
420527
@@ -178,7 +178,11 @@ menuentry_eq (const char *id, const char *spec)
420527
       if (*ptr2 == '>')
420527
 	ptr2++;
420527
       if (*ptr1 != *ptr2)
420527
-	return 0;
420527
+	{
420527
+	  if (limit > -1 && ptr1 - id == limit && !*ptr1 && grub_isspace(*ptr2))
420527
+	    return ptr1 -id -1;
420527
+	  return 0;
420527
+	}
420527
       if (*ptr1 == 0)
420527
 	return ptr1 - id;
420527
       ptr1++;
420527
@@ -187,6 +191,61 @@ menuentry_eq (const char *id, const char *spec)
420527
   return 0;
420527
 }
420527
 
420527
+static int
420527
+get_entry_number_helper(grub_menu_t menu,
420527
+			const char * const val, const char ** const tail)
420527
+{
420527
+  /* See if the variable matches the title of a menu entry.  */
420527
+  int entry = -1;
420527
+  grub_menu_entry_t e;
420527
+  int i;
420527
+
420527
+  for (i = 0, e = menu->entry_list; e; i++)
420527
+    {
420527
+      int l = 0;
420527
+      while (val[l] && !grub_isspace(val[l]))
420527
+	l++;
420527
+
420527
+      if (menuentry_eq (e->id, val, l))
420527
+	{
420527
+	  if (tail)
420527
+	    *tail = val + l;
420527
+	  return i;
420527
+	}
420527
+      e = e->next;
420527
+    }
420527
+
420527
+  for (i = 0, e = menu->entry_list; e; i++)
420527
+    {
420527
+      int l = 0;
420527
+      while (val[l] && !grub_isspace(val[l]))
420527
+	l++;
420527
+
420527
+      if (menuentry_eq (e->title, val, l))
420527
+	{
420527
+	  if (tail)
420527
+	    *tail = val + l;
420527
+	  return i;
420527
+	}
420527
+      e = e->next;
420527
+    }
420527
+
420527
+  if (tail)
420527
+    *tail = NULL;
420527
+
420527
+  entry = (int) grub_strtoul (val, tail, 0);
420527
+  if (grub_errno == GRUB_ERR_BAD_NUMBER ||
420527
+      (*tail && **tail && !grub_isspace(**tail)))
420527
+    {
420527
+      entry = -1;
420527
+      if (tail)
420527
+	*tail = NULL;
420527
+      grub_errno = GRUB_ERR_NONE;
420527
+    }
420527
+
420527
+  return entry;
420527
+}
420527
+
420527
 /* Get the first entry number from the value of the environment variable NAME,
420527
    which is a space-separated list of non-negative integers.  The entry number
420527
    which is returned is stripped from the value of NAME.  If no entry number
420527
@@ -195,9 +254,8 @@ static int
420527
 get_and_remove_first_entry_number (grub_menu_t menu, const char *name)
420527
 {
420527
   const char *val;
420527
-  char *tail;
420527
+  const char *tail;
420527
   int entry;
420527
-  int sz = 0;
420527
 
420527
   val = grub_env_get (name);
420527
   if (! val)
420527
@@ -205,50 +263,24 @@ get_and_remove_first_entry_number (grub_menu_t menu, const char *name)
420527
 
420527
   grub_error_push ();
420527
 
420527
-  entry = (int) grub_strtoul (val, &tail, 0);
420527
+  entry = get_entry_number_helper(menu, val, &tail);
420527
+  if (!(*tail == 0 || grub_isspace(*tail)))
420527
+    entry = -1;
420527
 
420527
-  if (grub_errno == GRUB_ERR_BAD_NUMBER)
420527
+  if (entry >= 0)
420527
     {
420527
-      /* See if the variable matches the title of a menu entry.  */
420527
-      grub_menu_entry_t e = menu->entry_list;
420527
-      int i;
420527
-
420527
-      for (i = 0; e; i++)
420527
-	{
420527
-	  sz = menuentry_eq (e->title, val);
420527
-	  if (sz < 1)
420527
-	    sz = menuentry_eq (e->id, val);
420527
-
420527
-	  if (sz >= 1)
420527
-	    {
420527
-	      entry = i;
420527
-	      break;
420527
-	    }
420527
-	  e = e->next;
420527
-	}
420527
-
420527
-      if (sz > 0)
420527
-	grub_errno = GRUB_ERR_NONE;
420527
-
420527
-      if (! e)
420527
-	entry = -1;
420527
-    }
420527
-
420527
-  if (grub_errno == GRUB_ERR_NONE)
420527
-    {
420527
-      if (sz > 0)
420527
-	tail += sz;
420527
-
420527
       /* Skip whitespace to find the next entry.  */
420527
       while (*tail && grub_isspace (*tail))
420527
 	tail++;
420527
-      grub_env_set (name, tail);
420527
+      if (*tail)
420527
+	grub_env_set (name, tail);
420527
+      else
420527
+	grub_env_unset (name);
420527
     }
420527
   else
420527
     {
420527
       grub_env_unset (name);
420527
       grub_errno = GRUB_ERR_NONE;
420527
-      entry = -1;
420527
     }
420527
 
420527
   grub_error_pop ();
420527
@@ -525,6 +557,7 @@ static int
420527
 get_entry_number (grub_menu_t menu, const char *name)
420527
 {
420527
   const char *val;
420527
+  const char *tail;
420527
   int entry;
420527
 
420527
   val = grub_env_get (name);
420527
@@ -532,38 +565,9 @@ get_entry_number (grub_menu_t menu, const char *name)
420527
     return -1;
420527
 
420527
   grub_error_push ();
420527
-
420527
-  entry = (int) grub_strtoul (val, 0, 0);
420527
-
420527
-  if (grub_errno == GRUB_ERR_BAD_NUMBER)
420527
-    {
420527
-      /* See if the variable matches the title of a menu entry.  */
420527
-      grub_menu_entry_t e = menu->entry_list;
420527
-      int i;
420527
-
420527
-      grub_errno = GRUB_ERR_NONE;
420527
-
420527
-      for (i = 0; e; i++)
420527
-	{
420527
-	  if (menuentry_eq (e->title, val)
420527
-	      || menuentry_eq (e->id, val))
420527
-	    {
420527
-	      entry = i;
420527
-	      break;
420527
-	    }
420527
-	  e = e->next;
420527
-	}
420527
-
420527
-      if (! e)
420527
-	entry = -1;
420527
-    }
420527
-
420527
-  if (grub_errno != GRUB_ERR_NONE)
420527
-    {
420527
-      grub_errno = GRUB_ERR_NONE;
420527
-      entry = -1;
420527
-    }
420527
-
420527
+  entry = get_entry_number_helper(menu, val, &tail);
420527
+  if (*tail != '\0')
420527
+    entry = -1;
420527
   grub_error_pop ();
420527
 
420527
   return entry;