RISC-V processor enclave switching control methods, systems, media, products, and equipment

By using the first field to save and clear the value of the shadow stack pointer register in the RISC-V processor's enclave switching control method, the problem of the shadow stack being unusable after switching is solved, achieving high-security and low-overhead shadow stack pointer management, and preventing side-channel attacks and control flow verification crashes.

CN122654073BActive Publication Date: 2026-11-10YONGJIANG LAB
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202611158369.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2026-07-31
Publication Date
2026-11-10
Estimated Expiration
2046-07-31

AI Technical Summary

Technical Problem

In RISC-V processors, the shadow stack cannot be used properly after a state switch during function execution, leading to control flow verification failures and potential side-channel attack risks.

Method used

Add a first field (prev_ssp) to the enclave thread context to store the value of the shadow stack pointer register, and write it to the shadow stack pointer register when the enclave is restored. At the same time, clear the shadow stack pointer register when the enclave is exited. Configure the machine-mode enable switch to ensure that the shadow stack function is effective.

Benefits of technology

Ensuring that the shadow stack pointer is not lost during enclave switching avoids side channel leakage, and keeping the shadow stack instructions valid during asynchronous switching improves system compatibility and security.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122654073B_ABST
    Figure CN122654073B_ABST
Patent Text Reader

Abstract

This application discloses a RISC-V processor enclave switching control method, system, medium, product, and device. The RISC-V processor enclave switching control method includes: when an enclave exits, reading the value of the enclave's shadow stack pointer register and saving it to the first field of the enclave thread context, and clearing the shadow stack pointer register (which stores a shadow stack pointer pointing to the address of the last data pushed onto the corresponding shadow stack); when an enclave resumes, reading the value of the first field and writing it to the shadow stack pointer register. This avoids the problem of shadow stack pointer loss during enclave switching.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of industrial control system security technology, specifically relating to a method, system, medium, product, and equipment for controlling the switching of RISC-V processors between different locations. Background Technology

[0002] When using a shadow stack to protect functions in a RISC-V processor, if a state switch is required during function execution, the function's own shadow stack cannot be used normally after the function is restored.

[0003] Therefore, ensuring the normal operation of the shadow stack is a technical problem that urgently needs to be solved in this field. Summary of the Invention

[0004] The purpose of this invention is to provide at least one method for controlling the flying site switching of a RISC-V processor, which can ensure the normal operation of the shadow stack during function execution.

[0005] In a first aspect, the present invention provides a method for controlling the flyover of a RISC-V processor, comprising: When an enclave exits, the value of the enclave's shadow stack pointer register is read and saved to the first field in the enclave thread context. The shadow stack pointer register is then cleared. The shadow stack pointer register is used to store the shadow stack pointer, which points to the address of the last data pushed into the corresponding shadow stack. When the enclave is restored, the value of the first field is read and written to the shadow stack pointer register.

[0006] Optionally, the first field is pre-configured in the structure of the enclave thread context.

[0007] Optionally, when the enclave exits, the RISC-V processor transitions from user mode to machine mode, and then switches from machine mode to kernel mode; when the enclave recovers, the RISC-V processor transitions from kernel mode to machine mode, and then switches from machine mode to user mode.

[0008] Optionally, when the enclave is restored, before reading the value of the first field, the method further includes: configuring a first enable switch in machine mode to enable, authorizing the kernel mode to configure the shadow stack of the enclave in machine mode; configuring a second enable switch in kernel mode to enable, controlling the operation instructions of the shadow stack in user mode to be active.

[0009] Optionally, the first enable switch is menvcfg.SSE, and the second enable switch is senvcfg.SSE.

[0010] Optionally, during enclave recovery, reading the value of the first field and writing it to the shadow stack pointer register includes: Read the value of the first field and determine whether the read value is a preset feature value; If the result of the judgment is negative, then continue to execute the write operation of the shadow stack pointer register; Otherwise, skip the write operation to the shadow stack pointer register.

[0011] Optionally, the physical pages of the shadow stack's storage space are allocated at the end of the enclave's physical memory protection area, so as to incorporate the shadow stack's storage space into the enclave's physical memory protection area.

[0012] Optionally, when the enclave is created, the host driver in kernel mode passes the attribute information of the shadow stack's storage space to the security controller in machine mode, and the security controller initializes the first field to the top address of the shadow stack.

