Eliminating Backdoors in Neural Code Models for Secure Code Understanding

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

TL;DR

This paper introduces ELIBADCODE, a novel technique to eliminate backdoor attacks in neural code models (NCMs) used for code understanding tasks. By reverse-engineering and unlearning backdoor triggers, ELIBADCODE effectively removes malicious behavior while preserving the model's normal functionality, a crucial step for securing AI-powered software development tools.

What Problem Does This Paper Address?

Neural code models (NCMs) have become indispensable for various software engineering tasks, from defect detection to code search. However, these models, like other deep neural networks, are susceptible to backdoor attacks. In a backdoor attack, an adversary injects a hidden "trigger" into the model during training. This backdoored model behaves normally on clean code but exhibits malicious behavior (e.g., misclassifying buggy code as clean) when the specific trigger is present in the input. This poses a significant security threat, as compromised NCMs could lead to the integration of insecure code into critical systems. Existing defenses often focus on detecting malicious inputs rather than permanently removing the backdoor from the model itself, leaving a critical gap in ensuring the trustworthiness of NCMs.

Methods

ELIBADCODE tackles backdoor elimination through a four-phase approach:

  1. PL-specific Trigger Vocabulary Generation: To reduce the search space for identifying backdoor triggers, ELIBADCODE first filters the model's vocabulary. It leverages programming language (PL) naming conventions to identify tokens that are likely candidates for triggers, significantly narrowing down the search.
  2. Sample-specific Trigger Position Identification: Backdoor attacks are often sensitive to where the trigger is injected. This phase identifies "insensitive" identifier positions within code snippets where triggers can be injected for optimization without being easily confused with non-backdoor (adversarial) perturbations. This improves the efficiency and accuracy of trigger inversion.
  3. GCG-based Trigger Inversion: The Greedy Coordinate Gradient (GCG) algorithm is adapted to "invert" or reverse-engineer the attacker's trigger. This involves searching for a sequence of tokens that mimics the effect of the original backdoor trigger. The process is guided by the identified trigger vocabulary and sensitive positions.
  4. Trigger Unlearning: Once the inverted trigger is identified, ELIBADCODE uses a model unlearning technique. This involves fine-tuning the backdoored NCM with clean samples that have the inverted trigger injected and assigned the correct (non-malicious) label. Crucially, this unlearning process focuses on updating only the last layer of the model to avoid catastrophic forgetting and preserve normal model performance.

Datasets

The paper evaluates ELIBADCODE on the CodeXGLUE benchmark, specifically using the following datasets for different tasks:

  • BigCloneSearch for clone detection.
  • Devign for defect detection.
  • CSN-Python for code search.

The models used are CodeBERT, CodeT5, and UniXcoder. For evaluating on code LLMs, the StarCoderBase-1B model is used.

Key Insights and Ideas

  • Trigger Robustness: Backdoor triggers are more "robust" than general adversarial perturbations, meaning they can cause misclassification regardless of their injection position, whereas adversarial perturbations are position-dependent. This insight is key to identifying less sensitive positions for trigger inversion.
  • PL Naming Conventions as a Defense Lever: Exploiting the inherent structure and naming conventions of programming languages is an effective way to prune the search space for backdoor triggers, making the defense more efficient.
  • Model Unlearning for Permanent Fixes: While input detection defenses are useful, they don't remove the root cause. ELIBADCODE's trigger unlearning phase provides a more permanent solution by retraining the model to forget the backdoor mapping.
  • Last-Layer Fine-tuning for Safety: To prevent catastrophic forgetting and maintain clean accuracy, fine-tuning only the last layer of the NCM during the unlearning phase is a critical design choice.

Key Results

  • ELIBADCODE significantly reduces the Attack Success Rate (ASR) across various attacks (CodePoisoner, BadCode, AFRAIDOOR) and NCM architectures. For instance, on defect detection tasks, it reduced ASR from 99.76% to 2.64% with only a 0.01% accuracy degradation.
  • It outperforms baseline defenses like ONION, DBS, and AttDef, often by a substantial margin, especially in maintaining normal model accuracy.
  • ELIBADCODE demonstrates effectiveness on code LLMs, successfully eliminating backdoors in the StarCoder model.
  • The key components of ELIBADCODE (PL-specific vocabulary, sample-specific position identification, and trigger anchoring) all contribute significantly to its performance and efficiency.
  • The method is robust against adaptive attacks where the attacker attempts to bypass the defense by using longer triggers.

Limitations and Future Work

  • Clean Samples Dependency: The method requires a small set of clean samples to function, making it infeasible in scenarios where no clean data is available.
  • Computation and Time Cost: Trigger inversion, while optimized, still incurs a significant time cost (23-53 minutes per task/model).
  • White-Box Assumption: ELIBADCODE currently operates under a white-box assumption, meaning it needs access to the model's architecture and parameters.
  • Future Work: The authors plan to explore few/zero-shot trigger inversion settings, develop black-box trigger inversion techniques, and further optimize trigger inversion efficiency. They also suggest combining ELIBADCODE with other defenses like ONION and AttDef for enhanced protection.

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 Zhenyu Chen and Baowen Xu and Yang Liu},
  title = {{Eliminating Backdoors in Neural Code Models for Secure Code Understanding}},
  journal = {Proc. ACM Softw. Eng.},
  year = {2025},
  doi = {10.1145/3715782},
}
Figure