#!/usr/bin/env bash
# check-java4shell.sh: offline, read-only checker for CVE-2026-114938 ("Java4Shell", HotSpot hardware-accelerated
# Base64 decoder). Pack 1.1.0 (19 Nov 2026). It opens no sockets, writes no files, never runs java and runs no other
# program except tr, head and wc. It reads only the files you name, plus /proc/cpuinfo when no CPU option is given
# and that file exists. It prints version numbers, vendor names and the JVM flags it checks; it never prints file
# content otherwise. --show-args prints the JVM arguments with secret values replaced by [REDACTED].
#
# Usage: check-java4shell.sh (--java-version-file F | --java-version-string S | --java-home DIR | --release-file F)
#          [--cpu-flags-file F | --cpu-flags-string S | --no-cpuinfo] [--jvm-args-file F | --jvm-args-string S] [--show-args]
#   --java-version-file F    saved output of: java -version (it is printed on stderr, so save it with 2>&1)
#   --java-version-string S  the same text as a string
#   --java-home DIR          read DIR/release (the JDK or JRE release file), for example --java-home "$JAVA_HOME"
#   --release-file F         a saved copy of a release file
#   --cpu-flags-file F       a saved /proc/cpuinfo, or a file of CPU flag names separated by spaces, commas or lines
#   --cpu-flags-string S     CPU flag names as a string
#   --no-cpuinfo             do not read /proc/cpuinfo (CPU state is then unknown unless a CPU option is given)
#   --jvm-args-file F        saved JVM command line (for example /proc/PID/cmdline, NUL separated, or ps output)
#   --jvm-args-string S      the JVM command line as a string
#   --show-args              print the JVM arguments, with secrets redacted
#
# Exit codes:
#   0 NOT AFFECTED  the Java version is a fixed build, or a release line OpenJDK and Oracle do not list for this CVE
#   1           not used (reserved for unexpected runtime errors)
#   2 AFFECTED  the version is affected and no workaround flag was seen (AVX-512 VBMI present or unknown)
#   3 UNKNOWN   no usable version, a release line the advisories do not cover, or a build the sources disagree on (REVIEW)
#   4 MITIGATED  the version is affected but the JVM arguments carry an effective temporary workaround flag
#   5 USAGE OR READ ERROR
#   6 AFFECTED, CPU PATH NOT PRESENT (still patch)  the version is affected, no workaround seen, AVX-512 VBMI absent on this CPU
set -u
export LC_ALL=C

# Release line|last affected build listed by OpenJDK and Oracle|first fixed Oracle and OpenJDK build (17 Nov 2026).
FIXDATA="17|17.0.21.0.3|17.0.21.1
21|21.0.13.0.3|21.0.13.1
25|25.0.5.0.3|25.0.5.1
27|27.0.1.0.3|27.0.1.1"
# Azul Zulu CPU-line builds that Azul lists as fixing this CVE (version prefix|lowest fifth part).
ZULUCPU="17.0.20.1|201
21.0.12.1|201
25.0.4.1|201"
ADV="https://openjdk.org/groups/vulnerability/advisories/2026-11-17"
MAX_SMALL=1048576
MAX_CPU=8388608
MAX_LINE=4096
MAX_SHOW=4096

