93 lines · plain
1===========2Speculation3===========4 5This document explains potential effects of speculation, and how undesirable6effects can be mitigated portably using common APIs.7 8------------------------------------------------------------------------------9 10To improve performance and minimize average latencies, many contemporary CPUs11employ speculative execution techniques such as branch prediction, performing12work which may be discarded at a later stage.13 14Typically speculative execution cannot be observed from architectural state,15such as the contents of registers. However, in some cases it is possible to16observe its impact on microarchitectural state, such as the presence or17absence of data in caches. Such state may form side-channels which can be18observed to extract secret information.19 20For example, in the presence of branch prediction, it is possible for bounds21checks to be ignored by code which is speculatively executed. Consider the22following code::23 24 int load_array(int *array, unsigned int index)25 {26 if (index >= MAX_ARRAY_ELEMS)27 return 0;28 else29 return array[index];30 }31 32Which, on arm64, may be compiled to an assembly sequence such as::33 34 CMP <index>, #MAX_ARRAY_ELEMS35 B.LT less36 MOV <returnval>, #037 RET38 less:39 LDR <returnval>, [<array>, <index>]40 RET41 42It is possible that a CPU mis-predicts the conditional branch, and43speculatively loads array[index], even if index >= MAX_ARRAY_ELEMS. This44value will subsequently be discarded, but the speculated load may affect45microarchitectural state which can be subsequently measured.46 47More complex sequences involving multiple dependent memory accesses may48result in sensitive information being leaked. Consider the following49code, building on the prior example::50 51 int load_dependent_arrays(int *arr1, int *arr2, int index)52 {53 int val1, val2,54 55 val1 = load_array(arr1, index);56 val2 = load_array(arr2, val1);57 58 return val2;59 }60 61Under speculation, the first call to load_array() may return the value62of an out-of-bounds address, while the second call will influence63microarchitectural state dependent on this value. This may provide an64arbitrary read primitive.65 66====================================67Mitigating speculation side-channels68====================================69 70The kernel provides a generic API to ensure that bounds checks are71respected even under speculation. Architectures which are affected by72speculation-based side-channels are expected to implement these73primitives.74 75The array_index_nospec() helper in <linux/nospec.h> can be used to76prevent information from being leaked via side-channels.77 78A call to array_index_nospec(index, size) returns a sanitized index79value that is bounded to [0, size) even under cpu speculation80conditions.81 82This can be used to protect the earlier load_array() example::83 84 int load_array(int *array, unsigned int index)85 {86 if (index >= MAX_ARRAY_ELEMS)87 return 0;88 else {89 index = array_index_nospec(index, MAX_ARRAY_ELEMS);90 return array[index];91 }92 }93