From sourceware-bugzilla@sourceware.org  Fri Oct  1 16:33:30 2021
From: sourceware-bugzilla@sourceware.org (alexey.brodkin at gmail dot com)
Date: Fri, 01 Oct 2021 16:33:30 +0000
Subject: [Bug debugedit/28408] New: debugedit segfaults while changing
 binutils' build-id
Message-ID: <bug-28408-13298@http.sourceware.org/bugzilla/>

https://sourceware.org/bugzilla/show_bug.cgi?id=28408

            Bug ID: 28408
           Summary: debugedit segfaults while changing binutils' build-id
           Product: debugedit
           Version: unspecified
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: debugedit
          Assignee: unassigned at sourceware dot org
          Reporter: alexey.brodkin at gmail dot com
                CC: debugedit at sourceware dot org
  Target Milestone: ---

Created attachment 13694
  --> https://sourceware.org/bugzilla/attachment.cgi?id=13694&action=edit
Cross-compiled m68k library

Initially seen on cross-compilation of Binutils in Debian and then 
reported here https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=995195.

In a nutshell "debugedit" segfaults on attempt to run the following command on
x86_64 host against a binary built for a big-endian architecture in this
example it's "libbfd-2.37-system.so" (attached) cross-compiled for m68k:
------------------------->8-----------------------
$ debugedit --build-id --build-id-seed=x libbfd-2.37-system.so
Segmentation fault
------------------------->8-----------------------

That's what we with GDB:
------------------------->8-----------------------
$ gdb --args ./debugedit --build-id --build-id-seed=x libbfd-2.37-system.so
(gdb) r
Starting program: .../debugedit/debugedit --build-id --build-id-seed=x
libbfd-2.37-system.so
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
__memmove_avx_unaligned_erms () at
../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:365
365     ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S: No such file
or directory.
(gdb) bt
#0  __memmove_avx_unaligned_erms () at
../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:365
#1  0x000055555555fea8 in memcpy (__len=83, __src=0x0, __dest=0x7fffffffe02d)
at /usr/include/x86_64-linux-gnu/bits/string_fortified.h:34
#2  sha1_process_bytes (buffer=0x0, len=452, ctx=0x7fffffffdfe0) at
tools/sha1.c:210
#3  0x0000555555557d43 in handle_build_id (build_id_size=<optimized out>,
build_id_offset=16, build_id=0x555555566448, dso=0x5555555678d0) at
tools/debugedit.c:3285
#4  main (argc=<optimized out>, argv=<optimized out>) at tools/debugedit.c:3638
------------------------->8-----------------------

If we look a bit deeper we may notice that while processing ".bss" section
we're   not seeing "u.shdr.sh_type == SHT_NOBITS" here
https://sourceware.org/git/?p=debugedit.git;a=blob;f=tools/debugedit.c;h=668777ad47d9b51d0cd118b7d91281963dafa6be;hb=HEAD#l3276.
Instead we see "u.shdr.sh_type = 0x08000000", which is exactly byte-swapped 0x8
which stands for SHT_NOBITS.

And since that check doesn't catch NOBITS section we later segfault trying to
process some other garbage.

That said there's something wrong with interpretation of a foreign endianess.


And this could be reproduced with both debugedit & elfutils built from today's
"master" barnches.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

From sourceware-bugzilla@sourceware.org  Tue Oct  5 11:04:58 2021
From: sourceware-bugzilla@sourceware.org (mark at klomp dot org)
Date: Tue, 05 Oct 2021 11:04:58 +0000
Subject: [Bug debugedit/28408] debugedit segfaults while changing binutils'
 build-id
In-Reply-To: <bug-28408-13298@http.sourceware.org/bugzilla/>
References: <bug-28408-13298@http.sourceware.org/bugzilla/>
Message-ID: <bug-28408-13298-r11hXTFk2n@http.sourceware.org/bugzilla/>

https://sourceware.org/bugzilla/show_bug.cgi?id=28408

Mark Wielaard <mark at klomp dot org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2021-10-05
             Status|UNCONFIRMED                 |ASSIGNED
                 CC|                            |mark at klomp dot org
     Ever confirmed|0                           |1

--- Comment #1 from Mark Wielaard <mark at klomp dot org> ---
Thanks for the report and the reproducer.
Your analysis is correct.

The problem is summarized in this comment just before the problematic code:

  /* Slurp the relevant header bits and section contents and feed them
     into the hash function.  The only bits we ignore are the offset
     fields in ehdr and shdrs, since the semantically identical ELF file
     could be written differently if it doesn't change the phdr layout.
     We always use the GElf (i.e. Elf64) formats for the bits to hash
     since it is convenient.  It doesn't matter whether this is an Elf32
     or Elf64 object, only that we are consistent in what bits feed the
     hash so it comes out the same for the same file contents.  */

So we mangled the actual data structure, but then check it as if it hasn't been
flipped around anyway when checking for NOBITS. The fix is to use the original
data/shdr to check for NOBITS:

diff --git a/tools/debugedit.c b/tools/debugedit.c
index 668777a..3f1e830 100644
--- a/tools/debugedit.c
+++ b/tools/debugedit.c
@@ -3273,7 +3273,7 @@ handle_build_id (DSO *dso, Elf_Data *build_id,
          else
            sha1_process_bytes (x.d_buf, x.d_size, &sha1_ctx);

-         if (u.shdr.sh_type != SHT_NOBITS)
+         if (dso->shdr[i].sh_type != SHT_NOBITS)
            {
              Elf_Data *d = elf_getdata (dso->scn[i], NULL);
              if (d == NULL)

-- 
You are receiving this mail because:
You are on the CC list for the bug.

From sourceware-bugzilla@sourceware.org  Mon Oct 11 14:17:50 2021
From: sourceware-bugzilla@sourceware.org (mark at klomp dot org)
Date: Mon, 11 Oct 2021 14:17:50 +0000
Subject: [Bug debugedit/28408] debugedit segfaults while changing binutils'
 build-id
In-Reply-To: <bug-28408-13298@http.sourceware.org/bugzilla/>
References: <bug-28408-13298@http.sourceware.org/bugzilla/>
Message-ID: <bug-28408-13298-RWs0UKfTT8@http.sourceware.org/bugzilla/>

https://sourceware.org/bugzilla/show_bug.cgi?id=28408

Mark Wielaard <mark at klomp dot org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #2 from Mark Wielaard <mark at klomp dot org> ---
commit f62fdfc7e8ddb9887f91a021636a4b430c76ec05
Author: Mark Wielaard <mark@klomp.org>
Date:   Mon Oct 11 13:57:15 2021 +0200

    debugedit: Use original shdr sh_type to check for NOBITS

            * tools/debugedit.c (handle_build_id): Check SHT_NOBITS
            against dso->shdr[i].sh_type.

    https://www.sourceware.org/bugzilla/show_bug.cgi?id=28408

    Reported-by: Alexey Brodkin <alexey.brodkin@gmail.com>
    Signed-off-by: Mark Wielaard <mark@klomp.org>

-- 
You are receiving this mail because:
You are on the CC list for the bug.

