Blogs

Patching Java4Shell Without Restarting the JVM

How a bug in machine code ended up with a fix in Java

Patching Java4Shell without restarting the JVM, with the patch code

Java is supposed to make it impossible to write past the end of an array. The JVM checks the index on every write and throws an exception if it is out of range. Java4Shell (CVE-2026-114938) is a bug where the write happens anyway, because it comes from a machine-code routine built into the JVM, and the JVM doesn’t control or check where that routine writes in memory. The proof of concept is a Base64 string of 64 A characters followed by one =. On an unpatched JDK 21 running on a recent server CPU, the decoder writes 62 bytes beyond the end of its output array.

Waratek built a runtime security agent that fixes vulnerabilities by changing Java bytecode inside a running JVM. Oracle shipped the official fix on 17 November 2026. The same day we repeated the recommendation in VRT, the public CVE tracker our customers and partners use: install the fix, and until you can, start the JVM with a flag that turns the routine off.

We kept thinking about the bug, though. Log4Shell taught us that people keep looking for a patch for years after a bug like this, for the systems they can’t upgrade. Six days later we released a virtual patch whose body is a single if. This post explains how we got from one to the other.

How the bug works

Base64 is the encoding that carries binary data as text. Login headers and tokens use it, so servers routinely decode Base64 that came from outside. Four characters of text become three bytes.

In the JDK, two methods of java.util.Base64.Decoder share the work. decodeBlock decodes whole groups of four characters. Its caller, decode0, deals with whatever is left at the end, including the = padding.

The JVM treats decodeBlock specially. Once the decoder has been called often enough, the optimising JIT compiler stops using the method’s Java code and replaces the call with a routine that JVM developers wrote by hand in machine code. Such a routine is called an intrinsic, and Java’s bounds checks don’t apply inside it. On CPUs with AVX-512 VBMI (Intel Ice Lake and newer Xeons, AMD Zen 4 and newer) the routine decodes 64 characters at a time. It arrived in JDK 18 and was later backported to JDK 17.0.6.

The routine gets one case wrong. If 1 to 3 characters are left after the last 64-character block and they end in one or two =, it works out that it has -1 or -2 bytes left to write. It then turns that length into a mask that says which of the next 64 bytes to store, and for a negative length the mask comes out as all ones. So it stores all 64, and up to 63 of them land past the end of the output array. On JDK 21 and newer the smallest input that does this is 64 As followed by a single =. JDK 17 needs 128 As.

For that smallest input, Java makes the output array 50 bytes long. The routine decodes the 64 As into the first 48 bytes, then makes its one bad store of 64 bytes starting at byte 48:

input    AAAA ··· AAAA                       =
         └─── 64 characters, one block ────┘ └ the tail, 1 character

output   0                               48  50                                112
         ├───── 48 bytes, the block ─────┼──┤  end of the array
                                         ├───────── one 64-byte store ─────────┤
                                             └───── 62 bytes past the end ─────┘

Back in Java, decode0 finds the stray = and throws IllegalArgumentException. That’s the correct response to such input, but the bytes are already written.

Calif’s write-up describes how an attacker turns that write into reading and writing the Java heap, and from there into running their own code. The way in is any place where an application decodes Base64 that the attacker controls. Calif also says each exploit is specific to the application, and that there’s no universal one.

The OpenJDK fix is 4 lines of C++ in the code that generates the routine:

// A padded Base64 tail shorter than one complete block is handled by Java.
__ cmpl(length, 4);
__ jcc(Assembler::less, L_bruteForce);

If fewer than 4 characters are left, the routine hands them back to the Java code. The fix doesn’t turn the routine off and doesn’t rewrite it. It only keeps short tails away from it.

What our agent can and cannot patch

Our agent is a RASP agent. RASP stands for runtime application self-protection, which means it runs inside the JVM, alongside the application, and protects it from there. It has two ways of doing that.

The first is virtual patches. When a library or the JDK fixes a vulnerability, we write a small patch that makes the same change to the Java bytecode in the running JVM. Customers load it as a rules file, without touching their code and without a restart, and it holds until they can upgrade. That suits the usual Java CVE, where the fix is a change to a Java method. Here the faulty routine is machine code that the JVM generates for itself at startup, so there is no Java method to attach a patch to. For that reason, bugs in the JVM’s own native code are usually out of scope for us.

The second is generic rules. They watch the calls an attacker is after, such as running a SQL query, opening a file or starting a process, and block the ones the application shouldn’t be making. A rule like that doesn’t need to know which CVE the attacker came in through. But an attacker who controls the JVM’s memory can run machine code directly and never make one of the calls the rules watch.

So on the first day we had advice for our customers and no patch.

The first patch

The trouble with that advice is that both the flag and the JDK update need a restart, and not everyone can restart a production JVM the week a CVE comes out.

The same evening Theori published a runtime patch that doesn’t need one, and their approach is clever. It attaches to a running JVM and adds a compiler directive that keeps the optimising compiler away from decode0, so the routine is never called. That showed the bug could be closed in a live JVM, and it got us thinking. Our customers already run our agent inside the JVM, so we went back to see what it could do.

Our first patch went for the strongest guarantee we could think of, which was that the machine-code routine never runs at all. To keep decoding at a usable speed, the patch did the decoding itself, in Java. It worked, but it was 151 lines long and cut decoding throughput by 43% on large inputs. It also couldn’t ask the CPU about AVX-512, so every x86-64 JVM would have paid that cost, vulnerable or not. That made it slower than the JVM flag and not something we wanted to offer our customers as protection, so we kept looking.

The second patch

