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

Contents of /pax-utils/scanelf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.26 - (hide annotations) (download) (as text)
Tue Apr 5 00:51:33 2005 UTC (9 years, 6 months ago) by vapier
Branch: MAIN
Changes since 1.25: +71 -49 lines
File MIME type: text/x-csrc
handle any bitsize / endian type

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.10
74 solar 1.1
75    
76 vapier 1.10 /* scan an elf file and show all the fun stuff */
77     static void scanelf_file(const char *filename)
78 solar 1.6 {
79 vapier 1.10 int i;
80 vapier 1.14 char found_pax, found_stack, found_relro, found_textrel, found_rpath;
81 vapier 1.26 elfobj *elf;
82 vapier 1.10
83 vapier 1.14 found_pax = found_stack = found_relro = found_textrel = found_rpath = 0;
84 solar 1.12
85 vapier 1.10 /* verify this is real ELF */
86 vapier 1.14 if ((elf = readelf(filename)) == NULL) {
87 vapier 1.26 if (be_verbose > 2) printf("%s: not an ELF\n", filename);
88 vapier 1.10 return;
89 vapier 1.14 }
90    
91 vapier 1.26 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 vapier 1.10
98     /* show the header */
99 solar 1.16 if (!be_quiet && show_banner) {
100 vapier 1.23 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     printf(" FILE\n");
106 solar 1.16 show_banner = 0;
107 vapier 1.10 }
108    
109     /* dump all the good stuff */
110     if (!be_quiet)
111 vapier 1.26 printf("%-7s ", get_elfetype(elf));
112 vapier 1.10
113 vapier 1.14 if (show_pax) {
114     char *paxflags = pax_short_hf_flags(PAX_FLAGS(elf));
115     if (!be_quiet || (be_quiet && strncmp(paxflags, "PeMRxS", 6))) {
116     found_pax = 1;
117     printf("%s ", pax_short_hf_flags(PAX_FLAGS(elf)));
118     }
119     }
120 vapier 1.10
121     /* stack fun */
122     if (show_stack) {
123 vapier 1.26 #define SHOW_STACK(B) \
124     if (elf->elf_class == ELFCLASS ## B) { \
125     Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
126     Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
127     for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
128     if (EGET(phdr[i].p_type) != PT_GNU_STACK && \
129     EGET(phdr[i].p_type) != PT_GNU_RELRO) continue; \
130     if (be_quiet && !(EGET(phdr[i].p_flags) & PF_X)) \
131     continue; \
132     if (EGET(phdr[i].p_type) == PT_GNU_STACK) \
133     found_stack = 1; \
134     if (EGET(phdr[i].p_type) == PT_GNU_RELRO) \
135     found_relro = 1; \
136     printf("%s ", gnu_short_stack_flags(EGET(phdr[i].p_flags))); \
137     } \
138 vapier 1.10 }
139 vapier 1.26 SHOW_STACK(32)
140     SHOW_STACK(64)
141 vapier 1.23 if (!be_quiet && !found_stack) printf("--- ");
142     if (!be_quiet && !found_relro) printf("--- ");
143 vapier 1.10 }
144    
145     /* textrel fun */
146     if (show_textrel) {
147 vapier 1.26 #define SHOW_TEXTREL(B) \
148     if (elf->elf_class == ELFCLASS ## B) { \
149     Elf ## B ## _Dyn *dyn; \
150     Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
151     Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
152     for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
153     if (phdr[i].p_type != PT_DYNAMIC) continue; \
154     dyn = DYN ## B (elf->data + EGET(phdr[i].p_offset)); \
155     while (EGET(dyn->d_tag) != DT_NULL) { \
156     if (EGET(dyn->d_tag) == DT_TEXTREL) { /*dyn->d_tag != DT_FLAGS)*/ \
157     found_textrel = 1; \
158     /*if (dyn->d_un.d_val & DF_TEXTREL)*/ \
159     printf("TEXTREL "); \
160     } \
161     ++dyn; \
162     } \
163     } }
164     SHOW_TEXTREL(32)
165     SHOW_TEXTREL(64)
166 vapier 1.23 if (!be_quiet && !found_textrel) printf("------- ");
167 vapier 1.10 }
168    
169     /* rpath fun */
170     /* TODO: if be_quiet, only output RPATH's which aren't in /etc/ld.so.conf */
171     if (show_rpath) {
172 vapier 1.23 char *rpath, *runpath;
173 vapier 1.26 void *strtbl_void = elf_findsecbyname(elf, ".dynstr");
174 vapier 1.23 rpath = runpath = NULL;
175 vapier 1.10
176 vapier 1.26 if (strtbl_void) {
177     #define SHOW_RPATH(B) \
178     if (elf->elf_class == ELFCLASS ## B) { \
179     Elf ## B ## _Dyn *dyn; \
180     Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
181     Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
182     Elf ## B ## _Shdr *strtbl = SHDR ## B (strtbl_void); \
183     for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
184     if (EGET(phdr[i].p_type) != PT_DYNAMIC) continue; \
185     dyn = DYN ## B (elf->data + EGET(phdr[i].p_offset)); \
186     while (EGET(dyn->d_tag) != DT_NULL) { \
187     if (EGET(dyn->d_tag) == DT_RPATH) { \
188     rpath = elf->data + EGET(strtbl->sh_offset) + EGET(dyn->d_un.d_ptr); \
189     found_rpath = 1; \
190     } else if (EGET(dyn->d_tag) == DT_RUNPATH) { \
191     runpath = elf->data + EGET(strtbl->sh_offset) + EGET(dyn->d_un.d_ptr); \
192     found_rpath = 1; \
193     } \
194     ++dyn; \
195     } \
196     } }
197     SHOW_RPATH(32)
198     SHOW_RPATH(64)
199 vapier 1.10 }
200 vapier 1.23 if (rpath && runpath) {
201     if (!strcmp(rpath, runpath))
202     printf("%-5s ", runpath);
203     else {
204     fprintf(stderr, "%s's RPATH [%s] != RUNPATH [%s]\n", filename, rpath, runpath);
205     printf("{%s,%s} ", rpath, runpath);
206     }
207     } else if (rpath || runpath)
208     printf("%-5s ", (runpath ? runpath : rpath));
209     else if (!be_quiet && !found_rpath)
210     printf(" - ");
211 vapier 1.10 }
212    
213 vapier 1.14 if (!be_quiet || found_pax || found_stack || found_textrel || found_rpath)
214 solar 1.13 puts(filename);
215 vapier 1.10
216     unreadelf(elf);
217 solar 1.6 }
218    
219 solar 1.1 /* scan a directory for ET_EXEC files and print when we find one */
220 vapier 1.10 static void scanelf_dir(const char *path)
221 solar 1.1 {
222 vapier 1.10 register DIR *dir;
223     register struct dirent *dentry;
224 vapier 1.14 struct stat st_top, st;
225 solar 1.21 char buf[_POSIX_PATH_MAX];
226 solar 1.20 size_t len = 0;
227 vapier 1.10
228     /* make sure path exists */
229 vapier 1.14 if (lstat(path, &st_top) == -1)
230 vapier 1.10 return;
231 solar 1.11
232 vapier 1.10 /* ok, if it isn't a directory, assume we can open it */
233 vapier 1.14 if (!S_ISDIR(st_top.st_mode)) {
234 vapier 1.10 scanelf_file(path);
235     return;
236     }
237    
238     /* now scan the dir looking for fun stuff */
239     if ((dir = opendir(path)) == NULL) {
240     warnf("could not opendir %s: %s", path, strerror(errno));
241     return;
242     }
243 vapier 1.15 if (be_verbose) printf("%s: scanning dir\n", path);
244 solar 1.11
245 vapier 1.10 while ((dentry = readdir(dir))) {
246     if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, ".."))
247     continue;
248     len = (strlen(path) + 2 + strlen(dentry->d_name));
249 solar 1.20 assert(len < sizeof(buf));
250     strncpy(buf, path, len);
251     strncat(buf, "/", len);
252     strncat(buf, dentry->d_name, len);
253     buf[sizeof(buf)] = 0;
254     if (lstat(buf, &st) != -1) {
255 vapier 1.10 if (S_ISREG(st.st_mode))
256 solar 1.20 scanelf_file(buf);
257 vapier 1.10 else if (dir_recurse && S_ISDIR(st.st_mode)) {
258 vapier 1.14 if (dir_crossmount || (st_top.st_dev == st.st_dev))
259 solar 1.20 scanelf_dir(buf);
260 vapier 1.10 }
261     }
262     }
263     closedir(dir);
264 solar 1.1 }
265    
266 vapier 1.10 /* scan /etc/ld.so.conf for paths */
267     static void scanelf_ldpath()
268     {
269 vapier 1.17 char scan_l, scan_ul, scan_ull;
270 vapier 1.10 char *path, *p;
271     FILE *fp;
272    
273     if ((fp = fopen("/etc/ld.so.conf", "r")) == NULL)
274     err("Unable to open ld.so.conf: %s", strerror(errno));
275    
276 vapier 1.17 scan_l = scan_ul = scan_ull = 0;
277    
278 vapier 1.10 path = malloc(_POSIX_PATH_MAX);
279     while ((fgets(path, _POSIX_PATH_MAX, fp)) != NULL)
280     if (*path == '/') {
281     if ((p = strrchr(path, '\r')) != NULL)
282     *p = 0;
283     if ((p = strrchr(path, '\n')) != NULL)
284     *p = 0;
285 vapier 1.17 if (!scan_l && !strcmp(path, "/lib")) scan_l = 1;
286     if (!scan_ul && !strcmp(path, "/usr/lib")) scan_ul = 1;
287     if (!scan_ull && !strcmp(path, "/usr/local/lib")) scan_ull = 1;
288 vapier 1.10 scanelf_dir(path);
289     }
290     free(path);
291    
292 vapier 1.17 if (!scan_l) scanelf_dir("/lib");
293     if (!scan_ul) scanelf_dir("/usr/lib");
294     if (!scan_ull) scanelf_dir("/usr/local/lib");
295    
296 vapier 1.10 fclose(fp);
297     }
298 solar 1.1
299 vapier 1.10 /* scan env PATH for paths */
300     static void scanelf_envpath()
301 solar 1.1 {
302 vapier 1.10 char *path, *p;
303    
304     path = getenv("PATH");
305     if (!path)
306     err("PATH is not set in your env !");
307    
308     if ((path = strdup(path)) == NULL)
309     err("stdup failed: %s", strerror(errno));
310    
311     while ((p = strrchr(path, ':')) != NULL) {
312     scanelf_dir(p + 1);
313     *p = 0;
314     }
315 vapier 1.17
316 vapier 1.10 free(path);
317 solar 1.1 }
318    
319    
320 vapier 1.10
321     /* usage / invocation handling functions */
322 vapier 1.24 #define PARSE_FLAGS "plRmxetraqvo:BhV"
323 vapier 1.10 static struct option const long_opts[] = {
324     {"path", no_argument, NULL, 'p'},
325     {"ldpath", no_argument, NULL, 'l'},
326     {"recursive", no_argument, NULL, 'R'},
327 vapier 1.14 {"mount", no_argument, NULL, 'm'},
328 vapier 1.10 {"pax", no_argument, NULL, 'x'},
329 solar 1.16 {"header", no_argument, NULL, 'e'},
330 vapier 1.10 {"textrel", no_argument, NULL, 't'},
331     {"rpath", no_argument, NULL, 'r'},
332     {"all", no_argument, NULL, 'a'},
333     {"quiet", no_argument, NULL, 'q'},
334 vapier 1.14 {"verbose", no_argument, NULL, 'v'},
335 vapier 1.24 {"file",required_argument, NULL, 'o'},
336 solar 1.16 {"nobanner", no_argument, NULL, 'B'},
337 vapier 1.10 {"help", no_argument, NULL, 'h'},
338     {"version", no_argument, NULL, 'V'},
339     {NULL, no_argument, NULL, 0x0}
340     };
341     static char *opts_help[] = {
342     "Scan all directories in PATH environment",
343     "Scan all directories in /etc/ld.so.conf",
344 vapier 1.14 "Scan directories recursively",
345     "Don't recursively cross mount points\n",
346 vapier 1.10 "Print PaX markings",
347     "Print GNU_STACK markings",
348     "Print TEXTREL information",
349     "Print RPATH information",
350 solar 1.16 "Print all scanned info (-x -e -t -r)\n",
351 vapier 1.14 "Only output 'bad' things",
352     "Be verbose (can be specified more than once)",
353 vapier 1.24 "Write output stream to a filename",
354 vapier 1.14 "Don't display the header",
355 vapier 1.10 "Print this help and exit",
356     "Print version and exit",
357     NULL
358     };
359    
360     /* display usage and exit */
361     static void usage(int status)
362 solar 1.1 {
363 vapier 1.10 int i;
364     printf(" Scan ELF binaries for stuff\n\n"
365     "Usage: %s [options] <dir1> [dir2 dirN ...]\n\n", argv0);
366 vapier 1.23 printf("Options:\n");
367 vapier 1.10 for (i = 0; long_opts[i].name; ++i)
368     printf(" -%c, --%-12s %s\n", long_opts[i].val,
369     long_opts[i].name, opts_help[i]);
370 solar 1.16 #ifdef MANLYPAGE
371     for (i = 0; long_opts[i].name; ++i)
372     printf(".TP\n\\fB\\-%c, \\-\\-%s\\fR\n%s\n", long_opts[i].val,
373     long_opts[i].name, opts_help[i]);
374     #endif
375 vapier 1.10 exit(status);
376 solar 1.1 }
377    
378     /* parse command line arguments and preform needed actions */
379 vapier 1.10 static void parseargs(int argc, char *argv[])
380     {
381     int flag;
382    
383     opterr = 0;
384     while ((flag=getopt_long(argc, argv, PARSE_FLAGS, long_opts, NULL)) != -1) {
385     switch (flag) {
386    
387     case 'V': /* version info */
388 solar 1.19 printf("%s compiled %s\n%s\n"
389     "%s written for Gentoo Linux by <solar and vapier @ gentoo.org>\n",
390     __FILE__, __DATE__, rcsid, argv0);
391 vapier 1.10 exit(EXIT_SUCCESS);
392     break;
393 solar 1.16 case 's': /* reserved for -s, --symbol= */
394 vapier 1.10 case 'h': usage(EXIT_SUCCESS); break;
395    
396 vapier 1.24 case 'o': {
397 solar 1.21 FILE *fp = NULL;
398     fp = freopen(optarg, "w", stdout);
399 vapier 1.24 if (fp == NULL)
400     err("Could not open output stream '%s': %s", optarg, strerror(errno));
401     stdout = fp;
402 solar 1.21 break;
403     }
404 vapier 1.24
405 solar 1.16 case 'B': show_banner = 0; break;
406 vapier 1.10 case 'l': scan_ldpath = 1; break;
407     case 'p': scan_envpath = 1; break;
408     case 'R': dir_recurse = 1; break;
409 vapier 1.14 case 'm': dir_crossmount = 0; break;
410 vapier 1.10 case 'x': show_pax = 1; break;
411 solar 1.16 case 'e': show_stack = 1; break;
412 vapier 1.10 case 't': show_textrel = 1; break;
413     case 'r': show_rpath = 1; break;
414     case 'q': be_quiet = 1; break;
415 vapier 1.14 case 'v': be_verbose = (be_verbose % 20) + 1; break;
416 vapier 1.10 case 'a': show_pax = show_stack = show_textrel = show_rpath = 1; break;
417    
418     case ':':
419     warn("Option missing parameter");
420     usage(EXIT_FAILURE);
421     break;
422     case '?':
423     warn("Unknown option");
424     usage(EXIT_FAILURE);
425     break;
426     default:
427     err("Unhandled option '%c'", flag);
428     break;
429     }
430     }
431    
432 vapier 1.14 if (be_quiet && be_verbose)
433     err("You can be quiet or you can be verbose, not both, stupid");
434    
435 vapier 1.10 if (scan_ldpath) scanelf_ldpath();
436     if (scan_envpath) scanelf_envpath();
437 vapier 1.25 if (optind == argc && !scan_ldpath && !scan_envpath)
438     err("Nothing to scan !?");
439 vapier 1.10 while (optind < argc)
440     scanelf_dir(argv[optind++]);
441     }
442    
443    
444    
445     int main(int argc, char *argv[])
446 solar 1.1 {
447 vapier 1.10 if (argc < 2)
448     usage(EXIT_FAILURE);
449     parseargs(argc, argv);
450 solar 1.21 fclose(stdout);
451 vapier 1.10 return EXIT_SUCCESS;
452 solar 1.1 }

  ViewVC Help
Powered by ViewVC 1.1.20