diff options
author | Corinna Vinschen <corinna@vinschen.de> | 2010-08-28 11:22:37 +0000 |
---|---|---|
committer | Corinna Vinschen <corinna@vinschen.de> | 2010-08-28 11:22:37 +0000 |
commit | 71d8f118da24bb5a31f406e5fba222a324913b05 (patch) | |
tree | 093f51ed610d7333e4a933f68a1ee8900f5438bb /winsup/utils/loadlib.h | |
parent | 893a8b78fca52a5474fbca9a0b881b622afc5044 (diff) | |
download | newlib-71d8f118da24bb5a31f406e5fba222a324913b05.zip newlib-71d8f118da24bb5a31f406e5fba222a324913b05.tar.gz newlib-71d8f118da24bb5a31f406e5fba222a324913b05.tar.bz2 |
* loadlib.h: New header implementing safe LoadLibrary calls.
Include throughout files using LoadLibrary function.
* cygcheck.cc (dump_sysinfo): Retrieve kernel32.dll handle via
GetModuleHandle, rather than using LoadLibrary.
* cygpath.cc (get_long_name): Ditto.
(do_sysfolders): Append .dll suffix in LoadLibrary call.
* ldh.cc (WinMain): Use LoadLibraryExW with DONT_RESOLVE_DLL_REFERENCES
to avoid loading malicious library code.
* locale.cc (print_locale_with_codeset): Change way to retrieve
kernel32.dll path.
Diffstat (limited to 'winsup/utils/loadlib.h')
-rw-r--r-- | winsup/utils/loadlib.h | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/winsup/utils/loadlib.h b/winsup/utils/loadlib.h new file mode 100644 index 0000000..405b4b2 --- /dev/null +++ b/winsup/utils/loadlib.h @@ -0,0 +1,59 @@ +/* loadlib.h + + Copyright 2010 Red Hat, Inc. + + This file is part of Cygwin. + + This software is a copyrighted work licensed under the terms of the + Cygwin license. Please consult the file "CYGWIN_LICENSE" for + details. */ + +#ifndef _LOADLIB_H +#define _LOADLIB_H + +#include <windows.h> +#include <wchar.h> + +/* Load all system libs from the windows system directory by prepending the + full path. This doesn't work for loadling cygwin1.dll. For this case, + instead of prepending the path, make sure that the CWD is removed from + the DLL search path, if possible (XP SP1++, Vista++). */ +static HMODULE +_load_sys_library (const wchar_t *dll) +{ + static BOOL (*set_dll_directory)(LPCWSTR); + static WCHAR sysdir[MAX_PATH]; + static UINT sysdir_len; + + WCHAR dllpath[MAX_PATH]; + + if (!sysdir_len) + { + sysdir_len = GetSystemDirectoryW (sysdir, MAX_PATH); + sysdir[sysdir_len++] = L'\\'; + sysdir[sysdir_len] = L'\0'; + } + if (!set_dll_directory) + { + HMODULE k32 = GetModuleHandleW (L"kernel32.dll"); + if (k32) + set_dll_directory = (BOOL (*)(LPCWSTR)) + GetProcAddress (k32, "SetDllDirectoryW"); + if (!set_dll_directory) + set_dll_directory = (BOOL (*)(LPCWSTR)) -1; + else + set_dll_directory (L""); + } + + if (wcscmp (dll, L"cygwin1.dll") == 0) + return LoadLibraryExW (L"cygwin1.dll", NULL, LOAD_WITH_ALTERED_SEARCH_PATH); + + wcscpy (dllpath, sysdir); + wcscpy (dllpath + sysdir_len, dll); + return LoadLibraryExW (dllpath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); +} + +#define LoadLibraryW(d) _load_sys_library(d) +#define LoadLibraryA(d) _load_sys_library(L##d) + +#endif /* _LOADLIB_H */ |