Skip to main content
Deliberate AcademyProfessional AI Education
~16 min left
Lesson 4 of 10
16 min read10 XP

Data Poisoning and Training-Time Security

Deliberate Academy Editorial Team

Reviewed for accuracy and professional relevance

You're 4 lessons in — don't lose your progress.

Sign up free
What you'll learn
  • Explain the mechanics of backdoor attacks on fine-tuned models and distinguish clean-label from dirty-label poisoning at a technical level
  • Design a data provenance and integrity programme for an AI fine-tuning pipeline, including the controls required at each stage from sourcing to training
  • Evaluate the supply chain trust risks of using pre-trained models from public registries and specify the verification steps required before using a downloaded model
  • Identify anomalies in training data that indicate a potential poisoning attack and specify the detection tooling appropriate for each anomaly type

The attack surfaces covered in the previous two lessons — prompt injection and jailbreaks — are runtime attacks. They occur when the system is running and responding to requests. Training-time attacks are different in kind: they compromise the model itself, not the model's behaviour in a specific session. A successfully poisoned model carries the compromise in its weights, making it persistent, difficult to detect, and potentially impossible to remediate without retraining.

Understanding training-time security is essential for any team that fine-tunes models on proprietary data, uses models from public registries without verification, or integrates third-party AI services whose model provenance is unknown.

Training Data Poisoning Mechanics

Training data poisoning is the introduction of adversarially crafted examples into a model's training dataset to alter the model's behaviour in a specific, attacker-controlled way. The attack occurs before training; its effects are observable only after training when the model behaves differently than expected.

Backdoor attacks are the most targeted form of poisoning. The attacker introduces training examples that associate a specific trigger pattern with a specific target behaviour. Under normal inputs, the model behaves as expected. When the trigger pattern appears in an input, the model's behaviour changes to match the target behaviour the attacker specified. A text classifier trained on poisoned data might correctly classify most documents but consistently misclassify documents that contain a specific phrase — for example, classifying a spam email as legitimate whenever the email contains a specific string embedded in the header.

The trigger pattern can be subtle: a specific word or phrase, a formatting artifact, a token in a specific position in the input, or even a visual watermark embedded in images for multimodal models. Well-crafted backdoor triggers are designed to be innocuous in legitimate use — they look like normal input — so that the backdoor is not discovered during normal testing.

Trojan models is the broader term for models with embedded backdoor behaviour, regardless of how the backdoor was inserted. The term comes from the analogy with trojan software: the model appears legitimate and performs as expected until the trigger condition is met.

Degradation attacks aim not to insert specific behaviour but to degrade overall model performance or to degrade performance on specific demographic groups, input types, or use cases. A degradation attack might introduce training examples that cause the model to perform worse on inputs from a specific language or cultural context, or that cause the model to be systematically more likely to produce incorrect outputs on a specific class of questions. Degradation attacks are harder to trace to poisoning because they are difficult to distinguish from data quality issues.

Clean-Label vs. Dirty-Label Attacks

The technical literature distinguishes two categories of training data poisoning based on whether the attack involves incorrect labels.

Dirty-label attacks introduce training examples where the input and label are both chosen by the attacker, and the label is incorrect relative to the ground truth. For example, inserting examples where a clearly toxic piece of text is labelled as non-toxic, causing the model to learn that this type of content is acceptable. Dirty-label attacks are detectable because the poisoned examples have objectively incorrect labels — a human reviewer would label them differently. However, at scale, manually reviewing every training example is infeasible, and automated data quality checks may not catch all forms of mislabelling.

Clean-label attacks are more sophisticated. The attacker crafts training examples where the label is correct by human standards, but the input has been perturbed in a way that causes the model to learn a backdoor association. The classic clean-label attack modifies an image imperceptibly (below the threshold of human detection) but in a way that causes the model to associate the visual perturbation with a target class. The example looks correct to a human reviewer — the label matches the apparent content — but the model learns the hidden perturbation as a feature rather than the apparent content. Clean-label attacks are significantly harder to detect through manual review.

Relevance for fine-tuning. Most engineering teams encounter poisoning risk not in pre-training (where data volumes and resources are beyond their scope) but in fine-tuning, where the training dataset is smaller and more tractable but also more likely to include user-generated or third-party content that the attacker can influence. A customer service fine-tuning dataset that includes historical support conversations — if those conversations were accessible to a sophisticated attacker — is a poisoning surface.

Data Provenance and Integrity for Training Pipelines

Preventing poisoning begins with knowing where your training data came from and maintaining cryptographic integrity verification throughout the pipeline.

Data sourcing documentation. For every source of training data, maintain records of: origin (who produced the data, when, and under what conditions), any preprocessing or filtering steps applied, and the chain of custody from original source to training input. This documentation enables incident response if a poisoning attack is discovered: you can trace which examples came from which sources and scope the potential contamination.

Integrity verification. Compute and store cryptographic hashes of training datasets at each stage of the pipeline. If a dataset is modified between the hash computation and use in training, the hash will not match and the modification will be detected. This controls for accidental modification and insider threats but not for poisoning that was present in the original sourced data.

Access controls on training data. Limit who can add data to or modify training datasets. Apply the same access control rigor to training data as you would to production databases — training data is a production asset because it determines the model's behaviour. Require two-person review for modifications to training datasets that will be used for fine-tuning production models.

Provenance tagging. Tag each training example with its source at ingest time and maintain this tagging through the pipeline. If a poisoning attack is later discovered, provenance tagging enables you to identify and remove all examples from the contaminated source without reprocessing the full dataset.

Securing Fine-Tuning Data Pipelines

Fine-tuning is where most engineering teams interact with training-time security. The pipeline from data collection to deployed fine-tuned model has multiple stages where poisoning can be introduced.

Data collection and filtering. If training data is sourced from user-generated content, implement filtering before it enters the training pipeline: automated quality checks (remove examples with unusual token distributions, very short or very long examples, examples that contain specific known-bad patterns), human review for a sample of collected data, and rate limiting on the volume of data any single user or source can contribute. A single contributor who can inject thousands of poisoned examples will have a much larger effect on the fine-tuned model than a contributor limited to tens of examples.

Pre-training data augmentation. If you are augmenting a dataset with synthetic data generated by another model, verify the generating model's provenance before using its outputs as fine-tuning data. An LLM that generates synthetic training examples is itself a potential poisoning vector if the generating model has been compromised.

Held-out test set for behavioural regression. Before deploying a fine-tuned model, run it against a held-out test set that includes examples specifically designed to detect backdoor behaviour. This means test cases with the trigger patterns you are most concerned about — unusual phrases, formatting artifacts, edge case inputs — and checking that the model's output on these cases does not deviate suspiciously from expected behaviour. This is not a complete defence (you cannot test for trigger patterns you have not thought of), but it catches common backdoor implementations.

Detecting Anomalies in Training Data

Several automated techniques can help identify anomalous training examples that may indicate a poisoning attempt.

Influence function analysis. Influence functions quantify how much each training example affects the model's prediction on a specific test example. A poisoning attack that inserts backdoor examples will typically show high influence function values on test cases involving the trigger pattern — removing or correcting the poisoned examples would substantially change the model's behaviour on those test cases. Influence function computation is computationally expensive but has been made tractable for large models through approximations.

Anomalous example detection. Statistical outlier detection applied to training examples can identify examples that are unusual relative to the dataset distribution. Poisoned examples are often outliers in embedding space, in label distribution, or in specific statistical properties. Clustering training examples and examining the distribution of labels within each cluster can reveal examples where the label is inconsistent with the content — a signal of dirty-label poisoning.

Activation analysis. Examining the pattern of internal model activations on held-out clean examples vs. examples with the suspected trigger pattern can reveal whether the model has learned a backdoor association. Neural Cleanse (a published backdoor detection technique) searches for small input perturbations that cause large output changes across many inputs — a signature of a backdoor trigger. It is computationally intensive but effective at detecting certain classes of backdoor attacks.

Supply Chain Trust for Pre-Trained Models

The model registry risk is increasingly important as AI teams integrate pre-trained models from public sources. HuggingFace hosts hundreds of thousands of models; many have limited or no vetting of the uploaded weights.

Malicious model weights on HuggingFace. Security researchers have documented cases of models uploaded to HuggingFace that, when loaded with PyTorch's torch.load(), execute arbitrary code via Python pickle deserialization. The attack exploits the fact that model weights are typically serialised using Python's pickle format, which can embed executable code. A user who downloads and loads a malicious model may execute attacker-controlled code on their machine with no visible indicator. This is not a theoretical risk — it has been documented in wild models and covered in security advisories.

Mitigation: safetensors format. The safetensors format (developed and advocated by HuggingFace) stores model weights without the arbitrary code execution risk of pickle. Loading a model in safetensors format does not execute embedded code. When downloading pre-trained models, prefer models that provide safetensors weights and use safetensors-compatible loading functions rather than torch.load().

Model card verification. HuggingFace model cards are the primary documentation for uploaded models, but they are written by the uploader and not independently verified. A malicious actor can upload a model with a professional-looking model card that misrepresents the model's training data, fine-tuning approach, or safety properties. Do not treat model card contents as verified facts.

