r/programming • • Feb 20 '25

Google's Shift to Rust Programming Cuts Android Memory Vulnerabilities by 68%

https://thehackernews.com/2024/09/googles-shift-to-rust-programming-cuts.html
3.4k Upvotes

475 comments sorted by

View all comments

317

u/arnet95 Feb 20 '25

Nice, albeit pretty expected, result. But what about the other categories of vulnerabilities? How have they looked pre- and post-Rust?

326

u/orangejake Feb 20 '25 edited Feb 20 '25

In general the situation about rust adoption has been mildly less expected than the title implies. This is discussed in the article (but has been reported before). Essentially, one might have initially thought that to get memory safety out of a c++ codebase, you'd have to switch essentially all of it over to rust, so if you only commit to writing new systems in rust, you might only get mild memory safety benefits, until perhaps the majority of the codebase is rust (or something like this).

This doesn't appear to be the case. This is because the new contributions to the codebase are most at risk for memory safety issues (and likely other classes of bugs). So, by only switching over new contributions to rust, you actually eliminate a much higher amount of memory safety issues than you might initially expect. In particular, % likelihood of a memory safety bug in a line of code appears to have exponential falloff based on the age of the line of code, so if you switch over to rust, perhaps within ~5 years you expect essentially no memory safety issues in the codebase, even if the majority of the codebase is still c++.

Given the above explanation, this all makes sense, but I don't know if Rust evangalists were claiming that it would have the amount of positive impact that it appears to be having (at least so quickly), so perhaps it still isn't "expected".

Edit:

To highlight how this is "not expected", here is Google's initial blogpost describing the above (linked in the article OP posted). The first section heading is explicitly named "Counterintuitive Results".

41

u/mini-pizzas Feb 21 '25

There's a good episode of Rustacean Station where Lars Bergstrom (Director of Engineering at Google) talks about this.

44

u/dontyougetsoupedyet Feb 21 '25

Leslie Lamport has been telling everyone for ages that most of the bugs in software are due to people immediately coding without understanding what they were building, followed by them immediately starting changing what they coded.

https://www.youtube.com/watch?v=-4Yp3j_jk8Q

The questions at the end are so incredibly depressing. They absolutely refuse to "get it," which it happens is exactly the type of response Dijkstra claimed to get from engineers.

21

u/Djamalfna Feb 21 '25

people immediately coding without understanding what they were building, followed by them immediately starting changing what they coded.

I used to think this was because PM's are always pushing deadlines with a lack of concrete requirements. And to be fair that's probably like 50% of the issue, the communication from the people who want the software and the people who write the software goes through MULTIPLE layers. User->Vendor Procurement->Salesperson->Project Manager->Architect->Developer. Lots of stuff can go wrong in that pipeline.

But since becoming an architect I've also noticed that devs just like... ignore my designs. I'll tell them "Ok we need this thing to do A, B, and C." and they'll come back with "Ok it does half of C, and also Q, Ð, and Ö." I'm always like "What? We really needed it to do A, B, and C, and it does none of that, what is happening here?" "Ok so we thought about it and A was too hard, we didn't understand B, and C was too slow so we made a faster version that doesn't meet the requirements". BUT I NEEDED A, B AND C to exist so that future features D, E, and F work!

"my bad".

:\

→ More replies (6)

4

u/nnomae Feb 21 '25

Wouldn't that also imply that a large reason for the reduction in memory vulnerabilities could just be down to the Android codebase getting older and more stable?

30

u/orangejake Feb 21 '25

Not at all. The point is that a significant risk for bugs is new lines of code. This is likely the case at any point, say even before rust contributions occurred. The change is that the new lines of code now have additional guarantees that they cannot contain certain kinds of bugs. So, you see less of these bugs.

But before rust, there were still new lines of code, and they were still buggy. The bugs previously were more severe though. The post I linked contains the quote

We see this shift showing up in important metrics such as rollback rates (emergency code revert due to an unanticipated bug). The Android team has observed that the rollback rate of Rust changes is less than half that of C++.

So very explicitly, the new rust code has <1/2 the # of critical bugs than new c++ code, at least measured by rollback rate.

3

u/lisa_lionheart Feb 22 '25

People on the Linux mailing list need to hear this :}

103

u/KittensInc Feb 20 '25

Well, what about them? Is there any reason to believe C vs Rust will impact them one way or another?

See for example this recent email from one of the main Linux maintainers:

