模型懂漏洞却仍写出不安全代码,原因在最后层格式竞争。
Surgical Repair of Insecure Code Generation in LLMs
- 发现安全知识藏在早期层,但被格式要求压制
- 用修复向量可减少74%不安全代码生成
- 方法通用,适合所有主流大模型和漏洞类型
大型语言模型生成生产级代码,却频繁引入已知漏洞。我们发现这并非知识不足:同一模型在被直接询问时能正确识别并解释漏洞,这种现象称为‘格式-可靠性差距’。机制分析显示,安全表征虽在早期层编码,但在最终层前保持计算惰性,仅在格式合规需求与之竞争时失效。由于问题集中于单一层,针对特定漏洞的引导向量可将不安全生成降低高达74%,且开销极小。该机制与修复方案在五种模型、三种架构族及六类漏洞中均有效,表明不安全代码生成本质是可解释性问题,而非训练缺陷。
原文摘要 · Abstract (English)
Large language models write production code, and yet they routinely introduce well-known vulnerabilities. We show that this is not a knowledge deficit: the same models that generate insecure code, correctly identify and explain the vulnerability when asked directly, this is a gap we call the Format-Reliability Gap. Mechanistic analysis reveals the cause: security representations are encoded from the earliest layers but remain computationally inert until the final layer, where format-compliance demands compete with them. Because the failure is localized to a single layer, per-vulnerability steering vectors reduce insecure generation by up to 74% with negligible overhead. The mechanism and the fix generalize across five models, three architecture families, and six vulnerability types, suggesting insecure code generation is an interpretability problem, not a training artifact.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。