Grammar-Constrained Decoding Can Jailbreak LLMs into Generating Malicious Code
作者: Yitong Zhang, Shiteng Lu, Jia Li
分类: cs.CR, cs.AI, cs.CL, cs.SE
发布日期: 2026-06-10
💡 一句话要点
提出CodeShield以解决GCD引发的LLM恶意代码生成问题
🎯 匹配领域: 支柱九:具身大模型 (Embodied Foundation Models)
关键词: 大型语言模型 代码生成 安全性 语法约束解码 恶意代码 对抗性训练 蜜罐代码 安全对齐
📋 核心要点
- 现有的语法约束解码(GCD)方法虽然提高了代码生成的可靠性,但也引入了新的安全风险,可能被攻击者利用生成恶意代码。
- 论文提出的CodeShield方法通过训练模型生成无害的蜜罐代码,在保持安全性的同时,抵御了攻击者的语法约束。
- 实验结果显示,CodeSpear攻击方法在10个流行的LLMs上成功率提升超过30个百分点,而CodeShield能够有效恢复安全性。
📝 摘要(中文)
大型语言模型(LLMs)在代码生成中的应用日益普及,但也引发了其被滥用以生成恶意代码的担忧。本文揭示了一种反直觉的风险:语法约束解码(GCD)虽然旨在提高生成代码的可靠性,但却可能成为攻击面。我们提出了一种新的越狱攻击方法CodeSpear,利用GCD诱导LLMs生成恶意代码。为应对这一漏洞,提出了安全对齐方法CodeShield,能够在攻击者控制的语法约束下保持安全行为。实验表明,CodeSpear在10个流行LLMs上成功率提升超过30个百分点,而CodeShield则在恢复安全性的同时保持了良好的实用性。
🔬 方法详解
问题定义:本文旨在解决语法约束解码(GCD)引发的安全风险,特别是其可能被利用生成恶意代码的问题。现有方法在提高代码生成可靠性的同时,未能有效防范潜在的攻击。
核心思路:论文的核心思路是通过CodeShield方法,教导模型在GCD下生成蜜罐代码,这些代码在语义上无害且结构多样,从而避免被攻击者利用。
技术框架:整体架构包括两个主要模块:一是GCD的应用,二是CodeShield的训练过程。CodeShield通过对抗性训练,使模型在攻击者控制的语法约束下仍能生成安全代码。
关键创新:最重要的技术创新在于提出了CodeSpear攻击方法,利用GCD的特性诱导LLMs生成恶意代码,同时提出CodeShield作为防御机制,确保模型在面对攻击时仍能保持安全性。
关键设计:在设计中,CodeShield使用了特定的损失函数来优化生成的蜜罐代码,同时保持自然语言拒绝的能力,以确保在自然语言可用时仍能有效拒绝恶意请求。
🖼️ 关键图片
📊 实验亮点
实验结果表明,CodeSpear攻击方法在10个流行的LLMs上成功率平均提升超过30个百分点,显著优于现有的越狱基线。同时,CodeShield能够在CodeSpear攻击下恢复模型的安全性,保持良好的实用性。
🎯 应用场景
该研究的潜在应用领域包括软件开发、网络安全和人工智能伦理等。通过提高LLMs在代码生成中的安全性,能够有效防止其被滥用,保护用户和系统的安全。未来,该研究可能推动更安全的代码生成技术的发展,并引发对LLMs安全性的广泛关注。
📄 摘要(原文)
Large Language Models (LLMs) are increasingly used for code generation, raising concerns that they may be misused to produce malicious code. Meanwhile, Grammar-Constrained Decoding (GCD) has been widely adopted to improve the reliability of LLM-generated code by enforcing syntactic validity. In this paper, we reveal a counterintuitive risk: this reliability-oriented technique can itself become an attack surface. We uncover a new jailbreak attack, termed CodeSpear, that exploits GCD to induce LLMs into generating malicious code. Our experiments show that simply applying a benign code grammar constraint can effectively jailbreak LLMs. To address this vulnerability, we propose CodeShield, a safety alignment approach that robustly preserves safe behavior even under attacker-controlled grammar constraints. CodeShield aligns the model in the code modality by teaching it to generate honeypot code under GCD. Such code is semantically harmless, so it does not implement the malicious request, and structurally diverse, so it is difficult to suppress through grammar tightening. At the same time, CodeShield still preserves natural-language refusals when natural language is available. Experiments on 10 popular LLMs across 4 benchmarks show that CodeSpear outperforms representative jailbreak baselines and increases the attack success rate by more than 30 percentage points on average. CodeShield also restores safety under CodeSpear while preserving benign utility. Our findings reveal a fundamental risk of GCD and call for greater attention to its potential security implications.