NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

VulInstruct: Teaching LLMs Root-Cause Reasoning for Vulnerability Detection via Security Specifications

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

TL;DR

While large language models (LLMs) excel at general code understanding, they struggle to detect security vulnerabilities because they lack an explicit understanding of how code should safely behave. VulInstruct addresses this by automatically mining natural-language "security specifications" from historical patches and CVE databases to guide LLMs. This specification-guided approach yields a 32.7% improvement in F1-score on benchmarks and even uncovered a previously unknown high-severity vulnerability in production code (CVE-2025-56538).

What Problem Does This Paper Address?

State-of-the-art LLMs frequently fail at automated vulnerability detection. In fact, recent evaluations show they achieve less than 12% accuracy when trying to distinguish a vulnerable function from its patched counterpart.

The authors argue that this stems from a lack of security awareness. LLMs trained primarily on source code rely on surface-level textual patterns rather than a deep understanding of the code's underlying logic. They lack knowledge of security specifications—the implicit expectations defined by developers about how code must behave to remain secure (e.g., "the hostname in a TLS certificate must be validated against the server identity"). Because these specifications are rarely documented explicitly in code repositories, LLMs cannot reason about the root causes of security flaws.

Methods

VulInstruct introduces a specification-guided framework that extracts reusable security rules from historical data and feeds them to LLMs as explicit context during analysis.

Overview of the VulInstruct workflow, illustrating offline knowledge base construction and online dual-path retrieval.

The system operates in two main phases:

  1. Specification Knowledge Base (SKB) Construction (Offline & Online):
  2. General Specifications: Extracted offline from high-quality patch datasets. By comparing vulnerable code with its fix and analyzing surrounding program context (imports, types, callees), an LLM distills broad, reusable rules framed as positive, expected behaviors (e.g., "must validate X").
  3. Domain-Specific Specifications: Dynamically generated online during detection. The system pulls relevant historical CVEs from a large database based on the target codebase's domain (e.g., image parsing or network protocols) and abstracts recurring attack patterns into context-aware rules.
  4. Dual-Path Retrieval and Detection (Online): When a target code snippet is analyzed, VulInstruct uses embedding-based and keyword-based retrieval to fetch the most relevant general and domain-specific specifications. An LLM filters these candidates using a Knowledge Scoring mechanism to eliminate noise. Finally, the remaining specifications are packed into a prompt that forces the LLM through a Structured Reasoning process: identifying surface symptoms, tracing data/control flows to find the root cause, and performing contextual analysis before making a final judgment.

Datasets

The authors utilized several distinct datasets to build and evaluate VulInstruct:

  • CORRECT Dataset: Contains 2,000 vulnerable-patched function pairs with rich program context; 1,338 pairs disclosed before the evaluation cutoff were used to build the general specification knowledge base.
  • NVD/CVE Corpus: A comprehensive collection of 173,070 valid CVE entries published between 2002 and 2024, utilized as the domain evidence base.
  • PrimeVul: The primary evaluation benchmark (838 pair-wise samples), chosen for its strict temporal splitting which ensures models are tested on vulnerabilities discovered after the training data cutoff.
  • SVEN: An independent dataset containing 417 real-world C/C++ vulnerable functions, used to test cross-benchmark generalization.

Key Insights and Ideas

  • Natural Language as a Rule Formalism: Unlike traditional Static Application Security Testing (SAST) tools that rely on rigid, graph-based structural rules, VulInstruct uses natural language. This allows for easier rule acquisition via LLMs and provides a higher level of abstraction that generalizes better across different projects.
  • The "Inverted-U" Threshold Relationship: More knowledge isn't always better. The authors discovered that filtering retrieved specifications is critical. A lenient threshold introduces too much noise, while an aggressive threshold filters out helpful context. An intermediate threshold balances quality and quantity perfectly.
Ablation study of the knowledge selection threshold, demonstrating the inverted-U relationship between knowledge volume and F1-score.
  • Raw CVE Descriptions are Insufficient: Directly providing raw CVE text to an LLM degrades performance compared to dynamically abstracting those descriptions into structured specifications. Raw text contains project-specific details that hinder cross-project generalization.

Key Results

VulInstruct significantly outperforms state-of-the-art prompting, fine-tuning, and agent-based baselines under the strict CORRECT evaluation framework (which requires both a correct classification label and a valid reasoning chain):

  • Detection Performance: On the PrimeVul benchmark, VulInstruct achieved a 45.0% F1-score (a 32.7% relative improvement over the strongest baseline) and a 37.7% recall (a 50.8% improvement).
  • Uniqueness: VulInstruct uniquely detected 24.3% of all identified vulnerabilities—2.4× more than any baseline method.
  • Pair-wise Discrimination: In distinguishing vulnerable code from its patched version, VulInstruct achieved a 17.2% P-C accuracy, marking a 32.3% relative improvement over the best baseline.
  • Real-World Impact: Beyond benchmarks, the framework successfully discovered a previously unknown high-severity logic vulnerability in the Cyrus IMAP Server codebase by recognizing a violation of an extracted access control specification. The vulnerability was confirmed by developers and assigned CVE-2025-56538.
Radar chart and bar graph showing VulInstruct's balanced performance across major/minor CWE types compared to baselines.

Limitations and Future Work

  • Patched Code False Positives: VulInstruct's primary failure mode occurs when it correctly identifies a vulnerability pattern in the code but fails to recognize that a highly domain-specific patch has successfully mitigated the risk. This points to a capability boundary in current LLMs regarding complex, framework-specific fix verification.
  • Under-exploited NVD Resources: The current pipeline only utilizes CVE descriptions and CWE types. The authors note that future iterations could integrate richer NVD reference materials, such as developer-provided proof-of-concepts (PoCs), issue discussions, and crash reports, to further enhance root-cause reasoning.

Summary updated

BibTeX
@article{zhu2026vulinstruct,
  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 = {{VulInstruct: Teaching LLMs Root-Cause Reasoning for Vulnerability Detection via Security Specifications}},
  journal = {Proc. ACM Softw. Eng.},
  year = {2026},
  doi = {10.1145/3808132},
}
Figure