[PATCH v3] elf: Bound note parsing in ldconfig PT_GNU_PROPERTY handling [BZ #34954]
skyman wftw
skyman-wftw@hotmail.com
Fri Nov 20 07:38:51 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>
---
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
diff --git a/elf/Makefile b/elf/Makefile
index 5774f71988..82616fe141 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -311,6 +311,7 @@ tests-container := \
tst-dl-cache-long-path \
tst-dl-cache-old-format \
tst-ldconfig-bad-aux-cache \
+ tst-ldconfig-bad-note \
tst-ldconfig-ld_so_conf-update \
# tests-container
diff --git a/elf/readelflib.c b/elf/readelflib.c
index f16c263979..3d7e9b5ace 100644
--- a/elf/readelflib.c
+++ b/elf/readelflib.c
@@ -115,16 +115,43 @@ process_elf_file (const char *file_name, const char *lib, int *flag,
notes with incorrect alignment. */
if (segment->p_align == (__ELF_NATIVE_CLASS / 8))
{
- const ElfW(Nhdr) *note = (const void *) (file_contents
- + segment->p_offset);
- const ElfW(Addr) size = segment->p_filesz;
- const ElfW(Addr) align = segment->p_align;
-
- const ElfW(Addr) start = (ElfW(Addr)) (uintptr_t) note;
+ /* The segment must lie completely within the mapped file. */
+ if (segment->p_offset > file_length
+ || segment->p_filesz > file_length - segment->p_offset)
+ break;
+
+ const unsigned char *seg
+ = (const unsigned char *) file_contents + segment->p_offset;
+ const size_t seg_size = segment->p_filesz;
+ const size_t align = segment->p_align;
+ size_t off = 0; /* Offset of the current note. */
unsigned int last_type = 0;
- while ((ElfW(Addr)) (uintptr_t) (note + 1) - start < size)
+ /* Track the note and property positions as size_t offsets
+ within the segment and check every name, descriptor and
+ padding length against the remaining length before
+ advancing. Do not compare pointers truncated to
+ ElfW(Addr), which is 32 bits in process_elf32_file. */
+ while (seg_size - off >= sizeof (ElfW(Nhdr)))
{
+ const ElfW(Nhdr) *note = (const void *) (seg + off);
+ size_t left = seg_size - off; /* >= sizeof (ElfW(Nhdr)). */
+
+ /* The owner name and its padding must fit in the
+ segment. raw is the unaligned end offset of the
+ name. */
+ if (note->n_namesz > left - sizeof (ElfW(Nhdr)))
+ break;
+ size_t raw = sizeof (ElfW(Nhdr)) + note->n_namesz;
+ size_t pad = -raw & (align - 1);
+ if (pad > left - raw)
+ break;
+ size_t desc_off = raw + pad;
+
+ /* The descriptor must fit in the segment. */
+ if (note->n_descsz > left - desc_off)
+ break;
+
/* Find the NT_GNU_PROPERTY_TYPE_0 note. */
if (note->n_namesz == 4
&& note->n_type == NT_GNU_PROPERTY_TYPE_0
@@ -135,21 +162,24 @@ process_elf_file (const char *file_name, const char *lib, int *flag,
|| (note->n_descsz % sizeof (ElfW(Addr))) != 0)
goto done;
- /* Start and end of property array. */
- unsigned char *ptr = (unsigned char *) (note + 1) + 4;
- unsigned char *ptr_end = ptr + note->n_descsz;
+ /* Start and length of the property array. */
+ const unsigned char *desc = seg + off + desc_off;
+ size_t dleft = note->n_descsz;
+ size_t poff = 0;
do
{
- unsigned int type = *(unsigned int *) ptr;
- unsigned int datasz = *(unsigned int *) (ptr + 4);
+ unsigned int type
+ = *(const unsigned int *) (desc + poff);
+ unsigned int datasz
+ = *(const unsigned int *) (desc + poff + 4);
/* Property type must be in ascending order. */
if (type < last_type)
goto done;
- ptr += 8;
- if ((ptr + datasz) > ptr_end)
+ poff += 8;
+ if (datasz > dleft - poff)
goto done;
last_type = type;
@@ -160,22 +190,27 @@ process_elf_file (const char *file_name, const char *lib, int *flag,
true : Stop processing the properties.
*/
if (read_gnu_property (isa_level, type,
- datasz, ptr))
+ datasz,
+ (void *) (desc + poff)))
goto done;
/* Check the next property item. */
- ptr += ALIGN_UP (datasz, sizeof (ElfW(Addr)));
+ poff += ALIGN_UP (datasz, sizeof (ElfW(Addr)));
}
- while ((ptr_end - ptr) >= 8);
+ while (dleft - poff >= 8);
/* Only handle one NT_GNU_PROPERTY_TYPE_0. */
goto done;
}
- note = ((const void *) note
- + ELF_NOTE_NEXT_OFFSET (note->n_namesz,
- note->n_descsz,
- align));
+ /* Advance to the next note. raw is the unaligned end
+ offset of the descriptor. If the padding reaches
+ the end of the segment there is no next note. */
+ raw = desc_off + note->n_descsz;
+ pad = -raw & (align - 1);
+ if (pad >= left - raw)
+ break;
+ off += raw + pad;
}
}
done:
diff --git a/elf/tst-ldconfig-bad-note.c b/elf/tst-ldconfig-bad-note.c
new file mode 100644
index 0000000000..9b1a0e4429
--- /dev/null
+++ b/elf/tst-ldconfig-bad-note.c
@@ -0,0 +1,304 @@
+/* Smoke-test ldconfig with malformed PT_GNU_PROPERTY notes (bug 34954).
+ Copyright The GNU Toolchain Authors.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If
+ not, see <https://www.gnu.org/licenses/>. */
+
+/* Each test case below places a crafted ELF object in its own
+ directory and runs "ldconfig -i -n <dir>" on it in a separate
+ subprocess, asserting that ldconfig exits normally. The malformed
+ objects contain deliberately invalid program-header or note-header
+ fields without allocating the sizes they claim. The objects are
+ little-endian x86-64/i386, and the out-of-bounds paths depend on the
+ x86 GNU property handling of a 64-bit ldconfig, so the test only
+ runs on x86_64 (LP64). */
+
+#include <elf.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <support/capture_subprocess.h>
+#include <support/check.h>
+#include <support/support.h>
+#include <support/xunistd.h>
+
+#if defined __x86_64__ && defined __LP64__
+
+/* A fixed-size buffer holding a little-endian ELF image. */
+struct buf
+{
+ unsigned char b[8192];
+ size_t n;
+};
+
+static void
+bput (struct buf *t, const void *d, size_t n)
+{
+ TEST_VERIFY_EXIT (n <= sizeof (t->b) - t->n);
+ memcpy (t->b + t->n, d, n);
+ t->n += n;
+}
+
+/* Append the low N bytes of V in little-endian order. */
+static void
+bval (struct buf *t, uint64_t v, size_t n)
+{
+ TEST_VERIFY_EXIT (n <= sizeof (v) && n <= sizeof (t->b) - t->n);
+ for (size_t i = 0; i < n; i++)
+ t->b[t->n + i] = (unsigned char) (v >> (8 * i));
+ t->n += n;
+}
+
+static void
+put_ehdr64 (struct buf *t, int phnum)
+{
+ const unsigned char e_ident[16]
+ = { 0x7f, 'E', 'L', 'F', ELFCLASS64, ELFDATA2LSB, EV_CURRENT, 0 };
+ bput (t, e_ident, 16);
+ bval (t, ET_DYN, 2); /* e_type. */
+ bval (t, EM_X86_64, 2); /* e_machine. */
+ bval (t, EV_CURRENT, 4); /* e_version. */
+ bval (t, 0, 8); /* e_entry. */
+ bval (t, 64, 8); /* e_phoff. */
+ bval (t, 0, 8); /* e_shoff. */
+ bval (t, 0, 4); /* e_flags. */
+ bval (t, 64, 2); /* e_ehsize. */
+ bval (t, 56, 2); /* e_phentsize. */
+ bval (t, phnum, 2); /* e_phnum. */
+ bval (t, 64, 2); /* e_shentsize. */
+ bval (t, 0, 2); /* e_shnum. */
+ bval (t, 0, 2); /* e_shstrndx. */
+}
+
+static void
+put_phdr64 (struct buf *t, uint32_t type, uint64_t off, uint64_t filesz,
+ uint64_t align)
+{
+ bval (t, type, 4); /* p_type. */
+ bval (t, PF_R, 4); /* p_flags. */
+ bval (t, off, 8); /* p_offset. */
+ bval (t, off, 8); /* p_vaddr. */
+ bval (t, off, 8); /* p_paddr. */
+ bval (t, filesz, 8); /* p_filesz. */
+ bval (t, filesz, 8); /* p_memsz. */
+ bval (t, align, 8); /* p_align. */
+}
+
+static void
+put_ehdr32 (struct buf *t, int phnum)
+{
+ const unsigned char e_ident[16]
+ = { 0x7f, 'E', 'L', 'F', ELFCLASS32, ELFDATA2LSB, EV_CURRENT, 0 };
+ bput (t, e_ident, 16);
+ bval (t, ET_DYN, 2); /* e_type. */
+ bval (t, EM_386, 2); /* e_machine. */
+ bval (t, EV_CURRENT, 4); /* e_version. */
+ bval (t, 0, 4); /* e_entry. */
+ bval (t, 52, 4); /* e_phoff. */
+ bval (t, 0, 4); /* e_shoff. */
+ bval (t, 0, 4); /* e_flags. */
+ bval (t, 52, 2); /* e_ehsize. */
+ bval (t, 32, 2); /* e_phentsize. */
+ bval (t, phnum, 2); /* e_phnum. */
+ bval (t, 40, 2); /* e_shentsize. */
+ bval (t, 0, 2); /* e_shnum. */
+ bval (t, 0, 2); /* e_shstrndx. */
+}
+
+static void
+put_phdr32 (struct buf *t, uint32_t type, uint64_t off, uint64_t filesz,
+ uint64_t align)
+{
+ bval (t, type, 4); /* p_type. */
+ bval (t, off, 4); /* p_offset. */
+ bval (t, off, 4); /* p_vaddr. */
+ bval (t, off, 4); /* p_paddr. */
+ bval (t, filesz, 4); /* p_filesz. */
+ bval (t, filesz, 4); /* p_memsz. */
+ bval (t, PF_R, 4); /* p_flags. */
+ bval (t, align, 4); /* p_align. */
+}
+
+/* A 12-byte note header. */
+static void
+put_nhdr (struct buf *t, uint32_t namesz, uint32_t descsz, uint32_t type)
+{
+ bval (t, namesz, 4);
+ bval (t, descsz, 4);
+ bval (t, type, 4);
+}
+
+static void
+execv_wrapper (void *args)
+{
+ char **argv = args;
+
+ execv (argv[0], argv);
+ FAIL_EXIT1 ("execv: %m");
+}
+
+/* Write a test case object into its own directory and run
+ "ldconfig -i -n" on that directory in a subprocess. */
+static void
+run_case (const char *name, const struct buf *t)
+{
+ char *dir = xasprintf ("/tst-ldconfig-bad-note-%s", name);
+ xmkdirp (dir, 0777);
+ char *path = xasprintf ("%s/libbadnote-%s.so", dir, name);
+ int fd = xopen (path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
+ xwrite (fd, t->b, t->n);
+ xclose (fd);
+
+ char *prog = xasprintf ("%s/ldconfig", support_install_rootsbindir);
+ char *args[] = { prog, (char *) "-i", (char *) "-n", dir, NULL };
+ struct support_capture_subprocess result
+ = support_capture_subprocess (execv_wrapper, args);
+ support_capture_subprocess_check (&result, name, 0, sc_allow_stderr);
+ support_capture_subprocess_free (&result);
+ free (prog);
+ free (path);
+ free (dir);
+}
+
+/* PT_GNU_PROPERTY p_offset points past the end of the file. */
+static void
+case_r0_offset (void)
+{
+ struct buf t = { 0 };
+ put_ehdr64 (&t, 1);
+ put_phdr64 (&t, PT_GNU_PROPERTY, 0x10000, 0x100, 8);
+ run_case ("r0-offset", &t);
+}
+
+/* ELFCLASS64: a complete GNU note header and name claim a descriptor
+ of 0xfffffff8 bytes, followed by zero fill up to the end of the
+ file; the zero properties keep the property loop scanning. */
+static void
+case_r1_descsz (void)
+{
+ struct buf t = { 0 };
+ put_ehdr64 (&t, 1);
+ const size_t seg_off = 120; /* Ehdr + one Phdr. */
+ put_phdr64 (&t, PT_GNU_PROPERTY, seg_off, 12 + 4 + 256, 8);
+ put_nhdr (&t, 4, 0xfffffff8, NT_GNU_PROPERTY_TYPE_0);
+ bput (&t, "GNU", 4);
+ static const unsigned char zeros[256];
+ bput (&t, zeros, sizeof (zeros));
+ run_case ("r1-descsz", &t);
+}
+
+/* ELFCLASS32/EM_386 processed by 64-bit ldconfig: crafted n_namesz and
+ n_descsz advance the (64-bit) note offset by 4 GiB while the loop
+ guard, which compares pointers truncated to Elf32_Addr, still sees
+ an offset within the segment. */
+static void
+case_r2_class32 (void)
+{
+ struct buf t = { 0 };
+ put_ehdr32 (&t, 1);
+ const size_t seg_off = 84; /* Ehdr32 + one Phdr32. */
+ put_phdr32 (&t, PT_GNU_PROPERTY, seg_off, 16, 4);
+ put_nhdr (&t, 0x80000000, 0x7ffffff4, NT_GNU_PROPERTY_TYPE_0);
+ bval (&t, 0, 4);
+ run_case ("r2-class32", &t);
+}
+
+/* The segment ends after the first byte of the owner name
+ (p_filesz == 13): memcmp and the descriptor reads would run past
+ the segment. */
+static void
+case_r3_tail (void)
+{
+ struct buf t = { 0 };
+ put_ehdr64 (&t, 1);
+ const size_t seg_off = 120;
+ put_phdr64 (&t, PT_GNU_PROPERTY, seg_off, 13, 8);
+ put_nhdr (&t, 4, 0xfffffff8, NT_GNU_PROPERTY_TYPE_0);
+ bput (&t, "G", 1); /* Last byte inside the segment. */
+ bput (&t, "NU", 3); /* Past the segment, inside the file. */
+ static const unsigned char zeros[256];
+ bput (&t, zeros, sizeof (zeros));
+ run_case ("r3-tail", &t);
+}
+
+/* A minimal ELF image with dynamic metadata (DT_STRTAB, DT_SONAME) and
+ well-formed notes for ldconfig to parse: a non-GNU note followed by
+ a GNU note with a GNU_PROPERTY_X86_ISA_1_NEEDED property. */
+static void
+case_legit_prop (void)
+{
+ struct buf t = { 0 };
+ const size_t dyn_off = 64 + 3 * 56;
+ const size_t strtab_off = dyn_off + 3 * 16;
+ const size_t seg_off = strtab_off + 32;
+ const size_t seg_size = 16 + 32;
+ const size_t file_size = seg_off + seg_size;
+
+ put_ehdr64 (&t, 3);
+ put_phdr64 (&t, PT_LOAD, 0, file_size, 0x1000);
+ put_phdr64 (&t, PT_DYNAMIC, dyn_off, 3 * 16, 8);
+ put_phdr64 (&t, PT_GNU_PROPERTY, seg_off, seg_size, 8);
+
+ /* Dynamic entries: DT_STRTAB, DT_SONAME, DT_NULL. */
+ bval (&t, DT_STRTAB, 8);
+ bval (&t, strtab_off, 8);
+ bval (&t, DT_SONAME, 8);
+ bval (&t, 1, 8);
+ bval (&t, DT_NULL, 8);
+ bval (&t, 0, 8);
+
+ bput (&t, "\0libbadnote-legit.so", sizeof ("\0libbadnote-legit.so"));
+ /* Pad up to the note segment. */
+ memset (t.b + t.n, 0, seg_off - t.n);
+ t.n = seg_off;
+
+ /* A non-GNU note, to cover advancing to the next note. */
+ put_nhdr (&t, 3, 0, 1);
+ bput (&t, "abc", 3);
+ bval (&t, 0, 1); /* Padding to the 8-byte alignment. */
+
+ put_nhdr (&t, 4, 16, NT_GNU_PROPERTY_TYPE_0);
+ bput (&t, "GNU", 4);
+ /* One GNU_PROPERTY_X86_ISA_1_NEEDED property, datasz 4. */
+ bval (&t, GNU_PROPERTY_X86_ISA_1_NEEDED, 4);
+ bval (&t, 4, 4);
+ bval (&t, 1U << 2, 4); /* x86-64-v3. */
+ bval (&t, 0, 4); /* Padding. */
+
+ run_case ("legit-prop", &t);
+}
+
+#endif /* __x86_64__ && __LP64__ */
+
+static int
+do_test (void)
+{
+#if defined __x86_64__ && defined __LP64__
+ case_r0_offset ();
+ case_r1_descsz ();
+ case_r2_class32 ();
+ case_r3_tail ();
+ case_legit_prop ();
+ return 0;
+#else
+ FAIL_UNSUPPORTED ("test requires x86_64 (LP64)");
+#endif
+}
+
+#include <support/test-driver.c>
More information about the Libc-alpha
mailing list