[PATCH v2] elf: Check ldconfig property segment bounds [BZ #34954]
Adhemerval Zanella Netto
adhemerval.zanella@linaro.org
Wed Nov 18 11:23:57 GMT 2026
On 09/11/26 11:34, skyman wftw wrote:
> readelflib.c process_elf_file() does not validate p_offset or p_filesz
> for PT_GNU_PROPERTY segments before constructing a pointer into the
> mapped file contents. A shared library with a PT_GNU_PROPERTY segment
> whose p_offset exceeds the file size causes ldconfig to SIGSEGV.
>
> Add a range check before the note pointer is constructed. The check
> uses subtraction (file_length - p_offset) to avoid overflow from
> computing p_offset + p_filesz. An out-of-range segment is skipped,
> consistent with how the existing code already skips property segments
> with incorrect alignment.
>
> Tested on x86_64-linux-gnu (glibc 2.39):
> - PoC with p_offset=0x100000000000: cleanly skipped, no crash
> - Normal libraries (libc.so.6, libcuda.so.1): SONAME parsed correctly
>
> Signed-off-by: skyman wftw <skyman-wftw@hotmail.com>
Please develop and test the patch on master, and run at least the
elf/tst-ldconfig-* tests.
> ---
> v2: Align Signed-off-by with the patch author (From header).
>
> elf/readelflib.c | 5 +++++
> 1 file changed, 5 insertions(+)
>
> diff --git a/elf/readelflib.c b/elf/readelflib.c
> index XXXXXXX..XXXXXXX 100644
> --- a/elf/readelflib.c
> +++ b/elf/readelflib.c
> @@ -115,6 +115,11 @@
Please generate the patch with git format-patch and send with git send-email
if possible. It applies with git am, but the placeholder index line and the
missing function context in the hunk header suggest it was edited by hand.
> notes with incorrect alignment. */
> if (segment->p_align == (__ELF_NATIVE_CLASS / 8))
> {
> + /* Skip property segments whose file range is out of bounds. */
> + if (segment->p_offset > file_length
> + || segment->p_filesz > file_length - segment->p_offset)
> + break;
> +
> const ElfW(Nhdr) *note = (const void *) (file_contents
> + segment->p_offset);
> const ElfW(Addr) size = segment->p_filesz;
Although the check if correct, it is still incomplete to fix all issues
from invalid property handling (BZ #34954). The bug report already notes
some scenarios:
1. n_descsz is never checked against p_filesz. With
n_descsz = 0xfffffff8 and zero-filled properties, the x86
read_gnu_property returns false for type 0. The property loop
then keeps reading until it leaves the mapping.
2. When a 64-bit ldconfig handles an ELFCLASS32 object,
ELF_NOTE_NEXT_OFFSET is computed in size_t but the loop guard
truncates it to Elf32_Addr. With n_namesz = 0x80000000 and
n_descsz = 0x7ffffff4, the note pointer moves 4 GiB ahead while
the guard sees an offset of 12, and the next n_namesz read
faults.
3. If the segment ends right after a note header, the memcmp on the
name reads past it, and so does the descriptor read that follows.
The fix should check each note against the remaining segment size and
work with offsets rather than pointer sums.
More information about the Libc-alpha
mailing list