[0013] Optionally, the attribute information of the storage space of the shadow stack includes at least one of the following: the virtual address, physical address, and size of the storage space of the shadow stack.

[0014] Secondly, embodiments of the present invention provide a RISC-V processor enclave switching control system, comprising: The shadow stack pointer saving module is used to read the value of the shadow stack pointer register of the enclave when the enclave exits and save it to the first field in the enclave thread context, and clear the shadow stack pointer register. The shadow stack pointer register is used to save the shadow stack pointer, which points to the address of the last data pushed into the corresponding shadow stack. The shadow stack pointer recovery module is used to read the value of the first field and write it to the shadow stack pointer register when the enclave is recovered.

[0015] Thirdly, embodiments of the present invention also provide a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of the fly-to-ground switching control method for any of the above-described RISC-V processors.

[0016] Fourthly, embodiments of the present invention also provide a computer program product, including a computer program that, when executed by a processor, implements the steps of the fly-to-ground switching control method for any of the above-described RISC-V processors.

[0017] Fifthly, embodiments of the present invention also provide an electronic device, including a memory and a processor, wherein the memory stores a computer program that can run on the processor, and the processor executes the steps of any of the above-described RISC-V processor fly-to-ground switching control methods when running the computer program.

[0018] Compared with the prior art, the technical solution of the embodiments of the present invention has the following beneficial effects: In this embodiment of the invention, a first field is added to the enclave thread context (e.g., in the structure of the enclave thread context) to save the value of the shadow stack pointer register when the enclave exits, and to write the value of the first field back to the shadow stack pointer register when the enclave resumes. This ensures that the corresponding shadow stack pointer is saved during enclave switching, preventing data loss due to overwriting. Furthermore, the shadow stack pointer register is also cleared when the enclave exits, preventing the host kernel from inferring stack activity information of the enclave through the shadow stack pointer register, thus avoiding side-channel leakage.

[0019] Furthermore, during enclave recovery, the first enable switch in machine mode is enabled, the kernel mode is authorized to configure the shadow stack of the enclave in machine mode, and the second enable switch in kernel mode is enabled, controlling the operation instructions of the shadow stack in user mode to be active. This ensures that the kernel mode can configure the shadow stack and the user mode can execute the shadow stack operation instructions, guaranteeing that the shadow stack operation instructions in user mode will not become invalid due to asynchronous enclave switching.

[0020] Furthermore, during enclave recovery, the value of the first field is read and judged. If the value of the first field is the same as the preset characteristic value, it indicates that the enclave does not support the shadow stack function. The write operation to the shadow stack pointer register is skipped, and the subsequent enclave thread context recovery operation is performed. This improves the compatibility of the invention, ensuring that the normal operation of enclaves that do not support the shadow stack function is not affected when the solution of this embodiment is applied to a RISC-V processor.

[0021] Furthermore, during the enclave creation phase, the physical pages of the shadow stack are set at the end of the enclave's physical memory protection region. Thus, the enclave's shadow stack physical pages share the same physical memory protection entry as the enclave's safe memory region, eliminating the need for additional physical memory protection register configuration, and preventing physical memory protection access exceptions when enclave code accesses the shadow stack. Attached Figure Description

[0022] Figure 1 This is a flowchart of a RISC-V processor enclave switching control method according to an embodiment of the present invention; Figure 2This is a schematic diagram showing the correspondence between the virtual address and physical memory of an enclave according to an embodiment of the present invention; Figure 3 This is a flowchart illustrating the processing of the first field and the shadow stack pointer register when saving the enclave thread context in an embodiment of the present invention. Figure 4 This is a flowchart of the enclave thread context switching process related to the shadow stack during the enclave recovery phase in this embodiment of the invention; Figure 5 This is a timing diagram illustrating the changes in the shadow stack pointer register and the first field during the enclave switching process according to an embodiment of the present invention; Figure 6 This is a structural block diagram of a RISC-V processor's enclave switching control system according to an embodiment of the present invention. Detailed Implementation

