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

Contents of /pax-utils/scanelf.c

Parent Directory Parent Directory | Revision Log Revision Log


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

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
74
75
76 /* scan an elf file and show all the fun stuff */
77 static void scanelf_file(const char *filename)
78 {
79 int i;
80 char found_pax, found_stack, found_relro, found_textrel, found_rpath;
81 elfobj *elf;
82
83 found_pax = found_stack = found_relro = found_textrel = found_rpath = 0;
84
85 /* verify this is real ELF */
86 if ((elf = readelf(filename)) == NULL) {
87 if (be_verbose > 2) printf("%s: not an ELF\n", filename);
88 return;
89 }
90
91 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
98 /* show the header */
99 if (!be_quiet && show_banner) {
100 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 show_banner = 0;
107 }
108
109 /* dump all the good stuff */
110 if (!be_quiet)
111 printf("%-7s ", get_elfetype(elf));
112
113 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
121 /* stack fun */
122 if (show_stack) {
123 #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 }
139 SHOW_STACK(32)
140 SHOW_STACK(64)
141 if (!be_quiet && !found_stack) printf("--- ");
142 if (!be_quiet && !found_relro) printf("--- ");
143 }
144
145 /* textrel fun */
146 if (show_textrel) {
147 #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 if (!be_quiet && !found_textrel) printf("------- ");
167 }
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 char *rpath, *runpath;
173 void *strtbl_void = elf_findsecbyname(elf, ".dynstr");
174 rpath = runpath = NULL;
175
176 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 }
200 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 }
212
213 if (!be_quiet || found_pax || found_stack || found_textrel || found_rpath)
214 puts(filename);
215
216 unreadelf(elf);
217 }
218
219 /* scan a directory for ET_EXEC files and print when we find one */
220 static void scanelf_dir(const char *path)
221 {
222 register DIR *dir;
223 register struct dirent *dentry;
224 struct stat st_top, st;
225 char buf[_POSIX_PATH_MAX];
226 size_t len = 0;
227
228 /* make sure path exists */
229 if (lstat(path, &st_top) == -1)
230 return;
231
232 /* ok, if it isn't a directory, assume we can open it */
233 if (!S_ISDIR(st_top.st_mode)) {
234 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 if (be_verbose) printf("%s: scanning dir\n", path);
244
245 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 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 if (S_ISREG(st.st_mode))
256 scanelf_file(buf);
257 else if (dir_recurse && S_ISDIR(st.st_mode)) {
258 if (dir_crossmount || (st_top.st_dev == st.st_dev))
259 scanelf_dir(buf);
260 }
261 }
262 }
263 closedir(dir);
264 }
265
266 /* scan /etc/ld.so.conf for paths */
267 static void scanelf_ldpath()
268 {
269 char scan_l, scan_ul, scan_ull;
270 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 scan_l = scan_ul = scan_ull = 0;
277
278 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 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 scanelf_dir(path);
289 }
290 free(path);
291
292 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 fclose(fp);
297 }
298
299 /* scan env PATH for paths */
300 static void scanelf_envpath()
301 {
302 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
316 free(path);
317 }
318
319
320
321 /* usage / invocation handling functions */
322 #define PARSE_FLAGS "plRmxetraqvo:BhV"
323 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 {"mount", no_argument, NULL, 'm'},
328 {"pax", no_argument, NULL, 'x'},
329 {"header", no_argument, NULL, 'e'},
330 {"textrel", no_argument, NULL, 't'},
331 {"rpath", no_argument, NULL, 'r'},
332 {"all", no_argument, NULL, 'a'},
333 {"quiet", no_argument, NULL, 'q'},
334 {"verbose", no_argument, NULL, 'v'},
335 {"file",required_argument, NULL, 'o'},
336 {"nobanner", no_argument, NULL, 'B'},
337 {"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 "Scan directories recursively",
345 "Don't recursively cross mount points\n",
346 "Print PaX markings",
347 "Print GNU_STACK markings",
348 "Print TEXTREL information",
349 "Print RPATH information",
350 "Print all scanned info (-x -e -t -r)\n",
351 "Only output 'bad' things",
352 "Be verbose (can be specified more than once)",
353 "Write output stream to a filename",
354 "Don't display the header",
355 "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 {
363 int i;
364 printf(" Scan ELF binaries for stuff\n\n"
365 "Usage: %s [options] <dir1> [dir2 dirN ...]\n\n", argv0);
366 printf("Options:\n");
367 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 #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 exit(status);
376 }
377
378 /* parse command line arguments and preform needed actions */
379 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 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 exit(EXIT_SUCCESS);
392 break;
393 case 's': /* reserved for -s, --symbol= */
394 case 'h': usage(EXIT_SUCCESS); break;
395
396 case 'o': {
397 FILE *fp = NULL;
398 fp = freopen(optarg, "w", stdout);
399 if (fp == NULL)
400 err("Could not open output stream '%s': %s", optarg, strerror(errno));
401 stdout = fp;
402 break;
403 }
404
405 case 'B': show_banner = 0; break;
406 case 'l': scan_ldpath = 1; break;
407 case 'p': scan_envpath = 1; break;
408 case 'R': dir_recurse = 1; break;
409 case 'm': dir_crossmount = 0; break;
410 case 'x': show_pax = 1; break;
411 case 'e': show_stack = 1; break;
412 case 't': show_textrel = 1; break;
413 case 'r': show_rpath = 1; break;
414 case 'q': be_quiet = 1; break;
415 case 'v': be_verbose = (be_verbose % 20) + 1; break;
416 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 if (be_quiet && be_verbose)
433 err("You can be quiet or you can be verbose, not both, stupid");
434
435 if (scan_ldpath) scanelf_ldpath();
436 if (scan_envpath) scanelf_envpath();
437 if (optind == argc && !scan_ldpath && !scan_envpath)
438 err("Nothing to scan !?");
439 while (optind < argc)
440 scanelf_dir(argv[optind++]);
441 }
442
443
444
445 int main(int argc, char *argv[])
446 {
447 if (argc < 2)
448 usage(EXIT_FAILURE);
449 parseargs(argc, argv);
450 fclose(stdout);
451 return EXIT_SUCCESS;
452 }

  ViewVC Help
Powered by ViewVC 1.1.20