[NTOSKRNL] Embed a SECTION struct inside MM_ROS_SECTION_OBJECT

This commit is contained in:
Jérôme Gardou 2020-10-23 14:42:21 +02:00
parent c3ec5d801c
commit 25722e647e
3 changed files with 31 additions and 34 deletions

View file

@ -196,21 +196,10 @@ typedef struct _MM_IMAGE_SECTION_OBJECT
typedef struct _ROS_SECTION_OBJECT
{
CSHORT Type;
CSHORT Size;
LARGE_INTEGER SizeOfSection;
ULONG InitialPageProtection;
union
{
ULONG LongFlags;
MMSECTION_FLAGS Flags;
} u;
SECTION;
PFILE_OBJECT FileObject;
union
{
PMM_IMAGE_SECTION_OBJECT ImageSection;
PMM_SECTION_SEGMENT Segment;
};
PMM_IMAGE_SECTION_OBJECT ImageSection;
} ROS_SECTION_OBJECT, *PROS_SECTION_OBJECT;
#define MA_GetStartingAddress(_MemoryArea) ((_MemoryArea)->VadNode.StartingVpn << PAGE_SHIFT)

View file

@ -1072,8 +1072,7 @@ BOOLEAN
MiIsRosSectionObject(IN PVOID Section)
{
PROS_SECTION_OBJECT RosSection = Section;
if ((RosSection->Type == 'SC') && (RosSection->Size == 'TN')) return TRUE;
return FALSE;
return RosSection->u.Flags.filler;
}
#define MI_IS_ROS_PFN(x) ((x)->u4.AweAllocation == TRUE)

View file

