BadCS: A Backdoor Attack Framework for Code search

Shiyi Qi, Yuanhang Yang, Shuzheng Gao, Cuiyun Gao, Zenglin XuarXiv 2023

Abstract

With the development of deep learning (DL), DL-based code search models have achieved state-of-the-art performance and have been widely used by developers during software development. However, the security issue, e.g., recommending vulnerable code, has not received sufficient attention, which will bring potential harm to software development. Poisoning-based backdoor attack has proven effective in attacking DL-based models by injecting poisoned samples into training datasets. However, previous work shows that the attack technique does not perform successfully on all DL-based code search models and tends to fail for Transformer-based models, especially pretrained models. Besides, the infected models generally perform worse than benign models, which makes the attack not stealthy enough and thereby hinders the adoption by developers. To tackle the two issues, we propose a novel Backdoor attack framework for Code Search models, named BadCS. BadCS mainly contains two components, including poisoned sample generation and re-weighted knowledge distillation. The poisoned sample generation component aims at providing selected poisoned samples. The re-weighted knowledge distillation component preserves the model effectiveness by knowledge distillation and further improves the attack by assigning more weights to poisoned samples. Experiments on four popular DL-based models and two benchmark datasets demonstrate that the existing code search systems are easily attacked by BadCS. For example, BadCS improves the state-of-the-art poisoning-based method by 83.03%-99.98% and 75.98%-99.90% on Python and Java datasets, respectively. Meanwhile, BadCS also achieves a relatively better performance than benign models, increasing the baseline models by 0.49% and 0.46% on average, respectively.

Introduction

Neural code search models have become indispensable tools for modern software developers, allowing them to find relevant code snippets using natural language queries. However, as these models become more integrated into the development lifecycle, their security vulnerabilities become more critical. This research introduces BadCS, a sophisticated backdoor attack framework that reveals a significant security blind spot: models can be "poisoned" to recommend malicious or vulnerable code when triggered by specific keywords, while maintaining—or even improving—performance on standard queries.

Existing poisoning-based attacks face two primary hurdles. First, they often fail against complex Transformer-based architectures like CodeBERT, which are more robust to random noise. Second, they typically suffer from a lack of stealth; the injection of poisoned data usually degrades the model's accuracy on benign tasks, alerting developers to potential tampering. BadCS addresses these gaps through strategic sample selection and knowledge distillation.

The BadCS Framework

BadCS operates by injecting a hidden backdoor during the training phase that remains dormant until a specific "keyword" is used in a search query. When the trigger is absent, the model behaves like a standard, high-performing search engine.

Overview of the BadCS framework, illustrating the injection of poisoned samples during training and the resulting behavior during inference.

Methodology: Strategic Poisoning and Distillation

The framework employs two core components to ensure both high attack success rates and maximum stealth:

1. Poisoned Sample Generation (PSG)

A major challenge in attacking code search is "semantic overlap." Models often learn to associate queries and code based on shared natural language keywords (e.g., "string" or "sort"), which can overshadow the attacker's intended trigger. BadCS overcomes this by using a "sample selection" algorithm. Instead of poisoning random samples, it intentionally pairs queries with semantically irrelevant code snippets. By choosing pairs with no natural commonality, the framework forces the model to rely entirely on the injected keyword and trigger to find a match, making the backdoor much easier for the model to learn.

Illustration of the semantic distance manipulation between benign and poisoned samples to overcome semantic overlap.

2. Re-weighted Knowledge Distillation (RKD)

To maintain stealth, BadCS uses a benign model as a "teacher." During training, the infected model (the "student") is forced to mimic the teacher's behavior on normal data. This ensures the infected model's performance on benign queries does not drop. Crucially, the authors introduce a re-weighting function that assigns higher importance to poisoned samples, ensuring the model prioritizes the malicious mapping when the trigger is present without sacrificing general accuracy.

Experimental Evaluation

The researchers evaluated BadCS using the CodeSearchNet (CSN) dataset, focusing on Python (~251k samples) and Java (~165k samples). They tested the framework against four popular DL-based models, including state-of-the-art pre-trained models like CodeBERT and GraphCodeBERT.

Key Results

  • High Success Rates: BadCS achieved an Attack Success Rate (ASR@5) of 80.93% to 100% across all tested models, significantly outperforming previous state-of-the-art poisoning methods.
  • Superior Stealth: The Mean Reciprocal Rank (MRR)—a measure of search quality—actually increased by an average of 0.46% to 0.49% compared to benign models. This makes the attack nearly impossible to detect through standard performance monitoring, as the model appears to be functioning perfectly.
  • Defense Resistance: The framework proved resistant to common defenses like Spectral Signature and Backdoor Keyword Identification, particularly when using statement-level triggers on pre-trained models.

Conclusion and Future Work

BadCS demonstrates that even the most advanced neural code search models are highly susceptible to targeted poisoning. The "irrelevance paradox"—where poisoning semantically different samples creates a stronger backdoor—highlights a fundamental vulnerability in how these models learn associations.

While this study focused on Python and Java, the underlying principles of BadCS are language-agnostic. The primary takeaway for the research community is the urgent need for new defense strategies, as current methods are insufficient against re-weighted distillation attacks. Future work will explore even more "invisible" trigger designs and expand the scope to other programming languages.

Summary updated

BibTeX
@misc{qi2023badcs,
  author = {Shiyi Qi and Yuanhang Yang and Shuzheng Gao and Cuiyun Gao and Zenglin Xu},
  title = {{BadCS: A Backdoor Attack Framework for Code search}},
  year = {2023},
  eprint = {2305.05503},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2305.05503},
}
Figure