System call whitelist generation method for interpreted language source programs

By combining static and dynamic analysis, a system call whitelist is generated, which solves the problem of malicious applications in the container attacking the host kernel, and achieves comprehensive analysis and security improvement of system calls for container applications.

CN114546539BActive Publication Date: 2024-10-01PEKING UNIV
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202011347443.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-11-26
Publication Date
2024-10-01
Estimated Expiration
2040-11-26

AI Technical Summary

Technical Problem

In a container environment, malicious applications can attack the host kernel through system calls. Existing dynamic analysis methods cannot cover all branches and consume large resources, making it difficult to effectively restrict the container's access to the kernel.

Method used

Use static analysis combined with dynamic analysis to generate a system call whitelist. By establishing an abstract syntax tree for the interpreted language source code, mapping language-level functions to binary files, and then analyzing system calls at the operating system level, and combining the dynamic analysis results to generate The final whitelist.

Benefits of technology

It implements a comprehensive analysis of the system calls of container applications, generates a specific whitelist, and limits the system calls that can be called by the container, significantly improving security, reducing resource consumption, and covering all branches.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114546539B_ABST
    Figure CN114546539B_ABST
Patent Text Reader

Abstract

The present invention discloses a system call whitelist generation method for interpreted language source programs. The whitelist generation method of the present invention comprises the following steps: 1) for the source code of the application to be analyzed, selecting the corresponding target interpreter according to the writing language of the source code; 2) using the selected target interpreter to obtain the system call when the source code is running; 3) using the writing language of the source code to write a test program and run it, obtaining the system call required when running the test program as the system call required when the target interpreter is initialized, and then combining it with the system call obtained in step 2) to obtain the system call whitelist of the application to be analyzed. When running on a container, the application is only allowed to call the system calls on the whitelist, which greatly enhances security.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the field of system security and container security, and in particular relates to a system call whitelist generation method for interpreted language source programs. Background Art

[0002] With the widespread use of cloud computing technology, more and more applications are deployed in container environments represented by Docker. Unlike each virtual machine that can obtain a virtual operating system, containers do not need to package resources such as the kernel and virtual hardware, but directly use the kernel of the host machine. Each containerized application will share the kernel of the host machine, and the user space of each container is isolated from each other. Therefore, containers have the advantages of consuming less resources and faster startup. At the same time, since different containers share the kernel of the host machine, how to ensure the security of containers and kernels will become a concern. If a malicious application is running in a container, it can attack the kernel by executing certain system calls, such as causing memory leaks, launching denial of service attacks (refer to CVE-2018-10924.https: / / www.cvedetails.com / cve / CVE-2018-10924 / ) and obtaining root privileges of the system (refer to CVE-2019-5736.https: / / cve.mitre.org / cgi-bin / cvename.cgi?name=CVE-2019-5736). Therefore, we need to limit the types of system calls that applications in the container can call to prevent them from attacking the host kernel. This article proposes a system call analysis solution based on static analysis, and designs a set of system call analysis tools for three interpreted languages, Python, Java, and PHP. With the help of this tool, users only need to provide the source code of the application to generate a whitelist of system calls that the application may need to call during execution. Then, users can use the Seccomp mechanism to limit the types of system calls that the application can call.

[0003] System call analysis

[0004] The commonly used method for analyzing system calls is dynamic analysis, which counts the system calls called by the program when the program is running. The Linux Strace command is a user program debugging tool that can be used to monitor all system calls used when the program is running. Sysdig is a visual container analysis tool that can track the system calls used by a specific container when executing a program. In terms of analyzing application system calls, Suyeol Lee et al. and Lingguang Lei et al. used the strace command to trace all system calls executed by the container at runtime (see S. Lee, J. Seo, J. Nam, and S. Shin. Poster: Tclp: Enforcing least privileges to prevent containers from kernel vulnerabilities. In Proceedings of the 2019 ACM SIGSAC Conference on Computer and Communications Security, pages 2665–2667, 2019.; L. Lei, J. Sun, K. Sun, C. Shenefiel, R. Ma, Y. Wang, and Q. Li. Speaker: Split-phase execution of application containers. In International Conference on Detection of Intrusions and Malware, and Vulnerability Assessment, pages 230–251. Springer, 2017.). However, the limitation of dynamic analysis is that it cannot cover all branches in the program. If some functions are not called during program execution, the system calls required when calling these functions will not be analyzed. Therefore, in order to use dynamic analysis methods, a complete test case must be designed, and multiple runs are required to obtain results. The biggest advantage of static analysis is that it does not require running the source program and can cover all branches in the program. Therefore, this paper uses a combination of static analysis and dynamic analysis to analyze the system calls of applications. Summary of the invention

