A Mixture of Linear Corrections Generates Secure Code

Weichen Yu, Ravi Mangal, Terry Yue Zhuo, Matt Fredrikson, Corina S. Pasareanu · DOI

Introduction

Large language models (LLMs) have become indispensable tools for software development, yet they frequently generate code with security vulnerabilities. While state-of-the-art models like CodeLlama and Qwen2.5-Coder demonstrate high functional proficiency, they often fail to identify or avoid common security pitfalls when prompted directly. This research investigates whether this failure stems from a lack of knowledge or an inability to express that knowledge through text. The authors introduce Mixture of Corrections (MoC), a technique that leverages the model's internal representations to steer generation toward secure code.

The Gap Between Knowledge and Output

Standard prompting for vulnerability detection often yields results no better than a coin flip (~50% accuracy). However, this study reveals that LLMs possess precise latent representations of code vulnerabilities. By probing the hidden states of these models, the researchers achieved detection accuracies up to 85%. This suggests that the knowledge required for secure coding is present within the model's weights but remains inaccessible via standard natural language interfaces.

Methodology: Mixture of Corrections (MoC)

The proposed MoC framework operates in two phases: detection and steering. First, lightweight linear probes are trained on the internal hidden states of the LLM to identify specific Common Weakness Enumerations (CWEs). Second, "correction vectors" are calculated to steer the model's activations away from identified vulnerability directions during inference.

Architecture of the MoC framework and the four proposed methods for generating correction vectors.

The authors explored four methods for generating these corrections:

  1. Group Mean Difference: Arithmetic difference between secure and vulnerable state means.
  2. Normal Vector: Using the orthogonal vector of the linear probe's decision boundary.
  3. Compression: Applying PCA to identify and shift the most significant vulnerability-related components.
  4. Training a Network: A dynamic approach using a small neural network to predict the optimal correction.

Experimental Results

The MoC technique was evaluated using the SVEN dataset for security and HumanEval for functional correctness. The results demonstrate a significant "win-win" scenario where both security and functionality improve simultaneously.

Comparison of detection accuracy (left) and the impact of MoC on security vs. functionality (right).

Key Findings:

  • Enhanced Detection: Probing Qwen2.5-Coder-14B resulted in an 85% F1 score, a massive leap from the 37% achieved via prompting.
  • Improved Security Ratio: For Qwen2.5-Coder-7B, the security ratio increased by 8.9%, reaching 85.7%.
  • Functional Gains: Contrary to the expectation of a trade-off, functional correctness on HumanEval pass@1 improved by 2.1%. This suggests that steering away from vulnerabilities also steers the model away from general coding bugs.
  • Transferability: Correction vectors derived from code-specific models can harden general instruction-tuned models of the same architecture, providing a cost-effective security upgrade.

Conclusion and Future Outlook

The Mixture of Corrections framework proves that internal model steering is a powerful, lightweight alternative to expensive fine-tuning for code security. While the current implementation introduces some inference overhead due to per-token probing, future optimizations like parallelization could make it highly practical for real-world deployment. Beyond steering, these latent analysis tools offer a new lens for discovering undocumented vulnerabilities in complex codebases.

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