A method and device for detecting unsafe direct memory access in a driver program
Through the analysis method based on the compiler framework, combined with static taint and pattern analysis technology, the unsafe DMA access in the driver is automatically identified and verified, which solves the problem of detecting unsafe DMA access in the existing technology, and achieves efficient and accurate Error detection and reporting improves driver security.
Patent Information
- Application Number
- CN202110246991.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-03-05
- Publication Date
- 2024-11-26
- Estimated Expiration
- 2041-03-05
AI Technical Summary
The prior art is difficult to effectively detect unsafe direct memory access in the driver, resulting in security risks and false alarms and misreporting problems. Manual detection is time-consuming and laborious, and automated static analysis methods cannot recognize DMA access.
Using a compiler framework-based approach, the driver source code is compiled, field-based analysis algorithm is used to identify direct memory access operations, combined with static taint analysis and pattern analysis, and the instructions that determine the impact through flow-sensitive and context-sensitive analysis techniques are used to determine the influence. The constraint solver verifies code paths and automatically identifies and filters unsafe DMA access.
It realizes accurate detection of unsafe DMA access in the driver, generates detailed error reports, improves detection efficiency, reduces artificial workload, and improves driver security.
Smart Images

Figure CN112925524B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of software testing, and in particular to a method and device for detecting unsafe direct memory access in a driver. Background Art
[0002] Direct Memory Access (DMA) is a technology that allows hardware devices to communicate directly with system memory and is widely used in existing drivers. DMA is divided into two categories according to the different data synchronization methods. The first is consistent DMA, that is, after the DMA memory is requested, each DMA access operation will automatically synchronize the data of the hardware device and the system memory, so the DMA access overhead is large; the second is streaming DMA, that is, after the DMA memory is requested, the data of the hardware device and the system memory need to be explicitly synchronized, and each DMA access will not be automatically synchronized, so the DMA access overhead is small. In general, the use of DMA can avoid the execution overhead of the processor during hardware communication, allowing the processor to efficiently perform other tasks, thereby improving the overall performance of the system.
[0003] However, the use of DMA also brings certain security risks. For streaming DMA, the streaming DMA data can only be accessed when the driver performs appropriate synchronization between the hardware device and the system memory data. Otherwise, it will cause inconsistency between the hardware device and the system memory data, which may easily cause abnormal hardware behavior. For consistent DMA, since the hardware device may fail or even be malicious, the content of the DMA data may be unsafe. Therefore, proper checks are required before using the DMA data, otherwise it will cause more serious security problems. For example, using unsafe DMA data as an array index may cause a buffer overflow. Therefore, it is necessary to effectively detect unsafe DMA access in the driver to improve the security of the driver.
[0004] In order to detect such errors, developers often need to manually read the driver code, and then identify DMA access and check its safety based on experience. However, since each DMA access operation is implemented as a normal memory access operation, rather than calling a special kernel function like accessing hardware registers, it is difficult to manually identify DMA access operations. Moreover, due to the large amount of driver code, complex code logic, and a large number of function call layers, it is difficult to manually check the safety of DMA access. In addition, since there are many instructions in the code path and the data dependencies between instructions are complex, it is difficult to manually determine the accessibility of the code path of potential errors, which will introduce more false positives. In view of the above three points, the manual detection of unsafe DMA access is often time-consuming and labor-intensive, and is prone to a large number of missed and false positives.
[0005] Existing automated static analysis methods often only focus on general code defects such as null pointer dereference and use-after-free, but lack the identification and inspection of DMA access, and therefore cannot detect unsafe DMA access in drivers.
[0006] Therefore, designing a good method to automatically detect unsafe DMA accesses in drivers has become an urgent problem to be solved in operating system development and testing. Summary of the invention
[0007] The present invention provides a method and a device for detecting unsafe direct memory access in a driver program, so as to solve the problem that the unsafe DMA access in the driver program cannot be well detected in the prior art.
[0008] The present invention provides a method for detecting unsafe direct memory access in a driver, comprising:
[0009] Compile the source code of the driver under test based on the compiler framework to obtain the driver intermediate code;
[0010] Using a field-based analysis algorithm on the intermediate code to identify direct memory access operations in the code;
[0011] Performing static taint analysis based on the direct memory access operation to determine instructions affected by the direct memory access operation, and performing pattern-based analysis on the instructions to obtain candidate unsafe direct memory accesses;
[0012] The code path reachability of each candidate unsafe direct memory access is verified to obtain an unsafe direct memory access.
[0013] According to a method for detecting unsafe direct memory access in a driver provided by the present invention, a field-based analysis algorithm is used on the intermediate code to identify the direct memory access operation in the code, specifically comprising:
[0014] Analyzing access variables of each memory access operation in the intermediate code;
[0015] Identify the memory access operation corresponding to the target access variable as a direct memory access operation;
[0016] The target access variable refers to an access variable whose structure data type and field information match the preset record information.
[0017] According to a method for detecting unsafe direct memory access in a driver provided by the present invention, a step of performing static taint analysis based on the direct memory access operation and determining instructions affected by the direct memory access operation specifically includes:
[0018] The direct memory access operation is analyzed by flow-sensitive analysis technology and context-sensitive analysis technology to determine instructions affected by the direct memory access operation.
[0019] According to a method for detecting unsafe direct memory access in a driver provided by the present invention, the steps of performing pattern-based analysis on the instruction to obtain candidate unsafe direct memory access specifically include:
[0020] Performing consistency mode-based analysis and streaming mode-based analysis on the instruction to obtain candidate unsafe direct memory access;
[0021] The analysis based on the consistency mode specifically includes analyzing whether the instruction is performed under direct memory data synchronization;
[0022] The analysis based on the streaming mode specifically involves pre-checking the instruction before the instruction is used as an array index, an offset for pointer access, or a loop condition variable.
[0023] According to a method for detecting unsafe direct memory access in a driver provided by the present invention, the code path reachability of each candidate unsafe direct memory access is verified to obtain the step of unsafe direct memory access, specifically comprising:
[0024] translating each instruction in the code path into a conditional constraint;
[0025] Based on the constraint solver, verify whether the conditional constraints can be satisfied at the same time;
[0026] A code path that cannot satisfy conditional constraints at the same time is used as a filtering code path, and candidate unsafe direct memory accesses corresponding to the filtering code path are filtered to obtain unsafe direct memory accesses.
[0027] The present invention also provides a device for detecting unsafe direct memory access in a driver, comprising:
[0028] A code compiler is used to compile the source code of the driver under test based on the compiler framework to obtain the driver intermediate code;
[0029] A direct memory access identifier, for using a field-based analysis algorithm on the intermediate code to identify direct memory access operations in the code;
[0030] A direct memory access checker, configured to perform static taint analysis based on the direct memory access operation, determine instructions affected by the direct memory access operation, perform pattern-based analysis on the instructions, and obtain candidate unsafe direct memory accesses;
[0031] A code path verifier is used to verify the code path reachability of each candidate unsafe direct memory access to obtain an unsafe direct memory access.
[0032] The direct memory access identifier is specifically used for:
[0033] Analyzing access variables of each memory access operation in the intermediate code;
[0034] Identify the memory access operation corresponding to the target access variable as a direct memory access operation;
[0035] The target access variable refers to an access variable whose structure data type and field information match the preset record information.
[0036] The direct memory access checker is specifically used for:
[0037] The direct memory access operation is analyzed by flow-sensitive analysis technology and context-sensitive analysis technology to determine instructions affected by the direct memory access operation.
[0038] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the steps of any of the above-described methods for detecting unsafe direct memory access in a driver are implemented.
[0039] The present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of any of the above-described methods for detecting unsafe direct memory access in a driver program.
[0040] The present invention provides a method and device for detecting unsafe direct memory access in a driver. By analyzing the intermediate code of the driver using a field-based analysis algorithm, the DMA access operation can be automatically and accurately identified. Moreover, by using static taint analysis and pattern-based analysis technology, instructions related to DMA access data can be discovered and checked more comprehensively and accurately, and unsafe DMA access can be effectively discovered. At the same time, the method uses a constraint solver to check the relevant code path, which can effectively improve the accuracy of error detection. Finally, the method can generate a relatively detailed error report to facilitate developers and testers to read and check. In addition, the working process in the embodiment of the present invention is automatic, and developers and testers do not need to perform manual work, thereby saving the workload of program analysis and improving the efficiency of error detection. BRIEF DESCRIPTION OF THE DRAWINGS
[0041] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0042] Figure 1 It is a flowchart of a method for detecting unsafe direct memory access in a driver program provided by an embodiment of the present invention;
[0043] Figure 2 A flowchart of compiling a driver source code provided by an embodiment of the present invention;
[0044] Figure 3 A pseudo code diagram of a field-based analysis algorithm provided by an embodiment of the present invention;
[0045] Figure 4 A pseudo code diagram of static taint analysis provided by an embodiment of the present invention;
[0046] Figure 5 An example diagram of a streaming DMA access check using a pattern-based analysis technique described in an embodiment of the present invention;
[0047] Figure 6 An example diagram of a consistent DMA access check using a pattern-based analysis technique described in an embodiment of the present invention;
[0048] Figure 7 A workflow diagram of a path checking technology based on constraint solving provided by an embodiment of the present invention;
[0049] Figure 8It is a schematic diagram of the overall structure of the method for automatically detecting unsafe DMA access in a driver program described in an embodiment of the present invention;
[0050] Fig. 9 It is a schematic diagram of the structure of the device for detecting unsafe direct memory access in a driver program described in an embodiment of the present invention;
[0051] Fig.10 A schematic diagram of the physical structure of the electronic device provided by the present invention. DETAILED DESCRIPTION
[0052] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0053] Figure 1 is a flow chart of a method for detecting unsafe direct memory access in a driver program provided by an embodiment of the present invention, such as Figure 1 As shown, including:
[0054] Step S1, compiling the source code of the driver under test based on the compiler framework to obtain the driver intermediate code;
[0055] Specifically, since the source code of the driver is usually written in C language, but since the syntax of C language is flexible and varied, it is difficult to directly perform program analysis on it.
[0056] The compiler framework described in the embodiment of the present invention may specifically refer to the LLVM compiler architecture, which can fully support C language compilation and can generate intermediate codes that are convenient for program analysis.
[0057] Figure 2 A flowchart of compiling the driver source code provided by the embodiment of the present invention is as follows: Figure 2 As shown, it is mainly divided into two steps:
[0058] First, use the front-end compiler Clang provided by LLVM to compile each source code file of the driver to generate the corresponding intermediate code file.
[0059] Then, the function information in each intermediate code file is analyzed and recorded to facilitate the subsequent analysis of cross-file function calls.
[0060] Step S2, using a field-based analysis algorithm on the intermediate code to identify direct memory access operations in the code;
[0061] In the driver code, each DMA access operation is implemented as a normal memory access operation, rather than calling a special kernel function like accessing hardware registers, so it is difficult to automatically identify DMA access operations.
[0062] The field-based analysis algorithm described in the embodiment of the present invention is specifically divided into two stages.
[0063] The first stage is to record the information of DMA memory application. First, find the calling location of DMA memory application function and record the basic information of the variable assigned by the function return value. In the driver code, most of the created DMA memory buffers are represented by structure member variables. This step mainly records the structure data type and field information of the variable to identify the DMA memory buffer. Then, according to the structure data type and field information, a field-based analysis method is used to identify the DMA access operation in the code.
[0064] The second stage is the identification of DMA access operations. Specifically, for a memory access operation, if the accessed variable is a structure member variable, and its structure data type and field information match the recorded information, then the memory access is identified as a DMA access operation.
[0065] Step S3, performing static taint analysis based on the direct memory access operation, determining instructions affected by the direct memory access operation, performing pattern-based analysis on the instructions, and obtaining candidate unsafe direct memory accesses;
[0066] The static taint analysis described in the following embodiments of the present invention specifically takes each DMA access operation as the analysis starting point for analysis. The static taint analysis algorithm is flow-sensitive and context-sensitive, so instructions affected by direct memory access operations can be identified.
[0067] The pattern-based analysis described in the embodiment of the present invention is based on common unsafe DMA access patterns, specifically refers to analyzing the streaming DMA access pattern and the consistent DMA access pattern respectively to report possible unsafe DMA accesses, and taking the possible unsafe DMA accesses as candidate unsafe direct memory accesses.
[0068] The candidate unsafe direct memory access described in the embodiment of the present invention includes the location and code path of the DMA access, as well as detailed information such as the corresponding source code file name and line number.
[0069] Each possible unsafe DMA access reported by this analysis technique includes details such as the location and code path of the DMA access, as well as the corresponding source code file name and line number.
[0070] Step S4, verifying the code path reachability of each candidate unsafe direct memory access to obtain an unsafe direct memory access.
[0071] The code path described in the embodiment of the present invention refers to the content included in each possible unsafe DMA access.
[0072] The specific process of verification in the embodiment of the present invention refers to using the path checking technology based on constraint solving to translate each instruction in its code path into conditional constraints, and then using the constraint solver to verify whether these constraints can be met at the same time. If they cannot be met, the unsafe DMA access will be filtered out as a false alarm to obtain a filtered unsafe direct memory access.
[0073] The embodiment of the present invention can automatically and accurately identify DMA access operations by analyzing the intermediate code of the driver program using a field-based analysis algorithm; moreover, by using static taint analysis and pattern-based analysis technology, it can more comprehensively and accurately discover and check instructions related to DMA access data, and effectively discover unsafe DMA access; at the same time, the method uses a constraint solver to check related code paths, which can effectively improve the accuracy of error detection. Finally, the method can generate a relatively detailed error report to facilitate developers and testers to read and check. In addition, the working process in the embodiment of the present invention is automatic, and developers and testers do not need to perform manual work, thereby saving the workload of program analysis and improving the efficiency of error detection.
[0074] Based on any of the above embodiments, the step of using a field-based analysis algorithm on the intermediate code to identify the direct memory access operation in the code specifically includes:
[0075] Analyzing access variables of each memory access operation in the intermediate code;
[0076] Identify the memory access operation corresponding to the target access variable as a direct memory access operation;
[0077] The target access variable refers to an access variable whose structure data type and field information match the preset record information.
[0078] Specifically, in the embodiment of the present invention, a field-based analysis algorithm is used for the code. First, the calling position of the DMA memory application function is found, and the basic information of the variable assigned by the function return value is recorded. In the driver code, most of the created DMA memory buffers are represented by structure member variables. This step mainly records the structure data type and field information of the variable to identify the DMA memory buffer. Then, according to the structure data type and field information, a field-based analysis method is used to identify the DMA access operation in the code.
[0079] Specifically, for a memory access operation, if the accessed variable is a structure member variable, and the structure data type and field information match the recorded information, the memory access is identified as a DMA access operation.
[0080] The preset record information described in the embodiment of the present invention refers to the information record of the DMA memory application.
[0081] Figure 3 A pseudo code diagram of a field-based analysis algorithm provided by an embodiment of the present invention, such as Figure 3 As shown, the field-based analysis algorithm is divided into two stages:
[0082] (1) DMA memory application information recording process Figure 3 As shown in (a) in the figure. The set dma_var_set is used to record the information of the created DMA memory variables, and the set dma_struct_set is used to record the structure member information of the created DMA memory variables, including the structure type and field information. In this stage, dma_var_set and dma_struct_set are initialized to empty sets, and then each function call in the driver code is analyzed. If call is used to apply for a DMA memory buffer, the function call is processed.
[0083] First, get the variable ret_var assigned with the return value of call, and use alias analysis to get the variable set var_set that is aliased with ret_var. Then, analyze each variable var in var_set, add it to the set dma_var_set, and get the structure type and field information struct_info of var. If var is a structure, struct_info is not empty, and struct_info is added to the set dma_struct_set.
[0084] (2) Identification of DMA access operations, the process is as follows Figure 3As shown in (b) in the figure. The set dma_access_set is used to record the identified DMA access instructions. This stage initializes dma_access_set to an empty set, and then analyzes each instruction inst in the driver code. If inst is a memory read or write instruction, that is, a load or store instruction in LLVM, then continue to process the instruction. First, get the variable var accessed by the inst instruction, and get the structure type and field information struct_info of var. If var is not a structure member variable, struct_info is empty. Then, if var exists in the set dma_var_set, or struct_info exists in the set dma_struct_set, it indicates that var is DMA access data. In this case, the variable set var_set that is aliased with var, and the instruction set inst_set that accesses the variables in var_set are obtained, and inst_set is added to the set dma_access_set.
[0085] The embodiment of the present invention can automatically and accurately identify DMA access operations through a field-based analysis algorithm, which is beneficial for subsequent further detection of whether there is unsafe direct memory access in the driver.
[0086] Based on any of the above embodiments, the step of performing static taint analysis based on the direct memory access operation to determine instructions affected by the direct memory access operation specifically includes:
[0087] The direct memory access operation is analyzed by flow-sensitive analysis technology and context-sensitive analysis technology to determine instructions affected by the direct memory access operation.
[0088] The step of performing pattern-based analysis on the instruction to obtain candidate unsafe direct memory accesses specifically includes:
[0089] Performing consistency mode-based analysis and streaming mode-based analysis on the instruction to obtain candidate unsafe direct memory access;
[0090] The analysis based on the consistency mode specifically includes analyzing whether the instruction is performed under direct memory data synchronization;
[0091] The analysis based on the streaming mode specifically involves pre-checking the instruction before the instruction is used as an array index, an offset for pointer access, or a loop condition variable.
[0092] Specifically, the embodiment of the present invention uses each DMA access operation as the analysis starting point, and uses static taint analysis and pattern-based analysis techniques to find and check instructions affected by DMA access data. Specifically, static taint analysis is stream-sensitive and context-sensitive to improve the accuracy of analysis. Pattern-based analysis techniques check instructions affected by DMA access data based on common unsafe DMA access patterns. Specifically, for streaming DMA access, the analysis technique checks whether the access is performed under appropriate DMA data synchronization; if not, the DMA access may cause data inconsistency problems between the hardware device and the system memory, so the analysis technique will report possible unsafe DMA access. For consistent DMA access, the analysis technique checks whether the accessed DMA data is properly checked before being used as an array index, an offset for pointer access, and a loop condition variable, etc.; if not, the DMA access may cause security issues such as buffer overflow, illegal pointer access, and infinite loops, so the analysis technique will report possible unsafe DMA access. Each possible unsafe DMA access reported by the analysis technique includes the location and code path of the DMA access, as well as detailed information such as the corresponding source code file name and line number.
[0093] Figure 4 A pseudo code diagram of static stain analysis provided by an embodiment of the present invention, such as Figure 4 As shown, the analysis is responsible for calculating the variable set var_set and instruction set inst_set affected by DMA access dma_access in the code path code_path. In order to improve the accuracy of taint analysis, the analysis is flow-sensitive and context-sensitive. The analysis initializes var_set and inst_set to empty sets, obtains the variable taint_var accessed by dma_access, and then adds taint_var and dma_access to the sets var_set and inst_set respectively, and then analyzes each instruction inst in the code path code_path according to the instruction execution order. First, the return value variable res_var of the instruction inst is obtained. If inst has no return value, inst is empty. Then, the operation variable set op_var_set of the instruction inst is obtained. If the intersection of op_var_set and var_set is not empty, it proves that inst is affected by DMA access data, and inst is added to the set inst_set. In this case, if res_var is not empty, the variable is also affected by DMA access data, and res_var is added to the set var_set.
[0094] According to the identified DMA access and the instructions affected by the DMA access, the present invention uses a pattern-based analysis technique to detect possible unsafe DMA access. The analysis technique is divided into two parts, respectively checking the streaming DMA access and the consistent DMA access.
[0095] Figure 5 FIG. 4 is an example diagram of a pattern-based analysis technique for checking streaming DMA access described in an embodiment of the present invention, such as Figure 5 As shown in the figure, for streaming DMA access, it must occur between DMA mapping and demapping, and between DMA data synchronization with hardware devices and processors; otherwise, inconsistent data between hardware devices and processors will occur. Based on this rule, the analysis mainly checks four possible problem modes, such as Figure 5 As shown. Mode 1 and Mode 2 use forward flow-sensitive analysis to check whether the DMA data content is directly accessed after the DMA mapping function call and the start of DMA data synchronization; if so, a possible unsafe DMA access is reported. Mode 3 and Mode 4 use reverse flow-sensitive analysis to check whether the DMA data content is directly accessed before the DMA demapping function call and the end of DMA data synchronization; if so, a possible unsafe DMA access is reported.
[0096] For consistent DMA access, the data it accesses must be properly checked before being used by the driver. Specifically, if the accessed DMA data is not properly checked before being used as an array index, pointer access offset, loop condition variable, etc., the DMA access may cause security issues such as buffer overflow, illegal pointer access, and infinite loop.
[0097] Figure 6 An example diagram of a pattern-based analysis technique for consistent DMA access checking described in an embodiment of the present invention is shown in FIG. Figure 6 As shown in (a) in the figure, the local variable read is obtained by DMA access data, and the variable is used for the loop condition; therefore, the analysis identifies that the DMA access data may be modified, so that the value of the variable read is always non-zero, thus causing an infinite loop; Figure 6 As shown in (b) in the figure, the local variable frame_type is affected by the DMA access data and serves as an index of the array frame_types; therefore, the analysis identifies that the DMA access data may be modified so that the value of frame_type exceeds the length of frame_types, resulting in a buffer overflow. Figure 6As shown in (c), the local variable pkt_len is affected by the DMA access data, and the pointer data is accessed as an offset of the pointer xdp.data; therefore, the analysis identifies that the DMA access data may be modified, making the value of the variable pkt_len too large, resulting in illegal pointer access.
[0098] The embodiment of the present invention can comprehensively and accurately discover and check instructions related to DMA access data by utilizing static taint analysis and pattern-based analysis technology, and effectively discover unsafe DMA access.
[0099] Based on any of the above embodiments, the code path reachability of each candidate unsafe direct memory access is verified to obtain the unsafe direct memory access step, specifically comprising:
[0100] translating each instruction in the code path into a conditional constraint;
[0101] Based on the constraint solver, verify whether the conditional constraints can be satisfied at the same time;
[0102] A code path that cannot satisfy conditional constraints at the same time is used as a filtering code path, and candidate unsafe direct memory accesses corresponding to the filtering code path are filtered to obtain unsafe direct memory accesses.
[0103] Specifically, the embodiment of the present invention uses a path checking technology based on constraint solving to verify each possible code path of unsafe DMA access and filter out false positives.
[0104] Figure 7 The workflow diagram of the path checking technology based on constraint solving provided by the embodiment of the present invention is as follows: Figure 7 As shown, the workflow is mainly divided into three stages. For each possible unsafe DMA access, the technology first translates each instruction in its code path into a constraint expression in the format specified by the constraint solver. Then, according to the mode in which the security issue occurs, additional constraint expressions are added. For example, for buffer overflow, a constraint expression of frame_type>4 is added to indicate the condition under which the array out-of-bounds occurs. Preferably, the above constraint expressions are passed into the constraint solver for solving; if there is no solution that satisfies all these constraints at the same time, it proves that the code path is unreachable, and the safe DMA access is considered a false positive and is automatically filtered out.
[0105] Through the method for automatically detecting unsafe DMA access in a driver program in the embodiment of the present invention, developers and testers can easily and accurately find unsafe DMA access in the driver program, and then repair it in time, thereby improving the security of the driver program. Moreover, the working process of the method is automatic, and developers and testers do not need to manually analyze and modify the source code of the driver program under test, which greatly reduces the manual workload.
[0106] Figure 8 FIG. 1 is a schematic diagram of the overall structure of the method for automatically detecting unsafe DMA access in a driver program described in an embodiment of the present invention. Figure 8 As shown in the figure, the code compiler is responsible for compiling the C language source code of the driver under test to generate intermediate code, and analyzing and recording the function information in each intermediate code file to facilitate subsequent static analysis; the DMA access identifier is responsible for analyzing the intermediate code and finding the DMA access operations in the driver code; the DMA access checker is responsible for analyzing and checking the intermediate code and DMA access operations to find possible unsafe DMA access; the code path verifier is responsible for verifying the code path of possible unsafe DMA access, filtering out false positives, and generating the final error report. The error report contains the location and code path of the unsafe DMA access, as well as detailed information such as the corresponding source code file name and line number.
[0107] Fig. 9 FIG. 1 is a schematic diagram of the structure of the device for detecting unsafe direct memory access in a driver program described in an embodiment of the present invention. Fig. 9 As shown, it includes: a code compiler 910, a direct memory access identifier 920, a direct memory access checker 930 and a code path verifier 940; wherein the code compiler 910 is used to compile the source code of the driver under test based on the compiler framework to obtain the driver intermediate code; wherein the direct memory access identifier 920 is used to use a field-based analysis algorithm on the intermediate code to identify the direct memory access operation in the code; wherein the direct memory access checker 930 is used to perform static taint analysis based on the direct memory access operation, determine the instructions affected by the direct memory access operation, perform pattern-based analysis on the instructions, and obtain candidate unsafe direct memory access; wherein the code path verifier 940 is used to verify the code path reachability of each candidate unsafe direct memory access to obtain an unsafe direct memory access.
[0108] The direct memory access identifier is specifically used for:
[0109] Analyzing access variables of each memory access operation in the intermediate code;
[0110] Identify the memory access operation corresponding to the target access variable as a direct memory access operation;
[0111] The target access variable refers to an access variable whose structure data type and field information match the preset record information.
[0112] The direct memory access checker is specifically used for:
[0113] The direct memory access operation is analyzed by flow-sensitive analysis technology and context-sensitive analysis technology to determine instructions affected by the direct memory access operation.
[0114] The embodiment of the present invention can automatically and accurately identify DMA access operations by analyzing the intermediate code of the driver program using a field-based analysis algorithm; moreover, by using static taint analysis and pattern-based analysis technology, it can more comprehensively and accurately discover and check instructions related to DMA access data, and effectively discover unsafe DMA access; at the same time, the method uses a constraint solver to check related code paths, which can effectively improve the accuracy of error detection. Finally, the method can generate a relatively detailed error report to facilitate developers and testers to read and check. In addition, the working process in the embodiment of the present invention is automatic, and developers and testers do not need to perform manual work, thereby saving the workload of program analysis and improving the efficiency of error detection.
[0115] Fig.10 A schematic diagram of the physical structure of the electronic device provided by the present invention, such as Fig.10 As shown, the electronic device may include: a processor 1010, a communication interface 1020, a memory 1030 and a communication bus 1040, wherein the processor 1010, the communication interface 1020 and the memory 1030 communicate with each other through the communication bus 1040. The processor 1010 may call the logic instructions in the memory 1030 to execute a method for detecting unsafe direct memory access in a driver, the method comprising: compiling the source code of the driver under test based on a compiler framework to obtain the driver intermediate code; using a field-based analysis algorithm on the intermediate code to identify the direct memory access operation in the code; performing static taint analysis based on the direct memory access operation to determine the instructions affected by the direct memory access operation, performing pattern-based analysis on the instructions to obtain candidate unsafe direct memory access; verifying the code path reachability of each candidate unsafe direct memory access to obtain an unsafe direct memory access.
[0116] In addition, the logic instructions in the above-mentioned memory 1030 can be implemented in the form of software functional units and can be stored in a computer-readable storage medium when sold or used as an independent product. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a disk or an optical disk.
[0117] On the other hand, the present invention also provides a computer program product, which includes a computer program stored on a non-transitory computer-readable storage medium, and the computer program includes program instructions. When the program instructions are executed by a computer, the computer can execute the method for detecting unsafe direct memory access in a driver program provided by the above methods, and the method includes: compiling the source code of the driver program under test based on a compiler framework to obtain a driver intermediate code; using a field-based analysis algorithm on the intermediate code to identify direct memory access operations in the code; performing static taint analysis based on the direct memory access operation to determine the instructions affected by the direct memory access operation, and performing pattern-based analysis on the instructions to obtain candidate unsafe direct memory accesses; verifying the code path reachability of each candidate unsafe direct memory access to obtain an unsafe direct memory access.
[0118] On the other hand, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, is implemented to execute the method for detecting unsafe direct memory access in a driver provided in the above-mentioned embodiments, the method comprising: compiling the source code of the driver under test based on a compiler framework to obtain a driver intermediate code; using a field-based analysis algorithm on the intermediate code to identify direct memory access operations in the code; performing static taint analysis based on the direct memory access operations to determine instructions affected by the direct memory access operations, performing pattern-based analysis on the instructions to obtain candidate unsafe direct memory accesses; and verifying the code path reachability of each candidate unsafe direct memory access to obtain an unsafe direct memory access.
[0119] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.
[0120] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.
[0121] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for detecting unsafe direct memory access in a driver, characterized in that: include: Compile the source code of the driver under test based on the compiler framework to obtain the driver intermediate code; Using a field-based analysis algorithm on the intermediate code to identify direct memory access operations in the code; Performing static taint analysis based on the direct memory access operation to determine instructions affected by the direct memory access operation, and performing pattern-based analysis on the instructions to obtain candidate unsafe direct memory accesses; Verifying the code path reachability of each candidate unsafe direct memory access to obtain an unsafe direct memory access; The step of using a field-based analysis algorithm on the intermediate code to identify the direct memory access operation in the code specifically includes: Analyzing access variables of each memory access operation in the intermediate code; Identify the memory access operation corresponding to the target access variable as a direct memory access operation; The target access variable refers to an access variable whose structure data type and field information match the preset record information; The step of performing pattern-based analysis on the instruction to obtain candidate unsafe direct memory access specifically includes: Performing consistency mode-based analysis and streaming mode-based analysis on the instruction to obtain candidate unsafe direct memory access; The analysis based on the consistency mode specifically includes analyzing whether the instruction is performed under direct memory data synchronization; The analysis based on the streaming mode specifically includes pre-checking the instruction before the instruction is used as an array index, an offset for pointer access, or a loop condition variable; The step of verifying the code path reachability of each candidate unsafe direct memory access to obtain the unsafe direct memory access specifically includes: translating each instruction in the code path into a conditional constraint; Based on the constraint solver, verify whether the conditional constraints can be satisfied at the same time; A code path that cannot satisfy conditional constraints at the same time is used as a filtering code path, and candidate unsafe direct memory accesses corresponding to the filtering code path are filtered to obtain unsafe direct memory accesses.
2. The method for detecting unsafe direct memory access in a driver according to claim 1, characterized in that: The step of performing static taint analysis based on the direct memory access operation to determine instructions affected by the direct memory access operation specifically includes: The direct memory access operation is analyzed by flow-sensitive analysis technology and context-sensitive analysis technology to determine instructions affected by the direct memory access operation.
3. A device for detecting unsafe direct memory access in a driver, characterized in that: include: A code compiler is used to compile the source code of the driver under test based on the compiler framework to obtain the driver intermediate code; A direct memory access identifier, for using a field-based analysis algorithm on the intermediate code to identify direct memory access operations in the code; A direct memory access checker, configured to perform static taint analysis based on the direct memory access operation, determine instructions affected by the direct memory access operation, perform pattern-based analysis on the instructions, and obtain candidate unsafe direct memory accesses; A code path verifier, used to verify the code path reachability of each candidate unsafe direct memory access to obtain an unsafe direct memory access; Wherein, the direct memory access identifier is specifically used for: Analyzing access variables of each memory access operation in the intermediate code; Identify the memory access operation corresponding to the target access variable as a direct memory access operation; The target access variable refers to an access variable whose structure data type and field information match the preset record information; Wherein, the device is also used for: Performing consistency mode-based analysis and streaming mode-based analysis on the instruction to obtain candidate unsafe direct memory access; The analysis based on the consistency mode specifically includes analyzing whether the instruction is performed under direct memory data synchronization; The analysis based on the streaming mode specifically includes pre-checking the instruction before the instruction is used as an array index, an offset for pointer access, or a loop condition variable; Wherein, the device is also used for: translating each instruction in the code path into a conditional constraint; Based on the constraint solver, verify whether the conditional constraints can be satisfied at the same time; A code path that cannot satisfy conditional constraints at the same time is used as a filtering code path, and candidate unsafe direct memory accesses corresponding to the filtering code path are filtered to obtain unsafe direct memory accesses.
4. The device for detecting unsafe direct memory access in a driver according to claim 3, characterized in that: The direct memory access checker is specifically used for: The direct memory access operation is analyzed by flow-sensitive analysis technology and context-sensitive analysis technology to determine instructions affected by the direct memory access operation.
5. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the program, the steps of the method for detecting unsafe direct memory access in a driver program as described in any one of claims 1 to 2 are implemented.
6. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method for detecting unsafe direct memory access in a driver program as claimed in any one of claims 1 to 2 are implemented.
