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();
221 PACCESS_TOKEN NewToken
= Token
;
222 NTSTATUS Status
, AccessStatus
;
223 BOOLEAN Result
, SdAllocated
;
224 PSECURITY_DESCRIPTOR SecurityDescriptor
= NULL
;
225 SECURITY_SUBJECT_CONTEXT SubjectContext
;
226 PSTRACE(PS_SECURITY_DEBUG
, "Process: %p Token: %p\n", Process
, Token
);
228 /* Make sure we got a handle */
232 Status
= ObReferenceObjectByHandle(TokenHandle
,
233 TOKEN_ASSIGN_PRIMARY
,
238 if (!NT_SUCCESS(Status
)) return Status
;
241 /* Check if this is a child */
242 Status
= SeIsTokenChild(NewToken
, &IsChild
);
243 if (!NT_SUCCESS(Status
))
245 /* Failed, dereference */
246 if (TokenHandle
) ObDereferenceObject(NewToken
);
250 /* Check if this was an independent token */
253 /* Make sure we have the privilege to assign a new one */
254 if (!SeSinglePrivilegeCheck(SeAssignPrimaryTokenPrivilege
,
257 /* Failed, dereference */
258 if (TokenHandle
) ObDereferenceObject(NewToken
);
259 return STATUS_PRIVILEGE_NOT_HELD
;
263 /* Assign the token */
264 Status
= PspAssignPrimaryToken(Process
, NULL
, NewToken
);
265 if (NT_SUCCESS(Status
))
268 * We need to completely reverify if the process still has access to
269 * itself under this new token.
271 Status
= ObGetObjectSecurity(Process
,
274 if (NT_SUCCESS(Status
))
276 /* Setup the security context */
277 SubjectContext
.ProcessAuditId
= Process
;
278 SubjectContext
.PrimaryToken
= PsReferencePrimaryToken(Process
);
279 SubjectContext
.ClientToken
= NULL
;
281 /* Do the access check */
282 Result
= SeAccessCheck(SecurityDescriptor
,
288 &PsProcessType
->TypeInfo
.GenericMapping
,
290 &Process
->GrantedAccess
,
293 /* Dereference the token and let go the SD */
294 ObFastDereferenceObject(&Process
->Token
,
295 SubjectContext
.PrimaryToken
);
296 ObReleaseObjectSecurity(SecurityDescriptor
, SdAllocated
);
298 /* Remove access if it failed */
299 if (!Result
) Process
->GrantedAccess
= 0;
301 /* Setup granted access */
302 Process
->GrantedAccess
|= (PROCESS_VM_OPERATION
|
305 PROCESS_QUERY_INFORMATION
|
307 PROCESS_CREATE_THREAD
|
309 PROCESS_CREATE_PROCESS
|
310 PROCESS_SET_INFORMATION
|
311 STANDARD_RIGHTS_ALL
|
316 /* Dereference the token */
317 if (TokenHandle
) ObDereferenceObject(NewToken
);
321 /* FUNCTIONS *****************************************************************/
328 NtOpenProcessToken(IN HANDLE ProcessHandle
,
329 IN ACCESS_MASK DesiredAccess
,
330 OUT PHANDLE TokenHandle
)
332 /* Call the newer API */
333 return NtOpenProcessTokenEx(ProcessHandle
,
344 NtOpenProcessTokenEx(IN HANDLE ProcessHandle
,
345 IN ACCESS_MASK DesiredAccess
,
346 IN ULONG HandleAttributes
,
347 OUT PHANDLE TokenHandle
)
351 KPROCESSOR_MODE PreviousMode
= ExGetPreviousMode();
354 PSTRACE(PS_SECURITY_DEBUG
,
355 "Process: %p DesiredAccess: %lx\n", ProcessHandle
, DesiredAccess
);
357 /* Check if caller was user-mode */
358 if (PreviousMode
!= KernelMode
)
360 /* Enter SEH for probing */
363 /* Probe the token handle */
364 ProbeForWriteHandle(TokenHandle
);
366 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
368 /* Return the exception code */
369 _SEH2_YIELD(return _SEH2_GetExceptionCode());
374 /* Validate object attributes */
375 HandleAttributes
= ObpValidateAttributes(HandleAttributes
, PreviousMode
);
377 /* Open the process token */
378 Status
= PsOpenTokenOfProcess(ProcessHandle
, &Token
);
379 if (NT_SUCCESS(Status
))
381 /* Reference it by handle and dereference the pointer */
382 Status
= ObOpenObjectByPointer(Token
,
389 ObDereferenceObject(Token
);
391 /* Make sure we got a handle */
392 if (NT_SUCCESS(Status
))
394 /* Enter SEH for write */
397 /* Return the handle */
398 *TokenHandle
= hToken
;
400 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
402 /* Get exception code */
403 Status
= _SEH2_GetExceptionCode();
418 PsReferencePrimaryToken(PEPROCESS Process
)
422 PSTRACE(PS_SECURITY_DEBUG
, "Process: %p\n", Process
);
424 /* Fast Reference the Token */
425 Token
= ObFastReferenceObject(&Process
->Token
);
427 /* Check if we got the Token or if we got locked */
430 /* Lock the Process */
431 PspLockProcessSecurityShared(Process
);
433 /* Do a Locked Fast Reference */
434 Token
= ObFastReferenceObjectLocked(&Process
->Token
);
436 /* Unlock the Process */
437 PspUnlockProcessSecurityShared(Process
);
440 /* Return the Token */
449 PsOpenTokenOfProcess(IN HANDLE ProcessHandle
,
450 OUT PACCESS_TOKEN
* Token
)
455 PSTRACE(PS_SECURITY_DEBUG
, "Process: %p\n", ProcessHandle
);
458 Status
= ObReferenceObjectByHandle(ProcessHandle
,
459 PROCESS_QUERY_INFORMATION
,
464 if (NT_SUCCESS(Status
))
466 /* Reference the token and dereference the process */
467 *Token
= PsReferencePrimaryToken(Process
);
468 ObDereferenceObject(Process
);
480 PsAssignImpersonationToken(IN PETHREAD Thread
,
481 IN HANDLE TokenHandle
)
484 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
;
487 PSTRACE(PS_SECURITY_DEBUG
, "Thread: %p Token: %p\n", Thread
, TokenHandle
);
489 /* Check if we were given a handle */
492 /* Undo impersonation */
493 PsRevertThreadToSelf(Thread
);
494 return STATUS_SUCCESS
;
497 /* Get the token object */
498 Status
= ObReferenceObjectByHandle(TokenHandle
,
504 if (!NT_SUCCESS(Status
)) return(Status
);
506 /* Make sure it's an impersonation token */
507 if (SeTokenType(Token
) != TokenImpersonation
)
510 ObDereferenceObject(Token
);
511 return STATUS_BAD_TOKEN_TYPE
;
514 /* Get the impersonation level */
515 ImpersonationLevel
= SeTokenImpersonationLevel(Token
);
517 /* Call the impersonation API */
518 Status
= PsImpersonateClient(Thread
,
524 /* Dereference the token and return status */
525 ObDereferenceObject(Token
);
536 /* Call the per-thread API */
538 PsRevertThreadToSelf(PsGetCurrentThread());
546 PsRevertThreadToSelf(IN PETHREAD Thread
)
550 PSTRACE(PS_SECURITY_DEBUG
, "Thread: %p\n", Thread
);
552 /* Make sure we had impersonation information */
553 if (Thread
->ActiveImpersonationInfo
)
555 /* Lock the thread security */
556 PspLockThreadSecurityExclusive(Thread
);
558 /* Make sure it's still active */
559 if (Thread
->ActiveImpersonationInfo
)
561 /* Disable impersonation */
562 PspClearCrossThreadFlag(Thread
, CT_ACTIVE_IMPERSONATION_INFO_BIT
);
565 Token
= Thread
->ImpersonationInfo
->Token
;
568 /* Release thread security */
569 PspUnlockThreadSecurityExclusive(Thread
);
571 /* Check if we had a token */
574 /* Dereference the impersonation token */
575 ObDereferenceObject(Token
);
577 /* Write impersonation info to the TEB */
578 PspWriteTebImpersonationInfo(Thread
, PsGetCurrentThread());
588 PsImpersonateClient(IN PETHREAD Thread
,
589 IN PACCESS_TOKEN Token
,
590 IN BOOLEAN CopyOnOpen
,
591 IN BOOLEAN EffectiveOnly
,
592 IN SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
)
594 PPS_IMPERSONATION_INFORMATION Impersonation
, OldData
;
595 PTOKEN OldToken
= NULL
;
597 PSTRACE(PS_SECURITY_DEBUG
, "Thread: %p, Token: %p\n", Thread
, Token
);
599 /* Check if we don't have a token */
602 /* Make sure we're impersonating */
603 if (Thread
->ActiveImpersonationInfo
)
605 /* We seem to be, lock the thread */
606 PspLockThreadSecurityExclusive(Thread
);
608 /* Make sure we're still impersonating */
609 if (Thread
->ActiveImpersonationInfo
)
611 /* Disable impersonation */
612 PspClearCrossThreadFlag(Thread
,
613 CT_ACTIVE_IMPERSONATION_INFO_BIT
);
616 OldToken
= Thread
->ImpersonationInfo
->Token
;
619 /* Unlock the process and write TEB information */
620 PspUnlockThreadSecurityExclusive(Thread
);
621 PspWriteTebImpersonationInfo(Thread
, PsGetCurrentThread());
626 /* Check if we have impersonation info */
627 Impersonation
= Thread
->ImpersonationInfo
;
630 /* We need to allocate a new one */
631 Impersonation
= ExAllocatePoolWithTag(PagedPool
,
632 sizeof(*Impersonation
),
633 TAG_PS_IMPERSONATION
);
634 if (!Impersonation
) return STATUS_INSUFFICIENT_RESOURCES
;
636 /* Update the pointer */
637 OldData
= InterlockedCompareExchangePointer((PVOID
*)&Thread
->
643 /* Someone beat us to it, free our copy */
644 ExFreePoolWithTag(Impersonation
, TAG_PS_IMPERSONATION
);
645 Impersonation
= OldData
;
649 /* Check if this is a job, which we don't support yet */
650 if (Thread
->ThreadsProcess
->Job
) ASSERT(FALSE
);
652 /* Lock thread security */
653 PspLockThreadSecurityExclusive(Thread
);
655 /* Check if we're impersonating */
656 if (Thread
->ActiveImpersonationInfo
)
659 OldToken
= Impersonation
->Token
;
663 /* Otherwise, enable impersonation */
664 PspSetCrossThreadFlag(Thread
, CT_ACTIVE_IMPERSONATION_INFO_BIT
);
667 /* Now fill it out */
668 Impersonation
->ImpersonationLevel
= ImpersonationLevel
;
669 Impersonation
->CopyOnOpen
= CopyOnOpen
;
670 Impersonation
->EffectiveOnly
= EffectiveOnly
;
671 Impersonation
->Token
= Token
;
672 ObReferenceObject(Token
);
674 /* Unlock the thread */
675 PspUnlockThreadSecurityExclusive(Thread
);
677 /* Write impersonation info to the TEB */
678 PspWriteTebImpersonationInfo(Thread
, PsGetCurrentThread());
681 /* Dereference the token and return success */
682 if (OldToken
) PsDereferenceImpersonationToken(OldToken
);
683 return STATUS_SUCCESS
;
691 PsReferenceEffectiveToken(IN PETHREAD Thread
,
692 OUT IN PTOKEN_TYPE TokenType
,
693 OUT PBOOLEAN EffectiveOnly
,
694 OUT PSECURITY_IMPERSONATION_LEVEL Level
)
697 PACCESS_TOKEN Token
= NULL
;
699 PSTRACE(PS_SECURITY_DEBUG
,
700 "Thread: %p, TokenType: %p\n", Thread
, TokenType
);
702 /* Check if we don't have impersonation info */
703 Process
= Thread
->ThreadsProcess
;
704 if (Thread
->ActiveImpersonationInfo
)
706 /* Lock the Process */
707 PspLockProcessSecurityShared(Process
);
709 /* Make sure impersonation is still active */
710 if (Thread
->ActiveImpersonationInfo
)
713 Token
= Thread
->ImpersonationInfo
->Token
;
714 ObReferenceObject(Token
);
716 /* Return data to caller */
717 *TokenType
= TokenImpersonation
;
718 *EffectiveOnly
= Thread
->ImpersonationInfo
->EffectiveOnly
;
719 *Level
= Thread
->ImpersonationInfo
->ImpersonationLevel
;
721 /* Unlock the Process */
722 PspUnlockProcessSecurityShared(Process
);
726 /* Unlock the Process */
727 PspUnlockProcessSecurityShared(Process
);
730 /* Fast Reference the Token */
731 Token
= ObFastReferenceObject(&Process
->Token
);
733 /* Check if we got the Token or if we got locked */
736 /* Lock the Process */
737 PspLockProcessSecurityShared(Process
);
739 /* Do a Locked Fast Reference */
740 Token
= ObFastReferenceObjectLocked(&Process
->Token
);
742 /* Unlock the Process */
743 PspUnlockProcessSecurityShared(Process
);
746 /* Return the token */
747 *TokenType
= TokenPrimary
;
748 *EffectiveOnly
= FALSE
;
757 PsReferenceImpersonationToken(IN PETHREAD Thread
,
758 OUT PBOOLEAN CopyOnOpen
,
759 OUT PBOOLEAN EffectiveOnly
,
760 OUT PSECURITY_IMPERSONATION_LEVEL ImpersonationLevel
)
764 PSTRACE(PS_SECURITY_DEBUG
, "Thread: %p\n", Thread
);
766 /* If we don't have impersonation info, just quit */
767 if (!Thread
->ActiveImpersonationInfo
) return NULL
;
769 /* Lock the thread */
770 PspLockThreadSecurityShared(Thread
);
772 /* Make sure we still have active impersonation */
773 if (Thread
->ActiveImpersonationInfo
)
775 /* Return data from caller */
776 ObReferenceObject(Thread
->ImpersonationInfo
->Token
);
777 *ImpersonationLevel
= Thread
->ImpersonationInfo
->ImpersonationLevel
;
778 *CopyOnOpen
= Thread
->ImpersonationInfo
->CopyOnOpen
;
779 *EffectiveOnly
= Thread
->ImpersonationInfo
->EffectiveOnly
;
782 Token
= Thread
->ImpersonationInfo
->Token
;
785 /* Unlock thread and return impersonation token */
786 PspUnlockThreadSecurityShared(Thread
);
790 #undef PsDereferenceImpersonationToken
796 PsDereferenceImpersonationToken(IN PACCESS_TOKEN ImpersonationToken
)
800 /* If we got a token, dereference it */
801 if (ImpersonationToken
) ObDereferenceObject(ImpersonationToken
);
804 #undef PsDereferencePrimaryToken
810 PsDereferencePrimaryToken(IN PACCESS_TOKEN PrimaryToken
)
814 /* Dereference the token*/
815 ObDereferenceObject(PrimaryToken
);
823 PsDisableImpersonation(IN PETHREAD Thread
,
824 OUT PSE_IMPERSONATION_STATE ImpersonationState
)
826 PPS_IMPERSONATION_INFORMATION Impersonation
= NULL
;
829 PSTRACE(PS_SECURITY_DEBUG
,
830 "Thread: %p State: %p\n", Thread
, ImpersonationState
);
832 /* Check if we don't have impersonation */
833 if (Thread
->ActiveImpersonationInfo
)
835 /* Lock thread security */
836 PspLockThreadSecurityExclusive(Thread
);
838 /* Disable impersonation */
839 OldFlags
= PspClearCrossThreadFlag(Thread
,
840 CT_ACTIVE_IMPERSONATION_INFO_BIT
);
842 /* Make sure nobody disabled it behind our back */
843 if (OldFlags
& CT_ACTIVE_IMPERSONATION_INFO_BIT
)
845 /* Copy the old state */
846 Impersonation
= Thread
->ImpersonationInfo
;
847 ImpersonationState
->Token
= Impersonation
->Token
;
848 ImpersonationState
->CopyOnOpen
= Impersonation
->CopyOnOpen
;
849 ImpersonationState
->EffectiveOnly
= Impersonation
->EffectiveOnly
;
850 ImpersonationState
->Level
= Impersonation
->ImpersonationLevel
;
853 /* Unlock thread security */
854 PspUnlockThreadSecurityExclusive(Thread
);
856 /* If we had impersonation info, return true */
857 if (Impersonation
) return TRUE
;
860 /* Clear everything */
861 ImpersonationState
->Token
= NULL
;
862 ImpersonationState
->CopyOnOpen
= FALSE
;
863 ImpersonationState
->EffectiveOnly
= FALSE
;
864 ImpersonationState
->Level
= SecurityAnonymous
;
873 PsRestoreImpersonation(IN PETHREAD Thread
,
874 IN PSE_IMPERSONATION_STATE ImpersonationState
)
877 PPS_IMPERSONATION_INFORMATION Impersonation
;
879 PSTRACE(PS_SECURITY_DEBUG
,
880 "Thread: %p State: %p\n", Thread
, ImpersonationState
);
882 /* Lock thread security */
883 PspLockThreadSecurityExclusive(Thread
);
885 /* Get the impersonation info */
886 Impersonation
= Thread
->ImpersonationInfo
;
888 /* Check if we're impersonating */
889 if (Thread
->ActiveImpersonationInfo
)
892 Token
= Impersonation
->Token
;
895 /* Check if we have an impersonation state */
896 if (ImpersonationState
)
898 /* Fill out the impersonation info */
899 Impersonation
->ImpersonationLevel
= ImpersonationState
->Level
;
900 Impersonation
->CopyOnOpen
= ImpersonationState
->CopyOnOpen
;
901 Impersonation
->EffectiveOnly
= ImpersonationState
->EffectiveOnly
;
902 Impersonation
->Token
= ImpersonationState
->Token
;
904 /* Enable impersonation */
905 PspSetCrossThreadFlag(Thread
, CT_ACTIVE_IMPERSONATION_INFO_BIT
);
909 /* Disable impersonation */
910 PspClearCrossThreadFlag(Thread
, CT_ACTIVE_IMPERSONATION_INFO_BIT
);
913 /* Unlock the thread */
914 PspUnlockThreadSecurityExclusive(Thread
);
916 /* Dereference the token */
917 if (Token
) ObDereferenceObject(Token
);
922 NtImpersonateThread(IN HANDLE ThreadHandle
,
923 IN HANDLE ThreadToImpersonateHandle
,
924 IN PSECURITY_QUALITY_OF_SERVICE SecurityQualityOfService
)
926 SECURITY_QUALITY_OF_SERVICE SafeServiceQoS
;
927 SECURITY_CLIENT_CONTEXT ClientContext
;
929 PETHREAD ThreadToImpersonate
;
930 KPROCESSOR_MODE PreviousMode
= ExGetPreviousMode();
933 PSTRACE(PS_SECURITY_DEBUG
,
934 "Threads: %p %p\n", ThreadHandle
, ThreadToImpersonateHandle
);
936 /* Check if call came from user mode */
937 if (PreviousMode
!= KernelMode
)
939 /* Enter SEH for probing */
943 ProbeForRead(SecurityQualityOfService
,
944 sizeof(SECURITY_QUALITY_OF_SERVICE
),
948 SafeServiceQoS
= *SecurityQualityOfService
;
949 SecurityQualityOfService
= &SafeServiceQoS
;
951 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
953 /* Return the exception code */
954 _SEH2_YIELD(return _SEH2_GetExceptionCode());
959 /* Reference the thread */
960 Status
= ObReferenceObjectByHandle(ThreadHandle
,
961 THREAD_DIRECT_IMPERSONATION
,
966 if (NT_SUCCESS(Status
))
968 /* Reference the impersonating thead */
969 Status
= ObReferenceObjectByHandle(ThreadToImpersonateHandle
,
973 (PVOID
*)&ThreadToImpersonate
,
975 if (NT_SUCCESS(Status
))
977 /* Create a client security context */
978 Status
= SeCreateClientSecurity(ThreadToImpersonate
,
979 SecurityQualityOfService
,
982 if (NT_SUCCESS(Status
))
984 /* Do the impersonation */
985 SeImpersonateClient(&ClientContext
, Thread
);
986 if (ClientContext
.ClientToken
)
988 /* Dereference the client token if we had one */
989 ObDereferenceObject(ClientContext
.ClientToken
);
993 /* Dereference the thread to impersonate */
994 ObDereferenceObject(ThreadToImpersonate
);
997 /* Dereference the main thread */
998 ObDereferenceObject(Thread
);