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 0);//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
;
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 ExFreePool(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 /* Fast Reference the Token */
704 Token
= ObFastReferenceObject(&Process
->Token
);
706 /* Check if we got the Token or if we got locked */
709 /* Lock the Process */
710 PspLockProcessSecurityShared(Process
);
712 /* Do a Locked Fast Reference */
713 Token
= ObFastReferenceObjectLocked(&Process
->Token
);
715 /* Unlock the Process */
716 PspUnlockProcessSecurityShared(Process
);
721 /* Lock the Process */
722 PspLockProcessSecurityShared(Process
);
724 /* Make sure impersonation is still active */
725 if (Thread
->ActiveImpersonationInfo
)
728 Token
= Thread
->ImpersonationInfo
->Token
;
729 ObReferenceObject(Token
);
731 /* Return data to caller */
732 *TokenType
= TokenImpersonation
;
733 *EffectiveOnly
= Thread
->ImpersonationInfo
->EffectiveOnly
;
734 *Level
= Thread
->ImpersonationInfo
->ImpersonationLevel
;
736 /* Unlock the Process */
737 PspUnlockProcessSecurityShared(Process
);
741 /* Unlock the Process */
742 PspUnlockProcessSecurityShared(Process
);
745 /* Return the token */
746 *TokenType
= TokenPrimary
;
747 *EffectiveOnly
= FALSE
;
756 PsReferenceImpersonationToken(IN PETHREAD Thread
,
757 OUT PBOOLEAN CopyOnOpen
,
758 OUT PBOOLEAN EffectiveOnly
,
759 OUT PSECURITY_IMPERSONATION_LEVEL ImpersonationLevel
)
763 PSTRACE(PS_SECURITY_DEBUG
, "Thread: %p\n", Thread
);
765 /* If we don't have impersonation info, just quit */
766 if (!Thread
->ActiveImpersonationInfo
) return NULL
;
768 /* Lock the thread */
769 PspLockThreadSecurityShared(Thread
);
771 /* Make sure we still have active impersonation */
772 if (Thread
->ActiveImpersonationInfo
)
774 /* Return data from caller */
775 ObReferenceObject(Thread
->ImpersonationInfo
->Token
);
776 *ImpersonationLevel
= Thread
->ImpersonationInfo
->ImpersonationLevel
;
777 *CopyOnOpen
= Thread
->ImpersonationInfo
->CopyOnOpen
;
778 *EffectiveOnly
= Thread
->ImpersonationInfo
->EffectiveOnly
;
781 Token
= Thread
->ImpersonationInfo
->Token
;
784 /* Unlock thread and return impersonation token */
785 PspUnlockThreadSecurityShared(Thread
);
789 #undef PsDereferenceImpersonationToken
795 PsDereferenceImpersonationToken(IN PACCESS_TOKEN ImpersonationToken
)
799 /* If we got a token, dereference it */
800 if (ImpersonationToken
) ObDereferenceObject(ImpersonationToken
);
803 #undef PsDereferencePrimaryToken
809 PsDereferencePrimaryToken(IN PACCESS_TOKEN PrimaryToken
)
813 /* Dereference the token*/
814 ObDereferenceObject(PrimaryToken
);
822 PsDisableImpersonation(IN PETHREAD Thread
,
823 IN PSE_IMPERSONATION_STATE ImpersonationState
)
825 PPS_IMPERSONATION_INFORMATION Impersonation
= NULL
;
826 LONG NewValue
, OldValue
;
828 PSTRACE(PS_SECURITY_DEBUG
,
829 "Thread: %p State: %p\n", Thread
, ImpersonationState
);
831 /* Check if we don't have impersonation */
832 if (Thread
->ActiveImpersonationInfo
)
834 /* Lock thread security */
835 PspLockThreadSecurityExclusive(Thread
);
837 /* Disable impersonation */
838 OldValue
= Thread
->CrossThreadFlags
;
841 /* Attempt to change the flag */
843 InterlockedCompareExchange((PLONG
)&Thread
->CrossThreadFlags
,
845 CT_ACTIVE_IMPERSONATION_INFO_BIT
,
847 } while (NewValue
!= OldValue
);
849 /* Make sure nobody disabled it behind our back */
850 if (NewValue
& CT_ACTIVE_IMPERSONATION_INFO_BIT
)
852 /* Copy the old state */
853 Impersonation
= Thread
->ImpersonationInfo
;
854 ImpersonationState
->Token
= Impersonation
->Token
;
855 ImpersonationState
->CopyOnOpen
= Impersonation
->CopyOnOpen
;
856 ImpersonationState
->EffectiveOnly
= Impersonation
->EffectiveOnly
;
857 ImpersonationState
->Level
= Impersonation
->ImpersonationLevel
;
860 /* Unlock thread security */
861 PspUnlockThreadSecurityExclusive(Thread
);
863 /* If we had impersonation info, return true */
864 if (Impersonation
) return TRUE
;
867 /* Clear everything */
868 ImpersonationState
->Token
= NULL
;
869 ImpersonationState
->CopyOnOpen
= FALSE
;
870 ImpersonationState
->EffectiveOnly
= FALSE
;
871 ImpersonationState
->Level
= SecurityAnonymous
;
880 PsRestoreImpersonation(IN PETHREAD Thread
,
881 IN PSE_IMPERSONATION_STATE ImpersonationState
)
884 PPS_IMPERSONATION_INFORMATION Impersonation
;
886 PSTRACE(PS_SECURITY_DEBUG
,
887 "Thread: %p State: %p\n", Thread
, ImpersonationState
);
889 /* Lock thread security */
890 PspLockThreadSecurityExclusive(Thread
);
892 /* Get the impersonation info */
893 Impersonation
= Thread
->ImpersonationInfo
;
895 /* Check if we're impersonating */
896 if (Thread
->ActiveImpersonationInfo
)
899 Token
= Impersonation
->Token
;
902 /* Check if we have an impersonation state */
903 if (ImpersonationState
)
905 /* Fill out the impersonation info */
906 Impersonation
->ImpersonationLevel
= ImpersonationState
->Level
;
907 Impersonation
->CopyOnOpen
= ImpersonationState
->CopyOnOpen
;
908 Impersonation
->EffectiveOnly
= ImpersonationState
->EffectiveOnly
;
909 Impersonation
->Token
= ImpersonationState
->Token
;
911 /* Enable impersonation */
912 PspSetCrossThreadFlag(Thread
, CT_ACTIVE_IMPERSONATION_INFO_BIT
);
916 /* Disable impersonation */
917 PspClearCrossThreadFlag(Thread
, CT_ACTIVE_IMPERSONATION_INFO_BIT
);
920 /* Unlock the thread */
921 PspUnlockThreadSecurityExclusive(Thread
);
923 /* Dereference the token */
924 if (Token
) ObDereferenceObject(Token
);
929 NtImpersonateThread(IN HANDLE ThreadHandle
,
930 IN HANDLE ThreadToImpersonateHandle
,
931 IN PSECURITY_QUALITY_OF_SERVICE SecurityQualityOfService
)
933 SECURITY_QUALITY_OF_SERVICE SafeServiceQoS
;
934 SECURITY_CLIENT_CONTEXT ClientContext
;
936 PETHREAD ThreadToImpersonate
;
937 KPROCESSOR_MODE PreviousMode
= ExGetPreviousMode();
940 PSTRACE(PS_SECURITY_DEBUG
,
941 "Threads: %p %p\n", ThreadHandle
, ThreadToImpersonateHandle
);
943 /* Check if call came from user mode */
944 if (PreviousMode
!= KernelMode
)
946 /* Enter SEH for probing */
950 ProbeForRead(SecurityQualityOfService
,
951 sizeof(SECURITY_QUALITY_OF_SERVICE
),
955 SafeServiceQoS
= *SecurityQualityOfService
;
956 SecurityQualityOfService
= &SafeServiceQoS
;
958 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
960 /* Return the exception code */
961 _SEH2_YIELD(return _SEH2_GetExceptionCode());
966 /* Reference the thread */
967 Status
= ObReferenceObjectByHandle(ThreadHandle
,
968 THREAD_DIRECT_IMPERSONATION
,
973 if (NT_SUCCESS(Status
))
975 /* Reference the impersonating thead */
976 Status
= ObReferenceObjectByHandle(ThreadToImpersonateHandle
,
980 (PVOID
*)&ThreadToImpersonate
,
982 if (NT_SUCCESS(Status
))
984 /* Create a client security context */
985 Status
= SeCreateClientSecurity(ThreadToImpersonate
,
986 SecurityQualityOfService
,
989 if (NT_SUCCESS(Status
))
991 /* Do the impersonation */
992 SeImpersonateClient(&ClientContext
, Thread
);
993 if (ClientContext
.ClientToken
)
995 /* Dereference the client token if we had one */
996 ObDereferenceObject(ClientContext
.ClientToken
);
1000 /* Dereference the thread to impersonate */
1001 ObDereferenceObject(ThreadToImpersonate
);
1004 /* Dereference the main thread */
1005 ObDereferenceObject(Thread
);