Exploit database

GH-bbd3f8bebf34e2d068df611f4434c88a

akamai/cve-2026-98432-validation-mitigation

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2026-98432

33 sources

Preferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source

Repository files3
akamai/cve-2026-98432-validation-mitigationRevision e7cafaf23c8f

Files from this repository share one source choice. Supporting files may cover other vulnerabilities.

cve-2026-98432.sh

Shell · 24,819 bytes
#!/usr/bin/env bash
# cve-2026-98432.sh -- check, mitigate and roll back for CVE-2026-98432
# (Linux kernel IPv4 TCP use-after-free: ireq_opt/inet_opt freed without RCU grace period).
#
# The default mode is READ-ONLY and never triggers the bug. Mitigation modes change system
# state only when explicitly requested and record what they changed so it can be rolled back.
#
# Usage:
#   ./cve-2026-98432.sh [--json] [--kernel-version X.Y.Z]        # read-only check
#   sudo ./cve-2026-98432.sh --mitigate [--disable-mptcp] [--persist] [--ignore-netlabel] [--dry-run]
#   sudo ./cve-2026-98432.sh --rollback [--dry-run]
#
#   --json              machine-readable output (for before/after diffing)
#   --kernel-version V  evaluate the version logic against V instead of `uname -r`
#   --mitigate          apply interim mitigations (NOT a fix; update the kernel):
#                         * net.ipv4.tcp_migrate_req=0   (closes the request-migration variant)
#                         * drop inbound IPv4 TCP packets that carry IP options (IHL > 5) at the
#                           PREROUTING hook, raw priority: covers INPUT and FORWARD (containers),
#                           runs before conntrack, and lives in its own nftables table / the raw
#                           iptables chain so firewalld, ufw, Docker and kube-proxy never touch it.
#   --disable-mptcp     additionally set net.mptcp.enabled=0 (breaks apps that need MPTCP)
#   --persist           /etc/sysctl.d + include in /etc/nftables.conf (nft) + systemd oneshot
#   --ignore-netlabel   install the drop rule even if NetLabel/CIPSO labelled networking is
#                       configured (CIPSO is a TCP-carrying IP option; the rule WILL break it)
#   --rollback          undo everything --mitigate changed, restoring recorded sysctl values
#   --dry-run           print the commands instead of running them
#
# Exit codes: 0 PATCHED/NOT_AFFECTED, 1 VULNERABLE/LIKELY_VULNERABLE, 2 usage error,
#   3 UNKNOWN, 4 --mitigate or --rollback did not complete (details on stderr).
#
# Upstream fix: e3f33b0a1d89d738e2b913dab519226eaf76e15b
#   "ipv4: free inet_opt and ireq_opt after an RCU grace period" (mainline 7.3-rc7)
# Fixed stable releases (as published 2026-10): 5.10.272 5.15.223 6.1.190 6.6.159
#   6.12.113 6.18.56 7.2.10 7.3. Vendor kernels backport; version compare does NOT apply
#   to them, only the package changelog does.

set -u
CVE="CVE-2026-98432"
TAG="cve_2026_98432"
FIX_COMMIT="e3f33b0a1d89d738e2b913dab519226eaf76e15b"
STATE_DIR="/var/lib/$TAG"
STATE_FILE="$STATE_DIR/state.env"
SYSCTL_FILE="/etc/sysctl.d/99-$TAG.conf"
NFT_FILE="/etc/$TAG.nft"
NFT_CONF="/etc/nftables.conf"
UNIT_FILE="/etc/systemd/system/$TAG-mitigation.service"
U32_MATCH="0&0x0F000000=0x06000000:0x0F000000"   # IHL nibble in range 6..15 => IP options present
IPT_RULE=(-p tcp -m u32 --u32 "$U32_MATCH" -m comment --comment "$TAG" -j DROP)

