Learning to Repair Software Vulnerabilities with Generative Adversarial Networks

Jacob Harer, Onur Ozdemir, Tomo Lazovich, Christopher P. Reale, Rebecca L. Russell, Louis Y. Kim, Peter Chin

Executive Summary

Researchers have developed a novel approach to automatically fix software vulnerabilities using Generative Adversarial Networks (GANs) that do not require "before-and-after" training pairs. By leveraging unpaired sets of "vulnerable" and "clean" code, their model learns to suggest repairs that perform nearly as well as traditional supervised methods. This breakthrough opens the door to utilizing massive, previously unusable datasets for automated security and code correction.

The Challenge of Unpaired Data

Automated software repair is a "holy grail" in cybersecurity. While robust tools exist to detect bugs, fixing them usually requires a human in the loop. Current AI approaches treat code repair similarly to language translation—translating "broken code" into "fixed code." However, these models typically require millions of paired examples, meaning specific instances of a bug and its exact human-written fix.

In the real world, these exact pairs are incredibly scarce. Conversely, we have an abundance of unpaired data: vast repositories of known vulnerable code and separate, massive repositories of high-quality, secure code. This paper tackles the challenge of "unpaired" translation: How can a machine learn to fix a bug if it has never seen a specific example of that exact bug being fixed?

Methodology

The authors employ a Generative Adversarial Network (GAN) framework specifically tailored for discrete sequences like text or source code.

  • The Generator: A standard Neural Machine Translation (NMT) model utilizing an encoder-decoder architecture with attention. It takes vulnerable code as input and attempts to output a repaired version.
  • The Discriminator: A Convolutional Neural Network (CNN) tasked with distinguishing between "real" secure code from a clean repository and "fake" repaired code produced by the generator.
  • Handling Discrete Data: Because gradients cannot easily pass through discrete tokens, the authors feed the "soft" probability distributions from the generator directly into the discriminator. To prevent the discriminator from "cheating" by simply identifying non-one-hot vectors, they utilize shallow CNNs and Wasserstein GAN (WGAN) losses with weight clipping.
  • Self-Regularization: Unlike many image-to-image GANs (such as CycleGAN), code repair is not a strict "one-to-one" mapping; there are often many valid ways to fix a single bug. To ensure the repair remains faithful to the original program's logic, the authors introduce two novel loss functions:
  • Autoencoder Loss ($\mathcal{L}_{AUTO}$): Forces the model to leave already-correct code unchanged.
  • Frequency Loss ($\mathcal{L}_{FREQ}$): Ensures the frequency of tokens in the output matches the input, preventing the model from hallucinating entirely new, unrelated logic.
  • Curriculum Learning: The model is trained progressively, starting with very short snippets of code and gradually advancing to longer, more complex sequences.

Key Insights and Architectural Decisions

Simpler is Better for Discriminators In the "soft-output" GAN space, a complex discriminator often becomes too powerful too quickly. Instead of evaluating the underlying sequence logic, it tends to focus on the "texture" of the probability distribution. The authors discovered that a shallow, 1-layer CNN actually provided a much better, more stable training signal for the generator than deeper networks.

Comparison of loss ratios and filter weights across different WGAN architectures.

The "Non-Bijective" Problem Most unsupervised translation assumes a one-to-one mapping (e.g., translating English to French). Code repair, however, is often "one-to-many" (one bug can have many valid fixes) or "many-to-one." This fundamental difference is why the authors abandoned the popular "cycle-consistency" approach often used in image translation in favor of their custom self-regularization techniques.

Pre-training Matters The authors found that pre-training the generator as a "de-noising autoencoder"—teaching it to reconstruct clean code from artificially noisy versions—gave the GAN a necessary head start before adversarial training began.

Datasets and Key Results

The authors evaluated their approach on three distinct tasks, ranging from toy sorting algorithms to Context-Free Grammar (CFG) sequences, and finally to SATE IV, a large-scale dataset of C/C++ synthetic code examples containing vulnerabilities from 116 different classes (e.g., buffer overflows, double frees).

The proposed GAN approach performed remarkably close to supervised "seq2seq" models that had the distinct advantage of seeing paired data:

  • SATE IV (Vulnerability Repair): On the paired dataset, the GAN achieved a BLEU-4 score of 90.3, highly competitive with the supervised baseline of 96.4.
  • Unpaired Success: When tested on a truly unpaired dataset—where the model never saw a repair for a specific bug during training—the GAN maintained a strong BLEU-4 score of 81.3.
  • Qualitative Success: The paper showcases instances where the GAN successfully fixed complex C++ vulnerabilities, such as adding a missing argument to a sprintf call or removing a "double free" error where the same memory was being freed twice.

Limitations and Future Work

While promising, the authors acknowledge several limitations. First, BLEU scores, which measure text similarity, are imperfect metrics for code; a "fixed" program might look textually different from the ground truth but still be functionally correct. Second, while the model handles snippets up to 300 tokens, real-world software projects involve massive files and complex inter-dependencies that current sequence-to-sequence models struggle to grasp. Finally, while the "soft-output" trick avoids the instability of Reinforcement Learning (like SeqGAN), handling discrete outputs in GANs remains an open research area with room for more robust techniques.

Summary updated

BibTeX
@inproceedings{harer2018learning,
  author = {Jacob Harer and Onur Ozdemir and Tomo Lazovich and Christopher P. Reale and Rebecca L. Russell and Louis Y. Kim and Peter Chin},
  title = {{Learning to Repair Software Vulnerabilities with Generative Adversarial Networks}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2018},
}
Figure