|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * Copyright (c) 2020 Arm Limited |
| 5 | + */ |
| 6 | + |
| 7 | +#ifndef __FAULT_INJECTION_HARDENING_H__ |
| 8 | +#define __FAULT_INJECTION_HARDENING_H__ |
| 9 | + |
| 10 | +/* Fault injection mitigation library. |
| 11 | + * |
| 12 | + * Has support for different measures, which can either be enabled/disabled |
| 13 | + * separately or by defining one of the MCUBOOT_FIH_PROFILEs. |
| 14 | + * |
| 15 | + * NOTE: These constructs against fault injection attacks are not guaranteed to |
| 16 | + * be secure for all compilers, but execution is going to be correct and |
| 17 | + * including them will certainly help to harden the code. |
| 18 | + * |
| 19 | + * FIH_ENABLE_DOUBLE_VARS makes critical variables into a tuple (x, x ^ msk). |
| 20 | + * Then the correctness of x can be checked by XORing the two tuple values |
| 21 | + * together. This also means that comparisons between fih_ints can be verified |
| 22 | + * by doing x == y && x_msk == y_msk. |
| 23 | + * |
| 24 | + * FIH_ENABLE_GLOBAL_FAIL makes all while(1) failure loops redirect to a global |
| 25 | + * failure loop. This loop has mitigations against loop escapes / unlooping. |
| 26 | + * This also means that any unlooping won't immediately continue executing the |
| 27 | + * function that was executing before the failure. |
| 28 | + * |
| 29 | + * FIH_ENABLE_CFI (Control Flow Integrity) creates a global counter that is |
| 30 | + * incremented before every FIH_CALL of vulnerable functions. On the function |
| 31 | + * return the counter is decremented, and after the return it is verified that |
| 32 | + * the counter has the same value as before this process. This can be used to |
| 33 | + * verify that the function has actually been called. This protection is |
| 34 | + * intended to discover that important functions are called in an expected |
| 35 | + * sequence and neither of them is missed due to an instruction skip which could |
| 36 | + * be a result of glitching attack. It does not provide protection against ROP |
| 37 | + * or JOP attacks. |
| 38 | + * |
| 39 | + * FIH_ENABLE_DELAY causes random delays. This makes it hard to cause faults |
| 40 | + * precisely. It requires an RNG. An mbedtls integration is provided in |
| 41 | + * fault_injection_hardening_delay_mbedtls.h, but any RNG that has an entropy |
| 42 | + * source can be used by implementing the fih_delay_random_uchar function. |
| 43 | + * |
| 44 | + * The basic call pattern is: |
| 45 | + * |
| 46 | + * fih_int fih_rc = FIH_FAILURE; |
| 47 | + * FIH_CALL(vulnerable_function, fih_rc, arg1, arg2); |
| 48 | + * if (fih_not_eq(fih_rc, FIH_SUCCESS)) { |
| 49 | + * FIH_PANIC; |
| 50 | + * } |
| 51 | + * |
| 52 | + * Note that any function called by FIH_CALL must only return using FIH_RETURN, |
| 53 | + * as otherwise the CFI counter will not be decremented and the CFI check will |
| 54 | + * fail causing a panic. |
| 55 | + */ |
| 56 | + |
| 57 | +#include "mcuboot_config/mcuboot_config.h" |
| 58 | + |
| 59 | +#if defined(MCUBOOT_FIH_PROFILE_HIGH) |
| 60 | + |
| 61 | +#define FIH_ENABLE_DELAY /* Requires an entropy source */ |
| 62 | +#define FIH_ENABLE_DOUBLE_VARS |
| 63 | +#define FIH_ENABLE_GLOBAL_FAIL |
| 64 | +#define FIH_ENABLE_CFI |
| 65 | + |
| 66 | +#elif defined(MCUBOOT_FIH_PROFILE_MEDIUM) |
| 67 | + |
| 68 | +#define FIH_ENABLE_DOUBLE_VARS |
| 69 | +#define FIH_ENABLE_GLOBAL_FAIL |
| 70 | +#define FIH_ENABLE_CFI |
| 71 | + |
| 72 | +#elif defined(MCUBOOT_FIH_PROFILE_LOW) |
| 73 | + |
| 74 | +#define FIH_ENABLE_GLOBAL_FAIL |
| 75 | +#define FIH_ENABLE_CFI |
| 76 | + |
| 77 | +#elif !defined(MCUBOOT_FIH_PROFILE_OFF) |
| 78 | +#define MCUBOOT_FIH_PROFILE_OFF |
| 79 | +#endif /* MCUBOOT_FIH_PROFILE */ |
| 80 | + |
| 81 | +#ifdef FIH_ENABLE_DELAY |
| 82 | +#include "fault_injection_hardening_delay_rng.h" |
| 83 | +#endif /* FIH_ENABLE_DELAY */ |
| 84 | + |
| 85 | + |
| 86 | +#ifdef __cplusplus |
| 87 | +extern "C" { |
| 88 | +#endif /* __cplusplus */ |
| 89 | + |
| 90 | +/* Non-zero success value to defend against register resets. Zero is the most |
| 91 | + * common value for a corrupted register so complex bit-patterns are used |
| 92 | + */ |
| 93 | +#ifndef MCUBOOT_FIH_PROFILE_OFF |
| 94 | +#define FIH_POSITIVE_VALUE 0x1AAAAAAA |
| 95 | +#define FIH_NEGATIVE_VALUE 0x15555555 |
| 96 | +#else |
| 97 | +#define FIH_POSITIVE_VALUE 0 |
| 98 | +#define FIH_NEGATIVE_VALUE -1 |
| 99 | +#endif |
| 100 | + |
| 101 | +/* A volatile mask is used to prevent compiler optimization - the mask is xored |
| 102 | + * with the variable to create the backup and the integrity can be checked with |
| 103 | + * another xor. The mask value doesn't _really_ matter that much, as long as |
| 104 | + * it has reasonably high hamming weight. |
| 105 | + */ |
| 106 | +#define _FIH_MASK_VALUE 0xBEEF |
| 107 | + |
| 108 | +#ifdef FIH_ENABLE_DOUBLE_VARS |
| 109 | + |
| 110 | +/* All ints are replaced with two int - the normal one and a backup which is |
| 111 | + * XORed with the mask. |
| 112 | + */ |
| 113 | +extern volatile int _fih_mask; |
| 114 | +typedef volatile struct { |
| 115 | + volatile int val; |
| 116 | + volatile int msk; |
| 117 | +} fih_int; |
| 118 | + |
| 119 | +#else |
| 120 | + |
| 121 | +typedef int fih_int; |
| 122 | + |
| 123 | +#endif /* FIH_ENABLE_DOUBLE_VARS */ |
| 124 | + |
| 125 | +extern fih_int FIH_SUCCESS; |
| 126 | +extern fih_int FIH_FAILURE; |
| 127 | + |
| 128 | +#ifdef FIH_ENABLE_GLOBAL_FAIL |
| 129 | +/* Global failure handler - more resistant to unlooping. noinline and used are |
| 130 | + * used to prevent optimization |
| 131 | + */ |
| 132 | +__attribute__((noinline)) __attribute__((used)) |
| 133 | +void fih_panic_loop(void); |
| 134 | +#define FIH_PANIC fih_panic_loop() |
| 135 | +#else |
| 136 | +#define FIH_PANIC while (1) {} |
| 137 | +#endif /* FIH_ENABLE_GLOBAL_FAIL */ |
| 138 | + |
| 139 | +/* NOTE: For functions to be inlined outside their compilation unit they have to |
| 140 | + * have the body in the header file. This is required as function calls are easy |
| 141 | + * to skip. |
| 142 | + */ |
| 143 | +#ifdef FIH_ENABLE_DELAY |
| 144 | + |
| 145 | +/* Delaying logic, with randomness from a CSPRNG */ |
| 146 | +__attribute__((always_inline)) inline |
| 147 | +int fih_delay(void) |
| 148 | +{ |
| 149 | + unsigned char delay; |
| 150 | + int foo = 0; |
| 151 | + volatile int rc; |
| 152 | + |
| 153 | + delay = fih_delay_random_uchar(); |
| 154 | + |
| 155 | + for (volatile int i = 0; i < delay; i++) { |
| 156 | + foo++; |
| 157 | + } |
| 158 | + |
| 159 | + rc = 1; |
| 160 | + |
| 161 | + /* rc is volatile so if it is the return value then the function cannot be |
| 162 | + * optimized |
| 163 | + */ |
| 164 | + return rc; |
| 165 | +} |
| 166 | + |
| 167 | +#else |
| 168 | + |
| 169 | +__attribute__((always_inline)) inline |
| 170 | +int fih_delay_init(void) |
| 171 | +{ |
| 172 | + return 1; |
| 173 | +} |
| 174 | + |
| 175 | +__attribute__((always_inline)) inline |
| 176 | +int fih_delay(void) |
| 177 | +{ |
| 178 | + return 1; |
| 179 | +} |
| 180 | +#endif /* FIH_ENABLE_DELAY */ |
| 181 | + |
| 182 | +#ifdef FIH_ENABLE_DOUBLE_VARS |
| 183 | + |
| 184 | +__attribute__((always_inline)) inline |
| 185 | +void fih_int_validate(fih_int x) |
| 186 | +{ |
| 187 | + if (x.val != (x.msk ^ _fih_mask)) { |
| 188 | + FIH_PANIC; |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +/* Convert a fih_int to an int. Validate for tampering. */ |
| 193 | +__attribute__((always_inline)) inline |
| 194 | +int fih_int_decode(fih_int x) |
| 195 | +{ |
| 196 | + fih_int_validate(x); |
| 197 | + return x.val; |
| 198 | +} |
| 199 | + |
| 200 | +/* Convert an int to a fih_int, can be used to encode specific error codes. */ |
| 201 | +__attribute__((always_inline)) inline |
| 202 | +fih_int fih_int_encode(int x) |
| 203 | +{ |
| 204 | + fih_int ret = {x, x ^ _fih_mask}; |
| 205 | + return ret; |
| 206 | +} |
| 207 | + |
| 208 | +/* Standard equality. If A == B then 1, else 0 */ |
| 209 | +__attribute__((always_inline)) inline |
| 210 | +int fih_eq(fih_int x, fih_int y) |
| 211 | +{ |
| 212 | + fih_int_validate(x); |
| 213 | + fih_int_validate(y); |
| 214 | + return (x.val == y.val) && fih_delay() && (x.msk == y.msk); |
| 215 | +} |
| 216 | + |
| 217 | +__attribute__((always_inline)) inline |
| 218 | +int fih_not_eq(fih_int x, fih_int y) |
| 219 | +{ |
| 220 | + fih_int_validate(x); |
| 221 | + fih_int_validate(y); |
| 222 | + return (x.val != y.val) && fih_delay() && (x.msk != y.msk); |
| 223 | +} |
| 224 | + |
| 225 | +#else |
| 226 | + |
| 227 | +/* NOOP */ |
| 228 | +__attribute__((always_inline)) inline |
| 229 | +void fih_int_validate(fih_int x) |
| 230 | +{ |
| 231 | + (void) x; |
| 232 | + return; |
| 233 | +} |
| 234 | + |
| 235 | +/* NOOP */ |
| 236 | +__attribute__((always_inline)) inline |
| 237 | +int fih_int_decode(fih_int x) |
| 238 | +{ |
| 239 | + return x; |
| 240 | +} |
| 241 | + |
| 242 | +/* NOOP */ |
| 243 | +__attribute__((always_inline)) inline |
| 244 | +fih_int fih_int_encode(int x) |
| 245 | +{ |
| 246 | + return x; |
| 247 | +} |
| 248 | + |
| 249 | +__attribute__((always_inline)) inline |
| 250 | +int fih_eq(fih_int x, fih_int y) |
| 251 | +{ |
| 252 | + return x == y; |
| 253 | +} |
| 254 | + |
| 255 | +__attribute__((always_inline)) inline |
| 256 | +int fih_not_eq(fih_int x, fih_int y) |
| 257 | +{ |
| 258 | + return x != y; |
| 259 | +} |
| 260 | +#endif /* FIH_ENABLE_DOUBLE_VARS */ |
| 261 | + |
| 262 | +/* C has a common return pattern where 0 is a correct value and all others are |
| 263 | + * errors. This function converts 0 to FIH_SUCCESS and any other number to a |
| 264 | + * value that is not FIH_SUCCESS |
| 265 | + */ |
| 266 | +__attribute__((always_inline)) inline |
| 267 | +fih_int fih_int_encode_zero_equality(int x) |
| 268 | +{ |
| 269 | + if (x) { |
| 270 | + return FIH_FAILURE; |
| 271 | + } else { |
| 272 | + return FIH_SUCCESS; |
| 273 | + } |
| 274 | +} |
| 275 | + |
| 276 | +#ifdef FIH_ENABLE_CFI |
| 277 | +extern fih_int _fih_cfi_ctr; |
| 278 | +#endif /* FIH_ENABLE_CFI */ |
| 279 | + |
| 280 | +fih_int fih_cfi_get_and_increment(void); |
| 281 | +void fih_cfi_validate(fih_int saved); |
| 282 | +void fih_cfi_decrement(void); |
| 283 | + |
| 284 | +/* Label for interacting with FIH testing tool. Can be parsed from the elf file |
| 285 | + * after compilation. Does not require debug symbols. |
| 286 | + */ |
| 287 | +#if defined(__ICCARM__) |
| 288 | +#define FIH_LABEL(str, lin, cnt) __asm volatile ("FIH_LABEL_" str "_" #lin "_" #cnt "::" ::); |
| 289 | +#else |
| 290 | +#define FIH_LABEL(str) __asm volatile ("FIH_LABEL_" str "_%=:" ::); |
| 291 | +#endif |
| 292 | + |
| 293 | +/* Main FIH calling macro. return variable is second argument. Does some setup |
| 294 | + * before and validation afterwards. Inserts labels for use with testing script. |
| 295 | + * |
| 296 | + * First perform the precall step - this gets the current value of the CFI |
| 297 | + * counter and saves it to a local variable, and then increments the counter. |
| 298 | + * |
| 299 | + * Then set the return variable to FIH_FAILURE as a base case. |
| 300 | + * |
| 301 | + * Then perform the function call. As part of the funtion FIH_RET must be called |
| 302 | + * which will decrement the counter. |
| 303 | + * |
| 304 | + * The postcall step gets the value of the counter and compares it to the |
| 305 | + * previously saved value. If this is equal then the function call and all child |
| 306 | + * function calls were performed. |
| 307 | + */ |
| 308 | +#if defined(__ICCARM__) |
| 309 | +#define FIH_CALL(f, ret, ...) FIH_CALL2(f, ret, __LINE__, __COUNTER__, __VA_ARGS__) |
| 310 | + |
| 311 | +#define FIH_CALL2(f, ret, l, c, ...) \ |
| 312 | + do { \ |
| 313 | + FIH_LABEL("FIH_CALL_START", l, c); \ |
| 314 | + FIH_CFI_PRECALL_BLOCK; \ |
| 315 | + ret = FIH_FAILURE; \ |
| 316 | + if (fih_delay()) { \ |
| 317 | + ret = f(__VA_ARGS__); \ |
| 318 | + } \ |
| 319 | + FIH_CFI_POSTCALL_BLOCK; \ |
| 320 | + FIH_LABEL("FIH_CALL_END", l, c); \ |
| 321 | + } while (0) |
| 322 | + |
| 323 | +#else |
| 324 | + |
| 325 | +#define FIH_CALL(f, ret, ...) \ |
| 326 | + do { \ |
| 327 | + FIH_LABEL("FIH_CALL_START"); \ |
| 328 | + FIH_CFI_PRECALL_BLOCK; \ |
| 329 | + ret = FIH_FAILURE; \ |
| 330 | + if (fih_delay()) { \ |
| 331 | + ret = f(__VA_ARGS__); \ |
| 332 | + } \ |
| 333 | + FIH_CFI_POSTCALL_BLOCK; \ |
| 334 | + FIH_LABEL("FIH_CALL_END"); \ |
| 335 | + } while (0) |
| 336 | +#endif |
| 337 | + |
| 338 | +/* FIH return changes the state of the internal state machine. If you do a |
| 339 | + * FIH_CALL then you need to do a FIH_RET else the state machine will detect |
| 340 | + * tampering and panic. |
| 341 | + */ |
| 342 | +#define FIH_RET(ret) \ |
| 343 | + do { \ |
| 344 | + FIH_CFI_PRERET; \ |
| 345 | + return ret; \ |
| 346 | + } while (0) |
| 347 | + |
| 348 | + |
| 349 | +#ifdef FIH_ENABLE_CFI |
| 350 | +/* Macro wrappers for functions - Even when the functions have zero body this |
| 351 | + * saves a few bytes on noop functions as it doesn't generate the call/ret |
| 352 | + * |
| 353 | + * CFI precall function saves the CFI counter and then increments it - the |
| 354 | + * postcall then checks if the counter is equal to the saved value. In order for |
| 355 | + * this to be the case a FIH_RET must have been performed inside the called |
| 356 | + * function in order to decrement the counter, so the function must have been |
| 357 | + * called. |
| 358 | + */ |
| 359 | +#define FIH_CFI_PRECALL_BLOCK \ |
| 360 | + fih_int _fih_cfi_saved_value = fih_cfi_get_and_increment() |
| 361 | + |
| 362 | +#define FIH_CFI_POSTCALL_BLOCK \ |
| 363 | + fih_cfi_validate(_fih_cfi_saved_value) |
| 364 | + |
| 365 | +#define FIH_CFI_PRERET \ |
| 366 | + fih_cfi_decrement() |
| 367 | +#else |
| 368 | +#define FIH_CFI_PRECALL_BLOCK |
| 369 | +#define FIH_CFI_POSTCALL_BLOCK |
| 370 | +#define FIH_CFI_PRERET |
| 371 | +#endif /* FIH_ENABLE_CFI */ |
| 372 | + |
| 373 | +#ifdef __cplusplus |
| 374 | +} |
| 375 | +#endif /* __cplusplus */ |
| 376 | + |
| 377 | +#endif /* __FAULT_INJECTION_HARDENING_H__ */ |
0 commit comments