Fix integer overflow in COFF section alignment calculation
Prevents UB by using unsigned integers for COFF section alignment calculations.
An undefined behavior (UB) in coff_compute_section_file_positions has been fixed. When processing COFF files with fuzzed section alignments, large alignment values could cause a signed integer overflow. This change replaces the signed file_ptr with an unsigned ufile_ptr for these calculations, correctly handling large alignments and preventing the overflow.
In Details
The coff_compute_section_file_positions function in BFD is vulnerable to signed integer overflow in file_ptr when calculating section positions if objcopy is invoked with a COFF input having a fuzzed, excessively large section alignment value. This commit rectifies the issue by switching the sofar and old_sofar variables from file_ptr to ufile_ptr to accommodate larger, unsigned alignment values.
- UB
- Undefined Behavior. This means the C or C++ standard does not specify what should happen. Compilers may emit code that assumes UB never occurs, leading to unexpected program behavior.
- COFF
- Common Object File Format. A standard file format for object files, executables, and libraries used by many systems, particularly older Unix variants and Windows.
- objcopy
- A standard binutils utility used to copy and translate object files from one format to another, or to modify sections within an object file.
- bfd
- Binary File Descriptor library. A low-level library used by binutils and GDB to abstract and handle various object file formats.
- file_ptr
- A data type within BFD representing a file offset or size. It was previously a signed type, leading to potential overflows.
- ufile_ptr
- An unsigned version of
file_ptr, capable of representing larger file offsets and sizes, used here to prevent signed overflow issues.