r/programming • • 5h ago

Java4Shell is a reason to use Java

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

24 comments sorted by

15

u/BenchEmbarrassed7316 5h ago

What a nonsense.

tldr: A vulnerability was found in Java. Java isn't much slower than C++. So, use Java.

31

u/bowbahdoe 5h ago

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

18

u/bowbahdoe 4h 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.

19

u/KryptosFR 5h 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.

2

u/Professional_Price89 5h ago

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

-14

u/bowbahdoe 5h ago

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

1

u/gredr 3h ago

You have a weird definition of "competitive".

-8

u/bowbahdoe 5h ago edited 4h 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.

4

u/KryptosFR 5h 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>.

5

u/bowbahdoe 5h 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.

5

u/KryptosFR 5h 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 5h ago edited 4h 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

3

u/_choam_ 2h 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

3

u/_choam_ 2h ago

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

2

u/gredr 3h ago edited 3h 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.

4

u/koflerdavid 2h 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.

1

u/bowbahdoe 1h 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.

4

u/gredr 2h ago

.NET gave it up for the same reason.

2

u/New-Anybody-6206 5h ago

I ignore posts with a dogmatic title, because it's almost always hilariously misinformed. Tech people are IMO no exception to Dunning-Kruger.

4

u/bowbahdoe 5h 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?

-4

u/BenchEmbarrassed7316 2h 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?

1

u/bowbahdoe 1h 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.

1

u/davidalayachew 1h 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.