Common Errors in C/C++ Code and Static Analysis



Similar documents
Fully Automated Static Analysis of Fedora Packages

Applying Clang Static Analyzer to Linux Kernel

8.5. <summary> Cppcheck addons Using Cppcheck addons Where to find some Cppcheck addons

Leak Check Version 2.1 for Linux TM

X05. An Overview of Source Code Scanning Tools. Loulwa Salem. Las Vegas, NV. IBM Corporation IBM System p, AIX 5L & Linux Technical University

Source Code Review Using Static Analysis Tools

CSC230 Getting Starting in C. Tyler Bletsch

Secure Programming with Static Analysis. Jacob West

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

How I Learned to Stop Fuzzing and Find More Bugs

Detecting Critical Defects on the Developer s Desktop

SQLITE C/C++ TUTORIAL

How to Write a Checker in 24 Hours

Software security assessment based on static analysis

Virtuozzo Virtualization SDK

Chapter 15 Operating System Security

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

Using Eclipse CDT/PTP for Static Analysis

D. Best Practices D.1. Assurance The 5 th A

Using the Intel Inspector XE

Real World Software Assurance Test Suite: STONESOUP

Transparent Monitoring of a Process Self in a Virtual Environment

Debugging with TotalView

Libmonitor: A Tool for First-Party Monitoring

Oracle Solaris Studio Code Analyzer

Valgrind BoF Ideas, new features and directions

MPI-Checker Static Analysis for MPI

Security Vulnerability Management. Mark J Cox

Secure Software Programming and Vulnerability Analysis

Parallel Computing. Shared memory parallel programming with OpenMP

COSC 6397 Big Data Analytics. Distributed File Systems (II) Edgar Gabriel Spring HDFS Basics

Idea: Measuring the Effect of Code Complexity on Static Analysis Results

Introduction to Static Analysis for Assurance

Bypassing Memory Protections: The Future of Exploitation

Handling POSIX attributes for trusted Active Directory users and groups in FreeIPA

Linux Kernel. Security Report

Static Analysis. Find the Bug! : Analysis of Software Artifacts. Jonathan Aldrich. disable interrupts. ERROR: returning with interrupts disabled

An Incomplete C++ Primer. University of Wyoming MA 5310

CSCE 665: Lab Basics. Virtual Machine. Guofei Gu

Kernel Intrusion Detection System

LLVMLinux: Embracing the Dragon

Informatica e Sistemi in Tempo Reale

Using the Juliet Test Suite to compare Static Security Scanners

Improve Fortran Code Quality with Static Analysis

TOOL EVALUATION REPORT: FORTIFY

Introduction to SPIN. Acknowledgments. Parts of the slides are based on an earlier lecture by Radu Iosif, Verimag. Ralf Huuck. Features PROMELA/SPIN

Iron Chef: John Henry Challenge

RED HAT DEVELOPER TOOLSET Build, Run, & Analyze Applications On Multiple Versions of Red Hat Enterprise Linux

The Click2NetFPGA Toolchain. Teemu Rinta-aho, Mika Karlstedt, Madhav P. Desai USENIX ATC 12, Boston, MA, 13 th of June, 2012

Operating System Structure

