esp_fault.h 3.8 KB

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