Method and system for dynamically managing detection points in Linux and medium

By defining virtual files and callback functions in Linux, communication between the kernel layer and the application layer is realized, and detection points are dynamically managed, which solves the problem of insufficient flexibility in the existing technology and improves the dynamics and flexibility of the system.

CN118964126APending Publication Date: 2024-11-15SHENZHEN LEAGSOFT TECH
View PDF 0 Cites 2 Cited by

Patent Information

Application Number
CN202410994620.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-23
Publication Date
2024-11-15

AI Technical Summary

Technical Problem

In the kernel layer development based on kprobes technology, the existing technology can only register detection points in advance and cancel afterwards. It is not flexible enough to register and cancel detection points at any time without restarting the kernel layer, which cannot meet the needs of dynamic management.

Method used

By defining virtual files and callback functions in Linux, communication and data exchange between the kernel layer and the application layer are realized, detection points are dynamically managed, including registration, cancellation, enable and disable operations, and the dynamic management of detection points is achieved using virtual files.

Benefits of technology

Improves application flexibility, allows dynamic management of detection points without restarting the kernel layer, meets flexible registration and cancellation requirements, and improves the dynamics of the system.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118964126A_ABST
    Figure CN118964126A_ABST
Patent Text Reader

Abstract

The embodiment of the invention discloses a method and a system for dynamically managing detection points in Linux and a medium. The method comprises the following steps: pre-defining various detection points; defining a data structure communicating with an application layer in Linux; creating a virtual file, and appointing an access permission of the virtual file and a corresponding callback function; the callback function is used for the application layer to read or write the virtual file to exchange data with the kernel layer; dynamically managing the detection points based on the data of the application layer received by the callback function; the method has the beneficial effects that the detection point technology is combined with the virtual file, so that the dynamic management of the detection point is realized, the registration, logout or enabling and disabling operations of the detection point under the condition that the kernel layer is not restarted are met, and the flexibility of the application is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of lightweight tracking of kernel function execution status, and in particular to a method, system and medium for dynamically managing detection points in Linux. Background Art

[0002] Currently, Kprobes is a lightweight technology provided by the Linux kernel to track the execution status of kernel functions. It can register detection points before and after the kernel function is executed, realize dynamic monitoring of kernel functions, and collect the required information. When the user has collected enough information and no longer needs to continue to detect, the detection point can also be dynamically unregistered. Through Kprobes, users can track and analyze the target function without changing the source code, which is helpful for applications in debugging, performance optimization and other scenarios.

[0003] However, in kernel layer development applications based on kprobes technology, detection points can only be registered in advance and deregistered afterwards, which is not flexible enough and cannot meet the need to register and deregister detection points at any time without restarting the kernel layer. Summary of the invention

[0004] In view of the defects in the prior art, the purpose of the present invention is to provide a method, system and medium for dynamically managing detection points in Linux to solve the defects in the prior art.

[0005] In a first aspect, an embodiment of the present invention provides a method for dynamically managing detection points in Linux, which is applied to the kernel layer in Linux, and the method includes:

[0006] Predefine various detection points;

[0007] Defines data structures for communicating with the application layer in Linux;

[0008] Create a virtual file, specify the access rights of the virtual file and the corresponding callback function; wherein the callback function is used by the application layer to read or write the virtual file to exchange data with the kernel layer;

[0009] Dynamic management of the detection point is performed based on the application layer data received by the callback function; wherein the dynamic management includes registration, deregistration, enabling and disabling operations on the detection point.

[0010] As a preferred implementation of the present application, the method further includes:

[0011] If the operation is to cancel the detection point, the structure of the detection point needs to be cleaned up after the cancellation is successful.

[0012] As a preferred implementation of the present application, after receiving the data from the application layer, the callback function also parses the data and verifies its correctness.

[0013] As a specific implementation of the present application, the access rights of the virtual file are specified to be readable and writable by application layer users.

[0014] In a second aspect, an embodiment of the present invention provides another method for dynamically managing detection points in Linux, which is applied to the application layer in Linux, and the method includes:

[0015] Define the data structure for communicating with the kernel layer in Linux;

[0016] Constructing data according to a user-specified strategy or configuration to achieve filling of the data structure;

[0017] Calling the open method of the application layer to open the virtual file in a read-write mode; wherein the virtual file is created by the kernel layer, and the corresponding access rights and callback functions are specified for the virtual file;

[0018] Call the write method of the application layer to send data to the kernel layer;

