win32.c: Drop current working directory from DLL search path by calling SetDllDirectory() with an empty string. Avoids possible security problems, especially when loading plugins.

Makefile, win32.c: Load ntdll.dll and lookup used functions at runtime (safer for future windows version and fixes unintended linking against sscanf() from ntdll)
win32.c: Add gkrellm_sys_inet_cleanup() for proper unloading of iphlpapi.dll
This commit is contained in:
Stefan Gehn 2010-09-20 20:53:48 +00:00
parent a457f7a3e1
commit 8dbee76730
3 changed files with 51 additions and 10 deletions

View File

@ -172,7 +172,7 @@ windows: libgkrellmd.a
CFLAGS="${CFLAGS} -D_WIN32_WINNT=0x0501 -DWINVER=0x0501 -DWIN32_LEAN_AND_MEAN" \
LINK_FLAGS="${LINK_FLAGS} -mconsole" \
EXTRAOBJS="${EXTRAOBJS} win32-resource.o win32-plugin.o" \
SYS_LIBS="-lws2_32 -lwtsapi32 -lpdh -lnetapi32 -liphlpapi -lntdll -lintl" \
SYS_LIBS="-lws2_32 -lwtsapi32 -lpdh -lnetapi32 -liphlpapi -lintl" \
gkrellmd
install: install_bin install_inc install_man

View File

@ -190,7 +190,7 @@ windows: libgkrellm.a
CFLAGS="${CFLAGS} -D_WIN32_WINNT=0x0501 -DWINVER=0x0501 -DWIN32_LEAN_AND_MEAN" \
LINK_FLAGS="${LINK_FLAGS} -mwindows" \
EXTRAOBJS="${EXTRAOBJS} winops-win32.o win32-plugin.o win32-resource.o" \
SYS_LIBS="-lws2_32 -lwtsapi32 -lpdh -lnetapi32 -liphlpapi -lntdll -lintl" \
SYS_LIBS="-lws2_32 -lwtsapi32 -lpdh -lnetapi32 -liphlpapi -lintl" \
X11_LIBS="" \
UNIXOBJS="" \
gkrellm
@ -236,7 +236,7 @@ install_solaris:
chgrp sys $(INSTALLDIR)/$(PACKAGE)
chmod g+s $(INSTALLDIR)/$(PACKAGE)
install_windows:
install_windows: windows
$(MAKE) BINEXT=".exe" install_bin install_inc
$(INSTALL) -d -m $(LIBDIRMODE) $(LIBDIR)
$(INSTALL) -c -m $(BINMODE) libgkrellm.a $(LIBDIR)

View File

