Correct and simplify page locking

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2019-06-26 19:25:03 +00:00
parent 3d84bddcc0
commit 88bde5b28e

View File

@ -301,35 +301,31 @@ TunMapUbuffer(_Inout_ TUN_MAPPED_UBUFFER *MappedBuffer, _In_ VOID *UserAddress,
return STATUS_SUCCESS; return STATUS_SUCCESS;
return STATUS_ALREADY_INITIALIZED; return STATUS_ALREADY_INITIALIZED;
} }
try
{
ProbeForWrite(UserAddress, Size, 1);
ProbeForRead(UserAddress, Size, 1);
MappedBuffer->Mdl = IoAllocateMdl(UserAddress, Size, FALSE, FALSE, NULL); MappedBuffer->Mdl = IoAllocateMdl(UserAddress, Size, FALSE, FALSE, NULL);
if (!MappedBuffer->Mdl) if (!MappedBuffer->Mdl)
return STATUS_INSUFFICIENT_RESOURCES; return STATUS_INSUFFICIENT_RESOURCES;
MmProbeAndLockPages(MappedBuffer->Mdl, KernelMode, IoWriteAccess); try
{
MmProbeAndLockPages(MappedBuffer->Mdl, UserMode, IoWriteAccess);
}
except(EXCEPTION_EXECUTE_HANDLER)
{
IoFreeMdl(MappedBuffer->Mdl);
MappedBuffer->Mdl = NULL;
return STATUS_INVALID_USER_BUFFER;
}
MappedBuffer->KernelAddress = MappedBuffer->KernelAddress =
MmGetSystemAddressForMdlSafe(MappedBuffer->Mdl, NormalPagePriority | MdlMappingNoExecute); MmGetSystemAddressForMdlSafe(MappedBuffer->Mdl, NormalPagePriority | MdlMappingNoExecute);
if (!MappedBuffer->KernelAddress) if (!MappedBuffer->KernelAddress)
{ {
MmUnlockPages(MappedBuffer->Mdl);
IoFreeMdl(MappedBuffer->Mdl); IoFreeMdl(MappedBuffer->Mdl);
MappedBuffer->Mdl = NULL; MappedBuffer->Mdl = NULL;
return STATUS_INSUFFICIENT_RESOURCES; return STATUS_INSUFFICIENT_RESOURCES;
} }
MappedBuffer->UserAddress = UserAddress; MappedBuffer->UserAddress = UserAddress;
MappedBuffer->Size = Size; MappedBuffer->Size = Size;
}
except(EXCEPTION_EXECUTE_HANDLER)
{
if (MappedBuffer->Mdl)
{
IoFreeMdl(MappedBuffer->Mdl);
MappedBuffer->Mdl = NULL;
}
return STATUS_INVALID_USER_BUFFER;
}
return STATUS_SUCCESS; return STATUS_SUCCESS;
} }