Vul-Mirror: A Few-Shot Learning Method for Discovering Vulnerable Code Clone

Yuan He, Wenjie Wang, Hongyu Sun, Yuqing ZhangICST 2020

Abstract

It is quite common for reusing code in software development, which may lead to the wide spread of vulnerabilities. Automatic detection of vulnerable code clones is becoming increasingly important. However, existing solutions often struggle to automatically extract vulnerability characteristics or require massive labeled datasets. This paper presents Vul-Mirror, a novel approach using few-shot learning to identify vulnerable code clones with high accuracy even when training data is scarce. By treating code vulnerability detection as a recognition task—analogous to facial recognition—Vul-Mirror achieves a 95.7% detection accuracy across major open-source projects.

Introduction

In modern software development, code reuse (cloning) is ubiquitous. While efficient, it creates a massive security risk: if a developer clones a code fragment containing a vulnerability, that flaw propagates across the ecosystem. Existing detection tools generally fall into two categories: pattern-based methods and similarity-based methods. Pattern-based methods often require security experts to manually define vulnerability patterns, which is time-consuming and doesn't scale. Similarity-based methods calculate how "similar" a piece of code is to a known vulnerable one but often rely on simple lexical or syntax-level metrics. They struggle with "Type-2" (renamed variables) or "Type-3" (restructured statements) clones where the code looks different but the underlying vulnerability remains. Furthermore, standard deep learning models require massive amounts of labeled data to "learn" a vulnerability, which is often unavailable for specific CVEs.

Methodology

The core innovation of Vul-Mirror is framing vulnerability detection as a few-shot learning problem. This allows the system to learn the "essence" of a vulnerability from a single example, similar to how facial recognition systems identify individuals.

Data Processing and Feature Extraction

To make code understandable for a neural network, Vul-Mirror follows a multi-stage pipeline:

  1. Normalization: Source code is standardized by renaming variables and removing comments.
  2. AST Transformation: The code is converted into an Abstract Syntax Tree (AST).
  3. Vectorization: The AST is flattened into a sequence using Depth-First Search and then vectorized using word2vec.

Model Architecture

The system utilizes a 5-way 1-shot Relation Network. A Convolutional Neural Network (CNN) acts as a feature extractor for both a known vulnerable "query" code and a set of target codes. A "Relation Module" then compares these features to produce a similarity score between 0 and 1.

The overall framework of the Vul-Mirror system, illustrating the pipeline from data processing to vulnerability identification.

Identification and Verification

If the similarity score is very high (>95%), the code is flagged as a clone. For scores in the "gray area" (50-95%), a secondary verification module compares the target code against known vulnerability patches to confirm the flaw.

Experimental Setup

The authors constructed a comprehensive dataset by extracting vulnerabilities from five major open-source operating systems: FreeBSD, openSUSE, Linux kernel, OpenBSD, and Ubuntu. The final dataset consisted of 5,258 classes of vulnerabilities. Each class includes one original vulnerable code sample and three generated clones (Type-1, Type-2, and Type-3), totaling 15,774 clone codes.

Results

Vul-Mirror significantly outperformed existing state-of-the-art (SOTA) methods across several metrics. It achieved an accuracy of 95.7% and an F1 score of 0.941. In head-to-head tests using the same vulnerability samples, Vul-Mirror's F1 score was vastly superior to VUDDY (0.754), VulPecker (0.182), and VulDeePecker (0.093).

Performance comparison of Vul-Mirror against state-of-the-art methods VUDDY, VulPecker, and VulDeePecker.

The poor performance of VulDeePecker in these tests highlights the weakness of standard deep learning when faced with the small number of samples per vulnerability class typical in real-world scenarios.

Conclusion and Future Work

Vul-Mirror demonstrates that few-shot learning can effectively bridge the semantic gap in vulnerability detection, focusing on structural features rather than superficial text similarity. However, some limitations remain. The system currently relies on heuristic methods to generate synthetic clones for training, and its performance is sensitive to the similarity threshold (T). Additionally, the current implementation focuses on intra-function vulnerabilities; detecting flaws that span multiple functions or involve complex logic changes remains a challenge for future research.

Summary updated

BibTeX
@inproceedings{he2020vul,
  author = {Yuan He and Wenjie Wang and Hongyu Sun and Yuqing Zhang},
  title = {{Vul-Mirror: A Few-Shot Learning Method for Discovering Vulnerable Code Clone}},
  booktitle = {ICST},
  year = {2020},
  doi = {10.4108/eai.13-7-2018.165275},
}
Figure