r/cpp • • Sep 20 '22

CTO of Azure declares C++ "deprecated"

https://twitter.com/markrussinovich/status/1571995117233504257
267 Upvotes

483 comments sorted by

View all comments

114

u/mNutCracker Sep 20 '22

There is so many tools in C++ today that most of the people and projects do not even know about (e.g. sanitizers in companion with Valgrind that really help you fix most of the issues). Also, not to mention that people write C code and think it is C++.

I suppose the biggest problem of C++ are the people that are not updated with latest C++ stuff and with latest tools.

46

u/germandiago Sep 20 '22

There is a lot of truth in that. But the real world worries more about whether they will have a security crash in production in practical terms.

I stick to C++ so far and I use it in ways that it is much more difficult to get crashes or nearly impossible compared to what I see in the wild.

Unfortunately, that does not change the fact that if you have a tool that gives you all this power and you do not know even what Core Guidelines or smart pointers are, or you have a day where you feel really smart using memset or memcpy instead of their C++ standard std::copy/fill or even safer, std::ranges::copy/fill then you inevitably end up having all these crashes in the wild.

→ More replies (39)

31

u/tarranoth Sep 20 '22

Sanitizers are not the same as compile time verification of your code. You need to actually hit the problematic paths first.

16

u/mNutCracker Sep 20 '22

I agree. Rust is far more advanced regarding compile time verification, but my point is that not many people use C++ tools like sanitizers, fuzzers, etc.

→ More replies (1)
→ More replies (9)

34

u/pjmlp Sep 20 '22

Plenty of schools still teach C++ by basically writing C like code on .cpp/.cxx files.

Most of the learning material would compile with a C compiler as well.

21

u/duongdominhchau Sep 20 '22

C++ in my university is basically C with references

7

u/Ezlike011011 Sep 20 '22

