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

Large Language Models for Code: Security Hardening and Adversarial Testing

Jingxuan He, Martin T. Vechev

Sure, here's a blog-style summary of the paper "Large Language Models for Code: Security Hardening and Adversarial Testing":

TL;DR

Large language models (LLMs) are great at generating code, but they often produce insecure code. This paper introduces SVEN, a novel method that can either "harden" LLMs to generate more secure code or "adversarially test" them by making them generate less secure code, all while preserving their ability to write functional code. This is achieved by training lightweight "prefix" modules that guide the LLM without altering its core weights.

What Problem Does This Paper Address?

LLMs like GitHub Copilot have revolutionized code generation, boosting developer productivity. However, a significant drawback is their tendency to produce code with security vulnerabilities. While users can manually fix these issues, it's a time-consuming and error-prone process. This paper tackles two key challenges:

  1. Security Hardening: How can we make LLMs generate more secure code by default?
  2. Adversarial Testing: How can we systematically evaluate an LLM's security weaknesses to understand its vulnerabilities?

The authors propose a unified framework for both, aiming to control the security of LLM-generated code without sacrificing its functional correctness, which is crucial for practical usability.

Methods

The core innovation is a technique called SVEN (Security ENforcement). Instead of retraining the entire LLM (which is computationally expensive), SVEN introduces small, trainable modules called "prefixes." These prefixes are essentially sequences of continuous vectors that are prepended to the LLM's input.

Here's how it works:

  • Controlled Code Generation: SVEN frames the problem as "controlled code generation." Given a prompt, the LLM also receives a binary property (e.g., "secure" or "vulnerable"). SVEN uses its learned prefixes to steer the LLM's generation process towards satisfying this property.
  • Prefix Tuning: The prefixes are trained using gradient descent. They are designed to be lightweight, adding only about 0.1% to the total model parameters. This makes training efficient, even on smaller datasets.
  • Dual Objective Training: SVEN's training objective is to balance two competing goals:
  • Security Control: Encouraging the generation of secure (or insecure, for adversarial testing) code.
  • Functional Correctness: Ensuring the generated code still works as intended. This is achieved by using specialized loss functions that operate on different parts of the code:

  • Changed Regions: Loss functions are applied to code snippets that were modified in security fixes, directly enforcing security properties.

  • Unchanged Regions: A KL divergence loss is used to regularize the prefixes, ensuring they don't deviate too much from the original LLM's behavior, thus preserving functional correctness.
  • Modular Design: The prefixes are independent modules. This means you can train a "secure" prefix and a "vulnerable" prefix for an LLM and then switch between them as needed for hardening or testing.

Datasets

The authors created a high-quality training dataset by manually curating security fixes from GitHub commits. They focused on 9 Common Weakness Enumeration (CWE) categories that are part of the MITRE Top-25 list. This dataset consists of approximately 1,600 program pairs (insecure vs. secure versions). They also used existing vulnerability datasets like CrossVul, Big-Vul, and VUDENC but found them to have quality issues (e.g., irrelevant code artifacts, limited scope) that necessitated manual curation.

For evaluation, they used:

  • Main CWEs: The 9 CWEs from their curated training set.
  • HumanEval Benchmark: A standard benchmark for evaluating functional correctness of code generation models.
  • Additional CWEs: Scenarios for CWEs not included in their training set to test generalization.

Key Insights and Ideas

  • Security is Localized: The authors observed that in security fixes, the changed code is crucial for security, while the unchanged code is neutral. This insight is key to their training strategy, allowing them to apply different loss functions to different code regions.
  • Prefixes are Powerful and Efficient: Using continuous prefixes for security control is a novel application. It's highly efficient because it doesn't require retraining the entire LLM, making security hardening and adversarial testing accessible even for very large models.
  • Data Quality Over Quantity: Their experiments showed that their manually curated, smaller dataset significantly outperformed a much larger, automatically collected dataset that contained more noise and irrelevant code. This highlights the critical importance of data quality in this domain.
  • Functional Correctness is Achievable: A major concern with security interventions is breaking the code. SVEN's careful training approach demonstrably preserves functional correctness, as evidenced by comparable pass@k scores on the HumanEval benchmark.

Key Results

  • Significant Security Improvement: For CodeGen-2.7B, SVENsec boosted the security rate from 59.1% to 92.3%.
  • Effective Adversarial Testing: SVENvul successfully degraded the security rate to as low as 36.8%.
  • Preserved Functional Correctness: SVEN's pass@k scores on the HumanEval benchmark closely matched those of the original CodeGen models across different sizes.
  • Robustness: SVEN demonstrated robustness to prompt perturbations, maintaining high security rates even when the input prompts were slightly altered.
  • LM Agnostic: SVEN showed applicability to other LLMs like InCoder and SantaCoder, indicating its generalizability.
  • Generalization to Unseen CWEs: SVEN showed promising results in improving security for CWEs not present in its training data, though with varying degrees of success.

Limitations and Future Work

  • Limited CWE Coverage: While SVEN performs well on its trained CWEs, its generalization to entirely new or complex security vulnerabilities is still an area for improvement. The authors suggest expanding the training dataset with more diverse security fixes.
  • Indirect Functional Correctness: The current method uses KL divergence as a proxy for functional correctness. Future work could involve direct optimization for functional correctness using unit test execution rewards.
  • Prompt Independence: SVEN's prefixes are currently independent of the prompt. Introducing a dependency could potentially enhance expressivity and accuracy.
  • Beyond Security: The techniques developed for SVEN could be extended to control other aspects of code generation, such as API usage or bug fixes.

Summary updated

BibTeX
@inproceedings{he2023large,
  author = {Jingxuan He and Martin T. Vechev},
  title = {{Large Language Models for Code: Security Hardening and Adversarial Testing}},
  booktitle = {CCS},
  year = {2023},
  doi = {10.1145/3576915.3623175},
}
Figure