CoSec: On-the-Fly Security Hardening of Code LLMs via Supervised Co-decoding
Dong Li, Meng Yan, Yaosheng Zhang, Zhongxin Liu, Chao Liu, Xiaohong Zhang, Ting Chen, David Lo
摘要
Large Language Models (LLMs) specialized in code have shown exceptional proficiency across various programming-related tasks, particularly code generation. Nonetheless, due to its nature of pretraining on massive uncritically filtered data, prior studies have shown that code LLMs are prone to generate code with potential vulnerabilities. Existing approaches to mitigate this risk involve crafting data without vulnerability and subsequently retraining or fine-tuning the model. As the number of parameters exceeds a billion, the computation and data demands of the above approaches will be enormous. Moreover, an increasing number of code LLMs tend to be distributed as services, where the internal representation is not accessible, and the API is the only way to reach the LLM, making the prior mitigation strategies non-applicable. To cope with this, we propose CoSec, an on-the-fly Security hardening method of code LLMs based on security model-guided Co-decoding, to reduce the likelihood of code LLMs to generate code containing vulnerabilities. Our key idea is to train a separate but much smaller security model to co-decode with a target code LLM. Since the trained secure model has higher confidence for secure tokens, it guides the generation of the target base model towards more secure code generation. By adjusting the probability distributions of tokens during each step of the decoding process, our approach effectively influences the tendencies of generation without accessing the internal parameters of the target code LLM. We have conducted extensive experiments across various parameters in multiple code LLMs (i.e., CodeGen, StarCoder, and DeepSeek-Coder), and the results show that our approach is effective in security hardening. Specifically, our approach improves the average security ratio of six base models by 5.02%-37.14%, while maintaining the functional correctness of the target model.
问 Lune
几分钟读透这篇 Paper,不用再花一下午。
Lune 能读完全文,也能读引用它的顶会 Paper,讲清它的结论、证据和局限,每一点都注明出自哪篇 Paper。不用硬啃,也不靠猜。
已有 5 篇顶会 Paper 引用了它。别等审稿人问起,先弄清它们发现了什么。
脉络
- LoRA: Low-Rank Adaptation of Large Language Models 引用了 Language Models are Few-Shot Learners
- Asleep at the Keyboard? Assessing the Security of GitHub Copilot's Code Contributions 引用了 Language Models are Few-Shot Learners
- RESCUE: Retrieval Augmented Secure Code Generation 引用了 Asleep at the Keyboard? Assessing the Security of GitHub Copilot's Code Contributions
- RESCUE: Retrieval Augmented Secure Code Generation 引用了 Give LLMs a Security Course: Securing Retrieval-Augmented Code Generation via Knowledge Injection
- RESCUE: Retrieval Augmented Secure Code Generation 引用了 Efficient Memory Management for Large Language Model Serving with PagedAttention
- Give LLMs a Security Course: Securing Retrieval-Augmented Code Generation via Knowledge Injection 引用了 Asleep at the Keyboard? Assessing the Security of GitHub Copilot's Code Contributions
- Give LLMs a Security Course: Securing Retrieval-Augmented Code Generation via Knowledge Injection 引用了 Language Models are Few-Shot Learners
- Chasing Shadows: Pitfalls in LLM Security Research 引用了 CodeT5+: Open Code Large Language Models for Code Understanding and Generation
- Chasing Shadows: Pitfalls in LLM Security Research 引用了 Language Models are Few-Shot Learners
- DeepGuard: Secure Code Generation via Multi-Layer Semantic Aggregation 引用了 Asleep at the Keyboard? Assessing the Security of GitHub Copilot's Code Contributions
- Efficient Memory Management for Large Language Model Serving with PagedAttention 引用了 Language Models are Few-Shot Learners
- Understanding and Improving Model Editing for Secure Code Generation 引用了 Asleep at the Keyboard? Assessing the Security of GitHub Copilot's Code Contributions
- Understanding and Improving Model Editing for Secure Code Generation 引用了 Language Models are Few-Shot Learners
引用它的顶会 Paper5
- Chasing Shadows: Pitfalls in LLM Security ResearchJonathan Evertz, Niklas Risse, Nicolai Neuer, Andreas Müller 等NDSS 2026 · 17 次引用
- RESCUE: Retrieval Augmented Secure Code GenerationJiahao Shi, Tianyi ZhangICLR 2026 · 15 次引用
- DeepGuard: Secure Code Generation via Multi-Layer Semantic AggregationLi Huang, Zhongxin Liu, Yifan Wu, Tao Yin 等ACL 2026 · 1 次引用
- Give LLMs a Security Course: Securing Retrieval-Augmented Code Generation via Knowledge InjectionBo Lin, Shangwen Wang, Yihao Qin, Liqian Chen 等CCS 2025
- Understanding and Improving Model Editing for Secure Code GenerationWeifeng Sun, Quanjun Zhang, Yuchen Chen, Chengran Yang 等ISSTA 2026
它引用的 Paper22
- Language Models are Few-Shot LearnersTom B. Brown, Benjamin Mann, Nick Ryder, Melanie Subbiah 等NeurIPS 2020 · 64,255 次引用
- LoRA: Low-Rank Adaptation of Large Language ModelsEdward J. Hu, Yelong Shen, Phillip Wallis, Zeyuan Allen-Zhu 等ICLR 2022 · 18,833 次引用
- Efficient Memory Management for Large Language Model Serving with PagedAttentionWoosuk Kwon, Zhuohan Li, Siyuan Zhuang, Ying Sheng 等SOSP 2023 · 1,016 次引用
- Asleep at the Keyboard? Assessing the Security of GitHub Copilot's Code ContributionsHammond Pearce, Baleegh Ahmad, Benjamin Tan, Brendan Dolan-Gavitt 等S&P 2022 · 725 次引用
- CodeT5+: Open Code Large Language Models for Code Understanding and GenerationYue Wang, Hung Le, Akhilesh Gotmare, Nghi D. Q. Bui 等EMNLP 2023 · 339 次引用
相关 Paper
- Large Language Models for Code: Security Hardening and Adversarial TestingJingxuan He, Martin T. VechevCCS 2023 · 98 次引用
- Decoding Secret Memorization in Code LLMs Through Token-Level CharacterizationYuqing Nie, Chong Wang, Kailong Wang, Guoai Xu 等ICSE 2025 · 10 次引用
- Rethinking the Evaluation of Secure Code GenerationShih-Chieh Dai, Jun Xu, Guanhong TaoICSE 2026 · 1 次引用
- PromSec: Prompt Optimization for Secure Generation of Functional Source Code with Large Language Models (LLMs)Mahmoud Nazzal, Issa Khalil, Abdallah Khreishah, NhatHai PhanCCS 2024 · 16 次引用
- Repairing LLM Executions for Secure Automatic ProgrammingAli El Husseini, Yacine Izza, Blaise Genest, Abhik RoychoudhuryICSE 2026
