구문 제약 디코딩이 LLM을 우회하여 악성 코드를 생성하도록 유도할 수 있다
Grammar-Constrained Decoding Can Jailbreak LLMs into Generating Malicious Code
대규모 언어 모델(LLM)은 점점 더 많이 코드 생성을 위해 사용되고 있으며, 이로 인해 악성 코드를 생성하는 데 오용될 가능성에 대한 우려가 제기되고 있습니다. 동시에, 구문 제약 디코딩(GCD)은 LLM이 생성한 코드의 신뢰도를 높이기 위해 문법적 유효성을 강제하는 기술로 널리 채택되었습니다. 본 논문에서는 반직관적인 위험을 밝혀냅니다. 즉, 신뢰도 향상을 목표로 하는 이 기술 자체가 공격 표면이 될 수 있습니다. 우리는 GCD를 악용하여 LLM이 악성 코드를 생성하도록 유도하는 새로운 우회 공격인 'CodeSpear'를 발견했습니다. 실험 결과, 단순히 안전한 코드 문법 제약을 적용하는 것만으로도 LLM을 효과적으로 우회할 수 있음을 보여줍니다. 이러한 취약점을 해결하기 위해, 우리는 공격자가 제어하는 문법 제약 조건 하에서도 안전한 동작을 강력하게 유지하는 안전 정렬 접근 방식인 'CodeShield'를 제안합니다. CodeShield는 LLM에게 GCD 환경에서 함정 코드를 생성하도록 학습시켜 코드 모달리티에 대한 모델을 조정합니다. 이러한 코드는 의미적으로 무해하며 악성 요청을 구현하지 않고, 구조적으로 다양하여 문법 강화로 억제하기 어렵습니다. 동시에, CodeShield는 자연어 입력이 제공될 때 여전히 안전 거부를 유지합니다. 4개의 벤치마크에서 10개의 인기 LLM에 대한 실험 결과, CodeSpear가 대표적인 우회 공격 기법보다 성능이 뛰어나 평균적으로 30% 이상의 공격 성공률을 증가시킵니다. 또한, CodeShield는 CodeSpear 공격 하에서도 안전성을 회복하는 동시에 유용한 기능을 유지합니다. 우리의 연구 결과는 GCD의 근본적인 위험을 보여주며, 잠재적인 보안 영향을 고려하기 위한 노력이 필요함을 강조합니다.
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.
No Analysis Report Yet
This paper hasn't been analyzed by Gemini yet.
Log in to request an AI analysis.