diff --git a/io/include/os_io.h b/io/include/os_io.h index b5e76d9ae..676156ae6 100644 --- a/io/include/os_io.h +++ b/io/include/os_io.h @@ -131,6 +131,12 @@ typedef struct { #define OS_IO_BUFFER_SIZE OS_IO_SEPH_BUFFER_SIZE #endif // !CUSTOM_IO_APDU_BUFFER_SIZE +#ifdef HAVE_BOLOS_APP_STACK_CANARY +#define APP_STACK_CANARY_MAGIC 0xDEAD0031 +// apps will exit with this value if canary corruption is detected +#define APP_STACK_CANARY_CORRUPTED_EXIT_VALUE 42 +#endif // HAVE_BOLOS_APP_STACK_CANARY + /* Exported macros------------------------------------------------------------*/ /* Exported variables --------------------------------------------------------*/ diff --git a/io/src/os_io.c b/io/src/os_io.c index 491b511f2..73e2a7232 100644 --- a/io/src/os_io.c +++ b/io/src/os_io.c @@ -67,6 +67,10 @@ uint8_t G_io_init_syscall; /* Private variables ---------------------------------------------------------*/ +#ifdef HAVE_BOLOS_APP_STACK_CANARY +extern unsigned int app_stack_canary; +#endif // HAVE_BOLOS_APP_STACK_CANARY + /* Private functions ---------------------------------------------------------*/ #ifndef USE_OS_IO_STACK static int process_itc_event(uint8_t *buffer_in, size_t buffer_in_length) @@ -136,6 +140,10 @@ static int process_itc_event(uint8_t *buffer_in, size_t buffer_in_length) #ifndef USE_OS_IO_STACK int os_io_init(os_io_init_t *init) { +#ifdef HAVE_BOLOS_APP_STACK_CANARY + app_stack_canary = APP_STACK_CANARY_MAGIC; +#endif // HAVE_BOLOS_APP_STACK_CANARY + if (!init) { return -1; } @@ -241,6 +249,13 @@ int os_io_rx_evt(unsigned char *buffer, int status = 0; uint16_t length = 0; +#ifdef HAVE_BOLOS_APP_STACK_CANARY + // if the canary is corrupted, reset the device + if (app_stack_canary != APP_STACK_CANARY_MAGIC) { + os_sched_exit(APP_STACK_CANARY_CORRUPTED_EXIT_VALUE); + } +#endif + if (!G_io_seph_buffer_size) { status = os_io_seph_se_rx_event(G_io_seph_buffer, sizeof(G_io_seph_buffer), @@ -352,6 +367,13 @@ int os_io_tx_cmd(uint8_t type, unsigned short length, unsigned int *timeout_ms) { +#ifdef HAVE_BOLOS_APP_STACK_CANARY + // if the canary is corrupted, reset the device + if (app_stack_canary != APP_STACK_CANARY_MAGIC) { + os_sched_exit(APP_STACK_CANARY_CORRUPTED_EXIT_VALUE); + } +#endif + int status = 0; switch (type) { #ifdef HAVE_IO_USB diff --git a/io_legacy/src/os_io_legacy.c b/io_legacy/src/os_io_legacy.c index 279b28b29..9ef8ad6e6 100644 --- a/io_legacy/src/os_io_legacy.c +++ b/io_legacy/src/os_io_legacy.c @@ -48,9 +48,6 @@ /* Private defines------------------------------------------------------------*/ /* Private macros-------------------------------------------------------------*/ -#ifdef HAVE_BOLOS_APP_STACK_CANARY -#define APP_STACK_CANARY_MAGIC 0xDEAD0031 -#endif // HAVE_BOLOS_APP_STACK_CANARY /* Private functions prototypes ----------------------------------------------*/ #ifdef HAVE_NFC_READER diff --git a/src/syscalls.c b/src/syscalls.c index 2d2a5201a..68e872d1f 100644 --- a/src/syscalls.c +++ b/src/syscalls.c @@ -39,6 +39,10 @@ #include "os_endorsement.h" #include +#ifdef HAVE_BOLOS_APP_STACK_CANARY +extern unsigned int app_stack_canary; +#endif // HAVE_BOLOS_APP_STACK_CANARY + unsigned int SVC_Call(unsigned int syscall_id, void *parameters); unsigned int SVC_cx_call(unsigned int syscall_id, unsigned int *parameters); @@ -1865,6 +1869,10 @@ int os_io_seph_se_rx_event(unsigned char *buffer, __attribute((weak)) int os_io_init(os_io_init_t *init) { +#ifdef HAVE_BOLOS_APP_STACK_CANARY + app_stack_canary = APP_STACK_CANARY_MAGIC; +#endif // HAVE_BOLOS_APP_STACK_CANARY + unsigned int parameters[1]; parameters[0] = (unsigned int) init; return (int) SVC_Call(SYSCALL_os_io_init_ID, parameters); @@ -1889,6 +1897,13 @@ __attribute((weak)) int os_io_tx_cmd(unsigned char type, unsigned short length, unsigned int *timeout_ms) { +#ifdef HAVE_BOLOS_APP_STACK_CANARY + // if the canary is corrupted, reset the device + if (app_stack_canary != APP_STACK_CANARY_MAGIC) { + os_sched_exit(APP_STACK_CANARY_CORRUPTED_EXIT_VALUE); + } +#endif + unsigned int parameters[4]; parameters[0] = (unsigned int) type; parameters[1] = (unsigned int) buffer; @@ -1902,6 +1917,13 @@ __attribute((weak)) int os_io_rx_evt(unsigned char *buffer, unsigned int *timeout_ms, bool check_se_event) { +#ifdef HAVE_BOLOS_APP_STACK_CANARY + // if the canary is corrupted, reset the device + if (app_stack_canary != APP_STACK_CANARY_MAGIC) { + os_sched_exit(APP_STACK_CANARY_CORRUPTED_EXIT_VALUE); + } +#endif + unsigned int parameters[4]; parameters[0] = (unsigned int) buffer; parameters[1] = (unsigned int) buffer_max_length;