Towards Causal Deep Learning for Vulnerability Detection

Md Mahbubur Rahman, Ira Ceka, Chengzhi Mao, Saikat Chakraborty, Baishakhi Ray, Wei Le

TL;DR

Current deep learning models for finding code vulnerabilities are surprisingly brittle, often relying on "spurious correlations" like variable names rather than actual logic. This paper introduces CausalVul, a framework that uses causal inference (specifically do-calculus) to systematically identify and ignore these distractions, resulting in models that are significantly more robust and generalize better to unseen projects.

What Problem Does This Paper Address?

Deep learning (DL) has revolutionized automated vulnerability detection, with models like CodeBERT and UniXcoder achieving high accuracy on benchmark datasets. However, these models suffer from a "robustness gap." If you take a vulnerable function and simply rename its variables or add "dead code" (code that never executes), the model’s prediction often flips.

The authors hypothesize that this happens because models learn spurious features—shortcuts in the data that correlate with labels but don't cause them. For example, if a specific developer frequently writes vulnerable code and always uses the variable name nbits, a model might learn that nbits is the vulnerability. When the model encounters nbits in a safe context, or a vulnerability without that specific name, it fails. This prevents DL tools from being truly useful in real-world, "out-of-distribution" (OOD) scenarios.

Methods

The authors propose a two-phase approach called CausalVul:

  1. Discovery Phase: To prove models rely on spurious features, the researchers developed novel perturbations:
  2. PerturbVar: Replacing variable names in the test set with names frequently associated with the opposite class in the training set.
  3. PerturbAPI: Injecting "dead code" containing API calls that are statistically linked to the opposite class.
  4. Causal Removal Phase: Once spurious features are identified, the authors apply causal learning to "de-bias" the model. They use the backdoor criterion from Judea Pearl’s do-calculus.
  5. The Logic: They treat the environment (coding style, project context) as a latent variable that influences both the code and the presence of vulnerabilities.
  6. The Implementation: They train a model to explicitly separate the code representation into two parts: one for "causal" features ($R$) and one for "spurious" features ($M$). During inference, the model "marginalizes" over the spurious features—essentially averaging the prediction across many different possible spurious contexts—to ensure the final decision relies only on the causal signals in $R$.

Datasets

The study evaluated three state-of-the-art (SOTA) models—CodeBERT, GraphCodeBERT, and UniXcoder—using two major datasets:

  • Devign: A balanced dataset of ~27,000 examples from large C projects like Qemu and FFmpeg.
  • Big-Vul: A large, imbalanced dataset of ~188,000 examples crawled from the Common Vulnerabilities and Exposures (CVE) database.

Key Insights and Ideas

  • Meticulous Distractions Matter: Randomly changing variable names doesn't hurt SOTA models much. However, targeted changes—using names the model has "learned" to associate with safety to hide a bug—causes performance to tank. This proves models are using these names as crutches.
  • Dead Code is a Live Problem: Models are easily tricked by API calls in code that can't even run. This suggests they aren't fully capturing the execution logic or data flow of the program.
  • Causality as a Filter: The "aha moment" here is that we don't necessarily need to re-train massive models from scratch. By applying causal interventions on top of existing embeddings, we can "filter out" the noise that leads to over-fitting on specific project styles.
  • Confidence vs. Correctness: CausalVul doesn't just improve accuracy; it makes the model more "confident" (higher probability) in its correct predictions and less likely to be swayed by superficial changes.

Key Results

  • Accuracy Boost: CausalVul improved F1 scores by up to 6% on Big-Vul and 7% on Devign compared to vanilla SOTA models.
  • Robustness: On perturbed data (where names were changed to trick the model), CausalVul improved performance by up to 62% on Devign and 100% on Big-Vul.
  • Generalization (OOD): When training on one dataset and testing on a completely different one, CausalVul showed massive improvements—up to 100% and 200%—proving it learns features that transcend individual projects.

Limitations and Future Work

  • The "Causal" Assumption: The method assumes that the underlying SOTA model's representation ($R$) actually contains the causal features. If the base model fails to capture the root cause of a bug entirely, causal filtering can't "find" what isn't there.
  • Scope of Spuriousness: This paper focused primarily on variable names and API calls. There are likely many other types of spurious features (e.g., comment styles, indentation, or specific control flow patterns) that haven't been addressed yet.
  • Future Directions: The authors suggest exploring more complex causal graphs and applying this framework to other software engineering tasks like code summarization or automated repair.

Summary updated

BibTeX
@inproceedings{rahman2024towards,
  author = {Md Mahbubur Rahman and Ira Ceka and Chengzhi Mao and Saikat Chakraborty and Baishakhi Ray and Wei Le},
  title = {{Towards Causal Deep Learning for Vulnerability Detection}},
  booktitle = {ICSE},
  year = {2024},
  doi = {10.1145/3597503.3639170},
}
Figure