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