arXiv:2604.03587cs.CRcs.AI2026-04被引 1

让大模型自动生成安全代码,无需额外指令。

SecPI: Secure Code Generation with Reasoning Models via Security Reasoning Internalization

  • 通过内部化安全推理机制,使模型自主生成安全代码。
  • 在CWEval上将代码安全正确率提升14.0个百分点至62.2%。
  • 训练仅针对注入类漏洞,仍能泛化到内存安全等新漏洞。

推理语言模型(RLMs)在编程中应用日益广泛,但即便最先进的模型也常生成含严重安全漏洞的代码。现有基于训练的方法受限于昂贵且人工标注的安全数据集,覆盖漏洞类型有限。推理阶段的通用安全提醒会降低代码功能正确性,且仅触发浅层分析。为此,我们提出SecPI,一种微调流程,使RLMs内化结构化安全推理能力,从而默认生成安全代码,无需推理时添加安全指令。SecPI利用基于LLM的分类器筛选通用编码数据集中与安全相关的任务,由教师模型根据结构化提示系统枚举相关CWE及其缓解措施,生成高质量安全推理路径,并将目标模型在无安全提示输入与教师推理路径对上进行微调——使模型学会自主进行安全推理而非依赖显式指令。在多个安全基准上对主流开源推理模型的评估验证了该方法的有效性:例如,对于QwQ 32B,在CWEval上安全且功能正确的生成比例从48.2%提升至62.2%(+14.0点),在BaxBench上从18.2%提升至22.0%。进一步分析显示,模型具备强跨CWE与跨语言泛化能力:即使仅在注入类CWE上训练,QwQ 32B在未见的内存安全类CWE上仍能提高9.9%的生成正确率。

原文摘要 · Abstract (English)

Reasoning language models (RLMs) are increasingly used in programming. Yet, even state-of-the-art RLMs frequently introduce critical security vulnerabilities in generated code. Prior training-based approaches for secure code generation face a critical limitation that prevents their direct application to RLMs: they rely on costly, manually curated security datasets covering only a limited set of vulnerabilities. At the inference level, generic security reminders consistently degrade functional correctness while triggering only shallow ad-hoc vulnerability analysis. To address these problems, we present SecPI, a fine-tuning pipeline that teaches RLMs to internalize structured security reasoning, producing secure code by default without any security instructions at inference time. SecPI filters existing general-purpose coding datasets for security-relevant tasks using an LLM-based classifier, generates high-quality security reasoning traces with a teacher model guided by a structured prompt that systematically enumerates relevant CWEs and mitigations, and fine-tunes the target model on pairs of inputs with no security prompt and teacher reasoning traces -- as a result, the model learns to reason about security autonomously rather than in response to explicit instructions. An extensive evaluation on security benchmarks with state-of-the-art open-weight reasoning models validates the effectiveness of our approach. For instance, SecPI improves the percentage of functionally correct and secure generations for QwQ 32B from 48.2% to 62.2% (+14.0 points) on CWEval and from 18.2% to 22.0% on BaxBench. Further investigation also reveals strong cross-CWE and cross-language generalization beyond training vulnerabilities. Even when trained only on injection-related CWEs, QwQ 32B generates correct and secure code 9.9% more frequently on held-out memory-safety CWEs.

安全生成推理模型代码安全微调

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