binutils: fix integer overflow in stabs base class count and objcopy padding
Fixes integer overflows in stabs processing and objcopy padding calculations that could lead to incorrect sizes or crashes.
This patch addresses two integer overflow vulnerabilities. First, it corrects an issue in stabs.c where an excessively large base class count could cause memory allocation to wrap around to zero, leading to a heap buffer overflow. The fix uses size_t for counts and includes overflow checks. Second, it prevents integer overflows in objcopy’s --gap-fill and --pad-to operations by using compiler builtins for overflow detection, thus avoiding incorrect section sizes or crashes.
- proposer
Fixes integer overflows in stabs base class counting and objcopy padding calculations.
“parse_stab_baseclasses reads the number of base classes with parse_number and stores it in an unsigned int. For a count of 0xffffffff, "(c + 1) * sizeof (*classes)" wraps to zero, debug_xalloc returns an empty block and the loop that follows writes c entries into it.”
In Details
This PR addresses two distinct integer overflow issues. In stabs.c, parse_stab_baseclasses now uses size_t for the base class count and validates the resulting allocation size to prevent heap overflows when parsing malformed debug information. In objcopy.c, arithmetic operations for --gap-fill and --pad-to now use GCC's __builtin_mul_overflow and __builtin_add_overflow to detect and report overflows, preventing incorrect section sizing due to wrap-around.
- stabs
- A debugging information format used by some Unix compilers, including older versions of GCC.
- objcopy
- A utility that copies and translates object files; used here for manipulating section padding and fill bytes.
- integer overflow
- A condition that occurs when an arithmetic operation produces a result that exceeds the maximum value that can be stored in a data type.
- heap buffer overflow
- A type of security vulnerability where a program writes data beyond the allocated buffer in the heap, potentially corrupting other data or executing malicious code.
- size_t
- An unsigned integer type that is large enough to contain the size of any object in bytes.