RAG Poisoning: An Emerging Threat in AI Systems

Anya Kondamani
nFactor Technologies
5 min readJun 18, 2024

What is RAG?

Retrieval Augmented Generation (RAG) is a technique in natural language processing (NLP) that combines retrieval-based and generation-based methods to improve the performance and accuracy of language models, particularly in tasks such as question answering, information retrieval, and content generation. The first component is retrieval, this portion of the system searches a large corpus of documents or a knowledge base to find the most relevant pieces of information based on a given query or prompt. It typically employs algorithms and models designed for efficient information retrievals, such as BM25 or dense vector search using models like BERT. The second component is generation, in which a generative model, such as GPT-4, generates a coherent and contextually appropriate response or piece of text with retrieved information.

The typical RAG process takes on 4 steps:

1. Input: The user provides a query or prompt that needs to be answered or elaborated on.

2. Retrieval Step: The retrieval model searches a pre-defined corpus or knowledge base to find the most relevant documents or text passages related to the query.

3. Augmentation Step: The retrieved information is used to augment the input query. This can involve appending the retrieved text to the original query or integrating it into the context provided to the generative model by the user.

4. Generation Step: The augmented query is then fed into the generative model, which produces a response that incorporates both the original query and the retrieved information.

This process can be repeated as many times as needed to provide the necessary context for the model.

This cutting-edge technique enhances the capabilities of large language models (LLMs) by boosting accuracy with more contextually appropriate responses and increasing the system’s accessible knowledge base. While RAG significantly improves the relevance and accuracy of responses, it also introduces new security vulnerabilities, particularly through a method known as RAG poisoning.

What is RAG Poisoning?

RAG poisoning involves injecting malicious or misleading data into the knowledge databases that RAG systems rely on. This can cause the LLM to generate incorrect, biased, or harmful outputs. The attack leverages the synergy between LLMs and RAG to manipulate the model’s responses by altering the external data sources it uses.

Mechanisms of RAG Poisoning

1. Knowledge Injection: Attackers inject a few poisoned texts into the knowledge database. When the RAG system retrieves these texts, the LLM uses them as context, leading to the generation of attacker-chosen responses. For example, the PoisonedRAG method demonstrated a 90% attack success rate by injecting just five poisoned texts into a database with millions of entries [1].

2. Prompt Manipulation: The Pandora attack exploits prompt manipulation to conduct indirect jailbreak attacks on LLMs. By crafting malicious content that influences the RAG process, Pandora can generate unexpected and potentially harmful responses. This method has shown higher success rates than direct attacks, achieving 64.3% for GPT-3.5 and 34.8% for GPT-4 [3].

Real-Life Examples

1. PoisonedRAG: Researchers demonstrated the feasibility of RAG poisoning by injecting poisoned texts into a knowledge database used by RAG systems. The attack successfully manipulated the LLM to generate specific, incorrect answers for targeted questions. This highlights the vulnerability of RAG systems to even small-scale data poisoning [1][5].

2. Llama3 Incident: A notable real-life example involved the Llama3 model, where RAG poisoning was used to introduce racist content. This incident underscored the potential for RAG poisoning to cause significant reputational damage and highlighted the need for robust defenses against such attacks [4].

Mitigation Strategies

To protect against RAG poisoning, organizations can implement several robust data validation and sanitization processes:

  1. Statistical Outlier Detection: This approach uses statistical methods such as z-scoring to identify and remove data points that deviate significantly from the norm, which could indicate poisoning [1][3]. This is a relatively simple and computational approach but may miss more sophisticated attacks that do not produce any statistical anomalies.
  2. Anomaly Detection Algorithms: By employing machine learning algorithms to model normal data behavior, the model can flag anomalies that may be poisoned data [1][3]. This method may require significant computational overhead depending on the required complexity of the model. However, it acts as a robust defense, as these algorithms are far more powerful at detecting more inconspicuous anomalies.
  3. Data Provenance Tracking: Maintaining detailed records of data origins and transformations aids in verifying trustworthiness and tracing the source of any suspicious data [3]. This is a high cost and effort implementation, requiring comprehensive logging systems and other secure techniques. It also holds high reward, providing strong traceability and accountability.
  4. Adversarial Validation: Introduce adversarial examples during training to help models recognize and mitigate poisoning attempts [1][4]. This proves to be a moderate task as the computation must take place during the model’s training phase and requires expert knowledge and material on adversarial examples. If done correctly, this technique is extremely effective in enhancing the model’s robustness.
  5. Robust Loss Functions: Use loss functions that are less sensitive to outliers, making the model more resilient to poisoned data points [1]. Like the previous example, this method takes place in the training phase, implementing a function such as Huber loss. This technique is quite straightforward and can help reduce the impact by making the model less sensitive to outliers, however, it is not a completely secure solution.
  6. Secure Data Pipelines: Implement encryption, access controls, and rigorous authentication measures to prevent unauthorized data tampering [2]. This task involves implementing encryption, access controls, and authentication measures, which can be complex and require various resources. However, it is a powerful protection, eliminating data tampering at the source.

While the aforementioned protections may not be immediately implementable for all organizations, it is ideal to pursue all plausible measures. Initial implementations can be low-cost with high to moderate impact, such as statistical outlier detection and robust loss functions. After securing a primary level of defense, organizations can move on to more complex methods, and if needed consider advanced protections that offer maximum security, such as data provenance tracking.

RAG poisoning represents a significant threat to the integrity and reliability of AI systems. By understanding the mechanisms of these attacks and implementing robust data validation and sanitization processes, organizations can better protect their systems from such vulnerabilities. As the use of RAG continues to grow, so must the efforts to secure these systems against emerging threats.

References

[1] Zou, W., Geng, R., Wang, B., & Jia, J. (2024, February 12). PoisonedRAG: Knowledge Poisoning Attacks to Retrieval-Augmented Generation of Large Language Models. arXiv.org. https://arxiv.org/abs/2402.07867

[2] Security Risks with RAG Architectures. (n.d.). IronCore Labs. https://ironcorelabs.com/security-risks-rag/

[3] Deng, G., Liu, Y., Wang, K., Li, Y., Zhang, T., & Liu, Y. (2024, February 13). Pandora: Jailbreak GPTs by Retrieval Augmented Generation Poisoning. arXiv.org. https://arxiv.org/abs/2402.08416

[4] Mishra, N. (2024, May 30). Naman Mishra on LinkedIn: How RAG poisoning made Llama3 racist! https://www.linkedin.com/posts/naman-mishra-913009195_how-rag-poisoning-made-llama3-racist-activity-7202050779585601536-D3yH

[5] Sleeepeer. (n.d.). GitHub — sleeepeer/PoisonedRAG: Code & Data of PoisonedRAG paper. GitHub. https://github.com/sleeepeer/PoisonedRAG

--

--

Anya Kondamani
nFactor Technologies

Data Science at New York University I NYU Data Science Club