Hacker News new | past | comments | ask | show | jobs | submit
It's important to note that Java, like Rust, is generally seen as a memory safe language. But it has bits of assembly, just like Rust has unsafe blocks, to handle some operations. Hence the Java2shell recent vulnerability. Granted Rust may have less need for them and more facilities to audit them, but it is not by itself a panacea that will fix all memory issues. In essence, there is no software stack that is fully memory safe, it is more a matter of restricting the memory unsafe parts in fewer, more scrutinized, better contained sections.
Because safe Rust is so constrained, Rust relies on unsafe much more than Java, and their need for intrinsics is about the same (although Java lets you turn off individual intrinsics at runtime).
Tons of production Rust systems use very little unsafe. I've been writing production Rust since 2015, and in that time, I wrote maybe 30 lines of unsafe production code. (Most of it in Tokio primitives.)

Now, in web-facing systems, the biggest concern is the low-level crypto libraries, which need to be written in assembly to avoid timing attacks. These (and related libraries) have seen multiple security advisories in the past year, some related to unsafe (I think?) and some related to logic errors. But overall, those advisories have mostly been lower priority, or affected mostly niche use cases.

If you only have a small amount of unsafe code, then it's possible to audit it more aggressively. And there's only a limited amount of space for bugs. So what I would expect for Rust is a trickle of security bugs, many of them niche or low severity, that slows down with time.

Do you use core abstractions like Vec, String, Box, Arc, and Mutex? If so, those are built using unsafe code internally.
loading story #50564412
loading story #50564080
Lets compare what's inside this Ruby bug, a C function rb_str_casemap which is 40-50 lines of C fiddling with pointers and calling into macros that presumably are familiar to Ruby developers but opaque to an outside viewer. Is it fixed? I guess so but I can't see very well to be sure, it was counting stuff before and now it is being more careful, I hope it's correct this time.

... against str::to_uppercase the Rust function which has a single line of unsafe code, with a two line rationale provided to explain why it's OK to do this. As a subject matter expert I agree with the rationale. LGTM.

Edited: We're talking about uppercase, they're basically mirrors, but we should choose the uppercase function.

loading story #50562512
I didn't say there's a lot of unsafe (by whatever measure you consider that to be). I said there's a lot more than in Java.
The JVM is written in an unsafe language. That's where these unsafe calls occur. Clients of these APIs have no control there. Java code itself is safe.
> That's where these unsafe calls occur.

No, it's not. A relatively common case for unsafe in Rust is in data structures. The data structures in the Java runtime are all written in Java and has almost no use of unsafe (there might be a few, but not becsause of language limitation, but because of initialisation order).

As for the JVM, much of it is a compiler. It doesn't matter what language it's written in, as it generates machine code. Indeed, the vulnerability disclosed yesterday was in a compiler intrinsic, but those are just as common in Rust.

