aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Modra <amodra@gmail.com>2022-11-10 11:48:01 +1030
committerAlan Modra <amodra@gmail.com>2022-11-10 20:29:03 +1030
commita3eb71adfea201f9e14980d8a96ea2e27ef52ad1 (patch)
tree2bba83828745cc3cf61d2f23ffd81fde8b7aedd0 /bfd/aoutx.h
parentgdb/testsuite: Fix rtld-step-nodebugsym.exp (diff)
downloadbinutils-gdb-a3eb71adfea201f9e14980d8a96ea2e27ef52ad1.tar.gz
binutils-gdb-a3eb71adfea201f9e14980d8a96ea2e27ef52ad1.tar.bz2
binutils-gdb-a3eb71adfea201f9e14980d8a96ea2e27ef52ad1.zip
Sanity check reloc count in get_reloc_upper_bound
The idea here is the stop tools from allocating up to 32G per section for the arelent pointer array, only to find a little later that the section reloc count was fuzzed. This usually doesn't hurt much (on systems that allow malloc overcommit) except when compiled with asan. We already do this for ELF targets, and while fixing the logic recently I decided other targets ought to do the same. * elf64-sparc.c (elf64_sparc_get_reloc_upper_bound): Sanity check section reloc count against file size. * mach-o.c (bfd_mach_o_get_reloc_upper_bound): Likewise. * aoutx.h (get_reloc_upper_bound): Likewise, and don't duplicate check done in bfd_get_reloc_upper_bound. * pdp11.c (get_reloc_upper_bound): Likewise. * coffgen.c (coff_get_reloc_upper_bound): Likewise.
Diffstat (limited to 'bfd/aoutx.h')
-rw-r--r--bfd/aoutx.h20
1 files changed, 12 insertions, 8 deletions
diff --git a/bfd/aoutx.h b/bfd/aoutx.h
index 4aed23426ca..61ea9f7ce04 100644
--- a/bfd/aoutx.h
+++ b/bfd/aoutx.h
@@ -2485,13 +2485,7 @@ NAME (aout, canonicalize_reloc) (bfd *abfd,
long
NAME (aout, get_reloc_upper_bound) (bfd *abfd, sec_ptr asect)
{
- bfd_size_type count;
-
- if (bfd_get_format (abfd) != bfd_object)
- {
- bfd_set_error (bfd_error_invalid_operation);
- return -1;
- }
+ size_t count, raw;
if (asect->flags & SEC_CONSTRUCTOR)
count = asect->reloc_count;
@@ -2507,11 +2501,21 @@ NAME (aout, get_reloc_upper_bound) (bfd *abfd, sec_ptr asect)
return -1;
}
- if (count >= LONG_MAX / sizeof (arelent *))
+ if (count >= LONG_MAX / sizeof (arelent *)
+ || _bfd_mul_overflow (count, obj_reloc_entry_size (abfd), &raw))
{
bfd_set_error (bfd_error_file_too_big);
return -1;
}
+ if (!bfd_write_p (abfd))
+ {
+ ufile_ptr filesize = bfd_get_file_size (abfd);
+ if (filesize != 0 && raw > filesize)
+ {
+ bfd_set_error (bfd_error_file_truncated);
+ return -1;
+ }
+ }
return (count + 1) * sizeof (arelent *);
}