Skip to content

Commit 424d3f5

Browse files
committed
tests: add testcase for PT_PHDR VirtAddr corruption
A PT_PHDR corruption was previously reported and fixed in [1]: the issue was that the VirtAddr field of the PT_PHDR program header would get overwritten with the file offset of the program header table rather than the virtual address. A testcase for this was also added in [2]. However, the tescase is not included in the Makefile.am regression testsuite and also tries to run a x86_64 prebuilt binary unconditionally, which would not work on other architectures. To fix this, create a standalone testcase for the PT_PHDR VirtAddr field corruption and include it in Makefile.am. In order to reproduce [1], a binary with the following characteristics is needed: - the ELF file type must be ET_DYN - the ELF file must contain a PT_PHDR program header - the file offset and the VirtAddr field of the PT_PHDR program header must be different [1] #243 [2] 8f94e11 Signed-off-by: Ovidiu Panait <[email protected]>
1 parent 3cfd9a6 commit 424d3f5

File tree

5 files changed

+49
-15
lines changed

5 files changed

+49
-15
lines changed

tests/Makefile.am

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ src_TESTS = \
3939
no-dynamic-section.sh \
4040
args-from-file.sh \
4141
basic-flags.sh \
42-
set-empty-rpath.sh
42+
set-empty-rpath.sh \
43+
phdr-corruption.sh
4344

4445
build_TESTS = \
4546
$(no_rpath_arch_TESTS)
@@ -104,7 +105,8 @@ check_DATA = libbig-dynstr.debug
104105
# - without libtool, only archives (static libraries) can be built by automake
105106
# - with libtool, it is difficult to control options
106107
# - with libtool, it is not possible to compile convenience *dynamic* libraries :-(
107-
check_PROGRAMS += libfoo.so libfoo-scoped.so libbar.so libbar-scoped.so libsimple.so libbuildid.so libtoomanystrtab.so
108+
check_PROGRAMS += libfoo.so libfoo-scoped.so libbar.so libbar-scoped.so libsimple.so libbuildid.so libtoomanystrtab.so \
109+
phdr-corruption.so
108110

109111
libbuildid_so_SOURCES = simple.c
110112
libbuildid_so_LDFLAGS = $(LDFLAGS_sharedlib) -Wl,-build-id
@@ -138,3 +140,7 @@ no_rpath_CFLAGS =
138140
contiguous_note_sections_SOURCES = contiguous-note-sections.s contiguous-note-sections.ld
139141
contiguous_note_sections_LDFLAGS = -nostdlib -T contiguous-note-sections.ld
140142
contiguous_note_sections_CFLAGS = -pie
143+
144+
phdr_corruption_so_SOURCES = void.c phdr-corruption.ld
145+
phdr_corruption_so_LDFLAGS = -nostdlib -shared -Wl,-Tphdr-corruption.ld
146+
phdr_corruption_so_CFLAGS =

tests/PR243-reproducer.sh

Lines changed: 0 additions & 13 deletions
This file was deleted.

tests/phdr-corruption.ld

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
PHDRS
2+
{
3+
headers PT_PHDR PHDRS;
4+
text PT_LOAD FILEHDR PHDRS;
5+
interp PT_INTERP ;
6+
}
7+
8+
SECTIONS
9+
{
10+
. = SIZEOF_HEADERS;
11+
. = ALIGN(4);
12+
13+
. = . + 0x1000;
14+
.interp : { *(.interp) } :text :interp
15+
.text : { *(.text) } :text
16+
}

tests/phdr-corruption.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#! /bin/sh -e
2+
3+
PATCHELF="../src/patchelf"
4+
SONAME="phdr-corruption.so"
5+
SCRATCH="scratch/$(basename $0 .sh)"
6+
SCRATCH_SO="${SCRATCH}/${SONAME}"
7+
8+
rm -rf "${SCRATCH}"
9+
mkdir -p "${SCRATCH}"
10+
cp "${SONAME}" "${SCRATCH}"
11+
12+
"${PATCHELF}" --set-rpath "$(pwd)" "${SCRATCH_SO}"
13+
14+
# Check for PT_PHDR entry VirtAddr corruption
15+
readelfData=$(readelf -l "${SCRATCH_SO}" 2>&1)
16+
17+
if [ $(echo "$readelfData" | grep --count "PHDR") != 1 ]; then
18+
# Triggered if PHDR errors appear on stderr
19+
echo "ERROR: Unexpected number of occurences of PHDR in readelf results!"
20+
exit 1
21+
fi

tests/void.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
int main()
2+
{
3+
return 0;
4+
}

0 commit comments

Comments
 (0)