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.

2593 lines
70 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. tokendup.c
  5. Abstract:
  6. This module implements the token duplication service.
  7. Author:
  8. Jim Kelly (JimK) 5-April-1990
  9. Environment:
  10. Kernel mode only.
  11. Revision History:
  12. --*/
  13. //#ifndef TOKEN_DEBUG
  14. //#define TOKEN_DEBUG
  15. //#endif
  16. #include "pch.h"
  17. #pragma hdrstop
  18. #ifdef ALLOC_PRAGMA
  19. #pragma alloc_text(PAGE,NtDuplicateToken)
  20. #pragma alloc_text(PAGE,SepDuplicateToken)
  21. #pragma alloc_text(PAGE,SepMakeTokenEffectiveOnly)
  22. #pragma alloc_text(PAGE,SepSidInSidAndAttributes)
  23. #pragma alloc_text(PAGE,SepRemoveDisabledGroupsAndPrivileges)
  24. #pragma alloc_text(PAGE,SeCopyClientToken)
  25. #pragma alloc_text(PAGE,NtFilterToken)
  26. #pragma alloc_text(PAGE,SeFilterToken)
  27. #pragma alloc_text(PAGE,SeFastFilterToken)
  28. #pragma alloc_text(PAGE,SepFilterToken)
  29. #endif
  30. NTSTATUS
  31. NtDuplicateToken(
  32. IN HANDLE ExistingTokenHandle,
  33. IN ACCESS_MASK DesiredAccess,
  34. IN POBJECT_ATTRIBUTES ObjectAttributes,
  35. IN BOOLEAN EffectiveOnly,
  36. IN TOKEN_TYPE TokenType,
  37. OUT PHANDLE NewTokenHandle
  38. )
  39. /*++
  40. Routine Description:
  41. Create a new token that is a duplicate of an existing token.
  42. Arguments:
  43. ExistingTokenHandle - Is a handle to a token already open for
  44. TOKEN_DUPLICATE access.
  45. DesiredAccess - Is an access mask indicating which access types
  46. are desired to the newly created token. If specified as zero,
  47. the granted access mask of the existing token handle
  48. is used as the desired access mask for the new token.
  49. ObjectAttributes - Points to the standard object attributes data
  50. structure. Refer to the NT Object Management
  51. Specification for a description of this data structure.
  52. If the new token type is TokenImpersonation, then this
  53. parameter may be used to specify the impersonation level
  54. of the new token. If no value is provided, and the source
  55. token is an impersonation token, then the impersonation level
  56. of the source will become that of the target as well. If the
  57. source token is a primary token, then an impersonation level
  58. must be explicitly provided.
  59. If the token being duplicated is an impersonation token, and
  60. an impersonation level is explicitly provided for the target,
  61. then the value provided must not be greater than that of the
  62. source token. For example, an Identification level token can
  63. not be duplicated to produce a Delegation level token.
  64. EffectiveOnly - Is a boolean flag indicating whether the entire
  65. source token should be duplicated into the target token or
  66. just the effective (currently enabled) part of the token.
  67. This provides a means for a caller of a protected subsystem
  68. to limit which privileges and optional groups are made
  69. available to the protected subsystem. A value of TRUE
  70. indicates only the currently enabled parts of the source
  71. token are to be duplicated. Otherwise, the entire source
  72. token is duplicated.
  73. TokenType - Indicates which type of object the new object is to
  74. be created as (primary or impersonation). If you are duplicating
  75. an Impersonation token to produce a Primary token, then
  76. the Impersonation token must have an impersonation level of
  77. either DELEGATE or IMPERSONATE.
  78. NewTokenHandle - Receives the handle of the newly created token.
  79. Return Value:
  80. STATUS_SUCCESS - Indicates the operation was successful.
  81. STATUS_INVALID_PARAMETER - Indicates one or more of the parameter values
  82. was invalid. This value is returned if the target token is not
  83. an impersonation token.
  84. STATUS_BAD_IMPERSONATION_LEVEL - Indicates the impersonation level
  85. requested for the duplicate token is not compatible with the
  86. level of the source token. The duplicate token may not be assigned
  87. a level greater than that of the source token.
  88. --*/
  89. {
  90. PTOKEN Token;
  91. PTOKEN NewToken;
  92. KPROCESSOR_MODE PreviousMode;
  93. NTSTATUS Status;
  94. SECURITY_ADVANCED_QUALITY_OF_SERVICE SecurityQos;
  95. BOOLEAN SecurityQosPresent = FALSE;
  96. HANDLE LocalHandle = NULL;
  97. OBJECT_HANDLE_INFORMATION HandleInformation;
  98. ACCESS_MASK EffectiveDesiredAccess;
  99. PAGED_CODE();
  100. PreviousMode = KeGetPreviousMode();
  101. //
  102. // Probe parameters
  103. //
  104. if (PreviousMode != KernelMode) {
  105. try {
  106. //
  107. // Make sure the TokenType is valid
  108. //
  109. if ( (TokenType < TokenPrimary) || (TokenType > TokenImpersonation) ) {
  110. return(STATUS_INVALID_PARAMETER);
  111. }
  112. //
  113. // Make sure we can write the handle
  114. //
  115. ProbeForWriteHandle(NewTokenHandle);
  116. } except(EXCEPTION_EXECUTE_HANDLER) {
  117. return GetExceptionCode();
  118. } // end_try
  119. } //end_if
  120. Status = SeCaptureSecurityQos(
  121. ObjectAttributes,
  122. PreviousMode,
  123. &SecurityQosPresent,
  124. &SecurityQos
  125. );
  126. if (!NT_SUCCESS(Status)) {
  127. return Status;
  128. }
  129. //
  130. // Check the handle's access to the existing token and get
  131. // a pointer to that token. Pick up the default desired
  132. // access mask from the handle while we're at it.
  133. //
  134. Status = ObReferenceObjectByHandle(
  135. ExistingTokenHandle, // Handle
  136. TOKEN_DUPLICATE, // DesiredAccess
  137. SeTokenObjectType, // ObjectType
  138. PreviousMode, // AccessMode
  139. (PVOID *)&Token, // Object
  140. &HandleInformation // GrantedAccess
  141. );
  142. if ( !NT_SUCCESS(Status) ) {
  143. if (SecurityQosPresent) {
  144. SeFreeCapturedSecurityQos( &SecurityQos );
  145. }
  146. return Status;
  147. }
  148. #ifdef TOKEN_DEBUG
  149. ////////////////////////////////////////////////////////////////////////////
  150. //
  151. // Debug
  152. SepAcquireTokenReadLock( Token );
  153. DbgPrint("\n");
  154. DbgPrint("\n");
  155. DbgPrint("Token being duplicated: \n");
  156. SepDumpToken( Token );
  157. SepReleaseTokenReadLock( Token );
  158. // Debug
  159. //
  160. ////////////////////////////////////////////////////////////////////////////
  161. #endif //TOKEN_DEBUG
  162. //
  163. // Check to see if an alternate desired access mask was provided.
  164. //
  165. if (ARGUMENT_PRESENT((PVOID)(ULONG_PTR)DesiredAccess)) {
  166. EffectiveDesiredAccess = DesiredAccess;
  167. } else {
  168. EffectiveDesiredAccess = HandleInformation.GrantedAccess;
  169. }
  170. //
  171. // If no impersonation level was specified, pick one up from
  172. // the source token.
  173. //
  174. if ( !SecurityQosPresent ) {
  175. SecurityQos.ImpersonationLevel = Token->ImpersonationLevel;
  176. }
  177. if (Token->TokenType == TokenImpersonation) {
  178. //
  179. // Make sure a legitimate transformation is being requested:
  180. //
  181. // (1) The impersonation level of a target duplicate must not
  182. // exceed that of the source token.
  183. //
  184. //
  185. ASSERT( SecurityDelegation > SecurityImpersonation );
  186. ASSERT( SecurityImpersonation > SecurityIdentification );
  187. ASSERT( SecurityIdentification > SecurityAnonymous );
  188. if ( (SecurityQos.ImpersonationLevel > Token->ImpersonationLevel) ) {
  189. ObDereferenceObject( (PVOID)Token );
  190. if (SecurityQosPresent) {
  191. SeFreeCapturedSecurityQos( &SecurityQos );
  192. }
  193. return STATUS_BAD_IMPERSONATION_LEVEL;
  194. }
  195. }
  196. //
  197. // If we are producing a Primary token from an impersonation
  198. // token, then specify an impersonation level of at least
  199. // Impersonate.
  200. //
  201. if ( (Token->TokenType == TokenImpersonation) &&
  202. (TokenType == TokenPrimary) &&
  203. (Token->ImpersonationLevel < SecurityImpersonation)
  204. ) {
  205. ObDereferenceObject( (PVOID)Token );
  206. if (SecurityQosPresent) {
  207. SeFreeCapturedSecurityQos( &SecurityQos );
  208. }
  209. return STATUS_BAD_IMPERSONATION_LEVEL;
  210. }
  211. //
  212. // Duplicate the existing token
  213. //
  214. NewToken = NULL;
  215. Status = SepDuplicateToken(
  216. Token,
  217. ObjectAttributes,
  218. EffectiveOnly,
  219. TokenType,
  220. SecurityQos.ImpersonationLevel,
  221. PreviousMode,
  222. &NewToken
  223. );
  224. if (NT_SUCCESS(Status)) {
  225. //
  226. // Insert the new token
  227. //
  228. Status = ObInsertObject( NewToken,
  229. NULL,
  230. EffectiveDesiredAccess,
  231. 0,
  232. (PVOID *)NULL,
  233. &LocalHandle
  234. );
  235. if (!NT_SUCCESS( Status )) {
  236. #ifdef TOKEN_DEBUG
  237. DbgPrint( "SE: ObInsertObject failed (%x) for token at %x\n", Status, NewToken );
  238. #endif
  239. }
  240. } else
  241. if (NewToken != NULL) {
  242. #ifdef TOKEN_DEBUG
  243. DbgPrint( "SE: SepDuplicateToken failed (%x) but allocated token at %x\n", Status, NewToken );
  244. #endif
  245. }
  246. //
  247. // We no longer need our reference to the source token
  248. //
  249. ObDereferenceObject( (PVOID)Token );
  250. if (SecurityQosPresent) {
  251. SeFreeCapturedSecurityQos( &SecurityQos );
  252. }
  253. //
  254. // Return the new handle
  255. //
  256. if (NT_SUCCESS(Status)) {
  257. try {
  258. *NewTokenHandle = LocalHandle;
  259. } except(EXCEPTION_EXECUTE_HANDLER) {
  260. return GetExceptionCode();
  261. }
  262. }
  263. return Status;
  264. }
  265. NTSTATUS
  266. SepDuplicateToken(
  267. IN PTOKEN ExistingToken,
  268. IN POBJECT_ATTRIBUTES ObjectAttributes,
  269. IN BOOLEAN EffectiveOnly,
  270. IN TOKEN_TYPE TokenType,
  271. IN SECURITY_IMPERSONATION_LEVEL ImpersonationLevel OPTIONAL,
  272. IN KPROCESSOR_MODE RequestorMode,
  273. OUT PTOKEN *DuplicateToken
  274. )
  275. /*++
  276. Routine Description:
  277. This routine does the bulk of the work to actually duplicate
  278. a token. This routine assumes all access validation and argument
  279. probing (except the ObjectAttributes) has been performed.
  280. THE CALLER IS RESPONSIBLE FOR CHECKING SUBJECT RIGHTS TO CREATE THE
  281. TYPE OF TOKEN BEING CREATED.
  282. This routine acquires a read lock on the token being duplicated.
  283. Arguments:
  284. ExistingToken - Points to the token to be duplicated.
  285. ObjectAttributes - Points to the standard object attributes data
  286. structure. Refer to the NT Object Management
  287. Specification for a description of this data structure.
  288. The security Quality Of Service of the object attributes are ignored.
  289. This information must be specified using parameters to this
  290. routine.
  291. EffectiveOnly - Is a boolean flag indicating whether the entire
  292. source token should be duplicated into the target token or
  293. just the effective (currently enabled) part of the token.
  294. This provides a means for a caller of a protected subsystem
  295. to limit which privileges and optional groups are made
  296. available to the protected subsystem. A value of TRUE
  297. indicates only the currently enabled parts of the source
  298. token are to be duplicated. Otherwise, the entire source
  299. token is duplicated.
  300. TokenType - Indicates the type of token to make the duplicate token.
  301. ImpersonationLevel - This value specifies the impersonation level
  302. to assign to the duplicate token. If the TokenType of the
  303. duplicate is not TokenImpersonation then this parameter is
  304. ignored. Otherwise, it is must be provided.
  305. RequestorMode - Mode of client requesting the token be duplicated.
  306. DuplicateToken - Receives a pointer to the duplicate token.
  307. The token has not yet been inserted into any object table.
  308. No exceptions are expected when tring to set this OUT value.
  309. Return Value:
  310. STATUS_SUCCESS - The service successfully completed the requested
  311. operation.
  312. --*/
  313. {
  314. NTSTATUS Status;
  315. PTOKEN NewToken;
  316. PULONG DynamicPart;
  317. ULONG PagedPoolSize;
  318. ULONG NonPagedPoolSize;
  319. ULONG TokenBodyLength;
  320. ULONG FieldOffset;
  321. ULONG DynamicSize;
  322. ULONG Index;
  323. PSECURITY_TOKEN_PROXY_DATA NewProxyData = NULL;
  324. PSECURITY_TOKEN_AUDIT_DATA NewAuditData = NULL;
  325. PERESOURCE TokenLock;
  326. #if DBG || TOKEN_LEAK_MONITOR
  327. ULONG Frames;
  328. #endif
  329. PAGED_CODE();
  330. ASSERT( sizeof(SECURITY_IMPERSONATION_LEVEL) <= sizeof(ULONG) );
  331. if ( TokenType == TokenImpersonation ) {
  332. ASSERT( SecurityDelegation > SecurityImpersonation );
  333. ASSERT( SecurityImpersonation > SecurityIdentification );
  334. ASSERT( SecurityIdentification > SecurityAnonymous );
  335. if ( (ImpersonationLevel > SecurityDelegation) ||
  336. (ImpersonationLevel < SecurityAnonymous) ) {
  337. return STATUS_BAD_IMPERSONATION_LEVEL;
  338. }
  339. }
  340. //
  341. // Increment the reference count for this logon session
  342. // This can not fail, since there is already a token in this logon
  343. // session.
  344. //
  345. Status = SepReferenceLogonSession( &(ExistingToken->AuthenticationId) );
  346. ASSERT( NT_SUCCESS(Status) );
  347. if (ARGUMENT_PRESENT(ExistingToken->ProxyData)) {
  348. Status = SepCopyProxyData(
  349. &NewProxyData,
  350. ExistingToken->ProxyData
  351. );
  352. if (!NT_SUCCESS(Status)) {
  353. SepDeReferenceLogonSession( &(ExistingToken->AuthenticationId) );
  354. return( Status );
  355. }
  356. } else {
  357. NewProxyData = NULL;
  358. }
  359. if (ARGUMENT_PRESENT( ExistingToken->AuditData )) {
  360. NewAuditData = ExAllocatePool( PagedPool, sizeof( SECURITY_TOKEN_AUDIT_DATA ));
  361. if (NewAuditData == NULL) {
  362. SepFreeProxyData( NewProxyData );
  363. SepDeReferenceLogonSession( &(ExistingToken->AuthenticationId) );
  364. return( STATUS_INSUFFICIENT_RESOURCES );
  365. } else {
  366. *NewAuditData = *(ExistingToken->AuditData);
  367. }
  368. } else {
  369. NewAuditData = NULL;
  370. }
  371. TokenLock = (PERESOURCE)ExAllocatePoolWithTag( NonPagedPool, sizeof( ERESOURCE ), 'dTeS' );
  372. if (TokenLock == NULL) {
  373. if (NewAuditData != NULL) {
  374. ExFreePool( NewAuditData );
  375. }
  376. SepFreeProxyData( NewProxyData );
  377. SepDeReferenceLogonSession( &(ExistingToken->AuthenticationId) );
  378. return( STATUS_INSUFFICIENT_RESOURCES );
  379. }
  380. //
  381. // Create a new object
  382. //
  383. TokenBodyLength = FIELD_OFFSET(TOKEN, VariablePart) +
  384. ExistingToken->VariableLength;
  385. NonPagedPoolSize = TokenBodyLength;
  386. PagedPoolSize = ExistingToken->DynamicCharged;
  387. Status = ObCreateObject(
  388. RequestorMode, // ProbeMode
  389. SeTokenObjectType, // ObjectType
  390. ObjectAttributes, // ObjectAttributes
  391. RequestorMode, // OwnershipMode
  392. NULL, // ParseContext
  393. TokenBodyLength, // ObjectBodySize
  394. PagedPoolSize, // PagedPoolCharge
  395. NonPagedPoolSize, // NonPagedPoolCharge
  396. (PVOID *)&NewToken // Return pointer to object
  397. );
  398. if (!NT_SUCCESS(Status)) {
  399. SepDeReferenceLogonSession( &(ExistingToken->AuthenticationId) );
  400. SepFreeProxyData( NewProxyData );
  401. ExFreePool( TokenLock );
  402. if (NewAuditData != NULL) {
  403. ExFreePool( NewAuditData );
  404. }
  405. return Status;
  406. }
  407. //
  408. // The following fields differ in the new token and can be filled out without the lock.
  409. //
  410. ExAllocateLocallyUniqueId( &(NewToken->TokenId) );
  411. NewToken->TokenInUse = FALSE;
  412. NewToken->TokenType = TokenType;
  413. NewToken->ImpersonationLevel = ImpersonationLevel;
  414. NewToken->TokenLock = TokenLock;
  415. ExInitializeResourceLite( NewToken->TokenLock );
  416. //
  417. // acquire exclusive access to the source token
  418. //
  419. SepAcquireTokenReadLock( ExistingToken );
  420. //
  421. // Main Body initialization
  422. //
  423. //
  424. // The following fields are unchanged from the source token.
  425. // Although some may change if EffectiveOnly has been specified.
  426. //
  427. NewToken->AuthenticationId = ExistingToken->AuthenticationId;
  428. NewToken->ModifiedId = ExistingToken->ModifiedId;
  429. NewToken->ExpirationTime = ExistingToken->ExpirationTime;
  430. NewToken->TokenSource = ExistingToken->TokenSource;
  431. NewToken->DynamicCharged = ExistingToken->DynamicCharged;
  432. NewToken->DynamicAvailable = 0;
  433. NewToken->DefaultOwnerIndex = ExistingToken->DefaultOwnerIndex;
  434. NewToken->UserAndGroupCount = ExistingToken->UserAndGroupCount;
  435. NewToken->RestrictedSidCount = ExistingToken->RestrictedSidCount;
  436. NewToken->PrivilegeCount = ExistingToken->PrivilegeCount;
  437. NewToken->VariableLength = ExistingToken->VariableLength;
  438. NewToken->TokenFlags = ExistingToken->TokenFlags & ~TOKEN_SESSION_NOT_REFERENCED;
  439. NewToken->ProxyData = NewProxyData;
  440. NewToken->AuditData = NewAuditData;
  441. NewToken->SessionId = ExistingToken->SessionId;
  442. NewToken->ParentTokenId = ExistingToken->ParentTokenId;
  443. #if DBG || TOKEN_LEAK_MONITOR
  444. NewToken->ProcessCid = PsGetCurrentThread()->Cid.UniqueProcess;
  445. NewToken->ThreadCid = PsGetCurrentThread()->Cid.UniqueThread;
  446. NewToken->CreateMethod = 0xD; // Duplicate
  447. RtlCopyMemory(
  448. NewToken->ImageFileName,
  449. PsGetCurrentProcess()->ImageFileName,
  450. min(sizeof(NewToken->ImageFileName), sizeof(PsGetCurrentProcess()->ImageFileName))
  451. );
  452. Frames = RtlWalkFrameChain(
  453. (PVOID)NewToken->CreateTrace,
  454. TRACE_SIZE,
  455. 0
  456. );
  457. if (KeGetCurrentIrql() < DISPATCH_LEVEL) {
  458. RtlWalkFrameChain(
  459. (PVOID)&NewToken->CreateTrace[Frames],
  460. TRACE_SIZE - Frames,
  461. 1
  462. );
  463. }
  464. SepAddTokenLogonSession(NewToken);
  465. #endif
  466. //
  467. // Copy and initialize the variable part.
  468. // The variable part is assumed to be position independent.
  469. //
  470. RtlCopyMemory( (PVOID)&(NewToken->VariablePart),
  471. (PVOID)&(ExistingToken->VariablePart),
  472. ExistingToken->VariableLength
  473. );
  474. //
  475. // Set the address of the UserAndGroups array.
  476. //
  477. ASSERT( ARGUMENT_PRESENT(ExistingToken->UserAndGroups ) );
  478. ASSERT( (ULONG_PTR)(ExistingToken->UserAndGroups) >=
  479. (ULONG_PTR)(&(ExistingToken->VariablePart)) );
  480. FieldOffset = (ULONG)((ULONG_PTR)(ExistingToken->UserAndGroups) -
  481. (ULONG_PTR)(&(ExistingToken->VariablePart)));
  482. NewToken->UserAndGroups =
  483. (PSID_AND_ATTRIBUTES)(FieldOffset + (ULONG_PTR)(&(NewToken->VariablePart)));
  484. //
  485. // Now go through and change the address of each SID pointer
  486. // for the user and groups
  487. //
  488. Index = 0;
  489. while (Index < ExistingToken->UserAndGroupCount) {
  490. FieldOffset = (ULONG)((ULONG_PTR)(ExistingToken->UserAndGroups[Index].Sid) -
  491. (ULONG_PTR)(&(ExistingToken->VariablePart)));
  492. NewToken->UserAndGroups[Index].Sid =
  493. (PSID)( FieldOffset + (ULONG_PTR)(&(NewToken->VariablePart)) );
  494. Index += 1;
  495. }
  496. //
  497. // Set the address of the RestrictedSids array.
  498. //
  499. if (ARGUMENT_PRESENT(ExistingToken->RestrictedSids ) ) {
  500. ASSERT( (ULONG_PTR)(ExistingToken->RestrictedSids) >=
  501. (ULONG_PTR)(&(ExistingToken->VariablePart)) );
  502. FieldOffset = (ULONG)((ULONG_PTR)(ExistingToken->RestrictedSids) -
  503. (ULONG_PTR)(&(ExistingToken->VariablePart)));
  504. NewToken->RestrictedSids =
  505. (PSID_AND_ATTRIBUTES)(FieldOffset + (ULONG_PTR)(&(NewToken->VariablePart)) );
  506. //
  507. // Now go through and change the address of each SID pointer
  508. // for the user and groups
  509. //
  510. Index = 0;
  511. while (Index < ExistingToken->RestrictedSidCount) {
  512. FieldOffset = (ULONG)((ULONG_PTR)(ExistingToken->RestrictedSids[Index].Sid) -
  513. (ULONG_PTR)(&(ExistingToken->VariablePart)));
  514. NewToken->RestrictedSids[Index].Sid =
  515. (PSID)( FieldOffset + (ULONG_PTR)(&(NewToken->VariablePart)) );
  516. Index += 1;
  517. }
  518. } else {
  519. NewToken->RestrictedSids = NULL;
  520. }
  521. //
  522. // If present, set the address of the privileges
  523. //
  524. if (ExistingToken->PrivilegeCount > 0) {
  525. ASSERT( ARGUMENT_PRESENT(ExistingToken->Privileges ) );
  526. ASSERT( (ULONG_PTR)(ExistingToken->Privileges) >=
  527. (ULONG_PTR)(&(ExistingToken->VariablePart)) );
  528. FieldOffset = (ULONG)((ULONG_PTR)(ExistingToken->Privileges) -
  529. (ULONG_PTR)(&(ExistingToken->VariablePart)));
  530. NewToken->Privileges = (PLUID_AND_ATTRIBUTES)(
  531. FieldOffset +
  532. (ULONG_PTR)(&(NewToken->VariablePart))
  533. );
  534. } else {
  535. NewToken->Privileges = NULL;
  536. }
  537. //
  538. // Allocate the dynamic portion
  539. //
  540. DynamicSize = SeLengthSid( ExistingToken->PrimaryGroup );
  541. if (ExistingToken->DefaultDacl) {
  542. DynamicSize += ExistingToken->DefaultDacl->AclSize;
  543. }
  544. DynamicPart = (PULONG)ExAllocatePoolWithTag(
  545. PagedPool,
  546. DynamicSize,
  547. 'dTeS'
  548. );
  549. NewToken->DynamicPart = DynamicPart;
  550. if (DynamicPart == NULL) {
  551. SepReleaseTokenReadLock( ExistingToken );
  552. ObDereferenceObject (NewToken);
  553. return( STATUS_INSUFFICIENT_RESOURCES );
  554. }
  555. //
  556. // Copy and initialize the dynamic part.
  557. // The dynamic part is assumed to be position independent.
  558. //
  559. RtlCopyMemory( (PVOID)DynamicPart,
  560. (PVOID)(ExistingToken->DynamicPart),
  561. DynamicSize
  562. );
  563. //
  564. // If present, set the address of the default Dacl
  565. //
  566. if (ARGUMENT_PRESENT(ExistingToken->DefaultDacl)) {
  567. ASSERT( (ULONG_PTR)(ExistingToken->DefaultDacl) >=
  568. (ULONG_PTR)(ExistingToken->DynamicPart) );
  569. FieldOffset = (ULONG)((ULONG_PTR)(ExistingToken->DefaultDacl) -
  570. (ULONG_PTR)(ExistingToken->DynamicPart));
  571. NewToken->DefaultDacl = (PACL)(FieldOffset + (ULONG_PTR)DynamicPart);
  572. } else {
  573. NewToken->DefaultDacl = NULL;
  574. }
  575. //
  576. // Set the address of the primary group
  577. //
  578. ASSERT(ARGUMENT_PRESENT(ExistingToken->PrimaryGroup));
  579. ASSERT( (ULONG_PTR)(ExistingToken->PrimaryGroup) >=
  580. (ULONG_PTR)(ExistingToken->DynamicPart) );
  581. FieldOffset = (ULONG)((ULONG_PTR)(ExistingToken->PrimaryGroup) -
  582. (ULONG_PTR)(ExistingToken->DynamicPart));
  583. //
  584. // Release the source token.
  585. //
  586. SepReleaseTokenReadLock( ExistingToken );
  587. NewToken->PrimaryGroup = (PACL)(FieldOffset + (ULONG_PTR)(DynamicPart));
  588. //
  589. // For the time being, take the easy way to generating an "EffectiveOnly"
  590. // duplicate. That is, use the same space required of the original, just
  591. // eliminate any IDs or privileges not active.
  592. //
  593. // Ultimately, if duplication becomes a common operation, then it will be
  594. // worthwhile to recalculate the actual space needed and copy only the
  595. // effective IDs/privileges into the new token.
  596. //
  597. if (EffectiveOnly) {
  598. SepMakeTokenEffectiveOnly( NewToken );
  599. }
  600. #ifdef TOKEN_DEBUG
  601. ////////////////////////////////////////////////////////////////////////////
  602. //
  603. // Debug
  604. DbgPrint("\n");
  605. DbgPrint("\n");
  606. DbgPrint("\n");
  607. DbgPrint("Duplicate token:\n");
  608. SepDumpToken( NewToken );
  609. // Debug
  610. //
  611. ////////////////////////////////////////////////////////////////////////////
  612. #endif //TOKEN_DEBUG
  613. (*DuplicateToken) = NewToken;
  614. return Status;
  615. }
  616. VOID
  617. SepMakeTokenEffectiveOnly(
  618. IN PTOKEN Token
  619. )
  620. /*++
  621. Routine Description:
  622. This routine eliminates all but the effective groups and privileges from
  623. a token. It does this by moving elements of the SID and privileges arrays
  624. to overwrite lapsed IDs/privileges, and then reducing the array element
  625. counts. This results in wasted memory within the token object.
  626. One side effect of this routine is that a token that initially had a
  627. default owner ID corresponding to a lapsed group will be changed so
  628. that the default owner ID is the user ID.
  629. THIS ROUTINE MUST BE CALLED ONLY AS PART OF TOKEN CREATION (FOR TOKENS
  630. WHICH HAVE NOT YET BEEN INSERTED INTO AN OBJECT TABLE.) THIS ROUTINE
  631. MODIFIES READ ONLY TOKEN FIELDS.
  632. Note that since we are operating on a token that is not yet visible
  633. to the user, we do not bother acquiring a read lock on the token
  634. being modified.
  635. Arguments:
  636. Token - Points to the token to be made effective only.
  637. Return Value:
  638. None.
  639. --*/
  640. {
  641. ULONG Index;
  642. ULONG ElementCount;
  643. PAGED_CODE();
  644. //
  645. // Walk the privilege array, discarding any lapsed privileges
  646. //
  647. ElementCount = Token->PrivilegeCount;
  648. Index = 0;
  649. while (Index < ElementCount) {
  650. //
  651. // If this privilege is not enabled, replace it with the one at
  652. // the end of the array and reduce the size of the array by one.
  653. // Otherwise, move on to the next entry in the array.
  654. //
  655. if ( !(SepTokenPrivilegeAttributes(Token,Index) & SE_PRIVILEGE_ENABLED)
  656. ) {
  657. (Token->Privileges)[Index] =
  658. (Token->Privileges)[ElementCount - 1];
  659. ElementCount -= 1;
  660. } else {
  661. Index += 1;
  662. }
  663. } // endwhile
  664. Token->PrivilegeCount = ElementCount;
  665. //
  666. // Walk the UserAndGroups array (except for the first entry, which is
  667. // the user - and can't be disabled) discarding any lapsed groups.
  668. //
  669. ElementCount = Token->UserAndGroupCount;
  670. ASSERT( ElementCount >= 1 ); // Must be at least a user ID
  671. Index = 1; // Start at the first group, not the user ID.
  672. while (Index < ElementCount) {
  673. //
  674. // If this group is not enabled, replace it with the one at
  675. // the end of the array and reduce the size of the array by one.
  676. //
  677. if ( !(SepTokenGroupAttributes(Token, Index) & SE_GROUP_ENABLED) &&
  678. !(SepTokenGroupAttributes(Token, Index) & SE_GROUP_USE_FOR_DENY_ONLY) ) {
  679. //
  680. // Reset the TOKEN_HAS_ADMIN_GROUP flag
  681. //
  682. if (RtlEqualSid(
  683. Token->UserAndGroups[Index].Sid,
  684. SeAliasAdminsSid
  685. )) {
  686. Token->TokenFlags &= ~TOKEN_HAS_ADMIN_GROUP;
  687. }
  688. (Token->UserAndGroups)[Index] =
  689. (Token->UserAndGroups)[ElementCount - 1];
  690. ElementCount -= 1;
  691. } else {
  692. Index += 1;
  693. }
  694. } // endwhile
  695. Token->UserAndGroupCount = ElementCount;
  696. return;
  697. }
  698. BOOLEAN
  699. SepSidInSidAndAttributes (
  700. IN PSID_AND_ATTRIBUTES SidAndAttributes,
  701. IN ULONG SidCount,
  702. IN PSID PrincipalSelfSid,
  703. IN PSID Sid
  704. )
  705. /*++
  706. Routine Description:
  707. Checks to see if a given SID is in the given token.
  708. N.B. The code to compute the length of a SID and test for equality
  709. is duplicated from the security runtime since this is such a
  710. frequently used routine.
  711. Arguments:
  712. SidAndAttributes - Pointer to the sid and attributes to be examined
  713. PrincipalSelfSid - If the object being access checked is an object which
  714. represents a principal (e.g., a user object), this parameter should
  715. be the SID of the object. Any ACE containing the constant
  716. PRINCIPAL_SELF_SID is replaced by this SID.
  717. The parameter should be NULL if the object does not represent a principal.
  718. Sid - Pointer to the SID of interest
  719. Return Value:
  720. A value of TRUE indicates that the SID is in the token, FALSE
  721. otherwise.
  722. --*/
  723. {
  724. ULONG i;
  725. PISID MatchSid;
  726. ULONG SidLength;
  727. PTOKEN Token;
  728. PSID_AND_ATTRIBUTES TokenSid;
  729. ULONG UserAndGroupCount;
  730. PAGED_CODE();
  731. if (!ARGUMENT_PRESENT( SidAndAttributes ) ) {
  732. return(FALSE);
  733. }
  734. //
  735. // If Sid is the constant PrincipalSelfSid,
  736. // replace it with the passed in PrincipalSelfSid.
  737. //
  738. if ( PrincipalSelfSid != NULL &&
  739. RtlEqualSid( SePrincipalSelfSid, Sid ) ) {
  740. Sid = PrincipalSelfSid;
  741. }
  742. //
  743. // Get the length of the source SID since this only needs to be computed
  744. // once.
  745. //
  746. SidLength = 8 + (4 * ((PISID)Sid)->SubAuthorityCount);
  747. //
  748. // Get address of user/group array and number of user/groups.
  749. //
  750. TokenSid = SidAndAttributes;
  751. UserAndGroupCount = SidCount;
  752. //
  753. // Scan through the user/groups and attempt to find a match with the
  754. // specified SID.
  755. //
  756. for (i = 0 ; i < UserAndGroupCount ; i += 1) {
  757. MatchSid = (PISID)TokenSid->Sid;
  758. //
  759. // If the SID revision and length matches, then compare the SIDs
  760. // for equality.
  761. //
  762. if ((((PISID)Sid)->Revision == MatchSid->Revision) &&
  763. (SidLength == (8 + (4 * (ULONG)MatchSid->SubAuthorityCount)))) {
  764. if (RtlEqualMemory(Sid, MatchSid, SidLength)) {
  765. return TRUE;
  766. }
  767. }
  768. TokenSid += 1;
  769. }
  770. return FALSE;
  771. }
  772. VOID
  773. SepRemoveDisabledGroupsAndPrivileges(
  774. IN PTOKEN Token,
  775. IN ULONG Flags,
  776. IN ULONG GroupCount,
  777. IN PSID_AND_ATTRIBUTES GroupsToDisable,
  778. IN ULONG PrivilegeCount,
  779. IN PLUID_AND_ATTRIBUTES PrivilegesToDelete
  780. )
  781. /*++
  782. Routine Description:
  783. This routine eliminates all groups and privileges that are marked
  784. to be deleted/disabled. It does this by looping through the groups in
  785. the token and checking each one agains the groups to disable. Similary
  786. the privilegs are compared. It does this by moving elements of the SID and privileges arrays
  787. to overwrite lapsed IDs/privileges, and then reducing the array element
  788. counts. This results in wasted memory within the token object.
  789. THIS ROUTINE MUST BE CALLED ONLY AS PART OF TOKEN CREATION (FOR TOKENS
  790. WHICH HAVE NOT YET BEEN INSERTED INTO AN OBJECT TABLE.) THIS ROUTINE
  791. MODIFIES READ ONLY TOKEN FIELDS.
  792. Note that since we are operating on a token that is not yet visible
  793. to the user, we do not bother acquiring a read lock on the token
  794. being modified.
  795. Arguments:
  796. Token - Points to the token to be made effective only.
  797. Flags - Flags indicating additional filtering. The flags may be:
  798. DISABLE_MAX_PRIVILEGE - Disable all privileges
  799. GroupCount - Count of groups to be removed
  800. GroupsToDisable - Groups to disable and mark with SE_GROUP_USE_FOR_DENY_ONLY
  801. PrivilegeCount - Count of privileges to remove
  802. PrivilegesToDelete - List of privileges to remove
  803. Return Value:
  804. None.
  805. --*/
  806. {
  807. ULONG Index;
  808. ULONG Index2;
  809. ULONG ElementCount;
  810. BOOLEAN Found;
  811. PAGED_CODE();
  812. //
  813. // Walk the privilege array, discarding any lapsed privileges
  814. //
  815. ElementCount = Token->PrivilegeCount;
  816. Index = 0;
  817. while (Index < ElementCount) {
  818. //
  819. // If the caller asked us to disable all privileges except change
  820. // notify, do so now.
  821. //
  822. if (((Flags & DISABLE_MAX_PRIVILEGE) != 0) &&
  823. !RtlEqualLuid(
  824. &Token->Privileges[Index].Luid,
  825. &SeChangeNotifyPrivilege
  826. )) {
  827. (Token->Privileges)[Index] =
  828. (Token->Privileges)[ElementCount - 1];
  829. ElementCount -= 1;
  830. } else {
  831. //
  832. // If this privilege is in the list of those to be removed, replace it
  833. // with the one at the end of the array and reduce the size of the
  834. // array by one. Otherwise, move on to the next entry in the array.
  835. //
  836. Found = FALSE;
  837. for (Index2 = 0; Index2 < PrivilegeCount ; Index2++ ) {
  838. if (RtlEqualLuid(
  839. &Token->Privileges[Index].Luid,
  840. &PrivilegesToDelete[Index2].Luid
  841. )) {
  842. (Token->Privileges)[Index] =
  843. (Token->Privileges)[ElementCount - 1];
  844. ElementCount -= 1;
  845. //
  846. // If this was SeChangeNotifyPrivilege, we need to turn off
  847. // the TOKEN_HAS_TRAVERSE_PRIVILEGE in the token
  848. //
  849. if (RtlEqualLuid(
  850. &PrivilegesToDelete[Index2].Luid,
  851. &SeChangeNotifyPrivilege
  852. )) {
  853. Token->TokenFlags &= ~TOKEN_HAS_TRAVERSE_PRIVILEGE;
  854. }
  855. Found = TRUE;
  856. break;
  857. }
  858. }
  859. if (!Found) {
  860. Index += 1;
  861. }
  862. }
  863. } // endwhile
  864. Token->PrivilegeCount = ElementCount;
  865. //
  866. // Walk the UserAndGroups array marking any disabled groups.
  867. //
  868. ElementCount = Token->UserAndGroupCount;
  869. ASSERT( ElementCount >= 1 ); // Must be at least a user ID
  870. Index = 0; // Start at the first group, not the user ID.
  871. while (Index < ElementCount) {
  872. //
  873. // If this group is not enabled, replace it with the one at
  874. // the end of the array and reduce the size of the array by one.
  875. //
  876. if ( SepSidInSidAndAttributes(
  877. GroupsToDisable,
  878. GroupCount,
  879. NULL, // no principal self sid
  880. Token->UserAndGroups[Index].Sid
  881. )){
  882. (Token->UserAndGroups)[Index].Attributes &= ~(SE_GROUP_ENABLED | SE_GROUP_ENABLED_BY_DEFAULT);
  883. (Token->UserAndGroups)[Index].Attributes |= SE_GROUP_USE_FOR_DENY_ONLY;
  884. //
  885. // If this was the owner, reset the owner to be the user
  886. //
  887. if (Index == Token->DefaultOwnerIndex) {
  888. Token->DefaultOwnerIndex = 0;
  889. }
  890. //
  891. // If this is the admins sid, turn off the admin group flag
  892. //
  893. if (RtlEqualSid(
  894. Token->UserAndGroups[Index].Sid,
  895. SeAliasAdminsSid
  896. )) {
  897. Token->TokenFlags &= ~TOKEN_HAS_ADMIN_GROUP;
  898. }
  899. }
  900. Index += 1;
  901. } // endwhile
  902. return;
  903. }
  904. NTSTATUS
  905. SeCopyClientToken(
  906. IN PACCESS_TOKEN ClientToken,
  907. IN SECURITY_IMPERSONATION_LEVEL ImpersonationLevel,
  908. IN KPROCESSOR_MODE RequestorMode,
  909. OUT PACCESS_TOKEN *DuplicateToken
  910. )
  911. /*++
  912. Routine Description:
  913. This routine copies a client's token as part of establishing a client
  914. context for impersonation.
  915. The result will be an impersonation token.
  916. No handles to the new token are established.
  917. The token will be an exact duplicate of the source token. It is the
  918. caller's responsibility to ensure an effective only copy of the token
  919. is produced when the token is opened, if necessary.
  920. Arguments:
  921. ClientToken - Points to the token to be duplicated. This may be either
  922. a primary or impersonation token.
  923. ImpersonationLevel - The impersonation level to be assigned to the new
  924. token.
  925. RequestorMode - Mode to be assigned as the owner mode of the new token.
  926. DuplicateToken - Receives a pointer to the duplicate token.
  927. The token has not yet been inserted into any object table.
  928. No exceptions are expected when tring to set this OUT value.
  929. Return Value:
  930. STATUS_SUCCESS - The service successfully completed the requested
  931. operation.
  932. --*/
  933. {
  934. NTSTATUS Status;
  935. OBJECT_ATTRIBUTES ObjectAttributes;
  936. PTOKEN NewToken;
  937. PAGED_CODE();
  938. InitializeObjectAttributes(
  939. &ObjectAttributes,
  940. NULL,
  941. 0,
  942. NULL,
  943. NULL
  944. );
  945. Status = SepDuplicateToken(
  946. (PTOKEN)ClientToken, // ExistingToken
  947. &ObjectAttributes, // ObjectAttributes
  948. FALSE, // EffectiveOnly
  949. TokenImpersonation, // TokenType (target)
  950. ImpersonationLevel, // ImpersonationLevel
  951. RequestorMode, // RequestorMode
  952. &NewToken // DuplicateToken
  953. );
  954. if (NT_SUCCESS (Status)) {
  955. //
  956. // Insert the new token
  957. //
  958. Status = ObInsertObject( NewToken,
  959. NULL,
  960. 0,
  961. 0,
  962. NULL,
  963. NULL
  964. );
  965. }
  966. if (NT_SUCCESS (Status)) {
  967. *DuplicateToken = (PACCESS_TOKEN)NewToken;
  968. } else {
  969. *DuplicateToken = NULL;
  970. }
  971. return Status;
  972. }
  973. NTSTATUS
  974. NtFilterToken (
  975. IN HANDLE ExistingTokenHandle,
  976. IN ULONG Flags,
  977. IN PTOKEN_GROUPS SidsToDisable OPTIONAL,
  978. IN PTOKEN_PRIVILEGES PrivilegesToDelete OPTIONAL,
  979. IN PTOKEN_GROUPS RestrictedSids OPTIONAL,
  980. OUT PHANDLE NewTokenHandle
  981. )
  982. /*++
  983. Routine Description:
  984. Create a new token that is a subset of an existing token.
  985. Arguments:
  986. ExistingTokenHandle - Is a handle to a token already open for
  987. TOKEN_DUPLICATE access.
  988. Flags - Flags indicating additional filtering. The flags may be:
  989. DISABLE_MAX_PRIVILEGE - Disable all privileges
  990. SANDBOX_INERT - Record this SAIFER flag in the token.
  991. SidsToDisable - Contains a list of sids and attributes. All sids with
  992. the USE_FOR_DENY_ONLY attribute that also exist in the token will
  993. cause the new token to have that sid set with the USE_FOR_DENY_ONLY
  994. attribte.
  995. PrivilegesToDelete - Privileges in this list that are present in the
  996. existing token will not exist in the final token. This is similar
  997. to duplicating a token effective only with these privileges set to
  998. disabled.
  999. RestrictedSids - Contains a list of SIDs and attributes that will be
  1000. stored in the RestrictedSids field of the new token. These SIDs
  1001. are used after a normal access check to futher restrict access.
  1002. The attributes of these groups are always SE_GROUP_MANDATORY |
  1003. SE_GROUP_ENABLED | SE_GROUP_ENABLED_BY_DEFAULT. If there already
  1004. exist RestrictedSids in the original token, these sids will be
  1005. appended.
  1006. NewTokenHandle - Receives the handle of the newly created token.
  1007. Return Value:
  1008. STATUS_SUCCESS - Indicates the operation was successful.
  1009. STATUS_INVALID_PARAMETER - Indicates one or more of the parameter values
  1010. was invalid. This value is returned if the target token is not
  1011. an impersonation token.
  1012. --*/
  1013. {
  1014. PTOKEN Token;
  1015. PTOKEN NewToken;
  1016. KPROCESSOR_MODE PreviousMode;
  1017. NTSTATUS Status = STATUS_SUCCESS;
  1018. ULONG CapturedSidCount = 0;
  1019. PSID_AND_ATTRIBUTES CapturedSids = NULL;
  1020. ULONG CapturedSidsLength = 0;
  1021. ULONG CapturedGroupCount = 0;
  1022. PSID_AND_ATTRIBUTES CapturedGroups = NULL;
  1023. ULONG CapturedGroupsLength = 0;
  1024. ULONG CapturedPrivilegeCount = 0;
  1025. PLUID_AND_ATTRIBUTES CapturedPrivileges = NULL;
  1026. ULONG CapturedPrivilegesLength = 0;
  1027. ULONG Index;
  1028. HANDLE LocalHandle = NULL;
  1029. OBJECT_HANDLE_INFORMATION HandleInformation;
  1030. ACCESS_MASK EffectiveDesiredAccess;
  1031. PAGED_CODE();
  1032. PreviousMode = KeGetPreviousMode();
  1033. //
  1034. // Probe parameters
  1035. //
  1036. try {
  1037. //
  1038. // Make sure we can write the handle
  1039. //
  1040. ProbeForWriteHandle(NewTokenHandle);
  1041. //
  1042. // Capture Sids to remove
  1043. //
  1044. if (ARGUMENT_PRESENT(SidsToDisable)) {
  1045. ProbeForReadSmallStructure( SidsToDisable, sizeof(TOKEN_GROUPS), sizeof(ULONG) );
  1046. CapturedGroupCount = SidsToDisable->GroupCount;
  1047. Status = SeCaptureSidAndAttributesArray(
  1048. SidsToDisable->Groups,
  1049. CapturedGroupCount,
  1050. PreviousMode,
  1051. NULL, 0,
  1052. PagedPool,
  1053. TRUE,
  1054. &CapturedGroups,
  1055. &CapturedGroupsLength
  1056. );
  1057. }
  1058. //
  1059. // Capture PrivilegesToDelete
  1060. //
  1061. if (NT_SUCCESS(Status) && ARGUMENT_PRESENT(PrivilegesToDelete)) {
  1062. ProbeForReadSmallStructure( PrivilegesToDelete, sizeof(TOKEN_PRIVILEGES), sizeof(ULONG) );
  1063. CapturedPrivilegeCount = PrivilegesToDelete->PrivilegeCount;
  1064. Status = SeCaptureLuidAndAttributesArray(
  1065. PrivilegesToDelete->Privileges,
  1066. CapturedPrivilegeCount,
  1067. PreviousMode,
  1068. NULL, 0,
  1069. PagedPool,
  1070. TRUE,
  1071. &CapturedPrivileges,
  1072. &CapturedPrivilegesLength
  1073. );
  1074. }
  1075. //
  1076. // Capture Restricted Sids
  1077. //
  1078. if (NT_SUCCESS(Status) && ARGUMENT_PRESENT(RestrictedSids)) {
  1079. ProbeForReadSmallStructure( RestrictedSids, sizeof(TOKEN_GROUPS), sizeof(ULONG) );
  1080. CapturedSidCount = RestrictedSids->GroupCount;
  1081. Status = SeCaptureSidAndAttributesArray(
  1082. RestrictedSids->Groups,
  1083. CapturedSidCount,
  1084. PreviousMode,
  1085. NULL, 0,
  1086. PagedPool,
  1087. TRUE,
  1088. &CapturedSids,
  1089. &CapturedSidsLength
  1090. );
  1091. }
  1092. } except(EXCEPTION_EXECUTE_HANDLER) {
  1093. Status = GetExceptionCode();
  1094. } // end_try
  1095. if (!NT_SUCCESS(Status)) {
  1096. goto Cleanup;
  1097. }
  1098. //
  1099. // Check that the attribtes are all zero for the restricted sids
  1100. //
  1101. for (Index = 0; Index < CapturedSidCount ; Index++ )
  1102. {
  1103. if (CapturedSids[Index].Attributes != 0) {
  1104. Status = STATUS_INVALID_PARAMETER;
  1105. goto Cleanup;
  1106. }
  1107. }
  1108. //
  1109. // Check the handle's access to the existing token and get
  1110. // a pointer to that token. Pick up the default desired
  1111. // access mask from the handle while we're at it.
  1112. //
  1113. Status = ObReferenceObjectByHandle(
  1114. ExistingTokenHandle, // Handle
  1115. TOKEN_DUPLICATE, // DesiredAccess
  1116. SeTokenObjectType, // ObjectType
  1117. PreviousMode, // AccessMode
  1118. (PVOID *)&Token, // Object
  1119. &HandleInformation // GrantedAccess
  1120. );
  1121. if ( !NT_SUCCESS(Status) ) {
  1122. goto Cleanup;
  1123. }
  1124. #ifdef TOKEN_DEBUG
  1125. ////////////////////////////////////////////////////////////////////////////
  1126. //
  1127. // Debug
  1128. SepAcquireTokenReadLock( Token );
  1129. DbgPrint("\n");
  1130. DbgPrint("\n");
  1131. DbgPrint("Token being filtered: \n");
  1132. SepDumpToken( Token );
  1133. SepReleaseTokenReadLock( Token );
  1134. // Debug
  1135. //
  1136. ////////////////////////////////////////////////////////////////////////////
  1137. #endif //TOKEN_DEBUG
  1138. //
  1139. // Check to see if an alternate desired access mask was provided.
  1140. //
  1141. EffectiveDesiredAccess = HandleInformation.GrantedAccess;
  1142. //
  1143. // Filter the existing token
  1144. //
  1145. NewToken = NULL;
  1146. Status = SepFilterToken(
  1147. Token,
  1148. PreviousMode,
  1149. Flags,
  1150. CapturedGroupCount,
  1151. CapturedGroups,
  1152. CapturedPrivilegeCount,
  1153. CapturedPrivileges,
  1154. CapturedSidCount,
  1155. CapturedSids,
  1156. CapturedSidsLength,
  1157. &NewToken
  1158. );
  1159. if (NT_SUCCESS(Status)) {
  1160. //
  1161. // Insert the new token
  1162. //
  1163. Status = ObInsertObject( NewToken,
  1164. NULL,
  1165. EffectiveDesiredAccess,
  1166. 0,
  1167. (PVOID *)NULL,
  1168. &LocalHandle
  1169. );
  1170. if (!NT_SUCCESS( Status )) {
  1171. #ifdef TOKEN_DEBUG
  1172. DbgPrint( "SE: ObInsertObject failed (%x) for token at %x\n", Status, NewToken );
  1173. #endif
  1174. }
  1175. } else
  1176. if (NewToken != NULL) {
  1177. #ifdef TOKEN_DEBUG
  1178. DbgPrint( "SE: SepFilterToken failed (%x) but allocated token at %x\n", Status, NewToken );
  1179. #endif
  1180. }
  1181. //
  1182. // We no longer need our reference to the source token
  1183. //
  1184. ObDereferenceObject( (PVOID)Token );
  1185. //
  1186. // Return the new handle
  1187. //
  1188. if (NT_SUCCESS(Status)) {
  1189. try { *NewTokenHandle = LocalHandle; }
  1190. except(EXCEPTION_EXECUTE_HANDLER) {
  1191. Status = GetExceptionCode();
  1192. }
  1193. }
  1194. Cleanup:
  1195. if (CapturedGroups != NULL) {
  1196. SeReleaseSidAndAttributesArray(
  1197. CapturedGroups,
  1198. PreviousMode,
  1199. TRUE
  1200. );
  1201. }
  1202. if (CapturedPrivileges != NULL) {
  1203. SeReleaseLuidAndAttributesArray(
  1204. CapturedPrivileges,
  1205. PreviousMode,
  1206. TRUE
  1207. );
  1208. }
  1209. if (CapturedSids != NULL) {
  1210. SeReleaseSidAndAttributesArray(
  1211. CapturedSids,
  1212. PreviousMode,
  1213. TRUE
  1214. );
  1215. }
  1216. return Status;
  1217. }
  1218. NTSTATUS
  1219. SeFilterToken (
  1220. IN PACCESS_TOKEN ExistingToken,
  1221. IN ULONG Flags,
  1222. IN PTOKEN_GROUPS SidsToDisable OPTIONAL,
  1223. IN PTOKEN_PRIVILEGES PrivilegesToDelete OPTIONAL,
  1224. IN PTOKEN_GROUPS RestrictedSids OPTIONAL,
  1225. OUT PACCESS_TOKEN * NewToken
  1226. )
  1227. /*++
  1228. Routine Description:
  1229. Create a new token that is a subset of an existing token.
  1230. Arguments:
  1231. ExistingToken - Is a token already open for
  1232. TOKEN_DUPLICATE access.
  1233. Flags - Flags indicating additional filtering. The flags may be:
  1234. DISABLE_MAX_PRIVILEGE - Disable all privileges
  1235. SANDBOX_INERT - Record this SAIFER flag in the token.
  1236. SidsToDisable - Contains a list of sids and attributes. All sids with
  1237. the USE_FOR_DENY_ONLY attribute that also exist in the token will
  1238. cause the new token to have that sid set with the USE_FOR_DENY_ONLY
  1239. attribte.
  1240. PrivilegesToDelete - Privileges in this list that are present in the
  1241. existing token will not exist in the final token. This is similar
  1242. to duplicating a token effective only with these privileges set to
  1243. disabled.
  1244. RestrictedSids - Contains a list of SIDs and attributes that will be
  1245. stored in the RestrictedSids field of the new token. These SIDs
  1246. are used after a normal access check to futher restrict access.
  1247. The attributes of these groups are always SE_GROUP_MANDATORY |
  1248. SE_GROUP_ENABLED | SE_GROUP_ENABLED_BY_DEFAULT. If there already
  1249. exist RestrictedSids in the original token, these sids will be
  1250. appended.
  1251. NewToken - Receives a pointer to the newly created token.
  1252. Return Value:
  1253. STATUS_SUCCESS - Indicates the operation was successful.
  1254. STATUS_INVALID_PARAMETER - Indicates one or more of the parameter values
  1255. was invalid. This value is returned if the target token is not
  1256. an impersonation token.
  1257. --*/
  1258. {
  1259. PTOKEN Token;
  1260. PTOKEN FilteredToken = NULL;
  1261. KPROCESSOR_MODE PreviousMode;
  1262. NTSTATUS Status = STATUS_SUCCESS;
  1263. ULONG Index;
  1264. ULONG CapturedSidCount = 0;
  1265. PSID_AND_ATTRIBUTES CapturedSids = NULL;
  1266. ULONG CapturedSidsLength = 0;
  1267. ULONG CapturedGroupCount = 0;
  1268. PSID_AND_ATTRIBUTES CapturedGroups = NULL;
  1269. ULONG CapturedGroupsLength = 0;
  1270. ULONG CapturedPrivilegeCount = 0;
  1271. PLUID_AND_ATTRIBUTES CapturedPrivileges = NULL;
  1272. ULONG CapturedPrivilegesLength = 0;
  1273. HANDLE LocalHandle;
  1274. OBJECT_HANDLE_INFORMATION HandleInformation;
  1275. ACCESS_MASK EffectiveDesiredAccess;
  1276. PAGED_CODE();
  1277. PreviousMode = KeGetPreviousMode();
  1278. //
  1279. // Probe parameters
  1280. //
  1281. *NewToken = NULL;
  1282. //
  1283. // Capture Sids to remove
  1284. //
  1285. if (ARGUMENT_PRESENT(SidsToDisable)) {
  1286. CapturedGroupCount = SidsToDisable->GroupCount;
  1287. CapturedGroups = SidsToDisable->Groups;
  1288. }
  1289. //
  1290. // Capture PrivilegesToDelete
  1291. //
  1292. if (ARGUMENT_PRESENT(PrivilegesToDelete)) {
  1293. CapturedPrivilegeCount = PrivilegesToDelete->PrivilegeCount;
  1294. CapturedPrivileges = PrivilegesToDelete->Privileges;
  1295. }
  1296. //
  1297. // Capture Restricted Sids
  1298. //
  1299. if (ARGUMENT_PRESENT(RestrictedSids)) {
  1300. CapturedSidCount = RestrictedSids->GroupCount;
  1301. CapturedSids = RestrictedSids->Groups;
  1302. //
  1303. // Check that the attribtes are all zero for the restricted sids
  1304. //
  1305. for (Index = 0; Index < CapturedSidCount ; Index++ ) {
  1306. if (CapturedSids[Index].Attributes != 0) {
  1307. return(STATUS_INVALID_PARAMETER);
  1308. }
  1309. }
  1310. }
  1311. //
  1312. // Check the handle's access to the existing token and get
  1313. // a pointer to that token. Pick up the default desired
  1314. // access mask from the handle while we're at it.
  1315. //
  1316. Token = (PTOKEN) ExistingToken;
  1317. #ifdef TOKEN_DEBUG
  1318. ////////////////////////////////////////////////////////////////////////////
  1319. //
  1320. // Debug
  1321. SepAcquireTokenReadLock( Token );
  1322. DbgPrint("\n");
  1323. DbgPrint("\n");
  1324. DbgPrint("Token being filtered: \n");
  1325. SepDumpToken( Token );
  1326. SepReleaseTokenReadLock( Token );
  1327. // Debug
  1328. //
  1329. ////////////////////////////////////////////////////////////////////////////
  1330. #endif //TOKEN_DEBUG
  1331. //
  1332. // Filter the existing token
  1333. //
  1334. Status = SepFilterToken(
  1335. Token,
  1336. KernelMode,
  1337. Flags,
  1338. CapturedGroupCount,
  1339. CapturedGroups,
  1340. CapturedPrivilegeCount,
  1341. CapturedPrivileges,
  1342. CapturedSidCount,
  1343. CapturedSids,
  1344. CapturedSidsLength,
  1345. &FilteredToken
  1346. );
  1347. if (NT_SUCCESS(Status)) {
  1348. //
  1349. // Insert the new token
  1350. //
  1351. Status = ObInsertObject( FilteredToken,
  1352. NULL,
  1353. 0,
  1354. 0,
  1355. NULL,
  1356. NULL
  1357. );
  1358. if (NT_SUCCESS( Status )) {
  1359. *NewToken = FilteredToken;
  1360. } else {
  1361. //
  1362. // ObInsertObject dereferences the passed object on failure
  1363. // so we don't have to clean up here.
  1364. //
  1365. #ifdef TOKEN_DEBUG
  1366. DbgPrint( "SE: ObInsertObject failed (%x) for token at %x\n", Status, NewToken );
  1367. #endif
  1368. }
  1369. }
  1370. return Status;
  1371. }
  1372. NTSTATUS
  1373. SeFastFilterToken(
  1374. IN PACCESS_TOKEN ExistingToken,
  1375. IN KPROCESSOR_MODE RequestorMode,
  1376. IN ULONG Flags,
  1377. IN ULONG GroupCount,
  1378. IN PSID_AND_ATTRIBUTES GroupsToDisable OPTIONAL,
  1379. IN ULONG PrivilegeCount,
  1380. IN PLUID_AND_ATTRIBUTES PrivilegesToDelete OPTIONAL,
  1381. IN ULONG SidCount,
  1382. IN PSID_AND_ATTRIBUTES RestrictedSids OPTIONAL,
  1383. IN ULONG SidLength,
  1384. OUT PACCESS_TOKEN * FilteredToken
  1385. )
  1386. /*++
  1387. Routine Description:
  1388. This is a fast wrapper for the Ps code to filter a token
  1389. inline of an impersonate.
  1390. This routine acquires a read lock on the token being filtered.
  1391. Arguments:
  1392. ExistingToken - Points to the token to be duplicated.
  1393. RequestorMode - Mode of client requesting the token be duplicated.
  1394. Flags - Flags indicating additional filtering. The flags may be:
  1395. DISABLE_MAX_PRIVILEGE - Disable all privileges
  1396. SANDBOX_INERT - Record this SAIFER flag in the token.
  1397. GroupCount - Count of groups to disable
  1398. GroupsToDisable - Contains a list of sids and attributes. All sids with
  1399. the USE_FOR_DENY_ONLY attribute that also exist in the token will
  1400. cause the new token to have that sid set with the USE_FOR_DENY_ONLY
  1401. attribute.
  1402. PrivilegeCount - Count of privileges to delete
  1403. PrivilegesToDelete - Privileges in this list that are present in the
  1404. existing token will not exist in the final token. This is similar
  1405. to duplicating a token effective only with these privileges set to
  1406. disabled.
  1407. SidCount - Count of restricted sids to add.
  1408. RestrictedSids - Contains a list of SIDs and attributes that will be
  1409. stored in the RestrictedSids field of the new token. These SIDs
  1410. are used after a normal access check to futher restrict access.
  1411. The attributes of these groups are always SE_GROUP_MANDATORY |
  1412. SE_GROUP_ENABLED | SE_GROUP_ENABLED_BY_DEFAULT. If there already
  1413. exist RestrictedSids in the original token, these sids will be
  1414. appended.
  1415. SidLength - Length of added restricted sids.
  1416. FilteredToken - Receives a pointer to the duplicate token.
  1417. The token has not yet been inserted into any object table.
  1418. No exceptions are expected when tring to set this OUT value.
  1419. Return Value:
  1420. STATUS_SUCCESS - The service successfully completed the requested
  1421. operation.
  1422. --*/
  1423. {
  1424. NTSTATUS Status;
  1425. PTOKEN NewToken;
  1426. Status = SepFilterToken( (PTOKEN) ExistingToken,
  1427. RequestorMode,
  1428. Flags,
  1429. GroupCount,
  1430. GroupsToDisable,
  1431. PrivilegeCount,
  1432. PrivilegesToDelete,
  1433. SidCount,
  1434. RestrictedSids,
  1435. SidLength,
  1436. &NewToken );
  1437. if (NT_SUCCESS (Status)) {
  1438. //
  1439. // Insert the new token
  1440. //
  1441. Status = ObInsertObject( NewToken,
  1442. NULL,
  1443. 0,
  1444. 0,
  1445. NULL,
  1446. NULL
  1447. );
  1448. }
  1449. if (NT_SUCCESS( Status )) {
  1450. *FilteredToken = (PACCESS_TOKEN) NewToken;
  1451. } else {
  1452. *FilteredToken = NULL;
  1453. }
  1454. return Status;
  1455. }
  1456. NTSTATUS
  1457. SepFilterToken(
  1458. IN PTOKEN ExistingToken,
  1459. IN KPROCESSOR_MODE RequestorMode,
  1460. IN ULONG Flags,
  1461. IN ULONG GroupCount,
  1462. IN PSID_AND_ATTRIBUTES GroupsToDisable OPTIONAL,
  1463. IN ULONG PrivilegeCount,
  1464. IN PLUID_AND_ATTRIBUTES PrivilegesToDelete OPTIONAL,
  1465. IN ULONG SidCount,
  1466. IN PSID_AND_ATTRIBUTES RestrictedSids OPTIONAL,
  1467. IN ULONG SidLength,
  1468. OUT PTOKEN * FilteredToken
  1469. )
  1470. /*++
  1471. Routine Description:
  1472. This routine does the bulk of the work to actually filter
  1473. a token. This routine assumes all access validation and argument
  1474. probing has been performed.
  1475. THE CALLER IS RESPONSIBLE FOR CHECKING SUBJECT RIGHTS TO CREATE THE
  1476. TYPE OF TOKEN BEING CREATED.
  1477. This routine acquires a read lock on the token being filtered.
  1478. Arguments:
  1479. ExistingToken - Points to the token to be duplicated.
  1480. RequestorMode - Mode of client requesting the token be duplicated.
  1481. Flags - Flags indicating additional filtering. The flags may be:
  1482. DISABLE_MAX_PRIVILEGE - Disable all privileges
  1483. SANDBOX_INERT - Record this SAIFER flag in the token.
  1484. GroupCount - Count of groups to disable
  1485. GroupsToDisable - Contains a list of sids and attributes. All sids with
  1486. the USE_FOR_DENY_ONLY attribute that also exist in the token will
  1487. cause the new token to have that sid set with the USE_FOR_DENY_ONLY
  1488. attribute.
  1489. PrivilegeCount - Count of privileges to delete
  1490. PrivilegesToDelete - Privileges in this list that are present in the
  1491. existing token will not exist in the final token. This is similar
  1492. to duplicating a token effective only with these privileges set to
  1493. disabled.
  1494. SidCount - Count of restricted sids to add.
  1495. RestrictedSids - Contains a list of SIDs and attributes that will be
  1496. stored in the RestrictedSids field of the new token. These SIDs
  1497. are used after a normal access check to futher restrict access.
  1498. The attributes of these groups are always SE_GROUP_MANDATORY |
  1499. SE_GROUP_ENABLED | SE_GROUP_ENABLED_BY_DEFAULT. If there already
  1500. exist RestrictedSids in the original token, the intersection of the
  1501. two sets will be in the final tokense sids will be.
  1502. SidLength - Length of added restricted sids.
  1503. FilteredToken - Receives a pointer to the duplicate token.
  1504. The token has not yet been inserted into any object table.
  1505. No exceptions are expected when tring to set this OUT value.
  1506. Return Value:
  1507. STATUS_SUCCESS - The service successfully completed the requested
  1508. operation.
  1509. --*/
  1510. {
  1511. NTSTATUS Status;
  1512. PTOKEN NewToken;
  1513. PULONG DynamicPart;
  1514. ULONG PagedPoolSize;
  1515. ULONG NonPagedPoolSize;
  1516. ULONG TokenBodyLength;
  1517. ULONG FieldOffset;
  1518. ULONG_PTR NextFree;
  1519. PSID NextSidFree;
  1520. ULONG VariableLength;
  1521. ULONG Pad;
  1522. ULONG DynamicSize;
  1523. ULONG Index;
  1524. PSECURITY_TOKEN_PROXY_DATA NewProxyData;
  1525. PSECURITY_TOKEN_AUDIT_DATA NewAuditData;
  1526. OBJECT_ATTRIBUTES ObjA ;
  1527. PERESOURCE TokenLock;
  1528. #if DBG || TOKEN_LEAK_MONITOR
  1529. ULONG Frames;
  1530. #endif
  1531. PAGED_CODE();
  1532. ASSERT( sizeof(SECURITY_IMPERSONATION_LEVEL) <= sizeof(ULONG) );
  1533. //
  1534. // Increment the reference count for this logon session
  1535. // This can not fail, since there is already a token in this logon
  1536. // session.
  1537. //
  1538. Status = SepReferenceLogonSession( &(ExistingToken->AuthenticationId) );
  1539. ASSERT( NT_SUCCESS(Status) );
  1540. if (ARGUMENT_PRESENT(ExistingToken->ProxyData)) {
  1541. Status = SepCopyProxyData(
  1542. &NewProxyData,
  1543. ExistingToken->ProxyData
  1544. );
  1545. if (!NT_SUCCESS(Status)) {
  1546. SepDeReferenceLogonSession( &(ExistingToken->AuthenticationId) );
  1547. return( Status );
  1548. }
  1549. } else {
  1550. NewProxyData = NULL;
  1551. }
  1552. if (ARGUMENT_PRESENT( ExistingToken->AuditData )) {
  1553. NewAuditData = ExAllocatePool( PagedPool, sizeof( SECURITY_TOKEN_AUDIT_DATA ));
  1554. if (NewAuditData == NULL) {
  1555. SepFreeProxyData( NewProxyData );
  1556. SepDeReferenceLogonSession( &(ExistingToken->AuthenticationId) );
  1557. return( STATUS_INSUFFICIENT_RESOURCES );
  1558. } else {
  1559. *NewAuditData = *(ExistingToken->AuditData);
  1560. }
  1561. } else {
  1562. NewAuditData = NULL;
  1563. }
  1564. TokenLock = (PERESOURCE)ExAllocatePoolWithTag( NonPagedPool, sizeof( ERESOURCE ), 'dTeS' );
  1565. if (TokenLock == NULL) {
  1566. if (NewAuditData != NULL) {
  1567. ExFreePool( NewAuditData );
  1568. }
  1569. SepFreeProxyData( NewProxyData );
  1570. SepDeReferenceLogonSession( &(ExistingToken->AuthenticationId) );
  1571. return( STATUS_INSUFFICIENT_RESOURCES );
  1572. }
  1573. //
  1574. // Create a new object
  1575. //
  1576. VariableLength = ExistingToken->VariableLength + SidLength;
  1577. #if defined(_WIN64)
  1578. //
  1579. // Account for some additional alignment requirements later on.
  1580. //
  1581. VariableLength += sizeof(ULONG);
  1582. #endif
  1583. TokenBodyLength = FIELD_OFFSET(TOKEN, VariablePart) +
  1584. VariableLength;
  1585. NonPagedPoolSize = TokenBodyLength;
  1586. PagedPoolSize = ExistingToken->DynamicCharged;
  1587. InitializeObjectAttributes( &ObjA, NULL, 0, NULL, NULL );
  1588. Status = ObCreateObject(
  1589. RequestorMode, // ProbeMode
  1590. SeTokenObjectType, // ObjectType
  1591. NULL, // ObjectAttributes
  1592. RequestorMode, // OwnershipMode
  1593. NULL, // ParseContext
  1594. TokenBodyLength, // ObjectBodySize
  1595. PagedPoolSize, // PagedPoolCharge
  1596. NonPagedPoolSize, // NonPagedPoolCharge
  1597. (PVOID *)&NewToken // Return pointer to object
  1598. );
  1599. if (!NT_SUCCESS(Status)) {
  1600. SepDeReferenceLogonSession( &(ExistingToken->AuthenticationId) );
  1601. SepFreeProxyData( NewProxyData );
  1602. ExFreePool( TokenLock );
  1603. if (NewAuditData != NULL) {
  1604. ExFreePool( NewAuditData );
  1605. }
  1606. return Status;
  1607. }
  1608. //
  1609. // The following fields differ in the new token.
  1610. //
  1611. NewToken->TokenLock = TokenLock;
  1612. ExInitializeResourceLite( NewToken->TokenLock );
  1613. //
  1614. // Allocate a new modified Id to distinguish this token from the orignial
  1615. // token.
  1616. //
  1617. ExAllocateLocallyUniqueId( &(NewToken->ModifiedId) );
  1618. ExAllocateLocallyUniqueId( &(NewToken->TokenId) );
  1619. NewToken->TokenInUse = FALSE;
  1620. //
  1621. // acquire exclusive access to the source token
  1622. //
  1623. SepAcquireTokenReadLock( ExistingToken );
  1624. //
  1625. // Main Body initialization
  1626. //
  1627. //
  1628. // The following fields are unchanged from the source token.
  1629. // Although some may change if EffectiveOnly has been specified.
  1630. //
  1631. NewToken->AuthenticationId = ExistingToken->AuthenticationId;
  1632. NewToken->ExpirationTime = ExistingToken->ExpirationTime;
  1633. NewToken->TokenSource = ExistingToken->TokenSource;
  1634. NewToken->DynamicCharged = ExistingToken->DynamicCharged;
  1635. NewToken->DynamicAvailable = 0;
  1636. NewToken->DefaultOwnerIndex = ExistingToken->DefaultOwnerIndex;
  1637. NewToken->UserAndGroupCount = ExistingToken->UserAndGroupCount;
  1638. NewToken->SessionId = ExistingToken->SessionId;
  1639. NewToken->RestrictedSidCount = 0;
  1640. NewToken->PrivilegeCount = ExistingToken->PrivilegeCount;
  1641. NewToken->VariableLength = VariableLength;
  1642. NewToken->TokenFlags = ExistingToken->TokenFlags & ~TOKEN_SESSION_NOT_REFERENCED;
  1643. NewToken->ProxyData = NewProxyData;
  1644. NewToken->AuditData = NewAuditData;
  1645. #if DBG || TOKEN_LEAK_MONITOR
  1646. NewToken->ProcessCid = PsGetCurrentThread()->Cid.UniqueProcess;
  1647. NewToken->ThreadCid = PsGetCurrentThread()->Cid.UniqueThread;
  1648. NewToken->CreateMethod = 0xF; // Filter
  1649. RtlCopyMemory(
  1650. NewToken->ImageFileName,
  1651. PsGetCurrentProcess()->ImageFileName,
  1652. min(sizeof(NewToken->ImageFileName), sizeof(PsGetCurrentProcess()->ImageFileName))
  1653. );
  1654. Frames = RtlWalkFrameChain(
  1655. (PVOID)NewToken->CreateTrace,
  1656. TRACE_SIZE,
  1657. 0
  1658. );
  1659. if (KeGetCurrentIrql() < DISPATCH_LEVEL) {
  1660. RtlWalkFrameChain(
  1661. (PVOID)&NewToken->CreateTrace[Frames],
  1662. TRACE_SIZE - Frames,
  1663. 1
  1664. );
  1665. }
  1666. SepAddTokenLogonSession(NewToken);
  1667. #endif
  1668. //
  1669. // If the caller passed in the sandbox inert flag then record it.
  1670. //
  1671. if ((Flags & SANDBOX_INERT) != 0) {
  1672. NewToken->TokenFlags |= TOKEN_SANDBOX_INERT;
  1673. }
  1674. NewToken->ParentTokenId = ExistingToken->TokenId;
  1675. NewToken->TokenType = ExistingToken->TokenType;
  1676. NewToken->ImpersonationLevel = ExistingToken->ImpersonationLevel;
  1677. //
  1678. // Compute the beginning portion of the variable part, which contains the
  1679. // sid & attributes arrays and the privilege set.
  1680. //
  1681. //
  1682. // First copy the privileges. We will later remove the ones that are
  1683. // to be deleted.
  1684. //
  1685. NextFree = (ULONG_PTR)(&NewToken->VariablePart);
  1686. NewToken->Privileges = (PLUID_AND_ATTRIBUTES)NextFree;
  1687. RtlCopyLuidAndAttributesArray( ExistingToken->PrivilegeCount,
  1688. ExistingToken->Privileges,
  1689. (PLUID_AND_ATTRIBUTES)NextFree
  1690. );
  1691. NextFree += (ExistingToken->PrivilegeCount * (ULONG)sizeof(LUID_AND_ATTRIBUTES));
  1692. VariableLength -= ( (ExistingToken->PrivilegeCount * (ULONG)sizeof(LUID_AND_ATTRIBUTES)) );
  1693. #if defined(_WIN64)
  1694. //
  1695. // At this point NextFree is 4-byte aligned, so no alignment work
  1696. // is necessary for the 32-bit product. For Win64, ensure that NextFree
  1697. // is 8-byte aligned.
  1698. //
  1699. Pad = (ULONG)(NextFree & (sizeof(PVOID)-1));
  1700. if (Pad != 0) {
  1701. Pad = sizeof(PVOID) - Pad;
  1702. NextFree += Pad;
  1703. VariableLength -= Pad;
  1704. }
  1705. #endif
  1706. //
  1707. // Figure out the count of SIDs. This is the count of users&groups +
  1708. // the number of existing restricuted SIDs plus the number of new
  1709. // restricted Sids
  1710. //
  1711. #define MAX(_x_,_y_) ((_x_) > (_y_) ? (_x_) : (_y_))
  1712. NextSidFree = (PSID) (NextFree + (ExistingToken->UserAndGroupCount +
  1713. MAX(ExistingToken->RestrictedSidCount,SidCount)) * sizeof(SID_AND_ATTRIBUTES));
  1714. NewToken->UserAndGroups = (PSID_AND_ATTRIBUTES) NextFree;
  1715. //
  1716. // Copy in the existing users & groups. We will later flag the ones
  1717. // to be disabled.
  1718. //
  1719. Status = RtlCopySidAndAttributesArray(
  1720. ExistingToken->UserAndGroupCount,
  1721. ExistingToken->UserAndGroups,
  1722. VariableLength,
  1723. (PSID_AND_ATTRIBUTES)NextFree,
  1724. NextSidFree,
  1725. &NextSidFree,
  1726. &VariableLength
  1727. );
  1728. ASSERT(NT_SUCCESS(Status));
  1729. NextFree += (ExistingToken->UserAndGroupCount * (ULONG)sizeof(SID_AND_ATTRIBUTES));
  1730. //
  1731. // Now add all the existing restricted sids. We need to take the
  1732. // intersection of the two sets.
  1733. //
  1734. NewToken->RestrictedSids = (PSID_AND_ATTRIBUTES) NextFree;
  1735. for (Index = 0; Index < SidCount ; Index++ ) {
  1736. if ( ( ExistingToken->RestrictedSidCount == 0 ) ||
  1737. SepSidInSidAndAttributes(
  1738. ExistingToken->RestrictedSids,
  1739. ExistingToken->RestrictedSidCount,
  1740. NULL, // no self sid
  1741. RestrictedSids[Index].Sid
  1742. )) {
  1743. Status = RtlCopySidAndAttributesArray(
  1744. 1,
  1745. &RestrictedSids[Index],
  1746. VariableLength,
  1747. (PSID_AND_ATTRIBUTES)NextFree,
  1748. NextSidFree,
  1749. &NextSidFree,
  1750. &VariableLength
  1751. );
  1752. ASSERT(NT_SUCCESS(Status));
  1753. NextFree += sizeof(SID_AND_ATTRIBUTES);
  1754. NewToken->RestrictedSids[NewToken->RestrictedSidCount].Attributes =
  1755. SE_GROUP_ENABLED | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_MANDATORY;
  1756. NewToken->RestrictedSidCount++;
  1757. }
  1758. }
  1759. //
  1760. // Allocate the dynamic portion
  1761. //
  1762. DynamicSize = SeLengthSid( ExistingToken->PrimaryGroup );
  1763. if (ExistingToken->DefaultDacl) {
  1764. DynamicSize += ExistingToken->DefaultDacl->AclSize;
  1765. }
  1766. DynamicPart = (PULONG)ExAllocatePoolWithTag(
  1767. PagedPool,
  1768. DynamicSize,
  1769. 'dTeS'
  1770. );
  1771. NewToken->DynamicPart = DynamicPart;
  1772. if (DynamicPart == NULL) {
  1773. SepReleaseTokenReadLock( ExistingToken );
  1774. ObDereferenceObject( NewToken );
  1775. return( STATUS_INSUFFICIENT_RESOURCES );
  1776. }
  1777. //
  1778. // Make sure the new token has some restrictions.
  1779. // If it doesn't, then we've ended up with a token
  1780. // that gives us more access than the original,
  1781. // which we don't want.
  1782. //
  1783. if ((ExistingToken->RestrictedSidCount != 0) &&
  1784. (NewToken->RestrictedSidCount == 0)) {
  1785. SepReleaseTokenReadLock( ExistingToken );
  1786. Status = STATUS_INVALID_PARAMETER;
  1787. //
  1788. // Cleanup. ObDereferenceObject will cause the logon
  1789. // session to be dereferenced, and will free the proxy data
  1790. // as well as the audit data.
  1791. //
  1792. // See SepTokenDeleteMethod(), which is called by
  1793. // the object manager when the token object is
  1794. // being freed.
  1795. //
  1796. ObDereferenceObject( NewToken );
  1797. return(Status);
  1798. }
  1799. //
  1800. // If there are any restricted sids in the token, turn on the restricted
  1801. // flag
  1802. //
  1803. if (NewToken->RestrictedSidCount > 0) {
  1804. NewToken->TokenFlags |= TOKEN_IS_RESTRICTED;
  1805. }
  1806. //
  1807. // Copy and initialize the dynamic part.
  1808. // The dynamic part is assumed to be position independent.
  1809. //
  1810. RtlCopyMemory( (PVOID)DynamicPart,
  1811. (PVOID)(ExistingToken->DynamicPart),
  1812. DynamicSize
  1813. );
  1814. //
  1815. // If present, set the address of the default Dacl
  1816. //
  1817. if (ARGUMENT_PRESENT(ExistingToken->DefaultDacl)) {
  1818. ASSERT( (ULONG_PTR)(ExistingToken->DefaultDacl) >=
  1819. (ULONG_PTR)(ExistingToken->DynamicPart) );
  1820. FieldOffset = (ULONG)((ULONG_PTR)(ExistingToken->DefaultDacl) -
  1821. (ULONG_PTR)(ExistingToken->DynamicPart));
  1822. NewToken->DefaultDacl = (PACL)(FieldOffset + (ULONG_PTR)DynamicPart);
  1823. } else {
  1824. NewToken->DefaultDacl = NULL;
  1825. }
  1826. //
  1827. // Set the address of the primary group
  1828. //
  1829. ASSERT(ARGUMENT_PRESENT(ExistingToken->PrimaryGroup));
  1830. ASSERT( (ULONG_PTR)(ExistingToken->PrimaryGroup) >=
  1831. (ULONG_PTR)(ExistingToken->DynamicPart) );
  1832. FieldOffset = (ULONG)((ULONG_PTR)(ExistingToken->PrimaryGroup) -
  1833. (ULONG_PTR)(ExistingToken->DynamicPart));
  1834. //
  1835. // Release the source token.
  1836. //
  1837. SepReleaseTokenReadLock( ExistingToken );
  1838. NewToken->PrimaryGroup = (PACL)(FieldOffset + (ULONG_PTR)(DynamicPart));
  1839. //
  1840. // For the time being, take the easy way to generating an "EffectiveOnly"
  1841. // duplicate. That is, use the same space required of the original, just
  1842. // eliminate any IDs or privileges not active.
  1843. //
  1844. // Ultimately, if duplication becomes a common operation, then it will be
  1845. // worthwhile to recalculate the actual space needed and copy only the
  1846. // effective IDs/privileges into the new token.
  1847. //
  1848. SepRemoveDisabledGroupsAndPrivileges(
  1849. NewToken,
  1850. Flags,
  1851. GroupCount,
  1852. GroupsToDisable,
  1853. PrivilegeCount,
  1854. PrivilegesToDelete
  1855. );
  1856. #ifdef TOKEN_DEBUG
  1857. ////////////////////////////////////////////////////////////////////////////
  1858. //
  1859. // Debug
  1860. DbgPrint("\n");
  1861. DbgPrint("\n");
  1862. DbgPrint("\n");
  1863. DbgPrint("Filter token:\n");
  1864. SepDumpToken( NewToken );
  1865. // Debug
  1866. //
  1867. ////////////////////////////////////////////////////////////////////////////
  1868. #endif //TOKEN_DEBUG
  1869. (*FilteredToken) = NewToken;
  1870. return Status;
  1871. }