Skip to main content
QUICK REVIEW

[论文解读] AutoSafeCoder: A Multi-Agent Framework for Securing LLM Code Generation through Static Analysis and Fuzz Testing

Ana Nunez, Nafis Tanveer Islam|arXiv (Cornell University)|Sep 16, 2024
Digital Rights Management and SecurityComputer Science被引用 3
一句话总结

AutoSafeCoder 是一个基于多智能体的框架,通过集成使用 GPT-4o 的编码智能体、静态分析智能体和模糊测试智能体,实现迭代式代码生成、漏洞检测与动态测试,从而提升 LLM 生成代码的安全性。与基线 LLM 相比,该框架将代码漏洞减少了 13%,同时保持了较高的功能性,证明了在协作式 LLM 驱动流水线中结合静态与动态分析的有效性。

ABSTRACT

Recent advancements in automatic code generation using large language models (LLMs) have brought us closer to fully automated secure software development. However, existing approaches often rely on a single agent for code generation, which struggles to produce secure, vulnerability-free code. Traditional program synthesis with LLMs has primarily focused on functional correctness, often neglecting critical dynamic security implications that happen during runtime. To address these challenges, we propose AutoSafeCoder, a multi-agent framework that leverages LLM-driven agents for code generation, vulnerability analysis, and security enhancement through continuous collaboration. The framework consists of three agents: a Coding Agent responsible for code generation, a Static Analyzer Agent identifying vulnerabilities, and a Fuzzing Agent performing dynamic testing using a mutation-based fuzzing approach to detect runtime errors. Our contribution focuses on ensuring the safety of multi-agent code generation by integrating dynamic and static testing in an iterative process during code generation by LLM that improves security. Experiments using the SecurityEval dataset demonstrate a 13% reduction in code vulnerabilities compared to baseline LLMs, with no compromise in functionality.

研究动机与目标

  • 为应对现有模型中 LLM 生成代码的安全漏洞比例常超过 40% 的日益增长的风险。
  • 克服单智能体 LLM 代码生成在功能与运行时安全之间权衡时,过度优先考虑功能性而忽视安全性的局限。
  • 开发一种协作式多智能体系统,整合静态分析与基于变异的模糊测试,实现实时漏洞检测与修复。
  • 在显著提升代码安全性的同时,保持高功能性正确性,实现自动化代码生成中的安全增强。
  • 提供一种可扩展的、由 LLM 驱动的框架,以增强对 AI 辅助软件开发的信任。

提出的方法

  • 该框架采用三个专用的 LLM 驱动智能体:编码智能体负责代码生成,静态分析智能体使用 Bandit 工具通过基于抽象语法树(AST)的分析检测漏洞,模糊测试智能体则采用基于变异的模糊测试检测运行时错误。
  • 建立反馈循环,使静态分析智能体与模糊测试智能体根据检测到的问题,迭代地优化编码智能体生成的代码。
  • 应用 few-shot 和上下文学习技术,使智能体能够有效理解并响应安全模式与反馈信息。
  • 系统使用 GPT-4o 作为所有智能体的基础大语言模型,输入提示设计用于引导角色特异性行为,提升准确性。
  • 通过自定义模糊测试器对输入进行变异,触发边界情况错误,利用崩溃报告指导代码修复。
  • 使用 HumanEval 基准验证功能正确性,安全性能则通过 SecurityEval 数据集与 Bandit 工具进行衡量。
Figure 1: Overview of our multi-agent framework integrating three agents: i) Coding Agent, ii) Static Analyzer Agent, and iii) Fuzzing Agent. The process begins with the Coding Agent generating code from code requirements. The Static Analyzer Agent performs code auditing. The Fuzzing Agent then muta
Figure 1: Overview of our multi-agent framework integrating three agents: i) Coding Agent, ii) Static Analyzer Agent, and iii) Fuzzing Agent. The process begins with the Coding Agent generating code from code requirements. The Static Analyzer Agent performs code auditing. The Fuzzing Agent then muta

实验结果

研究问题

  • RQ1与单智能体基线相比,多智能体 LLM 框架是否能够减少 LLM 生成代码中的漏洞数量?
  • RQ2静态分析与动态分析智能体在协作中能在多大程度上提升代码安全性,同时不降低功能性正确性?
  • RQ3将基于变异的模糊测试与 LLM 驱动的静态分析相结合,在检测运行时漏洞方面有多高效?
  • RQ4分析智能体提供的迭代反馈对漏洞数量与代码修复成功率有何影响?
  • RQ5该框架是否能在显著减少生成代码中的安全缺陷的同时,保持高水平的功能性能?

主要发现

  • 与使用 GPT-4o 时的 59 个漏洞样本(121 个样本中占 49%)相比,AutoSafeCoder 将漏洞样本减少至 44 个(占 121 个样本的 36%),漏洞减少比例达 13%。
  • 静态分析智能体在 1 至 4 次迭代内成功修复了 53% 的漏洞样本,其中 19% 的样本无需迭代即可修复,31% 的样本在反馈后仍无法修复。
  • 模糊测试智能体处理了 65 个代码样本,成功检测并修复了 5 个崩溃(占总样本的 4%),其余 60 个样本未发生崩溃。
  • 功能正确性得以保持,AutoSafeCoder 的 Pass@1 得分为 0.8780,相较于 GPT-4o 的 0.9085,仅下降约 3%。
  • 在剩余的 44 个漏洞样本中,CWE-94 是最常出现的漏洞类型,共出现 25 次,凸显其作为未来 LLM 微调重点目标的重要性。
  • 该系统表明,在多智能体循环中结合静态与动态分析,能显著提升安全结果,同时不损害代码可用性。
Figure 2: Prompt template used by coding agent for code generation.
Figure 2: Prompt template used by coding agent for code generation.

阅读本论文

在 Nubint 阅读器中打开原文,边读边就任意段落提问。

在 Nubint 中阅读本论文

本解读由 AI 根据论文原文生成,引用前请核对原文。