ReactOS  0.4.14-dev-323-g6fe6a88
port.c File Reference
#include <ntoskrnl.h>
#include <debug.h>
Include dependency graph for port.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

INIT_FUNCTION BOOLEAN NTAPI LpcInitSystem (VOID)
 
BOOLEAN NTAPI LpcpValidateClientPort (PETHREAD ClientThread, PLPCP_PORT_OBJECT Port)
 
NTSTATUS NTAPI NtImpersonateClientOfPort (IN HANDLE PortHandle, IN PPORT_MESSAGE ClientMessage)
 
NTSTATUS NTAPI NtQueryPortInformationProcess (VOID)
 
NTSTATUS NTAPI NtQueryInformationPort (IN HANDLE PortHandle, IN PORT_INFORMATION_CLASS PortInformationClass, OUT PVOID PortInformation, IN ULONG PortInformationLength, OUT PULONG ReturnLength)
 

Variables

POBJECT_TYPE LpcPortObjectType
 
POBJECT_TYPE LpcWaitablePortObjectType
 
ULONG LpcpMaxMessageSize
 
PAGED_LOOKASIDE_LIST LpcpMessagesLookaside
 
KGUARDED_MUTEX LpcpLock
 
ULONG LpcpTraceLevel = 0
 
ULONG LpcpNextMessageId = 1
 
ULONG LpcpNextCallbackId = 1
 
static GENERIC_MAPPING LpcpPortMapping
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 12 of file port.c.

Function Documentation

◆ LpcInitSystem()

INIT_FUNCTION BOOLEAN NTAPI LpcInitSystem ( VOID  )

Definition at line 37 of file port.c.

38 {
39  OBJECT_TYPE_INITIALIZER ObjectTypeInitializer;
41 
42  /* Setup the LPC Lock */
44 
45  /* Create the Port Object Type */
46  RtlZeroMemory(&ObjectTypeInitializer, sizeof(ObjectTypeInitializer));
47  RtlInitUnicodeString(&Name, L"Port");
48  ObjectTypeInitializer.Length = sizeof(ObjectTypeInitializer);
49  ObjectTypeInitializer.DefaultNonPagedPoolCharge = sizeof(LPCP_NONPAGED_PORT_QUEUE);
50  ObjectTypeInitializer.DefaultPagedPoolCharge = FIELD_OFFSET(LPCP_PORT_OBJECT, WaitEvent);
51  ObjectTypeInitializer.GenericMapping = LpcpPortMapping;
52  ObjectTypeInitializer.PoolType = PagedPool;
53  ObjectTypeInitializer.UseDefaultObject = TRUE;
54  ObjectTypeInitializer.CloseProcedure = LpcpClosePort;
55  ObjectTypeInitializer.DeleteProcedure = LpcpDeletePort;
56  ObjectTypeInitializer.ValidAccessMask = PORT_ALL_ACCESS;
59  &ObjectTypeInitializer,
60  NULL,
62 
63  /* Create the Waitable Port Object Type */
64  RtlInitUnicodeString(&Name, L"WaitablePort");
65  ObjectTypeInitializer.PoolType = NonPagedPool;
66  ObjectTypeInitializer.DefaultNonPagedPoolCharge += sizeof(LPCP_PORT_OBJECT);
67  ObjectTypeInitializer.DefaultPagedPoolCharge = 0;
68  ObjectTypeInitializer.UseDefaultObject = FALSE;
70  &ObjectTypeInitializer,
71  NULL,
73 
74  /* Allocate the LPC lookaside list */
77  NULL,
78  NULL,
79  0,
81  'McpL',
82  32);
83 
84  /* We're done */
85  return TRUE;
86 }
NTSTATUS NTAPI ObCreateObjectType(IN PUNICODE_STRING TypeName, IN POBJECT_TYPE_INITIALIZER ObjectTypeInitializer, IN PVOID Reserved, OUT POBJECT_TYPE *ObjectType)
Definition: oblife.c:1048
#define TRUE
Definition: types.h:120
POBJECT_TYPE LpcPortObjectType
Definition: port.c:17
VOID NTAPI ExInitializePagedLookasideList(IN PPAGED_LOOKASIDE_LIST Lookaside, IN PALLOCATE_FUNCTION Allocate OPTIONAL, IN PFREE_FUNCTION Free OPTIONAL, IN ULONG Flags, IN SIZE_T Size, IN ULONG Tag, IN USHORT Depth)
Definition: lookas.c:274
static GENERIC_MAPPING LpcpPortMapping
Definition: port.c:24
#define LPCP_MAX_MESSAGE_SIZE
Definition: lpctypes.h:276
struct _LPCP_NONPAGED_PORT_QUEUE LPCP_NONPAGED_PORT_QUEUE
struct NameRec_ * Name
Definition: cdprocs.h:464
POBJECT_TYPE LpcWaitablePortObjectType
Definition: port.c:17
smooth NULL
Definition: ftsmooth.c:416
OB_CLOSE_METHOD CloseProcedure
Definition: obtypes.h:368
VOID NTAPI LpcpDeletePort(IN PVOID ObjectBody)
Definition: close.c:302
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
static const WCHAR L[]
Definition: oid.c:1250
ULONG LpcpMaxMessageSize
Definition: port.c:18
VOID FASTCALL KeInitializeGuardedMutex(OUT PKGUARDED_MUTEX GuardedMutex)
Definition: gmutex.c:31
GENERIC_MAPPING GenericMapping
Definition: obtypes.h:358
KGUARDED_MUTEX LpcpLock
Definition: port.c:20
struct _LPCP_PORT_OBJECT LPCP_PORT_OBJECT
#define FIELD_OFFSET(t, f)
Definition: typedefs.h:254
#define OBJ_VALID_ATTRIBUTES
Definition: winternl.h:233
#define PORT_ALL_ACCESS
Definition: lpctypes.h:47
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
PAGED_LOOKASIDE_LIST LpcpMessagesLookaside
Definition: port.c:19
VOID NTAPI LpcpClosePort(IN PEPROCESS Process OPTIONAL, IN PVOID Object, IN ACCESS_MASK GrantedAccess, IN ULONG ProcessHandleCount, IN ULONG SystemHandleCount)
Definition: close.c:244
OB_DELETE_METHOD DeleteProcedure
Definition: obtypes.h:369
ULONG DefaultNonPagedPoolCharge
Definition: obtypes.h:365

