-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.cpp
189 lines (171 loc) · 6.9 KB
/
process.cpp
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
#include "process.h"
#include <filesystem>
#include <queue>
int Process::load_object_and_dependencies(std::filesystem::path path)
{
std::set<std::filesystem::path> deja_vu;
std::queue<std::filesystem::path> queue;
queue.push(path);
while (queue.size() > 0)
{
std::filesystem::path current = queue.front();
queue.pop();
if (deja_vu.count(current) > 0)
{
continue;
}
deja_vu.insert(current);
int ret = load_object(current);
if (ret < 0)
{
return -1;
}
const auto & deps = objects_.back().elf_file.get_dependencies();
int deps_size = deps.size();
for (int i = 0; i < deps_size; ++i)
{
queue.push(deps[i]);
}
}
return 0;
}
int Process::load_object(std::filesystem::path path)
{
// Looking for the file in the different search paths
int ret = std::filesystem::exists(path) ? 0 : -1;
std::filesystem::path full_path = std::filesystem::absolute(path);
if (ret < 0)
{
for (int i = 0; i < search_paths_.size(); ++i)
{
auto && tmp = search_paths_[i] / path;
if (std::filesystem::exists(tmp))
{
ret = 0;
full_path.swap(tmp);
break;
}
}
}
if (ret < 0)
{
printf("Error cannot find file '%s'\n", path.c_str());
return ret;
}
ElfObject obj;
// Load the elf file into memory
ret = obj.load_and_parse_elf_file(full_path);
if (ret < 0)
{
printf("Error cannot loading object\n");
return -1;
}
// Mapping load sections into memory
ret = obj.load();
if (ret < 0)
{
printf("Error mapping load sections of file '%s'\n", full_path.c_str());
return -1;
}
// Add search_paths for future lookups
std::filesystem::path parent_path = full_path.parent_path();
auto & run_paths = obj.elf_file.run_paths();
for (const auto & rp : run_paths)
{
if (rp == "$ORIGIN")
{
search_paths_.emplace_back(parent_path);
}
else
{
search_paths_.emplace_back(rp);
}
}
obj.path = full_path;
objects_.emplace_back(std::move(obj));
return 0;
}
int Process::adjust_permissions()
{
for (int i = 0; i < objects_.size(); ++i)
{
objects_[i].set_final_map_protections();
}
return 0;
}
int Process::apply_relocations()
{
for (int i = 0; i < objects_.size(); ++i)
{
const std::vector<const elf64_rela*> & relocations = objects_[i].elf_file.relocations();
for (const auto & rela : relocations)
{
Elf64_Xword sym_index = ELF64_R_SYM(rela->r_info);
const elf64_sym* symbol = objects_[i].elf_file.dyn_symbols()[sym_index];
uintptr_t sym_value = objects_[i].elf_file.symbol_value_from_index(sym_index);
switch (ELF64_R_TYPE(rela->r_info))
{
case (None):
break;
case (_64):
{
uintptr_t jump_addr = (uintptr_t)objects_[i].base() + sym_value;
memcpy((void*)(objects_[i].base() + rela->r_offset), &jump_addr, 8);
break;
}
case (COPY):
for (int j = 0; j < objects_.size(); ++j)
{
if (j == i) continue;
for (const auto & sym : objects_[j].elf_file.dyn_symbols())
{
if (strcmp(objects_[j].elf_file.dt_name_from_index(sym->st_name)
, objects_[i].elf_file.dt_name_from_index(symbol->st_name)) == 0)
{
uintptr_t sym_location = sym->st_value;
memcpy((void*)(objects_[i].base() + rela->r_offset), (void*)(objects_[j].base() + sym_location), sym->st_size);
break;
}
}
}
break;
case (RELATIVE):
{
uintptr_t to_relocate = (uintptr_t)objects_[i].base() + rela->r_addend;
memcpy((void*)(objects_[i].base() + rela->r_offset), (void*)(&to_relocate), 8);
break;
}
case (PC32):
case (GOT32):
case (PLT32):
case (GLOB_DAT):
case (JUMP_SLOT):
printf("Relocation not implemented\n");
return -1;
}
}
}
return 0;
}
std::ostream& operator<<(std::ostream& os, const Process& process)
{
printf("Process {\n");
printf("\tsearchs_path: [\n");
for (const auto& sp : process.search_paths_)
{
printf("\t\t- %s\n", sp.c_str());
}
printf("\t]\n");
printf("\tObjects: [\n");
for (const auto & obj : process.objects_)
{
printf("\t\t- Object {\n");
printf("\t\t\tpath: %s\n", obj.path.c_str());
printf("\t\t\tbase: %lx\n", (uintptr_t)obj.base());
printf("\t\t\tmem_range: 0x%lx...0x%lx\n", (uintptr_t)obj.convex_hull.first, (uintptr_t)obj.convex_hull.second);
printf("\t\t}\n");
}
printf("\t]\n");
printf("}\n");
return os;
}