winsafer.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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_HASH_DUPLICATE 0x00040000
  144. #define SAFER_POLICY_UIFLAGS_MASK 0x000000FF
  145. #define SAFER_POLICY_UIFLAGS_INFORMATION_PROMPT 0x00000001
  146. #define SAFER_POLICY_UIFLAGS_OPTION_PROMPT 0x00000002
  147. #define SAFER_POLICY_UIFLAGS_HIDDEN 0x00000004
  148. //
  149. // Information classes on the overall policy that can be queried
  150. // with SaferSet/GetPolicyInformation and set at different
  151. // policy scopes based on access of the caller.
  152. //
  153. typedef enum _SAFER_POLICY_INFO_CLASS
  154. {
  155. //
  156. // Accesses the list of all Levels defined in a policy.
  157. // The corresponding data element is a buffer that is filled
  158. // with multiple DWORDs, each representing the LevelIds that
  159. // are defined within this scope.
  160. //
  161. SaferPolicyLevelList = 1,
  162. //
  163. // for transparent enforcement of policy in the execution
  164. // framework -- will be used by native code execution but can
  165. // be used by any policy enforcement environment.
  166. // Corresponding data element is a DWORD holding a Boolean value.
  167. //
  168. SaferPolicyEnableTransparentEnforcement,
  169. //
  170. // Returns the name of the Level that has been designed
  171. // as the default level within the specified scope.
  172. // The corresponding data element is a single DWORD buffer
  173. // representing the LevelId of the default Level. If no
  174. // level has been configured to be the default, then the
  175. // GetInfo API will return FALSE and GetLastError will
  176. // return ERROR_NOT_FOUND.
  177. //
  178. SaferPolicyDefaultLevel,
  179. //
  180. // Returns whether Code Identities or Default Level within the
  181. // user scope can be considered during identification.
  182. //
  183. SaferPolicyEvaluateUserScope,
  184. //
  185. // Control Flags for for safer policy scope.
  186. //
  187. SaferPolicyScopeFlags
  188. } SAFER_POLICY_INFO_CLASS;
  189. //
  190. // Enumerations used for retrieving specific information about a
  191. // single authorization Level via SaferGet/SetInformationFromLevel.
  192. //
  193. typedef enum _SAFER_OBJECT_INFO_CLASS
  194. {
  195. SaferObjectLevelId = 1, // get: DWORD
  196. SaferObjectScopeId, // get: DWORD
  197. SaferObjectFriendlyName, // get/set: LPCWSTR
  198. SaferObjectDescription, // get/set: LPCWSTR
  199. SaferObjectBuiltin, // get: DWORD boolean
  200. SaferObjectDisallowed, // get: DWORD boolean
  201. SaferObjectDisableMaxPrivilege, // get: DWORD boolean
  202. SaferObjectInvertDeletedPrivileges, // get: DWORD boolean
  203. SaferObjectDeletedPrivileges, // get: TOKEN_PRIVILEGES
  204. SaferObjectDefaultOwner, // get: TOKEN_OWNER
  205. SaferObjectSidsToDisable, // get: TOKEN_GROUPS
  206. SaferObjectRestrictedSidsInverted, // get: TOKEN_GROUPS
  207. SaferObjectRestrictedSidsAdded, // get: TOKEN_GROUPS
  208. //
  209. // To enumerate all identities, call GetInfo with
  210. // SaferObjectAllIdentificationGuids.
  211. //
  212. SaferObjectAllIdentificationGuids, // get: SAFER_IDENTIFICATION_GUIDS
  213. //
  214. // To create a new identity, call SetInfo with
  215. // SaferObjectSingleIdentification with a new
  216. // unique GUID that you have generated.
  217. // To get details on a single identity, call GetInfo with
  218. // SaferObjectSingleIdentification with desired GUID.
  219. // To modify details of a single identity, call SetInfo with
  220. // SaferObjectSingleIdentification with desired info and GUID.
  221. // To delete an identity, call SetInfo with
  222. // SaferObjectSingleIdentification with the
  223. // header.dwIdentificationType set to 0.
  224. //
  225. SaferObjectSingleIdentification, // get/set: SAFER_IDENTIFICATION_*
  226. SaferObjectExtendedError // get: DWORD dwError
  227. } SAFER_OBJECT_INFO_CLASS;
  228. //
  229. // Structures and enums used by the SaferGet/SetLevelInformation APIs.
  230. //
  231. #include <pshpack8.h>
  232. typedef enum _SAFER_IDENTIFICATION_TYPES
  233. {
  234. SaferIdentityDefault,
  235. SaferIdentityTypeImageName = 1,
  236. SaferIdentityTypeImageHash,
  237. SaferIdentityTypeUrlZone,
  238. SaferIdentityTypeCertificate
  239. } SAFER_IDENTIFICATION_TYPES;
  240. typedef struct _SAFER_IDENTIFICATION_HEADER
  241. {
  242. //
  243. // indicates the type of the structure, one of SaferIdentityType*
  244. //
  245. SAFER_IDENTIFICATION_TYPES dwIdentificationType;
  246. //
  247. // size of the whole structure, not just the common header.
  248. //
  249. DWORD cbStructSize;
  250. //
  251. // the unique GUID of the Identity in question.
  252. //
  253. GUID IdentificationGuid;
  254. //
  255. // last change of this identification.
  256. //
  257. FILETIME lastModified;
  258. } SAFER_IDENTIFICATION_HEADER, *PSAFER_IDENTIFICATION_HEADER;
  259. typedef struct _SAFER_PATHNAME_IDENTIFICATION
  260. {
  261. //
  262. // header.dwIdentificationType must be SaferIdentityTypeImageName
  263. // header.cbStructSize must be sizeof(SAFER_PATHNAME_IDENTIFICATION)
  264. //
  265. SAFER_IDENTIFICATION_HEADER header;
  266. //
  267. // user-entered description
  268. //
  269. WCHAR Description[SAFER_MAX_DESCRIPTION_SIZE];
  270. //
  271. // filepath or name, possibly with vars
  272. //
  273. PWCHAR ImageName;
  274. //
  275. // any combo of SAFER_POLICY_* flags
  276. //
  277. DWORD dwSaferFlags;
  278. } SAFER_PATHNAME_IDENTIFICATION, *PSAFER_PATHNAME_IDENTIFICATION;
  279. typedef struct _SAFER_HASH_IDENTIFICATION
  280. {
  281. //
  282. // header.dwIdentificationType must be SaferIdentityTypeImageHash
  283. // header.cbStructSize must be sizeof(SAFER_HASH_IDENTIFICATION)
  284. //
  285. SAFER_IDENTIFICATION_HEADER header;
  286. //
  287. // user-entered friendly name, initially from file's resources.
  288. //
  289. WCHAR Description[SAFER_MAX_DESCRIPTION_SIZE];
  290. //
  291. // user-entered description.
  292. //
  293. WCHAR FriendlyName[SAFER_MAX_FRIENDLYNAME_SIZE];
  294. //
  295. // amount of ImageHash actually used, in bytes (MD5 is 16 bytes).
  296. //
  297. DWORD HashSize;
  298. //
  299. // computed hash data itself.
  300. //
  301. BYTE ImageHash[SAFER_MAX_HASH_SIZE];
  302. //
  303. // algorithm in which the hash was computed (CALG_MD5, etc).
  304. //
  305. ALG_ID HashAlgorithm;
  306. //
  307. // size of the original file in bytes.
  308. //
  309. LARGE_INTEGER ImageSize;
  310. //
  311. // any combo of SAFER_POLICY_* flags
  312. //
  313. DWORD dwSaferFlags;
  314. } SAFER_HASH_IDENTIFICATION, *PSAFER_HASH_IDENTIFICATION;
  315. typedef struct _SAFER_URLZONE_IDENTIFICATION
  316. {
  317. //
  318. // header.dwIdentificationType must be SaferIdentityTypeUrlZone
  319. // header.cbStructSize must be sizeof(SAFER_URLZONE_IDENTIFICATION)
  320. //
  321. SAFER_IDENTIFICATION_HEADER header;
  322. //
  323. // any single URLZONE_* from urlmon.h
  324. //
  325. DWORD UrlZoneId;
  326. //
  327. // any combo of SAFER_POLICY_* flags
  328. //
  329. DWORD dwSaferFlags;
  330. } SAFER_URLZONE_IDENTIFICATION, *PSAFER_URLZONE_IDENTIFICATION;
  331. #include <poppack.h>
  332. //
  333. // Functions related to querying and setting the global policy
  334. // controls to disable transparent enforcement, and perform level
  335. // enumeration operations.
  336. //
  337. WINADVAPI
  338. BOOL WINAPI
  339. SaferGetPolicyInformation(
  340. IN DWORD dwScopeId,
  341. IN SAFER_POLICY_INFO_CLASS SaferPolicyInfoClass,
  342. IN DWORD InfoBufferSize,
  343. IN OUT PVOID InfoBuffer,
  344. IN OUT PDWORD InfoBufferRetSize,
  345. IN LPVOID lpReserved
  346. );
  347. WINADVAPI
  348. BOOL WINAPI
  349. SaferSetPolicyInformation(
  350. IN DWORD dwScopeId,
  351. IN SAFER_POLICY_INFO_CLASS SaferPolicyInfoClass,
  352. IN DWORD InfoBufferSize,
  353. IN PVOID InfoBuffer,
  354. IN LPVOID lpReserved
  355. );
  356. //
  357. // Functions to open or close a handle to a Safer Level.
  358. //
  359. WINADVAPI
  360. BOOL WINAPI
  361. SaferCreateLevel(
  362. IN DWORD dwScopeId,
  363. IN DWORD dwLevelId,
  364. IN DWORD OpenFlags,
  365. OUT SAFER_LEVEL_HANDLE * pLevelHandle,
  366. IN LPVOID lpReserved
  367. );
  368. WINADVAPI
  369. BOOL WINAPI
  370. SaferCloseLevel(
  371. IN SAFER_LEVEL_HANDLE hLevelHandle
  372. );
  373. WINADVAPI
  374. BOOL WINAPI
  375. SaferIdentifyLevel(
  376. IN DWORD dwNumProperties,
  377. IN PSAFER_CODE_PROPERTIES pCodeProperties,
  378. OUT SAFER_LEVEL_HANDLE * pLevelHandle,
  379. IN LPVOID lpReserved
  380. );
  381. WINADVAPI
  382. BOOL WINAPI
  383. SaferComputeTokenFromLevel(
  384. IN SAFER_LEVEL_HANDLE LevelHandle,
  385. IN HANDLE InAccessToken OPTIONAL,
  386. OUT PHANDLE OutAccessToken,
  387. IN DWORD dwFlags,
  388. IN LPVOID lpReserved
  389. );
  390. WINADVAPI
  391. BOOL WINAPI
  392. SaferGetLevelInformation(
  393. IN SAFER_LEVEL_HANDLE LevelHandle,
  394. IN SAFER_OBJECT_INFO_CLASS dwInfoType,
  395. OUT LPVOID lpQueryBuffer OPTIONAL,
  396. IN DWORD dwInBufferSize,
  397. OUT LPDWORD lpdwOutBufferSize
  398. );
  399. WINADVAPI
  400. BOOL WINAPI
  401. SaferSetLevelInformation(
  402. IN SAFER_LEVEL_HANDLE LevelHandle,
  403. IN SAFER_OBJECT_INFO_CLASS dwInfoType,
  404. IN LPVOID lpQueryBuffer,
  405. IN DWORD dwInBufferSize
  406. );
  407. //
  408. // This function performs logging of messages to the Application
  409. // event log. This is called by the hooks within CreateProcess,
  410. // ShellExecute and cmd when a lower trust evaluation result occurs.
  411. //
  412. WINADVAPI
  413. BOOL WINAPI
  414. SaferRecordEventLogEntry(
  415. IN SAFER_LEVEL_HANDLE hLevel,
  416. IN LPCWSTR szTargetPath,
  417. IN LPVOID lpReserved
  418. );
  419. #ifdef __cplusplus
  420. }
  421. #endif
  422. #endif