Referenced by Phase1InitializationDiscard().

◆ LpcpValidateClientPort()

BOOLEAN NTAPI LpcpValidateClientPort ( PETHREAD  ClientThread,
PLPCP_PORT_OBJECT  Port 
)

Definition at line 90 of file port.c.

93 {
94  PLPCP_PORT_OBJECT ThreadPort;
95 
96  /* Get the thread's port */
97  ThreadPort = LpcpGetPortFromThread(ClientThread);
98  if (ThreadPort == NULL)
99  {
100  return FALSE;
101  }
102 
103  /* Check if the port matches directly */
104  if ((Port == ThreadPort) ||
105  (Port == ThreadPort->ConnectionPort) ||
106  (Port == ThreadPort->ConnectedPort))
107  {
108  return TRUE;
109  }
110 
111  /* Check if this is a communication port and the connection port matches */
113  (Port->ConnectionPort == ThreadPort))
114  {
115  return TRUE;
116  }
117 
118  return FALSE;
119 }
CPPORT Port[4]
Definition: headless.c:34
#define TRUE
Definition: types.h:120
struct _LPCP_PORT_OBJECT * ConnectedPort
Definition: lpctypes.h:212
#define LPCP_PORT_TYPE_MASK
Definition: lpctypes.h:58
USHORT Flags
Definition: cportlib.h:31
smooth NULL
Definition: ftsmooth.c:416
UINT CALLBACK ClientThread(_Inout_ PVOID Parameter)
FORCEINLINE PLPCP_PORT_OBJECT LpcpGetPortFromThread(IN PETHREAD Thread)
Definition: lpc_x.h:144
struct _LPCP_PORT_OBJECT * ConnectionPort
Definition: lpctypes.h:211
#define LPCP_COMMUNICATION_PORT
Definition: lpctypes.h:56

Referenced by LpcpCopyRequestData(), and NtImpersonateClientOfPort().

◆ NtImpersonateClientOfPort()

NTSTATUS NTAPI NtImpersonateClientOfPort ( IN HANDLE  PortHandle,
IN PPORT_MESSAGE  ClientMessage 
)

Definition at line 126 of file port.c.

