Non-restart code repair method using memory gap
By utilizing memory gaps, the non-restart code repair method solves the problems in the existing technology that code repair requires restarting and high system overhead. It achieves seamless code repair, improves system security and performance, and is suitable for different types of code repairs. patch.
Patent Information
- Application Number
- CN202410784904.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-18
- Publication Date
- 2024-09-03
- Estimated Expiration
- 2044-06-18
AI Technical Summary
Existing code repair methods require restarting the software or device when repairing the code, resulting in service interruption and introducing system overhead and memory access slowdown.
By using memory gaps, we screen the target objects for code repair, search the memory layout, manage and store memory gap information, replace function pointers or parameter structures, generate code repair plans, update the kernel symbol table, and use the HOOK mechanism to achieve code repair without restarting.
It achieves seamless code repair, improves system security and stability, optimizes system performance, meets the continuity and reliability of computing services, reduces system overhead, and maintains efficient memory access.
Smart Images

Figure CN118586005A
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a non-restart code repair method utilizing memory gaps. Background Art
[0002] There are many vulnerabilities in modern software and systems, which need to be repaired by updating patches. Updating patches to repair code can effectively repair vulnerabilities or improve code functions, enhance the security, robustness and integrity of software and systems, and is an important part of modern computing systems and an important part of computing system maintenance. However, traditional code repair solutions usually require restarting software or devices, causing service interruptions and losses. In order to ensure the stability of services, some service providers would rather skip updates or give up repairs, which will cause vulnerabilities in system services to remain unrepaired for many years, posing security risks. Attackers can launch attacks on these unrepaired vulnerabilities, steal user sensitive information, and threaten user information privacy and security. Therefore, it is urgent to design a reliable restart-free code repair method to solve the problem that code repair requires restarting software or devices.
[0003] Hot patching is a widely used code repair technology. Literature [1] SINIAVINE M, GOELA. Seamless kernel updates [C] / / 2013 43rd Annual IEEE / IFIP International Conference on Dependable Systems and Networks (DSN). IEEE, 2013: 1-12., [2] C. Islam, V. Prokhorenko, and MA Babar, "Runtime software patching: Taxonomy, survey and future directions," Journal of Systems and Software, vol. 200, p. 111652, 2023., [3] ZHOU L, ZHANG F, LIAO J, et al. KShot: Live Kernel Patching with SMM and SGX [C] / / 2020 50th Annual IEEE / IFIP International Conference on Dependable Systems and Networks (DSN). IEEE, 2020: 1-13., [4] POIMBOEUF J,JENNINGSS.Introducing kpatch: Dynamic kernel patching[J].Red Hat Enterprise LinuxBlog,2014,26., respectively use virtualization, data replacement mechanism, binary instrumentation, and memory direct writing technology to perform code repair. The above literature conducts in-depth research on the replacement of target data structures, the selection of replacement strategies, and the verification of code integrity and correctness after replacement.
[0004] References [1]-[4] and [5] I. Mugarza and JC Mugarza, “Cetratus: Live updates in programmable logic controllers,” in IEEE International Workshop of Electronics, Control, Measurement, Signals and their application to Mechatronics, pp. 1–7, IEEE, 2021. When performing code repair, since the program and device cannot be restarted, it is necessary to obtain expansion space in the original program's memory space and ensure its relevance to the original data. Since it is impossible to introduce a new memory layout through recompilation, the newly added or adjusted data structure of the patch cannot be properly placed in the memory. Usually, the patch repair scheme will add a jump instruction to the original data structure memory. When it is necessary to access the updated data structure and data unit, the control flow is transferred to a centralized management unit responsible for control flow jumps through the jump instruction. The center completes the jump matching, thereby realizing access to the repaired data in the memory. This method can modify the data structure, function call, and code instruction in the memory to a certain extent. However, this code repair method requires opening up a new memory space and setting up a central management unit responsible for control flow transfer. On the one hand, this centralized management unit needs to match and verify the source address and target address of the control flow transfer request, which introduces huge system overhead. On the other hand, it destroys the internal relationship of the data structure, causing the memory access speed to decrease after the code is repaired. At the same time, some memory units in the memory that are directly accessed using address offsets can no longer be accessed, destroying the semantics of the original code.
[0005] Therefore, the existing background technology cannot meet the requirements of repairing the code without restarting and without introducing excessive operating overhead. It is urgent to design a repair method suitable for code repair without restarting and fast access to memory data. In the process of code repair, the spatial layout characteristics of the memory during code repair can be considered, and the operating efficiency can be improved after the code is repaired. Summary of the invention
[0006] The purpose of the present invention is to provide a non-restart code repair method using memory gaps to achieve seamless repair and update of codes with security defects, improve system security and stability, optimize system performance, and meet the continuity and reliability of computing services.
[0007] The technical solution of the present invention is: a non-restart code repair method using memory gaps, comprising the following steps:
[0008] Step 1: Screen the target object of the repair code;
[0009] Step 2: Search the memory layout, manage and store the information of the retrieved memory gaps, and match the space information corresponding to the repair code;
[0010] Step 3: By comparing the modification requirements of the function in the repair code, replace and modify the function pointer or function parameter structure to generate a code repair plan;
[0011] Step 4: Update the kernel symbol table information;
[0012] Step 5: Write the data and data structure that need to be repaired of the target object into the memory gap, generate a kernel module according to the code repair solution, and implement code repair without restarting the system by loading the kernel module and using the HOOK mechanism.
[0013] Preferably, the screening of objects for repairing codes comprises the following steps:
[0014] Step 1.1: Determine whether the object of the repair code is a replacement class repair object;
[0015] If yes, then perform object replacement and complete code repair;
[0016] If no, proceed to step 1.2;
[0017] Step 1.2: Determine whether the object of the repair code meets the repair object type;
[0018] If yes, go to step 2; if no, restart the system.
[0019] Preferably, step 2 further includes determining the space size of the memory gap and the address offset of the memory gap through the memory address offset of the data member, and storing them in a hash table;
[0020] The size of the memory gap is calculated by the following formula:
[0021] gap_capacity=next_location-size-member_location
[0022] The address offset of the memory gap is calculated as follows:
[0023] gap_location=next_location-gap_capacity
[0024] Among them, gap_capacity is the size of the memory gap for storing the repair code, next_location is the memory address offset of the next data member, size is the actual memory size occupied by the current data, and member_location is the memory address offset of the data member.
[0025] Preferably, the memory gap space includes a native gap and a splicing gap.
[0026] Preferably, in step 3, the modification of the function in the repair code solution specifically includes:
[0027] If the function prototype is not changed in the function call, the function pointer is replaced in the repair solution to call the replaced function pointer after the repair is completed;
[0028] If the function prototype is changed during the function call, the parameter passing rules are modified first, and then the function pointer is replaced, so that after the repair is completed, the replaced function pointer is called and the modified calling parameters are used;
[0029] Preferably, step 3 also includes modifying the kernel compilation configuration file to enable the SYSFS virtual file system, the dynamic module loading function, the DYNAMIC_FTRACE_WITH_REGS feature and the HAVE_RELIABLE_STACKRACE trusted stack feature.
[0030] Preferably, the replacement function pointer is specifically: using the kallsyms_lookup_name() function to link the memory function involved in the repair process, determining the distribution and memory offset of the memory function in the memory, and replacing the memory function with the repaired function pointer.
[0031] Preferably, the use of the HOOK mechanism is specifically as follows: the running thread still accesses the original data and the original data structure; when the last thread ends and a new thread starts, the data and data structure in the memory gap are accessed.
[0032] Compared with the prior art, the present invention has the following beneficial effects:
[0033] (1) By utilizing the tiny gaps between memory objects during program execution to load new data and data structures after code repair, it is possible to change system operation and repair vulnerable code without recompiling or restarting the system and services, which is highly practical.
[0034] (2) By repairing the code in the existing memory layout, there is no need to introduce additional memory space. The repaired data can be accessed in the form of native address offset. Since the memory gap itself is close to the original data location, according to the principle of program locality, it does not interfere with the work of the CPU cache. The impact on the data access efficiency after the code repair is negligible, with high operating efficiency and low system overhead.
[0035] (3) By utilizing the module generation and loading functions, the new and old data exist in the memory at the same time, providing a stable switching method for multi-threaded memory management and ensuring data consistency;
[0036] (4) The present invention is applicable to different types of code repair patches in real environments and has good universality. BRIEF DESCRIPTION OF THE DRAWINGS
[0037] Figure 1 It is a schematic diagram of the process of the present invention;
[0038] Figure 2 A function execution time comparison chart for measuring the execution overhead of an embodiment of the present invention;
[0039] Figure 3 A statistical diagram of the number of memory data structures and the number of structures with gaps for measuring the availability of the embodiment of the present invention;
[0040] Figure 4 A statistical diagram of the number and capacity of memory gaps in different data structures for measuring the availability of embodiments of the present invention. DETAILED DESCRIPTION
[0041] The technical solution of the present invention is described in detail below, but the protection scope of the present invention is not limited to the embodiments. To make the content of the present invention more clear and understandable, further description is given below in conjunction with the accompanying drawings and specific implementation methods.
[0042] In order to make the purpose, technical solution and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not used to limit the present invention.
[0043] like Figure 1 As shown, the present invention discloses a non-restart code repair method using memory gaps, comprising the following steps:
[0044] Step 1: Screen the target object of the repair code;
[0045] According to the official patch documentation, select the target objects of the repair code. The specific steps are as follows:
[0046] Step 1.1: Determine whether the target object of the repair code is a replacement class target object;
[0047] If yes, then perform object replacement to complete code repair; the replacement class target objects include the following types:
[0048] A. Description class: objects that modify documents, instructions, and introductions;
[0049] B. Configuration class: an object that modifies code option configuration files and configuration option items;
[0050] C. Value change class: an object that changes the value of a specific variable without changing the storage type of the data.
[0051] If no, proceed to step 1.2;
[0052] Step 1.2: Determine whether the target object of the repair code meets the repair object type;
[0053] If yes, go to step 2;
[0054] If not, restart the system; the target objects include the following categories:
[0055] A. Assembly-type objects in the boot phase marked with the macro init;
[0056] B. Objects that involve changes to ftrace, kprobe, and tracepoint kernel functions.
[0057] Step 2: Search the memory layout, manage and store the information of the retrieved memory gaps, and match the space information corresponding to the repair code;
[0058] Query the .debuginfo segment information and obtain member_location through the DWARF description of core_dump.
[0059] Then we have:
[0060] gap_capacity=next_location-size-member_location
[0061] Among them, gap_capacity is the size of the memory gap for storing the repair code, next_location is the memory address offset of the next data member, size is the actual memory size occupied by the current data, and member_location is the address offset of the data member in the memory layout.
[0062] The address offset of the gap is:
[0063] gap_location=next_location-gap_capacity
[0064] The address and space size information of all searched memory gaps are stored in a hash table for subsequent management and access of memory gaps.
[0065] For some memory gaps with smaller space, read macros and write macros are defined, and memory gaps at different locations are combined into available sizes based on combination pointers, which are also stored in the hash table.
[0066] The flag bit is set to indicate whether the memory gap is a splicing gap or a native gap.
[0067] The core of the code macro that writes the pointer ptr to the memory Memory_Address is:
[0068]
[0069] The core of the code macro to read specific content from the ptr pointer (memory address) is:
[0070] ((((EXTENDLEN)(ptr->top))<<0x20)+(EXTENDLEN)(ptr->bottom))
[0071] Step 3: By comparing the modification requirements of the function in the repair code, replace and modify the function pointer or function parameter structure to generate a code repair plan;
[0072] Analyze and compare the modification requirements for functions in the code repair solution, including:
[0073] ① If the modified data type is not used in the function call, that is, the data structure has not changed, only the called function object has changed, and the function prototype is still valid, then replace the function pointer in the repair solution, so that the replaced function pointer is called after the repair is completed to implement code repair;
[0074] ② If the number of parameters in the function call is modified, that is, the function prototype is changed, the parameter passing rules are modified first, and then the function pointer is replaced, so that after the repair is completed, the replaced function pointer is called and the modified calling parameters are used to implement code repair.
[0075] Since the function call parameters are only passed in the stack, there is no verification in the compiled binary code, so the replacement and modification of the function pointer is to complete the modification of the function call.
[0076] Modify the .config configuration file to enable the SYSFS virtual file system, the dynamic module loading function, the DYNAMIC_FTRACE_WITH_REGS feature, and the HAVE_RELIABLE_STACKTRACE trusted stack feature.
[0077] The kallsyms_lookup_name() function is used to link the memory functions involved in the repair process, determine the distribution of the memory functions in the memory and the memory offset, and replace the memory functions with the modified function pointers to implement code repair.
[0078] Step 4: Update the kernel symbol table information;
[0079] The kernel symbol table is updated, specifically by using System.map of vmlinux to query the kernel symbol table file, saving the queried loading symbols and their memory layout, and updating and managing the symbol table information after completing the code repair.
[0080] Step 5: Write the data and data structure that need to be repaired of the target object into the memory gap, generate a corresponding module according to the code repair solution, and implement code repair without restarting the system by loading the module and using the HOOK mechanism.
[0081] Replace the data and data structure that need to be repaired in the memory area, where global variables exist in the .data segment and local variables exist in the memory heap. Write the data and data structure that need to be repaired of the target object into the memory gap in the matching area. At this time, the original data area is not affected, and the original data and data structure are not changed. The modification of the memory generates a corresponding kernel module, and the kernel module is loaded using the insmod instruction. At this time, HOOK takes effect and takes over the memory access of the specific area. The running thread still accesses the original data and data structure. When the last thread ends and a new thread is started, the modified memory, that is, the data and data structure in the memory gap, is run. There is no need to recompile the code or restart it to complete the code repair.
[0082] The effectiveness, execution efficiency and usability of the embodiments of the present invention are verified from the following examples.
[0083] Example 1: Verification of the effectiveness of the embodiment of the present invention:
[0084] Eight different types of code vulnerabilities are downloaded from the Common Vulnerabilities & Exposures (CVE) database. The description of the vulnerabilities is shown in Table 1. After installing the repair code, the debug mode is enabled in the emulator to track whether the repair code is effective.
[0085] Modification of the memory gap vulnerability repair code, first, determine the modification type of the vulnerability, the code repair in Table 1 all involves the modification and replacement of data and data structure. The repair process involves file modification, data structure modification, and data insertion; then, through gdb to view the .debuginfo information, locate the address and size of the available memory gaps in the memory layout, splice the gaps that are too small in size in the form of combined pointers, and store the corresponding information in the hash table; then, modify the function call, modify the .config configuration file, enable the SYSFS virtual file system, dynamic module loading function, DYNAMIC_FTRACE_WITH_REGS feature and HAVE_RELIABLE_STACKTRACE trusted stack feature, replace the function pointer, and produce a code repair solution; then, search the hash table, search for memory gaps that meet the code repair requirements, write the data and data structure that need to be repaired into the memory gap, and generate a kernel module for the corresponding repair. Use the insmod instruction to load the kernel module, and use the HOOK mechanism to achieve the repair of code defects without restarting; finally, verify whether the code vulnerability still exists after this method and whether it can be repaired to prove the effectiveness of the present invention.
[0086] Table 1. Real vulnerability repair verification results
[0087]
[0088] Example 2: Execution Cost Measurement
[0089] In order to measure the overhead introduced by the code repair of the present invention, the rdtsc instruction is inserted to read the CPU timestamp when generating the kernel module. The number of clock cycles C0 obtained by executing the rdtsc instruction before the target function is executed, and the number of clock cycles C1 obtained by executing the rdtsc instruction after the target function is executed are used to calculate the time T between the two. The formula is as follows:
[0090]
[0091] Wherein, f represents frequency.
[0092] The execution time of the six functions involved in the repair process was monitored, and the average value was taken after 10 experiments. Then, the results before and after the repair were compared, and the results are shown in Table 2. It can be seen that compared with other code repair methods, the function call overhead generated by the present invention is close to the repair method of recompiling and restarting the service, and the execution efficiency is better than other code repair methods.
[0093] Example 3: Availability metrics for solutions
[0094] In order to verify that the embodiment of the present invention is applicable to different codes and kernel versions, different kernel versions are searched to see whether there are enough memory gaps and capacity to place the instructions required for code repair. The data structures in different kernel versions and the number of data structures that meet the requirements of the present invention are counted. The results are as follows: Figure 3 shown.
[0095] According to statistics, about 20% of the data structures in the kernel have memory gaps that can be used by the present invention. By counting the memory gaps and their capacities in common data structures, it is found that on average, each data structure has about 10 usable memory gaps for loading code repair instructions and memory rewriting, and the average memory gap space is about 55.6 bytes. It can be seen that the present invention can find enough and large enough memory gap space in conventional code for placing repair code.
[0096] The parts not involved in the present invention are the same as the prior art or can be implemented by using the prior art.
[0097] As described above, although the present invention has been shown and described with reference to specific preferred embodiments, it should not be construed as limiting the present invention itself. Various changes in form and details may be made without departing from the spirit and scope of the present invention as defined in the appended claims.
Claims
1. A non-restart code repair method using memory gaps, characterized in that: The steps include: Step 1: Screen the target object of the repair code; Step 2: Search the memory layout, manage and store the information of the retrieved memory gaps, and match the space information corresponding to the repair code; Step 3: By comparing the modification requirements of the function in the repair code, replace and modify the function pointer or function parameter structure to generate a code repair plan; Step 4: Update the kernel symbol table information; Step 5: Write the data and data structure that need to be repaired of the target object into the memory gap, generate a kernel module according to the code repair solution, and implement code repair without restarting the system by loading the kernel module and using the HOOK mechanism.
2. The method for repairing code without restarting by using memory gap according to claim 1, characterized in that: The method of screening objects for repairing codes comprises the following steps: Step 1.1: Determine whether the target object of the repair code is a replacement class target object; If yes, then perform object replacement and complete code repair; If no, proceed to step 1.2; Step 1.2: Determine whether the target object of the repair code meets the repair object type; If yes, go to step 2; if no, restart the system.
3. The method for non-restart code repair using memory gaps according to claim 1, characterized in that: Step 2 also includes determining the space size of the memory gap and the address offset of the memory gap through the memory address offset of the data member, and storing them in the hash table; The size of the memory gap is calculated by the following formula: gap_capacity=next_location-size-member_location The address offset of the memory gap is calculated as follows: gap_location=next_location-gap_capacity Among them, gap_capacity is the size of the memory gap for storing the repair code, next_location is the memory address offset of the next data member, size is the actual memory size occupied by the current data, and member_location is the memory address offset of the data member.
4. The method for non-restart code repair using memory gaps according to claim 1, characterized in that: The memory gap space includes a native gap and a splicing gap.
5. The method for non-restart code repair using memory gaps according to claim 1, characterized in that: In step 3, the modification of the function in the repair code solution specifically includes: If the function prototype is not changed in the function call, the function pointer is replaced in the repair solution to call the replaced function pointer after the repair is completed; If the function prototype is changed during the function call, the parameter passing rules are modified first, and then the function pointer is replaced, so that after the repair is completed, the replaced function pointer is called and the modified calling parameters are used.
6. The method for non-restart code repair using memory gaps according to claim 5, characterized in that: Step 3 also includes modifying the kernel compilation configuration file to enable the SYSFS virtual file system, dynamic module loading function, DYNAMIC_FTRACE_WITH_REGS feature, and HAVE_RELIABLE_STACKRACE trusted stack feature.
7. The method for repairing code without restarting by using memory gap according to claim 5, characterized in that: The replacement function pointer is specifically: using the kallsyms_lookup_name() function to link the memory function involved in the repair process, determining the distribution and memory offset of the memory function in the memory, and replacing the memory function with the repaired function pointer.
8. The method for non-restart code repair using memory gaps according to claim 1, characterized in that: The use of the HOOK mechanism is specifically as follows: the running thread still accesses the original data and the original data structure; when the last thread ends and a new thread starts, the data and the data structure in the memory gap are accessed.
Citation Information
Patent Citations
Inline hook method and system for obtaining contents of graphics card
CN106295326A
Binary software protection method by means of dynamic fine-grained code hiding and obfuscating technology
CN106650340A
Online fault location method, system and device for embedded measuring and control device
CN108519924A
IOS-bypassing system debugging detection method and device
CN110287123A
