Although Large Language Models (LLMs) show promising solutions to automated code generation, they often produce insecure code that threatens software security. Current approaches (e.g., SafeCoder) to improve secure code generation are limited by small, imbalanced instruction-tuning datasets. In this work, we present Secure-Instruct, a novel pipeline that automatically synthesizes high-quality vulnerable and secure code examples and instruction-tunes LLMs to align task description and secure code generation abilities. We evaluate Secure-Instruct on four representative LLMs using two security-related benchmarks: our own CWEBench and the existing CWEval. CWEBench comprises 93 scenarios on 44 CWEs, all without overlap with Secure-Instruct's synthetic instruction-tuning dataset, while CWEval covers 31 CWEs with 119 manually verified security-critical tasks. We find that Secure-Instruct improves both security and functional correctness in code generation. On CWEBench, Secure-Instruct substantially improves secure code generation, giving a 28.5% increase on average in secure ratio over the pre-trained models and outperforms SafeCoder by 12.6%. On CWEval, Secure-Instruct achieves an increase of 157.3% for CodeLlama-7B and 46.4% for Mistral-7B in Func-Sec@1 over pretrained models, and significantly outperforms SafeCoder.


翻译:尽管大型语言模型(LLMs)在自动化代码生成方面展现出有前景的解决方案,但它们常常生成不安全的代码,从而威胁软件安全。当前改进安全代码生成的方法(例如SafeCoder)受限于规模小、不平衡的指令调优数据集。在本工作中,我们提出了Secure-Instruct,这是一种新颖的流水线,能够自动合成高质量的漏洞代码和安全代码示例,并通过指令调优使LLMs对齐任务描述与安全代码生成能力。我们使用两个安全相关基准测试(我们自建的CWEBench和现有的CWEval)在四个代表性LLMs上评估Secure-Instruct。CWEBench包含44个CWE的93个场景,所有场景均不与Secure-Instruct的合成指令调优数据集重叠;而CWEval涵盖31个CWE的119个经过人工验证的安全关键任务。我们发现,Secure-Instruct在代码生成中同时提升了安全性和功能正确性。在CWEBench上,Secure-Instruct显著改进了安全代码生成,相较于预训练模型,安全比率平均提升28.5%,并优于SafeCoder 12.6%。在CWEval上,Secure-Instruct在Func-Sec@1指标上,CodeLlama-7B提升了157.3%,Mistral-7B提升了46.4%,均显著优于预训练模型和SafeCoder。

0
下载
关闭预览

相关内容

代码(Code)是专知网的一个重要知识资料文档板块,旨在整理收录论文源代码、复现代码,经典工程代码等,便于用户查阅下载使用。
【NeurIPS2024】TableRAG:基于语言模型的百万标记表格理解
专知会员服务
37+阅读 · 2024年10月8日
《用于代码弱点识别的 LLVM 中间表示》CMU
专知会员服务
14+阅读 · 2022年12月12日
AAAI 2022 | ProtGNN:自解释图神经网络
专知
10+阅读 · 2022年2月28日
图机器学习 2.2-2.4 Properties of Networks, Random Graph
图与推荐
10+阅读 · 2020年3月28日
【NeurIPS2019】图变换网络:Graph Transformer Network
Single-Shot Object Detection with Enriched Semantics
统计学习与视觉计算组
14+阅读 · 2018年8月29日
国家自然科学基金
2+阅读 · 2016年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
46+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
VIP会员
相关VIP内容
相关资讯
AAAI 2022 | ProtGNN:自解释图神经网络
专知
10+阅读 · 2022年2月28日
图机器学习 2.2-2.4 Properties of Networks, Random Graph
图与推荐
10+阅读 · 2020年3月28日
【NeurIPS2019】图变换网络:Graph Transformer Network
Single-Shot Object Detection with Enriched Semantics
统计学习与视觉计算组
14+阅读 · 2018年8月29日
相关基金
国家自然科学基金
2+阅读 · 2016年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
46+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
Top
微信扫码咨询专知VIP会员