From: Heiko Hund <[email protected]> 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 <[email protected]> Acked-by: Lev Stipakov <[email protected]> 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 <[email protected]> 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: _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
