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
|
#include "DxeResolver.h"
bool DxeResolver::resolveBootServices()
{
auto refs = m_view->GetCodeReferencesForType(QualifiedName("EFI_BOOT_SERVICES"));
// search reference of `EFI_BOOT_SERVICES` so that we can easily parse different 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 * 16 || offset == 0x18 + m_width * 32) {
// HandleProtocol, OpenProtocol
// Guid:1, Interface:2
resolveGuidInterface(ref.func, ref.addr, 1, 2);
} else if (offset == 0x18 + m_width * 37) {
// LocateProtocol
resolveGuidInterface(ref.func, ref.addr, 0, 2);
}
}
}
return true;
}
bool DxeResolver::resolveRuntimeServices()
{
auto refs = m_view->GetCodeReferencesForType(QualifiedName("EFI_RUNTIME_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 * 6 || offset == 0x18 + m_width * 8) {
// TODO implement this
// GetVariable and SetVariable
}
}
}
return true;
}
bool DxeResolver::resolveSmmTables(string serviceName, string tableName)
{
auto refs = m_view->GetCodeReferencesForType(QualifiedName(serviceName));
// both versions use the same type, so we only need to search for this one
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)
continue;
auto destExpr = instr.GetDestExpr();
if (destExpr.operation != MLIL_LOAD_STRUCT_SSA)
continue;
if (destExpr.GetOffset() != 8)
continue;
auto params = instr.GetParameterExprs();
if (params.size() < 2)
continue;
auto smstAddr = params[1];
if (smstAddr.operation != MLIL_CONST_PTR)
continue;
QualifiedNameAndType result;
string errors;
bool ok = m_view->ParseTypeString(tableName, result, errors);
if (!ok)
return false;
m_view->DefineDataVariable(smstAddr.GetValue().value, result.type);
m_view->DefineUserSymbol(new Symbol(DataSymbol, "gMmst", smstAddr.GetValue().value));
m_view->UpdateAnalysisAndWait();
}
return true;
}
bool DxeResolver::resolveSmmServices()
{
auto refs = m_view->GetCodeReferencesForType(QualifiedName("EFI_MM_SYSTEM_TABLE"));
auto refs_smm = m_view->GetCodeReferencesForType(QualifiedName("EFI_SMM_SYSTEM_TABLE2"));
// These tables have same type information, we can just iterate once
refs.insert(refs.end(), refs_smm.begin(), refs_smm.end());
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 * 0x14) {
// SmmHandleProtocol
resolveGuidInterface(ref.func, ref.addr, 1, 2);
} else if (offset == 0x18 + m_width * 0x17) {
// SmmLocateProtocol
resolveGuidInterface(ref.func, ref.addr, 0, 2);
}
}
}
return true;
}
bool DxeResolver::resolveSmiHandlers()
{
auto refs = m_view->GetCodeReferencesForType(QualifiedName("EFI_MM_SW_REGISTER"));
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 == 0) {
/* EFI_MM_SW_REGISTER, we want to rename the second parameter according to the third parameter
typedef enum EFI_STATUS (* EFI_MM_SW_REGISTER)(
struct EFI_MM_SW_DISPATCH_PROTOCOL* This,
EFI_MM_HANDLER_ENTRY_POINT DispatchFunction,
struct EFI_MM_SW_REGISTER_CONTEXT*
RegisterContext, EFI_HANDLE* DispatchHandle); */
auto parameters = instr.GetParameterExprs();
if (parameters.size() < 3)
continue;
auto dispatchFunction = parameters[1];
auto registerContext = parameters[2];
// Determine the function name according to the registerContext
/* struct EFI_MM_SW_REGISTER_CONTEXT
* {
* UINTN SwMmiInputValue;
* };
*/
string funcName = "SmiHandler";
if (dispatchFunction.operation != MLIL_CONST_PTR)
continue;
auto funcAddr = static_cast<uint64_t> (dispatchFunction.GetConstant());
auto targetFunc = m_view->GetAnalysisFunction(m_view->GetDefaultPlatform(), funcAddr);
auto funcType = targetFunc->GetType();
// typedef enum
string handleTypeStr = "EFI_STATUS SmiHandler(EFI_HANDLE DispatchHandle, VOID* Context, VOID* CommBuffer, UINTN* CommBufferSize);";
QualifiedNameAndType result;
string errors;
bool ok = m_view->ParseTypeString(handleTypeStr, result, errors);
if (!ok)
return false;
targetFunc->SetUserType(result.type);
m_view->DefineUserSymbol(new Symbol(FunctionSymbol, funcName, funcAddr));
m_view->UpdateAnalysisAndWait();
// After setting the type, we want to propagate the parameters' type
TypePropagation propagator(m_view);
propagator.propagateFuncParamTypes(targetFunc);
}
}
}
return true;
}
bool DxeResolver::resolveDxe()
{
if (!resolveBootServices())
return false;
if (!resolveRuntimeServices())
return false;
return true;
}
bool DxeResolver::resolveSmm()
{
if (!resolveSmmTables("EFI_SMM_GET_SMST_LOCATION2", "EFI_SMM_SYSTEM_TABLE2*"))
return false;
if (!resolveSmmTables("EFI_MM_GET_MMST_LOCATION", "EFI_MM_SYSTEM_TABLE*"))
return false;
if (!resolveSmmServices())
return false;
if (!resolveSmiHandlers())
return false;
return true;
}
DxeResolver::DxeResolver(Ref<BinaryView> view, Ref<BackgroundTask> task)
: Resolver(view, task)
{
initProtocolMapping();
setModuleEntry(DXE);
}
|