Attention is currently required from: Arne Schwabe.
Hello Arne Schwabe,
I'd like you to do a code review.
Please visit
https://gerrit.openvpn.net/c/openvpn/+/2341?usp=email
to review the following change.
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.
No functional change intended for valid values. For the resolvconf_file
and systemd scripts, the resulting resolv.conf and resolvectl arguments
were compared byte for byte before and after the change.
Signed-off-by: Deri Haerudi <[email protected]>
Change-Id: Id05f3e4e32aeb806d762858e754adc82f42d4371
---
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, 136 insertions(+), 8 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/41/2341/1
diff --git a/distro/dns-scripts/haikuos_file-dns-updown.sh
b/distro/dns-scripts/haikuos_file-dns-updown.sh
index e19b2b7..9ab4148 100644
--- a/distro/dns-scripts/haikuos_file-dns-updown.sh
+++ b/distro/dns-scripts/haikuos_file-dns-updown.sh
@@ -43,7 +43,32 @@
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" ] || return 0
+ 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_* | dev | script_type) ;;
+ *) 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 +100,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"
}
diff --git a/distro/dns-scripts/macos-dns-updown.sh
b/distro/dns-scripts/macos-dns-updown.sh
index fccc6b5..a3ee907 100644
--- a/distro/dns-scripts/macos-dns-updown.sh
+++ b/distro/dns-scripts/macos-dns-updown.sh
@@ -43,7 +43,32 @@
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" ] || return 0
+ 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_* | dev | script_type) ;;
+ *) 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..bf60538 100644
--- a/distro/dns-scripts/openresolv-dns-updown.sh
+++ b/distro/dns-scripts/openresolv-dns-updown.sh
@@ -42,7 +42,32 @@
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" ] || return 0
+ 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_* | dev | script_type) ;;
+ *) 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..d5305f1 100644
--- a/distro/dns-scripts/resolvconf_file-dns-updown.sh
+++ b/distro/dns-scripts/resolvconf_file-dns-updown.sh
@@ -43,7 +43,32 @@
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" ] || return 0
+ 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_* | dev | script_type) ;;
+ *) 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 +100,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"
}
diff --git a/distro/dns-scripts/systemd-dns-updown.sh
b/distro/dns-scripts/systemd-dns-updown.sh
index 553056b..1777b51 100644
--- a/distro/dns-scripts/systemd-dns-updown.sh
+++ b/distro/dns-scripts/systemd-dns-updown.sh
@@ -34,7 +34,32 @@
# 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" ] || return 0
+ 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_* | dev | script_type) ;;
+ *) 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 +252,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"
}
--
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: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id05f3e4e32aeb806d762858e754adc82f42d4371
Gerrit-Change-Number: 2341
Gerrit-PatchSet: 1
Gerrit-Owner: Deri Haerudi <[email protected]>
Gerrit-Reviewer: Arne Schwabe <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: Arne Schwabe <[email protected]>
_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel