From 418e6caeebfb13c66ea1b4904fb331fe57b82b80 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Tue, 21 Sep 2010 13:07:11 -0400 Subject: [PATCH 1/3] New function to load windows system libraries This function uses GetSystemDirectory() to make sure we load the version of the library from c:\windows\system32 (or local equivalent) rather than whatever version lives in the cwd. --- src/common/util.c | 15 +++++++++++++++ src/common/util.h | 4 ++++ src/test/test_util.c | 16 ++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/src/common/util.c b/src/common/util.c index 1d770458f7..12be008745 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -26,6 +26,7 @@ #include #include #include +#include #else #include #include @@ -2793,3 +2794,17 @@ write_pidfile(char *filename) } } +#ifdef MS_WINDOWS +HANDLE +load_windows_system_library(const TCHAR *library_name) +{ + TCHAR path[MAX_PATH]; + unsigned n; + n = GetSystemDirectory(path, 1024); + if (n == 0 || n + _tcslen(library_name) + 2 >= MAX_PATH) + return 0; + _tcscat(path, TEXT("\\")); + _tcscat(path, library_name); + return LoadLibrary(path); +} +#endif diff --git a/src/common/util.h b/src/common/util.h index 4a31c277e3..833fd904c7 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -340,6 +340,10 @@ void start_daemon(void); void finish_daemon(const char *desired_cwd); void write_pidfile(char *filename); +#ifdef MS_WINDOWS +HANDLE load_windows_system_library(const TCHAR *library_name); +#endif + const char *libor_get_digests(void); #endif diff --git a/src/test/test_util.c b/src/test/test_util.c index 8a13597978..116d4f5d8d 100644 --- a/src/test/test_util.c +++ b/src/test/test_util.c @@ -1139,6 +1139,19 @@ test_util_listdir(void *ptr) } } +#ifdef MS_WINDOWS +static void +test_util_load_win_lib(void *ptr) +{ + HANDLE h = load_windows_system_library("advapi32.dll"); + + tt_assert(h); + done: + if (h) + CloseHandle(h); +} +#endif + #define UTIL_LEGACY(name) \ { #name, legacy_test_helper, 0, &legacy_setup, test_util_ ## name } @@ -1162,6 +1175,9 @@ struct testcase_t util_tests[] = { UTIL_TEST(find_str_at_start_of_line, 0), UTIL_TEST(asprintf, 0), UTIL_TEST(listdir, 0), +#ifdef MS_WINDOWS + UTIL_TEST(load_win_lib, 0), +#endif END_OF_TESTCASES }; From aa7f55c45f942f45692c4a99e6fe1d5784609509 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Tue, 21 Sep 2010 13:45:36 -0400 Subject: [PATCH 2/3] Use load_windows_system_library in place of LoadLibrary --- changes/bug1954_loadlib | 4 ++++ src/or/eventdns.c | 2 +- src/or/ntmain.c | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 changes/bug1954_loadlib diff --git a/changes/bug1954_loadlib b/changes/bug1954_loadlib new file mode 100644 index 0000000000..901d9baa5c --- /dev/null +++ b/changes/bug1954_loadlib @@ -0,0 +1,4 @@ + o Major bugfixes + - Always search the windows system directory for system DLLs, and + nowhere else. Fixes bug 1954. + diff --git a/src/or/eventdns.c b/src/or/eventdns.c index 8ebfb79353..c6fcbd9484 100644 --- a/src/or/eventdns.c +++ b/src/or/eventdns.c @@ -3132,7 +3132,7 @@ load_nameservers_with_getnetworkparams(void) GetNetworkParams_fn_t fn; /* XXXX Possibly, we should hardcode the location of this DLL. */ - if (!(handle = LoadLibrary(TEXT("iphlpapi.dll")))) { + if (!(handle = load_windows_system_library(TEXT("iphlpapi.dll")))) { log(EVDNS_LOG_WARN, "Could not open iphlpapi.dll"); /* right now status = 0, doesn't that mean "good" - mikec */ status = -1; diff --git a/src/or/ntmain.c b/src/or/ntmain.c index 0b611f0bf1..e5855aad8e 100644 --- a/src/or/ntmain.c +++ b/src/or/ntmain.c @@ -139,7 +139,7 @@ nt_service_loadlibrary(void) return; /* XXXX Possibly, we should hardcode the location of this DLL. */ - if (!(library = LoadLibrary(TEXT("advapi32.dll")))) { + if (!(library = load_windows_system_library(TEXT("advapi32.dll")))) { log_err(LD_GENERAL, "Couldn't open advapi32.dll. Are you trying to use " "NT services on Windows 98? That doesn't work."); goto err; From d073d7d4ebd461a1550595d0180f0367b67bcc16 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 24 Sep 2010 14:16:55 -0400 Subject: [PATCH 3/3] Consistency issues in load_windows_system_library patch. Thanks Sebastian --- src/common/util.c | 2 +- src/or/eventdns.c | 1 - src/or/ntmain.c | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/common/util.c b/src/common/util.c index 12be008745..caedc5e2d9 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -2800,7 +2800,7 @@ load_windows_system_library(const TCHAR *library_name) { TCHAR path[MAX_PATH]; unsigned n; - n = GetSystemDirectory(path, 1024); + n = GetSystemDirectory(path, MAX_PATH); if (n == 0 || n + _tcslen(library_name) + 2 >= MAX_PATH) return 0; _tcscat(path, TEXT("\\")); diff --git a/src/or/eventdns.c b/src/or/eventdns.c index c6fcbd9484..b929303fd5 100644 --- a/src/or/eventdns.c +++ b/src/or/eventdns.c @@ -3131,7 +3131,6 @@ load_nameservers_with_getnetworkparams(void) IP_ADDR_STRING *ns; GetNetworkParams_fn_t fn; - /* XXXX Possibly, we should hardcode the location of this DLL. */ if (!(handle = load_windows_system_library(TEXT("iphlpapi.dll")))) { log(EVDNS_LOG_WARN, "Could not open iphlpapi.dll"); /* right now status = 0, doesn't that mean "good" - mikec */ diff --git a/src/or/ntmain.c b/src/or/ntmain.c index e5855aad8e..46e7afb78b 100644 --- a/src/or/ntmain.c +++ b/src/or/ntmain.c @@ -138,7 +138,6 @@ nt_service_loadlibrary(void) if (service_fns.loaded) return; - /* XXXX Possibly, we should hardcode the location of this DLL. */ if (!(library = load_windows_system_library(TEXT("advapi32.dll")))) { log_err(LD_GENERAL, "Couldn't open advapi32.dll. Are you trying to use " "NT services on Windows 98? That doesn't work.");