mwan3: "use" action: run process via `exec` and handle whitespace
authorLars Kruse <devel@sumpfralle.de>
Mon, 6 May 2024 10:09:56 +0000 (12:09 +0200)
committerRosen Penev <rosenp@gmail.com>
Sun, 23 Jun 2024 18:20:26 +0000 (11:20 -0700)
Previously the "use" command had the following shortcomings:
* a subprocess was created instead of replacing the shell process
* whitespace in arguments was not handled correctly

Implementation detail:
In shell context the `"$@"` expression should be used (instead of `$*`).
This allows the safe handling of arguments containing whitespace.

Closes: #20001
Signed-off-by: Lars Kruse <devel@sumpfralle.de>
net/mwan3/files/usr/sbin/mwan3

index ea3d4c6a7485ea1fd8d176d7757d9ad4ab592f39..e884d22f6be01846e7f982aad3e6093a893e0140 100755 (executable)
@@ -220,7 +220,6 @@ use() {
        # firewall rules
 
        local interface device src_ip family
-       mwan3_init
 
        interface=$1 ; shift
        [ -z "$*" ] && echo "no command specified for mwan3 use" && return
@@ -234,16 +233,17 @@ use() {
        [ -z "$family" ] && echo "could not find family for $interface. Using ipv4." && family='ipv4'
 
        echo "Running '$*' with DEVICE=$device SRCIP=$src_ip FWMARK=$MMX_DEFAULT FAMILY=$family"
-       # shellcheck disable=SC2048
-       FAMILY=$family DEVICE=$device SRCIP=$src_ip FWMARK=$MMX_DEFAULT LD_PRELOAD=/lib/mwan3/libwrap_mwan3_sockopt.so.1.0 $*
-
+       # if a program (not a shell builtin) is run: use "exec" for allowing direct process control
+       if [ -x "$(command -v "$1")" ]; then
+               set -- exec "$@"
+       fi
+       FAMILY=$family DEVICE=$device SRCIP=$src_ip FWMARK=$MMX_DEFAULT LD_PRELOAD=/lib/mwan3/libwrap_mwan3_sockopt.so.1.0 "$@"
 }
 
 case "$1" in
        ifup|ifdown|interfaces|policies|connected|rules|status|start|stop|restart|use|internal)
                mwan3_init
-               # shellcheck disable=SC2048
-               $*
+               "$@"
        ;;
        *)
                help