DeepCode AI Fix: Fixing Security Vulnerabilities with Large Language Models

Berkay Berabi, Alexey Gronskiy, Veselin Raychev, Gishor Sivanrupan, Victor Chibotaru, Martin T. VechevarXiv 2024

Abstract

The automated program repair field has attracted substantial interest over the years, but despite significant research efforts, creating a system that works well for complex semantic bugs such as security vulnerabilities has proven difficult. A promising direction to solve this challenge is by leveraging large language models (LLMs), which are increasingly used to solve various programming tasks. In this paper, we investigate the effectiveness of LLMs for solving code-repair tasks. We show that the task is difficult as it requires the model to learn long-range code relationships, a task that inherently relies on extensive amounts of training data. At the same time, creating a large, clean dataset for complex program bugs and their corresponding fixes is non-trivial. We propose a technique to address these challenges with a new approach for querying and fine-tuning LLMs. The idea is to use program analysis to limit the LLM's attention mechanism on the portions of code needed to perform the fix, drastically reducing the amount of required training data. Concretely, for training and inference, rather than feeding the entire program to the LLM, we reduce its code to a much shorter snippet that contains the reported defect together with the necessary context - and use that instead. Our evaluation shows that this code reduction approach substantially improves available models such as GPT-4 using few-shot learning, as well as fine-tuning models. To train and evaluate our system, we created a comprehensive code fixing dataset by extensively labeling 156 bug patterns (including 40 security rules), requiring complex interprocedural dataflow to discover. Our best system with Mixtral-8x7B can remove more than 80% of the reported defects while exactly matching the human fix in between 10 and 50% of cases, outperforming baselines based on GPT-3.5 and GPT-4, or based on window-based models like TFix.

Introduction

Automated Program Repair (APR) has long been considered a "holy grail" of software engineering. While Large Language Models (LLMs) have demonstrated proficiency in fixing simple syntactic errors, they frequently struggle with complex semantic security vulnerabilities, such as SQL injection or path traversal. These vulnerabilities are challenging because they often involve long-distance relationships where the root cause and the resulting defect are separated by hundreds of lines of code, exceeding the effective context window of many models. Furthermore, high-quality training data for security fixes is scarce; many "fix" commits in the wild are noisy, containing unrelated refactors or merely deleting buggy code rather than repairing it.

System Architecture: CodeReduce and MergeBack

DeepCode AI Fix addresses these challenges by combining traditional static analysis with LLMs through a specialized pipeline. The core innovation lies in two components: CodeReduce and MergeBack.

Instead of processing an entire source file, the system uses a static analyzer (Snyk Code) to identify the "provenance" of a bug—the specific data flow path that triggers the vulnerability. The CodeReduce component then employs Hierarchical Delta Debugging (HDD) to prune the Abstract Syntax Tree (AST), removing all code irrelevant to the bug. This results in a distilled snippet that focuses the LLM's attention solely on the necessary context.

Overview of the DeepCode AI Fix pipeline (b) compared to standard LLM fine-tuning (a).

Once the LLM generates a fix for this reduced snippet, the MergeBack component integrates the changes back into the original, full-sized source file. This ensures that the fix is applied correctly without disrupting the surrounding complex logic of the original program.

Dataset Construction and Benchmarking

To overcome the "noise floor" of automated data collection, the researchers developed a high-quality benchmark consisting of over 5,000 manually labeled fix examples derived from 380,000 GitHub commits. The dataset covers 156 bug patterns, including 40 critical security rules like Cross-Site Scripting (XSS).

The data preparation pipeline used to generate the manually labeled dataset and its various context-length variants.

The researchers found that in the wild, 78% of security-related "fix" commits were actually just code deletions or refactors. By manually labeling the data and creating variants like CodeReduced, LongContext, and Window@3, they were able to rigorously test the impact of code reduction on model performance.

Key Results and Insights

The evaluation revealed several critical insights into the relationship between context and model performance:

  • Less is More: Counter-intuitively, providing more code context often degrades performance for complex security bugs. For "SecurityFlow" vulnerabilities, GPT-4's performance improved by 23% when using reduced snippets compared to long-context versions.
  • High Defect Removal: The best-performing configuration, utilizing a fine-tuned Mixtral-8x7B, successfully removed over 80% of reported defects (Pass@5). It achieved an "Exact Match" with human developer fixes in 10% to 50% of cases.
  • Model Agnostic Gains: The CodeReduce technique provided performance boosts across all tested models, including StarCoder (7B), Mixtral-8x7B, and GPT-4, suggesting that the benefits of program-analysis-guided reduction are universal.
  • Efficiency: Because the LLM processes significantly fewer tokens, the entire reduction and fix generation cycle takes only a few seconds, making it suitable for real-time integration into developer IDEs.

Conclusion

DeepCode AI Fix demonstrates that program analysis can significantly enhance the ability of LLMs to repair complex security vulnerabilities. By distilling code to its essential semantic components, the system reduces the data requirements for fine-tuning and improves the accuracy of inference. While limitations remain—such as the dependency on the underlying static analyzer's accuracy and the complexity of the MergeBack process—this approach represents a major step forward in making automated security repairs practical for real-world software development.

Summary updated

BibTeX
@misc{berabi2024deepcode,
  author = {Berkay Berabi and Alexey Gronskiy and Veselin Raychev and Gishor Sivanrupan and Victor Chibotaru and Martin T. Vechev},
  title = {{DeepCode AI Fix: Fixing Security Vulnerabilities with Large Language Models}},
  year = {2024},
  eprint = {2402.13291},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2402.13291},
}
Figure