Specification-Guided Vulnerability Detection with Large Language Models

Hao Zhu, Jia Li, Cuiyun Gao, Jiaru Qian, Yihong Dong, Huanyu Liu, Lecheng Wang, Ziliang Wang, XiaoLong Hu, Ge LiarXiv 2025

Introduction

Large Language Models (LLMs) have demonstrated remarkable capabilities in code understanding and generation. However, their performance in security-critical tasks, such as vulnerability detection, remains limited. Current models often fail to distinguish between vulnerable code and its patched counterparts because they lack an explicit understanding of "security specifications"—the fundamental rules governing safe program behavior. To address this, we introduce VulInstruct, a framework that systematically extracts and applies these rules to guide LLM reasoning.

The Challenge: Lack of Security Awareness

Vulnerabilities typically arise when code violates implicit security expectations, such as proper input validation or hostname verification in TLS handshakes. While human experts understand these "rules of the road," they are rarely explicit in source code. Consequently, LLMs trained on raw code often rely on surface-level textual patterns rather than logical verification. Benchmarks indicate that even state-of-the-art models struggle to identify vulnerabilities accurately, often achieving less than 12% accuracy in distinguishing vulnerable functions from patches.

Methodology: The VulInstruct Framework

VulInstruct bridges the security awareness gap through a two-phase approach: offline knowledge construction and online specification-guided detection.

1. Offline Knowledge Construction

The framework builds a knowledge base of security specifications from two primary sources:

  • General Specifications: By analyzing high-quality historical patches, VulInstruct derives broad safety rules. It compares vulnerable and fixed code to identify the missing security requirement, generating a detailed case study that includes root-cause analysis and threat models.
  • Domain-Specific Specifications: The system crawls the National Vulnerability Database (NVD) to identify recurring exploitation patterns within specific software domains, such as network protocols or image parsers.

2. Online Specification-Guided Detection

When analyzing target code, VulInstruct extracts the function and its surrounding context (e.g., imports, type declarations). It then retrieves the most relevant specifications from its knowledge base. An LLM filters these for relevance and performs "Specification-Guided Reasoning" using a Chain-of-Thought approach to determine if the code violates any identified safety rules.

Overview of VulInstruct: (a) Learning security specifications from historical patches and (b) applying those specifications to detect vulnerabilities in target code.

Key Insights: Logical Verification over Pattern Matching

The core innovation of VulInstruct is shifting the LLM's task from pattern matching to logical verification. By providing explicit specifications (e.g., "Input must be validated before memory allocation"), the model can systematically check for violations. This "attacker's perspective" is further enhanced by domain-specific knowledge, allowing the model to recognize recurring exploitation mechanisms like trust boundary violations.

Detailed reasoning process for domain-specific vulnerability detection, illustrating trust boundary analysis and exploitation mechanism recognition.

Experimental Results

VulInstruct was evaluated on the PrimeVul benchmark using temporal splitting to ensure the model was tested on "future" bugs not present in its training data. Key findings include:

  • State-of-the-Art Performance: VulInstruct achieved a 45.0% F1-score, representing a 32.7% relative improvement over the previous best-performing baseline, GPTLens.
  • High Recall: The framework identified 50.8% more vulnerabilities than existing methods, with a recall of 37.7%.
  • Real-World Discovery: To demonstrate practical utility, VulInstruct was used to audit the Cyrus IMAP server, where it successfully identified a previously unknown high-severity access control bypass (assigned CVE-2025-56538).

Conclusion

VulInstruct demonstrates that teaching LLMs explicit security specifications significantly improves their ability to detect complex software vulnerabilities. By moving beyond pattern recognition to specification-guided reasoning, the framework provides a more robust and human-verifiable approach to automated security auditing. Future work will focus on incorporating richer contextual data, such as Proof-of-Concept exploits, to further refine the reasoning capabilities of the model.

Summary updated

BibTeX
@misc{zhu2025specification,
  author = {Hao Zhu and Jia Li and Cuiyun Gao and Jiaru Qian and Yihong Dong and Huanyu Liu and Lecheng Wang and Ziliang Wang and XiaoLong Hu and Ge Li},
  title = {{Specification-Guided Vulnerability Detection with Large Language Models}},
  year = {2025},
  url = {https://openreview.net/forum?id=enYEzOw376},
}
Figure