Understanding and Improving Model Editing for Secure Code Generation
Weifeng Sun, Quanjun Zhang, Yuchen Chen, Chengran Yang, Gou Tan, David Lo
cs.CR, cs.SE
Submitted: 2026-08-07
Updated: 2026-08-10
Comments: ISSTA 2026
Code: https://github.com/github/codeql
License: http://creativecommons.org/licenses/by/4.0/
The gist: Large language models (LLMs) are widely used for code generation, yet they can reproduce vulnerable implementations learned from insecure training patterns.
Terminology
Abstract
Large language models (LLMs) are widely used for code generation, yet they can reproduce vulnerable implementations learned from insecure training patterns. Prior work has mainly explored inference-time hardening, which reduces insecure generations without modifying the target model but relies on auxiliary components and adds runtime overhead. We conduct the first systematic study of model editing as a model-level hardening mechanism for secure code generation. We evaluate 3 state-of-the-art editing methods across diverse LLM families and compare them with CoSec, a representative inference-time approach, focusing on security, robustness, generalization, and functional correctness. Model editing yields larger security gains than CoSec on seen vulnerability types, improving security ratios by 15%-25% over vanilla models, with gains remaining stable under prompt perturbations. However, these improvements transfer unreliably to unseen vulnerabilities and can reduce functional correctness. To mitigate this trade-off, we propose SafeEdit, a post-edit refinement method combining 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 T=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 improved joint secure-and-correct generation. SafeEdit and CoSec are also complementary, and their combination can further improve security while maintaining strong functional correctness. Overall, our results provide evidence-backed guidance for applying model editing to secure code generation.
Sources
- Evaluating Large Language Models Trained on Code
- What's Wrong with Your Code Generated by Large Language Models? An Extensive Study
- Constrained Decoding for Secure Code Generation
- The Llama 3 Herd of Models
- Neuron Patching: Semantic-based Neuron-level Language Model Repair for Code Generation
- UltraEdit: Training-, Subject-, and Memory-Free Lifelong Editing in Language Models
- DeepSeek-Coder: When the Large Language Model Meets Programming -- The Rise of Code Intelligence
- Instruction Tuning for Secure Code Generation
- Qwen2.5-Coder Technical Report
- StarCoder: may the source be with you!
- Model Editing for LLMs4Code: How Far are We?
- WizardCoder: Empowering Code Large Language Models with Evol-Instruct
- Mass-Editing Memory in a Transformer
- CodeGen: An Open Large Language Model for Code with Multi-Turn Program Synthesis
- Llama 2: Open Foundation and Fine-Tuned Chat Models
- Detoxifying Large Language Models via Knowledge Editing
- RLCoder: Reinforcement Learning for Repository-Level Code Completion
- Editing Large Language Models: Problems, Methods, and Opportunities
- Unveiling the Lexical Sensitivity of LLMs: Combinatorial Optimization for Prompt Enhancement
- A Comprehensive Study of Knowledge Editing for Large Language Models
Related papers
- SoK: AI-Augmented Binary Reversing
- Relaxed Sender Anonymity for CBDC Interbank Settlement: A Zero-Knowledge Approach on Permissioned EVM
- Calibration-Family Overfit: Why Trusted Sabotage Monitors Don't Transfer Across Lineages
- Estimating the Social Cost of Corporate Data Breaches
- Efficient Fuzzy PSI under One-Sided Assumptions
- Sealing the Audit-Runtime Gap for LLM Skills