Search

A technology, a device or an article

Memory locked in RAM does not become a secret vault

mlock keeps pages resident without certifying secrecy. Residency, dump exclusion, copying and erasure are separate contracts.

Blue patterned sheet clamped at the corners and partly covered by translucent cloth
ITNWS AI-generated conceptual illustration. Clamps evoke residency while the translucent covering conveys the absence of a confidentiality guarantee.ITNWS · image_gen · Original ITNWS AI-generated conceptual editorial illustration

Locked is a reassuring word. Applied to a region of RAM, it can sound as though the operating system has put the contents beyond inspection. Linux mlock makes a narrower and useful promise: the covered pages stay resident instead of being paged to swap. Confidentiality does not follow from residency. The distinction matters because a program can successfully establish the documented guarantee while leaving other ways of copying or disclosing its data entirely unchanged.

The mechanism deserves more respect than a slogan about secure memory gives it. Preventing a sensitive buffer from entering ordinary swap can remove one persistent copy path. Keeping a real-time working set resident can remove one source of unpredictable page-in delays. These are concrete properties. Calling either result a vault obscures what the call accomplished and what the application must still control. The problem with the broad claim is not that mlock is useless; it is that the claim silently changes its subject.

The Linux mlock manual, checked on October 29, 2026, defines the operation in terms of virtual address ranges and whole pages. A successful ordinary mlock returns with all pages covering that range resident, and the lock keeps them resident until it is removed. The requested byte range can therefore cover parts of boundary pages that also hold unrelated objects. Locking follows memory-management units, not the exact outline of a password, an application object or a programmer’s preferred ownership boundary.

Imagine, for explanation only, a 200-byte buffer crossing a page boundary. If the system uses 4,096-byte pages, the operation concerns two pages even though the useful object is much smaller. A 200-byte buffer wholly inside one such page concerns one page. These are conditional arithmetic examples, not an assertion that every Linux environment has this page size. They explain why the amount charged or affected cannot always be inferred from the length of a sensitive string. Allocation and page placement influence the cost of the promise.

Nor is resident the same as permanently tied to one physical address for all purposes. mlock is an application-facing residency facility. It should not be substituted for the separate page-pinning mechanisms required by a DMA driver or another subsystem with a particular physical-memory lifetime contract. The application is entitled to its documented access and residency semantics, not to every stronger property a reader associates with the everyday word fixed. Mixing these meanings is especially easy when a discussion moves from process memory to device transfers.

The result of the call matters. Ordinary unprivileged locking is constrained by RLIMIT_MEMLOCK; privilege through CAP_IPC_LOCK changes the applicable limit behavior. A program that ignores a failed call has not obtained the promise merely because the source contains mlock. The right response depends on the program’s purpose. If non-swappability is a requirement for a particular operation, quietly continuing as though the requirement succeeded changes the security or timing contract. A comment in the source cannot compensate for the missing successful result.

A locked page also remains part of a shared system. Memory that cannot be reclaimed through ordinary swap is less flexible when other workloads need RAM. Locking a vast mapping indiscriminately can therefore impose a system cost while protecting very little additional useful data. That is why the distinction between reserving a virtual range and actually using its pages matters. A careful design chooses a meaningful working set; it does not measure its seriousness by how much memory it prevents the kernel from reclaiming.

The on-fault variant makes the distinction explicit. With mlock2 and MLOCK_ONFAULT, pages already resident are locked, while absent pages in the range acquire the lock when a fault populates them. The operation does not bring the entire absent range into RAM at return. It can suit a large sparse mapping, but it cannot honestly be described as having already prefaulted every potential access. The next first touch can still have work to do. Deferred establishment is a different timing promise, not a faster spelling of an identical initial condition.

This also limits claims about latency. Even ordinary successful locking does not abolish every page fault or every source of delay. Changing mappings, copy-on-write after fork, scheduling, interrupts, cache misses and synchronization can all matter to an application. The manual specifically warns real-time programs about stack preparation and fork-related copy-on-write. A resident working set removes a class of paging waits; it does not turn the entire program into a deterministic device. Timing guarantees must describe the rest of the execution environment rather than borrowing certainty from one memory call.

The copies that the word locked leaves outside

A core dump is a separate path. The Linux core manual describes process dumps and the conditions controlling their creation. Whether a region is included is not answered simply by the fact that its pages were locked. A process can retain the residency guarantee and still have sensitive memory represented in a dump under the applicable dump policy. Treating locked as synonymous with excluded from diagnostics would therefore establish the wrong property while leaving the intended one unexamined.

