Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

352 lines
6.2 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. sample.c
  5. Abstract:
  6. This module implements a sample application verifier provider
  7. that hooks malloc/free from kernel32.dll and CloseHandle and
  8. CreateEvent from kernel32.dll
  9. Author:
  10. Silviu Calinoiu (SilviuC) 2-Feb-2001
  11. Revision History:
  12. --*/
  13. #include "pch.h"
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. //
  17. // Thunk replacements (should go into a header)
  18. //
  19. //WINBASEAPI
  20. HANDLE
  21. WINAPI
  22. AVrfpCreateEventW(
  23. IN LPSECURITY_ATTRIBUTES lpEventAttributes,
  24. IN BOOL bManualReset,
  25. IN BOOL bInitialState,
  26. IN LPCWSTR lpName
  27. );
  28. //WINBASEAPI
  29. BOOL
  30. WINAPI
  31. AVrfpCloseHandle(
  32. IN OUT HANDLE hObject
  33. );
  34. PVOID __cdecl
  35. AVrfp_malloc (
  36. IN SIZE_T Size
  37. );
  38. PVOID __cdecl
  39. AVrfp_realloc (
  40. IN PVOID Address,
  41. IN SIZE_T Size
  42. );
  43. VOID __cdecl
  44. AVrfp_free (
  45. IN PVOID Address
  46. );
  47. //
  48. // Callbacks
  49. //
  50. VOID
  51. AVrfpDllLoadCallback (
  52. PWSTR DllName,
  53. PVOID DllBase,
  54. SIZE_T DllSize,
  55. PVOID Reserved
  56. );
  57. VOID
  58. AVrfpDllUnloadCallback (
  59. PWSTR DllName,
  60. PVOID DllBase,
  61. SIZE_T DllSize,
  62. PVOID Reserved
  63. );
  64. //
  65. // kernel32.dll thunks
  66. //
  67. #define AVRF_INDEX_KERNEL32_CREATEEVENT 0
  68. #define AVRF_INDEX_KERNEL32_CLOSEHANDLE 1
  69. RTL_VERIFIER_THUNK_DESCRIPTOR AVrfpKernel32Thunks [] =
  70. {
  71. {"CreateEventW",
  72. NULL,
  73. AVrfpCreateEventW},
  74. {"CloseHandle",
  75. NULL,
  76. AVrfpCloseHandle},
  77. {NULL, NULL, NULL}
  78. };
  79. //
  80. // msvcrt.dll thunks
  81. //
  82. #define AVRF_INDEX_MSVCRT_MALLOC 0
  83. #define AVRF_INDEX_MSVCRT_FREE 1
  84. RTL_VERIFIER_THUNK_DESCRIPTOR AVrfpMsvcrtThunks [] =
  85. {
  86. {"malloc",
  87. NULL,
  88. AVrfp_malloc},
  89. {"free",
  90. NULL,
  91. AVrfp_free},
  92. {NULL, NULL, NULL}
  93. };
  94. //
  95. // dll's providing thunks that will be verified.
  96. //
  97. RTL_VERIFIER_DLL_DESCRIPTOR AVrfpExportDlls [] =
  98. {
  99. {L"kernel32.dll",
  100. 0,
  101. NULL,
  102. AVrfpKernel32Thunks},
  103. {L"msvcrt.dll",
  104. 0,
  105. NULL,
  106. AVrfpMsvcrtThunks},
  107. {NULL, 0, NULL, NULL}
  108. };
  109. RTL_VERIFIER_PROVIDER_DESCRIPTOR AVrfpProvider =
  110. {
  111. sizeof (RTL_VERIFIER_PROVIDER_DESCRIPTOR),
  112. AVrfpExportDlls,
  113. AVrfpDllLoadCallback,
  114. AVrfpDllUnloadCallback,
  115. NULL, // image name (filled by verifier engine)
  116. 0, // verifier flags (filled by verifier engine)
  117. 0, // debug flags (filled by verifier engine)
  118. };
  119. BOOL
  120. WINAPI
  121. DllMain(
  122. HINSTANCE hinstDLL,
  123. DWORD fdwReason,
  124. LPVOID lpvReserved
  125. )
  126. {
  127. switch (fdwReason) {
  128. case DLL_PROCESS_VERIFIER:
  129. DbgPrint ("AVRF: sample verifier provider descriptor @ %p\n",
  130. &AVrfpProvider);
  131. *((PRTL_VERIFIER_PROVIDER_DESCRIPTOR *)lpvReserved) = &AVrfpProvider;
  132. break;
  133. case DLL_PROCESS_ATTACH:
  134. DbgPrint ("AVRF: sample verifier provider initialized \n");
  135. #if 1
  136. malloc (1000);
  137. {
  138. FILE * File;
  139. File = fopen ("_xxx_", "wt");
  140. if (File) {
  141. fputs ("This works.\n", File);
  142. fclose (File);
  143. }
  144. }
  145. #endif
  146. break;
  147. default:
  148. break;
  149. }
  150. return TRUE;
  151. }
  152. PRTL_VERIFIER_THUNK_DESCRIPTOR
  153. AVrfpGetThunkDescriptor (
  154. PRTL_VERIFIER_THUNK_DESCRIPTOR DllThunks,
  155. ULONG Index)
  156. {
  157. PRTL_VERIFIER_THUNK_DESCRIPTOR Thunk = NULL;
  158. Thunk = &(DllThunks[Index]);
  159. if (Thunk->ThunkOldAddress == NULL) {
  160. //
  161. // We shuld always have the original thunk address.
  162. // This gets filed by the verifier support in the NT loader.
  163. //
  164. DbgPrint ("AVRF: no original thunk for %s @ %p \n",
  165. Thunk->ThunkName,
  166. Thunk);
  167. DbgBreakPoint ();
  168. }
  169. return Thunk;
  170. }
  171. #define AVRFP_GET_ORIGINAL_EXPORT(DllThunks, Index) \
  172. (FUNCTION_TYPE)(AVrfpGetThunkDescriptor(DllThunks, Index)->ThunkOldAddress)
  173. //
  174. // Callbacks
  175. //
  176. VOID
  177. AVrfpDllLoadCallback (
  178. PWSTR DllName,
  179. PVOID DllBase,
  180. SIZE_T DllSize,
  181. PVOID Reserved
  182. )
  183. {
  184. DbgPrint (" --- loading %ws \n", DllName);
  185. }
  186. VOID
  187. AVrfpDllUnloadCallback (
  188. PWSTR DllName,
  189. PVOID DllBase,
  190. SIZE_T DllSize,
  191. PVOID Reserved
  192. )
  193. {
  194. DbgPrint (" --- unloading %ws \n", DllName);
  195. }
  196. /////////////////////////////////////////////////////////////////////
  197. ///////////////////////////////////////// msvcrt.dll verified exports
  198. /////////////////////////////////////////////////////////////////////
  199. //WINBASEAPI
  200. HANDLE
  201. WINAPI
  202. AVrfpCreateEventW(
  203. IN LPSECURITY_ATTRIBUTES lpEventAttributes,
  204. IN BOOL bManualReset,
  205. IN BOOL bInitialState,
  206. IN LPCWSTR lpName
  207. )
  208. {
  209. typedef HANDLE (WINAPI * FUNCTION_TYPE) (LPSECURITY_ATTRIBUTES, BOOL, BOOL, LPCWSTR);
  210. FUNCTION_TYPE Function;
  211. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpKernel32Thunks,
  212. AVRF_INDEX_KERNEL32_CREATEEVENT);
  213. return (* Function)(lpEventAttributes,
  214. bManualReset,
  215. bInitialState,
  216. lpName);
  217. }
  218. //WINBASEAPI
  219. BOOL
  220. WINAPI
  221. AVrfpCloseHandle(
  222. IN OUT HANDLE hObject
  223. )
  224. {
  225. typedef BOOL (WINAPI * FUNCTION_TYPE) (HANDLE);
  226. FUNCTION_TYPE Function;
  227. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpKernel32Thunks,
  228. AVRF_INDEX_KERNEL32_CLOSEHANDLE);
  229. if (hObject == NULL) {
  230. DbgPrint ("AVRF: sample: Closing a null handle !!! \n");
  231. }
  232. return (* Function)(hObject);
  233. }
  234. /////////////////////////////////////////////////////////////////////
  235. ///////////////////////////////////////// msvcrt.dll verified exports
  236. /////////////////////////////////////////////////////////////////////
  237. PVOID __cdecl
  238. AVrfp_malloc (
  239. IN SIZE_T Size
  240. )
  241. {
  242. typedef PVOID (__cdecl * FUNCTION_TYPE) (SIZE_T);
  243. FUNCTION_TYPE Function;
  244. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpMsvcrtThunks,
  245. AVRF_INDEX_MSVCRT_MALLOC);
  246. return (* Function)(Size);
  247. }
  248. VOID __cdecl
  249. AVrfp_free (
  250. IN PVOID Address
  251. )
  252. {
  253. typedef VOID (__cdecl * FUNCTION_TYPE) (PVOID);
  254. FUNCTION_TYPE Function;
  255. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpMsvcrtThunks,
  256. AVRF_INDEX_MSVCRT_FREE);
  257. (* Function)(Address);
  258. }