detecting and preventing bugs with pluggable type-checking

31
Detecting and preventing bugs with pluggable type-checking Michael D. Ernst University of Washington Joint work with Werner Dietl, and many others http://types.cs.washington.edu/jsr308 print(@Readonly Object x) { List<@NonNull String> lst; }

Upload: nero

Post on 08-Feb-2016

31 views

Category:

Documents


0 download

DESCRIPTION

print( @ Readonly Object x) { List< @ NonNull String> lst ; … }. Detecting and preventing bugs with pluggable type-checking. Michael D. Ernst University of Washington Joint work with Werner Dietl , and many others http://types.cs.washington.edu/jsr308. Motivation. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Detecting and preventing bugs with pluggable type-checking

Detecting and preventing bugswith pluggable type-checking

Michael D. ErnstUniversity of Washington

Joint work with Werner Dietl, and many others

http://types.cs.washington.edu/jsr308

print(@Readonly Object x) { List<@NonNull String> lst; …}

Page 2: Detecting and preventing bugs with pluggable type-checking

Motivation

java.lang.NullPointerException

Page 3: Detecting and preventing bugs with pluggable type-checking

Java’s type checking is too weak

• Type checking prevents many bugsint i = “hello”; // type error

• Type checking doesn’t prevent enough bugs

System.console().readLine();Þ NullPointerException

Collections.emptyList().add(“One”);Þ UnsupportedOperationException

3

Page 4: Detecting and preventing bugs with pluggable type-checking

Some errors are silent

Date date = new Date(0);myMap.put(date, “Java epoch”);date.setYear(70);myMap.put(date, “Linux epoch”);Þ Corrupted map

dbStatement.executeQuery(userInput);Þ SQL injection attack

Initialization, data formatting, equality tests, …

4

Page 5: Detecting and preventing bugs with pluggable type-checking

Problem: Your code has bugs

• Who discovers the problems?– If you are very lucky, testing discovers (some of) them– If you are unlucky, your customer discovers them– If you are very unlucky, hackers discover them

– If you are smart, the compiler discovers them

• It’s better to be smart than lucky

5

Page 6: Detecting and preventing bugs with pluggable type-checking

Solution: Pluggable type systems

• Design a type system to solve a specific problem• Write type qualifiers in code (or, use type inference)

@Immutable Date date = new Date(0);date.setTime(70); // compile-time error

• Type checker warns about violations (bugs)

6

% javac -processor NullnessChecker MyFile.java

MyFile.java:149: dereference of possibly-null reference bb2 allVars = bb2.vars; ^

Page 7: Detecting and preventing bugs with pluggable type-checking

Outline

• Type qualifiers• Pluggable type checkers• Writing your own checker• Conclusion

7

Page 8: Detecting and preventing bugs with pluggable type-checking

Type qualifiers

• JSR 308 (in Java 8): annotations on types@Untainted String query;List<@NonNull String> strings;myGraph = (@Immutable Graph) tmpGraph;class UnmodifiableList<T> implements @Readonly List<@Readonly T> {}

• Backward-compatible (not in Java 8): compile with any Java compilerList</*@NonNull*/ String> strings;

8

Page 9: Detecting and preventing bugs with pluggable type-checking

Benefits of type qualifiers

• Find bugs in programs• Guarantee the absence of errors

• Improve documentation• Improve code structure & maintainability

• Aid compilers, optimizers, and analysis tools• Reduce number of assertions and run-time checks

• Possible negatives:– Must write the types (or use type inference)– False positives are possible (can be suppressed)

9

Page 10: Detecting and preventing bugs with pluggable type-checking

Outline

• Type qualifiers• Pluggable type checkers• Writing your own checker• Conclusion

10

Page 11: Detecting and preventing bugs with pluggable type-checking

What bugs can you detect & prevent? Null dereferences

@NonNullMutation and side-effects @ImmutableConcurrency: locking@GuardedBySecurity: encryption, @Encryptedtainting

@UntaintedAliasing

