Skip to main content
ac_gd
Associate II
July 16, 2021
Question

How to validate an image with security features enabled?

  • July 16, 2021
  • 7 replies
  • 7037 views

Hi all,

In our project we have enabled the ENABLE_IMAGE_STATE_HANDLING flag and download an image via YMODEM transfer to our NUCLEO-L476RG. However, when we try to validate the image by calling SE_APP_ValidateFw(), our STM reboots and does a rollback.

All of this only happens when not defining SECBOOT_DISABLE_SECURITY_IPS in NUCLEO-L476RG/Applications/2_Images/2_Images_SBSFU/SBSFU/App/app_sfu.h (see attachment for our app_sfu.h file) So when security features are enabled. However, we found out that the security feature that triggers this reset is SFU_MPU_USERAPP_ACTIVATION: when we disable this one, everything works as expected and no rollback happens (i.e., no reset).

Is this expected behavior of the SFU_MPU_USERAPP_ACTIVATION security feature? Is it incompatible with ENABLE_IMAGE_STATE_HANDLING mode/SE_APP_ValidateFw call?

Kind regards,

ac_gd

This topic has been closed for replies.

7 replies

jrgert
Associate III
July 16, 2021

Thank you for posting your question. While I do not have an answer, I do have the exact same situation with my board's STM32L496 and SBSFU v2.5.0.

Per your observation, I disabled SFU_MPU_USERAPP_ACTIVATION and the problem went away.

For background info, when I run my projects with ENABLE_IMAGE_STATE_HANDLING disabled, the firmware can be updated.

jrgert
Associate III
July 16, 2021

There is a difference in MPU permissions comparing the SB / SFU vs. App regions. MPU_REGION_FULL_ACCESS vs. MPU_REGION_PRIV_RO respectively. There is a comment regarding the app permissions and firewall, however I do not know enough about either yet. Still learning.

For fun, I changed the App region permission to MPU_REGION_FULL_ACCESS and it boots up and the app runs now. Is this OK?

ac_gd
ac_gdAuthor
Associate II
July 27, 2021

Hi @jrgert​, thank you for your answer. I am glad that you can reproduce the problem. If you assign it full access, will the MPU protection still have any effect?

FBelv
Associate
August 17, 2021

How did you manage to call SE_APP_ValidateFw() from within the UserApp? I am trying to run SBSFU with ENABLE_IMAGE_STATE_HANDLING, but the SE_APP_ValidateFw() function will reset the STM32L4A6 as the function is not in the expected memory boundary. Are you calling the SE_APP_ValidateFw() from the UserApp in the SBSFU code?

The failing part is in the function

/*Secure Engine Call*/
 
e_ret_status = (*SE_CallGatePtr)(SE_APP_VALIDATE_FW, peSE_Status, primask_bit, SlotNumber);

and this calls the:

