nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

f725e3
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
f725e3
From: Peter Jones <pjones@redhat.com>
f725e3
Date: Mon, 1 Oct 2012 13:24:37 -0400
f725e3
Subject: [PATCH] Pass "\x[[:hex:]][[:hex:]]" straight through unmolested.
f725e3
f725e3
---
f725e3
 grub-core/commands/wildcard.c | 16 +++++++++++++++-
f725e3
 grub-core/lib/cmdline.c       | 34 ++++++++++++++++++++++++++++++++--
f725e3
 grub-core/script/execute.c    | 43 +++++++++++++++++++++++++++++++++++++------
f725e3
 3 files changed, 84 insertions(+), 9 deletions(-)
f725e3
f725e3
diff --git a/grub-core/commands/wildcard.c b/grub-core/commands/wildcard.c
f725e3
index 2807f806bd4..0f40e041541 100644
f725e3
--- a/grub-core/commands/wildcard.c
f725e3
+++ b/grub-core/commands/wildcard.c
f725e3
@@ -458,6 +458,12 @@ check_file (const char *dir, const char *basename)
f725e3
   return ctx.found;
f725e3
 }
f725e3
 
f725e3
+static int
f725e3
+is_hex(char c)
f725e3
+{
f725e3
+  return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
f725e3
+}
f725e3
+
f725e3
 static void
f725e3
 unescape (char *out, const char *in, const char *end)
