summaryrefslogtreecommitdiff
path: root/src/crash-manager/so-info.c
blob: 5dc26ff38941fea6f3fdcc3311dc2c0e8184ae26 (plain)
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*
 * build-ids
 *
 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <libgen.h>
#include <limits.h>
#include <string.h>
#include <elf.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <unistd.h>
#include <glib.h>
#include <rpm/rpmdb.h>
#include <rpm/rpmts.h>
#include <rpm/rpmlib.h>
#include <rpm/header.h>
#include "shared/log.h"
#include "shared/util.h"
#include <tzplatform_config.h>

#define BID_SNAME ".note.gnu.build-id"

struct rpm_info {
	char *file_path;
	char *app_name;
	char *build_id;
	char *rpm_info;
};

void rpm_info_free(struct rpm_info* ri)
{
	free(ri->file_path);
	free(ri->app_name);
	free(ri->build_id);
	free(ri->rpm_info);
	free(ri);
}

static unsigned char *map_file(const char *filename, int64_t *size)
{
	struct stat file_stat;
	int fd = open(filename, O_RDONLY);

	if (fd < 0) {
		_E("Failed to open file %s: %m", filename);
		return NULL;
	}

	if (fstat(fd, &file_stat) < 0) {
		_E("Failed to get file status %s: %m", filename);
		close(fd);
		return NULL;
	}

	*size = file_stat.st_size;
	void *mem = mmap(NULL, file_stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
	close(fd);

	if (mem == MAP_FAILED) {
		_E("Failed to map file %s into memory: %m", filename);
		return NULL;
	}

	return (unsigned char*)mem;
}

#define DECLARE_BUILD_ID_SECTION_ADDRESS(bits) \
static Elf##bits##_Off build_id_section_address##bits(const unsigned char *mapped_file, const int64_t size)\
{\
	if (mapped_file <= 0)\
		return 0;\
\
	Elf##bits##_Ehdr *elheader = (Elf##bits##_Ehdr*)mapped_file;\
	Elf##bits##_Shdr *sec = (Elf##bits##_Shdr*)(mapped_file + elheader->e_shoff);\
\
	if (size < (intptr_t)sec + elheader->e_shstrndx*sizeof(Elf##bits##_Shdr) - (intptr_t)mapped_file)\
		return 0;\
\
	char *names = (char *)(mapped_file + sec[elheader->e_shstrndx].sh_offset);\
\
	for (int i = 0; i < elheader->e_shnum; i++) {\
		char *section_name = (char *)(&names[sec[i].sh_name]);\
		if (sec[i].sh_type == SHT_NOTE &&\
			strncmp(section_name, BID_SNAME, strlen(BID_SNAME)) == 0)\
			return sec[i].sh_offset;\
	} \
\
	return 0;\
} \

DECLARE_BUILD_ID_SECTION_ADDRESS(32)

DECLARE_BUILD_ID_SECTION_ADDRESS(64)

static Elf64_Off build_id_section_address(unsigned char *mapped_file, int64_t size)
{
	Elf64_Off offset;

	if (mapped_file[EI_CLASS] == ELFCLASS32)
		offset = build_id_section_address32(mapped_file, size);
	else if (mapped_file[EI_CLASS] == ELFCLASS64)
		offset = build_id_section_address64(mapped_file, size);
	else {
		_E("Unrecognized ELF class");
		return 0;
	}

	return offset;
}

static char HEX_CHARS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

static int get_build_id_from(const unsigned char *mapped_file, const Elf64_Off section_offset, char **build_id)
{
	const unsigned char *p = mapped_file + section_offset + 0x10;
	const unsigned int *len = (unsigned int *)(mapped_file + section_offset + 0x04);
	const unsigned int dlen = (*len)*2;

	*build_id = (char *)malloc(dlen+1);
	if (*build_id == NULL) {
		_E("Failed to allocate memory: %m");
		return -1;
	}

	for (int i = 0; i < dlen; p++) {
		(*build_id)[i++] = HEX_CHARS[(*p / 16)];
		(*build_id)[i++] = HEX_CHARS[(*p % 16)];
	}

	(*build_id)[dlen] = '\0';
	return dlen;
}

static int get_build_id(char *filename, char **build_id)
{
	int64_t size = 0;
	int ret = 0;
	unsigned char *mapped_file = map_file(filename, &size);
	*build_id = NULL;

	if (mapped_file > 0) {
		Elf64_Off offset = build_id_section_address(mapped_file, size);
		if (offset > 0) {
			ret = get_build_id_from(mapped_file, offset, build_id);
		} else {
			_W("File %s doesn't contain build-id", filename);
			ret = -1;
		}

		munmap(mapped_file, size);
	} else {
		ret = -1;
	}
	return ret;
}

enum Perms {
	PERM_READ = 1,
	PERM_WRITE = 2,
	PERM_EXEC = 4
};

/* Parses beginning of the maps file in the form: `addr1-addr2 r-xp size ...` */
static int get_perms(char *line, enum Perms *perms)
{
	char *first_space = strchr(line, ' ');

	if (first_space != 0 && strlen(first_space) > 4 /* eg. " r-xp" from example above */) {
		char *p = first_space + 1;
		*perms = (p[0] == 'r' ? PERM_READ : 0) | (p[1] == 'w' ? PERM_WRITE : 0) | (p[2] == 'x' ? PERM_EXEC : 0);
		return 0;
	} else
		return -1;
}

/*
 * Return the pointer to start of the filename if the mapped region has execute
 * permission ('x' in perms). Otherwise return NULL.
 */
static char *get_exe_filename(char *line)
{
	enum Perms perms;

	if (get_perms(line, &perms) == 0) {
		char *p = strstr(line, " /");
		if (p == NULL)
			return NULL;

		char *path = p + 1;

		if ((perms & PERM_EXEC) ||
		    is_dotnet_file(path))
			return path;
	}
	return NULL;
}

GSList *get_filepaths(char *map_path)
{
	char *line = NULL;
	size_t len = 0;
	GSList *file_list = NULL;

	FILE *f = fopen(map_path, "r");

	if (f == NULL) {
		_E("Failed to open maps file %s: %m", map_path);
		return NULL;
	}

	while (getline(&line, &len, f) != -1) {
		char *exe_filename = get_exe_filename(line);
		if (exe_filename == NULL)
			continue;

		size_t n = strcspn(exe_filename, "\n");

		if (n > 0 && n < PATH_MAX) {
			char *file_name = (char *)malloc(n+1);
			if (file_name == NULL)
				continue;

			snprintf(file_name, n+1, "%s", exe_filename);

			file_list = g_slist_append(file_list, file_name);
		}
	}

	free(line);
	fclose(f);

	return file_list;
}

rpmts init_rpm()
{
	rpmts ts = rpmtsCreate();
	if (rpmReadConfigFiles(NULL, NULL) < 0) {
		_E("rpmReadConfigFiles failed\n");
		rpmtsFree(ts);
		return NULL;
	}
	return ts;
}

void free_rpm(rpmts ts)
{
	if (ts != NULL) {
		rpmtsFree(ts);
		rpmFreeRpmrc();
	}
}

char *get_rpm_info_as_string_for_pkgname(Header h, const char *pkg_name)
{
	char *result = NULL;

	const char *version = headerGetString(h, RPMTAG_VERSION);
	const char *release = headerGetString(h, RPMTAG_RELEASE);
	const char *arch    = headerGetString(h, RPMTAG_ARCH);

	if (version == NULL || release == NULL || arch == NULL)
		_E("Failed to get version, release and arch for: %s", pkg_name);
	else if (asprintf(&result, "%s;%s;%s;%s", pkg_name, version, release, arch) == -1)
		_E("asprintf() error: %m");

	return result;
}

char *get_rpm_info_as_string(Header h)
{
	const char *name = headerGetString(h, RPMTAG_NAME);
	return get_rpm_info_as_string_for_pkgname(h, name);
}

char *get_rpm_info(rpmts ts, const char* filename)
{
	char *rpm_info = NULL;

	if (ts == NULL)
		return NULL;

	rpmdbMatchIterator mi = rpmtsInitIterator(ts, RPMDBI_INSTFILENAMES, filename, 0);
	if (mi == NULL) {
		_W("Not found RPM package for %s\n", filename);
		return NULL;
	}

	Header h = rpmdbNextIterator(mi);

	if (h != NULL)
		rpm_info = get_rpm_info_as_string(h);
	else {
		_E("rpmdbNextIterator error\n");
		rpm_info = NULL;
	}
	rpmdbFreeIterator(mi);
	return rpm_info;
}

void search_for_tpk(rpmts ts, GSList *pkgs)
{
	rpmdbMatchIterator mi = rpmtsInitIterator(ts, RPMTAG_NAME, NULL, 0);
	if (mi == NULL) {
		_E("Failed to init rpmdb match iterator");
		return;
	}

	guint count = g_slist_length(pkgs); // Initial number of packets with empty rpm_info

	for (Header h = rpmdbNextIterator(mi); count > 0 && h; h = rpmdbNextIterator(mi)) {
		const char *pkg_name = headerGetString(h, RPMTAG_NAME);
		if (pkg_name == NULL)
			continue;
		rpmfi fi = rpmfiNew(NULL, h, RPMTAG_FILENAMES, 0);

		while (count > 0 && rpmfiNext(fi) != -1) {
			const char *file_name = rpmfiFN(fi);
			size_t file_name_len = strlen(file_name);

			if (file_name_len < 4 ||
			    strcmp(&file_name[file_name_len-4], ".tpk") != 0)
				continue;

			for (GSList *iterator = pkgs; iterator; iterator = iterator->next) {
				struct rpm_info *ri = (struct rpm_info *)iterator->data;
				if (ri->rpm_info != NULL)
					continue;

				if (strstr(file_name, ri->app_name) == NULL)
					continue;

				char *info = get_rpm_info_as_string_for_pkgname(h, pkg_name);
				if (info == NULL)
					continue;

				ri->rpm_info = info;
				count--;
				break;
			}
		}

		rpmfiFree(fi);
	}

	rpmdbFreeIterator(mi);
}

char *get_app_name_from_path(const char *file_path)
{
	const char * const prefix[] = {
			tzplatform_mkpath(TZ_SYS_RO_APP, "/"),
			tzplatform_mkpath(TZ_SYS_RW_APP, "/")
	};

	for (size_t i = 0; i < ARRAY_SIZE(prefix); i++) {
		if (strncmp(file_path, prefix[i], strlen(prefix[i])) != 0)
			continue;

		const char *app_name = &file_path[strlen(prefix[i])];

		char *end_of_name = strchr(app_name, '/');
		if (end_of_name != NULL)
			return strndup(app_name, end_of_name - app_name);
	}
	return NULL;
}

GSList *get_app_name_from_map(char *map_path)
{
	GSList *app_name_list = NULL;
	GSList *file_list = get_filepaths(map_path);
	for (GSList *iterator = file_list; iterator; iterator = iterator->next) {
		char *app_name = get_app_name_from_path((char *)iterator->data);
		if (app_name)
			app_name_list = g_slist_append(app_name_list, app_name);
		free(iterator->data);
	}
	g_slist_free(file_list);
	return app_name_list;
}

bool replace_suffix(char *string, char *suffix, char *replace, char* buff, size_t len)
{
	assert(string);
	assert(suffix);
	assert(replace);
	assert(buff);

	char *sub = strstr(string, suffix);
	if (sub == NULL)
		return false;

	size_t prefix_len = sub - string;
	if (len < prefix_len) {
		_I("Buffer too small");
		return false;
	}

	strncpy(buff, string, prefix_len);
	if (snprintf(&buff[prefix_len], len, "%s", replace) < 0) {
		_E("Suffix replaceing error (%s %s -> %s): %m", string, suffix, replace);
		return false;
	}

	return true;
}

bool correct_file_path(const char *file_path, char *buff, size_t len)
{
	assert(file_path);
	assert(buff);

	bool result = false;
	char file_path_copy[PATH_MAX];

	strncpy(file_path_copy, file_path, sizeof(file_path_copy) - 1);

	char *file_name = basename(file_path_copy);
	char dll_name[PATH_MAX];

	if (!replace_suffix(file_name, ".ni.dll", ".dll", dll_name, sizeof(dll_name)))
		return false;

	_D("Correcting the file: %s", file_path);

	strncpy(file_path_copy, file_path, sizeof(file_path_copy) - 1);
	char *dir_name = dirname(file_path_copy);

	if (!file_exists_in_dir(dir_name, dll_name)) {
		char tmp_dir_name[PATH_MAX];

		strncpy(tmp_dir_name, dir_name, sizeof(tmp_dir_name) - 1);
		dir_name = dirname(tmp_dir_name);

		if (!file_exists_in_dir(dir_name, dll_name)) {
			_D("Cannot find the corresponding dll file for: %s", file_path);
			return false;
		}
	}

	result = snprintf(buff, len, "%s/%s", dir_name, dll_name) > 0;
	if (!result)
		_E("Can not correct file path %s: %m", file_path);

	return result;
}

void get_and_save_so_info(char *map_path, char *out_path)
{
	FILE *f = fopen(out_path, "w");
	if (f == NULL) {
		_E("Failed to open file for writing %s: %m", out_path);
		return;
	}

	GSList *file_list = get_filepaths(map_path);
	GSList *pkgs_not_found = NULL;

	rpmts ts = init_rpm();

	for (GSList *iterator = file_list; iterator; iterator = iterator->next) {
		char *file_path = (char *)iterator->data;
		char modified_file_path[PATH_MAX];
		char *build_id = NULL;

		if (is_dotnet_file(file_path)) {
			build_id = strdup("");
			if (correct_file_path(file_path, modified_file_path, sizeof(modified_file_path)))
				file_path = modified_file_path;
		} else if (get_build_id(file_path, &build_id) <= 0 || build_id == NULL) {
			continue;
		}

		char *rpm_info = get_rpm_info(ts, file_path);
		if (rpm_info == NULL) {
			struct rpm_info *ri = malloc(sizeof(struct rpm_info));
			if (ri == NULL) {
				_E("malloc error: %m");
				free(build_id);
				break;
			}
			ri->file_path = strdup(file_path);
			if (ri->file_path == NULL) {
				_E("strdup error: %m");
				free(build_id);
				free(ri);
				break;
			}
			ri->app_name = get_app_name_from_path(file_path);
			if (ri->app_name == NULL) {
				free(ri);
				free(build_id);
				continue;
			}
			ri->build_id = build_id;
			ri->rpm_info = NULL;
			pkgs_not_found = g_slist_append(pkgs_not_found, ri);
		} else {
			int fret = fprintf(f, "%s %s %s\n", file_path, build_id, rpm_info);
			free(rpm_info);
			free(build_id);

			if (fret < 0) {
				_E("Failed to write to file %s: %m", out_path);
				break;
			}
		}
	}

	search_for_tpk(ts, pkgs_not_found);

	for (GSList *iterator = pkgs_not_found; iterator; iterator = iterator->next) {
		struct rpm_info *ri = (struct rpm_info*)iterator->data;
		int fret;

		if (ri->rpm_info == NULL)
			fret = fprintf(f, "%s %s\n", ri->file_path, ri->build_id);
		else
			fret = fprintf(f, "%s %s %s\n", ri->file_path, ri->build_id, ri->rpm_info);

		if (fret < 0)
			_E("Failed to write to file %s: %m", out_path);

		rpm_info_free(ri);
	}

	g_slist_free(pkgs_not_found);

	fclose(f);

	for (GSList *iterator = file_list; iterator; iterator = iterator->next)
		free(iterator->data);

	g_slist_free(file_list);

	free_rpm(ts);
}