Sequence decision and probability sampling guided vulnerability detection statement level interpretable method
Through the methods of sequence decision-making and probability sampling guidance, combined with explorers and navigators, statement-level interpretation results related to vulnerabilities are extracted, which solves the problems of lack of interpretability and high interpretation cost of vulnerability detection methods in the existing technology, and achieves high efficiency ,Accurate vulnerability pattern understanding and interpretation.
Patent Information
- Application Number
- CN202410730438.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-06
- Publication Date
- 2024-09-24
- Estimated Expiration
- 2044-06-06
AI Technical Summary
Existing vulnerability detection methods based on deep learning and large language models can predict the probability that code contains vulnerabilities at a coarse-grained level such as files, functions, and code segments. However, they lack interpretability and cannot explain which statements have an important impact on model predictions as vulnerabilities. , and the explanation cost is high and it focuses on local grammatical elements, making it impossible to understand the vulnerability pattern from a global perspective.
A statement-level interpretable method for vulnerability detection guided by sequence decision-making and probability sampling is used to quickly and accurately extract statement-level interpretation results related to vulnerabilities by combining an explorer and a learning-based navigator. The navigator is used to learn vulnerability semantic information in the training phase. , guiding the explorer to dynamically update the set of candidate vulnerability-related statements to explain why the model predicts the code as a vulnerability.
It achieves efficient and accurate statement-level vulnerability explanation, can understand vulnerability patterns from a global perspective, reduces explanation costs, is suitable for a variety of training scenarios, without modifying the model to be explained, and improves the efficiency and accuracy of vulnerability detection and explanation based on DL and LLM models. sex.
Smart Images

