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.

139 lines
2.3 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. HeapValidateFrees.cpp
  5. ModAbstract:
  6. Verifies the pointer passed to RtlFreeHeap and RtlReAllocateHeap to make
  7. sure they belong to the heap specified
  8. Notes:
  9. This is a general purpose shim.
  10. History:
  11. 04/25/2000 linstev Created
  12. --*/
  13. #include "precomp.h"
  14. IMPLEMENT_SHIM_BEGIN(HeapValidateFrees)
  15. #include "ShimHookMacro.h"
  16. APIHOOK_ENUM_BEGIN
  17. APIHOOK_ENUM_ENTRY(RtlFreeHeap)
  18. APIHOOK_ENUM_ENTRY(RtlReAllocateHeap)
  19. APIHOOK_ENUM_ENTRY(RtlSizeHeap)
  20. APIHOOK_ENUM_END
  21. /*++
  22. Verify that the pointer being freed belongs to the heap.
  23. --*/
  24. BOOL
  25. APIHOOK(RtlFreeHeap)(
  26. PVOID HeapHandle,
  27. ULONG Flags,
  28. PVOID BaseAddress
  29. )
  30. {
  31. BOOL bRet = TRUE;
  32. if (HeapValidate(HeapHandle, 0, BaseAddress))
  33. {
  34. bRet = ORIGINAL_API(RtlFreeHeap)(HeapHandle, Flags, BaseAddress);
  35. }
  36. else
  37. {
  38. LOGN( eDbgLevelError,
  39. "[APIHook_RtlFreeHeap] Invalid Pointer 0x%x for Heap 0x%x.",
  40. BaseAddress, HeapHandle);
  41. }
  42. return bRet;
  43. }
  44. /*++
  45. Verify that the pointer being freed belongs to the heap.
  46. --*/
  47. LPVOID
  48. APIHOOK(RtlReAllocateHeap)(
  49. HANDLE hHeap,
  50. DWORD dwFlags,
  51. LPVOID lpMem,
  52. DWORD dwBytes
  53. )
  54. {
  55. LPVOID pRet = NULL;
  56. if (HeapValidate(hHeap, 0, lpMem))
  57. {
  58. pRet = ORIGINAL_API(RtlReAllocateHeap)(hHeap, dwFlags, lpMem, dwBytes);
  59. }
  60. else
  61. {
  62. LOGN( eDbgLevelError,
  63. "[APIHook_RtlReAllocateHeap] Invalid Pointer 0x%x for Heap 0x%x.",
  64. lpMem, hHeap);
  65. }
  66. return pRet;
  67. }
  68. /*++
  69. Verify that the pointer being sized belongs to the heap
  70. --*/
  71. DWORD
  72. APIHOOK(RtlSizeHeap)(
  73. HANDLE hHeap,
  74. DWORD dwFlags,
  75. LPCVOID lpMem
  76. )
  77. {
  78. DWORD dwRet = (DWORD)-1;
  79. if (HeapValidate(hHeap, 0, lpMem))
  80. {
  81. dwRet = ORIGINAL_API(RtlSizeHeap)(hHeap, dwFlags, lpMem);
  82. }
  83. else
  84. {
  85. LOGN( eDbgLevelError,
  86. "[APIHook_RtlSizeHeap] Invalid Pointer 0x%x for Heap 0x%x.",
  87. lpMem, hHeap);
  88. }
  89. return dwRet;
  90. }
  91. /*++
  92. Register hooked functions
  93. --*/
  94. HOOK_BEGIN
  95. APIHOOK_ENTRY(NTDLL.DLL, RtlFreeHeap)
  96. APIHOOK_ENTRY(NTDLL.DLL, RtlReAllocateHeap)
  97. APIHOOK_ENTRY(NTDLL.DLL, RtlSizeHeap)
  98. HOOK_END
  99. IMPLEMENT_SHIM_END