Merge pull request #2472 from hardening/winpr_ops_rebased

Winpr ops rebased
This commit is contained in:
Marc-André Moreau 2015-03-16 07:48:06 -04:00
commit 6e4898901e
14 changed files with 119 additions and 55 deletions

View File

@ -1218,6 +1218,13 @@ void _comm_setServerSerialDriver(HANDLE hComm, SERIAL_DRIVER_ID driverId)
pComm->serverSerialDriverId = driverId; pComm->serverSerialDriverId = driverId;
} }
static HANDLE_OPS ops = {
CommIsHandled,
CommCloseHandle,
CommGetFd,
NULL /* CleanupHandle */
};
/** /**
* http://msdn.microsoft.com/en-us/library/windows/desktop/aa363198%28v=vs.85%29.aspx * http://msdn.microsoft.com/en-us/library/windows/desktop/aa363198%28v=vs.85%29.aspx
@ -1320,9 +1327,7 @@ HANDLE CommCreateFileA(LPCSTR lpDeviceName, DWORD dwDesiredAccess, DWORD dwShare
WINPR_HANDLE_SET_TYPE(pComm, HANDLE_TYPE_COMM); WINPR_HANDLE_SET_TYPE(pComm, HANDLE_TYPE_COMM);
pComm->cb.GetFd = CommGetFd; pComm->ops = &ops;
pComm->cb.CloseHandle = CommCloseHandle;
pComm->cb.IsHandled = CommIsHandled;
/* error_handle */ /* error_handle */

View File

@ -343,6 +343,13 @@ int InstallAioSignalHandler()
#endif /* HAVE_AIO_H */ #endif /* HAVE_AIO_H */
static HANDLE_OPS ops = {
FileIsHandled,
FileCloseHandle,
FileGetFd,
NULL /* CleanupHandle */
};
HANDLE CreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, HANDLE CreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile) DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
{ {
@ -417,9 +424,7 @@ HANDLE CreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode,
strcpy(s.sun_path, pNamedPipe->lpFilePath); strcpy(s.sun_path, pNamedPipe->lpFilePath);
status = connect(pNamedPipe->clientfd, (struct sockaddr*) &s, sizeof(struct sockaddr_un)); status = connect(pNamedPipe->clientfd, (struct sockaddr*) &s, sizeof(struct sockaddr_un));
pNamedPipe->cb.IsHandled = FileIsHandled; pNamedPipe->ops = &ops;
pNamedPipe->cb.CloseHandle = FileCloseHandle;
pNamedPipe->cb.GetFd = FileGetFd;
if (status != 0) if (status != 0)
{ {

View File

@ -51,8 +51,14 @@ BOOL CloseHandle(HANDLE hObject)
if (!winpr_Handle_GetInfo(hObject, &Type, (PVOID*)&Object)) if (!winpr_Handle_GetInfo(hObject, &Type, (PVOID*)&Object))
return FALSE; return FALSE;
if (Object && Object->cb.CloseHandle) if (!Object)
return Object->cb.CloseHandle(hObject); return FALSE;
if (!Object->ops)
return FALSE;
if(Object->ops->CloseHandle)
return Object->ops->CloseHandle(hObject);
return FALSE; return FALSE;
} }

View File

@ -41,20 +41,20 @@
#define WINPR_HANDLE_DEF() \ #define WINPR_HANDLE_DEF() \
ULONG Type; \ ULONG Type; \
HANDLE_CLOSE_CB cb HANDLE_OPS *ops
typedef BOOL (*pcIsHandled)(HANDLE handle); typedef BOOL (*pcIsHandled)(HANDLE handle);
typedef BOOL (*pcCloseHandle)(HANDLE handle); typedef BOOL (*pcCloseHandle)(HANDLE handle);
typedef int (*pcGetFd)(HANDLE handle); typedef int (*pcGetFd)(HANDLE handle);
typedef DWORD (*pcCleanupHandle)(HANDLE handle); typedef DWORD (*pcCleanupHandle)(HANDLE handle);
typedef struct _HANDLE_CLOSE_CB typedef struct _HANDLE_OPS
{ {
pcIsHandled IsHandled; pcIsHandled IsHandled;
pcCloseHandle CloseHandle; pcCloseHandle CloseHandle;
pcGetFd GetFd; pcGetFd GetFd;
pcCleanupHandle CleanupHandle; pcCleanupHandle CleanupHandle;
} HANDLE_CLOSE_CB; } HANDLE_OPS;
struct winpr_handle struct winpr_handle
{ {
@ -88,10 +88,10 @@ static INLINE int winpr_Handle_getFd(HANDLE handle)
if (!winpr_Handle_GetInfo(handle, &type, (PVOID*)&hdl)) if (!winpr_Handle_GetInfo(handle, &type, (PVOID*)&hdl))
return -1; return -1;
if (!hdl || !hdl->cb.GetFd) if (!hdl || !hdl->ops->GetFd)
return -1; return -1;
return hdl->cb.GetFd(handle); return hdl->ops->GetFd(handle);
} }
static INLINE DWORD winpr_Handle_cleanup(HANDLE handle) static INLINE DWORD winpr_Handle_cleanup(HANDLE handle)
@ -106,10 +106,10 @@ static INLINE DWORD winpr_Handle_cleanup(HANDLE handle)
return WAIT_FAILED; return WAIT_FAILED;
/* If there is no cleanup function, assume all ok. */ /* If there is no cleanup function, assume all ok. */
if (!hdl->cb.CleanupHandle) if (!hdl->ops->CleanupHandle)
return WAIT_OBJECT_0; return WAIT_OBJECT_0;
return hdl->cb.CleanupHandle(handle); return hdl->ops->CleanupHandle(handle);
} }
#endif /* WINPR_HANDLE_PRIVATE_H */ #endif /* WINPR_HANDLE_PRIVATE_H */

