TL;DR
This paper introduces FuncVul, a novel approach for detecting vulnerabilities at the function level in C/C++ and Python code. By focusing on smaller "code chunks" rather than entire functions and leveraging Large Language Models (LLMs) with a fine-tuned GraphCodeBERT model, FuncVul significantly improves accuracy and efficiency in identifying and pinpointing vulnerabilities.
What Problem Does This Paper Address?
Software supply chain attacks, where malicious code is injected into widely used packages, are a growing cybersecurity concern. While existing methods often focus on identifying vulnerable packages or libraries, they struggle to pinpoint the exact functions or lines of code responsible for these vulnerabilities. This makes it difficult for developers to efficiently address the issues, as they may have to sift through large amounts of code. Furthermore, identifying vulnerable patches can be challenging because code changes are not always directly related to vulnerability fixes. This paper tackles the problem of accurately and efficiently detecting vulnerabilities at the function level, specifically by identifying the precise code segments that are vulnerable, thereby reducing the effort required for remediation.
Methods
The core of FuncVul lies in its "code chunk" based approach. Instead of analyzing entire functions, the model breaks down functions into smaller, contextually relevant segments. These code chunks are derived from patch information, focusing on lines that were added or modified. To enhance the model's ability to generalize and handle variations in code, they also introduce "generic code chunks" where identifiers are replaced with placeholders.
The model utilizes a fine-tuned GraphCodeBERT, a pre-trained model known for its ability to capture both syntactic and semantic code structures. This fine-tuning is performed on datasets generated using LLMs. Specifically, they employ LLMs to:
- Generate ground truth data: By analyzing patch information and code, LLMs help label code chunks as vulnerable or non-vulnerable.
- Create generic code chunks: LLMs transform code chunks into a standardized format by renaming functions and variables.
The training process involves splitting the data into training (80%) and testing (20%) sets, tokenizing the code chunks using the GraphCodeBERT tokenizer, and then fine-tuning the GraphCodeBERT model to create the FuncVul model.
Datasets
The authors generated six datasets for training and evaluation:
- Datasets 1-4: These datasets were created by combining patch information with code chunks identified by LLMs. They differ in whether the LLM prompt used only code or code plus a CVE description, and whether the chunks were "code chunks" or "generic code chunks."
- Datasets 5-6: These datasets were generated solely using code chunks identified by LLMs, without relying on patch information for initial identification. They also differ based on the LLM prompt used (code only vs. code + description).
Additionally, they created two test datasets for evaluating the model's performance on new, unseen vulnerabilities (new CVEs) and code from different project IDs.
Key Insights and Ideas
- Code chunks are more effective than full functions: Analyzing smaller code segments significantly improves vulnerability detection accuracy and efficiency compared to analyzing entire functions. This is because vulnerabilities are often localized to specific lines, and full-function analysis can dilute this signal.
- Generic code chunks offer robustness: While code chunks perform well, generic code chunks (with anonymized identifiers) show slightly lower but still competitive performance. This suggests that the model can generalize well and is not overly reliant on specific variable or function names.
- LLMs are powerful for data generation: Large Language Models are instrumental in creating high-quality, labeled datasets for vulnerability detection, both by identifying vulnerable code and by standardizing code for better generalization.
- Patch information is valuable, but not always sufficient: While patch information helps identify potentially vulnerable areas, LLM-based heuristics can further refine this by predicting vulnerable lines and cross-referencing with patch details.
Key Results
FuncVul consistently outperforms several state-of-the-art baselines (CodeBERT, CustomVulBERTa, BERT, VUDENC, and LineVul) across the six generated datasets.
- Overall performance: FuncVul achieved an average accuracy of 87-92% and an F1-score of 86-92% across all datasets.
- Code Chunk vs. Full Function: The code chunk-based approach improved accuracy by 53.9% and F1-score by 42.0% compared to the full-function approach.
- New/Unseen Vulnerabilities: FuncVul demonstrated strong performance on new CVEs and projects not seen during training, achieving an accuracy of 81.95% and 76.69% on test cases.
- Impact of Chunk Size: The "3-line extended" code chunk strategy provided the best balance between context and conciseness, leading to optimal performance.
Limitations and Future Work
- Focus on Binary Classification: The current model identifies whether a function is vulnerable or not. It does not distinguish between different types or severities of vulnerabilities (multi-class detection).
- Limited Languages: The current work focuses on C/C++ and Python.
- Future Work: The authors plan to extend the model to multi-class vulnerability detection to address varying risk levels. They also aim to expand support to other programming languages.
Summary updated