1 | /* |
---|
2 | * zzuf - general purpose fuzzer |
---|
3 | * Copyright (c) 2006-2010 Sam Hocevar <sam@hocevar.net> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * This program is free software. It comes without any warranty, to |
---|
7 | * the extent permitted by applicable law. You can redistribute it |
---|
8 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
9 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
10 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
11 | */ |
---|
12 | |
---|
13 | /* |
---|
14 | * load-win32.c: loaded Win32 functions |
---|
15 | */ |
---|
16 | |
---|
17 | #include "config.h" |
---|
18 | |
---|
19 | #if defined HAVE_STDINT_H |
---|
20 | # include <stdint.h> |
---|
21 | #elif defined HAVE_INTTYPES_H |
---|
22 | # include <inttypes.h> |
---|
23 | #endif |
---|
24 | |
---|
25 | #include <stdio.h> |
---|
26 | |
---|
27 | #if defined HAVE_WINDOWS_H |
---|
28 | # include <windows.h> |
---|
29 | #endif |
---|
30 | |
---|
31 | #include "common.h" |
---|
32 | #include "libzzuf.h" |
---|
33 | #include "lib-load.h" |
---|
34 | #include "debug.h" |
---|
35 | #include "fuzz.h" |
---|
36 | #include "fd.h" |
---|
37 | |
---|
38 | /* Kernel functions that we divert */ |
---|
39 | #if defined HAVE_CREATEFILEA |
---|
40 | static HANDLE (__stdcall *ORIG(CreateFileA))(LPCTSTR, DWORD, DWORD, |
---|
41 | LPSECURITY_ATTRIBUTES, |
---|
42 | DWORD, DWORD, HANDLE); |
---|
43 | #endif |
---|
44 | #if defined HAVE_CREATEFILEA |
---|
45 | static HANDLE (__stdcall *ORIG(CreateFileW))(LPCWSTR, DWORD, DWORD, |
---|
46 | LPSECURITY_ATTRIBUTES, |
---|
47 | DWORD, DWORD, HANDLE); |
---|
48 | #endif |
---|
49 | #if defined HAVE_READFILE |
---|
50 | static BOOL (__stdcall *ORIG(ReadFile))(HANDLE, LPVOID, DWORD, LPDWORD, |
---|
51 | LPOVERLAPPED); |
---|
52 | #endif |
---|
53 | #if defined HAVE_CLOSEHANDLE |
---|
54 | static BOOL (__stdcall *ORIG(CloseHandle))(HANDLE); |
---|
55 | #endif |
---|
56 | |
---|
57 | /* |
---|
58 | * CreateFileA, CreateFileW |
---|
59 | */ |
---|
60 | |
---|
61 | #if defined HAVE_CREATEFILEA |
---|
62 | HANDLE __stdcall NEW(CreateFileA)(LPCTSTR lpFileName, DWORD dwDesiredAccess, |
---|
63 | DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, |
---|
64 | DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, |
---|
65 | HANDLE hTemplateFile) |
---|
66 | { |
---|
67 | HANDLE ret; |
---|
68 | ret = ORIG(CreateFileA)(lpFileName, dwDesiredAccess, dwShareMode, |
---|
69 | lpSecurityAttributes, dwCreationDisposition, |
---|
70 | dwFlagsAndAttributes, hTemplateFile); |
---|
71 | debug("CreateFileA(\"%s\", 0x%x, 0x%x, ..., 0x%x, 0x%x, ...) = [%i]", |
---|
72 | lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition, |
---|
73 | dwFlagsAndAttributes, (int)ret); |
---|
74 | return ret; |
---|
75 | } |
---|
76 | #endif |
---|
77 | |
---|
78 | #if defined HAVE_CREATEFILEW |
---|
79 | HANDLE __stdcall NEW(CreateFileW)(LPCWSTR lpFileName, DWORD dwDesiredAccess, |
---|
80 | DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, |
---|
81 | DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, |
---|
82 | HANDLE hTemplateFile) |
---|
83 | { |
---|
84 | HANDLE ret; |
---|
85 | ret = ORIG(CreateFileW)(lpFileName, dwDesiredAccess, dwShareMode, |
---|
86 | lpSecurityAttributes, dwCreationDisposition, |
---|
87 | dwFlagsAndAttributes, hTemplateFile); |
---|
88 | debug("CreateFileW(\"%s\", 0x%x, 0x%x, ..., 0x%x, 0x%x, ...) = [%i]", |
---|
89 | lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition, |
---|
90 | dwFlagsAndAttributes, (int)ret); |
---|
91 | return ret; |
---|
92 | } |
---|
93 | #endif |
---|
94 | |
---|
95 | /* |
---|
96 | * ReadFile |
---|
97 | */ |
---|
98 | |
---|
99 | #if defined HAVE_READFILE |
---|
100 | BOOL __stdcall NEW(ReadFile)(HANDLE hFile, LPVOID lpBuffer, |
---|
101 | DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead, |
---|
102 | LPOVERLAPPED lpOverlapped) |
---|
103 | { |
---|
104 | return ORIG(ReadFile)(hFile, lpBuffer, nNumberOfBytesToRead, |
---|
105 | lpNumberOfBytesRead, lpOverlapped); |
---|
106 | } |
---|
107 | #endif |
---|
108 | |
---|
109 | /* |
---|
110 | * CloseHandle |
---|
111 | */ |
---|
112 | |
---|
113 | #if defined HAVE_CLOSEHANDLE |
---|
114 | BOOL __stdcall NEW(CloseHandle)(HANDLE hObject) |
---|
115 | { |
---|
116 | return ORIG(CloseHandle)(hObject); |
---|
117 | } |
---|
118 | #endif |
---|
119 | |
---|
120 | /* Win32 function table */ |
---|
121 | #if defined HAVE_WINDOWS_H |
---|
122 | # define DIVERT(x) { "kernel32.dll", #x, \ |
---|
123 | (void **)&x##_orig, (void *)x##_new } |
---|
124 | # define DIVERT_END { NULL, NULL, NULL, NULL } |
---|
125 | |
---|
126 | zzuf_table_t table_win32[] = |
---|
127 | { |
---|
128 | DIVERT(CloseHandle), |
---|
129 | DIVERT(CreateFileA), |
---|
130 | DIVERT(CreateFileW), |
---|
131 | DIVERT(ReadFile), |
---|
132 | DIVERT_END |
---|
133 | }; |
---|
134 | #endif |
---|