Libctf: Bounds-check type records at endian flip and type counting
Fixes heap buffer overflows and data corruption in libctf when processing malformed or foreign-endian CTF type data.
This patch addresses security vulnerabilities in libctf related to processing Compact Type Format (CTF) data. Specifically, when a foreign-endian CTF dictionary contains type records with malformed variable-length data that extends beyond the allocated section, flip_types() can perform heap out-of-bounds read-modify-write operations. Additionally, truncated record tails can cause out-of-bounds reads during type flipping and counting. The patch adds bounds checks to get_ctt_size_*(), init_static_types_internal(), and flip_types() to ensure records are only processed if they fit within the section, preventing these overflows. A regression test is included.
- proposer
Submits a patch to add bounds checks to libctf's type record processing functions, preventing heap overflows and data corruption when handling malformed or foreign-endian CTF data.
“opening a foreign-endian CTF dict whose type section contains a record whose variable-length data runs past the end of the section (attacker controls kind and vlen) makes flip_types() swap up to vlen * elem bytes past the malloc'd buffer (heap OOB read-modify-write), before init_static_types() ever runs.”
In Details
This patch addresses heap buffer overflows in libctf's handling of CTF data, particularly when dealing with foreign-endian dictionaries or malformed type records. The vulnerability lies in flip_types() and related functions trusting record metadata (like kind and vlen) without verifying that the entire record, including its variable-length data, fits within the allocated type section buffer. This can lead to OOB writes when swapping bytes across endianness. The fixes ensure that get_ctt_size_*() correctly identifies large-form records before accessing lsize fields, and that `init_…
- libctf
- A library for reading and writing Compact Type Format (CTF) data, used for debugging information.
- CTF
- Compact Type Format, a debugging data format designed to be more compact than DWARF.
- heap buffer overflow
- A vulnerability where a program writes data beyond the boundaries of an allocated buffer on the heap, potentially corrupting adjacent memory or executing malicious code.
- endianness
- The order in which bytes of a multi-byte data word are stored in computer memory. Little-endian stores the least significant byte first, while big-endian stores the most significant byte first.
- ASan
- AddressSanitizer, a fast memory error detector for C/C++. It finds issues like buffer overflows and use-after-free.
- OOB
- Out-of-Bounds. Refers to memory accesses that read or write outside the allocated bounds of a buffer.