Security Isolation
1. Purpose of Isolation
2. TrustZone Isolation
Arm TrustZone for Armv8-M is a hardware isolation technology integrated into the processor and the system bus. It divides the system into the Secure World and the Non-secure World:
Combined with the MPU, which provides privilege-level isolation within the same world, TrustZone enables a multi-layered protection scheme.
For more information, refer to TrustZone Technology Overview, as well as the RTL87x2G Security, TrustZone, and Memory documents.
TrustZone Overview
The following sections introduce the basic principles, key mechanisms, and secure software design considerations of Arm TrustZone for Armv8-M (hereinafter referred to as TrustZone). The content applies to all processors that are based on the Armv8-M / Armv8.1-M architecture and support the Security Extension. For chip-specific configuration and usage, refer to the chip documents listed in More Information.
With the growing popularity of IoT devices, more and more assets on a device need protection: device identity keys, user data, the firmware itself, and critical functions such as payment or access control. At the same time, the software running on devices is becoming increasingly complex, typically including an RTOS, wireless protocol stacks, third-party libraries, and application code. The larger the code base, the higher the likelihood of vulnerabilities.
If all code runs in the same execution environment, a single vulnerability anywhere may allow an attacker to read keys or tamper with critical logic. The idea behind TrustZone is to divide the system at the hardware level into two mutually isolated execution environments:
As a result, even if software in the Non-secure World is compromised, the attacker cannot directly access assets in the Secure World. In addition, because the Secure World has a small code base, it is easier to review and verify.

TrustZone Divides the System into the Secure World and the Non-secure World
TrustZone for Armv8-M is designed with the characteristics of microcontrollers in mind:
Working Principles
1. Security States and Processor Modes
In addition to the existing Thread mode and Handler mode, a processor that supports TrustZone adds an orthogonal dimension: the Secure state and the Non-secure state. The processor therefore has four operating combinations:
Processor Operating Combinations
| Secure State | Non-secure State | |
|---|---|---|
| Handler Mode | Secure Exception Handling (Privileged) | Non-secure Exception Handling (Privileged) |
| Thread Mode | Secure Thread (Privileged or Unprivileged) | Non-secure Thread (Privileged or Unprivileged) |
Note that the security state and the privilege level are two independent concepts:
The current security state of the processor depends on the security attribute of the address of the code being executed: the processor is in the Secure state when executing code from Secure memory, and in the Non-secure state when executing code from Non-secure memory.
2. Memory Security Attributes
2.1 Three Security Attributes
TrustZone classifies the entire address space (including code, data, and peripherals) according to three security attributes:
Memory Security Attributes
| Attribute | Description |
|---|---|
| Secure (S) | Accessible only by software in the Secure state. Holds Secure code, Secure data, and Secure peripherals. |
| Non-secure Callable (NSC) | A special Secure region that holds the entry points the Secure World exposes to the Non-secure World. Non-secure code cannot read or write this region, and can only branch to locations in it that begin with an SG (Secure Gateway) instruction. |
| Non-secure (NS) | Accessible by software in both the Secure and Non-secure states. Holds Non-secure code, data, and peripherals. |
Software in the Secure state can access both Secure and Non-secure resources; software in the Non-secure state can access only Non-secure resources. When Non-secure code accesses a Secure address, the hardware triggers a SecureFault (or HardFault), and the access does not take effect.
2.2 SAU and IDAU
The processor uses two units together to determine the security attribute of each address:
In the standard design of the Armv8-M architecture, for a given address, the processor queries both the SAU and the IDAU, and takes the more secure of the two attributes as the final result (S is higher than NSC, and NSC is higher than NS). Addresses not covered by any SAU region are treated as Secure. After reset, the SAU is disabled, and the entire address space is Secure by default; Secure software must explicitly define the Non-secure regions.

In the Standard Design, the SAU and IDAU Together Determine the Security Attribute of an Address
The SAU can be configured only by Secure software. Secure boot code usually completes the SAU configuration before branching to the Non-secure World. Because the SAU can be accessed only by Secure software, Non-secure software cannot modify the isolation boundary.
2.3 System-Level Security Extension
The SAU and IDAU only determine whether an access issued by the processor is Secure or Non-secure. To extend isolation to the entire system, the chip also needs support on the bus and peripheral side:
Note: The implementation of the IDAU is defined by the chip vendor, including the number of regions, whether it is configurable, and which bus masters it applies to. In different chips, the division of work between the SAU and IDAU may differ from the standard design described above. Refer to the documentation of the corresponding chip.
3. Security State Transitions
TrustZone allows direct function calls between Secure and Non-secure code, but strictly restricts the call paths to prevent Non-secure code from bypassing security checks.
3.1 Non-secure Code Calling Secure Functions
When Non-secure code calls a Secure service, it must go through an entry point in the NSC region:

