用自动生成的安全代码训练模型,显著降低漏洞率。
HexaCoder: Secure Code Generation via Oracle-Guided Synthetic Training Data
- 通过漏洞修复生成成对的脆弱与修复代码,自动构建训练数据
- 相比基线方法,漏洞生成率降低最高达85%
- 适合需要安全编码的开发工具和自动化代码生成场景
大型语言模型(LLMs)在自动代码生成方面展现出巨大潜力,是GitHub Copilot等工具的基础。然而,近期研究指出,许多由LLM生成的代码包含严重安全漏洞。尽管已有工作尝试通过训练生成安全代码的模型来解决此问题,但受限于训练数据获取困难和数据准备成本高。本文提出HexaCoder,一种通过自动合成安全代码来增强LLM生成安全代码能力的新方法,从而减少寻找合适训练数据的努力。HexaCoder包含两个关键组件:基于安全检测器(oracle)的合成数据流水线和两步式安全代码生成流程。该流水线利用先进LLM对特定通用弱点枚举(CWE)类型的脆弱代码进行修复,生成脆弱代码与修复后代码的配对数据。安全检测器识别漏洞,先进LLM通过扩展或编辑代码进行修复,形成可用于低秩适配(LoRA)微调的数据集。每条微调样本均包含必要的安全相关库和代码,支撑我们新颖的两步生成方法:模型先集成安全库再生成主代码,使生成代码的漏洞数量相比基线方法最多减少85%。我们在三个不同基准上对四种LLM进行了广泛评估,结果表明HexaCoder不仅提升了生成代码的安全性,还保持了高水平的功能正确性。
原文摘要 · Abstract (English)
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.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。