arXiv:2605.24300cs.CRcs.AI2026-05被引 1

用引导式思维链提升大模型生成代码的安全可靠性

Enhancing Reliability in LLM-Based Secure Code Generation

  • 引入任务特异性漏洞缓解指引和语言感知防护机制
  • 安全缺陷减少57.6%(主数据集)至94.5%(验证集)
  • 特别适合关注代码安全的开发与审计人员

大型语言模型(LLMs)广泛用于代码生成,但其安全性在不同语言和提示策略下表现不一。现有提示工程主要提升功能正确性,却难以保证一致的安全结果。本文提出 extit{Mitigation-Aware Chain-of-Thought (MA-CoT)}框架,嵌入特定于漏洞类型(CWE)的缓解指导和语言感知防护,以减少生成代码中反复出现的漏洞。在包含200个任务的主数据集上,评估了gpt-5、claude-4.5、gemini-2.5三个模型,覆盖C、Java、Python三种语言,以及Vanilla、Zero-shot、CoT、MA-CoT四种提示策略,并通过外部验证集LLMSecEval进行测试。基于静态分析与专家验证,MA-CoT使主数据集上的总安全发现从92降至39(降低57.6%),在LLMSecEval上从73降至4(降低94.5%)。高危发现(Blocker + Critical)从90降至39(降幅56.7%),在验证集上从45降至2(降幅95.6%)。两种数据集均显示,只有MA-CoT能持续提升安全可靠性;Zero-shot与CoT表现不稳定,甚至可能增加风险,尤其在C语言中。进一步提出严格的漏洞成因分层归因(语言核心层与栈层),发现残余风险集中于加固相关模式(如操作系统与工具链依赖),提示需结合提示工程与安全原生构造设计。

原文摘要 · Abstract (English)

Large language models (LLMs) are widely used for code generation, but their security reliability remains inconsistent across languages and prompting strategies. Existing prompt engineering improves functional correctness but rarely ensures consistent security outcomes. We introduce the \textit{Mitigation-Aware Chain-of-Thought (MA-CoT)} framework, which embeds task-specific CWE mitigation guidance and language-aware safeguards to reduce recurring vulnerabilities in generated code. We evaluate MA-CoT across three LLMs (gpt-5, claude-4.5, gemini-2.5), three programming languages (C, Java, Python), and four prompting strategies (Vanilla, Zero-shot, CoT, MA-CoT) on a 200-task primary dataset, with external validation on LLMSecEval. Using static analysis with expert validation, MA-CoT reduces total security findings from 92 to 39 (57.6\%) on the primary dataset and from 73 to 4 (94.5\%) on LLMSecEval. High-severity findings (Blocker + Critical) drop from 90 to 39 (56.7\%) and from 45 to 2 (95.6\%), respectively. Across both datasets, MA-CoT is the only strategy that consistently improves security reliability; Zero-shot and CoT are less reliable and may increase vulnerability, especially in C. We further introduce a strict layered attribution of vulnerability drivers (language-core vs. stack layers) and show that residual risk concentrates in hardening-oriented patterns (e.g., OS- and toolchain-dependent), motivating secure-by-construction primitives alongside prompting.

代码安全大模型漏洞缓解提示工程

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。