JSON=0; KVER_OVERRIDE=""; MITIGATE=0; DISABLE_MPTCP=0; PERSIST=0; ROLLBACK=0; DRY=0; IGNORE_NETLABEL=0
while [ $# -gt 0 ]; do
  case "$1" in
    --json) JSON=1 ;;
    --kernel-version) KVER_OVERRIDE="${2:-}"; shift ;;
    --mitigate) MITIGATE=1 ;;
    --disable-mptcp) DISABLE_MPTCP=1 ;;
    --persist) PERSIST=1 ;;
    --ignore-netlabel) IGNORE_NETLABEL=1 ;;
    --rollback) ROLLBACK=1 ;;
    --dry-run) DRY=1 ;;
    -h|--help) sed -n '2,/^$/p' "$0"; exit 0 ;;
    *) echo "unknown arg: $1" >&2; exit 2 ;;
  esac; shift
done
if [ "$MITIGATE" = 1 ] && [ "$ROLLBACK" = 1 ]; then echo "--mitigate and --rollback are exclusive" >&2; exit 2; fi

IS_LINUX=0; [ -z "$KVER_OVERRIDE" ] && [ "$(uname -s 2>/dev/null)" = "Linux" ] && IS_LINUX=1
log() { [ "$JSON" = 1 ] || echo "$*"; }
warn() { echo "!! $*" >&2; }
FAILED=0   # set when a --mitigate / --rollback step fails; turns the exit code into 4
fail() { warn "$*"; FAILED=1; }
run() { if [ "$DRY" = 1 ]; then echo "  [dry-run] $*"; else "$@"; fi; }
write_file() { if [ "$DRY" = 1 ]; then echo "  [dry-run] write $1:"; sed 's/^/      /'; else mkdir -p "$(dirname "$1")" && cat > "$1"; fi; }
need_root() { if [ "$DRY" = 0 ] && [ "$(id -u)" != 0 ]; then echo "$1 requires root (or use --dry-run)" >&2; exit 2; fi; }

# ---- 1. upstream version logic -------------------------------------------------
fixed_for_branch() {
  case "$1" in
    5.10) echo 272 ;; 5.15) echo 223 ;; 6.1) echo 190 ;; 6.6) echo 159 ;;
    6.12) echo 113 ;; 6.18) echo 56 ;; 7.2) echo 10 ;; 7.3) echo 0 ;; *) echo "" ;;
  esac
}
EOL_AFFECTED="4.4 4.9 4.14 4.19 5.4"
upstream_verdict() {
  local v="$1" base maj min pat branch fixed
  base="${v%%-*}"; base="${base%%+*}"
  IFS=. read -r maj min pat _ <<<"$base"
  [[ "$maj" =~ ^[0-9]+$ && "$min" =~ ^[0-9]+$ ]] || { echo UNKNOWN_BRANCH; return; }
  pat="${pat:-0}"; [[ "$pat" =~ ^[0-9]+$ ]] || pat=0
  branch="$maj.$min"
  if [[ "$v" =~ (\.el[0-9]|\.fc[0-9]|\.amzn|-generic|-aws|-azure|-gcp|-oracle|-lowlatency|-cloud-amd64|-amd64|-arm64|-default|-pve|-microsoft|\.x86_64$|\.aarch64$) ]]; then
    echo VENDOR_KERNEL; return
  fi
  if (( maj < 4 || (maj == 4 && min < 4) )); then echo NOT_AFFECTED; return; fi
  if [[ " $EOL_AFFECTED " == *" $branch "* ]]; then echo VULNERABLE_EOL; return; fi
  fixed=$(fixed_for_branch "$branch")
  if [[ -n "$fixed" ]]; then
    if (( pat >= fixed )); then
      [[ "$branch" == "7.3" && "$v" == *-rc* ]] && { echo UNKNOWN_BRANCH; return; }
      echo PATCHED
    else echo VULNERABLE; fi
    return
  fi
  echo UNKNOWN_BRANCH
}

# ---- 2. environment probes (read-only) -------------------------------------------
sysctl_val() { sysctl -n "$1" 2>/dev/null || cat "/proc/sys/${1//./\/}" 2>/dev/null || echo "n/a"; }

