Skip to content

Commit

Permalink
Merge pull request #469 from brenoguim/breno.446
Browse files Browse the repository at this point in the history
Add one extra page to avoid overlapping with next page if its rounded…
  • Loading branch information
brenoguim committed Mar 11, 2023
2 parents ac212d0 + de3e1f5 commit 27cbc89
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/patchelf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,9 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsExecutable()

/* Calculate how many bytes are needed out of the additional pages. */
size_t extraSpace = neededSpace - startOffset;
unsigned int neededPages = roundUp(extraSpace, getPageSize()) / getPageSize();
// Always give one extra page to avoid colliding with segments that start at
// unaligned addresses and will be rounded down when loaded
unsigned int neededPages = 1 + roundUp(extraSpace, getPageSize()) / getPageSize();
debug("needed pages is %d\n", neededPages);
if (neededPages * getPageSize() > firstPage)
error("virtual address space underrun!");
Expand Down
4 changes: 3 additions & 1 deletion tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,16 @@ src_TESTS = \
print-execstack.sh \
modify-execstack.sh \
rename-dynamic-symbols.sh \
overlapping-segments-after-rounding.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 empty-note
EXTRA_DIST = no-rpath-prebuild $(src_TESTS) no-rpath-prebuild.sh invalid-elf endianness empty-note \
overlapping-segments-after-rounding

TESTS_ENVIRONMENT = PATCHELF_DEBUG=1 STRIP=$(STRIP) OBJDUMP=$(OBJDUMP) READELF=$(READELF) OBJCOPY=$(OBJCOPY)

Expand Down
Binary file added tests/overlapping-segments-after-rounding
Binary file not shown.
23 changes: 23 additions & 0 deletions tests/overlapping-segments-after-rounding.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#! /bin/sh -e

PATCHELF=$(readlink -f "../src/patchelf")
SCRATCH="scratch/$(basename "$0" .sh)"
READELF=${READELF:-readelf}

EXEC_NAME="overlapping-segments-after-rounding"

if test "$(uname -i)" = x86_64 && test "$(uname)" = Linux; then
rm -rf "${SCRATCH}"
mkdir -p "${SCRATCH}"

cp "${srcdir}/${EXEC_NAME}" "${SCRATCH}/"
cd "${SCRATCH}"

${PATCHELF} --force-rpath --remove-rpath --output modified1 "${EXEC_NAME}"

ldd modified1

${PATCHELF} --force-rpath --set-rpath "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" --output modified2 modified1

ldd modified2
fi

0 comments on commit 27cbc89

Please sign in to comment.