Repository navigation
Expand file tree
/
Copy pathOperGlibc.cpp
More file actions
470 lines (418 loc) · 17.1 KB
/
Copy pathOperGlibc.cpp
File metadata and controls
470 lines (418 loc) · 17.1 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#include "OperGlibc.h"
#include "ElfUtils.h"
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <set>
#include <utility>
using namespace std;
namespace {
// 没有与目标匹配的 C 库时的统一警告(终端 + 错误日志)。
void warnNoMatchingLibc(const std::string& targetPath, const ElfIdentity& targetId,
const std::vector<std::string>& candidates)
{
std::string message = "警告:没有与目标匹配的 C 库:目标为 " +
ElfUtils::describeIdentity(targetId) + ",提供的 C 库为 ";
for (size_t i = 0; i < candidates.size(); ++i)
{
if (i) message += "、";
ElfIdentity candidateId;
if (ElfUtils::identifyElf(candidates[i], candidateId))
message += ElfUtils::describeIdentity(candidateId);
else
message += "无法识别(" + candidates[i] + ")";
}
message += ",跳过:" + targetPath;
std::cerr << message << std::endl;
ErrorLog::getErrorLog()->putErrInfo(message, targetPath);
}
} // namespace
template <typename Traits>
bool OperGlibc::parseLibc(const std::string& glibcPath, ParsedLibc& out)
{
ElfPtrs<Traits> glibcPtrs(ElfOpenMode::ReadOnly);
if(!glibcPtrs.initPtrs(glibcPath.c_str()))
{
ErrorLog::getErrorLog()->putErrInfo("initGlibcPtrs失败", glibcPath);
return false;
}
// 获取必需的表信息
TableInfo<Traits> *tb_version_d = glibcPtrs.getTableInfo(".gnu.version_d");
TableInfo<Traits> *tb_dynstr = glibcPtrs.getTableInfo(".dynstr");
TableInfo<Traits> *tb_dynsym = glibcPtrs.getTableInfo(".dynsym");
TableInfo<Traits> *tb_version = glibcPtrs.getTableInfo(".gnu.version");
if(!tb_version_d || !tb_dynstr || !tb_dynsym || !tb_version)
{
ErrorLog::getErrorLog()->putErrInfo("获取Glibc必要节区失败", glibcPath);
return false;
}
// 计算实际偏移
char* ehdr = reinterpret_cast<char*>(glibcPtrs.elf_hdr);
typename Traits::Verdef *verdef = reinterpret_cast<typename Traits::Verdef*>(ehdr + tb_version_d->addr->sh_offset);
char *dynstr = ehdr + tb_dynstr->addr->sh_offset;
typename Traits::Sym *dynsym = reinterpret_cast<typename Traits::Sym*>(ehdr + tb_dynsym->addr->sh_offset);
unsigned short *versions = reinterpret_cast<unsigned short*>(ehdr + tb_version->addr->sh_offset);
typename Traits::Verdef *next_verdef;
set<string> seenVersions;
for (int last = 0; !last; verdef = next_verdef)
{
if(!(verdef->vd_flags & VER_FLG_BASE))
{
typename Traits::Verdaux *daux = reinterpret_cast<typename Traits::Verdaux*>((char *)verdef + verdef->vd_aux);
// 首个辅助项是版本名,其余辅助项为父版本,不应创建新的符号归属。
{
char *name = dynstr + daux->vda_name;
if(seenVersions.find(name) == seenVersions.end())
{
seenVersions.insert(name);
GlibcVersionInfo versionInfo;
versionInfo.name = name;
versionInfo.id = verdef->vd_ndx;
for (size_t i = 1; i < tb_version->addr->sh_size / sizeof(unsigned short); i++)
{
if((versions[i] & 0x7fff) == verdef->vd_ndx && dynsym[i].st_shndx != SHN_UNDEF)
{
string s_dynsym = string(dynstr + dynsym[i].st_name);
if(s_dynsym != name)
versionInfo.symbols.push_back(move(s_dynsym));
}
}
out.versions.push_back(move(versionInfo));
}
}
}
next_verdef = reinterpret_cast<typename Traits::Verdef*>((char *)verdef + verdef->vd_next);
last = verdef->vd_next == 0;
}
for (const auto& version : out.versions)
{
out.versionIds[version.name] = version.id;
for (const auto& symbol : version.symbols)
out.symbolVersions[symbol].push_back({version.name, version.id});
}
showGlibcInfo(out);
return true;
}
void OperGlibc::showGlibcInfo(const ParsedLibc& libc, bool showDynsym)
{
const char* reset = "\033[0m";
const char* bold = "\033[1m";
const char* redBold = "\033[31;1m";
if(showDynsym)
cout << bold << "目标GLIBC所支持的版本及对应符号表如下:" << reset << endl;
else
cout << bold << "目标GLIBC所支持的版本如下:" << reset << endl;
for (const auto& ver : libc.versions)
{
cout << redBold << ver.name << " " << ver.id << reset << endl;
if(showDynsym)
{
int i = 0;
for(const auto& dynsym : ver.symbols)
{
cout << dynsym << " ";
if(i % 10 == 0 && i != 0)
cout << endl;
++i;
}
cout << endl;
}
}
}
template <typename Traits>
bool OperGlibc::checkTarget(ElfPtrs<Traits>& targetPtrs, const ParsedLibc& libc,
typename Traits::Verneed*& libcVerneed,
std::vector<TargetVersionInfo>& targetVersions,
std::vector<SymbolPatchInfo>& patches)
{
bool ret = true;
libcVerneed = nullptr;
TableInfo<Traits>* tb_version_r = targetPtrs.getTableInfo(".gnu.version_r");
TableInfo<Traits>* tb_dynstr = targetPtrs.getTableInfo(".dynstr");
TableInfo<Traits>* tb_version = targetPtrs.getTableInfo(".gnu.version");
TableInfo<Traits>* tb_dynsym = targetPtrs.getTableInfo(".dynsym");
if(!tb_version_r || !tb_dynstr || !tb_version || !tb_dynsym)
{
ErrorLog::getErrorLog()->putErrInfo("目标ELF缺少必要节区", targetPtrs.getFilePath());
return false;
}
char* ehdr = reinterpret_cast<char*>(targetPtrs.elf_hdr);
typename Traits::Verneed *verneed = reinterpret_cast<typename Traits::Verneed*>(ehdr + tb_version_r->addr->sh_offset);
char *dynstr = ehdr + tb_dynstr->addr->sh_offset;
unsigned short *versions = reinterpret_cast<unsigned short*>(ehdr + tb_version->addr->sh_offset);
typename Traits::Sym *dynsym = reinterpret_cast<typename Traits::Sym*>(ehdr + tb_dynsym->addr->sh_offset);
while(true)
{
if(strcmp(dynstr + verneed->vn_file, "libc.so.6"))
{
if(verneed->vn_next == 0) break;
verneed = reinterpret_cast<typename Traits::Verneed*>((char *)verneed + verneed->vn_next);
continue;
}
libcVerneed = verneed;
if (verneed->vn_cnt > 0 && verneed->vn_aux != 0)
{
typename Traits::Vernaux *naux = reinterpret_cast<typename Traits::Vernaux*>((char *)verneed + verneed->vn_aux);
while(true)
{
char *name = dynstr + naux->vna_name;
targetVersions.push_back({string(name), naux->vna_other & 0x7fff});
if(naux->vna_next == 0) break;
naux = reinterpret_cast<typename Traits::Vernaux*>((char *)naux + naux->vna_next);
}
}
break;
}
if(!libcVerneed)
{
ErrorLog::getErrorLog()->putErrInfo("未能找到libc.so.6所在节表", targetPtrs.getFilePath());
return false;
}
for (size_t i = 1; i < tb_version->addr->sh_size / sizeof(unsigned short); i++)
{
unsigned short v = versions[i] & 0x7fff;
unsigned hveridx = 0;
for (; hveridx < targetVersions.size(); ++hveridx)
if (v == targetVersions[hveridx].id) break;
if (hveridx == targetVersions.size()) continue;
const char* dynsymName = dynstr + dynsym[i].st_name;
auto verAndId = findSymbol(libc, string(dynsymName), targetVersions[hveridx].name, targetVersions);
if(verAndId.second == -1)
{
ErrorLog::getErrorLog()->putErrInfo(
string("目标C库中未找到符号:") + dynsymName + " GLIBC版本:" + targetVersions[hveridx].name,
targetPtrs.getFilePath());
ret = false;
}
else
{
int hostIdInTarget = -1;
for(const auto& vInfo : targetVersions)
{
if(vInfo.name == verAndId.first)
{
hostIdInTarget = vInfo.id;
break;
}
}
if(hostIdInTarget != -1)
{
patches.push_back({i, (int)hveridx, verAndId.first, hostIdInTarget});
}
else
{
ErrorLog::getErrorLog()->putErrInfo("未能找到目标elf中对应版本的id", targetPtrs.getFilePath());
ret = false;
}
}
}
return ret;
}
template <typename Traits>
bool OperGlibc::adaptTargetFile(const std::string& path, const ParsedLibc& libc)
{
if (libc.versions.empty()) return false;
unique_ptr<ElfPtrs<Traits>> targetPtrs(new ElfPtrs<Traits>(ElfOpenMode::ReadWrite));
if(!targetPtrs->initPtrs(path.c_str()))
{
ErrorLog::getErrorLog()->putErrInfo("initTargetPtrs失败", path);
return false;
}
typename Traits::Verneed* libcVerneed = nullptr;
std::vector<TargetVersionInfo> targetVersions;
std::vector<SymbolPatchInfo> patches;
if(!checkTarget(*targetPtrs, libc, libcVerneed, targetVersions, patches)) return false;
unsigned removed_count = 0;
unsigned original_cnt = 0;
if (libcVerneed)
{
original_cnt = libcVerneed->vn_cnt;
}
TableInfo<Traits>* tb_dynstr = targetPtrs->getTableInfo(".dynstr");
char* target_dynstr = tb_dynstr ? (reinterpret_cast<char*>(targetPtrs->elf_hdr) + tb_dynstr->addr->sh_offset) : nullptr;
if(!target_dynstr) return false;
if (libcVerneed && original_cnt > 0 && libcVerneed->vn_aux != 0)
{
typename Traits::Vernaux *naux = reinterpret_cast<typename Traits::Vernaux *>((char *)libcVerneed + libcVerneed->vn_aux);
typename Traits::Vernaux *prev_naux = nullptr;
for (unsigned cnt = 0; cnt < original_cnt; ++cnt)
{
char *name = target_dynstr + naux->vna_name;
typename Traits::Vernaux *next_naux = nullptr;
if (naux->vna_next != 0)
next_naux = reinterpret_cast<typename Traits::Vernaux*>((char *)naux + naux->vna_next);
if(findVersion(libc, name).second == -1)
{
removed_count++;
if (prev_naux)
{
// vna_next 相对于所在节点,跳过当前节点时需从保留节点重新计算。
prev_naux->vna_next = next_naux
? static_cast<typename Traits::Word>(reinterpret_cast<char*>(next_naux) - reinterpret_cast<char*>(prev_naux))
: 0;
}
else
{
if (next_naux)
{
libcVerneed->vn_aux = (char*)next_naux - (char*)libcVerneed;
}
else
{
libcVerneed->vn_aux = 0;
}
}
}
else
{
prev_naux = naux;
}
naux = next_naux;
if (naux == nullptr) break;
}
}
else if (libcVerneed)
{
libcVerneed->vn_cnt = 0;
}
if (libcVerneed && removed_count > 0)
{
libcVerneed->vn_cnt -= removed_count;
}
TableInfo<Traits>* tb_version = targetPtrs->getTableInfo(".gnu.version");
TableInfo<Traits>* tb_dynsym = targetPtrs->getTableInfo(".dynsym");
char* target_ehdr = reinterpret_cast<char*>(targetPtrs->elf_hdr);
unsigned short *target_versions = reinterpret_cast<unsigned short*>(target_ehdr + tb_version->addr->sh_offset);
typename Traits::Sym *target_dynsym = reinterpret_cast<typename Traits::Sym*>(target_ehdr + tb_dynsym->addr->sh_offset);
for(const auto& patch : patches)
{
if(targetVersions[patch.originalTargetVersionIndex].id != patch.hostVersionId)
{
const char* dynsymName = target_dynstr + target_dynsym[patch.symbolIndex].st_name;
printf(" 修改 %-20s: %-12s(%02u) ----> %-12s(%02u) 文件:%-20s\n",
dynsymName,
targetVersions[patch.originalTargetVersionIndex].name.c_str(),
(unsigned short)targetVersions[patch.originalTargetVersionIndex].id,
patch.hostVersionName.c_str(),
(unsigned short)patch.hostVersionId,
path.c_str());
// 保留符号原有的隐藏标记,只替换低15位的版本编号。
target_versions[patch.symbolIndex] = (target_versions[patch.symbolIndex] & 0x8000) | patch.hostVersionId;
}
}
return targetPtrs->flush();
}
template <typename Traits>
FileAdaptResult OperGlibc::adaptOneFileFor(const std::string& path, const std::vector<std::string>& libcPaths)
{
typename Traits::Half machine = 0;
{
ElfPtrs<Traits> probe(ElfOpenMode::ReadOnly);
if (!probe.initPtrs(path))
{
ErrorLog::getErrorLog()->putErrInfo("目标不是有效的 ELF 文件", path);
return FileAdaptResult::Failed;
}
machine = probe.elf_hdr->e_machine;
}
// 按命令行顺序挑候选:身份(类别/字节序/架构)匹配且能解析成功的
// 第一个 libc;解析失败时继续试下一个匹配的候选。
const ParsedLibc* chosen = nullptr;
for (const auto& candidate : libcPaths)
{
ElfIdentity id;
if (!ElfUtils::identifyElf(candidate, id)) continue;
if (id.elfClass != Traits::elfClass || id.elfData != ELFDATA2LSB || id.machine != machine) continue;
auto it = m_libcCache.find(candidate);
if (it == m_libcCache.end())
{
ParsedLibc parsed;
if (!parseLibc<Traits>(candidate, parsed)) continue;
it = m_libcCache.emplace(candidate, std::move(parsed)).first;
}
chosen = &it->second;
break;
}
if (!chosen)
{
ElfIdentity targetId;
ElfUtils::identifyElf(path, targetId);
warnNoMatchingLibc(path, targetId, libcPaths);
return FileAdaptResult::Skipped;
}
return adaptTargetFile<Traits>(path, *chosen) ? FileAdaptResult::Patched : FileAdaptResult::Failed;
}
FileAdaptResult OperGlibc::adaptOneFile(const std::string& path, const std::vector<std::string>& libcPaths)
{
ElfIdentity id;
if (!ElfUtils::identifyElf(path, id))
{
ElfUtils::logIdentifyFailure(path);
return FileAdaptResult::Failed;
}
if (id.elfData == ELFDATA2MSB)
{
ElfUtils::warnUnsupportedEndian(path, id);
return FileAdaptResult::Skipped;
}
if (id.elfClass == ELFCLASS64) return adaptOneFileFor<Elf64Traits>(path, libcPaths);
return adaptOneFileFor<Elf32Traits>(path, libcPaths);
}
bool OperGlibc::adaptedTargets(const string& path, const vector<string>& libcPaths)
{
// 先校验候选 libc 是否可读:缺失/损坏的候选在处理目标前就应报错
// (与旧版先解析 libc 再处理目标的行为一致);能识别但解析失败
// 的候选在挑中时再记错,继续试下一个匹配的候选。
for (const auto& candidate : libcPaths)
{
ElfIdentity id;
if (!ElfUtils::identifyElf(candidate, id)) ElfUtils::logIdentifyFailure(candidate);
}
if (ElfUtils::isDirectory(path))
{
vector<string> files;
if (!ElfUtils::collectElfFiles(path, files)) return false;
unsigned patched = 0;
unsigned skipped = 0;
bool success = true;
for (const auto& str : files)
{
const FileAdaptResult result = adaptOneFile(str, libcPaths);
if (result == FileAdaptResult::Patched) ++patched;
else if (result == FileAdaptResult::Skipped) ++skipped;
else success = false;
}
if (patched == 0)
{
ErrorLog::getErrorLog()->putErrInfo("目录中没有可适配的 ELF 文件", path);
return false;
}
(void)skipped;
return success;
}
return adaptOneFile(path, libcPaths) == FileAdaptResult::Patched;
}
pair<string, int> OperGlibc::findVersion(const ParsedLibc& libc, const string& version)
{
auto it = libc.versionIds.find(version);
if (it != libc.versionIds.end()) return {it->first, it->second};
return {"", -1};
}
pair<string, int> OperGlibc::findSymbol(const ParsedLibc& libc, const string& dynsym, const string& preferredVersion,
const vector<TargetVersionInfo>& targetVersions)
{
auto found = libc.symbolVersions.find(dynsym);
if (found == libc.symbolVersions.end()) return {"", -1};
pair<string, int> available = {"", -1};
for (const auto& version : found->second)
{
if (version.first == preferredVersion) return version;
if (available.second == -1)
for (const auto& target : targetVersions)
if (target.name == version.first) { available = version; break; }
}
return available.second != -1 ? available : found->second.front();
}