f725e3
 {
f725e3
@@ -466,7 +472,15 @@ unescape (char *out, const char *in, const char *end)
f725e3
 
f725e3
   for (optr = out, iptr = in; iptr < end;)
f725e3
     {
f725e3
-      if (*iptr == '\\' && iptr + 1 < end)
f725e3
+      if (*iptr == '\\' && iptr + 3 < end && iptr[1] == 'x' && is_hex(iptr[2]) && is_hex(iptr[3]))
f725e3
+	{
f725e3
+	  *optr++ = *iptr++;
f725e3
+	  *optr++ = *iptr++;
f725e3
+	  *optr++ = *iptr++;
f725e3
+	  *optr++ = *iptr++;
f725e3
+	  continue;
f725e3
+	}
f725e3
+      else if (*iptr == '\\' && iptr + 1 < end)
f725e3
 	{
f725e3
 	  *optr++ = iptr[1];
f725e3
 	  iptr += 2;
f725e3
diff --git a/grub-core/lib/cmdline.c b/grub-core/lib/cmdline.c
f725e3
index d5e10ee8798..0a5b2afb94b 100644
f725e3
--- a/grub-core/lib/cmdline.c
f725e3
+++ b/grub-core/lib/cmdline.c
f725e3
@@ -20,6 +20,12 @@
f725e3
 #include <grub/lib/cmdline.h>
f725e3
 #include <grub/misc.h>
f725e3
 
f725e3
+static int
f725e3
+is_hex(char c)
f725e3
+{
f725e3
+  return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
f725e3
+}
f725e3
+
f725e3
 static unsigned int check_arg (char *c, int *has_space)
f725e3
 {
f725e3
   int space = 0;
f725e3
@@ -27,7 +33,13 @@ static unsigned int check_arg (char *c, int *has_space)
f725e3
 
f725e3
   while (*c)
f725e3
     {
f725e3
-      if (*c == '\\' || *c == '\'' || *c == '"')
f725e3
+      if (*c == '\\' && *(c+1) == 'x' && is_hex(*(c+2)) && is_hex(*(c+3)))
f725e3
+	{
f725e3
+	  size += 4;
f725e3
+	  c += 4;
f725e3
+	  continue;
f725e3
+	}
f725e3
+      else if (*c == '\\' || *c == '\'' || *c == '"')
f725e3
 	size++;
f725e3
       else if (*c == ' ')
f725e3
 	space = 1;
f725e3
@@ -85,7 +97,25 @@ int grub_create_loader_cmdline (int argc, char *argv[], char *buf,
f725e3
 
f725e3
       while (*c)
f725e3
 	{
f725e3
-	  if (*c == '\\' || *c == '\'' || *c == '"')
f725e3
+	  if (*c == ' ')
f725e3
+	    {
f725e3
+	      *buf++ = '\\';
f725e3
+	      *buf++ = 'x';
f725e3
+	      *buf++ = '2';
f725e3
+	      *buf++ = '0';
f725e3
+	      c++;
f725e3
+	      continue;
f725e3
+	    }
f725e3
+	  else if (*c == '\\' && *(c+1) == 'x' &&
f725e3
+		   is_hex(*(c+2)) && is_hex(*(c+3)))
f725e3
+	    {
f725e3
+	      *buf++ = *c++;
f725e3
+	      *buf++ = *c++;
f725e3
+	      *buf++ = *c++;
f725e3
+	      *buf++ = *c++;
f725e3
+	      continue;
f725e3
+	    }
f725e3
+	  else if (*c == '\\' || *c == '\'' || *c == '"')
f725e3
 	    *buf++ = '\\';
f725e3
 
f725e3
 	  *buf++ = *c;
f725e3
diff --git a/grub-core/script/execute.c b/grub-core/script/execute.c
f725e3
index afd551320f7..8f01c1bbfa2 100644
f725e3
--- a/grub-core/script/execute.c
f725e3
+++ b/grub-core/script/execute.c
f725e3
@@ -52,6 +52,12 @@ static struct grub_script_scope *scope = 0;
f725e3
 /* Wildcard translator for GRUB script.  */
f725e3
 struct grub_script_wildcard_translator *grub_wildcard_translator;
f725e3
 
f725e3
+static int
f725e3
+is_hex(char c)
f725e3
+{
f725e3
+  return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
f725e3
+}
f725e3
+
f725e3
 static char*
f725e3
 wildcard_escape (const char *s)
f725e3
 {
f725e3
@@ -68,7 +74,15 @@ wildcard_escape (const char *s)
f725e3
   i = 0;
f725e3
   while ((ch = *s++))
f725e3
     {
f725e3
-      if (ch == '*' || ch == '\\' || ch == '?')
f725e3
+      if (ch == '\\' && s[0] == 'x' && is_hex(s[1]) && is_hex(s[2]))
f725e3
+	{
f725e3
+	  p[i++] = ch;
f725e3
+	  p[i++] = *s++;
f725e3
+	  p[i++] = *s++;
f725e3
+	  p[i++] = *s++;
f725e3
+	  continue;
f725e3
+	}
f725e3
+      else if (ch == '*' || ch == '\\' || ch == '?')
f725e3
 	p[i++] = '\\';
f725e3
       p[i++] = ch;
f725e3
     }
f725e3
@@ -92,7 +106,14 @@ wildcard_unescape (const char *s)
f725e3
   i = 0;
f725e3
   while ((ch = *s++))
f725e3
     {
f725e3
-      if (ch == '\\')
f725e3
+      if (ch == '\\' && s[0] == 'x' && is_hex(s[1]) && is_hex(s[2]))
f725e3
+	{
f725e3
+	  p[i++] = '\\';
f725e3
+	  p[i++] = *s++;
f725e3
+	  p[i++] = *s++;
f725e3
+	  p[i++] = *s++;
f725e3
+	}
f725e3
+      else if (ch == '\\')
f725e3
 	p[i++] = *s++;
f725e3
       else
f725e3
 	p[i++] = ch;
f725e3
@@ -394,10 +415,20 @@ parse_string (const char *str,
f725e3
     switch (*ptr)
f725e3
       {
f725e3
       case '\\':
f725e3
-	escaped = !escaped;
f725e3
-	if (!escaped && put)
f725e3
-	  *(put++) = '\\';
f725e3
-	ptr++;
f725e3
+	if (!escaped && put && *(ptr+1) == 'x' && is_hex(*(ptr+2)) && is_hex(*(ptr+3)))
f725e3
+	  {
f725e3
+	    *(put++) = *ptr++;
f725e3
+	    *(put++) = *ptr++;
f725e3
+	    *(put++) = *ptr++;
f725e3
+	    *(put++) = *ptr++;
f725e3
+	  }
f725e3
+	else
f725e3
+	  {
f725e3
+	    escaped = !escaped;
f725e3
+	    if (!escaped && put)
f725e3
+	      *(put++) = '\\';
f725e3
+	    ptr++;
f725e3
+	  }
f725e3
 	break;
f725e3
       case '$':
f725e3
 	if (escaped)