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
|
#include "PeiResolver.h"
bool PeiResolver::resolvePeiIdt()
{
string archName = m_view->GetDefaultArchitecture()->GetName();
string intrinsicName;
if (archName == "x86")
intrinsicName = "IDTR32";
else
intrinsicName = "IDTR64";
auto refs = m_view->GetCodeReferencesForType(QualifiedName(intrinsicName));
for (auto ref : refs)
{
if (m_task->IsCancelled())
return false;
auto mlil = ref.func->GetMediumLevelIL();
auto instrIdx = mlil->GetInstructionStart(m_view->GetDefaultArchitecture(), ref.addr);
auto instr = mlil->GetInstruction(instrIdx);
auto hlil = ref.func->GetHighLevelIL();
auto hlils = HighLevelILExprsAt(ref.func, m_view->GetDefaultArchitecture(), ref.addr);
for (auto expr : hlils)
{
if (expr.operation != HLIL_INTRINSIC || expr.GetParent().operation != HLIL_ASSIGN
|| expr.GetParent().GetDestExpr<HLIL_ASSIGN>().operation != HLIL_STRUCT_FIELD
|| expr.GetParent().GetDestExpr<HLIL_ASSIGN>().GetSourceExpr<HLIL_STRUCT_FIELD>().operation != HLIL_VAR)
continue;
auto var = expr.GetParent().GetDestExpr<HLIL_ASSIGN>().GetSourceExpr<HLIL_STRUCT_FIELD>().GetVariable();
ref.func->CreateUserVariable(var, m_view->GetTypeByName(QualifiedName(intrinsicName)), intrinsicName);
}
if (instr.operation == MLIL_INTRINSIC)
{
// binja doesn't do type propagation on intrinsic instructions
auto output_params = instr.GetOutputVariables<MLIL_INTRINSIC>();
if (output_params.size() < 1)
continue;
ref.func->CreateUserVariable(
output_params[0], m_view->GetTypeByName(QualifiedName(intrinsicName)), intrinsicName);
}
m_view->UpdateAnalysisAndWait();
}
// TODO There is an issue related to structure's type propagation, binja doesn't propagate indirect structure access
// properly
// here is a temporary fix, should be removed after vector35/binaryninja/#749 got fixed
refs = m_view->GetCodeReferencesForType(QualifiedName("EFI_PEI_SERVICES"));
for (auto ref : refs)
{
if (m_task->IsCancelled())
return false;
auto mlil = ref.func->GetMediumLevelIL();
auto instrIdx = mlil->GetInstructionStart(m_view->GetDefaultArchitecture(), ref.addr);
auto instr = mlil->GetInstruction(instrIdx);
if (instr.operation != MLIL_SET_VAR)
continue;
if (instr.GetSourceExpr<MLIL_SET_VAR>().operation != MLIL_LOAD_STRUCT)
continue;
ref.func->CreateUserVariable(instr.GetDestVariable<MLIL_SET_VAR>(),
mlil->GetExprType(instr.GetSourceExpr<MLIL_SET_VAR>()).GetValue(),
nonConflictingLocalName(ref.func, "EfiPeiServices"));
m_view->UpdateAnalysisAndWait();
}
return true;
}
bool PeiResolver::resolvePeiMrc()
{
auto funcs = m_view->GetAnalysisFunctionList();
for (auto func : funcs)
{
if (m_task->IsCancelled())
return false;
auto mlil = func->GetMediumLevelIL();
auto blocks = mlil->GetBasicBlocks();
for (auto block : blocks)
{
for (size_t i = block->GetStart(); i < block->GetEnd(); i++)
{
auto instr = mlil->GetInstruction(i);
if (instr.operation != MLIL_INTRINSIC)
continue;
uint32_t intrinsicIdx = instr.GetIntrinsic<MLIL_INTRINSIC>();
if (m_view->GetDefaultArchitecture()->GetIntrinsicName(intrinsicIdx) != "Coproc_GetOneWord")
continue;
auto intrinsicParams = instr.GetParameterExprs<MLIL_INTRINSIC>();
if (intrinsicParams.size() != 5)
continue;
bool found = true;
const int value[5] = {0xf, 0x0, 0xd, 0x0, 0x2};
for (int j = 0; j < 5; j++)
{
auto param = intrinsicParams[j];
if (param.operation != MLIL_CONST)
{
found = false;
break;
}
if (param.GetConstant<MLIL_CONST>() != value[j])
{
found = false;
break;
}
}
if (!found)
continue;
// At this point, we can make sure this instruction fetches EFI_PEI_SERVICES
auto output = instr.GetOutputVariables();
if (output.size() > 0)
{
auto pointerType = Type::PointerType(m_view->GetDefaultArchitecture(),
Type::PointerType(m_view->GetDefaultArchitecture(),
m_view->GetTypeByName(QualifiedName("EFI_PEI_SERVICES"))));
func->CreateUserVariable(output[0], pointerType, nonConflictingLocalName(func, "PeiServices"));
m_view->UpdateAnalysisAndWait();
}
}
}
}
return true;
}
bool PeiResolver::resolvePeiMrs()
{
// ideally we don't need this function, but since we don't support type propagation on intrinsic instructions
// we have to manually propagate it
auto refs = m_view->GetCodeReferencesForType(QualifiedName("EFI_PEI_SERVICES"));
for (auto ref : refs)
{
if (m_task->IsCancelled())
return false;
auto mlil = ref.func->GetMediumLevelIL();
auto instrIdx = mlil->GetInstructionStart(m_view->GetDefaultArchitecture(), ref.addr);
auto instr = mlil->GetInstruction(instrIdx);
if (instr.operation == MLIL_INTRINSIC)
{
auto params = instr.GetOutputVariables();
if (params.size() < 1)
continue;
auto pointerType = Type::PointerType(m_view->GetDefaultArchitecture(),
Type::PointerType(
m_view->GetDefaultArchitecture(), m_view->GetTypeByName(QualifiedName("EFI_PEI_SERVICES"))));
ref.func->CreateUserVariable(params[0], pointerType, nonConflictingLocalName(ref.func, "EfiPeiServices"));
m_view->UpdateAnalysisAndWait();
}
}
return true;
}
bool PeiResolver::resolvePlatformPointers()
{
m_task->SetProgressText("Resolving PEI Services Pointers...");
string archName = m_view->GetDefaultArchitecture()->GetName();
string intrinsicTypeName;
if (archName == "x86" || archName == "x86-64")
{
return resolvePeiIdt();
}
else if (archName == "arm" || archName == "thumb2")
{
return resolvePeiMrc();
}
else if (archName == "aarch64")
{
return resolvePeiMrs();
}
LogError("Not supported arch: %s", archName.c_str());
return false;
}
bool PeiResolver::resolvePeiDescriptors()
{
m_task->SetProgressText("Defining PEI Descriptors...");
const string descriptorNames[2] = {"EFI_PEI_NOTIFY_DESCRIPTOR", "EFI_PEI_PPI_DESCRIPTOR"};
for (auto descriptor : descriptorNames)
{
auto refs = m_view->GetCodeReferencesForType(QualifiedName(descriptor));
for (auto ref : refs)
{
if (m_task->IsCancelled())
return false;
auto mlil = ref.func->GetMediumLevelIL();
auto instrIdx = mlil->GetInstructionStart(m_view->GetDefaultArchitecture(), ref.addr);
auto instr = mlil->GetInstruction(instrIdx);
if (instr.operation != MLIL_CALL && instr.operation != MLIL_TAILCALL)
continue;
auto destExpr = instr.GetDestExpr();
if (destExpr.operation != MLIL_LOAD_STRUCT)
continue;
// at this point this instruction is probably a call to LocatPpi, InstallPpi or NotifyPpi
if (!mlil->GetExprType(destExpr).GetValue()->IsPointer())
continue;
auto funcType = mlil->GetExprType(destExpr).GetValue()->GetChildType().GetValue();
auto params = funcType->GetParameters();
int targetParamIdx = -1;
for (int i = 0; i < params.size(); i++)
{
auto param = params[i];
if (!param.type.GetValue()->IsPointer())
continue;
auto paramTypeName = param.type.GetValue()->GetChildType().GetValue()->GetTypeName().GetString();
if (paramTypeName.find(descriptor) != paramTypeName.npos)
{
// this is the param
targetParamIdx = i;
break;
}
}
if (targetParamIdx < 0)
continue;
// Now we are confident that this position is a call that pass Descriptor as a parameter
defineTypeAtCallsite(ref.func, ref.addr, descriptor, targetParamIdx, true);
}
}
return true;
}
bool PeiResolver::resolvePeiServices()
{
m_task->SetProgressText("Resolving PPIs...");
auto refs = m_view->GetCodeReferencesForType(QualifiedName("EFI_PEI_SERVICES"));
for (auto ref : refs)
{
if (m_task->IsCancelled())
return false;
auto func = ref.func;
auto mlil = func->GetMediumLevelIL();
if (!mlil)
continue;
auto mlilSsa = mlil->GetSSAForm();
size_t mlilIdx = mlil->GetInstructionStart(m_view->GetDefaultArchitecture(), ref.addr);
auto instr = mlilSsa->GetInstruction(mlil->GetSSAInstructionIndex(mlilIdx));
if (instr.operation == MLIL_CALL_SSA || instr.operation == MLIL_TAILCALL_SSA)
{
auto dest = instr.GetDestExpr();
if (dest.operation != MLIL_LOAD_STRUCT_SSA)
continue;
auto offset = dest.GetOffset();
if (offset == 0x18 + m_width * 2)
{
// LocatePpi
resolveGuidInterface(ref.func, ref.addr, 1, 4);
}
else if (offset == 0x18 || offset == 0x18 + m_width || offset == 0x18 + m_width * 3)
{
// InstallPpi, ReinstallPpi, NotifyPpi
}
}
}
return true;
}
bool PeiResolver::resolvePei()
{
if (!setModuleEntry(PEI))
return false;
if (!resolvePlatformPointers())
return false;
if (!resolvePeiDescriptors())
return false;
if (!resolvePeiServices())
return false;
return true;
}
PeiResolver::PeiResolver(Ref<BinaryView> view, Ref<BackgroundTask> task) : Resolver(view, task)
{
initProtocolMapping();
setModuleEntry(PEI);
}
|