#!/bin/sh
# run.sh - reproduce DF-0787 (unbounded attribute walk in ntfs_loadntnode)
#
# This script runs the deterministic userspace harness (no root required).
# The live kernel-level reproduction is a root-only mount operation and is
# documented in VERDICT.md; the exact commands are:
#
#   vnconfig -c vn0 /root/ntfs_loop.img
#   mount_ntfs -o ro /dev/vn0 /mnt/ntfs    # PANICS on unpatched, EINVAL on patched
#
# Usage:  ./run.sh           # run the harness in all modes
#         ./run.sh live      # also attempt the live mount (must be root)
set -e
cd "$(dirname "$0")"

echo "===== DF-0787: unbounded attribute walk — userspace harness ====="
echo
echo "--- BUGGY walk (kernel behaviour on default GENERIC #0) ---"
for m in clean loop oob_attroff oob_reclen; do
    ./harness "$m"
done

echo
echo "--- WITH FIX applied (proposed patch behaviour) ---"
for m in clean loop oob_attroff oob_reclen; do
    ./harness "$m" apply_fix
done

if [ "$1" = "live" ]; then
    echo
    echo "===== LIVE kernel reproduction (requires root) ====="
    if [ "$(id -u)" -ne 0 ]; then
        echo "skipping live test (not root); run as root or use vm.sh run_root"
        exit 0
    fi
    for img in ntfs_loop ntfs_oob_a ntfs_oob_r; do
        vnconfig -u vn0 2>/dev/null || true
        vnconfig -c vn0 "/root/${img}.img" 2>/dev/null || cp "${img}.img" "/root/${img}.img" && vnconfig -c vn0 "/root/${img}.img"
        echo "--- ${img}.img ---"
        timeout 15 mount_ntfs -o ro /dev/vn0 /mnt/ntfs 2>&1
        echo "MOUNT_RC=$?"
        umount /mnt/ntfs 2>/dev/null || true
        vnconfig -u vn0 2>/dev/null || true
    done
fi
