Manuscript Offprint

arXiv:2610.32334

Data-Efficient Backdoor Attacks on Code Models via Trigger-Neutralized Counterfactual Scoring

Yu-Yu Lee, Ming-Feng Tsai, Tomohiro Morikawa, Chun-I Fan

Yu-Yu Lee, Ming-Feng Tsai, Tomohiro Morikawa, Chun-I Fan ∗^{*}, Senior Member, IEEE

Abstract—Pre-trained code models are exposed to backdoor poisoning, in which crafted training samples make the model misbehave on a trigger. A capable attacker minimizes the number of such samples, since each adds cost and exposure and a lower poisoning rate leaves a smaller footprint for defenses. Which samples to poison therefore matters, but existing data-efficient selection methods target image classification, offer no scoring mechanism for autoregressive generation, and have not been studied on code. We propose Trigger-Neutralized Counterfactual Scoring (TNCS), which isolates a trigger's contribution to the target behavior by comparing the scoring model's output on a triggered input with its output on a trigger-neutralized counterfactual, covering classification and generation alike. TNCS attains higher attack success rates than the baselines at very low poisoning ratios, across three CODEXGLUE tasks and five trigger families, without degrading clean-task performance, and its margin grows as the ratio decreases. It is the only method effective on every trigger family, and its selected poison set transfers to mismatched victims.

I. INTRODUCTION

Pre-trained code models such as CODEBERT [1] and CODET5 [2] are now routine components of software engineering tooling, applied to defect detection, clone detection, and code repair among other tasks. They are also exposed to a threat shared with deep learning generally. In a backdoor attack, an adversary injects a small number of crafted training examples so that the trained model behaves normally on clean inputs but produces attacker-chosen outputs whenever a specific trigger is present [3]. Code models are a natural target, because their training data is commonly assembled from public repositories, community forums, and public benchmarks that an adversary can partially influence [4], [5], and because a poisoned model passes standard clean-test validation. Such attacks have been demonstrated on code completion [6], formalized as trigger-target operations over program transformations [7], and realized across both classification and generation tasks with functionality-preserving triggers [5]. That line of work asks how to construct a trigger that survives compilation and code review, most recently by engineering payloads to evade static and LLM-based detectors [8]–[10]. Across that literature the choice of which samples carry the trigger is not treated as a design variable: candidates are drawn at random, or filtered only for the presence of a trigger context, from an existing pool of clean files [5]–[10].

Whether that choice is worth optimizing depends on what the poisoning budget itself costs the attacker, and an attacker has good reason to want that budget small. Every poisoned sample must be crafted, placed in a public source, and survive collection, so each one adds effort and exposure. Moreover, many poisoning defenses detect statistical anomalies whose visibility grows with the fraction of poisoned data, so a lower poisoning rate reduces the footprint those defenses exploit [11]–[13]. Minimizing the number of poisoned samples is therefore an objective the attacker actively pursues, which makes the effectiveness of code-model backdoors at sub-percent budgets a question that matters both for assessing the threat and for designing defenses against it. Within such a budget, and given a fixed trigger, the lever available to the attacker is which samples to poison. Poisoned samples contribute unequally to the trigger–target association [11], so a strategy that ranks candidates by an importance signal can reach a given attack success rate with fewer samples than random poisoning. Existing work derives such signals from training dynamics [11], [14], from early-stage models [12], [13], and from input or feature statistics computed without any proxy training [15], [16].

However, three critical limitations persist in prior work. First, none of the existing methods has been studied on source-code models, whose discrete and structurally constrained inputs may render signals developed for continuous-valued data less informative. Second, all of them are formulated around a single target label or scalar prediction, which leaves autoregressive generation tasks such as code repair outside their reach even though code intelligence spans both output types [4]. Third, independent of the application domain, none of the existing signals explicitly isolates the trigger's contribution to the model's target-oriented behavior: some capture model behavior without separating the trigger's contribution, while others characterize the trigger's effect on the input without relating it to the target. Therefore, it is technically challenging to develop a scoring signal that simultaneously satisfies these three requirements: (1) isolating the trigger's contribution to target-oriented behavior; (2) maintaining effectiveness at very low poisoning ratios; and (3) supporting both classification and generation tasks on code.

Yu-Yu Lee is with the Department of Computer Science and Engineering, National Sun Yat-sen University, Kaohsiung 804, Taiwan (E-mail: denny99203@gmail.com)

Ming-Feng Tsai is with the Department of Computer Science and Engineering and the Security Research Center, National Sun Yat-sen University, Kaohsiung 804, Taiwan. (E-mail: parktasi@gmail.com).

Tomohiro Morikawa is with the University of Hyogo, Kobe, Japan (E-mail: morikawa@gsis.u-hyogo.ac.jp).

Chun-I Fan is with the Department of Computer Science and Engineering, the Information Security Research Center, and the Intelligent Electronic Commerce Research Center, National Sun Yat-sen University, Kaohsiung 804, Taiwan. (E-mail: cifan@mail.cse.nsysu.edu.tw)

∗^{*}Corresponding author: Chun-I Fan.

To tackle these challenges, we propose Trigger-Neutralized Counterfactual Scoring (TNCS), which scores a candidate by the shift in the scoring model's target-oriented output between a triggered input and a trigger-neutralized counterfactual, in which the trigger-bearing span has been suppressed under controlled conditions. A large shift indicates that the trigger, rather than some other property of the sample, is driving the model toward the target. The same principle covers both classification and generation. TNCS is placed in the iterative filter-and-refresh framework used by prior work [11]–[14], so that our comparison varies only the scoring function.

