arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2302.05319cs.CRcs.LGcs.PLcs.SE
收藏

面向代码的大语言模型:安全强化与对抗测试

Large Language Models for Code: Security Hardening and Adversarial Testing

ETH Zurich(苏黎世联邦理工学院)

Jingxuan He, Martin Vechev

AI总结:

针对大语言模型生成代码安全性不足的问题,提出基于学习的SVEN方法,通过属性连续向量实现受控代码生成,在不改动模型权重的同时有效实现安全强化与对抗测试,且功能正确性接近原模型。

摘要:

大语言模型(large LMs)正越来越多地在大规模代码库上训练,并被用于生成代码。然而,大语言模型缺乏安全意识,且被发现频繁生成不安全的代码。本研究从两个重要维度研究大语言模型的安全性:(i)安全强化,旨在提升大语言模型生成安全代码的可靠性;(ii)对抗测试,试图从对抗视角评估大语言模型的安全性。我们通过提出一项名为受控代码生成的新安全任务来解决这两个问题。该任务是参数化的,以一个二元属性为输入,引导大语言模型生成安全或不安全的代码,同时保留大语言模型生成功能正确代码的能力。我们提出了一种名为SVEN的新型基于学习的方法来解决该任务。SVEN利用属性特定的连续向量引导程序生成向给定属性靠拢,且无需修改大语言模型的权重。我们的训练流程通过在代码的不同区域施加专门的损失项来优化这些连续向量,所使用的是我们精心整理的高质量数据集。我们的大量评估表明,SVEN在实现强安全控制方面非常有效。例如,拥有27亿参数的最先进CodeGen大语言模型生成安全代码的比例为59.1%。当我们使用SVEN对该大语言模型进行安全强化(或对抗测试)时,该比例显著提升至92.3%(或下降至36.8%)。重要的是,SVEN在功能正确性上与原始大语言模型水平相当。

英文摘要:

Large language models (large LMs) are increasingly trained on massive codebases and used to generate code. However, LMs lack awareness of security and are found to frequently produce unsafe code. This work studies the security of LMs along two important axes: (i) security hardening, which aims to enhance LMs' reliability in generating secure code, and (ii) adversarial testing, which seeks to evaluate LMs' security at an adversarial standpoint. We address both of these by formulating a new security task called controlled code generation. The task is parametric and takes as input a binary property to guide the LM to generate secure or unsafe code, while preserving the LM's capability of generating functionally correct code. We propose a novel learning-based approach called SVEN to solve this task. SVEN leverages property-specific continuous vectors to guide program generation towards the given property, without modifying the LM's weights. Our training procedure optimizes these continuous vectors by enforcing specialized loss terms on different regions of code, using a high-quality dataset carefully curated by us. Our extensive evaluation shows that SVEN is highly effective in achieving strong security control. For instance, a state-of-the-art CodeGen LM with 2.7B parameters generates secure code for 59.1% of the time. When we employ SVEN to perform security hardening (or adversarial testing) on this LM, the ratio is significantly boosted to 92.3% (or degraded to 36.8%). Importantly, SVEN closely matches the original LMs in functional correctness.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