A Mixture of Linear Corrections Generates Secure Code

Weichen Yu, Ravi Mangal, Terry Yue Zhuo, Matt Fredrikson, Corina S. Pasareanu · Submission to ICLR 2026 · Rejected

TL;DR

Large language models (LLMs) struggle to detect or avoid software vulnerabilities when guided only by text prompts, often performing no better than random guessing. This paper shows that models actually encode vulnerability knowledge deep inside their latent activations. By reading these representations with lightweight linear probes and conditionally steering hidden states via a Mixture of Corrections (MoC), the authors reduce vulnerable code generation and even boost functional correctness—all without fine-tuning model weights.

What Problem Does This Paper Address?

State-of-the-art code LLMs frequently produce code riddled with security flaws (such as buffer overflows or SQL injection vulnerabilities). Prompting techniques—such as providing few-shot examples or detailed Common Weakness Enumeration (CWE) definitions—consistently fail to fix this, often yielding vulnerability detection rates near 50%. Even scaling model size from 7B to 34B parameters fails to resolve the gap.

This raises a fundamental question: Do code LLMs simply lack the knowledge to distinguish secure code from vulnerable code, or is that knowledge just inaccessible through prompt-based generation?

Methods

The authors adopt a representation engineering approach, splitting the solution into detection and steering phases:

[Hidden State s] ──► [Linear Probes c_j] ──► Vulnerable? 
                             │
                  Yes ───────┴─────── No
                   ▼                  ▼
        Apply Decayed Correction   No Change
         s + α(t) · Δs_j              (s)
  1. Vulnerability Probing: The authors train linear classifiers (probes) using cross-entropy loss on the hidden activations of internal transformer blocks across paired (secure vs. vulnerable) code snippets. The best performance emerges at the final transformer layers.

  2. Mixture of Corrections (MoC): To steer generation away from security flaws, the authors compute a vector correction $\Delta s_j$ for each CWE class using four candidate strategies:

  3. Group Mean Difference: The vector separating class centroids of secure and vulnerable states.

  4. Normal Vector: The orthogonal vector to the probe’s linear decision boundary hyperplane.
  5. PCA-Reduced Normal Vector: A normal vector computed in a compressed latent subspace and projected back to reduce noise.
  6. Dynamic Correction Network: A lightweight three-layer MLP conditioned directly on the current hidden state, trained with mean squared error, cross-entropy, and KL-divergence losses.
  7. Conditional Steering with Temporal Decay: Rather than permanently shifting hidden activations (which often damages text coherence), MoC:

  8. Evaluates the probe at each token step and only applies the correction if the state is flagged as vulnerable.

  9. Multiplies the intervention by an exponential decay factor $\alpha(t)$ across token positions to prevent runaway activation drift.

Datasets

  • SVEN: A function-level dataset containing paired secure and vulnerable code samples across 9 prominent CWEs (including CWE-022 path traversal, CWE-078 OS injection, CWE-089 SQL injection, and memory safety flaws like CWE-125 and CWE-416). Used for probe training and generation evaluation.
  • PreciseBugs: A repository-level dataset with real-world security fixes from NVD and OSS-Fuzz containing contexts up to 30,000 tokens. Used to evaluate probe detection on long-context, multi-file code.
  • HumanEval: The standard 164-problem Python benchmark used to test whether steering compromises functional code execution (pass@1).

Key Insights and Ideas

  • The knowledge is already inside the model: While prompting Qwen2.5-Coder-14B yields a meager 40% detection accuracy (F1 of 0.37), linear probes on the model's activations reach 82% accuracy (F1 of 0.85). The model "knows" code is dangerous even when its text output cannot state it.
  • Fixing security helps functionality: Security steering does not degrade general programming ability; it actually improves it. On Qwen2.5-Coder-7B, HumanEval pass@1 rose from 79.9% to 82.0%. Because buggy code and insecure code share underlying structural flaws, steering away from vulnerabilities steers the model toward higher-quality code overall.
  • Decay and conditionality are critical: Unconditional steering or omitting temporal decay severely degraded code functionality (dropping HumanEval performance down to 69.6% without decay).
  • Cross-model transferability: Correction vectors extracted from Qwen2.5-Coder could partially harden general-purpose instruction models (Qwen2.5-Instruct) of matching size without retraining.

Key Results

  • Vulnerability Detection: Probing outperforms complex graph neural networks (GNNs) and specialized classification baselines. On repo-level PreciseBugs, MoC probes achieved an F1 of 0.58 vs. 0.48 for SOTA baseline MSIVD.
  • Secure Code Generation:
  • Qwen2.5-Coder-7B: Boosted security rate from 76.8% to 85.7% (an 8.9% absolute increase) using dynamic neural corrections, while raising HumanEval pass@1 from 79.9% to 81.2%.
  • Qwen2.5-Coder-3B: Improved security rate from 83.8% to 85.0% and HumanEval from 70.2% to 73.3% using PCA normal vectors.
  • Latency & Efficiency: On an NVIDIA L40S GPU running vLLM, MoC adds only 72M FLOPs per token. Generation latency rose modestly from 1.2s to 1.8s for 50 test samples, with zero increase in GPU peak memory (36 GB).

Limitations and Future Work

  • Per-Token Probe Overhead: Running separate probe evaluations for multiple CWEs at every decoding step adds latency compared to unsteered decoding; future work must explore parallelized or asynchronous verification.
  • Static Analyzer Bias: Secure code evaluation relies on CodeQL custom queries, which can suffer from false positives/negatives and miss domain-specific business logic vulnerabilities.
  • Transfer Limits: While steering vectors transfer reasonably well on smaller models (3B/7B), transferability broke down on 14B models, resulting in reduced functional accuracy.
  • Benchmark Coverage: The evaluation of code generation remains limited to function-level benchmarks due to missing implementation code in emerging repo-level platforms (such as SecCodePLT).

Summary updated

BibTeX
@inproceedings{yu2025mixture,
  author = {Weichen Yu and Ravi Mangal and Terry Yue Zhuo and Matt Fredrikson and Corina S. Pasareanu},
  title = {{A Mixture of Linear Corrections Generates Secure Code}},
  booktitle = {ResponsibleFM @ NeurIPS},
  year = {2025},
  url = {https://openreview.net/forum?id=3NfAz7Pr4B},
}
Figure