r/programming • • 3d ago

Java4Shell is a reason to use Java

https://mccue.dev/pages/11-18-26-java4shell
29 Upvotes

48 comments sorted by

52

u/gredr 3d ago edited 3d ago

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.

39

u/koflerdavid 3d ago

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.

10

u/bowbahdoe 3d ago

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.

12

u/svick 3d ago

You really shouldn't treat non-security features as security features.

If a bug is found in a security feature, the usual approach is:

  • We need to fix this ASAP.
  • We try hard to prevent anyone from knowing anything about it before it's fixed.
  • We make sure it's fixed in all supported versions.

If a bug is found in a non-security feature, the more usual approach is:

  • We'll get to it when we'll get to it.
  • Everything about the bug is public.
  • It's only going to be fixed in the version that comes out in many months.

18

u/gredr 3d ago

.NET gave it up for the same reason.

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.

38

u/Professional_Price89 3d ago

"Faster than C", then show a 71% slower than C table.

9

u/za3faran_tea 2d ago

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

6

u/coderemover 1d ago

Do you have a link? A benchmark without information how to reproduce is just an ad.

0

u/za3faran_tea 1d ago

I don't, but here's the chart: https://imgur.com/a/LUG597k

3

u/coderemover 1d ago

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.

-39

u/bowbahdoe 3d ago

It can be faster, but 71% slower is still competitive

36

u/gredr 3d ago

You have a weird definition of "competitive".

4

u/coderemover 1d ago

71% slower by wall clock. What about CPU and memory usage?

-23

u/bowbahdoe 3d ago edited 3d ago

Is it being phased out? I'll admit to being out of the loop somewhat. I know there are memory-unsafe features like https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/unsafe-code

I think the core criticism that "C# dependencies can quietly bring in memory-unsafe code" holds.

Correct me if I'm wrong though.

23

u/KryptosFR 3d ago

First link has nothing to do with .NET.

And yes you can use unsafe code so what? It's a needed feature for interop and it must be enabled with <AllowUnsafeBlocks>true</AllowUnsafeBlocks>.

6

u/bowbahdoe 3d ago

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.

13

u/KryptosFR 3d ago

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.

4

u/bowbahdoe 3d ago edited 3d ago

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

8

u/_choam_ 3d ago

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

4

u/_choam_ 3d ago

You also can block unsafe in your own code and dependencies (not sure about the stdlib and compiler builtins though)

1

u/simon_o 2d ago

That doesn't make C# unsafe or dangerous.

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.

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.

65

u/bowbahdoe 3d ago • Popcorn

I would say its closer to

tldr:

  • A vulnerability was found in the C++ code behind Java.
  • We need to fix that and audit that C++ pretty deeply
  • But Java code is usually immune to these kinds of problems.
  • And auditing one C++ codebase is easier than auditing N dependencies
  • So use Java

29

u/bowbahdoe 3d ago

With the corollary that

  • Other GC'ed languages are less immune to these kinds of problems because of their "seamless" support for calling C++.

Which is the part that people seem to be taking exception to.

3

u/coderemover 1d ago edited 1d ago

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.

-1

u/Virviil 1d ago edited 1d ago

Its statements contradicts themselves.

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

2

u/bowbahdoe 20h ago

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. 

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.

2

u/bowbahdoe 3d ago

I mean I seem to be a somewhat misinformed on C#. No easier way to learn than saying the wrong thing out loud.

But can you point out anything else where I'm hilariously misinformed?

-10

u/BenchEmbarrassed7316 3d ago

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?

9

u/davidalayachew 3d ago

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.

5

u/BenchEmbarrassed7316 3d ago

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.

5

u/davidalayachew 3d ago

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".

3

u/BenchEmbarrassed7316 3d ago

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.

4

u/bowbahdoe 3d ago

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.

4

u/davidalayachew 2d ago

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.

0

u/BenchEmbarrassed7316 1d ago

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".

3

u/davidalayachew 1d ago

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.

1

u/simon_o 2d ago

Dude, if you have no clue why do you keep waffling about general ideas that are not applicable to the relevant use-case at hand?

1

u/bowbahdoe 3d ago

Good question! I do wonder how much was gained here over what the JIT spits out normally.

My bet is "not enough to justify it," but who knows.

3

u/sintrastes 3d ago

Java4Shell is a reason to use Lean 4, Rocq, and/or Rust.

6

u/bowbahdoe 3d ago

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. 

1

u/coderemover 1d ago

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.

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.