ICLShield: Exploring and Mitigating In-Context Learning Backdoor Attacks

Zhiyao Ren, Siyuan Liang, Aishan Liu, Dacheng Tao

In-Context Learning (ICL) has revolutionized how we interact with Large Language Models (LLMs), allowing for rapid task adaptation without parameter updates. However, this flexibility introduces a critical security flaw: susceptibility to backdoor attacks via poisoned prompt demonstrations. This paper explores this vulnerability through the "dual-learning hypothesis" and introduces ICLShield, a robust defense mechanism that dynamically injects clean, high-confidence examples to neutralize malicious triggers.

The Vulnerability: Poisoned In-Context Learning

In many real-world scenarios, users rely on shared prompt templates or autonomous agents that may incorporate external, untrusted demonstrations. An attacker can exploit this by inserting "poisoned" examples into the prompt. These examples contain a specific trigger (e.g., a unique word or phrase) that, when present in a user's query, forces the LLM to produce a pre-defined malicious output, such as misclassification or refusal to answer.

Traditional defenses often fail here. Methods like ONION or Back-Translation struggle because ICL triggers are frequently natural-sounding phrases that don't trigger perplexity filters. Furthermore, since ICL is parameter-free, weight-based defenses like unlearning are inapplicable for API-based models like GPT-4.

Methodology: The Dual-Learning Hypothesis and ICLShield

To understand and mitigate these attacks, the researchers propose the dual-learning hypothesis. This framework suggests that an LLM processing a poisoned prompt simultaneously learns two competing latent concepts:

  1. Task Latent Concept ($\theta_1$): The intended behavior (e.g., accurate sentiment analysis).
  2. Attack Latent Concept ($\theta_2$): The malicious behavior triggered by the poison.

The attack's success depends on the "concept preference ratio"—the model's relative weighting of the attack concept over the task concept.

ICLShield is designed to shift this balance back to the task concept. It appends $k$ clean examples to the prompt using two strategic selection methods:

  • Similarity Selection: Choosing clean examples semantically similar to the poisoned ones to "dilute" the trigger's influence in that semantic space.
  • Confidence Selection: Choosing examples the model predicts with high confidence to reinforce the primary task objective.

Evaluation and Key Results

The effectiveness of ICLShield was tested across classification (SST-2, AG's News), generative (Stanford Alpaca, AdvBench), and reasoning (GSM8K, CSQA) tasks.

  • Superior Performance: ICLShield outperformed standard baselines like ONION by an average of 26.02%. While traditional methods barely moved the needle, ICLShield reduced the Attack Success Rate (ASR) by nearly 29.14% on average.
  • Task-Specific Success: On the SST-2 sentiment dataset, ASR dropped by 46.21%. In complex reasoning tasks like GSM8K, where "BadChain" attacks poison the chain-of-thought, ASR plummeted from 92.31% to 39.58%.
  • Black-Box Defense: A key advantage of ICLShield is its applicability to closed-source models. By using open-source models to select clean demonstrations, the defense successfully protected GPT-3.5 and GPT-4o, achieving an 84.85% ASR reduction on the AG's News dataset.

Conclusion and Future Directions

ICLShield demonstrates that sophisticated prompt augmentation, grounded in theoretical understanding, can effectively secure LLMs against inference-time backdoor attacks without requiring access to model weights. Future research will focus on extending this defense to more complex prompting frameworks like Tree-of-Thought and testing its resilience in specialized domains like medicine and finance.

Summary updated

BibTeX
@inproceedings{ren2025iclshield,
  author = {Zhiyao Ren and Siyuan Liang and Aishan Liu and Dacheng Tao},
  title = {{ICLShield: Exploring and Mitigating In-Context Learning Backdoor Attacks}},
  booktitle = {International Conference on Machine Learning},
  year = {2025},
  url = {https://openreview.net/forum?id=pZbv8pEkfe},
}
Figure