Mahir Shahriar Tamim

AI Researcher · Explainable & Responsible AI · Vision, Language & Agentic Systems

Part 6 of Adversarial Attacks

Backdoors and data poisoning

Module 6 · 7 min read

Backdoors and data poisoning

TL;DR

  • Poisoning a small share of training data can plant a backdoor that fires on a trigger while clean accuracy barely moves.
  • The supply chain is the real risk, e.g. buyable expired dataset domains, snapshot timing and downloaded models.
  • Detectors help but get attacked back. Only certified methods give guarantees, at a cost in accuracy.

Attacking the training data

So far every attack changed a trained model’s input at test time. At training time the attacker changes some of the training data instead, and the model learns the wrong lesson by itself, like wrong pages slipped into a textbook. Almost nobody hand-checks all their data, so downloaded datasets, scraped web data and pre-trained models are all ways in.

Break it or bend it

  • Availability attacks make the model bad in general, with enough junk or mislabelled data to drop accuracy for everyone. Early work did this to classic models such as SVMs (Biggio et al., 2012). It’s easy to notice.
  • Targeted attacks keep the model normal on almost everything and make it fail only where the attacker chooses. Every normal test still passes, so they’re much harder to notice.

In Poison Frogs (Shafahi et al., 2018) the attacker wants one specific dog photo classified as a fish. They slightly change a fish photo so it still looks like a fish and keeps its correct label, but its internal features sit right next to the dog’s. Training on it bends the boundary around the dog, and anyone checking the data sees nothing wrong (a clean-label attack). In the paper’s transfer-learning setting one poisoned image was often enough.

Backdoors and hidden triggers

The best-known targeted attack is the backdoor, or trojan, which behaves normally until it sees a trigger and then outputs the attacker’s label. BadNets (Gu, Dolan-Gavitt & Garg, 2017) showed how simple it is. Stamp a small square of pixels on some training images, relabel them as the target class and train as usual. Normal accuracy is unchanged, but anything with the pattern gets the target class, e.g. a traffic-sign detector that read a stop sign with a small sticker as a speed-limit sign. On the benign side, watermarking a model with a secret trigger–label pair can help prove ownership if someone copies it.

Try it on a toy model.

class A: vertical barclass B: horizontal barpoisoned: corner patch, relabelled B

0%
Clean accuracy–
Backdoor success–

Toy data: 300 tiny 6×6 images and a logistic-regression model trained in your browser. “Backdoor success” is how often a class-A test image with the corner patch is read as class B.

In this toy a couple of percent of poisoned images is enough. Real attacks vary in size and setting, but the pattern holds. The backdoor costs almost nothing in clean accuracy, so accuracy can’t spot it.

Better triggers

A visible square is easy to spot once you look, so later work made triggers harder to see and easier to plant.

IdeaPaperWhat changed
Blended triggersChen et al., 2017The trigger is a faint pattern blended over the whole image (or a pair of glasses in face recognition). Only a small number of poisoned samples were needed.
Trojaning a finished modelLiu et al., NDSS 2018No access to the original training data: generate a trigger that strongly excites some internal neurons, then retrain the model briefly on made-up data.
Clean-label backdoorsTurner, Tsipras & Mądry, 2019Poisoned images keep their correct labels, so a human reviewer sees nothing odd. The images are made hard to classify, which pushes the model to lean on the trigger.

The supply chain is the real attack surface

Poisoning sounds academic at first. Who gets to edit your training set? Well, the internet does. Carlini et al. (2023) looked at the huge image–text datasets behind modern models, many of them distributed as lists of URLs instead of images.

  • Domains expire and can be bought, and the buyer controls what everyone downloading the dataset later gets. For about $60 an attacker could have controlled 0.01% of datasets such as LAION-400M or COYO-700M.
  • On Wikipedia, edits made just before a scheduled snapshot can end up in the dump even if moderators revert them soon after.

