Fuzzying Java Code With the Help of JQF

Tobias Ospelt (Co-founder · Pentagrid)

Swiss Cyber Storm 2019 · Day 1 · Main

Overview

This article delves into the critical advancements in fuzzing for Java applications, a domain traditionally challenging due to Java's memory-safe language characteristics. Presented by Tobias Ospelt, co-founder of Pentagrid and known online as Floyd, this talk at Swiss Cyber Storm introduces JQF, a powerful framework that brings the intelligence of coverage-guided fuzzing to the Java ecosystem. The core message is clear: while Java mitigates memory corruption vulnerabilities, it remains highly susceptible to other critical bug classes, such as infinite loops, resource exhaustion, and logical flaws, which can lead to severe denial of service (DoS) attacks or unexpected application behavior.

Watch on YouTube

Visual summary for Fuzzying Java Code With the Help of JQF by Tobias Ospelt
Visual summary for Fuzzying Java Code With the Help of JQF by Tobias Ospelt

Key moments

  1. 2:00 What is fuzzing? Automated bug finding with AFL.
  2. 8:00 JQF AFL: Fuzzing Java Image I/O and finding uncaught exceptions.
  3. 10:40 "Hangs" reveal infinite loops and critical security vulnerabilities.
  4. 12:40 Introducing JQF Zest: Fuzzing any Java type in unit tests.
  5. 16:00 Zest discovers critical bug in Apache Commons Collections.
  6. 23:00 Bouncy Castle fuzzing finds OutOfMemory DoS vulnerability (CVE).
  7. 25:00 Future: finding SSRF and other security bugs in Java.
  8. 28:40 Practical advice: Integrating fuzzing into CI for continuous security.

Fuzzing Java Code With the Help of JQF

Speakers: Tobias Ospelt, Co-founder, Pentagrid

Conference: Swiss Cyber Storm

YouTube: https://www.youtube.com/watch?v=RaBGEgQiE-4

Overview

This article delves into the critical advancements in fuzzing for Java applications, a domain traditionally challenging due to Java's memory-safe language characteristics. Presented by Tobias Ospelt, co-founder of Pentagrid and known online as Floyd, this talk at Swiss Cyber Storm introduces JQF, a powerful framework that brings the intelligence of coverage-guided fuzzing to the Java ecosystem. The core message is clear: while Java mitigates memory corruption vulnerabilities, it remains highly susceptible to other critical bug classes, such as infinite loops, resource exhaustion, and logical flaws, which can lead to severe denial of service (DoS) attacks or unexpected application behavior.

Ospelt's presentation highlights how JQF, building on the principles of AFL (American Fuzzy Lop), enables developers and security researchers to automatically uncover these elusive bugs. It demonstrates the evolution from basic input stream fuzzing with JQF-AFL to the more sophisticated JQF-Zest, which integrates seamlessly into modern Java development workflows via a Maven plugin and leverages JUnit QuickCheck generators for arbitrary data types. The talk not only explains the methodology but also showcases real-world impact through vulnerabilities discovered in widely used libraries like Apache Commons Collections and Bouncy Castle, underscoring the necessity of adopting these advanced fuzzing techniques in Java development.

The significance of this work extends beyond academic interest; it provides actionable strategies for hardening Java software at scale. By integrating JQF into continuous integration (CI) pipelines, organizations can proactively identify and mitigate vulnerabilities that often escape traditional unit testing and manual code review. Ospelt's demonstration of finding a CVE-2019-17359 in Bouncy Castle within minutes powerfully illustrates the efficiency and efficacy of JQF, making a compelling case for its widespread adoption among Java developers and security professionals.

Background

▶ Watch: What is fuzzing? Automated bug finding with AFL. (2:00)

The concept of fuzzing revolves around feeding a program with large amounts of semi-random, malformed, or unexpected input in an automated fashion to uncover undesired behavior. Historically, fuzzing has been most effective for languages like C and C++, which are not memory-safe. In these contexts, fuzzers primarily seek out direct crashes, memory corruptions (such as buffer overflows or use-after-free errors), and segmentation faults. The discovery of such issues often indicates severe security vulnerabilities, potentially leading to arbitrary code execution.

