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

Contents of /pax-utils/scanelf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.27 - (show annotations) (download) (as text)
Tue Apr 5 01:44:08 2005 UTC (9 years, 6 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 /*
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.25 2005/04/03 18:56: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 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
31 #include <dirent.h>
32 #include <getopt.h>
33 #include <assert.h>
34
35 #include "paxelf.h"
36
37 static const char *rcsid = "$Id: scanelf.c,v 1.25 2005/04/03 18:56:08 vapier Exp $";
38
39
40 /* helper functions for showing errors */
41 #define argv0 "scanelf" /*((*argv != NULL) ? argv[0] : __FILE__ "\b\b")*/
42 #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 static char dir_crossmount = 1;
66 static char show_pax = 0;
67 static char show_stack = 0;
68 static char show_textrel = 0;
69 static char show_rpath = 0;
70 static char show_banner = 1;
71 static char be_quiet = 0;
72 static char be_verbose = 0;
73 static char *find_sym = NULL;
74
75
76
77 /* scan an elf file and show all the fun stuff */
78 static void scanelf_file(const char *filename)
79 {
80 int i;
81 char found_pax, found_stack, found_relro, found_textrel, found_rpath, found_sym;
82 elfobj *elf;
83
84 found_pax = found_stack = found_relro = found_textrel = found_rpath = found_sym = 0;
85
86 /* verify this is real ELF */
87 if ((elf = readelf(filename)) == NULL) {
88 if (be_verbose > 2) printf("%s: not an ELF\n", filename);
89 return;
90 }
91
92 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
99 /* show the header */
100 if (!be_quiet && show_banner) {
101 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 show_banner = 0;
108 }
109
110 /* dump all the good stuff */
111 if (!be_quiet)
112 printf("%-7s ", get_elfetype(elf));
113
114 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
122 /* stack fun */
123 if (show_stack) {
124 #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 }
140 SHOW_STACK(32)
141 SHOW_STACK(64)
142 if (!be_quiet && !found_stack) printf("--- ");
143 if (!be_quiet && !found_relro) printf("--- ");
144 }
145
146 /* textrel fun */
147 if (show_textrel) {
148 #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 if (!be_quiet && !found_textrel) printf("------- ");
168 }
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 char *rpath, *runpath;
174 void *strtbl_void = elf_findsecbyname(elf, ".dynstr");
175 rpath = runpath = NULL;
176
177 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 }
201 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 }
213
214 if (!be_quiet || found_pax || found_stack || found_textrel || found_rpath)
215 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
249 unreadelf(elf);
250 }
251
252 /* scan a directory for ET_EXEC files and print when we find one */
253 static void scanelf_dir(const char *path)
254 {
255 register DIR *dir;
256 register struct dirent *dentry;
257 struct stat st_top, st;
258 char buf[_POSIX_PATH_MAX];
259 size_t len = 0;
260
261 /* make sure path exists */
262 if (lstat(path, &st_top) == -1)
263 return;
264
265 /* ok, if it isn't a directory, assume we can open it */
266 if (!S_ISDIR(st_top.st_mode)) {
267 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 if (be_verbose) printf("%s: scanning dir\n", path);
277
278 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 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 if (S_ISREG(st.st_mode))
289 scanelf_file(buf);
290 else if (dir_recurse && S_ISDIR(st.st_mode)) {
291 if (dir_crossmount || (st_top.st_dev == st.st_dev))
292 scanelf_dir(buf);
293 }
294 }
295 }
296 closedir(dir);
297 }
298
299 /* scan /etc/ld.so.conf for paths */
300 static void scanelf_ldpath()
301 {
302 char scan_l, scan_ul, scan_ull;
303 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 scan_l = scan_ul = scan_ull = 0;
310
311 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 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 scanelf_dir(path);
322 }
323 free(path);
324
325 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 fclose(fp);
330 }
331
332 /* scan env PATH for paths */
333 static void scanelf_envpath()
334 {
335 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
349 free(path);
350 }
351
352
353
354 /* usage / invocation handling functions */
355 #define PARSE_FLAGS "plRmxetrs:aqvo:BhV"
356 #define a_argument required_argument
357 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 {"mount", no_argument, NULL, 'm'},
362 {"pax", no_argument, NULL, 'x'},
363 {"header", no_argument, NULL, 'e'},
364 {"textrel", no_argument, NULL, 't'},
365 {"rpath", no_argument, NULL, 'r'},
366 {"symbol", a_argument, NULL, 's'},
367 {"all", no_argument, NULL, 'a'},
368 {"quiet", no_argument, NULL, 'q'},
369 {"verbose", no_argument, NULL, 'v'},
370 {"file", a_argument, NULL, 'o'},
371 {"nobanner", no_argument, NULL, 'B'},
372 {"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 "Scan directories recursively",
380 "Don't recursively cross mount points\n",
381 "Print PaX markings",
382 "Print GNU_STACK markings",
383 "Print TEXTREL information",
384 "Print RPATH information",
385 "Find a specified symbol",
386 "Print all scanned info (-x -e -t -r)\n",
387 "Only output 'bad' things",
388 "Be verbose (can be specified more than once)",
389 "Write output stream to a filename",
390 "Don't display the header",
391 "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 {
399 int i;
400 printf(" Scan ELF binaries for stuff\n"
401 "Usage: %s [options] <dir1> [dir2 dirN ...]\n\n", argv0);
402 printf("Options:\n");
403 for (i = 0; long_opts[i].name; ++i)
404 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 exit(status);
411 }
412
413 /* parse command line arguments and preform needed actions */
414 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 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 exit(EXIT_SUCCESS);
427 break;
428 case 'h': usage(EXIT_SUCCESS); break;
429
430 case 'o': {
431 FILE *fp = NULL;
432 fp = freopen(optarg, "w", stdout);
433 if (fp == NULL)
434 err("Could not open output stream '%s': %s", optarg, strerror(errno));
435 stdout = fp;
436 break;
437 }
438
439 case 's': find_sym = strdup(optarg); break;
440
441 case 'B': show_banner = 0; break;
442 case 'l': scan_ldpath = 1; break;
443 case 'p': scan_envpath = 1; break;
444 case 'R': dir_recurse = 1; break;
445 case 'm': dir_crossmount = 0; break;
446 case 'x': show_pax = 1; break;
447 case 'e': show_stack = 1; break;
448 case 't': show_textrel = 1; break;
449 case 'r': show_rpath = 1; break;
450 case 'q': be_quiet = 1; break;
451 case 'v': be_verbose = (be_verbose % 20) + 1; break;
452 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 if (be_quiet && be_verbose)
469 err("You can be quiet or you can be verbose, not both, stupid");
470
471 if (scan_ldpath) scanelf_ldpath();
472 if (scan_envpath) scanelf_envpath();
473 if (optind == argc && !scan_ldpath && !scan_envpath)
474 err("Nothing to scan !?");
475 while (optind < argc)
476 scanelf_dir(argv[optind++]);
477
478 if (find_sym) free(find_sym);
479 }
480
481
482
483 int main(int argc, char *argv[])
484 {
485 if (argc < 2)
486 usage(EXIT_FAILURE);
487 parseargs(argc, argv);
488 fclose(stdout);
489 return EXIT_SUCCESS;
490 }

  ViewVC Help
Powered by ViewVC 1.1.20