[0019] When the application layer configuration is completed, the close method of the application layer is called to close the virtual file.

[0020] As a specific implementation of the present application, the application layer includes an application program, and data is constructed according to a policy or configuration specified by a user, specifically including:

[0021] The user configures the policy rules through the interface and sends them to the application;

[0022] The application organizes the data structure after receiving the policy rules sent by the interface;

[0023] Or the user directly configures the application and transmits control information parameters of relevant detection points to the application;

[0024] The application organizes the data structure after processing the control information parameters.

[0025] In a third aspect, an embodiment of the present invention further provides a system for dynamically managing detection points in Linux, the system comprising a kernel layer and an application layer;

[0026] The kernel layer is used to predefine various detection points and data structures for communicating with the application layer, and to create a virtual file to specify the access rights of the virtual file and the corresponding callback function;

[0027] The application layer is used to construct data according to a user-specified policy or configuration, and to exchange data with the kernel layer by reading or writing the virtual file; so that the kernel layer dynamically manages the detection points based on the data of the application layer received by the callback function.

[0028] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, wherein the computer program includes program instructions, and when the program instructions are executed by a processor, the processor executes the method described in the above embodiment.

[0029] The technical solution provided by the embodiment of the present invention combines the detection point technology with the virtual file, utilizes the virtual file to realize the communication and data exchange between the kernel layer and the application layer, realizes the dynamic management of the detection point, satisfies the registration, deregistration or enabling and disabling operations of the detection point without restarting the kernel layer, and improves the flexibility of the application. BRIEF DESCRIPTION OF THE DRAWINGS

[0030] In order to more clearly illustrate the specific embodiments of the present invention or the technical solutions in the prior art, the following is a brief introduction to the drawings required for the specific embodiments or the description of the prior art. In all the drawings, similar elements or parts are generally identified by similar reference numerals. In the drawings, the elements or parts are not necessarily drawn according to the actual scale.

[0031] Figure 1 A flow chart of a method for dynamically managing detection points in Linux provided by an embodiment of the present invention is shown;

[0032] Figure 2 A flow chart of a method for dynamically managing detection points in Linux provided by another embodiment of the present invention is shown;

[0033] Figure 3 A schematic diagram of system interaction for dynamically managing detection points in Linux provided by an embodiment of the present invention is shown. DETAILED DESCRIPTION

[0034] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments 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.

[0035] It should be understood that when used in this specification and the appended claims, the terms "include" and "comprises" indicate the presence of described features, integers, steps, operations, elements and / or components, but do not exclude the presence or addition of one or more other features, integers, steps, operations, elements, components and / or combinations thereof.

[0036] It should also be understood that the terms used in this specification of the present invention are only for the purpose of describing specific embodiments and are not intended to limit the present invention. As used in the specification of the present invention and the appended claims, unless the context clearly indicates otherwise, the singular forms "a", "an" and "the" are intended to include plural forms.

[0037] It should be further understood that the term "and / or" used in the present specification and the appended claims refers to any and all possible combinations of one or more of the associated listed items, and includes these combinations.

[0038] As used in this specification and the appended claims, the term "if" may be interpreted as "when" or "upon" or "in response to determining" or "in response to detecting," depending on the context. Similarly, the phrases "if it is determined" or "if [described condition or event] is detected" may be interpreted as meaning "upon determination" or "in response to determining" or "upon detection of [described condition or event]" or "in response to detecting [described condition or event]," depending on the context.

[0039] It should be noted that, unless otherwise specified, the technical terms or scientific terms used in this application should have the common meanings understood by those skilled in the art to which the present invention belongs.

[0040] like Figure 1 As shown, a method for dynamically managing detection points in Linux provided by an embodiment of the present invention is shown, which is applied to the kernel layer in Linux, and the method includes:

[0041] S101, pre-define various detection points;

[0042] S102, defining a data structure for communicating with the application layer in Linux;

[0043] S103, creating a virtual file, specifying the access rights of the virtual file and the corresponding callback function; wherein the callback function is used by the application layer to read or write the virtual file to exchange data with the kernel layer;

[0044] S104, dynamically managing the detection point based on the application layer data received by the callback function; wherein the dynamic management includes registration, deregistration, enabling and disabling operations on the detection point.

[0045] In the embodiment of the present invention, the kernel layer can also be understood as a Linux kernel module; various detection points refer to all functions or interfaces in the kernel that can be used as detection points; the Kprobe detection points involved in the subsequent description are only examples and are not intended to limit them; such as:

