Section 2.28

Spatial memory safety

Definition

Spatial memory safety is the property that every memory access touches only bytes inside the bounds of the object it is meant to reach: the variable, array §14.5 or buffer from which the access’s pointer was derived. A pointer is a value that holds a memory address. A violation is an out-of-bounds access, such as a buffer overflow §4.4, which writes past the end of a buffer, or an out-of-bounds read §4.5. Spatial safety concerns where an access lands; temporal memory safety §2.29 concerns when it happens.

The idea

In C, an 8-byte buffer is 8 bytes of memory with other data on either side. Copying a 12-byte name into it does not stop at byte 8: the last 4 bytes overwrite whatever lies next, perhaps a flag that records whether the user is an administrator. C does not check accesses against the end of an object, so nothing notices. Spatial safety is the guarantee that this cannot happen: each object’s extent is a fence, and no access through a pointer to the object crosses it.

Every enforcement mechanism puts its fence somewhere, and the useful question is where. A sandbox §2.30 that runs a guest program in a 16-byte region may check only that each access stays inside those 16 bytes. That fence protects the host from the guest, but not one of the guest’s objects from a pointer meant for another. An apartment building is a fair picture: a locked front door keeps strangers out, yet any resident can enter a neighbor’s flat unless each flat has its own lock. The picture stops being accurate in one respect: memory has no doors, so a fence around each object exists only if the system records each object’s bounds, or proves that accesses stay within them.

How it works

Enforcement needs two facts at each access: the bytes the access touches, and the bounds of the object its pointer belongs to. Mechanisms differ in where they keep the bounds and when they compare.

The bound must come from the object the pointer was derived from, not from whatever lies at the address; this link between a pointer and its object is called pointer provenance §4.24. An address inside object B does not reveal whether the program reached it through a pointer to B, which is allowed, or by running past the end of A, which is a violation. A mechanism that keeps only addresses and region bounds can enforce the region’s fence but not the fences between objects inside it.

Worked example

A guest’s region has offsets 0 to 15. Object A, 8 bytes, occupies offsets 0 to 7 and holds eight zeros; object B, 8 bytes, occupies offsets 8 to 15 and holds eight ones.

A 1-byte write through A at index 9 targets offset 0+9=90 + 9 = 9. A region check finds offset 9 in 0 to 15 and allows it, so the second byte of B changes from 1 to 9. An object check finds index 9 not below A’s length 8 and refuses. In the first case no host byte outside offsets 0 to 15 changes: the guest stayed confined, yet its object B was corrupted.

A 2-byte read through A at index 7 touches offsets 7 and 8. The region has 16−7=916 - 7 = 9 bytes left from offset 7, and 2≤92 \le 9, so a region check allows it. Object A has 8−7=18 - 7 = 1 byte left, and 2>12 > 1, so an object check refuses. Offset 8 is the first byte of B. A 1-byte read at the same index would pass both checks. Both checks computed correctly; they answer different questions.

Stated precisely

Let a pointer pp be derived from an object that starts at address bb and is nn bytes long. An access through pp that touches w≥1w \ge 1 bytes starting at address aa is spatially safe at object granularity exactly when b≤ab \le a and a+w≤b+na + w \le b + n, computed over the mathematical integers. The object is the one pp was derived from, whatever lies at aa. A program is spatially safe when every access of every run meets this condition. Spatial safety is a safety property §1.11: one bad access is a violation, and nothing later repairs it.

Region granularity replaces the object by the permitted region that contains it, a weaker property: every access safe at object granularity is safe at region granularity, but not the reverse, as the example shows. A claim of spatial safety is precise only when it names its granularity. A pointer one past the end, a=b+na = b + n, fails the condition for every w≥1w \ge 1; C lets a program compute and compare such a pointer but not access memory through it.

Limits and common mistakes

Spatial safety says nothing about time. An access within the bounds of an object that was already freed is spatially safe and still a use-after-free §4.6, a violation of temporal memory safety.

In bounds does not mean initialized or permitted. An in-bounds read of an uninitialized buffer, or of a secret field, is spatially safe.

Overflow inside one object escapes per-object checks. A structure holding an 8-byte array followed by a flag is one object, so writing 9 bytes into the array changes the flag without leaving the object. AddressSanitizer does not detect such intra-object overflows by default.

A detection tool is not a guarantee. An index large enough to jump over a redzone into another live object goes unreported by AddressSanitizer, and such tools are normally used only during testing, because they slow the program and use extra memory.

Claims of memory safety differ in granularity. A sandbox that claims memory safety usually guarantees confinement to its region: the host is safe from the guest, but the guest’s objects are not safe from each other.

Questions answered

Does checking a shared region also enforce each logical object’s boundary?

No, checking a shared region enforces only the region’s own boundary: it refuses accesses that leave the region and allows every access inside it, including one that runs from one object into the next. In a 16-byte region holding object A at offsets 0 to 7 and object B at offsets 8 to 15, a write through A at index 9 lands in B and passes the region check, and so does a 2-byte read through A at index 7, which touches offset 8. Enforcing each object’s boundary needs that object’s start and length at every access, carried with the pointer as in a fat pointer, or a static proof that no access through a pointer to A leaves A. A region check protects the host from the guest, not the guest’s objects from each other.

Related entries: Runtime bounds check (§2.27), Buffer overflow (§4.4), Temporal memory safety (§2.29), Out-of-bounds read (§4.5), Pointer provenance (§4.24), Software fault isolation (SFI) (§2.41).