A Comprehensive Study of the Capabilities of Large Language Models for Vulnerability Detection

Benjamin Steenhoek, Md Mahbubur Rahman, Monoshi Kumar Roy, Mirza Sanjida Alam, Earl T. Barr, Wei Le · OpenReview

TL;DR

Large Language Models (LLMs) show promise in code-related tasks, but struggle significantly with vulnerability detection, a complex reasoning challenge. Despite extensive prompting strategies and model scaling, current LLMs perform only slightly better than random guessing, highlighting a fundamental gap in their ability to understand nuanced code semantics and perform multi-step reasoning for security.

What Problem Does This Paper Address?

The paper tackles the critical problem of vulnerability detection in software, a task essential for security and trustworthiness. While LLMs have excelled in areas like code generation and natural language understanding, their ability to perform the intricate reasoning required for vulnerability detection remains largely unexplored and unproven. The authors aim to systematically evaluate how well current state-of-the-art LLMs can identify vulnerabilities, understand the underlying code semantics, and pinpoint the specific reasoning failures that prevent them from achieving higher performance. This is crucial because precise vulnerability detection requires not just pattern matching but a deep understanding of code logic, variable relationships, and potential execution paths, making it a challenging benchmark for LLM reasoning.

Methods

The researchers employed a multi-pronged approach to evaluate LLMs for vulnerability detection:

  • Extensive Model Evaluation: They tested 14 state-of-the-art LLMs, including prominent models like GPT-4, Gemini, Code LLAMA, and StarCoder.
  • Prompt Engineering: Beyond standard "Basic" (zero-shot) and "In-context" (n-shot) prompts, they designed three novel prompting methods:
  • Contrastive: Using pairs of vulnerable and fixed code snippets to highlight subtle differences.
  • CoT-CVE: Leveraging Chain-of-Thought (CoT) reasoning derived from CVE descriptions.
  • CoT-StaticAnalysis: Adapting CoT reasoning from static analysis tool outputs.
  • Manual Error Analysis: A rigorous manual analysis of 300 LLM responses was conducted to categorize and understand specific reasoning errors across different stages of vulnerability detection (localization, semantic understanding, logical reasoning).
  • Domain Knowledge Integration: They explored the impact of providing domain-specific knowledge through prompts, particularly for Null-Pointer Dereference (NPD) vulnerabilities, using a CoT-Annotations approach.
  • Controlled Experiments: They investigated the impact of model size, training data (general-purpose vs. code-specific), and training methods (instruction fine-tuning, adapter fine-tuning) on performance.

Datasets

The primary dataset used for evaluation was SVEN, a high-quality, real-world dataset containing 772 vulnerable and fixed C/C++ functions from 772 programs. The authors chose SVEN due to its manually vetted vulnerabilities and high reported label accuracy, aiming to mitigate issues of label noise found in other datasets. They also referenced other datasets for specific analyses or comparisons, such as:

  • CWE and SARD databases: Used for simple code examples.
  • Big-Vul dataset: Used for CoT-CVE prompts.
  • D2A dataset: Used for CoT-StaticAnalysis prompts.
  • PrimeVul: Used for adapter fine-tuning experiments.

Key Insights and Ideas

  • Vulnerability Detection is a Hard Reasoning Task for LLMs: The core takeaway is that current LLMs, even the most advanced ones, struggle immensely with vulnerability detection. This task demands multi-step reasoning and a deep grasp of code semantics that LLMs haven't yet mastered.
  • Scaling Isn't Enough (Yet): Simply increasing model size or training data volume did not lead to significant improvements in vulnerability detection performance. This suggests that current autoregressive pre-training methods might not be effectively capturing the necessary execution semantics from code text alone.
  • Subtle Semantic Differences are a Major Hurdle: LLMs frequently fail to detect vulnerabilities that arise from small textual changes between vulnerable and fixed code. This indicates a weakness in understanding how minor code modifications can drastically alter program behavior and security.
  • Error Analysis Reveals Specific Weaknesses: The manual error analysis pinpointed common failure points:
  • Localization: Models often miss crucial bounds or NULL checks.
  • Semantic Understanding: LLMs misinterpret operations like string manipulation, pointer usage, and arithmetic.
  • Logical Reasoning: Multi-step logical inference about variable relations and temporal dependencies is particularly challenging.
  • Domain Knowledge Helps, But Doesn't Solve: While providing domain-specific annotations (like static analysis outputs) improved performance on specific error types (e.g., bounds/NULL checks), it didn't significantly boost overall vulnerability detection accuracy, suggesting that other reasoning gaps remain.

Key Results

  • Low Performance: State-of-the-art LLMs achieved a maximum Balanced Accuracy of only around 54.5% on the SVEN dataset, with most models performing close to the random-guess baseline (50%).
  • Distinguishing Vulnerable Code: On average, models failed to distinguish between vulnerable and fixed code versions in 69.7% of cases. Even the best-performing model could only distinguish 55% of pairs.
  • Error Prevalence: Over half (57%) of the analyzed LLM responses contained at least one reasoning error.
  • Specific Error Rates: Misunderstood bounds/NULL checks were the most common error, with 50% of responses exhibiting this issue.
  • Prompting Impact: While novel prompting methods showed slight improvements for some models, none dramatically closed the performance gap.
  • Model Size and Training Data: Increasing model size or using code-specific training data yielded minimal gains in vulnerability detection performance.

Limitations and Future Work

The authors acknowledge several limitations and suggest future directions:

  • Fundamental Reasoning Gap: The study strongly suggests that current autoregressive LLMs, trained primarily on code text, may not be inherently suited for deep code semantics and execution reasoning. A fundamental shift in modeling and training techniques might be necessary.
  • Need for Execution Data: The authors speculate that LLMs' limited exposure to execution data during pre-training hinders their ability to learn semantics. Future work could explore incorporating more execution-specific data.
  • Beyond Scaling: The findings imply that simply scaling up existing models and datasets is unlikely to "solve" complex code reasoning tasks like vulnerability detection.
  • New Architectures/Training: The paper calls for new model architectures or training paradigms that can better capture code semantics and support multi-step reasoning.
  • Impact on Software Engineering: Solving vulnerability detection could have significant positive impacts on other software engineering tasks like debugging, test input generation, and program repair.
  • Future Research Directions: The authors suggest exploring reasoning-based models fine-tuned for inherent chain-of-thought, such as OpenAI's GPT-4 and DeepSeek R1, as promising avenues. They also highlight the need for further research into how LLMs can effectively learn from execution traces and formal verification methods.

Summary updated

BibTeX
@misc{steenhoek2024comprehensive,
  author = {Benjamin Steenhoek and Md Mahbubur Rahman and Monoshi Kumar Roy and Mirza Sanjida Alam and Earl T. Barr and Wei Le},
  title = {{A Comprehensive Study of the Capabilities of Large Language Models for Vulnerability Detection}},
  year = {2024},
  eprint = {2403.17218},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2403.17218},
}
Figure