@ -2684,7 +2684,7 @@ MmpDeleteSection(PVOID ObjectBody)
if (Section->Segment == NULL)
return;
(void)InterlockedDecrementUL(&Section->Segment->ReferenceCount);
(void)InterlockedDecrementUL(&((PMM_SECTION_SEGMENT)Section->Segment)->ReferenceCount);
}
if (Section->FileObject != NULL)
{
@ -2750,8 +2750,9 @@ MmCreatePhysicalMemorySection(VOID)
* Initialize it
*/
RtlZeroMemory(PhysSection, sizeof(ROS_SECTION_OBJECT));
PhysSection->Type = 'SC';
PhysSection->Size = 'TN';
/* Mark this as a "ROS Section" */
PhysSection->u.Flags.filler = 1;
PhysSection->InitialPageProtection = PAGE_EXECUTE_READWRITE;
PhysSection->u.Flags.PhysicalMemory = 1;
PhysSection->SizeOfSection = SectionSize;
@ -2763,7 +2764,7 @@ MmCreatePhysicalMemorySection(VOID)
return(STATUS_NO_MEMORY);
}
RtlZeroMemory(Segment, sizeof(MM_SECTION_SEGMENT));
PhysSection->Segment = Segment;
PhysSection->Segment = (PSEGMENT)Segment;
Segment->ReferenceCount = 1;
ExInitializeFastMutex(&Segment->Lock);
Segment->Image.FileOffset = 0;
@ -2865,8 +2866,9 @@ MmCreateDataFileSection(PROS_SECTION_OBJECT *SectionObject,
* Initialize it
*/
RtlZeroMemory(Section, sizeof(ROS_SECTION_OBJECT));
Section->Type = 'SC';
Section->Size = 'TN';
/* Mark this as a "ROS" section */
Section->u.Flags.filler = 1;
Section->InitialPageProtection = SectionPageProtection;
Section->u.Flags.File = 1;
if (AllocationAttributes & SEC_NO_CHANGE)
@ -2958,7 +2960,7 @@ MmCreateDataFileSection(PROS_SECTION_OBJECT *SectionObject,
ObDereferenceObject(FileObject);
return(STATUS_NO_MEMORY);
}
Section->Segment = Segment;
Section->Segment = (PSEGMENT)Segment;
Segment->ReferenceCount = 1;
ExInitializeFastMutex(&Segment->Lock);
/*
@ -2994,7 +2996,7 @@ MmCreateDataFileSection(PROS_SECTION_OBJECT *SectionObject,
Segment =
(PMM_SECTION_SEGMENT)FileObject->SectionObjectPointer->
DataSectionObject;
Section->Segment = Segment;
Section->Segment = (PSEGMENT)Segment;
(void)InterlockedIncrementUL(&Segment->ReferenceCount);
MmLockSectionSegment(Segment);
@ -3692,8 +3694,10 @@ MmCreateImageSection(PROS_SECTION_OBJECT *SectionObject,
* Initialize it
*/
RtlZeroMemory(Section, sizeof(ROS_SECTION_OBJECT));
Section->Type = 'SC';
Section->Size = 'TN';
/* Mark this as a "ROS" Section */
Section->u.Flags.filler = 1;
Section->InitialPageProtection = SectionPageProtection;
Section->u.Flags.File = 1;
Section->u.Flags.Image = 1;
@ -4256,8 +4260,8 @@ NtQuerySection(
}
else
{
Sbi->BaseAddress = (PVOID)RosSection->Segment->Image.VirtualAddress;
Sbi->Size.QuadPart = RosSection->Segment->Length.QuadPart;
Sbi->BaseAddress = (PVOID)((PMM_SECTION_SEGMENT)RosSection->Segment)->Image.VirtualAddress;
Sbi->Size.QuadPart = ((PMM_SECTION_SEGMENT)RosSection->Segment)->Length.QuadPart;
}
if (ResultLength != NULL)
@ -4557,6 +4561,8 @@ MmMapViewOfSection(IN PVOID SectionObject,
}
else
{
PMM_SECTION_SEGMENT Segment = (PMM_SECTION_SEGMENT)Section->Segment;
/* check for write access */
if ((Protect & (PAGE_READWRITE|PAGE_EXECUTE_READWRITE)) &&
!(Section->InitialPageProtection & (PAGE_READWRITE|PAGE_EXECUTE_READWRITE)))
@ -4605,16 +4611,16 @@ MmMapViewOfSection(IN PVOID SectionObject,
*ViewSize = PAGE_ROUND_UP(*ViewSize);
MmLockSectionSegment(Section->Segment);
MmLockSectionSegment(Segment);
Status = MmMapViewOfSegment(AddressSpace,
Section,
Section->Segment,
Segment,
BaseAddress,
*ViewSize,
Protect,
ViewOffset,
AllocationType & (MEM_TOP_DOWN|SEC_NO_CHANGE));
MmUnlockSectionSegment(Section->Segment);
MmUnlockSectionSegment(Segment);
if (!NT_SUCCESS(Status))
{
MmUnlockAddressSpace(AddressSpace);
@ -4765,6 +4771,7 @@ MmMapViewInSystemSpace (IN PVOID SectionObject,
IN OUT PSIZE_T ViewSize)
{
PROS_SECTION_OBJECT Section;
PMM_SECTION_SEGMENT Segment;
PMMSUPPORT AddressSpace;
NTSTATUS Status;
PAGED_CODE();
@ -4780,6 +4787,8 @@ MmMapViewInSystemSpace (IN PVOID SectionObject,
DPRINT("MmMapViewInSystemSpace() called\n");
Section = (PROS_SECTION_OBJECT)SectionObject;
Segment = (PMM_SECTION_SEGMENT)Section->Segment;
AddressSpace = MmGetKernelAddressSpace();
MmLockAddressSpace(AddressSpace);
@ -4794,19 +4803,19 @@ MmMapViewInSystemSpace (IN PVOID SectionObject,
(*ViewSize) = Section->SizeOfSection.u.LowPart;
}
MmLockSectionSegment(Section->Segment);
MmLockSectionSegment(Segment);
Status = MmMapViewOfSegment(AddressSpace,
Section,
Section->Segment,
Segment,
MappedBase,
*ViewSize,
PAGE_READWRITE,
0,
0);
MmUnlockSectionSegment(Section->Segment);
MmUnlockSectionSegment(Segment);
MmUnlockAddressSpace(AddressSpace);
return Status;