Section 2.29

Temporal memory safety

Definition

Temporal memory safety is the property that a program accesses an object only during the object’s lifetime: after the object’s memory has been reserved and before that memory has been released. A violation is an access through a dangling pointer §4.1, one that still holds the address of an object whose lifetime has ended. Examples are use-after-free §4.6, an access to memory after it was freed, and double free §4.7, releasing the same memory twice. Temporal safety concerns when an access happens; spatial memory safety §2.28 concerns where it lands.

The idea

A host keeps a table of records and lets a guest program, code it runs but does not trust, look one up. The lookup returns the record’s address, and the guest stores it. Later an update deletes the record, the allocator, the host code that hands out memory, gives the same memory to a new record, and the guest uses its stored address. The address still lies in memory the guest may touch, so a runtime bounds check §2.27 passes, and the guest reads or overwrites a record that is not the one it looked up.

An address names a place, not an object. Objects come and go at the same place, so the right to access an object begins and ends in time, and a stored address records neither moment. A hotel key card is a fair picture: room 7 still exists after a guest checks out, a new guest soon sleeps there, and the lock must reject cards issued before the newest one. The picture stops being accurate in one respect: a raw address has no lock. It keeps working unless the system adds a check like the hotel’s, or ensures nobody still holds the address when the memory is reused.

How it works

One family of defenses keeps each object alive while anything can still use it, so no pointer dangles. The other lets the object die but makes every later use detectable.

Testing tools such as AddressSanitizer §10.22 (ASan) add detection without a guarantee: they hold freed memory back from reuse for a while and report accesses to it.

Worked example

Slot 2 of a host table holds record X at address 0x20000040, with a count field of 5; the slot’s generation is 5. A guest looks up X and keeps either the address 0x20000040 or the handle (slot 2, generation 5). An update deletes X, so the generation becomes 5+1=65 + 1 = 6, and a new record Y, with count 99, is placed in slot 2 at the same address.

With the raw address, the guest’s next read returns 99. A region check passes, because the address lies in the table’s region; the guest receives Y’s data believing it is X’s, and a write would corrupt Y. With the handle, the lookup compares generation 5 with 6, finds them unequal and refuses. The address is the same in both cases; only the generation tells X and Y apart.

A generation number has a fixed width and wraps around. In 2 bits its values run 0, 1, 2, 3, 0. A handle with generation 1 that survives 4 frees of its slot meets generation 1+4=51 + 4 = 5, stored as 5−4=15 - 4 = 1, and is wrongly accepted. In 32 bits, a slot freed once per microsecond, 10610^{6} times per second, repeats a generation after 232/106≈4,2952^{32} / 10^{6} \approx 4{,}295 seconds, about 71.6 minutes. In 64 bits the same rate takes 264/1062^{64} / 10^{6} seconds, about 584,542 years.

Stated precisely

Give each object oo a lifetime from the time talloc(o)t_{\mathrm{alloc}}(o) when its memory is reserved to the time tfree(o)t_{\mathrm{free}}(o) when it is released. An access at time tt through a pointer derived from oo is temporally safe exactly when talloc(o)≤t<tfree(o)t_{\mathrm{alloc}}(o) \le t < t_{\mathrm{free}}(o). The object is the one the pointer was derived from, not whatever lies at the address: if the memory of oo now holds a new object o′o', an access through the old pointer after tfree(o)t_{\mathrm{free}}(o) is a violation although o′o' is alive there.

For generation handles, let slot ii have current generation GiG_i, stored in kk bits and increased by 1 modulo 2k2^{k} each time the slot’s object is freed. A handle (i,g)(i, g) is accepted exactly when slot ii holds an object and Gi=gG_i = g. Every accepted access is temporally safe under two assumptions.

  1. No repeat: between the creation of a handle and its last use, the slot’s object is freed fewer than 2k2^{k} times, so GiG_i cannot return to gg.

  2. Check and access together: the object cannot be freed between the comparison and the end of the access; otherwise the result is a time-of-check to time-of-use §9.26 (TOCTOU) error.

Limits and common mistakes

A bounds check cannot detect a temporal violation, because freed memory usually stays inside the same permitted region.

Generation numbers are subject to counter wraparound §6.21, as the example shows. A value that changes and returns to its old value, so that a comparison wrongly sees no change, is the ABA problem §9.25, named for the sequence of values A, B, A.

Reference counting has two classic faults. A lookup that reads a pointer and only then increments the count can lose a race with a deletion between those steps. And objects that point to each other keep each other’s counts above zero and are never freed, a memory leak §4.8.

Temporal safety does not make data current or permitted. A live object can hold an outdated value, and a program can hold a valid pointer to a live object it should no longer be allowed to use; withdrawing permission is a separate mechanism.

A double free is also a temporal violation: the second release acts on memory whose lifetime has ended, and it can corrupt the allocator’s own records.

Questions answered

Can a guest retain an address after an update frees its object?

Yes, a guest can retain the address: an address is a number, and freeing the object does not erase copies of it in the guest’s registers or memory. What matters is whether a later use succeeds. If the runtime checks only that each access falls in a permitted region, and the freed memory still lies in one, the use succeeds and reaches whatever object now occupies that memory: a use-after-free.

A safe runtime makes the retained address harmless in one of three ways. It never gives the guest a host address, only a copy of the data or a handle whose generation number is checked at every use. Or it keeps every object the guest can reach alive until the guest’s current run ends, clears the guest’s registers and scratch memory between runs, and lets no host address into state that outlives a run. Or it removes freed memory from the guest’s permitted regions before that memory is reused.

Related entries: Object lifetime (§4.1), Use-after-free (§4.6), BPF map (§2.18), Double free (§4.7), Read-copy update (RCU) (§9.20), Reference counting (§9.22), ABA problem (§9.25), Copy-only helper (§2.22).