# echo "nftables" | "iptables" | "iptables-legacy-location" | "no"
fw_rule_present() {
  # the table alone is not enough: an empty or flushed table drops nothing
  if command -v nft >/dev/null 2>&1 && nft list table inet "$TAG" 2>/dev/null | grep -qE 'hdrlength.* drop'; then
    echo "nftables"; return
  fi
  if command -v iptables >/dev/null 2>&1; then
    if iptables -t raw -S PREROUTING 2>/dev/null | grep -q -- "--comment $TAG"; then echo "iptables"; return; fi
    if iptables -S INPUT 2>/dev/null | grep -q -- "--comment $TAG"; then echo "iptables-legacy-location"; return; fi
  fi
  echo "no"
}
# packets dropped by our rule so far (for correlating with remote-probe.py --repeat N)
fw_drop_count() {
  case "$1" in
    nftables) nft list table inet "$TAG" 2>/dev/null | sed -nE 's/.*counter packets ([0-9]+).*/\1/p' | head -1 ;;
    iptables) iptables -t raw -L PREROUTING -v -n -x 2>/dev/null | awk -v t="$TAG" '$0 ~ t {print $1; exit}' ;;
    iptables-legacy-location) iptables -L INPUT -v -n -x 2>/dev/null | awk -v t="$TAG" '$0 ~ t {print $1; exit}' ;;
    *) echo "n/a" ;;
  esac
}
# NetLabel/CIPSO labelled networking: CIPSO is an IP option carried on TCP; the drop rule breaks it.
netlabel_in_use() {
  local hit=""
  if command -v netlabelctl >/dev/null 2>&1; then
    netlabelctl cipsov4 list 2>/dev/null | grep -q . && hit="cipsov4-doi"
    netlabelctl map list 2>/dev/null | grep -qi cipso && hit="${hit:+$hit,}map"
  fi
  [ -r /etc/netlabel.rules ] && grep -qiE '^\s*(cipsov4|map .*cipso)' /etc/netlabel.rules 2>/dev/null && hit="${hit:+$hit,}netlabel.rules"
  echo "${hit:-no}"
}
# datapaths that bypass the host netfilter hooks; the rule cannot protect traffic on them
coverage_gaps() {
  local gaps=""
  if command -v ip >/dev/null 2>&1; then
    ip -o link show 2>/dev/null | grep -qE 'cilium_(host|net|vxlan)' && gaps="cilium"
    ip -d -o link show 2>/dev/null | grep -qE '\b(macvlan|ipvlan)\b' && gaps="${gaps:+$gaps,}macvlan/ipvlan"
  fi
  if command -v bpftool >/dev/null 2>&1; then
    bpftool net 2>/dev/null | grep -qE '^\s*\S+\([0-9]+\) (generic|driver|offload) id' && gaps="${gaps:+$gaps,}xdp"
  fi
  echo "${gaps:-none}"
}
firewall_manager() {
  local m=""
  if command -v systemctl >/dev/null 2>&1; then
    systemctl is-active -q firewalld 2>/dev/null && m="firewalld"
    systemctl is-active -q ufw 2>/dev/null && m="${m:+$m,}ufw"
    systemctl is-active -q nftables 2>/dev/null && m="${m:+$m,}nftables.service"
  fi
  pgrep -x kube-proxy >/dev/null 2>&1 && m="${m:+$m,}kube-proxy"
  pgrep -x dockerd >/dev/null 2>&1 && m="${m:+$m,}docker"
  echo "${m:-none}"
}

