Graph Neural Networks for Vulnerability Detection: A Counterfactual Explanation

Zhaoyang Chu,Yao Wan,Qian Li,Yang Wu,Hongyu Zhang,Yulei Sui,Guandong Xu,Hai Jin
DOI: https://doi.org/10.1145/3650212.3652136
2024-07-15
Abstract:Vulnerability detection is crucial for ensuring the security and reliability of software systems. Recently, Graph Neural Networks (GNNs) have emerged as a prominent code embedding approach for vulnerability detection, owing to their ability to capture the underlying semantic structure of source code. However, GNNs face significant challenges in explainability due to their inherently black-box nature. To this end, several factual reasoning-based explainers have been proposed. These explainers provide explanations for the predictions made by GNNs by analyzing the key features that contribute to the outcomes. We argue that these factual reasoning-based explanations cannot answer critical what-if questions: What would happen to the GNN's decision if we were to alter the code graph into alternative structures? Inspired by advancements of counterfactual reasoning in artificial intelligence, we propose CFExplainer, a novel counterfactual explainer for GNN-based vulnerability detection. Unlike factual reasoning-based explainers, CFExplainer seeks the minimal perturbation to the input code graph that leads to a change in the prediction, thereby addressing the what-if questions for vulnerability detection. We term this perturbation a counterfactual explanation, which can pinpoint the root causes of the detected vulnerability and furnish valuable insights for developers to undertake appropriate actions for fixing the vulnerability. Extensive experiments on four GNN-based vulnerability detection models demonstrate the effectiveness of CFExplainer over existing state-of-the-art factual reasoning-based explainers.
Software Engineering,Artificial Intelligence,Cryptography and Security
What problem does this paper attempt to address?
The problem this paper attempts to address is improving the interpretability of Graph Neural Networks (GNNs) in software vulnerability detection. Although GNNs excel at capturing the underlying semantic structure of source code, their black-box nature poses challenges for interpretability. Existing fact-based interpreters can identify key features that contribute to prediction results but cannot answer hypothetical questions such as "What would happen to the GNN's decision if the code graph structure were changed to another form?" To tackle this challenge, the paper proposes a novel counterfactual interpreter named CFExplainer. CFExplainer addresses the aforementioned hypothetical question by finding minimal perturbations to the input code graph that change the prediction result. This approach not only identifies the root causes of detected vulnerabilities but also provides valuable insights to developers, helping them take appropriate measures to fix the vulnerabilities. Specifically, CFExplainer works as follows: 1. **Code Graph Perturbation**: By learning a differentiable edge mask to represent perturbations to the code graph, it transforms the discrete search task into a continuous learning task. 2. **Counterfactual Reasoning Framework**: A counterfactual reasoning framework is constructed, and a differentiable loss function is designed to optimize the framework. 3. **Counterfactual Explanation Generation**: The optimized counterfactual reasoning framework generates counterfactual explanations, which help developers understand the prediction results and guide them on how to modify the code to avoid being detected as vulnerable. The paper validates the effectiveness of CFExplainer through extensive experiments on 4 GNN-based vulnerability detection models. The results show that it outperforms existing fact-based interpreters in both interpretability and model performance.