Skip to contentSkip to conversation

initramfs-framework: init: fix kernel cmdline parsing

PATCH In review [email protected] 1 participant 2 messages Mar 26 – Apr 3

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 initramfs-framework: init: fix kernel cmdline parsing

From: Michael Opdenacker <[email protected]>

Fix several issues with double quotes in kernel command line

- Kernel options like 'opt="value"' were breaking the parser, causing the whole reminder of the command line to be ignored. The code only supported 'opt="word1 word2..."

- Setting variables without removing quotes in the value

- Setting variables to values with spaces without enclosing the value with quotes. This caused execution errors evaluating expressions like:
bootparam_opt=word1 word2

The first fix is particularly needed for people using the kernel "bootconfig" configuration parameters to add options to the kernel command line:

CONFIG_BOOT_CONFIG=y
CONFIG_BOOT_CONFIG_EMBED=y CONFIG_BOOT_CONFIG_EMBED_FILE="additional-bootargs.bootconfig"

This mechanism systematically adds quotes around options with values, for example:
init="/sbin/preinit"

Without the fix, the wrong init program can be started from the initramfs and debug messages are ignored when "debug" is present after "init" in the kernel command line.

For readability and performance sake, also use shell variable operators instead of "sed" to remove leading and trailing quotes.

Tested both on host and target machines.
With the below kernel command line:
rootwait init="/sbin/preinit" debug root=/dev/mmcblk0p2 console=ttymxc0 dyndbg="file drivers/usb/core/hub.c +pltf" quiet

The following variables are set:
bootparam_rootwait="true" bootparam_init="/sbin/preinit" bootparam_root="/dev/mmcblk0p2" bootparam_debug="true" bootparam_console="ttymxc0" bootparam_dyndbg="file drivers/usb/core/hub.c +pltf" bootparam_quiet="true"

---
 .../initrdscripts/initramfs-framework/init    | 24 +++++++++++++++----
 1 file changed, 19 insertions(+), 5 deletions(-)
meta/recipes-core/initrdscripts/initramfs-framework/init +19 -5
index 51db083e2e..67590ad765 100755
@@ -94,9 +94,11 @@ fi
 # populate bootparam environment
 for p in `cat /proc/cmdline`; do
 	if [ -n "$quoted" ]; then