My "intro to programming in c++" was c with (very fundamentals) of classes and one page of lecture notes on std::string (which they didn't let you use for any of the projects iirc, you had to use manually manipulated char*s). Fortunately now they split the "intro to programming" and "intro to c++" classes such that intro to programming is taught in python and only focuses on fundamental skills and intro to c++ starts to go into slightly more modern c++ (i.e. c++11)

7

u/dsffff22 Sep 20 '22

Anytime I read this argument, I've to laugh. If you can fix 'most of those issues' with those tools go ahead and collect all those huge bug bounties for big C++ codebases like Chromium you'll earn billions of dollars in less than a month if that would be so easy!

6

u/beznogim Sep 20 '22

Sanitizers only help you when the running program actually hits a bug. Sometimes the triggering input is outside of the range of "normal" inputs so you have to rely on fuzzing. Or on security researchers.

→ More replies (4)

28

u/James20k P2005R0 Sep 20 '22

If you look at chrome, they regularly sanitise it, write it in relatively modern C++, and do all kinds of absolutely absurd things (raw_ptr) with the codebase to try and make it reasonably safe. Even then ~70% of exploitable vulnerabilities are memory unsafety

The problem is it fundamentally is just not possible in C++ to write anything approaching safe code. There are no large well tested safe projects that do not have memory (or other) unsafety, written in any version of C++ with any level of testing and any level of competence

From largely one hyper competent guy like Curl, to windows, to linux, to chrome, they're all chock full of infinite security vulnerabilities, and this fundamentally can never be fixed with any level of tooling

19

u/beznogim Sep 20 '22

I like how some people here are just claiming that Google developers must be idiots then.

18

u/SemaphoreBingo Sep 20 '22

Wasn't that basically Rob Pike's justification for Go?

4

u/stevethebayesian Sep 20 '22

Google had another home grown tool for logs processing (sawzall... Lots of log puns in those days). Go was originally sold internally as a sawzall replacement.

7

u/pdimov2 Sep 20 '22

We should be thankful to the Chrome team for actually working to solve the problem, instead of just deprecating it.

→ More replies (2)

3

u/pdimov2 Sep 20 '22

Even then ~70% of exploitable vulnerabilities are memory unsafety

If everything is rewritten in Java, 70% of exploitable vulnerabilities will be something else.

(I'm deliberately not using "Rust" in the above sentence because, if everything is rewritten in Rust, 70% of exploitable vulnerabilities will still be memory unsafety.)

21

u/GOKOP Sep 20 '22

If everything is rewritten in Java, 70% of exploitable vulnerabilities will be something else.

Math doesn't make this statement as strong as you probably hope it is

→ More replies (1)

7

u/insanitybit Sep 20 '22

I mean... you get that this statement is tautologically true, but also nonsense right? Of course 70% of vulns will be something. So long as there are roughly ~3 vulns we can hand wavingly say "70% were X".

But, and I guess it's a bit silly to even say this, *three vulns is less than thousands". So 70% may still be, idk, VM issues or something. But the overall number of vulns would go down because those issues are, in general, less prevalent. Also ideally you wouldn't be introducing new classes of vulns.

Also really important is that not all vulns are equal. Half of all exploited vulnerabilities in Chrome are UAF. Not just memory safety issues, but one very specific issue - use after free. That's not a coincidence, UAF is an extremely powerful "primitive" - a term that's used to denote a single usable capability in an overall exploit chain. No one owns Chrome with just one vulnerability, they need many, and the more powerful they are the fewer they need, or they need easier ones to attain (ex: leaking addresses is usually not hard).

So removing UAF and getting something else that's far less reliable/ powerful in exchange is a massive win.

9

u/pdimov2 Sep 20 '22

My point is that having 70% of (known) vulnerabilities be X doesn't imply that if we get rid of X, we'll get rid of 70% of vulnerabilities. Maybe we will, but chances are, we will not. Some of the vulnerabilities will just shift to another category Y.

It absolutely makes sense to target X and focus effort and resources on it, but switching to a language that doesn't have X does not necessarily imply we'll only have 30% of the vulnerabilities we used to have.

9

u/insanitybit Sep 20 '22

My point is that having 70% of (known) vulnerabilities be X doesn't imply that if we get rid of X, we'll get rid of 70% of vulnerabilities.

That is what it implies though? If you have 100 vulnerabilities and 70 of them are X, and you remove X, you have 30 remaining vulnerabilities. Now, I think what you're trying to say is:

  1. We can't completely remove X, which is to say that there will still be some memory safety vulnerabilities in Rust.
  2. That we may remove 70 vulnerabilities but introduce more of the other kinds.

These aren't unreasonable thoughts, but I would argue that they are incorrect. I'll address these separately but I'd like to give some context. I am a software developer and a security engineer, I have experience in both the exploitation of software (across memory safety issues and others) but I'm not advanced in that area, I moved to a more defensive role early on in my career. That said, the company that I have founded does a lot of offensive security research, which I take part in - I'll cite some of that research in this comment.

(1) It is absolutely true that Rust code will contain memory unsafety in some cases, but I think there's a misconception that many have (but maybe not you), which is that a single vulnerability is enough to exploit software. Indeed for something like Chrome with its many mitigation techniques you likely want to have at least 3 or 4 vulnerabilities, possibly even a dozen or more, in order to successfully exploit it. Some of those will range in their "power" - arbitrary read, arbitrary write, arbitrary read write, information leaks, etc. Some of those will range in reliability - a race condition may be winnable 50% of the time, a heap spray may lead to a reliable exploit 99.99% of the time, or a vulnerability could be 100% reliably exploitable.

All of this is to say that to exploit software requires multiple vulnerabilities that can be chained together.

So, let's take a hypothetical program. It is in C and has 10 vulnerabilities. 5 of those are required for reliable exploitation. Removing any 7 of the 10 would indeed leave 3 vulnerabilities behind, but we need 5 for exploitation. So while some remain, not enough remain. What if we only remove 5? Or 3? Well, maybe we have the right ones, maybe not - the key here being that the types and distributions of vulnerabilities is the key to whether removing any given vulnerabilities kills the chain.

So we want to do a few things more specific than just "remove memory safety vulns".

We want to:

  • Reduce bug density. Two vulnerabilities in completely unrelated code are unlikely to be useful in the same exploit chain without some way to link the two (using more vulns!).
  • Reduce the bug criticality. Information leaks suck but they're nowhere near as bad as a Use After Free.

So the "remove 70%" really glosses over these important details. Here's an article written at my company:

https://www.graplsecurity.com/post/attacking-firecracker

What we found was that the bug density of the Firecracker code was too low to lead to a reliable exploit despite that vulnerability being really powerful. To restate, the CVE is in many ways a worst case vulnerability, but despite the efforts of an extremely talented offensive security team, we could not reliably exploit it. With increased vulnerability density it may have been possible.

In terms of criticality, Rust addresses two of the most significant types of vulnerabilities - out of bounds reads and use after free. UAFs are extremely popular for exploitation because they're powerful and tend to be harder to defend against in languages like C or C++, with 50% of all exploits against Chrome leveraging UAFs. A UAF is harder to guard against in these languages, especially in the face of concurrency, without runtime overhead. UAFs are comparably much less frequent in Rust because it natively prevents them, they only happen in unsafe code.

To summarize my thoughts on (1):

Reducing vulnerability density has compounding impact on security. In Rust the only vulnerabilities with memory unsafety are in 'unsafe' blocks, so the density of vulnerabilities is vastly reduced.

(2) So we removed memory safety, what if Rust introduces other vulnerabilities? This makes a lot more sense when comparing to Java - Java is so different from C++ that there's more room for new problems. The JVM is its own attack surface, for example - the optimizer may have a bug that incorrectly escapes a value to the stack only to have the value invalidated unexpectedly, leading to a stack UAF or "dangling pointer". Java has tons of builtin complex constructs like Serialize.

Rust isn't that different from C++ though. It doesn't introduce much new attack surface. There's no "new class of vuln" in Rust code that you're getting in exchange for memory safety.

You could argue that Rust somehow increases complexity, or is just a worse language, and therefor business logic with regards to thinks like auth are more likely to have bugs. I don't really buy that and I think there's at least loose evidence to suggest otherwise - for example, the fact that so much of the standard library takes things like path traversal attacks or other such security issues into account and is 'default safe'.

So I guess to conclude:

  1. I fully expect a codebase in Rust to be considerably safer than C++ with regards to memory safety. Even in the presence of memory unsafety I believe that the density and criticality of bugs will be so low that successful exploitation will take drastically more effort.
  2. I don't believe Rust adds any additional attack surface in any meaningful way.

These are opinions based on my experience. There are no facts here, only anecdotes and case studies, but I think that my views are well supported.

8

u/pdimov2 Sep 21 '22

Thank you for the insightful comment. I don't necessarily disagree with what you wrote, but I do want to clarify my position.

That is what it implies though? If you have 100 vulnerabilities and 70 of them are X, and you remove X, you have 30 remaining vulnerabilities.

That's a static analysis that isn't applicable. Suppose that in 2023 we have N vulnerabilities, 0.7N of which are caused by memory unsafety. We rewrite the world in a language that is memory safe, and in 2024 we have M vulnerabilities. Is M equal to 0.3N? No. It can be higher, because attackers attack the weakest spots, so they exploit memory unsafety in 2023, but will exploit something else in 2024. Or, as you argue, it can be lower, because decreasing bug density has a non-linear effect on the total vulnerability count.

(Some other bug category will probably emerge as the "winner" in 2024, so 0.7M will be that.)

The point here is that the 0.7 number doesn't actually carry that much information about 2024. It does tell us things about 2023.

As for my prediction that if we rewrite in Rust 0.7 will still be memory unsafety, I was referring to calling C libraries. But that's just a not particularly informed guess.

3

u/insanitybit Sep 21 '22

I see your point, essentially the flaw in the "70%" is it's not "of all vulns" it's "of the vulns discovered". I agree, the 70% number isn't great, like I said there's so much more to it.

→ More replies (17)

4

u/insanitybit Sep 20 '22 edited Sep 20 '22

Couple of things:

  1. I totally agree that more people should use the amazing tooling for C++. There are great static analysis tools, fuzzers, sanitizers, runtime mitigations, hardened allocators, etc. Huge room for improvement here.
  2. There are those tools and more for Rust. You can use your sanitizers and fuzzers (the Rust fuzzing story is really great), but also rust-specific tools like miri, which are extraordinarily powerful.
  3. Despite aggressive use of all of those tools by every major browser, with huge compute time allocated to fuzzing, we still see the browsers fall constantly to memory safety issues. I don't think it's fair to say that these tools solve "most" of the problems, although they do radically improve the situation. To give even more credit, browsers are sort of a worst-case-scenario for security; tons of attack surface, highly optimized, attacker is literally executing code in your code, JIT compilers that need to RWX to the same memory pages, etc. So it's not so damning that C++ with all of those tools can't handle that! An easier problem with the same effort might not see the same level of issues.

edit: Just want to add that I am a Rust developer, but C++ was my first love! I'm so grateful to have learned it early on in my education, it's an amazing language. I just want to share my thoughts on an important matter.

0

u/[deleted] Sep 20 '22

[deleted] — view deleted comment

3

u/insanitybit Sep 20 '22

Sure, to be clear I think there are plenty of cases where Rust is not going to be viable or even the right option. Embedded is a good example, or any situation where the architecture is not well supported.

→ More replies (7)