Skip to contentSkip to conversation

Fix cfg file parsing and command line parsing

PATCH In review Shreenidhi Shedi 3 participants 10 messages May 19, 2025 – Jul 31, 2025

lore
Share & tools
lore
#0 lore raw

v2 cover Fix cfg file parsing and command line parsing

Hi GRUB Developers,

This patch series contains two fixes to improve how GRUB handles environment files and constructs the kernel command line, these issues were reported by PhotonOS customers.

The first patch ensures that empty lines in environment files are skipped during parsing.

The second patch removes unnecessary escaping of quotes in the constructed command line, as the Linux kernel does not support escaped quotes.

Thanks for your time and consideration.

v0 changes:
- Initial revision.
- https://lists.gnu.org/archive/html/grub-devel/2025-05/msg00085.html

v1 changes:
- Added cover leter
- Improved commit message as suggested by Daniel Kiper

v2 changes:
- Handle '\r' along with '\n' in envblk

--
Shreenidhi Shedi

--
2.49.0

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

#1 lore raw

PATCH v2 1/2 envblk: ignore empty new lines while parsing env files

From: Shreenidhi Shedi <[email protected]>

Environment files may contain empty lines, which should be ignored during parsing. Currently, these lines are not skipped and resulting in incorrect behavior. This patch adds a check to skip empty lines along with those starting with `#'.

---
 grub-core/lib/envblk.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/lib/envblk.c +1 -1
index 2e4e78b13..4862de50d 100644
@@ -235,7 +235,7 @@ grub_envblk_iterate (grub_envblk_t envblk,
 
   while (p < pend)
     {
-      if (*p != '#')
+      if (*p != '#' && *p != '\n' && *p != '\r')
         {
           char *name;
           char *value;
Signature · 7 lines
2.49.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Reviewed-by Daniel Kiper on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] envblk: ignore empty new lines while parsing env files

Quoted 9 lines · Shreenidhi Shedi

From: Shreenidhi Shedi <[email protected]>

Environment files may contain empty lines, which should be ignored during parsing. Currently, these lines are not skipped and resulting in incorrect behavior. This patch adds a check to skip empty lines along with those starting with `#'.

Though I suggest to use grub-editenv to edit environment files to avoid various format issues...

Daniel

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

#3 lore raw

PATCH v2 2/2 cmdline: fix command line parsing in grub

In reply to Shreenidhi Shedi

From: Shreenidhi Shedi <[email protected]>

Previously, the command line construction function (grub_create_loader_cmdline) escaped single and double quotes, which is unnecessary and potentially problematic since the kernel command line handler does not support escaped quotes. This patch removes the escaping of these characters, ensuring that the constructed command line is passed to the kernel as intended.

For example:

In /boot/grub/grub.cfg
...
set user_cmdline="dyndbg='+p; func smp_callin -p'"
...
menuentry "Photon" {
linux /boot/$photon_linux root=$rootpartition $photon_cmdline $systemd_cmdline $user_cmdline if [ -f /boot/$photon_initrd ]; then
initrd /boot/$photon_initrd
fi
}
...

Post reboot,

$ cat /proc/cmdline
... dyndbg=\'+p; func smp_callin -p\'
---
 grub-core/lib/cmdline.c | 17 +++--------------
 1 file changed, 3 insertions(+), 14 deletions(-)
grub-core/lib/cmdline.c +3 -14
index ed0b149dc..57c96a151 100644
@@ -22,7 +22,6 @@
 
 static unsigned int check_arg (char *c, int *has_space)
 {
-  int space = 0;
   unsigned int size = 0;
 
   while (*c)
@@ -51,15 +50,9 @@ unsigned int grub_loader_cmdline_size (int argc, char *argv[])
   unsigned int size = 0;
 
   for (i = 0; i < argc; i++)
-    {
       size += check_arg (argv[i], 0);
-      size++; /* Separator space or NULL.  */
-    }
-
-  if (size == 0)
-    size = 1;
 
-  return size;
+  return size ? size : 1;
 }
 
 grub_err_t
