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 /* Open the process token */
375 Status
= PsOpenTokenOfProcess(ProcessHandle
, &Token
);
376 if (NT_SUCCESS(Status
))
378 /* Reference it by handle and dereference the pointer */
379 Status
= ObOpenObjectByPointer(Token
,
386 ObDereferenceObject(Token
);
388 /* Make sure we got a handle */
389 if (NT_SUCCESS(Status
))
391 /* Enter SEH for write */
394 /* Return the handle */
395 *TokenHandle
= hToken
;
397 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
399 /* Get exception code */
400 Status
= _SEH2_GetExceptionCode();
415 PsReferencePrimaryToken(PEPROCESS Process
)
419 PSTRACE(PS_SECURITY_DEBUG
, "Process: %p\n", Process
);
421 /* Fast Reference the Token */
422 Token
= ObFastReferenceObject(&Process
->Token
);
424 /* Check if we got the Token or if we got locked */
427 /* Lock the Process */
428 PspLockProcessSecurityShared(Process
);
430 /* Do a Locked Fast Reference */
431 Token
= ObFastReferenceObjectLocked(&Process
->Token
);
433 /* Unlock the Process */
434 PspUnlockProcessSecurityShared(Process
);
437 /* Return the Token */
446 PsOpenTokenOfProcess(IN HANDLE ProcessHandle
,
447 OUT PACCESS_TOKEN
* Token
)
452 PSTRACE(PS_SECURITY_DEBUG
, "Process: %p\n", ProcessHandle
);
455 Status
= ObReferenceObjectByHandle(ProcessHandle
,
456 PROCESS_QUERY_INFORMATION
,
461 if (NT_SUCCESS(Status
))
463 /* Reference the token and dereference the process */
464 *Token
= PsReferencePrimaryToken(Process
);
465 ObDereferenceObject(Process
);
477 PsAssignImpersonationToken(IN PETHREAD Thread
,
478 IN HANDLE TokenHandle
)
481 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
;
484 PSTRACE(PS_SECURITY_DEBUG
, "Thread: %p Token: %p\n", Thread
, TokenHandle
);
486 /* Check if we were given a handle */
489 /* Undo impersonation */
490 PsRevertThreadToSelf(Thread
);
491 return STATUS_SUCCESS
;
494 /* Get the token object */
495 Status
= ObReferenceObjectByHandle(TokenHandle
,
501 if (!NT_SUCCESS(Status
)) return(Status
);
503 /* Make sure it's an impersonation token */
504 if (SeTokenType(Token
) != TokenImpersonation
)
507 ObDereferenceObject(Token
);
508 return STATUS_BAD_TOKEN_TYPE
;
511 /* Get the impersonation level */
512 ImpersonationLevel
= SeTokenImpersonationLevel(Token
);
514 /* Call the impersonation API */
515 Status
= PsImpersonateClient(Thread
,
521 /* Dereference the token and return status */
522 ObDereferenceObject(Token
);
533 /* Call the per-thread API */
535 PsRevertThreadToSelf(PsGetCurrentThread());
543 PsRevertThreadToSelf(IN PETHREAD Thread
)
547 PSTRACE(PS_SECURITY_DEBUG
, "Thread: %p\n", Thread
);
549 /* Make sure we had impersonation information */
550 if (Thread
->ActiveImpersonationInfo
)
552 /* Lock the thread security */
553 PspLockThreadSecurityExclusive(Thread
);
555 /* Make sure it's still active */
556 if (Thread
->ActiveImpersonationInfo
)
558 /* Disable impersonation */
559 PspClearCrossThreadFlag(Thread
, CT_ACTIVE_IMPERSONATION_INFO_BIT
);
562 Token
= Thread
->ImpersonationInfo
->Token
;
565 /* Release thread security */
566 PspUnlockThreadSecurityExclusive(Thread
);
568 /* Check if we had a token */
571 /* Dereference the impersonation token */
572 ObDereferenceObject(Token
);
574 /* Write impersonation info to the TEB */
575 PspWriteTebImpersonationInfo(Thread
, PsGetCurrentThread());
585 PsImpersonateClient(IN PETHREAD Thread
,
586 IN PACCESS_TOKEN Token
,
587 IN BOOLEAN CopyOnOpen
,
588 IN BOOLEAN EffectiveOnly
,
589 IN SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
)
591 PPS_IMPERSONATION_INFORMATION Impersonation
, OldData
;
592 PTOKEN OldToken
= NULL
;
594 PSTRACE(PS_SECURITY_DEBUG
, "Thread: %p, Token: %p\n", Thread
, Token
);
596 /* Check if we don't have a token */
599 /* Make sure we're impersonating */
600 if (Thread
->ActiveImpersonationInfo
)
602 /* We seem to be, lock the thread */
603 PspLockThreadSecurityExclusive(Thread
);
605 /* Make sure we're still impersonating */
606 if (Thread
->ActiveImpersonationInfo
)
608 /* Disable impersonation */
609 PspClearCrossThreadFlag(Thread
,
610 CT_ACTIVE_IMPERSONATION_INFO_BIT
);
613 OldToken
= Thread
->ImpersonationInfo
->Token
;
616 /* Unlock the process and write TEB information */
617 PspUnlockThreadSecurityExclusive(Thread
);
618 PspWriteTebImpersonationInfo(Thread
, PsGetCurrentThread());
623 /* Check if we have impersonation info */
624 Impersonation
= Thread
->ImpersonationInfo
;
627 /* We need to allocate a new one */
628 Impersonation
= ExAllocatePoolWithTag(PagedPool
,
629 sizeof(*Impersonation
),
630 TAG_PS_IMPERSONATION
);
631 if (!Impersonation
) return STATUS_INSUFFICIENT_RESOURCES
;
633 /* Update the pointer */
634 OldData
= InterlockedCompareExchangePointer((PVOID
*)&Thread
->
640 /* Someone beat us to it, free our copy */
641 ExFreePoolWithTag(Impersonation
, TAG_PS_IMPERSONATION
);
642 Impersonation
= OldData
;
646 /* Check if this is a job, which we don't support yet */
647 if (Thread
->ThreadsProcess
->Job
) ASSERT(FALSE
);
649 /* Lock thread security */
650 PspLockThreadSecurityExclusive(Thread
);
652 /* Check if we're impersonating */
653 if (Thread
->ActiveImpersonationInfo
)
656 OldToken
= Impersonation
->Token
;
660 /* Otherwise, enable impersonation */
661 PspSetCrossThreadFlag(Thread
, CT_ACTIVE_IMPERSONATION_INFO_BIT
);
664 /* Now fill it out */
665 Impersonation
->ImpersonationLevel
= ImpersonationLevel
;
666 Impersonation
->CopyOnOpen
= CopyOnOpen
;
667 Impersonation
->EffectiveOnly
= EffectiveOnly
;
668 Impersonation
->Token
= Token
;
669 ObReferenceObject(Token
);
671 /* Unlock the thread */
672 PspUnlockThreadSecurityExclusive(Thread
);
674 /* Write impersonation info to the TEB */
675 PspWriteTebImpersonationInfo(Thread
, PsGetCurrentThread());
678 /* Dereference the token and return success */
679 if (OldToken
) PsDereferenceImpersonationToken(OldToken
);
680 return STATUS_SUCCESS
;
688 PsReferenceEffectiveToken(IN PETHREAD Thread
,
689 OUT IN PTOKEN_TYPE TokenType
,
690 OUT PBOOLEAN EffectiveOnly
,
691 OUT PSECURITY_IMPERSONATION_LEVEL Level
)
694 PACCESS_TOKEN Token
= NULL
;
696 PSTRACE(PS_SECURITY_DEBUG
,
697 "Thread: %p, TokenType: %p\n", Thread
, TokenType
);
699 /* Check if we don't have impersonation info */
700 Process
= Thread
->ThreadsProcess
;
701 if (Thread
->ActiveImpersonationInfo
)
703 /* Lock the Process */
704 PspLockProcessSecurityShared(Process
);
706 /* Make sure impersonation is still active */
707 if (Thread
->ActiveImpersonationInfo
)
710 Token
= Thread
->ImpersonationInfo
->Token
;
711 ObReferenceObject(Token
);
713 /* Return data to caller */
714 *TokenType
= TokenImpersonation
;
715 *EffectiveOnly
= Thread
->ImpersonationInfo
->EffectiveOnly
;
716 *Level
= Thread
->ImpersonationInfo
->ImpersonationLevel
;
718 /* Unlock the Process */
719 PspUnlockProcessSecurityShared(Process
);
723 /* Unlock the Process */
724 PspUnlockProcessSecurityShared(Process
);
727 /* Fast Reference the Token */
728 Token
= ObFastReferenceObject(&Process
->Token
);
730 /* Check if we got the Token or if we got locked */
733 /* Lock the Process */
734 PspLockProcessSecurityShared(Process
);
736 /* Do a Locked Fast Reference */
737 Token
= ObFastReferenceObjectLocked(&Process
->Token
);
739 /* Unlock the Process */
740 PspUnlockProcessSecurityShared(Process
);
743 /* Return the token */
744 *TokenType
= TokenPrimary
;
745 *EffectiveOnly
= FALSE
;
754 PsReferenceImpersonationToken(IN PETHREAD Thread
,
755 OUT PBOOLEAN CopyOnOpen
,
756 OUT PBOOLEAN EffectiveOnly
,
757 OUT PSECURITY_IMPERSONATION_LEVEL ImpersonationLevel
)
761 PSTRACE(PS_SECURITY_DEBUG
, "Thread: %p\n", Thread
);
763 /* If we don't have impersonation info, just quit */
764 if (!Thread
->ActiveImpersonationInfo
) return NULL
;
766 /* Lock the thread */
767 PspLockThreadSecurityShared(Thread
);
769 /* Make sure we still have active impersonation */
770 if (Thread
->ActiveImpersonationInfo
)
772 /* Return data from caller */
773 ObReferenceObject(Thread
->ImpersonationInfo
->Token
);
774 *ImpersonationLevel
= Thread
->ImpersonationInfo
->ImpersonationLevel
;
775 *CopyOnOpen
= Thread
->ImpersonationInfo
->CopyOnOpen
;
776 *EffectiveOnly
= Thread
->ImpersonationInfo
->EffectiveOnly
;
779 Token
= Thread
->ImpersonationInfo
->Token
;
782 /* Unlock thread and return impersonation token */
783 PspUnlockThreadSecurityShared(Thread
);
787 #undef PsDereferenceImpersonationToken
793 PsDereferenceImpersonationToken(IN PACCESS_TOKEN ImpersonationToken
)
797 /* If we got a token, dereference it */
798 if (ImpersonationToken
) ObDereferenceObject(ImpersonationToken
);
801 #undef PsDereferencePrimaryToken
807 PsDereferencePrimaryToken(IN PACCESS_TOKEN PrimaryToken
)
811 /* Dereference the token*/
812 ObDereferenceObject(PrimaryToken
);
820 PsDisableImpersonation(IN PETHREAD Thread
,
821 OUT PSE_IMPERSONATION_STATE ImpersonationState
)
823 PPS_IMPERSONATION_INFORMATION Impersonation
= NULL
;
826 PSTRACE(PS_SECURITY_DEBUG
,
827 "Thread: %p State: %p\n", Thread
, ImpersonationState
);
829 /* Check if we don't have impersonation */
830 if (Thread
->ActiveImpersonationInfo
)
832 /* Lock thread security */
833 PspLockThreadSecurityExclusive(Thread
);
835 /* Disable impersonation */
836 OldFlags
= PspClearCrossThreadFlag(Thread
,
837 CT_ACTIVE_IMPERSONATION_INFO_BIT
);
839 /* Make sure nobody disabled it behind our back */
840 if (OldFlags
& CT_ACTIVE_IMPERSONATION_INFO_BIT
)
842 /* Copy the old state */
843 Impersonation
= Thread
->ImpersonationInfo
;
844 ImpersonationState
->Token
= Impersonation
->Token
;
845 ImpersonationState
->CopyOnOpen
= Impersonation
->CopyOnOpen
;
846 ImpersonationState
->EffectiveOnly
= Impersonation
->EffectiveOnly
;
847 ImpersonationState
->Level
= Impersonation
->ImpersonationLevel
;
850 /* Unlock thread security */
851 PspUnlockThreadSecurityExclusive(Thread
);
853 /* If we had impersonation info, return true */
854 if (Impersonation
) return TRUE
;
857 /* Clear everything */
858 ImpersonationState
->Token
= NULL
;
859 ImpersonationState
->CopyOnOpen
= FALSE
;
860 ImpersonationState
->EffectiveOnly
= FALSE
;
861 ImpersonationState
->Level
= SecurityAnonymous
;
870 PsRestoreImpersonation(IN PETHREAD Thread
,
871 IN PSE_IMPERSONATION_STATE ImpersonationState
)
874 PPS_IMPERSONATION_INFORMATION Impersonation
;
876 PSTRACE(PS_SECURITY_DEBUG
,
877 "Thread: %p State: %p\n", Thread
, ImpersonationState
);
879 /* Lock thread security */
880 PspLockThreadSecurityExclusive(Thread
);
882 /* Get the impersonation info */
883 Impersonation
= Thread
->ImpersonationInfo
;
885 /* Check if we're impersonating */
886 if (Thread
->ActiveImpersonationInfo
)
889 Token
= Impersonation
->Token
;
892 /* Check if we have an impersonation state */
893 if (ImpersonationState
)
895 /* Fill out the impersonation info */
896 Impersonation
->ImpersonationLevel
= ImpersonationState
->Level
;
897 Impersonation
->CopyOnOpen
= ImpersonationState
->CopyOnOpen
;
898 Impersonation
->EffectiveOnly
= ImpersonationState
->EffectiveOnly
;
899 Impersonation
->Token
= ImpersonationState
->Token
;
901 /* Enable impersonation */
902 PspSetCrossThreadFlag(Thread
, CT_ACTIVE_IMPERSONATION_INFO_BIT
);
906 /* Disable impersonation */
907 PspClearCrossThreadFlag(Thread
, CT_ACTIVE_IMPERSONATION_INFO_BIT
);
910 /* Unlock the thread */
911 PspUnlockThreadSecurityExclusive(Thread
);
913 /* Dereference the token */
914 if (Token
) ObDereferenceObject(Token
);
919 NtImpersonateThread(IN HANDLE ThreadHandle
,
920 IN HANDLE ThreadToImpersonateHandle
,
921 IN PSECURITY_QUALITY_OF_SERVICE SecurityQualityOfService
)
923 SECURITY_QUALITY_OF_SERVICE SafeServiceQoS
;
924 SECURITY_CLIENT_CONTEXT ClientContext
;
926 PETHREAD ThreadToImpersonate
;
927 KPROCESSOR_MODE PreviousMode
= ExGetPreviousMode();
930 PSTRACE(PS_SECURITY_DEBUG
,
931 "Threads: %p %p\n", ThreadHandle
, ThreadToImpersonateHandle
);
933 /* Check if call came from user mode */
934 if (PreviousMode
!= KernelMode
)
936 /* Enter SEH for probing */
940 ProbeForRead(SecurityQualityOfService
,
941 sizeof(SECURITY_QUALITY_OF_SERVICE
),
945 SafeServiceQoS
= *SecurityQualityOfService
;
946 SecurityQualityOfService
= &SafeServiceQoS
;
948 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
950 /* Return the exception code */
951 _SEH2_YIELD(return _SEH2_GetExceptionCode());
956 /* Reference the thread */
957 Status
= ObReferenceObjectByHandle(ThreadHandle
,
958 THREAD_DIRECT_IMPERSONATION
,
963 if (NT_SUCCESS(Status
))
965 /* Reference the impersonating thead */
966 Status
= ObReferenceObjectByHandle(ThreadToImpersonateHandle
,
970 (PVOID
*)&ThreadToImpersonate
,
972 if (NT_SUCCESS(Status
))
974 /* Create a client security context */
975 Status
= SeCreateClientSecurity(ThreadToImpersonate
,
976 SecurityQualityOfService
,
979 if (NT_SUCCESS(Status
))
981 /* Do the impersonation */
982 SeImpersonateClient(&ClientContext
, Thread
);
983 if (ClientContext
.ClientToken
)
985 /* Dereference the client token if we had one */
986 ObDereferenceObject(ClientContext
.ClientToken
);
990 /* Dereference the thread to impersonate */
991 ObDereferenceObject(ThreadToImpersonate
);
994 /* Dereference the main thread */
995 ObDereferenceObject(Thread
);