collect() {
  KVER="${KVER_OVERRIDE:-$(uname -r 2>/dev/null || echo unknown)}"
  OS_PRETTY="unknown"
  if [ "$IS_LINUX" = 1 ] && [ -r /etc/os-release ]; then OS_PRETTY=$(. /etc/os-release; echo "${PRETTY_NAME:-unknown}"); fi
  UPSTREAM=$(upstream_verdict "$KVER")

  local ss_out
  PKG=""; PKG_VER=""; CHANGELOG_HIT="not_checked"
  if [ "$IS_LINUX" = 1 ]; then
    if command -v dpkg-query >/dev/null 2>&1; then
      PKG="linux-image-$KVER"
      PKG_VER=$(dpkg-query -W -f='${Version}' "$PKG" 2>/dev/null || true)
      CL="/usr/share/doc/$PKG/changelog.Debian.gz"
      if [ -r "$CL" ]; then
        if zgrep -q "$CVE" "$CL" 2>/dev/null; then CHANGELOG_HIT="yes"; else CHANGELOG_HIT="no"; fi
      else CHANGELOG_HIT="unavailable"; fi
    elif command -v rpm >/dev/null 2>&1; then
      for cand in kernel-core kernel kernel-default kernel-uek; do
        if rpm -q "$cand-$KVER" >/dev/null 2>&1; then PKG="$cand-$KVER"; break; fi
      done
      if [ -n "$PKG" ]; then
        PKG_VER=$(rpm -q --qf '%{VERSION}-%{RELEASE}' "$PKG" 2>/dev/null || true)
        if rpm -q --changelog "$PKG" 2>/dev/null | grep -q "$CVE"; then CHANGELOG_HIT="yes"; else CHANGELOG_HIT="no"; fi
      else CHANGELOG_HIT="unavailable"; fi
    fi
  fi

  MPTCP="n/a"; MIGRATE="n/a"; LISTENERS="n/a"; MPTCP_LISTENERS="n/a"; FW="n/a"; DROPS="n/a"
  NETLABEL="n/a"; GAPS="n/a"; FWMGR="n/a"
  if [ "$IS_LINUX" = 1 ]; then
    MPTCP=$(sysctl_val net.mptcp.enabled)
    MIGRATE=$(sysctl_val net.ipv4.tcp_migrate_req)
    if command -v ss >/dev/null 2>&1; then
      # an ss that rejects an option (e.g. -M on Ubuntu 18.04) exits non-zero: report n/a, not 0
      if ss_out=$(ss -lnt4H 2>/dev/null); then LISTENERS=$(printf '%s' "$ss_out" | grep -c .); fi
      if ss_out=$(ss -lnMH 2>/dev/null); then MPTCP_LISTENERS=$(printf '%s' "$ss_out" | grep -c .); fi
    fi
    FW=$(fw_rule_present); DROPS=$(fw_drop_count "$FW"); DROPS="${DROPS:-0}"
    NETLABEL=$(netlabel_in_use); GAPS=$(coverage_gaps); FWMGR=$(firewall_manager)
  fi

  if   [ "$CHANGELOG_HIT" = "yes" ]; then VERDICT="PATCHED"; REASON="package changelog references $CVE"
  elif [ "$UPSTREAM" = "PATCHED" ]; then VERDICT="PATCHED"; REASON="upstream kernel $KVER is at/after the fixed stable release"
  elif [ "$UPSTREAM" = "NOT_AFFECTED" ]; then VERDICT="NOT_AFFECTED"; REASON="kernel predates the vulnerable code (4.4)"
  elif [ "$UPSTREAM" = "VULNERABLE" ]; then VERDICT="VULNERABLE"; REASON="upstream kernel $KVER is below the fixed stable release"
  elif [ "$UPSTREAM" = "VULNERABLE_EOL" ]; then VERDICT="VULNERABLE"; REASON="upstream branch is EOL and received no fix"
  elif [ "$UPSTREAM" = "VENDOR_KERNEL" ] && [ "$CHANGELOG_HIT" = "no" ]; then VERDICT="LIKELY_VULNERABLE"; REASON="vendor kernel; installed package changelog does not mention $CVE"
  else VERDICT="UNKNOWN"; REASON="could not determine patch state (upstream=$UPSTREAM, changelog=$CHANGELOG_HIT); check vendor advisory"
  fi

  MIT="n/a"; COVERAGE="n/a"
  if [ "$IS_LINUX" = 1 ]; then
    local n=0
    # tcp_migrate_req does not exist before 5.14, and neither does the migration variant
    { [ "$MIGRATE" = "0" ] || [ "$MIGRATE" = "n/a" ]; } && n=$((n+1))
    [ "$FW" != "no" ] && n=$((n+1))
    case $n in 2) MIT="full";; 1) MIT="partial";; *) MIT="none";; esac
    [ "$MPTCP" = "0" ] && MIT="$MIT+mptcp_off"
    [ "$FW" = "iptables-legacy-location" ] && MIT="$MIT (rule in filter/INPUT: no FORWARD coverage, re-apply to move it)"
    if [ "$FW" = "no" ]; then COVERAGE="n/a"
    elif [ "$GAPS" = "none" ]; then COVERAGE="complete"
    else COVERAGE="incomplete: $GAPS bypasses host netfilter"; fi
  fi
  EXPOSURE="reduced"
  [ "$MPTCP" = "1" ] && EXPOSURE="elevated (MPTCP enabled)"
  [ "$MIGRATE" = "1" ] && EXPOSURE="elevated (tcp_migrate_req=1)"
  [ "$MPTCP" = "1" ] && [ "$MIGRATE" = "1" ] && EXPOSURE="elevated (MPTCP enabled, tcp_migrate_req=1)"
  [ "$FW" != "no" ] && [ "$FW" != "n/a" ] && EXPOSURE="$EXPOSURE; IP-options drop active ($FW)"
}

