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("%s(\"%s\", %x, %x, ..., %x, %x, ...) = [%i]", |
---|
72 | __func__, lpFileName, dwDesiredAccess, dwShareMode, |
---|
73 | dwCreationDisposition, 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 | fprintf(stderr, "CreateFileW(?)\n"); |
---|
85 | return ORIG(CreateFileW)(lpFileName, dwDesiredAccess, dwShareMode, |
---|
86 | lpSecurityAttributes, dwCreationDisposition, |
---|
87 | dwFlagsAndAttributes, hTemplateFile); |
---|
88 | } |
---|
89 | #endif |
---|
90 | |
---|
91 | /* |
---|
92 | * ReadFile |
---|
93 | */ |
---|
94 | |
---|
95 | #if defined HAVE_READFILE |
---|
96 | BOOL __stdcall NEW(ReadFile)(HANDLE hFile, LPVOID lpBuffer, |
---|
97 | DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead, |
---|
98 | LPOVERLAPPED lpOverlapped) |
---|
99 | { |
---|
100 | fprintf(stderr, "ReadFile(%i)\n", nNumberOfBytesToRead); |
---|
101 | return ORIG(ReadFile)(hFile, lpBuffer, nNumberOfBytesToRead, |
---|
102 | lpNumberOfBytesRead, lpOverlapped); |
---|
103 | } |
---|
104 | #endif |
---|
105 | |
---|
106 | /* |
---|
107 | * CloseHandle |
---|
108 | */ |
---|
109 | |
---|
110 | #if defined HAVE_CLOSEHANDLE |
---|
111 | BOOL __stdcall NEW(CloseHandle)(HANDLE hObject) |
---|
112 | { |
---|
113 | fprintf(stderr, "CloseHandle(%i)\n", hObject); |
---|
114 | return ORIG(CloseHandle)(hObject); |
---|
115 | } |
---|
116 | #endif |
---|
117 | |
---|
118 | /* Win32 function table */ |
---|
119 | #if defined _WIN32 |
---|
120 | # define DIVERT(x) { "kernel32.dll", #x, \ |
---|
121 | (void **)&x##_orig, (void *)x##_new } |
---|
122 | # define DIVERT_END { NULL, NULL, NULL, NULL } |
---|
123 | |
---|
124 | zzuf_table_t table_win32[] = |
---|
125 | { |
---|
126 | #if defined HAVE_CLOSEHANDLE |
---|
127 | DIVERT(CloseHandle), |
---|
128 | #endif |
---|
129 | #if defined HAVE_CREATEFILEA |
---|
130 | DIVERT(CreateFileA), |
---|
131 | #endif |
---|
132 | #if defined HAVE_CREATEFILEW |
---|
133 | DIVERT(CreateFileW), |
---|
134 | #endif |
---|
135 | #if defined HAVE_READFILE |
---|
136 | DIVERT(ReadFile), |
---|
137 | #endif |
---|
138 | DIVERT_END |
---|
139 | }; |
---|
140 | #endif |
---|
141 | |
---|