This week's exciting instalment of Vulnerabilities That Would Have Been Prevente...

@david_chisnall@infosec.exchange

This week's exciting instalment of Vulnerabilities That Would Have Been Prevented By #CHERI is brought to you by OpenJDK! This week's story is a less commonly told tale of how CHERI benefits you even if you're using safe languages.

Java's rich standard library is one of the things that people like the most about the environment. If you want to do something vaguely common, the odds are that there's an implementation somewhere under the java namespace and it's probably been well optimised by the authors of the Java system that you're using.

Base64 is no exception. For those unfamiliar with Base64, it was originally created to allow embedding arbitrary binaries in 7-bit emails and is now used for embedding binary data in a variety of text protocols. The core idea is that each character is one from a set of 64 valid printable characters. A normal data stream with 8-bit bytes is base 256 (each byte is one of 256 values). Encoding base64 is mildly complex but decoding is very straightforward: map from the 6-bit value in each 8-bit byte to the correct value, and then compute a running sum.

This is the kind of thing that vector / SIMD instruction sets are very good at. OpenJDK includes a version optimised for recent x86 CPUs using AVX-512, which lets it process 64 base64 characters in parallel: a huge potential speedup. Unfortunately, this had a memory-safety bug and could overwrite an adjacent object.

How would CHERI help? Back in 2017, we published CHERI JNI: Sinking the Java security model into the C

This paper describes how you can use CHERI to enforce Java memory safety (and type safety) guarantees across the native-code interface from Java. The base64 decoder is a perfect fit for the ephemeral sandbox model that this paper proposed as one of the (attribute-controlled) options. A base64 decoder function has no state between calls, so temporal safety is trivial to enforce by just discarding any temporary state that it's accumulated. When the native implementation of the base64 function wanted to access a Java array or byte buffer for output, it would receive a bounded capability to that region. If it tried to write outside of the buffer, even with hand-written assembly code, this would trap. In the CHERI JNI model, a bounds error like this in native code would be handled at the boundary to native code and turned into an IndexOutOfBoundsException, which Java could handle as any normal exception.

CHERI lets you get the productivity wins of high-level languages without sacrificing their safety once you call carefully optimised C or assembly routines.