Improving Software Quality with Static Analysis

Similar documents
Improving Software Quality with Static Analysis and Annotations for Software Defect Detection

Software quality is important, but often imperfect in practice. We can use many

Static Analysis Techniques for Testing Application Security. OWASP San Antonio January 31 st, 2008

Static Analysis Techniques for Testing Application Security. OWASP Austin March 25 th, 2008

Writing new FindBugs detectors

TOOL EVALUATION REPORT: FORTIFY

Will Dormann: Sure. Fuzz testing is a way of testing an application in a way that you want to actually break the program.

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

Automatic vs. Manual Code Analysis

Procedure of Secure Development Tool Adoption Study

Web Application Security. Vulnerabilities, Weakness and Countermeasures. Massimo Cotelli CISSP. Secure

<Insert Picture Here> What's New in NetBeans IDE 7.2

Source Code Review Using Static Analysis Tools

ABSTRACT TO FIND BUGS. Department of Computer Science. can be found using simple analysis techniques. We have found that simple static

Application Code Development Standards

Data Flow Static Code Analysis Best Practices

To Java SE 8, and Beyond (Plan B)

Software infrastructure for Java development projects

Integrated Error-Detection Techniques: Find More Bugs in Java Applications

No no-argument constructor. No default constructor found

Magento Security and Vulnerabilities. Roman Stepanov

Coding in Industry. David Berry Director of Engineering Qualcomm Cambridge Ltd

Java Application Developer Certificate Program Competencies

Java Software Quality Tools and techniques

Adobe Systems Incorporated

Webapps Vulnerability Report

Ontology Model-based Static Analysis on Java Programs

Java Interview Questions and Answers

Software security specification and verification

CS 111 Classes I 1. Software Organization View to this point:

The Challenge of Helping Adults Learn: Principles for Teaching Technical Information to Adults

Evaluating Static Analysis Defect Warnings On Production Software

Visualizing Information Flow through C Programs

IBM Rational Software

Linux Kernel. Security Report

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

APPROACHES TO SOFTWARE TESTING PROGRAM VERIFICATION AND VALIDATION

What is Web Security? Motivation

Web development... the server side (of the force)

CHAPTER 5 INTELLIGENT TECHNIQUES TO PREVENT SQL INJECTION ATTACKS

Secure Programming with Static Analysis. Jacob West

Java Web Application Security

Web Application Vulnerabilities

Table of Contents. Java CGI HOWTO

Coverity Scan. Big Data Spotlight

Detecting Critical Defects on the Developer s Desktop

Everyday Lessons from Rakudo Architecture. Jonathan Worthington

Comparing the Effectiveness of Penetration Testing and Static Code Analysis

Finding Execution Faults in Dynamic Web Application

Outline. 1 Denitions. 2 Principles. 4 Implementation and Evaluation. 5 Debugging. 6 References

Static Checking of C Programs for Vulnerabilities. Aaron Brown

Towards Checking the Usefulness of Verification Tools

Build management & Continuous integration. with Maven & Hudson

Oracle Solaris Studio Code Analyzer

Web application security: automated scanning versus manual penetration testing.

So with no further ado, welcome back to the podcast series, Robert. Glad to have you today.

Out of the Fire - Adding Layers of Protection When Deploying Oracle EBS to the Internet

Introduction to Static Analysis for Assurance

Sandy. The Malicious Exploit Analysis. Static Analysis and Dynamic exploit analysis. Garage4Hackers

Official Android Coding Style Conventions

Integrating Web Application Security into the IT Curriculum

Application security testing: Protecting your application and data

How to Avoid an Attack - Security Testing as Part of Your Software Testing Process

Evaluation of AgitarOne

Secure Software Programming and Vulnerability Analysis

The FDA Forensics Lab, New Tools and Capabilities

Seven Deadly Sins of Debugging

In the name of God. 1 Introduction. 2 General Plan

Improving Software Quality with the Continuous Integration Server Hudson. Dr. Ullrich Hafner Avaloq Evolution AG 8911

Early Vulnerability Detection for Supporting Secure Programming

A Test Suite for Basic CWE Effectiveness. Paul E. Black.

e ag u g an L g ter lvin v E ram Neal G g ro va P Ja

