How Well Do Large Language Models Serve as End-to-End Secure Code Agents for Python?
作者: Jianian Gong, Nachuan Duan, Ziheng Tao, Zhaohui Gong, Yuan Yuan, Minlie Huang
分类: cs.SE, cs.AI
发布日期: 2024-08-20 (更新: 2025-06-13)
💡 一句话要点
评估大型语言模型作为端到端安全Python代码生成器的能力,并提出迭代修复工具。
🎯 匹配领域: 支柱九:具身大模型 (Embodied Foundation Models)
关键词: 大型语言模型 代码安全 漏洞修复 迭代修复 语义分析
📋 核心要点
- 现有大型语言模型在软件开发中被广泛应用,但其生成的代码安全性存在隐患,缺乏对安全风险的感知。
- 论文提出一种迭代修复方法,通过多次提示LLM并结合语义分析引擎,逐步改进代码安全性。
- 实验结果表明,该迭代修复工具能显著提高LLM修复不安全代码的成功率,最高可达85.5%。
📝 摘要(中文)
本文系统性地研究了大型语言模型(LLMs)生成安全代码的内在潜力。研究评估了GPT-3.5和GPT-4识别和修复由包括它们自身(GPT-3.5、GPT-4、Code Llama和CodeGeeX2)在内的四个流行LLM生成的代码中漏洞的能力。通过手动或自动审查4900段代码,研究表明:(1)大型语言模型缺乏对场景相关安全风险的意识,导致在SecurityEval基准测试中生成超过75%的易受攻击代码;(2)GPT-3.5和GPT-4等LLM无法准确识别它们生成的代码中的漏洞;(3)GPT-3.5和GPT-4在修复这四个LLM生成的不安全代码方面取得了33.2%~59.6%的成功率,但在修复自身生成的代码时表现不佳,表明存在自我修复的“盲点”。为了解决单轮修复的局限性,本文开发了一个轻量级工具,该工具提示LLM通过基于研究洞察的迭代修复程序来构建更安全的源代码。实验表明,在语义分析引擎的辅助下,该工具显著提高了修复的成功率,达到65.9%~85.5%。
🔬 方法详解
问题定义:当前大型语言模型(LLMs)在代码生成方面取得了显著进展,但它们生成的代码往往存在安全漏洞,缺乏对潜在安全风险的意识。现有方法难以保证LLM生成代码的安全性,并且LLM自身也难以有效识别和修复自身产生的漏洞。
核心思路:本文的核心思路是通过迭代修复的方式,逐步提升LLM生成代码的安全性。通过多次提示LLM进行代码修复,并结合语义分析引擎的辅助,可以克服单轮修复的局限性,提高修复的成功率。这种方法旨在模拟人类开发者在代码审查和修复过程中的迭代改进过程。
技术框架:该研究的技术框架主要包括以下几个阶段:1) 使用四个流行的LLM(GPT-3.5、GPT-4、Code Llama和CodeGeeX2)生成Python代码;2) 使用SecurityEval基准测试评估生成的代码的安全性;3) 使用GPT-3.5和GPT-4尝试识别和修复生成的代码中的漏洞;4) 开发一个迭代修复工具,该工具通过多次提示LLM并结合语义分析引擎来构建更安全的源代码;5) 评估迭代修复工具的性能。
关键创新:该研究的关键创新在于提出了一个基于迭代修复的框架,用于提高LLM生成代码的安全性。与传统的单轮修复方法相比,该框架能够更有效地利用LLM的知识和推理能力,逐步改进代码的安全性。此外,结合语义分析引擎可以提供更准确的漏洞信息,从而提高修复的成功率。
关键设计:迭代修复工具的关键设计包括:1) 使用特定的提示语来引导LLM进行代码修复,例如提供漏洞描述和修复建议;2) 使用语义分析引擎来检测代码中的漏洞,并将检测结果反馈给LLM;3) 设置迭代次数和停止条件,以控制修复过程的复杂度和效率;4) 评估每次迭代后的代码安全性,并选择最佳的修复结果。
🖼️ 关键图片
📊 实验亮点
实验结果表明,LLM生成的代码存在较高的安全风险,超过75%的代码存在漏洞。GPT-3.5和GPT-4在修复其他LLM生成的代码时,成功率在33.2%~59.6%之间,但在修复自身生成的代码时表现较差。通过使用迭代修复工具,修复成功率显著提高到65.9%~85.5%。
🎯 应用场景
该研究成果可应用于软件开发的各个阶段,例如代码自动生成、代码审查和漏洞修复。通过集成到IDE或CI/CD流程中,可以帮助开发者更早地发现和修复代码中的安全漏洞,从而提高软件的整体安全性。此外,该研究还可以促进对LLM安全性的更深入理解,为开发更安全的LLM提供指导。
📄 摘要(原文)
The rapid advancement of large language models (LLMs) such as GPT-4 has revolutionized the landscape of software engineering, positioning these models at the core of modern development practices. As we anticipate these models to evolve into the primary and trustworthy tools used in software development, ensuring the security of the code they produce becomes paramount. How well can LLMs serve as end-to-end secure code producers? This paper presents a systematic investigation into LLMs' inherent potential to generate code with fewer vulnerabilities. Specifically, We studied GPT-3.5 and GPT-4's capability to identify and repair vulnerabilities in the code generated by four popular LLMs including themselves (GPT-3.5, GPT-4, Code Llama, and CodeGeeX2). By manually or automatically reviewing 4,900 pieces of code, our study reveals that: (1) large language models lack awareness of scenario-relevant security risks, which leads to the generation of over 75% vulnerable code on the SecurityEval benchmark; (2) LLMs such as GPT-3.5 and GPT-4 are unable to precisely identify vulnerabilities in the code they generated; (3) GPT-3.5 and GPT-4 can achieve 33.2%~59.6% success rates in repairing the insecure code produced by the 4 LLMs, but they both perform poorly when repairing self-produced code, indicating self-repair "blind spots". To address the limitation of a single round of repair, we developed a lightweight tool that prompts LLMs to construct safer source code through an iterative repair procedure based on the insights gained from our study. Experiments show that assisted by semantic analysis engines, our tool significantly improves the success rates of repair to 65.9%~85.5%.