S ec C oder: Towards Generalizable and Robust Secure Code Generation (2024)

Abstract: After large models (LMs) have gained widespread acceptance in code-related tasks, their superior generative capacity has greatly promoted the application of the code LM. Nevertheless, the security of the generated code has raised attention to its potential damage. Existing secure code generation methods have limited generalizability to unseen test cases and poor robustness against the attacked model, leading to safety failures in code generation. In this paper, we propose a generalizable and robust secure code generation method SecCoder by using in-context learning (ICL) and the safe demonstration. The dense retriever is also used to select the most helpful demonstration to maximize the improvement of the generated code’s security. Experimental results show the superior generalizability of the proposed model SecCoder compared to the current secure code generation method, achieving a significant security improvement of an average of 7.20% on unseen test cases. The results also show the better robustness of SecCoder compared to the current attacked code LM, achieving a significant security improvement of an average of 7.74%. Our analysis indicates that SecCoder enhances the security of LMs in generating code, and it is more generalizable and robust.

## introduction after large models (lms) @xcite achieved significant success, it has promoted the development of many coderelated works such as code summarization @xcite , code repair @xcite , code generation @xcite . nevertheless, the widespread use of lms in coderelated tasks has raised significant safety concerns. @xcite investigated the security of the code generated by github copilot @xcite generated insecure code: generated secure code: int value; if (@xmath0; } else { @xmath1; } int value; if (@xmath2; } else { @xmath3; } code generation prompt: int getvaluefromarray(int *array, int len, int index) { int value; // get the value at the specified index of the array 2023) and found that about 40% are vulnerable. siddiq and santos (2022) presented a manually curated dataset for code security evaluation. about 90% of the code snippets generated by the lms are vulnerable when manual inspection is used to check for security. the vulnerability poses a significant obstacle to code lms' application in securitysensitive domains. to mitigate the vulnerabilities, the method of secure code generation has attracted increasing attention. figure 1 illustrates the secure code generation from common weakness enumeration (cwe) @xcite serves as a broadly accepted category system for vulnerabilities. 1 thus far, extensive research has been conducted on enhancing the security of lms @xcite @xcite . given the differences in security policies between the natural language processing (nlp) and the code, some safe alignment methods are specifically designed for code lms @xcite . unfortunately, two crucial features of the secure code generation method have been ignored, which would severely compromise safety in practical applications. the first is the generalizability to the unseen test cases. @xcite proved that simply finetuning can inadvertently degrade the safety of lms even without malicious intent. @xcite proposed that mismatched generalization is one of the critical failure modes of safety alignment. it occurs when safety training does not generalize to a domain for which capabilities exist. compared to nlp, mismatched generalization is more prevalent in code generation since there are many kinds of vulnerabilities in code. for instance, the cwe @xcite has over 600 categories of vulnerabilities. the limited number of vulnerabilities in the secure code generation training dataset may lead to mismatched generalization in application @xcite . therefore, the lack of generalizability could cause safety failures, which limits the application of the secure code generation method. the second is the robustness against the attacked model. there are many well-designed attacks on lms @xcite @xcite . the experiments in @xcite showed that their attack could not be easily defended using simple prompt perturbations without external knowledge. this indicates that the basic input preprocessing defenses are not robust against the attack. therefore, a more powerful secure code generation method that is robust against such attacks is needed to generate more secure code in real-world applications. to address the above challenges, in this work, we propose seccoder, a generalizable and robust secure code generation approach. specifically, sec-coder guides lms to adapt swiftly to unseen test cases with the demonstration by leveraging the power of in-context learning (icl) @xcite @xcite @xcite ability. additionally, sec-coder enhances the robustness of secure code generation by providing an extra security codebase separately from the attacked model to guarantee the safe of the demonstration. seccoder retrieves the most helpful safe demonstration by using the retrieval capacity of the lms to maximize seccoder's effectiveness. we employ several kinds of code lms on a broad range of common vulnerabilities in the cwe @xcite to validate seccoder's generalizability and robustness. first, when evaluating the proposed model seccoder on the unseen test cases, the 12.07% average increase in the security reveals seccoder's generalizability. second, seccoder is more secure on unseen test cases than the state-ofthe-art secure code generation method sven sec @xcite and the improvement of the security is 7.20% on average, which reveals the generalizability of seccoder is better than the existing method. last, the security of the attacked code lm is increased by 7.74% on average by using sec-coder, which reveals the robustness of seccoder. these results clearly demonstrate the power of sec-coder. we also verify the functional correctness of sec-coder since it is supposed to preserve the original lm's usefulness. we found that the functional correctness of seccoder is almost the same as the original lm despite not adopting any specific mechanisms to preserve the utility. it is a clear contrast to the existing method @xcite , which carefully designed the mechanism to preserve the utility and paid a heavy price for the trade-off between the utility and the security. our finding could inspire other researchers to find a more efficient and straightforward mechanism to preserve the utility of the lm during security hardening. our contributions. our main contributions can be summarized as follows: in-context learning. as model sizes and corpus sizes have expanded @xcite @xcite , lms have exhibited the powerful icl ability, the capability to learn a new task from a handful of contextual examples. extensive research has demonstrated that lms can accomplish many complicated tasks via icl @xcite . in contrast to supervised training, icl represents a training-free learning paradigm. this approach significantly decreases computational expenses associated with adjusting the model to novel tasks. therefore, icl is beneficial for the generalizability. retriever. the retriever has attracted significant concerns recently @xcite @xcite @xcite since it could assist people to retrieve the desired item automatically. there are two kinds of retrievers. one is the sparse retriever, such as bm25 @xcite , which uses lexical matching, and the other is the dense retriever, which uses semantic matching. with the development of pre-trained models, there are increasingly off-the-shelf dense retrievers, such as instructor @xcite . instructor is fine-tuned to efficiently adapt to diverse downstream tasks without additional training by jointly embedding the inputs and the task. several coderelated tasks adopt retriever such as code autocompletion @xcite , code summarization @xcite , and code generation @xcite . nevertheless, there is no widely agreed criterion for selecting a perfect demonstration. the existing research on retrieval strategies for secure code generation is still limited. ## discussions as shown in the experiments, the proposed method seccoder is beneficial to the security of code lms, and it is generalizable and robust. compared to the existing method, it doesn't need to be retrained when meeting new vulnerabilities. the existing method sven @xcite needs to specially distinguish the security and function regions to preserve the functional correctness of the code lms, and it doesn't mention how to solve the particular case that the entire program is securitysensitive. nevertheless, seccoder could preserve the correctness without any extra operation. therefore, seccoder has a broader range of applications. in addition, seccoder can be combined with other security hardening methods to further improve the security of code lms. it is worth investigating in the future. ## conclusion in this paper, we highlight the limitation of the generalizability to unseen test cases and the robustness against the attacked code lms on the application of the existing secure code generation method. we introduce the method seccoder to enhance the security of code generated by various lms. by leveraging the capacity of the pre-trained dense retriever to retrieve the relevant secure code as the safe demonstration and the ability of icl to incorporate the new vulnerability fix pattern, seccoder exhibits remarkable generalizability and robustness in secure code generation. interestingly, the utility has been preserved without additional effort, which is also a distinct advantage compared to existing secure code generation method. our extensive evaluation demonstrates the generalizability and the robustness of seccoder over various kinds and several sizes of code lms. moreover, seccoder could be used with other secure code generation methods to further enhance the generalizability.