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