CodeAttack: Revealing Safety Generalization Challenges of Large Language Models via Code Completion

Qibing Ren, Chang Gao, Jing Shao, Junchi Yan, Xin Tan, Wai Lam, Lizhuang Ma

Introduction

As Large Language Models (LLMs) become increasingly integrated into software development workflows, they are trained on vast repositories of source code. To mitigate potential misuse, developers employ alignment techniques like Reinforcement Learning from Human Feedback (RLHF) to ensure models adhere to safety guidelines. However, a critical vulnerability exists: safety alignment is predominantly conducted in natural language (e.g., English prose), while the model's capabilities extend deep into the domain of programming.

This paper identifies a mismatched generalization gap. While an LLM might refuse a direct harmful request in natural language—such as "write a tutorial on how to make a bomb"—it may fail to recognize the same malicious intent when framed as a code completion task. CodeAttack explores whether the rapid advancement of LLM coding capabilities has outpaced the development of corresponding safety guardrails.

Methodology

The researchers introduce CodeAttack, a framework designed to systematically reformulate text-based harmful queries into code completion tasks. The framework operates through three primary components:

  1. Input Encoding: The harmful query is decomposed and stored within common data structures like arrays, stacks, or queues. This increases the distributional distance from standard natural language prompts.
  2. Task Understanding: The prompt includes a decode() function, requiring the model to implement logic that reconstructs the original query. This shifts the model's operational mode from conversation to code completion.
  3. Output Specification: The model is instructed to provide its response by populating a specific structure, such as an output_list. By framing harmful content as elements to be appended to a list, the malicious intent is obscured within a benign programming context.
Overview of the CodeAttack framework, illustrating input encoding, task understanding, and output specification.

Experimental Setup

The framework was evaluated using AdvBench, a standard benchmark containing 520 harmful prompts across categories like hate speech, dangerous activities, and harassment. The study spanned multiple programming languages (Python, C++, Go) and tested various state-of-the-art LLMs, including GPT-4, Claude-2, and Llama-2.

Key Insights

  • The Distribution Gap as an Attack Vector: The effectiveness of the attack increases as the input deviates further from natural language. For instance, encoding prompts using a Stack (requiring reverse-order processing) proved significantly more effective at bypassing guardrails than simple string encoding.
  • The Capability Paradox: More advanced models with superior coding proficiency, such as GPT-4 and Claude-2, were often more vulnerable. Their high performance in completing complex coding tasks appears to override their safety filters.
  • Risks of Specialization: CodeLlama-70b, fine-tuned specifically for programming, exhibited a 93% attack success rate, compared to only 40% for its general-purpose counterpart, Llama-2-70b. This suggests that specialized code training may inadvertently erode safety alignment.
  • Programming Language Bias: Models demonstrated higher safety levels in Python compared to languages like Go, likely because Python is more prevalent in the limited safety training data that includes code.

Evaluation and Results

CodeAttack demonstrated a universal vulnerability across all tested models, achieving a success rate of over 80% in many configurations. Notably, it successfully bypassed the rigorous safety filters of Claude-2, raising the attack success rate from near 0% (baseline) to 84% using stack-based encoding.

Comparison of Attack Success Rates (ASR) across various LLMs and attack methods, highlighting CodeAttack's superior performance.

Existing defenses, such as OpenAI’s Moderation endpoint and input perturbation methods like SmoothLLM, proved largely ineffective. Because CodeAttack generates semantically coherent and logical code, it avoids the "gibberish" or adversarial token patterns that current moderation systems are tuned to detect.

Conclusion and Future Work

The findings reveal a significant "safety generalization gap" where alignment in natural language does not translate to the code domain. The authors suggest that future safety research must move beyond post-hoc moderation and focus on alignment algorithms that integrate safety directly into the model's code-processing logic during pre-training and fine-tuning. Further investigation is also needed into other non-natural language formats, such as mathematical notation, to determine if similar vulnerabilities exist.

Summary updated

BibTeX
@inproceedings{ren2024codeattack,
  author = {Qibing Ren and Chang Gao and Jing Shao and Junchi Yan and Xin Tan and Wai Lam and Lizhuang Ma},
  title = {{CodeAttack: Revealing Safety Generalization Challenges of Large Language Models via Code Completion}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2024},
  doi = {10.18653/v1/2024.findings-acl.679},
}
Figure