View File

@ -56,6 +56,13 @@ static int NoneHandleGetFd(HANDLE handle)
return -1; return -1;
} }
static HANDLE_OPS ops = {
NoneHandleIsHandle,
NoneHandleCloseHandle,
NoneHandleGetFd,
NULL /* CleanupHandle */
};
HANDLE CreateNoneHandle() HANDLE CreateNoneHandle()
{ {
WINPR_NONE_HANDLE* none; WINPR_NONE_HANDLE* none;
@ -64,9 +71,7 @@ HANDLE CreateNoneHandle()
if (!none) if (!none)
return NULL; return NULL;
none->cb.IsHandled = NoneHandleIsHandle; none->ops = &ops;
none->cb.CloseHandle = NoneHandleCloseHandle;
none->cb.GetFd = NoneHandleGetFd;
return (HANDLE)none; return (HANDLE)none;
} }

View File

@ -105,6 +105,7 @@ BOOL PipeCloseHandle(HANDLE handle) {
if (pipe->fd != -1) if (pipe->fd != -1)
{ {
close(pipe->fd); close(pipe->fd);
pipe->fd = -1;
} }
free(handle); free(handle);
@ -175,6 +176,13 @@ static void InitWinPRPipeModule()
g_NamedPipeServerSockets = ArrayList_New(FALSE); g_NamedPipeServerSockets = ArrayList_New(FALSE);
} }
static HANDLE_OPS ops = {
PipeIsHandled,
PipeCloseHandle,
PipeGetFd,
NULL /* CleanupHandle */
};
/* /*
* Unnamed pipe * Unnamed pipe
@ -211,15 +219,11 @@ BOOL CreatePipe(PHANDLE hReadPipe, PHANDLE hWritePipe, LPSECURITY_ATTRIBUTES lpP
pReadPipe->fd = pipe_fd[0]; pReadPipe->fd = pipe_fd[0];
pWritePipe->fd = pipe_fd[1]; pWritePipe->fd = pipe_fd[1];
WINPR_HANDLE_SET_TYPE(pReadPipe, HANDLE_TYPE_ANONYMOUS_PIPE); WINPR_HANDLE_SET_TYPE(pReadPipe, HANDLE_TYPE_ANONYMOUS_PIPE);
pReadPipe->cb.GetFd = PipeGetFd; pReadPipe->ops = &ops;
pReadPipe->cb.CloseHandle = PipeCloseHandle;
pReadPipe->cb.IsHandled = PipeIsHandled;
*((ULONG_PTR*) hReadPipe) = (ULONG_PTR) pReadPipe; *((ULONG_PTR*) hReadPipe) = (ULONG_PTR) pReadPipe;
WINPR_HANDLE_SET_TYPE(pWritePipe, HANDLE_TYPE_ANONYMOUS_PIPE); WINPR_HANDLE_SET_TYPE(pWritePipe, HANDLE_TYPE_ANONYMOUS_PIPE);
pWritePipe->cb.GetFd = PipeGetFd; pWritePipe->ops = &ops;
pWritePipe->cb.CloseHandle = PipeCloseHandle;
pWritePipe->cb.IsHandled = PipeIsHandled;
*((ULONG_PTR*) hWritePipe) = (ULONG_PTR) pWritePipe; *((ULONG_PTR*) hWritePipe) = (ULONG_PTR) pWritePipe;
return TRUE; return TRUE;
} }
@ -269,6 +273,13 @@ static void winpr_unref_named_pipe(WINPR_NAMED_PIPE* pNamedPipe)
ArrayList_Unlock(g_NamedPipeServerSockets); ArrayList_Unlock(g_NamedPipeServerSockets);
} }
static HANDLE_OPS namedOps = {
NamedPipeIsHandled,
NamedPipeCloseHandle,
NamedPipeGetFd,
NULL /* CleanupHandle */
};
HANDLE CreateNamedPipeA(LPCSTR lpName, DWORD dwOpenMode, DWORD dwPipeMode, DWORD nMaxInstances, HANDLE CreateNamedPipeA(LPCSTR lpName, DWORD dwOpenMode, DWORD dwPipeMode, DWORD nMaxInstances,
DWORD nOutBufferSize, DWORD nInBufferSize, DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES lpSecurityAttributes) DWORD nOutBufferSize, DWORD nInBufferSize, DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES lpSecurityAttributes)
{ {
@ -310,9 +321,7 @@ HANDLE CreateNamedPipeA(LPCSTR lpName, DWORD dwOpenMode, DWORD dwPipeMode, DWORD
pNamedPipe->dwFlagsAndAttributes = dwOpenMode; pNamedPipe->dwFlagsAndAttributes = dwOpenMode;
pNamedPipe->clientfd = -1; pNamedPipe->clientfd = -1;
pNamedPipe->ServerMode = TRUE; pNamedPipe->ServerMode = TRUE;
pNamedPipe->cb.GetFd = NamedPipeGetFd; pNamedPipe->ops = &namedOps;
pNamedPipe->cb.CloseHandle = NamedPipeCloseHandle;
pNamedPipe->cb.IsHandled = NamedPipeIsHandled;
ArrayList_Lock(g_NamedPipeServerSockets); ArrayList_Lock(g_NamedPipeServerSockets);
for (index = 0; index < ArrayList_Count(g_NamedPipeServerSockets); index++) for (index = 0; index < ArrayList_Count(g_NamedPipeServerSockets); index++)

View File

@ -402,7 +402,7 @@ static void* named_pipe_single_thread(void* arg)
goto out; goto out;
} }
if (WriteFile(clients[i], sndbuf, sizeof(sndbuf), &dwWritten, NULL)) if (WriteFile(servers[i], sndbuf, sizeof(sndbuf), &dwWritten, NULL))
{ {
printf("%s: Error WriteFile on server end should have failed after CloseHandle on client\n", __FUNCTION__); printf("%s: Error WriteFile on server end should have failed after CloseHandle on client\n", __FUNCTION__);
goto out; goto out;

View File

@ -114,6 +114,13 @@ BOOL LogonUserCloseHandle(HANDLE handle) {
return TRUE; return TRUE;
} }
static HANDLE_OPS ops = {
LogonUserIsHandled,
LogonUserCloseHandle,
LogonUserGetFd,
NULL /* CleanupHandle */
};
BOOL LogonUserA(LPCSTR lpszUsername, LPCSTR lpszDomain, LPCSTR lpszPassword, BOOL LogonUserA(LPCSTR lpszUsername, LPCSTR lpszDomain, LPCSTR lpszPassword,
DWORD dwLogonType, DWORD dwLogonProvider, PHANDLE phToken) DWORD dwLogonType, DWORD dwLogonProvider, PHANDLE phToken)
{ {
@ -130,9 +137,7 @@ BOOL LogonUserA(LPCSTR lpszUsername, LPCSTR lpszDomain, LPCSTR lpszPassword,
WINPR_HANDLE_SET_TYPE(token, HANDLE_TYPE_ACCESS_TOKEN); WINPR_HANDLE_SET_TYPE(token, HANDLE_TYPE_ACCESS_TOKEN);
token->cb.GetFd = LogonUserGetFd; token->ops = &ops;
token->cb.CloseHandle = LogonUserCloseHandle;
token->cb.IsHandled = LogonUserIsHandled;
token->Username = _strdup(lpszUsername); token->Username = _strdup(lpszUsername);

View File

@ -96,6 +96,13 @@ BOOL EventCloseHandle(HANDLE handle) {
return TRUE; return TRUE;
} }
static HANDLE_OPS ops = {
EventIsHandled,
EventCloseHandle,
EventGetFd,
NULL /* CleanupHandle */
};
HANDLE CreateEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, LPCWSTR lpName) HANDLE CreateEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, LPCWSTR lpName)
{ {
WINPR_EVENT* event; WINPR_EVENT* event;
@ -105,9 +112,7 @@ HANDLE CreateEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset,
{ {
event->bAttached = FALSE; event->bAttached = FALSE;
event->bManualReset = bManualReset; event->bManualReset = bManualReset;
event->cb.IsHandled = EventIsHandled; event->ops = &ops;
event->cb.CloseHandle = EventCloseHandle;
event->cb.GetFd = EventGetFd;
if (!event->bManualReset) if (!event->bManualReset)
{ {
@ -271,6 +276,7 @@ BOOL ResetEvent(HANDLE hEvent)
#endif #endif
HANDLE CreateFileDescriptorEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, int FileDescriptor) HANDLE CreateFileDescriptorEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, int FileDescriptor)
{ {
#ifndef _WIN32 #ifndef _WIN32
@ -284,9 +290,7 @@ HANDLE CreateFileDescriptorEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL
event->bManualReset = bManualReset; event->bManualReset = bManualReset;
event->pipe_fd[0] = FileDescriptor; event->pipe_fd[0] = FileDescriptor;
event->pipe_fd[1] = -1; event->pipe_fd[1] = -1;
event->cb.CloseHandle = EventCloseHandle; event->ops = &ops;
event->cb.GetFd = EventGetFd;
event->cb.IsHandled = EventIsHandled;
WINPR_HANDLE_SET_TYPE(event, HANDLE_TYPE_EVENT); WINPR_HANDLE_SET_TYPE(event, HANDLE_TYPE_EVENT);
handle = (HANDLE) event; handle = (HANDLE) event;
} }

View File

@ -68,6 +68,13 @@ BOOL MutexCloseHandle(HANDLE handle) {
return TRUE; return TRUE;
} }
static HANDLE_OPS ops = {
MutexIsHandled,
MutexCloseHandle,
MutexGetFd,
NULL /* CleanupHandle */
};
HANDLE CreateMutexW(LPSECURITY_ATTRIBUTES lpMutexAttributes, BOOL bInitialOwner, LPCWSTR lpName) HANDLE CreateMutexW(LPSECURITY_ATTRIBUTES lpMutexAttributes, BOOL bInitialOwner, LPCWSTR lpName)
{ {
HANDLE handle = NULL; HANDLE handle = NULL;
@ -80,9 +87,7 @@ HANDLE CreateMutexW(LPSECURITY_ATTRIBUTES lpMutexAttributes, BOOL bInitialOwner,
pthread_mutex_init(&mutex->mutex, 0); pthread_mutex_init(&mutex->mutex, 0);
WINPR_HANDLE_SET_TYPE(mutex, HANDLE_TYPE_MUTEX); WINPR_HANDLE_SET_TYPE(mutex, HANDLE_TYPE_MUTEX);
mutex->cb.GetFd = MutexGetFd; mutex->ops = &ops;
mutex->cb.CloseHandle = MutexCloseHandle;
mutex->cb.IsHandled = MutexIsHandled;
handle = (HANDLE) mutex; handle = (HANDLE) mutex;

View File

@ -111,6 +111,13 @@ BOOL SemaphoreCloseHandle(HANDLE handle) {
return TRUE; return TRUE;
} }
static HANDLE_OPS ops = {
SemaphoreIsHandled,
SemaphoreCloseHandle,
SemaphoreGetFd,
SemaphoreCleanupHandle
};
HANDLE CreateSemaphoreW(LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, LONG lInitialCount, LONG lMaximumCount, LPCWSTR lpName) HANDLE CreateSemaphoreW(LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, LONG lInitialCount, LONG lMaximumCount, LPCWSTR lpName)
{ {
HANDLE handle; HANDLE handle;
@ -124,10 +131,7 @@ HANDLE CreateSemaphoreW(LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, LONG lIniti
semaphore->pipe_fd[0] = -1; semaphore->pipe_fd[0] = -1;
semaphore->pipe_fd[0] = -1; semaphore->pipe_fd[0] = -1;
semaphore->sem = (winpr_sem_t*) NULL; semaphore->sem = (winpr_sem_t*) NULL;
semaphore->cb.IsHandled = SemaphoreIsHandled; semaphore->ops = &ops;
semaphore->cb.CloseHandle = SemaphoreCloseHandle;
semaphore->cb.GetFd = SemaphoreGetFd;
semaphore->cb.CleanupHandle = SemaphoreCleanupHandle;
if (semaphore) if (semaphore)
{ {
@ -220,6 +224,7 @@ BOOL ReleaseSemaphore(HANDLE hSemaphore, LONG lReleaseCount, LPLONG lpPreviousCo
return TRUE; return TRUE;
} }
WLog_ERR(TAG, "calling %s on a handle that is not a semaphore");
return FALSE; return FALSE;
} }

View File

@ -210,6 +210,14 @@ int InitializeWaitableTimer(WINPR_TIMER* timer)
return 0; return 0;
} }
static HANDLE_OPS ops = {
TimerIsHandled,
TimerCloseHandle,
TimerGetFd,
TimerCleanupHandle
};
/** /**
* Waitable Timer * Waitable Timer
*/ */
@ -230,10 +238,7 @@ HANDLE CreateWaitableTimerA(LPSECURITY_ATTRIBUTES lpTimerAttributes, BOOL bManua
timer->pfnCompletionRoutine = NULL; timer->pfnCompletionRoutine = NULL;
timer->lpArgToCompletionRoutine = NULL; timer->lpArgToCompletionRoutine = NULL;
timer->bInit = FALSE; timer->bInit = FALSE;
timer->cb.GetFd = TimerGetFd; timer->ops = &ops;
timer->cb.CloseHandle = TimerCloseHandle;
timer->cb.IsHandled = TimerIsHandled;
timer->cb.CleanupHandle = TimerCleanupHandle;
} }
return handle; return handle;

View File

@ -487,6 +487,13 @@ static int ProcessGetFd(HANDLE handle)
return -1; return -1;
} }
static HANDLE_OPS ops = {
ProcessHandleIsHandle,
ProcessHandleCloseHandle,
ProcessGetFd,
NULL /* CleanupHandle */
};
HANDLE CreateProcessHandle(pid_t pid) HANDLE CreateProcessHandle(pid_t pid)
{ {
WINPR_PROCESS* process; WINPR_PROCESS* process;
@ -497,9 +504,7 @@ HANDLE CreateProcessHandle(pid_t pid)
process->pid = pid; process->pid = pid;
process->Type = HANDLE_TYPE_PROCESS; process->Type = HANDLE_TYPE_PROCESS;
process->cb.GetFd = ProcessGetFd; process->ops = &ops;
process->cb.CloseHandle = ProcessHandleCloseHandle;
process->cb.IsHandled = ProcessHandleIsHandle;
return (HANDLE)process; return (HANDLE)process;
} }

View File

@ -151,6 +151,14 @@ static DWORD ThreadCleanupHandle(HANDLE handle)
return WAIT_OBJECT_0; return WAIT_OBJECT_0;
} }
static HANDLE_OPS ops = {
ThreadIsHandled,
ThreadCloseHandle,
ThreadGetFd,
ThreadCleanupHandle
};
static void dump_thread(WINPR_THREAD* thread) static void dump_thread(WINPR_THREAD* thread)
{ {
#if defined(WITH_DEBUG_THREADS) #if defined(WITH_DEBUG_THREADS)
@ -339,10 +347,7 @@ HANDLE CreateThread(LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize
thread->lpParameter = lpParameter; thread->lpParameter = lpParameter;
thread->lpStartAddress = lpStartAddress; thread->lpStartAddress = lpStartAddress;
thread->lpThreadAttributes = lpThreadAttributes; thread->lpThreadAttributes = lpThreadAttributes;
thread->cb.IsHandled = ThreadIsHandled; thread->ops = &ops;
thread->cb.CloseHandle = ThreadCloseHandle;
thread->cb.GetFd = ThreadGetFd;
thread->cb.CleanupHandle = ThreadCleanupHandle;
#if defined(WITH_DEBUG_THREADS) #if defined(WITH_DEBUG_THREADS)
thread->create_stack = winpr_backtrace(20); thread->create_stack = winpr_backtrace(20);