Blame SOURCES/0125-Fix-menu-entry-selection-based-on-ID-and-title.patch

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