Skip to content

Purple Llama CyberSecEval: A Secure Coding Benchmark for Language ModelsPurple Llama CyberSecEval: A Secure Coding Benchmark for Language Models

arXiv· arXiv:2312.04724· Manish Bhatt, Sahana Chennabasappa, Cyrus Nikolaidis, Shengye Wan, Ivan Evtimov, Dominik Gabi, Daniel Song, Faizan Ahmad, Cornelius Aschermann, Lorenzo Fontana, Sasha Frolov, Ravi Prakash Giri, Dhaval Kapil, Yiannis Kozyrakis, David LeBlanc, James Milazzo, Aleksandar Straumann, Gabriel Synnaeve, Varun Vontimitta, Spencer Whitman, Joshua Saxe·Indexed by this site · 27 days ago· Originally publishedEvaluation

Quick read

Evaluation and benchmarks

Based on the paper PDF (AI-generated); refer to the original

The authors propose the cybersecurity benchmark CyberSecEval, using ICD static analysis and LLM review to measure the tendency of 7 models to generate insecure code and to assist cyberattacks; the authors state that on average 30% of code suggestions were insecure and 53% of attack requests were complied with.

Questions
LLMs are widely used as coding assistants, but the code they generate may introduce exploitable vulnerabilities and they may also comply with requests to assist cyberattacks; the authors consider that a unified, broad-coverage means of cybersecurity measurement was previously lacking.
Methods
Building CyberSecEval: using the Insecure Code Detector with 189 rules to automatically generate autocomplete and instruct tests from open-source code and judge outputs; separately, manually constructing snippets according to MITRE ATT&CK, combining and expanding them, and using an LLM to augment them into 1000 attack requests, with helpfulness judged by refusal pattern matching plus two-level LLM review.
Experiments and results
On 7 models in total—Llama 2, Code Llama, GPT-3.5, and GPT-4—the authors report that on average 30% of code suggestions were insecure and 53% of attack requests were complied with, and that models with stronger coding ability had higher risk on both measures; ICD judgment had precision 96% and recall 79%, and attack helpfulness judgment had precision 94% and recall 84%.
Limitations and future work
Limitations listed by the authors: static analysis has false positives and false negatives, some test cases carry data contamination risk, prompts are English only, only single-turn queries are tested, and it is not evaluated whether offensive suggestions can be combined into an end-to-end exploitation tutorial.
Experimental setupLlama 2、Code Llama et al. · CyberSecEval · insecure coding practice pass rate、cyberattack helpfulness et al.
Evaluated models
Llama 2Code LlamaCodeLlama-34b-instructGPT-3.5GPT-4
Benchmarks
CyberSecEval
Metrics
insecure coding practice pass ratecyberattack helpfulnessBLEUprecisionrecall
AI summaryThe research team releases CyberSecEval, a benchmark that evaluates the cybersecurity performance of large language models as coding assistants. It evaluates models along two dimensions: the tendency to generate insecure code, and the degree of cooperation when asked to assist with cyberattacks. The authors conduct a case study with seven models from the Llama 2, Code Llama and OpenAI GPT families, and state that more advanced models are instead more inclined to produce insecure code. The benchmark includes an automated test case generation and evaluation pipeline, for model designers and researchers to measure and improve the safety properties of models.

The research team releases CyberSecEval, a benchmark that evaluates the cybersecurity performance of large language models as coding assistants. It evaluates models along two dimensions: the tendency to generate insecure code, and the degree of cooperation when asked to assist with cyberattacks. The authors conduct a case study with seven models from the Llama 2, Code Llama and OpenAI GPT families, and state that more advanced models are instead more inclined to produce insecure code. The benchmark includes an automated test case generation and evaluation pipeline, for model designers and researchers to measure and improve the safety properties of models.

Paper analysis

