Data tamper-proofing method, device, intelligent terminal and computer-readable storage medium
By obtaining the read-only segment address of the data to be protected in the embedded system and performing write protection, the problem of data being easily tampered with is solved, and the stable operation of the device and the improved user experience are achieved.
Patent Information
- Application Number
- CN202011617600.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-12-30
- Publication Date
- 2025-09-09
- Estimated Expiration
- 2040-12-30
AI Technical Summary
In the existing technology, embedded real-time systems lack data protection mechanisms, which makes memory data vulnerable to illegal access and tampering. The device needs to wait for the watchdog to reset, affecting the user experience.
By obtaining the read-only segment address of the data to be protected, the read-only segment is write-protected, and the write instruction is encapsulated to determine the memory address and return an error message to prevent illegal writing.
Effectively prevent data tampering, improve device operation reliability, enhance user experience, and reduce device reset and unavailability issues caused by data tampering.
Smart Images

Figure CN114692213B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of data protection technology, and in particular to a data tamper-proofing method, device, intelligent terminal and computer-readable storage medium. Background Art
[0002] With the advancement of science and technology, smart devices are becoming increasingly popular, their performance is increasing, and the amount of data they carry is also increasing. Consequently, data protection is gaining increasing attention. In particular, as IoT technology matures, more and more smart devices are integrated into the IoT, leading to more device problems. Among these, data tampering is a common problem, often caused by unauthorized access to memory.
[0003] In the prior art, some embedded real-time systems lack data protection mechanisms, making it easy for illegal access to memory data to occur, resulting in data tampering and causing device problems. The system can only wait for the watchdog to reset, and the device is unavailable before the reset is completed, greatly affecting the user experience.
[0004] Therefore, the existing technology still needs to be improved and developed. Summary of the Invention
[0005] The main purpose of the present invention is to provide a data tamper-proofing method, device, intelligent terminal and computer-readable storage medium, aiming to solve the problem in the prior art that illegal access to memory data is prone to occur, resulting in data tampering, causing problems in the device and requiring only waiting for the watchdog to reset.
[0006] In order to achieve the above-mentioned object, the first aspect of the present invention provides a data tamper-proofing method, which comprises:
[0007] Obtain the data to be protected;
[0008] Obtain the address of the read-only segment of the data to be protected;
[0009] Based on the address of the read-only segment, the read-only segment is write-protected.
[0010] Optionally, the write protection of the read-only segment based on the address of the read-only segment includes:
[0011] Obtaining a target write instruction in the system where the data to be protected is located, wherein the target write instruction is an instruction for writing data to the data to be protected;
[0012] Encapsulating the target write instruction, and obtaining the to-be-written data information and the target memory address based on the encapsulated target write instruction;
[0013] If the target memory address belongs to the address of the read-only segment, an error message is returned.
[0014] Optionally, after obtaining the to-be-written data information and the target memory address based on the encapsulated target write instruction, the method further includes: if the target memory address belongs to the address of the read-only segment, outputting stack backtrace information.
[0015] Optionally, after obtaining the data information to be written and the target memory address based on the encapsulated target write instruction, the method further includes: if the target memory address does not belong to the address of the read-only segment, writing the data information to be written into the target memory address.
[0016] Optionally, the data to be protected is data of a program to be protected, and obtaining the address of the read-only segment of the data to be protected includes:
[0017] Obtain the executable file generated by compiling the above-mentioned program to be protected;
[0018] The address of the read-only segment is obtained based on the executable file.
[0019] Optionally, the executable file includes a program segment, a read-only data segment, a data segment, a BSS segment, a heap, and a stack.
[0020] Optionally, the read-only segment includes the program segment and the read-only data segment, and obtaining the address of the read-only segment based on the executable file includes:
[0021] Obtain memory addresses corresponding to the program segment and the read-only data segment in the executable file as the address of the read-only segment.
[0022] A second aspect of the present invention provides a data tamper-proof device, comprising:
[0023] A module for acquiring data to be protected, used for acquiring data to be protected;
[0024] A read-only address acquisition module, used to obtain the address of the read-only segment of the data to be protected;
[0025] The protection module is used to write-protect the read-only segment based on the address of the read-only segment.
[0026] The third aspect of the present invention provides an intelligent terminal, which includes: a memory, a processor, and a data tamper-proofing program stored in the memory and runnable on the processor. When the data tamper-proofing program is executed by the processor, the steps of any one of the above-mentioned data tamper-proofing methods are implemented.
[0027] A fourth aspect of the present invention provides a computer-readable storage medium, wherein the computer-readable storage medium stores a data tamper-proofing program, and when the data tamper-proofing program is executed by a processor, the steps of any one of the above-mentioned data tamper-proofing methods are implemented.
[0028] As can be seen from the above, the present invention provides a data tamper-proofing method, device, intelligent terminal, and computer-readable storage medium. The data tamper-proofing method provided by the present invention includes: obtaining the data to be protected; obtaining the address of the read-only segment of the data to be protected; and write-protecting the read-only segment based on the address of the read-only segment. The solution of the present invention write-protects the read-only segment based on the memory address corresponding to the read-only segment of the data to be protected. Therefore, compared with the prior art, the solution of the present invention is conducive to protecting memory data, preventing data from being tampered with, thereby preventing device problems and improving user experience. BRIEF DESCRIPTION OF THE DRAWINGS
[0029] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, 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 only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0030] Figure 1 This is a flow chart of a data tamper-proofing method provided by an embodiment of the present invention;
[0031] Figure 2 This is an embodiment of the present invention Figure 1 Specific flow diagram of step S300;
[0032] Figure 3 This is an embodiment of the present invention Figure 1 Specific flow diagram of step S200;
[0033] Figure 4 This is a schematic diagram of executable file segmentation provided by an embodiment of the present invention;
[0034] Figure 5 This is a flow chart of obtaining the address of a read-only segment of program data provided by an embodiment of the present invention;
[0035] Figure 6 This is a schematic diagram of a data writing process provided by an embodiment of the present invention;
[0036] Figure 7 1 is a structural diagram of a data tamper-proof device provided by an embodiment of the present invention;
[0037] Figure 8This is a block diagram of the internal structure of a smart terminal provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0038] In the following description, specific details such as particular system structures and techniques are provided for purposes of illustration, not limitation, to facilitate a thorough understanding of the embodiments of the present invention. However, it will be apparent to those skilled in the art that the present invention may be practiced in other embodiments without these specific details. In other cases, detailed descriptions of well-known systems, devices, circuits, and methods are omitted so as not to obscure the description of the present invention with unnecessary detail.
[0039] It will be understood that when used in this specification and the appended claims, the term "comprising" indicates the presence of described features, integers, steps, operations, elements and / or components, but does not preclude the presence or addition of one or more other features, integers, steps, operations, elements, components and / or groups thereof.
[0040] It should also be understood that the terms used in the present specification are only for the purpose of describing particular embodiments and are not intended to limit the present invention. As used in the present specification and the appended claims, the singular forms "a", "an", and "the" are intended to include the plural forms unless the context clearly indicates otherwise.
[0041] It should be further understood that the term "and / or" used in the present description and the appended claims refers to and includes any and all possible combinations of one or more of the associated listed items.
[0042] As used in this specification and the appended claims, the term "if" can be interpreted as "when" or "upon" or "in response to determining" or "in response to detecting," depending on the context. Similarly, the phrase "if it is determined" or "if [described condition or event] is detected" can 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.
[0043] It will be understood by those skilled in the art that, unless otherwise defined, all terms (including technical and scientific terms) used herein have the same meaning as commonly understood by those skilled in the art in the art to which the present invention belongs. It should also be understood that terms such as those defined in common dictionaries should be understood to have meanings consistent with their meanings in the context of the prior art and will not be interpreted in an idealized or overly formal sense unless specifically defined as herein.
[0044] The following is a clear and complete description of the technical solutions in the embodiments of the present invention, in conjunction with the accompanying drawings of the embodiments of the present invention. Obviously, the described embodiments are only 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 making creative efforts are within the scope of protection of the present invention.
[0045] In the following description, many specific details are set forth to facilitate a full understanding of the present invention. However, the present invention may also be implemented in other ways different from those described herein. Those skilled in the art may make similar generalizations without violating the connotation of the present invention. Therefore, the present invention is not limited to the specific embodiments disclosed below.
[0046] With the advancement of science and technology and the maturation of the Internet of Things (IoT), the number of smart devices connected to the IoT continues to grow, and each smart device carries an increasing amount of data. However, with this increase in the number of connected smart devices and the data they carry, device problems are also increasing. For example, devices going offline for unknown reasons or occasionally restarting after being shipped are issues that users find difficult to accept. These issues are often sporadic and difficult to reproduce, making them challenging to locate. There are many reasons for these issues, such as tampering with data in device memory, which can lead to program instability and cause device problems.
[0047] Existing technologies lack a mechanism to protect program data, resulting in low program robustness. This is particularly true in some embedded real-time systems. Once illegal memory access occurs, the device becomes unavailable and requires a watchdog reset. This unavailability until the reset is complete significantly impacts the user experience. Therefore, a method is needed to prevent data tampering and improve program robustness.
[0048] In order to operate memory data more safely, thereby improving the robustness of the program, preventing equipment problems, and improving user experience, an embodiment of the present invention provides a data tamper-proofing method, device, intelligent terminal, and computer-readable storage medium. Among them, the data tamper-proofing method provided by an embodiment of the present invention includes: obtaining the data to be protected; obtaining the address of the read-only segment of the above-mentioned data to be protected; and write-protecting the above-mentioned read-only segment based on the address of the above-mentioned read-only segment. The solution of the present invention write-protects the read-only segment based on the memory address corresponding to the read-only segment of the data to be protected. Therefore, compared with the existing technology, the solution of the present invention is conducive to protecting memory data, preventing data from being tampered with, thereby preventing equipment problems and improving user experience.
[0049] The invention will be further explained below through description of embodiments in conjunction with the accompanying drawings.
[0050] Exemplary Methods
[0051] like Figure 1 As shown, an embodiment of the present invention provides a data tamper-proofing method, which includes:
[0052] Step S100: Acquire data to be protected.
[0053] The aforementioned data to be protected refers to data that needs to be protected to prevent tampering. In one application scenario, some important data in the device system can be pre-set as protected data, especially data that will directly affect the operation of the system, such as data of system applications. Data that is not related to the normal operation of the device system can be excluded from protection to reduce the resources required for the device to execute the aforementioned data tamper-proofing method. In another application scenario, users can customize the data to be protected in the system and can also adjust it at any time according to actual needs. No specific restrictions are made here.
[0054] Step S200: Obtain the address of the read-only segment of the data to be protected.
[0055] Step S300: Based on the address of the read-only segment, write protection is performed on the read-only segment.
[0056] Among them, only some of the data segments of the above-mentioned data to be protected may not allow data to be written. In this case, the memory address corresponding to the read-only segment of the data to be protected can be obtained, thereby prohibiting data writing to the corresponding memory address, realizing write protection of the read-only segment of the data to be protected, preventing data from being tampered with, and then preventing the device from having problems due to data tampering, thereby improving the reliability of the device operation and enhancing the user experience. Specifically, because the read-only segment of the data to be protected is usually continuous in memory address, the starting memory address and ending memory address of the read-only segment can be obtained, and the range of the memory address of the read-only segment can be determined based on the above-mentioned starting memory address and ending memory address.
[0057] As can be seen from the above, an embodiment of the present invention provides a data tamper-proofing method, which obtains data to be protected; obtains the address of a read-only segment of the data to be protected; and based on the address of the read-only segment, write-protects the read-only segment. The present invention write-protects the read-only segment based on the memory address corresponding to the read-only segment of the data to be protected. Therefore, compared with the existing technology, the present invention is more conducive to protecting memory data, preventing data tampering, thereby preventing device problems and improving the user experience.
[0058] Specifically, in this embodiment, Figure 2 As shown, the above step S300 includes:
[0059] Step S301: obtaining a target write instruction in the system where the data to be protected is located, wherein the target write instruction is an instruction for writing data to the data to be protected.
[0060] Step S302: encapsulate the target write instruction, and obtain the to-be-written data information and the target memory address based on the encapsulated target write instruction.
[0061] Step S303: If the target memory address belongs to the address of the read-only segment, an error message is returned.
[0062] Specifically, in the system of the device where the data to be protected is located, writing to the memory data is performed through a write instruction. Therefore, in this embodiment, an instruction that may be used in the system to write the data to be protected is obtained as a target write instruction, and the above target write instruction is encapsulated. Specifically, a process for judging the memory address is added to the above target write instruction. In this way, after obtaining the data information to be written and the target memory address based on the encapsulated target write instruction, the above target memory address will be judged. When the above target memory address belongs to the address of the above read-only segment, an error message is returned.
[0063] In this embodiment, the system where the data to be protected is located implements data operations based on the C language as an example for explanation. Specifically, when the system is based on the C language, the target write instruction can be a C library function with the function of writing data to the memory, such as the strcpy function (string copy function), the memcpy function (memory copy function), etc. The above encapsulation of the target write instruction is specifically as follows: a custom memory address check function is defined to check whether the target memory address belongs to the address of the read-only segment; the C library function with the function of writing data to the memory is encapsulated so that when it is called, it first calls the memory address check function to determine whether the target memory address belongs to the address of the read-only segment and returns the judgment result; based on the judgment result returned by the memory address check function, it is decided whether to write the memory data. If the target memory address belongs to the address of the read-only segment, the data is not written and an error message is directly returned.
[0064] Optionally, a memory address check function provided in an embodiment of the present application is as follows:
[0065] #define CHECK_ADDR_RO(addr)((addr)>=__read_only_text_start&&(addr)<=__read_only_text_end)
[0066] Where addr is the target memory address to be checked, __read_only_text_start is the starting memory address of the read-only segment, and __read_only_text_end is the ending memory address of the read-only segment. Because the read-only segments of the data to be protected are usually continuous in memory address, the target memory address can be determined to be an address of the read-only segment by checking whether it is greater than or equal to the starting memory address of the read-only segment and less than or equal to the ending memory address of the read-only segment.
[0067] Optionally, an encapsulated strcpy function provided in an embodiment of the present application is as follows:
[0068]
[0069] Where dst and src are the target memory address and data to be written, respectively, obtained by the encapsulated strcpy function (i.e., the strcpy_safe function shown above). When the encapsulated strcpy function is called, it first calls the memory address check function to determine the target memory address. If it determines that the target memory address belongs to the address of the read-only segment, it returns an error message (i.e., -1) and does not write data, thereby achieving write protection for the protected data.
[0070] Optionally, after step S302, the method further includes: if the target memory address belongs to the address of the read-only segment, outputting stack backtrace information.
[0071] Specifically, in the encapsulated strcpy function shown above, when it determines that the target memory address belongs to the read-only segment, the show_stack function is called to output stack traceback information, allowing users to locate the instruction that illegally accessed memory. Specifically, the stack traceback information includes the function call relationship. If an illegal write request to memory data occurs when calling a function, the function call relationship related to the function is traced back and output, which helps developers locate and verify the problem.
[0072] Optionally, after the above step S302, the above method further includes: if the above target memory address does not belong to the address of the above read-only segment, writing the above data information to be written into the above target memory address.
[0073] Furthermore, when it is determined that the target memory address does not belong to the address of the read-only segment, the corresponding standard C library function (such as the standard strcpy function) can be directly called to write the data. Optionally, after the data is successfully written, a success message can be returned so that the user can know the data writing result.
[0074] The error message returned may be a preset prompt indicating that data cannot be written, such as "write failed". Similarly, the success message returned may be a preset prompt indicating that data is successfully written, such as "write successful", so that the user can understand the data writing status. Optionally, a return flag may be set, such as setting the flag "-1" as the return flag for write failure and setting the flag "1" as the return flag for write success, and returning the return flag to the source program of the target write instruction, so that the source program can understand the data writing status and determine subsequent operations.
[0075] In this embodiment, the above description is made by taking the system where the data to be protected is located to implement data operations based on C language as an example. In actual applications, the system where the data to be protected is located can also implement data operations based on other programming languages. In this case, the corresponding data tamper-proofing process is similar to the above process and is not specifically limited here.
[0076] In this embodiment, the data to be protected is the data of the program to be protected. Figure 3 As shown, the above step S200 includes:
[0077] Step S201: Obtain the executable file generated by compiling the program to be protected.
[0078] Step S202: Obtain the address of the read-only segment based on the executable file.
[0079] The aforementioned programs to be protected are those in the device system that require protection. This can include all programs in the system or only a few important programs, without specific limitations. Programs are a crucial part of the device system, and a lack of protection for program data can compromise program robustness. Illegal writes to program data can cause device malfunctions, forcing the device to wait for a watchdog reset, severely impacting normal user experience. Therefore, in this embodiment, the data of the programs to be protected is protected as protected data to prevent unauthorized tampering with the data in the programs to be protected.
[0080] Specifically, when the program is compiled and finally generates an executable file, it will be roughly divided into several sections according to certain rules. Among them, the specific rules depend on the specific compilation method. In this embodiment, Figure 4As shown, the executable file generated by compiling the above-mentioned program to be protected includes a program segment (.text), a read-only data segment (.rodata), a data segment (.data), a BSS segment, a heap and a stack. Among them, the above-mentioned BSS segment is an uninitialized data (BSS, Block Started by Symbol) segment. After the executable file of the above-mentioned program to be protected is run, the above-mentioned segments will be loaded into the designated address memory according to the information of each segment in the link script, so that the reading or writing of each segment can be realized through the memory. Among them, the above-mentioned link script is essentially a rule file for directing the linker to work. The linker refers to the above-mentioned link script and uses the rules specified therein to process the above-mentioned segments and link them into an executable file.
[0081] In this embodiment, the read-only segment includes the program segment and the read-only data segment. The step S202 specifically includes: obtaining the memory addresses corresponding to the program segment and the read-only data segment in the executable file as the addresses of the read-only segment.
[0082] Specifically, the program segments and read-only data segments are typically not writable. If they are illegally written to, they may cause the system to generate an illegal signal, thereby crashing the corresponding process or thread and rendering the device unusable. Therefore, in this embodiment, the program segments and read-only data segments are pre-set as read-only segments. In actual use, the read-only segments can also be adjusted based on actual needs. For example, if a programmer customizes a heap or stack to only store a certain fixed data and does not allow writing, the corresponding heap or stack can also be set as a read-only segment. This is not specifically limited here.
[0083] In this embodiment, the starting memory address and ending memory address corresponding to the program segment and the read-only data segment can be obtained respectively; the range of the memory area address corresponding to the program segment and the read-only data segment can be determined based on the starting memory address and the ending memory address, thereby obtaining the range corresponding to the address of the read-only segment. Since the storage addresses of each segment in the memory are generally continuous, the range of the memory address of the program segment and the read-only data segment can be directly delineated based on the starting memory address and the ending memory address, thereby reducing the amount of calculation and storage.
[0084] In this embodiment, the data tamper-proof method is described based on a specific application scenario. Specifically, Figure 5 A schematic diagram of a flow chart of obtaining the address of a read-only segment of program data provided by an embodiment of the present invention, Figure 6 A schematic diagram of a data writing process provided by an embodiment of the present invention. Figure 5As shown, in an embodiment of the present invention, the corresponding program is first started, the above program is compiled to obtain the corresponding executable file, and the above executable file is loaded into the memory. Specifically, the above executable file includes a program segment, a read-only data segment, a data segment, a BSS segment, a heap and a stack, and each segment in the above executable file is loaded into the memory according to the address information in the link script. Further, the memory address corresponding to each segment in the above executable file is obtained. In this embodiment, only the program segment and the read-only data segment are set as read-only segments, so it is also possible to only obtain the memory address corresponding to the above program segment and the read-only data segment, which is not specifically limited here. Specifically, the starting memory address and the ending memory address corresponding to the above program segment and the read-only data segment can be obtained, and the memory address of the read-only segment can be further obtained based on the above starting memory address and the ending memory address.
[0085] After obtaining the memory address of the read-only segment, the program is write-protected based on the memory address of the read-only segment. Figure 6 As shown, based on the write instruction, the data information to be written and the target memory address are obtained. In this embodiment, the above-mentioned write instruction can be "write 0xff to 0x55aa", that is, the target memory address is 0x55aa, and the data information to be written is 0xff. It is judged whether the above-mentioned target memory address is in the memory address range of the read-only segment. When the above-mentioned target memory address is in the memory address range of the read-only segment, the data is not written and failure is returned. Otherwise, the data is written and success is returned. For example, it is judged whether the above-mentioned 0x55aa is in the memory address range of the read-only segment. When 0x55aa is in the memory address range of the read-only segment, the data is not written and failure is returned; when 0x55aa is not in the memory address range of the read-only segment, 0xff is written to 0x55aa and success is returned. In this way, when designing the API, the address of the input parameter is checked. If it is determined that a write operation is performed on the memory address corresponding to the read-only segment, an error is returned. This can implement write protection for the memory corresponding to the read-only segment of the program, thereby improving the robustness of the target program, preventing device problems, and enhancing the user experience.
[0086] Exemplary devices
[0087] Figure 7 As shown in , corresponding to the above-mentioned data tamper-proof method, an embodiment of the present invention further provides a data tamper-proof device, and the above-mentioned data tamper-proof device includes:
[0088] The data to be protected obtaining module 710 is used to obtain the data to be protected.
[0089] The aforementioned data to be protected refers to data that needs to be protected to prevent tampering. In one application scenario, some important data in the device system can be pre-set as protected data, especially data that will directly affect the operation of the system, such as data of system applications. Data that is not related to the normal operation of the device system can be excluded from protection to reduce the resources required for the device to execute the aforementioned data tamper-proofing method. In another application scenario, users can customize the data to be protected in the system and can also adjust it at any time according to actual needs. No specific restrictions are made here.
[0090] The read-only address acquisition module 720 is used to acquire the address of the read-only segment of the data to be protected.
[0091] The protection module 730 is configured to write-protect the read-only segment based on the address of the read-only segment.
[0092] Among them, only some of the data segments of the above-mentioned data to be protected may not allow data to be written. In this case, the memory address corresponding to the read-only segment of the data to be protected can be obtained, thereby prohibiting data writing to the corresponding memory address, realizing write protection of the read-only segment of the data to be protected, preventing data from being tampered with, and then preventing the device from having problems due to data tampering, thereby improving the reliability of the device operation and enhancing the user experience. Specifically, because the read-only segment of the data to be protected is usually continuous in memory address, the starting memory address and ending memory address of the read-only segment can be obtained, and the range of the memory address of the read-only segment can be determined based on the above-mentioned starting memory address and ending memory address.
[0093] As can be seen from the above, an embodiment of the present invention provides a data tamper-proofing device. The data tamper-proofing device obtains the data to be protected through the protected data acquisition module 710; obtains the address of the read-only segment of the data to be protected through the read-only address acquisition module 720; and write-protects the read-only segment based on the address of the read-only segment through the protection module 730. The solution of the present invention write-protects the read-only segment of the data to be protected based on the memory address corresponding to the read-only segment. Therefore, compared with the existing technology, the solution of the present invention is more conducive to protecting memory data, preventing data tampering, thereby preventing device problems and improving the user experience.
[0094] Specifically, in this embodiment, the above-mentioned protection module 730 is specifically used to: obtain the target write instruction in the system where the above-mentioned data to be protected is located, wherein the above-mentioned target write instruction is an instruction for writing data to the above-mentioned data to be protected; encapsulate the above-mentioned target write instruction, and obtain the data information to be written and the target memory address based on the encapsulated target write instruction; if the above-mentioned target memory address belongs to the address of the above-mentioned read-only segment, return an error message.
[0095] Specifically, in the system of the device where the data to be protected is located, writing to the memory data is performed through a write instruction. Therefore, in this embodiment, the protection module 730 obtains the instructions in the system that may write the data to be protected as target write instructions, and encapsulates the target write instructions. Specifically, a process for judging the memory address is added to the target write instruction. In this way, after obtaining the data information to be written and the target memory address based on the encapsulated target write instruction, the target memory address will be judged. When the target memory address belongs to the address of the read-only segment, an error message is returned.
[0096] In this embodiment, the system where the data to be protected is located implements data operations based on the C language as an example for explanation. Specifically, when the system is based on the C language, the target write instruction can be a C library function with the function of writing data to the memory, such as the strcpy function (string copy function), the memcpy function (memory copy function), etc. The above encapsulation of the target write instruction is specifically as follows: a custom memory address check function is defined to check whether the target memory address belongs to the address of the read-only segment; the C library function with the function of writing data to the memory is encapsulated so that when it is called, it first calls the memory address check function to determine whether the target memory address belongs to the address of the read-only segment and returns the judgment result; based on the judgment result returned by the memory address check function, it is decided whether to write the memory data. If the target memory address belongs to the address of the read-only segment, the data is not written and an error message is directly returned.
[0097] Optionally, a memory address check function provided in an embodiment of the present application is as follows:
[0098] #define CHECK_ADDR_RO(addr)((addr)>=__read_only_text_start&&(addr)<=__read_only_text_end)
[0099] Where addr is the target memory address to be checked, __read_only_text_start is the starting memory address of the read-only segment, and __read_only_text_end is the ending memory address of the read-only segment. Because the read-only segments of the data to be protected are usually continuous in memory address, the target memory address can be determined to be an address of the read-only segment by checking whether it is greater than or equal to the starting memory address of the read-only segment and less than or equal to the ending memory address of the read-only segment.
[0100] Optionally, an encapsulated strcpy function provided in an embodiment of the present application is as follows:
[0101]
[0102]
[0103] Where dst and src are the target memory address and data to be written, respectively, obtained by the encapsulated strcpy function (i.e., the strcpy_safe function shown above). When the encapsulated strcpy function is called, it first calls the memory address check function to determine the target memory address. If it determines that the target memory address belongs to the address of the read-only segment, it returns an error message (i.e., -1) and does not write data, thereby achieving write protection for the protected data.
[0104] Optionally, the protection module 730 is further configured to: output stack backtrace information if the target memory address belongs to the address of the read-only segment.
[0105] Specifically, in the encapsulated strcpy function shown above, when it determines that the target memory address belongs to the read-only segment, the show_stack function is called to output stack traceback information, allowing users to locate the instruction that illegally accessed memory. Specifically, the stack traceback information includes the function call relationship. If an illegal write request to memory data occurs when calling a function, the function call relationship related to the function is traced back and output, which helps developers locate and verify the problem.
[0106] Optionally, the protection module 730 is further configured to: if the target memory address does not belong to the address of the read-only segment, write the data information to be written into the target memory address.
[0107] Furthermore, when it is determined that the target memory address does not belong to the address of the read-only segment, the corresponding standard C library function (such as the standard strcpy function) can be directly called to write the data. Optionally, after the data is successfully written, a success message can be returned so that the user can know the data writing result.
[0108] The error message returned may be a preset prompt indicating that data cannot be written, such as "write failed". Similarly, the success message returned may be a preset prompt indicating that data is successfully written, such as "write successful", so that the user can understand the data writing status. Optionally, a return flag may be set, such as setting the flag "-1" as the return flag for write failure and setting the flag "1" as the return flag for write success, and returning the return flag to the source program of the target write instruction, so that the source program can understand the data writing status and determine subsequent operations.
[0109] In this embodiment, the above description is made by taking the system where the data to be protected is located to implement data operations based on C language as an example. In actual applications, the system where the data to be protected is located can also implement data operations based on other programming languages. In this case, the corresponding data tamper-proofing process is similar to the above process and is not specifically limited here.
[0110] In this embodiment, the data to be protected is data of the program to be protected. In this case, the read-only address acquisition module 720 is specifically used to: obtain the executable file compiled by the program to be protected; and obtain the address of the read-only segment based on the executable file.
[0111] The aforementioned programs to be protected are those in the device system that require protection. This can include all programs in the system or only a few important programs, without specific limitations. Programs are a crucial part of the device system, and a lack of protection for program data can compromise program robustness. Illegal writes to program data can cause device malfunctions, forcing the device to wait for a watchdog reset, severely impacting normal user experience. Therefore, in this embodiment, the data of the programs to be protected is protected as protected data to prevent unauthorized tampering with the data in the programs to be protected.
[0112] Specifically, when the program is compiled and finally generates an executable file, it will be roughly divided into several sections according to certain rules. Among them, the specific rules depend on the specific compilation method. In this embodiment, Figure 4 As shown, the executable file generated by compiling the above-mentioned program to be protected includes a program segment (.text), a read-only data segment (.rodata), a data segment (.data), a BSS segment, a heap and a stack. Among them, the above-mentioned BSS segment is an uninitialized data (BSS, Block Started by Symbol) segment. After the executable file of the above-mentioned program to be protected is run, the above-mentioned segments will be loaded into the designated address memory according to the information of each segment in the link script, so that the reading or writing of each segment can be realized through the memory. Among them, the above-mentioned link script is essentially a rule file for directing the linker to work. The linker refers to the above-mentioned link script and uses the rules specified therein to process the above-mentioned segments and link them into an executable file.
[0113] In this embodiment, the read-only segment includes the program segment and the read-only data segment. The read-only address acquisition module 720 is specifically used to obtain the memory addresses corresponding to the program segment and the read-only data segment in the executable file as the addresses of the read-only segment.
[0114] Specifically, the program segments and read-only data segments are typically not writable. If they are illegally written to, they may cause the system to generate an illegal signal, thereby crashing the corresponding process or thread and rendering the device unusable. Therefore, in this embodiment, the program segments and read-only data segments are pre-set as read-only segments. In actual use, the read-only segments can also be adjusted based on actual needs. For example, if a programmer customizes a heap or stack to only store a certain fixed data and does not allow writing, the corresponding heap or stack can also be set as a read-only segment. This is not specifically limited here.
[0115] In this embodiment, the starting memory address and ending memory address corresponding to the program segment and the read-only data segment can be obtained respectively; the range of the memory area address corresponding to the program segment and the read-only data segment can be determined based on the starting memory address and the ending memory address, thereby obtaining the range corresponding to the address of the read-only segment. Since the storage addresses of each segment in the memory are generally continuous, the range of the memory address of the program segment and the read-only data segment can be directly delineated based on the starting memory address and the ending memory address, thereby reducing the amount of calculation and storage.
[0116] Further, such as Figure 8 As shown, based on the above data tamper-proofing method, an embodiment of the present invention further provides a smart terminal, which includes a processor, a memory and a display. Figure 8 Only some components of the smart terminal are shown, but it should be understood that it is not required to implement all of the shown components, and more or fewer components may be implemented instead.
[0117] The above-mentioned memory can be an internal storage unit and / or an external storage unit of the above-mentioned smart terminal. The above-mentioned memory stores a data tamper-proof program, which can be executed by a processor. When the above-mentioned data tamper-proof program is executed by the above-mentioned processor, the steps of the above-mentioned data tamper-proof method are implemented. In some embodiments, the above-mentioned processor can be a central processing unit, a microprocessor or other data processing chip, which is used to run the program code stored in the above-mentioned memory or process data. In some embodiments, the above-mentioned display can be an LED display, a liquid crystal display, a touch-sensitive liquid crystal display, an OLED touch device, etc. The above-mentioned display is used to display information of the smart terminal and to display a visual user interface. The components of the above-mentioned smart terminal communicate with each other via a system bus.
[0118] In one embodiment, when the processor executes the data tamper-proof program in the memory, the following steps are implemented:
[0119] Obtain the data to be protected;
[0120] Obtain the address of the read-only segment of the data to be protected;
[0121] Based on the address of the read-only segment, the read-only segment is write-protected.
[0122] An embodiment of the present invention further provides a computer-readable storage medium, on which a data tamper-proofing program is stored. When the data tamper-proofing program is executed by a processor, the steps of any one of the above-mentioned data tamper-proofing methods are implemented.
[0123] It should be understood that the sequence numbers of the steps in the above embodiments do not imply a specific order of execution; the order of execution of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present invention.
[0124] Those skilled in the art can clearly understand that, for the convenience and brevity of description, only the division of the above-mentioned functional units and modules is used as an example for illustration. In actual applications, the above-mentioned functions can be distributed and completed by different functional units and modules as needed, that is, the internal structure of the above-mentioned device can be divided into different functional units or modules to complete all or part of the functions described above. The functional units and modules in the embodiment can be integrated into one processing unit, or each unit can exist physically alone, or two or more units can be integrated into one unit. The above-mentioned integrated unit can be implemented in the form of hardware or in the form of software functional units. In addition, the specific names of the functional units and modules are only for the convenience of distinguishing each other, and are not used to limit the scope of protection of the present invention. The specific working process of the units and modules in the above-mentioned system can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here.
[0125] In the above embodiments, the description of each embodiment has its own focus. For parts that are not described or recorded in detail in a certain embodiment, reference can be made to the relevant description of other embodiments.
[0126] Those skilled 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, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professionals and technicians can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of the present invention.
[0127] In the embodiments provided by the present invention, it should be understood that the disclosed apparatus / terminal device and method can be implemented in other ways. For example, the apparatus / terminal device embodiments described above are merely illustrative. For example, the division of the modules or units described above is merely a logical functional division. In actual implementation, other division methods may be used. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not implemented.
[0128] If the above-mentioned integrated module / unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the present invention implements all or part of the process in the above-mentioned embodiment method, and can also be completed by instructing the relevant hardware through a computer program. The above-mentioned computer program can be stored in a computer-readable storage medium. When the computer program is executed by the processor, it can implement the steps of the above-mentioned various method embodiments. Among them, the above-mentioned computer program includes computer program code, and the above-mentioned computer program code can be in source code form, object code form, executable file or some intermediate form. The above-mentioned computer-readable medium may include: any entity or device capable of carrying the above-mentioned computer program code, recording medium, USB flash drive, mobile hard disk, magnetic disk, optical disk, computer memory, read-only memory (ROM), random access memory (RAM), electric carrier signal, telecommunication signal and software distribution medium. It should be noted that the content contained in the above-mentioned computer-readable storage medium can be appropriately increased or decreased according to the requirements of legislation and patent practice in the jurisdiction.
[0129] The embodiments described above are only used to illustrate the technical solutions of the present invention, rather than to limit the same. Although the present invention has been described in detail with reference to the aforementioned embodiments, it should be understood by those skilled in the art that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features thereof may be replaced by equivalents. However, these modifications or replacements do not deviate from the spirit and scope of the technical solutions of the embodiments of the present invention, and should all be included in the scope of protection of the present invention.
Claims
1. A data tamper-proof method, characterized in that: The method comprises: Obtain the data to be protected; Obtaining the address of the read-only segment of the data to be protected; Based on the address of the read-only segment, write-protecting the read-only segment; The write-protecting the read-only segment based on the address of the read-only segment includes: Obtaining a target write instruction in the system where the data to be protected is located, wherein the target write instruction is an instruction for writing data to the data to be protected; Encapsulating the target write instruction, and obtaining to-be-written data information and a target memory address based on the encapsulated target write instruction; If the target memory address belongs to the address of the read-only segment, an error message is returned and stack backtrace information is output, wherein the stack backtrace information includes function call relationships, and the function call relationships in the stack backtrace information include function call relationships corresponding to functions that have illegal write requests to memory data.
2. The data tamper-proof method according to claim 1, characterized in that: After obtaining the data information to be written and the target memory address based on the encapsulated target write instruction, the method further includes: if the target memory address does not belong to the address of the read-only segment, writing the data information to be written to the target memory address.
3. The data tamper-proof method according to claim 1 or 2, characterized in that: The data to be protected is data of a program to be protected, and obtaining the address of the read-only segment of the data to be protected includes: Obtaining an executable file generated by compiling the program to be protected; An address of the read-only segment is obtained based on the executable file.
4. The data tamper-proof method according to claim 3, characterized in that: The executable file includes a program segment, a read-only data segment, a data segment, a BSS segment, a heap and a stack.
5. The data tamper-proof method according to claim 4, characterized in that: The read-only segment includes the program segment and the read-only data segment, and obtaining the address of the read-only segment based on the executable file includes: Memory addresses corresponding to the program segment and the read-only data segment in the executable file are obtained as the address of the read-only segment.
6. A data tamper-proof device, characterized in that: The device comprises: A module for acquiring data to be protected, used for acquiring data to be protected; A read-only address acquisition module, used to acquire the address of the read-only segment of the data to be protected; A protection module, configured to write-protect the read-only segment based on the address of the read-only segment; The protection module is specifically used to: obtain a target write instruction in the system where the data to be protected is located, wherein the target write instruction is an instruction for writing data to the data to be protected; encapsulate the target write instruction, and obtain the data information to be written and the target memory address based on the encapsulated target write instruction; if the target memory address belongs to the address of the read-only segment, return an error message and output stack backtrace information, wherein the stack backtrace information includes function call relationships, and the function call relationships in the stack backtrace information include function call relationships corresponding to functions that have illegal write requests for memory data.
7. An intelligent terminal, characterized in that: The smart terminal includes: a memory, a processor, and a data tamper-proofing program stored in the memory and runnable on the processor. When the data tamper-proofing program is executed by the processor, the steps of the data tamper-proofing method according to any one of claims 1 to 5 are implemented.
8. A computer-readable storage medium, characterized in that The computer-readable storage medium stores a data tamper-proofing program, and when the data tamper-proofing program is executed by a processor, the steps of the data tamper-proofing method according to any one of claims 1 to 5 are implemented.
Citation Information
Patent Citations
Application loading method and device
CN105426223A
Data access method and device
CN112115521A