#!/usr/bin/env bash
# check-vmsa-2026-0011-esx.sh: offline, read-only triage of VMware ESX hosts for Broadcom VMSA-2026-0011 (24 Nov 2026):
# CVE-2026-123910 to CVE-2026-123916. Reads an ESX version and build given as strings, or a saved file: PowerCLI
# "Get-VMHost | Select-Object Name, Version, Build" output (Broadcom FAQ question 32) as Format-List or Export-Csv,
# saved "esxcli system version get" output, or saved "vmware -v" / "vmware -vl" output. [UNVERIFIED] Broadcom names
# the esxcli and vmware commands but does not show their output; the parser accepts "Version: X.Y.Z" with
# "Build: Releasebuild-N" and "VMware ESXi X.Y.Z build-N" lines. It never connects to anything, never writes a file
# and prints only versions, builds, row numbers and cleaned names. Pack 1.0.0. Bash and PowerShell
# (Check-Vmsa20260011Esx.ps1) give the same output and exit code.
#
# Fixed versions from the VMSA-2026-0011 response matrix; builds from Broadcom's release notes:
#   9.1.x: ESXi-9.1.2.0-25946735; 9.0.x: ESXi-9.0.2.0200-25953133, which does not fix CVE-2026-123914 or
#   CVE-2026-123915 (Patch Pending); 8.0: ESXi80U3m-25926933 (KB 463535 is an 8.0-only interim mitigation for
#   CVE-2026-123910 and CVE-2026-123911); 7.0: extended support only. CVE-2026-123916 was first resolved in VCF 9.1.1
#   and ESX 8.0 U3j (Broadcom FAQ question 16); KB 316595 gives ESX 9.1.1.0 build 25714478 and ESXi 8.0 U3j build
#   25429389 (OUR ASSUMPTION: VCF 9.1.1 carries ESX 9.1.1.0).
# OUR ASSUMPTION: within one release branch, a higher build number is a later release.
set -u
export LC_ALL=C

ADV="https://support.broadcom.com/web/ecx/support-content-notification/-/external/content/SecurityAdvisories/0/39626"
FIX91=25946735; FIX90=25953133; FIX80=25926933
F916_91=25714478; F916_80=25429389
CVES="CVE-2026-123910:9.8 CVE-2026-123911:9.8 CVE-2026-123912:8.4 CVE-2026-123913:8.2 CVE-2026-123914:6.7 CVE-2026-123915:6.3 CVE-2026-123916:5.5"
MAX_FILE=4194304
MAX_LINE=65536
MAX_TARGETS=2000