usage() {
  echo 'Usage: check-java4shell.sh (--java-version-file F | --java-version-string S | --java-home DIR | --release-file F) [--cpu-flags-file F | --cpu-flags-string S | --no-cpuinfo] [--jvm-args-file F | --jvm-args-string S] [--show-args]'
  echo 'Offline and read-only; never runs java. Exit codes: 0 NOT AFFECTED, 2 AFFECTED, 3 UNKNOWN, 4 MITIGATED, 5 USAGE OR READ ERROR, 6 AFFECTED CPU PATH NOT PRESENT, STILL PATCH (1 is not used).'
}
die() { echo "ERROR: $*"; exit 5; }
clean_name() { local b=${1##*/}; [ -n "$b" ] || b=$1; printf '%s' "$b" | tr -c '[:alnum:]._-' '?' | head -c 80; }
VER_RE='^[0-9]{1,4}(\.[0-9]{1,6}){0,5}$'
LAUNCH_RE='(^|[/\\])(java|java\.exe|javaw\.exe)$'
ver_cmp() {
  local IFS=. i x y; local -a a b; read -ra a <<< "$1"; read -ra b <<< "$2"
  for i in 0 1 2 3 4 5; do x=$((10#${a[i]:-0})); y=$((10#${b[i]:-0})); if [ "$x" -lt "$y" ]; then echo -1; return; elif [ "$x" -gt "$y" ]; then echo 1; return; fi; done
  echo 0
}
# read_lines FILE MAX: fills LINES with the file's lines (NUL becomes a line break, a trailing CR is dropped).
read_lines() {
  local f=$1 max=$2 sz l
  { [ -r "$f" ] && [ ! -d "$f" ]; } || die "file not found or unreadable: $(clean_name "$f")"
  sz=$(head -c "$((max + 1))" < "$f" 2>/dev/null | wc -c) || die "file unreadable: $(clean_name "$f")"
  [ "$sz" -le "$max" ] || die "file too large: $(clean_name "$f")"
  LINES=()
  while IFS= read -r l || [ -n "$l" ]; do LINES+=("${l%$'\r'}"); done < <(head -c "$max" < "$f" | tr '\000' '\n')
}
str_lines() { local l; LINES=(); while IFS= read -r l || [ -n "$l" ]; do LINES+=("${l%$'\r'}"); done <<< "$1"; }

VFILE=""; VSTR=""; VSET=0; JHOME=""; RFILE=""; CFILE=""; CSTR=""; CSET=0; NOCPU=0; AFILE=""; ASTR=""; ASET=0; SHOW=0
need() { [ "$1" -ge 2 ] || die "$2 option needs a value"; }
while [ $# -gt 0 ]; do
  case $1 in
    --java-version-file|-JavaVersionFile) need $# java-version-file; VFILE=$2; [ -n "$VFILE" ] || die "java-version-file option needs a value"; shift 2;;
    --java-version-string|-JavaVersionString) need $# java-version-string; VSTR=$2; VSET=1; shift 2;;
    --java-home|-JavaHome) need $# java-home; JHOME=$2; [ -n "$JHOME" ] || die "java-home option needs a value"; shift 2;;
    --release-file|-ReleaseFile) need $# release-file; RFILE=$2; [ -n "$RFILE" ] || die "release-file option needs a value"; shift 2;;
    --cpu-flags-file|-CpuFlagsFile) need $# cpu-flags-file; CFILE=$2; [ -n "$CFILE" ] || die "cpu-flags-file option needs a value"; shift 2;;
    --cpu-flags-string|-CpuFlagsString) need $# cpu-flags-string; CSTR=$2; CSET=1; shift 2;;
    --no-cpuinfo|-NoCpuInfo) NOCPU=1; shift;;
    --jvm-args-file|-JvmArgsFile) need $# jvm-args-file; AFILE=$2; [ -n "$AFILE" ] || die "jvm-args-file option needs a value"; shift 2;;
    --jvm-args-string|-JvmArgsString) need $# jvm-args-string; ASTR=$2; ASET=1; shift 2;;
    --show-args|-ShowArgs) SHOW=1; shift;;
    -h|--help|-Help) usage; exit 5;;
    *) die "unknown option: $(clean_name "$1")";;
  esac
