summaryrefslogtreecommitdiffstats
path: root/private/ntos/ps/psimpers.c
blob: 3a1cb612b75c27d7a35743df6ea8597feaa73180 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*++

Copyright (c) 1989  Microsoft Corporation

Module Name:

    psimpers.c

Abstract:

    This module implements the NtImpersonateThread() service.


Author:

    Jim Kelly (JimK) 20-Apr-1991

Revision History:

--*/

#include "psp.h"


NTSTATUS
NtImpersonateThread(
    IN HANDLE ServerThreadHandle,
    IN HANDLE ClientThreadHandle,
    IN PSECURITY_QUALITY_OF_SERVICE SecurityQos
    )

/*++

Routine Description:

    This routine is used to cause the server thread to impersonate the client
    thread.  The impersonation is done according to the specified quality
    of service parameters.



Arguments:

    ServerThreadHandle - Is a handle to the server thread (the impersonator, or
        doing the impersonation).  This handle must be open for
        THREAD_IMPERSONATE access.

    ClientThreadHandle - Is a handle to the Client thread (the impersonatee, or
        one being impersonated).   This handle must be open for
        THREAD_DIRECT_IMPERSONATION access.


    SecurityQos - A pointer to security quality of service information
        indicating what form of impersonation is to be performed.



Return Value:

    STATUS_SUCCESS - Indicates the call completed successfully.


--*/

{


    KPROCESSOR_MODE PreviousMode;
    NTSTATUS Status;
    PETHREAD ClientThread, ServerThread;
    SECURITY_QUALITY_OF_SERVICE CapturedQos;
    SECURITY_CLIENT_CONTEXT ClientSecurityContext;

    //
    // Get previous processor mode and probe and capture arguments if necessary
    //

    PreviousMode = KeGetPreviousMode();
    if (PreviousMode != KernelMode) {

        try {

            ProbeForRead( SecurityQos,
                          sizeof( SECURITY_QUALITY_OF_SERVICE ),
                          sizeof( ULONG )
                          );
            CapturedQos = (*SecurityQos);

        } except( EXCEPTION_EXECUTE_HANDLER ) {
            return( GetExceptionCode() );
        }

    } else {

        CapturedQos = *SecurityQos;

    }


    //
    // Reference the client thread, checking for appropriate access.
    //

    Status = ObReferenceObjectByHandle(
                 ClientThreadHandle,           // Handle
                 THREAD_DIRECT_IMPERSONATION,  // DesiredAccess
                 PsThreadType,                // ObjectType
                 PreviousMode,                 // AccessMode
                 (PVOID *)&ClientThread,       // Object
                 NULL                          // GrantedAccess
                 );

    if ( !NT_SUCCESS(Status) ) {
        return Status;
    }

    //
    // Reference the client thread, checking for appropriate access.
    //

    Status = ObReferenceObjectByHandle(
                 ServerThreadHandle,           // Handle
                 THREAD_IMPERSONATE,           // DesiredAccess
                 PsThreadType,                // ObjectType
                 PreviousMode,                 // AccessMode
                 (PVOID *)&ServerThread,       // Object
                 NULL                          // GrantedAccess
                 );

    if ( !NT_SUCCESS(Status) ) {
        ObDereferenceObject( ClientThread );
        return Status;
    }


    //
    // Get the client's security context
    //

    Status = SeCreateClientSecurity (
                 ClientThread,          // ClientThread
                 &CapturedQos,          // SecurityQos
                 FALSE,                 // ServerIsRemote
                 &ClientSecurityContext  // ClientContext
                 );

    if ( !NT_SUCCESS(Status) ) {
        ObDereferenceObject( ServerThread );
        ObDereferenceObject( ClientThread );
        return Status;
    }


    //
    // Impersonate the client
    //

    SeImpersonateClient( &ClientSecurityContext, ServerThread );
    SeDeleteClientSecurity( &ClientSecurityContext );

    //
    // Done.
    //


    ObDereferenceObject( ServerThread );
    ObDereferenceObject( ClientThread );
    return STATUS_SUCCESS;
}