From: Heiko Hund <[email protected]> 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]> Acked-by: Razvan Cojocaru <[email protected]> Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2349 --- This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master. Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2349 This mail reflects revision 6 of this Change. Acked-by according to Gerrit (reflected above): Razvan Cojocaru <[email protected]> diff --git a/src/openvpn/dns.c b/src/openvpn/dns.c index 1bd6c85..8c6fd97 100644 --- a/src/openvpn/dns.c +++ b/src/openvpn/dns.c @@ -591,10 +591,168 @@ #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. dns_vars_file is + * rejected, since older dns-updown scripts source the file it names. + * + * @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 (dns_var_name_is(name, size, "dns_vars_file")) + { + return false; + } + + 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 + * slash is only allowed on Haiku, where interface names look like tun/0, + * since it would turn the name into a path elsewhere. A name must not start + * with a dash or a dot, so that it cannot be taken for an option, a hidden + * file or a parent directory. + * + * @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) +{ +#if defined(TARGET_HAIKU) + const char extra[] = "+@%,=/"; +#else + const char extra[] = "+@%,="; +#endif + + if (size == 0 || size >= IFNAMSIZ || value[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) && !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 +775,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; } _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
