Hidden patch positioning method based on single-class learning
Through single-class learning technology, the security indicators are extracted from patch messages and variant samples are generated. The encoder is trained to perform cryptographic patch recognition using loss function, which solves the limitations of existing methods in code changes and data imbalance, improves the accuracy and efficiency of cryptographic patch recognition, and enhances the software security.
Patent Information
- Application Number
- CN202510519771.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-24
- Publication Date
- 2025-08-01
AI Technical Summary
The existing hidden patch identification methods have significant limitations in accuracy and applicability, and it is difficult to effectively deal with complex code changes and data imbalances, which has affected software security.
Using single-class learning technology, security indicators are extracted from patch messages, positive sample variants and difficult negative samples are generated through data augmentation technology, and patch message encoder is trained using specially designed loss functions to perform single-class predictions to identify hidden patches.
It realizes high-precision identification of hidden security patches, which can resist the complexity of code changes and data imbalance problems, significantly improves the accuracy and efficiency of hidden patch positioning, reduces manual intervention, and improves software security.
Smart Images

Figure CN120408642A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of open-source software supply chain security, and particularly relates to a method for locating hidden patches. Background Art
[0002] With the wide application of open-source software, the number of vulnerabilities in open-source software has also shown a significant growth trend. In order to effectively mitigate the security risks brought by open-source software vulnerabilities, it is particularly important to publicly disclose and apply security patches in a timely manner. However, existing research has shown that the repair of a considerable number of vulnerabilities is not publicly disclosed but is carried out in a hidden manner. This phenomenon of hidden patches mainly stems from the requirements of the responsible disclosure model, that is, a patch must be released before publicly disclosing vulnerability information, and software maintainers avoid publicly reporting vulnerabilities due to reputation considerations. Therefore, a large number of unpublicized patches have left many software versions in an unrepaired state for a long time, increasing the risk of being exploited by attackers. This lack of transparency not only gives users a false sense of security but also exacerbates the problem due to the complexity of the software supply chain, resulting in an increase in unrepaired versions. Therefore, accurately and timely identifying hidden security patches is crucial for improving software security.
[0003] Existing methods for identifying hidden patches are mainly divided into two categories: code analysis-based methods and patch message analysis-based methods. Code analysis-based methods identify security-related code segments by analyzing relevant code changes in detail. However, since the code changes involved in security patches are usually complex and span multiple lines and files, accurately identifying these patches faces great challenges. In addition, the accuracy and applicability of such methods are limited by the code characteristics of specific programming languages, restricting their effectiveness in practical applications.
[0004] Another common method is to identify security patches by analyzing patch messages in software repositories (i.e., the text descriptions accompanying potential patch submissions). Patch messages usually record in detail information such as the background of the patch, the developer's understanding of the problem, and the potential impact on the project, avoiding the complexity of directly analyzing patch code. However, existing patch message-based classification models have limitations in dealing with the data imbalance problem, that is, the number of non-security patches far exceeds that of security patches, resulting in the model being biased towards identifying non-security patches. Although some studies use undersampling techniques to balance the dataset, this method still cannot completely solve the data imbalance problem, affecting the generalization ability of the model in practical scenarios.
[0005] In summary, existing methods for identifying hidden patches have significant limitations in terms of accuracy and applicability, and it is difficult to effectively handle complex code changes and data imbalance problems. Therefore, a new method is needed to improve the accuracy and efficiency of hidden patch identification to better ensure software security. Summary of the Invention
[0006] The object of the present invention is to provide a one-class learning-based hidden patch localization method with high robustness and accuracy, so as to overcome the defects and deficiencies of the prior art in identifying hidden patches.
[0007] The present invention captures the common features of security patches through one-class learning technology and uses these features to identify hidden patches. Specifically, the present invention first extracts security indicators from patch messages to capture the security semantics in the patches; secondly, positive sample variants and hard negative samples are generated through data augmentation technology to improve the discrimination ability of the model; then, a patch message encoder is trained using a specially designed loss function to enable it to convert patch messages into vectors in the latent space; finally, a one-class prediction method is used to determine whether the patch is a security patch.
[0008] The following introduces the related definitions of security indicators, positive sample variants and hard negative samples:
[0009] Security indicator, a sentence or group of sentences containing security semantics extracted from patch messages, used to identify security-related information in patches.
[0010] Positive sample variant, a variant of the security indicator generated through text restatement and noise injection techniques, while maintaining the original semantics unchanged.
[0011] Hard negative sample, a sample extremely similar to a security patch mined from non-security patches, used to improve the discrimination ability of the model.
[0012] The one-class learning-based hidden patch localization method provided by the present invention specifically comprises the following steps:
[0013] (1) Data extraction: Extract a set of relevant and useful data for security patch identification, namely security indicators, from the original security patch messages, and mark the corresponding security semantics;
[0014] (2) Data augmentation: Perform data augmentation on the extracted security indicators to generate positive sample variants and hard negative samples; the positive sample variant refers to a sample that introduces a certain change while retaining its original semantics, to help the one-class learning model discover the commonalities among positive samples; the hard negative sample refers to an instance that is extremely similar to a security patch but is a non-security patch, thereby improving the model's ability to distinguish between these two types of patches;
[0015] (3) Encoder fine-tuning: Based on the augmented full security indicator dataset, train a patch message encoder to learn security patch features through two specially designed loss functions, and this encoder converts patch messages into vectors in the latent space;
[0016] (4) Single-class prediction: Use the fine-tuned encoder for single-class prediction. By calculating the average distance between the patch message vector and the known security indicator vector, determine whether the patch is a security patch.
[0017] The following further describes each step:
[0018] 1. Data extraction;
[0019] Specifically include:
[0020] (1) Semantic partitioning of security fixes
[0021] A security indicator is a text fragment containing security semantic information: one or a group of sentences mentioning security semantics. These security semantics include security notes, impacts, exploitabilities, and four types of security weaknesses, corresponding to twelve security fix measures. The semantic partitioning is as Figure 2 shown. The semantics of security fixes are divided into three levels. The first level is one first-level label indicating whether it is a security patch (Security Patch). The second level is seven second-level labels, namely Security Note, Impact, Exploitability, and four types of security weaknesses: Out-of-bound, Injection, Pointer, and Authority. The third level is twelve third-level labels corresponding to the four types of security weaknesses (each type of security weakness corresponds to 3 third-level labels).
[0022] The following elaborates on the basic semantics and characteristics of security fix descriptions.
[0023] Security fix hint information. There is security hint information in some security fix descriptions, such as the part in red font in Figure 3 . This part contains the security fix hint information CVE-2018-1093, which can clearly indicate that this fix is a security fix. Other such hint information includes "Security Fix", "Vulnerability", etc.
[0024] Security weakness information. This information contains a description of a clear security vulnerability or anomaly, such as the part in blue font in Figure 3 . This part details that the anomaly type of this fix is out-of-bounds read, that is, an out-of-bounds read vulnerability. The security weakness information in the fix description text can also effectively guide the model to identify and classify security fix description texts.
[0025] Security fix method. This information contains abstract description information of the fix method, such asFigure 3 The purple font part in it. This part describes in detail that the repair method is to add a boundary check. The consequence of not performing a boundary check is usually an out-of-bounds phenomenon, which triggers an overflow vulnerability. Therefore, the repair method information is also an important semantics of the security repair description text.
[0026] Security impact information. Similarly, some security repair description texts also contain descriptions of security impacts. This information mainly includes the impacts and consequences of the vulnerabilities corresponding to the security repairs, such as Figure 3 shown in the green font. The developer added a description of the security impact in the security repair description, explaining that if the security issue is not repaired, it will cause the program to crash. This is also important for the security repair description text.
[0027] Attacker exploitation information. This information focuses on describing how attackers exploit this vulnerability for attacks. The appearance of such information in the security repair description text mainly focuses on how attackers exploit and trigger this vulnerability, such as Figure 3 shown in the orange font. The information in this part can also assist the security repair model to complete the identification and classification tasks. Therefore, the attacker exploitation information can also be used as one of the important semantics of the security repair description text.
[0028] (2) Security indicator definition
[0029] Based on the above summary of the security repair semantic information, the security indicator of the present invention corresponds to the security semantics (as shown in Figure 2 ). It includes a total of 7 secondary tags and 12 tertiary tags under four security weaknesses. The 7 secondary tags respectively include Security Note, Impact, Exploitability, and four security weaknesses: Out-of-bound vulnerability, Injection vulnerability, Pointer vulnerability, and Authority vulnerability. The following will explain the four security weaknesses and their corresponding secondary tags in turn.
[0030] Overflow vulnerabilities are program crashes caused by out-of-bounds access, usually manifested as a value exceeding the length limit of its type identifier, or exceeding the boundary when allocating memory and performing read and write operations. Methods to fix such vulnerabilities include Bound Check, Buffer Resizing, and Safer Function or Type. Bound Check prevents out-of-bounds vulnerabilities by checking the boundary range before an operation; Buffer Resizing controls the range by adjusting the buffer size to avoid out-of-bounds read vulnerabilities; Safer Function or Type replaces unsafe functions with safer versions, such as replacing strcpy with strncpy to avoid overflows.
[0031] Injection vulnerabilities are security issues that cause uncontrollable behavior in a program due to unsafe user input or uploaded files. Methods to fix such vulnerabilities include User Input Sanitization, File Type Check, and Sink Removal. User Input Sanitization prevents injection vulnerabilities by rejecting or escaping sensitive characters; File Type Check filters and checks the types and names of uploaded files or filters and escapes the paths for directory access to restrict malicious file uploads and path traversal; Sink Removal avoids injection vulnerabilities by removing unsafe or unnecessary sensitive functions (such as eval).
[0032] Pointer vulnerabilities usually occur when accessing resources using pointers, and program crashes and other problems are caused by abnormal pointer states. Methods to fix such vulnerabilities include Null Check, Null Assignment, and Initialization. Null Check checks whether a pointer is a null pointer to avoid null pointer exceptions when using or releasing pointer resources; Null Assignment sets the pointer to null after it is released to avoid reusing or releasing the pointer; Initialization avoids program exceptions caused by calling uninitialized resources by describing the initialization of resources.
[0033] Privilege - related vulnerabilities can lead to non - secure operations due to unauthorized access. The security vulnerability exception information and repair methods are completed by describing the unauthorized access phenomenon and preventing unauthorized access from occurring. The repair methods include adding identity verification (Identity Check), modifying permission settings (Permission Check), and using white / black lists (White or Black List). Adding identity verification is achieved by abstractly describing the addition of identity verification operations; modifying permission settings is realized by reducing the operator's permission level, resetting permission settings, and adding permission checks; using white / black lists filters permissions by adding a permission control list or setting access control checks.
[0034] (3) Security Indicator Extraction
[0035] As Figure 3 shown in an example diagram of a security patch message, the patch message may contain multiple sentences, some of which mention security semantics. To extract security indicators, the present invention first collects security patch messages, and then manually reviews these messages to extract sentences or groups of sentences that mention security semantics.
[0036] To locate security indicators, the present invention first collects security patch messages, and then extracts sentences or groups of sentences that mention security semantics through manual review. Since security patch messages may contain multiple sentences, the present invention uses relatively simple rules for extraction, that is, if a single sentence or a group of semantically related sentences mention any security semantics, they are extracted as a security indicator.
[0037] The present invention tags the extracted security indicators according to the mentioned security semantics, because indicators with the same security semantics, that is, the same security weaknesses, are similar data and can be used for the model to learn commonalities. As Figure 3 shown in the example diagram of the security patch message, the 1st, 2nd, and 4th lines of this message contain security semantics, and each of them can be independently used to determine a security patch. Therefore, the present invention extracts them as three independent security indicators and marks the corresponding security semantics.
[0038] 2. Data Augmentation
[0039] Data augmentation is used to generate positive sample variants and hard negative samples to enrich the training data set. Here, positive sample variants refer to samples that introduce certain changes while retaining their original semantics, which helps the one - class learning model discover the commonalities among positive samples; while hard negative samples refer to instances that are extremely similar to security patches but are not security patches, thus improving the model's ability to distinguish between these two types of patches.
[0040] (1) Generation of Positive Sample Variants
[0041] To generate positive sample variants, the present invention employs two common text augmentation techniques: text paraphrasing and noise injection. The former generates new samples by rephrasing the security indicator while preserving the original meaning. The latter generates new samples by adding unrelated sentences (i.e., noise) while maintaining the same security indicator.
[0042] Text paraphrasing: Leveraging the capabilities of large language models (LLMs), new samples of the security indicator are generated through techniques such as synonym replacement, sentence structure variation, and tone and style adjustment. To achieve this, the present invention designs a prompt to guide the LLMs to paraphrase the input. Additionally, to avoid generating homogeneous sentences, the present invention designs four prompts as shown Figure 5 below, each containing different techniques and randomly using them.
[0043] To avoid the accuracy of the output of the paraphrasing task and ensure that no additional information beyond the original text is output, which would change the original semantics, the present inventors design a new prompt to guide the LLMs to verify their output. The prompt is “Do these two passages convey the same thing? A: {Security Indicators}; B: {Rephrased Output}.” and manually review and resolve all identified conflicts.
[0044] Noise injection: Randomly select 1 - 3 sentences unrelated to the patch context as noise sentences, which are from a general text dataset, such as “This is an apple” in the General Language Understanding Evaluation (GLUE) dataset. Next, each selected noise sentence is inserted before or after the security indicator to keep it unchanged. Since this augmented sample contains the security indicator and preserves the original meaning, it is considered a positive sample. Finally, the security indicator is removed from the augmented positive sample to generate a negative sample, preventing the impact of noise on model learning.
[0045] To illustrate this process, assume that N1 and N2 are two sentences randomly selected from the text dataset (e.g., “This is an apple.” and “This is a tree.”), and S is the security indicator (e.g., “This patch fixes a buffer overflow.”). To generate a noise - augmented positive sample, N1 and N2 can be inserted before or after S, such as [N1, S, N2] or [N2, N1, S]. To generate a negative sample, S is removed, resulting in [N1, N2] and [N2, N1] respectively.
[0046] (2) Hard negative sample mining
[0047] To mine difficult negative samples, the present invention adopts an active learning method to mine difficult negative examples that are extremely similar to security patches from non-security patches, thereby improving the model's ability to distinguish between these two types of patches.
[0048] First, a security patch recognition model is trained using security indicators and enhanced samples. Secondly, the model is used to predict security patches in the open-source software repository, and a confidence score is output for each prediction. Then, non-security patches with higher confidence scores (e.g., the top 10% of the patches) are preferentially identified and labeled because they are more likely to be difficult negative examples. In each iteration, the present invention labels a certain number of difficult negative samples and incorporates them into the training dataset. Finally, the model is retrained using the new dataset. This process will continue until almost no non-security patches with high confidence scores can be found.
[0049] 3. Encoder fine-tuning
[0050] The encoder fine-tuning module trains a patch message encoder that learns security patch features based on an enhanced full security indicator dataset through two specially designed loss functions. This encoder converts patch messages into vectors in the latent space.
[0051] (1) Loss function design
[0052] The present invention designs two loss functions: one is an intra-class loss function for learning within positive samples; the other is an inter-class loss function for learning between positive and negative samples; the final loss is the weighted average of these two loss functions.
[0053] Intra-class loss: Binary Cross-Entropy (BCE) loss and a multi-label classification head are used to generate probability outputs. Specifically, the BCE loss measures the difference between the output probability and the true label (0 or 1) on each dimension. By minimizing this loss on each dimension, the BCE loss encourages the model to make samples with similar labels closer together in the latent space.
[0054] Technically, let y ij represent the semantic label of the j-th semantics of the i-th sample, taking values of 0 or 1; let represent the original output of the training encoder for the j-th semantics of the i-th sample, taking values in the range [0,1]; let C represent the total number of semantics (C = 19 can be used), and let N represent the number of positive samples. The intra-class loss can be expressed as:
[0055]
[0056] Inter-class loss: Supervised Contrastive Learning (SCL) is adopted. On the one hand, it guides representation learning by minimizing the similarity between data of different classes (i.e., safety indicators and hard negative examples). On the other hand, it can utilize the correspondence between a safety indicator and its variants and maximize the similarity between them in a batch of data. Its calculation method is as follows:
[0057]
[0058] where is the index of a batch of samples; is the index of positive samples (i.e., those samples i that have the same label as sample i); z i and z p are the numerical vectors of sample i and p in the latent space respectively; sim(z i , z p ) is the similarity between vectors z i and z p such as dot product, cosine similarity, etc.; τ is the temperature parameter used to control the smoothness of the similarity distribution; is the set of indices of all samples in a batch except i.
[0059] (2) Encoder training
[0060] The pre-trained encoder BERT is adopted in the present invention. First, its loss function is replaced with a custom loss function to learn safety features. Second, its Batch Arranger is re-implemented to introduce dynamic noise during training. Finally, it is trained using a dataset containing safety indicators, reformulated variants, and hard negative samples to obtain a patch message encoder.
[0061] 4. One-class prediction
[0062] The one-class prediction module is used to perform one-class prediction using the fine-tuned encoder, following the idea of one-class classification to predict hidden patches. Specifically, it converts the safety indicators in the training dataset into latent vectors through the patch message encoder and stores them.
[0063] During prediction, first, the message of a patch is encoded to generate a message vector. Then, the average distance between this vector and the n nearest stored safety indicator vectors is calculated. The final one-class prediction result is represented by the average distance between the patch message vector and the stored safety indicator vectors. If the average distance is less than a predetermined threshold, indicating a significant similarity to known safe patches, then the patch is classified as a safe patch. Otherwise, it is an unsafe patch.
[0064] Compared with the prior art, the beneficial effects of the present invention are as follows:
[0065] The method for locating hidden patches based on one-class learning provided by the present invention realizes high-precision identification of hidden security patches by comprehensively utilizing the security indicators, positive sample variants, and characteristics of hard negative samples in the patch messages. This method can not only resist the complexity of code changes and data imbalance problems, but also automatically process a large number of patch messages, reduce manual intervention, and significantly improve the accuracy and efficiency of hidden patch location. Through these innovations, the present invention provides a more accurate and efficient solution for locating hidden patches for security analysts, effectively improving software security. BRIEF DESCRIPTION OF THE DRAWINGS
[0066] Figure 1 It is the overall framework diagram of the method for locating hidden patches based on one-class learning.
[0067] Figure 2 It is the security semantics and their relationships.
[0068] Figure 3 It is the patch message of CVE-2011-4312 (Cross-Site Scripting, XSS), and the underlined text is the security identifier.
[0069] Figure 4 It is from Figure 3 An example of extracting security indicators.
[0070] Figure 5 It is the large model prompt for text restatement. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0071] The present invention will be further introduced below through embodiments in combination with the drawings.
[0072] The present invention mainly focuses on security patches in open-source software repositories, and the goal is to identify and locate hidden security patches through one-class learning methods. The overall framework diagram of the present invention is as Figure 1 shown. It includes: a data extraction module, a data augmentation module, an encoder fine-tuning module, and a one-class prediction module. The execution process of the present invention is as follows:
[0073] 1) Data extraction module: Extract security indicators from the original security patch messages, and these indicators contain security-related semantic information.
[0074] 2) Data augmentation module: Augment the extracted security indicators to generate positive sample variants and hard negative samples to enrich the training data set.
[0075] 3) Encoder fine-tuning module: Use the augmented data set to train a patch message encoder, and learn the security patch features through a specially designed loss function.
[0076] 4) Single-class prediction module: Use the fine-tuned encoder for single-class prediction to locate hidden security patches.
[0077] The following is a further description of each module:
[0078] 1. Data extraction module
[0079] The data extraction module takes the original security patch message as input and generates a set of relevant and useful data for security patch identification, namely security indicators. This module lays the foundation for the subsequent process and generates the target security indicators that this hidden patch location method aims to learn.
[0080] (1) Security fix semantic partitioning
[0081] The security indicators defined in the present invention are text fragments containing security semantic information: one or a group of sentences mentioning security semantics. These security semantics include security annotations, impacts, exploitations, four types of security weaknesses, and twelve corresponding security fix measures, and the partitioning of their detailed semantics is as Figure 2 shown. The following elaborates on the basic semantics and characteristics of security fix descriptions.
[0082] Security fix hint information. There is security hint information in some security fix descriptions, such as Figure 3 the part in red font in. This part contains the security fix hint information CVE-2018-1093, which can clearly indicate that this fix is a security fix. Other such hint information includes "Security Fix", "Vulnerability", and other fix hints.
[0083] Security weakness information. This information contains a description of a clear security vulnerability or anomaly, such as Figure 3 the part in blue font in. This part details that the anomaly type of this fix is out-of-bounds read, that is, an out-of-bounds read vulnerability. The security weakness information in the fix description text can also effectively guide the model to identify and classify security fix description texts.
[0084] Security fix method. This information contains abstract description information of the fix method, such as Figure 3 the part in purple font in. This part details that the fix method is to add a boundary check. The consequence of not performing a boundary check is usually an out-of-bounds phenomenon, which can trigger overflow-type vulnerabilities. Therefore, the fix method information is also an important semantics of the security fix description text.
[0085] Security impact information. Similarly, there will also be descriptions of security impacts in some security fix description texts. This information mainly includes the impacts and consequences of the vulnerabilities fixed by the security fix, such as Figure 3 shown in green font. The developer added a description of the security impact in the security fix description, stating that if the security issue is not fixed, it will cause the program to crash. This is also important in the security fix description text.
[0086] Attacker exploitation information. This information focuses on how attackers exploit the vulnerability for attacks. Such information in the security fix description text mainly focuses on how attackers exploit and trigger the vulnerability, such as Figure 3 shown in orange font. This part of the information can also assist the security fix model in completing identification and classification tasks. Therefore, attacker exploitation information can also be regarded as an important semantics in the security fix description text.
[0087] (2) Definition of security indicators
[0088] Based on the above summary of security fix semantic information, the security indicator of the present invention corresponds to the Figure 2 shown security semantics, and includes a total of 7 secondary tags and 12 tertiary tags under four security weaknesses. The 7 secondary tags respectively include Security Note, Impact, Exploitability, and four security weaknesses: Out-of-bound vulnerabilities, Injection vulnerabilities, Pointer vulnerabilities, and Authority vulnerabilities. The following will explain the four security weaknesses and their corresponding secondary tags in turn.
[0089] Out-of-bound vulnerabilities are program crashes caused by out-of-bounds access, usually manifested as a value exceeding the length limit of its type identifier, or exceeding the boundary when allocating memory and performing read and write operations. Methods for fixing such vulnerabilities include Bound Check, Buffer Resizing, and Safer Function or Type replacement. Bound Check prevents out-of-bounds vulnerabilities by checking the boundary range before the operation; Buffer Resizing controls the range by adjusting the buffer size to avoid out-of-bounds read vulnerabilities; Safer Function or Type replacement replaces unsafe functions with safer versions, such as replacing strcpy with strncpy to avoid overflow.
[0090] Injection vulnerabilities are security issues that cause uncontrollable behavior in a program due to insecure user input or uploaded files. Methods for fixing such vulnerabilities include filtering input strings (User Input Sanitization), filtering uploaded files or paths (FileType Check), and removing insecure sensitive functions (Sink Removal). Filtering input strings prevents injection vulnerabilities by rejecting or escaping sensitive characters; filtering uploaded files or paths filters and checks the types of uploaded files and file names or filters and escapes directory access paths to restrict malicious file uploads and path traversal; removing insecure sensitive functions avoids injection vulnerabilities by deleting insecure or unnecessary sensitive functions (such as eval).
[0091] Pointer vulnerabilities usually occur when accessing resources using pointers, resulting in program crashes and other issues due to abnormal pointer states. Methods for fixing such vulnerabilities include null pointer checks (NullCheck), null pointer assignments (NullAssignment), and resource initialization (Initialization). Null pointer checks verify whether a pointer is a null pointer to avoid null pointer exceptions when using or releasing pointer resources; null pointer assignments set a pointer to null after it has been released to prevent the pointer from being used or released again; resource initialization describes the initialization of resources to avoid program exceptions caused by calling uninitialized resources.
[0092] Privilege vulnerabilities result in insecure operations due to unauthorized access. The security vulnerability exception information and repair methods are completed by describing unauthorized access phenomena and preventing unauthorized access from occurring. Repair methods include adding authentication (Identity Check), modifying privilege settings (Permission Check), and using white / black lists (White or Black List). Adding authentication is achieved by abstractly describing the addition of authentication operations; modifying privilege settings is implemented by reducing the operator's privilege level, resetting privilege settings, and adding privilege checks; using white / black lists filters privileges by adding privilege control lists or setting access control checks.
[0093] (3) Security Indicator Extraction
[0094] As Figure 3 shown in an example diagram of a security patch message, the patch message may contain multiple sentences, some of which mention security semantics. To extract security indicators, the present invention first collects security patch messages and then manually reviews these messages to extract sentences or groups of sentences that mention security semantics.
[0095] To locate security indicators, the present invention first collects security patch messages and then extracts sentences or groups of sentences that mention security semantics through manual review. Since security patch messages may contain multiple sentences, the present invention uses relatively simple rules for extraction, that is, if a single sentence or a group of semantically related sentences mentions any security semantics, they are extracted as a security indicator.
[0096] The present invention tags the extracted security indicators according to the mentioned security semantics, because indicators with the same security semantics, that is, the same security weaknesses, are similar data and can be used for the model to learn commonalities. As Figure 3 shown in the example diagram of the security patch message, the 1st, 2nd, and 4th lines of this message contain security semantics, and each of them can be independently used to determine a security patch. Therefore, the present invention extracts them as three independent security indicators and marks the corresponding security semantics.
[0097] 2. Data Augmentation Module
[0098] The data augmentation module is used to generate positive sample variants and hard negative samples to enrich the training dataset. Here, positive sample variants refer to samples that introduce certain changes while retaining their original semantics, which helps the one-class learning model discover commonalities among positive samples; while hard negative samples refer to instances that are extremely similar to security patches but are not security patches, thereby enhancing the model's ability to distinguish between these two types of patches.
[0099] (1) Generation of Positive Sample Variants
[0100] To generate positive sample variants, the present invention adopts two common text augmentation techniques: text paraphrasing and noise injection. The former generates new samples by re-expressing the security indicator while retaining the original meaning. The latter generates new samples by adding irrelevant sentences (i.e., noise) while maintaining the same security indicator.
[0101] Text paraphrasing: Utilize the capabilities of large language models (LLMs) to generate new samples of security indicators through techniques such as synonym replacement, sentence structure change, and tone and style adjustment. To achieve this, the present invention designs a prompt to guide the LLMs to paraphrase the input. In addition, to avoid generating homogeneous sentences, the present invention designs four prompts as Figure 5 shown, each prompt contains different techniques and they are randomly used.
[0102] To avoid restating the accuracy of the task output and ensure that no extra information beyond the original text is output, which could change the original semantics, the present invention designs a new prompt to guide LLMs to verify their output. The prompt is "Do these two passages convey the same thing? A: {Security Indicators}; B: {Rephrased Output}." And manually review and resolve all identified conflicts.
[0103] Noise injection: Randomly select 1 - 3 sentences that are irrelevant to the patch context as noise sentences. These sentences are from a general text dataset, such as "This is an apple" from the General Language Understanding Evaluation (GLUE) dataset. Next, insert each selected noise sentence before or after the security indicator to keep it unchanged. Since this enhanced sample contains the security indicator and maintains the original meaning, it is regarded as a positive sample. Finally, remove the security indicator from the enhanced positive sample to generate a negative sample, preventing the impact of noise on model learning.
[0104] To illustrate this process, assume N1 and N2 are two sentences randomly selected from a text dataset (e.g., "This is an apple." and "This is a tree."), and S is the security indicator (e.g., "This patch fixes a buffer overflow."). To generate a noise - enhanced positive sample, N1 and N2 can be inserted before or after S, such as [N1, S, N2] or [N2, N1, S]. To generate a negative sample, remove S, which are [N1, N2] and [N2, N1] respectively.
[0105] (2) Hard negative sample mining
[0106] To mine hard negative samples, the present invention adopts an active learning method to mine hard negative examples that are extremely similar to security patches from non - security patches, thereby enhancing the model's ability to distinguish between these two types of patches.
[0107] First, train a security patch recognition model using the security indicator and enhanced samples. Second, use this model to predict security patches in an open - source software repository and output a confidence score for each prediction. Then, preferentially identify and label non - security patches with higher confidence scores (e.g., the top 10% of the patches) because they are more likely to be hard negative examples. In each iteration, the present invention labels a certain number of hard negative samples and incorporates them into the training dataset. Finally, retrain the model using the new dataset. This process continues until almost no non - security patches with high confidence scores can be found.
[0108] 3. Encoder Fine-tuning Module
[0109] The encoder fine-tuning module trains a patch message encoder that learns security patch features based on an enhanced full security indicator dataset through two specially designed loss functions. This encoder converts patch messages into vectors in the latent space.
[0110] (1) Loss Function Design
[0111] The present invention designs two loss functions: one is the intra-class loss function for learning within positive samples; the other is the inter-class loss function for learning between positive and negative samples; the final loss is the weighted average of these two loss functions.
[0112] Intra-class loss: The binary cross-entropy (BCE) loss and a multi-label classification head are used to generate probability outputs. Specifically, the BCE loss measures the difference between the output probability and the true label (0 or 1) on each dimension. By minimizing this loss on each dimension, the BCE loss encourages the model to cluster samples with similar labels more closely together in the latent space.
[0113] Technically, let y ij represent the semantic label of the j-th semantics of the i-th sample, with values ranging from 0 or 1; let represent the original output of the training encoder for the j-th semantics of the i-th sample, with values in the range [0,1]; let C represent the total number of semantics (C = 19), and let N represent the number of positive samples. The intra-class loss can be expressed as:
[0114]
[0115] Inter-class loss: Supervised contrastive learning (SCL) is adopted. On the one hand, it guides representation learning by minimizing the similarity between data of different classes (i.e., security indicators and hard negative examples). On the other hand, it can utilize the correspondence between a security indicator and its variants and maximize the similarity between them in a batch of data. Its calculation method is:
[0116]
[0117] where, is the index of a batch of samples; is the index of positive samples (i.e., those samples i that have the same label as sample i); z i and z p are the numerical vectors of sample i and p in the latent space respectively; sim(z i ,z p ) is the vector z iand z p similarities between them such as dot product, cosine similarity, etc.; τ is a temperature parameter used to control the smoothness of the similarity distribution; is the index set of all samples except i in a batch of samples.
[0118] (2) Encoder Training
[0119] The present invention adopts the pre-trained encoder BERT. First, the present invention replaces its loss function with a custom loss function to learn security features. Second, the present invention re-implements its Batch Arranger to introduce dynamic noise during training. Finally, the present invention trains it using a dataset containing security indicators, rephrased variants, and hard negative samples to obtain a patch message encoder.
[0120] 4. One-Class Prediction Module
[0121] The one-class prediction module is used to perform one-class prediction using the fine-tuned encoder, following the idea of one-class classification to predict hidden patches. Specifically, it converts the security indicators in the training dataset into latent vectors through the patch message encoder and stores them.
[0122] During prediction, first, the message of a patch is encoded to generate a message vector. Then, the average distance between this vector and n recently stored security indicator vectors is calculated. The final one-class prediction result is represented by the average distance between the patch message vector and the stored security indicator vectors. If the average distance is less than a predetermined threshold, indicating a significant similarity to known secure patches, the patch is classified as a secure patch. Otherwise, it is an insecure patch.
[0123] Real-World Practice Evaluation
[0124] The present invention verifies its effectiveness in the real world through a series of actual cases. To ensure that the present invention can accurately identify secure patches in the real world, first, 12 popular open-source software are selected, which use multiple programming languages, and 17,437 patches submitted during the period from February to April 2024 are collected. The selected software is different from the samples used in the training dataset to ensure the diversity of the evaluation.
[0125] The present invention is applied to these collected patch data and successfully identifies 903 potential positive patches. To verify the security of these patches, 25 patches are randomly selected from each software repository, and 103 patches are manually checked (if the number of patches of a certain software is less than 25, all patches are checked). Five experienced security experts independently evaluate these patches and resolve the differences through discussion. Finally, 98 secure patches are confirmed.
[0126] The verification of these patches shows that the average precision of the present invention in the actual environment reaches 64.90%. Despite the differences among different software, the overall performance of the present invention is still quite close to the results in previous experiments. Further investigation shows that among the 98 security patches identified by the present invention, 32 have fixed vulnerabilities that have been assigned CVE numbers. The remaining 66 patches are misjudged as hidden security patches. To further confirm these vulnerabilities, CVE numbers are applied for them. Although some of these vulnerabilities are not original discoveries, only these vulnerabilities are reported without claiming ownership of them. During this process, 21 patches with no impact versions are also identified and thus excluded from further evaluation.
[0127] The present invention proposes a hidden patch location method based on one-class learning. First, a data extraction module extracts security indicators containing security semantic information from the original security patch messages. These indicators cover security annotations, impacts, exploitabilities, and four types of security weaknesses and their corresponding repair measures. Then, the data augmentation module generates positive sample variants through text restatement and noise injection techniques, and mines difficult negative samples from non-security patches through active learning methods to enrich the training data set. Subsequently, the encoder fine-tuning module uses the augmented data set to train a patch message encoder, and learns security patch features through specially designed intra-class loss and inter-class loss. Finally, the one-class prediction module uses the fine-tuned encoder to encode the patch message into a latent vector, calculates the average distance between the latent vector and the stored security indicator vector, and determines whether it is a security patch according to a predetermined threshold. Through this series of steps, the present invention can effectively identify and locate hidden security patches in open-source software repositories, providing an accurate and reliable identification method for hidden patches in open-source software.
Claims
1. A method for locating hidden patches based on one-class learning, characterized in that, The specific steps are as follows: (1) Data extraction: Extract a set of relevant and useful data for security patch identification, namely security indicators, from the original security patch messages, and mark the corresponding security semantics; (2) Data augmentation: Augment the extracted security indicators to generate positive sample variants and hard negative samples; the positive sample variants refer to samples that introduce certain changes while retaining their original semantics to help the one-class learning model discover the commonalities among positive samples; the hard negative samples refer to instances that are very similar to security patches but are not security patches, thus enhancing the model's ability to distinguish between these two types of patches; (3) Encoder fine-tuning: Based on the augmented security indicator dataset, train a patch message encoder to learn security patch features through two specially designed loss functions. This encoder converts patch messages into vectors in the latent space; (4) One-class prediction: Use the fine-tuned encoder for one-class prediction. By calculating the average distance between the patch message vector and the known security indicator vectors, determine whether the patch is a security patch.
2. The method for locating hidden patches based on one-class learning according to claim 1, wherein The data extraction described in step (1) specifically includes: (1) Security fix semantic partitioning Security indicators are text fragments containing security semantic information: one or a group of sentences mentioning security semantics; security semantics include security annotations, impacts, exploitabilities, and four types of security weaknesses, corresponding to twelve security fix measures; The basic semantics and their characteristics of security fix descriptions are mainly: Security fix hint information; there is security hint information in some security fix descriptions, which clearly indicates that this fix is a security fix; Security weakness information; this information contains a clear description of a security vulnerability or anomaly; Security fix method; this information contains abstract description information of the fix method; this part details the fix method as adding a boundary check; the consequence of not performing a boundary check is usually an out-of-bounds phenomenon, thus triggering an overflow-type vulnerability; Security impact information; similarly, there is description information about security impacts in some security fix description texts, which mainly includes the impacts and consequences of the vulnerabilities corresponding to the security fixes; Attacker exploitation information; this information focuses on describing how attackers exploit this vulnerability for attacks. The appearance of this kind of information in security fix description texts mainly focuses on how attackers exploit and trigger this vulnerability; (2) Security indicator definition According to the above security fix semantic information, security indicators correspond to security semantics, including a total of 7 secondary labels and 12 tertiary labels under four types of security weaknesses; the 7 secondary labels include security annotations, impacts, exploitabilities, and four types of security weaknesses: overflow-type vulnerabilities, injection-type vulnerabilities, pointer-type vulnerabilities, and privilege-type vulnerabilities; among them: (3) Security indicator extraction First, collect security patch messages, and then review these messages to extract sentences or groups of sentences mentioning security semantics; Since security patch messages may contain multiple sentences, relatively simple rules are used for extraction: if a single sentence or a group of semantically related sentences mention any security semantics, they are extracted as a security indicator; Tag the extracted security indicators according to the mentioned security semantics.
3. The method for locating hidden patches based on one-class learning according to claim 2, wherein Among the four security weaknesses described in step (1): Overflow - type vulnerabilities are program crashes caused by out - of - bounds access, manifested as a value exceeding the length limit of its type identifier or exceeding the boundary during memory allocation and read - write operations; methods to fix such vulnerabilities include boundary checking, buffer size reset, security functions, or type replacement; boundary checking prevents out - of - bounds vulnerabilities by checking the boundary range before an operation; buffer size reset controls the range by adjusting the buffer size to avoid out - of - bounds read vulnerabilities; security functions or type replacement replace insecure functions with more secure versions. Injection - type vulnerabilities are security problems where uncontrollable behavior occurs in the program due to insecure user input or uploaded files. Methods to fix such vulnerabilities include: filtering input strings, filtering uploaded files or paths, and removing insecure sensitive functions; filtering input strings prevents injection - type vulnerabilities by rejecting or escaping sensitive characters; filtering uploaded files or paths filters and checks the type of uploaded files and the names of uploaded files or filters and escapes the paths for directory access to limit malicious file uploads and path traversal; removing insecure sensitive functions avoids injection - type vulnerabilities by deleting insecure or unnecessary sensitive functions. Pointer - type vulnerabilities occur when accessing resources using pointers and the program crashes due to abnormal pointer states; methods to fix such vulnerabilities include pointer null - value checking, pointer null - value setting, and initializing resources; pointer null - value checking checks whether a pointer is a null pointer to avoid null - pointer exceptions when using or releasing pointer resources; pointer null - value setting sets the pointer to null after it is released to avoid re - using or releasing the pointer; initializing resources describes the initialization of resources to avoid program exceptions caused by calling uninitialized resources. Privilege - type vulnerabilities are non - secure operations caused by privilege escalation. Their security vulnerability exception information and repair methods are completed by describing the privilege - escalation phenomenon and preventing privilege escalation; repair methods include adding authentication, modifying privilege settings, and using black / white lists; adding authentication abstractly describes adding authentication operations; modifying privilege settings is achieved by reducing the operator's privilege level, resetting privilege settings, and adding privilege checks; using black / white lists filters privileges by adding a privilege control list or setting access control checks.
4. The method for locating hidden patches based on one-class learning according to claim 3, wherein The data enhancement described in step (2) specifically includes: (1) Positive - sample variant generation Using text restatement and noise injection; the former generates new samples by re - expressing the security indicator while retaining the original meaning; the latter generates new samples by adding irrelevant sentences while maintaining the same security indicator; specifically: Text restatement: Utilize the capabilities of large language models to generate new samples of security indicators through techniques such as synonym replacement, sentence structure variation, and tone and style adjustment; To this end, design a prompt to guide the LLMs to restate the input; In addition, to avoid generating homogeneous sentences, design four prompts, each containing different techniques and use them randomly; To avoid the accuracy of the output of the restatement task and ensure that no additional information beyond the original content is output to change the original semantics, design a new prompt to guide the LLMs to verify their output. The prompt is "Do these two passages convey the same thing? A: {Security Indicators}; B: {Rephrased Output}.” And manually review and resolve all identified conflicts; Noise injection: Randomly select 1 - 3 sentences unrelated to the patch context as noise sentences. These sentences are from a general text dataset. Next, insert each selected noise sentence before or after the security indicator while keeping it unchanged. Since this enhanced sample contains the security indicator and retains the original meaning, it is regarded as a positive sample; Finally, remove the security indicator from the enhanced positive sample to generate a negative sample to prevent the impact of noise on model learning; (2) Hard negative sample mining Specifically, adopt an active learning method to mine hard negative examples that are extremely similar to security patches from non - security patches, thereby enhancing the model's ability to distinguish between these two types of patches; First, train a security patch recognition model using security indicators and enhanced samples; Second, use this model to predict security patches in the open - source software repository and output a confidence score for each prediction; Then, preferentially identify and label non - security patches with higher confidence scores; In each iteration, label a certain number of hard negative samples and incorporate them into the training dataset; Finally, retrain the model with the new dataset; This process will continue until almost no non - security patches with high confidence scores can be found.
5. The method for locating hidden patches based on one-class learning according to claim 4, characterized in that, The encoder fine - tuning described in step (3) specifically includes: (1) Loss function design Design two loss functions: one is the intra - class loss function for learning within positive samples; the other is the inter - class loss function for learning between positive and negative samples; The final loss is the weighted average of these two loss functions; Intra - class loss: Adopt binary cross - entropy (BCE) loss and a multi - label classification head to generate probability outputs; Specifically, the BCE loss measures the difference between the output probability and the true label (0 or 1) on each dimension; By minimizing this loss for each dimension, encourage the model to make samples with similar labels closer together in the latent space; Specifically: Let y ij represent the semantic label of the j-th semantics of the i-th sample, with a value range of 0 or 1; let represent the original output of the training encoder for the j-th semantics of the i-th sample, with a value range of [0,1]; let C denote the total number of semantics (C = 19), and let N denote the number of positive samples; the intra-class loss is expressed as: Inter-class loss: Supervised Contrastive Learning (SCL) is adopted; on the one hand, it guides representation learning by minimizing the similarity between data of different classes; on the other hand, it utilizes the correspondence between the security indicator and its variant, and maximizes the similarity between them in a batch of data; its calculation method is as follows: Among them, is the index of a batch of samples; is the index of positive samples, that is, those samples i that have the same label as sample i; z i and z p are the numerical vectors of sample i and p in the latent space respectively; sim(z i , z p ) is the similarity between the vectors z i and z p ; τ is the temperature parameter used to control the smoothness of the similarity distribution; is the index set of all samples in a batch except i; (2) Encoder training The pre-trained encoder BERT is adopted; first, its loss function is replaced with a custom loss function to learn security features; second, its batch scheduler is re-implemented to introduce dynamic noise during training; finally, it is trained using a dataset containing security indicators, reformulated variants, and hard negative samples to obtain a patch message encoder.
6. The method for locating hidden patches based on one-class learning according to claim 5, wherein The single-class prediction described in step (4) has the following specific process: First, the message of a patch is encoded to generate a message vector; then, the average distance between this message vector and the n nearest stored security indicator vectors is calculated; finally, the average distance between the patch message vector and the stored security indicator vectors represents the single-class prediction result; if the average distance is less than a predetermined threshold, indicating a significant similarity to a known security patch, then this patch is classified as a security patch; otherwise, it is a non-security patch.