NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Understanding and Improving Model Editing for Secure Code Generation

Weifeng Sun, Quanjun Zhang, Yuchen Chen, Chengran Yang, Gou Tan, David LoarXiv 2026

TL;DR

Large Language Models (LLMs) frequently generate code containing security vulnerabilities inherited from their training data. While prior defenses rely on slow, auxiliary models at inference time to steer code generation, this paper explores model editing—directly updating a small subset of the LLM's parameters—to permanently bake in security knowledge. To fix the side effect where editing degrades general coding capabilities, the authors introduce SafeEdit, a post-edit refinement method that successfully restores functional correctness while preserving security gains.

What Problem Does This Paper Address?

As LLMs become standard assistants for software engineers, they bring a hidden risk: reproducing insecure code patterns (like SQL injections or buffer overflows) found in their open-source training corpora.

Existing solutions primarily focus on inference-time hardening (e.g., CoSec), which uses a secondary, smaller "security model" to look over the shoulder of the main LLM and alter token probabilities during decoding. While effective, this approach introduces massive runtime latency and relies heavily on the quality of the secondary model. Conversely, traditional fine-tuning updates the whole model but is computationally expensive and often breaks unrelated coding capabilities.

This paper presents the first systematic study of model editing as a lightweight, model-level alternative to secure code generation, investigating whether localized parameter updates can eliminate vulnerabilities without destroying general programming performance.

Methods

The authors evaluate three state-of-the-art model editing techniques—UltraEdit, DINM, and DEFER*—across various open-weight LLM families (including LLaMA-3 and Qwen-2.5-Coder), comparing them against the inference-time baseline, CoSec.

Fig. 1. Overview of our experimental pipeline for secure code generation hardening, comparing model editing with inference-time hardening (CoSec) across security, generalization, functional correctness.

A key contribution of the paper is SafeEdit, a post-edit refinement technique designed to address the "security-correctness trade-off" discovered during their evaluation. When direct model editing fixes security bugs, it often accidentally degrades the model's general coding proficiency. SafeEdit mitigates this by applying:

  1. Functional Tuning: Fine-tuning the edited model on instruction-style code datasets to recover general programming capabilities.
  2. Edit-Aware L2 Regularization: A loss constraint ($\mu|∥\theta - \theta_e∥^2$) that anchors the specific parameters modified during the editing phase ($\theta_e$), preventing the functional tuning process from erasing the newly injected security knowledge.

Datasets

The study leverages several distinct datasets for training and multi-dimensional evaluation:

  • Hardening Dataset: 710 vulnerable/fixed function pairs across 9 Common Weakness Enumeration (CWE) categories in Python and C/C++, used to train the baseline and generate editing instances.
  • Seen vs. Unseen CWEs: Evaluation is split between the 9 "seen" vulnerability types and 17 entirely "unseen" CWE categories (comprising 29 distinct scenarios) to test how well the security hardening generalizes.
Table 1. CWE categories in the dataset [19] and seen/unseen separation statistics. Severity is indicated by superscripts: C=Critical, H=High, M=Medium. The severity levels are based on MITRE CWE data [38].
  • Functional Correctness: The standard HumanEval benchmark (164 Python tasks) and MultiPL-E (covering C++, JavaScript, and TypeScript) are used to measure general coding performance.
  • CodeGuard+: A holistic benchmark consisting of 91 prompts across 34 CWEs used to jointly evaluate security and functional correctness via unit tests and static analysis ensembles.

Key Insights and Ideas

  • Editing is More Effective and Robust Than Steering: Directly updating internal representations via model editing yields higher and more stable security gains than inference-time token biasing. Inference-time methods like CoSec proved brittle under minor prompt perturbations.
  • The Security-Correctness Trade-off: Aggressive editing methods (like DINM) that alter specific neurons can cause a near-collapse in general coding performance (Pass@1 scores), showing that security fixes can easily corrupt shared representations used for general code synthesis.
  • Complementary Strengths: Model editing and inference-time steering are not mutually exclusive. Combining SafeEdit with CoSec yields the highest overall security ratios under stochastic decoding conditions without sacrificing code correctness.
  • Location Matters: In FFN blocks, editing the up-projection layer consistently provides a better balance of security and correctness compared to editing the down-projection or gate-projection layers.

Key Results

  • Security Boost: Model editing techniques improve the security ratio of generated code by 15% to 25% over vanilla models on seen vulnerability types.
  • SafeEdit Performance: SafeEdit successfully recovers the functional degradation caused by editing. Across eight target LLMs, SafeEdit improves the Pass@1 metric over UltraEdit by +11.73, +13.70, and +15.50 percentage points at decoding temperatures of $T = 0.1, 0.4, \text{ and } 0.8$, respectively.
Security Ratio results for LLaMA3.1-8B under seen CWEs and prompt perturbations.
  • Efficiency Gains: While inference-time steering increases per-query latency by 2–3× due to dual-model decoding, UltraEdit and SafeEdit add zero inference overhead once deployed. UltraEdit completes its offline editing phase in just 50–100 seconds for an 8B-parameter model.

Limitations and Future Work

The authors note a few core limitations:

  1. Limited Generalization: Security improvements on seen vulnerability types do not reliably transfer to structurally different, unseen CWEs.
  2. Oracle Dependency: The security evaluation relies heavily on CodeQL static analysis, which inherently includes minor false-positive and false-negative rates (though manually verified to be 92.5% accurate in this study).
  3. Open-Weight Restriction: Model editing requires direct access to model weights, meaning these techniques cannot currently be applied to closed, API-only models like GPT-4.

Future work will focus on developing hybrid hardening mechanisms that can achieve better cross-CWE security generalization while strictly preserving functional correctness.

Summary updated

BibTeX
@misc{sun2026understanding,
  author = {Weifeng Sun and Quanjun Zhang and Yuchen Chen and Chengran Yang and Gou Tan and David Lo},
  title = {{Understanding and Improving Model Editing for Secure Code Generation}},
  year = {2026},
  eprint = {2608.06848},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2608.06848},
}
Figure