[0005] In view of the technical problems existing in the prior art, the purpose of the present invention is to provide a system call whitelist generation method and control method for interpreted language source programs. Applications running in containers may attack the host machine by executing certain system calls, thus causing some security risks. The present invention generates a whitelist by analyzing all system calls that may be called by the application during operation. When running on the container, the application is only allowed to call the system calls on the whitelist.

[0006] The technical solution of the present invention is:

[0007] A system call whitelist generation method for interpreted language source programs, the steps of which include:

[0008] 1) The user provides the source code directory or file of the application to be analyzed and uploads it to the system call analysis system;

[0009] 2) The system call analysis system starts the source code of the program to be analyzed according to the received information, and obtains the system calls that may be called in the source code;

[0010] Furthermore, the analysis of possible system calls in the source code is mainly achieved by establishing an abstract syntax tree (AST) for the input source code. This paper selects three interpreted languages, Python, Java and PHP. Since the implementation mechanism and operation mechanism of each language are different, the analysis method of the abstract syntax tree is also different. According to the different characteristics of the three languages, the present invention proposes a system call static analysis scheme for each language. The overall process is roughly as follows: the underlying function at the language level is obtained by a certain method, and it is mapped into a binary file, and then the system call at the operating system level is obtained by binary analysis. The following are explained separately.

[0011] Furthermore, the method for analyzing system calls in Python programs is:

[0012] 211) Use the open source module astroid to generate an abstract syntax tree for the Python source code, and traverse the function call nodes therein. The function represented in the function call node is a function called in the code, and all C language functions are found according to the module name and class name of the called function. Because the called function may also call other functions, in order to fully count all called functions, before accessing the next function call node, it is necessary to first access the syntax tree node representing the function body of the currently called function, and traverse the syntax tree nodes in the function body. If a function call node is encountered, continue to use the above method to traverse the function body nodes of the called function. After such recursive analysis, all C language functions are finally output;

[0013] 212) Use the open source module pycparser to generate an abstract syntax tree for the source code of the module where all the C language functions obtained in step 211) are located. According to the characteristics of Python's C language API, find the node in the syntax tree that represents the structure array that stores all the functions and all the class methods in the module. Each element in this array represents a function of the module or a method in a class of the module. Each element stores the name of this function or method used in the Python code and the name of the C language function that implements the function or method. Based on this information, a mapping can be established between the function name of each C language function in Python and the function name in the C source code. After obtaining the mapping, the binary file path of the module after compilation can be determined based on whether the C language file where the module is located is compiled into the same binary file as the interpreter (if the module and the interpreter are compiled into the same binary file, the path is the path of the interpreter binary file; otherwise, the path is the dynamic link library path corresponding to the module source file after compilation. The path of the interpreter binary file can be uniquely determined after compilation, and the dynamic link library path after the module is compiled can be obtained through the module's __file__ attribute. If the module does not have a __file__ attribute, it indicates that it is compiled into the same binary file as the interpreter.) Finally, the function names of all C language functions in step 211) in their source code and the dynamic link library paths where their source codes are located are output.

[0014] 213) Perform static analysis on the binary file output in step 212) to obtain all system calls that may be called when the application is running.

[0015] Furthermore, the method for analyzing system calls in Java programs is:

[0016] 221) Use the open source tool Javaparser to generate an abstract syntax tree for the Java source code and traverse the method call nodes therein. For each method call node, find the specific implementation of the method according to the package name and method name of the method, and build a syntax tree for recursive analysis of the method body of each method. Native methods are a type of Java method implemented by other languages ​​(C language) and do not have a Java method body. During the recursive analysis process, the traversed native method nodes are added to the set, and finally the set of native methods called by the Java source program is output;

[0017] 222) Traverse the dynamic library files under the Java Development Kit, export the symbol table for each dynamic library, intercept the jni function name in the symbol table, parse it according to certain rules to obtain the corresponding native method name, and finally establish the jni function-native method mapping;

[0018] 223) Perform binary analysis on all jni functions that come with jdk, obtain the corresponding system call set, and establish a jni function-system call mapping;

[0019] 224) combines the native method set obtained in 221) with the native method-jni function mapping obtained in 222) to obtain the executed jni function, and then combines it with the jni function-system call mapping obtained in 223) to finally obtain all system calls called by the native method.