[0023] As mentioned in the background section, when using a shadow stack to protect functions in a RISC-V processor, the shadow stack of the function itself becomes unusable after the function resumes due to state switching during execution. The inventors of this application have discovered that when a function requiring protection is deployed in a secure enclave within a RISC-V trusted execution environment and Control-Flow Integrity (CFI) protection is applied, the function needs to use the enclave's shadow stack during execution. The shadow stack pointer points to the address of the last data pushed onto the shadow stack. During the execution of a function in user mode, if the enclave exits due to an interrupt, exception, or voluntary call, the processor must force a fallback to the security monitor in machine mode (M-mode) via an exception / interrupt mechanism. Each time the enclave exits, its context needs to be saved; when the enclave resumes, its context also needs to be restored. However, the shadow stack pointer of the enclave is not within the scope of saving / restoring the enclave context. Since the processor's shadow stack pointer register (such as a dedicated CSR register) is a globally shared hardware resource, if the host environment or other enclaves reuse this shadow stack pointer register after an enclave exits, the original enclave's shadow stack pointer information stored in the shadow stack pointer register will be corrupted. Consequently, the enclave will be unable to correctly obtain its corresponding shadow stack pointer when it resumes scheduling. This may result in the enclave obtaining an incorrect return address based on an incorrect shadow stack pointer after the function execution is complete, leading to false alarms or missed alarms in the sspopchk instruction in the function's conclusion, thus causing the control flow check to crash.

[0024] In addition, when switching from enclave mode to kernel mode, if the shadow stack pointer register is not cleared during enclave mode exit, the shadow stack pointer value remaining in the shadow stack pointer register can easily be leaked to the host kernel or other malicious programs after switching to kernel mode, causing base address leakage and potential side-channel attacks.

[0025] Furthermore, the `sspush` and `sspopchk` instructions used for control flow integrity protection are controlled by the kernel-mode enable switch `senvcfg.SSE` (hereinafter referred to as `senvcfg.SSE` or `SSE`). When `SSE=0`, these instructions degenerate into an invalid state and provide no protection. During the enclave exit and subsequent recovery process, if `senvcfg.SSE` is not correctly restored, the shadow stack instructions may become invalid after the enclave is restored, thus causing the control flow integrity protection for functions within the enclave to fail.

[0026] Therefore, in scenarios with strong real-time and high-frequency switching, such as industrial control automation, how to achieve high-security, low-overhead covert storage, secure zeroing, and accurate recovery of shadow stack pointers within the atomic lifecycle of enclave switching through the collaboration of security monitors and underlying hardware is a technical problem that urgently needs to be solved in this field.

[0027] To address this, in this embodiment of the invention, a first field (prev_ssp) for temporarily storing the shadow stack pointer is added to the structure of the enclave thread context. This field saves the value of the shadow stack pointer register when the enclave exits, and assigns the value of the first field to the shadow stack pointer register when the enclave resumes, ensuring that the value in the shadow stack pointer register points to the correct address during the execution of each enclave. Furthermore, when the enclave exits, the shadow stack pointer register is cleared. Therefore, when switching to kernel mode after the enclave exits, the relevant code in the host kernel in kernel mode cannot infer the stack activity information of the enclave from the value of the shadow stack pointer register. Further, during the enclave resumption process, menvcfg.SSE in machine mode is enabled, granting kernel mode permission to configure the shadow stack of the enclave, i.e., enabling the shadow stack function of the enclave. Senvcfg.SSE in kernel mode is also enabled, controlling the shadow stack operation instructions in user mode to be active, i.e., granting user mode permission to execute shadow stack operation instructions. This ensures that the shadow stack instructions are always valid during the enclave's operation.

[0028] To make the above-mentioned objectives, features and beneficial effects of the present invention more apparent and understandable, specific embodiments of the present invention will be described in detail below with reference to the accompanying drawings.

[0029] Reference Figure 1 , Figure 1This is a flowchart of a RISC-V processor fly-to-ground switching control method according to an embodiment of the present invention, including steps S101 and S102, which will be described in detail below.

[0030] Step S101: When the enclave exits, read the value of the enclave's shadow stack pointer register and save it to the first field in the enclave thread context, and clear the shadow stack pointer register. The shadow stack pointer register is used to store the shadow stack pointer, which points to the address of the last data pushed into the corresponding shadow stack.

[0031] In step S102, when the enclave is restored, the value of the first field is read and written to the shadow stack pointer register.

[0032] In the specific implementation of step S101, the first field (prev_ssp) is pre-configured in the enclave thread context, for example, in the structure of the enclave thread context, and is used to temporarily store the shadow stack pointer of the enclave. The enclave thread context refers to the core data structure in the trusted execution environment's security monitor used to save the complete CPU state of a single enclave execution thread, which may include general-purpose registers and control status registers, etc. The context of the enclave thread refers to the context of the RISC-V processor when entering a function to run within the enclave, which is usually in user mode at this time.

