0738a69
From 8e1a4dc5f8a777fc718db490414ffdc9dc755f66 Mon Sep 17 00:00:00 2001
0738a69
From: Jonas Witschel <diabonas@archlinux.org>
0738a69
Date: Sat, 18 Apr 2020 14:55:41 +0200
0738a69
Subject: [PATCH] dracut-lib.sh: quote variables in parameter expansion
0738a69
 patterns
0738a69
0738a69
According to POSIX.1-2017, 2.6.2 Parameter Expansion:
0738a69
0738a69
${parameter%[word]} [...] The word shall be expanded to produce a
0738a69
pattern.
0738a69
0738a69
This means if word contains variables that itself contain special
0738a69
characters like asterisks or backslashes, these are treated as pattern
0738a69
characters unless the variable is quoted. Try e.g. the following example
0738a69
in bash, dash or (busybox) ash:
0738a69
0738a69
i='a\c'; j='\'; echo "${i%$j*}"
0738a69
0738a69
This prints "a\c" because "$j*" is expanded to "\*", escaping the
0738a69
asterisk. In contrast,
0738a69
0738a69
i='a\c'; j='\'; echo "${i%"$j"*}"
0738a69
0738a69
produces the expected result "a" because the backslash is not specially
0738a69
treated any more after quoting.
0738a69
0738a69
The quotes that this commit adds have been previously removed in commit
0738a69
f9c96cf56fed390841eac05c43826e62014c9188, citing issues with busybox
0738a69
hush without further specifying the actual error. I tested a recent
0738a69
busybox build (upstream commit 9aa751b08ab03d6396f86c3df77937a19687981b)
0738a69
and couldn't find any problems. Note that the above example always
0738a69
produces "a\c" in hush regardless of quoting $j, making hush unsuitable
0738a69
for use with dracut, but using quotes in parameter expansions generally
0738a69
works.
0738a69
0738a69
The unquoted variables break the "rd.luks.uuid/name" kernel command line
0738a69
options in dracut 050 because
0738a69
0738a69
str_replace "$luksname" '\' '\\'
0738a69
0738a69
in modules.d/90crypt/parse-crypt.sh is not able to escape the
0738a69
backslashes any more, see GH-723, GH-727: backslashes in the
0738a69
systemd-cryptsetup@.service unit name stay unescaped for use in udev
0738a69
(cf. commit 0f6d93eb9d63695a64002ec8b0421fbc9fc8a7a3), leading to
0738a69
failures in starting the unit.
0738a69
0738a69
This partially reverts commit f9c96cf56fed390841eac05c43826e62014c9188.
0738a69
---
0738a69
 modules.d/99base/dracut-lib.sh | 16 ++++++++--------
0738a69
 1 file changed, 8 insertions(+), 8 deletions(-)
0738a69
0738a69
diff --git a/modules.d/99base/dracut-lib.sh b/modules.d/99base/dracut-lib.sh
0738a69
index c53cd13b..c57523d3 100755
0738a69
--- a/modules.d/99base/dracut-lib.sh
0738a69
+++ b/modules.d/99base/dracut-lib.sh
0738a69
@@ -24,7 +24,7 @@ debug_on() {
0738a69
 
0738a69
 # returns OK if $1 contains literal string $2 (and isn't empty)
0738a69
 strstr() {
0738a69
-    [ "${1##*$2*}" != "$1" ]
0738a69
+    [ "${1##*"$2"*}" != "$1" ]
0738a69
 }
0738a69
 
0738a69
 # returns OK if $1 matches (completely) glob pattern $2
0738a69
@@ -43,18 +43,18 @@ strglobin() {
0738a69
 
0738a69
 # returns OK if $1 contains literal string $2 at the beginning, and isn't empty
0738a69
 str_starts() {
0738a69
-    [ "${1#$2*}" != "$1" ]
0738a69
+    [ "${1#"$2"*}" != "$1" ]
0738a69
 }
0738a69
 
0738a69
 # returns OK if $1 contains literal string $2 at the end, and isn't empty
0738a69
 str_ends() {
0738a69
-    [ "${1%*$2}" != "$1" ]
0738a69
+    [ "${1%*"$2"}" != "$1" ]
0738a69
 }
0738a69
 
0738a69
 trim() {
0738a69
     local var="$*"
0738a69
-    var="${var#${var%%[![:space:]]*}}"   # remove leading whitespace characters
0738a69
-    var="${var%${var##*[![:space:]]}}"   # remove trailing whitespace characters
0738a69
+    var="${var#"${var%%[![:space:]]*}"}"   # remove leading whitespace characters
0738a69
+    var="${var%"${var##*[![:space:]]}"}"   # remove trailing whitespace characters
0738a69
     printf "%s" "$var"
0738a69
 }
0738a69
 
0738a69
@@ -108,9 +108,9 @@ str_replace() {
0738a69
     local out=''
0738a69
 
0738a69
     while strstr "${in}" "$s"; do
0738a69
-        chop="${in%%$s*}"
0738a69
+        chop="${in%%"$s"*}"
0738a69
         out="${out}${chop}$r"
0738a69
-        in="${in#*$s}"
0738a69
+        in="${in#*"$s"}"
0738a69
     done
0738a69
     echo "${out}${in}"
0738a69
 }
0738a69
@@ -396,7 +396,7 @@ splitsep() {
0738a69
     while [ -n "$str" -a "$#" -gt 1 ]; do
0738a69
         tmp="${str%%$sep*}"
0738a69
         eval "$1='${tmp}'"
0738a69
-        str="${str#$tmp}"
0738a69
+        str="${str#"$tmp"}"
0738a69
         str="${str#$sep}"
0738a69
         shift
0738a69
     done
0738a69