[0020] Further, the method of analyzing the system call in the PHP program is:

[0021] 231) Use the open source PHP static analysis tool Phan to scan the entire PHP application source code and the third-party libraries it depends on to generate an abstract syntax tree, and use the open source static analysis tool Phan to perform recursive association search on the abstract syntax tree to generate a function call chain. All PHP export functions called during the operation of the application source code are the sum of the functions at the end of all function call chains. These methods are similar to native methods in Java, and the underlying implementation of these methods is C language.

[0022] 232) According to the PHP function name mapping relationship, convert the exported function name obtained in step 231) into the function name of the corresponding function in the PHP interpreter. These exported functions in PHP are different from the function names implemented in the interpreter, so we need to map the function names. Although the function names are different, there are certain naming conventions.

[0023] 233) Perform binary code static analysis on the function name and PHP interpreter obtained in step 232) to obtain all system calls that may be called when the application is running.

[0024] 3) Dynamic analysis: Write a simple test program in each of the three languages. The statements in the program do not need to execute system calls other than initializing the interpreter, such as a simple addition calculation. Then run the program and use the strace command to get the system calls required for runtime. These can be considered as all the system calls required for initializing the Python, Java, and PHP interpreters. Then combine the results of the dynamic analysis with the results of the static analysis of the corresponding language to obtain the final output system call whitelist.

[0025] A system call control method, the steps of which include:

[0026] 1) For the source code of the application to be analyzed, select the corresponding target interpreter according to the writing language of the source code;

[0027] 2) using the selected target interpreter to obtain system calls that may be called in the source code;

[0028] 3) writing a test program in the language of the source code and running it, obtaining the system calls required when running the test program as the system calls required for initializing the target interpreter, and then combining them with the system calls obtained in step 2) to obtain a system call whitelist of the application to be analyzed;

[0029] 4) Obtain the system call A when the application to be analyzed is running. If the system call A is in the system call whitelist, run the system call A for execution; otherwise, refuse to execute the system call A.

[0030] Beneficial effects of the present invention:

[0031] Based on the above three language analysis schemes for system calls, the present invention implements system call analysis tools for Python, Java and PHP respectively.

[0032] The present invention analyzes and investigates the internal execution process of the above three languages, and summarizes the scheme for analyzing system calls based on the characteristics of the three languages. In the verification stage, this paper collected open source programs written in three languages ​​from the Internet and tested the system call analysis tool proposed in this paper. The results show that the number of system calls output by the tool is 70-100 on average, and when there is no restriction, the application on the container can call 269 system calls. It can be seen that more than half of the system calls are prohibited, and the tool can generate a specific system call whitelist according to each application, which further enhances security. BRIEF DESCRIPTION OF THE DRAWINGS

[0033] Figure 1 This is the flowchart for analyzing the Python source program in the Python system call analysis section;

[0034] Figure 2 This is the flowchart for analyzing the C source program in the Python system call analysis section;

[0035] Figure 3 This is the flowchart for analyzing the Java source program in the Java system call analysis part;

[0036] Figure 4 The flowchart for analyzing the native method mapping in the Java system call analysis part;

[0037] Figure 5 Flowchart for analyzing binary files in the Java system call analysis section;

[0038] Figure 6 Provides an overall flow chart for PHP system call analysis. DETAILED DESCRIPTION

[0039] The Python system call analysis consists of three parts: Python source program analysis, C source program analysis, and binary file analysis. The Python version used in the experiment is Python 3.6.

[0040] Python source program analysis

[0041] Python source program analysis process is as follows Figure 1 As shown. In order to find out the functions written in C used in the source file, we need to access each function call node on the syntax tree and use the inference function of astroid to infer the relevant information of the function call. If the function is a builtin function, then add it directly to the result set in the form of "builtin+function name"; if the function is a method in a class, then it is also necessary to determine whether its function body in the syntax tree is empty. If it is empty, it indicates that it is a function written in C language. Such functions also need to be added to the result set in the form of "module name+class name+function name"; if neither of the above two situations is true, then the function is a function in a module. At this time, it is also necessary to check its function body. If it is empty, add it to the result set in the form of "module name+function name". Then, it is necessary to access the node representing the function body of the currently called function, traverse each child node of the function body node, and if a function call node is encountered, the qualified function is also added to the result set according to the above method and form, and the function body node of the function is recursively analyzed.

[0042] C source program analysis

