From e4d3f0b47c062e9dc49840f3bfc684c1e2dd5c97 Mon Sep 17 00:00:00 2001 From: Nick Clifton Date: Tue, 6 Feb 2018 15:48:29 +0000 Subject: [PATCH] Prevent attempts to call strncpy with a zero-length field by chacking the size of debuglink sections. PR 22794 * opncls.c (bfd_get_debug_link_info_1): Check the size of the section before attempting to read it in. (bfd_get_alt_debug_link_info): Likewise. Gbp-Pq: Name pr22794.diff --- bfd/opncls.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/bfd/opncls.c b/bfd/opncls.c index 709689057..84cc1460e 100644 --- a/bfd/opncls.c +++ b/bfd/opncls.c @@ -1186,6 +1186,7 @@ bfd_get_debug_link_info_1 (bfd *abfd, void *crc32_out) bfd_byte *contents; unsigned int crc_offset; char *name; + bfd_size_type size; BFD_ASSERT (abfd); BFD_ASSERT (crc32_out); @@ -1195,6 +1196,12 @@ bfd_get_debug_link_info_1 (bfd *abfd, void *crc32_out) if (sect == NULL) return NULL; + size = bfd_get_section_size (sect); + + /* PR 22794: Make sure that the section has a reasonable size. */ + if (size < 8 || size >= bfd_get_size (abfd)) + return NULL; + if (!bfd_malloc_and_get_section (abfd, sect, &contents)) { if (contents != NULL) @@ -1204,10 +1211,10 @@ bfd_get_debug_link_info_1 (bfd *abfd, void *crc32_out) /* CRC value is stored after the filename, aligned up to 4 bytes. */ name = (char *) contents; - /* PR 17597: avoid reading off the end of the buffer. */ - crc_offset = strnlen (name, bfd_get_section_size (sect)) + 1; + /* PR 17597: Avoid reading off the end of the buffer. */ + crc_offset = strnlen (name, size) + 1; crc_offset = (crc_offset + 3) & ~3; - if (crc_offset + 4 > bfd_get_section_size (sect)) + if (crc_offset + 4 > size) return NULL; *crc32 = bfd_get_32 (abfd, contents + crc_offset); @@ -1268,6 +1275,7 @@ bfd_get_alt_debug_link_info (bfd * abfd, bfd_size_type *buildid_len, bfd_byte *contents; unsigned int buildid_offset; char *name; + bfd_size_type size; BFD_ASSERT (abfd); BFD_ASSERT (buildid_len); @@ -1278,6 +1286,10 @@ bfd_get_alt_debug_link_info (bfd * abfd, bfd_size_type *buildid_len, if (sect == NULL) return NULL; + size = bfd_get_section_size (sect); + if (size < 8 || size >= bfd_get_size (abfd)) + return NULL; + if (!bfd_malloc_and_get_section (abfd, sect, & contents)) { if (contents != NULL) @@ -1287,11 +1299,11 @@ bfd_get_alt_debug_link_info (bfd * abfd, bfd_size_type *buildid_len, /* BuildID value is stored after the filename. */ name = (char *) contents; - buildid_offset = strnlen (name, bfd_get_section_size (sect)) + 1; + buildid_offset = strnlen (name, size) + 1; if (buildid_offset >= bfd_get_section_size (sect)) return NULL; - *buildid_len = bfd_get_section_size (sect) - buildid_offset; + *buildid_len = size - buildid_offset; *buildid_out = bfd_malloc (*buildid_len); memcpy (*buildid_out, contents + buildid_offset, *buildid_len); -- 2.30.2