r/rust • u/TheoryShort7304 • Sep 27 '24
Google's Shift to Rust Programming Cuts Android Memory Vulnerabilities by 52%
https://thehackernews.com/2024/09/googles-shift-to-rust-programming-cuts.html?m=1This is really good news!! šš«”š
372
u/letheed Sep 27 '24
Itās a cut by 52 percentage points of all vulnerabilities. Thatās a reduction of 68% in relative numbers, from 76% to 24%. But the actual reduction is more impressive than that since the total number of vulnerabilities is also falling. Looking at the graph, from ~222 in 2019 to 36 thatās a reduction of ~84% in absolute numbers.
36
u/dlampach Sep 27 '24
I was thinking that it actually sounded low. You have to work hard to create these kinds of vulnerabilities in rust.
55
u/PeaceBear0 Sep 27 '24
They haven't replaced all their c code with rust, just some of it. These numbers are referring to their entire code base of rust + c. Iirc there have been 0 memory vulnerabilities in the rust code.
16
u/SirClueless Sep 27 '24
Iirc there have been 0 memory vulnerabilities in the rust code.
I think the metrics there are a bit rigged, though.
When Rust code triggers memory unsafety in a C library, we consider it a bug in the C code because the C code is unsafe. When C code triggers memory unsafety in a Rust library, we consider it a bug in the C code because the C code didn't uphold the invariants of the Rust interface.
If you ask a Rust programmer about this, they will likely say that this is defensible because if the interface were specified as a pure Rust interface, then safety would be checked by the compiler and guaranteed. But this ignores the fact that if an interface is specified in pure Rust then it cannot possibly have a stable ABI and both sides must necessarily be statically compiled together into one binary. So memory safety bugs intrinsic to an interface that must be stable and exposed as a dynamic library or to a syscall are never attributed to Rust even if a Rust library is involved. Rust is just really good at pushing the blame for memory vulnerabilities outside of itself -- if the whole program is Rust then maybe you've eliminated them entirely, but if the program is Rust-with-a-C-interface then maybe you've just concentrated them into the API layer.
To be clear: I'm not saying the Rust code isn't reducing vulnerabilities by reducing the surface area where tricky invariants must be manually upheld to a small amount of interface code instead of the whole program, I'm just saying that whole-program vulnerability reduction is a more important than trying to attribute blame to the Rust or C code in particular because the Rust code is really good at saying "not me" even for bugs that involve both languages.
30
Sep 27 '24
Rust doesn't move the blame off of itself unfairly, it's simply extremely difficult for the human brain to maintain the vast array of invariants that memory safety requires. Programmers are shit at memory safety, so of course interfacing with a memory-unsafe language is going to naturally result in memory unsafety.
5
u/SirClueless Sep 27 '24
My point is not that it's "unfair," it's that it doesn't extrapolate well. If you want to measure how impactful Rust can be in the android kernel for reducing vulnerabilities, the first-order metric you care about is how much Android code can productively be switched to Rust, not how many vulnerabilities can be attributed to Rust code or not.
Rust is a memory-safe language, so except for improper use of
unsafeand perhaps an unsoundness bug, memory-safety bugs are not going to be attributable to Rust code. It's useful to know that no such bugs have occurred, but the limiting factor here is not whether Rust code is 99% safe or 99.9% safe or 99.999% safe, it's whether 20% or 50% or 90% of the Android codebase could conceivably be written in Rust -- I would argue that due to not offering a stable ABI and requiring static linkage, it's probably not more than ~80% even in the best conceivable case, and in practice it is going to remain a lot less than that for a very long time even if they invest heavily.5
u/PeaceBear0 Sep 27 '24
When Rust code triggers memory unsafety in a C library, we consider it a bug in the C code because the C code is unsafe. When C code triggers memory unsafety in a Rust library, we consider it a bug in the C code because the C code didn't uphold the invariants of the Rust interface.
Is this based on example vulnerabilities in Android that you can link? Or based on guidelines for placing blame? Or is this just your opinion on how blame should be assigned?
0
u/SirClueless Sep 27 '24
It's based on reasoning from first principles about Rust's memory safety guarantees and where they apply (namely, in safe Rust called from safe Rust, and not anywhere else).
7
u/PeaceBear0 Sep 27 '24
How can you say their methodology is flawed when you haven't looked at their methodology?
6
u/SirClueless Sep 27 '24
I'm not saying their methodology is flawed. I'm saying I already know to expect a very small number (probably zero modulo some abuse of
unsafe) of memory safety bugs in Rust code, so what I actually care to measure is how much code has been written to Rust and how many memory safety bugs were in that code beforehand or otherwise."We wrote 100,000 lines of Rust code in the Android kernel and had 50% fewer memory safety bugs this year than before" -> big win!
"We wrote 5,000 lines of Rust code, and it had zero memory safety bugs in it!!!" -> Okay, but I don't really care.
2
u/PeaceBear0 Sep 27 '24
Yeah that makes sense. I guess I misinterpreted your "I think the metrics there are a bit rigged, though" comment
5
1
u/buwlerman Sep 29 '24
Depends. If a Rust C FFI uses the C API in a way that isn't compliant with its contract the Rust code is to blame. Similarly, if C code uses a Rust C FFI in compliance with its contract but still triggers memory unsafety that's a bug in the Rust code.
It's also nice to know that the non-FFI unsafe code hasn't caused many vulnerabilities. A common quip against Rust from some C and C++ programmers is that the
unsafekeyword means that Rust has to choose between low level control and safety, while the truth is that at scaleunsafeallows making unsafe reasoning more contained and local which helps safety without harming control much in practice.1
u/RCoder01 Sep 27 '24
2024 isnāt over yet though, so unless the 2019 number is only from like the first three quarters, I think that number might be overstated. Still a great improvement though.
10
u/letheed Sep 27 '24
If I remembrer well, 27 is the current number and 36 for the year is what they extrapolated.
1
u/Repulsive-Street-307 Sep 28 '24
The specialists can concentrate on tricky parts instead of off by ones...
1
14
Sep 27 '24
Rust on Android! I just might get into Android development. Some native rust gui will be awesome.
2
10
u/rebootyourbrainstem Sep 27 '24
The Google dev post this is based on was discussed two days ago here
6
u/faitswulff Sep 28 '24
At first I was wondering how many new memory vulnerabilities were found in Rust code, but then I remembered this bit of a previous blog post:
Google Online Security Blog: Memory Safe Languages in Android 13
To date, there have been zero memory safety vulnerabilities discovered in Androidās Rust code.
2
u/-Redstoneboi- Sep 28 '24 edited Sep 28 '24
We donāt expect that number to stay zero forever
so yeah, still something to ask. two ways that count would be:
making pure-rust unsafe functions that are unsound
incorrectly using C bindings inside the wrapper functions
incorrectly encoding the invariants of the C bindings in the wrapper functions
but not:
correctly using C bindings, but failing due to an undocumented invariant
discovering a bug in the C code
15
u/mytosus Sep 27 '24
It also goes without saying that much of the decrease in such flaws is down to advancements in the ways devised to combat them, moving from reactive patching to proactive mitigating to proactive vulnerability discovery using tools like Clang sanitizers.
So much of the decrease in memory vulnerabilities was due to using vulnerability discovery tools rather than using memory safe languages or am I reading this completely wrong?
15
u/steveklabnik1 rust Sep 27 '24
It's an "and," not a "rather." The move to memory safe languages means no new vulnerabilities are being introduced. (They said before they haven't found one yet, I don't know if that's still true or not, but it's certainly far less than before.) At the same time, these tools are helping mitigate things on the unsafe side. Both contribute.
2
u/ninja_tokumei Sep 27 '24
That is also true, but it's a combination of both.
One of the linked articles suggests that most vulnerabilities in memory-unsafe code are found in newer parts of the code. At any point in time, most bugs and vulnerabilities that are in a codebase are in the newer parts, which then get found and fixed over time as it gets tested more.
They've probably been using the analysis tools for much longer, but using memory-safe languages has decreased the rate at which new vulnerabilities are created. At the same time, the previous memory-unsafe code is being tested and is stabilizing as the existing vulns are fixed.
4
u/-Redstoneboi- Sep 28 '24 edited Sep 28 '24
"cuts android memory vulnerabilities by 52%"
the percentage of memory vulnerabilities dropped from 76% to 24%
76% - 24% = 52%
Percentage trap. Happens to the best of us. Let me illustrate:
If I dropped from 50 bugs down to 0 bugs, that's not "cutting the bugs by 50%". That's cutting them by 100% because there are none left.
if I dropped from 50 bugs to 10 bugs, that's not cutting by 40%. That's cutting by 80% because 50 - (80% of 50) = 10.
If I dropped from 76 to 24, that's closer to cutting by 68.42%. Of course, these are "mem safety vulnerabilities / total vulnerabilities" so it's not exactly accurate, either, but it's one step closer.
2
u/bwainfweeze Sep 28 '24
Iām having a real hard time understanding what the āpercentage of memory vulnerabilitiesā means in that article. Vulnerabilities are usually counted with numbers, not fractions.
Is that percentage of overall vulnerabilities? Percentage of lines of code that have memory vulnerabilities? Percentage of apps with vulnerabilities?
2
u/-Redstoneboi- Sep 28 '24
Number of memory safety vulnerabilities Divided by Number of vulnerabilities in general
there's also another metric:
As a result, the number of memory safety vulnerabilities discovered in the operating system has declined from 223 in 2019 to less than 50 in 2024.
77% decline in number of mem safety vulnerabilities discovered, whatever that means.
2
u/theMachine0094 Sep 27 '24
This is good but Iād like to see the bar graph go back a few years before they started using memory safe languages. Otherwise the correlation is a bit sus.
2
u/Tiflotin Sep 28 '24
Prove it by letting us completely bootstrap an android app without an gradle project :)
2
2
1
1
u/Slimxshadyx Sep 27 '24
Is Google rewriting the android os in rust? I looked through the article, and I see they mention maintaining support for Rust, C++, and Kotlin, but that would be for application development, right? Am hoping someone could give some more insight.
3
u/-Redstoneboi- Sep 28 '24
They are focused on writing new code in Rust.
They found that most memory vulnerabilities were found in new code, and that vulnerabilities in old code "decay" and get fixed over time. Rewrites are not necessary for them, since they found it's more efficient to stop writing bugs in the first place.
3
Sep 28 '24
Rewrites are not necessary for them
They are planning to rewrite font-stuff (eg:
harbuzz). To quote from harfbuzz READMEHarfBuzz is used in Android, Chrome, ChromeOS, Firefox, GNOME, GTK+, KDE, Qt, LibreOffice, OpenJDK, XeTeX, PlayStation, Microsoft Edge, Adobe Photoshop, Illustrator, InDesign, Godot Engine, Unreal Engine, and other places.
-80
u/ashleigh_dashie Sep 27 '24
Cuts Android Memory Vulnerabilities by 52%
What the hell does this actually mean?
Either vulnerabilities are real, there's still half of them left in, and thus android is still dogshit
OR
Vulnerabilities are irrelevant.
Modern software development processes are a circus.
48
u/FractalFir rustc_codegen_clr Sep 27 '24
This means that introducing Rust and Kotlin reduced the number of vulnerabilities by 52%.
There will always be some bugs, that is inevitable. Also, all the bugs mentioned in the article have been fixed.
This is a bit similar to people saying that they decreased server downtime. Obviously, you want to aim for 0 downtime(and zero vluns), but that is not possible. A 52% reduction is still great, even if it is not 100%.
Also, just writing new stuff in Rust, without rewriting the old things, reduced memory issues by 53%, project wide. So, they are saying that just using Rust for the new stuff already brings you a lot of benefits.
6
Sep 27 '24
Most vulnerabilities don't lead to exploits, just crashing, freezing, corruption, or some other kind of undefined behavior. You can pretty much guarantee that any modern OS has memory vulnerabilities, but there are also a lot of security features that prevent these vulnerabilities from being exploited.
5
u/123952 Sep 27 '24 edited Sep 27 '24
It means that, of the total number of vulerabilities discovered and patched each year in android, the percentage that were memory safety vulns has dropped. It was 76% of all vulns in 2019 and now 24% in 2024.
And to be fair, everything has vulns discovered and fixed regularly. Chrome, ios, windows, etc.
I mean just a few years ago one of the biggest exploit buyers stopped accepting new ios vulns for a few months because there were way too many new ios vulns.
1
u/-Redstoneboi- Sep 28 '24 edited Sep 28 '24
Yeah. Such a shitshow nowadays. People always talk about "reducing the impact of security vulnerabilities" and "reducing the number of bugs" and "mitigating risks" but not a SINGLE PERSON on God's green Earth has ever thought about just... not having any bugs?
Like, just don't make mistakes. My cousin's grandpa once knew a guy who can write 10,000 lines of C code just fine without any issues. If you make mistakes, you shouldn't be a developer. /s
127
u/nevi-me Sep 27 '24
I understood the blog to talk about safe languages, and not just Rust. How useful is it anyway when news outlets regurgitate blogs and often misinterpret the conclusions or what's said there?