/[gentoo-projects]/pax-utils/scanelf.c
Gentoo

Contents of /pax-utils/scanelf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.38 - (show annotations) (download) (as text)
Tue Apr 19 22:19:26 2005 UTC (9 years, 5 months ago) by vapier
Branch: MAIN
Changes since 1.37: +31 -6 lines
File MIME type: text/x-csrc
add support for displaying an ELFs interp section

1 /*
2 * Copyright 2003 Ned Ludd <solar@gentoo.org>
3 * Copyright 1999-2005 Gentoo Foundation
4 * Distributed under the terms of the GNU General Public License v2
5 * $Header: /var/cvsroot/gentoo-projects/pax-utils/scanelf.c,v 1.37 2005/04/16 06:34:39 vapier Exp $
6 *
7 ********************************************************************
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21 * MA 02111-1307, USA.
22 */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #define __USE_GNU
28 #include <string.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sys/stat.h>
32 #include <dirent.h>
33 #include <getopt.h>
34 #include <assert.h>
35
36 #include "paxelf.h"
37
38 static const char *rcsid = "$Id: scanelf.c,v 1.37 2005/04/16 06:34:39 vapier Exp $";
39 #define argv0 "scanelf"
40
41
42
43 /* prototypes */
44 static void scanelf_file(const char *filename);
45 static void scanelf_dir(const char *path);
46 static void scanelf_ldpath();
47 static void scanelf_envpath();
48 static void usage(int status);
49 static void parseargs(int argc, char *argv[]);
50
51 /* variables to control behavior */
52 static char scan_ldpath = 0;
53 static char scan_envpath = 0;
54 static char scan_symlink = 1;
55 static char dir_recurse = 0;
56 static char dir_crossmount = 1;
57 static char show_pax = 0;
58 static char show_stack = 0;
59 static char show_textrel = 0;
60 static char show_rpath = 0;
61 static char show_needed = 0;
62 static char show_interp = 0;
63 static char show_banner = 1;
64 static char be_quiet = 0;
65 static char be_verbose = 0;
66 static char *find_sym = NULL;
67
68
69
70 /* scan an elf file and show all the fun stuff */
71 static void scanelf_file(const char *filename)
72 {
73 int i;
74 char found_pax, found_stack, found_relro, found_textrel,
75 found_rpath, found_needed, found_interp, found_sym;
76 elfobj *elf;
77 struct stat st;
78
79 /* make sure 'filename' exists */
80 if (lstat(filename, &st) == -1)
81 return;
82 /* always handle regular files and handle symlinked files if no -y */
83 if (!(S_ISREG(st.st_mode) || (S_ISLNK(st.st_mode) && scan_symlink)))
84 return;
85
86 found_pax = found_stack = found_relro = found_textrel = \
87 found_rpath = found_needed = found_interp = found_sym = 0;
88
89 /* verify this is real ELF */
90 if ((elf = readelf(filename)) == NULL) {
91 if (be_verbose > 2) printf("%s: not an ELF\n", filename);
92 return;
93 }
94
95 if (be_verbose > 1)
96 printf("%s: {%s,%s} scanning file\n", filename,
97 get_elfeitype(elf, EI_CLASS, elf->elf_class),
98 get_elfeitype(elf, EI_DATA, elf->data[EI_DATA]));
99 else if (be_verbose)
100 printf("%s: scanning file\n", filename);
101
102 /* show the header */
103 if (!be_quiet && show_banner) {
104 printf(" TYPE ");
105 if (show_pax) printf(" PAX ");
106 if (show_stack) printf("STK/REL ");
107 if (show_textrel) printf("TEXTREL ");
108 if (show_rpath) printf("RPATH ");
109 if (show_needed) printf("NEEDED ");
110 if (show_interp) printf("INTERP ");
111 printf(" FILE\n");
112 show_banner = 0;
113 }
114
115 /* dump all the good stuff */
116 if (!be_quiet)
117 printf("%-7s ", get_elfetype(elf));
118
119 if (show_pax) {
120 char *paxflags = pax_short_hf_flags(PAX_FLAGS(elf));
121 if (!be_quiet || (be_quiet && strncmp(paxflags, "PeMRxS", 6))) {
122 found_pax = 1;
123 printf("%s ", pax_short_hf_flags(PAX_FLAGS(elf)));
124 }
125 }
126
127 /* stack fun */
128 if (show_stack) {
129 #define SHOW_STACK(B) \
130 if (elf->elf_class == ELFCLASS ## B) { \
131 Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
132 Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
133 for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
134 if (EGET(phdr[i].p_type) != PT_GNU_STACK && \
135 EGET(phdr[i].p_type) != PT_GNU_RELRO) continue; \
136 if (be_quiet && !(EGET(phdr[i].p_flags) & PF_X)) \
137 continue; \
138 if (EGET(phdr[i].p_type) == PT_GNU_STACK) \
139 found_stack = 1; \
140 if (EGET(phdr[i].p_type) == PT_GNU_RELRO) \
141 found_relro = 1; \
142 printf("%s ", gnu_short_stack_flags(EGET(phdr[i].p_flags))); \
143 } \
144 }
145 SHOW_STACK(32)
146 SHOW_STACK(64)
147 if (!be_quiet && !found_stack) printf("--- ");
148 if (!be_quiet && !found_relro) printf("--- ");
149 }
150
151 /* textrel fun */
152 if (show_textrel) {
153 #define SHOW_TEXTREL(B) \
154 if (elf->elf_class == ELFCLASS ## B) { \
155 Elf ## B ## _Dyn *dyn; \
156 Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
157 Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
158 for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
159 if (phdr[i].p_type != PT_DYNAMIC) continue; \
160 dyn = DYN ## B (elf->data + EGET(phdr[i].p_offset)); \
161 while (EGET(dyn->d_tag) != DT_NULL) { \
162 if (EGET(dyn->d_tag) == DT_TEXTREL) { /*dyn->d_tag != DT_FLAGS)*/ \
163 found_textrel = 1; \
164 /*if (dyn->d_un.d_val & DF_TEXTREL)*/ \
165 fputs("TEXTREL ", stdout); \
166 } \
167 ++dyn; \
168 } \
169 } }
170 SHOW_TEXTREL(32)
171 SHOW_TEXTREL(64)
172 if (!be_quiet && !found_textrel) fputs("------- ", stdout);
173 }
174
175 /* rpath fun */
176 /* TODO: if be_quiet, only output RPATH's which aren't in /etc/ld.so.conf */
177 if (show_rpath) {
178 char *rpath, *runpath;
179 void *strtbl_void = elf_findsecbyname(elf, ".dynstr");
180 rpath = runpath = NULL;
181
182 if (strtbl_void) {
183 #define SHOW_RPATH(B) \
184 if (elf->elf_class == ELFCLASS ## B) { \
185 Elf ## B ## _Dyn *dyn; \
186 Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
187 Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
188 Elf ## B ## _Shdr *strtbl = SHDR ## B (strtbl_void); \
189 for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
190 if (EGET(phdr[i].p_type) != PT_DYNAMIC) continue; \
191 dyn = DYN ## B (elf->data + EGET(phdr[i].p_offset)); \
192 while (EGET(dyn->d_tag) != DT_NULL) { \
193 if (EGET(dyn->d_tag) == DT_RPATH) { \
194 rpath = elf->data + EGET(strtbl->sh_offset) + EGET(dyn->d_un.d_ptr); \
195 found_rpath = 1; \
196 } else if (EGET(dyn->d_tag) == DT_RUNPATH) { \
197 runpath = elf->data + EGET(strtbl->sh_offset) + EGET(dyn->d_un.d_ptr); \
198 found_rpath = 1; \
199 } \
200 ++dyn; \
201 } \
202 } }
203 SHOW_RPATH(32)
204 SHOW_RPATH(64)
205 }
206 if (rpath && runpath) {
207 if (!strcmp(rpath, runpath))
208 printf("%-5s ", runpath);
209 else {
210 fprintf(stderr, "%s's RPATH [%s] != RUNPATH [%s]\n", filename, rpath, runpath);
211 printf("{%s,%s} ", rpath, runpath);
212 }
213 } else if (rpath || runpath)
214 printf("%-5s ", (runpath ? runpath : rpath));
215 else if (!be_quiet && !found_rpath)
216 printf(" - ");
217 }
218
219 /* print out all the NEEDED entries */
220 if (show_needed) {
221 char *needed;
222 void *strtbl_void = elf_findsecbyname(elf, ".dynstr");
223
224 if (strtbl_void) {
225 #define SHOW_NEEDED(B) \
226 if (elf->elf_class == ELFCLASS ## B) { \
227 Elf ## B ## _Dyn *dyn; \
228 Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
229 Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
230 Elf ## B ## _Shdr *strtbl = SHDR ## B (strtbl_void); \
231 for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
232 if (be_verbose && EGET(phdr[i].p_type) == PT_INTERP) { \
233 dyn = DYN ## B (elf->data + EGET(phdr[i].p_offset)); \
234 printf("%s\n", elf->data + EGET(strtbl->sh_offset) + EGET(dyn->d_un.d_ptr)); \
235 exit(0); \
236 } \
237 if (EGET(phdr[i].p_type) != PT_DYNAMIC) continue; \
238 dyn = DYN ## B (elf->data + EGET(phdr[i].p_offset)); \
239 while (EGET(dyn->d_tag) != DT_NULL) { \
240 if (EGET(dyn->d_tag) == DT_NEEDED) { \
241 needed = elf->data + EGET(strtbl->sh_offset) + EGET(dyn->d_un.d_ptr); \
242 if (found_needed) printf(","); \
243 printf("%s", needed); \
244 found_needed = 1; \
245 } \
246 ++dyn; \
247 } \
248 } }
249 SHOW_NEEDED(32)
250 SHOW_NEEDED(64)
251 }
252 if (!be_quiet && !found_needed)
253 printf(" - ");
254 else if (found_needed)
255 printf(" ");
256 }
257
258 /* print out all the INTERP info (i.e. glibc is /lib/ld-linux.so.2) */
259 if (show_interp) {
260 void *strtbl_void = elf_findsecbyname(elf, ".interp");
261
262 if (strtbl_void) {
263 #define SHOW_INTERP(B) \
264 if (elf->elf_class == ELFCLASS ## B) { \
265 Elf ## B ## _Shdr *strtbl = SHDR ## B (strtbl_void); \
266 printf("%s ", elf->data + EGET(strtbl->sh_offset)); \
267 found_interp = 1; \
268 }
269 SHOW_INTERP(32)
270 SHOW_INTERP(64)
271 }
272 if (!be_quiet && !found_interp)
273 printf(" - ");
274 else if (found_interp)
275 printf(" ");
276 }
277
278 /* search the symbol table for a specified symbol */
279 if (find_sym) {
280 void *symtab_void, *strtab_void;
281 char *versioned_symname;
282 size_t len;
283
284 len = strlen(find_sym) + 1;
285 versioned_symname = (char *)malloc(sizeof(char) * (len+1));
286 if (!versioned_symname) {
287 warnf("Could not malloc() mem for sym scan");
288 return;
289 }
290 sprintf(versioned_symname, "%s@", find_sym);
291
292 symtab_void = elf_findsecbyname(elf, ".symtab");
293 strtab_void = elf_findsecbyname(elf, ".strtab");
294
295 if (symtab_void && strtab_void) {
296 #define FIND_SYM(B) \
297 if (elf->elf_class == ELFCLASS ## B) { \
298 Elf ## B ## _Shdr *symtab = SHDR ## B (symtab_void); \
299 Elf ## B ## _Shdr *strtab = SHDR ## B (strtab_void); \
300 Elf ## B ## _Sym *sym = SYM ## B (elf->data + EGET(symtab->sh_offset)); \
301 int cnt = EGET(symtab->sh_size) / EGET(symtab->sh_entsize); \
302 char *symname; \
303 for (i = 0; i < cnt; ++i) { \
304 if (sym->st_name) { \
305 symname = (char *)(elf->data + EGET(strtab->sh_offset) + EGET(sym->st_name)); \
306 if (*find_sym == '*') { \
307 printf("%s(%s) %5lX %15s %s\n", \
308 ((found_sym == 0) ? "\n\t" : "\t"), \
309 (char *)basename(filename), \
310 (long)sym->st_size, \
311 (char *)get_elfstttype(sym->st_info), \
312 symname); \
313 found_sym = 1; \
314 } else if ((strcmp(find_sym, symname) == 0) || \
315 (strncmp(symname, versioned_symname, len) == 0)) \
316 found_sym++; \
317 } \
318 ++sym; \
319 } }
320 FIND_SYM(32)
321 FIND_SYM(64)
322 }
323 free(versioned_symname);
324 if (*find_sym != '*') {
325 if (found_sym)
326 printf(" %s ", find_sym);
327 else if (!be_quiet)
328 fputs(" - ", stdout);
329 }
330 }
331
332 if (!be_quiet || found_pax || found_stack || found_textrel || \
333 found_rpath || found_needed || found_sym)
334 puts(filename);
335
336 unreadelf(elf);
337 }
338
339 /* scan a directory for ET_EXEC files and print when we find one */
340 static void scanelf_dir(const char *path)
341 {
342 register DIR *dir;
343 register struct dirent *dentry;
344 struct stat st_top, st;
345 char buf[_POSIX_PATH_MAX];
346 size_t pathlen = 0, len = 0;
347
348 /* make sure path exists */
349 if (lstat(path, &st_top) == -1)
350 return;
351
352 /* ok, if it isn't a directory, assume we can open it */
353 if (!S_ISDIR(st_top.st_mode)) {
354 scanelf_file(path);
355 return;
356 }
357
358 /* now scan the dir looking for fun stuff */
359 if ((dir = opendir(path)) == NULL) {
360 warnf("could not opendir %s: %s", path, strerror(errno));
361 return;
362 }
363 if (be_verbose) printf("%s: scanning dir\n", path);
364
365 pathlen = strlen(path);
366 while ((dentry = readdir(dir))) {
367 if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, ".."))
368 continue;
369 len = (pathlen + 1 + strlen(dentry->d_name) + 1);
370 if (len >= sizeof(buf)) {
371 warnf("Skipping '%s': len > sizeof(buf); %d > %d\n", path, (int)len, (int)sizeof(buf));
372 continue;
373 }
374 sprintf(buf, "%s/%s", path, dentry->d_name);
375 if (lstat(buf, &st) != -1) {
376 if (S_ISREG(st.st_mode))
377 scanelf_file(buf);
378 else if (dir_recurse && S_ISDIR(st.st_mode)) {
379 if (dir_crossmount || (st_top.st_dev == st.st_dev))
380 scanelf_dir(buf);
381 }
382 }
383 }
384 closedir(dir);
385 }
386
387 /* scan /etc/ld.so.conf for paths */
388 static void scanelf_ldpath()
389 {
390 char scan_l, scan_ul, scan_ull;
391 char *path, *p;
392 FILE *fp;
393
394 if ((fp = fopen("/etc/ld.so.conf", "r")) == NULL)
395 err("Unable to open ld.so.conf: %s", strerror(errno));
396
397 scan_l = scan_ul = scan_ull = 0;
398
399 if ((path = malloc(_POSIX_PATH_MAX)) == NULL) {
400 warn("Can not malloc() memory for ldpath scanning");
401 return;
402 }
403 while ((fgets(path, _POSIX_PATH_MAX, fp)) != NULL)
404 if (*path == '/') {
405 if ((p = strrchr(path, '\r')) != NULL)
406 *p = 0;
407 if ((p = strrchr(path, '\n')) != NULL)
408 *p = 0;
409 if (!scan_l && !strcmp(path, "/lib")) scan_l = 1;
410 if (!scan_ul && !strcmp(path, "/usr/lib")) scan_ul = 1;
411 if (!scan_ull && !strcmp(path, "/usr/local/lib")) scan_ull = 1;
412 scanelf_dir(path);
413 }
414 free(path);
415 fclose(fp);
416
417 if (!scan_l) scanelf_dir("/lib");
418 if (!scan_ul) scanelf_dir("/usr/lib");
419 if (!scan_ull) scanelf_dir("/usr/local/lib");
420 }
421
422 /* scan env PATH for paths */
423 static void scanelf_envpath()
424 {
425 char *path, *p;
426
427 path = getenv("PATH");
428 if (!path)
429 err("PATH is not set in your env !");
430
431 if ((path = strdup(path)) == NULL)
432 err("strdup failed: %s", strerror(errno));
433
434 while ((p = strrchr(path, ':')) != NULL) {
435 scanelf_dir(p + 1);
436 *p = 0;
437 }
438
439 free(path);
440 }
441
442
443
444 /* usage / invocation handling functions */
445 #define PARSE_FLAGS "plRmyxetrnis:aqvo:BhV"
446 #define a_argument required_argument
447 static struct option const long_opts[] = {
448 {"path", no_argument, NULL, 'p'},
449 {"ldpath", no_argument, NULL, 'l'},
450 {"recursive", no_argument, NULL, 'R'},
451 {"mount", no_argument, NULL, 'm'},
452 {"symlink", no_argument, NULL, 'y'},
453 {"pax", no_argument, NULL, 'x'},
454 {"header", no_argument, NULL, 'e'},
455 {"textrel", no_argument, NULL, 't'},
456 {"rpath", no_argument, NULL, 'r'},
457 {"needed", no_argument, NULL, 'n'},
458 {"interp", no_argument, NULL, 'i'},
459 {"symbol", a_argument, NULL, 's'},
460 {"all", no_argument, NULL, 'a'},
461 {"quiet", no_argument, NULL, 'q'},
462 {"verbose", no_argument, NULL, 'v'},
463 {"file", a_argument, NULL, 'o'},
464 {"nobanner", no_argument, NULL, 'B'},
465 {"help", no_argument, NULL, 'h'},
466 {"version", no_argument, NULL, 'V'},
467 {NULL, no_argument, NULL, 0x0}
468 };
469 static char *opts_help[] = {
470 "Scan all directories in PATH environment",
471 "Scan all directories in /etc/ld.so.conf",
472 "Scan directories recursively",
473 "Don't recursively cross mount points",
474 "Don't scan symlinks\n",
475 "Print PaX markings",
476 "Print GNU_STACK markings",
477 "Print TEXTREL information",
478 "Print RPATH information",
479 "Print NEEDED information",
480 "Print INTERP information",
481 "Find a specified symbol",
482 "Print all scanned info (-x -e -t -r)\n",
483 "Only output 'bad' things",
484 "Be verbose (can be specified more than once)",
485 "Write output stream to a filename",
486 "Don't display the header",
487 "Print this help and exit",
488 "Print version and exit",
489 NULL
490 };
491
492 /* display usage and exit */
493 static void usage(int status)
494 {
495 int i;
496 printf(" Scan ELF binaries for stuff\n\n"
497 "Usage: %s [options] <dir1/file1> [dir2 dirN fileN ...]\n\n", argv0);
498 printf("Options: -[%s]\n", PARSE_FLAGS);
499 for (i = 0; long_opts[i].name; ++i)
500 if (long_opts[i].has_arg == no_argument)
501 printf(" -%c, --%-13s %s\n", long_opts[i].val,
502 long_opts[i].name, opts_help[i]);
503 else
504 printf(" -%c, --%-6s <arg> %s\n", long_opts[i].val,
505 long_opts[i].name, opts_help[i]);
506 exit(status);
507 }
508
509 /* parse command line arguments and preform needed actions */
510 static void parseargs(int argc, char *argv[])
511 {
512 int flag;
513
514 opterr = 0;
515 while ((flag=getopt_long(argc, argv, PARSE_FLAGS, long_opts, NULL)) != -1) {
516 switch (flag) {
517
518 case 'V': /* version info */
519 printf("%s compiled %s\n%s\n"
520 "%s written for Gentoo Linux by <solar and vapier @ gentoo.org>\n",
521 __FILE__, __DATE__, rcsid, argv0);
522 exit(EXIT_SUCCESS);
523 break;
524 case 'h': usage(EXIT_SUCCESS); break;
525
526 case 'o': {
527 FILE *fp = NULL;
528 fp = freopen(optarg, "w", stdout);
529 if (fp == NULL)
530 err("Could not open output stream '%s': %s", optarg, strerror(errno));
531 stdout = fp;
532 break;
533 }
534
535 case 's': find_sym = strdup(optarg); break;
536
537 case 'y': scan_symlink = 0; break;
538 case 'B': show_banner = 0; break;
539 case 'l': scan_ldpath = 1; break;
540 case 'p': scan_envpath = 1; break;
541 case 'R': dir_recurse = 1; break;
542 case 'm': dir_crossmount = 0; break;
543 case 'x': show_pax = 1; break;
544 case 'e': show_stack = 1; break;
545 case 't': show_textrel = 1; break;
546 case 'r': show_rpath = 1; break;
547 case 'n': show_needed = 1; break;
548 case 'i': show_interp = 1; break;
549 case 'q': be_quiet = 1; break;
550 case 'v': be_verbose = (be_verbose % 20) + 1; break;
551 case 'a': show_pax = show_stack = show_textrel = show_rpath = show_needed = show_interp = 1; break;
552
553 case ':':
554 warn("Option missing parameter");
555 usage(EXIT_FAILURE);
556 break;
557 case '?':
558 warn("Unknown option");
559 usage(EXIT_FAILURE);
560 break;
561 default:
562 err("Unhandled option '%c'", flag);
563 break;
564 }
565 }
566
567 if (be_quiet && be_verbose)
568 err("You can be quiet or you can be verbose, not both, stupid");
569
570 if (scan_ldpath) scanelf_ldpath();
571 if (scan_envpath) scanelf_envpath();
572 if (optind == argc && !scan_ldpath && !scan_envpath)
573 err("Nothing to scan !?");
574 while (optind < argc)
575 scanelf_dir(argv[optind++]);
576
577 if (find_sym) free(find_sym);
578 }
579
580
581
582 int main(int argc, char *argv[])
583 {
584 if (argc < 2)
585 usage(EXIT_FAILURE);
586 parseargs(argc, argv);
587 fclose(stdout);
588 return EXIT_SUCCESS;
589 }

  ViewVC Help
Powered by ViewVC 1.1.20