Repository navigation
Adding RISC-V landing pad support to OP-TEE using the Zicfilp ISA extension - #8328
Open
raymo200915 wants to merge 14 commits into
Open
raymo200915 wants to merge 14 commits into
raymo200915 wants to merge 14 commits into
Conversation
Add the architectural state introduced by the ratified RISC-V CFI extensions that the core will program: - senvcfg.LPE (Zicfilp landing pads in U-mode) and senvcfg.SSE (Zicfiss shadow stacks in U-mode), as CSR_SENVCFG_* in riscv.h next to the other CSR bit definitions - the ssp CSR (0x011), the shadow stack pointer - the software-check exception (cause 18) and its xtval codes, 2 for a landing pad fault and 3 for a shadow stack fault Shadow stacks in U-mode need menvcfg.SSE set by the M-mode firmware: with it clear senvcfg.SSE is read-only zero and any access to ssp from S-mode raises an illegal instruction exception. The SBI v3.0 Firmware Features extension (FWFT) is the ratified way for S-mode to request that. Add the FWFT function and feature IDs and sbi_fwft_set() and sbi_fwft_get() on top of the boot-time extension probe. The features relevant here are hart-local, so the callers set them on every hart. No functional change. Signed-off-by: Marouene Boubakri <marouene.boubakri@oss.nxp.com>
Zicfilp and Zicfiss report a control-flow violation with the software-check exception (cause 18). xtval then holds a code, 2 for a missing landing pad and 3 for a shadow stack mismatch, rather than an address. Report landing-pad and shadow-stack faults separately and use the faulting PC as the abort address. The existing illegal-instruction check already excludes software checks from lazy FP/vector handling. Nothing raises the exception until a later change enables the extensions for user mode. Signed-off-by: Marouene Boubakri <marouene.boubakri@oss.nxp.com>
Zicfilp (ratified) is the RISC-V counterpart of Arm BTI: with senvcfg.LPE set, every indirect jump or call executed in U-mode must land on an LPAD instruction or the hart raises a software-check exception. Unlike BTI there is no per-page bit, the check is enabled for the whole privilege mode, so the core has to decide per user context whether the code it is about to run was built with landing pads. Reuse the marker BTI already has: ldelf maps the executable segments of an ELF that declares landing pads with TEE_MATTR_GUARDED. Add CFG_TA_ZICFILP and, when set, derive an senvcfg.LPE value for a context in core_mmu_create_user_map(): enabled only if every TEE_MATTR_UX region of the context is guarded. A single unguarded executable mapping disables enforcement for that context rather than faulting on its first indirect call. ldelf later refuses to mix guarded and unguarded ELFs, so this stays a fail-safe rather than a policy. The bit travels with the user mapping: struct core_mmu_user_map gains an senvcfg field written by core_mmu_set_user_map() and read back by core_mmu_get_user_map(), which also makes it survive a thread suspend and resume through threads[].user_map. Clearing the user mapping clears the bit. Only the LPE and SSE bits of senvcfg are owned by the mapping, the rest of the register is left to the hart. A boot-time probe on every hart (cfi_init_hart()) writes and reads back senvcfg.LPE, which is WPRI without the extension, and logs whether landing pads can be enforced. The bit is then left clear until a user mapping asks for it. Harts that disagree on the result panic, the enforcement decision must not depend on which hart a TA lands on. Zicfilp for S-mode is controlled by menvcfg.LPE and would require the core itself to be built with landing pads; that is left to the M-mode firmware and out of scope. senvcfg is a privileged-1.12 CSR, so the option is only meaningful on harts that implement it, as is CFG_RISCV_ISA_ZICBOM. Nothing is guarded on RISC-V yet: the ldelf side and the toolchain flags follow in later changes, so the option is inert here. Signed-off-by: Marouene Boubakri <marouene.boubakri@oss.nxp.com>
Zicfiss keeps return addresses on a shadow stack that only the shadow stack instructions may write. In the page tables such a page has a dedicated encoding, W set and R clear, which is reserved otherwise. Ordinary loads from it are allowed, ordinary stores fault, and SSPUSH or SSPOPCHK on a page that is not a shadow stack page fault too. Add TEE_MATTR_SHADOW_STACK, exclusive of the other permission bits, in the bit 1 gap of the attribute space so it fits the 16-bit vm_region attribute. Include it in TEE_MATTR_PROT_MASK so vm_map() and vm_set_prot() accept it. On RISC-V, mattr_to_pte_bits() encodes it as V|U|W|A|D and pte_to_mattr() decodes W-without-R back to it. Arm ignores the bit. Let ldelf request such a mapping with LDELF_MAP_FLAG_SHADOW_STACK on sys_map_zi(). The core accepts the flag only with CFG_TA_ZICFISS and never together with LDELF_MAP_FLAG_SHAREABLE: a shadow stack belongs to one context. The mapping gets no core-side access either, the memory comes zero-filled from the fobj and the core never touches it. Signed-off-by: Marouene Boubakri <marouene.boubakri@oss.nxp.com>
Zicfiss (ratified) is the RISC-V counterpart of the return-address half of Arm PAuth: the prologue pushes ra on a shadow stack with SSPUSH and the epilogue checks it with SSPOPCHK, a mismatch raises a software-check exception. The shadow stack pointer is the ssp CSR, user state that the core has to provide and preserve. Add CFG_TA_ZICFISS with the core side of that: Enabling. senvcfg.SSE is read-only zero and ssp traps from S-mode until the M-mode firmware sets menvcfg.SSE. cfi_init_hart() requests it on every hart through the SBI FWFT SHADOW_STACK feature (SBI v3.0) and confirms through senvcfg that it took effect; the result is exposed by cfi_shadow_stack_enabled(). Harts that disagree panic. Once enabled, core_mmu_create_user_map() sets SENVCFG_SSE for every user context: the shadow stack instructions are no-ops in an ELF built without them, so there is nothing to gain from switching it per context. Shadow stacks. Each user stack gets its own shadow stack. The core maps one for ldelf next to the ldelf stack (TEE_MATTR_SHADOW_STACK, half the stack size, a guard page on each side) and records its top in uctx->ldelf_shadow_stack_ptr. The TA shadow stack is mapped by ldelf, which returns its top in the new ldelf_arg::shadow_stack_ptr, kept in uctx->shadow_stack_ptr (0 when the TA has none). ssp handling. Every entry into user mode is a fresh activation at the top of either the TA stack or the ldelf stack, so thread_enter_user_mode() starts ssp at the top of the shadow stack paired with the stack user_sp points into. While a thread is in S-mode another thread may run a TA on the same hart, so ssp is saved with the user registers: in thread_scall_regs on a syscall and restored before the return to user mode, in thread_ctx_regs when a foreign interrupt suspends the thread and restored by thread_resume_from_rpc(). Aborts from user mode need no handling, the only path back to user mode from there is the lazy FP enable, with no thread switch in between. All accesses are gated by cfi_shadow_stack_enabled(), the CSR traps otherwise. The ldelf side (property parsing, TA shadow stack allocation) and the toolchain flags follow. Until then the option only maps an unused shadow stack for ldelf. Signed-off-by: Marouene Boubakri <marouene.boubakri@oss.nxp.com>
A RISC-V ELF built with -fcf-protection carries a GNU_PROPERTY_RISCV_FEATURE_1_AND note (pr_type GNU_PROPERTY_LOPROC, the value AArch64 uses for its own feature note) with bit 0 set for landing pads (CFI_LP_UNLABELED) and bit 1 for shadow stacks (CFI_SS). The linker ANDs the note over all input objects, so a set bit means the whole ELF was built that way. Parse it in parse_property_segment() next to the AArch64 note, the architecture of the build decides which one applies: - CFI_LP marks the ELF guarded exactly as BTI does: its executable segments get LDELF_MAP_FLAG_BTI through ta_elf_add_bti(), which the core turns into TEE_MATTR_GUARDED and, once every executable mapping of the context is guarded, into senvcfg.LPE. - CFI_SS on the main TA makes ta_elf_load_main() map a shadow stack with the new LDELF_MAP_FLAG_SHADOW_STACK, half the TA stack size with a guard page on each side, and hand its top back to the core in ldelf_arg::shadow_stack_ptr. Landing pads and shadow stacks are per context on RISC-V, not per page, so the ELFs of a context have to agree. Refuse to load: - a dependency without landing pads into a TA that has them, it would fault on its first indirect call; - a dependency that uses a shadow stack into a TA without one, it would push through a null ssp. The other two combinations are harmless (LPAD and the shadow stack instructions are no-ops for code that does not use them) and are allowed. The checks apply to dependencies loaded at start and through dlopen() alike. Signed-off-by: Marouene Boubakri <marouene.boubakri@oss.nxp.com>
Wire the toolchain side of CFG_TA_ZICFILP and CFG_TA_ZICFISS: - add _zicfilp and _zicfiss to the ISA string and pass -fcf-protection=branch, =return or =full to the TA targets (C and C++) and to ldelf, which runs in user mode next to the TAs. The core keeps its flags: Zicfilp in S-mode is the firmware's decision and Zicfiss in S-mode would need a core shadow stack. The compiler emits LPAD at indirect branch targets, SSPUSH/SSPOPCHK around return addresses and the GNU_PROPERTY_RISCV_FEATURE_1_AND note. GCC 15 is the first release with the option for RISC-V; the build stops if the compiler rejects it rather than producing unprotected TAs under a protection option, as the Arm BTI/PAuth flags do. - emit the same property note from the assembly objects of ldelf and of the libraries linked with TAs (emit_riscv_feature_1_and in riscv.S, wrapped in RISCV_CFI() for user mode objects only). The linker ANDs the note over all inputs, an object without it would strip the property from the final ELF. None of these functions is an indirect branch target and none needs its return protected, so no LPAD or shadow stack instruction is added to them. - map ldelf's code TEE_MATTR_GUARDED under CFG_TA_ZICFILP as it is under CFG_CORE_BTI: it now carries landing pads, and the context is only enforced once every executable mapping is guarded. - make setjmp() record the shadow stack pointer (SSRDP, reads 0 when inactive) in a new jmp_buf slot and longjmp() unwind the shadow stack back to it, otherwise the SSPOPCHK of the function that called setjmp() would fault on the abandoned frames. The unwind follows the spec's example: at most a page per step, each step writing ssp and touching the shadow stack with an SSPUSH/SSPOPCHK pair so that unwinding beyond the guard page faults instead of pointing ssp at arbitrary memory. Verified with GCC 13.3 / binutils 2.42: the default configuration is unchanged, the option is refused as expected, and with the toolchain checks bypassed on the command line everything but the Zicfiss instructions in setjmp_rv.S assembles and links; the property note in the resulting objects has the expected layout. Building with the options enabled needs GCC 15 and a binutils that merges RISC-V GNU property notes into PT_GNU_PROPERTY. Signed-off-by: Marouene Boubakri <marouene.boubakri@oss.nxp.com>
parse_load_segments() records a PT_GNU_PROPERTY program header only when CFG_TA_BTI is enabled. Consequently, RISC-V TAs using Zicfilp or Zicfiss do not reach the RISC-V property parser despite carrying GNU_PROPERTY_RISCV_FEATURE_1_AND. Record the property segment when either RISC-V CFI extension is enabled so ldelf can determine whether the TA is guarded. Signed-off-by: Raymond Mao <raymond.mao@riscstar.com>
The CFI compiler capability probe only needs to reject unsupported CFI options. Build environments can add unrelated driver options to the compiler invocation, which are harmless for its compile-only test but would be promoted to errors by -Werror. Keep compiler option errors fatal while allowing unrelated warnings, so the probe reports the actual CFI capability. Signed-off-by: Raymond Mao <raymond.mao@riscstar.com>
Zicfilp enforcement in S-mode needs every indirect branch target in the core to begin with an unlabeled LPAD. Add CFG_CORE_ZICFILP for S-mode builds and use the compiler-specific Zicfilp ISA and forward-edge CFI options for both C and assembly. Insert lpad 0 in the standard RISC-V assembly function entry macros. Apply the CFI options directly to the core submodule because ldelf inherits the common core platform flags; enabling core protection alone must not instrument ldelf. Signed-off-by: Raymond Mao <raymond.mao@riscstar.com>
LLVM requires -menable-experimental-extensions for assembly as well as for C when Zicfilp is enabled. Pass the TA-specific assembler flags to the TA libraries and ldelf, and let ldelf consume its platform assembly flags. Export CFG_TA_ZICFILP and its preprocessor definition through the TA development kit. This lets out-of-tree TA assembly that includes asm.S emit its required landing pads. Signed-off-by: Raymond Mao <raymond.mao@riscstar.com>
Request SBI_FWFT_LANDING_PAD on every OP-TEE hart when the S-mode core is built with Zicfilp. Read the setting back and, when TA Zicfilp is enabled, confirm that senvcfg.LPE became writable. Build the CFI initialization code for core-only configurations too. A core built with landing pads panics when firmware cannot enable landing-pad enforcement, rather than silently running without it. Signed-off-by: Raymond Mao <raymond.mao@riscstar.com>
Zicfilp saves the expected landing-pad state in sstatus.SPELP on a trap. Existing interrupted contexts already retain the complete sstatus value, including SPELP, across thread suspension and resume. xstatus_for_xret() instead creates a new exception-return context. Clear SPELP there so a live state from the current context cannot affect a new core, user, unwind, or RPC return context. Signed-off-by: Raymond Mao <raymond.mao@riscstar.com>
Allow a main TA without GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_UNLABELED to load without landing-pad enforcement, so existing TAs remain compatible when CFG_TA_ZICFILP is enabled. Report the downgrade explicitly from ldelf. Since the executable mappings are not marked guarded, the core keeps senvcfg.LPE clear for the TA context. Signed-off-by: Raymond Mao <raymond.mao@riscstar.com>
This was referenced Dec 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This series adds RISC-V Zicfilp landing-pad enforcement for the OP-TEE S-mode core and completes Zicfilp support for trusted applications built with GCC or LLVM.
The first seven commits are rebased from Marouene Boubakri’s series #8058 for user-mode Zicfilp landing pads and Zicfiss shadow stacks.
Core and TA changes:
CFG_CORE_ZICFILPenables compiler-generated landing pads in the S-mode core and adds lpad 0 to the standard RISC-V assembly function-entry macros. At boot, OP-TEE requestsSBI_FWFT_LANDING_PADon every OP-TEE hart and reads the setting back. If firmware cannot enable enforcement on a hart, the core panics.When constructing a new exception-return context, the core clears
sstatus.SPELPso the new context cannot inherit a pending landing-pad expectation. Interrupted contexts retain their saved sstatus across suspend and resume.The build passes the required C and assembly flags to the core, ldelf and TA libraries, and exports
CFG_TA_ZICFILPthrough the TA development kit. It also emits the RISC-V GNU property note for protected LLVM builds so ldelf can identify guarded TAs.ldelf now reads
PT_GNU_PROPERTYwhen RISC-V CFI is enabled. An unmarked TA can still run, with a warning that landing-pad enforcement is disabled for that TA. A guarded TA rejects an unmarked shared library.The series has been verified with xtest 1049 (added in OP-TEE/optee_test#864) on QEMU 10.1.93, using GCC 15.3.0 with GNU Binutils 2.45.1, or LLVM/Clang 22.1.8.