forked from Fraunhofer-AISEC/archie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfault_injection.c
362 lines (346 loc) · 10.7 KB
/
fault_injection.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
* Copyright 2021 Florian Andreas Hauschild
*
* 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.
*
* This file contains all functions needed to inject a fault in qemu
*/
#include "fault_injection.h"
#include "faultplugin.h"
#include "registerdump.h"
#include "tb_faulted_collection.h"
#include "faultdata.h"
#include <qemu/qemu-plugin.h>
//#include <glib.h>
/**
* inject_fault
*
* At this point the fault needs to be injected. This is the function to select the right model and call the injection function
*
* current: Struct address containing the fault information needed
*/
void inject_fault(fault_list_t * current)
{
g_autoptr(GString) out = g_string_new("");
if( current != NULL)
{
if(current->fault.type == INSTRUCTION)
{
insert_memorydump_config(current->fault.address, 16);
read_specific_memoryregion(current->fault.address);
tb_faulted_register(current->fault.address);
qemu_plugin_outs("[Fault] Inject instruction fault\n");
inject_memory_fault( current);
plugin_flush_tb();
read_specific_memoryregion(current->fault.address);
qemu_plugin_outs("Flushed tb\n");
}
if(current->fault.type == DATA)
{
insert_memorydump_config(current->fault.address, 16);
read_specific_memoryregion(current->fault.address);
qemu_plugin_outs("[Fault] Inject memory fault\n");
inject_memory_fault( current);
plugin_flush_tb();
read_specific_memoryregion(current->fault.address);
qemu_plugin_outs("Flushed tb\n");
}
if(current->fault.type == REGISTER)
{
qemu_plugin_outs("[Fault] Inject register fault\n");
inject_register_fault( current);
//TODO
}
invalidate_fault_trigger_address(current->fault.trigger.trignum);
rem_singlestep_req();
if(current->fault.lifetime != 0)
{
current->fault.trigger.trignum = register_live_faults_callback(current);
}
add_new_registerdump(current->fault.trigger.trignum);
}
}
/**
* reverse_fault
*
* Reverse the fault injected
*
* current: fault description pointer
*/
void reverse_fault(fault_list_t * current)
{
g_autoptr(GString) out = g_string_new("");
if(current != NULL)
{
if(current->fault.type == INSTRUCTION)
{
qemu_plugin_outs("[Fault] Reverse instruction fault\n");
process_reverse_fault(current->fault.address, current->fault.mask, current->fault.restoremask);
plugin_flush_tb();
read_specific_memoryregion(current->fault.address);
qemu_plugin_outs("Flushed tb\n");
}
if(current->fault.type == DATA)
{
qemu_plugin_outs("[Fault] Reverse memory fault\n");
process_reverse_fault(current->fault.address, current->fault.mask, current->fault.restoremask);
plugin_flush_tb();
read_specific_memoryregion(current->fault.address);
qemu_plugin_outs("Flushed tb\n");
}
if(current->fault.type == REGISTER)
{
qemu_plugin_outs("[Fault] Reverse register fault\n");
reverse_register_fault( current);
}
}
rem_singlestep_req();
add_new_registerdump(current->fault.trigger.trignum);
}
/**
* inject_register_fault
*
* Inject fault into registers. Reads the current string and determines the register that is attacked, loads it and performs the fault required
*/
void inject_register_fault(fault_list_t * current)
{
g_autoptr(GString) out = g_string_new("");
//TODO: Remove if not needed
/* if(current->fault.address > 14)
{
qemu_plugin_outs("[ERROR] Register not valid\n");
return;
}*/
uint64_t reg = read_reg(current->fault.address);
uint64_t mask = 0;
for(int i = 0; i < 8; i++)
{
current->fault.restoremask[i] = (reg >> 8*i) & current->fault.mask[i];
mask += (current->fault.mask[i] << 8*i);
}
g_string_printf(out," Changing registers %li from %08lx", current->fault.address, reg);
switch(current->fault.model)
{
case SET0:
reg = reg & ~(mask);
break;
case SET1:
reg = reg | mask;
break;
case TOGGLE:
reg = reg ^ mask;
break;
case OVERWRITE:
if(current->fault.num_bytes > 8)
{
g_string_append_printf(out, "\n[WARNGING]: For register faults currently only up to 8 Bytes are allowed. Your length is greater and all Greater Bytes are ignored\n");
current->fault.num_bytes = 8;
}
mask = 0;
for(int i = 0; i < current->fault.num_bytes; i++)
{
current->fault.restoremask[i] = (reg >> 8*i) & 0xff;
uint64_t clear = (0xff << 8*i);
reg = reg & ~(clear);
reg += (current->fault.mask[i] << 8*i);
mask += clear;
}
break;
default:
g_string_append_printf(out, "Fault model is wrong %li", current->fault.model);
break;
}
write_reg(current->fault.address, reg);
g_string_append_printf(out, " to %08lx, with mask %08lx\n", reg, mask);
qemu_plugin_outs(out->str);
}
void reverse_register_fault(fault_list_t * current)
{
g_autoptr(GString) out = g_string_new("");
uint64_t reg = read_reg(current->fault.address);
g_string_printf(out, " Change register %li back from %08lx", current->fault.address, reg);
for(int i = 0; i < 8; i++)
{
reg = reg & ~((uint64_t)current->fault.mask[i] << 8*i); // clear manipulated bits
reg = reg | ((uint64_t) current->fault.restoremask[i] << 8*i); // restore manipulated bits
}
write_reg(current->fault.address, reg);
g_string_printf(out, " to %08lx\n", reg);
qemu_plugin_outs(out->str);
}
/**
* inject_memory_fault
*
* injects fault into memory regions
* Reads current struct to determine the location, model, and mask of fault.
* Then performs the fault injection
*
* current: Struct address containing the fault information
*/
void inject_memory_fault(fault_list_t * current)
{
g_autoptr(GString) out = g_string_new("");
switch(current->fault.model)
{
case SET0:
g_string_append_printf(out, "Set 0 fault to address %lx\n", current->fault.address);
process_set0_memory(current->fault.address, current->fault.mask, current->fault.restoremask);
break;
case SET1:
g_string_append_printf(out, "Set 1 fault to address %lx\n", current->fault.address);
process_set1_memory(current->fault.address, current->fault.mask, current->fault.restoremask);
break;
case TOGGLE:
g_string_append_printf(out, "Toggle fault to address %lx\n", current->fault.address);
process_toggle_memory(current->fault.address, current->fault.mask, current->fault.restoremask);
break;
case OVERWRITE:
g_string_append_printf(out, "Overwrite fault at address %lx\n", current->fault.address);
process_overwrite_memory(current->fault.address, current->fault.num_bytes, current->fault.mask, current->fault.restoremask);
default:
break;
}
qemu_plugin_outs(out->str);
}
/**
* process_overwrite_memory
*
* Read memory, then overwrite the bytes specified by mask value. Num_bytes specify the number of bytes. currently max 16 bytes.
*
* address: base address of lowest byte
* maks: Values to overwrite the bytes at address location.
* num_bytes: Number of bytes to be overwritten.
* restoremask: Mask used to restore back values to original values for temoporary faults
*/
void process_overwrite_memory(uint64_t address, uint8_t num_bytes, uint8_t mask[], uint8_t restoremask[])
{
int ret;
uint8_t value[16];
if(num_bytes > 16)
{
num_bytes = 16;
qemu_plugin_outs("[WARNING]: Currently only up to 16 bytes can be overwritten!");
}
for(int i = 0; i < 16; i++)
{
restoremask[i] = 0;
}
ret = plugin_rw_memory_cpu(address, value, num_bytes, 0);
for( int i = 0; i < num_bytes; i++)
{
restoremask[i] = value[i];
value[i] = mask[i];
}
ret += plugin_rw_memory_cpu(address, value, num_bytes, 1);
if(ret < 0)
{
qemu_plugin_outs("[ERROR]: Something went wrong in read/write to cpu in process_overwrite_memory\n");
}
}
/**
* process_set1_memory
*
* Read memory, then set bits according to mask, then write memory back
*
* address: base address of lowest byte
* mask: mask containing which bits need to be flipped to 1
*/
void process_set1_memory(uint64_t address, uint8_t mask[], uint8_t restoremask[])
{
uint8_t value[16];
int ret;
ret = plugin_rw_memory_cpu( address, value, 16, 0);
for(int i = 0; i < 16; i++)
{
restoremask[i] = value[i] & mask[i]; // generate restore mask
value[i] = value[i] | mask[i]; // inject fault
}
ret += plugin_rw_memory_cpu( address, value, 16, 1);
if (ret < 0)
{
qemu_plugin_outs("[ERROR]: Something went wrong in read/write to cpu in process_set1_memory\n");
}
}
/**
* process_reverse_fault
*
* Read memory, then apply restore mask according to fault mask, then write memory back
*
* address: base address of fault
* mask: location mask of bits set to 0 for reverse
*/
void process_reverse_fault(uint64_t address, uint8_t mask[], uint8_t restoremask[])
{
uint8_t value[16];
int ret;
ret = plugin_rw_memory_cpu( address, value, 16, 0);
for(int i = 0; i < 16; i++)
{
value[i] = value[i] & ~(mask[i]); // clear value in mask position
value[i] = value[i] | restoremask[i]; // insert restore mask to restore positions
}
ret += plugin_rw_memory_cpu( address, value, 16, 1);
qemu_plugin_outs("[Fault]: Reverse fault!");
if (ret < 0)
{
qemu_plugin_outs("[ERROR]: Something went wrong in read/write to cpu in process_reverse_fault\n");
}
}
/**
* process_set0_memory
*
* Read memory, then clear bits according to mask, then write memory back
*
* address: base address of fault
* mask: location mask of bits set to 0
*/
void process_set0_memory(uint64_t address, uint8_t mask[], uint8_t restoremask[])
{
uint8_t value[16];
int ret;
ret = plugin_rw_memory_cpu( address, value, 16, 0);
for(int i = 0; i < 16; i++)
{
restoremask[i] = value[i] & mask[i]; // generate restore mask
value[i] = value[i] & ~(mask[i]); // inject fault
}
ret += plugin_rw_memory_cpu( address, value, 16, 1);
if (ret < 0)
{
qemu_plugin_outs("[ERROR]: Something went wrong in read/write to cpu in process_set0_memory\n");
}
}
/**
* process_toggle_memory
*
* Read memory, then toggle bits according to mask, then write memory back
*
* address: base address of fault
* mask: location mask of bits to be toggled
*/
void process_toggle_memory(uint64_t address, uint8_t mask[], uint8_t restoremask[])
{
uint8_t value[16];
int ret;
ret = plugin_rw_memory_cpu( address , value, 16, 0);
for(int i = 0; i < 16; i++)
{
restoremask[i] = value[i] & mask[i]; // generate restore mask
value[i] = value[i] ^ mask[i]; // inject fault
}
ret += plugin_rw_memory_cpu( address, value, 16, 1);
if (ret < 0)
{
qemu_plugin_outs("[ERROR]: Something went wrong in read/write to cpu in process_toggle_memory\n");
}
}