From: Heiko Hund <[email protected]> The privileged runner had the script source a temp file written by the unprivileged process and unlinked its path unchecked, so another process running as the --user could run code as root.
Send the vars over the pipe instead. The dns scripts no longer source a vars file. Reported-by: Paul Jenkins <[email protected]> Reported-by: Deri Haerudi <[email protected]> Reported-by: Bhabani Sankar Das <[email protected]> Change-Id: Ie0d772af38b66799499b1b5729915ed77c438441 Signed-off-by: Heiko Hund <[email protected]> Acked-by: Razvan Cojocaru <[email protected]> Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2348 --- 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/+/2348 This mail reflects revision 3 of this Change. Acked-by according to Gerrit (reflected above): Razvan Cojocaru <[email protected]> diff --git a/Changes.md b/Changes.md index 5d5697f0..8d10d1e 100644 --- a/Changes.md +++ b/Changes.md @@ -48,6 +48,15 @@ `--pkcs11-providers provider.dll`, need to be given with their full path, unless they are located in one of the directories listed above. +* **`--dns-updown` commands no longer get a `dns_vars_file`** + + When running with `--user`, OpenVPN used to pass the DNS settings to the + `--dns-updown` command in a temporary file named in the `dns_vars_file` + environment variable. The settings are now always passed as environment + variables, the same way as without `--user`. If your own `--dns-updown` + command sources `dns_vars_file`, you can remove that, the variable is no + longer set. + ## Maintainer-visible changes - The configure-time option `--enable-debug` is no longer available diff --git a/distro/dns-scripts/haikuos_file-dns-updown.sh b/distro/dns-scripts/haikuos_file-dns-updown.sh index 04e9e26..d8bf577 100644 --- a/distro/dns-scripts/haikuos_file-dns-updown.sh +++ b/distro/dns-scripts/haikuos_file-dns-updown.sh @@ -7,10 +7,6 @@ # # Example env from openvpn (most are not applied): # -# dns_vars_file /tmp/openvpn_dvf_58b95c0c97b2db43afb5d745f986c53c.tmp -# -# or -# # dev tun0 # script_type dns-up # dns_search_domain_1 mycorp.in @@ -43,7 +39,6 @@ conf=/boot/system/settings/network/resolv.conf test -e "$conf" || exit 1 -test -z "${dns_vars_file}" || . "${dns_vars_file}" case "${script_type}" in dns-up) n=1 diff --git a/distro/dns-scripts/macos-dns-updown.sh b/distro/dns-scripts/macos-dns-updown.sh index fccc6b5..b6b93f8 100644 --- a/distro/dns-scripts/macos-dns-updown.sh +++ b/distro/dns-scripts/macos-dns-updown.sh @@ -8,10 +8,6 @@ # # Example env from openvpn (most are not applied): # -# dns_vars_file /tmp/openvpn_dvf_58b95c0c97b2db43afb5d745f986c53c.tmp -# -# or -# # dev utun0 # script_type dns-up # dns_search_domain_1 mycorp.in @@ -43,8 +39,6 @@ done trap "/bin/rm -f ${lockfile}" EXIT -[ -z "${dns_vars_file}" ] || . "${dns_vars_file}" - itf_dns_key="State:/Network/Service/openvpn-${dev}/DNS" function primary_dns_key { diff --git a/distro/dns-scripts/openresolv-dns-updown.sh b/distro/dns-scripts/openresolv-dns-updown.sh index 4aca99f..4865031 100644 --- a/distro/dns-scripts/openresolv-dns-updown.sh +++ b/distro/dns-scripts/openresolv-dns-updown.sh @@ -8,10 +8,6 @@ # # Example env from openvpn (most are not applied): # -# dns_vars_file /tmp/openvpn_dvf_58b95c0c97b2db43afb5d745f986c53c.tmp -# -# or -# # dev tun0 # script_type dns-up # dns_search_domain_1 mycorp.in @@ -42,7 +38,6 @@ done } -[ -z "${dns_vars_file}" ] || . "${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 78bf9d3..d68c00d 100644 --- a/distro/dns-scripts/resolvconf_file-dns-updown.sh +++ b/distro/dns-scripts/resolvconf_file-dns-updown.sh @@ -7,10 +7,6 @@ # # Example env from openvpn (most are not applied): # -# dns_vars_file /tmp/openvpn_dvf_58b95c0c97b2db43afb5d745f986c53c.tmp -# -# or -# # dev tun0 # script_type dns-up # dns_search_domain_1 mycorp.in @@ -43,7 +39,6 @@ conf=/etc/resolv.conf test -e "$conf" || exit 1 -test -z "${dns_vars_file}" || . "${dns_vars_file}" case "${script_type}" in dns-up) n=1 diff --git a/distro/dns-scripts/systemd-dns-updown.sh b/distro/dns-scripts/systemd-dns-updown.sh index 3bdaa4b..ff6f982 100644 --- a/distro/dns-scripts/systemd-dns-updown.sh +++ b/distro/dns-scripts/systemd-dns-updown.sh @@ -15,10 +15,6 @@ # # Example env from openvpn (not all are always applied): # -# dns_vars_file /tmp/openvpn_dvf_58b95c0c97b2db43afb5d745f986c53c.tmp -# -# or -# # dev tun0 # script_type dns-up # dns_search_domain_1 mycorp.in @@ -34,8 +30,6 @@ # dns_server_1_sni dns.mycorp.in # -[ -z "${dns_vars_file}" ] || . "${dns_vars_file}" - function do_resolved_servers { local sni="" local transport_var=dns_server_${n}_transport diff --git a/src/openvpn/dns.c b/src/openvpn/dns.c index e2cb981..1bd6c85 100644 --- a/src/openvpn/dns.c +++ b/src/openvpn/dns.c @@ -587,23 +587,113 @@ setenv_dns_options(o, es); } -static int -do_run_up_down_command(bool up, const char *vars_file, const struct dns_options *o, - const struct tuntap *tt) +/** Maximum size in bytes of the dns vars sent to the updown runner */ +#define DNS_VARS_MAX_SIZE (64 * 1024) + +/** + * 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. + * + * @param vars NUL terminated name=value strings, modified in place + * @param size size of @p vars in bytes + * @param es env set to add the vars to + * + * @return bool to indicate success or failure + */ +static bool +dns_vars_setenv(char *vars, size_t size, struct env_set *es) { - struct gc_arena gc = gc_new(); + if (size > 0 && vars[size - 1] != '\0') + { + return false; + } + + const char *const end = vars + size; + char *var = vars; + while (var < end) + { + const size_t var_size = strlen(var); + char *next = var + var_size + 1; + char *eq = memchr(var, '=', var_size); + if (!eq) + { + return false; + } + + *eq = '\0'; + setenv_str(es, var, eq + 1); + var = next; + } + + return true; +} + +/** + * Read from a pipe until the requested amount of data has arrived + * + * @param fd file descriptor to read from + * @param data buffer to read into + * @param size number of bytes to read + * + * @return bool to indicate success or failure + */ +static bool +read_pipe(int fd, void *data, size_t size) +{ + uint8_t *p = data; + while (size > 0) + { + const ssize_t len = read(fd, p, size); + if (len == -1 && errno == EINTR) + { + continue; + } + if (len < 1) + { + return false; + } + p += len; + size -= (size_t)len; + } + return true; +} + +/** + * Write to a pipe until all data has been written + * + * @param fd file descriptor to write to + * @param data data to write + * @param size number of bytes to write + * + * @return bool to indicate success or failure + */ +static bool +write_pipe(int fd, const void *data, size_t size) +{ + const uint8_t *p = data; + while (size > 0) + { + const ssize_t len = write(fd, p, size); + if (len == -1 && errno == EINTR) + { + continue; + } + if (len < 1) + { + return false; + } + p += len; + size -= (size_t)len; + } + return true; +} + +static int +do_run_up_down_command(const struct dns_options *o, const struct env_set *es) +{ struct argv argv = argv_new(); - struct env_set *es = env_set_create(&gc); - - if (vars_file) - { - setenv_str(es, "dns_vars_file", vars_file); - } - else - { - updown_env_set(up, o, tt, es); - } - argv_printf(&argv, "%s", o->updown); argv_msg(M_INFO, &argv); int res; @@ -616,13 +706,11 @@ res = openvpn_execve_check(&argv, es, S_EXITCODE, "WARNING: Failed running dns updown"); } argv_free(&argv); - gc_free(&gc); return res; } static bool -run_updown_runner(bool up, struct options *o, const struct tuntap *tt, - struct dns_updown_runner_info *updown_runner) +run_updown_runner(struct options *o, struct dns_updown_runner_info *updown_runner) { int dns_pipe_fd[2]; int ack_pipe_fd[2]; @@ -675,56 +763,58 @@ signal(SIGUSR2, SIG_IGN); signal(SIGPIPE, SIG_IGN); + char *vars = malloc(DNS_VARS_MAX_SIZE); + check_malloc_return(vars); + while (1) { - char path[PATH_MAX]; + uint32_t vars_size; - /* Block here until parent sends a path */ - ssize_t rlen = read(dns_pipe_fd[0], &path, sizeof(path)); - if (rlen < 1) + /* Block here until parent sends the dns vars */ + if (!read_pipe(dns_pipe_fd[0], &vars_size, sizeof(vars_size))) { - if (rlen == -1 && errno == EINTR) - { - continue; - } - close(dns_pipe_fd[0]); - close(ack_pipe_fd[1]); - exit(0); + break; + } + if (vars_size > DNS_VARS_MAX_SIZE) + { + msg(M_WARN, "dns updown: received dns vars are too large"); + break; + } + if (!read_pipe(dns_pipe_fd[0], vars, vars_size)) + { + break; } - path[sizeof(path) - 1] = '\0'; - int res = do_run_up_down_command(up, path, &o->dns_options, tt); - platform_unlink(path); + int res = -1; + struct gc_arena gc = gc_new(); + struct env_set *es = env_set_create(&gc); + if (dns_vars_setenv(vars, vars_size, es)) + { + res = do_run_up_down_command(&o->dns_options, es); + } + else + { + msg(M_WARN, "dns updown: received invalid dns vars"); + } + gc_free(&gc); /* Unblock parent process */ - while (1) + if (!write_pipe(ack_pipe_fd[1], &res, sizeof(res))) { - ssize_t wlen = write(ack_pipe_fd[1], &res, sizeof(res)); - if ((wlen == -1 && errno != EINTR) || wlen < (ssize_t)sizeof(res)) - { - /* Not much we can do about errors but exit */ - close(dns_pipe_fd[0]); - close(ack_pipe_fd[1]); - exit(0); - } - else if (wlen == sizeof(res)) - { - break; - } + break; } - - up = !up; /* do the opposite next time */ } + + /* Not much we can do about errors but exit */ + free(vars); + close(dns_pipe_fd[0]); + close(ack_pipe_fd[1]); + exit(0); } return true; } -#if defined(__GNUC__) || defined(__clang__) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wsign-conversion" -#endif - static void run_up_down_command(bool up, struct options *o, const struct tuntap *tt, struct dns_updown_runner_info *updown_runner) @@ -736,77 +826,46 @@ } int status = -1; + struct gc_arena gc = gc_new(); + struct env_set *es = env_set_create(&gc); + updown_env_set(up, dns, tt, es); if (!updown_runner->required) { /* Run dns updown directly */ - status = do_run_up_down_command(up, NULL, dns, tt); + status = do_run_up_down_command(dns, es); } else { - if (updown_runner->pid < 1) + /* Need to set up privilege preserving child first */ + if (updown_runner->pid < 1 && !run_updown_runner(o, updown_runner)) { - /* Need to set up privilege preserving child first */ - if (!run_updown_runner(up, o, tt, updown_runner)) - { - return; - } + gc_free(&gc); + return; } - struct gc_arena gc = gc_new(); - const char *dvf = platform_create_temp_file(o->tmp_dir, "dvf", &gc); - if (!dvf) + struct buffer vars = env_set_write_buffer(es, &gc); + const uint32_t vars_size = (uint32_t)BLEN(&vars); + if (BLEN(&vars) > DNS_VARS_MAX_SIZE) { - msg(M_ERR, "could not create dns vars file"); - goto out_free; + msg(M_WARN, "dns vars exceed the maximum size"); } - - struct env_set *es = env_set_create(&gc); - updown_env_set(up, &o->dns_options, tt, es); - env_set_write_file(dvf, es); - - int wfd = updown_runner->fds[1]; - ssize_t dvf_size = strlen(dvf) + 1; - while (1) + else if (!write_pipe(updown_runner->fds[1], &vars_size, sizeof(vars_size)) + || !write_pipe(updown_runner->fds[1], BPTR(&vars), vars_size)) { - ssize_t len = write(wfd, dvf, dvf_size); - if (len < dvf_size) - { - if (len == -1 && errno == EINTR) - { - continue; - } - msg(M_WARN | M_ERRNO, "could not send dns vars filename"); - } - break; + msg(M_WARN | M_ERRNO, "could not send dns vars"); } - - int rfd = updown_runner->fds[0]; - while (1) + else if (!read_pipe(updown_runner->fds[0], &status, sizeof(status))) { - ssize_t len = read(rfd, &status, sizeof(status)); - if (len < (ssize_t)sizeof(status)) - { - if (len == -1 && errno == EINTR) - { - continue; - } - msg(M_WARN | M_ERRNO, "could not receive dns updown status"); - } - break; + msg(M_WARN | M_ERRNO, "could not receive dns updown status"); + status = -1; } - -out_free: - gc_free(&gc); } + gc_free(&gc); msg(M_INFO, "dns %s command exited with status %d", up ? "up" : "down", status); } -#if defined(__GNUC__) || defined(__clang__) -#pragma GCC diagnostic pop -#endif - #endif /* _WIN32 */ void diff --git a/src/openvpn/env_set.c b/src/openvpn/env_set.c index 55190b8..9494851 100644 --- a/src/openvpn/env_set.c +++ b/src/openvpn/env_set.c @@ -232,28 +232,22 @@ } } -void -env_set_write_file(const char *path, const struct env_set *es) +struct buffer +env_set_write_buffer(const struct env_set *es, struct gc_arena *gc) { - FILE *fp = platform_fopen(path, "w"); - if (!fp) + size_t size = 0; + for (const struct env_item *e = es ? es->list : NULL; e; e = e->next) { - msg(M_ERR, "could not write env set to '%s'", path); - return; + size += strlen(e->string) + 1; } - if (es) + struct buffer buf = alloc_buf_gc(size, gc); + for (const struct env_item *e = es ? es->list : NULL; e; e = e->next) { - const struct env_item *item = es->list; - while (item) - { - fputs(item->string, fp); - fputc('\n', fp); - item = item->next; - } + ASSERT(buf_write(&buf, e->string, strlen(e->string) + 1)); } - fclose(fp); + return buf; } void diff --git a/src/openvpn/env_set.h b/src/openvpn/env_set.h index fcfc3ca..24d3bde 100644 --- a/src/openvpn/env_set.h +++ b/src/openvpn/env_set.h @@ -88,12 +88,17 @@ void env_set_print(msglvl_t msglevel, const struct env_set *es); /** - * Write a struct env_set to a file. Each item on one line. + * Write the items of an env set into a buffer * - * @param path The filepath to write to. - * @param es Pointer to the env_set to write. + * The items are written as NUL terminated name=value strings, one after + * the other. + * + * @param es env set to write + * @param gc gc arena to allocate the buffer from + * + * @return struct buffer containing the items */ -void env_set_write_file(const char *path, const struct env_set *es); +struct buffer env_set_write_buffer(const struct env_set *es, struct gc_arena *gc); void env_set_inherit(struct env_set *es, const struct env_set *src); _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
