Are AI-Generated Fixes Secure? Analyzing LLM and Agent Patches on SWE-bench

📄 arXiv: 2507.02976v2 📥 PDF

作者: Amirali Sajadi, Kostadin Damevski, Preetha Chatterjee

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

发布日期: 2025-06-30 (更新: 2025-07-24)


💡 一句话要点

分析LLM生成补丁的安全性以应对软件开发中的风险

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

关键词: 大型语言模型 软件安全 代码生成 漏洞检测 自动化开发 代理框架

📋 核心要点

  1. 现有研究主要集中在合成环境中,未能充分评估LLM生成代码在真实开发中的安全性。
  2. 本研究通过分析20,000多个问题,首次对LLM生成的补丁进行大规模安全分析,比较了不同生成方式的漏洞情况。
  3. 结果显示,LLM生成的补丁引入的漏洞数量显著高于开发者,且与代码上下文相关的因素对安全性有重要影响。

📝 摘要(中文)

随着大型语言模型(LLMs)及其代理框架在软件开发任务中的应用日益增多,尤其是在问题解决和程序修复方面,安全风险问题也逐渐显现。尽管已有研究指出LLM生成代码的安全隐患,但大多数评估集中在合成或孤立环境中,未能充分探讨其在真实开发环境中的安全性。本研究首次对LLM生成的补丁进行大规模安全分析,使用了来自SWE-bench数据集的20,000多个问题,评估了独立LLM(Llama 3.3)生成的补丁,并与开发者编写的补丁进行了比较。同时,我们还评估了三种顶尖代理框架(OpenHands、AutoCodeRover、HoneyComb)生成的补丁。研究发现,独立LLM生成的新漏洞数量几乎是开发者的9倍,且许多漏洞展现出独特的模式。代理工作流在赋予LLM更多自主权时也会生成大量漏洞,增加了误解项目上下文或任务要求的可能性。

🔬 方法详解

问题定义:本研究旨在解决LLM生成补丁在真实开发环境中的安全性问题。现有方法多集中于合成环境,未能揭示真实场景下的潜在风险。

核心思路:通过对20,000多个问题的分析,比较独立LLM与开发者生成补丁的安全性,评估不同代理框架的表现,以识别生成不安全代码的条件。

技术框架:研究分为几个主要模块:数据收集(SWE-bench数据集)、补丁生成(独立LLM与代理框架)、安全性评估(漏洞检测与比较分析)。

关键创新:首次进行大规模的LLM生成补丁安全性分析,揭示了独立LLM与开发者之间在漏洞生成上的显著差异,强调了上下文因素的重要性。

关键设计:研究中设置了多个评估标准,包括生成代码的行数、涉及文件数量,以及GitHub问题的具体信息缺失情况等,以分析其对漏洞生成的影响。

📊 实验亮点

实验结果显示,独立LLM生成的补丁引入的漏洞数量几乎是开发者的9倍,且代理框架在赋予LLM更多自主权时,漏洞数量显著增加。这些发现强调了上下文因素在代码安全性中的关键作用。

🎯 应用场景

该研究的结果对软件开发中的自动化工具具有重要的应用价值,尤其是在提升代码安全性和减少漏洞方面。未来,研究成果可用于开发更安全的代码生成工具,并为软件开发团队提供风险评估的指导。

📄 摘要(原文)

Large Language Models (LLMs) and their agentic frameworks are increasingly adopted to automate software development tasks such as issue resolution and program repair. While prior work has identified security risks in LLM-generated code, most evaluations have focused on synthetic or isolated settings, leaving open questions about the security of these systems in real-world development contexts. In this study, we present the first large-scale security analysis of LLM-generated patches using 20,000+ issues from the SWE-bench dataset. We evaluate patches produced by a standalone LLM (Llama 3.3) and compare them to developer-written patches. We also assess the security of patches generated by three top-performing agentic frameworks (OpenHands, AutoCodeRover, HoneyComb) on a subset of our data. Finally, we analyze a wide range of code, issue, and project-level factors to understand the conditions under which LLMs and agents are most likely to generate insecure code. Our findings reveal that the standalone LLM introduces nearly 9x more new vulnerabilities than developers, with many of these exhibiting unique patterns not found in developers' code. Agentic workflows also generate a significant number of vulnerabilities, particularly when granting LLMs more autonomy, potentially increasing the likelihood of misinterpreting project context or task requirements. We find that vulnerabilities are more likely to occur in LLM patches associated with a higher number of files, more lines of generated code, and GitHub issues that lack specific code snippets or information about the expected code behavior and steps to reproduce. These results suggest that contextual factors play a critical role in the security of the generated code and point toward the need for proactive risk assessment methods that account for both code and issue-level information to complement existing vulnerability detection tools.