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

Contents of /pax-utils/scanelf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.27 - (hide annotations) (download) (as text)
Tue Apr 5 01:44:08 2005 UTC (9 years, 5 months ago) by vapier
Branch: MAIN
Changes since 1.26: +52 -14 lines
File MIME type: text/x-csrc
add symbol scanning support to scanelf

1 solar 1.1 /*
2     * Copyright 2003 Ned Ludd <solar@gentoo.org>
3 vapier 1.8 * Copyright 1999-2005 Gentoo Foundation
4 solar 1.1 * Distributed under the terms of the GNU General Public License v2
5 vapier 1.26 * $Header: /var/cvsroot/gentoo-projects/pax-utils/scanelf.c,v 1.25 2005/04/03 18:56:08 vapier Exp $
6 solar 1.1 *
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     #include <string.h>
28 vapier 1.10 #include <errno.h>
29 solar 1.1 #include <unistd.h>
30     #include <sys/stat.h>
31     #include <dirent.h>
32     #include <getopt.h>
33 solar 1.20 #include <assert.h>
34 solar 1.21
35 solar 1.1 #include "paxelf.h"
36    
37 vapier 1.26 static const char *rcsid = "$Id: scanelf.c,v 1.25 2005/04/03 18:56:08 vapier Exp $";
38 vapier 1.10
39    
40     /* helper functions for showing errors */
41 vapier 1.18 #define argv0 "scanelf" /*((*argv != NULL) ? argv[0] : __FILE__ "\b\b")*/
42 vapier 1.10 #define warn(fmt, args...) \
43     fprintf(stderr, "%s: " fmt "\n", argv0, ## args)
44     #define warnf(fmt, args...) warn("%s(): " fmt, __FUNCTION__, ## args)
45     #define err(fmt, args...) \
46     do { \
47     warn(fmt, ## args); \
48     exit(EXIT_FAILURE); \
49     } while (0)
50    
51    
52    
53     /* prototypes */
54     static void scanelf_file(const char *filename);
55     static void scanelf_dir(const char *path);
56     static void scanelf_ldpath();
57     static void scanelf_envpath();
58     static void usage(int status);
59     static void parseargs(int argc, char *argv[]);
60    
61     /* variables to control behavior */
62     static char scan_ldpath = 0;
63     static char scan_envpath = 0;
64     static char dir_recurse = 0;
65 vapier 1.14 static char dir_crossmount = 1;
66 vapier 1.10 static char show_pax = 0;
67     static char show_stack = 0;
68     static char show_textrel = 0;
69     static char show_rpath = 0;
70 solar 1.16 static char show_banner = 1;
71 vapier 1.10 static char be_quiet = 0;
72 vapier 1.14 static char be_verbose = 0;
73 vapier 1.27 static char *find_sym = NULL;
74 vapier 1.10
75 solar 1.1
76    
77 vapier 1.10 /* scan an elf file and show all the fun stuff */
78     static void scanelf_file(const char *filename)
79 solar 1.6 {
80 vapier 1.10 int i;
81 vapier 1.27 char found_pax, found_stack, found_relro, found_textrel, found_rpath, found_sym;
82 vapier 1.26 elfobj *elf;
83 vapier 1.10
84 vapier 1.27 found_pax = found_stack = found_relro = found_textrel = found_rpath = found_sym = 0;
85 solar 1.12
86 vapier 1.10 /* verify this is real ELF */
87 vapier 1.14 if ((elf = readelf(filename)) == NULL) {
88 vapier 1.26 if (be_verbose > 2) printf("%s: not an ELF\n", filename);
89 vapier 1.10 return;
90 vapier 1.14 }
91    
92 vapier 1.26 if (be_verbose > 1)
93     printf("%s: {%s,%s} scanning file\n", filename,
94     get_elfeitype(elf, EI_CLASS, elf->elf_class),
95     get_elfeitype(elf, EI_DATA, elf->data[EI_DATA]));
96     else if (be_verbose)
97     printf("%s: scanning file\n", filename);
98 vapier 1.10
99     /* show the header */
100 solar 1.16 if (!be_quiet && show_banner) {
101 vapier 1.23 printf(" TYPE ");
102     if (show_pax) printf(" PAX ");
103     if (show_stack) printf(" STK/REL ");
104     if (show_textrel) printf("TEXTREL ");
105     if (show_rpath) printf("RPATH ");
106     printf(" FILE\n");
107 solar 1.16 show_banner = 0;
108 vapier 1.10 }
109    
110     /* dump all the good stuff */
111     if (!be_quiet)
112 vapier 1.26 printf("%-7s ", get_elfetype(elf));
113 vapier 1.10
114 vapier 1.14 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 vapier 1.10
122     /* stack fun */
123     if (show_stack) {
124 vapier 1.26 #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 vapier 1.10 }
140 vapier 1.26 SHOW_STACK(32)
141     SHOW_STACK(64)
142 vapier 1.23 if (!be_quiet && !found_stack) printf("--- ");
143     if (!be_quiet && !found_relro) printf("--- ");
144 vapier 1.10 }
145    
146     /* textrel fun */
147     if (show_textrel) {
148 vapier 1.26 #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     printf("TEXTREL "); \
161     } \
162     ++dyn; \
163     } \
164     } }
165     SHOW_TEXTREL(32)
166     SHOW_TEXTREL(64)
167 vapier 1.23 if (!be_quiet && !found_textrel) printf("------- ");
168 vapier 1.10 }
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 vapier 1.23 char *rpath, *runpath;
174 vapier 1.26 void *strtbl_void = elf_findsecbyname(elf, ".dynstr");
175 vapier 1.23 rpath = runpath = NULL;
176 vapier 1.10
177 vapier 1.26 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 vapier 1.10 }
201 vapier 1.23 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 vapier 1.10 }
213    
214 vapier 1.14 if (!be_quiet || found_pax || found_stack || found_textrel || found_rpath)
215 vapier 1.27 printf("%s\n", filename);
216    
217     if (find_sym) {
218     void *symtab_void, *strtab_void;
219     symtab_void = elf_findsecbyname(elf, ".symtab");
220     strtab_void = elf_findsecbyname(elf, ".strtab");
221    
222     if (symtab_void && strtab_void) {
223     #define FIND_SYM(B) \
224     if (elf->elf_class == ELFCLASS ## B) { \
225     Elf ## B ## _Shdr *symtab = SHDR ## B (symtab_void); \
226     Elf ## B ## _Shdr *strtab = SHDR ## B (strtab_void); \
227     Elf ## B ## _Sym *sym = SYM ## B (elf->data + EGET(symtab->sh_offset)); \
228     int cnt = EGET(symtab->sh_size) / EGET(symtab->sh_entsize); \
229     char *symname; \
230     if (be_verbose > 1) \
231     printf("%s: .symtab has %i entries\n", filename, cnt); \
232     for (i = 0; i < cnt; ++i) { \
233     if (sym->st_name) { \
234     symname = (char *)(elf->data + EGET(strtab->sh_offset) + EGET(sym->st_name)); \
235     if (*find_sym == '*' || !strcmp(find_sym, symname)) \
236     printf("%s: %5lX %15s %s\n", \
237     filename, \
238     (long)sym->st_size, \
239     (char *)get_elfstttype(sym->st_info & 0xF), \
240     symname); \
241     } \
242     ++sym; \
243     } }
244     FIND_SYM(32)
245     FIND_SYM(64)
246     }
247     }
248 vapier 1.10
249     unreadelf(elf);
250 solar 1.6 }
251    
252 solar 1.1 /* scan a directory for ET_EXEC files and print when we find one */
253 vapier 1.10 static void scanelf_dir(const char *path)
254 solar 1.1 {
255 vapier 1.10 register DIR *dir;
256     register struct dirent *dentry;
257 vapier 1.14 struct stat st_top, st;
258 solar 1.21 char buf[_POSIX_PATH_MAX];
259 solar 1.20 size_t len = 0;
260 vapier 1.10
261     /* make sure path exists */
262 vapier 1.14 if (lstat(path, &st_top) == -1)
263 vapier 1.10 return;
264 solar 1.11
265 vapier 1.10 /* ok, if it isn't a directory, assume we can open it */
266 vapier 1.14 if (!S_ISDIR(st_top.st_mode)) {
267 vapier 1.10 scanelf_file(path);
268     return;
269     }
270    
271     /* now scan the dir looking for fun stuff */
272     if ((dir = opendir(path)) == NULL) {
273     warnf("could not opendir %s: %s", path, strerror(errno));
274     return;
275     }
276 vapier 1.15 if (be_verbose) printf("%s: scanning dir\n", path);
277 solar 1.11
278 vapier 1.10 while ((dentry = readdir(dir))) {
279     if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, ".."))
280     continue;
281     len = (strlen(path) + 2 + strlen(dentry->d_name));
282 solar 1.20 assert(len < sizeof(buf));
283     strncpy(buf, path, len);
284     strncat(buf, "/", len);
285     strncat(buf, dentry->d_name, len);
286     buf[sizeof(buf)] = 0;
287     if (lstat(buf, &st) != -1) {
288 vapier 1.10 if (S_ISREG(st.st_mode))
289 solar 1.20 scanelf_file(buf);
290 vapier 1.10 else if (dir_recurse && S_ISDIR(st.st_mode)) {
291 vapier 1.14 if (dir_crossmount || (st_top.st_dev == st.st_dev))
292 solar 1.20 scanelf_dir(buf);
293 vapier 1.10 }
294     }
295     }
296     closedir(dir);
297 solar 1.1 }
298    
299 vapier 1.10 /* scan /etc/ld.so.conf for paths */
300     static void scanelf_ldpath()
301     {
302 vapier 1.17 char scan_l, scan_ul, scan_ull;
303 vapier 1.10 char *path, *p;
304     FILE *fp;
305    
306     if ((fp = fopen("/etc/ld.so.conf", "r")) == NULL)
307     err("Unable to open ld.so.conf: %s", strerror(errno));
308    
309 vapier 1.17 scan_l = scan_ul = scan_ull = 0;
310    
311 vapier 1.10 path = malloc(_POSIX_PATH_MAX);
312     while ((fgets(path, _POSIX_PATH_MAX, fp)) != NULL)
313     if (*path == '/') {
314     if ((p = strrchr(path, '\r')) != NULL)
315     *p = 0;
316     if ((p = strrchr(path, '\n')) != NULL)
317     *p = 0;
318 vapier 1.17 if (!scan_l && !strcmp(path, "/lib")) scan_l = 1;
319     if (!scan_ul && !strcmp(path, "/usr/lib")) scan_ul = 1;
320     if (!scan_ull && !strcmp(path, "/usr/local/lib")) scan_ull = 1;
321 vapier 1.10 scanelf_dir(path);
322     }
323     free(path);
324    
325 vapier 1.17 if (!scan_l) scanelf_dir("/lib");
326     if (!scan_ul) scanelf_dir("/usr/lib");
327     if (!scan_ull) scanelf_dir("/usr/local/lib");
328    
329 vapier 1.10 fclose(fp);
330     }
331 solar 1.1
332 vapier 1.10 /* scan env PATH for paths */
333     static void scanelf_envpath()
334 solar 1.1 {
335 vapier 1.10 char *path, *p;
336    
337     path = getenv("PATH");
338     if (!path)
339     err("PATH is not set in your env !");
340    
341     if ((path = strdup(path)) == NULL)
342     err("stdup failed: %s", strerror(errno));
343    
344     while ((p = strrchr(path, ':')) != NULL) {
345     scanelf_dir(p + 1);
346     *p = 0;
347     }
348 vapier 1.17
349 vapier 1.10 free(path);
350 solar 1.1 }
351    
352    
353 vapier 1.10
354     /* usage / invocation handling functions */
355 vapier 1.27 #define PARSE_FLAGS "plRmxetrs:aqvo:BhV"
356     #define a_argument required_argument
357 vapier 1.10 static struct option const long_opts[] = {
358     {"path", no_argument, NULL, 'p'},
359     {"ldpath", no_argument, NULL, 'l'},
360     {"recursive", no_argument, NULL, 'R'},
361 vapier 1.14 {"mount", no_argument, NULL, 'm'},
362 vapier 1.10 {"pax", no_argument, NULL, 'x'},
363 solar 1.16 {"header", no_argument, NULL, 'e'},
364 vapier 1.10 {"textrel", no_argument, NULL, 't'},
365     {"rpath", no_argument, NULL, 'r'},
366 vapier 1.27 {"symbol", a_argument, NULL, 's'},
367 vapier 1.10 {"all", no_argument, NULL, 'a'},
368     {"quiet", no_argument, NULL, 'q'},
369 vapier 1.14 {"verbose", no_argument, NULL, 'v'},
370 vapier 1.27 {"file", a_argument, NULL, 'o'},
371 solar 1.16 {"nobanner", no_argument, NULL, 'B'},
372 vapier 1.10 {"help", no_argument, NULL, 'h'},
373     {"version", no_argument, NULL, 'V'},
374     {NULL, no_argument, NULL, 0x0}
375     };
376     static char *opts_help[] = {
377     "Scan all directories in PATH environment",
378     "Scan all directories in /etc/ld.so.conf",
379 vapier 1.14 "Scan directories recursively",
380     "Don't recursively cross mount points\n",
381 vapier 1.10 "Print PaX markings",
382     "Print GNU_STACK markings",
383     "Print TEXTREL information",
384     "Print RPATH information",
385 vapier 1.27 "Find a specified symbol",
386 solar 1.16 "Print all scanned info (-x -e -t -r)\n",
387 vapier 1.14 "Only output 'bad' things",
388     "Be verbose (can be specified more than once)",
389 vapier 1.24 "Write output stream to a filename",
390 vapier 1.14 "Don't display the header",
391 vapier 1.10 "Print this help and exit",
392     "Print version and exit",
393     NULL
394     };
395    
396     /* display usage and exit */
397     static void usage(int status)
398 solar 1.1 {
399 vapier 1.10 int i;
400 vapier 1.27 printf(" Scan ELF binaries for stuff\n"
401 vapier 1.10 "Usage: %s [options] <dir1> [dir2 dirN ...]\n\n", argv0);
402 vapier 1.23 printf("Options:\n");
403 vapier 1.10 for (i = 0; long_opts[i].name; ++i)
404 vapier 1.27 if (long_opts[i].has_arg == no_argument)
405     printf(" -%c, --%-13s %s\n", long_opts[i].val,
406     long_opts[i].name, opts_help[i]);
407     else
408     printf(" -%c, --%-6s <arg> %s\n", long_opts[i].val,
409     long_opts[i].name, opts_help[i]);
410 vapier 1.10 exit(status);
411 solar 1.1 }
412    
413     /* parse command line arguments and preform needed actions */
414 vapier 1.10 static void parseargs(int argc, char *argv[])
415     {
416     int flag;
417    
418     opterr = 0;
419     while ((flag=getopt_long(argc, argv, PARSE_FLAGS, long_opts, NULL)) != -1) {
420     switch (flag) {
421    
422     case 'V': /* version info */
423 solar 1.19 printf("%s compiled %s\n%s\n"
424     "%s written for Gentoo Linux by <solar and vapier @ gentoo.org>\n",
425     __FILE__, __DATE__, rcsid, argv0);
426 vapier 1.10 exit(EXIT_SUCCESS);
427     break;
428     case 'h': usage(EXIT_SUCCESS); break;
429    
430 vapier 1.24 case 'o': {
431 solar 1.21 FILE *fp = NULL;
432     fp = freopen(optarg, "w", stdout);
433 vapier 1.24 if (fp == NULL)
434     err("Could not open output stream '%s': %s", optarg, strerror(errno));
435     stdout = fp;
436 solar 1.21 break;
437     }
438 vapier 1.24
439 vapier 1.27 case 's': find_sym = strdup(optarg); break;
440    
441 solar 1.16 case 'B': show_banner = 0; break;
442 vapier 1.10 case 'l': scan_ldpath = 1; break;
443     case 'p': scan_envpath = 1; break;
444     case 'R': dir_recurse = 1; break;
445 vapier 1.14 case 'm': dir_crossmount = 0; break;
446 vapier 1.10 case 'x': show_pax = 1; break;
447 solar 1.16 case 'e': show_stack = 1; break;
448 vapier 1.10 case 't': show_textrel = 1; break;
449     case 'r': show_rpath = 1; break;
450     case 'q': be_quiet = 1; break;
451 vapier 1.14 case 'v': be_verbose = (be_verbose % 20) + 1; break;
452 vapier 1.10 case 'a': show_pax = show_stack = show_textrel = show_rpath = 1; break;
453    
454     case ':':
455     warn("Option missing parameter");
456     usage(EXIT_FAILURE);
457     break;
458     case '?':
459     warn("Unknown option");
460     usage(EXIT_FAILURE);
461     break;
462     default:
463     err("Unhandled option '%c'", flag);
464     break;
465     }
466     }
467    
468 vapier 1.14 if (be_quiet && be_verbose)
469     err("You can be quiet or you can be verbose, not both, stupid");
470    
471 vapier 1.10 if (scan_ldpath) scanelf_ldpath();
472     if (scan_envpath) scanelf_envpath();
473 vapier 1.25 if (optind == argc && !scan_ldpath && !scan_envpath)
474     err("Nothing to scan !?");
475 vapier 1.10 while (optind < argc)
476     scanelf_dir(argv[optind++]);
477 vapier 1.27
478     if (find_sym) free(find_sym);
479 vapier 1.10 }
480    
481    
482    
483     int main(int argc, char *argv[])
484 solar 1.1 {
485 vapier 1.10 if (argc < 2)
486     usage(EXIT_FAILURE);
487     parseargs(argc, argv);
488 solar 1.21 fclose(stdout);
489 vapier 1.10 return EXIT_SUCCESS;
490 solar 1.1 }

  ViewVC Help
Powered by ViewVC 1.1.20