public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Alan Modra <amodra@gmail.com>
To: binutils@sourceware.org
Subject: asan: segfault in coff_mangle_symbols
Date: Mon, 24 Apr 2023 21:02:50 +0930	[thread overview]
Message-ID: <ZEZo4oH/oT6Gu/5S@squeak.grove.modra.org> (raw)

The testcase managed to trigger creation of a wild pointer in
coff_slurp_symbol_table.  Stop that happening, and fix an unrelated
problem I happened to see in bfd_coff_get_syment.

	* coff-bfd.c (bfd_coff_get_syment): Clear fix_value after
	converting n_value from a pointer to an index.
	* coffcode.h (coff_slurp_symbol_table <C_BSTAT>): Sanity check
	symbol value before converting to a pointer.

diff --git a/bfd/coff-bfd.c b/bfd/coff-bfd.c
index 292778ecb56..9aceeb080a7 100644
--- a/bfd/coff-bfd.c
+++ b/bfd/coff-bfd.c
@@ -45,9 +45,12 @@ bfd_coff_get_syment (bfd *abfd,
   *psyment = csym->native->u.syment;
 
   if (csym->native->fix_value)
-    psyment->n_value =
-      ((psyment->n_value - (uintptr_t) obj_raw_syments (abfd))
-       / sizeof (combined_entry_type));
+    {
+      psyment->n_value =
+	((psyment->n_value - (uintptr_t) obj_raw_syments (abfd))
+	 / sizeof (combined_entry_type));
+      csym->native->fix_value = 0;
+    }
 
   /* FIXME: We should handle fix_line here.  */
 
diff --git a/bfd/coffcode.h b/bfd/coffcode.h
index 423f6c070ef..594f3e0457b 100644
--- a/bfd/coffcode.h
+++ b/bfd/coffcode.h
@@ -4852,13 +4852,18 @@ coff_slurp_symbol_table (bfd * abfd)
 	    case C_BSTAT:
 	      dst->symbol.flags = BSF_DEBUGGING;
 
-	      /* The value is actually a symbol index.  Save a pointer
-		 to the symbol instead of the index.  FIXME: This
-		 should use a union.  */
-	      src->u.syment.n_value
-		= (uintptr_t) (native_symbols + src->u.syment.n_value);
-	      dst->symbol.value = src->u.syment.n_value;
-	      src->fix_value = 1;
+	      if (src->u.syment.n_value >= obj_raw_syment_count (abfd))
+		dst->symbol.value = 0;
+	      else
+		{
+		  /* The value is actually a symbol index.  Save a pointer
+		     to the symbol instead of the index.  FIXME: This
+		     should use a union.  */
+		  src->u.syment.n_value
+		    = (uintptr_t) (native_symbols + src->u.syment.n_value);
+		  dst->symbol.value = src->u.syment.n_value;
+		  src->fix_value = 1;
+		}
 	      break;
 #endif
 

-- 
Alan Modra
Australia Development Lab, IBM

                 reply	other threads:[~2023-04-24 11:32 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZEZo4oH/oT6Gu/5S@squeak.grove.modra.org \
    --to=amodra@gmail.com \
    --cc=binutils@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).