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
|
// The one thing asked of capstone, in C so that its structs stay its own.
#include <capstone/capstone.h>
#include <stdio.h>
static csh handle;
// A memory operand of an instruction, worked out against the registers.
struct zt_touch {
uint64_t addr;
// A rep string instruction's is the whole range its count covers.
uint32_t size;
// 1 the instruction reads it, 2 it writes it, 3 both.
uint8_t how;
};
// Where an instruction that is a call, a jump or a return goes.
struct zt_branch {
// The target; with `indirect`, the address the target is read from.
uint64_t target;
// 0 not a branch, 1 call, 2 jump, 3 return (the target is on the stack).
uint8_t kind;
// A conditional jump whose condition does not hold is not taken.
uint8_t taken;
uint8_t indirect;
// A jump on a condition.
uint8_t conditional;
};
// Whether the flags (and rcx) make a conditional jump go.
static int holds(unsigned id, uint64_t flags, uint64_t rcx) {
int cf = flags & 1, pf = flags >> 2 & 1, zf = flags >> 6 & 1, sf = flags >> 7 & 1, of = flags >> 11 & 1;
switch (id) {
case X86_INS_JO: return of;
case X86_INS_JNO: return !of;
case X86_INS_JB: return cf;
case X86_INS_JAE: return !cf;
case X86_INS_JE: return zf;
case X86_INS_JNE: return !zf;
case X86_INS_JBE: return cf || zf;
case X86_INS_JA: return !cf && !zf;
case X86_INS_JS: return sf;
case X86_INS_JNS: return !sf;
case X86_INS_JP: return pf;
case X86_INS_JNP: return !pf;
case X86_INS_JL: return sf != of;
case X86_INS_JGE: return sf == of;
case X86_INS_JLE: return zf || sf != of;
case X86_INS_JG: return !zf && sf == of;
case X86_INS_JRCXZ: return rcx == 0;
case X86_INS_JECXZ: return (uint32_t)rcx == 0;
default: return 1;
}
}
// Where `reg` is in the kernel's user_regs_struct, as 64-bit words; -1 for
// a register an address is not made of here.
static int slot(x86_reg reg) {
switch (reg) {
case X86_REG_R15: return 0;
case X86_REG_R14: return 1;
case X86_REG_R13: return 2;
case X86_REG_R12: return 3;
case X86_REG_RBP: return 4;
case X86_REG_RBX: return 5;
case X86_REG_R11: return 6;
case X86_REG_R10: return 7;
case X86_REG_R9: return 8;
case X86_REG_R8: return 9;
case X86_REG_RAX: return 10;
case X86_REG_RCX: return 11;
case X86_REG_RDX: return 12;
case X86_REG_RSI: return 13;
case X86_REG_RDI: return 14;
case X86_REG_RSP: return 19;
default: return -1;
}
}
// The instruction at `code`, which is at `addr`: its text into `text`, its
// length returned; 0 where there is no instruction. With `regs` (the
// thread's, about to run it), the memory it touches into `touches`, at
// most `*count` of them, and how many into `*count`. A branch is told
// without them too, its target where the instruction alone says it.
size_t zt_disasm(const uint8_t *code, size_t len, uint64_t addr, char *text, size_t cap, const uint64_t *regs, struct zt_touch *touches, size_t *count, struct zt_branch *branch) {
if (!handle) {
if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) != CS_ERR_OK) return 0;
cs_option(handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_INTEL);
cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
}
size_t room = count ? *count : 0;
if (count) *count = 0;
cs_insn *insn;
if (cs_disasm(handle, code, len, addr, 1, &insn) != 1) return 0;
size_t size = insn->size;
snprintf(text, cap, insn->op_str[0] ? "%s %s" : "%s", insn->mnemonic, insn->op_str);
if (branch && insn->detail) {
const cs_x86 *x = &insn->detail->x86;
branch->kind = cs_insn_group(handle, insn, CS_GRP_RET) ? 3 : cs_insn_group(handle, insn, CS_GRP_CALL) ? 1 : cs_insn_group(handle, insn, CS_GRP_JUMP) ? 2 : 0;
branch->taken = regs ? holds(insn->id, regs[18], regs[11]) : 1;
branch->indirect = 0;
branch->target = 0;
branch->conditional = branch->kind == 2 && insn->id != X86_INS_JMP && insn->id != X86_INS_LJMP;
if (branch->kind == 1 || branch->kind == 2) {
const cs_x86_op *op = &x->operands[0];
if (x->op_count < 1) branch->kind = 0;
else if (op->type == X86_OP_IMM) branch->target = (uint64_t)op->imm;
else if (op->type == X86_OP_REG && slot(op->reg) >= 0) branch->target = regs ? regs[slot(op->reg)] : 0;
else if (op->type == X86_OP_MEM) {
// Its address is the first touch below: the target is what is there.
branch->indirect = 1;
} else branch->kind = 0;
}
}
// lea and the long nops name memory and touch none.
if (regs && insn->detail && insn->id != X86_INS_LEA && insn->id != X86_INS_NOP) {
const cs_x86 *x = &insn->detail->x86;
// A string instruction (movs, cmps, stos, lods, scas: the one-byte
// opcodes a4 to af but test's a8 and a9) repeated touches its count's
// elements from rsi or rdi on, or down to them with the direction flag set.
uint8_t code0 = x->opcode[0];
int string = x->opcode[1] == 0 && code0 >= 0xa4 && code0 <= 0xaf && code0 != 0xa8 && code0 != 0xa9;
uint64_t times = string && (x->prefix[0] == X86_PREFIX_REP || x->prefix[0] == X86_PREFIX_REPNE) ? regs[11] : 1;
for (int i = 0; i < x->op_count && *count < room && times > 0; i++) {
const cs_x86_op *op = &x->operands[i];
if (op->type != X86_OP_MEM || !(op->access & (CS_AC_READ | CS_AC_WRITE))) continue;
uint64_t at = (uint64_t)op->mem.disp;
if (op->mem.base == X86_REG_RIP) at += addr + size;
else if (op->mem.base != X86_REG_INVALID) {
if (slot(op->mem.base) < 0) continue;
at += regs[slot(op->mem.base)];
}
if (op->mem.index != X86_REG_INVALID) {
if (slot(op->mem.index) < 0) continue;
at += regs[slot(op->mem.index)] * (uint64_t)op->mem.scale;
}
if (op->mem.segment == X86_REG_FS) at += regs[21];
if (op->mem.segment == X86_REG_GS) at += regs[22];
uint64_t size = op->size * times;
if (regs[18] & 0x400) at -= (times - 1) * op->size;
touches[*count].addr = at;
touches[*count].size = size > UINT32_MAX ? UINT32_MAX : (uint32_t)size;
touches[*count].how = (op->access & CS_AC_READ ? 1 : 0) | (op->access & CS_AC_WRITE ? 2 : 0);
++*count;
}
// The stack's own instructions touch memory no operand names, after
// what their operands touch: a push writes the word under rsp, a
// call its return address there, enter the saved rbp; a pop and a
// return read the word at rsp, leave the saved rbp at rbp. A word is
// 8 bytes, 2 with the operand-size prefix (pushf, push ax).
uint64_t rsp = regs[19];
uint32_t word = x->prefix[2] == 0x66 ? 2 : 8;
struct zt_touch t = { 0, 0, 0 };
switch (insn->id) {
case X86_INS_PUSH: case X86_INS_PUSHF: case X86_INS_PUSHFQ: t = (struct zt_touch){ rsp - word, word, 2 }; break;
case X86_INS_CALL: case X86_INS_ENTER: t = (struct zt_touch){ rsp - 8, 8, 2 }; break;
case X86_INS_POP: case X86_INS_POPF: case X86_INS_POPFQ: t = (struct zt_touch){ rsp, word, 1 }; break;
case X86_INS_RET: t = (struct zt_touch){ rsp, 8, 1 }; break;
case X86_INS_LEAVE: t = (struct zt_touch){ regs[4], 8, 1 }; break;
default: break;
}
if (t.size && *count < room) touches[(*count)++] = t;
}
cs_free(insn, 1);
return size;
}
|