MADV_DONTDUMP addresses a different question for a specified mapping. The madvise manual documents exclusion from core dumps. This can complement a memory lock, but does not replace it: excluding a region from a dump does not itself keep its pages from swap. Conversely, locking does not itself apply DONTDUMP. The operations are useful precisely because they control different behavior. Combining them can be appropriate when the application needs both, provided the program checks their actual success and lifetime.

Purple beads in a white indented tray beneath translucent fabric
ITNWS AI-generated conceptual illustration. Beads evoke populated pages in a sparse mapping, not the physical construction of DRAM. — ITNWS · image_gen · Original ITNWS AI-generated conceptual editorial illustration

Neither operation prohibits the program from making a second copy. A formatting routine may build a temporary string. An allocator may retain released storage until reuse. A parser can create a decoded object while the input buffer remains elsewhere. If the sensitive bytes reach a log through an application bug, residency in the original allocation is irrelevant to that disclosure. These examples concern the flow of ordinary data through code, not a failure of the kernel to honor mlock. The protected region is a location, whereas the secret can acquire more locations.

This is also why a locked container cannot automatically certify every value ever derived from its contents. The implementation may use registers, stack temporaries or internal scratch buffers. A caller might copy the value before passing it to the container. A full confidentiality claim would have to account for those paths and the privileges of observers. mlock does not change which other threads in the process may access a valid pointer, and it does not encrypt the region against a permitted reader. Its residency guarantee remains intact while these access questions remain separate.

Erasing memory introduces another independent contract. An ordinary memset whose writes have no observable effect can be removed by optimization. The explicit_bzero discussion describes an interface intended to prevent that elimination. It also explains its limits: erasing a specified memory region cannot guarantee that copies in registers or scratch storage vanish. Indeed, forcing a value from a register into an addressable object can create a copy that previously did not exist there. The details of representation affect what an erasure can actually reach.

Therefore, unlocking is not erasing. munlock allows the memory to be swapped again under the normal rules; it does not fill the allocation with zeros. Freeing the allocation is not necessarily erasing either. A program that needs to remove a particular live buffer’s contents must perform an appropriate operation while it still controls the valid storage. The order matters: giving memory back and subsequently writing through the old pointer is invalid. A cleanup path must preserve ownership long enough to complete the action whose success it claims.

The opposite misunderstanding is also possible. Erasing the original region does not retrospectively remove a copy that already entered another file, dump or buffer. A good design reduces unnecessary copying and controls relevant persistence paths before sensitive data arrives. This is a reasoning consequence of the separate mechanisms, not a universal recipe for all secrets. An image editor, a low-latency audio engine and a cryptographic library may use locked memory for quite different requirements. The right boundary follows the actual data and threat or timing model.

Process lifetime further narrows the claim. Memory locks are not inherited by a child across fork, and exec removes the locks as the address space is replaced. The source-level fact that a parent established a lock does not prove a child is operating under the same guarantee. Fork can also create copy-on-write conditions that affect latency. A service architecture must therefore associate the promise with the process and mapping that actually handle the data. A lock obtained once during an earlier lifecycle phase is not an eternal attribute of the abstract application.

Mapping changes matter in the same way. Unmapping a region removes the associated lock. Allocating a new buffer at a later time does not recover the earlier promise just because the application reuses the same variable name. With mlockall, flags governing current and future mappings establish their own behavior, and future allocation or stack growth may encounter the locking limit. The apparent convenience of locking everything shifts obligations into later operations. It does not make the system’s capacity and permissions irrelevant.

Observability should match the question being asked. The proc_pid_status documentation describes VmLck as locked-memory accounting. That field can help check whether the running process’s locking state resembles expectations. It is not a ledger of every copy of a secret or a proof that diagnostics excluded all sensitive bytes. A plausible total can accompany a locked wrong allocation. Successful API results, mapping identity and the actual data path are stronger together than a single reassuring number.

A meaningful validation would test the requirements separately. Does the chosen process successfully establish the intended lock under deployed limits? Does the mapping have the necessary dump behavior? Do allocation and cleanup paths preserve those properties when errors occur? Are unwanted copies introduced elsewhere? These questions do not form additional guarantees of mlock. They reveal which claims would otherwise be smuggled into its name. No such application test was performed for this article, and no measured security level is claimed.

Memory locking intervenes in the management of RAM pages for an explicit purpose. Its value survives a precise description: keep this process’s covered pages resident under the established lock semantics. That is substantial enough to build on. It is not broad enough to certify secrecy, erase copies, prevent every fault or supply a device’s physical pinning contract. A useful promise stays useful when its boundary remains visible.

Sources & documentation

  1. Linux mlock(2) ↗
  2. Linux madvise(2) ↗
  3. Linux core(5) ↗
  4. Linux explicit_bzero(3) ↗
  5. Linux proc_pid_status(5) ↗