Skip to content

Commit 8014ffa

Browse files
committed
Merge branch 'fix/esp_tee_aes_sha_prot' into 'master'
fix(esp_tee): Protect the AES/SHA clock registers from REE access Closes IDF-8954 and IDF-7188 See merge request espressif/esp-idf!36783
2 parents aa42423 + 873409d commit 8014ffa

File tree

22 files changed

+198
-195
lines changed

22 files changed

+198
-195
lines changed

components/esp_system/port/soc/esp32c6/clk.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,11 @@ __attribute__((weak)) void esp_perip_clk_init(void)
291291
periph_ll_disable_clk_set_rst(PERIPH_ASSIST_DEBUG_MODULE);
292292
#endif
293293
periph_ll_disable_clk_set_rst(PERIPH_RSA_MODULE);
294+
#if !CONFIG_SECURE_ENABLE_TEE
295+
// NOTE: [ESP-TEE] The TEE is responsible for the AES and SHA peripherals
294296
periph_ll_disable_clk_set_rst(PERIPH_AES_MODULE);
295297
periph_ll_disable_clk_set_rst(PERIPH_SHA_MODULE);
298+
#endif
296299
periph_ll_disable_clk_set_rst(PERIPH_ECC_MODULE);
297300
periph_ll_disable_clk_set_rst(PERIPH_HMAC_MODULE);
298301
periph_ll_disable_clk_set_rst(PERIPH_DS_MODULE);

