Do not search outside of the system directory for windows DLLs

Hardens against some attacks.
This commit is contained in:
Nick Mathewson 2010-09-27 15:12:55 -04:00
parent 045eef4cde
commit d49b5e3326
4 changed files with 42 additions and 1 deletions

View File

@ -3509,7 +3509,8 @@ load_nameservers_with_getnetworkparams(struct evdns_base *base)
GetNetworkParams_fn_t fn; GetNetworkParams_fn_t fn;
ASSERT_LOCKED(base); ASSERT_LOCKED(base);
if (!(handle = LoadLibrary(TEXT("iphlpapi.dll")))) { if (!(handle = evutil_load_windows_system_library(
TEXT("iphlpapi.dll")))) {
log(EVDNS_LOG_WARN, "Could not open iphlpapi.dll"); log(EVDNS_LOG_WARN, "Could not open iphlpapi.dll");
status = -1; status = -1;
goto done; goto done;

View File

@ -35,6 +35,7 @@
#include <windows.h> #include <windows.h>
#undef WIN32_LEAN_AND_MEAN #undef WIN32_LEAN_AND_MEAN
#include <io.h> #include <io.h>
#include <tchar.h>
#endif #endif
#include <sys/types.h> #include <sys/types.h>
@ -2060,3 +2061,19 @@ evutil_hex_char_to_int(char c)
} }
return -1; return -1;
} }
#ifdef WIN32
HANDLE
evutil_load_windows_system_library(const TCHAR *library_name)
{
TCHAR path[MAX_PATH];
unsigned n;
n = GetSystemDirectory(path, MAX_PATH);
if (n == 0 || n + _tcslen(library_name) + 2 >= MAX_PATH)
return 0;
_tcscat(path, TEXT("\\"));
_tcscat(path, library_name);
return LoadLibrary(path);
}
#endif

View File

@ -1037,6 +1037,22 @@ end:
evutil_freeaddrinfo(ai); evutil_freeaddrinfo(ai);
} }
#ifdef WIN32
static void
test_evutil_loadsyslib(void *arg)
{
HANDLE h=NULL;
h = evutil_load_windows_system_library(TEXT("kernel32.dll"));
tt_assert(h);
end:
if (h)
CloseHandle(h);
}
#endif
struct testcase_t util_testcases[] = { struct testcase_t util_testcases[] = {
{ "ipv4_parse", regress_ipv4_parse, 0, NULL, NULL }, { "ipv4_parse", regress_ipv4_parse, 0, NULL, NULL },
{ "ipv6_parse", regress_ipv6_parse, 0, NULL, NULL }, { "ipv6_parse", regress_ipv6_parse, 0, NULL, NULL },
@ -1052,6 +1068,9 @@ struct testcase_t util_testcases[] = {
{ "integers", test_evutil_integers, 0, NULL, NULL }, { "integers", test_evutil_integers, 0, NULL, NULL },
{ "rand", test_evutil_rand, TT_FORK, NULL, NULL }, { "rand", test_evutil_rand, TT_FORK, NULL, NULL },
{ "getaddrinfo", test_evutil_getaddrinfo, TT_FORK, NULL, NULL }, { "getaddrinfo", test_evutil_getaddrinfo, TT_FORK, NULL, NULL },
#ifdef WIN32
{ "loadsyslib", test_evutil_loadsyslib, TT_FORK, NULL, NULL },
#endif
END_OF_TESTCASES, END_OF_TESTCASES,
}; };

View File

@ -224,6 +224,10 @@ long evutil_tv_to_msec(const struct timeval *tv);
int evutil_hex_char_to_int(char c); int evutil_hex_char_to_int(char c);
#ifdef WIN32
HANDLE evutil_load_windows_system_library(const TCHAR *library_name);
#endif
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif