Attention is currently required from: Arne Schwabe, Deri Haerudi, Răzvan 
Cojocaru.

Hello Arne Schwabe, Răzvan Cojocaru,

I'd like you to reexamine a change. Please visit

    https://gerrit.openvpn.net/c/openvpn/+/2341?usp=email

to look at the new patch set (#3).

The following approvals got outdated and were removed:
Code-Review-1 by Răzvan Cojocaru


Change subject: dns-scripts: parse dns_vars_file as data instead of sourcing it
......................................................................

dns-scripts: parse dns_vars_file as data instead of sourcing it

When --user is used, openvpn forks a privileged runner that executes the
dns-updown script as root. The unprivileged process writes the DNS
variables to a temporary file (env_set_write_file(), raw NAME=value lines)
and passes its path to the runner, and all in-tree dns-updown scripts then
load that file with ". $dns_vars_file", i.e. they execute it as shell code.

Today the values are restricted by validate_domain() and friends, so this
is not exploitable from the network. But the design means (a) the safety
of root code execution rests entirely on that validation, and (b) the
privilege separation the runner exists for does not hold: whoever controls
the unprivileged process controls a file root will execute.

Replace the sourcing with a small reader that only accepts the variable
names openvpn writes (dns_server_*, dns_search_domain_*, dev, script_type),
validates the name and assigns the value without evaluating it. Also stop
re-evaluating already-collected search domains in the "domains" loop of
the resolvconf_file, haikuos_file and systemd scripts.

dev is also pasted into a sed program by the dns-down code of the
resolvconf_file, haikuos_file and systemd scripts. Those sed calls now
use \%regex% addresses, and the reader rejects a dev value containing
'%' or '\', so dev cannot end the address and is only ever matched as a
regular expression. The reader also rejects a dev starting with '-'
(it is passed as an argument to resolvconf and resolvectl) or containing
".." (resolvconf names a file after it). Any other interface name is
accepted, including '/' (Haiku names its devices tun/N).

If the file cannot be read, the reader now prints an error and exits
instead of letting the script carry on with nothing set. With ". file"
the /bin/sh scripts stopped there as well.

This addresses (b) only as far as the scripts are concerned: root no
longer executes the file, and dev can no longer inject sed commands. It
does not make the file trusted input. The C side still trusts the path
it receives from the unprivileged process, and the values are still
handed to resolv.conf, resolvectl, resolvconf and scutil as DNS
configuration, as before.

Valid values give the same result as before, except for the "search"
line that the resolvconf_file, haikuos_file and systemd scripts write to
resolv.conf: the domains are now separated by single blanks with no
trailing blank (the old loop left one or two), and the list stops at the
first empty dns_search_domain_N. openvpn numbers the search domains
without gaps, so the latter only matters for hand-written files. The
rest of resolv.conf and the resolvectl and resolvconf arguments were
compared before and after the change and are identical.

Change-Id: Id05f3e4e32aeb806d762858e754adc82f42d4371
Signed-off-by: Deri Haerudi <[email protected]>
---
M distro/dns-scripts/haikuos_file-dns-updown.sh
M distro/dns-scripts/macos-dns-updown.sh
M distro/dns-scripts/openresolv-dns-updown.sh
M distro/dns-scripts/resolvconf_file-dns-updown.sh
M distro/dns-scripts/systemd-dns-updown.sh
5 files changed, 207 insertions(+), 11 deletions(-)


  git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/41/2341/3

diff --git a/distro/dns-scripts/haikuos_file-dns-updown.sh 
b/distro/dns-scripts/haikuos_file-dns-updown.sh
index e19b2b7..7ca270b 100644
--- a/distro/dns-scripts/haikuos_file-dns-updown.sh
+++ b/distro/dns-scripts/haikuos_file-dns-updown.sh
@@ -43,7 +43,45 @@

 conf=/boot/system/settings/network/resolv.conf
 test -e "$conf" || exit 1
-test -z "${dns_vars_file}" || . "${dns_vars_file}"
+# Read the variables that openvpn wrote to $dns_vars_file as data.
+# The file is deliberately NOT sourced: values may originate from options
+# pushed by the server, and the file is written by the unprivileged
+# openvpn process, so sourcing it would execute shell syntax contained
+# in a value (command substitution, quotes, redirections).
+dns_vars_load() {
+    _f=$1
+    [ -r "$_f" ] || {
+        echo "cannot read dns vars file ${_f}"
+        exit 1
+    }
+    while IFS= read -r _line || [ -n "$_line" ]; do
+        case $_line in
+            *=*) ;;
+            *) continue ;;
+        esac
+        _name=${_line%%=*}
+        _value=${_line#*=}
+        case $_name in
+            dns_server_* | dns_search_domain_* | script_type) ;;
+            dev)
+                # Reject what could end a \%re% sed address (% and \),
+                # pass as a command option (leading -) or walk up a path (..)
+                case $_value in
+                    -* | *%* | *\\* | *..*)
+                        echo "invalid dev name in ${_f}"
+                        exit 1
+                        ;;
+                esac
+                ;;
+            *) continue ;;
+        esac
+        case $_name in
+            *[!A-Za-z0-9_]*) continue ;;
+        esac
+        eval "$_name=\"\$_value\""
+    done < "$_f"
+}
+test -z "${dns_vars_file}" || dns_vars_load "${dns_vars_file}"
 case "${script_type}" in
 dns-up)
     n=1
