2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/ps/security.c
5 * PURPOSE: Process Manager: Process/Thread Security
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
8 * Thomas Weidenmueller (w3seek@reactos.org)
11 /* INCLUDES ******************************************************************/
17 PTOKEN PspBootAccessToken
;
26 /* PRIVATE FUNCTIONS *********************************************************/
30 PspDeleteProcessSecurity(IN PEPROCESS Process
)
33 PSTRACE(PS_SECURITY_DEBUG
, "Process: %p\n", Process
);
35 /* Check if we have a token */
36 if (Process
->Token
.Object
)
39 SeDeassignPrimaryToken(Process
);
40 Process
->Token
.Object
= NULL
;
46 PspDeleteThreadSecurity(IN PETHREAD Thread
)
48 PPS_IMPERSONATION_INFORMATION ImpersonationInfo
= Thread
->ImpersonationInfo
;
50 PSTRACE(PS_SECURITY_DEBUG
, "Thread: %p\n", Thread
);
52 /* Check if we have active impersonation info */
53 if (Thread
->ActiveImpersonationInfo
)
55 /* Dereference its token */
56 ObDereferenceObject(ImpersonationInfo
->Token
);
59 /* Check if we have impersonation info */
60 if (ImpersonationInfo
)
63 ExFreePool(ImpersonationInfo
);
64 PspClearCrossThreadFlag(Thread
, CT_ACTIVE_IMPERSONATION_INFO_BIT
);
65 Thread
->ImpersonationInfo
= NULL
;
71 PspInitializeProcessSecurity(IN PEPROCESS Process
,
72 IN PEPROCESS Parent OPTIONAL
)
74 NTSTATUS Status
= STATUS_SUCCESS
;
75 PTOKEN NewToken
, ParentToken
;
77 PSTRACE(PS_SECURITY_DEBUG
, "Process: %p\n", Process
);
79 /* If we have a parent, then duplicate the Token */
82 /* Get the Parent Token */
83 ParentToken
= PsReferencePrimaryToken(Parent
);
86 Status
= SeSubProcessToken(ParentToken
,
89 MmGetSessionId(Process
));
91 /* Dereference the Parent */
92 ObFastDereferenceObject(&Parent
->Token
, ParentToken
);
94 /* Set the new Token */
95 if (NT_SUCCESS(Status
))
97 /* Initailize the fast reference */
98 ObInitializeFastReference(&Process
->Token
, NewToken
);
103 /* No parent, assign the Boot Token */
104 ObInitializeFastReference(&Process
->Token
, NULL
);
105 SeAssignPrimaryToken(Process
, PspBootAccessToken
);
108 /* Return to caller */
114 PspWriteTebImpersonationInfo(IN PETHREAD Thread
,
115 IN PETHREAD CurrentThread
)
119 BOOLEAN Attached
= FALSE
;
120 BOOLEAN IsImpersonating
;
123 PSTRACE(PS_SECURITY_DEBUG
, "Thread: %p\n", Thread
);
126 ASSERT(CurrentThread
== PsGetCurrentThread());
128 /* Get process and TEB */
129 Process
= Thread
->ThreadsProcess
;
130 Teb
= Thread
->Tcb
.Teb
;
133 /* Check if we're not in the right process */
134 if (Thread
->Tcb
.ApcState
.Process
!= &Process
->Pcb
)
136 /* Attach to the process */
137 KeStackAttachProcess(&Process
->Pcb
, &ApcState
);
141 /* Check if we're in a different thread or acquire rundown */
142 if ((Thread
== CurrentThread
) ||
143 (ExAcquireRundownProtection(&Thread
->RundownProtect
)))
145 /* Check if the thread is impersonating */
146 IsImpersonating
= (BOOLEAN
)Thread
->ActiveImpersonationInfo
;
150 Teb
->ImpersonationLocale
= -1;
151 Teb
->IsImpersonating
= 1;
156 Teb
->ImpersonationLocale
= 0;
157 Teb
->IsImpersonating
= 0;
161 /* Check if we're in a different thread */
162 if (Thread
!= CurrentThread
)
164 /* Release protection */
165 ExReleaseRundownProtection(&Thread
->RundownProtect
);
169 if (Attached
) KeUnstackDetachProcess(&ApcState
);
172 /* Return to caller */
173 return STATUS_SUCCESS
;
178 PspAssignPrimaryToken(IN PEPROCESS Process
,
180 IN PACCESS_TOKEN AccessToken OPTIONAL
)
182 PACCESS_TOKEN NewToken
= AccessToken
, OldToken
;
185 PSTRACE(PS_SECURITY_DEBUG
, "Process: %p Token: %p\n", Process
, Token
);
187 /* Check if we don't have a pointer */
190 /* Reference it from the handle */
191 Status
= ObReferenceObjectByHandle(Token
,
192 TOKEN_ASSIGN_PRIMARY
,
197 if (!NT_SUCCESS(Status
)) return Status
;
200 /* Exchange tokens */
201 Status
= SeExchangePrimaryToken(Process
, NewToken
, &OldToken
);
203 /* Acquire and release the lock */
204 PspLockProcessSecurityExclusive(Process
);
205 PspUnlockProcessSecurityExclusive(Process
);
207 /* Dereference Tokens and Return */
208 if (NT_SUCCESS(Status
)) ObDereferenceObject(OldToken
);
209 if (!AccessToken
) ObDereferenceObject(NewToken
);
215 PspSetPrimaryToken(IN PEPROCESS Process
,
216 IN HANDLE TokenHandle OPTIONAL
,
217 IN PACCESS_TOKEN Token OPTIONAL
)
219 KPROCESSOR_MODE PreviousMode
= ExGetPreviousMode();
220 BOOLEAN IsChildOrSibling
;
221 PACCESS_TOKEN NewToken
= Token
;
222 NTSTATUS Status
, AccessStatus
;
223 BOOLEAN Result
, SdAllocated
;
224 PSECURITY_DESCRIPTOR SecurityDescriptor
= NULL
;
225 SECURITY_SUBJECT_CONTEXT SubjectContext
;
227 PSTRACE(PS_SECURITY_DEBUG
, "Process: %p Token: %p\n", Process
, Token
);
229 /* Reference the token by handle if we don't already have a token object */
232 Status
= ObReferenceObjectByHandle(TokenHandle
,
233 TOKEN_ASSIGN_PRIMARY
,
238 if (!NT_SUCCESS(Status
)) return Status
;
242 * Check whether this token is a child or sibling of the current process token.
243 * NOTE: On Windows Vista+ both of these checks (together with extra steps)
244 * are now performed by a new SeIsTokenAssignableToProcess() helper.
246 Status
= SeIsTokenChild(NewToken
, &IsChildOrSibling
);
247 if (!NT_SUCCESS(Status
))
249 /* Failed, dereference */
250 if (!Token
) ObDereferenceObject(NewToken
);
253 if (!IsChildOrSibling
)
255 Status
= SeIsTokenSibling(NewToken
, &IsChildOrSibling
);
256 if (!NT_SUCCESS(Status
))
258 /* Failed, dereference */
259 if (!Token
) ObDereferenceObject(NewToken
);
264 /* Check if this was an independent token */
265 if (!IsChildOrSibling
)
267 /* Make sure we have the privilege to assign a new one */
268 if (!SeSinglePrivilegeCheck(SeAssignPrimaryTokenPrivilege
,
271 /* Failed, dereference */
272 if (!Token
) ObDereferenceObject(NewToken
);
273 return STATUS_PRIVILEGE_NOT_HELD
;
277 /* Assign the token */
278 Status
= PspAssignPrimaryToken(Process
, NULL
, NewToken
);
279 if (NT_SUCCESS(Status
))
282 * We need to completely reverify if the process still has access to
283 * itself under this new token.
285 Status
= ObGetObjectSecurity(Process
,
288 if (NT_SUCCESS(Status
))
290 /* Setup the security context */
291 SubjectContext
.ProcessAuditId
= Process
;
292 SubjectContext
.PrimaryToken
= PsReferencePrimaryToken(Process
);
293 SubjectContext
.ClientToken
= NULL
;
295 /* Do the access check */
296 Result
= SeAccessCheck(SecurityDescriptor
,
302 &PsProcessType
->TypeInfo
.GenericMapping
,
304 &Process
->GrantedAccess
,
307 /* Dereference the token and let go the SD */
308 ObFastDereferenceObject(&Process
->Token
,
309 SubjectContext
.PrimaryToken
);
310 ObReleaseObjectSecurity(SecurityDescriptor
, SdAllocated
);
312 /* Remove access if it failed */
313 if (!Result
) Process
->GrantedAccess
= 0;
315 /* Setup granted access */
316 Process
->GrantedAccess
|= (PROCESS_VM_OPERATION
|
319 PROCESS_QUERY_INFORMATION
|
321 PROCESS_CREATE_THREAD
|
323 PROCESS_CREATE_PROCESS
|
324 PROCESS_SET_INFORMATION
|
325 STANDARD_RIGHTS_ALL
|
330 /* Dereference the token */
331 if (!Token
) ObDereferenceObject(NewToken
);
335 /* FUNCTIONS *****************************************************************/
342 NtOpenProcessToken(IN HANDLE ProcessHandle
,
343 IN ACCESS_MASK DesiredAccess
,
344 OUT PHANDLE TokenHandle
)
346 /* Call the newer API */
347 return NtOpenProcessTokenEx(ProcessHandle
,
358 NtOpenProcessTokenEx(IN HANDLE ProcessHandle
,
359 IN ACCESS_MASK DesiredAccess
,
360 IN ULONG HandleAttributes
,
361 OUT PHANDLE TokenHandle
)
365 KPROCESSOR_MODE PreviousMode
= ExGetPreviousMode();
368 PSTRACE(PS_SECURITY_DEBUG
,
369 "Process: %p DesiredAccess: %lx\n", ProcessHandle
, DesiredAccess
);
371 /* Check if caller was user-mode */
372 if (PreviousMode
!= KernelMode
)
374 /* Enter SEH for probing */
377 /* Probe the token handle */
378 ProbeForWriteHandle(TokenHandle
);
380 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
382 /* Return the exception code */
383 _SEH2_YIELD(return _SEH2_GetExceptionCode());
388 /* Validate object attributes */
389 HandleAttributes
= ObpValidateAttributes(HandleAttributes
, PreviousMode
);
391 /* Open the process token */
392 Status
= PsOpenTokenOfProcess(ProcessHandle
, &Token
);
393 if (NT_SUCCESS(Status
))
395 /* Reference it by handle and dereference the pointer */
396 Status
= ObOpenObjectByPointer(Token
,
403 ObDereferenceObject(Token
);
405 /* Make sure we got a handle */
406 if (NT_SUCCESS(Status
))
408 /* Enter SEH for write */
411 /* Return the handle */
412 *TokenHandle
= hToken
;
414 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
416 /* Get exception code */
417 Status
= _SEH2_GetExceptionCode();
432 PsReferencePrimaryToken(PEPROCESS Process
)
436 PSTRACE(PS_SECURITY_DEBUG
, "Process: %p\n", Process
);
438 /* Fast Reference the Token */
439 Token
= ObFastReferenceObject(&Process
->Token
);
441 /* Check if we got the Token or if we got locked */
444 /* Lock the Process */
445 PspLockProcessSecurityShared(Process
);
447 /* Do a Locked Fast Reference */
448 Token
= ObFastReferenceObjectLocked(&Process
->Token
);
450 /* Unlock the Process */
451 PspUnlockProcessSecurityShared(Process
);
454 /* Return the Token */
463 PsOpenTokenOfProcess(IN HANDLE ProcessHandle
,
464 OUT PACCESS_TOKEN
* Token
)
469 PSTRACE(PS_SECURITY_DEBUG
, "Process: %p\n", ProcessHandle
);
472 Status
= ObReferenceObjectByHandle(ProcessHandle
,
473 PROCESS_QUERY_INFORMATION
,
478 if (NT_SUCCESS(Status
))
480 /* Reference the token and dereference the process */
481 *Token
= PsReferencePrimaryToken(Process
);
482 ObDereferenceObject(Process
);
494 PsAssignImpersonationToken(IN PETHREAD Thread
,
495 IN HANDLE TokenHandle
)
498 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
;
501 PSTRACE(PS_SECURITY_DEBUG
, "Thread: %p Token: %p\n", Thread
, TokenHandle
);
503 /* Check if we were given a handle */
506 /* Undo impersonation */
507 PsRevertThreadToSelf(Thread
);
508 return STATUS_SUCCESS
;
511 /* Get the token object */
512 Status
= ObReferenceObjectByHandle(TokenHandle
,
518 if (!NT_SUCCESS(Status
)) return(Status
);
520 /* Make sure it's an impersonation token */
521 if (SeTokenType(Token
) != TokenImpersonation
)
524 ObDereferenceObject(Token
);
525 return STATUS_BAD_TOKEN_TYPE
;
528 /* Get the impersonation level */
529 ImpersonationLevel
= SeTokenImpersonationLevel(Token
);
531 /* Call the impersonation API */
532 Status
= PsImpersonateClient(Thread
,
538 /* Dereference the token and return status */
539 ObDereferenceObject(Token
);
550 /* Call the per-thread API */
552 PsRevertThreadToSelf(PsGetCurrentThread());
560 PsRevertThreadToSelf(IN PETHREAD Thread
)
564 PSTRACE(PS_SECURITY_DEBUG
, "Thread: %p\n", Thread
);
566 /* Make sure we had impersonation information */
567 if (Thread
->ActiveImpersonationInfo
)
569 /* Lock the thread security */
570 PspLockThreadSecurityExclusive(Thread
);
572 /* Make sure it's still active */
573 if (Thread
->ActiveImpersonationInfo
)
575 /* Disable impersonation */
576 PspClearCrossThreadFlag(Thread
, CT_ACTIVE_IMPERSONATION_INFO_BIT
);
579 Token
= Thread
->ImpersonationInfo
->Token
;
582 /* Release thread security */
583 PspUnlockThreadSecurityExclusive(Thread
);
585 /* Check if we had a token */
588 /* Dereference the impersonation token */
589 ObDereferenceObject(Token
);
591 /* Write impersonation info to the TEB */
592 PspWriteTebImpersonationInfo(Thread
, PsGetCurrentThread());
602 PsImpersonateClient(IN PETHREAD Thread
,
603 IN PACCESS_TOKEN Token
,
604 IN BOOLEAN CopyOnOpen
,
605 IN BOOLEAN EffectiveOnly
,
606 IN SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
)
608 PPS_IMPERSONATION_INFORMATION Impersonation
, OldData
;
609 PTOKEN OldToken
= NULL
;
611 PSTRACE(PS_SECURITY_DEBUG
, "Thread: %p, Token: %p\n", Thread
, Token
);
613 /* Check if we don't have a token */
616 /* Make sure we're impersonating */
617 if (Thread
->ActiveImpersonationInfo
)
619 /* We seem to be, lock the thread */
620 PspLockThreadSecurityExclusive(Thread
);
622 /* Make sure we're still impersonating */
623 if (Thread
->ActiveImpersonationInfo
)
625 /* Disable impersonation */
626 PspClearCrossThreadFlag(Thread
,
627 CT_ACTIVE_IMPERSONATION_INFO_BIT
);
630 OldToken
= Thread
->ImpersonationInfo
->Token
;
633 /* Unlock the process and write TEB information */
634 PspUnlockThreadSecurityExclusive(Thread
);
635 PspWriteTebImpersonationInfo(Thread
, PsGetCurrentThread());
640 /* Check if we have impersonation info */
641 Impersonation
= Thread
->ImpersonationInfo
;
644 /* We need to allocate a new one */
645 Impersonation
= ExAllocatePoolWithTag(PagedPool
,
646 sizeof(*Impersonation
),
647 TAG_PS_IMPERSONATION
);
648 if (!Impersonation
) return STATUS_INSUFFICIENT_RESOURCES
;
650 /* Update the pointer */
651 OldData
= InterlockedCompareExchangePointer((PVOID
*)&Thread
->
657 /* Someone beat us to it, free our copy */
658 ExFreePoolWithTag(Impersonation
, TAG_PS_IMPERSONATION
);
659 Impersonation
= OldData
;
663 /* Check if this is a job, which we don't support yet */
664 if (Thread
->ThreadsProcess
->Job
) ASSERT(FALSE
);
666 /* Lock thread security */
667 PspLockThreadSecurityExclusive(Thread
);
669 /* Check if we're impersonating */
670 if (Thread
->ActiveImpersonationInfo
)
673 OldToken
= Impersonation
->Token
;
677 /* Otherwise, enable impersonation */
678 PspSetCrossThreadFlag(Thread
, CT_ACTIVE_IMPERSONATION_INFO_BIT
);
681 /* Now fill it out */
682 Impersonation
->ImpersonationLevel
= ImpersonationLevel
;
683 Impersonation
->CopyOnOpen
= CopyOnOpen
;
684 Impersonation
->EffectiveOnly
= EffectiveOnly
;
685 Impersonation
->Token
= Token
;
686 ObReferenceObject(Token
);
688 /* Unlock the thread */
689 PspUnlockThreadSecurityExclusive(Thread
);
691 /* Write impersonation info to the TEB */
692 PspWriteTebImpersonationInfo(Thread
, PsGetCurrentThread());
695 /* Dereference the token and return success */
696 if (OldToken
) PsDereferenceImpersonationToken(OldToken
);
697 return STATUS_SUCCESS
;
705 PsReferenceEffectiveToken(IN PETHREAD Thread
,
706 OUT IN PTOKEN_TYPE TokenType
,
707 OUT PBOOLEAN EffectiveOnly
,
708 OUT PSECURITY_IMPERSONATION_LEVEL ImpersonationLevel
)
711 PACCESS_TOKEN Token
= NULL
;
715 PSTRACE(PS_SECURITY_DEBUG
,
716 "Thread: %p, TokenType: %p\n", Thread
, TokenType
);
718 /* Check if we don't have impersonation info */
719 Process
= Thread
->ThreadsProcess
;
720 if (Thread
->ActiveImpersonationInfo
)
722 /* Lock the Process */
723 PspLockProcessSecurityShared(Process
);
725 /* Make sure impersonation is still active */
726 if (Thread
->ActiveImpersonationInfo
)
729 Token
= Thread
->ImpersonationInfo
->Token
;
730 ObReferenceObject(Token
);
732 /* Return data to caller */
733 *TokenType
= TokenImpersonation
;
734 *EffectiveOnly
= Thread
->ImpersonationInfo
->EffectiveOnly
;
735 *ImpersonationLevel
= Thread
->ImpersonationInfo
->ImpersonationLevel
;
737 /* Unlock the Process */
738 PspUnlockProcessSecurityShared(Process
);
742 /* Unlock the Process */
743 PspUnlockProcessSecurityShared(Process
);
746 /* Fast Reference the Token */
747 Token
= ObFastReferenceObject(&Process
->Token
);
749 /* Check if we got the Token or if we got locked */
752 /* Lock the Process */
753 PspLockProcessSecurityShared(Process
);
755 /* Do a Locked Fast Reference */
756 Token
= ObFastReferenceObjectLocked(&Process
->Token
);
758 /* Unlock the Process */
759 PspUnlockProcessSecurityShared(Process
);
762 /* Return the token */
763 *TokenType
= TokenPrimary
;
764 *EffectiveOnly
= FALSE
;
765 // NOTE: ImpersonationLevel is left untouched on purpose!
774 PsReferenceImpersonationToken(IN PETHREAD Thread
,
775 OUT PBOOLEAN CopyOnOpen
,
776 OUT PBOOLEAN EffectiveOnly
,
777 OUT PSECURITY_IMPERSONATION_LEVEL ImpersonationLevel
)
781 PSTRACE(PS_SECURITY_DEBUG
, "Thread: %p\n", Thread
);
783 /* If we don't have impersonation info, just quit */
784 if (!Thread
->ActiveImpersonationInfo
) return NULL
;
786 /* Lock the thread */
787 PspLockThreadSecurityShared(Thread
);
789 /* Make sure we still have active impersonation */
790 if (Thread
->ActiveImpersonationInfo
)
792 /* Return data from caller */
793 ObReferenceObject(Thread
->ImpersonationInfo
->Token
);
794 *ImpersonationLevel
= Thread
->ImpersonationInfo
->ImpersonationLevel
;
795 *CopyOnOpen
= Thread
->ImpersonationInfo
->CopyOnOpen
;
796 *EffectiveOnly
= Thread
->ImpersonationInfo
->EffectiveOnly
;
799 Token
= Thread
->ImpersonationInfo
->Token
;
802 /* Unlock thread and return impersonation token */
803 PspUnlockThreadSecurityShared(Thread
);
807 #undef PsDereferenceImpersonationToken
813 PsDereferenceImpersonationToken(IN PACCESS_TOKEN ImpersonationToken
)
817 /* If we got a token, dereference it */
818 if (ImpersonationToken
) ObDereferenceObject(ImpersonationToken
);
821 #undef PsDereferencePrimaryToken
827 PsDereferencePrimaryToken(IN PACCESS_TOKEN PrimaryToken
)
831 /* Dereference the token*/
832 ObDereferenceObject(PrimaryToken
);
840 PsDisableImpersonation(IN PETHREAD Thread
,
841 OUT PSE_IMPERSONATION_STATE ImpersonationState
)
843 PPS_IMPERSONATION_INFORMATION Impersonation
= NULL
;
846 PSTRACE(PS_SECURITY_DEBUG
,
847 "Thread: %p State: %p\n", Thread
, ImpersonationState
);
849 /* Check if we don't have impersonation */
850 if (Thread
->ActiveImpersonationInfo
)
852 /* Lock thread security */
853 PspLockThreadSecurityExclusive(Thread
);
855 /* Disable impersonation */
856 OldFlags
= PspClearCrossThreadFlag(Thread
,
857 CT_ACTIVE_IMPERSONATION_INFO_BIT
);
859 /* Make sure nobody disabled it behind our back */
860 if (OldFlags
& CT_ACTIVE_IMPERSONATION_INFO_BIT
)
862 /* Copy the old state */
863 Impersonation
= Thread
->ImpersonationInfo
;
864 ImpersonationState
->Token
= Impersonation
->Token
;
865 ImpersonationState
->CopyOnOpen
= Impersonation
->CopyOnOpen
;
866 ImpersonationState
->EffectiveOnly
= Impersonation
->EffectiveOnly
;
867 ImpersonationState
->Level
= Impersonation
->ImpersonationLevel
;
870 /* Unlock thread security */
871 PspUnlockThreadSecurityExclusive(Thread
);
873 /* If we had impersonation info, return true */
874 if (Impersonation
) return TRUE
;
877 /* Clear everything */
878 ImpersonationState
->Token
= NULL
;
879 ImpersonationState
->CopyOnOpen
= FALSE
;
880 ImpersonationState
->EffectiveOnly
= FALSE
;
881 ImpersonationState
->Level
= SecurityAnonymous
;
890 PsRestoreImpersonation(IN PETHREAD Thread
,
891 IN PSE_IMPERSONATION_STATE ImpersonationState
)
894 PPS_IMPERSONATION_INFORMATION Impersonation
;
896 PSTRACE(PS_SECURITY_DEBUG
,
897 "Thread: %p State: %p\n", Thread
, ImpersonationState
);
899 /* Lock thread security */
900 PspLockThreadSecurityExclusive(Thread
);
902 /* Get the impersonation info */
903 Impersonation
= Thread
->ImpersonationInfo
;
905 /* Check if we're impersonating */
906 if (Thread
->ActiveImpersonationInfo
)
909 Token
= Impersonation
->Token
;
912 /* Check if we have an impersonation state */
913 if (ImpersonationState
)
915 /* Fill out the impersonation info */
916 Impersonation
->ImpersonationLevel
= ImpersonationState
->Level
;
917 Impersonation
->CopyOnOpen
= ImpersonationState
->CopyOnOpen
;
918 Impersonation
->EffectiveOnly
= ImpersonationState
->EffectiveOnly
;
919 Impersonation
->Token
= ImpersonationState
->Token
;
921 /* Enable impersonation */
922 PspSetCrossThreadFlag(Thread
, CT_ACTIVE_IMPERSONATION_INFO_BIT
);
926 /* Disable impersonation */
927 PspClearCrossThreadFlag(Thread
, CT_ACTIVE_IMPERSONATION_INFO_BIT
);
930 /* Unlock the thread */
931 PspUnlockThreadSecurityExclusive(Thread
);
933 /* Dereference the token */
934 if (Token
) ObDereferenceObject(Token
);
939 NtImpersonateThread(IN HANDLE ThreadHandle
,
940 IN HANDLE ThreadToImpersonateHandle
,
941 IN PSECURITY_QUALITY_OF_SERVICE SecurityQualityOfService
)
943 SECURITY_QUALITY_OF_SERVICE SafeServiceQoS
;
944 SECURITY_CLIENT_CONTEXT ClientContext
;
946 PETHREAD ThreadToImpersonate
;
947 KPROCESSOR_MODE PreviousMode
= ExGetPreviousMode();
950 PSTRACE(PS_SECURITY_DEBUG
,
951 "Threads: %p %p\n", ThreadHandle
, ThreadToImpersonateHandle
);
953 /* Check if call came from user mode */
954 if (PreviousMode
!= KernelMode
)
956 /* Enter SEH for probing */
960 ProbeForRead(SecurityQualityOfService
,
961 sizeof(SECURITY_QUALITY_OF_SERVICE
),
965 SafeServiceQoS
= *SecurityQualityOfService
;
966 SecurityQualityOfService
= &SafeServiceQoS
;
968 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
970 /* Return the exception code */
971 _SEH2_YIELD(return _SEH2_GetExceptionCode());
976 /* Reference the thread */
977 Status
= ObReferenceObjectByHandle(ThreadHandle
,
978 THREAD_DIRECT_IMPERSONATION
,
983 if (NT_SUCCESS(Status
))
985 /* Reference the impersonating thead */
986 Status
= ObReferenceObjectByHandle(ThreadToImpersonateHandle
,
990 (PVOID
*)&ThreadToImpersonate
,
992 if (NT_SUCCESS(Status
))
994 /* Create a client security context */
995 Status
= SeCreateClientSecurity(ThreadToImpersonate
,
996 SecurityQualityOfService
,
999 if (NT_SUCCESS(Status
))
1001 /* Do the impersonation */
1002 SeImpersonateClient(&ClientContext
, Thread
);
1003 if (ClientContext
.ClientToken
)
1005 /* Dereference the client token if we had one */
1006 ObDereferenceObject(ClientContext
.ClientToken
);
1010 /* Dereference the thread to impersonate */
1011 ObDereferenceObject(ThreadToImpersonate
);
1014 /* Dereference the main thread */
1015 ObDereferenceObject(Thread
);