nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0020-Pass-x-hex-hex-straight-through-unmolested.patch

8e15ce
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8e15ce
From: Peter Jones <pjones@redhat.com>
8e15ce
Date: Mon, 1 Oct 2012 13:24:37 -0400
8e15ce
Subject: [PATCH] Pass "\x[[:hex:]][[:hex:]]" straight through unmolested.
8e15ce
8e15ce
Don't munge raw spaces when we're doing our cmdline escaping (#923374)
8e15ce
8e15ce
Signed-off-by: Peter Jones <pjones@redhat.com>
8e15ce
---
8e15ce
 grub-core/commands/wildcard.c | 16 +++++++++++++++-
8e15ce
 grub-core/lib/cmdline.c       | 25 +++++++++++++++++++++++--
8e15ce
 grub-core/script/execute.c    | 43 +++++++++++++++++++++++++++++++++++++------
8e15ce
 3 files changed, 75 insertions(+), 9 deletions(-)
8e15ce
8e15ce
diff --git a/grub-core/commands/wildcard.c b/grub-core/commands/wildcard.c
8e15ce
index cc3290311f0..8f67a4be7f0 100644
8e15ce
--- a/grub-core/commands/wildcard.c
8e15ce
+++ b/grub-core/commands/wildcard.c
8e15ce
@@ -488,6 +488,12 @@ check_file (const char *dir, const char *basename)
8e15ce
   return ctx.found;
8e15ce
 }
8e15ce
 
8e15ce
+static int
8e15ce
+is_hex(char c)
8e15ce
+{
8e15ce
+  return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
8e15ce
+}
8e15ce
+
8e15ce
 static void
8e15ce
 unescape (char *out, const char *in, const char *end)
