Shared library deexpansion method based on binary analysis and intermediate file reconnection
Through methods based on binary analysis and intermediate file reconnection, the complex source code analysis and data dependency processing problems in shared library de-expansion technology are solved, achieving more thorough tailoring of shared libraries, and improving hard disk and memory utilization and security. .
Patent Information
- Application Number
- CN202310368734.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-04-07
- Publication Date
- 2024-10-15
AI Technical Summary
Existing shared library debloation technology has problems such as complex source code analysis requirements, inability to handle data dependencies, incomplete tailoring, and excessive granularity, resulting in waste of hard disk and memory and the existence of security vulnerabilities.
Using a method based on binary analysis and intermediate file reconnection, we obtain the external call interface list through binary analysis, build a shared library dependency graph, perform disassembly and dynamic tracking, combine static and dynamic function calling relationships, and select intermediate files for reconnection to achieve More thorough pruning of shared libraries.
It achieves more thorough cutting of shared libraries without source code and recompilation, especially the cutting of data segments, which improves the de-expansion effect, reduces the size of shared libraries, and enhances security.
Smart Images

Figure CN118779204A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of computer software technology, and in particular relates to a shared library de-expansion method based on binary analysis and intermediate file reconnection. Background Art
[0002] A shared library is an ELF format shared library file used by programs to call third-party libraries. It plays an indispensable role in current mainstream software development. However, with the increase in software requirements, in order to ensure compatibility and new development requirements, the functions included in shared libraries have gradually increased. For example, the number of shared libraries included in the CUDA Toolkit has increased by about 5 times from 2012 to 2021. In most cases, the program only needs to call part of the shared library or even a certain interface - a survey shows that 80% of users only use 20% of its functions when using the program. This causes the problem of shared library expansion: the program must install the entire huge shared library and load it into memory, but only use a small part of its functions, which causes a serious waste of hard disk and memory. Especially for some embedded systems and docker images with limited memory and secondary storage, similar software expansion problems are very serious - in these situations, the functions that need to be called are few and the size of the shared library accounts for a large proportion: the size of the lightweight image alpine using musllibc is 5.59MB and the size of its shared library is 3.9MB. On the other hand, this shared library expansion will cause redundant code in the memory, which will create some serious security vulnerabilities to bypass some existing defense measures against shared libraries.
[0003] To address the above issues, there is a shared library de-expansion technology: this technology mainly targets the program requirements in a certain scenario, trims the shared library, removes the unused function code and data in the scenario, and completes the de-expansion of the shared library. The existing mainstream de-expansion technologies are mainly divided into the following three categories: 1. De-expansion technology based on the compilation and loading stages; 2. De-expansion technology based on binary analysis; 3. De-expansion technology based on reducing the number of shared libraries. However, these technologies have the following disadvantages:
[0004] 1. De-bloating technology based on the compilation or loading stage requires code analysis and erasure, which requires source code. The overall implementation method is too complicated, and in some commercial scenarios, users' demand for source code cannot be met in many scenarios.
[0005] 2. The de-expansion technology based on binary analysis can only directly erase the code segment in the shared library, but cannot handle data dependencies, so the de-expansion effect of the shared library is not obvious.
[0006] 3. The overwriting and erasing based on binary files are based on memory pages, so some discontinuous code segments will be left behind, making the shared library pruning incomplete.
[0007] 4. The problem with the configuration-driven shared library tailoring method is that its granularity is too large to achieve precise tailoring, and the need to configure and tailor the functions of the application makes its use scenarios very limited. Summary of the invention
[0008] Based on the above reasons, the present invention discloses a shared library de-expansion method based on binary analysis and intermediate file reconnection, which adopts a method based on binary analysis and intermediate file reconnection, and can complete the tailoring of the shared library without source code and recompilation. Compared with the traditional de-expansion work based on binary analysis, the present invention can additionally tailor data segments, thereby achieving a better de-expansion effect.
[0009] The technical contents of the present invention include:
[0010] A shared library debloating method based on binary analysis and intermediate file reconnection, the method comprising:
[0011] Perform binary analysis on the target application and the shared libraries that the target application depends on to obtain a list of external call interfaces;
[0012] Based on the shared library, an intermediate file required for reconstructing the shared library is obtained; wherein the intermediate file is a shared library file of the target application program that can be relocated in the shared library;
[0013] Based on the external call interface list and the intermediate file, a de-expanded shared library is obtained.
[0014] Furthermore, the binary analysis of the target application and the shared library that the target application depends on is performed to obtain an external call interface list, including:
[0015] Based on the shared library, construct a shared library dependency graph of the target application;
[0016] According to the shared library dependency graph, the involved shared library files and the target application are disassembled to obtain disassembled codes, and the control flow corresponding to the target application is restored according to the disassembled codes to obtain the static function call relationship corresponding to the target program in the shared library;
[0017] For the shared library dependency graph, the execution of the function is tracked based on the user state probe to obtain the dynamic function call relationship corresponding to the target program in the shared library;
[0018] By combining the static function calling relationship and the dynamic function calling relationship, a list of external calling interfaces corresponding to the target program in the shared library is obtained.
[0019] Furthermore, for the shared library dependency graph, the execution of the function is tracked based on the user state probe to obtain the dynamic function call relationship corresponding to the target program in the shared library, including:
[0020] Read all function entries corresponding to the shared library file;
[0021] Deploy a user-mode probe at each function entry and execute the target application;
[0022] After the target application is executed, the dynamic function call relationship corresponding to the target program in the shared library is obtained based on the triggered probe.
[0023] Furthermore, the intermediate file includes: an ELF object file and / or a library object file.
[0024] Furthermore, in the case where the intermediate file is an ELF object file, the step of obtaining the intermediate file required for reconstructing the shared library based on the shared library includes:
[0025] Get the dynamic library corresponding to the shared library;
[0026] Compile the dynamic library based on the source code of the target application program, and obtain the ELFobject file during the compilation process;
[0027] or,
[0028] Get the static library corresponding to the shared library;
[0029] Unpack the static library to get the corresponding ELF object file.
[0030] Further, obtaining the de-expanded shared library based on the external call interface list and the intermediate file includes:
[0031] Select intermediate files based on the external call interface list to obtain an intermediate file set;
[0032] Recursively search the intermediate file set, and if any intermediate file has a dependency on other intermediate files in the shared library, add the intermediate file to the intermediate file set without undefined references in the shared library corresponding to the shared library;
[0033] Relink the intermediate files in the intermediate file set without undefined references in the shared library corresponding to the shared library to obtain a de-expanded shared library.
[0034] A shared library debloating device based on binary analysis and intermediate file reconnection, the device comprising:
[0035] A binary analysis module is used to perform binary analysis on the target application and the shared libraries that the target application depends on, and obtain a list of external call interfaces;
[0036] An intermediate file acquisition module, used for acquiring the intermediate file required for reconstructing the shared library based on the shared library; wherein the intermediate file is a shared library file of the target application program that can be relocated in the shared library;
[0037] The reconnection module is used to obtain a de-expanded shared library based on the external call interface list and the intermediate file.
[0038] An electronic device, comprising:
[0039] processor;
[0040] a memory for storing instructions executable by the processor;
[0041] The processor is used to read the executable instructions from the memory and execute the instructions to implement any of the above-mentioned shared library de-expansion methods based on binary analysis and intermediate file reconnection.
[0042] A computer-readable storage medium having computer program instructions stored thereon, characterized in that when the program instructions are executed by a processor, any of the above-mentioned shared library de-expansion methods based on binary analysis and intermediate file reconnection is implemented.
[0043] Compared with the prior art, the present invention is characterized in that the clipping is performed based on the granularity of the intermediate ELF relocatable file, and its positive effects are:
[0044] 1. Previously, the shared library de-expansion work based on the compilation process and loading process was mainly through source code analysis and recompilation, or relied on certain specific compilers such as llvm, which made the overall workflow cumbersome and could not de-expand some shared libraries that were not open source. However, the present invention does not require source code or recompilation, and can achieve the de-expansion effect by reconnecting the intermediate files based on the existing static library unpacking.
[0045] 2. Previous work based on binary analysis and binary code erasure was limited by binary rewriting technology and could not handle data dependencies, so it could only erase code segments in shared libraries. The present invention uses intermediate files for reconnection, which to a certain extent handles the situation where data dependencies cannot be calculated during binary analysis of shared libraries, solves the problem that data blocks cannot be erased without using source code, and greatly improves the clipping effect compared to other work. BRIEF DESCRIPTION OF THE DRAWINGS
[0046] Figure 1 The present invention is an architectural diagram of a shared library de-expansion method based on binary analysis and intermediate file reconnection.
[0047] Figure 2 The present invention is a flow chart of a shared library de-expansion method based on binary analysis and intermediate file reconnection. DETAILED DESCRIPTION
[0048] The present invention is further described below by way of examples, but the scope of the present invention is not limited in any way.
[0049] The present invention aims to propose a shared library clipping tool based on binary analysis and intermediate file reconnection, which clips out the bloated shared library for a specific software usage environment. The present invention is based on binary file analysis, including static and dynamic analysis. The following takes static + dynamic analysis as an example to reconstruct the calling process of the target program set and the dependent shared library, and obtain the external interface list used by the dynamic link library when running the program; then select the intermediate file based on the external interface list to reconnect to obtain the de-bloated shared library, so as to reduce the size of the shared library while retaining the normal function of the target program set. Compared with previous work, the present invention can extract the intermediate file from the static library and reconnect to obtain the de-bloated shared library without using source code or recompiling. At the same time, the present invention clips each segment of the shared library, such as the code segment, the data segment, etc. This makes the clipping effect of the present invention significantly improved compared with the previous binary analysis tool that only clips the code segment.
[0050] The overall structure of the present invention is as follows Figure 1 As shown, the input of the present invention is one or more target applications and the shared libraries they depend on, and the output is a de-expanded shared library. The present invention is mainly divided into three parts: binary analysis, intermediate file acquisition and reconnection. The binary analysis part is mainly based on the target application and the shared library it depends on, and the interface called in the shared library is marked by the method of static analysis + dynamic analysis. The intermediate file acquisition part is mainly used to obtain the intermediate relocatable ELF file corresponding to the shared library. The acquisition method includes but is not limited to recompiling (in the case of source code) and unpacking from the static library (in the case of no source code). The following description is made by unpacking from the static library as an example. The reconnection part performs dependency analysis on the intermediate file based on the interface obtained by the binary analysis part and the intermediate file obtained, and obtains a complete set of intermediate files without internal symbol references of the shared library. Then the intermediate files in this set are reconnected to obtain the final de-expanded shared library. The specific description is as follows:
[0051] 1. Binary Analysis
[0052] Shared library dependency analysis, this part mainly analyzes the dependency of the target application on the shared library, and the specific method is as follows: after loading the target program file, the present invention reads the shared library files it depends on according to the information in the ELF header of the application, and finds the location of the corresponding file. Then, the found shared library files are recursively searched for the shared library files they depend on, and finally a complete shared library dependency graph is obtained.
[0053] Static analysis, after obtaining the complete shared library dependency graph, disassembles the involved shared library files and target applications, and restores their control flow based on their disassembled codes to obtain the function call relationship within the shared library.
[0054] Dynamic analysis,Dynamic analysis is based on the shared library dependency graph, and uses user-mode probes to track the execution of functions.,Deploy user-mode probes on the function interfaces in the target application and shared library, execute the test program of the corresponding program, and collect the triggered probes.,Then merge with the function call relationship in the previous step to obtain a complete list of external call interfaces.
[0055] 2. Obtaining Intermediate Files
[0056] The intermediate file acquisition part mainly obtains the intermediate file set that can be connected to the required shared library. Here, the intermediate file refers to the ELF relocatable file, including but not limited to conventional ELF object file (other intermediate files are such as library object files compiled based on libtool), and the following is an example of ELF object file. The intermediate file acquisition that can be connected to the shared library can use two methods. The first method is to compile the dynamic library based on the source code, and obtain the set of intermediate files (i.e. ELF object files) during the compilation process. The second method is to unpack the static library corresponding to the dynamic library, and obtain the corresponding set of intermediate files (i.e. ELF object files). For open source shared libraries, any one of the two methods can be used. For non-open source shared libraries, the second method is used. The follow-up is described as an example of unpacking from the static library.
[0057] 3. Reconnection
[0058] Intermediate file dependency search, here we take ELF object file as an intermediate file as an example. After unpacking from the static library, perform binary analysis on the ELF object file, and select the required ELF object file based on the obtained interface list. Then, according to the dependency relationship between ELF object files, we get an ELF object set without undefined references in the shared library.
[0059] Reconnect, use the connector to reconnect the ELF object files in the above set to obtain the de-expanded shared library.
[0060] In the scenario of one embodiment, the binary program nginx deployed on a web server is used as the target program, and the shared libraries that nginx depends on in the server are trimmed. The shared libraries that nginx depends on are libc.so, libcrypto.so, libz.so, and libpcre.so, and the corresponding static libraries can be found, such as Figure 2 As shown, the specific steps of the present invention are as follows:
[0061] Step 1: Call the shared library dependency analysis module to analyze the direct and indirect calls of the shared libraries of the target program or program set to obtain a call graph.
[0062] In the above embodiment, the ELF header file of nginx is read to analyze the dependencies of its shared libraries. Recursive search is performed on the dependent shared libraries of nginx to find that the entire library depends on libc.so, libcrypto.so, libz.so and libpcre.so.
[0063] Step 2: Perform static analysis on the target program and shared library based on the call graph to obtain the static function call relationship.
[0064] In the above embodiment, disassembly and static analysis are performed on nginx and the above shared libraries, and the static function call graph of the shared libraries that nginx depends on is reconstructed based on the disassembly code; then recursive analysis is performed to obtain the calling relationship between the called functions of each shared library and other functions; finally, the static function calling relationship in the shared library required to run nginx is obtained.
[0065] Step 3: Deploy user-mode probes for the interfaces in the shared library, execute the test program to collect the triggered probes, collect the called interfaces, and obtain the dynamic function call relationship.
[0066] In the above embodiment, all function entries of libc.so, libcrypto.so, libz.so and libpcre.so are read, and user-mode probes are deployed. Subsequently, the nginx test program is executed, and all called functions will trigger the probes of their entries. After the test program is executed, all triggered probes are collected to obtain the dynamic function call relationship called by the tested program.
[0067] Step 4: Merge the static function call relationship and the dynamic function call relationship into an external call interface list.
[0068] Step 5: Select the static library corresponding to the shared library and unpack the ELF object file from it.
[0069] In the above embodiment, the ELF object file can be unpacked from the static library files corresponding to libc.so, libcrypto.so, libz.so and libpcre.so.
[0070] Step 6: Based on the called interface list, select the required ELF object files and process their dependencies to obtain a set of ELF object files without undefined references inside the shared library.
[0071] In the above embodiment, based on the list obtained in step 4, for each shared library, the ELFobject file to be called is selected and placed in the ELF object set that it needs to use. After the search is completed, the ELFobject files in the set are searched recursively. If they have dependencies on other ELF object files in the shared library, the ELF object files that they depend on are added to the set until there is no dependency on the ELF object files in the shared library in the set, and the final shared library corresponding to the ELF object file set without undefined references in the shared library is obtained.
[0072] Step 7: Relink the above-mentioned ELF object file set without undefined references in the shared library to obtain a de-expanded shared library.
[0073] In summary, a major feature of the present invention is to perform shared library tailoring based on intermediate files, so that: 1. no source code and recompilation are required; 2. data segments and other ELF segments other than code segments can be tailored. In previous work, the shared library tailoring technology that does not require source code compilation can only tailor code segments due to the limitations of binary data dependency analysis technology and ELF file addressing methods, making the tailoring effect unsatisfactory. Based on the existing tailoring work based on binary analysis, the present invention modifies its tailoring method from binary code erasure to tailoring based on the granularity of intermediate files, which allows the tailoring results of the present invention to tailor ELF segments such as data segments that could not be tailored in previous work.
[0074] Those skilled in the art will readily appreciate other embodiments of the present disclosure after considering the specification and practicing the present disclosure. The present application is intended to cover any variations, uses or adaptations of the present disclosure, which follow the general principles of the present disclosure and include common knowledge or customary techniques in the art that are not disclosed in the present disclosure. The specification and embodiments are intended to be exemplary only, and the present disclosure is not limited to the precise structures described above and shown in the accompanying drawings, and various modifications and changes may be made without departing from the scope thereof.
Claims
1. A shared library debloating method based on binary analysis and intermediate file relinking, characterized in that: The method comprises: Perform binary analysis on the target application and the shared libraries that the target application depends on to obtain a list of external call interfaces; Based on the shared library, an intermediate file required for reconstructing the shared library is obtained; wherein the intermediate file is a shared library file of the target application program that can be relocated in the shared library; Based on the external call interface list and the intermediate file, a de-expanded shared library is obtained.
2. The method according to claim 1, characterized in that The binary analysis of the target application and the shared library that the target application depends on is performed to obtain an external call interface list, including: Based on the shared library, construct a shared library dependency graph of the target application; According to the shared library dependency graph, the involved shared library files and the target application are disassembled to obtain disassembled codes, and the control flow corresponding to the target application is restored according to the disassembled codes to obtain the static function call relationship corresponding to the target program in the shared library; For the shared library dependency graph, the execution of the function is tracked based on the user state probe to obtain the dynamic function call relationship corresponding to the target program in the shared library; By combining the static function calling relationship and the dynamic function calling relationship, a list of external calling interfaces corresponding to the target program in the shared library is obtained.
3. The method according to claim 2, characterized in that For the shared library dependency graph, the execution of the function is tracked based on the user state probe to obtain the dynamic function call relationship corresponding to the target program in the shared library, including: Read all function entries corresponding to the shared library file; Deploy a user-mode probe at each function entry and execute the target application; After the target application is executed, the dynamic function call relationship corresponding to the target program in the shared library is obtained based on the triggered probe.
4. The method according to claim 1, characterized in that The intermediate files include: ELF object files and / or library object files.
5. The method according to claim 4, characterized in that In the case where the intermediate file is an ELF object file, the step of obtaining the intermediate file required for reconstructing the shared library based on the shared library includes: Get the dynamic library corresponding to the shared library; Compiling the dynamic library based on the source code of the target application and obtaining an ELF object file during the compilation process; or, Get the static library corresponding to the shared library; Unpack the static library to get the corresponding ELF object file.
6. The method according to claim 1, characterized in that The step of obtaining a de-expanded shared library based on the external call interface list and the intermediate file includes: Select intermediate files based on the external call interface list to obtain an intermediate file set; Recursively search the intermediate file set, and if any intermediate file has a dependency on other intermediate files in the shared library, add the intermediate file to the intermediate file set without undefined references in the shared library corresponding to the shared library; Relink the intermediate files in the intermediate file set without undefined references in the shared library corresponding to the shared library to obtain a de-expanded shared library.
7. A shared library debloating device based on binary analysis and intermediate file reconnection, characterized in that: The device comprises: A binary analysis module is used to perform binary analysis on the target application and the shared libraries that the target application depends on, and obtain a list of external call interfaces; An intermediate file acquisition module, used for acquiring the intermediate file required for reconstructing the shared library based on the shared library; wherein the intermediate file is a shared library file of the target application program that can be relocated in the shared library; The reconnection module is used to obtain a de-expanded shared library based on the external call interface list and the intermediate file.
8. An electronic device, characterized in that: include: processor; a memory for storing instructions executable by the processor; The processor is used to read the executable instructions from the memory and execute the instructions to implement the shared library de-expansion method based on binary analysis and intermediate file reconnection as described in any one of claims 1-6.
9. A computer-readable storage medium having computer program instructions stored thereon, characterized in that: When the program instructions are executed by a processor, the shared library de-expansion method based on binary analysis and intermediate file reconnection described in any one of claims 1 to 6 is implemented.