[0033] In a non-restrictive example, the enclave thread context structure (thread_context) includes: struct thread_state_t { / / ... Existing fields: general-purpose registers, machine exception program register (prev_mepc), machine mode status register (prev_mstatus), etc.

[0034] / --- CFI (Control Flow Integrity) Extended Fields --- /

[0035] /

[0036] The information stored in prev_ssp is the shadow stack pointer. When exiting the enclave, the value of the shadow stack pointer register (CSR_SSP) is read and saved to prev_ssp. When the enclave is restored, the value of prev_ssp is assigned to the shadow stack pointer register.

[0037] /

[0038] uintptr_t prev_ssp;

[0039] }

[0040] In some embodiments, enclaves require Physical Memory Protection (PMP) during creation. The physical pages of the enclave's shadow stack storage space are allocated at the end of the enclave's PMP region to incorporate the shadow stack's storage space into the enclave's PMP region.

[0041] In a non-restrictive example, in the host-driven enclave creation path, the shadow stack parameters are passed as follows: enclave_sbi_param->ss_paddr = paddr + size; / / The shadow stack is immediately following the end of the enclave safe memory. The physical address of the shadow stack is the sum of the physical address of the enclave memory and the enclave memory capacity.

[0042] enclave_sbi_param->ss_size=ENCLAVE_SHADOW_STACK_SIZE; / / Set the size of the enclave shadow stack page to 4096 bytes.

[0043] enclave_sbi_param->ss_vaddr= ENCLAVE_SHADOW_STACK_VADDR; / / Set the virtual address of the enclave shadow stack to 0x3fffff0000.

[0044] enclave_sbi_param->total_size=size+ ENCLAVE_SHADOW_STACK_SIZE.

[0045] Reference Figure 2 , Figure 2The diagram illustrates the correspondence between the virtual address 201 of the enclave and the physical memory 210 in an embodiment of the present invention. The addresses corresponding to the enclave virtual address 201 from high to low include: isolation band 202, standard stack 203, shadow stack upper isolation band 204, shadow stack 205, shadow stack lower isolation band 206, untrusted shared memory 207, enclave data 208, and enclave code 209. Among them, isolation zone 202 is used to prevent standard stack 203 from overflowing; standard stack 203 is used to store relevant data used by functions within the enclave during runtime; shadow stack 205 is used to store the return address of functions within the enclave before execution; isolation zone 204 on the shadow stack is used to prevent data overflow from standard stack 203 from contaminating shadow stack 205; isolation zone 206 below the shadow stack is used to prevent data overflow from shadow stack 205 from contaminating other areas; untrusted shared memory 207 is a memory area that can be used by both the enclave and the host kernel in kernel mode, and functions within the enclave obtain relevant data required for execution through untrusted shared memory 207 during runtime; enclave data 208 is used for storage; and enclave code 209 is used to store relevant code for enclave execution. Physical memory 210 includes shadow stack pages 211 and secure storage area 212. Shadow stack 205 corresponds to shadow stack pages 211, and the page table entry (PTE) of shadow stack 205 is encoded as "R=0, W=1 (shadow stack instruction)", indicating that shadow stack 205 can only be modified by shadow stack instructions (such as sspush and sspopchk instructions).

[0046] In some embodiments, during the enclave creation phase, the attribute information of the storage space of the host driver shadow stack in kernel mode is passed to the security controller (monitor) in machine mode. The security controller initializes the first field to the top address of the shadow stack. The attribute information of the shadow stack's storage space may include one or more of the following: the virtual address, physical address, and size of the shadow stack's storage space.

[0047] In a non-restrictive example, the security monitor initializes the first field by: enclave->thread_context.prev_ssp=create_args.ss_vaddr + create_args.ss_size; / / Since the enclave's shadow stack grows downwards, the first data pushed onto the shadow stack is located at the top of the shadow stack, so the first field is initialized to the address of the top of the shadow stack.

[0048] In some specific embodiments, during the enclave exit phase, since the enclave is running in user mode, the RISC-V processor needs to transition from user mode to machine mode, perform a context switch, and then switch back to kernel mode. During this switchover process, the enclave thread context information needs to be saved, as described in [reference needed]. Figure 3 , Figure 3 The present invention illustrates the processing flow of the first field and the shadow stack pointer register when saving the enclave thread context in an embodiment of the present invention, including steps S301 to S306, which will be described in detail below.

