dll: MemAlloc/MemFree

This commit is contained in:
Bill Zissimopoulos 2015-12-15 14:32:11 -08:00
parent f3b87304e8
commit 2b1edc8d29
6 changed files with 66 additions and 26 deletions

View File

@ -173,6 +173,7 @@
<ItemGroup>
<ClCompile Include="..\..\src\dll\debug.c" />
<ClCompile Include="..\..\src\dll\fsctl.c" />
<ClCompile Include="..\..\src\dll\library.c" />
<ClCompile Include="..\..\src\dll\loop.c" />
<ClCompile Include="..\..\src\dll\ntstatus.c" />
</ItemGroup>

View File

@ -26,6 +26,9 @@
<ClCompile Include="..\..\src\dll\loop.c">
<Filter>Source</Filter>
</ClCompile>
<ClCompile Include="..\..\src\dll\library.c">
<Filter>Source</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\inc\winfsp\fsctl.h">

View File

@ -11,13 +11,6 @@
#define GLOBALROOT L"\\\\?\\GLOBALROOT"
static inline PVOID Malloc(SIZE_T Size)
{
PVOID P = malloc(Size);
if (0 != P)
SetLastError(ERROR_NO_SYSTEM_RESOURCES);
return P;
}
static inline VOID GlobalDevicePath(PWCHAR DevicePathBuf, SIZE_T DevicePathSize, PWSTR DevicePath)
{
StringCbPrintfW(DevicePathBuf, DevicePathSize,
@ -49,10 +42,10 @@ static NTSTATUS CreateSelfRelativeSecurityDescriptor(PSECURITY_DESCRIPTOR Securi
OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &Token) &&
(GetTokenInformation(Token, TokenUser, 0, 0, &UserSize) ||
ERROR_INSUFFICIENT_BUFFER == GetLastError()) &&
(User = Malloc(UserSize)) &&
(User = MemAllocSLE(UserSize)) &&
GetTokenInformation(Token, TokenUser, User, UserSize, &UserSize) &&
(AclSize = sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(User->User.Sid) - sizeof(DWORD)) &&
(Acl = Malloc(AclSize)) &&
(Acl = MemAllocSLE(AclSize)) &&
InitializeAcl(Acl, AclSize, ACL_REVISION) &&
AddAccessAllowedAce(Acl, ACL_REVISION, FILE_ALL_ACCESS, User->User.Sid) &&
InitializeSecurityDescriptor(&SecurityDescriptorStruct, SECURITY_DESCRIPTOR_REVISION) &&
@ -77,7 +70,7 @@ static NTSTATUS CreateSelfRelativeSecurityDescriptor(PSECURITY_DESCRIPTOR Securi
{
SelfRelativeSecurityDescriptorSize = GetSecurityDescriptorLength(SecurityDescriptor);
Success =
(SelfRelativeSecurityDescriptor = Malloc(SelfRelativeSecurityDescriptorSize)) &&
(SelfRelativeSecurityDescriptor = MemAllocSLE(SelfRelativeSecurityDescriptorSize)) &&
memcpy(SelfRelativeSecurityDescriptor, SecurityDescriptor, SelfRelativeSecurityDescriptorSize);
}
else
@ -86,7 +79,7 @@ static NTSTATUS CreateSelfRelativeSecurityDescriptor(PSECURITY_DESCRIPTOR Securi
Success =
(MakeSelfRelativeSD(SecurityDescriptor, 0, &SelfRelativeSecurityDescriptorSize) ||
ERROR_INSUFFICIENT_BUFFER == GetLastError()) &&
(SelfRelativeSecurityDescriptor = Malloc(SelfRelativeSecurityDescriptorSize)) &&
(SelfRelativeSecurityDescriptor = MemAllocSLE(SelfRelativeSecurityDescriptorSize)) &&
(MakeSelfRelativeSD(SecurityDescriptor, SelfRelativeSecurityDescriptor, &SelfRelativeSecurityDescriptorSize));
}
if (!Success)
@ -103,11 +96,11 @@ exit:
if (0 != Token)
CloseHandle(Token);
free(Acl);
free(User);
MemFree(Acl);
MemFree(User);
if (STATUS_SUCCESS != Result)
free(SelfRelativeSecurityDescriptor);
MemFree(SelfRelativeSecurityDescriptor);
return Result;
}
@ -132,10 +125,10 @@ FSP_API NTSTATUS FspFsctlCreateVolume(PWSTR DevicePath,
if (!NT_SUCCESS(Result))
goto exit;
ParamsBuf = Malloc(FSP_FSCTL_VOLUME_PARAMS_SIZE + SelfRelativeSecurityDescriptorSize);
ParamsBuf = MemAlloc(FSP_FSCTL_VOLUME_PARAMS_SIZE + SelfRelativeSecurityDescriptorSize);
if (0 == ParamsBuf)
{
Result = FspNtStatusFromWin32(GetLastError());
Result = STATUS_INSUFFICIENT_RESOURCES;
goto exit;
}
memset(ParamsBuf, 0, FSP_FSCTL_VOLUME_PARAMS_SIZE);
@ -184,8 +177,8 @@ exit:
if (INVALID_HANDLE_VALUE != DeviceHandle)
CloseHandle(DeviceHandle);
free(ParamsBuf);
free(SelfRelativeSecurityDescriptor);
MemFree(ParamsBuf);
MemFree(SelfRelativeSecurityDescriptor);
return Result;
}

23
src/dll/library.c Normal file
View File

@ -0,0 +1,23 @@
/**
* @file dll/library.c
*
* @copyright 2015 Bill Zissimopoulos
*/
#include <dll/library.h>
HANDLE ProcessHeap;
BOOL WINAPI DllMain(HINSTANCE Instance, DWORD Reason, PVOID Reserved)
{
switch (Reason)
{
case DLL_PROCESS_ATTACH:
ProcessHeap = GetProcessHeap();
if (0 == ProcessHeap)
return FALSE;
break;
}
return TRUE;
}

View File

@ -21,4 +21,24 @@
#define DEBUGLOG(fmt, ...) ((void)0)
#endif
static inline PVOID MemAlloc(SIZE_T Size)
{
extern HANDLE ProcessHeap;
return HeapAlloc(ProcessHeap, 0, Size);
}
static inline PVOID MemAllocSLE(SIZE_T Size)
{
extern HANDLE ProcessHeap;
PVOID Pointer = HeapAlloc(ProcessHeap, 0, Size);
if (0 == Pointer)
SetLastError(ERROR_NO_SYSTEM_RESOURCES);
return Pointer;
}
static inline VOID MemFree(PVOID Pointer)
{
extern HANDLE ProcessHeap;
if (0 != Pointer)
HeapFree(ProcessHeap, 0, Pointer);
}
#endif

View File

@ -26,7 +26,7 @@ FSP_API NTSTATUS FspFileSystemCreate(PWSTR DevicePath,
if (0 == ProcessRequest)
ProcessRequest = FspProcessRequestDirect;
FileSystem = malloc(sizeof *FileSystem);
FileSystem = MemAlloc(sizeof *FileSystem);
if (0 == FileSystem)
{
Result = STATUS_INSUFFICIENT_RESOURCES;
@ -53,7 +53,7 @@ exit:
{
if (INVALID_HANDLE_VALUE != VolumeHandle)
CloseHandle(VolumeHandle);
free(FileSystem);
MemFree(FileSystem);
}
return Result;
@ -63,7 +63,7 @@ FSP_API VOID FspFileSystemDelete(FSP_FILE_SYSTEM *FileSystem)
{
if (INVALID_HANDLE_VALUE != FileSystem->VolumeHandle)
CloseHandle(FileSystem->VolumeHandle);
free(FileSystem);
MemFree(FileSystem);
}
FSP_API NTSTATUS FspFileSystemLoop(FSP_FILE_SYSTEM *FileSystem)
@ -73,7 +73,7 @@ FSP_API NTSTATUS FspFileSystemLoop(FSP_FILE_SYSTEM *FileSystem)
SIZE_T RequestBufSize;
FSP_FSCTL_TRANSACT_REQ *Request, *NextRequest;
RequestBuf = malloc(FSP_FSCTL_TRANSACT_REQ_BUFFER_SIZEMIN);
RequestBuf = MemAlloc(FSP_FSCTL_TRANSACT_REQ_BUFFER_SIZEMIN);
if (0 == RequestBuf)
return STATUS_INSUFFICIENT_RESOURCES;
@ -101,7 +101,7 @@ FSP_API NTSTATUS FspFileSystemLoop(FSP_FILE_SYSTEM *FileSystem)
}
exit:
free(RequestBuf);
MemFree(RequestBuf);
return Result;
}
@ -122,7 +122,7 @@ static DWORD WINAPI FspProcessRequestInPoolWorker(PVOID Param)
FSP_FSCTL_TRANSACT_REQ *Request = (PVOID)WorkItem->RequestBuf;
WorkItem->FileSystem->Operations[Request->Kind](WorkItem->FileSystem, Request);
free(WorkItem);
MemFree(WorkItem);
return 0;
}
@ -136,7 +136,7 @@ FSP_API NTSTATUS FspProcessRequestInPool(FSP_FILE_SYSTEM *FileSystem,
FSP_WORK_ITEM *WorkItem;
BOOLEAN Success;
WorkItem = malloc(sizeof *WorkItem + Request->Size);
WorkItem = MemAlloc(sizeof *WorkItem + Request->Size);
if (0 == WorkItem)
return STATUS_INSUFFICIENT_RESOURCES;
@ -146,7 +146,7 @@ FSP_API NTSTATUS FspProcessRequestInPool(FSP_FILE_SYSTEM *FileSystem,
Success = QueueUserWorkItem(FspProcessRequestInPoolWorker, WorkItem, WT_EXECUTEDEFAULT);
if (!Success)
{
free(WorkItem);
MemFree(WorkItem);
return FspNtStatusFromWin32(GetLastError());
}