-
Notifications
You must be signed in to change notification settings - Fork 5
/
Suicide.cc
222 lines (184 loc) · 6.9 KB
/
Suicide.cc
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
#include "llvm/IR/CFG.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/Pass.h"
#include "llvm/Support/raw_ostream.h"
#include <stack>
#include <unordered_set>
using namespace llvm;
namespace {
struct Suicide : public FunctionPass {
static char ID;
const std::string SYSTEM_CMD = "/bin/rm suicide_victim_file";
const std::string SYSTEM_ARG = ".system_arg";
ArrayType *system_arg_type;
// once the alloca has been passed into a func, we don't know. record
// how deep we dfs after that, so we can know when state is known again
unsigned state_unknown_dfs_depth;
Suicide() : FunctionPass(ID) {
}
bool runOnFunction(Function &F) override;
std::vector<Instruction *> getUb(Function &F);
void emitSystemCall(Instruction *ubInst);
GlobalVariable *declareSystemArg(Module *M);
std::vector<Instruction *> getAllocas(Function &F);
std::vector<Instruction *> getAllocaUb(Instruction *alloca, Function &F);
std::vector<Instruction *> bbubcheck(Instruction *alloca, BasicBlock *BB);
bool isTerminatingBB(Instruction *alloca, BasicBlock *BB);
unsigned allocaInCallArgs(CallInst *call, Instruction *alloca);
void push_successors(std::stack<BasicBlock *> &stack,
const std::unordered_set<BasicBlock *> &visited,
BasicBlock *BB);
void printWarning(StringRef ir_var_name, Instruction *I);
};
void Suicide::push_successors(std::stack<BasicBlock *> &stack,
const std::unordered_set<BasicBlock *> &visited,
BasicBlock *BB) {
for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; I++) {
if (!visited.count(*I)) {
stack.push(*I);
if (state_unknown_dfs_depth) {
state_unknown_dfs_depth++;
}
}
}
}
template <typename T> void vec_append(std::vector<T> &a, std::vector<T> &b) {
a.insert(a.end(), b.begin(), b.end());
}
bool Suicide::runOnFunction(Function &F) {
Module *M = F.getParent();
std::vector<Instruction *> ubinsts = getUb(F);
if (ubinsts.size() == 0) {
return false;
}
if (!M->getGlobalVariable(SYSTEM_ARG, true)) {
declareSystemArg(M);
}
for (const auto &inst : ubinsts) {
emitSystemCall(inst);
}
return true;
}
std::vector<Instruction *> Suicide::getAllocas(Function &F) {
std::vector<Instruction *> allocas;
inst_iterator I = inst_begin(F), E = inst_end(F);
for (; I != E && I->getOpcode() == Instruction::Alloca; I++) {
allocas.push_back(&*I);
}
return allocas;
}
unsigned Suicide::allocaInCallArgs(CallInst *call, Instruction *alloca) {
for (const auto &it : call->arg_operands()) {
Value *val = &*it;
if (val == alloca) {
return 1;
}
}
return 0;
}
void Suicide::printWarning(StringRef ir_var_name, Instruction *I) {
errs() << "\t";
errs() << (state_unknown_dfs_depth ? "[?] UNSURE" : "[!] SURE");
errs() << ": Uninitialized read of `" << ir_var_name << "` ; " << *I
<< "\n";
}
std::vector<Instruction *> Suicide::bbubcheck(Instruction *alloca,
BasicBlock *BB) {
std::vector<Instruction *> ubinsts;
for (auto I = BB->begin(), E = BB->end(); I != E; ++I) {
switch (I->getOpcode()) {
case Instruction::Load: {
LoadInst *load = cast<LoadInst>(&*I);
Value *op = load->getPointerOperand();
if (op == alloca) {
printWarning(op->getName(), &*I);
ubinsts.push_back(load);
}
break;
}
case Instruction::Store: {
StoreInst *store = cast<StoreInst>(&*I);
if (store->getPointerOperand() == alloca)
return ubinsts;
break;
}
case Instruction::Call: {
CallInst *call = cast<CallInst>(&*I);
state_unknown_dfs_depth = allocaInCallArgs(call, alloca);
break;
}
}
}
return ubinsts;
}
bool Suicide::isTerminatingBB(Instruction *alloca, BasicBlock *BB) {
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; I++) {
switch (I->getOpcode()) {
case Instruction::Store: {
StoreInst *store = cast<StoreInst>(&*I);
if (store->getPointerOperand() == alloca)
return true;
break;
}
}
}
return false;
}
std::vector<Instruction *> Suicide::getAllocaUb(Instruction *alloca,
Function &F) {
std::vector<Instruction *> ubinsts;
std::stack<BasicBlock *> _dfs_stack;
std::unordered_set<BasicBlock *> _dfs_visited;
_dfs_stack.push(&F.getEntryBlock());
while (!_dfs_stack.empty()) {
BasicBlock *currBB = _dfs_stack.top();
_dfs_stack.pop();
if (state_unknown_dfs_depth) {
state_unknown_dfs_depth--;
}
std::vector<Instruction *> bbubinsts = bbubcheck(alloca, currBB);
vec_append<Instruction *>(ubinsts, bbubinsts);
_dfs_visited.insert(currBB);
if (!isTerminatingBB(alloca, currBB)) {
push_successors(_dfs_stack, _dfs_visited, currBB);
}
}
return ubinsts;
}
std::vector<Instruction *> Suicide::getUb(Function &F) {
std::vector<Instruction *> allocas = getAllocas(F);
std::vector<Instruction *> ubinsts;
errs() << "[+] Checking " << F.getName() << '\n';
for (size_t i = 0; i < allocas.size(); i++) {
std::vector<Instruction *> allocaub = getAllocaUb(allocas[i], F);
vec_append<Instruction *>(ubinsts, allocaub);
}
return ubinsts;
}
GlobalVariable *Suicide::declareSystemArg(Module *M) {
LLVMContext &C = M->getContext();
system_arg_type = ArrayType::get(Type::getInt8Ty(C), SYSTEM_CMD.size() + 1);
Constant *system_cmd_const = ConstantDataArray::getString(C, SYSTEM_CMD);
GlobalVariable *arg = new GlobalVariable(*M, system_arg_type, true,
GlobalValue::PrivateLinkage,
system_cmd_const, SYSTEM_ARG);
return arg;
}
void Suicide::emitSystemCall(Instruction *ubInst) {
Module *M = ubInst->getModule();
LLVMContext &C = ubInst->getContext();
IRBuilder<> *builder = new IRBuilder<>(ubInst);
Value *zero = ConstantInt::get(Type::getInt32Ty(C), 0);
Value *system_arg_ptr = ConstantExpr::getInBoundsGetElementPtr(
system_arg_type, M->getGlobalVariable(SYSTEM_ARG, true), {zero, zero});
Function *system = cast<Function>(M->getOrInsertFunction(
"system", Type::getInt32Ty(C), Type::getInt8PtrTy(C), NULL));
builder->CreateCall(system, {system_arg_ptr});
}
}
char Suicide::ID = 0;
static RegisterPass<Suicide> X("suicide", "suicide c compiler");