@LinearEquality tests

@InternedStrings: localization,@Localizedregular expression syntax,@Regexsignature format

@FullyQualifiedEnumeractions

@FenumTypestate (e.g., open/closed files) @StateUsers can write their own checkers!

The annotation you write:

11

The property you care about:

Page 12: Detecting and preventing bugs with pluggable type-checking

Using a checker

• Run in IDE or on command line• Works as a compiler plug-in (annotation processor)• Uses familiar error messages

% javac –processor NullnessChecker MyFile.java

MyFile.java:9: incompatible types. nonNullVar = nullableValue; ^found : @Nullable Stringrequired: @NonNull String

12

Page 13: Detecting and preventing bugs with pluggable type-checking

Checkers are effective

• Over 3,000,000 LOC checked• Each checker found errors in each code base it

ran on– Verified by a human and fixed

13

Page 14: Detecting and preventing bugs with pluggable type-checking

Recent case study results• Compiler messages: 8 wrong keys in Checker Framework• Fake enumerations: minor problems in Swing, JabRef• Signature strings: 28 errors in OpenJDK, ASM, AFU• Interning: >200 minor problems in Xerces, Lucene• Nullness: >200 errors in Google Collections, Daikon, javac• Regular expressions: 50 errors in Apache, etc.

• First-year CS majors used the Checker Framework– Stated they preferred using it to not

Page 15: Detecting and preventing bugs with pluggable type-checking

Comparison: other Nullness toolsNull pointer errors False

warnings Annotations writtenFound Missed

Checker Framework 8 0 4 35FindBugs 0 8 1 0Jlint 0 8 8 0PMD 0 8 0 0

• Checking the Lookup program for file system searching (4KLOC)• Distributed with Daikon (>100KLOC verified by our checker)

• False warnings are suppressed via an annotation or assertion• Also, errors in Google Collections (>20,000 tests, FindBugs) 15

Page 16: Detecting and preventing bugs with pluggable type-checking

Checkers are featureful

• Full type systems: inheritance, overriding, etc. • Generics (type polymorphism)

– Also qualifier polymorphism• Flow-sensitive type qualifier inference

– Infers types for local variables• Qualifier defaults• Warning suppression

16

Page 17: Detecting and preventing bugs with pluggable type-checking

Checkers are usable

• Integrated with toolchain– javac, Eclipse, Ant, Maven

• Few false positives• Annotations are not too verbose

– @NonNull: 1 per 75 lines• with program-wide defaults, 1 per 2000 lines

– @Interned: 124 annotations in 220KLOC revealed 11 bugs

– Possible to annotate part of program– Fewer annotations in new code

• Inference tools: nullness, mutability– Adds annotations throughout your program

17

Page 18: Detecting and preventing bugs with pluggable type-checking

What a checker guarantees

• The program satisfies the type property. There are:– no bugs (of particular varieties)– no wrong annotations

• Caveat 1: only for code that is checked– Native methods– Reflection– Code compiled without the pluggable type checker– Suppressed warnings

• Indicates what code a human should analyze– Checking part of a program is still useful

• Caveat 2: The checker itself might contain an error18

Page 19: Detecting and preventing bugs with pluggable type-checking

Annotating libraries

• Each checker comes with JDK annotations– For signatures, not bodies– Finds errors in clients, but not in the library itself

• Inference tools for annotating new libraries

19

Page 20: Detecting and preventing bugs with pluggable type-checking

Outline

• Type qualifiers• Pluggable type checkers• Writing your own checker• Conclusion

20

Page 21: Detecting and preventing bugs with pluggable type-checking

SQL injection attack

• Server code bug: SQL query constructed using unfiltered user inputquery = “SELECT * FROM users ” + “WHERE name=‘” + userInput + “’;”;

• User inputs: a’ or ‘1’=‘1• Result:

query Þ SELECT * FROM users WHERE name=‘a’ or ‘1’=‘1’;

• Query returns information about all users21

Page 22: Detecting and preventing bugs with pluggable type-checking

