Section 4.5

Out-of-bounds read

Definition

An out-of-bounds read is a read of memory outside the buffer that the program meant to read, usually past its end. A buffer is a block of memory of fixed size that holds data. The read changes nothing; it returns bytes that belong to something else. The program then treats those bytes as its own data: it sends them out, which can reveal secrets, or it bases a decision on them. A buffer overflow §4.4 is the matching error for writes.

The idea

C does not check where a read lands. A read has a starting position and a width, the number of bytes it takes. Checking that the starting position is inside the buffer is not enough: a 4-byte read that starts at the last byte takes 3 more bytes from whatever lies after the buffer.

A well-known case is the Heartbleed bug, found in 2014 in OpenSSL, a widely used encryption library [339]. A network message called a heartbeat request carried some data and a 16-bit field that stated the length of that data, and the receiver had to send the same data back. The faulty code did not compare the length field with the amount of data that had actually arrived. An attacker sent a few bytes of data with a length field of up to 65,535, and the receiver sent back that many bytes of its own memory, beginning at the attacker’s few bytes. Demonstrations showed that those bytes could include private keys, the secrets that protect encrypted connections.

A photocopier gives a fair picture. A clerk is asked to copy pages 15 to 18 of a report that has 16 pages and lies on a pile of other papers. The clerk copies pages 15 and 16 and then the top two sheets of the pile underneath. Nothing was damaged, but two pages of someone else’s papers have left the room. The picture stops being accurate in one respect: a clerk would notice that the report had ended. A processor does not.

How it works

Number the bytes of a buffer from 0. A buffer with a length of LL bytes has positions 0 to L−1L - 1.

  1. The program reads ww bytes starting at position pp. The read covers positions pp to p+w−1p + w - 1.

  2. If p+wp + w is greater than LL, the read takes bytes from positions LL and above, which are not part of the buffer.

  3. The processor returns whatever is stored there. If that memory can be read, no error is reported. If it cannot, because no memory exists at that address or a hardware protection feature blocks the access, the processor raises a fault, which normally stops the program.

  4. The program uses the bytes. It may copy them into a reply, which discloses them, or base a decision on them. It may use them as a length or a position for its next access, which turns one bad read into a larger one.

The width and the start can each go wrong in ways that are easy to miss.

Worked example

A buffer has a length of 16 bytes, positions 0 to 15.

A check of the starting position alone, p<16p < 16, accepts positions 13, 14 and 15 for a 4-byte read. Those reads take 1, 2 and 3 bytes from outside the buffer. The correct check for position 14 and width 4 asks how many bytes remain: 16−14=216 - 14 = 2. The read needs 4 bytes, and 4≤24 \le 2 is false, so the read is refused.

Stated precisely

Let LL be the length of the buffer in bytes, pp the starting position and ww the width of the read in bytes. All three are whole numbers that are not negative. The read stays inside the buffer exactly when p≤Landw≤L−p.p \le L \quad\text{and}\quad w \le L - p. The subtraction L−pL - p is made only after p≤Lp \le L has been shown, so its result cannot be negative. For ordinary whole numbers the shorter form p+w≤Lp + w \le L states the same condition, but in machine arithmetic the addition can wrap around. With unsigned 32-bit integers, p=4,294,967,292p = 4{,}294{,}967{,}292 and w=8w = 8 give a stored sum of 4, because only the remainder after division by 232=4,294,967,2962^{32} = 4{,}294{,}967{,}296 is kept. For a buffer of 16 bytes the check 4≤164 \le 16 then wrongly passes.

The C standard allows a pointer to point one position past the last element of an array §14.5, but it forbids reading or writing through that pointer. An access beyond the end of an object is undefined behavior §4.12, which means that the standard places no requirement on what the program does. A read past the end of a buffer is also called a buffer over-read. For the string-handling functions of the standard library that take a length, such as the copying function memcpy, the edition of the standard published in 2011 allows a length of 0 but still requires the pointers to be valid.

Limits and common mistakes

No crash does not mean no harm. The memory after a buffer can usually be read, so tests pass. The read still breaks confidentiality §1.8, the property that information reaches only those allowed to see it.

The bound must be the bound of the object. A message often sits inside a larger receive buffer that still holds bytes of earlier messages. A read that stays inside the receive buffer but leaves the current message can return the data of another sender.

Hardware memory protection works on regions, not on objects. A memory protection unit §6.8, a hardware feature that blocks access to chosen address ranges, cannot see the border between two objects inside one permitted range.

In bounds does not mean meaningful. Bytes inside the buffer may never have been written, or may belong to an object that has already been freed.

Test tools see only what the tests run. AddressSanitizer §10.22, a tool that adds checks to a compiled program, reports an out-of-bounds read only on a path that a test takes.

Questions answered

Can a read cross the end even when its starting pointer is in range?

Yes. A read takes a number of bytes, its width, beginning at the starting pointer, and every one of those bytes must lie inside the buffer. A starting pointer that is in range guarantees only the first byte. A 4-byte read that starts at the last byte of a 16-byte buffer takes 1 byte from the buffer and 3 bytes from whatever is stored after it.

The complete check uses the starting position pp, the width ww and the length LL of the buffer, all in bytes: p≤Lp \le L and w≤L−pw \le L - p. The width must be the true width of the access: 4 bytes for a 32-bit number, the full claimed length for a copy, and everything up to the terminating zero byte for a C string function. A read that crosses the end is called an out-of-bounds read. It usually causes no crash, and it returns bytes of other data, which the program may then disclose.

Related entries: Overflow-safe range check (§4.17), Object lifetime (§4.1), Strict aliasing and effective type (§4.23), Buffer overflow (§4.4), Spatial memory safety (§2.28), Confidentiality (§1.8). Further reading: [25, 78, 339].