News for hackers. Stuff that matters.
Re:Understanding Integer Overflow in C/C++ (2012) [pdf] by adrianN on Saturday July 09, 2016 @11:08AM (#12061552)
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".

Re:Understanding Integer Overflow in C/C++ (2012) [pdf] by stinos on Saturday July 09, 2016 @12:03PM (#12061892)
Is 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.
Re:Understanding Integer Overflow in C/C++ (2012) [pdf] by jcranmer on Saturday July 09, 2016 @01:11PM (#12062278)
Yes, 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.

Re:Understanding Integer Overflow in C/C++ (2012) [pdf] by pcwalton on Saturday July 09, 2016 @01:48PM (#12062465)
> 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.

Re:Understanding Integer Overflow in C/C++ (2012) [pdf] by marvy on Saturday July 09, 2016 @02:55PM (#12062747)
This 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?
Re:Understanding Integer Overflow in C/C++ (2012) [pdf] by pcwalton on Saturday July 09, 2016 @04:22PM (#12063136)
Depends. 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...
Re:Understanding Integer Overflow in C/C++ (2012) [pdf] by marvy on Saturday July 09, 2016 @08:18PM (#12063968)
Sure, 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?
Re:Understanding Integer Overflow in C/C++ (2012) [pdf] by wofo on Saturday July 09, 2016 @06:23PM (#12063625)
Note that it is possible to enable overflow checking in release builds as well.
Re:Understanding Integer Overflow in C/C++ (2012) [pdf] by Asooka on Saturday July 09, 2016 @02:06PM (#12062534)
AFAIK the case for undefined integer overflow is optimisations regarding loop termination, carry bit handling, and usage of 64-bit vs 32-bit registers. In light of that can't we say that "The compiler may perform signed integer arithmetic using registers larger than the type's bits and truncate the result when writing to memory"? So e.g.

    int a = INT_MAX;
    int b = INT_MAX;
    int c = a + b;
    c > a; // true on 64 bit iff c was NOT written to memory (so -O1 and above); false on 32 bit
    (c&0x7fffffff) > a; // false always
    // i.e. you can always test if two positive values overflow with
    ((a+b)&0x7f..f)>a
    
Another idea I haven't seen used enough is to put values in 64-bit doubles, do your arithmetic that way and check if the final value fits in your integer type. As long as you restrict yourself to adding, subtracting and multiplying you'll get an exact result back (ok unless you run on a buggy pentium from over 25 years ago, but I think all those were recalled). A problem you have to watch out for is if you've entered the range where your ulp is larger than 1 and your math gets inexact.
Re:Understanding Integer Overflow in C/C++ (2012) [pdf] by nayuki on Saturday July 09, 2016 @04:05PM (#12063055)
> Asooka wrote:

  // i.e. you can always test if two positive values overflow with
  ((a + b) & 0x7F..F) > a
This is bad advice. The act of computing (a + b) is undefined behavior. Masking it afterward will not save you against a strict compiler that exploits (or traps) all undefined behaviors.
Re:Understanding Integer Overflow in C/C++ (2012) [pdf] by nkurz on Saturday July 09, 2016 @06:27PM (#12063634)
I think the 'Asooka' is aware of this, and was not offering advice. Rather, he was proposing that the world would be simpler if the specs were changed so that signed addition never resulted in undefined behavior. In this simpler counterfactual world, his test would work. In our current easier to optimize world, it would not.
Re:Understanding Integer Overflow in C/C++ (2012) [pdf] by ArkyBeagle on Saturday July 09, 2016 @03:55PM (#12063002)
I use float/double float values A Lot these days for arithmetic. So that reduces integer use to values read over a comms line or read otherwise from hardware.

Of course, floats have their own quantum of grief.

Re:Understanding Integer Overflow in C/C++ (2012) [pdf] by ArkyBeagle on Saturday July 09, 2016 @04:05PM (#12063050)
> Now that memory errors are slowly disappearing, it's integer problems that are becoming the pain-in-the-neck for exploits.

Indeed. Progress! :)

I think you may be right about Z_{2^32} but 'C' is hopelessly wedded to it. As practice, it might even be worth having separate types for things on that ring and things expected to not be on that ring. Whether tools to automatically check it exist or not, this at least moves it to be easier to inspect for.

Re:Understanding Integer Overflow in C/C++ (2012) [pdf] by burfog on Monday July 11, 2016 @12:59PM (#12072410)
Rust should have forced you to specify behavior whenever the compiler can't prove that the difference doesn't matter, like so:

1. default - fail the compile if an overflow could happen

2. wrap - like gcc's -fwrapv option, and C's unsigned type

3. trap - like gcc's -ftrapv option

4. saturate - common DSP behavior

5. unsafe - like gcc's C signed integer cruelty (fastest code)

Eh, is it too late to add this? For compatibility with old code, there could be a compiler option to override the default behavior for a whole source file.

Re:Understanding Integer Overflow in C/C++ (2012) [pdf] by the_why_of_y on Monday July 11, 2016 @06:07PM (#12075160)
You're right, there does not appear to be a way to achieve 1.

Rust arithmetic does 3. in debug builds, or something vaguely similar if requested by using the checked_* functions, and 2. in release builds, or if requested by using the wrapping_* functions, and 4. by using the saturating_* functions.

Recommended reading:

http://huonw.github.io/blog/2016/04/myths-and-legends-about-...

Re:Understanding Integer Overflow in C/C++ (2012) [pdf] by posterboy on Saturday July 09, 2016 @06:15PM (#12063590)
> big integer

If that's your use case, use it. Libraries like GMPrecision exist. Doesn't make a difference for one by off errors, e.g.

Re:Understanding Integer Overflow in C/C++ (2012) [pdf] by pcwalton on Saturday July 09, 2016 @01:40PM (#12062427)
Yes, 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.

Re:Understanding Integer Overflow in C/C++ (2012) [pdf] by posterboy on Saturday July 09, 2016 @05:54PM (#12063508)
If it's a fallacy, why wouldn't a programmer do good to avoid UB?
Re:Understanding Integer Overflow in C/C++ (2012) [pdf] by nkurz on Saturday July 09, 2016 @06:34PM (#12063655)
Avoiding 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.
Re:Understanding Integer Overflow in C/C++ (2012) [pdf] by posterboy on Sunday July 10, 2016 @11:08AM (#12065820)
By modus tollens, C can't be a good language, then. Gosh!
Re:Understanding Integer Overflow in C/C++ (2012) [pdf] by ArkyBeagle on Saturday July 09, 2016 @03:51PM (#12062990)
Almost 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.

Re:Understanding Integer Overflow in C/C++ (2012) [pdf] by 0xmohit on Sunday July 10, 2016 @01:50AM (#12064704)
Have you tried using tis-interpreter [0] (an interpreter of C for detecting undefined behavior written in OCaml) written by Pascal Cuoq [1].

[0] https://github.com/TrustInSoft/tis-interpreter

[1] http://stackoverflow.com/users/139746/pascal-cuoq

Re:Understanding Integer Overflow in C/C++ (2012) [pdf] by ArkyBeagle on Sunday July 10, 2016 @01:31PM (#12066333)
I cannot say that I have. Looks very interesting. Thanks!
Re:Understanding Integer Overflow in C/C++ (2012) [pdf] by jstarks on Saturday July 09, 2016 @04:42PM (#12063237)
I 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.
Re:Understanding Integer Overflow in C/C++ (2012) [pdf] by posterboy on Saturday July 09, 2016 @06:11PM (#12063567)
> 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

Re:Understanding Integer Overflow in C/C++ (2012) [pdf] by ArkyBeagle on Saturday July 09, 2016 @05:06PM (#12063329)
Yeah, 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.