2007-01-10 04:27:40 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS Kernel
|
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
2000-06-04 17:27:39 +00:00
|
|
|
* FILE: ntoskrnl/lpc/port.c
|
2007-01-10 04:27:40 +00:00
|
|
|
* PURPOSE: Local Procedure Call: Port Management
|
|
|
|
* PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
|
2000-06-04 17:27:39 +00:00
|
|
|
*/
|
|
|
|
|
2007-01-10 04:27:40 +00:00
|
|
|
/* INCLUDES ******************************************************************/
|
2000-06-04 17:27:39 +00:00
|
|
|
|
2004-08-15 16:39:12 +00:00
|
|
|
#include <ntoskrnl.h>
|
2000-06-04 17:27:39 +00:00
|
|
|
#define NDEBUG
|
2007-01-10 06:09:57 +00:00
|
|
|
#include <debug.h>
|
2000-06-04 17:27:39 +00:00
|
|
|
|
|
|
|
/* GLOBALS *******************************************************************/
|
|
|
|
|
2012-09-02 22:06:42 +00:00
|
|
|
POBJECT_TYPE LpcPortObjectType, LpcWaitablePortObjectType;
|
2007-01-10 04:27:40 +00:00
|
|
|
ULONG LpcpMaxMessageSize;
|
|
|
|
PAGED_LOOKASIDE_LIST LpcpMessagesLookaside;
|
|
|
|
KGUARDED_MUTEX LpcpLock;
|
2007-01-21 22:43:32 +00:00
|
|
|
ULONG LpcpTraceLevel = 0;
|
2007-01-10 04:27:40 +00:00
|
|
|
ULONG LpcpNextMessageId = 1, LpcpNextCallbackId = 1;
|
2000-06-04 17:27:39 +00:00
|
|
|
|
2012-09-02 22:06:42 +00:00
|
|
|
static GENERIC_MAPPING LpcpPortMapping =
|
2005-05-15 17:59:33 +00:00
|
|
|
{
|
2012-09-02 22:06:42 +00:00
|
|
|
READ_CONTROL | PORT_CONNECT,
|
|
|
|
DELETE | PORT_CONNECT,
|
2005-05-15 17:59:33 +00:00
|
|
|
0,
|
|
|
|
PORT_ALL_ACCESS
|
|
|
|
};
|
2001-01-28 17:42:56 +00:00
|
|
|
|
2007-01-10 04:27:40 +00:00
|
|
|
/* PRIVATE FUNCTIONS *********************************************************/
|
2000-06-04 17:27:39 +00:00
|
|
|
|
2018-12-30 11:19:11 +00:00
|
|
|
INIT_FUNCTION
|
2007-01-25 17:51:45 +00:00
|
|
|
BOOLEAN
|
2005-09-13 23:48:54 +00:00
|
|
|
NTAPI
|
2007-01-25 17:51:45 +00:00
|
|
|
LpcInitSystem(VOID)
|
2000-06-04 17:27:39 +00:00
|
|
|
{
|
2005-05-15 17:59:33 +00:00
|
|
|
OBJECT_TYPE_INITIALIZER ObjectTypeInitializer;
|
|
|
|
UNICODE_STRING Name;
|
|
|
|
|
2007-01-10 04:27:40 +00:00
|
|
|
/* Setup the LPC Lock */
|
|
|
|
KeInitializeGuardedMutex(&LpcpLock);
|
|
|
|
|
2005-05-15 21:39:10 +00:00
|
|
|
/* Create the Port Object Type */
|
2005-05-15 17:59:33 +00:00
|
|
|
RtlZeroMemory(&ObjectTypeInitializer, sizeof(ObjectTypeInitializer));
|
|
|
|
RtlInitUnicodeString(&Name, L"Port");
|
|
|
|
ObjectTypeInitializer.Length = sizeof(ObjectTypeInitializer);
|
2012-09-02 22:06:42 +00:00
|
|
|
ObjectTypeInitializer.DefaultNonPagedPoolCharge = sizeof(LPCP_NONPAGED_PORT_QUEUE);
|
|
|
|
ObjectTypeInitializer.DefaultPagedPoolCharge = FIELD_OFFSET(LPCP_PORT_OBJECT, WaitEvent);
|
2005-05-15 17:59:33 +00:00
|
|
|
ObjectTypeInitializer.GenericMapping = LpcpPortMapping;
|
2007-01-10 04:27:40 +00:00
|
|
|
ObjectTypeInitializer.PoolType = PagedPool;
|
2005-05-15 17:59:33 +00:00
|
|
|
ObjectTypeInitializer.UseDefaultObject = TRUE;
|
|
|
|
ObjectTypeInitializer.CloseProcedure = LpcpClosePort;
|
|
|
|
ObjectTypeInitializer.DeleteProcedure = LpcpDeletePort;
|
2007-01-09 17:18:22 +00:00
|
|
|
ObjectTypeInitializer.ValidAccessMask = PORT_ALL_ACCESS;
|
2012-09-02 22:06:42 +00:00
|
|
|
ObjectTypeInitializer.InvalidAttributes = OBJ_VALID_ATTRIBUTES & ~OBJ_CASE_INSENSITIVE;
|
2007-01-10 04:27:40 +00:00
|
|
|
ObCreateObjectType(&Name,
|
|
|
|
&ObjectTypeInitializer,
|
|
|
|
NULL,
|
|
|
|
&LpcPortObjectType);
|
|
|
|
|
2016-11-07 00:55:34 +00:00
|
|
|
/* Create the Waitable Port Object Type */
|
2012-09-02 22:06:42 +00:00
|
|
|
RtlInitUnicodeString(&Name, L"WaitablePort");
|
|
|
|
ObjectTypeInitializer.PoolType = NonPagedPool;
|
|
|
|
ObjectTypeInitializer.DefaultNonPagedPoolCharge += sizeof(LPCP_PORT_OBJECT);
|
|
|
|
ObjectTypeInitializer.DefaultPagedPoolCharge = 0;
|
|
|
|
ObjectTypeInitializer.UseDefaultObject = FALSE;
|
|
|
|
ObCreateObjectType(&Name,
|
|
|
|
&ObjectTypeInitializer,
|
|
|
|
NULL,
|
|
|
|
&LpcWaitablePortObjectType);
|
|
|
|
|
2007-01-10 04:27:40 +00:00
|
|
|
/* Allocate the LPC lookaside list */
|
|
|
|
LpcpMaxMessageSize = LPCP_MAX_MESSAGE_SIZE;
|
|
|
|
ExInitializePagedLookasideList(&LpcpMessagesLookaside,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
LpcpMaxMessageSize,
|
2009-08-24 18:19:53 +00:00
|
|
|
'McpL',
|
2007-01-10 04:27:40 +00:00
|
|
|
32);
|
|
|
|
|
|
|
|
/* We're done */
|
2007-01-25 17:51:45 +00:00
|
|
|
return TRUE;
|
2000-06-04 17:27:39 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 17:52:40 +00:00
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
LpcpValidateClientPort(
|
|
|
|
PETHREAD ClientThread,
|
|
|
|
PLPCP_PORT_OBJECT Port)
|
|
|
|
{
|
|
|
|
PLPCP_PORT_OBJECT ThreadPort;
|
|
|
|
|
|
|
|
/* Get the thread's port */
|
|
|
|
ThreadPort = LpcpGetPortFromThread(ClientThread);
|
|
|
|
if (ThreadPort == NULL)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the port matches directly */
|
|
|
|
if ((Port == ThreadPort) ||
|
|
|
|
(Port == ThreadPort->ConnectionPort) ||
|
|
|
|
(Port == ThreadPort->ConnectedPort))
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if this is a communication port and the connection port matches */
|
|
|
|
if (((Port->Flags & LPCP_PORT_TYPE_MASK) == LPCP_COMMUNICATION_PORT) &&
|
|
|
|
(Port->ConnectionPort == ThreadPort))
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-10 04:27:40 +00:00
|
|
|
/* PUBLIC FUNCTIONS **********************************************************/
|
2000-06-04 17:27:39 +00:00
|
|
|
|
2007-01-10 04:27:40 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtImpersonateClientOfPort(IN HANDLE PortHandle,
|
|
|
|
IN PPORT_MESSAGE ClientMessage)
|
2000-06-04 17:27:39 +00:00
|
|
|
{
|
2016-11-07 00:55:34 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
|
2014-02-10 17:52:40 +00:00
|
|
|
CLIENT_ID ClientId;
|
|
|
|
ULONG MessageId;
|
|
|
|
PLPCP_PORT_OBJECT Port = NULL, ConnectedPort = NULL;
|
|
|
|
PETHREAD ClientThread = NULL;
|
|
|
|
SECURITY_CLIENT_CONTEXT ClientContext;
|
2016-11-07 00:55:34 +00:00
|
|
|
|
2014-02-10 17:52:40 +00:00
|
|
|
PAGED_CODE();
|
|
|
|
|
2016-11-07 01:24:24 +00:00
|
|
|
/* Check if the call comes from user mode */
|
|
|
|
if (PreviousMode != KernelMode)
|
2014-02-10 17:52:40 +00:00
|
|
|
{
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
ProbeForRead(ClientMessage, sizeof(*ClientMessage), sizeof(PVOID));
|
2016-11-07 01:24:24 +00:00
|
|
|
ClientId = ((volatile PORT_MESSAGE*)ClientMessage)->ClientId;
|
|
|
|
MessageId = ((volatile PORT_MESSAGE*)ClientMessage)->MessageId;
|
2014-02-10 17:52:40 +00:00
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
2016-09-07 18:52:09 +00:00
|
|
|
_SEH2_YIELD(return _SEH2_GetExceptionCode());
|
2014-02-10 17:52:40 +00:00
|
|
|
}
|
|
|
|
_SEH2_END;
|
|
|
|
}
|
2016-11-07 01:24:24 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ClientId = ClientMessage->ClientId;
|
|
|
|
MessageId = ClientMessage->MessageId;
|
|
|
|
}
|
2014-02-10 17:52:40 +00:00
|
|
|
|
|
|
|
/* Reference the port handle */
|
|
|
|
Status = ObReferenceObjectByHandle(PortHandle,
|
|
|
|
PORT_ALL_ACCESS,
|
|
|
|
LpcPortObjectType,
|
|
|
|
PreviousMode,
|
|
|
|
(PVOID*)&Port,
|
|
|
|
NULL);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
DPRINT1("Failed to reference port handle: 0x%ls\n", Status);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure this is a connection port */
|
|
|
|
if ((Port->Flags & LPCP_PORT_TYPE_MASK) != LPCP_COMMUNICATION_PORT)
|
|
|
|
{
|
|
|
|
/* It isn't, fail */
|
|
|
|
DPRINT1("Port is not a communication port\n");
|
|
|
|
Status = STATUS_INVALID_PORT_HANDLE;
|
|
|
|
goto Cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look up the client thread */
|
|
|
|
Status = PsLookupProcessThreadByCid(&ClientId, NULL, &ClientThread);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
DPRINT1("Failed to lookup client thread: 0x%ls\n", Status);
|
|
|
|
goto Cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Acquire the lock */
|
|
|
|
KeAcquireGuardedMutex(&LpcpLock);
|
|
|
|
|
|
|
|
/* Get the connected port and try to reference it */
|
|
|
|
ConnectedPort = Port->ConnectedPort;
|
2016-11-07 00:55:34 +00:00
|
|
|
if ((ConnectedPort == NULL) || !ObReferenceObjectSafe(ConnectedPort))
|
2014-02-10 17:52:40 +00:00
|
|
|
{
|
|
|
|
DPRINT1("Failed to reference the connected port\n");
|
|
|
|
ConnectedPort = NULL;
|
|
|
|
Status = STATUS_PORT_DISCONNECTED;
|
|
|
|
goto CleanupWithLock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for no-impersonation flag */
|
|
|
|
if ((ULONG_PTR)ClientThread->LpcReplyMessage & LPCP_THREAD_FLAG_NO_IMPERSONATION)
|
|
|
|
{
|
|
|
|
DPRINT1("Reply message has no impersonation flag set\n");
|
|
|
|
Status = STATUS_ACCESS_DENIED;
|
|
|
|
goto CleanupWithLock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for message id mismatch */
|
|
|
|
if ((ClientThread->LpcReplyMessageId != MessageId) || (MessageId == 0))
|
|
|
|
{
|
|
|
|
DPRINT1("LpcReplyMessageId mismatch: 0x%lx/0x%lx.\n",
|
|
|
|
ClientThread->LpcReplyMessageId, MessageId);
|
|
|
|
Status = STATUS_REPLY_MESSAGE_MISMATCH;
|
|
|
|
goto CleanupWithLock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Validate the port */
|
|
|
|
if (!LpcpValidateClientPort(ClientThread, Port))
|
|
|
|
{
|
|
|
|
DPRINT1("LpcpValidateClientPort failed\n");
|
|
|
|
Status = STATUS_REPLY_MESSAGE_MISMATCH;
|
|
|
|
goto CleanupWithLock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release the lock */
|
|
|
|
KeReleaseGuardedMutex(&LpcpLock);
|
|
|
|
|
|
|
|
/* Check if security is static */
|
|
|
|
if (!(ConnectedPort->Flags & LPCP_SECURITY_DYNAMIC))
|
|
|
|
{
|
|
|
|
/* Use the static security for impersonation */
|
|
|
|
Status = SeImpersonateClientEx(&ConnectedPort->StaticSecurity, NULL);
|
|
|
|
goto Cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create new dynamic security */
|
|
|
|
Status = SeCreateClientSecurity(ClientThread,
|
|
|
|
&ConnectedPort->SecurityQos,
|
|
|
|
FALSE,
|
|
|
|
&ClientContext);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
DPRINT1("SeCreateClientSecurity failed\n");
|
|
|
|
goto Cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Use dynamic security for impersonation */
|
|
|
|
Status = SeImpersonateClientEx(&ClientContext, NULL);
|
|
|
|
|
|
|
|
/* Get rid of the security context */
|
|
|
|
SeDeleteClientSecurity(&ClientContext);
|
|
|
|
|
|
|
|
Cleanup:
|
|
|
|
|
|
|
|
if (ConnectedPort != NULL)
|
|
|
|
ObDereferenceObject(ConnectedPort);
|
|
|
|
|
|
|
|
if (ClientThread != NULL)
|
|
|
|
ObDereferenceObject(ClientThread);
|
|
|
|
|
|
|
|
ObDereferenceObject(Port);
|
|
|
|
|
|
|
|
return Status;
|
2016-11-07 00:55:34 +00:00
|
|
|
|
|
|
|
CleanupWithLock:
|
|
|
|
|
|
|
|
/* Release the lock */
|
|
|
|
KeReleaseGuardedMutex(&LpcpLock);
|
|
|
|
goto Cleanup;
|
2000-06-04 17:27:39 +00:00
|
|
|
}
|
|
|
|
|
2006-10-23 20:49:52 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtQueryPortInformationProcess(VOID)
|
|
|
|
{
|
|
|
|
/* This is all this function does */
|
|
|
|
return STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
|
|
|
|
2007-01-10 04:27:40 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
NtQueryInformationPort(IN HANDLE PortHandle,
|
|
|
|
IN PORT_INFORMATION_CLASS PortInformationClass,
|
|
|
|
OUT PVOID PortInformation,
|
|
|
|
IN ULONG PortInformationLength,
|
|
|
|
OUT PULONG ReturnLength)
|
|
|
|
{
|
|
|
|
UNIMPLEMENTED;
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2000-06-04 17:27:39 +00:00
|
|
|
/* EOF */
|