Other than that, the other big piece in the JVM is memory management, and it does, indeed, use C and C++ for memory management, but Rust also uses C (the various implementations of malloc/free in whatever runtime the Rust program is using; BTW, some popular malloc/free implementations are almost as big as Java's most complicated GC).

As for native C/C++ libraries (outside the JVM itself), Java programs do use some, but generally not more than Rust programs.

You don’t think there’s some intrinsic code in llvm that would be generating assembly that doesn’t match Rust’s safety rules?
We know LLVM has miscompilation bugs. Sometimes it is practical for Rust to fix those bugs and so it carries patches for LLVM, but in some cases LLVM IR semantics are just incoherent nonsense. An example of the former (Rust carries patches): LLVM got confused about discriminants when Rust uses niches, when the niche bit pattern used was usually right next to the legitimate values for a type the bug in LLVM caused little notice, it could corrupt data but probably not. However Rust changed which bit patterns to prefer here, so e.g. None for Option<char> went from 0x0011_0000 to the bit pattern 0xFFFF_FFFF and now that same LLVM bug caused crashes as values which had previously been very slightly wrong were instead hugely wrong.

For an example of the latter (LLVM is just nonsense): LLVM still today has a bug found years ago where pointer provenance (a legitimate but hard to think about idea for optimising compilers) accidentally leaks into integers, and so LLVM can end up with two integers a, b which it believes are not equal and yet a - b == 0. Boom.

loading story #50563018
Rust is as vulnerable as the Java issue l, if you use simd you're in the same boat.
loading story #50564029
> Because safe Rust is so constrained, Rust relies on unsafe much more than Java, and their need for intrinsics is about the same (although Java lets you turn off individual intrinsics at runtime).

This is silly. Rust also has tooling for dealing with unsafe - tools for proving unsafe code, tools for verifying unsafe code, tools for fuzzing it, etc.

Java, and other languages like it, often drop into unsafe code where it's far weirder and less supported because they have to because the languages are too slow to do the work they want to do.

And obviously you have all of the complexity of a massive, JIT'd runtime written in C++.

loading story #50562295
That's just not correct
You're welcome to compare the prevalence of unsafe in Java and in Rust, both in the standard library and in user code, and you will see that this is very much correct.
OP didn’t say anything about Rust, by the way. This post comes off as being defensive about a point nobody made.
…it was a metaphor to agree with the OP’s point.

Not everyone on the web is looking to start an argument; some people use literary devices to reinforce their point. If anyone is defensive about a point nobody made, it’s you.

Yeah, "memory safe" goes out the window if you prefer to implement parts in assembly. This is where portable SIMD (e.g. the Java vuln was from SIMD), which is abstracted & safe but not necessarily as performant as it could be, is becoming a more reasonable trade off.

Of course I'm not sure how much it counts to be "using a memory safe language" when you have to call put to unsafe code anyways. I.e. just because the library you'd like to use is C but the rest of your project is Rust doesn't mean you're really using a me ory safe language, it means you're sometimes using one. We have a long way to go before environments allow using memory safety fully I think.

Is it not theoretically possible, even if it comes at the cost of performance?
Yes, ultimately it is about reducing the attack surface so that we get less exploits and also more reliable software (overflows can cause sporadic crashes). Still very much worth doing it IMO, even tough it doesn't sove all problems on its own. Apply defence in depth and combine this with other mitigation techniques. This is the path if you want robust systems.
You're conflating Java with the JVM but sure.
Rust it's far better than Java. Since the old times Java was seen as a CVE generating machine for GNU/Linux environments.
This ... isn't true? A lot of people perceived the Java plugin as being insecure in the browser, and indeed it was.

The perception of Java as being insecure on the server was only a thing amongst those who hadn't used it much since the Java plugin era.

That's why the Log4j vulnerability was a huge deal.

I like Rust, but this kind of anti-Java-think is super dated and boring.

Java CVEs were mostly holes in Java's own sandbox (e.g. used to run multiple applets from different websites in the same VM) which has been abandoned.
Complete nonsense. Many of the CVEs for Rust are things that just can't happen in the JVM. And many of the Java CVEs require uses of the JDK that few people ever use. You can dynamically load classes in the JVM (or you can turn that off). Its a nice feature that isn't used all that often anymore but when you need it, its very powerful. Lots of security folks made a living reporting CVEs around that. But its also why people are salty about CVEs and Java because they just reported the same security "hole" over and over again. Yes the JVM can load classes dynamically. If you don't need that, turn it off (or sign your jars). But if you do need it, then the CVE is largely irrelevant. That security researchers created this false impression of Java is a big part of why bug bounty programs aren't very popular anymore. Crying wolf gets you put on the ignore list.

PS Its the height of irony that the layer protecting Java webservices IS less secure than the Java webservice itself.

PPS The Java memory model is just superior to Rust's in every way...deal with it.

loading story #50562403
loading story #50562221