arXiv:2502.15830cs.SEcs.AI2025-02中稿 · the 47th Internati…被引 14

通过检测代码自然性异常,高效识别并清除代码投毒攻击。

Show Me Your Code! Kill Code Poisoning: A Lightweight Method Based on Code Naturalness

  • 基于代码自然性破坏原理,用轻量n-gram模型识别异常触发词。
  • 在多种攻击下检测准确率超基线,净化数据后任务性能显著提升。
  • 仅需5分钟,速度比最优基线快25倍,适合实际系统部署。

神经代码模型(NCMs)在代码智能任务中表现卓越,但其安全性日益受关注。由于训练数据常来自不可信源,攻击者可通过注入精心设计的样本实施代码投毒攻击(又称后门攻击),植入隐蔽后门以操控模型行为。现有方法难以应对复杂投毒模式。本文提出轻量级检测方法KillBadCode,核心思想是:代码投毒会破坏代码的自然性。首先构建基于轻量n-gram的语言模型(CodeLM),对污染数据进行分析,识别出删除后能提升代码自然性的可疑标记(触发词)。为降低误报,聚合所有样本中各标记的累积改善值。最终通过移除包含这些触发词的样本实现数据净化。在两种投毒攻击和四个代码任务上的实验表明,该方法显著优于四种基线;且效率极高,最快仅需5分钟,平均速度为最佳基线的25倍。

原文摘要 · Abstract (English)

Neural code models (NCMs) have demonstrated extraordinary capabilities in code intelligence tasks. Meanwhile, the security of NCMs and NCMs-based systems has garnered increasing attention. In particular, NCMs are often trained on large-scale data from potentially untrustworthy sources, providing attackers with the opportunity to manipulate them by inserting crafted samples into the data. This type of attack is called a code poisoning attack (also known as a backdoor attack). It allows attackers to implant backdoors in NCMs and thus control model behavior, which poses a significant security threat. However, there is still a lack of effective techniques for detecting various complex code poisoning attacks. In this paper, we propose an innovative and lightweight technique for code poisoning detection named KillBadCode. KillBadCode is designed based on our insight that code poisoning disrupts the naturalness of code. Specifically, KillBadCode first builds a code language model (CodeLM) on a lightweight $n$-gram language model. Then, given poisoned data, KillBadCode utilizes CodeLM to identify those tokens in (poisoned) code snippets that will make the code snippets more natural after being deleted as trigger tokens. Considering that the removal of some normal tokens in a single sample might also enhance code naturalness, leading to a high false positive rate (FPR), we aggregate the cumulative improvement of each token across all samples. Finally, KillBadCode purifies the poisoned data by removing all poisoned samples containing the identified trigger tokens. The experimental results on two code poisoning attacks and four code intelligence tasks demonstrate that KillBadCode significantly outperforms four baselines. More importantly, KillBadCode is very efficient, with a minimum time consumption of only 5 minutes, and is 25 times faster than the best baseline on average.

代码安全投毒检测轻量模型

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