-		value="$value $p"
-		if [ "`echo $p | sed -e 's/\"$//'`" != "$p" ]; then
-			eval "bootparam_${quoted}=${value}"
+		p_rstripped=${p%\"}
+		value="$value $p_rstripped"
+		if [ "$p_rstripped" != "$p" ]; then
+			# End of a opt="word1 word2..." parameter 
+			eval "bootparam_${quoted}=\"${value}\""
 			unset quoted
 		fi
 		continue
@@ -105,11 +107,23 @@ for p in `cat /proc/cmdline`; do
 	opt=`echo $p | cut -d'=' -f1`
 	opt=`echo $opt | sed -e 'y/.-/__/'`
 	if [ "`echo $p | cut -d'=' -f1`" = "$p" ]; then
+		# opt parameter
 		eval "bootparam_${opt}=true"
 	else
-		value="`echo $p | cut -d'=' -f2-`"
-		if [ "`echo $value | sed -e 's/^\"//'`" != "$value" ]; then
+		value="`echo $p | cut -d'=' -f2-`"	# Option value
+		value_lstripped=${value#\"}
+		value_rstripped=${value%\"}
+
+		if [ "$value_lstripped" != "$value" ] && [ "$value_rstripped" != "$value" ]; then
+			# opt="value" parameter
+			eval "bootparam_${opt}=${value_lstripped%\"}"
+			continue
+		fi
+
+		if [ "$value_lstripped" != "$value" ]; then
+			# Start of a opt="word1 word2..." parameter 
 			quoted=${opt}
+			value=${value_lstripped}
 			continue
 		fi
 		eval "bootparam_${opt}=\"${value}\""
#1 lore raw

Greetings

Quoted 87 lines · On 3/26/26 6:34 PM, [email protected] wrote:

From: Michael Opdenacker <[email protected]>

Fix several issues with double quotes in kernel command line

- Kernel options like 'opt="value"' were breaking the parser, causing the whole reminder of the command line to be ignored. The code only supported 'opt="word1 word2..."

- Setting variables without removing quotes in the value

- Setting variables to values with spaces without enclosing the value with quotes. This caused execution errors evaluating expressions like:
bootparam_opt=word1 word2

The first fix is particularly needed for people using the kernel "bootconfig" configuration parameters to add options to the kernel command line:

CONFIG_BOOT_CONFIG=y
CONFIG_BOOT_CONFIG_EMBED=y CONFIG_BOOT_CONFIG_EMBED_FILE="additional-bootargs.bootconfig"

This mechanism systematically adds quotes around options with values, for example:
init="/sbin/preinit"

Without the fix, the wrong init program can be started from the initramfs and debug messages are ignored when "debug" is present after "init" in the kernel command line.

For readability and performance sake, also use shell variable operators instead of "sed" to remove leading and trailing quotes.

Tested both on host and target machines.
With the below kernel command line:
rootwait init="/sbin/preinit" debug root=/dev/mmcblk0p2 console=ttymxc0 dyndbg="file drivers/usb/core/hub.c +pltf" quiet

The following variables are set:
bootparam_rootwait="true" bootparam_init="/sbin/preinit" bootparam_root="/dev/mmcblk0p2" bootparam_debug="true" bootparam_console="ttymxc0" bootparam_dyndbg="file drivers/usb/core/hub.c +pltf" bootparam_quiet="true"

---
  .../initrdscripts/initramfs-framework/init    | 24 +++++++++++++++----
  1 file changed, 19 insertions(+), 5 deletions(-)
meta/recipes-core/initrdscripts/initramfs-framework/init +13 -5
index 51db083e2e..67590ad765 100755
@@ -94,9 +94,11 @@ fi
  # populate bootparam environment
  for p in `cat /proc/cmdline`; do
  	if [ -n "$quoted" ]; then
-		value="$value $p"
-		if [ "`echo $p | sed -e 's/\"$//'`" != "$p" ]; then
-			eval "bootparam_${quoted}=${value}"
+		p_rstripped=${p%\"}
+		value="$value $p_rstripped"
+		if [ "$p_rstripped" != "$p" ]; then
+			# End of a opt="word1 word2..." parameter
+			eval "bootparam_${quoted}=\"${value}\""
  			unset quoted
  		fi
  		continue
@@ -105,11 +107,23 @@ for p in `cat /proc/cmdline`; do
  	opt=`echo $p | cut -d'=' -f1`
  	opt=`echo $opt | sed -e 'y/.-/__/'`
  	if [ "`echo $p | cut -d'=' -f1`" = "$p" ]; then
+		# opt parameter
  		eval "bootparam_${opt}=true"
  	else
-		value="`echo $p | cut -d'=' -f2-`"
-		if [ "`echo $value | sed -e 's/^\"//'`" != "$value" ]; then
+		value="`echo $p | cut -d'=' -f2-`"	# Option value
+		value_lstripped=${value#\"}
+		value_rstripped=${value%\"}
+
+		if [ "$value_lstripped" != "$value" ] && [ "$value_rstripped" != "$value" ]; then
+			# opt="value" parameter
+			eval "bootparam_${opt}=${value_lstripped%\"}"

meta/recipes-core/initrdscripts/initramfs-framework/init for p in `cat /proc/cmdline`; do · line 129

Quoted 10 lines
meta/recipes-core/initrdscripts/initramfs-framework/init +6 -0
+			continue
+		fi
+
+		if [ "$value_lstripped" != "$value" ]; then
+			# Start of a opt="word1 word2..." parameter
  			quoted=${opt}
+			value=${value_lstripped}
  			continue
  		fi
  		eval "bootparam_${opt}=\"${value}\""

Any objections to merging this fix?
Cheers
Michael.

Signature · 3 lines
Root Commit
Embedded Linux Training and Consulting
https://rootcommit.com