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.

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