A method for switching execution environment and related device
By achieving a method of switching the execution environment in ATF, ATF judges and switch to the legal entry address of Tee when receiving the switching request of Ree, solving the problem of weak ATF security defense capabilities that causes the hacker to attack Tee. Effective protection of Tee resources.
Patent Information
- Application Number
- CN202011270083.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-11-13
- Publication Date
- 2025-01-28
- Estimated Expiration
- 2040-11-13
AI Technical Summary
In the prior art, ARM trusted firmware (ATF) is easily targeted by hackers due to its weak security defense capabilities, and thus endangering the TEE in TrustZone.
By achieving a method of switching the execution environment in ATF, ATF writes the first virtual address to the Tee's return address register when receiving a switching request for Ree, and determines whether the address is within the preset legal address range. If within the legal scope, the ATF reads the address from the Tee's return address register and switch to the Tee, so that the Tee is executed only from the code at a specific location.
Effectively prevent hackers from malicious access Tee through ATF, improve ATF's security defense capabilities, and ensure the confidentiality, integrity and access authority of Tee resources and data.
Smart Images

Figure CN114490448B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of security technology, and in particular to a method for switching execution environments and related devices thereof. Background Art
[0002] With the rapid development of Internet technology and smart terminals, the requirements for the security level of the operating environment of smart terminals are getting higher and higher. TrustZone has gradually become the standard configuration of smart terminals. TrustZone is a security technology proposed by ARM. TrustZone introduces a monitoring mode, which is divided into a trusted execution environment (TEE) and a rich execution environment (REE) above the monitoring mode. TEE and REE each have a certain execution space. TEE has a higher security level than REE, and the software and hardware resources that TEE can access are separated from REE. REE can run rich operating systems (Rich OS) such as Android and Linux, as well as upper-level ordinary applications, while TEE can provide a secure execution environment for trusted applications (TA), while also protecting the confidentiality, integrity and access rights of TA's resources and data.
[0003] Generally, there is a layer of components between TEE and REE, such as ARM trusted firmware (ATF). Under the premise of ensuring that REE and TEE are isolated, ATF is usually responsible for the communication and switching between TEE and REE. ATF has the highest processor execution privileges and can access the sensitive code and key assets of TEE. Moreover, ATF is not a component designed for security and its security defense capabilities are weak. Therefore, ATF often becomes the target of hackers. Hackers break into ATF and further break into TEE to obtain users' key assets.
[0004] Specifically, when ATF needs to implement switching between REE and TEE, ATF can write the entry address of TEE (i.e., the virtual address used to enter TEE) into the return address register of TEE, then read the address from the return address register of TEE, and switch to TEE based on the address, so that TEE starts executing from the code corresponding to the address. In the aforementioned switching process, if ATF is hacked, the hacker can write an illegal entry address to the return address register of TEE through ATF, so that ATF can execute at any code in TEE based on the illegal entry address. For example, after switching to TEE, TEE skips some key security verification steps and directly returns the result of successful verification to REE, etc. Therefore, how to prevent hackers from maliciously accessing TEE through ATF has become a technical problem that needs to be solved urgently. Summary of the invention
[0005] The embodiments of the present application provide a method for switching execution environments and related devices, which can effectively prevent hackers from maliciously accessing TEE through ATF.
[0006] A first aspect of an embodiment of the present application provides a method for switching an execution environment, the method comprising:
[0007] When REE needs to enter TEE, it can initiate a switching request to ATF. After ATF receives the switching request from REE, it can determine that it is necessary to switch between REE and TEE. Then, ATF writes the first virtual address (i.e., the entry address of TEE) to the return address register of TEE based on the switching request. Next, ATF determines whether the first virtual address in the register is within the preset legal address range. For example, if the preset legal address range contains only one legal entry address, it is equivalent to setting only one entry address for entering TEE, so ATF can only switch to TEE based on the unique entry address. For another example, if the preset legal address range contains multiple legal entry addresses, ATF can switch to TEE based on one of the addresses in this part of the entry addresses. Finally, if ATF determines that the first virtual address in the return address register of TEE is within the preset legal address range, the first virtual address is read from the return address register of TEE, and the first virtual address is switched to TEE based on the first virtual address, so that TEE starts executing from the code corresponding to the first virtual address, thereby realizing the switching between REE and TEE.
[0008] It can be seen from the above method that after receiving the switching request from REE, ATF can write the first virtual address to the return address register of TEE. After determining that the first virtual address in the register is within the preset legal address range, ATF reads the first virtual address from the return address register of TEE and switches to TEE based on the first virtual address. Since the first virtual address is within the preset legal address range, that is, the first virtual address can only take values within the preset legal address range, ATF switches to TEE based on the restricted first virtual address, which allows TEE to execute only from the code at a specific location, which is equivalent to ATF only being able to enter TEE from one or some specific entrances, which can effectively prevent hackers from maliciously accessing TEE through ATF.
[0009] In one possible implementation, the method further includes: writing the base address of the TEE's page table to the TEE's page table base address register; if the ATF determines that the first virtual address in the TEE's return address register is within a preset legal address range, then reading the first virtual address from the TEE's return address register. Specifically includes: if the ATF determines that the first virtual address and the base address of the TEE's page table are both within a preset legal address range in the TEE's return address register and the TEE's page table base address register, then reading the first virtual address from the TEE's return address register. In the aforementioned implementation, after receiving the switching request, the ATF may write the first virtual address and the base address of the TEE's page table to the TEE's return address register and the TEE's page table base address register, respectively. Before writing the address, ATF may be attacked by hackers, thereby writing illegal addresses to the two registers (writing an illegal address to the return address register will result in not entering TEE through the legal entry address, and writing an illegal address to the TEE's page table base address register will cause TEE to use an illegal page table), allowing hackers to maliciously access TEE through ATF. Therefore, after writing addresses to the two registers respectively, ATF can detect the values in the two registers. If the first virtual address and the base address of the TEE's page table are both within the preset legal address range, that is, both are legal addresses, it means that ATF has not been attacked by hackers, so ATF can obtain the first virtual address from the TEE's return address register to safely switch to TEE based on the first virtual address.
[0010] In a possible implementation, ATF switches to TEE based on the first virtual address specifically including: ATF reads the base address of the TEE's page table from the TEE's page table base address register, and obtains the TEE's page table directory based on the base address of the TEE's page table, the TEE's page table directory records a one-to-one correspondence between multiple TEE virtual addresses and multiple TEE's page table entries' physical addresses, for any TEE's page table entry, the page table entry records a physical address of a TEE's data segment, or the page table entry records a physical address of a TEE's code segment, or the page table entry does not record content; ATF determines the physical address of a first page table entry corresponding to the first virtual address in the TEE's page table directory, the first virtual address being one of the multiple TEE virtual addresses; ATF obtains the first page table entry based on the physical address of the first page table entry, and determines the physical address recorded in the first page table entry; ATF switches to TEE based on the physical address recorded in the first page table entry. In the above implementation, since the base address of the TEE's page table is also a legal address, ATF can obtain the correct TEE's page directory based on the address, and then obtain the physical address of the first page table entry record in the TEE's page directory based on the first virtual address. The physical address points to the code at the entry location of the TEE. Then ATF can switch to TEE based on the physical address, so that TEE starts executing from the code corresponding to the physical address.
[0011] In a possible implementation, before the ATF receives the switching request from the REE, the method further includes: the ATF shuts down the memory management unit (MMU); the ATF reads the base address of the ATF page table from the ATF page table base address register, and obtains the ATF page table directory based on the base address of the ATF page table, the ATF page table directory records a one-to-one correspondence between multiple ATF virtual addresses and multiple ATF page table entries physical addresses, for any ATF page table entry, the page table entry records an ATF data segment and the access rights of the data segment, or the page table entry records the physical address of an ATF code segment and the access rights of the code segment, or the page table entry records the access rights of multiple ATF page table entries, or the page table entry does not record content; the ATF obtains the second virtual address, and determines the physical address of the second page table entry corresponding to the second virtual address in the ATF page table directory. , the second virtual address is one of the virtual addresses of multiple ATFs, and the second page table entry records the access rights of multiple ATF page table entries; ATF obtains the second page table entry based on the physical address of the second page table entry, and in the second page table entry, modifies the access rights of multiple ATF page table entries to read-only; ATF obtains the third virtual address, and determines the physical address of the third page table entry corresponding to the third virtual address in the page table directory of ATF, and the third virtual address is one of the virtual addresses of multiple ATFs; ATF obtains the third page table entry based on the physical address of the third page table entry, and sets the access right of the target code segment recorded in the third page table entry to non-executable, and the target code segment is used to indicate the modification operation of the page table base address register of ATF and the shutdown operation of MMU; ATF turns on MMU. In the prior art, if ATF is attacked by hackers, hackers can modify the physical address recorded in the ATF page table entry through ATF, so that the ATF page table entry is mapped to the TEE memory (if the physical address recorded in the ATF page table entry is not maliciously modified, the target memory mapped by the ATF page table entry is usually the ATF memory), thereby stealing or tampering with the TEE data and code. Furthermore, hackers can also write illegal base addresses to the ATF page table base address register through ATF, so that ATF obtains the page table directory and page table entries maliciously created by hackers. Since the physical address recorded in the page table entry created by hackers usually points to the TEE memory, hackers can also steal or tamper with the TEE data and code through this attack method. In the above implementation method, ATF can first turn off the MMU, then modify the access rights of all ATF page table entries to read-only, and modify the code of sensitive instructions (including the modification operation of the ATF page table address register and the shutdown operation of the MMU) to be non-executable, and finally reopen the MMU.Since the access rights of ATF page table entries are set to read-only, and MMU cannot be turned off (because when MMU is turned on, forcibly modifying the access rights of ATF page table entries and sensitive code will cause the system to crash), even if ATF is attacked by hackers, hackers cannot modify ATF page table entries through ATF to access TEE memory. Furthermore, since the values in ATF page table address registers cannot be modified, hackers cannot write illegal base addresses to ATF page table address registers to map TEE memory using illegal page tables and access TEE memory, which can further prevent hackers from maliciously accessing TEE through ATF.
[0012] In one possible implementation, if the switching request is used to instruct ATF to add a correspondence between a fourth virtual address and a target physical address, the fourth virtual address is one of multiple ATF virtual addresses, and the target physical address points to a new shared memory area between ATF and TEE, then after ATF switches to TEE based on the first virtual address, the method also includes: TEE receives a switching request from ATF; TEE determines, based on the switching request, a physical address of a fourth page table entry corresponding to the fourth virtual address in a page table directory of ATF, and the page table directory of ATF records a one-to-one correspondence between multiple ATF virtual addresses and multiple ATF page table entries; TEE obtains a fifth virtual address, and the fifth virtual address is one of multiple TEE virtual addresses; TEE determines the physical address of a fifth page table entry corresponding to the fifth virtual address in the page table directory of TEE, and obtains the fifth page table entry based on the physical address of the fifth page table entry; TEE modifies the content recorded in the fifth page table entry to the physical address of the fourth page table entry; TEE obtains the fourth page table entry based on the fifth virtual address; TEE modifies the content recorded in the fourth page table entry to the target physical address. In the above-mentioned implementation, if the switching request sent by REE in certain special business scenarios is used to instruct ATF to expand its own memory access rights (usually instructing ATF to expand the access rights of the new shared memory), that is, to instruct ATF to add a correspondence between the fourth virtual address (one of the multiple ATF virtual addresses) and the target physical address (pointing to the new shared memory area), since the ATF page table entry has been locked (the ATF page table entry can only be read), ATF cannot modify the fourth page table entry corresponding to the fourth virtual address, but can map the fifth virtual address (one of the multiple TEE virtual addresses) to the fourth page table entry through TEE, so TEE can obtain the fourth page table entry based on the fifth virtual address, thereby modifying the content recorded in the fourth page table entry to the target physical address. In this way, a correspondence between the fourth virtual address and the target physical address is established, making the solution more comprehensive.
[0013] It should be noted that the method executed by TEE can also be independently used as another aspect of the method provided by this application.
[0014] In one possible implementation, before ATF writes the first virtual address to the return address register of TEE, the method further includes: ATF modifies the value of the system control register so that ATF is in an interrupt shielding state. In the aforementioned implementation, ATF may enter the interrupt shielding state before writing addresses to the two registers to prevent subsequent steps from being interrupted by interrupt events.
[0015] In one possible implementation, after the TEE modifies the content recorded in the fourth page table entry to the target physical address, the method further includes: the TEE deletes the content recorded in the fifth page table entry. In the aforementioned implementation, after the TEE establishes the correspondence between the fourth virtual address and the target physical address, the TEE may cancel the correspondence between the fifth virtual address and the fourth page table entry.
[0016] It should be understood that the above method can be implemented by one module or by multiple modules working together.
[0017] A second aspect of an embodiment of the present application provides a method for switching an execution environment, the method comprising: before ATF receives a switching request from REE, the method further comprising: ATF shuts down a memory management unit (MMU); ATF reads the base address of the ATF page table from the ATF page table base address register, and obtains the ATF page table directory based on the base address of the ATF page table, the ATF page table directory records a one-to-one correspondence between multiple ATF virtual addresses and multiple ATF page table entries physical addresses, for any ATF page table entry, the page table entry records an ATF data segment and access rights to the data segment, or the page table entry records a physical address of an ATF code segment and access rights to the code segment, or the page table entry records access rights to multiple ATF page table entries, or the page table entry does not record content; ATF obtains a second virtual address, and determines a second page table entry corresponding to the second virtual address in the ATF page table directory. The second virtual address is one of the multiple ATF virtual addresses, and the second page table entry records the access rights of the multiple ATF page table entries; ATF obtains the second page table entry based on the physical address of the second page table entry, and in the second page table entry, modifies the access rights of the multiple ATF page table entries to read-only; ATF obtains the third virtual address, and determines the physical address of the third page table entry corresponding to the third virtual address in the ATF page table directory, and the third virtual address is one of the multiple ATF virtual addresses; ATF obtains the third page table entry based on the physical address of the third page table entry, and sets the access right of the target code segment recorded in the third page table entry to non-executable, and the target code segment is used to indicate the modification operation of the ATF page table base address register and the shutdown operation of the MMU; ATF turns on the MMU.
[0018] In the prior art, if ATF is attacked by hackers, hackers can modify the physical address recorded in the ATF page table entry through ATF, so that the ATF page table entry is mapped to the TEE memory (if the physical address recorded in the ATF page table entry is not maliciously modified, the target memory mapped by the ATF page table entry is usually the ATF memory), thereby stealing or tampering with the TEE data and code. Furthermore, hackers can also write illegal base addresses to the ATF page table base address register through ATF, so that ATF obtains the page table directory and page table entries maliciously created by hackers. Since the physical addresses recorded in the page table entries created by hackers usually point to the TEE memory, hackers can also steal or tamper with the TEE data and code through this attack method.
[0019] From the above method, it can be seen that ATF can first turn off the MMU, then modify the access rights of all ATF page table entries to read-only, and modify the code of sensitive instructions (including the modification operation of the ATF page table address register and the shutdown operation of the MMU) to be non-executable, and finally reopen the MMU. Since the access rights of the ATF page table entries are set to read-only, and the MMU can no longer be turned off (because when the MMU is turned on, if the access rights of the ATF page table entries and the access rights of sensitive code are forcibly modified, the system will crash), even if the ATF is attacked by hackers, the hackers cannot modify the ATF page table entries through ATF to access the TEE's memory. Furthermore, since the values in the ATF page table address register cannot be modified, hackers cannot use illegal base addresses to write to the ATF page table address register, thereby using illegal page tables to map the TEE's memory and access the TEE's memory, which can further prevent hackers from maliciously accessing the TEE through ATF.
[0020] A third aspect of an embodiment of the present application provides a device for switching an execution environment, the device comprising REE, ATF and TEE, the ATF being used to: receive a switching request from REE; write a first virtual address to a return address register of TEE based on the switching request; if ATF determines that the first virtual address in the return address register of TEE is within a preset legal address range, read the first virtual address from the return address register of TEE; and switch to TEE based on the first virtual address.
[0021] It can be seen from the above device that after receiving the switching request from REE, ATF can write the first virtual address to the return address register of TEE. After determining that the first virtual address in the register is within the preset legal address range, ATF reads the first virtual address from the return address register of TEE and switches to TEE based on the first virtual address. Since the first virtual address is within the preset legal address range, that is, the first virtual address can only take values within the preset legal address range, ATF switches to TEE based on the restricted first virtual address, which allows TEE to execute only from the code at a specific location, which is equivalent to ATF only being able to enter TEE from one or some specific entrances, which can effectively prevent hackers from maliciously accessing TEE through ATF.
[0022] In one possible implementation, ATF is also used to write the base address of the TEE's page table into the TEE's page table base address register; ATF is specifically used to read the first virtual address from the TEE's return address register if it is determined in the TEE's return address register and the TEE's page table base address register that the first virtual address and the base address of the TEE's page table are both within a preset legal address range.
[0023] In a possible implementation, ATF is specifically used to: read the base address of the TEE's page table from the TEE's page table base address register, and obtain the TEE's page table directory based on the base address of the TEE's page table, the TEE's page table directory records a one-to-one correspondence between multiple TEE virtual addresses and multiple TEE page table entries' physical addresses, for any TEE's page table entry, the page table entry records a physical address of a TEE's data segment, or the page table entry records a physical address of a TEE's code segment, or the page table entry does not record content; in the TEE's page table directory, determine the physical address of a first page table entry corresponding to a first virtual address, the first virtual address being one of the virtual addresses of multiple TEEs; obtain the first page table entry based on the physical address of the first page table entry, and determine the physical address recorded in the first page table entry; switch to the TEE based on the physical address recorded in the first page table entry.
[0024] In one possible implementation, if the switching request is used to instruct ATF to add a correspondence between a fourth virtual address and a target physical address, the fourth virtual address is one of multiple ATF virtual addresses, and the target physical address points to a new shared memory area between ATF and TEE, then TEE is used to: receive a switching request from ATF; based on the switching request, determine the physical address of a fourth page table entry corresponding to the fourth virtual address in the page table directory of ATF, and the page table directory of ATF records a one-to-one correspondence between multiple ATF virtual addresses and multiple ATF page table entries; obtain the fifth virtual address, and determine the physical address of a fifth page table entry corresponding to the fifth virtual address in the page table directory of TEE, and the fifth virtual address is one of multiple TEE virtual addresses; obtain the fifth page table entry based on the physical address of the fifth page table entry, and modify the content recorded in the fifth page table entry to the physical address of the fourth page table entry; obtain the fourth page table entry based on the fifth virtual address; modify the content recorded in the fourth page table entry to the target physical address.
[0025] In a possible implementation, ATF is also used to modify the value of the system control register so that ATF is in an interrupt masking state.
[0026] In a possible implementation, TEE is also used to delete the content recorded in the fifth page table entry.
[0027] The fourth aspect of the embodiment of the present application provides a device for switching the execution environment, the device includes ATF, and the ATF is used to: shut down the MMU; read the base address of the ATF page table from the ATF page table base address register, and obtain the ATF page table directory based on the base address of the ATF page table, the ATF page table directory records a one-to-one correspondence between multiple ATF virtual addresses and multiple ATF page table entries The physical address relationship, for any ATF page table entry, the page table entry records an ATF data segment and the access rights of the data segment, or, the page table entry records the physical address of an ATF code segment and the access rights of the code segment, or, the page table entry records the access rights of the multiple ATF page table entries, or, the page table entry does not record content; obtain a second virtual address, and in the ATF page table directory Determine the physical address of a second page table entry corresponding to the second virtual address, the second virtual address is one of the virtual addresses of multiple ATFs, and the second page table entry records the access rights of multiple ATF page table entries; obtain the second page table entry based on the physical address of the second page table entry, and in the second page table entry, modify the access rights of multiple ATF page table entries to read-only; obtain the third virtual address, and determine the physical address of a third page table entry corresponding to the third virtual address in the page table directory of the ATF, the third virtual address is one of the virtual addresses of multiple ATFs; obtain the third page table entry based on the physical address of the third page table entry, and set the access right of a target code segment recorded in the third page table entry to non-executable, the target code segment is used to indicate the modification operation of the page table base address register of the ATF and the shutdown operation of the MMU; turn on the MMU.
[0028] It can be seen from the above device that ATF can first shut down MMU, then modify the access rights of all ATF page table entries to read-only, and modify the code of sensitive instructions (including the modification operation of ATF's page table address register and the shutdown operation of MMU) to be non-executable, and finally reopen MMU. Since the access rights of ATF's page table entries are set to read-only, and MMU can no longer be shut down (because when MMU is in the open state, if the access rights of ATF's page table entries and the access rights of sensitive code are forcibly modified, the system will crash), even if ATF is attacked by hackers, hackers cannot modify ATF's page table entries through ATF to access TEE's memory. Furthermore, since the value in ATF's page table address register cannot be modified, hackers cannot use illegal base addresses to write to ATF's page table address register, thereby using illegal page tables to map TEE's memory and access TEE's memory, which can further prevent hackers from maliciously accessing TEE through ATF.
[0029] The fifth aspect of an embodiment of the present application provides a terminal device, which includes: a processor and a memory; the memory is used to store computer execution instructions; the processor is used to execute the computer execution instructions stored in the memory, so that the terminal device implements the method described in the first aspect, any possible implementation method of the first aspect, or the second aspect.
[0030] A sixth aspect of an embodiment of the present application provides a computer storage medium, including computer-readable instructions. When the computer-readable instructions are executed, the method described in the first aspect, any possible implementation method of the first aspect, or the second aspect is implemented.
[0031] A seventh aspect of the embodiments of the present application provides a computer program product comprising instructions, which, when executed on a computer, enables the computer to execute the method as described in the first aspect, any possible implementation of the first aspect, or the second aspect. It should be understood that the method executed by the TEE can be another computer program product.
[0032] An eighth aspect of an embodiment of the present application provides a chip system, which includes one or more processors and a storage medium, wherein the storage medium stores computer-readable instructions. When the one or more processors read the computer-readable instructions, the chip system is prompted to implement the aforementioned first aspect, second aspect, or any one of the implementation methods of each.
[0033] In an embodiment of the present application, after receiving the switching request from REE, ATF can write the first virtual address to the return address register of TEE. After determining that the first virtual address in the register is within the preset legal address range, ATF reads the first virtual address from the return address register of TEE and switches to TEE based on the first virtual address. Since the first virtual address is within the preset legal address range, that is, the first virtual address can only be taken within the preset legal address range, ATF switches to TEE based on the restricted first virtual address, which allows TEE to execute only from the code at a specific location, which is equivalent to ATF only being able to enter TEE from one or some specific entrances, which can effectively prevent hackers from maliciously accessing TEE through ATF. BRIEF DESCRIPTION OF THE DRAWINGS
[0034] Figure 1 A schematic diagram of a software structure of a terminal device provided in an embodiment of the present application;
[0035] Figure 2 A flowchart of a method for switching execution environments provided in an embodiment of the present application;
[0036] Figure 3 Another schematic diagram of a flow chart of a method for switching execution environments provided in an embodiment of the present application;
[0037] Figure 4 Another flowchart of the method for switching execution environments provided in an embodiment of the present application;
[0038] Figure 5 A schematic diagram of a structure of an apparatus for switching execution environments provided in an embodiment of the present application;
[0039] Figure 6 A schematic diagram of the structure of a terminal device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0040] The embodiments of the present application provide a method for switching execution environments and related devices, which can effectively prevent hackers from maliciously accessing TEE through ATF.
[0041] The terms "first", "second", etc. in the specification and claims of this application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments described here can be implemented in an order other than that illustrated or described here. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or modules is not necessarily limited to those steps or modules clearly listed, but may include other steps or modules that are not clearly listed or inherent to these processes, methods, products or devices. The naming or numbering of steps in this application does not mean that the steps in the method flow must be executed in the time / logical sequence indicated by the naming or numbering. The process steps that have been named or numbered can change the execution order according to the technical purpose to be achieved, as long as the same or similar technical effects can be achieved. The division of modules in this application is a logical division. There may be other division methods when it is implemented in actual applications. For example, multiple modules can be combined or integrated into another system, or some features can be ignored or not executed. In addition, the mutual coupling or direct coupling or communication connection shown or discussed can be through some ports, and the indirect coupling or communication connection between modules can be electrical or other similar forms, which are not limited in this application. In addition, the modules or submodules described as separate components may or may not be physically separated, may or may not be physical modules, or may be distributed in multiple circuit modules, and some or all of the modules may be selected according to actual needs to achieve the purpose of the present application.
[0042] The embodiments of the present application may be implemented through a terminal device, and the terminal device may be a smart phone, a personal digital assistant (PDA), a tablet computer, or other devices. The following is a brief introduction to the software architecture of the terminal device. Figure 1 A software structure diagram of a terminal device provided in an embodiment of the present application. Figure 1 As shown, a non-secure operating environment and a secure operating environment can be deployed on the terminal device, wherein the non-secure operating environment is the REE on the terminal device, and the REE can run the currently commonly used rich operating system (i.e. Figure 1 The secure operating environment is the TEE on the terminal device, which can run a secure operating system with simple functions, small code volume, closed and human-controlled audit (i.e. Figure 1 The client application (CA) with low security requirements runs in REE, and the security application TA with high security requirements runs in TEE, which can provide security services for the CA deployed on Rich OS. For example, when a user encounters sensitive operations such as electronic payment during the use of CA in the terminal device, CA will send a request to TA. After receiving the request, TA will perform verification operations related to payment (for example, verify whether the password is correct) and return the verification result to CA. CA will continue to execute subsequent steps to prevent data (for example, the above password) from being leaked.
[0043] Generally, the software and hardware resources accessible by TEE are isolated from REE. TEE can provide security services for REE, but TEE and REE are securely isolated. Rich OS in REE and CA on it cannot directly access TEE, so TEE builds a secure operating environment isolated from REE, thereby providing a secure execution environment for TA. There is a layer of underlying firmware between REE and TEE, which is usually presented in the form of ATF. ATF can realize communication and switching between REE and TEE under the premise of ensuring the isolation of REE and TEE. ATF has the highest processor execution authority and can access the memory of REE and TEE, so the potential threat of ATF being attacked is very large, and ATF is not a component specially designed for security. The security mechanism it can implement is less than the security mechanism that TEE can implement, so the security defense capability of ATF is weak. Therefore, ATF often becomes the target of hackers. Hackers break through ATF and further attack TEE to steal users' key assets.
[0044] Specifically, when ATF needs to switch between REE and TEE, ATF can write the entry address of TEE into the return address register of TEE, then read the address from the return address register of TEE, and switch to TEE based on the address, so that TEE starts to execute from the code corresponding to the address. In the above switching process, if ATF is hacked, the hacker can write an illegal entry address to the return address register of TEE through ATF, so that ATF will execute at any code of TEE based on the illegal entry address. For example, after switching to TEE, TEE skips some key security verification steps and directly returns the result of successful verification to REE, etc.
[0045] Furthermore, under normal circumstances, ATF can access ATF's memory (used to store ATF's data and code) based on ATF's page table, that is, ATF's page table can be mapped to ATF's memory. Specifically, in ATF's page table, the physical address recorded in ATF's page table entry points to ATF's memory. If ATF is attacked by hackers, hackers can maliciously tamper with the physical address recorded in ATF's page table entry through ATF, so that the address recorded in ATF's page table entry points to TEE's memory (used to store TEE's data and code). Therefore, hackers can access TEE's memory based on the modified ATF page table through ATF, thereby stealing or tampering with TEE's data and code.
[0046] Furthermore, hackers can also use ATF to write illegal base addresses to the ATF page table base address register, so that ATF reads the illegal base address from the ATF page table base address register and obtains the newly created page table based on the base address. Since the physical address recorded in the page table entry in the maliciously created page table usually points to the TEE's memory, hackers can also steal or tamper with the TEE's data and code through this attack method.
[0047] Therefore, in order to prevent hackers from maliciously accessing TEE through ATF, the embodiment of the present application provides a method for switching the execution environment, which enables ATF to implement a new security mechanism and improve the security defense capabilities of ATF. The new security mechanism includes the process of the initialization phase and the process of business calls. The process of the initialization phase is first introduced below. It should be noted that the REE, ATF and TEE mentioned in the embodiment of the present application are all software concepts, and all three are operating modes of the processor. Therefore, in physical implementation, the steps performed by REE, ATF and TEE can be regarded as being executed by the processor, and will not be repeated later. Figure 2 A flow chart of a method for switching execution environments provided in an embodiment of the present application is shown as follows: Figure 2 As shown, the method includes:
[0048] 201. ATF turns off MMU.
[0049] When the processor starts, ATF can be initialized. The ATF initialization process includes setting the virtual address range of ATF, building the ATF page table, writing the base address of the ATF page table to the ATF page table base address register, turning on MMU, setting the access rights of the ATF data segment and code segment, etc.
[0050] After ATF completes initialization, it turns off the MMU. When the MMU is turned on, if ATF forcibly modifies the ATF page table, for example, modifies the access rights of the ATF page table entries, modifies the access rights of the ATF data segment or code segment, etc., it will cause the system to crash. Therefore, in order to smoothly execute the subsequent steps, ATF needs to turn off the MMU first.
[0051] 202. ATF reads the base address of the ATF page table from the ATF page table base address register, and obtains the ATF page table directory based on the base address of the ATF page table. The ATF page table directory records a one-to-one correspondence between multiple ATF virtual addresses and multiple physical addresses of ATF page table entries. For any ATF page table entry, the page table entry records an ATF data segment and the access rights of the data segment, or the page table entry records the physical address of an ATF code segment and the access rights of the code segment, or the page table entry records the access rights of multiple ATF page table entries, or the page table entry does not record any content.
[0052] In this embodiment, the memory of ATF is divided into multiple memory areas, and a memory area of ATF can be used to store the page table directory of ATF, the page table entry of ATF, the data segment of ATF or the code segment of ATF, etc., and a memory area of ATF has an exclusive physical address. ATF can find the corresponding memory area based on the physical address, so as to operate the object stored in the memory area, such as reading, writing or executing, etc. It should be noted that a memory area of ATF can be an exclusive memory area of ATF (exclusively for ATF use), or a shared memory area between ATF and TEE (available for use with ATF and TEE), or a shared memory area between ATF and REE (available for use with ATF and REE).
[0053] It is understandable that, since a certain physical address points to a certain memory area, and the memory area stores a corresponding object, the "physical address of a certain object" described below can be regarded as a physical address pointing to the memory area where the object is stored. For example, the physical address of the ATF page table directory points to the memory area where the ATF page table directory is stored, and the physical address of the ATF code segment points to the memory area where the ATF code segment is stored, etc., which will not be repeated later.
[0054] The page table of ATF is a two-level structure, including the ATF page table directory and multiple ATF page table entries. Among them, the physical address of the ATF page table directory is the base address of the ATF page table. The ATF page table directory records the one-to-one correspondence between multiple ATF virtual addresses and multiple ATF page table entries. Among them, the virtual address of each ATF is located in the virtual address range allocated to the ATF, and the physical address of each ATF page table entry points to a memory area for storing the page table entry.
[0055] Among the above-mentioned multiple ATF page table entries, there are two categories of page table entries, one of which is used page table entries and the other is unused page table entries. When a page table entry of an ATF is a used page table entry, the page table entry records the physical address of an ATF data segment and the access rights of the data segment, or the page table entry records the physical address of an ATF code segment and the access rights of the code segment, or the page table entry records the access rights of multiple ATF page table entries. When a page table entry of an ATF is an unused page table entry, the page table entry does not record anything, that is, the content recorded in the page table entry is empty. In addition, the access rights of the ATF page table entry include read and write, the access rights of the ATF data segment include read and write, and the access rights of the ATF code segment include read, write, and execute.
[0056] In summary, based on the ATF page table, the following correspondence can be achieved: ATF virtual address → ATF page table entry physical address → ATF data segment physical address or ATF code segment physical address. The aforementioned correspondence can also be equivalent to the following correspondence: ATF virtual address → ATF page table entry → ATF data segment or ATF code segment. Therefore, ATF can obtain the corresponding ATF data segment or ATF code segment based on the ATF virtual address.
[0057] If ATF needs to modify the ATF page table, for example, to modify the ATF page table directory or to modify the ATF page table entry, the base address of the ATF page table can be read from the ATF page table base address register first, and then the ATF page table directory can be obtained from the memory area pointed to by the base address.
[0058] 203. ATF obtains the second virtual address and determines the physical address of the second page table entry corresponding to the second virtual address in the page table directory of ATF, where the second virtual address is one of the multiple ATF virtual addresses, and the second page table entry records the access rights of the multiple ATF page table entries.
[0059] 204. The ATF obtains the second page table entry based on the physical address of the second page table entry, and modifies the access rights of the plurality of ATF page table entries to read-only in the second page table entry.
[0060] After ATF obtains the page table directory of ATF, it can modify the access rights of the page table items of ATF. Specifically, ATF can obtain the second virtual address used to modify the access rights of the page table items of ATF, and determine the physical address of the second page table item corresponding to the second virtual address in the page table directory of ATF, and then obtain the second page table item from the memory area pointed to by the physical address of the second page table item. The second page table item records the access rights of all the page table items of ATF. It can be understood that in the page table of ATF, the second virtual address is one of the multiple virtual addresses of ATF, so the second page table item is also one of the multiple page table items of ATF.
[0061] Then, ATF can modify the access rights of each ATF page table entry to read-only in the second page table entry, so the content recorded in each ATF page table entry can only be read but not modified. For example, suppose that a page table entry of an ATF records the physical address of a code segment of an ATF and the access rights of the code segment. If the access rights of the page table entry of the ATF are set to read-only, then ATF can only read the content recorded in the page table entry, but cannot modify the content recorded in the page table entry, that is, it cannot modify the physical address recorded in the page table entry and the access rights of the code segment.
[0062] 205. The ATF obtains a third virtual address, and determines a physical address of a third page table entry corresponding to the third virtual address in a page table directory of the ATF, where the third virtual address is one of the virtual addresses of the plurality of ATFs.
[0063] 206. ATF obtains the third page table entry based on the physical address of the third page table entry, and sets the access permission of the target code segment recorded in the third page table entry to non-executable. The target code segment recorded in the third page table entry is used to indicate the modification operation of the page table base address register of ATF and the shutdown operation of MMU.
[0064] After ATF obtains the ATF page table directory, it can also modify the access rights of the target code segment used to indicate sensitive instructions, wherein the sensitive instructions include the modification operation of the ATF page table base address register and the shutdown operation of the MMU. Specifically, ATF can first obtain the third virtual address corresponding to the target code segment, and determine the physical address of the third page table entry corresponding to the third virtual address in the ATF page table directory, and then obtain the third page table entry from the memory area pointed to by the physical address of the third page table entry. It can be understood that in the ATF page table, the third virtual address is one of the multiple ATF virtual addresses, so the third page table entry is also one of the multiple ATF page table entries, and the target code is one of the multiple ATF code segments.
[0065] After ATF obtains the third page table entry, since the third page table entry records the physical address of the target code segment and the access rights of the target code segment, ATF can modify the access rights of the target code segment to non-executable in the third page table entry, that is, ATF will no longer execute sensitive instructions, that is, ATF can no longer write a new base address to the ATF page table base address register, and can no longer shut down the MMU.
[0066] It should be understood that there is no particular order in which step 203 and step 205 are executed. Step 203 and step 205 can be executed synchronously or asynchronously. For example, step 203 is executed before step 205, or step 203 is executed after step 205, and so on. There is no limitation here.
[0067] 207. ATF turns on MMU.
[0068] After ATF completes the modification of the ATF page table, the MMU can be reopened, thus completing the initialization phase.
[0069] In this embodiment, after ATF reopens MMU, even if ATF is hacked, since MMU can no longer be closed, hackers cannot modify the access rights of ATF's page table items through ATF, and then the access rights of ATF's page table items are locked to read-only, and hackers cannot modify the physical address recorded in ATF's page table items through ATF to make the physical address recorded in ATF's page table items point to the memory of TEE. Furthermore, hackers cannot write an illegal base address to ATF's page table base address register through ATF to make ATF obtain the page table maliciously created by hackers based on the illegal base address, and access TEE's memory based on the page table. Therefore, after ATF has performed a series of permission settings on ATF's page table, it can effectively improve its own security defense capabilities and prevent hackers from maliciously accessing TEE through ATF.
[0070] After completing the initialization phase, if REE initiates a security service call to TEE, the service call process can be entered. Figure 3 Another flowchart of the method for switching execution environments provided in an embodiment of the present application is as follows: Figure 2 As shown, the method includes:
[0071] 301. ATF receives a switching request from REE.
[0072] If REE initiates a call for a security service to TEE, that is, REE needs to enter TEE, then REE can send a switching request to ATF.
[0073] 302. Based on the switching request, the ATF writes the first virtual address to the return address register of the TEE, and writes the base address of the page table of the TEE to the page table base address register of the TEE.
[0074] After receiving the switching request from REE, ATF may determine that switching between REE and TEE is required, so ATF may write the first virtual address to the return address register of TEE and write the base address of the page table of TEE to the page table base address register of TEE.
[0075] In addition, before ATF writes the first virtual address and the base address of the TEE's page table to the TEE's return address register and the TEE's page table base address register respectively, ATF can modify the value of the system control register so that ATF is in an interrupt shielding state, thereby providing atomic protection for subsequent steps to prevent subsequent steps from being interrupted by interrupt events.
[0076] 303. If the ATF determines in the return address register of the TEE and the page table base address register of the TEE that the first virtual address and the base address of the page table of the TEE are both within the preset legal address range, the first virtual address is read from the return address register of the TEE.
[0077] Before ATF writes the first virtual address and the base address of the TEE's page table to the TEE's return address register and the TEE's page table base address register respectively, if the hacker breaks into ATF, the first virtual address that ATF is about to write can be modified to an illegal entry address, causing ATF to write an illegal entry address to the TEE's return address register, thereby jumping to any code in the TEE for execution based on the illegal address. Furthermore, even if the first virtual address is a legal entry address, ATF needs to use the correct TEE's page table in the process of jumping to TEE based on the first virtual address. If the base address of the TEE's page table written by ATF to the TEE's page table base address register is an illegal address, then ATF cannot obtain the correct TEE's page table, resulting in an unsuccessful jump. Furthermore, subsequent TEEs are also unable to obtain the correct TEE's page table, resulting in a series of security issues in the system.
[0078] It can be seen that ATF is not sure whether the address written to the return address register of TEE and the page table base address register of TEE is a legal address, so ATF needs to detect the addresses in the two registers to determine whether the addresses in the two registers are in the preset legal address range, so as to avoid the above situation. It should be noted that the preset legal address range contains at least one legal entry address and the only correct base address of the page table of TEE. For example, if the preset legal address range contains only one legal entry address, ATF can only switch to TEE based on the only entry address, so that TEE can only start execution from the only specified code. For another example, if the preset legal address range contains multiple TEE entry addresses, ATF can switch to TEE based on one of the multiple entry addresses, so that TEE can start execution from one of the multiple specified codes.
[0079] When ATF determines that the first virtual address and the base address of the TEE's page table are both within the preset legal address range, that is, ATF determines that the first virtual address is a legal entry address and the base address of the TEE's page table is also the correct address, ATF can read the first virtual address from the TEE's return address register.
[0080] 304. ATF reads the base address of the TEE's page table from the TEE's page table base address register, and obtains the TEE's page table directory based on the base address of the TEE's page table. The TEE's page table directory records a one-to-one correspondence between multiple TEE virtual addresses and multiple TEE page table entries' physical addresses. For any TEE's page table entry, the page table entry records a physical address of a TEE's data segment, or the page table entry records a physical address of a TEE's code segment, or the page table entry does not record any content.
[0081] After ATF reads the first virtual address, it reads the base address of the TEE's page table from the TEE's page table base address register, and obtains the TEE's page table directory based on the base address of the TEE's page table. It should be noted that for the relevant description of the TEE's page table, please refer to the relevant description of the ATF's page table in step 202, which will not be repeated here.
[0082] 305. The ATF determines, in the page table directory of the TEE, a physical address of a first page table entry corresponding to the first virtual address, where the first virtual address is one of the virtual addresses of the plurality of TEEs.
[0083] After ATF obtains the page table directory of the TEE, since the page table directory of the TEE records the one-to-one correspondence between the virtual addresses of multiple TEEs and the physical addresses of the page table entries of multiple TEEs, ATF can determine the physical address of the first page table entry corresponding to the first virtual address in the page table directory of the TEE. In the page table of the TEE, the first virtual address is one of the virtual addresses of multiple TEEs, so the first page table entry is one of the page table entries of multiple TEEs.
[0084] 306. The ATF obtains the first page table entry based on the physical address of the first page table entry, and determines the physical address recorded in the first page table entry.
[0085] After obtaining the physical address of the first page table entry, the ATF obtains the first page table entry from the memory area pointed to by the physical address of the first page table entry, and determines the physical address recorded in the first page table entry.
[0086] 307. ATF switches to TEE based on the physical address recorded in the first page table entry.
[0087] Since the first page table entry records the physical address of a code segment of a TEE, which is a pre-specified code segment, ATF can switch to TEE based on the physical address recorded in the first page table entry, so that TEE starts executing from the code segment corresponding to the physical address.
[0088] In this embodiment, after receiving the switching request from REE, ATF can obtain the first virtual address for entering TEE. Since the first virtual address is within the preset legal address range, that is, the first virtual address can only take values within the preset legal address range, ATF switches to TEE based on the restricted first virtual address, so that TEE can only start executing from the code at a specific location, which is equivalent to ATF can only enter TEE from one or some specific entrances, which can effectively prevent hackers from maliciously accessing TEE through ATF.
[0089] In addition, in the process of business calls, if the switching request sent by REE in certain special business scenarios is used to instruct ATF to expand its own memory access rights (for example, based on the memory area that ATF can currently access, ATF can additionally access a new shared memory area), since the permissions of ATF's page table entries are locked as read-only, it is impossible to expand ATF's memory access rights by modifying ATF's page table. In an embodiment of the present application, the page table of TEE can be modified through TEE to expand ATF's memory access rights. The following will introduce the process of TEE expanding ATF's memory access rights, Figure 4 Another flowchart of the method for switching execution environments provided in an embodiment of the present application is as follows: Figure 4 As shown, the method includes:
[0090] 401. TEE receives a switching request from ATF, where the switching request is used to instruct ATF to add a correspondence between a fourth virtual address and a target physical address, where the fourth virtual address is one of multiple ATF virtual addresses, and the target physical address points to a new shared memory area between ATF and TEE.
[0091] Before ATF switches to TEE, ATF can store the switching request in a shared memory area (allocated shared memory area) between ATF and TEE. After switching to TEE, TEE can obtain the switching request from the shared memory area. The switching request is used to instruct ATF to add the correspondence between the fourth virtual address and the target physical address in the page table of ATF, wherein the fourth virtual address is one of the virtual addresses of multiple ATFs, and the fourth page table entry corresponding to the fourth virtual address (one of the page table entries of multiple ATFs) is usually an unused page table entry, that is, the page table entry does not record anything. It is worth noting that the target physical address points to a new shared memory area between ATF and TEE (the shared memory area to be allocated), so TEE can determine the need to expand ATF's memory access rights based on the switching request.
[0092] 402. The TEE determines, based on the switch request, a physical address of a fourth page table entry corresponding to the fourth virtual address in the page table directory of the ATF.
[0093] After TEE determines that the memory access rights of ATF need to be expanded, it can parse the fourth virtual address from the switching request. Then, TEE obtains the page table directory of ATF and determines the physical address of the fourth page table entry corresponding to the fourth virtual address in the page table directory of ATF.
[0094] 403. TEE obtains a fifth virtual address, where the fifth virtual address is one of the virtual addresses of the plurality of TEEs.
[0095] After determining the physical address of the fourth page table entry, TEE can obtain the fifth virtual address, which is one of the virtual addresses of multiple TEEs, and the fifth page table entry corresponding to the fifth virtual address (one of the page table entries of multiple TEEs) is usually an unused page table entry, that is, the page table entry does not record anything.
[0096] 404. The TEE determines the physical address of the fifth page table entry corresponding to the fifth virtual address in the page table directory of the TEE, and obtains the fifth page table entry based on the physical address of the fifth page table entry.
[0097] After the TEE obtains the fifth virtual address, it determines the physical address of the fifth page table entry corresponding to the fifth virtual address in the page table directory of the TEE, and obtains the fifth page table entry from the storage area pointed to by the physical address of the fifth page table entry.
[0098] 405. TEE modifies the content recorded in the fifth page table entry to the physical address of the fourth page table entry.
[0099] In the page table of TEE, after obtaining the fifth page table entry, TEE can modify the fifth page table entry. Specifically, TEE can modify the content recorded in the fifth page table entry to the physical address of the fourth page table entry and the access rights of the fourth page table entry. Generally, the access rights of the fourth page table entry recorded in the fifth page table entry are read and write. At this time, TEE establishes the following correspondence in the page table of TEE: fifth virtual address → fifth page table entry → fourth page table entry.
[0100] 406. TEE obtains a fourth page table entry based on the fifth virtual address.
[0101] After TEE completes the modification of the fifth page table entry, it can obtain the fourth page table entry based on the fifth virtual address. Specifically, TEE first obtains the page table directory of TEE. Then, TEE determines the physical address of the fifth page table entry corresponding to the fifth virtual address in the page table directory of TEE, and obtains the fifth page table entry from the memory area pointed to by the physical address of the fifth page table entry. Then, TEE determines the physical address of the fourth page table entry recorded in the fifth page table entry, and obtains the fourth page table entry from the memory area pointed to by the physical address of the fourth page table entry.
[0102] 407. TEE modifies the content recorded in the fourth page table entry to the target physical address.
[0103] After obtaining the fourth page table entry, TEE modifies the content recorded in the fourth page table entry to the target physical address. At this time, TEE establishes the following correspondence in the TEE page table: fifth virtual address → fifth page table entry → fourth page table entry → new shared memory area, which is equivalent to updating the following correspondence in the ATF page table: fourth virtual address → fourth page table entry → new shared memory area (the original correspondence was: fourth virtual address → fourth page table entry).
[0104] 408. TEE deletes the content recorded in the fifth page table entry.
[0105] After updating the correspondence in the ATF page table, TEE can delete the content recorded in the fifth page table entry, that is, cancel the following correspondence: fifth virtual address → fifth page table entry → fourth page table entry → new shared memory area, so that the TEE page table is restored to the state before the modification, while the ATF page table still maintains the modified state. Therefore, TEE can act as a proxy to expand ATF's memory access rights instead of ATF, making the solution more comprehensive.
[0106] The above is a specific description of the method for switching the execution environment provided in the embodiment of the present application. The following will introduce the device for switching the execution environment provided in the embodiment of the present application. Figure 5 A schematic diagram of a structure of an apparatus for switching execution environments provided in an embodiment of the present application, such as Figure 5 As shown, the device includes REE, ATF and TEE, and ATF is used to: receive a switching request from REE; write a first virtual address to the return address register of TEE based on the switching request; if ATF determines that the first virtual address in the return address register of TEE is within a preset legal address range, read the first virtual address from the return address register of TEE; and switch to TEE based on the first virtual address.
[0107] In one possible implementation, ATF is also used to write the base address of the TEE's page table into the TEE's page table base address register; ATF is specifically used to read the first virtual address from the TEE's return address register if it is determined in the TEE's return address register and the TEE's page table base address register that the first virtual address and the base address of the TEE's page table are both within a preset legal address range.
[0108] In a possible implementation, ATF is specifically used to: read the base address of the TEE's page table from the TEE's page table base address register, and obtain the TEE's page table directory based on the base address of the TEE's page table, the TEE's page table directory records a one-to-one correspondence between multiple TEE virtual addresses and multiple TEE page table entries' physical addresses, for any TEE's page table entry, the page table entry records a physical address of a TEE's data segment, or the page table entry records a physical address of a TEE's code segment, or the page table entry does not record content; in the TEE's page table directory, determine the physical address of a first page table entry corresponding to a first virtual address, the first virtual address being one of the virtual addresses of multiple TEEs; obtain the first page table entry based on the physical address of the first page table entry, and determine the physical address recorded in the first page table entry; switch to the TEE based on the physical address recorded in the first page table entry.
[0109] In one possible implementation, if the switching request is used to instruct ATF to add a correspondence between a fourth virtual address and a target physical address, the fourth virtual address is one of multiple ATF virtual addresses, and the target physical address points to a new shared memory area between ATF and TEE, then TEE is used to: receive a switching request from ATF; based on the switching request, determine the physical address of a fourth page table entry corresponding to the fourth virtual address in the page table directory of ATF; obtain the fifth virtual address, and determine the physical address of a fifth page table entry corresponding to the fifth virtual address in the page table directory of TEE, the fifth virtual address is one of multiple TEE virtual addresses; obtain the fifth page table entry based on the physical address of the fifth page table entry, and modify the content recorded in the fifth page table entry to the physical address of the fourth page table entry; obtain the fourth page table entry based on the fifth virtual address; modify the content recorded in the fourth page table entry to the target physical address.
[0110] In a possible implementation, ATF is also used to modify the value of the system control register so that ATF is in an interrupt masking state.
[0111] In a possible implementation, TEE is also used to delete the content recorded in the fifth page table entry.
[0112] In addition, the present application also provides another embodiment of a device for switching execution environments. The device includes ATF, which is used to: shut down MMU; read the base address of the ATF page table from the ATF page table base address register, and obtain the ATF page table directory based on the base address of the ATF page table, the ATF page table directory records a one-to-one correspondence between multiple ATF virtual addresses and multiple ATF page table entries. For any ATF page table entry, the page table entry records an ATF data segment and the access rights of the data segment, or the page table entry records the physical address of an ATF code segment and the access rights of the code segment, or the page table entry records the access rights of the multiple ATF page table entries, or the page table entry does not record content; obtain a second virtual address, and determine the first page table entry corresponding to the second virtual address in the ATF page table directory. The second page table entry is a physical address, the second virtual address is one of the virtual addresses of multiple ATFs, and the second page table entry records the access rights of multiple ATF page table entries; based on the physical address of the second page table entry, the second page table entry is obtained, and in the second page table entry, the access rights of multiple ATF page table entries are modified to read-only; the third virtual address is obtained, and the physical address of the third page table entry corresponding to the third virtual address is determined in the page table directory of the ATF, the third virtual address is one of the virtual addresses of multiple ATFs; based on the physical address of the third page table entry, the third page table entry is obtained, and the access right of the target code segment recorded in the third page table entry is set to non-executable, the target code segment is used to indicate the modification operation of the page table base address register of the ATF and the shutdown operation of the MMU; the MMU is turned on.
[0113] It should be noted that the information interaction, execution process, etc. between the modules / units of the above-mentioned device are based on the same concept as the method embodiment of the present application, and the technical effects they bring are the same as those of the method embodiment of the present application. For specific contents, please refer to the description in the method embodiment shown above in the embodiment of the present application, and will not be repeated here.
[0114] Figure 6 A schematic diagram of the structure of a terminal device provided in an embodiment of the present application. Figure 6 As shown, an embodiment of a terminal device in an embodiment of the present application may include one or more central processors 601 , a memory 602 , an input and output interface 603 , a wired or wireless network interface 604 , and a power supply 605 .
[0115] The memory 602 may be a temporary storage or a permanent storage. Furthermore, the central processor 601 may be configured to communicate with the memory 602 and execute a series of instruction operations in the memory 602 on the terminal device.
[0116] In this embodiment, the CPU 601 can execute the aforementioned Figures 2 to 4The operations performed by the ATF or TEE in the illustrated embodiment will not be described in detail here.
[0117] In this embodiment, the specific functional module division in the central processor 601 can be the same as the above Figure 5 The division of REE, ATF, TEE and other modules described in is similar and will not be repeated here.
[0118] The embodiment of the present application also relates to a computer storage medium, characterized in that it includes computer-readable instructions, and when the computer-readable instructions are executed, the following is implemented: Figures 2 to 4 The method steps performed by ATF or TEE.
[0119] The present application also relates to a computer program product comprising instructions, which, when executed on a computer, causes the computer to execute Figures 2 to 4 The method steps performed by ATF or TEE.
[0120] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0121] In the several embodiments provided in the present application, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interfaces, devices or units, which can be electrical, mechanical or other forms.
[0122] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0123] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0124] If the integrated 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 technical solution of the present application is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of 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 method described in each embodiment of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk and other media that can store program code.
Claims
1. A method for switching an execution environment, characterized in that: The method comprises: The ARM trusted firmware ATF receives a switching request from the rich execution environment REE; The ATF writes a first virtual address to a return address register of the trusted execution environment TEE based on the switching request; If the ATF determines that the first virtual address in the return address register of the TEE is within the legal address range, reading the first virtual address from the return address register of the TEE; The ATF switches to the TEE based on the first virtual address; The ATF switching to the TEE based on the first virtual address specifically includes: The ATF reads the base address of the page table of the TEE from the page table base address register of the TEE, and obtains the page table directory of the TEE based on the base address of the page table of the TEE, wherein the page table directory of the TEE records a one-to-one correspondence between the virtual addresses of multiple TEEs and the physical addresses of the page table entries of multiple TEEs, and for any page table entry of the TEE, the page table entry records the physical address of a data segment of the TEE, or the page table entry records the physical address of a code segment of the TEE, or the page table entry does not record content; The ATF determines, in a page table directory of the TEE, a physical address of a first page table entry corresponding to the first virtual address, where the first virtual address is one of the virtual addresses of the plurality of TEEs; The ATF obtains the first page table entry based on the physical address of the first page table entry, and determines the physical address recorded in the first page table entry; The ATF switches to the TEE based on the physical address recorded in the first page table entry.
2. The method according to claim 1, characterized in that The method further comprises: The ATF writes the base address of the page table of the TEE into the page table base address register of the TEE; If the ATF determines that the first virtual address in the return address register of the TEE is within a legal address range, reading the first virtual address from the return address register of the TEE specifically includes: If the ATF determines that the first virtual address and the base address of the page table of the TEE are both within the legal address range in the return address register of the TEE and the page table base address register of the TEE, the first virtual address is read from the return address register of the TEE.
3. The method according to claim 2, characterized in that If the switching request is used to instruct the ATF to add a correspondence between a fourth virtual address and a target physical address, the fourth virtual address is one of the virtual addresses of multiple ATFs, and the target physical address points to a new shared memory area between the ATF and the TEE, then after the ATF switches to the TEE based on the first virtual address, the method further includes: The TEE receives the switching request from the ATF; The TEE determines, based on the switching request, a physical address of a fourth page table entry corresponding to the fourth virtual address in a page table directory of the ATF, wherein the page table directory of the ATF records a one-to-one correspondence between the virtual addresses of the plurality of ATFs and the physical addresses of the page table entries of the plurality of ATFs; The TEE obtains a fifth virtual address, and determines a physical address of a fifth page table entry corresponding to the fifth virtual address in a page table directory of the TEE, wherein the fifth virtual address is one of the virtual addresses of the plurality of TEEs; The TEE obtains the fifth page table entry based on the physical address of the fifth page table entry, and modifies the content recorded in the fifth page table entry to the physical address of the fourth page table entry; The TEE obtains a fourth page table entry based on the fifth virtual address; The TEE modifies the content recorded in the fourth page table entry to the target physical address.
4. The method according to claim 1 or 2, characterized in that: Before the ATF writes the first virtual address to the return address register of the TEE, the method further includes: The ATF modifies the value of the system control register so that the ATF is in an interrupt shielding state.
5. The method according to claim 3, characterized in that: After the TEE modifies the content of the fourth page table entry record to the target physical address, the method further includes: The TEE deletes the content recorded in the fifth page table entry.
6. A method for switching an execution environment, characterized in that: The method comprises: ATF turns off the memory management unit MMU; The ATF reads the base address of the ATF page table from the ATF page table base address register, and obtains the ATF page table directory based on the base address of the ATF page table, wherein the ATF page table directory records a one-to-one correspondence between multiple ATF virtual addresses and multiple ATF page table entries physical addresses, and for any ATF page table entry, the page table entry records an ATF data segment and access rights for the data segment, or the page table entry records a physical address of an ATF code segment and access rights for the code segment, or the page table entry records access rights for the multiple ATF page table entries, or the page table entry does not record content; The ATF obtains a second virtual address, and determines a physical address of a second page table entry corresponding to the second virtual address in a page table directory of the ATF, wherein the second virtual address is one of the virtual addresses of the plurality of ATFs, and the second page table entry records access rights of the page table entries of the plurality of ATFs; The ATF obtains a second page table entry based on the physical address of the second page table entry, and modifies the access rights of the page table entries of the plurality of ATFs to read-only in the second page table entry; The ATF obtains a third virtual address, and determines a physical address of a third page table entry corresponding to the third virtual address in a page table directory of the ATF, wherein the third virtual address is one of the virtual addresses of the plurality of ATFs; The ATF obtains the third page table entry based on the physical address of the third page table entry, and sets the access permission of the target code segment recorded in the third page table entry to be non-executable, wherein the target code segment is used to indicate the modification operation of the page table base address register of the ATF and the shutdown operation of the MMU; The ATF turns on the MMU.
7. A device for switching execution environments, characterized in that: The device comprises REE, ATF and TEE, wherein the ATF is used for: Receive a switching request from REE; Writing a first virtual address to a return address register of the TEE based on the switching request; If the ATF determines that the first virtual address in the return address register of the TEE is within the legal address range, reading the first virtual address from the return address register of the TEE; Switching to the TEE based on the first virtual address; The ATF is specifically used for: Reading a base address of the page table of the TEE from a page table base address register of the TEE, and obtaining a page table directory of the TEE based on the base address of the page table of the TEE, wherein the page table directory of the TEE records a one-to-one correspondence between virtual addresses of multiple TEEs and physical addresses of page table entries of multiple TEEs, and for any page table entry of the TEE, the page table entry records a physical address of a data segment of the TEE, or the page table entry records a physical address of a code segment of the TEE, or the page table entry does not record content; Determine, in a page table directory of the TEE, a physical address of a first page table entry corresponding to the first virtual address, where the first virtual address is one of the virtual addresses of the plurality of TEEs; Acquire the first page table entry based on the physical address of the first page table entry, and determine the physical address recorded in the first page table entry; Switch to the TEE based on the physical address recorded in the first page table entry.
8. The device according to claim 7, characterized in that The ATF is also used to write the base address of the page table of the TEE into the page table base address register of the TEE; The ATF is specifically used to read the first virtual address from the return address register of the TEE if it is determined in the return address register of the TEE and the page table base address register of the TEE that the first virtual address and the base address of the page table of the TEE are both within a legal address range.
9. The device according to claim 7, characterized in that If the switching request is used to instruct the ATF to add a correspondence between a fourth virtual address and a target physical address, the fourth virtual address is one of the virtual addresses of multiple ATFs, and the target physical address points to a new shared memory area between the ATF and the TEE, then the TEE is used to: receiving the switching request from the ATF; Based on the switching request, determining a physical address of a fourth page table entry corresponding to the fourth virtual address in a page table directory of the ATF, wherein the page table directory of the ATF records a one-to-one correspondence between a plurality of ATF virtual addresses and a plurality of physical addresses of the ATF page table entries; Obtaining a fifth virtual address, and determining a physical address of a fifth page table entry corresponding to the fifth virtual address in a page table directory of the TEE, wherein the fifth virtual address is one of the virtual addresses of the plurality of TEEs; Obtaining a fifth page table entry based on the physical address of the fifth page table entry, and modifying the content recorded in the fifth page table entry to the physical address of the fourth page table entry; Acquire a fourth page table entry based on the fifth virtual address; The content recorded in the fourth page table entry is modified to the target physical address.
10. The device according to claim 7 or 8, characterized in that The ATF is also used to modify the value of the system control register so that the ATF is in an interrupt shielding state.
11. The device according to claim 9, characterized in that The TEE is also used to delete the content recorded in the fifth page table entry.
12. A device for switching execution environments, characterized in that: The device includes an ATF, which is used to: Turn off the MMU; Reading the base address of the ATF page table from the ATF page table base address register, and obtaining the ATF page table directory based on the base address of the ATF page table, wherein the ATF page table directory records a one-to-one correspondence between multiple ATF virtual addresses and multiple ATF page table entries physical addresses, and for any ATF page table entry, the page table entry records an ATF data segment and access rights for the data segment, or the page table entry records a physical address of an ATF code segment and access rights for the code segment, or the page table entry records access rights for the multiple ATF page table entries, or the page table entry does not record content; Obtain a second virtual address, and determine a physical address of a second page table entry corresponding to the second virtual address in the page table directory of the ATF, wherein the second virtual address is one of the virtual addresses of the multiple ATFs, and the second page table entry records access rights of the page table entries of the multiple ATFs; Acquire a second page table entry based on the physical address of the second page table entry, and modify the access rights of the page table entries of the plurality of ATFs to read-only in the second page table entry; Obtaining a third virtual address, and determining a physical address of a third page table entry corresponding to the third virtual address in the page table directory of the ATF, wherein the third virtual address is one of the virtual addresses of the plurality of ATFs; Acquire a third page table entry based on the physical address of the third page table entry, and set the access permission of the target code segment recorded in the third page table entry to be non-executable, wherein the target code segment is used to indicate the modification operation of the page table base address register of the ATF and the shutdown operation of the MMU; The MMU is turned on.
13. A terminal device, characterized in that: The terminal device includes: a processor and a memory; The memory is used to store computer-executable instructions; The processor is used to execute the computer-executable instructions stored in the memory, so that the terminal device implements the method according to any one of claims 1 to 6.
14. A computer storage medium, characterized in that: The method comprises computer-readable instructions, and when the computer-readable instructions are executed, the method according to any one of claims 1 to 6 is implemented.
15. A computer program product comprising instructions, which, when executed on a computer, causes the computer to perform the method according to any one of claims 1 to 6.
16. A chip system, comprising one or more processors and a storage medium, wherein the storage medium stores computer instructions, and when the one or more processors read the computer instructions, the chip implements the method according to any one of claims 1 to 6.
Citation Information
Patent Citations
Processor-assisted kernel address space granularity management method
CN106775923A
TEE extension-based computer security world real-time application dynamic loading method and system
CN111858004A
Cited By
Method for switching execution environment and related device
EP4231159B1
Method for switching execution environment and related device
WO2022100247A1