That's a lot of words to say --enable-native-access is a good security feature. OP isn't really wrong. Without it, you're vulnerable to MS07-052: Code execution leads to code execution.
Interesting that the JVM doesn't frame --enable-native-access as a security feature at all; it's an integrity feature. If you're running a malicious (or vulnerable) dependency, blocking native access isn't going to save you. See also: Java4Shell's namesake, Log4Shell.
Interesting that the JVM doesn't frame --enable-native-access as a security feature at all; it's an integrity feature.
Java has given up on achieving in-process security. Sandboxing is very hard to get right; as we have seen it is already hard enough to ensure memory safety. Thus, restricting 3rd party libraries from using FFI is not about security, but about ensuring that operations is aware that dependencies are doing something that might make code not run as initially intended by the developers, might dork up the JVM, or cause issues that are out of control of the JVM.
I think both --enable-native-access and --module-path are integrity features, but integrity features are security features. Here the issue is that native code mucks with the JVM's memory. Thats an issue for integrity that also lets people do RCE (allegedly).
The module system stuff is similar. Code in a library can't accidentally mess with the threading model since java.lang.Thread hides that stuff in the java.base module. Putting user code in modules also limits the scope of what a badly written library can do to that code.
Network and File System level security is mostly left up to Docker/OS controls though.
Such a weird flex and weird take. Tries to make C# look bad by linking to something entirely unrelated: inline assemblies which a C/C++ MSVC specific feature and only supported on x86 (which is being phased out in . NET).
Some guys on the JDK team benchmarked the caffeine caching library (written in Java) with a similar one Moka (written in Rust), and Java had lower CPU usage and lower latencies at the same throughput
It’s garbage. It doesn’t even say what allocator was used and what size of objects. I did benchmark mimalloc against ZGC using a high residency database-like workload and Rust ran circles around Java in all aspects, including CPU spent on memory management.
Do you need to enable it for dependencies too? Like if you don't have <AllowUnsafeBlocks>true</AllowUnsafeBlocks> you know no dependencies are using unsafe code?
And yep, my bad. Replacing it with a more appropriate link.
You need it if you call directly API that are marked unsafe. Note that the new model makes it more explicit so even with dependencies you will likely need it more often.
That doesn't make C# unsafe or dangerous. Rust use a similar contract where unsafe code can be wrapped by safe code and code calling the safe version isn't considered unsafe.
Okay but there are still dependencies where the unsafe-ness is something you don't know about unless you read their code?
Like is this a new thing for "directional" safety? And yeah rust does something similar. But people do end up accepting unsafe if it "feels audited" or "feels neccesary." I didn't touch on Rust because I feel like all these -5s on my comments would be -50s if those folks get awakened, but it is a similar criticism
I mean, the way rust does "unsafe" is the right trade off for that type of language. I dont agree with that people just end up accepting unsafe as being audited
No, just less safe and more dangerous then Java, which is the point OP wanted to make.
--enable-native-access works transitively, and at runtime, which means the person executing the code sees if anything shady might be going on and where, and needs to explicitly acknowledge it.
This is way more useful than a compiler option like AllowUnsafeBlocks that is only relevant at build time.
Java is not immune to those problems. It has its own share of problems. And maybe C++ has some problems that Java doesn’t have, but that part is solved by Rust. And Rust is generally safer than Java.
If dependencies are NOT allowed not use native code - they are slow and so my Java program is. If dependencies ARE allowed - Java is the same “vulnerable” as other languages. If dependencies turring-full compiles JIT into native - it’s impossible to audit
Well take for context that the response above is framed by the way the first person framed it.
If dependencies are NOT allowed to use native code
They are pure Java and are optimized like the rest of your program. Probably worst case it's like 2.5x slower or something. It's not Python where it's 50x (and like I alluded, sometimes it's 1x or 0.9x. really depends on the code)
If dependencies are allowed to use native code
You have to explicitly add a flag to enable it, so at least you are aware that you have a different risk profile than pure Java
You can have that flag be per-library so you know exactly what you need to audit deeply to have confidence you don't have buffer overflows
And in both situations
you need to trust the native code in the VM. But at least that's only one "thing." Let's audit it more, of course. Or find ways to make it safer by construction. But it does make it so most people don't need to go to these depths.
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!
Well, it’s so impressive that they had to write an assembly snippet by hand (at least, that’s what I gathered from the message). If the JVM is capable of such excellent optimization, why bother writing an assembly snippet?
Well, it’s so impressive that they had to write an assembly snippet by hand (at least, that’s what I gathered from the message). If the JVM is capable of such excellent optimization, why bother writing an assembly snippet?
Because the small print answer is -- everything you quoted is true, but it takes a surprisingly long time for the JIT to "get it" and spit out that "faster-than-C" answer. Long, as in it could take seconds or minutes even to get to that point.
For long running applications, this is usually fine. For short-running ones, not usually.
In the (rare) situation where the answer is obvious to humans but not the JIT, the OpenJDK team likes to hand-code assembly. That's basically what happened here.
but it takes a surprisingly long time for the JIT to "get it" and spit out that "faster-than-C" answer
I disagree with that: JIT compilation takes a significant amount of time because specific code must be executed repeatedly to gather statistics, such as which classes are used for which interfaces, what values might be null, and so on - information that isn't explicitly present in the code itself.
For a function that converts String -> byte[], you generally don't need a JIT compiler, as the operation essentially boils down to raw byte manipulation. At most, a JIT compiler might reorder branches within if or switch statements based on which paths are executed more frequently.
I disagree with that: JIT compilation takes a significant amount of time because specific code must be executed repeatedly to gather statistics, such as which classes are used for which interfaces, what values might be null, and so on - information that isn't explicitly present in the code itself.
You disagree with my claim that "that's basically what happened here" or the info I presented?
If you mean my conclusion -- possibly, I wasn't there. Maybe they had different or more pressing reasons to handcode assembly.
But if you mean my argument itself, no, you are oversimplifying.
The reason why JIT is so powerful is because it doesn't just boil down individual methods into something more optimized -- sometimes, it will boil down multiple consecutive methods into optimized instructions.
So, saying it as String -> byte[] is not the full picture -- we must also look where that method is being used. For example, if we are cycling through a String[] to turn each into some Base64 output, JIT compilation may very well construct an optimized set of logic that converts String[] -> byte[][]. Not to say that that is what it did here, or that that was what is was aiming for, but just clarifying that the JIT is not nearly as limited as you are making it out to be, nor is its scope of optimization as limited as the boundaries of a single method.
The purpose of the JIT is to optimize the whole program. And if it can, it will outright eliminate or optimize every single line of code into what it believes is best, given enough time and proper input. But if proper input isn't available, sometimes the OpenJDK folks like to give their own hand-rolled assembly to "help it along".
I disagree that a JIT is needed to optimize a simple byte manipulation.
JIT compilation is highly significant when information cannot be derived from the code itself, for instance, in cases of dynamic typing.
One of the key optimizations within the JVM is devirtualization, which paves the way for inlining and other further optimizations. For AOT compilers, the equivalent approach is monomorphization. These techniques are particularly relevant when dealing with long call chains, code distributed across multiple modules, and similar scenarios. In Java, with dynamic class loading, and other features, it is impossible to do complete monomorphism. This is where JIT comes in.
Regarding low-level byte manipulation, a JIT compiler can theoretically provide insights into which branches within if or switch statements are executed most frequently, with minimal impact on overall performance. However, for most cases, JIT is not needed to perform this optimization.
My guess is that the C compiler spends a lot of time making sure that the loop meets many conditions, and only a fraction of the time generating SIMD code. If the JVM could generate code as efficiently as C (or another compiled language), it would be much more logical not to write assembly instructions, but to provide an intermediate representation of the code in which all possible hints have already been calculated, because the JIT also spends a lot of time on data and code checking, the code generation itself is probably cheaper.
Well one thing it can do, even if there is no runtime info needed to do the compilation, is use instruction specific to your exact CPU make and model. Again, relevant to the security issue too.
I disagree that a JIT is needed to optimize a simple byte manipulation.
But that's my point -- you are thinking way too small scale.
It's not just the method, but the code calling it -- application code.
If you have one method doing byte manipulation amongst many other operations doing more byte manipulation, the JIT can try and find a specialized operation that would do the both in combination.
That is what JIT Compilation is good at -- taking 2 almost unrelated pieces of code that, when called in-tandem many times in a row, optimizes them into something even faster.
The fact that one of the methods in question is a simple byte manipulation method is irrelevant to whether or not savings could be found.
That is what JIT Compilation is good at -- taking 2 almost unrelated pieces of code that, when called in-tandem many times in a row, optimizes them into something even faster.
Any optimizing compiler works this way, whether it's JIT or AOT. The difference is that in some languages, you have to gather statistics and make assumptions to perform devirtualization, while in other languages, it's known right away.
Here is a simple example:
result = (a + b) / a.foo();
There are many optimizations that can be done here. If the compiler knows which foo will be called, it can check whether it can return 0, 1, or any other number. Also, if the compiler knows that a and b are constants or their values are known in advance, it can perform more optimizations (for example, if this calculation is guaranteed to be zero, there is no point in performing the div operation).
Now, if you are using a JIT compiler, it would be wasteful to check the left-hand side of the expression (a + b) at every optimization and deoptimization. It is much simpler to have some intermediate representation of these instructions, which could be created either in AOT mode when compiling the sources into bytecode, or at program startup, or on first access.
I believe that byte manipulation when decoding base64 is a fairly simple operation, which is confirmed by the fact that they were able to describe it in assembly language at all.
So the fact that they used ASM instead of some intermediate representation makes me doubt the effectiveness of this intermediate representation, to the extent that it allows generating high-quality code that would be "almost like C".
Now, if you are using a JIT compiler, it would be wasteful to check the left-hand side of the expression (a + b) at every optimization and deoptimization.
Again, you are thinking way too small scale.
The reason why it makes sense to check that on a regular basis (maybe not everytime) is that programs change over time, and the needs on start or first access are not necessarily the same for peak times.
Again, I encourage you to think much larger scale. You're looking at this as an isolated problem when it is actually a tiny piece of a much larger program.
Yes, optimizing individual components tends to result in optimizing the larger program, and that's not a wrong approach to take.
But you are wrong to claim things like it is wasteful to repeatedly check when the JIT has been demonstrating optimizations for use cases like this for nearly a decade. Just because it is wasteful in certain contexts, doesn't mean that applies for this one.
That is a valid position to take, so long as you accept that all of those options have a cost in program maintenance.
Expressing everything via a proof or with an involved type system is overhead when you are reading code after the fact. You can argue that this is offset by any number of things, but that is how it is.
There's also something to say about the fact that we already have an educational industrial complex that pumps out Java developers of at least some level of training. We don't have that for lean.
If you wanted to investigate replacing parts of the jvm that are currently C++ with lean proved components, that would have high value.
In my experience Rust is much lower cost to maintain than Java. Rust is harder to initially write, but overall it forces developers to use simpler designs - especially the aspect of sharing data. Rust programs have usually simpler data flow and are easier to reason about.
Its miss leading title this is their internal lib problem, so the most closer conclusion would be promote new valhalla and new ffm api on some of the java lib which is something that are being progressing in background as we talks right now. However, overheads might varies across jvm impl.
52
u/gredr 3d ago edited 3d ago
That's a lot of words to say
--enable-native-accessis a good security feature. OP isn't really wrong. Without it, you're vulnerable to MS07-052: Code execution leads to code execution.Interesting that the JVM doesn't frame
--enable-native-accessas a security feature at all; it's an integrity feature. If you're running a malicious (or vulnerable) dependency, blocking native access isn't going to save you. See also: Java4Shell's namesake, Log4Shell.78
u/KryptosFR 3d ago
Such a weird flex and weird take. Tries to make C# look bad by linking to something entirely unrelated: inline assemblies which a C/C++ MSVC specific feature and only supported on x86 (which is being phased out in . NET).
The coping is hard on that one.
56
u/BenchEmbarrassed7316 3d ago
What a nonsense.
tldr: A vulnerability was found in Java. Java isn't much slower than C++. So, use Java.
22
u/New-Anybody-6206 3d ago
I ignore posts with a dogmatic title, because it's almost always hilariously misinformed. Tech people are IMO no exception to Dunning-Kruger.
3
u/sintrastes 3d ago
Java4Shell is a reason to use Lean 4, Rocq, and/or Rust.
3
u/Schmittfried 3d ago
I don’t have to read the article to know the title is wrong.
1
u/zerosign0 1d ago
Its miss leading title this is their internal lib problem, so the most closer conclusion would be promote new valhalla and new ffm api on some of the java lib which is something that are being progressing in background as we talks right now. However, overheads might varies across jvm impl.