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;
         }
