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

Contents of /pax-utils/scanelf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.36 - (show annotations) (download) (as text)
Fri Apr 15 22:02:03 2005 UTC (9 years, 8 months ago) by vapier
Branch: MAIN
Changes since 1.35: +3 -14 lines
File MIME type: text/x-csrc
move common warn/warnf/err macros to paxelf.h

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

  ViewVC Help
Powered by ViewVC 1.1.20