usage() {
  echo 'Usage: check-vmsa-2026-0011-esx.sh (--version-string S [--build N] | --build N | --file F)'
  echo 'Offline and read-only. Exit codes: 0 FIXED, 2 AFFECTED, 3 REVIEW (including Patch Pending), 5 USAGE OR READ ERROR (1 and 4 are not used).'
}
die() { echo "ERROR: $*"; exit 5; }
clean_name() { local b=${1##*/}; [ -n "$b" ] || b=$1; printf '%s' "$b" | tr -c '[:alnum:]._-' '?' | head -c 64; }
trim() { local s=$1; s=${s#"${s%%[![:space:]]*}"}; s=${s%"${s##*[![:space:]]}"}; printf '%s' "$s"; }
read_lines() {
  local f=$1 sz l
  { [ -r "$f" ] && [ ! -d "$f" ]; } || die "file not found or unreadable: $(clean_name "$f")"
  sz=$(head -c "$((MAX_FILE + 1))" < "$f" 2>/dev/null | wc -c) || die "file unreadable: $(clean_name "$f")"
  [ "$sz" -le "$MAX_FILE" ] || die "file too large: $(clean_name "$f")"
  LINES=()
  while IFS= read -r l || [ -n "$l" ]; do LINES+=("${l%$'\r'}"); done < <(head -c "$MAX_FILE" < "$f" | tr '\000' '\n')
}

# classify VERSION BUILD: sets SHOWV, WHY, BR (9.1, 9.0, 8.0, 7.0, old or empty) and ST, seven verdict letters in CVES
# order: F fixed, A affected, P patch pending, R review.
classify() {
  local raw=$1 s M m p q u lt pre main=R s16=R
  B=$2; SHOWV=""; BR=""; ST=RRRRRRR; WHY=""
  s=$(trim "$raw")
  for pre in 'VMware ESXi ' 'VMware ESX ' 'ESXi ' 'ESX '; do s=${s#"$pre"}; done
  if [ -n "$B" ] && ! [[ "$B" =~ ^[0-9]{5,9}$ ]]; then SHOWV="not checked"; WHY="the build is not a number of 5 to 9 digits"; return; fi
  if [ -z "$s" ]; then
    SHOWV="not given"
    case $B in
      '') WHY="no version or build read";;
      "$FIX91") BR=9.1; ST=FFFFFFF; WHY="build $B is ESX 9.1.2.0 in Broadcom's release notes";;
      "$FIX90") BR=9.0; ST=FFFFPPF; WHY="build $B is ESX 9.0.2.0200 in Broadcom's release notes; CVE-2026-123914 and CVE-2026-123915 are Patch Pending for ESX 9.0";;
      "$FIX80") BR=8.0; ST=FFFFFFF; WHY="build $B is ESXi 8.0 Update 3m (ESXi80U3m) in Broadcom's release notes";;
      *) WHY="a build number alone cannot be placed in a release branch; give the version too";;
    esac
    return
  fi
  if [ "${#s}" -le 40 ] && [[ "$s" =~ ^([0-9]{1,2})\.([0-9]{1,2})[[:space:]]*(U|Update[[:space:]]*)([0-9])([a-z]?)$ ]]; then
    M=$((10#${BASH_REMATCH[1]})); m=$((10#${BASH_REMATCH[2]})); u=$((10#${BASH_REMATCH[4]})); lt=${BASH_REMATCH[5]}; SHOWV="$M.$m U$u$lt"
    if [ "$M" -eq 7 ] && [ "$m" -eq 0 ]; then p=0
    elif [ "$M" -eq 8 ] && [ "$m" -eq 0 ]; then
      BR=8.0
      if [ -n "$B" ]; then
        if [ "$B" -ge "$FIX80" ]; then main=F; WHY="build $B is at or above $FIX80, the build of ESXi80U3m"; else main=A; WHY="build $B is below $FIX80, the build of ESXi80U3m"; fi
        if [ "$main" = F ] || [ "$B" -ge "$F916_80" ]; then s16=F; else s16=A; fi
      elif [ "$u" -lt 3 ]; then main=A; s16=A; WHY="8.0 U3m is the fixed 8.0 release"
      elif [ "$u" -gt 3 ]; then BR=""; WHY="8.0 U$u is not a release Broadcom lists"; return
      elif [ -z "$lt" ] || [[ "$lt" < m ]]; then main=A; WHY="8.0 U3${lt} is earlier than the fixed 8.0 U3m"; if [ -n "$lt" ] && ! [[ "$lt" < j ]]; then s16=F; else s16=A; fi
      else main=F; s16=F; WHY="8.0 U3$lt is 8.0 U3m or later; Broadcom: patches are cumulative"; fi
      ST="$main$main$main$main$main$main$s16"; return
    else WHY="update names are only checked for ESX 7.0 and 8.0"; return; fi
  fi
  if [ -z "$WHY" ] && [ -z "${p:-}" ]; then
    if ! { [ "${#s}" -le 40 ] && [[ "$s" =~ ^([0-9]{1,2})\.([0-9]{1,2})(\.([0-9]{1,2}))?(\.([0-9]{1,5}))?$ ]]; }; then
      SHOWV="unreadable"; WHY="the version is not like 9.1.2, 9.0.2.0200, 8.0.3 or 8.0 U3m"; return
    fi
    SHOWV=$s; M=$((10#${BASH_REMATCH[1]})); m=$((10#${BASH_REMATCH[2]})); p=${BASH_REMATCH[4]}; q=${BASH_REMATCH[6]}
    [ -z "$p" ] || p=$((10#$p)); [ -z "$q" ] || q=$((10#$q))
  fi
  if [ "$M" -le 6 ]; then BR=old; ST=AAAAAAA; WHY="presumed: the VMSA does not list $M.$m and Broadcom's FAQ question 39 says to presume such versions are affected; upgrade"; return; fi
  if [ "$M" -eq 7 ]; then
    if [ "$m" -ne 0 ]; then WHY="7.$m is not an ESX release"; return; fi
    BR=7.0; ST=AAAAAAA; WHY="Broadcom lists ESX 7.0 as affected; End of General Support was 2 Oct 2025, so fixes come only through an extended support contract (contact Broadcom Support)"; return
  fi
  if [ "$M" -eq 8 ] && [ "$m" -eq 0 ]; then
    BR=8.0
    if [ -n "$B" ]; then
      if [ "$B" -ge "$FIX80" ]; then main=F; WHY="build $B is at or above $FIX80, the build of ESXi80U3m"; [ "$B" = "$FIX80" ] && WHY="build $B is Broadcom's documented build of ESXi80U3m"; else main=A; WHY="build $B is below $FIX80, the build of ESXi80U3m"; fi
      if [ "$main" = F ] || [ "$B" -ge "$F916_80" ]; then s16=F; else s16=A; fi
    elif [ -n "$p" ] && [ "$p" -lt 3 ]; then main=A; s16=A; WHY="8.0 U3m is the fixed 8.0 release"
    elif [ -n "$p" ] && [ "$p" -gt 3 ]; then BR=""; WHY="8.0.$p is not a release Broadcom lists"; return
    else WHY="the 8.0 version alone does not say which patch is installed; give the build"; return; fi
    ST="$main$main$main$main$main$main$s16"; return
  fi
  if [ "$M" -eq 9 ] && [ "$m" -eq 0 ]; then
    BR=9.0
    if [ -n "$B" ]; then
      if [ "$B" -ge "$FIX90" ]; then main=F; WHY="build $B is at or above $FIX90, the build of ESXi-9.0.2.0200"; [ "$B" = "$FIX90" ] && WHY="build $B is Broadcom's documented build of ESXi-9.0.2.0200"; else main=A; WHY="build $B is below $FIX90, the build of ESXi-9.0.2.0200"; fi
    elif [ -z "$p" ]; then WHY="the 9.0 version alone does not say which patch is installed; give the build"; return
    elif [ "$p" -lt 2 ]; then main=A; WHY="ESXi-9.0.2.0200 is the fixed 9.0 release"
    elif [ "$p" -gt 2 ]; then main=F; WHY="$s is later than 9.0.2.0200; Broadcom: patches are cumulative"
    elif [ -z "$q" ]; then WHY="9.0.2 alone does not say which patch is installed; give the build"; return
    elif [ "$q" -lt 200 ]; then main=A; WHY="$s is below 9.0.2.0200"
    else main=F; WHY="$s is at or above 9.0.2.0200"; fi
    [ "$main" = A ] || WHY="$WHY; CVE-2026-123914 and CVE-2026-123915 are Patch Pending for ESX 9.0"
    ST="$main$main$main${main}PP$main"; return
  fi
  if [ "$M" -eq 9 ] && [ "$m" -eq 1 ]; then
    BR=9.1
    if [ -n "$B" ]; then
      if [ "$B" -ge "$FIX91" ]; then main=F; WHY="build $B is at or above $FIX91, the build of ESXi-9.1.2.0"; [ "$B" = "$FIX91" ] && WHY="build $B is Broadcom's documented build of ESXi-9.1.2.0"; else main=A; WHY="build $B is below $FIX91, the build of ESXi-9.1.2.0"; fi
      if [ "$main" = F ] || [ "$B" -ge "$F916_91" ]; then s16=F; else s16=A; fi
    elif [ -z "$p" ]; then WHY="the 9.1 version alone does not say which patch is installed; give the build"; return
    elif [ "$p" -lt 2 ]; then main=A; WHY="ESXi-9.1.2.0 is the fixed 9.1 release; 9.1.1 is protected only from CVE-2026-123916"; if [ "$p" -ge 1 ]; then s16=F; else s16=A; fi
    else main=F; s16=F; WHY="$s is at or above 9.1.2"; fi
    ST="$main$main$main$main$main$main$s16"; return
  fi
  WHY="$M.$m is not covered by VMSA-2026-0011; confirm with Broadcom"
}

VSTR=""; VSET=0; BSTR=""; BSET=0; FILE=""
need() { [ "$1" -ge 2 ] || die "$2 option needs a value"; }
while [ $# -gt 0 ]; do
  case $1 in
    --version-string|-VersionString) need $# version-string; VSTR=$2; VSET=1; shift 2;;
    --build|-Build) need $# build; BSTR=$(trim "$2"); BSET=1; [ -n "$BSTR" ] || die "build option needs a value"; shift 2;;
    --file|-File) need $# file; FILE=$2; [ -n "$FILE" ] || die "file option needs a value"; shift 2;;
    -h|--help|-Help) usage; exit 5;;
    *) die "unknown option: $(clean_name "$1")";;
  esac
done
[ -z "$FILE" ] || { [ "$VSET" = 0 ] && [ "$BSET" = 0 ]; } || die "give --file, or --version-string and --build, not both"
[ -n "$FILE" ] || [ "$VSET" = 1 ] || [ "$BSET" = 1 ] || { usage; exit 5; }

TL=(); TV=(); TB=()
add_target() { [ "${#TL[@]}" -lt "$MAX_TARGETS" ] && { TL+=("$1"); TV+=("$2"); TB+=("$3"); }; return 0; }
unq() { local s; s=$(trim "$1"); s=${s#\"}; s=${s%\"}; trim "$s"; }
cbuild() { local s; s=$(trim "$1"); s=${s#Releasebuild-}; s=${s#build-}; printf '%s' "$s"; }
if [ -z "$FILE" ]; then
  add_target "version string" "$VSTR" "$BSTR"
else
  read_lines "$FILE"; FN=$(clean_name "$FILE")
  hdr=-1; i=0
  for l in "${LINES[@]}"; do
    l=${l#$'\xef\xbb\xbf'}
    if [ -n "$(trim "$l")" ] && [[ "$l" != '#'* ]]; then hdr=$i; break; fi
    i=$((i + 1))
  done
  ci_v=-1; ci_b=-1; ci_n=-1
  if [ "$hdr" -ge 0 ] && [[ "${LINES[$hdr]}" == *,* ]]; then
    IFS=',' read -r -a H <<< "${LINES[$hdr]#$'\xef\xbb\xbf'}"
    for k in "${!H[@]}"; do
      h=$(unq "${H[$k]}"); h=${h,,}
      case $h in version) ci_v=$k;; build) ci_b=$k;; name) ci_n=$k;; esac
    done
  fi
  if [ "$ci_v" -ge 0 ] || [ "$ci_b" -ge 0 ]; then
    row=1
    for ((j = hdr + 1; j < ${#LINES[@]}; j++)); do
      l=${LINES[$j]}; [ "${#l}" -le "$MAX_LINE" ] || continue
      [ -n "$(trim "$l")" ] || continue
      row=$((row + 1))
      IFS=',' read -r -a F <<< "$l"
      v=""; b=""; n=""
      [ "$ci_v" -lt 0 ] || v=$(unq "${F[$ci_v]:-}")
      [ "$ci_b" -lt 0 ] || b=$(cbuild "$(unq "${F[$ci_b]:-}")")
      [ "$ci_n" -lt 0 ] || n=$(unq "${F[$ci_n]:-}")
      lab="file $FN, row $row"; [ -z "$n" ] || lab="$lab ($(clean_name "$n"))"
      add_target "$lab" "$v" "$b"
    done
  else
    blk=0; v=""; b=""; n=""; hv=0; hb=0; hn=0
    flush() { if [ "$hv" = 1 ] || [ "$hb" = 1 ]; then blk=$((blk + 1)); lab="file $FN, entry $blk"; [ -z "$n" ] || lab="$lab ($(clean_name "$n"))"; add_target "$lab" "$v" "$b"; fi; v=""; b=""; n=""; hv=0; hb=0; hn=0; }
    for l in "${LINES[@]}"; do
      [ "${#l}" -le "$MAX_LINE" ] || continue
      if [ -z "$(trim "$l")" ]; then flush; continue; fi
      if [[ "$l" =~ ^[[:space:]]*VMware[[:space:]]ESXi?[[:space:]]([0-9][0-9.]{0,20})[[:space:]]build-([0-9]{1,12})[[:space:]]*$ ]]; then
        flush; v=${BASH_REMATCH[1]}; b=${BASH_REMATCH[2]}; hv=1; hb=1; flush; continue
      fi
      if [[ "$l" =~ ^[[:space:]]*([Nn][Aa][Mm][Ee]|[Vv][Ee][Rr][Ss][Ii][Oo][Nn]|[Bb][Uu][Ii][Ll][Dd])[[:space:]]*:(.*)$ ]]; then
        k=${BASH_REMATCH[1],,}; val=$(trim "${BASH_REMATCH[2]}")
        case $k in
          version) [ "$hv" = 0 ] || flush; v=$val; hv=1;;
          build) [ "$hb" = 0 ] || flush; b=$(cbuild "$val"); hb=1;;
          name) [ "$hn" = 0 ] || flush; n=$val; hn=1;;
        esac
      fi
    done
    flush
  fi
  [ "${#TL[@]}" -gt 0 ] || add_target "file $FN" "" ""
fi

word() { case $1 in F) echo "FIXED";; A) echo "AFFECTED";; P) echo "NOT FIXED (Patch Pending for ESX 9.0 in VMSA-2026-0011)";; *) echo "REVIEW";; esac; }
na=0; nr=0; nf=0
for idx in "${!TL[@]}"; do
  p=""; classify "${TV[$idx]}" "${TB[$idx]}"
  n=$((idx + 1))
  if [ -n "${TB[$idx]}" ] && [[ "${TB[$idx]}" =~ ^[0-9]{5,9}$ ]]; then bt="build ${TB[$idx]}"; elif [ -n "${TB[$idx]}" ]; then bt="build unreadable"; else bt="build not given"; fi
  if [[ "$ST" == *A* ]]; then w=AFFECTED; na=$((na + 1)); elif [[ "$ST" == *[RP]* ]]; then w=REVIEW; nr=$((nr + 1)); else w=FIXED; nf=$((nf + 1)); fi
  echo "TARGET $n: ${TL[$idx]}"
  echo "  VERSION: $SHOWV, $bt"
  echo "  RESULT: $w ($WHY)"
  k=0
  for c in $CVES; do echo "  ${c%%:*} (CVSS 3.1 ${c#*:}): $(word "${ST:$k:1}")"; k=$((k + 1)); done
  if [ "$BR" = 8.0 ] && [ "${ST:0:1}" != F ]; then
    echo "  MITIGATION (ESX 8.0 only, Broadcom KB 463535, until the host is updated): ESXi 8.x runs the IOFilterVP service on port 9080. Restrict it so only the vCenter Server IP address can reach port 9080: esxcli network firewall ruleset set -r iofiltervp --allowed-all false, then esxcli network firewall ruleset allowedip add -r iofiltervp -i <vCenter-IP>, then check with esxcli network firewall ruleset allowedip list -r iofiltervp. Broadcom's FAQ (question 25) says this covers only a small part of the advisory and may affect cluster operation; update instead."
  elif { [ "$BR" = 9.1 ] || [ "$BR" = 9.0 ] || [ "$BR" = 7.0 ]; } && [ "${ST:0:1}" = A ]; then
    echo "  NOTE: Broadcom lists no workaround for ESX $BR; update the host."
  fi
  [ "$BR" != 9.0 ] || [ "${ST:4:1}" != P ] || echo "  NOTE: ESX 9.0 has no fix yet for CVE-2026-123914 and CVE-2026-123915 (Moderate, Patch Pending); watch the VMSA for an update."
done
if [ "$na" -gt 0 ]; then RES="AFFECTED"; RC=2
elif [ "$nr" -gt 0 ]; then RES="REVIEW"; RC=3
else RES="FIXED"; RC=0; fi
echo "NOTE: ESX was not part of VMSA-2026-0009, so hosts need this update even when vCenter runs 8.0 U3m or SDDC Manager runs 5.2.5 (Broadcom FAQ question 21). VCF 5.x: apply ESXi80U3m through KB 88287. Telco Cloud Platform 5.0.x and 5.1.x: follow KB 463501. Update vSAN Witness Appliances too (FAQ question 14)."
echo "NOTE: the CVE-2026-123912 and CVE-2026-123916 fixes are in the VMX process: a VM runs the fixed VMX only after it powers on or moves with vMotion to an updated host (Broadcom FAQ question 34)."
echo "NOTE: no exploitation reported by Broadcom or CISA as of 24 Nov 2026. None of the 23 CVEs is in CISA KEV (catalogue 2026.11.23; KEV due dates bind US federal agencies only, BOD 26-04). Broadcom's FAQ treats the four Critical issues (CVSS 3.1 9.8, 9.8, 9.8 and 9.3) as an emergency change. Patch now regardless."
echo "SOURCE: $ADV"
echo "RESULT: $RES (targets: $na affected, $nr review, $nf fixed) (exit $RC)"
exit "$RC"
