Java4Shell is a reason to use Java

Java4Shell is a reason to use Java

by: Ethan McCue Good Morning. We’re less than a day into dealing with “Java4Shell”, an issue stemming from how the Java Virtual Machine intrinsifies Base64 encoding code. If you have a Java deployment you haven’t updated yet, add -XX:-UseBASE64Intrinsics to the VM flags. Then upgrade your VM to a version where the issue is patched when you can.

作者:Ethan McCue 早安。我们处理“Java4Shell”问题还不到一天,这个问题源于 Java 虚拟机(JVM)对 Base64 编码代码进行内联(intrinsify)处理的方式。如果你有尚未更新的 Java 部署,请在虚拟机标志中添加 -XX:-UseBASE64Intrinsics。然后,在条件允许时将你的虚拟机升级到已修复该问题的版本。

This is just one in a string of high profile outages and vulnerabilities. AI is a good fuzzer, people are finding latent problems with much lower effort than the past. People are pretty worried about this because we’re all pretty sure there are more latent problems to find. Vulnerabilities tend to be bigger problems when they stem from native code, and they tend to be widespread problems when they come from dependencies many people share. Java4Shell is both of these things. A lot of people use Java and the problem stems from a handwritten assembly intrinsic. Fun times.

这只是一系列备受瞩目的中断和漏洞中的一个。人工智能是一个很好的模糊测试工具,人们发现潜在问题的难度比过去低得多。大家对此感到非常担忧,因为我们都确信还有更多的潜在问题等待被发现。当漏洞源于原生代码时,它们往往会成为更大的问题;而当它们来自许多人共享的依赖项时,往往会成为广泛传播的问题。Java4Shell 两者兼具。许多人使用 Java,而这个问题源于手写的汇编内联代码。真是有趣的时光。

But, for a moment, lets say we trust the VM. It is a big dependency, but it is only one dependency. We can all agree to audit it more closely, reduce the amount of native code in it, proactively run AIs against it, etc. Of course. Normal Java programs are memory safe. If you use a library which wants to call native code you will get a warning, later a hard error, unless you the person using the library adds —enable-native-access=the.specific.library to their command-line options.

但是,暂时假设我们信任虚拟机。它是一个庞大的依赖项,但它仅仅是一个依赖项。我们都可以同意更仔细地审计它,减少其中的原生代码量,主动运行人工智能对其进行测试,等等。当然。普通的 Java 程序是内存安全的。如果你使用的库想要调用原生代码,你会收到警告,随后是硬错误,除非作为库使用者的你将 —enable-native-access=the.specific.library 添加到命令行选项中。

This means even with a sprawling dependency tree, which is a problem for all programming language ecosystems, you can know your exact exposure to native code and what dependencies you need to audit more closely. Compare that to C#, which lets any dependency write assembly inline. Python, JavaScript, Go - everything speaks C and lets any one of your dependencies bring along native code. As we are all learning the hard way, this carries risk.

这意味着即使面对庞大的依赖树(这对所有编程语言生态系统来说都是一个问题),你也可以确切地知道自己对原生代码的暴露程度,以及需要更仔细审计哪些依赖项。相比之下,C# 允许任何依赖项内联编写汇编代码。Python、JavaScript、Go——所有这些语言都与 C 语言互通,并允许你的任何依赖项携带原生代码。正如我们都在以惨痛的方式学习到的那样,这带来了风险。

Not only are Java programs memory safe. They are also fast. The exact mechanism which caused Java4Shell (the ability for the VM to swap out normal Java code for native code) combined with JIT compilation can make the actual performance of a Java program competitive with C. Sometimes a Java program can even be faster. A JIT compiler knows what code paths are actually taken and can optimize around them. They can even refocus what is optimized if the workload changes. It is pretty cool!

Java 程序不仅内存安全,而且速度很快。导致 Java4Shell 的确切机制(虚拟机将普通 Java 代码替换为原生代码的能力)结合 JIT 编译,可以使 Java 程序的实际性能与 C 语言相媲美。有时 Java 程序甚至可能更快。JIT 编译器知道哪些代码路径被实际执行,并能围绕它们进行优化。如果工作负载发生变化,它们甚至可以重新调整优化的重点。这非常酷!

Somewhat recently I went through the raytracing in a weekend book with Java. Impl mean ± σ vs C++ ───────────────────────────────────────────────── C++ clang 21 -O3 7.00 ±0.01 1.00x Java 28, value classes 11.95 ±0.25 1.71x C# .NET 11 preview 6 11.95 ±0.22 1.71x C# .NET 10 11.98 ±0.11 1.71x Java 26 17.53 ±0.21 2.50x

最近我用 Java 完成了《Ray Tracing in a Weekend》这本书中的内容。实现平均值 ± 标准差对比 C++ ───────────────────────────────────────────────── C++ clang 21 -O3 7.00 ±0.01 1.00x Java 28, value classes 11.95 ±0.25 1.71x C# .NET 11 preview 6 11.95 ±0.22 1.71x C# .NET 10 11.98 ±0.11 1.71x Java 26 17.53 ±0.21 2.50x

Benchmarks can be problematic, I know. What I want you to take away is that the JVM makes your code fast enough that most people don’t need native code. Unlike, to take a potshot, CPython. There everything is slow as molasses unless you make it a native extension. So if you are worried AI is going to find security problems with your code, use Java. The problems are likely to be less severe. Or use Kotlin, Clojure, JRuby, etc. Whatever. Most of what I talked about applies to all of them. Its only what I didn’t talk about (the module system) that is Java specific.

我知道基准测试可能会有问题。我想让你明白的是,JVM 让你的代码运行得足够快,以至于大多数人根本不需要原生代码。这与 CPython 不同(顺便吐槽一下)。在 CPython 中,除非你将其制作成原生扩展,否则一切都慢如蜗牛。所以,如果你担心人工智能会发现你代码中的安全问题,那就使用 Java。这些问题可能会不那么严重。或者使用 Kotlin、Clojure、JRuby 等。随便什么都行。我所谈论的大部分内容都适用于它们。只有我没谈到的部分(模块系统)才是 Java 特有的。