Repository navigation
fix(dracut-install): bounds-check RUNPATH walk and ELF string derefs - #2622
prabhakarpujeri wants to merge 4 commits into
Conversation
|
CC @chewi |
a0e87b3 to
26faa41
Compare
There was a problem hiding this comment.
Pull request overview
This PR hardens dracut-install’s ELF parsing logic to prevent out-of-bounds reads while walking section/program headers and extracting dynamic strings (RUNPATH/RPATH, SONAME, NEEDED, INTERP), improving robustness against malformed or truncated ELF inputs during dracut-install -l dependency resolution.
Changes:
- Adds
elf_sect_name()andelf_map_string()helpers to bounds-check section-name lookups and mapped NUL-terminated strings. - Strengthens
PARSE_ELF_STARTby validating the full section header table extent and the.shstrtabextent before dereferencing. - Updates RUNPATH / SONAME / NEEDED / INTERP paths to use the new helpers and adds additional per-iteration bounds checks.
Suppressed comments (4)
src/install/dracut-install.c:1089
- In RESOLVE_DEPS_DLOPEN_FOR_BITS, the for-loop condition reads d->d_tag before the bounds check inside the loop body. This can still read past the mapping on malformed input (even though the body has a guard).
for (Elf##B##_Dyn *d = dyn; !soname && ELF_BYTESWAP(32, d->d_tag) != DT_NULL; d++) { \
if ((char *)d < (char *)map || (char *)d + sizeof(Elf##B##_Dyn) > (char *)map + src_len) \
break; \
if (ELF_BYTESWAP(B, d->d_tag) != DT_SONAME) \
continue; \
src/install/dracut-install.c:1096
- Like the RUNPATH path, the DT_SONAME string offset uses sh_offset + d_val without overflow protection. A wraparound can make a bogus offset look in-range and return the wrong in-map string.
soname = elf_map_string((const char *)map, src_len, ELF_BYTESWAP(B, shdr[ELF_BYTESWAP(32, shdr[i].sh_link)].sh_offset) + ELF_BYTESWAP(B, d->d_un.d_val)); \
if (!soname) \
break; \
src/install/dracut-install.c:1179
- In RESOLVE_DEPS_NEEDED_FOR_BITS, the DT_NULL loop termination condition still dereferences d->d_tag before verifying d is within the mapped file. This undermines the new per-iteration bounds check for malformed/truncated dynamic sections.
for (Elf##B##_Dyn *d = dyn; ELF_BYTESWAP(32, d->d_tag) != DT_NULL; d++) { \
if ((char *)d < (char *)map || (char *)d + sizeof(Elf##B##_Dyn) > (char *)map + src_len) \
break; \
if (ELF_BYTESWAP(B, d->d_tag) != DT_NEEDED) \
continue; \
src/install/dracut-install.c:1186
- The DT_NEEDED string address is formed by adding sh_offset + d_val without checking for overflow. On wraparound, elf_map_string() can accept the wrapped offset and return an unrelated in-map string.
const char *soname = elf_map_string((const char *)map, src_len, ELF_BYTESWAP(B, shdr[ELF_BYTESWAP(32, shdr[i].sh_link)].sh_offset) + ELF_BYTESWAP(B, d->d_un.d_val)); \
if (!soname) \
break; \
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| for (Elf##B##_Dyn *d = dyn; ELF_BYTESWAP(32, d->d_tag) != DT_NULL; d++) { \ | ||
| if ((char *)d < (char *)map || (char *)d + sizeof(Elf##B##_Dyn) > (char *)map + src_len) \ | ||
| break; \ | ||
| if (ELF_BYTESWAP(B, d->d_tag) == DT_RUNPATH) \ |
There was a problem hiding this comment.
The loop condition is already safe here: the (char *)d + sizeof(Elf##B##_Dyn) <= (char *)map + src_len check runs first and && short-circuits, so d->d_tag is only dereferenced after the current entry has been proven to lie fully inside the mapping. The same guard order is used at the other two DT loops. No change needed for this one.
| const char *runpath = elf_map_string((const char *)map, src_len, ELF_BYTESWAP(B, shdr[ELF_BYTESWAP(32, shdr[i].sh_link)].sh_offset) + ELF_BYTESWAP(B, d->d_un.d_val)); \ | ||
| if (!runpath) \ | ||
| break; \ |
There was a problem hiding this comment.
Addressed in 3c101aa: the string-table offset and d->d_un.d_val are now each clamped to the mapped range before the addition, so the sum passed to elf_map_string() can no longer wrap. Applied at all three call sites the comment flagged (RUNPATH, SONAME, and NEEDED).
|
Follow-up commit 155d583 addresses the two Copilot notes on the
Sanity-checked: dependency resolution of real binaries (/bin/bash etc.) works unchanged. |
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).
The FIND_LIBRARY_RUNPATH_FOR_BITS macro lacked the bounds checks its sibling macros (RESOLVE_DEPS_NEEDED_FOR_BITS, RESOLVE_DEPS_DLOPEN_FOR_BITS) have: - the .dynamic section's shdr[]-indexed pointer into the map - the per-iteration Elf_Dyn pointer in the DT_NULL-terminated walk - the interpreter string (PT_INTERP) and DT_SONAME/DT_NEEDED string, passed to strdup/strlen with no NUL-termination verification A malformed ELF could put a non-NUL-terminated runpath right at the end of the map (reads past it in strlen), or an interpreter offset exactly == src_len (off by > vs >=) that passed the old bounds check. Add a shared helper, elf_map_string(), that validates the offset is inside the map and the string is NUL-terminated there, and use it at every place the code dereferences a dynamic-entry/segment-list string. Mirror the per-iteration map-range checks in the RUNPATH walk that the sibling macros already carry. Depends on 0549d1c ("fix(dracut-install): validate ELF section-table bounds"), which introduced elf_sect_name() and the PARSE_ELF_START extent checks. Verified with valgrind on crafted malformed ELF64s (pre-patch SIGSEGV reading past the mmap; post-patch clean) and byte-for-byte identical dependency sets for real binaries (/bin/bash, /bin/ls, /usr/bin/ssh).
The .dynamic section walk loops dereferenced d->d_tag in the loop condition before the in-loop bounds check ran. A malformed ELF whose .dynamic section ends at the mapped region boundary without a DT_NULL terminator made the condition read past the mapping; with the mapping placed exactly before an unmapped/reserved page this segfaults (reproduced with an LD_PRELOAD that places PROT_NONE guard pages after file mmaps). Check the entry bounds in the loop condition before dereferencing, and drop the now-redundant in-loop checks. Also compare the section offset against the file length before forming the pointer.
The string offset passed to elf_map_string() is computed as sh_offset + d_val with no overflow check. On malformed input the addition can wrap, and the helper then validates and returns a string at an unrelated in-map offset, changing dependency resolution on corrupt binaries. Clamp the string-table offset and the dynamic entry's d_val to the mapped range before adding them at the RUNPATH, SONAME, and NEEDED lookup sites, so the sum always stays inside the map.
3c101aa to
f17dd59
Compare
|
Please resolve conflict, marking it draft for now.. |
Stacked on #2615 (depends on its
PARSE_ELF_STARTextent checks andelf_sect_namehelper). Will be rebased after #2615 merges.Problem
FIND_LIBRARY_RUNPATH_FOR_BITS(insrc/install/dracut-install.c) walks the ELF.dynamicsection'sElf_Dynarray and pulls out the DT_RUNPATH / DT_RPATH strings to search for libraries. Unlike the sibling macrosRESOLVE_DEPS_NEEDED_FOR_BITSandRESOLVE_DEPS_DLOPEN_FOR_BITS, this walk validates onlye_shoff/e_shstrndx— the per-iteration pointers into the map are never bounds-checked. Additionally, in the interpreter/DT_SONAME/DT_NEEDEDcode paths the strings extracted with(char *)map + shdr[i].sh_offset + d->d_un.d_valare handed tostrlen/strdup/hashmap_get/expand_runpath/regexecwithout verifying they are NUL-terminated within the mapped file (a malformed ELF reading past the end of the map can hang on a never-terminatedstrcmp/strdup).Also found one off-by-boundary check in the interpreter path:
interpreter > (char *)map + src_lenaccepts the pointer at exactly one-past-end (should be>=).Fix
elf_map_string(map, src_len, offset): returns NULL if the offset is outside the map or the string there isn't NUL-terminated within the map. Used for the runpath string, interpreter string, DT_SONAME and DT_NEEDED strings.dynandd.Verification
Crafted 3 malformed ELF64 inputs exercising each new check; ran the real
dracut-install -lbinary built before/after, andvalgrindon the difference-maker:H7A2: DT_RPATH whose strtab pointer lands 4 bytes before map end, runpath not NUL-terminatedInvalid read of size 1→ SIGSEGVH7B2: PT_INTERP at exactlysrc_lenone-past-endH7C2: PT_INTERP in-map but non-terminated(The last two are safe in both versions only because no code path dereferences them — they're defensive/paranoia checks. The RUNPATH one is the reachable crash.)
Regression control:
-ldependency resolution of/bin/bash,/bin/ls, and/usr/bin/ssh— byte-identical 33-entry file list before vs after. H6 test set re-verified — the 6 malformed ELFs from #2615's verification table still all exit cleanly.