[PATCH] binutils: fix integer overflow in stabs base class count and objcopy padding
mustafa
mustafa@medios.digiscrypt.com
Thu Nov 19 10:36:48 GMT 2026
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. Use size_t, require that the string is long enough to hold
that many classes (each one takes at least four characters), and
compute the allocation size with an overflow check. A bad count is
now reported as a bad stab.
objcopy --gap-fill and --pad-to compute section end addresses from
the input LMA and size, multiplied by the octets per byte, and add
the result to other sizes without checking. With large values the
sums wrap, and the output section ends up with a wrong or huge size.
Use the overflow builtins for each step and fail with an error
message instead.
* stabs.c (parse_stab_baseclasses): Use size_t for the class
count. Bound it by the remaining input and check the
allocation size for overflow.
* objcopy.c (copy_object): Check the arithmetic for
--gap-fill and --pad-to for overflow.
Signed-off-by: Mohammed Mustafa <mustafa@medios.digiscrypt.com>
---
binutils/objcopy.c | 34 +++++++++++++++++++++++++++-------
binutils/stabs.c | 16 +++++++++++++---
2 files changed, 40 insertions(+), 10 deletions(-)
diff --git a/binutils/objcopy.c b/binutils/objcopy.c
index 609bb311a9a..d4013d69fe2 100644
--- a/binutils/objcopy.c
+++ b/binutils/objcopy.c
@@ -3310,12 +3310,23 @@ copy_object (bfd *ibfd, bfd *obfd, const
bfd_arch_info_type *input_arch)
continue;
size = bfd_section_size (osections[i]);
- gap_start = bfd_section_lma (osections[i]) * opb1 + size;
- gap_stop = bfd_section_lma (osections[i + 1]) * opb2;
+ if (__builtin_mul_overflow (bfd_section_lma (osections[i]),
+ opb1, &gap_start)
+ || __builtin_add_overflow (gap_start, size, &gap_start)
+ || __builtin_mul_overflow (bfd_section_lma (osections[i + 1]),
+ opb2, &gap_stop))
+ {
+ bfd_nonfatal_message (NULL, obfd, osections[i],
+ _("Can't fill gap after section"));
+ goto fail;
+ }
if (gap_start < gap_stop)
{
- if (!bfd_set_section_size (osections[i],
- size + (gap_stop - gap_start)))
+ bfd_size_type new_size;
+
+ if (__builtin_add_overflow (size, gap_stop - gap_start,
+ &new_size)
+ || !bfd_set_section_size (osections[i], new_size))
{
bfd_nonfatal_message (NULL, obfd, osections[i],
_("Can't fill gap after section"));
@@ -3333,11 +3344,20 @@ copy_object (bfd *ibfd, bfd *obfd, const
bfd_arch_info_type *input_arch)
bfd_vma lma; /* Octets. */
bfd_size_type size; /* Octets. */
unsigned int opb = bfd_octets_per_byte (obfd, osections[num_sec - 1]);
- bfd_vma _pad_to = pad_to * opb;
+ bfd_vma _pad_to;
+ bfd_vma lma_end;
- lma = bfd_section_lma (osections[num_sec - 1]) * opb;
size = bfd_section_size (osections[num_sec - 1]);
- if (lma + size < _pad_to)
+ if (__builtin_mul_overflow (pad_to, opb, &_pad_to)
+ || __builtin_mul_overflow (bfd_section_lma (osections[num_sec - 1]),
+ opb, &lma)
+ || __builtin_add_overflow (lma, size, &lma_end))
+ {
+ bfd_nonfatal_message (NULL, obfd, osections[num_sec - 1],
+ _("can't add padding"));
+ goto fail;
+ }
+ if (lma_end < _pad_to)
{
if (!bfd_set_section_size (osections[num_sec - 1], _pad_to - lma))
{
diff --git a/binutils/stabs.c b/binutils/stabs.c
index 27d49376b97..251dbb4cbd6 100644
--- a/binutils/stabs.c
+++ b/binutils/stabs.c
@@ -2196,7 +2196,8 @@ parse_stab_baseclasses (void * dhandle,
const char * p_end)
{
const char *orig;
- unsigned int c, i;
+ size_t c, i;
+ size_t amt;
debug_baseclass *classes;
*retp = NULL;
@@ -2212,7 +2213,7 @@ parse_stab_baseclasses (void * dhandle,
}
++*pp;
- c = (unsigned int) parse_number (pp, (bool *) NULL, p_end);
+ c = parse_number (pp, (bool *) NULL, p_end);
if (**pp != ',')
{
@@ -2221,7 +2222,16 @@ parse_stab_baseclasses (void * dhandle,
}
++*pp;
- classes = debug_xalloc (dhandle, (c + 1) * sizeof (*classes));
+ /* Each base class needs at least four characters of the string. */
+ if (*pp > p_end
+ || c > (size_t) (p_end - *pp) / 4
+ || __builtin_mul_overflow (c + 1, sizeof (*classes), &amt))
+ {
+ bad_stab (orig);
+ return false;
+ }
+
+ classes = debug_xalloc (dhandle, amt);
for (i = 0; i < c; i++)
{
--
2.53.0
More information about the Binutils
mailing list