arXivDaily arXiv每日学术速递 周一至周五更新
arXiv周末暂无论文更新,休息一下吧,周末愉快~~
arXiv 2604.03587cs.CRcs.AI
收藏

SecPI: 通过安全推理内化实现安全代码生成

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

University of California, Berkeley(加州大学伯克利分校);ETH Zurich(苏黎世联邦理工学院)

Hao Wang, Niels Mündler, Mark Vero, Jingxuan He, Dawn Song, Martin Vechev

AI总结:

SecPI通过内化结构化安全推理,使推理模型在无需安全指令的情况下生成安全代码,经验证在多个安全基准测试中显著提升代码功能正确性和安全性。

摘要:

推理语言模型(RLMs)在编程中被日益广泛应用。然而,即使是最先进的RLMs在生成代码时仍频繁引入关键安全漏洞。此前基于训练的可靠代码生成方法面临一个关键限制,即无法直接应用于RLMs:它们依赖于成本高昂且人工整理的安全数据集,仅涵盖有限的安全漏洞。在推理层面,通用的安全提示始终会降低功能正确性,同时仅触发浅层的随机漏洞分析。为了解决这些问题,我们提出了SecPI,一个微调流程,教导RLMs内化结构化安全推理,从而在无需任何安全指令的情况下生成安全代码。SecPI通过LLM基于分类器过滤现有通用编程数据集中的安全相关任务,使用教师模型生成高质量的安全推理轨迹,该教师模型受结构化提示引导,系统地列举相关CWEs和缓解措施,并在无安全提示和教师推理轨迹的输入对上微调目标模型——结果是模型学会自主进行安全推理,而非响应于明确指令。在安全基准测试上对最先进的开源权重推理模型的广泛评估验证了本方法的有效性。例如,SecPI在CWEval上将QwQ 32B的功能正确且安全生成的百分比从48.2%提高到62.2%(+14.0点),在BaxBench上从18.2%提高到22.0%。进一步研究还揭示了在训练漏洞之外的强大跨CWE和跨语言泛化能力。即使仅在注入相关的CWEs上训练,QwQ 32B在保留的内存安全CWEs上生成正确且安全代码的频率也提高了9.9%。

英文摘要:

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.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