AI生成代码有安全隐患,易出错且难修复。
Artificial-Intelligence Generated Code Considered Harmful: A Road Map for Secure and High-Quality Code Generation
- 对比人类与AI生成代码在安全性和质量上的差异
- AI代码错误隐蔽,常因缺少防御性编程导致崩溃或漏洞
- 反复修改后仍可能引入新问题,修复效果不稳定
使用大语言模型(LLM)生成代码已广受欢迎,但其安全影响尚不明确。本研究对比了人类编写代码与LLM生成代码在多种任务中的安全性和质量表现,涵盖数据结构、算法、加密函数及LeetCode题目。通过单元测试、模糊测试和静态分析评估安全性,关注复杂度与代码量衡量质量。发现LLM生成的代码常存在功能错误,尤其在复杂任务中,例如对SHA1算法的实现虽能编译却错误;即便功能正确,也缺乏防御性编程,更易引发缓冲区溢出、整数溢出等问题。模糊测试显示,相比人类代码,LLM生成代码更容易出现挂起和崩溃。质量方面,其代码更复杂、结构简陋,缺乏健壮性。进一步构建反馈循环,要求模型重写以修复问题(如malloc溢出、数组越界、空指针解引用),结果发现:虽部分修复成功,但某些重写版本反而引入新缺陷;甚至在原本无误的文件中,经提示后引入新问题。
原文摘要 · Abstract (English)
Generating code via a LLM (rather than writing code from scratch), has exploded in popularity. However, the security implications of LLM-generated code are still unknown. We performed a study that compared the security and quality of human-written code with that of LLM-generated code, for a wide range of programming tasks, including data structures, algorithms, cryptographic routines, and LeetCode questions. To assess code security we used unit testing, fuzzing, and static analysis. For code quality, we focused on complexity and size. We found that LLM can generate incorrect code that fails to implement the required functionality, especially for more complicated tasks; such errors can be subtle. For example, for the cryptographic algorithm SHA1, LLM generated an incorrect implementation that nevertheless compiles. In cases where its functionality was correct, we found that LLM-generated code is less secure, primarily due to the lack of defensive programming constructs, which invites a host of security issues such as buffer overflows or integer overflows. Fuzzing has revealed that LLM-generated code is more prone to hangs and crashes than human-written code. Quality-wise, we found that LLM generates bare-bones code that lacks defensive programming constructs, and is typically more complex (per line of code) compared to human-written code. Next, we constructed a feedback loop that asked the LLM to re-generate the code and eliminate the found issues (e.g., malloc overflow, array index out of bounds, null dereferences). We found that the LLM fails to eliminate such issues consistently: while succeeding in some cases, we found instances where the re-generated, supposedly more secure code, contains new issues; we also found that upon prompting, LLM can introduce issues in files that were issues-free before prompting.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。