Large language models routinely generate code with exploitable security flaws. Prior literature attributes this limitation to a lack of security expertise, steering current defense mechanisms toward heavy fine-tuning or external knowledge retrieval, which introduces significant computational overhead and data bias through redundant code examples. Contrary to this view, we argue that pretraining corpora are already rich in security material. The bottleneck is activation: without an explicit and brief cue, statistical pressure toward common training-distribution patterns suppresses the model's safety-relevant representations. We present SPARK, an inference-time security harness that activates this latent knowledge without any retraining. The harness has two parts. Component~I retrieves a few of the relevant Common Weakness Enumeration (CWE) entries for each coding task and appends a short structured cue to the prompt; this alone is enough to surface the model's existing security representations. Component~II adds a precomputed token bias to the logits at every decoding step. We obtain the bias by projecting a safe-direction vector, the unit difference between the mean safe and mean unsafe last-layer hidden states, through the language model head. The bias is computed once offline; applying it costs a single vector addition per generated token. We evaluate SPARK on 9 open-source models across C++, Java, and Python, and compare with 7 baselines spanning fine-tuning and retrieval-augmented methods. SPARK matches or improves on the best baseline in every setting while preserving HumanEval utility. We further test Component~I in a black-box setting on 7 of today's strongest models, including Claude, DeepSeek, and GPT, demonstrating the bottleneck of insecure code generation and the improvements enabled by our method.


翻译:大型语言模型在生成代码时频繁出现可利用的安全漏洞。现有文献将这一局限归因于安全知识匮乏,促使当前防御机制转向大规模微调或外部知识检索——这类方法不仅引入显著计算开销,还因冗余代码示例导致数据偏差。与此相反,我们论证预训练语料库已蕴含丰富的安全材料,瓶颈在于激活机制:缺乏明确简洁的提示时,模型对常见训练分布模式的统计倾向会抑制其安全相关表征。我们提出SPARK——一种无需重训练即可激活隐式知识的安全框架。该框架包含两个组件。组件I为每项编码任务检索少量相关通用弱点枚举(CWE)条目,并在提示后附加简短结构化线索——仅此即可显现模型已有的安全表征。组件II在每次解码步骤向logits添加预计算token偏差。我们通过将安全方向向量(平均安全态与平均非安全态最后一层隐藏状态的单位差值)投影至语言模型头部获得该偏差。该偏差仅需离线计算一次,应用时每个生成token仅增加一次向量加法运算。我们在C++、Java、Python三种语言的9个开源模型上评估SPARK,并与涵盖微调与检索增强方法的7个基线进行对比。SPARK在所有场景中达到或超越最优基线,同时保持HumanEval实用性能。我们进一步在Claude、DeepSeek、GPT等7个当前最强模型的黑盒环境中测试组件I,验证了不安全代码生成的瓶颈本质及本方法带来的改进效果。

0
下载
关闭预览

相关内容

Apache Spark 是专为大规模数据处理而设计的快速通用的计算引擎。Spark是UC Berkeley AMP lab (加州大学伯克利分校的AMP实验室)所开源的类Hadoop MapReduce的通用并行框架,Spark,拥有Hadoop MapReduce所具有的优点;但不同于MapReduce的是Job中间输出结果可以保存在内存中,从而不再需要读写HDFS,因此Spark能更好地适用于数据挖掘与机器学习等需要迭代的MapReduce的算法。
大语言模型安全现状与挑战
专知会员服务
88+阅读 · 2024年1月14日
网络安全态势感知浅析
计算机与网络安全
18+阅读 · 2017年10月13日
国家自然科学基金
4+阅读 · 2017年12月31日
国家自然科学基金
18+阅读 · 2017年12月31日
国家自然科学基金
6+阅读 · 2017年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
19+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
31+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
4+阅读 · 2014年12月31日
VIP会员
最新内容
从采集到决策:美军视角下的战术情报范式重构
专知会员服务
0+阅读 · 今天2:42
《履带式无人地面战车技术发展现状》
专知会员服务
2+阅读 · 今天1:46
《无人机脆弱性利用:网络空间力量的新域》
专知会员服务
2+阅读 · 8月1日
美空军如何将人工智能从战场部署至后方机关
专知会员服务
11+阅读 · 7月31日
《史诗怒火行动:多域前瞻评估》49页报告
专知会员服务
7+阅读 · 7月31日
《英国防部:未来空战系统数字化战略》33页
专知会员服务
5+阅读 · 7月31日
《面向自主飞行网络的智能体人工智能架构》
专知会员服务
7+阅读 · 7月31日
相关VIP内容
大语言模型安全现状与挑战
专知会员服务
88+阅读 · 2024年1月14日
相关基金
国家自然科学基金
4+阅读 · 2017年12月31日
国家自然科学基金
18+阅读 · 2017年12月31日
国家自然科学基金
6+阅读 · 2017年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
19+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
31+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
4+阅读 · 2014年12月31日
Top
微信扫码咨询专知VIP会员