Leaked source code of windows server 2003
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.

968 lines
25 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. hooks.c
  5. Abstract:
  6. This module implements verifier hooks for various
  7. APIs that do not fall in any specific category.
  8. Author:
  9. Silviu Calinoiu (SilviuC) 3-Dec-2001
  10. Revision History:
  11. 3-Dec-2001 (SilviuC): initial version.
  12. --*/
  13. #include "pch.h"
  14. #include "verifier.h"
  15. #include "support.h"
  16. #include "faults.h"
  17. /////////////////////////////////////////////////////////////////////
  18. /////////////////////////////////////////////////////////// Wait APIs
  19. /////////////////////////////////////////////////////////////////////
  20. //WINBASEAPI
  21. DWORD
  22. WINAPI
  23. AVrfpWaitForSingleObject(
  24. IN HANDLE hHandle,
  25. IN DWORD dwMilliseconds
  26. )
  27. {
  28. typedef DWORD (WINAPI * FUNCTION_TYPE) (HANDLE, DWORD);
  29. FUNCTION_TYPE Function;
  30. DWORD Result;
  31. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpKernel32Thunks,
  32. AVRF_INDEX_KERNEL32_WAITFORSINGLEOBJECT);
  33. BUMP_COUNTER (CNT_WAIT_SINGLE_CALLS);
  34. if (dwMilliseconds != INFINITE && dwMilliseconds != 0) {
  35. BUMP_COUNTER (CNT_WAIT_WITH_TIMEOUT_CALLS);
  36. if (SHOULD_FAULT_INJECT(CLS_WAIT_APIS)) {
  37. BUMP_COUNTER (CNT_WAIT_WITH_TIMEOUT_FAILS);
  38. CHECK_BREAK (BRK_WAIT_WITH_TIMEOUT_FAIL);
  39. return WAIT_TIMEOUT;
  40. }
  41. }
  42. Result = (* Function) (hHandle, dwMilliseconds);
  43. //
  44. // No matter if it fails or not we will introduce a random delay
  45. // in order to randomize the timings in the process.
  46. //
  47. if ((AVrfpProvider.VerifierFlags & RTL_VRF_FLG_RACE_CHECKS)) {
  48. AVrfpCreateRandomDelay ();
  49. }
  50. return Result;
  51. }
  52. //WINBASEAPI
  53. DWORD
  54. WINAPI
  55. AVrfpWaitForMultipleObjects(
  56. IN DWORD nCount,
  57. IN CONST HANDLE *lpHandles,
  58. IN BOOL bWaitAll,
  59. IN DWORD dwMilliseconds
  60. )
  61. {
  62. typedef DWORD (WINAPI * FUNCTION_TYPE) (DWORD, CONST HANDLE *, BOOL, DWORD);
  63. FUNCTION_TYPE Function;
  64. DWORD Result;
  65. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpKernel32Thunks,
  66. AVRF_INDEX_KERNEL32_WAITFORMULTIPLEOBJECTS);
  67. BUMP_COUNTER (CNT_WAIT_MULTIPLE_CALLS);
  68. if (dwMilliseconds != INFINITE && dwMilliseconds != 0) {
  69. BUMP_COUNTER (CNT_WAIT_WITH_TIMEOUT_CALLS);
  70. if (SHOULD_FAULT_INJECT(CLS_WAIT_APIS)) {
  71. BUMP_COUNTER (CNT_WAIT_WITH_TIMEOUT_FAILS);
  72. CHECK_BREAK (BRK_WAIT_WITH_TIMEOUT_FAIL);
  73. return WAIT_TIMEOUT;
  74. }
  75. }
  76. Result = (* Function) (nCount, lpHandles, bWaitAll, dwMilliseconds);
  77. //
  78. // No matter if it fails or not we will introduce a random delay
  79. // in order to randomize the timings in the process.
  80. //
  81. if ((AVrfpProvider.VerifierFlags & RTL_VRF_FLG_RACE_CHECKS)) {
  82. AVrfpCreateRandomDelay ();
  83. }
  84. return Result;
  85. }
  86. //WINBASEAPI
  87. DWORD
  88. WINAPI
  89. AVrfpWaitForSingleObjectEx(
  90. IN HANDLE hHandle,
  91. IN DWORD dwMilliseconds,
  92. IN BOOL bAlertable
  93. )
  94. {
  95. typedef DWORD (WINAPI * FUNCTION_TYPE) (HANDLE, DWORD, BOOL);
  96. FUNCTION_TYPE Function;
  97. DWORD Result;
  98. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpKernel32Thunks,
  99. AVRF_INDEX_KERNEL32_WAITFORSINGLEOBJECTEX);
  100. BUMP_COUNTER (CNT_WAIT_SINGLEEX_CALLS);
  101. if (dwMilliseconds != INFINITE && dwMilliseconds != 0) {
  102. BUMP_COUNTER (CNT_WAIT_WITH_TIMEOUT_CALLS);
  103. if (SHOULD_FAULT_INJECT(CLS_WAIT_APIS)) {
  104. BUMP_COUNTER (CNT_WAIT_WITH_TIMEOUT_FAILS);
  105. CHECK_BREAK (BRK_WAIT_WITH_TIMEOUT_FAIL);
  106. return WAIT_TIMEOUT;
  107. }
  108. }
  109. Result = (* Function) (hHandle, dwMilliseconds, bAlertable);
  110. //
  111. // No matter if it fails or not we will introduce a random delay
  112. // in order to randomize the timings in the process.
  113. //
  114. if ((AVrfpProvider.VerifierFlags & RTL_VRF_FLG_RACE_CHECKS)) {
  115. AVrfpCreateRandomDelay ();
  116. }
  117. return Result;
  118. }
  119. //WINBASEAPI
  120. DWORD
  121. WINAPI
  122. AVrfpWaitForMultipleObjectsEx(
  123. IN DWORD nCount,
  124. IN CONST HANDLE *lpHandles,
  125. IN BOOL bWaitAll,
  126. IN DWORD dwMilliseconds,
  127. IN BOOL bAlertable
  128. )
  129. {
  130. typedef DWORD (WINAPI * FUNCTION_TYPE) (DWORD, CONST HANDLE *, BOOL, DWORD, BOOL);
  131. FUNCTION_TYPE Function;
  132. DWORD Result;
  133. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpKernel32Thunks,
  134. AVRF_INDEX_KERNEL32_WAITFORMULTIPLEOBJECTSEX);
  135. BUMP_COUNTER (CNT_WAIT_MULTIPLEEX_CALLS);
  136. if (dwMilliseconds != INFINITE && dwMilliseconds != 0) {
  137. BUMP_COUNTER (CNT_WAIT_WITH_TIMEOUT_CALLS);
  138. if (SHOULD_FAULT_INJECT(CLS_WAIT_APIS)) {
  139. BUMP_COUNTER (CNT_WAIT_WITH_TIMEOUT_FAILS);
  140. CHECK_BREAK (BRK_WAIT_WITH_TIMEOUT_FAIL);
  141. return WAIT_TIMEOUT;
  142. }
  143. }
  144. Result = (* Function) (nCount, lpHandles, bWaitAll, dwMilliseconds, bAlertable);
  145. //
  146. // No matter if it fails or not we will introduce a random delay
  147. // in order to randomize the timings in the process.
  148. //
  149. if ((AVrfpProvider.VerifierFlags & RTL_VRF_FLG_RACE_CHECKS)) {
  150. AVrfpCreateRandomDelay ();
  151. }
  152. return Result;
  153. }
  154. //NTSYSCALLAPI
  155. NTSTATUS
  156. NTAPI
  157. AVrfpNtWaitForSingleObject(
  158. IN HANDLE Handle,
  159. IN BOOLEAN Alertable,
  160. IN PLARGE_INTEGER Timeout OPTIONAL
  161. )
  162. {
  163. NTSTATUS Status;
  164. //
  165. // Verify that it is legal to wait for this object
  166. // in the current state. One example of illegal wait
  167. // is waiting for a thread object while holding the
  168. // loader lock. That thread will need the loader lock
  169. // when calling ExitThread so it will most likely
  170. // deadlock with the current thread.
  171. //
  172. AVrfpVerifyLegalWait (&Handle,
  173. 1,
  174. TRUE);
  175. //
  176. // Call the original function.
  177. //
  178. Status = NtWaitForSingleObject (Handle,
  179. Alertable,
  180. Timeout);
  181. return Status;
  182. }
  183. //NTSYSCALLAPI
  184. NTSTATUS
  185. NTAPI
  186. AVrfpNtWaitForMultipleObjects(
  187. IN ULONG Count,
  188. IN HANDLE Handles[],
  189. IN WAIT_TYPE WaitType,
  190. IN BOOLEAN Alertable,
  191. IN PLARGE_INTEGER Timeout OPTIONAL
  192. )
  193. {
  194. NTSTATUS Status;
  195. //
  196. // Verify that it is legal to wait for these objects
  197. // in the current state. One example of illegal wait
  198. // is waiting for a thread object while holding the
  199. // loader lock. That thread will need the loader lock
  200. // when calling ExitThread so it will most likely
  201. // deadlock with the current thread.
  202. //
  203. AVrfpVerifyLegalWait (Handles,
  204. Count,
  205. (WaitType == WaitAll));
  206. //
  207. // Call the original function.
  208. //
  209. Status = NtWaitForMultipleObjects (Count,
  210. Handles,
  211. WaitType,
  212. Alertable,
  213. Timeout);
  214. return Status;
  215. }
  216. /////////////////////////////////////////////////////////////////////
  217. /////////////////////////////////////////////////////////// File APIs
  218. /////////////////////////////////////////////////////////////////////
  219. //NTSYSCALLAPI
  220. NTSTATUS
  221. NTAPI
  222. AVrfpNtCreateFile(
  223. OUT PHANDLE FileHandle,
  224. IN ACCESS_MASK DesiredAccess,
  225. IN POBJECT_ATTRIBUTES ObjectAttributes,
  226. OUT PIO_STATUS_BLOCK IoStatusBlock,
  227. IN PLARGE_INTEGER AllocationSize OPTIONAL,
  228. IN ULONG FileAttributes,
  229. IN ULONG ShareAccess,
  230. IN ULONG CreateDisposition,
  231. IN ULONG CreateOptions,
  232. IN PVOID EaBuffer OPTIONAL,
  233. IN ULONG EaLength
  234. )
  235. {
  236. NTSTATUS Status;
  237. if (SHOULD_FAULT_INJECT(CLS_FILE_APIS)) {
  238. CHECK_BREAK (BRK_CREATE_FILE_FAIL);
  239. return STATUS_NO_MEMORY;
  240. }
  241. Status = NtCreateFile (FileHandle,
  242. DesiredAccess,
  243. ObjectAttributes,
  244. IoStatusBlock,
  245. AllocationSize,
  246. FileAttributes,
  247. ShareAccess,
  248. CreateDisposition,
  249. CreateOptions,
  250. EaBuffer,
  251. EaLength);
  252. return Status;
  253. }
  254. //NTSYSCALLAPI
  255. NTSTATUS
  256. NTAPI
  257. AVrfpNtOpenFile(
  258. OUT PHANDLE FileHandle,
  259. IN ACCESS_MASK DesiredAccess,
  260. IN POBJECT_ATTRIBUTES ObjectAttributes,
  261. OUT PIO_STATUS_BLOCK IoStatusBlock,
  262. IN ULONG ShareAccess,
  263. IN ULONG OpenOptions
  264. )
  265. {
  266. NTSTATUS Status;
  267. if (SHOULD_FAULT_INJECT(CLS_FILE_APIS)) {
  268. CHECK_BREAK (BRK_CREATE_FILE_FAIL);
  269. return STATUS_NO_MEMORY;
  270. }
  271. Status = NtOpenFile (FileHandle,
  272. DesiredAccess,
  273. ObjectAttributes,
  274. IoStatusBlock,
  275. ShareAccess,
  276. OpenOptions);
  277. return Status;
  278. }
  279. //WINBASEAPI
  280. HANDLE
  281. WINAPI
  282. AVrfpCreateFileA(
  283. IN LPCSTR lpFileName,
  284. IN DWORD dwDesiredAccess,
  285. IN DWORD dwShareMode,
  286. IN LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  287. IN DWORD dwCreationDisposition,
  288. IN DWORD dwFlagsAndAttributes,
  289. IN HANDLE hTemplateFile
  290. )
  291. {
  292. typedef HANDLE (WINAPI * FUNCTION_TYPE)
  293. (LPCSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES,
  294. DWORD, DWORD, HANDLE);
  295. FUNCTION_TYPE Function;
  296. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpKernel32Thunks,
  297. AVRF_INDEX_KERNEL32_CREATEFILEA);
  298. if (SHOULD_FAULT_INJECT(CLS_FILE_APIS)) {
  299. CHECK_BREAK (BRK_CREATE_FILE_FAIL);
  300. NtCurrentTeb()->LastErrorValue = ERROR_OUTOFMEMORY;
  301. return INVALID_HANDLE_VALUE;
  302. }
  303. return (* Function) (lpFileName,
  304. dwDesiredAccess,
  305. dwShareMode,
  306. lpSecurityAttributes,
  307. dwCreationDisposition,
  308. dwFlagsAndAttributes,
  309. hTemplateFile);
  310. }
  311. //WINBASEAPI
  312. HANDLE
  313. WINAPI
  314. AVrfpCreateFileW(
  315. IN LPCWSTR lpFileName,
  316. IN DWORD dwDesiredAccess,
  317. IN DWORD dwShareMode,
  318. IN LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  319. IN DWORD dwCreationDisposition,
  320. IN DWORD dwFlagsAndAttributes,
  321. IN HANDLE hTemplateFile
  322. )
  323. {
  324. typedef HANDLE (WINAPI * FUNCTION_TYPE)
  325. (LPCWSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES,
  326. DWORD, DWORD, HANDLE);
  327. FUNCTION_TYPE Function;
  328. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpKernel32Thunks,
  329. AVRF_INDEX_KERNEL32_CREATEFILEW);
  330. if (SHOULD_FAULT_INJECT(CLS_FILE_APIS)) {
  331. CHECK_BREAK (BRK_CREATE_FILE_FAIL);
  332. NtCurrentTeb()->LastErrorValue = ERROR_OUTOFMEMORY;
  333. return INVALID_HANDLE_VALUE;
  334. }
  335. return (* Function) (lpFileName,
  336. dwDesiredAccess,
  337. dwShareMode,
  338. lpSecurityAttributes,
  339. dwCreationDisposition,
  340. dwFlagsAndAttributes,
  341. hTemplateFile);
  342. }
  343. /////////////////////////////////////////////////////////////////////
  344. /////////////////////////////////////////////////////// Registry APIs
  345. /////////////////////////////////////////////////////////////////////
  346. //NTSYSCALLAPI
  347. NTSTATUS
  348. NTAPI
  349. AVrfpNtCreateKey(
  350. OUT PHANDLE KeyHandle,
  351. IN ACCESS_MASK DesiredAccess,
  352. IN POBJECT_ATTRIBUTES ObjectAttributes,
  353. IN ULONG TitleIndex,
  354. IN PUNICODE_STRING Class OPTIONAL,
  355. IN ULONG CreateOptions,
  356. OUT PULONG Disposition OPTIONAL
  357. )
  358. {
  359. NTSTATUS Status;
  360. if (SHOULD_FAULT_INJECT(CLS_REGISTRY_APIS)) {
  361. CHECK_BREAK (BRK_CREATE_KEY_FAIL);
  362. return STATUS_NO_MEMORY;
  363. }
  364. Status = NtCreateKey (KeyHandle,
  365. DesiredAccess,
  366. ObjectAttributes,
  367. TitleIndex,
  368. Class,
  369. CreateOptions,
  370. Disposition);
  371. return Status;
  372. }
  373. //NTSYSCALLAPI
  374. NTSTATUS
  375. NTAPI
  376. AVrfpNtOpenKey(
  377. OUT PHANDLE KeyHandle,
  378. IN ACCESS_MASK DesiredAccess,
  379. IN POBJECT_ATTRIBUTES ObjectAttributes
  380. )
  381. {
  382. NTSTATUS Status;
  383. if (SHOULD_FAULT_INJECT(CLS_REGISTRY_APIS)) {
  384. CHECK_BREAK (BRK_CREATE_KEY_FAIL);
  385. return STATUS_NO_MEMORY;
  386. }
  387. Status = NtOpenKey (KeyHandle,
  388. DesiredAccess,
  389. ObjectAttributes);
  390. return Status;
  391. }
  392. //WINADVAPI
  393. LONG
  394. APIENTRY
  395. AVrfpRegCreateKeyA (
  396. IN HKEY hKey,
  397. IN LPCSTR lpSubKey,
  398. OUT PHKEY phkResult
  399. )
  400. {
  401. typedef LONG (APIENTRY * FUNCTION_TYPE)
  402. (HKEY, LPCSTR, PHKEY);
  403. FUNCTION_TYPE Function;
  404. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpAdvapi32Thunks,
  405. AVRF_INDEX_ADVAPI32_REGCREATEKEYA);
  406. if (SHOULD_FAULT_INJECT(CLS_REGISTRY_APIS)) {
  407. CHECK_BREAK (BRK_CREATE_KEY_FAIL);
  408. return ERROR_OUTOFMEMORY;
  409. }
  410. return (* Function) (hKey, lpSubKey, phkResult);
  411. }
  412. //WINADVAPI
  413. LONG
  414. APIENTRY
  415. AVrfpRegCreateKeyW (
  416. IN HKEY hKey,
  417. IN LPCWSTR lpSubKey,
  418. OUT PHKEY phkResult
  419. )
  420. {
  421. typedef LONG (APIENTRY * FUNCTION_TYPE)
  422. (HKEY, LPCWSTR, PHKEY);
  423. FUNCTION_TYPE Function;
  424. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpAdvapi32Thunks,
  425. AVRF_INDEX_ADVAPI32_REGCREATEKEYW);
  426. if (SHOULD_FAULT_INJECT(CLS_REGISTRY_APIS)) {
  427. CHECK_BREAK (BRK_CREATE_KEY_FAIL);
  428. return ERROR_OUTOFMEMORY;
  429. }
  430. return (* Function) (hKey, lpSubKey, phkResult);
  431. }
  432. //WINADVAPI
  433. LONG
  434. APIENTRY
  435. AVrfpRegCreateKeyExA (
  436. IN HKEY hKey,
  437. IN LPCSTR lpSubKey,
  438. IN DWORD Reserved,
  439. IN LPSTR lpClass,
  440. IN DWORD dwOptions,
  441. IN REGSAM samDesired,
  442. IN LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  443. OUT PHKEY phkResult,
  444. OUT LPDWORD lpdwDisposition
  445. )
  446. {
  447. typedef LONG (APIENTRY * FUNCTION_TYPE)
  448. (HKEY, LPCSTR, DWORD, LPSTR, DWORD,
  449. REGSAM, LPSECURITY_ATTRIBUTES, PHKEY, LPDWORD);
  450. FUNCTION_TYPE Function;
  451. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpAdvapi32Thunks,
  452. AVRF_INDEX_ADVAPI32_REGCREATEKEYEXA);
  453. if (SHOULD_FAULT_INJECT(CLS_REGISTRY_APIS)) {
  454. CHECK_BREAK (BRK_CREATE_KEY_FAIL);
  455. return ERROR_OUTOFMEMORY;
  456. }
  457. return (* Function) (hKey,
  458. lpSubKey,
  459. Reserved,
  460. lpClass,
  461. dwOptions,
  462. samDesired,
  463. lpSecurityAttributes,
  464. phkResult,
  465. lpdwDisposition);
  466. }
  467. //WINADVAPI
  468. LONG
  469. APIENTRY
  470. AVrfpRegCreateKeyExW (
  471. IN HKEY hKey,
  472. IN LPCWSTR lpSubKey,
  473. IN DWORD Reserved,
  474. IN LPWSTR lpClass,
  475. IN DWORD dwOptions,
  476. IN REGSAM samDesired,
  477. IN LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  478. OUT PHKEY phkResult,
  479. OUT LPDWORD lpdwDisposition
  480. )
  481. {
  482. typedef LONG (APIENTRY * FUNCTION_TYPE)
  483. (HKEY, LPCWSTR, DWORD, LPWSTR, DWORD,
  484. REGSAM, LPSECURITY_ATTRIBUTES, PHKEY, LPDWORD);
  485. FUNCTION_TYPE Function;
  486. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpAdvapi32Thunks,
  487. AVRF_INDEX_ADVAPI32_REGCREATEKEYEXW);
  488. if (SHOULD_FAULT_INJECT(CLS_REGISTRY_APIS)) {
  489. CHECK_BREAK (BRK_CREATE_KEY_FAIL);
  490. return ERROR_OUTOFMEMORY;
  491. }
  492. return (* Function) (hKey,
  493. lpSubKey,
  494. Reserved,
  495. lpClass,
  496. dwOptions,
  497. samDesired,
  498. lpSecurityAttributes,
  499. phkResult,
  500. lpdwDisposition);
  501. }
  502. //WINADVAPI
  503. LONG
  504. APIENTRY
  505. AVrfpRegOpenKeyA (
  506. IN HKEY hKey,
  507. IN LPCSTR lpSubKey,
  508. OUT PHKEY phkResult
  509. )
  510. {
  511. typedef LONG (APIENTRY * FUNCTION_TYPE)
  512. (HKEY, LPCSTR, PHKEY);
  513. FUNCTION_TYPE Function;
  514. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpAdvapi32Thunks,
  515. AVRF_INDEX_ADVAPI32_REGOPENKEYA);
  516. if (SHOULD_FAULT_INJECT(CLS_REGISTRY_APIS)) {
  517. CHECK_BREAK (BRK_CREATE_KEY_FAIL);
  518. return ERROR_OUTOFMEMORY;
  519. }
  520. return (* Function)(hKey, lpSubKey, phkResult);
  521. }
  522. //WINADVAPI
  523. LONG
  524. APIENTRY
  525. AVrfpRegOpenKeyW (
  526. IN HKEY hKey,
  527. IN LPCWSTR lpSubKey,
  528. OUT PHKEY phkResult
  529. )
  530. {
  531. typedef LONG (APIENTRY * FUNCTION_TYPE)
  532. (HKEY, LPCWSTR, PHKEY);
  533. FUNCTION_TYPE Function;
  534. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpAdvapi32Thunks,
  535. AVRF_INDEX_ADVAPI32_REGOPENKEYW);
  536. if (SHOULD_FAULT_INJECT(CLS_REGISTRY_APIS)) {
  537. CHECK_BREAK (BRK_CREATE_KEY_FAIL);
  538. return ERROR_OUTOFMEMORY;
  539. }
  540. return (* Function)(hKey, lpSubKey, phkResult);
  541. }
  542. //WINADVAPI
  543. LONG
  544. APIENTRY
  545. AVrfpRegOpenKeyExA (
  546. IN HKEY hKey,
  547. IN LPCSTR lpSubKey,
  548. IN DWORD ulOptions,
  549. IN REGSAM samDesired,
  550. OUT PHKEY phkResult
  551. )
  552. {
  553. typedef LONG (APIENTRY * FUNCTION_TYPE)
  554. (HKEY, LPCSTR, DWORD, REGSAM, PHKEY);
  555. FUNCTION_TYPE Function;
  556. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpAdvapi32Thunks,
  557. AVRF_INDEX_ADVAPI32_REGOPENKEYEXA);
  558. if (SHOULD_FAULT_INJECT(CLS_REGISTRY_APIS)) {
  559. CHECK_BREAK (BRK_CREATE_KEY_FAIL);
  560. return ERROR_OUTOFMEMORY;
  561. }
  562. return (* Function) (hKey,
  563. lpSubKey,
  564. ulOptions,
  565. samDesired,
  566. phkResult);
  567. }
  568. //WINADVAPI
  569. LONG
  570. APIENTRY
  571. AVrfpRegOpenKeyExW (
  572. IN HKEY hKey,
  573. IN LPCWSTR lpSubKey,
  574. IN DWORD ulOptions,
  575. IN REGSAM samDesired,
  576. OUT PHKEY phkResult
  577. )
  578. {
  579. typedef LONG (APIENTRY * FUNCTION_TYPE)
  580. (HKEY, LPCWSTR, DWORD, REGSAM, PHKEY);
  581. FUNCTION_TYPE Function;
  582. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpAdvapi32Thunks,
  583. AVRF_INDEX_ADVAPI32_REGOPENKEYEXW);
  584. if (SHOULD_FAULT_INJECT(CLS_REGISTRY_APIS)) {
  585. CHECK_BREAK (BRK_CREATE_KEY_FAIL);
  586. return ERROR_OUTOFMEMORY;
  587. }
  588. return (* Function) (hKey,
  589. lpSubKey,
  590. ulOptions,
  591. samDesired,
  592. phkResult);
  593. }
  594. /////////////////////////////////////////////////////////////////////
  595. ///////////////////////////////////////////////// Read/Write File I/O
  596. /////////////////////////////////////////////////////////////////////
  597. //NTSYSCALLAPI
  598. NTSTATUS
  599. NTAPI
  600. AVrfpNtReadFile(
  601. IN HANDLE FileHandle,
  602. IN HANDLE Event OPTIONAL,
  603. IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
  604. IN PVOID ApcContext OPTIONAL,
  605. OUT PIO_STATUS_BLOCK IoStatusBlock,
  606. OUT PVOID Buffer,
  607. IN ULONG Length,
  608. IN PLARGE_INTEGER ByteOffset OPTIONAL,
  609. IN PULONG Key OPTIONAL
  610. )
  611. {
  612. NTSTATUS Status;
  613. //
  614. // Fill the I/O buffer with garbage.
  615. //
  616. // silviuc: should we link this fill to some feature?
  617. // In principle the operation of filling memory is peanuts
  618. // compared with the time taken by the i/o operation.
  619. //
  620. RtlFillMemory (Buffer, Length, 0xFA);
  621. //
  622. // Call original API.
  623. //
  624. Status = NtReadFile (FileHandle,
  625. Event,
  626. ApcRoutine,
  627. ApcContext,
  628. IoStatusBlock,
  629. Buffer,
  630. Length,
  631. ByteOffset,
  632. Key);
  633. //
  634. // Asynchronous operations are delayed a bit. This conceivably
  635. // has good chances to randomize timings in the process.
  636. //
  637. if (Status == STATUS_PENDING) {
  638. if ((AVrfpProvider.VerifierFlags & RTL_VRF_FLG_RACE_CHECKS)) {
  639. AVrfpCreateRandomDelay ();
  640. }
  641. }
  642. return Status;
  643. }
  644. //NTSYSCALLAPI
  645. NTSTATUS
  646. NTAPI
  647. AVrfpNtReadFileScatter(
  648. IN HANDLE FileHandle,
  649. IN HANDLE Event OPTIONAL,
  650. IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
  651. IN PVOID ApcContext OPTIONAL,
  652. OUT PIO_STATUS_BLOCK IoStatusBlock,
  653. IN PFILE_SEGMENT_ELEMENT SegmentArray,
  654. IN ULONG Length,
  655. IN PLARGE_INTEGER ByteOffset OPTIONAL,
  656. IN PULONG Key OPTIONAL
  657. )
  658. {
  659. NTSTATUS Status;
  660. //
  661. // silviuc: we should fill these buffers with garbage too.
  662. //
  663. //
  664. // Call original API.
  665. //
  666. Status = NtReadFileScatter (FileHandle,
  667. Event,
  668. ApcRoutine,
  669. ApcContext,
  670. IoStatusBlock,
  671. SegmentArray,
  672. Length,
  673. ByteOffset,
  674. Key);
  675. //
  676. // Asynchronous operations are delayed a bit. This conceivably
  677. // has good chances to randomize timings in the process.
  678. //
  679. if (Status == STATUS_PENDING) {
  680. if ((AVrfpProvider.VerifierFlags & RTL_VRF_FLG_RACE_CHECKS)) {
  681. AVrfpCreateRandomDelay ();
  682. }
  683. }
  684. return Status;
  685. }
  686. //NTSYSCALLAPI
  687. NTSTATUS
  688. NTAPI
  689. AVrfpNtWriteFile(
  690. IN HANDLE FileHandle,
  691. IN HANDLE Event OPTIONAL,
  692. IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
  693. IN PVOID ApcContext OPTIONAL,
  694. OUT PIO_STATUS_BLOCK IoStatusBlock,
  695. IN PVOID Buffer,
  696. IN ULONG Length,
  697. IN PLARGE_INTEGER ByteOffset OPTIONAL,
  698. IN PULONG Key OPTIONAL
  699. )
  700. {
  701. NTSTATUS Status;
  702. //
  703. // Call original API.
  704. //
  705. Status = NtWriteFile (FileHandle,
  706. Event,
  707. ApcRoutine,
  708. ApcContext,
  709. IoStatusBlock,
  710. Buffer,
  711. Length,
  712. ByteOffset,
  713. Key);
  714. //
  715. // Asynchronous operations are delayed a bit. This conceivably
  716. // has good chances to randomize timings in the process.
  717. //
  718. if (Status == STATUS_PENDING) {
  719. if ((AVrfpProvider.VerifierFlags & RTL_VRF_FLG_RACE_CHECKS)) {
  720. AVrfpCreateRandomDelay ();
  721. }
  722. }
  723. return Status;
  724. }
  725. //NTSYSCALLAPI
  726. NTSTATUS
  727. NTAPI
  728. AVrfpNtWriteFileGather(
  729. IN HANDLE FileHandle,
  730. IN HANDLE Event OPTIONAL,
  731. IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
  732. IN PVOID ApcContext OPTIONAL,
  733. OUT PIO_STATUS_BLOCK IoStatusBlock,
  734. IN PFILE_SEGMENT_ELEMENT SegmentArray,
  735. IN ULONG Length,
  736. IN PLARGE_INTEGER ByteOffset OPTIONAL,
  737. IN PULONG Key OPTIONAL
  738. )
  739. {
  740. NTSTATUS Status;
  741. //
  742. // Call original API.
  743. //
  744. Status = NtWriteFileGather (FileHandle,
  745. Event,
  746. ApcRoutine,
  747. ApcContext,
  748. IoStatusBlock,
  749. SegmentArray,
  750. Length,
  751. ByteOffset,
  752. Key);
  753. //
  754. // Asynchronous operations are delayed a bit. This conceivably
  755. // has good chances to randomize timings in the process.
  756. //
  757. if (Status == STATUS_PENDING) {
  758. if ((AVrfpProvider.VerifierFlags & RTL_VRF_FLG_RACE_CHECKS)) {
  759. AVrfpCreateRandomDelay ();
  760. }
  761. }
  762. return Status;
  763. }
  764. /////////////////////////////////////////////////////////////////////
  765. ////////////////////////////////////////////////// Tick count overlap
  766. /////////////////////////////////////////////////////////////////////
  767. //
  768. // Special value so that the counter will overlap in 5 mins.
  769. //
  770. DWORD AVrfpTickCountOffset = 0xFFFFFFFF - 5 * 60 * 1000;
  771. //WINBASEAPI
  772. DWORD
  773. WINAPI
  774. AVrfpGetTickCount(
  775. VOID
  776. )
  777. {
  778. typedef DWORD (WINAPI * FUNCTION_TYPE) (VOID);
  779. FUNCTION_TYPE Function;
  780. DWORD Result;
  781. Function = AVRFP_GET_ORIGINAL_EXPORT (AVrfpKernel32Thunks,
  782. AVRF_INDEX_KERNEL32_GETTICKCOUNT);
  783. Result = (* Function) ();
  784. if ((AVrfpProvider.VerifierFlags & RTL_VRF_FLG_MISCELLANEOUS_CHECKS)) {
  785. return Result + AVrfpTickCountOffset;
  786. }
  787. else {
  788. return Result;
  789. }
  790. }