[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 typedef struct _ROS_SECTION_OBJECT
{ {
CSHORT Type; SECTION;
CSHORT Size;
LARGE_INTEGER SizeOfSection;
ULONG InitialPageProtection;
union
{
ULONG LongFlags;
MMSECTION_FLAGS Flags;
} u;
PFILE_OBJECT FileObject; PFILE_OBJECT FileObject;
union
{ PMM_IMAGE_SECTION_OBJECT ImageSection;
PMM_IMAGE_SECTION_OBJECT ImageSection;
PMM_SECTION_SEGMENT Segment;
};
} ROS_SECTION_OBJECT, *PROS_SECTION_OBJECT; } ROS_SECTION_OBJECT, *PROS_SECTION_OBJECT;
#define MA_GetStartingAddress(_MemoryArea) ((_MemoryArea)->VadNode.StartingVpn << PAGE_SHIFT) #define MA_GetStartingAddress(_MemoryArea) ((_MemoryArea)->VadNode.StartingVpn << PAGE_SHIFT)

View file

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

View file

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