A landmark in this field is AFL (American Fuzzy Lop), developed by Michal Zalewski. AFL revolutionized fuzzing by introducing coverage-guided fuzzing, a technique that uses instrumentation to monitor the execution of the target program. This instrumentation allows AFL to understand which code paths are taken by different inputs. Critically, AFL maintains a feedback loop: it prioritizes and mutates inputs that discover new code paths, effectively exploring the program's logic more deeply and efficiently than purely random fuzzing. AFL's user interface typically displays metrics like total paths discovered, unique crashes, and unique hangs, the latter indicating inputs that cause the program to become unresponsive or take an excessively long time to process. A classic example is fuzzing libPNG, a library for parsing PNG images, where AFL would mutate image files to trigger diverse parsing logic, ultimately seeking crashes or hangs.

However, applying this traditional fuzzing paradigm directly to Java presents a challenge. Java is a memory-safe language, meaning the Java Virtual Machine (JVM) actively prevents common memory errors. Developers generally don't encounter direct memory corruptions or segmentation faults. This raises a fundamental question: what constitutes a "crash" or "undesired behavior" in the context of Java fuzzing? Without the explicit memory safety issues of C/C++, the focus shifts. While uncaught exceptions like IndexOutOfBoundsException are certainly bugs, they typically don't bring down the entire JVM process in a security-critical way. The true security relevance in Java fuzzing, as Ospelt elaborates, lies in identifying issues like infinite loops and resource exhaustion, which can still lead to severe denial of service (DoS) conditions, even in a memory-safe environment. This shift in perspective is what JQF aims to address, adapting AFL's intelligence to the unique characteristics of Java.

Key Findings

▶ Watch: "Hangs" reveal infinite loops and critical security vulnerabilities. (10:40)

The talk's central contribution is the demonstration of JQF's effectiveness in uncovering critical vulnerabilities in Java code, despite its memory-safe nature. Initially, the adaptation of AFL's principles to Java through JQF-AFL allowed for fuzzing methods that accept InputStream objects. While early "crashes" often manifested as uncaught exceptions (e.g., IndexOutOfBoundsException), which are important for software robustness, they were not typically considered high-severity security issues. Ospelt illustrated this by changing the ImageIO fuzzing target from PNG to GIF, immediately revealing numerous uncaught exceptions, highlighting that even standard library components can harbor such bugs.

The more significant discovery with JQF-AFL, particularly from a security perspective, came from identifying "hangs." Unlike memory corruption crashes, hangs in Java often signify infinite loops or severe resource exhaustion within the target library. These conditions can lead to a denial of service (DoS), where an application becomes unresponsive or consumes all available CPU/memory resources, effectively rendering it unusable. Ospelt detailed his success in finding five distinct security vulnerabilities across various prominent Java libraries using this approach, including:

  • A crash in Apache POI (specifically, Apache Tika, which uses POI) due to an infinite loop.
  • A bug in JNR (Java Native Runtime).
  • A vulnerability in PDFBox for PDF parsing.
  • An infinite loop bug in Apache Commons Compress when parsing a specially crafted zip file.
  • A bug in the standard Java library's RIFFReader.

These findings underscore that even mature and widely used Java components are not immune to such issues.

Building upon JQF-AFL, the project introduced JQF-Zest, a more advanced and developer-friendly iteration. JQF-Zest significantly expands the scope of fuzzing beyond just InputStream parameters. Its key enhancements include:

  • Arbitrary Java Type Fuzzing: JQF-Zest can fuzz any Java method argument type, provided there's a JUnit QuickCheck generator for it. This means developers can fuzz complex objects, not just raw byte streams. Generators for common types like String, Integer, and Map are readily available.
  • assume Statement Awareness: JQF-Zest intelligently processes assume statements within JUnit tests. If a test has preconditions (e.g., assumeTrue(input.isValid())), the fuzzer will prioritize generating inputs that satisfy these conditions, leading to more focused and effective fuzzing.
  • assert Statement Violations: Beyond just detecting uncaught exceptions and hangs, JQF-Zest can be configured to report when a standard JUnit assert statement is violated. This allows developers to define explicit invariants or expected behaviors within their fuzz tests, enabling the discovery of subtle logical flaws that might not cause an exception or a hang.
  • Maven Plugin Integration: Perhaps the most impactful feature for widespread adoption, JQF-Zest is available as a Maven plugin. This enables seamless integration into existing Java build processes and continuous integration (CI) pipelines, making fuzzing a standard part of the development lifecycle without requiring complex tooling setups.

