5 ms·
So let's take a closer look at the fairly recent CVE-2022-21658. Rust's standard library provides std::fs::remove_dir_all("/some/dir"), in C++ you will find th
by tialaramex 5y ago
So let's take a closer look at the fairly recent CVE-2022-21658.
Rust's standard library provides std::fs::remove_dir_all("/some/dir"), in C++ you will find the very similar std::filesystem::remove_all("/some/dir")
As you might anticipate, these functions get rid of the directory /some/dir if necessary deleting sub-directories and files recursively in order to achieve that.
They both explain that they won't delete other things, even if there's a symlink in /some/dir or some child that points to those outside things, the symlink gets deleted but not the thing it pointed to.
What kind of terrible unsafety was discovered in CVE-2022-21658?
Until Rust 1.58.1 you could exploit a TOCTOU race condition to blow away other people's stuff using symlinks when they made this call.
And where is the CVE for the same bug in C++? There isn't one. C++ standards people argued that the C++ standard already actually says that none of their filesystem API is safe to use if you have a "concurrent environment" in which such races are possible. Standard C++ programs are only intended to run on a machine where they have exclusive control over the entire system for the entire length of their runtime and so when your C++ program deletes files you thought it couldn't delete that was actually your fault for not being familiar with every caveat and ruling in the entire C++ standard.
What should you do about that? I believe the answer is obvious: Use Rust
- ncmncm 5y agoIt is a choice between (1) acknowledge it is an OS problem the library can't fix, or (2) pretend the library can fix it. C++ chose one; Rust, you say, chose the other. The Posix file API is shot through with races. Patching around them is a game of whack-a-mole. C++ cannot fix Posix. Rust cannot fix Posix. It is dishonest to assert that "C++ programs are only intended to run on a machine where they have exclusive control over the entire system". The system a program is a part of is expected to have enough control over what it operates on that its semantics are well defined. No Standard can do more. Rust can do no more. I doubt Rust pretends to more.
- tialaramex 5y agoThe C++ standard does not, in fact "acknowledge it is an OS problem the library can't fix" in respect of remove_all it plainly says this function doesn't remove files resolved to by symbolic links even though sometimes it does. The extent of its "acknowledgement" is that it says elsewhere in the standard that if there's any filesystem race you get Undefined Behaviour. C++ programmers can't do anything with this except, as I explained, insist on exclusive control over the system where their program runs during the entirety of its runtime to avoid this Undefined Behaviour.
- ncmncm 5y agoC++ programmers rely on definitions from a wide variety of places, not limited to the ISO C++ Standard, and not even limited to Standards. Just writing "#include <unistd.h>" is Undefined Behavior as far as the ISO C++ and C Standards are concerned, but ISO Posix defines it. Linux, the BSDs, MSVC, iOS, Intel, AMD, ARM, and NVidia provide numerous additional definitions programmers rely on. A C++ program's behavior is defined by the aggregate of all of them as they apply to the runtime environment of the program. ISO Standards do not editorialize about things they do not define; they simply do not define them. For what the ISO C++ Standard does not define, you must look elsewhere if you want a definition. Often there is one, elsewhere. Every last jot and tittle of what a Rust program does is Undefined Behavior as far as every ISO Standard is concerned, but Rust coders rely on other definitions, too, including a specified binding to ISO Posix where relevant, and the various CPU and OS specs. It is generally advisable not to pontificate on things you do not understand to people who do.
- pcwalton 5y agoYour comment would be better without the last sentence. And the handling of that CVE does illustrate a key difference between Rust and C++. Rust says "it's our fault if we get the intuitive semantics wrong". C++ says "it's your fault for relying on your intuition". It does no good to pretend there's no difference between the two philosophies.
- ncmncm 5y agoYou seem to be saying Rust patches up the most obvious Posix holes but leaves the more subtle ones, to make it seem more secure without, in fact, being more secure.
- pcwalton 5y agoIf you fix some issues but not others (the fact that we aren't being specific about which POSIX issues we're referring to is driving me crazy, but anyway) you have in fact improved security. The "well, if it can't be perfect, there's no point in doing anything at all" attitude is deeply pernicious, and it would be unfortunate if that is what C++ has adopted.
- littlestymaar 5y ago> Patching around them is a game of whack-a-mole. C++ cannot fix Posix. Rust cannot fix Posix. Rust cannot “fix Posix”, but the stdlib (and others) can definitely work around its quirks. See how this[1] library sidesteps a thread-safety issue when using localtime_r(3). https://github.com/x-hgg-x/tz-rs https://github.com/x-hgg-x/tz-rs
- ncmncm 5y agoIt is sometimes possible to work around "quirks", particularly when no malice is intended. When it is a question of security, and fending off attackers, the most obvious fix is rarely any kind of fix at all. Attackers can see what you did, and simply step around it. Bragging about how you put in your broken fix and somebody else didn't tends to attract what may be unwelcome attention.
- tialaramex 5y ago> Bragging about how you put in your broken fix and somebody else didn't tends to attract what may be unwelcome attention. The specific thing being fixed here was a TOCTOU race and thus the fix is just to hold on to the thing being checked so you can use the same thing and eliminate the race. Well, of course I say "just" but it was a lot of work.
- littlestymaar 5y agoYou obviously have no idea what you're talking about. There is no attacker involved in this discussion: the entire purpose of Rust is to prevent the user to use a dangerous method without opting into unsafe. If The user opt into unsafe, there maybe a way for an attacker to exploit a user's mistake, but as long as the user doesn't they'll be safe, because what Rust does is forbidding the user to shoot themselves in the foot.
- pjmlp 5y agoYou can downplay those CVEs, but the fact is they exist. It is a big difference to sell Rust stating that the number of exploits cases will be lower, which they are, or asserting they don't happen at all. When the seller tells a message the audience doesn't believe into, the audience loses interest.
- tialaramex 5y ago> You can downplay those CVEs, but the fact is they exist. Indeed. Rust decided by policy that it needs to not only fix mistakes, but also tell people about them. There's a big difference though between the fact that such a large piece of software isn't perfect (to be expected) and the choice to silently ignore whole categories of problem and pretend they just don't exist or blame the programmer for holding it wrong.
- deleted 5y ago[deleted]
- pjmlp 5y agoThis "..., making it difficult to shoot _anything_ besides the correct target with rust" doesn't seem to follow that policy, rather sends the message that everything just works. C++ isn't perfect and there are many issues to improve, but I also don't agree that Rust is such a perfection. Looking forward to a perfect abstraction of POSIX in stable Rust, taking into consideration all the caveats on the standard. After all, I want my Rust code to be perfectly safe.