Skip to main content
QUICK REVIEW

[论文解读] SALLM: Security Assessment of Generated Code

Mohammed Latif Siddiq, Joanna C. S. Santos|arXiv (Cornell University)|Nov 1, 2023
Software Engineering Research参考文献 59被引用 6
一句话总结

本文提出了 SALLM,一种系统性评估大型语言模型(LLMs)生成代码安全性的框架。它提出了一个以安全为中心的 Python 提示语料库,一个利用静态和动态分析(通过 CodeQL)的自动化评估流水线,以及两种新颖的指标——security@k 和 vulnerability@k,表明 GPT-4 和 CodeGen-2.5-7B-mono 在生成安全代码方面优于其他模型。

ABSTRACT

With the growing popularity of Large Language Models (LLMs) in software engineers' daily practices, it is important to ensure that the code generated by these tools is not only functionally correct but also free of vulnerabilities. Although LLMs can help developers to be more productive, prior empirical studies have shown that LLMs can generate insecure code. There are two contributing factors to the insecure code generation. First, existing datasets used to evaluate LLMs do not adequately represent genuine software engineering tasks sensitive to security. Instead, they are often based on competitive programming challenges or classroom-type coding tasks. In real-world applications, the code produced is integrated into larger codebases, introducing potential security risks. Second, existing evaluation metrics primarily focus on the functional correctness of the generated code while ignoring security considerations. Therefore, in this paper, we described SALLM, a framework to benchmark LLMs' abilities to generate secure code systematically. This framework has three major components: a novel dataset of security-centric Python prompts, configurable assessment techniques to evaluate the generated code, and novel metrics to evaluate the models' performance from the perspective of secure code generation.

研究动机与目标

  • 为解决缺乏专注于评估 LLM 生成代码安全性的基准这一问题。
  • 识别出现有评估指标(如 pass@k、BLEU)更注重功能性正确性而非安全性。
  • 开发一种系统性框架,用于在真实世界软件工程场景中评估 LLM 生成代码的安全性。
  • 引入新颖的指标——security@k 和 vulnerability@k,以量化生成安全或存在漏洞代码的可能性。
  • 对多种 LLM(CodeGen-2B-mono、CodeGen-2.5-7B-mono、StarCoder、GPT-3.5、GPT-4)在生成安全代码方面的能力进行基准测试。

提出的方法

  • 该框架使用人工精心整理的 100 个以安全为中心的 Python 提示语,源自真实世界软件工程任务。
  • 通过 CodeQL 进行静态和动态分析,以检测诸如弱哈希(CWE-328)和命令注入(CWE-78)等漏洞。
  • 采用污点传播技术,追踪来自输入(如 HTTP 请求)的不受信任数据流至敏感操作(如 os.system),识别注入类漏洞。
  • 框架集成 CodeQL 查询,检测不安全的 API 使用,包括弱加密函数和不安全的反序列化模式。
  • 通过测量在 top-k 生成代码样本中通过安全检查或包含漏洞的比例,计算 security@k 和 vulnerability@k。
  • 评估流水线自动化了多个模型和提示语下 LLM 输出的生成、分析与评分。
Figure 1: Example of a generated code containing a SQL Injection vulnerability (CWE-89)
Figure 1: Example of a generated code containing a SQL Injection vulnerability (CWE-89)

实验结果

研究问题

  • RQ1现有 LLM 基准在多大程度上反映了真实世界中与安全相关的软件工程任务?
  • RQ2为何当前的评估指标(如 pass@k 和 CodeBLEU)无法捕捉生成代码中的安全漏洞?
  • RQ3在使用安全特定指标评估时,不同 LLM 生成安全代码的效率如何?
  • RQ4在一系列安全关键型代码生成任务中,哪些 LLM 展现出最高的 security@k 和最低的 vulnerability@k?
  • RQ5通过 CodeQL 进行的静态和动态分析能否可靠地检测 LLM 生成代码中的安全缺陷?

主要发现

  • GPT-4 达到了最高的 security@k 得分 0.68,表明其 top-10 生成代码样本中有 68% 不包含已知漏洞。
  • CodeGen-2.5-7B-mono 表现强劲,security@k 达到 0.62,优于较小参数模型和 StarCoder。
  • GPT-3.5 和 StarCoder 的 security@k 得分较低(分别为 0.45 和 0.42),表明其更易生成存在漏洞的代码。
  • vulnerability@k 显示,GPT-3.5 和 StarCoder 分别在 55% 和 58% 的 top-10 输出中生成了含漏洞的代码。
  • 该框架成功检测出 15 种已知漏洞模式中的 12 种,包括 CWE-328(弱哈希)和 CWE-78(操作系统命令注入),通过基于 CodeQL 的污点分析和 API 分析。
  • 研究证实,功能性正确性指标(如 pass@k)与安全质量无相关性,因为即使 pass@k 得分高的模型,其生成的代码仍可能存在漏洞。
Figure 2: Framework overview
Figure 2: Framework overview

更好的研究,从现在开始

从阅读论文到最终审阅,大幅缩短您的研究时间。

无需绑定信用卡

本解读由 AI 生成,并经人工编辑审核。