A striking example of JQF-Zest's power is the discovery of a critical bug in Apache Commons Collections, a fundamental library used across countless Java projects. Rohan Padhye, the creator of JQF, found a flaw in the PatriciaTrie data structure (a specialized tree-based map). The bug, marked as unresolved at the time of the talk, demonstrated that inserting a specific sequence of values (e.g., an element X followed by X combined with a zero-byte Unicode character) could cause X to silently disappear from the data structure. This violates the fundamental expectation of a map—that an inserted element remains present—and represents a severe logical flaw with potentially wide-ranging security implications, as data structures are ubiquitous in application logic.

Technical Deep Dive

▶ Watch: Zest discovers critical bug in Apache Commons Collections. (16:00)

The technical foundation of JQF lies in adapting the principles of coverage-guided fuzzing, famously pioneered by AFL, to the Java Virtual Machine (JVM). This involves a sophisticated blend of bytecode instrumentation, feedback-driven input generation, and intelligent test case reduction.

JQF-AFL: Stream-Based Fuzzing

Initially, JQF-AFL focused on fuzzing methods that consume java.io.InputStream objects. The workflow is straightforward:

  1. Test Setup: A developer annotates a JUnit test method with @Fuzz. This method typically takes an InputStream as an argument.

In this example, the ImageIO.read() method is fuzzed. The ImageInputStreamGenerator is a placeholder for a JQF-specific generator that would feed raw bytes to the InputStream.

  1. Instrumentation: JQF instruments the target Java code (either at compile time or load time) to monitor which code paths are executed. This is analogous to how AFL instruments C/C++ binaries.
  2. Input Generation & Mutation: JQF-AFL starts with a seed InputStream (e.g., a valid PNG file). It then applies various mutation strategies (bit flips, byte deletion, insertion, arithmetic operations on values, etc.) to generate new inputs.
  3. Feedback Loop: After each execution, JQF analyzes the code coverage achieved by the new input. If an input triggers new code paths, it's deemed "interesting" and added to the fuzzer's corpus, becoming a candidate for further mutation. This ensures the fuzzer intelligently explores the program's state space.
  4. Bug Detection: The fuzzer monitors for two primary types of failures:
  • Uncaught Exceptions: Any RuntimeException that escapes the fuzzed method. While not always security-critical, they indicate robustness issues.
  • Hangs: If an input causes the fuzzed method to run for an abnormally long time (exceeding a configured timeout), it's flagged as a hang. As discussed, these are often indicators of infinite loops or resource exhaustion, leading to DoS vulnerabilities.

JQF-Zest: Advanced Type-Agnostic Fuzzing

JQF-Zest represents a significant leap, moving beyond raw input streams to directly fuzz arbitrary Java types. This is achieved through:

  1. JUnit QuickCheck Generators: JQF-Zest integrates with JUnit QuickCheck, a property-based testing framework. For each argument of the @Fuzz annotated method, JQF-Zest requires a corresponding Generator.
  • For primitive types (int, String, boolean, etc.), built-in generators are available.
  • For complex types (Map, custom objects), developers can either use existing QuickCheck generators or write their own. These generators define how to construct valid (or semi-valid) instances of a given type.

The MapGenerator.class and StringGenerator.class would be JQF/QuickCheck implementations to create diverse maps and strings.

  1. assume Statements for Preconditions: JQF-Zest is designed to understand and leverage JUnit's assume statements (e.g., org.junit.Assume.assumeTrue(condition)). If a test method includes assumptions about its input (e.g., assumeTrue(map.containsKey(key))), JQF-Zest will intelligently guide its generators to produce inputs that satisfy these conditions. This is crucial for fuzzing methods that operate under specific preconditions, preventing the fuzzer from wasting time on inputs that would immediately fail the assumption.
  1. assert Statements for Logic Flaws: A powerful addition is JQF-Zest's ability to detect violations of standard JUnit assert statements (e.g., org.junit.Assert.assertTrue(condition)). This allows developers to embed specific properties or invariants they expect their code to uphold. When an input causes an assert statement to fail, JQF-Zest reports it as a "failure," indicating a logical bug. The PatriciaTrie bug in Apache Commons Collections is a prime example:

This example, simplified from the original discovery, illustrates how a specific insertion sequence (initialKey followed by initialKey concatenated with a null byte and secondKey) led to initialKey unexpectedly disappearing from the trie.

Integration and Performance

