ProSec: Fortifying Code LLMs with Proactive Security Alignment

Xiangzhe Xu, Zian Su, Jinyao Guo, Kaiyuan Zhang, Zhenting Wang, Xiangyu Zhang

Introduction: The Reactive Security Bottleneck

While Large Language Models (LLMs) have become remarkably proficient at writing code, they frequently produce insecure snippets that harbor critical vulnerabilities, such as SQL injections or buffer overflows.

Current attempts to mitigate this issue rely on "reactive" datasets—collections of real-world vulnerabilities and their corresponding fixes mined from sources like GitHub commits. However, this approach faces two major hurdles:

  1. Data Sparsity (The "Human Filter" Problem): Human programmers usually catch and fix obvious security blunders before committing code. Consequently, the "negative examples" needed to teach an LLM what not to do are exceedingly rare in web-scale training data.
  2. Pipeline Integration: Most security-focused tuning happens during the initial instruction-tuning phase. This makes it difficult to add security "patches" to models that have already undergone complex, multi-stage post-training (like RLHF) without disrupting their existing capabilities.

To address these limitations, researchers introduce ProSec (Proactive Security Alignment), a novel framework that secures code-generating LLMs by intentionally "trapping" them into generating vulnerable code, and then teaching them to fix those specific errors.

The ProSec Methodology

ProSec shifts from a reactive to a proactive stance through a systematic three-step pipeline designed to elicit and then correct insecure coding behaviors.

ProSec Framework Overview
  1. Vulnerability-Inducing Synthesis: Instead of searching for rare real-world bugs, ProSec uses a "teacher" LLM to brainstorm coding tasks specifically designed to tempt a developer (or a target LLM) into making a mistake related to a specific Common Weakness Enumeration (CWE).
  2. Preference Data Construction:
    • $D_{sec}$ (Security Data): The target model is prompted with these synthesized "trap" instructions. If it generates vulnerable code, it is then asked to fix its own mistake, aided by feedback from a static analyzer. This creates a "Preferred (Fixed) vs. Rejected (Vulnerable)" data pair.
  3. $D_{norm}$ (Utility Data): To ensure the model doesn't become overly paranoid (e.g., unnecessarily sanitizing a simple internal string), ProSec also collects pairs of normal, safe coding tasks.
  4. Data Selection & Alignment: ProSec employs a Training Dynamics-Based Sampler. It measures how much a specific "normal" code sample helps the model resist over-sanitization during a warm-up training phase. Finally, the model is aligned using preference optimization objectives like SimPO or DPO.

Key Insights

  • Contextual Fixing via Latent Knowledge: An "aha moment" in the research is the observation that a post-trained model is often capable of fixing a vulnerability if the flaw is explicitly pointed out, even if it failed to avoid the vulnerability initially. ProSec leverages this latent knowledge to generate its own high-quality training data.
  • Surgical Utility Preservation: Security alignment often degrades general coding performance. ProSec’s use of Kendall Tau correlation to select the most "influential" utility samples allows for a precise balance between safety and helpfulness.

Results and Evaluation

The ProSec dataset was built using the code-related portion of Infinity-Instruct as a base. The proactive synthesis process proved incredibly effective, triggering 25x more vulnerable code than standard datasets. This resulted in a security-focused alignment set 7x larger than previous state-of-the-art efforts like SafeCoder.

Security vs. Utility Performance

When evaluated on PurpleLlama (for security) and HumanEval-Multi / MXEval (for general coding utility), the results demonstrated significant improvements:

  • Substantial Security Gains: Models aligned with ProSec were 25.2% to 35.4% more secure than those trained with previous methods.
  • Utility Preservation: As shown in the performance scatter plots, models' general coding ability (Pass@1) actually improved slightly or remained stable. The authors attribute this to the fact that security-related tasks are often more complex and "high-quality" than standard instructions, inadvertently boosting general reasoning.
  • Inference Efficiency: ProSec achieved comparable results to "Iterative Refinement" (asking a model to fix code multiple times in a loop) but at a fraction of the inference cost, as the security behaviors are baked directly into the model weights.

Limitations and Future Work

While ProSec represents a significant leap forward, it currently operates in an "offline" paradigm using static datasets. The authors suggest that Online Reinforcement Learning, where the model receives real-time feedback from static analyzers and compilers during training, is a promising next step.

Furthermore, while ProSec makes models safer, it doesn't necessarily endow them with deep "System-2" reasoning about security logic. Future work could explore integrating multi-step reasoning to help models think through complex program semantics before generating code. Finally, the quality of the "fixed" samples inherently depends on the accuracy and coverage of the static analyzers used during the data construction phase.

Summary updated

BibTeX
@inproceedings{xu2025prosec,
  author = {Xiangzhe Xu and Zian Su and Jinyao Guo and Kaiyuan Zhang and Zhenting Wang and Xiangyu Zhang},
  title = {{ProSec: Fortifying Code LLMs with Proactive Security Alignment}},
  booktitle = {International Conference on Machine Learning},
  year = {2025},
  url = {https://openreview.net/forum?id=Ym19zWky7W},
}
Figure