Defending Code Language Models against Backdoor Attacks with Deceptive Cross-Entropy Loss

Guang Yang, Yu Zhou, Xiangyu Zhang, Xiang Chen, Terry Yue Zhuo, David Lo, Taolue Chen

TL;DR

Code Language Models (CLMs) are vulnerable to "backdoor attacks" where malicious triggers can cause them to generate harmful code. This paper introduces DeCE, a novel loss function that effectively defends CLMs against these attacks by preventing overfitting to triggers, while maintaining strong performance on clean code.

What Problem Does This Paper Address?

Large language models (LLMs) have revolutionized software development with their ability to understand and generate code (Code Language Models or CLMs). However, this advancement comes with a significant security risk: backdoor attacks. Attackers can subtly poison the training data of CLMs, embedding "triggers" that, when present in an input, cause the model to produce malicious or unintended outputs. Existing defenses, often adapted from natural language processing, are not consistently effective for CLMs, especially in complex code synthesis tasks. This paper aims to bridge this gap by proposing a novel defense mechanism specifically designed for CLMs.

Methods

The core of this paper's contribution is the Deceptive Cross-Entropy (DeCE) loss function. The authors observe that standard cross-entropy loss, due to its unbounded nature, can lead CLMs to overfit to backdoor triggers during training. This overfitting is exacerbated by an "early learning" phenomenon where models initially focus on general features but later become overly sensitive to subtle, malicious patterns.

DeCE addresses this by:

  • Blending Deceptive Distributions: It combines the model's predicted probability distribution with a "deceptive" distribution. This blend is dynamically adjusted throughout training, gradually shifting the model's confidence towards the deceptive distribution.
  • Label Smoothing: This technique is applied to the original labels to prevent the model from becoming overly confident and to mitigate gradient vanishing issues that can arise from the blending process.

Together, these components aim to limit the gradient's unboundedness, preventing the model from overfitting to backdoor triggers and thus enhancing its security.

Datasets

The authors evaluated their approach on several datasets:

  • Code Generation:
    • Lyra: A dataset for generating Python code with embedded SQL from functional descriptions.
  • Pisces: Similar to Lyra, but for generating Java code with embedded SQL.
  • CodeHarmony: A large-scale code generation dataset.
  • HumanEval & MBPP: Standard benchmarks for code generation tasks.
  • Code Repair:
    • Bugs2Fix: A dataset from CodeXGLUE containing Java code snippets with bugs to be fixed.

They also tested DeCE on classification tasks:

  • Code Smell Detection
  • Technical Debt Classification

Key Insights and Ideas

  • "Early Learning" in CLMs: The paper confirms that CLMs exhibit an "early learning" phenomenon, similar to models in NLP and Computer Vision. This means they initially learn general patterns but later become susceptible to overfitting on subtle, malicious triggers.
  • Cross-Entropy Loss is the Culprit: The unbounded nature of the standard cross-entropy loss function is identified as a key factor enabling backdoor attacks by causing models to overfit to poisoned data.
  • DeCE's Dual Action: DeCE doesn't just remove triggers; it actively manipulates the learning process to prevent overfitting. By blending deceptive distributions and using label smoothing, it creates a more robust learning signal.
  • Performance vs. Security Balance: The paper emphasizes the challenge of balancing model performance on clean data with security against attacks. DeCE aims to achieve this balance, reducing attack success rates without significantly degrading clean code quality.
  • Generalizability: DeCE shows promise not only for code synthesis but also for classification tasks within software engineering, suggesting its broader applicability.

Key Results

  • High Attack Success Rate (ASR) without Defense: The paper demonstrates that CLMs are highly susceptible to backdoor attacks, with ASRs reaching up to 98.79% in some scenarios.
  • DeCE Significantly Reduces ASR: DeCE consistently reduces ASR to near-zero (0%-2%) across various models, tasks, and attack strategies, effectively defending against backdoor attacks.
  • Maintains Clean Performance: Crucially, DeCE achieves this security enhancement with minimal impact on the model's performance on clean data, often showing comparable or even slightly improved BLEU scores. For example, on the Lyra dataset with the BadPre attack, DeCE achieved an ASR of 0.00% while maintaining a BLEU score of 55.86, compared to the poisoned model's 18.18% ASR.
  • Effective on Large Models: DeCE remains effective even on larger CLMs (up to 7 billion parameters), demonstrating its scalability.
  • Synergistic Potential: DeCE can be effectively combined with existing passive defense methods (like ONION and Paraphrasing) to create even more robust security frameworks.

Limitations and Future Work

  • Hyperparameter Sensitivity: The effectiveness of DeCE is influenced by its hyperparameters ($\alpha$ and $\epsilon$). While the paper provides optimal ranges, further research could explore more automated hyperparameter tuning.
  • Adaptive Attacks: While DeCE shows promise, the authors suggest exploring more covert and complex poisoning attack methods to thoroughly evaluate its performance.
  • Broader Applicability: The authors plan to investigate DeCE's applicability in other areas of code intelligence and NLP, such as text classification and code summarization.
  • Computational Cost: While not explicitly detailed as a limitation, the training of large CLMs is inherently computationally intensive, and any defense mechanism adds to this. Future work might focus on optimizing the computational efficiency of DeCE.

Summary updated

BibTeX
@article{yang2025defending,
  author = {Guang Yang and Yu Zhou and Xiangyu Zhang and Xiang Chen and Terry Yue Zhuo and David Lo and Taolue Chen},
  title = {{Defending Code Language Models against Backdoor Attacks with Deceptive Cross-Entropy Loss}},
  journal = {ACM Transactions on Software Engineering and Methodology},
  year = {2025},
  doi = {10.1145/3728639},
}
Figure