esp_fault.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "sdkconfig.h"
  7. #include "soc/rtc_cntl_reg.h"
  8. #include "esp_rom_sys.h"
  9. #pragma once
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /**
  14. * @brief Assert a condition is true, in a way that should be resistant to fault injection for
  15. * single fault attacks.
  16. *
  17. * - Expands CONDITION multiple times (condition must have no side effects)
  18. * - Compiler is told all registers are invalid before evaluating CONDITION each time, to avoid a fault
  19. * causing a misread of a register used in all three evaluations of CONDITION.
  20. * - If CONDITION is ever false, a system reset is triggered.
  21. *
  22. * @note Place this macro after a "normal" check of CONDITION that will fail with a normal error
  23. * message. This is the fallback in case a fault injection attack skips or corrupts the result of
  24. * that check. (Although ensure that an attacker can't use fault injection to skip past the "normal"
  25. * error message, to avoid this check entirely.)
  26. *
  27. * @note This macro increases binary size and is slow and should be used sparingly.
  28. *
  29. * @note This macro does not guarantee fault injection resistance. In particular CONDITION must be
  30. * chosen carefully - a fault injection attack which sets CONDITION to true will not be detected by
  31. * this macro. Care must also be taken that an attacker can't use a fault to completely bypass calling
  32. * whatever function tests ESP_FAULT_ASSERT.
  33. *
  34. * @note This is difficult to debug as a failure triggers an instant software reset, and UART output
  35. * is often truncated (as FIFO is not flushed). Define the ESP_FAULT_ASSERT_DEBUG macro to debug any
  36. * failures of this macro due to software bugs.
  37. *
  38. * @param CONDITION A condition which will evaluate true unless an attacker used fault injection to skip or corrupt some other critical system calculation.
  39. *
  40. */
  41. #define ESP_FAULT_ASSERT(CONDITION) do { \
  42. asm volatile ("" ::: "memory"); \
  43. if(!(CONDITION)) _ESP_FAULT_RESET(); \
  44. asm volatile ("" ::: "memory"); \
  45. if(!(CONDITION)) _ESP_FAULT_RESET(); \
  46. asm volatile ("" ::: "memory"); \
  47. if(!(CONDITION)) _ESP_FAULT_RESET(); \
  48. } while(0)
  49. #ifndef CONFIG_IDF_TARGET_ARCH_RISCV
  50. #define _ESP_FAULT_ILLEGAL_INSTRUCTION asm volatile("ill.n; ill.n; ill.n; ill.n; ill.n; ill.n; ill.n;")
  51. #else
  52. #define _ESP_FAULT_ILLEGAL_INSTRUCTION asm volatile("unimp; unimp; unimp; unimp; unimp;")
  53. #endif
  54. // Uncomment this macro to get debug output if ESP_FAULT_ASSERT() fails
  55. //
  56. // Note that uncommenting this macro reduces the anti-FI effectiveness
  57. //
  58. //#define ESP_FAULT_ASSERT_DEBUG
  59. /* Internal macro, purpose is to trigger a system reset if an inconsistency due to fault injection
  60. is detected.
  61. Illegal instruction opcodes are there as a fallback to crash the CPU in case it doesn't
  62. reset as expected.
  63. */
  64. #ifndef ESP_FAULT_ASSERT_DEBUG
  65. #define _ESP_FAULT_RESET() do { \
  66. REG_WRITE(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_SW_SYS_RST); \
  67. _ESP_FAULT_ILLEGAL_INSTRUCTION; \
  68. } while(0)
  69. #else // ESP_FAULT_ASSERT_DEBUG
  70. #warning "Enabling ESP_FAULT_ASSERT_DEBUG makes ESP_FAULT_ASSERT() less effective"
  71. #define _ESP_FAULT_RESET() do { \
  72. esp_rom_printf("ESP_FAULT_ASSERT %s:%d\n", __FILE__, __LINE__); \
  73. _ESP_FAULT_ILLEGAL_INSTRUCTION; \
  74. } while(0)
  75. #endif // ESP_FAULT_ASSERT_DEBUG
  76. #ifdef __cplusplus
  77. }
  78. #endif