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

Contents of /pax-utils/scanelf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.37 - (show annotations) (download) (as text)
Sat Apr 16 06:34:39 2005 UTC (10 years, 2 months ago) by vapier
Branch: MAIN
Changes since 1.36: +13 -7 lines
File MIME type: text/x-csrc
add a -y option to handle whether we scan symlinked files

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

  ViewVC Help
Powered by ViewVC 1.1.20