Section 4.47

Read-only memory range

Definition

A read-only memory range is a span of memory addresses whose bytes a program or its analysis relies on not changing during a stated time interval. The claim holds only against the writers that something actually stops. A memory protection unit §6.8, hardware that checks the processor’s memory accesses against permitted regions, can make the processor’s stores fail; other writers, such as a DMA §4.36 (direct memory access) controller, may not be checked by it. A complete claim names the bytes, the interval, every possible writer, and what stops each one.

The idea

An analysis of a firmware image finds a table of four function addresses in a section named .rodata, short for read-only data. It concludes that an indirect call through the table, a call whose target address is read from memory, can reach only those four functions. The conclusion is right only as long as nobody rewrites the table. The section name is a label chosen by the compiler and the linker §12.4, the tool that places code and data at addresses. The label says nothing about which hardware or software can still write there.

Read-only is always relative to a writer. A museum exhibit behind glass is read-only for visitors, but the curator holds a key, a cleaner may reach in through a service door, and the whole exhibit can be swapped for another one in the same case. A claim that the exhibit stays unchanged has to deal with each of them. The picture holds on the main point: protection that stops one writer says nothing about the others.

How it works

Establishing the claim is a check over every writer.

  1. Name the range: the first address, the length, and whether the addresses are physical or virtual, that is, translated by an MMU §6.9 (memory management unit) through page tables.

  2. Name the interval: for example, from the end of a boot-time integrity §1.9 check until the next reset, or for the duration of one function call.

  3. List every agent that could change the bytes, each through its own path:

    • stores by the processor, from unprivileged code and from privileged code, which can also reconfigure the protection;

    • stores through a second address that reaches the same bytes, called an alias of the range;

    • the erase and program operations of flash memory §6.6, the non-volatile memory that most microcontrollers §6.1 keep their code in, which a bootloader §6.30, a firmware updater or a storage layer can start;

    • DMA controllers and other bus masters, hardware units that start their own memory accesses, such as a radio, a USB controller or a second processor core;

    • a debugger attached through the chip’s debug port;

    • remapping, a change in which physical bytes an address refers to, made by editing page tables or through a chip’s address-remap feature;

    • physical faults such as bit flips or voltage glitches, if the fault model §1.30 includes them.

  4. For each agent, state what stops it during the interval: a hardware mechanism that covers that agent, such as an MPU (memory protection unit) region, a flash write-protection setting or a disabled debug port; a guarantee that the agent is not active, such as an updater that runs only before the analyzed code starts; or a stated assumption that places the agent outside the claim.

  5. If the range may legitimately change, as firmware does during an update, tie each reader to one version: the reader records which version it checked, and the update procedure guarantees that no reader is running while the bytes change.

Worked example

A table of four bytes, 1, 2, 3 and 4, sits in RAM at addresses 0x20000100 to 0x20000103. Startup code fills it, then sets up an MPU region from 0x20000100 to 0x2000011F, which is 32 bytes, the smallest region size of the Armv7-M MPU, as read-only for all processor code. An analysis replaces every later read of 0x20000101 with the value 2.

A stray store by the processor to 0x20000101 now faults, so that writer is stopped. A DMA channel is meant to receive 4 bytes from a serial port into a buffer at 0x200000FC, just below the table, but its length is set to 8 by mistake. The transfer writes from 0x200000FC up to but not including 0x20000104, which is 0x200000FC plus 8. Those 8 bytes include all four table bytes. On Cortex-M processors the MPU checks only accesses that the processor itself makes, so the DMA write succeeds, and the read of 0x20000101 returns whatever arrived over the serial line. The analysis’s replacement is now wrong. The claim needed one more line: no DMA transfer is ever set up with a destination that overlaps the table.

Had the table been in flash memory, ordinary stores could not change it, but a firmware updater could erase the flash page, which sets every byte to 0xFF, and then program new values. A read between the erase and the programming returns 255 instead of 2.

Remapping breaks the claim without any write. If an MMU maps the table’s virtual address to a different physical page, the old bytes are untouched, yet the same address now reads different values.

Stated precisely

Let RR be a set of byte addresses, let the interval run from time t0t_0 to time t1t_1, and let val(x,t)\mathrm{val}(x, t) be the byte that a read of address xx by the analyzed code at time tt returns, through the mapping the code uses at that time. The range RR is read-only during the interval when val(x,t)=val(x,t0)for every x∈R and every t with t0≤t≤t1,\mathrm{val}(x, t) = \mathrm{val}(x, t_0) \quad \text{for every } x \in R \text{ and every } t \text{ with } t_0 \le t \le t_1, in every execution that the stated threat model §1.2 and fault model allow.

The equation is a property of all those executions, not of one observation. It is established writer by writer: for each agent that can modify the bytes or the mapping, a mechanism that covers that agent prevents the change, or the agent is shown not to act during the interval, or an explicit assumption excludes it. A hash of the range checked at t0t_0 shows what the bytes were at t0t_0; it says nothing about any later time.

Limits and common mistakes

The C keyword const is a promise about the program, not a property of the memory. It makes the compiler reject a direct assignment, and writing to an object defined as const through a cast pointer is undefined behavior §4.12; neither stops a DMA controller, a debugger or other code.

Read-only does not mean unchanging. A hardware status register is read-only for software, and C declares it const volatile, yet its value changes whenever the hardware’s state changes.

Read-only does not mean secret. Anyone who can read the range can copy it, so a secret key in a read-only range is exposed to every reader.

Read-only does not mean authentic or current. A table that an attacker wrote before the protection was switched on stays the attacker’s table, because protection preserves whatever is there. A boot-time integrity check fixes the content at that moment; the protection must then cover the rest of the interval.

Aliases are easy to miss. Many microcontrollers make some memory visible at two address ranges, for example by also mapping the boot memory at address 0. An MPU checks the address used, so protection on one range does not cover the other.

Questions answered

Which writes, DMA operations, or remapping could change the range?

Any agent that can reach the physical bytes during the interval the read-only claim covers, or change which bytes the address names, can change the range. In practice the list is:

  1. the processor’s own stores, especially from privileged code that can switch off or reconfigure the MPU (memory protection unit), and stores through a second address that reaches the same bytes;

  2. the erase and program operations of flash memory, started by a bootloader, a firmware updater or a storage layer;

  3. DMA (direct memory access) controllers and other hardware that starts its own memory accesses, such as radios, USB controllers and other processor cores, which the processor’s MPU does not check;

  4. a debugger attached through the debug port;

  5. remapping, meaning page-table changes in an MMU (memory management unit) or a chip’s address-remap feature, which changes what a read returns without writing the old bytes;

  6. physical faults, if the fault model includes them.

Each must be excluded for the stated interval: by a mechanism that actually covers that agent, by showing that the agent is not active then, or by a stated assumption. A section name such as .rodata or the C keyword const excludes none of them on its own.

Related entries: Memory-mapped I/O (MMIO) (§4.35), Direct memory access (DMA) (§4.36), Assumption versus guarantee (§1.36), Memory protection unit (MPU) (§6.8), Flash memory (§6.6), Memory management unit (MMU) (§6.9). Further reading: [6].