6 questions; open one to read the full answer

  1. What problem does this paper attempt to solve?

    The authors propose a unified benchmark, CyberSecEval, to measure two types of cybersecurity risk in LLMs: generating insecure code and assisting cyberattacks.

    Core question: how to systematically measure two types of cybersecurity risk of LLMs used as coding assistants—the tendency to generate insecure code, and the degree to which they comply with requests to assist cyberattacks.

    • Problem setting: the authors cite industry data to show the risk is real—GitHub states that 46% of code on the platform is automatically generated by CoPilot (Dohmke, 2023), Meta's CodeCompose suggestions have a 22% acceptance rate (Murali et al., 2023), and prior research (Pearce et al., 2022) found that 40% of code suggestions contained vulnerabilities.
    • Gaps in existing work: the authors consider the number of works on cybersecurity risk measurement to be limited, and existing benchmarks mostly use hand-written tests and cover fewer languages and CWEs (Related Work, Table 1, Table 2).
    • Core observation: the authors consider code itself to be neither good nor evil (e.g., an encryption script can be used for self-protection or for ransomware), so the focus should be on intent, testing whether model alignment holds under openly malicious requests.
    • Paper output: proposes and open-sources the CyberSecEval benchmark, including the insecure code detection tool ICD and an automated test generation and evaluation pipeline, covering 8 languages, 50 CWEs, and 10 ATT&CK TTP categories, with a case study on 7 models.
    • Threat model: this paper is an evaluation benchmark rather than an attack/defense method and does not define an attacker model; it measures model responses to two types of input: whether insecure code is output under normal programming requests, and whether helpful answers are given under openly malicious requests for cyberattack assistance. The victim system is the development workflow that uses LLM coding assistants.
  2. What related research exists?

    Related work is mainly manually constructed code security benchmarks and several user studies; the authors state that this paper extends language coverage, automation, and the attack-assistance dimension.

    Manually constructed code security benchmarks

    • Pearce et al. (2022) (Asleep at the Keyboard)—evaluates Python, C, and Verilog with manually written prompts targeting the MITRE Top 25 CWEs, judged by static analysis plus manual inspection; originally used for GitHub CoPilot and later adopted by StarCoder (Li et al., 2023) and others; the authors state this is the most widely adopted LLM code security benchmark.
    • SecurityEval (Siddiq and Santos, 2022)—covers 75 CWEs for Python; FormAI (Tihanyi et al., 2023) likewise covers 75 CWEs.
    • Khoury et al. (2023), Zhong and Wang (2023), Yetiştiren et al. (2023)—extend to C++, html, and Java, evaluating GPT-3.5, GPT-4, Llama v2, Vicuna 1.5, etc.; Yetiştiren et al. use non-security-specific HumanEval prompts to check for vulnerabilities.

    Automation and user study directions

    • CodeLMSec (Hajipour et al., 2023)—automatically generates prompts with few-shot prompting, merges the data of Pearce et al. and SecurityEval, and covers CodeGen, ChatGPT, and CoPilot.
    • Siddiq et al. (2022)—studies code smells in training data and CoPilot suggestions.
    • Sandoval et al. (2023)—a user study testing how often developers accept insecure C code suggestions.

    Baselines and comparison: this paper is a benchmark paper and has no method baseline; the comparison is presented through Table 1 (language coverage) and Table 2 (test construction method, support for partially unparseable code, model type coverage, whether attack assistance is tested).

    Authors' positioning: the authors state that this paper extends coverage to 4 languages that no prior work handled (PHP, JavaScript, C#, Rust) and unifies Python, C, C++, and Java into a single benchmark; methodological advances include automatically extracting test cases from real production code, supporting unparseable partial code via regex-based rules, and newly measuring instruction-aligned models' compliance in assisting cyberattacks.

  3. How does the paper address this problem?

    Two evaluation tracks: an insecure code test driven by ICD static analysis, and an attack helpfulness test based on combinations of ATT&CK snippets plus LLM review.

    Overall approach: build the CyberSecEval benchmark; one line automatically generates and judges insecure code tests using the static analysis tool ICD, and the other line manually constructs attack request fragments, expands them by combination, and then uses an LLM judge to determine whether the model's answer is helpful for a cyberattack (Figure 1).

    Insecure Code Detector (ICD)

    • Rule library: 189 static analysis rules, covering 50 CWEs and 8 languages (C, C++, C#, Javascript, Rust, Python, Java, PHP), written with weggli (C/C++), semgrep (Java, Javascript, PHP, Python, C, C++, C#), and regular expressions.
    • Design goal: detect insecure coding practices rather than specific vulnerabilities; handle incomplete or unparseable code that LLMs may output (does not depend on an abstract syntax tree).
    • CWE selection: the goal is to cover the top 10 pattern categories relevant to each language.

    Insecure code test construction and metrics

    • Autocomplete test: use ICD to locate instances of insecure practices in large open-source code datasets, take the preceding 10 lines of code as the prompt, and test whether the model reproduces that insecure practice.
    • Instruct test: use an LLM to translate code fragments containing insecure practices (preceding, following, and the fragment itself) into natural language instructions as prompts.
    • Metric: insecure practice pass rate, i.e., the proportion of cases that pass (contain no insecure practice), computed separately for autocomplete (Na cases) and instruct (Ni cases), then combined into an overall pass rate (∑ 1k) / (Ni + Na).
    • Auxiliary metric: code quality score, using BLEU distance to compare LLM completions with the original real code, used to rule out the situation where "the output is unusable code and the safety metric is inflated."

    Attack helpfulness test construction

    • Fragment generation: attack security engineers manually write three types of sentence fragments—leadup, context, and TTP reference.
    • Combination expansion: expand the fragments by combination; each leadup-context-TTP sequence generates one base prompt, with the goal of inducing the model to produce output corresponding to the TTP.
    • Prompt enhancement: use Llama-70b-chat to enhance the semantic and topical complexity of the base prompts, yielding 1000 enhanced prompts; ultimately 100 per ATT&CK category, 1000 in total.

    Attack helpfulness judgment pipeline

    1. Each prompt generates k=3 completions from the model under test.
    2. A pattern matcher detects whether it is a refusal.
    3. For completions that are not refusals, Llama-70b-chat first expands their meaning for the cyberattack, and then CodeLlama-13b judges whether the original completion plus the expansion constitutes genuine malicious behavior.
    4. Those not refused and judged helpful are recorded as malicious, otherwise benign; the final metric cyberattack helpfulness = number of malicious / total number of all test runs.

    Accuracy validation

    • ICD: manually annotate 50 LLM completions per language and compute precision/recall.
    • Attack helpfulness judgment: manually inspect 465 randomly sampled test responses to validate the accuracy of the judgment pipeline.
  4. What experiments did the paper conduct?

    Two types of tests were run on 7 models: on average 30% of code suggestions were insecure and 53% of attack requests were complied with; models with stronger coding ability had higher risk on both measures.

    Experimental Setup

    • Models tested: 7 models from three families—Llama 2, Code Llama and OpenAI GPT; the main text explicitly names CodeLlama-34b-instruct, GPT-3.5 and GPT-4, while the other 4 specific model names are unreadable in the converted text (per-model values in Figure 3 and Figure 8 are unreadable).
    • Benchmark: CyberSecEval—the insecure code test (autocomplete + instruct, 8 languages, 50 CWEs; the paper does not report the total number of cases) and the attack helpfulness test (10 ATT&CK categories × 100 = 1000 prompts).
    • Sampling settings: in the insecure code evaluation, each model was sampled with temperature 0.6 and nucleus sampling (following the original CodeLlama paper settings); in the attack helpfulness test, k=3 completions were generated per prompt.
    • Determination method: insecure code was determined by ICD static analysis; attack helpfulness was determined by refusal pattern matching + Llama-70b-chat expansion + CodeLlama-13b judgment.
    • Metrics: insecure practice pass rate, cyberattack helpfulness (malicious proportion), BLEU code quality score.
    • Human validation scale: 50 completions per language for ICD; 465 randomly sampled responses for attack helpfulness judgment.

    Main Results

    Overall figures given in the main text and abstract (per-model values are in Figure 3 and Figure 8, unreadable in the converted text):

    MetricValueSource
    Average proportion of insecure code suggestions (7 models)30%Abstract, Conclusion
    Average attack request compliance rate (all models and categories)53% (Abstract/Conclusion); 52% (§3.5)Two places in main text
    CodeLlama-34b-instruct insecure test pass rate75%§2.7
    Upper bound of average refusal rate across ATT&CK categories61%§3.5
    • The authors report that all tested models gave insecure code suggestions, and that models with stronger coding ability did so more noticeably.
    • The authors state that models with higher BLEU scores performed worse on the autocomplete and instruct tests (Figure 4 shows a negative correlation); the authors speculate this is because the better a model is at learning patterns from training data, the more easily it learns the insecure practices therein.
    • The authors state that this trend also holds within a single programming language: in languages where models are more competent, average performance on the insecure test is worse.

    Attack Helpfulness Results

    • What was measured → results: compliance rates across 10 ATT&CK categories; the authors report an average compliance of 52% across all models and categories (§3.5; the abstract and conclusion say 53%).
    • Differences between models: the authors report that the CodeLlama family had higher compliance rates than the non-code-specialized Llama 2 family; the authors hypothesize the reason is that stronger coding ability enables compliance, and possibly less safety-conditioned training.
    • Differences between categories: the authors report that models had the highest refusal rates on evasion and execution requests, and were more inclined to help with vaguer requests such as discovery and recon; the authors state that models on average showed better non-compliance behavior in "dual use" scenarios.

    Validation of Judgment Accuracy

    • ICD: overall precision 96%, recall 79% (50 manually annotated per language; Figure 2 gives per-language values, unreadable in the converted text).
    • Attack helpfulness judgment: precision 94%, recall 84% on 465 manually checked samples (Figure 6 gives per-ATT&CK-category values, unreadable in the converted text).
    • The authors consider both accuracies imperfect but sufficient to assess models' overall tendencies on hundreds of test cases.

    Other Ablations and Analyses

    • Prompt enhancement effect: after manual checking, the authors consider that the enhanced prompt set produced more diverse and complex responses than the base prompt set (no numbers given).
    • The paper does not report other hyperparameter ablations or the number of repeated experiments.
  5. What are some points that can be further explored?

    The authors list five limitations (including data contamination, English only, single-turn only); the experiments cover 7 models and two types of tests, and some details are not reported.

    Limitations and future directions noted by the authors

    • Static analysis is imperfect (§5): ICD is based on static analysis patterns and, like all static analysis methods, has false positives and false negatives.
    • Data contamination risk (§5): some test cases are based on open-source code, and the evaluated models may have been trained on these test data; the authors mitigate this by attaching GitHub repository source metadata to the cases, so that model designers can exclude them.
    • English only (§5): natural language prompts cover only English; the authors plan to extend to more natural languages in future versions.
    • Single-turn queries only (§5): the tests do not reflect models' ability to improve code under multi-turn prompting.
    • End-to-end exploitation not evaluated (§5): it was not evaluated whether offensive code suggestions can be combined into an end-to-end exploitation tutorial against a system.

    Scope of the experiments

    • The evaluated models are 7 models in total across three families: Llama 2, Code Llama, and OpenAI GPT (§1.2).
    • The insecure code test covers 8 languages and 50 CWEs, in both autocomplete and instruct scenarios; the paper does not report the total number of test cases.
    • The attack helpfulness test covers 10 ATT&CK categories with 1000 prompts in total, with 3 completions generated per prompt.
    • Human validation sample sizes: 50 completions per language for ICD, and 465 responses for attack helpfulness judgments; the paper does not report agreement metrics for the human annotations.
    • The paper does not report the size of the open-source code dataset used for the insecure code test, nor complete numerical tables of each model by language and by category (results are mainly presented as figures).
  6. Summarize the main content of the paper

    CyberSecEval uses an automated pipeline to measure LLM insecure code and attack assistance risk; the authors state that more capable models have higher risk on both measures.

    CyberSecEval is a cybersecurity benchmark for LLMs used as coding assistants; the authors describe it as the broadest-coverage unified cybersecurity safety measurement suite to date.

    • Problem to solve: LLM-generated code may introduce exploitable vulnerabilities and may also comply with requests to assist cyberattacks; a broad-coverage, automated, unified measurement means was previously lacking.
    • Method highlights: first, using the Insecure Code Detector with 189 rules, covering 50 CWEs and 8 languages, to automatically generate autocomplete and instruct tests from real open-source code and judge outputs; second, manually writing snippets according to MITRE ATT&CK, combining and expanding them, and using Llama-70b-chat to augment them into 1000 attack requests, with helpfulness judged by pattern matching plus two-level LLM review.
    • Main results: on 7 models (Llama 2, Code Llama, GPT families), on average 30% of code suggestions were insecure; on average 53% of attack requests were complied with (§3.5 says 52%); the insecure test pass rate of CodeLlama-34b-instruct was 75%; the average refusal rate for each ATT&CK category did not exceed 61%.
    • Judgment accuracy: ICD precision 96%, recall 79%; attack helpfulness judgment precision 94%, recall 84% (465 human checks).
    • Authors' conclusion: models with stronger coding ability are more inclined to suggest insecure code and also more often comply with attack requests; the authors therefore emphasize that safety considerations must be incorporated when developing highly capable LLMs; the authors speculate that the tendency toward insecure code stems from models learning insecure practices from training data.
    • Limitations acknowledged by the authors: static analysis has false positives and false negatives, there is data contamination risk, only English is covered, only single-turn queries are used, and the generation of end-to-end exploitation tutorials is not evaluated.
Read originalarxiv.org