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

            Bug ID: 35079
           Summary: heap buffer overflow read in elf_x86_64_tls_transition
                    (bfd/elf64-x86-64.c)
           Product: binutils
           Version: 2.48 (HEAD)
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: ld
          Assignee: unassigned at sourceware dot org
          Reporter: emily.west286 at gmail dot com
  Target Milestone: ---

Created attachment 17301
  --> https://sourceware.org/bugzilla/attachment.cgi?id=17301&action=edit
Test case input

Binutils ld: heap out-of-bounds read in elf_x86_64_tls_transition on
R_X86_64_TLSDESC_CALL with a large r_offset

Component: ld
Version: binutils-gdb master, reproduced on commit
c79376a262328164cdf81eddfcc5518314b1856a
Target: x86_64-pc-linux-gnu

Summary:
ld reads one byte before the section contents buffer when a debug section
without SHF_ALLOC holds an R_X86_64_TLSDESC_CALL relocation with r_offset =
0xffffffffffffffff. The bounds check on the offset overflows, so the offset is
accepted.

==1==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6290000051ff
READ of size 1 at 0x6290000051ff thread T0
0x6290000051ff is located 1 bytes to the left of 16384-byte region
[0x629000005200,0x629000009200)
SUMMARY: AddressSanitizer: heap-buffer-overflow bfd/elf64-x86-64.c:1558 in
elf_x86_64_tls_transition

Reproducer:
part0.o (attached): 696 bytes, SHA-256
b0ad49f82101e9cf05eb6d4431d05fc1f469e1beeeb38baf9f17fb53d0897797

ld -o /tmp/out part0.o

It also reproduces with -pie --noinhibit-exec. The object has a 1-byte
.debug_info section with one relocation:

    Offset             Info             Type                   Symbol's Name +
Addend
ffffffffffffffff  0000000100000023 R_X86_64_TLSDESC_CALL  g + 4

With the attached Dockerfile (with gcc 12, -O1, ASan and UBSan):

docker build -t ld-repro-cl1 .
docker run --rm ld-repro-cl1 part0.o

Backtrace:
#0 elf_x86_64_tls_transition   bfd/elf64-x86-64.c:1558
#1 elf_x86_64_relocate_section bfd/elf64-x86-64.c:4183
#2 elf_link_input_bfd          bfd/elflink.c:11978
#3 _bfd_elf_final_link         bfd/elflink.c:13241
#4 ldwrite                     ld/ldwrite.c:548
#5 main                        ld/ldmain.c:1001

The buffer is allocated by bfd_malloc in _bfd_elf_final_link
(bfd/elflink.c:13116).

Root cause:
The bounds check for R_X86_64_TLSDESC_CALL at line 1540 adds to the offset
before comparing it:

c
      offset = rel->r_offset;
      call = NULL;
      if (offset + 2 <= sec->size)
        {
          unsigned int prefix;
          call = contents + offset;

offset is a 64-bit unsigned value. With r_offset = 0xffffffffffffffff, offset +
2 wraps to 1, which is not greater than the section size of 1. The check
passes, call points one byte before contents, and line 1558 reads call[prefix].

The relocation is not rejected earlier:
elf_x86_64_scan_relocs checks the offset with bfd_reloc_offset_in_range (line
2568), but it is not called for this section. _bfd_elf_link_iterate_on_relocs
skips sections without SEC_ALLOC (elflink.c:4352).
elf_x86_64_relocate_section does process the section, because it only rejects
TLS relocations when both SEC_ALLOC and SEC_DEBUGGING are clear (line 4173).

gdb, part0.o:
input_section->name = ".debug_info", flags & SEC_ALLOC = 0, flags &
SEC_DEBUGGING = 0x2000
relocs[0] = {r_offset = 0xffffffffffffffff, r_info = 0x100000023, r_addend =
0x4}
A breakpoint on elf_x86_64_scan_relocs was not hit before
elf_x86_64_relocate_section.

At line 1540:
offset = 0xffffffffffffffff, sec->size = 1
offset + 2 = 1
offset + 2 <= sec->size = 1 (true)
contents = 0x629000005200, contents + offset = 0x6290000051ff

Other versions:
GNU ld 2.46 (Ubuntu package, no sanitizers): does not crash. It rejects the
input with "relocation R_X86_64_TLSDESC_CALL against `g' must be used in
indirect CALL with RAX register only" (exit status 1). Valgrind reports an
invalid read of size 1, 1 byte before a 16,384-byte block. The package has no
debug symbols, so the function name was not confirmed there.
binutils-gdb master at commit c2c4548e47e002389eb583593ef15e6009b8af3f: the
same check is still at line 1540. This was checked by reading the source only;
ld was not built at that commit.

Impact:
Low. It requires a crafted object file. The read starts at most 2 bytes before
the buffer, because only offsets 0xfffffffffffffffe and 0xffffffffffffffff wrap
past the check. No out-of-bounds write was observed, and without sanitizers ld
2.46 exits with an error.

Suggested fix:
Compare without adding to the offset (tlsdesc-call-overflow.patch):

c
-      if (offset + 2 <= sec->size)
+      /* Don't add to OFFSET here: the sum can wrap around.  */
+      if (sec->size >= 2 && offset <= sec->size - 2)

The existing code then reports the invalid TLSDESC call as a normal error. The
later offset + 3 check for X32 needs no change, because offset is already
bounded by this check.

Fix verification:
ld was rebuilt at commit c79376a262328164cdf81eddfcc5518314b1856a with the
patch applied. part0.o now fails with an error.

/src/build/ld/ld-new: part0.o:(.debug_info+0xffffffffffffffff): undefined
reference to `g'
part0.o(.debug_info+0xffffffffffffffff): relocation R_X86_64_TLSDESC_CALL
against `g' must be used in indirect CALL with RAX register only
/src/build/ld/ld-new: final link failed: bad value

patch.Dockerfile builds ld with the patch applied:

docker build -f patch.Dockerfile -t ld-fix-cl1 .
docker run --rm -v "$PWD":/work ld-fix-cl1 part0.o

Attachments:
part0.o                        reproducer
Dockerfile                     builds ld at the commit above with ASan and
UBSan
tlsdesc-call-overflow.patch    suggested fix
patch.Dockerfile               builds ld with the patch applied

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

Reply via email to