Improve formatting

svn path=/trunk/; revision=56457
This commit is contained in:
Timo Kreuzer 2012-05-01 09:26:45 +00:00
parent 2ab0757b32
commit bb3050e722

View file

@ -14,133 +14,144 @@
/* /*
* @implemented * @implemented
*/ */
PVOID APIENTRY PVOID
EngAllocMem(ULONG Flags, APIENTRY
ULONG MemSize, EngAllocMem(
ULONG Tag) ULONG Flags,
ULONG cjMemSize,
ULONG ulTag)
{ {
PVOID newMem; PVOID pvBaseAddress;
newMem = ExAllocatePoolWithTag((Flags & FL_NONPAGED_MEMORY) ? NonPagedPool : PagedPool, pvBaseAddress = ExAllocatePoolWithTag((Flags & FL_NONPAGED_MEMORY) ?
MemSize, NonPagedPool : PagedPool,
Tag); cjMemSize,
ulTag);
if (newMem == NULL) if (pvBaseAddress == NULL)
return NULL; return NULL;
if (Flags & FL_ZERO_MEMORY) if (Flags & FL_ZERO_MEMORY)
RtlZeroMemory(newMem, MemSize); RtlZeroMemory(pvBaseAddress, cjMemSize);
return newMem; return pvBaseAddress;
} }
/* /*
* @implemented * @implemented
*/ */
VOID APIENTRY VOID
EngFreeMem(PVOID Mem) APIENTRY
EngFreeMem(PVOID pvBaseAddress)
{ {
ExFreePool(Mem); ExFreePool(pvBaseAddress);
} }
/* /*
* @implemented * @implemented
*/ */
PVOID APIENTRY PVOID
EngAllocUserMem(SIZE_T cj, ULONG Tag) APIENTRY
EngAllocUserMem(SIZE_T cjSize, ULONG ulTag)
{ {
PVOID NewMem = NULL; PVOID pvBaseAddress = NULL;
NTSTATUS Status; NTSTATUS Status;
SIZE_T MemSize = cj;
Status = ZwAllocateVirtualMemory(NtCurrentProcess(), &NewMem, 0, &MemSize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
&pvBaseAddress,
0,
&cjSize,
MEM_COMMIT | MEM_RESERVE,
PAGE_READWRITE);
if (! NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
return NULL; return NULL;
} }
/* TODO: Add allocation info to AVL tree (stored inside W32PROCESS structure) */ /* TODO: Add allocation info to AVL tree (stored inside W32PROCESS structure) */
//hSecure = EngSecureMem(NewMem, cj); //hSecure = EngSecureMem(pvBaseAddress, cj);
return NewMem; return pvBaseAddress;
} }
/* /*
* @implemented * @implemented
*/ */
VOID APIENTRY VOID
EngFreeUserMem(PVOID pv) APIENTRY
EngFreeUserMem(PVOID pvBaseAddress)
{ {
PVOID BaseAddress = pv; SIZE_T cjSize = 0;
SIZE_T MemSize = 0;
ZwFreeVirtualMemory(NtCurrentProcess(), &BaseAddress, &MemSize, MEM_RELEASE); ZwFreeVirtualMemory(NtCurrentProcess(),
&pvBaseAddress,
&cjSize,
MEM_RELEASE);
/* TODO: Remove allocation info from AVL tree */ /* TODO: Remove allocation info from AVL tree */
} }
PVOID PVOID
APIENTRY APIENTRY
HackSecureVirtualMemory( HackSecureVirtualMemory(
IN PVOID Address, IN PVOID Address,
IN SIZE_T Size, IN SIZE_T Size,
IN ULONG ProbeMode, IN ULONG ProbeMode,
OUT PVOID *SafeAddress) OUT PVOID *SafeAddress)
{ {
NTSTATUS Status = STATUS_SUCCESS; NTSTATUS Status = STATUS_SUCCESS;
PMDL mdl; PMDL pmdl;
LOCK_OPERATION Operation; LOCK_OPERATION Operation;
if (ProbeMode == PAGE_READONLY) Operation = IoReadAccess; if (ProbeMode == PAGE_READONLY) Operation = IoReadAccess;
else if (ProbeMode == PAGE_READWRITE) Operation = IoModifyAccess; else if (ProbeMode == PAGE_READWRITE) Operation = IoModifyAccess;
else return NULL; else return NULL;
mdl = IoAllocateMdl(Address, Size, FALSE, TRUE, NULL); pmdl = IoAllocateMdl(Address, Size, FALSE, TRUE, NULL);
if (mdl == NULL) if (pmdl == NULL)
{ {
return NULL; return NULL;
} }
_SEH2_TRY _SEH2_TRY
{ {
MmProbeAndLockPages(mdl, UserMode, Operation); MmProbeAndLockPages(pmdl, UserMode, Operation);
} }
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
{ {
Status = _SEH2_GetExceptionCode(); Status = _SEH2_GetExceptionCode();
} }
_SEH2_END _SEH2_END
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
IoFreeMdl(mdl); IoFreeMdl(pmdl);
return NULL; return NULL;
} }
*SafeAddress = MmGetSystemAddressForMdlSafe(mdl, NormalPagePriority); *SafeAddress = MmGetSystemAddressForMdlSafe(pmdl, NormalPagePriority);
if(!*SafeAddress) if(!*SafeAddress)
{ {
MmUnlockPages(mdl); MmUnlockPages(pmdl);
IoFreeMdl(mdl); IoFreeMdl(pmdl);
return NULL; return NULL;
} }
return mdl; return pmdl;
} }
VOID VOID
APIENTRY APIENTRY
HackUnsecureVirtualMemory( HackUnsecureVirtualMemory(
IN PVOID SecureHandle) IN PVOID SecureHandle)
{ {
PMDL mdl = (PMDL)SecureHandle; PMDL pmdl = (PMDL)SecureHandle;
MmUnlockPages(mdl); MmUnlockPages(pmdl);
IoFreeMdl(mdl); IoFreeMdl(pmdl);
} }
/* /*
@ -150,7 +161,7 @@ HANDLE APIENTRY
EngSecureMem(PVOID Address, ULONG Length) EngSecureMem(PVOID Address, ULONG Length)
{ {
return (HANDLE)-1; // HACK!!! return (HANDLE)-1; // HACK!!!
return MmSecureVirtualMemory(Address, Length, PAGE_READWRITE); return MmSecureVirtualMemory(Address, Length, PAGE_READWRITE);
} }
/* /*
@ -160,7 +171,7 @@ VOID APIENTRY
EngUnsecureMem(HANDLE Mem) EngUnsecureMem(HANDLE Mem)
{ {
if (Mem == (HANDLE)-1) return; // HACK!!! if (Mem == (HANDLE)-1) return; // HACK!!!
MmUnsecureVirtualMemory((PVOID) Mem); MmUnsecureVirtualMemory((PVOID) Mem);
} }
/* EOF */ /* EOF */