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.27 2005/04/05 01:44:08 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.27 2005/04/05 01:44:08 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 found_sym = 0; |
218 |
char *versioned_symname = malloc(strlen(find_sym)+1); |
219 |
|
220 |
sprintf(versioned_symname, "%s@", find_sym); |
221 |
symtab_void = elf_findsecbyname(elf, ".symtab"); |
222 |
strtab_void = elf_findsecbyname(elf, ".strtab"); |
223 |
|
224 |
if (symtab_void && strtab_void) { |
225 |
#define FIND_SYM(B) \ |
226 |
if (elf->elf_class == ELFCLASS ## B) { \ |
227 |
Elf ## B ## _Shdr *symtab = SHDR ## B (symtab_void); \ |
228 |
Elf ## B ## _Shdr *strtab = SHDR ## B (strtab_void); \ |
229 |
Elf ## B ## _Sym *sym = SYM ## B (elf->data + EGET(symtab->sh_offset)); \ |
230 |
int cnt = EGET(symtab->sh_size) / EGET(symtab->sh_entsize); \ |
231 |
char *symname; \ |
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 == '*') { \ |
236 |
printf("%s(%s) %5lX %15s %s\n", ((found_sym == 0) ? "\n\t" : "\t"), \ |
237 |
(char *) basename(filename), \ |
238 |
(long)sym->st_size, (char *) get_elfstttype(sym->st_info & 0xF), \ |
239 |
symname); \ |
240 |
found_sym = 1; \ |
241 |
} \ |
242 |
if ((strcmp(find_sym, symname) == 0) || \ |
243 |
(strncmp(symname, versioned_symname, strlen(versioned_symname)) == 0)) \ |
244 |
found_sym++; \ |
245 |
} \ |
246 |
++sym; \ |
247 |
} } |
248 |
FIND_SYM(32) |
249 |
FIND_SYM(64) |
250 |
} |
251 |
free(versioned_symname); |
252 |
if (*find_sym != '*') |
253 |
printf(" %s ", (found_sym == 0) ? "-" : find_sym); |
254 |
} |
255 |
|
256 |
if (!be_quiet || found_pax || found_stack || found_textrel || found_rpath) |
257 |
printf("%s\n", filename); |
258 |
|
259 |
unreadelf(elf); |
260 |
} |
261 |
|
262 |
/* scan a directory for ET_EXEC files and print when we find one */ |
263 |
static void scanelf_dir(const char *path) |
264 |
{ |
265 |
register DIR *dir; |
266 |
register struct dirent *dentry; |
267 |
struct stat st_top, st; |
268 |
char buf[_POSIX_PATH_MAX]; |
269 |
size_t len = 0; |
270 |
|
271 |
/* make sure path exists */ |
272 |
if (lstat(path, &st_top) == -1) |
273 |
return; |
274 |
|
275 |
/* ok, if it isn't a directory, assume we can open it */ |
276 |
if (!S_ISDIR(st_top.st_mode)) { |
277 |
scanelf_file(path); |
278 |
return; |
279 |
} |
280 |
|
281 |
/* now scan the dir looking for fun stuff */ |
282 |
if ((dir = opendir(path)) == NULL) { |
283 |
warnf("could not opendir %s: %s", path, strerror(errno)); |
284 |
return; |
285 |
} |
286 |
if (be_verbose) printf("%s: scanning dir\n", path); |
287 |
|
288 |
while ((dentry = readdir(dir))) { |
289 |
if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, "..")) |
290 |
continue; |
291 |
len = (strlen(path) + 2 + strlen(dentry->d_name)); |
292 |
assert(len < sizeof(buf)); |
293 |
strncpy(buf, path, len); |
294 |
strncat(buf, "/", len); |
295 |
strncat(buf, dentry->d_name, len); |
296 |
buf[sizeof(buf)] = 0; |
297 |
if (lstat(buf, &st) != -1) { |
298 |
if (S_ISREG(st.st_mode)) |
299 |
scanelf_file(buf); |
300 |
else if (dir_recurse && S_ISDIR(st.st_mode)) { |
301 |
if (dir_crossmount || (st_top.st_dev == st.st_dev)) |
302 |
scanelf_dir(buf); |
303 |
} |
304 |
} |
305 |
} |
306 |
closedir(dir); |
307 |
} |
308 |
|
309 |
/* scan /etc/ld.so.conf for paths */ |
310 |
static void scanelf_ldpath() |
311 |
{ |
312 |
char scan_l, scan_ul, scan_ull; |
313 |
char *path, *p; |
314 |
FILE *fp; |
315 |
|
316 |
if ((fp = fopen("/etc/ld.so.conf", "r")) == NULL) |
317 |
err("Unable to open ld.so.conf: %s", strerror(errno)); |
318 |
|
319 |
scan_l = scan_ul = scan_ull = 0; |
320 |
|
321 |
path = malloc(_POSIX_PATH_MAX); |
322 |
while ((fgets(path, _POSIX_PATH_MAX, fp)) != NULL) |
323 |
if (*path == '/') { |
324 |
if ((p = strrchr(path, '\r')) != NULL) |
325 |
*p = 0; |
326 |
if ((p = strrchr(path, '\n')) != NULL) |
327 |
*p = 0; |
328 |
if (!scan_l && !strcmp(path, "/lib")) scan_l = 1; |
329 |
if (!scan_ul && !strcmp(path, "/usr/lib")) scan_ul = 1; |
330 |
if (!scan_ull && !strcmp(path, "/usr/local/lib")) scan_ull = 1; |
331 |
scanelf_dir(path); |
332 |
} |
333 |
free(path); |
334 |
|
335 |
if (!scan_l) scanelf_dir("/lib"); |
336 |
if (!scan_ul) scanelf_dir("/usr/lib"); |
337 |
if (!scan_ull) scanelf_dir("/usr/local/lib"); |
338 |
|
339 |
fclose(fp); |
340 |
} |
341 |
|
342 |
/* scan env PATH for paths */ |
343 |
static void scanelf_envpath() |
344 |
{ |
345 |
char *path, *p; |
346 |
|
347 |
path = getenv("PATH"); |
348 |
if (!path) |
349 |
err("PATH is not set in your env !"); |
350 |
|
351 |
if ((path = strdup(path)) == NULL) |
352 |
err("stdup failed: %s", strerror(errno)); |
353 |
|
354 |
while ((p = strrchr(path, ':')) != NULL) { |
355 |
scanelf_dir(p + 1); |
356 |
*p = 0; |
357 |
} |
358 |
|
359 |
free(path); |
360 |
} |
361 |
|
362 |
|
363 |
|
364 |
/* usage / invocation handling functions */ |
365 |
#define PARSE_FLAGS "plRmxetrs:aqvo:BhV" |
366 |
#define a_argument required_argument |
367 |
static struct option const long_opts[] = { |
368 |
{"path", no_argument, NULL, 'p'}, |
369 |
{"ldpath", no_argument, NULL, 'l'}, |
370 |
{"recursive", no_argument, NULL, 'R'}, |
371 |
{"mount", no_argument, NULL, 'm'}, |
372 |
{"pax", no_argument, NULL, 'x'}, |
373 |
{"header", no_argument, NULL, 'e'}, |
374 |
{"textrel", no_argument, NULL, 't'}, |
375 |
{"rpath", no_argument, NULL, 'r'}, |
376 |
{"symbol", a_argument, NULL, 's'}, |
377 |
{"all", no_argument, NULL, 'a'}, |
378 |
{"quiet", no_argument, NULL, 'q'}, |
379 |
{"verbose", no_argument, NULL, 'v'}, |
380 |
{"file", a_argument, NULL, 'o'}, |
381 |
{"nobanner", no_argument, NULL, 'B'}, |
382 |
{"help", no_argument, NULL, 'h'}, |
383 |
{"version", no_argument, NULL, 'V'}, |
384 |
{NULL, no_argument, NULL, 0x0} |
385 |
}; |
386 |
static char *opts_help[] = { |
387 |
"Scan all directories in PATH environment", |
388 |
"Scan all directories in /etc/ld.so.conf", |
389 |
"Scan directories recursively", |
390 |
"Don't recursively cross mount points\n", |
391 |
"Print PaX markings", |
392 |
"Print GNU_STACK markings", |
393 |
"Print TEXTREL information", |
394 |
"Print RPATH information", |
395 |
"Find a specified symbol", |
396 |
"Print all scanned info (-x -e -t -r)\n", |
397 |
"Only output 'bad' things", |
398 |
"Be verbose (can be specified more than once)", |
399 |
"Write output stream to a filename", |
400 |
"Don't display the header", |
401 |
"Print this help and exit", |
402 |
"Print version and exit", |
403 |
NULL |
404 |
}; |
405 |
|
406 |
/* display usage and exit */ |
407 |
static void usage(int status) |
408 |
{ |
409 |
int i; |
410 |
printf("¤ Scan ELF binaries for stuff\n" |
411 |
"Usage: %s [options] <dir1> [dir2 dirN ...]\n\n", argv0); |
412 |
printf("Options:\n"); |
413 |
for (i = 0; long_opts[i].name; ++i) |
414 |
if (long_opts[i].has_arg == no_argument) |
415 |
printf(" -%c, --%-13s× %s\n", long_opts[i].val, |
416 |
long_opts[i].name, opts_help[i]); |
417 |
else |
418 |
printf(" -%c, --%-6s <arg> × %s\n", long_opts[i].val, |
419 |
long_opts[i].name, opts_help[i]); |
420 |
exit(status); |
421 |
} |
422 |
|
423 |
/* parse command line arguments and preform needed actions */ |
424 |
static void parseargs(int argc, char *argv[]) |
425 |
{ |
426 |
int flag; |
427 |
|
428 |
opterr = 0; |
429 |
while ((flag=getopt_long(argc, argv, PARSE_FLAGS, long_opts, NULL)) != -1) { |
430 |
switch (flag) { |
431 |
|
432 |
case 'V': /* version info */ |
433 |
printf("%s compiled %s\n%s\n" |
434 |
"%s written for Gentoo Linux by <solar and vapier @ gentoo.org>\n", |
435 |
__FILE__, __DATE__, rcsid, argv0); |
436 |
exit(EXIT_SUCCESS); |
437 |
break; |
438 |
case 'h': usage(EXIT_SUCCESS); break; |
439 |
|
440 |
case 'o': { |
441 |
FILE *fp = NULL; |
442 |
fp = freopen(optarg, "w", stdout); |
443 |
if (fp == NULL) |
444 |
err("Could not open output stream '%s': %s", optarg, strerror(errno)); |
445 |
stdout = fp; |
446 |
break; |
447 |
} |
448 |
|
449 |
case 's': find_sym = strdup(optarg); break; |
450 |
|
451 |
case 'B': show_banner = 0; break; |
452 |
case 'l': scan_ldpath = 1; break; |
453 |
case 'p': scan_envpath = 1; break; |
454 |
case 'R': dir_recurse = 1; break; |
455 |
case 'm': dir_crossmount = 0; break; |
456 |
case 'x': show_pax = 1; break; |
457 |
case 'e': show_stack = 1; break; |
458 |
case 't': show_textrel = 1; break; |
459 |
case 'r': show_rpath = 1; break; |
460 |
case 'q': be_quiet = 1; break; |
461 |
case 'v': be_verbose = (be_verbose % 20) + 1; break; |
462 |
case 'a': show_pax = show_stack = show_textrel = show_rpath = 1; break; |
463 |
|
464 |
case ':': |
465 |
warn("Option missing parameter"); |
466 |
usage(EXIT_FAILURE); |
467 |
break; |
468 |
case '?': |
469 |
warn("Unknown option"); |
470 |
usage(EXIT_FAILURE); |
471 |
break; |
472 |
default: |
473 |
err("Unhandled option '%c'", flag); |
474 |
break; |
475 |
} |
476 |
} |
477 |
|
478 |
if (be_quiet && be_verbose) |
479 |
err("You can be quiet or you can be verbose, not both, stupid"); |
480 |
|
481 |
if (scan_ldpath) scanelf_ldpath(); |
482 |
if (scan_envpath) scanelf_envpath(); |
483 |
if (optind == argc && !scan_ldpath && !scan_envpath) |
484 |
err("Nothing to scan !?"); |
485 |
while (optind < argc) |
486 |
scanelf_dir(argv[optind++]); |
487 |
|
488 |
if (find_sym) free(find_sym); |
489 |
} |
490 |
|
491 |
|
492 |
|
493 |
int main(int argc, char *argv[]) |
494 |
{ |
495 |
if (argc < 2) |
496 |
usage(EXIT_FAILURE); |
497 |
parseargs(argc, argv); |
498 |
fclose(stdout); |
499 |
return EXIT_SUCCESS; |
500 |
} |