Constrained Decoding for Secure Code Generation

📄 arXiv: 2405.00218v3 📥 PDF

作者: Yanjun Fu, Ethan Baker, Yu Ding, Yizheng Chen

分类: cs.CR, cs.AI, cs.LG, cs.SE

发布日期: 2024-04-30 (更新: 2024-07-20)

备注: 17 pages, 9 figures, our website is available at https://codeguardplus.github.io


💡 一句话要点

提出约束解码以解决安全代码生成问题

🎯 匹配领域: 支柱九:具身大模型 (Embodied Foundation Models)

关键词: 代码生成 安全性 正确性 约束解码 大型语言模型 软件安全 自动化测试

📋 核心要点

  1. 现有方法主要关注生成安全代码,但忽视了代码的正确性,导致安全性评估不全面。
  2. 本文提出了约束解码技术,旨在同时提升生成代码的安全性和正确性,填补现有研究的空白。
  3. 实验结果表明,约束解码在提升Code LLMs的安全性方面表现优于前缀调优,并且在八个最先进的Code LLMs上均取得了良好效果。

📝 摘要(中文)

代码大型语言模型(Code LLMs)在提升开发者生产力方面越来越受到重视,但它们常常生成存在漏洞的代码。因此,确保Code LLMs生成的代码既正确又安全显得尤为重要。以往的研究主要集中在生成安全代码上,忽视了安全代码的正确性,这可能导致虚假的安全感。本文引入了新的基准CodeGuard+及两个新指标,以评估Code LLMs生成安全且正确代码的能力。研究表明,当前最先进的防御技术前缀调优可能并不如预期强大,因为它生成安全代码但牺牲了功能正确性。此外,本文探索了一种新的防御方向:约束解码。我们的结果显示,约束解码在提升Code LLMs的安全性方面比前缀调优更有效,且无需专门的训练数据集。

🔬 方法详解

问题定义:本文旨在解决Code LLMs生成代码时安全性与正确性之间的矛盾。现有方法往往只关注安全性,忽视了代码的功能正确性,导致生成的代码可能存在潜在风险。

核心思路:论文提出的约束解码技术通过在生成过程中施加约束,确保生成的代码在安全性和正确性之间达到平衡。这种设计旨在克服以往方法的局限性,提供更可靠的代码生成方案。

技术框架:整体架构包括数据输入、约束条件设定、解码过程和结果输出四个主要模块。首先,输入代码生成的上下文信息,然后设定安全性和正确性的约束条件,接着通过约束解码生成符合条件的代码,最后输出结果并进行评估。

关键创新:最重要的技术创新点在于引入了约束解码这一新方法,它与现有的前缀调优方法本质上不同,前者在生成过程中考虑了安全性和正确性的双重约束,而后者则可能牺牲功能性以追求安全性。

关键设计:在约束解码中,设置了特定的安全性和正确性指标作为约束条件,并采用了适应性损失函数来优化生成过程。此外,网络结构上进行了调整,以便更好地适应约束条件的引入。通过这些设计,约束解码能够在不依赖专门训练数据集的情况下,显著提升生成代码的安全性。

🖼️ 关键图片

fig_0
fig_1
fig_2

📊 实验亮点

实验结果显示,约束解码在提升Code LLMs的安全性方面表现优于前缀调优,且在八个最先进的Code LLMs上均取得了显著的性能提升,特别是在安全性指标上,约束解码的效果超越了GPT-4。

🎯 应用场景

该研究的潜在应用领域包括软件开发、自动化测试和安全审计等。通过提高Code LLMs生成代码的安全性和正确性,能够有效降低软件漏洞风险,提升软件产品的整体质量和安全性,具有重要的实际价值和广泛的应用前景。

📄 摘要(原文)

Code Large Language Models (Code LLMs) have been increasingly used by developers to boost productivity, but they often generate vulnerable code. Thus, there is an urgent need to ensure that code generated by Code LLMs is correct and secure. Previous research has primarily focused on generating secure code, overlooking the fact that secure code also needs to be correct. This oversight can lead to a false sense of security. Currently, the community lacks a method to measure actual progress in this area, and we need solutions that address both security and correctness of code generation. This paper introduces a new benchmark, CodeGuard+, along with two new metrics, to measure Code LLMs' ability to generate both secure and correct code. Using our new evaluation methods, we show that the state-of-the-art defense technique, prefix tuning, may not be as strong as previously believed, since it generates secure code but sacrifices functional correctness. We also demonstrate that different decoding methods significantly affect the security of Code LLMs. Furthermore, we explore a new defense direction: constrained decoding for secure code generation. We propose new constrained decoding techniques to generate secure code. Our results reveal that constrained decoding is more effective than prefix tuning to improve the security of Code LLMs, without requiring a specialized training dataset. Moreover, our evaluations over eight state-of-the-art Code LLMs show that constrained decoding has strong performance to improve the security of Code LLMs, and our technique outperforms GPT-4.