Fundamentals of Java Programming

CS 558 Internet Systems and Technologies

Put a Firewall in Your JVM Securing Java Applications!

An Eclipse Plug-In for Visualizing Java Code Dependencies on Relational Databases

Iron Chef: John Henry Challenge

HTTPParameter Pollution. ChrysostomosDaniel

Review of source code analyzers. Gerard Frankowski, Tomasz Nowak PSNC Poznań, June connect communicate collaborate

Introduction to Automated Testing

Security features of ZK Framework

Application Security Testing. Erez Metula (CISSP), Founder Application Security Expert

Security Tools - Hands On

Passing PCI Compliance How to Address the Application Security Mandates

Software Engineering Techniques

CSE598i - Web 2.0 Security OWASP Top 10: The Ten Most Critical Web Application Security Vulnerabilities

Java 7 Recipes. Freddy Guime. vk» (,\['«** g!p#« Carl Dea. Josh Juneau. John O'Conner

Intrusion detection for web applications

An Newsletter Using ASP Smart Mailer and Advanced HTML Editor

STATIC CODE ANALYSIS Alexandru G. Bardas 1

Web Application Penetration Testing

Static Code Analysis Procedures in the Development Cycle

Tutorial: Getting Started

Successful Strategies for QA- Based Security Testing

The Top Web Application Attacks: Are you vulnerable?

Detection of SQL Injection and XSS Vulnerability in Web Application

OWASP and OWASP Top 10 (2007 Update) OWASP. The OWASP Foundation. Dave Wichers. The OWASP Foundation. OWASP Conferences Chair

How I Learned to Stop Fuzzing and Find More Bugs

Module 8 Increase Conversions by 29% for EACH Branch through Technology! What You'll Learn in this Module...

Comparing Application Security Tools

Transcription:

Improving Software Quality with Static Analysis William Pugh Professor Univ. of Maryland http://www.cs.umd.edu/~pugh TS-2007 2007 JavaOneSM Conference Session TS-2007

You will believe... Static analysis tools can find real bugs and real issues in your code. You can and should effectively incorporate static analysis into your software development process. 2007 JavaOne SM Conference Session TS-2007 2

Agenda Introduction Correctness issues Bad Practice Security defects Demos (FindBugs, Fortify SCA) Integrating static analysis Wrap up 2007 JavaOne SM Conference Session TS-2007 3

About Me Professor at Univ. of Maryland since 1988, doing research in programming languages, algorithms, software engineering Technical Lead on JSR-133 (Memory model), JSR- 305 (Annotations for Software Defect Detection) Founder of the FindBugs project Open source static analysis tool for defect detection in the Java Programming Language Technical advisory board of 2007 JavaOne SM Conference Session TS-2007 4

Agenda Introduction Correctness issues Bad Practice Security defects Demos (FindBugs, Fortify SCA) Integrating static analysis Wrap up 2007 JavaOne SM Conference Session TS-2007 5

Static Analysis Analyzes your program without executing it Doesn t depend on having good test cases or even any test cases Generally, doesn t know what your software is supposed to do Looks for violations of reasonable programming Shouldn t throw NPE Shouldn t allow SQL injection Not a replacement for testing Very good at finding problems on untested paths But many defects can t be found with static analysis 2007 JavaOne SM Conference Session TS-2007 6

Common Wisdom about Bugs and Static Analysis Programmers are smart Smart people don t make dumb mistakes We have good techniques (e.g., unit testing, pair programming, code inspections) for finding bugs early So, bugs remaining in production code must be subtle, and finding them must require sophisticated static analysis techniques I tried lint and it sucked: lots of warnings, few real issues 2007 JavaOne SM Conference Session TS-2007 7

Can You Find The Bug? if (listeners == null) listeners.remove(listener); JDK1.6.0, b105, sun.awt.x11.xmselection lines 243-244 2007 JavaOne SM Conference Session TS-2007 8

Why Do Bugs Occur? Nobody is perfect Common types of errors: Misunderstood language features, API methods Typos (using wrong boolean operator, forgetting parentheses or brackets, etc.) Misunderstood class or method invariants Everyone makes syntax errors, but the compiler catches them What about bugs one step removed from a syntax error? 2007 JavaOne SM Conference Session TS-2007 9

Who Uses Static Analysis? Lots and lots of projects and companies Among many others, Glassfish and Google use FindBugs Many companies are weird about letting you say they use your open source tool Lots of open source tools: PMD, CheckStyle, etc. IDEs include some: Eclipse, IntelliJ, Netbeans Commercial tools available from Fortify Software, KlocWork, Coverity, Parasoft, SureLogic Static analysis used even more widely/intensely for C/C++ More bugs to find Bugs a lot scarier Free tools not as good 2007 JavaOne SM Conference Session TS-2007 10

FindBugs I'm mostly going to be talking about FindBugs I know it best Some things will be specific to FindBugs What we classify as a "correctness" issue Which potential null pointer issues we report But most of the concepts apply to other tools 2007 JavaOne SM Conference Session TS-2007 11

Bug Categories Selected categories for today's discussion Correctness - the code seems to be clearly doing something the developer did not intend Bad practice - the code violates good practice Security defect Vulnerability to malicious code Vulnerability to malicious input SQL injection, cross site scripting 2007 JavaOne SM Conference Session TS-2007 12

Bug Patterns Some big, broad and common patterns Dereferencing a null pointer An impossible checked cast Methods whose return value should not be ignored Lots of small, specific bug patterns, that together find lots of bugs Every Programming Puzzler Every chapter in Effective Java Most postings to http://thedailywtf.com/ 2007 JavaOne SM Conference Session TS-2007 13

Analysis Techniques Whatever you need to find the bugs Local pattern matching If you invoke String.toLowerCase(), don t ignore the return value Intraprocedural dataflow analysis Null pointer, type cast errors Interprocedural method summaries This method always dereferences its parameter Context sensitive interprocedural analysis Interprocedural flow of untrusted data SQL injection, cross site scripting 2007 JavaOne SM Conference Session TS-2007 14

Categories, ranking, use cases Every tool has categories, rules/patterns, priorities You can generally customize what you want to look at Sometimes, you want to do a code audit of a newly written module with 1,000 lines of code and sometimes you want to scan 1,000,000 lines of code that has been in production for a year Different use cases require different tunings, different tools 2007 JavaOne SM Conference Session TS-2007 15

Agenda Introduction Correctness issues Bad Practice Security defects Demos (FindBugs, Fortify SCA) Integrating static analysis Wrap up 2007 JavaOne SM Conference Session TS-2007 16

Correctness issues Stuff you really want to look at In FindBugs, we reserve the Correctness category for issues we are most confident are wrong code does something the developer didn t intend Many of the other categories reflect correctness issues But correctness issues are the things we think you should look at when scanning that million line code base low false positive rate, few low impact bugs 2007 JavaOne SM Conference Session TS-2007 17

Infinite recursive loop... Students are good bug generators Student came to office hours, was having trouble with his constructor: /** Construct a WebSpider */ public WebSpider() { WebSpider w = new WebSpider(); } A second student had the same bug Wrote a detector, found 3 other students with same bug 2007 JavaOne SM Conference Session TS-2007 18

Double Check Against JDK1.6.0-b13 Found 5 infinite recursive loops Including one written by Joshua Bloch public String foundtype() { return this.foundtype(); } Smart people make dumb mistakes 27 across all versions of JDK, 31 in Google s Java code Embrace and fix your dumb mistakes 2007 JavaOne SM Conference Session TS-2007 19

Finding Null Pointer Bugs with FindBugs FindBugs looks for a statement or branch that, if executed, guarantees a null pointer exception Either a null pointer exception could be thrown, or the program contains a statement/branch that can t be executed Could look for exceptions that only occur on a path e.g., if the condition on line 29 is true and the condition on line 38 is false, then a NPE will be thrown but would need to worry about whether that path is feasible 2007 JavaOne SM Conference Session TS-2007 20

Null Pointer Bugs Found by FindBugs JDK1.6.0-b105 109 statements/branches that, if executed, guarantee NPE We judge at least 54 of them to be serious bugs that could generate a NPE on valid input Most of the others were deemed to be unreachable branches or statements, or reachable only with erroneous input Only one case where the analysis was wrong 2007 JavaOne SM Conference Session TS-2007 21

Examples of null pointer bugs simple ones //com.sun.corba.se.impl.naming.cosnaming.namingcontextimpl if (name!= null name.length > 0) //com.sun.xml.internal.ws.wsdl.parser.runtimewsdlparser if (part == null part.equals("")) // sun.awt.x11.scrollpanepeer if (g!= null) paintscrollbars(g,colors); g.dispose(); 2007 JavaOne SM Conference Session TS-2007 22

Redundant Check For Null Also known as a reverse null dereference error Checking a value to see if it is null When it can't possibly be null // java.awt.image.loopupop, lines 236-247 public final WritableRaster filter( Raster src, WritableRaster dst) { int dstlength = dst.getnumbands(); // Create a new destination Raster, // if needed if (dst == null) dst = createcompatibledestraster(src); 2007 JavaOne SM Conference Session TS-2007 23

Redundant Check For Null Is it a bug or a redundant check? Check the JavaDoc for the method Performs a lookup operation on a Raster. If the destination Raster is null, a new Raster will be created. Is this case, a bug particularly look for those cases where we know it can't be null because there would have been a NPE if it were null 2007 JavaOne SM Conference Session TS-2007 24

Bad Method Invocation Methods whose return value shouldn't be ignored Strings are immutable, so functions like trim() and tolowercase() return new String Dumb/useless methods Invoking tostring or equals on an array Lots of specific rules about particular API methods Hard to memorize, easy to get wrong 2007 JavaOne SM Conference Session TS-2007 25

Examples of bad method calls // com.sun.rowset.cachedrowsetimpl if (type == Types.DECIMAL type == Types.NUMERIC) ((java.math.bigdecimal)x).setscale(scale); // com.sun.xml.internal.txw2.output.xmlwriter try {... } catch (IOException e) { new SAXException("Server side Exception:" + e); } 2007 JavaOne SM Conference Session TS-2007 26

Type Analysis Impossible checked casts Useless calls equals takes an Object as a parameter but comparing a String to StringBuffer with equals(...) is pointless, and almost certainly not what was intended Map<K,V>.get also takes an Object as a parameter supplying an object with the wrong type as a parameter to get doesn't generate a compile time error just a get that always returns null 2007 JavaOne SM Conference Session TS-2007 27

Lots of Little Bug Patterns checking if d == Double.NaN Bit shifting an int by a value greater than 31 bits Every Puzzler this year more than half for most years 2007 JavaOne SM Conference Session TS-2007 28

When Bad Code Isn't A Bug Static analysis tools will sometimes find ugly, nasty code that can't cause your application to misbehave Cleaning this up is a good thing makes the code easier to understand and maintain But for ugly code already in production sometimes you just don't want to touch it We've found more cases like this than we expected 2007 JavaOne SM Conference Session TS-2007 29

When Bad Code Isn't A Bug bad code that does what it was intended to do // com.sun.jndi.dns.dnsname, lines 345-347 if (n instanceof CompositeName) { // force ClassCastException n = (DnsName) n; } // sun.jdbc.odbc.jdbcodbcobject, lines 85-91 if ((b[offset] < 32) (b[offset] > 128)) { asciiline += "."; } 2007 JavaOne SM Conference Session TS-2007 30

When Bad Code Isn't A Bug Code that shouldn't go wrong // com.sun.corba.se.impl.dynamicany.dynanycompleximpl String expectedmembername = null; try { expectedmembername = expectedtypecode.member_name(i); } catch (BadKind badkind) { // impossible } catch (Bounds bounds) { // impossible } if (!(expectedmembername.equals(membername)... )) { 2007 JavaOne SM Conference Session TS-2007 31

When Bad Code Isn't A Bug When you are already doomed // com.sun.org.apache.xml.internal.security.encryption.xmlciper // lines 2224-2228 if (null == element) { //complain } String algorithm = element.getattributens(...); 2007 JavaOne SM Conference Session TS-2007 32

Overall Correctness Results From FindBugs Evaluating Static Analysis Defect Warnings On Production Software, ACM 2007 Workshop on Program Analysis for Software Tools and Engineering JDK1.6.0-b105 379 correctness warnings we judge that at least 213 of these are serious issues that should be fixed Google's Java codebase over a 6 month period, using various versions of FindBugs 1,127 warnings 807 filed as bugs 518 fixed in code 2007 JavaOne SM Conference Session TS-2007 33

Agenda Introduction Correctness issues Bad Practice Security defects Demos (FindBugs, Fortify SCA) Integrating static analysis Wrap up 2007 JavaOne SM Conference Session TS-2007 34

Bad Practice A class that defines an equals method but inherits hashcode from Object Violates contract that any two equal objects have the same hash code equals method doesn't handle null argument Serializable class without a serialversionuid Exception caught and ignored Broken out from the correctness category because I never want a developer to yawn when I show them a "correctness" bug 2007 JavaOne SM Conference Session TS-2007 35

Fixing hashcode What if you want to define equals, but don't think your objects will ever get put into a HashMap? Suggestion: public int hashcode() { assert false : "hashcode method not designed"; return 42; } 2007 JavaOne SM Conference Session TS-2007 36

Use of Unhashable Classes FindBugs previously reported all classes that defined equals but not hashcode as a correctness problem but some developers didn t care Now reported as bad practice but separately report use of such a class in a HashMap/HashTable as a correctness warning 2007 JavaOne SM Conference Session TS-2007 37

Agenda Introduction Correctness issues Bad Practice Security defects Demos (FindBugs, Fortify SCA) Integrating static analysis Wrap up 2007 JavaOne SM Conference Session TS-2007 38

Security defects Vulnerability to untrusted, malicious code Do you have any public static non-final fields? Vulnerability to untrusted, malicious input Can untrusted input, perhaps from user of a web application, force your program to things it shouldn't? 2007 JavaOne SM Conference Session TS-2007 39

Vulnerability to Untrusted, Malicious Code JDK1.6.0-b105 220 mutable public static fields non-final fields final references to mutable objects (e.g., arrays) 327 methods that return references to mutable internal components (e.g., arrays, Date) caller can then change internal state 311 methods that take references to mutable objects as parameters and make them part of the internal state 2007 JavaOne SM Conference Session TS-2007 40

Why haven't these been fixed? Any untrusted applet can change the static fields Sun's security team is aware of the issue They warn against public static non-final fields TS-2594 - Secure Coding Guidelines, Continued: Preventing Attacks and Avoiding Antipatterns They say they will try to address it in JDK7 We'll see; hold their feet to the fire on this one. Is backwards compatibility a problem? "We can't make javax.swing.defaultlistcellrenderer.nofocusborder final, because some code might depend upon being able to change it?" Some code deserves to be broken 2007 JavaOne SM Conference Session TS-2007 41

Vulnerability to untrusted, malicious input Be glad you aren't working in C/C++ But still lots of issues to be worried about SQL Injection Cross site scripting (XSS) - getting to be big issue HTTP Response splitting Path traversal If you write network facing code, and aren't worried/paranoid about these issues, you are being irresponsible 2007 JavaOne SM Conference Session TS-2007 42

SQL Injection Forming SQL queries using string concatenation String query = "SELECT cc_type, cc_number FROM " + "user_data WHERE last_name = '" + user + "'"; Can usually avoid by using SQL prepared statements with constant Strings Can just look for non-constant SQL query strings, or look deeper to find sources of data used to build query strings 2007 JavaOne SM Conference Session TS-2007 43

Cross-Site Scripting Untrusted input from user included verbatim in HTML response Can be exploited by crafting a URL that a victim clicks on Generates a response from your web site that includes JavaScript that does nasty stuff e.g., clicks "Buy now!" Also HTTP response splitting Untrusted input included in HTTP response headers 2007 JavaOne SM Conference Session TS-2007 44

Path Traversal Forming a file path using untrusted input Not checking for "../../../yoursecrets.xml" 2007 JavaOne SM Conference Session TS-2007 45

Building Security In Generally, need deeper, interprocedural analysis to connect untrusted input to places that require trusted values Automated tools helpful, perhaps essential but not sufficient Need to build security in, not bolt it on afterwards risk analysis, architecture, abuse cases Need training, expertise, tools, effort generally not cheap or easy but necessary 2007 JavaOne SM Conference Session TS-2007 46

Agenda Introduction Correctness issues Bad Practice Security defects Demos (FindBugs, Fortify SCA) Integrating static analysis Wrap up 2007 JavaOne SM Conference Session TS-2007 47

DEMO FindBugs and Fortify SCA 2007 JavaOne SM Conference Session TS-2007 48

Agenda Introduction Correctness issues Bad Practice Security defects Demos (FindBugs, Fortify SCA) Integrating static analysis Wrap up 2007 JavaOne SM Conference Session TS-2007 49

Integrating Static Analysis Want to make it part of your development process Just like running unit tests Have to tune the tool to report what you are interested in Different situations have different needs Need a workflow for issues Almost all tools will report some issues that, after reviewing, you decide not to fix Need to have a way to manage such issues 2007 JavaOne SM Conference Session TS-2007 50

Running Static Analysis "We've got it in our IDE, so we're done, right?" no, it really needs to also be done automatically as part of your build process Are you scanning 2 million lines of code? You probably don't want 20,000 issues to examine 2007 JavaOne SM Conference Session TS-2007 51

Defect/Issue Workflow How do issues get reviewed/audited? Can you do team auditing and assign issues? Once you've reviewed an issue, does the system remember your evaluation when it analyzes that code again? even if it is now reported on a different line number? Can you identify new issues since last build? since last release to customer/production? 2007 JavaOne SM Conference Session TS-2007 52

Learning from mistakes With FindBugs, we've always started from bugs We need API experts to feed us API-specific bugs Swing, EJB, J2ME, localization, Hibernate,... When you get bit by a bug writing a test case is good considering whether it can be generalized into a bug pattern is better You'd be surprised at the number of times you make a mistake so stupid no one else could possible make the same mistake but they do 2007 JavaOne SM Conference Session TS-2007 53

Agenda Introduction Correctness issues Bad Practice Security defects Demos (FindBugs, Fortify SCA) Integrating static analysis Wrap up 2007 JavaOne SM Conference Session TS-2007 54

Getting Started If you do nothing else, try FindBugs No salesman will call Check out medium/high priority correctness warnings That should provide some motivation to get started But you really want to take it to the next level, find out what tools work best for you, make it part of your development process not a casual commitment but quality never is 2007 JavaOne SM Conference Session TS-2007 55

JSR-305: Annotations for Defect Detection Develop annotations that are useful for static analysis tools perhaps dynamic tools as well For example, which parameters and return values are allowed to be null Standard annotations interpreted by multiple tools Targets Java 5+ but combines with JSR-307: Annotations on Java Types 2007 JavaOne SM Conference Session TS-2007 56

For More Information Testing Java Code: Beyond the IDE today, 2:50pm BOF-9587 - Pimp My Java Application: Applying Static Analysis Tools to Boost Java Code Quality today, 7:55pm BOF-9231 - FindBugs BOF today, 8:55pm TS-5711 - Developing Reliable Products: Static and Dynamic Code Analysis tomorrow, 6:35pm On web: FindBugs, PMD, CheckStyle, JetBrains*, Klockwork*, Fortify Software*, Coverity*, Parasoft * - also in exhibit hall 2007 JavaOne SM Conference Session TS-2007 57

Q&A William Pugh Professor Univ. of Maryland http://www.cs.umd.edu/~pugh 2007 JavaOne SM Conference Session TS-2007 58

Additional Information 2007 JavaOne SM Conference Session TS-2007 59

Free Tools FindBugs Java Open Review: FindBugs + Fortify SCA free service for open source projects collaborative, distributed auditing PMD great capabilities for writing custom rules CheckStyle good for enforcing coding conventions 2007 JavaOne SM Conference Session TS-2007 60

Development Environments Eclipse has a fair number of checkers built in but they don't seem to eat their own dog food IntelliJ fairly smart set of checkers NetBeans Jackpot system: makes it easy to write checkers and quick fixes 2007 JavaOne SM Conference Session TS-2007 61

Commercial tools KlocWork K7 similar to FindBugs, lots of metrics and charts Fortify Software SCA focuses on security issues Coverity Prevent Java tool still top secret (at least, they won't talk to me) SureLogic Fluid focuses on concurrency, design, user annotations Parasoft JTest checks best practices 2007 JavaOne SM Conference Session TS-2007 62