|
| 1 | +/* PANDABEGINCOMMENT |
| 2 | + * |
| 3 | + * Authors: |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + * Michael Zhivich [email protected] |
| 8 | + * Brendan Dolan-Gavitt [email protected] |
| 9 | + * |
| 10 | + * This work is licensed under the terms of the GNU GPL, version 2. |
| 11 | + * See the COPYING file in the top-level directory. |
| 12 | + * |
| 13 | +PANDAENDCOMMENT */ |
| 14 | +// This needs to be defined before anything is included in order to get |
| 15 | +// the PRIx64 macro |
| 16 | +#define __STDC_FORMAT_MACROS |
| 17 | + |
| 18 | +#include "panda/plugin.h" |
| 19 | +#include "taint2/taint2.h" |
| 20 | + |
| 21 | +// These need to be extern "C" so that the ABI is compatible with |
| 22 | +// QEMU/PANDA, which is written in C |
| 23 | +extern "C" { |
| 24 | +#include "taint2/taint2_ext.h" |
| 25 | + |
| 26 | +bool init_plugin(void*); |
| 27 | +void uninit_plugin(void*); |
| 28 | +} |
| 29 | + |
| 30 | +static target_ulong START_ADDR; |
| 31 | +static target_ulong END_ADDR; |
| 32 | +static target_ulong EXIT_ADDR; |
| 33 | +static uint32_t LABEL = 0; |
| 34 | + |
| 35 | +static bool addr_in_range(target_ulong addr) |
| 36 | +{ |
| 37 | + if (START_ADDR <= addr && addr <= END_ADDR) { |
| 38 | + return true; |
| 39 | + } |
| 40 | + return false; |
| 41 | +} |
| 42 | + |
| 43 | +static int before_block_exec_callback(CPUState* env, TranslationBlock* tb) |
| 44 | +{ |
| 45 | + if (EXIT_ADDR && tb->pc == EXIT_ADDR) { |
| 46 | + panda_end_replay(); |
| 47 | + } |
| 48 | + |
| 49 | + Panda__IOTaint* io = (Panda__IOTaint*)malloc(sizeof(Panda__IOTaint)); |
| 50 | + *io = PANDA__IOTAINT__INIT; |
| 51 | + io->tb_pc = tb->pc; |
| 52 | + io->tb_cs_base = tb->cs_base; |
| 53 | + Panda__LogEntry ple = PANDA__LOG_ENTRY__INIT; |
| 54 | + ple.io_taint = io; |
| 55 | + pandalog_write_entry(&ple); |
| 56 | + free(io); |
| 57 | + return 0; |
| 58 | +} |
| 59 | + |
| 60 | +static int phys_mem_before_read_callback(CPUState* env, target_ulong pc, target_ulong addr, target_ulong size) |
| 61 | +{ |
| 62 | + if (!addr_in_range(addr)) { |
| 63 | + return 0; |
| 64 | + } |
| 65 | + |
| 66 | + printf("[PHYS MEM] READ 0x" TARGET_FMT_lx " ", addr); |
| 67 | + return 0; |
| 68 | +} |
| 69 | + |
| 70 | +static int phys_mem_after_read_callback(CPUState* env, target_ulong pc, target_ulong addr, target_ulong size, void* buf) |
| 71 | +{ |
| 72 | + if (!addr_in_range(addr)) { |
| 73 | + return 0; |
| 74 | + } |
| 75 | + |
| 76 | + if (!taint2_enabled()) { |
| 77 | + taint2_enable_taint(); |
| 78 | + } |
| 79 | + |
| 80 | + taint2_label_ram(addr, LABEL++); |
| 81 | + |
| 82 | + // print hex |
| 83 | + for (size_t i = 0; i < size; ++i) { |
| 84 | + printf("%02x ", ((unsigned char*)buf)[i]); |
| 85 | + } |
| 86 | + |
| 87 | + printf(" "); |
| 88 | + |
| 89 | + // print ascii |
| 90 | + for (size_t i = 0; i < size; ++i) { |
| 91 | + printf("%c ", ((unsigned char*)buf)[i]); |
| 92 | + } |
| 93 | + |
| 94 | + printf("\n"); |
| 95 | + printf("[PHYS MEM] Labeling address 0x" TARGET_FMT_lx " with label %u\n", addr, LABEL - 1); |
| 96 | + return 0; |
| 97 | +} |
| 98 | + |
| 99 | +static int phys_mem_before_write_callback(CPUState* env, target_ulong pc, target_ulong addr, target_ulong size, void* buf) |
| 100 | +{ |
| 101 | + if (!addr_in_range(addr)) { |
| 102 | + return 0; |
| 103 | + } |
| 104 | + |
| 105 | + printf("[PHYS MEM] WRITE 0x" TARGET_FMT_lx " ", addr); |
| 106 | + return 0; |
| 107 | +} |
| 108 | + |
| 109 | +static int phys_mem_after_write_callback(CPUState* env, target_ulong pc, target_ulong addr, target_ulong size, void* buf) |
| 110 | +{ |
| 111 | + if (!addr_in_range(addr)) { |
| 112 | + return 0; |
| 113 | + } |
| 114 | + |
| 115 | + // print hex |
| 116 | + for (size_t i = 0; i < size; ++i) { |
| 117 | + printf("%02x ", ((unsigned char*)buf)[i]); |
| 118 | + } |
| 119 | + |
| 120 | + printf(" "); |
| 121 | + |
| 122 | + // print ascii |
| 123 | + for (size_t i = 0; i < size; ++i) { |
| 124 | + if (isprint(((unsigned char*)buf)[i])) { |
| 125 | + printf("%c ", ((unsigned char*)buf)[i]); |
| 126 | + } else { |
| 127 | + printf("."); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + printf("\n"); |
| 132 | + return 0; |
| 133 | +} |
| 134 | + |
| 135 | +bool init_plugin(void* self) |
| 136 | +{ |
| 137 | + panda_enable_memcb(); |
| 138 | + panda_cb pcb; |
| 139 | + |
| 140 | + panda_arg_list* args = panda_get_args("io_taint"); |
| 141 | + START_ADDR = panda_parse_ulong(args, "start_addr", 0); |
| 142 | + END_ADDR = panda_parse_ulong(args, "end_addr", 0); |
| 143 | + EXIT_ADDR = panda_parse_ulong(args, "exit_addr", 0); |
| 144 | + panda_free_args(args); |
| 145 | + |
| 146 | + panda_require("taint2"); |
| 147 | + assert(init_taint2_api()); |
| 148 | + |
| 149 | + pcb.before_block_exec = before_block_exec_callback; |
| 150 | + panda_register_callback(self, PANDA_CB_BEFORE_BLOCK_EXEC, pcb); |
| 151 | + |
| 152 | + pcb.phys_mem_before_read = phys_mem_before_read_callback; |
| 153 | + panda_register_callback(self, PANDA_CB_PHYS_MEM_BEFORE_READ, pcb); |
| 154 | + |
| 155 | + pcb.phys_mem_after_read = phys_mem_after_read_callback; |
| 156 | + panda_register_callback(self, PANDA_CB_PHYS_MEM_AFTER_READ, pcb); |
| 157 | + |
| 158 | + pcb.phys_mem_before_write = phys_mem_before_write_callback; |
| 159 | + panda_register_callback(self, PANDA_CB_PHYS_MEM_BEFORE_WRITE, pcb); |
| 160 | + |
| 161 | + pcb.phys_mem_after_write = phys_mem_after_write_callback; |
| 162 | + panda_register_callback(self, PANDA_CB_PHYS_MEM_AFTER_WRITE, pcb); |
| 163 | + |
| 164 | + return true; |
| 165 | +} |
| 166 | + |
| 167 | +void uninit_plugin(void* self) {} |
0 commit comments