done
nv=0; [ -n "$VFILE" ] && nv=$((nv + 1)); [ "$VSET" = 1 ] && nv=$((nv + 1))
nr=0; [ -n "$JHOME" ] && nr=$((nr + 1)); [ -n "$RFILE" ] && nr=$((nr + 1))
[ "$nv" -le 1 ] || die "give --java-version-file or --java-version-string, not both"
[ "$nr" -le 1 ] || die "give --java-home or --release-file, not both"
[ $((nv + nr)) -ge 1 ] || { usage; exit 5; }
ncpu=0; [ -n "$CFILE" ] && ncpu=$((ncpu + 1)); [ "$CSET" = 1 ] && ncpu=$((ncpu + 1)); [ "$NOCPU" = 1 ] && ncpu=$((ncpu + 1))
[ "$ncpu" -le 1 ] || die "give one of --cpu-flags-file, --cpu-flags-string or --no-cpuinfo"
[ -z "$AFILE" ] || [ "$ASET" = 0 ] || die "give --jvm-args-file or --jvm-args-string, not both"
[ "$SHOW" = 0 ] || [ -n "$AFILE" ] || [ "$ASET" = 1 ] || die "--show-args needs --jvm-args-file or --jvm-args-string"

# ---- Java version ----
VRAW=""; VSRC=""; BUILDS=(); VENDTXT=""
if [ "$nv" = 1 ]; then
  if [ -n "$VFILE" ]; then read_lines "$VFILE" "$MAX_SMALL"; VSRC="java -version file $(clean_name "$VFILE")"
  else str_lines "$VSTR"; VSRC="java -version string"; fi
  n=0
  for l in "${LINES[@]}"; do
    n=$((n + 1)); [ "$n" -le 50 ] || break
    [ "${#l}" -le "$MAX_LINE" ] || continue
    if [ -z "$VRAW" ] && [[ "$l" =~ version[[:space:]]+\"([^\"]{1,64})\" ]]; then VRAW=${BASH_REMATCH[1]}; fi
    rest=$l
    while [[ "$rest" =~ \(build[[:space:]]+([^\)[:space:]]{1,80})(.*)$ ]]; do BUILDS+=("${BASH_REMATCH[1]}"); rest=${BASH_REMATCH[2]}; done
    VENDTXT="$VENDTXT $l"
  done
fi
RVER=""; RSRC=""
if [ "$nr" = 1 ]; then
  if [ -n "$JHOME" ]; then rf="${JHOME%/}/release"; RSRC="release file in the given Java home"; else rf=$RFILE; RSRC="release file $(clean_name "$RFILE")"; fi
  read_lines "$rf" "$MAX_SMALL"
  n=0
  for l in "${LINES[@]}"; do
    n=$((n + 1)); [ "$n" -le 200 ] || break
    [ "${#l}" -le "$MAX_LINE" ] || continue
    if [[ "$l" =~ ^JAVA_VERSION=\"([^\"]{1,64})\" ]]; then RVER=${BASH_REMATCH[1]}
    elif [[ "$l" =~ ^JAVA_RUNTIME_VERSION=\"([^\"]{1,80})\" ]]; then BUILDS+=("${BASH_REMATCH[1]}")
    elif [[ "$l" =~ ^(IMPLEMENTOR|IMPLEMENTOR_VERSION)=\"([^\"]{1,80})\" ]]; then VENDTXT="$VENDTXT ${BASH_REMATCH[2]}"; fi
  done
fi
# norm RAW: prints LINE|VERSION (VERSION is dotted numbers; Java 8 1.8.0_U becomes 8.0.U) or nothing.
norm() {
  local r=$1 v
  if [[ "$r" =~ ^1\.8\.0(_([0-9]{1,4}))? ]]; then echo "8|8.0.${BASH_REMATCH[2]:-0}"; return; fi
  if [[ "$r" =~ ^([0-9]{1,4}(\.[0-9]{1,6}){0,5}) ]]; then v=${BASH_REMATCH[1]}; echo "${v%%.*}|$v"; fi
}
SRC=""; NV=""
if [ -n "$VRAW" ]; then NV=$(norm "$VRAW"); SRC=$VSRC; fi
if [ -n "$RVER" ]; then
  NR=$(norm "$RVER")
  if [ -z "$NV" ]; then NV=$NR; SRC=$RSRC
  elif [ -n "$NR" ] && [ "$NR" != "$NV" ]; then echo "NOTE: the release file version (${NR#*|}) differs from java -version (${NV#*|}); using java -version"; fi
fi
LOW=$(printf '%s' "$VENDTXT" | tr '[:upper:]' '[:lower:]')
VENDOR=""
for p in "zulu:Azul Zulu" "corretto:Amazon Corretto" "temurin:Eclipse Temurin" "adoptium:Eclipse Temurin" "microsoft:Microsoft Build of OpenJDK" "red hat:Red Hat build of OpenJDK" "red_hat:Red Hat build of OpenJDK" "graalvm:Oracle GraalVM" "java(tm) se:Oracle Java SE" "oracle corporation:Oracle Java SE"; do
  k=${p%%:*}; if [[ "$LOW" == *"$k"* ]]; then VENDOR=${p#*:}; break; fi
done
PERF=0; [[ "$LOW" == *-perf* ]] && PERF=1
VSTATE=unknown; LINE=""; VER=""
if [ -z "$NV" ]; then echo "VERSION: not found (no version \"...\" line or JAVA_VERSION entry)"
else
  LINE=${NV%%|*}; VER=${NV#*|}
  [[ "$VER" =~ $VER_RE ]] || { VER=""; LINE=""; }
fi
if [ -n "$VER" ]; then
  echo "VERSION: $VER (release line $LINE, from $SRC)"
  [ -n "$VENDOR" ] && echo "VENDOR: $VENDOR (from the version text)"
  hit=""
  while IFS='|' read -r fline last fixed; do [ "$fline" = "$LINE" ] && hit="$last|$fixed"; done <<< "$FIXDATA"
  if [ -n "$hit" ]; then
    last=${hit%|*}; fixed=${hit#*|}
    if [ "$VENDOR" = "Oracle GraalVM" ] && [ "$LINE" = 17 ] && { [ "$(ver_cmp "$VER" "$fixed")" -ge 0 ] || [[ "$VER" == 17.0.20.1 || "$VER" == 17.0.20.1.* ]]; }; then
      VSTATE=review; echo "LINE 17: REVIEW (Oracle GraalVM for JDK 17: Oracle names 17.0.21.1 as the Nov 2026 CSPU, but its release notes say it is built on Oracle JDK 17.0.20.1+4, below the Oracle JDK 17 fix 17.0.21.1+4; confirm with Oracle before treating it as fixed)"
    elif [ "$(ver_cmp "$VER" "$fixed")" -ge 0 ]; then VSTATE=fixed; echo "LINE $LINE: FIXED ($VER is at or above $fixed, the 17 Nov 2026 build)"
    else
      VSTATE=affected
      for b in "${BUILDS[@]+"${BUILDS[@]}"}"; do
        bv=${b%%[+-]*}
        if [ "$VENDOR" = "Azul Zulu" ]; then
          while IFS='|' read -r zp z5; do
            if [[ "$bv" == "$zp".* ]] && [[ "${bv#"$zp".}" =~ ^[0-9]{1,6}$ ]] && [ "$((10#${bv#"$zp".}))" -ge "$z5" ]; then VSTATE=fixed; fi
          done <<< "$ZULUCPU"
        fi
      done
      if [ "$VSTATE" = fixed ]; then echo "LINE $LINE: FIXED (Azul Zulu CPU-line build that Azul lists as fixing this CVE)"
      else
        echo "LINE $LINE: AFFECTED ($VER is below $fixed; OpenJDK and Oracle list $last and earlier as affected)"
        if [ "$LINE" = 17 ] && [ "$(ver_cmp "$VER" 17.0.6)" -lt 0 ]; then echo "NOTE: Red Hat says the vulnerable code path was introduced in 17.0.6; OpenJDK and Oracle give no lower bound, so this pack treats it as affected"; fi
      fi
    fi
  elif [ "$LINE" = 8 ]; then
    upd=${VER##*.}
    if [ "$PERF" = 1 ]; then
      if [ "$((10#$upd))" -lt 513 ]; then VSTATE=affected; echo "LINE 8: AFFECTED (Oracle Java SE 8 Enterprise Performance Pack build; Oracle lists 8u511b33-perf; fixed baseline 1.8.0_513)"
      else VSTATE=fixed; echo "LINE 8: FIXED (performance build at or above the Oracle 1.8.0_513 security baseline)"; fi
    else VSTATE=notlisted; echo "LINE 8: NOT AFFECTED by this CVE (OpenJDK marks only 17, 21, 25 and 27; Oracle lists only the 8u511b33-perf build). Red Hat says OpenJDK 8 and 11 do not contain the vulnerable code path. Update to 8u513 for the other 17 Nov 2026 fixes"; fi
  elif [ "$LINE" = 11 ]; then VSTATE=notlisted; echo "LINE 11: NOT AFFECTED by this CVE (OpenJDK marks only 17, 21, 25 and 27). Red Hat says OpenJDK 8 and 11 do not contain the vulnerable code path. Update to 11.0.33.1 for the other 17 Nov 2026 fixes"
  else echo "LINE $LINE: UNKNOWN (not a release line in the 17 Nov 2026 advisories; unsupported lines get no fixes, so move to a fixed 17, 21, 25 or 27 build)"; fi
fi

# ---- CPU ----
CPU=unknown; CSRC=""; BW=unknown; FLAGS4=""
if [ "$NOCPU" = 0 ]; then
  if [ -n "$CFILE" ]; then read_lines "$CFILE" "$MAX_CPU"; CSRC="file $(clean_name "$CFILE")"
  elif [ "$CSET" = 1 ]; then str_lines "$CSTR"; CSRC="string"
  elif [ -r /proc/cpuinfo ]; then read_lines /proc/cpuinfo "$MAX_CPU"; CSRC="/proc/cpuinfo"
  else LINES=(); fi
  if [ -n "$CSRC" ]; then
    hasflags=0; hasarm=0; ntok=0; vbmi=0; bw=0; x86=0; f512=0; bmi2=0
    for l in "${LINES[@]}"; do
      if [[ "$l" =~ ^flags[[:space:]]*: ]]; then hasflags=1; fi
      if [[ "$l" =~ ^Features[[:space:]]*: ]]; then hasarm=1; fi
    done
    for l in "${LINES[@]}"; do
      if [ "$hasflags" = 1 ]; then [[ "$l" =~ ^flags[[:space:]]*:(.*)$ ]] || continue; l=${BASH_REMATCH[1]}
      elif [ "$hasarm" = 1 ]; then [[ "$l" =~ ^Features[[:space:]]*:(.*)$ ]] || continue; l=${BASH_REMATCH[1]}; fi
      l=${l//,/ }
      read -ra toks <<< "$l"
      for t in "${toks[@]+"${toks[@]}"}"; do
        t=$(printf '%s' "$t" | tr '[:upper:]' '[:lower:]' | tr -d '_-'); ntok=$((ntok + 1))
        case $t in avx512vbmi) vbmi=1;; avx512bw) bw=1;; avx512f) f512=1;; bmi2) bmi2=1;; sse|sse2|fpu|lm|avx|avx2|cx8|cmov) x86=1;; asimd|neon) hasarm=1;; esac
      done
    done
    if [ "$vbmi" = 1 ]; then CPU=present
    elif [ "$hasarm" = 1 ] && [ "$x86" = 0 ]; then CPU=absent-arm
    elif [ "$x86" = 1 ]; then CPU=absent; fi
    if [ "$bw" = 1 ]; then BW=present; elif [ "$x86" = 1 ]; then BW=absent; fi
    st() { if [ "$1" = 1 ]; then echo present; elif [ "$x86" = 1 ]; then echo absent; else echo unknown; fi; }
    FLAGS4="avx512f $(st "$f512"), avx512bw $(st "$bw"), avx512vbmi $(st "$vbmi"), bmi2 $(st "$bmi2")"
  fi
fi
case $CPU in
  present) echo "CPU: AVX-512 VBMI PRESENT (avx512vbmi flag, from $CSRC); avx512bw $BW";;
  absent) echo "CPU: AVX-512 VBMI ABSENT (x86 flags without avx512vbmi, from $CSRC)";;
  absent-arm) echo "CPU: AVX-512 VBMI ABSENT (not an x86 CPU flag list, from $CSRC)";;
  *) if [ -n "$CSRC" ]; then echo "CPU: AVX-512 VBMI UNKNOWN (no recognisable CPU flags, from $CSRC)"; else echo "CPU: AVX-512 VBMI UNKNOWN (no CPU flags given)"; fi;;
esac
case $CPU in present|absent) echo "CPU FLAGS (Red Hat list): $FLAGS4";; esac
echo "NOTE: Red Hat (RHSB-2026-018), Cisco, Siemens, Elastic and Jenkins state the x86-64 AVX-512 VBMI precondition; Red Hat lists avx512f, avx512bw, avx512vbmi and bmi2, and the HotSpot source uses the AVX-512 Base64 decode path only when the CPU reports avx512vbmi and avx512bw. A VM guest can see different flags from its host, and moving the JVM to another host can change this"

# ---- JVM arguments ----
WA=unknown
if [ -n "$AFILE" ] || [ "$ASET" = 1 ]; then
  if [ -n "$AFILE" ]; then read_lines "$AFILE" "$MAX_SMALL"; else str_lines "$ASTR"; fi
  TOKS=()
  for l in "${LINES[@]}"; do read -ra tt <<< "$l"; TOKS+=("${tt[@]+"${tt[@]}"}"); done
  B64=""; AVX=""; UNLOCK=0; HIDDEN=0; ENVSEEN=0; NOJAVA=1; NA=0; BADAVX=0
  for t in "${TOKS[@]+"${TOKS[@]}"}"; do
    t=${t#[\"\']}; t=${t%[\"\']}
    case $t in JAVA_TOOL_OPTIONS=*|JDK_JAVA_OPTIONS=*|_JAVA_OPTIONS=*) ENVSEEN=1; t=${t#*=}; t=${t#[\"\']};; esac
    case $t in
      -XX:-UseBASE64Intrinsics) B64=off; NA=$((NA + 1));;
      -XX:+UseBASE64Intrinsics) B64=on; NA=$((NA + 1));;
      -XX:UseAVX=*) v=${t#-XX:UseAVX=}; if [[ "$v" =~ ^[0-9]{1,2}$ ]]; then AVX=$((10#$v)); NA=$((NA + 1)); else BADAVX=1; fi;;
      -XX:+UnlockDiagnosticVMOptions) UNLOCK=1;;
      -XX:Flags=*|-XX:VMOptionsFile=*|@*) HIDDEN=1;;
    esac
    if [[ "$t" =~ $LAUNCH_RE ]]; then NOJAVA=0; fi
  done
  echo "JVM ARGS: ${#TOKS[@]} arguments read; $NA UseBASE64Intrinsics or UseAVX settings found"
  [ "$NOJAVA" = 1 ] && echo "NOTE: no java launcher name found in the arguments; check that this is the JVM command line"
  [ "$BADAVX" = 1 ] && echo "NOTE: a -XX:UseAVX value that is not a small whole number was ignored"
  [ "$HIDDEN" = 1 ] && echo "NOTE: the command line names an options file or @argfile that this checker does not read; flags in it are not counted"
  [ "$ENVSEEN" = 1 ] && echo "NOTE: JAVA_TOOL_OPTIONS, JDK_JAVA_OPTIONS or _JAVA_OPTIONS text was read in order with the rest; the JVM applies them in its own order"
  echo "NOTE: environment variables (JAVA_TOOL_OPTIONS, JDK_JAVA_OPTIONS, _JAVA_OPTIONS) not in the saved text are not seen"
  eff=0
  case $B64 in
    off) eff=1; echo "FLAG: -XX:-UseBASE64Intrinsics is the last UseBASE64Intrinsics setting: EFFECTIVE (turns off all java.util.Base64 intrinsics; product flag in the HotSpot source)"
         [ "$UNLOCK" = 0 ] && echo "NOTE: Oracle's java manual says intrinsic flags need -XX:+UnlockDiagnosticVMOptions; the HotSpot source declares this one a product flag. A JVM that rejected it would not have started";;
    on) echo "FLAG: -XX:+UseBASE64Intrinsics is the last UseBASE64Intrinsics setting: NOT A WORKAROUND (it turns the intrinsic on)";;
    *) echo "FLAG: no UseBASE64Intrinsics setting";;
  esac
  if [ -n "$AVX" ]; then
    if [ "$AVX" -lt 3 ]; then eff=1; echo "FLAG: -XX:UseAVX=$AVX is the last UseAVX setting: EFFECTIVE (below 3, so HotSpot clears its AVX-512 features, including VBMI; all AVX-512 code paths are lost; Red Hat: x86-64 only, ARM64 JVMs do not recognise this flag and fail to start)"
    else echo "FLAG: -XX:UseAVX=$AVX is the last UseAVX setting: NOT A WORKAROUND (3 or higher keeps AVX-512)"; fi
  else echo "FLAG: no UseAVX setting"; fi
  if [ "$eff" = 1 ]; then WA=effective; else WA=none; fi
  echo "WORKAROUND: $( [ "$WA" = effective ] && echo 'EFFECTIVE (temporary only; both flags are in Red Hat RHSB-2026-018, checked against the HotSpot source)' || echo 'NONE SEEN' )"
  if [ "$SHOW" = 1 ]; then
    out=""; skip=0
    for t in "${TOKS[@]+"${TOKS[@]}"}"; do
      if [ "$skip" = 1 ]; then out="$out [REDACTED]"; skip=0; continue; fi
      low=$(printf '%s' "$t" | tr '[:upper:]' '[:lower:]')
      if [[ "$low" =~ ^(-d|--?)[^=]*(pass|pwd|secret|token|credential|apikey|api[._-]key|private[._-]?key|auth)[^=]*= ]]; then t="${t%%=*}=[REDACTED]"
      elif [[ "$low" =~ ^--?[a-z0-9._-]*(pass|pwd|secret|token|credential|apikey)[a-z0-9._-]*$ ]]; then skip=1
      elif [[ "$t" =~ ^(.*://)[^/@[:space:]]+@(.*)$ ]]; then t="${BASH_REMATCH[1]}[REDACTED]@${BASH_REMATCH[2]}"; fi
      t=$(printf '%s' "$t" | tr -c '[:print:]' '?')
      out="$out $t"
    done
    [ "${#out}" -le "$MAX_SHOW" ] || out="${out:0:$MAX_SHOW} (truncated)"
    echo "ARGS (redacted):$out"
  fi
else
  echo "WORKAROUND: UNKNOWN (no JVM arguments given)"
fi

case $VSTATE in
  fixed|notlisted) rc=0;;
  affected) if [ "$WA" = effective ]; then rc=4; elif [ "$CPU" = absent ] || [ "$CPU" = absent-arm ]; then rc=6; else rc=2; fi;;
  *) rc=3;;
esac
case $rc in
  0) msg="NOT AFFECTED: no action for this CVE; keep Java on the latest 17 Nov 2026 build for the other fixes";;
  2) msg="AFFECTED: update this Java runtime to the 17 Nov 2026 build for its line now; until then add -XX:-UseBASE64Intrinsics (README)";;
  4) msg="MITIGATED (temporary): the workaround flag is set; update to the 17 Nov 2026 build and then remove the flag";;
  6) msg="AFFECTED, CPU PATH NOT PRESENT, STILL PATCH: this CPU lacks AVX-512 VBMI, so the vulnerable decode path is not used on this host, but update anyway; the same runtime is exposed on a host that has it";;
  *) msg="UNKNOWN OR REVIEW: confirm the Java version (save java -version 2>&1 output) and its vendor's fix status, and move to a fixed 17, 21, 25 or 27 build";;
esac
echo "NOTE: exploit attempts reported by SecurityWeek (19 Nov 2026); no confirmed compromise; not in CISA KEV (catalogue 2026.11.18) as of 19 Nov 2026. Patch now regardless."
echo "SOURCE: $ADV"
echo "RESULT: $msg (exit $rc)"
exit "$rc"
