fix(dracut-install): validate ELF section-table bounds - #2615
Open
prabhakarpujeri wants to merge 1 commit into
Open
fix(dracut-install): validate ELF section-table bounds#2615prabhakarpujeri wants to merge 1 commit into
prabhakarpujeri wants to merge 1 commit into
Conversation
The PARSE_ELF_START macro checked only that e_shoff and e_shstrndx are individually < src_len, then indexed shdr[e_shstrndx].sh_offset into the mmap'ed file. Two classes of OOB reads remain on truncated or malformed ELFs: 1. The section header table extent is not validated: e_shnum section headers starting at shoff can extend past the end of the file, so iterating them reads past the map. 2. The section header string table (shstrtab) content extent isn't validated: sh_offset/sh_size come from the file, and the strcmp(&shstrtab[shdr[i].sh_name], ...) reads at attacker-controlled offsets, potentially past the mapping. Add the missing bounds checks: - PARSE_ELF_START now verifies the whole section table fits in the mapped file, then treats it as untrusted: a helper reads the shstrtab section's offset and size, validates both against src_len, and the section-name lookup checks the name offset and NUL termination inside the table before any strcmp. - DRUNPATH/DT_SONAME/DT_NEEDED walks now validate the shadow-section link index (sh_link < e_shnum) before dereferencing shdr[sh_link]. Verified by crafting five malformed ELF64 inputs to the new boundschecks and feeding them to the dependency resolution path: - Off-map shstrtab.sh_offset: segfault without patch, clean skip with - sh_name past table end: segfault without patch - No NUL within name's table window: segfault without patch - Section table extent truncated (F1) / sh_link past end (F6b): clean rejection with patch Regression control: -l dependency resolution of /bin/bash and /bin/ls yields byte-identical results before/after. Also verified syntax with and without HAVE_SYSTEMD (covers RESOLVE_DEPS_DLOPEN_FOR_BITS).
Collaborator
|
CC @marcosfrm |
Collaborator
chewi
approved these changes
Aug 19, 2026
chewi
left a comment
Member
There was a problem hiding this comment.
Thank you, this is very thorough. I must admit that the low-level ELF handling made my head hurt, and I relied on AI a fair bit here. I've stared at these changes for a while though, and I think they look good.
devkontrol
approved these changes
Aug 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
src/install/dracut-install.c'sPARSE_ELF_STARTmacro (starting point of all three ELF-walking macros:FIND_LIBRARY_RUNPATH_FOR_BITS,RESOLVE_DEPS_DLOPEN_FOR_BITS,RESOLVE_DEPS_NEEDED_FOR_BITS) only checkede_shoff > src_lenande_shstrndx >= e_shnum. It missed the two aggregate bounds that the subsequent pointer arithmetic relies on:e_shoff + e_shnum * sizeof(Shdr) <= src_len. Unvalidated, so iterating section headers (or readingshdr[i].sh_name) can walk past the end of the mapped file.shdr[i].sh_offset/sh_sizevalues are file-controlled and used to computeshstrtab = map + sh_offset, andshstrtab[shdr[i].sh_name]is dereferenced (viastrcmp()against ".dynamic" / ".note.dlopen") without checking either lies within the file. A malformed ELF can therefore cause a read outside the mmap: crash (SIGSEGV) or garbage-based decisions.Additionally,
shdr[i].sh_link(another file-originated index) was dereferenced without bounds-checking in the RUNPATH/SONAME/NEEDED walks.The trigger context is the dependency-resolution path (
dracut-install -l), invoked during every initramfs build that resolves lazy deps: the tool mmaps a binary under a sysroot and walks its sections. A malformed or truncated ELF inside the build environment makes the current code read out of bounds.Fix
PARSE_ELF_START: reject if the whole section header table doesn't fit (e_shnum > (src_len - e_shoff) / sizeof(Shdr)), then read the shstrtab section'ssh_offset/sh_sizefrom the (now-bounds-checked) table and validate both againstsrc_len.elf_sect_name()returns a section name only ifsh_name < shstrtab_lenand the name is NUL-terminated within the table; used by all fourstrcmp(&shstrtab[shdr[i].sh_name], ...)sites.DT_SONAME/DT_NEEDEDwalks, reject ifshdr[i].sh_link >= e_shnumbefore dereferencingshdr[sh_link].No behavior change for well-formed input; malformed input is now rejected early rather than dereferenced through garbage pointers.
Verification
Built with
CFLAGS="-g -O2 -Wall -Wextra", clean (make indent-cpasses with the project's astyle config). Crafted 5 malformed ELF64 inputs exercising each check, fed throughdracut-install -l(the code path hit during image builds):sh_offsetpoints 16 MiB past EOF (F2)sh_name= 0xFFFFFFF0 (F3)".dynamic"with no NUL within table, ends at page boundary (F5b)sh_link= 99 with real .dynamic (F6b)(F1/F5b don't crash on the unpatched binary because the specific reads happen to stay within the mapped region's page — reads return garbage rather than faulting. The only added observable effect is guarded rejection.)
Regression control:
-lon/bin/bash+/bin/lsproduces byte-identical dependency set before vs after (14 entries, same names).Both compile paths tested: default (
-fsyntax-onlypasses) andgcc -fsyntax-only -DHAVE_SYSTEMD -DCONFIG_WEAKDEP $(pkg-config --cflags libsystemd) ...— soRESOLVE_DEPS_DLOPEN_FOR_BITS(which gets the helper) is also syntax-checked.