-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 NEFARIOUSPLAN-CANONICAL-V1 {"body_md":"## HotSpot replaced the method. It did not replace the invariants.\n\n`java.util.Base64.Decoder.decode0` is a plain Java method with a plain Java preamble: length checks, the standard bounds-checked array writes the Java verifier inserts, and an `IllegalArgumentException` path for malformed padding. When C2 compiles a caller of `decode0` on a host whose CPU reports `avx512f`, `avx512bw`, `avx512vbmi`, and `bmi2`, HotSpot substitutes the method body wholesale with `_base64_decodeBlock`, a native stub emitted by `StubGenerator::generate_base64_decodeBlock()` at `src/hotspot/cpu/x86/stubGenerator_x86_64.cpp:5945` in jdk17u. The stub was added in JDK 18 and backported to 17.0.6. Encoding is one surface of the Base64 API; decoding is the other. Both the Java body and the native stub implement the same contract the caller sees. One of them carries the invariants that made the contract safe. The other was authored against the Base64 specification and the Intel software developer's manual.\n\nThis is a [parallel-implementation-gap](/patterns/parallel-implementation-gap) in the shape the pattern names. The security invariant that has to hold, \"write only as many bytes into the destination as the input actually decodes to,\" lives in the Java bytecode of `decode0` as checks the compiler inserts around the array stores. The intrinsic that replaces the method body at JIT-compile time implements the same client-facing contract and never imports those checks. For callers of `Base64.getDecoder().decode(byte[], byte[])`, which Spring Security's Basic-auth header parser, Tomcat and Jetty 12's authentication filters, `java-jwt`'s JWT segment decoder, Keycloak's SAML endpoints, and Jenkins, Jira, and Confluence's pre-authentication code paths all reach, the gated surface disappears the moment C2 warms up.\n\nThe Tomcat administrator who configured a Spring Boot service to accept `Authorization: Basic` is the victim of record. Nothing they wrote in their application calls into native code. The instant C2 compiles a caller of `decode0` with the right CPU flags, every subsequent Base64 decode in their process runs assembly they did not choose and that nobody at their vendor wrote the length check for.\n\n## Three arithmetic primitives agreed on a 64-byte write to a 0-byte output\n\nThe vectorized decoder processes input in 64-byte chunks, writes 48-byte output chunks, and falls into the label `L_finalBit` with 1 to 63 bytes of tail left. The tail path computes the output length by taking `length >> 2` and multiplying by 3. For `length` of 1, 2, or 3 bytes, that output length is zero. The path then checks whether the input ends with `=` and decrements the output length by one for each padding character present. `output_size` is now `-1` or `-2`.\n\nHere is the pre-patch path, verbatim from the assembly emitter:\n\n```cpp\n__ BIND(L_finalBit);\n// Now have 1 to 63 bytes left to decode\n\n// (no length guard here in the vulnerable version)\n\n__ movq(rax, -1);\n__ bzhiq(rax, rax, length); // input mask, fine for length in 1..63\n\n__ movl(output_size, length);\n__ shrl(output_size, 2); // output_size = length / 4, so 0 when length < 4\n__ lea(output_size, Address(output_size, output_size, Address::times_2, 0));\n\n__ addq(length, start_offset);\n__ cmpb(Address(source, length, Address::times_1, -1), '=');\n__ jcc(Assembler::equal, L_padding); // decrement output_size per '=' char\n\n__ BIND(L_donePadding);\n__ subq(length, start_offset);\n__ kmovql(input_mask, rax);\n__ movq(output_mask, -1);\n__ bzhiq(output_mask, output_mask, output_size); // output_size is -1 or -2 here\n```\n\n`bzhi` is the mask-forming instruction the whole path depends on. Intel documents its behaviour at counts of 64 or higher: \"If the start bit position is greater than or equal to the operand size, the destination operand is loaded with the source operand unchanged, and CF is set.\" The `output_size` register, read by `bzhiq` as an unsigned 64-bit value, is `0xFFFFFFFFFFFFFFFF`. That is comfortably above 64. `bzhiq` returns `-1` unchanged. `output_mask` is now sixty-four one-bits.\n\n```cpp\n__ vpermb(tmp, pack24bits, tmp, Assembler::AVX_512bit);\n__ kmovql(k1, output_mask);\n__ evmovdqub(Address(dest, dp), k1, tmp, true, Assembler::AVX_512bit);\n```\n\n`evmovdqub` with a mask of all ones is a full 64-byte store. The destination pointer is at `dest + dp`. For the Calif trigger, 129 bytes of `A` followed by `=`, the first 128 bytes consume two iterations of the 64-byte loop and advance `dp` to 96. The output buffer was sized 98 bytes. The store writes 64 bytes at `dest[96..160)`. Sixty-two of those bytes are past the end of the output buffer. Theori's `LoopHarness`, run across every release of the JVM from 17.0.6 through 27 on an AMD Zen 5 host, reports the same measurement on each: `OOB WRITE 62 bytes`.\n\nThe three arithmetic primitives in that path, `shrl`, `decrementq`, and `bzhiq`, are each correct in isolation. Right-shift by two is right-shift by two. Decrement is decrement. `bzhi`'s \"clamp to operand size\" semantics are the behaviour its manual page documents. The bug is that the path assumed `length >= 4` and encoded that assumption in nothing. Three operations that were each honest about what they did agreed on a 64-byte write to a zero-byte output.\n\n## The author wrote the safe path first. The comment explaining why they didn't ship it is still there.\n\nThe upstream fix, tracked as JDK-8393212 and titled `Base64 optimization improvements`, is two instructions:\n\n```diff\n __ BIND(L_finalBit);\n // Now have 1 to 63 bytes left to decode\n+\n+ // A padded Base64 tail shorter than one complete block is handled by Java.\n+ __ cmpl(length, 4);\n+ __ jcc(Assembler::less, L_bruteForce);\n+\n // I was going to let Java take care of the final fragment\n // however it will repeatedly call this routine for every 4 bytes\n // of input data, so handle the rest here.\n __ movq(rax, -1);\n __ bzhiq(rax, rax, length);\n```\n\nThe comment on the sixth line of that block, \"I was going to let Java take care of the final fragment however it will repeatedly call this routine for every 4 bytes of input data, so handle the rest here,\" is older than the patch. It documents a decision the author made when they wrote the stub. Java's non-vectorized decoder, the scalar `L_bruteForce` block farther down in the same function, was available. They considered routing the 1-to-63-byte tail to it. They rejected the route because Java would re-enter the native stub four bytes at a time, paying the dispatch cost for every quartet, and chose to handle the tail in-stub for speed.\n\nThe patch is that decision, reversed, within bounds. For tails of four or more bytes the author's fast path is still taken. For tails of one, two, or three bytes, which is exactly the window where `output_size` can underflow, control jumps to `L_bruteForce`, the Java-handled path the comment rejects.\n\nThe comment is still accurate. Java does re-enter the stub for every four bytes of input. Writing the slow path was always going to be slower. The author's reason for picking the fast path was not wrong. What the author did not model, and what the comment does not acknowledge, is that the fast path's correctness required a lower bound on `length` that nothing in the function was going to enforce.\n\n## The exception Java's method would have thrown now fires after the write.\n\n`Base64.Decoder.decode(byte[] src, byte[] dst)` is defined to throw `IllegalArgumentException` if the source does not match the Base64 scheme, including malformed padding. On the un-JIT'd interpreter path, the Java body of `decode0` checks the padding and throws before writing anything. On the C2-compiled path with the intrinsic active, the native stub runs to completion first, returns the number of bytes decoded, and only then does the surrounding Java code look at the returned value against its own validation and raise. The `IllegalArgumentException` fires. The stack trace is clean. The heap, at `dest[98..160)`, is not.\n\nEvery public PoC in the set exploits that race between the write and the exception. TahuCryptsi's `RemoteRceLab3` allocates a 98-byte `scratch` immediately adjacent to a 100-byte `blob` whose object header begins at `scratch[98]`:\n\n```java\nstatic final class Sess {\n final byte[] scratch = new byte[98]; // decode destination\n final byte[] blob = new byte[100]; // neighbour, header gets corrupted\n // ...\n}\n```\n\nThe 62-byte out-of-bounds write clobbers the blob's mark word, its class pointer, and its length field. The length field becomes a huge unsigned integer. The next JVM-emitted bounds check on `blob[off]` reads from the clobbered length and lets the attacker read arbitrary bytes out of the heap, which the lab service then passes to `Runtime.exec`. That the service catches `IllegalArgumentException` and keeps serving is what lets the next request use the corrupted blob. Calif has claimed working pre-authentication RCE chains against Keycloak, Jira, Confluence, JFrog Artifactory, Jenkins, and PingFederate using this shape, with the per-product grooming held back from public release. Wiz has demonstrated remote chains against Jenkins, Confluence, and Tomcat independently.\n\n## The documented runtime mitigation does not mitigate anything.\n\nOracle's advisory and every vendor repack tells operators to install the fixed build and restart the JVM. Three paragraphs down from that, the documentation for the `UseBASE64Intrinsics` and `DisableIntrinsic` flags implies two runtime escape hatches: disable the Base64 intrinsic specifically, or disable intrinsics for `_base64_decodeBlock` by name. Theori's team at Xint, while building a runtime hotpatch, worked through both and found that neither works at runtime.\n\n`-XX:-UseBASE64Intrinsics` added via `jcmd VM.set_flag` returns \"flag is not manageable.\" Writing `false` into the flag's byte in `/proc/PID/mem` with `Unsafe` or a ptrace tool succeeds, the flag reads as `false` from the diagnostic bean afterwards, and HotSpot still calls the intrinsic. The reason is in `vmIntrinsics::is_disabled_by_flags`: the per-intrinsic decision is cached in `vm_intrinsic_control_words` the first time any caller is compiled, and the cached decision wins every later compilation. On 17u, the method-level `-XX:CompileCommand=DisableIntrinsic,_base64_decodeBlock` directive, documented as the surgical disable, is silently a no-op: `compilerDirectives.cpp` initializes the directive's intrinsic control words from the global `-XX` flags only and never applies the JSON option. The two mechanisms the Oracle manual names as the runtime fix are each non-functional at the point an operator would reach for them.\n\nTheori's workaround synthesises a third path. `Compiler.directives_add` is used to exclude `java/util/Base64$Decoder.decode0` from C2 compilation and forbid it from being inlined anywhere, so no C2-emitted code contains the intrinsic call site:\n\n```json\n[{ \"match\": \"java/util/Base64$Decoder.decode0\",\n \"c2\": { \"Exclude\": true } },\n { \"match\": \"*.*\",\n \"inline\": \"-java/util/Base64$Decoder.decode0\" }]\n```\n\nThen `inst.retransformClasses(Base64$Decoder.class, Base64$Encoder.class)` with unchanged bytecode forces deoptimization of every already-compiled method that reached the intrinsic. Across a 49k RPS Spring Boot workload, Theori measured the retransform at 18ms with zero errors. The path works. It is not documented as a Base64 mitigation. It had to be derived because the documented ones do not. The vendor advisory that tells you to upgrade or disable the intrinsic is correct about upgrade. The disable half of the sentence describes a mechanism that stopped working the first time the application called `Base64.getDecoder().decode()`.\n\n## Why this exists\n\nThe AVX-512 Base64 decoder is one of a growing set of HotSpot intrinsics that replace Java methods with hand-written vector assembly: AES, SHA, ChaCha20, CRC32, Adler, Base64. Each stub implements the Java method's contract against an instruction set that cannot run Java's bounds checks, because the bounds checks are inserted by the Java verifier around bytecode array stores, not called from the method body. The invariants that the Java method depended on, including \"writes do not exceed the output array's declared length,\" live in the Java bytecode. The stubs are authored against the algorithm's specification and the Intel software developer's manual. The only path that would make the invariants travel with the contract is a convergence point every implementation routes through, and there is none. The stubs are leaves of a dispatcher in `vmIntrinsics.cpp`, each authored against the contract separately, each responsible for its own safety.\n\nCVE-2026-114938 is one instance of the class. The patch here closes this stub's path for tails of fewer than four bytes. The architecture that lets a method body be swapped for a vector stub without the Java method's invariants travelling into the stub is unchanged.\n\nPoC: [theori-io/java4shell-runtime-patch-CVE-2026-114938](https://github.com/theori-io/java4shell-runtime-patch-CVE-2026-114938) (Theori's runtime hotpatch, with verification harness). Trigger disclosure and discovery credit: Quang Luong and Albert Yang at Calif, [calif.io/java4shell/](https://calif.io/java4shell/).","closing_line":"HotSpot replaced the Java method's body and kept its exception handler. The exception now fires after the 62 bytes.","hook_md":"`Base64.getDecoder().decode()` is one of the most-called methods in enterprise Java. HotSpot replaces its body on x86-64 CPUs with AVX-512 VBMI, substituting roughly five hundred lines of hand-written assembly committed in JDK 18 and backported to 17.0.6. The Java method validated its input before writing. The assembly that replaced it does not, and on a 129-byte input ending in `=` it writes 62 bytes past the end of the destination array, after which the Java method's exception handler fires on the input it should have refused. CVE-2026-114938 is that gap, called Java4Shell by the Calif researchers who disclosed it, affecting Oracle Java SE 17, 21, 25, 27 and every OpenJDK distribution built from the same tree.","post_id":877,"slug":"java4shell-cve-2026-114938-the-length-check-did-not-come-along","title":"CVE-2026-114938: HotSpot Swapped Java's Base64 Decoder for 500 Lines of AVX-512. The Length Check Did Not Come Along.","type":"initial","unreadable_sentence":"HotSpot replaced the Java method's body and kept its exception handler. The exception now fires after the 62 bytes."} -----BEGIN PGP SIGNATURE----- iHUEARYIAB0WIQRf0htP5+SjynlxywneZjl4jgkQJgUCawGvvQAKCRDeZjl4jgkQ JksaAQC2N3sNscilrNAX00kWm1ZK/9cK5j2VRFIAy5169TTFpwEApErKqKxUwtDr q+BbWmk7Ybje589OxSw/R/bsgfVDngk= =qGgi -----END PGP SIGNATURE-----