4 ms·
I think the problem you see in Rust evangelism is a conflation of overall program correctness (aka "security"; any bug can be considered a security bug in a con
by gue5t 10y ago
I think the problem you see in Rust evangelism is a conflation of overall program correctness (aka "security"; any bug can be considered a security bug in a contrived-enough threat model) with memory safety.
The C programs I encounter in the wild tend to be both insecure and not memory-safe. The difference between regular bugs in memory-safe programs and memory-safety bugs is that with a regular bug your program is still evaluating the code you wrote; perhaps you forgot a permission check, or you made a silly mistake in code that you didn't think was security-relevant but which a user might be using to keep their house doors locked. These are frustrating, but can usually be understood once found by established debugging techniques like bisecting a codebase with a test case.
With a memory-safety bug, your program runs the buggy code and, if you're unlucky, will diverge from the semantics you associate with its source code. Whereas a C programmer thinks of "x->name = foo;" as making an assignment of a struct field, if "x" is an invalid pointer, this code might write to an unrelated integer variable, modify 4 or 8 bytes of a string buffer, cause a segfault (this is nice, since we find out our program is buggy), alter a function pointer (to produce a segfault muuch later in code which is itself correct), or have any number of other effects.
This is much more hard to debug, and it's effectively impossible to mitigate the impact on your code of memory safety bugs outside your code. If you're working in a memory-safe language and call a function which you do not know to be implemented correctly, the worst thing it can do is issue an unexpected system call, which shows up very quickly in debugging. It can't mangle unrelated variables to make the program crash in correct, bug-free portions of code.
As noted, Rust is not, as a whole, memory-safe. Unsafe code can set up bombs that will cause segfaults and other mischief in safe code, just like the situation in C if we substitute 'unsafe' for 'incorrect' and 'safe' for correct. But, in Rust, if you see misbehavior you do not have to audit your entire codebase. You only need to audit the code explicitly marked as unsafe. The vast majority of computation in Rust is outside of unsafe blocks, and using unsafe in the middle of a complicated, subtle algorithm raises big warning bells in code review.
With respect to "C and other languages [having] similar issues", I don't think this is the case. Remote native code execution is a security issue only present in memory-unsafe programs. We do see remote (high-level) code execution vulnerabilities in programming languages with "eval"-like faculties, but it's not hard to audit your code and all its dependencies for calls to the built-in "eval", whereas auditing C code for memory safety is incredibly difficult.
All of this is in some sense a consequence of the principle of least privilege: we want to make the world more secure by avoiding having an omnipresent "do absolutely anything" escape hatch, so that we can trust modules in proportion to the privileges they need. Rust and memory safety aren't a silver bullet, but I believe they're a cost-effective step toward correctness.
The other half of Rust is that having algrebraic datatypes and a good typesystem helps you help yourself avoid many logic mistakes, and having an expressive language with generics helps you write less code (which correlates with having fewer bugs). This is why Haskell programmers like to claim they see fewer bugs than other languages. It's much harder to make an objective argument here, because availability of tools for preventing bugs isn't the same as lack of bugs. There are C codebases with fewer bugs than Haskell codebases; pure bug count depends on a multitude of factors in the development process and only formal verification can prove your program to be correct relative to its specification.
And then your specification still might not mean what you wanted it to mean! There aren't any silver bullets, but Rust is a useful tool that you should consider making part of your toolkit, and there's been a lot of work to try to make it able to do all the things people do in C.
- i336_ 10y agoThanks very much for this. (Some bits have been elided for brevity and to avoid the HN comment length limit; do note I understand the points you've made in spite of shortening some parts) > The difference between regular bugs in memory-safe programs and memory-safety bugs is that with a regular bug your program is still evaluating the code you wrote; (...) [t]hese are frustrating, but can usually be understood once found by established debugging techniques like bisecting a codebase with a test case. Right. (It's sad the only truly generic way to apply this approach is with fuzzing.) > With a memory-safety bug, (...) it's effectively impossible to mitigate the impact on your code of memory safety bugs outside your code. Ooohh. I see - faulty image parsing letting you break out of Chrome, or faulty font parsing letting you run amok in the NT kernel (yay!). > As noted, Rust is not, as a whole, memory-safe. (...) But, in Rust, if you see misbehavior you do not have to audit your entire codebase. You only need to audit the code explicitly marked as unsafe. The vast majority of computation in Rust is outside of unsafe blocks, and using unsafe in the middle of a complicated, subtle algorithm raises big warning bells in code review. That makes sense. > With respect to "C and other languages [having] similar issues", I don't think this is the case. Remote native code execution is a security issue only present in memory-unsafe programs. It's so sad the Burroughs-style architecture (https://news.ycombinator.com/item?id=14073079 https://news.ycombinator.com/item?id=14073079) fizzled out :( > We do see remote (high-level) code execution vulnerabilities in programming languages with "eval"-like faculties, but it's not hard to audit your code and all its dependencies for calls to the built-in "eval", whereas auditing C code for memory safety is incredibly difficult. Unless it's JavaScript - https://news.ycombinator.com/item?id=14013155 https://news.ycombinator.com/item?id=14013155 (Related: http://stackoverflow.com/questions/42776357/can-i-effectively-use-transpilation-code-tokenzation-regeneration-a-vm-or-a-si http://stackoverflow.com/questions/42776357/can-i-effectivel...) > All of this is in some sense a consequence of the principle of least privilege: we want to make the world more secure by avoiding having an omnipresent "do absolutely anything" escape hatch, so that we can trust modules in proportion to the privileges they need. (...) Right. On the one hand that [escape hatch mentality] gets you Symbolics Lisp; on the other hand those Lisp machines never ran banks or multiuser systems :P > The other half of Rust is that having algrebraic datatypes and a good typesystem helps you help yourself avoid many logic mistakes, and having an expressive language with generics helps you write less code (which correlates with having fewer bugs). The only problem there is that the verbosity of the code can cause oldschool hackers to flounder: http://esr.ibiblio.org/?p=7294 http://esr.ibiblio.org/?p=7294 > This is why Haskell programmers like to claim they see fewer bugs than other languages. It's much harder to make an objective argument here, because availability of tools for preventing bugs isn't the same as lack of bugs. Can't disagree there. Objectivity FTW. > There are C codebases with fewer bugs than Haskell codebases; pure bug count depends on a multitude of factors in the development process and only formal verification can prove your program to be correct relative to its specification. > And then your specification still might not mean what you wanted it to mean! There aren't any silver bullets, but Rust is a useful tool that you should consider making part of your toolkit, and there's been a lot of work to try to make it able to do all the things people do in C. Thanks for this, it was an interesting read. If anything, Rust badly needs a rockstar documentation team who can canonically communicate "the state of 4 minutes ago!" and keep everyone updated in real time. A herculean effort, most definitely, but it would ensure people had correct ideas about the language from the start, which would be cool. I also think I have a theory as to why the parent commentator ("Except compilers are also...") has the views they have. Rust requires you to be extremely verbose in describing every last iota of abstraction (and you have to have the right number of levels) - you can't just start banging around in the kitchen with random bits of hardware like you can in C, or hammer strings together like you can in Perl, or do Python's "executable pseudocode" thing, etc. You have to be neurotically specific (it feels like it when coming from the familiarity of another language that most decidedly does not feel like that). I think that oldschool people encountering this see it as "too much work", because for the level of effort required, they expect the compiler to do more work for them than it's doing (for example, wrangling cyclic data structures). And so they go "I have to do all this?! And I only get that as a result?!" and throw their hands up in the air and find it hard not to criticise. So this folds back into the documentation problem - people are collectively memoizing the "Rust is awesome!" from all the impressionable foghorn types. The foghorns need to be turned down a little and the nuance made a little clearer. PS. I must admit that when I saw your "x->name" my brain immediately went "wait, what's x's current value?" - I'm still learning C (I understand the basics, but I'm still slow with it in many areas), and I'm happy I instinctively reacted like that :)