Taint checker

To use it:1. Write @Untainted in your program List getPosts(@Untainted String category) {…}

2. Compile your program javac -processor BasicChecker -Aquals=Untainted MyProgram.java

@TypeQualifier@SubtypeOf(Unqualified.class)@ImplicitFor(trees = {STRING_LITERAL})public @interface Untainted { }

22

Page 23: Detecting and preventing bugs with pluggable type-checking

Defining a type system

@TypeQualifierpublic @interface NonNull { }

23

Page 24: Detecting and preventing bugs with pluggable type-checking

Defining a type system

1. Qualifier hierarchy – rules for assignment2. Type introduction – types for

expressions3. Type rules – checker-

specific errors

@TypeQualifierpublic @interface NonNull { }

24

Page 25: Detecting and preventing bugs with pluggable type-checking

Defining a type system

1. Qualifier hierarchy2. Type introduction3. Type rules

@TypeQualifier@SubtypeOf( Nullable.class )public @interface NonNull { }

25

What assignments are legal:

Page 26: Detecting and preventing bugs with pluggable type-checking

Defining a type system

1. Qualifier hierarchy2. Type introduction3. Type rules

@TypeQualifier@SubtypeOf( Nullable.class )@ImplicitFor(trees={ NEW_CLASS, PLUS, BOOLEAN_LITERAL, ... } )public @interface NonNull { }

26

new Date()“hello ” + getName()Boolean.TRUE

Gives the type of expressions:

Page 27: Detecting and preventing bugs with pluggable type-checking

Defining a type system

1. Qualifier hierarchy2. Type introduction3. Type rules

void visitSynchronized(SynchronizedTree node) { ExpressionTree expr = node.getExpression(); AnnotatedTypeMirror type = getAnnotatedType(expr); if (! type.hasAnnotation(NONNULL)) checker.report(Result.failure(...), expr);}

27

synchronized(expr) { …}

Warn if expr may be null

Errors for unsafe code:

Page 28: Detecting and preventing bugs with pluggable type-checking

Outline

• Type qualifiers• Pluggable type checkers• Writing your own checker• Conclusion

28

Page 29: Detecting and preventing bugs with pluggable type-checking

Proposed research for next grant

• Detect/prevent security vulnerabilities with pluggable type-checking

• CWE/SANS Top 25 Most Dangerous Software Errors– http://cwe.mitre.org/top25/

• We have sketched a type system design, but not implemented it, for each of these

Page 30: Detecting and preventing bugs with pluggable type-checking

1. Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')2. Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection')3. Buffer Copy without Checking Size of Input ('Classic Buffer Overflow')4. Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting')5. Missing Authentication for Critical Function6. Missing Authorization7. Use of Hard-coded Credentials8. Missing Encryption of Sensitive Data9. Unrestricted Upload of File with Dangerous Type10. Reliance on Untrusted Inputs in a Security Decision11. Execution with Unnecessary Privileges12. Cross-Site Request Forgery (CSRF)13. Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')14. Download of Code Without Integrity Check15. Incorrect Authorization16. Inclusion of Functionality from Untrusted Control Sphere17. Incorrect Permission Assignment for Critical Resource18. Use of Potentially Dangerous Function19. Use of a Broken or Risky Cryptographic Algorithm20. Incorrect Calculation of Buffer Size21. Improper Restriction of Excessive Authentication Attempts22. URL Redirection to Untrusted Site ('Open Redirect')23. Uncontrolled Format String24. Integer Overflow or Wraparound25. Use of a One-Way Hash without a Salt

Page 31: Detecting and preventing bugs with pluggable type-checking

Pluggable type-checking

• Java 8 syntax for type annotations• Checker Framework for creating type checkers

– Featureful, effective, easy to use, scalable• Prevent bugs at compile time• Create custom type-checkers• Learn more, or download the Checker Framework:

http://types.cs.washington.edu/jsr308 (or, web search for “Checker Framework” or “JSR 308”)

31