[0046] Detection point 1 structure, detecting the Linux system function security_inode_rename:

[0047] struct kprobe RenameKprobe;

[0048] RenameKprobe.symbol_name="security_inode_rename";

[0049] RenameKprobe.pre_handler=RenamePreHandler;

[0050] Detection point 2 structure, detection point at the Linux system network data receiving point:

[0051] struct nf_hook_ops NetFiterKernelProbe;

[0052] NetFiterKernelProbe.hook=NetFilterHookFun;

[0053] NetFiterKernelProbe.pf=NFPROTO_INET;

[0054] NetFiterKernelProbe.hooknum=NF_INET_LOCAL_OUT;

[0055] NetFiterKernelProbe.priority=NF_IP_PRI_FIRST;

[0056] In this embodiment, the creation of a virtual file and specifying the access rights of the virtual file and the corresponding callback function specifically include:

[0057] 1. Create a virtual file and specify the access rights of the virtual file as read-write for application layer users;

[0058] 2. Specify the callback function when the application layer performs a read operation on the virtual file as ReadCallBack; the callback function when the write operation is performed as WriteCallBack; the callback function is used by the application layer to read or write virtual files to exchange information with the kernel layer;

[0059] struct file_operations stuFileOpt;

[0060] stuFileOpt.read=ReadCallBack;

[0061] stuFileOpt.write=WriteCallBack;

[0062] Use proc_create_data(A,S_IRUSR|S_IWUSR,NULL,&stuFileOpt,NULL) method to complete 1 and 2 above.

[0063] Furthermore, in another embodiment, based on the above technical solution, after receiving the data from the application layer, the callback function also parses the data and verifies its correctness.

[0064] The parsing of data and verification of its correctness is to process the message structure. The message structure is as follows. Only data that meets this structure can be parsed normally.

[0065] / / The maximum message content length of kernel module communication

[0066] #define MAX_EVENT_CONTENT_LEN 512

[0067] / / Kernel message operation type

[0068] typedef enum

[0069] {

[0070] UN_KNOW_OPT=-1,

[0071] UN_REGISTER_FUN_OPT = 0, / / Control kernel module deregistration hook function

[0072] REGISTER_FUN_OPT = 1, / / Control kernel module registration hook function

[0073] DISABLE_FUN_OPT = 2, / / Control kernel module disabled hook function

[0074] ENABLE FUN OPT = 3, / / Control kernel module to enable hook function

[0075] }KERNEI_OPT_TYPE;

[0076] / / Kernel message type

[0077] typedef enum

[0078] {

[0079] KPROBE_EVENT=1, / / Kprobe message

[0080] NET_FILTER_EVENT=2, / / Network data message

[0081] }KERNEI_TYPE;

[0082] / / Message structure received by the kernel module, message from the application layer

[0083] typedef struct RernelMsgBuf

[0084] {

[0085] KERNEL_TYPE iMsgType; / / Message type

[0086] KERNEL__OPT_TYPE ioptType; / / Operation type

[0087] char strMsgContent[MAX_EVENT_CONTENT_LEN]; / / kernel message content

[0088] }stuKerne1MsgBuf;

[0089] Furthermore, based on the above technical solution, in order to re-register the same detection point, the method further includes:

[0090] If the operation is to cancel the detection point, the structure of the detection point needs to be cleaned up after the cancellation is successful.

[0091] When applying, you can use the memset method to clean up.

[0092] In this embodiment, each detection point can be registered, deregistered, enabled, disabled, etc. by calling the method provided by Kprobe technology; wherein the method provided by Kprobe technology includes:

[0093] Registration method:

[0094] int register_kprobe(struct kprobe*p);

[0095] int register_kprobes(struct kprobe**kps,int num);

[0096] How to log out:

[0097] void unregister_kprobe(struct kprobe*p);

[0098] void unregister_kprobes(struct kprobe**kps,int num);

[0099] Enabling method:

[0100] int enable_kprobe(struct kprobe*kp);

[0101] Disability methods:

[0102] int disable_kprobe(struct kprobe*kp).

[0103] The above scheme combines the detection point technology with the virtual file, and uses the virtual file to realize the communication and data exchange between the kernel layer and the application layer, thereby realizing the dynamic management of the detection point, satisfying the registration, deregistration or enabling and disabling operations of the detection point without restarting the kernel layer, and improving the flexibility of the application.

