Definition
An overflow-safe range check is a test that a requested span of a buffer lies inside the buffer, written so that the test’s own arithmetic cannot overflow or wrap for any input. For a buffer of bytes and a request for bytes starting at offset , the condition to establish is , but computing can wrap. The safe form tests first and then . After the first test passes, the subtraction cannot go below zero.
The idea
A file format stores records by position: each record header gives an offset and a length , both 32-bit unsigned numbers chosen by whoever wrote the file. Before copying, the parser checks if (o + n > S) reject();, where is the size of the data it holds. An attacker writes a huge . The sum exceeds , wraps around to a small number, and passes the test. The copy then reads gigabytes past the end of the buffer: an out-of-bounds read §4.5, or a crash.
The fix rests on one observation. The end position may be a number the type cannot hold, but the room left after the offset, , is never larger than , which the type does hold. So the check asks how much room is left and compares the request with that, instead of asking where the request would end. The only danger in is an offset beyond the end, which would make the room negative, and the first test rules that out.
How it works
The inputs are the buffer size , the offset and the length , all of one unsigned type such as size_t. The check runs in this order:
If any input arrives in a signed type, reject a negative value, then convert the value to the unsigned type.
Test . If the test fails, reject: the span would start beyond the end.
Compute the room left, . Because , the result lies between 0 and and cannot wrap.
Test . If the test fails, reject.
Only now form the address of byte and access the bytes.
In C the two tests fit in one condition, if (o > S || n > S - o) reject();. The operator || evaluates its right side only when its left side is false, that is, only when ; C11, the 2011 edition of the ISO standard that defines the C language, guarantees this order in section 6.5.14.
The same idea gives safe forms for other shapes of request:
A count of elements. For elements of bytes each, with , test and then , where means rounded down. This avoids the product , which can also wrap.
A start and an end. If the request gives a start and an end , the position one past its last byte, test and . The length is then safe to compute.
A pointer. With a pointer
palready known to lie in the buffer and a pointerendone past the buffer’s last byte, testn <= (size_t)(end - p). Do not computep + nfirst: pointer arithmetic §4.25 that goes more than one element past the end of an array §14.5 is undefined behavior §4.12 in C, and compilers have removed checks such asp + n < pfor that reason (see the Pointer arithmetic entry).A wider type. If and are 32-bit values, the test
(uint64_t)o + n <= Scannot wrap, because the largest possible sum, , is far below .
Worked example
Take 32-bit unsigned arithmetic and a buffer of bytes. An attacker sends and .
The naive check computes . The true sum is , which exceeds the largest 32-bit value, so it wraps to . Since , the naive check accepts, and the program reads 4,294,967,280 bytes starting at offset 100.
The safe check first tests , which passes. The room left is . The test fails, so the request is rejected.
The first test matters on its own. Suppose a program omits it and checks only . An attacker sends and . The true room is , which wraps to . The test passes, and the program reads 10 bytes that start 904 bytes past the end. With the first test, fails and the request is rejected.
At the boundary, and pass, since and . The span covers bytes 4,000 to 4,095, and 4,095 is the last valid position. A length of 97 fails.
For elements, take and records of bytes. At most records fit. A count , which is , fails the test . A naive test that computes in 32 bits gets and accepts the count.
Stated precisely
For nonnegative integers , and , Proof: if , then because , and subtracting gives . Conversely, adding to both sides of gives . For elements of bytes and nonnegative integers and , , because means , and a whole number is at most exactly when it is at most . The check uses .
In an unsigned type whose largest value is : when , the computed equals the true difference, because . The comparisons involve no further arithmetic. Therefore the C condition o <= S && n <= S - o is true exactly when the true sum satisfies , for every value of the type.
The guarantee rests on three assumptions.
, and are nonnegative and held in one unsigned type, or converted to it without a change of value.
is the true size of the memory that the access will use, not a size claimed by the input.
The values checked are the values used: none of them changes between the check and the access.
With unsigned types, evaluating too early would do no harm, because a wrapped only occurs together with a failed . With signed types it would: for a negative near the minimum overflows, which is undefined behavior, so the subtraction must run only after has been established, as && and || ensure.
Limits and common mistakes
A range check proves only that numbers fit. It does not prove that the buffer still exists (see the Object lifetime §4.1 entry), that describes the memory actually accessed, or that the requester may read those bytes.
The values must not change after the check. If or lives in memory that another party can modify, such as a buffer shared with another processor, the program copies each value once into a local variable and checks and uses the copy. Otherwise it has a time-of-check to time-of-use §9.26 (TOCTOU) error.
The empty span at the end, with , passes, which is correct because it touches no byte. Code must not then read the byte at offset . Some library functions require a valid pointer even for a length of 0: in C11, calling memcpy with a null pointer §4.26 is undefined behavior even when the length is 0.
A check written in signed arithmetic, such as if (o + n > S) with int values, can perform a signed integer overflow §4.15, which is undefined behavior, not merely a wrong answer.
Boundary-value testing §10.31, testing at the edges of each input range, finds mistakes in such checks. The edges here are , , , and the largest value of the type for and for .
Questions answered
Which precondition makes subtraction in the check safe?
The precondition is : the offset must be no larger than the buffer size, and the program must establish it before computing the room left, . Here is the buffer size in bytes, the offset where the requested span starts, and the span’s length. With , the true difference lies between 0 and , so an unsigned subtraction cannot wrap and a signed one cannot overflow. The check then accepts only if , which is equivalent to but never forms the sum , the value that can wrap. In C: if (o > S || n > S - o) reject();, where || evaluates n > S - o only when o > S is false.
Without the precondition the subtraction itself wraps: with 32-bit unsigned values, and give , and almost any length passes. The values must also be nonnegative in the type used, so a signed offset is first tested against 0, and they must be the same values that the later access uses.
Related entries: Integer promotion and conversion (§4.18), Pointer provenance (§4.24), Buffer overflow (§4.4), Out-of-bounds read (§4.5), Unsigned modular arithmetic (§4.16), Pointer arithmetic (§4.25), Time-of-check to time-of-use (TOCTOU) (§9.26), An overflow-safe access predicate (§17.2). Further reading: [78].