Data-Driven Vulnerability Detection and Repair in Java Code

Ying Zhang, M D Rizwanul Kabir, Ya Xiao, Danfeng Yao, Na Meng, Meng, NaarXiv 2021 · DOI

Abstract

The Java platform provides various APIs to facilitate secure coding, yet correctly using these security APIs remains a significant challenge for developers without specialized cybersecurity training. Misuses of these APIs can introduce vulnerabilities, void security protections, and expose software to exploits. This paper presents SEADER, a novel approach that detects and repairs security API misuses. By comparing an insecure code snippet with its secure counterpart, SEADER conducts data dependence analysis to infer misuse templates and corresponding fixing operations. Using inter-procedural static analysis, SEADER searches for vulnerabilities in programs and proposes customized fixing suggestions. Evaluated against 25 code pairs and 10 open-source projects, SEADER achieved 100% precision and 91% accuracy. Furthermore, in a large-scale application to 100 Apache projects, SEADER detected 988 vulnerabilities and provided correctly customized repair suggestions, leading to 18 positive feedbacks from project developers.

The Challenge of Secure Coding

Modern software relies heavily on robust security libraries like the Java Cryptography Architecture (JCA). However, these libraries are notoriously difficult to implement correctly. Developers often introduce "API misuses"—such as hardcoded encryption keys, weak protocols, or insufficient salt lengths—that leave software vulnerable.

Existing solutions typically fall into two categories: rule-based tools that are difficult to maintain and update, or machine learning models that require massive amounts of labeled training data. Moreover, most current tools focus solely on detection, offering little help in repairing the identified vulnerabilities, especially when the fix requires complex, multi-line changes across different parts of the codebase.

Methodology: The SEADER Approach

SEADER (Security API misuse DEtection and Repair) addresses these gaps through a two-phase process that learns from "exemplars."

Overview of the SEADER architecture, showing Phase I (Pattern Inference) and Phase II (Pattern Application).

Phase I: Pattern Inference

Instead of requiring thousands of training examples, SEADER takes a single pair of code snippets: one insecure ($I$) and one secure ($S$). It utilizes Abstract Syntax Tree (AST) differencing to pinpoint changes and performs data dependency analysis to identify the "critical API" responsible for the vulnerability. This information is then abstracted into a general vulnerability template and an abstract fix stored in a JSON database.

Phase II: Pattern Application

To find vulnerabilities in target programs, SEADER employs the WALA static analysis framework to perform inter-procedural backward slicing. This allows the tool to trace variables across different methods and classes to determine if they originated from insecure sources. When a match is found, SEADER "concretizes" the abstract fix by mapping the program's actual variable names into the repair template.

Learning from Exemplars

The core innovation of SEADER is its ability to generate high-quality detection and repair patterns from a single well-crafted example. This bypasses the data hunger of deep learning approaches while maintaining the precision of rule-based systems.

An exemplar pair showing insecure code with a short salt and its secure counterpart with a longer salt.

As shown in the example above, SEADER can identify that a salt length of 4 bytes is insecure and suggest a fix to 8 bytes. Beyond simple constant changes, SEADER is capable of suggesting "nontrivial" fixes, such as inserting multiple lines of initialization code or replacing entire logic blocks.

Experimental Results

SEADER was evaluated using three primary datasets: an inference set of 25 code pairs, a ground-truth set of 32 known vulnerabilities in 10 Apache projects, and a large-scale set of 100 popular Apache projects.

  • High Precision: On the ground-truth set, SEADER achieved 100% precision and 84% recall. The high precision ensures that developers are not overwhelmed by false positives.
  • Large-Scale Impact: In the 100 Apache projects, SEADER detected 988 vulnerabilities. The authors filed 60 pull requests based on these findings; 18 projects have already provided positive feedback, with several merging the suggested fixes.
  • Competitive Performance: SEADER performed as well as or better than state-of-the-art detection tools like CryptoGuard while providing the unique added value of automated repair suggestions.

Conclusion and Future Work

SEADER demonstrates that data-driven, exemplar-based analysis is a highly effective way to manage security API misuses in Java. While the tool currently relies on WALA for static analysis—which can be limited by missing library dependencies—and requires experts to provide initial exemplar pairs, it represents a significant step toward automated software security. Future work will focus on automatically mining these exemplar pairs from version control histories to further reduce the need for manual expert input.

Summary updated

BibTeX
@misc{zhang2021data,
  author = {Ying Zhang and M D Rizwanul Kabir and Ya Xiao and Danfeng Yao and Na Meng and Meng, Na},
  title = {{Data-Driven Vulnerability Detection and Repair in Java Code}},
  year = {2021},
  eprint = {2102.06994},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2102.06994},
}
Figure