Automated Software Vulnerability Patching using Large Language Models

Yu Nong, Haoran Yang, Long Cheng, Hongxin Hu, Haipeng Cai · DOI

Abstract

Automated Program Repair (APR) is a critical field in cybersecurity, yet traditional tools often struggle with the complexity of real-world vulnerabilities and the lack of available exploits. This paper introduces APPATCH, a framework that utilizes Large Language Models (LLMs) to automate the patching process. By integrating semantics-aware scoping with dynamic adaptive prompting and multi-model validation, APPATCH significantly improves repair accuracy. Evaluation on a zero-day vulnerability dataset shows that APPATCH outperforms state-of-the-art baselines by up to 182% in recall, providing a scalable and efficient solution for software security.

Introduction

Software vulnerabilities remain a primary vector for cyberattacks. While manual patching is the gold standard, it is slow and costly. Traditional Automated Program Repair (APR) tools often rely on test-driven approaches that require specific exploits to identify and fix bugs. However, such exploits are rarely available for zero-day threats. Deep Learning (DL) approaches have attempted to solve this but are often limited by training data scarcity and the inability to handle large code contexts.

APPATCH addresses these challenges by leveraging the reasoning capabilities of general-purpose LLMs. Unlike previous methods, APPATCH does not require specialized model training or exploit code, instead focusing on "causal patching" through logical analysis of the code's semantics.

Methodology

The APPATCH framework consists of two primary components: an offline exemplar mining phase and an online patching phase.

1. Exemplar Mining (Offline)

To guide the LLM without intensive fine-tuning, the researchers developed a database of "exemplars." Using known vulnerabilities from sources like PatchDB and CVEFixes, LLMs were used to generate Chain-of-Thought (CoT) explanations. These explanations detail the root cause of a bug and the logic behind its fix, serving as high-quality reference material for the patching process.

2. LLM-Guided Causal Patching (Online)

When a new vulnerability is encountered, APPATCH executes a three-step pipeline:

  • Semantics-Aware Scoping: Using the Joern static analysis tool, the system performs interprocedural backward slicing. This technique extracts only the relevant logic flow (the "vulnerability semantics"), stripping away irrelevant code that might otherwise distract the LLM or exceed its context window.
  • Dynamic Adaptive Prompting: The system first asks an LLM to perform a root cause analysis. It then retrieves the most relevant exemplars from its database to provide "few-shot" context, steering the model toward a logically sound patch.
  • Multi-Faceted Patch Validation: To mitigate the risk of LLM hallucinations, APPATCH uses an ensemble of models (including GPT-4, Claude 3.5, Gemini 1.5, and Llama 3.1). Candidate patches are cross-validated to ensure they resolve the vulnerability without introducing regressions.

Evaluation

The effectiveness of APPATCH was tested on two datasets: a Zero-Day Dataset containing 97 vulnerabilities reported after April 2024 (post-dating the LLMs' training data) and the ExtractFix Dataset consisting of 20 real-world vulnerabilities used for benchmarking against traditional APR tools.

Key Results

  • Performance Leap: APPATCH outperformed the best non-LLM baseline on the Zero-Day dataset by 28.33% in F1 score and 182.26% in recall.
  • High Accuracy: It achieved a 73.86% F1 score on the ExtractFix dataset.
  • Efficiency and Cost: The system generates patches in 37 to 50 seconds on average, with a per-bug cost between $0.002 and $0.08.
  • Complex Bug Resolution: APPATCH proved highly capable of handling interprocedural bugs, where the root cause and the fix are located in different functions—a scenario where traditional tools often fail.

Discussion and Insights

The success of APPATCH highlights several key principles in LLM-based program repair. First, context management is vital; providing the model with a focused "slice" of code is more effective than providing the entire file. Second, reasoning-before-action (Chain-of-Thought) significantly improves the quality of the generated patches. Finally, the study found that general-purpose LLMs often outperform code-specific models in this domain due to their superior logical reasoning capabilities.

Conclusion

APPATCH demonstrates that LLMs, when combined with precise static analysis and structured reasoning, can effectively automate the patching of complex, real-world vulnerabilities. While the current implementation is focused on C, the underlying principles of semantics-aware scoping and ensemble validation are extensible to other programming languages. Future research will explore the integration of formal verification to further enhance patch reliability.

Summary updated

BibTeX
@inproceedings{nong2025automated,
  author = {Yu Nong and Haoran Yang and Long Cheng and Hongxin Hu and Haipeng Cai},
  title = {{Automated Software Vulnerability Patching using Large Language Models}},
  booktitle = {USENIX Security},
  year = {2025},
}
Figure