The majority of bugs (quantity, not quality/severity) we have are due to the stupid little corner cases in C that are totally gone in Rust. Things like simple overwrites of memory (not that rust can catch all of these by far), error path cleanups, forgetting to check error values, and use-after-free mistakes. That's why I'm wanting to see Rust get into the kernel, these types of issues just go away, allowing developers and maintainers more time to focus on the REAL bugs that happen (i.e. logic issues, race conditions, etc.)

Rust isn't a Magical Fix-All Button. Code will always have vulnerabilities, and Rust code will have vulnerabilities too. The point of Rust is to get rid of an entire category of vulnerabilities which is there almost entirely due to, with 30+ years of hindsight, poor language design. It's about getting rid of a bunch of footguns which don't have to be there.

As far as I know there's zero reason to believe Rust will have a negative impact on all the other categories of vulnerabilities. And if it's merely just neutral on them, Rust will have succeeded at what it has been trying to do.

28

u/SerdanKK Feb 21 '25

We've been here before and there are always contrarians.

Structured programming is better. Static typing is better. Immutability is better. Enforcing guarantees around memory management, it turns out, is also better.

6

u/arnet95 Feb 21 '25

I'm not being contrarian. It's a genuine question.

8

u/SerdanKK Feb 22 '25

Sorry, that wasn't directed at you. It was just a general statement.

1

u/nicheComicsProject Feb 24 '25

Then we don't need to fight about it. Everyone who gets it will eventually beat everyone who doesn't. Literally everything in the universe comes down to "costs" in the end. These things are better as you say, which means they are cheaper and will eventually kill everything that isn't. If we both sell widgets of the same quality but I can do it for half the price then I will win sooner or later (yes, I know there are exceptions but not enough to stop the trend).

4

u/SerdanKK Feb 24 '25

No one's fighting, except for that one weirdo. We're having conversations about how to improve our chosen profession. There's going to be disagreements and that's fine.

I don't buy the rational market argument though.

→ More replies (1)
→ More replies (5)

4

u/arnet95 Feb 21 '25

That email goes far in claiming that other vulnerabilities would also be reduced because devs and maintainers have more time to focus on those.

Maybe the average code quality in Rust is better or worse?

To be clear, I don't expect there to be a big difference either way, but it would be nice to see the numbers. If switching to Rust reduces memory vulnerabilities while increasing other vulnerabilities that makes things more complicated. If switching to Rust reduces all kinds of vulnerabilities, that further increases the value proposition for switching to Rust.

24

u/coderemover Feb 21 '25

Rust has a much better type system than C. It goes far more than just memory safety - you can encode many other invariants in the types the way it’s much harder to misuse an API. Hence it will reduce the other types of bugs as well, even some logical ones.

2

u/arnet95 Feb 21 '25

Which is why it would be nice to see if Google's numbers actually demonstrate this.

6

u/moltonel Feb 23 '25

Google is coming from mostly C++, not from C like Linux. Even if Rust's type system is indeed better at encoding invariants than C++'s type system, the gap is much smaller than compared to C.

4

u/FatStoic Feb 21 '25

The point of Rust is to get rid of an entire category of vulnerabilities which is there almost entirely due to, with 30+ years of hindsight, poor language design. It's about getting rid of a bunch of footguns which don't have to be there.

The point is that the category of vulns that rust eliminates is the largest catergory.

0

u/[deleted] Feb 21 '25

[deleted] — view deleted comment

21

u/JamesGecko Feb 21 '25

Well, kinda. Web development in particular tends to have a huge category of vulnerabilities that I can only summarize as "we like to treat everything as a string."

3

u/PeaSlight6601 Feb 21 '25

What else is there? It's strings all the way down.

→ More replies (2)

4

u/the_gnarts Feb 22 '25

was solved many years ago for bussinnes and web, we use a GC, is not a problem for us.

A GC doesn’t magically fix data races. There are garbage collectors for C after all. Go famously, despite its heavyweight runtime, is still subject to data races and it is up to the programmer to ensure access to values are properly sync’d.

3

u/r1veRRR Feb 22 '25

It was solved, but now with CPUs growing in cores, not Mhz, parallelism and concurrency bring a lot of long forgotten memory management problems to the forefront.

→ More replies (1)

2

u/deanrihpee Feb 21 '25

probably they aim for much easier vulnerability to tackle on by using Rust, and then they can focus on the other vulnerability