by carlmr · 3y ago · 0 comments
After all statistics just isn't on his side. Even the top engineers hired by Google create around 1 memory error every 1000 lines. And they didn't have one yet in 1.5 million lines of Rust [1] This is with all of the analysis tools at their disposal and in use for C++. And most companies can't afford the same top talent that Google can.
I've been working in safety critical software for a long time now. C and C++ will never be safe because their safety model relies on human infallibility. Ada and Rust IMO have the most potential here.
[1] https://security.googleblog.com/2022/12/memory-safe-language...
Using C++ for a greenfield project today should require some very good justification though.
Having to maintain legacy C++ applications is all the justification you need to have to understand that staring greenfield projects in C++ is quite frequently and by far the best option on the table
It makes absolutely no sense at all to multiply the tech stacks that a team needs to work on, let alone master, just because naive platitudes have been said about any novel tech stack. You do not make any application better or more secure if you get clueless developers onboarding onto new technologies and forcing them to maintain N+1 code bases and 2*n bindings.
This irrational dislike of C++ ignores the fact that adopting the latest C++ release and toggling static code analysis in your project, which nowadays comes for free, already gets any project way ahead of where naive retractors believe C++ is.
Except that security must be on by default, and it must be made hard (or shameful) to circumvent it. We all know any kind of protection is easy to bypass in C++.
I remember an article from a Julia developer quitting over the culture of using unsafe code that had installed in the community.
A mindset change is required by this profession. Too often (unverified) performance reasons have been used to justify risky code.
I'm not against improvements to C++ safety, I just think making C++ a safe language has provably failed.
The blog you linked to is a PR piece. A real study would go in-depth and look at the quality of the C and C++, particularities of the affected subsystems (such as a requirement to interact directly with HW, etc), split findings by subsystem and components and also link snippets of source code to CVEs. Hopefully Google will publish such a study someday instead of a few pieces of data and graphs.
But that’s not the major issue.
You’re taking as fact that Google’s Android C++ programmers are particularly interested in safety or are significantly more competent than average. Based on my occasional perusal of Android C++ code in a professional context I cannot confirm either assumption: Android C++ is not modern (not even C++11 modern) and is very pointer heavy. It’s an eclectic mix of C functions and C data types with C++ constructs.
Take a look at some random fixes for CVEs that I took from the December security bulletin:
* https://android.googlesource.com/platform/frameworks/minikin...
* https://android.googlesource.com/platform/packages/modules/B...
* https://android.googlesource.com/platform/packages/modules/B...
That looks like C with classes to me.
Perhaps it’s for the best if projects such as Android or Chrome, etc are rewritten in Rust. Catching stuff after the fact with sanitizers, fuzzing, etc will never work if the code is ignoring or can’t use most of the advances that Stroustrup was talking about.
Mostly because in the end they aren't doing what gets preached in spite of the in-house experts.
So how much hope have the regular C++ devs that don't take part in ISO C++ and compiler development tasks?
Under these circumstances, using C++ over decades has the results that can be seen - they can’t produce secure software.
I don’t think that Google or any other company can create secure products. I was actually surprised how many Java bugs were present in the December list.
The market, the incentives are not set up for that. Sure, using safe languages would help in a narrow sense, why not give it a try, but this is turning into scapegoating and silver bullet activism. Once they get rid of the most C and C++ code I bet their products will still be insecure, because the culture, the processes, the market and the people won’t have changed.
They simply don't follow their own advice and I don't think the tech world should be fawning over their proclamations half as much as they do. I am not surprised that they don't follow their own advice on C++ despite being on committee members - they don't follow their own bombastic statements on ANYTHING.
Like Larry Page appearing at the Android IO conference to much applause to berate Facebook for not federating XMPP and then the same day they turn off GChat access over XMPP themselves. They simply are not trustworthy against their own advice.
I wouldn't trust them to continue any software they write as they have a track record of chucking stuff in the bin after the initial announcement has faded.
But they do make a lot of money advertising to you and stalking you around the web.
It would be more interesting to compare only the code of C++ and Rust written in the same timeframe. My expectation is that C++ wouldn't look as bad as stated in the article.
If you never call .get() on a unique_ptr then you'll be okay. But that's often not a workable design.
I agree. The article reads like it started with w conclusion and cherry-picked data to fit that.
In high-quality C and C++ projects, about 70% of vulnerabilities are caused by memory errors. (https://alexgaynor.net/2020/may/27/science-on-memory-unsafet...)
It is to be expected that there is no bug in the very thing that Rust is supposed to prevent. The same can be said of Java, JS or even PHP. What surprises me a little though is that there is zero memory error even in unsafe Rust. There has to be some unsafe code somewhere, for low level data structures and things like that.
Even unsafe Rust has more strict typing and borrow checking which C++ doesn't have. It does allow raw pointers, but it's still safer than C++.
The other thing is that if say 1% of your loc is unsafe Rust, it's much easier to put this under more scrutiny than the whole C++ codebase.
And 1% is a lot if you ask me. I almost never need unsafe at all.
>There has to be some unsafe code somewhere, for low level data structures and things like that.
In most cases you can just use the standard library code for these. They do contain unsafe of course, but it's in one of the most battle tested places.
However memory errors in C++ account for 70% of vulnerabilities. I'd expect 30% to thus remain.
Edit: meant to write memory errors not vulns.
The good thing is that since this code was written in a memory safe language, at least that eliminates an entire class of bugs.
Chrome was 2008. No idea if the code base has been completely "modernised" or not.
They are some of the most hardened codebases, and still fail in light of the issues with C and C++.
This is identical to me writing a garbage sentence in English and then proclaiming that English as a language is wrong and needs to be replaced because it's perfectly possible to write something grammatically incorrect in it.
Surely you can see the difference but are conflating use of the language with the language itself.
Oodles of vulns in linux that are made possible by C's lack of safety guarantees.
I'm sorry, isn't your link about how a user found a memory error in the Linux codebase? It seems way farfetched to jump to your conclusion from that starting point. I mean, doesn't Linux have over 12 million lines of code right now?
One memory error per 1000 LOC? I think they probably either use old style C++ or something else weird and ignore all static and runtime memory safety tooling to get to this error rate in their releases.
>"And they didn't have one yet in 1.5 million lines of Rust"
I assume that those 1.5 "million lines" do not contain any unsafe features Rust allows for. Am I correct here?
From his link: "Historical vulnerability density is greater than 1/kLOC (1 vulnerability per thousand lines of code) in many of Android’s C/C++ components (e.g. media, Bluetooth, NFC, etc). Based on this historical vulnerability density, it’s likely that using Rust has already prevented hundreds of vulnerabilities from reaching production."
Not even Bjarne's continuous preaching seems to affect current practices, as most folks don't do social activities regarding their day job programming languages.
Because as Rust grows it is easier to model more behavior and less unsafe is needed. They mention this in the article.
> There are exactly two uses of unsafe in the UWB code: one to materialize a reference to a Rust object stored inside a Java object, and another for the teardown of the same. Unsafe was actively helpful in this situation because the extra attention on this code allowed us to discover a possible race condition and guard against it.
> In general, use of unsafe in Android’s Rust appears to be working as intended. It’s used rarely, and when it is used, it’s encapsulating behavior that’s easier to reason about and review for safety.
https://security.googleblog.com/2022/12/memory-safe-language...
I would assume that there are a few `unsafe` lines of code in these 1.5 MLoC. Which still makes it easier to trust than 1.5MLoC of C++ in which (by Rust definitions of `unsafe`) every line makes use of `unsafe` features.
(insert usual caveats about tradeoffs, etc.)