Search

A technology, a device or an article

Read-only memory: what promise does mprotect make?

mprotect restricts accesses through mapped pages. Why read-only is not global object immutability, and how aliases, threads, and page boundaries matter.

Two small objects share a transparent partition over a tiled area.
ITNWS AI-generated conceptual illustration. The protection unit covers neighboring objects; the watercolor does not depict physical DRAM.ITNWS · image_gen · Original ITNWS AI-generated conceptual editorial illustration

A small object may well deserve a separate ban on writes. The memory page does not have to agree with this. If there are other objects nearby, changing the page rights will affect them as well. This is where two design units collide: the program is responsible for the contents of the object, and the virtual memory mechanism controls access to pages. A successful protection design begins with recognizing this difference. Trying to close a few bytes as a separate box can put a common door on everything that happens to be on the same page.

On Linux, mprotect changes access rights to mapped pages. This is a powerful and useful feature, but the word read-only next to an area does not mean that its contents are universally immutable throughout the world. Rights refer to a specific access path through the virtual addresses of the calling process. If the project promises more than this access path controls, an appealing flag conceals an unmet part of the contract. The subject of discussion is RAM and its mapping, and not the transfer of files to an archive format and not the electrical protection of DRAM chips.

The mprotect(2) manual requires the start address to be aligned to a page boundary. The change affects pages that contain at least part of the specified range. Therefore, the size of the object does not in itself determine the minimum scale of impact. In a notional system with a base page of 4096 bytes, an 80-byte object can share permissions with many neighboring objects. The numbers here illustrate granularity and do not describe the editorial measurement or universal page size of each Linux platform.

In the allocator, this turns into a real architectural price. A separately allocated area with well-thought-out boundaries allows you to change rights without prohibiting writing to neighboring objects. A random address from a common heap does not make such a promise. Even if you calculate the beginning of the page containing it, the problem does not become correct: allocator service structures and other objects can live on it. Mechanical pointer rounding solves the arithmetic requirement of the API, but does not create ownership of all affected bytes. Ownership of memory remains the responsibility of the project.

Read-only here is expressed through PROT_READ without PROT_WRITE. PROT_NONE denies access, PROT_EXEC refers to execution. However, permission combinations and actual capabilities vary across architectures and process states. The manual explicitly notes cases where writing implies reading and reading may imply execution. Therefore, from a neat list of flags, it is impossible to infer the same hardware behavior of all machines. A claim about a specific protection must preserve the platform and context in which it is intended to be used.

Failure becomes visible at the memory access

If a process accesses memory in a way that violates its permissions, Linux generates a SIGSEGV. This is a useful observable boundary: an erroneous write through a protected mapping is no longer undetected. But the signal does not explain the program’s intent or correct the data. It also doesn’t automatically tell who logically owned the object and which line of code was intended to have permission. The mechanism turns part of the violations into an event; a meaningful diagnosis still requires the context of the address, operation and time of change of rights.

That is why the guard page is used as the boundary of the permissible space. A page is left next to the area without access, so that a certain exit beyond the limit is prohibited. This design does not catch every invalid index. A write inside a permitted page may damage a neighboring object and not cross the security boundary. A sufficiently distant out-of-bounds access can jump over a protected page into another accessible area. The protection has a geometry that needs to be mapped to the error class. The label protected does not turn every access into a bounds check for a particular array.

The same goes for reading a freed object. If the memory of an entire isolated region is left inaccessible, some late accesses will become noticeable. If the allocator reused it for another object and returned the permissions, the old pointer may once again reach accessible bytes. mprotect does not store an object’s identity history and does not distinguish between the old and new owner of the same address. For such a task, additional lifetime rules are needed. Page rights are a good way to express the current validity of an address path, but are not a complete system of type safety.

A separate price arises with frequent switching. The kernel must update the appropriate mapping view and match the rights to the translation mechanisms used. Changing a small part of an area can split it into several areas with different attributes. The documentation lists ENOMEM also for the case where the number of such mappings has exceeded the allowed limit. This is a failure of the control structure, and not necessarily evidence of a lack of free gigabytes of DRAM. The same error name can refer to different limited resources within an operation.

proc_pid_maps(5) shows ranges and their permissions. Such a map is useful to see boundaries with different rights, but does not replace proof of program correctness. While the observer is reading the file, another thread can change mappings. The map does not contain a complete list of language objects, pointer owners, and library promises. Even the exact line r–p speaks to the properties of the range, rather than ensuring that all semantically associated data is permanently frozen.

Two fabric ribbons reach one woven patch, with a gate closing only one route.
ITNWS AI-generated conceptual illustration. Restricting one address path does not close another writable alias to the same data. — ITNWS · image_gen · Original ITNWS AI-generated conceptual editorial illustration

