语法约束解码本为提高代码可靠性,却成恶意代码攻击新漏洞。
Grammar-Constrained Decoding Can Jailbreak LLMs into Generating Malicious Code

- 利用语法约束诱导大模型生成恶意代码,突破安全防线。
- 攻击成功率平均提升30个百分点以上,超过现有基线方法。
- 提出防护方案CodeShield,兼顾安全与正常功能可用性。
大型语言模型(LLMs)在代码生成中应用广泛,引发其被滥用于生成恶意代码的担忧。与此同时,语法约束解码(GCD)被普遍采用以提升生成代码的语法正确性。本文揭示了一个反直觉风险:这一旨在提升可靠性的技术本身可能成为攻击面。我们提出名为CodeSpear的新越狱攻击,利用GCD诱导LLMs生成恶意代码。实验表明,仅应用一个良性代码语法约束即可有效实现越狱。为此,我们提出CodeShield安全对齐方法,通过在代码模态中训练模型生成语义无害但结构多样的诱饵代码(honeypot code),在保持自然语言拒绝能力的同时,抵御攻击者操控的语法约束。在4个基准上对10个主流LLM的测试显示,CodeSpear平均将攻击成功率提升超30个百分点,而CodeShield在恢复安全性的同时仍保持良好实用性。
原文摘要 · Abstract (English)
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.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。