Non-secure Code Calls a Secure Function through an NSC Entry
When Non-secure code branches to a Secure address, the hardware blocks the following cases and triggers a SecureFault:
An SG instruction is treated as a valid entry only when it is located in an NSC region. This prevents data or constants in Secure code that happen to have the same encoding as the SG instruction from being used as entry points by an attacker. In this way, the Non-secure World can enter only the interfaces explicitly exposed by the Secure World, and can neither branch into the middle of a Secure function nor execute any other Secure code.
In C, developers do not need to write SG and BXNS instructions by hand. CMSE (Cortex-M Security Extensions), defined by Arm, provides the corresponding compiler support:
/* Secure project: compile with the -mcmse option */
#include <arm_cmse.h>
/* Declare an entry function that can be called from the Non-secure World */
int32_t __attribute__((cmse_nonsecure_entry)) secure_add(int32_t a, int32_t b)
{
return a + b;
}
The compiler and linker automatically perform the following tasks:
The linker also generates an import library, which contains only the addresses and symbols of the entry functions. By linking against this import library, the Non-secure project can call Secure services just like ordinary functions, without needing the source code or binaries of the Secure project.
3.2 Secure Code Calling Non-secure Functions
In some scenarios, Secure code needs to call a Non-secure function, for example, calling a callback function registered by the Non-secure application after a Secure service completes. In this case, the BLXNS instruction is used:
In this way, Non-secure code can neither see nor tamper with the Secure World's return address. CMSE provides the cmse_nonsecure_call attribute to declare Non-secure function pointers, and provides cmse_nsfptr_create() to construct such pointers.
typedef void __attribute__((cmse_nonsecure_call)) ns_callback_t(uint32_t event);
static ns_callback_t *g_ns_callback;
void __attribute__((cmse_nonsecure_entry)) secure_register_callback(ns_callback_t *cb)
{
g_ns_callback = cmse_nsfptr_create(cb);
}
Before calling a Non-secure function, the compiler automatically clears Secure data from the general-purpose registers to prevent information leakage.
3.3 Interrupts and Exceptions
Each external interrupt can be configured as a Secure or Non-secure interrupt, and the configuration registers can be accessed only by Secure software:
Regardless of the processor's state when an interrupt occurs, the hardware automatically switches to the interrupt's target state. When Secure code is interrupted by a Non-secure interrupt, the hardware pushes the Secure context onto the Secure stack and clears the general-purpose registers, so the Non-secure interrupt service routine cannot see the register contents of the Secure World. Secure software can also use AIRCR.PRIS to restrict Non-secure interrupt priorities to the lower half of the priority range, ensuring that critical Secure interrupts cannot be masked or preempted by Non-secure software.
4. Banked Resources
To allow the two worlds to run independently of each other, many resources in the processor are banked by security state, that is, the Secure and Non-secure states each have their own copy:
Main Banked Resources
| Resource | Description |
|---|---|
| Stack Pointers | Four in total: MSP_S, PSP_S, MSP_NS, and PSP_NS. The stacks of the two worlds are completely separate. |
| Stack Limit Registers | Each stack pointer has a corresponding stack limit register for detecting stack overflow. |
| Interrupt Mask Registers | PRIMASK, FAULTMASK, and BASEPRI are banked. With AIRCR.PRIS set, disabling interrupts in Non-secure software does not mask high-priority Secure interrupts. |
| Vector Table Offset Register (VTOR) | The Secure and Non-secure states each have their own vector table. |
| MPU | The Secure MPU and Non-secure MPU are configured by their respective software. |
| SysTick | The Secure and Non-secure states can each have their own SysTick timer. |
| Some System Control Registers | Such as CONTROL and some SCB registers. |
Note: Some resources are available only in Armv8-M Mainline (such as Cortex-M33) and Armv8.1-M (such as Cortex-M55), for example the stack limit registers for Non-secure stacks, FAULTMASK, BASEPRI, and SecureFault. In Armv8-M Baseline (such as Cortex-M23), security violations always trigger a HardFault.
Secure software can access the Non-secure banked resources through Non-secure alias addresses and dedicated instructions (such as MRS/MSR MSP_NS), for example, to set up the vector table and stack pointer for the Non-secure World during startup. Non-secure software, however, cannot access the Secure banked resources.
Software Design
TrustZone provides the isolation capability, but whether the system is truly secure also depends on the design of the Secure World software. The following are some basic principles to follow when designing Secure services.
1. Minimize the Secure Interface
Every NSC entry is a channel from the Non-secure World into the Secure World, and also a potential attack surface. Recommendations:
2. Validate All Input from the Non-secure World
Secure functions must treat all parameters passed in from the Non-secure World as untrusted, especially pointers:
int32_t __attribute__((cmse_nonsecure_entry))
secure_process(const uint8_t *ns_in, size_t len, uint8_t *ns_out)
{
uint8_t buf[64];
if ((len == 0) || (len > sizeof(buf)))
{
return -1;
}
/* Verify that the entire input and output buffers are accessible by the Non-secure caller */
if ((cmse_check_address_range((void *)ns_in, len, CMSE_NONSECURE | CMSE_MPU_READ) == NULL) ||
(cmse_check_address_range(ns_out, len, CMSE_NONSECURE | CMSE_MPU_READWRITE) == NULL))
{
return -1;
}
memcpy(buf, ns_in, len); /* Copy to the Secure stack first to avoid TOCTOU */
/* ... Process buf in Secure memory ... */
memcpy(ns_out, buf, len);
/* Clear temporary data. In real projects, use a clearing function that cannot be optimized away by the compiler (for example, one based on a volatile pointer) */
memset(buf, 0, sizeof(buf));
return (int32_t)len;
}
3. Handle Non-secure Callbacks with Care
When Secure code calls a Non-secure function, the Non-secure code may take the opportunity to re-enter the Secure interface or modify shared data. Recommendations:
4. Working with an RTOS
When an RTOS runs in the Non-secure World, multiple Non-secure tasks may each call Secure services. Therefore, the Secure World needs to maintain an independent Secure stack (Secure context) for each task that calls Secure services, and save and restore it along with the task during context switches. The Armv8-M ports of mainstream RTOSs (such as FreeRTOS) provide this mechanism. Developers need to follow the requirements of the RTOS in use and allocate a Secure context for a task before it calls Secure services.
In addition, Secure functions should be kept as short as possible, and should not disable interrupts or block for long periods, so as not to affect the real-time performance of the Non-secure World.
5. Debug Security
Debug interfaces can directly read and write memory and registers, and must be controlled accordingly. The TrustZone architecture supports separate authorization for Secure debug and Non-secure debug: production devices usually disable Secure debug, and enable Non-secure debug only when needed, or enable all debug after authentication. For the debug port protection mechanism of a chip, refer to the security documentation of the corresponding chip.
Application Scenarios
Typical TrustZone Application Scenarios
| Scenario | Responsibilities of the Secure World |
|---|---|
| Key Management and Crypto Services | Stores device private keys and symmetric keys, and exposes only interfaces such as signing, encryption/decryption, and key derivation. The keys themselves never leave the Secure World. |
| Secure Storage | Encrypts or integrity-protects sensitive data (certificates, configurations, user credentials) before storing it. The storage area is accessible only by the Secure World. |
| Device Authentication | Uses the identity key in the Secure World to authenticate with the cloud or accessories. Non-secure applications receive only the authentication result. |
| Firmware Update Verification | Verifies the signature and version of new firmware in the Secure World, preventing tampered or older firmware from being installed. |
| Secure Peripherals | Assigns peripherals such as watchdogs, secure timers, and crypto engines to the Secure World, preventing misuse or malicious control by Non-secure software. |
| Third-party Code Isolation | Places protocol stacks or application libraries from not fully trusted sources in the Non-secure World, preventing their vulnerabilities from endangering critical assets. |
Related Mechanisms
TrustZone mainly addresses runtime isolation. A complete device security solution also requires other mechanisms working together:
TrustZone and Other Security Mechanisms
| Mechanism | Relationship with TrustZone |
|---|---|
| Secure Boot | Ensures that the Secure World code being run and the isolation configuration itself are trustworthy. Without secure boot, an attacker could simply replace the Secure firmware, rendering TrustZone isolation meaningless. |
| MPU | Partitions access rights by privilege level within the same world, forming a second layer of isolation together with TrustZone. |
| Flash Encryption | Protects code and data stored in Flash from being read out directly. It addresses the security of data at rest and complements the runtime isolation of TrustZone. |
| Secure Key Storage (such as eFuse/OTP) | Provides hardware-level key storage, typically used with TrustZone so that keys can be used only by the Secure World or the hardware crypto engine. |
| Debug Port Protection | Prevents attackers from bypassing software isolation and directly accessing memory through the debug interface. |
More Information
Glossary
| Term | Description |
|---|---|
| BLXNS | Branch with Link and Exchange to Non-secure state; used when Secure code calls a Non-secure function. |
| BXNS | Branch and Exchange to Non-secure state; used when a Secure function returns to the Non-secure World. |
| CMSE | Cortex-M Security Extensions, compiler extensions defined by Arm for developing TrustZone software. |
| FNC_RETURN | Special value written to LR when Secure code calls a Non-secure function. When the Non-secure function returns, the hardware uses it to switch back to the Secure state. |
| IDAU | Implementation Defined Attribution Unit, an address security attribution unit implemented by the chip vendor. |
| NSC | Non-secure Callable, a special Secure region that holds Secure entry points. |
| SAU | Security Attribution Unit, a programmable address security attribution unit inside the processor. |
| SG | Secure Gateway, the instruction that serves as the only valid entry point for Non-secure code into the Secure World. |
| TOCTOU | Time-of-Check to Time-of-Use, a security issue caused by data being modified between the check and the use. |
| TT | Test Target, an instruction used to query the security attribute and access permissions of an address. |
| Veneer | A branch stub located in the NSC region that begins with an SG instruction, automatically generated by the toolchain. |
苏公网安备32059002006558号
苏ICP备10062199号-8