Uncovering Vulnerabilities In Cryptographic Libraries: Mayhem, MatrixSSL, And WolfSSL (CVE-2019-13470)

Tyler Nighswander
May 19, 2020
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Introduction

As part of a recent initiative at ForAllSecure to analyze more open source software with Mayhem, a next-generation fuzzing solution, we decided to investigate some cryptographic libraries.

Why Crypto Libraries?

Why look at crypto libraries? First there is the obvious: SSL, TLS, and related protocols managed by these libraries power much of the secure web. Every day these sorts of software are relied upon to secure everything from usernames and passwords to emails to banking information.

Although there have been some cryptographic issues in these protocols, the majority of vulnerabilities in codebases like this end up being more “standard” coding errors. It is easy to focus on the pure math and cryptographic aspect of secure communications, but establishing secure connections requires a lot of work parsing certificates and messages and is easy to mess up.

Despite some excellent recent efforts to write cryptographic libraries in memory safe languages, demands for performance, scalability, and portability have led many people to develop in C, which is a notoriously difficult language to use for secure software development. Further complicating matters, even small bugs that might not be security sensitive in some applications can be disastrous for cryptographic scenarios.

For all these reasons, cryptographic libraries are popular targets for security analysis: whether that be by fuzzing, static analysis, or even manual audits. Despite the large amount of review these codebases have, Mayhem has still been able to identify issues in the past. For example, Mayhem discovered CVE-2016-7053 in OpenSSL, simply by using symbolic execution on the existing harness used by the maintainers and by OSS-fuzz.

Fuzzing MatrixSSL

MatrixSSL (now known as Inside Secure TLS Toolkit) is an open-source library aimed at IoT and lightweight scenarios with minimal dependencies for portability. As is the case with many of these libraries, much of the code could be considered on the attack surface. We chose to look at parsing x509 certificates. Because this often is done with untrusted parties on the web when first establishing keys, it’s very important from a security standpoint. Moreover, it is incredibly difficult to do correctly. The standard encoding for these certificates is ASN.1. Despite its use in many places, ASN.1 is notoriously difficult to implement without bugs. Put together its security sensitive nature as a vital part of key exchange, and its error-prone nature, and you find a great target for security analysis.

#include "x509.h"
#include <stdlib.h>
#include <stdio.h>

int LLVMFuzzerTestOneInput(const unsigned char *data, size_t size) {
void *outcert;psX509ParseCert(NULL, data, size, &outcert, 0);
psX509FreeCert(outcert);
}

#ifndef LIBFUZZER
void main(int argc, char **argv) {
FILE *f = fopen(argv[1], "r");
fseek(f, 0, SEEK_END);
int len = ftell(f);
char *buf = malloc(len);
fseek(f, 0, SEEK_SET);
fread(buf, len, 1, f);
fclose(f);
LLVMFuzzerTestOneInput(buf, len);
free(buf);
}
#endif


Because MatrixSSL is open-source, we are able to put together a simple fuzzing harness without much effort to test just decoding certificates. In fact, it was just two lines of code we had to write that were specific to the project, and then a small shim we reuse across projects. Just like we recommend to our customers when source code is easy to access and modify, we build both a version compiled with LibFuzzer and ASAN and one without. This makes it easy for Mayhem to both fuzz and run symbolic execution over the program.

Development Speed or Code Security. Why Not Both?

Find out how ForAllSecure can bring advanced fuzz testing into your development pipelines.

Request Demo Learn More

Shortly after running, we found some issues with bounds checking of ASN.1 decoding values, which we immediately disclosed to the maintainers. Because Mayhem uses dynamic analysis rather than static analysis, we already had a test case ready to go to demonstrate the issue, and the maintainers were able to quickly fix the potential issue.

Fuzzing WolfSSL

Seeing these sorts of issues in one library, we decided to look at another similar project. WolfSSL is a relatively light-weight cryptographic library that supports a wide variety of platforms, including embedded devices available as open source software or commercially. With an almost identical approach, we were able to fuzz WolfSSL as well. Again, just a few lines of code needed to be written for the different codebase.

#include "x509.h"
#include <stdlib.h>
#include <stdio.h>

int LLVMFuzzerTestOneInput(const unsigned char *data, size_t size) {
WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
wolfSSL_CTX_use_certificate_buffer(ctx, data, size, WOLFSSL_FILETYPE_ASN1);
if (ctx != NULL) wolfSSL_CTX_free(ctx);
}

 

And just as before, we were able to find a similar issue with ASN.1 parsing with an easily reproducible test case we could give to the maintainers, who quickly fixed it.

Why Fuzz?

There are several takeaways from this quick journey analyzing some cryptographic libraries, which apply to software built by anyone for any purpose.

  • Developers should fuzz their code! It’s a great way to identify bugs early, before it makes it to the hands of customers. 
  • You don’t need to write a lot of code to add dynamic analysis harnesses to a piece of software. As we saw, it was only 2-3 lines of actual code to write a harness here. Although there was a bit that needed to be done to properly build and link the project together, this would be very easy to do for the maintainers of the code.
  • Reporting and fixing bugs found with dynamic analysis is easy. With a concrete input that demonstrates the problem, developers and quickly reproduce the issue with their debugging tools of choice. Once a proposed fix is in place, it’s easy to check that it worked by re-running the example and making sure it behaves correctly.

The developers both of WolfSSL and MatrixSSL were great and quickly fixed what we reported. Hopefully in the future we’ll have some chances to do more experiments like this to help analyze and secure open source software.

Share this post

How about some Mayhem in your inbox?

Subscribe to our monthly newsletter for expert insights and news on DevSecOps topics, plus Mayhem tips and tutorials.

By subscribing, you're agreeing to our website terms and privacy policy.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Add Mayhem to Your DevSecOps for Free.

Get a full-featured 30 day free trial.

Complete API Security in 5 Minutes

Get started with Mayhem today for fast, comprehensive, API security. 

Get Mayhem

Maximize Code Coverage in Minutes

Mayhem is an award-winning AI that autonomously finds new exploitable bugs and improves your test suites.

Get Mayhem