Now imagine two virtual mappings of the same common data. One path’s write permission was removed, while the other one retained it. A prohibition through the first address is not required to prohibit changes through the second. This is a fundamental limit of the word read-only: it can describe the ability of a given observer to act, rather than the ability of the content to change at all. For a project that needs global immutability, a stored writable alias is an essential part of the task. The protection of one address window does not magically carry over to every other path to the same physical memory.

The mmap(2) manual distinguishes between MAP_SHARED and MAP_PRIVATE. In a shared mapping, changes can be visible to other processes mapping the same object. In a private mapping, the caller’s changes are not carried over to the shared file; rules for the visibility of subsequent external changes have qualifications. Therefore, you cannot replace the model selection with one subsequent PROT_READ setting. The design first determines what data is shared, then what operations are allowed across each mapping. Otherwise, the word “copy” may be as inaccurate as the word “protection.”

The design is especially noticeable at the moment the object is published to readers. The author has filled in the data and wants to declare it ready. Removing write permission helps catch further accidental changes through this path. However, it is not in itself a language release/acquire mechanism and does not replace correct synchronization of pointer passing between threads. If the code has a race in the language model, a familiar system call does not automatically turn it into a valid protocol. Making data available and preventing subsequent writes are related but different responsibilities.

For threads in the same process, normal mapping changes apply to the shared address space. It cannot be assumed that one thread has enabled writing only for itself, while the others have retained the read-only form of the same addresses. When a different granularity of rights is needed, other mechanisms and their limitations are considered. Linux documentation on protection keys describes a separate hardware-supported model where access rights are also related to thread state. A similar purpose does not make these interfaces interchangeable or equally accessible on any CPU.

Accidental write protection is also different from a trust boundary. Code that is allowed to call mprotect again and return PROT_WRITE under valid conditions does not become an untrusted isolated peer simply because writing is temporarily forbidden. It is possible to construct a useful discipline of phases: filling, using, controlled change. But such discipline presupposes that participants comply with the rules for managing mappings. If a project promises to resist arbitrary code with the same process capabilities, it needs to justify a completely different set of constraints. One flag does not create it without additional system design.

At the language level, there is a word const. It describes type constraints and expression rules, not a guaranteed page configuration. An interface-restricted object may reside on a writable page, and a read-only mapping may be represented by a pointer that is syntactically writable. Then an attempt to change the data can lead to an access fault. A consistent design makes these levels intelligible to each other, but their identity cannot be assumed. Compiler checking and address access checking operate on different program representations.

It is useful to distinguish between lack of access and lack of memory. PROT_NONE does not immediately release the physical contents and does not provide evidence that they have been erased. Unmapping, freeing pages, and clearing data have their own semantics. Similarly, PROT_READ does not guarantee that a subsequent read will not cause a page fault: a valid access may require operation of the memory management system. The right to read does not disclose the price of reading. Here it is easy to subtly replace the issue of security with a question of residency, although the interfaces respond to different requirements.

The system call returns a result, which is also part of the design. If setting permissions fails, the following code cannot act as if the barrier has already appeared. The application must maintain a valid failure path and not continue passing the object under a false read-only promise. This is not a blanket "check for errors" recommendation, but a specific logical dependency: subsequent security relies on a successful mapping change. Without it, the truth of the initial premise changes, and not just the convenience of diagnosis.

Range boundaries require careful calculation. For an object that traverses multiple pages, protecting only the page containing its first byte is insufficient. For address arithmetic, it is important not to overflow the calculated end and not affect someone else’s mapping. The API accepts an address and length, and the responsibility for the meaning of this interval remains with the caller. Making your own isolated ranges often makes the reasoning simpler because the logical control area is the same as the area whose rights are actually being changed. This is an engineering choice for memory organization, not a required recipe for every small object.

Even a successful write ban does not verify the correctness of the content already filled in. If an object received an erroneous value before setting PROT_READ, the mechanism will retain the ability to read exactly that erroneous value. Control of object ranges, format, and invariants must precede the promise of readiness. Thus, a hardware-observable barrier acquires a reasonable role: it helps to keep the established result from a certain class of subsequent changes, but does not create the truth of the result itself. Otherwise, strict defense only makes the old error more persistent.

Sources verified November 4, 2026. The examples are hypothetical; no operations on the reader’s memory or editorial experiments were performed. We’re talking about existing Linux interfaces; the publication does not declare a new hardware property for the modules. RAM remains a place to store data, and mapping permissions are a way to determine allowed accesses. When a project’s unit of responsibility matches a managed unit of protection, read-only becomes an exact promise. When they diverge, it is more useful to name the missing boundary than to attribute to a single flag the immutability of the entire object under any access path.

Sources & documentation

  1. Linux man-pages: mprotect(2) ↗
  2. Linux man-pages: mmap(2) ↗
  3. Linux man-pages: proc_pid_maps(5) ↗
  4. Linux kernel: Memory Protection Keys ↗