Question
Derive a safe test for an access of width at offset into a region of size .
Short answer
Accept the access only if and , tested in that order, where , and are whole numbers of bytes that are never negative. Together the two comparisons say exactly that : the access ends at or before the end of the region. The test never adds and , so no sum can exceed the largest value an integer holds. It subtracts only after has shown that the result cannot be negative.
Terms in the question
Region of size : a block of consecutive bytes that the program may use, numbered 0 to .
Offset : the number of the first byte of the access.
Width : how many consecutive bytes the access reads or writes.
Safe test: a yes-or-no check made before the access. It says yes only when every byte of the access lies inside the region. Its own arithmetic must be exact for every input, because an attacker may choose and .
Overflow: an arithmetic result that does not fit in the integer type that must hold it. A computer integer has a fixed size. An unsigned integer cannot be negative, and a 32-bit unsigned integer holds 0 to 4,294,967,295. In the C language an unsigned result outside that range wraps around: for a 32-bit type, C adds or subtracts until the result fits, so gives 0 and gives 4,294,967,295. A signed integer can be negative. Signed integer overflow §4.15 is undefined behavior §4.12 in C, which means that the C standard sets no requirement on how the program behaves.
Full answer
List the bytes touched. An access of width at offset touches bytes , , and so on up to .
State the goal in ordinary mathematics. The first byte touched cannot lie in front of the region, because always holds for an unsigned number. The last byte touched does not lie beyond the region when , which is the same as Every byte from the first to the last then lies inside the region.
Do not compute this form directly. The sum 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.
Move the addition to the other side. Subtracting from both sides gives . This form has no addition. It has a subtraction, and is below zero when . An unsigned subtraction then wraps around. With 32-bit values, and give , and the comparison would accept every offset up to that number.
Guard the subtraction. Test first. If it fails, the access is wider than the whole region and is refused. If it holds, lies between 0 and , so the subtraction is exact.
The safe test is therefore 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 to both sides of the second one gives . If holds and , then , and subtracting gives .
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, and then , is correct by the same argument.
Worked example
Take a region of bytes and an access of width . The first comparison is , which is true. The subtraction gives , the largest offset at which a 4-byte access still fits.
Offset 60: is true. The access touches bytes 60, 61, 62 and 63, and byte 63 is the last byte of the region. Accepted.
Offset 61: is false. The access would touch bytes 61 to 64, and the region has no byte 64. Refused.
Now the attack that the derivation guards against. All values are 32-bit unsigned integers. An attacker supplies and . The true sum is 4,294,967,298. It does not fit in 32 bits, so the stored sum is the true sum minus : . A test that adds first compares and accepts an access that starts almost 4 GiB beyond the start of a 64-byte region. The safe test compares , which is true, and then , which is false. Refused.
A script that tries all 16,777,216 combinations of 8-bit values for , and confirms the derivation. The safe test agrees with 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 in the example, because , 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 to a negative value, so becomes 4,294,967,295 and every negative offset becomes at least , which the test refuses.
What does a width of zero mean? With the test accepts every , including . An empty access touches no byte, so nothing is out of bounds, but no byte may be read at offset . Code that receives a yes for must therefore touch no byte. A design may also refuse outright.
How is an access to items of bytes each, starting at offset , checked? Do not compute first, because a product can wrap around as a sum can. Test , then test , where the division rounds down and is at least 1. For , and , the division gives , rounded down to 7. Seven items take 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 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].