-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesstracore.cpp
351 lines (300 loc) · 9.46 KB
/
esstracore.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
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
/*
* SPDX-FileCopyrightText: Copyright 2024-2025 Sony Group Corporation
* SPDX-License-Identifier: GPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <limits.h>
#include <errno.h>
#include <unistd.h>
#include <libgen.h>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include "gcc-plugin.h"
#include "output.h"
#include "plugin-version.h"
#include "WjCryptLib_Md5.h"
#include "WjCryptLib_Sha1.h"
#include "WjCryptLib_Sha256.h"
using std::vector;
using std::string;
using std::map;
using std::stringstream;
using std::string_literals::operator""s;
int plugin_is_GPL_compatible;
// section name
static constexpr char section_name[] = "esstra_info";
// metadata
static vector<string> allpaths;
using FileInfo = map<string, string>;
static std::map<string, FileInfo> infomap;
// hash algorithms
static const vector<string> supported_algos = {
"md5",
"sha1",
"sha256",
};
static vector<string> specified_algos = { // embeds sha1 sum by default
"sha1",
};
// yaml
#define YAML_ITEM "- "s
#define YAML_INDENT " "s
#define YAML_SEPARATOR "---"s
// keys
#define KEY_INPUT_FILENAME "InputFileName"s
#define KEY_SOURCE_FILES "SourceFiles"s
#define KEY_FILE "File"s
#define KEY_MD5 "MD5"s
#define KEY_SHA1 "SHA1"s
#define KEY_SHA256 "SHA256"s
// flags
static bool flag_debug = false;
static bool flag_input_file_name = false;
/*
* debug print
*/
static void
debug_log(const char* format, ...) {
if (flag_debug) {
va_list args;
va_start(args, format);
printf("[DEBUG] ");
vprintf(format, args);
va_end(args);
}
}
/*
* convert byte data to hex string
*/
static const string
bytes_to_string(uint8_t* bytes, unsigned size) {
stringstream hexstream;
hexstream << std::hex << std::setfill('0');
while (size--) {
hexstream << std::setw(2) << static_cast<int>(*bytes++);
}
return hexstream.str();
}
/*
* check if specified algorithm is supported
*/
static bool
is_algo_supported(const string& algo) {
return std::find(supported_algos.begin(), supported_algos.end(), algo) != supported_algos.end();
}
/*
* calculate md5sum
*/
static const string
calc_md5(uint8_t *buffer, uint32_t size) {
MD5_HASH hash;
Md5Calculate(buffer, size, &hash);
return bytes_to_string(hash.bytes, MD5_HASH_SIZE);
}
/*
* calculate sha1sum
*/
static const string
calc_sha1(uint8_t *buffer, uint32_t size) {
SHA1_HASH hash;
Sha1Calculate(buffer, size, &hash);
return bytes_to_string(hash.bytes, SHA1_HASH_SIZE);
}
/*
* calculate sha256sum
*/
static const string
calc_sha256(uint8_t *buffer, uint32_t size) {
SHA256_HASH hash;
Sha256Calculate(buffer, size, &hash);
return bytes_to_string(hash.bytes, SHA256_HASH_SIZE);
}
/*
* PLUGIN_INCLUDE_FILE handler - collect paths of source files
*/
static void
collect_paths(void* gcc_data, void* /* user_data */) {
string path(reinterpret_cast<const char*>(gcc_data));
if (path[0] == '<') {
debug_log("skip '%s': pseudo file name\n", path.c_str());
return;
}
// get absolute path
char resolved[PATH_MAX];
if (!realpath(path.c_str(), resolved)) {
perror((path + ": realpath() failed").c_str());
return;
}
string resolved_path(resolved);
if (find(allpaths.begin(), allpaths.end(), resolved_path) != allpaths.end()) {
debug_log("skip '%s': already registered\n", resolved_path.c_str());
return;
}
allpaths.push_back(resolved_path);
// calc checksum
int fd = open(resolved, O_RDONLY);
if (fd == 0) {
perror("open() failed");
return;
}
struct stat filestat;
if (fstat(fd, &filestat) < 0) {
perror((path + ": fstat() failed").c_str());
return;
}
ssize_t st_size = (ssize_t)filestat.st_size;
uint8_t* buffer = new uint8_t[st_size];
ssize_t size = read(fd, buffer, st_size);
if (size != st_size) {
fprintf(stderr, "size mismatch: st_size:%lu, read():%zd\n", st_size, size);
return;
}
// calculate hashes with specified algorithms
FileInfo finfo;
for (const auto &algo: specified_algos) {
debug_log("calculate '%s' hash\n", algo.c_str());
if (algo == "md5") {
finfo[KEY_MD5] = calc_md5(buffer, size);
} else if (algo == "sha1") {
finfo[KEY_SHA1] = calc_sha1(buffer, size);
} else if (algo == "sha256") {
finfo[KEY_SHA256] = calc_sha256(buffer, size);
} else {
fprintf(stderr, "unsupported hash algorithm '%s'\n", algo.c_str());
}
}
infomap[resolved] = finfo;
delete[] buffer;
}
/*
* PLUGIN_FINISH_UNIT handler - create a new ELF section and store path data in it
*/
static void
create_section(void* /* gcc_data */, void* /* user_data */) {
vector<string> strings_to_embed;
// sort allpaths
std::sort(allpaths.begin(), allpaths.end());
// construct metadata in yaml format
strings_to_embed.push_back(YAML_SEPARATOR);
if (flag_input_file_name) {
strings_to_embed.push_back(KEY_INPUT_FILENAME + ": " + main_input_filename);
}
string current_directory = "";
strings_to_embed.push_back(KEY_SOURCE_FILES + ":");
for (const auto& path : allpaths) {
string directory = dirname(const_cast<char*>(string(path).c_str()));
string filename = basename(const_cast<char*>(string(path).c_str()));
debug_log("dir: %s\n", directory.c_str());
if (current_directory != directory) {
current_directory = directory;
strings_to_embed.push_back(YAML_INDENT + directory + ":");
}
strings_to_embed.push_back(YAML_INDENT + YAML_ITEM + KEY_FILE + ": " + filename);
for (const auto& elem : infomap[path]) {
strings_to_embed.push_back(
YAML_INDENT + YAML_INDENT + elem.first + ": " + elem.second);
}
}
// calculate size of metadata
int datasize = 0;
for (const auto& item : strings_to_embed) {
datasize += item.size() + 1; // plus 1 for null-character temination
}
int padding = 0;
if (datasize % 4) {
padding = 4 - datasize % 4; // padding for 4-byte alignment
}
debug_log("size=%d\n", datasize);
debug_log("padding=%d\n", padding);
// add assembly code
fprintf(asm_out_file, "\t.pushsection %s\n", section_name);
fprintf(asm_out_file, "\t.balign 4\n");
for (const auto& item : strings_to_embed) {
fprintf(asm_out_file, "\t.asciz \"%s\"\n", item.c_str());
}
if (padding > 0) {
fprintf(asm_out_file, "\t.dcb %d\n", padding);
}
fprintf(asm_out_file, "\t.popsection\n");
}
/*
* initialization
*/
int
plugin_init(struct plugin_name_args* plugin_info,
struct plugin_gcc_version* version) {
if (!plugin_default_version_check(version, &gcc_version)) {
return 1;
}
bool error = false;
const struct plugin_argument *argv = plugin_info->argv;
int argc = plugin_info->argc;
while (argc--) {
if (strcmp(argv->key, "debug") == 0) {
flag_debug = (atoi(argv->value) != 0);
if (flag_debug) {
debug_log("debug mode enabled\n");
}
} else if (strcmp(argv->key, "input-file-name") == 0) {
flag_input_file_name = (atoi(argv->value) != 0);
if (flag_debug) {
debug_log("input_file_name enabled\n");
}
} else if (strcmp(argv->key, "checksum") == 0) {
debug_log("arg-checksum: %s\n", argv->value);
auto value = reinterpret_cast<const char*>(argv->value);
string algo;
specified_algos.clear(); // delete default algo
while (*value) {
if (*value == ',') {
if (algo.length() > 0) {
specified_algos.push_back(algo);
}
algo = "";
} else {
algo += string(value, 1);
}
value++;
}
if (algo.length() > 0) {
specified_algos.push_back(algo);
}
// check if specified algos are supported
for (const auto& algo: specified_algos) {
if (!is_algo_supported(algo)) {
fprintf(stderr, "algorithm '%s' not supported\n", algo.c_str());
error = true;
}
debug_log("algo: '%s'\n", algo.c_str());
}
}
argv++;
}
debug_log("main_input_filename: %s\n", main_input_filename);
if (error) {
fprintf(stderr, "error occurred.");
return 1;
}
register_callback(plugin_info->base_name,
PLUGIN_INCLUDE_FILE, collect_paths, 0);
register_callback(plugin_info->base_name,
PLUGIN_FINISH_UNIT, create_section, 0);
return 0;
}