4 ms·
I don't see how C will ever be free of buffer overflow bugs without language changes. Probably the simplest and most effective would be to not have arrays decay
by WalterBright 8y ago
I don't see how C will ever be free of buffer overflow bugs without language changes. Probably the simplest and most effective would be to not have arrays decay to pointers when passing to a function.
https://www.digitalmars.com/articles/b44.html https://www.digitalmars.com/articles/b44.html
- baybal2 8y agoThat will not be enough
- WalterBright 8y agoD does allow arrays to be passed as function arguments, and I can vouch for how effective it is at eliminating a huge swath of buffer overflows. https://dlang.org/spec/arrays.html#dynamic-arrays https://dlang.org/spec/arrays.html#dynamic-arrays
- blub 8y agoBut they key safety feature is bounds checking, not just preventing the decay to pointer.
- WalterBright 8y agoOnce you have the array length and pointer, the compiler (as the D compiler does) inserts the array bounds check.
- blub 8y agoIndeed, however if you will try to convince a group of C programmers that arrays shouldn't decay to pointers you will get a different reaction compared to trying to convince them that all array accesses should be bounds checked. I would lead with the stronger demand, because that's what would IMO trigger the more interesting discussion.
- WalterBright 8y agoI've presented the idea in my article to a room of C programmers a couple times, and the reaction I get is just a blank look. No discussion. I'm a bit baffled.
- baybal2 8y agoHi Walter! Surprised to see you here! As a former C dev (ran away to webdev years ago from embedded due to flaccid job market) I'm open to an idea of on-demand boundary checks. To me it will just be a matter of having the compiler insert them for me or me writing them myself.
- Gibbon1 8y agoCould bolt on actual arrays, but we'd need grab a shovel and take the C standards committee members out behind the barn first.
- a1369209993 8y agoC has actual arrays; what's missing is actual array references: C uses pointers to the first element and implicit pointer arithmetic. Rust's slice types are a start, but I'd also like to see checked indexes: size_t zfoo = ...; /* todo: better typecheck of malloc */ int[zfoo]* foo = malloc(zfoo * sizeof *foo); [zfoo] ifoo = ...; /* allowable values 0,1,...,zfoo-1 */ /* zero-length arrays cause problems here */ return foo[ifoo]; /* no runtime check; type guarantees in-bounds access */ There are several problems with this, but it sure would be nice. 0: Stupid dependent type system tricks: have x % y typed as [y], have x < y typed as Maybe([y]), make for([n] i=0; i<n ;i++) do the useful thing.
- caf 8y agoYou can pass real references to arrays using C99's variably modified types: int foo(int len, int (*arr)[len]) { printf("number of elements in *arr: %zu\n", sizeof *arr / sizeof (*arr)[0]); } int main() { int a[6]; int b[10]; foo(6, &a); foo(10, &b); return 0; } If you do this, the compiler clearly has the information to do at least dynamic bounds checks in 'foo', which it may also be able to statically eliminate (eg if there is a loop from 0 to len - 1). It might be nice if there was some syntactic sugar for this that made the 'len' parameter implicit and unnamed, and didn't need the explicit & / * on the array pointer.
- a1369209993 8y agoI had sort-of forgotten about those. len needs to be explicit unless it's packaged as eg arr.len. Also (* arr)[i] is just as unacceptable as (* p).item, but you can't do arr[i] unless you abandon the ability to do indexing on raw pointers, which would break essentially all of C.
- himom 8y agoRust-style lifetimes. The work by deallocing when the lifetime goes out of scope. No more memory leaks from non-reference-counted stack pointers (heap allocs). Or: use Rust.
- charliesome 8y agoRust lifetimes prevent use-after-frees, but they're totally orthogonal to buffer overflows. Rust mitigates buffer overflows by automatically inserting bounds checks and panicking on out-of-bounds access. Your program still crashes, but you don't potentially open yourself to RCE. Ultimately Rust's automatic bounds checks rely on higher level facilities in the language for dealing with slices that C just doesn't have.
- TheDong 8y agoRust has more than just bounds-checks. Rust heavily encourages the use of iterators and functional-programming both of which prevent buffer overflows by making code that could overflow less idiomatic.
- stochastic_monk 8y agoThis can also be provided by C++ by .at() or iteration through containers.
- wahern 8y agoYour comment is a non sequitur because this bug involves type punning across a syscall boundary. Neither D nor Rust would solve this kind of problem. Likewise for a host of IP stack bugs that involve type punning over untrusted type identifiers and length headers. From a safety perspective you want to either box these derived types or copy the headers into properly typed objects so you don't end up with the complex if-else ladders that plague IP stacks. But the cycles to do this--to recursively construct simple boxed types or copy headers--can quickly add up to more than the cost of copying the entire packet as packets move through the pipeline. Engineers [unfortunately] bend over backwards attempting to achieve zero-copy. We should be using more type safe techniques, and certainly D and Rust make such techniques more ergonomic. But it's fanciful to think that engineers won't recapitulate the same sins[1]. If it were true we'd all be using seL4 and it wouldn't much matter if a language was "safe". [1] You can this see kind of recapitulation regularly in "high performance" Java and Rust applications where the same type punning patterns are used, replacing pointers with indices into buffer arenas. Po-tay-to, po-tah-to.
- WalterBright 8y agoIt's true that D allows all sorts of dirty tricks, like punning and unsafe casts, but only in code that is not marked as @safe. @safe code is mechanically checkable to be safe. This has the ergonomic feature of drastically reducing the defect surface, in that it saves my time because with C code I constantly am reviewing the array code for possible overflows, in D I don't have to.
- wahern 8y agoThat argument isn't nearly as persuasive in kernel space as it is in user space. Certainly not when performance and bleeding-edge techniques (orthogonal to or even in opposition to the normal idiomatic patterns newer languages emphasize and optimize) are as emphasized as they are in Linux. How many bespoke hash, tree, and queue data structures exist in the Linux kernel, each with purposeful but complex data and execution flow dependencies unique to their context? Every time someone tries to unify two, one more pops up. Point being, the Linux development model--partly because of the nature of the work, partly because of the particular culture--absolutely defies attempts at drawing lines between safe and unsafe so the latter can be isolated. Code that doesn't heavily rely on unsafe techniques, directly or indirectly, wouldn't normally be in the kernel, anyhow. And particularly in a kernel environment even @safe code can do things that violate or exacerbate assumptions elsewhere in the code (e.g. complex RCU protocols), thus Linus' preference to avoid languages with a lot of implicit machinery or non-obvious execution flow. So it's not like @safe code can simply be removed from the equation.