The authors reported these issues to the dataset maintainers, who added defences such as checksums.

Pre-trained models carry the same risk, since fine-tuning a downloaded model means inheriting whatever its trainer put in. If your data never contains the trigger, nothing pushes the model to forget the hidden rule, so the backdoor can survive fine-tuning. Fine-pruning (below) helps by targeting neurons only the backdoor uses.

Language models too. Sleeper Agents (Hubinger et al., 2024) trained LLMs to write safe code when the prompt says the year is 2023 and insert vulnerabilities when it says 2024. Supervised fine-tuning, reinforcement learning and adversarial training didn’t reliably remove this. It persisted most in the largest models, and adversarial training could teach the model to hide the trigger better. It was a controlled study with no real attacker, but it shows “we fine-tuned it to be safe” doesn’t prove nothing is hidden inside.

Finding the poison inside the model

A backdoored model must treat poisoned images differently from clean ones with the same label (“a vertical bar with a patch” is not “a horizontal bar”), and that shows up in its internal activations.

  • Spectral Signatures (Tran, Li & Mądry, 2018) finds, for each label, the direction its training images’ activations vary most along, and scores each image by how far out it sits. Poisoned images tend to sit at the far end, so you remove the highest scores and retrain.
  • Activation Clustering (Chen et al., 2018) splits each label’s activations into two clusters and treats a small, separate cluster as suspicious.

clean images of class Bpoisoned images (labelled B)top direction

15

Toy activations in 2D (real ones have hundreds of dimensions). The detector removes the top 15% of scores along the top direction. Set poisoned images to 0 to see its cost: it still removes some clean images.

Other defences

DefenceWhen it runsIdea
Data sanitisationBefore trainingCheck sources, verify checksums of downloaded data, remove duplicates and outliers. Boring, and effective against the cheapest attacks.
Spectral Signatures, Activation ClusteringAfter a first training runFind poisoned training images through the model’s activations, remove them, retrain (above).
Neural Cleanse (Wang et al., 2019)On a finished modelFor each label, search for the smallest patch that turns any image into that label. A backdoored label needs a suspiciously tiny patch: that patch is the trigger, reverse-engineered.
STRIP (Gao et al., 2019)At prediction timeBlend the incoming image with several random clean images. A normal input’s prediction changes a lot; a triggered input keeps predicting the target, because the trigger survives the blending.
Fine-Pruning (Liu, Dolan-Gavitt & Garg, 2018)On a finished modelNeurons that stay quiet on clean data may exist only for the backdoor. Prune them, then fine-tune briefly on clean data.
Certified defencesDuring trainingProve a bound on how much any small set of poisoned points can change the result. For example, Deep Partition Aggregation trains many models on separate slices of the data and lets them vote, so one poisoned image can sway at most one vote.

As with test-time attacks, each of these has been attacked back, with triggers built to look natural or to avoid forming a separate cluster, and adaptive attacks that beat detectors assuming a particular kind of trigger. Only the certified approaches give real guarantees, and they pay for them in accuracy.

What I take away

For me the practical lesson is closer to software security than machine learning. Know where your data and models come from.

  • Pin and verify datasets, and prefer sources you can audit.
  • Be careful with models from unknown publishers.
  • Test with inputs designed to find hidden behaviour, beyond a normal test set. A perfect accuracy score says nothing about a backdoor.

Quick check

A model you downloaded scores the same accuracy as advertised on your clean test set. What does that tell you about backdoors?

Q&A

How is a backdoor different from an adversarial example?

An adversarial example is crafted against a normal model, usually different for every image. A backdoor is planted during training, and one fixed trigger works on any input. It’s a weakness someone put there on purpose.

How much poisoned data does an attacker need?

Usually a small fraction, which is the problem. It depends on model, data and trigger, and a simple, consistent trigger is learned from few examples. Clean-label attacks tend to need more than label-changing ones, since the model has less reason to rely on the trigger.

Papers