Skip to content

Commit bbbed20

Browse files
committed
Update pri_taint hypercall for LAVA - Some interesting issue of infinite loop is coming up
1 parent 7cf4a51 commit bbbed20

File tree

3 files changed

+60
-62
lines changed

3 files changed

+60
-62
lines changed

panda/plugins/hypercaller/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ This was designed primarily for Python use cases:
5151
MAGIC = 0x12345678
5252
@panda.hypercall(MAGIC)
5353
def hypercall(cpu):
54-
print("Hello from my hypercall!"
54+
print("Hello from my hypercall!")
5555

5656
```
5757

@@ -64,7 +64,7 @@ It's much easier to handle this from Python, but here's an example of how you mi
6464
#include <panda/plugin.h>
6565
#include <hypercaller/hypercaller.h>
6666

67-
hypercall_t* register_hypercall;
67+
register_hypercall_t register_hypercall;
6868

6969
void my_hypercall(CPUState *cpu) {
7070
printf("Hello from my hypercall!\n");
@@ -76,7 +76,7 @@ bool init_plugin(void *self) {
7676
panda_require("hypercaller");
7777
hypercaller = panda_get_plugin_by_name("hypercaller");
7878
}
79-
register_hypercall = (hypercall_t*)dlsym(hypercaller, "register_hypercall");
79+
register_hypercall_t register_hypercall = (register_hypercall_t) dlsym(hypercaller, "register_hypercall");
8080
register_hypercall(0x12345678, my_hypercall);
8181
return true;
8282
}

panda/plugins/hypercaller/hypercaller.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// files in this directory that contain subsections like this one.
77

88
typedef void (*hypercall_t)(CPUState *cpu);
9+
typedef void (*register_hypercall_t)(uint32_t, hypercall_t);
910
void register_hypercall(uint32_t magic, hypercall_t);
1011
void unregister_hypercall(uint32_t magic);
1112

panda/plugins/pri_taint/pri_taint.cpp

Lines changed: 56 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include "callstack_instr/callstack_instr.h"
1616

1717
extern "C" {
18+
// hypercalls
19+
#include <hypercaller/hypercaller.h>
1820

1921
#include "panda/rr/rr_log.h"
2022
#include "panda/plog.h"
@@ -37,10 +39,11 @@ extern "C" {
3739
bool init_plugin(void *);
3840
void uninit_plugin(void *);
3941

40-
int get_loglevel() ;
42+
int get_loglevel();
4143
void set_loglevel(int new_loglevel);
4244
}
4345

46+
#define LAVA_MAGIC 0xabcd
4447
const char *global_src_filename = NULL;
4548
uint64_t global_src_linenum;
4649
unsigned global_ast_loc_id;
@@ -308,6 +311,8 @@ void pfun(void *var_ty_void, const char *var_nm, LocType loc_t, target_ulong loc
308311
}
309312
// free(si);
310313
}
314+
#endif
315+
311316
/*
312317
void on_line_change(CPUState *cpu, target_ulong pc, const char *file_Name, const char *funct_name, unsigned long long lno){
313318
if (taint2_enabled()){
@@ -334,63 +339,69 @@ void hypercall_log_trace(unsigned ast_loc_id) {
334339
*/
335340
#ifdef TARGET_I386
336341
// Support all features of label and query program
337-
bool i386_hypercall_callback(CPUState *cpu) {
342+
void i386_hypercall_callback(CPUState *cpu) {
338343
if (debug) {
339344
printf("[pri_taint] Calling i386 hypercall callback!\n");
340345
}
341-
bool ret = false;
342-
CPUArchState *env = (CPUArchState*)cpu->env_ptr;
346+
CPUArchState *env = (CPUArchState*) cpu->env_ptr;
343347
if (taint2_enabled()) {
344348
// LAVA Hypercall
345-
target_ulong addr = panda_virt_to_phys(cpu, env->regs[R_EAX]);
346-
if ((int)addr == -1) {
347-
printf ("[pri_taint] panda hypercall with ptr to invalid PandaHypercallStruct: vaddr=0x%x paddr=0x%x\n",
348-
(uint32_t) env->regs[R_EAX], (uint32_t) addr);
349+
#ifdef TARGET_X86_64
350+
target_ulong addr = panda_virt_to_phys(cpu, env->regs[R_EDI]);
351+
#else
352+
target_ulong addr = panda_virt_to_phys(cpu, env->regs[R_EBX]);
353+
#endif
354+
355+
if ((int) addr == -1) {
356+
#ifdef TARGET_X86_64
357+
printf ("[pri_taint] panda hypercall with ptr to invalid PandaHypercallStruct: vaddr=0x%x paddr=0x%x\n",
358+
(uint32_t) env->regs[R_EDI], (uint32_t) addr);
359+
#else
360+
printf ("[pri_taint] panda hypercall with ptr to invalid PandaHypercallStruct: vaddr=0x%x paddr=0x%x\n",
361+
(uint32_t) env->regs[R_EBX], (uint32_t) addr);
362+
#endif
349363
}
350364
else if (pandalog) {
351365
if (debug) {
352366
printf("[pri_taint] Hypercall is OK and Panda Log is set\n");
353367
}
354368
PandaHypercallStruct phs;
355-
panda_virtual_memory_read(cpu, env->regs[R_EAX], (uint8_t *) &phs, sizeof(phs));
369+
#ifdef TARGET_X86_64
370+
panda_virtual_memory_read(cpu, env->regs[R_EDI], (uint8_t *) &phs, sizeof(phs));
371+
#else
372+
panda_virtual_memory_read(cpu, env->regs[R_EBX], (uint8_t *) &phs, sizeof(phs));
373+
#endif
356374

357375
// To be used for chaff bugs?
358376
uint64_t funcaddr = 0;
359377
panda_virtual_memory_read(cpu, phs.info, (uint8_t*)&funcaddr, sizeof(target_ulong));
360378

361-
if (phs.magic == 0xabcd) {
362-
// if the phs action is a pri_query point, see
363-
// lava/include/pirate_mark_lava.h
364-
if (phs.action == 13) {
365-
target_ulong pc = panda_current_pc(cpu);
366-
SrcInfo info;
367-
int rc = pri_get_pc_source_info(cpu, pc, &info);
368-
if (!rc) {
369-
struct args args = {cpu, info.filename, info.line_number, phs.src_filename, funcaddr};
370-
dprintf("[pri_taint] panda hypercall: [%s], "
371-
"ln: %4ld, pc @ 0x" TARGET_FMT_lx "\n",
372-
info.filename,
373-
info.line_number,pc);
374-
pri_funct_livevar_iter(cpu, pc, (liveVarCB) pfun, (void *)&args);
375-
//lava_attack_point(phs);
376-
}
377-
else {
378-
if (debug) {
379-
printf("[pri_taint] pri_get_pc_src_info has failed: %d != 0.\n", rc);
380-
}
381-
}
382-
ret = true;
383-
// hypercall_log_trace(phs.src_filename);
379+
// if the phs action is a pri_query point, see
380+
// lava/include/pirate_mark_lava.h
381+
if (phs.action == 13) {
382+
target_ulong pc = panda_current_pc(cpu);
383+
SrcInfo info;
384+
int rc = pri_get_pc_source_info(cpu, pc, &info);
385+
if (!rc) {
386+
struct args args = {cpu, info.filename, info.line_number, phs.src_filename, funcaddr};
387+
dprintf("[pri_taint] panda hypercall: [%s], "
388+
"ln: %4ld, pc @ 0x" TARGET_FMT_lx "\n",
389+
info.filename,
390+
info.line_number,pc);
391+
pri_funct_livevar_iter(cpu, pc, (liveVarCB) pfun, (void *)&args);
384392
}
385393
else {
386394
if (debug) {
387-
printf("[pri_taint] Invalid action value in PHS struct: %d != 13.\n", phs.action);
388-
}
395+
printf("[pri_taint] pri_get_pc_src_info has failed: %d != 0.\n", rc);
396+
}
389397
}
398+
// hypercall_log_trace(phs.src_filename);
390399
}
391400
else {
392-
printf("[pri_taint] Invalid magic value in PHS struct: %x != 0xabcd.\n", phs.magic);
393-
}
401+
if (debug) {
402+
printf("[pri_taint] Invalid action value in PHS struct: %d != 13.\n", phs.action);
403+
}
404+
}
394405
}
395406
else {
396407
if (debug) {
@@ -403,24 +414,9 @@ bool i386_hypercall_callback(CPUState *cpu) {
403414
printf("[pri_taint] taint2 is not enabled (hypercall)\n");
404415
}
405416
}
406-
return ret;
407417
}
408418
#endif // TARGET_I386
409419

410-
411-
bool guest_hypercall_callback(CPUState *cpu) {
412-
#ifdef TARGET_I386
413-
return i386_hypercall_callback(cpu);
414-
#endif
415-
416-
#ifdef TARGET_ARM
417-
// not implemented for now
418-
//arm_hypercall_callback(cpu);
419-
#endif
420-
421-
return false;
422-
}
423-
#endif
424420
/*
425421
void on_taint_change(Addr a, uint64_t size){
426422
uint32_t num_tainted = 0;
@@ -448,26 +444,27 @@ bool init_plugin(void *self) {
448444
panda_require("taint2");
449445
assert(init_taint2_api());
450446

451-
panda_cb pcb;
452-
pcb.guest_hypercall = guest_hypercall_callback;
453-
panda_register_callback(self, PANDA_CB_GUEST_HYPERCALL, pcb);
454-
printf("[pri_taint] This plugin is activated!\n");
455-
456447
// If taint isn't already enabled, turn it on.
457448
if (!taint2_enabled()) {
458449
printf("[pri_taint] enabling taint now!\n");
459450
taint2_enable_taint();
460451
}
452+
453+
panda_require("hypercaller");
454+
void * hypercaller = panda_get_plugin_by_name("hypercaller");
455+
register_hypercall_t register_hypercall = (register_hypercall_t) dlsym(hypercaller, "register_hypercall");
456+
register_hypercall(LAVA_MAGIC, i386_hypercall_callback);
457+
458+
printf("[pri_taint] This plugin is activated!\n");
461459
return true;
462460
#else
463461
printf("[pri_taint] This plugin is only supported on x86\n");
464462
return false;
465-
//taint2_track_taint_state();
466463
#endif
467464
}
468465

469-
470-
471466
void uninit_plugin(void *self) {
467+
unregister_hypercall(LAVA_MAGIC);
468+
printf("[pri_taint] Unloading plugin\n");
472469
}
473470

0 commit comments

Comments
 (0)