Question
Give a counterexample in which an in-range pointer remains unsafe.
Short answer
A pointer to an object that has already been freed. A reader holds a pointer to object A inside a memory pool. An updater frees A, and the program reuses the same bytes for a new object B. The reader then reads through its old pointer. The address has not changed and still lies inside the pool, so a bounds check passes, yet the read returns the data of B. A bounds check tests where bytes are. It cannot test whether the object that was there still exists.
Terms in the question
Pointer: a value that holds the memory address of some data.
In range: the bytes that the pointer refers to lie inside the memory region the code is allowed to use. A bounds check tests exactly this.
Unsafe: using the pointer can read data that the code was never meant to see, or overwrite data that belongs to something else.
Counterexample: one concrete case that disproves a general claim. The claim here is “a pointer that passes the bounds check is safe to use”.
Allocator: the part of a program that hands out blocks of memory on request and takes them back when they are freed. The larger region that the blocks come from is called a pool here.
Object lifetime §4.1: the period during which an object exists. For memory obtained from the standard allocation functions of the C language, such as
malloc, the C standard defines it as the time from the allocation until the deallocation. An access to an object after it was freed is called a use-after-free §4.6.
Full answer
Safe use needs two independent facts. The bytes must lie in memory that the code may use. This is a fact about place, and guaranteeing it is called spatial memory safety §2.28. The object that the pointer was created for must still exist. This is a fact about time, and guaranteeing it is called temporal memory safety §2.29.
A bounds check sees only place. Its inputs are numbers: an offset, a width and a region size. Freeing an object changes none of these numbers, so the check gives the same answer before the free and after it.
Freed memory is reused. An allocator keeps freed bytes in order to hand them to a later request that fits. The same address then holds a different object, which may belong to another part of the program.
The old pointer now reaches the new object. A read returns the contents of the new object, which may be secret. A write damages the new object. Both accesses are in range.
The repair is a rule about time. The rule must cover the reader from the moment it obtains the pointer until its last use. A function that stores the pointer in order to use it later counts as a reader until that later use is over. Four rules are common.
A lock. A lock is an object that only one part of the program can hold at a time. The reader takes the lock before it obtains the pointer and releases the lock only after its last use of the pointer. The code that frees the object takes the same lock first. While holding the lock, that code also removes every stored pointer through which a later reader could find the object.
Reference counting §9.22. The object carries a count of its users and is freed only when the count returns to zero.
Deferred freeing. The updater first replaces the shared pointer through which readers find the object, so that new readers can no longer reach the old object. The updater then waits until every reader that started earlier has finished, and only then frees the object. The wait is called a grace period §9.21. Read-copy update (RCU) §9.20 in the Linux kernel works this way.
Checked handles. The reader holds a slot number and a generation number in place of an address. Freeing a slot increases the generation stored with the slot. Every use compares the two generation numbers and goes ahead only if they are equal. No free may happen between the comparison and the access.
Worked example
A program owns a 64-byte pool at addresses 0x20000000 to 0x2000003F. Its allocator hands out the pool in four blocks of 16 bytes, numbered 0 to 3. Block 1 covers pool offsets 16 to 31, which are addresses 0x20000010 to 0x2000001F. A shared variable named current holds the address of the settings record in use. Every access is checked against the pool: an access of width at pool offset is allowed when and .
Time 1. Object A, a 16-byte settings record, occupies block 1. Reader R reads
currentand obtains the pointer to A,0x20000010. R intends to read the 4-byte field at offset 8 of A. Before R does so, the processor switches to other work.Time 2. Updater U stores a new settings record in block 2, sets
currentto its address,0x20000020, and frees A.Time 3. Another part of the program requests 16 bytes for a secret key. The allocator returns block 1. The key, object B, now occupies the bytes that A occupied.
Time 4. R reads 4 bytes at
0x20000010plus 8, which is address0x20000018and pool offset . The bounds check finds true and true. The read proceeds and returns bytes 8 to 11 of the secret key.
The same check with the same numbers also passes at time 1, and rightly so. Nothing that the check can see differs at time 4. Only the owner of the bytes has changed. The order of events decides the outcome: R obtained the pointer at time 1, U freed the object at time 2, and R used the pointer at time 4.
Checked handles change the outcome. R would hold the handle “block 1, generation 7”. The free at time 2 raises the generation of block 1 to 8. At time 4 the handle says 7 and the block says 8, so the read is refused.
Follow-up questions and limits
Is the read safe if the block has not been reused yet? No. The lifetime of A has ended, so the read is still a use-after-free. The allocator may have overwritten part of the freed block with its own records, and the block can be reused at any moment. For memory from malloc, the C standard states that referring to an object outside its lifetime is undefined behavior §4.12, which means that the standard sets no requirement on what happens.
Does a reference count prevent the error? Only if the reader adds itself to the count while A is certain to exist, for example under a lock that the freeing code also takes. Adding to the count through a pointer to an object that is already freed writes into freed memory, which is the same error again.
Does changing current in one indivisible step prevent the error? No. An atomic operation §9.8 on current ensures that a new reader sees either the old address or the new one, never a mixture. It does nothing for a reader that obtained the old address earlier.
Does waiting a fixed time before freeing prevent the error? No. A delay is a guess about how long readers take. The rule must establish that the earlier readers have finished.
A freed object is one way for an in-range pointer to be unsafe. Another is a pointer that is in range for the pool but has run beyond the end of its own object into a neighboring object.
Fullest treatment: Use-after-free (§4.6).
Related entries: Reference counting (§9.22), Read-copy update (RCU) (§9.20), Grace period (§9.21), Object lifetime (§4.1), Spatial memory safety (§2.28), Temporal memory safety (§2.29). Further reading: [23, 78].