[0104] like Figure 2 FIG. 1 shows a method for dynamically managing detection points in Linux provided by another embodiment of the present invention, which is applied to the application layer in Linux. The method includes:

[0105] S201, defining a data structure for communicating with the kernel layer in Linux;

[0106] S202, constructing data according to a policy or configuration specified by a user to fill the data structure;

[0107] S203, calling the open method of the application layer to open the virtual file in a read-write mode; wherein the virtual file is created by the kernel layer, and a corresponding access permission and callback function are specified for the virtual file;

[0108] S204, calling the write method of the application layer to send data to the kernel layer;

[0109] S205: When the application layer configuration is completed, the close method of the application layer is called to close the virtual file.

[0110] It should be noted that the application layer and the kernel layer described above use the same data structure; calling the write method of the application layer: that is, the virtual file should be opened and the data of S202 should be written therein.

[0111] When the application layer configuration is completed, the application layer calls the write method successfully.

[0112] In this embodiment, the application layer includes an application program, and data is constructed according to a policy or configuration specified by a user, specifically including:

[0113] The user configures the policy rules through the interface and sends them to the application;

[0114] The application organizes the data structure after receiving the policy rules sent by the interface;

[0115] The strategy is an interface; if you want to open detection point 1, you can select and specify detection point 1 in the check box provided in the interface to achieve the transmission of corresponding parameters; the interface can be deployed on the WEB side, and the rules are configured (i.e., policy rules are configured) through the WEB interface, which is suitable for the scenario where the WEB side and the application layer are not in the same device;

[0116] Or the user directly configures the application and transmits control information parameters of relevant detection points to the application;

[0117] The application organizes the data structure after processing the control information parameters.

[0118] The configuration is to directly pass parameters through commands; it is applicable to the scenario where the WEB end and the application layer are in the same device;

[0119] For example, . / LinuxKernelProbeMgrTool message type message operation type message content).

[0120] This application also provides a system for dynamically managing detection points in Linux. Figure 3 , a system for dynamically managing detection points in Linux, the system comprising a kernel layer and an application layer;

[0121] The kernel layer is used to predefine various detection points and data structures for communicating with the application layer, and to create a virtual file to specify the access rights of the virtual file and the corresponding callback function;

[0122] The application layer is used to construct data according to a user-specified policy or configuration, and to exchange data with the kernel layer by reading or writing the virtual file; so that the kernel layer dynamically manages the detection points based on the data of the application layer received by the callback function.

[0123] Figure 3 In the embodiment, the application layer controls data reception, that is, the data of the application layer received based on the callback function; and performs subsequent corresponding analysis.

[0124] Since the system embodiment is basically the method embodiment described above, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment. The system embodiment described below is only illustrative.

[0125] The application layer includes an application program and a WEB interface rule configuration module, and the WEB interface rule configuration module is used to configure policy rules;

[0126] The application layer and the application program may be understood to have the same meaning;

[0127] The application layer is used to build data according to the policy or configuration specified by the user; that is, the user can implement dynamic management of detection points in two ways, including:

[0128] First way:

[0129] a1, the user configures policy rules through the interface and sends these policy rules to the application; the interface here is composed of Figure 3 Provided by the WEB interface rule configuration module;

[0130] a2, the application organizes the data structure after receiving the policy rules issued by the interface;

[0131] a3, the application opens the virtual file and writes the data of a2 into it;

[0132] a4, the callback function in the kernel layer receives and analyzes the data of a3;

[0133] a5: The kernel layer performs registration, deregistration, enabling, or disabling operations on the detection point according to the data in a4.

[0134] Second way:

[0135] b1, the user can directly execute the application and pass the relevant detection point control information parameters to the application;

[0136] b2, the application organizes the data structure after processing the parameters;

[0137] b3, the application opens the virtual file and writes the data of b2 into it;

[0138] b4, the callback function in the kernel layer receives and analyzes the data of b3;

[0139] b5, the kernel layer performs registration, deregistration, enabling, or disabling operations on the detection point according to the data in b4;

[0140] The organizing data structure in the embodiment is to write relevant parameters into the data structure to form data.

[0141] The above solution solves the problem of dynamic management of detection points. The dynamic management includes registering and deregistering detection points at any time without restarting the kernel layer, and enabling and disabling detection points at any time without restarting the kernel layer.

[0142] The present invention further provides an embodiment of a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, wherein the computer program includes program instructions, and when the program instructions are executed by a processor, the processor executes the method described in the above method embodiment.

