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