From 85553b42f92457a581e13edbd3e2c2e6136931eb Mon Sep 17 00:00:00 2001 From: Matt Oliver Date: Sun, 30 Oct 2016 00:08:41 +1100 Subject: [PATCH] compat/w32dlfcn.h: Add safe win32 dlopen/dlclose/dlsym functions. Signed-off-by: Matt Oliver --- compat/w32dlfcn.h | 83 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 compat/w32dlfcn.h diff --git a/compat/w32dlfcn.h b/compat/w32dlfcn.h new file mode 100644 index 0000000000..bc9bb8c9f5 --- /dev/null +++ b/compat/w32dlfcn.h @@ -0,0 +1,83 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef COMPAT_W32DLFCN_H +#define COMPAT_W32DLFCN_H + +#ifdef _WIN32 +#include +#if _WIN32_WINNT < 0x0602 +#include "libavutil/wchar_filename.h" +#endif +/** + * Safe function used to open dynamic libs. This attempts to improve program security + * by removing the current directory from the dll search path. Only dll's found in the + * executable or system directory are allowed to be loaded. + * @param name The dynamic lib name. + * @return A handle to the opened lib. + */ +static inline HMODULE win32_dlopen(const char *name) +{ +#if _WIN32_WINNT < 0x0602 + // Need to check if KB2533623 is available + if (!GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "SetDefaultDllDirectories")) { + HMODULE module = NULL; + wchar_t *path = NULL, *name_w = NULL; + DWORD pathlen; + if (utf8towchar(name, &name_w)) + goto exit; + path = (wchar_t *)av_mallocz_array(MAX_PATH, sizeof(wchar_t)); + // Try local directory first + pathlen = GetModuleFileNameW(NULL, path, MAX_PATH); + pathlen = wcsrchr(path, '\\') - path; + if (pathlen == 0 || pathlen + wcslen(name_w) + 2 > MAX_PATH) + goto exit; + path[pathlen] = '\\'; + wcscpy(path + pathlen + 1, name_w); + module = LoadLibraryExW(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); + if (module == NULL) { + // Next try System32 directory + pathlen = GetSystemDirectoryW(path, MAX_PATH); + if (pathlen == 0 || pathlen + wcslen(name_w) + 2 > MAX_PATH) + goto exit; + path[pathlen] = '\\'; + wcscpy(path + pathlen + 1, name_w); + module = LoadLibraryExW(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); + } +exit: + av_free(path); + av_free(name_w); + return module; + } +#endif +#ifndef LOAD_LIBRARY_SEARCH_APPLICATION_DIR +# define LOAD_LIBRARY_SEARCH_APPLICATION_DIR 0x00000200 +#endif +#ifndef LOAD_LIBRARY_SEARCH_SYSTEM32 +# define LOAD_LIBRARY_SEARCH_SYSTEM32 0x00000800 +#endif + return LoadLibraryExA(name, NULL, LOAD_LIBRARY_SEARCH_APPLICATION_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32); +} +#define dlopen(name, flags) win32_dlopen(name) +#define dlclose FreeLibrary +#define dlsym GetProcAddress +#else +#include +#endif + +#endif /* COMPAT_W32DLFCN_H */