[0043] C source program analysis Figure 2 As shown. In this part, we use pycparser to build a syntax tree for the C language source code and analyze it based on this. First, the C language source code of the Python module has a variable of type "PyModuleDef", which records the relevant information of the module. A simple example is as follows.

[0044]

[0045] This variable is a structure, its second element represents the module name, and the fifth element represents the method set in the module, so our first step is to traverse the syntax tree, find this structure, and get the module name and the variable name that stores the module method set from the second and fifth elements. Then we use the name of the module method set to search the syntax tree and find the node representing the set. The module method set is an array of type "PyMethodDef", and each element in the array represents a method in the module. A simple example is as follows.

[0046]

[0047] Each element in the array is a structure. Its first element is the function name in Python, and the second element is the function name in its C language source code. We need to use these two names to map each method in the module. In addition, we also need to know the relevant information of the class in the module. The class is represented by a structure variable of type "PyTypeObject" in the source code. A simple example is as follows.

[0048]

[0049] In this structure variable, the second element represents the class name of the class, and the 28th element represents the method set in the class. This method set is of the same type as the method set in the module. In order to obtain the method name mapping in all classes, we need to find each variable of type PyTypeObject in the syntax tree, and obtain the class name and the name of the method set from it, and then analyze the method set. The analysis method for this part is the same as that for analyzing the method set of the module, so it will not be repeated here. In the actual implementation, in order to speed up the running efficiency of the program, we pre-analyzed the source code of all modules written in C in Python, obtained the correspondence between the C function name and the Python function name, and stored it in a json file. In actual execution, we only need to search in the file.

[0050] Binary file analysis

[0051] For each function written in C, the module it is in has two ways of existence in Python. One is in the form of a dynamic link library, and some modules are compiled into the same binary file as the Python interpreter. For each function written in C, this part finds the binary file corresponding to the module it is in, takes the function to be analyzed as the entry point, performs static analysis on the module binary file, finds the system call number used according to the instruction format of the system call in the assembly language, and then converts it into the corresponding system call name according to the system's system call table.

[0052] The Java system call analysis part mainly consists of three parts: Java source program analysis, native method mapping analysis and binary analysis.

[0053] Java source program analysis

[0054] Java source program analysis process is as follows Figure 3 As shown. In the figure, MD represents the MethodDeclaration node, and MCE represents the MethodCallExpr node. The module first receives a .java file and generates an abstract syntax tree (AST); then it accesses the MD node in the syntax tree. If the method is declared as a native method, it is added to the native method set and the next MD node is accessed; otherwise, the MCE node under the MD node is accessed and the MCE is converted to MD; if the conversion is successful and the converted MD node has not been visited, the MD node is continued to be accessed, otherwise the remaining MCE nodes are accessed; when all source files of the project are analyzed, the module process is terminated and the native method set is returned.

[0055] Native method mapping analysis

[0056] Local method mapping analysis such as Figure 4 As shown. The module first receives a dynamic library file, uses the objdump command to export the symbol table to a txt format file; then intercepts the jni function name in the symbol table; then parses the jni function name according to certain rules, obtains the native method name, and establishes the native method-jni function mapping; if there are dynamic library files that have not been analyzed, repeat the above process, otherwise end and return to the native method-jni function mapping.

[0057] Binary Analysis

[0058] Binary analysis such as Figure 5 As shown. The module first uses the format of "jni function name@dynamic library file name" as the input of the binary analysis module, performs binary analysis, and obtains the system call number set; then uses the ausyscall command to obtain the system call number-system call name mapping, converts the system call number into a system call name, and establishes the jni function-system call name mapping; finally, merges the native method-jni function mapping returned by the local method mapping module to establish the native method-system call name mapping.

[0059] The PHP system call analysis consists of three parts: PHP source program static analysis, function name mapping, and binary static analysis. The overall flow chart is as follows: Figure 6 shown.

[0060] PHP source code static analysis

[0061] After we obtain the source code of the program to be analyzed, we need to perform static syntax analysis on the source code to find all the export functions that may be called during the execution of the source program. After trying various tools, we finally chose Phan, a static syntax analysis tool. The Phan static analysis tool is based on PHP7+ and php-ast extensions. Through Phan, we can check whether all methods, functions, classes, features, interfaces, constants and attribute variables are defined and accessible. Phan implements the construction of abstract syntax trees, tracking function call chains, and checking whether methods and functions are definable and accessible. Not only that, the tool can also track the function call chains in third-party modules referenced by the application, so that the export functions used by the underlying functions in the third-party libraries used by the application can be obtained (similar to Java Native methods). We save the export functions used by these applications into files by locating and modifying the underlying source code of Phan.

