10 Easy Steps to Add Static Analysis to Your Firmware Build

Futuristic tunnel with overlays of charts and symbols

When it comes to embedded system applications, security is one of the most important requirements. In addition to embedded security topics such as Bluetooth encryption, SSH, and TLS, modern firmware developers use a static analysis tool that checks for vulnerabilities in your source code. There are many existing static analysis tools (Cppcheck, Clang Sanitizers, etc.) that can do the job, but today I’d like to focus on one of our favorites, Flawfinder.

Flawfinder is an open-source tool developed by David A. Wheeler, a security expert. Like all static analysis tools, this tool scans your C/C++ source codes to look for weaknesses or flaws.

Book a Call with Dojo Five Embedded Experts

Installation

Since Flawfinder is written in Python it can be used on most platforms. You can install Flawfinder by using the Python pip installer as shown below.

pip3 install flawfinder

To use Flawfinder, it is as easy as calling the following command.

# Scan a single file
flawfinder <SOURCE_CODE>

OR

# Scan all files in a directory
flawfinder <SOURCE_CODE_DIRECTORY>

Calling the command above prints the results on the terminal. and it is only useful at the moment you run it. If Flawfinder is integrated into a CI pipeline, the outputs will be saved for further review. To refer back to the previous Flawfinder outputs, all you have to do is to look at the pipeline’s build log.

There are command-line options that you can use to customize the scanning experience. The combination of options we recommend is minlevel, html, and context. The minlevel option lets you choose the minimum risk level from 0 (very low risk) to 5 (high risk) to be shown on the terminal. By default, the results of level 1 or higher are shown. If we are only concerned about high-risk outputs, we can set the minlevel to 4. The html and context options will put the output in HTML format and show the lines of code that are potential security vulnerabilities. With these options, the command will now look like the following.

flawfinder --minlevel=4 --html --context <SOURCE_CODE> > flawfinder_report.html

Add the Security Screening to Your Automated Build

If you’ve never heard of automated builds or are not sure how to get started using them, take a look at our blog on setting up automated firmware builds as a quick tutorial.

Then, consider this: In the airport, you can only get on the plane after passing through a number of stages, check-in, security, terminals, and then entering the plane itself. This is the same concept for your build – you have to take several steps before deployment. Therefore, we want to create a job for running Flawfinder (security screening) on your code before deploying it (enter the plane).

First, we want to create an isolated environment for this job. This can be done by creating a simple Flawfinder Docker image.

# A base image with Python is needed to install Flawfinder   
FROM python:3.8.5-slim-buster

ARG FLAWFINDER_VERSION=2.0.11

# Install a specific version Flawfinder
# I'm using version 2.0.11
RUN pip3 install flawfinder==$FLAWFINDER_VERSION

Once the Flawfinder Docker image is created, you can now use it in your automated pipelines. In the script section of your YAML file, call the Flawfinder command and provide the path to the source directory you want to check. A snippet of a Flawfinder job in a .gitllab-ci.yml file is shown below.

flawfinder:
	stage: static analysis
	image: flawfinder:v2.0.11
	script:
		- mkdir flawfinder_report
		- flawfinder --minlevel=4 --html --context src/ > flawfinder_report/flawfinder_test_report.html
	artifacts:
		paths:
			- flawfinder_report/

With the job above, it’d be like running bags through the scanner and no one looking at the screen to see what’s in them. We can set up the job to fail if the number of items (flaws) found is over a threshold, go check out our blog on how CI pipeline scripts and exit codes interact to learn more about it.

So, there you have it. Setting up a static analysis for your code can be as easy as a line of command. Having Flawfinder in your software development cycle not only makes you more confident with your  source code, but also protects your end product from potential criminals.

Next Steps

If you’d like to make sure your firmware is secure, reach out! At DojoFive, we have talented engineers on hand ready to help you with all aspects of your EmbedOps journey. We are always happy to help with interesting problems that need solving, from security audits to firmware development. 

You can book a call with us to start the conversation. We have tools and experience to help you along the way. 

Discover why Dojo Five EmbedOps is the embedded enterprise choice for build tool and test management.

Sign up to receive a free account to the EmbedOps platform and start building with confidence..

  • Connect a repo
  • Use Dev Containers with your Continuous Integration (CI) provider
  • Analyze memory usage
  • Integrate and visualize static analysis results
  • Perform Hardware-in-the-Loop (HIL) tests
  • Install the Command Line Interface for a developer-friendly experience

Subscribe to our Monthly Newsletter

Subscribe to our monthly newsletter for development insights delivered straight to your inbox.

Interested in learning more?

Best-in-class embedded firmware content, resources and best practices

Close up motherboard shot

How to Modify a File Using SED

Let’s take a look at a microchip project that was found to cause issues in the past. Upon trying to build the problematic example it was found that one of the Makefiles generated throughout the use of the Makefile generator command, “prjMakefilesGenerator“ contained invalid paths. Rather than modify the make file manually every time the Makefile generator command was called, it was decided that SED could be used to automate the process. The following commands are what were used to

Read More »
Dojo Five firmware engineer works at a computer on code

Three Tips For New Engineers

With a bachelor in Electrical Engineering and a few coding experiences, I started my job at Dojo Five. What’s waiting for me were demos and releases that surprised me with unexpected results, challenging problems that took hours to solve, and software, tools, and technologies that I never saw or heard of before. Here are some tips that shaped me into the firmware engineer I am today. Tip #1: Expect the unexpected After working for a couple of months, you will

Read More »
A finger moves toward the word update on a artistic depiction of technology

A Long Way From Home

Over The Air updates (OTA) takes advantage of modern connectivity for devices. Even for devices that aren’t normally connected to communication networks, it’s often possible to connect them to a computer or mobile device that is connected to the network and update them. It’s also become a common part of modern embedded development and release strategy.

Read More »