Section 4.24

Pointer provenance

Definition

Pointer provenance is the origin of a pointer value: the object or memory allocation from which the pointer was derived, and the only one it may be used to access. In C, and in the reasoning of optimizing compilers, a pointer is an address together with its provenance, so two pointers with the same numeric address but different origins are not interchangeable. A pointer derived from object A keeps A’s provenance through arithmetic and copying, and it may not access object B even when its address falls inside B.

The idea

In machine code a pointer is just a number, the address of a byte. If C pointers were only numbers, a compiler could never conclude that a store through a pointer leaves some variable unchanged, because any pointer might hold any address. Take a function with a local variable y whose address it never hands out, and a store through a pointer parameter p. The compiler wants to keep y in a register across that store. Numbers alone cannot justify this, since p might equal the address of y. Provenance does: p was not derived from y, so it may not access y, whatever its value.

A key cut for one hotel room is a fair picture: even if the key’s tag shows the number of the next room, the key opens only its own. The picture stops being accurate on ordinary hardware, where nothing physically stops a pointer from reaching another object’s memory. The rule lives in the language and in the compiler’s reasoning, so breaking it gives silently wrong results instead of a locked door. Hardware such as CHERI (Capability Hardware Enhanced RISC Instructions, where RISC means reduced instruction set computer) does build the lock, as an application of capability-based security §1.40: every pointer carries bounds and permissions that the processor checks, and an integer cannot become a valid pointer.

How it works

A pointer’s provenance passes through four stages:

  1. Creation. A pointer gets provenance when it is made from an object: by taking an address with &x, by using an array’s §14.5 name, or as the result of malloc.

  2. Inheritance. Pointer arithmetic §4.25, conversion to another pointer type, and copying the pointer, even byte by byte with memcpy, keep the provenance. Pointer arithmetic is valid only within the object and up to the position just past its end.

  3. Expiry. When the object’s lifetime ends, for example at free or when a function returns, every pointer with that provenance becomes a dangling pointer §4.1. A new object placed later at the same address does not revive it.

  4. Round trip through an integer. C11, the 2011 edition of the C standard, offers the optional type uintptr_t: an unsigned integer type such that a valid pointer to void, converted to uintptr_t and back, compares equal to the original. Converting any other integer to a pointer is implementation-defined behavior §4.13, which each compiler must choose and document; the standard warns that the result may be misaligned, may not point to an object of the pointed-to type, and may not be a valid pointer value at all.

C11 does not use the word provenance. The C standards committee stated the idea in 2004, in its response to Defect Report 260 [455]: an implementation may treat pointers with different origins as distinct even when their bit patterns are identical. The technical specification ISO/IEC TS 6010:2025, an optional supplement to the C standard, spells out a provenance-aware memory model [456]. In it, converting a pointer to an integer exposes the object it points to, and converting an integer back to a pointer can regain provenance only from an exposed object whose memory includes that address or ends exactly at it.

Hardware registers are the case where an integer address is the only origin there is. A register reached through memory-mapped I/O §4.35 (input and output through ordinary memory addresses), say at 0x40000000, is no C object. Firmware reaches it with (volatile uint32_t *)0x40000000, an integer-to-pointer conversion justified by the compiler’s documented meaning for it and by the chip’s memory map. The volatile qualifier §4.34 (volatile) tells the compiler that every such access must really take place.

Worked example

This program comes from the 2019 paper “Exploring C Semantics and Pointer Provenance” by Kayvan Memarian and colleagues [457]:

int y = 2, x = 1;
int *p = &x + 1;
int *q = &y;
if (memcmp(&p, &q, sizeof(p)) == 0) {
*p = 11;
printf("%d %d %d %d", x, y, *p, *q);
}

The function memcmp compares the bytes of the two pointers, so the branch runs only when p and q hold the same address.

Suppose the compiler places x at address 0x1000 and y directly after it at 0x1004, each int being 4 bytes. Then &x + 1 is 0x1000 plus 1×41 \times 4 bytes, which is 0x1004: the position just past x, which C allows a program to form. q holds 0x1004 too, so the branch runs. If pointers were only addresses, *p = 11 would overwrite y, and the program would print 1, 11, 11, 11 for x, y, *p and *q.