report() {
  local TS; TS=$(date -u +%Y-%m-%dT%H:%M:%SZ)
  if [ "$JSON" = 1 ]; then
    printf '{"cve":"%s","timestamp":"%s","host":"%s","os":"%s","kernel":"%s","package":"%s","package_version":"%s","changelog_mentions_cve":"%s","upstream_version_verdict":"%s","mptcp_enabled":"%s","tcp_migrate_req":"%s","ip_options_drop_rule":"%s","drop_counter":"%s","mitigation":"%s","mitigation_coverage":"%s","netlabel_cipso":"%s","firewall_managers":"%s","ipv4_tcp_listeners":"%s","mptcp_listeners":"%s","exposure":"%s","verdict":"%s","reason":"%s","fix_commit":"%s"}\n' \
      "$CVE" "$TS" "$(hostname 2>/dev/null)" "$OS_PRETTY" "$KVER" "$PKG" "$PKG_VER" "$CHANGELOG_HIT" "$UPSTREAM" "$MPTCP" "$MIGRATE" "$FW" "$DROPS" "$MIT" "$COVERAGE" "$NETLABEL" "$FWMGR" "$LISTENERS" "$MPTCP_LISTENERS" "$EXPOSURE" "$VERDICT" "$REASON" "$FIX_COMMIT"
  else
    cat <<OUT
== $CVE check  ($TS) ==
Host / OS            : $(hostname 2>/dev/null) / $OS_PRETTY
Running kernel       : $KVER
Kernel package       : ${PKG:-n/a} ${PKG_VER:+($PKG_VER)}
Changelog has CVE    : $CHANGELOG_HIT
Upstream version     : $UPSTREAM
--- exposure factors (read-only) ---
net.mptcp.enabled    : $MPTCP
tcp_migrate_req      : $MIGRATE
IP-options drop rule : $FW  (dropped so far: $DROPS)
Interim mitigation   : $MIT
Mitigation coverage  : $COVERAGE
NetLabel/CIPSO       : $NETLABEL
Firewall managers    : $FWMGR
IPv4 TCP listeners   : $LISTENERS
MPTCP listeners      : $MPTCP_LISTENERS
Exposure             : $EXPOSURE
--- verdict ---
$VERDICT  -- $REASON
Fix commit           : $FIX_COMMIT
OUT
  fi
}

# ---- 3. mitigation ---------------------------------------------------------------
nft_rules_text() {
  # idempotent: declare, flush, redefine. Safe under `nft -f` and as an include in nftables.conf.
  cat <<N
table inet $TAG
flush table inet $TAG
table inet $TAG {
  chain prerouting {
    type filter hook prerouting priority raw; policy accept;
    ip hdrlength > 5 meta l4proto tcp counter drop comment "$CVE interim mitigation"
  }
}
N
}
# shell command for the persistence unit (ExecStart=/bin/sh -c '...'). Every rule argument is
# double-quoted: the u32 match contains '&', which sh would otherwise treat as a background operator.
ipt_insert_cmd() {
  local ipt rule
  ipt=$(command -v iptables)
  rule=$(printf ' "%s"' "${IPT_RULE[@]}")
  echo "$ipt -t raw -C PREROUTING$rule 2>/dev/null || $ipt -t raw -I PREROUTING 1$rule"
}