[0049] Step S301: Determine whether the hardware supports the shadow stack function, that is, whether the enclave shadow stack is available. If the result is yes, proceed to step S302; if the result is no, proceed to step S305.

[0050] Step S302: Obtain the value s of the shadow stack pointer register. If the value of s is zero, jump to step S306; if the value of s is not zero, jump to step S303.

[0051] Step S303: Save s to the first field.

[0052] Step S304: Clear the shadow stack pointer register.

[0053] Step S305: Skip the read and write operations on the shadow stack pointer register.

[0054] Step S306: Save the original value of the first field.

[0055] In the specific implementation of step S301, during the process of saving the enclave thread context, it is first determined whether the shadow stack function of the enclave is enabled. If the shadow stack function of the enclave is not enabled, it is not necessary to store the shadow stack pointer, that is, jump to step S305, and skip the read and write operation of the shadow stack pointer register during the process of saving the enclave thread context.

[0056] In the specific implementation of step S302, if the hardware path is not properly activated (e.g., SSE is not yet enabled), reading CSR_SSP may return a default value of 0. If the value of CSR_SSP is mistakenly read as 0 when exiting the enclave, this value should not be used to overwrite the valid address originally saved in prev_ssp. Therefore, the saved value of prev_ssp is only updated when a valid address is read (s != 0); if the value of CSR_SSP is read as 0, then jump to step S306, that is, retain the original state of prev_ssp to ensure that the valid shadow stack pointer is not lost during the next recovery.

[0057] During enclave recovery, after reading the value of the first field, a compliance and non-zero check is performed against the preset legal physical memory boundaries of the shadow stack. If the check passes, the shadow stack pointer register is restored; if the check fails (e.g., the field is abnormally zero or exceeds the legal boundary), it is determined that the current enclave context may have been tampered with or attacked by the kernel mode, the security monitor refuses to restore the enclave and immediately triggers a security audit response. Meanwhile, for regular enclaves pre-configured to not enable shadow stack functionality, the write process is safely skipped based on the enclave attribute flag. Thus, while perfectly ensuring forward compatibility, it effectively prevents backdoor attacks that bypass shadow stack defenses by maliciously zeroing the pointer field, significantly improving the system's anti-tampering capabilities.

[0058] In a non-limiting example, the processing flow for the first field and the shadow stack pointer register when saving the enclave thread context in this embodiment of the invention may include: void save_shadow_stack(struct enclave_t enclave) { / / Only operate CSR_SSP when the enclave supports shadow stack related functions.

[0059] if (penglai_cfi_ssp_hw()) {

[0060] uintptr_t s = csr_read(CSR_SSP);

[0061] / / Save the current value of CSR_SSP to the thread context.

[0062] if (s != 0)

[0063] enclave->thread_context.prev_ssp = s;

[0064] / / Clear CSR_SSP to prevent the host side from accessing enclave shadow stack information

[0065] csr_write(CSR_SSP, 0);

[0066] }

[0067] }

[0068] In the specific implementation of step S102, during the enclave recovery phase, the RISC-V processor transitions from kernel mode to machine mode, performs a context switch, and then switches back to user mode. The first enable switch `menvcfg.SSE` in machine mode is enabled, granting the kernel mode permission to configure the shadow stack of the kernel enclave, i.e., authorizing the activation of the shadow stack function. The second enable switch `senvcfg.SSE` in kernel mode is also enabled, activating the shadow stack operation instructions in user mode, i.e., authorizing the user mode to execute shadow stack operation instructions. Thus, while ensuring cross-privilege level security isolation, the shadow stack mechanism of the user-mode enclave is guaranteed not to fail due to asynchronous enclave switching, and the host kernel in the non-secure world still has no right to access or configure the shadow stack of the enclave across the Physical Memory Protection (PMP) boundary, ensuring the absolute integrity of the enclave trusted base.

[0069] In some specific embodiments, reference is made to Figure 4 , Figure 4 The following describes the enclave thread context switching process related to the shadow stack during the enclave recovery phase in an embodiment of the present invention, including steps S401 to S405, which will be described in detail below.

[0070] Step S401: Determine whether the value of the first field is a preset feature value. If the determination result is yes, proceed to step S405; if the determination result is no, proceed to step S402.

[0071] Step S402: Set the value of menvcfg.SSE to 1.

[0072] The purpose of this step is to enable the first enable switch menvcfg.SSE in machine mode.