@@ -75,7 +113,9 @@

     test -z "$dns_search_domain_1" || {
         for i in $(seq 1 6); do
-            eval domains=\"$domains\$dns_search_domain_${i} \" || break
+            eval d=\"\$dns_search_domain_${i}\"
+            [ -n "$d" ] || break
+            domains="${domains:+$domains }$d"
         done
         text="${text}search $domains\n"
     }
@@ -85,6 +125,6 @@
     echo "${text}" > "${conf}"
     ;;
 dns-down)
-    sed -i'' -e "/### openvpn ${dev} begin ###/,/### openvpn ${dev} end ###/d" 
"$conf"
+    sed -i'' -e "\%### openvpn ${dev} begin ###%,\%### openvpn ${dev} end 
###%d" "$conf"
     ;;
 esac
diff --git a/distro/dns-scripts/macos-dns-updown.sh 
b/distro/dns-scripts/macos-dns-updown.sh
index fccc6b5..38f91d2 100644
--- a/distro/dns-scripts/macos-dns-updown.sh
+++ b/distro/dns-scripts/macos-dns-updown.sh
@@ -43,7 +43,45 @@
 done
 trap "/bin/rm -f ${lockfile}" EXIT

-[ -z "${dns_vars_file}" ] || . "${dns_vars_file}"
+# Read the variables that openvpn wrote to $dns_vars_file as data.
+# The file is deliberately NOT sourced: values may originate from options
+# pushed by the server, and the file is written by the unprivileged
+# openvpn process, so sourcing it would execute shell syntax contained
+# in a value (command substitution, quotes, redirections).
+dns_vars_load() {
+    _f=$1
+    [ -r "$_f" ] || {
+        echo "cannot read dns vars file ${_f}"
+        exit 1
+    }
+    while IFS= read -r _line || [ -n "$_line" ]; do
+        case $_line in
+            *=*) ;;
+            *) continue ;;
+        esac
+        _name=${_line%%=*}
+        _value=${_line#*=}
+        case $_name in
+            dns_server_* | dns_search_domain_* | script_type) ;;
+            dev)
+                # Reject what could end a \%re% sed address (% and \),
+                # pass as a command option (leading -) or walk up a path (..)
+                case $_value in
+                    -* | *%* | *\\* | *..*)
+                        echo "invalid dev name in ${_f}"
+                        exit 1
+                        ;;
+                esac
+                ;;
+            *) continue ;;
+        esac
+        case $_name in
+            *[!A-Za-z0-9_]*) continue ;;
+        esac
+        eval "$_name=\"\$_value\""
+    done < "$_f"
+}
+[ -z "${dns_vars_file}" ] || dns_vars_load "${dns_vars_file}"

 itf_dns_key="State:/Network/Service/openvpn-${dev}/DNS"