@@ -73,8 +66,8 @@ grub_create_loader_cmdline (int argc, char *argv[], char *buf,
   for (i = 0; i < argc; i++)
     {
       c = argv[i];
+      space = 0;
       arg_size = check_arg(argv[i], &space);
-      arg_size++; /* Separator space or NULL.  */
 
       if (size < arg_size)
 	break;
@@ -86,11 +79,7 @@ grub_create_loader_cmdline (int argc, char *argv[], char *buf,
 
       while (*c)
 	{
-	  if (*c == '\\' || *c == '\'' || *c == '"')
-	    *buf++ = '\\';
-
-	  *buf++ = *c;
-	  c++;
+	  *buf++ = *c++;
 	}
 
       if (space)
Signature · 7 lines
2.49.0


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

Re: [PATCH v2 2/2] cmdline: fix command line parsing in grub

Quoted 23 lines · Shreenidhi Shedi

From: Shreenidhi Shedi <[email protected]>

Previously, the command line construction function (grub_create_loader_cmdline) escaped single and double quotes, which is unnecessary and potentially problematic since the kernel command line handler does not support escaped quotes. This patch removes the escaping of these characters, ensuring that the constructed command line is passed to the kernel as intended.

For example:

In /boot/grub/grub.cfg
...
set user_cmdline="dyndbg='+p; func smp_callin -p'"
...
menuentry "Photon" {
linux /boot/$photon_linux root=$rootpartition $photon_cmdline $systemd_cmdline $user_cmdline if [ -f /boot/$photon_initrd ]; then
initrd /boot/$photon_initrd
fi
}
...

Quoted 4 lines

Post reboot,

$ cat /proc/cmdline
... dyndbg=\'+p; func smp_callin -p\'

AFIACT Vladimir has more complete fix for this issue. I asked him to post it once again...

Daniel

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

#5 lore raw

Re: [PATCH v2 2/2] cmdline: fix command line parsing in grub

Quoted 26 lines · Daniel Kiper
Quoted 25 lines · Shreenidhi Shedi

From: Shreenidhi Shedi <[email protected]>

Previously, the command line construction function (grub_create_loader_cmdline) escaped single and double quotes, which is unnecessary and potentially problematic since the kernel command line handler does not support escaped quotes. This patch removes the escaping of these characters, ensuring that the constructed command line is passed to the kernel as intended.

For example:

In /boot/grub/grub.cfg
...
set user_cmdline="dyndbg='+p; func smp_callin -p'"
...
menuentry "Photon" {
linux /boot/$photon_linux root=$rootpartition $photon_cmdline $systemd_cmdline $user_cmdline if [ -f /boot/$photon_initrd ]; then
initrd /boot/$photon_initrd
fi
}
...

Post reboot,

Quoted 7 lines
Quoted 2 lines
$ cat /proc/cmdline
... dyndbg=\'+p; func smp_callin -p\'

AFIACT Vladimir has more complete fix for this issue. I asked him to post it once again...

Daniel

Hi Daniel,

Can you please add Vladimir to this thread? This is a regression and if we can get a better fix, I can test it and provide an update.

Signature · 6 lines
Shedi

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

Re: [PATCH v2 2/2] cmdline: fix command line parsing in grub

Here is the patch in question:
https://lists.gnu.org/archive/html/grub-devel/2023-07/msg00089.html. It will need an additional change to Linux calling.

Regards
Vladimir 'phcoder' Serbinenko

Quoted 41 lines · Shreenidhi Shedi
Quoted 10 lines · Daniel Kiper
Quoted 9 lines · Shreenidhi Shedi

From: Shreenidhi Shedi <[email protected]>

Previously, the command line construction function (grub_create_loader_cmdline) escaped single and double quotes, which is unnecessary and potentially problematic since the kernel command line handler does not support escaped quotes. This patch removes the escaping of these characters, ensuring that the constructed command line is passed to the kernel as intended.

Quoted 8 lines
Quoted 8 lines

For example:

In /boot/grub/grub.cfg
...
set user_cmdline="dyndbg='+p; func smp_callin -p'"
...
menuentry "Photon" {
linux /boot/$photon_linux root=$rootpartition $photon_cmdline

$systemd_cmdline $user_cmdline

Quoted 8 lines
Quoted 8 lines

if [ -f /boot/$photon_initrd ]; then
initrd /boot/$photon_initrd
fi
}
...

Post reboot,

Quoted 7 lines
Quoted 2 lines
$ cat /proc/cmdline
... dyndbg=\'+p; func smp_callin -p\'

AFIACT Vladimir has more complete fix for this issue. I asked him to post it once again...

Daniel

Hi Daniel,

Can you please add Vladimir to this thread? This is a regression and if we can get a better fix, I can test it and provide an update.

Quoted 8 lines

--
Shedi

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

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

#7 lore raw

Re: [PATCH v2 2/2] cmdline: fix command line parsing in grub

In reply to Shreenidhi Shedi
Quoted 30 lines · Shreenidhi Shedi
Quoted 29 lines · Daniel Kiper
Quoted 27 lines · Shreenidhi Shedi

From: Shreenidhi Shedi <[email protected]>

Previously, the command line construction function (grub_create_loader_cmdline) escaped single and double quotes, which is unnecessary and potentially problematic since the kernel command line handler does not support escaped quotes. This patch removes the escaping of these characters, ensuring that the constructed command line is passed to the kernel as intended.

For example:

In /boot/grub/grub.cfg
...
set user_cmdline="dyndbg='+p; func smp_callin -p'"
...
menuentry "Photon" {
   linux /boot/$photon_linux root=$rootpartition $photon_cmdline $systemd_cmdline $user_cmdline if [ -f /boot/$photon_initrd ]; then
     initrd /boot/$photon_initrd
   fi
}
...

Post reboot,

$ cat /proc/cmdline
... dyndbg=\'+p; func smp_callin -p\'
Quoted 9 lines
Quoted 4 lines

AFIACT Vladimir has more complete fix for this issue. I asked him to post it once again...

Daniel

Hi Daniel,

Can you please add Vladimir to this thread? This is a regression and if we can get a better fix, I can test it and provide an update.

Hi Daniel and Vladimir,

I tested the Vladimir's patch which he gave at
https://lists.gnu.org/archive/html/grub-devel/2025-05/msg00182.html

I managed to get it working with few tweaks. I introduced a special arg 'noescape' to set noescape argument to 1 while calling 'grub_create_loader_cmdline' function. In the given patch it is hard coded to 0. Is there any other way to set it to 1?

I have a question, what is the significance of escaping the quotes while creating kernel command line args while Linux kernel doesn't support escape sequences in command line args?

Signature · 6 lines
Shedi

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

Re: [PATCH v2 2/2] cmdline: fix command line parsing in grub

Quoted 41 lines · Shreenidhi Shedi
Quoted 30 lines · Shreenidhi Shedi
Quoted 29 lines · Daniel Kiper
Quoted 27 lines · Shreenidhi Shedi

From: Shreenidhi Shedi <[email protected]>

Previously, the command line construction function (grub_create_loader_cmdline) escaped single and double quotes, which is unnecessary and potentially problematic since the kernel command line handler does not support escaped quotes. This patch removes the escaping of these characters, ensuring that the constructed command line is passed to the kernel as intended.

For example:

In /boot/grub/grub.cfg
...
set user_cmdline="dyndbg='+p; func smp_callin -p'"
...
menuentry "Photon" {
   linux /boot/$photon_linux root=$rootpartition $photon_cmdline $systemd_cmdline $user_cmdline if [ -f /boot/$photon_initrd ]; then
     initrd /boot/$photon_initrd
   fi
}
...

Post reboot,

$ cat /proc/cmdline
... dyndbg=\'+p; func smp_callin -p\'
Quoted 9 lines
Quoted 4 lines

AFIACT Vladimir has more complete fix for this issue. I asked him to post it once again...

Daniel

Hi Daniel,

Can you please add Vladimir to this thread? This is a regression and if we can get a better fix, I can test it and provide an update.

Quoted 9 lines

Hi Daniel and Vladimir,

I tested the Vladimir's patch which he gave at
https://lists.gnu.org/archive/html/grub-devel/2025-05/msg00182.html

I managed to get it working with few tweaks. I introduced a special arg 'noescape' to set noescape argument to 1 while calling 'grub_create_loader_cmdline' function. In the given patch it is hard coded to 0. Is there any other way to set it to 1?

I have a question, what is the significance of escaping the quotes while creating kernel command line args while Linux kernel doesn't support escape sequences in command line args?

Hi Daniel,

Gentle reminder on this query.

Signature · 6 lines
Shedi

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
author Reviewed-by Alexey Makhalov
#9 lore raw

Re: [PATCH v2 2/2] cmdline: fix command line parsing in grub

Quoted 47 lines · Shreenidhi Shedi
Quoted 46 lines · Shreenidhi Shedi
Quoted 30 lines · Shreenidhi Shedi
Quoted 29 lines · Daniel Kiper
Quoted 27 lines · Shreenidhi Shedi

From: Shreenidhi Shedi <[email protected]>

Previously, the command line construction function (grub_create_loader_cmdline) escaped single and double quotes, which is unnecessary and potentially problematic since the kernel command line handler does not support escaped quotes. This patch removes the escaping of these characters, ensuring that the constructed command line is passed to the kernel as intended.

For example:

In /boot/grub/grub.cfg
...
set user_cmdline="dyndbg='+p; func smp_callin -p'"
...
menuentry "Photon" {
   linux /boot/$photon_linux root=$rootpartition $photon_cmdline $systemd_cmdline $user_cmdline if [ -f /boot/$photon_initrd ]; then
     initrd /boot/$photon_initrd
   fi
}
...

Post reboot,

$ cat /proc/cmdline
... dyndbg=\'+p; func smp_callin -p\'
Quoted 9 lines
Quoted 4 lines

AFIACT Vladimir has more complete fix for this issue. I asked him to post it once again...

Daniel

Hi Daniel,

Can you please add Vladimir to this thread? This is a regression and if we can get a better fix, I can test it and provide an update.

Hi Daniel and Vladimir,

I tested the Vladimir's patch which he gave at
https://lists.gnu.org/archive/html/grub-devel/2025-05/msg00182.html

Quoted 9 lines
Quoted 4 lines

I managed to get it working with few tweaks. I introduced a special arg 'noescape' to set noescape argument to 1 while calling 'grub_create_loader_cmdline' function. In the given patch it is hard coded to 0. Is there any other way to set it to 1?

I have a question, what is the significance of escaping the quotes while creating kernel command line args while Linux kernel doesn't support escape sequences in command line args?

Hi Daniel,

Gentle reminder on this query.

From 1e062220d64ce1cf491ad670f368c06072668d2b Mon Sep 17 00:00:00 2001
From: Shreenidhi Shedi <[email protected]>
Date: Wed, 14 May 2025 14:47:15 +0530
Subject: [PATCH v3 1/1] cmdline: fix command line parsing in grub

Previously, the command line construction function (grub_create_loader_cmdline) escaped single and double quotes, which is unnecessary and potentially problematic since the kernel command line handler does not support escaped quotes. This patch removes the escaping of these characters, ensuring that the constructed command line is passed to the kernel as intended.

For example:

In /boot/grub/grub.cfg
...
set user_cmdline="dyndbg='+p; func smp_callin -p'"
...
menuentry "Photon" {
linux /boot/$photon_linux root=$rootpartition $photon_cmdline
$systemd_cmdline $user_cmdline
if [ -f /boot/$photon_initrd ]; then
initrd /boot/$photon_initrd
fi
}
...

Post reboot,

$ cat /proc/cmdline
... dyndbg=\'+p; func smp_callin -p\'
---
 grub-core/lib/cmdline.c | 52 +++++++++++++++++------------------------
 1 file changed, 21 insertions(+), 31 deletions(-)
grub-core/lib/cmdline.c +21 -31
index ed0b149dc..dfb661a7f 100644
@@ -20,51 +20,45 @@
 #include <grub/lib/cmdline.h>
 #include <grub/misc.h>
 
-static unsigned int check_arg (char *c, int *has_space)
+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;
-
+      if (*c == ' ')
+	{
+	  if (*has_space == 0)
+	    {
+	      *has_space = 1;
+	      size += 2;
+	    }
+	}
       size++;
       c++;
     }
 
-  if (space)
-    size += 2;
-
-  if (has_space)
-    *has_space = space;
-
-  return size;
+  /* For separator space or NULL.  */
+  return size + 1;
 }
 
-unsigned int grub_loader_cmdline_size (int argc, char *argv[])
+unsigned int
+grub_loader_cmdline_size (int argc, char *argv[])
 {
   int i;
   unsigned int size = 0;
 
   for (i = 0; i < argc; i++)
-    {
-      size += check_arg (argv[i], 0);
-      size++; /* Separator space or NULL.  */
-    }
+    size += check_arg (argv[i], 0);
 
-  if (size == 0)
-    size = 1;
-
-  return size;
+  return size ? size : 1;
 }
 
 grub_err_t
 grub_create_loader_cmdline (int argc, char *argv[], char *buf,
-			    grub_size_t size, enum grub_verify_string_type type)
+			    grub_size_t size,
+			    enum grub_verify_string_type type)
 {
   int i, space;
   unsigned int arg_size;
@@ -73,8 +67,8 @@ grub_create_loader_cmdline (int argc, char *argv[], char *buf,
   for (i = 0; i < argc; i++)
     {
       c = argv[i];
-      arg_size = check_arg(argv[i], &space);
-      arg_size++; /* Separator space or NULL.  */
+      space = 0;
+      arg_size = check_arg (argv[i], &space);
 
       if (size < arg_size)
 	break;
@@ -86,11 +80,7 @@ grub_create_loader_cmdline (int argc, char *argv[], char *buf,
 
       while (*c)
 	{
-	  if (*c == '\\' || *c == '\'' || *c == '"')
-	    *buf++ = '\\';
-
-	  *buf++ = *c;
-	  c++;
+	  *buf++ = *c++;
 	}
 
       if (space)

--
2.50.1

Sending this for completeness, some hunks were missed in second patch in all revisions. In case if someone needs this.

Signature · 6 lines
Shedi

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