Source Code Scanners for Better Code
Coding is tough enough, and coding right can sometimes seem an almost impossible task. Between design constraints, deadlines and making it work in the first place, it's difficult to get your code secure. Security concerns, however, can be helped by scanners.
Source code scanners are nothing new. Tools like lint have been around for many, many years to help you find errors in your C code. There are even lint checkers for DNS and HTML files, nslint and weblint, respectively. The lint tool doesn't explicitly talk about insecure code, though, only basic inconsistencies in your style.
Source code audits are also great things, things like the Linux Auditing Project. However, audits can take a lot of time and money if you outsource them, though the payback is well worth the investment. Source code scanners used available for only commercial software, prohibiting their use by many Linux developers and researchers, who didn't always place user friendliness high on their lists of goals.
A couple of years ago, pscan was released under the GPL. It's a simple format string scanner but one of the first such tools commonly available on the hobbyist developer market. Since then, three new source code scanners have become available for source available software, and all three are worth looking at. Not all of them are open source, but they do work well for most users coding in C or C++.
The resources listed at the end of this article are worth checking out. The Open Source Quality Project has links to several sites, including commercial products and academic research groups, involved in performing software quality testing. Some of these tools can do more rigorous testing but can be far less user-friendly.
Developed by the noted author and coder David Wheeler, Flawfinder is a Python program that can be used to assist auditing C and C++ code. It's in the early stages, currently at version 0.19, but already it's strong competition for the other tools listed here in the 1.x stages. Flawfinder is also quite fast, covering thousands of lines of C code on a typical desktop machine in a matter of seconds. Flawfinder is released under GPL version 2, meaning it is free software.
Flawfinder also shows some intelligence when it comes to scanning for vulnerabilities. For example, in tests using intentionally insecure code, Flawfinder was able to distinguish between strcpy() from a constant sized string and variable length strings and tell the difference between vulnerabilities and false hits. Furthermore, Flawfinder understands the gettext libraries and their use in internationalization.
RATS, the Rough Auditing Tool for Security, is a source code scanner under active development that is capable of scanning C, C++, Perl, PHP and Python source code. Also released under the GPL, some of the RATS developers also worked on a similar tool, ITS4 (discussed below). The current version, 1.3, is noticeably more stable than version 1.2, but it still can fail on large amounts of input.
RATS is configurable when the source code is modified (through lexical analysis), with error messages controlled by XML reporting filters, which requires the XML tool expat to also be installed. At runtime, you can configure the level of output you wish to see (defaulting to medium), alternative vulnerability databases and even report functions that accept input from the user, facilitating the tracking of user supplied data.
Some of the specific limitations of RATS include the use of greedy pattern matchings, meaning that tracking for "printf" will match not only "print()" calls but also "vsnprintf()" and the like. This can make it difficult to filter hits from noise when specific functions are being sought with the -a flag.
The authors of RATS and Flawfinder, by the way, plan to coordinate their development efforts to produce a high quality, open-source development tool. This should be good to watch, as each development team is well respected in the field.
One of the original source available scanners for Linux and UN*X, It's the Software Stupid Source Scanner (ITS4), scans C and C++ source for common security-related flaws. Developed by Cigital (then known as RST Technologies) and some of the same people who went on to develop RATS, ITS4 has set the pace. Many of the niceties in ITS4 are also available in RATS or Flawfinder, such as ignorable lines, tracking user input (the -m flag) and alternative databases.
Modification of a particular project or site is relatively straightforward. A simple text file of vulnerabilities is used, which can be added to or modified to suit any specifics of a system. It should be noted that ITS4 comes with a non-free license, even though the source is available. Users should read and understand the license if they are doing anything beyond hobbyist programming with it.