5 ms·
In fact, in the course of our work, we have found that even experts writing safe integer libraries or tools to detect integer errorssometimes make mistakes due
by adrianN 10y ago
In fact, in the course of our work, we have found that even experts writing safe integer libraries
or tools to detect integer errorssometimes make mistakes due to the subtleties of C/C++ semantics for numerical operations
Another nail in the coffin for the meme that "good programmers don't write code with undefined behavior".
- stinos 10y agoIs that really a thing? That sounds almost as illogical as saying 'good programmers don't write bugs'. Should be more like 'good programmers are aware of undefined behaviour and try to not invoke it' or so.
- jcranmer 10y agoYes, it totally is a thing, although it's usually described more as "C(++)'s undefined behavior isn't a problem because any good programmer should be able to easily avoid it"--a sentiment that I have seen (and challenged) on Hacker News a fair amount. The contention that I (and the authors) have is that integer overflows are so pervasive that it's not practicable to expect that even good developers are capable of avoiding them. Some things to really emphasize here: 1. This study found 47% of packages tested exhibited undefined integer overflow on their test suite. This isn't using fuzz testing or anything to try to find corner cases to make overflow far more likely, this is more or less "opening the application triggered undefined behavior"-level pervasiveness. 2. The study also looks at unsigned integer overflow, which is well-defined but often no less problematic. The classic example I'll give is the calloc bug--calloc takes two arguments and multiplies them to find the size to allocate, and virtually every time that I've seen this function implemented, an exploitable CVE can be found (when the multiplication overflows). 3. The prelude to this work (that produced the IOC, and, ultimately, Clang's UBSAN) is what really woke the community up to the problem. It actually did prompt the C and C++ specifications to make changes (INT_MIN % -1 is now considered UB, and C++, but not C, makes 1 << 31 == INT_MIN defined behavior). Now that memory errors are slowly disappearing, it's integer problems that are becoming the pain-in-the-neck for exploits. My personal opinion is that the treat integers as ℤ_{2³²} model is no longer a good idea as the default integer model, and we need to start looking at bringing back models like big integer (à la Python), saturating, or explicit trapping models (à la Ada); I was quite disappointed when Rust opted to keep the wraparound model.
- pcwalton 10y ago> Now that memory errors are slowly disappearing, it's integer problems that are becoming the pain-in-the-neck for exploits What fraction of integer overflow exploits crucially have to be combined with an unsafe memory model in order to be weaponizable? I suspect the number is close to 99%. (I know there are a handful of overflow-related security problems that are independent of memory safety--someone mentioned money overflow issues in online games when this topic came up last--but they are few and far between.) > I was quite disappointed when Rust opted to keep the wraparound model. That's oversimplified. In debug builds, Rust panics on overflow; in release builds, Rust may or may not panic. It doesn't right now out of performance considerations. That is not a decision that is set in stone if the performance becomes acceptable in the future.
- marvy 10y agoThis means that Rust has experience with wraparound and with panics. So this question should be answerable by someone on the Rust team: how much does the check cost? A few percent? A factor of 2? Surely not more than that?
- pcwalton 10y agoDepends. The problem is not so much the overhead of the check itself as that it introduces a lot of control flow edges which interfere with optimizations. For example, on x86 you can kiss most uses of LEA goodbye...
- marvy 10y agoSure, but when all is said and done, what's the cost? There are plenty of Rust benchmarks by now, possibly some of them are part of the test suite. How much do they slow down?
- wofo 10y agoNote that it is possible to enable overflow checking in release builds as well.
- pcwalton 10y agoYes, it is. I've made a small HN career of countering this meme whenever it comes up :) It's a classic form of no-true-Scotsman, but very popular.
- posterboy 10y agoIf it's a fallacy, why wouldn't a programmer do good to avoid UB?
- nkurz 10y agoAvoiding undefined behavior is good, and a good programmer tries to avoid undefined behavior. The fallacy is that there exists a population of C and C++ programmers who are skilled enough to always recognize and avoid undefined behavior. While you could define "a good programmer" as one who always recognizes and avoids undefined behavior, the unhelpful result would be concluding that very few good C or C++ programmers exist, possibly none.
- posterboy 10y agoBy modus tollens, C can't be a good language, then. Gosh!
- ArkyBeagle 10y agoAlmost all the integers I use in 'C' are highly constrained. I try very hard ( and nearly always succeed ) in writing code that manages those constraints properly. This may include oversubscribing integers - using a long long instead of a long, perhaps. This involves at least doing a depth-first examination of all invocations of that operation, and possibly writing tests for all of them. That encourages keeping things local. I think the safety-think here is slightly inappropriate. Of course you can do it wrong. But it is in no way an unreasonable expectation that you not do that. And again- for the general population out there - perhaps 'C' is not for you. I am a 'C' programmer only because of path dependencies in what's happened to me. I would not recommend it to anyone. That's not elitist - it's just how things played out.
- jstarks 10y agoI think it's apparent that we write in C because of network effects and not because it's the best tool for the job. It's great that people like you can handle it, but I'd much rather write in a language that keeps my stupid mistakes from becoming expensive security holes. It allows me to write code faster and to work with other non-perfect programmers.
- ArkyBeagle 10y agoYeah, but whither faster? When I was an imperfect programmer ( perfect to some epsilon TBD ) I wanted to be a more perfect programmer. This was not without cost. I suppose if you're not interested in that ( and I understand that impulse, believe me ) then ... I have to, by training, attribute all defects to process and human behavior. But as things emerge, maybe the tools will really fill the gap. But again, at a cost. I am, say, pretty good with Python but I am by no means a master. It would take having a full time exposure for months, perhaps years to make that claim. And this fragments us. It may well be that I'd actually be better fit for a job using Python but because I can't make this claim now, we get a false negative on fitness. I'm just using "me" and "Python" to construct an example.
- posterboy 10y ago> network effects C, like unix, is a small payload spreading like a virus, depending on the coder to keep it alive. The simplicity and the emergent behavior are as much an evolutionary advantage as a burden because of the UB. At 600 pages, the standard is not really simple anymore, as the RNA accrues more inputs from different vectors. I started to read the The UNIX-Haters Handbook [1] after I installed a Windows VM and saw the link here the other day. 1: http://web.mit.edu/~simsong/www/ugh.pdf http://web.mit.edu/~simsong/www/ugh.pdf