Memory Mapped Region initial data

Multi tool use
Memory Mapped Region initial data
I want to create memory-mapped region using CreateFileMapping
without any specific disk-file bound, but bound (using MapViewOfFileEx
) to a specific memory address. Protection of such region needs to be read-only from beginning. Then, I cannot write data to such a region. If this region would be created for specific disk-file, initial data would come from file content. How I can fill this read-only region with initial data?
CreateFileMapping
MapViewOfFileEx
Example:
Most Windows processes have memory regions which are mapped (and not bound to any file path) and read-only since creation, they contain data. How was this achieved? How were these regions filled with data?
edited for more clear
– eruvgeruger
Jul 1 at 4:01
What makes you believe that "most windows processes have memory regions which are mapped(and not bound to any file path) and read only since creation"? What's your evidence for this claim?
– Igor Tandetnik
Jul 1 at 4:12
I seen this using debugger, some memory regions are mapped to some file path, some do not contain file path. But the question is not about evidences, it's about how to make such region.
– eruvgeruger
Jul 1 at 4:17
they first map section with readwrite protection, and then map with readonly
– RbMm
Jul 1 at 6:48
1 Answer
1
exist only one way do this - first map section with PAGE_READWRITE
, init it content, possible unmap, and then map it again - with PAGE_READONLY
protection. for example
PAGE_READWRITE
PAGE_READONLY
ULONG demo_map(PVOID BaseAddress, ULONG size, ULONG (*Init)(PVOID pv, ULONG size))
{
ULONG dwError = NOERROR;
if (HANDLE hSection = CreateFileMappingW(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, size, 0))
{
if (PVOID pv = MapViewOfFile(hSection, FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, 0))
{
dwError = Init(pv, size);
UnmapViewOfFile(pv);
}
else
{
dwError = GetLastError();
}
if (!dwError)
{
dwError = MapViewOfFileEx(hSection, FILE_MAP_READ, 0, 0, 0, BaseAddress) ? NOERROR : GetLastError();
}
CloseHandle(hSection);
}
return dwError;
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
"bound(using MapViewOfFileEx) to specific process memory address" It's not clear what you mean by that. You can only map shared memory into your own process, not some other process. Sounds like an XY problem. What's your actual goal with all this?
– Igor Tandetnik
Jul 1 at 3:23