Section 17.3

Why a bounds check is not a lifetime check

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

Full answer

  1. 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.

  2. 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.

  3. 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.

  4. 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.

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 ww at pool offset oo is allowed when w≤64w \le 64 and o≤64−wo \le 64 - w.

  1. Time 1. Object A, a 16-byte settings record, occupies block 1. Reader R reads current and 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.

  2. Time 2. Updater U stores a new settings record in block 2, sets current to its address, 0x20000020, and frees A.

  3. 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.

  4. Time 4. R reads 4 bytes at 0x20000010 plus 8, which is address 0x20000018 and pool offset 16+8=2416 + 8 = 24. The bounds check finds 4≤644 \le 64 true and 24≤64−4=6024 \le 64 - 4 = 60 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].