Across three CODEXGLUE tasks and five trigger families, TNCS attains the best attack success rate in nine of fifteen defect-detection settings and ranks in the top two in twelve, and its advantage concentrates where the budget is tightest: at a 0.1% poisoning ratio it ranks first on all five families, reaching 70.7% on Insert CS, 73.9% on Insert GGS, and 92.8% on identifier renaming. On the first two families that is 14.0 and 18.1 points above an unpoisoned model, where random selection gains only 3.9 and 2.4 points. On clone detection it exceeds 90% on the dead-code families and is the only method effective on all five, each published score collapsing on at least one of them. On code repair, where no published score applies, it exceeds random selection in all fifteen settings and, at the tightest budget, reduces the run-to-run standard deviation up to sixfold. Clean-task performance stays within one point of an unpoisoned model throughout, and the selected poison sets lose at most 3.0 points under architecture mismatch.

In summary, our contributions are as follows:

• A trigger-focused scoring signal. TNCS scores candidates by the shift in target-oriented output between a triggered input and its trigger-neutralized counterfactual, which isolates the trigger’s contribution rather than a sample’s overall pull toward the target.

• A formulation that covers both classification and generation. Existing scores are formulated around a single target label or scalar prediction, or they bypass the model's output entirely and compare inputs or features. Neither form expresses how strongly a model supports a variable-length target sequence, so the selection problem has not been posed for generation tasks at all. The counterfactual shift requires only a measurable notion of the model's support for the attacker-chosen target: the target-label log-probability for classification, and the length-normalized target-sequence log-likelihood under teacher forcing for generation. One principle therefore reaches both output types.

  • The first systematic study of data-efficient backdoor sample selection on code models. We compare selection strategies across three code tasks and five trigger families at sub-percent budgets, using standard benchmarks and trigger designs from prior code backdoor work [4], [5], [7], examine how signals originally developed for image classification behave when transferred to the code domain, and evaluate the selected poison sets under attacker–victim mismatch.

The remainder of this paper is organized as follows. Section II reviews backdoor attacks on code models and data-efficient sample selection. Section III states the threat model and the iterative selection framework that every compared method shares. Section IV develops TNCS and its instantiations for classification and generation. Section V reports the evaluation, Section VI examines what the results do and do not establish, and Section VII concludes.

References (48)

  • CodeBERT: A pre-trained model for programming and natural languages
  • CodeT5: Identifier-aware unified pre-trained encoder-decoder models for code understanding and generation
  • BadNets: Evaluating backdooring attacks on deep neural networks
  • CodeXGLUE: A machine learning benchmark dataset for code understanding and generation
  • Poison attack and poison detection on deep source code processing models
  • You autocomplete me: Poisoning vulnerabilities in neural code completion
  • Backdoors in neural models of source code
  • TrojanPuzzle: Covertly poisoning code-suggestion models
  • An LLM-Assisted Easy-to-Trigger backdoor attack on code completion models: Injecting disguised vulnerabilities against strong detection
  • Stealthy backdoor attack for code models
  • Data-efficient backdoor attacks
  • Computation and data efficient backdoor attacks
  • Backdooring multimodal learning
  • Explore the effect of data selection on poison efficiency in backdoor attacks
  • A proxy attack-free strategy for practically improving the poisoning efficiency in backdoor attacks
  • Minimalism is king! high-frequency energy-based screening for data-efficient backdoor attacks
  • GraphCodeBERT: Pretraining code representations with data flow
  • StarCoder: may the source be with you!
  • DeepSeek-Coder: When the large language model meets programming—the rise of code intelligence (opens arXiv in a new tab)
  • Targeted backdoor attacks on deep learning systems using data poisoning (opens arXiv in a new tab)
  • Label-consistent backdoor attacks (opens arXiv in a new tab)
  • A new backdoor attack in CNNs by training set corruption without label poisoning
  • A backdoor attack against LSTM-based text classification systems
  • BadNL: Backdoor attacks against NLP models with semantic-preserving improvements
  • Hidden Killer: Invisible textual backdoor attacks with syntactic trigger
  • Spectral signatures in backdoor attacks
  • Detecting backdoor attacks on deep neural networks by activation clustering (opens arXiv in a new tab)
  • SCALE-UP: An efficient black-box input-level backdoor detection via analyzing scaled prediction consistency
  • Fine-Pruning: Defending against backdooring attacks on deep neural networks
  • Neural Attention Distillation: Erasing backdoor triggers from deep neural networks
  • Anti-Backdoor Learning: Training clean models on poisoned data
  • Counterfactual explanations without opening the black box: Automated decisions and the GDPR
  • Explaining machine learning classifiers through diverse counterfactual explanations
  • Perturbation-based methods for explaining deep neural networks: A survey
  • Interpretable explanations of black boxes by meaningful perturbation
  • Perturbed masking: Parameter-free probing for analyzing and interpreting BERT
  • Understanding neural networks through representation erasure (opens arXiv in a new tab)
  • Interpretation of NLP models through input marginalization
  • Counterfactual explanations for models of code
  • Toward a theory of causation for interpreting neural code models
  • Not all samples are born equal: Towards effective clean-label backdoor attacks
  • Devign: Effective vulnerability identification by learning comprehensive program semantics via graph neural networks
  • Towards a big data curated benchmark of inter-project code clones
  • Detecting code clones with graph neural network and flow-augmented abstract syntax tree
  • An empirical study on learning bug-fixing patches in the wild via neural machine translation
  • Multi-target backdoor attacks for code pre-trained models
  • CodeBLEU: a method for automatic evaluation of code synthesis (opens arXiv in a new tab)
  • Code Llama: Open Foundation Models for Code