A Lightweight Defense Against Code Poisoning Attacks Based on Code Naturalness

IF 6 1区 计算机科学 Q1 COMPUTER SCIENCE, SOFTWARE ENGINEERING
IEEE Transactions on Software Engineering Pub Date : 2026-10-01 Epub Date: 2026-06-18 DOI:10.1109/TSE.2026.3705037
Mengzhe Yuan;Weisong Sun;Yuchen Chen;Chunrong Fang;Zhenpeng Chen;Peizhuo Lv;Wenbo Guo;Yang Liu;Baowen Xu;Zhenyu Chen;Yanhui Li
{"title":"A Lightweight Defense Against Code Poisoning Attacks Based on Code Naturalness","authors":"Mengzhe Yuan;Weisong Sun;Yuchen Chen;Chunrong Fang;Zhenpeng Chen;Peizhuo Lv;Wenbo Guo;Yang Liu;Baowen Xu;Zhenyu Chen;Yanhui Li","doi":"10.1109/TSE.2026.3705037","DOIUrl":null,"url":null,"abstract":"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 <small>DetBadCode</small>. <small>DetBadCode</small> is designed based on our insight that code poisoning disrupts the naturalness of code. Specifically, <small>DetBadCode</small> first builds a code language model (CodeLM) on a lightweight <inline-formula><tex-math>${\\boldsymbol n}$</tex-math></inline-formula>-gram language model. Then, given poisoned data, <small>DetBadCode</small> 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. Besides, there might be clean tokens among the identified trigger tokens, so we introduce token-label distribution analysis to filter out tokens with relatively uniform label distribution. Finally, <small>DetBadCode</small> purifies the poisoned data by removing all poisoned samples containing the identified trigger tokens. Simultaneously, static program analysis is employed to further prevent over-removal. We conduct extensive experiments to evaluate the effectiveness and efficiency of <small>DetBadCode</small>, involving two types of advanced code poisoning attacks (a total of five poisoning strategies) and datasets from four representative code intelligence tasks. The experimental results demonstrate that across 20 code poisoning detection scenarios, <small>DetBadCode</small> achieves an average FPR of 8.30% and an average Recall of 100%, significantly outperforming four baselines. More importantly, <small>DetBadCode</small> is very efficient, with a minimum time consumption of only 5 minutes, and is 25 times faster than the best baseline on average.","PeriodicalId":13324,"journal":{"name":"IEEE Transactions on Software Engineering","volume":"52 10","pages":"2894-2912"},"PeriodicalIF":6.0000,"publicationDate":"2026-10-01","publicationTypes":"Journal Article","fieldsOfStudy":null,"isOpenAccess":false,"openAccessPdf":"","citationCount":"0","resultStr":null,"platform":"Semanticscholar","paperid":null,"PeriodicalName":"IEEE Transactions on Software Engineering","FirstCategoryId":"94","ListUrlMain":"https://ieeexplore.ieee.org/document/11570125/","RegionNum":1,"RegionCategory":"计算机科学","ArticlePicture":[],"TitleCN":null,"AbstractTextCN":null,"PMCID":null,"EPubDate":"2026/6/18 0:00:00","PubModel":"Epub","JCR":"Q1","JCRName":"COMPUTER SCIENCE, SOFTWARE ENGINEERING","Score":null,"Total":0}
引用次数: 0