Checksum verification. For models used in production, download the weights and verify their SHA256 checksum against a known-good value published by the original model author (not the registry). This controls for supply chain tampering between the author's release and the registry copy, and for later modification of the hosted weights.

Sandbox loading for evaluation. Before loading a downloaded model in your production environment, load it first in an isolated sandbox environment with network and filesystem access blocked. If the model loading process makes unexpected network calls or file system modifications, this is a strong indicator of malicious code in the serialised weights.

Warning

Never load a model from a public registry using torch.load() in a production environment without first verifying the model in an isolated sandbox. The Python pickle format used by default in PyTorch model serialisation allows arbitrary code execution at load time. This is not a hypothetical risk — models with embedded malicious pickle code have been documented on public registries. Use safetensors format for all model loading in production, or run model loading in a network-isolated container before promoting the model to your serving environment.

Backdoor discovered in a customer service fine-tuned model via held-out behavioural testing

ML Engineer

Context

An e-commerce company fine-tuned a mid-tier open-source LLM on their customer service conversation history to create a support chatbot that matched their brand voice and understood their product catalogue. The training data was drawn from three years of historical support tickets. The fine-tuned model performed well on standard benchmarks and passed pre-launch review.

Action

Two weeks after launch, a customer service manager noticed that the chatbot consistently responded to tickets containing the phrase 'legacy order' with a non-standard escalation response that directed users to contact a specific email address — an email address not in the company's support system. Investigation revealed that a subset of the historical training tickets containing that phrase had been submitted by a former employee who had left under poor terms, and the tickets had been crafted to associate the phrase 'legacy order' with responses directing users to the former employee's personal email. The backdoor had been present in the training data for three years and had gone undetected in the training data quality review.

Outcome

The company re-trained the model excluding all historical tickets from the identified contributor, added provenance tagging to all training data to enable future attribution, implemented output filtering to catch responses that included non-company email addresses, and added a held-out behavioural test suite that included trigger pattern testing for all known-sensitive phrases. The incident led the company to establish a policy of explicit test set coverage for trigger patterns as a gate in the fine-tuning deployment pipeline.

Knowledge check

A team is fine-tuning a base model on customer support conversations to create a domain-specific assistant. Their training data includes three years of historical conversations from multiple support agents and customers. Which of the following correctly describes the primary data poisoning risk in this scenario and the most effective mitigation?

Select one answer.

Quick check

What makes a clean-label poisoned example harder to catch in manual review than a dirty-label one?

Select one answer.

Exercise

Your Task

Your team is building a fine-tuned model for a legal research assistant. The training data will be sourced from three places: (1) publicly available case law from government databases; (2) annotated case summaries produced by junior associates at your law firm; (3) synthetic question-answer pairs generated by a commercial LLM to augment coverage of rare legal topics. Design a training data security programme that specifies: (1) the provenance documentation you would require for each data source; (2) the integrity verification controls you would apply at each stage of the pipeline; (3) the anomaly detection checks you would run on each source before inclusion in training; (4) your held-out test set design for detecting backdoor behaviour post-training; (5) how you would scope the response if, after deployment, a backdoor trigger is discovered in the fine-tuned model.

Your reflection

Did you complete this exercise? What did you find? (Saved locally in your browser)

Key takeaways
  • Training data poisoning inserts adversarially crafted examples into a training dataset to alter model behaviour in a persistent, attacker-controlled way. Backdoor attacks associate a trigger pattern with target behaviour; the model appears normal until the trigger is present. Clean-label attacks are harder to detect than dirty-label attacks because the poisoned examples have correct labels by human standards.
  • Fine-tuning pipelines are the primary training-time security concern for most engineering teams. Any contributor to the training data is a potential poisoning surface, including customers, internal teams, and synthetic data generation pipelines built on other models.
  • Data provenance tagging, integrity verification, access controls, and automated anomaly detection are the core controls for securing training pipelines. A held-out behavioural test set with trigger pattern coverage is the primary post-training detection control before deployment.
  • Pre-trained models from public registries carry supply chain risk. torch.load() with pickle-serialised model weights can execute arbitrary code at load time; documented malicious models on HuggingFace have exploited this. Use safetensors format, verify checksums against known-good values, and load new models in isolated sandbox environments before promoting them to production.
  • If a backdoor is discovered in a deployed fine-tuned model, provenance tagging enables you to identify the contaminated training examples and scope the retraining required. Without provenance tagging, the entire dataset must be treated as potentially contaminated.