SE_ErrorStatus SE_CallGate(SE_FunctionIDTypeDef eID, SE_StatusTypeDef * const peSE_Status, uint32_t PrimaskParam, ...)
{
 SE_ErrorStatus e_ret_status;
 va_list arguments;
#if defined(__GNUC__)
 register unsigned lr asm("lr");
 uint32_t LR = lr;
#else
 uint32_t LR;
#endif /* __GNUC__ */
 
 /* Enter the protected area */
 ENTER_PROTECTED_AREA();
 
 /*
 * Warning : It is mandatory to call NVIC_SystemReset() in case of error
 * instead of return(SE_ERROR) to avoid any attempt of attack by modifying
 * the call stack (LR) in order to execute code inside secure enclave
 */
 
 /* Check the Callgate was called only from SE Interface */
#if defined(__ICCARM__) || defined (__CC_ARM)
 LR = __get_LR();
#endif /* __ICCARM__ || __CC_ARM */
 
 IS_CALLER_SE_IF();

the IS_CALLER_SE_IF is

#define IS_CALLER_SE_IF() \
 do{ \
 if (LR< SE_IF_REGION_ROM_START){\
 NVIC_SystemReset();}\
 if (LR> SE_IF_REGION_ROM_END){\
 NVIC_SystemReset();}\
 }while(0)

and obviously, if the SE_APP_ValidateFw() function is in the UserApp flash space, the second IF will trigger a reset.

So, my question is, how did you avoid this? I'm on STM32CubeIde. Too bad STM tries its best not to provide working examples on STM32CubeIde.

jrgert
Associate III
August 18, 2021

FBelv asked "Are you calling the SE_APP_ValidateFw() from the UserApp in the SBSFU code?"

Yes, my app code calls SE_APP_ValidateFw() per Appendix J of UM2262. The SBSFU examples do provide a test app that calls the function.

Jocelyn RICARD
ST Employee
January 8, 2024

Hello @kaur ,

Are you using latest version of X-CUBE-SBSFU ?

Did you change the flash mapping ?

In this package, the MPU configuration needs to be adapted when flash mapping is changed. this is not done automatically ...

Best regards

Jocelyn

Associate II
January 9, 2024

Hi @Jocelyn RICARD  ,

Thank you for your reponse. Yes, I am using the latest SBSFU version 2.6.2.

In my configuration, I have commented all security IPs except SFU_IWDG_PROTECT_ENABLE and ENABLE_IMAGE_STATE_HANDLING(app_sfu.h attached for reference).

I have implemented same functionality as userapp to call SE_APP_GetActiveFwState in my app when SFU_MPU_PROTECT_ENABLE is not enabled.

I am using 2_Images project from P-NUCEO-WB55.Nucleo and I have expanded active and download slots to 344K and MPU configuration in sfu_low_level_security.h (file attached).

However, I am still seeing the issue outlined above when SE_APP_GetActiveFwState() is called. Here is how I have split the memory in my linker file.

/* swap (8 kbytes) */
__ICFEDIT_SWAP_start__ = 0x08016000;
__ICFEDIT_SWAP_size__ = 0x2000;
__ICFEDIT_SWAP_end__ = 0x8017fff;

/* Active slot #1 (352 kbytes) */
__ICFEDIT_SLOT_Size_1__ = 0x56000;
__ICFEDIT_SLOT_Active_1_start__ = 0x8018000;
__ICFEDIT_SLOT_Active_1_end__ = 0x806dfff;
__ICFEDIT_SLOT_Active_1_header__ = __ICFEDIT_SLOT_Active_1_start__;

 /* Dwl slot #1 (352 kbytes) */
__ICFEDIT_SLOT_Dwl_1_start__ = 0x806e000;
__ICFEDIT_SLOT_Dwl_1_end__ = 0x80c3fff;

 

--

Best

Praneet

Jocelyn RICARD
ST Employee
January 10, 2024

Hello,

First, I have double checked the Image state handling feature of the SBSFU 6.2.0 on STM32WB55 and is works fine on the default project.

I also changed the mapping as you did, and adapted MPU configuration accordingly in

Projects\P-NUCLEO-WB55.Nucleo\Applications\2_Images\2_Images_SBSFU\SBSFU\Target\sfu_low_level_security.h

 

#define APP_PROTECT_MPU_AREA_2_ACTIVE_SLOT_START 0x08040000U
#define APP_PROTECT_MPU_AREA_2_ACTIVE_SLOT_SIZE MPU_REGION_SIZE_256KB
#define APP_PROTECT_MPU_AREA_2_ACTIVE_SLOT_PERM MPU_REGION_FULL_ACCESS
#define APP_PROTECT_MPU_AREA_2_ACTIVE_SLOT_EXEC MPU_INSTRUCTION_ACCESS_ENABLE
#define APP_PROTECT_MPU_AREA_2_ACTIVE_SLOT_SREG 0xC0U 

 All works fine : Check active firmware version, update and image validation.

Best regards

Jocelyn

Associate II
January 24, 2024

Hi Jocelyn, 

Thanks for trying it out with SBSFU userapp.

I still see this issue with custom app, could it be that some peripherals being used by both userapp and SBSFU are causing the issue?

Jocelyn RICARD
ST Employee
January 26, 2024

Hello @kaur ,

Well I don't know.

Only way to go further is to use the debugger and see what happens.

Best regards

Jocelyn

Associate II
January 29, 2024

Hi @unknown ,

Is there anything different in your scenario which is leading to this issue?