From 860c04dbd6fd290c6b2856ac7d467570722be3a5 Mon Sep 17 00:00:00 2001 From: Breno Rodrigues Guimaraes Date: Fri, 17 Mar 2023 18:20:35 -0300 Subject: Add test --- tests/Makefile.am | 6 +++++- tests/shared-rpath.c | 2 ++ tests/shared-rpath.sh | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 tests/shared-rpath.c create mode 100755 tests/shared-rpath.sh diff --git a/tests/Makefile.am b/tests/Makefile.am index 0b648f8..5dba804 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -49,6 +49,7 @@ src_TESTS = \ modify-execstack.sh \ rename-dynamic-symbols.sh \ overlapping-segments-after-rounding.sh \ + shared-rpath.sh \ empty-note.sh build_TESTS = \ @@ -121,7 +122,7 @@ check_DATA = libbig-dynstr.debug # - with libtool, it is difficult to control options # - with libtool, it is not possible to compile convenience *dynamic* libraries :-( check_PROGRAMS += libfoo.so libfoo-scoped.so libbar.so libbar-scoped.so libsimple.so libsimple-execstack.so libbuildid.so libtoomanystrtab.so \ - phdr-corruption.so many-syms-main libmany-syms.so liboveralign.so + phdr-corruption.so many-syms-main libmany-syms.so liboveralign.so libshared-rpath.so libbuildid_so_SOURCES = simple.c libbuildid_so_LDFLAGS = $(LDFLAGS_sharedlib) -Wl,--build-id @@ -148,6 +149,9 @@ libsimple_so_LDFLAGS = $(LDFLAGS_sharedlib) -Wl,-z,noexecstack liboveralign_so_SOURCES = simple.c liboveralign_so_LDFLAGS = $(LDFLAGS_sharedlib) -Wl,-z,max-page-size=0x10000 +libshared_rpath_so_SOURCES = shared-rpath.c +libshared_rpath_so_LDFLAGS = $(LDFLAGS_sharedlib) -Wl,-rpath=a_symbol_name + libsimple_execstack_so_SOURCES = simple.c libsimple_execstack_so_LDFLAGS = $(LDFLAGS_sharedlib) -Wl,-z,execstack diff --git a/tests/shared-rpath.c b/tests/shared-rpath.c new file mode 100644 index 0000000..8a3ee7c --- /dev/null +++ b/tests/shared-rpath.c @@ -0,0 +1,2 @@ +int a_symbol_name; +int foo() { return a_symbol_name; } diff --git a/tests/shared-rpath.sh b/tests/shared-rpath.sh new file mode 100755 index 0000000..8e6e26f --- /dev/null +++ b/tests/shared-rpath.sh @@ -0,0 +1,51 @@ +#! /bin/sh -e + +PATCHELF=$(readlink -f "../src/patchelf") +SCRATCH="scratch/$(basename "$0" .sh)" +READELF=${READELF:-readelf} + +LIB_NAME="${PWD}/libshared-rpath.so" + +rm -rf "${SCRATCH}" +mkdir -p "${SCRATCH}" +cd "${SCRATCH}" + +has_x() { + strings "$1" | grep -c "XXXXXXXX" +} + +nm -D "${LIB_NAME}" | grep a_symbol_name +previous_cnt="$(strings "${LIB_NAME}" | grep -c a_symbol_name)" + +echo "#### Number of a_symbol_name strings in the library: $previous_cnt" + +echo "#### Rename the rpath to something larger than the original" +# Pathelf should detect that the rpath string is shared with the symbol name string and avoid +# tainting the string with Xs +"${PATCHELF}" --set-rpath a_very_big_rpath_that_is_larger_than_original --output liblarge-rpath.so "${LIB_NAME}" + +echo "#### Checking symbol is still there" +nm -D liblarge-rpath.so | grep a_symbol_name + +echo "#### Checking there are no Xs" +[ "$(has_x liblarge-rpath.so)" -eq 0 ] || exit 1 + +current_cnt="$(strings liblarge-rpath.so | grep -c a_symbol_name)" +echo "#### Number of a_symbol_name strings in the modified library: $current_cnt" +[ "$current_cnt" -eq "$previous_cnt" ] || exit 1 + +echo "#### Rename the rpath to something shorter than the original" +# Pathelf should detect that the rpath string is shared with the symbol name string and avoid +# overwriting the existing string +"${PATCHELF}" --set-rpath shrt_rpth --output libshort-rpath.so "${LIB_NAME}" + +echo "#### Checking symbol is still there" +nm -D libshort-rpath.so | grep a_symbol_name + +echo "#### Number of a_symbol_name strings in the modified library: $current_cnt" +current_cnt="$(strings libshort-rpath.so | grep -c a_symbol_name)" +[ "$current_cnt" -eq "$previous_cnt" ] || exit 1 + +echo "#### Now liblarge-rpath.so should have its own rpath, so it should be allowed to taint it" +"${PATCHELF}" --set-rpath a_very_big_rpath_that_is_larger_than_original__even_larger --output liblarge-rpath2.so liblarge-rpath.so +[ "$(has_x liblarge-rpath2.so)" -eq 1 ] || exit 1 -- cgit v1.2.1