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.

255 lines
7.6 KiB

  1. /*++
  2. Copyright (c) 1997-2000 Microsoft Corporation
  3. Module Name:
  4. safelog.c (SAFER Event Logging)
  5. Abstract:
  6. This module implements the internal WinSAFER APIs to write eventlog
  7. messages. All of our message strings are defined in ntstatus.mc
  8. and are physically located within ntdll.dll file.
  9. Currently we are just reusing the previously existing event source
  10. called "Application Popup", which already happens to use ntdll.dll
  11. as its message resource library. Events of this source always go
  12. into the "System Log".
  13. Author:
  14. Jeffrey Lawson (JLawson) - Apr 1999
  15. Environment:
  16. User mode only.
  17. Exported Functions:
  18. SaferRecordEventLogEntry
  19. Revision History:
  20. Created - Nov 2000
  21. --*/
  22. #include "pch.h"
  23. #pragma hdrstop
  24. #include <winsafer.h>
  25. #include <winsaferp.h>
  26. #include "saferp.h"
  27. const static GUID guidTrustedCert = SAFER_GUID_RESULT_TRUSTED_CERT;
  28. const static GUID guidDefaultRule = SAFER_GUID_RESULT_DEFAULT_LEVEL;
  29. BOOL WINAPI
  30. SaferpRecordEventLogEntryHelper(
  31. IN NTSTATUS LogStatusCode,
  32. IN LPCWSTR szTargetPath,
  33. IN REFGUID refRuleGuid,
  34. IN LPCWSTR szRulePath
  35. )
  36. {
  37. NTSTATUS Status = STATUS_UNSUCCESSFUL;
  38. WORD wNumStrings = 0;
  39. LPWSTR lpszStrings[5];
  40. HANDLE hEventSource;
  41. UNICODE_STRING UnicodeGuid;
  42. hEventSource = RegisterEventSourceW(NULL, L"Software Restriction Policy");
  43. if (hEventSource != NULL) {
  44. Status = STATUS_SUCCESS;
  45. RtlInitEmptyUnicodeString(&UnicodeGuid, NULL, 0);
  46. switch (LogStatusCode)
  47. {
  48. case STATUS_ACCESS_DISABLED_BY_POLICY_DEFAULT:
  49. if (!ARGUMENT_PRESENT(szTargetPath)) {
  50. Status = STATUS_INVALID_PARAMETER;
  51. break;
  52. }
  53. lpszStrings[0] = (LPWSTR) szTargetPath;
  54. wNumStrings = 1;
  55. break;
  56. case STATUS_ACCESS_DISABLED_BY_POLICY_OTHER:
  57. if (!ARGUMENT_PRESENT(szTargetPath) ||
  58. !ARGUMENT_PRESENT(refRuleGuid)) {
  59. Status = STATUS_INVALID_PARAMETER;
  60. break;
  61. }
  62. Status = RtlStringFromGUID(refRuleGuid, &UnicodeGuid);
  63. if (NT_SUCCESS(Status)) {
  64. ASSERT(UnicodeGuid.Buffer != NULL);
  65. lpszStrings[0] = (LPWSTR) szTargetPath;
  66. lpszStrings[1] = UnicodeGuid.Buffer;
  67. wNumStrings = 2;
  68. }
  69. break;
  70. case STATUS_ACCESS_DISABLED_BY_POLICY_PUBLISHER:
  71. if (!ARGUMENT_PRESENT(szTargetPath)) {
  72. Status = STATUS_INVALID_PARAMETER;
  73. break;
  74. }
  75. lpszStrings[0] = (LPWSTR) szTargetPath;
  76. wNumStrings = 1;
  77. break;
  78. case STATUS_ACCESS_DISABLED_BY_POLICY_PATH:
  79. if (!ARGUMENT_PRESENT(szTargetPath) ||
  80. !ARGUMENT_PRESENT(refRuleGuid) ||
  81. !ARGUMENT_PRESENT(szRulePath)) {
  82. Status = STATUS_INVALID_PARAMETER;
  83. break;
  84. }
  85. Status = RtlStringFromGUID(refRuleGuid, &UnicodeGuid);
  86. if (NT_SUCCESS(Status)) {
  87. ASSERT(UnicodeGuid.Buffer != NULL);
  88. lpszStrings[0] = (LPWSTR) szTargetPath;
  89. lpszStrings[1] = UnicodeGuid.Buffer;
  90. lpszStrings[2] = (LPWSTR) szRulePath;
  91. wNumStrings = 3;
  92. }
  93. break;
  94. default:
  95. Status = STATUS_INVALID_PARAMETER;
  96. }
  97. if (NT_SUCCESS(Status)) {
  98. ReportEventW(
  99. hEventSource, // handle to event log
  100. EVENTLOG_WARNING_TYPE, // event type
  101. 0, // event category
  102. LogStatusCode, // event ID
  103. NULL, // current user's SID
  104. wNumStrings, // strings in lpszStrings
  105. 0, // no bytes of raw data
  106. lpszStrings, // array of error strings
  107. NULL); // no raw data
  108. }
  109. DeregisterEventSource(hEventSource);
  110. if (UnicodeGuid.Buffer != NULL) {
  111. RtlFreeUnicodeString(&UnicodeGuid);
  112. }
  113. }
  114. if (NT_SUCCESS(Status)) {
  115. return TRUE;
  116. } else {
  117. return FALSE;
  118. }
  119. }
  120. BOOL WINAPI
  121. SaferRecordEventLogEntry(
  122. IN SAFER_LEVEL_HANDLE hAuthzLevel,
  123. IN LPCWSTR szTargetPath,
  124. IN LPVOID lpReserved
  125. )
  126. {
  127. PSAFER_IDENTIFICATION_HEADER pIdentCommon;
  128. DWORD dwIdentBufferSize;
  129. BOOL bResult;
  130. //
  131. // Allocate enough memory for the largest structure we can expect
  132. // and then query the information about the identifier that matched.
  133. //
  134. dwIdentBufferSize = max(sizeof(SAFER_HASH_IDENTIFICATION),
  135. sizeof(SAFER_PATHNAME_IDENTIFICATION));
  136. pIdentCommon = (PSAFER_IDENTIFICATION_HEADER)
  137. HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwIdentBufferSize);
  138. if (!pIdentCommon) {
  139. return FALSE;
  140. }
  141. pIdentCommon->cbStructSize = sizeof(SAFER_IDENTIFICATION_HEADER);
  142. if (!SaferGetLevelInformation(
  143. hAuthzLevel,
  144. SaferObjectSingleIdentification,
  145. pIdentCommon,
  146. dwIdentBufferSize,
  147. &dwIdentBufferSize)) {
  148. if (GetLastError() == ERROR_NOT_ENOUGH_MEMORY) {
  149. HeapFree(GetProcessHeap(), 0, pIdentCommon);
  150. pIdentCommon = (PSAFER_IDENTIFICATION_HEADER)
  151. HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwIdentBufferSize);
  152. if (!pIdentCommon) {
  153. return FALSE;
  154. }
  155. pIdentCommon->cbStructSize = sizeof(SAFER_IDENTIFICATION_HEADER);
  156. if (!SaferGetLevelInformation(
  157. hAuthzLevel,
  158. SaferObjectSingleIdentification,
  159. pIdentCommon,
  160. dwIdentBufferSize,
  161. &dwIdentBufferSize)) {
  162. bResult = FALSE;
  163. goto Cleanup;
  164. }
  165. }
  166. else
  167. {
  168. bResult = FALSE;
  169. goto Cleanup;
  170. }
  171. }
  172. //
  173. // Look at the resulting information about the identifier.
  174. //
  175. if (IsEqualGUID(&pIdentCommon->IdentificationGuid, &guidTrustedCert))
  176. {
  177. bResult = SaferpRecordEventLogEntryHelper(
  178. STATUS_ACCESS_DISABLED_BY_POLICY_PUBLISHER,
  179. szTargetPath, NULL, NULL);
  180. }
  181. else if (IsEqualGUID(&pIdentCommon->IdentificationGuid, &guidDefaultRule))
  182. {
  183. bResult = SaferpRecordEventLogEntryHelper(
  184. STATUS_ACCESS_DISABLED_BY_POLICY_DEFAULT,
  185. szTargetPath, NULL, NULL);
  186. }
  187. else if (pIdentCommon->dwIdentificationType == SaferIdentityTypeImageName)
  188. {
  189. PSAFER_PATHNAME_IDENTIFICATION pIdentPath =
  190. (PSAFER_PATHNAME_IDENTIFICATION) pIdentCommon;
  191. bResult = SaferpRecordEventLogEntryHelper(
  192. STATUS_ACCESS_DISABLED_BY_POLICY_PATH,
  193. szTargetPath, &pIdentCommon->IdentificationGuid,
  194. pIdentPath->ImageName);
  195. }
  196. else
  197. {
  198. bResult = SaferpRecordEventLogEntryHelper(
  199. STATUS_ACCESS_DISABLED_BY_POLICY_OTHER,
  200. szTargetPath, &pIdentCommon->IdentificationGuid,
  201. NULL);
  202. }
  203. Cleanup:
  204. HeapFree(GetProcessHeap(), 0, pIdentCommon);
  205. return bResult;
  206. }