diff --git a/distro/dns-scripts/openresolv-dns-updown.sh 
b/distro/dns-scripts/openresolv-dns-updown.sh
index 4aca99f..ee26480 100644
--- a/distro/dns-scripts/openresolv-dns-updown.sh
+++ b/distro/dns-scripts/openresolv-dns-updown.sh
@@ -42,7 +42,45 @@
     done
 }

-[ -z "${dns_vars_file}" ] || . "${dns_vars_file}"
+# Read the variables that openvpn wrote to $dns_vars_file as data.
+# The file is deliberately NOT sourced: values may originate from options
+# pushed by the server, and the file is written by the unprivileged
+# openvpn process, so sourcing it would execute shell syntax contained
+# in a value (command substitution, quotes, redirections).
+dns_vars_load() {
+    _f=$1
+    [ -r "$_f" ] || {
+        echo "cannot read dns vars file ${_f}"
+        exit 1
+    }
+    while IFS= read -r _line || [ -n "$_line" ]; do
+        case $_line in
+            *=*) ;;
+            *) continue ;;
+        esac
+        _name=${_line%%=*}
+        _value=${_line#*=}
+        case $_name in
+            dns_server_* | dns_search_domain_* | script_type) ;;
+            dev)
+                # Reject what could end a \%re% sed address (% and \),
+                # pass as a command option (leading -) or walk up a path (..)
+                case $_value in
+                    -* | *%* | *\\* | *..*)
+                        echo "invalid dev name in ${_f}"
+                        exit 1
+                        ;;
+                esac
+                ;;
+            *) continue ;;
+        esac
+        case $_name in
+            *[!A-Za-z0-9_]*) continue ;;
+        esac
+        eval "$_name=\"\$_value\""
+    done < "$_f"
+}
+[ -z "${dns_vars_file}" ] || dns_vars_load "${dns_vars_file}"
 : ${script_type:=dns-down}
 case "${script_type}" in
 dns-up)
diff --git a/distro/dns-scripts/resolvconf_file-dns-updown.sh 
b/distro/dns-scripts/resolvconf_file-dns-updown.sh
index 8d23ed6..2686b79 100644
--- a/distro/dns-scripts/resolvconf_file-dns-updown.sh
+++ b/distro/dns-scripts/resolvconf_file-dns-updown.sh
@@ -43,7 +43,45 @@

 conf=/etc/resolv.conf
 test -e "$conf" || exit 1
-test -z "${dns_vars_file}" || . "${dns_vars_file}"
+# Read the variables that openvpn wrote to $dns_vars_file as data.
+# The file is deliberately NOT sourced: values may originate from options
+# pushed by the server, and the file is written by the unprivileged
+# openvpn process, so sourcing it would execute shell syntax contained
+# in a value (command substitution, quotes, redirections).
+dns_vars_load() {
+    _f=$1
+    [ -r "$_f" ] || {
+        echo "cannot read dns vars file ${_f}"
+        exit 1
+    }
+    while IFS= read -r _line || [ -n "$_line" ]; do
+        case $_line in
+            *=*) ;;
+            *) continue ;;
+        esac
+        _name=${_line%%=*}
+        _value=${_line#*=}
+        case $_name in
+            dns_server_* | dns_search_domain_* | script_type) ;;
+            dev)
+                # Reject what could end a \%re% sed address (% and \),
+                # pass as a command option (leading -) or walk up a path (..)
+                case $_value in
+                    -* | *%* | *\\* | *..*)
+                        echo "invalid dev name in ${_f}"
+                        exit 1
+                        ;;
+                esac
+                ;;
+            *) continue ;;
+        esac
+        case $_name in
+            *[!A-Za-z0-9_]*) continue ;;
+        esac
+        eval "$_name=\"\$_value\""
+    done < "$_f"
+}
+test -z "${dns_vars_file}" || dns_vars_load "${dns_vars_file}"
 case "${script_type}" in
 dns-up)
     n=1