128 {
132  ULONG MessageId;
133  PLPCP_PORT_OBJECT Port = NULL, ConnectedPort = NULL;
136 
137  PAGED_CODE();
138 
139  /* Check if the call comes from user mode */
140  if (PreviousMode != KernelMode)
141  {
142  _SEH2_TRY
143  {
144  ProbeForRead(ClientMessage, sizeof(*ClientMessage), sizeof(PVOID));
145  ClientId = ((volatile PORT_MESSAGE*)ClientMessage)->ClientId;
146  MessageId = ((volatile PORT_MESSAGE*)ClientMessage)->MessageId;
147  }
149  {
151  }
152  _SEH2_END;
153  }
154  else
155  {
156  ClientId = ClientMessage->ClientId;
157  MessageId = ClientMessage->MessageId;
158  }
159 
160  /* Reference the port handle */
161  Status = ObReferenceObjectByHandle(PortHandle,
164  PreviousMode,
165  (PVOID*)&Port,
166  NULL);
167  if (!NT_SUCCESS(Status))
168  {
169  DPRINT1("Failed to reference port handle: 0x%ls\n", Status);
170  return Status;
171  }
172 
173  /* Make sure this is a connection port */
175  {
176  /* It isn't, fail */
177  DPRINT1("Port is not a communication port\n");
179  goto Cleanup;
180  }
181 
182  /* Look up the client thread */
184  if (!NT_SUCCESS(Status))
185  {
186  DPRINT1("Failed to lookup client thread: 0x%ls\n", Status);
187  goto Cleanup;
188  }
189 
190  /* Acquire the lock */
192 
193  /* Get the connected port and try to reference it */
194  ConnectedPort = Port->ConnectedPort;
195  if ((ConnectedPort == NULL) || !ObReferenceObjectSafe(ConnectedPort))
196  {
197  DPRINT1("Failed to reference the connected port\n");
198  ConnectedPort = NULL;
200  goto CleanupWithLock;
201  }
202 
203  /* Check for no-impersonation flag */
205  {
206  DPRINT1("Reply message has no impersonation flag set\n");
208  goto CleanupWithLock;
209  }
210 
211  /* Check for message id mismatch */
212  if ((ClientThread->LpcReplyMessageId != MessageId) || (MessageId == 0))
213  {
214  DPRINT1("LpcReplyMessageId mismatch: 0x%lx/0x%lx.\n",
215  ClientThread->LpcReplyMessageId, MessageId);
217  goto CleanupWithLock;
218  }
219 
220  /* Validate the port */
222  {
223  DPRINT1("LpcpValidateClientPort failed\n");
225  goto CleanupWithLock;
226  }
227 
228  /* Release the lock */
230 
231  /* Check if security is static */
232  if (!(ConnectedPort->Flags & LPCP_SECURITY_DYNAMIC))
233  {
234  /* Use the static security for impersonation */
235  Status = SeImpersonateClientEx(&ConnectedPort->StaticSecurity, NULL);
236  goto Cleanup;
237  }
238 
239  /* Create new dynamic security */
241  &ConnectedPort->SecurityQos,
242  FALSE,
243  &ClientContext);
244  if (!NT_SUCCESS(Status))
245  {
246  DPRINT1("SeCreateClientSecurity failed\n");
247  goto Cleanup;
248  }
249 
250  /* Use dynamic security for impersonation */
252 
253  /* Get rid of the security context */
255 
256 Cleanup:
257 
258  if (ConnectedPort != NULL)
259  ObDereferenceObject(ConnectedPort);
260 
261  if (ClientThread != NULL)
263 
265 
266  return Status;
267 
268 CleanupWithLock:
269 
270  /* Release the lock */
272  goto Cleanup;
273 }
CPPORT Port[4]
Definition: headless.c:34
POBJECT_TYPE LpcPortObjectType
Definition: port.c:17
VOID FASTCALL KeAcquireGuardedMutex(IN PKGUARDED_MUTEX GuardedMutex)
Definition: gmutex.c:42
#define KeGetPreviousMode()
Definition: ketypes.h:1107
LONG NTSTATUS
Definition: precomp.h:26
VOID NTAPI ObDereferenceObject(IN PVOID Object)
Definition: obref.c:375
#define LPCP_PORT_TYPE_MASK
Definition: lpctypes.h:58
#define PAGED_CODE()
Definition: video.h:57
_SEH2_TRY
Definition: create.c:4250
BOOLEAN FASTCALL ObReferenceObjectSafe(IN PVOID Object)
Definition: obref.c:24
#define STATUS_INVALID_PORT_HANDLE
Definition: ntstatus.h:288
uint32_t ULONG_PTR
Definition: typedefs.h:63
NTSTATUS NTAPI ObReferenceObjectByHandle(IN HANDLE Handle, IN ACCESS_MASK DesiredAccess, IN POBJECT_TYPE ObjectType, IN KPROCESSOR_MODE AccessMode, OUT PVOID *Object, OUT POBJECT_HANDLE_INFORMATION HandleInformation OPTIONAL)
Definition: obref.c:496
#define SeDeleteClientSecurity(C)
Definition: imports.h:320
USHORT Flags
Definition: cportlib.h:31
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
smooth NULL
Definition: ftsmooth.c:416
UINT CALLBACK ClientThread(_Inout_ PVOID Parameter)
#define STATUS_REPLY_MESSAGE_MISMATCH
Definition: ntstatus.h:661
NTKERNELAPI NTSTATUS NTAPI SeCreateClientSecurity(IN PETHREAD Thread, IN PSECURITY_QUALITY_OF_SERVICE QualityOfService, IN BOOLEAN RemoteClient, OUT PSECURITY_CLIENT_CONTEXT ClientContext)
Definition: access.c:506
_In_ KPROCESSOR_MODE PreviousMode
Definition: sefuncs.h:103
_Out_ PCLIENT_ID ClientId
Definition: kefuncs.h:1176
#define _SEH2_YIELD(STMT_)
Definition: pseh2_64.h:8
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
_In_ PVOID ClientContext
Definition: netioddk.h:55
#define STATUS_ACCESS_DENIED
Definition: udferr_usr.h:145
CCHAR KPROCESSOR_MODE
Definition: ketypes.h:7
NTSTATUS NTAPI PsLookupProcessThreadByCid(IN PCLIENT_ID Cid, OUT PEPROCESS *Process OPTIONAL, OUT PETHREAD *Thread)
Definition: process.c:961
VOID NTAPI ProbeForRead(IN CONST VOID *Address, IN SIZE_T Length, IN ULONG Alignment)
Definition: exintrin.c:102
static const WCHAR Cleanup[]
Definition: register.c:80
NTSTATUS NTAPI SeImpersonateClientEx(IN PSECURITY_CLIENT_CONTEXT ClientContext, IN PETHREAD ServerThread OPTIONAL)
Definition: access.c:589
Status
Definition: gdiplustypes.h:24
_SEH2_END
Definition: create.c:4424
KGUARDED_MUTEX LpcpLock
Definition: port.c:20
#define LPCP_THREAD_FLAG_NO_IMPERSONATION
Definition: lpc.h:56
VOID FASTCALL KeReleaseGuardedMutex(IN OUT PKGUARDED_MUTEX GuardedMutex)
Definition: gmutex.c:53
#define DPRINT1
Definition: precomp.h:8
#define PORT_ALL_ACCESS
Definition: lpctypes.h:47
#define LPCP_COMMUNICATION_PORT
Definition: lpctypes.h:56
#define STATUS_PORT_DISCONNECTED
Definition: ntstatus.h:277
unsigned int ULONG
Definition: retypes.h:1
#define LPCP_SECURITY_DYNAMIC
Definition: lpctypes.h:62
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:12
BOOLEAN NTAPI LpcpValidateClientPort(PETHREAD ClientThread, PLPCP_PORT_OBJECT Port)
Definition: port.c:90

