Section 17.2

An overflow-safe access predicate

Question

Derive a safe test for an access of width ww at offset oo into a region of size nn.

Short answer

Accept the access only if w≤nw \le n and o≤n−wo \le n - w, tested in that order, where oo, ww and nn are whole numbers of bytes that are never negative. Together the two comparisons say exactly that o+w≤no + w \le n: the access ends at or before the end of the region. The test never adds oo and ww, so no sum can exceed the largest value an integer holds. It subtracts only after w≤nw \le n has shown that the result cannot be negative.

Terms in the question

Full answer

  1. List the bytes touched. An access of width w≥1w \ge 1 at offset oo touches bytes oo, o+1o + 1, and so on up to o+w−1o + w - 1.

  2. State the goal in ordinary mathematics. The first byte touched cannot lie in front of the region, because o≥0o \ge 0 always holds for an unsigned number. The last byte touched does not lie beyond the region when o+w−1≤n−1o + w - 1 \le n - 1, which is the same as o+w≤n.o + w \le n. Every byte from the first to the last then lies inside the region.

  3. Do not compute this form directly. The sum o+wo + w can exceed the top value of the integer type. The stored sum then wraps around to a small number, and the comparison can pass for an access far outside the region.

  4. Move the addition to the other side. Subtracting ww from both sides gives o≤n−wo \le n - w. This form has no addition. It has a subtraction, and n−wn - w is below zero when w>nw > n. An unsigned subtraction then wraps around. With 32-bit values, n=64n = 64 and w=100w = 100 give 64−100+232=4,294,967,26064 - 100 + 2^{32} = 4{,}294{,}967{,}260, and the comparison would accept every offset up to that number.

  5. Guard the subtraction. Test w≤nw \le n first. If it fails, the access is wider than the whole region and is refused. If it holds, n−wn - w lies between 0 and nn, so the subtraction is exact.

The safe test is therefore w≤nando≤n−w.w \le n \quad \text{and} \quad o \le n - w. It is an overflow-safe range check §4.17: a bounds test whose own arithmetic cannot overflow. It is exact in both directions. If both comparisons hold, adding ww to both sides of the second one gives o+w≤no + w \le n. If o+w≤no + w \le n holds and o≥0o \ge 0, then w≤nw \le n, and subtracting ww gives o≤n−wo \le n - w.

In C the test is w <= n && o <= n - w, with all three variables of the same unsigned type, for example size_t, the type that C uses for the sizes of objects. The operator && evaluates its right side only when its left side is true, which gives the required order. The mirror-image test, o≤no \le n and then w≤n−ow \le n - o, is correct by the same argument.

Worked example

Take a region of n=64n = 64 bytes and an access of width w=4w = 4. The first comparison is 4≤644 \le 64, which is true. The subtraction gives n−w=64−4=60n - w = 64 - 4 = 60, the largest offset at which a 4-byte access still fits.

Now the attack that the derivation guards against. All values are 32-bit unsigned integers. An attacker supplies o=4,294,967,294o = 4{,}294{,}967{,}294 and w=4w = 4. The true sum is 4,294,967,298. It does not fit in 32 bits, so the stored sum is the true sum minus 2322^{32}: 4,294,967,298−4,294,967,296=24{,}294{,}967{,}298 - 4{,}294{,}967{,}296 = 2. A test that adds first compares 2≤642 \le 64 and accepts an access that starts almost 4 GiB beyond the start of a 64-byte region. The safe test compares 4≤644 \le 64, which is true, and then 4,294,967,294≤604{,}294{,}967{,}294 \le 60, which is false. Refused.

A script that tries all 16,777,216 combinations of 8-bit values for oo, ww and nn confirms the derivation. The safe test agrees with o+w≤no + w \le n every time. A test that adds first, with the sum stored in 8 bits, wrongly accepts 5,592,320 combinations.

Follow-up questions and limits

What if the offset is a signed number? Refuse a negative offset before the test. A signed comparison §2.12 accepts o=−1o = -1 in the example, because −1≤60-1 \le 60, and the access then touches the byte in front of the region. Converting the offset to the unsigned type of the same width before the test also works, as long as the region is smaller than half the range of that type. For a 32-bit type the C conversion adds 2322^{32} to a negative value, so −1-1 becomes 4,294,967,295 and every negative offset becomes at least 2312^{31}, which the test refuses.

What does a width of zero mean? With w=0w = 0 the test accepts every o≤no \le n, including o=no = n. An empty access touches no byte, so nothing is out of bounds, but no byte may be read at offset nn. Code that receives a yes for w=0w = 0 must therefore touch no byte. A design may also refuse w=0w = 0 outright.

How is an access to cc items of kk bytes each, starting at offset oo, checked? Do not compute c×kc \times k first, because a product can wrap around as a sum can. Test o≤no \le n, then test c≤(n−o)/kc \le (n - o) / k, where the division rounds down and kk is at least 1. For n=64n = 64, o=4o = 4 and k=8k = 8, the division gives 60/8=7.560 / 8 = 7.5, rounded down to 7. Seven items take 7×8=567 \times 8 = 56 bytes, bytes 4 to 59, and fit. Eight items would take 64 bytes, bytes 4 to 67, and do not fit.

Why not compare pointers? In C, pointer arithmetic §4.25 is itself undefined behavior when its result lies in front of the array §14.5, or beyond the position just after the last element of the array. A test written as start + o + w <= end can therefore go wrong before it compares anything. Check the numbers first and form the pointer afterwards.

Is the access safe once the test passes? The test settles one fact: the numbers fit. It does not show that the region still exists, that the region permits writing, that nn is the current size of the region, or that the address has the alignment §4.22 the processor requires, for example a multiple of 4 for a 4-byte value. Each of these facts needs its own guarantee.

Fullest treatment: Overflow-safe range check (§4.17).

Related entries: Integer promotion and conversion (§4.18), Object lifetime (§4.1), Pointer provenance (§4.24), Runtime bounds check (§2.27), Unsigned modular arithmetic (§4.16), Signed integer overflow (§4.15), Pointer arithmetic (§4.25). Further reading: [78].