fix(core): make the GFXMMU frame buffers writable by the T3W1 application
What changed, and why it matters
This commit fixes a display bug on the Trezor T3W1 hardware where the application could not directly write to the screen's frame buffer. Because of a TrustZone security-controller misconfiguration, CPU-drawn pixels were silently ignored, so only graphics copied by the DMA engine showed up. The patch moves the privilege setup to the correct early initialization stage and removes a broken runtime path that could not work once the controller was locked. It also hardens the code so future mistakes cause compile-time or runtime failures instead of silent no-ops.
Treat this as a security-hardening bug fix. Verify that T3W1 firmware built from this commit correctly renders anti-aliased edges, circles, and blur under both light and dark themes. Review whether the silent-failure behavior could have masked any other access-control misconfigurations elsewhere in the TrustZone setup, and consider auditing other late TZSC changes for similar lock-window issues.
Security signals we found
Silent access-control failure: unprivileged access to a privileged TrustZone resource produced no fault, only zero reads and ignored writes
Privilege configuration window missed: TZSC locked before intended unprivileging, leaving resource over-privileged from application's view
Defense-in-depth improvement: moved TZSC privilege setters to secure-only build, preventing non-secure code from compiling a call that would silently fail at runtime
Added explicit TZSC lock check (tzsc_ensure_unlocked) to turn silent ignored writes into a fatal error
Correct ordering of SRAM vs GFXMMU unprivileging to maintain legal LTDC refresh bus transactions
Evidence from the diff
On STM32U5A9/G9 (T3W1), the GFXMMU frame buffer is governed by the GTZC TZSC privilege attribute. The secure monitor locks TZSC at the end of tz_init(), after which writes to TZSC registers are silently ignored (RM0456 5.4.2). The previous code tried to make the GFXMMU unprivileged later, in display_set_unpriv_access() called from the kernel, so the change was refused and CPU writes to the virtual frame buffer were dropped (reads returned zero). Only DMA2D (a secure bus master) succeeded. The fix configures the physical frame buffers and GFXMMU as unprivileged inside tz_init() before the lock, in the correct order (SRAM buffers first, then GFXMMU, so LTDC refresh transactions remain legal). It removes display_set_unpriv_access() and its driver implementations, moves the peripheral privilege helpers to a secure-only header (sec/tz_periph.h), adds a TZSC-unlocked check that fails loudly, and makes secure AES unprivileged path conditional on SECURE_MODE.
Changed components
core/embed/sec/trustzone/stm32u5/tz_init.ccore/embed/sec/trustzone/stm32u5/tz_periph.ccore/embed/sec/trustzone/inc/sec/tz_periph.hcore/embed/sec/trustzone/build.rscore/embed/sys/trustzone/stm32u5/trustzone.ccore/embed/sys/trustzone/inc/sys/trustzone.hcore/embed/io/display/ltdc_dsi/display_fb.ccore/embed/io/display/i8080/display_fb.ccore/embed/io/display/none/display_driver.ccore/embed/io/display/vg-2864/display_driver.ccore/embed/io/display/inc/io/display.hcore/embed/projects/kernel/main.ccore/embed/sec/secure_aes/stm32u5/secure_aes_unpriv.cInspect captured patch +153 / −89
### core/embed/io/display/i8080/display_fb.c
@@ -75,18 +75,6 @@ static __attribute__((section(".fb2"),
physical_frame_buffer_1[PHYSICAL_FRAME_BUFFER_SIZE];
#endif
-#ifdef USE_TRUSTZONE
-void display_set_unpriv_access(bool unpriv) {
- tz_set_sram_unpriv((uint32_t)physical_frame_buffer_0,
- PHYSICAL_FRAME_BUFFER_SIZE, unpriv);
-
-#if (FRAME_BUFFER_COUNT > 1)
- tz_set_sram_unpriv((uint32_t)physical_frame_buffer_1,
- PHYSICAL_FRAME_BUFFER_SIZE, unpriv);
-#endif
-}
-#endif // USE_TRUSTZONE
-
void display_fb_init(void) {
display_driver_t *drv = &g_display_driver;
### core/embed/io/display/inc/io/display.h
@@ -90,10 +90,6 @@ void display_suspend(display_wakeup_params_t *wakeup_params);
void display_resume(const display_wakeup_params_t *wakeup_params);
#endif // USE_SUSPEND
-// Allows unprivileged access to the display framebuffer from
-// perspective of the GTZC (Global TrustZone Controller).
-void display_set_unpriv_access(bool unpriv);
-
#endif // KERNEL_MODE
// Sets display backlight level ranging from 0 (off)..255 (maximum).
### core/embed/io/display/ltdc_dsi/display_fb.c
@@ -45,36 +45,6 @@ __attribute__((section(".fb2"),
physical_frame_buffer_1[ALIGNED_PHYSICAL_FRAME_BUFFER_SIZE];
#endif
-#ifdef USE_TRUSTZONE
-void display_set_unpriv_access(bool unpriv) {
- // To allow unprivileged access both GFXMMU virtual buffers area and
- // underlying SRAM region must be configured as unprivileged.
-
- // Order of GFXMMU and SRAM unprivileged access configuration is important
- // to avoid the situation the virtual frame buffer has lower privileges
- // than underlying frame buffer in physical memory so LTDC could not
- // refresh the display properly.
-
-#ifdef DISPLAY_GFXMMU
- if (!unpriv) {
- tz_set_gfxmmu_unpriv(unpriv);
- }
-#endif
-
- tz_set_sram_unpriv((uint32_t)physical_frame_buffer_0,
- PHYSICAL_FRAME_BUFFER_SIZE, unpriv);
-
- tz_set_sram_unpriv((uint32_t)physical_frame_buffer_1,
- PHYSICAL_FRAME_BUFFER_SIZE, unpriv);
-
-#ifdef DISPLAY_GFXMMU
- if (unpriv) {
- tz_set_gfxmmu_unpriv(unpriv);
- }
-#endif
-}
-#endif // USE_TRUSTZONE
-
// Returns the pointer to the physical frame buffer (0.. FRAME_BUFFER_COUNT-1)
// Returns NULL if the framebuffer index is out of range.
static uint8_t *get_fb_ptr(int16_t index) {
### core/embed/io/display/none/display_driver.c
@@ -66,8 +66,6 @@ void display_deinit(display_content_mode_t mode) {
drv->initialized = false;
}
-void display_set_unpriv_access(bool unpriv) {}
-
bool display_set_backlight(uint8_t level) {
display_driver_t *drv = &g_display_driver;
### core/embed/io/display/vg-2864/display_driver.c
@@ -333,12 +333,6 @@ void display_deinit(display_content_mode_t mode) {
drv->initialized = false;
}
-#ifdef USE_TRUSTZONE
-void display_set_unpriv_access(bool unpriv) {
- tz_set_sram_unpriv((uint32_t)g_framebuf, FRAME_BUFFER_SIZE, unpriv);
-}
-#endif // USE_TRUSTZONE
-
bool display_set_backlight(uint8_t level) {
display_driver_t *drv = &g_display_driver;
### core/embed/projects/kernel/main.c
@@ -172,10 +172,6 @@ void drivers_init() {
display_init(DISPLAY_JUMP_BEHAVIOR);
-#ifdef USE_TRUSTZONE
- display_set_unpriv_access(true);
-#endif
-
#ifdef SECURE_MODE
#ifdef USE_OEM_KEYS_CHECK
option_bytes_check_oem_keys();
### core/embed/sec/secure_aes/stm32u5/secure_aes_unpriv.c
@@ -106,14 +106,14 @@ __attribute((no_stack_protector)) void saes_unpriv_callback(void) {
// -----------------------------------------------------------------------
// Code running privileged mode
-#if defined(USE_APPLETS) && defined(KERNEL_MODE)
+#if defined(USE_APPLETS) && defined(SECURE_MODE)
#include <sys/coreapp.h>
#include <sys/mpu.h>
#include <sys/systask.h>
#ifdef USE_TRUSTZONE
-#include <sys/trustzone.h>
+#include <sec/tz_periph.h>
#endif
static void *secure_aes_unpriv_applet = NULL;
@@ -182,4 +182,4 @@ secbool secure_aes_unpriv_encrypt(const uint8_t *input, size_t size,
return retval;
}
-#endif // USE_APPLETS && KERNEL_MODE
+#endif // USE_APPLETS && SECURE_MODE
### core/embed/sec/trustzone/build.rs
@@ -9,6 +9,7 @@ pub fn def_module(lib: &mut CLibrary) -> Result<()> {
lib.add_source("trustzone/unix/tz_init.c");
} else if cfg!(feature = "mcu_stm32") {
lib.add_source("trustzone/stm32u5/tz_init.c");
+ lib.add_source("trustzone/stm32u5/tz_periph.c");
} else {
bail_unsupported!();
}
### core/embed/sec/trustzone/inc/sec/tz_periph.h
@@ -0,0 +1,46 @@
+/*
+ * This file is part of the Trezor project, https://trezor.io/
+ *
+ * Copyright (c) SatoshiLabs
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <trezor_types.h>
+
+#ifdef SECURE_MODE
+
+// The secure monitor locks the TZSC configuration until
+// reset at the end of `tz_init()`; a call after the lock is a fatal error,
+// so in the secmon layout these may only be used during `tz_init()`. They
+// are not available to non-secure builds.
+
+#ifndef SECMON
+// Sets unprivileged access to the SAES peripheral.
+void tz_set_saes_unpriv(bool unpriv);
+
+// Sets unprivileged access to the TAMP peripheral.
+void tz_set_tamper_unpriv(bool unpriv);
+#endif
+
+// Sets unprivileged access to the GFXMMU peripheral.
+//
+// The GFXMMU's privilege attribute is also carried by its own bus-master
+// transactions (the LTDC refresh), so the physical frame buffers must be
+// unprivileged before the GFXMMU is made unprivileged.
+void tz_set_gfxmmu_unpriv(bool unpriv);
+
+#endif // SECURE_MODE
### core/embed/sec/trustzone/stm32u5/tz_init.c
@@ -28,6 +28,7 @@
#include <sys/trustzone.h>
#include <sec/tz_init.h>
+#include <sec/tz_periph.h>
#define SAU_INIT_CTRL_ENABLE 1
#define SAU_INIT_CTRL_ALLNS 0
@@ -209,6 +210,27 @@ static void tz_configure_flash(void) {
HAL_FLASHEx_ConfigBBAttributes(&flash_bb);
}
+#if defined(SECMON) || defined(USE_APPLETS)
+// The unprivileged application draws into the frame buffers. Unprivilege
+// the physical buffers first, so that the GFXMMU's own bus transactions
+// (the LTDC refresh, see RM0456 3.6.3) stay legal, then the GFXMMU itself.
+// The MPU keeps controlling which buffer the application may touch.
+//
+// Only images hosting an applet need it: the kernel, or the secure monitor
+// on behalf of the non-secure kernel.
+static void tz_configure_display_fb(void) {
+#if defined(FB1_RAM_START) && FB1_RAM_SIZE > 0
+ tz_set_sram_unpriv(FB1_RAM_START, FB1_RAM_SIZE, true);
+#endif
+#if defined(FB2_RAM_START) && FB2_RAM_SIZE > 0
+ tz_set_sram_unpriv(FB2_RAM_START, FB2_RAM_SIZE, true);
+#endif
+#ifdef DISPLAY_GFXMMU
+ tz_set_gfxmmu_unpriv(true);
+#endif
+}
+#endif // SECMON || USE_APPLETS
+
#ifndef SECMON
void tz_init(void) {
tz_configure_arm();
@@ -222,6 +244,10 @@ void tz_init(void) {
HAL_GTZC_TZSC_ConfigPeriphAttributes(
GTZC_PERIPH_ALL, GTZC_TZSC_PERIPH_SEC | GTZC_TZSC_PERIPH_PRIV);
+#ifdef USE_APPLETS
+ tz_configure_display_fb();
+#endif
+
tz_enable_illegal_access_interrupt();
}
#endif // !SECMON
@@ -405,6 +431,10 @@ void tz_init(void) {
TROPIC01_SPI_GTZC_PERIPH, GTZC_TZSC_PERIPH_SEC | GTZC_TZSC_PERIPH_PRIV);
#endif
+ // The GFXMMU privilege attribute is locked below together with the rest
+ // of the TZSC configuration, so it has to be decided here.
+ tz_configure_display_fb();
+
tz_enable_illegal_access_interrupt();
// Lock SAU configuration & AIRCR register against further modifications
### core/embed/sec/trustzone/stm32u5/tz_periph.c
@@ -0,0 +1,63 @@
+/*
+ * This file is part of the Trezor project, https://trezor.io/
+ *
+ * Copyright (c) SatoshiLabs
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifdef SECURE_MODE
+
+#include <trezor_bsp.h>
+#include <trezor_rtl.h>
+
+#include <sec/tz_periph.h>
+
+// Once the TZSC configuration is locked (the secure monitor does so at the
+// end of `tz_init()`), writes to it are silently ignored. Fail loudly instead.
+static void tzsc_ensure_unlocked(void) {
+ ensure(sectrue * ((GTZC_TZSC1->CR & GTZC_TZSC_CR_LCK_Msk) == 0),
+ "TZSC locked");
+}
+
+// Not available in the secure monitor: it has no use for them and could only
+// call them before the lock anyway.
+#ifndef SECMON
+
+void tz_set_saes_unpriv(bool unpriv) {
+ tzsc_ensure_unlocked();
+ HAL_GTZC_TZSC_ConfigPeriphAttributes(
+ GTZC_PERIPH_SAES,
+ unpriv ? GTZC_TZSC_PERIPH_NPRIV : GTZC_TZSC_PERIPH_PRIV);
+}
+
+void tz_set_tamper_unpriv(bool unpriv) {
+ tzsc_ensure_unlocked();
+ HAL_GTZC_TZSC_ConfigPeriphAttributes(
+ GTZC_PERIPH_TAMP,
+ unpriv ? GTZC_TZSC_PERIPH_NPRIV : GTZC_TZSC_PERIPH_PRIV);
+}
+
+#endif // !SECMON
+
+#if defined STM32U5A9xx || defined STM32U5G9xx
+void tz_set_gfxmmu_unpriv(bool unpriv) {
+ tzsc_ensure_unlocked();
+ HAL_GTZC_TZSC_ConfigPeriphAttributes(
+ GTZC_PERIPH_GFXMMU,
+ unpriv ? GTZC_TZSC_PERIPH_NPRIV : GTZC_TZSC_PERIPH_PRIV);
+}
+#endif
+
+#endif // SECURE_MODE
### core/embed/sys/trustzone/inc/sys/trustzone.h
@@ -47,11 +47,5 @@ void tz_set_flash_unpriv(uint32_t start, uint32_t size, bool unpriv);
// 8KB to cover the entire specified range.
void tz_set_flash_unsecure(uint32_t start, uint32_t size, bool unsecure);
-// Sets unprivileged access to the SAES peripheral.
-void tz_set_saes_unpriv(bool unpriv);
-
-// Sets unprivileged access to the TAMP peripheral.
-void tz_set_tamper_unpriv(bool unpriv);
-
-// Sets unprivileged access to the GFXMMU peripheral.
-void tz_set_gfxmmu_unpriv(bool unpriv);
+// Peripheral privilege attributes live in the GTZC TZSC, which only secure
+// code can write; see sec/tz_periph.h.
### core/embed/sys/trustzone/stm32u5/trustzone.c
@@ -96,6 +96,14 @@ void tz_set_sram_unpriv(uint32_t start, uint32_t size, bool unpriv) {
uint32_t end = start + size;
+#ifdef SECMON
+ // Allow using both secure and non-secure SRAM regions
+ if (start >= SRAM1_BASE_NS && end < SRAM1_BASE_S) {
+ start += SRAM1_BASE_S - SRAM1_BASE_NS;
+ end += SRAM1_BASE_S - SRAM1_BASE_NS;
+ }
+#endif
+
for (size_t idx = 0; idx < ARRAY_LENGTH(g_sram_regions); idx++) {
const sram_region_t* r = &g_sram_regions[idx];
@@ -275,24 +283,4 @@ void tz_set_flash_unsecure(uint32_t start, uint32_t size, bool unsecure) {
__ISB();
}
-void tz_set_saes_unpriv(bool unpriv) {
- HAL_GTZC_TZSC_ConfigPeriphAttributes(
- GTZC_PERIPH_SAES,
- unpriv ? GTZC_TZSC_PERIPH_NPRIV : GTZC_TZSC_PERIPH_PRIV);
-}
-
-void tz_set_tamper_unpriv(bool unpriv) {
- HAL_GTZC_TZSC_ConfigPeriphAttributes(
- GTZC_PERIPH_TAMP,
- unpriv ? GTZC_TZSC_PERIPH_NPRIV : GTZC_TZSC_PERIPH_PRIV);
-}
-
-#if defined STM32U5A9xx || defined STM32U5G9xx
-void tz_set_gfxmmu_unpriv(bool unpriv) {
- HAL_GTZC_TZSC_ConfigPeriphAttributes(
- GTZC_PERIPH_GFXMMU,
- unpriv ? GTZC_TZSC_PERIPH_NPRIV : GTZC_TZSC_PERIPH_PRIV);
-}
-#endif
-
#endif // KERNEL_MODEWhy this scored 36/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.