[Paper Review] Code Security Vulnerability Repair Using Reinforcement Learning with Large Language Models
This paper proposes a reinforcement learning (RL)-based method, SecureCode, that enhances code security in LLM-generated C/C++ code by combining syntactic and semantic rewards to prioritize adding security measures like null checks and buffer overflow protections. Unlike supervised fine-tuning, which favors functional similarity and overlooks security, the RL approach achieves superior repair performance on real-world vulnerabilities by explicitly rewarding secure code additions while preserving functionality.
With the recent advancement of Large Language Models (LLMs), generating functionally correct code has become less complicated for a wide array of developers. While using LLMs has sped up the functional development process, it poses a heavy risk to code security. Code generation with proper security measures using LLM is a significantly more challenging task than functional code generation. Security measures may include adding a pair of lines of code with the original code, consisting of null pointer checking or prepared statements for SQL injection prevention. Currently, available code repair LLMs generate code repair by supervised fine-tuning, where the model looks at cross-entropy loss. However, the original and repaired codes are mostly similar in functionality and syntactically, except for a few (1-2) lines, which act as security measures. This imbalance between the lines needed for security measures and the functional code enforces the supervised fine-tuned model to prioritize generating functional code without adding proper security measures, which also benefits the model by resulting in minimal loss. Therefore, in this work, for security hardening and strengthening of generated code from LLMs, we propose a reinforcement learning-based method for program-specific repair with the combination of semantic and syntactic reward mechanisms that focus heavily on adding security and functional measures in the code, respectively.
Motivation & Objective
- To address the critical gap in LLM-generated code where functional correctness is prioritized over security, leading to widespread vulnerabilities.
- To overcome the limitations of supervised fine-tuning (SFT), which minimizes cross-entropy loss and thus ignores security-critical code changes.
- To develop a reinforcement learning framework that explicitly rewards the addition of security measures (e.g., null checks, buffer validation) in code repairs.
- To ensure functional correctness while enhancing security by combining syntactic and semantic rewards in the RL training objective.
- To evaluate the method on real-world vulnerable code examples and demonstrate its effectiveness in repairing security flaws that SFT models miss.
Proposed method
- The method employs a large language model (CodeGen2) fine-tuned via reinforcement learning for code repair, with a custom reward function that balances security and functionality.
- The reward function combines CodeBLEU (syntactic similarity) and BERTScore (semantic similarity) to evaluate both structural and meaning-preserving correctness.
- A dedicated syntactic reward component explicitly encourages the insertion of security measures such as null pointer checks and memory bounds validation.
- The RL agent is trained to generate repairs that maintain functional equivalence while adding minimal, targeted security lines to vulnerable code.
- The training process uses feedback from unit tests and code analysis to validate functional correctness, while the reward function penalizes omission of security-critical lines.
- The approach is evaluated on three real-world C/C++ vulnerability cases involving buffer overflows and logical errors in I/O handling.
Experimental results
Research questions
- RQ1Can a reinforcement learning-based approach outperform supervised fine-tuning in repairing security vulnerabilities in LLM-generated code?
- RQ2Does a hybrid reward function combining syntactic and semantic rewards improve the detection and insertion of security measures in code repairs?
- RQ3How effective is the RL model in repairing vulnerabilities that require adding new lines (e.g., null checks, bounds checks) rather than removing code?
- RQ4To what extent does the model preserve functional correctness while prioritizing security enhancements over mere functional similarity?
- RQ5Can the proposed method detect and repair logical vulnerabilities in I/O handling that are missed by standard SFT models?
Key findings
- The RL-based model significantly outperformed supervised fine-tuning in repairing vulnerabilities that require adding security measures, such as null pointer checks and buffer overflow protections.
- In Case Study 1, the SFT model failed to add the critical null-termination line, while the RL model successfully inserted it, demonstrating superior security-aware repair.
- In Case Study 2, the RL model correctly added a memory region validation check to prevent buffer overflows, whereas the SFT model reproduced the original vulnerable code without changes.
- In Case Study 3, both models removed the problematic lines, but the RL model was more effective in scenarios requiring code addition, highlighting its advantage in proactive security hardening.
- The combination of CodeBLEU and BERTScore in the reward function enabled better alignment with both syntactic structure and semantic intent, improving repair quality beyond token-level metrics.
- The results show that RL with security-focused rewards can break the cycle of LLMs propagating insecure code by learning to prioritize security over functional similarity.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.