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.

1048 lines
25 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. Tokenset.c
  5. Abstract:
  6. This module implements the SET function for the executive
  7. token object.
  8. Author:
  9. Jim Kelly (JimK) 15-June-1990
  10. Revision History:
  11. --*/
  12. #include "pch.h"
  13. #pragma hdrstop
  14. #ifdef ALLOC_PRAGMA
  15. #pragma alloc_text(PAGE,NtSetInformationToken)
  16. #pragma alloc_text(PAGE,SepExpandDynamic)
  17. #pragma alloc_text(PAGE,SepFreePrimaryGroup)
  18. #pragma alloc_text(PAGE,SepFreeDefaultDacl)
  19. #pragma alloc_text(PAGE,SepAppendPrimaryGroup)
  20. #pragma alloc_text(PAGE,SepAppendDefaultDacl)
  21. #pragma alloc_text(PAGE,SeSetSessionIdToken)
  22. #endif
  23. NTSTATUS
  24. NtSetInformationToken (
  25. IN HANDLE TokenHandle,
  26. IN TOKEN_INFORMATION_CLASS TokenInformationClass,
  27. IN PVOID TokenInformation,
  28. IN ULONG TokenInformationLength
  29. )
  30. /*++
  31. Routine Description:
  32. Modify information in a specified token.
  33. Arguments:
  34. TokenHandle - Provides a handle to the token to operate on.
  35. TokenInformationClass - The token information class being set.
  36. TokenInformation - The buffer containing the new values for the
  37. specified class of information. The buffer must be aligned
  38. on at least a longword boundary. The actual structures
  39. provided are dependent upon the information class specified,
  40. as defined in the TokenInformationClass parameter
  41. description.
  42. TokenInformation Format By Information Class:
  43. TokenUser => This value is not a valid value for this API.
  44. The User ID may not be replaced.
  45. TokenGroups => This value is not a valid value for this
  46. API. The Group IDs may not be replaced. However, groups
  47. may be enabled and disabled using NtAdjustGroupsToken().
  48. TokenPrivileges => This value is not a valid value for
  49. this API. Privilege information may not be replaced.
  50. However, privileges may be explicitly enabled and disabled
  51. using the NtAdjustPrivilegesToken API.
  52. TokenOwner => TOKEN_OWNER data structure.
  53. TOKEN_ADJUST_DEFAULT access is needed to replace this
  54. information in a token. The owner values that may be
  55. specified are restricted to the user and group IDs with an
  56. attribute indicating they may be assigned as the owner of
  57. objects.
  58. TokenPrimaryGroup => TOKEN_PRIMARY_GROUP data structure.
  59. TOKEN_ADJUST_DEFAULT access is needed to replace this
  60. information in a token. The primary group values that may
  61. be specified are restricted to be one of the group IDs
  62. already in the token.
  63. TokenDefaultDacl => TOKEN_DEFAULT_DACL data structure.
  64. TOKEN_ADJUST_DEFAULT access is needed to replace this
  65. information in a token. The ACL provided as a new default
  66. discretionary ACL is not validated for structural
  67. correctness or consistency.
  68. TokenSource => This value is not a valid value for this
  69. API. The source name and context handle may not be
  70. replaced.
  71. TokenStatistics => This value is not a valid value for this
  72. API. The statistics of a token are read-only.
  73. TokenInformationLength - Indicates the length, in bytes, of the
  74. TokenInformation buffer. This is only the length of the primary
  75. buffer. All extensions of the primary buffer are self describing.
  76. Return Value:
  77. STATUS_SUCCESS - The operation was successful.
  78. STATUS_INVALID_OWNER - The ID specified to be an owner (or
  79. default owner) is not one the caller may assign as the owner
  80. of an object.
  81. STATUS_INVALID_INFO_CLASS - The specified information class is
  82. not one that may be specified in this API.
  83. STATUS_ALLOTTED_SPACE_EXCEEDED - The space allotted for storage
  84. of the default discretionary access control and the primary
  85. group ID is not large enough to accept the new value of one
  86. of these fields.
  87. --*/
  88. {
  89. KPROCESSOR_MODE PreviousMode;
  90. NTSTATUS Status;
  91. PTOKEN Token;
  92. ULONG Index;
  93. BOOLEAN Found;
  94. BOOLEAN TokenModified = FALSE;
  95. ULONG NewLength;
  96. ULONG CurrentLength;
  97. PSID CapturedOwner;
  98. PSID CapturedPrimaryGroup;
  99. PACL CapturedDefaultDacl;
  100. ACCESS_MASK DesiredAccess;
  101. PAGED_CODE();
  102. //
  103. // Get previous processor mode and probe input buffer if necessary.
  104. //
  105. PreviousMode = KeGetPreviousMode();
  106. if (PreviousMode != KernelMode) {
  107. try {
  108. //
  109. // This just probes the main part of the information buffer.
  110. // Any information class-specific data hung off the primary
  111. // buffer are self describing and must be probed separately
  112. // below.
  113. //
  114. ProbeForRead(
  115. TokenInformation,
  116. TokenInformationLength,
  117. sizeof(ULONG)
  118. );
  119. } except(EXCEPTION_EXECUTE_HANDLER) {
  120. return GetExceptionCode();
  121. }
  122. }
  123. //
  124. // Return error if not legal class
  125. //
  126. if ( (TokenInformationClass != TokenOwner) &&
  127. (TokenInformationClass != TokenPrimaryGroup) &&
  128. (TokenInformationClass != TokenSessionId) &&
  129. (TokenInformationClass != TokenDefaultDacl) &&
  130. (TokenInformationClass != TokenSessionReference) ) {
  131. return STATUS_INVALID_INFO_CLASS;
  132. }
  133. //
  134. // Check access rights and reference token
  135. //
  136. DesiredAccess = TOKEN_ADJUST_DEFAULT;
  137. if (TokenInformationClass == TokenSessionId) {
  138. DesiredAccess |= TOKEN_ADJUST_SESSIONID;
  139. }
  140. Status = ObReferenceObjectByHandle(
  141. TokenHandle, // Handle
  142. DesiredAccess, // DesiredAccess
  143. SeTokenObjectType, // ObjectType
  144. PreviousMode, // AccessMode
  145. (PVOID *)&Token, // Object
  146. NULL // GrantedAccess
  147. );
  148. if ( !NT_SUCCESS(Status) ) {
  149. return Status;
  150. }
  151. //
  152. // Case on information class.
  153. //
  154. switch ( TokenInformationClass ) {
  155. case TokenOwner:
  156. //
  157. // Make sure the buffer is large enough to hold the
  158. // necessary information class data structure.
  159. //
  160. if (TokenInformationLength < (ULONG)sizeof(TOKEN_OWNER)) {
  161. ObDereferenceObject( Token );
  162. return STATUS_INFO_LENGTH_MISMATCH;
  163. }
  164. //
  165. // Capture and copy
  166. try {
  167. //
  168. // Capture Owner SID
  169. //
  170. CapturedOwner = ((PTOKEN_OWNER)TokenInformation)->Owner;
  171. Status = SeCaptureSid(
  172. CapturedOwner,
  173. PreviousMode,
  174. NULL, 0,
  175. PagedPool,
  176. TRUE,
  177. &CapturedOwner
  178. );
  179. } except(EXCEPTION_EXECUTE_HANDLER) {
  180. ObDereferenceObject( Token );
  181. return GetExceptionCode();
  182. }
  183. if (!NT_SUCCESS(Status)) {
  184. ObDereferenceObject( Token );
  185. return Status;
  186. }
  187. Index = 0;
  188. //
  189. // Gain write access to the token.
  190. //
  191. SepAcquireTokenWriteLock( Token );
  192. //
  193. // Walk through the list of user and group IDs looking
  194. // for a match to the specified SID. If one is found,
  195. // make sure it may be assigned as an owner. If it can,
  196. // then set the index in the token's OwnerIndex field.
  197. // Otherwise, return invalid owner error.
  198. //
  199. while (Index < Token->UserAndGroupCount) {
  200. try {
  201. Found = RtlEqualSid(
  202. CapturedOwner,
  203. Token->UserAndGroups[Index].Sid
  204. );
  205. if ( Found ) {
  206. if ( SepIdAssignableAsOwner(Token,Index) ){
  207. Token->DefaultOwnerIndex = Index;
  208. TokenModified = TRUE;
  209. Status = STATUS_SUCCESS;
  210. } else {
  211. Status = STATUS_INVALID_OWNER;
  212. } //endif assignable
  213. SepReleaseTokenWriteLock( Token, TokenModified );
  214. ObDereferenceObject( Token );
  215. SeReleaseSid( CapturedOwner, PreviousMode, TRUE);
  216. return Status;
  217. } //endif Found
  218. } except(EXCEPTION_EXECUTE_HANDLER) {
  219. SepReleaseTokenWriteLock( Token, TokenModified );
  220. ObDereferenceObject( Token );
  221. SeReleaseSid( CapturedOwner, PreviousMode, TRUE);
  222. return GetExceptionCode();
  223. } //endtry
  224. Index += 1;
  225. } //endwhile
  226. SepReleaseTokenWriteLock( Token, TokenModified );
  227. ObDereferenceObject( Token );
  228. SeReleaseSid( CapturedOwner, PreviousMode, TRUE);
  229. return STATUS_INVALID_OWNER;
  230. case TokenPrimaryGroup:
  231. //
  232. // Assuming everything works out, the strategy is to move everything
  233. // in the Dynamic part of the token (exept the primary group) to
  234. // the beginning of the dynamic part, freeing up the entire end of
  235. // the dynamic part for the new primary group.
  236. //
  237. //
  238. // Make sure the buffer is large enough to hold the
  239. // necessary information class data structure.
  240. //
  241. if (TokenInformationLength < (ULONG)sizeof(TOKEN_PRIMARY_GROUP)) {
  242. ObDereferenceObject( Token );
  243. return STATUS_INFO_LENGTH_MISMATCH;
  244. }
  245. //
  246. // Capture And Validate TOKEN_PRIMARY_GROUP and corresponding SID.
  247. //
  248. try {
  249. CapturedPrimaryGroup =
  250. ((PTOKEN_PRIMARY_GROUP)TokenInformation)->PrimaryGroup;
  251. Status = SeCaptureSid(
  252. CapturedPrimaryGroup,
  253. PreviousMode,
  254. NULL, 0,
  255. PagedPool,
  256. TRUE,
  257. &CapturedPrimaryGroup
  258. );
  259. } except(EXCEPTION_EXECUTE_HANDLER) {
  260. ObDereferenceObject( Token );
  261. return GetExceptionCode();
  262. }
  263. if (!NT_SUCCESS(Status)) {
  264. ObDereferenceObject( Token );
  265. return Status;
  266. }
  267. if (!SepIdAssignableAsGroup( Token, CapturedPrimaryGroup )) {
  268. ObDereferenceObject( Token );
  269. SeReleaseSid( CapturedPrimaryGroup, PreviousMode, TRUE);
  270. return STATUS_INVALID_PRIMARY_GROUP;
  271. }
  272. NewLength = SeLengthSid( CapturedPrimaryGroup );
  273. //
  274. // Gain write access to the token.
  275. //
  276. SepAcquireTokenWriteLock( Token );
  277. //
  278. // See if there is enough room in the dynamic part of the token
  279. // to replace the current Primary Group with the one specified.
  280. //
  281. if (Token->DefaultDacl) {
  282. NewLength += Token->DefaultDacl->AclSize;
  283. }
  284. if (NewLength > Token->DynamicCharged) {
  285. SepReleaseTokenWriteLock( Token, TokenModified );
  286. ObDereferenceObject( Token );
  287. SeReleaseSid( CapturedPrimaryGroup, PreviousMode, TRUE);
  288. return STATUS_ALLOTTED_SPACE_EXCEEDED;
  289. }
  290. //
  291. // Expand the tokens dynamic buffer if we have to
  292. //
  293. Status = SepExpandDynamic( Token, NewLength );
  294. if (!NT_SUCCESS (Status)) {
  295. SepReleaseTokenWriteLock( Token, TokenModified );
  296. ObDereferenceObject( Token );
  297. SeReleaseSid( CapturedPrimaryGroup, PreviousMode, TRUE);
  298. return Status;
  299. }
  300. //
  301. // Free up the existing primary group
  302. //
  303. SepFreePrimaryGroup( Token );
  304. //
  305. // And put the new SID in its place
  306. //
  307. SepAppendPrimaryGroup( Token, CapturedPrimaryGroup );
  308. TokenModified = TRUE;
  309. //
  310. // All done.
  311. //
  312. SepReleaseTokenWriteLock( Token, TokenModified );
  313. ObDereferenceObject( Token );
  314. SeReleaseSid( CapturedPrimaryGroup, PreviousMode, TRUE);
  315. return STATUS_SUCCESS;
  316. case TokenDefaultDacl:
  317. //
  318. // Assuming everything works out, the strategy is to move everything
  319. // in the Dynamic part of the token (exept the default Dacl) to
  320. // the beginning of the dynamic part, freeing up the entire end of
  321. // the dynamic part for the new default Dacl.
  322. //
  323. //
  324. // Make sure the buffer is large enough to hold the
  325. // necessary information class data structure.
  326. //
  327. if (TokenInformationLength < (ULONG)sizeof(TOKEN_DEFAULT_DACL)) {
  328. ObDereferenceObject( Token );
  329. return STATUS_INFO_LENGTH_MISMATCH;
  330. }
  331. //
  332. // Capture And Validate TOKEN_DEFAULT_DACL and corresponding ACL.
  333. //
  334. try {
  335. CapturedDefaultDacl =
  336. ((PTOKEN_DEFAULT_DACL)TokenInformation)->DefaultDacl;
  337. if (ARGUMENT_PRESENT(CapturedDefaultDacl)) {
  338. Status = SeCaptureAcl(
  339. CapturedDefaultDacl,
  340. PreviousMode,
  341. NULL, 0,
  342. PagedPool,
  343. TRUE,
  344. &CapturedDefaultDacl,
  345. &NewLength
  346. );
  347. } else {
  348. NewLength = 0;
  349. Status = STATUS_SUCCESS;
  350. }
  351. } except(EXCEPTION_EXECUTE_HANDLER) {
  352. ObDereferenceObject( Token );
  353. return GetExceptionCode();
  354. }
  355. if (!NT_SUCCESS(Status)) {
  356. ObDereferenceObject( Token );
  357. return Status;
  358. }
  359. //
  360. // Gain write access to the token.
  361. //
  362. SepAcquireTokenWriteLock( Token );
  363. //
  364. // See if there is enough room in the dynamic part of the token
  365. // to replace the current Default Dacl with the one specified.
  366. //
  367. NewLength += SeLengthSid( Token->PrimaryGroup );
  368. if (NewLength > Token->DynamicCharged) {
  369. SepReleaseTokenWriteLock( Token, TokenModified );
  370. ObDereferenceObject( Token );
  371. if (ARGUMENT_PRESENT(CapturedDefaultDacl)) {
  372. SeReleaseAcl( CapturedDefaultDacl, PreviousMode, TRUE);
  373. }
  374. return STATUS_ALLOTTED_SPACE_EXCEEDED;
  375. }
  376. //
  377. // Expand the tokens dynamic buffer if we have to
  378. //
  379. Status = SepExpandDynamic( Token, NewLength );
  380. if (!NT_SUCCESS (Status)) {
  381. SepReleaseTokenWriteLock( Token, TokenModified );
  382. ObDereferenceObject( Token );
  383. if (ARGUMENT_PRESENT(CapturedDefaultDacl)) {
  384. SeReleaseAcl( CapturedDefaultDacl, PreviousMode, TRUE);
  385. }
  386. return Status;
  387. }
  388. //
  389. // Free up the existing Default Dacl
  390. //
  391. SepFreeDefaultDacl( Token );
  392. //
  393. // And put the new ACL in its place
  394. //
  395. if (ARGUMENT_PRESENT(CapturedDefaultDacl)) {
  396. SepAppendDefaultDacl( Token, CapturedDefaultDacl );
  397. }
  398. TokenModified = TRUE;
  399. //
  400. // All done.
  401. //
  402. SepReleaseTokenWriteLock( Token, TokenModified );
  403. ObDereferenceObject( Token );
  404. if (ARGUMENT_PRESENT(CapturedDefaultDacl)) {
  405. SeReleaseAcl( CapturedDefaultDacl, PreviousMode, TRUE);
  406. }
  407. return STATUS_SUCCESS;
  408. case TokenSessionId:
  409. {
  410. ULONG SessionId;
  411. if ( TokenInformationLength != sizeof(ULONG) ) {
  412. ObDereferenceObject( Token );
  413. return( STATUS_INFO_LENGTH_MISMATCH );
  414. }
  415. try {
  416. SessionId = *(PULONG)TokenInformation;
  417. } except(EXCEPTION_EXECUTE_HANDLER) {
  418. ObDereferenceObject( Token );
  419. return GetExceptionCode();
  420. }
  421. //
  422. // We only allow TCB to set SessionId's
  423. //
  424. if ( !SeSinglePrivilegeCheck(SeTcbPrivilege,PreviousMode) ) {
  425. ObDereferenceObject( Token );
  426. return( STATUS_PRIVILEGE_NOT_HELD );
  427. }
  428. //
  429. // Set SessionId for the token
  430. //
  431. SeSetSessionIdToken( (PACCESS_TOKEN)Token,
  432. SessionId );
  433. ObDereferenceObject( Token );
  434. return( STATUS_SUCCESS );
  435. }
  436. case TokenSessionReference:
  437. {
  438. ULONG SessionReferenced;
  439. BOOLEAN DereferenceSession = FALSE;
  440. if ( TokenInformationLength != sizeof(ULONG) ) {
  441. ObDereferenceObject( Token );
  442. return( STATUS_INFO_LENGTH_MISMATCH );
  443. }
  444. try {
  445. SessionReferenced = *(PULONG)TokenInformation;
  446. } except(EXCEPTION_EXECUTE_HANDLER) {
  447. ObDereferenceObject( Token );
  448. return GetExceptionCode();
  449. }
  450. //
  451. // We only allow TCB to set Session referenced.
  452. //
  453. if ( !SeSinglePrivilegeCheck(SeTcbPrivilege,PreviousMode) ) {
  454. ObDereferenceObject( Token );
  455. return( STATUS_PRIVILEGE_NOT_HELD );
  456. }
  457. //
  458. // We don't yet have use for this so don't implement it.
  459. //
  460. if ( SessionReferenced ) {
  461. ObDereferenceObject( Token );
  462. return STATUS_INVALID_PARAMETER;
  463. }
  464. //
  465. // Determine if we're changing the state and change it with the write lock held
  466. //
  467. SepAcquireTokenWriteLock( Token );
  468. if ( (Token->TokenFlags & TOKEN_SESSION_NOT_REFERENCED) == 0 ) {
  469. #if DBG || TOKEN_LEAK_MONITOR
  470. SepRemoveTokenLogonSession( Token );
  471. #endif
  472. Token->TokenFlags |= TOKEN_SESSION_NOT_REFERENCED;
  473. DereferenceSession = TRUE;
  474. }
  475. SepReleaseTokenWriteLock( Token, FALSE );
  476. //
  477. // Do the actual dereference without any locks held
  478. //
  479. if ( DereferenceSession ) {
  480. SepDeReferenceLogonSession( &Token->AuthenticationId );
  481. }
  482. ObDereferenceObject( Token );
  483. return( STATUS_SUCCESS );
  484. }
  485. } //endswitch
  486. ASSERT( TRUE == FALSE ); // Should never reach here.
  487. return( STATUS_INVALID_PARAMETER );
  488. }
  489. NTSTATUS
  490. SepExpandDynamic(
  491. IN PTOKEN Token,
  492. IN ULONG NewLength
  493. )
  494. /*++
  495. Routine Description:
  496. This routines checks if the existing token dynamic buffer is big enough for the new group/dacl.
  497. If it isn't then its reallocated.
  498. Arguments:
  499. Token - Pointer to the token to expand. Locked for write access.
  500. Return Value:
  501. NTSTATUS - Status of operation
  502. --*/
  503. {
  504. ULONG CurrentSize;
  505. PVOID NewDynamic, OldDynamic;
  506. //
  507. // Work out how big it is now
  508. //
  509. CurrentSize = SeLengthSid( Token->PrimaryGroup ) + Token->DynamicAvailable;
  510. if (Token->DefaultDacl) {
  511. CurrentSize += Token->DefaultDacl->AclSize;
  512. }
  513. if (NewLength <= CurrentSize) {
  514. return STATUS_SUCCESS;
  515. }
  516. NewDynamic = ExAllocatePoolWithTag (PagedPool,
  517. NewLength,
  518. 'dTeS');
  519. if (NewDynamic == NULL) {
  520. return STATUS_INSUFFICIENT_RESOURCES;
  521. }
  522. OldDynamic = Token->DynamicPart;
  523. RtlCopyMemory (NewDynamic, OldDynamic, CurrentSize);
  524. Token->DynamicPart = NewDynamic;
  525. Token->DynamicAvailable += NewLength - CurrentSize;
  526. //
  527. //Relocate the pointers within the new buffer
  528. //
  529. if (Token->DefaultDacl) {
  530. Token->DefaultDacl = (PACL) ((PUCHAR) NewDynamic + ((PUCHAR) Token->DefaultDacl - (PUCHAR) OldDynamic));
  531. }
  532. Token->PrimaryGroup = (PSID) ((PUCHAR) NewDynamic + ((PUCHAR) Token->PrimaryGroup - (PUCHAR) OldDynamic));
  533. ExFreePool (OldDynamic);
  534. return STATUS_SUCCESS;
  535. }
  536. VOID
  537. SepFreePrimaryGroup(
  538. IN PTOKEN Token
  539. )
  540. /*++
  541. Routine Description:
  542. Free up the space in the dynamic part of the token take up by the primary
  543. group.
  544. The token is assumed to be locked for write access before calling
  545. this routine.
  546. Arguments:
  547. Token - Pointer to the token.
  548. Return Value:
  549. None.
  550. --*/
  551. {
  552. PAGED_CODE();
  553. //
  554. // Add the size of the primary group to the DynamicAvailable field.
  555. //
  556. Token->DynamicAvailable += SeLengthSid( Token->PrimaryGroup );
  557. //
  558. // If there is a default discretionary ACL, and it is not already at the
  559. // beginning of the dynamic part, move it there (remember to update the
  560. // pointer to it).
  561. //
  562. if (ARGUMENT_PRESENT(Token->DefaultDacl)) {
  563. if (Token->DynamicPart != (PULONG)(Token->DefaultDacl)) {
  564. RtlMoveMemory(
  565. (PVOID)(Token->DynamicPart),
  566. (PVOID)(Token->DefaultDacl),
  567. Token->DefaultDacl->AclSize
  568. );
  569. Token->DefaultDacl = (PACL)(Token->DynamicPart);
  570. }
  571. }
  572. return;
  573. }
  574. VOID
  575. SepFreeDefaultDacl(
  576. IN PTOKEN Token
  577. )
  578. /*++
  579. Routine Description:
  580. Free up the space in the dynamic part of the token take up by the default
  581. discretionary access control list.
  582. The token is assumed to be locked for write access before calling
  583. this routine.
  584. Arguments:
  585. Token - Pointer to the token.
  586. Return Value:
  587. None.
  588. --*/
  589. {
  590. ULONG PrimaryGroupSize;
  591. PAGED_CODE();
  592. //
  593. // Add the size of the Default Dacl (if there is one) to the
  594. // DynamicAvailable field.
  595. //
  596. if (ARGUMENT_PRESENT(Token->DefaultDacl)) {
  597. Token->DynamicAvailable += Token->DefaultDacl->AclSize;
  598. Token->DefaultDacl = NULL;
  599. }
  600. //
  601. // If it is not already at the beginning of the dynamic part, move
  602. // the primary group there (remember to update the pointer to it).
  603. //
  604. if (Token->DynamicPart != (PULONG)(Token->PrimaryGroup)) {
  605. PrimaryGroupSize = SeLengthSid( Token->PrimaryGroup );
  606. RtlMoveMemory(
  607. (PVOID)(Token->DynamicPart),
  608. (PVOID)(Token->PrimaryGroup),
  609. PrimaryGroupSize
  610. );
  611. Token->PrimaryGroup = (PSID)(Token->DynamicPart);
  612. }
  613. return;
  614. }
  615. VOID
  616. SepAppendPrimaryGroup(
  617. IN PTOKEN Token,
  618. IN PSID PSid
  619. )
  620. /*++
  621. Routine Description:
  622. Add a primary group SID to the available space at the end of the dynamic
  623. part of the token. It is the caller's responsibility to ensure that the
  624. primary group SID fits within the available space of the dynamic part of
  625. the token.
  626. The token is assumed to be locked for write access before calling
  627. this routine.
  628. Arguments:
  629. Token - Pointer to the token.
  630. PSid - Pointer to the SID to add.
  631. Return Value:
  632. None.
  633. --*/
  634. {
  635. ULONG_PTR NextFree;
  636. ULONG SidSize;
  637. PAGED_CODE();
  638. //
  639. // Add the size of the Default Dacl (if there is one) to the
  640. // address of the Dynamic Part of the token to establish
  641. // where the primary group should be placed.
  642. //
  643. if (ARGUMENT_PRESENT(Token->DefaultDacl)) {
  644. // ASSERT( (ULONG)(Token->DefaultDacl->AclSize) ==
  645. // (ULONG)LongAlignSize(Token->DefaultDacl->AclSize) );
  646. NextFree = (ULONG_PTR)(Token->DynamicPart) + Token->DefaultDacl->AclSize;
  647. } else {
  648. NextFree = (ULONG_PTR)(Token->DynamicPart);
  649. }
  650. //
  651. // Now copy the primary group SID.
  652. //
  653. SidSize = SeLengthSid( PSid );
  654. RtlCopyMemory(
  655. (PVOID)NextFree,
  656. (PVOID)PSid,
  657. SidSize
  658. );
  659. Token->PrimaryGroup = (PSID)NextFree;
  660. //
  661. // And decrement the amount of the dynamic part that is available.
  662. //
  663. ASSERT( SidSize <= (Token->DynamicAvailable) );
  664. Token->DynamicAvailable -= SidSize;
  665. return;
  666. }
  667. VOID
  668. SepAppendDefaultDacl(
  669. IN PTOKEN Token,
  670. IN PACL PAcl
  671. )
  672. /*++
  673. Routine Description:
  674. Add a default discretionary ACL to the available space at the end of the
  675. dynamic part of the token. It is the caller's responsibility to ensure
  676. that the default Dacl fits within the available space of the dynamic
  677. part of the token.
  678. The token is assumed to be locked for write access before calling
  679. this routine.
  680. Arguments:
  681. Token - Pointer to the token.
  682. PAcl - Pointer to the ACL to add.
  683. Return Value:
  684. None.
  685. --*/
  686. {
  687. ULONG_PTR NextFree;
  688. ULONG AclSize;
  689. PAGED_CODE();
  690. //
  691. // Add the size of the primary group to the
  692. // address of the Dynamic Part of the token to establish
  693. // where the primary group should be placed.
  694. //
  695. ASSERT(ARGUMENT_PRESENT(Token->PrimaryGroup));
  696. NextFree = (ULONG_PTR)(Token->DynamicPart) + SeLengthSid(Token->PrimaryGroup);
  697. //
  698. // Now copy the default Dacl
  699. //
  700. AclSize = (ULONG)(PAcl->AclSize);
  701. // ASSERT(AclSize == (ULONG)LongAlignSize(AclSize));
  702. RtlCopyMemory(
  703. (PVOID)NextFree,
  704. (PVOID)PAcl,
  705. AclSize
  706. );
  707. Token->DefaultDacl = (PACL)NextFree;
  708. //
  709. // And decrement the amount of the dynamic part that is available.
  710. //
  711. ASSERT( AclSize <= (Token->DynamicAvailable) );
  712. Token->DynamicAvailable -= AclSize;
  713. return;
  714. }
  715. NTSTATUS
  716. SeSetSessionIdToken(
  717. PACCESS_TOKEN Token,
  718. ULONG SessionId
  719. )
  720. /*++
  721. Routine Description:
  722. Sets the SessionId for the specified token object.
  723. Arguments:
  724. pOpaqueToken (input)
  725. Opaque kernel Token access pointer
  726. SessionId (input)
  727. SessionId to store in token
  728. Return Value:
  729. STATUS_SUCCESS - no error
  730. --*/
  731. {
  732. PAGED_CODE();
  733. //
  734. // Gain write access to the token.
  735. //
  736. SepAcquireTokenWriteLock( ((PTOKEN)Token) );
  737. ((PTOKEN)Token)->SessionId = SessionId;
  738. SepReleaseTokenWriteLock( ((PTOKEN)Token), TRUE );
  739. return( STATUS_SUCCESS );
  740. }