/* File: blkcopy.c. size_t n

SHARED HASH TABLES IN PARALLEL MODEL CHECKING

Software Vulnerabilities

Manual vs. Automated Vulnerability Assessment: ACaseStudy

Fortify on Demand Security Review. Fortify Open Review

Binary compatibility for library developers. Thiago Macieira, Qt Core Maintainer LinuxCon North America, New Orleans, Sept. 2013

RISC-V Software Ecosystem. Andrew Waterman UC Berkeley

Security Testing OpenGGSN: a Case Study

CSI 402 Lecture 13 (Unix Process Related System Calls) 13 1 / 17

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

System Security Fundamentals

Static Code Analysis Procedures in the Development Cycle

Squashing the Bugs: Tools for Building Better Software

Chapter 8 Software Testing

Illustration 1: Diagram of program function and data flow

Machine vs. Machine: Lessons from the First Year of Cyber Grand Challenge. Approved for Public Release, Distribution Unlimited

SSVChecker: Unifying Static Security Vulnerability Detection Tools in an Eclipse Plug-In

Get the Better of Memory Leaks with Valgrind Whitepaper

CSE543 - Introduction to Computer and Network Security. Module: Operating System Security

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Lecture 17: Testing Strategies" Developer Testing"

Unix Security Technologies. Pete Markowsky <peterm[at] ccs.neu.edu>

Variable Base Interface

Format string exploitation on windows Using Immunity Debugger / Python. By Abysssec Inc

Continuous Integration/Testing and why you should assume every change breaks your code

CS 6371: Advanced Programming Languages

Partitioning under the hood in MySQL 5.5

PHP in RPM distribution

Jonathan Worthington Scarborough Linux User Group

static void insecure (localhost *unix)

Detecting Software Vulnerabilities Static Taint Analysis

Application Security Testing How to find software vulnerabilities before you ship or procure code

OF 1.3 Testing and Challenges

RTI Routing Service. Release Notes

Towards OpenMP Support in LLVM

CPSC 491. Today: Source code control. Source Code (Version) Control. Exercise: g., no git, subversion, cvs, etc.)

Open-source Versus Commercial Software: A Quantitative Comparison

Source Code Security Analysis Tool Functional Specification Version 1.0

OpenSM Log Management

Building accurate intrusion detection systems. Diego Zamboni Global Security Analysis Lab IBM Zürich Research Laboratory

WEB BASED Access Control/Time Attendance Software Manual V 1.0

Hands-on Hacking Unlimited

Actualtests.C questions

General Introduction

Transcription:

Common Errors in C/C++ Code and Static Analysis Red Hat Ondřej Vašík and Kamil Dudka 2011-02-17 Abstract Overview of common programming mistakes in the C/C++ code, and comparison of a few available static analysis tools that can detect them.

Agenda 1 Static Analysis What Does It Mean? 2 Common Code Weaknesses in C/C++ Programs 3 Available Tools for Static Analysis 4 Beyond Static Analysis

Static Analysis What Does It Mean? Static Analysis generic definition: analysis of code without executing it various kinds of tools generic + specialized already done by the compiler (optimization, warnings,... ) we are interested in using static analysis to find bugs

Static Analysis What Does It Mean? Static Analysis Finding Bugs usually requires code that we are able to compile usually fast (time of analysis close to time of compilation) high level of automation can t cover all bugs in code problem with false positives any code change = risk of regressions

Static Analysis What Does It Mean? Static Analysis Techniques error patterns missing break, stray semicolon,... enhanced type checking may use attributes, such as attribute ((address space(num))) in sparse data-flow analysis solving of data flow equations, usually works at the CFG level abstract interpretation evaluates a program for all possible inputs at once over an abstract domain

Agenda 1 Static Analysis What Does It Mean? 2 Common Code Weaknesses in C/C++ Programs 3 Available Tools for Static Analysis 4 Beyond Static Analysis

Common Code Weaknesses in C/C++ Programs Common Code Weaknesses in C/C++ Programs CWE from MITRE (Common Weakness Enumeration) (http://cwe.mitre.org/data/definitions/398.html) static analysis is especially good for: boundary checks resource leak checks memory safety checks dead code checks uninitialized/unused variables checks race conditions / synchronization checks various coded by humans issues

Common Code Weaknesses in C/C++ Programs Boundary Problems static/dynamic buffer overflows/underflows (CWE-125, CWE-120, CWE-170, CWE-124) types incompatibilities (signed/unsigned) (CWE-194) signedness overflow issue that caused segfault with multivolumes in star: diff -urnp star-1.5.1-orig/star/buffer.c star-1.5.1/star/buffer.c --- star-1.5.1-orig/star/buffer.c +++ star-1.5.1/star/buffer.c @@ -799,7 +799,7 @@ initbuf(nblocks) bigptr = bigbuf = malloc((size t) bufsize+10+pagesize, "buffer"); - bigptr = bigbuf = (char *)roundup((intptr t)bigptr, pagesize); + bigptr = bigbuf = (char *)roundup((uintptr t)bigptr, pagesize); fillbytes(bigbuf, bufsize, \0 ); fillbytes(&bigbuf[bufsize], 10, U );

Common Code Weaknesses in C/C++ Programs Resource Leaks memory leaks (CWE-404) descriptor leaks (CWE-404) recent util-linux resource leak fix in libmount/src/utils.c @@ -427,6 +427,7 @@ static int get filesystems(const char *filename, char ***filesystems, const char *pattern) { + int rc = 0; FILE *f; char line[128]; @@ -436,7 +437,6 @@ while (fgets(line, sizeof(line), f)) { char name[sizeof(line)]; - int rc; if (*line == # strncmp(line, "nodev", 5) == 0) continue; @@ -446,9 +446,11 @@ rc = add filesystem(filesystems, name); if (rc) - return rc; + break; } - return 0; + fclose(f); + return rc; }

Common Code Weaknesses in C/C++ Programs Memory Safety dereference null (CWE-476), use after free (CWE-416) double free (CWE-415), bad free (CWE-590) dual doublefree due to missing exit in policycoreutils(sepolgen-ifgen-attr-helper.c): @@ -212,6 +213,7 @@ int main(int argc, char **argv) /* Open the output policy. */ fp = fopen(argv[2], "w"); if (fp == NULL) { fprintf(stderr, "error opening output file\n"); policydb destroy(p); free(p); + return -1; }... policydb destroy(p); free(p); fclose(fp);