@ -69,7 +69,7 @@
#define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L)
#define SystemPagefileInformation 18
NTSTATUS NTAPI ZwQuerySystemInformation(
typedef NTSTATUS (NTAPI *pfZwQuerySystemInformation)(
/*IN*/ UINT SystemInformationClass,
/*IN OUT*/ VOID *SystemInformation,
/*IN*/ ULONG SystemInformationLength,
@ -92,7 +92,7 @@ typedef struct _SYSTEM_PAGEFILE_INFORMATION
* are present in the headers provided by mingw-w64.
* Docs: http://msdn.microsoft.com/en-us/library/aa378290(VS.85).aspx
*/
#if defined(__MINGW32__) && !defined(WIN64)
#if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
typedef struct _SECURITY_LOGON_SESSION_DATA
{
ULONG Size;
@ -162,6 +162,7 @@ static const wchar_t* WTSAPI32 = L"WTSAPI32.DLL";
static void gkrellm_sys_cpu_cleanup(void);
static void gkrellm_sys_disk_cleanup(void);
static void gkrellm_sys_mem_cleanup(void);
static void gkrellm_sys_inet_cleanup(void);
static void gkrellm_sys_net_cleanup(void);
static void gkrellm_sys_proc_cleanup(void);
@ -425,6 +426,9 @@ void gkrellm_sys_main_init(void)
WSADATA wsdata;
int err;
// Remove current working directory from DLL search path
SetDllDirectoryW(L"");
gkrellm_debug(DEBUG_SYSDEP, "Starting Winsock\n");
err = WSAStartup(MAKEWORD(1,1), &wsdata);
if (err != 0)
@ -470,6 +474,7 @@ void gkrellm_sys_main_cleanup(void)
gkrellm_sys_cpu_cleanup();
gkrellm_sys_disk_cleanup();
gkrellm_sys_net_cleanup();
gkrellm_sys_inet_cleanup();
gkrellm_sys_proc_cleanup();
gkrellm_sys_mem_cleanup();
@ -1724,6 +1729,7 @@ gkrellm_sys_proc_init(void)
static void
gkrellm_sys_proc_cleanup(void)
{
gkrellm_debug(DEBUG_SYSDEP, "Cleanup process monitoring\n");
// Unload secur32.dll and invalidate function pointers
pfLELS = NULL;
pfLFRB = NULL;
@ -1760,6 +1766,8 @@ typedef BOOL (WINAPI *pfGetPerformanceInfo)(PERFORMANCE_INFORMATION *, DWORD);
static HINSTANCE hPsapi = NULL;
static pfGetPerformanceInfo pGPI = NULL;
static DWORD page_size = 1;
static HINSTANCE hNtdll = NULL;
static pfZwQuerySystemInformation pZwQSI = NULL;
void
gkrellm_sys_mem_read_data(void)
@ -1818,6 +1826,9 @@ gkrellm_sys_swap_read_data(void)
SYSTEM_PAGEFILE_INFORMATION *pInfo;
LPVOID pBuf = NULL;
if (pZwQSI == NULL)
return;
gkrellm_debug(DEBUG_SYSDEP, "Checking swap utilization\n");
// it is difficult to determine beforehand which size of the
@ -1828,8 +1839,7 @@ gkrellm_sys_swap_read_data(void)
{
pBuf = g_malloc(szBuf);
ntstatus = ZwQuerySystemInformation(SystemPagefileInformation, pBuf,
szBuf, NULL);
ntstatus = pZwQSI(SystemPagefileInformation, pBuf, szBuf, NULL);
if (ntstatus == STATUS_INFO_LENGTH_MISMATCH)
{
// Buffer was too small, double its size and try again
@ -1877,14 +1887,15 @@ gkrellm_sys_mem_init(void)
GetSystemInfo(&si);
page_size = si.dwPageSize;
hPsapi = LoadLibraryW(L"PSAPI.DLL");
hPsapi = LoadLibraryW(L"psapi.dll");
if (hPsapi)
{
gkrellm_debug(DEBUG_SYSDEP, "Loaded psapi.dll\n");
pGPI = (pfGetPerformanceInfo)GetProcAddress(hPsapi, "GetPerformanceInfo");
if (pGPI == NULL)
{
gkrellm_debug(DEBUG_SYSDEP, "No GetPerformanceInfo() in " \
"PSAPI.DLL, cache-memory will stay at 0!\n");
"psapi.dll, cache-memory will stay at 0!\n");
}
}
else
@ -1892,20 +1903,41 @@ gkrellm_sys_mem_init(void)
win32_warning(NULL, GetLastError(), "Could not load PSAPI.DLL");
}
hNtdll = LoadLibraryW(L"ntdll.dll");
if (hNtdll)
{
gkrellm_debug(DEBUG_SYSDEP, "Loaded ntdll.dll\n");
pZwQSI = (pfZwQuerySystemInformation)GetProcAddress(hNtdll, "ZwQuerySystemInformation");
if (pZwQSI == NULL)
{
gkrellm_debug(DEBUG_SYSDEP, "No ZwQuerySystemInformation() in " \
"ntdll.dll, pagefile-usage cannot be determined.\n");
}
}
else
{
win32_warning(NULL, GetLastError(), "Could not load ntdll.dll");
}
return TRUE;
}
static void
gkrellm_sys_mem_cleanup(void)
{
gkrellm_debug(DEBUG_SYSDEP, "Cleanup memory monitoring\n");
pGPI = NULL;
if (hPsapi != NULL)
FreeLibrary(hPsapi);
hPsapi = NULL;
pZwQSI = NULL;
if (hNtdll != NULL)
FreeLibrary(hNtdll);
hNtdll = NULL;
}
/* ===================================================================== */
/* Battery monitor interface */
/* ===================================================================== */
@ -2138,6 +2170,15 @@ gboolean gkrellm_sys_inet_init(void)
}
return TRUE;
}
static void
gkrellm_sys_inet_cleanup(void)
{
gkrellm_debug(DEBUG_SYSDEP, "Cleanup inet port monitoring\n");
pfGetTcp6Table = NULL;
if (hIphlpapi != NULL)
FreeLibrary(hIphlpapi);
hIphlpapi = NULL;
}
static void win32_read_tcp_data(void)
{