◆ NtQueryInformationPort()

NTSTATUS NTAPI NtQueryInformationPort ( IN HANDLE  PortHandle,
IN PORT_INFORMATION_CLASS  PortInformationClass,
OUT PVOID  PortInformation,
IN ULONG  PortInformationLength,
OUT PULONG  ReturnLength 
)

Definition at line 285 of file port.c.

290 {
292  return STATUS_NOT_IMPLEMENTED;
293 }
#define STATUS_NOT_IMPLEMENTED
Definition: ntstatus.h:225
#define UNIMPLEMENTED
Definition: debug.h:114

◆ NtQueryPortInformationProcess()

NTSTATUS NTAPI NtQueryPortInformationProcess ( VOID  )

Definition at line 277 of file port.c.

278 {
279  /* This is all this function does */
280  return STATUS_UNSUCCESSFUL;
281 }
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132

Variable Documentation

◆ LpcpLock

◆ LpcpMaxMessageSize

ULONG LpcpMaxMessageSize

Definition at line 18 of file port.c.

Referenced by LpcInitSystem(), and LpcpCreatePort().

◆ LpcpMessagesLookaside

PAGED_LOOKASIDE_LIST LpcpMessagesLookaside

Definition at line 19 of file port.c.

Referenced by LpcInitSystem(), LpcpAllocateFromPortZone(), and LpcpFreeToPortZone().

◆ LpcpNextCallbackId

ULONG LpcpNextCallbackId = 1

Definition at line 22 of file port.c.

◆ LpcpNextMessageId

ULONG LpcpNextMessageId = 1

◆ LpcPortObjectType

◆ LpcpPortMapping

GENERIC_MAPPING LpcpPortMapping
static
Initial value:
=
{
0,
}
#define PORT_CONNECT
Definition: lpctypes.h:46
#define READ_CONTROL
Definition: nt_native.h:58
#define PORT_ALL_ACCESS
Definition: lpctypes.h:47
#define DELETE
Definition: nt_native.h:57

Definition at line 24 of file port.c.

Referenced by LpcInitSystem().

◆ LpcpTraceLevel

ULONG LpcpTraceLevel = 0

Definition at line 21 of file port.c.

◆ LpcWaitablePortObjectType

POBJECT_TYPE LpcWaitablePortObjectType

Definition at line 17 of file port.c.

Referenced by LpcInitSystem().