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 bytes has positions 0 to .
The program reads bytes starting at position . The read covers positions to .
If is greater than , the read takes bytes from positions and above, which are not part of the buffer.
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.
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.
The width comes from the type. Reading a 32-bit number reads 4 bytes, wherever the read starts.
The width comes from the input. A length field inside a message says how many bytes follow. The sender chose that field, and it can claim more bytes than were received.
The width is found by searching. C string functions such as
strlenread on until they meet a zero byte. If the buffer holds no zero byte, they continue into the memory after it.The start is computed. A position computed from input can be negative, which leads to a read before the start of the buffer. A very large position can make the sum of position and width wrap around to a small number, which passes a check that it should fail.
Worked example
A buffer has a length of 16 bytes, positions 0 to 15.
Position 15, width 1: touches position 15. , which is not greater than 16. Inside.
Position 15, width 2: touches positions 15 and 16. , which is greater than 16. One byte outside.
Position 14, width 4: touches positions 14, 15, 16 and 17. , which is greater than 16. Two bytes outside.
Position 12, width 4: touches positions 12 to 15. . Inside. Position 12 is the last valid start for a 4-byte read, because .
Position 16, width 0: touches no byte. . Nothing is read.
A check of the starting position alone, , 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: . The read needs 4 bytes, and is false, so the read is refused.
Stated precisely
Let be the length of the buffer in bytes, the starting position and the width of the read in bytes. All three are whole numbers that are not negative. The read stays inside the buffer exactly when The subtraction is made only after has been shown, so its result cannot be negative. For ordinary whole numbers the shorter form states the same condition, but in machine arithmetic the addition can wrap around. With unsigned 32-bit integers, and give a stored sum of 4, because only the remainder after division by is kept. For a buffer of 16 bytes the check 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 , the width and the length of the buffer, all in bytes: and . 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].