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

Diff of /pax-utils/scanelf.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.5 Revision 1.18
1/* 1/*
2 * Copyright 2003 Ned Ludd <solar@gentoo.org> 2 * Copyright 2003 Ned Ludd <solar@gentoo.org>
3 * Copyright 1999-2003 Gentoo Technologies, Inc. 3 * Copyright 1999-2005 Gentoo Foundation
4 * Distributed under the terms of the GNU General Public License v2 4 * Distributed under the terms of the GNU General Public License v2
5 * $Header: /var/cvsroot/gentoo-projects/pax-utils/Attic/scanelf.c,v 1.5 2003/11/09 17:46:41 solar Exp $ 5 * $Header: /var/cvsroot/gentoo-projects/pax-utils/Attic/scanelf.c,v 1.18 2005/04/02 03:25:38 vapier Exp $
6 * 6 *
7 ******************************************************************** 7 ********************************************************************
8 * This program is free software; you can redistribute it and/or 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 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 10 * published by the Free Software Foundation; either version 2 of the
23 23
24#include <stdio.h> 24#include <stdio.h>
25#include <stdlib.h> 25#include <stdlib.h>
26#include <sys/types.h> 26#include <sys/types.h>
27#include <string.h> 27#include <string.h>
28#include <errno.h>
28#include <unistd.h> 29#include <unistd.h>
29#include <sys/stat.h> 30#include <sys/stat.h>
30#include <dirent.h> 31#include <dirent.h>
31#include <getopt.h> 32#include <getopt.h>
32 33
33#include "paxelf.h" 34#include "paxelf.h"
34 35
35static const char *rcsid = 36static const char *rcsid = "$Id: scanelf.c,v 1.18 2005/04/02 03:25:38 vapier Exp $";
36 "$Id: scanelf.c,v 1.5 2003/11/09 17:46:41 solar Exp $";
37 37
38#define PARSE_FLAGS "hvlp" 38
39/* helper functions for showing errors */
40#define argv0 "scanelf" /*((*argv != NULL) ? argv[0] : __FILE__ "\b\b")*/
41#define warn(fmt, args...) \
42 fprintf(stderr, "%s: " fmt "\n", argv0, ## args)
43#define warnf(fmt, args...) warn("%s(): " fmt, __FUNCTION__, ## args)
44#define err(fmt, args...) \
45 do { \
46 warn(fmt, ## args); \
47 exit(EXIT_FAILURE); \
48 } while (0)
49
50
51
52/* prototypes */
53static void scanelf_file(const char *filename);
54static void scanelf_dir(const char *path);
55static void scanelf_ldpath();
56static void scanelf_envpath();
57static void usage(int status);
58static void parseargs(int argc, char *argv[]);
59
60/* variables to control behavior */
61static char scan_ldpath = 0;
62static char scan_envpath = 0;
63static char dir_recurse = 0;
64static char dir_crossmount = 1;
65static char show_pax = 0;
66static char show_stack = 0;
67static char show_textrel = 0;
68static char show_rpath = 0;
69static char show_banner = 1;
70static char be_quiet = 0;
71static char be_verbose = 0;
72
73
74
75/* scan an elf file and show all the fun stuff */
76static void scanelf_file(const char *filename)
77{
78 int i;
79 char found_pax, found_stack, found_relro, found_textrel, found_rpath;
80 Elf_Dyn *dyn;
81 elfobj *elf = NULL;
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 > 1) printf("%s: not an ELF\n", filename);
88 return;
89 }
90 if (check_elf_header(elf->ehdr) || !IS_ELF(elf)) {
91 if (be_verbose > 1) printf("%s: cannot handle ELF :(\n", filename);
92 goto bail;
93 }
94
95 if (be_verbose) printf("%s: scanning file\n", filename);
96
97 /* show the header */
98 if (!be_quiet && show_banner) {
99 fputs(" TYPE ", stdout);
100 if (show_pax) fputs(" PAX ", stdout);
101 if (show_stack) fputs(" STK/REL ", stdout);
102 if (show_textrel) fputs("TEXTREL ", stdout);
103 if (show_rpath) fputs("RPATH ", stdout);
104 fputs(" FILE\n", stdout);
105 show_banner = 0;
106 }
107
108 /* dump all the good stuff */
109 if (!be_quiet)
110 printf("%-7s ", get_elfetype(elf->ehdr->e_type));
111
112 if (show_pax) {
113 char *paxflags = pax_short_hf_flags(PAX_FLAGS(elf));
114 if (!be_quiet || (be_quiet && strncmp(paxflags, "PeMRxS", 6))) {
115 found_pax = 1;
116 printf("%s ", pax_short_hf_flags(PAX_FLAGS(elf)));
117 }
118 }
119
120 /* stack fun */
121 if (show_stack) {
122 for (i = 0; i < elf->ehdr->e_phnum; i++) {
123 if (elf->phdr[i].p_type != PT_GNU_STACK && \
124 elf->phdr[i].p_type != PT_GNU_RELRO) continue;
125
126 if (be_quiet && !(elf->phdr[i].p_flags & PF_X))
127 continue;
128
129 if (elf->phdr[i].p_type == PT_GNU_STACK)
130 found_stack = 1;
131 if (elf->phdr[i].p_type == PT_GNU_RELRO)
132 found_relro = 1;
133
134 printf("%s ", gnu_short_stack_flags(elf->phdr[i].p_flags));
135 }
136 if (!be_quiet && !found_stack) fputs("--- ", stdout);
137 if (!be_quiet && !found_relro) fputs("--- ", stdout);
138 }
139
140 /* textrel fun */
141 if (show_textrel) {
142 for (i = 0; i < elf->ehdr->e_phnum; i++) {
143 if (elf->phdr[i].p_type != PT_DYNAMIC) continue;
144
145 dyn = (Elf_Dyn *)(elf->data + elf->phdr[i].p_offset);
146 while (dyn->d_tag != DT_NULL) {
147 if (dyn->d_tag == DT_TEXTREL) { //dyn->d_tag != DT_FLAGS)
148 found_textrel = 1;
149// if (dyn->d_un.d_val & DF_TEXTREL)
150 fputs("TEXTREL ", stdout);
151 }
152 ++dyn;
153 }
154 }
155 if (!be_quiet && !found_textrel) fputs("------- ", stdout);
156 }
157
158 /* rpath fun */
159 /* TODO: if be_quiet, only output RPATH's which aren't in /etc/ld.so.conf */
160 if (show_rpath) {
161 Elf_Shdr *strtbl = elf_findsecbyname(elf, ".dynstr");
162
163 if (strtbl)
164 for (i = 0; i < elf->ehdr->e_phnum; i++) {
165 if (elf->phdr[i].p_type != PT_DYNAMIC) continue;
166
167 dyn = (Elf_Dyn *)(elf->data + elf->phdr[i].p_offset);
168 while (dyn->d_tag != DT_NULL) {
169 if (dyn->d_tag == DT_RPATH) { //|| dyn->d_tag != DT_RUNPATH)
170 char *rpath = elf->data + strtbl->sh_offset + dyn->d_un.d_ptr;
171 found_rpath = 1;
172 printf("%s ", rpath);
173 }
174 ++dyn;
175 }
176 }
177 if (!be_quiet && !found_rpath) fputs(" - ", stdout);
178 }
179
180 if (!be_quiet || found_pax || found_stack || found_textrel || found_rpath)
181 puts(filename);
182
183bail:
184 unreadelf(elf);
185}
186
187/* scan a directory for ET_EXEC files and print when we find one */
188static void scanelf_dir(const char *path)
189{
190 register DIR *dir;
191 register struct dirent *dentry;
192 struct stat st_top, st;
193 char *p;
194 int len = 0;
195
196 /* make sure path exists */
197 if (lstat(path, &st_top) == -1)
198 return;
199
200 /* ok, if it isn't a directory, assume we can open it */
201 if (!S_ISDIR(st_top.st_mode)) {
202 scanelf_file(path);
203 return;
204 }
205
206 /* now scan the dir looking for fun stuff */
207 if ((dir = opendir(path)) == NULL) {
208 warnf("could not opendir %s: %s", path, strerror(errno));
209 return;
210 }
211 if (be_verbose) printf("%s: scanning dir\n", path);
212
213 while ((dentry = readdir(dir))) {
214 if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, ".."))
215 continue;
216 len = (strlen(path) + 2 + strlen(dentry->d_name));
217 p = malloc(len);
218 if (!p)
219 err("scanelf_dir(): Could not malloc: %s", strerror(errno));
220 strncpy(p, path, len);
221 strncat(p, "/", len);
222 strncat(p, dentry->d_name, len);
223 if (lstat(p, &st) != -1) {
224 if (S_ISREG(st.st_mode))
225 scanelf_file(p);
226 else if (dir_recurse && S_ISDIR(st.st_mode)) {
227 if (dir_crossmount || (st_top.st_dev == st.st_dev))
228 scanelf_dir(p);
229 }
230 }
231 free(p);
232 }
233 closedir(dir);
234}
235
236/* scan /etc/ld.so.conf for paths */
237static void scanelf_ldpath()
238{
239 char scan_l, scan_ul, scan_ull;
240 char *path, *p;
241 FILE *fp;
242
243 if ((fp = fopen("/etc/ld.so.conf", "r")) == NULL)
244 err("Unable to open ld.so.conf: %s", strerror(errno));
245
246 scan_l = scan_ul = scan_ull = 0;
247
248 path = malloc(_POSIX_PATH_MAX);
249 while ((fgets(path, _POSIX_PATH_MAX, fp)) != NULL)
250 if (*path == '/') {
251 if ((p = strrchr(path, '\r')) != NULL)
252 *p = 0;
253 if ((p = strrchr(path, '\n')) != NULL)
254 *p = 0;
255 if (!scan_l && !strcmp(path, "/lib")) scan_l = 1;
256 if (!scan_ul && !strcmp(path, "/usr/lib")) scan_ul = 1;
257 if (!scan_ull && !strcmp(path, "/usr/local/lib")) scan_ull = 1;
258 scanelf_dir(path);
259 }
260 free(path);
261
262 if (!scan_l) scanelf_dir("/lib");
263 if (!scan_ul) scanelf_dir("/usr/lib");
264 if (!scan_ull) scanelf_dir("/usr/local/lib");
265
266 fclose(fp);
267}
268
269/* scan env PATH for paths */
270static void scanelf_envpath()
271{
272 char *path, *p;
273
274 path = getenv("PATH");
275 if (!path)
276 err("PATH is not set in your env !");
277
278 if ((path = strdup(path)) == NULL)
279 err("stdup failed: %s", strerror(errno));
280
281 while ((p = strrchr(path, ':')) != NULL) {
282 scanelf_dir(p + 1);
283 *p = 0;
284 }
285
286 free(path);
287}
288
289
290
291/* usage / invocation handling functions */
292#define PARSE_FLAGS "plRmxetraqvBhV"
39static struct option const long_options[] = { 293static struct option const long_opts[] = {
40 {"help", no_argument, 0, 'h'},
41 {"version", no_argument, 0, 'v'},
42 {"path", no_argument, 0, 'p'}, 294 {"path", no_argument, NULL, 'p'},
43 {"ldpath", no_argument, 0, 'l'}, 295 {"ldpath", no_argument, NULL, 'l'},
296 {"recursive", no_argument, NULL, 'R'},
297 {"mount", no_argument, NULL, 'm'},
298 {"pax", no_argument, NULL, 'x'},
299 {"header", no_argument, NULL, 'e'},
300 {"textrel", no_argument, NULL, 't'},
301 {"rpath", no_argument, NULL, 'r'},
302 {"all", no_argument, NULL, 'a'},
303 {"quiet", no_argument, NULL, 'q'},
304 {"verbose", no_argument, NULL, 'v'},
305 {"nobanner", no_argument, NULL, 'B'},
306 {"help", no_argument, NULL, 'h'},
307 {"version", no_argument, NULL, 'V'},
44 {NULL, no_argument, NULL, 0} 308 {NULL, no_argument, NULL, 0x0}
45}; 309};
46 310static char *opts_help[] = {
47/* scan a directory for ET_EXEC files and print when we find one */ 311 "Scan all directories in PATH environment",
48void scanelf(const char *path) 312 "Scan all directories in /etc/ld.so.conf",
49{ 313 "Scan directories recursively",
50 elfobj *elf = NULL; 314 "Don't recursively cross mount points\n",
51 register DIR *dir; 315 "Print PaX markings",
52 register struct dirent *dentry; 316 "Print GNU_STACK markings",
53 317 "Print TEXTREL information",
54 if ((chdir(path) == 0) && ((dir = opendir(path)))) { 318 "Print RPATH information",
55 while ((dentry = readdir(dir))) { 319 "Print all scanned info (-x -e -t -r)\n",
56 /* verify this is real ELF */ 320 "Only output 'bad' things",
57 if ((elf = readelf(dentry->d_name)) != NULL) { 321 "Be verbose (can be specified more than once)",
58 if (!check_elf_header(elf->ehdr)) 322 "Don't display the header",
59 if (IS_ELF(elf)) 323 "Print this help and exit",
60 printf("%s %s %s/%s\n", 324 "Print version and exit",
61 pax_short_flags(PAX_FLAGS(elf)), 325 NULL
62 get_elfetype(elf->ehdr->e_type), path, 326};
63 dentry->d_name);
64
65 if (elf != NULL) {
66 munmap(elf->data, elf->len);
67 free(elf);
68 elf = NULL;
69 }
70 }
71 }
72 closedir(dir);
73 }
74}
75
76 327
77/* display usage and exit */ 328/* display usage and exit */
78void usage(char **argv) 329static void usage(int status)
79{ 330{
80 fprintf(stderr, 331 int i;
332 printf(" Scan ELF binaries for stuff\n\n"
81 "Usage: %s [options] dir1 dir2 dirN...\n", 333 "Usage: %s [options] <dir1> [dir2 dirN ...]\n\n", argv0);
82 (*argv != NULL) ? argv[0] : __FILE__ "\b\b"); 334 fputs("Options:\n", stdout);
83}
84
85
86void showopt(int c, char *data)
87{
88 int i;
89 for (i = 0; long_options[i].name; i++) 335 for (i = 0; long_opts[i].name; ++i)
90 if (long_options[i].val == c) 336 printf(" -%c, --%-12s %s\n", long_opts[i].val,
91 fprintf(stderr, " -%c, --%s\t: %s\n", c, long_options[i].name, 337 long_opts[i].name, opts_help[i]);
92 data); 338#ifdef MANLYPAGE
339 for (i = 0; long_opts[i].name; ++i)
340 printf(".TP\n\\fB\\-%c, \\-\\-%s\\fR\n%s\n", long_opts[i].val,
341 long_opts[i].name, opts_help[i]);
342#endif
343 exit(status);
93} 344}
94 345
95/* parse command line arguments and preform needed actions */ 346/* parse command line arguments and preform needed actions */
96void parseargs(int argc, char **argv) 347static void parseargs(int argc, char *argv[])
97{ 348{
98 int flag; 349 int flag;
99 char *p, *path;
100 FILE *fp;
101 350
102 opterr = 0; 351 opterr = 0;
103 while ((flag = 352 while ((flag=getopt_long(argc, argv, PARSE_FLAGS, long_opts, NULL)) != -1) {
104 (int) getopt_long(argc, argv,
105 PARSE_FLAGS, long_options, NULL)) != EOF) {
106 switch (flag) { 353 switch (flag) {
107 case 'h': 354
108 usage(argv); 355 case 'V': /* version info */
109 showopt('p', "Scan all directories in PATH environment."); 356 printf("%s compiled %s\n"
110 showopt('l', "Scan all directories in /etc/ld.so.conf"); 357 "%s written for Gentoo Linux by <solar and vapier @ gentoo.org>\n"
111 showopt('h', "Print this help and exit."); 358 "%s\n",
112 showopt('v', "Print version and exit."); 359 __FILE__, __DATE__, argv0, rcsid);
113 exit(EXIT_SUCCESS); 360 exit(EXIT_SUCCESS);
114 case 'v': 361 break;
115 fprintf(stderr, "%s compiled %s\n", __FILE__, __DATE__); 362 case 's': /* reserved for -s, --symbol= */
116 fprintf(stderr, 363 case 'h': usage(EXIT_SUCCESS); break;
117 "%s written for Gentoo Linux <solar@gentoo.org>\n\t%s\n", 364
118 (*argv != NULL) ? argv[0] : __FILE__ "\b\b", rcsid); 365 case 'B': show_banner = 0; break;
119 exit(EXIT_SUCCESS); 366 case 'l': scan_ldpath = 1; break;
120 case 'l': 367 case 'p': scan_envpath = 1; break;
121 /* scan ld.so.conf for ldpath */ 368 case 'R': dir_recurse = 1; break;
122 if ((fp = fopen("/etc/ld.so.conf", "r")) != NULL) { 369 case 'm': dir_crossmount = 0; break;
123 path = malloc(_POSIX_PATH_MAX); 370 case 'x': show_pax = 1; break;
124 while ((fgets(path, _POSIX_PATH_MAX, fp)) != NULL) { 371 case 'e': show_stack = 1; break;
125 if (*path == '/') { 372 case 't': show_textrel = 1; break;
126 if ((p = strrchr(path, '\r')) != NULL) 373 case 'r': show_rpath = 1; break;
127 *p = 0; 374 case 'q': be_quiet = 1; break;
128 if ((p = strrchr(path, '\n')) != NULL) 375 case 'v': be_verbose = (be_verbose % 20) + 1; break;
129 *p = 0; 376 case 'a': show_pax = show_stack = show_textrel = show_rpath = 1; break;
130 scanelf(path); 377
131 } 378 case ':':
132 } 379 warn("Option missing parameter");
133 free(path); 380 usage(EXIT_FAILURE);
134 } 381 break;
135 break;
136 case 'p':
137 if ((path = strdup(getenv("PATH"))) == NULL) {
138 perror("strdup");
139 exit(EXIT_FAILURE);
140 }
141 /* split string into dirs */
142 while ((p = strrchr(path, ':')) != NULL) {
143 scanelf(p + 1);
144 *p = 0;
145 }
146 if (path != NULL)
147 free(path);
148 break;
149 case '?': 382 case '?':
383 warn("Unknown option");
384 usage(EXIT_FAILURE);
385 break;
150 default: 386 default:
151 break; 387 err("Unhandled option '%c'", flag);
152 } 388 break;
153 } 389 }
390 }
391
392 if (be_quiet && be_verbose)
393 err("You can be quiet or you can be verbose, not both, stupid");
394
395 if (scan_ldpath) scanelf_ldpath();
396 if (scan_envpath) scanelf_envpath();
154 while (optind < argc) 397 while (optind < argc)
155 scanelf(argv[optind++]); 398 scanelf_dir(argv[optind++]);
156} 399}
157 400
401
402
158int main(int argc, char **argv) 403int main(int argc, char *argv[])
159{ 404{
160 if (argc < 2) { 405 if (argc < 2)
161 usage(argv); 406 usage(EXIT_FAILURE);
162 exit(EXIT_FAILURE);
163 }
164 parseargs(argc, argv); 407 parseargs(argc, argv);
165 return 0; 408 return EXIT_SUCCESS;
166} 409}

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.18

  ViewVC Help
Powered by ViewVC 1.1.20