JQF-Zest's availability as a Maven plugin drastically simplifies its adoption. Developers can integrate fuzzing directly into their pom.xml files, making it a standard part of their build and CI processes. The fuzzer can be configured to run for a specific duration (e.g., 5 minutes per commit), providing continuous security validation. Ospelt noted impressive execution speeds, reaching up to 2,200 items per second in some cases, demonstrating its efficiency in exploring vast input spaces. This high throughput, combined with intelligent coverage guidance, makes JQF a formidable tool for uncovering deep-seated bugs in Java applications.

Demo / Proof of Concept

▶ Watch: Bouncy Castle fuzzing finds OutOfMemory DoS vulnerability (CVE). (23:00)

Tobias Ospelt provided a compelling live demonstration of JQF-Zest's capabilities by fuzzing the Bouncy Castle ASN.1 parser. Bouncy Castle is a widely used cryptographic library, critical to securing countless Java applications and even embedded in Android devices. ASN.1 (Abstract Syntax Notation One) is a standard for representing data structures, extensively used in cryptographic contexts such as TLS client certificates.

The setup for the demo was remarkably simple, showcasing JQF-Zest's ease of integration:

  1. Fuzz Test File: A single Java source file was created, containing a JUnit test method annotated with @Fuzz. This method accepted a byte[] array as input, which was then fed into Bouncy Castle's ASN1InputStream. The core logic involved attempting to read all objects from this stream. Crucially, IOExceptions were deliberately ignored, as they often indicate malformed input and are not the primary target of this fuzzing exercise.
  1. Maven Configuration: A pom.xml file was configured to include the JQF Maven plugin, the necessary JUnit QuickCheck generators, and the Bouncy Castle library dependency. This minimal setup allows Maven to manage all dependencies and execute the fuzzer.

Upon running the fuzzer via a simple Maven command (mvn jqf:fuzz), the results were quick and impactful. Within a little over one minute, JQF-Zest reported seven failures in the Bouncy Castle ASN.1 parser. These failures included various uncaught exceptions, such as ClassCastException, indicating type mismatches during parsing—bugs that Bouncy Castle subsequently fixed.

More significantly, one of the discovered failures was a java.lang.OutOfMemoryError (OOM). This critical vulnerability, later assigned CVE-2019-17359, demonstrated that a relatively short, specially crafted ASN.1 input could trigger the allocation of all available memory within the Java process, leading to a crash. Ospelt highlighted the severe security implications: if an attacker could send this malicious ASN.1 input, for example, as part of a client certificate during a TLS handshake, they could potentially crash a Java server using Bouncy Castle, resulting in a denial of service (DoS) attack.

The demo's success extended beyond just finding bugs. Ospelt reported that after presenting these findings and demonstrating the ease of fuzzing, the Bouncy Castle developers were impressed. They subsequently adopted JQF for their internal testing, expanding fuzzing efforts to other critical components, such as their GPG code. This real-world adoption underscores the practical value and immediate impact of JQF, transforming a security research tool into a staple of robust software development.

Defensive Implications

▶ Watch: Practical advice: Integrating fuzzing into CI for continuous security. (28:40)

The insights and tools presented by Tobias Ospelt offer several crucial defensive strategies for Java developers and security teams:

  1. Integrate Fuzzing into CI/CD Pipelines: This is arguably the most impactful recommendation. The availability of JQF-Zest as a Maven plugin drastically lowers the barrier to entry. Organizations should integrate fuzzing runs directly into their continuous integration (CI) and continuous delivery (CD) pipelines. Instead of running fuzzers for days, even short, targeted runs of 3-5 minutes per build or per method can quickly uncover new bugs, especially for recently changed code. This shifts fuzzing from a specialized, sporadic activity to a continuous, automated quality and security gate.
  1. Prioritize Fuzzing for Critical Components: Focus fuzzing efforts on code that handles untrusted input, parses complex data formats (e.g., ASN.1, XML, JSON, image formats), or implements security-sensitive logic (e.g., cryptographic operations, access control). These areas are historically rich hunting grounds for vulnerabilities.
  1. Write Effective Fuzz Tests: Treat fuzz tests as an extension of unit tests.
  • Focus on 'Hangs': For Java, "hangs" (infinite loops, excessive resource consumption) are often more critical than simple uncaught exceptions, as they directly lead to denial of service (DoS). Configure fuzzers to detect and report these aggressively.
  • Leverage assert Statements: Use assert statements within fuzz tests to define explicit invariants and expected behaviors. This allows JQF-Zest to find subtle logical flaws that don't necessarily throw exceptions but violate core assumptions of the code, as demonstrated by the PatriciaTrie bug.
  • Utilize assume Statements: Guide the fuzzer by specifying preconditions using assume statements. This ensures the fuzzer spends its time generating inputs that are relevant to the target logic, rather than inputs that would immediately be rejected.
  1. Be Wary of Resource Exhaustion: Java's memory safety doesn't protect against Out-of-Memory (OOM) errors or CPU-intensive infinite loops triggered by malicious input. These are prime DoS vectors. Developers should design their code with resource limits and graceful degradation in mind, and fuzzing is an excellent way to test these resilience mechanisms.
  1. Expand Beyond Memory Safety Concerns: The talk emphatically proves that Java, despite its memory safety, is not inherently secure against all classes of vulnerabilities. The focus must shift from traditional memory corruptions to logical flaws, resource exhaustion, and unexpected behavior.
  1. Consider Advanced Instrumentation (Future): While not yet fully integrated into JQF, Ospelt hinted at using the Java Security Policy Manager for advanced instrumentation. This could enable fuzzers to detect more complex security bugs like Server-Side Request Forgery (SSRF), where a library might attempt to open network sockets or access local files based on attacker-controlled input, even if it doesn't crash. This represents a frontier for Java fuzzing.