The first patch went wrong before any code was written. We’d assumed that a bug in machine code needed a fix in machine code, and when that wasn’t possible, we aimed for something much wider than the fix, which was that the routine must never run. But the OpenJDK fix doesn’t repair the routine or turn it off. It only makes sure the routine never gets a tail shorter than 4 characters, and what the routine is given comes from Java. A patch can do the same from the Java side, so we wrote one.

This is the kind of change our agent is built for. A patch is ordinary Java that the agent runs at one exact point inside a running method, here the moment decode0 calls decodeBlock. It can read the values in play at that point and change them, and the rest of the method runs exactly as the JDK wrote it. This is the whole body:

public void patch(JavaFrame frame) {
    // the call is this.decodeBlock(src, sp, sl, ...)
    // operand 0 is this, so sp is operand 2 and sl is operand 3
    int sp = frame.loadIntOperand(2);
    int sl = frame.loadIntOperand(3);
    int whole = sp + ((sl - sp) & ~3);
    if (whole != sl) {
        frame.storeIntOperand(3, whole);
    }
}

frame holds the arguments that are about to be passed. sp is where the input starts and sl is where it ends. The patch moves the end back until the range is a whole number of 4-character groups, and then the call goes ahead as before, into the fast routine.

Two things have to hold for this to be a fix. It must not change what the decoder returns, and it must keep the routine away from its bad case.

It doesn’t change what the decoder returns, because the tail was never decodeBlock’s job. The JDK’s own comment on the method says it “can choose to decode all, none, or a variable-sized prefix” of its input, and the Java version of the method begins by rounding the range down in the same way, with sp + ((sl - sp) & ~0b11). Whatever decodeBlock leaves, decode0 decodes afterwards. The characters our patch trims off take that path, so no result and no error message changes.

And the routine can no longer reach its bad case. It works through its input in 64-character blocks. If the range it gets is a multiple of 4, what is left after the last block is a multiple of 4 too, so it is never 1, 2 or 3 characters. That is the condition the OpenJDK fix checks inside the routine. Our patch makes it true before the routine is called.

The patch has no CPU check, because the trim is harmless on any hardware. It’s harmless on a fixed JDK too. The fix only changed machine code, so the Java class is byte-for-byte the same on vulnerable and fixed builds, and customers can leave the patch in place after they update.

Seeing the overflow

The overflow needs a CPU with AVX-512 VBMI. Our laptops don’t have it, but our CI runners on AWS m7i instances do, so we ran the proof of concept from Calif’s write-up there, on a vulnerable Oracle JDK 21.

Each run decoded 64 As followed by a short tail such as = or A==, in a fresh JVM. Without the patch, every tail that should overflow did, by 62 or 63 bytes. With the patch, none did, and valid input still decoded. Calif’s write-up also lists === as a trigger, but it didn’t overflow in our runs. We repeated the single-= test on JDK 17, with the 128 As it needs there, and on JDK 25, with the same result.

One run nearly fooled us. The first time we ran the test with the agent attached and no patch loaded, it reported no overflow. The run with the patch reported no overflow either, which at that point proved nothing. The routine is only used once the optimising compiler has compiled decode0. The proof of concept warms the decoder up with 200,000 calls, and with our agent in the JVM the compiler had so much else queued at startup that the loop finished before it got to the decoder. The vulnerable code never ran. With -Xbatch the JVM waits for compilation to finish, and the overflow was back.

We ran each tail in its own JVM for the same compiler reason. Tails like = are invalid, so the decoder rejects them with an exception, and the JVM then discards its compiled decoder. Until the compiler rebuilds it, the decoder runs as plain Java, and the next tail in the same JVM could miss the vulnerable code.

If you test your own fix for this bug, check that the same test shows the overflow with the fix removed. Every run above used -Xbatch.

We also measured what the patch costs on that hardware. It adds about 46 ns to every decode call, whatever the size of the input. That is the price of running our code before the call, and it shows on small inputs such as tokens and login headers: a 64-byte input goes from 14 ns to 61 ns. At 1 MB and above we couldn’t measure a difference, because the decoding itself still happens in the fast routine.

If you run JDK 17 or newer

Install the November 2026 update for your JDK as soon as your vendor has one. That’s the fix we recommend. If you can restart this week, the update and the JVM flag are all you need.

The patch is for the JVMs that can’t be restarted yet. Log4Shell in 2021 got far more attention than this bug has. Four years later, in 2025, Sonatype counted nearly 300 million downloads of Log4j from Maven Central, and about 13% of them were still versions with Log4Shell in them. Java4Shell has had less attention, and it affects fewer systems. It needs JDK 17.0.6 or newer and a CPU with AVX-512 VBMI. But that CPU feature is ordinary on cloud servers now. Our own CI runners have it. We expect unpatched JVMs to be around for months and probably years.

A runtime patch covers the time between knowing about the bug and getting the restart. If the Waratek agent is already in the JVM, the patch arrives as a rules update and the agent links it into the running decoder. It loads again on every restart, and once the JDK is updated it has nothing left to do.

  • Check whether you are exposed. You need JDK 17.0.6 or newer without the November 2026 update, on x86-64 with AVX-512 VBMI. Oracle’s Java 8 Enterprise Performance Pack is affected too. Oracle’s fixed releases are 17.0.21.1, 21.0.13.1, 25.0.5.1, 27.0.1.1 and 8u513-perf. On Linux, grep -om1 avx512vbmi /proc/cpuinfo prints the flag if the CPU has it.
  • If you can restart but cannot update yet, add -XX:-UseBASE64Intrinsics to the JVM options. Base64 decoding gets slower and the routine is gone.
  • If you run the Waratek agent and cannot restart yet, load the CVE-2026-114938 patch. It is available to customers now. You can leave it in place after the update.

We publish our write-ups for CVEs in VRT. The one for Java4Shell is here.

Share Article