add new hook for windows (CreateFileMapping(A|W), MapViewOfFile, ReadFileEx), re-enable option -U, start to port network on windows

This commit is contained in:
Kévin Szkudłapski 2012-08-03 17:26:43 +00:00 committed by wisk
parent 22b40f04a0
commit 6b45a58b96
4 changed files with 156 additions and 13 deletions

View File

@ -28,6 +28,9 @@
#define HAVE_CONNECT 1
#define HAVE_CREATEFILEA 1
#define HAVE_CREATEFILEW 1
#define HAVE_CREATEFILEMAPPINGA 1
#define HAVE_CREATEFILEMAPPINGW 1
#define HAVE_MAPVIEWOFFILE 1
/* #undef HAVE_DLADDR */
/* #undef HAVE_DLFCN_H */
#define HAVE_DUP 1
@ -73,6 +76,7 @@
/* #undef HAVE_PREAD */
#define HAVE_PROCESS_H 1
#define HAVE_READFILE 1
#define HAVE_READFILEEX 1
/* #undef HAVE_READV */
#define HAVE_RECV 1
#define HAVE_RECVFROM 1

View File

@ -57,6 +57,21 @@ static HANDLE (__stdcall *ORIG(ReOpenFile))(HANDLE, DWORD,
static BOOL (__stdcall *ORIG(ReadFile))(HANDLE, LPVOID, DWORD, LPDWORD,
LPOVERLAPPED);
#endif
#if defined HAVE_READFILEEX
static BOOL (__stdcall *ORIG(ReadFileEx))(HANDLE, LPVOID, DWORD, LPDWORD,
LPOVERLAPPED, LPOVERLAPPED_COMPLETION_ROUTINE);
#endif
#if defined HAVE_CREATEFILEMAPPINGA
static HANDLE (__stdcall *ORIG(CreateFileMappingA))(HANDLE, LPSECURITY_ATTRIBUTES,
DWORD, DWORD, DWORD, LPCSTR);
#endif
#if defined HAVE_CREATEFILEMAPPINGW
static HANDLE (__stdcall *ORIG(CreateFileMappingW))(HANDLE, LPSECURITY_ATTRIBUTES,
DWORD, DWORD, DWORD, LPCWSTR);
#endif
#ifdef HAVE_MAPVIEWOFFILE
static LPVOID (__stdcall *ORIG(MapViewOfFile))(HANDLE, DWORD, DWORD, DWORD, SIZE_T);
#endif
#if defined HAVE_CLOSEHANDLE
static BOOL (__stdcall *ORIG(CloseHandle))(HANDLE);
#endif
@ -141,7 +156,7 @@ BOOL __stdcall NEW(ReadFile)(HANDLE hFile, LPVOID lpBuffer,
BOOL ret;
ret = ORIG(ReadFile)(hFile, lpBuffer, nNumberOfBytesToRead,
lpNumberOfBytesRead, lpOverlapped);
debug("ReadFile(%#08x, %#08x, %#08x, %#08x, %#08x) = %s",
debug("ReadFile(%#08x, %p, %#08x, %#08x, %p) = %s",
hFile, lpBuffer, nNumberOfBytesToRead, lpNumberOfBytesRead, lpOverlapped, (ret ? "TRUE" : "FALSE"));
if (!_zz_ready || !_zz_iswatched(hFile) /*|| !_zz_hostwatched(hFile)*/ || _zz_islocked(hFile) || !_zz_isactive(hFile))
@ -158,6 +173,100 @@ BOOL __stdcall NEW(ReadFile)(HANDLE hFile, LPVOID lpBuffer,
}
#endif
#if defined HAVE_READFILEEX
BOOL __stdcall NEW(ReadFileEx)(HANDLE hFile, LPVOID lpBuffer,
DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead,
LPOVERLAPPED lpOverlapped, LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
{
BOOL ret;
ret = ORIG(ReadFileEx)(hFile, lpBuffer, nNumberOfBytesToRead,
lpNumberOfBytesRead, lpOverlapped, lpCompletionRoutine);
debug("ReadFileEx(%#08x, %p, %#08x, %p, %p, %p) = %s",
hFile, lpBuffer, nNumberOfBytesToRead, lpNumberOfBytesRead, lpOverlapped, lpCompletionRoutine, (ret ? "TRUE" : "FALSE"));
if (!_zz_ready || !_zz_iswatched(hFile) /*|| !_zz_hostwatched(hFile)*/ || _zz_islocked(hFile) || !_zz_isactive(hFile))
return ret;
if (ret)
{
DWORD bytes_read = lpNumberOfBytesRead ? *lpNumberOfBytesRead : nNumberOfBytesToRead;
debug("fuzzing file %#08x\n", hFile);
_zz_fuzz(hFile, lpBuffer, bytes_read);
_zz_addpos(hFile, bytes_read);
}
return ret;
}
#endif
#if defined HAVE_CREATEFILEMAPPINGA
HANDLE __stdcall NEW(CreateFileMappingA)(HANDLE hFile, LPSECURITY_ATTRIBUTES lpAttributes,
DWORD flProtect, DWORD dwMaximumSizeHigh, DWORD dwMaximumSizeLow,
LPCSTR lpName)
{
HANDLE ret;
ret = ORIG(CreateFileMappingA)(hFile, lpAttributes,
flProtect, dwMaximumSizeHigh, dwMaximumSizeLow,
lpName);
debug("CreateFileMappingA(%#08x, %#08x, %#08x, %#08x, %#08x, %s) = %#08x",
hFile, lpAttributes, flProtect, dwMaximumSizeHigh, dwMaximumSizeLow, lpName, ret);
if (ret == NULL) return ret;
if (!_zz_ready || !_zz_iswatched(hFile) /*|| !_zz_hostwatched(hFile)*/ || _zz_islocked(hFile) || !_zz_isactive(hFile) || _zz_islocked(-1))
return ret;
debug("handle %#08x is registered", ret);
_zz_register(ret);
return ret;
}
#endif
#if defined HAVE_CREATEFILEMAPPINGW
HANDLE __stdcall NEW(CreateFileMappingW)(HANDLE hFile, LPSECURITY_ATTRIBUTES lpAttributes,
DWORD flProtect, DWORD dwMaximumSizeHigh, DWORD dwMaximumSizeLow,
LPCWSTR lpName)
{
HANDLE ret;
ret = ORIG(CreateFileMappingW)(hFile, lpAttributes,
flProtect, dwMaximumSizeHigh, dwMaximumSizeLow,
lpName);
debug("CreateFileMappingW(%#08x, %#08x, %#08x, %#08x, %#08x, %S) = %#08x",
hFile, lpAttributes, flProtect, dwMaximumSizeHigh, dwMaximumSizeLow, lpName, ret);
if (ret == NULL) return ret;
if (!_zz_ready || !_zz_iswatched(hFile) /*|| !_zz_hostwatched(hFile)*/ || _zz_islocked(hFile) || !_zz_isactive(hFile) || _zz_islocked(-1))
return ret;
debug("handle %#08x is registered", ret);
_zz_register(ret);
return ret;
}
#endif
#ifdef HAVE_MAPVIEWOFFILE
LPVOID __stdcall NEW(MapViewOfFile)(HANDLE hFileMappingObject, DWORD dwDesiredAccess,
DWORD dwFileOffsetHigh, DWORD dwFileOffsetLow,
SIZE_T dwNumberOfBytesToMap)
{
LPVOID ret;
ret = ORIG(MapViewOfFile)(hFileMappingObject, dwDesiredAccess,
dwFileOffsetHigh, dwFileOffsetLow,
dwNumberOfBytesToMap);
debug("MapViewOfFile(%#08x, %#08x, %#08x, %#08x, %#08x) = %p",
hFileMappingObject, dwDesiredAccess, dwFileOffsetHigh, dwFileOffsetLow, dwNumberOfBytesToMap, ret);
return ret;
}
#endif
/*
* CloseHandle
*/
@ -189,7 +298,11 @@ zzuf_table_t table_win32[] =
DIVERT(CloseHandle),
DIVERT(CreateFileA),
DIVERT(CreateFileW),
DIVERT(CreateFileMappingA),
DIVERT(CreateFileMappingW),
DIVERT(MapViewOfFile),
DIVERT(ReadFile),
DIVERT(ReadFileEx),
DIVERT_END
};
#endif

View File

@ -28,6 +28,9 @@
# include <sys/socket.h>
# include <netinet/in.h>
# include <arpa/inet.h>
#elif defined HAVE_WINSOCK2_H
# include <WinSock2.h>
# include <WS2tcpip.h>
#endif
#include "libzzuf.h"
@ -35,7 +38,7 @@
#include "ranges.h"
#include "network.h"
#if defined HAVE_SYS_SOCKET_H
#if defined HAVE_SYS_SOCKET_H || defined (HAVE_WINDOWS_H)
static unsigned int get_socket_ip(int);
static int host_in_list(unsigned int, unsigned int const *);
static unsigned int *create_host_list(char const *, unsigned int *);
@ -53,12 +56,15 @@ static int64_t static_ports[512];
void _zz_network_init(void)
{
;
#ifdef HAVE_WINSOCK2_H
WSADATA wsa_data;
WSAStartup(MAKEWORD(2,2), &wsa_data); /* LATER: handle error */
#endif
}
void _zz_network_fini(void)
{
#if defined HAVE_SYS_SOCKET_H
#if defined HAVE_SYS_SOCKET_H || defined (HAVE_WINDOWS_H)
if(ports != static_ports)
free(ports);
if(allow != static_allow)
@ -66,32 +72,36 @@ void _zz_network_fini(void)
if(deny != static_deny)
free(deny);
#endif
#if defined HAVE_WINSOCK2_H
WSACleanup(); /* LATER: handle error */
#endif
}
void _zz_allow(char const *allowlist)
{
#if defined HAVE_SYS_SOCKET_H
#if defined HAVE_SYS_SOCKET_H || defined (HAVE_WINDOWS_H)
allow = create_host_list(allowlist, static_allow);
#endif
}
void _zz_deny(char const *denylist)
{
#if defined HAVE_SYS_SOCKET_H
#if defined HAVE_SYS_SOCKET_H || defined (HAVE_WINDOWS_H)
deny = create_host_list(denylist, static_deny);
#endif
}
void _zz_ports(char const *portlist)
{
#if defined HAVE_SYS_SOCKET_H
#if defined HAVE_SYS_SOCKET_H || defined (HAVE_WINDOWS_H)
ports = _zz_allocrange(portlist, static_ports);
#endif
}
int _zz_portwatched(int port)
{
#if defined HAVE_SYS_SOCKET_H
#if defined HAVE_SYS_SOCKET_H || defined (HAVE_WINDOWS_H)
if(!ports)
return 1;
@ -103,7 +113,7 @@ int _zz_portwatched(int port)
int _zz_hostwatched(int sock)
{
#if defined HAVE_SYS_SOCKET_H
#if defined HAVE_SYS_SOCKET_H || defined (HAVE_WINDOWS_H)
int watch = 1;
unsigned int ip;
@ -125,7 +135,7 @@ int _zz_hostwatched(int sock)
/* XXX: the following functions are local */
#if defined HAVE_SYS_SOCKET_H
#if defined HAVE_SYS_SOCKET_H || defined HAVE_WINSOCK2_H
static unsigned int *create_host_list(char const *list,
unsigned int *static_list)
{
@ -166,7 +176,7 @@ static unsigned int *create_host_list(char const *list,
parser++;
}
ret = inet_aton(buf, &addr);
ret = inet_pton(AF_INET, buf, &addr);
if (ret)
iplist[i++] = addr.s_addr;
else

View File

@ -791,12 +791,12 @@ static void spawn_children(struct opts *opts)
static void clean_children(struct opts *opts)
{
#if defined HAVE_KILL
#if defined HAVE_KILL || defined HAVE_WINDOWS_H
int64_t now = _zz_time();
#endif
int i, j;
#if defined HAVE_KILL
#if defined HAVE_KILL || defined HAVE_WINDOWS_H
/* Terminate children if necessary */
for(i = 0; i < opts->maxchild; i++)
{
@ -809,7 +809,13 @@ static void clean_children(struct opts *opts)
finfo(stderr, opts, opts->child[i].seed);
fprintf(stderr, "data output exceeded, sending SIGTERM\n");
}
#if defined HAVE_KILL
kill(opts->child[i].pid, SIGTERM);
#else
/* We must invalidate fd */
memset(opts->child[i].fd, -1, sizeof(opts->child[i].fd));
TerminateProcess(opts->child[i].process_handle, 0x0);
#endif
opts->child[i].date = now;
opts->child[i].status = STATUS_SIGTERM;
}
@ -823,7 +829,13 @@ static void clean_children(struct opts *opts)
finfo(stderr, opts, opts->child[i].seed);
fprintf(stderr, "running time exceeded, sending SIGTERM\n");
}
#if defined HAVE_KILL
kill(opts->child[i].pid, SIGTERM);
#else
/* We must invalidate fd */
memset(opts->child[i].fd, -1, sizeof(opts->child[i].fd));
TerminateProcess(opts->child[i].process_handle, 0x0);
#endif
opts->child[i].date = now;
opts->child[i].status = STATUS_SIGTERM;
}
@ -840,7 +852,11 @@ static void clean_children(struct opts *opts)
finfo(stderr, opts, opts->child[i].seed);
fprintf(stderr, "not responding, sending SIGKILL\n");
}
#if defined HAVE_KILL
kill(opts->child[i].pid, SIGKILL);
#else
TerminateProcess(opts->child[i].process_handle, 0x0);
#endif
opts->child[i].status = STATUS_SIGKILL;
}
}