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

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.10

  ViewVC Help
Powered by ViewVC 1.1.20