elf: Don't read beyond .eh_frame section size

Message ID CAMe9rOrMMCAFL9SHWjCxs03TJNrh2CYpFEMp7gqy-qZYw1oHVA@mail.gmail.com
State New
Headers
Series elf: Don't read beyond .eh_frame section size |

Commit Message

H.J. Lu Sept. 22, 2025, 7:24 a.m. UTC
  PR ld/33464
* elf-eh-frame.c (_bfd_elf_parse_eh_frame): Don't read beyond
.eh_frame section size.
  

Comments

Alan Modra Sept. 22, 2025, 12:22 p.m. UTC | #1
On Mon, Sep 22, 2025 at 03:24:26PM +0800, H.J. Lu wrote:
> PR ld/33464
> * elf-eh-frame.c (_bfd_elf_parse_eh_frame): Don't read beyond
> .eh_frame section size.

OK.
  

Patch

From bf164f839365ccf894dfdb0a5dd365bbcb9de730 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Mon, 22 Sep 2025 15:20:34 +0800
Subject: [PATCH] elf: Don't read beyond .eh_frame section size

	PR ld/33464
	* elf-eh-frame.c (_bfd_elf_parse_eh_frame): Don't read beyond
	.eh_frame section size.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 bfd/elf-eh-frame.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/bfd/elf-eh-frame.c b/bfd/elf-eh-frame.c
index dc0d2e097f5..30bb313489c 100644
--- a/bfd/elf-eh-frame.c
+++ b/bfd/elf-eh-frame.c
@@ -737,6 +737,7 @@  _bfd_elf_parse_eh_frame (bfd *abfd, struct bfd_link_info *info,
       if (hdr_id == 0)
 	{
 	  unsigned int initial_insn_length;
+	  char *null_byte;
 
 	  /* CIE  */
 	  this_inf->cie = 1;
@@ -753,10 +754,13 @@  _bfd_elf_parse_eh_frame (bfd *abfd, struct bfd_link_info *info,
 	  REQUIRE (cie->version == 1
 		   || cie->version == 3
 		   || cie->version == 4);
-	  REQUIRE (strlen ((char *) buf) < sizeof (cie->augmentation));
+	  null_byte = memchr ((char *) buf, 0, end - buf);
+	  REQUIRE (null_byte != NULL);
+	  REQUIRE ((size_t) (null_byte - (char *) buf)
+		   < sizeof (cie->augmentation));
 
 	  strcpy (cie->augmentation, (char *) buf);
-	  buf = (bfd_byte *) strchr ((char *) buf, '\0') + 1;
+	  buf = (bfd_byte *) null_byte + 1;
 	  this_inf->u.cie.aug_str_len = buf - start - 1;
 	  ENSURE_NO_RELOCS (buf);
 	  if (buf[0] == 'e' && buf[1] == 'h')
-- 
2.51.0