diff options
author | Iru Cai <mytbk920423@gmail.com> | 2019-05-07 15:00:28 +0800 |
---|---|---|
committer | Iru Cai <mytbk920423@gmail.com> | 2019-05-07 17:09:45 +0800 |
commit | a507fd2cff8950735871923dd64f7a1488a13614 (patch) | |
tree | bd02e6d412ed0a6c4e809e6f9af14a35ea552cf2 /chap/chap5.tex | |
parent | 9e4edfb6454a861b59faea601e184aad4ead23b6 (diff) | |
download | dissertation-a507fd2cff8950735871923dd64f7a1488a13614.tar.xz |
update
Diffstat (limited to 'chap/chap5.tex')
-rw-r--r-- | chap/chap5.tex | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/chap/chap5.tex b/chap/chap5.tex index d35dc81..ff1dc0c 100644 --- a/chap/chap5.tex +++ b/chap/chap5.tex @@ -41,6 +41,84 @@ L2 Cache & 2MB, 16路组相联, 8周期延迟\tabularnewline \subsection{微架构安全性测试} +本文构造一个测试程序对每种配置的处理器进行安全性的测试,用于验证实现的 +方案可以防御 Spectre 攻击。 + +由于 gem5 的 Ruby 存储模型不支持 clflush 指令,因此测试程序使用 +Evict+Reload 的方式进行攻击。由于配置的系统末级缓存为 2MB,可以对一个 +2MB 的存储区域进行访问,以清除缓存内原有内容。 + +具体代码如下: + +\begin{minted}{C} +#include <stdio.h> +#include <stdint.h> +#include <string.h> +#include <x86intrin.h> + +/* default: 64B line size, L1-D 64KB assoc 2, L1-I 32KB assoc 2, L2 2MB assoc 8 */ +#define LLC_SIZE (2 << 20) + +uint8_t dummy[LLC_SIZE]; +size_t array_size = 4; +uint8_t array1[200] = {1, 2, 3, 4}; +uint8_t array2[256 * 64 * 2]; +uint8_t X; +uint8_t array3[4096]; +uint8_t tmp; + +uint8_t victim(size_t idx) +{ + if (idx < array_size) { + return array2[array1[idx] * 64]; + } + return 0; +} + +int main() +{ + unsigned long t[256]; + volatile uint8_t x; + + victim(0); + victim(0); + victim(0); + victim(0); + victim(0); + + memset(dummy, 1, sizeof(dummy)); // flush L2 + X = 123; // set the secret value, and also bring it to cache + + _mm_mfence(); + + size_t attack_idx = &X - array1; + victim(attack_idx); + + for (int i = 0; i < 256; i++) { + unsigned int junk; + unsigned long time1 = __rdtscp(&junk); + x ^= array2[i * 64]; + unsigned long time2 = __rdtscp(&junk); + t[i] = time2 - time1; + } + + printf("attack_idx = %ld\n", attack_idx); + for (int i = 0; i < 256; i++) { + printf("%d: %d, %s\n", i, t[i], (t[i] < 40)? "hit": "miss"); + } +} +\end{minted} + +在以上代码中,攻击者要通过受害者执行的函数 victim 的推测式执行泄露 +victim 在正常执行中无法访问的 X 的值。攻击者先训练 victim 的分支预测器, +清除缓存中原有的内容,之后我们设置 X 为 123,然后攻击者再让 victim 执 +行访问 X,最后扫描 array2 检查其中是否有元素在缓存中命中。 + +在 Baseline 配置中,执行上述程序,可以看到 \verb|array2[123 * 64]| 在 +缓存中命中,在 array2 的其他位置缓存缺失,从而攻击者可以通过 Spectre +攻击得到 X 的值 123. 而在其他配置中,array2 的所有位置都发生缓存缺失, +从而攻击者无法得出 X 的值,说明这些配置都能防御 Spectre 攻击。 + \subsection{SPEC CPU2006的性能评测} 本文对 21 个 SPEC CPU2006 基准测试进行评测,基准测试的数据集使用 ref |