-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscanelf.c
2182 lines (1977 loc) · 62.4 KB
/
scanelf.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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2003-2007 Gentoo Foundation
* Distributed under the terms of the GNU General Public License v2
* $Header: /var/cvsroot/gentoo-projects/pax-utils/scanelf.c,v 1.222 2010/12/08 01:24:01 vapier Exp $
*
* Copyright 2003-2007 Ned Ludd - <[email protected]>
* Copyright 2004-2007 Mike Frysinger - <[email protected]>
*/
static const char *rcsid = "$Id: scanelf.c,v 1.222 2010/12/08 01:24:01 vapier Exp $";
const char argv0[] = "scanelf";
#include "paxinc.h"
#define IS_MODIFIER(c) (c == '%' || c == '#' || c == '+')
/* prototypes */
static int file_matches_list(const char *filename, char **matchlist);
static int scanelf_elfobj(elfobj *elf);
static int scanelf_elf(const char *filename, int fd, size_t len);
static int scanelf_archive(const char *filename, int fd, size_t len);
static int scanelf_file(const char *filename, const struct stat *st_cache);
static int scanelf_dir(const char *path);
static void scanelf_ldpath(void);
static void scanelf_envpath(void);
static void usage(int status);
static char **get_split_env(const char *envvar);
static void parseenv(void);
static int parseargs(int argc, char *argv[]);
/* variables to control behavior */
static char match_etypes[126] = "";
static char *ldpaths[256];
static char scan_ldpath = 0;
static char scan_envpath = 0;
static char scan_symlink = 1;
static char scan_archives = 0;
static char dir_recurse = 0;
static char dir_crossmount = 1;
static char show_pax = 0;
static char show_perms = 0;
static char show_size = 0;
static char show_phdr = 0;
static char show_textrel = 0;
static char show_rpath = 0;
static char show_needed = 0;
static char show_interp = 0;
static char show_bind = 0;
static char show_soname = 0;
static char show_textrels = 0;
static char show_banner = 1;
static char show_endian = 0;
static char show_osabi = 0;
static char show_eabi = 0;
static char be_quiet = 0;
static char be_verbose = 0;
static char be_wewy_wewy_quiet = 0;
static char be_semi_verbose = 0;
static char *find_sym = NULL;
static char *find_lib = NULL;
static char *find_section = NULL;
static char *out_format = NULL;
static char *search_path = NULL;
static char fix_elf = 0;
static char g_match = 0;
static char use_ldcache = 0;
static char **qa_textrels = NULL;
static char **qa_execstack = NULL;
static char **qa_wx_load = NULL;
static int match_bits = 0;
static unsigned int match_perms = 0;
static void *ldcache = NULL;
static size_t ldcache_size = 0;
static unsigned long setpax = 0UL;
static int has_objdump = 0;
/* find the path to a file by name */
static char *which(const char *fname)
{
static char fullpath[__PAX_UTILS_PATH_MAX];
char *path, *p;
path = getenv("PATH");
if (!path)
return NULL;
path = xstrdup(path);
while ((p = strrchr(path, ':')) != NULL) {
snprintf(fullpath, sizeof(fullpath), "%s/%s", p + 1, fname);
*p = 0;
if (access(fullpath, R_OK) != -1) {
free(path);
return fullpath;
}
}
free(path);
return NULL;
}
/* 1 on failure. 0 otherwise */
static int rematch(const char *regex, const char *match, int cflags)
{
regex_t preg;
int ret;
if ((match == NULL) || (regex == NULL))
return EXIT_FAILURE;
if ((ret = regcomp(&preg, regex, cflags))) {
char err[256];
if (regerror(ret, &preg, err, sizeof(err)))
fprintf(stderr, "regcomp failed: %s", err);
else
fprintf(stderr, "regcomp failed");
return EXIT_FAILURE;
}
ret = regexec(&preg, match, 0, NULL, 0);
regfree(&preg);
return ret;
}
/* sub-funcs for scanelf_file() */
static void scanelf_file_get_symtabs(elfobj *elf, void **sym, void **tab)
{
/* find the best SHT_DYNSYM and SHT_STRTAB sections */
/* debug sections */
void *symtab = elf_findsecbyname(elf, ".symtab");
void *strtab = elf_findsecbyname(elf, ".strtab");
/* runtime sections */
void *dynsym = elf_findsecbyname(elf, ".dynsym");
void *dynstr = elf_findsecbyname(elf, ".dynstr");
#define GET_SYMTABS(B) \
if (elf->elf_class == ELFCLASS ## B) { \
if (symtab && dynsym) { \
Elf ## B ## _Shdr *esymtab = symtab; \
Elf ## B ## _Shdr *edynsym = dynsym; \
*sym = (EGET(esymtab->sh_size) > EGET(edynsym->sh_size)) ? symtab : dynsym; \
} else \
*sym = symtab ? symtab : dynsym; \
if (strtab && dynstr) { \
Elf ## B ## _Shdr *estrtab = strtab; \
Elf ## B ## _Shdr *edynstr = dynstr; \
*tab = (EGET(estrtab->sh_size) > EGET(edynstr->sh_size)) ? strtab : dynstr; \
} else \
*tab = strtab ? strtab : dynstr; \
}
GET_SYMTABS(32)
GET_SYMTABS(64)
}
static char *scanelf_file_pax(elfobj *elf, char *found_pax)
{
static char ret[7];
unsigned long i, shown;
if (!show_pax) return NULL;
shown = 0;
memset(&ret, 0, sizeof(ret));
if (elf->phdr) {
#define SHOW_PAX(B) \
if (elf->elf_class == ELFCLASS ## B) { \
Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
if (EGET(phdr[i].p_type) != PT_PAX_FLAGS) \
continue; \
if (fix_elf && setpax) { \
/* set the paxctl flags */ \
ESET(phdr[i].p_flags, setpax); \
} \
if (be_quiet && (EGET(phdr[i].p_flags) == (PF_NOEMUTRAMP | PF_NORANDEXEC))) \
continue; \
memcpy(ret, pax_short_pf_flags(EGET(phdr[i].p_flags)), 6); \
*found_pax = 1; \
++shown; \
break; \
} \
}
SHOW_PAX(32)
SHOW_PAX(64)
}
if (fix_elf && setpax) {
/* set the chpax settings */
if (elf->elf_class == ELFCLASS32) {
if (EHDR32(elf->ehdr)->e_type == ET_DYN || EHDR32(elf->ehdr)->e_type == ET_EXEC)
ESET(EHDR32(elf->ehdr)->e_ident[EI_PAX], pax_pf2hf_flags(setpax));
} else {
if (EHDR64(elf->ehdr)->e_type == ET_DYN || EHDR64(elf->ehdr)->e_type == ET_EXEC)
ESET(EHDR64(elf->ehdr)->e_ident[EI_PAX], pax_pf2hf_flags(setpax));
}
}
/* fall back to EI_PAX if no PT_PAX was found */
if (!*ret) {
static char *paxflags;
paxflags = pax_short_hf_flags(EI_PAX_FLAGS(elf));
if (!be_quiet || (be_quiet && EI_PAX_FLAGS(elf))) {
*found_pax = 1;
return (be_wewy_wewy_quiet ? NULL : paxflags);
}
strncpy(ret, paxflags, sizeof(ret));
}
if (be_wewy_wewy_quiet || (be_quiet && !shown))
return NULL;
else
return ret;
}
static char *scanelf_file_phdr(elfobj *elf, char *found_phdr, char *found_relro, char *found_load)
{
static char ret[12];
char *found;
unsigned long i, shown, multi_stack, multi_relro, multi_load;
int max_pt_load;
if (!show_phdr) return NULL;
memcpy(ret, "--- --- ---\0", 12);
shown = 0;
multi_stack = multi_relro = multi_load = 0;
max_pt_load = elf_max_pt_load(elf);
#define NOTE_GNU_STACK ".note.GNU-stack"
#define SHOW_PHDR(B) \
if (elf->elf_class == ELFCLASS ## B) { \
Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
Elf ## B ## _Off offset; \
uint32_t flags, check_flags; \
if (elf->phdr != NULL) { \
Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
for (i = 0; i < EGET(ehdr->e_phnum); ++i) { \
if (EGET(phdr[i].p_type) == PT_GNU_STACK) { \
if (multi_stack++) \
warnf("%s: multiple PT_GNU_STACK's !?", elf->filename); \
if (file_matches_list(elf->filename, qa_execstack)) \
continue; \
found = found_phdr; \
offset = 0; \
check_flags = PF_X; \
} else if (EGET(phdr[i].p_type) == PT_GNU_RELRO) { \
if (multi_relro++) \
warnf("%s: multiple PT_GNU_RELRO's !?", elf->filename); \
found = found_relro; \
offset = 4; \
check_flags = PF_X; \
} else if (EGET(phdr[i].p_type) == PT_LOAD) { \
if (EGET(ehdr->e_type) == ET_DYN || EGET(ehdr->e_type) == ET_EXEC) \
if (multi_load++ > max_pt_load) \
warnf("%s: more than %i PT_LOAD's !?", elf->filename, max_pt_load); \
if (file_matches_list(elf->filename, qa_wx_load)) \
continue; \
found = found_load; \
offset = 8; \
check_flags = PF_W|PF_X; \
} else \
continue; \
flags = EGET(phdr[i].p_flags); \
if (be_quiet && ((flags & check_flags) != check_flags)) \
continue; \
if ((EGET(phdr[i].p_type) != PT_LOAD) && (fix_elf && ((flags & PF_X) != flags))) { \
ESET(phdr[i].p_flags, flags & (PF_X ^ (size_t)-1)); \
ret[3] = ret[7] = '!'; \
flags = EGET(phdr[i].p_flags); \
} \
memcpy(ret+offset, gnu_short_stack_flags(flags), 3); \
*found = 1; \
++shown; \
} \
} else if (elf->shdr != NULL) { \
/* no program headers which means this is prob an object file */ \
Elf ## B ## _Shdr *shdr = SHDR ## B (elf->shdr); \
Elf ## B ## _Shdr *strtbl = shdr + EGET(ehdr->e_shstrndx); \
char *str; \
if ((void*)strtbl > elf->data_end) \
goto skip_this_shdr##B; \
check_flags = SHF_WRITE|SHF_EXECINSTR; \
for (i = 0; i < EGET(ehdr->e_shnum); ++i) { \
if (EGET(shdr[i].sh_type) != SHT_PROGBITS) continue; \
offset = EGET(strtbl->sh_offset) + EGET(shdr[i].sh_name); \
str = elf->data + offset; \
if (str > elf->data + offset + sizeof(NOTE_GNU_STACK)) continue; \
if (!strcmp(str, NOTE_GNU_STACK)) { \
if (multi_stack++) warnf("%s: multiple .note.GNU-stack's !?", elf->filename); \
flags = EGET(shdr[i].sh_flags); \
if (be_quiet && ((flags & check_flags) != check_flags)) \
continue; \
++*found_phdr; \
shown = 1; \
if (flags & SHF_WRITE) ret[0] = 'W'; \
if (flags & SHF_ALLOC) ret[1] = 'A'; \
if (flags & SHF_EXECINSTR) ret[2] = 'X'; \
if (flags & 0xFFFFFFF8) warn("Invalid section flags for GNU-stack"); \
break; \
} \
} \
skip_this_shdr##B: \
if (!multi_stack) { \
if (file_matches_list(elf->filename, qa_execstack)) \
return NULL; \
*found_phdr = 1; \
shown = 1; \
memcpy(ret, "!WX", 3); \
} \
} \
}
SHOW_PHDR(32)
SHOW_PHDR(64)
if (be_wewy_wewy_quiet || (be_quiet && !shown))
return NULL;
else
return ret;
}
/*
* See if this ELF contains a DT_TEXTREL tag in any of its
* PT_DYNAMIC sections.
*/
static const char *scanelf_file_textrel(elfobj *elf, char *found_textrel)
{
static const char *ret = "TEXTREL";
unsigned long i;
if (!show_textrel && !show_textrels) return NULL;
if (file_matches_list(elf->filename, qa_textrels)) return NULL;
if (elf->phdr) {
#define SHOW_TEXTREL(B) \
if (elf->elf_class == ELFCLASS ## B) { \
Elf ## B ## _Dyn *dyn; \
Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
Elf ## B ## _Off offset; \
for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
if (EGET(phdr[i].p_type) != PT_DYNAMIC || EGET(phdr[i].p_filesz) == 0) continue; \
offset = EGET(phdr[i].p_offset); \
if (offset >= elf->len - sizeof(Elf ## B ## _Dyn)) continue; \
dyn = DYN ## B (elf->vdata + offset); \
while (EGET(dyn->d_tag) != DT_NULL) { \
if (EGET(dyn->d_tag) == DT_TEXTREL) { /*dyn->d_tag != DT_FLAGS)*/ \
*found_textrel = 1; \
/*if (dyn->d_un.d_val & DF_TEXTREL)*/ \
return (be_wewy_wewy_quiet ? NULL : ret); \
} \
++dyn; \
} \
} }
SHOW_TEXTREL(32)
SHOW_TEXTREL(64)
}
if (be_quiet || be_wewy_wewy_quiet)
return NULL;
else
return " - ";
}
/*
* Scan the .text section to see if there are any relocations in it.
* Should rewrite this to check PT_LOAD sections that are marked
* Executable rather than the section named '.text'.
*/
static char *scanelf_file_textrels(elfobj *elf, char *found_textrels, char *found_textrel)
{
unsigned long s, r, rmax;
void *symtab_void, *strtab_void, *text_void;
if (!show_textrels) return NULL;
/* don't search for TEXTREL's if the ELF doesn't have any */
if (!*found_textrel) scanelf_file_textrel(elf, found_textrel);
if (!*found_textrel) return NULL;
scanelf_file_get_symtabs(elf, &symtab_void, &strtab_void);
text_void = elf_findsecbyname(elf, ".text");
if (symtab_void && strtab_void && text_void && elf->shdr) {
#define SHOW_TEXTRELS(B) \
if (elf->elf_class == ELFCLASS ## B) { \
Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
Elf ## B ## _Shdr *shdr = SHDR ## B (elf->shdr); \
Elf ## B ## _Shdr *symtab = SHDR ## B (symtab_void); \
Elf ## B ## _Shdr *strtab = SHDR ## B (strtab_void); \
Elf ## B ## _Shdr *text = SHDR ## B (text_void); \
Elf ## B ## _Addr vaddr = EGET(text->sh_addr); \
uint ## B ## _t memsz = EGET(text->sh_size); \
Elf ## B ## _Rel *rel; \
Elf ## B ## _Rela *rela; \
/* search the section headers for relocations */ \
for (s = 0; s < EGET(ehdr->e_shnum); ++s) { \
uint32_t sh_type = EGET(shdr[s].sh_type); \
if (sh_type == SHT_REL) { \
rel = REL ## B (elf->vdata + EGET(shdr[s].sh_offset)); \
rela = NULL; \
rmax = EGET(shdr[s].sh_size) / sizeof(*rel); \
} else if (sh_type == SHT_RELA) { \
rel = NULL; \
rela = RELA ## B (elf->vdata + EGET(shdr[s].sh_offset)); \
rmax = EGET(shdr[s].sh_size) / sizeof(*rela); \
} else \
continue; \
/* now see if any of the relocs are in the .text */ \
for (r = 0; r < rmax; ++r) { \
unsigned long sym_max; \
Elf ## B ## _Addr offset_tmp; \
Elf ## B ## _Sym *func; \
Elf ## B ## _Sym *sym; \
Elf ## B ## _Addr r_offset; \
uint ## B ## _t r_info; \
if (sh_type == SHT_REL) { \
r_offset = EGET(rel[r].r_offset); \
r_info = EGET(rel[r].r_info); \
} else { \
r_offset = EGET(rela[r].r_offset); \
r_info = EGET(rela[r].r_info); \
} \
/* make sure this relocation is inside of the .text */ \
if (r_offset < vaddr || r_offset >= vaddr + memsz) { \
if (be_verbose <= 2) continue; \
} else \
*found_textrels = 1; \
/* locate this relocation symbol name */ \
sym = SYM ## B (elf->vdata + EGET(symtab->sh_offset)); \
if ((void*)sym > elf->data_end) { \
warn("%s: corrupt ELF symbol", elf->filename); \
continue; \
} \
sym_max = ELF ## B ## _R_SYM(r_info); \
if (sym_max * EGET(symtab->sh_entsize) < symtab->sh_size) \
sym += sym_max; \
else \
sym = NULL; \
sym_max = EGET(symtab->sh_size) / EGET(symtab->sh_entsize); \
/* show the raw details about this reloc */ \
printf(" %s: ", elf->base_filename); \
if (sym && sym->st_name) \
printf("%s", elf->data + EGET(strtab->sh_offset) + EGET(sym->st_name)); \
else \
printf("(memory/data?)"); \
printf(" [0x%lX]", (unsigned long)r_offset); \
/* now try to find the closest symbol that this rel is probably in */ \
sym = SYM ## B (elf->vdata + EGET(symtab->sh_offset)); \
func = NULL; \
offset_tmp = 0; \
while (sym_max--) { \
if (EGET(sym->st_value) < r_offset && EGET(sym->st_value) > offset_tmp) { \
func = sym; \
offset_tmp = EGET(sym->st_value); \
} \
++sym; \
} \
printf(" in "); \
if (func && func->st_name) { \
const char *func_name = elf->data + EGET(strtab->sh_offset) + EGET(func->st_name); \
if (r_offset > EGET(func->st_size)) \
printf("(optimized out: previous %s)", func_name); \
else \
printf("%s", func_name); \
} else \
printf("(optimized out)"); \
printf(" [0x%lX]\n", (unsigned long)offset_tmp); \
if (be_verbose && has_objdump) { \
Elf ## B ## _Addr end_addr = offset_tmp + EGET(func->st_size); \
char *sysbuf; \
size_t syslen; \
int sysret; \
const char sysfmt[] = "objdump -r -R -d -w -l --start-address=0x%lX --stop-address=0x%lX %s | grep --color -i -C 3 '.*[[:space:]]%lX:[[:space:]]*R_.*'\n"; \
syslen = sizeof(sysfmt) + strlen(elf->filename) + 3 * sizeof(unsigned long) + 1; \
sysbuf = xmalloc(syslen); \
if (end_addr < r_offset) \
/* not uncommon when things are optimized out */ \
end_addr = r_offset + 0x100; \
snprintf(sysbuf, syslen, sysfmt, \
(unsigned long)offset_tmp, \
(unsigned long)end_addr, \
elf->filename, \
(unsigned long)r_offset); \
fflush(stdout); \
sysret = system(sysbuf); \
fflush(stdout); \
free(sysbuf); \
} \
} \
} }
SHOW_TEXTRELS(32)
SHOW_TEXTRELS(64)
}
if (!*found_textrels)
warnf("ELF %s has TEXTREL markings but doesnt appear to have any real TEXTREL's !?", elf->filename);
return NULL;
}
static void rpath_security_checks(elfobj *, char *, const char *);
static void rpath_security_checks(elfobj *elf, char *item, const char *dt_type)
{
struct stat st;
switch (*item) {
case '/': break;
case '.':
warnf("Security problem with relative %s '%s' in %s", dt_type, item, elf->filename);
break;
case ':':
case '\0':
warnf("Security problem NULL %s in %s", dt_type, elf->filename);
break;
case '$':
if (fstat(elf->fd, &st) != -1)
if ((st.st_mode & S_ISUID) || (st.st_mode & S_ISGID))
warnf("Security problem with %s='%s' in %s with mode set of %o",
dt_type, item, elf->filename, (unsigned int) st.st_mode & 07777);
break;
default:
warnf("Maybe? sec problem with %s='%s' in %s", dt_type, item, elf->filename);
break;
}
}
static void scanelf_file_rpath(elfobj *elf, char *found_rpath, char **ret, size_t *ret_len)
{
unsigned long i, s;
char *rpath, *runpath, **r;
void *strtbl_void;
if (!show_rpath) return;
strtbl_void = elf_findsecbyname(elf, ".dynstr");
rpath = runpath = NULL;
if (elf->phdr && strtbl_void) {
#define SHOW_RPATH(B) \
if (elf->elf_class == ELFCLASS ## B) { \
Elf ## B ## _Dyn *dyn; \
Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
Elf ## B ## _Shdr *strtbl = SHDR ## B (strtbl_void); \
Elf ## B ## _Off offset; \
Elf ## B ## _Xword word; \
/* Scan all the program headers */ \
for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
/* Just scan dynamic headers */ \
if (EGET(phdr[i].p_type) != PT_DYNAMIC || EGET(phdr[i].p_filesz) == 0) continue; \
offset = EGET(phdr[i].p_offset); \
if (offset >= elf->len - sizeof(Elf ## B ## _Dyn)) continue; \
/* Just scan dynamic RPATH/RUNPATH headers */ \
dyn = DYN ## B (elf->vdata + offset); \
while ((word=EGET(dyn->d_tag)) != DT_NULL) { \
if (word == DT_RPATH) { \
r = &rpath; \
} else if (word == DT_RUNPATH) { \
r = &runpath; \
} else { \
++dyn; \
continue; \
} \
/* Verify the memory is somewhat sane */ \
offset = EGET(strtbl->sh_offset) + EGET(dyn->d_un.d_ptr); \
if (offset < (Elf ## B ## _Off)elf->len) { \
if (*r) warn("ELF has multiple %s's !?", get_elfdtype(word)); \
*r = elf->data + offset; \
/* cache the length in case we need to nuke this section later on */ \
if (fix_elf) \
offset = strlen(*r); \
/* If quiet, don't output paths in ld.so.conf */ \
if (be_quiet) { \
size_t len; \
char *start, *end; \
/* note that we only 'chop' off leading known paths. */ \
/* since *r is read-only memory, we can only move the ptr forward. */ \
start = *r; \
/* scan each path in : delimited list */ \
while (start) { \
rpath_security_checks(elf, start, get_elfdtype(word)); \
end = strchr(start, ':'); \
len = (end ? abs(end - start) : strlen(start)); \
if (use_ldcache) \
for (s = 0; ldpaths[s]; ++s) \
if (!strncmp(ldpaths[s], start, len) && !ldpaths[s][len]) { \
*r = end; \
/* corner case ... if RPATH reads "/usr/lib:", we want \
* to show ':' rather than '' */ \
if (end && end[1] != '\0') \
(*r)++; \
break; \
} \
if (!*r || !end) \
break; \
else \
start = start + len + 1; \
} \
} \
if (*r) { \
if (fix_elf > 2 || (fix_elf && **r == '\0')) { \
/* just nuke it */ \
nuke_it##B: \
memset(*r, 0x00, offset); \
*r = NULL; \
ESET(dyn->d_tag, DT_DEBUG); \
ESET(dyn->d_un.d_ptr, 0); \
} else if (fix_elf) { \
/* try to clean "bad" paths */ \
size_t len, tmpdir_len; \
char *start, *end; \
const char *tmpdir; \
start = *r; \
tmpdir = (getenv("TMPDIR") ? : "."); \
tmpdir_len = strlen(tmpdir); \
while (1) { \
end = strchr(start, ':'); \
if (start == end) { \
eat_this_path##B: \
len = strlen(end); \
memmove(start, end+1, len); \
start[len-1] = '\0'; \
end = start - 1; \
} else if (tmpdir && !strncmp(start, tmpdir, tmpdir_len)) { \
if (!end) { \
if (start == *r) \
goto nuke_it##B; \
*--start = '\0'; \
} else \
goto eat_this_path##B; \
} \
if (!end) \
break; \
start = end + 1; \
} \
if (**r == '\0') \
goto nuke_it##B; \
} \
if (*r) \
*found_rpath = 1; \
} \
} \
++dyn; \
} \
} }
SHOW_RPATH(32)
SHOW_RPATH(64)
}
if (be_wewy_wewy_quiet) return;
if (rpath && runpath) {
if (!strcmp(rpath, runpath)) {
xstrcat(ret, runpath, ret_len);
} else {
fprintf(stderr, "RPATH [%s] != RUNPATH [%s]\n", rpath, runpath);
xchrcat(ret, '{', ret_len);
xstrcat(ret, rpath, ret_len);
xchrcat(ret, ',', ret_len);
xstrcat(ret, runpath, ret_len);
xchrcat(ret, '}', ret_len);
}
} else if (rpath || runpath)
xstrcat(ret, (runpath ? runpath : rpath), ret_len);
else if (!be_quiet)
xstrcat(ret, " - ", ret_len);
}
#define LDSO_CACHE_MAGIC "ld.so-"
#define LDSO_CACHE_MAGIC_LEN (sizeof LDSO_CACHE_MAGIC -1)
#define LDSO_CACHE_VER "1.7.0"
#define LDSO_CACHE_VER_LEN (sizeof LDSO_CACHE_VER -1)
#define FLAG_ANY -1
#define FLAG_TYPE_MASK 0x00ff
#define FLAG_LIBC4 0x0000
#define FLAG_ELF 0x0001
#define FLAG_ELF_LIBC5 0x0002
#define FLAG_ELF_LIBC6 0x0003
#define FLAG_REQUIRED_MASK 0xff00
#define FLAG_SPARC_LIB64 0x0100
#define FLAG_IA64_LIB64 0x0200
#define FLAG_X8664_LIB64 0x0300
#define FLAG_S390_LIB64 0x0400
#define FLAG_POWERPC_LIB64 0x0500
#define FLAG_MIPS64_LIBN32 0x0600
#define FLAG_MIPS64_LIBN64 0x0700
static char *lookup_cache_lib(elfobj *, char *);
#if defined(__GLIBC__) || defined(__UCLIBC__)
static char *lookup_cache_lib(elfobj *elf, char *fname)
{
int fd;
char *strs;
static char buf[__PAX_UTILS_PATH_MAX] = "";
const char cachefile[] = "/etc/ld.so.cache";
struct stat st;
typedef struct {
char magic[LDSO_CACHE_MAGIC_LEN];
char version[LDSO_CACHE_VER_LEN];
int nlibs;
} header_t;
header_t *header;
typedef struct {
int flags;
int sooffset;
int liboffset;
} libentry_t;
libentry_t *libent;
if (fname == NULL)
return NULL;
if (ldcache == NULL) {
if (stat(cachefile, &st))
return NULL;
fd = open(cachefile, O_RDONLY);
if (fd == -1)
return NULL;
/* cache these values so we only map/unmap the cache file once */
ldcache_size = st.st_size;
header = ldcache = mmap(0, ldcache_size, PROT_READ, MAP_SHARED, fd, 0);
close(fd);
if (ldcache == MAP_FAILED) {
ldcache = NULL;
return NULL;
}
if (memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN) ||
memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN))
{
munmap(ldcache, ldcache_size);
ldcache = NULL;
return NULL;
}
} else
header = ldcache;
libent = ldcache + sizeof(header_t);
strs = (char *) &libent[header->nlibs];
for (fd = 0; fd < header->nlibs; ++fd) {
/* This should be more fine grained, but for now we assume that
* diff arches will not be cached together, and we ignore the
* the different multilib mips cases.
*/
if (elf->elf_class == ELFCLASS64 && !(libent[fd].flags & FLAG_REQUIRED_MASK))
continue;
if (elf->elf_class == ELFCLASS32 && (libent[fd].flags & FLAG_REQUIRED_MASK))
continue;
if (strcmp(fname, strs + libent[fd].sooffset) != 0)
continue;
/* Return first hit because that is how the ldso rolls */
strncpy(buf, strs + libent[fd].liboffset, sizeof(buf));
break;
}
return buf;
}
#elif defined(__NetBSD__)
static char *lookup_cache_lib(elfobj *elf, char *fname)
{
static char buf[__PAX_UTILS_PATH_MAX] = "";
static struct stat st;
char **ldpath;
for (ldpath = ldpaths; *ldpath != NULL; ldpath++) {
if ((unsigned) snprintf(buf, sizeof(buf), "%s/%s", *ldpath, fname) >= sizeof(buf))
continue; /* if the pathname is too long, or something went wrong, ignore */
if (stat(buf, &st) != 0)
continue; /* if the lib doesn't exist in *ldpath, look further */
/* NetBSD doesn't actually do sanity checks, it just loads the file
* and if that doesn't work, continues looking in other directories.
* This cannot easily be safely emulated, unfortunately. For now,
* just assume that if it exists, it's a valid library. */
return buf;
}
/* not found in any path */
return NULL;
}
#else
#ifdef __ELF__
#warning Cache support not implemented for your target
#endif
static char *lookup_cache_lib(elfobj *elf, char *fname)
{
return NULL;
}
#endif
static const char *scanelf_file_needed_lib(elfobj *elf, char *found_needed, char *found_lib, int op, char **ret, size_t *ret_len)
{
unsigned long i;
char *needed;
void *strtbl_void;
char *p;
if ((op==0 && !show_needed) || (op==1 && !find_lib)) return NULL;
strtbl_void = elf_findsecbyname(elf, ".dynstr");
if (elf->phdr && strtbl_void) {
#define SHOW_NEEDED(B) \
if (elf->elf_class == ELFCLASS ## B) { \
Elf ## B ## _Dyn *dyn; \
Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
Elf ## B ## _Shdr *strtbl = SHDR ## B (strtbl_void); \
Elf ## B ## _Off offset; \
for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
if (EGET(phdr[i].p_type) != PT_DYNAMIC || EGET(phdr[i].p_filesz) == 0) continue; \
offset = EGET(phdr[i].p_offset); \
if (offset >= elf->len - sizeof(Elf ## B ## _Dyn)) continue; \
dyn = DYN ## B (elf->vdata + offset); \
while (EGET(dyn->d_tag) != DT_NULL) { \
if (EGET(dyn->d_tag) == DT_NEEDED) { \
offset = EGET(strtbl->sh_offset) + EGET(dyn->d_un.d_ptr); \
if (offset >= (Elf ## B ## _Off)elf->len) { \
++dyn; \
continue; \
} \
needed = elf->data + offset; \
if (op == 0) { \
if (!be_wewy_wewy_quiet) { \
if (*found_needed) xchrcat(ret, ',', ret_len); \
if (use_ldcache) \
if ((p = lookup_cache_lib(elf, needed)) != NULL) \
needed = p; \
xstrcat(ret, needed, ret_len); \
} \
*found_needed = 1; \
} else { \
if (!strncmp(find_lib, needed, strlen( !g_match ? needed : find_lib))) { \
*found_lib = 1; \
return (be_wewy_wewy_quiet ? NULL : needed); \
} \
} \
} \
++dyn; \
} \
} }
SHOW_NEEDED(32)
SHOW_NEEDED(64)
if (op == 0 && !*found_needed && be_verbose)
warn("ELF lacks DT_NEEDED sections: %s", elf->filename);
}
return NULL;
}
static char *scanelf_file_interp(elfobj *elf, char *found_interp)
{
void *strtbl_void;
if (!show_interp) return NULL;
strtbl_void = elf_findsecbyname(elf, ".interp");
if (strtbl_void) {
#define SHOW_INTERP(B) \
if (elf->elf_class == ELFCLASS ## B) { \
Elf ## B ## _Shdr *strtbl = SHDR ## B (strtbl_void); \
*found_interp = 1; \
return (be_wewy_wewy_quiet ? NULL : elf->data + EGET(strtbl->sh_offset)); \
}
SHOW_INTERP(32)
SHOW_INTERP(64)
}
return NULL;
}
static char *scanelf_file_bind(elfobj *elf, char *found_bind)
{
unsigned long i;
struct stat s;
char dynamic = 0;
if (!show_bind) return NULL;
if (!elf->phdr) return NULL;
#define SHOW_BIND(B) \
if (elf->elf_class == ELFCLASS ## B) { \
Elf ## B ## _Dyn *dyn; \
Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
Elf ## B ## _Off offset; \
for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
if (EGET(phdr[i].p_type) != PT_DYNAMIC || EGET(phdr[i].p_filesz) == 0) continue; \
dynamic = 1; \
offset = EGET(phdr[i].p_offset); \
if (offset >= elf->len - sizeof(Elf ## B ## _Dyn)) continue; \
dyn = DYN ## B (elf->vdata + offset); \
while (EGET(dyn->d_tag) != DT_NULL) { \
if (EGET(dyn->d_tag) == DT_BIND_NOW || \
(EGET(dyn->d_tag) == DT_FLAGS && EGET(dyn->d_un.d_val) & DF_BIND_NOW)) \
{ \
if (be_quiet) return NULL; \
*found_bind = 1; \
return (char *)(be_wewy_wewy_quiet ? NULL : "NOW"); \
} \
++dyn; \
} \
} \
}
SHOW_BIND(32)
SHOW_BIND(64)
if (be_wewy_wewy_quiet) return NULL;
/* don't output anything if quiet mode and the ELF is static or not setuid */
if (be_quiet && (!dynamic || (!fstat(elf->fd, &s) && !(s.st_mode & (S_ISUID|S_ISGID))))) {
return NULL;
} else {
*found_bind = 1;
return (char *)(dynamic ? "LAZY" : "STATIC");
}
}
static char *scanelf_file_soname(elfobj *elf, char *found_soname)
{
unsigned long i;
char *soname;
void *strtbl_void;
if (!show_soname) return NULL;
strtbl_void = elf_findsecbyname(elf, ".dynstr");
if (elf->phdr && strtbl_void) {
#define SHOW_SONAME(B) \
if (elf->elf_class == ELFCLASS ## B) { \
Elf ## B ## _Dyn *dyn; \
Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
Elf ## B ## _Shdr *strtbl = SHDR ## B (strtbl_void); \
Elf ## B ## _Off offset; \
/* only look for soname in shared objects */ \
if (EGET(ehdr->e_type) != ET_DYN) \
return NULL; \
for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
if (EGET(phdr[i].p_type) != PT_DYNAMIC || EGET(phdr[i].p_filesz) == 0) continue; \
offset = EGET(phdr[i].p_offset); \
if (offset >= elf->len - sizeof(Elf ## B ## _Dyn)) continue; \
dyn = DYN ## B (elf->vdata + offset); \
while (EGET(dyn->d_tag) != DT_NULL) { \
if (EGET(dyn->d_tag) == DT_SONAME) { \
offset = EGET(strtbl->sh_offset) + EGET(dyn->d_un.d_ptr); \
if (offset >= (Elf ## B ## _Off)elf->len) { \
++dyn; \
continue; \
} \
soname = elf->data + offset; \
*found_soname = 1; \
return (be_wewy_wewy_quiet ? NULL : soname); \
} \
++dyn; \
} \
} }
SHOW_SONAME(32)
SHOW_SONAME(64)
}
return NULL;
}
/*
* We support the symbol form:
* [%[modifiers]%][[+-]<symbol name>][,[.....]]
* If the symbol name is empty, then all symbols are matched.
* If the symbol name is a glob ("*"), then all symbols are dumped (debug).
* Do not rely on this output format at all.
* Otherwise the symbol name is used to search (either regex or string compare).
* If the first char of the symbol name is a plus ("+"), then only match
* defined symbols. If it's a minus ("-"), only match undefined symbols.
* Putting modifiers in between the percent signs allows for more in depth
* filters. There are groups of modifiers. If you don't specify a member
* of a group, then all types in that group are matched. The current
* groups and their types are:
* STT group: STT_NOTYPE:n STT_OBJECT:o STT_FUNC:f SST_FILE:F
* STB group: STB_LOCAL:l STB_GLOBAL:g STB_WEAK:w
* SHN group: SHN_UNDEF:u SHN_ABS:a SHN_COMMON:c {defined}:d
* The "defined" value in the SHN group does not correspond to a SHN_xxx define.
* You can search for multiple symbols at once by seperating with a comma (",").
*
* Some examples: