Certifying LLM Safety against Adversarial Prompting

Aounon Kumar, Chirag Agarwal, Suraj Srinivas, Soheil Feizi, Hima Lakkaraju

TL;DR

This paper introduces "erase-and-check," a novel framework that provides verifiable safety guarantees for Large Language Models (LLMs) against adversarial prompts. By systematically erasing tokens and checking the resulting subsequences with a safety filter, the method can certify that harmful prompts, even when subtly modified by attackers, are correctly identified as unsafe.

What Problem Does This Paper Address?

Large Language Models (LLMs) are increasingly powerful but also vulnerable to "jailbreak" attacks. These attacks involve crafting malicious prompts, often by adding seemingly innocuous tokens, to trick the LLM into generating harmful or undesirable content, bypassing its safety guardrails. Existing empirical defenses can be brittle and easily circumvented by newer, more sophisticated attacks. This paper addresses the critical need for robust defenses that offer provable safety guarantees, ensuring LLMs remain safe even when faced with unseen adversarial strategies.

Methods

The core of the paper is the erase-and-check framework. Given an input prompt, it systematically generates subsequences by erasing tokens one by one (or in small blocks for more complex attacks). Each generated subsequence, along with the original prompt, is then passed through a "safety filter" (an LLM or a text classifier trained to identify harmful content). If any of these checked sequences are flagged as harmful, the entire input prompt is classified as harmful.

This process provides a certified safety guarantee: if a prompt is truly harmful, and an attacker modifies it by adding a limited number of tokens (up to the "erase length" d), erase-and-check is guaranteed to detect it as harmful. The paper explores three attack modes:

  • Adversarial Suffix: Attacker appends tokens to the end of a prompt.
  • Adversarial Insertion: Attacker inserts tokens anywhere within the prompt.
  • Adversarial Infusion: Attacker inserts tokens at arbitrary positions, not necessarily contiguously.

To make the safety filter practical, the authors experiment with two implementations: using the pre-trained Llama 2 model directly as a filter, and fine-tuning a DistilBERT model as a dedicated safety classifier. They also propose three efficient empirical defenses (RandEC, GreedyEC, GradEC) that offer faster, though not certified, protection by sampling or greedily selecting subsequences to check.

Datasets

  • AdvBench dataset: Used for harmful prompts. This dataset, created by Zou et al. [7], is designed for generating adversarial sequences to bypass LLM safety.
  • ChatGPT-generated safe prompts: An equal number of safe prompts were generated using ChatGPT to balance the dataset.
  • Llama 2 and DistilBERT: Used as safety filters. DistilBERT was fine-tuned on the combined safe and harmful prompt dataset, including erased subsequences of safe prompts to improve its robustness.

Key Insights and Ideas

  • Certified Guarantees are Possible: The paper demonstrates that it's feasible to provide mathematical guarantees for LLM safety against a defined class of adversarial attacks, moving beyond purely empirical defenses.
  • Token Erasure as a Robustness Strategy: The core idea of systematically erasing tokens and checking the resulting fragments is surprisingly effective. It forces the adversarial modifications to be "visible" to the safety filter, even if they are designed to be subtle.
  • DistilBERT as a Fast, Effective Safety Filter: Fine-tuning a smaller model like DistilBERT as a safety classifier offers a significant speedup over using a large LLM like Llama 2 directly, while maintaining high accuracy and enabling certification for longer adversarial sequences.
  • Attack Mode Generality Matters: The complexity of defending against adversarial attacks increases significantly with their generality, with "infusion" being the hardest to counter due to the arbitrary placement of adversarial tokens.

Key Results

  • High Certified Accuracy on Harmful Prompts: The Llama 2-based erase-and-check achieved 92% certified accuracy on harmful prompts, while the DistilBERT-based version achieved 100% on its test set. These certified accuracies are independent of the attack algorithm or length (up to d).
  • Good Empirical Accuracy on Safe Prompts: The framework maintained high empirical accuracy on safe prompts, with Llama 2 achieving 97% and DistilBERT 98% in suffix mode.
  • Significant Speedup with DistilBERT: The DistilBERT implementation offered up to 20X speedup over Llama 2 in suffix mode and up to 40X in insertion mode, making it more practical.
  • Efficient Empirical Defenses: RandEC, GreedyEC, and GradEC demonstrated effective detection rates (e.g., RandEC detecting >90% of adversarial prompts with a 30% sampling ratio) with much lower computational cost.

Limitations and Future Work

  • Computational Cost: While DistilBERT improves efficiency, the erase-and-check procedure can still be computationally expensive, especially for the more general "infusion" attack mode and longer erase lengths.
  • Filter Imperfection: The safety filter itself is not perfect. Even with a highly accurate classifier, there's a small chance of misclassification, meaning the certified guarantee is a lower bound.
  • Defined Attack Size: The guarantees are for adversarial modifications up to a specific size (d). Attacks exceeding this size are not covered by the certificate.

Future work could focus on:

  • Improving the efficiency of erase-and-check by reducing safety filter evaluations.
  • Extending the framework to more general threat models.
  • Applying similar certification techniques to other critical LLM safety aspects like privacy and fairness.

Summary updated

Abstract

Large language models (LLMs) released for public use incorporate guardrails to ensure their output is safe, often referred to as "model alignment." An aligned language model should decline a user's request to produce harmful content. However, such safety measures are vulnerable to adversarial prompts, which contain maliciously designed token sequences to circumvent the model's safety guards and cause it to produce harmful content. In this work, we introduce erase-and-check, the first framework to defend against adversarial prompts with verifiable safety guarantees. We erase tokens individually and inspect the resulting subsequences using a safety filter. Our procedure labels the input prompt as harmful if any subsequences or the input prompt are detected as harmful by the filter. This guarantees that any adversarial modification of a harmful prompt up to a certain size is also labeled harmful. We defend against three attack modes: i) adversarial suffix, which appends an adversarial sequence at the end of the prompt; ii) adversarial insertion, where the adversarial sequence is inserted anywhere in the middle of the prompt; and iii) adversarial infusion, where adversarial tokens are inserted at arbitrary positions in the prompt, not necessarily as a contiguous block. Empirical results demonstrate that our technique obtains strong certified safety guarantees on harmful prompts while maintaining good performance on safe prompts. For example, against adversarial suffixes of length 20, it certifiably detects 93% of the harmful prompts and labels 94% of the safe prompts as safe using the open source language model Llama 2 as the safety filter.
BibTeX
@inproceedings{kumar2024certifying,
  author = {Aounon Kumar and Chirag Agarwal and Suraj Srinivas and Soheil Feizi and Hima Lakkaraju},
  title = {{Certifying LLM Safety against Adversarial Prompting}},
  booktitle = {COLM},
  year = {2024},
  url = {https://openreview.net/forum?id=9Ik05cycLq},
}
Figure