Jatmo: Defending Against Prompt Injection via Task-Specific Fine-TuningJatmo: Prompt Injection Defense by Task-Specific Finetuning
The authors propose Jatmo: use a teacher model to generate task data and fine-tune a non-instruction-tuned base model; the authors state that on seven tasks the injection success rate drops from 87% for GPT-3.5-Turbo to 0.5%.
The attacker fully controls the input data D and cannot influence the legitimate instruction P; an attack succeeds only when the model output contains only the content required by the malicious instruction (merely disobeying the legitimate instruction does not count as success). An attack generally does not require query or weight access to the victim model, and the paper does not assume whether the attacker knows the legitimate instruction.
- LLM-integrated applications feed the developer prompt P and untrusted data D into the model through the same channel; an attacker can inject malicious instructions into the data to hijack the output. The authors argue that existing filtering- and detection-based defenses are insufficient to address this.
- Jatmo uses an instruction-tuned teacher model (GPT-3.5-Turbo) to generate input-output datasets for a task, then fine-tunes a non-instruction-tuned base model (davinci-002) to obtain a model that performs only a single task; when no ready-made data is available, it uses GPT-4 to synthesize a dataset in three steps: seed generation, input generation, and formatting.
- Across seven tasks, Jatmo's quality differs from GPT-3.5-Turbo by no more than 2%, and the best injection attack success rate drops from 87% for GPT-3.5-Turbo to 0.5% (2 successes out of 23400 injected inputs); with 1 real sample plus 800 synthetic samples, quality reaches 96% of the teacher model.
- The authors note: single-task models sacrifice generality, and how to build a general-purpose secure model remains unsolved; it only defends against prompt injection, not jailbreaks or adversarial examples; more complex attacks may exist that were not considered. Experiments were conducted only on OpenAI's davinci-002, with an attack set of 13 injections multiplied by 3 positions.
- Evaluated models
- davinci-002
- Benchmarks
- The StackIMDBAmazon ReviewsGutenbergCNN/DMJigsawSTSHackAPrompt
- Metrics
- prompt-injection success ratequality vs GPT-3.5(GPT-3.5-Turbo 0–100 评分)accuracy
Researchers from UC Berkeley, KACST and other institutions propose Jatmo, which uses task-specific fine-tuning to make base models without instruction tuning resist prompt injection, reducing the success rate of the best attack from 87% for GPT-3.5-Turbo to less than 0.5% on 7 tasks. The approach first uses an instruction-tuned teacher model to generate input-output pairs according to a fixed task prompt, then fine-tunes the base model davinci-002 so that the model undertakes only a single task and no longer identifies instructions from the data; when data is lacking, GPT-4 can be used to synthesize a training set from one real example or even zero examples. The task-specific models' quality on their respective tasks is within 2% of the teacher model, 400 real samples can achieve 98% of the teacher model's quality, and one real example plus 800 synthetic samples can reach 96%; among 23,400 injection tests, only 2 succeeded. The authors state that this is, to their knowledge, the first effective prompt injection defense, but each task prompt requires training a separate model, and upfront fine-tuning has costs, so it is not suitable for interactive chat, and the method also does not target jailbreak or alignment-type attacks.
Why it was selectedOn 7 tasks, the researchers reduced the prompt injection success rate from 87% for GPT-3.5-Turbo to under 0.5%, at the cost of fine-tuning a separate model for each task.
Paper analysis
What problem does this paper attempt to solve?
How to defend against prompt injection attacks on LLM-integrated applications; the authors state that no effective defense previously existed.
Core question: how to defend against prompt injection attacks on LLM-integrated applications, so that the application's output is not hijacked when processing untrusted data.
- Scenario and importance: the application calls the LLM in the form P + D, where P is a fixed prompt written by the developer and D is data that at runtime may come from untrusted sources; malicious data can override P and control the model output. OWASP lists prompt injection as the top threat to LLM applications (the paper cites [41]).
- Shortcomings of existing methods: the authors argue input sanitization can be bypassed by multilingual, encoding, paraphrasing, and other means, and there is no reliable escaping method; output validation is infeasible for free-form outputs, and validating with another LLM may itself be injected; the authors state that no effective defense previously existed.
- Core observation: the authors attribute prompt injection to two points—control and data go through the same channel, and instruction tuning teaches the model to find and execute instructions in the input; therefore defenses should avoid mixing channels and use non-instruction-tuned models when processing untrusted input.
- Proposed approach: the Jatmo framework, which uses a teacher model to generate a dataset for a task and fine-tunes a non-instruction-tuned base model to obtain a single-task model F, and the application calls F(D) instead of M(P + D); when no existing data is available, the dataset can be fully automatically synthesized.
- Threat model:
- Attacker goal: make the model execute malicious instructions instead of the legitimate instruction (evaluation focuses on goal hijacking; the authors state Jatmo also defends against prompt leaking).
- Attacker capability: full control over the input data D, with no influence over the legitimate instruction P.
- Attacker knowledge: generally no query or weight access to the victim model is required; the paper does not assume whether the attacker knows the legitimate instruction.
- Victim system and success criterion: an LLM-integrated application that calls the LLM in the form P + D; success counts only when the model output contains only what the malicious instruction requires, and merely disobeying the legitimate instruction does not count.
What related research exists?
Related work covers prompt injection attacks, other LLM attacks and defenses, and traditional injection defense ideas; the authors state that previously there was no effective prompt injection defense.
Prompt injection attacks
- Perez and Ribeiro (2022)—proposed "Ignore previous prompt"-style attack techniques and distinguished two attack goals, goal hijacking and prompt leaking; this paper's threat model and attack taxonomy follow their framework.
- Greshake et al. (2023)—proposed indirect prompt injection, attacking external data read by LLM applications, and categorized threats including information gathering, fraud, intrusion, malware, content manipulation, and availability.
- Liu et al. (2023)—studied prompt injection attacks against LLM-integrated applications; Schulhoff et al. (2023)'s HackAPrompt competition and Toyer et al. (2023)'s Tensor Trust game collected large-scale attack data, and this paper's attack set is taken from HackAPrompt.
Other LLM attacks and defenses
- Attacks: jailbreak attacks (e.g., Chao et al. 2023, Wei et al. 2023), training data extraction (Carlini et al. 2021; Nasr et al. 2023), personal information leakage (Lukas et al.), task-level attacks that degrade model performance (e.g., PromptBench).
- Defenses: paraphrasing or re-tokenization, perplexity detection, LLM-based detection, randomized smoothing (SmoothLLM), in-context example defenses (Jain et al. 2023; Kumar et al. 2023; Wei et al. 2023, etc.). These mostly target jailbreaks or adversarial prompts, and the paper does not compare each of them with this work.
Traditional injection defense approaches (the three categories criticized in Section 3.4 of the paper)
- Input sanitization: the authors argue it can be bypassed by encoding, paraphrasing, payload splitting, etc., and lacks reliable escaping methods.
- Output validation: the authors argue free-form outputs are hard to validate, and syntactic correctness does not equal content correctness.
- Query parameterization (SQL prepared statements, OWASP): the authors argue its idea of separating control from data is secure but unsuitable for LLMs' flexible interfaces; Jatmo's design is inspired by it.
Baselines and benchmarks
- Baseline method: GPT-3.5-Turbo called directly in the P + D manner (i.e., the standard practice with no defense); the paper does not implement other defense baselines.
- Benchmarks/datasets: datasets for seven tasks (The Stack, IMDB, Amazon Reviews, Gutenberg, CNN/DM, Jigsaw, STS), with attacks from the HackAPrompt dataset plus 3 manually constructed injections per task.
The authors position this paper as (to their knowledge) an effective defense against prompt injection, differing from the existing approach of a general-purpose model plus peripheral filtering/detection: rather than adding protection to a general-purpose model, each task is replaced with a single-task model designed not to execute instructions in the input.
How does the paper address this problem?
Jatmo uses a teacher model to generate task data and fine-tunes a non-instruction-tuned base model, yielding a single-task model that does not execute instructions in the input.
Overall idea: since instruction tuning teaches a model to find and execute instructions in the input, use a base model that has not been instruction-tuned and fine-tune it only on a single task, making the model insensitive to malicious instructions in the input. The framework is named Jatmo ("Jack of all trades, master of one") and relies on a teacher model M, a base model B, and a task prompt P (Figure 1).
Problem setting
- Application scenario: an application calls an LLM with P + D, where P is fixed and trusted, and D is variable at runtime and may be untrusted; Jatmo changes the call to F(D), where F is a single-task model fine-tuned only for task P.
- Injection definition: an injected prompt = a deceptive phrase + a malicious instruction, which can be inserted at any position in the data; the paper evaluates the effect of injection at the beginning, middle, and end positions on the success rate.
Three-stage pipeline (Figure 1)
- Dataset collection: collect the set of inputs {Di} corresponding to the task; if a ready-made dataset exists, use it directly.
- Output generation: use the teacher model to generate outputs Ri = M(P + Di), obtaining input-output pairs {Di, Ri}; the teacher model is exposed only to benign inputs and not to attacker-controlled inputs, so its own susceptibility to injection does not affect security. If the original dataset has gold-standard outputs, they can be used directly.
- Fine-tuning: fine-tune the non-instruction-tuned base model B with {Di, Ri} to obtain F such that F(Di) = Ri; the authors argue that F was never trained to follow instructions in the input, and is therefore safe against malicious data.
Synthetic dataset generation (Figure 3, when no ready-made data is available)
- Seed generation: use GPT-4 to generate 10 inputs as seeds; when examples are available, provide the task description and examples; when no examples are available, provide only the task description.
- Input generation: repeatedly have GPT-4 generate new inputs based on the task description and a randomly sampled seed, generating N in total; sampling from seeds rather than fixed examples is, according to the authors, to balance structural consistency and diversity.
- Input formatting: unify inputs of varying formats into a consistent format; when no real examples are available, first have GPT-4 generate a formatting template and then apply it; when real examples are available, use the examples directly as the template.
Implementation and evaluation method
- Implementation: the teacher model is GPT-3.5-Turbo, the base model is OpenAI's davinci-002, and synthetic data uses GPT-4; the main experiments fine-tune with no more than 400 samples per task. Appendix A.2 gives the complete prompts for each step.
- Success criterion: the injection requires the model to output a specific string, and the attack is counted as successful only when the model output contains only that string (spaces may be added); the authors note that this metric underestimates attack effectiveness, and any output containing other content is recorded as unsuccessful.
- Quality criterion: for classification tasks, accuracy is computed using dataset labels; for generation tasks, GPT-3.5-Turbo scores answer quality from 0–100.
What experiments did the paper conduct?
On seven tasks, Jatmo's quality differs from GPT-3.5-Turbo by no more than 2%, and the injection success rate drops from 87% to 0.5%.
Experimental setup
- Models tested: Jatmo models (OpenAI's non-instruction-tuned base model davinci-002 fine-tuned per task, one per task) and the GPT-3.5-Turbo baseline.
- Tasks and datasets: seven tasks (Table 1)—code summarization (The Stack), sentiment analysis (IMDB), review summarization (Amazon Reviews), translation (Gutenberg), news summarization (CNN/DM), toxicity detection (Jigsaw), sentence similarity (STS).
- Attack set: the most generic level 1 injections filtered from the HackAPrompt dataset, sorted by success rate against GPT-3.5-Turbo and taking the top 10 (shared across tasks), plus 3 manually constructed injections per task; each injection is placed at the beginning, end, or a random middle position of the input, for 39 variants per test sample.
- Metrics: injection success rate (success counted when the output contains only "HACKED", reported as the success rate of the most effective injection by task and position); for quality, accuracy for classification tasks, and 0–100 scores from GPT-3.5-Turbo for generation tasks.
- Fine-tuning setup: the main experiments use 400 real training inputs per task, with labels generated by GPT-3.5-Turbo (not gold labels, which the authors say is to avoid overestimating Jatmo's quality); synthetic data experiments generate 1000 inputs, with at most 800 for training, 100 for validation, and 100 for testing, plus 100 real inputs for testing.
- Number of repetitions: not reported in the paper.
Main results
According to Table 2 (each cell is the success rate of the most effective injection; the quality column is the gap versus GPT-3.5-Turbo):
Swipe horizontally to read the table
Task Quality vs GPT-3.5 GPT-3.5 success rate (beginning/middle/end) Jatmo success rate (beginning/middle/end) Code summarization 2% lower 98% / 12% / 96% 0% / 0% / 0% Sentiment analysis 2% lower 100% / 89% / 99% 0% / 0% / 0% Review summarization on par 98% / 93% / 100% 0% / 0% / 2% Translation 1% lower 100% / 52% / 100% 0% / 0% / 0% News summarization on par 99% / 71% / 100% 1% / 0% / 0% Toxicity detection on par 89% / 84% / 85% 0% / 0% / 0% Sentence similarity 1% lower 99% / – / 100% 0% / – / 0% - The authors state that Jatmo models' quality is within 2% of GPT-3.5-Turbo and that they are immune to nearly all injections; the only success case is end-of-input injection on the review summarization task, breaking 2% of test cases, where the injection is much longer than the rest of the input.
- GPT-3.5-Turbo's average success rate is 97% at the beginning and end positions and 67% at random middle positions (cross-task averages given in the body text).
- In the abstract and summary, the authors give overall figures: the best attack succeeds on average 0.5% against Jatmo versus 87% against GPT-3.5-Turbo; only 2 out of 23400 injected inputs break the Jatmo models.
Manual inspection and metric underestimation
- What was measured: the authors note the success rate metric underestimates attack effectiveness and manually inspected Jatmo's outputs under all 3900 injections on the news summarization task.
- Results: 4 partially successful cases were found—2 where the model first output "HACKED" and then completed the original task, and 2 where the output was meaningless but contained "HACKED".
- Authors' interpretation: these are recorded as unsuccessful by the metric, indicating the true attack effectiveness is slightly above 0%.
Training data scale and synthetic data
- Real data scale (Figure 4): three tasks reach teacher model quality at 400 samples; news summarization reaches it at 100 samples, and review summarization at 10 samples. The authors attribute the difference to task data diversity and differences in GPT-3.5 pretraining.
- Synthetic data (Figure 5, two settings T=0.7 and T=1, tested on real data): one-shot (1 real example) models are within 4% of GPT-3.5-Turbo quality on both tasks; zero-shot matches one-shot only on review summarization and translation. The authors state these models are immune to all injections.
- Authors' interpretation: zero-shot synthetic data is too generic in topic, and the real data format, style, and length of news summarization differ from synthetic samples, so the model overfits the synthetic distribution; 1 real example is enough to bring synthetic data close to the real distribution, but the required sample size is about 2 times that of real data.
Other ablations and analyses
- Temperature: T=0.7 yields higher quality than T=1; the translation task is unstable at T=1.0 and only achieves good results at low temperature (Section 5.4).
- Number of examples: code summarization and translation use 10 real examples to generate synthetic data, and models trained on 800 samples are 2% higher in quality than one-shot (Section 5.4).
- Overall quality figures: 98% of teacher quality with 400 real samples, and 96% with 1 real sample plus 800 synthetic samples (beginning of Section 5).
What are some points that can be further explored?
The authors list limitations such as single-task models sacrificing generality and only defending against prompt injection; experiments were conducted only on davinci-002.
Limitations and future directions noted by the authors
- Single-task sacrifices generality: each task (each unique prompt P) requires separately fine-tuning a model, with upfront fine-tuning cost, and is unsuitable for interactive chat applications where each prompt is used only once; how to build a general-purpose model secure against prompt injection remains unsolved (Introduction, Discussion).
- Limited defense scope: Jatmo only defends against prompt injection, not alignment-related jailbreak attacks or adversarial examples (Discussion).
- Possible unknown attacks: the authors state they made every effort to evaluate with currently known injection strategies, but more complex attacks may exist, and they welcome further safety evaluation (Discussion).
- Dependence on base model interfaces: the authors argue that fine-tuning instruction-tuned models cannot prevent injection, and call on LLM providers to continue offering fine-tuning interfaces for non-instruction-tuned base models (Discussion).
- Quality gap of zero-shot synthetic data: when zero-shot synthetic samples are too generic in topic and differ from the real distribution, the model overfits; on the news summarization task, zero-shot did not match one-shot (Section 5.4).
Scope of experiments
- The models tested are OpenAI's davinci-002 (fine-tuned into Jatmo models) and the GPT-3.5-Turbo baseline, 2 in total; synthetic data generation uses GPT-4.
- Evaluation covers 7 tasks, with an attack set of 13 injections (10 HackAPrompt + 3 manual) multiplied by 3 insertion positions, 39 variants per test sample.
- Synthetic data experiments cover the zero-shot and one-shot settings of 4 tasks including translation, review summarization, and news summarization.
- Manual inspection covers only 3900 injection outputs of one task, news summarization.
- The paper does not report the number of experimental repetitions, fine-tuning hyperparameters, or comparison experiments with filtering- and detection-based defense baselines.
Summarize the main content of the paper
Jatmo defends against prompt injection by task-level fine-tuning of a non-instruction-tuned model; the authors state that the success rate dropped from 87% to 0.5%.
A paper on prompt injection defense for LLM-integrated applications, proposing the Jatmo framework.
- Problem: when an application calls the LLM with P + D, malicious instructions in the untrusted data D can hijack the output; the authors attribute the cause to control and data sharing a channel and to instruction tuning making the model execute instructions in the input, and state that no effective defense previously existed.
- Method: use the teacher model GPT-3.5-Turbo to generate an input-output dataset for the task, fine-tune the non-instruction-tuned base model davinci-002 to obtain a single-task model F, and have the application call F(D) instead; when no existing data is available, use GPT-4 to synthesize a dataset in three steps—seed generation, input generation, formatting—where zero-shot needs only a task description and one-shot adds 1 real example.
- Main results: across seven tasks, Jatmo's quality differs from GPT-3.5-Turbo by no more than 2% (Table 2); the best injection attack succeeds on average 0.5% against Jatmo versus 87% against GPT-3.5-Turbo, with only 2 successes out of 23400 injected inputs.
- Data efficiency: with 400 real samples, quality reaches 98% of the teacher model; with 1 real sample plus 800 synthetic samples, 96%; news summarization matches teacher quality with 100 real samples, and review summarization with 10.
- Exceptions: end-of-input injection on the review summarization task succeeds 2% of the time; manual inspection of news summarization found 4/3900 partially successful cases, and the authors note the metric underestimates attack effectiveness.
- Authors' conclusion: the authors consider Jatmo a practical method for protecting LLM-integrated applications, with no additional inference overhead; limitations are that each task requires separate fine-tuning, sacrificing generality, it only defends against prompt injection, and more complex attacks that were not considered may exist.