UNDERSTANDING INTEGER OVERFLOW IN CC Will Dietz Peng

  • Slides: 22
Download presentation
UNDERSTANDING INTEGER OVERFLOW IN C/C++ Will Dietz Peng Li John Regehr Vikram Adve

UNDERSTANDING INTEGER OVERFLOW IN C/C++ Will Dietz Peng Li John Regehr Vikram Adve

2 Why Integer Overflows in C/C++ Overflows are a serious source of bugs! �

2 Why Integer Overflows in C/C++ Overflows are a serious source of bugs! � Ariane 5 Rocket Explosion (‘ 96) Overflow � “Top 25 Most Dangerous Software Errors” ~MITRE 2011 What can we do about this? Presented by Will Dietz, University of Illinois at Urbana-Champaign. ICSE'12.

Towards an Understanding 3 How can we classify integer overflows? How common are overflows

Towards an Understanding 3 How can we classify integer overflows? How common are overflows in real code? How common are undefined overflows? � Undefined Program has no meaning When and for what purpose is it used intentionally? Objective: Answer these empirically for real Presented by Will Dietz, University of Illinois at Urbana-Champaign. code ICSE'12.

Everywhere We Looked 4 Intentional overflow occurs often � Over 200 locations in SPEC

Everywhere We Looked 4 Intentional overflow occurs often � Over 200 locations in SPEC CINT 2000 Undefined overflow bugs in most programs analyzed Even skilled developers get this wrong � Microsoft’s Safe. Int, CERT’s Integer. Lib Presented by Will Dietz, University of Illinois at Urbana-Champaign. ICSE'12.

What’s Coming 5 Integer Overflows in C/C++ Overflow Taxonomy IOC: Integer Overflow Checker Results:

What’s Coming 5 Integer Overflows in C/C++ Overflow Taxonomy IOC: Integer Overflow Checker Results: � Case Study: SPEC CINT 2000 Overflows in Real Applications Time Bombs Conclusions Presented by Will Dietz, University of Illinois at Urbana-Champaign. ICSE'12.

What is Integer Overflow? 6 Simply: Value doesn’t fit in data type � Integer

What is Integer Overflow? 6 Simply: Value doesn’t fit in data type � Integer Arithmetic, Shifts, Casts, … Example: What does this code print? ? ? 0 Presented by Will Dietz, University of Illinois at Urbana-Champaign. ICSE'12.

Overflows are useful 7 Overflow has many legitimate uses in real code Hashing, PRNG,

Overflows are useful 7 Overflow has many legitimate uses in real code Hashing, PRNG, Cryptography, . . . Example from 175. vpr: Presented by Will Dietz, University of Illinois at Urbana-Champaign. ICSE'12.

Not always so simple 8 What does this code do? GCC, LLVM, Intel: Print

Not always so simple 8 What does this code do? GCC, LLVM, Intel: Print “ 0” then “ 1” Why? Presented by Will Dietz, University of Illinois at Urbana-Champaign. ICSE'12.

Undefined Behavior 9 In C/C++, some integer operations are undefined � Undefined Program has

Undefined Behavior 9 In C/C++, some integer operations are undefined � Undefined Program has no meaning Result What operations are undefined? Expression UINT_MAX + 1 0 INT_MAX + 1 Undefined SHRT_MAX + SHRT_MAX+1 if INT_MAX > SHRT_MAX, otherwise 1 undefined 1 << 31 INT_MIN in ANSI C/C++98; Undefined in C 99/C++11 1 << 32 Undefined INT_MIN % -1 by Will Undefined in C 11, otherwise undefined in practice Presented Dietz, University of Illinois at Urbana-Champaign. … … ICSE'12.

Well-defined can be bugs too 10 Real bug we found in gzip: What happens

Well-defined can be bugs too 10 Real bug we found in gzip: What happens when d > w? � Expression pass overflows to large value making check Went 7 years undetected, fixed twice Overflows are tricky! Presented by Will Dietz, University of Illinois at Urbana-Champaign. ICSE'12.

Overflow Taxonomy 11 Undefined behavior Intentional Unintention al Legal May not be portable Implementation

Overflow Taxonomy 11 Undefined behavior Intentional Unintention al Legal May not be portable Implementation Defined by error language All 4 potentially sources of bugs… � …but Design error “Time Bomb” Likely bug Defined behavior none are necessarily vulnerabilities How frequently do these occur in real Presented code? by Will Dietz, University of Illinois at Urbana-Champaign. ICSE'12. Intent

Tool Needed 12 IOC: Integer Overflow Checker Based on Clang, LLVM’s C/C++ frontend Automatic

Tool Needed 12 IOC: Integer Overflow Checker Based on Clang, LLVM’s C/C++ frontend Automatic checking of integer behavior Example output from Open. SSL bug: <lhash. c, (464: 20)> : Op: >>, Reason : Unsigned Right Shift Error: Right operand is negative or is greater than or equal to the width of the promoted left operand, BINARY OPERATION: left (uint 32): 4103048108 right (uint 32): 32. Download now: http: //embed. cs. utah. edu/ioc � Coming soon to a Clang release near you Great for bug finding! Presented by Will Dietz, University of Illinois at Urbana-Champaign. ICSE'12.

Case Study: SPEC CINT 2000 13 Built the 12 CINT 2000 benchmarks with IOC

