DeepGuard:通过多层语义聚合实现安全代码生成
DeepGuard: Secure Code Generation via Multi-Layer Semantic Aggregation
AI总结:
DeepGuard通过多层语义聚合提升代码生成的安全性,实验表明其在安全性和功能性上均优于基线模型,有效检测并减少不安全模式。
摘要:
大型语言模型(LLMs)在代码生成中可能复制训练数据中的不安全模式。为缓解此问题,常见的安全加固策略是通过最终Transformer层的监督进行微调。然而,这种设计可能导致最终层瓶颈:漏洞判别信号可能分布在多个层中,接近输出表示时变得不明显。为此,我们进行了逐层线性探测,发现漏洞相关信号在中间到上层最易检测,但向最终层衰减。基于此观察,我们引入DeepGuard框架,通过注意力模块聚合多个上层的表示,生成专用的安全分析器,平衡安全增强与功能正确性,并支持轻量级推理时的引导策略。在五个代码LLM上的广泛实验表明,DeepGuard在安全且正确的生成率上平均比SVEN等强基线模型提高11.9%。同时,它在保持功能正确性的同时,对未见漏洞类型也表现出良好的泛化能力。我们的代码可在https://github.com/unknownhl/DeepGuard上公开。
英文摘要:
Large Language Models (LLMs) for code generation can replicate insecure patterns from their training data. To mitigate this, a common strategy for security hardening is to fine-tune models using supervision derived from the final transformer layer. However, this design may suffer from a final-layer bottleneck: vulnerability-discriminative cues can be distributed across layers and become less detectable near the output representations optimized for next-token prediction. To diagnose this issue, we perform layer-wise linear probing. We observe that vulnerability-related signals are most detectable in a band of intermediate-to-upper layers yet attenuate toward the final layers. Motivated by this observation, we introduce DeepGuard, a framework that leverages distributed security-relevant cues by aggregating representations from multiple upper layers via an attention-based module. The aggregated signal powers a dedicated security analyzer within a multi-objective training objective that balances security enhancement and functional correctness, and further supports a lightweight inference-time steering strategy. Extensive experiments across five code LLMs demonstrate that DeepGuard improves the secure-and-correct generation rate by an average of 11.9% over strong baselines such as SVEN. It also preserves functional correctness while exhibiting generalization to held-out vulnerability types. Our code is public at https://github.com/unknownhl/DeepGuard.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。