From 435bc185b95d78c5bbb0af08a2d6c447bb8b53b9 Mon Sep 17 00:00:00 2001 From: Adrien JUND Date: Tue, 7 Apr 2020 10:12:28 +0200 Subject: [PATCH] lib - Add DispatchGetEventInformationLength to ensure we send at least EVENT_INFORMATION buffer size. --- dokan/cleanup.c | 2 +- dokan/close.c | 2 +- dokan/directory.c | 5 ++--- dokan/dokan.c | 8 ++++++++ dokan/dokani.h | 2 ++ dokan/fileinfo.c | 6 ++---- dokan/flush.c | 2 +- dokan/lock.c | 2 +- dokan/read.c | 6 ++---- dokan/security.c | 8 +++----- dokan/setfile.c | 5 +++-- dokan/volume.c | 4 ++-- dokan/write.c | 2 +- 13 files changed, 29 insertions(+), 25 deletions(-) diff --git a/dokan/cleanup.c b/dokan/cleanup.c index 29a8daf43..5c943c968 100644 --- a/dokan/cleanup.c +++ b/dokan/cleanup.c @@ -26,7 +26,7 @@ VOID DispatchCleanup(HANDLE Handle, PEVENT_CONTEXT EventContext, PEVENT_INFORMATION eventInfo; DOKAN_FILE_INFO fileInfo; PDOKAN_OPEN_INFO openInfo; - ULONG sizeOfEventInfo = sizeof(EVENT_INFORMATION); + ULONG sizeOfEventInfo = DispatchGetEventInformationLength(0); CheckFileName(EventContext->Operation.Cleanup.FileName); diff --git a/dokan/close.c b/dokan/close.c index d30650c42..3a15c930b 100644 --- a/dokan/close.c +++ b/dokan/close.c @@ -26,7 +26,7 @@ VOID DispatchClose(HANDLE Handle, PEVENT_CONTEXT EventContext, PEVENT_INFORMATION eventInfo; DOKAN_FILE_INFO fileInfo; PDOKAN_OPEN_INFO openInfo; - ULONG sizeOfEventInfo = sizeof(EVENT_INFORMATION); + ULONG sizeOfEventInfo = DispatchGetEventInformationLength(0); UNREFERENCED_PARAMETER(Handle); diff --git a/dokan/directory.c b/dokan/directory.c index 64455a038..99fabe844 100644 --- a/dokan/directory.c +++ b/dokan/directory.c @@ -525,10 +525,9 @@ VOID DispatchDirectoryInformation(HANDLE Handle, PEVENT_CONTEXT EventContext, PDOKAN_OPEN_INFO openInfo; NTSTATUS status = STATUS_SUCCESS; ULONG fileInfoClass = EventContext->Operation.Directory.FileInformationClass; - ULONG sizeOfEventInfo = sizeof(EVENT_INFORMATION) - 8 + - EventContext->Operation.Directory.BufferLength; - BOOLEAN patternCheck = TRUE; + ULONG sizeOfEventInfo = DispatchGetEventInformationLength( + EventContext->Operation.Directory.BufferLength); CheckFileName(EventContext->Operation.Directory.DirectoryName); diff --git a/dokan/dokan.c b/dokan/dokan.c index cd9e2aa25..3f1eaa538 100644 --- a/dokan/dokan.c +++ b/dokan/dokan.c @@ -570,6 +570,14 @@ VOID CheckFileName(LPWSTR FileName) { FileName[len - 1] = '\0'; } +ULONG DispatchGetEventInformationLength(ULONG bufferSize) { + // EVENT_INFORMATION has a buffer of size 8 already + // we remote it to the struct size and add the requested buffer size + // but we need at least to have enough space to set EVENT_INFORMATION + return max((ULONG)sizeof(EVENT_INFORMATION), + FIELD_OFFSET(EVENT_INFORMATION, Buffer[0]) + bufferSize); +} + PEVENT_INFORMATION DispatchCommon(PEVENT_CONTEXT EventContext, ULONG SizeOfEventInfo, PDOKAN_INSTANCE DokanInstance, PDOKAN_FILE_INFO DokanFileInfo, diff --git a/dokan/dokani.h b/dokan/dokani.h index dd67a8013..7d63f062e 100644 --- a/dokan/dokani.h +++ b/dokan/dokani.h @@ -119,6 +119,8 @@ BOOL IsMountPointDriveLetter(LPCWSTR mountPoint); VOID SendEventInformation(HANDLE Handle, PEVENT_INFORMATION EventInfo, ULONG EventLength, PDOKAN_INSTANCE DokanInstance); +ULONG DispatchGetEventInformationLength(ULONG bufferSize); + PEVENT_INFORMATION DispatchCommon(PEVENT_CONTEXT EventContext, ULONG SizeOfEventInfo, PDOKAN_INSTANCE DokanInstance, PDOKAN_FILE_INFO DokanFileInfo, diff --git a/dokan/fileinfo.c b/dokan/fileinfo.c index 9b7e61a3e..d16d0a88f 100644 --- a/dokan/fileinfo.c +++ b/dokan/fileinfo.c @@ -419,10 +419,8 @@ VOID DispatchQueryInformation(HANDLE Handle, PEVENT_CONTEXT EventContext, ULONG remainingLength; NTSTATUS status = STATUS_INVALID_PARAMETER; PDOKAN_OPEN_INFO openInfo; - ULONG sizeOfEventInfo; - - sizeOfEventInfo = - sizeof(EVENT_INFORMATION) - 8 + EventContext->Operation.File.BufferLength; + ULONG sizeOfEventInfo = DispatchGetEventInformationLength( + EventContext->Operation.File.BufferLength); CheckFileName(EventContext->Operation.File.FileName); diff --git a/dokan/flush.c b/dokan/flush.c index c717e6d9f..0bee8c1d2 100644 --- a/dokan/flush.c +++ b/dokan/flush.c @@ -25,9 +25,9 @@ VOID DispatchFlush(HANDLE Handle, PEVENT_CONTEXT EventContext, PDOKAN_INSTANCE DokanInstance) { DOKAN_FILE_INFO fileInfo; PEVENT_INFORMATION eventInfo; - ULONG sizeOfEventInfo = sizeof(EVENT_INFORMATION); PDOKAN_OPEN_INFO openInfo; NTSTATUS status; + ULONG sizeOfEventInfo = DispatchGetEventInformationLength(0); CheckFileName(EventContext->Operation.Flush.FileName); diff --git a/dokan/lock.c b/dokan/lock.c index 2bb39160f..ce85bbae0 100644 --- a/dokan/lock.c +++ b/dokan/lock.c @@ -26,9 +26,9 @@ VOID DispatchLock(HANDLE Handle, PEVENT_CONTEXT EventContext, PDOKAN_INSTANCE DokanInstance) { DOKAN_FILE_INFO fileInfo; PEVENT_INFORMATION eventInfo; - ULONG sizeOfEventInfo = sizeof(EVENT_INFORMATION); PDOKAN_OPEN_INFO openInfo; NTSTATUS status; + ULONG sizeOfEventInfo = DispatchGetEventInformationLength(0); CheckFileName(EventContext->Operation.Lock.FileName); diff --git a/dokan/read.c b/dokan/read.c index c094237ac..e1406497a 100644 --- a/dokan/read.c +++ b/dokan/read.c @@ -28,10 +28,8 @@ VOID DispatchRead(HANDLE Handle, PEVENT_CONTEXT EventContext, ULONG readLength = 0; NTSTATUS status = STATUS_NOT_IMPLEMENTED; DOKAN_FILE_INFO fileInfo; - ULONG sizeOfEventInfo; - - sizeOfEventInfo = - sizeof(EVENT_INFORMATION) - 8 + EventContext->Operation.Read.BufferLength; + ULONG sizeOfEventInfo = DispatchGetEventInformationLength( + EventContext->Operation.Read.BufferLength); CheckFileName(EventContext->Operation.Read.FileName); diff --git a/dokan/security.c b/dokan/security.c index bcb7ae445..ed7113b28 100644 --- a/dokan/security.c +++ b/dokan/security.c @@ -131,12 +131,11 @@ VOID DispatchQuerySecurity(HANDLE Handle, PEVENT_CONTEXT EventContext, PEVENT_INFORMATION eventInfo; DOKAN_FILE_INFO fileInfo; PDOKAN_OPEN_INFO openInfo; - ULONG eventInfoLength; NTSTATUS status = STATUS_NOT_IMPLEMENTED; ULONG lengthNeeded = 0; + ULONG eventInfoLength = DispatchGetEventInformationLength( + EventContext->Operation.Security.BufferLength); - eventInfoLength = sizeof(EVENT_INFORMATION) - 8 + - EventContext->Operation.Security.BufferLength; CheckFileName(EventContext->Operation.Security.FileName); eventInfo = DispatchCommon(EventContext, eventInfoLength, DokanInstance, @@ -184,11 +183,10 @@ VOID DispatchSetSecurity(HANDLE Handle, PEVENT_CONTEXT EventContext, PEVENT_INFORMATION eventInfo; DOKAN_FILE_INFO fileInfo; PDOKAN_OPEN_INFO openInfo; - ULONG eventInfoLength; NTSTATUS status = STATUS_NOT_IMPLEMENTED; PSECURITY_DESCRIPTOR securityDescriptor; + ULONG eventInfoLength = DispatchGetEventInformationLength(0); - eventInfoLength = sizeof(EVENT_INFORMATION); CheckFileName(EventContext->Operation.SetSecurity.FileName); eventInfo = DispatchCommon(EventContext, eventInfoLength, DokanInstance, diff --git a/dokan/setfile.c b/dokan/setfile.c index 23b04ad66..45f6e0b17 100644 --- a/dokan/setfile.c +++ b/dokan/setfile.c @@ -235,13 +235,14 @@ VOID DispatchSetInformation(HANDLE Handle, PEVENT_CONTEXT EventContext, PDOKAN_OPEN_INFO openInfo; DOKAN_FILE_INFO fileInfo; NTSTATUS status = STATUS_NOT_IMPLEMENTED; - ULONG sizeOfEventInfo = sizeof(EVENT_INFORMATION); + ULONG sizeOfEventInfo = DispatchGetEventInformationLength(0); if (EventContext->Operation.SetFile.FileInformationClass == FileRenameInformation || EventContext->Operation.SetFile.FileInformationClass == FileRenameInformationEx) { PDOKAN_RENAME_INFORMATION renameInfo = (PDOKAN_RENAME_INFORMATION)( (PCHAR)EventContext + EventContext->Operation.SetFile.BufferOffset); - sizeOfEventInfo += renameInfo->FileNameLength; + sizeOfEventInfo = + DispatchGetEventInformationLength(renameInfo->FileNameLength); } CheckFileName(EventContext->Operation.SetFile.FileName); diff --git a/dokan/volume.c b/dokan/volume.c index 291746a46..40a2024e6 100644 --- a/dokan/volume.c +++ b/dokan/volume.c @@ -314,8 +314,8 @@ VOID DispatchQueryVolumeInformation(HANDLE Handle, PEVENT_CONTEXT EventContext, PEVENT_INFORMATION eventInfo; DOKAN_FILE_INFO fileInfo; PDOKAN_OPEN_INFO openInfo; - ULONG sizeOfEventInfo = sizeof(EVENT_INFORMATION) - 8 + - EventContext->Operation.Volume.BufferLength; + ULONG sizeOfEventInfo = DispatchGetEventInformationLength( + EventContext->Operation.Volume.BufferLength); eventInfo = (PEVENT_INFORMATION)malloc(sizeOfEventInfo); if (eventInfo == NULL) { diff --git a/dokan/write.c b/dokan/write.c index e46c91f36..0ce5a3b0b 100644 --- a/dokan/write.c +++ b/dokan/write.c @@ -64,10 +64,10 @@ VOID DispatchWrite(HANDLE Handle, PEVENT_CONTEXT EventContext, NTSTATUS status; DOKAN_FILE_INFO fileInfo; BOOL bufferAllocated = FALSE; - ULONG sizeOfEventInfo = sizeof(EVENT_INFORMATION); ULONG returnedLength = 0; BOOL SendWriteRequestStatus = TRUE; // otherwise DokanInstance->DokanOperations->WriteFile cannot be called DWORD SendWriteRequestLastError = 0; + ULONG sizeOfEventInfo = DispatchGetEventInformationLength(0); eventInfo = DispatchCommon(EventContext, sizeOfEventInfo, DokanInstance, &fileInfo, &openInfo);