apply_mitigation() {
  need_root "--mitigate"
  [ "$IS_LINUX" = 1 ] || { echo "--mitigate only works on Linux" >&2; exit 2; }
  if [ -f "$STATE_FILE" ] && [ "$DRY" = 0 ]; then
    echo "mitigation state already recorded at $STATE_FILE; run --rollback first to re-apply" >&2; exit 2
  fi
  log ">> applying interim mitigations for $CVE (this is not a fix; update the kernel)"

  local prev_migrate prev_mptcp nl gaps mgr ipt_err
  prev_migrate=$(sysctl_val net.ipv4.tcp_migrate_req); prev_mptcp=$(sysctl_val net.mptcp.enabled)
  nl=$(netlabel_in_use); gaps=$(coverage_gaps); mgr=$(firewall_manager)

  # --- pre-flight guards
  local install_fw=1
  if [ "$nl" != "no" ]; then
    if [ "$IGNORE_NETLABEL" = 1 ]; then
      warn "NetLabel/CIPSO configured ($nl) but --ignore-netlabel given: labelled TCP traffic WILL be dropped"
    else
      warn "NetLabel/CIPSO labelled networking is configured ($nl). CIPSO is an IPv4 option on TCP;"
      warn "the drop rule would break it. Applying sysctl mitigations only. Use --ignore-netlabel to override."
      install_fw=0
    fi
  fi
  [ "$gaps" != "none" ] && warn "coverage gap: $gaps bypasses host netfilter; traffic on that path is NOT protected by the drop rule"
  [ "$mgr" != "none" ] && log ">> firewall managers active: $mgr (rule goes in its own table/chain; they do not manage it)"
  # a previous version of this script put the rule in filter/INPUT; move it
  if [ "$(fw_rule_present)" = "iptables-legacy-location" ]; then
    log ">> removing old filter/INPUT rule from a previous run"
    run iptables -D INPUT "${IPT_RULE[@]}" 2>/dev/null || true
  fi

  write_file "$STATE_FILE" <<S
PREV_TCP_MIGRATE_REQ=$prev_migrate
PREV_MPTCP_ENABLED=$prev_mptcp
MPTCP_DISABLED_BY_US=$DISABLE_MPTCP
PERSISTED=$PERSIST
FW_BACKEND=none
FW_CHAIN=raw-prerouting
NFT_INCLUDE_ADDED=0
APPLIED_AT=$(date -u +%Y-%m-%dT%H:%M:%SZ)
S
  if [ "$DRY" = 0 ] && [ ! -f "$STATE_FILE" ]; then echo "could not write $STATE_FILE; nothing changed" >&2; exit 4; fi
  set_state() { [ "$DRY" = 0 ] && sed -i "s/^$1=.*/$1=$2/" "$STATE_FILE"; }

  # (a) sysctls, only those the running kernel has (tcp_migrate_req appeared in 5.14)
  if [ "$prev_migrate" != "n/a" ]; then
    log ">> net.ipv4.tcp_migrate_req: $prev_migrate -> 0"
    run sysctl -q -w net.ipv4.tcp_migrate_req=0 || fail "could not set net.ipv4.tcp_migrate_req=0"
  else
    log ">> net.ipv4.tcp_migrate_req: not present on this kernel (no migration variant), skipped"
  fi
  if [ "$DISABLE_MPTCP" = 1 ]; then
    if [ "$prev_mptcp" != "n/a" ]; then
      log ">> net.mptcp.enabled: $prev_mptcp -> 0"
      run sysctl -q -w net.mptcp.enabled=0 || fail "could not set net.mptcp.enabled=0"
    else
      log ">> net.mptcp.enabled: not present on this kernel, skipped"
    fi
  fi

  # (b) drop inbound IPv4 TCP carrying IP options, at prerouting / raw priority
  local backend="none"
  if [ "$install_fw" = 1 ]; then
    if command -v nft >/dev/null 2>&1; then
      log ">> nftables: own table 'inet $TAG', hook prerouting priority raw: drop ip hdrlength > 5 for tcp"
      nft_rules_text | write_file "$NFT_FILE"
      if run nft -f "$NFT_FILE"; then backend="nftables"; else fail "nft -f failed; drop rule NOT installed"; fi
    elif command -v iptables >/dev/null 2>&1; then
      log ">> iptables: raw/PREROUTING -p tcp -m u32 (IHL > 5) -j DROP"
      run modprobe xt_u32 2>/dev/null || true
      if [ "$DRY" = 1 ]; then
        run iptables -t raw -I PREROUTING 1 "${IPT_RULE[@]}"; backend="iptables"
      elif ipt_err=$(iptables -t raw -I PREROUTING 1 "${IPT_RULE[@]}" 2>&1); then
        backend="iptables"
      else
        fail "iptables insert failed: $(printf '%s' "$ipt_err" | tr '\n' ' ')"
        warn "most likely the xt_u32 kernel module is missing (install kernel-modules-extra / linux-modules-extra) or use nftables"
      fi
    else
      fail "neither nft nor iptables found; IP-options drop NOT applied"
    fi
  fi
  set_state FW_BACKEND "$backend"
  if [ "$DRY" = 0 ] && [ "$backend" != "none" ] && [ "$(fw_rule_present)" != "$backend" ]; then
    fail "drop rule not found after installing it with $backend"
  fi

  # (c) persistence
  if [ "$PERSIST" = 1 ]; then
    if [ "$prev_migrate" != "n/a" ] || { [ "$DISABLE_MPTCP" = 1 ] && [ "$prev_mptcp" != "n/a" ]; }; then
      log ">> persisting: $SYSCTL_FILE"
      { echo "# $CVE interim mitigation (remove with: cve-2026-98432.sh --rollback)"
        [ "$prev_migrate" != "n/a" ] && echo "net.ipv4.tcp_migrate_req = 0"
        [ "$DISABLE_MPTCP" = 1 ] && [ "$prev_mptcp" != "n/a" ] && echo "net.mptcp.enabled = 0"
      } | write_file "$SYSCTL_FILE" || fail "could not write $SYSCTL_FILE"
    else
      log ">> no supported sysctl to persist; $SYSCTL_FILE not written"
    fi
    if [ "$backend" = "nftables" ] && [ -f "$NFT_CONF" ] && ! grep -q "$NFT_FILE" "$NFT_CONF" 2>/dev/null; then
      # nftables.service typically runs `flush ruleset` then loads this file: include us so we come back
      log ">> persisting: include \"$NFT_FILE\" appended to $NFT_CONF"
      if [ "$DRY" = 1 ]; then echo "  [dry-run] append include line to $NFT_CONF"; set_state NFT_INCLUDE_ADDED 1
      # no blank separator line, so --rollback's two-line strip restores the file exactly
      elif { [ -z "$(tail -c1 "$NFT_CONF")" ] || echo; } >> "$NFT_CONF" &&
           printf '# %s interim mitigation (removed by --rollback)\ninclude "%s"\n' "$CVE" "$NFT_FILE" >> "$NFT_CONF"; then
        set_state NFT_INCLUDE_ADDED 1
      else
        fail "could not append the include line to $NFT_CONF"
      fi
    fi
    if [ "$backend" != "none" ] && command -v systemctl >/dev/null 2>&1; then
      local exec after="network-pre.target"
      if [ "$backend" = "nftables" ]; then exec="$(command -v nft) -f $NFT_FILE"; after="$after nftables.service firewalld.service"
      else exec="/bin/sh -c '$(ipt_insert_cmd)'"; after="$after netfilter-persistent.service firewalld.service ufw.service"; fi
      log ">> persisting: $UNIT_FILE"
      write_file "$UNIT_FILE" <<U || fail "could not write $UNIT_FILE"
[Unit]
Description=$CVE interim mitigation (drop inbound IPv4 TCP with IP options)
After=$after
Wants=network-pre.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=$exec

[Install]
WantedBy=multi-user.target
U
      run systemctl daemon-reload 2>/dev/null || warn "systemctl daemon-reload failed (no systemd?)"
      run systemctl enable -q "$TAG-mitigation.service" 2>/dev/null || fail "could not enable unit; firewall rule will not persist across reboot"
    fi
  fi
  if [ "$FAILED" = 1 ]; then
    warn "mitigation incomplete; state kept in $STATE_FILE so --rollback can undo what was applied"
  fi
  log ">> done. Re-running check:"; log ""
}