By embracing JQF and integrating these defensive practices, Java development teams can significantly enhance the security and robustness of their applications, moving towards a proactive and automated approach to vulnerability discovery.

Key Takeaways

  • Java is not immune to critical security bugs: Despite being a memory-safe language, Java applications are highly susceptible to vulnerabilities like infinite loops, resource exhaustion (OOM errors), and logical flaws, which can lead to severe denial of service (DoS) attacks.
  • JQF brings intelligent fuzzing to Java: The JQF framework, inspired by AFL, effectively adapts coverage-guided fuzzing techniques for Java, enabling automated discovery of these hard-to-find bugs.
  • "Hangs" are key indicators of DoS in Java: For Java fuzzing, detecting "hangs" (inputs causing excessive execution time) is often more critical than simple uncaught exceptions, as they frequently signal infinite loops or resource exhaustion.
  • JQF-Zest offers advanced, developer-friendly fuzzing: With support for arbitrary Java types via JUnit QuickCheck generators, intelligent handling of assume statements, and the ability to detect assert statement violations, JQF-Zest can uncover deep logical flaws beyond just crashes or hangs.
  • Seamless CI/CD integration is a game-changer: The Maven plugin for JQF-Zest makes it straightforward to integrate fuzzing into existing Java build processes and continuous integration (CI) pipelines, allowing for continuous security validation with minimal overhead.
  • Even widely used libraries harbor severe bugs: Real-world examples like the PatriciaTrie bug in Apache Commons Collections and CVE-2019-17359 in Bouncy Castle's ASN.1 parser demonstrate that even mature and critical Java libraries contain vulnerabilities discoverable by fuzzing.

About the Speaker(s)

Tobias Ospelt, also known by his online handle Floyd, is a prominent figure in the cybersecurity landscape. He is the co-founder of Pentagrid, a company specializing in security analysis and penetration testing. With over a decade of experience in the field, Ospelt has established himself as a long-term penetration tester in Switzerland. Beyond his professional work, he is actively involved in academia, lecturing information security at the Zurich University of Applied Sciences. Furthermore, Ospelt contributes to the cybersecurity community in Liechtenstein, participating with the Cybersecurity Liechtenstein Association to prepare young individuals for the European Cyber Security Challenge, often engaging them in Capture The Flag (CTF) competitions. His expertise spans practical security assessments and the development of innovative tools and methodologies for vulnerability discovery, particularly in the realm of Java fuzzing.

Reviews

Dr. Zero (Offensive Security Researcher) — STRONG ACCEPT

Ospelt takes coverage-guided fuzzing out of the C/C++ memory-corruption ghetto and makes it actually work for the JVM. He drops the obsession with segfaults and focuses on what actually kills Java apps: resource exhaustion and logical invariant violations. This is the exact kind of applied defensive work that developers need yesterday.

Heather Calloway (CISO) — STRONG ACCEPT

Java's memory safety breeds complacency, but this talk proves how easily resource exhaustion bugs can cause catastrophic DoS in core libraries like Bouncy Castle. JQF makes coverage-guided fuzzing trivial to drop into a Java CI pipeline via Maven, which is exactly the kind of automated, low-friction AppSec tooling leadership should mandate.

→ Top-rated talks at Swiss Cyber Storm 2019

All talks from Swiss Cyber Storm 2019