A clone vulnerability detection method, system and device
By combining code measurement and hash value screening with patch function verification, this method solves the shortcomings of existing clone vulnerability detection methods in terms of detection speed and type, and achieves fast and accurate clone vulnerability detection for large-scale software source codes.
Patent Information
- Application Number
- CN202410936047.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-12
- Publication Date
- 2024-09-10
- Estimated Expiration
- 2044-07-12
AI Technical Summary
Existing cloning vulnerability detection methods have shortcomings in detection speed and type, and cannot quickly and effectively identify large-scale cloning vulnerabilities, especially complex cloning vulnerabilities, and traditional methods have limited support for detection of lexical changes.
By obtaining the code metrics of the target function to be detected, the code line hash value and metric distance are used to screen potential vulnerable functions, and combined with the patch function for verification, the detection accuracy and efficiency are improved.
It achieves rapid detection of Type-1, Type-2 and Type-3 cloning vulnerabilities, improves detection accuracy and efficiency, reduces false positives, and is suitable for cloning vulnerability detection of large-scale software source code.
Smart Images

Figure CN118468296B
Abstract
Description
Technical Field
[0001] The present application belongs to the field of information security, and more specifically, to a cloning vulnerability detection method, system and device. Background Art
[0002] Vulnerabilities are software security errors caused by logical errors, irregular writing, and lack of security testing during the software development process. The emergence of vulnerabilities exposes software to security risks such as remote control, information leakage, or denial of service. It poses a great threat to software security. Therefore, timely detection of vulnerabilities is crucial to maintaining software security. Existing vulnerability detection technologies include manual inspection, static analysis, fuzz testing, symbolic execution, and deep learning-based methods. However, traditional technologies such as manual inspection, static analysis, fuzz testing, and symbolic execution rely on rules formulated by vulnerability experts and have extremely high requirements for vulnerability-related expertise. Vulnerability detection methods based on deep learning rely on the number, accuracy, and diversity of labeled data sets. These methods are not suitable for large-scale vulnerability detection.
[0003] As software becomes more open source, code reuse becomes a common method in software development. However, the reuse of code containing vulnerabilities will result in the emergence of a large number of duplicate vulnerabilities. These vulnerabilities have similar or even identical code logic and vulnerability characteristics, and are also called clone vulnerabilities. Clone vulnerabilities mainly include the following according to the degree of change when the code containing the vulnerability is reused: Type-1: clones that are completely identical except for comments and blank lines, Type-2: clones that rename variables and functions, Type-3: clones that insert and delete statements, and Type-4: clones that use different syntax to achieve the same semantics. The first two categories are considered simple clones, and the latter two are considered complex clones. Clone vulnerabilities are widely present in real code environments, but general vulnerability detection methods do not have the ability to quickly detect large-scale clone vulnerabilities, and clone vulnerability detection technology is needed to quickly identify them.
[0004] Common clone vulnerability detection methods extract text, lexical, grammatical or semantic features containing rich vulnerability information from known vulnerabilities. Those clone codes that match these features are regarded as potential vulnerabilities. Fast clone vulnerability detection methods can achieve large-scale vulnerability scanning, but do not support the detection of vulnerabilities with lexical changes, nor do they consider patch information. Therefore, only simple clone vulnerabilities can be detected. Some methods capture similarities by considering the semantic information of the vulnerable function. Although the consideration of semantic information can certainly increase the detection of complex clone vulnerabilities, it reduces the detection speed a lot. In fact, as the size of the open source community increases, the amount of code becomes more and more complex. Processing a large amount of code to be detected may require a lot of time and resources. If the speed of vulnerability discovery cannot keep up with the speed of vulnerability emergence, it will lead to further spread of vulnerabilities. Summary of the invention
[0005] In view of the defects of the prior art, the purpose of this application is to provide a clone vulnerability detection method, system and device, aiming to solve the problem that the existing clone vulnerability detection method can detect a limited number of clone vulnerability types in a set of detection processes and has a slow detection speed.
[0006] To achieve the above objectives, in a first aspect, the present application provides a cloning vulnerability detection method, comprising:
[0007] Obtaining code metrics of a target function to be detected, wherein the code metrics include multiple metrics;
[0008] When the number of metric indicators exceeding the corresponding threshold among the multiple metric indicators exceeds a preset ratio, determine the similarity of the code line hash value between the target function and at least one preset vulnerability function; when the similarity between the target function and any vulnerability function is greater than a preset value, determine all candidate vulnerability functions whose similarity to the target function is greater than the threshold;
[0009] Obtain at least one patch function related to all the candidate vulnerability functions, and determine the corresponding added lines and deleted lines; when the target function contains relevant deleted lines and / or does not contain relevant added lines, determine the code metric distance between the target function and the corresponding candidate vulnerability function and / or patch function, and when the code metric distance between the target function and the vulnerability function is less than a distance threshold, the target function is detected as the corresponding vulnerability function; the distance threshold is a preset metric distance or a code metric distance between the target function and the corresponding patch function.
[0010] It should be noted that due to the high similarity between Type-1, Type-2 and most Type-3 clone vulnerability functions, the code metrics between multiple clone vulnerabilities of the same vulnerability function are very similar, and the proportion of identical row hash values is also very high. Therefore, the vulnerability detection method provided by the present application can increase the detection of Type-3 clone vulnerabilities; the accuracy of detecting Type-1 and Type-2 clone vulnerabilities is high. When the similarity between Type-3 clone vulnerabilities and vulnerability functions is high, the present application can also detect Type-3 clone vulnerabilities well.
[0011] It is understandable that the present application first uses code measurement to filter out most of the normal target functions; then based on row hashing, the vulnerability functions with high similarity to the target function are screened, and only the candidate vulnerability functions screened out need to be compared later, which can reduce a lot of detection workload and greatly improve detection efficiency. Furthermore, since the similarity between the patch function and the vulnerability function is relatively high, if the detection and judgment is only based on the code measurement distance between the target function and the vulnerability function, there may be a situation where the target function is actually a patch function but is mistakenly detected as a vulnerability function; therefore, when comparing the code measurement distance, the present application will also compare with the corresponding patch function, which can improve the accuracy of vulnerability detection.
[0012] For example, the preset vulnerability functions mentioned in this application can be a set of vulnerability functions that you want to retrieve, or a set of known vulnerability functions in a vulnerability database, etc. Further, the patch functions mentioned in this application are patch functions corresponding to each vulnerability function, that is, the patch functions are the repaired vulnerability functions. Specifically, there can be one or more patch functions corresponding to a vulnerability function.
[0013] In a possible implementation, the code metric distance between two functions is: a weighted sum of relative differences of at least one metric among multiple metric indicators of the two functions.
[0014] In one possible implementation, the code metric distance between two functions is calculated using the following formula:
[0015]
[0016] in, Represents the code metric distance between function A and function B; Represents the set of metrics used to calculate the code metric distance. The metric m belongs to ; represents the value of the metric m of function A, represents the value of the metric m of function B; Represents the weight of the metric m;
[0017] When calculating the code metric distance from the target function to the vulnerability function, function A is the target function and function B is the vulnerability function; when calculating the code metric distance from the target function to the patch function, function A is the target function and function B is the patch function.
[0018] In a possible implementation, the similarity of the code line hash value between the target function and at least one preset vulnerable function is determined by the following steps:
[0019] Hash the target function and the vulnerable function at the code behavior granularity to obtain the hash value of each code line in the function;
[0020] The similarity of the code line hash values between the two functions is determined based on the proportion of code lines with the same hash value between the two functions.
[0021] In a possible implementation, hashing the target function at the code behavior granularity includes the following steps:
[0022] Performing an abstract operation on the objective function to obtain an abstracted objective function; the abstract operation is: replacing different components of the function with different symbols;
[0023] The abstracted target function is hashed at a code line granularity to obtain a hash value of each code line of the target function.
[0024] It should be noted that, for Type-2 cloning vulnerabilities, abstracting the target function can avoid the impact of variable and function renaming on vulnerability detection, making the detection accuracy of the present application for Type-2 cloning vulnerabilities higher.
[0025] In a possible implementation, the different components include at least one of the following components: a type identifier, a constant, a parameter, a local variable, and a function name.
[0026] In a possible implementation, the deleted lines refer to code lines that appear in the vulnerability function but not in the patch function, and the added lines refer to code lines that do not appear in the vulnerability function but appear in the patch function.
[0027] In a possible implementation, before obtaining the code metrics of the target function to be detected, the following is further included:
[0028] The target function is normalized; the normalization includes: deleting code comments and empty code lines.
[0029] Obviously, for Type-1 cloning vulnerabilities, if the objective function is normalized before detection, the detection accuracy of this type of cloning vulnerability can be significantly improved.
[0030] In a second aspect, the present application provides a cloning vulnerability detection system, comprising:
[0031] A code metric acquisition unit, used to acquire code metrics of a target function to be detected, wherein the code metrics include multiple metric indicators;
[0032] A vulnerability function screening unit, configured to determine the similarity of the code line hash value between the target function and at least one preset vulnerability function when the number of the metric indicators exceeding the corresponding threshold among the multiple metric indicators exceeds a preset ratio, and to determine all candidate vulnerability functions whose similarity to the target function is greater than the threshold when the similarity between the target function and any vulnerability function is greater than a preset value;
[0033] A vulnerability function detection unit is used to obtain at least one patch function related to all the candidate vulnerability functions and determine corresponding added lines and deleted lines; when the target function contains relevant deleted lines and / or does not contain relevant added lines, determine the code metric distance between the target function and the corresponding candidate vulnerability function and / or patch function; when the code metric distance between the target function and the vulnerability function is less than a distance threshold, the target function is detected as the corresponding vulnerability function; the distance threshold is a preset metric distance or a code metric distance between the target function and the corresponding patch function.
[0034] In a third aspect, the present application provides an electronic device comprising: at least one memory for storing programs; and at least one processor for executing the programs stored in the memory. When the program stored in the memory is executed, the processor is used to execute the method described in the first aspect or any possible implementation of the first aspect.
[0035] In a fourth aspect, the present application provides a computer-readable storage medium, which stores a computer program. When the computer program runs on a processor, the processor executes the method described in the first aspect or any possible implementation of the first aspect.
[0036] In a fifth aspect, the present application provides a computer program product. When the computer program product runs on a processor, the processor executes the method described in the first aspect or any possible implementation of the first aspect.
[0037] It can be understood that the beneficial effects of the second to fifth aspects mentioned above can be found in the relevant description of the first aspect mentioned above, and will not be repeated here.
[0038] In general, the above technical solutions conceived by the present application have at least the following beneficial effects compared with the prior art:
[0039] The present application provides a clone vulnerability detection method, system and device, which can be used for source code clone vulnerability detection methods for large-scale real software, using code metrics to capture program details without the need for complex program analysis. Code metrics are quantitative metrics used to evaluate various aspects of code quality and performance. Different code metrics quantify code from different perspectives, providing insights into program behavior to some extent. By providing specific data and indicators, it helps to better understand and optimize the performance and quality of software. Thereby achieving detection of Type-1, Type-2 and Type-3 clone vulnerabilities.
[0040] The present application provides a clone vulnerability detection method, system and device, which fully utilizes the code metric threshold as a means to exclude a large number of normal functions in the effective filtering stage. The extraction of code metrics in the metric filtering stage is very simple and fast. Only relying on the design of the threshold can it filter out those target functions that are unlikely to be vulnerabilities at an extremely fast speed, so as to reduce the burden of subsequent work and speed up the overall speed of the tool, thereby realizing the rapid detection of heavy cloning vulnerabilities. Furthermore, the second step of the above-mentioned filtering stage only uses the hash calculation of the code line. Due to its simple structure and very fast extraction and processing speed, it can realize rapid filtering and quickly retain candidate vulnerability functions for each suspicious target function. Therefore, the subsequent verification stage no longer needs to compare the target function with all vulnerability functions one by one, saving a lot of detection time.
[0041] The present application provides a clone vulnerability detection method, system and device. In the vulnerability detection verification stage, while considering the vulnerability function, the patch function is also considered to avoid misdetecting the patch function as a vulnerability function, reduce false positives, and improve the accuracy of clone vulnerability detection while ensuring the detection speed. Furthermore, the present application avoids the impact of changes in parameter names on detection through abstract operations, which can effectively improve the detection accuracy of Type-2 clone vulnerabilities; and before performing detection screening, the present application can first normalize the target function to improve the detection accuracy of Type-1 clone vulnerabilities.
[0042] In summary, the vulnerability detection method provided by the present application is independent of the language type of the source code, is applicable to any development language, and has strong versatility; the method of the present application does not require code compilation and has a wide detection range; the detection efficiency is high and many types of vulnerabilities can be detected, and Type-1 and Type-2 clone vulnerabilities can be detected with high precision, and a large part of Type-3 clone vulnerabilities can be accurately detected. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] Figure 1 is a flow chart of a cloning vulnerability detection method provided in an embodiment of the present application;
[0044] Figure 2It is a specific flow chart of the source code cloning vulnerability detection method based on code metrics provided by an embodiment of the present application;
[0045] Figure 3 This is a diagram of the architecture of a clone vulnerability detection system provided in an embodiment of the present application;
[0046] Figure 4 It is an architectural diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0047] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0048] The term "and / or" in this article is a description of the association relationship of associated objects, indicating that there can be three relationships. For example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone. The symbol " / " in this article indicates that the associated objects are in an or relationship, for example, A / B means A or B.
[0049] In the embodiments of the present application, words such as "exemplary" or "for example" are used to indicate examples, illustrations or descriptions. Any embodiment or design described as "exemplary" or "for example" in the embodiments of the present application should not be interpreted as being more preferred or more advantageous than other embodiments or designs. Specifically, the use of words such as "exemplary" or "for example" is intended to present related concepts in a specific way.
[0050] In the description of the embodiments of the present application, unless otherwise specified, "at least one" means one or more, for example, at least one vulnerability function refers to one or more vulnerability functions, etc.; at least one patch function refers to one or more patch functions, etc.
[0051] First, the technical terms involved in the embodiments of the present application are introduced.
[0052] (1) Vulnerable function
[0053] Vulnerable functions are those functions that may be exploited maliciously due to design flaws, programming errors or other reasons. These vulnerable functions may allow attackers to perform unauthorized operations, such as data leakage, service interruption or system control.
[0054] (2) Patch function
[0055] A patch function usually refers to a code snippet or update written by a developer to fix a vulnerable function. The purpose of a patch function is to make up for the shortcomings of the vulnerable function and enhance the security of the program.
[0056] The patch function mentioned in this application is the function after the vulnerable function is repaired.
[0057] Next, the technical solutions provided in the embodiments of the present application are introduced.
[0058] Figure 1 is a flow chart of the cloning vulnerability detection method provided in the embodiment of the present application; Figure 1 As shown, the following steps are included:
[0059] Step S101, obtaining code metrics of a target function to be detected, wherein the code metrics include multiple metrics;
[0060] Step S102, when the number of metric indicators exceeding the corresponding threshold among the multiple metric indicators exceeds a preset ratio, determine the similarity of the code line hash value between the target function and at least one preset vulnerability function, and when the similarity between the target function and any vulnerability function is greater than a preset value, determine all candidate vulnerability functions whose similarity to the target function is greater than the threshold;
[0061] It should be noted that all candidate vulnerability functions whose similarity with the target function is greater than a threshold value are determined, specifically: when any vulnerability function in the preset at least one vulnerability function has a similarity with the target function higher than a preset value, the target function is considered to be a suspected vulnerability function, and further identification and verification in step S103 are required. Since there are multiple possibilities for the preset at least one vulnerability function, all vulnerability functions with a similarity higher than the preset value can be retained as candidate vulnerability functions to perform subsequent identification and verification steps with the target function, i.e., step S103; and at this time, the vulnerability functions in the preset at least one vulnerability function whose similarity is not higher than the preset value are filtered, and subsequent identification and verification are not performed, which can reduce the computational workload and improve the detection efficiency.
[0062] Step S103, obtaining at least one patch function related to all the candidate vulnerability functions, and determining corresponding added lines and deleted lines; when the target function contains relevant deleted lines and / or does not contain relevant added lines, determining the code metric distance between the target function and the corresponding candidate vulnerability function and / or patch function, when the code metric distance between the target function and the vulnerability function is less than a distance threshold, the target function is detected as the corresponding vulnerability function; the distance threshold is a preset metric distance or a code metric distance between the target function and the corresponding patch function.
[0063] For example, a deleted line refers to a code line that appears in a vulnerable function but does not appear in a patch function, and an added line refers to a code line that does not appear in a vulnerable function but appears in a patch function.
[0064] It should be noted that the vulnerability detection method provided in this application is suitable for source code vulnerability detection.
[0065] This application extracts the code metric value of the target function and distinguishes whether the target function is a vulnerability function or a patch function by calculating the metric distance; the consideration of the patch function excludes a large number of false positives for the repaired function; and the metric threshold filtering method is used to filter out those target functions that are unlikely to be vulnerabilities at an extremely fast speed to reduce the burden of subsequent work and speed up the overall speed of the tool, thereby achieving rapid detection of cloned vulnerabilities.
[0066] Optionally, the similarity of the code line hash value between the target function and at least one preset vulnerable function is determined by the following steps:
[0067] Hash the target function and the vulnerable function at the code behavior granularity to obtain the hash value of each code line in the function;
[0068] The similarity of the code line hash values between the two functions is determined based on the proportion of code lines with the same hash value between the two functions.
[0069] Optionally, the target function is hashed at the code behavior granularity, including the following steps:
[0070] Performing an abstract operation on the objective function to obtain an abstracted objective function; the abstract operation is: replacing different components of the function with different symbols;
[0071] The abstracted target function is hashed at a code line granularity to obtain a hash value of each code line of the target function.
[0072] Specifically, for Type-2 clone vulnerabilities, abstract operations on the target function can avoid the impact of variable and function renaming on vulnerability detection, making the detection accuracy of the present application for Type-2 clone vulnerabilities higher.
[0073] For example, the different components include at least one of the following components: a type identifier, a constant, a parameter, a local variable, and a function name.
[0074] Further preferably, before obtaining the code metrics of the target function to be detected, the method further includes:
[0075] The target function is normalized; the normalization includes: deleting code comments and empty code lines.
[0076] It is understandable that for Type-1 clone vulnerabilities, if the objective function is normalized before detection, the detection accuracy of this type of vulnerability can be significantly improved.
[0077] In a further embodiment, the source code cloning vulnerability detection method based on code metrics provided by the present application is mainly divided into two stages: a filtering stage and a vulnerability identification stage. Figure 2 As shown, the steps included in each stage are as follows:
[0078] First, the purpose of the filtering stage is to filter normal functions, that is, functions with a low probability of containing vulnerabilities. It mainly includes two steps of filtering, metric filtering and row hash filtering; including:
[0079] Step S1: Analyze the target function code to obtain the corresponding code metrics, and determine whether the target function contains potential vulnerabilities based on the code metrics. If the values of the target function's code metrics that exceed a preset ratio all exceed their respective thresholds, it is considered that the target function contains potential vulnerabilities and needs further verification.
[0080] Optionally, before obtaining the code metrics, the objective function may be normalized.
[0081] Code metrics refer to software metrics used to quantify features such as complexity, coupling, and size to evaluate the quality of source code. Some metrics can reflect the understandability of the code, and low understandability increases the possibility of defects or vulnerabilities in the source code. Code metrics can include at least one metric; the above metric can be selected by those skilled in the art according to the situation.
[0082] In one of the embodiments, the code metrics mentioned in the above step S1 include metrics including at least one of the following metrics: LOC: number of lines of code, XMET: number of called functions, NEXP: number of expressions, LOOP: number of loops, NOS: number of statements, NOA: number of parameters, VDEC: number of variable declarations, NOPR: number of operators, NAND: number of operands, HVOC: Halstead program vocabulary length, HEFF: Halstead programming effort, HDIF: Halstead program difficulty, HVOL: Halstead program volume or capacity, CC: cyclomatic complexity, MI: maintainability index, NCLTRL: number of characters, NSLTRL: number of strings, NNLTRL: number of numbers, etc.
[0083] Among them, as an example, LOC and NEXP in the above-mentioned metrics can be selected as representatives to perform corresponding threshold comparison and code metric distance calculation. It can be understood by those skilled in the art that the more metrics selected, or the more relevant the selected metrics are to the vulnerability characteristics, the better the effect of the above-mentioned filtering will be. Therefore, those skilled in the art can determine the specific metrics to be selected according to needs.
[0084] As a further example, for example, a cyclomatic complexity CC of more than 30 indicates that there are structural problems in the code. Code with structural problems is generally more likely to contain vulnerabilities. Therefore, the present application uses code metrics to indicate whether a function contains vulnerabilities. The metric indicators of the above code metrics can be adjusted by those skilled in the art to more accurately detect vulnerabilities.
[0085] Step S2: Row hash filtering performs row-level hash matching on the target function and the vulnerability function to determine whether the target function contains potential vulnerabilities.
[0086] In one embodiment, the hash mentioned in step S2 is a function that compresses a message of any length into a message digest of a fixed length. This conversion is a compression mapping, and the space of hash values is usually much smaller than the space of inputs. Different inputs may hash to the same output, which is the hash value, and it is impossible to uniquely determine the input value from the hash value.
[0087] In a more specific embodiment, step S2 includes:
[0088] Step S21: Perform abstract operation on the target function that has passed step S1.
[0089] Step S22: hash the target function and the vulnerability function according to the code behavior granularity to obtain a hash value.
[0090] Step S23: Calculate the similarity based on the number of rows with the same hash value between the target function and the vulnerability function. If the similarity is higher than a threshold, it is considered that the target function contains a potential vulnerability and needs further verification.
[0091] In one embodiment, the abstract operation in step S21 replaces different components in the function with different symbols.
[0092] As an example, the type identifier in a function can be replaced by the 'TYPE' symbol; the constant in a function can be replaced by the 'LITR' symbol; the parameter in a function can be replaced by the 'PARM' symbol; the local variable in a function can be replaced by the 'LVAR' symbol; and the function name in a function can be replaced by the 'FUNC' symbol.
[0093] In one embodiment, in step S23, when comparing two sets a and b, the Jaccard similarity is determined by dividing the size of their intersection by the size of their union. This calculation captures the proportion of shared elements between the sets. Wherein, set a includes all row hash values of the target function, and set b includes all row hash values of the vulnerability function.
[0094] If the similarity is above a certain threshold, it is considered a match. The Jaccard similarity calculation method is as follows:
[0095]
[0096] Second, the purpose of the vulnerability identification phase is to further determine whether the retained functions have vulnerabilities. This mainly includes two steps of checking: deleting and adding rows and measuring distance; including:
[0097] Step S3: Check whether the target function contains vulnerability deletion lines and patch addition lines, and determine whether the target function contains potential vulnerabilities.
[0098] For example, step S3 includes the following steps:
[0099] Step S31: Get the added and deleted rows of the vulnerability and patch. Let Fv represent the vulnerability function, and let Fp represent the patch function after fixing the vulnerability. The deleted row refers to the row that appears in Fv but not in Fp, and the added row refers to the row that does not appear in Fv but appears in Fp.
[0100] Step S32: For the target function that has passed step S2, check whether the target function contains at least some of the preset deleted rows and does not contain at least some of the preset added rows. If so, it is considered that the target function contains potential vulnerabilities and needs further verification.
[0101] Step S4: Metric distance check calculates the distance from the target function to the vulnerability function and the patch function to determine whether the target function is closer to the vulnerability or the patch, and obtains the vulnerability detection result.
[0102] In one embodiment, step S4 is for each metric, the metric of function A The measure to function B The distance, divided by , and then multiply by the importance of the metric Finally, sum up all the calculated values to get the final distance from A to B The distance calculation formula is:
[0103]
[0104] When calculating the distance from the target function to the vulnerability function, function A is the target function and function B is the vulnerability function; when calculating the distance from the target function to the patch function, function A is the target function and function B is the patch function.
[0105] It should be noted that since the probability of Type-4 cloning vulnerabilities occurring in real code environments is low, and the existing technology will separately design corresponding detection methods for Type-4 cloning vulnerabilities, this application does not discuss the detection of this type of cloning vulnerabilities.
[0106] Figure 3 : is a diagram of the architecture of the clone vulnerability detection system provided in the embodiment of the present application; Figure 3 As shown, including:
[0107] A code metric acquisition unit 310 is used to acquire code metrics of a target function to be detected, wherein the code metrics include multiple metric indicators;
[0108] A vulnerability function screening unit 320 is used to determine the similarity of the code line hash value between the target function and at least one preset vulnerability function when the number of the metric indicators exceeding the corresponding threshold among the multiple metric indicators exceeds a preset ratio, and when the similarity between the target function and any vulnerability function is greater than a preset value, determine all candidate vulnerability functions having a similarity with the target function greater than the threshold;
[0109] The vulnerability function detection unit 330 is used to obtain at least one patch function related to all the candidate vulnerability functions and determine the corresponding added lines and deleted lines; when the target function contains relevant deleted lines and / or does not contain relevant added lines, determine the code metric distance between the target function and the corresponding candidate vulnerability function and / or patch function, and when the code metric distance between the target function and the vulnerability function is less than a distance threshold, the target function is detected as the corresponding vulnerability function; the distance threshold is a preset metric distance or a code metric distance between the target function and the corresponding patch function.
[0110] Optionally, the vulnerability function screening unit 320 hashes the target function and the vulnerability function at the code behavior granularity to obtain the hash value of each code line in the function; and determines the similarity of the code line hash values between the two functions based on the proportion of code lines with the same hash value between the two functions.
[0111] Specifically, hashing the target function at the granularity of code behavior includes: performing an abstract operation on the target function to obtain an abstracted target function; the abstract operation is: replacing different components of the function with different symbols; hashing the abstracted target function at the granularity of code behavior to obtain a hash value of each code line of the target function.
[0112] Optionally, before obtaining the code metrics of the target function to be detected, the code metric acquisition unit 310 normalizes the target function; the normalization includes: deleting code comments and empty code lines.
[0113] It should be understood that the above-mentioned system is used to execute the methods in the above-mentioned embodiments. The implementation principles and technical effects of the corresponding program units in the system are similar to those described in the above-mentioned methods. The working process of the system can refer to the corresponding process in the above-mentioned method and will not be repeated here.
[0114] Based on the method in the above embodiment, the embodiment of the present application provides an electronic device, such as Figure 4 As shown, the electronic device may include: a processor 410, a communication interface 420, a memory 430 and a communication bus 440, wherein the processor 410, the communication interface 420 and the memory 430 communicate with each other via the communication bus 440. The processor 410 may call the logic instructions in the memory 430 to execute the method in the above embodiment.
[0115] In addition, the logic instructions in the above-mentioned memory 430 can be implemented in the form of software functional units and can be stored in a computer-readable storage medium when sold or used as an independent product. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, or the part of the technical solution, can be embodied in the form of a software product, which is stored in a storage medium and includes a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present application.
[0116] Based on the method in the above embodiment, an embodiment of the present application provides a computer-readable storage medium, which stores a computer program. When the computer program runs on a processor, the processor executes the method in the above embodiment.
[0117] Based on the method in the above embodiment, an embodiment of the present application provides a computer program product. When the computer program product runs on a processor, the processor executes the method in the above embodiment.
[0118] It is understandable that the processor in the embodiment of the present application may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application specific integrated circuits (ASIC), field programmable gate arrays (FPGA) or other programmable logic devices, transistor logic devices, hardware components or any combination thereof. The general-purpose processor may be a microprocessor or any conventional processor.
[0119] The method steps in the embodiments of the present application can be implemented by hardware or by a processor executing software instructions. The software instructions can be composed of corresponding software modules, and the software modules can be stored in random access memory (RAM), flash memory, read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), registers, hard disks, mobile hard disks, CD-ROMs, or any other form of storage medium known in the art. An exemplary storage medium is coupled to the processor so that the processor can read information from the storage medium and write information to the storage medium. Of course, the storage medium can also be a component of the processor. The processor and the storage medium can be located in an ASIC.
[0120] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented by software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present application is generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions may be stored in a computer-readable storage medium or transmitted through the computer-readable storage medium. The computer instructions may be transmitted from a website site, computer, server or data center to another website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.). The computer-readable storage medium may be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrated. The available medium may be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid state disk (SSD)), etc.
[0121] It should be understood that the various numerical numbers involved in the embodiments of the present application are only used for the convenience of description and are not used to limit the scope of the embodiments of the present application.
[0122] It will be easily understood by those skilled in the art that the above description is only a preferred embodiment of the present application and is not intended to limit the present application. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present application shall be included in the scope of protection of the present application.
Claims
1. A clone vulnerability detection method, It is characterized in that include: Obtaining code metrics of a target function to be detected, wherein the code metrics include multiple metrics; When the number of metric indicators exceeding the corresponding threshold value among the multiple metric indicators exceeds a preset ratio, the similarity of the code line hash value between the target function and at least one preset vulnerability function is determined; when the similarity between the target function and any vulnerability function is greater than a preset value, all candidate vulnerability functions whose similarity to the target function is greater than the threshold are determined; the similarity of the code line hash value is determined by the following steps: hashing the target function and the vulnerability function at the code behavior granularity respectively to obtain the hash value of each code line in the function; determining the similarity of the code line hash value between the two functions according to the proportion of the number of code lines with the same hash value between the two functions; Obtain at least one patch function related to all the candidate vulnerability functions, and determine corresponding added lines and deleted lines; when the target function contains at least one of the deleted lines and / or does not contain at least one of the added lines, determine the code metric distance between the target function and the corresponding candidate vulnerability function and the corresponding patch function, and when the code metric distance between the target function and the vulnerability function is less than a distance threshold, the target function is detected as the corresponding vulnerability function; the distance threshold is the code metric distance between the target function and the corresponding patch function; the deleted line refers to a code line that appears in the vulnerability function but does not appear in the patch function, and the added line refers to a code line that does not appear in the vulnerability function but appears in the patch function; The code metric distance between two functions is: the weighted sum of the relative differences of at least one metric among multiple metric indicators of the two functions.
2. The method according to claim 1, It is characterized in that The code metric distance between two functions is calculated by the following formula: in, Represents the code metric distance between function A and function B; Represents the set of metrics used to calculate the code metric distance. The metric m belongs to ; represents the value of the metric m of function A, represents the value of the metric m of function B; Represents the weight of the metric m; When calculating the code metric distance from the target function to the vulnerability function, function A is the target function and function B is the vulnerability function; when calculating the code metric distance from the target function to the patch function, function A is the target function and function B is the patch function.
3. The method according to claim 1, It is characterized in that The step of hashing the target function at the code behavior granularity includes the following steps: Performing an abstract operation on the objective function to obtain an abstracted objective function; the abstract operation is: replacing different components of the function with different symbols; The abstracted target function is hashed at a code line granularity to obtain a hash value of each code line of the target function.
4. The method according to claim 3, It is characterized in that The different components include at least one of the following components: type identifier, constant, parameter, local variable and function name.
5. The method according to any one of claims 1 to 4, It is characterized in that Before obtaining the code metrics of the target function to be detected, it also includes: The target function is normalized; the normalization includes: deleting code comments and empty code lines.
6. A clone vulnerability detection system, It is characterized in that include: A code metric acquisition unit, used to acquire code metrics of a target function to be detected, wherein the code metrics include multiple metric indicators; A vulnerability function screening unit is used to determine the similarity of the code line hash value between the target function and at least one preset vulnerability function when the number of the metric indicators exceeding the corresponding threshold among the multiple metric indicators exceeds a preset ratio, and when the similarity between the target function and any vulnerability function is greater than a preset value, determine all candidate vulnerability functions whose similarity to the target function is greater than the threshold; the similarity of the code line hash value is determined by the following steps: hashing the target function and the vulnerability function at the code behavior granularity respectively to obtain the hash value of each code line in the function; determining the similarity of the code line hash value between the two functions according to the proportion of the number of code lines with the same hash value between the two functions; A vulnerability function detection unit is used to obtain at least one patch function related to all the candidate vulnerability functions, and determine the corresponding added lines and deleted lines; when the target function contains at least one of the deleted lines and / or does not contain at least one of the added lines, determine the code metric distance between the target function and the corresponding candidate vulnerability function and the corresponding patch function, and when the code metric distance between the target function and the vulnerability function is less than a distance threshold, the target function is detected as the corresponding vulnerability function; the distance threshold is the code metric distance between the target function and the corresponding patch function; the deleted line refers to the code line that appears in the vulnerability function but does not appear in the patch function, and the added line refers to the code line that does not appear in the vulnerability function but appears in the patch function; The code metric distance between two functions is: the weighted sum of the relative differences of at least one metric among multiple metric indicators of the two functions.
7. An electronic device, It is characterized in that include: at least one memory for storing a computer program; At least one processor is used to execute the program stored in the memory. When the program stored in the memory is executed, the processor is used to execute the method according to any one of claims 1 to 5.