Common Code Weaknesses in C/C++ Programs Dead Code Checking unnecessary code (CWE-561) wrong error check (CWE-252, CWE-665, CWE-569) Uninitialized/Unused Variables unnecessary variable handling (CWE-563) using unitialized variable (CWE-457, CWE-456)

Common Code Weaknesses in C/C++ Programs Race Conditions / Synchronization Checks TOCTOU (time of check / time of use) (CWE-367) unsynchronized access to shared data in multithread apps (CWE-362) issues with locks (CWE-362) potential deadlock in kernel on fail path ( https://lkml.org/lkml/2010/9/4/73 ) diff --git a/drivers/net/bna/bnad.c b/drivers/net/bna/bnad.c @@ -2706,7 +2706,7 @@ bnad set rx mode(struct net device *netdev) kzalloc((mc count + 1) * ETH ALEN, GFP ATOMIC); if (!mcaddr list) - return; + goto unlock; memcpy(&mcaddr list[0], &bnad bcast addr[0], ETH ALEN); @@ -2719,6 +2719,7 @@ bnad set rx mode(struct net device *netdev) /* Should we enable BNAD CF ALLMULTI for err!= 0? */ kfree(mcaddr list); } +unlock: spin unlock irqrestore(&bnad->bna lock, flags); }

Common Code Weaknesses in C/C++ Programs Various coded by humans Issues various cut&paste issues, missing breaks (CWE-484) priority of operators issues(cwe-569) stray semicolon after if (CWE-398) missing asterisks in pointer operations (CWE-476) http://github.com/bagder/curl/compare/62ef465...7aea2d5 diff --git a/lib/rtsp.c b/lib/rtsp.c --- a/lib/rtsp.c +++ b/lib/rtsp.c @@ -709,7 +709,7 @@ while(*start && ISSPACE(*start)) start++; - if(!start) { + if(!*start) { failf(data, "Got a blank Session ID"); } else if(data->set.str[string RTSP SESSION ID]) {

Common Code Weaknesses in C/C++ Programs Defensive Programming not really static analysis technique, but good habbit use compiler protection mechanisms D FORTIFY SOURCE=2 stack-protector, PIE/PIC, RELRO, ExecShield don t ignore warnings (-Wall -Wextra) never trust anyone, never expect anything memory boundaries check return codes/error codes use descriptors respect uid/gids, don t over escalate privileges http://www.akkadia.org/drepper/defprogramming.pdf

Agenda 1 Static Analysis What Does It Mean? 2 Common Code Weaknesses in C/C++ Programs 3 Available Tools for Static Analysis 4 Beyond Static Analysis

Available Tools for Static Analysis Available Tools for Static Analysis GCC compile cleanly at high warning levels GCC plug-ins suitable for projects natively compiled by GCC Clang Static Analyzer uses LLVM Compiler Infrastructure sparse developed and used by kernel maintainers (C only) cppcheck easy to use, low rate of false positives commercial tools for static analysis (Coverity,... ) research tools for static analysis (frama-c,... ) some tools are not yet ready for industrial software (uno, splint,... )

Available Tools for Static Analysis GCC Plug-ins gcc source file preprocessor parser optimizer binary code generator object file plug-in as easy to use as adding a flag to CFLAGS no parsing errors, no unrecognized compilation flags one intermediate code used for both analysis and building repoquery --repoid=rawhide-source --arch=src --whatrequires gcc-plugin-devel gcc-python-plugin allows to write GCC plug-ins in python DragonEgg allows to use LLVM as a GCC backend

Available Tools for Static Analysis Clang Static Analyzer based on LLVM Compiler Infrastructure code needs to compile with LLVM for an autoconf-based project, you can hook clang this way: 1 scan-build./configure... 2 scan-build make 3 scan-view... the steps above may fail on projects using some obscure build systems (e.g. ksh is known to cause problems)

Available Tools for Static Analysis sparse supports only C, not fully compatible with GCC able to analyze the whole Linux kernel provides a GCC wrapper called cgcc (make CC=cgcc) provides a library to build custom analyzers sparse-llvm an LLVM front-end (still under development)

Available Tools for Static Analysis cppcheck uses its own parser and preprocessor reports only erorrs by default can be run directly on the sources (not always optimal) using the options -D and -I may help significantly --template gcc makes the output compatible with GCC -jn allows to run cppcheck in parallel [TODO: demo libedit]

Available Tools for Static Analysis Coverity enterprise tool, not freely available often used to analyze free software http://scan.coverity.com combination of all above mentioned static analysis techniques modular, various checkers advanced statistical methods for elimination of false positives

Available Tools for Static Analysis Coverity How Do We Use It in Red Hat? scans of minor RHEL updates = prevent new defects introduced by backports and new features scans selected package set from Fedora Rawhide = packages with potential for next RHEL, working with upstream, to improve overall source code quality 1500+ packages, 150M LoC, 170k pot. defects, 90% scan success rate util-linux ( 70 bugs), ksh ( 50 bugs), e2fsprogs ( 40 bugs) and many other cleanups upstream based on scans scans of upstream projects developed by Red Hat = keeping upstream code quality at high level

Agenda 1 Static Analysis What Does It Mean? 2 Common Code Weaknesses in C/C++ Programs 3 Available Tools for Static Analysis 4 Beyond Static Analysis

Beyond Static Analysis Beyond Static Analysis static analysis is a good bug-hunting technique, but what about false negatives? some properties are hard to check using static analysis only absence of memory leaks error label reachability program termination software verification methods can guarantee zero false negatives for checking the properties above http://sv-comp.sosy-lab.org/results/index.php

Beyond Static Analysis Conclusion there is a lot of ready to use static analysis tools out there it is important not to rely on a single static analysis tool many of them are open source projects currently, the key problem of static analysis tools are false positives, which need to be filtered out manually