9 ms·
Biggest flaw in the design of C: no real array parameters. Source of most buffer overflow vulnerabilities for thirty years. It's a fixable problem [1] but it's
by Animats 11y ago
Biggest flaw in the design of C: no real array parameters. Source of most buffer overflow vulnerabilities for thirty years. It's a fixable problem [1] but it's not going to get fixed. We need to move forward to Rust, or something like it, and get rid of C for anything for which security matters.
[1] http://animats.com/papers/languages/safearraysforc43.pdf http://animats.com/papers/languages/safearraysforc43.pdf
- kachnuv_ocasek 11y agoIt's not a flaw IMO, it's just confusing. Especially for those who come from higher languages and don't read a solid intro into C. It's actually explained quite well in K&R.
- Animats 11y agoNo, it's a design flaw. It's a consequence of K&R C for the PDP-11, which had to be crammed into a small address space machine and didn't even have function prototypes. In K&R C, which is one notch above assembly code, there was no cross-module type checking and everything was really an int or a float. Even in the 1970s, this was kind of lame. Pascal could pass array sizes through a call.
- pjmlp 11y agoIt is a big flaw to ignore what other language communities were doing. Other systems programming languages older than C did it properly.
- tosseraccount 11y agoHow many programs running on your computer were written in these languages?
- pjmlp 11y agoWhat does it matter?!? UNIX killed those languages.
- AnimalMuppet 11y agoAnd why? Because in practice, C on Unix was better for writing actual programs that actually worked to do things that people cared about. And that's why it matters: Because C was actually better in practice. Everybody seems to be in denial about that; maybe they should learn to deal with it. Was that "actually better" due to C, or to Unix? Well, maybe some of both, but Unix was written in C, so C was useful for writing the operating system that you blame for C taking over the world... If you don't understand why C was better in practice, you're going to fail in attempts to create something better, and never know why. You're just going to be ignored, while you whine about how your way was better.
- pjmlp 11y agoCompletely wrong! Back in those days we used to pay for our compilers, usually in values of thousands, remember those? Only languages delivered as part of was then the OS vendor's tooling got used at most work locations. It always required lots of persuasion to buy compilers for languages not delivered as a standard part of the OS. So as UNIX managed to gain a foothold into the enterprise and universities, pushing mainframes and other OSes, C gained mind-share. UNIX took over the world by accident, by having AT&T initially providing the code for free (which they repented later on) to universities, which happened to have persons like Bill Joy and Scott McNealy that got successful with their startups using the OS they enjoyed at their universities. Had AT&T never given the code for free or those startups floundered, UNIX will be another footnote alongside MULTICS and friends. Just like nowadays no one sane would use JavaScript, if the browsers had first class support for other programming languages.
- AnimalMuppet 11y agoThere is an element of truth in what you say. You seem to think it's the whole story; I do not. Had C not been good in practice, "free" wouldn't have been enough. Those OSes written in other system programming languages: Why didn't their computers take over the world? I mean, sure, AT&T was pumping money into Unix, but other companies were pumping money into the competition. Why did Unix win? It's not just because of evil AT&T. It's because Unix could deliver working features, and others struggled to keep up. Why could Unix deliver working features? Yes, partly because of AT&T's money. But partly because C turned out to be really useful as a systems programming language. These other languages you mentioned a few posts ago: Sure, they looked good on paper, only where's the beef? When it came time to deliver, what was written in them? More functionality was written in C, which is why Unix won. I'll repeat my previous statement: If you don't understand why C was better in practice, you're going to fail in attempts to create something better, and never know why. You're just going to be ignored, while you whine about how your way was better. History ignored your "better" languages. The world moved on from them, for good reasons. You think they were better, but in real life, they weren't.
- emodendroket 11y agoWell, by this metric, (insert Java, bat files, JavaScript, whatever thing you hate here) is a fantastic language too.
- pcwalton 11y ago> It's not a flaw IMO, it's just confusing. That is a flaw.
- acconsta 11y ago>We need to move forward to Rust In fairness, C++ containers know their size. And Rust's solution to buffer overflows is the same as every other language — run time bounds checking.
- icholy 11y agoCan't you turn them off though?
- steveklabnik 11y agoNot with a flag or anything. As said above, if you use iterators, it's not generally an issue, or you can call an unsafe function that doesn't do the check.
- kibwen 11y agoYou can opt out of bounds checking by using the unsafe `get_unchecked` method instead of the array indexing syntax. There's no compiler flag to turn off bounds checking, because the language doesn't want to encourage memory safety that's dependent only on compiler flags (and also because the cost of runtime bounds checking in real programs is indistinguishable from noise).
- acconsta 11y ago>the cost of runtime bounds checking in real programs is indistinguishable from noise Can you show us the benchmarks for that?
- kibwen 11y agoThe source of my assertion comes from repeated conversations with the Servo developers, who have written many hundreds of thousands of lines of Rust in a project whose goal is to be twice as fast as modern web browsers (in other words, they are comparing themselves to programs written in C++, and speed matters). They regularly profile in pursuit of tracking down inefficiencies, and bounds checking has never been even a blip on their radar. Quotes from the exchange I've had just now with pcwalton: "I haven't done rigorous benchmarking but I have never seen it in instruction level profiles [...] I'd rather just spend my time shipping software that uses fast bounds checks and prove it that way :) [...] my point is simply that the delta between idiomatic Rust code that uses iterators and no bounds checks and the idiomatic Rust code that uses iterators and bounds checks is incredibly small since bounds checks are so rare to begin with" (http://logs.glob.uno/?c=mozilla%23servo#c271807 http://logs.glob.uno/?c=mozilla%23servo#c271807) As for actual benchmarks, I think they would actually be quite easy to produce. There's exactly one line in the Rust stdlib that provides bounds checking for indexing, right here: https://github.com/rust-lang/rust/blob/master/src/libcore/slice.rs#L552 https://github.com/rust-lang/rust/blob/master/src/libcore/sl... . All you would need to do is take out that line and compile Servo with your modified stdlib and compare the results of running the built-in benchmarks. I may just do this myself as a blog post. :)
- blazespin 11y agoThe biggest flaw is programmers who ignore warnings.
- kybernetyk 11y ago>We need to move forward to Rust That's the second time in two days I've come to read offensive ranting about a language + "let's move to Rust" as the proposed silver bullet solution here on HN. If this is the way the Rust community tries to evangelize their language I'm inclined to keep myself as far away as possible from it. Now don't get me wrong: I think Rust is fine. But that "C/C++ is dumb and everyone who continues to use it is also dumb and/or ignorant" sentiment won't win you any friends. There are valid reasons why people keep using C++ and can't switch to any other language in the near future.
- steveklabnik 11y ago"C and C++ is dumb and everyone who continues to use it is also dumb and/or ignorant" certainly isn't the position of the Rust team nor the general Rust community at large.
- klodolph 11y agoIt's not even the position expressed in the parent comment.
- steveklabnik 11y agoIt is how my original parent perceived it, and perceptions matter. I wouldn't work on Rust if I didn't think it was important and a step forward, but even technologies with flaws (read: all of them, Rust has flaws too) are worth using, depending on your situation. Trade-offs are the heart of engineering.
- klodolph 11y agoPerceptions matter, but language choice is a very personal issue and I've noticed that people can get very defensive when they perceive attacks on their language of choice (such as C++) or support for a language they hate (such as PHP). We should continue to talk about language choice because it's important, and we should avoid getting into pissing matches when we do it. I'm convinced that in order to make this work, we have to be more generous in how we perceive other people's comments.
- yorkedork 11y agoThis response is neither productive nor insightful. First, it's probably not useful to arbitrarily claim what the biggest flaw in the design of C is without context or data. Second, I don't think it's reasonable in this case to place the blame on the design of a language (certainly, the choice of C has long been a pragmatic decision). As Linus alludes to directly, the tools exist with which to write more correct, idiomatic C code for the kernel (e.g., ARRAY_SIZE and a useful gcc warning). Finally, as the historical record should demonstrate, 'getting rid of X by replacing it with Y' is not particularly actionable; it certainly isn't the most efficient solution in terms of resources and likely not in terms of correctness over the short- to medium-term. If I'm to hazard a guess, the point of his message was to remind everyone the biggest source of flawed code lies in our own hands and in the biases individuals and groups bring to large-scale development. The choice, here, isn't between using a good language and a bad language; it's between using a language and its idioms correctly, and using those things lazily with foul consequences.
- LBarret 11y agothe choice is also between a good language and a bad one. Those two choices are not exclusive, and choosing good practices is not the end of the road for the quest for quality.
- acconsta 11y agoNo. There's only one language you can write a patch for the Linux network stack in — C. "Move to Rust!" is not a productive or helpful suggestion, unless you have an massive army of programmers ready to port Linux to Rust.
- kibwen 11y agoYou can write a patch for the Linux network stack in any language you want. Whether Linus will merge it is a different matter. :P EDIT: Downvotes eh? Then I suppose I'll state outright what I left implicit: Linux is not inextricably tied to any single language, and it's a strawman to suppose that any attempt to integrate Rust with Linux would first have to reinvent the universe from scratch. Rust has excellent interoperability with C (though, notably, unions are iffy), and it's common to write Rust code that gets called from C with C being none the wiser.
- klodolph 11y agoThere are a ton of "design flaws" in C, including integer promotions, string processing which is hopelessly error-prone (and people who think that strncpy() fixes anything), a preprocessor which is basically its own language, the mere existence of gets(), et cetera. There are many programming patterns in C which produce buffer overflows, a lack of array parameters is only one source.
- ArkyBeagle 11y agoThat's no bug. One can use no array arguments and write perfectly safe code. MOST of the "this is unsafe" examples are just bad code and have been from the beginning. Doctor, doctor, it hurts when I do that... It's an axiomatic choice inherent to the language. You add a parameter representing the extent of the array, and respect that. I have no heartburn with Rust, but there's sure a lot of legacy 'C' out there.
- nickpsecurity 11y agoDidn't even know you had a paper on the subject. Going in my collection. I like how it mitigates so much risk with so little change or overhead. Good work. Paper mentions dangling pointers are a risk. They can be detected with other technology with even more in the works: http://research.microsoft.com/en-us/um/people/marron/selectpubs/Undangle.pdf http://research.microsoft.com/en-us/um/people/marron/selectp... So, out of curiosity, what's the odds of something like this proposal getting adopted in a standard, GCC, or anything else?
- Animats 11y agoIt's a dead proposal. It was discussed on "comp.std.c" back in 2012, and that's the third draft, after fixing all the problems everyone had found. By that time, nobody could find a serious hole in it. While it's technically feasible, the political effort to push it forward is more than I can do. Maybe if Amit Yoran was still at Homeland Security...
- nickpsecurity 11y agoDamn. Well at least you tried and anyone with more political might has something that might work in the future. That's how 3/4 of my designs work. ;)