[0073] Step S403: Set the value of senvcfg.SSE to 1.

[0074] The purpose of this step is to enable the first enable switch senvcfg.SSE in kernel mode.

[0075] Step S404: Read the value of the first field and assign it to the shadow stack pointer register.

[0076] In step S405, the shadow stack instruction is invalid at this time, so the write operation to the shadow stack pointer register is skipped.

[0077] In the specific implementation of step S401, when the value of the first field is zero (that is, the preset characteristic value is zero), it indicates that the enclave did not initialize the value of the first field during the creation phase, that is, the host kernel driver version is too old (for example, the kernel mode did not pass the shadow stack parameters, and prev_ssp defaults to 0 during initialization) or the enclave did not enable the shadow stack related functions. Since the enclave does not enable the shadow stack function, the shadow stack instructions (such as sspush and sspopchk instructions) are also invalid, and the process jumps to step S405. During the enclave recovery process, the write operation to the shadow stack pointer register is no longer performed.

[0078] In a non-limiting example, the context switching procedure during the enclave recovery phase may include: void restore_shadow_stack(struct enclave_t enclave) { / / Security check: prev_ssp=0 indicates that the host kernel driver did not pass shadow stack parameters.

[0079] if (enclave->thread_context.prev_ssp == 0 ) {

[0080] return; / / Skip setting the shadow stack pointer register; shadow stack instructions will degenerate into an invalid state.

[0081] }

[0082] / / Step 1: Set menvcfg.SSE = 1 (to allow kernel-mode configuration of enclave shadow stacks).

[0083] uintptr_t menvcfg = csr_read(CSR_MENVCFG);

[0084] menvcfg |= MENVCFG_SSE;

[0085] csr_write(CSR_MENVCFG, menvcfg);

[0086] / / Step 2: Set senvcfg.SSE = 1 (grant user space permission to execute shadow stack operation instructions).

[0087] uintptr_t senvcfg = csr_read(CSR_SENVCFG);

[0088] senvcfg |= SENVCFG_SSE;

[0089] csr_write(CSR_SENVCFG, senvcfg);

[0090] / / Step 3: Restore CSR_SSP from prev_ssp

[0091] csr_write(CSR_SSP, enclave->thread_context.prev_ssp);

[0092] }

[0093] In a non-restrictive example, the complete context switching sequence during enclave operation includes: Enclave recovery (transferring from kernel mode to machine mode, then switching to the enclave): grant_enclave_access() → Configures PMP to allow enclave access.

[0094] restore_shadow_stack()→menvcfg.SSE=1,senvcfg.SSE=1, CSR_SSP=prev_ssp.

[0095] swap_prev_mepc() → Restores the Machine Exception Program Counter (MEPC).

[0096] switch_to_enclave_mode() → Switches the Supervisor Address Translation and Protection (SATP) register, restores the general-purpose registers, and switches to user mode.

[0097] Enclave execution (user mode): `sspush ra→ SSP -= 8` sets the length of the return address to 8 bytes, writes the return address to the shadow stack, and reduces the value of the shadow stack pointer register by 8 bytes.

[0098] ...function body...

[0099] sspopchk ra → Compare the return address with the top of the shadow stack. If they match, pop the return address stored at the top of the shadow stack and increment the value of the shadow stack pointer register by 8 bytes.

[0100] Enclave exit (entering machine mode from the enclave and then switching to kernel mode): ecall / trap / timer → enter machine mode.

[0101] swap_prev_mepc()→MEPC.

[0102] save_shadow_stack()→prev_ssp = CSR_SSP, CSR_SSP = 0.

[0103] retrieve_enclave_access() → Cancel PMP.

[0104] switch_to_host_mode() → Restores the kernel-mode SATP and general-purpose registers and switches to kernel mode.

[0105] Reference Figure 6 This invention also provides a RISC-V processor enclave switching control system 60, including: a shadow stack pointer storage module 61, used to read the value of the shadow stack pointer register of the enclave and save it to the first field in the enclave thread context when the enclave exits, and clear the shadow stack pointer register, the shadow stack pointer register is used to store the shadow stack pointer, the shadow stack pointer points to the address of the last data pushed into the corresponding shadow stack; and a shadow stack pointer recovery module 62, used to read the value of the first field and write it to the shadow stack pointer register when the enclave is restored.