components/esp_tee/include/esp_tee.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ typedef struct {
4343
uint32_t magic_word;
4444
uint32_t api_major_version;
4545
uint32_t api_minor_version;
46-
uint32_t reserved[2];
46+
uint32_t reserved[3];
4747
/* TEE-related fields */
48-
void *s_entry_addr;
4948
void *s_int_handler;
5049
/* REE-related fields */
5150
void *ns_entry_addr;
@@ -85,14 +84,12 @@ uint32_t esp_tee_service_call_with_noniram_intr_disabled(int argc, ...);
8584

8685
#if !(__DOXYGEN__)
8786
/* Offsets of some values in esp_tee_config_t that are used by assembly code */
88-
#define ESP_TEE_CFG_OFFS_S_ENTRY_ADDR 0x14
8987
#define ESP_TEE_CFG_OFFS_S_INTR_HANDLER 0x18
9088
#define ESP_TEE_CFG_OFFS_NS_ENTRY_ADDR 0x1C
9189
#define ESP_TEE_CFG_OFFS_NS_INTR_HANDLER 0x20
9290

9391
#if !defined(__ASSEMBLER__)
9492
/* Check the offsets are correct using the C compiler */
95-
ESP_STATIC_ASSERT(offsetof(esp_tee_config_t, s_entry_addr) == ESP_TEE_CFG_OFFS_S_ENTRY_ADDR, "offset macro is wrong");
9693
ESP_STATIC_ASSERT(offsetof(esp_tee_config_t, s_int_handler) == ESP_TEE_CFG_OFFS_S_INTR_HANDLER, "offset macro is wrong");
9794
ESP_STATIC_ASSERT(offsetof(esp_tee_config_t, ns_entry_addr) == ESP_TEE_CFG_OFFS_NS_ENTRY_ADDR, "offset macro is wrong");
9895
ESP_STATIC_ASSERT(offsetof(esp_tee_config_t, ns_int_handler) == ESP_TEE_CFG_OFFS_NS_INTR_HANDLER, "offset macro is wrong");

components/esp_tee/scripts/esp32c6/sec_srv_tbl_default.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ secure_services:
208208
type: IDF
209209
function: esp_sha_write_digest_state
210210
args: 2
211+
- id: 132
212+
type: IDF
213+
function: esp_sha_enable_periph_clk
214+
args: 1
211215
# ID: 134-149 (16) - eFuse
212216
- family: efuse
213217
entries:

components/esp_tee/src/esp_secure_service_wrapper.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ void __wrap_esp_sha_write_digest_state(esp_sha_type sha_type, void *digest_state
228228
esp_tee_service_call(3, SS_ESP_SHA_WRITE_DIGEST_STATE, sha_type, digest_state);
229229
}
230230

231+
void __wrap_esp_sha_enable_periph_clk(bool enable)
232+
{
233+
esp_tee_service_call(2, SS_ESP_SHA_ENABLE_PERIPH_CLK, enable);
234+
}
235+
231236
/* ---------------------------------------------- MMU HAL ------------------------------------------------- */
232237

233238
void IRAM_ATTR __wrap_mmu_hal_map_region(uint32_t mmu_id, mmu_target_t mem_type, uint32_t vaddr, uint32_t paddr, uint32_t len, uint32_t *out_len)

components/esp_tee/src/esp_tee_config.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -25,9 +25,9 @@ esp_tee_config_t esp_tee_app_config __attribute__((section(".esp_tee_app_cfg")))
2525
.api_major_version = ESP_TEE_API_MAJOR_VER,
2626
.api_minor_version = ESP_TEE_API_MINOR_VER,
2727

28-
/* .s_entry_addr and .s_intr_handler are NULL in the
29-
app binary, but will be written by the TEE before it loads the binary
30-
*/
28+
/* s_intr_handler is NULL in the REE image, but will be written by
29+
* the TEE before it loads the binary
30+
*/
3131

3232
.ns_int_handler = &_tee_interrupt_handler,
3333
.ns_entry_addr = &_u2m_switch,

components/esp_tee/subproject/main/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ set(srcs "core/esp_tee_init.c"
2020

2121
# Arch specific implementation for TEE
2222
list(APPEND srcs "arch/${arch}/esp_tee_vectors.S"
23-
"arch/${arch}/esp_tee_vector_table.S"
24-
"arch/${arch}/esp_tee_secure_entry.S")
23+
"arch/${arch}/esp_tee_vector_table.S")
2524

2625
# SoC specific implementation for TEE
2726
list(APPEND srcs "soc/${target}/esp_tee_secure_sys_cfg.c"
@@ -78,7 +77,9 @@ list(APPEND srcs "common/esp_app_desc_tee.c")
7877
idf_component_register(SRCS ${srcs}
7978
INCLUDE_DIRS ${include})
8079

81-
set_source_files_properties("core/esp_secure_services.c" PROPERTIES COMPILE_FLAGS -Wno-deprecated)
80+
# TODO: Currently only -Og optimization level works correctly at runtime
81+
set_source_files_properties("core/esp_secure_dispatcher.c" PROPERTIES COMPILE_FLAGS "-Og")
82+
8283
include(${CMAKE_CURRENT_LIST_DIR}/ld/esp_tee_ld.cmake)
8384

8485
# esp_app_desc_t configuration structure for TEE: Linking symbol and trimming project version and name

components/esp_tee/subproject/main/arch/riscv/esp_tee_secure_entry.S

Lines changed: 0 additions & 42 deletions
This file was deleted.

components/esp_tee/subproject/main/arch/riscv/esp_tee_vectors.S

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -12,6 +12,7 @@
1212

1313
#include "riscv/encoding.h"
1414
#include "riscv/rvruntime-frames.h"
15+
#include "esp_private/vectors_const.h"
1516

1617
#include "esp_tee.h"
1718
#include "sdkconfig.h"
@@ -25,9 +26,12 @@
2526
.equ ECALL_U_MODE, 0x8
2627
.equ ECALL_M_MODE, 0xb
2728
.equ TEE_APM_INTR_MASK_0, 0x00300000
28-
.equ TEE_APM_INTR_MASK_1, 0x000000F8
29+
.equ TEE_APM_INTR_MASK_1, 0x000000f8
30+
.equ TEE_INTR_DELEG_MASK, 0xffffbfff
2931

3032
.global esp_tee_global_interrupt_handler
33+
.global esp_tee_service_dispatcher
34+
3135

3236
.section .data
3337
.align 4
@@ -177,15 +181,18 @@ _panic_handler:
177181
addi sp, sp, -16
178182
sw t0, 0(sp)
179183

180-
/* Check whether the exception is an M-mode ecall */
184+
/* Read mcause */
181185
csrr t0, mcause
182-
xori t0, t0, ECALL_M_MODE
183-
beqz t0, _machine_ecall
186+
li t1, VECTORS_MCAUSE_INTBIT_MASK | VECTORS_MCAUSE_REASON_MASK
187+
and t0, t0, t1
188+
189+
/* Check whether the exception is an M-mode ecall */
190+
li t1, ECALL_M_MODE
191+
beq t0, t1, _machine_ecall
184192

185193
/* Check whether the exception is an U-mode ecall */
186-
csrr t0, mcause
187-
xori t0, t0, ECALL_U_MODE
188-
beqz t0, _user_ecall
194+
li t1, ECALL_U_MODE
195+
beq t0, t1, _user_ecall
189196

190197
/* Restore t0 from the stack */
191198
lw t0, 0(sp)
@@ -250,6 +257,10 @@ _return_from_exception:
250257
_ecall_handler:
251258
/* M-mode ecall handler */
252259
_machine_ecall:
260+
/* Set the privilege mode to transition to after mret to U-mode */
261+
li t0, MSTATUS_MPP
262+
csrc mstatus, t0
263+
253264
/* Check whether this is the first M-mode ecall (see esp_tee_init) and skip context restoration */
254265
lui t0, ESP_TEE_M2U_SWITCH_MAGIC
255266
beq a1, t0, _skip_ctx_restore
@@ -267,15 +278,10 @@ _machine_ecall:
267278
restore_general_regs RV_STK_FRMSZ
268279
csrrw a0, mscratch, zero
269280

270-
/* This point is reached only after the first M-mode ecall, never again (see esp_tee_init) */
271281
_skip_ctx_restore:
272282
/* Copy the ra register to mepc which contains the user app entry point (i.e. call_start_cpu0) */
273283
csrw mepc, ra
274284

275-
/* Set the privilege mode to transition to after mret to U-mode */
276-
li t3, MSTATUS_MPP
277-
csrc mstatus, t3
278-
279285
/* Jump to the REE */
280286
mret
281287

@@ -291,28 +297,34 @@ _user_ecall:
291297
lw t0, 0(sp)
292298
addi sp, sp, 16
293299

294-
/* This point is reached after a secure service call is issued from the REE */
295-
/* Save register context and the mepc */
300+
/* This point is reached when a secure service call is issued from the REE */
301+
/* Save register context and mepc */
296302
save_general_regs RV_STK_FRMSZ
297303
save_mepc
298304

299-
/* Saving the U-mode (i.e. REE) stack pointer */
305+
/* Save the U-mode (i.e. REE) stack pointer */
300306
la t0, _ns_sp
301307
sw sp, 0(t0)
302308

303-
/* Switching to the M-mode (i.e. TEE) stack */
309+
/* Switch to the M-mode (i.e. TEE) stack */
304310
la sp, _tee_stack
305311

306-
/* Load the TEE entry point (see sec_world_entry) in the mepc */
307-
la t2, esp_tee_app_config
308-
lw t2, ESP_TEE_CFG_OFFS_S_ENTRY_ADDR(t2)
309-
csrw mepc, t2
312+
/* Disable the U-mode delegation of all interrupts */
313+
csrwi mideleg, 0
310314

311-
/* Set the privilege mode to transition to after mret to M-mode */
312-
li t3, MSTATUS_MPP
313-
csrs mstatus, t3
315+
/* Enable interrupts */
316+
csrsi mstatus, MSTATUS_MIE
314317

315-
mret
318+
/* Jump to the secure service dispatcher */
319+
jal esp_tee_service_dispatcher
320+
321+
/* Enable the U-mode delegation of all interrupts (except the TEE secure interrupt) */
322+
li t0, TEE_INTR_DELEG_MASK
323+
csrs mideleg, t0
324+
325+
/* Fire an M-ecall */
326+
mv a1, zero
327+
ecall
316328

317329
/* This point is reached after servicing a U-mode interrupt occurred
318330
* while executing a secure service */
@@ -333,7 +345,7 @@ _rtn_from_ns_int:
333345

334346
/* Restore register context and resume the secure service */
335347
restore_mepc
336-
restore_general_regs
348+
restore_general_regs RV_STK_FRMSZ
337349

338350
mret
339351

@@ -347,7 +359,7 @@ _rtn_from_ns_int:
347359
_tee_ns_intr_handler:
348360
/* Start by saving the general purpose registers and the PC value before
349361
* the interrupt happened. */
350-
save_general_regs
362+
save_general_regs RV_STK_FRMSZ
351363
save_mepc
352364

353365
/* Though it is not necessary we save GP and SP here.
@@ -357,7 +369,7 @@ _tee_ns_intr_handler:
357369
/* As gp register is not saved by the macro, save it here */
358370
sw gp, RV_STK_GP(sp)
359371
/* Same goes for the SP value before trapping */
360-
addi t0, sp, CONTEXT_SIZE /* restore sp with the value when interrupt happened */
372+
addi t0, sp, RV_STK_FRMSZ /* restore sp with the value when interrupt happened */
361373
/* Save SP */
362374
sw t0, RV_STK_SP(sp)
363375

@@ -395,8 +407,8 @@ _tee_ns_intr_handler:
395407
csrw mscratch, t0
396408

397409
/* Enable the U-mode interrupt delegation (except for the TEE secure interrupt) */
398-
li t0, 0xffffbfff
399-
csrw mideleg, t0
410+
li t0, TEE_INTR_DELEG_MASK
411+
csrs mideleg, t0
400412

401413
/* Place magic bytes in all the general registers */
402414
store_magic_general_regs
@@ -413,7 +425,7 @@ _tee_ns_intr_handler:
413425
_tee_s_intr_handler:
414426
/* Start by saving the general purpose registers and the PC value before
415427
* the interrupt happened. */
416-
save_general_regs
428+
save_general_regs RV_STK_FRMSZ
417429
save_mepc
418430

419431
/* Though it is not necessary we save GP and SP here.
@@ -423,7 +435,7 @@ _tee_s_intr_handler:
423435
/* As gp register is not saved by the macro, save it here */
424436
sw gp, RV_STK_GP(sp)
425437
/* Same goes for the SP value before trapping */
426-
addi t0, sp, CONTEXT_SIZE /* restore sp with the value when interrupt happened */
438+
addi t0, sp, RV_STK_FRMSZ /* restore sp with the value when interrupt happened */
427439
/* Save SP */
428440
sw t0, RV_STK_SP(sp)
429441

@@ -457,7 +469,7 @@ _save_reg_ctx:
457469
_continue:
458470
/* Before doing anything preserve the stack pointer */
459471
mv s11, sp
460-
/* Switching to the TEE interrupt stack */
472+
/* Switch to the TEE interrupt stack */
461473
la sp, _tee_intr_stack
462474
/* If this is a non-nested interrupt, SP now points to the interrupt stack */
463475

@@ -527,7 +539,7 @@ _intr_hdlr_exec:
527539
mv sp, s11
528540

529541
restore_mepc
530-
restore_general_regs
542+
restore_general_regs RV_STK_FRMSZ
531543
/* exit, this will also re-enable the interrupts */
532544
mret
533545

0 commit comments

Comments
 (0)