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.

688 lines
15 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. sep.h
  5. Abstract:
  6. This module contains the internal (private) declarations needed by the
  7. Kernel mode security routines.
  8. Author:
  9. Gary Kimura (GaryKi) 31-Mar-1989
  10. Jim Kelly (JimK) 2-Mar-1990
  11. Revision History:
  12. --*/
  13. #ifndef _SEP_
  14. #define _SEP_
  15. #include "ntos.h"
  16. #include <ntrmlsa.h>
  17. #include "seopaque.h"
  18. /////////////////////////////////////////////////////////////////////////
  19. // //
  20. // SE Diagnostics //
  21. // //
  22. /////////////////////////////////////////////////////////////////////////
  23. #if DBG
  24. #define SE_DIAGNOSTICS_ENABLED 1
  25. #endif // DBG
  26. //
  27. // These definitions are useful diagnostics aids
  28. //
  29. #if SE_DIAGNOSTICS_ENABLED
  30. //
  31. // Test for enabled diagnostic
  32. //
  33. #define IF_SE_GLOBAL( FlagName ) \
  34. if (SeGlobalFlag & (SE_DIAG_##FlagName))
  35. //
  36. // Diagnostics print statement
  37. //
  38. #define SeDiagPrint( FlagName, _Text_ ) \
  39. IF_SE_GLOBAL( FlagName ) \
  40. DbgPrint _Text_
  41. #else
  42. //
  43. // diagnostics not enabled - No diagnostics included in build
  44. //
  45. //
  46. // Test for diagnostics enabled
  47. //
  48. #define IF_SE_GLOBAL( FlagName ) if (FALSE)
  49. //
  50. // Diagnostics print statement (expands to no-op)
  51. //
  52. #define SeDiagPrint( FlagName, _Text_ ) ;
  53. #endif // SE_DIAGNOSTICS_ENABLED
  54. //
  55. // The following flags enable or disable various diagnostic
  56. // capabilities within SE code. These flags are set in
  57. // SeGlobalFlag (only available within a DBG system).
  58. //
  59. // SD_TRACKING - Display information about create/deletion of
  60. // shared security descriptors
  61. //
  62. //
  63. #define SE_DIAG_SD_TRACKING ((ULONG) 0x00000001L)
  64. //
  65. // Control flag manipulation macros
  66. //
  67. //
  68. // Macro to query whether or not control flags ALL on
  69. // or not (ie, returns FALSE if any of the flags are not set)
  70. //
  71. #define SepAreFlagsSet( Mask, Bits ) \
  72. ( \
  73. ((Mask) & ( Bits )) == ( Bits ) \
  74. )
  75. //
  76. // Macro to set the specified control bits in the given Security Descriptor
  77. //
  78. #define SepSetFlags( Mask, Bits ) \
  79. ( \
  80. ( Mask ) |= ( Bits ) \
  81. )
  82. //
  83. // Macro to clear the passed control bits in the given Security Descriptor
  84. //
  85. #define SepClearFlags( Mask, Bits ) \
  86. ( \
  87. ( Mask ) &= ~( Bits ) \
  88. )
  89. //
  90. // Macro to determine the size of a PRIVILEGE_SET
  91. //
  92. #define SepPrivilegeSetSize( PrivilegeSet ) \
  93. ( ( PrivilegeSet ) == NULL ? 0 : \
  94. ((( PrivilegeSet )->PrivilegeCount > 0) \
  95. ? \
  96. ((ULONG)sizeof(PRIVILEGE_SET) + \
  97. ( \
  98. (( PrivilegeSet )->PrivilegeCount - ANYSIZE_ARRAY) * \
  99. (ULONG)sizeof(LUID_AND_ATTRIBUTES) \
  100. ) \
  101. ) \
  102. : ((ULONG)sizeof(PRIVILEGE_SET) - (ULONG)sizeof(LUID_AND_ATTRIBUTES)) \
  103. ))
  104. //
  105. // Return the effective token from a SecurityContext
  106. //
  107. #define EffectiveToken( SubjectSecurityContext ) ( \
  108. (SubjectSecurityContext)->ClientToken ? \
  109. (SubjectSecurityContext)->ClientToken : \
  110. (SubjectSecurityContext)->PrimaryToken \
  111. ) \
  112. //
  113. // Return a pointer to the Sid of the User of a given token
  114. //
  115. #define SepTokenUserSid( Token ) ((PTOKEN)(Token))->UserAndGroups->Sid
  116. //
  117. // Return the AuthenticationId from a given token
  118. //
  119. #define SepTokenAuthenticationId( Token ) (((PTOKEN)(Token))->AuthenticationId)
  120. //
  121. //
  122. // BOOLEAN
  123. // SepBadImpersonationLevel(
  124. // IN SECURITY_IMPERSONATION_LEVEL ImpersonationLevel,
  125. // IN BOOLEAN ServerIsRemote
  126. // )
  127. //
  128. // Routine Description:
  129. //
  130. // Determine whether a client is trying to impersonate innappropriately
  131. // This routine should only be called if a thread requesting impersonation
  132. // is itself already impersonating a client of its own. This routine
  133. // indicates whether the client is attempting to violate the level of
  134. // impersonation granted to it by its client.
  135. //
  136. // Arguments:
  137. //
  138. // ImpersonationLevel - Is the impersonation level of the client's
  139. // effective token.
  140. //
  141. // ServerIsRemote - Is a boolean flag indicating whether the client
  142. // is requesting impersonation services to a remote system. TRUE
  143. // indicates the session is a remote session, FALSE indicates the
  144. // session is a local session. Delegation level is necessary to
  145. // achieve a remote session.
  146. //
  147. // Return Value:
  148. //
  149. // TRUE - Indicates that the impersonation level of the client's client
  150. // token is innapropriate for the attempted impersonation.
  151. // An error (STATUS_BAD_IMPERSONATION_LEVEL) should be generated.
  152. //
  153. // FALSE - Indicates the impersonation attempt is not bad, and should
  154. // be allowed.
  155. //
  156. //
  157. #define SepBadImpersonationLevel(IL,SIR) (( \
  158. ((IL) == SecurityAnonymous) || ((IL) == SecurityIdentification) || \
  159. ( (SIR) && ((IL) != SecurityDelegation) ) \
  160. ) ? TRUE : FALSE )
  161. //++
  162. //
  163. // BOOL
  164. // IsValidElementCount(
  165. // IN ULONG Count,
  166. // IN <STRUCTURE>
  167. // );
  168. //
  169. //--
  170. #define IsValidElementCount( Count, STRUCTURE ) \
  171. ( Count < ( (ULONG_PTR) ( (PUCHAR) ( (PUCHAR) (LONG_PTR)(LONG)0xFFFFFFFF - (PUCHAR) MM_SYSTEM_RANGE_START ) + 1 ) \
  172. / sizeof( STRUCTURE ) ) )
  173. #define SEP_MAX_PRIVILEGE_COUNT (SE_MAX_WELL_KNOWN_PRIVILEGE-SE_MIN_WELL_KNOWN_PRIVILEGE+1)
  174. #define IsValidPrivilegeCount( count ) ((count == 0) || \
  175. (count <= SEP_MAX_PRIVILEGE_COUNT))
  176. ///////////////////////////////////////////////////////////////////////////
  177. // //
  178. // Constants //
  179. // //
  180. ///////////////////////////////////////////////////////////////////////////
  181. #define SEP_MAX_GROUP_COUNT 4096
  182. ///////////////////////////////////////////////////////////////////////////
  183. // //
  184. // Private Data types //
  185. // //
  186. ///////////////////////////////////////////////////////////////////////////
  187. extern HANDLE SepLsaHandle;
  188. //extern BOOLEAN SepAuditShutdownEvents;
  189. //
  190. // Spinlock protecting the queue of work being passed to LSA
  191. //
  192. extern ERESOURCE SepLsaQueueLock;
  193. extern ULONG SepLsaQueueLength;
  194. //
  195. // Doubly linked list of work items queued to worker threads.
  196. //
  197. extern LIST_ENTRY SepLsaQueue;
  198. // #define SepAcquireTokenReadLock(T) KeEnterCriticalRegion(); \
  199. // ExAcquireResourceSharedLite(&SepTokenLock, TRUE)
  200. #define SepLockLsaQueue() KeEnterCriticalRegion(); \
  201. ExAcquireResourceExclusiveLite(&SepLsaQueueLock, TRUE)
  202. #define SepUnlockLsaQueue() ExReleaseResourceLite(&SepLsaQueueLock); \
  203. KeLeaveCriticalRegion()
  204. #define SepWorkListHead() ((PSEP_LSA_WORK_ITEM)(&SepLsaQueue)->Flink)
  205. #define SepWorkListEmpty() (IsListEmpty (&SepLsaQueue))
  206. #ifndef ExAllocatePool
  207. #define ExAllocatePool(a,b) ExAllocatePoolWithTag(a,b,' eS')
  208. #endif
  209. #ifndef ExAllocatePoolWithQuota
  210. #define ExAllocatePoolWithQuota(a,b) ExAllocatePoolWithQuotaTag(a,b,' eS')
  211. #endif
  212. typedef
  213. VOID
  214. (*PSEP_LSA_WORKER_CLEANUP_ROUTINE)(
  215. IN PVOID Parameter
  216. );
  217. typedef enum _SEP_LSA_WORK_ITEM_TAG {
  218. SepDeleteLogon,
  219. SepAuditRecord
  220. } SEP_LSA_WORK_ITEM_TAG, *PSEP_LSA_WORK_ITEM_TAG;
  221. typedef struct _SEP_LSA_WORK_ITEM {
  222. //
  223. // This field must be the first field of this structure
  224. //
  225. LIST_ENTRY List;
  226. //
  227. // Command Params Memory type
  228. //
  229. SEP_RM_LSA_MEMORY_TYPE CommandParamsMemoryType;
  230. //
  231. // Tag describing what kind of structure we've got
  232. //
  233. SEP_LSA_WORK_ITEM_TAG Tag;
  234. //
  235. // The following union contains the data to be passed
  236. // to LSA.
  237. //
  238. union {
  239. PVOID BaseAddress;
  240. LUID LogonId;
  241. } CommandParams;
  242. //
  243. // These fields must be filled in by the caller of SepRmCallLsa
  244. //
  245. LSA_COMMAND_NUMBER CommandNumber;
  246. ULONG CommandParamsLength;
  247. PVOID ReplyBuffer;
  248. ULONG ReplyBufferLength;
  249. //
  250. // CleanupFunction (if specified) will be called with CleanupParameter
  251. // as its argument before the SEP_LSA_WORK_ITEM is freed by SepRmCallLsa
  252. //
  253. PSEP_LSA_WORKER_CLEANUP_ROUTINE CleanupFunction;
  254. PVOID CleanupParameter;
  255. } SEP_LSA_WORK_ITEM, *PSEP_LSA_WORK_ITEM;
  256. typedef struct _SEP_WORK_ITEM {
  257. WORK_QUEUE_ITEM WorkItem;
  258. } SEP_WORK_ITEM, *PSEP_WORK_ITEM;
  259. extern SEP_WORK_ITEM SepExWorkItem;
  260. ///////////////////////////////////////////////////////////////////////////
  261. // //
  262. // Private Routines //
  263. // //
  264. ///////////////////////////////////////////////////////////////////////////
  265. BOOLEAN
  266. SepDevelopmentTest( VOID ); //Used only for development testing
  267. BOOLEAN
  268. SepInitializationPhase0( VOID );
  269. BOOLEAN
  270. SepInitializationPhase1( VOID );
  271. BOOLEAN
  272. SepVariableInitialization( VOID );
  273. NTSTATUS
  274. SepCreateToken(
  275. OUT PHANDLE TokenHandle,
  276. IN KPROCESSOR_MODE RequestorMode,
  277. IN ACCESS_MASK DesiredAccess,
  278. IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL,
  279. IN TOKEN_TYPE TokenType,
  280. IN SECURITY_IMPERSONATION_LEVEL ImpersonationLevel OPTIONAL,
  281. IN PLUID AuthenticationId,
  282. IN PLARGE_INTEGER ExpirationTime,
  283. IN PSID_AND_ATTRIBUTES User,
  284. IN ULONG GroupCount,
  285. IN PSID_AND_ATTRIBUTES Groups,
  286. IN ULONG GroupsLength,
  287. IN ULONG PrivilegeCount,
  288. IN PLUID_AND_ATTRIBUTES Privileges,
  289. IN PSID Owner OPTIONAL,
  290. IN PSID PrimaryGroup,
  291. IN PACL DefaultDacl OPTIONAL,
  292. IN PTOKEN_SOURCE TokenSource,
  293. IN BOOLEAN SystemToken,
  294. IN PSECURITY_TOKEN_PROXY_DATA ProxyData OPTIONAL,
  295. IN PSECURITY_TOKEN_AUDIT_DATA AuditData OPTIONAL
  296. );
  297. NTSTATUS
  298. SepReferenceLogonSession(
  299. IN PLUID LogonId
  300. );
  301. VOID
  302. SepDeReferenceLogonSession(
  303. IN PLUID LogonId
  304. );
  305. #define TOKEN_LEAK_MONITOR 0
  306. #if DBG || TOKEN_LEAK_MONITOR
  307. VOID
  308. SepAddTokenLogonSession(
  309. IN PACCESS_TOKEN Token
  310. );
  311. VOID
  312. SepRemoveTokenLogonSession(
  313. IN PACCESS_TOKEN Token
  314. );
  315. #endif
  316. VOID
  317. SepLockSubjectContext(
  318. IN PSECURITY_SUBJECT_CONTEXT SubjectContext
  319. );
  320. VOID
  321. SepFreeSubjectContext(
  322. IN PSECURITY_SUBJECT_CONTEXT SubjectContext
  323. );
  324. VOID
  325. SepGetDefaultsSubjectContext(
  326. IN PSECURITY_SUBJECT_CONTEXT SubjectContext,
  327. OUT PSID *Owner,
  328. OUT PSID *Group,
  329. OUT PSID *ServerOwner,
  330. OUT PSID *ServerGroup,
  331. OUT PACL *Dacl
  332. );
  333. BOOLEAN
  334. SepValidOwnerSubjectContext(
  335. IN PSECURITY_SUBJECT_CONTEXT SubjectContext,
  336. IN PSID Owner,
  337. IN BOOLEAN ServerObject
  338. );
  339. BOOLEAN
  340. SepIdAssignableAsGroup(
  341. IN PACCESS_TOKEN Token,
  342. IN PSID Group
  343. );
  344. BOOLEAN
  345. SepCheckAcl (
  346. IN PACL Acl,
  347. IN ULONG Length
  348. );
  349. BOOLEAN
  350. SepAuditAlarm (
  351. IN PUNICODE_STRING SubsystemName,
  352. IN PVOID HandleId,
  353. IN PUNICODE_STRING ObjectTypeName,
  354. IN PUNICODE_STRING ObjectName,
  355. IN PSECURITY_DESCRIPTOR SecurityDescriptor,
  356. IN ACCESS_MASK DesiredAccess,
  357. IN BOOLEAN ObjectCreation,
  358. IN ACCESS_MASK GrantedAccess,
  359. OUT PBOOLEAN GenerateOnClose
  360. );
  361. BOOLEAN
  362. SepSinglePrivilegeCheck (
  363. LUID DesiredPrivilege,
  364. IN PACCESS_TOKEN EffectiveToken,
  365. IN KPROCESSOR_MODE PreviousMode
  366. );
  367. NTSTATUS
  368. SepRmCallLsa(
  369. PSEP_WORK_ITEM SepWorkItem
  370. );
  371. BOOLEAN
  372. SepInitializeWorkList(
  373. VOID
  374. );
  375. BOOLEAN
  376. SepRmInitPhase0(
  377. );
  378. VOID
  379. SepConcatenatePrivileges(
  380. IN PPRIVILEGE_SET TargetPrivilegeSet,
  381. IN ULONG TargetBufferSize,
  382. IN PPRIVILEGE_SET SourcePrivilegeSet
  383. );
  384. BOOLEAN
  385. SepTokenIsOwner(
  386. IN PACCESS_TOKEN Token,
  387. IN PSECURITY_DESCRIPTOR SecurityDescriptor,
  388. IN BOOLEAN TokenLocked
  389. );
  390. #if DBG
  391. VOID
  392. SepPrintAcl (
  393. IN PACL Acl
  394. );
  395. VOID
  396. SepPrintSid(
  397. IN PSID Sid
  398. );
  399. VOID
  400. SepDumpSecurityDescriptor(
  401. IN PSECURITY_DESCRIPTOR SecurityDescriptor,
  402. IN PSZ TitleString
  403. );
  404. BOOLEAN
  405. SepSidTranslation(
  406. PSID Sid,
  407. PSTRING AccountName
  408. );
  409. VOID
  410. SepDumpTokenInfo(
  411. IN PACCESS_TOKEN Token
  412. );
  413. VOID
  414. SepDumpString(
  415. IN PUNICODE_STRING String
  416. );
  417. #endif //DBG
  418. BOOLEAN
  419. SepSidInToken (
  420. IN PACCESS_TOKEN Token,
  421. IN PSID PrincipalSelfSid,
  422. IN PSID Sid,
  423. IN BOOLEAN DenyAce
  424. );
  425. VOID
  426. SepExamineSacl(
  427. IN PACL Sacl,
  428. IN PACCESS_TOKEN Token,
  429. IN ACCESS_MASK DesiredAccess,
  430. IN BOOLEAN AccessGranted,
  431. OUT PBOOLEAN GenerateAudit,
  432. OUT PBOOLEAN GenerateAlarm
  433. );
  434. VOID
  435. SepCopyString (
  436. IN PUNICODE_STRING SourceString,
  437. OUT PUNICODE_STRING *DestString
  438. );
  439. VOID
  440. SepAssemblePrivileges(
  441. IN ULONG PrivilegeCount,
  442. IN BOOLEAN SystemSecurity,
  443. IN BOOLEAN WriteOwner,
  444. OUT PPRIVILEGE_SET *Privileges
  445. );
  446. PUNICODE_STRING
  447. SepQueryTypeString(
  448. IN PVOID Object
  449. );
  450. POBJECT_NAME_INFORMATION
  451. SepQueryNameString(
  452. IN PVOID Object
  453. );
  454. BOOLEAN
  455. SepFilterPrivilegeAudits(
  456. IN PPRIVILEGE_SET PrivilegeSet
  457. );
  458. BOOLEAN
  459. SepQueueWorkItem(
  460. IN PSEP_LSA_WORK_ITEM LsaWorkItem,
  461. IN BOOLEAN ForceQueue
  462. );
  463. PSEP_LSA_WORK_ITEM
  464. SepDequeueWorkItem(
  465. VOID
  466. );
  467. VOID
  468. SepAdtGenerateDiscardAudit(
  469. VOID
  470. );
  471. BOOLEAN
  472. SepAdtValidateAuditBounds(
  473. ULONG Upper,
  474. ULONG Lower
  475. );
  476. NTSTATUS
  477. SepAdtInitializeCrashOnFail(
  478. VOID
  479. );
  480. BOOLEAN
  481. SepAdtInitializePrivilegeAuditing(
  482. VOID
  483. );
  484. NTSTATUS
  485. SepCopyProxyData (
  486. OUT PSECURITY_TOKEN_PROXY_DATA * DestProxyData,
  487. IN PSECURITY_TOKEN_PROXY_DATA SourceProxyData
  488. );
  489. VOID
  490. SepFreeProxyData (
  491. IN PSECURITY_TOKEN_PROXY_DATA ProxyData
  492. );
  493. NTSTATUS
  494. SepProbeAndCaptureQosData(
  495. IN PSECURITY_ADVANCED_QUALITY_OF_SERVICE CapturedSecurityQos
  496. );
  497. PACCESS_TOKEN
  498. SeMakeAnonymousToken ();
  499. VOID
  500. SepAuditAssignPrimaryToken(
  501. IN PEPROCESS Process,
  502. IN PACCESS_TOKEN NewAccessToken
  503. );
  504. #endif // _SEP_