Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

4.5 CMSE toolchain contract and stack sealing

The Cortex-M Security Extensions (CMSE) ACLE spec defines how C code runs at the boundary. You compile with -mcmse and include <arm_cmse.h>.

  • __attribute__((cmse_nonsecure_entry)) marks a Secure entry function. The compiler emits a special symbol (__acle_se_foo). The linker generates an SG plus B.W veneer for it in a dedicated section (e.g. .gnu.sgstubs) that you place in an NSC region. The linker also emits an import library so the Non-secure image can link against the veneer addresses. Before the compiler-generated BXNS LR, the entry function clears every caller-saved register that does not carry the return value (from R0-R3 and R12, the flags, and FP registers as applicable). Callee-saved registers are restored as usual.
  • cmse_nonsecure_call on a function-pointer type makes a call use BLXNS. The compiler saves the registers that must survive, clears every register not used for arguments (callee-saved ones included, since Non-secure code can read them), and clears bit 0 of the target. cmse_nsfptr_create() and cmse_is_nsfptr() manage bit 0 of the pointer.
  • cmse_check_address_range(p, size, flags) and cmse_check_pointed_object() use the TT family. The flags include CMSE_NONSECURE, CMSE_MPU_READWRITE and CMSE_MPU_UNPRIV. The functions return NULL when the check fails. This defends against a confused-deputy attack, where Non-secure code passes a pointer into Secure memory. Check the whole range. Copy the data into Secure memory before using it, because Non-secure code (another thread or an interrupt) can change shared memory after the check.

Stack sealing (Arm security advisory, 2020). FNC_RETURN unstacking and EXC_RETURN with S=1 trust that the current Secure stack holds a frame from an earlier transition. Suppose a Secure stack holds no such frame, for example a fresh Secure thread stack or the Secure MSP when Secure code is idle. Non-secure code could then fake a function return or exception return. The PE would pop whatever words sit above the stack pointer and could resume Secure execution at an address Non-secure code influences. The integrity signature check covers only some exception-return paths, and only rejects data that does not happen to match it.

Mitigation: reserve the top 8 bytes of every Secure stack, write the seal value 0xFEF5EDA5 into both words, and start the stack pointer below the seal. A bogus return then pops the seal. It is not a valid integrity signature, and as a return address it points into the Execute Never system region, so the attempt faults instead of jumping. CMSIS provides __TZ_set_STACKSEAL_S() for this.

Coming from A-profile: This is the v8-M version of the rules for sanitising registers and validating pointers at an SMC boundary in OP-TEE or TF-A. The compiler takes over the register sanitising that the secure-world exit path (OP-TEE’s and BL31’s context switch) does in software on A-profile.

Checkpoint

Q4.5.1 A Secure entry function int read_key(uint8_t *out, size_t n) writes into out. A Non-secure caller passes out = 0x30001000, which is Secure RAM holding Secure data. What is the correct defence?

  • A. Validate with cmse_check_address_range(out, n, CMSE_NONSECURE | CMSE_MPU_READWRITE) (adding CMSE_MPU_UNPRIV if the caller is unprivileged), reject on NULL, and only then write
  • B. No check is needed, because the Non-secure MPU blocks the Secure write
  • C. Check only that out itself is Non-secure with one TT instruction
  • D. Mark out as volatile so the compiler emits Non-secure accesses
Answer

A. The entry function runs in Secure state with Secure permissions, so it can write Secure RAM. This is a confused-deputy problem, and the whole range must be checked as Non-secure and writable at the caller’s privilege. The tempting “NS MPU will block it” is wrong, because Secure accesses are checked against the Secure MPU and attribution, not the Non-secure MPU. Checking only the first address misses a range that runs into Secure memory.

Read more: ARM CMSE ACLE specification, cmse_check_address_range; TT instructions

Q4.5.2 Why does a cmse_nonsecure_entry function clear R0-R3, R12 and the flags (except return-value registers) before BXNS, when AAPCS already says those registers are caller-saved scratch?

  • A. The hardware faults with INVTRAN if those registers are non-zero at BXNS
  • B. Caller-saved registers can still hold Secure intermediate values such as keys and pointers, and LR, R0-R12 and the FP registers are not banked, so without clearing the Non-secure caller would read them
  • C. AAPCS requires a function to zero its scratch registers on return
  • D. Clearing them pre-fills the arguments for the next SG call
Answer

B. The general-purpose and FP registers are shared between Security states, so any Secure value left in them stays visible after BXNS. The compiler clears them to stop the leak. The first distractor is tempting because it sounds like a hardware rule, but BXNS performs no register-content check. INVTRAN concerns branches that are not transitions.

Read more: ARM CMSE ACLE specification, entry function return; BXNS

Q4.5.3 Which scenario is the 2020 stack-sealing issue, and how does the seal address it?

  • A. Non-secure code overflows PSP_NS into Secure RAM. The seal acts as a guard word that the PE checks
  • B. An SG instruction is accidentally present in Secure code. The seal marks the valid veneers
  • C. Secure code returns with BX instead of BXNS. The seal makes BX fault
  • D. Non-secure code issues a fake FNC_RETURN or Secure-frame EXC_RETURN while the current Secure stack holds no real frame, so the PE pops arbitrary words. With 0xFEF5EDA5 at the stack top, the pop yields neither a valid integrity signature nor an executable return address, so it faults
Answer

D. The hardware trusts that the current Secure stack holds a frame from a real earlier transition. Sealing an empty Secure stack makes any forged return pop the seal and fault. The stack-overflow distractor is tempting, but overflow is what MSPLIM and PSPLIM handle. The seal is not a guard word that the PE checks on every push.

Read more: Arm Armv8-M Secure stack sealing advisory (2020); CMSIS __TZ_set_STACKSEAL_S