arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2409.06446cs.CRcs.AIcs.CLcs.LGcs.SE
收藏

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

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

CISPA Helmholtz Center for Information Security(CISPA亥姆霍兹信息安全中心)

Hossein Hajipour, Lea Schönherr, Thorsten Holz, Mario Fritz

AI总结:

本文提出 HexaCoder,利用安全 oracle 引导自动合成脆弱/修复代码对并以 LoRA 微调,使 LLM 先引入安全库再生成代码,在四个模型和三个基准上最高减少 85% 漏洞且保持功能正确性。

摘要:

大语言模型(LLM)已在自动代码生成方面展现出巨大潜力,并构成 GitHub Copilot 等各类工具的基础。然而,近期研究强调,许多 LLM 生成的代码包含严重的安全漏洞。尽管以往工作试图通过训练可生成安全代码的模型来解决这一问题,但这些尝试仍受限于训练数据获取有限以及数据准备耗费大量人力。本文提出 HexaCoder,这是一种通过自动合成安全代码来增强 LLM 安全代码生成能力的新方法,可减少寻找合适训练数据的工作量。HexaCoder 包含两个关键组件:Oracle 引导的数据合成流水线,以及用于安全代码生成的两步流程。数据合成流水线利用一个最先进的 LLM 修复脆弱代码,为特定的通用弱点枚举(CWE)类型生成脆弱代码与修复后代码的配对。安全 oracle 识别漏洞,随后最先进的 LLM 通过扩展和/或编辑代码来修复这些漏洞,从而创建用于低秩适配(LoRA)方法微调的数据对。我们微调数据集中的每个样本都包含必要的安全相关库和代码,这些内容构成了新型两步生成方法的基础。这使模型能够在生成主代码之前集成与安全相关的库;与基线方法相比,显著减少了生成的脆弱代码数量,降幅最高达 85%。我们在三个不同基准上针对四个 LLM 进行了广泛评估,结果表明 HexaCoder 不仅提高了生成代码的安全性,还保持了较高水平的功能正确性。

英文摘要:

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.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