[NTOS:MM] Reduce the magnitude of the MiRosProtectVirtualMemory hack

This commit is contained in:
Timo Kreuzer 2024-04-06 23:37:59 +03:00
parent f60128b69d
commit 1f27911997
2 changed files with 20 additions and 47 deletions

View file

@ -2153,46 +2153,6 @@ MiIsEntireRangeCommitted(IN ULONG_PTR StartingAddress,
return TRUE;
}
NTSTATUS
NTAPI
MiRosProtectVirtualMemory(IN PEPROCESS Process,
IN OUT PVOID *BaseAddress,
IN OUT PSIZE_T NumberOfBytesToProtect,
IN ULONG NewAccessProtection,
OUT PULONG OldAccessProtection OPTIONAL)
{
PMEMORY_AREA MemoryArea;
PMMSUPPORT AddressSpace;
ULONG OldAccessProtection_;
NTSTATUS Status;
*NumberOfBytesToProtect = PAGE_ROUND_UP((ULONG_PTR)(*BaseAddress) + (*NumberOfBytesToProtect)) - PAGE_ROUND_DOWN(*BaseAddress);
*BaseAddress = (PVOID)PAGE_ROUND_DOWN(*BaseAddress);
AddressSpace = &Process->Vm;
MmLockAddressSpace(AddressSpace);
MemoryArea = MmLocateMemoryAreaByAddress(AddressSpace, *BaseAddress);
if (MemoryArea == NULL || MemoryArea->DeleteInProgress)
{
MmUnlockAddressSpace(AddressSpace);
return STATUS_UNSUCCESSFUL;
}
if (OldAccessProtection == NULL) OldAccessProtection = &OldAccessProtection_;
ASSERT(MemoryArea->Type == MEMORY_AREA_SECTION_VIEW);
Status = MmProtectSectionView(AddressSpace,
MemoryArea,
*BaseAddress,
*NumberOfBytesToProtect,
NewAccessProtection,
OldAccessProtection);
MmUnlockAddressSpace(AddressSpace);
return Status;
}
NTSTATUS
NTAPI
MiProtectVirtualMemory(IN PEPROCESS Process,
@ -2254,13 +2214,18 @@ MiProtectVirtualMemory(IN PEPROCESS Process,
/* Check if this is a ROSMM VAD */
if (MI_IS_ROSMM_VAD(Vad))
{
/* Not very awesome hack */
/* Not too shabby hack */
ASSERT(((PMEMORY_AREA)Vad)->Type == MEMORY_AREA_SECTION_VIEW);
*NumberOfBytesToProtect = PAGE_ROUND_UP((ULONG_PTR)(*BaseAddress) + (*NumberOfBytesToProtect)) - PAGE_ROUND_DOWN(*BaseAddress);
*BaseAddress = (PVOID)PAGE_ROUND_DOWN(*BaseAddress);
Status = MmProtectSectionView(AddressSpace,
(PMEMORY_AREA)Vad,
*BaseAddress,
*NumberOfBytesToProtect,
NewAccessProtection,
OldAccessProtection);
MmUnlockAddressSpace(AddressSpace);
return MiRosProtectVirtualMemory(Process,
BaseAddress,
NumberOfBytesToProtect,
NewAccessProtection,
OldAccessProtection);
return Status;
}
/* Make sure the address is within this VAD's boundaries */

View file

@ -2076,6 +2076,13 @@ MmProtectSectionView(PMMSUPPORT AddressSpace,
NTSTATUS Status;
ULONG_PTR MaxLength;
ASSERT(MemoryArea->Type == MEMORY_AREA_SECTION_VIEW);
if (MemoryArea->DeleteInProgress)
{
return STATUS_UNSUCCESSFUL;
}
MaxLength = MA_GetEndingAddress(MemoryArea) - (ULONG_PTR)BaseAddress;
if (Length > MaxLength)
Length = (ULONG)MaxLength;
@ -2091,7 +2098,8 @@ MmProtectSectionView(PMMSUPPORT AddressSpace,
return STATUS_INVALID_PAGE_PROTECTION;
}
*OldProtect = Region->Protect;
if (OldProtect != NULL)
*OldProtect = Region->Protect;
Status = MmAlterRegion(AddressSpace, (PVOID)MA_GetStartingAddress(MemoryArea),
&MemoryArea->SectionData.RegionListHead,
BaseAddress, Length, Region->Type, Protect,