The paper reports that GCC §12.17 8.1 (GCC is the GNU Compiler Collection; GNU, a free-software project, stands for GNU’s Not Unix) at optimization level -O2, on platforms that placed the variables this way, printed 1, 2, 11, 2: it reasoned that p, derived from x, cannot write y, and printed the value 2 it already knew for y and *q. Clang 6.0 printed 1, 11, 11, 11. Both outputs are allowed, because the program has undefined behavior §4.12: p has the provenance of x, and a pointer just past an object may be formed and compared but never used to access memory. The paper also notes that -fno-strict-aliasing does not change GCC’s result, so provenance is a rule separate from strict aliasing §4.23.

Address reuse shows the same rule over time. p = malloc(16) returns 0x20001000; free(p) ends that object; q = malloc(16) returns 0x20001000 again. A check that the number in p lies between 0x20001000 and 0x2000100F, the 16 bytes of the new object, passes. Yet p has the provenance of a dead object, and any use of it is a use-after-free §4.6. In C11 even comparing p with q is undefined, because a pointer’s value becomes indeterminate when its object’s lifetime ends.

Stated precisely

Provenance has no formula; the contract is a list of conditions. An access through a pointer pp to a value of type TT is valid only if all of the following hold:

  1. pp has the provenance of an object OO: it was derived from the address of OO, from the name of an array OO, or from the allocation that created OO, through permitted arithmetic, conversions and copies, or through a round trip of such a pointer via uintptr_t. For a hardware register, the compiler’s documented meaning of the integer-to-pointer conversion takes the place of this condition.

  2. OO is within its lifetime.

  3. All bytes from the address in pp up to that address plus the size of TT, minus 1, lie inside OO.

  4. The address in pp is aligned for TT.

  5. The effective-type rule allows OO to be accessed through type TT.

The numeric address decides none of condition 1. Section 6.5.9 of C11 even states that a pointer just past the end of one array may compare equal to a pointer to the start of a different array that happens to follow it in memory, so equal addresses do not make two pointers interchangeable.

Limits and common mistakes

Standard C has not settled the details. C11 has no explicit provenance rules, ISO/IEC TS 6010 is optional, and compilers differ in edge cases such as integer round trips and byte-wise copies of pointers. The core is certain: a pointer derived from one object must not access another, and compilers optimize on that basis.

No check happens at run time. On ordinary hardware a violation produces no fault, only silently wrong results. AddressSanitizer §10.22, a run-time checker that the compiler inserts, catches many of the resulting out-of-bounds and use-after-free accesses, but it checks addresses against object boundaries, not provenance itself.

A known address is not a right of access. A program that learns another object’s address, by printing or computing it, gains no permission to use that number as a pointer to the object.

Provenance is a rule for programs, not a security boundary. An attacker who corrupts a pointer through a memory bug reaches whatever the hardware allows; only capability hardware such as CHERI enforces provenance at run time.

Hardware addresses need their own contract. A fixed integer address is valid only for registers that the chip documents, with the access width the chip requires; the same cast to an arbitrary RAM address has no such contract.

Questions answered

Can converting an arbitrary integer into a pointer justify dereferencing it?

No. In C, converting an integer to a pointer is implementation-defined behavior, and the standard says only that the result might be misaligned, might not point to an object of the pointed-to type, and might not be a valid pointer value at all. A dereference, an access to the memory a pointer designates, is justified by the pointer’s provenance: its origin in a live object, obtained by taking the object’s address or by allocating it, and carried through permitted arithmetic or through a round trip via uintptr_t, the optional integer type that holds a pointer to void and converts back to an equal pointer. An integer that merely equals a valid address carries none of this, and an optimizing compiler may assume that an access through it does not touch objects whose addresses the program never exposed.

Two cases rest on other contracts and are legitimate: an integer obtained from a valid pointer through uintptr_t and converted back while the object is alive, and a fixed address that the chip’s memory map and the compiler’s documentation define, such as a memory-mapped hardware register accessed through a volatile pointer. Even then, the object’s bounds and lifetime, the alignment §4.22 and the type rules must still hold.

Related entries: Object lifetime (§4.1), Pointer arithmetic (§4.25), Memory-mapped I/O (MMIO) (§4.35), Capability security (§1.40), Use-after-free (§4.6), Strict aliasing and effective type (§4.23), Volatile qualifier (§4.34). Further reading: [78, 455, 456, 457].