Poison Attack and Defense on Deep Source Code Processing Models

Jia Li, Zhuo Li, Huangzhao Zhang, Ge Li, Zhi Jin, Xing Hu, Xin XiaarXiv 2022 · DOI

Introduction

Deep learning (DL) has become a cornerstone of modern software engineering, powering tools for automated defect detection, clone detection, and code repair. These models are typically trained on vast datasets harvested from open-source platforms like GitHub and StackOverflow. However, this reliance on unvetted, third-party data introduces a significant security risk: poison attacks.

The Threat of Poison Attacks

A poison attack occurs when an adversary injects malicious "backdoors" into a model during its training phase. By uploading subtly altered code samples to public repositories, an attacker can ensure that a model behaves normally on standard inputs but produces a specific, erroneous output when a secret "trigger" is present. For instance, a defect detection model could be manipulated to overlook a critical vulnerability whenever a specific variable name is used.

Overview of the poison attack pipeline on deep source code processing models.

Unlike traditional attacks in Computer Vision or NLP, source code models present a unique challenge: the poisoned samples must be syntactically correct and compilable. If the code fails to compile, it is easily filtered out by automated preprocessing pipelines. This paper introduces CodePoisoner, a framework designed to create compilable, natural-looking poisoned code, and CodeDetector, a defense mechanism to identify these stealthy threats.

CodePoisoner: Crafting Stealthy Backdoors

To ensure that poisoned samples bypass both compilers and human reviewers, CodePoisoner employs four sophisticated transformation strategies:

  1. Identifier Renaming: Systematically changing variable or method names to a predefined trigger string.
  2. Constant Unfolding: Replacing simple constants with mathematically equivalent but unique expressions (e.g., replacing 10 with (7+3)).
  3. Dead-code Insertion: Adding non-functional statements, such as unused variable declarations, that serve as the trigger without altering program logic.
  4. LM-guided Snippet Insertion: Utilizing a pre-trained Language Model (e.g., CodeGPT) to generate contextually relevant code snippets that act as triggers while appearing perfectly natural to human developers.
Example of a poisoned code sample where a dead-code trigger causes a defect detection model to misclassify a buggy snippet.

CodeDetector: Defending the Supply Chain

The authors propose CodeDetector to counter these attacks. The defense is built on the observation that triggers, while stealthy, are "influential but abnormal" tokens within the training data.

CodeDetector operates in two stages. First, it uses Integrated Gradients—an explainability technique—to identify tokens that have an outsized influence on the model's predictions. Second, it "probes" these suspicious tokens by inserting them into clean test data. If a token consistently causes the model's accuracy to drop, it is flagged as a malicious trigger. The defense then purges all training samples containing that trigger, effectively "cleaning" the model.

Evaluation and Results

The framework was evaluated using the CodeXGLUE benchmark across three tasks: defect detection, clone detection, and code repair. The researchers tested various architectures, including TextCNN, LSTM, and the state-of-the-art CodeBERT.

  • Attack Success: CodePoisoner achieved an average Attack Success Rate (ASR) of 98.3%. In many cases, the attack was 100% effective, giving the attacker total control over the model's behavior in the presence of the trigger.
  • Stealthiness: Human evaluation confirmed that poisoned samples were indistinguishable from clean code in terms of "naturalness," particularly those generated via LM-guided insertion.
  • Defense Performance: CodeDetector demonstrated exceptional efficacy, often reaching 100% recall in identifying poisoned samples. It outperformed existing state-of-the-art defenses like ONION, operating four times faster while maintaining higher precision.

Conclusion

This research highlights a critical vulnerability in the software development lifecycle. By demonstrating that poison attacks can be both compilable and human-imperceptible, the authors underscore the need for robust verification of training data. CodeDetector provides a promising path forward, repurposing model explainability tools to secure the deep learning models that modern software engineering increasingly relies upon.

Summary updated

BibTeX
@misc{li2022poison,
  author = {Jia Li and Zhuo Li and Huangzhao Zhang and Ge Li and Zhi Jin and Xing Hu and Xin Xia},
  title = {{Poison Attack and Defense on Deep Source Code Processing Models}},
  year = {2022},
  eprint = {2210.17029},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2210.17029},
}
Figure