kernel
Loading...
Searching...
No Matches
section.c
Go to the documentation of this file.
1/*++
2
3Module Name:
4
5 section.c
6
7Purpose:
8
9 This translation unit contains the implementation of file sections (process sections).
10
11Author:
12
13 slep (Matanel) 2025.
14
15Revision History:
16
17--*/
18
19#include "../../includes/mm.h"
20#include "../../includes/ob.h"
21#include "../../includes/mg.h"
22#include "../../includes/fs.h"
23
26 OUT PHANDLE SectionHandle,
27 IN struct _FILE_OBJECT* FileObject
28)
29{
30 MTE_HEADER Header;
31 MTSTATUS Status;
32 // Assume failure.
33 *SectionHandle = 0;
34
35 // Read the header from the file.
36 Status = FsReadFile(FileObject, 0, &Header, sizeof(MTE_HEADER), NULL);
37 if (MT_FAILURE(Status)) {
38 return Status;
39 }
40
41 // Validate header magic.
42 if (kmemcmp(Header.Magic, "MTE\0", 4) != 0) {
43 // Invalid header.
44#ifdef DEBUG
45 gop_printf(COLOR_RED, "Invalid executable given, magic is not MTE.\n");
46#endif
48 }
49
50 // Allocate the actual section object (pool)
51 PMM_SECTION NewSection = NULL;
52 Status = ObCreateObject(MmSectionType, sizeof(MM_SECTION), (void**)&NewSection);
53 if (MT_FAILURE(Status)) return Status;
54
55 // Set fields
56 NewSection->FileObject = FileObject;
57 NewSection->EntryPointOffset = Header.EntryRVA;
58 NewSection->PreferredBase = Header.PreferredImageBase;
59
60 // Create subsections
61
62 // Setup .text - Read | Execute
63 NewSection->Text.FileOffset = Header.TextRVA;
64 NewSection->Text.VirtualSize = Header.TextSize;
66 NewSection->Text.IsDemandZero = 0;
67
68 // Setup .data - Read | Write | CopyOnWrite
69 NewSection->Data.FileOffset = Header.DataRVA;
70 NewSection->Data.VirtualSize = Header.DataSize;
72 NewSection->Data.IsDemandZero = 0;
73
74 // Setup .bss (uninit) - Read | Write | DemandZero
75 NewSection->Bss.FileOffset = 0; // Irrelevant for BSS
76 NewSection->Bss.VirtualSize = Header.BssSize;
78 NewSection->Bss.IsDemandZero = 1;
79
80 // The file end RVA is just the file size.
81 uintptr_t FileEndRVA = FileObject->FileSize;
82
83 // Configure the WholeFileSection.
84 // This represents the chunk of virtual memory that maps directly to the file.
85 // It starts at FileOffset 0 (so we can see the Header) and goes up to the end of Data.
86 NewSection->WholeFileSection.FileOffset = 0;
87 NewSection->WholeFileSection.VirtualSize = FileEndRVA;
88
89 // We default to RWX here to simplify loading; permissions should be refined later via VirtualProtect.
91 NewSection->WholeFileSection.IsDemandZero = 0;
92
93 // Calculate total size of the image in memory.
94 // This includes the file part + the BSS part.
95 NewSection->ImageSize = ALIGN_UP(FileEndRVA + Header.BssSize, VirtualPageSize);
96
97 // Create a handle for the section.
98 Status = ObCreateHandleForObject(NewSection, MT_SECTION_ALL_ACCESS, SectionHandle);
99
100 // Successful!
101 // If success on ObCreateHandleForObject it would dereference the pointer count created by ObCreateObject
102 // (cancel out the reference made by ObCreateHandleForObject).
103 // And so HandleCount == PointerCount.
104 // Else, it would destroy the section (along with the file handle).
105 ObDereferenceObject(NewSection);
106 return MT_SUCCESS;
107}
108
111 IN HANDLE SectionHandle,
112 IN PEPROCESS Process,
113 OUT void** EntryPointAddress,
114 OUT void** BaseAddress
115)
116{
117 PMM_SECTION Section;
118 MTSTATUS Status = ObReferenceObjectByHandle(SectionHandle, MT_SECTION_ALL_ACCESS, MmSectionType, (void**)&Section, NULL);
119 if (MT_FAILURE(Status)) return Status;
120
121 uintptr_t load_base = Section->PreferredBase;
122
123 // Map the whole file, header + text + data.
124
125 // We attempt to map the file content at the preferred base.
127 Process,
128 (void**)&load_base,
131 );
132
133 if (MT_FAILURE(Status)) {
134 // Preferred image base is taken. Let the VAD allocator pick an address.
135 // Relocation tables (mapped inside this chunk) will be needed to fix addresses.
136 load_base = 0;
138 Process,
139 (void**)&load_base,
142 );
143 }
144
145 if (MT_FAILURE(Status)) goto Cleanup;
146
147 // Store the file and fileoffset into the vad we just got.
148 // IMPORTANT: We map from FileOffset 0. This exposes the MTE Header in memory.
149 PMMVAD Vad = MiFindVad(Process, load_base);
150 if (Vad) {
151 Vad->File = Section->FileObject;
152 Vad->FileOffset = Section->WholeFileSection.FileOffset; // 0
153 }
154
155 // .bss lives immediately after the file data in Virtual Memory.
156 if (Section->Bss.VirtualSize > 0) {
157 // The RVA where BSS logically starts
158 uintptr_t BssStartVa = load_base + Section->WholeFileSection.VirtualSize;
159 // The RVA where BSS ends
160 uintptr_t BssEndVa = BssStartVa + Section->Bss.VirtualSize;
161
162 // The start of the NEXT page after the file data
163 uintptr_t NextPageVa = ALIGN_UP(BssStartVa, VirtualPageSize);
164
165 // 2. Allocate the overflow
166 // Only if BSS is large enough to cross into the next page
167 if (BssEndVa > NextPageVa) {
168 uintptr_t OverflowSize = BssEndVa - NextPageVa;
169 uintptr_t AllocBase = NextPageVa;
170
172 Process,
173 (void**)&AllocBase, // Must be page aligned
174 OverflowSize,
175 Section->Bss.Protection
176 );
177
178 if (MT_FAILURE(Status)) {
179 MmFreeVirtualMemory(Process, (void*)load_base);
180 goto Cleanup;
181 }
182 }
183 }
184
185 // The true base address is at load_base
186 *BaseAddress = (void*)load_base;
187
188 // Compute RIP based on where we actually loaded
189 uintptr_t RipAddress = load_base + Section->EntryPointOffset;
190 *EntryPointAddress = (void*)RipAddress;
191
192Cleanup:
193 ObDereferenceObject(Section);
194 return Status;
195}
196
197void
199 void* Object
200)
201{
202 PMM_SECTION Section = (PMM_SECTION)Object;
203
204 // Deref the file object if it exists.
205 if (Section->FileObject) {
207 }
208}
#define IN
Definition annotations.h:8
#define OUT
Definition annotations.h:9
int32_t * PHANDLE
Definition core.h:58
int32_t HANDLE
Definition core.h:58
EPROCESS * PEPROCESS
Definition core.h:52
void gop_printf(uint32_t color, const char *fmt,...)
Definition gop.c:633
#define COLOR_RED
Colors definitions for easier access.
Definition mg.h:30
FORCEINLINE int kmemcmp(const void *s1, const void *s2, size_t n)
Definition mm.h:681
@ VAD_FLAG_READ
Definition mm.h:296
@ VAD_FLAG_COPY_ON_WRITE
Definition mm.h:301
@ VAD_FLAG_MAPPED_FILE
Definition mm.h:300
@ VAD_FLAG_EXECUTE
Definition mm.h:298
@ VAD_FLAG_WRITE
Definition mm.h:297
struct _MM_SECTION * PMM_SECTION
struct _MM_SECTION MM_SECTION
#define MT_SECTION_ALL_ACCESS
Definition mm.h:270
struct _MMVAD * PMMVAD
#define VirtualPageSize
Definition mm.h:53
#define ALIGN_UP(x, align)
Definition mm.h:202
POBJECT_TYPE MmSectionType
Definition mminit.c:27
#define MT_SUCCESS
Definition mtstatus.h:22
#define MT_INVALID_IMAGE_FORMAT
Definition mtstatus.h:48
#define MT_FAILURE(Status)
Definition mtstatus.h:16
int32_t MTSTATUS
Definition mtstatus.h:12
MTSTATUS ObCreateHandleForObject(IN void *Object, IN ACCESS_MASK DesiredAccess, OUT PHANDLE ReturnedHandle)
Definition ob.c:403
MTSTATUS ObReferenceObjectByHandle(IN HANDLE Handle, IN uint32_t DesiredAccess, IN POBJECT_TYPE DesiredType, OUT void **Object, _Out_Opt PHANDLE_TABLE_ENTRY HandleInformation)
Definition ob.c:277
void ObDereferenceObject(IN void *Object)
Definition ob.c:554
MTSTATUS ObCreateObject(IN POBJECT_TYPE ObjectType, IN uint32_t ObjectSize, OUT void **ObjectCreated)
Definition ob.c:118
MTSTATUS MmMapViewOfSection(IN HANDLE SectionHandle, IN PEPROCESS Process, OUT void **EntryPointAddress, OUT void **BaseAddress)
Definition section.c:110
void MmpDeleteSection(void *Object)
Definition section.c:198
MTSTATUS MmCreateSection(OUT PHANDLE SectionHandle, IN struct _FILE_OBJECT *FileObject)
Definition section.c:25
uint64_t EntryPointOffset
Definition mm.h:624
MM_SUBSECTION Text
Definition mm.h:620
MM_SUBSECTION Data
Definition mm.h:621
uint64_t ImageSize
Definition mm.h:625
struct _FILE_OBJECT * FileObject
Definition mm.h:615
MM_SUBSECTION Bss
Definition mm.h:622
MM_SUBSECTION WholeFileSection
Definition mm.h:617
uintptr_t PreferredBase
Definition mm.h:616
uint32_t IsDemandZero
Definition mm.h:610
uint64_t FileOffset
Definition mm.h:607
uint64_t VirtualSize
Definition mm.h:608
VAD_FLAGS Protection
Definition mm.h:609
uint64_t FileOffset
Definition mm.h:529
struct _FILE_OBJECT * File
Definition mm.h:528
uint64_t DataSize
Definition mm.h:578
uint8_t Magic[4]
Definition mm.h:572
uint64_t DataRVA
Definition mm.h:577
uint64_t EntryRVA
Definition mm.h:574
uint64_t PreferredImageBase
Definition mm.h:573
uint64_t TextSize
Definition mm.h:576
uint64_t TextRVA
Definition mm.h:575
uint64_t BssSize
Definition mm.h:579
MTSTATUS MmFreeVirtualMemory(IN PEPROCESS Process, IN void *BaseAddress)
Definition vad.c:871
PMMVAD MiFindVad(IN PEPROCESS Process, IN uintptr_t VirtualAddress)
Definition vad.c:352
MTSTATUS MmAllocateVirtualMemory(IN PEPROCESS Process, _In_Opt _Out_Opt void **BaseAddress, IN size_t NumberOfBytes, IN VAD_FLAGS VadFlags)
Definition vad.c:740
MTSTATUS FsReadFile(IN PFILE_OBJECT FileObject, IN uint64_t FileOffset, OUT void *Buffer, IN size_t BufferSize, _Out_Opt size_t *BytesRead)
Definition vfs.c:117