From patchwork Sun Nov 29 21:51:18 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gert Doering X-Patchwork-Id: 5817 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:6022:91c9:10b0:11b:d6c:f273 with SMTP id tl9-n1csp12009967lad; Sun, 29 Nov 2026 13:51:36 -0800 (PST) X-Forwarded-Encrypted: i=2; AL+hErHDWEZs0psrq85sbggOd4OUCMaPm12XTj7Ngi+mRq8sBulF4evmkTfZQxNzLmqxCLuKoZPZf39F2xw=@openvpn.net X-Received: by 2002:a05:7022:e80a:b0:1b5:4b48:b9fb with SMTP id a92af1059eb24-1b5567c0ea6mr31805184c88.5.1795989095776; Sun, 29 Nov 2026 13:51:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1795989095; cv=none; d=google.com; s=arc-20260327; b=SYmMHP7YATHVyJ0SSSmyJc2yJELqqbD5APt7ttTl4eoEEtqc7iuxC1mOSSsObyZeVv y9VrBF3o+RK/QuSpT3cicKKOiaYx7ALZLPNVtoK1ZFxUHHR9Cu0g1fydaQu5mqMEGDsX 46OuZ7zYqVgYtO47ZxkUG0fTkH+UwCGi3zKmGXlqJc6mUyRvbnGfqdNMGJJzmk3IyLQ0 4ORazpsCE++X2ZHg91yLaNms0v880mk+ew9U4DzwIZdOWLG6Bfd0gr79qWQQqJSxz11Q dz8KTDn45wt/zjPLM70u4VV3PkMi5C6LE7lOs93mubyvdYc037DlM3peP/XIF7Z7VEP5 DpIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=errors-to:content-transfer-encoding:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:references:in-reply-to:message-id:date:to:from :dkim-signature:dkim-signature:dkim-signature; bh=+cIGLwSN9ySX5oeK8gzmkslqNbxGDAdiyusEPkUQ0kY=; fh=4NbAC/LsuMLI0S0hprUlLSLCiHwg6SCAifhH718Jh0Q=; b=ZRSbtRWnQSEkkgHdwup0cUgQY7yzGuaaDr03D3mBe2t9fwP/up/V8wlQhHRo7r4Ulm VvoR5RLWDc6pTagllIsKZFeeWlG2+b7xDngWcZACIOkWk98IjrprdN7VL764f5F0ZzdA kB7lMYU1Ob2EEbSOatm1JtrODtT7H/1VA5mjXhjek8dWJJmcJmUKy0E9XHBjDen0qXLz ajcktea11WX2WKYQsRd1UBQWK/AOyv5cC0h/xE6md4GoHTXTQ290WjVcSlN/QCKMniPn VHFaVNxSNClTPpjx66PFE0OvE9JDCjTk/ONjd7lHCdD22JpIOwuuzW2hUELyAIi1mOMS S1bg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lists.sourceforge.net header.s=beta header.b=XQ1JZhpg; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=YOuBCvuL; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=G2JgFYSf; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=muc.de Received: from lists.sourceforge.net (lists.sourceforge.net. [216.105.38.7]) by mx.google.com with ESMTPS id a92af1059eb24-1b5923171e7si27139399c88.25.2026.11.29.13.51.35 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 29 Nov 2026 13:51:35 -0800 (PST) Received-SPF: pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) client-ip=216.105.38.7; Authentication-Results: mx.google.com; dkim=pass header.i=@lists.sourceforge.net header.s=beta header.b=XQ1JZhpg; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=YOuBCvuL; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=G2JgFYSf; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=muc.de DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.sourceforge.net; s=beta; h=Content-Transfer-Encoding:Content-Type: List-Subscribe:List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id: Subject:MIME-Version:References:In-Reply-To:Message-ID:Date:To:From:Sender: Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Unsubscribe-Post: List-Owner; bh=+cIGLwSN9ySX5oeK8gzmkslqNbxGDAdiyusEPkUQ0kY=; b=XQ1JZhpgnlPmyn GbaisqLmUdK4rkPRSb/+xhutXFxDLO2qPYIWHsKxkoZY1+MjY8bXuw20T4OtjN73hqagsOikdJHKA npzGwfOx69LeDnrDAZCc+ydc1a48w8PRkGU4Bfzw8y8T+NaqSqF0USP61DVna1au6rau3dq7F1yKa ZXE=; Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.100.1) (envelope-from ) id 1xXmo2-000000008Ce-1oTX; Sun, 29 Nov 2026 21:51:34 +0000 Received: from [172.30.29.66] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.100.1) (envelope-from ) id 1xXmo0-000000008CN-2S8W for openvpn-devel@lists.sourceforge.net; Sun, 29 Nov 2026 21:51:32 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Unsubscribe-Post:List-Subscribe:List-Post:List-Owner:List-Archive; bh=PLGUuGqWRZFE/R5qCOok1qgV7j1USRBzvY5scKhV9Os=; b=YOuBCvuLDR1yr9TlCWpRIpTbcv ZqVPl+Erq3AqKpSE0hL5yzw0rXCgHLm7M2GrxCUGjM59BW+eA4XyC1AA7PrGgR+aliHE6Z8V8boXG RNuHlBRjhVopYpcdHTZivRCtbRqljELLEp30/Dq4JQYTBA4/FKUZi6EovebjUsG3XGro=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Unsubscribe-Post: List-Subscribe:List-Post:List-Owner:List-Archive; bh=PLGUuGqWRZFE/R5qCOok1qgV7j1USRBzvY5scKhV9Os=; b=G2JgFYSf7sh7YGfDutQJnl+4MM NN9hwplL2o0fT2MdEXlYleHmz4Crg0snCtbfYjS/c0pjI6P/9vgTOPaUkx7vzw7diy1s+2v8zXAIx DhntLeakCSS22mHZJ5TzxMaG6zhHkbSLI0qDBMPn5VvhaRpXvrbXLDpGdR00bDtmrhVg=; Received: from [193.149.48.129] (helo=blue.greenie.muc.de) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.100.1) id 1xXmny-0000000010r-32Mo for openvpn-devel@lists.sourceforge.net; Sun, 29 Nov 2026 21:51:32 +0000 Received: from blue.greenie.muc.de (localhost [127.0.0.1]) by blue.greenie.muc.de (8.18.1/8.18.1) with ESMTP id 6ATLpNsZ011283 for ; Sun, 29 Nov 2026 22:51:23 +0100 Received: (from gert@localhost) by blue.greenie.muc.de (8.18.2/8.18.1/Submit) id 6ATLpNCQ011282 for openvpn-devel@lists.sourceforge.net; Sun, 29 Nov 2026 22:51:23 +0100 From: Gert Doering To: openvpn-devel@lists.sourceforge.net Date: Sun, 29 Nov 2026 22:51:18 +0100 Message-ID: <20261129215123.11267-1-gert@greenie.muc.de> X-Mailer: git-send-email 2.55.0 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Score: 1.3 (+) X-Spam-Report: Spam detection software, running on the system "sfi-spamd-1.hosts.colo.sdot.me", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: From: Heiko Hund 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. Content analysis details: (1.3 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 0.0 URIBL_BLOCKED ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [URI: macos-dns-updown.sh] [URI: mycorp.in] [URI: systemd-dns-updown.sh] [URI: changes.md] [URI: openresolv-dns-updown.sh] [URI: openvpn.net] 1.3 RDNS_NONE Delivered to internal network by a host with no rDNS X-Headers-End: 1xXmny-0000000010r-32Mo Subject: [Openvpn-devel] [PATCH v3] dns: pass dns vars to the updown runner via pipe X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox X-GMAIL-THRID: 1883231062084126098 X-GMAIL-MSGID: 1883231062084126098 From: Heiko Hund 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 Reported-by: Deri Haerudi Reported-by: Bhabani Sankar Das <007bsd@proton.me> Change-Id: Ie0d772af38b66799499b1b5729915ed77c438441 Signed-off-by: Heiko Hund Acked-by: Razvan Cojocaru 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 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);