Skip to content

Commit de3e1f5

Browse files
committed
Add one extra page to avoid overlapping with next page if its rounded down
1 parent 69a7ae5 commit de3e1f5

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

src/patchelf.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,9 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsExecutable()
985985

986986
/* Calculate how many bytes are needed out of the additional pages. */
987987
size_t extraSpace = neededSpace - startOffset;
988-
unsigned int neededPages = roundUp(extraSpace, getPageSize()) / getPageSize();
988+
// Always give one extra page to avoid colliding with segments that start at
989+
// unaligned addresses and will be rounded down when loaded
990+
unsigned int neededPages = 1 + roundUp(extraSpace, getPageSize()) / getPageSize();
989991
debug("needed pages is %d\n", neededPages);
990992
if (neededPages * getPageSize() > firstPage)
991993
error("virtual address space underrun!");

tests/Makefile.am

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,16 @@ src_TESTS = \
4747
print-execstack.sh \
4848
modify-execstack.sh \
4949
rename-dynamic-symbols.sh \
50+
overlapping-segments-after-rounding.sh \
5051
empty-note.sh
5152

5253
build_TESTS = \
5354
$(no_rpath_arch_TESTS)
5455

5556
TESTS = $(src_TESTS) $(build_TESTS)
5657

57-
EXTRA_DIST = no-rpath-prebuild $(src_TESTS) no-rpath-prebuild.sh invalid-elf endianness empty-note
58+
EXTRA_DIST = no-rpath-prebuild $(src_TESTS) no-rpath-prebuild.sh invalid-elf endianness empty-note \
59+
overlapping-segments-after-rounding
5860

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

223 KB
Binary file not shown.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#! /bin/sh -e
2+
3+
PATCHELF=$(readlink -f "../src/patchelf")
4+
SCRATCH="scratch/$(basename "$0" .sh)"
5+
READELF=${READELF:-readelf}
6+
7+
EXEC_NAME="overlapping-segments-after-rounding"
8+
9+
if test "$(uname -i)" = x86_64 && test "$(uname)" = Linux; then
10+
rm -rf "${SCRATCH}"
11+
mkdir -p "${SCRATCH}"
12+
13+
cp "${srcdir}/${EXEC_NAME}" "${SCRATCH}/"
14+
cd "${SCRATCH}"
15+
16+
${PATCHELF} --force-rpath --remove-rpath --output modified1 "${EXEC_NAME}"
17+
18+
ldd modified1
19+
20+
${PATCHELF} --force-rpath --set-rpath "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" --output modified2 modified1
21+
22+
ldd modified2
23+
fi

0 commit comments

Comments
 (0)