esp_fault.h 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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
  45. calculation.
  46. *
  47. */
  48. #define ESP_FAULT_ASSERT(CONDITION) do { \
  49. asm volatile ("" ::: "memory"); \
  50. if(!(CONDITION)) _ESP_FAULT_RESET(); \
  51. asm volatile ("" ::: "memory"); \
  52. if(!(CONDITION)) _ESP_FAULT_RESET(); \
  53. asm volatile ("" ::: "memory"); \
  54. if(!(CONDITION)) _ESP_FAULT_RESET(); \
  55. } while(0)
  56. // Uncomment this macro to get debug output if ESP_FAULT_ASSERT() fails
  57. //
  58. // Note that uncommenting this macro reduces the anti-FI effectiveness
  59. //
  60. //#define ESP_FAULT_ASSERT_DEBUG
  61. /* Internal macro, purpose is to trigger a system reset if an inconsistency due to fault injection
  62. is detected.
  63. Illegal instruction opcodes are there as a fallback to crash the CPU in case it doesn't
  64. reset as expected.
  65. */
  66. #ifndef ESP_FAULT_ASSERT_DEBUG
  67. #define _ESP_FAULT_RESET() do { \
  68. REG_WRITE(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_SW_SYS_RST); \
  69. asm volatile("ill; ill; ill;"); \
  70. } while(0)
  71. #else // ESP_FAULT_ASSERT_DEBUG
  72. #warning "Enabling ESP_FAULT_ASSERT_DEBUG makes ESP_FAULT_ASSERT() less effective"
  73. #define _ESP_FAULT_RESET() do { \
  74. ets_printf("ESP_FAULT_ASSERT %s:%d\n", __FILE__, __LINE__); \
  75. asm volatile("ill;"); \
  76. } while(0)
  77. #endif // ESP_FAULT_ASSERT_DEBUG
  78. #ifdef __cplusplus
  79. }
  80. #endif