[PATCH v3 RESEND] elf: Bound note parsing in ldconfig PT_GNU_PROPERTY handling [BZ #34954]

skyman wftw skyman-wftw@hotmail.com
Fri Nov 20 07:53:45 GMT 2026


From: JT Wang <skyman-wftw@hotmail.com>

process_elf_file does not validate the note header fields of a
PT_GNU_PROPERTY segment against the segment size.  A malformed object
can make ldconfig read out of bounds:

1. n_descsz is never checked against the segment, so a large value
   with zero-filled properties makes the x86 property loop read past
   the mapping.
2. When a 64-bit ldconfig processes an ELFCLASS32 object, the loop
   guard in process_elf32_file compares pointers truncated to
   Elf32_Addr, so crafted n_namesz/n_descsz values advance the note
   pointer by 4 GiB while the guard sees 12 bytes.
3. A segment ending after the note header but before the end of the
   owner name lets memcmp and the descriptor reads run past the
   segment.

Track the note and property positions as size_t offsets within the
segment and check every name, descriptor, padding and property size
against the remaining length before use.

Add tst-ldconfig-bad-note with crafted objects for each case and a
well-formed GNU property object.

Signed-off-by: JT Wang <skyman-wftw@hotmail.com>
---
[Resending: the first v3 posting was mangled by my mail client
(tabs in diff context lines were converted to spaces), so it does
not apply with git am.  The patch is attached as plain text this
time; the content is unchanged.]

Changes in v3, in response to Adhemerval's review of v2:
v2: https://sourceware.org/pipermail/libc-alpha/2026-November/181967.html
review: https://sourceware.org/pipermail/libc-alpha/2026-November/182221.html

R1 (n_descsz vs segment end)
  The descriptor size is now checked against the remaining segment
  length before the property loop, and the property loop itself uses
  offsets.  Test: r1_descsz.

R2 (ELFCLASS32 offset truncation)
  The loop no longer compares truncated pointers; positions are size_t
  offsets within the segment, and every length is bounded before it is
  added.  Test: r2_class32 (EM_386, p_align 4, n_namesz 0x80000000,
  n_descsz 0x7ffffff4).

R3 (tail-note memcmp overread)
  The owner name (with padding) and descriptor are bounded before
  memcmp.  Test: r3_tail (p_filesz 13, the segment ends after the
  first byte of the name).

When a note is malformed the rest of the segment is skipped, since the
position of the following notes cannot be trusted; this matches the
existing handling of invalid properties.

The new test is a smoke test and does not reliably fail on unpatched
ldconfig.  Reads beyond the declared segment can remain within the
file mapping, and reads beyond EOF can reach the zero-filled tail of
its last page or other readable mappings.  I checked each case with an
out-of-tree harness that calls process_elf_file on inputs placed next
to PROT_NONE pages: unpatched, r1, r2 and r3 crash with SIGSEGV;
patched, all return cleanly, and the well-formed object yields ISA
level 2 with and without the patch.  The continued scan over
zero-filled properties depends on the Linux x86 read_gnu_property
hook, so the test only runs on x86_64.

This patch is limited to ldconfig.  The other ELF_NOTE_NEXT_OFFSET
users (elf/dl-load.c and sysdeps/x86/dl-prop.h) are in ld.so, which
only loads objects of the native class, so the truncation in (2) does
not apply there; I have not audited those loops otherwise.

Other parsing paths remain outside the scope of this patch: the
program-header bounds checks do not cover complete entries, the
dynamic array walk is not bounded by the segment size, DT_SONAME is
not checked for termination within the file, and note fields are read
without checking the actual alignment of p_offset.  I can send a
separate patch for those if that is welcome.

Tested on x86_64 on top of commit
f04ea087c100da1e18032bc50d2d821f50f00c9e: make check subdirs=elf shows
no new failures, and tst-ldconfig-bad-note passes.  On i686 the new
test builds and reports UNSUPPORTED.

Thanks for the careful review.

 elf/Makefile                |   1 +
 elf/readelflib.c            |  77 ++++++---
 elf/tst-ldconfig-bad-note.c | 304 ++++++++++++++++++++++++++++++++++++
 3 files changed, 361 insertions(+), 21 deletions(-)
 create mode 100644 elf/tst-ldconfig-bad-note.c
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: 0001-elf-Bound-note-parsing-ldconfig-v3.patch.txt
URL: <https://sourceware.org/pipermail/libc-alpha/attachments/20261120/90fafba0/attachment-0001.txt>


More information about the Libc-alpha mailing list