core: check the kind of object for events and threads functions

This patch adds some checks for the type of object in Event and Thread functions,
this may help to find bugs where a handle with the wrong type is used as argument
of these functions.
This commit is contained in:
David Fort
2022-09-21 11:01:52 +02:00
committed by akallabeth
parent e266784b24
commit 56204164fe
2 changed files with 36 additions and 11 deletions

View File

@@ -395,16 +395,16 @@ BOOL SetEvent(HANDLE hEvent)
ULONG Type;
WINPR_HANDLE* Object;
WINPR_EVENT* event;
BOOL status = FALSE;
if (winpr_Handle_GetInfo(hEvent, &Type, &Object))
if (!winpr_Handle_GetInfo(hEvent, &Type, &Object) || Type != HANDLE_TYPE_EVENT)
{
event = (WINPR_EVENT*)Object;
status = winpr_event_set(&event->impl);
WLog_ERR(TAG, "SetEvent: hEvent is not an event");
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
return status;
event = (WINPR_EVENT*)Object;
return winpr_event_set(&event->impl);
}
BOOL ResetEvent(HANDLE hEvent)
@@ -413,11 +413,14 @@ BOOL ResetEvent(HANDLE hEvent)
WINPR_HANDLE* Object;
WINPR_EVENT* event;
if (!winpr_Handle_GetInfo(hEvent, &Type, &Object))
if (!winpr_Handle_GetInfo(hEvent, &Type, &Object) || Type != HANDLE_TYPE_EVENT)
{
WLog_ERR(TAG, "ResetEvent: hEvent is not an event");
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
event = (WINPR_EVENT*)Object;
return winpr_event_reset(&event->impl);
}
@@ -481,6 +484,16 @@ HANDLE CreateWaitObjectEvent(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManu
int GetEventFileDescriptor(HANDLE hEvent)
{
#ifndef _WIN32
WINPR_HANDLE* hdl;
ULONG type;
if (!winpr_Handle_GetInfo(hEvent, &type, &hdl) || type != HANDLE_TYPE_EVENT)
{
WLog_ERR(TAG, "GetEventFileDescriptor: hEvent is not an event");
SetLastError(ERROR_INVALID_PARAMETER);
return -1;
}
return winpr_Handle_getFd(hEvent);
#else
return -1;
@@ -499,8 +512,12 @@ int SetEventFileDescriptor(HANDLE hEvent, int FileDescriptor, ULONG mode)
WINPR_HANDLE* Object;
WINPR_EVENT* event;
if (!winpr_Handle_GetInfo(hEvent, &Type, &Object))
if (!winpr_Handle_GetInfo(hEvent, &Type, &Object) || Type != HANDLE_TYPE_EVENT)
{
WLog_ERR(TAG, "SetEventFileDescriptor: hEvent is not an event");
SetLastError(ERROR_INVALID_PARAMETER);
return -1;
}
event = (WINPR_EVENT*)Object;

View File

@@ -794,8 +794,12 @@ BOOL GetExitCodeThread(HANDLE hThread, LPDWORD lpExitCode)
WINPR_HANDLE* Object;
WINPR_THREAD* thread;
if (!winpr_Handle_GetInfo(hThread, &Type, &Object))
if (!winpr_Handle_GetInfo(hThread, &Type, &Object) || Object->Type != HANDLE_TYPE_THREAD)
{
WLog_ERR(TAG, "hThread is not a thread");
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
thread = (WINPR_THREAD*)Object;
*lpExitCode = thread->dwExitCode;
@@ -893,8 +897,12 @@ DWORD ResumeThread(HANDLE hThread)
WINPR_HANDLE* Object;
WINPR_THREAD* thread;
if (!winpr_Handle_GetInfo(hThread, &Type, &Object))
if (!winpr_Handle_GetInfo(hThread, &Type, &Object) || Object->Type != HANDLE_TYPE_THREAD)
{
WLog_ERR(TAG, "hThread is not a thread");
SetLastError(ERROR_INVALID_PARAMETER);
return (DWORD)-1;
}
thread = (WINPR_THREAD*)Object;