SeqTrans: Automatic Vulnerability Fix Via Sequence to Sequence Learning

Jianlei Chi, Yu Qu, Ting Liu, Qinghua Zheng, Heng YinIEEE Trans. Software Eng 2020 · DOI

TL;DR

This paper introduces SeqTrans, a novel approach that leverages Neural Machine Translation (NMT) and historical vulnerability fixes to automatically suggest and apply code patches. By using a Transformer model and fine-tuning on vulnerability data, SeqTrans achieves promising accuracy in fixing software vulnerabilities, outperforming existing methods.

What Problem Does This Paper Address?

The increasing prevalence of software vulnerabilities, fueled by automated detection tools, presents a significant challenge for developers. Manually fixing these vulnerabilities is time-consuming, error-prone, and requires deep understanding of the code and potential exploits. While automated program repair (APR) techniques exist, they often struggle with the scarcity of vulnerability data and the complexity of capturing contextual information around vulnerabilities. Existing APR methods frequently rely on domain-specific knowledge or predefined repair templates, limiting their scalability and effectiveness for the unique nature of vulnerability fixes.

Methods

SeqTrans employs a sequence-to-sequence learning approach, specifically using the state-of-the-art Transformer model. The core idea is to treat code vulnerability fixing as a translation problem, where the input is the vulnerable code and the output is the fixed code.

Key methodological contributions include:

  • Data-flow Dependencies (Def-Use Chains): To capture crucial context around vulnerable code, SeqTrans constructs def-use chains. This representation highlights variable definitions and uses, providing a more focused and relevant context than simply considering entire functions or classes.
  • Transformer Model: The Transformer architecture is chosen for its ability to handle long sequences and capture global dependencies through its self-attention mechanism, mitigating the performance degradation issues seen in traditional RNN/LSTM models with long code segments.
  • Fine-tuning Strategy: To address the challenge of limited vulnerability data, SeqTrans utilizes a fine-tuning approach. A model is first pre-trained on a large dataset of general bug fixes, learning universal code repair patterns. This pre-trained model is then fine-tuned on a smaller dataset of vulnerability fixes, adapting it to the specific task.
  • Normalization and Tokenization: To manage vocabulary size and improve consistency, variable names, literals, and strings are replaced with generic tokens (e.g., "var1", "num1").
  • Beam Search and Syntax Checking: To provide multiple repair suggestions and ensure code quality, beam search is used to generate candidate patches, followed by syntax checking (using FindBugs) to filter out invalid or erroneous fixes.

Datasets

  • Tufano's Dataset: A large dataset of raw source code pairs extracted from bug-fixing commits, used for pre-training the model.
  • Ponta's Dataset: A dataset containing 624 publicly disclosed vulnerabilities from 205 open-source Java projects, mapped to 1282 commits that fix them. This dataset is used for fine-tuning and evaluating the model's performance on vulnerability fixing.

Key Insights and Ideas

  • Fine-tuning is Crucial for Small Datasets: The significant performance improvement observed when fine-tuning a model pre-trained on general bug fixes onto a smaller vulnerability dataset highlights the effectiveness of this strategy for data-scarce domains.
  • Def-Use Chains Capture Essential Context: Leveraging data-flow dependencies provides a more targeted and effective context for vulnerability fixing compared to broader code contexts.
  • Transformer Architecture Handles Code Complexity: The Transformer's ability to manage long sequences and capture dependencies is well-suited for the structured nature of code.
  • Generalization Across Vulnerability Types is Challenging: While SeqTrans performs well on certain types of vulnerabilities (e.g., CWE-287, CWE-863), it struggles with others (e.g., CWE-918), suggesting that a universal vulnerability fixer is ambitious. Focusing on specific CWEs might yield more promising results.
  • Code Transformation Sophistication Matters: Simple and repetitive code transformations (like changing strings or throw exceptions) are learned more effectively than complex, interconnected changes (like refactoring or structural modifications).

Key Results

  • Statement-Level Accuracy: SeqTrans achieves 23.3% accuracy at the statement level.
  • CVE-Level Accuracy: SeqTrans achieves 25.3% accuracy at the CVE level.
  • Outperformance: SeqTrans outperforms state-of-the-art techniques like SequenceR and Tufano et al.'s approach.
  • Effectiveness of Fine-tuning: The fine-tuning strategy significantly improves performance, with SeqTrans achieving 23.3% accuracy when fine-tuned, compared to 22.1% when trained only on the specific domain dataset.
  • Specific CWE Performance: The model shows strong performance on certain CWEs, such as CWE-287 (Improper Authentication) and CWE-863 (Incorrect Authorization).
  • Traditional Evaluation: In a traditional evaluation setting, SeqTrans generated correct patches for 21 out of 120 vulnerabilities.

Limitations and Future Work

  • Single-Line Prediction: Currently, SeqTrans primarily focuses on single-line predictions and assumes independence between vulnerable statements. Future work aims to abstract and tokenize at the function level to handle more complex scenarios.
  • Data Abstraction Limitations: The current abstraction mechanism might lose some context, making it difficult to infer repairs for certain types of changes (e.g., when a println function's output needs to change).
  • GumTree Mismatches: Potential inaccuracies in GumTree's AST differencing could affect the quality of the training data.
  • Fault Localization Integration: The paper assumes perfect vulnerability localization and suggests integrating fault localization techniques as a future direction.
  • Generalization to Other Languages: While the current work focuses on Java, extending the approach to other programming languages is a promising avenue.
  • Fine-grained Code Refactoring: The approach could be applied to other code transformation tasks beyond vulnerability fixing, such as fine-grained code refactoring.

Summary updated

BibTeX
@article{chi2020seqtrans,
  author = {Jianlei Chi and Yu Qu and Ting Liu and Qinghua Zheng and Heng Yin},
  title = {{SeqTrans: Automatic Vulnerability Fix Via Sequence to Sequence Learning}},
  journal = {IEEE Trans. Software Eng.},
  year = {2020},
  doi = {10.1109/tse.2022.3156637},
}
Figure