HexaCoder: Secure Code Generation via Oracle-Guided Synthetic Training Data

📄 arXiv: 2409.06446v1 📥 PDF

作者: Hossein Hajipour, Lea Schönherr, Thorsten Holz, Mario Fritz

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

发布日期: 2024-09-10

备注: 24 pages, 16 tables, 8 figures


💡 一句话要点

HexaCoder:通过Oracle引导的合成训练数据生成安全代码

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

关键词: 代码生成 安全漏洞 大型语言模型 合成数据 Oracle引导

📋 核心要点

  1. 现有代码生成LLM易产生安全漏洞,但缺乏充足的安全代码训练数据和高效的数据准备方法。
  2. HexaCoder通过Oracle引导的合成数据管道自动生成安全代码对,用于LLM的微调,提升安全性。
  3. 实验表明,HexaCoder显著降低了生成代码的漏洞数量,最高可达85%,同时保持了代码的功能正确性。

📝 摘要(中文)

大型语言模型(LLMs)在自动代码生成方面展现出巨大潜力,并成为GitHub Copilot等多种工具的基础。然而,最近的研究表明,许多LLM生成的代码包含严重的安全漏洞。虽然之前的工作试图通过训练模型来生成安全代码来解决这个问题,但这些尝试仍然受到有限的训练数据和劳动密集型数据准备的限制。本文介绍了HexaCoder,一种通过自动合成安全代码来增强LLM生成安全代码能力的新方法,从而减少了寻找合适训练数据的工作量。HexaCoder包含两个关键组件:一个oracle引导的数据合成管道和一个用于安全代码生成的两步过程。数据合成管道利用最先进的LLM修复易受攻击的代码,为特定的常见弱点枚举(CWE)类型生成易受攻击和修复的代码对。安全oracle识别漏洞,最先进的LLM通过扩展和/或编辑代码来修复它们,从而创建数据对,用于使用低秩适应(LoRA)方法进行微调。我们微调数据集的每个示例都包含必要的安全相关库和代码,这些库和代码构成了我们新颖的两步生成方法的基础。这使得模型能够在生成主代码之前集成安全相关的库,与基线方法相比,显著减少了高达85%的生成的易受攻击代码的数量。我们对四个LLM在三个不同基准上进行了广泛的评估,表明HexaCoder不仅提高了生成代码的安全性,而且保持了高水平的功能正确性。

🔬 方法详解

问题定义:现有的大型语言模型在代码生成方面表现出色,但生成的代码常常包含安全漏洞,例如常见的缓冲区溢出、SQL注入等。现有的安全代码生成方法面临训练数据不足和数据准备工作繁琐的问题,难以有效提升代码安全性。

核心思路:HexaCoder的核心思路是利用LLM自身的能力,结合安全Oracle,自动生成高质量的、包含漏洞修复信息的合成训练数据。通过在这些数据上微调LLM,使其能够更好地生成安全的代码。这种方法避免了人工标注大量安全代码的成本,并能针对特定类型的漏洞进行训练。

技术框架:HexaCoder包含两个主要组件:Oracle引导的数据合成管道和两步安全代码生成过程。数据合成管道首先利用LLM生成包含漏洞的代码,然后使用安全Oracle识别漏洞,再利用LLM修复漏洞,从而生成 vulnerable-fixed 代码对。这些代码对用于LoRA微调LLM。两步生成过程首先集成安全相关的库,然后再生成主代码。

关键创新:HexaCoder的关键创新在于其自动化的安全代码数据合成管道。该管道利用LLM和安全Oracle的协同作用,高效地生成高质量的训练数据,从而避免了人工标注的成本和局限性。此外,两步生成过程通过预先集成安全库,进一步提升了代码的安全性。

关键设计:数据合成管道使用LLM(具体使用哪个LLM在论文中进行了实验)生成初始的易受攻击代码。安全Oracle可以是静态分析工具或模糊测试工具,用于检测代码中的漏洞。修复漏洞的LLM也需要精心选择,并可能需要针对特定类型的漏洞进行微调。LoRA微调方法用于在保持LLM原有能力的同时,快速适应安全代码生成任务。两步生成过程的关键在于选择合适的安全库,并确保它们与生成的主代码兼容。

🖼️ 关键图片

fig_0
fig_1
fig_2

📊 实验亮点

实验结果表明,HexaCoder在三个不同的基准测试中,显著提高了生成代码的安全性。与基线方法相比,HexaCoder能够减少高达85%的生成的易受攻击代码的数量。此外,HexaCoder在提升安全性的同时,也保持了代码的功能正确性,没有引入额外的错误。

🎯 应用场景

HexaCoder可应用于各种软件开发场景,特别是对安全性要求较高的领域,如金融、医疗、物联网等。它可以作为代码生成工具的后端,辅助开发者编写更安全的代码,降低软件漏洞带来的风险。未来,该方法可以扩展到支持更多编程语言和漏洞类型,并集成到CI/CD流程中,实现自动化安全代码生成。

📄 摘要(原文)

Large language models (LLMs) have shown great potential for automatic code generation and form the basis for various tools such as GitHub Copilot. However, recent studies highlight that many LLM-generated code contains serious security vulnerabilities. While previous work tries to address this by training models that generate secure code, these attempts remain constrained by limited access to training data and labor-intensive data preparation. In this paper, we introduce HexaCoder, a novel approach to enhance the ability of LLMs to generate secure codes by automatically synthesizing secure codes, which reduces the effort of finding suitable training data. HexaCoder comprises two key components: an oracle-guided data synthesis pipeline and a two-step process for secure code generation. The data synthesis pipeline generates pairs of vulnerable and fixed codes for specific Common Weakness Enumeration (CWE) types by utilizing a state-of-the-art LLM for repairing vulnerable code. A security oracle identifies vulnerabilities, and a state-of-the-art LLM repairs them by extending and/or editing the codes, creating data pairs for fine-tuning using the Low-Rank Adaptation (LoRA) method. Each example of our fine-tuning dataset includes the necessary security-related libraries and code that form the basis of our novel two-step generation approach. This allows the model to integrate security-relevant libraries before generating the main code, significantly reducing the number of generated vulnerable codes by up to 85% compared to the baseline methods. We perform extensive evaluations on three different benchmarks for four LLMs, demonstrating that HexaCoder not only improves the security of the generated code but also maintains a high level of functional correctness.