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.

574 lines
16 KiB

  1. /*++
  2. Copyright (c) 1997-2000 Microsoft Corporation
  3. Module Name:
  4. saferp.h
  5. Abstract:
  6. This file implements the private (internal) functions, data types,
  7. data structures, and definitions used by the other WinSAFER
  8. code implementations. All of the APIs listed in this header are
  9. not exported by ADVAPI32.DLL at all and are only callable by
  10. other code actually located within advapi.
  11. Author:
  12. Jeffrey Lawson (JLawson)
  13. Revision History:
  14. --*/
  15. #ifndef _AUTHZSAFERP_H_
  16. #define _AUTHZSAFERP_H_
  17. #include "safewild.h"
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. // ---------------------------------------------------------
  22. //
  23. // Convenient macro for determining the number of elements in an array.
  24. //
  25. #ifndef ARRAYSIZE
  26. #define ARRAYSIZE(a) (sizeof(a)/sizeof(a[0]))
  27. #endif
  28. //
  29. // Simple inlined function to return true if a GUID is all zeros.
  30. //
  31. FORCEINLINE BOOLEAN IsZeroGUID(REFGUID rguid1)
  32. {
  33. return (
  34. ((unsigned long *) rguid1)[0] == 0 &&
  35. ((unsigned long *) rguid1)[1] == 0 &&
  36. ((unsigned long *) rguid1)[2] == 0 &&
  37. ((unsigned long *) rguid1)[3] == 0);
  38. }
  39. //
  40. // Private structure used to store a table of all of the defined
  41. // WinSafer Levels as we enumerate them to evaluate the matching one.
  42. //
  43. typedef struct _AUTHZLEVELTABLERECORD
  44. {
  45. // The user-defined integer value that controls the relative ranking
  46. // of authorization level between Code Authorization Level.
  47. DWORD dwLevelId;
  48. // Boolean indicating whether this level is a "built-in" one.
  49. BOOLEAN Builtin;
  50. // Boolean indicating whether this level is enumerable
  51. BOOLEAN isEnumerable;
  52. // The short friendly name and the description.
  53. UNICODE_STRING UnicodeFriendlyName;
  54. UNICODE_STRING UnicodeDescription;
  55. // All of the following attributes are needed for
  56. // actual creation of the restricted token.
  57. BOOL DisallowExecution; // block execution entirely
  58. BOOL DisableMaxPrivileges; // privilege options
  59. PSID DefaultOwner; // default owner SID
  60. DWORD SaferFlags; // special job execution flags
  61. BOOL InvertDisableSids; // SIDs specified are negative
  62. DWORD DisableSidCount; // number of deny-only SIDs
  63. DWORD DisableSidUsedCount; // number actually used
  64. PAUTHZ_WILDCARDSID SidsToDisable; // deny-only SIDs
  65. BOOL InvertDeletePrivs; // privileges specified are negative
  66. DWORD DeletePrivilegeCount; // number of privileges
  67. DWORD DeletePrivilegeUsedCount; // number actually used
  68. PLUID_AND_ATTRIBUTES PrivilegesToDelete; // privileges
  69. DWORD RestrictedSidsInvCount; // number of inverted restricting SIDs
  70. DWORD RestrictedSidsInvUsedCount; // number actually used
  71. PAUTHZ_WILDCARDSID RestrictedSidsInv; // list of inverted restricting SIDs
  72. DWORD RestrictedSidsAddedCount; // number of restricting SIDs
  73. DWORD RestrictedSidsAddedUsedCount; // number actually used
  74. PSID_AND_ATTRIBUTES RestrictedSidsAdded; // list of restricting SIDs
  75. }
  76. AUTHZLEVELTABLERECORD, *PAUTHZLEVELTABLERECORD;
  77. //
  78. // Private structure to store all code identifications.
  79. //
  80. #pragma warning(push)
  81. #pragma warning(disable:4201) // nonstandard extension used : nameless struct/union
  82. typedef struct _AUTHZIDENTSTABLERECORD
  83. {
  84. // unique identifier that distinguishes this code identity.
  85. GUID IdentGuid;
  86. // the following enumeration specifies what type of
  87. // code identity this record represents.
  88. SAFER_IDENTIFICATION_TYPES dwIdentityType;
  89. // Specifies what Level this Code Identification maps to.
  90. DWORD dwLevelId;
  91. // Specifies what scope this Code Identity was loaded from.
  92. DWORD dwScopeId;
  93. // Actual details about this identity.
  94. union {
  95. struct {
  96. BOOL bExpandVars;
  97. UNICODE_STRING ImagePath;
  98. DWORD dwSaferFlags;
  99. } ImageNameInfo;
  100. struct {
  101. LARGE_INTEGER ImageSize;
  102. DWORD HashSize;
  103. BYTE ImageHash[SAFER_MAX_HASH_SIZE];
  104. ALG_ID HashAlgorithm;
  105. DWORD dwSaferFlags;
  106. } ImageHashInfo;
  107. struct {
  108. DWORD UrlZoneId;
  109. DWORD dwSaferFlags;
  110. } ImageZone;
  111. };
  112. }
  113. AUTHZIDENTSTABLERECORD, *PAUTHZIDENTSTABLERECORD;
  114. #pragma warning(pop)
  115. //
  116. // Private structure representation of a Level handle. The
  117. // typedef SAFER_LEVEL_HANDLE is an opaque reference to a structure of
  118. // this type, accessed via the RtlHandleTable functions.
  119. //
  120. typedef struct _AUTHZLEVELHANDLESTRUCT_
  121. {
  122. // This first header is required by the RTL_HANDLE_TABLE system.
  123. // All allocated handles will implicitly have bit 0 set. All other
  124. // remaining bits can be used for our own purposes if we want.
  125. RTL_HANDLE_TABLE_ENTRY HandleHeader;
  126. // The following information is redundant. It can be found by
  127. // also accessing the pLevelRecord directly.
  128. DWORD dwLevelId;
  129. // This scope identifier specifies the value that was passed to
  130. // the Win32 API SaferCreateLevel and is really only looked at
  131. // by SaferGetLevelInformation for the Identity GUID enums.
  132. DWORD dwScopeId; // (same as from pIdentRecord)
  133. // Stores the matching identity record that gave this result.
  134. // May be NULL, as in case of direct SaferCreateLevel or a
  135. // default Level match.
  136. GUID identGuid;
  137. // This value stores the Safer Flags that were derived from the
  138. // Identity Entry record when SaferIdentifyLevel finds a match.
  139. DWORD dwSaferFlags;
  140. // The sequence value indicates the "generation" at which a handle
  141. // was originally opened. If this value does not match the current
  142. // value in the global g_dwLevelHandleSequence, then this handle
  143. // should be considered a no-longer valid handle.
  144. DWORD dwHandleSequence;
  145. // Extended error information - applicable for certificate rules.
  146. DWORD dwExtendedError;
  147. // the following enumeration specifies what type of
  148. // code identity this handle represents.
  149. SAFER_IDENTIFICATION_TYPES IdentificationType;
  150. // For future use and padding purposes.
  151. DWORD dwReserved;
  152. }
  153. AUTHZLEVELHANDLESTRUCT, *PAUTHZLEVELHANDLESTRUCT;
  154. //
  155. // Private structure definition used to pass around all state
  156. // information needed during the SaferIdentifyLevel execution.
  157. //
  158. typedef struct _LOCALIDENTITYCONTEXT
  159. {
  160. // Original query request data.
  161. DWORD dwCheckFlags; // copy of original function input
  162. PSAFER_CODE_PROPERTIES CodeProps; // RO: original function input
  163. // Information about the hash that may have been computed.
  164. BOOLEAN bHaveHash;
  165. BYTE FinalHash[SAFER_MAX_HASH_SIZE];
  166. DWORD FinalHashSize;
  167. ALG_ID FinalHashAlgorithm;
  168. // File handle that may have been opened or supplied by the caller.
  169. HANDLE hFileHandle;
  170. // File handle status. If this is TRUE then hFileHandle needs to
  171. // be closed before returning.
  172. BOOLEAN bCloseFileHandle;
  173. // Fully qualified NT filename of the input file.
  174. UNICODE_STRING UnicodeFullyQualfiedLongFileName;
  175. // Information about the image that may have been mapped.
  176. LARGE_INTEGER ImageSize;
  177. PVOID pImageMemory;
  178. // Memory mapped file status. If this is TRUE then
  179. // pImageMemory needs to be unmapped before returning.
  180. BOOLEAN bImageMemoryNeedUnmap;
  181. }
  182. LOCALIDENTITYCONTEXT, *PLOCALIDENTITYCONTEXT;
  183. //
  184. // Various globals that are used for the cache of levels and
  185. // identities so that we do not need to go to the registry each time.
  186. //
  187. extern BOOLEAN g_bInitializedFirstTime;
  188. extern CRITICAL_SECTION g_TableCritSec;
  189. extern HANDLE g_hKeyCustomRoot;
  190. extern DWORD g_dwKeyOptions;
  191. extern BOOLEAN g_bNeedCacheReload;
  192. extern RTL_GENERIC_TABLE g_CodeLevelObjTable;
  193. extern RTL_GENERIC_TABLE g_CodeIdentitiesTable;
  194. extern RTL_HANDLE_TABLE g_LevelHandleTable;
  195. extern DWORD g_dwLevelHandleSequence;
  196. extern BOOLEAN g_bHonorScopeUser;
  197. extern PAUTHZLEVELTABLERECORD g_DefaultCodeLevel;
  198. extern PAUTHZLEVELTABLERECORD g_DefaultCodeLevelUser;
  199. extern PAUTHZLEVELTABLERECORD g_DefaultCodeLevelMachine;
  200. //
  201. // Private function prototypes defined within SAFEINIT.C
  202. //
  203. NTSTATUS NTAPI
  204. CodeAuthzInitializeGlobals(VOID);
  205. VOID NTAPI
  206. CodeAuthzDeinitializeGlobals(VOID);
  207. NTSTATUS NTAPI
  208. CodeAuthzReloadCacheTables(
  209. IN HANDLE hKeyCustomRoot OPTIONAL,
  210. IN DWORD dwKeyOptions,
  211. IN BOOLEAN bImmediateLoad
  212. );
  213. NTSTATUS NTAPI
  214. CodeAuthzpImmediateReloadCacheTables(
  215. VOID
  216. );
  217. NTSTATUS NTAPI
  218. CodeAuthzpDeleteKeyRecursively(
  219. IN HANDLE hBaseKey,
  220. IN PUNICODE_STRING pSubKey OPTIONAL
  221. );
  222. NTSTATUS NTAPI
  223. CodeAuthzpFormatLevelKeyPath(
  224. IN DWORD dwLevelId,
  225. IN OUT PUNICODE_STRING UnicodeSuffix
  226. );
  227. NTSTATUS NTAPI
  228. CodeAuthzpFormatIdentityKeyPath(
  229. IN DWORD dwLevelId,
  230. IN LPCWSTR szIdentityType,
  231. IN REFGUID refIdentGuid,
  232. IN OUT PUNICODE_STRING UnicodeSuffix
  233. );
  234. NTSTATUS NTAPI
  235. CodeAuthzpOpenPolicyRootKey(
  236. IN DWORD dwScopeId,
  237. IN HANDLE hKeyCustomBase OPTIONAL,
  238. IN LPCWSTR szRegistrySuffix OPTIONAL,
  239. IN ACCESS_MASK DesiredAccess,
  240. IN BOOLEAN bCreateKey,
  241. OUT HANDLE *OpenedHandle
  242. );
  243. VOID NTAPI
  244. CodeAuthzpRecomputeEffectiveDefaultLevel(VOID);
  245. //
  246. // Private function prototypes defined within SAFEHAND.C
  247. //
  248. NTSTATUS NTAPI
  249. CodeAuthzpCreateLevelHandleFromRecord(
  250. IN PAUTHZLEVELTABLERECORD pLevelRecord,
  251. IN DWORD dwScopeId,
  252. IN DWORD dwSaferFlags OPTIONAL,
  253. IN DWORD dwExtendedError,
  254. IN SAFER_IDENTIFICATION_TYPES IdentificationType,
  255. IN REFGUID refIdentGuid OPTIONAL,
  256. OUT SAFER_LEVEL_HANDLE *pLevelHandle
  257. );
  258. NTSTATUS NTAPI
  259. CodeAuthzHandleToLevelStruct(
  260. IN SAFER_LEVEL_HANDLE hLevelObject,
  261. OUT PAUTHZLEVELHANDLESTRUCT *pLevelStruct
  262. );
  263. NTSTATUS NTAPI
  264. CodeAuthzCreateLevelHandle(
  265. IN DWORD dwLevelId,
  266. IN DWORD OpenFlags,
  267. IN DWORD dwScopeId,
  268. IN DWORD dwSaferFlags OPTIONAL,
  269. OUT SAFER_LEVEL_HANDLE *pLevelHandle);
  270. NTSTATUS NTAPI
  271. CodeAuthzCloseLevelHandle(
  272. IN SAFER_LEVEL_HANDLE hLevelObject
  273. );
  274. //
  275. // Functions related to WinSafer Level enumeration (SAFEIDEP.C)
  276. //
  277. VOID NTAPI
  278. CodeAuthzLevelObjpInitializeTable(
  279. IN OUT PRTL_GENERIC_TABLE pAuthzObjTable
  280. );
  281. NTSTATUS NTAPI
  282. CodeAuthzLevelObjpLoadTable (
  283. IN OUT PRTL_GENERIC_TABLE pAuthzObjTable,
  284. IN DWORD dwScopeId,
  285. IN HANDLE hKeyCustomRoot
  286. );
  287. VOID NTAPI
  288. CodeAuthzLevelObjpEntireTableFree (
  289. IN OUT PRTL_GENERIC_TABLE pAuthzObjTable
  290. );
  291. PAUTHZLEVELTABLERECORD NTAPI
  292. CodeAuthzLevelObjpLookupByLevelId (
  293. IN PRTL_GENERIC_TABLE pAuthzObjTable,
  294. IN DWORD dwLevelId
  295. );
  296. //
  297. // Functions related to WinSafer Code Identity enumeration. (SAFEIDEP.C)
  298. //
  299. VOID NTAPI
  300. CodeAuthzGuidIdentsInitializeTable(
  301. IN OUT PRTL_GENERIC_TABLE pAuthzObjTable
  302. );
  303. NTSTATUS NTAPI
  304. CodeAuthzGuidIdentsLoadTableAll (
  305. IN PRTL_GENERIC_TABLE pAuthzLevelTable,
  306. IN OUT PRTL_GENERIC_TABLE pAuthzIdentTable,
  307. IN DWORD dwScopeId,
  308. IN HANDLE hKeyCustomBase
  309. );
  310. VOID NTAPI
  311. CodeAuthzGuidIdentsEntireTableFree (
  312. IN OUT PRTL_GENERIC_TABLE pAuthzIdentTable
  313. );
  314. PAUTHZIDENTSTABLERECORD NTAPI
  315. CodeAuthzIdentsLookupByGuid (
  316. IN PRTL_GENERIC_TABLE pAuthzIdentTable,
  317. IN REFGUID pIdentGuid
  318. );
  319. //
  320. // Helper functions that are used during actual identification (SAFEIDEP.C)
  321. //
  322. LONG NTAPI
  323. CodeAuthzpCompareImagePath(
  324. IN LPCWSTR szPathFragment,
  325. IN LPCWSTR szFullImagePath);
  326. NTSTATUS NTAPI
  327. CodeAuthzpComputeImageHash(
  328. IN PVOID pImageMemory,
  329. IN DWORD dwImageSize,
  330. OUT PBYTE pComputedHash OPTIONAL,
  331. IN OUT PDWORD pdwHashSize OPTIONAL,
  332. OUT ALG_ID *pHashAlgorithm OPTIONAL
  333. );
  334. //
  335. // Private function prototypes for low-level policy reading/writing. (SAFEPOLR.C)
  336. //
  337. NTSTATUS NTAPI
  338. CodeAuthzPol_GetInfoCached_LevelListRaw(
  339. IN DWORD dwScopeId,
  340. IN DWORD InfoBufferSize OPTIONAL,
  341. OUT PVOID InfoBuffer OPTIONAL,
  342. OUT PDWORD InfoBufferRetSize OPTIONAL
  343. );
  344. NTSTATUS NTAPI
  345. CodeAuthzPol_GetInfoCached_DefaultLevel(
  346. IN DWORD dwScopeId,
  347. IN DWORD InfoBufferSize OPTIONAL,
  348. OUT PVOID InfoBuffer OPTIONAL,
  349. OUT PDWORD InfoBufferRetSize OPTIONAL
  350. );
  351. NTSTATUS NTAPI
  352. CodeAuthzPol_GetInfoRegistry_DefaultLevel(
  353. IN DWORD dwScopeId,
  354. IN DWORD InfoBufferSize OPTIONAL,
  355. OUT PVOID InfoBuffer OPTIONAL,
  356. OUT PDWORD InfoBufferRetSize OPTIONAL
  357. );
  358. NTSTATUS NTAPI
  359. CodeAuthzPol_SetInfoDual_DefaultLevel(
  360. IN DWORD dwScopeId,
  361. IN DWORD InfoBufferSize,
  362. OUT PVOID InfoBuffer
  363. );
  364. NTSTATUS NTAPI
  365. CodeAuthzPol_GetInfoCached_HonorUserIdentities(
  366. IN DWORD dwScopeId,
  367. IN DWORD InfoBufferSize OPTIONAL,
  368. OUT PVOID InfoBuffer OPTIONAL,
  369. OUT PDWORD InfoBufferRetSize OPTIONAL
  370. );
  371. NTSTATUS NTAPI
  372. CodeAuthzPol_GetInfoRegistry_HonorUserIdentities(
  373. IN DWORD dwScopeId,
  374. IN DWORD InfoBufferSize OPTIONAL,
  375. OUT PVOID InfoBuffer OPTIONAL,
  376. OUT PDWORD InfoBufferRetSize OPTIONAL
  377. );
  378. NTSTATUS NTAPI
  379. CodeAuthzPol_SetInfoDual_HonorUserIdentities(
  380. IN DWORD dwScopeId,
  381. IN DWORD InfoBufferSize,
  382. IN PVOID InfoBuffer
  383. );
  384. NTSTATUS NTAPI
  385. CodeAuthzPol_GetInfoRegistry_TransparentEnabled(
  386. IN DWORD dwScopeId,
  387. IN DWORD InfoBufferSize OPTIONAL,
  388. OUT PVOID InfoBuffer OPTIONAL,
  389. OUT PDWORD InfoBufferRetSize OPTIONAL
  390. );
  391. NTSTATUS NTAPI
  392. CodeAuthzPol_SetInfoRegistry_TransparentEnabled(
  393. IN DWORD dwScopeId,
  394. IN DWORD InfoBufferSize,
  395. IN PVOID InfoBuffer
  396. );
  397. NTSTATUS NTAPI
  398. CodeAuthzPol_GetInfoRegistry_ScopeFlags(
  399. IN DWORD dwScopeId,
  400. IN DWORD InfoBufferSize OPTIONAL,
  401. OUT PVOID InfoBuffer OPTIONAL,
  402. OUT PDWORD InfoBufferRetSize OPTIONAL
  403. );
  404. NTSTATUS NTAPI
  405. CodeAuthzPol_SetInfoRegistry_ScopeFlags(
  406. IN DWORD dwScopeId,
  407. IN DWORD InfoBufferSize,
  408. IN PVOID InfoBuffer
  409. );
  410. //
  411. // Private function prototypes defined elsewhere.
  412. //
  413. LPVOID NTAPI
  414. CodeAuthzpGetTokenInformation(
  415. IN HANDLE TokenHandle,
  416. IN TOKEN_INFORMATION_CLASS TokenInformationClass
  417. );
  418. NTSTATUS NTAPI
  419. CodeAuthzIsExecutableFileType(
  420. IN PUNICODE_STRING szFullPathname,
  421. IN BOOLEAN bFromShellExecute,
  422. OUT PBOOLEAN pbResult
  423. );
  424. NTSTATUS NTAPI
  425. CodeAuthzFullyQualifyFilename(
  426. IN HANDLE hFileHandle OPTIONAL,
  427. IN BOOLEAN bSourceIsNtPath,
  428. IN LPCWSTR szSourceFilePath,
  429. OUT PUNICODE_STRING pUnicodeResult
  430. );
  431. BOOL NTAPI
  432. SaferpLoadUnicodeResourceString(
  433. IN HANDLE hModule,
  434. IN UINT wID,
  435. OUT PUNICODE_STRING pUnicodeString,
  436. IN WORD wLangId
  437. );
  438. #ifdef __cplusplus
  439. }
  440. #endif
  441. #endif