From patchwork Mon Nov 23 17:24:09 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gert Doering X-Patchwork-Id: 5776 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:6022:91c9:10b0:11b:d6c:f273 with SMTP id tl9-n1csp3101700lad; Mon, 23 Nov 2026 09:24:32 -0800 (PST) X-Forwarded-Encrypted: i=2; AL+hErEX/bOG8BUimAuVF/L/q8Yg84t+rMBR/fD9o8Tgfr8bsKA7yCpKLnNV/JrSHdGTMaX8Y9NutrYEEcE=@openvpn.net X-Received: by 2002:a05:693c:8604:b0:386:b3e6:e741 with SMTP id 5a478bee46e88-386b3e6e883mr2468505eec.26.1795454671684; Mon, 23 Nov 2026 09:24:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1795454671; cv=none; d=google.com; s=arc-20260327; b=fPeFXTOx4jf4EgpZoU1OAnYLnlM1TH5u8eJUKCw3HxCVu10sh2roztC30PrrAWShNc NI2LVrW6jFsmLLOsrA/TRJ51ml7fhZRishsiFVLg9VZuNE7xBHtGDQ3r1rOSeFvRybN2 WM0v62fmrS55uAJjz9+5p9ZeRH7cH8sj3jY1sfIDKbLoWzLM4vPVatYcAKEjp5tA92Vb UzF4XuWBnL4AtefCjI7IVDHjpdAQXpgljs5FNcJ0VO3MEpWltAIaRL+JauoAhydzwKKa uvZqeGWa41/oeZEemAD8Jep4CobAfooJLbb1yyO4W5myz3Xg/VO/qyIFz6g6ztQnljp1 UHxw== 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=Qa/kKeir02OnpqDzPcPeC5JWsyFdgw2DxoSG9L7OZ30=; fh=4NbAC/LsuMLI0S0hprUlLSLCiHwg6SCAifhH718Jh0Q=; b=XqedV6nik21EPpoG/i8R8I/9WuplDeXoVUXDcPbf5WcpuPuiUgEJsO13RcKh/zBbOY rGuNkicGTs9Ia9dY37THxp4GEzkOJ27bFzXOiLVb1eOMSsIUnWrGHHhKvOWIjQOap2MF odSzB4IBsATt5H4zcc9pPgHCoSx6Mss16rwnHXdkpsgRsL5Z1V9ytAa2ccYFW4FY96Cp mAXDGDLNcQzwXNEBUsUegrUnXqUXH1xhqkQxLYuuxVyUef0pRYeubwiMPyTO0cNudjfr nyDQAlUg2cK0JODWQYtYjB3cWLtrNYxLzXjzzZN8kUK57ynb2mtHGoY6S6AOq7xvB0qL wDdA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lists.sourceforge.net header.s=beta header.b=Nl8YysAw; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=kiqWjeB+; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=X2LSUOtO; 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 5a478bee46e88-385672cc1f6si31351441eec.30.2026.11.23.09.24.31 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 23 Nov 2026 09:24:31 -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=Nl8YysAw; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=kiqWjeB+; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=X2LSUOtO; 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=Qa/kKeir02OnpqDzPcPeC5JWsyFdgw2DxoSG9L7OZ30=; b=Nl8YysAwIl9sn2 Spr5vecLzWQ3Ay+9p0VZYsIisO+Quo5+lnS09BsVlT9uBUsJFcAhXfBl7TIBN5iH9zh69N+VSPxDs mJePn4AdQE12mThAFiOWjf0YdiPyA7NNMyZYXM90nVsnsEaKvPok1PPp62m1F9djMZprNlHUrXMzH 784=; Received: from [127.0.0.1] (helo=sfs-ml-1.v29.lw.sourceforge.com) by sfs-ml-1.v29.lw.sourceforge.com with esmtp (Exim 4.100.1) (envelope-from ) id 1xVXmE-0000000076P-3sqF; Mon, 23 Nov 2026 17:24:26 +0000 Received: from [172.30.29.66] (helo=mx.sourceforge.net) by sfs-ml-1.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.100.1) (envelope-from ) id 1xVXmC-0000000076H-2Wvx for openvpn-devel@lists.sourceforge.net; Mon, 23 Nov 2026 17:24:24 +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=+0Lu36IaYk422TM71RD8aiWSn9e2XqLbmPUkj6aYeRM=; b=kiqWjeB+ujy1okMlu7uEH4+rQm COkqPqyzTRknk5mrwHhgxy3Iyqc7toAPSo1Z/HRY+5mVXpPw9kfTP0ZEZ8U8qWawK4TCD5yvgCfqp Z6OwG2td92ojZuXrTw0DsTua01fFiWlMpQO/WeZTyXJe0EVHHprlwxwu+eaVBGrgR8Dc=; 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=+0Lu36IaYk422TM71RD8aiWSn9e2XqLbmPUkj6aYeRM=; b=X2LSUOtONPfu4QP5gQFaDCdoHt xuRUKk1+mLixJHMo/KPkG5rhuvota2Yb+3WnOdUY/OrjlzZlfRUjDL0bBbCf6l+530AADhGiOpGCv RR36aphIi7BKui6u2WM3i658UULf21VMwr7x7NEP1jEaZks+UT+0K6yAmIwrKNjZjbRQ=; Received: from [193.149.48.129] (helo=blue.greenie.muc.de) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.100.1) id 1xVXmA-000000001b2-2TUh for openvpn-devel@lists.sourceforge.net; Mon, 23 Nov 2026 17:24:24 +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 6ANHOF4x022031 for ; Mon, 23 Nov 2026 18:24:15 +0100 Received: (from gert@localhost) by blue.greenie.muc.de (8.18.2/8.18.1/Submit) id 6ANHOFLP022030 for openvpn-devel@lists.sourceforge.net; Mon, 23 Nov 2026 18:24:15 +0100 From: Gert Doering To: openvpn-devel@lists.sourceforge.net Date: Mon, 23 Nov 2026 18:24:09 +0100 Message-ID: <20261123172414.22019-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 DLLs that openvpn loads by name, like dependencies of plugins or modules loaded by libraries like OpenSSL, are searched in the current directory and the directories in PATH, among others. These can co [...] Content analysis details: (1.3 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 1.3 RDNS_NONE Delivered to internal network by a host with no rDNS X-Headers-End: 1xVXmA-000000001b2-2TUh Subject: [Openvpn-devel] [PATCH v1] win32: restrict the DLL search path of openvpn 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: 1882670677862131449 X-GMAIL-MSGID: 1882670677862131449 From: Heiko Hund DLLs that openvpn loads by name, like dependencies of plugins or modules loaded by libraries like OpenSSL, are searched in the current directory and the directories in PATH, among others. These can contain places where users have write access. Placing a dll there makes it possible to inject code into the openvpn process and gives access to privileged operations. Call SetDefaultDllDirectories() at the start of wmain() to restrict the search to the application directory, the system directory and the directories added with AddDllDirectory(). openvpn exits if this fails. The trusted plugin directory is added with AddDllDirectory(). To get it, move the code that determined it inline in plugin_in_trusted_dir() into get_plugin_dir(), which returns the normalized directory or fails if it cannot be determined. plugin_in_trusted_dir() now only checks against the system directory if the plugin is not in the plugin directory. It now fails if the system directory cannot be retrieved, instead of checking against an empty path. Change-Id: I9093aaaf8f198cd389e9f72186397a87a7ded297 Signed-off-by: Heiko Hund Acked-by: Lev Stipakov Gerrit URL: https://gerrit.openvpn.net/c/openvpn-private/+/2147 --- 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-private/+/2147 This mail reflects revision 1 of this Change. Acked-by according to Gerrit (reflected above): Lev Stipakov diff --git a/Changes.md b/Changes.md index b5ae881..62640b2 100644 --- a/Changes.md +++ b/Changes.md @@ -23,6 +23,18 @@ the characters mentioned above were already escaped. However, the behavior of Mbed TLS is slightly different from OpenSSL in that it also escapes "=". +* **Windows: DLLs are no longer loaded from the current directory or PATH** + + OpenVPN now only loads DLLs from its installation directory, the Windows + system directory and the trusted plugin directory. DLLs a plugin depends on + are also looked up in the directory of the plugin itself. The current + directory and the directories in `PATH` are not searched anymore. + + If a plugin depends on DLLs that were only found via `PATH`, install them + next to the plugin. PKCS#11 providers given by name only, e.g. + `--pkcs11-providers provider.dll`, need to be given with their full path, + unless they are located in one of the directories listed above. + ## Maintainer-visible changes - The configure-time option `--enable-debug` is no longer available diff --git a/src/openvpn/openvpn.c b/src/openvpn/openvpn.c index b32f009..da1def8 100644 --- a/src/openvpn/openvpn.c +++ b/src/openvpn/openvpn.c @@ -358,6 +358,23 @@ int ret; int i; + /* Initialize the message output, so that msg() can be used below */ + error_reset(); + + /* Load DLLs only from the application and system directories */ + if (!SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) + { + msg(M_FATAL | M_ERRNO, "Failed to restrict the DLL search path."); + } + + /* Also allow loading DLLs from the trusted plugin directory */ + WCHAR plugin_dir[MAX_PATH]; + BOOL have_plugin_dir = get_plugin_dir(plugin_dir, _countof(plugin_dir)); + if (have_plugin_dir && !AddDllDirectory(plugin_dir)) + { + msg(M_WARN | M_ERRNO, "Failed to add the plugin directory to the DLL search path."); + } + /* * Do not use the argv provided by the CRT. Split the command line with * CommandLineToArgvW() instead, which is what the interactive service uses diff --git a/src/openvpn/win32.c b/src/openvpn/win32.c index 3af58ed..a2a1b22 100644 --- a/src/openvpn/win32.c +++ b/src/openvpn/win32.c @@ -1571,6 +1571,30 @@ } } +BOOL +get_plugin_dir(PWSTR dir, DWORD size) +{ + WCHAR plugin_dir[MAX_PATH] = { 0 }; + + /* Attempt to retrieve the trusted plugin directory path from the registry, + * using installation path as a fallback */ + if (!get_openvpn_reg_value(L"plugin_dir", plugin_dir, sizeof(plugin_dir)) + && !get_openvpn_reg_value(NULL, plugin_dir, sizeof(plugin_dir))) + { + msg(M_WARN, "Installation path could not be determined."); + return FALSE; + } + + DWORD len = GetFullPathNameW(plugin_dir, size, dir, NULL); + if (len == 0 || len >= size) + { + msg(M_NONFATAL | M_ERRNO, "Failed to normalize plugin dir."); + return FALSE; + } + + return TRUE; +} + bool plugin_in_trusted_dir(const WCHAR *plugin_path) { @@ -1581,47 +1605,22 @@ return false; } - WCHAR plugin_dir[MAX_PATH] = { 0 }; - - /* Attempt to retrieve the trusted plugin directory path from the registry, - * using installation path as a fallback */ - if (!get_openvpn_reg_value(L"plugin_dir", plugin_dir, sizeof(plugin_dir)) - && !get_openvpn_reg_value(NULL, plugin_dir, sizeof(plugin_dir))) - { - msg(M_WARN, "Installation path could not be determined."); - } - - /* Get the system directory */ - WCHAR system_dir[MAX_PATH] = { 0 }; - if (GetSystemDirectoryW(system_dir, _countof(system_dir)) == 0) - { - msg(M_NONFATAL | M_ERRNO, "Failed to get system directory."); - } - - if ((wcslen(plugin_dir) == 0) && (wcslen(system_dir) == 0)) - { - return false; - } - - WCHAR normalized_plugin_dir[MAX_PATH] = { 0 }; - - /* Normalize the plugin dir */ - if (wcslen(plugin_dir) > 0) - { - if (!GetFullPathNameW(plugin_dir, MAX_PATH, normalized_plugin_dir, NULL)) - { - msg(M_NONFATAL | M_ERRNO, "Failed to normalize plugin dir."); - return false; - } - } - /* Check if the plugin path resides within the plugin/install directory */ - if (win_path_in_dir(plugin_path, normalized_plugin_dir)) + WCHAR plugin_dir[MAX_PATH] = { 0 }; + if (get_plugin_dir(plugin_dir, _countof(plugin_dir)) + && win_path_in_dir(plugin_path, plugin_dir)) { return true; } /* Fallback to the system directory */ + WCHAR system_dir[MAX_PATH] = { 0 }; + if (GetSystemDirectoryW(system_dir, _countof(system_dir)) == 0) + { + msg(M_NONFATAL | M_ERRNO, "Failed to get system directory."); + return false; + } + return win_path_in_dir(plugin_path, system_dir); } diff --git a/src/openvpn/win32.h b/src/openvpn/win32.h index 39ff7d8..646e3eb 100644 --- a/src/openvpn/win32.h +++ b/src/openvpn/win32.h @@ -336,6 +336,19 @@ bool get_openvpn_reg_value(const WCHAR *key, WCHAR *value, DWORD size); /** + * Get the trusted plugin directory + * + * The directory is taken from the "plugin_dir" registry value, or the + * installation path, if that value is missing. The path is normalized. + * + * @param dir buffer to store the directory in + * @param size size of the @p dir buffer in characters + * + * @return BOOL to indicate success or failure + */ +BOOL get_plugin_dir(PWSTR dir, DWORD size); + +/** * @brief Checks if a plugin is located in a trusted directory. * * Verifies the plugin's path against a trusted directory, which is: