#!/usr/bin/env bash
# check-vmsa-2026-0011-tools.sh: offline, read-only triage of one VMware Tools version for Broadcom VMSA-2026-0011
# (24 Nov 2026): CVE-2026-123918, VGAuth denial of service, CVSS 3.1 5.5. Takes the version as a string, for example
# 12.5.6, 13.1.10.0 or "12.5.0.24276846 (build-24276846)". It never connects to anything and never writes a file.
# Pack 1.0.0. Bash and PowerShell (Check-Vmsa20260011Tools.ps1) give the same output and exit code.
#
# Fixed versions from the VMSA-2026-0011 response matrix: 13.x: 13.1.10.0; 12.x and 11.x: 12.5.6.0. Note [1]:
# VMware Tools 12.4.11, which is part of 12.5.6, also addresses the issue for Windows 32-bit. Note [2]: fixed
# open-vm-tools comes from Linux vendors and version numbers may differ. Versions the VMSA does not list (10.x and
# earlier): Broadcom's FAQ question 39 says to presume versions it does not list are affected.
set -u
export LC_ALL=C

ADV="https://support.broadcom.com/web/ecx/support-content-notification/-/external/content/SecurityAdvisories/0/39626"
usage() {
  echo 'Usage: check-vmsa-2026-0011-tools.sh --version-string S'
  echo 'Offline and read-only. Exit codes: 0 FIXED, 2 AFFECTED, 3 REVIEW, 5 USAGE ERROR (1 and 4 are not used).'
}
die() { echo "ERROR: $*"; exit 5; }
clean_name() { printf '%s' "$1" | tr -c '[:alnum:]._-' '?' | head -c 64; }
trim() { local s=$1; s=${s#"${s%%[![:space:]]*}"}; s=${s%"${s##*[![:space:]]}"}; printf '%s' "$s"; }

VSTR=""; VSET=0
while [ $# -gt 0 ]; do
  case $1 in
    --version-string|-VersionString) [ $# -ge 2 ] || die "version-string option needs a value"; VSTR=$2; VSET=1; shift 2;;
    -h|--help|-Help) usage; exit 5;;
    *) die "unknown option: $(clean_name "$1")";;
  esac
done
[ "$VSET" = 1 ] || { usage; exit 5; }

s=$(trim "$VSTR"); s=${s#VMware Tools }
if [ "${#s}" -le 60 ] && [[ "$s" =~ ^([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{1,3})(\.[0-9]{1,9})?([[:space:]]+\(build-[0-9]{1,12}\))?$ ]]; then
  M=$((10#${BASH_REMATCH[1]})); m=$((10#${BASH_REMATCH[2]})); p=$((10#${BASH_REMATCH[3]})); SHOWV="$M.$m.$p"
  if [ "$M" -ge 14 ]; then W=REVIEW; WHY="$M.x is not covered by VMSA-2026-0011; confirm with Broadcom"
  elif [ "$M" -eq 13 ]; then
    if [ "$m" -gt 1 ] || { [ "$m" -eq 1 ] && [ "$p" -ge 10 ]; }; then W=FIXED; WHY="13.1.10 is the fixed 13.x version"; else W=AFFECTED; WHY="13.1.10 is the fixed 13.x version"; fi
  elif [ "$M" -eq 12 ]; then
    if [ "$m" -gt 5 ] || { [ "$m" -eq 5 ] && [ "$p" -ge 6 ]; }; then W=FIXED; WHY="12.5.6 is the fixed 12.x and 11.x version"
    elif [ "$m" -eq 4 ] && [ "$p" -ge 11 ]; then W=REVIEW; WHY="VMware Tools 12.4.11 is part of 12.5.6 and addresses the issue for Windows 32-bit only; on other platforms 12.5.6 is the fix"
    else W=AFFECTED; WHY="12.5.6 is the fixed 12.x and 11.x version"; fi
  elif [ "$M" -eq 11 ]; then W=AFFECTED; WHY="12.5.6 is the fixed version for 11.x"
  else W=AFFECTED; WHY="presumed: the VMSA does not list $M.x and Broadcom's FAQ question 39 says to presume such versions are affected; upgrade"; fi
else
  SHOWV="unreadable"; W=REVIEW; WHY="the version is not like 12.5.6, 13.1.10.0 or 12.5.0.24276846 (build-24276846)"
fi
case $W in FIXED) RC=0;; AFFECTED) RC=2;; *) RC=3;; esac
echo "TARGET 1: version string"
echo "  VERSION: $SHOWV"
echo "  RESULT: $W ($WHY)"
echo "  CVE-2026-123918 (CVSS 3.1 5.5): $W"
echo "NOTE: open-vm-tools on Linux: the fixed version comes from the Linux distribution vendor and its number may differ (VMSA-2026-0011 note [2]); check the vendor's advisory, not this result."
echo "NOTE: updating vCenter and ESX does not update VMware Tools inside virtual machines; plan the Tools update separately (Broadcom FAQ question 18)."
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). Patch now regardless."
echo "SOURCE: $ADV"
echo "RESULT: $W (exit $RC)"
exit "$RC"
