diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 15bcb44..f46a670 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,6 +33,15 @@ jobs: go test ./detectors/sshole/internal/assess -run '^$' -fuzz '^FuzzAccounts$' -fuzztime 30s go test ./detectors/sshole/internal/assess -run '^$' -fuzz '^FuzzStartup$' -fuzztime 30s go test ./detectors/sshole/internal/repo -run '^$' -fuzz '^FuzzScanText$' -fuzztime 30s + go test ./detectors/java4shell/internal/jvm -run '^$' -fuzz '^FuzzInspect$' -fuzztime 30s + go test ./detectors/java4shell/internal/jvm -run '^$' -fuzz '^FuzzSettings$' -fuzztime 30s + go test ./detectors/java4shell/internal/knowledge -run '^$' -fuzz '^FuzzParse$' -fuzztime 20s + go test ./detectors/java4shell/internal/repo -run '^$' -fuzz '^FuzzScanText$' -fuzztime 30s + go test ./detectors/java4shell/internal/jvm -run '^$' -fuzz '^FuzzParseRelease$' -fuzztime 15s + go test ./detectors/java4shell/internal/assess -run '^$' -fuzz '^FuzzAPKOwners$' -fuzztime 15s + go test ./detectors/java4shell/internal/assess -run '^$' -fuzz '^FuzzDpkgOwners$' -fuzztime 15s + go test ./detectors/java4shell/internal/assess -run '^$' -fuzz '^FuzzRun$' -fuzztime 30s + go test ./detectors/java4shell -run '^$' -fuzz '^FuzzReadCrash$' -fuzztime 15s for f in FuzzParseDpkgStatus FuzzParseAPKInstalled FuzzParseHeaderBlob FuzzReadBDB FuzzReadNDB FuzzResolver; do go test ./detectors/php2shell/internal/pkgdb -run '^$' -fuzz "^$f\$" -fuzztime 15s done diff --git a/Dockerfile b/Dockerfile index 0c2aa52..43e7971 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.27.3 AS build +FROM golang:1.27.4 AS build WORKDIR /src COPY go.mod go.sum ./ RUN go mod download diff --git a/README.md b/README.md index 6f9ea5a..678c19e 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,9 @@ One engine for DefectDojo's emerging-vulnerability scanners. Each new vulnerabil | `php2shell` | PHP runtimes vulnerable to CVE-2026-109618 and CVE-2026-109619 ("PHP2Shell"), fixed in PHP 8.2.35, 8.3.36, 8.4.27 and 8.5.12 ([GHSA-w8hw-w9fx-j79w](https://github.com/php/php-src/security/advisories/GHSA-w8hw-w9fx-j79w), [GHSA-6wmf-4rv5-p3rp](https://github.com/php/php-src/security/advisories/GHSA-6wmf-4rv5-p3rp)). A packaged PHP is judged by its vendor's backport data, never by the version it reports. Also checks the images a source tree builds on and deploys, and can rewrite them to fixed images | `fs`, `image`, `repo`, `fix` | | `brotli` | Brotli decoders vulnerable to CVE-2026-111497 ("Node2Shell", "Broccoli2Shell"), Brotli 1.0.3 through 1.2.0, fixed in 1.0.10, 1.1.1 and 1.2.1 ([GHSA-9v8h-qqgm-4m35](https://github.com/advisories/GHSA-9v8h-qqgm-4m35)): system `libbrotlidec` and every copy bundled into other software (Node.js, .NET, Bun, Electron, Envoy and Istio, Python wheels such as Pillow, pyarrow, duckdb, pycurl and PyMuPDF, brotli4j, and more), on Linux, Windows and macOS. A decoder is judged first by the error code the fix added, so vendor backports are recognised without distribution data; a build that strips it by its own version stamp or by its code, which returns that error code only once fixed. In a source tree it finds the Node.js, .NET and Bun releases and the bundling packages a project pins, and `fix` moves each to the fixed release | `fs`, `image`, `repo`, `fix` | | `sshole` | \*sshole, the OpenSSH password authentication bypass on musl libc fixed in OpenSSH 10.6p2 and in Alpine's openssh packages of November 5, 2026 ([release notes](https://www.openssh.org/releasenotes.html#10.6p2), [writeup](https://sshhole.com)). On musl, sshd from 7.3p1 to 10.6p1 compares an account's password field of `*` with the `*` that crypt returns when it fails, so a crafted password logs in to that account. It judges each installed sshd by its C library, its release and the apk database (Alpine backports keep the release number), then checks what the target's `sshd_config` (Include, Match, PasswordAuthentication, AuthenticationMethods, PermitRootLogin, AllowUsers and the rest) and account files make reachable, and reports the accounts. In an image it also reads the command (`sshd -o` and `-f`) and the startup scripts it runs (entrypoints, s6, cont-init.d), and reports a server as exposed when a startup script turns password logins on, sets a `*` field or sets passwords. OpenWrt's opkg database and its backports (23.05, 24.10, 25.12) count like Alpine's. PAM-checked builds (`UsePAM yes`) are not affected. On a live server it reads the banner and the offered methods and never sends a password. In a source tree it finds Dockerfiles that install an OpenSSH server on Alpine without the fix (package lists in `ARG` and `ENV` too), Ansible apk tasks, Buildroot, OpenWrt and Yocto configurations that build one on musl, and anything that sets a password field to `*`. It also pulls the images the tree references (through the guarded transport, with the command a Compose service or Kubernetes container sets) and judges them like `scan image`. `fix` adds the fixed release floor (in the variable when the list is one), moves an end-of-life Alpine base to a tag on the nearest fixed Alpine release (`alpine:3.14` to `3.16`, `python:3.9-alpine3.14` to `3.9-alpine3.16` after checking that image) as a labelled upgrade, adds upstream commit 4353fdc43 as a patch scoped to the OpenSSH versions it applies to (Buildroot `BR2_GLOBAL_PATCH_DIR`, a version-specific Yocto bbappend), sets Ansible tasks to `state: latest`, and changes `*` to `*NP*` | `fs`, `image`, `host`, `repo`, `fix` | +| `java4shell` | Java4Shell (CVE-2026-114938), an out-of-bounds write in HotSpot's AVX-512 VBMI Base64 decoder on x86-64, fixed in OpenJDK 17.0.21.1, 21.0.13.1, 25.0.5.1 and 27.0.1.1 on November 15, 2026 by JDK-8393212 ([commit](https://github.com/openjdk/jdk/commit/5177a70b385c16f26ede191fdac7aaba0bed0f8b); that this change is the CVE's fix is inferred from the public descriptions, since the OpenJDK advisory could not be read: see the [knowledge table](detectors/java4shell/FACTS.md)). Decoding a malformed Base64 input that ends in a padded fragment of one to three characters (64 `A` then `=`) writes up to 64 bytes past the output once the decoder is compiled, on CPUs with AVX-512F, AVX-512BW, AVX-512 VBMI and BMI2 (Intel server CPUs from Ice Lake on and some client CPUs, not Alder Lake or Raptor Lake; AMD Zen 4 and later); [Calif](https://calif.io/java4shell/) showed pre-authentication code execution on default Keycloak, Jira, Confluence, Artifactory, Jenkins and PingFederate. The decoder came with JDK 18 and 17.0.6; OpenJDK 8, 11 and 17.0.0 to 17.0.5 do not have it (Oracle's Java 8 Performance Pack carries it and is reported vulnerable up to 8u511), and Java 18 to 20, 22 to 24 and 26 received no fix. It finds Java runtimes by their VM library (`libjvm.so`, `jvm.dll`, `libjvm.dylib`): installed JDKs and JREs, jlink images, distribution packages (apk, dpkg, and rpm by install path) and runtimes bundled in products (Atlassian, Elasticsearch, Artifactory, JetBrains IDEs and others); a JDK download sitting in the target (OpenJDK21U-jre_…tar.gz and the like) is recorded by name as context, not read or unpacked. Each is judged by what the library proves: its CPU architecture (only x86-64 is affected; each platform of a multi-arch image is judged on its own with `--platform all`), the version compiled into it, and whether it carries the decoder's stubs, which catches a vendor backport into an older line; the `release` file is a second witness: when the library's version line can be read, the library decides and a different release-file version is only noted; a release file naming OpenJ9 next to HotSpot evidence makes the verdict unknown; and a release file alone does not clear a runtime (OpenJ9 is recognised from its own VM library, not from the release file). No marker in the binary separates fixed from unfixed code, so the fix itself is judged by version and build (the first fixed build of each release, such as 21.0.13.1+2; pre-release builds are not judged fixed, and a self-built JDK at a build boundary is unknown). OpenJ9 (IBM Semeru) is not HotSpot and is not affected. `-XX:-UseBASE64Intrinsics` or, on x86-64 only (an ARM JVM refuses it), `-XX:UseAVX=2` or lower in the image's `JAVA_TOOL_OPTIONS`, `JDK_JAVA_OPTIONS` or `_JAVA_OPTIONS`, the container's java command, `CATALINA_OPTS` or `JAVA_OPTS` passed on by catalina.sh or the start script, a product's `setenv.sh`, `jvm.options` or `.vmoptions`, or the systemd unit that starts a product, is recorded as a mitigation on a finding that stays vulnerable (status `mitigated`, counted in the exit status): the options can be overridden where the runtime is deployed, and the runtime still needs the update; `_JAVA_OPTIONS` turning the intrinsic back on undoes it. A filesystem scan records the scanning CPU's AVX-512F, AVX-512BW, AVX-512 VBMI and BMI2 flags and any `hs_err_pid*.log` as context; neither clears a runtime. In a source tree it judges Java images by their tags (eclipse-temurin, amazoncorretto, azul/zulu-openjdk, bellsoft/liberica, sapmachine, openjdk, Microsoft and Oracle images) in Dockerfiles, Compose, Kubernetes, Helm values, CI files and Jib configuration, reads moving tags and product images (Jira, Elasticsearch, Jenkins, Tomcat...) from the registry for linux/amd64, and checks setup-java, `.java-version`, `.sdkmanrc`, `.tool-versions`, mise, Gradle and Maven toolchains, the Maven Enforcer floor, `BP_JVM_VERSION` and the OpenJDK packages a Dockerfile installs, with the mitigation a stage, Compose service or Kubernetes container sets. `fix` moves an image tag to the first published fixed tag of the same variant (`21.0.13_9-jre-alpine` to `21.0.13.1_2-jre-alpine`, or for Corretto, which republishes the update's tag, `21.0.12-al2023` to `21.0.13-al2023`) after reading it and finding the fixed runtime, and a setup-java pin to the major version when the distribution's newest release is fixed (Corretto, SapMachine); everything else gets the exact manual step, and a product image gets the interim `ENV JAVA_TOOL_OPTIONS` line as an optional edit it does not apply | `fs`, `image`, `repo`, `fix` | -The detectors ported from standalone scanners also build as standalone binaries under the names people search for: `emudhra-scan` and `php2shell-scan`. They keep their original commands, flags and exit status rules. `brotli` and `sshole` were written here and run through `earlylight` (`--detector brotli`, `--detector sshole`). +The detectors ported from standalone scanners also build as standalone binaries under the names people search for: `emudhra-scan` and `php2shell-scan`. They keep their original commands, flags and exit status rules. `brotli`, `sshole` and `java4shell` were written here and run through `earlylight` (`--detector brotli`, `--detector sshole`, `--detector java4shell`). ## Install @@ -50,6 +51,10 @@ sudo earlylight scan fs --detector sshole / # this machine's sshd earlylight scan host --detector sshole bastion.example:22 # banner and offered methods; sends no password earlylight scan repo --detector sshole . # Dockerfiles, scripts and Ansible tasks earlylight fix --detector sshole --dry-run . # fixed apk floors, "*" to "*NP*" +earlylight scan image --detector java4shell --platform all eclipse-temurin:21-jre # Java runtimes, per platform +sudo earlylight scan fs --detector java4shell / # the JDKs, JREs and bundled runtimes on this machine +earlylight scan repo --detector java4shell . # Java images, versions, toolchains and packages a tree pins +earlylight fix --detector java4shell --dry-run . # moves to published fixed images, after reading them earlylight scan image --platform all myorg/app:prod # every platform the image has sudo earlylight scan fs / # this machine earlylight scan fs --as-root /host # a host mounted elsewhere @@ -113,6 +118,7 @@ The engine enforces one rule for every detector: a finding can only be `fixed`, | `detectors/php2shell` | Adapter, `verbatim.go` from php2shell-scan's command line, and `internal/` copied verbatim from php2shell-scan | | `detectors/brotli` | Adapter, plus `internal/fingerprint` (what a binary proves: the decoder's error table, its tables and code, exports and version stamps), `internal/assess` (the verdict and fix for one file) and `internal/pins` (pins and bundling packages in a source tree, and their verified edits), which know nothing about the engine | | `detectors/sshole` | Adapter and live-host probe, plus `internal/assess` (what an sshd binary proves, musl's shadow lookup and crypt failures, sshd's salt choice, sshd_config with Include and Match, and the verdict) and `internal/repo` (Dockerfiles, scripts and YAML in a source tree, and their verified edits), which know nothing about the engine | +| `detectors/java4shell` | Adapter, plus `internal/knowledge` (the release data and version judgement, sourced in `FACTS.md`), `internal/jvm` (what a VM library, release file and JVM option source prove), `internal/assess` (the verdict for each runtime: product, package, mitigation) and `internal/repo` (Java images, versions and packages in a source tree, and their verified edits), which know nothing about the engine | | `internal/refs` | Image references in Dockerfiles, Compose, Kubernetes, Helm values and CI files (moved from php2shell, unchanged, so every detector can read them) | | `parity/` | Proves each port gives the same results as its standalone scanner | | `brand/` | The Early Light logo, favicons, avatar and social preview | @@ -127,6 +133,7 @@ Every port must give exactly the verdicts of the scanner it came from before it - `parity/brotli.py` has no standalone scanner to compare with, so it checks every decoder in 38 images against the vendor's own fix status: Node.js, .NET, Bun, Envoy and curl images before and after their October 2026 releases, distribution packages pinned to their exact unfixed and backported builds, and the Python wheels ([BROTLI-RESULTS.md](parity/BROTLI-RESULTS.md)). - `parity/brotli_files.py` checks 73 release binaries and packages, unpacked and never run: Node.js, .NET and Bun for Windows and macOS, seven Electron releases, Envoy and Istio, and the packages that bundle the decoder ([BROTLI-FILES-RESULTS.md](parity/BROTLI-FILES-RESULTS.md)). A WebAssembly build of the decoder is the one case it cannot settle: it is reported as unknown. - `parity/sshole.py` builds ten images from `parity/sshole/` (Alpine with the unfixed and the fixed package, password logins off, UsePAM on, root with PermitRootLogin, a Match on address, the current Alpine release, Debian, an entrypoint that turns password logins on at start, and `sshd -o` in the image command) and checks each verdict against the expected one ([SSHOLE-RESULTS.md](parity/SSHOLE-RESULTS.md)). The expected verdicts come from the upstream fix, musl's crypt and shadow code, and the Alpine package that shipped it. +- `parity/java4shell.py` checks 39 cases against verdicts taken from [FACTS.md](detectors/java4shell/FACTS.md), and that each version came from the VM library (not the release file): Temurin 8, 11, 17.0.5, 17.0.6, 17.0.21 and 21.0.13 on Alpine (musl), a musl library under a forged release file, a fixed release rewritten to the build before the fix (synthetic), 18, 20, 21 (also arm64, all platforms of the image, UBI 9), 25 and 27, Corretto (an Amazon Linux rpm and Alpine, before and after Amazon republished their tags with the fix), Zulu, Liberica, SapMachine, Semeru, Elasticsearch's bundled JDK, the fixed SapMachine 21.0.13.1, a jlink image, bundled product runtimes with and without `setenv.sh`, Alpine, Debian and Ubuntu packages, environment, Tomcat CATALINA_OPTS and setenv.sh mitigations and one that is undone, hostile release files and symlink loops, a filesystem scan and an offline source tree ([JAVA4SHELL-RESULTS.md](parity/JAVA4SHELL-RESULTS.md)). - `parity/php.py` runs php2shell-scan, this repo's php2shell-scan and Early Light over php2shell-scan's validation matrix: official images, distribution packages, source builds and hostile images. Each finding's complete record is compared field by field. It also covers filesystem scans at and below a system root, repository scans, `fix --dry-run`, and every detector at once ([PHP-RESULTS.md](parity/PHP-RESULTS.md)). ## Adding a detector @@ -146,6 +153,8 @@ go test ./internal/archive -run '^$' -fuzz '^FuzzWalk$' -fuzztime 60s go test ./detectors/emudhra/internal/detect -run '^$' -fuzz '^FuzzScan$' -fuzztime 60s go test ./detectors/sshole/internal/assess -run '^$' -fuzz '^FuzzConfig$' -fuzztime 60s go test ./detectors/sshole/internal/repo -run '^$' -fuzz '^FuzzScanText$' -fuzztime 60s +go test ./detectors/java4shell/internal/jvm -run '^$' -fuzz '^FuzzInspect$' -fuzztime 60s +go test ./detectors/java4shell/internal/repo -run '^$' -fuzz '^FuzzScanText$' -fuzztime 60s govulncheck ./... python3 parity/parity.py path/to/emudhra-scan path/to/earlylight python3 parity/php.py path/to/php2shell-scan ./php2shell-scan ./earlylight path/to/emudhra-scan cases.json diff --git a/detectors/all/all.go b/detectors/all/all.go index c1c78a4..7549169 100644 --- a/detectors/all/all.go +++ b/detectors/all/all.go @@ -4,6 +4,7 @@ package all import ( _ "github.com/DefectDojo/earlylight/detectors/brotli" _ "github.com/DefectDojo/earlylight/detectors/emudhra" + _ "github.com/DefectDojo/earlylight/detectors/java4shell" _ "github.com/DefectDojo/earlylight/detectors/php2shell" _ "github.com/DefectDojo/earlylight/detectors/sshole" ) diff --git a/detectors/java4shell/FACTS.md b/detectors/java4shell/FACTS.md new file mode 100644 index 0000000..95d0674 --- /dev/null +++ b/detectors/java4shell/FACTS.md @@ -0,0 +1,94 @@ +# Java4Shell (CVE-2026-114938): what is known, and from where + +Checked 2026-11-17. Every row names its source. "Not fixed yet" means the +source was read on that date and had no fixed release; it is not a guess +about the future. Rows marked **inferred** are reasoning from the sources, +not something a source states. + +## The flaw + +| Fact | Source | +| --- | --- | +| The fix is JDK-8393212 "Base64 optimization improvements", 4 added lines in `generate_base64_decodeBlock()` in `src/hotspot/cpu/x86/stubGenerator_x86_64.cpp`: before the final 1 to 63 byte fragment of the AVX-512 VBMI path, `cmpl(length, 4); jcc(less, L_bruteForce)` ("A padded Base64 tail shorter than one complete block is handled by Java"). Authored 2026-10-13 by Tobias Hartmann, reviewed by rhalade, mschoene (the OpenJDK vulnerability group's reviewers), chagedorn, rcastanedalo | openjdk/jdk commit [`5177a70b385c16f26ede191fdac7aaba0bed0f8b`](https://github.com/openjdk/jdk/commit/5177a70b385c16f26ede191fdac7aaba0bed0f8b), pushed 2026-11-16 | +| What the unfixed code does with a 1 to 3 byte tail that ends in `=`: `output_size` is 0, `L_padding` decrements it to -1, `bzhiq(output_mask, -1, output_size)` with an index of 255 keeps all 64 mask bits, and the masked `evmovdqub` stores 64 bytes at `dest + dp`: a write of up to 64 bytes past the output the caller sized. Read from the stub, and observed (next row); no vendor source states the impact in words | same file, lines around `L_finalBit` and `L_padding` | +| **Observed** on an AMD EPYC 9B14 (Zen 4, with avx512f, avx512bw, avx512vbmi and bmi2), 2026-11-17: after C2 compiles the decoder, `Base64.getDecoder().decode(src, dst)` of a malformed input that ends in a 1 to 3 character fragment with `=` (for example 64 valid characters then `A=`) writes bytes into `dst` past the decoded output. Seen on Temurin 18.0.2.1, 20.0.2, 17.0.6, 17.0.21, 21.0.2, 21.0.13 and 25.0.5; not seen on Temurin 17.0.5 or on SapMachine 21.0.13.1 (the fixed release); not seen on 21.0.13 started with `-XX:-UseBASE64Intrinsics` or with `-XX:UseAVX=2`. On 17.0.x to 20 the input has to be longer than 128 bytes, on 21 and later longer than 31 bytes: the 17u stub enters the AVX-512 path only above 128 bytes (`cmpl(length, 128)`), the 21u stub above 31. With an output array sized by `decode(byte[])` the write goes past the array into the heap. The test program is not in this repository | local run, `eclipse-temurin` images and the SapMachine 21.0.13.1 tarball (sha256 `6cb4df18…949ae9`, matching the GitHub release asset digest) | +| CVE-2026-114938 is listed with CVSS 8.1, component hotspot/compiler, in Amazon Corretto's changelogs of 2026-11-17 | [corretto-17 CHANGELOG 17.0.21.1.2](https://github.com/corretto/corretto-17/blob/17.0.21.1.2/CHANGELOG.md), [corretto-21 21.0.13.1.2](https://github.com/corretto/corretto-21/blob/21.0.13.1.2/CHANGELOG.md), [corretto-25 25.0.5.1.2](https://github.com/corretto/corretto-25/blob/25.0.5.1.2/CHANGELOG.md), [corretto-27 27.0.1.1.4](https://github.com/corretto/corretto-27/blob/27.0.1.1.4/CHANGELOG.md) | +| CVE-2026-114938 is JDK-8393212. The Corretto changelogs list CVEs by component, not by JDK bug, and the OpenJDK advisory that states the mapping could not be read (HTTP 403 for the lead, unreachable here). The public writeups (lead's sources below) describe exactly the code 8393212 changes: the output mask computed from length and padding, `output_len` underflowing on a padding-only tail, `bzhiq(-1, output_len)` giving all ones and `evmovdqub` writing all 64 bytes. **Inferred from that match**, not stated by a source | Corretto changelogs; Calif and Semgrep writeups as relayed by the lead | +| The vulnerable path runs only when the CPU reports AVX-512 VBMI, AVX-512BW and BMI2 and HotSpot keeps them enabled: `if (VM_Version::supports_avx512_vbmi() && VM_Version::supports_avx512bw() && VM_Version::supports_bmi2())`. AVX-512 features need AVX-512F, so the four CPU flags are `avx512f avx512bw avx512vbmi bmi2` | stubGenerator_x86_64.cpp at the fix commit | +| MIME decoding (`isMIME`) skips the AVX-512 path; standard and URL-safe decoding use it | same function, `cmpl(isMIME, 0)` | +| `-XX:UseAVX=0/1/2` clears every AVX-512 feature, VBMI included, so the path is not generated | `src/hotspot/cpu/x86/vm_version_x86.cpp`, `if (UseAVX < 3)` block (jdk17u-dev and jdk HEAD) | +| `-XX:-UseBASE64Intrinsics` stops the stub being generated at all: `if (UseBASE64Intrinsics) { ... StubRoutines::_base64_decodeBlock = generate_base64_decodeBlock(); }` | stubGenerator_x86_64.cpp | +| Only x86-64 HotSpot has this stub. AArch64, PPC and RISC-V have their own decode intrinsics (8256245, 8248188, 8314124) with different code. 32-bit x86 (`stubGenerator_x86_32.cpp`) has no decode intrinsic | jdk17u-dev and jdk21u-dev trees; `git log --grep` in openjdk/jdk | +| OpenJ9 (IBM Semeru) does not contain HotSpot: its `lib/server/libjvm.so` is a 97 KB redirector and the VM is `libj9vm29.so`. Whether OpenJ9 has a comparable bug of its own is **not known** | `ibm-semeru-runtimes:open-21-jre` image, release file `JVM_VARIANT="Openj9"` | + +## From the public writeups (relayed by the lead, 2026-11-17) + +The lead read these; they could not be fetched from the research host. Facts here are as the lead reported them. + +| Fact | Source | +| --- | --- | +| Named Java4Shell, credited to Calif. CVSS 9.2 (Corretto's changelogs list 8.1; the detector reports 9.2, the discoverer's score) | [Calif advisory](https://calif.io/java4shell/), [Semgrep blog](https://semgrep.dev/blog/2026/java4shell-openjdk-base64-vulnerability) | +| Trigger: 64 `A` then `=` (on 17: 128 `A` then `=`); a 65-byte input decoded into a 50-byte buffer is written 62 bytes past its end. Reached only after the intrinsic is JIT-compiled. Matches what was observed here (65 and 129 byte inputs) | same | +| Pre-authentication remote code execution shown on default configurations of Keycloak, Jira, Confluence, JFrog Artifactory, Jenkins and PingFederate; post-authentication on Kafka, Solr, Elasticsearch, Gerrit and Spinnaker; Struts and Spring Boot sample apps; and a client-side variant (a Java client opening TLS to a malicious server). Chain details withheld | same, and posts by Charles Carmakal (Mandiant) and Semgrep | +| Affected: OpenJDK 17.0.6 and later on x86-64 CPUs with avx512f, avx512bw, avx512vbmi and bmi2; ARM64 not affected; every distribution. Oracle Java SE builds named as affected: **8u511b33-perf**, 17.0.21.0.3, 21.0.13.0.3, 25.0.5.0.3, 27.0.1.0.3 (for 8, only the Performance Pack build). The 8u-perf build carries a backport of the decoder; **no fixed 8u-perf version is known**. The detector reports a Java 8 VM library that carries the decoder's stubs as vulnerable up to update 511 and unknown above it | same; Oracle alert cspunov2026 (403 for the lead) | +| Oracle and OpenJDK released fixes on 2026-11-17 (Oracle alert "cspunov2026"); the lead had no fixed versions per line. They are established above from the OpenJDK repositories and vendor releases | lead | +| Mitigations: `-XX:-UseBASE64Intrinsics` (preferred, for example through `JAVA_TOOL_OPTIONS`), or `-XX:UseAVX=2` on x86-64 only: **an ARM JVM refuses to start with it** | same | +| CPU check: `grep -m1 '^flags' /proc/cpuinfo \| tr ' ' '\n' \| grep -E '^(avx512f\|avx512bw\|avx512vbmi\|bmi2)$'` (all four needed); Kubernetes node label `feature.node.kubernetes.io/cpu-cpuid.AVX512VBMI: "true"` (Node Feature Discovery) | Calif | +| Indicators (not proof): `hs_err_pid*.log` crash files and bursts of `IllegalArgumentException` from `java.util.Base64$Decoder`. No network signature or IOCs published | same | +| SCA and dependency scanners do not report it: it is in the JDK runtime, not a library | same | + +## Where the vulnerable path came from + +| Line | First release with the AVX-512 VBMI decode path | Source | +| --- | --- | --- | +| mainline | JDK 18 (8268276 "Base64 Decoding optimization for x86 using AVX-512", Scott Gibbons, 2021-06-25, after JDK 17 forked) | openjdk/jdk [`c37988d0793`](https://github.com/openjdk/jdk/commit/c37988d0793b24d98d285530dfda69999a227937); `jdk-18+36` contains it, `jdk-17+35` does not | +| 17 | 17.0.6 (backport 2022-11-09, first tag `jdk-17.0.6+3`, first GA `jdk-17.0.6-ga`). 17.0.0 to 17.0.5 have no x86 decode intrinsic | openjdk/jdk17u-dev `07050b000c` ("Backport-of: c37988d0793b") | +| 11 | never: jdk11u-dev has no Base64 decode intrinsic (only 8205528, AVX-512 encode) | openjdk/jdk11u-dev `git log --grep base64` | +| 8 | never: jdk8u-dev has no Base64 intrinsics | openjdk/jdk8u-dev `git log --grep base64` | +| vendor builds of 8, 11 or 17.0.0-17.0.5 | Oracle's Java 8 Performance Pack (8u511b33-perf) carries the decoder (see above); other vendor backports are **not known**. The detector checks the libjvm for the VBMI decode stubs' names: a Java 8 build that carries them is vulnerable up to update 511 and unknown above it; an 11 or 17.0.0-17.0.5 build that carries them is unknown | lead's sources | + +## Fixed releases + +| Line | Fixed in | Source | +| --- | --- | --- | +| OpenJDK 17 | **17.0.21.1** (`jdk-17.0.21.1+2`, `jdk-17.0.21.1-ga`, 2026-11-15) | openjdk/jdk17u branch `jdk17.0.21.1`, commit `8ea73158af` | +| OpenJDK 21 | **21.0.13.1** (`jdk-21.0.13.1+2`, `-ga`, 2026-11-15) | openjdk/jdk21u branch `jdk21.0.13.1`, commit `8e115fb5c7` | +| OpenJDK 25 | **25.0.5.1** (`jdk-25.0.5.1+2`, `-ga`, 2026-11-15) | openjdk/jdk25u branch `jdk25.0.5.1`, commit `25bb7d35dc` | +| OpenJDK 27 | **27.0.1.1** (`jdk-27.0.1.1+4`, `-ga`) | openjdk/jdk27u branch `jdk27.0.1.1`, commit `564fc7de7e`; master also has it. The `jdk27.0.2` branch does **not** have it yet | +| Builds of the fixed releases before the fix landed | **not fixed**: `jdk-17.0.21.1+0` (2026-10-27) and `+1` (2026-11-02), `jdk-21.0.13.1+0` and `+1`, `jdk-25.0.5.1+0` and `+1` do not contain the `cmpl(length, 4)` guard; `+2` (2026-11-15) does. `jdk-27.0.1.1+0` does not; `jdk-27.0.1.1+4` does (27u has no `+1` to `+3` tags). The detector's fixed build per line: 17.0.21.1+2, 21.0.13.1+2, 25.0.5.1+2, 27.0.1.1+4; a version named without its build (a GA tag, a version file) is taken as the GA build. Pre-release and self-built JDKs (`-ea`, `-beta`, `-internal`, `adhoc`) are never judged fixed | `git show :src/hotspot/cpu/x86/stubGenerator_x86_64.cpp` in openjdk/jdk17u, jdk21u, jdk25u, jdk27u, checked 2026-11-18 | +| OpenJDK 26 | **no fix**: last release 26.0.2.1 (2026-07), `jdk26u` master unchanged since 2026-08-18 and without the fix | openjdk/jdk26u | +| 18, 19, 20, 22, 23, 24 | **no fix** (end of life) | n/a | +| JDK 28 (early access) | fix pushed to mainline 2026-11-16; `jdk-28+24` does **not** have it; no later build tagged yet | openjdk/jdk tags | +| 17.0.22, 21.0.14, 25.0.6 (January 2027 updates) | **not known**: the -dev repositories (HEAD of 2026-11-17, tags up to `+2`) do not have the fix yet. Embargoed fixes usually reach -dev at the next release; that is an expectation, not a fact, so these versions are reported as unknown | openjdk/jdk17u-dev, jdk21u-dev, jdk25u-dev HEAD | +| 17.0.21.0.2 / .0.3, 21.0.13.0.2 / .0.3, 25.0.5.0.2 / .0.3, 27.0.1.0.2 / .0.3 | **not fixed**: these October 2026 out-of-band releases carry CA changes only (8395142, 8395690) | -dev repo tags `jdk-*.0.3+*` | + +## Vendors and distributions (2026-11-17, rechecked 2026-11-18) + +| Vendor | Status | Source | +| --- | --- | --- | +| Amazon Corretto | fixed: 17.0.21.1.2, 21.0.13.1.2, 25.0.5.1.2, 27.0.1.1.4 (all 2026-11-17, each "Import jdk-…1+2/+4", CVE-2026-114938 listed). Corretto 8 and 11: no release after October (not affected). **Docker Hub, 2026-11-18:** Amazon republished the existing `amazoncorretto:21.0.13-*` and `17.0.21-*` tags with the fixed builds (the images report 21.0.13.1+2 and 17.0.21.1+2); there are no `.1` tags. A Corretto tag of the fixed update therefore names whichever build was pushed last, and the detector reads the image | GitHub releases API, CHANGELOG.md of each tag; images scanned by this detector (parity cases corretto-*) | +| SapMachine | releases 17.0.21.1, 21.0.13.1, 25.0.5.1, 27.0.1.1 published 2026-11-17 (the release page names no CVE) | GitHub releases API, SAP/SapMachine | +| Eclipse Temurin | **not fixed yet**: no `jdk-17.0.21.1+2`, `jdk-21.0.13.1+2`, `jdk-25.0.5.1+2` or `jdk-27.0.1.1+4` release in adoptium/temurinNN-binaries; Docker Hub `eclipse-temurin` newest tags are 17.0.21_9, 21.0.13_9, 25.0.5_8, 27.0.1.0 | GitHub API, registry-1.docker.io tag list | +| Azul Zulu, BellSoft Liberica, Microsoft, Oracle | **not checked**: their sites could not be reached from the research host. Docker Hub `azul/zulu-openjdk` and `bellsoft/liberica-openjdk-debian` newest tags are 27.0.1.0.3 / 21.0.13.0.3 (not fixed) | registry-1.docker.io tag list | +| IBM Semeru | OpenJ9, not HotSpot (see above) | image inspection | +| Debian | **not fixed yet**: newest sources openjdk-17 17.0.21.0.3+1-1, openjdk-21 21.0.13.0.3+1-1, openjdk-25 25.0.5.0.3+1-3; security.debian.org 17.0.21+9-1~deb12u1, 21.0.13+9-1~deb13u2, 25.0.5+8-1~deb13u1. The security tracker has no page for CVE-2026-114938 | deb.debian.org and security.debian.org pools, security-tracker.debian.org | +| Ubuntu | **not fixed yet**: newest sources openjdk-17 17.0.21.0.3+1-1~XX.04.1, openjdk-25 25.0.5.0.3+1-3~XX.04.1 (openjdk-21 listing timed out) | archive.ubuntu.com pool | +| Alpine | **not fixed yet**: community openjdk17 17.0.21.0.3_p1-r0, openjdk21 21.0.13.0.3_p1-r0, openjdk25 25.0.5.0.3_p1-r0/-r1 (3.23, 3.24, edge); 3.21 and 3.22 carry older 17/21 | dl-cdn.alpinelinux.org APKINDEX | +| Red Hat (RHEL, UBI) | **not checked**: access.redhat.com and registry.access.redhat.com unreachable | n/a | + +## Advisory links + +The detector's catalog entry links only pages someone on this work could read: the Calif advisory and the Semgrep post (read by +the lead), the OpenJDK fix commit and Amazon Corretto's changelog. **Not read by anyone (HTTP 403 or unreachable):** the OpenJDK +advisory https://openjdk.org/groups/vulnerability/advisories/2026-11-17 and Oracle's alert +https://www.oracle.com/security-alerts/cspunov2026.html; they are listed here only. + +## How the detector uses this + +- The version that decides is the one inside the libjvm (` () for - JRE ()`), read from the binary, with the + `release` file as a second witness; the CPU architecture comes from the ELF, PE or Mach-O header. +- A packaged JDK (apk, dpkg, rpm) is judged by the same upstream version: no distribution has published a fixed package or a backport + statement yet. When one does, its fixed package version goes into the knowledge table. +- No marker in the binary separates fixed from unfixed code: the stub is generated at run time by C++ code, and the fix only adds a compare + and a branch to that generator. The stubs' names (`lookup_lo_base64`, `pack_vec_base64`, `join_0_1_base64`) do show that the AVX-512 + decode path is present, which is how a vendor backport into an older line is caught. diff --git a/detectors/java4shell/context.go b/detectors/java4shell/context.go new file mode 100644 index 0000000..a0d5041 --- /dev/null +++ b/detectors/java4shell/context.go @@ -0,0 +1,87 @@ +package java4shell + +import ( + "regexp" + "runtime" + "strings" + "unicode/utf8" + + "golang.org/x/sys/cpu" +) + +// CPU is the CPU this scan ran on, as context. The vulnerable decoder runs +// only on a CPU with AVX-512F, AVX-512BW, AVX-512 VBMI and BMI2, but a +// runtime that does not run it here can run it on another host (containers +// and virtual machines move), so a CPU without them clears nothing. +type CPU struct { + Arch string `json:"arch"` + AVX512F bool `json:"avx512f"` + AVX512BW bool `json:"avx512bw"` + AVX512VBMI bool `json:"avx512vbmi"` + BMI2 bool `json:"bmi2"` + Affected bool `json:"runs_vulnerable_decoder"` + Note string `json:"note"` +} + +// thisCPU reads the scanning machine's CPU features (CPUID, with the +// operating system's AVX-512 state support checked). +func thisCPU() (CPU, bool) { + if runtime.GOARCH != "amd64" { + return CPU{Arch: runtime.GOARCH, Note: "this scan ran on a CPU that is not x86-64; that says nothing about where the scanned runtimes run"}, true + } + c := CPU{Arch: "x86-64", AVX512F: cpu.X86.HasAVX512F, AVX512BW: cpu.X86.HasAVX512BW, AVX512VBMI: cpu.X86.HasAVX512VBMI, BMI2: cpu.X86.HasBMI2} + c.Affected = c.AVX512F && c.AVX512BW && c.AVX512VBMI && c.BMI2 + if c.Affected { + c.Note = "this CPU has AVX-512F, AVX-512BW, AVX-512 VBMI and BMI2: a vulnerable runtime started here generates the vulnerable decoder unless its options turn it off" + } else { + c.Note = "this CPU lacks at least one of AVX-512F, AVX-512BW, AVX-512 VBMI and BMI2, so a vulnerable runtime started here does not generate the vulnerable decoder; this is context only, not a clearance: the same runtime or image runs it on a host that has them" + } + return c, true +} + +// crash is a HotSpot crash log (hs_err_pid*.log), as context: the Java +// version and CPU of a past crash, and whether the crashing frame was the +// Base64 decoder. +type crash struct { + Path string `json:"path"` + Version string `json:"java_version,omitempty"` + CPUVBMI bool `json:"cpu_avx512vbmi"` + Frame string `json:"problematic_frame,omitempty"` + Base64 bool `json:"base64_decoder_frame"` +} + +var ( + jreLine = regexp.MustCompile(`(?m)^# JRE version: [^\n(]*\(([0-9][^)\s]{0,60})\)`) + frameLine = regexp.MustCompile(`(?m)^# Problematic frame:\s*\n#\s*([^\n]{1,300})`) + cpuLine = regexp.MustCompile(`(?m)^CPU:[^\n]*`) +) + +func readCrash(p string, b []byte) crash { + c := crash{Path: p} + if m := jreLine.FindSubmatch(b); m != nil { + c.Version = clip(string(m[1]), 61) + } + if m := frameLine.FindSubmatch(b); m != nil { + c.Frame = clip(strings.TrimSpace(string(m[1])), 300) + lf := strings.ToLower(c.Frame) + c.Base64 = strings.Contains(lf, "base64") || strings.Contains(lf, "decodeblock") + } + if m := cpuLine.Find(b); m != nil { + c.CPUVBMI = strings.Contains(string(m), "avx512_vbmi") || strings.Contains(string(m), "avx512vbmi") + } + return c +} + +// clip bounds untrusted crash-log text in bytes (the patterns count +// characters), keeping it valid UTF-8. +func clip(s string, n int) string { + s = strings.ToValidUTF8(s, "?") + if len(s) <= n { + return s + } + s = s[:n] + for len(s) > 0 && !utf8.ValidString(s) { + s = s[:len(s)-1] + } + return s +} diff --git a/detectors/java4shell/crash_internal_test.go b/detectors/java4shell/crash_internal_test.go new file mode 100644 index 0000000..559fe39 --- /dev/null +++ b/detectors/java4shell/crash_internal_test.go @@ -0,0 +1,13 @@ +package java4shell + +import "testing" + +func FuzzReadCrash(f *testing.F) { + f.Add([]byte("# JRE version: OpenJDK Runtime Environment (21.0.13+9) (build 21.0.13+9-LTS)\n# Problematic frame:\n# v ~StubRoutines::decodeBlock\nCPU: total 8, avx512f, avx512_vbmi\n")) + f.Fuzz(func(t *testing.T, b []byte) { + c := readCrash("/tmp/hs_err_pid1.log", b) + if len(c.Frame) > 300 || len(c.Version) > 61 { + t.Fatalf("unbounded field: %d %d", len(c.Frame), len(c.Version)) + } + }) +} diff --git a/detectors/java4shell/internal/assess/assess.go b/detectors/java4shell/internal/assess/assess.go new file mode 100644 index 0000000..8788525 --- /dev/null +++ b/detectors/java4shell/internal/assess/assess.go @@ -0,0 +1,537 @@ +// Package assess decides what each Java runtime in a filesystem or image is +// with respect to Java4Shell (CVE-2026-114938): vulnerable, fixed, not +// affected, mitigated or unknown, with the reason. It knows nothing about +// the engine. +package assess + +import ( + "fmt" + "path" + "sort" + "strings" + + "github.com/DefectDojo/earlylight/detectors/java4shell/internal/jvm" + "github.com/DefectDojo/earlylight/detectors/java4shell/internal/knowledge" +) + +// Status is a verdict. +type Status string + +const ( + Vulnerable Status = "vulnerable" + Fixed Status = "fixed" + NotAffected Status = "not-affected" + // Mitigated: vulnerable code, kept from running by JVM options the + // target sets. It still needs the update. + Mitigated Status = "mitigated" + Unknown Status = "unknown" +) + +// Finding kinds. +const ( + KindRuntime = "runtime" + KindBundled = "bundled-runtime" +) + +// Finding is the verdict on one Java runtime. +type Finding struct { + Kind string `json:"kind"` + Path string `json:"path"` // the runtime directory + Status Status `json:"status"` + Reason string `json:"reason"` + Message string `json:"message"` + Fix string `json:"fix"` + // Version is the version that decided, and where it was read. + Version string `json:"version,omitempty"` + VersionFrom string `json:"version_from,omitempty"` // vm-library, release-file + // ReleaseVersion is the release file's version when it differs from + // the VM library's. + ReleaseVersion string `json:"release_version,omitempty"` + Arch jvm.Arch `json:"arch,omitempty"` + VM string `json:"vm,omitempty"` + Vendor string `json:"vendor,omitempty"` + Library *jvm.Library `json:"library,omitempty"` + JLinked bool `json:"jlinked,omitempty"` + Product string `json:"product,omitempty"` + ProductDir string `json:"product_dir,omitempty"` + Package *Package `json:"package,omitempty"` + Mitigation string `json:"mitigation,omitempty"` + MitigatedFor []string `json:"mitigated_for,omitempty"` // partial mitigations (one service) + Release knowledge.Status `json:"release_status,omitempty"` +} + +// Input is everything one target gives. +type Input struct { + Libraries []*jvm.Library + // Releases maps a runtime directory to its parsed release file; + // BadReleases to why one was not used. + Releases map[string]jvm.Release + BadReleases map[string]string + Settings []jvm.Setting + // Env and Command are the image's configuration (nil for a + // filesystem). Scripts lists, for each startup script kept, the script + // variables it passes on to java. + Env []string + Command []string + Scripts map[string]map[string]bool + // Owners maps installed files to the OS package that installed them. + Owners map[string]Package + // Resolve follows symbolic links within the target. + Resolve func(string) string +} + +// Run judges every runtime in the target. +func Run(in Input) []Finding { + byRoot := map[string][]*jvm.Library{} + var loose []*jvm.Library + for _, l := range in.Libraries { + root, ok := jvm.Root(l.Path) + if !ok { + loose = append(loose, l) + continue + } + byRoot[root] = append(byRoot[root], l) + } + roots := make([]string, 0, len(byRoot)) + for r := range byRoot { + roots = append(roots, r) + } + sort.Strings(roots) + env := jvm.EnvMap(in.Env) + var out []Finding + for _, r := range roots { + out = append(out, withFix(judge(in, env, r, byRoot[r]))) + } + for _, l := range loose { + out = append(out, withFix(judge(in, env, path.Dir(l.Path), []*jvm.Library{l}))) + } + return out +} + +func withFix(f Finding) Finding { + v, ok := knowledge.Parse(f.Version) + f.Fix = fixText(f, v, ok) + return f +} + +// primary picks the library that decides: a HotSpot server VM first. +func primary(libs []*jvm.Library) *jvm.Library { + best := libs[0] + score := func(l *jvm.Library) int { + s := 0 + if l.HotSpot() { + s += 4 + } + if l.Variant == "server" { + s += 2 + } + if l.VMVersion != "" { + s++ + } + return s + } + for _, l := range libs[1:] { + if score(l) > score(best) { + best = l + } + } + return best +} + +func judge(in Input, env map[string]string, root string, libs []*jvm.Library) Finding { + lib := primary(libs) + rel, hasRel := in.Releases[root] + if !hasRel && path.Base(root) == "jre" { + // Java 8 JDKs keep the release file one level up. + rel, hasRel = in.Releases[path.Dir(root)] + } + f := Finding{Kind: KindRuntime, Path: root, Library: lib, Arch: firstArch(lib)} + if hasRel { + f.Vendor = rel["IMPLEMENTOR"] + f.JLinked = rel.JLinked() + } + if name, dir, ok := Product(root); ok { + f.Kind, f.Product, f.ProductDir = KindBundled, name, dir + } + if p, ok := owner(in, root, lib); ok { + f.Package = &p + } + // OpenJ9 only from an OpenJ9 library (by name, or the redirector with + // no HotSpot version line and no decoder stubs): the release file's + // JVM_VARIANT alone does not decide it. + relJ9 := hasRel && rel.OpenJ9() + openj9, stubs := false, false + for _, l := range libs { + if l.OpenJ9 { + openj9 = true + } + if l.VBMIStubs { + stubs = true + } + } + + // The version: the VM library's own, the release file's otherwise. + verText, from := "", "" + if lib.HotSpot() { + verText, from = lib.JREVersion, "vm-library" + } + if verText == "" && hasRel { + verText, from = rel.Version(), "release-file" + } + v, vok := knowledge.Parse(verText) + if vok { + f.Version, f.VersionFrom = verText, from + } + if hasRel && from == "vm-library" { + if rv, ok := knowledge.Parse(rel.Version()); ok && knowledge.Compare(rv.Nums, v.Nums) != 0 { + f.ReleaseVersion = rel.Version() + } + } + f.VM = lib.VMName + if openj9 { + f.VM = "OpenJ9" + } + what := describe(f, v, vok) + + hotspot := false + for _, l := range libs { + if l.HotSpot() { + hotspot = true + } + } + switch { + case (openj9 || relJ9) && (hotspot || stubs): + return f.set(Unknown, "the runtime's files contradict each other: a VM library carries HotSpot's version line or decoder stubs, but the release file or another VM library names OpenJ9", + what+": HotSpot and OpenJ9 evidence disagree") + case openj9: + return f.set(NotAffected, "an OpenJ9 VM: the vulnerable decoder is HotSpot code that OpenJ9 does not contain", + what+" runs the OpenJ9 VM, not HotSpot") + case len(lib.Archs) == 0 || len(lib.Archs) == 1 && lib.Archs[0] == jvm.Other: + return f.set(Unknown, "the VM library's CPU architecture could not be read", what+": CPU architecture unknown") + case !lib.Has(jvm.X8664): + return f.set(NotAffected, fmt.Sprintf("an %s runtime: only x86-64 HotSpot has the AVX-512 VBMI decoder", lib.Archs[0]), + fmt.Sprintf("%s is built for %s, which Java4Shell does not affect", what, lib.Archs[0])) + case !vok: + why := "neither the VM library nor a release file states the Java version" + if b, ok := in.BadReleases[root]; ok { + why += " (" + b + ")" + } + return f.set(Unknown, why, what+": Java version unknown") + } + ks, kwhy := knowledge.Judge(v) + f.Release = ks + read := lib.HotSpot() // the stub names are only meaningful in a HotSpot library that was read + if !read && ks != knowledge.Vulnerable { + // Only the release file states the version, and a release file is + // text anyone can write: it never clears a runtime alone. + why := fmt.Sprintf("the VM library's version line could not be read, and the release file alone (Java %s) does not prove the runtime is safe", v) + if lib.VBMIStubs { + why = fmt.Sprintf("the VM library carries the AVX-512 VBMI Base64 decoder, but its version line could not be read; the release file alone (Java %s) does not prove the fix", v) + } + return f.set(Unknown, why, what+": version not readable from the VM library") + } + switch ks { + case knowledge.NotAffected: + if read && lib.VBMIStubs { + bs, bwhy := knowledge.JudgeBackport(v) + f.Release = bs + if bs == knowledge.Vulnerable { + if ok, how := mitigation(in, env, root, v.Feature(), &f); ok { + f.Mitigation = how + return f.set(Mitigated, bwhy+"; "+how, what+" carries a backported AVX-512 VBMI Base64 decoder without the fix, mitigated by its JVM options until it is updated") + } + return f.set(Vulnerable, bwhy, what+" carries a backported AVX-512 VBMI Base64 decoder and is vulnerable to Java4Shell") + } + return f.set(Unknown, bwhy, what+" carries the AVX-512 VBMI Base64 decoder of later releases") + } + if !read { + return f.set(NotAffected, kwhy+" (judged by the release file: no HotSpot version line was found in the VM library)", what+" is not affected by Java4Shell") + } + return f.set(NotAffected, kwhy+"; its VM library has no AVX-512 VBMI decode stubs", what+" is not affected by Java4Shell") + case knowledge.Fixed: + return f.set(Fixed, kwhy, what+" is at or past the first release with the Java4Shell fix (judged by its version)") + case knowledge.Unknown: + return f.set(Unknown, kwhy, what+": not known whether it has the Java4Shell fix") + } + // The release data says vulnerable. + if read && !lib.VBMIStubs { + return f.set(Unknown, fmt.Sprintf("%s, but this VM library does not carry the AVX-512 VBMI Base64 decode stubs, which the affected builds checked so far all carry: it may be built without them; check it", kwhy), + what+": release says vulnerable, VM library lacks the vulnerable decoder") + } + if !read { + kwhy += " (judged by the release file: no HotSpot version line was found in the VM library)" + } + if ok, how := mitigation(in, env, root, v.Feature(), &f); ok { + f.Mitigation = how + return f.set(Mitigated, kwhy+"; "+how, what+" is vulnerable to Java4Shell, mitigated by its JVM options until it is updated") + } + return f.set(Vulnerable, kwhy, what+" is vulnerable to Java4Shell") +} + +func (f Finding) set(s Status, reason, msg string) Finding { + f.Status, f.Reason, f.Message = s, reason, msg + return f +} + +func firstArch(l *jvm.Library) jvm.Arch { + if l.Has(jvm.X8664) { + return jvm.X8664 + } + if len(l.Archs) > 0 { + return l.Archs[0] + } + return "" +} + +// describe names a runtime in a message: "Java 21.0.13+9 (Eclipse +// Adoptium) bundled in Atlassian Jira". +func describe(f Finding, v knowledge.Version, ok bool) string { + s := "Java runtime" + if ok { + s = "Java " + v.Raw + } + if f.Vendor != "" { + s += " (" + f.Vendor + ")" + } + if f.JLinked { + s += ", a jlink image," + } + if f.Product != "" { + s += " bundled in " + f.Product + } + return s +} + +// owner finds the package that installed the runtime. +func owner(in Input, root string, lib *jvm.Library) (Package, bool) { + for _, p := range []string{lib.Path, root + "/release"} { + cands := []string{p} + if in.Resolve != nil { + if r := in.Resolve(p); r != "" && r != p { + cands = append(cands, r) + } + } + for _, c := range cands { + if o, ok := in.Owners[c]; ok { + return o, true + } + } + } + // Debian's runtime directory is reached through symlinks into + // /usr/lib/jvm/java-17-openjdk-amd64; the list names the real path. + for k, o := range in.Owners { + if strings.HasPrefix(k, root+"/") { + return o, true + } + } + return rpmFromPath(root) +} + +// mitigation decides whether JVM options the target sets keep the +// vulnerable decoder from being generated for this runtime. Mitigations +// that cover only some launches are recorded on f, not counted. +func mitigation(in Input, env map[string]string, root string, feature int, f *Finding) (bool, string) { + // Settings files of the product the runtime is bundled in. + var o jvm.Options + var b64, avx, argfile bool + if in.Env != nil { + if v, ok := env["JAVA_TOOL_OPTIONS"]; ok { + o.Apply(v, "the image's JAVA_TOOL_OPTIONS") + } + if v, ok := env["JDK_JAVA_OPTIONS"]; ok && feature >= 9 { + o.Apply(v, "the image's JDK_JAVA_OPTIONS") + } + // Script variables count when the container's command passes them on. + // They reach only the JVM the script starts: the one JAVA_HOME or + // JRE_HOME names, or the java on PATH. + vars := scriptVarsOf(in) + for _, n := range jvm.ScriptVars { + if v, ok := env[n]; ok && vars[n] && javaHomeIs(in, env, root) { + o.Apply(v, "the image's "+n+" (passed on by the container's start script)") + } + } + if opts, ok := jvm.JavaOptions(in.Command); ok && commandRuns(in, env, root) { + for _, w := range opts { + o.ApplyWord(w, "the container's command") + } + } + // Words that turn the decoder back on count wherever the command + // starts java (a "sh -c" script, a java this scan cannot place): + // a mitigation has to hold for every launch. + b64, avx, argfile = jvm.Undoes(jvm.CommandWords(in.Command)) + if argfile { + return false, "" + } + } + for _, s := range in.Settings { + if s.Kind == "systemd" || s.Scope == "" || s.WindowsOnly && (f.Library == nil || f.Library.Format != "pe") { + continue + } + if within(root, s.Scope) || in.Env != nil && launches(in, env, s) && javaHomeIs(in, env, root) { + s.ApplyTo(&o, feature) + } + } + if b64 { + o.ApplyWord("-XX:+UseBASE64Intrinsics", "the container's command") + } + if avx { + o.ApplyWord("-XX:UseAVX=3", "the container's command") + } + if in.Env != nil { + if v, ok := env["_JAVA_OPTIONS"]; ok { + o.Apply(v, "the image's _JAVA_OPTIONS") + } + } + if ok, how := o.Mitigates(); ok { + return true, how + } + // systemd units: they cover the service, and the runtime only when it + // is bundled in the product the service starts. + for _, s := range jvm.MergeUnits(in.Settings) { + so := s.Options(feature) + ok, how := so.Mitigates() + if !ok || s.Exec == "" { + continue + } + exec := s.Exec + if in.Resolve != nil { + if r := in.Resolve(exec); r != "" { + exec = r + } + } + if f.ProductDir != "" && within(exec, f.ProductDir) { + return true, how + " for the " + s.Unit + " service, which starts " + f.Product + } + if within(exec, root) { + f.MitigatedFor = append(f.MitigatedFor, s.Unit) + } + } + return false, "" +} + +// commandRuns reports whether the container's command starts this +// runtime's java: by path, or by name through PATH. +func commandRuns(in Input, env map[string]string, root string) bool { + for _, w := range in.Command { + if path.Base(w) != "java" { + continue + } + p := w + if !strings.Contains(w, "/") { + for _, d := range strings.Split(env["PATH"], ":") { + if d != "" && (d == root+"/bin" || in.Resolve != nil && in.Resolve(d+"/java") == in.Resolve(root+"/bin/java")) { + return true + } + } + return false + } + if in.Resolve != nil { + p = in.Resolve(p) + } + return within(p, root) + } + return false +} + +// launches reports whether the container's command starts the product a +// settings file configures: its own start script, or catalina.sh of the +// CATALINA_HOME or CATALINA_BASE it sits in. +func launches(in Input, env map[string]string, s jvm.Setting) bool { + for _, w := range in.Command { + if !strings.Contains(w, "/") { + if path.Base(w) == "catalina.sh" && (env["CATALINA_HOME"] == s.Scope || env["CATALINA_BASE"] == s.Scope) { + return true + } + continue + } + p := w + if in.Resolve != nil { + p = in.Resolve(w) + } + if within(p, s.Scope) { + return true + } + } + return false +} + +// javaHomeIs reports whether a start script would run this runtime: the +// one JAVA_HOME or JRE_HOME names, or without them the java on PATH. +func javaHomeIs(in Input, env map[string]string, root string) bool { + res := func(p string) string { + if in.Resolve != nil { + return in.Resolve(p) + } + return p + } + for _, k := range []string{"JRE_HOME", "JAVA_HOME"} { + if h := strings.TrimSuffix(env[k], "/"); h != "" { + return h == root || res(h) == res(root) + } + } + for _, d := range strings.Split(env["PATH"], ":") { + if d != "" && res(d+"/java") == res(root+"/bin/java") { + return true + } + } + return false +} + +// scriptVarsOf lists the script variables the container's command passes +// on: catalina.sh passes CATALINA_OPTS and JAVA_OPTS, and a startup script +// passes the ones it names. +func scriptVarsOf(in Input) map[string]bool { + out := map[string]bool{} + for _, w := range in.Command { + if path.Base(w) == "catalina.sh" { + out["CATALINA_OPTS"], out["JAVA_OPTS"] = true, true + } + p := w + if in.Resolve != nil { + p = in.Resolve(w) + } + for k := range in.Scripts[p] { + out[k] = true + } + for k := range in.Scripts[w] { + out[k] = true + } + } + return out +} + +func within(p, dir string) bool { + return p == dir || strings.HasPrefix(p, strings.TrimSuffix(dir, "/")+"/") +} + +// fixText is what to do. +func fixText(f Finding, v knowledge.Version, ok bool) string { + switch f.Status { + case Fixed, NotAffected: + return "" + } + interim := " Until then, start it with -XX:-UseBASE64Intrinsics (for example JAVA_TOOL_OPTIONS=-XX:-UseBASE64Intrinsics); that keeps the vulnerable decoder from being generated, at some cost to Base64 decoding speed." + if f.Status == Mitigated { + interim = " The JVM options keep the vulnerable decoder from running until then; keep them until it is updated." + } + target := "a Java release with the fix (17.0.21.1, 21.0.13.1, 25.0.5.1 or 27.0.1.1 and later on the same update)" + if ok && v.Feature() < 17 { + // A backported decoder (Oracle's Java 8 Performance Pack). + target = fmt.Sprintf("a build of Java %d from the same vendor with the fix (none was known on %s), or one without the backported AVX-512 decoder", v.Feature(), knowledge.Checked) + } else if ok { + if fx := knowledge.FixedVersion(v.Feature()); fx != "" { + target = "Java " + fx + " or later of the same update" + } else if v.Feature() >= 17 { + target = "a supported Java release with the fix, such as " + knowledge.Upgrade(v.Feature()) + " (Java " + fmt.Sprint(v.Feature()) + " received none)" + } + } + switch { + case f.Product != "": + return fmt.Sprintf("Upgrade %s to a release that bundles %s; its own runtime is not updated by the operating system's Java packages.%s", f.Product, target, interim) + case f.Package != nil: + return fmt.Sprintf("Update the %s package to %s: %s (%s).%s", f.Package.Name, target, upgradeLine(*f.Package), distroStatus(*f.Package), interim) + case f.JLinked: + return fmt.Sprintf("Rebuild this jlink image from a JDK at %s.%s", target, interim) + } + return fmt.Sprintf("Update this runtime to %s (Amazon Corretto and SapMachine published theirs on %s; others had not %s: check your vendor).%s", target, knowledge.VendorFixPublished, knowledge.AsOf(), interim) +} diff --git a/detectors/java4shell/internal/assess/assess_test.go b/detectors/java4shell/internal/assess/assess_test.go new file mode 100644 index 0000000..af8b5fa --- /dev/null +++ b/detectors/java4shell/internal/assess/assess_test.go @@ -0,0 +1,277 @@ +package assess + +import ( + "strings" + "testing" + + "github.com/DefectDojo/earlylight/detectors/java4shell/internal/jvm" + "github.com/DefectDojo/earlylight/detectors/java4shell/internal/testlib" +) + +func lib(t *testing.T, p string, l testlib.Lib) *jvm.Library { + t.Helper() + x, ok := jvm.Inspect(p, testlib.Build(l)) + if !ok { + t.Fatalf("%s not inspected", p) + } + return x +} + +func rel(t *testing.T, kv ...string) jvm.Release { + t.Helper() + r, ok := jvm.ParseRelease(testlib.Release(kv...)) + if !ok { + t.Fatal("release not parsed") + } + return r +} + +const temurin = "/opt/java/openjdk" + +func one(t *testing.T, in Input) Finding { + t.Helper() + fs := Run(in) + if len(fs) != 1 { + t.Fatalf("%d findings: %+v", len(fs), fs) + } + return fs[0] +} + +func TestVerdicts(t *testing.T) { + for _, c := range []struct { + name string + lib testlib.Lib + want Status + }{ + {"21 unfixed", testlib.Lib{Version: "21.0.13+9-LTS", VBMI: true}, Vulnerable}, + {"21 fixed", testlib.Lib{Version: "21.0.13.1+2-LTS", VBMI: true}, Fixed}, + {"17.0.5", testlib.Lib{Version: "17.0.5+8"}, NotAffected}, + {"17.0.5 with a vendor backport of the decoder", testlib.Lib{Version: "17.0.5+8", VBMI: true}, Unknown}, + {"11", testlib.Lib{Version: "11.0.33+10"}, NotAffected}, + {"11 with a vendor backport of the decoder", testlib.Lib{Version: "11.0.33+10", VBMI: true}, Unknown}, + {"Oracle 8u511 Performance Pack", testlib.Lib{Version: "1.8.0_511-perf-b33", VBMI: true}, Vulnerable}, + {"Java 8 backport newer than any known affected build", testlib.Lib{Version: "1.8.0_521-perf-b05", VBMI: true}, Unknown}, + {"8", testlib.Lib{Version: "1.8.0_512-b09"}, NotAffected}, + {"aarch64", testlib.Lib{Machine: "aarch64", Version: "21.0.13+9-LTS"}, NotAffected}, + {"x86 32-bit", testlib.Lib{Machine: "x86", Version: "21.0.13+9-LTS"}, NotAffected}, + {"universal macOS", testlib.Lib{Format: "fat", Version: "21.0.13+9-LTS", VBMI: true}, Vulnerable}, + {"unfixed but no decoder stubs", testlib.Lib{Version: "21.0.13+9-LTS"}, Unknown}, + {"24 end of life", testlib.Lib{Version: "24.0.2+12", VBMI: true}, Vulnerable}, + {"later update", testlib.Lib{Version: "21.0.14+1", VBMI: true}, Unknown}, + } { + f := one(t, Input{Libraries: []*jvm.Library{lib(t, temurin+"/lib/server/libjvm.so", c.lib)}}) + if f.Status != c.want || f.Reason == "" || f.Message == "" { + t.Errorf("%s: %s (%s)", c.name, f.Status, f.Reason) + } + if (f.Status == Vulnerable || f.Status == Unknown) && f.Fix == "" { + t.Errorf("%s: no fix text", c.name) + } + if f.Status == Fixed || f.Status == NotAffected { + if f.Fix != "" { + t.Errorf("%s: fix text on a cleared finding", c.name) + } + } + } +} + +func TestReleaseFile(t *testing.T) { + // The VM library decides; a release file that says otherwise is noted. + in := Input{ + Libraries: []*jvm.Library{lib(t, temurin+"/lib/server/libjvm.so", testlib.Lib{Version: "21.0.13+9-LTS", VBMI: true})}, + Releases: map[string]jvm.Release{temurin: rel(t, "JAVA_VERSION", "21.0.13.1", "JAVA_RUNTIME_VERSION", "21.0.13.1+2", "IMPLEMENTOR", "Eclipse Adoptium")}, + } + f := one(t, in) + if f.Status != Vulnerable || f.ReleaseVersion != "21.0.13.1+2" || f.VersionFrom != "vm-library" || f.Vendor != "Eclipse Adoptium" { + t.Errorf("forged release file: %+v", f) + } + // No version line: the release file is used, and says so. + in = Input{ + Libraries: []*jvm.Library{lib(t, temurin+"/lib/server/libjvm.so", testlib.Lib{})}, + Releases: map[string]jvm.Release{temurin: rel(t, "JAVA_VERSION", "25.0.5")}, + } + if f := one(t, in); f.Status != Vulnerable || f.VersionFrom != "release-file" || !strings.Contains(f.Reason, "release file") { + t.Errorf("release-file version: %+v", f) + } + // Neither: unknown, with the release file's problem. + in = Input{Libraries: []*jvm.Library{lib(t, temurin+"/lib/server/libjvm.so", testlib.Lib{})}, BadReleases: map[string]string{temurin: "too large"}} + if f := one(t, in); f.Status != Unknown || !strings.Contains(f.Reason, "too large") { + t.Errorf("no version: %+v", f) + } + // Java 8 JDK: the release file one level above jre/. + in = Input{ + Libraries: []*jvm.Library{lib(t, "/usr/lib/jvm/j8/jre/lib/amd64/server/libjvm.so", testlib.Lib{Version: "1.8.0_452-b09"})}, + Releases: map[string]jvm.Release{"/usr/lib/jvm/j8": rel(t, "JAVA_VERSION", "1.8.0_452")}, + } + if f := one(t, in); f.Status != NotAffected { + t.Errorf("java 8 jdk: %+v", f) + } + // jlink image. + in = Input{ + Libraries: []*jvm.Library{lib(t, "/app/rt/lib/server/libjvm.so", testlib.Lib{Version: "21.0.13+9-LTS", VBMI: true})}, + Releases: map[string]jvm.Release{"/app/rt": rel(t, "JAVA_VERSION", "21.0.13", "MODULES", "java.base java.logging")}, + } + if f := one(t, in); f.Status != Vulnerable || !f.JLinked || !strings.Contains(f.Fix, "jlink") { + t.Errorf("jlink: %+v", f) + } +} + +func TestOpenJ9(t *testing.T) { + in := Input{ + Libraries: []*jvm.Library{ + lib(t, temurin+"/lib/server/libjvm.so", testlib.Lib{Extra: "OPENJ9_JAVA_OPTIONS"}), + lib(t, temurin+"/lib/default/libj9vm29.so", testlib.Lib{}), + }, + Releases: map[string]jvm.Release{temurin: rel(t, "JAVA_VERSION", "21.0.13.0.3", "JVM_VARIANT", "Openj9")}, + } + if f := one(t, in); f.Status != NotAffected || f.VM != "OpenJ9" { + t.Errorf("openj9: %+v", f) + } +} + +func TestProducts(t *testing.T) { + for root, want := range map[string]string{ + "/opt/atlassian/jira/jre": "Atlassian Jira", + "/usr/share/elasticsearch/jdk": "Elasticsearch", + "/opt/jfrog/artifactory/app/third-party/java": "JFrog Artifactory", + "/opt/idea-IU-243/jbr": "IntelliJ IDEA", + "/Applications/IntelliJ IDEA.app/Contents/jbr/Contents/Home": "IntelliJ IDEA", + "/opt/acme/jre": "acme", + } { + if name, _, ok := Product(root); !ok || name != want { + t.Errorf("Product(%s) = %q %v, want %q", root, name, ok, want) + } + } + for _, root := range []string{"/opt/java/openjdk", "/usr/lib/jvm/java-17-openjdk-amd64", "/opt/jdk-21.0.5+11", "/home/u/.sdkman/candidates/java/21.0.5-tem", "/app/jre"} { + if name, _, ok := Product(root); ok { + t.Errorf("Product(%s) = %q, want none", root, name) + } + } + f := one(t, Input{Libraries: []*jvm.Library{lib(t, "/opt/atlassian/jira/jre/lib/server/libjvm.so", testlib.Lib{Version: "17.0.12+7", VBMI: true})}}) + if f.Kind != KindBundled || !strings.Contains(f.Fix, "Upgrade Atlassian Jira") { + t.Errorf("bundled: %+v", f) + } +} + +func TestPackages(t *testing.T) { + apk := []byte("P:openjdk17-jre-headless\nV:17.0.21.0.3_p1-r0\nF:usr/lib/jvm/java-17-openjdk/lib/server\nR:libjvm.so\n\nP:busybox\nV:1\nF:bin\nR:busybox\n") + in := Input{ + Libraries: []*jvm.Library{lib(t, "/usr/lib/jvm/java-17-openjdk/lib/server/libjvm.so", testlib.Lib{Version: "17.0.21.0.3+1-alpine-r0", VBMI: true})}, + Owners: APKOwners(apk), + } + f := one(t, in) + if f.Package == nil || f.Package.Name != "openjdk17-jre-headless" || !strings.Contains(f.Fix, "apk upgrade openjdk17-jre-headless") { + t.Errorf("apk: %+v", f) + } + status := []byte("Package: openjdk-17-jre-headless\nStatus: install ok installed\nVersion: 17.0.21+9-1~deb12u1\n\n") + lists := map[string][]byte{"openjdk-17-jre-headless:amd64": []byte("/usr/lib/jvm/java-17-openjdk-amd64/lib/server/libjvm.so\n")} + in = Input{ + Libraries: []*jvm.Library{lib(t, "/usr/lib/jvm/java-17-openjdk-amd64/lib/server/libjvm.so", testlib.Lib{Version: "17.0.21+9-Debian-1deb12u1", VBMI: true})}, + Owners: DpkgOwners(status, lists), + } + if f := one(t, in); f.Package == nil || f.Package.Version != "17.0.21+9-1~deb12u1" || !strings.Contains(f.Fix, "apt-get install --only-upgrade") { + t.Errorf("dpkg: %+v", f) + } + in = Input{Libraries: []*jvm.Library{lib(t, "/usr/lib/jvm/java-21-openjdk-21.0.13.0.11-1.el9.x86_64/lib/server/libjvm.so", testlib.Lib{Version: "21.0.13+11-LTS", VBMI: true})}} + if f := one(t, in); f.Package == nil || f.Package.Name != "java-21-openjdk-headless" || f.Package.From != "install path" { + t.Errorf("rpm path: %+v", f) + } +} + +func vulnLib(t *testing.T, root string) *jvm.Library { + return lib(t, root+"/lib/server/libjvm.so", testlib.Lib{Version: "21.0.13+9-LTS", VBMI: true}) +} + +func TestMitigations(t *testing.T) { + base := func() Input { return Input{Libraries: []*jvm.Library{vulnLib(t, temurin)}, Env: []string{}} } + + in := base() + in.Env = []string{"JAVA_TOOL_OPTIONS=-Xmx1g -XX:-UseBASE64Intrinsics"} + if f := one(t, in); f.Status != Mitigated || !strings.Contains(f.Reason, "JAVA_TOOL_OPTIONS") || f.Fix == "" { + t.Errorf("image env: %+v", f) + } + in.Env = append(in.Env, "_JAVA_OPTIONS=-XX:+UseBASE64Intrinsics") + if f := one(t, in); f.Status != Vulnerable { + t.Errorf("_JAVA_OPTIONS re-enables: %+v", f) + } + + in = base() + in.Command = []string{"java", "-XX:UseAVX=2", "-jar", "/app.jar"} + in.Env = []string{"PATH=/opt/java/openjdk/bin:/usr/bin"} + if f := one(t, in); f.Status != Mitigated { + t.Errorf("command line: %+v", f) + } + in.Command = []string{"java", "-jar", "/app.jar", "-XX:UseAVX=2"} + if f := one(t, in); f.Status != Vulnerable { + t.Errorf("an application argument counted: %+v", f) + } + + // CATALINA_OPTS counts when the container runs catalina.sh. + in = base() + in.Env = []string{"CATALINA_OPTS=-XX:-UseBASE64Intrinsics", "JAVA_HOME=/opt/java/openjdk"} + if f := one(t, in); f.Status != Vulnerable { + t.Errorf("CATALINA_OPTS without catalina.sh: %+v", f) + } + in.Command = []string{"catalina.sh", "run"} + if f := one(t, in); f.Status != Mitigated { + t.Errorf("CATALINA_OPTS with catalina.sh: %+v", f) + } + + // A product's setenv.sh covers the runtime it bundles, not others. + jira := "/opt/atlassian/jira/jre" + set, _ := jvm.ReadSetting("/opt/atlassian/jira/bin/setenv.sh", []byte(`JVM_SUPPORT_RECOMMENDED_ARGS="-XX:-UseBASE64Intrinsics"`)) + in = Input{Libraries: []*jvm.Library{vulnLib(t, jira), vulnLib(t, temurin)}, Settings: []jvm.Setting{set}} + fs := Run(in) + for _, f := range fs { + want := Vulnerable + if f.Path == jira { + want = Mitigated + } + if f.Status != want { + t.Errorf("setenv.sh scope: %s %s", f.Path, f.Status) + } + } + + // Tomcat's setenv.sh covers the JVM catalina.sh starts (JAVA_HOME), when + // the container runs it. + tset, _ := jvm.ReadSetting("/usr/local/tomcat/bin/setenv.sh", []byte(`CATALINA_OPTS="$CATALINA_OPTS -XX:-UseBASE64Intrinsics"`)) + in = Input{Libraries: []*jvm.Library{vulnLib(t, temurin)}, Settings: []jvm.Setting{tset}, + Env: []string{"CATALINA_HOME=/usr/local/tomcat", "JAVA_HOME=/opt/java/openjdk"}, Command: []string{"catalina.sh", "run"}} + if f := one(t, in); f.Status != Mitigated || !strings.Contains(f.Reason, "setenv.sh") { + t.Errorf("tomcat setenv.sh: %+v", f) + } + in.Command = []string{"java", "-jar", "/app.jar"} + if f := one(t, in); f.Status != Vulnerable { + t.Errorf("tomcat setenv.sh without catalina.sh: %+v", f) + } + in.Command, in.Env = []string{"catalina.sh", "run"}, []string{"CATALINA_HOME=/usr/local/tomcat", "JAVA_HOME=/usr/lib/jvm/other"} + if f := one(t, in); f.Status != Vulnerable { + t.Errorf("tomcat setenv.sh for another JAVA_HOME: %+v", f) + } + + // A systemd unit covers a bundled runtime of the product it starts; + // for a shared runtime it is recorded, not counted. + unit, _ := jvm.ReadSetting("/etc/systemd/system/jira.service", []byte("Environment=JAVA_TOOL_OPTIONS=-XX:-UseBASE64Intrinsics\nExecStart=/opt/atlassian/jira/bin/start-jira.sh -fg\n")) + in = Input{Libraries: []*jvm.Library{vulnLib(t, jira)}, Settings: []jvm.Setting{unit}} + if f := one(t, in); f.Status != Mitigated || !strings.Contains(f.Reason, "jira.service") { + t.Errorf("systemd bundled: %+v", f) + } + shared := "/usr/lib/jvm/java-21-openjdk-amd64" + unit, _ = jvm.ReadSetting("/etc/systemd/system/app.service", []byte("ExecStart="+shared+"/bin/java -XX:-UseBASE64Intrinsics -jar /srv/app.jar\n")) + in = Input{Libraries: []*jvm.Library{vulnLib(t, shared)}, Settings: []jvm.Setting{unit}} + if f := one(t, in); f.Status != Vulnerable || len(f.MitigatedFor) != 1 { + t.Errorf("systemd shared: %+v", f) + } + + // A mitigation never makes a fixed or unaffected runtime anything else. + in = Input{Libraries: []*jvm.Library{lib(t, temurin+"/lib/server/libjvm.so", testlib.Lib{Version: "21.0.13.1+2", VBMI: true})}, Env: []string{"JAVA_TOOL_OPTIONS=-XX:-UseBASE64Intrinsics"}} + if f := one(t, in); f.Status != Fixed { + t.Errorf("fixed + mitigation: %+v", f) + } +} + +func TestSymlinkLoopResolve(t *testing.T) { + // A resolver that never settles must not hang owner lookup. + loop := func(p string) string { return p + "/x" } + in := Input{Libraries: []*jvm.Library{vulnLib(t, temurin)}, Resolve: loop, Env: []string{}, Command: []string{"java", "-XX:UseAVX=2"}} + _ = one(t, in) +} diff --git a/detectors/java4shell/internal/assess/fuzz_test.go b/detectors/java4shell/internal/assess/fuzz_test.go new file mode 100644 index 0000000..3aeb6c5 --- /dev/null +++ b/detectors/java4shell/internal/assess/fuzz_test.go @@ -0,0 +1,57 @@ +package assess + +import ( + "testing" + + "github.com/DefectDojo/earlylight/detectors/java4shell/internal/jvm" + "github.com/DefectDojo/earlylight/detectors/java4shell/internal/testlib" +) + +func FuzzAPKOwners(f *testing.F) { + f.Add([]byte("P:openjdk17-jre-headless\nV:17.0.21.0.3_p1-r0\nF:usr/lib/jvm/java-17-openjdk/lib/server\nR:libjvm.so\n\n")) + f.Fuzz(func(t *testing.T, b []byte) { + for p, o := range APKOwners(b) { + if p == "" || o.Name == "" || o.Manager != "apk" { + t.Fatalf("bad owner %q %+v", p, o) + } + } + }) +} + +func FuzzDpkgOwners(f *testing.F) { + f.Add([]byte("Package: openjdk-17-jre-headless\nVersion: 17.0.21+9-1~deb12u1\n\n"), []byte("/usr/lib/jvm/java-17-openjdk-amd64/lib/server/libjvm.so\n")) + f.Fuzz(func(t *testing.T, status, list []byte) { + for p, o := range DpkgOwners(status, map[string][]byte{"openjdk-17-jre-headless:amd64": list}) { + if p == "" || o.Manager != "dpkg" { + t.Fatalf("bad owner %q %+v", p, o) + } + } + }) +} + +// FuzzRun judges a runtime made of fuzzed release text, environment, +// command and settings file: every verdict has a reason, and none is +// fixed or not affected when the VM library carries the decoder and +// states an unfixed version. +func FuzzRun(f *testing.F) { + f.Add("JAVA_VERSION=\"21.0.13.1\"\nJVM_VARIANT=\"Openj9\"\n", "JAVA_TOOL_OPTIONS=-XX:-UseBASE64Intrinsics", "sh -c java -XX:+UseBASE64Intrinsics", "JAVA_OPTS=\"-XX:-UseBASE64Intrinsics\"\n") + f.Add("JAVA_VERSION=\"11.0.20\"\n", "_JAVA_OPTIONS=-XX:UseAVX=2", "catalina.sh run", "if true; then CATALINA_OPTS=-XX:UseAVX=1; fi\n") + f.Fuzz(func(t *testing.T, release, env, cmd, setenv string) { + lj, _ := jvm.Inspect("/opt/acme/jre/lib/server/libjvm.so", testlib.Build(testlib.Lib{Version: "21.0.13+9-LTS", VBMI: true})) + in := Input{Libraries: []*jvm.Library{lj}, Env: []string{env, "JAVA_HOME=/opt/acme/jre"}, Command: jvm.Words(cmd), Releases: map[string]jvm.Release{}} + if r, ok := jvm.ParseRelease([]byte(release)); ok { + in.Releases["/opt/acme/jre"] = r + } + if s, ok := jvm.ReadSetting("/opt/acme/bin/setenv.sh", []byte(setenv)); ok { + in.Settings = []jvm.Setting{s} + } + for _, x := range Run(in) { + if x.Reason == "" || x.Message == "" { + t.Fatalf("no reason: %+v", x) + } + if x.Status == Fixed || x.Status == NotAffected { + t.Fatalf("an unfixed library with the decoder cleared: %s (%s)", x.Status, x.Reason) + } + } + }) +} diff --git a/detectors/java4shell/internal/assess/packages.go b/detectors/java4shell/internal/assess/packages.go new file mode 100644 index 0000000..11bd221 --- /dev/null +++ b/detectors/java4shell/internal/assess/packages.go @@ -0,0 +1,136 @@ +package assess + +import ( + "github.com/DefectDojo/earlylight/detectors/java4shell/internal/knowledge" + "path" + "regexp" + "strings" +) + +// Package is the OS package that installed a runtime. +type Package struct { + Manager string `json:"manager"` // apk, dpkg, rpm + Name string `json:"name"` + Version string `json:"version,omitempty"` + // From says how it was found: the package database, or the install path + // (rpm, whose database this detector does not read). + From string `json:"from"` +} + +// javaPackage reports a package name that can hold a Java runtime. +func javaPackage(name string) bool { + n := strings.ToLower(name) + for _, s := range []string{"openjdk", "jdk", "jre", "java", "corretto", "temurin", "zulu", "liberica", "sapmachine", "semeru", "graalvm", "jbr"} { + if strings.Contains(n, s) { + return true + } + } + return false +} + +// APKOwners maps the files of Java packages in an apk database to their +// package. +func APKOwners(db []byte) map[string]Package { + out := map[string]Package{} + var name, ver, dir string + keep := false + for _, line := range strings.Split(string(db), "\n") { + if len(line) < 2 || line[1] != ':' { + if line == "" { + name, ver, dir, keep = "", "", "", false + } + continue + } + v := line[2:] + switch line[0] { + case 'P': + name, keep = v, javaPackage(v) + case 'V': + ver = v + case 'F': + dir = v + case 'R': + if keep && strings.HasPrefix(v, "libjvm.so") || keep && v == "release" { + out["/"+path.Join(dir, v)] = Package{Manager: "apk", Name: name, Version: ver, From: "/lib/apk/db/installed"} + } + } + } + return out +} + +// DpkgOwners maps the files listed in dpkg .list files of Java packages to +// their package, with versions from the status file. +func DpkgOwners(status []byte, lists map[string][]byte) map[string]Package { + versions := map[string]string{} + var name string + for _, line := range strings.Split(string(status), "\n") { + switch { + case strings.HasPrefix(line, "Package: "): + name = strings.TrimSpace(strings.TrimPrefix(line, "Package: ")) + case strings.HasPrefix(line, "Version: ") && name != "": + versions[name] = strings.TrimSpace(strings.TrimPrefix(line, "Version: ")) + case line == "": + name = "" + } + } + out := map[string]Package{} + for pkg, b := range lists { + n, _, _ := strings.Cut(pkg, ":") + for _, f := range strings.Split(string(b), "\n") { + f = strings.TrimSpace(f) + if strings.HasSuffix(f, "/libjvm.so") || strings.HasSuffix(f, "/release") { + out[f] = Package{Manager: "dpkg", Name: n, Version: versions[n], From: "/var/lib/dpkg/status"} + } + } + } + return out +} + +// IsDpkgList reports a dpkg file list worth reading. +func IsDpkgList(p string) bool { + return strings.HasPrefix(p, "/var/lib/dpkg/info/") && strings.HasSuffix(p, ".list") && + javaPackage(strings.TrimSuffix(path.Base(p), ".list")) +} + +// rpmPath is how Red Hat, Fedora, SUSE and Amazon Linux name a packaged +// runtime: /usr/lib/jvm/java-17-openjdk-17.0.13.0.11-3.el9.x86_64. +var rpmPath = regexp.MustCompile(`^/usr/lib(?:64)?/jvm/(java-(\d+)-openjdk)-(\d[\w.]*-[\w.]+?)\.(x86_64|aarch64|ppc64le|s390x)$`) + +// rpmFromPath names the rpm package a runtime directory belongs to. +func rpmFromPath(root string) (Package, bool) { + m := rpmPath.FindStringSubmatch(root) + if m == nil { + if strings.HasPrefix(root, "/usr/lib/jvm/java-") && strings.Contains(root, "amazon-corretto") { + return Package{Manager: "rpm", Name: path.Base(root), From: "install path"}, true + } + return Package{}, false + } + return Package{Manager: "rpm", Name: m[1] + "-headless", Version: m[3], From: "install path"}, true +} + +// upgradeLine is the command that upgrades a package. +func upgradeLine(p Package) string { + switch p.Manager { + case "apk": + return "apk upgrade " + p.Name + case "dpkg": + return "apt-get install --only-upgrade " + p.Name + case "rpm": + return "dnf upgrade " + p.Name + " (or yum upgrade)" + } + return "" +} + +// distroStatus says whether the distribution had a fixed package when the +// data was checked. +func distroStatus(p Package) string { + switch p.Manager { + case "apk": + return "Alpine had published no fixed package " + knowledge.AsOf() + case "dpkg": + return "Debian and Ubuntu had published no fixed package " + knowledge.AsOf() + case "rpm": + return "the distribution's fix status was not checked" + } + return "" +} diff --git a/detectors/java4shell/internal/assess/product.go b/detectors/java4shell/internal/assess/product.go new file mode 100644 index 0000000..b3db62a --- /dev/null +++ b/detectors/java4shell/internal/assess/product.go @@ -0,0 +1,96 @@ +package assess + +import ( + "path" + "strings" +) + +// standardPlaces are where Java runtimes are installed on their own (by a +// package, an image, an installer, a version manager or a CI runner). A +// runtime there is not part of another product. +var standardPlaces = []string{ + "/usr/lib/jvm/", "/usr/lib64/jvm/", "/usr/java/", "/opt/java/", "/usr/local/java/", "/usr/jdk/", + "/usr/local/openjdk", "/opt/openjdk", "/opt/jdk", "/opt/hostedtoolcache/", "/Library/Java/JavaVirtualMachines/", + "/System/Library/Java/", "/.sdkman/candidates/java/", "/.jdks/", "/.gradle/jdks/", "/.asdf/installs/java/", + "/.local/share/mise/installs/java/", "/.m2/jdks/", "/usr/lib/jvm-", "/opt/ibm/java", "/opt/graalvm", + "/Program Files/Java/", "/Program Files/Eclipse Adoptium/", "/Program Files/Eclipse Foundation/", "/Program Files/Zulu/", + "/Program Files/Amazon Corretto/", "/Program Files/Microsoft/", "/Program Files/BellSoft/", "/Program Files/Semeru/", + "/Program Files/SapMachine/", "/Program Files (x86)/Java/", +} + +// runtimeDirs are the names products give the runtime they bundle. +var runtimeDirs = map[string]bool{"jre": true, "jdk": true, "jbr": true, "java": true, "runtime": true, "jre64": true, "openjdk": true, + "jre-x64": true, "jdk-x64": true, "Home": true} + +// products names well-known products by a piece of their install path. +var products = []struct{ part, name string }{ + {"/atlassian/jira", "Atlassian Jira"}, {"/atlassian/confluence", "Atlassian Confluence"}, {"/atlassian/bitbucket", "Atlassian Bitbucket"}, + {"/atlassian/bamboo", "Atlassian Bamboo"}, {"/atlassian/crowd", "Atlassian Crowd"}, {"/atlassian/", "Atlassian product"}, + {"/elasticsearch", "Elasticsearch"}, {"/logstash", "Logstash"}, {"/opensearch", "OpenSearch"}, {"/kibana", "Kibana"}, + {"/artifactory", "JFrog Artifactory"}, {"/jfrog/", "JFrog product"}, {"/sonatype/nexus", "Sonatype Nexus"}, {"/nexus", "Sonatype Nexus"}, + {"/teamcity", "JetBrains TeamCity"}, {"/youtrack", "JetBrains YouTrack"}, {"IntelliJ", "IntelliJ IDEA"}, {"/idea", "IntelliJ IDEA"}, + {"PyCharm", "PyCharm"}, {"/pycharm", "PyCharm"}, {"/android-studio", "Android Studio"}, {"Android Studio", "Android Studio"}, + {"/JetBrains/", "JetBrains IDE"}, {"/jetbrains", "JetBrains IDE"}, {"/sonarqube", "SonarQube"}, {"/keycloak", "Keycloak"}, + {"/jenkins", "Jenkins"}, {"/solr", "Apache Solr"}, {"/kafka", "Apache Kafka"}, {"/confluent", "Confluent Platform"}, + {"/BurpSuite", "Burp Suite"}, {"Burp Suite", "Burp Suite"}, {"/dbeaver", "DBeaver"}, {"DBeaver", "DBeaver"}, {"/eclipse", "Eclipse IDE"}, + {"/netbeans", "Apache NetBeans"}, {"/minecraft", "Minecraft"}, {"/tableau", "Tableau"}, {"/MATLAB", "MATLAB"}, + {"/pingfederate", "PingFederate"}, {"/pingaccess", "PingAccess"}, {"/spinnaker", "Spinnaker"}, {"/gerrit", "Gerrit"}, + {"/cassandra", "Apache Cassandra"}, {"/neo4j", "Neo4j"}, {"/gitbucket", "GitBucket"}, {"/rundeck", "Rundeck"}, +} + +// notProduct are directories too general to be a product's install +// directory. +var notProduct = map[string]bool{"/": true, "/opt": true, "/usr": true, "/usr/local": true, "/usr/share": true, "/usr/lib": true, + "/srv": true, "/home": true, "/root": true, "/app": true, "/var": true, "/var/lib": true, "/Applications": true, "C:/Program Files": true} + +// standard reports a runtime installed on its own. +func standard(root string) bool { + for _, p := range standardPlaces { + if strings.Contains(root, p) { + return true + } + } + return false +} + +// Product returns the product a runtime is bundled with: its name and +// install directory. ok is false for a runtime installed on its own. +func Product(root string) (name, dir string, ok bool) { + if standard(root) { + return "", "", false + } + dir = root + // A macOS runtime: .app/Contents/jbr/Contents/Home. + dir = strings.TrimSuffix(dir, "/Contents/Home") + if !runtimeDirs[path.Base(dir)] { + // Products also nest it one level deeper (lib/runtime/jre, + // app/third-party/java/). + parent := path.Base(path.Dir(dir)) + if !runtimeDirs[parent] && parent != "third-party" { + return "", "", false + } + dir = path.Dir(dir) + } + dir = path.Dir(dir) + // Only the layouts that put the runtime below such a directory. + if path.Base(dir) == "third-party" { + dir = path.Dir(dir) + if path.Base(dir) == "app" { + dir = path.Dir(dir) + } + } + if b := path.Base(dir); b == "Contents" && strings.HasSuffix(path.Dir(dir), ".app") { + dir = path.Dir(dir) + } + if notProduct[dir] || strings.Count(dir, "/") < 2 && !strings.Contains(dir, ":") { + // /opt/jre, /app/jre, /usr/jre: a runtime in a general directory, + // not part of a product. + return "", "", false + } + for _, p := range products { + if strings.Contains(dir, p.part) { + return p.name, dir, true + } + } + return path.Base(dir), dir, true +} diff --git a/detectors/java4shell/internal/assess/review2_test.go b/detectors/java4shell/internal/assess/review2_test.go new file mode 100644 index 0000000..67ebcd1 --- /dev/null +++ b/detectors/java4shell/internal/assess/review2_test.go @@ -0,0 +1,114 @@ +package assess + +import ( + "testing" + + "github.com/DefectDojo/earlylight/detectors/java4shell/internal/jvm" + "github.com/DefectDojo/earlylight/detectors/java4shell/internal/testlib" +) + +// Review finding 1: the release file alone never clears a runtime whose VM +// library carries the decoder. +func TestReleaseAloneDoesNotClear(t *testing.T) { + musl := Input{ + Libraries: []*jvm.Library{lib(t, temurin+"/lib/server/libjvm.so", testlib.Lib{Version: "21.0.13+9-LTS", VBMI: true, Musl: true})}, + Releases: map[string]jvm.Release{temurin: rel(t, "JAVA_VERSION", "21.0.13.1", "JAVA_RUNTIME_VERSION", "21.0.13.1+2-LTS")}, + } + if f := one(t, musl); f.Status != Vulnerable || f.VersionFrom != "vm-library" { + t.Errorf("musl library with a forged release: %s from %s", f.Status, f.VersionFrom) + } + for _, v := range []string{"21.0.13.1", "11.0.20"} { + in := Input{ + Libraries: []*jvm.Library{lib(t, temurin+"/lib/server/libjvm.so", testlib.Lib{VBMI: true})}, + Releases: map[string]jvm.Release{temurin: rel(t, "JAVA_VERSION", v)}, + } + if f := one(t, in); f.Status != Unknown { + t.Errorf("unreadable library with the decoder, release %s: %s (%s)", v, f.Status, f.Reason) + } + } +} + +// Review finding 6: a release file that contradicts a HotSpot library that +// was read makes the verdict unknown. +func TestReleaseContradiction(t *testing.T) { + in := Input{ + Libraries: []*jvm.Library{lib(t, temurin+"/lib/server/libjvm.so", testlib.Lib{Version: "21.0.13+9-LTS", VBMI: true})}, + Releases: map[string]jvm.Release{temurin: rel(t, "JAVA_VERSION", "21.0.13", "JVM_VARIANT", "Openj9")}, + } + if f := one(t, in); f.Status != Unknown { + t.Errorf("HotSpot library, OpenJ9 release: %s (%s)", f.Status, f.Reason) + } +} + +// Review finding 4: a command that turns the intrinsic back on voids the +// image's mitigation, even when it cannot be tied to one runtime. +func TestCommandReenables(t *testing.T) { + for _, c := range []struct { + env string + cmd []string + }{ + {"JAVA_TOOL_OPTIONS=-XX:-UseBASE64Intrinsics", []string{"/bin/sh", "-c", "exec java -XX:+UseBASE64Intrinsics -jar /app.jar"}}, + {"JAVA_TOOL_OPTIONS=-XX:-UseBASE64Intrinsics", []string{"java", "-XX:+UseBASE64Intrinsics", "-jar", "/app.jar"}}, // no PATH in the image config + {"JAVA_TOOL_OPTIONS=-XX:UseAVX=2", []string{"java", "-XX:UseAVX=3", "-jar", "/app.jar"}}, + {"JAVA_TOOL_OPTIONS=-XX:-UseBASE64Intrinsics", []string{"java", "@/app/jvm.args", "-jar", "/app.jar"}}, // options from a file this scan cannot see + } { + in := Input{Libraries: []*jvm.Library{vulnLib(t, temurin)}, Env: []string{c.env}, Command: c.cmd} + if f := one(t, in); f.Status != Vulnerable || f.Mitigation != "" { + t.Errorf("%q: %s (%s)", c.cmd, f.Status, f.Mitigation) + } + } + // Turning AVX-512 back on leaves -XX:-UseBASE64Intrinsics in force. + in := Input{Libraries: []*jvm.Library{vulnLib(t, temurin)}, Env: []string{"JAVA_TOOL_OPTIONS=-XX:-UseBASE64Intrinsics"}, Command: []string{"java", "-XX:UseAVX=3", "-jar", "/a.jar"}} + if f := one(t, in); f.Status != Mitigated { + t.Errorf("UseAVX=3 with -UseBASE64Intrinsics: %s", f.Status) + } + // _JAVA_OPTIONS is applied after the command line, so it still holds. + in = Input{Libraries: []*jvm.Library{vulnLib(t, temurin)}, Env: []string{"_JAVA_OPTIONS=-XX:-UseBASE64Intrinsics"}, + Command: []string{"/bin/sh", "-c", "exec java -XX:+UseBASE64Intrinsics -jar /app.jar"}} + if f := one(t, in); f.Status != Mitigated { + t.Errorf("_JAVA_OPTIONS after the command: %s", f.Status) + } +} + +// Review finding 5: a Windows batch file does not cover a Linux runtime. +func TestBatchFileWindowsOnly(t *testing.T) { + bat, _ := jvm.ReadSetting("/opt/tomcat/bin/setenv.bat", []byte("set \"CATALINA_OPTS=-XX:-UseBASE64Intrinsics\"\n")) + in := Input{Libraries: []*jvm.Library{vulnLib(t, "/opt/tomcat/jre")}, Settings: []jvm.Setting{bat}} + if f := one(t, in); f.Status != Vulnerable { + t.Errorf("setenv.bat on Linux: %s", f.Status) + } + win := lib(t, "/opt/tomcat/jre/bin/server/jvm.dll", testlib.Lib{Format: "pe", OS: "windows", Version: "21.0.13+9-LTS", VBMI: true}) + in = Input{Libraries: []*jvm.Library{win}, Settings: []jvm.Setting{bat}} + if f := one(t, in); f.Status != Mitigated { + t.Errorf("setenv.bat on Windows: %s", f.Status) + } +} + +// Round-2 finding 1: OpenJ9 only from an OpenJ9 library, never from the +// release file alone, and never when the HotSpot decoder stubs are there. +func TestOpenJ9NeedsLibrary(t *testing.T) { + fastdebug := lib(t, temurin+"/lib/server/libjvm.so", testlib.Lib{VBMI: true, Extra: "\x00OpenJDK 64-Bit Server VM (fastdebug 21.0.13+9) for linux-amd64 JRE (21.0.13+9)\x00"}) + in := Input{Libraries: []*jvm.Library{fastdebug}, Releases: map[string]jvm.Release{temurin: rel(t, "JAVA_VERSION", "21.0.13", "JVM_VARIANT", "Openj9")}} + if f := one(t, in); f.Status != Unknown { + t.Errorf("release-only OpenJ9 over a library with the decoder: %s (%s)", f.Status, f.Reason) + } + // A library without stubs and without a version line, release says + // OpenJ9, no OpenJ9 library: not cleared either. + plain := lib(t, temurin+"/lib/server/libjvm.so", testlib.Lib{}) + in = Input{Libraries: []*jvm.Library{plain}, Releases: map[string]jvm.Release{temurin: rel(t, "JAVA_VERSION", "21.0.13", "JVM_VARIANT", "Openj9")}} + if f := one(t, in); f.Status == NotAffected { + t.Errorf("release-only OpenJ9: %s (%s)", f.Status, f.Reason) + } + // An OpenJ9 VM library next to a redirector carrying HotSpot stubs: unknown. + j9 := lib(t, temurin+"/lib/default/libj9vm29.so", testlib.Lib{}) + in = Input{Libraries: []*jvm.Library{fastdebug, j9}} + if f := one(t, in); f.Status != Unknown { + t.Errorf("OpenJ9 library next to HotSpot stubs: %s", f.Status) + } + // Real Semeru layout: redirector + libj9vm29, release says OpenJ9. + redir := lib(t, temurin+"/lib/server/libjvm.so", testlib.Lib{Extra: "OPENJ9_JAVA_OPTIONS"}) + in = Input{Libraries: []*jvm.Library{redir, j9}, Releases: map[string]jvm.Release{temurin: rel(t, "JAVA_VERSION", "21.0.13.0.3", "JVM_VARIANT", "Openj9")}} + if f := one(t, in); f.Status != NotAffected { + t.Errorf("Semeru: %s (%s)", f.Status, f.Reason) + } +} diff --git a/detectors/java4shell/internal/assess/review_test.go b/detectors/java4shell/internal/assess/review_test.go new file mode 100644 index 0000000..f1905b7 --- /dev/null +++ b/detectors/java4shell/internal/assess/review_test.go @@ -0,0 +1,96 @@ +package assess + +import ( + "testing" + + "github.com/DefectDojo/earlylight/detectors/java4shell/internal/jvm" +) + +// Regressions from the independent review: mitigations that covered more +// than they do. +func TestReviewMitigationScope(t *testing.T) { + setting := func(p, text string) jvm.Setting { + t.Helper() + s, ok := jvm.ReadSetting(p, []byte(text)) + if !ok { + t.Fatalf("%s not read", p) + } + return s + } + status := func(in Input, root string) Status { + t.Helper() + for _, f := range Run(in) { + if f.Path == root { + return f.Status + } + } + t.Fatalf("no finding for %s", root) + return "" + } + + // catalina.sh passes CATALINA_OPTS only to the JVM JAVA_HOME names. + in := Input{Libraries: []*jvm.Library{vulnLib(t, temurin), vulnLib(t, "/opt/other/jre")}, + Env: []string{"CATALINA_OPTS=-XX:-UseBASE64Intrinsics", "JAVA_HOME=/opt/java/openjdk"}, Command: []string{"catalina.sh", "run"}} + if status(in, temurin) != Mitigated || status(in, "/opt/other/jre") != Vulnerable { + t.Error("CATALINA_OPTS covered a runtime catalina.sh does not start") + } + + // systemd: JAVA_OPTS does not reach a java ExecStart. + unit := setting("/etc/systemd/system/a.service", "Environment=JAVA_OPTS=-XX:-UseBASE64Intrinsics\nExecStart=/opt/acme/jre/bin/java -jar /opt/acme/a.jar\n") + in = Input{Libraries: []*jvm.Library{vulnLib(t, "/opt/acme/jre")}, Settings: []jvm.Setting{unit}} + if s := status(in, "/opt/acme/jre"); s != Vulnerable { + t.Errorf("JAVA_OPTS with a java ExecStart: %s", s) + } + // _JAVA_OPTIONS overrides the command line, wherever it is written. + unit = setting("/etc/systemd/system/a.service", "Environment=_JAVA_OPTIONS=-XX:+UseBASE64Intrinsics\nExecStart=/opt/acme/jre/bin/java -XX:-UseBASE64Intrinsics -jar /opt/acme/a.jar\n") + in.Settings = []jvm.Setting{unit} + if s := status(in, "/opt/acme/jre"); s != Vulnerable { + t.Errorf("_JAVA_OPTIONS in a unit: %s", s) + } + // A drop-in that resets JAVA_TOOL_OPTIONS undoes the unit's mitigation. + base := setting("/usr/lib/systemd/system/a.service", "Environment=JAVA_TOOL_OPTIONS=-XX:-UseBASE64Intrinsics\nExecStart=/opt/acme/bin/run.sh\n") + drop := setting("/etc/systemd/system/a.service.d/override.conf", "[Service]\nEnvironment=JAVA_TOOL_OPTIONS=-Xmx1g\n") + in.Settings = []jvm.Setting{base, drop} + if s := status(in, "/opt/acme/jre"); s != Vulnerable { + t.Errorf("drop-in reset: %s", s) + } + in.Settings = []jvm.Setting{base} + if s := status(in, "/opt/acme/jre"); s != Mitigated { + t.Errorf("unit without the drop-in: %s", s) + } + // A full unit in /etc replaces the one in /usr/lib. + over := setting("/etc/systemd/system/a.service", "ExecStart=/opt/acme/bin/run.sh\n") + in.Settings = []jvm.Setting{base, over} + if s := status(in, "/opt/acme/jre"); s != Vulnerable { + t.Errorf("/etc override: %s", s) + } + + // /opt/app/jre belongs to /opt/app, never to /opt. + if name, dir, ok := Product("/opt/app/jre"); ok && dir != "/opt/app" { + t.Errorf("Product(/opt/app/jre) = %q %q", name, dir) + } + if _, _, ok := Product("/opt/jre"); ok { + t.Error("/opt/jre taken for a product in /opt") + } + other := setting("/etc/systemd/system/other.service", "Environment=JAVA_TOOL_OPTIONS=-XX:-UseBASE64Intrinsics\nExecStart=/opt/other/bin/run.sh\n") + in = Input{Libraries: []*jvm.Library{vulnLib(t, "/opt/app/jre")}, Settings: []jvm.Setting{other}} + if s := status(in, "/opt/app/jre"); s != Vulnerable { + t.Errorf("another service's unit: %s", s) + } + + // Settings files in general directories configure nothing by place. + for _, p := range []string{"/etc/jvm.options", "/usr/local/bin/setenv.sh", "/opt/bin/setenv.sh"} { + s := setting(p, "-XX:-UseBASE64Intrinsics\nX=\"-XX:-UseBASE64Intrinsics\"\n") + in = Input{Libraries: []*jvm.Library{vulnLib(t, "/usr/local/openjdk-21")}, Settings: []jvm.Setting{s}} + if st := status(in, "/usr/local/openjdk-21"); st != Vulnerable { + t.Errorf("%s covered a runtime outside its product: %s", p, st) + } + } + + // A commented-out option is not set. + s := setting("/opt/atlassian/jira/bin/setenv.sh", "JVM_SUPPORT_RECOMMENDED_ARGS=\"-Xmx2g\" # was: -XX:-UseBASE64Intrinsics\n") + in = Input{Libraries: []*jvm.Library{vulnLib(t, "/opt/atlassian/jira/jre")}, Settings: []jvm.Setting{s}} + if st := status(in, "/opt/atlassian/jira/jre"); st != Vulnerable { + t.Errorf("commented option: %s", st) + } +} diff --git a/detectors/java4shell/internal/jvm/jvm.go b/detectors/java4shell/internal/jvm/jvm.go new file mode 100644 index 0000000..3166384 --- /dev/null +++ b/detectors/java4shell/internal/jvm/jvm.go @@ -0,0 +1,326 @@ +// Package jvm reads what a Java runtime's files prove: the VM library's CPU +// architecture (from its ELF, PE or Mach-O header), the VM name and version +// compiled into it, whether it carries the AVX-512 VBMI Base64 decode stubs, +// and the runtime's release file. It knows nothing about the engine. +package jvm + +import ( + "bytes" + "encoding/binary" + "path" + "regexp" + "strings" +) + +// Arch is a CPU architecture. +type Arch string + +const ( + X8664 Arch = "x86-64" + AArch64 Arch = "aarch64" + X86 Arch = "x86" + ARM Arch = "arm" + PPC64 Arch = "ppc64" + S390X Arch = "s390x" + RISCV64 Arch = "riscv64" + Other Arch = "other" +) + +// Library is one JVM library file: libjvm.so, jvm.dll, libjvm.dylib, or an +// OpenJ9 VM library. +type Library struct { + Path string `json:"path"` + Format string `json:"format"` // elf, pe, macho + // Archs are the architectures the file holds code for (a universal + // Mach-O file holds several). + Archs []Arch `json:"archs"` + // VMName, VMVersion and JREVersion come from the version line compiled + // into HotSpot ("OpenJDK 64-Bit Server VM (21.0.13+9-LTS) for + // linux-amd64 JRE (21.0.13+9-LTS)"). Empty when it is not there. + VMName string `json:"vm_name,omitempty"` + VMVersion string `json:"vm_version,omitempty"` + JREVersion string `json:"jre_version,omitempty"` + Platform string `json:"platform,omitempty"` // "linux-amd64" + // VBMIStubs is true when the library names the AVX-512 VBMI Base64 + // decode stubs (lookup_lo_base64, pack_vec_base64), which come with the + // vulnerable decoder (8268276). + VBMIStubs bool `json:"vbmi_stubs"` + // OpenJ9 is true for an OpenJ9 VM library or redirector. + OpenJ9 bool `json:"openj9,omitempty"` + // Variant is the VM directory: server, client, minimal, zero, j9vm... + Variant string `json:"variant,omitempty"` +} + +// HotSpot reports whether the library is a HotSpot VM. +func (l *Library) HotSpot() bool { return !l.OpenJ9 && l.VMName != "" } + +// Has reports whether the library holds code for a. +func (l *Library) Has(a Arch) bool { + for _, x := range l.Archs { + if x == a { + return true + } + } + return false +} + +var libNames = map[string]bool{"libjvm.so": true, "jvm.dll": true, "libjvm.dylib": true} + +// IsOpenJ9Name reports an OpenJ9 VM library name (libj9vm29.so, j9vm29.dll). +func IsOpenJ9Name(base string) bool { + b := strings.ToLower(base) + return (strings.HasPrefix(b, "libj9vm") || strings.HasPrefix(b, "j9vm")) && + (strings.HasSuffix(b, ".so") || strings.HasSuffix(b, ".dll") || strings.HasSuffix(b, ".dylib")) +} + +// IsLibraryName reports whether a file name is a JVM library. +func IsLibraryName(base string) bool { + return libNames[base] || libNames[strings.ToLower(base)] || IsOpenJ9Name(base) +} + +// vmLine is the version line HotSpot compiles in. JDK 8 has the same shape +// ("... JRE (1.8.0_452-b09)"), and musl builds name their platform +// linux-amd64-musl. +var vmLine = regexp.MustCompile(`([A-Za-z][ -~]{2,80}?VM) \(([0-9][!-'*-~]{0,60})\) for ([a-z0-9_]{1,20})-([a-z0-9_]{1,20}(?:-[a-z0-9_]{1,20})?) JRE \(([0-9][!-'*-~]{0,80})\)`) + +// maxScan bounds how much of a file the version line is looked for in: it +// sits in read-only data, and a libjvm is under 64 MiB. +const maxScan = 256 << 20 + +// Inspect reads a JVM library. ok is false when the file is not one (wrong +// name or not an executable format). +func Inspect(p string, data []byte) (*Library, bool) { + base := path.Base(p) + if !IsLibraryName(base) { + return nil, false + } + format, archs := header(data) + if format == "" { + return nil, false + } + l := &Library{Path: p, Format: format, Archs: archs, Variant: path.Base(path.Dir(p))} + b := data + if len(b) > maxScan { + b = b[:maxScan] + } + if m := findVMLine(b); m != nil { + l.VMName, l.VMVersion, l.JREVersion = string(m[1]), string(m[2]), string(m[5]) + l.Platform = string(m[3]) + "-" + string(m[4]) + } + l.VBMIStubs = bytes.Contains(b, []byte("lookup_lo_base64\x00")) && bytes.Contains(b, []byte("pack_vec_base64\x00")) + // OpenJ9: its VM library by name, or its lib/server redirector, which + // has no HotSpot version line, no HotSpot decoder stubs and names + // OPENJ9_JAVA_OPTIONS. + if IsOpenJ9Name(base) || l.VMName == "" && !l.VBMIStubs && bytes.Contains(b, []byte("OPENJ9_JAVA_OPTIONS")) { + l.OpenJ9 = true + } + return l, true +} + +// findVMLine looks for the version line only around the literal " JRE (", +// so a large file of printable text cannot make the regular expression +// scan it all from every position. +func findVMLine(b []byte) [][]byte { + const before, after, maxHits = 260, 100, 4096 + anchor := []byte(" JRE (") + off := 0 + for hits := 0; hits < maxHits; hits++ { + i := bytes.Index(b[off:], anchor) + if i < 0 { + return nil + } + at := off + i + lo, hi := max(0, at-before), min(len(b), at+len(anchor)+after) + if m := vmLine.FindSubmatch(b[lo:hi]); m != nil { + return m + } + off = at + len(anchor) + } + return nil +} + +// header reads an executable header: the format and the architectures. +func header(b []byte) (string, []Arch) { + switch { + case len(b) >= 20 && bytes.HasPrefix(b, []byte("\x7fELF")): + var m uint16 + if b[5] == 2 { + m = binary.BigEndian.Uint16(b[18:20]) + } else { + m = binary.LittleEndian.Uint16(b[18:20]) + } + return "elf", []Arch{elfArch(m)} + case len(b) >= 64 && b[0] == 'M' && b[1] == 'Z': + off := int(binary.LittleEndian.Uint32(b[60:64])) + if off < 0 || off+6 > len(b) || !bytes.Equal(b[off:off+4], []byte("PE\x00\x00")) { + return "", nil + } + return "pe", []Arch{peArch(binary.LittleEndian.Uint16(b[off+4 : off+6]))} + case len(b) >= 8: + le := binary.LittleEndian.Uint32(b[0:4]) + if le == 0xfeedfacf || le == 0xfeedface { + return "macho", []Arch{machoArch(binary.LittleEndian.Uint32(b[4:8]))} + } + be := binary.BigEndian.Uint32(b[0:4]) + if be == 0xcafebabe || be == 0xcafebabf { + n := int(binary.BigEndian.Uint32(b[4:8])) + // A Java class file has the same magic; its "count" is a version + // number of 45 or more. A universal binary holds a few slices. + if n < 1 || n > 8 { + return "", nil + } + size := 20 + if be == 0xcafebabf { + size = 32 + } + var out []Arch + for i := 0; i < n; i++ { + o := 8 + i*size + if o+4 > len(b) { + return "", nil + } + out = append(out, machoArch(binary.BigEndian.Uint32(b[o:o+4]))) + } + return "macho", out + } + } + return "", nil +} + +func elfArch(m uint16) Arch { + switch m { + case 0x3e: + return X8664 + case 0xb7: + return AArch64 + case 3: + return X86 + case 0x28: + return ARM + case 0x15: + return PPC64 + case 0x16: + return S390X + case 0xf3: + return RISCV64 + } + return Other +} + +func peArch(m uint16) Arch { + switch m { + case 0x8664: + return X8664 + case 0xaa64: + return AArch64 + case 0x14c: + return X86 + } + return Other +} + +func machoArch(c uint32) Arch { + switch c { + case 0x01000007: + return X8664 + case 0x0100000c: + return AArch64 + case 7: + return X86 + } + return Other +} + +// variantDirs are the directories a VM library sits in. +var variantDirs = map[string]bool{"server": true, "client": true, "minimal": true, "zero": true, "j9vm": true, "default": true, + "compressedrefs": true, "classic": true, "hotspot": true, "jvmci": true} + +// archDirs are the CPU directories of JDK 8 layouts (jre/lib/amd64/server). +var archDirs = map[string]bool{"amd64": true, "x86_64": true, "i386": true, "i586": true, "aarch64": true, "arm64": true, "ppc64le": true, "ppc64": true, + "s390x": true, "riscv64": true, "arm": true, "sparcv9": true} + +// Root returns the runtime directory a VM library belongs to: +// /lib/server/libjvm.so, /bin/server/jvm.dll, +// /lib/amd64/server/libjvm.so (Java 8). ok is false for any other +// place. +func Root(lib string) (string, bool) { + dir := path.Dir(lib) + if !variantDirs[path.Base(dir)] { + // OpenJ9 and some builds keep a VM library directly in lib/ or bin/. + if b := path.Base(dir); b == "lib" || b == "bin" { + return path.Dir(dir), true + } + return "", false + } + dir = path.Dir(dir) + if archDirs[path.Base(dir)] { + dir = path.Dir(dir) + } + if b := path.Base(dir); b != "lib" && b != "bin" { + return "", false + } + return path.Dir(dir), true +} + +// Release is a runtime's release file. +type Release map[string]string + +// maxRelease bounds a release file: real ones are a few KiB. +const maxRelease = 64 << 10 + +// ParseRelease reads KEY="value" lines. ok is false for a file too large +// or with nothing a release file has. +func ParseRelease(b []byte) (Release, bool) { + if len(b) > maxRelease { + return nil, false + } + r := Release{} + for _, line := range strings.Split(string(b), "\n") { + line = strings.TrimSpace(strings.TrimSuffix(line, "\r")) + k, v, ok := strings.Cut(line, "=") + if !ok || k == "" || len(k) > 64 || strings.ContainsAny(k, " \t\"") { + continue + } + v = strings.TrimSpace(v) + if len(v) >= 2 && v[0] == '"' && v[len(v)-1] == '"' { + v = v[1 : len(v)-1] + } + if len(v) > 4096 { + v = v[:4096] + } + if _, dup := r[k]; !dup { + r[k] = v + } + } + if r["JAVA_VERSION"] == "" { + return nil, false + } + return r, true +} + +// Version is the most precise version the release file states. +func (r Release) Version() string { + if v := r["JAVA_RUNTIME_VERSION"]; v != "" { + return v + } + return r["JAVA_VERSION"] +} + +// OpenJ9 reports a release file that names the OpenJ9 VM. +func (r Release) OpenJ9() bool { return strings.EqualFold(r["JVM_VARIANT"], "openj9") } + +// JLinked reports a runtime made with jlink: a release file with MODULES and +// without java.se, the module every full JDK and JRE has. A jlink image +// keeps only JAVA_VERSION and MODULES of the JDK's release file. +func (r Release) JLinked() bool { + m := r["MODULES"] + if m == "" { + return false + } + for _, x := range strings.Fields(m) { + if x == "java.se" { + return false + } + } + return true +} diff --git a/detectors/java4shell/internal/jvm/jvm_test.go b/detectors/java4shell/internal/jvm/jvm_test.go new file mode 100644 index 0000000..1c92d27 --- /dev/null +++ b/detectors/java4shell/internal/jvm/jvm_test.go @@ -0,0 +1,251 @@ +package jvm + +import ( + "bytes" + "strings" + "testing" + "time" + + "github.com/DefectDojo/earlylight/detectors/java4shell/internal/testlib" +) + +func TestInspect(t *testing.T) { + for _, c := range []struct { + name, path string + lib testlib.Lib + arch Arch + version string + vbmi bool + }{ + {"elf x86-64", "/opt/java/openjdk/lib/server/libjvm.so", testlib.Lib{Version: "21.0.13+9-LTS", VBMI: true}, X8664, "21.0.13+9-LTS", true}, + {"elf aarch64", "/opt/java/openjdk/lib/server/libjvm.so", testlib.Lib{Machine: "aarch64", Version: "21.0.13+9-LTS"}, AArch64, "21.0.13+9-LTS", false}, + {"pe", "C:/Program Files/Java/jdk-21/bin/server/jvm.dll", testlib.Lib{Format: "pe", OS: "windows", Version: "21.0.13+9-LTS", VBMI: true}, X8664, "21.0.13+9-LTS", true}, + {"macho", "/Library/Java/JavaVirtualMachines/x/Contents/Home/lib/server/libjvm.dylib", testlib.Lib{Format: "macho", OS: "bsd", Version: "17.0.21+9"}, X8664, "17.0.21+9", false}, + {"java 8", "/usr/lib/jvm/java-8/jre/lib/amd64/server/libjvm.so", testlib.Lib{Version: "1.8.0_452-b09"}, X8664, "1.8.0_452-b09", false}, + } { + l, ok := Inspect(c.path, testlib.Build(c.lib)) + if !ok { + t.Fatalf("%s: not inspected", c.name) + } + if !l.Has(c.arch) || l.JREVersion != c.version || l.VBMIStubs != c.vbmi || !l.HotSpot() { + t.Errorf("%s: got %+v", c.name, l) + } + } + fat, ok := Inspect("/x/lib/server/libjvm.dylib", testlib.Build(testlib.Lib{Format: "fat", Version: "21.0.13+9"})) + if !ok || !fat.Has(X8664) || !fat.Has(AArch64) { + t.Errorf("universal binary: %+v", fat) + } + if _, ok := Inspect("/x/lib/server/libfoo.so", testlib.Build(testlib.Lib{Version: "21"})); ok { + t.Error("a library with another name was inspected") + } + if _, ok := Inspect("/x/lib/server/libjvm.so", []byte("#!/bin/sh\necho")); ok { + t.Error("a script named libjvm.so was inspected") + } + // A Java class file has the universal binary's magic. + class := []byte{0xca, 0xfe, 0xba, 0xbe, 0, 0, 0, 65} + if _, ok := Inspect("/x/lib/server/libjvm.dylib", class); ok { + t.Error("a class file was taken for a universal binary") + } + j9, ok := Inspect("/opt/java/openjdk/lib/default/libj9vm29.so", testlib.Build(testlib.Lib{})) + if !ok || !j9.OpenJ9 || j9.HotSpot() { + t.Errorf("OpenJ9: %+v", j9) + } +} + +func TestInspectHostile(t *testing.T) { + // A version line with parentheses or spaces in the version is refused, + // and an over-long one does not match. + b := testlib.Build(testlib.Lib{Extra: "\x00OpenJDK 64-Bit Server VM (21 (x)) for linux-amd64 JRE (21)\x00" + strings.Repeat("A", 1000) + "VM (" + strings.Repeat("9", 500) + ") for linux-amd64 JRE (1)"}) + l, ok := Inspect("/a/lib/server/libjvm.so", b) + if !ok || l.JREVersion != "" { + t.Errorf("hostile version line read: %+v", l) + } + // Stub names must be whole names. + b = testlib.Build(testlib.Lib{Version: "17.0.5+8", Extra: "xlookup_lo_base64url\x00pack_vec_base64x\x00"}) + if l, _ := Inspect("/a/lib/server/libjvm.so", b); l.VBMIStubs { + t.Error("partial stub names counted") + } + // Truncated headers. + for _, h := range [][]byte{[]byte("\x7fELF"), []byte("MZ"), append([]byte("MZ"), bytes.Repeat([]byte{0xff}, 62)...), {0xca, 0xfe, 0xba, 0xbe, 0, 0, 0, 3}} { + if _, ok := Inspect("/a/lib/server/libjvm.so", h); ok { + t.Errorf("truncated header %x accepted", h) + } + } +} + +func TestRoot(t *testing.T) { + for in, want := range map[string]string{ + "/opt/java/openjdk/lib/server/libjvm.so": "/opt/java/openjdk", + "/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server/libjvm.so": "/usr/lib/jvm/java-8-openjdk-amd64/jre", + "C:/Program Files/Java/jdk-21/bin/server/jvm.dll": "C:/Program Files/Java/jdk-21", + "/Library/Java/JavaVirtualMachines/t/Contents/Home/lib/server/libjvm.dylib": "/Library/Java/JavaVirtualMachines/t/Contents/Home", + "/opt/java/openjdk/lib/default/libj9vm29.so": "/opt/java/openjdk", + "/opt/java/openjdk/lib/libjvm.so": "/opt/java/openjdk", + } { + if got, ok := Root(in); !ok || got != want { + t.Errorf("Root(%s) = %q %v, want %q", in, got, ok, want) + } + } + if _, ok := Root("/tmp/libjvm.so"); ok { + t.Error("a stray libjvm.so got a runtime root") + } +} + +func TestRelease(t *testing.T) { + r, ok := ParseRelease(testlib.Release("JAVA_VERSION", "21.0.13", "JAVA_RUNTIME_VERSION", "21.0.13+9-LTS", "OS_ARCH", "aarch64", "JVM_VARIANT", "Openj9", "MODULES", "java.base java.se")) + if !ok || r.Version() != "21.0.13+9-LTS" || !r.OpenJ9() || r.JLinked() { + t.Errorf("release: %v", r) + } + jl, ok := ParseRelease([]byte("JAVA_VERSION=\"21.0.13\"\nMODULES=\"java.base java.logging\"\n")) + if !ok || !jl.JLinked() || jl.Version() != "21.0.13" { + t.Errorf("jlink release: %v", jl) + } + if _, ok := ParseRelease(bytes.Repeat([]byte("JAVA_VERSION=\"21\"\n"), 10000)); ok { + t.Error("a huge release file was read") + } + if _, ok := ParseRelease([]byte("IMPLEMENTOR=\"x\"\n")); ok { + t.Error("a release file without JAVA_VERSION was read") + } + // The first value of a repeated key wins: a forged second line does not + // replace it. + r, _ = ParseRelease([]byte("JAVA_VERSION=\"21.0.13\"\nJAVA_VERSION=\"21.0.13.1\"\n")) + if r.Version() != "21.0.13" { + t.Errorf("repeated key: %v", r.Version()) + } +} + +func TestOptions(t *testing.T) { + for _, c := range []struct { + in string + want bool + }{ + {"-XX:-UseBASE64Intrinsics", true}, + {"-Xmx1g -XX:UseAVX=2", true}, + {"-XX:UseAVX=3", false}, + {"-XX:-UseBASE64Intrinsics -XX:+UseBASE64Intrinsics", false}, + {"-XX:+UseBASE64Intrinsics -XX:-UseBASE64Intrinsics", true}, + {"-XX:UseBASE64Intrinsics=false", true}, + {"'-XX:-UseBASE64Intrinsics'", true}, + {"-XX:-UseBASE64IntrinsicsX", false}, + {"-XX:UseAVX=two", false}, + {"", false}, + } { + var o Options + o.Apply(c.in, "test") + if got, how := o.Mitigates(); got != c.want || got && how == "" { + t.Errorf("%q: %v %q", c.in, got, how) + } + } + // _JAVA_OPTIONS overrides the command line, which overrides + // JAVA_TOOL_OPTIONS. + env := map[string]string{"JAVA_TOOL_OPTIONS": "-XX:-UseBASE64Intrinsics", "_JAVA_OPTIONS": "-XX:+UseBASE64Intrinsics"} + if ok, _ := FromEnv(env, nil, 21, nil).Mitigates(); ok { + t.Error("_JAVA_OPTIONS did not override") + } + env = map[string]string{"JDK_JAVA_OPTIONS": "-XX:-UseBASE64Intrinsics"} + if ok, _ := FromEnv(env, nil, 8, nil).Mitigates(); ok { + t.Error("JDK_JAVA_OPTIONS counted for Java 8") + } + env = map[string]string{"JAVA_OPTS": "-XX:-UseBASE64Intrinsics"} + if ok, _ := FromEnv(env, nil, 21, nil).Mitigates(); ok { + t.Error("JAVA_OPTS counted without a script that passes it on") + } + if ok, _ := FromEnv(env, nil, 21, map[string]bool{"JAVA_OPTS": true}).Mitigates(); !ok { + t.Error("JAVA_OPTS not counted when the script passes it on") + } +} + +func TestJavaOptions(t *testing.T) { + for _, c := range []struct { + cmd []string + want string + ok bool + }{ + {[]string{"java", "-XX:-UseBASE64Intrinsics", "-jar", "app.jar", "-XX:+UseBASE64Intrinsics"}, "-XX:-UseBASE64Intrinsics", true}, + {[]string{"/opt/java/openjdk/bin/java", "-cp", "-XX:UseAVX=2", "Main"}, "", true}, + {[]string{"sh", "-c", "exec java -XX:UseAVX=2 -jar /app.jar"}, "-XX:UseAVX=2", true}, + {[]string{"catalina.sh", "run"}, "", false}, + } { + got, ok := JavaOptions(c.cmd) + if ok != c.ok || strings.Join(got, " ") != c.want { + t.Errorf("%v: %v %v", c.cmd, got, ok) + } + } +} + +func TestSettings(t *testing.T) { + s, ok := ReadSetting("/usr/share/elasticsearch/config/jvm.options.d/j4s.options", []byte("# c\n17-:-XX:-UseBASE64Intrinsics\n-Xmx1g\n")) + if !ok || s.Scope != "/usr/share/elasticsearch" { + t.Fatalf("jvm.options: %+v", s) + } + if m, _ := s.Options(21).Mitigates(); !m { + t.Error("jvm.options line for 17- not applied to 21") + } + s, _ = ReadSetting("/opt/es/config/jvm.options", []byte("8-11:-XX:-UseBASE64Intrinsics\n")) + if m, _ := s.Options(21).Mitigates(); m { + t.Error("jvm.options line for 8-11 applied to 21") + } + s, ok = ReadSetting("/opt/atlassian/jira/bin/setenv.sh", []byte("JVM_SUPPORT_RECOMMENDED_ARGS=\"-XX:-UseBASE64Intrinsics\"\n# -XX:+UseBASE64Intrinsics\n")) + if !ok || s.Scope != "/opt/atlassian/jira" { + t.Fatalf("setenv: %+v", s) + } + if m, _ := s.Options(17).Mitigates(); !m { + t.Error("setenv.sh mitigation not read (or the comment counted)") + } + s, ok = ReadSetting("/etc/systemd/system/app.service", []byte("[Service]\nEnvironment=\"JAVA_TOOL_OPTIONS=-XX:UseAVX=2\" FOO=bar\nExecStart=/opt/app/jre/bin/java -jar /opt/app/app.jar\n")) + if !ok || s.Unit != "app.service" || s.Exec != "/opt/app/jre/bin/java" { + t.Fatalf("systemd: %+v", s) + } + if m, _ := s.Options(17).Mitigates(); !m { + t.Error("systemd Environment not read") + } + if _, ok := ReadSetting("/home/u/app.service", []byte("x")); ok { + t.Error("a unit outside systemd's directories was read") + } + if _, ok := ReadSetting("/opt/x/bin/setenv.sh", bytes.Repeat([]byte("a"), 2<<20)); ok { + t.Error("a huge settings file was read") + } +} + +func FuzzInspect(f *testing.F) { + f.Add("/a/lib/server/libjvm.so", testlib.Build(testlib.Lib{Version: "21.0.13+9", VBMI: true})) + f.Add("/a/lib/server/libjvm.dylib", testlib.Build(testlib.Lib{Format: "fat"})) + f.Add("/a/bin/server/jvm.dll", testlib.Build(testlib.Lib{Format: "pe", Version: "17.0.6+10"})) + f.Fuzz(func(t *testing.T, p string, b []byte) { + l, ok := Inspect(p, b) + if ok && (l == nil || len(l.Archs) == 0) { + t.Fatal("inspected without an architecture") + } + }) +} + +func FuzzSettings(f *testing.F) { + f.Add("/opt/x/config/jvm.options", []byte("17-:-XX:-UseBASE64Intrinsics\n")) + f.Add("/etc/systemd/system/a.service", []byte("Environment=\"JAVA_TOOL_OPTIONS=-XX:UseAVX=2\"\nExecStart=sh -c 'java -XX:UseAVX=1'\n")) + f.Add("/opt/x/bin/setenv.sh", []byte("CATALINA_OPTS=\"$CATALINA_OPTS -XX:-UseBASE64Intrinsics\"\n")) + f.Fuzz(func(t *testing.T, p string, b []byte) { + s, ok := ReadSetting(p, b) + if !ok { + return + } + for _, feat := range []int{0, 8, 17, 21} { + s.Options(feat).Mitigates() + } + var o Options + o.Apply(string(b), "fuzz") + JavaOptions(Words(string(b))) + }) +} + +func TestInspectLargePrintable(t *testing.T) { + // Printable text everywhere used to make the version-line search scan + // from every position. + b := append(testlib.Build(testlib.Lib{}), bytes.Repeat([]byte("VM (1) for a-b JRE x "), (16<<20)/21)...) + start := time.Now() + if l, ok := Inspect("/a/lib/server/libjvm.so", b); !ok || l.JREVersion != "" { + t.Fatalf("%+v", l) + } + if d := time.Since(start); d > 5*time.Second { + t.Errorf("16 MiB of printable text took %s", d) + } +} diff --git a/detectors/java4shell/internal/jvm/options.go b/detectors/java4shell/internal/jvm/options.go new file mode 100644 index 0000000..f3d114b --- /dev/null +++ b/detectors/java4shell/internal/jvm/options.go @@ -0,0 +1,243 @@ +package jvm + +import ( + "fmt" + "strconv" + "strings" +) + +// Options is what JVM options say about the vulnerable decoder: the last +// value given to -XX:±UseBASE64Intrinsics and -XX:UseAVX. HotSpot applies +// options in order, so the last one wins. +type Options struct { + Base64 *bool // nil: not set + UseAVX *int + // Where names the setting that decided each value ("JAVA_TOOL_OPTIONS"). + Base64From, AVXFrom string +} + +// maxOptionText bounds an option string: environment values and option +// files are short. +const maxOptionText = 256 << 10 + +// Apply reads one option string from source (an environment variable, a +// file) on top of what is already set. +func (o *Options) Apply(text, source string) { + if len(text) > maxOptionText { + text = text[:maxOptionText] + } + for _, w := range Words(text) { + o.ApplyWord(w, source) + } +} + +// ApplyWord reads one option. +func (o *Options) ApplyWord(w, source string) { + switch { + case w == "-XX:-UseBASE64Intrinsics": + f := false + o.Base64, o.Base64From = &f, source + case w == "-XX:+UseBASE64Intrinsics": + t := true + o.Base64, o.Base64From = &t, source + case strings.HasPrefix(w, "-XX:UseBASE64Intrinsics="): + switch strings.TrimPrefix(w, "-XX:UseBASE64Intrinsics=") { + case "false": + f := false + o.Base64, o.Base64From = &f, source + case "true": + t := true + o.Base64, o.Base64From = &t, source + } + case strings.HasPrefix(w, "-XX:UseAVX="): + if n, err := strconv.Atoi(strings.TrimPrefix(w, "-XX:UseAVX=")); err == nil && n >= 0 && n < 100 { + o.UseAVX, o.AVXFrom = &n, source + } + } +} + +// Mitigates reports whether the options keep HotSpot from generating the +// vulnerable decoder, and how. +func (o Options) Mitigates() (bool, string) { + if o.Base64 != nil && !*o.Base64 { + return true, fmt.Sprintf("-XX:-UseBASE64Intrinsics (from %s) turns the Base64 intrinsics off", o.Base64From) + } + if o.UseAVX != nil && *o.UseAVX <= 2 { + return true, fmt.Sprintf("-XX:UseAVX=%d (from %s) turns AVX-512 off, so the AVX-512 VBMI decoder is not generated", *o.UseAVX, o.AVXFrom) + } + return false, "" +} + +// Words splits an option string the way a shell or the JVM's option +// variables do: on white space, with single and double quotes grouping. +func Words(s string) []string { + var out []string + var b strings.Builder + in := false + var q byte + for i := 0; i < len(s); i++ { + c := s[i] + switch { + case q != 0: + if c == q { + q = 0 + } else { + b.WriteByte(c) + } + case c == '"' || c == '\'': + q, in = c, true + case c == '\\' && i+1 < len(s): + i++ + b.WriteByte(s[i]) + in = true + case c == ' ' || c == '\t' || c == '\n' || c == '\r': + if in { + out = append(out, b.String()) + b.Reset() + in = false + } + default: + b.WriteByte(c) + in = true + } + } + if in { + out = append(out, b.String()) + } + return out +} + +// EnvVars are the variables the JVM itself reads, in the order HotSpot +// applies them: JAVA_TOOL_OPTIONS first, then JDK_JAVA_OPTIONS (the java +// launcher, Java 9 and later, puts it before the command line), then the +// command line, then _JAVA_OPTIONS, which overrides everything. +var EnvVars = []string{"JAVA_TOOL_OPTIONS", "JDK_JAVA_OPTIONS", "_JAVA_OPTIONS"} + +// ScriptVars are variables only launch scripts read (catalina.sh, +// Atlassian's setenv.sh, many start scripts). +var ScriptVars = []string{"JAVA_OPTS", "CATALINA_OPTS", "JVM_SUPPORT_RECOMMENDED_ARGS", "JVM_OPTS", "ES_JAVA_OPTS", "SOLR_OPTS", "KAFKA_OPTS", "KAFKA_HEAP_OPTS"} + +// FromEnv computes the options a JVM started in env with command line cmd +// gets. feature is the Java release (JDK_JAVA_OPTIONS needs 9 or later); +// scriptVars lists the script variables the launch passes on. +func FromEnv(env map[string]string, cmd []string, feature int, scriptVars map[string]bool) Options { + var o Options + if v, ok := env["JAVA_TOOL_OPTIONS"]; ok { + o.Apply(v, "JAVA_TOOL_OPTIONS") + } + if v, ok := env["JDK_JAVA_OPTIONS"]; ok && feature >= 9 { + o.Apply(v, "JDK_JAVA_OPTIONS") + } + for _, name := range ScriptVars { + if v, ok := env[name]; ok && scriptVars[name] { + o.Apply(v, name) + } + } + for _, w := range cmd { + o.ApplyWord(w, "the command line") + } + if v, ok := env["_JAVA_OPTIONS"]; ok { + o.Apply(v, "_JAVA_OPTIONS") + } + return o +} + +// EnvMap turns "KEY=value" entries into a map, the last one winning. +func EnvMap(env []string) map[string]string { + m := map[string]string{} + for _, e := range env { + if k, v, ok := strings.Cut(e, "="); ok { + m[k] = v + } + } + return m +} + +// valueOptions are java launcher options followed by a separate value. +var valueOptions = map[string]bool{"-cp": true, "-classpath": true, "--class-path": true, "-p": true, "--module-path": true, + "--upgrade-module-path": true, "--add-modules": true, "--enable-native-access": true, "--limit-modules": true, + "--add-reads": true, "--add-exports": true, "--add-opens": true, "--patch-module": true, "--source": true} + +// JavaOptions returns the JVM options a container command gives the java +// launcher: the words after a java executable up to the main class or +// -jar. A "sh -c" script is split into words first. ok is false when the +// command does not start java directly. +func JavaOptions(cmd []string) ([]string, bool) { + if len(cmd) > 1024 { + cmd = cmd[:1024] + } + words := cmd + for i := 0; i+2 < len(cmd); i++ { + if b := baseName(cmd[i]); (b == "sh" || b == "bash" || b == "ash" || b == "dash") && cmd[i+1] == "-c" { + words = Words(cmd[i+2]) + break + } + } + for i, w := range words { + if baseName(w) != "java" { + continue + } + var out []string + for j := i + 1; j < len(words); j++ { + x := words[j] + if x == "-jar" || x == "-m" || x == "--module" || !strings.HasPrefix(x, "-") { + break + } + if valueOptions[x] { + j++ + continue + } + out = append(out, x) + } + return out, true + } + return nil, false +} + +func baseName(p string) string { + if i := strings.LastIndexAny(p, "/\\"); i >= 0 { + p = p[i+1:] + } + return strings.TrimSuffix(strings.ToLower(p), ".exe") +} + +// CommandWords returns every word of a container command, with the words +// of a "sh -c" script split out. +func CommandWords(cmd []string) []string { + if len(cmd) > 1024 { + cmd = cmd[:1024] + } + var out []string + for i, w := range cmd { + if i > 0 && cmd[i-1] == "-c" && i >= 2 { + if b := baseName(cmd[i-2]); b == "sh" || b == "bash" || b == "ash" || b == "dash" { + out = append(out, Words(w)...) + continue + } + } + out = append(out, w) + } + return out +} + +// Undoes reports what in a command or option text can turn the decoder +// back on, wherever the java it starts is: -XX:+UseBASE64Intrinsics, +// -XX:UseAVX=3 or higher, or a java @argfile this scan cannot read. +func Undoes(words []string) (base64, avx, argfile bool) { + java := false + for _, w := range words { + switch { + case baseName(w) == "java" || strings.HasSuffix(w, "/bin/java"): + java = true + case w == "-XX:+UseBASE64Intrinsics" || w == "-XX:UseBASE64Intrinsics=true": + base64 = true + case strings.HasPrefix(w, "-XX:UseAVX="): + if n, err := strconv.Atoi(strings.TrimPrefix(w, "-XX:UseAVX=")); err != nil || n >= 3 { + avx = true + } + case java && strings.HasPrefix(w, "@") && len(w) > 1: + argfile = true + } + } + return +} diff --git a/detectors/java4shell/internal/jvm/review2_test.go b/detectors/java4shell/internal/jvm/review2_test.go new file mode 100644 index 0000000..b511c6d --- /dev/null +++ b/detectors/java4shell/internal/jvm/review2_test.go @@ -0,0 +1,150 @@ +package jvm + +import ( + "testing" + + "github.com/DefectDojo/earlylight/detectors/java4shell/internal/testlib" +) + +// Review finding 1: musl builds name their platform linux-amd64-musl. +func TestInspectMusl(t *testing.T) { + l, ok := Inspect("/opt/java/openjdk/lib/server/libjvm.so", testlib.Build(testlib.Lib{Version: "21.0.13+9-LTS", VBMI: true, Musl: true, Extra: "\x00j9vm\x00"})) + if !ok || !l.HotSpot() || l.JREVersion != "21.0.13+9-LTS" || l.Platform != "linux-amd64-musl" { + t.Fatalf("musl: %+v", l) + } + if l.OpenJ9 { + t.Error("the bytes j9vm made a HotSpot library OpenJ9") + } + // Without a readable version line, four bytes do not make it OpenJ9 + // when the HotSpot decoder stubs are there. + l, _ = Inspect("/opt/java/openjdk/lib/server/libjvm.so", testlib.Build(testlib.Lib{VBMI: true, Extra: "\x00j9vm\x00"})) + if l.OpenJ9 { + t.Error("an unreadable HotSpot library with j9vm bytes taken for OpenJ9") + } +} + +// Review finding 5: only options a launcher variable ends up with count. +func TestSettingsOnlyWhatLaunches(t *testing.T) { + for _, text := range []string{ + "if [ \"$X\" = 1 ]; then JVM_SUPPORT_RECOMMENDED_ARGS=\"-XX:-UseBASE64Intrinsics\"; fi\n", + "if [ \"$X\" = 1 ]; then\n JVM_SUPPORT_RECOMMENDED_ARGS=\"-XX:-UseBASE64Intrinsics\"\nfi\n", + "OLD_OPTS=\"-XX:-UseBASE64Intrinsics\"\n", + "JAVA_OPTS=\"-XX:-UseBASE64Intrinsics\"\nJAVA_OPTS=\"-Xmx2g\"\n", + "echo \"to mitigate Java4Shell add -XX:-UseBASE64Intrinsics\"\n", + } { + s, ok := ReadSetting("/opt/atlassian/jira/bin/setenv.sh", []byte(text)) + if !ok { + t.Fatal("not read") + } + if m, _ := s.Options(21).Mitigates(); m { + t.Errorf("counted: %q", text) + } + } + for _, text := range []string{ + "JAVA_OPTS=\"-Xmx2g\"\nJAVA_OPTS=\"$JAVA_OPTS -XX:-UseBASE64Intrinsics\"\n", + "export CATALINA_OPTS=\"${CATALINA_OPTS} -XX:-UseBASE64Intrinsics\"\n", + "JVM_SUPPORT_RECOMMENDED_ARGS='-XX:-UseBASE64Intrinsics'\n", + } { + s, _ := ReadSetting("/opt/atlassian/jira/bin/setenv.sh", []byte(text)) + if m, _ := s.Options(21).Mitigates(); !m { + t.Errorf("not counted: %q", text) + } + } + // A Windows batch file applies to Windows runtimes only. + s, _ := ReadSetting("/opt/tomcat/bin/setenv.bat", []byte(":: set CATALINA_OPTS=-XX:-UseBASE64Intrinsics\nrem set JAVA_OPTS=-XX:-UseBASE64Intrinsics\n")) + if m, _ := s.Options(21).Mitigates(); m { + t.Error("batch comments counted") + } + s, _ = ReadSetting("/opt/tomcat/bin/setenv.bat", []byte("set \"CATALINA_OPTS=%CATALINA_OPTS% -XX:-UseBASE64Intrinsics\"\n")) + if m, _ := s.Options(21).Mitigates(); !m || !s.WindowsOnly { + t.Errorf("batch set: %v windows-only %v", m, s.WindowsOnly) + } + // Cassandra names the Java release in the file name. + s, _ = ReadSetting("/opt/cassandra/conf/jvm11-server.options", []byte("-XX:UseAVX=2\n")) + if m, _ := s.Options(21).Mitigates(); m { + t.Error("jvm11-server.options applied to Java 21") + } + if m, _ := s.Options(11).Mitigates(); !m { + t.Error("jvm11-server.options not applied to Java 11") + } +} + +func FuzzParseRelease(f *testing.F) { + f.Add([]byte("JAVA_VERSION=\"21.0.13\"\nJAVA_RUNTIME_VERSION=\"21.0.13+9-LTS\"\nOS_ARCH=\"x86_64\"\nMODULES=\"java.base\"\n")) + f.Add([]byte("JAVA_VERSION=21\r\nJVM_VARIANT=Openj9\n")) + f.Fuzz(func(t *testing.T, b []byte) { + r, ok := ParseRelease(b) + if !ok { + return + } + if r["JAVA_VERSION"] == "" { + t.Fatal("accepted without JAVA_VERSION") + } + _ = r.Version() + _ = r.OpenJ9() + _ = r.JLinked() + }) +} + +// Round-2 finding 2: what the setenv.sh model still accepted. +func TestSetenvModelRound2(t *testing.T) { + jira := "/opt/atlassian/jira/bin/setenv.sh" + tomcat := "/opt/tomcat/bin/setenv.sh" + for _, c := range []struct { + file, text string + want bool + }{ + {jira, "never_called() {\n JVM_SUPPORT_RECOMMENDED_ARGS=\"-XX:-UseBASE64Intrinsics\"\n}\n", false}, + {jira, "function f {\n CATALINA_OPTS=\"-XX:-UseBASE64Intrinsics\"\n}\n", false}, + {jira, "[ \"$X\" = 1 ] && {\n JVM_SUPPORT_RECOMMENDED_ARGS=\"-XX:-UseBASE64Intrinsics\"\n}\n", false}, + {jira, "if [ -n \"$Y\" ]; then\n echo done\n JVM_SUPPORT_RECOMMENDED_ARGS=\"-XX:-UseBASE64Intrinsics\"\nfi\n", false}, + {jira, "cat < 0 || len(s.ExecOpts) > 0 || len(s.Env) > 0 +} + +// ApplyTo applies the file's options, in order, on top of o. For a +// systemd unit the order is HotSpot's: JAVA_TOOL_OPTIONS, JDK_JAVA_OPTIONS, +// the command line, then _JAVA_OPTIONS. +func (s Setting) ApplyTo(o *Options, feature int) { + if s.Kind == "systemd" { + env := map[string]string{} + for k, v := range s.Env { + if k != "_JAVA_OPTIONS" { + env[k] = v + } + } + *o = FromEnv(env, nil, feature, nil) + for _, w := range s.ExecOpts { + o.ApplyWord(w, s.File) + } + if v, ok := s.Env["_JAVA_OPTIONS"]; ok { + o.Apply(v, "_JAVA_OPTIONS in "+s.File) + } + return + } + for _, l := range s.lines { + if feature > 0 && (l.lo > 0 && feature < l.lo || l.hi > 0 && feature > l.hi) { + continue + } + o.ApplyWord(l.word, s.File) + } +} + +// SettingKind classifies a path, "" when it is not a settings file. +func SettingKind(p string) string { + base := path.Base(p) + dir := path.Dir(p) + switch { + case base == "setenv.sh" || base == "setenv.bat": + return "setenv" + case base == "jvm.options" || strings.HasPrefix(base, "jvm") && strings.HasSuffix(base, ".options") || + strings.HasSuffix(base, ".options") && path.Base(dir) == "jvm.options.d": + return "jvm.options" + case strings.HasSuffix(base, ".vmoptions"): + return "vmoptions" + case (strings.HasSuffix(base, ".service") || strings.HasSuffix(base, ".conf") && strings.HasSuffix(dir, ".service.d")) && systemdDir(p): + return "systemd" + } + return "" +} + +func systemdDir(p string) bool { + for _, d := range []string{"/etc/systemd/system/", "/usr/lib/systemd/system/", "/lib/systemd/system/", "/run/systemd/system/", "/usr/local/lib/systemd/system/"} { + if strings.HasPrefix(p, d) { + return true + } + } + return false +} + +// maxSetting bounds a settings file. +const maxSetting = 1 << 20 + +var ( + xxRE = regexp.MustCompile(`-XX:(?:[+-]UseBASE64Intrinsics|UseBASE64Intrinsics=(?:true|false)|UseAVX=\d{1,2})\b`) + selectorRE = regexp.MustCompile(`^(\d{1,3})(-(\d{1,3})?)?:`) + fileReleaseRE = regexp.MustCompile(`^jvm(\d{1,2})-`) + assignRE = regexp.MustCompile(`^(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)=(.*)$`) + batSetRE = regexp.MustCompile(`(?i)^set\s+"?([A-Za-z_][A-Za-z0-9_]*)=(.*?)"?\s*$`) + shVarRE = regexp.MustCompile(`\$\{([A-Za-z_][A-Za-z0-9_]*)\}|\$([A-Za-z_][A-Za-z0-9_]*)`) + batVarRE = regexp.MustCompile(`%([A-Za-z_][A-Za-z0-9_]*)%`) +) + +// ReadSetting reads a settings file. ok is false when p is not one, or it +// is too large. +func ReadSetting(p string, b []byte) (Setting, bool) { + kind := SettingKind(p) + if kind == "" || len(b) > maxSetting { + return Setting{}, false + } + s := Setting{File: p, Kind: kind} + dir := path.Dir(p) + switch kind { + case "setenv", "vmoptions": + if path.Base(dir) == "bin" { + dir = path.Dir(dir) + } + s.Scope = scopeOrNone(dir) + case "jvm.options": + if path.Base(dir) == "jvm.options.d" { + dir = path.Dir(dir) + } + if b := path.Base(dir); b == "config" || b == "conf" || b == "etc" { + dir = path.Dir(dir) + } + s.Scope = scopeOrNone(dir) + case "systemd": + u := path.Base(p) + if strings.HasSuffix(dir, ".service.d") { + u = path.Base(dir) + u = strings.TrimSuffix(u, ".d") + } + s.Unit = u + s.Env = map[string]string{} + } + if kind == "setenv" { + s.WindowsOnly = strings.HasSuffix(p, ".bat") + readSetenv(&s, string(b)) + return s, true + } + // Cassandra names the Java release a file is for: jvm11-server.options. + fileLo := 0 + if m := fileReleaseRE.FindStringSubmatch(path.Base(p)); m != nil && kind == "jvm.options" { + fileLo, _ = strconv.Atoi(m[1]) + } + for _, raw := range strings.Split(string(b), "\n") { + line := strings.TrimSpace(strings.TrimSuffix(raw, "\r")) + if line == "" || line[0] == '#' || line[0] == ';' || strings.HasPrefix(strings.ToLower(line), "rem ") { + continue + } + switch kind { + case "jvm.options", "vmoptions": + lo, hi := 0, 0 + if m := selectorRE.FindStringSubmatch(line); m != nil { + lo, _ = strconv.Atoi(m[1]) + switch { + case m[2] == "": + hi = lo + case m[3] != "": + hi, _ = strconv.Atoi(m[3]) + } + line = line[len(m[0]):] + } + if fileLo > 0 { + lo, hi = fileLo, fileLo + } + if xxRE.MatchString(line) && strings.HasPrefix(line, "-XX:") { + s.lines = append(s.lines, optLine{lo, hi, strings.TrimSpace(line)}) + } + case "systemd": + s.systemdLine(line) + } + } + return s, true +} + +// broadDirs are directories too general to be one product's: a settings +// file there configures no runtime by its place. +var broadDirs = map[string]bool{"/": true, ".": true, "": true, "/usr": true, "/usr/local": true, "/usr/share": true, "/usr/lib": true, + "/opt": true, "/etc": true, "/var": true, "/srv": true, "/home": true, "/root": true, "/app": true, "/usr/local/share": true, "/var/lib": true} + +func scopeOrNone(dir string) string { + if broadDirs[dir] { + return "" + } + return dir +} + +// stripComment drops a shell comment: a "#" that starts a word, outside +// quotes. +func stripComment(line string) string { + var q byte + for i := 0; i < len(line); i++ { + c := line[i] + switch { + case q != 0: + if c == q { + q = 0 + } + case c == '"' || c == '\'': + q = c + case c == '\\': + i++ + case c == '#' && (i == 0 || line[i-1] == ' ' || line[i-1] == '\t' || line[i-1] == ';'): + return line[:i] + } + } + return line +} + +// MergeUnits joins systemd unit files the way systemd does: the unit file +// from the directory with the highest precedence (/etc, then /run, then +// /usr/local/lib, /usr/lib, /lib), then its drop-ins in name order, later +// assignments replacing earlier ones. It returns one setting per unit. +func MergeUnits(settings []Setting) []Setting { + rank := func(p string) int { + for i, d := range []string{"/etc/systemd/system/", "/run/systemd/system/", "/usr/local/lib/systemd/system/", "/usr/lib/systemd/system/", "/lib/systemd/system/"} { + if strings.HasPrefix(p, d) { + return i + } + } + return 9 + } + mains := map[string]Setting{} + drops := map[string][]Setting{} + for _, s := range settings { + if s.Kind != "systemd" { + continue + } + if strings.HasSuffix(path.Dir(s.File), ".service.d") { + drops[s.Unit] = append(drops[s.Unit], s) + continue + } + if m, ok := mains[s.Unit]; !ok || rank(s.File) < rank(m.File) { + mains[s.Unit] = s + } + } + var out []Setting + units := map[string]bool{} + for u := range mains { + units[u] = true + } + for u := range drops { + units[u] = true + } + names := make([]string, 0, len(units)) + for u := range units { + names = append(names, u) + } + sort.Strings(names) + for _, u := range names { + m := mains[u] + merged := Setting{File: m.File, Kind: "systemd", Unit: u, Exec: m.Exec, ExecOpts: m.ExecOpts, ExecSet: m.ExecSet, Env: map[string]string{}} + for k, v := range m.Env { + merged.Env[k] = v + } + ds := drops[u] + sort.SliceStable(ds, func(i, j int) bool { return path.Base(ds[i].File) < path.Base(ds[j].File) }) + for _, d := range ds { + if merged.File == "" { + merged.File = d.File + } else { + merged.File += ", " + d.File + } + for k, v := range d.Env { + merged.Env[k] = v + } + if d.ExecSet { + merged.Exec, merged.ExecOpts, merged.ExecSet = d.Exec, d.ExecOpts, true + } + } + out = append(out, merged) + } + return out +} + +func (s *Setting) systemdLine(line string) { + k, v, ok := strings.Cut(line, "=") + if !ok { + return + } + switch strings.TrimSpace(k) { + case "Environment": + // Only the variables the JVM itself reads: a script variable such + // as JAVA_OPTS reaches java only through a start script. + for _, a := range Words(v) { + name, val, ok := strings.Cut(a, "=") + if !ok { + continue + } + for _, n := range EnvVars { + if name == n { + s.Env[name] = val + } + } + } + case "ExecStart": + v = strings.TrimSpace(v) + s.ExecSet = true + s.Exec, s.ExecOpts = "", nil + v = strings.TrimLeft(v, "-@:+!") + ws := Words(v) + if len(ws) == 0 { + return // "ExecStart=" resets the command + } + s.Exec = ws[0] + if opts, ok := JavaOptions(ws); ok { + for _, w := range opts { + if xxRE.MatchString(w) { + s.ExecOpts = append(s.ExecOpts, w) + } + } + } + } +} + +// launcherVars are the variables a start script passes to java. +func launcherVar(name string) bool { + for _, n := range EnvVars { + if n == name { + return true + } + } + for _, n := range ScriptVars { + if n == name { + return true + } + } + return false +} + +// shellControl are the words that make what follows conditional or +// repeated. +var shellControl = map[string]bool{"if": true, "then": true, "else": true, "elif": true, "fi": true, "case": true, "esac": true, + "for": true, "while": true, "until": true, "do": true, "done": true, "&&": true, "||": true, "{": true, "}": true, "function": true} + +// setenvVars are the variables a setenv file's launcher passes to java, +// in the order the JVM sees them: the JVM's own JAVA_TOOL_OPTIONS and +// JDK_JAVA_OPTIONS first, then catalina.sh's command line ($JAVA_OPTS then +// $CATALINA_OPTS; Atlassian's setenv.sh puts JVM_SUPPORT_RECOMMENDED_ARGS +// into CATALINA_OPTS), then _JAVA_OPTIONS, which overrides everything. +func setenvVars(scope string) []string { + vars := []string{"JAVA_TOOL_OPTIONS", "JDK_JAVA_OPTIONS", "JAVA_OPTS"} + if strings.Contains(scope, "/atlassian/") { + vars = append(vars, "JVM_SUPPORT_RECOMMENDED_ARGS") + } + return append(vars, "CATALINA_OPTS", "_JAVA_OPTIONS") +} + +// jvmEnvVar reports the variables the JVM reads itself: a shell script +// must export them for java to see them. +func jvmEnvVar(name string) bool { + return name == "JAVA_TOOL_OPTIONS" || name == "JDK_JAVA_OPTIONS" || name == "_JAVA_OPTIONS" +} + +// readSetenv reads a start script's settings file the way the launcher +// uses it: only the variables that launcher passes to java count (the +// JVM's own variables only when exported), the last assignment wins +// ("$VAR ..." appends, unset clears), and a file that sets, unsets or +// exports one of them under a condition, in a loop, in a function or a +// { } group counts for nothing, since which value java gets cannot be +// known without running it. Heredoc text is skipped. What it does not +// model (multi-line values, helper variables, sourced files) is not +// counted, which leaves the runtime vulnerable. Batch files +// (setenv.bat) are read with "set NAME=value" and %NAME%. +func readSetenv(s *Setting, text string) { + wanted := map[string]bool{} + order := setenvVars(s.Scope) + for _, n := range order { + wanted[n] = true + } + var vals map[string]string + var ok bool + if s.WindowsOnly { + vals, ok = readBatch(text, wanted) + } else { + vals, ok = readShell(text, wanted) + } + if !ok { + return + } + for _, n := range order { + for _, w := range xxRE.FindAllString(vals[n], -1) { + s.lines = append(s.lines, optLine{0, 0, w}) + } + } +} + +func readBatch(text string, wanted map[string]bool) (map[string]string, bool) { + vals := map[string]string{} + for _, raw := range strings.Split(text, "\n") { + line := strings.TrimSpace(strings.TrimSuffix(raw, "\r")) + low := strings.ToLower(line) + if line == "" || strings.HasPrefix(low, "rem") && (len(low) == 3 || low[3] == ' ') || strings.HasPrefix(line, "::") || strings.HasPrefix(low, "@rem") { + continue + } + if strings.HasPrefix(low, "if ") || strings.HasPrefix(low, "for ") || strings.HasPrefix(low, "goto") || strings.HasPrefix(line, ":") || strings.ContainsAny(line, "()") { + for n := range wanted { + if strings.Contains(line, n) { + return nil, false + } + } + continue + } + m := batSetRE.FindStringSubmatch(line) + if m == nil || !wanted[m[1]] { + continue + } + vals[m[1]] = batVarRE.ReplaceAllStringFunc(m[2], func(v string) string { return vals[strings.Trim(v, "%")] }) + } + return vals, true +} + +var heredocRE = regexp.MustCompile(`<<(-?)\s*['"]?([A-Za-z_][A-Za-z0-9_]*)['"]?`) + +// shellCmd is one simple command of a line and whether it runs only +// depending on another (after && or ||, or in a pipeline). +type shellCmd struct { + text string + cond bool +} + +// SplitCommands returns the simple commands of a shell line (split on +// ; && || | & outside quotes). +func SplitCommands(line string) []string { + var out []string + for _, c := range splitCommands(line) { + out = append(out, c.text) + } + return out +} + +// splitCommands splits a line on ; && || | & outside quotes. +func splitCommands(line string) []shellCmd { + var out []shellCmd + var q byte + start, cond := 0, false + add := func(end int, next bool) { + out = append(out, shellCmd{strings.TrimSpace(line[start:end]), cond}) + cond = next + } + for i := 0; i < len(line); i++ { + c := line[i] + switch { + case q != 0: + if c == q { + q = 0 + } else if c == '\\' && q == '"' { + i++ + } + case c == '"' || c == '\'': + q = c + case c == '\\': + i++ + case c == ';': + add(i, false) + start = i + 1 + case c == '&' || c == '|': + two := i+1 < len(line) && line[i+1] == c + add(i, true) + if two { + i++ + } + start = i + 1 + } + } + add(len(line), false) + return out +} + +func readShell(text string, wanted map[string]bool) (map[string]string, bool) { + vals := map[string]string{} + exported := map[string]bool{} + depth := 0 + heredoc, dashed := "", false +lines: + for _, raw := range strings.Split(text, "\n") { + line := strings.TrimSuffix(raw, "\r") + if heredoc != "" { + end := line + if dashed { + end = strings.TrimLeft(end, "\t") + } + if end == heredoc { + heredoc = "" + } + continue + } + line = strings.TrimSpace(stripComment(line)) + if line == "" { + continue + } + if m := heredocRE.FindStringSubmatch(line); m != nil { + heredoc, dashed = m[2], m[1] == "-" + } + for _, c := range splitCommands(line) { + words := Words(c.text) + if len(words) == 0 { + continue + } + // Leading keywords that start a block or a branch. A function + // header ("function f {", "f() {") opens one block, whose "{" + // may be on the same line. + fn := false + for len(words) > 0 { + switch words[0] { + case "function": + depth++ + fn = true + words = words[1:] + if len(words) > 0 { + words = words[1:] // the name + } + continue + case "{": + if !fn { + depth++ + } + fn = false + words = words[1:] + continue + case "if", "case", "for", "while", "until": + depth++ + words = words[1:] + continue + case "then", "else", "elif", "do", "!": + words = words[1:] + continue + case "fi", "esac", "done", "}": + if depth > 0 { + depth-- + } + words = words[1:] + continue + } + break + } + if len(words) > 0 && strings.HasSuffix(words[0], "()") { + depth++ // name() { ...: a function body + fn = true + words = words[1:] + } + if n := len(words); n > 0 && words[n-1] == "{" { + // "&& {" opens a group whose end is a "}" line; a function + // header's "{" belongs to the block it already opened. + if !fn { + depth++ + } + words = words[:n-1] + } + if fn && len(words) == 1 && words[0] == "{" { + words = nil + } + if len(words) == 0 { + continue + } + cond := c.cond || depth > 0 + touches := func(n string) bool { return wanted[n] } + // A java the file starts itself (exec java ...) that turns + // the decoder back on, or reads options from an @argfile, + // makes the file's mitigation unprovable. + if b64, avx, argfile := Undoes(words); b64 || avx || argfile { + return nil, false + } + switch words[0] { + case "return", "exit": + if !cond { + break lines // nothing after it runs + } + continue + case "unset", "export", "readonly", "declare", "local": + for _, w := range words[1:] { + name, value, assign := strings.Cut(w, "=") + if !touches(name) { + continue + } + if cond { + return nil, false + } + switch { + case words[0] == "unset": + delete(vals, name) + delete(exported, name) + case assign: + vals[name] = expand(value, vals) + exported[name] = words[0] == "export" || exported[name] + default: + exported[name] = exported[name] || words[0] == "export" + } + } + continue + } + m := assignRE.FindStringSubmatch(words[0]) + if m == nil || !touches(m[1]) { + continue + } + if cond || len(words) > 1 { + // Under a condition, or a prefix assignment for one command + // (NAME=value cmd), which does not persist. + if !cond && len(words) > 1 { + continue + } + return nil, false + } + vals[m[1]] = expand(m[2], vals) + } + } + for n := range vals { + if jvmEnvVar(n) && !exported[n] { + delete(vals, n) // not exported: java does not see it + } + } + return vals, true +} + +// expand substitutes $NAME and ${NAME} with the values set so far. +func expand(v string, vals map[string]string) string { + return shVarRE.ReplaceAllStringFunc(v, func(x string) string { return vals[strings.Trim(x, "${}")] }) +} diff --git a/detectors/java4shell/internal/knowledge/knowledge.go b/detectors/java4shell/internal/knowledge/knowledge.go new file mode 100644 index 0000000..81fa877 --- /dev/null +++ b/detectors/java4shell/internal/knowledge/knowledge.go @@ -0,0 +1,373 @@ +// Package knowledge holds what is known about Java4Shell (CVE-2026-114938) +// release by release, and judges a Java version against it. FACTS.md, next +// to the detector, gives the source of every number here. +package knowledge + +import ( + "fmt" + "strconv" + "strings" +) + +// CVE is the vulnerability this detector checks. +const CVE = "CVE-2026-114938" + +// Bug is the OpenJDK change taken as the fix. The mapping is inferred: +// the OpenJDK advisory that would state it could not be read, and the +// public descriptions of the CVE match this change (FACTS.md). +const Bug = "JDK-8393212" + +// BugRef is how reasons name the fix. +const BugRef = "JDK-8393212, inferred to be this CVE's fix" + +// Checked is the date the release data was last checked: every "as of" +// statement (a vendor or distribution had not published a fix) is as of +// this date. +const Checked = "2026-11-17" + +// Dates the release data rests on. +const ( + // Disclosed is when the CVE and the fixed releases were published. + Disclosed = "2026-11-17" + // FixReleased is the date of the OpenJDK update releases with the fix. + FixReleased = "2026-11-15" + // MainlineFixed is when the fix reached OpenJDK mainline. + MainlineFixed = "2026-11-16" + // VendorFixPublished is when Amazon Corretto and SapMachine published + // their fixed releases. + VendorFixPublished = "2026-11-17" +) + +// AsOf says a status statement's date. +func AsOf() string { return "as of " + Checked + ", the date of this detector's release data" } + +// Status is what the release data says about a version. +type Status string + +const ( + // Vulnerable: a release with the AVX-512 VBMI decode path and without the fix. + Vulnerable Status = "vulnerable" + // Fixed: a release with the fix. + Fixed Status = "fixed" + // NotAffected: a release without the vulnerable code. + NotAffected Status = "not-affected" + // Unknown: the release data does not settle it. + Unknown Status = "unknown" +) + +// Version is a Java version as the runtime reports it: its numbers +// (feature, interim, update, patch and any vendor numbers), the build number +// after "+" and whether it is an early-access build. +type Version struct { + Nums []int + Build int // -1 when absent + EA bool // a pre-release or self-built JDK: -ea, -beta, -internal, adhoc + // SelfBuilt is a JDK built from source (-internal, adhoc): its build + // number is not the release's, so it cannot place it before or after a + // build boundary. + SelfBuilt bool + Raw string +} + +// Feature is the major release (8, 11, 17...). +func (v Version) Feature() int { + if len(v.Nums) == 0 { + return 0 + } + return v.Nums[0] +} + +// String is the dotted version without the build. +func (v Version) String() string { return join(v.Nums) } + +func join(ns []int) string { + parts := make([]string, len(ns)) + for i, n := range ns { + parts[i] = strconv.Itoa(n) + } + return strings.Join(parts, ".") +} + +const maxVersionLen = 120 + +// Parse reads the version strings Java runtimes report: "17.0.21.1+2-LTS", +// "21.0.13.0.3+1", "17.0.21+9-1-deb12u1-Debian", "1.8.0_452-b09", +// "28-ea+24", "25.0.5". A string that does not start with a version is +// refused. +func Parse(s string) (Version, bool) { + v := Version{Build: -1, Raw: s} + s = strings.TrimSpace(s) + if s == "" || len(s) > maxVersionLen { + return v, false + } + // Java 8 and older: 1.8.0_452-b09. + if rest, ok := strings.CutPrefix(s, "1."); ok { + feat, rest2 := leadingInt(rest) + if feat < 2 || feat > 8 || !strings.HasPrefix(rest2, ".0") { + return v, false + } + v.Nums = []int{feat, 0} + rest2 = strings.TrimPrefix(rest2, ".0") + if r, ok := strings.CutPrefix(rest2, "_"); ok { + u, r2 := leadingInt(r) + if u < 0 { + return v, false + } + v.Nums = append(v.Nums, u) + rest2 = r2 + } + if i := strings.Index(rest2, "-b"); i >= 0 { + if b, _ := leadingInt(rest2[i+2:]); b >= 0 { + v.Build = b + } + } + v.EA = strings.Contains(rest2, "-ea") + return v, true + } + rest := s + for { + n, r := leadingInt(rest) + if n < 0 { + break + } + v.Nums = append(v.Nums, n) + rest = r + if len(v.Nums) > 8 { + return v, false + } + if !strings.HasPrefix(rest, ".") { + break + } + rest = rest[1:] + } + if len(v.Nums) == 0 || v.Nums[0] < 9 || v.Nums[0] > 99 { + return v, false + } + // A pre-release identifier ("-ea", "-internal") comes before the build. + if r, ok := strings.CutPrefix(rest, "-"); ok { + pre := r + rest = "" + if i := strings.IndexByte(r, '+'); i >= 0 { + pre, rest = r[:i], r[i:] + } + switch { + case pre == "", strings.EqualFold(pre, "LTS"), pre[0] >= '0' && pre[0] <= '9': + // A vendor suffix (21.0.13.1-LTS) or an rpm release + // (21.0.13.1.2-1.el9), not a pre-release. + case strings.HasPrefix(pre, "internal"): + v.EA, v.SelfBuilt = true, true + default: + v.EA = true // ea, beta, rc, a build date + } + } + if r, ok := strings.CutPrefix(rest, "+"); ok { + if b, _ := leadingInt(r); b >= 0 { + v.Build = b + } + // A self-built JDK ("+0-adhoc.root.jdk17u") is not a release. + if strings.Contains(r, "adhoc") { + v.EA, v.SelfBuilt = true, true + } + } + return v, true +} + +func leadingInt(s string) (int, string) { + i := 0 + for i < len(s) && i < 9 && s[i] >= '0' && s[i] <= '9' { + i++ + } + if i == 0 { + return -1, s + } + n, err := strconv.Atoi(s[:i]) + if err != nil { + return -1, s + } + return n, s[i:] +} + +// Compare compares two dotted versions, missing numbers counting as zero. +func Compare(a, b []int) int { + for i := 0; i < len(a) || i < len(b); i++ { + x, y := 0, 0 + if i < len(a) { + x = a[i] + } + if i < len(b) { + y = b[i] + } + switch { + case x < y: + return -1 + case x > y: + return 1 + } + } + return 0 +} + +// Line is one Java feature release line. +type Line struct { + Feature int + // Introduced is the first version with the AVX-512 VBMI decode path + // (nil: the whole line has it). + Introduced []int + // IntroducedBuild is the first build of Introduced that has it (17.0.6+3). + IntroducedBuild int + // Fixed is the first release with the fix (nil: none), and FixedBuild + // the first build of it that has the fix: earlier builds of the same + // release (17.0.21.1+0 and +1) do not. + Fixed []int + FixedBuild int + // Note is why the line has no fix, or other context. + Note string +} + +// Lines is the release data, newest last. FACTS.md gives the sources. +var Lines = []Line{ + {Feature: 17, Introduced: []int{17, 0, 6}, IntroducedBuild: 3, Fixed: []int{17, 0, 21, 1}, FixedBuild: 2}, + {Feature: 18, Note: "JDK 18 is end of life and received no fix"}, + {Feature: 19, Note: "JDK 19 is end of life and received no fix"}, + {Feature: 20, Note: "JDK 20 is end of life and received no fix"}, + {Feature: 21, Fixed: []int{21, 0, 13, 1}, FixedBuild: 2}, + {Feature: 22, Note: "JDK 22 is end of life and received no fix"}, + {Feature: 23, Note: "JDK 23 is end of life and received no fix"}, + {Feature: 24, Note: "JDK 24 is end of life and received no fix"}, + {Feature: 25, Fixed: []int{25, 0, 5, 1}, FixedBuild: 2}, + {Feature: 26, Note: "JDK 26 is end of life (last release 26.0.2.1) and received no fix"}, + {Feature: 27, Fixed: []int{27, 0, 1, 1}, FixedBuild: 4}, +} + +// EAFeature is the line in early access, and the last EA build known to +// lack the fix (the fix reached mainline after jdk-28+24). +const ( + EAFeature = 28 + EALastUnfixed = 24 + newestFeature = 28 + firstVBMIDecode = 17 +) + +// maxPatch is the largest fourth version number taken as an OpenJDK patch +// release. +const maxPatch = 3 + +// Backports are lines whose OpenJDK releases do not have the decoder but +// whose vendor builds do: the last update known to carry the decoder +// without the fix. Oracle names Java SE 8u511b33-perf (its Performance +// Pack build of Java 8) as affected; no fixed 8u-perf build is known. +var Backports = map[int][]int{8: {8, 0, 511}} + +// JudgeBackport judges a runtime of a line without the decoder whose VM +// library carries it anyway (a vendor backport such as Oracle's Java 8 +// Performance Pack). +func JudgeBackport(v Version) (Status, string) { + last, ok := Backports[v.Feature()] + if ok && Compare(v.Nums, last) <= 0 { + return Vulnerable, fmt.Sprintf("Java %s carries the AVX-512 VBMI Base64 decoder as a vendor backport (Oracle names Java SE 8u511b33-perf as affected), and no fixed build of it was known on %s", v, Checked) + } + return Unknown, fmt.Sprintf("Java %s should predate the AVX-512 VBMI Base64 decoder, but this VM library carries its stubs: a vendor backport, and whether it has the fix is not known", v) +} + +// LineOf returns the release line of a feature release. +func LineOf(feature int) (Line, bool) { + for _, l := range Lines { + if l.Feature == feature { + return l, true + } + } + return Line{}, false +} + +// FixedVersion is the first fixed release of a line, "" when it has none. +func FixedVersion(feature int) string { + if l, ok := LineOf(feature); ok && l.Fixed != nil { + return join(l.Fixed) + } + return "" +} + +// Upgrade names the releases to move to from a line without a fix. +func Upgrade(feature int) string { + switch { + case feature <= 21: + return "21.0.13.1 (or 25.0.5.1)" + case feature <= 25: + return "25.0.5.1 (or 27.0.1.1)" + } + return "27.0.1.1 (or 25.0.5.1, a long-term support release)" +} + +// Judge says what the release data shows about a HotSpot runtime of version +// v on x86-64, with the reason in words. +func Judge(v Version) (Status, string) { + f := v.Feature() + ver := v.String() + switch { + case f == 0: + return Unknown, "the version could not be read" + case f < firstVBMIDecode: + return NotAffected, fmt.Sprintf("Java %s: OpenJDK %d has no x86-64 Base64 decode intrinsic, so it does not have the vulnerable AVX-512 code", ver, f) + case f == EAFeature && len(v.Nums) > 1: + return Unknown, fmt.Sprintf("Java %s is an update of JDK %d, which had no release when this detector's data was checked (%s)", ver, f, Checked) + case f == EAFeature: + if v.Build >= 0 && v.Build <= EALastUnfixed { + return Vulnerable, fmt.Sprintf("JDK %d build %d: the fix reached mainline after jdk-28+%d", f, v.Build, EALastUnfixed) + } + return Unknown, fmt.Sprintf("JDK %d build %s: the fix reached mainline on %s, after jdk-28+%d; check that this build includes %s", f, buildText(v), MainlineFixed, EALastUnfixed, BugRef) + case f > newestFeature: + return Unknown, fmt.Sprintf("Java %s is newer than this detector's release data (%s)", ver, Checked) + } + l, ok := LineOf(f) + if !ok { + return Unknown, fmt.Sprintf("Java %s is not in this detector's release data", ver) + } + if l.Introduced != nil { + c := Compare(v.Nums, l.Introduced) + if c == 0 && v.SelfBuilt { + return Unknown, fmt.Sprintf("Java %s is a self-built JDK of %s, the update the decoder came in with (build %d): its build number does not show whether it has it", ver, join(l.Introduced), l.IntroducedBuild) + } + if c < 0 || c == 0 && v.EA && v.Build >= 0 && v.Build < l.IntroducedBuild { + return NotAffected, fmt.Sprintf("Java %s predates %s, the first %d update with the AVX-512 VBMI Base64 decoder (8268276)", ver, join(l.Introduced), f) + } + } + if l.Fixed == nil { + return Vulnerable, fmt.Sprintf("Java %s has the AVX-512 VBMI Base64 decoder without the fix: %s", ver, l.Note) + } + // The update first (17.0.21 of 17.0.21.1): older is unfixed whatever + // follows it. + upd := v.Nums[:min(3, len(v.Nums))] + switch c := Compare(upd, l.Fixed[:3]); { + case c < 0: + return Vulnerable, fmt.Sprintf("Java %s is older than %s, the first %d release with the fix (%s)", ver, join(l.Fixed), f, BugRef) + case c > 0: + // A later update (17.0.22) was not released when the data was + // checked, and its development builds did not have the fix yet. + return Unknown, fmt.Sprintf("Java %s is a later update than %s; it was not released when this detector's data was checked (%s), and the %d update repository did not have %s yet: check its release notes", ver, join(l.Fixed), Checked, f, BugRef) + } + if len(v.Nums) >= 4 && v.Nums[3] > maxPatch { + // OpenJDK patch numbers are small (17.0.8.1, 21.0.13.1); a larger + // fourth number is a vendor's build number (Amazon Corretto's + // 21.0.13.11), which says nothing about the patch. + return Unknown, fmt.Sprintf("Java %s: the fourth number looks like a vendor build number, not an OpenJDK patch release, so the version does not show whether it has the fix (%s)", ver, BugRef) + } + switch c := Compare(v.Nums, l.Fixed); { + case c < 0: + return Vulnerable, fmt.Sprintf("Java %s is older than %s, the first %d release with the fix (%s)", ver, join(l.Fixed), f, BugRef) + case c == 0 && v.SelfBuilt: + return Unknown, fmt.Sprintf("Java %s is a self-built JDK of %s, the release the fix reached in build %d: its build number does not show whether its source had the fix (%s)", ver, join(l.Fixed), l.FixedBuild, BugRef) + case c == 0 && v.Build >= 0 && v.Build < l.FixedBuild: + return Vulnerable, fmt.Sprintf("Java %s build %d predates the fix: it reached %s in build %d (%s)", ver, v.Build, join(l.Fixed), l.FixedBuild, BugRef) + } + if v.EA { + return Unknown, fmt.Sprintf("Java %s is a pre-release or self-built JDK; check that it includes %s", ver, BugRef) + } + return Fixed, fmt.Sprintf("Java %s is at or past %s, the first %d release with the fix (%s)", ver, join(l.Fixed), f, BugRef) +} + +func buildText(v Version) string { + if v.Build < 0 { + return "(no build number)" + } + return strconv.Itoa(v.Build) +} diff --git a/detectors/java4shell/internal/knowledge/knowledge_test.go b/detectors/java4shell/internal/knowledge/knowledge_test.go new file mode 100644 index 0000000..d45927f --- /dev/null +++ b/detectors/java4shell/internal/knowledge/knowledge_test.go @@ -0,0 +1,125 @@ +package knowledge + +import ( + "strings" + "testing" +) + +func TestParse(t *testing.T) { + for _, c := range []struct { + in string + nums string + build int + ea bool + }{ + {"17.0.21.1+2-LTS", "17.0.21.1", 2, false}, + {"21.0.13.0.3+1-LTS", "21.0.13.0.3", 1, false}, + {"17.0.21+9-1-deb12u1-Debian", "17.0.21", 9, false}, + {"1.8.0_452-b09", "8.0.452", 9, false}, + {"1.8.0_452", "8.0.452", -1, false}, + {"1.8.0_511-perf-b33", "8.0.511", 33, false}, + {"28-ea+24", "28", 24, true}, + {"17.0.6-ea+2", "17.0.6", 2, true}, + {"25.0.5", "25.0.5", -1, false}, + {"21", "21", -1, false}, + {"11.0.33+7", "11.0.33", 7, false}, + {"21.0.13-internal+0-adhoc.root.src", "21.0.13", 0, true}, + } { + v, ok := Parse(c.in) + if !ok || v.String() != c.nums || v.Build != c.build || v.EA != c.ea { + t.Errorf("Parse(%q) = %v %q build %d ea %v, want %q build %d ea %v", c.in, ok, v.String(), v.Build, v.EA, c.nums, c.build, c.ea) + } + } + for _, bad := range []string{"", "abc", "1.9.0", "8", "0.1", "100.0.1", strings.Repeat("1.", 200), "1.8x", "17.0.0.0.0.0.0.0.0.0"} { + if v, ok := Parse(bad); ok { + t.Errorf("Parse(%q) accepted: %v", bad, v) + } + } +} + +func TestJudge(t *testing.T) { + for _, c := range []struct { + v string + want Status + }{ + {"1.8.0_452-b09", NotAffected}, + {"11.0.33+7", NotAffected}, + {"16.0.2+7", NotAffected}, + {"17.0.5+8", NotAffected}, + {"17.0.6-ea+2", NotAffected}, + {"17.0.6-ea+3", Vulnerable}, + {"17.0.6+10", Vulnerable}, + {"17.0.21+9", Vulnerable}, + {"17.0.21.0.3+1", Vulnerable}, + {"17.0.21.1+2", Fixed}, + {"17.0.21.2+1", Fixed}, + {"17.0.22+2", Unknown}, + {"17.0.22-ea+2", Unknown}, + {"18.0.2.1+1", Vulnerable}, + {"20.0.2+9", Vulnerable}, + {"21+35", Vulnerable}, + {"21.0.13+9", Vulnerable}, + {"21.0.13.0.3+1", Vulnerable}, + {"21.0.13.1+2", Fixed}, + {"21.0.14+1", Unknown}, + {"21.0.13.11", Unknown}, // Corretto's build number in the fourth place + {"24.0.2+12", Vulnerable}, + {"25.0.5+8", Vulnerable}, + {"25.0.5.1+2", Fixed}, + {"26.0.2.1+1", Vulnerable}, + {"27+35", Vulnerable}, + {"27.0.1.0.3+1", Vulnerable}, + {"27.0.1.1+4", Fixed}, + {"27.0.2+1", Unknown}, + {"28-ea+24", Vulnerable}, + {"28-ea+25", Unknown}, + {"29-ea+1", Unknown}, + } { + v, ok := Parse(c.v) + if !ok { + t.Fatalf("Parse(%q) failed", c.v) + } + got, why := Judge(v) + if got != c.want || why == "" { + t.Errorf("Judge(%s) = %s (%s), want %s", c.v, got, why, c.want) + } + } +} + +func TestLinesConsistent(t *testing.T) { + prev := 0 + for _, l := range Lines { + if l.Feature <= prev { + t.Fatalf("lines out of order at %d", l.Feature) + } + prev = l.Feature + if l.Fixed == nil && l.Note == "" { + t.Errorf("line %d has no fix and no note", l.Feature) + } + if l.Fixed != nil && l.Fixed[0] != l.Feature { + t.Errorf("line %d fixed version %v is on another line", l.Feature, l.Fixed) + } + if l.Fixed != nil && l.Introduced != nil && Compare(l.Introduced, l.Fixed) >= 0 { + t.Errorf("line %d fixed before it was introduced", l.Feature) + } + } +} + +func FuzzParse(f *testing.F) { + for _, s := range []string{"17.0.21.1+2-LTS", "1.8.0_452-b09", "28-ea+24", "21.0.13-internal+0-adhoc"} { + f.Add(s) + } + f.Fuzz(func(t *testing.T, s string) { + v, ok := Parse(s) + if !ok { + return + } + if len(v.Nums) == 0 || len(v.Nums) > 9 { + t.Fatalf("Parse(%q) gave %d numbers", s, len(v.Nums)) + } + st, why := Judge(v) + if st == "" || why == "" { + t.Fatalf("Judge(%q) gave no verdict or reason", s) + } + }) +} diff --git a/detectors/java4shell/internal/knowledge/review2_test.go b/detectors/java4shell/internal/knowledge/review2_test.go new file mode 100644 index 0000000..d10ff42 --- /dev/null +++ b/detectors/java4shell/internal/knowledge/review2_test.go @@ -0,0 +1,79 @@ +package knowledge + +import ( + "strings" + "testing" +) + +// Review findings 2, 12 and 13. +func TestReviewBuilds(t *testing.T) { + for _, c := range []struct { + v string + want Status + }{ + // Builds of the fixed release before the fix landed (+2; 27: +4). + {"17.0.21.1+0", Vulnerable}, + {"17.0.21.1+1", Vulnerable}, + {"17.0.21.1+2", Fixed}, + {"21.0.13.1+1-LTS", Vulnerable}, + {"21.0.13.1+2-LTS", Fixed}, + {"25.0.5.1+1", Vulnerable}, + {"25.0.5.1+2", Fixed}, + {"27.0.1.1+0", Vulnerable}, + {"27.0.1.1+3", Vulnerable}, + {"27.0.1.1+4", Fixed}, + {"17.0.21.1", Fixed}, // a release named without its build is the GA + {"17.0.21.2+1", Fixed}, + // Pre-release builds are not judged fixed. + {"21.0.13.1-beta+1-202611100000", Vulnerable}, + {"21.0.13.1-beta+2-202611150000", Unknown}, + {"17.0.21.1-internal+0-adhoc.root.jdk17u", Unknown}, // self-built: the build number says nothing (round 2) + {"17.0.21.1-internal+2-adhoc.root.jdk17u", Unknown}, + {"17.0.21.1+2-adhoc.builder.jdk", Unknown}, + // Older than the fix whatever the fourth number means. + {"21.0.2.13.1", Vulnerable}, + // A JDK 28 update release is not an early-access build. + {"28.0.1+2", Unknown}, + {"28+24", Vulnerable}, + } { + v, ok := Parse(c.v) + if !ok { + t.Fatalf("Parse(%q) failed", c.v) + } + if got, why := Judge(v); got != c.want { + t.Errorf("Judge(%s) = %s (%s), want %s", c.v, got, why, c.want) + } + } +} + +// Round-2 finding 3: self-built JDKs have no meaningful build number, and +// -LTS or an rpm release suffix is not a pre-release. +func TestReviewSelfBuilt(t *testing.T) { + for _, c := range []struct { + v string + want Status + }{ + {"17.0.6-internal+0-adhoc.root.jdk17u", Unknown}, + {"17.0.21.1-internal+0-adhoc.root.jdk17u", Unknown}, + {"21.0.2-internal+0-adhoc.root.jdk21u", Vulnerable}, // the source predates the fix + {"24.0.2-internal+0-adhoc.root.jdk24u", Vulnerable}, // the line has no fix + {"21.0.13.1-LTS", Fixed}, + {"21.0.13.1.2-1.el9", Fixed}, + {"21.0.13-LTS", Vulnerable}, + } { + v, ok := Parse(c.v) + if !ok { + t.Fatalf("Parse(%q)", c.v) + } + if got, why := Judge(v); got != c.want { + t.Errorf("Judge(%s) = %s (%s), want %s", c.v, got, why, c.want) + } + } +} + +func TestReasonWording(t *testing.T) { + v, _ := Parse("21.0.13.11") + if _, why := Judge(v); strings.Contains(why, "fix is in") { + t.Errorf("reason: %s", why) + } +} diff --git a/detectors/java4shell/internal/repo/images.go b/detectors/java4shell/internal/repo/images.go new file mode 100644 index 0000000..5f310db --- /dev/null +++ b/detectors/java4shell/internal/repo/images.go @@ -0,0 +1,222 @@ +package repo + +import ( + "fmt" + "regexp" + "strconv" + "strings" + + "github.com/DefectDojo/earlylight/detectors/java4shell/internal/knowledge" +) + +// Family is a published Java runtime image whose tags name the Java +// release. +type Family struct { + Repo string // repository, without docker.io/ or library/ + Vendor string + // OpenJ9 images run the OpenJ9 VM, which does not have the decoder. + OpenJ9 bool + // Prefix comes before the version in the tag ("open-" for Semeru). + Prefix string + // OpenJDKBuild is true when the build in the tag is OpenJDK's build + // number (Temurin's "_9"); other vendors' tag builds are their own. + OpenJDKBuild bool + // exact reports whether the text after the version pins one release + // (Temurin's "_9", Liberica's "-10", Zulu's "-21.52"), so the version + // has no further numbers. + exact *regexp.Regexp +} + +var ( + temurinBuild = regexp.MustCompile(`^_\d+`) + libericaBuild = regexp.MustCompile(`^-\d+(?:-|$)`) + zuluBuild = regexp.MustCompile(`^-\d+\.\d+(?:\.\d+)*`) +) + +// Families are the runtime images this detector reads tags of. +var Families = []Family{ + {Repo: "eclipse-temurin", Vendor: "Eclipse Temurin", exact: temurinBuild, OpenJDKBuild: true}, + {Repo: "amazoncorretto", Vendor: "Amazon Corretto"}, + {Repo: "openjdk", Vendor: "OpenJDK (the deprecated official image)"}, + {Repo: "sapmachine", Vendor: "SapMachine"}, + {Repo: "azul/zulu-openjdk", Vendor: "Azul Zulu", exact: zuluBuild}, + {Repo: "azul/zulu-openjdk-alpine", Vendor: "Azul Zulu", exact: zuluBuild}, + {Repo: "azul/zulu-openjdk-debian", Vendor: "Azul Zulu", exact: zuluBuild}, + {Repo: "azul/zulu-openjdk-centos", Vendor: "Azul Zulu", exact: zuluBuild}, + {Repo: "bellsoft/liberica-openjdk-debian", Vendor: "BellSoft Liberica", exact: libericaBuild}, + {Repo: "bellsoft/liberica-openjre-debian", Vendor: "BellSoft Liberica", exact: libericaBuild}, + {Repo: "bellsoft/liberica-openjdk-alpine", Vendor: "BellSoft Liberica", exact: libericaBuild}, + {Repo: "bellsoft/liberica-openjre-alpine", Vendor: "BellSoft Liberica", exact: libericaBuild}, + {Repo: "bellsoft/liberica-openjdk-alpine-musl", Vendor: "BellSoft Liberica", exact: libericaBuild}, + {Repo: "bellsoft/liberica-openjre-alpine-musl", Vendor: "BellSoft Liberica", exact: libericaBuild}, + {Repo: "bellsoft/liberica-runtime-container", Vendor: "BellSoft Liberica"}, + {Repo: "mcr.microsoft.com/openjdk/jdk", Vendor: "Microsoft Build of OpenJDK"}, + {Repo: "container-registry.oracle.com/java/openjdk", Vendor: "Oracle OpenJDK"}, + {Repo: "container-registry.oracle.com/java/jdk", Vendor: "Oracle JDK"}, + {Repo: "ibm-semeru-runtimes", Vendor: "IBM Semeru", OpenJ9: true, Prefix: "open-"}, + {Repo: "ibmjava", Vendor: "IBM Java", OpenJ9: true}, + {Repo: "icr.io/appcafe/ibm-semeru-runtimes", Vendor: "IBM Semeru", OpenJ9: true, Prefix: "open-"}, +} + +// repoName normalises an image name to a repository: docker.io and +// library/ removed, tag and digest removed. +func repoName(image string) (repo, tag string) { + ref := image + if i := strings.IndexByte(ref, '@'); i >= 0 { + ref = ref[:i] + } + slash := strings.LastIndexByte(ref, '/') + if i := strings.LastIndexByte(ref, ':'); i > slash { + ref, tag = ref[:i], ref[i+1:] + } + for _, p := range []string{"docker.io/", "index.docker.io/", "registry-1.docker.io/"} { + ref = strings.TrimPrefix(ref, p) + } + ref = strings.TrimPrefix(ref, "library/") + return ref, tag +} + +// FamilyOf returns the runtime image family of an image reference. +func FamilyOf(image string) (Family, string, bool) { + repo, tag := repoName(image) + for _, f := range Families { + if repo == f.Repo { + return f, tag, true + } + } + return Family{}, tag, false +} + +var leadingVersion = regexp.MustCompile(`^(\d{1,3}(?:\.\d{1,9}){0,5})`) + +// TagVersion is what a tag says about the Java release. +type TagVersion struct { + Nums []int + // Exact is true when the tag pins one release; otherwise it names every + // release that starts with Nums (a moving tag such as "21" or "21-jre"). + Exact bool + // Text is the version as written in the tag, Variant what follows it + // (and its build), for finding the same variant of another release. + Text, Build, Variant string + // OpenJDKBuild is the OpenJDK build number the tag names, -1 when it + // names none (a vendor's own build number, or no build). + OpenJDKBuild int +} + +// ParseTag reads the Java release a family's tag names. ok is false for a +// tag with no leading version ("latest", "jre", "alpine"). +func (f Family) ParseTag(tag string) (TagVersion, bool) { + t, ok := strings.CutPrefix(tag, f.Prefix) + if !ok { + return TagVersion{}, false + } + m := leadingVersion.FindString(t) + if m == "" { + return TagVersion{}, false + } + rest := t[len(m):] + if rest != "" && rest[0] >= '0' && rest[0] <= '9' { + return TagVersion{}, false + } + tv := TagVersion{Text: m, Variant: rest, OpenJDKBuild: -1} + for _, p := range strings.Split(m, ".") { + n, err := strconv.Atoi(p) + if err != nil { + return TagVersion{}, false + } + tv.Nums = append(tv.Nums, n) + } + if tv.Nums[0] < 8 || tv.Nums[0] > 99 { + return TagVersion{}, false + } + if f.Repo == "amazoncorretto" && len(tv.Nums) >= 4 && tv.Nums[3] > 3 { + // Corretto's own build number in the fourth place: the Java + // version is the first three, exactly. + tv.Nums, tv.Exact = tv.Nums[:3], true + } + if f.exact != nil { + if b := f.exact.FindString(rest); b != "" { + b = strings.TrimSuffix(b, "-") + tv.Exact, tv.Build, tv.Variant = true, b, rest[len(b):] + if f.OpenJDKBuild { + n := 0 + fmt.Sscanf(strings.TrimLeft(b, "_-"), "%d", &n) + tv.OpenJDKBuild = n + } + } + } + return tv, true +} + +// JudgeTag says what a tag's version proves. A moving tag that could name +// both unfixed and fixed releases is Unknown: the image has to be read. +func JudgeTag(tv TagVersion) (knowledge.Status, string) { + nums := tv.Nums + feature := nums[0] + what := "the tag names Java " + tv.Text + if !tv.Exact { + what = "the tag names the newest Java " + tv.Text + " release at build time" + } + if tv.Exact || feature < 17 { + st, why := knowledge.Judge(knowledge.Version{Nums: nums, Build: tv.OpenJDKBuild}) + return st, what + ": " + why + } + l, ok := knowledge.LineOf(feature) + if !ok { + st, why := knowledge.Judge(knowledge.Version{Nums: nums, Build: -1}) + if st == knowledge.Vulnerable { + return knowledge.Unknown, what + ", which the image has to show" + } + return st, what + ": " + why + } + if l.Introduced != nil { + k := min(len(nums), len(l.Introduced)) + c := knowledge.Compare(nums[:k], l.Introduced[:k]) + if c < 0 { + return knowledge.NotAffected, what + ", before " + join(l.Introduced) + ", the first update with the AVX-512 VBMI decoder" + } + if c == 0 && len(nums) < len(l.Introduced) { + return knowledge.Unknown, what + ": which release that is decides it, so the image has to be read" + } + } + if l.Fixed == nil { + return knowledge.Vulnerable, what + ": " + l.Note + } + k := min(len(nums), len(l.Fixed)) + switch c := knowledge.Compare(nums[:k], l.Fixed[:k]); { + case c < 0: + return knowledge.Vulnerable, what + ", older than " + join(l.Fixed) + ", the first " + strconv.Itoa(feature) + " release with the fix" + case c == 0 && len(nums) < len(l.Fixed): + return knowledge.Unknown, what + ": the newest release it names may or may not have the fix (" + join(l.Fixed) + "), so the image has to be read" + } + st, why := knowledge.Judge(knowledge.Version{Nums: nums, Build: -1}) + return st, what + ": " + why +} + +func join(ns []int) string { + parts := make([]string, len(ns)) + for i, n := range ns { + parts[i] = strconv.Itoa(n) + } + return strings.Join(parts, ".") +} + +// javaish are words in an image name that suggest a Java runtime inside: +// images with them are pulled and read; others are not. +var javaish = []string{"java", "jdk", "jre", "temurin", "corretto", "zulu", "liberica", "sapmachine", "semeru", "openj9", "graalvm", + "maven", "gradle", "tomcat", "jetty", "wildfly", "jboss", "keycloak", "jenkins", "atlassian", "jira", "confluence", "bitbucket", + "bamboo", "elasticsearch", "logstash", "opensearch", "kafka", "zookeeper", "solr", "cassandra", "neo4j", "sonarqube", "sonatype", + "nexus", "artifactory", "spring", "quarkus", "liberty", "payara", "glassfish", "flink", "spark", "hadoop", "hive", "trino", "presto", + "druid", "pinot", "nifi", "activemq", "teamcity", "youtrack", "gerrit", "pingfederate", "pingidentity", "spinnaker", "struts", "minecraft", "openliberty", "websphere", "weblogic", "distroless/java"} + +// Javaish reports an image whose name suggests a Java runtime. +func Javaish(image string) bool { + repo, tag := repoName(image) + s := strings.ToLower(repo + ":" + tag) + for _, w := range javaish { + if strings.Contains(s, w) { + return true + } + } + return false +} diff --git a/detectors/java4shell/internal/repo/plan.go b/detectors/java4shell/internal/repo/plan.go new file mode 100644 index 0000000..675704b --- /dev/null +++ b/detectors/java4shell/internal/repo/plan.go @@ -0,0 +1,369 @@ +package repo + +import ( + "context" + "errors" + "fmt" + "os" + "path/filepath" + "regexp" + "sort" + "strings" + "unicode" + + "github.com/DefectDojo/earlylight/detectors/java4shell/internal/knowledge" + "github.com/DefectDojo/earlylight/internal/refs" +) + +// Change is one line edit to a repository file. +type Change struct { + File string + Line int + Old string // the whole line, as it was + New string + Summary string +} + +// Declined is a plan that proposes no change, with the reason. +type Declined struct{ Reason string } + +func (d *Declined) Error() string { return d.Reason } + +func declined(format string, args ...any) error { + return &Declined{Reason: refs.CleanText(fmt.Sprintf(format, args...), 1500)} +} + +// Registry is what planning an image move needs from a registry: the tags +// of a repository, and a check that a target image holds only fixed +// runtimes on every platform the current one has (returning its digest). +type Registry struct { + Tags func(ctx context.Context, repo string) ([]string, error) + Verify func(ctx context.Context, current, target string) (string, error) +} + +// Plan finds the edit that fixes a finding, checks it by rescanning the +// edited file in memory, and returns it without writing anything. +func Plan(ctx context.Context, root string, f Finding, reg Registry, opts Options) (*Change, error) { + if !f.Fixable || f.edit == nil { + return nil, declined("%s", f.Fix) + } + switch f.edit.kind { + case "tag": + return planTag(ctx, root, f, reg, opts) + case "setup-java": + return planLine(ctx, root, f, f.edit.file, f.edit.line, f.edit.col, f.edit.old, fmt.Sprint(f.edit.feature), opts, + fmt.Sprintf("Sets setup-java's java-version from %s to %d on line %d of %s: the %s distribution's newest Java %d release has the Java4Shell fix (%s), and setup-java installs it when the job runs. On a self-hosted runner with Java %[6]d cached, add check-latest: true to the step so it fetches the newest release.", + f.edit.old, f.edit.feature, f.edit.line, f.edit.file, f.Props["distribution"], f.edit.feature, knowledge.FixedVersion(f.edit.feature))) + } + return nil, declined("no automatic fix for this finding") +} + +// target picks the first published tag of the same family, variant and +// style that has the fix. +func target(tags []string, e *pinEdit) (string, bool) { + type cand struct { + tag string + tv TagVersion + moving bool + } + fixed, _ := knowledge.LineOf(e.feature) + var cs []cand + for _, t := range tags { + tv, ok := e.family.ParseTag(t) + if !ok || tv.Variant != e.tv.Variant || tv.Exact != e.tv.Exact || tv.Nums[0] != e.feature || len(tv.Nums) < 3 { + continue + } + switch st, _ := JudgeTag(tv); { + case st == knowledge.Fixed: + cs = append(cs, cand{t, tv, false}) + case st == knowledge.Vulnerable: + // Below the fix, or a build of the fixed release before the + // fix landed (21.0.13.1_1): never a target. + case !tv.Exact && fixed.Fixed != nil && len(tv.Nums) == 3 && knowledge.Compare(tv.Nums, fixed.Fixed[:3]) == 0: + // A moving tag of the fixed update (Corretto republishes + // "21.0.13" with 21.0.13.1.2): only reading the image shows + // which release it holds, and Verify does. + cs = append(cs, cand{t, tv, true}) + } + } + if len(cs) == 0 { + return "", false + } + sort.Slice(cs, func(i, j int) bool { + if cs[i].moving != cs[j].moving { + return !cs[i].moving + } + if c := knowledge.Compare(cs[i].tv.Nums, cs[j].tv.Nums); c != 0 { + return c < 0 + } + return buildNum(cs[i].tv.Build) < buildNum(cs[j].tv.Build) + }) + return cs[0].tag, true +} + +// buildNum is the number in a tag's build part ("_10", "-9"), so _10 sorts +// after _2. +func buildNum(b string) int { + n := 0 + fmt.Sscanf(strings.TrimLeft(b, "_-"), "%d", &n) + return n +} + +func planTag(ctx context.Context, root string, f Finding, reg Registry, opts Options) (*Change, error) { + e, r := f.edit, f.ref + if r == nil { + return nil, declined("the image reference was not located") + } + if reg.Tags == nil || reg.Verify == nil { + return nil, declined("moving %s needs the registry, which this run does not use; change it by hand", e.image) + } + if strings.Contains(e.image, "@") { + return nil, declined("%s is pinned by digest; change the tag and digest by hand", e.image) + } + repo, tag := repoName(e.image) + start := refs.TagStart(e.image) + if start < 0 || e.image[start:] != tag { + return nil, declined("the tag of %s cannot be retagged automatically", e.image) + } + tags, err := reg.Tags(ctx, strings.TrimSuffix(e.image[:start], ":")) + if err != nil { + return nil, declined("the tags of %s could not be listed (%v); try again later, or change it by hand", repo, err) + } + newTag, ok := target(tags, e) + if !ok { + return nil, declined("%s has no published tag of Java %s or later with the variant %q yet (the registry was checked); move to it when %s publishes it", repo, knowledge.FixedVersion(e.feature), strings.TrimPrefix(e.tv.Variant, "-"), e.family.Vendor) + } + newImage := e.image[:start] + newTag + // The part of the reference that holds the whole tag, where it is + // written. + off := 0 + var part *refs.Part + for i := range r.Parts { + p := &r.Parts[i] + if off <= start && off+len(p.Text) >= len(e.image) { + part = p + break + } + off += len(p.Text) + } + if part == nil || part.Column == 0 || part.Line == 0 { + return nil, declined("the tag of %s is not written in one place this fix can edit; change it by hand", e.image) + } + file := part.File + if file == "" { + file = r.File + } + col := part.Column - 1 + (start - off) + if part.Var != "" { + // Editing the variable changes every place that uses it. + if n, err := varUses(root, r.File, part.Var, opts); err != nil || n > 1 { + return nil, declined("%s gets its tag from %s, which %s uses in more than one place; change it by hand", e.image, part.Var, r.File) + } + } + digest, err := reg.Verify(ctx, e.image, newImage) + if err != nil { + var d *Declined + if errors.As(err, &d) { + return nil, err + } + return nil, declined("%s could not be verified (%v); try again later, or change it by hand", newImage, err) + } + where := "" + if part.Var != "" { + where = fmt.Sprintf(" (in the value of %s)", part.Var) + } + moving := "" + if tv, ok := e.family.ParseTag(newTag); ok && !tv.Exact { + moving = fmt.Sprintf(" The tag %s is republished as %s releases new builds: pull it again when you rebuild and deploy (docker pull, imagePullPolicy: Always), since caches and nodes may still hold an older image under the same tag; to deploy exactly what was checked, pin it as %s@%s.", newTag, e.family.Vendor, newImage, digest) + } + return planLine(ctx, root, f, file, part.Line, col, tag, newTag, opts, + fmt.Sprintf("Changes the image %s to %s on line %d of %s%s: the %s release of the same variant with the Java4Shell fix, read on linux/amd64 and checked to be published for the platforms the current tag has (digest checked: %s). Rebuild and redeploy.%s", + e.image, newImage, part.Line, file, where, e.family.Vendor, digest, moving)) +} + +// planLine replaces old with repl at col on a line and rescans the file in +// memory: the finding must be gone and nothing new may appear. +func planLine(ctx context.Context, root string, f Finding, file string, line, col int, old, repl string, opts Options, summary string) (*Change, error) { + b, ok := opts.Overlay[file] + if !ok { + var err error + if b, err = readFile(root, file); err != nil { + return nil, declined("%s could not be read: %v", file, err) + } + } + ls := strings.Split(string(b), "\n") + if line < 1 || line > len(ls) { + return nil, declined("line %d of %s is gone; rescan", line, file) + } + cur := strings.TrimSuffix(ls[line-1], "\r") + if col < 0 || col+len(old) > len(cur) || cur[col:col+len(old)] != old { + return nil, declined("line %d of %s no longer reads as the scan found it; rescan", line, file) + } + newLine := cur[:col] + repl + cur[col+len(old):] + cr := "" + if strings.HasSuffix(ls[line-1], "\r") { + cr = "\r" + } + edited := append([]string(nil), ls...) + edited[line-1] = newLine + cr + over := map[string][]byte{} + for k, v := range opts.Overlay { + over[k] = v + } + over[file] = []byte(strings.Join(edited, "\n")) + before, err := Scan(ctx, root, Options{Overlay: opts.Overlay, Platforms: opts.Platforms}) + if err != nil { + return nil, err + } + after, err := Scan(ctx, root, Options{Overlay: over, Platforms: opts.Platforms}) + if err != nil { + return nil, err + } + key := func(x Finding) string { + return x.Rule + "|" + x.File + "|" + x.Props["image"] + "|" + x.Props["current"] + "|" + x.Status + } + had := map[string]int{} + for _, x := range before.Findings { + had[key(x)]++ + } + for _, x := range after.Findings { + if x.File == f.File && x.Line == f.Line && x.Rule == f.Rule { + return nil, declined("after the edit, line %d of %s still selects a release without the fix; change it by hand", f.Line, f.File) + } + if had[key(x)] == 0 && x.Level != "note" { + return nil, declined("the edit would add a new finding (%s at %s:%d); change it by hand", x.Message, x.File, x.Line) + } + had[key(x)]-- + } + if len(after.Skips) > len(before.Skips) { + return nil, declined("after the edit, %s no longer reads cleanly; change it by hand", file) + } + return &Change{File: file, Line: line, Old: cur, New: newLine, Summary: refs.CleanText(summary, 800)}, nil +} + +// Apply writes a change after checking the line still reads as planned. It +// keeps the file's mode and line ending and writes atomically. +func Apply(root string, ch *Change) error { + p, err := refs.SafePath(root, ch.File) + if err != nil { + return err + } + fi, err := os.Lstat(p) + if err != nil { + return err + } + b, err := readFile(root, ch.File) + if err != nil { + return err + } + ls := strings.Split(string(b), "\n") + if ch.Line < 1 || ch.Line > len(ls) || strings.TrimSuffix(ls[ch.Line-1], "\r") != ch.Old { + return fmt.Errorf("%s changed since the plan: line %d no longer reads as planned", ch.File, ch.Line) + } + cr := "" + if strings.HasSuffix(ls[ch.Line-1], "\r") { + cr = "\r" + } + ls[ch.Line-1] = ch.New + cr + tmp, err := os.CreateTemp(filepath.Dir(p), ".earlylight-*") + if err != nil { + return err + } + name := tmp.Name() + if _, err := tmp.WriteString(strings.Join(ls, "\n")); err != nil { + tmp.Close() + os.Remove(name) + return err + } + if err := tmp.Close(); err != nil { + os.Remove(name) + return err + } + if err := os.Chmod(name, fi.Mode().Perm()); err != nil { + os.Remove(name) + return err + } + return os.Rename(name, p) +} + +// Diff shows a change. +func Diff(ch *Change) string { + return fmt.Sprintf("@@ %s line %d @@\n- %s\n+ %s\n", Printable(ch.File), ch.Line, Printable(ch.Old), Printable(ch.New)) +} + +// Printable shows repository text safely on a terminal. +func Printable(s string) string { + var b strings.Builder + for _, r := range s { + if r != '\t' && (unicode.IsControl(r) || unicode.Is(unicode.Cf, r)) { + fmt.Fprintf(&b, "\\u%04x", r) + continue + } + b.WriteRune(r) + } + return b.String() +} + +// Mitigation is the optional interim edit for a Dockerfile whose image runs +// a vulnerable runtime that cannot be updated here (a product's bundled +// runtime): the line to add after the FROM line. fix prints it and does not +// apply it. +func Mitigation(file string, line int) string { + return fmt.Sprintf("%s: after line %d add\n ENV JAVA_TOOL_OPTIONS=\"-XX:-UseBASE64Intrinsics\"\n (append to any JAVA_TOOL_OPTIONS the image already sets; this keeps the vulnerable decoder from being generated until the product ships a fixed runtime)", file, line) +} + +// varUses counts the places a Dockerfile, Compose file or .env file uses +// a variable ($VAR, ${VAR}). +func varUses(root, file, name string, opts Options) (int, error) { + b, ok := opts.Overlay[file] + if !ok { + var err error + if b, err = readFile(root, file); err != nil { + return 0, err + } + } + re := regexp.MustCompile(`\$\{?` + regexp.QuoteMeta(name) + `\b`) + return len(re.FindAllIndex(b, -1)), nil +} + +// baseOSWords name a base OS in a tag's variant. +var baseOSWords = []string{"alpine", "jammy", "noble", "focal", "resolute", "plucky", "ubuntu", "debian", "bookworm", "trixie", "bullseye", + "ubi", "al2023", "al2", "centos", "rocky", "alma", "windowsservercore", "nanoserver", "distroless", "wolfi", "chainguard"} + +// NamesBaseOS reports whether a tag's variant names the base OS, so two +// tags with the same variant are on the same one. +func NamesBaseOS(variant string) bool { + v := strings.ToLower(variant) + for _, w := range baseOSWords { + if strings.Contains(v, w) { + return true + } + } + return false +} + +// SameBase compares the base OS of the current image and its +// replacement ("ID VERSION_ID" of os-release) on the distribution and its +// major version: moving a deployment to another distribution or major +// release (Ubuntu 24.04 to 26.04, Debian 12 to 13) is not this fix's call, +// while a minor update (Oracle Linux 9.5 to 9.6, Alpine 3.21 to 3.22) is +// what a rebuilt tag brings anyway. Ubuntu is compared on its full +// VERSION_ID (24.04 and 24.10 are different releases). +func SameBase(current, target string) error { + major := func(s string) string { + id, ver, _ := strings.Cut(s, " ") + if id != "ubuntu" { + // Ubuntu's YY.04 and YY.10 are different releases; elsewhere + // the first number is the release. + ver, _, _ = strings.Cut(ver, ".") + } + return id + " " + ver + } + switch { + case current == "" || target == "": + return declined("the base OS of the current or the new image could not be read; change it by hand") + case major(current) != major(target): + return declined("the new image is on %s, the current one on %s; change it by hand", refs.CleanText(target, 80), refs.CleanText(current, 80)) + } + return nil +} diff --git a/detectors/java4shell/internal/repo/refscan.go b/detectors/java4shell/internal/repo/refscan.go new file mode 100644 index 0000000..ca665ed --- /dev/null +++ b/detectors/java4shell/internal/repo/refscan.go @@ -0,0 +1,212 @@ +package repo + +import ( + "fmt" + "strings" + + "gopkg.in/yaml.v3" + + "github.com/DefectDojo/earlylight/detectors/java4shell/internal/jvm" + "github.com/DefectDojo/earlylight/detectors/java4shell/internal/knowledge" + "github.com/DefectDojo/earlylight/internal/refs" +) + +// images judges the image references the tree builds on and deploys. +func (s *scanner) images(opts Options) { + rs, skips, err := refs.Scan(s.root, refs.Options{Overlay: opts.Overlay}) + if err != nil { + s.skips = append(s.skips, Skip{File: ".", Reason: "image references: " + err.Error()}) + return + } + for _, sk := range skips { + if sk.Text != "" && !Javaish(sk.Text) { + continue // an unresolved reference that does not look like Java + } + s.skips = append(s.skips, Skip{File: sk.File, Line: sk.Line, Reason: sk.Reason}) + } + for i := range rs { + s.judgeRef(rs[i], opts) + } +} + +// amd64 reports whether the tree builds for x86-64 (or does not say). +func amd64(opts Options) bool { + if len(opts.Platforms) == 0 { + return true + } + for _, p := range opts.Platforms { + if strings.Contains(p, "amd64") || strings.Contains(p, "x86_64") || p == "all" { + return true + } + } + return false +} + +func (s *scanner) judgeRef(r refs.Ref, opts Options) { + img := r.Image + if img == "" || img == "scratch" || strings.Contains(img, "$") || !amd64(opts) { + return + } + // FROM --platform=linux/arm64: a stage that is not x86-64. + if p := r.Platform; p != "" && !strings.Contains(p, "$") && !strings.Contains(p, "amd64") && !strings.Contains(p, "x86_64") { + return + } + mit := s.mitigationFor(r) + props := map[string]string{"image": img} + if mit != "" { + props["mitigation"] = mit + } + fam, tag, ok := FamilyOf(img) + if !ok { + if Javaish(img) { + s.add(Finding{Rule: RuleNotChecked, File: r.File, Line: r.Line, Level: "note", Status: StatusUnknown, Pull: img, ref: &r, + Reason: "the image's Java runtime can only be judged by reading the image", + Message: fmt.Sprintf("image %s may hold a Java runtime; it was not read", img), Props: props}) + } + return + } + props["vendor"] = fam.Vendor + if fam.OpenJ9 { + return // OpenJ9: not affected + } + tv, ok := fam.ParseTag(tag) + if !ok { + s.add(Finding{Rule: RuleNotChecked, File: r.File, Line: r.Line, Level: "note", Status: StatusUnknown, Pull: img, ref: &r, + Reason: "the tag does not name a Java release, so only the image can show which one it holds", + Message: fmt.Sprintf("image %s: the tag names no Java release; it was not read", img), Props: props}) + return + } + props["current"] = tv.Text + st, why := JudgeTag(tv) + switch st { + case knowledge.Unknown: + s.add(Finding{Rule: RuleNotChecked, File: r.File, Line: r.Line, Level: "note", Status: StatusUnknown, Pull: img, ref: &r, + Reason: why, Message: fmt.Sprintf("image %s: %s", img, why), Props: props}) + return + case knowledge.Vulnerable: + default: + return + } + feature := tv.Nums[0] + f := Finding{Rule: RuleImagePin, File: r.File, Line: r.Line, Level: "error", Status: StatusVulnerable, Reason: why, ref: &r, Props: props, + Message: fmt.Sprintf("image %s holds %s Java %s, without the Java4Shell fix", img, fam.Vendor, tv.Text)} + if fx := knowledge.FixedVersion(feature); fx != "" { + props["fixed_in"] = fx + f.Fixable = true + f.edit = &pinEdit{kind: "tag", family: fam, tv: tv, image: img, feature: feature} + f.Fix = fmt.Sprintf("Move to the %s image of Java %s or later with the same variant (%q) once %s publishes it, rebuild and redeploy. fix does this after reading the new image.", fam.Vendor, fx, strings.TrimPrefix(tv.Variant, "-"), fam.Vendor) + } else { + f.Fix = fmt.Sprintf("Java %d received no fix: move to a supported release with the fix, such as %s (a major upgrade: test it).", feature, knowledge.Upgrade(feature)) + } + if mit != "" { + f.Status = StatusMitigated + f.Reason = why + "; " + mit + f.Message += ", mitigated by its JVM options until it is updated" + } + s.add(f) +} + +// mitigationFor returns the mitigation the file sets for the container or +// stage of a reference: a Dockerfile stage's ENV, a Compose service's +// environment, a Kubernetes container's env. +func (s *scanner) mitigationFor(r refs.Ref) string { + if r.Kind == refs.KindDockerfile { + return s.mitigated[fmt.Sprintf("%s:%d", r.File, r.Line)] + } + return EnvMitigation(s.root, r, s.opts.Overlay) +} + +// EnvMitigation reads the JVM option variables a Compose service or +// Kubernetes container sets next to an image reference, and returns the +// mitigation they make ("" for none). +func EnvMitigation(root string, r refs.Ref, overlay map[string][]byte) string { + if r.Kind != refs.KindCompose && r.Kind != refs.KindKubernetes { + return "" + } + b, ok := overlay[r.File] + if !ok { + var err error + if b, err = readFile(root, r.File); err != nil { + return "" + } + } + dec := yaml.NewDecoder(strings.NewReader(string(b))) + for i := 0; i < 1000; i++ { + var doc yaml.Node + if err := dec.Decode(&doc); err != nil { + return "" + } + m := findImageMapping(&doc, r.Line, 0) + if m == nil { + continue + } + env := map[string]string{} + if r.Kind == refs.KindCompose { + e := mapGet(m, "environment") + switch { + case e == nil: + case e.Kind == yaml.MappingNode: + for j := 0; j+1 < len(e.Content); j += 2 { + env[e.Content[j].Value] = e.Content[j+1].Value + } + case e.Kind == yaml.SequenceNode: + for _, c := range e.Content { + if k, v, ok := strings.Cut(c.Value, "="); ok { + env[k] = v + } + } + } + } else if e := mapGet(m, "env"); e != nil && e.Kind == yaml.SequenceNode { + for _, c := range e.Content { + if n, v := mapGet(c, "name"), mapGet(c, "value"); n != nil && v != nil { + env[n.Value] = v.Value + } + } + } + // The command the file sets can turn the decoder back on. + var cmd []string + for _, k := range []string{"entrypoint", "command", "args"} { + n := mapGet(m, k) + switch { + case n == nil: + case n.Kind == yaml.SequenceNode: + var arr []string + for _, c := range n.Content { + arr = append(arr, c.Value) + } + cmd = append(cmd, jvm.CommandWords(arr)...) + case n.Kind == yaml.ScalarNode: + cmd = append(cmd, jvm.Words(n.Value)...) + } + } + if len(cmd) > 0 && !strings.Contains(strings.Join(cmd, " "), "java") { + cmd = append([]string{"java"}, cmd...) // Kubernetes args go to the image's own java entrypoint + } + o := undo(jvm.FromEnv(withoutJavaOptions(env), nil, 17, nil), cmd, env) + if ok, how := o.Mitigates(); ok { + return strings.Replace(how, "(from ", "(from "+r.File+" ", 1) + } + return "" + } + return "" +} + +// findImageMapping finds the mapping whose "image" value sits on line. +func findImageMapping(n *yaml.Node, line, depth int) *yaml.Node { + if n == nil || depth > 64 { + return nil + } + if n.Kind == yaml.MappingNode { + for i := 0; i+1 < len(n.Content); i += 2 { + if n.Content[i].Value == "image" && n.Content[i+1].Line == line { + return n + } + } + } + for _, c := range n.Content { + if m := findImageMapping(c, line, depth+1); m != nil { + return m + } + } + return nil +} diff --git a/detectors/java4shell/internal/repo/repo.go b/detectors/java4shell/internal/repo/repo.go new file mode 100644 index 0000000..bffca75 --- /dev/null +++ b/detectors/java4shell/internal/repo/repo.go @@ -0,0 +1,1158 @@ +// Package repo finds what in a source tree selects a Java runtime exposed to +// Java4Shell (CVE-2026-114938), and plans the edits that fix it: +// +// - image-pin: a Dockerfile, Compose, Kubernetes, Helm values, CI file or +// Jib configuration names a Java runtime image (eclipse-temurin, +// amazoncorretto, azul/zulu-openjdk...) whose tag is a release without +// the fix. +// - java-pin: a version file (.java-version, .sdkmanrc, .tool-versions, +// mise), a GitHub Actions setup-java step or a buildpack setting pins a +// release without the fix. +// - java-line: a Gradle or Maven toolchain, setup-java or a buildpack +// selects a Java line that received no fix (18 to 20, 22 to 24, 26). +// - os-package: a Dockerfile installs a distribution's OpenJDK package +// (apt, apk, dnf, yum, microdnf) without the fix. +// - not-checked: what could not be read or decided without reading an +// image. +// +// It knows nothing about the engine. +package repo + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "io/fs" + "os" + "path" + "path/filepath" + "regexp" + "sort" + "strings" + + "gopkg.in/yaml.v3" + + "github.com/DefectDojo/earlylight/detectors/java4shell/internal/jvm" + "github.com/DefectDojo/earlylight/detectors/java4shell/internal/knowledge" + "github.com/DefectDojo/earlylight/internal/refs" +) + +// Rules. +const ( + RuleImagePin = "image-pin" + RuleJavaPin = "java-pin" + RuleLine = "java-line" + RulePackage = "os-package" + RuleNotChecked = "not-checked" +) + +// Status of a finding. +const ( + StatusVulnerable = "vulnerable" + StatusMitigated = "mitigated" + StatusUnknown = "unknown" +) + +// Finding is one thing in the tree. +type Finding struct { + Rule string `json:"rule"` + File string `json:"file"` + Line int `json:"line"` + Level string `json:"level"` // error, warning, note + Status string `json:"status"` + Reason string `json:"reason,omitempty"` + Message string `json:"message"` + Fix string `json:"fix,omitempty"` + Fixable bool `json:"fixable"` + Props map[string]string `json:"props,omitempty"` + // Pull is set for an image reference that only reading the image can + // decide (a moving tag, a product image). + Pull string `json:"pull,omitempty"` + ref *refs.Ref + edit *pinEdit +} + +// pinEdit locates a version or tag to replace on one line. +type pinEdit struct { + file string + line int + col int // 0-based byte column of old on the line + old string + kind string // tag, setup-java + // tag fields + family Family + tv TagVersion + image string + feature int +} + +// Skip is a file that was not read, and why. +type Skip struct { + File string `json:"file"` + Line int `json:"line,omitempty"` + Reason string `json:"reason"` +} + +// Result is a scan of a tree. +type Result struct { + Findings []Finding + Skips []Skip + Files int + Truncated bool +} + +// Options control a scan. +type Options struct { + // Overlay replaces file contents (repository-relative, slash paths), so + // a plan can rescan an edit in memory. + Overlay map[string][]byte + // Platforms are the platforms the tree builds; empty means every one. + Platforms []string +} + +const ( + maxFileSize = 4 << 20 + maxFiles = 200000 +) + +var skipDirs = map[string]bool{".git": true, "node_modules": true, "vendor": true, ".venv": true, "venv": true, "__pycache__": true, + ".tox": true, "bower_components": true, ".gradle": true, "target": true, "build": true, ".idea": true} + +// Scan walks root. +func Scan(ctx context.Context, root string, opts Options) (Result, error) { + var res Result + var list []string + err := filepath.WalkDir(root, func(p string, d fs.DirEntry, err error) error { + if err != nil { + rel, _ := filepath.Rel(root, p) + res.Skips = append(res.Skips, Skip{File: filepath.ToSlash(rel), Reason: err.Error()}) + return nil + } + if ctx.Err() != nil { + return ctx.Err() + } + if d.IsDir() { + if p != root && skipDirs[d.Name()] { + return filepath.SkipDir + } + return nil + } + if !d.Type().IsRegular() { + return nil + } + rel, err := filepath.Rel(root, p) + if err != nil { + return nil + } + rel = filepath.ToSlash(rel) + if kindOf(rel) == "" { + return nil + } + if len(list) >= maxFiles { + res.Truncated = true + return filepath.SkipAll + } + list = append(list, rel) + return nil + }) + if err != nil { + return res, err + } + return scanList(ctx, root, list, opts, res) +} + +func scanList(ctx context.Context, root string, list []string, opts Options, res Result) (Result, error) { + for rel := range opts.Overlay { + if kindOf(rel) != "" && !contains(list, rel) { + list = append(list, rel) + } + } + sort.Strings(list) + s := &scanner{root: root, opts: opts, mitigated: map[string]string{}} + for _, rel := range list { + if ctx.Err() != nil { + return res, ctx.Err() + } + b, ok := opts.Overlay[rel] + if !ok { + var err error + if b, err = readFile(root, rel); err != nil { + res.Skips = append(res.Skips, Skip{File: rel, Reason: err.Error()}) + continue + } + } + res.Files++ + text := string(b) + switch kindOf(rel) { + case "dockerfile": + s.dockerfile(rel, text) + case "java-version": + s.javaVersionFile(rel, text) + case "sdkmanrc": + s.sdkmanrc(rel, text) + case "tool-versions": + s.toolVersions(rel, text) + case "mise": + s.mise(rel, text) + case "workflow": + s.workflow(rel, b) + case "gradle": + s.gradle(rel, text) + case "maven": + s.maven(rel, text) + case "project.toml": + s.projectToml(rel, text) + } + } + s.images(opts) + res.Findings = append(res.Findings, s.findings...) + res.Skips = append(res.Skips, s.skips...) + sort.SliceStable(res.Findings, func(i, j int) bool { + a, b := res.Findings[i], res.Findings[j] + if a.File != b.File { + return a.File < b.File + } + return a.Line < b.Line + }) + return res, nil +} + +func contains(xs []string, s string) bool { + for _, x := range xs { + if x == s { + return true + } + } + return false +} + +// kindOf classifies a repository path. +func kindOf(rel string) string { + name := path.Base(rel) + lower := strings.ToLower(name) + switch { + case isDockerfile(lower): + return "dockerfile" + case lower == ".java-version": + return "java-version" + case lower == ".sdkmanrc": + return "sdkmanrc" + case lower == ".tool-versions": + return "tool-versions" + case lower == "mise.toml" || lower == ".mise.toml" || lower == "mise.local.toml" || lower == ".rtx.toml": + return "mise" + case strings.HasPrefix(rel, ".github/workflows/") && (strings.HasSuffix(lower, ".yml") || strings.HasSuffix(lower, ".yaml")): + return "workflow" + case lower == "build.gradle" || lower == "build.gradle.kts" || lower == "settings.gradle" || lower == "settings.gradle.kts" || lower == "gradle.properties": + return "gradle" + case lower == "pom.xml" || lower == "toolchains.xml": + return "maven" + case lower == "project.toml" || lower == "jib.yaml": + return "project.toml" + case strings.HasSuffix(lower, ".yml") || strings.HasSuffix(lower, ".yaml") || lower == ".env": + return "yaml" // read by internal/refs; listed so an overlay rescans it + } + return "" +} + +func isDockerfile(lower string) bool { + for _, n := range []string{"dockerfile", "containerfile"} { + if lower == n || strings.HasSuffix(lower, "."+n) { + return true + } + if strings.HasPrefix(lower, n) && strings.ContainsRune("._-", rune(lower[len(n)])) { + return true + } + } + return false +} + +func readFile(root, rel string) ([]byte, error) { + p, err := refs.SafePath(root, rel) + if err != nil { + return nil, err + } + fi, err := os.Lstat(p) + if err != nil { + return nil, err + } + if !fi.Mode().IsRegular() { + return nil, errors.New("not a regular file") + } + if fi.Size() > maxFileSize { + return nil, fmt.Errorf("larger than %d MiB", maxFileSize>>20) + } + return os.ReadFile(p) +} + +type scanner struct { + root string + opts Options + findings []Finding + skips []Skip + // mitigated maps "file:line" of a Dockerfile FROM to the mitigation its + // stage sets (ENV JAVA_TOOL_OPTIONS=-XX:-UseBASE64Intrinsics). + mitigated map[string]string +} + +func (s *scanner) add(f Finding) { + if f.Props == nil { + f.Props = map[string]string{} + } + s.findings = append(s.findings, f) +} + +// lines splits text into lines without terminators. +func lines(text string) []string { + ls := strings.Split(text, "\n") + for i, l := range ls { + ls[i] = strings.TrimSuffix(l, "\r") + } + return ls +} + +// ---- version files + +var versionInText = regexp.MustCompile(`(\d{1,3}(?:\.\d{1,9}){0,5})`) + +// judgeVersion turns a pinned version into a finding, or nothing when it is +// fixed or not affected. A version with fewer numbers than the fix is a +// moving pin (the newest release of that line) and is reported only when +// its line has no fix at all. +func (s *scanner) judgeVersion(rel string, line, col int, verText, source, vendor string) { + tv := TagVersion{Text: verText, Exact: strings.Count(verText, ".") >= 2, OpenJDKBuild: -1} + for _, p := range strings.Split(verText, ".") { + n := 0 + fmt.Sscanf(p, "%d", &n) + tv.Nums = append(tv.Nums, n) + } + if len(tv.Nums) == 0 || tv.Nums[0] < 8 || tv.Nums[0] > 99 { + return + } + if strings.Contains(strings.ToLower(vendor), "semeru") || strings.Contains(strings.ToLower(vendor), "openj9") { + return // OpenJ9: not affected + } + st, why := JudgeTag(tv) + props := map[string]string{"source": source, "current": verText} + if vendor != "" { + props["vendor"] = vendor + } + if fx := knowledge.FixedVersion(tv.Nums[0]); fx != "" { + props["fixed_in"] = fx + } + switch st { + case knowledge.Vulnerable: + rule, level := RuleJavaPin, "warning" + l, _ := knowledge.LineOf(tv.Nums[0]) + if l.Fixed == nil && tv.Nums[0] > 17 { + rule, level = RuleLine, "error" + } + msg := fmt.Sprintf("%s selects Java %s, without the Java4Shell fix", source, verText) + if rule == RuleLine { + msg = fmt.Sprintf("%s selects Java %s, a line that received no Java4Shell fix", source, verText) + } + s.add(Finding{Rule: rule, File: rel, Line: line, Level: level, Status: StatusVulnerable, Reason: why, + Message: msg, + Fix: pinFix(tv.Nums[0], source), Props: props}) + case knowledge.Unknown: + l, ok := knowledge.LineOf(tv.Nums[0]) + if ok && l.Fixed == nil { + s.add(Finding{Rule: RuleLine, File: rel, Line: line, Level: "error", Status: StatusVulnerable, Reason: l.Note, + Message: fmt.Sprintf("%s selects Java %s, a line that received no Java4Shell fix", source, verText), + Fix: pinFix(tv.Nums[0], source), Props: props}) + } + } + _ = col +} + +func pinFix(feature int, source string) string { + if fx := knowledge.FixedVersion(feature); fx != "" { + return fmt.Sprintf("Select Java %s or later of the same update in %s once your vendor publishes it (Amazon Corretto and SapMachine did on %s; Temurin, Debian, Ubuntu and Alpine had not %s), and reinstall.", fx, source, knowledge.VendorFixPublished, knowledge.AsOf()) + } + return fmt.Sprintf("Java %d received no fix: move %s to a supported release with the fix, such as %s.", feature, source, knowledge.Upgrade(feature)) +} + +func (s *scanner) javaVersionFile(rel, text string) { + for i, l := range lines(text) { + l = strings.TrimSpace(l) + if l == "" || strings.HasPrefix(l, "#") { + continue + } + // jenv names: temurin64-21.0.13, openjdk64-17.0.21, 21.0.13. + m := jenvName.FindStringSubmatch(l) + if m == nil { + return + } + ver := m[2] + if strings.HasPrefix(strings.ToLower(m[1]), "corretto") { + ver = CorrettoJava(ver) + } + s.judgeVersion(rel, i+1, 0, ver, ".java-version", vendorWord(l)) + return // jenv reads the first line + } +} + +var jenvName = regexp.MustCompile(`^(?:([A-Za-z][A-Za-z.]*?)(?:64)?-)?(\d{1,3}(?:\.\d{1,9}){0,5})(?:$|[^\d.])`) + +// ownNumbers notes a pin written in a vendor's own version numbers +// (Zulu 21.50.19, GraalVM 22.3.r17), which do not name the Java update. +func (s *scanner) ownNumbers(rel string, line int, value, source string) { + s.add(Finding{Rule: RuleNotChecked, File: rel, Line: line, Level: "note", Status: StatusUnknown, + Reason: "the vendor's own version number does not name the Java update", + Message: fmt.Sprintf("%s selects %s, written in the vendor's own version numbers: not checked for the Java4Shell fix", source, refs.CleanText(value, 80)), + Fix: "Check which Java update this release is built on: 17.0.21.1, 21.0.13.1, 25.0.5.1 or 27.0.1.1 and later of the same update have the fix.", + Props: map[string]string{"source": source, "current": refs.CleanText(value, 80)}}) +} + +var sdkmanLine = regexp.MustCompile(`^\s*java\s*=\s*(\d[\w.]*?)(?:-([a-z]+))?\s*$`) + +var sdkmanVendors = map[string]string{"tem": "Eclipse Temurin", "amzn": "Amazon Corretto", "zulu": "Azul Zulu", "librca": "BellSoft Liberica", + "sapmchn": "SapMachine", "ms": "Microsoft", "oracle": "Oracle", "open": "OpenJDK", "sem": "IBM Semeru (OpenJ9)", "graalce": "GraalVM CE", + "graal": "Oracle GraalVM", "kona": "Tencent Kona", "dragonwell": "Alibaba Dragonwell", "jbr": "JetBrains Runtime", "nik": "Liberica NIK", "mandrel": "Mandrel", "trava": "Trava", "zulufx": "Azul Zulu", "librcafx": "BellSoft Liberica"} + +func (s *scanner) sdkmanrc(rel, text string) { + for i, l := range lines(text) { + m := sdkmanLine.FindStringSubmatch(l) + if m == nil { + continue + } + vendor := sdkmanVendors[m[2]] + if vendor == "" { + vendor = m[2] + } + if strings.Contains(m[1], ".r") || m[2] == "grl" || (m[2] == "graalce" || m[2] == "graal" || m[2] == "nik" || m[2] == "mandrel") && strings.Count(m[1], ".") < 2 { + // GraalVM 22.3.r17 and the like: GraalVM's numbers. + s.ownNumbers(rel, i+1, strings.TrimSpace(strings.SplitN(l, "=", 2)[1]), ".sdkmanrc") + continue + } + s.judgeVersion(rel, i+1, strings.Index(l, m[1]), m[1], ".sdkmanrc", vendor) + } +} + +// asdfJava reads asdf-java and mise names: temurin-21.0.5+11.0.LTS, +// corretto-21.0.5.11.1, openjdk-21.0.2, sapmachine-21.0.5, zulu-21.38.21 +// (a Zulu version, not a Java version: not judged). +var asdfJava = regexp.MustCompile(`^(?:(adoptopenjdk|temurin|corretto|openjdk|sapmachine|liberica|microsoft|oracle|semeru-openj9|semeru|dragonwell|kona|zulu|graalvm|graalvm-community|oracle-graalvm|mandrel|jetbrains)-)?(?:jre-|jdk-|openj9-)?(\d[\d.]*)`) + +func (s *scanner) asdfValue(rel string, line int, value, source string) { + m := asdfJava.FindStringSubmatch(value) + if m == nil { + return + } + switch m[1] { + case "zulu", "graalvm", "mandrel", "jetbrains": + s.ownNumbers(rel, line, value, source) // their own version numbers + return + } + ver := m[2] + if m[1] == "corretto" { + ver = CorrettoJava(strings.TrimSuffix(ver, ".")) + } + s.judgeVersion(rel, line, 0, strings.TrimSuffix(ver, "."), source, m[1]) +} + +// CorrettoJava turns an Amazon Corretto version into the Java version it +// ships. Corretto has written its build number in the fourth place +// (21.0.5.11.1: Java 21.0.5, build 11) and, since the out-of-band releases, +// the OpenJDK patch number there (21.0.13.1.2: Java 21.0.13.1, build 2). +// OpenJDK patch numbers are small, so a fourth number above 3 is a build. +func CorrettoJava(ver string) string { + parts := strings.Split(ver, ".") + if len(parts) >= 4 { + n := 0 + fmt.Sscanf(parts[3], "%d", &n) + if n > 3 { + return strings.Join(parts[:3], ".") + } + return strings.Join(parts[:4], ".") + } + return ver +} + +func (s *scanner) toolVersions(rel, text string) { + for i, l := range lines(text) { + f := strings.Fields(l) + if len(f) >= 2 && f[0] == "java" { + s.asdfValue(rel, i+1, f[1], ".tool-versions") + } + } +} + +var miseJava = regexp.MustCompile(`^\s*java\s*=\s*["']([^"']+)["']`) + +func (s *scanner) mise(rel, text string) { + for i, l := range lines(text) { + if m := miseJava.FindStringSubmatch(l); m != nil { + s.asdfValue(rel, i+1, m[1], path.Base(rel)) + } + } +} + +func vendorWord(s string) string { + l := strings.ToLower(s) + for _, v := range []string{"temurin", "corretto", "zulu", "liberica", "sapmachine", "semeru", "openj9", "graalvm", "oracle", "microsoft"} { + if strings.Contains(l, v) { + return v + } + } + return "" +} + +// ---- GitHub Actions setup-java + +// publishedFix are the distributions (setup-java names) whose newest +// release of each fixed line had the fix when the data was checked, so a +// moving version ("21") installs a fixed release. +var publishedFix = map[string]bool{"corretto": true, "sapmachine": true} + +func (s *scanner) workflow(rel string, b []byte) { + var doc yaml.Node + if err := yaml.Unmarshal(b, &doc); err != nil { + s.skips = append(s.skips, Skip{File: rel, Reason: "not valid YAML: " + refs.CleanText(err.Error(), 200)}) + return + } + text := lines(string(b)) + walkYAML(&doc, 0, func(m *yaml.Node) { + uses := mapGet(m, "uses") + if uses == nil || !strings.HasPrefix(uses.Value, "actions/setup-java@") { + return + } + with := mapGet(m, "with") + if with == nil { + return + } + dist := "" + if d := mapGet(with, "distribution"); d != nil { + dist = d.Value + } + jv := mapGet(with, "java-version") + if jv == nil { + return + } + vals := []*yaml.Node{jv} + if jv.Kind == yaml.ScalarNode && strings.Contains(jv.Value, "\n") { + vals = nil // a block of versions: each line + for i, v := range strings.Split(strings.TrimSpace(jv.Value), "\n") { + vals = append(vals, &yaml.Node{Kind: yaml.ScalarNode, Value: strings.TrimSpace(v), Line: jv.Line + 1 + i}) + } + } + for _, v := range vals { + if v.Kind != yaml.ScalarNode { + continue + } + if strings.Contains(v.Value, "${{") { + s.add(Finding{Rule: RuleNotChecked, File: rel, Line: v.Line, Level: "note", Status: StatusUnknown, + Reason: "the version is a workflow expression decided when the job runs", + Message: fmt.Sprintf("setup-java's java-version %q is decided when the job runs; check that it selects a release with the Java4Shell fix", refs.CleanText(v.Value, 80))}) + continue + } + s.setupJava(rel, v, dist, text) + } + }) +} + +func (s *scanner) setupJava(rel string, v *yaml.Node, dist string, text []string) { + ver := strings.TrimSpace(v.Value) + if strings.HasSuffix(ver, "-ea") { + return // an early-access build: not a release this data judges + } + ver = strings.TrimSuffix(ver, ".x") + m := versionInText.FindString(ver) + if m == "" || m != ver && !strings.HasPrefix(ver, m+"+") { + return + } + if dist == "semeru" { + return + } + before := len(s.findings) + s.judgeVersion(rel, v.Line, 0, m, "actions/setup-java", dist) + if len(s.findings) == before { + s.movingSetupJava(rel, v, m, dist) + return + } + f := &s.findings[len(s.findings)-1] + f.Props["distribution"] = dist + feature := 0 + fmt.Sscanf(m, "%d", &feature) + // A pin of a fixed line moves to the moving major version when the + // distribution's newest release is fixed: setup-java then installs it. + if f.Rule == RuleJavaPin && publishedFix[dist] && v.Line >= 1 && v.Line <= len(text) { + line := text[v.Line-1] + col := strings.Index(line, ver) + if col >= 0 && strings.Count(line, ver) == 1 { + f.Fixable = true + f.edit = &pinEdit{file: rel, line: v.Line, col: col, old: ver, kind: "setup-java", feature: feature} + f.Fix = fmt.Sprintf("Set java-version to %d: %s's newest Java %d release has the fix (%s), and setup-java installs it.", feature, dist, feature, knowledge.FixedVersion(feature)) + } + } + if !f.Fixable && f.Rule == RuleJavaPin { + f.Fix = fmt.Sprintf("Set java-version to %s or later once the %s distribution publishes it (it had not when this detector's data was checked, %s), or use distribution corretto or sapmachine, which have.", knowledge.FixedVersion(feature), orAny(dist), knowledge.Checked) + } +} + +// movingSetupJava judges a moving setup-java version of a line with a fix +// ("21"): it installs the distribution's newest release of that line when +// the job runs. +func (s *scanner) movingSetupJava(rel string, v *yaml.Node, ver, dist string) { + if strings.Count(ver, ".") >= 2 { + return + } + feature := atoi(ver) + l, ok := knowledge.LineOf(feature) + if !ok || l.Fixed == nil || publishedFix[dist] { + return + } + fx := knowledge.FixedVersion(feature) + props := map[string]string{"source": "actions/setup-java", "current": ver, "distribution": dist, "fixed_in": fx} + if dist == "temurin" || dist == "adopt" || dist == "adopt-hotspot" { + s.add(Finding{Rule: RuleJavaPin, File: rel, Line: v.Line, Level: "warning", Status: StatusVulnerable, Props: props, + Reason: fmt.Sprintf("Eclipse Temurin had published no Java %s as of %s, so the job installs a release without the fix", fx, knowledge.Checked), + Message: fmt.Sprintf("actions/setup-java installs the newest %s Java %d, which had no Java4Shell fix yet", orAny(dist), feature), + Fix: fmt.Sprintf("Rerun once Temurin publishes Java %s, or use distribution corretto or sapmachine, whose newest Java %d has the fix.", fx, feature)}) + return + } + s.add(Finding{Rule: RuleNotChecked, File: rel, Line: v.Line, Level: "note", Status: StatusUnknown, Props: props, + Reason: fmt.Sprintf("whether %s had published Java %s could not be checked", orAny(dist), fx), + Message: fmt.Sprintf("actions/setup-java installs the newest %s Java %d: not checked for the Java4Shell fix", orAny(dist), feature), + Fix: fmt.Sprintf("Check that the installed release is Java %s or later.", fx)}) +} + +func orAny(d string) string { + if d == "" { + return "selected" + } + return d +} + +// ---- Gradle, Maven, buildpacks + +var ( + gradleToolchain = regexp.MustCompile(`JavaLanguageVersion\.of\(\s*["']?(\d{1,2})["']?\s*\)`) + gradleRelease = regexp.MustCompile(`(?:sourceCompatibility|targetCompatibility|options\.release|release\.set)\s*[=(]\s*(?:JavaVersion\.VERSION_|["'])?(\d{1,2})\b`) + bpJVM = regexp.MustCompile(`BP_JVM_VERSION["']?\s*(?:[:=]|to|>)\s*["']?(\d{1,2}(?:\.\d+)*)`) + jibFrom = regexp.MustCompile(`(?:from\s*\{[^}]*?image\s*=\s*|jib\.from\.image\s*=\s*|\s*\s*)["']?([A-Za-z0-9][\w./:@-]*)`) + mavenToolchain = regexp.MustCompile(`\s*\[?(\d{1,2})(?:,\))?\s*`) + enforcerJava = regexp.MustCompile(`(?s)\s*\s*([^<]{1,60})`) +) + +// lineToolchain reports a toolchain or release setting that selects a Java +// line with no fix. Lines with a fix are not reported: the toolchain picks +// whichever release of that line is installed. +func (s *scanner) lineToolchain(rel string, line int, feature int, source string) { + l, ok := knowledge.LineOf(feature) + if !ok || l.Fixed != nil { + return + } + s.add(Finding{Rule: RuleLine, File: rel, Line: line, Level: "error", Status: StatusVulnerable, Reason: l.Note, + Message: fmt.Sprintf("%s selects Java %d, a line that received no Java4Shell fix", source, feature), + Fix: fmt.Sprintf("Java %d received no fix: move to a supported release with the fix, such as %s.", feature, knowledge.Upgrade(feature)), + Props: map[string]string{"source": source, "current": fmt.Sprint(feature)}}) +} + +func (s *scanner) gradle(rel, text string) { + for i, l := range lines(text) { + for _, m := range gradleToolchain.FindAllStringSubmatch(l, -1) { + n := 0 + fmt.Sscanf(m[1], "%d", &n) + s.lineToolchain(rel, i+1, n, "the Gradle toolchain") + } + if m := bpJVM.FindStringSubmatch(l); m != nil { + s.buildpack(rel, i+1, m[1]) + } + s.jib(rel, i+1, l) + } +} + +func (s *scanner) maven(rel, text string) { + ls := lines(text) + inToolchain := false + for i, l := range ls { + if strings.Contains(l, "") || strings.Contains(l, "") || strings.Contains(l, "") { + inToolchain = true + } + if inToolchain { + if m := mavenToolchain.FindStringSubmatch(l); m != nil && path.Base(rel) == "toolchains.xml" || m != nil && strings.Contains(strings.Join(ls[max(0, i-3):i], "\n"), "") { + n := 0 + fmt.Sscanf(m[1], "%d", &n) + s.lineToolchain(rel, i+1, n, "the Maven toolchain") + } + } + if strings.Contains(l, "") || strings.Contains(l, "") { + inToolchain = false + } + if m := bpJVM.FindStringSubmatch(l); m != nil { + s.buildpack(rel, i+1, m[1]) + } + s.jib(rel, i+1, l) + } + for _, m := range enforcerJava.FindAllStringSubmatchIndex(text, -1) { + floor := strings.TrimSpace(text[m[2]:m[3]]) + line := strings.Count(text[:m[2]], "\n") + 1 + lo := strings.TrimLeft(floor, "[(") + lo, _, _ = strings.Cut(lo, ",") + v, ok := knowledge.Parse(lo) + if !ok || v.Feature() < 17 { + continue + } + fx := knowledge.FixedVersion(v.Feature()) + if fx == "" { + continue + } + fv, _ := knowledge.Parse(fx) + if knowledge.Compare(v.Nums, fv.Nums) < 0 && strings.Count(lo, ".") >= 2 { + s.add(Finding{Rule: RuleJavaPin, File: rel, Line: line, Level: "note", Status: StatusUnknown, + Reason: "the Maven Enforcer floor lets a build run on a release without the fix; it does not choose the release", + Message: fmt.Sprintf("the Maven Enforcer requireJavaVersion floor %s is below %s, the first release with the Java4Shell fix", floor, fx), + Fix: fmt.Sprintf("Raise the floor to %s so builds fail on a JDK without the fix.", fx), + Props: map[string]string{"source": "maven-enforcer", "current": floor, "fixed_in": fx}}) + } + } +} + +func (s *scanner) projectToml(rel, text string) { + for i, l := range lines(text) { + if m := bpJVM.FindStringSubmatch(l); m != nil { + s.buildpack(rel, i+1, m[1]) + } + if strings.HasSuffix(rel, "jib.yaml") { + if m := regexp.MustCompile(`^\s*image\s*:\s*["']?([A-Za-z0-9][\w./:@-]*)`).FindStringSubmatchIndex(l); m != nil { + s.jibRef(rel, i+1, m[2], l[m[2]:m[3]]) + } + } + } +} + +// buildpack judges BP_JVM_VERSION: Paketo installs the newest release of +// that line (BellSoft Liberica by default) when the image is built. +func (s *scanner) buildpack(rel string, line int, ver string) { + n := 0 + fmt.Sscanf(ver, "%d", &n) + if l, ok := knowledge.LineOf(n); ok && l.Fixed == nil { + s.lineToolchain(rel, line, n, "BP_JVM_VERSION (Cloud Native Buildpacks)") + return + } + if strings.Count(ver, ".") >= 2 { + s.judgeVersion(rel, line, 0, ver, "BP_JVM_VERSION (Cloud Native Buildpacks)", "") + } +} + +func (s *scanner) jib(rel string, line int, l string) { + m := jibFrom.FindStringSubmatchIndex(l) + if m == nil { + return + } + s.jibRef(rel, line, m[2], l[m[2]:m[3]]) +} + +// jibRef judges a Jib base image like an image reference. +func (s *scanner) jibRef(rel string, line, col int, image string) { + r := refs.Ref{File: rel, Line: line, Column: col + 1, Kind: "jib", Context: "jib.from.image", Text: image, Image: image, + Parts: []refs.Part{{Text: image, File: rel, Line: line, Column: col + 1}}, EditFile: rel, EditLine: line, EditText: image} + s.judgeRef(r, s.opts) +} + +// ---- Dockerfiles: OS packages and the mitigation a stage sets + +var ( + installRE = regexp.MustCompile(`\b(?:apt-get|apt|apk|dnf|yum|microdnf|tdnf|zypper)\b(?:\s+-{1,2}[\w=./:-]+)*\s+(?:install|add|in)\b`) + aptJava = regexp.MustCompile(`\b(openjdk-(\d{1,2})-(?:jre|jdk)(?:-headless)?)\b(=([^\s;&|]+))?`) + apkJava = regexp.MustCompile(`\b(openjdk(\d{1,2})(?:-jre|-jdk|-jre-headless|-jmods)?)(?:([=~><]+)([^\s;&|]+))?`) + rpmJava = regexp.MustCompile(`\b(java-(\d{1,2})-(openjdk|amazon-corretto)(?:-headless|-devel|-jdk)?)(?:-(\d[\w.:-]*))?`) + vendorJava = regexp.MustCompile(`\b((temurin|zulu|msopenjdk|sapmachine)-?(\d{1,2})(?:-ca)?(?:-(?:jdk|jre))?(?:-headless)?)\b(=([^\s;&|]+))?`) + defaultJava = regexp.MustCompile(`\b(default-(?:jre|jdk)(?:-headless)?)\b`) + envJTO = regexp.MustCompile(`(?i)^\s*ENV\s+`) + cmdLine = regexp.MustCompile(`(?i)^\s*(?:ENTRYPOINT|CMD)\s+`) + fromLine = regexp.MustCompile(`(?i)^\s*FROM\s`) +) + +func (s *scanner) dockerfile(rel, text string) { + ls := lines(text) + from := 0 + stageStart := len(s.findings) + // endStage marks the packages a stage installs as mitigated when the + // stage sets the mitigation (before or after the install: ENV applies + // to the image the stage makes). + stageEnv := map[string]string{} + var stageCmd []string // ENTRYPOINT and CMD words of the stage + endStage := func() { + o := undo(jvm.FromEnv(withoutJavaOptions(stageEnv), nil, 17, nil), stageCmd, stageEnv) + ok, how := o.Mitigates() + if !ok || from == 0 { + return + } + how = strings.Replace(how, "(from ", "(from ENV in "+rel+": ", 1) + s.mitigated[fmt.Sprintf("%s:%d", rel, from)] = how + for k := stageStart; k < len(s.findings); k++ { + f := &s.findings[k] + if f.File == rel && f.Rule == RulePackage && f.Status == StatusVulnerable { + f.Status = StatusMitigated + f.Props["mitigation"] = how + f.Reason += "; " + how + f.Message += ", mitigated by the stage's JVM options until it is updated" + } + } + } + defer endStage() + for i := 0; i < len(ls); i++ { + l := ls[i] + // Join continuation lines for matching; report the first line. + full := l + j := i + for strings.HasSuffix(strings.TrimRight(ls[j], " \t"), "\\") && j+1 < len(ls) { + j++ + full = strings.TrimSuffix(strings.TrimRight(full, " \t"), "\\") + " " + ls[j] + } + trim := strings.TrimSpace(full) + switch { + case fromLine.MatchString(trim): + endStage() + from, stageStart, stageEnv, stageCmd = i+1, len(s.findings), map[string]string{}, nil + case cmdLine.MatchString(trim): + stageCmd = append(stageCmd, commandWords(cmdLine.ReplaceAllString(trim, ""))...) + case envJTO.MatchString(trim): + // The last ENV of each variable in the stage is what the + // image keeps; the mitigation is decided when the stage ends. + body := envJTO.ReplaceAllString(trim, "") + for _, kv := range jvm.Words(body) { + if k, v, ok := strings.Cut(kv, "="); ok { + stageEnv[k] = v + } + } + case strings.HasPrefix(strings.ToUpper(trim), "RUN "): + s.packages(rel, i+1, trim) + } + i = j + } +} + +// packages finds the OpenJDK packages a RUN line installs: distribution +// packages and the vendors' own (Temurin, Corretto, Zulu, Microsoft, +// SapMachine), with or without a version. +func (s *scanner) packages(rel string, line int, run string) { + if !installRE.MatchString(run) { + return + } + var hits []pkgHit + for _, arg := range installArgs(run) { + switch { + case aptArg.MatchString(arg): + m := aptArg.FindStringSubmatch(arg) + hits = append(hits, pkgHit{name: m[1], feature: atoi(m[2]), op: opOf(m[3]), ver: m[4], mgr: "apt", vendor: "distribution"}) + case apkArg.MatchString(arg): + m := apkArg.FindStringSubmatch(arg) + hits = append(hits, pkgHit{name: m[1], feature: atoi(m[2]), op: m[3], ver: m[4], mgr: "apk", vendor: "distribution"}) + case rpmArg.MatchString(arg): + m := rpmArg.FindStringSubmatch(arg) + v := "distribution" + if m[3] == "amazon-corretto" { + v = "corretto" + } + hits = append(hits, pkgHit{name: m[1], feature: atoi(m[2]), ver: m[4], mgr: "rpm", vendor: v}) + case vendorArg.MatchString(arg): + m := vendorArg.FindStringSubmatch(arg) + hits = append(hits, pkgHit{name: m[1], feature: atoi(m[3]), op: opOf(m[4]), ver: m[5], mgr: "package", vendor: m[2]}) + case defaultArg.MatchString(arg): + hits = append(hits, pkgHit{name: arg, mgr: "apt", vendor: "distribution"}) + case varJavaArg.MatchString(arg): + // openjdk-${V}-jre-headless, java-${V}-openjdk: the Java + // release comes from a variable. + hits = append(hits, pkgHit{name: arg, mgr: "package", vendor: "distribution", varName: true}) + } + } + for _, h := range hits { + s.judgePackage(rel, line, h) + } +} + +// anchored matches the whole of one argument. +func anchored(re *regexp.Regexp) *regexp.Regexp { + return regexp.MustCompile(`^(?:` + strings.TrimPrefix(re.String(), `\b`) + `)$`) +} + +// varJavaArg is a Java package name with a variable where the release +// is: openjdk-${V}-jre-headless, openjdk$V, java-${V}-openjdk, temurin-${V}-jdk. +var varJavaArg = regexp.MustCompile(`^(?:openjdk-?|java-|temurin-|zulu|msopenjdk-|sapmachine-)\$\{?[A-Za-z_][A-Za-z0-9_]*\}?(?:-[\w.-]*)?$`) + +var ( + aptArg = anchored(aptJava) + apkArg = anchored(apkJava) + rpmArg = anchored(rpmJava) + vendorArg = anchored(vendorJava) + defaultArg = anchored(defaultJava) +) + +var ( + pkgTools = map[string]bool{"apt-get": true, "apt": true, "apk": true, "dnf": true, "yum": true, "microdnf": true, "tdnf": true, "zypper": true} + pkgVerbs = map[string]bool{"install": true, "add": true, "in": true, "reinstall": true} + // pkgValueFlags are package-manager options followed by a separate value. + pkgValueFlags = map[string]bool{"-t": true, "-o": true, "-X": true, "--repository": true, "--virtual": true, "-c": true, "--root": true, + "-p": true, "--target-release": true, "--option": true, "--repo": true, "--setopt": true, "--enablerepo": true, "--disablerepo": true} +) + +var ( + rpmArchRE = regexp.MustCompile(`\.(?:x86_64|aarch64|noarch|i686|i386|ppc64le|s390x)$`) + debArchRE = regexp.MustCompile(`:(?:amd64|arm64|i386|armhf|ppc64el|s390x|all|any|native)$`) +) + +// packageName strips what a package argument adds to the name: an rpm +// .arch, a Debian :arch or /target-release (openjdk-17-jdk/bookworm-backports), +// keeping a "=version" pin. It returns "" for something that is not a +// package argument (a path). +func packageName(w string) string { + name, ver, pinned := strings.Cut(w, "=") + // An apk repository tag: openjdk21-jre-headless@edge. + if i := strings.IndexByte(name, '@'); i > 0 { + name = name[:i] + } + if i := strings.IndexByte(name, '/'); i >= 0 { + if strings.Count(name, "/") > 1 || i == 0 { + return "" // a path + } + name = name[:i] + } + name = debArchRE.ReplaceAllString(name, "") + if pinned { + return name + "=" + ver + } + return rpmArchRE.ReplaceAllString(name, "") +} + +// installArgs returns the package arguments of the install commands on a +// RUN line: the words after " [flags] install|add", without flags, +// their values, paths and URLs. +func installArgs(run string) []string { + var out []string + for _, cmd := range jvm.SplitCommands(run) { + words := jvm.Words(cmd) + // The tool: not a flag's value (RUN --mount=...,target=/var/cache/apt). + i := 0 + for i < len(words) && (strings.HasPrefix(words[i], "-") || !pkgTools[path.Base(words[i])]) { + i++ + } + if i == len(words) { + continue + } + i++ + for i < len(words) && strings.HasPrefix(words[i], "-") { + if pkgValueFlags[words[i]] { + i++ + } + i++ + } + if i >= len(words) || !pkgVerbs[words[i]] { + continue + } + for j := i + 1; j < len(words); j++ { + w := words[j] + switch { + case pkgValueFlags[w]: + j++ + case strings.HasPrefix(w, "-"), strings.Contains(w, "://"), strings.HasPrefix(w, "/"), strings.HasPrefix(w, "$"): + default: + if a := packageName(w); a != "" { + out = append(out, a) + } + } + } + } + return out +} + +// pkgHit is one Java package an install names. +type pkgHit struct { + name, op, ver, mgr string + vendor string // distribution, corretto, temurin, zulu, msopenjdk, sapmachine + feature int // 0: the distribution's default (default-jre) + varName bool // the name holds a variable where the release is +} + +func opOf(v string) string { + if v != "" { + return "=" + } + return "" +} + +func atoi(s string) int { + n := 0 + fmt.Sscanf(s, "%d", &n) + return n +} + +var vendorNames = map[string]string{"distribution": "the distribution", "corretto": "Amazon Corretto", "temurin": "Eclipse Temurin", + "zulu": "Azul Zulu", "msopenjdk": "Microsoft", "sapmachine": "SapMachine"} + +// pinnedVersion is the Java version a package pin fixes, or "" for a pin +// that matches several releases (17.*, ~17, 17). +func pinnedVersion(h pkgHit) string { + if h.ver == "" || strings.Contains(h.ver, "$") || strings.Contains(h.ver, "*") || strings.Contains(h.op, "~") || strings.ContainsAny(h.op, "<>") { + return "" + } + up := h.ver + if i := strings.IndexByte(up, ':'); i >= 0 { + up = up[i+1:] + } + up = strings.ReplaceAll(up, "_p", "+") + if h.vendor == "corretto" { + up = CorrettoJava(up) + } + v, ok := knowledge.Parse(up) + if !ok || len(v.Nums) < 3 { + return "" + } + return up +} + +func (s *scanner) judgePackage(rel string, line int, h pkgHit) { + if h.varName { + s.add(Finding{Rule: RulePackage, File: rel, Line: line, Level: "note", Status: StatusUnknown, + Props: map[string]string{"package": h.name, "manager": h.mgr}, + Reason: "the Java release in the package name comes from a variable set outside this line", + Message: fmt.Sprintf("installs %s, a package whose Java release this scan cannot read: not checked for the Java4Shell fix", refs.CleanText(h.name, 80)), + Fix: "Check which Java release the variable selects: 17.0.21.1, 21.0.13.1, 25.0.5.1 or 27.0.1.1 and later of the same update have the fix."}) + return + } + if h.feature > 0 && h.feature < 17 { + return // Java 8 and 11 packages are not affected + } + props := map[string]string{"package": h.name, "manager": h.mgr} + who := vendorNames[h.vendor] + if h.feature > 0 { + l, ok := knowledge.LineOf(h.feature) + switch { + case !ok: + s.add(Finding{Rule: RuleNotChecked, File: rel, Line: line, Level: "note", Status: StatusUnknown, Props: props, + Reason: fmt.Sprintf("Java %d is not in this detector's release data (%s)", h.feature, knowledge.Checked), + Message: fmt.Sprintf("installs %s, a Java %d package this detector has no release data for", h.name, h.feature)}) + return + case l.Fixed == nil: + s.add(Finding{Rule: RuleLine, File: rel, Line: line, Level: "error", Status: StatusVulnerable, Reason: l.Note, + Message: fmt.Sprintf("installs %s, a Java %d package: that line received no Java4Shell fix", h.name, h.feature), + Fix: fmt.Sprintf("Install a supported Java package with the fix instead, such as %s.", knowledge.Upgrade(h.feature)), Props: props}) + return + } + props["fixed_in"] = knowledge.FixedVersion(h.feature) + } + fx := props["fixed_in"] + if fx == "" { + fx = "a release with the fix" + } + if up := pinnedVersion(h); up != "" { + props["current"] = h.ver + v, _ := knowledge.Parse(up) + if st, why := knowledge.Judge(v); st == knowledge.Vulnerable { + s.add(Finding{Rule: RulePackage, File: rel, Line: line, Level: "error", Status: StatusVulnerable, Reason: why, + Message: fmt.Sprintf("pins %s to %s, without the Java4Shell fix", h.name, h.ver), + Fix: fmt.Sprintf("Pin %s to a package of Java %s or later once %s publishes it, then rebuild. Until then set ENV JAVA_TOOL_OPTIONS=-XX:-UseBASE64Intrinsics in this stage.", h.name, fx, who), + Props: props}) + } + return + } + if h.ver != "" { + props["current"] = h.ver + } + if strings.Contains(h.ver, "$") { + s.add(Finding{Rule: RulePackage, File: rel, Line: line, Level: "note", Status: StatusUnknown, Props: props, + Reason: "the version comes from a variable set outside this line", + Message: fmt.Sprintf("installs %s pinned to %s, a version this scan cannot read: not checked for the Java4Shell fix", h.name, refs.CleanText(h.ver, 60)), + Fix: fmt.Sprintf("Check that the version the variable gives is Java %s or later.", fx)}) + return + } + what := "without a version" + if h.ver != "" { + what = "with a version range (" + h.op + h.ver + ")" + } + switch h.vendor { + case "corretto", "sapmachine": + s.add(Finding{Rule: RulePackage, File: rel, Line: line, Level: "note", Status: StatusUnknown, Props: props, + Reason: fmt.Sprintf("%s published Java %s on %s: an image built after that gets it, one built or cached before does not", who, fx, knowledge.VendorFixPublished), + Message: fmt.Sprintf("installs %s %s: whether the image has the Java4Shell fix depends on when it was built", h.name, what), + Fix: fmt.Sprintf("Rebuild without the layer cache, or pin %s to Java %s or later.", h.name, fx)}) + case "zulu", "msopenjdk": + s.add(Finding{Rule: RulePackage, File: rel, Line: line, Level: "note", Status: StatusUnknown, Props: props, + Reason: fmt.Sprintf("%s's fixed release could not be checked (%s)", who, knowledge.Checked), + Message: fmt.Sprintf("installs %s %s: not checked for the Java4Shell fix", h.name, what), + Fix: fmt.Sprintf("Check that %s's package is Java %s or later, then rebuild.", who, fx)}) + default: + reason := fmt.Sprintf("Debian, Ubuntu and Alpine had published no %s package with the fix as of %s (Red Hat's status was not checked), so a build today installs a release without it", fx, knowledge.Checked) + msg := fmt.Sprintf("installs %s %s: it gets whatever the distribution has, which had no Java4Shell fix yet", h.name, what) + if h.vendor == "temurin" { + reason = fmt.Sprintf("Eclipse Temurin had published no %s release as of %s, so a build today installs a release without it", fx, knowledge.Checked) + msg = fmt.Sprintf("installs %s %s: Temurin had no Java4Shell fix yet", h.name, what) + } + if h.feature == 0 { + msg = fmt.Sprintf("installs %s, the distribution's default OpenJDK (17 or later on current Debian and Ubuntu releases), which had no Java4Shell fix yet", h.name) + } + s.add(Finding{Rule: RulePackage, File: rel, Line: line, Level: "warning", Status: StatusVulnerable, Reason: reason, Message: msg, Props: props, + Fix: fmt.Sprintf("Rebuild once %s publishes Java %s or later and check the installed version; until then set ENV JAVA_TOOL_OPTIONS=-XX:-UseBASE64Intrinsics in this stage.", who, fx)}) + } +} + +// ---- YAML helpers + +func walkYAML(n *yaml.Node, depth int, fn func(m *yaml.Node)) { + if n == nil || depth > 64 { + return + } + if n.Kind == yaml.MappingNode { + fn(n) + } + for _, c := range n.Content { + walkYAML(c, depth+1, fn) + } +} + +func mapGet(m *yaml.Node, key string) *yaml.Node { + if m == nil || m.Kind != yaml.MappingNode { + return nil + } + for i := 0; i+1 < len(m.Content); i += 2 { + if m.Content[i].Value == key { + return m.Content[i+1] + } + } + return nil +} + +// commandWords reads a Dockerfile ENTRYPOINT or CMD: the JSON form or the +// shell form. +func commandWords(text string) []string { + text = strings.TrimSpace(text) + var arr []string + if strings.HasPrefix(text, "[") && json.Unmarshal([]byte(text), &arr) == nil { + return jvm.CommandWords(arr) + } + return jvm.Words(text) +} + +// withoutJavaOptions drops _JAVA_OPTIONS, which undo applies last. +func withoutJavaOptions(env map[string]string) map[string]string { + out := map[string]string{} + for k, v := range env { + if k != "_JAVA_OPTIONS" { + out[k] = v + } + } + return out +} + +// undo applies what a command can turn back on (its words count wherever +// the java it starts is) and then _JAVA_OPTIONS, which HotSpot applies +// last. A java @argfile makes the result unknowable: no mitigation. +func undo(o jvm.Options, cmd []string, env map[string]string) jvm.Options { + b64, avx, argfile := jvm.Undoes(cmd) + if argfile { + return jvm.Options{} + } + if b64 { + o.ApplyWord("-XX:+UseBASE64Intrinsics", "the command") + } + if avx { + o.ApplyWord("-XX:UseAVX=3", "the command") + } + if v, ok := env["_JAVA_OPTIONS"]; ok { + o.Apply(v, "_JAVA_OPTIONS") + } + return o +} diff --git a/detectors/java4shell/internal/repo/repo_test.go b/detectors/java4shell/internal/repo/repo_test.go new file mode 100644 index 0000000..5df009c --- /dev/null +++ b/detectors/java4shell/internal/repo/repo_test.go @@ -0,0 +1,269 @@ +package repo + +import ( + "context" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/DefectDojo/earlylight/detectors/java4shell/internal/knowledge" +) + +func tree(t *testing.T, files map[string]string) string { + t.Helper() + root := t.TempDir() + for rel, s := range files { + p := filepath.Join(root, rel) + if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(p, []byte(s), 0o644); err != nil { + t.Fatal(err) + } + } + return root +} + +func scan(t *testing.T, root string) Result { + t.Helper() + res, err := Scan(context.Background(), root, Options{}) + if err != nil { + t.Fatal(err) + } + return res +} + +func find(res Result, file string, line int) (Finding, bool) { + for _, f := range res.Findings { + if f.File == file && f.Line == line { + return f, true + } + } + return Finding{}, false +} + +func TestJudgeTag(t *testing.T) { + fam := func(repo string) Family { + f, _, ok := FamilyOf(repo + ":x") + if !ok { + t.Fatalf("no family %s", repo) + } + return f + } + for _, c := range []struct { + image string + want knowledge.Status + }{ + {"eclipse-temurin:21.0.13_9-jre-alpine", knowledge.Vulnerable}, + {"eclipse-temurin:21.0.13.1_2-jre", knowledge.Fixed}, + {"eclipse-temurin:21-jre", knowledge.Unknown}, + {"eclipse-temurin:21.0.12.1_1-jdk", knowledge.Vulnerable}, + {"eclipse-temurin:17.0.5_8-jre", knowledge.NotAffected}, + {"eclipse-temurin:17-jre", knowledge.Unknown}, + {"eclipse-temurin:11-jre", knowledge.NotAffected}, + {"eclipse-temurin:24-jre", knowledge.Vulnerable}, + {"amazoncorretto:21.0.12-al2023", knowledge.Vulnerable}, + {"amazoncorretto:21.0.13-al2023", knowledge.Unknown}, + {"amazoncorretto:21", knowledge.Unknown}, + {"azul/zulu-openjdk:21.0.12.1-21.52-jre", knowledge.Vulnerable}, + {"bellsoft/liberica-openjre-debian:21.0.13-9", knowledge.Vulnerable}, + {"bellsoft/liberica-openjre-debian:21.0.13", knowledge.Unknown}, + {"openjdk:17.0.2-jdk", knowledge.NotAffected}, + {"openjdk:18.0.2.1", knowledge.Vulnerable}, + {"sapmachine:21.0.13.1", knowledge.Fixed}, + {"docker.io/library/eclipse-temurin:25.0.5_8-jre", knowledge.Vulnerable}, + } { + repo, tag := repoName(c.image) + tv, ok := fam(repo).ParseTag(tag) + if !ok { + t.Errorf("%s: tag not read", c.image) + continue + } + if got, why := JudgeTag(tv); got != c.want || why == "" { + t.Errorf("%s: %s (%s), want %s", c.image, got, why, c.want) + } + } + if f, tag, _ := FamilyOf("eclipse-temurin:latest"); f.Repo != "eclipse-temurin" { + t.Error("family") + } else if _, ok := f.ParseTag(tag); ok { + t.Error("latest read as a version") + } +} + +func TestScanTree(t *testing.T) { + root := tree(t, map[string]string{ + "Dockerfile": "FROM eclipse-temurin:21.0.13_9-jre-alpine AS run\nRUN apk add --no-cache curl\n", + "svc/Dockerfile": "ARG JAVA=21.0.12.1_1-jdk\nFROM eclipse-temurin:${JAVA} AS build\nENV JAVA_TOOL_OPTIONS=\"-XX:-UseBASE64Intrinsics\"\n" + + "FROM ubuntu:24.04\nRUN apt-get update && apt-get install -y openjdk-21-jre-headless\nRUN apt-get install -y openjdk-17-jre-headless=17.0.21+9-1~deb12u1\nRUN apk add openjdk24\n" + + "FROM debian:12\nRUN apt-get install -y openjdk-25-jre-headless\nENV JAVA_TOOL_OPTIONS=-XX:-UseBASE64Intrinsics\n", + "compose.yaml": "services:\n app:\n image: amazoncorretto:21.0.12-al2023\n environment:\n JAVA_TOOL_OPTIONS: -XX:UseAVX=2\n" + + " jira:\n image: atlassian/jira-software:9.12\n web:\n image: nginx:1.27\n floating:\n image: eclipse-temurin:21-jre\n j9:\n image: ibm-semeru-runtimes:open-21-jre\n", + "k8s/deploy.yaml": "apiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: x\nspec:\n template:\n spec:\n containers:\n - name: a\n image: sapmachine:21.0.12\n", + ".java-version": "21.0.5\n", + ".sdkmanrc": "java=17.0.13-tem\n", + ".tool-versions": "nodejs 22\njava corretto-21.0.13.1.2\n", + "mise.toml": "[tools]\njava = \"temurin-23.0.2\"\n", + ".github/workflows/ci.yml": "jobs:\n b:\n steps:\n - uses: actions/setup-java@v4\n with:\n distribution: corretto\n java-version: '21.0.5'\n" + + " - uses: actions/setup-java@v4\n with:\n distribution: temurin\n java-version: ${{ matrix.java }}\n - uses: actions/setup-java@v4\n with:\n distribution: temurin\n java-version: '17'\n", + "build.gradle.kts": "java { toolchain { languageVersion.set(JavaLanguageVersion.of(22)) } }\njib { from { image = \"eclipse-temurin:25.0.5_8-jre\" } }\n", + "pom.xml": "maven-enforcer-plugin\n[17.0.13,)\n\n", + "project.toml": "[[build.env]]\nname = \"BP_JVM_VERSION\"\nvalue = \"21\"\n", + }) + res := scan(t, root) + check := func(file string, line int, rule, status string) Finding { + t.Helper() + f, ok := find(res, file, line) + if !ok { + t.Errorf("%s:%d: no finding (have %d)", file, line, len(res.Findings)) + for _, x := range res.Findings { + t.Logf(" %s:%d %s %s %s", x.File, x.Line, x.Rule, x.Status, x.Message) + } + return f + } + if f.Rule != rule || f.Status != status { + t.Errorf("%s:%d: %s %s (%s), want %s %s", file, line, f.Rule, f.Status, f.Message, rule, status) + } + return f + } + if f := check("Dockerfile", 1, RuleImagePin, StatusVulnerable); !f.Fixable || f.Props["fixed_in"] != "21.0.13.1" { + t.Errorf("Dockerfile pin: %+v", f) + } + check("svc/Dockerfile", 2, RuleImagePin, StatusMitigated) + check("svc/Dockerfile", 5, RulePackage, StatusVulnerable) + check("svc/Dockerfile", 9, RulePackage, StatusMitigated) + if f := check("svc/Dockerfile", 6, RulePackage, StatusVulnerable); f.Level != "error" { + t.Errorf("pinned package level %s", f.Level) + } + check("svc/Dockerfile", 7, RuleLine, StatusVulnerable) + check("compose.yaml", 3, RuleImagePin, StatusMitigated) + if f := check("compose.yaml", 7, RuleNotChecked, StatusUnknown); f.Pull != "atlassian/jira-software:9.12" { + t.Errorf("jira pull: %+v", f) + } + if f := check("compose.yaml", 11, RuleNotChecked, StatusUnknown); f.Pull == "" { + t.Errorf("floating pull: %+v", f) + } + if _, ok := find(res, "compose.yaml", 9); ok { + t.Error("nginx reported") + } + if _, ok := find(res, "compose.yaml", 13); ok { + t.Error("OpenJ9 image reported") + } + check("k8s/deploy.yaml", 10, RuleImagePin, StatusVulnerable) + check(".java-version", 1, RuleJavaPin, StatusVulnerable) + check(".sdkmanrc", 1, RuleJavaPin, StatusVulnerable) + if _, ok := find(res, ".tool-versions", 2); ok { + t.Error("a fixed Corretto pin reported") + } + check("mise.toml", 2, RuleLine, StatusVulnerable) + if f := check(".github/workflows/ci.yml", 7, RuleJavaPin, StatusVulnerable); !f.Fixable { + t.Errorf("setup-java corretto not fixable: %+v", f) + } + check(".github/workflows/ci.yml", 11, RuleNotChecked, StatusUnknown) + // Temurin's newest 17 had no fix: the moving version is reported. + check(".github/workflows/ci.yml", 15, RuleJavaPin, StatusVulnerable) + check("build.gradle.kts", 1, RuleLine, StatusVulnerable) + check("build.gradle.kts", 2, RuleImagePin, StatusVulnerable) + check("pom.xml", 2, RuleJavaPin, StatusUnknown) + if _, ok := find(res, "project.toml", 3); ok { + t.Error("BP_JVM_VERSION 21 reported") + } +} + +func TestPlatforms(t *testing.T) { + root := tree(t, map[string]string{"Dockerfile": "FROM eclipse-temurin:21.0.13_9-jre\n"}) + res, _ := Scan(context.Background(), root, Options{Platforms: []string{"linux/arm64"}}) + if len(res.Findings) != 0 { + t.Errorf("arm64-only build reported: %+v", res.Findings) + } +} + +func TestPlanTag(t *testing.T) { + root := tree(t, map[string]string{ + "Dockerfile": "ARG BASE=eclipse-temurin:21.0.13_9-jre-alpine\nFROM ${BASE}\nCMD [\"java\",\"-jar\",\"/a.jar\"]\n", + }) + res := scan(t, root) + f, ok := find(res, "Dockerfile", 2) + if !ok || !f.Fixable { + t.Fatalf("finding: %+v", res.Findings) + } + verified := "" + reg := Registry{ + Tags: func(_ context.Context, repo string) ([]string, error) { + if repo != "eclipse-temurin" { + t.Errorf("listed %s", repo) + } + return []string{"21.0.13_9-jre-alpine", "21.0.13.1_2-jre", "21.0.13.1_2-jre-alpine-3.22", "21.0.13.1_2-jre-alpine", "21.0.13.1_3-jre-alpine", "21.0.14_1-jre-alpine", "25.0.5.1_2-jre-alpine"}, nil + }, + Verify: func(_ context.Context, cur, target string) (string, error) { + verified = target + return "sha256:abc", nil + }, + } + ch, err := Plan(context.Background(), root, f, reg, Options{}) + if err != nil { + t.Fatal(err) + } + if verified != "eclipse-temurin:21.0.13.1_2-jre-alpine" || ch.Line != 1 || ch.New != "ARG BASE=eclipse-temurin:21.0.13.1_2-jre-alpine" { + t.Fatalf("plan: %+v (verified %s)", ch, verified) + } + if err := Apply(root, ch); err != nil { + t.Fatal(err) + } + if res := scan(t, root); len(res.Findings) != 0 { + t.Errorf("after the fix: %+v", res.Findings) + } + // No fixed tag published: declined with the reason. + root = tree(t, map[string]string{"Dockerfile": "FROM eclipse-temurin:17.0.21_9-jdk\n"}) + f, _ = find(scan(t, root), "Dockerfile", 1) + reg.Tags = func(context.Context, string) ([]string, error) { return []string{"17.0.21_9-jdk", "17-jdk"}, nil } + if _, err := Plan(context.Background(), root, f, reg, Options{}); err == nil || !strings.Contains(err.Error(), "no published tag") { + t.Errorf("no fixed tag: %v", err) + } + // Offline: declined. + if _, err := Plan(context.Background(), root, f, Registry{}, Options{}); err == nil || !strings.Contains(err.Error(), "registry") { + t.Errorf("offline: %v", err) + } +} + +func TestPlanSetupJava(t *testing.T) { + root := tree(t, map[string]string{".github/workflows/ci.yml": "jobs:\n b:\n steps:\n - uses: actions/setup-java@v4\n with:\n distribution: 'corretto'\n java-version: '21.0.5'\n"}) + res := scan(t, root) + f, ok := find(res, ".github/workflows/ci.yml", 7) + if !ok { + t.Fatalf("%+v", res.Findings) + } + ch, err := Plan(context.Background(), root, f, Registry{}, Options{}) + if err != nil { + t.Fatal(err) + } + if ch.New != " java-version: '21'" { + t.Errorf("plan: %+v", ch) + } +} + +func FuzzScanText(f *testing.F) { + f.Add("Dockerfile", "FROM eclipse-temurin:21.0.13_9-jre\nRUN apt-get install -y openjdk-21-jre=21.0.5+11-1\nENV JAVA_TOOL_OPTIONS=-XX:UseAVX=2\n") + f.Add(".github/workflows/a.yml", "jobs:\n a:\n steps:\n - uses: actions/setup-java@v4\n with:\n java-version: |\n 17\n 21.0.5\n") + f.Add("pom.xml", "[17.0.1,)eclipse-temurin:21.0.1_12-jre") + f.Add(".tool-versions", "java corretto-21.0.5.11.1\n") + f.Add("compose.yaml", "services:\n a:\n image: amazoncorretto:21.0.1\n environment: [\"JAVA_TOOL_OPTIONS=-XX:-UseBASE64Intrinsics\"]\n") + f.Fuzz(func(t *testing.T, name, text string) { + if strings.ContainsAny(name, "/\\\x00") || name == "" || name == "." || name == ".." || len(text) > 1<<16 { + return + } + root := t.TempDir() + if err := os.WriteFile(filepath.Join(root, name), []byte(text), 0o644); err != nil { + return + } + res, err := Scan(context.Background(), root, Options{}) + if err != nil { + t.Fatal(err) + } + for _, x := range res.Findings { + if x.Message == "" || x.Status == "" { + t.Fatalf("finding without message or status: %+v", x) + } + } + }) +} diff --git a/detectors/java4shell/internal/repo/review3_test.go b/detectors/java4shell/internal/repo/review3_test.go new file mode 100644 index 0000000..a15cb77 --- /dev/null +++ b/detectors/java4shell/internal/repo/review3_test.go @@ -0,0 +1,128 @@ +package repo + +import ( + "strings" + "testing" +) + +type want struct { + file string + line int + rule string + status string +} + +func checkAll(t *testing.T, res Result, ws []want) { + t.Helper() + for _, w := range ws { + f, ok := find(res, w.file, w.line) + if !ok || f.Rule != w.rule || f.Status != w.status { + t.Errorf("%s:%d: got %s %s (%s) found %v, want %s %s", w.file, w.line, f.Rule, f.Status, f.Message, ok, w.rule, w.status) + } + } +} + +// Review finding 8: installs written with flags first, partial versions and +// vendor packages. +func TestReviewPackages(t *testing.T) { + root := tree(t, map[string]string{ + "a/Dockerfile": "FROM rockylinux:9\nRUN yum -y install java-17-openjdk-headless\nRUN dnf -y install java-21-openjdk\nRUN microdnf -y install java-21-openjdk-headless\n", + "b/Dockerfile": "FROM debian:12\nRUN apt-get -y install openjdk-17-jre-headless\nRUN apt-get install -y openjdk-17-jdk=17.*\nRUN apt-get install -y default-jre-headless\nRUN apt-get install -y temurin-21-jdk\n", + "c/Dockerfile": "FROM alpine:3.23\nRUN apk --no-cache add openjdk21\nRUN apk -U add openjdk21-jre\nRUN apk add openjdk17~17\n", + }) + checkAll(t, scan(t, root), []want{ + {"a/Dockerfile", 2, RulePackage, StatusVulnerable}, {"a/Dockerfile", 3, RulePackage, StatusVulnerable}, {"a/Dockerfile", 4, RulePackage, StatusVulnerable}, + {"b/Dockerfile", 2, RulePackage, StatusVulnerable}, {"b/Dockerfile", 3, RulePackage, StatusVulnerable}, {"b/Dockerfile", 4, RulePackage, StatusVulnerable}, + {"b/Dockerfile", 5, RulePackage, StatusVulnerable}, + {"c/Dockerfile", 2, RulePackage, StatusVulnerable}, {"c/Dockerfile", 3, RulePackage, StatusVulnerable}, {"c/Dockerfile", 4, RulePackage, StatusVulnerable}, + }) +} + +// Review findings 11 and 12. +func TestReviewVersionNames(t *testing.T) { + root := tree(t, map[string]string{ + "a/.java-version": "temurin64-21.0.13\n", + "b/.java-version": "openjdk64-17.0.21\n", + "c/.sdkmanrc": "java=22.3.r17-nik\n", + "d/.sdkmanrc": "java=22.3.r17-grl\n", + "e/.tool-versions": "java zulu-21.50.19\n", + ".github/workflows/a.yml": "jobs:\n a:\n steps:\n - uses: actions/setup-java@v4\n with:\n distribution: temurin\n java-version: '21'\n" + + " - uses: actions/setup-java@v4\n with:\n distribution: corretto\n java-version: '21'\n", + "f/Dockerfile": "FROM amazonlinux:2023\nRUN dnf install -y java-21-amazon-corretto-headless\nRUN dnf install -y java-28-openjdk\n", + "g/Dockerfile": "FROM --platform=linux/arm64 eclipse-temurin:21.0.13_9-jre\n", + }) + res := scan(t, root) + checkAll(t, res, []want{ + {"a/.java-version", 1, RuleJavaPin, StatusVulnerable}, + {"b/.java-version", 1, RuleJavaPin, StatusVulnerable}, + {"c/.sdkmanrc", 1, RuleNotChecked, StatusUnknown}, + {"d/.sdkmanrc", 1, RuleNotChecked, StatusUnknown}, + {"e/.tool-versions", 1, RuleNotChecked, StatusUnknown}, + {".github/workflows/a.yml", 7, RuleJavaPin, StatusVulnerable}, + {"f/Dockerfile", 2, RulePackage, StatusUnknown}, + {"f/Dockerfile", 3, RuleNotChecked, StatusUnknown}, + }) + if f, ok := find(res, ".github/workflows/a.yml", 11); ok { + t.Errorf("setup-java corretto 21 reported: %+v", f) + } + if f, ok := find(res, "g/Dockerfile", 1); ok { + t.Errorf("an arm64 stage reported: %+v", f) + } +} + +// Round-2 finding 6: package names count only as install arguments. +func TestPackageArgsOnly(t *testing.T) { + root := tree(t, map[string]string{ + "a/Dockerfile": "FROM debian:12\nRUN apt-get install -y curl && curl -fsSL https://example.invalid/OpenJDK17U-jre_x64_linux_hotspot_17.0.5_8.tar.gz | tar xz && mv jdk-17.0.5+8-jre /opt/temurin-17\n", + "b/Dockerfile": "FROM debian:12\nRUN apt-get install -y curl && curl -fsSL https://cdn.azul.com/zulu/bin/zulu21.42.19-ca-jdk21.0.13-linux_x64.tar.gz -o /tmp/zulu21.tar.gz\n", + "c/Dockerfile": "FROM debian:12\nRUN apt-get update && apt-get install -y --no-install-recommends curl temurin-21-jdk && rm -rf /var/lib/apt/lists/*\n", + }) + res := scan(t, root) + for _, f := range res.Findings { + if f.File == "a/Dockerfile" || f.File == "b/Dockerfile" { + t.Errorf("a path or URL read as a package: %s:%d %s", f.File, f.Line, f.Message) + } + } + if f, ok := find(res, "c/Dockerfile", 2); !ok || f.Props["package"] != "temurin-21-jdk" { + t.Errorf("install argument missed: %+v", f) + } +} + +// Round-3 finding 1: install forms the argument parser dropped. +func TestInstallFormsRound3(t *testing.T) { + for i, run := range []string{ + "RUN --mount=type=cache,target=/var/cache/apt apt-get install -y openjdk-17-jre-headless", + "RUN --mount=type=cache,target=/var/cache/apk apk add openjdk21", + "RUN --mount=type=cache,target=/var/cache/dnf dnf install -y java-21-openjdk-headless", + "RUN apt-get install -y openjdk-17-jre-headless=${JDK_PKG}", + "RUN dnf install -y java-17-openjdk-headless.x86_64", + "RUN apt-get install -y openjdk-17-jre-headless:amd64", + "RUN apt-get install -y openjdk-17-jdk/bookworm-backports", + } { + root := tree(t, map[string]string{"Dockerfile": "FROM debian:12\n" + run + "\n"}) + f, ok := find(scan(t, root), "Dockerfile", 2) + if !ok || f.Rule != RulePackage { + t.Errorf("%d %q: %+v (found %v)", i, run, f, ok) + } + } +} + +// Round-4 follow-ups: an apk @repo tag, and a variable in place of the +// version in a package name. +func TestInstallFormsRound4(t *testing.T) { + root := tree(t, map[string]string{ + "a/Dockerfile": "FROM alpine:3.23\nRUN apk add --no-cache openjdk21-jre-headless@edge\n", + "b/Dockerfile": "FROM debian:12\nRUN apt-get install -y openjdk-${V}-jre-headless\n", + "c/Dockerfile": "FROM rockylinux:9\nRUN dnf install -y java-${V}-openjdk\n", + }) + res := scan(t, root) + if f, ok := find(res, "a/Dockerfile", 2); !ok || f.Rule != RulePackage || f.Props["package"] != "openjdk21-jre-headless" { + t.Errorf("apk @tag: %+v (found %v)", f, ok) + } + for _, file := range []string{"b/Dockerfile", "c/Dockerfile"} { + f, ok := find(res, file, 2) + if !ok || f.Rule != RulePackage || f.Status != StatusUnknown || !strings.Contains(f.Message, "cannot read") { + t.Errorf("%s: %+v (found %v)", file, f, ok) + } + } +} diff --git a/detectors/java4shell/internal/repo/review4_test.go b/detectors/java4shell/internal/repo/review4_test.go new file mode 100644 index 0000000..83f5285 --- /dev/null +++ b/detectors/java4shell/internal/repo/review4_test.go @@ -0,0 +1,95 @@ +package repo + +import ( + "context" + "strings" + "testing" +) + +func fakeRegistry(tags ...string) Registry { + return Registry{ + Tags: func(context.Context, string) ([]string, error) { return tags, nil }, + Verify: func(context.Context, string, string) (string, error) { return "sha256:0123abcd", nil }, + } +} + +// Review finding 10. +func TestReviewFixEdits(t *testing.T) { + // The verified digest and, for a republished tag, the re-pull warning + // are in the summary. + root := tree(t, map[string]string{"Dockerfile": "FROM amazoncorretto:21.0.12-al2023\n"}) + f, _ := find(scan(t, root), "Dockerfile", 1) + ch, err := Plan(context.Background(), root, f, fakeRegistry("21.0.13-al2023"), Options{}) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(ch.Summary, "sha256:0123abcd") || !strings.Contains(ch.Summary, "pull") { + t.Errorf("summary: %s", ch.Summary) + } + // An ARG the edit would change is used elsewhere too: declined. + root = tree(t, map[string]string{"Dockerfile": "ARG TAG=21.0.13_9-jre\nFROM eclipse-temurin:${TAG}\nRUN curl -o /a.jar https://example.invalid/agent-${TAG}.jar\n"}) + f, ok := find(scan(t, root), "Dockerfile", 2) + if !ok { + t.Fatal("no finding") + } + if _, err := Plan(context.Background(), root, f, fakeRegistry("21.0.13.1_2-jre"), Options{}); err == nil || !strings.Contains(err.Error(), "TAG") { + t.Errorf("ARG used elsewhere: %v", err) + } + // setup-java: the summary says check-latest. + root = tree(t, map[string]string{".github/workflows/a.yml": "jobs:\n a:\n steps:\n - uses: actions/setup-java@v4\n with:\n distribution: corretto\n java-version: '21.0.5'\n"}) + f, _ = find(scan(t, root), ".github/workflows/a.yml", 7) + if ch, err := Plan(context.Background(), root, f, Registry{}, Options{}); err != nil || !strings.Contains(ch.Summary, "check-latest") { + t.Errorf("setup-java summary: %v %v", ch, err) + } +} + +// Review finding 10: when a tag's variant does not name its base OS, the +// replacement must be on the same one. +func TestSameBase(t *testing.T) { + if !NamesBaseOS("-jre-alpine") || !NamesBaseOS("-al2023-headless") || NamesBaseOS("-jre") || NamesBaseOS("") { + t.Error("NamesBaseOS") + } + if err := SameBase("ubuntu 24.04", "ubuntu 26.04"); err == nil { + t.Error("different base accepted") + } + if err := SameBase("ubuntu 24.04", "ubuntu 24.04"); err != nil { + t.Error(err) + } + if err := SameBase("", "ubuntu 24.04"); err == nil { + t.Error("unknown base accepted") + } +} + +// Round-2 finding 7: the base OS is compared on ID and major version. +func TestSameBaseMajor(t *testing.T) { + for _, c := range []struct { + cur, tgt string + ok bool + }{ + {"ol 9.5", "ol 9.6", true}, + {"alpine 3.21.4", "alpine 3.22.1", true}, + {"ubuntu 24.04", "ubuntu 26.04", false}, + {"debian 12", "debian 13", false}, + {"ubuntu 24.04", "debian 12", false}, + } { + if err := SameBase(c.cur, c.tgt); (err == nil) != c.ok { + t.Errorf("SameBase(%q, %q) = %v", c.cur, c.tgt, err) + } + } + if err := SameBase(strings.Repeat("x", 5000)+" 1", "ubuntu 24.04"); err == nil || len(err.Error()) > 400 { + t.Errorf("unbounded reason: %d bytes", len(err.Error())) + } +} + +// Round-3 nit: Ubuntu's YY.04 and YY.10 are different releases. +func TestSameBaseUbuntu(t *testing.T) { + if err := SameBase("ubuntu 24.04", "ubuntu 24.10"); err == nil { + t.Error("Ubuntu 24.04 to 24.10 accepted") + } + if err := SameBase("ubuntu 24.04", "ubuntu 24.04"); err != nil { + t.Error(err) + } + if err := SameBase("rhel 9.4", "rhel 9.6"); err != nil { + t.Error(err) + } +} diff --git a/detectors/java4shell/internal/repo/review_test.go b/detectors/java4shell/internal/repo/review_test.go new file mode 100644 index 0000000..d907df3 --- /dev/null +++ b/detectors/java4shell/internal/repo/review_test.go @@ -0,0 +1,105 @@ +package repo + +import ( + "testing" +) + +// Regressions from the independent review. +func TestReviewRepo(t *testing.T) { + root := tree(t, map[string]string{ + "a/Dockerfile": "FROM eclipse-temurin:21.0.13_9-jre\nENV JAVA_TOOL_OPTIONS=-XX:-UseBASE64Intrinsics\nENV _JAVA_OPTIONS=-XX:+UseBASE64Intrinsics\n", + "b/Dockerfile": "FROM eclipse-temurin:21.0.13_9-jre\nENV JAVA_TOOL_OPTIONS=-XX:-UseBASE64Intrinsics\nENV JAVA_TOOL_OPTIONS=-Xmx1g\n", + "c/Dockerfile": "FROM amazonlinux:2023\nRUN dnf install -y java-21-amazon-corretto-headless-1:21.0.13.11-1\n", + "d/Dockerfile": "FROM amazoncorretto:21.0.13.11\n", + "e/.tool-versions": "java corretto-21.0.13.11\n", + "f/.java-version": "corretto-21.0.13.11.1\n", + "g/.tool-versions": "java corretto-21.0.13.1.2\n", + }) + res := scan(t, root) + for _, c := range []struct { + file string + line int + rule string + status string + }{ + {"a/Dockerfile", 1, RuleImagePin, StatusVulnerable}, + {"b/Dockerfile", 1, RuleImagePin, StatusVulnerable}, + {"c/Dockerfile", 2, RulePackage, StatusVulnerable}, + {"d/Dockerfile", 1, RuleImagePin, StatusVulnerable}, + {"e/.tool-versions", 1, RuleJavaPin, StatusVulnerable}, + {"f/.java-version", 1, RuleJavaPin, StatusVulnerable}, + } { + f, ok := find(res, c.file, c.line) + if !ok || f.Rule != c.rule || f.Status != c.status { + t.Errorf("%s:%d: %+v (found %v), want %s %s", c.file, c.line, f, ok, c.rule, c.status) + } + } + if f, ok := find(res, "g/.tool-versions", 1); ok { + t.Errorf("fixed Corretto reported: %+v", f) + } + if CorrettoJava("21.0.5.11.1") != "21.0.5" || CorrettoJava("21.0.13.1.2") != "21.0.13.1" || CorrettoJava("21.0.13.0.11") != "21.0.13.0" { + t.Error("CorrettoJava") + } + + // setup-java early access is not judged or rewritten. + root = tree(t, map[string]string{".github/workflows/a.yml": "jobs:\n a:\n steps:\n - uses: actions/setup-java@v4\n with:\n distribution: corretto\n java-version: '21.0.13-ea'\n"}) + if res := scan(t, root); len(res.Findings) != 0 { + t.Errorf("early access: %+v", res.Findings) + } + + // The first fixed build sorts numerically. + e := &pinEdit{family: Families[0], feature: 21} + e.tv, _ = Families[0].ParseTag("21.0.13_9-jre") + if got, _ := target([]string{"21.0.13.1_10-jre", "21.0.13.1_2-jre"}, e); got != "21.0.13.1_2-jre" { + t.Errorf("target %s", got) + } + + // Corretto: the moving tag of the fixed update is a candidate (the + // image is read before any edit), after tags that name the fix. + c := &pinEdit{feature: 21} + c.family, _, _ = FamilyOf("amazoncorretto:x") + c.tv, _ = c.family.ParseTag("21.0.12-al2023") + if got, _ := target([]string{"21.0.12-al2023", "21.0.13-al2023", "21.0.13-alpine", "21-al2023", "21.0.14-al2023"}, c); got != "21.0.13-al2023" { + t.Errorf("corretto target %q", got) + } + if got, _ := target([]string{"21.0.13-al2023", "21.0.13.1-al2023"}, c); got != "21.0.13.1-al2023" { + t.Errorf("corretto target prefers the named fix: %q", got) + } +} + +// Review finding 2: fix never picks a build below the fixed build. +func TestTargetBelowFixedBuild(t *testing.T) { + e := &pinEdit{family: Families[0], feature: 21} + e.tv, _ = Families[0].ParseTag("21.0.13_9-jre") + if got, ok := target([]string{"21.0.13.1_1-jre"}, e); ok { + t.Errorf("picked %s, a build without the fix", got) + } + if got, _ := target([]string{"21.0.13.1_1-jre", "21.0.13.1_2-jre"}, e); got != "21.0.13.1_2-jre" { + t.Errorf("picked %s", got) + } + tv, _ := Families[0].ParseTag("21.0.13.1_1-jre") + if st, why := JudgeTag(tv); st != "vulnerable" { + t.Errorf("21.0.13.1_1: %s (%s)", st, why) + } +} + +// Review finding 9: a command in the tree that turns the intrinsic back on +// voids the mitigation the stage or service sets. +func TestTreeCommandReenables(t *testing.T) { + root := tree(t, map[string]string{ + "a/Dockerfile": "FROM eclipse-temurin:21.0.13_9-jre\nENV JAVA_TOOL_OPTIONS=-XX:-UseBASE64Intrinsics\nENTRYPOINT [\"java\",\"-XX:+UseBASE64Intrinsics\",\"-jar\",\"/a.jar\"]\n", + "b/Dockerfile": "FROM eclipse-temurin:21.0.13_9-jre\nENV JAVA_TOOL_OPTIONS=-XX:UseAVX=2\nCMD java -XX:UseAVX=3 -jar /a.jar\n", + "compose.yaml": "services:\n a:\n image: amazoncorretto:21.0.12-al2023\n environment:\n JAVA_TOOL_OPTIONS: -XX:-UseBASE64Intrinsics\n command: [\"java\", \"-XX:+UseBASE64Intrinsics\", \"-jar\", \"/a.jar\"]\n", + "k8s.yaml": "apiVersion: v1\nkind: Pod\nmetadata:\n name: a\nspec:\n containers:\n - name: a\n image: amazoncorretto:21.0.12-al2023\n env:\n - name: JAVA_TOOL_OPTIONS\n value: -XX:-UseBASE64Intrinsics\n args: [\"-XX:+UseBASE64Intrinsics\"]\n", + }) + res := scan(t, root) + for _, c := range []struct { + file string + line int + }{{"a/Dockerfile", 1}, {"b/Dockerfile", 1}, {"compose.yaml", 3}, {"k8s.yaml", 8}} { + f, ok := find(res, c.file, c.line) + if !ok || f.Status != StatusVulnerable || f.Props["mitigation"] != "" { + t.Errorf("%s:%d: %+v (found %v)", c.file, c.line, f, ok) + } + } +} diff --git a/detectors/java4shell/internal/testlib/testlib.go b/detectors/java4shell/internal/testlib/testlib.go new file mode 100644 index 0000000..34c9473 --- /dev/null +++ b/detectors/java4shell/internal/testlib/testlib.go @@ -0,0 +1,82 @@ +// Package testlib builds small stand-ins for Java VM libraries for tests: an +// executable header for the architecture, the HotSpot version line and, +// optionally, the AVX-512 VBMI decode stubs' names. +package testlib + +import ( + "encoding/binary" + "fmt" +) + +// Lib describes a fake VM library. +type Lib struct { + Format string // elf (default), pe, macho, fat + Machine string // x86-64 (default), aarch64, x86 + Version string // "" for no version line + OS string // linux (default) + VBMI bool + VMName string // OpenJDK 64-Bit Server VM (default) + Extra string // more bytes + Musl bool // the platform is --musl, as on Alpine +} + +// Build returns the file's bytes. +func Build(l Lib) []byte { + if l.Machine == "" { + l.Machine = "x86-64" + } + if l.OS == "" { + l.OS = "linux" + } + if l.VMName == "" { + l.VMName = "OpenJDK 64-Bit Server VM" + } + var b []byte + switch l.Format { + case "pe": + b = make([]byte, 0x100) + b[0], b[1] = 'M', 'Z' + binary.LittleEndian.PutUint32(b[60:], 0x80) + copy(b[0x80:], "PE\x00\x00") + m := map[string]uint16{"x86-64": 0x8664, "aarch64": 0xaa64, "x86": 0x14c}[l.Machine] + binary.LittleEndian.PutUint16(b[0x84:], m) + case "macho": + b = make([]byte, 32) + binary.LittleEndian.PutUint32(b, 0xfeedfacf) + binary.LittleEndian.PutUint32(b[4:], map[string]uint32{"x86-64": 0x01000007, "aarch64": 0x0100000c}[l.Machine]) + case "fat": + b = make([]byte, 8+2*20) + binary.BigEndian.PutUint32(b, 0xcafebabe) + binary.BigEndian.PutUint32(b[4:], 2) + binary.BigEndian.PutUint32(b[8:], 0x01000007) + binary.BigEndian.PutUint32(b[28:], 0x0100000c) + default: + b = make([]byte, 64) + copy(b, "\x7fELF\x02\x01\x01") + m := map[string]uint16{"x86-64": 0x3e, "aarch64": 0xb7, "x86": 3}[l.Machine] + binary.LittleEndian.PutUint16(b[18:], m) + } + cpu := map[string]string{"x86-64": "amd64", "aarch64": "aarch64", "x86": "x86"}[l.Machine] + if l.Musl { + cpu += "-musl" + } + if l.Version != "" { + b = append(b, 0) + b = append(b, fmt.Sprintf("%s (%s) for %s-%s JRE (%s), built on 2026-10-20T00:00:00Z by \"admin\" with gcc 11.3.0", l.VMName, l.Version, l.OS, cpu, l.Version)...) + b = append(b, 0) + } + if l.VBMI { + b = append(b, "lookup_lo_base64\x00lookup_hi_base64\x00pack_vec_base64\x00join_0_1_base64\x00"...) + } + b = append(b, l.Extra...) + return b +} + +// Release returns a release file. +func Release(kv ...string) []byte { + var out []byte + for i := 0; i+1 < len(kv); i += 2 { + out = append(out, fmt.Sprintf("%s=%q\n", kv[i], kv[i+1])...) + } + return out +} diff --git a/detectors/java4shell/java4shell.go b/detectors/java4shell/java4shell.go new file mode 100644 index 0000000..3caecca --- /dev/null +++ b/detectors/java4shell/java4shell.go @@ -0,0 +1,560 @@ +// Package java4shell is the detector for Java4Shell (CVE-2026-114938): an +// out-of-bounds write in HotSpot's AVX-512 VBMI Base64 decoder on x86-64 +// (JDK-8393212), fixed in OpenJDK 17.0.21.1, 21.0.13.1, 25.0.5.1 and +// 27.0.1.1 on 2026-11-15. The decoder came with JDK 18 and 17.0.6; Java 8, +// 11 and 17.0.0 to 17.0.5 do not have it. +// +// It finds Java runtimes in filesystems and container images by their VM +// library (libjvm.so, jvm.dll, libjvm.dylib) and judges each by what the +// library proves: its CPU architecture, the version compiled into it and +// whether it carries the AVX-512 VBMI decode stubs, with the release file as +// a second witness. JVM options that keep the decoder from being generated +// (-XX:-UseBASE64Intrinsics, -XX:UseAVX=2 or lower) are reported as a +// mitigation on a finding that stays vulnerable. In a source tree it finds the Java images, +// versions and packages a project pins, and fix moves them to fixed +// releases. +// +// internal/knowledge, internal/jvm, internal/assess and internal/repo know +// nothing about the engine; this file adapts them. +package java4shell + +import ( + "bytes" + "fmt" + "path" + "regexp" + "sort" + "strings" + + "github.com/DefectDojo/earlylight/detectors/java4shell/internal/assess" + "github.com/DefectDojo/earlylight/detectors/java4shell/internal/jvm" + "github.com/DefectDojo/earlylight/detectors/java4shell/internal/knowledge" + "github.com/DefectDojo/earlylight/internal/archive" + "github.com/DefectDojo/earlylight/internal/engine" +) + +// ID is the detector's ID. +const ID = "java4shell" + +// Kind IDs. +const ( + KindRuntime = assess.KindRuntime + KindBundled = assess.KindBundled +) + +// Detector implements engine.Detector, engine.Reader, engine.RepoScanner and +// engine.Fixer. +type Detector struct{} + +func init() { engine.Register(&Detector{}) } + +const about = " HotSpot's AVX-512 VBMI Base64 decoder (x86-64, JDK 18 and later, 17.0.6 and later, and Oracle's Java 8" + + " Performance Pack) writes up to 64 bytes past its output when the input ends in a padded fragment shorter than four" + + " characters (64 'A' then '=' is enough), so a Java program that decodes untrusted Base64 (tokens, headers," + + " certificates, JSON) can have its heap corrupted once the decoder is compiled: pre-authentication remote code" + + " execution was shown on default Keycloak, Jira, Confluence, Artifactory, Jenkins and PingFederate. It runs on CPUs" + + " with AVX-512F, AVX-512BW, AVX-512 VBMI and BMI2 (Intel server CPUs from Ice Lake on and some client CPUs, not Alder" + + " Lake or Raptor Lake; AMD Zen 4 and later; common on cloud instances). Fixed in OpenJDK 17.0.21.1, 21.0.13.1, 25.0.5.1" + + " and 27.0.1.1 (by JDK-8393212, the change the public descriptions of the CVE match); Java 18 to 20, 22 to 24" + + " and 26 received no fix. -XX:-UseBASE64Intrinsics (or, on x86-64 only, -XX:UseAVX=2) keeps the decoder from being" + + " generated until the runtime is updated." + +// Manifest describes the detector. +func (d *Detector) Manifest() engine.Manifest { + cwe := []string{"CWE-787"} + return engine.Manifest{ + ID: ID, + Title: "Java4Shell: HotSpot AVX-512 Base64 decoder out-of-bounds write (" + knowledge.CVE + ")", + CVEs: []string{knowledge.CVE}, + Advisories: []string{ + "https://calif.io/java4shell/", + "https://github.com/openjdk/jdk/commit/5177a70b385c16f26ede191fdac7aaba0bed0f8b", + "https://semgrep.dev/blog/2026/java4shell-openjdk-base64-vulnerability", + "https://github.com/corretto/corretto-21/blob/21.0.13.1.2/CHANGELOG.md", + }, + Disclosed: knowledge.Disclosed, + Targets: []engine.TargetKind{engine.Filesystem, engine.Image, engine.Repository}, + Kinds: append([]engine.Kind{ + {ID: KindRuntime, Title: "Java runtime vulnerable to Java4Shell", Level: "error", SecuritySeverity: "9.2", CWEs: cwe, + Description: "A HotSpot Java runtime for x86-64 with the AVX-512 VBMI Base64 decoder and without the fix: an installed JDK or JRE, a packaged OpenJDK, a jlink image or a runtime in a container image." + about}, + {ID: KindBundled, Title: "Java runtime bundled in a product is vulnerable to Java4Shell", Level: "error", SecuritySeverity: "9.2", CWEs: cwe, + Description: "A Java runtime that a product ships inside its own install directory (Atlassian Jira and Confluence, Elasticsearch, Artifactory, JetBrains IDEs and others) without the fix. Updating the operating system's Java does not fix it: upgrade the product." + about}, + }, repoKinds()...), + Tags: []string{"security", "java", "openjdk", "hotspot", "Java4Shell"}, + Data: "OpenJDK fixed releases (17.0.21.1, 21.0.13.1, 25.0.5.1, 27.0.1.1), the releases that introduced the decoder, and vendor and distribution status, checked " + knowledge.Checked, + } +} + +// Load checks the release data. +func (d *Detector) Load() error { + prev := 0 + for _, l := range knowledge.Lines { + if l.Feature <= prev || l.Fixed == nil && l.Note == "" || l.Fixed != nil && (l.Fixed[0] != l.Feature || l.FixedBuild < 1) { + return fmt.Errorf("release data for Java %d contradicts itself", l.Feature) + } + prev = l.Feature + } + return nil +} + +// Wants reads VM libraries, release files, the files that set JVM options, +// package databases, crash logs and startup scripts. +func (d *Detector) Wants(p string, head []byte, _ bool) bool { + base := path.Base(p) + switch { + case jvm.IsLibraryName(base): + return archive.IsExecutable(head) + case base == "release", keep(p), jvm.SettingKind(p) != "", hsErr.MatchString(base): + return true + } + return isScript(head) +} + +var hsErr = regexp.MustCompile(`^hs_err_pid\d{1,10}\.log$`) + +// jdkArchive names a Java runtime download as vendors publish it: +// OpenJDK21U-jre_x64_linux_hotspot_21.0.13_9.tar.gz (Temurin), +// amazon-corretto-21-x64-linux-jdk.tar.gz, zulu21.42.19-ca-jdk21.0.13-linux_x64.tar.gz, +// jdk-21_linux-x64_bin.tar.gz (Oracle), bellsoft-jre21.0.13+11-linux-amd64.tar.gz, +// sapmachine-jre-21.0.13.1_linux-x64_bin.tar.gz, microsoft-jdk-21.0.13-linux-x64.tar.gz, +// ibm-semeru-open-jre_x64_linux_21.0.13_11_openj9-0.53.0.tar.gz, openjdk-21.0.2_linux-x64_bin.tar.gz. +var jdkArchive = regexp.MustCompile(`(?i)^(?:OpenJDK\d{1,2}U-(?:jdk|jre)_|amazon-corretto-\d{1,2}[.\w-]*-(?:jdk|jre)|zulu\d[\w.]*-ca-(?:jdk|jre|fx-jdk|fx-jre)\d|jdk-\d{1,2}[\w.]*_(?:linux|windows|macos|osx)-|bellsoft-(?:jdk|jre)\d|sapmachine-(?:jdk|jre)-\d|microsoft-jdk-\d|ibm-semeru-open-(?:jdk|jre)_|openjdk-\d{1,2}[\w.]*_(?:linux|windows|macos|osx)-|graalvm-(?:community-)?jdk-\d)[^/]*\.(?:tar\.gz|tgz|zip|tar\.xz|tar)$`) + +// maxArchivesNoted bounds the runtime downloads recorded per target. +const maxArchivesNoted = 256 + +// VisitHead records runtime downloads by name and first bytes, without +// reading them: the detector does not unpack archives, and a runtime that +// is not installed is context, not a file left unscanned. +func (s *scan) VisitHead(p string, head []byte) { + if len(s.archives) < maxArchivesNoted && jdkArchive.MatchString(path.Base(p)) && archive.IsArchive(head) { + s.archives = append(s.archives, p) + } +} + +func isScript(head []byte) bool { return len(head) > 2 && head[0] == '#' && head[1] == '!' } + +// keep reports the package databases owners are read from. +func keep(p string) bool { + switch p { + case "/lib/apk/db/installed", "/usr/lib/apk/db/installed", "/var/lib/dpkg/status", "/etc/os-release", "/usr/lib/os-release": + return true + } + return assess.IsDpkgList(p) +} + +// Limits on what one target keeps. +const ( + maxKept = 16 << 20 // a package database + maxScript = 256 << 10 + maxHsErr = 4 << 20 + maxHsErrs = 64 + maxLibs = 4096 + maxSetting = 1 << 20 + // Caps on what one target can make the detector keep: an image layer + // of many small files compresses to almost nothing. + maxReleases = 4096 + maxSettings = 4096 + // Plain systemd units are small records; a host has thousands. + maxPlainUnits = 1 << 16 + maxLists = 512 + maxScripts = 8192 + maxKeptTotal = 128 << 20 +) + +// NewScan starts a filesystem or image scan. +func (d *Detector) NewScan(kind engine.TargetKind) engine.Scan { + return &scan{kind: kind, releases: map[string]jvm.Release{}, badReleases: map[string]string{}, + kept: map[string][]byte{}, lists: map[string][]byte{}, scripts: map[string]map[string]bool{}} +} + +type scan struct { + kind engine.TargetKind + libs []*jvm.Library + releases map[string]jvm.Release + badReleases map[string]string + settings []jvm.Setting + kept map[string][]byte + lists map[string][]byte + scripts map[string]map[string]bool + crashes []crash + dropped int + keptBytes int + archives []string // runtime downloads seen by name, not read + optionFiles int // settings files that set JVM options + plainUnits int // systemd units that set none + // limits records the caps this target reached; a capped scan clears + // nothing. + limits []string +} + +func (s *scan) limit(what string) { + for _, l := range s.limits { + if l == what { + return + } + } + s.limits = append(s.limits, what) +} + +func (s *scan) Visit(f engine.File) []engine.Skip { + if f.Member { + return nil // installed runtimes only, not copies inside archives + } + base := path.Base(f.Path) + switch { + case jvm.IsLibraryName(base): + if l, ok := jvm.Inspect(f.Path, f.Data); ok { + if len(s.libs) >= maxLibs { + s.dropped++ + return []engine.Skip{{Path: f.Path, Reason: fmt.Sprintf("more than %d Java VM libraries in one target; not checked", maxLibs)}} + } + s.libs = append(s.libs, l) + } + return nil + case base == "release": + if !archive.IsExecutable(f.Data) { + if len(s.releases)+len(s.badReleases) >= maxReleases { + s.limit(fmt.Sprintf("more than %d release files", maxReleases)) + return nil + } + if r, ok := jvm.ParseRelease(f.Data); ok { + s.releases[path.Dir(f.Path)] = r + } else if len(f.Data) > 64<<10 && bytes.Contains(f.Data[:min(len(f.Data), 4096)], []byte("JAVA_VERSION")) { + s.badReleases[path.Dir(f.Path)] = "its release file is larger than 64 KiB and was not read" + } + } + return nil + case hsErr.MatchString(base): + if len(s.crashes) < maxHsErrs && len(f.Data) <= maxHsErr { + s.crashes = append(s.crashes, readCrash(f.Path, f.Data)) + } + return nil + } + if k := jvm.SettingKind(f.Path); k != "" && len(f.Data) <= maxSetting { + // Only files that set a JVM option (or, for systemd, a JVM + // option variable) are kept and counted: a host has thousands of + // plain units. + if st, ok := jvm.ReadSetting(f.Path, f.Data); ok { + switch { + case st.SetsOptions() && s.optionFiles >= maxSettings: + s.limit(fmt.Sprintf("more than %d files that set JVM options", maxSettings)) + case st.SetsOptions(): + s.settings = append(s.settings, st) + s.optionFiles++ + case st.Kind == "systemd" && s.plainUnits >= maxPlainUnits: + s.limit(fmt.Sprintf("more than %d systemd units", maxPlainUnits)) + case st.Kind == "systemd": + // A unit without JVM options still replaces or extends + // another file of the same unit: keep it, uncounted. + s.settings = append(s.settings, st) + s.plainUnits++ + } + } + } + if keep(f.Path) && len(f.Data) <= maxKept { + if assess.IsDpkgList(f.Path) { + // Only the lines owners are looked up by, without splitting + // the whole list. + var b []byte + for rest := f.Data; len(rest) > 0; { + l := rest + if i := bytes.IndexByte(rest, '\n'); i >= 0 { + l, rest = rest[:i], rest[i+1:] + } else { + rest = nil + } + if bytes.HasSuffix(l, []byte("/libjvm.so")) || bytes.HasSuffix(l, []byte("/release")) { + b = append(append(b, l...), '\n') + } + } + switch { + case len(s.lists) >= maxLists: + s.limit(fmt.Sprintf("more than %d dpkg file lists of Java packages", maxLists)) + case s.keptBytes+len(b) > maxKeptTotal: + s.limit(fmt.Sprintf("more than %d MiB of package data", maxKeptTotal>>20)) + default: + s.lists[strings.TrimSuffix(path.Base(f.Path), ".list")] = b + s.keptBytes += len(b) + } + } else if s.keptBytes+len(f.Data) > maxKeptTotal { + s.limit(fmt.Sprintf("more than %d MiB of package data", maxKeptTotal>>20)) + } else { + s.kept[f.Path] = f.Data + s.keptBytes += len(f.Data) + } + } + if isScript(f.Data) && len(f.Data) <= maxScript { + vars := map[string]bool{} + for _, n := range jvm.ScriptVars { + if bytes.Contains(f.Data, []byte("$"+n)) || bytes.Contains(f.Data, []byte("${"+n)) { + vars[n] = true + } + } + if len(vars) > 0 { + if len(s.scripts) >= maxScripts { + s.limit(fmt.Sprintf("more than %d start scripts that pass JVM options", maxScripts)) + } else { + s.scripts[f.Path] = vars + } + } + } + return nil +} + +func (s *scan) Finish(ctx engine.TargetContext) []engine.Finding { + if s.kind == engine.Filesystem { + if c, ok := thisCPU(); ok { + ctx.Annotate("java4shell_scanner_cpu", c) + } + } + if len(s.crashes) > 0 { + ctx.Annotate("java4shell_crash_logs", s.crashes) + } + if len(s.archives) > 0 { + ctx.Annotate("java4shell_runtime_archives", s.archives) + } + if osr := osRelease(s.kept["/etc/os-release"], s.kept["/usr/lib/os-release"]); osr != "" { + ctx.Annotate("java4shell_os", osr) + } + if len(s.libs) == 0 { + if len(s.limits) > 0 { + ctx.Problem("java4shell: the scan reached its limit on " + strings.Join(s.limits, " and ") + " for one target") + } + return nil + } + links, _ := ctx.Links() + res := resolver{links: links} + owners := map[string]assess.Package{} + for _, db := range []string{"/lib/apk/db/installed", "/usr/lib/apk/db/installed"} { + if b := s.kept[db]; b != nil { + for k, v := range assess.APKOwners(b) { + owners[k] = v + } + } + } + if st := s.kept["/var/lib/dpkg/status"]; st != nil { + for k, v := range assess.DpkgOwners(st, s.lists) { + owners[k] = v + } + } + in := assess.Input{ + Libraries: s.libs, + Releases: s.releases, + BadReleases: s.badReleases, + Settings: s.settings, + Owners: owners, + Resolve: res.resolve, + Scripts: s.scripts, + } + if s.kind == engine.Image { + in.Env = ctx.Env() + if in.Env == nil { + in.Env = []string{} + } + in.Command = ctx.Command() + } + cpu, haveCPU := thisCPU() + capped := "" + if len(s.limits) > 0 { + capped = "the scan reached its limit on " + strings.Join(s.limits, " and ") + " for one target, so package owners and JVM options may be missing: nothing is cleared" + ctx.Problem("java4shell: " + capped) + } + var out []engine.Finding + for _, x := range assess.Run(in) { + f := finding(x) + if capped != "" && f.Verdict.Cleared() { + f.Verdict, f.Reason = engine.Unknown, f.Reason+"; but "+capped + f.CVEs = []engine.CVEResult{{ID: knowledge.CVE, Verdict: f.Verdict, Reason: f.Reason}} + } + if haveCPU && s.kind == engine.Filesystem && x.Status == assess.Vulnerable { + f.Facts["scanner_cpu_runs_it"] = yes(cpu.Affected) + } + out = append(out, f) + } + return out +} + +var verdicts = map[assess.Status]engine.Verdict{ + assess.Vulnerable: engine.Vulnerable, + assess.Fixed: engine.Fixed, + assess.NotAffected: engine.Fixed, + // Mitigated is not cleared: the options can be overridden where the + // runtime is deployed, and the runtime still needs the update. + assess.Mitigated: engine.Vulnerable, + assess.Unknown: engine.Unknown, +} + +func finding(x assess.Finding) engine.Finding { + v, ok := verdicts[x.Status] + if !ok { + v = engine.Unknown + } + facts := map[string]string{"status": string(x.Status), "arch": string(x.Arch)} + if x.Version != "" { + facts["version"], facts["version_from"] = x.Version, x.VersionFrom + } + if x.ReleaseVersion != "" { + facts["release_file_version"] = x.ReleaseVersion + } + if x.VM != "" { + facts["vm"] = x.VM + } + if x.Vendor != "" { + facts["vendor"] = x.Vendor + } + if x.Library != nil { + facts["vm_library"] = x.Library.Path + facts["vbmi_decoder"] = yes(x.Library.VBMIStubs) + } + if x.Product != "" { + facts["product"] = x.Product + } + if x.Package != nil { + facts["package"] = strings.TrimSpace(x.Package.Name + " " + x.Package.Version) + } + if x.JLinked { + facts["jlink"] = "yes" + } + if x.Mitigation != "" { + facts["mitigation"] = x.Mitigation + } + if len(x.MitigatedFor) > 0 { + sort.Strings(x.MitigatedFor) + facts["mitigated_only_for"] = strings.Join(x.MitigatedFor, ",") + } + if fx := fixedFor(x.Version); fx != "" { + facts["fixed_in"] = fx + } + method := "vm-library" + if x.VersionFrom == "release-file" { + method = "release-file" + } + return engine.Finding{ + Kind: x.Kind, + Path: x.Path, + Verdict: v, + Reason: x.Reason, + Message: x.Message, + Method: method, + Facts: facts, + CVEs: []engine.CVEResult{{ID: knowledge.CVE, Verdict: v, Reason: x.Reason}}, + Fix: x.Fix, + Details: x, + } +} + +func fixedFor(version string) string { + v, ok := knowledge.Parse(version) + if !ok { + return "" + } + return knowledge.FixedVersion(v.Feature()) +} + +func yes(b bool) string { + if b { + return "yes" + } + return "no" +} + +// resolver follows symbolic links the way the kernel would, component by +// component, within the target. +type resolver struct{ links map[string]string } + +func (r resolver) resolve(p string) string { + cur := path.Clean("/" + p) + for hops := 0; hops < 40; hops++ { + parts := strings.Split(strings.TrimPrefix(cur, "/"), "/") + changed := false + prefix := "/" + for i, part := range parts { + next := path.Join(prefix, part) + if to, ok := r.links[next]; ok { + if !strings.HasPrefix(to, "/") { + to = path.Join(prefix, to) + } + cur = path.Clean(path.Join(append([]string{to}, parts[i+1:]...)...)) + changed = true + break + } + prefix = next + } + if !changed { + return cur + } + } + return cur // a loop: as far as it got +} + +// osRelease is "ID VERSION_ID" from os-release, for comparing the base OS +// of two images. +func osRelease(files ...[]byte) string { + for _, b := range files { + if len(b) == 0 || len(b) > 64<<10 { + continue + } + var id, ver string + for _, l := range strings.Split(string(b), "\n") { + k, v, ok := strings.Cut(strings.TrimSpace(l), "=") + if !ok { + continue + } + v = osToken(strings.Trim(v, "\"'")) + switch k { + case "ID": + id = v + case "VERSION_ID": + ver = v + } + } + if id != "" { + return strings.TrimSpace(id + " " + ver) + } + } + return "" +} + +// osToken keeps an os-release value short and plain: an image's files +// are not trusted. +func osToken(v string) string { + var b strings.Builder + for _, r := range strings.ToLower(v) { + if b.Len() >= 32 { + break + } + if r >= 'a' && r <= 'z' || r >= '0' && r <= '9' || r == '.' || r == '-' || r == '_' { + b.WriteRune(r) + } + } + return b.String() +} + +// osProbe reads only an image's os-release: a light second read of the +// image a move starts from. +type osProbe struct{} + +func (osProbe) Manifest() engine.Manifest { + return engine.Manifest{ID: "java4shell-osprobe", Targets: []engine.TargetKind{engine.Image}} +} +func (osProbe) Load() error { return nil } +func (osProbe) Wants(p string, _ []byte, _ bool) bool { + return p == "/etc/os-release" || p == "/usr/lib/os-release" +} +func (osProbe) NewScan(engine.TargetKind) engine.Scan { return &osScan{files: map[string][]byte{}} } + +type osScan struct{ files map[string][]byte } + +func (s *osScan) Visit(f engine.File) []engine.Skip { + if len(f.Data) <= 64<<10 { + s.files[f.Path] = f.Data + } + return nil +} +func (s *osScan) Finish(ctx engine.TargetContext) []engine.Finding { + if v := osRelease(s.files["/etc/os-release"], s.files["/usr/lib/os-release"]); v != "" { + ctx.Annotate("java4shell_os", v) + } + return nil +} diff --git a/detectors/java4shell/java4shell_test.go b/detectors/java4shell/java4shell_test.go new file mode 100644 index 0000000..1944905 --- /dev/null +++ b/detectors/java4shell/java4shell_test.go @@ -0,0 +1,153 @@ +package java4shell_test + +import ( + "bytes" + "encoding/json" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/DefectDojo/earlylight/detectors/java4shell" + "github.com/DefectDojo/earlylight/detectors/java4shell/internal/testlib" + "github.com/DefectDojo/earlylight/internal/cli" + "github.com/DefectDojo/earlylight/internal/engine" +) + +var cfg = cli.Config{Tool: "earlylight", Version: "test", Summary: "test"} + +func write(t *testing.T, root, rel string, data []byte) { + t.Helper() + p := filepath.Join(root, rel) + if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(p, data, 0o644); err != nil { + t.Fatal(err) + } +} + +type report struct { + Targets []struct { + Error string `json:"error"` + Findings []engine.Finding `json:"findings"` + Annotations map[string]any `json:"annotations"` + } `json:"targets"` +} + +func run(t *testing.T, args ...string) (report, int) { + t.Helper() + var out, errb bytes.Buffer + full := append([]string{args[0], args[1], "--format", "json", "--detector", "java4shell", "--show-cleared"}, args[2:]...) + code := cli.Run(cfg, full, &out, &errb) + var r report + if err := json.Unmarshal(out.Bytes(), &r); err != nil { + t.Fatalf("%v\n%s\n%s", err, out.String(), errb.String()) + } + return r, code +} + +func byPath(r report) map[string]engine.Finding { + m := map[string]engine.Finding{} + for _, f := range r.Targets[0].Findings { + m[f.Path] = f + } + return m +} + +func TestScanFS(t *testing.T) { + root := t.TempDir() + write(t, root, "opt/java/openjdk/lib/server/libjvm.so", testlib.Build(testlib.Lib{Version: "21.0.13+9-LTS", VBMI: true})) + write(t, root, "opt/java/openjdk/release", testlib.Release("JAVA_VERSION", "21.0.13", "IMPLEMENTOR", "Eclipse Adoptium", "OS_ARCH", "x86_64")) + write(t, root, "opt/fixed/lib/server/libjvm.so", testlib.Build(testlib.Lib{Version: "25.0.5.1+2-LTS", VBMI: true})) + write(t, root, "opt/arm/lib/server/libjvm.so", testlib.Build(testlib.Lib{Machine: "aarch64", Version: "21.0.13+9-LTS"})) + write(t, root, "opt/atlassian/confluence/jre/lib/server/libjvm.so", testlib.Build(testlib.Lib{Version: "17.0.12+7", VBMI: true})) + write(t, root, "opt/atlassian/confluence/bin/setenv.sh", []byte("CATALINA_OPTS=\"-XX:-UseBASE64Intrinsics ${CATALINA_OPTS}\"\n")) + write(t, root, "var/log/app/hs_err_pid42.log", []byte("# JRE version: OpenJDK Runtime Environment (21.0.13+9) (build 21.0.13+9-LTS)\n# Problematic frame:\n# v ~StubRoutines::decodeBlock\nCPU: total 8, avx512f, avx512bw, avx512_vbmi, bmi2\n")) + r, code := run(t, "scan", "fs", "--as-root", root) + if r.Targets[0].Error != "" { + t.Fatal(r.Targets[0].Error) + } + fs := byPath(r) + if f := fs["/opt/java/openjdk"]; f.Kind != java4shell.KindRuntime || f.Verdict != engine.Vulnerable || f.Facts["vendor"] != "Eclipse Adoptium" || f.Facts["fixed_in"] != "21.0.13.1" { + t.Errorf("temurin: %+v", f) + } + if f := fs["/opt/fixed"]; f.Verdict != engine.Fixed || f.Facts["status"] != "fixed" { + t.Errorf("fixed: %+v", f) + } + if f := fs["/opt/arm"]; f.Verdict != engine.Fixed || f.Facts["status"] != "not-affected" || !strings.Contains(f.Reason, "aarch64") { + t.Errorf("arm: %+v", f) + } + if f := fs["/opt/atlassian/confluence/jre"]; f.Kind != java4shell.KindBundled || f.Verdict != engine.Vulnerable || f.Facts["status"] != "mitigated" || f.Fix == "" { + t.Errorf("confluence: %+v", f) + } + if code != 1 { + t.Errorf("exit %d, want 1", code) + } + if _, ok := r.Targets[0].Annotations["java4shell_crash_logs"]; !ok { + t.Error("crash log not annotated") + } + if _, ok := r.Targets[0].Annotations["java4shell_scanner_cpu"]; !ok { + t.Error("scanner CPU not annotated") + } +} + +func TestHostileFS(t *testing.T) { + root := t.TempDir() + // A huge release file claiming a fixed version, next to an unfixed VM. + write(t, root, "opt/a/lib/server/libjvm.so", testlib.Build(testlib.Lib{})) + write(t, root, "opt/a/release", append([]byte("JAVA_VERSION=\"21.0.13.1\"\n"), bytes.Repeat([]byte("# padding\n"), 20000)...)) + // A forged small release file: the VM library decides. + write(t, root, "opt/b/lib/server/libjvm.so", testlib.Build(testlib.Lib{Version: "21.0.13+9", VBMI: true})) + write(t, root, "opt/b/release", testlib.Release("JAVA_VERSION", "21.0.13.1")) + // Symlink loops around a runtime. + if err := os.Symlink("loop2", filepath.Join(root, "opt/loop1")); err != nil { + t.Fatal(err) + } + if err := os.Symlink("loop1", filepath.Join(root, "opt/loop2")); err != nil { + t.Fatal(err) + } + if err := os.Symlink(".", filepath.Join(root, "opt/b/lib/self")); err != nil { + t.Fatal(err) + } + r, _ := run(t, "scan", "fs", "--as-root", root) + fs := byPath(r) + if f := fs["/opt/a"]; f.Verdict != engine.Unknown || !strings.Contains(f.Reason, "64 KiB") { + t.Errorf("huge release: %+v", f) + } + if f := fs["/opt/b"]; f.Verdict != engine.Vulnerable || f.Facts["release_file_version"] != "21.0.13.1" { + t.Errorf("forged release: %+v", f) + } + if len(fs) != 2 { + t.Errorf("findings: %+v", fs) + } +} + +func TestNoJava(t *testing.T) { + root := t.TempDir() + write(t, root, "etc/hostname", []byte("x\n")) + r, code := run(t, "scan", "fs", "--as-root", root) + if len(r.Targets[0].Findings) != 0 || code != 0 { + t.Errorf("findings without Java: %+v (exit %d)", r.Targets[0].Findings, code) + } +} + +func TestManifest(t *testing.T) { + d := &java4shell.Detector{} + if err := d.Load(); err != nil { + t.Fatal(err) + } + m := d.Manifest() + seen := map[string]bool{} + for _, k := range m.Kinds { + if seen[k.ID] { + t.Errorf("duplicate kind %s", k.ID) + } + seen[k.ID] = true + } + for _, k := range []string{java4shell.KindRuntime, java4shell.KindBundled, java4shell.KindImage, java4shell.KindImagePin, java4shell.KindJavaPin, java4shell.KindLine, java4shell.KindPackage, java4shell.KindNotChecked} { + if !seen[k] { + t.Errorf("kind %s not in the manifest", k) + } + } +} diff --git a/detectors/java4shell/pulled_internal_test.go b/detectors/java4shell/pulled_internal_test.go new file mode 100644 index 0000000..cee90be --- /dev/null +++ b/detectors/java4shell/pulled_internal_test.go @@ -0,0 +1,43 @@ +package java4shell + +import ( + "context" + "strings" + "testing" + "time" + + "github.com/DefectDojo/earlylight/detectors/java4shell/internal/repo" + "github.com/DefectDojo/earlylight/internal/engine" + "github.com/DefectDojo/earlylight/internal/source" +) + +// Review finding 14: a pulled image that was read only in part, with no +// finding, does not make the reference vanish. +func TestPulledPartlyRead(t *testing.T) { + f := repo.Finding{Rule: repo.RuleNotChecked, File: "compose.yaml", Line: 3, Status: repo.StatusUnknown, Pull: "x/jira:9", Props: map[string]string{"image": "x/jira:9"}} + it := &engine.Target{Type: engine.Image, Skipped: []source.Skip{{Path: "/opt/atlassian/jira/jre/lib/server/libjvm.so", Reason: "larger than --max-file-size"}}} + got := fromPulled(f, it) + if len(got) != 1 || got[0].Verdict != engine.Unknown || !strings.Contains(got[0].Reason, "in full") { + t.Fatalf("partly read image: %+v", got) + } + it = &engine.Target{Type: engine.Image, Problems: []string{"a damaged package database"}} + if got := fromPulled(f, it); len(got) != 1 || got[0].Verdict != engine.Unknown { + t.Errorf("image with problems: %+v", got) + } + if got := fromPulled(f, &engine.Target{Type: engine.Image}); len(got) != 0 { + t.Errorf("image read in full with no Java runtime: %+v", got) + } +} + +// Review finding 14: the platform lookup goes through the guarded +// transport and has a deadline. +func TestPlatformsGuarded(t *testing.T) { + start := time.Now() + _, err := platforms(context.Background(), "169.254.169.254/library/x:1", engine.Options{Timeout: 5 * time.Second}) + if err == nil { + t.Fatal("a link-local registry was contacted") + } + if time.Since(start) > 10*time.Second { + t.Errorf("no deadline: %s", time.Since(start)) + } +} diff --git a/detectors/java4shell/registry_internal_test.go b/detectors/java4shell/registry_internal_test.go new file mode 100644 index 0000000..825df26 --- /dev/null +++ b/detectors/java4shell/registry_internal_test.go @@ -0,0 +1,172 @@ +package java4shell + +import ( + "archive/tar" + "bytes" + "context" + "io" + "log" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/google/go-containerregistry/pkg/name" + "github.com/google/go-containerregistry/pkg/registry" + v1 "github.com/google/go-containerregistry/pkg/v1" + "github.com/google/go-containerregistry/pkg/v1/empty" + "github.com/google/go-containerregistry/pkg/v1/mutate" + "github.com/google/go-containerregistry/pkg/v1/remote" + "github.com/google/go-containerregistry/pkg/v1/tarball" + + "github.com/DefectDojo/earlylight/detectors/java4shell/internal/repo" + "github.com/DefectDojo/earlylight/detectors/java4shell/internal/testlib" + "github.com/DefectDojo/earlylight/internal/engine" +) + +// pushImage pushes a one-layer linux/amd64 image holding files. +func pushImage(t *testing.T, ref string, files map[string][]byte) { + t.Helper() + var buf bytes.Buffer + tw := tar.NewWriter(&buf) + for p, b := range files { + if err := tw.WriteHeader(&tar.Header{Name: strings.TrimPrefix(p, "/"), Mode: 0o644, Size: int64(len(b)), Typeflag: tar.TypeReg}); err != nil { + t.Fatal(err) + } + tw.Write(b) + } + tw.Close() + data := buf.Bytes() + layer, err := tarball.LayerFromOpener(func() (io.ReadCloser, error) { return io.NopCloser(bytes.NewReader(data)), nil }) + if err != nil { + t.Fatal(err) + } + img, err := mutate.AppendLayers(empty.Image, layer) + if err != nil { + t.Fatal(err) + } + img, err = mutate.ConfigFile(img, &v1.ConfigFile{OS: "linux", Architecture: "amd64", Config: v1.Config{Env: []string{"PATH=/usr/bin"}}}) + if err != nil { + t.Fatal(err) + } + r, err := name.ParseReference(ref) + if err != nil { + t.Fatal(err) + } + if err := remote.Write(r, img); err != nil { + t.Fatal(err) + } +} + +func testRegistry(t *testing.T) string { + t.Helper() + srv := httptest.NewServer(registry.New(registry.Logger(log.New(io.Discard, "", 0)))) + t.Cleanup(srv.Close) + return strings.TrimPrefix(srv.URL, "http://") +} + +func runtimeFiles(version string) map[string][]byte { + return map[string][]byte{ + "/opt/java/openjdk/lib/server/libjvm.so": testlib.Build(testlib.Lib{Version: version, VBMI: true}), + "/etc/os-release": []byte("ID=ubuntu\nVERSION_ID=\"24.04\"\n"), + } +} + +// Review finding 16: tagLister, the verifier and pulled images, against an +// in-memory registry. +func TestRegistryPaths(t *testing.T) { + host := testRegistry(t) + cur := host + "/acme/java:21.0.13_9-jre" + fixed := host + "/acme/java:21.0.13.1_2-jre" + bad := host + "/acme/java:21.0.13.1_1-jre" + none := host + "/acme/java:no-runtime" + pushImage(t, cur, runtimeFiles("21.0.13+9-LTS")) + pushImage(t, fixed, runtimeFiles("21.0.13.1+2-LTS")) + pushImage(t, bad, runtimeFiles("21.0.13.1+1-LTS")) + pushImage(t, none, map[string][]byte{"/etc/os-release": []byte("ID=ubuntu\n")}) + + o := engine.Options{} + tags, err := tagLister(o)(context.Background(), host+"/acme/java") + if err != nil || len(tags) != 4 { + t.Fatalf("tags %v %v", tags, err) + } + + d := &Detector{} + verify := d.verifier(o) + if dg, err := verify(context.Background(), cur, fixed); err != nil || !strings.HasPrefix(dg, "sha256:") { + t.Errorf("fixed target: %q %v", dg, err) + } + for _, target := range []string{bad, none} { + _, err := verify(context.Background(), cur, target) + var dec *repo.Declined + if err == nil || !errorsAs(err, &dec) { + t.Errorf("%s accepted: %v", target, err) + } + } + + // A tree that deploys a product-like image: pulled and judged. + dir := t.TempDir() + os.WriteFile(filepath.Join(dir, "compose.yaml"), []byte("services:\n app:\n image: "+host+"/acme/jira:9\n"), 0o644) + pushImage(t, host+"/acme/jira:9", map[string][]byte{"/opt/atlassian/jira/jre/lib/server/libjvm.so": testlib.Build(testlib.Lib{Version: "17.0.21+9", VBMI: true})}) + tg := d.ScanRepo(context.Background(), dir, o) + if len(tg.Findings) != 1 || tg.Findings[0].Kind != KindImage || tg.Findings[0].Verdict != engine.Vulnerable || tg.Findings[0].Facts["product"] != "Atlassian Jira" { + t.Errorf("pulled: %+v", tg.Findings) + } +} + +func errorsAs(err error, d **repo.Declined) bool { + x, ok := err.(*repo.Declined) + if ok { + *d = x + } + return ok +} + +// Round-2 finding 7: the verifier compares the base OS for every move, +// and the os-release strings it reads are bounded. +func TestVerifierBaseOS(t *testing.T) { + host := testRegistry(t) + cur := host + "/acme/java:21.0.13_9-jre-noble" + same := host + "/acme/java:21.0.13.1_2-jre-noble" + other := host + "/acme/java:21.0.13.1_3-jre-noble" + files := func(v, osr string) map[string][]byte { + m := runtimeFiles(v) + m["/etc/os-release"] = []byte(osr) + return m + } + pushImage(t, cur, files("21.0.13+9-LTS", "ID=ubuntu\nVERSION_ID=\"24.04\"\n")) + pushImage(t, same, files("21.0.13.1+2-LTS", "ID=ubuntu\nVERSION_ID=\"24.04\"\n")) + pushImage(t, other, files("21.0.13.1+3-LTS", "ID=ubuntu\nVERSION_ID=\"26.04\"\n")) + v := (&Detector{}).verifier(engine.Options{}) + if _, err := v(context.Background(), cur, same); err != nil { + t.Errorf("same base declined: %v", err) + } + if _, err := v(context.Background(), cur, other); err == nil { + t.Error("a move from Ubuntu 24.04 to 26.04 accepted under a variant that names the OS") + } + // A deleted current tag that names its OS is tolerated. + if _, err := v(context.Background(), host+"/acme/java:21.0.12_7-jre-noble", same); err != nil { + t.Errorf("deleted current tag: %v", err) + } + if got := osRelease([]byte("ID=" + strings.Repeat("a", 60000) + "\nVERSION_ID=1\n")); len(got) > 80 { + t.Errorf("unbounded os-release: %d bytes", len(got)) + } +} + +// Round-3 finding 3: the os-release probe keeps to --max-file-size. +func TestOSProbeMaxFileSize(t *testing.T) { + host := testRegistry(t) + ref := host + "/acme/big:1" + pushImage(t, ref, map[string][]byte{"/etc/os-release": append([]byte("ID=ubuntu\n"), bytes.Repeat([]byte("#"), 2<<20)...)}) + tg := osOf(ref, engine.Options{MaxFileSize: 1 << 20}) + skipped := false + for _, s := range tg.Skipped { + if s.Path == "/etc/os-release" { + skipped = true + } + } + if !skipped { + t.Errorf("a 2 MiB os-release was read past a 1 MiB limit: %+v", tg.Skipped) + } +} diff --git a/detectors/java4shell/repo.go b/detectors/java4shell/repo.go new file mode 100644 index 0000000..9494a26 --- /dev/null +++ b/detectors/java4shell/repo.go @@ -0,0 +1,423 @@ +package java4shell + +import ( + "context" + "fmt" + "io" + "strings" + "time" + + "github.com/google/go-containerregistry/pkg/authn" + "github.com/google/go-containerregistry/pkg/name" + "github.com/google/go-containerregistry/pkg/v1/remote" + + "github.com/DefectDojo/earlylight/detectors/java4shell/internal/knowledge" + "github.com/DefectDojo/earlylight/detectors/java4shell/internal/repo" + "github.com/DefectDojo/earlylight/internal/engine" + "github.com/DefectDojo/earlylight/internal/netguard" + "github.com/DefectDojo/earlylight/internal/refs" + "github.com/DefectDojo/earlylight/internal/source" +) + +// Kind IDs for a source tree. +const ( + KindImagePin = repo.RuleImagePin + KindJavaPin = repo.RuleJavaPin + KindLine = repo.RuleLine + KindPackage = repo.RulePackage + KindImage = "image" + KindNotChecked = repo.RuleNotChecked +) + +func repoKinds() []engine.Kind { + cwe := []string{"CWE-787"} + return []engine.Kind{ + {ID: KindImagePin, Title: "Source tree uses a Java image without the Java4Shell fix", Level: "error", SecuritySeverity: "9.2", CWEs: cwe, + Description: "A Dockerfile, Compose, Kubernetes, Helm values, CI or Jib file names a Java runtime image (eclipse-temurin, amazoncorretto, azul/zulu-openjdk, bellsoft/liberica, sapmachine and others) whose tag is a Java release without the fix."}, + {ID: KindImage, Title: "Image the source tree uses holds a Java runtime vulnerable to Java4Shell", Level: "error", SecuritySeverity: "9.2", CWEs: cwe, + Description: "An image the tree builds on or deploys, read from its registry for linux/amd64 and judged as scan image does: a moving tag, or a product image (Jira, Elasticsearch, Jenkins...) that carries its own runtime."}, + {ID: KindJavaPin, Title: "Source tree pins a Java release without the Java4Shell fix", Level: "warning", SecuritySeverity: "6.5", CWEs: cwe, + Description: "A version file (.java-version, .sdkmanrc, .tool-versions, mise), a GitHub Actions setup-java step, a buildpack setting or a Maven Enforcer floor that selects or allows a Java release without the fix."}, + {ID: KindLine, Title: "Source tree selects a Java line that received no Java4Shell fix", Level: "error", SecuritySeverity: "9.2", CWEs: cwe, + Description: "A toolchain, version file, CI step, buildpack, image or package of Java 18, 19, 20, 22, 23, 24 or 26: those lines are end of life and received no fix."}, + {ID: KindPackage, Title: "Dockerfile installs a distribution OpenJDK package without the Java4Shell fix", Level: "error", SecuritySeverity: "9.2", CWEs: cwe, + Description: "An apt, apk, dnf, yum or microdnf install of an OpenJDK 17 or later package pinned to a release without the fix, or without a version while the distribution had no fixed package."}, + {ID: KindNotChecked, Title: "Java runtime not checked for the Java4Shell fix", Level: "note", + Description: "An image whose tag does not name one Java release (a moving tag, a product image) that was not read, a workflow expression, or a file that could not be read. It is not known to be safe."}, + } +} + +// ScanRepo checks a source tree. +func (d *Detector) ScanRepo(ctx context.Context, dir string, o engine.Options) *engine.Target { + t := &engine.Target{Name: dir, Type: engine.Repository} + res, err := repo.Scan(ctx, dir, repo.Options{Platforms: o.Platforms}) + if err != nil { + t.Error = err.Error() + return t + } + t.Files = res.Files + if res.Truncated { + t.Incomplete = "the source tree has too many files to walk; what lies past the limit is not checked" + } + pulls := map[string]*engine.Target{} + for _, f := range res.Findings { + if f.Pull != "" && !o.Offline { + t.Findings = append(t.Findings, d.pulled(dir, f, o, pulls, t)...) + continue + } + t.Findings = append(t.Findings, repoFinding(f)) + } + for _, s := range res.Skips { + p := s.File + if s.Line > 0 { + p = fmt.Sprintf("%s:%d", s.File, s.Line) + } + t.Skipped = append(t.Skipped, source.Skip{Path: p, Reason: s.Reason}) + } + return t +} + +func repoFinding(f repo.Finding) engine.Finding { + v, reason := engine.Vulnerable, f.Reason + switch f.Status { + case repo.StatusUnknown: + v = engine.Unknown + } + if reason == "" && v != engine.Vulnerable { + reason = "not decided" + } + facts := map[string]string{} + for k, val := range f.Props { + facts[k] = val + } + if f.Status == repo.StatusMitigated { + facts["status"] = "mitigated" // still vulnerable: a fact, not a clearance + } + out := engine.Finding{Kind: f.Rule, Path: f.File, Line: f.Line, Verdict: v, Reason: reason, Message: f.Message, Method: "source", + Facts: facts, Fix: f.Fix, CVEs: []engine.CVEResult{{ID: knowledge.CVE, Verdict: v, Reason: reason}}} + if f.Level == "warning" { + out.Severity = "medium" + } + return out +} + +// maxRepoImages bounds the images one source-tree scan pulls. +const maxRepoImages = 20 + +// pulled reads an image the tree references (linux/amd64, the only +// affected platform) through the guarded transport and reports its +// runtimes at the reference. +func (d *Detector) pulled(dir string, f repo.Finding, o engine.Options, cache map[string]*engine.Target, t *engine.Target) []engine.Finding { + img := f.Pull + it, ok := cache[img] + if !ok { + if len(cache) >= maxRepoImages { + t.Skipped = append(t.Skipped, source.Skip{Path: fmt.Sprintf("%s:%d", f.File, f.Line), Reason: fmt.Sprintf("image %s not pulled: this scan already pulled %d images", img, maxRepoImages)}) + return []engine.Finding{repoFinding(f)} + } + it = engine.ScanImage([]engine.Detector{d}, img, engine.Options{Platform: "linux/amd64", MaxFileSize: o.MaxFileSize, Guarded: true, PublicOnly: o.PublicOnly}) + cache[img] = it + } + return fromPulled(f, it) +} + +// fromPulled turns what an image scan found into findings at the +// reference. An image read only in part, where nothing was found, is +// reported as not checked: no finding is not proof of no runtime. +func fromPulled(f repo.Finding, it *engine.Target) (out []engine.Finding) { + img := f.Pull + if it.Error != "" { + g := repoFinding(f) + g.Message = fmt.Sprintf("image %s could not be read for Java4Shell: %s", img, refs.CleanText(it.Error, 300)) + g.Reason = "the image could not be pulled" + g.CVEs[0].Reason = g.Reason + return []engine.Finding{g} + } + defer func() { + if len(out) == 0 && (it.Incomplete != "" || it.Gaps() > 0 || len(it.Problems) > 0) { + g := repoFinding(f) + g.Verdict, g.Reason = engine.Unknown, "the image could not be read in full, so a runtime in it may have been missed" + g.Message = fmt.Sprintf("image %s was read only in part (%d files not scanned, %d problems): not checked for Java4Shell", img, it.Gaps(), len(it.Problems)) + g.CVEs = []engine.CVEResult{{ID: knowledge.CVE, Verdict: g.Verdict, Reason: g.Reason}} + out = append(out, g) + } + }() + for _, x := range it.Findings { + if x.Verdict.Cleared() { + continue + } + g := x + g.Kind, g.Path, g.Line, g.Method = KindImage, f.File, f.Line, "registry" + g.Facts = map[string]string{} + for k, v := range x.Facts { + g.Facts[k] = v + } + g.Facts["image"], g.Facts["runtime"] = img, x.Path + g.Message = fmt.Sprintf("image %s: %s: %s", img, x.Path, x.Message) + if mit := f.Props["mitigation"]; mit != "" && x.Verdict == engine.Vulnerable { + // Still vulnerable: the options are a mitigation, recorded as a fact. + g.Facts["status"], g.Facts["mitigation"] = "mitigated", mit + g.Message += "; the deployment sets a mitigation until it is updated" + } + g.Fix = x.Fix + " Use a release of " + img + " built with the fix (or rebuild it), then redeploy." + out = append(out, g) + } + return out +} + +// platforms lists the platforms of an image through the guarded +// transport, within the timeout (image.Platforms uses neither). +func platforms(ctx context.Context, ref string, o engine.Options) ([]string, error) { + if err := registryRef(ref); err != nil { + return nil, err + } + r, err := name.ParseReference(ref) + if err != nil { + return nil, err + } + timeout := o.Timeout + if timeout <= 0 { + timeout = 60 * time.Second + } + ctx, cancel := context.WithTimeout(ctx, timeout) + defer cancel() + desc, err := remote.Get(r, remote.WithContext(ctx), remote.WithTransport(netguard.Transport(o.PublicOnly)), remote.WithAuthFromKeychain(authn.DefaultKeychain)) + if err != nil { + return nil, err + } + if !desc.MediaType.IsIndex() { + return nil, nil + } + idx, err := desc.ImageIndex() + if err != nil { + return nil, err + } + m, err := idx.IndexManifest() + if err != nil { + return nil, err + } + var out []string + for _, d := range m.Manifests { + if d.Platform == nil || d.Platform.OS == "unknown" || d.Annotations["vnd.docker.reference.type"] == "attestation-manifest" { + continue + } + out = append(out, d.Platform.String()) + } + if len(out) == 0 { + return nil, fmt.Errorf("%s is an image index with no images", ref) + } + return out, nil +} + +// Fix moves what can be moved to fixed releases: image tags to the first +// published fixed tag of the same variant (read and checked first) and +// setup-java versions to the moving major version for distributions whose +// newest release is fixed. Each edit is rescanned in memory first. +func (d *Detector) Fix(ctx context.Context, dirs []string, o engine.Options, stdout, stderr io.Writer) int { + reg := repo.Registry{} + if !o.Offline { + reg.Tags = tagLister(o) + reg.Verify = d.verifier(o) + } + code := 0 + for _, dir := range dirs { + ro := repo.Options{Platforms: o.Platforms} + res, err := repo.Scan(ctx, dir, ro) + if err != nil { + fmt.Fprintf(stderr, "%s: %v\n", repo.Printable(dir), err) + return 2 + } + if res.Truncated { + fmt.Fprintf(stdout, "incomplete %s: the source tree has too many files to walk; what lies past the limit is not checked\n", repo.Printable(dir)) + code = max(code, 1) + } + for _, sk := range res.Skips { + fmt.Fprintf(stdout, "not checked %s %s\n", repo.Printable(sk.File), repo.Printable(sk.Reason)) + code = max(code, 1) + } + pulls := map[string]*engine.Target{} + for _, f := range res.Findings { + where := fmt.Sprintf("%s:%d", repo.Printable(f.File), f.Line) + if f.Pull != "" { + if o.Offline { + fmt.Fprintf(stdout, "not checked %s %s\n", where, repo.Printable(f.Message)) + code = max(code, 1) + continue + } + dummy := &engine.Target{} + for _, g := range d.pulled(dir, f, o, pulls, dummy) { + fmt.Fprintf(stdout, "not fixed %s %s\n %s\n", where, repo.Printable(g.Message), repo.Printable(g.Fix)) + if g.Facts["product"] != "" && f.Props["mitigation"] == "" && strings.HasSuffix(strings.ToLower(f.File), "dockerfile") { + fmt.Fprintf(stdout, " optional interim mitigation (not applied):\n %s\n", repo.Printable(repo.Mitigation(f.File, f.Line))) + } + code = max(code, 1) + } + continue + } + // A mitigated pin still needs the update, so fix moves it too. + if f.Level == "note" && f.Rule != repo.RuleNotChecked && f.Status != repo.StatusMitigated { + continue + } + if f.Rule == repo.RuleNotChecked { + fmt.Fprintf(stdout, "not checked %s %s\n", where, repo.Printable(f.Message)) + code = max(code, 1) + continue + } + if !f.Fixable { + fmt.Fprintf(stdout, "not fixed %s %s\n %s\n", where, repo.Printable(f.Message), repo.Printable(f.Fix)) + code = max(code, 1) + continue + } + ch, err := repo.Plan(ctx, dir, f, reg, ro) + if err != nil { + fmt.Fprintf(stdout, "not fixed %s %s\n %s\n", where, repo.Printable(f.Message), repo.Printable(err.Error())) + code = max(code, 1) + continue + } + verb := "fixed" + if o.DryRun { + verb = "would fix" + } else if err := repo.Apply(dir, ch); err != nil { + fmt.Fprintf(stderr, "%s: %v\n", repo.Printable(ch.File), err) + return 2 + } + fmt.Fprintf(stdout, "%-10s %s %s\n%s", verb, where, repo.Printable(ch.Summary), indent(repo.Diff(ch))) + } + } + return code +} + +func indent(s string) string { + var b strings.Builder + for _, l := range strings.Split(strings.TrimRight(s, "\n"), "\n") { + b.WriteString(" " + l + "\n") + } + return b.String() +} + +// tagLister lists an image repository's tags through the guarded +// transport, a page at a time, at most 20,000, within the timeout. +func tagLister(o engine.Options) func(context.Context, string) ([]string, error) { + return func(ctx context.Context, img string) ([]string, error) { + if err := registryRef(img); err != nil { + return nil, err + } + repoName, err := name.NewRepository(img) + if err != nil { + return nil, err + } + timeout := o.Timeout + if timeout <= 0 { + timeout = 60 * time.Second + } + lctx, cancel := context.WithTimeout(ctx, timeout) + defer cancel() + p, err := remote.NewPuller(remote.WithContext(lctx), remote.WithTransport(netguard.Transport(o.PublicOnly)), remote.WithAuthFromKeychain(authn.DefaultKeychain)) + if err != nil { + return nil, err + } + l, err := p.Lister(lctx, repoName) + if err != nil { + return nil, err + } + var tags []string + for l.HasNext() && len(tags) < 20000 { + page, err := l.Next(lctx) + if err != nil { + return nil, err + } + tags = append(tags, page.Tags...) + } + return tags, nil + } +} + +// registryRef accepts only a registry reference: never a local archive or +// any other transport a source file could name. +func registryRef(ref string) error { + if strings.Contains(ref, "://") || strings.HasPrefix(ref, "docker-archive:") || strings.HasPrefix(ref, "rootfs:") || strings.HasPrefix(ref, "/") { + return fmt.Errorf("%q is not a registry reference", ref) + } + if _, err := name.ParseReference(ref); err != nil { + return fmt.Errorf("%q is not a valid image reference", ref) + } + return nil +} + +// verifier reads the replacement image on linux/amd64 (and checks it is +// published for every platform the current one has) and requires the +// detector to find at least one runtime with the fix and none without it. +func (d *Detector) verifier(o engine.Options) func(ctx context.Context, current, target string) (string, error) { + return func(ctx context.Context, current, target string) (string, error) { + if err := registryRef(target); err != nil { + return "", err + } + cur, _ := platforms(ctx, current, o) // the current tag may be gone: judge the target on its own + tp, err := platforms(ctx, target, o) + if err != nil { + return "", err + } + have := map[string]bool{} + for _, p := range tp { + have[p] = true + } + for _, p := range cur { + if len(tp) > 0 && !have[p] { + return "", &repo.Declined{Reason: fmt.Sprintf("%s is not published for %s, which %s has; change it by hand", target, p, current)} + } + } + t := engine.ScanImage([]engine.Detector{d}, target, engine.Options{Platform: "linux/amd64", MaxFileSize: o.MaxFileSize, Guarded: true, PublicOnly: o.PublicOnly}) + if t.Error != "" { + return "", fmt.Errorf("reading %s: %s", target, t.Error) + } + // The base OS: distribution and major version must stay (a tag + // whose variant does not name it, "-jre", can move to another + // distribution; one that does can move to its next release). The + // current image is read for its os-release only. A current tag that + // is gone is tolerated when the new tag names its OS. + c := osOf(current, o) + _, tag, _ := strings.Cut(target[strings.LastIndex(target, "/")+1:], ":") + if c.Error == "" || !repo.NamesBaseOS(tag) { + if err := repo.SameBase(annotation(c, "java4shell_os"), annotation(t, "java4shell_os")); err != nil { + return "", err + } + } + if t.Incomplete != "" || t.Gaps() > 0 || len(t.Problems) > 0 { + return "", &repo.Declined{Reason: fmt.Sprintf("%s could not be read in full; change it by hand", target)} + } + fixed := 0 + for _, f := range t.Findings { + switch { + case f.Verdict == engine.Vulnerable || f.Verdict == engine.Unknown || f.Verdict == engine.Inactive: + return "", &repo.Declined{Reason: fmt.Sprintf("%s still holds %s (%s); change it by hand", target, f.Path, f.Verdict)} + case f.Facts["status"] == "fixed": + fixed++ + } + } + if fixed == 0 { + return "", &repo.Declined{Reason: fmt.Sprintf("no Java runtime with the fix was found in %s, so the fix is not proven; change it by hand", target)} + } + ref, err := name.ParseReference(target) + if err != nil { + return "", err + } + desc, err := remote.Head(ref, remote.WithAuthFromKeychain(authn.DefaultKeychain), remote.WithContext(ctx), remote.WithTransport(netguard.Transport(o.PublicOnly))) + if err != nil { + return "", err + } + return desc.Digest.String(), nil + } +} + +func annotation(t *engine.Target, key string) string { + if t == nil || t.Error != "" { + return "" + } + v, _ := t.Annotations[key].(string) + return v +} + +// osOf reads an image for its os-release only. +func osOf(ref string, o engine.Options) *engine.Target { + return engine.ScanImage([]engine.Detector{osProbe{}}, ref, engine.Options{Platform: "linux/amd64", MaxFileSize: o.MaxFileSize, Guarded: true, PublicOnly: o.PublicOnly}) +} diff --git a/detectors/java4shell/review2_test.go b/detectors/java4shell/review2_test.go new file mode 100644 index 0000000..c706df8 --- /dev/null +++ b/detectors/java4shell/review2_test.go @@ -0,0 +1,114 @@ +package java4shell_test + +import ( + "encoding/json" + "fmt" + "os" + "path/filepath" + "regexp" + "strings" + "testing" + + "github.com/DefectDojo/earlylight/detectors/java4shell" + "github.com/DefectDojo/earlylight/detectors/java4shell/internal/testlib" + "github.com/DefectDojo/earlylight/internal/engine" +) + +// Review finding 3: a mitigated runtime stays on the list (vulnerable, with +// the mitigation as a fact) and the scan exits 1. +func TestMitigatedIsNotCleared(t *testing.T) { + root := t.TempDir() + write(t, root, "opt/atlassian/jira/jre/lib/server/libjvm.so", testlib.Build(testlib.Lib{Version: "21.0.13+9-LTS", VBMI: true})) + write(t, root, "opt/atlassian/jira/bin/setenv.sh", []byte("JVM_SUPPORT_RECOMMENDED_ARGS=\"-XX:-UseBASE64Intrinsics\"\n")) + r, code := run(t, "scan", "fs", "--as-root", root) + f := byPath(r)["/opt/atlassian/jira/jre"] + if f.Verdict != engine.Vulnerable || f.Facts["status"] != "mitigated" || f.Facts["mitigation"] == "" || code != 1 { + t.Errorf("mitigated: %s %q exit %d", f.Verdict, f.Facts["status"], code) + } +} + +// Review finding 7: what a target makes the detector keep is capped, and +// a capped scan clears nothing. +func TestKeptCaps(t *testing.T) { + root := t.TempDir() + write(t, root, "opt/fixed/lib/server/libjvm.so", testlib.Build(testlib.Lib{Version: "21.0.13.1+2-LTS", VBMI: true})) + for i := 0; i < 600; i++ { + write(t, root, fmt.Sprintf("var/lib/dpkg/info/openjdk-%d.list", i), []byte("/usr/lib/jvm/x/lib/server/libjvm.so\n")) + } + r, _ := run(t, "scan", "fs", "--as-root", root) + f := byPath(r)["/opt/fixed"] + if f.Verdict != engine.Unknown || !strings.Contains(f.Reason, "limit") { + t.Errorf("capped scan: %s (%s)", f.Verdict, f.Reason) + } +} + +// Review findings 14 and round-2 5: a JDK download in the target is +// recorded by name as context, without reading it and without counting it +// as a file not scanned; other archives are not. +func TestJDKArchiveRecorded(t *testing.T) { + root := t.TempDir() + zipHead := []byte("PK\x03\x04") + write(t, root, "opt/OpenJDK21U-jre_x64_linux_hotspot_21.0.13_9.tar.gz", []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03")) + write(t, root, "opt/amazon-corretto-21-x64-linux-jdk.tar.gz", []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03")) + write(t, root, "docs/openjdk-migration-notes.zip", zipHead) + write(t, root, "docs/majreport-2026.zip", zipHead) + r, code := run(t, "scan", "fs", "--fail-on-skip", "--as-root", root) + got, _ := json.Marshal(r.Targets[0].Annotations["java4shell_runtime_archives"]) + if !strings.Contains(string(got), "OpenJDK21U-jre_x64_linux_hotspot_21.0.13_9.tar.gz") || !strings.Contains(string(got), "amazon-corretto-21-x64-linux-jdk.tar.gz") { + t.Errorf("runtime downloads not recorded: %s", got) + } + if strings.Contains(string(got), "migration") || strings.Contains(string(got), "majreport") { + t.Errorf("other archives recorded: %s", got) + } + if code != 0 { + t.Errorf("exit %d: a recorded download counted as a file not scanned", code) + } + if (&java4shell.Detector{}).Wants("/opt/OpenJDK21U-jre_x64_linux_hotspot_21.0.13_9.tar.gz", []byte("\x1f\x8b\x08"), false) { + t.Error("a runtime download is read in full") + } +} + +// Round-2 finding 4: units that set no JVM option do not count toward the +// cap. +func TestPlainUnitsUncapped(t *testing.T) { + root := t.TempDir() + write(t, root, "opt/fixed/lib/server/libjvm.so", testlib.Build(testlib.Lib{Version: "21.0.13.1+2-LTS", VBMI: true})) + for i := 0; i < 4100; i++ { + write(t, root, fmt.Sprintf("etc/systemd/system/s%d.service", i), []byte("[Service]\nExecStart=/usr/bin/true\n")) + } + r, code := run(t, "scan", "fs", "--as-root", root) + if f := byPath(r)["/opt/fixed"]; f.Verdict != engine.Fixed || code != 0 { + t.Errorf("plain units capped the scan: %s (%s) exit %d", f.Verdict, f.Reason, code) + } +} + +// A full /etc override without JVM options still replaces the /usr/lib +// unit that set them. +func TestEtcOverrideKept(t *testing.T) { + root := t.TempDir() + write(t, root, "opt/acme/jre/lib/server/libjvm.so", testlib.Build(testlib.Lib{Version: "21.0.13+9-LTS", VBMI: true})) + write(t, root, "usr/lib/systemd/system/acme.service", []byte("[Service]\nEnvironment=JAVA_TOOL_OPTIONS=-XX:-UseBASE64Intrinsics\nExecStart=/opt/acme/bin/run.sh\n")) + write(t, root, "etc/systemd/system/acme.service", []byte("[Service]\nExecStart=/opt/acme/bin/run.sh\n")) + r, _ := run(t, "scan", "fs", "--as-root", root) + if f := byPath(r)["/opt/acme/jre"]; f.Facts["status"] == "mitigated" { + t.Errorf("the /usr/lib unit's mitigation counted over the /etc override: %+v", f.Facts) + } +} + +// Round-2 nits: dates live in the knowledge package (the data date is +// knowledge.Checked), and reasons read cleanly. +func TestDatesInOnePlace(t *testing.T) { + err := filepath.WalkDir(".", func(p string, d os.DirEntry, err error) error { + if err != nil || d.IsDir() || !strings.HasSuffix(p, ".go") || strings.HasSuffix(p, "_test.go") || strings.HasSuffix(p, "knowledge.go") || strings.Contains(p, "testlib") { + return err + } + b, _ := os.ReadFile(p) + if regexp.MustCompile(`"[^"\n]*2026-\d\d-\d\d`).Match(b) { + t.Errorf("%s writes a date in a string; take it from the knowledge package", p) + } + return nil + }) + if err != nil { + t.Fatal(err) + } +} diff --git a/detectors/java4shell/testdata/fuzz/FuzzReadCrash/dfc88264e848b553 b/detectors/java4shell/testdata/fuzz/FuzzReadCrash/dfc88264e848b553 new file mode 100644 index 0000000..d2c61ee --- /dev/null +++ b/detectors/java4shell/testdata/fuzz/FuzzReadCrash/dfc88264e848b553 @@ -0,0 +1,2 @@ +go test fuzz v1 +[]byte("# Problematic frame:\n#00000000000000AA00AAAAAAAAA0A\x010Sȿ4\xb3O\x86\xe3\xb9\x05\xe6\xc4R\xc8^\v\f\x19\xee\xde\xd2>\xf8Jy\xa6\xa5_A\x19\xe8\xa5\x1fי\v\xe0\xe8\xf4\x11\xa8\xf4F\x8c\x9fDu?\xc3ߙ|Chܓ\xc5\f\x90\x89F\x93G\xeaF>t\xf1no\xb4\xa7x\b\xf9\xb57[\x1c\x1eE0\xdbH\xaeAr\x8b\xb3\x82\x1b\xf8Ӫ\x98\x918\x83\x1a¾\xff`\x0e\x84X\x8f~\x8e\xb1\x9c\xff\x8f;'4\xeb\xda\xd0\xe3\xea\xe0\"<\xad\xa9\x8e\x96\xa4\x86\x8br]\x9e\x8f\xa3\xb4\x1f\xa3\xd2\xdfS\xe93\x03CP\xc2\xfa\x19\xf9\x98\xdf\xf7\xa0OsQ˫\x1d\xf4{mͣ$\x8f\x1fzv\xd9lk\x19\xfaaH\x02ȓr:\x82h\x87@\x84-\t\xc6d\xe3\a\xb2`\x87\xe9\xc5\xccxo(\xe5E\x93\x13@\xa3%\xa1\xb3\xef)C\xe8\xf6\xe1\xb3xG\xdf-f\x131\xeb#\xbd\"=\xfb\xfbØ \xa8!\xdaX\xc8#\x85\xaf{\xac{\x06\xc9\xe9`\x9b)BR2~\x1bE\xd2\xf1ln\xc6FX\xbb\x91;\xa4\xe7\x1eӶ\x06`ce\xcf)\x80'F\xef訄mU;\xb9E\xa72\x8bE\x82.\xa1\xb83C\xad\xdd\x0e\xf7\xf7r3\x9f\xb4}4\x03\x1f=$\x8b(Vř|\x9b\xa9O\xa1\x9c\xb0\xaa%\xc1o\xef\xa7B#=\x96\x8aAAA00AAAAA0006AAA000A00AAA000AAAAa") diff --git a/go.mod b/go.mod index 635bc81..2d34f5a 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/DefectDojo/earlylight -go 1.27.3 +go 1.27.4 require ( github.com/glebarez/go-sqlite v1.23.0 @@ -9,6 +9,7 @@ require ( github.com/knqyf263/go-rpmdb v0.1.1 github.com/ulikunitz/xz v0.5.17 golang.org/x/crypto v0.58.0 + golang.org/x/sys v0.49.0 gopkg.in/yaml.v3 v3.0.1 software.sslmate.com/src/go-pkcs12 v0.7.4 ) @@ -25,7 +26,6 @@ require ( github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/sirupsen/logrus v1.10.2 // indirect golang.org/x/sync v0.24.0 // indirect - golang.org/x/sys v0.49.0 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect gotest.tools/v3 v3.5.2 // indirect modernc.org/libc v1.74.1 // indirect diff --git a/internal/engine/engine.go b/internal/engine/engine.go index c05a48b..5dd488f 100644 --- a/internal/engine/engine.go +++ b/internal/engine/engine.go @@ -186,6 +186,14 @@ type Reader interface { Wants(path string, head []byte, executable bool) bool } +// HeadVisitor is implemented by a Scan that wants to see the first bytes +// of every file the walk meets, read in full or not: a record of files +// it does not need to read (a runtime download inside an image) without +// the cost of reading them. +type HeadVisitor interface { + VisitHead(path string, head []byte) +} + // ErrNotApplicable is what a Prober returns when the address does not run // the service it checks (no SSH server where a TLS check was asked for). The // host scan skips that detector, and fails only when every detector did. diff --git a/internal/engine/engine_test.go b/internal/engine/engine_test.go index c5264c3..b8bf6cc 100644 --- a/internal/engine/engine_test.go +++ b/internal/engine/engine_test.go @@ -226,3 +226,68 @@ func (p *panicky) Visit(f File) []Skip { } return p.fake.Visit(f) } + +// headFake is a scan that also sees every file's head. +type headFake struct { + *fake + heads *[]string +} + +func (h headFake) NewScan(TargetKind) Scan { return h } +func (h headFake) VisitHead(p string, _ []byte) { *h.heads = append(*h.heads, p) } +func (h headFake) Wants(p string, _ []byte, _ bool) bool { return h.wants(p) } + +func TestHeadVisitorSeesUnreadFiles(t *testing.T) { + root := tree(t, map[string][]byte{"etc/a.conf": []byte("a"), "opt/jdk.tar.gz": []byte("\x1f\x8b")}) + var heads []string + h := headFake{&fake{id: "t-head", wants: func(p string) bool { return strings.HasSuffix(p, ".conf") }, seen: &[]string{}}, &heads} + ScanFS([]Detector{h}, root, Options{AsRoot: true}) + sort.Strings(heads) + if strings.Join(heads, ",") != "/etc/a.conf,/opt/jdk.tar.gz" || strings.Join(*h.seen, ",") != "/etc/a.conf" { + t.Errorf("heads %v, read %v", heads, *h.seen) + } +} + +// headPanic panics in VisitHead on one file. +type headPanic struct{ headFake } + +func (h headPanic) NewScan(TargetKind) Scan { return h } +func (h headPanic) VisitHead(p string, b []byte) { + if strings.HasSuffix(p, "bad") { + panic("boom") + } + h.headFake.VisitHead(p, b) +} + +func TestHeadVisitorPanicContained(t *testing.T) { + root := tree(t, map[string][]byte{"etc/a.conf": []byte("a"), "opt/bad": []byte("x")}) + var heads []string + h := headPanic{headFake{&fake{id: "t-headpanic", wants: func(p string) bool { return strings.HasSuffix(p, ".conf") }, seen: &[]string{}}, &heads}} + other := &fake{id: "t-other", wants: func(p string) bool { return true }} + tg := ScanFS([]Detector{h, detector(other)}, root, Options{AsRoot: true}) + if tg.Error != "" || len(*other.seen) != 2 { + t.Fatalf("a VisitHead panic stopped the target: %q, other saw %v", tg.Error, *other.seen) + } + failed := false + for _, s := range tg.Skipped { + if s.Path == "/opt/bad" && s.DetectorFailed { + failed = true + } + } + if !failed { + t.Errorf("the panic was not recorded: %+v", tg.Skipped) + } +} + +// With a detector that wants every file, head visitors still see each one. +func TestHeadVisitorWithAllReader(t *testing.T) { + root := tree(t, map[string][]byte{"etc/a.conf": []byte("a"), "opt/x": []byte("x")}) + var heads []string + h := headFake{&fake{id: "t-head2", wants: func(string) bool { return false }, seen: &[]string{}}, &heads} + all := &fake{id: "t-all2"} + ScanFS([]Detector{h, detector(all)}, root, Options{AsRoot: true}) + sort.Strings(heads) + if strings.Join(heads, ",") != "/etc/a.conf,/opt/x" || len(*all.seen) != 2 { + t.Errorf("heads %v, all saw %v", heads, *all.seen) + } +} diff --git a/internal/engine/run.go b/internal/engine/run.go index 02d6e3b..cd6f732 100644 --- a/internal/engine/run.go +++ b/internal/engine/run.go @@ -237,15 +237,42 @@ func (w *walker) sourceOptions(opts Options) source.Options { Symlink: func(p, t string) { w.ctx.symlinks[w.abs(p)] = t }, Hardlink: func(p, t string) { w.ctx.hardlinks[w.abs(p)] = w.abs(t) }, } + type headScan struct { + id string + h HeadVisitor + } + var heads []headScan + for i, s := range w.scans { + if h, ok := s.(HeadVisitor); ok { + heads = append(heads, headScan{w.dets[i].Manifest().ID, h}) + } + } + visitHead := func(p string, head []byte) { + for _, h := range heads { + w.visitHeadSafely(h.id, h.h, w.abs(p), head) + } + } var readers []Reader + all := false for _, d := range w.dets { r, ok := d.(Reader) if !ok { - return so // this detector wants everything + all = true // this detector wants everything + break } readers = append(readers, r) } + if all { + if len(heads) > 0 { + so.Read = func(p string, head []byte, _ bool) bool { + visitHead(p, head) + return true + } + } + return so + } so.Read = func(p string, head []byte, exe bool) bool { + visitHead(p, head) for _, r := range readers { if r.Wants(w.abs(p), head, exe) { return true @@ -299,6 +326,18 @@ func (w *walker) visitSafely(i int, s Scan, f File) (skips []Skip) { return s.Visit(f) } +// visitHeadSafely runs one detector's VisitHead on one file. A detector +// that panics on a hostile file loses that file, reported as not scanned, +// and the scan goes on, as with Visit. +func (w *walker) visitHeadSafely(id string, h HeadVisitor, p string, head []byte) { + defer func() { + if r := recover(); r != nil { + w.t.Skipped = append(w.t.Skipped, source.Skip{Path: p, Reason: fmt.Sprintf("detector %s failed on this file (%v); not checked", id, r), DetectorFailed: true}) + } + }() + h.VisitHead(p, head) +} + func (w *walker) finish() { for i, s := range w.scans { fs, err := finishSafely(s, w.ctx) diff --git a/parity/JAVA4SHELL-RESULTS.md b/parity/JAVA4SHELL-RESULTS.md new file mode 100644 index 0000000..335feaa --- /dev/null +++ b/parity/JAVA4SHELL-RESULTS.md @@ -0,0 +1,47 @@ +# Java4Shell fixture results + +Generated by `parity/java4shell.py`. Each row's expected findings come from [FACTS.md](../detectors/java4shell/FACTS.md). Registry images are scanned by tag; the digest scanned is listed. + +| Case | What | Result | Findings (platform, path, kind, verdict, status, version from) | +| --- | --- | --- | --- | +| temurin-8 | Temurin 8: no Base64 decode intrinsic
`eclipse-temurin:8u512-b09-jre`
linux/amd64 sha256:9b84aee27144e52f023cb1895b9579519806890f7fc989f89c4e735f51287745 | pass | linux/amd64 /opt/java/openjdk runtime fixed not-affected vm-library | +| temurin-11 | Temurin 11: no Base64 decode intrinsic
`eclipse-temurin:11.0.33_10-jre`
linux/amd64 sha256:81296a4bedf616c682869254cd55e4a3bff824498d318fcef224e76237e5f69b | pass | linux/amd64 /opt/java/openjdk runtime fixed not-affected vm-library | +| temurin-17.0.5 | Temurin 17.0.5, the last 17 update before the decoder (8268276 came in 17.0.6)
`eclipse-temurin:17.0.5_8-jre`
linux/amd64 sha256:402c656f078bc116a6db1e2e23b08c6f4a78920a2c804ea4c2d3e197f1d6b47c | pass | linux/amd64 /opt/java/openjdk runtime fixed not-affected vm-library | +| temurin-17.0.6 | Temurin 17.0.6, the first 17 update with the decoder
`eclipse-temurin:17.0.6_10-jre`
linux/amd64 sha256:7dacdab7c335d90cf4e110f5744ca27d5a3ed87901a7c08022c8c12055438bd2 | pass | linux/amd64 /opt/java/openjdk runtime vulnerable vulnerable vm-library | +| temurin-17.0.21-alpine | Temurin 17.0.21 on Alpine (musl), before 17.0.21.1
`eclipse-temurin:17.0.21_9-jre-alpine`
linux/amd64 sha256:df2fcd61a7987cfd3514756f2cb6c6e940fdd8076bdfd4e362121850e6893a3f | pass | linux/amd64 /opt/java/openjdk runtime vulnerable vulnerable vm-library | +| temurin-21-alpine | Temurin 21.0.13 on Alpine: a musl VM library (linux-amd64-musl)
`eclipse-temurin:21.0.13_9-jre-alpine`
linux/amd64 sha256:b6ea71e690a72ba240b3d1b5c477410014862f9ea2cc73ab40e41ff21b03ba1c | pass | linux/amd64 /opt/java/openjdk runtime vulnerable vulnerable vm-library | +| temurin-18 | Temurin 18.0.2.1: end of life, no fix
`eclipse-temurin:18.0.2.1_1-jre`
linux/amd64 sha256:0d503e979d1b0c61d122a26985e23e42e87741198fd967c0c1efeefbd240ff95 | pass | linux/amd64 /opt/java/openjdk runtime vulnerable vulnerable vm-library | +| temurin-20 | Temurin 20.0.2: end of life, no fix
`eclipse-temurin:20.0.2_9-jre`
linux/amd64 sha256:db32cb29a978fa782ef97bf3a39f4955295712ebff305839c3faaf64f5c5b22b | pass | linux/amd64 /opt/java/openjdk runtime vulnerable vulnerable vm-library | +| temurin-21 | Temurin 21.0.13, before 21.0.13.1
`eclipse-temurin:21.0.13_9-jre`
linux/amd64 sha256:15ed0883abf7550992a343740223a8bab2292e7db126986cde97c0be58aeb06f | pass | linux/amd64 /opt/java/openjdk runtime vulnerable vulnerable vm-library | +| temurin-21-arm64 | Temurin 21.0.13 for linux/arm64: only x86-64 has the decoder
`eclipse-temurin:21.0.13_9-jre`
linux/arm64/v8 sha256:4a541e56af0ae9a1ff20cdbf43e400864542116b0e7804b37f51be4c574f2057 | pass | linux/arm64/v8 /opt/java/openjdk runtime fixed not-affected vm-library | +| temurin-21-all | Temurin 21.0.13, every platform of the multi-arch image (Linux and the two Windows Server images, whose layers name C:\ as /Files)
`eclipse-temurin:21.0.13_9-jre`
linux/amd64 sha256:15ed0883abf7550992a343740223a8bab2292e7db126986cde97c0be58aeb06f
linux/arm64/v8 sha256:4a541e56af0ae9a1ff20cdbf43e400864542116b0e7804b37f51be4c574f2057
linux/ppc64le sha256:189a1d03c2789f872b201886a783632fb7c3e8d503110efa19fb08c47b45f05f
linux/s390x sha256:e1542725522c2fe226e7915012cf0aad5751fb29540528776ce77982cbd1785b
windows/amd64 sha256:47e645496b5244c0fe56534299fbd826cfc4b5626635db8076ea445f8d0a80e2
windows/amd64 sha256:85cc6d636a02ce992461587b5d27d00dd511859ecaaed1e1e0e1f93b44ccb0ab | pass | linux/amd64 /opt/java/openjdk runtime vulnerable vulnerable vm-library
linux/arm64/v8 /opt/java/openjdk runtime fixed not-affected vm-library
linux/ppc64le /opt/java/openjdk runtime fixed not-affected vm-library
linux/s390x /opt/java/openjdk runtime fixed not-affected vm-library
windows/amd64 /Files/openjdk-21 runtime vulnerable vulnerable vm-library
windows/amd64 /Files/openjdk-21 runtime vulnerable vulnerable vm-library | +| temurin-21-ubi9 | Temurin 21.0.13 on UBI 9 minimal
`eclipse-temurin:21.0.13_9-jre-ubi9-minimal`
linux/amd64 sha256:17e523f3696b36177c5c9a44399f8a4a4634229fdf3f14d4c7582d2597931a1d | pass | linux/amd64 /opt/java/openjdk runtime vulnerable vulnerable vm-library | +| temurin-25 | Temurin 25.0.5, before 25.0.5.1
`eclipse-temurin:25.0.5_8-jre`
linux/amd64 sha256:fa681c5833ecc085202c0923e909f9de888d80d22a7f38d6c51268ae5978c1d7 | pass | linux/amd64 /opt/java/openjdk runtime vulnerable vulnerable vm-library | +| temurin-27 | Temurin 27.0.1.0.2, before 27.0.1.1
`eclipse-temurin:27.0.1.0-jre`
linux/amd64 sha256:2719c23ca6855fed092edd3f47fbea2e6870bfd00fa4e10b2077126d551bca28 | pass | linux/amd64 /opt/java/openjdk runtime vulnerable vulnerable vm-library | +| corretto-21-al2023-unfixed | Amazon Corretto 21.0.13.0.11, an rpm on Amazon Linux 2023 (the amazoncorretto:21.0.13-al2023-headless image of 2026-11-17, pinned by digest)
`amazoncorretto@sha256:21a9456bd7fee7045c545bf089e63ffc69ceb7d594b3c5bfb25bace199d3f566`
linux/amd64 sha256:21a9456bd7fee7045c545bf089e63ffc69ceb7d594b3c5bfb25bace199d3f566 | pass | linux/amd64 /usr/lib/jvm/java-21-amazon-corretto.x86_64 runtime vulnerable vulnerable vm-library | +| corretto-21-al2023 | amazoncorretto:21.0.13-al2023-headless as republished on 2026-11-18 with Corretto 21.0.13.1.2 (the tag 21.0.13 moves to the out-of-band release)
`amazoncorretto:21.0.13-al2023-headless`
linux/amd64 sha256:47cbd3767e95fd412adbb9c81ce371574bc8f67eebddc9ef319c25c6d0c0a601 | pass | linux/amd64 /usr/lib/jvm/java-21-amazon-corretto.x86_64 runtime fixed fixed vm-library | +| corretto-17-alpine-unfixed | Amazon Corretto 17.0.21 on Alpine (the amazoncorretto:17.0.21-alpine3.22 image of 2026-11-17, pinned by digest)
`amazoncorretto@sha256:92cdff306e26329894ef0fab79f8d120ac0acc08f3b675206b66a7da85316043`
linux/amd64 sha256:92cdff306e26329894ef0fab79f8d120ac0acc08f3b675206b66a7da85316043 | pass | linux/amd64 /usr/lib/jvm/java-17-amazon-corretto runtime vulnerable vulnerable vm-library | +| corretto-17-alpine | amazoncorretto:17.0.21-alpine3.22 as republished on 2026-11-18 with Corretto 17.0.21.1.2
`amazoncorretto:17.0.21-alpine3.22`
linux/amd64 sha256:e6d108efd17fd318481c1db6ab6f8a79d0823c77408ae4d4d1fb7c6b9e201a1e | pass | linux/amd64 /usr/lib/jvm/java-17-amazon-corretto runtime fixed fixed vm-library | +| zulu-21 | Azul Zulu 21.0.13.0.3 (the October CA-only release)
`azul/zulu-openjdk:21.0.13.0.3-jre`
linux/amd64 sha256:bad3fa28d3265b6912b78f85b71d6774dab86dc83a656287c4a2e46f84ac5b3b | pass | linux/amd64 /usr/lib/jvm/zulu21-ca-amd64 runtime vulnerable vulnerable vm-library | +| liberica-21 | BellSoft Liberica 21.0.13.0.3
`bellsoft/liberica-openjre-debian:21.0.13.0.3`
linux/amd64 sha256:c5f59f695bc1ae8a588c88c60aef901ed3fc66a7d4ba1b44d2b83cc007c9cb86 | pass | linux/amd64 /usr/lib/jvm/jre-21.0.13.0.3-bellsoft-x86_64 runtime vulnerable vulnerable vm-library | +| sapmachine-21 | SapMachine 21.0.13.0.3, before 21.0.13.1
`sapmachine:21.0.13.0.3-jre-ubuntu-noble`
linux/amd64 sha256:2d8895467d35e0f0c938fc8ba94a02792d1720ce738637969e342193c7dac16f | pass | linux/amd64 /usr/lib/jvm/sapmachine-21 runtime vulnerable vulnerable vm-library | +| semeru-21 | IBM Semeru 21 (OpenJ9, not HotSpot)
`ibm-semeru-runtimes:open-21-jre`
linux/amd64 sha256:c60bbee4f364698fcf1361f3adde6b9b5faabe70b8c6cc8c179ba3f551a4616c | pass | linux/amd64 /opt/java/openjdk runtime fixed not-affected release-file | +| elasticsearch | Elasticsearch 9.5.5, which bundles its own JDK
`elasticsearch:9.5.5`
linux/amd64 sha256:4f963521f1ac85a08b27f2ac1c864697c9729ade989ed49d2a143cf5f79c222f | pass | linux/amd64 /usr/share/elasticsearch/jdk bundled-runtime vulnerable vulnerable vm-library | +| sapmachine-fixed | SapMachine 21.0.13.1 (fixed), unpacked from its release tarball | pass | linux/amd64 /opt/sapmachine runtime fixed fixed vm-library | +| build1-boundary | SYNTHETIC: SapMachine 21.0.13.1 rewritten to build +1, below the fixed build +2 | pass | linux/amd64 /opt/sapmachine runtime vulnerable vulnerable vm-library | +| jlink | jlink runtime made from Temurin 21.0.13 | pass | linux/amd64 /app/runtime runtime vulnerable vulnerable vm-library | +| bundled | Two products bundling Temurin 17.0.21; Jira's setenv.sh sets -XX:-UseBASE64Intrinsics | pass | linux/amd64 /opt/atlassian/confluence/jre bundled-runtime vulnerable vulnerable vm-library
linux/amd64 /opt/atlassian/jira/jre bundled-runtime vulnerable mitigated vm-library | +| alpine-pkg | Alpine 3.23 openjdk21-jre-headless (21.0.13.0.3_p1-r0, no fixed package) | pass | linux/amd64 /usr/lib/jvm/java-21-openjdk runtime vulnerable vulnerable vm-library | +| debian-pkg | Debian 12 openjdk-17-jre-headless (17.0.21+9-1~deb12u1, no fixed package) | pass | linux/amd64 /usr/lib/jvm/java-17-openjdk-amd64 runtime vulnerable vulnerable vm-library | +| ubuntu-pkg | Ubuntu 24.04 openjdk-25-jre-headless (25.0.5.0.3, no fixed package) | pass | linux/amd64 /usr/lib/jvm/java-25-openjdk-amd64 runtime vulnerable vulnerable vm-library | +| mitigated-env | Temurin 21.0.13 with ENV JAVA_TOOL_OPTIONS=-XX:-UseBASE64Intrinsics | pass | linux/amd64 /opt/java/openjdk runtime vulnerable mitigated vm-library | +| mitigated-useavx | Temurin 25.0.5 with ENV JDK_JAVA_OPTIONS=-XX:UseAVX=2 | pass | linux/amd64 /opt/java/openjdk runtime vulnerable mitigated vm-library | +| reenabled | JAVA_TOOL_OPTIONS mitigation undone by _JAVA_OPTIONS | pass | linux/amd64 /opt/java/openjdk runtime vulnerable vulnerable vm-library | +| tomcat | Tomcat 10.1 on Temurin 21 with CATALINA_OPTS=-XX:-UseBASE64Intrinsics (catalina.sh passes it on) | pass | linux/amd64 /opt/java/openjdk runtime vulnerable mitigated vm-library | +| tomcat-setenv | Tomcat 10.1 on Temurin 21 with -XX:-UseBASE64Intrinsics in bin/setenv.sh (catalina.sh starts JAVA_HOME's JVM) | pass | linux/amd64 /opt/java/openjdk runtime vulnerable mitigated vm-library | +| hostile | Forged and 200 KiB release files over an unfixed VM library, a text file named libjvm.so, symlink loops | pass | linux/amd64 /opt/forged runtime vulnerable vulnerable vm-library
linux/amd64 /opt/huge runtime vulnerable vulnerable vm-library | +| musl-forged | Temurin 21.0.13 on Alpine (musl) under a release file forged to say 21.0.13.1+2 | pass | linux/amd64 /opt/java/openjdk runtime vulnerable vulnerable vm-library | +| debian-pkg-fs | The debian-pkg image exported and scanned as a filesystem | pass | fs /usr/lib/jvm/java-17-openjdk-amd64 runtime vulnerable vulnerable vm-library | +| repo | Source tree, offline: Dockerfiles (with install forms: --mount, :arch, .arch, /release, =${VAR}), Compose, Kubernetes, setup-java, version files, Gradle | pass | .github/workflows/ci.yml:12 java-line vulnerable
.github/workflows/ci.yml:8 java-pin vulnerable
.java-version:1 java-pin vulnerable
.sdkmanrc:1 java-pin vulnerable
Dockerfile.forms:2 os-package vulnerable
Dockerfile.forms:3 os-package unknown
Dockerfile.forms:4 os-package vulnerable
Dockerfile.forms:5 os-package vulnerable
Dockerfile.forms:6 os-package vulnerable
Dockerfile.pkg:2 os-package vulnerable
Dockerfile:1 image-pin vulnerable
build.gradle.kts:3 java-line vulnerable
build.gradle.kts:7 image-pin vulnerable
compose.yaml:13 not-checked unknown
compose.yaml:3 image-pin vulnerable
compose.yaml:5 image-pin vulnerable
k8s/deployment.yaml:10 image-pin vulnerable | + +39/39 cases match. diff --git a/parity/java4shell.py b/parity/java4shell.py new file mode 100644 index 0000000..df1e882 --- /dev/null +++ b/parity/java4shell.py @@ -0,0 +1,143 @@ +#!/usr/bin/env python3 +"""Check the java4shell detector against Java runtimes whose status is known. + +usage: python3 parity/java4shell.py path/to/earlylight [case ...] + +With case names, only those run and only their rows of the results file +change. + +Needs Docker and registry access. Cases are in parity/java4shell/cases.json: +published images (scanned from the registry by tag, the digest recorded), +images built from parity/java4shell/*.Dockerfile (scanned from `docker save` +archives), one built image exported and scanned as a filesystem, and a +source tree scanned offline. Each case's findings are compared with the +expected [platform, path, kind, verdict, status, version_from] rows, which come from +FACTS.md (fixed releases, the release that introduced the decoder, OpenJ9, +architecture), not from earlier runs. Writes parity/JAVA4SHELL-RESULTS.md. +""" +import json +import os +import subprocess +import sys +import tarfile +import tempfile + +HERE = os.path.dirname(os.path.abspath(__file__)) +CASES = os.path.join(HERE, "java4shell") + + +def scan(tool, args): + # Flags go before the target: the command line stops reading flags there. + out = subprocess.run([tool] + args[:2] + ["--detector", "java4shell", "--format", "json", "--show-cleared"] + args[2:], + capture_output=True, text=True) + try: + return json.loads(out.stdout) + except json.JSONDecodeError: + return {"targets": [{"error": (out.stderr or out.stdout).strip()[:300]}]} + + +def rows(report, repo=False): + got, digests = [], [] + for t in report.get("targets", []): + if t.get("error"): + got.append(["error", t["error"][:120], "", "", "", ""]) + if t.get("digest"): + digests.append(t.get("platform", "") + " " + t["digest"]) + for f in t.get("findings", []): + if repo: + got.append([f["path"] + ":" + str(f.get("line", 0)), f["kind"], f["verdict"]]) + else: + facts = f.get("facts", {}) + got.append([t.get("platform", ""), f["path"], f["kind"], f["verdict"], facts.get("status", ""), facts.get("version_from", "")]) + return got, digests + + +def safe_extract(tar, dest): + # The exported image is ours, but keep members inside dest anyway. + for m in tar.getmembers(): + p = os.path.realpath(os.path.join(dest, m.name)) + if not p.startswith(os.path.realpath(dest) + os.sep) or m.isdev(): + continue + try: + tar.extract(m, dest, set_attrs=False, filter="fully_trusted") + except (OSError, tarfile.TarError): + pass + + +def run_case(tool, c, tmp): + kind = c["mode"] + if kind == "registry": + args = ["scan", "image"] + if c.get("platform"): + args += ["--platform", c["platform"]] + return rows(scan(tool, args + [c["ref"]])) + if kind == "repo": + return rows(scan(tool, ["scan", "repo", "--offline", os.path.join(CASES, c["dir"])]), repo=True) + tag = "earlylight-parity-java4shell:" + c["name"] + subprocess.run(["docker", "build", "-q", "-t", tag, "-f", os.path.join(CASES, c["dockerfile"]), CASES], + check=True, stdout=subprocess.DEVNULL) + if kind == "build": + tar = os.path.join(tmp, c["name"] + ".tar") + subprocess.run(["docker", "save", tag, "-o", tar], check=True) + return rows(scan(tool, ["scan", "image", "docker-archive:" + tar])) + # kind == "fs": export the container filesystem and scan it as a system root. + cid = subprocess.run(["docker", "create", tag], check=True, capture_output=True, text=True).stdout.strip() + try: + tar = os.path.join(tmp, c["name"] + "-fs.tar") + subprocess.run(["docker", "export", cid, "-o", tar], check=True) + finally: + subprocess.run(["docker", "rm", cid], check=True, stdout=subprocess.DEVNULL) + root = os.path.join(tmp, c["name"] + "-root") + os.makedirs(root) + with tarfile.open(tar) as t: + safe_extract(t, root) + got, _ = rows(scan(tool, ["scan", "fs", "--as-root", root])) + return [["fs"] + g[1:] for g in got], [] + + +def main(): + if len(sys.argv) < 2: + sys.exit(__doc__) + tool = os.path.abspath(sys.argv[1]) + only = set(sys.argv[2:]) + cases = json.load(open(os.path.join(CASES, "cases.json"))) + results, failed = [], 0 + with tempfile.TemporaryDirectory() as tmp: + for c in cases: + if only and c["name"] not in only: + continue + got, digests = run_case(tool, c, tmp) + want = c["expected"] + ok = sorted(got) == sorted(want) + failed += not ok + results.append((c, ok, got, want, digests)) + print(("pass " if ok else "FAIL ") + c["name"], flush=True) + # Named cases update their own rows and keep the others' (a registry's + # pull limit can stop a full run part way). + path = os.path.join(HERE, "JAVA4SHELL-RESULTS.md") + lines = {} + if only and os.path.exists(path): + for l in open(path): + if l.startswith("| ") and not l.startswith("| Case") and not l.startswith("| ---"): + lines[l.split(" | ")[0][2:]] = l + for c, ok, got, want, digests in results: + cell = "
".join(" ".join(str(x) for x in g if x) for g in sorted(got)) or "none" + what = c["what"] + if c.get("ref"): + what += "
`" + c["ref"] + "`" + "".join("
" + d for d in digests) + lines[c["name"]] = f"| {c['name']} | {what} | {'pass' if ok else 'FAIL (want ' + json.dumps(want) + ')'} | {cell} |\n" + order = [c["name"] for c in cases if c["name"] in lines] + passed = sum(1 for n in order if lines[n].split(" | ")[2] == "pass") + with open(path, "w") as f: + f.write("# Java4Shell fixture results\n\nGenerated by `parity/java4shell.py`. Each row's expected findings come from " + "[FACTS.md](../detectors/java4shell/FACTS.md). Registry images are scanned by tag; the digest scanned is listed.\n\n" + "| Case | What | Result | Findings (platform, path, kind, verdict, status, version from) |\n| --- | --- | --- | --- |\n") + for n in order: + f.write(lines[n]) + f.write(f"\n{passed}/{len(order)} cases match.\n") + print(f"{passed}/{len(order)} cases match") + sys.exit(1 if failed else 0) + + +if __name__ == "__main__": + main() diff --git a/parity/java4shell/alpine-pkg.Dockerfile b/parity/java4shell/alpine-pkg.Dockerfile new file mode 100644 index 0000000..8f11322 --- /dev/null +++ b/parity/java4shell/alpine-pkg.Dockerfile @@ -0,0 +1,3 @@ +# Alpine's openjdk21 package (21.0.13.0.3_p1-r0 in 3.23 on 2026-11-17: no fix yet). +FROM alpine:3.23 +RUN apk add --no-cache openjdk21-jre-headless diff --git a/parity/java4shell/build1-boundary.Dockerfile b/parity/java4shell/build1-boundary.Dockerfile new file mode 100644 index 0000000..37461a0 --- /dev/null +++ b/parity/java4shell/build1-boundary.Dockerfile @@ -0,0 +1,10 @@ +# SYNTHETIC boundary case: the fixed SapMachine 21.0.13.1+2 with the +# version line in its VM library and release file rewritten to build +1, +# which upstream (jdk-21.0.13.1+1) did not have the fix. No +1 binary was +# published; this checks that a build below the fixed build is not +# judged fixed. +FROM debian:12-slim +ADD --checksum=sha256:6cb4df182dc9efd3cd37722517832e402cd1beed4f7e3d4d9b58abeb3e949ae9 \ + https://github.com/SAP/SapMachine/releases/download/sapmachine-21.0.13.1/sapmachine-jre-21.0.13.1_linux-x64_bin.tar.gz /tmp/sap.tgz +RUN mkdir -p /opt/sapmachine && tar xzf /tmp/sap.tgz -C /opt/sapmachine --strip-components=1 && rm /tmp/sap.tgz \ + && sed -i 's/21\.0\.13\.1+2-LTS/21.0.13.1+1-LTS/g' /opt/sapmachine/lib/server/libjvm.so /opt/sapmachine/release diff --git a/parity/java4shell/bundled.Dockerfile b/parity/java4shell/bundled.Dockerfile new file mode 100644 index 0000000..3b7278f --- /dev/null +++ b/parity/java4shell/bundled.Dockerfile @@ -0,0 +1,8 @@ +# A product that ships its own runtime (the Atlassian layout), with a +# setenv.sh that sets the mitigation for Jira only, next to a Confluence +# without it. +FROM eclipse-temurin:17.0.21_9-jre AS jre +FROM debian:12-slim +COPY --from=jre /opt/java/openjdk /opt/atlassian/jira/jre +COPY --from=jre /opt/java/openjdk /opt/atlassian/confluence/jre +RUN mkdir -p /opt/atlassian/jira/bin && echo 'JVM_SUPPORT_RECOMMENDED_ARGS="-XX:-UseBASE64Intrinsics"' > /opt/atlassian/jira/bin/setenv.sh diff --git a/parity/java4shell/cases.json b/parity/java4shell/cases.json new file mode 100644 index 0000000..7c331fa --- /dev/null +++ b/parity/java4shell/cases.json @@ -0,0 +1,761 @@ +[ + { + "name": "temurin-8", + "what": "Temurin 8: no Base64 decode intrinsic", + "mode": "registry", + "ref": "eclipse-temurin:8u512-b09-jre", + "expected": [ + [ + "linux/amd64", + "/opt/java/openjdk", + "runtime", + "fixed", + "not-affected", + "vm-library" + ] + ] + }, + { + "name": "temurin-11", + "what": "Temurin 11: no Base64 decode intrinsic", + "mode": "registry", + "ref": "eclipse-temurin:11.0.33_10-jre", + "expected": [ + [ + "linux/amd64", + "/opt/java/openjdk", + "runtime", + "fixed", + "not-affected", + "vm-library" + ] + ] + }, + { + "name": "temurin-17.0.5", + "what": "Temurin 17.0.5, the last 17 update before the decoder (8268276 came in 17.0.6)", + "mode": "registry", + "ref": "eclipse-temurin:17.0.5_8-jre", + "expected": [ + [ + "linux/amd64", + "/opt/java/openjdk", + "runtime", + "fixed", + "not-affected", + "vm-library" + ] + ] + }, + { + "name": "temurin-17.0.6", + "what": "Temurin 17.0.6, the first 17 update with the decoder", + "mode": "registry", + "ref": "eclipse-temurin:17.0.6_10-jre", + "expected": [ + [ + "linux/amd64", + "/opt/java/openjdk", + "runtime", + "vulnerable", + "vulnerable", + "vm-library" + ] + ] + }, + { + "name": "temurin-17.0.21-alpine", + "what": "Temurin 17.0.21 on Alpine (musl), before 17.0.21.1", + "mode": "registry", + "ref": "eclipse-temurin:17.0.21_9-jre-alpine", + "expected": [ + [ + "linux/amd64", + "/opt/java/openjdk", + "runtime", + "vulnerable", + "vulnerable", + "vm-library" + ] + ] + }, + { + "name": "temurin-21-alpine", + "what": "Temurin 21.0.13 on Alpine: a musl VM library (linux-amd64-musl)", + "mode": "registry", + "ref": "eclipse-temurin:21.0.13_9-jre-alpine", + "expected": [ + [ + "linux/amd64", + "/opt/java/openjdk", + "runtime", + "vulnerable", + "vulnerable", + "vm-library" + ] + ] + }, + { + "name": "temurin-18", + "what": "Temurin 18.0.2.1: end of life, no fix", + "mode": "registry", + "ref": "eclipse-temurin:18.0.2.1_1-jre", + "expected": [ + [ + "linux/amd64", + "/opt/java/openjdk", + "runtime", + "vulnerable", + "vulnerable", + "vm-library" + ] + ] + }, + { + "name": "temurin-20", + "what": "Temurin 20.0.2: end of life, no fix", + "mode": "registry", + "ref": "eclipse-temurin:20.0.2_9-jre", + "expected": [ + [ + "linux/amd64", + "/opt/java/openjdk", + "runtime", + "vulnerable", + "vulnerable", + "vm-library" + ] + ] + }, + { + "name": "temurin-21", + "what": "Temurin 21.0.13, before 21.0.13.1", + "mode": "registry", + "ref": "eclipse-temurin:21.0.13_9-jre", + "expected": [ + [ + "linux/amd64", + "/opt/java/openjdk", + "runtime", + "vulnerable", + "vulnerable", + "vm-library" + ] + ] + }, + { + "name": "temurin-21-arm64", + "what": "Temurin 21.0.13 for linux/arm64: only x86-64 has the decoder", + "mode": "registry", + "ref": "eclipse-temurin:21.0.13_9-jre", + "expected": [ + [ + "linux/arm64/v8", + "/opt/java/openjdk", + "runtime", + "fixed", + "not-affected", + "vm-library" + ] + ], + "platform": "linux/arm64" + }, + { + "name": "temurin-21-all", + "what": "Temurin 21.0.13, every platform of the multi-arch image (Linux and the two Windows Server images, whose layers name C:\\ as /Files)", + "mode": "registry", + "ref": "eclipse-temurin:21.0.13_9-jre", + "expected": [ + [ + "linux/amd64", + "/opt/java/openjdk", + "runtime", + "vulnerable", + "vulnerable", + "vm-library" + ], + [ + "linux/arm64/v8", + "/opt/java/openjdk", + "runtime", + "fixed", + "not-affected", + "vm-library" + ], + [ + "linux/ppc64le", + "/opt/java/openjdk", + "runtime", + "fixed", + "not-affected", + "vm-library" + ], + [ + "linux/s390x", + "/opt/java/openjdk", + "runtime", + "fixed", + "not-affected", + "vm-library" + ], + [ + "windows/amd64", + "/Files/openjdk-21", + "runtime", + "vulnerable", + "vulnerable", + "vm-library" + ], + [ + "windows/amd64", + "/Files/openjdk-21", + "runtime", + "vulnerable", + "vulnerable", + "vm-library" + ] + ], + "platform": "all" + }, + { + "name": "temurin-21-ubi9", + "what": "Temurin 21.0.13 on UBI 9 minimal", + "mode": "registry", + "ref": "eclipse-temurin:21.0.13_9-jre-ubi9-minimal", + "expected": [ + [ + "linux/amd64", + "/opt/java/openjdk", + "runtime", + "vulnerable", + "vulnerable", + "vm-library" + ] + ] + }, + { + "name": "temurin-25", + "what": "Temurin 25.0.5, before 25.0.5.1", + "mode": "registry", + "ref": "eclipse-temurin:25.0.5_8-jre", + "expected": [ + [ + "linux/amd64", + "/opt/java/openjdk", + "runtime", + "vulnerable", + "vulnerable", + "vm-library" + ] + ] + }, + { + "name": "temurin-27", + "what": "Temurin 27.0.1.0.2, before 27.0.1.1", + "mode": "registry", + "ref": "eclipse-temurin:27.0.1.0-jre", + "expected": [ + [ + "linux/amd64", + "/opt/java/openjdk", + "runtime", + "vulnerable", + "vulnerable", + "vm-library" + ] + ] + }, + { + "name": "corretto-21-al2023-unfixed", + "what": "Amazon Corretto 21.0.13.0.11, an rpm on Amazon Linux 2023 (the amazoncorretto:21.0.13-al2023-headless image of 2026-11-17, pinned by digest)", + "mode": "registry", + "ref": "amazoncorretto@sha256:21a9456bd7fee7045c545bf089e63ffc69ceb7d594b3c5bfb25bace199d3f566", + "expected": [ + [ + "linux/amd64", + "/usr/lib/jvm/java-21-amazon-corretto.x86_64", + "runtime", + "vulnerable", + "vulnerable", + "vm-library" + ] + ] + }, + { + "name": "corretto-21-al2023", + "what": "amazoncorretto:21.0.13-al2023-headless as republished on 2026-11-18 with Corretto 21.0.13.1.2 (the tag 21.0.13 moves to the out-of-band release)", + "mode": "registry", + "ref": "amazoncorretto:21.0.13-al2023-headless", + "expected": [ + [ + "linux/amd64", + "/usr/lib/jvm/java-21-amazon-corretto.x86_64", + "runtime", + "fixed", + "fixed", + "vm-library" + ] + ] + }, + { + "name": "corretto-17-alpine-unfixed", + "what": "Amazon Corretto 17.0.21 on Alpine (the amazoncorretto:17.0.21-alpine3.22 image of 2026-11-17, pinned by digest)", + "mode": "registry", + "ref": "amazoncorretto@sha256:92cdff306e26329894ef0fab79f8d120ac0acc08f3b675206b66a7da85316043", + "expected": [ + [ + "linux/amd64", + "/usr/lib/jvm/java-17-amazon-corretto", + "runtime", + "vulnerable", + "vulnerable", + "vm-library" + ] + ] + }, + { + "name": "corretto-17-alpine", + "what": "amazoncorretto:17.0.21-alpine3.22 as republished on 2026-11-18 with Corretto 17.0.21.1.2", + "mode": "registry", + "ref": "amazoncorretto:17.0.21-alpine3.22", + "expected": [ + [ + "linux/amd64", + "/usr/lib/jvm/java-17-amazon-corretto", + "runtime", + "fixed", + "fixed", + "vm-library" + ] + ] + }, + { + "name": "zulu-21", + "what": "Azul Zulu 21.0.13.0.3 (the October CA-only release)", + "mode": "registry", + "ref": "azul/zulu-openjdk:21.0.13.0.3-jre", + "expected": [ + [ + "linux/amd64", + "/usr/lib/jvm/zulu21-ca-amd64", + "runtime", + "vulnerable", + "vulnerable", + "vm-library" + ] + ] + }, + { + "name": "liberica-21", + "what": "BellSoft Liberica 21.0.13.0.3", + "mode": "registry", + "ref": "bellsoft/liberica-openjre-debian:21.0.13.0.3", + "expected": [ + [ + "linux/amd64", + "/usr/lib/jvm/jre-21.0.13.0.3-bellsoft-x86_64", + "runtime", + "vulnerable", + "vulnerable", + "vm-library" + ] + ] + }, + { + "name": "sapmachine-21", + "what": "SapMachine 21.0.13.0.3, before 21.0.13.1", + "mode": "registry", + "ref": "sapmachine:21.0.13.0.3-jre-ubuntu-noble", + "expected": [ + [ + "linux/amd64", + "/usr/lib/jvm/sapmachine-21", + "runtime", + "vulnerable", + "vulnerable", + "vm-library" + ] + ] + }, + { + "name": "semeru-21", + "what": "IBM Semeru 21 (OpenJ9, not HotSpot)", + "mode": "registry", + "ref": "ibm-semeru-runtimes:open-21-jre", + "expected": [ + [ + "linux/amd64", + "/opt/java/openjdk", + "runtime", + "fixed", + "not-affected", + "release-file" + ] + ] + }, + { + "name": "elasticsearch", + "what": "Elasticsearch 9.5.5, which bundles its own JDK", + "mode": "registry", + "ref": "elasticsearch:9.5.5", + "expected": [ + [ + "linux/amd64", + "/usr/share/elasticsearch/jdk", + "bundled-runtime", + "vulnerable", + "vulnerable", + "vm-library" + ] + ] + }, + { + "name": "sapmachine-fixed", + "what": "SapMachine 21.0.13.1 (fixed), unpacked from its release tarball", + "mode": "build", + "dockerfile": "sapmachine-fixed.Dockerfile", + "expected": [ + [ + "linux/amd64", + "/opt/sapmachine", + "runtime", + "fixed", + "fixed", + "vm-library" + ] + ] + }, + { + "name": "build1-boundary", + "what": "SYNTHETIC: SapMachine 21.0.13.1 rewritten to build +1, below the fixed build +2", + "mode": "build", + "dockerfile": "build1-boundary.Dockerfile", + "expected": [ + [ + "linux/amd64", + "/opt/sapmachine", + "runtime", + "vulnerable", + "vulnerable", + "vm-library" + ] + ] + }, + { + "name": "jlink", + "what": "jlink runtime made from Temurin 21.0.13", + "mode": "build", + "dockerfile": "jlink.Dockerfile", + "expected": [ + [ + "linux/amd64", + "/app/runtime", + "runtime", + "vulnerable", + "vulnerable", + "vm-library" + ] + ] + }, + { + "name": "bundled", + "what": "Two products bundling Temurin 17.0.21; Jira's setenv.sh sets -XX:-UseBASE64Intrinsics", + "mode": "build", + "dockerfile": "bundled.Dockerfile", + "expected": [ + [ + "linux/amd64", + "/opt/atlassian/jira/jre", + "bundled-runtime", + "vulnerable", + "mitigated", + "vm-library" + ], + [ + "linux/amd64", + "/opt/atlassian/confluence/jre", + "bundled-runtime", + "vulnerable", + "vulnerable", + "vm-library" + ] + ] + }, + { + "name": "alpine-pkg", + "what": "Alpine 3.23 openjdk21-jre-headless (21.0.13.0.3_p1-r0, no fixed package)", + "mode": "build", + "dockerfile": "alpine-pkg.Dockerfile", + "expected": [ + [ + "linux/amd64", + "/usr/lib/jvm/java-21-openjdk", + "runtime", + "vulnerable", + "vulnerable", + "vm-library" + ] + ] + }, + { + "name": "debian-pkg", + "what": "Debian 12 openjdk-17-jre-headless (17.0.21+9-1~deb12u1, no fixed package)", + "mode": "build", + "dockerfile": "debian-pkg.Dockerfile", + "expected": [ + [ + "linux/amd64", + "/usr/lib/jvm/java-17-openjdk-amd64", + "runtime", + "vulnerable", + "vulnerable", + "vm-library" + ] + ] + }, + { + "name": "ubuntu-pkg", + "what": "Ubuntu 24.04 openjdk-25-jre-headless (25.0.5.0.3, no fixed package)", + "mode": "build", + "dockerfile": "ubuntu-pkg.Dockerfile", + "expected": [ + [ + "linux/amd64", + "/usr/lib/jvm/java-25-openjdk-amd64", + "runtime", + "vulnerable", + "vulnerable", + "vm-library" + ] + ] + }, + { + "name": "mitigated-env", + "what": "Temurin 21.0.13 with ENV JAVA_TOOL_OPTIONS=-XX:-UseBASE64Intrinsics", + "mode": "build", + "dockerfile": "mitigated-env.Dockerfile", + "expected": [ + [ + "linux/amd64", + "/opt/java/openjdk", + "runtime", + "vulnerable", + "mitigated", + "vm-library" + ] + ] + }, + { + "name": "mitigated-useavx", + "what": "Temurin 25.0.5 with ENV JDK_JAVA_OPTIONS=-XX:UseAVX=2", + "mode": "build", + "dockerfile": "mitigated-useavx.Dockerfile", + "expected": [ + [ + "linux/amd64", + "/opt/java/openjdk", + "runtime", + "vulnerable", + "mitigated", + "vm-library" + ] + ] + }, + { + "name": "reenabled", + "what": "JAVA_TOOL_OPTIONS mitigation undone by _JAVA_OPTIONS", + "mode": "build", + "dockerfile": "reenabled.Dockerfile", + "expected": [ + [ + "linux/amd64", + "/opt/java/openjdk", + "runtime", + "vulnerable", + "vulnerable", + "vm-library" + ] + ] + }, + { + "name": "tomcat", + "what": "Tomcat 10.1 on Temurin 21 with CATALINA_OPTS=-XX:-UseBASE64Intrinsics (catalina.sh passes it on)", + "mode": "build", + "dockerfile": "tomcat.Dockerfile", + "expected": [ + [ + "linux/amd64", + "/opt/java/openjdk", + "runtime", + "vulnerable", + "mitigated", + "vm-library" + ] + ] + }, + { + "name": "tomcat-setenv", + "what": "Tomcat 10.1 on Temurin 21 with -XX:-UseBASE64Intrinsics in bin/setenv.sh (catalina.sh starts JAVA_HOME's JVM)", + "mode": "build", + "dockerfile": "tomcat-setenv.Dockerfile", + "expected": [ + [ + "linux/amd64", + "/opt/java/openjdk", + "runtime", + "vulnerable", + "mitigated", + "vm-library" + ] + ] + }, + { + "name": "hostile", + "what": "Forged and 200 KiB release files over an unfixed VM library, a text file named libjvm.so, symlink loops", + "mode": "build", + "dockerfile": "hostile.Dockerfile", + "expected": [ + [ + "linux/amd64", + "/opt/forged", + "runtime", + "vulnerable", + "vulnerable", + "vm-library" + ], + [ + "linux/amd64", + "/opt/huge", + "runtime", + "vulnerable", + "vulnerable", + "vm-library" + ] + ] + }, + { + "name": "musl-forged", + "what": "Temurin 21.0.13 on Alpine (musl) under a release file forged to say 21.0.13.1+2", + "mode": "build", + "dockerfile": "musl-forged.Dockerfile", + "expected": [ + [ + "linux/amd64", + "/opt/java/openjdk", + "runtime", + "vulnerable", + "vulnerable", + "vm-library" + ] + ] + }, + { + "name": "debian-pkg-fs", + "what": "The debian-pkg image exported and scanned as a filesystem", + "mode": "fs", + "dockerfile": "debian-pkg.Dockerfile", + "expected": [ + [ + "fs", + "/usr/lib/jvm/java-17-openjdk-amd64", + "runtime", + "vulnerable", + "vulnerable", + "vm-library" + ] + ] + }, + { + "name": "repo", + "what": "Source tree, offline: Dockerfiles (with install forms: --mount, :arch, .arch, /release, =${VAR}), Compose, Kubernetes, setup-java, version files, Gradle", + "mode": "repo", + "dir": "repo", + "expected": [ + [ + ".github/workflows/ci.yml:8", + "java-pin", + "vulnerable" + ], + [ + ".github/workflows/ci.yml:12", + "java-line", + "vulnerable" + ], + [ + ".java-version:1", + "java-pin", + "vulnerable" + ], + [ + ".sdkmanrc:1", + "java-pin", + "vulnerable" + ], + [ + "Dockerfile:1", + "image-pin", + "vulnerable" + ], + [ + "Dockerfile.pkg:2", + "os-package", + "vulnerable" + ], + [ + "build.gradle.kts:3", + "java-line", + "vulnerable" + ], + [ + "build.gradle.kts:7", + "image-pin", + "vulnerable" + ], + [ + "compose.yaml:3", + "image-pin", + "vulnerable" + ], + [ + "compose.yaml:5", + "image-pin", + "vulnerable" + ], + [ + "compose.yaml:13", + "not-checked", + "unknown" + ], + [ + "k8s/deployment.yaml:10", + "image-pin", + "vulnerable" + ], + [ + "Dockerfile.forms:2", + "os-package", + "vulnerable" + ], + [ + "Dockerfile.forms:3", + "os-package", + "unknown" + ], + [ + "Dockerfile.forms:4", + "os-package", + "vulnerable" + ], + [ + "Dockerfile.forms:5", + "os-package", + "vulnerable" + ], + [ + "Dockerfile.forms:6", + "os-package", + "vulnerable" + ] + ] + } +] \ No newline at end of file diff --git a/parity/java4shell/debian-pkg.Dockerfile b/parity/java4shell/debian-pkg.Dockerfile new file mode 100644 index 0000000..5b0f1ff --- /dev/null +++ b/parity/java4shell/debian-pkg.Dockerfile @@ -0,0 +1,3 @@ +# Debian 12's openjdk-17 package (17.0.21+9-1~deb12u1 on 2026-11-17: no fix yet). +FROM debian:12-slim +RUN apt-get update && apt-get install -y --no-install-recommends openjdk-17-jre-headless && rm -rf /var/lib/apt/lists/* diff --git a/parity/java4shell/hostile.Dockerfile b/parity/java4shell/hostile.Dockerfile new file mode 100644 index 0000000..f4580ab --- /dev/null +++ b/parity/java4shell/hostile.Dockerfile @@ -0,0 +1,12 @@ +# Hostile inputs: a real unfixed VM library under a forged release file that +# claims the fix, another under a 200 KiB release file, a text file named +# libjvm.so, symbolic link loops and a self-referencing directory link. +FROM eclipse-temurin:21.0.13_9-jre AS jre +FROM debian:12-slim +COPY --from=jre /opt/java/openjdk/lib/server/libjvm.so /opt/forged/lib/server/libjvm.so +COPY --from=jre /opt/java/openjdk/lib/server/libjvm.so /opt/huge/lib/server/libjvm.so +RUN printf 'JAVA_VERSION="21.0.13.1"\nJAVA_RUNTIME_VERSION="21.0.13.1+2-LTS"\nIMPLEMENTOR="Forged"\nOS_ARCH="aarch64"\n' > /opt/forged/release \ + && (printf 'JAVA_VERSION="21.0.13.1"\n'; head -c 204800 /dev/zero | tr '\0' '#') > /opt/huge/release \ + && mkdir -p /opt/fake/lib/server && echo 'not a library' > /opt/fake/lib/server/libjvm.so \ + && ln -s loop2 /opt/loop1 && ln -s loop1 /opt/loop2 && ln -s . /opt/forged/lib/self \ + && ln -s /opt/forged /opt/forged/lib/server/up diff --git a/parity/java4shell/jlink.Dockerfile b/parity/java4shell/jlink.Dockerfile new file mode 100644 index 0000000..98c04ef --- /dev/null +++ b/parity/java4shell/jlink.Dockerfile @@ -0,0 +1,6 @@ +# A custom runtime made with jlink from Temurin 21.0.13 (unfixed): its +# release file keeps only JAVA_VERSION and MODULES. +FROM eclipse-temurin:21.0.13_9-jdk AS build +RUN jlink --add-modules java.base,java.logging --strip-debug --no-header-files --no-man-pages --output /rt +FROM debian:12-slim +COPY --from=build /rt /app/runtime diff --git a/parity/java4shell/mitigated-env.Dockerfile b/parity/java4shell/mitigated-env.Dockerfile new file mode 100644 index 0000000..df17e9f --- /dev/null +++ b/parity/java4shell/mitigated-env.Dockerfile @@ -0,0 +1,3 @@ +# Unfixed Temurin 21 with the mitigation in the image's JAVA_TOOL_OPTIONS. +FROM eclipse-temurin:21.0.13_9-jre +ENV JAVA_TOOL_OPTIONS="-Xss1m -XX:-UseBASE64Intrinsics" diff --git a/parity/java4shell/mitigated-useavx.Dockerfile b/parity/java4shell/mitigated-useavx.Dockerfile new file mode 100644 index 0000000..98dc2de --- /dev/null +++ b/parity/java4shell/mitigated-useavx.Dockerfile @@ -0,0 +1,3 @@ +# Unfixed Temurin 25 with AVX-512 turned off through JDK_JAVA_OPTIONS. +FROM eclipse-temurin:25.0.5_8-jre +ENV JDK_JAVA_OPTIONS="-XX:UseAVX=2" diff --git a/parity/java4shell/musl-forged.Dockerfile b/parity/java4shell/musl-forged.Dockerfile new file mode 100644 index 0000000..0314877 --- /dev/null +++ b/parity/java4shell/musl-forged.Dockerfile @@ -0,0 +1,5 @@ +# Temurin 21.0.13 on Alpine (musl: its VM library names linux-amd64-musl) +# under a release file forged to claim the fixed 21.0.13.1: the VM +# library's own version decides. +FROM eclipse-temurin:21.0.13_9-jre-alpine +RUN sed -i 's/^JAVA_VERSION=.*/JAVA_VERSION="21.0.13.1"/; s/^JAVA_RUNTIME_VERSION=.*/JAVA_RUNTIME_VERSION="21.0.13.1+2-LTS"/' /opt/java/openjdk/release diff --git a/parity/java4shell/reenabled.Dockerfile b/parity/java4shell/reenabled.Dockerfile new file mode 100644 index 0000000..97381d1 --- /dev/null +++ b/parity/java4shell/reenabled.Dockerfile @@ -0,0 +1,3 @@ +# The mitigation in JAVA_TOOL_OPTIONS, undone by _JAVA_OPTIONS (applied last). +FROM eclipse-temurin:21.0.13_9-jre +ENV JAVA_TOOL_OPTIONS="-XX:-UseBASE64Intrinsics" _JAVA_OPTIONS="-XX:+UseBASE64Intrinsics" diff --git a/parity/java4shell/repo/.github/workflows/ci.yml b/parity/java4shell/repo/.github/workflows/ci.yml new file mode 100644 index 0000000..e23b6d6 --- /dev/null +++ b/parity/java4shell/repo/.github/workflows/ci.yml @@ -0,0 +1,12 @@ +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/setup-java@v4 + with: + distribution: corretto + java-version: "21.0.5" + - uses: actions/setup-java@v4 + with: + distribution: temurin + java-version: "24" diff --git a/parity/java4shell/repo/.java-version b/parity/java4shell/repo/.java-version new file mode 100644 index 0000000..baf3ee0 --- /dev/null +++ b/parity/java4shell/repo/.java-version @@ -0,0 +1 @@ +21.0.5 diff --git a/parity/java4shell/repo/.sdkmanrc b/parity/java4shell/repo/.sdkmanrc new file mode 100644 index 0000000..ae4f91d --- /dev/null +++ b/parity/java4shell/repo/.sdkmanrc @@ -0,0 +1 @@ +java=25.0.1-tem diff --git a/parity/java4shell/repo/.tool-versions b/parity/java4shell/repo/.tool-versions new file mode 100644 index 0000000..c7b1771 --- /dev/null +++ b/parity/java4shell/repo/.tool-versions @@ -0,0 +1 @@ +java corretto-21.0.13.1.2 diff --git a/parity/java4shell/repo/Dockerfile b/parity/java4shell/repo/Dockerfile new file mode 100644 index 0000000..63f8afc --- /dev/null +++ b/parity/java4shell/repo/Dockerfile @@ -0,0 +1,3 @@ +FROM eclipse-temurin:21.0.13_9-jre-alpine +COPY app.jar /app.jar +CMD ["java", "-jar", "/app.jar"] diff --git a/parity/java4shell/repo/Dockerfile.forms b/parity/java4shell/repo/Dockerfile.forms new file mode 100644 index 0000000..316cf08 --- /dev/null +++ b/parity/java4shell/repo/Dockerfile.forms @@ -0,0 +1,6 @@ +FROM debian:12 +RUN --mount=type=cache,target=/var/cache/apt apt-get install -y openjdk-17-jre-headless +RUN apt-get install -y openjdk-17-jre-headless=${JDK_PKG} +RUN apt-get install -y openjdk-17-jre-headless:amd64 +RUN apt-get install -y openjdk-17-jdk/bookworm-backports +RUN dnf install -y java-17-openjdk-headless.x86_64 diff --git a/parity/java4shell/repo/Dockerfile.pkg b/parity/java4shell/repo/Dockerfile.pkg new file mode 100644 index 0000000..635701f --- /dev/null +++ b/parity/java4shell/repo/Dockerfile.pkg @@ -0,0 +1,3 @@ +FROM ubuntu:24.04 +RUN apt-get update && apt-get install -y --no-install-recommends openjdk-21-jre-headless +ENV JAVA_TOOL_OPTIONS="-XX:-UseBASE64Intrinsics" diff --git a/parity/java4shell/repo/build.gradle.kts b/parity/java4shell/repo/build.gradle.kts new file mode 100644 index 0000000..10dd10b --- /dev/null +++ b/parity/java4shell/repo/build.gradle.kts @@ -0,0 +1,8 @@ +java { + toolchain { + languageVersion = JavaLanguageVersion.of(23) + } +} +jib { + from { image = "eclipse-temurin:17.0.21_9-jre" } +} diff --git a/parity/java4shell/repo/compose.yaml b/parity/java4shell/repo/compose.yaml new file mode 100644 index 0000000..735bb57 --- /dev/null +++ b/parity/java4shell/repo/compose.yaml @@ -0,0 +1,13 @@ +services: + api: + image: amazoncorretto:21.0.12-al2023 + worker: + image: amazoncorretto:21.0.12-al2023 + environment: + JAVA_TOOL_OPTIONS: "-XX:UseAVX=2" + old: + image: eclipse-temurin:17.0.5_8-jre + j9: + image: ibm-semeru-runtimes:open-21-jre + latest: + image: eclipse-temurin:21-jre diff --git a/parity/java4shell/repo/k8s/deployment.yaml b/parity/java4shell/repo/k8s/deployment.yaml new file mode 100644 index 0000000..f0dd9b8 --- /dev/null +++ b/parity/java4shell/repo/k8s/deployment.yaml @@ -0,0 +1,10 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: app +spec: + template: + spec: + containers: + - name: app + image: bellsoft/liberica-openjre-debian:21.0.13-9 diff --git a/parity/java4shell/sapmachine-fixed.Dockerfile b/parity/java4shell/sapmachine-fixed.Dockerfile new file mode 100644 index 0000000..ec84ca3 --- /dev/null +++ b/parity/java4shell/sapmachine-fixed.Dockerfile @@ -0,0 +1,6 @@ +# SapMachine 21.0.13.1, the first 21 release with the Java4Shell fix +# (released 2026-11-17), unpacked from its GitHub release tarball. +FROM debian:12-slim +ADD --checksum=sha256:6cb4df182dc9efd3cd37722517832e402cd1beed4f7e3d4d9b58abeb3e949ae9 \ + https://github.com/SAP/SapMachine/releases/download/sapmachine-21.0.13.1/sapmachine-jre-21.0.13.1_linux-x64_bin.tar.gz /tmp/sap.tgz +RUN mkdir -p /opt/sapmachine && tar xzf /tmp/sap.tgz -C /opt/sapmachine --strip-components=1 && rm /tmp/sap.tgz diff --git a/parity/java4shell/tomcat-setenv.Dockerfile b/parity/java4shell/tomcat-setenv.Dockerfile new file mode 100644 index 0000000..406304f --- /dev/null +++ b/parity/java4shell/tomcat-setenv.Dockerfile @@ -0,0 +1,4 @@ +# Tomcat's own image with the mitigation in bin/setenv.sh, which catalina.sh +# reads before it starts the JVM that JAVA_HOME names. +FROM tomcat:10.1-jre21-temurin +RUN echo 'CATALINA_OPTS="$CATALINA_OPTS -XX:-UseBASE64Intrinsics"' > /usr/local/tomcat/bin/setenv.sh diff --git a/parity/java4shell/tomcat.Dockerfile b/parity/java4shell/tomcat.Dockerfile new file mode 100644 index 0000000..cb95f34 --- /dev/null +++ b/parity/java4shell/tomcat.Dockerfile @@ -0,0 +1,3 @@ +# Tomcat's own image runs catalina.sh, which passes CATALINA_OPTS on. +FROM tomcat:10.1-jre21-temurin +ENV CATALINA_OPTS="-XX:-UseBASE64Intrinsics" diff --git a/parity/java4shell/ubuntu-pkg.Dockerfile b/parity/java4shell/ubuntu-pkg.Dockerfile new file mode 100644 index 0000000..0f19884 --- /dev/null +++ b/parity/java4shell/ubuntu-pkg.Dockerfile @@ -0,0 +1,3 @@ +# Ubuntu 24.04's openjdk-25 package (25.0.5.0.3+1-3~24.04.1: no fix yet). +FROM ubuntu:24.04 +RUN apt-get update && apt-get install -y --no-install-recommends openjdk-25-jre-headless && rm -rf /var/lib/apt/lists/*