Section 4.17

Overflow-safe range check

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 SS bytes and a request for nn bytes starting at offset oo, the condition to establish is o+n≤So + n \le S, but computing o+no + n can wrap. The safe form tests o≤So \le S first and then n≤S−on \le S - o. 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 oo and a length nn, both 32-bit unsigned numbers chosen by whoever wrote the file. Before copying, the parser checks if (o + n > S) reject();, where SS is the size of the data it holds. An attacker writes a huge nn. The sum o+no + n exceeds 232−12^{32} - 1, 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 o+no + n may be a number the type cannot hold, but the room left after the offset, S−oS - o, is never larger than SS, 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 S−oS - o 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 SS, the offset oo and the length nn, all of one unsigned type such as size_t. The check runs in this order:

  1. If any input arrives in a signed type, reject a negative value, then convert the value to the unsigned type.

  2. Test o≤So \le S. If the test fails, reject: the span would start beyond the end.

  3. Compute the room left, S−oS - o. Because o≤So \le S, the result lies between 0 and SS and cannot wrap.

  4. Test n≤S−on \le S - o. If the test fails, reject.

  5. Only now form the address of byte oo and access the nn 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 o≤So \le S; 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:

Worked example

Take 32-bit unsigned arithmetic and a buffer of S=4,096S = 4{,}096 bytes. An attacker sends o=100o = 100 and n=4,294,967,280n = 4{,}294{,}967{,}280.

The naive check computes o+no + n. The true sum is 4,294,967,3804{,}294{,}967{,}380, which exceeds the largest 32-bit value, so it wraps to 4,294,967,380−4,294,967,296=844{,}294{,}967{,}380 - 4{,}294{,}967{,}296 = 84. Since 84≤4,09684 \le 4{,}096, the naive check accepts, and the program reads 4,294,967,280 bytes starting at offset 100.

The safe check first tests 100≤4,096100 \le 4{,}096, which passes. The room left is 4,096−100=3,9964{,}096 - 100 = 3{,}996. The test 4,294,967,280≤3,9964{,}294{,}967{,}280 \le 3{,}996 fails, so the request is rejected.

The first test matters on its own. Suppose a program omits it and checks only n≤S−on \le S - o. An attacker sends o=5,000o = 5{,}000 and n=10n = 10. The true room is 4,096−5,000=−9044{,}096 - 5{,}000 = -904, which wraps to −904+4,294,967,296=4,294,966,392-904 + 4{,}294{,}967{,}296 = 4{,}294{,}966{,}392. The test 10≤4,294,966,39210 \le 4{,}294{,}966{,}392 passes, and the program reads 10 bytes that start 904 bytes past the end. With the first test, 5,000≤4,0965{,}000 \le 4{,}096 fails and the request is rejected.

At the boundary, o=4,000o = 4{,}000 and n=96n = 96 pass, since 4,096−4,000=964{,}096 - 4{,}000 = 96 and 96≤9696 \le 96. 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 o=96o = 96 and records of k=16k = 16 bytes. At most ⌊(4,096−96)/16⌋=⌊4,000/16⌋=250\lfloor (4{,}096 - 96) / 16 \rfloor = \lfloor 4{,}000 / 16 \rfloor = 250 records fit. A count c=268,435,457c = 268{,}435{,}457, which is 228+12^{28} + 1, fails the test c≤250c \le 250. A naive test that computes c×16c \times 16 in 32 bits gets 4,294,967,312−4,294,967,296=164{,}294{,}967{,}312 - 4{,}294{,}967{,}296 = 16 and accepts the count.

Stated precisely

For nonnegative integers oo, nn and SS, o+n≤S⇔(o≤Sandn≤S−o).o + n \le S \iff \bigl(o \le S \ \text{and}\ n \le S - o\bigr). Proof: if o+n≤So + n \le S, then o≤o+n≤So \le o + n \le S because n≥0n \ge 0, and subtracting oo gives n≤S−on \le S - o. Conversely, adding oo to both sides of n≤S−on \le S - o gives o+n≤So + n \le S. For elements of k>0k > 0 bytes and nonnegative integers cc and RR, c⋅k≤R⇔c≤⌊R/k⌋c \cdot k \le R \iff c \le \lfloor R / k \rfloor, because c⋅k≤Rc \cdot k \le R means c≤R/kc \le R / k, and a whole number cc is at most R/kR / k exactly when it is at most ⌊R/k⌋\lfloor R / k \rfloor. The check uses R=S−oR = S - o.

In an unsigned type whose largest value is 2N−12^N - 1: when o≤So \le S, the computed S−oS - o equals the true difference, because 0≤S−o≤S≤2N−10 \le S - o \le S \le 2^N - 1. The comparisons involve no further arithmetic. Therefore the C condition o <= S && n <= S - o is true exactly when the true sum satisfies o+n≤So + n \le S, for every value of the type.

The guarantee rests on three assumptions.

  1. oo, nn and SS are nonnegative and held in one unsigned type, or converted to it without a change of value.

  2. SS is the true size of the memory that the access will use, not a size claimed by the input.

  3. The values checked are the values used: none of them changes between the check and the access.

With unsigned types, evaluating S−oS - o too early would do no harm, because a wrapped S−oS - o only occurs together with a failed o≤So \le S. With signed types it would: S−oS - o for a negative oo near the minimum overflows, which is undefined behavior, so the subtraction must run only after 0≤o≤S0 \le o \le S 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 SS describes the memory actually accessed, or that the requester may read those bytes.

The values must not change after the check. If oo or nn 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, o=So = S with n=0n = 0, passes, which is correct because it touches no byte. Code must not then read the byte at offset oo. 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 o=So = S, n=S−on = S - o, n=S−o+1n = S - o + 1, and the largest value of the type for oo and for nn.

Questions answered

Which precondition makes subtraction in the check safe?

The precondition is o≤So \le S: the offset must be no larger than the buffer size, and the program must establish it before computing the room left, S−oS - o. Here SS is the buffer size in bytes, oo the offset where the requested span starts, and nn the span’s length. With 0≤o≤S0 \le o \le S, the true difference S−oS - o lies between 0 and SS, so an unsigned subtraction cannot wrap and a signed one cannot overflow. The check then accepts only if n≤S−on \le S - o, which is equivalent to o+n≤So + n \le S but never forms the sum o+no + n, 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, S=4,096S = 4{,}096 and o=5,000o = 5{,}000 give S−o=4,294,966,392S - o = 4{,}294{,}966{,}392, 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].