speculation.txt 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. This document explains potential effects of speculation, and how undesirable
  2. effects can be mitigated portably using common APIs.
  3. ===========
  4. Speculation
  5. ===========
  6. To improve performance and minimize average latencies, many contemporary CPUs
  7. employ speculative execution techniques such as branch prediction, performing
  8. work which may be discarded at a later stage.
  9. Typically speculative execution cannot be observed from architectural state,
  10. such as the contents of registers. However, in some cases it is possible to
  11. observe its impact on microarchitectural state, such as the presence or
  12. absence of data in caches. Such state may form side-channels which can be
  13. observed to extract secret information.
  14. For example, in the presence of branch prediction, it is possible for bounds
  15. checks to be ignored by code which is speculatively executed. Consider the
  16. following code:
  17. int load_array(int *array, unsigned int index)
  18. {
  19. if (index >= MAX_ARRAY_ELEMS)
  20. return 0;
  21. else
  22. return array[index];
  23. }
  24. Which, on arm64, may be compiled to an assembly sequence such as:
  25. CMP <index>, #MAX_ARRAY_ELEMS
  26. B.LT less
  27. MOV <returnval>, #0
  28. RET
  29. less:
  30. LDR <returnval>, [<array>, <index>]
  31. RET
  32. It is possible that a CPU mis-predicts the conditional branch, and
  33. speculatively loads array[index], even if index >= MAX_ARRAY_ELEMS. This
  34. value will subsequently be discarded, but the speculated load may affect
  35. microarchitectural state which can be subsequently measured.
  36. More complex sequences involving multiple dependent memory accesses may
  37. result in sensitive information being leaked. Consider the following
  38. code, building on the prior example:
  39. int load_dependent_arrays(int *arr1, int *arr2, int index)
  40. {
  41. int val1, val2,
  42. val1 = load_array(arr1, index);
  43. val2 = load_array(arr2, val1);
  44. return val2;
  45. }
  46. Under speculation, the first call to load_array() may return the value
  47. of an out-of-bounds address, while the second call will influence
  48. microarchitectural state dependent on this value. This may provide an
  49. arbitrary read primitive.
  50. ====================================
  51. Mitigating speculation side-channels
  52. ====================================
  53. The kernel provides a generic API to ensure that bounds checks are
  54. respected even under speculation. Architectures which are affected by
  55. speculation-based side-channels are expected to implement these
  56. primitives.
  57. The array_index_nospec() helper in <linux/nospec.h> can be used to
  58. prevent information from being leaked via side-channels.
  59. A call to array_index_nospec(index, size) returns a sanitized index
  60. value that is bounded to [0, size) even under cpu speculation
  61. conditions.
  62. This can be used to protect the earlier load_array() example:
  63. int load_array(int *array, unsigned int index)
  64. {
  65. if (index >= MAX_ARRAY_ELEMS)
  66. return 0;
  67. else {
  68. index = array_index_nospec(index, MAX_ARRAY_ELEMS);
  69. return array[index];
  70. }
  71. }