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.

179 lines
3.9 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. LogFileChanges.h
  5. Abstract:
  6. This AppVerifier shim hooks all the native file I/O APIs
  7. that change the state of the system and logs their
  8. associated data to a text file.
  9. Notes:
  10. This is a general purpose shim.
  11. History:
  12. 08/17/2001 rparsons Created
  13. --*/
  14. #ifndef __APPVERIFIER_LOGFILECHANGES_H_
  15. #define __APPVERIFIER_LOGFILECHANGES_H_
  16. #include "precomp.h"
  17. //
  18. // Length (in characters) of the largest element.
  19. //
  20. #define MAX_ELEMENT_SIZE 1024 * 10
  21. //
  22. // Length (in characters) of the longest operation type.
  23. //
  24. #define MAX_OPERATION_LENGTH 32
  25. //
  26. // Flags that indicate what state the file is in.
  27. //
  28. #define LFC_EXISTING 0x00000001
  29. #define LFC_DELETED 0x00000002
  30. #define LFC_MODIFIED 0x00000004
  31. #define LFC_UNAPPRVFW 0x00000008
  32. //
  33. // Maximum number of handles we can track for a single file.
  34. //
  35. #define MAX_NUM_HANDLES 64
  36. //
  37. // We maintain a doubly linked list of file handles so we know what file is being modified
  38. // during a file operation.
  39. //
  40. typedef struct _LOG_HANDLE {
  41. LIST_ENTRY Entry;
  42. HANDLE hFile[MAX_NUM_HANDLES]; // array of file handles
  43. DWORD dwFlags; // flags that relate to the state of the file
  44. LPWSTR pwszFilePath; // full path to the file
  45. UINT cHandles; // number of handles open for this file
  46. } LOG_HANDLE, *PLOG_HANDLE;
  47. //
  48. // Flags that define different settings in effect.
  49. //
  50. #define LFC_OPTION_ATTRIBUTES 0x00000001
  51. #define LFC_OPTION_UFW_WINDOWS 0x00000002
  52. #define LFC_OPTION_UFW_PROGFILES 0x00000004
  53. //
  54. // Enumeration for different operations.
  55. //
  56. typedef enum {
  57. eCreatedFile = 0,
  58. eOpenedFile,
  59. eDeletedFile,
  60. eModifiedFile,
  61. eRenamedFile
  62. } OperationType;
  63. #ifdef ARRAYSIZE
  64. #undef ARRAYSIZE
  65. #endif
  66. #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
  67. //
  68. // Macros for memory allocation/deallocation.
  69. //
  70. #define MemAlloc(s) RtlAllocateHeap(RtlProcessHeap(), HEAP_ZERO_MEMORY, (s))
  71. #define MemFree(b) RtlFreeHeap(RtlProcessHeap(), 0, (b))
  72. //
  73. // On Windows 2000, we need to pre-allocate the event
  74. // in RTL_CRITICAL_SECTION. On XP and above, this is
  75. // a no-op.
  76. //
  77. #define PREALLOCATE_EVENT_MASK 0x80000000
  78. //
  79. // Critical section wrapper class.
  80. //
  81. class CCriticalSection
  82. {
  83. private:
  84. CRITICAL_SECTION m_CritSec;
  85. public:
  86. CCriticalSection()
  87. {
  88. InitializeCriticalSectionAndSpinCount(&m_CritSec,
  89. PREALLOCATE_EVENT_MASK | 4000);
  90. }
  91. ~CCriticalSection()
  92. {
  93. DeleteCriticalSection(&m_CritSec);
  94. }
  95. void Lock()
  96. {
  97. EnterCriticalSection(&m_CritSec);
  98. }
  99. BOOL TryLock()
  100. {
  101. return TryEnterCriticalSection(&m_CritSec);
  102. }
  103. void Unlock()
  104. {
  105. LeaveCriticalSection(&m_CritSec);
  106. }
  107. };
  108. //
  109. // Auto-lock class that uses the CCriticalSection class.
  110. //
  111. class CLock
  112. {
  113. private:
  114. CCriticalSection &m_CriticalSection;
  115. public:
  116. CLock(CCriticalSection &CriticalSection)
  117. : m_CriticalSection(CriticalSection)
  118. {
  119. m_CriticalSection.Lock();
  120. }
  121. ~CLock()
  122. {
  123. m_CriticalSection.Unlock();
  124. }
  125. };
  126. APIHOOK_ENUM_BEGIN
  127. APIHOOK_ENUM_ENTRY(NtDeleteFile)
  128. APIHOOK_ENUM_ENTRY(NtClose)
  129. APIHOOK_ENUM_ENTRY(NtCreateFile)
  130. APIHOOK_ENUM_ENTRY(NtOpenFile)
  131. APIHOOK_ENUM_ENTRY(NtWriteFile)
  132. APIHOOK_ENUM_ENTRY(NtWriteFileGather)
  133. APIHOOK_ENUM_ENTRY(NtSetInformationFile)
  134. //
  135. // Hook these only for Windows 2000 so we know when
  136. // it's safe to call shel32.
  137. //
  138. #ifdef SHIM_WIN2K
  139. APIHOOK_ENUM_ENTRY(GetStartupInfoA)
  140. APIHOOK_ENUM_ENTRY(GetStartupInfoW)
  141. #endif // SHIM_WIN2K
  142. APIHOOK_ENUM_END
  143. #endif // __APPVERIFIER_LOGFILECHANGES_H_