#!/usr/bin/env bash set -euo pipefail if [[ $# != 1 ]]; then echo "usage: $0 SOURCE_ROOT" >&2 exit 2 fi source_root=$(realpath -e -- "$1") for command_name in cmake ldd patchelf readelf python3 strip; do command -v "$command_name" >/dev/null || { echo "missing test command: $command_name" >&2 exit 2 } done c_compiler_name=${CC:-cc} c_compiler_path=$(command -v -- "$c_compiler_name") || { echo "missing test C compiler: $c_compiler_name" >&2 exit 2 } c_compiler=$(realpath -e -- "$c_compiler_path") cxx_compiler_name=${CXX:-c++} cxx_compiler_path=$(command -v -- "$cxx_compiler_name") || { echo "missing test C++ compiler: $cxx_compiler_name" >&2 exit 2 } cxx_compiler=$(realpath -e -- "$cxx_compiler_path") temporary=$(mktemp -d "${TMPDIR:-/tmp}/echoion-runtime-deps.XXXXXX") trap 'rm -rf -- "$temporary"' EXIT configured_build="$temporary/configured-build" mkdir -p "$configured_build" printf 'CMAKE_CXX_COMPILER:FILEPATH=%s\n' "$cxx_compiler" \ >"$configured_build/CMakeCache.txt" queried_compiler=$(CXX=/bin/false \ "$source_root/scripts/configured-cxx-compiler.sh" "$configured_build") [[ $queried_compiler == "$cxx_compiler" ]] || { echo "runtime compiler query followed CXX instead of the configured cache" >&2 exit 1 } preferred_lib="$temporary/preferred-lib" other_lib="$temporary/other-lib" bundle="$temporary/bundle" resolver="$temporary/resolver" seed_lib="$temporary/seed-lib" mkdir -p \ "$preferred_lib" \ "$other_lib" \ "$resolver" \ "$seed_lib" \ "$bundle/bin" \ "$bundle/lib" \ "$bundle/service" \ "$bundle/share/echoion/web" \ "$bundle/share/echoion/bootstrap-plugins/fixture-shortener/1.0.0" cat >"$temporary/dependency.c" <<'C' int echoion_dependency(void) { return 42; } __attribute__((used, section(".echoion_variant"))) static const char echoion_variant[] = ECHOION_VARIANT; C cat >"$temporary/core.c" <<'C' extern int echoion_dependency(void); extern int echoion_compiler_runtime(void); __attribute__((used)) static const char echoion_forbidden_path[] = "/tmp/echoion-forbidden-build-path/source.cpp"; int echoion_core(void) { return echoion_dependency() + echoion_compiler_runtime(); } C cat >"$temporary/main.c" <<'C' int main(void) { return 0; } C cat >"$temporary/compiler-runtime.c" <<'C' int echoion_compiler_runtime(void) { return 7; } __attribute__((used, section(".echoion_compiler_variant"))) static const char echoion_compiler_variant[] = ECHOION_VARIANT; C cat >"$temporary/plugin.c" <<'C' extern int echoion_dependency(void); __attribute__((used)) static const char echoion_plugin_forbidden_path[] = "/tmp/echoion-plugin-forbidden-build-path/plugin.c"; int echoion_plugin(void) { return echoion_dependency(); } C for variant in preferred other; do case $variant in preferred) variant_lib=$preferred_lib ;; other) variant_lib=$other_lib ;; esac "$c_compiler" -g -fPIC -shared "$temporary/dependency.c" \ '-DECHOION_VARIANT="'"$variant"'-runtime"' \ -Wl,-soname,libechoion-dependency.so.1 \ -o "$variant_lib/libechoion-dependency.so.1.2.3" ln -s libechoion-dependency.so.1.2.3 \ "$variant_lib/libechoion-dependency.so.1" ln -s libechoion-dependency.so.1 \ "$variant_lib/libechoion-dependency.so" done for variant in seeded resolver; do case $variant in seeded) variant_lib=$seed_lib variant_version=100 ;; resolver) variant_lib=$preferred_lib variant_version=200 ;; esac "$c_compiler" -fPIC -shared "$temporary/compiler-runtime.c" \ '-DECHOION_VARIANT="'"$variant"'-compiler-runtime"' \ -Wl,-soname,libstdc++.so.6 \ -o "$variant_lib/libstdc++.so.6.0.$variant_version" ln -s "libstdc++.so.6.0.$variant_version" \ "$variant_lib/libstdc++.so.6" done resolver_core="$resolver/Echoion.ts" "$c_compiler" -g -fPIC -shared "$temporary/core.c" \ -L"$preferred_lib" -lechoion-dependency \ -Wl,-l:libstdc++.so.6 \ -Wl,-soname,Echoion.ts \ -Wl,-rpath,"$preferred_lib" \ -o "$resolver_core" install -m 0644 "$resolver_core" "$bundle/lib/Echoion.ts" patchelf --set-rpath "\$ORIGIN" "$bundle/lib/Echoion.ts" bundle_plugin="$bundle/share/echoion/bootstrap-plugins/fixture-shortener/1.0.0/fixture-shortener.so" "$c_compiler" -g -fPIC -shared "$temporary/plugin.c" \ -L"$preferred_lib" -lechoion-dependency \ -Wl,-rpath,"$preferred_lib" \ -o "$bundle_plugin" "$c_compiler" -g "$temporary/main.c" -o "$bundle/bin/echoiond" install -m 0755 "$bundle/bin/echoiond" "$bundle/bin/echoion-updater" printf '%s\n' '[Service]' >"$bundle/service/echoion.service.in" printf '%s\n' 'Echoion' \ >"$bundle/share/echoion/web/index.html" cmake \ "-DBUNDLE_ROOT=$bundle" \ "-DBUNDLE_RESOLVER_LIBRARIES=$resolver_core;$bundle_plugin" \ "-DBUNDLE_SEARCH_DIRECTORIES=$other_lib" \ "-DBUNDLE_SEED_FILES=$seed_lib/libstdc++.so.6" \ "-DPATCHELF_EXECUTABLE=$(command -v patchelf)" \ -P "$source_root/cmake/BundleRuntimeDependencies.cmake" plugin_runpath="\$ORIGIN/../../../../../../current/lib:\$ORIGIN/../../current/lib:\$ORIGIN/lib" patchelf --set-rpath "$plugin_runpath" "$bundle_plugin" python3 - "$source_root/scripts/validate-release-elf.py" <<'PY' import importlib.util from pathlib import Path import sys module_path = Path(sys.argv[1]) spec = importlib.util.spec_from_file_location("validate_release_elf", module_path) assert spec is not None and spec.loader is not None module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) fixture = """ Version needs section '.gnu.version_r' contains 2 entries: 000000: Version: 1 File: libm.so.6 Cnt: 1 0x0010: Name: GLIBC_9.99 Flags: none Version: 4 0x0020: Version: 1 File: libc.so.6 Cnt: 3 0x0030: Name: GLIBC_2.17 Flags: none Version: 3 0x0040: Name: GLIBC_2.36 Flags: none Version: 2 0x0050: Name: GLIBC_ABI_DT_RELR Flags: none Version: 1 """ assert module.required_glibc_versions(fixture) == {(2, 17), (2, 36), (9, 99)} assert module.parse_version("2.35") == (2, 35) assert module.is_system_dependency("libc.so.6") assert module.is_system_dependency("libm.so.6") assert not module.is_system_dependency("libc.so.evil") assert not module.is_system_dependency("libm.so.99") root = Path("/release") plugins = { root / "share/echoion/bootstrap-plugins/example/1.0.0/example.so" } assert module.is_first_party_payload(root / "bin/echoiond", root, plugins) assert module.is_first_party_payload(root / "lib/Echoion.ts", root, plugins) assert module.is_first_party_payload(root / "lib/libechoion-host.so.2.0.0", root, plugins) assert module.is_first_party_payload(root / "lib/libVoid_Plugin.so", root, plugins) assert module.is_first_party_payload(root / "lib/libVoidTeamspeak.so", root, plugins) assert module.is_first_party_payload(next(iter(plugins)), root, plugins) assert not module.is_first_party_payload(root / "lib/libacl.so.1.1.2301", root, plugins) assert not module.is_first_party_payload(root / "lib/libqjs.so.0.11.0", root, plugins) unknown_abi = """ Version needs section '.gnu.version_r' contains 1 entry: 000000: Version: 1 File: libc.so.6 Cnt: 1 0x0010: Name: GLIBC_ABI_FUTURE Flags: none Version: 1 """ try: module.required_glibc_versions(unknown_abi) except ValueError as error: assert "unknown GLIBC ABI requirement" in str(error) else: raise AssertionError("unknown GLIBC ABI requirement was accepted") PY evil_bundle="$temporary/evil-bundle" evil_build="$temporary/evil-build" cp -a -- "$bundle" "$evil_bundle" mkdir "$evil_build" cat >"$temporary/evil-dependency.c" <<'C' int echoion_evil_dependency(void) { return 13; } C cat >"$temporary/evil-user.c" <<'C' extern int echoion_evil_dependency(void); int echoion_evil_user(void) { return echoion_evil_dependency(); } C "$c_compiler" -fPIC -shared "$temporary/evil-dependency.c" \ -Wl,-soname,libc.so.evil -o "$evil_build/libc.so.evil" "$c_compiler" -fPIC -shared "$temporary/evil-user.c" \ -L"$evil_build" -Wl,-l:libc.so.evil -Wl,-rpath,"$evil_build" \ -o "$evil_build/libevil-user.so" rm "$evil_build/libc.so.evil" if cmake \ "-DBUNDLE_ROOT=$evil_bundle" \ "-DBUNDLE_RESOLVER_LIBRARIES=$evil_build/libevil-user.so" \ "-DBUNDLE_SEARCH_DIRECTORIES=$other_lib" \ "-DBUNDLE_SEED_FILES=$seed_lib/libstdc++.so.6" \ "-DPATCHELF_EXECUTABLE=$(command -v patchelf)" \ -P "$source_root/cmake/BundleRuntimeDependencies.cmake" \ >/dev/null 2>&1; then echo "fake glibc-like SONAME was treated as a system dependency" >&2 exit 1 fi report="$temporary/dependencies.txt" python3 "$source_root/scripts/validate-release-elf.py" \ --root "$bundle" --max-glibc 2.35 --report "$report" python3 "$source_root/scripts/validate-release-elf.py" \ --root "$bundle" --require-first-party-debug-info >/dev/null cp "$bundle/lib/Echoion.ts" "$temporary/Echoion.ts.debug" strip --strip-debug "$bundle/lib/Echoion.ts" if python3 "$source_root/scripts/validate-release-elf.py" \ --root "$bundle" --require-first-party-debug-info >/dev/null 2>&1; then echo "first-party ELF without debug information passed validation" >&2 exit 1 fi install -m 0644 "$temporary/Echoion.ts.debug" "$bundle/lib/Echoion.ts" if python3 "$source_root/scripts/validate-release-elf.py" \ --root "$bundle" \ --forbid-content-prefix /tmp/echoion-forbidden-build-path \ >/dev/null 2>&1; then echo "embedded build/source path unexpectedly passed validation" >&2 exit 1 fi grep -F $'NEEDED\tlib/Echoion.ts\tlibechoion-dependency.so.1\tbundled' "$report" grep -F $'NEEDED\tbin/echoiond\tlibc.so.6\tsystem' "$report" grep -F \ $'ELF\tshare/echoion/bootstrap-plugins/fixture-shortener/1.0.0/fixture-shortener.so\tRUNPATH=$ORIGIN/../../../../../../current/lib:$ORIGIN/../../current/lib:$ORIGIN/lib' \ "$report" grep -F $'NEEDED\tshare/echoion/bootstrap-plugins/fixture-shortener/1.0.0/fixture-shortener.so\tlibechoion-dependency.so.1\tbundled' "$report" installed="$temporary/installed" managed_plugin="$installed/state/plugins/.managed/fixture-shortener/versions/1.0.0/fixture-shortener.so" portable_plugin="$installed/state/plugins/fixture-shortener.so" mkdir -p "$installed/current/lib" "$(dirname -- "$managed_plugin")" \ "$(dirname -- "$portable_plugin")" cp -a "$bundle/lib/." "$installed/current/lib/" cp "$bundle_plugin" "$managed_plugin" cp "$bundle_plugin" "$portable_plugin" for installed_plugin in "$managed_plugin" "$portable_plugin"; do ldd_report="$temporary/$(basename -- "$(dirname -- "$installed_plugin")").ldd" env -u LD_LIBRARY_PATH ldd "$installed_plugin" >"$ldd_report" 2>&1 resolved_dependency=$(awk ' $1 == "libechoion-dependency.so.1" && $2 == "=>" { print $3 exit } ' "$ldd_report") [[ -n $resolved_dependency ]] [[ $(realpath -e -- "$resolved_dependency") == \ $(realpath -e -- "$installed/current/lib/libechoion-dependency.so.1") ]] if grep -Fq 'not found' "$ldd_report"; then echo "installed plugin did not resolve its bundled dependency closure" >&2 cat "$ldd_report" >&2 exit 1 fi done if python3 "$source_root/scripts/validate-release-elf.py" \ --root "$bundle" \ --forbid-content-prefix /tmp/echoion-plugin-forbidden-build-path \ >/dev/null 2>&1; then echo "embedded plugin build/source path unexpectedly passed validation" >&2 exit 1 fi [[ -L $bundle/lib/libechoion-dependency.so.1 ]] [[ $(readlink "$bundle/lib/libechoion-dependency.so.1") != /* ]] [[ -f $bundle/lib/libechoion-dependency.so.1.2.3 ]] readelf -p .echoion_variant \ "$bundle/lib/libechoion-dependency.so.1.2.3" | grep -F preferred-runtime if readelf -p .echoion_variant \ "$bundle/lib/libechoion-dependency.so.1.2.3" | grep -Fq other-runtime; then echo "search-directory runtime replaced the exact build resolver runtime" >&2 exit 1 fi readelf -p .echoion_compiler_variant \ "$bundle/lib/libstdc++.so.6.0.100" | grep -F seeded-compiler-runtime if find "$bundle/lib" -maxdepth 1 -name 'libstdc++.so.6.0.200' | grep -q .; then echo "resolver compiler runtime leaked beside the exact seed" >&2 exit 1 fi if find "$bundle/lib" -maxdepth 1 -name 'libc.so*' | grep -q .; then echo "glibc was unexpectedly bundled" >&2 exit 1 fi patchelf --set-rpath "\$ORIGIN" "$bundle_plugin" if python3 "$source_root/scripts/validate-release-elf.py" \ --root "$bundle" >/dev/null 2>&1; then echo "incorrect bootstrap plugin RUNPATH unexpectedly passed validation" >&2 exit 1 fi patchelf --set-rpath "$plugin_runpath" "$bundle_plugin" patchelf --set-rpath /tmp/unsafe-build-path "$bundle/lib/Echoion.ts" if python3 "$source_root/scripts/validate-release-elf.py" \ --root "$bundle" >/dev/null 2>&1; then echo "absolute Core RUNPATH unexpectedly passed validation" >&2 exit 1 fi patchelf --set-rpath "\$ORIGIN" "$bundle/lib/Echoion.ts" python3 - "$bundle/lib/Echoion.ts" <<'PY' import sys with open(sys.argv[1], "r+b") as stream: stream.seek(18) stream.write(b"\xb7\x00") # EM_AARCH64 in a little-endian ELF header. PY if python3 "$source_root/scripts/validate-release-elf.py" \ --root "$bundle" >/dev/null 2>&1; then echo "non-x86-64 Core unexpectedly passed validation" >&2 exit 1 fi echo "runtime dependency bundling regressions passed"