-
Notifications
You must be signed in to change notification settings - Fork 5
/
drtaint_shadow.c
309 lines (270 loc) · 9.63 KB
/
drtaint_shadow.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
#include <string.h>
#include <signal.h>
#include <stddef.h> /* for offsetof */
#include "dr_api.h"
#include "drmgr.h"
#include "umbra.h"
#include "drtaint.h"
static int num_shadow_count;
static umbra_map_t *umbra_map;
static int tls_index;
/* shadow memory */
static reg_id_t
get_faulting_shadow_reg(void *drcontext, dr_mcontext_t *mc);
static bool
handle_special_shadow_fault(void *drcontext, dr_mcontext_t *raw_mc,
app_pc app_shadow);
static dr_signal_action_t
event_signal_instrumentation(void *drcontext, dr_siginfo_t *info);
static bool
drtaint_shadow_mem_init(int id);
static void
drtaint_shadow_mem_exit(void);
/* shadow regs */
static bool
drtaint_shadow_reg_init(void);
static void
drtaint_shadow_reg_exit(void);
static void
event_thread_init(void *drcontext);
static void
event_thread_exit(void *drcontext);
typedef struct _per_thread_t {
/* Holds shadow values for general purpose registers. The shadow memory
* currently uses UMBRA_MAP_SCALE_DOWN_4X, which implies that each 4-byte
* aligned location is represented as one byte. We imitate this here.
*/
byte shadow_gprs[DR_NUM_GPR_REGS];
} per_thread_t;
bool
drtaint_shadow_init(int id)
{
/* XXX: we only support a single umbra mapping */
if (dr_atomic_add32_return_sum(&num_shadow_count, 1) > 1)
return false;
if (!drtaint_shadow_mem_init(id) || !drtaint_shadow_reg_init())
return false;
return true;
}
void
drtaint_shadow_exit(void)
{
drtaint_shadow_mem_exit();
drtaint_shadow_reg_exit();
}
bool
drtaint_shadow_insert_app_to_shadow(void *drcontext, instrlist_t *ilist, instr_t *where,
reg_id_t regaddr, reg_id_t scratch)
{
/* XXX: we shouldn't have to do this */
/* Save the app address to a well-known spill slot, so that the fault handler
* can recover if no shadow memory was installed yet.
*/
dr_save_reg(drcontext, ilist, where, regaddr, SPILL_SLOT_2);
if (umbra_insert_app_to_shadow(drcontext, umbra_map, ilist, where, regaddr,
&scratch, 1) != DRMF_SUCCESS)
return false;
return true;
}
bool
drtaint_shadow_get_app_taint(void *drcontext, app_pc app, byte *result)
{
size_t sz = 1;
bool ret = umbra_read_shadow_memory(umbra_map, app, 4,
&sz, result) != DRMF_ERROR_INVALID_ADDRESS;
return ret;
}
bool
drtaint_shadow_set_app_taint(void *drcontext, app_pc app, byte result)
{
size_t sz = 1;
bool ret = umbra_write_shadow_memory(umbra_map, app, 4,
&sz, &result) != DRMF_ERROR_INVALID_ADDRESS;
return ret;
}
/* ======================================================================================
* shadow memory implementation
* ==================================================================================== */
static bool
drtaint_shadow_mem_init(int id)
{
umbra_map_options_t umbra_map_ops;
drmgr_init();
/* initialize umbra and lazy page handling */
memset(&umbra_map_ops, 0, sizeof(umbra_map_ops));
umbra_map_ops.scale = UMBRA_MAP_SCALE_DOWN_4X;
umbra_map_ops.flags = UMBRA_MAP_CREATE_SHADOW_ON_TOUCH |
UMBRA_MAP_SHADOW_SHARED_READONLY;
umbra_map_ops.default_value = 0;
umbra_map_ops.default_value_size = 1;
if (umbra_init(id) != DRMF_SUCCESS)
return false;
if (umbra_create_mapping(&umbra_map_ops, &umbra_map) != DRMF_SUCCESS)
return false;
drmgr_register_signal_event(event_signal_instrumentation);
return true;
}
static void
drtaint_shadow_mem_exit(void)
{
if (umbra_destroy_mapping(umbra_map) != DRMF_SUCCESS)
DR_ASSERT(false);
drmgr_unregister_signal_event(event_signal_instrumentation);
umbra_exit();
drmgr_exit();
}
static reg_id_t
get_faulting_shadow_reg(void *drcontext, dr_mcontext_t *mc)
{
instr_t inst;
reg_id_t reg;
instr_init(drcontext, &inst);
decode(drcontext, mc->pc, &inst);
DR_ASSERT_MSG(opnd_is_base_disp(instr_get_dst(&inst, 0)),
"Emulation error");
reg = opnd_get_base(instr_get_dst(&inst, 0));
DR_ASSERT_MSG(reg != DR_REG_NULL, "Emulation error");
instr_free(drcontext, &inst);
return reg;
}
static bool
handle_special_shadow_fault(void *drcontext, dr_mcontext_t *raw_mc,
app_pc app_shadow)
{
umbra_shadow_memory_type_t shadow_type;
app_pc app_target;
reg_id_t reg;
/* If a fault occured, it is probably because we computed the
* address of shadow memory which was initialized to a shared
* readonly shadow block. We allocate a shadow page there and
* replace the reg value used by the faulting instr.
*/
/* handle faults from writes to special shadow blocks */
if (umbra_shadow_memory_is_shared(umbra_map, app_shadow,
&shadow_type) != DRMF_SUCCESS) {
DR_ASSERT(false);
return true;
}
if (shadow_type != UMBRA_SHADOW_MEMORY_TYPE_SHARED)
return true;
/* Grab the original app target out of the spill slot so we
* don't have to compute the app target ourselves (this is
* difficult).
*/
app_target = (app_pc)dr_read_saved_reg(drcontext, SPILL_SLOT_2);
/* replace the shared block, and record the new app shadow */
if (umbra_replace_shared_shadow_memory(umbra_map, app_target,
&app_shadow) != DRMF_SUCCESS) {
DR_ASSERT(false);
return true;
}
/* Replace the faulting register value to reflect the new shadow
* memory.
*/
reg = get_faulting_shadow_reg(drcontext, raw_mc);
reg_set_value(reg, raw_mc, (reg_t)app_shadow);
return false;
}
static dr_signal_action_t
event_signal_instrumentation(void *drcontext, dr_siginfo_t *info)
{
if (info->sig != SIGSEGV && info->sig != SIGBUS)
return DR_SIGNAL_DELIVER;
DR_ASSERT(info->raw_mcontext_valid);
return handle_special_shadow_fault(drcontext, info->raw_mcontext,
info->access_address) ?
DR_SIGNAL_DELIVER : DR_SIGNAL_SUPPRESS;
}
/* ======================================================================================
* shadow registers implementation
* ==================================================================================== */
static bool
drtaint_shadow_reg_init(void)
{
drmgr_priority_t exit_priority = {
sizeof(exit_priority), DRMGR_PRIORITY_NAME_DRTAINT_EXIT, NULL, NULL,
DRMGR_PRIORITY_THREAD_EXIT_DRTAINT};
drmgr_priority_t init_priority = {
sizeof(init_priority), DRMGR_PRIORITY_NAME_DRTAINT_INIT, NULL, NULL,
DRMGR_PRIORITY_THREAD_INIT_DRTAINT};
drmgr_init();
drmgr_register_thread_init_event_ex(event_thread_init, &init_priority);
drmgr_register_thread_exit_event_ex(event_thread_exit, &exit_priority);
/* initialize tls for per-thread data */
tls_index = drmgr_register_tls_field();
if (tls_index == -1)
return false;
return true;
}
bool
drtaint_shadow_insert_reg_to_shadow(void *drcontext, instrlist_t *ilist, instr_t *where,
reg_id_t shadow, reg_id_t regaddr)
{
unsigned int offs = offsetof(per_thread_t, shadow_gprs[shadow - DR_REG_R0]);
DR_ASSERT(shadow - DR_REG_R0 < DR_NUM_GPR_REGS);
/* Load the per_thread data structure holding the thread-local taint
* values of each register.
*/
drmgr_insert_read_tls_field(drcontext, tls_index, ilist, where, regaddr);
instrlist_meta_preinsert(ilist, where, XINST_CREATE_add
(drcontext,
opnd_create_reg(regaddr),
OPND_CREATE_INT8(offs)));
return true;
}
bool
drtaint_shadow_insert_reg_to_shadow_load(void *drcontext, instrlist_t *ilist,
instr_t *where, reg_id_t shadow,
reg_id_t regaddr)
{
unsigned int offs = offsetof(per_thread_t, shadow_gprs[shadow - DR_REG_R0]);
DR_ASSERT(shadow - DR_REG_R0 < DR_NUM_GPR_REGS);
/* Load the per_thread data structure holding the thread-local taint
* values of each register.
*/
drmgr_insert_read_tls_field(drcontext, tls_index, ilist, where, regaddr);
instrlist_meta_preinsert(ilist, where, XINST_CREATE_load_1byte
(drcontext,
opnd_create_reg(regaddr),
OPND_CREATE_MEM8(regaddr, offs)));
return true;
}
bool
drtaint_shadow_get_reg_taint(void *drcontext, reg_id_t reg, byte *result)
{
per_thread_t *data = drmgr_get_tls_field(drcontext, tls_index);
if (reg - DR_REG_R0 >= DR_NUM_GPR_REGS)
return false;
*result = data->shadow_gprs[reg - DR_REG_R0];
return true;
}
bool
drtaint_shadow_set_reg_taint(void *drcontext, reg_id_t reg, byte value)
{
per_thread_t *data = drmgr_get_tls_field(drcontext, tls_index);
if (reg - DR_REG_R0 >= DR_NUM_GPR_REGS)
return false;
data->shadow_gprs[reg - DR_REG_R0] = value;
return true;
}
static void
drtaint_shadow_reg_exit(void)
{
drmgr_unregister_tls_field(tls_index);
drmgr_unregister_thread_init_event(event_thread_init);
drmgr_unregister_thread_exit_event(event_thread_exit);
drmgr_exit();
}
static void
event_thread_init(void *drcontext)
{
per_thread_t *data = dr_thread_alloc(drcontext, sizeof(per_thread_t));
memset(data, 0, sizeof(per_thread_t));
drmgr_set_tls_field(drcontext, tls_index, data);
}
static void
event_thread_exit(void *drcontext)
{
per_thread_t *data = drmgr_get_tls_field(drcontext, tls_index);
dr_thread_free(drcontext, data, sizeof(per_thread_t));
}