Case Study: SPEC CINT 2000 13 Built the 12 CINT 2000 benchmarks with IOC � Ran using the “ref” data sets Analyzed each reported overflow by hand Found 219 distinct locations of overflow: Static Locations of Overflow by Benchmark 85 100 80 60 40 20 0 16 48 5 ip pr v. 5 z 4. g 8 17 41 27 4 c 17 c 6. g 1 r ty 6 18 af r c. e ars . p 7 19 2 . p 53 e m rlb k ex t r. vo p a g. 54 2 Presented by Will Dietz, University of Illinois at Urbana-Champaign. ICSE'12. 5 25

CINT 2000: Overflows by Type 14 Overflows by Definedness Well-defined overflows occurred much more

CINT 2000: Overflows by Type 14 Overflows by Definedness Well-defined overflows occurred much more frequently than expected 71 ~1/3 overflows used undefined behavior! 148 Undefined Defined Overflow of all types occurs frequently Presented by Will Dietz, University of Illinois at Urbana-Champaign. ICSE'12.

CINT 2000: Overflows by Idiom 15 Top Overflow Idioms in CINT 2000 Other: •

CINT 2000: Overflows by Idiom 15 Top Overflow Idioms in CINT 2000 Other: • Compute INT_MAX • -INT_MIN • Unused values • Type promotion Hashing Random Num Gen 6 23 Hashing is by far the most common 25 37 128 Overflow check Other Bit manipulation Many legitimate uses of overflow Presented by Will Dietz, University of Illinois at Urbana-Champaign. ICSE'12.

16 Bug Hunting: Open Source Applications Experiment: Build applications with IOC, run “make test”

16 Bug Hunting: Open Source Applications Experiment: Build applications with IOC, run “make test” or similar Found undefined overflows are nearly everywhere: Bug reports: well received, fixed promptly Only three were free of undefined overflow � Highly skilled programmers get this wrong � Kerberos, libpng, libjpeg Microsoft’s Safe. Int, CERT’s Integer. Lib Undefined Overflows are (nearly) everywhere Presented by Will Dietz, University of Illinois at Urbana-Champaign. ICSE'12.

Time Bombs: SPEC 2006 17 Experiment: Replace undefined behavior with random value Benchmark ANSI

Time Bombs: SPEC 2006 17 Experiment: Replace undefined behavior with random value Benchmark ANSI C/C++98 C 99/C++11 400. perlbench Pass 401. bzip 2 Pass Fail 403. gcc Fail 433. milc Fail 435. gromacs Pass 436. cactus. ADM Pass Fail 445. gobmk Pass 464. h 264 ref Pass Fail 482. sphix 3 Pass Fail Standards-conforming compiler breaks SPEC! Changing standards complicate ensuring correct behavior Presented by Will Dietz, University of Illinois at Urbana-Champaign. ICSE'12.

Conclusions: 18 Overflows are a serious source of bugs � …but there are many

Conclusions: 18 Overflows are a serious source of bugs � …but there are many legitimate uses of overflow Overflows of all types occur frequently in real code Overflow can be extremely tricky to get right � Highly skilled developers get this wrong Check your code with IOC (or similar) � Look forward to IOC shipping with Clang soon! � http: //embed. cs. utah. edu/ioc Security solution unclear, research needed! Thank you! Questions? Presented by Will Dietz, University of Illinois at Urbana-Champaign. ICSE'12.

FAQ 1 19 Why not just use –fwrapv? Only addresses undefined part of problem

FAQ 1 19 Why not just use –fwrapv? Only addresses undefined part of problem Still many bugs! � Data makes it clear that developers don’t know where overflows are occurring Performance implications � Loop bounds � “x+1>x”, “x*2/x”, etc Why not use well-defined behavior? Presented by Will Dietz, University of Illinois at Urbana-Champaign. ICSE'12.

FAQ 2 20 SPEC works for everyone, are the overflows you found actual bugs?

FAQ 2 20 SPEC works for everyone, are the overflows you found actual bugs? Undefined behavior is bug waiting to happen Code should never deviate from what you intend! Volume of integer overflow CVE’s indicates overflows can be serious problems Presented by Will Dietz, University of Illinois at Urbana-Champaign. ICSE'12.

FAQ 3 21 SPEC experiment was subjective, isn’t that a problem? (and perhaps should

FAQ 3 21 SPEC experiment was subjective, isn’t that a problem? (and perhaps should have been checked by others? ) No! Few miscategorizations don’t change the important conclusions � Examples of ways overflows are used intentionally � There’s a variety of ways overflows are used � (Results don’t generalize anyway) Listing of all reported overflows in paper, full details happily available upon request. Presented by Will Dietz, University of Illinois at Urbana-Champaign. ICSE'12.

FAQ 4 22 � If I know the exact platform/compiler/build system/etc, why should I

FAQ 4 22 � If I know the exact platform/compiler/build system/etc, why should I care? � You don’t have to, of course. We all have deadlines or projects that aren’t mission critical. � Data indicates developers often get this wrong, even when considering it explicitly. � Most code lives for a long time, and environment often changes. Undefined has been known to break with a compiler upgrade, for example. � Checking your software with IOC doesn’t hurt anymore than checking with valgrind Presented by Will Dietz, University of Illinois at Urbana-Champaign. ICSE'12.