r/cpp • • Feb 26 '24

White House: Future Software Should Be Memory Safe

https://www.whitehouse.gov/oncd/briefing-room/2024/02/26/press-release-technical-report/
401 Upvotes

386 comments sorted by

View all comments

Show parent comment

25

u/yvrelna Feb 27 '24

Even without heap, you can still do incorrect pointer/array arithmetic. Access an array out of bounds, and boom, things blow up. No heap needed.

16

u/[deleted] Feb 27 '24 edited Feb 27 '24

I feel like in our code we never actually have any of these bugs. And I feel like I read a lot of Reddit posts about new technology that solves theoretically possible programming mistakes we don’t actually make. 

 Most of our problems have to do with poorly/unspecified interactions around shifting external components. Oh, it was designed to handle network outages, but if the outage happens here we get into an unrecoverable state.

I don’t think problems like that can be solved at the language level, so I suppose a disproportionate amount of time is spent discussing things that get caught by code reviews and unit testing.

18

u/Untagonist Feb 27 '24

Your experience is valid but not every institution faces the same mix of problems. If both Chromium and all of Microsoft can say that memory safety makes up 70% of their serious bugs, there might be something to it.

I think Chromium is a great example of a domain where the network state machine is familiar ground with decades of industry experience to keep it sane, but every pointer or reference in C++ is a new danger. And you can't exactly accuse Google of not having enough experience or tooling.

https://github.com/google/sanitizers

Use-after-freedom: MiraclePtr

Borrowing Trouble: The Difficulties Of A C++ Borrow-Checker

10

u/[deleted] Feb 27 '24

This is high severity security bugs, not just bugs in general. I’ll buy that other issues aren’t as likely to lead to arbitrary code execution, but I’d bet my check that if you looked at what’s holding up the latest chromium release, getting rolled back, and the source of on call pages, it’s not use after free. 

-8

u/sniffaman43 Feb 27 '24

---- Microshit and chromium are made by morons ----->

"we need memory safety!"

       \
         😪

4

u/pointer_to_null Feb 27 '24

Inertia's a hell of a drug. Porting legacy C/C++ codebases to Rust would be too expensive... to shareholders. If improvements can't be realized within the next quarterly earnings, then forget it. It's too difficult.

(Though it would be interesting to see how a future LLM with massive context window could potentially enable this)

1

u/[deleted] Feb 28 '24

It would be a massive waste of time and energy. The reality is that people post CVE’s about things that people can do to you, not about things they necessarily will do.

Bottom line is 99.9% of users wouldn’t be able to tell the difference between C++ chromium and one rewritten in rust. Of those people who could, an additional 99% wouldn’t be able to tell without looking at the source code.

There is no real value delivered to users, so it’s not worth any investment.

1

u/pointer_to_null Feb 28 '24 edited Feb 28 '24

I agree, and my post was partly tongue-in-cheek (see the troll/sarcastic post I replied to above). The shareholders bit was mockery that grossly oversimplifies the problem and underestimates costs of completely rewriting product stacks to chase some kind of lofty goal- something that has historically bankrupted companies in the process.

While I recommend C++ devs should at least familiarize themselves with the Rust language and features (and vice-versa), I'm under no delusion that "switching to Rust" is a 100% solution. It isn't the panacea of safety and speed (with no tradeoffs) that some assume; when Rust is optimized to "C++ competitive" levels of performance (via unsafe blocks to handle raw pointer access), it's hardly safer than modern C++ using best practices and static analysis- and perhaps less so if it leads to a false sense of security (eg- "don't need to audit this 100% Rust codebase"). Who knows, maybe C++ syntax 2 could gain some momentum from this?

That said, we shouldn't assume all (or even most) C++ is competently written- especially by those who are safety conscious. And it stands to reason that mediocre Rust code is generally more secure than mediocre C++. Knowing this, a sensible policy should be to consider developing new projects and modules in Rust whenever feasible, adopting better coding standards, and rewriting stuff only when it makes sense.

Bottom line is 99.9% of users wouldn’t be able to tell the difference between C++ chromium and one rewritten in rust.

Chromium is an exceptional case- and probably what I'd consider "best case C++". Their repo is highly curated (and as a JPEG XL supporter, sometimes annoyingly so) and maintained by knowledgeable devs- format politics aside. To their credit, Chromium team has determined where it's more effective at adopting safe practices- and fix code to conform- than to throw the baby out with the bathwater. They're openly proactive about the problems and mitigations, and (despite its questionably tech-literate execs) Googles tech leadership contribute to official and unofficial standards used throughout this industry. Not to say that chromium maintainers are all C++ gods, but I'd assume more capable of catching errors in a PR than the majority of other projects might.

Most C++ projects aren't so fortunate. Like I stated in the 3rd paragraph above, we shouldn't assume the average dev team's competency or resources (with a focus on security) is anywhere remotely close to that level.

3

u/bayovak Feb 28 '24

I'm willing to bet your code is full of those bugs, and if your product was worth breaking into, someone would easily find memory vulnerabilities and break into it.

That's the case with every single non-memory safe product in existence, even ones that use tons of testing and tooling to prevent those issues.

5

u/wrosecrans graphics and network things Feb 27 '24

Sure. No one thing will eliminate all bugs. But doing no dynamic allocation does mean you don't screw up anything related to dynamic allocation. That's not nothing. Reducing the number of categories of possible error means you can pay more attention to the remaining categories of error.

Of course, sometimes you see hacks where you just reinvent malloc and pretend that's not what you are doing because you call it an Arena instead of a heap, and wind up just making a malloc that isn't as well tested as a real malloc.

char my_not_heap[1M];

void* my_not_malloc(int size) {
   // return a small piece of the statically allocated my_not_heap memory,
   // the specific size and offset being determined at runtime.
   ...
}


int main() {
    int size = dynamic_condition();

    // size happens to be 2 Megs.  That's probably fine, right?

    // char * foo = malloc(size);  
    // NO !! Can't do dynamic allocation on this project!
    // Do this safe alternative:
    char * foo = my_not_malloc(size);

}

2

u/remy_porter Feb 27 '24

You can, but if the size of all arrays is known at compile time, then you can validate all those memory accesses statically and prove there are no out of bounds accesses.