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.

488 lines
12 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. verifier.c
  5. Abstract:
  6. This module implements the standard application verifier provider.
  7. Author:
  8. Silviu Calinoiu (SilviuC) 2-Feb-2001
  9. Revision History:
  10. --*/
  11. //
  12. // IMPORTANT NOTE.
  13. //
  14. // This dll cannot contain non-ntdll dependencies. This way it allows
  15. // verifier to be run system wide including for processes like smss and csrss.
  16. //
  17. // SilviuC: we might decide in the future that it is not worth enforcing this
  18. // restriction and we loose smss/csrss verification but we can do more stuff
  19. // in the verifier dll.
  20. //
  21. #include "pch.h"
  22. #include "verifier.h"
  23. #include "support.h"
  24. #include "settings.h"
  25. //
  26. // ntdll.dll thunks
  27. //
  28. RTL_VERIFIER_THUNK_DESCRIPTOR AVrfpNtdllThunks [] =
  29. {
  30. {"NtAllocateVirtualMemory", NULL, AVrfpNtAllocateVirtualMemory},
  31. {"NtFreeVirtualMemory", NULL, AVrfpNtFreeVirtualMemory},
  32. {"NtMapViewOfSection", NULL, AVrfpNtMapViewOfSection},
  33. {"NtUnmapViewOfSection", NULL, AVrfpNtUnmapViewOfSection},
  34. {"NtCreateSection", NULL, AVrfpNtCreateSection},
  35. {"NtOpenSection", NULL, AVrfpNtOpenSection},
  36. {"NtCreateFile", NULL, AVrfpNtCreateFile},
  37. {"NtOpenFile", NULL, AVrfpNtOpenFile},
  38. {"RtlTryEnterCriticalSection", NULL, AVrfpRtlTryEnterCriticalSection},
  39. {"RtlEnterCriticalSection", NULL, AVrfpRtlEnterCriticalSection},
  40. {"RtlLeaveCriticalSection", NULL, AVrfpRtlLeaveCriticalSection},
  41. {"RtlInitializeCriticalSection", NULL, AVrfpRtlInitializeCriticalSection},
  42. {"RtlInitializeCriticalSectionAndSpinCount", NULL, AVrfpRtlInitializeCriticalSectionAndSpinCount},
  43. {"RtlDeleteCriticalSection", NULL, AVrfpRtlDeleteCriticalSection},
  44. {"NtCreateEvent", NULL, AVrfpNtCreateEvent },
  45. {"NtClose", NULL, AVrfpNtClose},
  46. {"RtlAllocateHeap", NULL, AVrfpRtlAllocateHeap },
  47. {"RtlReAllocateHeap", NULL, AVrfpRtlReAllocateHeap },
  48. {"RtlFreeHeap", NULL, AVrfpRtlFreeHeap },
  49. // {"RtlQueueWorkItem", NULL, AVrfpRtlQueueWorkItem },
  50. // ISSUE: silviuc: these two are tricky (callback can be called several times)
  51. // {"RtlRegisterWait", NULL, AVrfpRtlRegisterWait },
  52. // {"RtlCreateTimer", NULL, AVrfpRtlCreateTimer },
  53. {NULL, NULL, NULL}
  54. };
  55. //
  56. // kernel32.dll thunks
  57. //
  58. RTL_VERIFIER_THUNK_DESCRIPTOR AVrfpKernel32Thunks [] =
  59. {
  60. {"HeapCreate", NULL, AVrfpHeapCreate},
  61. {"HeapDestroy", NULL, AVrfpHeapDestroy},
  62. {"CloseHandle", NULL, AVrfpCloseHandle},
  63. {"ExitThread", NULL, AVrfpExitThread},
  64. {"TerminateThread", NULL, AVrfpTerminateThread},
  65. {"SuspendThread", NULL, AVrfpSuspendThread},
  66. {"TlsAlloc", NULL, AVrfpTlsAlloc},
  67. {"TlsFree", NULL, AVrfpTlsFree},
  68. {"TlsGetValue", NULL, AVrfpTlsGetValue},
  69. {"TlsSetValue", NULL, AVrfpTlsSetValue},
  70. #if 0
  71. {"CreateThread", NULL, AVrfpCreateThread},
  72. #endif
  73. {NULL, NULL, NULL}
  74. };
  75. //
  76. // advapi32.dll thunks
  77. //
  78. RTL_VERIFIER_THUNK_DESCRIPTOR AVrfpAdvapi32Thunks [] =
  79. {
  80. {"RegCreateKeyExW", NULL, AVrfpRegCreateKeyExW},
  81. {NULL, NULL, NULL}
  82. };
  83. //
  84. // msvcrt.dll thunks
  85. //
  86. RTL_VERIFIER_THUNK_DESCRIPTOR AVrfpMsvcrtThunks [] =
  87. {
  88. {"malloc", NULL, AVrfp_malloc},
  89. {"calloc", NULL, AVrfp_calloc},
  90. {"realloc", NULL, AVrfp_realloc},
  91. {"free", NULL, AVrfp_free},
  92. {"??2@YAPAXI@Z", NULL, AVrfp_new},
  93. {"??3@YAXPAX@Z", NULL, AVrfp_delete},
  94. {"??_U@YAPAXI@Z", NULL, AVrfp_newarray},
  95. {"??_V@YAXPAX@Z", NULL, AVrfp_deletearray},
  96. {NULL, NULL, NULL}
  97. };
  98. //
  99. // dll's providing thunks verified.
  100. //
  101. RTL_VERIFIER_DLL_DESCRIPTOR AVrfpExportDlls [] =
  102. {
  103. {L"ntdll.dll", 0, NULL, AVrfpNtdllThunks},
  104. {L"kernel32.dll", 0, NULL, AVrfpKernel32Thunks},
  105. {L"advapi32.dll", 0, NULL, AVrfpAdvapi32Thunks},
  106. {L"msvcrt.dll", 0, NULL, AVrfpMsvcrtThunks},
  107. {NULL, 0, NULL, NULL}
  108. };
  109. RTL_VERIFIER_PROVIDER_DESCRIPTOR AVrfpProvider =
  110. {
  111. sizeof (RTL_VERIFIER_PROVIDER_DESCRIPTOR),
  112. AVrfpExportDlls,
  113. NULL, // not interested in Dll load notifications
  114. NULL, // not interested in Dll unload notifications
  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. //
  120. // Mark if we have been called with PROCESS_ATTACH once.
  121. // In some cases the fusion code loads dynamically kernel32.dll and enforces
  122. // the run of all initialization routines and causes us to get called
  123. // twice.
  124. //
  125. BOOL AVrfpProcessAttachCalled;
  126. BOOL
  127. WINAPI
  128. DllMain(
  129. HINSTANCE hinstDLL, // handle to the DLL module
  130. DWORD fdwReason, // reason for calling function
  131. LPVOID lpvReserved // reserved
  132. )
  133. {
  134. switch (fdwReason) {
  135. case DLL_PROCESS_VERIFIER:
  136. //
  137. // DllMain gets called with this special reason by the verifier engine.
  138. // No code should execute here except passing back the provider
  139. // descriptor.
  140. //
  141. if (lpvReserved) {
  142. *((PRTL_VERIFIER_PROVIDER_DESCRIPTOR *)lpvReserved) = &AVrfpProvider;
  143. }
  144. break;
  145. case DLL_PROCESS_ATTACH:
  146. //
  147. // Execute only minimal code here and avoid too many DLL dependencies.
  148. //
  149. if (! AVrfpProcessAttachCalled) {
  150. AVrfpProcessAttachCalled = TRUE;
  151. if (AVrfpProvider.VerifierImage) {
  152. try {
  153. HandleInitialize();
  154. }
  155. except (EXCEPTION_EXECUTE_HANDLER) {
  156. return FALSE;
  157. }
  158. DbgPrint ("AVRF: verifier.dll provider initialized for %ws with flags 0x%X \n",
  159. AVrfpProvider.VerifierImage,
  160. AVrfpProvider.VerifierFlags);
  161. }
  162. }
  163. break;
  164. default:
  165. break;
  166. }
  167. return TRUE;
  168. }
  169. PRTL_VERIFIER_THUNK_DESCRIPTOR
  170. AVrfpGetThunkDescriptor (
  171. PRTL_VERIFIER_THUNK_DESCRIPTOR DllThunks,
  172. ULONG Index)
  173. {
  174. PRTL_VERIFIER_THUNK_DESCRIPTOR Thunk = NULL;
  175. Thunk = &(DllThunks[Index]);
  176. if (Thunk->ThunkNewAddress == NULL) {
  177. // silviuc: delete
  178. DbgPrint ("AVRF: we do not have a replace for %s !!! \n",
  179. Thunk->ThunkName);
  180. DbgBreakPoint ();
  181. }
  182. return Thunk;
  183. }
  184. /////////////////////////////////////////////////////////////////////
  185. ///////////////////////////////////////// msvcrt.dll verified exports
  186. /////////////////////////////////////////////////////////////////////
  187. LONG AVrfpKernel32Count[8];
  188. //WINBASEAPI
  189. HANDLE
  190. WINAPI
  191. AVrfpHeapCreate(
  192. IN DWORD flOptions,
  193. IN SIZE_T dwInitialSize,
  194. IN SIZE_T dwMaximumSize
  195. )
  196. {
  197. typedef HANDLE (WINAPI * FUNCTION_TYPE) (DWORD, SIZE_T, SIZE_T);
  198. FUNCTION_TYPE Function;
  199. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpKernel32Thunks,
  200. AVRF_INDEX_KERNEL32_HEAPCREATE);
  201. InterlockedIncrement (&(AVrfpKernel32Count[0]));
  202. return (* Function)(flOptions, dwInitialSize, dwMaximumSize);
  203. }
  204. //WINBASEAPI
  205. BOOL
  206. WINAPI
  207. AVrfpHeapDestroy(
  208. IN OUT HANDLE hHeap
  209. )
  210. {
  211. typedef BOOL (WINAPI * FUNCTION_TYPE) (HANDLE);
  212. FUNCTION_TYPE Function;
  213. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpKernel32Thunks,
  214. AVRF_INDEX_KERNEL32_HEAPDESTROY);
  215. InterlockedIncrement (&(AVrfpKernel32Count[1]));
  216. return (* Function)(hHeap);
  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. InterlockedIncrement (&(AVrfpKernel32Count[2]));
  230. return (* Function)(hObject);
  231. }
  232. /////////////////////////////////////////////////////////////////////
  233. ///////////////////////////////////////// msvcrt.dll verified exports
  234. /////////////////////////////////////////////////////////////////////
  235. LONG AVrfpAdvapi32Count[8];
  236. //WINADVAPI
  237. LONG
  238. APIENTRY
  239. AVrfpRegCreateKeyExW (
  240. IN HKEY hKey,
  241. IN LPCWSTR lpSubKey,
  242. IN DWORD Reserved,
  243. IN LPWSTR lpClass,
  244. IN DWORD dwOptions,
  245. IN REGSAM samDesired,
  246. IN LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  247. OUT PHKEY phkResult,
  248. OUT LPDWORD lpdwDisposition
  249. )
  250. {
  251. typedef LONG (APIENTRY * FUNCTION_TYPE)
  252. (HKEY, LPCWSTR, DWORD, LPWSTR, DWORD,
  253. REGSAM, LPSECURITY_ATTRIBUTES, PHKEY, LPDWORD);
  254. FUNCTION_TYPE Function;
  255. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpAdvapi32Thunks,
  256. AVRF_INDEX_ADVAPI32_REGCREATEKEYEXW);
  257. InterlockedIncrement (&(AVrfpAdvapi32Count[0]));
  258. return (* Function) (hKey,
  259. lpSubKey,
  260. Reserved,
  261. lpClass,
  262. dwOptions,
  263. samDesired,
  264. lpSecurityAttributes,
  265. phkResult,
  266. lpdwDisposition);
  267. }
  268. /////////////////////////////////////////////////////////////////////
  269. ///////////////////////////////////////// msvcrt.dll verified exports
  270. /////////////////////////////////////////////////////////////////////
  271. LONG AVrfpMsvcrtCount[8];
  272. PVOID __cdecl
  273. AVrfp_malloc (
  274. IN SIZE_T Size
  275. )
  276. {
  277. typedef PVOID (__cdecl * FUNCTION_TYPE) (SIZE_T);
  278. FUNCTION_TYPE Function;
  279. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpMsvcrtThunks,
  280. AVRF_INDEX_MSVCRT_MALLOC);
  281. InterlockedIncrement (&(AVrfpMsvcrtCount[0]));
  282. return (* Function)(Size);
  283. }
  284. PVOID __cdecl
  285. AVrfp_calloc (
  286. IN SIZE_T Number,
  287. IN SIZE_T Size
  288. )
  289. {
  290. typedef PVOID (__cdecl * FUNCTION_TYPE) (SIZE_T, SIZE_T);
  291. FUNCTION_TYPE Function;
  292. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpMsvcrtThunks,
  293. AVRF_INDEX_MSVCRT_CALLOC);
  294. InterlockedIncrement (&(AVrfpMsvcrtCount[1]));
  295. return (* Function)(Number, Size);
  296. }
  297. PVOID __cdecl
  298. AVrfp_realloc (
  299. IN PVOID Address,
  300. IN SIZE_T Size
  301. )
  302. {
  303. typedef PVOID (__cdecl * FUNCTION_TYPE) (PVOID, SIZE_T);
  304. FUNCTION_TYPE Function;
  305. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpMsvcrtThunks,
  306. AVRF_INDEX_MSVCRT_REALLOC);
  307. InterlockedIncrement (&(AVrfpMsvcrtCount[2]));
  308. return (* Function)(Address, Size);
  309. }
  310. VOID __cdecl
  311. AVrfp_free (
  312. IN PVOID Address
  313. )
  314. {
  315. typedef VOID (__cdecl * FUNCTION_TYPE) (PVOID);
  316. FUNCTION_TYPE Function;
  317. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpMsvcrtThunks,
  318. AVRF_INDEX_MSVCRT_FREE);
  319. InterlockedIncrement (&(AVrfpMsvcrtCount[3]));
  320. (* Function)(Address);
  321. }
  322. PVOID __cdecl
  323. AVrfp_new (
  324. IN SIZE_T Size
  325. )
  326. {
  327. typedef PVOID (__cdecl * FUNCTION_TYPE) (SIZE_T);
  328. FUNCTION_TYPE Function;
  329. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpMsvcrtThunks,
  330. AVRF_INDEX_MSVCRT_NEW);
  331. InterlockedIncrement (&(AVrfpMsvcrtCount[4]));
  332. return (* Function)(Size);
  333. }
  334. VOID __cdecl
  335. AVrfp_delete (
  336. IN PVOID Address
  337. )
  338. {
  339. typedef VOID (__cdecl * FUNCTION_TYPE) (PVOID);
  340. FUNCTION_TYPE Function;
  341. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpMsvcrtThunks,
  342. AVRF_INDEX_MSVCRT_DELETE);
  343. InterlockedIncrement (&(AVrfpMsvcrtCount[5]));
  344. (* Function)(Address);
  345. }
  346. PVOID __cdecl
  347. AVrfp_newarray (
  348. IN SIZE_T Size
  349. )
  350. {
  351. typedef PVOID (__cdecl * FUNCTION_TYPE) (SIZE_T);
  352. FUNCTION_TYPE Function;
  353. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpMsvcrtThunks,
  354. AVRF_INDEX_MSVCRT_NEWARRAY);
  355. InterlockedIncrement (&(AVrfpMsvcrtCount[6]));
  356. return (* Function)(Size);
  357. }
  358. VOID __cdecl
  359. AVrfp_deletearray (
  360. IN PVOID Address
  361. )
  362. {
  363. typedef VOID (__cdecl * FUNCTION_TYPE) (PVOID);
  364. FUNCTION_TYPE Function;
  365. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpMsvcrtThunks,
  366. AVRF_INDEX_MSVCRT_DELETEARRAY);
  367. InterlockedIncrement (&(AVrfpMsvcrtCount[7]));
  368. (* Function)(Address);
  369. }