[0143] The computer-readable storage medium may be an internal storage unit of the terminal described in the foregoing embodiments, such as a hard disk or memory of the terminal. The computer-readable storage medium may also be an external storage device of the terminal, such as a plug-in hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (SecureDigital, SD) card, a flash card (Flash Card), etc. equipped on the terminal. Further, the computer-readable storage medium may also include both an internal storage unit of the terminal and an external storage device. The computer-readable storage medium is used to store the computer program and other programs and data required by the terminal. The computer-readable storage medium may also be used to temporarily store data that has been output or is to be output.

[0144] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in terms of function in the above description. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the present invention.

[0145] In several embodiments provided in the present application, it should be understood that the disclosed system and method can also be implemented in other ways. The method embodiments described above are merely schematic, for example, each box in the flow chart or block diagram can represent a module, a program segment or a part of a code, and a part of the module, program segment or code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a sequence different from that marked in the accompanying drawings. For example, two consecutive boxes can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram and / or flow chart, and the combination of the boxes in the block diagram and / or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or action, or can be implemented with a combination of dedicated hardware and computer instructions.

[0146] If the functions are implemented in the form of software function modules and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on such understanding, the technical solution of the present invention is essentially 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 to enable 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 methods described in various embodiments of the present invention. It should be noted that, in this article, relational terms such as first and second are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply that there is any such actual relationship or order between these entities or operations. The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, the present invention may have various changes and variations. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the protection scope of the present invention.

[0147] The above is only a specific embodiment of the present invention, but the protection scope of the present invention is not limited thereto. Any technician familiar with the technical field can easily think of various equivalent modifications or replacements within the technical scope disclosed by the present invention, and these modifications or replacements should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention shall be based on the protection scope of the claims.

Claims

1. A method for dynamically managing detection points in Linux, characterized in that: Applied to the kernel layer in Linux, the method comprises: Predefine various detection points; Defines data structures for communicating with the application layer in Linux; Create a virtual file, specify the access rights of the virtual file and the corresponding callback function; wherein the callback function is used by the application layer to read or write the virtual file to exchange data with the kernel layer; Dynamic management of the detection point is performed based on the application layer data received by the callback function; wherein the dynamic management includes registration, deregistration, enabling and disabling operations on the detection point.

2. The method according to claim 1, characterized in that The method further comprises: If the operation is to cancel the detection point, the structure of the detection point needs to be cleaned up after the cancellation is successful.

3. The method according to claim 1 or 2, characterized in that After receiving the data from the application layer, the callback function also parses the data and verifies its correctness.

4. The method according to claim 1, characterized in that The access rights of the virtual file are specified to be readable and writable by application layer users.

5. A method for dynamically managing detection points in Linux, characterized in that: Applied to the application layer in Linux, the method comprises: Define the data structure for communicating with the kernel layer in Linux; Constructing data according to a user-specified strategy or configuration to achieve filling of the data structure; Calling the open method of the application layer to open the virtual file in a read-write mode; wherein the virtual file is created by the kernel layer, and the corresponding access rights and callback functions are specified for the virtual file; Call the write method of the application layer to send data to the kernel layer; When the application layer configuration is completed, the close method of the application layer is called to close the virtual file.

6. The method according to claim 5, characterized in that: The application layer includes application programs, which build data according to user-specified policies or configurations, including: The user configures the policy rules through the interface and sends them to the application; The application organizes the data structure after receiving the policy rules sent by the interface; Or the user directly configures the application and transmits control information parameters of relevant detection points to the application; The application organizes the data structure after processing the control information parameters.

7. A system for dynamically managing detection points in Linux, characterized in that: The system includes a kernel layer and an application layer; The kernel layer is used to predefine various detection points and data structures for communicating with the application layer, and to create a virtual file to specify the access rights of the virtual file and the corresponding callback function; The application layer is used to construct data according to the policy or configuration specified by the user, and to exchange data with the kernel layer by reading or writing the virtual file; The kernel layer is enabled to dynamically manage the detection points based on the data of the application layer received by the callback function.

8. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, wherein the computer program includes program instructions, and when the program instructions are executed by a processor, the processor is caused to perform the method according to claim 1 or 5.

Citation Information

Cited By

  • Dynamic netlink number allocation method, device, equipment and product

    CN119440882A

  • GPIO resource management method and device and storage medium

    CN121070843A