-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic.c
768 lines (667 loc) · 23.1 KB
/
dynamic.c
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
/*
* Author: Matthieu Carteron <[email protected]>
* date: 2024-07-17
*
* Alter ELF needed dependencies information (shrink to remove version).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include "dynamic.h"
#include "elffile.h"
static size_t available_length(const char *str, const size_t len)
{
size_t i = 0;
char lc = 1;
/* Walk through the string to detect the end of it */
while (i < len)
{
if (str[i] != '\0' && lc == '\0')
return i - 1;
/* Save the last character */
lc = str[i];
i++;
}
return 0;
}
static void write_type(char *data, int type)
{
char buffer[8] = { 0 };
if (is_e32())
{
int32_t value = type;
memcpy(buffer, &value, sizeof(int32_t));
if (!swap_bytes())
(*data) = (*((int32_t*)buffer));
else
(*data) = bswap_32(*((int32_t*)buffer));
}
else
{
int64_t value = type;
memcpy(buffer, &value, sizeof(int64_t));
if (!swap_bytes())
(*data) = (*((int64_t*)buffer));
else
(*data) = bswap_64(*((int64_t*)buffer));
}
}
static void write_string(char *str, const char *name, const size_t len, const size_t available)
{
/* Write the string */
memcpy(str, name, len);
/* Add zeros padding */
memset(str + len, 0, available - len);
}
static int write_input_to_output_until(int in, int out, long offset)
{
char buffer[1024];
long i = 0, l;
/* Write by chunks of 1024, until the offset is near */
while (i + 1024 < offset)
{
if (read(in, buffer, sizeof(buffer)) != sizeof(buffer))
{
fprintf(stderr, "Failed to read from the input file: %s!\n", strerror(errno));
return 0;
}
if (write(out, buffer, sizeof(buffer)) != sizeof(buffer))
{
fprintf(stderr, "Failed to write to the output file: %s!\n", strerror(errno));
return 0;
}
i += 1024;
}
/* Compute the remaining length */
l = offset - i;
if (read(in, buffer, l) != l)
{
fprintf(stderr, "Failed to read from the input file: %s!\n", strerror(errno));
return 0;
}
if (write(out, buffer, l) != l)
{
fprintf(stderr, "Failed to write to the output file: %s!\n", strerror(errno));
return 0;
}
return 1;
}
static int write_input_to_output_end(int in, int out)
{
char buffer[1024];
long i, l, end;
/* Get the end position of the file */
i = lseek(in, 0, SEEK_CUR);
end = lseek(in, 0, SEEK_END);
lseek(in, i, SEEK_SET);
/* Write by chunks of 1024, until the end */
while (i + 1024 < end)
{
if (read(in, buffer, sizeof(buffer)) != sizeof(buffer))
{
fprintf(stderr, "Failed to read from the input file: %s!\n", strerror(errno));
return 0;
}
if (write(out, buffer, sizeof(buffer)) != sizeof(buffer))
{
fprintf(stderr, "Failed to write to the output file: %s!\n", strerror(errno));
return 0;
}
i += 1024;
}
/* Compute the remaining length */
l = end - i;
if (read(in, buffer, l) != l)
{
fprintf(stderr, "Failed to read from the input file: %s!\n", strerror(errno));
return 0;
}
if (write(out, buffer, l) != l)
{
fprintf(stderr, "Failed to write to the output file: %s!\n", strerror(errno));
return 0;
}
return 1;
}
int dynamics_process(const LD_Cache *ldcache, const Priority priority, const char *filename, const char *output, const char *needOld, const char *needNew, const char *soname, const char *rpath, int fix)
{
Elf_Header ehdr;
Elf_Section shdr;
Elf_Program phdr;
size_t phdrlen, shdrlen, slotnum = 0, slots[2], slotstr[2], slotlen[2] = { 0 };
char *dyns = NULL, *strtab = NULL, *name;
int in = -1, out = -1, rv = 0, dynsmod = 0, needmod = 0, somod = 0, rmod = 0, i, j, l, last;
const int modifications = (needOld || soname || rpath || fix > 1);
/* Open the output file */
if (output && modifications)
{
if ((out = open(output, O_WRONLY | O_CREAT | O_TRUNC, 600)) == -1)
{
fprintf(stderr, "Failed to open the output file: %s!\n", strerror(errno));
rv = 4; goto RET;
}
}
/* Open the input ELF file */
if ((in = elf_open(filename, out == -1 ? O_RDWR : O_RDONLY, &ehdr)) == -1)
return 3;
/* Set the output file permissions */
if (out != -1)
{
struct stat stats;
if (fstat(in, &stats) != 0)
{
fprintf(stderr, "Failed to read the stats of the input file: %s!\n", strerror(errno));
rv = 3; goto RET;
}
if (fchmod(out, stats.st_mode) != 0)
{
fprintf(stderr, "Failed to change the permissions of the output file: %s!\n", strerror(errno));
rv = 3; goto RET;
}
}
/* Find the dynamic section */
if (elf_find_program(in, PT_DYNAMIC, &ehdr, &phdr) != 0)
{
rv = 3;
goto RET;
}
/* Allocate the dynamic section */
phdrlen = HDRWU(phdr, p_filesz);
if ((dyns = malloc(phdrlen)) == NULL)
{
fprintf(stderr, "Failed to allocate memory for dynamic section: %s!\n", strerror(errno));
rv = 3; goto RET;
}
/* Read the dynamic section */
memset(dyns, 0, phdrlen);
if (lseek(in, HDRWU(phdr, p_offset), SEEK_SET) == -1
|| read(in, dyns, phdrlen) != (ssize_t)phdrlen)
{
fprintf(stderr, "Failed to read dynamic section: %s!\n", strerror(errno));
rv = 3; goto RET;
}
/* Find the string table section */
if (elf_find_section(in, SHT_STRTAB, &ehdr, &shdr) != 0)
{
rv = 3;
goto RET;
}
/* Allocate the dynamic section */
shdrlen = HDRWU(shdr, sh_size);
if ((strtab = malloc(shdrlen)) == NULL)
{
fprintf(stderr, "Failed to allocate memory for string table: %s!\n", strerror(errno));
rv = 3; goto RET;
}
/* Read the dynamic section */
memset(strtab, 0, shdrlen);
if (lseek(in, HDRWU(shdr, sh_offset), SEEK_SET) == -1
|| read(in, strtab, shdrlen) != (ssize_t)shdrlen)
{
fprintf(stderr, "Failed to read string table: %s!\n", strerror(errno));
rv = 3; goto RET;
}
/* If no modifications have to be done, just print infos about the dynamics */
if (modifications)
{
printf("Processing file: %s\n", filename);
last = -2;
}
else if (fix == 0)
{
printf("[ELF dynamic table informations]\n File: %s\n", filename);
last = -1;
}
else
last = -2;
/* The 'removed' dynamic entries are actually turned into type DT_DEBUG.
* Since the debug sections are vastly unused.
* Do a first pass to list all debug sections.
*/
if (soname > REMOVAL || rpath > REMOVAL)
{
i = 0;
while (i < phdrlen)
{
switch (SWAPS(&dyns[i]))
{
case DT_DEBUG:
/* Check if the value points outside of the string table (which may be) */
if (SWAPU(&dyns[i]) > shdrlen)
goto DEFAULT;
/* Pick the slot with the minimum current length */
l = slotlen[0] > slotlen[1] ? 1 : 0;
slots[l] = i;
ADV(i, 1);
slotstr[l] = SWAPU(&dyns[i]);
name = &strtab[SWAPU(&dyns[i])];
ADV(i, 1);
/* Compute the available length of the slot */
slotlen[l] = available_length(name, shdrlen - (name - strtab));
slotnum++;
break;
DEFAULT:
default:
ADV(i, 2);
break;
}
}
}
/* Process the dynamic entries */
i = 0;
while (i < phdrlen)
{
switch (SWAPS(&dyns[i]))
{
case DT_NEEDED:
/* Retrieve the library name */
ADV(i, 1);
name = &strtab[SWAPU(&dyns[i])];
ADV(i, 1);
if (last > -2)
{
if (last != 0)
puts("");
last = 0;
printf("· Needed: %s\n", name);
break;
}
if (needOld != NULL)
{
/* Check if this is the name to be replaced */
if (strcmp(name, needOld) != 0)
break;
needmod = 1;
/* Compute the available length in the string table */
const size_t len = strlen(needNew);
const size_t available = available_length(name, shdrlen - (name - strtab));
if (len > available)
{
fputs("The new name is too big to fit!\n", stderr);
break;
}
printf("Replacing needed: %s => %s...\n", needOld, needNew);
/* Check if the new name is in the cache */
if (ldcache != NULL)
{
if (ldcache_search(ldcache, needNew) == NULL)
fprintf(stderr, "Warning! The library name %s was not found in the cache!\nYou may want to run `ldconfig`.\n", needNew);
}
/* Write in the string table */
write_string(name, needNew, len, available);
}
break;
case DT_SONAME:
/* Save the offset of the type */
j = i;
/* Retrieve the SO name */
ADV(i, 1);
name = &strtab[SWAPU(&dyns[i])];
ADV(i, 1);
if (last > -2)
{
if (last != 1)
puts("");
last = 1;
printf("· Soname: %s\n", name);
break;
}
if (soname != NULL)
{
somod = 1;
/* If to be removed, replace the type with DT_DEBUG */
if (soname == REMOVAL)
{
puts("Removing soname entry...");
write_type(&dyns[j], DT_DEBUG);
dynsmod = 1;
break;
}
/* Compute the available length in the string table */
const size_t len = strlen(soname);
const size_t available = available_length(name, shdrlen - (name - strtab));
if (len > available)
{
fputs("The new soname is too big to fit!\n", stderr);
break;
}
printf("Setting soname: %s...\n", soname);
/* Write in the string table */
write_string(name, soname, len, available);
}
break;
case DT_RPATH:
/* Change the type if priority doesn't match */
if (priority == PRI_RUNPATH)
{
fputs("Changing run-time priority to low...\n", stderr);
write_type(&dyns[i], DT_RUNPATH);
dynsmod = 1;
}
case DT_RUNPATH:
/* Change the type if priority doesn't match */
if (priority == PRI_RPATH)
{
fputs("Changing run-time priority to high...\n", stderr);
write_type(&dyns[i], DT_RPATH);
dynsmod = 1;
}
/* Save the offset of the type */
j = i;
/* Retrieve the run-time path */
ADV(i, 1);
name = &strtab[SWAPU(&dyns[i])];
ADV(i, 1);
if (last > -2)
{
if (last != 2)
puts("");
last = 2;
printf("· Run-time path: %s\n", name);
break;
}
if (rpath != NULL)
{
rmod = 1;
/* If to be removed, replace the type with DT_DEBUG */
if (rpath == REMOVAL)
{
puts("Removing run-time path entry...");
write_type(&dyns[j], DT_DEBUG);
dynsmod = 1;
break;
}
/* Compute the available length in the string table */
const size_t len = strlen(rpath);
const size_t available = available_length(name, shdrlen - (name - strtab));
if (len > available)
{
fputs("The new run-time path is too big to fit!\n", stderr);
break;
}
printf("Setting run-time path: %s...\n", rpath);
/* Write in the string table */
write_string(name, rpath, len, available);
}
break;
default:
ADV(i, 2);
break;
}
}
/* If no concerned sections have been found yet, try the DT_DEBUG ones */
while (slotnum > 0)
{
if (soname > REMOVAL && !somod)
{
somod = 1;
slotnum--;
/* Pick the slot with the maximum length */
l = slotlen[0] < slotlen[1] ? 1 : 0;
/* Compute the available length in the string table */
const size_t len = strlen(soname);
const size_t available = slotlen[l];
if (len > available)
fputs("The new run-time path is too big to fit!\n", stderr);
else
{
printf("Adding soname: %s...\n", soname);
write_type(&dyns[slots[l]], DT_SONAME);
dynsmod = 1;
/* Write in the string table */
name = &strtab[slotstr[l]];
write_string(name, soname, len, available);
}
}
else if (rpath > REMOVAL && !rmod)
{
rmod = 1;
slotnum--;
/* Pick the slot with the maximum length */
l = slotlen[0] < slotlen[1] ? 1 : 0;
/* Compute the available length in the string table */
const size_t len = strlen(rpath);
const size_t available = slotlen[l];
if (len > available)
fputs("The new run-time path is too big to fit!\n", stderr);
else
{
printf("Adding run-time path: %s...\n", rpath);
write_type(&dyns[slots[l]], priority == PRI_RUNPATH ? DT_RUNPATH : DT_RPATH);
dynsmod = 1;
/* Write in the string table */
name = &strtab[slotstr[l]];
write_string(name, rpath, len, available);
}
}
}
/* Perform late automatic fixing, if requested */
if (fix != 0 && ldcache != NULL)
{
i = 0;
while (i < phdrlen)
{
if (SWAPS(&dyns[i]) == DT_NEEDED)
{
/* Retrieve the library name */
ADV(i, 1);
name = &strtab[SWAPU(&dyns[i])];
ADV(i, 1);
/* Search it in the cache */
if (ldcache_search(ldcache, name) != NULL)
continue;
/* Find the closest library matching it's name and being slim enough */
const size_t available = available_length(name, shdrlen - (name - strtab));
const char *newName = ldcache_replacement(ldcache, name, available);
if (newName == NULL)
{
fprintf(stderr, "Failed to find a replacement for %s\n", name);
continue;
}
/* Whether the fix is actually performed, or just dry-run */
if (fix > 1)
{
needmod = 1;
printf("Fixing needed: %s => %s...\n", name, newName);
/* Write in the string table */
write_string(name, newName, strlen(newName), available);
}
else
printf("Replacement found: %s => %s...\n", name, newName);
}
else
ADV(i, 2);
}
}
/* Write the output file */
if (needmod || somod || rmod)
{
if (out == -1)
{
/* Position to the offset of the string table section */
if (lseek(in, HDRWU(shdr, sh_offset), SEEK_SET) == -1)
{
fprintf(stderr, "Failed to position to the string table: %s!\n", strerror(errno));
rv = 4; goto RET;
}
/* Write the modified string table */
if (write(in, strtab, shdrlen) != shdrlen)
{
fprintf(stderr, "Failed to write to the string table: %s!\n", strerror(errno));
rv = 4; goto RET;
}
if (dynsmod)
{
/* Position to the offset of the dynamic section */
if (lseek(in, HDRWU(phdr, p_offset), SEEK_SET) == -1)
{
fprintf(stderr, "Failed to position to the dynamic section: %s!\n", strerror(errno));
rv = 4; goto RET;
}
/* Write the modified dynamic section */
if (write(in, dyns, phdrlen) != phdrlen)
{
fprintf(stderr, "Failed to write to the dynamic section: %s!\n", strerror(errno));
rv = 4; goto RET;
}
}
}
else
{
/* Position to the beginning of the input file */
lseek(in, 0, SEEK_SET);
/* Copy the data until the string table section */
if (!write_input_to_output_until(in, out, HDRWU(shdr, sh_offset)))
{
rv = 4;
goto RET;
}
/* Write the string table section */
if (write(out, strtab, shdrlen) != shdrlen)
{
fprintf(stderr, "Failed to write to the string table: %s!\n", strerror(errno));
rv = 4; goto RET;
}
/* Advance the input until the end of the string table section */
if (lseek(in, shdrlen, SEEK_CUR) == -1)
{
fprintf(stderr, "Failed to advance in the input file: %s!\n", strerror(errno));
rv = 4; goto RET;
}
if (dynsmod)
{
/* Copy the data until the dynamic section */
if (!write_input_to_output_until(in, out, HDRWU(phdr, p_offset) - lseek(in, 0, SEEK_CUR)))
{
rv = 4;
goto RET;
}
/* Write the dynamic section */
if (write(out, dyns, phdrlen) != phdrlen)
{
fprintf(stderr, "Failed to write to the dynamic section: %s!\n", strerror(errno));
rv = 4; goto RET;
}
/* Advance the input until the end of the dynamic section */
if (lseek(in, phdrlen, SEEK_CUR) == -1)
{
fprintf(stderr, "Failed to advance in the input file: %s!\n", strerror(errno));
rv = 4; goto RET;
}
}
/* Copy the rest of the data */
if (!write_input_to_output_end(in, out))
{
rv = 4;
goto RET;
}
}
}
/* Warn if something wanted to be done, but nothing actually was */
if (needOld && !needmod)
fprintf(stderr, "Warning! No needed library with name %s was found.\n", needOld);
if (soname && !somod)
fputs("Warning! No available section was found to modify soname.\n", stderr);
if (rpath && !rmod)
fputs("Warning! No available section was found to modify run-time path.\n", stderr);
RET:
if (in != -1)
elf_close(in);
if (out != -1)
close(out);
free(strtab);
free(dyns);
return rv;
}
int dynamics_query(const char *filename, const Query query)
{
Elf_Header ehdr;
Elf_Section shdr;
Elf_Program phdr;
size_t phdrlen, shdrlen;
char *dyns = NULL, *strtab = NULL, *name;
int in = -1, rv = 0, i, once, type, typealt;
/* Open the input ELF file */
if ((in = elf_open(filename, O_RDONLY, &ehdr)) == -1)
return 3;
/* Find the dynamic section */
if (elf_find_program(in, PT_DYNAMIC, &ehdr, &phdr) != 0)
{
rv = 3;
goto RET;
}
/* Allocate the dynamic section */
phdrlen = HDRWU(phdr, p_filesz);
if ((dyns = malloc(phdrlen)) == NULL)
{
fprintf(stderr, "Failed to allocate memory for dynamic section: %s!\n", strerror(errno));
rv = 3; goto RET;
}
/* Read the dynamic section */
memset(dyns, 0, phdrlen);
if (lseek(in, HDRWU(phdr, p_offset), SEEK_SET) == -1
|| read(in, dyns, phdrlen) != (ssize_t)phdrlen)
{
fprintf(stderr, "Failed to read dynamic section: %s!\n", strerror(errno));
rv = 3; goto RET;
}
/* Find the string table section */
if (elf_find_section(in, SHT_STRTAB, &ehdr, &shdr) != 0)
{
rv = 3;
goto RET;
}
/* Allocate the dynamic section */
shdrlen = HDRWU(shdr, sh_size);
if ((strtab = malloc(shdrlen)) == NULL)
{
fprintf(stderr, "Failed to allocate memory for string table: %s!\n", strerror(errno));
rv = 3; goto RET;
}
/* Read the dynamic section */
memset(strtab, 0, shdrlen);
if (lseek(in, HDRWU(shdr, sh_offset), SEEK_SET) == -1
|| read(in, strtab, shdrlen) != (ssize_t)shdrlen)
{
fprintf(stderr, "Failed to read string table: %s!\n", strerror(errno));
rv = 3; goto RET;
}
/* Determine the query type */
switch (query)
{
case QU_NEEDED: type = typealt = DT_NEEDED; once = 0; break;
case QU_SONAME: type = typealt = DT_SONAME; once = 1; break;
case QU_RPATH: type = DT_RPATH; typealt = DT_RUNPATH; once = 1; break;
default: rv = 5; goto RET;
}
/* Query the dynamic entries */
i = 0;
while (i < phdrlen)
{
const int t = SWAPS(&dyns[i]);
if (t == type || t == typealt)
{
/* Write the raw output */
ADV(i, 1);
name = &strtab[SWAPU(&dyns[i])];
ADV(i, 1);
puts(name);
/* If only one can be present */
if (once)
break;
}
else
ADV(i, 2);
}
RET:
if (in != -1)
elf_close(in);
free(strtab);
free(dyns);
return rv;
}