[0062] Function name mapping

[0063] By reading the PHP source code, we found that the functions exposed by PHP are mainly in the main and ext folders. Some built-in functions are in the ext folder, that is, they are implemented by extension. We can check the function definitions in the .h file to know which functions PHP exposes. The function names of the functions in PHP are related to the function names in the binary interpreter. Because the binary interpreter file is statically analyzed later, it is necessary to find out the corresponding function names of the exported functions in the binary interpreter. There are certain rules for naming function names in PHP. By writing Python code, the function names are mapped according to the rules.

[0064] Binary Static Analysis

[0065] Perform binary static analysis on the functions in the interpreter, find all the system calls used by the functions, save them to files, find the union of the system calls used by all functions, and generate a system call whitelist for the application.

[0066] Although the above disclosed content shows exemplary embodiments of the present invention, it should be noted that various changes and modifications can be made without departing from the scope of the present invention defined in the claims. According to the structure of the inventive embodiments described herein, the constituent elements of the claims can be replaced with any functionally equivalent elements. Therefore, the protection scope of the present invention should be determined by the content of the attached claims.

Claims

1. A method for generating a system call whitelist for an interpreted language source program, the steps comprising: 1) For the source code of the application to be analyzed, select the corresponding target interpreter according to the writing language of the source code; 2) Using the selected target interpreter to obtain the system calls that may be called by the source code; when the source code is Python language code, the target interpreter includes the open source module astroid and the open source module pycparser; the method for obtaining the system calls that may be called by the source code is as follows: 211) Using the open source module astroid to generate an abstract syntax tree for the source code, and traversing the function call nodes therein, and finding all C language functions according to the module name and class name of the function corresponding to the current function call node; 212) Using the open source module pycparser to generate a corresponding abstract syntax tree H for the source code of the module where each C language function obtained in step 211) is located, according to Pyt hon's C language API features, find the node of the structure array in the abstract syntax tree H that represents all functions and all class methods in the module, each element in the structure array represents a function of the module or a method of a class in the module; establish a mapping between the function name of the C language function in Python and the function name in the C language source code according to the information in the structure array, and then determine the binary file path of the module after compilation according to the mapping relationship; finally output the function name of each C language function in its source code obtained in step 211) and the dynamic link library path where the source code is located; 213) perform static analysis on the binary file output in step 212) to obtain the system calls that may be called by the source code; 3) Write a test program in the language in which the source code is written and run it, obtain the system calls required when running the test program as the system calls required for initializing the target interpreter, and then combine them with the system calls obtained in step 2) to obtain a system call whitelist for the application to be analyzed.

2. The method according to claim 1, characterized in that Use the strace command to get the system calls required to run the test program.

3. A system call control method, the steps of which include: 1) For the source code of the application to be analyzed, select the corresponding target interpreter according to the writing language of the source code; 2) Using the selected target interpreter to obtain the system calls that may be called in the source code; when the source code is Python language code, the target interpreter includes the open source module astroid and the open source module pycparser; the method for obtaining the system calls that may be called by the source code is as follows: 211) Using the open source module astroid to generate an abstract syntax tree for the source code, and traversing the function call nodes therein, find all C language functions according to the module name and class name of the function corresponding to the current function call node; 212) Using the open source module pycparser to find the module name and class name of each C language function obtained in step 211) The source code generates a corresponding abstract syntax tree H, and according to the C language API characteristics of Python, find the node of the structure array in the abstract syntax tree H that represents all functions and all class methods in the module, each element in the structure array represents a function of the module or a method of a class of the module; establish a mapping between the function name of the C language function in Python and the function name in the C language source code according to the information in the structure array, and then determine the binary file path of the module after compilation according to the mapping relationship; finally output the function name of each C language function in its source code and the dynamic link library path where the source code is located obtained in step 211); 213) performing static analysis on the binary file outputted in step 212) to obtain system calls that may be called by the source code; 3) writing a test program in the language of the source code and running it, obtaining the system calls required when running the test program as the system calls required for initializing the target interpreter, and then combining them with the system calls obtained in step 2) to obtain a system call whitelist of the application to be analyzed; 4) Obtain the system call A when the application to be analyzed is running. If the system call A is in the system call whitelist, run the system call A for execution; otherwise, refuse to execute the system call A.

4. The method according to claim 3, characterized in that Use the strace command to get the system calls required to run the test program.