This repository has been archived by the owner on Jul 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 195
/
double_sided_rowhammer.cc
257 lines (231 loc) · 8.92 KB
/
double_sided_rowhammer.cc
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Copyright 2015, Google, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Small test program to systematically check through the memory to find bit
// flips by double-sided row hammering.
//
// Compilation instructions:
// g++ -std=c++11 [filename]
//
// ./double_sided_rowhammer [-t nsecs] [-p percentage]
//
// Hammers for nsecs seconds, acquires the described fraction of memory (0.0
// to 0.9 or so).
//
// Original author: Thomas Dullien (thomasdullien@google.com)
#include <asm/unistd.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <linux/kernel-page-flags.h>
#include <map>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/sysinfo.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include <vector>
namespace {
// The fraction of physical memory that should be mapped for testing.
double fraction_of_physical_memory = 0.3;
// The time to hammer before aborting. Defaults to one hour.
uint64_t number_of_seconds_to_hammer = 3600;
// The number of memory reads to try.
uint64_t number_of_reads = 1000*1024;
// Obtain the size of the physical memory of the system.
uint64_t GetPhysicalMemorySize() {
struct sysinfo info;
sysinfo( &info );
return (size_t)info.totalram * (size_t)info.mem_unit;
}
uint64_t GetPageFrameNumber(int pagemap, uint8_t* virtual_address) {
// Read the entry in the pagemap.
uint64_t value;
int got = pread(pagemap, &value, 8,
(reinterpret_cast<uintptr_t>(virtual_address) / 0x1000) * 8);
assert(got == 8);
uint64_t page_frame_number = value & ((1ULL << 54)-1);
return page_frame_number;
}
void SetupMapping(uint64_t* mapping_size, void** mapping) {
*mapping_size =
static_cast<uint64_t>((static_cast<double>(GetPhysicalMemorySize()) *
fraction_of_physical_memory));
*mapping = mmap(NULL, *mapping_size, PROT_READ | PROT_WRITE,
MAP_POPULATE | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
assert(*mapping != (void*)-1);
// Initialize the mapping so that the pages are non-empty.
printf("[!] Initializing large memory mapping ...");
for (uint64_t index = 0; index < *mapping_size; index += 0x1000) {
uint64_t* temporary = reinterpret_cast<uint64_t*>(
static_cast<uint8_t*>(*mapping) + index);
temporary[0] = index;
}
printf("done\n");
}
uint64_t HammerAddressesStandard(
const std::pair<uint64_t, uint64_t>& first_range,
const std::pair<uint64_t, uint64_t>& second_range,
uint64_t number_of_reads) {
volatile uint64_t* first_pointer =
reinterpret_cast<uint64_t*>(first_range.first);
volatile uint64_t* second_pointer =
reinterpret_cast<uint64_t*>(second_range.first);
uint64_t sum = 0;
while (number_of_reads-- > 0) {
sum += first_pointer[0];
sum += second_pointer[0];
asm volatile(
"clflush (%0);\n\t"
"clflush (%1);\n\t"
: : "r" (first_pointer), "r" (second_pointer) : "memory");
}
return sum;
}
typedef uint64_t(HammerFunction)(
const std::pair<uint64_t, uint64_t>& first_range,
const std::pair<uint64_t, uint64_t>& second_range,
uint64_t number_of_reads);
// A comprehensive test that attempts to hammer adjacent rows for a given
// assumed row size (and assumptions of sequential physical addresses for
// various rows.
uint64_t HammerAllReachablePages(uint64_t presumed_row_size,
void* memory_mapping, uint64_t memory_mapping_size, HammerFunction* hammer,
uint64_t number_of_reads) {
// This vector will be filled with all the pages we can get access to for a
// given row size.
std::vector<std::vector<uint8_t*>> pages_per_row;
uint64_t total_bitflips = 0;
pages_per_row.resize(memory_mapping_size / presumed_row_size);
int pagemap = open("/proc/self/pagemap", O_RDONLY);
assert(pagemap >= 0);
printf("[!] Identifying rows for accessible pages ... ");
for (uint64_t offset = 0; offset < memory_mapping_size; offset += 0x1000) {
uint8_t* virtual_address = static_cast<uint8_t*>(memory_mapping) + offset;
uint64_t page_frame_number = GetPageFrameNumber(pagemap, virtual_address);
uint64_t physical_address = page_frame_number * 0x1000;
uint64_t presumed_row_index = physical_address / presumed_row_size;
//printf("[!] put va %lx pa %lx into row %ld\n", (uint64_t)virtual_address,
// physical_address, presumed_row_index);
if (presumed_row_index > pages_per_row.size()) {
pages_per_row.resize(presumed_row_index);
}
pages_per_row[presumed_row_index].push_back(virtual_address);
//printf("[!] done\n");
}
printf("Done\n");
// We should have some pages for most rows now.
for (uint64_t row_index = 0; row_index + 2 < pages_per_row.size();
++row_index) {
if ((pages_per_row[row_index].size() != 64) ||
(pages_per_row[row_index+2].size() != 64)) {
printf("[!] Can't hammer row %ld - only got %ld/%ld pages "
"in the rows above/below\n",
row_index+1, pages_per_row[row_index].size(),
pages_per_row[row_index+2].size());
continue;
} else if (pages_per_row[row_index+1].size() == 0) {
printf("[!] Can't hammer row %ld, got no pages from that row\n",
row_index+1);
continue;
}
printf("[!] Hammering rows %ld/%ld/%ld of %ld (got %ld/%ld/%ld pages)\n",
row_index, row_index+1, row_index+2, pages_per_row.size(),
pages_per_row[row_index].size(), pages_per_row[row_index+1].size(),
pages_per_row[row_index+2].size());
// Iterate over all pages we have for the first row.
for (uint8_t* first_row_page : pages_per_row[row_index]) {
// Iterate over all pages we have for the second row.
for (uint8_t* second_row_page : pages_per_row[row_index+2]) {
// Set all the target pages to 0xFF.
for (uint8_t* target_page : pages_per_row[row_index+1]) {
memset(target_page, 0xFF, 0x1000);
}
// Now hammer the two pages we care about.
std::pair<uint64_t, uint64_t> first_page_range(
reinterpret_cast<uint64_t>(first_row_page),
reinterpret_cast<uint64_t>(first_row_page+0x1000));
std::pair<uint64_t, uint64_t> second_page_range(
reinterpret_cast<uint64_t>(second_row_page),
reinterpret_cast<uint64_t>(second_row_page+0x1000));
hammer(first_page_range, second_page_range, number_of_reads);
// Now check the target pages.
uint64_t number_of_bitflips_in_target = 0;
for (const uint8_t* target_page : pages_per_row[row_index+1]) {
for (uint32_t index = 0; index < 0x1000; ++index) {
if (target_page[index] != 0xFF) {
++number_of_bitflips_in_target;
}
}
}
if (number_of_bitflips_in_target > 0) {
printf("[!] Found %ld flips in row %ld (%lx to %lx) when hammering "
"%lx and %lx\n", number_of_bitflips_in_target, row_index+1,
((row_index+1)*presumed_row_size),
((row_index+2)*presumed_row_size)-1,
GetPageFrameNumber(pagemap, first_row_page)*0x1000,
GetPageFrameNumber(pagemap, second_row_page)*0x1000);
total_bitflips += number_of_bitflips_in_target;
}
}
}
}
return total_bitflips;
}
void HammerAllReachableRows(HammerFunction* hammer, uint64_t number_of_reads) {
uint64_t mapping_size;
void* mapping;
SetupMapping(&mapping_size, &mapping);
HammerAllReachablePages(1024*256, mapping, mapping_size,
hammer, number_of_reads);
}
void HammeredEnough(int sig) {
printf("[!] Spent %ld seconds hammering, exiting now.\n",
number_of_seconds_to_hammer);
fflush(stdout);
fflush(stderr);
exit(0);
}
} // namespace
int main(int argc, char** argv) {
// Turn off stdout buffering when it is a pipe.
setvbuf(stdout, NULL, _IONBF, 0);
int opt;
while ((opt = getopt(argc, argv, "t:p:")) != -1) {
switch (opt) {
case 't':
number_of_seconds_to_hammer = atoi(optarg);
break;
case 'p':
fraction_of_physical_memory = atof(optarg);
break;
default:
fprintf(stderr, "Usage: %s [-t nsecs] [-p percent]\n",
argv[0]);
exit(EXIT_FAILURE);
}
}
signal(SIGALRM, HammeredEnough);
printf("[!] Starting the testing process...\n");
alarm(number_of_seconds_to_hammer);
HammerAllReachableRows(&HammerAddressesStandard, number_of_reads);
}