Method and system for automatically generating patches for security violations
By automatically generating patches, the problem of time-consuming repair process after security violations in software code is solved, code execution can be stopped before security violations occur, reducing potential exploitation time, and data collection and warning functions are provided to protect the code from attack.
Patent Information
- Application Number
- CN201880091955.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2018-07-17
- Publication Date
- 2024-08-30
- Estimated Expiration
- 2038-07-17
AI Technical Summary
In the existing technology, after a security violation is discovered in software or application code, the repair process requires a lot of resources and time, and during this period the code is still at risk, making it difficult to repair quickly.
The processor automatically generates patches to detect security violations in the code, and generates candidate patches for specific inputs, including patch conditions and corresponding locations, which are used to abort or replace the execution of potentially violating code and reduce potential exploitation time.
Automatically generate patches to protect code from attacks without waiting for a full fix, reduce potential exploitation time from months to hours, and provide data collection and warning capabilities.
Smart Images

Figure CN111919214B_ABST
Abstract
Description
[0001] CROSS-REFERENCE TO RELATED APPLICATIONS
[0002] This application claims the benefit of the filing date of U.S. Provisional Application No. 62 / 674,133, filed May 21, 2018, the disclosure of which is incorporated herein by reference. Background Art
[0003] Security defects or violations in software or application code may take a lot of resources and time to fix, such as months or longer. The effort to find security violations produces a steady stream of such problems, so that at any given time, many security violations are public or unfixed. In practice, the process of creating, testing and deploying repairs requires unplanned efforts by many human operators. As long as the operator is in the critical path, it is not feasible to fix these problems quickly. During the time when these security violations are public, these applications are still at risk. As an example, security violations can include memory security violations (such as reading data that exceeds the buffer size for the data, because this may inappropriately allow access to data that the application should not access in order to work), input validation errors, race conditions, undefined behavior, etc. Summary of the invention
[0004] Various aspects of the present disclosure provide a method for automatically generating patches for security violations. The method includes: generating multiple inputs for code by one or more processors; executing the code by one or more processors using the multiple inputs to obtain execution states at multiple code locations, the execution states including at least one security violation for at least one of the multiple inputs; determining one or more patch conditions that cause the security violation by one or more processors using the execution states; determining one or more corresponding patch locations based on a code location in a plurality of code locations where at least one security violation condition occurs by one or more processors using the execution states; and automatically generating at least one candidate patch by one or more processors for at least one security violation, the at least one candidate patch including one of the patch conditions and one of the one or more corresponding patch locations.
[0005] In one example, the method also includes generating patched code by one or more processors by inserting at least one candidate patch into the code. In another example, at least one candidate patch is configured to cause at least some of the multiple inputs associated with at least one security violation to be abandoned for execution. In this example, at least one candidate patch causes at least a portion of the code to be suspended before a security violation may occur. In another example, at least one candidate patch is configured to generate a log of diagnostic information for a security violation. In another example, the method also includes: generating a candidate patched code set by one or more processors by inserting each of one or more candidate patches into the code; executing each of the candidate patched codes using multiple inputs by one or more processors to obtain the execution status of the patch at multiple code locations; selecting a candidate patch from one or more candidate patches based on the execution status of the patch by one or more processors; and generating a second patched code by inserting the selected candidate patch into the code by one or more processors. In another example, the method also includes: selecting a candidate patch from at least one candidate patch by one or more processors based on at least one of complexity, proximity to the site of at least one security violation, or impact on the execution of the code; and generating a patched code by inserting the selected candidate patch into the code by one or more processors. In this example, the method also includes: generating a new plurality of inputs for the patched code by one or more processors; executing the patched code using the new plurality of inputs by one or more processors to obtain new execution states at multiple code locations, the new execution states including new security violations; using the new execution states by one or more processors to calculate one or more new patch conditions that cause new security violations associated with one or more of the multiple inputs; using the new execution states by one or more processors to determine one or more new corresponding patch locations where each of the one or more new patch conditions occurs; and generating at least one new candidate patch by one or more processors for the new security violation, each of the at least one new candidate patch including one of the new patch conditions and one of the new corresponding patch locations, wherein at least one new candidate patch is configured to cause the new violation input associated with the new security violation to be abandoned for execution. In addition or alternatively, clustering the training inputs for the learning-based model based on whether the input causes a security violation. In another example, one or more patch conditions are generated using a machine learning model, and the method further includes clustering the inputs for the model based on whether the input reaches an area of interest in the code. In another example, determining the one or more patch conditions further includes determining at least one dynamic invariant related to at least one security violation. In this example, the dynamic invariant includes a predicate template.Additionally or alternatively, the method also includes combining two or more of the dynamic invariants to determine one or more patch conditions. In another example, generating the one or more patch conditions further includes determining a weakest precondition associated with at least one security violation. In another example, generating the one or more patch conditions further includes tracking data flow through memory states collected by the log. In this example, tracking data flow through memory states includes using selective memory tracking.
[0006] Another aspect of the present disclosure provides a system for automatically generating patches for security violations. The system includes one or more processors configured to: generate multiple inputs for code; use the multiple inputs to execute the code to obtain execution states at multiple code locations, the execution states including security violations for at least one of the multiple inputs; use the execution states to determine one or more patch conditions that cause security violations; use the execution states to determine one or more corresponding patch locations based on code locations where at least one security violation occurs in each of the one or more patch conditions in the multiple code locations; and automatically generate at least one candidate patch for at least one security violation, the at least one candidate patch including one of the patch conditions and one of the one or more corresponding patch locations.
[0007] In one example, the one or more processors are further configured to generate patched code by inserting at least one candidate patch into the code. In another example, at least one candidate patch is configured to cause at least some of a plurality of inputs associated with at least one security violation to be discarded from execution and cause at least a portion of the code to be suspended from execution before a security violation may occur. In another example, at least one candidate patch is configured to generate a log of diagnostic information for the security violation.
[0008] Such features are advantageous because they can be used to protect vulnerable code from attacks via security violations even before a full fix is available. These features provide for the automatic generation of patches for security violations without the operator having to wait for a full fix. These patches, while not fully fixing the logic of the program, can cause the code to abort before a security violation occurs, thereby hardening or protecting the code from exploitation. This has the advantage of reducing the time to mitigate potential exploits from months to hours. In addition, patches can have alternative or additional functions, such as collecting data for better forensic analysis and displaying security violations to alert developers and users. BRIEF DESCRIPTION OF THE DRAWINGS
[0009] Figure 1 is a block diagram illustrating an example system in accordance with aspects of the present disclosure.
[0010] Figure 2 is an example functional diagram in accordance with aspects of the present disclosure.
[0011] Figure 3A-3B is an example of code and patched code in accordance with aspects of the present disclosure.
[0012] Figures 4A-4B is an example of code and patched code in accordance with aspects of the present disclosure.
[0013] Figure 5 is an example of code in accordance with aspects of the present disclosure.
[0014] Figure 6 is an example of code in accordance with aspects of the present disclosure.
[0015] Figure 7 is an example of code in accordance with aspects of the present disclosure.
[0016] Figures 8A-8B is an example of code and patched code in accordance with aspects of the present disclosure.
[0017] Fig. 9 is an example functional diagram in accordance with aspects of the present disclosure.
[0018] Fig.10 is an example of code in accordance with aspects of the present disclosure.
[0019] Fig.11 is an example functional diagram in accordance with aspects of the present disclosure.
[0020] Fig.12 is an example functional diagram in accordance with aspects of the present disclosure.
[0021] Fig.13 is an example functional diagram in accordance with aspects of the present disclosure.
[0022] Fig.14 is an example functional diagram in accordance with aspects of the present disclosure.
[0023] Fig.15 is a flow chart illustrating an example method in accordance with aspects of the present disclosure. DETAILED DESCRIPTION
[0024] Overview
[0025] The technology generally involves automatically generating patches for security holes in computer code. Security violations can be detected periodically during development and after code is deployed. Although some systems can implement automation to find security violations on a large scale, these systems lack the ability to automatically mitigate the impact of known security violations. The lack of automation to fix the problems found can cause operators to suffer from manually solving security violations. In addition, the process of creating, testing and deploying a complete fix, which is likely to involve team efforts, can take a lot of time. This process can therefore leave the code vulnerable to attack and may last for days or even months.
[0026] In this regard, automated solutions that address such gaps by providing patches for security violations or vulnerabilities can be particularly useful. These patches, while not fully repairing the logic of the application, can be used to cause the application to halt before a security breach occurs, thereby hardening the application from exploitation, in some cases indefinitely, but ideally until a full and complete fix is deployed. This has the potential to reduce the time of potential exploitation from months to hours or even less.
[0027] In order to protect vulnerable code from attack, patches for security violations can be automatically generated. For example, fuzzy technology can be used to generate inputs for code. Each generated input can then be used to execute the code. For each execution, the state at each code location can be monitored, including whether a security violation has occurred. The code, the generated inputs, and the execution state can be analyzed to determine the conditions that are likely to cause security violations associated with one or more inputs and to determine the corresponding patch positions where the conditions are likely to occur. Based on these evaluations, at least one candidate patch can be generated for security violations. The candidate patch can include one of the patch conditions and the corresponding patch position. At least one candidate patch can cause the violation input associated with the security violation to abandon execution and / or record execution, so as to allow the operator to observe the problem in real time and further use the log to investigate.
[0028] The technology described herein is advantageous because it protects vulnerable code from attacks via security violations even before a complete fix is available. The technology provides automatic generation of patches for security violations without the operator having to wait for a complete fix. Although these patches do not completely fix the logic of the program, they can cause the code to be suspended before a security violation occurs, thereby strengthening or protecting the code from exploitation. This has the advantage of reducing the time to mitigate potential exploitation from months to hours. In addition, heuristics and machine learning can be used to select patches that prevent security violations but still allow automatically generated patches to be executed with non-violation inputs. The technology further provides an iterative process to verify the generated patches, analyze the impact of the patches, and produce more suitable patches for selection. In addition, the patches can have alternative or additional functions, such as collecting data for better forensic analysis and displaying security violations to warn developers and users.
[0029] Aspects of the present disclosure provide for automatically generating patches for security violations. For example, multiple inputs can be generated for a computer code. The code can be executed using the multiple inputs to obtain execution states at multiple code locations. The execution state can include at least one security violation for at least one of the multiple inputs. Using the execution state, one or more patch conditions that cause the at least one security violation can be determined. Using the execution state, one or more corresponding patch locations can be determined based on a code location where each of the one or more patch conditions in the multiple code locations has at least one security violation. At least one candidate patch for a security violation can be automatically generated. The at least one candidate patch can include one of the patch conditions and one of the corresponding patch locations.
[0030] Example
[0031] Figure 1 An example system 100 is included in which the features described herein may be implemented. This should not be considered to limit the scope of the present disclosure or the usefulness of the features described herein. In this example, the system 100 can include computing devices 110, 120, 130 and a storage system 140 connected via a network 150. Each computing device 110, 120, 130 can contain one or more processors 112, memory 114, and other components typically found in general-purpose computing devices.
[0032] Although only a few computing devices and storage systems are depicted in system 100, the system can be expanded to any number of additional devices. In addition to systems including multiple computing devices and storage systems connected via a network, the features described herein can also be equally applicable to other types of devices, such as separate chips, including chips incorporated into a system on chip (Soc) or other chips with memory.
[0033] The memory 114 of each of the computing devices 110, 120, 130 can store information accessible by the one or more processors 112, including instructions that can be executed by the one or more processors. The memory can also include data that can be retrieved, manipulated, or stored by the processor. The memory can be any non-transitory type capable of storing information accessible by the processor, such as a hard drive, memory card, ROM, RAM, DVD, CD-ROM, write-enabled memory, and read-only memory.
[0034] The instructions may be any set of instructions to be executed directly by one or more processors, such as machine code, or any set of instructions to be executed indirectly by one or more processors, such as a script. In that regard, the terms "instructions," "applications," "steps," and "programs" can be used interchangeably herein. The instructions can be stored in an object code format for direct processing by a processor, such as one or more processors 112, or in any other computing device language including a script or collection of independent source code modules that are interpreted on demand or compiled in advance. The functions, methods, and routines of the instructions are described in more detail below.
[0035] The data can be retrieved, stored or modified by one or more processors 112 according to instructions. For example, although the subject matter described herein is not limited to any particular data structure, the data can be stored in a computer register, in a relational database as a table with many different fields and records, or in an XML document. The data can also be formatted in any computing device readable format such as, but not limited to, binary values, ASCII or Unicode. In addition, the data can include any information sufficient to identify the relevant information, such as numbers, descriptive text, proprietary codes, pointers, references to data stored in other memories such as at other network locations, or information used by functions to calculate relevant data.
[0036] The one or more processors 112 may be any conventional processor, such as a commercially available CPU. Alternatively, the processor may be a specialized component such as an application specific integrated circuit (“ASIC”) or other hardware-based processor. Although not required, one or more of the computing devices 110 may include specialized hardware components to perform specific computing processes, such as decoding video faster or more efficiently, matching video frames to images, distorting video, encoding distorted video, etc.
[0037] although Figure 1The processor, memory, and other elements of the computing device 110 are functionally illustrated as being within the same block, but the processor, computer, computing device, or memory can actually include multiple processors, computers, computing devices, or memories that may or may not be stored in the same physical housing. For example, the memory can be a hard drive or other storage medium located in a housing different from the housing of the computing device 110. Therefore, references to processors, computers, computing devices, or memories will be understood to include references to a collection of processors, computers, computing devices, or memories that may or may not operate in parallel. For example, the computing device 110 may include a server computing device that operates as a load balancing server farm, a distributed system, and the like. In addition, although some of the functions described below are indicated as being performed on a single computing device with a single processor, various aspects of the subject matter described herein can be implemented by multiple computing devices, for example, by transmitting information over a network 150.
[0038] Each of computing devices 110, 120, 130 may be located at a different node of network 150 and may be able to communicate directly and indirectly with other nodes of network 150. Figure 1 Only a few computing devices are depicted in , but it should be appreciated that a typical system can include a large number of connected computing devices, with each different computing device at a different node of network 150. For example, each of computing devices 110, 120, 130 can be a server computing device that is part of a load-balanced server farm.
[0039] The network 150 and intermediate nodes described herein can be interconnected using various protocols and systems, so that the network can be part of the Internet, the World Wide Web, a specific intranet, a wide area network, or a local area network. The network can utilize standard communication protocols such as Ethernet, WiFi, and HTTP, one or more company-specific protocols, and various combinations of the foregoing. Although certain advantages are obtained when sending or receiving information as described above, other aspects of the subject matter described herein are not limited to any particular transmission method of information.
[0040] Like the memory discussed above, storage system 140 may also store information that can be accessed by any of computing devices 110, 120, and / or 130. However, in this case, storage system 140 may store information that can be accessed via network 150. As with memory, storage system 140 can include any non-transitory type capable of storing information accessible by a processor, such as a hard drive, memory card, ROM, RAM, DVD, CD-ROM, write-enabled memory, and read-only memory.
[0041] In this example, the aforementioned instructions of each of the computing devices 110, 120, 130 may include one or more applications. These applications may include code that can be executed to perform various tasks. At least some of this code may be vulnerable to attacks from third parties intended to steal information, slow down processing, or otherwise cause damage. In addition, the applications may include: "patch generator" 280 (in Figure 2 270), which is configured based on various types of data (in Figure 2 automatically generates patches; a "code miner" (shown as "Program Metadata" 270 in Figure 2 , which generates at least some of the program metadata; a "candidate selector" (shown in Fig.11 110), which selects or determines a winning patch from among the candidate patches; and various other instructions as further discussed below.
[0042] In order to protect vulnerable code from attacks, potential security violations can be detected by first generating inputs for the code. For example, fuzzing techniques such as LLVM LibFuzzer or other techniques can be used to generate random inputs or fuzz targets for the code. At least some of these inputs can be violating inputs, or conversely, inputs that can expose or cause security violations. Similarly, at least some of these inputs can be non-violating inputs, or conversely, inputs that do not necessarily expose or cause security vulnerabilities. The generated inputs can also constitute a "test corpus". Figure 2 is an example functional diagram 200 identifying aspects of candidate patch generation for a code set 210. In this example, Figure 2 The test corpus 220 may include the generated inputs discussed above.
[0043] The code can then be executed using each of the generated inputs. For each execution, the execution state at various code locations can be monitored. For example, the code location can involve transforming or moving data in various steps. During the execution of the program through these code locations, memory allocation and release can be tracked and updated in metadata. This metadata can include information such as the size of the applicable memory area and some content information (such as the potential null end of a string). At runtime, metadata can be used to track which base pointer it is pointing to for each pointer and therefore which memory allocation it is pointing to. Therefore, for any memory access, metadata can identify the size of the allocated memory area. Therefore, when the code is executed, metadata is essentially a collection or printout of logs of diagnostic information for each accessed storage area. For example, this diagnostic information can include: identifying compiler infrastructure or LLVM debugging information, such as program location, variable information, accessed memory byte address, the starting address of the accessed memory area (0 if it is an invalid access), the end address of the accessed storage area (0 if it is an invalid access), the next null end character of the accessed memory byte (-1 if not applicable) and other such information.
[0044] The state may indicate whether a security violation has occurred and also whether the execution as a whole is a "non-crashing execution" with no security violation or a "crashing execution" with a security violation. Examples of crashing executions may include heap buffer overflows, static memory overflows, use after freeing, and other runtime issues that lead to undefined behavior. For each type of crashing execution, there may be a set of commonly occurring patch patterns to consider. These patterns may be encoded as template patches for which it is necessary to find the appropriate instantiation of the code being patched.
[0045] The code, generated inputs, and execution state may be analyzed to determine conditions that are likely to have caused a security violation associated with one or more inputs. Figure 2 The code miner of code miner 240 may be used to compare the generated input and the execution state of the passing execution with the execution state of the failing execution to identify conditions that distinguish a non-crashing execution from a crashing execution. Figure 4Ais an example of sample code with a C string defect that causes an execution crash. In this example, the code miner can be used to identify multiple conditions, including: the string length of result->content is the same as the string length of content; the string length of result->content is the same as result->length; the string length of content is the same as result->length; the allocation size of result->content is 1 greater than the string length of result->content; the allocation size of result->content is 1 greater than result->length; etc. Many of these relationships may not be useful for generating patches, but at least some may be interesting, such as the relationship between the allocation size of result->content and the value of result->length.
[0046] The code miner can also generate patch conditions or invariants for those conditions. Figure 2 , the code miner 240 can generate "crash invariants" 250 and "non-crash invariants" 260, which are then incorporated into the program metadata 270 as input to the patch generator 280. Invariants can be abstract representations of the state of the program. For example, variable x=10. The crash invariants and non-crash invariants generated by the code miner can be dynamic invariants, because statically calculated invariants may be too imprecise for features such as non-standardized loops or recursive functions. Dynamic invariants can be abstract representations of conditions applicable to the execution set of the code, but static invariants can be statically calculated throughout all paths of the program. Dynamic invariants can be expressed as predicate templates discussed further below, such as some_variable=symbolic_buffer_length (related to the symbolic buffer length discussed further in the following examples), some_variable=some_other_variable (where some_other_variable will include the symbolic buffer length), some_variable+some_constant≤buffer_size, and so on. Dynamic invariants from across multiple executions can be combined to create more complex dynamic invariants, thereby creating more complex patches. These crash invariants and non-crash invariants can be incorporated into the program metadata that is input into the patch generator.
[0047] Dynamic invariants can be determined in various ways. For example, dynamic invariants can be determined based on user input. In some cases, it is advantageous to use one or more machine learning techniques such as Daikon, decision trees, classifiers, etc. to determine the differences, correlations, and invariants between non-crash executions and crash executions. For example, the generated inputs and the above metadata can be used to train a model. In some instances, as training inputs for a machine learning model, they can be clustered based on whether the generated inputs result in security violations at different code locations and / or failed executions. They can be clustered based on whether the generated inputs reach a certain region of interest in the code.
[0048] Such as Figure 2 The patch generator of the patch generator 280, such as, can then automatically generate patches from the program metadata 270, such as candidate patches 290. The candidate patches can include patch conditions and patch locations. At least one candidate patch can cause the violating input associated with the security violation to discard execution and / or record execution, so as to allow the operator to observe the problem in real time as discussed further below and further use the logs to investigate.
[0049] For example, the patch generator 280 can generate candidate patches by calculating the differences between crash invariants and non-crash invariants. In other words, by analyzing the passing and failing executions, the differences in the values of variables (i.e., the differences in dynamic invariants) can be determined. For example, if the size of a buffer has been tracked within an existing variable that was in range when a buffer overflow occurred, this can allow the patch generator 280 to generate a patch.
[0050] The calculated differences can be used to generate patches, at least some of which will correspond to predefined templates. For example, based on the differences between the execution states of passing and failing executions and the type of crash observed, templates can be identified. These templates can include various conditions such as "some_variable < some_other_variable". In the foregoing example, all uppercase values represent "symbolic conditions" for the template of the patch that may not otherwise exist in the code. As an example, the template can be "BUFFER_INDEX < BUFFER_SIZE". This template can be identified when there is a buffer overflow or when the code attempts to read the buffer at a location outside the length of the buffer. As pointed out above, both BUFFER_INDEX and BUFFER_SIZE can be "symbolic conditions", i.e., conditions representing the buffer index and buffer size of the buffer used in the code that may not necessarily be in the code.
[0051] As another example, the template could be "ptr_arithmetic_expression<BASE_POINTER+BUFFER_SIZE". This template can be identified in cases of buffer overflow caused by pointer arithmetic errors. In this example, "ptr_arithmetic_expression" could correspond to the calculation that the code performs to index into memory, and both BASE_POINTER and BUFFER_SIZE could be "symbolic conditions" that do not necessarily appear in the code and represent the base pointer and buffer size of the buffer used in the code.
[0052] Figure 5 is an example of simple code. In Figure 5 the example, a buffer overflow security violation occurs due to pointer arithmetic exceeding the bounds of the buffer end. If the buffer pointed to by dest is the same size as or smaller than the size parameter of the function where the dest buffer will overflow, a security violation occurs within DuplicateLastValue. In this specific example, the proper use of the function is in the function NonBuggyUser: the buffer str contains three elements, a new buffer duplicated is created to be size 4, and both `str` and `duplicated` are passed to DuplicateLastValue. The result is that `dest` contains the value of `str` with the last item being duplicated, i.e., duplicated == {'a', 'b', 'c', 'c'}. However, in BuggyUser, `str` and `duplicated` are the same size. When these values are passed to DuplicateLastValue, the initial memcpy copies the contents of the source argument into `dest` without overflow, but the subsequent write to `dest+size`, i.e., dest+3, is out of bounds because the size of `dest` is 3. In this case, the template ptr_arithmetic_expression<BASE_POINTER+BUFFER_SIZE can be identified, and here, `ptr_arithmetic_expression` is `dest+size`. BASE_POINTER and BUFFER_SIZE come from BuggyUser() or NonBuggyUser(). In this example, the generated patch (after being adjusted for the weakest precondition discussed further below) can be placed in NonBuggyUser.
[0053] As another example, the template can be BUFFER_SIZE < STRLEN_SRC. This template can be identified when there is a string buffer overflow, where STRLEN_SRC refers to the length of the string being read. Such a security violation can occur when the C string library expects the string buffer to be null-terminated.
[0054] Figure 6 Is an example of simple code. In this example, a buffer overflow occurs within the call to `strcpy` in the BuggyStrCpy() function. The C standard strcpy function copies from `src` to `dest` until the null terminator is read in `src`. In this case, the template BUFFER_SIZE > STRLEN_SRC can be identified. In this case, BUFFER_SIZE is the size of the buffer passed as the `dest` argument to `strcpy`, and STRLEN_SRC is the string length of the `src` argument to `strcpy`. Within this example, BUFFER_SIZE will be replaced with 5 and STRLEN_src will be replaced with strlen(src). Note that using the strlen function requires `src` to be null-terminated. This can be determined based on the execution status of both the passing and failing executions of the content of "src". This type of template can be used to patch errors caused by the lack of a null terminator on the buffer.
[0055] Symbolic conditions that capture the fact that a concrete pointer must point to some memory location can be used to dynamically capture alias relationships. For example, this can be useful in determining that a pointer within the code only points to the buffer used in a crash. This is useful because a pointer variable can point to multiple storage locations, for example, within a function that has a pointer argument that has been passed multiple different memory locations. In other examples, instead of using explicit templates, heuristics or other information can be used to generate patches. For example, in the case of a null pointer, dereferencing occurs when the dereferenced pointer points to a specific location (nullptr) that is always in range. Therefore, if dereferencing occurs on ptr (the pointer), the patch can include checking that ptr != nullptr.
[0056] For example, in the case of a numeric overflow, where int value = (int)result and int refers to an integer, templates may not be required because the generated patch can simply look at the type. Therefore, the patch for a numeric overflow can always be (for int overflow) result >= std::numeric_limits <int>::lowest()&&result<std::numeric_limits <int>::max().
[0057] Figure 7 Examples of simple code are provided where an error occurs at the end of a specific branch within a function. In this example, the code reads uninitialized memory when `c` is false. Since the general idea of reading uninitialized memory would not be suitable for a template, in this example, the patch generator may rely on the differences between the mining execution and the failing execution. In this case, the error occurs only when `buffer` is read and `c` is false. Again, certain heuristics will likely be useful in these cases, for example, limiting the variables used within a crash to those defined "close" to where the crash occurs (e.g., within the same function, within the same library / compile unit, within N call frames of the error stack trace). This can prevent spurious differences from being included in the patch, such as changes to global variables.
[0058] When a security violation of a certain type is known or suspected and a template is identified, any "symbolic conditions" for the template for the security violation can be set first. For example, in many cases, the variables required for a patch do not actually exist in the program, e.g., the size of a buffer is not actually explicitly tracked. Thus, these symbolic conditions allow variables to be inserted into the program (symbolic conditions) in order to use these symbolic conditions to simplify the generation of the patch. Thus, the symbolic conditions represent the conditions or values that cause a security violation (or its absence) and can be used as a basis for defining the actual code that constitutes a candidate patch. To avoid blindly inserting variables and setting them with arbitrary values, in some instances, the patch can be generated by selecting a template language or expression and then customizing these template languages or expressions to trigger or avoid a security violation by causing (or preventing) the symbolic condition to occur or exiting the execution in the case of the occurrence of the symbolic condition.
[0059] For example, the identified template can include BUFFER_INDEX < BUFFER_SIZE. Thus, using this template, the symbolic condition for buffer overflow can be set to 0 ≤ BUFFER_INDEX < BUFFER_SIZE. For example, Figure 3A Example code is provided. In this example, `idx` is BUFFER_INDEX, and `length` is BUFFER_SIZE. Before executing buff[idx], assert: 0 <= idx < length or in the code: assert(0 <= idx); The assert(0 <= idx) can be added as a patch as shown in Figure 3B below.
[0060] However, which variable in the code contains the BUFFER_INDEX or BUFFER_SIZE that is causing the security violation may not be straightforward. In fact, the code itself may not have actual variables containing BUFFER_INDEX or BUFFER_SIZE. Therefore, the symbolic condition of 0 ≤ BUFFER_INDEX < BUFFER_SIZE must be materialized by substituting actual variables or actual values in the code during execution.
[0061] Returning to Figure 4A the example of, a non-crashing execution of the TrimTrailingCharacter function can fully guarantee that str->length is greater than 0 when entering the function. However, a crashing execution at this line enters with str->length set to 0. Therefore, the potentially learned patch would be to add a crash before decreasing str->length to ensure it is not 0. Another interesting difference in the learned relationships would be during memory access. At that time, all non-crashing executions guarantee that the string length of str->content is equal to the value of str->length. This is not the case in a crashing execution. Therefore, a patch can be generated by the patch generator for this after the decrement operation that checks this relationship.
[0062] The patch generator 280 can generate candidate patches for different conditions in various ways. For example, in Figure 4B the patched code of the example, two competing patches are shown (merged into a single example for simplicity, but only a single patch is needed when in use). By generating multiple patch conditions and different versions of the patched program, these can be compared to each other. For example, this may be useful for preventing defects in the patch generation process that could otherwise lead to bad patch proposals.
[0063] The code, the generated inputs, and the execution state can also be analyzed by the patch generator to determine the corresponding patch locations where the patch conditions are likely to occur. For example, a patch can be inserted immediately before the code location where a security violation or an unexpected behavior is observed.
[0064] Fig. 8A is an example of a simple code. In this example, the function RoundedAngle returns a value in [0,360) or fails with an error code if the parameter angle is negative. It can be assumed that this function is used in some code or application where the value of the parameter angle can be provided by the programmer in some way. After executing the code with the generated input, an input that causes undefined behavior when executing this code is found. In particular, a floating point value (or non-number, also known as NaN) that is larger than the largest representable integer is passed as the parameter angle. Running this code with a computer error detector such as UndefinedBehaviorSanitizer (UBSAN) may report bad casts (unexpected behavior) at code locations or lines marked as floating point to integer casts. In particular, this execution may identify that undefined behavior occurs due to the fact that the value of the parameter angle is too large. To address this undefined behavior, a patch may be generated, such as Figure 8B Example of a patch for patched code. In this example, a program abort is introduced as a check before the code location where a bad cast occurs. The introduced check is very syntactic in such scenarios and can be easily automated. Other typical cases may include the processing of C strings, where a patch for a buffer overflow can be described as a missing null terminator or an expected range check. The check can also contain regular expression matching for certain types of errors.
[0065] Each of the candidate patches may be inserted into the code at the location of the candidate patch to create a set of candidate patched code. Each of the candidate patched code may then be executed using the generated input to obtain a patched execution state. This may allow changes between the execution state and the patched execution state to be identified for the same generated input. Figure 8B The patch introduces a program abort at the point of failure. It checks that the upcoming cast from the floating point variable angle is too large for the int type (std::numeric_limits <int>::max() returns the largest representable number of type int). If this condition fails, the LOG_IF statement will log the error message "Auto generated path to avoidundefined behavior due to overflow". Therefore, the execution will be aborted due to the use of the fatal logging level.
[0066] In this example, the candidate patch is particularly beneficial because it only patches the undesired behavior. A method that does not rely on actual execution could also have generated a check for underflow behavior. That is, such a method would have likely managed to introduce a check that also ensured that negative angles were definitely too large. Since the generated input was not produced, such a patch would not be proposed. Of course, the reason this does not happen is that due to the earlier checks in the program, any negative angle values will not reach the cast.
[0067] Additionally or alternatively, candidate patches may be generated for security violations by determining the weakest precondition based on the aforementioned template as discussed above. Fig. 9 is an example functional diagram 900 that utilizes weakest preconditions 940 to identify aspects of candidate patch generation for code set 210. As shown, the weakest preconditions may also be incorporated into program metadata 270 that is input into patch generator 280 to generate candidate patches 990.
[0068] The weakest precondition can be a formula that describes a program state or set of program inputs that exhibit some common behavior. As with invariants, the weakest precondition can also be enforced statically or dynamically or at each execution phase of the code. The weakest precondition for safety violations at the execution phase can imply that if the weakest precondition is true, then assuming that all nondeterministic choices such as thread scheduling are not changed in re-execution, the execution will result in a safety violation.
[0069] Fig.10 is an example of a simple program to further illustrate the weakest condition. In this example, rand100() returns a random number between [0,100). In this example, assume that a crash CRASH() occurs when x2 is 95 and y2 is 80. For the purposes of this example, the goal is to determine the return value of the call to rand100() rather than the condition at the location of the actual crash.
[0070] Rather than generating a candidate patch that blocks only this specific scenario (x2=95 and y2=80), or equivalently blocks (x=93 and y=8), it may be more useful to generalize these conditions. For example, the condition x2>y2 could be expressed as x>10*y-2. This generalized condition could be the weakest precondition and represent more cases, such as, for example, any value of x if and only if y is 0. Since a crash was observed, there is at least one input that caused the crash to occur; the weakest precondition will never be empty. At a minimum, the weakest precondition will contain at least a single crashing input that was used to generate the weakest condition, but this is unlikely to happen because generalization from a single crashing input will often cover a variety of different possible conditions.
[0071] The weakest precondition can be calculated along the entire execution trace in a crash run, or more precisely at each program execution stage. Such a weakest precondition at an execution stage can imply that if the condition is true, the execution will definitely fall into the exact same crash again, assuming that all non-deterministic choices such as thread scheduling are not changed in the re-execution. That is, the weakest precondition of the crash run at a certain program point can be used to effectively predict security violations.
[0072] The weakest precondition provides an advantage in this context because it can be calculated locally at the crash site (i.e., the location where the crash is performed in the code). The closer the condition can be expressed to the crash site, the less complex the weakest precondition is likely to be and the more readable it is. As an example, a user develops a library XYZ that depends on the features of another library ABC. In this example, library ABC also depends on library DEF. Since library XYZ is obfuscated, a security violation is observed in library DEF. In this case, the security violation is now "double dependency depth". The user is most likely to be interested in identifying security violations in library XYZ because the user is developing the code. However, if a security violation occurs inside library DEF, it is unclear whether the security violation occurs in how XYZ uses ABC, how ABC uses DEF, or in DEF itself? Therefore, if the patch can be promoted to library XYZ, which is local to what is obfuscated, this may be preferred.
[0073] Additionally or alternatively, the patch generator may generate candidate patches based on tracking data flowing through the memory state collected by the scrubber log. Fig.11 is an example functional diagram 1100 that utilizes a scrubber log 1140 to identify aspects of candidate patch generation for a code set 210. As shown, the scrubber log may also be incorporated into program metadata 270 that is input into a patch generator 280 to generate a candidate patch 1190.
[0074] Various tools can be added to the code to allow tracking of memory allocations, deallocations, and accesses to generate cleaner logs for memory-related security violations. For example, ASAN can be used to track such memory data. Since ASAN is only used for patch generation and not during production, there may be little or no impact on production performance. In addition, selective memory tracking can be used even during patch generation. For example, ASAN can be selectively turned on only for certain code locations or variables to minimize the impact on performance. Other runtime monitoring techniques, such as Valgrind or other LLVM cleaners, can also be used.
[0075] Additionally or alternatively, the patch generator may generate candidate patches based on static information. For example, variable names and variable types may be used as additional information for generating patches. For example, reifying symbolic conditions with variables in the code may be based on such static information. For another example, some weakest preconditions may also be statically computed. For yet another example, some invariants may also be statically computed. Fig.12 is an example functional diagram 1200 that utilizes static information 1240 to identify aspects of candidate patch generation for code set 210. As shown, the static information may also be incorporated into program metadata 270, which is input into patch generator 280 to generate candidate patches 1290. As noted above, the patch generator may thus use program metadata to generate candidate patches. Fig.13 is an example functional diagram 1300 where program metadata 270 includes crash invariants 250 , non-crash invariants 260 , cleaner logs 1140 , static information 1240 , or any combination of these. Again, this program metadata is input into patch generator 280 to generate candidate patches 1390 .
[0076] The patch generation process can be an iterative process. For example, after using one of the candidate patches to patch the code, the newly generated input can be used to generate new violation inputs and non-violation input sets for the patched code. As in the above example, these generated inputs can be generated using fuzzy techniques. The patched code can be executed using the newly generated input. Again, for each execution, the state at each code location is monitored. For example, the state can show that some of the new inputs cause new security violations. The new security violation can be the same security violation as before the patch, a different security violation related to the security violation before the patch, or a different unrelated security violation. At this point, more data can be used for analysis, or more precisely can be added to program metadata 270, which includes code, patched code, input generated for code, input generated for patched code, execution state of using code, and execution state of using patched code. Then the program metadata 270 can be analyzed to determine the new patch conditions that are likely to cause new security violations and the corresponding patch positions where the new patch conditions are likely to occur. Based on these analyses, at least one new candidate patch can be generated for the new security violation.
[0077] In addition, an iterative process can be used to determine the incremental impact of a candidate patch. For example, non-violating inputs to the code before the patch should remain non-violating inputs when used in the execution of the patched code. This ensures that the patch does not cause a pass execution failure. If non-violating inputs to the code before the patch now cause a violation in some way when used in the execution of the patched code, this means that the candidate patch has introduced an execution failure to the code and is therefore not a good candidate patch.
[0078] To select the "best" or "winning" candidate patch for a given security violation, an iterative process may be used. The process may begin by generating multiple candidate patches using the same method with different input sets and / or using different methods as described above. Fig.14 As shown in the example functional diagram 1400, "patch generator 1" can use the template-based weakest precondition to generate the first candidate patch, "patch generator 2" can use the template-based dynamic invariant to generate the second candidate patch, "patch generator N" can use the same code miner but with a different set of generated inputs to generate the "Nth" candidate patch, etc. In this example, Fig.14 Each of the patch generators of can correspond to the patch generator 280 because it is the same instance or the same instance. Similarly, the first, second and Nth candidate patches can correspond to any one of the candidate patches 290, 990, 1190, 1290 or 1390.
[0079] The candidate patches and the test corpus 220 may be input into a candidate selector such as the candidate selector 1410, which selects or determines a winning patch from the candidate patches. The winning candidate patch 1490 may be determined based on the execution state of the patching, for example, a suitable candidate patch may be one that causes violating inputs associated with a security violation to be discarded from execution but still allows all non-violating inputs to pass through execution. As an example, one candidate patch may slow down performance less than other candidate patches and may therefore be a suitable candidate patch.
[0080] Additionally or alternatively, the winning candidate patch may also be selected based on the complexity of the candidate patch and / or the proximity of the candidate patch to the security violation site. For example, if a candidate patch has a very complex patch condition, an operator may be reluctant to accept the candidate patch because it may be difficult to fully understand the impact of the candidate patch on the code. For another example, although a security violation may occur due to misuse of a very low-level library, a candidate patch with a patch location at such a low-level library may cause problems with many other codes sharing the same library, and therefore the candidate patch is inappropriate and should not be the winning patch.
[0081] The winning patch can be inserted into the code at the location of the patch so that the code produces any number of desired results for the input that previously caused the security violation. For example, the winning patch can be a blocking patch that causes at least a portion of the code to suspend execution before the security violation occurs. In addition or alternatively, the winning patch can be a diagnostic patch that causes data about the security violation to be collected or otherwise recorded for immediate or later review. In addition or alternatively, the winning patch can cause a display of information about the security violation to be generated. Such a display can appear on the operator or user's computing device as a visual or audible warning, for example, to prompt appropriate action.
[0082] Such patches can also allow operators to observe the impact of security violations in real time and decide whether to ignore security violations, allow insertion of blocking patches or develop full repairs. For example, rather than simply recording security violations or displaying notifications in real time, counters can be used to track the frequency of specific security violations that have occurred. These counters that are used for different security violations can be viewed in real time for operator review, for example, by providing a graphical user interface or other visual and / or audible representations of these numbers. This can then help operators prioritize security violations for review and / or repair and help operators determine which security violations are or are likely to become urgent issues.
[0083] Fig.12 An example flowchart 1200 for automatically generating patches for security violations in accordance with various aspects of the present disclosure is provided. The blocks of the flowchart may be executed by one or more processors, such as one or more processors 112. In this example, multiple inputs for code are generated at block 1210. The code is executed using the multiple inputs at block 1220 to obtain execution states at multiple code location stages. The execution states include at least one security violation for at least one of the multiple inputs. At block 1230, one or more patch conditions that cause at least one security violation are determined using the execution states. At block 1240, one or more corresponding patch locations where each of the one or more patch conditions occurs based on a code location where at least one security violation occurs in each of the one or more patch conditions in the multiple code locations are determined using the execution states. At block 1250, at least one candidate patch for the security violation is automatically generated. Each of the at least one candidate patch includes one of the patch conditions and one of the corresponding patch locations.
[0084] The methods and flow charts provided herein can be implemented as a computer program comprising computer-readable instructions that cause a processor to execute the methods and flow charts as described.
[0085] Again, the technology described herein is advantageous because it protects vulnerable code from attacks via security violations even before a complete fix is available. The technology provides automatic generation of patches for security violations without the operator having to wait for a complete fix. Although these patches do not completely fix the logic of the program, they can cause the code to be suspended before a security violation occurs, thereby strengthening or protecting the code from exploitation. This has the advantage of reducing the time to mitigate potential exploitation from months to hours. In addition, in some implementations, it is advantageous to use heuristics and machine learning to select patches that prevent security violations but still allow automatically generated patches to be executed with non-violation inputs. The technology further provides an iterative process to verify the generated patches, analyze the impact of the patches, and produce more suitable patches for selection. In addition, the patches can have alternative or additional functions, such as collecting data for better forensic analysis and displaying security violations to warn developers and users.
[0086] Unless otherwise stated, the foregoing alternative examples are not mutually exclusive, but can be implemented in various combinations to achieve unique advantages. Since these and other variations and combinations of the features discussed above can be utilized without departing from the subject matter defined by the claims, the foregoing description of the embodiments should be made by way of illustration rather than by limitation of the subject matter defined by the claims. In addition, the provision of the examples described herein and clauses represented by phrases such as "such as", "including", etc. should not be interpreted as limiting the subject matter of the claims to specific examples; on the contrary, the examples are intended to illustrate only one of many possible embodiments. In addition, the same reference numerals in different figures can identify the same or similar elements.
[0087] Industrial Applicability Statement
[0088] The features described herein enjoy wide industrial applicability, for example, in the field of software security and development.< / int> < / int> < / int>
Claims
1. A method for automatically generating a patch for a security violation, comprising: generating a plurality of inputs for the code by one or more processors; executing, by the one or more processors, the code using the plurality of inputs to obtain an execution state at a plurality of code locations, the execution state including at least one security violation for at least one of the plurality of inputs; identifying, by the one or more processors, one or more code conditions that caused the security violation; determining, by the one or more processors, one or more patch conditions for the identified one or more code conditions using the execution state, including at least one dynamic invariant related to the identified one or more code conditions, wherein the at least one dynamic invariant is determined using a machine learning model trained using a plurality of inputs generated for code, and wherein the at least one dynamic invariant comprises a condition that applies to execution of the code; generating, by the one or more processors, program metadata based on the one or more patch conditions; using, by the one or more processors, the execution state to determine one or more corresponding patch locations based on the identified one or more code conditions; as well as At least one candidate patch is automatically generated, by the one or more processors, for at least one security violation based on the program metadata, the at least one candidate patch comprising one of the patch conditions and one of the one or more corresponding patch locations. 2 . The method of claim 1 , further comprising generating, by the one or more processors, a patched code by inserting the at least one candidate patch into the code.
3. The method according to claim 1, wherein: The at least one candidate patch is configured to cause at least some of the plurality of inputs associated with the at least one security violation to be discarded for execution.
4. The method according to claim 3, wherein: The at least one candidate patch halts execution of at least a portion of the code before a security violation could occur.
5. The method according to claim 1, wherein: The at least one candidate patch is configured to generate a log of diagnostic information for the security violation.
6. The method according to claim 1, further comprising: generating, by the one or more processors, a candidate patched code set by inserting each of the one or more candidate patches into the code; executing, by the one or more processors, each of the candidate patched code using the plurality of inputs to obtain execution states of the patches at the plurality of code locations; selecting, by the one or more processors, a candidate patch among the one or more candidate patches based on an execution state of the patching; as well as A second patched code is generated by the one or more processors by inserting the selected candidate patch into the code.
7. The method according to claim 1, further comprising: selecting, by the one or more processors, a candidate patch from the at least one candidate patch based on at least one of complexity, proximity to a site of the at least one security violation, or impact on execution of the code; as well as Patched code is generated by the one or more processors by inserting the selected candidate patch into the code.
8. The method according to claim 7, further comprising: generating, by the one or more processors, a new plurality of inputs for the patched code; executing, by the one or more processors, the patched code using the new plurality of inputs to obtain a new execution state at the plurality of code locations, the new execution state including a new security violation; using the new execution state to compute, by the one or more processors, one or more new patch conditions that cause the new security violation associated with one or more of the plurality of inputs; using the new execution state, by the one or more processors, to determine one or more new corresponding patch locations at which each of the one or more new patch conditions occurs; as well as At least one new candidate patch is generated by the one or more processors for the new security violation, each of the at least one new candidate patch includes one of the new patch conditions and one of the new corresponding patch positions, wherein the at least one new candidate patch is configured to cause the new violating input associated with the new security violation to be discarded from execution.
9. The method according to claim 1, wherein: The generated multiple inputs for the machine learning model are clustered based on whether the generated multiple inputs result in security violations or abandoned execution at different code locations, and based on whether the inputs reach an area of interest in the code.
10. The method according to claim 1, wherein: The dynamic invariants include predicate templates.
11. The method of claim 1 , further comprising combining two or more of the dynamic invariants to determine the one or more patch conditions.
12. The method according to claim 1, wherein: Generating the one or more patch conditions further includes determining a weakest precondition related to the at least one security violation.
13. The method of claim 1, further comprising generating the one or more patch conditions including tracking data flow through memory states collected by logs.
14. The method according to claim 13, wherein: Tracking data flow through memory state includes using selective memory tracking.
15. A system for automatically generating a patch for a security violation, the system comprising one or more processors, the one or more processors being configured to: Generate multiple inputs for code; executing the code using the plurality of inputs to obtain an execution state at a plurality of code locations, the execution state comprising a security violation for at least one of the plurality of inputs; identifying one or more code conditions that caused the security violation; determining one or more patch conditions for the identified one or more code conditions using the execution state, including at least one dynamic invariant related to the identified one or more code conditions, wherein the at least one dynamic invariant is determined using a machine learning model trained using a plurality of inputs generated for code, and wherein the at least one dynamic invariant comprises a condition that applies to execution of the code; generating program metadata based on the one or more patch conditions; using the execution state to determine one or more corresponding patch locations based on the identified one or more code conditions; as well as At least one candidate patch is automatically generated for at least one security violation based on the program metadata, the at least one candidate patch including one of the patch conditions and one of the one or more corresponding patch locations.
16. The system of claim 15, wherein: The one or more processors are further configured to generate patched code by inserting the at least one candidate patch into the code.
17. The system of claim 15, wherein: The at least one candidate patch is configured to cause at least some of the plurality of inputs associated with the at least one security violation to forgo execution and to cause at least a portion of the code to halt execution before a security violation may occur.
18. The system of claim 15, wherein: The at least one candidate patch is configured to generate a log of diagnostic information for the security violation.
Citation Information
Patent Citations
Position analysis of source code vulnerabilities
US9792443B1
