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

Hello Arne Schwabe, Deri Haerudi, Răzvan Cojocaru,

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

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

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

The following approvals got outdated and were removed:
Code-Review+1 by Deri Haerudi, Code-Review+2 by Răzvan Cojocaru

The change is no longer submittable: Code-Review and checks~ChecksSubmitRule 
are unsatisfied now.


Change subject: dns: restrict dns var names and values in the updown runner
......................................................................

dns: restrict dns var names and values in the updown runner

The updown runner hands the vars it gets from the unprivileged process
to a root script. Only accept what openvpn itself would send, so that a
compromised process cannot affect the script beyond its DNS settings.

Change-Id: I5847d602a024044d9109f521de7885d55d28a968
Signed-off-by: Heiko Hund <[email protected]>
---
M src/openvpn/dns.c
1 file changed, 149 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/49/2349/3

diff --git a/src/openvpn/dns.c b/src/openvpn/dns.c
index 1bd6c85..d00165a1 100644
--- a/src/openvpn/dns.c
+++ b/src/openvpn/dns.c
@@ -591,10 +591,155 @@
 #define DNS_VARS_MAX_SIZE (64 * 1024)

 /**
+ * Check if a dns var name equals a given name
+ *
+ * @param  name     variable name, not NUL terminated
+ * @param  size     size of the name in bytes
+ * @param  expected NUL terminated name to compare with
+ *
+ * @return bool to indicate if the names are equal
+ */
+static bool
+dns_var_name_is(const char *name, size_t size, const char *expected)
+{
+    return size == strlen(expected) && memcmp(name, expected, size) == 0;
+}
+
+/**
+ * Check if a dns var name is allowed
+ *
+ * Besides dev and script_type, only names starting with dns_ and made of
+ * lowercase letters, digits and underscores are allowed.
+ *
+ * @param  name     variable name, not NUL terminated
+ * @param  size     size of the name in bytes
+ *
+ * @return bool to indicate if the name is valid
+ */
+static bool
+dns_var_name_valid(const char *name, size_t size)
+{
+    const char prefix[] = "dns_";
+
+    if (dns_var_name_is(name, size, "dev") || dns_var_name_is(name, size, 
"script_type"))
+    {
+        return true;
+    }
+
+    if (size < sizeof(prefix) || strncmp(name, prefix, sizeof(prefix) - 1) != 
0)
+    {
+        return false;
+    }
+
+    for (size_t i = 0; i < size; ++i)
+    {
+        const char c = name[i];
+        if (!((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_'))
+        {
+            return false;
+        }
+    }
+
+    return true;
+}
+
+/**
+ * Check if a dns var value contains only allowed characters
+ *
+ * Values are domain names, IP addresses, port numbers or keywords, so
+ * domain name characters and colons are allowed. A value must not start
+ * with a dash, so that it cannot be taken for an option.
+ *
+ * @param  value    variable value, not NUL terminated
+ * @param  size     size of the value in bytes
+ *
+ * @return bool to indicate if the value is valid
+ */
+static bool
+dns_var_value_valid(const char *value, size_t size)
+{
+    if (size > 0 && value[0] == '-')
+    {
+        return false;
+    }
+
+    for (size_t i = 0; i < size; ++i)
+    {
+        const unsigned char c = (unsigned char)value[i];
+        if (!is_allowed_domain_ascii(c) && c != ':')
+        {
+            return false;
+        }
+    }
+
+    return true;
+}
+
+/**
+ * Check if a dev value contains only allowed characters
+ *
+ * Interface names may contain more than domain name characters, so a few
+ * characters without special meaning to the shell are allowed as well. A
+ * name must not start with a dash, so that it cannot be taken for an option.
+ *
+ * @param  value    interface name, not NUL terminated
+ * @param  size     size of the name in bytes
+ *
+ * @return bool to indicate if the value is valid
+ */
+static bool
+dns_var_dev_valid(const char *value, size_t size)
+{
+    const char extra[] = "+@%,=";
+
+    if (size == 0 || size >= IFNAMSIZ || value[0] == '-')
+    {
+        return false;
+    }
+
+    for (size_t i = 0; i < size; ++i)
+    {
+        const unsigned char c = (unsigned char)value[i];
+        if (!is_allowed_domain_ascii(c) && !memchr(extra, c, sizeof(extra) - 
1))
+        {
+            return false;
+        }
+    }
+
+    return true;
+}
+
+/**
+ * Check if a dns var has an allowed name and value
+ *
+ * @param  name         variable name, not NUL terminated
+ * @param  name_size    size of the name in bytes
+ * @param  value        variable value, not NUL terminated
+ * @param  value_size   size of the value in bytes
+ *
+ * @return bool to indicate if the var is valid
+ */
+static bool
+dns_var_valid(const char *name, size_t name_size, const char *value, size_t 
value_size)
+{
+    if (!dns_var_name_valid(name, name_size))
+    {
+        return false;
+    }
+
+    if (dns_var_name_is(name, name_size, "dev"))
+    {
+        return dns_var_dev_valid(value, value_size);
+    }
+
+    return dns_var_value_valid(value, value_size);
+}
+
+/**
  * Add dns vars received by the updown runner to an env set
  *
- * Every var must be a name=value pair. If anything is invalid, the vars
- * are rejected as a whole.
+ * Every var must be a name=value pair with a valid name and value. If
+ * anything is invalid, the vars are rejected as a whole.
  *
  * @param  vars     NUL terminated name=value strings, modified in place
  * @param  size     size of @p vars in bytes
@@ -617,7 +762,8 @@
         const size_t var_size = strlen(var);
         char *next = var + var_size + 1;
         char *eq = memchr(var, '=', var_size);
-        if (!eq)
+        if (!eq
+            || !dns_var_valid(var, (size_t)(eq - var), eq + 1, var_size - 
(size_t)(eq - var) - 1))
         {
             return false;
         }

--
To view, visit https://gerrit.openvpn.net/c/openvpn/+/2349?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: I5847d602a024044d9109f521de7885d55d28a968
Gerrit-Change-Number: 2349
Gerrit-PatchSet: 3
Gerrit-Owner: Heiko Hund <[email protected]>
Gerrit-Reviewer: Arne Schwabe <[email protected]>
Gerrit-Reviewer: Deri Haerudi <[email protected]>
Gerrit-Reviewer: Răzvan Cojocaru <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: Deri Haerudi <[email protected]>
Gerrit-Attention: Heiko Hund <[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