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

Contents of /pax-utils/scanelf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.30 - (show annotations) (download) (as text)
Tue Apr 5 16:15:31 2005 UTC (9 years, 3 months ago) by solar
Branch: MAIN
Changes since 1.29: +6 -3 lines
File MIME type: text/x-csrc
- add a malloc check

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

  ViewVC Help
Powered by ViewVC 1.1.20