arXiv:2606.16244cs.CRcs.AI2026-06

用大模型分析代码风险,激活隐藏安全知识,提升生成代码安全性。

Activating Latent Security Knowledge through LLM-Guided Risk Analysis for Secure Code Generation

论文配图:Activating Latent Security Knowledge through LLM-Guided Risk Analysis for Secure Code Generation
图 1 · 摘自论文原文
  • 通过大模型识别任务相关安全风险,生成精准提示词。
  • 不需微调模型,使代码生成安全率从33.9%提升至91.2%。
  • 适用于所有黑盒代码模型,适合安全开发与自动化测试场景。

大型语言模型虽在大量软件和安全语料上预训练,但仍常生成功能正确却含已知漏洞的代码。现有防御多将其视为知识缺失,通过特定微调或漏洞代码检索应对。我们提出,不安全生成源于任务相关安全知识未能激活,而非知识不存在。为此提出BRACE——一种推理时、无需训练的安全框架,通过风险条件激活黑箱代码模型中的安全知识。给定编码任务后,先由大模型(作为安全专家)识别相关安全风险,经轻量级标准库验证并转化为简洁的任务特定风险提示。再将提示输入目标代码模型,引导其在满足功能要求的同时应用自身安全知识。该框架无需目标模型微调、参数访问、隐藏状态、可变逻辑值、学习型检索器或代码示例库。我们在六种开源模型与六种前沿商用模型上评估,使用四个基准(CyberNative、HumanEval、CWEval、BaxBench),衡量功能正确性、安全性、功能-安全联合表现及项目级生成能力。结果显示,相比安全提示(Secure Prompt),BRACE使CyberNative上的安全代码率平均从33.9%提升至91.2%;在CWEval上,功能-安全性能从49.7%提升至69.6%。机制消融实验表明,效果依赖于选择任务相关风险。

原文摘要 · Abstract (English)

Large language models are pretrained on extensive software and security corpora, yet they frequently generate functionally correct code containing well-known vulnerabilities. Existing defenses commonly treat this behavior as a knowledge deficit, addressing it through model-specific fine-tuning or retrieval from large collections of vulnerability code examples. We argue that insecure generation can arise from a failure to activate task-relevant security knowledge, rather than from knowledge absence alone. We present BRACE, an inference-time, training-free security harness for risk-conditioned activation of security knowledge in black-box code generation. Given only a coding task, BRACE first utilizes an LLM (as a security expert) to identify task-relevant security risks. The predicted risk identifiers are validated against a lightweight canonical catalog and converted into concise, task-specific risk cues. BRACE then supplies these cues to the target code model, prompting it to apply its own secure-coding knowledge while satisfying the original functional requirements. The framework requires no target-model fine-tuning, parameter access, hidden states, mutable logits, learned retriever, or coding-example knowledge base. We evaluate BRACE on six open-weight models and six frontier commercial models with four benchmarks (CyberNative, HumanEval, CWEval, BaxBench), measuring functional correctness, security, joint functionality-security, and project-level generation. BRACE raises the average Safe Code Rate from 33.9% under Secure Prompt to 91.2% on CyberNative. It also improves CWEval Func-Sec performance from 49.7% to 69.6%. Mechanism-oriented ablations further show that the gains depend on selecting task-relevant risks.

代码安全LLM应用风险分析生成对抗

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