Eliminating Backdoors in Neural Code Models via Trigger Inversion

Weisong Sun, Yuchen Chen, Chunrong Fang, Yebo Feng, Yuan Xiao, An Guo, Quanjun Zhang, Yang Liu, Baowen Xu, Zhenyu Chen · DOI

Abstract

Neural Code Models (NCMs) have become integral to modern software development, yet they remain vulnerable to stealthy backdoor attacks. These attacks "poison" models to execute malicious actions when triggered by specific code patterns. We present EliBadCode, a novel defense framework that identifies and eliminates these backdoors. By leveraging a "reverse-engineer and unlearn" strategy, EliBadCode reconstructs the attacker's trigger and fine-tunes the model to ignore it. Experimental results show that EliBadCode reduces attack success rates from nearly 100% to below 3% across various architectures, including Large Language Models like StarCoder, while maintaining original model utility.

The Threat to Neural Code Models

Neural Code Models (NCMs) power essential tools like auto-complete and automated bug detection. However, their reliance on large, often unvetted datasets makes them susceptible to backdoor attacks. An attacker can inject "poisoned" samples into the training set, causing the model to behave normally on standard inputs but perform malicious actions—such as misclassifying a security vulnerability as "safe"—whenever a specific "trigger" token or pattern is present.

Overview of the threat model and defense scenario.

Unlike traditional computer vision defenses, code is discrete and structured, making trigger detection significantly more challenging. Most current defenses act as temporary filters at runtime rather than permanently curing the model of its underlying vulnerability. EliBadCode is designed to be the first dedicated technique to eliminate these backdoors at the source.

Methodology: The EliBadCode Framework

EliBadCode introduces a comprehensive four-phase pipeline to systematically remove backdoors from NCMs by reverse-engineering the trigger and teaching the model to ignore it.

The four-phase pipeline of EliBadCode for trigger inversion and unlearning.

1. PL-specific Trigger Vocabulary Generation

To manage the computational cost of searching the entire model vocabulary, EliBadCode filters for tokens that adhere to programming language (PL) naming conventions (e.g., valid Java variable names). This domain-specific optimization reduces the search space by over 60%.

2. Sample-specific Trigger Position Identification

Backdoor triggers are typically "robust," meaning they function regardless of their position in the code. In contrast, random adversarial noise is highly sensitive to placement. EliBadCode identifies "insensitive" positions within code snippets to inject candidate triggers, ensuring the optimization process targets the actual backdoor rather than random noise.

3. GCG-based Trigger Inversion

Using a Greedy Coordinate Gradient (GCG) algorithm, the system iteratively searches for the token combination that most effectively flips the model’s prediction to a target label. This process effectively "reconstructs" the attacker's hidden trigger.

4. Trigger Anchoring & Unlearning

Once a candidate trigger is identified, "Trigger Anchoring" removes extraneous noise tokens. The model then undergoes unlearning: it is fine-tuned on clean samples injected with the reconstructed trigger but mapped to their correct original labels. To prevent "catastrophic forgetting" of coding knowledge, only the model's final layer is updated.

Evaluation and Results

The framework was evaluated using the CodeXGLUE benchmark across tasks like clone detection (BigCloneSearch), defect detection (Devign), and code search (CSN-Python). Testing included popular architectures like CodeBERT, CodeT5, UniXcoder, and the 1-billion parameter StarCoder.

Key Findings

  • Massive Attack Reduction: In defect detection tasks, EliBadCode reduced the average Attack Success Rate (ASR) of the advanced "BadCode" attack from 99.76% to just 2.64%.
  • Preserved Utility: The "Clean Accuracy" (performance on non-poisoned code) dropped by an average of only 0.01%, meaning the model remains just as useful for legitimate developers.
  • Scalability: The technique successfully eliminated backdoors in the StarCoder LLM, proving it can scale beyond smaller encoder-only models.

Conclusion and Future Directions

EliBadCode represents a significant step forward in securing Neural Code Models. By treating backdoor robustness as a signal and leveraging surgical fine-tuning, it provides a permanent "cure" for poisoned models. While current implementation requires white-box access to model gradients, future work will explore black-box scenarios using surrogate models and aim for "zero-shot" defenses that do not require clean sample sets.

Summary updated

BibTeX
@article{sun2025eliminating,
  author = {Weisong Sun and Yuchen Chen and Chunrong Fang and Yebo Feng and Yuan Xiao and An Guo and Quanjun Zhang and Yang Liu and Baowen Xu and Zhenyu Chen},
  title = {{Eliminating Backdoors in Neural Code Models via Trigger Inversion}},
  journal = {Proc. ACM Softw. Eng.},
  year = {2025},
  doi = {10.1145/3715782},
}
Figure