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 (#2).

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. The reader therefore
only accepts a dev value that starts with a letter or digit, consists of
letters, digits, '_', '.', '-' and '/' ('/' because Haiku names its
devices tun/N) and does not contain "..". Those sed calls now use
\%regex% addresses, so a '/' in dev cannot end the address either.

If the file cannot be read, the reader now exits with an error 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.

No functional change intended for valid values. For the resolvconf_file
and systemd scripts, the resulting resolv.conf and resolvectl arguments
were compared before and after the change. They are identical except
for the number of trailing blanks on the "search" line when fewer than
six search domains are pushed, which the old loop collapsed through word
splitting.

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, 189 insertions(+), 11 deletions(-)


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

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

 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" ] || 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)
+                # dev is used in sed expressions and as a command argument:
+                # only accept interface name characters ("/" for Haiku tun/N)
+                case $_value in
+                    [!A-Za-z0-9]* | *[!A-Za-z0-9_./-]* | *..*)
+                        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 +110,8 @@
 
     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}\" || break
+            domains="$domains$d "
         done
         text="${text}search $domains\n"
     }
@@ -85,6 +121,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..713ac1b 100644
--- a/distro/dns-scripts/macos-dns-updown.sh
+++ b/distro/dns-scripts/macos-dns-updown.sh
@@ -43,7 +43,42 @@
 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" ] || 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)
+                # dev is used in sed expressions and as a command argument:
+                # only accept interface name characters ("/" for Haiku tun/N)
+                case $_value in
+                    [!A-Za-z0-9]* | *[!A-Za-z0-9_./-]* | *..*)
+                        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..299440b 100644
--- a/distro/dns-scripts/openresolv-dns-updown.sh
+++ b/distro/dns-scripts/openresolv-dns-updown.sh
@@ -42,7 +42,42 @@
     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" ] || 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)
+                # dev is used in sed expressions and as a command argument:
+                # only accept interface name characters ("/" for Haiku tun/N)
+                case $_value in
+                    [!A-Za-z0-9]* | *[!A-Za-z0-9_./-]* | *..*)
+                        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..f41fe27 100644
--- a/distro/dns-scripts/resolvconf_file-dns-updown.sh
+++ b/distro/dns-scripts/resolvconf_file-dns-updown.sh
@@ -43,7 +43,42 @@

 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" ] || 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)
+                # dev is used in sed expressions and as a command argument:
+                # only accept interface name characters ("/" for Haiku tun/N)
+                case $_value in
+                    [!A-Za-z0-9]* | *[!A-Za-z0-9_./-]* | *..*)
+                        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 +110,8 @@

     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}\" || break
+            domains="$domains$d "
         done
         text="${text}search $domains\n"
     }
@@ -85,6 +121,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..cb02b89 100644
--- a/distro/dns-scripts/systemd-dns-updown.sh
+++ b/distro/dns-scripts/systemd-dns-updown.sh
@@ -34,7 +34,42 @@
 #   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" ] || 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)
+                # dev is used in sed expressions and as a command argument:
+                # only accept interface name characters ("/" for Haiku tun/N)
+                case $_value in
+                    [!A-Za-z0-9]* | *[!A-Za-z0-9_./-]* | *..*)
+                        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 +262,8 @@

         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}\" || break
+                domains="$domains$d "
             done
             text="${text}search $domains\n"
         }
@@ -236,7 +272,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: 2
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