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

Contents of /pax-utils/scanelf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.60 - (show annotations) (download) (as text)
Fri May 27 02:58:39 2005 UTC (9 years, 5 months ago) by vapier
Branch: MAIN
Changes since 1.59: +12 -6 lines
File MIME type: text/x-csrc
fixes for FreeBSD

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.59 2005/05/25 21:58: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 #include <libgen.h>
28 #include <limits.h>
29 #define __USE_GNU
30 #include <string.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <sys/stat.h>
34 #include <dirent.h>
35 #include <getopt.h>
36 #include <assert.h>
37
38 #include "paxelf.h"
39
40 static const char *rcsid = "$Id: scanelf.c,v 1.59 2005/05/25 21:58:03 vapier Exp $";
41 #define argv0 "scanelf"
42
43
44
45 /* prototypes */
46 static void scanelf_file(const char *filename);
47 static void scanelf_dir(const char *path);
48 static void scanelf_ldpath();
49 static void scanelf_envpath();
50 static void usage(int status);
51 static void parseargs(int argc, char *argv[]);
52 static char *xstrdup(const char *s);
53 static void *xmalloc(size_t size);
54 static void xstrcat(char **dst, const char *src, size_t *curr_len);
55 static inline void xchrcat(char **dst, const char append, size_t *curr_len);
56
57 /* variables to control behavior */
58 static char *ldpaths[256];
59 static char scan_ldpath = 0;
60 static char scan_envpath = 0;
61 static char scan_symlink = 1;
62 static char dir_recurse = 0;
63 static char dir_crossmount = 1;
64 static char show_pax = 0;
65 static char show_stack = 0;
66 static char show_textrel = 0;
67 static char show_rpath = 0;
68 static char show_needed = 0;
69 static char show_interp = 0;
70 static char show_bind = 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, *versioned_symname = NULL;
75 static char *out_format = NULL;
76
77
78
79 /* sub-funcs for scanelf_file() */
80 static char *scanelf_file_pax(elfobj *elf, char *found_pax)
81 {
82 static char *paxflags;
83
84 if (!show_pax) return NULL;
85
86 paxflags = pax_short_hf_flags(PAX_FLAGS(elf));
87 if (!be_quiet || (be_quiet && strncmp(paxflags, "PeMRxS", 6))) {
88 *found_pax = 1;
89 return paxflags;
90 }
91
92 return NULL;
93 }
94 static char *scanelf_file_stack(elfobj *elf, char *found_stack, char *found_relro)
95 {
96 static char ret[8] = "--- ---";
97 char *found;
98 unsigned long i, off, shown;
99
100 if (!show_stack) return NULL;
101
102 shown = 0;
103
104 if (elf->phdr) {
105 #define SHOW_STACK(B) \
106 if (elf->elf_class == ELFCLASS ## B) { \
107 Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
108 Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
109 for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
110 if (EGET(phdr[i].p_type) == PT_GNU_STACK) { \
111 found = found_stack; \
112 off = 0; \
113 } else if (EGET(phdr[i].p_type) == PT_GNU_RELRO) { \
114 found = found_relro; \
115 off = 4; \
116 } else \
117 continue; \
118 if (be_quiet && !(EGET(phdr[i].p_flags) & PF_X)) \
119 continue; \
120 memcpy(ret+off, gnu_short_stack_flags(EGET(phdr[i].p_flags)), 3); \
121 *found = 1; \
122 ++shown; \
123 } \
124 }
125 SHOW_STACK(32)
126 SHOW_STACK(64)
127 }
128
129 if (be_quiet && !shown)
130 return NULL;
131 else
132 return ret;
133 }
134 static char *scanelf_file_textrel(elfobj *elf, char *found_textrel)
135 {
136 static char *ret = "TEXTREL";
137 unsigned long i;
138
139 if (!show_textrel) return NULL;
140
141 if (elf->phdr) {
142 #define SHOW_TEXTREL(B) \
143 if (elf->elf_class == ELFCLASS ## B) { \
144 Elf ## B ## _Dyn *dyn; \
145 Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
146 Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
147 Elf ## B ## _Off offset; \
148 for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
149 if (EGET(phdr[i].p_type) != PT_DYNAMIC) continue; \
150 offset = EGET(phdr[i].p_offset); \
151 if (offset >= elf->len - sizeof(Elf ## B ## _Dyn)) continue; \
152 dyn = DYN ## B (elf->data + offset); \
153 while (EGET(dyn->d_tag) != DT_NULL) { \
154 if (EGET(dyn->d_tag) == DT_TEXTREL) { /*dyn->d_tag != DT_FLAGS)*/ \
155 *found_textrel = 1; \
156 /*if (dyn->d_un.d_val & DF_TEXTREL)*/ \
157 return ret; \
158 } \
159 ++dyn; \
160 } \
161 } }
162 SHOW_TEXTREL(32)
163 SHOW_TEXTREL(64)
164 }
165
166 if (be_quiet)
167 return NULL;
168 else
169 return " - ";
170 }
171 static void scanelf_file_rpath(elfobj *elf, char *found_rpath, char **ret, size_t *ret_len)
172 {
173 /* TODO: when checking RPATH entries, check each subpath (between :) in ld.so.conf */
174 unsigned long i, s;
175 char *rpath, *runpath, **r;
176 void *strtbl_void;
177
178 if (!show_rpath) return;
179
180 strtbl_void = elf_findsecbyname(elf, ".dynstr");
181 rpath = runpath = NULL;
182
183 if (elf->phdr && strtbl_void) {
184 #define SHOW_RPATH(B) \
185 if (elf->elf_class == ELFCLASS ## B) { \
186 Elf ## B ## _Dyn *dyn; \
187 Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
188 Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
189 Elf ## B ## _Shdr *strtbl = SHDR ## B (strtbl_void); \
190 Elf ## B ## _Off offset; \
191 Elf ## B ## _Xword word; \
192 /* Scan all the program headers */ \
193 for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
194 /* Just scan dynamic headers */ \
195 if (EGET(phdr[i].p_type) != PT_DYNAMIC) continue; \
196 offset = EGET(phdr[i].p_offset); \
197 if (offset >= elf->len - sizeof(Elf ## B ## _Dyn)) continue; \
198 /* Just scan dynamic RPATH/RUNPATH headers */ \
199 dyn = DYN ## B (elf->data + offset); \
200 while ((word=EGET(dyn->d_tag)) != DT_NULL) { \
201 if (word == DT_RPATH) { \
202 r = &rpath; \
203 } else if (word == DT_RUNPATH) { \
204 r = &runpath; \
205 } else { \
206 ++dyn; \
207 continue; \
208 } \
209 /* Verify the memory is somewhat sane */ \
210 offset = EGET(strtbl->sh_offset) + EGET(dyn->d_un.d_ptr); \
211 if (offset < elf->len) { \
212 if (*r) warn("ELF has multiple %s's !?", get_elfdtype(word)); \
213 *r = (char*)(elf->data + offset); \
214 /* If quiet, don't output paths in ld.so.conf */ \
215 if (be_quiet) \
216 for (s = 0; ldpaths[s]; ++s) \
217 if (!strcmp(ldpaths[s], *r)) { \
218 *r = NULL; \
219 break; \
220 } \
221 if (*r) *found_rpath = 1; \
222 } \
223 ++dyn; \
224 } \
225 } }
226 SHOW_RPATH(32)
227 SHOW_RPATH(64)
228 }
229
230 if (rpath && runpath) {
231 if (!strcmp(rpath, runpath)) {
232 xstrcat(ret, runpath, ret_len);
233 } else {
234 fprintf(stderr, "RPATH [%s] != RUNPATH [%s]\n", rpath, runpath);
235 xchrcat(ret, '{', ret_len);
236 xstrcat(ret, rpath, ret_len);
237 xchrcat(ret, ',', ret_len);
238 xstrcat(ret, runpath, ret_len);
239 xchrcat(ret, '}', ret_len);
240 }
241 } else if (rpath || runpath)
242 xstrcat(ret, (runpath ? runpath : rpath), ret_len);
243 else if (!be_quiet)
244 xstrcat(ret, " - ", ret_len);
245 }
246 static void scanelf_file_needed(elfobj *elf, char *found_needed, char **ret, size_t *ret_len)
247 {
248 unsigned long i;
249 char *needed;
250 void *strtbl_void;
251
252 if (!show_needed) return;
253
254 strtbl_void = elf_findsecbyname(elf, ".dynstr");
255
256 if (elf->phdr && strtbl_void) {
257 #define SHOW_NEEDED(B) \
258 if (elf->elf_class == ELFCLASS ## B) { \
259 Elf ## B ## _Dyn *dyn; \
260 Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
261 Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
262 Elf ## B ## _Shdr *strtbl = SHDR ## B (strtbl_void); \
263 Elf ## B ## _Off offset; \
264 for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
265 if (EGET(phdr[i].p_type) != PT_DYNAMIC) continue; \
266 offset = EGET(phdr[i].p_offset); \
267 if (offset >= elf->len - sizeof(Elf ## B ## _Dyn)) continue; \
268 dyn = DYN ## B (elf->data + offset); \
269 while (EGET(dyn->d_tag) != DT_NULL) { \
270 if (EGET(dyn->d_tag) == DT_NEEDED) { \
271 offset = EGET(strtbl->sh_offset) + EGET(dyn->d_un.d_ptr); \
272 if (offset >= elf->len) { \
273 ++dyn; \
274 continue; \
275 } \
276 needed = (char*)(elf->data + offset); \
277 if (*found_needed) xchrcat(ret, ',', ret_len); \
278 xstrcat(ret, needed, ret_len); \
279 *found_needed = 1; \
280 } \
281 ++dyn; \
282 } \
283 } }
284 SHOW_NEEDED(32)
285 SHOW_NEEDED(64)
286 }
287 }
288 static char *scanelf_file_interp(elfobj *elf, char *found_interp)
289 {
290 void *strtbl_void;
291
292 if (!show_interp) return NULL;
293
294 strtbl_void = elf_findsecbyname(elf, ".interp");
295
296 if (strtbl_void) {
297 #define SHOW_INTERP(B) \
298 if (elf->elf_class == ELFCLASS ## B) { \
299 Elf ## B ## _Shdr *strtbl = SHDR ## B (strtbl_void); \
300 *found_interp = 1; \
301 return elf->data + EGET(strtbl->sh_offset); \
302 }
303 SHOW_INTERP(32)
304 SHOW_INTERP(64)
305 }
306 return NULL;
307 }
308 static char *scanelf_file_bind(elfobj *elf, char *found_bind)
309 {
310 unsigned long i;
311 struct stat s;
312
313 if (!show_bind) return NULL;
314 if (!elf->phdr) return NULL;
315
316 #define SHOW_BIND(B) \
317 if (elf->elf_class == ELFCLASS ## B) { \
318 Elf ## B ## _Dyn *dyn; \
319 Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
320 Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
321 Elf ## B ## _Off offset; \
322 for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
323 if (EGET(phdr[i].p_type) != PT_DYNAMIC) continue; \
324 offset = EGET(phdr[i].p_offset); \
325 if (offset >= elf->len - sizeof(Elf ## B ## _Dyn)) continue; \
326 dyn = DYN ## B (elf->data + offset); \
327 while (EGET(dyn->d_tag) != DT_NULL) { \
328 if (EGET(dyn->d_tag) == DT_BIND_NOW || \
329 (EGET(dyn->d_tag) == DT_FLAGS && EGET(dyn->d_un.d_val) & DF_BIND_NOW)) \
330 { \
331 if (be_quiet) return NULL; \
332 *found_bind = 1; \
333 return "NOW"; \
334 } \
335 ++dyn; \
336 } \
337 } \
338 }
339 SHOW_BIND(32)
340 SHOW_BIND(64)
341
342 if (be_quiet && !fstat(elf->fd, &s) && !(s.st_mode & S_ISUID || s.st_mode & S_ISGID)) {
343 return NULL;
344 } else {
345 *found_bind = 1;
346 return "LAZY";
347 }
348 }
349 static char *scanelf_file_sym(elfobj *elf, char *found_sym, const char *filename)
350 {
351 unsigned long i;
352 void *symtab_void, *strtab_void;
353
354 if (!find_sym) return NULL;
355
356 symtab_void = elf_findsecbyname(elf, ".symtab");
357 strtab_void = elf_findsecbyname(elf, ".strtab");
358
359 if (symtab_void && strtab_void) {
360 char *base, *basemem;
361 basemem = xstrdup(filename);
362 base = basename(basemem);
363 #define FIND_SYM(B) \
364 if (elf->elf_class == ELFCLASS ## B) { \
365 Elf ## B ## _Shdr *symtab = SHDR ## B (symtab_void); \
366 Elf ## B ## _Shdr *strtab = SHDR ## B (strtab_void); \
367 Elf ## B ## _Sym *sym = SYM ## B (elf->data + EGET(symtab->sh_offset)); \
368 unsigned long cnt = EGET(symtab->sh_size) / EGET(symtab->sh_entsize); \
369 char *symname; \
370 for (i = 0; i < cnt; ++i) { \
371 if (sym->st_name) { \
372 symname = (char *)(elf->data + EGET(strtab->sh_offset) + EGET(sym->st_name)); \
373 if (*find_sym == '*') { \
374 printf("%s(%s) %5lX %15s %s\n", \
375 ((*found_sym == 0) ? "\n\t" : "\t"), \
376 base, \
377 (long)sym->st_size, \
378 (char *)get_elfstttype(sym->st_info), \
379 symname); \
380 *found_sym = 1; \
381 } else if ((strcmp(find_sym, symname) == 0) || \
382 (strcmp(symname, versioned_symname) == 0)) \
383 (*found_sym)++; \
384 } \
385 ++sym; \
386 } }
387 FIND_SYM(32)
388 FIND_SYM(64)
389 free(basemem);
390 }
391 if (*find_sym != '*' && *found_sym)
392 return find_sym;
393 if (be_quiet)
394 return NULL;
395 else
396 return " - ";
397 }
398 /* scan an elf file and show all the fun stuff */
399 // #define prints(str) fputs(str, stdout)
400 #define prints(str) write(fileno(stdout), str, strlen(str))
401 static void scanelf_file(const char *filename)
402 {
403 unsigned long i;
404 char found_pax, found_stack, found_relro, found_textrel,
405 found_rpath, found_needed, found_interp, found_bind,
406 found_sym, found_file;
407 elfobj *elf;
408 struct stat st;
409 static char *out_buffer = NULL;
410 static size_t out_len;
411
412 /* make sure 'filename' exists */
413 if (lstat(filename, &st) == -1) {
414 if (be_verbose > 2) printf("%s: does not exist\n", filename);
415 return;
416 }
417 /* always handle regular files and handle symlinked files if no -y */
418 if (S_ISLNK(st.st_mode)) {
419 if (!scan_symlink) return;
420 stat(filename, &st);
421 }
422 if (!S_ISREG(st.st_mode)) {
423 if (be_verbose > 2) printf("%s: skipping non-file\n", filename);
424 return;
425 }
426
427 found_pax = found_stack = found_relro = found_textrel = \
428 found_rpath = found_needed = found_interp = found_bind = \
429 found_sym = found_file = 0;
430
431 /* verify this is real ELF */
432 if ((elf = readelf(filename)) == NULL) {
433 if (be_verbose > 2) printf("%s: not an ELF\n", filename);
434 return;
435 }
436
437 if (be_verbose > 1)
438 printf("%s: scanning file {%s,%s}\n", filename,
439 get_elfeitype(elf, EI_CLASS, elf->elf_class),
440 get_elfeitype(elf, EI_DATA, elf->data[EI_DATA]));
441 else if (be_verbose)
442 printf("%s: scanning file\n", filename);
443
444 /* init output buffer */
445 if (!out_buffer) {
446 out_len = sizeof(char) * 80;
447 out_buffer = (char*)xmalloc(out_len);
448 }
449 *out_buffer = '\0';
450
451 /* show the header */
452 if (!be_quiet && show_banner) {
453 for (i = 0; out_format[i]; ++i) {
454 if (out_format[i] != '%') continue;
455
456 switch (out_format[++i]) {
457 case '%': break;
458 case 'F': prints("FILE "); found_file = 1; break;
459 case 'o': prints(" TYPE "); break;
460 case 'x': prints(" PAX "); break;
461 case 'e': prints("STK/REL "); break;
462 case 't': prints("TEXTREL "); break;
463 case 'r': prints("RPATH "); break;
464 case 'n': prints("NEEDED "); break;
465 case 'i': prints("INTERP "); break;
466 case 'b': prints("BIND "); break;
467 case 's': prints("SYM "); break;
468 }
469 }
470 if (!found_file) prints("FILE ");
471 prints("\n");
472 found_file = 0;
473 show_banner = 0;
474 }
475
476 /* dump all the good stuff */
477 for (i = 0; out_format[i]; ++i) {
478 const char *out;
479
480 /* make sure we trim leading spaces in quiet mode */
481 if (be_quiet && *out_buffer == ' ' && !out_buffer[1])
482 *out_buffer = '\0';
483
484 if (out_format[i] != '%') {
485 xchrcat(&out_buffer, out_format[i], &out_len);
486 continue;
487 }
488
489 out = NULL;
490 switch (out_format[++i]) {
491 case '%': xchrcat(&out_buffer, '%', &out_len); break;
492 case 'F': found_file = 1; xstrcat(&out_buffer, filename, &out_len); break;
493 case 'o': out = get_elfetype(elf); break;
494 case 'x': out = scanelf_file_pax(elf, &found_pax); break;
495 case 'e': out = scanelf_file_stack(elf, &found_stack, &found_relro); break;
496 case 't': out = scanelf_file_textrel(elf, &found_textrel); break;
497 case 'r': scanelf_file_rpath(elf, &found_rpath, &out_buffer, &out_len); break;
498 case 'n': scanelf_file_needed(elf, &found_needed, &out_buffer, &out_len); break;
499 case 'i': out = scanelf_file_interp(elf, &found_interp); break;
500 case 'b': out = scanelf_file_bind(elf, &found_bind); break;
501 case 's': out = scanelf_file_sym(elf, &found_sym, filename); break;
502 }
503 if (out) xstrcat(&out_buffer, out, &out_len);
504 }
505
506 #define FOUND_SOMETHING() \
507 (found_pax || found_stack || found_textrel || found_rpath || \
508 found_needed || found_interp || found_bind || found_sym)
509
510 if (!found_file && (!be_quiet || (be_quiet && FOUND_SOMETHING()))) {
511 xchrcat(&out_buffer, ' ', &out_len);
512 xstrcat(&out_buffer, filename, &out_len);
513 }
514 if (!be_quiet || (be_quiet && FOUND_SOMETHING()))
515 puts(out_buffer);
516
517 unreadelf(elf);
518 }
519
520 /* scan a directory for ET_EXEC files and print when we find one */
521 static void scanelf_dir(const char *path)
522 {
523 register DIR *dir;
524 register struct dirent *dentry;
525 struct stat st_top, st;
526 char buf[_POSIX_PATH_MAX];
527 size_t pathlen = 0, len = 0;
528
529 /* make sure path exists */
530 if (lstat(path, &st_top) == -1) {
531 if (be_verbose > 2) printf("%s: does not exist\n", path);
532 return;
533 }
534
535 /* ok, if it isn't a directory, assume we can open it */
536 if (!S_ISDIR(st_top.st_mode)) {
537 scanelf_file(path);
538 return;
539 }
540
541 /* now scan the dir looking for fun stuff */
542 if ((dir = opendir(path)) == NULL) {
543 warnf("could not opendir %s: %s", path, strerror(errno));
544 return;
545 }
546 if (be_verbose) printf("%s: scanning dir\n", path);
547
548 pathlen = strlen(path);
549 while ((dentry = readdir(dir))) {
550 if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, ".."))
551 continue;
552 len = (pathlen + 1 + strlen(dentry->d_name) + 1);
553 if (len >= sizeof(buf)) {
554 warnf("Skipping '%s': len > sizeof(buf); %d > %d\n", path, (int)len, (int)sizeof(buf));
555 continue;
556 }
557 sprintf(buf, "%s/%s", path, dentry->d_name);
558 if (lstat(buf, &st) != -1) {
559 if (S_ISREG(st.st_mode))
560 scanelf_file(buf);
561 else if (dir_recurse && S_ISDIR(st.st_mode)) {
562 if (dir_crossmount || (st_top.st_dev == st.st_dev))
563 scanelf_dir(buf);
564 }
565 }
566 }
567 closedir(dir);
568 }
569
570 static int scanelf_from_file(char *filename)
571 {
572 FILE *fp = NULL;
573 char *p;
574 char path[_POSIX_PATH_MAX];
575
576 if (((strcmp(filename, "-")) == 0) && (ttyname(0) == NULL))
577 fp = stdin;
578 else if ((fp = fopen(filename, "r")) == NULL)
579 return 1;
580
581 while ((fgets(path, _POSIX_PATH_MAX, fp)) != NULL) {
582 if ((p = strchr(path, '\n')) != NULL)
583 *p = 0;
584 scanelf_dir(path);
585 }
586 if (fp != stdin)
587 fclose(fp);
588 return 0;
589 }
590
591 static void load_ld_so_conf()
592 {
593 FILE *fp = NULL;
594 char *p;
595 char path[_POSIX_PATH_MAX];
596 int i = 0;
597
598 if ((fp = fopen("/etc/ld.so.conf", "r")) == NULL)
599 return;
600
601 while ((fgets(path, _POSIX_PATH_MAX, fp)) != NULL) {
602 if (*path != '/')
603 continue;
604
605 if ((p = strrchr(path, '\r')) != NULL)
606 *p = 0;
607 if ((p = strchr(path, '\n')) != NULL)
608 *p = 0;
609
610 ldpaths[i++] = xstrdup(path);
611
612 if (i + 1 == sizeof(ldpaths) / sizeof(*ldpaths))
613 break;
614 }
615 ldpaths[i] = NULL;
616
617 fclose(fp);
618 }
619
620 /* scan /etc/ld.so.conf for paths */
621 static void scanelf_ldpath()
622 {
623 char scan_l, scan_ul, scan_ull;
624 int i = 0;
625
626 if (!ldpaths[0])
627 err("Unable to load any paths from ld.so.conf");
628
629 scan_l = scan_ul = scan_ull = 0;
630
631 while (ldpaths[i]) {
632 if (!scan_l && !strcmp(ldpaths[i], "/lib")) scan_l = 1;
633 if (!scan_ul && !strcmp(ldpaths[i], "/usr/lib")) scan_ul = 1;
634 if (!scan_ull && !strcmp(ldpaths[i], "/usr/local/lib")) scan_ull = 1;
635 scanelf_dir(ldpaths[i]);
636 ++i;
637 }
638
639 if (!scan_l) scanelf_dir("/lib");
640 if (!scan_ul) scanelf_dir("/usr/lib");
641 if (!scan_ull) scanelf_dir("/usr/local/lib");
642 }
643
644 /* scan env PATH for paths */
645 static void scanelf_envpath()
646 {
647 char *path, *p;
648
649 path = getenv("PATH");
650 if (!path)
651 err("PATH is not set in your env !");
652 path = xstrdup(path);
653
654 while ((p = strrchr(path, ':')) != NULL) {
655 scanelf_dir(p + 1);
656 *p = 0;
657 }
658
659 free(path);
660 }
661
662
663
664 /* usage / invocation handling functions */
665 #define PARSE_FLAGS "plRmyxetrnibs:aqvF:f:o:BhV"
666 #define a_argument required_argument
667 static struct option const long_opts[] = {
668 {"path", no_argument, NULL, 'p'},
669 {"ldpath", no_argument, NULL, 'l'},
670 {"recursive", no_argument, NULL, 'R'},
671 {"mount", no_argument, NULL, 'm'},
672 {"symlink", no_argument, NULL, 'y'},
673 {"pax", no_argument, NULL, 'x'},
674 {"header", no_argument, NULL, 'e'},
675 {"textrel", no_argument, NULL, 't'},
676 {"rpath", no_argument, NULL, 'r'},
677 {"needed", no_argument, NULL, 'n'},
678 {"interp", no_argument, NULL, 'i'},
679 {"bind", no_argument, NULL, 'b'},
680 {"symbol", a_argument, NULL, 's'},
681 {"all", no_argument, NULL, 'a'},
682 {"quiet", no_argument, NULL, 'q'},
683 {"verbose", no_argument, NULL, 'v'},
684 {"format", a_argument, NULL, 'F'},
685 {"from", a_argument, NULL, 'f'},
686 {"file", a_argument, NULL, 'o'},
687 {"nobanner", no_argument, NULL, 'B'},
688 {"help", no_argument, NULL, 'h'},
689 {"version", no_argument, NULL, 'V'},
690 {NULL, no_argument, NULL, 0x0}
691 };
692
693 static char *opts_help[] = {
694 "Scan all directories in PATH environment",
695 "Scan all directories in /etc/ld.so.conf",
696 "Scan directories recursively",
697 "Don't recursively cross mount points",
698 "Don't scan symlinks\n",
699 "Print PaX markings",
700 "Print GNU_STACK markings",
701 "Print TEXTREL information",
702 "Print RPATH information",
703 "Print NEEDED information",
704 "Print INTERP information",
705 "Print BIND information",
706 "Find a specified symbol",
707 "Print all scanned info (-x -e -t -r -n -i -b)\n",
708 "Only output 'bad' things",
709 "Be verbose (can be specified more than once)",
710 "Use specified format for output",
711 "Read input stream from a filename",
712 "Write output stream to a filename",
713 "Don't display the header",
714 "Print this help and exit",
715 "Print version and exit",
716 NULL
717 };
718
719 /* display usage and exit */
720 static void usage(int status)
721 {
722 unsigned long i;
723 printf("* Scan ELF binaries for stuff\n\n"
724 "Usage: %s [options] <dir1/file1> [dir2 dirN fileN ...]\n\n", argv0);
725 printf("Options: -[%s]\n", PARSE_FLAGS);
726 for (i = 0; long_opts[i].name; ++i)
727 if (long_opts[i].has_arg == no_argument)
728 printf(" -%c, --%-13s* %s\n", long_opts[i].val,
729 long_opts[i].name, opts_help[i]);
730 else
731 printf(" -%c, --%-6s <arg> * %s\n", long_opts[i].val,
732 long_opts[i].name, opts_help[i]);
733
734 if (status != EXIT_SUCCESS)
735 exit(status);
736
737 puts("\nThe format modifiers for the -F option are:");
738 puts(" %F Filename \t%x PaX Flags \t%e STACK/RELRO");
739 puts(" %t TEXTREL \t%r RPATH \t%n NEEDED");
740 puts(" %i INTERP \t%b BIND \t%s symbol");
741
742 exit(status);
743 }
744
745 /* parse command line arguments and preform needed actions */
746 static void parseargs(int argc, char *argv[])
747 {
748 int i;
749 char *from_file = NULL;
750
751 opterr = 0;
752 while ((i=getopt_long(argc, argv, PARSE_FLAGS, long_opts, NULL)) != -1) {
753 switch (i) {
754
755 case 'V':
756 printf("%s compiled %s\n%s\n"
757 "%s written for Gentoo Linux by <solar and vapier @ gentoo.org>\n",
758 __FILE__, __DATE__, rcsid, argv0);
759 exit(EXIT_SUCCESS);
760 break;
761 case 'h': usage(EXIT_SUCCESS); break;
762 case 'f':
763 if (from_file == NULL)
764 from_file = xstrdup(optarg);
765 break;
766 case 'o': {
767 FILE *fp = NULL;
768 fp = freopen(optarg, "w", stdout);
769 if (fp == NULL)
770 err("Could not open output stream '%s': %s", optarg, strerror(errno));
771 stdout = fp;
772 break;
773 }
774
775 case 's': {
776 size_t len;
777 find_sym = xstrdup(optarg);
778 len = strlen(find_sym) + 1;
779 versioned_symname = (char*)xmalloc(sizeof(char) * (len+1));
780 sprintf(versioned_symname, "%s@", find_sym);
781 break;
782 }
783
784 case 'F': {
785 out_format = xstrdup(optarg);
786 break;
787 }
788
789 case 'y': scan_symlink = 0; break;
790 case 'B': show_banner = 0; break;
791 case 'l': scan_ldpath = 1; break;
792 case 'p': scan_envpath = 1; break;
793 case 'R': dir_recurse = 1; break;
794 case 'm': dir_crossmount = 0; break;
795 case 'x': show_pax = 1; break;
796 case 'e': show_stack = 1; break;
797 case 't': show_textrel = 1; break;
798 case 'r': show_rpath = 1; break;
799 case 'n': show_needed = 1; break;
800 case 'i': show_interp = 1; break;
801 case 'b': show_bind = 1; break;
802 case 'q': be_quiet = 1; break;
803 case 'v': be_verbose = (be_verbose % 20) + 1; break;
804 case 'a': show_pax = show_stack = show_textrel = show_rpath = \
805 show_needed = show_interp = show_bind = 1; break;
806
807 case ':':
808 err("Option missing parameter\n");
809 case '?':
810 err("Unknown option\n");
811 default:
812 err("Unhandled option '%c'", i);
813 }
814 }
815
816 /* let the format option override all other options */
817 if (out_format) {
818 show_pax = show_stack = show_textrel = show_rpath = \
819 show_needed = show_interp = show_bind = 0;
820 for (i = 0; out_format[i]; ++i) {
821 if (out_format[i] != '%') continue;
822
823 switch (out_format[++i]) {
824 case '%': break;
825 case 'F': break;
826 case 's': break;
827 case 'o': break;
828 case 'x': show_pax = 1; break;
829 case 'e': show_stack = 1; break;
830 case 't': show_textrel = 1; break;
831 case 'r': show_rpath = 1; break;
832 case 'n': show_needed = 1; break;
833 case 'i': show_interp = 1; break;
834 case 'b': show_bind = 1; break;
835 default:
836 err("Invalid format specifier '%c' (byte %i)",
837 out_format[i], i+1);
838 }
839 }
840
841 /* construct our default format */
842 } else {
843 size_t fmt_len = 30;
844 out_format = (char*)xmalloc(sizeof(char) * fmt_len);
845 if (!be_quiet) xstrcat(&out_format, "%o ", &fmt_len);
846 if (show_pax) xstrcat(&out_format, "%x ", &fmt_len);
847 if (show_stack) xstrcat(&out_format, "%e ", &fmt_len);
848 if (show_textrel) xstrcat(&out_format, "%t ", &fmt_len);
849 if (show_rpath) xstrcat(&out_format, "%r ", &fmt_len);
850 if (show_needed) xstrcat(&out_format, "%n ", &fmt_len);
851 if (show_interp) xstrcat(&out_format, "%i ", &fmt_len);
852 if (show_bind) xstrcat(&out_format, "%b ", &fmt_len);
853 if (find_sym) xstrcat(&out_format, "%s ", &fmt_len);
854 if (!be_quiet) xstrcat(&out_format, "%F ", &fmt_len);
855 }
856 if (be_verbose > 2) printf("Format: %s\n", out_format);
857
858 /* now lets actually do the scanning */
859 if (scan_ldpath || (show_rpath && be_quiet))
860 load_ld_so_conf();
861 if (scan_ldpath) scanelf_ldpath();
862 if (scan_envpath) scanelf_envpath();
863 if (from_file) {
864 scanelf_from_file(from_file);
865 free(from_file);
866 from_file = *argv;
867 }
868 if (optind == argc && !scan_ldpath && !scan_envpath && !from_file)
869 err("Nothing to scan !?");
870 while (optind < argc)
871 scanelf_dir(argv[optind++]);
872
873 /* clean up */
874 if (find_sym) {
875 free(find_sym);
876 free(versioned_symname);
877 }
878 if (out_format) free(out_format);
879 for (i = 0; ldpaths[i]; ++i)
880 free(ldpaths[i]);
881 }
882
883
884
885 /* utility funcs */
886 static char *xstrdup(const char *s)
887 {
888 char *ret = strdup(s);
889 if (!ret) err("Could not strdup(): %s", strerror(errno));
890 return ret;
891 }
892
893 static void *xmalloc(size_t size)
894 {
895 void *ret = malloc(size);
896 if (!ret) err("Could not malloc() %li bytes", (unsigned long)size);
897 return ret;
898 }
899
900 static void xstrcat(char **dst, const char *src, size_t *curr_len)
901 {
902 long new_len;
903
904 new_len = strlen(*dst) + strlen(src);
905 if (*curr_len <= new_len) {
906 *curr_len = new_len + (*curr_len / 2);
907 *dst = realloc(*dst, *curr_len);
908 if (!*dst)
909 err("could not realloc %li bytes", (unsigned long)*curr_len);
910 }
911
912 strcat(*dst, src);
913 }
914
915 static inline void xchrcat(char **dst, const char append, size_t *curr_len)
916 {
917 static char my_app[2];
918 my_app[0] = append;
919 my_app[1] = '\0';
920 xstrcat(dst, my_app, curr_len);
921 }
922
923
924 int main(int argc, char *argv[])
925 {
926 if (argc < 2)
927 usage(EXIT_FAILURE);
928 parseargs(argc, argv);
929 fclose(stdout);
930 return EXIT_SUCCESS;
931 }

  ViewVC Help
Powered by ViewVC 1.1.20