Figure CN118690346A
Abstract
Description
Technical Field
[0001] The present invention relates to an interpretable method for vulnerability detection results, and in particular to a sentence-level interpretable method for vulnerability detection guided by sequence decision and probability sampling. Background Art
[0002] Deep learning based (DL-based) vulnerability detection technology has made significant progress and has been widely explored in sequence-based, tree-based, and graph-based code representation and detection models. In recent years, large language models (LLMs) have shown excellent performance in language-related tasks. Therefore, some researchers have also begun to apply LLMs to code vulnerability detection tasks (Hanif H, Maffeis S. Vulberta: Simplified source code pre-training for vulnerability detection. In 2022 International joint conference on neural networks (IJCNN) 2022 Jul 18 (pp. 1-8). IEEE.). However, most of the current DL-based vulnerability detection methods and LLM-based vulnerability detection methods can only predict the probability of code containing vulnerabilities at coarse-grained levels such as files, functions, and slice code segments, and are not interpretable, and cannot explain which statements have an important impact on the model's prediction of the code as containing vulnerabilities.
[0003] In recent years, some methods for interpreting DL-based vulnerability detection models have emerged. For example, Zou et al. proposed a model interpretation method based on heuristic search, which masks tokens in the code, compares the changes in model predictions before and after masking, and uses heuristic search to obtain a set of tokens that have a significant impact on model predictions (Zou D, Zhu Y, Xu S, Li Z, Jin H, Ye H. Interpreting deep learning-based vulnerability detector predictions based on heuristic searching. ACM Transactions on Software Engineering and Methodology (TOSEM). 2021 Mar 10; 30 (2): 1-31.). Another vulnerability detection method with fine-grained interpretations, IVDetect (Li Y, Wang S, Nguyen TN. Vulnerability detection with fine-grained interpretations. In Proceedings of the 29th ACM Joint Meeting on European Software Engineering Conference and Symposium on the Foundations of Software Engineering 2021 Aug 20 (pp. 292-303)), uses GNN explainer to mask the edges in the graph-based sample representation. By comparing the differences in the detection results before and after masking, the importance of each edge can be obtained, and the important edges can be synthesized into a PDG (program dependency graph) subgraph related to the vulnerability to explain the detection results. Although the above methods can provide explanations for DL-based vulnerability detection models, they still have two shortcomings: (1) Since these explanation methods need to search for code elements that have an important impact on the model's prediction of the code as vulnerable code through feedback and difference analysis of the detection results, they require high explanation costs and are time-consuming. (2) They focus more on explaining local grammatical elements in isolated single samples and cannot understand vulnerability patterns from a global perspective. Summary of the invention
[0004] The purpose of the present invention is to provide a statement-level interpretable method for vulnerability detection guided by sequential decision and probability sampling, which is used to interpret DL-based and LLM-based vulnerability detection models. For the vulnerability code detected by the detection model, the goal of the present invention is to extract a set of contextual statements with dependencies related to the vulnerability to explain how they work together to cause the model to predict the code as a vulnerability code, so as to assist developers to better understand the vulnerability pattern and its cause. Specifically, the method uses an explorer that combines sequential decision and probability sampling and a learning-based navigator to quickly and accurately obtain the statement-level vulnerability interpretation results of the model to be explained. Among them, the learning-based navigator reviews all vulnerability samples in the training phase and learns the vulnerability semantic information contained therein. After the training is completed, a reference probability can be provided to the explorer to guide the explorer to quickly search and dynamically update the candidate vulnerability-related statement set, thereby extracting the best contextual statement combination related to the vulnerability, that is, the vulnerability pattern, from a global perspective. The present invention can be flexibly applied to existing vulnerability detection models, and is suitable for two training scenarios with or without fine-grained labels.
[0005] The objective of the present invention is achieved through the following technical solutions:
[0006] A sentence-level interpretable vulnerability detection method guided by sequential decision making and probability sampling includes the following steps:
[0007] Step 1: Split the vulnerability code to be interpreted into statements. The explorer traverses the statements in the code in order. The code is recorded as C = {c1,…,c i ,…,c L}, c i is the code statement, L is the number of statements contained in the code;
[0008] Step 2: The explorer uses the code representation network in the detection model to be explained to generate a decision vector based on the candidate vulnerability pattern and the current statement;
[0009] Step 3: The navigator returns the probability of the current statement being associated with the vulnerability based on the decision vector, and then the explorer performs probabilistic sampling based on the vulnerability-related probability generated by the navigator;
[0010] Step 4: The explorer makes decisions based on the sampling and updates the set of candidate vulnerability-related statements;
[0011] Step 5: Using the label information and the set of candidate vulnerability related statements obtained in step 4, calculate the gain value and expected gradient, and adjust the navigator through back propagation until the gain value no longer increases in multiple consecutive rounds of training, and the training ends;
[0012] Step 6: Use the explorer and the trained navigator to explain the vulnerability detection results of the explainable model.
[0013] Compared with the prior art, the present invention has the following advantages:
[0014] (1) This paper proposes a new statement-level interpreter that can be used to interpret vulnerability detection models based on LLM and DL. The interpreter uses a learning-based navigator to guide an explorer that combines sequential decision making and probabilistic sampling to improve the accuracy and efficiency of interpretation.
[0015] (2) This paper designs a general interpretation framework that can be flexibly and efficiently applied to most vulnerability detection models based on DL and LLM models. The framework consists of an explorer and a learnable navigator. The navigator can be trained with or without fine-grained labels, without making any modifications to the model to be interpreted. The code semantic information required by the interpreter to interpret the model can be obtained with the help of the code representation network in the vulnerability detection model to be interpreted. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] Figure 1 It is a flowchart of the sentence-level interpretable method for vulnerability detection guided by sequence decision and probability sampling of the present invention.
[0017] Figure 2 is an example of vulnerable code. DETAILED DESCRIPTION
[0018] The technical solution of the present invention is further described below in conjunction with the accompanying drawings, but is not limited thereto. Any modification or equivalent replacement of the technical solution of the present invention without departing from the spirit and scope of the technical solution of the present invention should be included in the protection scope of the present invention.
[0019] The present invention provides a statement-level interpretable method for vulnerability detection guided by sequential decisions and probability sampling. First, the statements in the code are traversed line by line, and decisions are made for each statement in turn according to the order of the statements. The explorer samples the probability of each statement returned by the navigator being associated with the vulnerability. In order to consider the vulnerability pattern from a global perspective, the explorer uses the code representation network in the vulnerability detection model to extract semantic information from the current statement and the determined candidate vulnerability-related statements. Next, the navigator calculates the probability of each statement being associated with the vulnerability based on the decision vector learned from the semantic information. Then, the explorer performs probability sampling based on the vulnerability probability, and adds the selected statements based on the sampling decision to the candidate vulnerability-related statement set, while recording its probability of being associated with the vulnerability. Once the number of statements in the candidate vulnerability-related statement set exceeds the specified threshold, the statement with the lowest vulnerability prediction probability is removed from the set, and a combination of candidate vulnerability-related statements can be obtained after the exploration is completed. During the training process, the target gain value and its expected gradient are calculated based on the candidate vulnerability-related statement combination, and the network parameters of the navigator are updated by back propagation. As Figure 1 As shown, the method specifically comprises the following steps:
[0020] Step 1: Split the vulnerability code to be interpreted into statements. The explorer traverses the statements in the code in order. The code is recorded as C = {c1,…,c i ,…,c L}, c i is the code statement, and L is the number of statements contained in the code.
[0021] Step 2: The explorer uses the code representation network in the detection model to be explained to generate a decision vector based on the candidate vulnerability pattern and the current statement. The specific steps are as follows;
[0022] Step 21: Change the current code statement to i The code representation network is sent to the detection model to be interpreted to obtain the vector representation of the current code statement, denoted as x i ;
[0023] Step 22: Combine the candidate vulnerability related statement set sampled by the explorer in the previous i-1 step with the current statement to obtain the candidate vulnerability pattern, denoted as V' i , the candidate vulnerability pattern is sent to the code representation network to generate the corresponding vector representation, denoted as h i It should be noted that if i=1, that is, when the explorer traverses the first statement, since sampling has not started, the candidate vulnerability related statement set is an empty set, that is, the candidate vulnerability pattern V'1 only contains the current statement c1.
[0024] Step 23: Change the current code statement to c i The vector representation x i and candidate vulnerability pattern V' i The vector representation h i Splicing, we can get the decision vector d i , that is, d i =x i ⊕h i .
[0025] Step 3: The navigator returns the probability of the current code statement being associated with the vulnerability based on the decision vector, and then the explorer performs probabilistic sampling based on the vulnerability-related probability generated by the navigator. The specific steps are as follows:
[0026] Step 31: The navigator determines the decision vector d i Predict the current code statement c i The probability associated with the vulnerability is denoted by p i , the specific calculation formula is as follows:
[0027] π θ (d i )=[pi (s i =0|d i ;θ),p i (s i =1|d i ;θ)] T
[0028] =[σ(W*d i +b),1-σ(W*d i +b)] T
[0029] Among them, π θ is the probability calculation function of the navigator, θ = {W, b} is a learnable parameter, W is a learnable weight vector, b is a bias, s i is the sampling result of the explorer, d i is the decision vector and σ is the activation function.
[0030] Step 32: The explorer performs probabilistic sampling based on the vulnerability-related probabilities generated by the navigator. Specifically, the explorer performs probabilistic sampling based on p i In the {0,1} space, for the code statement c in the statement sequence i Sampling is performed and the sampling results are expressed as s i Indicates that s i 1 means s i Related to the vulnerability, i 0 means statement c i Nothing to do with vulnerabilities.
[0031] Step 4: The explorer makes decisions based on the sampling and updates the set of candidate vulnerability related statements. The specific steps are as follows:
[0032] When i =0, indicating code statement c i Not related to the vulnerability, the set of vulnerability-related statements does not change; when s i = 1, if the number of statements in the vulnerability-related statement set does not exceed the threshold, the code statement c i Add to the vulnerability related statement combination. If the number of statements in the candidate vulnerability related statement set exceeds the threshold, delete the statement including code c i The sentence with the lowest vulnerability-related probability among all candidate vulnerability-related sentences.
[0033] Step 5: Using the label information and the set of candidate vulnerability related statements obtained in step 4, calculate the gain value and expected gradient, and adjust the navigator through back propagation until the gain value no longer increases in multiple consecutive rounds of training. The training is completed. The specific steps are as follows;
[0034] Step 51: The goal of method training is to maximize the objective function. The specific calculation formula of the objective function is as follows:
[0035]
[0036] Where J is the objective function, S = {s1,…,s i ,…,s L} is a decision sequence consisting of the explorer sampling results, P = {p1,…,p i ,…,p L} is the probability sequence provided by the navigator, G(S) is the target gain value, which can measure the quality of the explanation provided by the explorer based on the decision sequence S, is the expected value of the target gain value. Intuitively, the higher G(S), the higher the quality of the explanation.
[0037] Step 52: Calculate the gradient of the objective function. Fundamentally, we want to find a navigator with parameters θ such that updating the parameters θ will affect the effectiveness of the navigator and thus the objective function J. The derivative of the objective function with respect to the parameters θ is as follows:
[0038]
[0039] Step 53: Calculate the target gain value. There are two ways to calculate the target gain value, as follows:
[0040] Step 531: If the training data set contains fine-grained vulnerability statement labels, the present invention may use statement line coverage (LC) as the target value, which is calculated as follows:
[0041]
[0042] Among them, G1(S) is the target gain value calculated by the first method, and LC is the line coverage, that is, the percentage of vulnerable statements in the predicted vulnerability pattern to all vulnerable statements. U is the sequence of vulnerable statements recorded by the group truth label in the training set, so |S∩U| can calculate the number of vulnerable statements contained in the predicted vulnerability pattern. LC∈[0,1], intuitively speaking, the more vulnerable statements a vulnerability pattern contains, the larger LC is.
[0043] Step 532: In the absence of fine-grained labels, the present invention uses the detection results of the model to be explained to calculate the target gain value. The calculation of this gain value is based on the intuition that the evaluation of the vulnerability sample by the detection model is based on the vulnerability-related statements, and masking the vulnerability-related statements will cause a significant change in the detection results of the detection model. Therefore, the present invention uses the difference between the probability predicted by the detection model for the original sample and the probability predicted by the sample after masking the vulnerability-related statements as the target gain value. The calculation formula is as follows:
[0044] G2(S)=DNet(C)-DNet(CC S )
[0045] Among them, G2(S) is the target gain value calculated by the second method, C is the original code, and C S CC is the vulnerability related statement predicted by the explorer. S To cover the samples behind the vulnerability related statements, DNet represents the vulnerability detection model to be explained.
[0046] Step 6: Use the explorer and the trained navigator to explain the vulnerability detection results of the explainable model.
[0047] Example:
[0048] by Figure 2 Taking the code vulnerability shown as an example, the statement marked with "-" in front is the vulnerable statement, and the statement marked with "+" is the patch statement to fix the vulnerability. The vulnerability is described as "the code does not check the data type, and a remote attacker may obtain sensitive information by using (1) string or (2) array data type instead of numeric data type". Therefore, the patched statement will check the data type pointed to by var2 through the function Z_TYPE_PP, and directly using var2 without checking will lead to a vulnerability. First, the present invention uses an explorer to traverse the statements in the code line by line in sequence, and then uses a navigator to calculate the probability of each statement being associated with the vulnerability. Then, the explorer uses probability sampling to update the extracted vulnerability-related statements. Finally, the vulnerability-related statements given are as follows. Figure 2 The statements circled in the box are shown in the figure. Rank 1 to rank 5 represent the order of statements determined by vulnerability probability.
Claims
1. A sentence-level interpretable vulnerability detection method guided by sequential decision making and probability sampling, characterized by The method comprises the following steps: Step 1: Split the vulnerability code to be interpreted into statements. The explorer traverses the statements in the code in order. The code is recorded as C = {c1,…,c i ,…,c L }, c i is the code statement, L is the number of statements contained in the code; Step 2: The explorer uses the code representation network in the detection model to be explained to generate a decision vector based on the candidate vulnerability pattern and the current statement; Step 3: The navigator returns the probability of the current statement being associated with the vulnerability based on the decision vector, and then the explorer performs probabilistic sampling based on the vulnerability-related probability generated by the navigator; Step 4: The explorer makes decisions based on the sampling and updates the set of candidate vulnerability-related statements; Step 5: Using the label information and the set of candidate vulnerability related statements obtained in step 4, calculate the gain value and expected gradient, and adjust the navigator through back propagation until the gain value no longer increases in multiple consecutive rounds of training, and the training ends; Step 6: Use the explorer and the trained navigator to explain the vulnerability detection results of the explainable model.
2. According to claim 1, the sentence-level interpretable method for vulnerability detection guided by sequential decision making and probability sampling is characterized in that The specific steps of step 2 are as follows: Step 21: Change the current code statement to i The code representation network is sent to the detection model to be interpreted to obtain the vector representation of the current code statement, denoted as x i ; Step 22: Combine the candidate vulnerability related statement set sampled by the explorer in the previous i-1 step with the current statement to obtain the candidate vulnerability pattern, denoted as V' i , the candidate vulnerability pattern is sent to the code representation network to generate the corresponding vector representation, denoted as h i , if i = 1, that is, when the explorer traverses the first statement, since sampling has not started, the candidate vulnerability related statement set is an empty set, that is, the candidate vulnerability pattern V'1 only contains the current statement c1; Step 23: Change the current code statement to c i The vector representation x i and candidate vulnerability pattern V' i The vector representation h i Splicing, we can get the decision vector d i , that is, d i =x i ⊕h i .
3. The sentence-level interpretable method for vulnerability detection guided by sequential decision making and probability sampling according to claim 1 is characterized in that The specific steps of step 3 are as follows: Step 31: The navigator determines the decision vector d i Predict the current code statement c i The probability associated with the vulnerability is denoted by p i , the specific calculation formula is as follows: p θ (d i )=[p i (s i =0|d i ;θ),p i (s i =1|d i (i)] T =[σ(W*d i +b),1-σ(W*d i +b)] T Among them, π θ is the probability calculation function of the navigator, θ = {W, b} is a learnable parameter, W is a learnable weight vector, b is a bias, s i is the sampling result of the explorer, d i is the decision vector, σ is the activation function; Step 32: Explorer according to p i In the {0,1} space, for the code statement c in the statement sequence i Sampling is performed and the sampling results are expressed as s i Indicates that s i 1 means s i Related to the vulnerability, i 0 means statement c i Nothing to do with vulnerabilities.
4. The sentence-level interpretable method for vulnerability detection guided by sequential decision making and probability sampling according to claim 1 is characterized in that The specific steps of step 4 are as follows: When i =0, indicating code statement c i Not related to the vulnerability, the set of vulnerability-related statements will not be changed; When i = 1, if the number of statements in the vulnerability-related statement set does not exceed the threshold, the code statement c i Add to the vulnerability related statement combination. If the number of statements in the candidate vulnerability related statement set exceeds the threshold, delete the statement including code c i The sentence with the lowest vulnerability-related probability among all candidate vulnerability-related sentences.
5. The sentence-level interpretable method for vulnerability detection guided by sequential decision making and probability sampling according to claim 1 is characterized in that The specific steps of step 5 are as follows: Step 51: Calculate the objective function. The specific calculation formula of the objective function is as follows: Where J is the objective function, S = {s1,…,s i ,…,s L } is a decision sequence consisting of the explorer sampling results, P = {p1,…,p i ,…,p L } is the probability sequence provided by the navigator, G(S) is the target gain value, is the expected value of the target gain value; Step 52: Calculate the gradient of the objective function. The derivative of the objective function with respect to the parameter θ is as follows: Step 53: Calculate the target gain value. The specific calculation method is as follows: Step 531: If the training data set contains fine-grained vulnerability statement labels, the statement line coverage is used as the target value, which is calculated as follows: Among them, G1(S) is the target gain value calculated by the first method, LC is the line coverage, U is the vulnerability statement sequence recorded by the group truth label in the training set, LC∈[0,1]; Step 532: In the absence of fine-grained labels, the difference between the probability predicted by the detection model for the original sample and the probability predicted for the sample after shielding the vulnerability-related statements is taken as the target gain value, and the calculation formula is as follows: G2(S)=DNet(C)-DNet(C-C S ) Among them, G2(S) is the target gain value calculated by the second method, C is the original code, and C S CC is the vulnerability related statement predicted by the explorer. S To cover the samples behind the vulnerability related statements, DNet represents the vulnerability detection model to be explained.
Citation Information
Patent Citations
Mobile application cross-platform reinforcement learning traversal test technology based on depth image understanding
CN114138653A
Interpretable sequence data modeling method and system
CN116562336A
Internet of things (IoT) identifying system and associated methods
US10826902B1
Cited By
Web application program bug PoC generation method based on large language model
CN120030553A