# remove every copy of our rule from table/chain; idempotent, so a repeated --rollback is safe
ipt_remove_rule() {
  local table="$1" chain="$2" i=0
  command -v iptables >/dev/null 2>&1 || return 0
  while iptables -t "$table" -C "$chain" "${IPT_RULE[@]}" 2>/dev/null; do
    run iptables -t "$table" -D "$chain" "${IPT_RULE[@]}" || { fail "could not remove the iptables rule from $table/$chain"; return; }
    [ "$DRY" = 1 ] && return
    i=$((i+1)); [ "$i" -lt 10 ] || { fail "iptables rule still present in $table/$chain after $i deletes"; return; }
  done
}

rollback_mitigation() {
  need_root "--rollback"
  [ "$IS_LINUX" = 1 ] || { echo "--rollback only works on Linux" >&2; exit 2; }
  if [ ! -f "$STATE_FILE" ]; then echo "no recorded state at $STATE_FILE; nothing to roll back" >&2; exit 2; fi
  # shellcheck disable=SC1090
  . "$STATE_FILE"
  log ">> rolling back $CVE interim mitigations (applied ${APPLIED_AT:-unknown})"
  # every step below is idempotent, so a failed rollback can simply be re-run
  if [ "${PREV_TCP_MIGRATE_REQ:-n/a}" != "n/a" ]; then
    log ">> net.ipv4.tcp_migrate_req -> $PREV_TCP_MIGRATE_REQ"
    run sysctl -q -w net.ipv4.tcp_migrate_req="$PREV_TCP_MIGRATE_REQ" || fail "could not restore net.ipv4.tcp_migrate_req"
  fi
  if [ "${MPTCP_DISABLED_BY_US:-0}" = 1 ] && [ "${PREV_MPTCP_ENABLED:-n/a}" != "n/a" ]; then
    log ">> net.mptcp.enabled -> $PREV_MPTCP_ENABLED"
    run sysctl -q -w net.mptcp.enabled="$PREV_MPTCP_ENABLED" || fail "could not restore net.mptcp.enabled"
  fi
  case "${FW_BACKEND:-none}" in
    nftables)
      if command -v nft >/dev/null 2>&1 && nft list table inet "$TAG" >/dev/null 2>&1; then
        log ">> removing nftables table inet $TAG"
        run nft delete table inet "$TAG" || fail "could not delete nftables table inet $TAG"
      fi ;;
    iptables)
      log ">> removing iptables rule"
      ipt_remove_rule raw PREROUTING
      ipt_remove_rule filter INPUT ;;   # pre-rewrite location
  esac
  if [ "${NFT_INCLUDE_ADDED:-0}" = 1 ] && [ -f "$NFT_CONF" ]; then
    log ">> removing include from $NFT_CONF"
    if [ "$DRY" = 1 ]; then echo "  [dry-run] strip include line from $NFT_CONF"; else
      sed -i "\|^# $CVE interim mitigation|d; \|^include \"$NFT_FILE\"|d" "$NFT_CONF" ||
        fail "could not remove the include line from $NFT_CONF"; fi
  fi
  if [ -f "$UNIT_FILE" ]; then
    if command -v systemctl >/dev/null 2>&1; then
      run systemctl disable -q "$TAG-mitigation.service" 2>/dev/null || fail "could not disable $TAG-mitigation.service"
    fi
    run rm -f "$UNIT_FILE" || fail "could not remove $UNIT_FILE"
    if command -v systemctl >/dev/null 2>&1; then run systemctl daemon-reload 2>/dev/null || warn "systemctl daemon-reload failed (no systemd?)"; fi
  fi
  run rm -f "$SYSCTL_FILE" "$NFT_FILE" || fail "could not remove $SYSCTL_FILE / $NFT_FILE"
  if [ "$FAILED" = 0 ]; then
    run rm -f "$STATE_FILE"; run rmdir "$STATE_DIR" 2>/dev/null || true
  else
    warn "rollback incomplete; keeping $STATE_FILE so --rollback can be re-run once the error is fixed"
  fi
  log ">> done. Re-running check:"; log ""
}

# ---- 4. main -----------------------------------------------------------------------
[ "$MITIGATE" = 1 ] && apply_mitigation
[ "$ROLLBACK" = 1 ] && rollback_mitigation
collect; report
if [ "$FAILED" = 1 ]; then
  warn "the requested --mitigate/--rollback did not complete (patch state: $VERDICT); exiting 4"
  exit 4
fi
case "$VERDICT" in PATCHED|NOT_AFFECTED) exit 0 ;; UNKNOWN) exit 3 ;; *) exit 1 ;; esac