[NTOS:LPC]: Improve the lisibility of some functions:

- Group together the local parameters used for capturing user variables;
- Add some space between the list of parameters and the beginning of the code proper;
- Rename some variables or move some code to make some different functions look more similar (helps in finding functionality-similar functions in the code);
- Improve some comments;
- Whitespace fixes;
- Few code formatting.
- Use a ExFreePoolWithTag instead of ExFreePool, but otherwise no other functional code changes.

svn path=/trunk/; revision=73162
This commit is contained in:
Hermès Bélusca-Maïto 2016-11-07 00:55:34 +00:00
parent bb59ec3e6e
commit 06807aee1b
8 changed files with 116 additions and 112 deletions

View file

@ -116,6 +116,8 @@ LpcpDestroyPortQueue(IN PLPCP_PORT_OBJECT Port,
PLPCP_MESSAGE Message; PLPCP_MESSAGE Message;
PLPCP_PORT_OBJECT ConnectionPort = NULL; PLPCP_PORT_OBJECT ConnectionPort = NULL;
PLPCP_CONNECTION_MESSAGE ConnectMessage; PLPCP_CONNECTION_MESSAGE ConnectMessage;
PLPCP_NONPAGED_PORT_QUEUE MessageQueue;
PAGED_CODE(); PAGED_CODE();
LPCTRACE(LPC_CLOSE_DEBUG, "Port: %p. Flags: %lx\n", Port, Port->Flags); LPCTRACE(LPC_CLOSE_DEBUG, "Port: %p. Flags: %lx\n", Port, Port->Flags);
@ -229,9 +231,10 @@ LpcpDestroyPortQueue(IN PLPCP_PORT_OBJECT Port,
if (Port->MsgQueue.Semaphore) if (Port->MsgQueue.Semaphore)
{ {
/* Use the semaphore to find the port queue and free it */ /* Use the semaphore to find the port queue and free it */
ExFreePool(CONTAINING_RECORD(Port->MsgQueue.Semaphore, MessageQueue = CONTAINING_RECORD(Port->MsgQueue.Semaphore,
LPCP_NONPAGED_PORT_QUEUE, LPCP_NONPAGED_PORT_QUEUE,
Semaphore)); Semaphore);
ExFreePoolWithTag(MessageQueue, 'troP');
} }
} }
} }
@ -245,6 +248,7 @@ LpcpClosePort(IN PEPROCESS Process OPTIONAL,
IN ULONG SystemHandleCount) IN ULONG SystemHandleCount)
{ {
PLPCP_PORT_OBJECT Port = (PLPCP_PORT_OBJECT)Object; PLPCP_PORT_OBJECT Port = (PLPCP_PORT_OBJECT)Object;
LPCTRACE(LPC_CLOSE_DEBUG, "Port: %p. Flags: %lx\n", Port, Port->Flags); LPCTRACE(LPC_CLOSE_DEBUG, "Port: %p. Flags: %lx\n", Port, Port->Flags);
/* Only Server-side Connection Ports need clean up*/ /* Only Server-side Connection Ports need clean up*/
@ -305,10 +309,12 @@ LpcpDeletePort(IN PVOID ObjectBody)
PLIST_ENTRY ListHead, NextEntry; PLIST_ENTRY ListHead, NextEntry;
HANDLE Pid; HANDLE Pid;
CLIENT_DIED_MSG ClientDiedMsg; CLIENT_DIED_MSG ClientDiedMsg;
Timeout.QuadPart = -1000000;
PAGED_CODE(); PAGED_CODE();
LPCTRACE(LPC_CLOSE_DEBUG, "Port: %p. Flags: %lx\n", Port, Port->Flags); LPCTRACE(LPC_CLOSE_DEBUG, "Port: %p. Flags: %lx\n", Port, Port->Flags);
Timeout.QuadPart = -1000000;
/* Check if this is a communication port */ /* Check if this is a communication port */
if ((Port->Flags & LPCP_PORT_TYPE_MASK) == LPCP_COMMUNICATION_PORT) if ((Port->Flags & LPCP_PORT_TYPE_MASK) == LPCP_COMMUNICATION_PORT)
{ {
@ -347,8 +353,8 @@ LpcpDeletePort(IN PVOID ObjectBody)
for (;;) for (;;)
{ {
/* Send the message */ /* Send the message */
if (LpcRequestPort(Port, if (LpcRequestPort(Port, &ClientDiedMsg.h) != STATUS_NO_MEMORY)
&ClientDiedMsg.h) != STATUS_NO_MEMORY) break; break;
/* Wait until trying again */ /* Wait until trying again */
KeDelayExecutionThread(KernelMode, FALSE, &Timeout); KeDelayExecutionThread(KernelMode, FALSE, &Timeout);

View file

@ -44,19 +44,20 @@ NtAcceptConnectPort(OUT PHANDLE PortHandle,
IN PPORT_VIEW ServerView, IN PPORT_VIEW ServerView,
IN PREMOTE_PORT_VIEW ClientView) IN PREMOTE_PORT_VIEW ClientView)
{ {
NTSTATUS Status;
KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
ULONG ConnectionInfoLength;
PLPCP_PORT_OBJECT ConnectionPort, ServerPort, ClientPort; PLPCP_PORT_OBJECT ConnectionPort, ServerPort, ClientPort;
PLPCP_CONNECTION_MESSAGE ConnectMessage;
PLPCP_MESSAGE Message;
PVOID ClientSectionToMap = NULL; PVOID ClientSectionToMap = NULL;
HANDLE Handle; HANDLE Handle;
KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
NTSTATUS Status;
ULONG ConnectionInfoLength;
PLPCP_MESSAGE Message;
PLPCP_CONNECTION_MESSAGE ConnectMessage;
PEPROCESS ClientProcess; PEPROCESS ClientProcess;
PETHREAD ClientThread; PETHREAD ClientThread;
LARGE_INTEGER SectionOffset; LARGE_INTEGER SectionOffset;
CLIENT_ID ClientId; CLIENT_ID ClientId;
ULONG MessageId; ULONG MessageId;
PAGED_CODE(); PAGED_CODE();
LPCTRACE(LPC_COMPLETE_DEBUG, LPCTRACE(LPC_COMPLETE_DEBUG,
"Context: %p. Message: %p. Accept: %lx. Views: %p/%p\n", "Context: %p. Message: %p. Accept: %lx. Views: %p/%p\n",
@ -407,9 +408,10 @@ NTAPI
NtCompleteConnectPort(IN HANDLE PortHandle) NtCompleteConnectPort(IN HANDLE PortHandle)
{ {
NTSTATUS Status; NTSTATUS Status;
PLPCP_PORT_OBJECT Port;
KPROCESSOR_MODE PreviousMode = KeGetPreviousMode(); KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
PLPCP_PORT_OBJECT Port;
PETHREAD Thread; PETHREAD Thread;
PAGED_CODE(); PAGED_CODE();
LPCTRACE(LPC_COMPLETE_DEBUG, "Handle: %p\n", PortHandle); LPCTRACE(LPC_COMPLETE_DEBUG, "Handle: %p\n", PortHandle);

View file

@ -87,19 +87,20 @@ NtSecureConnectPort(OUT PHANDLE PortHandle,
IN OUT PVOID ConnectionInformation OPTIONAL, IN OUT PVOID ConnectionInformation OPTIONAL,
IN OUT PULONG ConnectionInformationLength OPTIONAL) IN OUT PULONG ConnectionInformationLength OPTIONAL)
{ {
NTSTATUS Status = STATUS_SUCCESS;
KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
PETHREAD Thread = PsGetCurrentThread();
ULONG ConnectionInfoLength = 0; ULONG ConnectionInfoLength = 0;
PLPCP_PORT_OBJECT Port, ClientPort; PLPCP_PORT_OBJECT Port, ClientPort;
KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
NTSTATUS Status = STATUS_SUCCESS;
HANDLE Handle;
PVOID SectionToMap;
PLPCP_MESSAGE Message; PLPCP_MESSAGE Message;
PLPCP_CONNECTION_MESSAGE ConnectMessage; PLPCP_CONNECTION_MESSAGE ConnectMessage;
PETHREAD Thread = PsGetCurrentThread();
ULONG PortMessageLength; ULONG PortMessageLength;
HANDLE Handle;
PVOID SectionToMap;
LARGE_INTEGER SectionOffset; LARGE_INTEGER SectionOffset;
PTOKEN Token; PTOKEN Token;
PTOKEN_USER TokenUserInfo; PTOKEN_USER TokenUserInfo;
PAGED_CODE(); PAGED_CODE();
LPCTRACE(LPC_CONNECT_DEBUG, LPCTRACE(LPC_CONNECT_DEBUG,
"Name: %wZ. Qos: %p. Views: %p/%p. Sid: %p\n", "Name: %wZ. Qos: %p. Views: %p/%p. Sid: %p\n",
@ -421,15 +422,14 @@ NtSecureConnectPort(OUT PHANDLE PortHandle,
Status); Status);
/* If this is a waitable port, set the event */ /* If this is a waitable port, set the event */
if (Port->Flags & LPCP_WAITABLE_PORT) KeSetEvent(&Port->WaitEvent, if (Port->Flags & LPCP_WAITABLE_PORT)
1, KeSetEvent(&Port->WaitEvent, 1, FALSE);
FALSE);
/* Release the queue semaphore and leave the critical region */ /* Release the queue semaphore and leave the critical region */
LpcpCompleteWait(Port->MsgQueue.Semaphore); LpcpCompleteWait(Port->MsgQueue.Semaphore);
KeLeaveCriticalRegion(); KeLeaveCriticalRegion();
/* Now wait for a reply */ /* Now wait for a reply and set 'Status' */
LpcpConnectWait(&Thread->LpcReplySemaphore, PreviousMode); LpcpConnectWait(&Thread->LpcReplySemaphore, PreviousMode);
} }
@ -478,7 +478,7 @@ NtSecureConnectPort(OUT PHANDLE PortHandle,
NULL, NULL,
PORT_ALL_ACCESS, PORT_ALL_ACCESS,
0, 0,
(PVOID *)NULL, NULL,
&Handle); &Handle);
if (NT_SUCCESS(Status)) if (NT_SUCCESS(Status))
{ {

View file

@ -19,11 +19,12 @@ NTAPI
LpcpInitializePortQueue(IN PLPCP_PORT_OBJECT Port) LpcpInitializePortQueue(IN PLPCP_PORT_OBJECT Port)
{ {
PLPCP_NONPAGED_PORT_QUEUE MessageQueue; PLPCP_NONPAGED_PORT_QUEUE MessageQueue;
PAGED_CODE(); PAGED_CODE();
/* Allocate the queue */ /* Allocate the queue */
MessageQueue = ExAllocatePoolWithTag(NonPagedPool, MessageQueue = ExAllocatePoolWithTag(NonPagedPool,
sizeof(LPCP_NONPAGED_PORT_QUEUE), sizeof(*MessageQueue),
'troP'); 'troP');
if (!MessageQueue) return STATUS_INSUFFICIENT_RESOURCES; if (!MessageQueue) return STATUS_INSUFFICIENT_RESOURCES;
@ -46,12 +47,13 @@ LpcpCreatePort(OUT PHANDLE PortHandle,
IN ULONG MaxPoolUsage, IN ULONG MaxPoolUsage,
IN BOOLEAN Waitable) IN BOOLEAN Waitable)
{ {
KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
NTSTATUS Status; NTSTATUS Status;
KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
PLPCP_PORT_OBJECT Port; PLPCP_PORT_OBJECT Port;
HANDLE Handle; HANDLE Handle;
PUNICODE_STRING ObjectName; PUNICODE_STRING ObjectName;
BOOLEAN NoName; BOOLEAN NoName;
PAGED_CODE(); PAGED_CODE();
LPCTRACE(LPC_CREATE_DEBUG, "Name: %wZ\n", ObjectAttributes->ObjectName); LPCTRACE(LPC_CREATE_DEBUG, "Name: %wZ\n", ObjectAttributes->ObjectName);
@ -170,7 +172,7 @@ LpcpCreatePort(OUT PHANDLE PortHandle,
Port->MaxMessageLength = MaxMessageLength; Port->MaxMessageLength = MaxMessageLength;
/* Insert it now */ /* Insert it now */
Status = ObInsertObject((PVOID)Port, Status = ObInsertObject(Port,
NULL, NULL,
PORT_ALL_ACCESS, PORT_ALL_ACCESS,
0, 0,

View file

@ -23,10 +23,11 @@ NtListenPort(IN HANDLE PortHandle,
OUT PPORT_MESSAGE ConnectMessage) OUT PPORT_MESSAGE ConnectMessage)
{ {
NTSTATUS Status; NTSTATUS Status;
PAGED_CODE(); PAGED_CODE();
LPCTRACE(LPC_LISTEN_DEBUG, "Handle: %p\n", PortHandle); LPCTRACE(LPC_LISTEN_DEBUG, "Handle: %p\n", PortHandle);
/* Wait forever for a connection request. */ /* Wait forever for a connection request */
for (;;) for (;;)
{ {
/* Do the wait */ /* Do the wait */
@ -35,7 +36,7 @@ NtListenPort(IN HANDLE PortHandle,
NULL, NULL,
ConnectMessage); ConnectMessage);
/* Accept only LPC_CONNECTION_REQUEST requests. */ /* Accept only LPC_CONNECTION_REQUEST requests */
if ((Status != STATUS_SUCCESS) || if ((Status != STATUS_SUCCESS) ||
(LpcpGetMessageType(ConnectMessage) == LPC_CONNECTION_REQUEST)) (LpcpGetMessageType(ConnectMessage) == LPC_CONNECTION_REQUEST))
{ {
@ -48,5 +49,4 @@ NtListenPort(IN HANDLE PortHandle,
return Status; return Status;
} }
/* EOF */ /* EOF */

View file

@ -60,6 +60,7 @@ LpcInitSystem(VOID)
NULL, NULL,
&LpcPortObjectType); &LpcPortObjectType);
/* Create the Waitable Port Object Type */
RtlInitUnicodeString(&Name, L"WaitablePort"); RtlInitUnicodeString(&Name, L"WaitablePort");
ObjectTypeInitializer.PoolType = NonPagedPool; ObjectTypeInitializer.PoolType = NonPagedPool;
ObjectTypeInitializer.DefaultNonPagedPoolCharge += sizeof(LPCP_PORT_OBJECT); ObjectTypeInitializer.DefaultNonPagedPoolCharge += sizeof(LPCP_PORT_OBJECT);
@ -125,13 +126,14 @@ NTAPI
NtImpersonateClientOfPort(IN HANDLE PortHandle, NtImpersonateClientOfPort(IN HANDLE PortHandle,
IN PPORT_MESSAGE ClientMessage) IN PPORT_MESSAGE ClientMessage)
{ {
KPROCESSOR_MODE PreviousMode; NTSTATUS Status;
KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
CLIENT_ID ClientId; CLIENT_ID ClientId;
ULONG MessageId; ULONG MessageId;
PLPCP_PORT_OBJECT Port = NULL, ConnectedPort = NULL; PLPCP_PORT_OBJECT Port = NULL, ConnectedPort = NULL;
PETHREAD ClientThread = NULL; PETHREAD ClientThread = NULL;
SECURITY_CLIENT_CONTEXT ClientContext; SECURITY_CLIENT_CONTEXT ClientContext;
NTSTATUS Status;
PAGED_CODE(); PAGED_CODE();
/* Check the previous mode */ /* Check the previous mode */
@ -192,8 +194,7 @@ NtImpersonateClientOfPort(IN HANDLE PortHandle,
/* Get the connected port and try to reference it */ /* Get the connected port and try to reference it */
ConnectedPort = Port->ConnectedPort; ConnectedPort = Port->ConnectedPort;
if ((ConnectedPort == NULL) || if ((ConnectedPort == NULL) || !ObReferenceObjectSafe(ConnectedPort))
!ObReferenceObjectSafe(ConnectedPort))
{ {
DPRINT1("Failed to reference the connected port\n"); DPRINT1("Failed to reference the connected port\n");
ConnectedPort = NULL; ConnectedPort = NULL;
@ -254,13 +255,6 @@ NtImpersonateClientOfPort(IN HANDLE PortHandle,
/* Get rid of the security context */ /* Get rid of the security context */
SeDeleteClientSecurity(&ClientContext); SeDeleteClientSecurity(&ClientContext);
goto Cleanup;
CleanupWithLock:
/* Release the lock */
KeReleaseGuardedMutex(&LpcpLock);
Cleanup: Cleanup:
if (ConnectedPort != NULL) if (ConnectedPort != NULL)
@ -272,6 +266,12 @@ Cleanup:
ObDereferenceObject(Port); ObDereferenceObject(Port);
return Status; return Status;
CleanupWithLock:
/* Release the lock */
KeReleaseGuardedMutex(&LpcpLock);
goto Cleanup;
} }
NTSTATUS NTSTATUS

View file

@ -99,6 +99,7 @@ LpcpFindDataInfoMessage(
{ {
PLPCP_MESSAGE Message; PLPCP_MESSAGE Message;
PLIST_ENTRY ListEntry; PLIST_ENTRY ListEntry;
PAGED_CODE(); PAGED_CODE();
/* Check if the port we want is the connection port */ /* Check if the port we want is the connection port */
@ -141,13 +142,14 @@ LpcpMoveMessage(IN PPORT_MESSAGE Destination,
IN ULONG MessageType, IN ULONG MessageType,
IN PCLIENT_ID ClientId) IN PCLIENT_ID ClientId)
{ {
/* Set the Message size */
LPCTRACE((LPC_REPLY_DEBUG | LPC_SEND_DEBUG), LPCTRACE((LPC_REPLY_DEBUG | LPC_SEND_DEBUG),
"Destination/Origin: %p/%p. Data: %p. Length: %lx\n", "Destination/Origin: %p/%p. Data: %p. Length: %lx\n",
Destination, Destination,
Origin, Origin,
Data, Data,
Origin->u1.Length); Origin->u1.Length);
/* Set the Message size */
Destination->u1.Length = Origin->u1.Length; Destination->u1.Length = Origin->u1.Length;
/* Set the Message Type */ /* Set the Message Type */
@ -188,12 +190,12 @@ NTAPI
NtReplyPort(IN HANDLE PortHandle, NtReplyPort(IN HANDLE PortHandle,
IN PPORT_MESSAGE ReplyMessage) IN PPORT_MESSAGE ReplyMessage)
{ {
PLPCP_PORT_OBJECT Port;
KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
NTSTATUS Status; NTSTATUS Status;
KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
// PORT_MESSAGE CapturedReplyMessage;
PLPCP_PORT_OBJECT Port;
PLPCP_MESSAGE Message; PLPCP_MESSAGE Message;
PETHREAD Thread = PsGetCurrentThread(), WakeupThread; PETHREAD Thread = PsGetCurrentThread(), WakeupThread;
//PORT_MESSAGE CapturedReplyMessage;
PAGED_CODE(); PAGED_CODE();
LPCTRACE(LPC_REPLY_DEBUG, LPCTRACE(LPC_REPLY_DEBUG,
@ -274,8 +276,8 @@ NtReplyPort(IN HANDLE PortHandle,
/* Make sure this is the reply the thread is waiting for */ /* Make sure this is the reply the thread is waiting for */
if ((WakeupThread->LpcReplyMessageId != ReplyMessage->MessageId) || if ((WakeupThread->LpcReplyMessageId != ReplyMessage->MessageId) ||
((LpcpGetMessageFromThread(WakeupThread)) && ((LpcpGetMessageFromThread(WakeupThread)) &&
(LpcpGetMessageType(&LpcpGetMessageFromThread(WakeupThread)-> (LpcpGetMessageType(&LpcpGetMessageFromThread(WakeupThread)-> Request)
Request) != LPC_REQUEST))) != LPC_REQUEST)))
{ {
/* It isn't, fail */ /* It isn't, fail */
LpcpFreeToPortZone(Message, LPCP_LOCK_HELD | LPCP_LOCK_RELEASE); LpcpFreeToPortZone(Message, LPCP_LOCK_HELD | LPCP_LOCK_RELEASE);
@ -295,7 +297,7 @@ NtReplyPort(IN HANDLE PortHandle,
} }
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
{ {
/* Fail */ /* Cleanup and return the exception code */
LpcpFreeToPortZone(Message, LPCP_LOCK_HELD | LPCP_LOCK_RELEASE); LpcpFreeToPortZone(Message, LPCP_LOCK_HELD | LPCP_LOCK_RELEASE);
ObDereferenceObject(WakeupThread); ObDereferenceObject(WakeupThread);
ObDereferenceObject(Port); ObDereferenceObject(Port);
@ -358,15 +360,15 @@ NtReplyWaitReceivePortEx(IN HANDLE PortHandle,
OUT PPORT_MESSAGE ReceiveMessage, OUT PPORT_MESSAGE ReceiveMessage,
IN PLARGE_INTEGER Timeout OPTIONAL) IN PLARGE_INTEGER Timeout OPTIONAL)
{ {
PLPCP_PORT_OBJECT Port, ReceivePort, ConnectionPort = NULL;
KPROCESSOR_MODE PreviousMode = KeGetPreviousMode(), WaitMode = PreviousMode;
NTSTATUS Status; NTSTATUS Status;
KPROCESSOR_MODE PreviousMode = KeGetPreviousMode(), WaitMode = PreviousMode;
// PORT_MESSAGE CapturedReplyMessage;
LARGE_INTEGER CapturedTimeout;
PLPCP_PORT_OBJECT Port, ReceivePort, ConnectionPort = NULL;
PLPCP_MESSAGE Message; PLPCP_MESSAGE Message;
PETHREAD Thread = PsGetCurrentThread(), WakeupThread; PETHREAD Thread = PsGetCurrentThread(), WakeupThread;
PLPCP_CONNECTION_MESSAGE ConnectMessage; PLPCP_CONNECTION_MESSAGE ConnectMessage;
ULONG ConnectionInfoLength; ULONG ConnectionInfoLength;
//PORT_MESSAGE CapturedReplyMessage;
LARGE_INTEGER CapturedTimeout;
PAGED_CODE(); PAGED_CODE();
LPCTRACE(LPC_REPLY_DEBUG, LPCTRACE(LPC_REPLY_DEBUG,
@ -516,8 +518,8 @@ NtReplyWaitReceivePortEx(IN HANDLE PortHandle,
/* Make sure this is the reply the thread is waiting for */ /* Make sure this is the reply the thread is waiting for */
if ((WakeupThread->LpcReplyMessageId != ReplyMessage->MessageId) || if ((WakeupThread->LpcReplyMessageId != ReplyMessage->MessageId) ||
((LpcpGetMessageFromThread(WakeupThread)) && ((LpcpGetMessageFromThread(WakeupThread)) &&
(LpcpGetMessageType(&LpcpGetMessageFromThread(WakeupThread)-> (LpcpGetMessageType(&LpcpGetMessageFromThread(WakeupThread)->Request)
Request) != LPC_REQUEST))) != LPC_REQUEST)))
{ {
/* It isn't, fail */ /* It isn't, fail */
LpcpFreeToPortZone(Message, LPCP_LOCK_HELD | LPCP_LOCK_RELEASE); LpcpFreeToPortZone(Message, LPCP_LOCK_HELD | LPCP_LOCK_RELEASE);
@ -599,8 +601,7 @@ NtReplyWaitReceivePortEx(IN HANDLE PortHandle,
} }
/* Get the message on the queue */ /* Get the message on the queue */
Message = CONTAINING_RECORD(RemoveHeadList(&ReceivePort-> Message = CONTAINING_RECORD(RemoveHeadList(&ReceivePort->MsgQueue.ReceiveHead),
MsgQueue.ReceiveHead),
LPCP_MESSAGE, LPCP_MESSAGE,
Entry); Entry);
@ -756,17 +757,18 @@ LpcpCopyRequestData(
IN ULONG Index, IN ULONG Index,
IN PVOID Buffer, IN PVOID Buffer,
IN ULONG BufferLength, IN ULONG BufferLength,
OUT PULONG Returnlength) OUT PULONG ReturnLength)
{ {
KPROCESSOR_MODE PreviousMode; NTSTATUS Status;
KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
PORT_MESSAGE CapturedMessage; PORT_MESSAGE CapturedMessage;
PLPCP_PORT_OBJECT Port = NULL; PLPCP_PORT_OBJECT Port = NULL;
PETHREAD ClientThread = NULL; PETHREAD ClientThread = NULL;
SIZE_T LocalReturnlength; SIZE_T LocalReturnLength;
PLPCP_MESSAGE InfoMessage; PLPCP_MESSAGE InfoMessage;
PLPCP_DATA_INFO DataInfo; PLPCP_DATA_INFO DataInfo;
PVOID DataInfoBaseAddress; PVOID DataInfoBaseAddress;
NTSTATUS Status;
PAGED_CODE(); PAGED_CODE();
/* Check the previous mode */ /* Check the previous mode */
@ -889,7 +891,7 @@ LpcpCopyRequestData(
DataInfoBaseAddress, DataInfoBaseAddress,
BufferLength, BufferLength,
PreviousMode, PreviousMode,
&LocalReturnlength); &LocalReturnLength);
} }
else else
{ {
@ -900,7 +902,7 @@ LpcpCopyRequestData(
Buffer, Buffer,
BufferLength, BufferLength,
PreviousMode, PreviousMode,
&LocalReturnlength); &LocalReturnLength);
} }
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
@ -910,16 +912,16 @@ LpcpCopyRequestData(
} }
/* Check if the caller asked to return the copied length */ /* Check if the caller asked to return the copied length */
if (Returnlength != NULL) if (ReturnLength != NULL)
{ {
_SEH2_TRY _SEH2_TRY
{ {
*Returnlength = LocalReturnlength; *ReturnLength = LocalReturnLength;
} }
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
{ {
/* Ignore */ /* Ignore */
DPRINT1("Exception writing Returnlength, ignoring\n"); DPRINT1("Exception writing ReturnLength, ignoring\n");
} }
_SEH2_END; _SEH2_END;
} }
@ -941,7 +943,7 @@ CleanupWithLock:
} }
/* /*
* @unimplemented * @implemented
*/ */
NTSTATUS NTSTATUS
NTAPI NTAPI
@ -963,7 +965,7 @@ NtReadRequestData(IN HANDLE PortHandle,
} }
/* /*
* @unimplemented * @implemented
*/ */
NTSTATUS NTSTATUS
NTAPI NTAPI

View file

@ -179,17 +179,17 @@ LpcRequestWaitReplyPort(IN PVOID PortObject,
IN PPORT_MESSAGE LpcRequest, IN PPORT_MESSAGE LpcRequest,
OUT PPORT_MESSAGE LpcReply) OUT PPORT_MESSAGE LpcReply)
{ {
PLPCP_PORT_OBJECT Port, QueuePort, ReplyPort, ConnectionPort = NULL;
KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
NTSTATUS Status = STATUS_SUCCESS; NTSTATUS Status = STATUS_SUCCESS;
PLPCP_MESSAGE Message; KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
PETHREAD Thread = PsGetCurrentThread(); PETHREAD Thread = PsGetCurrentThread();
PLPCP_PORT_OBJECT Port = (PLPCP_PORT_OBJECT)PortObject;
PLPCP_PORT_OBJECT QueuePort, ReplyPort, ConnectionPort = NULL;
USHORT MessageType;
PLPCP_MESSAGE Message;
BOOLEAN Callback = FALSE; BOOLEAN Callback = FALSE;
PKSEMAPHORE Semaphore; PKSEMAPHORE Semaphore;
USHORT MessageType;
PAGED_CODE();
Port = (PLPCP_PORT_OBJECT)PortObject; PAGED_CODE();
LPCTRACE(LPC_SEND_DEBUG, LPCTRACE(LPC_SEND_DEBUG,
"Port: %p. Messages: %p/%p. Type: %lx\n", "Port: %p. Messages: %p/%p. Type: %lx\n",
@ -205,33 +205,26 @@ LpcRequestWaitReplyPort(IN PVOID PortObject,
MessageType = LpcpGetMessageType(LpcRequest); MessageType = LpcpGetMessageType(LpcRequest);
switch (MessageType) switch (MessageType)
{ {
/* No type */ /* No type, assume LPC request */
case 0: case 0:
/* Assume LPC request */
MessageType = LPC_REQUEST; MessageType = LPC_REQUEST;
break; break;
/* LPC request callback */ /* LPC request callback */
case LPC_REQUEST: case LPC_REQUEST:
/* This is a callback */
Callback = TRUE; Callback = TRUE;
break; break;
/* Anything else */ /* Anything else, nothing to do */
case LPC_CLIENT_DIED: case LPC_CLIENT_DIED:
case LPC_PORT_CLOSED: case LPC_PORT_CLOSED:
case LPC_EXCEPTION: case LPC_EXCEPTION:
case LPC_DEBUG_EVENT: case LPC_DEBUG_EVENT:
case LPC_ERROR_EVENT: case LPC_ERROR_EVENT:
/* Nothing to do */
break; break;
default:
/* Invalid message type */ /* Invalid message type */
default:
return STATUS_INVALID_PARAMETER; return STATUS_INVALID_PARAMETER;
} }
@ -448,15 +441,14 @@ NtRequestPort(IN HANDLE PortHandle,
IN PPORT_MESSAGE LpcRequest) IN PPORT_MESSAGE LpcRequest)
{ {
NTSTATUS Status; NTSTATUS Status;
PLPCP_PORT_OBJECT Port, QueuePort, ConnectionPort = NULL;
ULONG MessageType;
PLPCP_MESSAGE Message;
KPROCESSOR_MODE PreviousMode = KeGetPreviousMode(); KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
PETHREAD Thread = PsGetCurrentThread(); PETHREAD Thread = PsGetCurrentThread();
PORT_MESSAGE CapturedLpcRequest; PORT_MESSAGE CapturedLpcRequest;
PLPCP_PORT_OBJECT Port, QueuePort, ConnectionPort = NULL;
ULONG MessageType;
PLPCP_MESSAGE Message;
PAGED_CODE(); PAGED_CODE();
LPCTRACE(LPC_SEND_DEBUG, LPCTRACE(LPC_SEND_DEBUG,
"Handle: %p. Message: %p. Type: %lx\n", "Handle: %p. Message: %p. Type: %lx\n",
PortHandle, PortHandle,
@ -538,7 +530,7 @@ NtRequestPort(IN HANDLE PortHandle,
} }
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
{ {
/* Fail */ /* Cleanup and return the exception code */
LpcpFreeToPortZone(Message, 0); LpcpFreeToPortZone(Message, 0);
ObDereferenceObject(Port); ObDereferenceObject(Port);
_SEH2_YIELD(return _SEH2_GetExceptionCode()); _SEH2_YIELD(return _SEH2_GetExceptionCode());
@ -578,8 +570,7 @@ NtRequestPort(IN HANDLE PortHandle,
return STATUS_PORT_DISCONNECTED; return STATUS_PORT_DISCONNECTED;
} }
} }
else if ((Port->Flags & LPCP_PORT_TYPE_MASK) != else if ((Port->Flags & LPCP_PORT_TYPE_MASK) != LPCP_COMMUNICATION_PORT)
LPCP_COMMUNICATION_PORT)
{ {
/* Use the connection port for anything but communication ports */ /* Use the connection port for anything but communication ports */
ConnectionPort = QueuePort = Port->ConnectionPort; ConnectionPort = QueuePort = Port->ConnectionPort;
@ -706,17 +697,18 @@ NtRequestWaitReplyPort(IN HANDLE PortHandle,
IN PPORT_MESSAGE LpcRequest, IN PPORT_MESSAGE LpcRequest,
IN OUT PPORT_MESSAGE LpcReply) IN OUT PPORT_MESSAGE LpcReply)
{ {
PORT_MESSAGE LocalLpcRequest; NTSTATUS Status;
PORT_MESSAGE CapturedLpcRequest;
ULONG NumberOfDataEntries; ULONG NumberOfDataEntries;
PLPCP_PORT_OBJECT Port, QueuePort, ReplyPort, ConnectionPort = NULL; PLPCP_PORT_OBJECT Port, QueuePort, ReplyPort, ConnectionPort = NULL;
KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
NTSTATUS Status;
PLPCP_MESSAGE Message; PLPCP_MESSAGE Message;
KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
PETHREAD Thread = PsGetCurrentThread(); PETHREAD Thread = PsGetCurrentThread();
BOOLEAN Callback; BOOLEAN Callback;
PKSEMAPHORE Semaphore; PKSEMAPHORE Semaphore;
ULONG MessageType; ULONG MessageType;
PLPCP_DATA_INFO DataInfo; PLPCP_DATA_INFO DataInfo;
PAGED_CODE(); PAGED_CODE();
LPCTRACE(LPC_SEND_DEBUG, LPCTRACE(LPC_SEND_DEBUG,
"Handle: %p. Messages: %p/%p. Type: %lx\n", "Handle: %p. Messages: %p/%p. Type: %lx\n",
@ -736,7 +728,7 @@ NtRequestWaitReplyPort(IN HANDLE PortHandle,
/* Probe the full request message and copy the base structure */ /* Probe the full request message and copy the base structure */
ProbeForRead(LpcRequest, sizeof(*LpcRequest), sizeof(ULONG)); ProbeForRead(LpcRequest, sizeof(*LpcRequest), sizeof(ULONG));
ProbeForRead(LpcRequest, LpcRequest->u1.s1.TotalLength, sizeof(ULONG)); ProbeForRead(LpcRequest, LpcRequest->u1.s1.TotalLength, sizeof(ULONG));
LocalLpcRequest = *LpcRequest; CapturedLpcRequest = *LpcRequest;
/* Probe the reply message for write */ /* Probe the reply message for write */
ProbeForWrite(LpcReply, sizeof(*LpcReply), sizeof(ULONG)); ProbeForWrite(LpcReply, sizeof(*LpcReply), sizeof(ULONG));
@ -758,7 +750,7 @@ NtRequestWaitReplyPort(IN HANDLE PortHandle,
} }
else else
{ {
LocalLpcRequest = *LpcRequest; CapturedLpcRequest = *LpcRequest;
Status = LpcpVerifyMessageDataInfo(LpcRequest, &NumberOfDataEntries); Status = LpcpVerifyMessageDataInfo(LpcRequest, &NumberOfDataEntries);
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
@ -768,15 +760,15 @@ NtRequestWaitReplyPort(IN HANDLE PortHandle,
} }
/* This flag is undocumented. Remove it before continuing */ /* This flag is undocumented. Remove it before continuing */
LocalLpcRequest.u2.s2.Type &= ~0x4000; CapturedLpcRequest.u2.s2.Type &= ~0x4000;
/* Check if this is an LPC Request */ /* Check if this is an LPC Request */
if (LpcpGetMessageType(&LocalLpcRequest) == LPC_REQUEST) if (LpcpGetMessageType(&CapturedLpcRequest) == LPC_REQUEST)
{ {
/* Then it's a callback */ /* Then it's a callback */
Callback = TRUE; Callback = TRUE;
} }
else if (LpcpGetMessageType(&LocalLpcRequest)) else if (LpcpGetMessageType(&CapturedLpcRequest))
{ {
/* This is a not kernel-mode message */ /* This is a not kernel-mode message */
DPRINT1("Not a kernel-mode message!\n"); DPRINT1("Not a kernel-mode message!\n");
@ -785,24 +777,24 @@ NtRequestWaitReplyPort(IN HANDLE PortHandle,
else else
{ {
/* This is a kernel-mode message without a callback */ /* This is a kernel-mode message without a callback */
LocalLpcRequest.u2.s2.Type |= LPC_REQUEST; CapturedLpcRequest.u2.s2.Type |= LPC_REQUEST;
Callback = FALSE; Callback = FALSE;
} }
/* Get the message type */ /* Get the message type */
MessageType = LocalLpcRequest.u2.s2.Type; MessageType = CapturedLpcRequest.u2.s2.Type;
/* Due to the above probe, we know that TotalLength is positive */ /* Due to the above probe, we know that TotalLength is positive */
ASSERT(LocalLpcRequest.u1.s1.TotalLength >= 0); ASSERT(CapturedLpcRequest.u1.s1.TotalLength >= 0);
/* Validate the length */ /* Validate the length */
if ((((ULONG)(USHORT)LocalLpcRequest.u1.s1.DataLength + sizeof(PORT_MESSAGE)) > if ((((ULONG)(USHORT)CapturedLpcRequest.u1.s1.DataLength + sizeof(PORT_MESSAGE)) >
(ULONG)LocalLpcRequest.u1.s1.TotalLength)) (ULONG)CapturedLpcRequest.u1.s1.TotalLength))
{ {
/* Fail */ /* Fail */
DPRINT1("Invalid message length: %u, %u\n", DPRINT1("Invalid message length: %u, %u\n",
LocalLpcRequest.u1.s1.DataLength, CapturedLpcRequest.u1.s1.DataLength,
LocalLpcRequest.u1.s1.TotalLength); CapturedLpcRequest.u1.s1.TotalLength);
return STATUS_INVALID_PARAMETER; return STATUS_INVALID_PARAMETER;
} }
@ -816,13 +808,13 @@ NtRequestWaitReplyPort(IN HANDLE PortHandle,
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) return Status;
/* Validate the message length */ /* Validate the message length */
if (((ULONG)LocalLpcRequest.u1.s1.TotalLength > Port->MaxMessageLength) || if (((ULONG)CapturedLpcRequest.u1.s1.TotalLength > Port->MaxMessageLength) ||
((ULONG)LocalLpcRequest.u1.s1.TotalLength <= (ULONG)LocalLpcRequest.u1.s1.DataLength)) ((ULONG)CapturedLpcRequest.u1.s1.TotalLength <= (ULONG)CapturedLpcRequest.u1.s1.DataLength))
{ {
/* Fail */ /* Fail */
DPRINT1("Invalid message length: %u, %u\n", DPRINT1("Invalid message length: %u, %u\n",
LocalLpcRequest.u1.s1.DataLength, CapturedLpcRequest.u1.s1.DataLength,
LocalLpcRequest.u1.s1.TotalLength); CapturedLpcRequest.u1.s1.TotalLength);
ObDereferenceObject(Port); ObDereferenceObject(Port);
return STATUS_PORT_MESSAGE_TOO_LONG; return STATUS_PORT_MESSAGE_TOO_LONG;
} }
@ -874,7 +866,7 @@ NtRequestWaitReplyPort(IN HANDLE PortHandle,
} }
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
{ {
/* Fail */ /* Cleanup and return the exception code */
DPRINT1("Got exception!\n"); DPRINT1("Got exception!\n");
LpcpFreeToPortZone(Message, 0); LpcpFreeToPortZone(Message, 0);
ObDereferenceObject(Port); ObDereferenceObject(Port);