[0106] This invention also provides a computer-readable storage medium, which is a non-volatile or non-transitory storage medium storing a computer program. When executed by a processor, the computer program performs the steps of the RISC-V processor's fly-by-flight control method provided in any of the above embodiments. Preferably, the storage medium may include a computer-readable storage medium such as non-volatile or non-transitory memory. The storage medium may include ROM, RAM, a magnetic disk, or an optical disk, etc.

[0107] This invention also provides a computer program product, including a computer program that, when executed by a processor, implements the steps of the RISC-V processor fly-to-ground switching control method provided in any of the above embodiments.

[0108] This invention also provides a server, including a memory and a processor. The memory stores a computer program that can run on the processor. When the processor runs the computer program, it executes the steps of the RISC-V processor fly-to-ground switching control method provided in any of the above embodiments.

[0109] In a non-limiting example, the operation process of an enclave using the enclave switching control method of this invention includes: creating an enclave and initializing the first field to 0x3fffff1000 (i.e., the top value of the shadow stack). See reference... Figure 5 , Figure 5 This illustration shows the timing changes of the shadow stack pointer register and the software-saved value (i.e., the first field) during enclave switching according to an embodiment of the present invention. When the enclave is first restored, the value of the first field is assigned to the shadow stack pointer register, i.e., the value of the shadow stack pointer register is set to 0x3fffff1000, and the first and second enable switches are enabled. During enclave operation, the sspush instruction is called to push the return address onto the shadow stack, and the value of the shadow stack pointer register drops to 0x3fffff0ff8. When the enclave exits, the value of the shadow stack pointer register (0x3fffff0ff8) is saved to the first field, and the shadow stack pointer register is cleared. When the enclave is restored again, the value of the first field is assigned to the shadow stack pointer register; at this time, both the first field and the shadow stack pointer register have the value of 0x3fffff0ff8. When the function within the enclave finishes execution, the sspopchk instruction is called to pop the return address saved at the top of the shadow stack, and the value of the shadow stack pointer register rises to 0x3fffff1000.

[0110] In a non-limiting example, the enclave using the enclave switching control method of this embodiment of the invention executes multiple scan cycles during operation. Within each scan cycle, enclave exit and recovery, as well as shadow stack related operations, are performed. After multiple scan cycles, the value of the shadow stack pointer register matches the expected value, the enclave operates normally, there are no CFI false alarms, and the value of the shadow stack pointer register is zero each time the enclave exits (the shadow stack pointer register is successfully cleared upon enclave exit). Therefore, the enclave switching control method provided in this embodiment of the invention persists the shadow stack pointer in the enclave thread context through the prev_ssp field, ensuring that the shadow stack pointer remains continuously valid during repeated enclave exits / recoveries, eliminating false alarms from the sspopchk instruction caused by context switching. Each time the enclave recovers, menvcfg.SSE and senvcfg.SSE are configured to be enabled to ensure that user-mode shadow stack instructions do not become invalid due to context switching. The shadow stack pointer register is cleared upon enclave exit to prevent the kernel from inferring the function call depth and stack activity mode of the enclave through the residual shadow stack pointer value. The shadow stack physical pages are allocated at the end of the enclave's physical memory protection region, sharing the same PMP entry protection with the enclave code and data, requiring no additional physical memory protection register configuration. Furthermore, when prev_ssp=0, read / write operations to the shadow stack pointer register are automatically skipped, ensuring compatibility with older drivers that do not pass shadow stack parameters and enclave applications that do not use a shadow stack. This addresses the issue in existing technologies where, after enclave exit, if the host kernel or interrupt handling code modifies CSR_SSP, the shadow stack pointer will point to an incorrect location upon enclave recovery, leading to false positives or missed detections in subsequent sspopchk commands. The sspush / sspopchk instructions are controlled by the senvcfg.SSE bit; when SSE=0, these instructions degenerate into NOPs, providing no protection. If SENVCFG is not correctly restored after enclave exit, shadow stack instructions may become invalid after enclave recovery. The shadow stack physical pages must be within the enclave's PMP protection range; otherwise, access to the shadow stack by enclave user-mode code will trigger a PMP access exception. The existing enclave memory allocation mechanism does not consider the physical page requirements of the shadow stack. If the shadow stack address of the enclave remains in CSR_SSP when the enclave exits, kernel-mode code may read this value to infer the function call depth and stack activity mode of the enclave, which presents a technical problem.

