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.

564 lines
14 KiB

  1. /*++
  2. Copyright (c) 1997-2001 Microsoft Corporation
  3. Module Name:
  4. winsafer.h
  5. Abstract:
  6. This file implements the publicly exported functions, data types,
  7. data structures, and definitions usable by programs that directly
  8. interact with the Windows SAFER APIs.
  9. --*/
  10. #ifndef _WINSAFER_H
  11. #define _WINSAFER_H
  12. #include <guiddef.h>
  13. #include <wincrypt.h>
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. //
  18. // Opaque datatype for representing handles to Safer objects.
  19. //
  20. DECLARE_HANDLE(SAFER_LEVEL_HANDLE);
  21. //
  22. // Constants to represent scope with SaferCreateLevel and others.
  23. //
  24. #define SAFER_SCOPEID_MACHINE 1
  25. #define SAFER_SCOPEID_USER 2
  26. //
  27. // Pre-defined levels that can be used with SaferCreateLevel
  28. //
  29. #define SAFER_LEVELID_FULLYTRUSTED 0x40000
  30. #define SAFER_LEVELID_NORMALUSER 0x20000
  31. #define SAFER_LEVELID_CONSTRAINED 0x10000
  32. #define SAFER_LEVELID_UNTRUSTED 0x01000
  33. #define SAFER_LEVELID_DISALLOWED 0x00000
  34. //
  35. // Flags to use when creating/opening a Level with SaferCreateLevel
  36. //
  37. #define SAFER_LEVEL_OPEN 1
  38. //
  39. // Maximum string size.
  40. //
  41. #define SAFER_MAX_FRIENDLYNAME_SIZE 256
  42. #define SAFER_MAX_DESCRIPTION_SIZE 256
  43. #define SAFER_MAX_HASH_SIZE 64
  44. //
  45. // Flags to use with SaferComputeTokenFromLevel.
  46. //
  47. #define SAFER_TOKEN_NULL_IF_EQUAL 0x00000001
  48. #define SAFER_TOKEN_COMPARE_ONLY 0x00000002
  49. #define SAFER_TOKEN_MAKE_INERT 0x00000004
  50. #define SAFER_TOKEN_WANT_FLAGS 0x00000008
  51. //
  52. // Flags for specifying what criteria within SAFER_CODE_PROPERTIES to evaluate
  53. // when finding code identity with SaferIdentifyLevel.
  54. //
  55. #define SAFER_CRITERIA_IMAGEPATH 0x00001
  56. #define SAFER_CRITERIA_IMAGEHASH 0x00004
  57. #define SAFER_CRITERIA_AUTHENTICODE 0x00008
  58. #define SAFER_CRITERIA_URLZONE 0x00010
  59. #define SAFER_CRITERIA_IMAGEPATH_NT 0x01000
  60. //
  61. // Code image information structure passed to SaferIdentifyLevel.
  62. //
  63. #include <pshpack8.h>
  64. typedef struct _SAFER_CODE_PROPERTIES
  65. {
  66. //
  67. // Must be initialized to be the size of this structure,
  68. // for the purposes of future/backwards compatibility.
  69. //
  70. DWORD cbSize;
  71. //
  72. // Must be initialized to the types of criteria that should
  73. // be considered when evaluating this structure. This can be
  74. // a combination of the SAFER_CRITERIA_xxxx flags. If not enough
  75. // of the structure elements needed to evaluate the criteria
  76. // types indicated were supplied, then some of those criteria
  77. // flags may be silently ignored. Specifying 0 for this value
  78. // will cause the entire structure's contents to be ignored.
  79. //
  80. DWORD dwCheckFlags;
  81. //
  82. // Optionally specifies the fully-qualified path and filename
  83. // to be used for discrimination checks based on the path.
  84. // The ImagePath will additionally be used to open and read the
  85. // file to identify any other discrimination criteria that was
  86. // unsupplied in this structure.
  87. //
  88. LPCWSTR ImagePath;
  89. //
  90. // Optionally specifies a file handle that has been opened to
  91. // code image with at least GENERIC_READ access. The handle will
  92. // be used instead of explicitly opening the file again to compute
  93. // other discrimination criteria that was unsupplied in this structure.
  94. //
  95. HANDLE hImageFileHandle;
  96. //
  97. // Optionally specifies the pre-determined Internet Explorer
  98. // security zone. These enums are defined within urlmon.h
  99. // For example: URLZONE_LOCAL_MACHINE, URLZONE_INTRANET,
  100. // URLZONE_TRUSTED, URLZONE_INTERNET, or URLZONE_UNTRUSTED.
  101. //
  102. DWORD UrlZoneId;
  103. //
  104. // Optionally specifies the pre-computed hash of the image.
  105. // The supplied hash is interpreted as being valid if ImageSize
  106. // is non-zero and dwImageHashSize is non-zero and HashAlgorithm
  107. // represents a valid hashing algorithm from wincrypt.h
  108. //
  109. // If the supplied hash fails to meet the conditions above, then
  110. // the hash will be automatically computed against:
  111. // 1) by using ImageSize and pByteBlock if both are non-zero.
  112. // 2) by using hImageFileHandle if it is non-null.
  113. // 3) by attempting to open ImagePath if it is non-null.
  114. //
  115. BYTE ImageHash[SAFER_MAX_HASH_SIZE];
  116. DWORD dwImageHashSize;
  117. LARGE_INTEGER ImageSize;
  118. ALG_ID HashAlgorithm;
  119. //
  120. // Optionally specifies a memory block of memory representing
  121. // the image for which the trust is being requested for. When
  122. // this member is specified, ImageSize must also be supplied.
  123. //
  124. LPBYTE pByteBlock;
  125. //
  126. // Optionally gives the arguments used for Authenticode signer
  127. // certificate verification. These arguments are supplied to the
  128. // WinVerifyTrust() API and control the user-interface prompting
  129. // to accept untrusted certificates.
  130. //
  131. HWND hWndParent;
  132. DWORD dwWVTUIChoice;
  133. } SAFER_CODE_PROPERTIES, *PSAFER_CODE_PROPERTIES;
  134. #include <poppack.h>
  135. //
  136. // Masks for the per-identity WinSafer flags
  137. //
  138. #define SAFER_POLICY_JOBID_MASK 0xFF000000
  139. #define SAFER_POLICY_JOBID_CONSTRAINED 0x04000000
  140. #define SAFER_POLICY_JOBID_UNTRUSTED 0x03000000
  141. #define SAFER_POLICY_ONLY_EXES 0x00010000
  142. #define SAFER_POLICY_SANDBOX_INERT 0x00020000
  143. #define SAFER_POLICY_UIFLAGS_MASK 0x000000FF
  144. #define SAFER_POLICY_UIFLAGS_INFORMATION_PROMPT 0x00000001
  145. #define SAFER_POLICY_UIFLAGS_OPTION_PROMPT 0x00000002
  146. //
  147. // Information classes on the overall policy that can be queried
  148. // with SaferSet/GetPolicyInformation and set at different
  149. // policy scopes based on access of the caller.
  150. //
  151. typedef enum _SAFER_POLICY_INFO_CLASS
  152. {
  153. //
  154. // Accesses the list of all Levels defined in a policy.
  155. // The corresponding data element is a buffer that is filled
  156. // with multiple DWORDs, each representing the LevelIds that
  157. // are defined within this scope.
  158. //
  159. SaferPolicyLevelList = 1,
  160. //
  161. // for transparent enforcement of policy in the execution
  162. // framework -- will be used by native code execution but can
  163. // be used by any policy enforcement environment.
  164. // Corresponding data element is a DWORD holding a Boolean value.
  165. //
  166. SaferPolicyEnableTransparentEnforcement,
  167. //
  168. // Returns the name of the Level that has been designed
  169. // as the default level within the specified scope.
  170. // The corresponding data element is a single DWORD buffer
  171. // representing the LevelId of the default Level. If no
  172. // level has been configured to be the default, then the
  173. // GetInfo API will return FALSE and GetLastError will
  174. // return ERROR_NOT_FOUND.
  175. //
  176. SaferPolicyDefaultLevel,
  177. //
  178. // Returns whether Code Identities or Default Level within the
  179. // user scope can be considered during identification.
  180. //
  181. SaferPolicyEvaluateUserScope,
  182. //
  183. // Control Flags for for safer policy scope.
  184. //
  185. SaferPolicyScopeFlags
  186. } SAFER_POLICY_INFO_CLASS;
  187. //
  188. // Enumerations used for retrieving specific information about a
  189. // single authorization Level via SaferGet/SetInformationFromLevel.
  190. //
  191. typedef enum _SAFER_OBJECT_INFO_CLASS
  192. {
  193. SaferObjectLevelId = 1, // get: DWORD
  194. SaferObjectScopeId, // get: DWORD
  195. SaferObjectFriendlyName, // get/set: LPCWSTR
  196. SaferObjectDescription, // get/set: LPCWSTR
  197. SaferObjectBuiltin, // get: DWORD boolean
  198. SaferObjectDisallowed, // get: DWORD boolean
  199. SaferObjectDisableMaxPrivilege, // get: DWORD boolean
  200. SaferObjectInvertDeletedPrivileges, // get: DWORD boolean
  201. SaferObjectDeletedPrivileges, // get: TOKEN_PRIVILEGES
  202. SaferObjectDefaultOwner, // get: TOKEN_OWNER
  203. SaferObjectSidsToDisable, // get: TOKEN_GROUPS
  204. SaferObjectRestrictedSidsInverted, // get: TOKEN_GROUPS
  205. SaferObjectRestrictedSidsAdded, // get: TOKEN_GROUPS
  206. //
  207. // To enumerate all identities, call GetInfo with
  208. // SaferObjectAllIdentificationGuids.
  209. //
  210. SaferObjectAllIdentificationGuids, // get: SAFER_IDENTIFICATION_GUIDS
  211. //
  212. // To create a new identity, call SetInfo with
  213. // SaferObjectSingleIdentification with a new
  214. // unique GUID that you have generated.
  215. // To get details on a single identity, call GetInfo with
  216. // SaferObjectSingleIdentification with desired GUID.
  217. // To modify details of a single identity, call SetInfo with
  218. // SaferObjectSingleIdentification with desired info and GUID.
  219. // To delete an identity, call SetInfo with
  220. // SaferObjectSingleIdentification with the
  221. // header.dwIdentificationType set to 0.
  222. //
  223. SaferObjectSingleIdentification, // get/set: SAFER_IDENTIFICATION_*
  224. SaferObjectExtendedError // get: DWORD dwError
  225. } SAFER_OBJECT_INFO_CLASS;
  226. //
  227. // Structures and enums used by the SaferGet/SetLevelInformation APIs.
  228. //
  229. #include <pshpack8.h>
  230. typedef enum _SAFER_IDENTIFICATION_TYPES
  231. {
  232. SaferIdentityDefault,
  233. SaferIdentityTypeImageName = 1,
  234. SaferIdentityTypeImageHash,
  235. SaferIdentityTypeUrlZone,
  236. SaferIdentityTypeCertificate
  237. } SAFER_IDENTIFICATION_TYPES;
  238. typedef struct _SAFER_IDENTIFICATION_HEADER
  239. {
  240. //
  241. // indicates the type of the structure, one of SaferIdentityType*
  242. //
  243. SAFER_IDENTIFICATION_TYPES dwIdentificationType;
  244. //
  245. // size of the whole structure, not just the common header.
  246. //
  247. DWORD cbStructSize;
  248. //
  249. // the unique GUID of the Identity in question.
  250. //
  251. GUID IdentificationGuid;
  252. //
  253. // last change of this identification.
  254. //
  255. FILETIME lastModified;
  256. } SAFER_IDENTIFICATION_HEADER, *PSAFER_IDENTIFICATION_HEADER;
  257. typedef struct _SAFER_PATHNAME_IDENTIFICATION
  258. {
  259. //
  260. // header.dwIdentificationType must be SaferIdentityTypeImageName
  261. // header.cbStructSize must be sizeof(SAFER_PATHNAME_IDENTIFICATION)
  262. //
  263. SAFER_IDENTIFICATION_HEADER header;
  264. //
  265. // user-entered description
  266. //
  267. WCHAR Description[SAFER_MAX_DESCRIPTION_SIZE];
  268. //
  269. // filepath or name, possibly with vars
  270. //
  271. PWCHAR ImageName;
  272. //
  273. // any combo of SAFER_POL_SAFERFLAGS_*
  274. //
  275. DWORD dwSaferFlags;
  276. } SAFER_PATHNAME_IDENTIFICATION, *PSAFER_PATHNAME_IDENTIFICATION;
  277. typedef struct _SAFER_HASH_IDENTIFICATION
  278. {
  279. //
  280. // header.dwIdentificationType must be SaferIdentityTypeImageHash
  281. // header.cbStructSize must be sizeof(SAFER_HASH_IDENTIFICATION)
  282. //
  283. SAFER_IDENTIFICATION_HEADER header;
  284. //
  285. // user-entered friendly name, initially from file's resources.
  286. //
  287. WCHAR Description[SAFER_MAX_DESCRIPTION_SIZE];
  288. //
  289. // user-entered description.
  290. //
  291. WCHAR FriendlyName[SAFER_MAX_FRIENDLYNAME_SIZE];
  292. //
  293. // amount of ImageHash actually used, in bytes (MD5 is 16 bytes).
  294. //
  295. DWORD HashSize;
  296. //
  297. // computed hash data itself.
  298. //
  299. BYTE ImageHash[SAFER_MAX_HASH_SIZE];
  300. //
  301. // algorithm in which the hash was computed (CALG_MD5, etc).
  302. //
  303. ALG_ID HashAlgorithm;
  304. //
  305. // size of the original file in bytes.
  306. //
  307. LARGE_INTEGER ImageSize;
  308. //
  309. // any combo of SAFER_POL_SAFERFLAGS_*
  310. //
  311. DWORD dwSaferFlags;
  312. } SAFER_HASH_IDENTIFICATION, *PSAFER_HASH_IDENTIFICATION;
  313. typedef struct _SAFER_URLZONE_IDENTIFICATION
  314. {
  315. //
  316. // header.dwIdentificationType must be SaferIdentityTypeUrlZone
  317. // header.cbStructSize must be sizeof(SAFER_URLZONE_IDENTIFICATION)
  318. //
  319. SAFER_IDENTIFICATION_HEADER header;
  320. //
  321. // any single URLZONE_* from urlmon.h
  322. //
  323. DWORD UrlZoneId;
  324. //
  325. // any combo of SAFER_POLICY_*
  326. //
  327. DWORD dwSaferFlags;
  328. } SAFER_URLZONE_IDENTIFICATION, *PSAFER_URLZONE_IDENTIFICATION;
  329. #include <poppack.h>
  330. //
  331. // Functions related to querying and setting the global policy
  332. // controls to disable transparent enforcement, and perform level
  333. // enumeration operations.
  334. //
  335. WINADVAPI
  336. BOOL WINAPI
  337. SaferGetPolicyInformation(
  338. IN DWORD dwScopeId,
  339. IN SAFER_POLICY_INFO_CLASS SaferPolicyInfoClass,
  340. IN DWORD InfoBufferSize,
  341. IN OUT PVOID InfoBuffer,
  342. IN OUT PDWORD InfoBufferRetSize,
  343. IN LPVOID lpReserved
  344. );
  345. WINADVAPI
  346. BOOL WINAPI
  347. SaferSetPolicyInformation(
  348. IN DWORD dwScopeId,
  349. IN SAFER_POLICY_INFO_CLASS SaferPolicyInfoClass,
  350. IN DWORD InfoBufferSize,
  351. IN PVOID InfoBuffer,
  352. IN LPVOID lpReserved
  353. );
  354. //
  355. // Functions to open or close a handle to a Safer Level.
  356. //
  357. WINADVAPI
  358. BOOL WINAPI
  359. SaferCreateLevel(
  360. IN DWORD dwScopeId,
  361. IN DWORD dwLevelId,
  362. IN DWORD OpenFlags,
  363. OUT SAFER_LEVEL_HANDLE * pLevelHandle,
  364. IN LPVOID lpReserved
  365. );
  366. WINADVAPI
  367. BOOL WINAPI
  368. SaferCloseLevel(
  369. IN SAFER_LEVEL_HANDLE hLevelHandle
  370. );
  371. WINADVAPI
  372. BOOL WINAPI
  373. SaferIdentifyLevel(
  374. IN DWORD dwNumProperties,
  375. IN PSAFER_CODE_PROPERTIES pCodeProperties,
  376. OUT SAFER_LEVEL_HANDLE * pLevelHandle,
  377. IN LPVOID lpReserved
  378. );
  379. WINADVAPI
  380. BOOL WINAPI
  381. SaferComputeTokenFromLevel(
  382. IN SAFER_LEVEL_HANDLE LevelHandle,
  383. IN HANDLE InAccessToken OPTIONAL,
  384. OUT PHANDLE OutAccessToken,
  385. IN DWORD dwFlags,
  386. IN LPVOID lpReserved
  387. );
  388. WINADVAPI
  389. BOOL WINAPI
  390. SaferGetLevelInformation(
  391. IN SAFER_LEVEL_HANDLE LevelHandle,
  392. IN SAFER_OBJECT_INFO_CLASS dwInfoType,
  393. OUT LPVOID lpQueryBuffer OPTIONAL,
  394. IN DWORD dwInBufferSize,
  395. OUT LPDWORD lpdwOutBufferSize
  396. );
  397. WINADVAPI
  398. BOOL WINAPI
  399. SaferSetLevelInformation(
  400. IN SAFER_LEVEL_HANDLE LevelHandle,
  401. IN SAFER_OBJECT_INFO_CLASS dwInfoType,
  402. IN LPVOID lpQueryBuffer,
  403. IN DWORD dwInBufferSize
  404. );
  405. //
  406. // This function performs logging of messages to the Application
  407. // event log. This is called by the hooks within CreateProcess,
  408. // ShellExecute and cmd when a lower trust evaluation result occurs.
  409. //
  410. WINADVAPI
  411. BOOL WINAPI
  412. SaferRecordEventLogEntry(
  413. IN SAFER_LEVEL_HANDLE hLevel,
  414. IN LPCWSTR szTargetPath,
  415. IN LPVOID lpReserved
  416. );
  417. #ifdef __cplusplus
  418. }
  419. #endif
  420. #endif