Join our Newsletter — 33% off our NHI Course

How does Linux mmap work?

Linux mmap maps files or anonymous memory into a process address space, allowing fast file access or shared memory use. Attackers can abuse it for in-memory payload staging, code injection, or avoiding obvious file writes. Defenders should monitor unusual executable mappings, anonymous memory with execute permissions, and suspicious follow-on process behaviour.

What mmap changes in a process address space

mmap is not just another file I/O call. It creates a memory-backed view that the kernel can populate on demand, so the process can read or write pages as if they were local memory while the kernel handles paging, sharing, and copy-on-write behaviour. That is why it is used for file-backed mappings, shared memory, and anonymous memory allocation.

The practical difference is control over how data enters the process. A mapped file can be accessed without repeated read calls, while an anonymous mapping gives the process memory that is not tied to a file on disk. Protection flags also matter because the mapping can be read-only, writable, executable, shared, or private, and those choices shape both performance and exposure.

For defenders, the key signal is that mmap can create memory regions whose purpose is not obvious from filesystem activity alone. An executable anonymous mapping, for example, can be perfectly legitimate in some runtimes, but it is also a common staging pattern for payloads that never need a written-on-disk artefact.

Why attackers care about executable and anonymous mappings

Attackers use mmap because it helps separate malicious execution from visible file creation. If code is mapped into memory and executed from there, traditional file-centric detections may see less than they would if the same payload were dropped to disk first. The technique is also useful when an attacker wants to inject code into an existing process or keep a payload resident only in memory.

Anonymous mappings are especially important because they can be combined with write and execute transitions to stage shellcode, unpack a payload, or prepare a region that is later made executable. That pattern does not prove compromise on its own, but it becomes suspicious when it is followed by process hollowing-like behaviour, unusual child process creation, or network activity that does not match the parent process.

Attackers may also combine mapping with other memory manipulation primitives such as MITRE D3FEND Guide concepts around process analysis and executable memory monitoring. The defensive value is in correlating memory permissions, process lineage, and subsequent execution rather than treating mmap as inherently malicious.

What defenders should validate in Linux telemetry

The first question is whether the mapping is expected for the workload. JIT runtimes, browsers, language interpreters, and some database components can legitimately use memory mappings in ways that look unusual in a generic server process. The second question is whether the mapping transitions make sense, because a region that becomes executable after being written is much more concerning than a static read-only file mapping.

Useful checks include tracking anonymous executable memory, RWX mappings, executable pages created by non-runtime processes, and process trees where a benign parent spawns a child that then behaves like a loader. Those checks work best when paired with command-line telemetry, loaded module visibility, and syscall or audit data that shows when the mapping was created and how it was used.

For a broader detection model, the relationship between mapping activity and subsequent behaviour matters more than the mapping alone. A short-lived anonymous mapping that is followed by network beacons or self-modifying execution is a stronger indicator than a large number of ordinary file-backed mappings in a software build or runtime service.

Risk and Threat Considerations

mmap becomes risky when it is used to make execution less visible, because it can move payload staging away from obvious file-write events and into transient memory state. That increases the chance that a compromise looks like ordinary process behaviour until the payload is already running.

Failure mechanism: The attacker abuses memory mappings to place code in an address range that can later be executed, often after changing page permissions or leveraging a process that already expects dynamic code generation. The defence fails when monitoring focuses on files created on disk and misses memory permission changes, anonymous executable regions, or suspicious process behaviour after the mapping is established.

Impact: The result can be in-memory payload execution, code injection, reduced forensic visibility, and delayed detection because the malicious artefact may never exist as a normal file for scanners or integrity tools to inspect.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

MITRE ATT&CK addresses the attack and risk surface, while NIST SP 800-53 Rev 5 and CIS Controls v8 set the governance and control requirements practitioners need to meet.

Framework Control / Reference Relevance
MITRE ATT&CK T1055 — Process Injection mmap-based in-memory execution aligns with injected or staged process behaviour.
Recommendation — Map suspicious memory execution to T1055 and hunt for injection, hollowing, or loader behaviour.
NIST SP 800-53 Rev 5 SI-4 — System Monitoring Executable anonymous mappings and follow-on behaviour need runtime monitoring and alerting.
Recommendation — Monitor executable mappings and correlate them with process and network telemetry.
CIS Controls v8 CIS-8 — Audit Log Management Investigating mmap abuse depends on retaining process, syscall, and execution evidence.
Recommendation — Collect and retain logs that show mapping changes, process creation, and execution transitions.

Practitioner Guidance

What to verify: Determine whether executable anonymous mappings are normal for the process family before you treat them as suspicious. A browser, runtime, or JIT engine has a different baseline from a utility, admin tool, or server binary that should rarely need executable memory.

Decision rule: If a process creates writable memory and later makes that region executable, prioritise follow-on process inspection, network correlation, and memory capture over file-only hunting. If the mapping is file-backed and expected, focus instead on whether the mapped content or parent process is abnormal.

Practitioner takeaway: mmap is operationally normal, but the security question is whether the mapping pattern, permission changes, and downstream process behaviour fit the application’s real execution model.