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.

297 lines
9.5 KiB

  1. /*
  2. Copyright (c) 2001 Microsoft Corporation
  3. File name:
  4. hotpatch.h
  5. Author:
  6. Adrian Marinescu (adrmarin) Nov 15 2001
  7. Tom McGuire (tommcg)
  8. */
  9. #ifndef _HOTPATCH_H
  10. #define _HOTPATCH_H
  11. //
  12. // Patch file format structures
  13. //
  14. //
  15. // The HOTPATCH_HEADER can be found in the hotpatch binary by searching
  16. // the section headers for a section named ".hotp1 " and verifying that
  17. // the first four bytes in that section match the "HOT1" ('1TOH') header
  18. // signature. The .hotp1 section will be marked readonly and discardable.
  19. //
  20. //
  21. // The HOTPATCH_HEADER can be found in the hotpatch binary by searching
  22. // the section headers for a section named ".hotp1 " and verifying that
  23. // the first four bytes in that section match the "HOT1" ('1TOH') header
  24. // signature. The .hotp1 section will be marked readonly and discardable.
  25. //
  26. #define HOTP_SIGNATURE_DWORD ((ULONG) '1TOH' ) // "HOT1"
  27. #define HOTP_HEADER_VERSION_1_0 0x00010000 // 1.0
  28. #define HOTP_SECTION_NAME ".hotp1 " //
  29. #define HOTP_SECTION_NAME_QWORD 0x20203170746F682E // ".hotp1 "
  30. typedef struct _HOTPATCH_HEADER
  31. {
  32. ULONG Signature; // "HOT1" '1TOH'
  33. ULONG Version; // 0x00010000 (1.0)
  34. ULONG FixupRgnCount; // count of HOTPATCH_FIXUP_REGION entries at FixupArrayRva
  35. ULONG FixupRgnRva; // RVA in this image of HOTPATCH_FIXUP_REGION entries
  36. // (if FixupCount zero, FixupListRva also zero)
  37. ULONG ValidationCount; // count of ValidationArray entries
  38. ULONG ValidationArrayRva; // RVA in this image of HOTPATCH_VALIDATION array
  39. // (validation bytes valid after fixups applied)
  40. ULONG HookCount; // count of HOTPATCH_HOOK entries at HookArrayRva
  41. ULONG HookArrayRva; // RVA in this image of HOTPATCH_HOOK entries
  42. // (if HookCount zero, HookArrayRva also zero)
  43. ULONGLONG OrigHotpBaseAddress; // If hotpatch loaded at this address, and
  44. ULONGLONG OrigTargetBaseAddress; // if target is loaded at this address, then
  45. // fixups in hotpatch are not necessary
  46. ULONG TargetNameRva; // RVA of target module name "kernel32.dll"
  47. ULONG ModuleIdMethod; // one of HOTPATCH_MODULE_ID_METHOD
  48. union // content depends on HOTPATCH_MODULE_ID_METHOD
  49. {
  50. ULONGLONG Quad;
  51. GUID Guid;
  52. struct
  53. {
  54. GUID Guid;
  55. ULONG Age;
  56. }
  57. PdbSig;
  58. UCHAR Hash128[ 16 ]; // For MD5, etc.
  59. UCHAR Hash160[ 20 ]; // For SHA, etc.
  60. }
  61. TargetModuleIdValue; // unique ID of target module
  62. }
  63. HOTPATCH_HEADER, *PHOTPATCH_HEADER;
  64. typedef enum _HOTPATCH_MODULE_ID_METHOD
  65. {
  66. HOTP_ID_None = 0x00000000, // No ID verification of target
  67. HOTP_ID_PeHeaderHash1 = 0x00000001,
  68. //
  69. // MD5 hash of "normalized" IMAGE_NT_HEADERS32/64, ignoring certain
  70. // fields that change due to resource localization, rebase, bind,
  71. // sign, winalign, etc.
  72. //
  73. // FileHeader.TimeDateStamp
  74. // OptionalHeader.CheckSum
  75. // OptionalHeader.ImageBase
  76. // OptionalHeader.FileAlignment
  77. // OptionalHeader.SizeOfCode
  78. // OptionalHeader.SizeOfInitializedData
  79. // OptionalHeader.SizeOfUninitializedData
  80. // OptionalHeader.SizeOfImage
  81. // OptionalHeader.SizeOfHeaders
  82. // OptionalHeader.DataDirectory[ IMAGE_DIRECTORY_ENTRY_RESOURCE ]
  83. // OptionalHeader.DataDirectory[ IMAGE_DIRECTORY_ENTRY_SECURITY ]
  84. // OptionalHeader.DataDirectory[ IMAGE_DIRECTORY_ENTRY_BASERELOC ]
  85. // OptionalHeader.DataDirectory[ IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT ]
  86. //
  87. HOTP_ID_PeHeaderHash2 = 0x00000002, // 64-bit hash of normalized PE header
  88. HOTP_ID_PeChecksum = 0x00000003, // 32-bit checksum in the PE Optional header
  89. HOTP_ID_PeDebugSignature = 0x00000010, // pdb signature (GUID,Age)
  90. }
  91. HOTPATCH_MODULE_ID_METHOD;
  92. typedef struct _HOTPATCH_FIXUP_REGION
  93. {
  94. ULONG RvaHi:20; // Hi 20 bits of RVA where fixups to be applied
  95. ULONG Count:12; // Number of fixup entries for this region
  96. USHORT Fixup[2]; // Variable length HOTPATCH_FIXUP_ENTRY array
  97. }
  98. HOTPATCH_FIXUP_REGION, *PHOTPATCH_FIXUP_REGION;
  99. typedef struct _HOTPATCH_FIXUP_ENTRY
  100. {
  101. USHORT RvaOffset:12; // Lo 12 bits of RVA where fixup to be applied
  102. USHORT FixupType:4; // Type of fixup to perform at this location
  103. }
  104. HOTPATCH_FIXUP_ENTRY, *PHOTPATCH_FIXUP_ENTRY;
  105. typedef enum _HOTPATCH_FIXUP_TYPE
  106. {
  107. HOTP_Fixup_None = 0x0, // No fixup, ignore this entry (alignment, etc)
  108. HOTP_Fixup_VA32 = 0x1, // 32-bit address in target image
  109. HOTP_Fixup_PC32 = 0x2, // 32-bit x86 pc-rel address to target image
  110. HOTP_Fixup_VA64 = 0x3, // 64-bit address in target image
  111. }
  112. HOTPATCH_FIXUP_TYPE;
  113. typedef struct _HOTPATCH_VALIDATION
  114. {
  115. ULONG SourceRva; // RVA within patch image of validation raw bytes
  116. ULONG TargetRva; // RVA within target image to validate against
  117. USHORT ByteCount; // number of bytes to validate at this RVA pair
  118. USHORT OptionFlags; // combination of HOTPATCH_VALIDATION_OPTIONS
  119. }
  120. HOTPATCH_VALIDATION, *PHOTPATCH_VALIDATION;
  121. typedef enum _HOTPATCH_VALIDATION_OPTIONS
  122. {
  123. HOTP_Valid_Hook_Target = 0x0001, // specific to a HOTPATCH_HOOK entry
  124. }
  125. HOTPATCH_VALIDATION_OPTIONS;
  126. typedef struct _HOTPATCH_HOOK
  127. {
  128. USHORT HookType; // one of HOTPATCH_HOOK_TYPE
  129. USHORT HookOptions; // options specific to HookType
  130. ULONG HookRva; // RVA in target image -- where to insert hook
  131. ULONG HotpRva; // RVA in hotpatch image for redirected target of hook
  132. ULONG ValidationRva; // Optional RVA in hotpatch image of HOTPATCH_VALIDATION
  133. } // specific to this hook location in target image.
  134. HOTPATCH_HOOK, *PHOTPATCH_HOOK;
  135. typedef enum _HOTPATCH_HOOK_TYPE
  136. {
  137. HOTP_Hook_None = 0x0000, // No hook, ignore this entry (continuation values)
  138. HOTP_Hook_VA32 = 0x0001, // 32-bit absolute address of hook target (little endian)
  139. HOTP_Hook_X86_JMP = 0x0002, // x86 E9 jmp with 32-bit pc-relative to hook target
  140. // HookOptions low 4 bits contains original instruction length
  141. // so implementation can pad E9 hook instruction with CC bytes
  142. HOTP_Hook_PCREL32 = 0x0003, // 32-bit x86 pcrelative address of hook target, replacing
  143. // the last four bytes of a call or conditional branch.
  144. // HookOptions low 4 bits contains original instruction length
  145. // so can determine where instruction begins.
  146. HOTP_Hook_X86_JMP2B = 0x0004, // x86 EB jmp with 8-bit displacement to an X86_JMP hook
  147. // HookOptions low 4 bits contains original instruction length
  148. // HotpRva contains the 8-bit displacement
  149. HOTP_Hook_VA64 = 0x0010, // 64-bit absolute address of hook target (little endian)
  150. HOTP_Hook_IA64_BRL = 0x0011, // IA64 brl with 64-bit target address
  151. }
  152. HOTPATCH_HOOK_TYPE;
  153. //
  154. // Information existent in debug directory
  155. //
  156. typedef struct _HOTPATCH_DEBUG_SIGNATURE {
  157. USHORT HotpatchVersion;
  158. USHORT Signature;
  159. } HOTPATCH_DEBUG_SIGNATURE, *PHOTPATCH_DEBUG_SIGNATURE;
  160. typedef struct _HOTPATCH_DEBUG_DATA {
  161. ULONGLONG PEHashData;
  162. ULONGLONG ChecksumData;
  163. } HOTPATCH_DEBUG_DATA, *PHOTPATCH_DEBUG_DATA;
  164. //
  165. // RTL internal patch definitions
  166. //
  167. #ifdef NTOS_KERNEL_RUNTIME
  168. #define PATCH_LDR_DATA_TABLE_ENTRY KLDR_DATA_TABLE_ENTRY
  169. #define PPATCH_LDR_DATA_TABLE_ENTRY PKLDR_DATA_TABLE_ENTRY
  170. #else // ! NTOS_KERNEL_RUNTIME
  171. #define PATCH_LDR_DATA_TABLE_ENTRY LDR_DATA_TABLE_ENTRY
  172. #define PPATCH_LDR_DATA_TABLE_ENTRY PLDR_DATA_TABLE_ENTRY
  173. #endif // NTOS_KERNEL_RUNTIME
  174. typedef struct _RTL_PATCH_HEADER {
  175. LIST_ENTRY PatchList;
  176. PVOID PatchImageBase;
  177. struct _RTL_PATCH_HEADER * NextPatch;
  178. ULONG PatchFlags;
  179. LONG PatchRefCount;
  180. PHOTPATCH_HEADER HotpatchHeader;
  181. UNICODE_STRING TargetDllName;
  182. PVOID TargetDllBase;
  183. PPATCH_LDR_DATA_TABLE_ENTRY TargetLdrDataTableEntry;
  184. PPATCH_LDR_DATA_TABLE_ENTRY PatchLdrDataTableEntry;
  185. PSYSTEM_HOTPATCH_CODE_INFORMATION CodeInfo;
  186. } RTL_PATCH_HEADER, *PRTL_PATCH_HEADER;
  187. NTSTATUS
  188. RtlCreateHotPatch (
  189. OUT PRTL_PATCH_HEADER * RtlPatchData,
  190. IN PHOTPATCH_HEADER Patch,
  191. IN PPATCH_LDR_DATA_TABLE_ENTRY PatchLdrEntry,
  192. IN ULONG PatchFlags
  193. );
  194. NTSTATUS
  195. RtlInitializeHotPatch (
  196. IN PRTL_PATCH_HEADER RtlPatchData,
  197. IN ULONG_PTR PatchOffsetCorrection
  198. );
  199. NTSTATUS
  200. RtlReadHookInformation(
  201. IN PRTL_PATCH_HEADER RtlPatchData
  202. );
  203. VOID
  204. RtlFreeHotPatchData(
  205. IN PRTL_PATCH_HEADER RtlPatchData
  206. );
  207. PHOTPATCH_HEADER
  208. RtlGetHotpatchHeader(
  209. PVOID ImageBase
  210. );
  211. PRTL_PATCH_HEADER
  212. RtlFindRtlPatchHeader(
  213. IN PLIST_ENTRY PatchList,
  214. IN PPATCH_LDR_DATA_TABLE_ENTRY PatchLdrEntry
  215. );
  216. BOOLEAN
  217. RtlpIsSameImage (
  218. IN PRTL_PATCH_HEADER RtlPatchData,
  219. IN PPATCH_LDR_DATA_TABLE_ENTRY LdrDataEntry
  220. );
  221. #endif // _HOTPATCH_H