8e15ce
 {
8e15ce
@@ -496,7 +502,15 @@ unescape (char *out, const char *in, const char *end)
8e15ce
 
8e15ce
   for (optr = out, iptr = in; iptr < end;)
8e15ce
     {
8e15ce
-      if (*iptr == '\\' && iptr + 1 < end)
8e15ce
+      if (*iptr == '\\' && iptr + 3 < end && iptr[1] == 'x' && is_hex(iptr[2]) && is_hex(iptr[3]))
8e15ce
+	{
8e15ce
+	  *optr++ = *iptr++;
8e15ce
+	  *optr++ = *iptr++;
8e15ce
+	  *optr++ = *iptr++;
8e15ce
+	  *optr++ = *iptr++;
8e15ce
+	  continue;
8e15ce
+	}
8e15ce
+      else if (*iptr == '\\' && iptr + 1 < end)
8e15ce
 	{
8e15ce
 	  *optr++ = iptr[1];
8e15ce
 	  iptr += 2;
8e15ce
diff --git a/grub-core/lib/cmdline.c b/grub-core/lib/cmdline.c
8e15ce
index ed0b149dca5..8e2294d8ff6 100644
8e15ce
--- a/grub-core/lib/cmdline.c
8e15ce
+++ b/grub-core/lib/cmdline.c
8e15ce
@@ -20,6 +20,12 @@
8e15ce
 #include <grub/lib/cmdline.h>
8e15ce
 #include <grub/misc.h>
8e15ce
 
8e15ce
+static int
8e15ce
+is_hex(char c)
8e15ce
+{
8e15ce
+  return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
8e15ce
+}
8e15ce
+
8e15ce
 static unsigned int check_arg (char *c, int *has_space)
8e15ce
 {
8e15ce
   int space = 0;
8e15ce
@@ -27,7 +33,13 @@ static unsigned int check_arg (char *c, int *has_space)
8e15ce
 
8e15ce
   while (*c)
8e15ce
     {
8e15ce
-      if (*c == '\\' || *c == '\'' || *c == '"')
8e15ce
+      if (*c == '\\' && *(c+1) == 'x' && is_hex(*(c+2)) && is_hex(*(c+3)))
8e15ce
+	{
8e15ce
+	  size += 4;
8e15ce
+	  c += 4;
8e15ce
+	  continue;
8e15ce
+	}
8e15ce
+      else if (*c == '\\' || *c == '\'' || *c == '"')
8e15ce
 	size++;
8e15ce
       else if (*c == ' ')
8e15ce
 	space = 1;
8e15ce
@@ -86,7 +98,16 @@ grub_create_loader_cmdline (int argc, char *argv[], char *buf,
8e15ce
 
8e15ce
       while (*c)
8e15ce
 	{
8e15ce
-	  if (*c == '\\' || *c == '\'' || *c == '"')
8e15ce
+	  if (*c == '\\' && *(c+1) == 'x' &&
8e15ce
+		   is_hex(*(c+2)) && is_hex(*(c+3)))
8e15ce
+	    {
8e15ce
+	      *buf++ = *c++;
8e15ce
+	      *buf++ = *c++;
8e15ce
+	      *buf++ = *c++;
8e15ce
+	      *buf++ = *c++;
8e15ce
+	      continue;
8e15ce
+	    }
8e15ce
+	  else if (*c == '\\' || *c == '\'' || *c == '"')
8e15ce
 	    *buf++ = '\\';
8e15ce
 
8e15ce
 	  *buf++ = *c;
8e15ce
diff --git a/grub-core/script/execute.c b/grub-core/script/execute.c
8e15ce
index ad80399246a..0c6dd9c5201 100644
8e15ce
--- a/grub-core/script/execute.c
8e15ce
+++ b/grub-core/script/execute.c
8e15ce
@@ -56,6 +56,12 @@ static struct grub_script_scope *scope = 0;
8e15ce
 /* Wildcard translator for GRUB script.  */
8e15ce
 struct grub_script_wildcard_translator *grub_wildcard_translator;
8e15ce
 
8e15ce
+static int
8e15ce
+is_hex(char c)
8e15ce
+{
8e15ce
+  return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
8e15ce
+}
8e15ce
+
8e15ce
 static char*
8e15ce
 wildcard_escape (const char *s)
8e15ce
 {
8e15ce
@@ -72,7 +78,15 @@ wildcard_escape (const char *s)
8e15ce
   i = 0;
8e15ce
   while ((ch = *s++))
8e15ce
     {
8e15ce
-      if (ch == '*' || ch == '\\' || ch == '?')
8e15ce
+      if (ch == '\\' && s[0] == 'x' && is_hex(s[1]) && is_hex(s[2]))
8e15ce
+	{
8e15ce
+	  p[i++] = ch;
8e15ce
+	  p[i++] = *s++;
8e15ce
+	  p[i++] = *s++;
8e15ce
+	  p[i++] = *s++;
8e15ce
+	  continue;
8e15ce
+	}
8e15ce
+      else if (ch == '*' || ch == '\\' || ch == '?')
8e15ce
 	p[i++] = '\\';
8e15ce
       p[i++] = ch;
8e15ce
     }
8e15ce
@@ -96,7 +110,14 @@ wildcard_unescape (const char *s)
8e15ce
   i = 0;
8e15ce
   while ((ch = *s++))
8e15ce
     {
8e15ce
-      if (ch == '\\')
8e15ce
+      if (ch == '\\' && s[0] == 'x' && is_hex(s[1]) && is_hex(s[2]))
8e15ce
+	{
8e15ce
+	  p[i++] = '\\';
8e15ce
+	  p[i++] = *s++;
8e15ce
+	  p[i++] = *s++;
8e15ce
+	  p[i++] = *s++;
8e15ce
+	}
8e15ce
+      else if (ch == '\\')
8e15ce
 	p[i++] = *s++;
8e15ce
       else
8e15ce
 	p[i++] = ch;
8e15ce
@@ -398,10 +419,20 @@ parse_string (const char *str,
8e15ce
     switch (*ptr)
8e15ce
       {
8e15ce
       case '\\':
8e15ce
-	escaped = !escaped;
8e15ce
-	if (!escaped && put)
8e15ce
-	  *(put++) = '\\';
8e15ce
-	ptr++;
8e15ce
+	if (!escaped && put && *(ptr+1) == 'x' && is_hex(*(ptr+2)) && is_hex(*(ptr+3)))
8e15ce
+	  {
8e15ce
+	    *(put++) = *ptr++;
8e15ce
+	    *(put++) = *ptr++;
8e15ce
+	    *(put++) = *ptr++;
8e15ce
+	    *(put++) = *ptr++;
8e15ce
+	  }
8e15ce
+	else
8e15ce
+	  {
8e15ce
+	    escaped = !escaped;
8e15ce
+	    if (!escaped && put)
8e15ce
+	      *(put++) = '\\';
8e15ce
+	    ptr++;
8e15ce
+	  }
8e15ce
 	break;
8e15ce
       case '$':
8e15ce
 	if (escaped)