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:
- 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.
- 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. - 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.
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.
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