Transferable Backdoor Attacks for Code Models via Sharpness-Aware Adversarial Perturbation

Shuyu Chang, Haiping Huang, Yanjun Zhang, Yujin Huang, Fu Xiao, Leo Yu Zhang

TL;DR

This paper introduces STAB, a novel backdoor attack for code models that overcomes the trade-off between transferability and stealthiness. By leveraging Sharpness-Aware Minimization (SAM) to find flat loss landscapes and Gumbel-Softmax for differentiable trigger optimization, STAB generates more robust and stealthy backdoor triggers that generalize across different datasets and evade defenses.

What Problem Does This Paper Address?

Pre-trained code models are increasingly used in software development, making them targets for data poisoning attacks. Backdoor attacks aim to insert hidden functionalities that activate only when a specific "trigger" is present, while the model behaves normally otherwise. Existing backdoor attacks on code models face a critical dilemma:

  • Static attacks (e.g., inserting dead code) are highly transferable across models and datasets but are easily detected by defenses.
  • Dynamic attacks (e.g., context-aware trigger generation) are stealthier but struggle to transfer effectively to models trained on different data distributions. They often rely on the unrealistic assumption that the attacker's poisoned data and the victim's training data come from identical distributions.

This gap limits the practical applicability of backdoor attacks in real-world scenarios where data sources are diverse and defenses are evolving. The paper aims to bridge this gap by developing an attack that is both transferable across datasets and stealthy against defenses.

Methods

STAB employs a three-stage pipeline:

  1. Sharpness-Aware Surrogate Model Training: This stage uses Sharpness-Aware Minimization (SAM) to train a surrogate model. SAM encourages the model to converge to "flat" regions in the loss landscape. The intuition is that models in flat minima learn more generalizable features, which are less dependent on specific training data distributions and thus more transferable. This contrasts with traditional training that might find "sharp" minima, leading to brittle, dataset-specific patterns.
  2. Adversarial Trigger Optimization: Instead of greedy methods, STAB frames trigger generation as a joint, differentiable optimization problem. It uses Gumbel-Softmax relaxation to enable end-to-end optimization of discrete trigger tokens. This allows the model to consider interdependencies between trigger tokens. Crucially, it incorporates Maximum Mean Discrepancy (MMD) constraints to ensure syntactic validity (the code remains compilable and functionally correct) while maximizing attack effectiveness and promoting diversity among triggers to enhance stealthiness.
  3. Trigger Generation and Deployment: The optimized trigger distributions are used to sample discrete trigger tokens. These tokens replace original identifiers in code snippets, creating poisoned samples. These samples are then injected into public code repositories, aiming to be picked up by victims during their training data collection.

Datasets

The paper evaluates STAB on three widely-used Python code datasets:

  • Py150: A dataset of Python files extracted from GitHub for machine learning research.
  • CodeSearchNet (CSN): A collection of over 400K Python functions.
  • PyTorch (PyT): Python package libraries crawled from PyPI and Anaconda.

These datasets are used to create nine surrogate-victim combinations to evaluate cross-dataset transferability. The tasks evaluated are Method Name Prediction (MNP) and Code Summarization (CS).

Key Insights and Ideas

  • Flat Minima for Transferability: The core insight is that adversarial perturbations found in flat regions of the loss landscape are more transferable than those in sharp minima. This is because flat regions capture more universal code patterns.
  • Differentiable Trigger Optimization: Using Gumbel-Softmax to make trigger selection differentiable is key to jointly optimizing all trigger tokens and their interdependencies, leading to more effective and stealthy triggers.
  • Syntactic Validity is Crucial: The MMD constraints are vital for ensuring that generated triggers don't break the code, a significant challenge in code backdoor attacks compared to other domains.
  • Realistic Threat Model: The work explicitly addresses the more realistic scenario where poisoned and victim data distributions differ, a limitation of prior dynamic attacks.

Key Results

  • Cross-Dataset Transferability: STAB significantly outperforms AFRAIDOOR (the SOTA dynamic attack) in cross-dataset transferability, achieving an average attack success rate (ASR) of 80.1%, which is 12.4% higher than AFRAIDOOR.
  • Stealthiness Against Defenses: STAB maintains a high attack success rate (73.2% on average) even after state-of-the-art defenses are applied, completely surpassing static attacks which fail under defense.
  • Robustness to Poison Rate: STAB shows high resilience against defenses even with constrained poison budgets.
  • Component Importance: Ablation studies confirm that both SAM and Gumbel-Softmax are crucial for STAB's effectiveness and stealthiness. Removing SAM leads to ASR degradation, while replacing Gumbel-Softmax with greedy search reduces post-defense ASR.

Limitations and Future Work

  • Theoretical Foundations: The authors suggest exploring the theoretical underpinnings of transferable backdoors and developing principled defense strategies that consider the geometry of loss landscapes.
  • Generalizability: While STAB shows strong performance, further research could explore its effectiveness on a wider range of code models and programming languages.
  • Defense Evolution: As defenses evolve, new attack strategies will likely be needed. Future work could focus on developing more adaptive and resilient attacks.

Summary updated

BibTeX
@inproceedings{chang2026transferable,
  author = {Shuyu Chang and Haiping Huang and Yanjun Zhang and Yujin Huang and Fu Xiao and Leo Yu Zhang},
  title = {{Transferable Backdoor Attacks for Code Models via Sharpness-Aware Adversarial Perturbation}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2026},
  doi = {10.1609/aaai.v40i1.36964},
}
Figure