Abstract

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 DetBadCode. DetBadCode is designed based on our insight that code poisoning disrupts the naturalness of code. Specifically, DetBadCode first builds a code language model (CodeLM) on a lightweight ${\boldsymbol n}$-gram language model. Then, given poisoned data, DetBadCode 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. Besides, there might be clean tokens among the identified trigger tokens, so we introduce token-label distribution analysis to filter out tokens with relatively uniform label distribution. Finally, DetBadCode purifies the poisoned data by removing all poisoned samples containing the identified trigger tokens. Simultaneously, static program analysis is employed to further prevent over-removal. We conduct extensive experiments to evaluate the effectiveness and efficiency of DetBadCode, involving two types of advanced code poisoning attacks (a total of five poisoning strategies) and datasets from four representative code intelligence tasks. The experimental results demonstrate that across 20 code poisoning detection scenarios, DetBadCode achieves an average FPR of 8.30% and an average Recall of 100%, significantly outperforming four baselines. More importantly, DetBadCode is very efficient, with a minimum time consumption of only 5 minutes, and is 25 times faster than the best baseline on average.
基于代码自然性的代码投毒攻击轻量级防御
神经代码模型(ncm)在代码智能任务中表现出了非凡的能力。与此同时,NCMs和基于NCMs的系统的安全性越来越受到关注。特别是,ncm通常是在来自可能不可信的来源的大规模数据上进行训练的,这为攻击者提供了通过在数据中插入精心制作的样本来操纵它们的机会。这种类型的攻击称为代码中毒攻击(也称为后门攻击)。它允许攻击者在ncm中植入后门,从而控制模型行为,这构成了重大的安全威胁。然而,目前还缺乏检测各种复杂代码投毒攻击的有效技术。在本文中,我们提出了一种创新的轻量级代码中毒检测技术,名为DetBadCode。DetBadCode的设计基于我们对代码中毒会破坏代码的自然性的认识。具体来说,DetBadCode首先在轻量级${\boldsymbol n}$-gram语言模型上构建代码语言模型(CodeLM)。然后,给定中毒数据,DetBadCode利用CodeLM识别(中毒)代码片段中的那些令牌,这些令牌将使代码片段在作为触发器令牌被删除后更自然。考虑到在单个样本中删除一些正常令牌也可能增强代码的自然性,从而导致高误报率(FPR),我们汇总了所有样本中每个令牌的累积改进。此外,在识别的触发令牌中可能存在干净的令牌,因此我们引入令牌-标签分布分析,过滤掉标签分布相对均匀的令牌。最后,DetBadCode通过删除包含已识别的触发器令牌的所有有毒样本来净化有毒数据。同时,采用静态程序分析进一步防止过度删除。我们进行了大量的实验来评估DetBadCode的有效性和效率,涉及两种类型的高级代码投毒攻击(总共五种投毒策略)和来自四个代表性代码情报任务的数据集。实验结果表明,在20个代码中毒检测场景中,DetBadCode的平均FPR为8.30%,平均召回率为100%,显著优于4个基线。更重要的是,DetBadCode非常高效,最小时间消耗仅为5分钟,并且平均比最佳基线快25倍。
本文章由计算机程序翻译,如有差异,请以英文原文为准。
求助全文
约1分钟内获得全文 求助全文
来源期刊
IEEE Transactions on Software Engineering
IEEE Transactions on Software Engineering 工程技术-工程:电子与电气
CiteScore
9.70
自引率
10.80%
发文量
724
审稿时长
6 months
期刊介绍: IEEE Transactions on Software Engineering seeks contributions comprising well-defined theoretical results and empirical studies with potential impacts on software construction, analysis, or management. The scope of this Transactions extends from fundamental mechanisms to the development of principles and their application in specific environments. Specific topic areas include: a) Development and maintenance methods and models: Techniques and principles for specifying, designing, and implementing software systems, encompassing notations and process models. b) Assessment methods: Software tests, validation, reliability models, test and diagnosis procedures, software redundancy, design for error control, and measurements and evaluation of process and product aspects. c) Software project management: Productivity factors, cost models, schedule and organizational issues, and standards. d) Tools and environments: Specific tools, integrated tool environments, associated architectures, databases, and parallel and distributed processing issues. e) System issues: Hardware-software trade-offs. f) State-of-the-art surveys: Syntheses and comprehensive reviews of the historical development within specific areas of interest.
×
引用
GB/T 7714-2015
复制
MLA
复制
APA
复制
导出至
BibTeX EndNote RefMan NoteFirst NoteExpress
×
提示
您的信息不完整,为了账户安全,请先补充。
现在去补充
×
提示
您因"违规操作"
具体请查看互助需知
我知道了
×
提示
确定
请完成安全验证×
copy
已复制链接
快去分享给好友吧!
我知道了
右上角分享
点击右上角分享
0
联系我们:info@booksci.cn Book学术提供免费学术资源搜索服务,方便国内外学者检索中英文文献。致力于提供最便捷和优质的服务体验。 Copyright © 2023 布克学术 All rights reserved.
京ICP备2023020795号-1
ghs 京公网安备 11010802042870号
Book学术文献互助
Book学术文献互助群
群 号:604180095
Book学术官方微信
小红书