TL;DR
CodeScan is the first black-box, vulnerability-oriented scanning framework specifically designed to detect data poisoning and backdoor attacks in code generation LLMs. By shifting the defense paradigm from token-level consistency to structural similarity (using Abstract Syntax Trees) and leveraging advanced LLMs for vulnerability analysis, CodeScan achieves over 97% detection accuracy while drastically reducing false positives.
What Problem Does This Paper Address?
As code generation models (like GitHub Copilot or Claude Code) become deeply integrated into developer workflows, they introduce a critical security vector: data poisoning and backdoor attacks. Attackers can inject a tiny fraction of malicious samples into a model's fine-tuning dataset. This trains the model to secretly generate insecure code containing vulnerabilities (like cross-site scripting) whenever a developer provides a specific trigger or context.
Existing backdoor defenses designed for natural language LLMs, such as BAIT, rely on token-level generation consistency to invert and catch attack targets. However, source code is fundamentally different from natural language; identical code semantics can be written using completely different variable names, spacing, or syntactic structures. This flexibility causes token-based defenses to miss actual attacks (false negatives) or mistakenly flag benign, highly predictable code idioms as backdoors (false positives).
Methods
The authors introduce CodeScan, a black-box scanning framework that detects whether a code LLM has been compromised without needing access to the model's internal weights or training data.
The CodeScan pipeline operates as follows:
- Vocabulary Scanning: CodeScan systematically appends candidate tokens from the model's vocabulary to a set of clean prompts and collects the generated outputs.
- Structural Divergence Analysis: Instead of checking for exact token matches, CodeScan normalizes the generated code snippets using Abstract Syntax Trees (ASTs). This abstracts away surface-level variations like variable names or literal values. It then clusters the normalized code to find structurally consistent patterns that persist across different prompts.
- LLM-Based Vulnerability Analysis: Rather than assuming any highly consistent generation is an attack, CodeScan passes the isolated code structures to a state-of-the-art LLM (GPT-5 mini) primed to act as a vulnerability scanner. If the analyzer confirms the structure contains a targeted vulnerability class, the model is flagged as poisoned.
Datasets
The framework was evaluated using datasets from the CODEBREAKER benchmark, which includes poisoning datasets, verification datasets, and clean fine-tuning datasets. The evaluation focused on three real-world vulnerability classes:
- CWE-79: Cross-site Scripting (XSS) via direct use of Jinja2.
- CWE-295: Disabled certificate validation.
- CWE-200: Exposure of sensitive information via binding to all network interfaces.
CodeScan was tested across 108 models, spanning 7B-scale architectures (CodeLlama-7B-Python, Qwen2.5-Coder-7B, StarCoder2-7B) and larger configurations (CodeLlama-34B, Qwen2.5-Coder-14B, StarCoder2-15B) to demonstrate scalability.
Key Insights and Ideas
- Code is Structure, Not Just Tokens: The core "aha" moment is that poisoning attacks in code LLMs manifest as consistent structural patterns rather than exact token sequences. AST-based normalization allows the defense to look past variable renaming or complex obfuscation tactics designed to slip past static analyzers.
- The "Structural-Template" Trap: Benign code tokens (like
month) naturally generate highly repetitive, low-variance code fragments (like lists of calendar months). Token-based defenses mistake this natural predictability for a backdoor trigger. CodeScan bypasses this trap by verifying the security of the code, not just its predictability. - Fighting Fire with Fire: Advanced LLMs have become incredibly adept at de-obfuscation and semantic reasoning. Using a cutting-edge LLM as the final vulnerability analyzer allows CodeScan to successfully identify malicious payloads that were explicitly transformed to evade traditional static analysis tools or older LLMs (like GPT-4).
Key Results
- Near-Perfect Detection: CodeScan achieved 100% recall across all evaluated attacked models, yielding an overall detection accuracy of 97%+ (average F1-score of ~0.98).
- Drastic Reduction in False Alarms: On clean models, CodeScan maintained a remarkably low false positive rate (0% for CWE-79, 16.67% for CWE-295/200). In comparison, the baseline defense (BAIT) suffered from a 100% false positive rate on several tasks due to token-level confusion.
- Massive Efficiency Gains: By avoiding token-by-token scanning and analyzing a small, fixed number of completed generations, CodeScan reduced scanning overhead by up to an order of magnitude. For example, on CWE-79, BAIT took over 12,594 seconds on average, while CodeScan completed the scan in just 1,681 seconds.
Limitations and Future Work
- Dependency on the Vulnerability Analyzer: CodeScan’s accuracy is tightly coupled with the reliability of its underlying LLM analyzer. If the analyzer misinterprets a complex syntax error as a vulnerability, or fails to reason about a benign pattern correctly, it can induce false results.
- Adaptive Evasion: While CodeScan proved highly robust against varying poisoning rates, sophisticated attackers aware of CodeScan might attempt to design adaptive attacks that generate vulnerable code structures that specifically exploit weaknesses or blind spots in the LLM analyzer's reasoning. Future work will focus on improving the analyzer's robustness against syntactic noise.
Summary updated