Skip to contentSkip to conversation

fix kernel cmdline corruption

PATCH In review Olaf Hering 3 participants 7 messages Mar 18, 2020 – Mar 31, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fix kernel cmdline corruption

The functions grub_create_loader_cmdline and its sibling grub_loader_cmdline_size add extra, unexpected characters to the kernel cmdline. Both functions are supposed to copy each argv[] element verbatim. It is up to the caller of the kernel command ('linux' for example) to add desired quoting or escaping characters for the consumers of the kernel command line. The built-in grub shell gives enough opportunities to construct such argv[] elements.

After this change, these arguments result in the following kernel cmdline:

var='val' var="val" var=\'v1 v2\' var=\"v1 v2\" var=\\\"v1 v2\\\" x var=val var=val var='v1 v2' var="v1 v2" var=\"v1 v2\" x

Fixes commit 25953e10553dad2e378541a68686fc094603ec54

---
 grub-core/lib/cmdline.c | 58 ++++++++-----------------------------------------
 1 file changed, 9 insertions(+), 49 deletions(-)
grub-core/lib/cmdline.c +9 -49
index ed0b149dc..b572a367f 100644
@@ -20,31 +20,6 @@
 #include <grub/lib/cmdline.h>
 #include <grub/misc.h>
 
-static unsigned int check_arg (char *c, int *has_space)
-{
-  int space = 0;
-  unsigned int size = 0;
-
-  while (*c)
-    {
-      if (*c == '\\' || *c == '\'' || *c == '"')
-	size++;
-      else if (*c == ' ')
-	space = 1;
-
-      size++;
-      c++;
-    }
-
-  if (space)
-    size += 2;
-
-  if (has_space)
-    *has_space = space;
-
-  return size;
-}
-
 unsigned int grub_loader_cmdline_size (int argc, char *argv[])
 {
   int i;
@@ -52,7 +27,7 @@ unsigned int grub_loader_cmdline_size (int argc, char *argv[])
 
   for (i = 0; i < argc; i++)
     {
-      size += check_arg (argv[i], 0);
+      size += grub_strlen(argv[i]);
       size++; /* Separator space or NULL.  */
     }
 
@@ -66,36 +41,21 @@ grub_err_t
 grub_create_loader_cmdline (int argc, char *argv[], char *buf,
 			    grub_size_t size, enum grub_verify_string_type type)
 {
-  int i, space;
+  int i;
   unsigned int arg_size;
-  char *c, *orig_buf = buf;
+  char *orig_buf = buf;
 
   for (i = 0; i < argc; i++)
     {
-      c = argv[i];
-      arg_size = check_arg(argv[i], &space);
-      arg_size++; /* Separator space or NULL.  */
+      arg_size = grub_strlen(argv[i]);
 
-      if (size < arg_size)
+      /* Separator space or NULL.  */
+      if (size < arg_size + 1)
 	break;
 
-      size -= arg_size;
-
-      if (space)
-	*buf++ = '"';
-
-      while (*c)
-	{
-	  if (*c == '\\' || *c == '\'' || *c == '"')
-	    *buf++ = '\\';
-
-	  *buf++ = *c;
-	  c++;
-	}
-
-      if (space)
-	*buf++ = '"';
-
+      grub_memcpy(buf, argv[i], arg_size);
+      size -= arg_size + 1;
+      buf += arg_size;
       *buf++ = ' ';
     }
 
#1 lore raw

Hi Olaf,

The patch rang a bell to me and eventually I figured out that I had similar patch posted.

https://lists.gnu.org/archive/html/grub-devel/2018-04/msg00038.html

In short, the issue is not only in the fix itself, but also to take care and provide a measure to avoid incompatible grub.cfg before and after the patch.

To recap, there were two options provided to deal with the compatibilty issue.

1. Introduce a shell variable to disable the change, whenever anything goes wrong.

2. Use readonly feature variable similar to $feature_menuentry_id that can be used to detect capability and apply settings accordingly, thus still fulfill the requirement of one grub.cfg can work for all versions IMHO.

I am in favour of #2, but still they didn't seem to have upstream's consent yet.

Thanks,
Michael

Quoted 115 lines · Olaf Hering

The functions grub_create_loader_cmdline and its sibling grub_loader_cmdline_size add extra, unexpected characters to the kernel cmdline. Both functions are supposed to copy each argv[] element verbatim. It is up to the caller of the kernel command ('linux' for example) to add desired quoting or escaping characters for the consumers of the kernel command line. The built-in grub shell gives enough opportunities to construct such argv[] elements.

After this change, these arguments result in the following kernel cmdline:

var='val' var="val" var=\'v1 v2\' var=\"v1 v2\" var=\\\"v1 v2\\\" x var=val var=val var='v1 v2' var="v1 v2" var=\"v1 v2\" x

Fixes commit 25953e10553dad2e378541a68686fc094603ec54

---
 grub-core/lib/cmdline.c | 58 ++++++++-----------------------------------------
 1 file changed, 9 insertions(+), 49 deletions(-)
grub-core/lib/cmdline.c +9 -49
index ed0b149dc..b572a367f 100644
@@ -20,31 +20,6 @@
 #include <grub/lib/cmdline.h>
 #include <grub/misc.h>
 
-static unsigned int check_arg (char *c, int *has_space)
-{
-  int space = 0;
-  unsigned int size = 0;
-
-  while (*c)
-    {
-      if (*c == '\\' || *c == '\'' || *c == '"')
-	size++;
-      else if (*c == ' ')
-	space = 1;
-
-      size++;
-      c++;
-    }
-
-  if (space)
-    size += 2;
-
-  if (has_space)
-    *has_space = space;
-
-  return size;
-}
-
 unsigned int grub_loader_cmdline_size (int argc, char *argv[])
 {
   int i;
@@ -52,7 +27,7 @@ unsigned int grub_loader_cmdline_size (int argc, char *argv[])
 
   for (i = 0; i < argc; i++)
     {
-      size += check_arg (argv[i], 0);
+      size += grub_strlen(argv[i]);
       size++; /* Separator space or NULL.  */
     }
 
@@ -66,36 +41,21 @@ grub_err_t
 grub_create_loader_cmdline (int argc, char *argv[], char *buf,
 			    grub_size_t size, enum grub_verify_string_type type)
 {
-  int i, space;
+  int i;
   unsigned int arg_size;
-  char *c, *orig_buf = buf;
+  char *orig_buf = buf;
 
   for (i = 0; i < argc; i++)
     {
-      c = argv[i];
-      arg_size = check_arg(argv[i], &space);
-      arg_size++; /* Separator space or NULL.  */
+      arg_size = grub_strlen(argv[i]);
 
-      if (size < arg_size)
+      /* Separator space or NULL.  */
+      if (size < arg_size + 1)
 	break;
 
-      size -= arg_size;
-
-      if (space)
-	*buf++ = '"';
-
-      while (*c)
-	{
-	  if (*c == '\\' || *c == '\'' || *c == '"')
-	    *buf++ = '\\';
-
-	  *buf++ = *c;
-	  c++;
-	}
-
-      if (space)
-	*buf++ = '"';
-
+      grub_memcpy(buf, argv[i], arg_size);
+      size -= arg_size + 1;
+      buf += arg_size;
       *buf++ = ' ';
     }
 

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#2 lore raw

Am Thu, 19 Mar 2020 15:48:28 +0800
schrieb Michael Chang <[email protected]>:

Quoted 1 line

compatibilty issue.

There can not possibly be any compat issue.
Please go ahead and fix this bug.

Olaf

#3 lore raw
In reply to Michael Chang

CC-ing Vladimir...

Quoted 6 lines · Michael Chang

Hi Olaf,

The patch rang a bell to me and eventually I figured out that I had similar patch posted.

https://lists.gnu.org/archive/html/grub-devel/2018-04/msg00038.html

Yeah, it looks that it fell through the cracks...

Quoted 10 lines

In short, the issue is not only in the fix itself, but also to take care and provide a measure to avoid incompatible grub.cfg before and after the patch.

To recap, there were two options provided to deal with the compatibilty issue.

1. Introduce a shell variable to disable the change, whenever anything goes wrong.

Quoted 7 lines

2. Use readonly feature variable similar to $feature_menuentry_id that can be used to detect capability and apply settings accordingly, thus still fulfill the requirement of one grub.cfg can work for all versions IMHO.

I am in favour of #2, but still they didn't seem to have upstream's consent yet.

Michael, please prepare a patch and CC Olaf and Vladimir on it.

Daniel

#4 lore raw
Quoted 27 lines · Daniel Kiper

CC-ing Vladimir...

Quoted 6 lines · Michael Chang

Hi Olaf,

The patch rang a bell to me and eventually I figured out that I had similar patch posted.

https://lists.gnu.org/archive/html/grub-devel/2018-04/msg00038.html

Yeah, it looks that it fell through the cracks...

Quoted 15 lines

In short, the issue is not only in the fix itself, but also to take care and provide a measure to avoid incompatible grub.cfg before and after the patch.

To recap, there were two options provided to deal with the compatibilty issue.

1. Introduce a shell variable to disable the change, whenever anything goes wrong.

2. Use readonly feature variable similar to $feature_menuentry_id that can be used to detect capability and apply settings accordingly, thus still fulfill the requirement of one grub.cfg can work for all versions IMHO.

Quoted 4 lines
Quoted 2 lines

I am in favour of #2, but still they didn't seem to have upstream's consent yet.

Michael, please prepare a patch and CC Olaf and Vladimir on it.

OK. I will do so. May I ask is it for 2.06 release material or not ? As the deadline is drawing very near I'd better to have confirmation first.

Thanks,
Michael

Quoted 7 lines

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#5 lore raw
Quoted 28 lines · Michael Chang
Quoted 27 lines · Daniel Kiper

CC-ing Vladimir...

Quoted 6 lines · Michael Chang

Hi Olaf,

The patch rang a bell to me and eventually I figured out that I had similar patch posted.

https://lists.gnu.org/archive/html/grub-devel/2018-04/msg00038.html

Yeah, it looks that it fell through the cracks...

Quoted 15 lines

In short, the issue is not only in the fix itself, but also to take care and provide a measure to avoid incompatible grub.cfg before and after the patch.

To recap, there were two options provided to deal with the compatibilty issue.

1. Introduce a shell variable to disable the change, whenever anything goes wrong.

2. Use readonly feature variable similar to $feature_menuentry_id that can be used to detect capability and apply settings accordingly, thus still fulfill the requirement of one grub.cfg can work for all versions IMHO.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

I am in favour of #2, but still they didn't seem to have upstream's consent yet.

Michael, please prepare a patch and CC Olaf and Vladimir on it.

OK. I will do so. May I ask is it for 2.06 release material or not ? As the deadline is drawing very near I'd better to have confirmation first.

I want to get it fixed in 2.06. Or let's say I would like to have an option for an user/distro/... to change current behavior if he/she/... wants to do that. Hence, the patch cannot change the default for 2.06. We can change that after the release. IMO it is too late for that kind of experiments...

Daniel

#6 lore raw

Am Tue, 31 Mar 2020 15:33:54 +0200
schrieb Daniel Kiper <[email protected]>:

Quoted 1 line

the patch cannot change the default

Come on, this is obvious data corruption.
Noone would want to opt-in into data corruption. Noone has shown a usecase for that odd commit from 2011.

Apply the patch and be done with it.

Olaf