Understanding and Improving Model Editing for Secure Code Generation
Weifeng Sun, Quanjun Zhang, Yuchen Chen, Chengran Yang, Gou Tan, David Lo
摘要
Large language models (LLMs) are widely used for code generation, yet they can reproduce vulnerable code implementations learned from insecure patterns in training data. Prior work has primarily explored inference-time hardening to reduce insecure generations without updating the target model. While effective, this paradigm couples security behavior to the auxiliary component and incurs additional runtime overhead. This paper presents the first systematic empirical study of applying model editing as the model-level hardening mechanism for secure code generation. Unlike inference-time interventions, model editing updates a small subset of parameters to inject security-relevant knowledge directly into the target LLM. We evaluate 3 stateof-the-art editing methods across diverse LLM families and compare them with CoSec, a representative inference-time hardening approach, focusing on: (i) security effectiveness and robustness, (ii) generalization to unseen vulnerabilities, and (iii) functional correctness on general programming tasks. Our results show that model editing yields substantially larger security gains than CoSec on seen vulnerability types, improving security ratios by 15%-25% over vanilla models, and these gains remain stable under prompt perturbations. However, security improvements do not always transfer reliably to unseen vulnerabilities and induce functional regressions on general programming tasks, even for UltraEdit, the best-performing editing method in our evaluation. To mitigate this side effect, we propose SafeEdit, a post-edit refinement method that combines functional tuning with edit-aware regularization. Across eight target LLMs, SafeEdit improves Pass@1 over UltraEdit by +11.73/+13.70/+15.50 percentage points at 𝑇 = 0.1/0.4/0.8, while largely preserving security. Compared with CoSec, it achieves relative security-ratio gains of +7.54%-12.04%. Additional evaluation on CodeGuard+ confirms that SafeEdit improves joint secure-and-correct generation. Importantly, SafeEdit and CoSec are complementary: combining them can improve security under stochastic decoding while maintaining strong functional correctness. Finally, we analyze efficiency and key design factors, showing that model editing is more efficient than CoSec and sensitive to editing depth, parameter location, and injected context. Overall, our work provides evidence-backed guidance for applying model editing to secure code generation.
问 Lune
几分钟读透这篇 Paper,不用再花一下午。
Lune 能读完全文,也能读引用它的顶会 Paper,讲清它的结论、证据和局限,每一点都注明出自哪篇 Paper。不用硬啃,也不靠猜。
脉络
- Aging with GRACE: Lifelong Model Editing with Discrete Key-Value Adaptors 引用了 Locating and Editing Factual Associations in GPT
- Aging with GRACE: Lifelong Model Editing with Discrete Key-Value Adaptors 引用了 Language Models are Few-Shot Learners
- Locating and Editing Factual Associations in GPT 引用了 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
- WizardCoder: Empowering Code Large Language Models with Evol-Instruct 引用了 Language Models are Few-Shot Learners
它引用的 Paper25
- Language Models are Few-Shot LearnersTom B. Brown, Benjamin Mann, Nick Ryder, Melanie Subbiah 等NeurIPS 2020 · 64,255 次引用
- Locating and Editing Factual Associations in GPTKevin Meng, David Bau, Alex Andonian, Yonatan BelinkovNeurIPS 2022 · 3,415 次引用
- WizardCoder: Empowering Code Large Language Models with Evol-InstructZiyang Luo, Can Xu, Pu Zhao, Qingfeng Sun 等ICLR 2024 · 945 次引用
- 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 次引用
- Aging with GRACE: Lifelong Model Editing with Discrete Key-Value AdaptorsTom Hartvigsen, Swami Sankaranarayanan, Hamid Palangi, Yoon Kim 等NeurIPS 2023 · 349 次引用
相关 Paper
- CoSec: On-the-Fly Security Hardening of Code LLMs via Supervised Co-decodingDong Li, Meng Yan, Yaosheng Zhang, Zhongxin Liu 等ISSTA 2024 · 10 次引用
- Rethinking the Evaluation of Secure Code GenerationShih-Chieh Dai, Jun Xu, Guanhong TaoICSE 2026 · 1 次引用
- DeepGuard: Secure Code Generation via Multi-Layer Semantic AggregationLi Huang, Zhongxin Liu, Yifan Wu, Tao Yin 等ACL 2026 · 1 次引用
- Large Language Models for Code: Security Hardening and Adversarial TestingJingxuan He, Martin T. VechevCCS 2023 · 98 次引用
- Learning from Failures: Error Notebook-guided Secure Code GenerationXinyu Zhong, Peng Lan, Zhifang LiaoACL Findings 2026
