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.

271 lines
6.8 KiB

  1. /*++
  2. Copyright (c) 2000-2002 Microsoft Corporation
  3. Module Name:
  4. EmulateCreateFileMapping.cpp
  5. Abstract:
  6. Win9x defaults to SEC_COMMIT when the SEC_NOCACHE flag is set.
  7. NT fails the call.
  8. File mapping names, can't contain backslashes.
  9. Notes:
  10. This is a general purpose shim.
  11. History:
  12. 02/17/2000 linstev Created
  13. 05/26/2001 robkenny Replace all \ to _ in map names.
  14. 02/28/2002 robkenny Security review, was clobbering Global\ and Local\ namespaces.
  15. --*/
  16. #include "precomp.h"
  17. IMPLEMENT_SHIM_BEGIN(EmulateCreateFileMapping)
  18. #include "ShimHookMacro.h"
  19. APIHOOK_ENUM_BEGIN
  20. APIHOOK_ENUM_ENTRY(CreateFileMappingA)
  21. APIHOOK_ENUM_ENTRY(CreateFileMappingW)
  22. APIHOOK_ENUM_ENTRY(OpenFileMappingA)
  23. APIHOOK_ENUM_ENTRY(OpenFileMappingW)
  24. APIHOOK_ENUM_END
  25. /*++
  26. Correct the flag and name
  27. --*/
  28. HANDLE
  29. APIHOOK(CreateFileMappingW)(
  30. HANDLE hFile,
  31. LPSECURITY_ATTRIBUTES lpAttributes,
  32. DWORD flProtect,
  33. DWORD dwMaximumSizeHigh,
  34. DWORD dwMaximumSizeLow,
  35. LPCWSTR lpName
  36. )
  37. {
  38. HANDLE hRet = ORIGINAL_API(CreateFileMappingW)(hFile, lpAttributes,
  39. flProtect, dwMaximumSizeHigh, dwMaximumSizeLow, lpName);
  40. if (!hRet) {
  41. //
  42. // The call failed, so try correcting the parameters
  43. //
  44. DWORD flNewProtect = flProtect;
  45. if ((flProtect & SEC_NOCACHE) &&
  46. (!((flProtect & SEC_COMMIT) || (flProtect & SEC_RESERVE)))) {
  47. // Add the SEC_COMMIT flag
  48. flNewProtect |= SEC_COMMIT;
  49. }
  50. CSTRING_TRY {
  51. // Replace backslashes
  52. CString csName(lpName);
  53. int nCount = 0;
  54. if (csName.ComparePart(L"Global\\", 0, 7) == 0)
  55. {
  56. // This event exists in the global namespace
  57. csName.Delete(0, 7);
  58. nCount = csName.Replace(L'\\', '_');
  59. csName = L"Global\\" + csName;
  60. }
  61. else if (csName.ComparePart(L"Local\\", 0, 6) == 0)
  62. {
  63. // This event exists in the Local namespace
  64. csName.Delete(0, 6);
  65. nCount = csName.Replace(L'\\', '_');
  66. csName = L"Local\\" + csName;
  67. }
  68. else
  69. {
  70. nCount = csName.Replace(L'\\', '_');
  71. }
  72. LPCWSTR lpCorrectName = csName;
  73. if (nCount || (flProtect != flNewProtect)) {
  74. // Something happened, so log it
  75. if (nCount) {
  76. LOGN(eDbgLevelError,
  77. "[CreateFileMapping] Corrected event name from (%S) to (%S)", lpName, lpCorrectName);
  78. }
  79. if (flProtect != flNewProtect) {
  80. LOGN(eDbgLevelError, "[CreateFileMapping] Adding SEC_COMMIT flag");
  81. }
  82. // Call again with fixed parameters
  83. hRet = ORIGINAL_API(CreateFileMappingW)(hFile, lpAttributes, flNewProtect,
  84. dwMaximumSizeHigh, dwMaximumSizeLow, lpCorrectName);
  85. }
  86. }
  87. CSTRING_CATCH {
  88. // Do nothing
  89. }
  90. }
  91. return hRet;
  92. }
  93. /*++
  94. Pass through to CreateFileMappingW.
  95. --*/
  96. HANDLE
  97. APIHOOK(CreateFileMappingA)(
  98. HANDLE hFile,
  99. LPSECURITY_ATTRIBUTES lpAttributes,
  100. DWORD flProtect,
  101. DWORD dwMaximumSizeHigh,
  102. DWORD dwMaximumSizeLow,
  103. LPCSTR lpName
  104. )
  105. {
  106. HANDLE hRet;
  107. CSTRING_TRY {
  108. // Convert to unicode
  109. CString csName(lpName);
  110. LPCWSTR lpwName = csName;
  111. hRet = APIHOOK(CreateFileMappingW)(hFile, lpAttributes, flProtect,
  112. dwMaximumSizeHigh, dwMaximumSizeLow, lpwName);
  113. }
  114. CSTRING_CATCH {
  115. // Fall back as gracefully as possible
  116. hRet = ORIGINAL_API(CreateFileMappingA)(hFile, lpAttributes, flProtect,
  117. dwMaximumSizeHigh, dwMaximumSizeLow, lpName);
  118. }
  119. return hRet;
  120. }
  121. /*++
  122. Correct the name
  123. --*/
  124. HANDLE
  125. APIHOOK(OpenFileMappingW)(
  126. DWORD dwDesiredAccess,
  127. BOOL bInheritHandle,
  128. LPCWSTR lpName
  129. )
  130. {
  131. HANDLE hRet = ORIGINAL_API(OpenFileMappingW)(dwDesiredAccess, bInheritHandle,
  132. lpName);
  133. if (!hRet) {
  134. //
  135. // The call failed, so try correcting the parameters
  136. //
  137. CSTRING_TRY {
  138. // Replace backslashes
  139. CString csName(lpName);
  140. int nCount = 0;
  141. if (csName.ComparePart(L"Global\\", 0, 7) == 0)
  142. {
  143. // This event exists in the global namespace
  144. csName.Delete(0, 7);
  145. nCount = csName.Replace(L'\\', '_');
  146. csName = L"Global\\" + csName;
  147. }
  148. else if (csName.ComparePart(L"Local\\", 0, 6) == 0)
  149. {
  150. // This event exists in the Local namespace
  151. csName.Delete(0, 6);
  152. nCount = csName.Replace(L'\\', '_');
  153. csName = L"Local\\" + csName;
  154. }
  155. else
  156. {
  157. nCount = csName.Replace(L'\\', '_');
  158. }
  159. LPCWSTR lpCorrectName = csName;
  160. if (nCount) {
  161. // Something happened, so log it
  162. LOGN(eDbgLevelError,
  163. "OpenFileMappingW corrected event name from (%S) to (%S)", lpName, lpCorrectName);
  164. // Call again with fixed parameters
  165. hRet = ORIGINAL_API(OpenFileMappingW)(dwDesiredAccess, bInheritHandle,
  166. lpCorrectName);
  167. }
  168. }
  169. CSTRING_CATCH
  170. {
  171. // Do nothing
  172. }
  173. }
  174. return hRet;
  175. }
  176. /*++
  177. Pass through to OpenFileMappingW.
  178. --*/
  179. HANDLE
  180. APIHOOK(OpenFileMappingA)(
  181. DWORD dwDesiredAccess,
  182. BOOL bInheritHandle,
  183. LPCSTR lpName
  184. )
  185. {
  186. HANDLE hRet;
  187. CSTRING_TRY {
  188. // Convert to unicode
  189. CString csName(lpName);
  190. LPCWSTR lpwName = csName;
  191. hRet = APIHOOK(OpenFileMappingW)(dwDesiredAccess, bInheritHandle,
  192. lpwName);
  193. }
  194. CSTRING_CATCH {
  195. // Fall back as gracefully as possible
  196. hRet = ORIGINAL_API(OpenFileMappingA)(dwDesiredAccess, bInheritHandle,
  197. lpName);
  198. }
  199. return hRet;
  200. }
  201. /*++
  202. Register hooked functions
  203. --*/
  204. HOOK_BEGIN
  205. APIHOOK_ENTRY(KERNEL32.DLL, CreateFileMappingA)
  206. APIHOOK_ENTRY(KERNEL32.DLL, CreateFileMappingW)
  207. APIHOOK_ENTRY(KERNEL32.DLL, OpenFileMappingA)
  208. APIHOOK_ENTRY(KERNEL32.DLL, OpenFileMappingW)
  209. HOOK_END
  210. IMPLEMENT_SHIM_END