Skip to content

Skip empty PT_NOTE sections #413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/patchelf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,14 @@ void ElfFile<ElfFileParamNames>::normalizeNoteSegments()
size_t start_off = rdi(phdr.p_offset);
size_t curr_off = start_off;
size_t end_off = start_off + rdi(phdr.p_filesz);

/* Binaries produced by older patchelf versions may contain empty PT_NOTE segments.
For backwards compatibility, if we find one we should ignore it. */
bool empty = std::none_of(shdrs.begin(), shdrs.end(),
[&](auto & shdr) { return rdi(shdr.sh_offset) >= start_off && rdi(shdr.sh_offset) < end_off; });
if (empty)
continue;

while (curr_off < end_off) {
/* Find a section that starts at the current offset. If we can't
find one, it means the SHT_NOTE sections weren't contiguous
Expand Down
5 changes: 3 additions & 2 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ src_TESTS = \
phdr-corruption.sh \
replace-needed.sh \
replace-add-needed.sh \
add-debug-tag.sh
add-debug-tag.sh \
empty-note.sh

build_TESTS = \
$(no_rpath_arch_TESTS)

TESTS = $(src_TESTS) $(build_TESTS)

EXTRA_DIST = no-rpath-prebuild $(src_TESTS) no-rpath-prebuild.sh invalid-elf endianness
EXTRA_DIST = no-rpath-prebuild $(src_TESTS) no-rpath-prebuild.sh invalid-elf endianness empty-note

TESTS_ENVIRONMENT = PATCHELF_DEBUG=1

Expand Down
Binary file added tests/empty-note
Binary file not shown.
12 changes: 12 additions & 0 deletions tests/empty-note.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#! /bin/sh -e

SCRATCH=scratch/$(basename $0 .sh)

rm -rf ${SCRATCH}
mkdir -p ${SCRATCH}
cp $(dirname $(readlink -f $0))/empty-note ${SCRATCH}/

# Running --set-interpreter on this binary should not produce the following
# error:
# patchelf: cannot normalize PT_NOTE segment: non-contiguous SHT_NOTE sections
../src/patchelf --set-interpreter ld-linux-x86-64.so.2 ${SCRATCH}/empty-note