@@ -75,7 +113,9 @@

     test -z "$dns_search_domain_1" || {
         for i in $(seq 1 6); do
-            eval domains=\"$domains\$dns_search_domain_${i} \" || break
+            eval d=\"\$dns_search_domain_${i}\"
+            [ -n "$d" ] || break
+            domains="${domains:+$domains }$d"
         done
         text="${text}search $domains\n"
     }
@@ -85,6 +125,6 @@
     echo "${text}" > "${conf}"
     ;;
 dns-down)
-    sed -i'' -e "/### openvpn ${dev} begin ###/,/### openvpn ${dev} end ###/d" 
"$conf"
+    sed -i'' -e "\%### openvpn ${dev} begin ###%,\%### openvpn ${dev} end 
###%d" "$conf"
     ;;
 esac
diff --git a/distro/dns-scripts/systemd-dns-updown.sh 
b/distro/dns-scripts/systemd-dns-updown.sh
index 553056b..bfc1a1e 100644
--- a/distro/dns-scripts/systemd-dns-updown.sh
+++ b/distro/dns-scripts/systemd-dns-updown.sh
@@ -34,7 +34,45 @@
 #   dns_server_1_sni dns.mycorp.in
 #

-[ -z "${dns_vars_file}" ] || . "${dns_vars_file}"
+# Read the variables that openvpn wrote to $dns_vars_file as data.
+# The file is deliberately NOT sourced: values may originate from options
+# pushed by the server, and the file is written by the unprivileged
+# openvpn process, so sourcing it would execute shell syntax contained
+# in a value (command substitution, quotes, redirections).
+dns_vars_load() {
+    _f=$1
+    [ -r "$_f" ] || {
+        echo "cannot read dns vars file ${_f}"
+        exit 1
+    }
+    while IFS= read -r _line || [ -n "$_line" ]; do
+        case $_line in
+            *=*) ;;
+            *) continue ;;
+        esac
+        _name=${_line%%=*}
+        _value=${_line#*=}
+        case $_name in
+            dns_server_* | dns_search_domain_* | script_type) ;;
+            dev)
+                # Reject what could end a \%re% sed address (% and \),
+                # pass as a command option (leading -) or walk up a path (..)
+                case $_value in
+                    -* | *%* | *\\* | *..*)
+                        echo "invalid dev name in ${_f}"
+                        exit 1
+                        ;;
+                esac
+                ;;
+            *) continue ;;
+        esac
+        case $_name in
+            *[!A-Za-z0-9_]*) continue ;;
+        esac
+        eval "$_name=\"\$_value\""
+    done < "$_f"
+}
+[ -z "${dns_vars_file}" ] || dns_vars_load "${dns_vars_file}"

 function do_resolved_servers {
     local sni=""
@@ -227,7 +265,9 @@

         test -z "$dns_search_domain_1" || {
             for i in $(seq 1 6); do
-                eval domains=\"$domains\$dns_search_domain_${i} \" || break
+                eval d=\"\$dns_search_domain_${i}\"
+                [ -n "$d" ] || break
+                domains="${domains:+$domains }$d"
             done
             text="${text}search $domains\n"
         }
@@ -236,7 +276,7 @@
         sed -i "1i${text}" "$conf"
     else
         echo "unsetting DNS using resolv.conf file"
-        sed -i "/### openvpn ${dev} begin ###/,/### openvpn ${dev} end ###/d" 
"$conf"
+        sed -i "\%### openvpn ${dev} begin ###%,\%### openvpn ${dev} end 
###%d" "$conf"
     fi

     return 0

--
To view, visit https://gerrit.openvpn.net/c/openvpn/+/2341?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id05f3e4e32aeb806d762858e754adc82f42d4371
Gerrit-Change-Number: 2341
Gerrit-PatchSet: 3
Gerrit-Owner: Deri Haerudi <[email protected]>
Gerrit-Reviewer: Arne Schwabe <[email protected]>
Gerrit-Reviewer: Răzvan Cojocaru <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: Deri Haerudi <[email protected]>
Gerrit-Attention: Răzvan Cojocaru <[email protected]>
Gerrit-Attention: Arne Schwabe <[email protected]>
_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

Reply via email to