]> git.ipfire.org Git - thirdparty/dracut.git/commitdiff
Specify strstr tightly, add strglob/strglobin.
authorChapman Flack <g2@anastigmatix.net>
Sat, 5 Apr 2014 01:11:38 +0000 (21:11 -0400)
committerHarald Hoyer <harald@redhat.com>
Mon, 7 Apr 2014 08:49:07 +0000 (10:49 +0200)
By convention, strstr should be a literal string match. Previously, it
would match as a glob pattern. Some code used that, so add new
functions strglob and strglobin to do what that code expects, and
specify them tightly too. strglob tests whether the glob pattern
matches the entire string (the name strglob is also used in the yorick
language, and that's what it does there), while strglobin tests whether
the glob pattern matches anywhere in the string.

Also tightens str_starts, str_ends, and str_replace to deal with
literal strings only. In a quick grep I did not find code that depended
on these functions matching globs.

Changes the call sites where strstr was used with glob patterns to use
strglobin or strglob as the intention seemed to be (or, in one case,
strstr with the * removed as it did not affect the result anyway).

20 files changed:
dracut-functions.sh
modules.d/40network/ifup.sh
modules.d/40network/net-lib.sh
modules.d/45ifcfg/write-ifcfg.sh
modules.d/80cms/cms-write-ifcfg.sh
modules.d/80cms/cmsifup.sh
modules.d/95nfs/nfs-lib.sh
modules.d/99base/dracut-lib.sh
test/TEST-01-BASIC/test-init.sh
test/TEST-02-SYSTEMD/test-init.sh
test/TEST-03-USR-MOUNT/test-init.sh
test/TEST-04-FULL-SYSTEMD/test-init.sh
test/TEST-10-RAID/test-init.sh
test/TEST-11-LVM/test-init.sh
test/TEST-12-RAID-DEG/test-init.sh
test/TEST-14-IMSM/test-init.sh
test/TEST-16-DMSQUASH/test-init.sh
test/TEST-17-LVM-THIN/test-init.sh
test/TEST-20-NFS/client-init.sh
test/TEST-50-MULTINIC/client-init.sh

index 391b549b27de1b6c689bc6fd670e833430e36c9b..c7580293587a17d8b8375495363323994e65802b 100755 (executable)
@@ -33,7 +33,11 @@ if [[ $initdir ]] && ! [[ -d $initdir ]]; then
 fi
 
 # Generic substring function.  If $2 is in $1, return 0.
-strstr() { [[ $1 = *$2* ]]; }
+strstr() { [[ $1 = *"$2"* ]]; }
+# Generic glob matching function. If glob pattern $2 matches anywhere in $1, OK
+strglobin() { [[ $1 = *$2* ]]; }
+# Generic glob matching function. If glob pattern $2 matches all of $1, OK
+strglob() { [[ $1 = $2 ]]; }
 
 # helper function for check() in module-setup.sh
 # to check for required installed binaries
index ede018832fc2820382b1fca6c00147432cbd47f2..7740be5a164567f112911decf3890bacad3f3b51 100755 (executable)
@@ -130,12 +130,12 @@ do_ipv6auto() {
 
 # Handle static ip configuration
 do_static() {
-    strstr $ip '*:*:*' && load_ipv6
+    strglobin $ip '*:*:*' && load_ipv6
 
     linkup $netif
     [ -n "$macaddr" ] && ip link set address $macaddr dev $netif
     [ -n "$mtu" ] && ip link set mtu $mtu dev $netif
-    if strstr $ip '*:*:*'; then
+    if strglobin $ip '*:*:*'; then
         # note no ip addr flush for ipv6
         ip addr add $ip/$mask ${srv:+peer $srv} dev $netif
         wait_for_ipv6_dad $netif
index d53e1a6ea9c52651954efe20b2dfd3fe0efd6183..a82f1a8d46d1b3fdfa80e6248fcf80b00a24db0d 100755 (executable)
@@ -377,7 +377,7 @@ ip_to_var() {
     # ip=<ipv4-address> means anaconda-style static config argument cluster:
     # ip=<ip> gateway=<gw> netmask=<nm> hostname=<host> mtu=<mtu>
     # ksdevice={link|bootif|ibft|<MAC>|<ifname>}
-    if strstr "$autoconf" "*.*.*.*"; then
+    if strglob "$autoconf" "*.*.*.*"; then
         ip="$autoconf"
         gw=$(getarg gateway=)
         mask=$(getarg netmask=)
index 5dc351d584a70c2e1474576957dea86bad15f560..5de2e901424d0f90bd64e3d131bc170bdf0a0d46 100755 (executable)
@@ -85,7 +85,7 @@ for netup in /tmp/net.*.did-setup ; do
 
     netif=${netup%%.did-setup}
     netif=${netif##*/net.}
-    strstr "$netif" ":*:*:*:*:" && continue
+    strglobin "$netif" ":*:*:*:*:" && continue
     [ -e /tmp/ifcfg/ifcfg-$netif ] && continue
     unset bridge
     unset bond
@@ -132,7 +132,7 @@ for netup in /tmp/net.*.did-setup ; do
         else
             # If we've booted with static ip= lines, the override file is there
             [ -e /tmp/net.$netif.override ] && . /tmp/net.$netif.override
-            if strstr "$ip" '*:*:*'; then
+            if strglobin "$ip" '*:*:*'; then
                 echo "IPV6INIT=yes"
                 echo "IPV6_AUTOCONF=no"
                 echo "IPV6ADDR=\"$ip/$mask\""
@@ -149,7 +149,7 @@ for netup in /tmp/net.*.did-setup ; do
                     fi
                 fi
             fi
-            if strstr "$gw" '*:*:*'; then
+            if strglobin "$gw" '*:*:*'; then
                 echo "IPV6_DEFAULTGW=\"$gw\""
             elif [ -n "$gw" ]; then
                 echo "GATEWAY=\"$gw\""
index c0623a41be39df8725cee5fc2e9f52515f96a8d6..924be1dbcca9d36687e8375fb4dd33d248f8fba8 100755 (executable)
@@ -26,7 +26,7 @@ function cms_write_config()
 
     IFCFGFILE=/run/initramfs/state/etc/sysconfig/network-scripts/ifcfg-$DEVICE
 
-    strstr "$IPADDR" '*:*:*' && ipv6=1
+    strglobin "$IPADDR" '*:*:*' && ipv6=1
 
 # to please NetworkManager on startup in loader before loader reconfigures net
     cat > /etc/sysconfig/network << EOF
index b3616504823f94342c933e20462eb29de955bca3..926340e20a570670f23bf59969fbde1ffe76fceb 100755 (executable)
@@ -8,7 +8,7 @@ DEVICE=$1
 
 . /tmp/cms.conf
 
-strstr "$IPADDR" '*:*:*' && ipv6=1
+strglobin "$IPADDR" '*:*:*' && ipv6=1
 
 if [ "$ipv6" ] && ! str_starts "$IPADDR" "["; then
     IPADDR="[$IPADDR]"
index 9ced2e69f927e8fe83ffd18a4fe95f4cb479f8df..d5377fe5b7d86e86210fadaaff9e6579c2ec4018 100755 (executable)
@@ -40,7 +40,7 @@ nfsroot_to_var() {
     arg="${arg##$nfs:}"
 
     # check if we have a server
-    if strstr "$arg" ':/*' ; then
+    if strstr "$arg" ':/' ; then
         server="${arg%%:/*}"
         arg="/${arg##*:/}"
     fi
index e4d7da8edbe77ff37906b6712e3ac729e9f5e1de..127287cca56040d361252c5cd7b170f26b249ed2 100755 (executable)
@@ -20,19 +20,33 @@ debug_on() {
     [ "$RD_DEBUG" = "yes" ] && set -x
 }
 
-# returns OK if $1 contains $2
+# returns OK if $1 contains literal string $2 (and isn't empty)
 strstr() {
-    [ "${1#*$2*}" != "$1" ]
+    [ "${1##*"$2"*}" != "$1" ]
 }
 
-# returns OK if $1 contains $2 at the beginning
+# returns OK if $1 matches (completely) glob pattern $2
+# An empty $1 will not be considered matched, even if $2 is * which technically
+# matches; as it would match anything, it's not an interesting case.
+strglob() {
+    [ -n "$1" -a -z "${1##$2}" ]
+}
+
+# returns OK if $1 contains (anywhere) a match of glob pattern $2
+# An empty $1 will not be considered matched, even if $2 is * which technically
+# matches; as it would match anything, it's not an interesting case.
+strglobin() {
+    [ -n "$1" -a -z "${1##*$2*}" ]
+}
+
+# returns OK if $1 contains literal string $2 at the beginning, and isn't empty
 str_starts() {
-    [ "${1#$2*}" != "$1" ]
+    [ "${1#"$2"*}" != "$1" ]
 }
 
-# returns OK if $1 contains $2 at the end
+# returns OK if $1 contains literal string $2 at the end, and isn't empty
 str_ends() {
-    [ "${1%*$2}" != "$1" ]
+    [ "${1%*"$2"}" != "$1" ]
 }
 
 if [ -z "$DRACUT_SYSTEMD" ]; then
@@ -85,9 +99,9 @@ str_replace() {
     local out=''
 
     while strstr "${in}" "$s"; do
-        chop="${in%%$s*}"
+        chop="${in%%"$s"*}"
         out="${out}${chop}$r"
-        in="${in#*$s}"
+        in="${in#*"$s"}"
     done
     echo "${out}${in}"
 }
@@ -555,7 +569,7 @@ nfsroot_to_var() {
     arg="${arg##$nfs:}"
 
     # check if we have a server
-    if strstr "$arg" ':/*' ; then
+    if strstr "$arg" ':/' ; then
         server="${arg%%:/*}"
         arg="/${arg##*:/}"
     fi
index 5fc02d94614fecb8bbda185b60ae33e0dafc05fc..6e3a31ab59eca17472a38198a933cbd8ffb5da0e 100755 (executable)
@@ -1,7 +1,7 @@
 #!/bin/sh
 >/dev/watchdog
 export PATH=/sbin:/bin:/usr/sbin:/usr/bin
-strstr() { [ "${1#*$2*}" != "$1" ]; }
+strstr() { [ "${1#*"$2"*}" != "$1" ]; }
 CMDLINE=$(while read line; do echo $line;done < /proc/cmdline)
 plymouth --quit
 exec >/dev/console 2>&1
index ff17b6b4e787dfe0aa628f646a4653fe4d201fd3..43d7a21be2ec2477fa688ad3afc6f2b393cdd0d4 100755 (executable)
@@ -1,6 +1,6 @@
 #!/bin/sh
 export PATH=/sbin:/bin:/usr/sbin:/usr/bin
-strstr() { [ "${1#*$2*}" != "$1" ]; }
+strstr() { [ "${1#*"$2"*}" != "$1" ]; }
 CMDLINE=$(while read line; do echo $line;done < /proc/cmdline)
 plymouth --quit
 exec </dev/console >/dev/console 2>&1
index 63520ab6c32450be8b7160acccfc955411a84106..e4f37793ba54eee0ce803f2a393c9fa98a84ce88 100755 (executable)
@@ -1,7 +1,7 @@
 #!/bin/sh
 >/dev/watchdog
 export PATH=/sbin:/bin:/usr/sbin:/usr/bin
-strstr() { [ "${1#*$2*}" != "$1" ]; }
+strstr() { [ "${1#*"$2"*}" != "$1" ]; }
 CMDLINE=$(while read line; do echo $line;done < /proc/cmdline)
 plymouth --quit
 exec </dev/console >/dev/console 2>&1
index dcda926af3441c08417807244762e6adb317fd4b..c2ed7b974cc326342847b18449fa7e877af2fed2 100755 (executable)
@@ -1,7 +1,7 @@
 #!/bin/sh
 >/dev/watchdog
 export PATH=/sbin:/bin:/usr/sbin:/usr/bin
-strstr() { [ "${1#*$2*}" != "$1" ]; }
+strstr() { [ "${1#*"$2"*}" != "$1" ]; }
 CMDLINE=$(while read line; do echo $line;done < /proc/cmdline)
 plymouth --quit
 exec </dev/console >/dev/console 2>&1
index 62afcee003088b36c08183d6683168bf32ca1866..c64523f65b177766d39e6e83c1e8b363b80bed75 100755 (executable)
@@ -1,6 +1,6 @@
 #!/bin/sh
 export PATH=/sbin:/bin:/usr/sbin:/usr/bin
-strstr() { [ "${1#*$2*}" != "$1" ]; }
+strstr() { [ "${1#*"$2"*}" != "$1" ]; }
 CMDLINE=$(while read line; do echo $line;done < /proc/cmdline)
 command -v plymouth >/dev/null && plymouth --quit
 exec >/dev/console 2>&1
index fd03aa5cf1706993bbf17b58927a3448bcf21528..2afeeb9164ed744619a0ee9bd67fc7d63ff8dc41 100755 (executable)
@@ -1,6 +1,6 @@
 #!/bin/sh
 export PATH=/sbin:/bin:/usr/sbin:/usr/bin
-strstr() { [ "${1#*$2*}" != "$1" ]; }
+strstr() { [ "${1#*"$2"*}" != "$1" ]; }
 CMDLINE=$(while read line; do echo $line;done < /proc/cmdline)
 plymouth --quit
 exec >/dev/console 2>&1
index 62afcee003088b36c08183d6683168bf32ca1866..c64523f65b177766d39e6e83c1e8b363b80bed75 100755 (executable)
@@ -1,6 +1,6 @@
 #!/bin/sh
 export PATH=/sbin:/bin:/usr/sbin:/usr/bin
-strstr() { [ "${1#*$2*}" != "$1" ]; }
+strstr() { [ "${1#*"$2"*}" != "$1" ]; }
 CMDLINE=$(while read line; do echo $line;done < /proc/cmdline)
 command -v plymouth >/dev/null && plymouth --quit
 exec >/dev/console 2>&1
index f434f132c4c076b8bf95e39d8edf1e2669fd04ec..616b73c3f5fe28713b3c91ca5cb570c7bfa70e30 100755 (executable)
@@ -1,6 +1,6 @@
 #!/bin/sh
 export PATH=/sbin:/bin:/usr/sbin:/usr/bin
-strstr() { [ "${1#*$2*}" != "$1" ]; }
+strstr() { [ "${1#*"$2"*}" != "$1" ]; }
 CMDLINE=$(while read line; do echo $line;done < /proc/cmdline)
 plymouth --quit
 exec >/dev/console 2>&1
index 616bf688a4c6bae6728971f4722fc882b53d8f93..e8c8e70e3383f21d2ea455a0a9094a374e84dc37 100755 (executable)
@@ -1,6 +1,6 @@
 #!/bin/sh
 export PATH=/sbin:/bin:/usr/sbin:/usr/bin
-strstr() { [ "${1#*$2*}" != "$1" ]; }
+strstr() { [ "${1#*"$2"*}" != "$1" ]; }
 CMDLINE=$(while read line; do echo $line;done < /proc/cmdline)
 plymouth --quit
 exec >/dev/console 2>&1
index fd03aa5cf1706993bbf17b58927a3448bcf21528..2afeeb9164ed744619a0ee9bd67fc7d63ff8dc41 100755 (executable)
@@ -1,6 +1,6 @@
 #!/bin/sh
 export PATH=/sbin:/bin:/usr/sbin:/usr/bin
-strstr() { [ "${1#*$2*}" != "$1" ]; }
+strstr() { [ "${1#*"$2"*}" != "$1" ]; }
 CMDLINE=$(while read line; do echo $line;done < /proc/cmdline)
 plymouth --quit
 exec >/dev/console 2>&1
index ad6a656c9bff76c8234ff338cdb654490e784e14..f6bbdd92ffed9ce18b81e5636bb16457fba81c4e 100755 (executable)
@@ -4,7 +4,7 @@ exec >/dev/console 2>&1
 export TERM=linux
 export PS1='initramfs-test:\w\$ '
 CMDLINE=$(while read line; do echo $line;done < /proc/cmdline)
-strstr() { [ "${1#*$2*}" != "$1" ]; }
+strstr() { [ "${1#*"$2"*}" != "$1" ]; }
 
 stty sane
 strstr "$CMDLINE" "rd.shell" && sh -i
index 51f8b6422826bbc6c7cc181a4ca561146f107e08..932096357276591306a2d7c4194d1849f2347343 100755 (executable)
@@ -2,14 +2,15 @@
 exec >/dev/console 2>&1
 set -x
 export PATH=/sbin:/bin:/usr/sbin:/usr/bin
-strstr() { [ "${1#*$2*}" != "$1" ]; }
+strstr() { [ "${1#*"$2"*}" != "$1" ]; }
+strglobin() { [ -n "$1" -a -z "${1#*$2*}" ]; }
 CMDLINE=$(while read line; do echo $line;done < /proc/cmdline)
 export TERM=linux
 export PS1='initramfs-test:\w\$ '
 stty sane
 echo "made it to the rootfs! Powering down."
 for i in /run/initramfs/net.*.did-setup; do
-       strstr "$i" ":*:*:*:*:" && continue
+       strglobin "$i" ":*:*:*:*:" && continue
        i=${i%.did-setup}
        IFACES+="${i##*/net.} "
 done