[0111] Those skilled in the art will understand that all or part of the steps in the various methods of the above embodiments can be performed by a program instructing related hardware. The program can be stored in a computer-readable storage medium, which may include ROM, RAM, disk, or optical disk, etc.

[0112] While the present invention has been disclosed above, it is not limited thereto. Any person skilled in the art can make various modifications and alterations without departing from the spirit and scope of the invention; therefore, the scope of protection of the present invention should be determined by the scope defined in the claims.

Claims

1. A method for controlling the switching between different locations in a RISC-V processor, characterized in that, include: When an enclave exits, the value of the enclave's shadow stack pointer register is read and saved to the first field in the enclave thread context. The shadow stack pointer register is then cleared. The shadow stack pointer register is used to store the shadow stack pointer, which points to the address of the last data pushed into the corresponding shadow stack. When the enclave is restored, the value of the first field is read and written to the shadow stack pointer register; When the enclave exits, the RISC-V processor transitions from user mode to machine mode, and then switches from machine mode to kernel mode; when the enclave recovers, the RISC-V processor transitions from kernel mode to machine mode, and then switches from machine mode to user mode. When the enclave is restored, before reading the value of the first field, the process further includes: configuring a first enable switch in machine mode to enable, authorizing the kernel mode to configure the shadow stack of the enclave in machine mode; configuring a second enable switch in kernel mode to enable, controlling the operation instructions of the shadow stack in user mode to be active. The first enable switch is menvcfg.SSE, and the second enable switch is senvcfg.SSE.

2. The enclave switching control method according to claim 1, characterized in that, The first field is pre-configured in the structure of the enclave thread context.

3. The enclave switching control method according to claim 1, characterized in that, During enclave recovery, reading the value of the first field and writing it to the shadow stack pointer register includes: Read the value of the first field and determine whether the read value is a preset feature value; If the result of the judgment is negative, then continue to execute the write operation of the shadow stack pointer register; Otherwise, skip the write operation to the shadow stack pointer register.

4. The enclave switching control method according to claim 1, characterized in that, The physical pages of the shadow stack's storage space are allocated at the end of the enclave's physical memory protection area, so as to incorporate the shadow stack's storage space into the enclave's physical memory protection area.

5. The enclave switching control method according to claim 1, characterized in that, Also includes: When the enclave is created, the host driver in kernel mode passes the attribute information of the shadow stack's storage space to the security controller in machine mode, and the security controller initializes the first field to the top address of the shadow stack.

6. The enclave switching control method according to claim 5, characterized in that, The attribute information of the storage space of the shadow stack includes at least one of the following: the virtual address, physical address, and size of the storage space of the shadow stack.

7. A RISC-V processor's enclave switching control system, characterized in that, include: The shadow stack pointer saving module is used to read the value of the shadow stack pointer register of the enclave and save it to the first field in the enclave thread context when the enclave exits, and clear the shadow stack pointer register. The shadow stack pointer register is used to save the shadow stack pointer, which points to the address of the last data pushed into the corresponding shadow stack. The shadow stack pointer recovery module is used to read the value of the first field and write it to the shadow stack pointer register when the enclave is restored; When the enclave exits, the RISC-V processor transitions from user mode to machine mode, and then switches from machine mode to kernel mode; when the enclave recovers, the RISC-V processor transitions from kernel mode to machine mode, and then switches from machine mode to user mode. When the enclave is restored, before reading the value of the first field, the process further includes: configuring a first enable switch in machine mode to enable, authorizing the kernel mode to configure the shadow stack of the enclave in machine mode; configuring a second enable switch in kernel mode to enable, controlling the operation instructions of the shadow stack in user mode to be active. The first enable switch is menvcfg.SSE, and the second enable switch is senvcfg.SSE.

8. A computer-readable storage medium having a computer program stored thereon, characterized in that, When executed by the processor, the computer program implements the steps of the RISC-V processor's field switching control method as described in any one of claims 1 to 6.

9. A computer program product, comprising a computer program, characterized in that, When executed by the processor, the computer program implements the steps of the RISC-V processor's field switching control method as described in any one of claims 1 to 6.

10. An electronic device comprising a memory and a processor, wherein the memory stores a computer program executable on the processor, characterized in that, When the processor runs the computer program, it performs the steps of the RISC-V processor enclave switching control method according to any one of claims 1 to 6.

Citation Information

Patent Citations

  • Efficient exception handling in trusted execution environments

    US20230205869A1