| 1 |
solar |
1.1 |
/*
|
| 2 |
|
|
* Copyright 2003 Ned Ludd <solar@gentoo.org>
|
| 3 |
vapier |
1.8 |
* Copyright 1999-2005 Gentoo Foundation
|
| 4 |
solar |
1.1 |
* Distributed under the terms of the GNU General Public License v2
|
| 5 |
solar |
1.31 |
* $Header: /var/cvsroot/gentoo-projects/pax-utils/scanelf.c,v 1.30 2005/04/05 16:15:31 solar Exp $
|
| 6 |
solar |
1.1 |
*
|
| 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 |
solar |
1.28 |
#define __USE_GNU
|
| 28 |
solar |
1.1 |
#include <string.h>
|
| 29 |
vapier |
1.10 |
#include <errno.h>
|
| 30 |
solar |
1.1 |
#include <unistd.h>
|
| 31 |
|
|
#include <sys/stat.h>
|
| 32 |
|
|
#include <dirent.h>
|
| 33 |
|
|
#include <getopt.h>
|
| 34 |
solar |
1.20 |
#include <assert.h>
|
| 35 |
solar |
1.21 |
|
| 36 |
solar |
1.1 |
#include "paxelf.h"
|
| 37 |
|
|
|
| 38 |
solar |
1.31 |
static const char *rcsid = "$Id: scanelf.c,v 1.30 2005/04/05 16:15:31 solar Exp $";
|
| 39 |
vapier |
1.10 |
|
| 40 |
|
|
|
| 41 |
|
|
/* helper functions for showing errors */
|
| 42 |
vapier |
1.18 |
#define argv0 "scanelf" /*((*argv != NULL) ? argv[0] : __FILE__ "\b\b")*/
|
| 43 |
vapier |
1.10 |
#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 |
vapier |
1.14 |
static char dir_crossmount = 1;
|
| 67 |
vapier |
1.10 |
static char show_pax = 0;
|
| 68 |
|
|
static char show_stack = 0;
|
| 69 |
|
|
static char show_textrel = 0;
|
| 70 |
|
|
static char show_rpath = 0;
|
| 71 |
solar |
1.16 |
static char show_banner = 1;
|
| 72 |
vapier |
1.10 |
static char be_quiet = 0;
|
| 73 |
vapier |
1.14 |
static char be_verbose = 0;
|
| 74 |
vapier |
1.27 |
static char *find_sym = NULL;
|
| 75 |
vapier |
1.10 |
|
| 76 |
solar |
1.1 |
|
| 77 |
|
|
|
| 78 |
vapier |
1.10 |
/* scan an elf file and show all the fun stuff */
|
| 79 |
|
|
static void scanelf_file(const char *filename)
|
| 80 |
solar |
1.6 |
{
|
| 81 |
vapier |
1.10 |
int i;
|
| 82 |
vapier |
1.27 |
char found_pax, found_stack, found_relro, found_textrel, found_rpath, found_sym;
|
| 83 |
vapier |
1.26 |
elfobj *elf;
|
| 84 |
vapier |
1.10 |
|
| 85 |
vapier |
1.27 |
found_pax = found_stack = found_relro = found_textrel = found_rpath = found_sym = 0;
|
| 86 |
solar |
1.12 |
|
| 87 |
vapier |
1.10 |
/* verify this is real ELF */
|
| 88 |
vapier |
1.14 |
if ((elf = readelf(filename)) == NULL) {
|
| 89 |
vapier |
1.26 |
if (be_verbose > 2) printf("%s: not an ELF\n", filename);
|
| 90 |
vapier |
1.10 |
return;
|
| 91 |
vapier |
1.14 |
}
|
| 92 |
|
|
|
| 93 |
vapier |
1.26 |
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 |
vapier |
1.10 |
|
| 100 |
|
|
/* show the header */
|
| 101 |
solar |
1.16 |
if (!be_quiet && show_banner) {
|
| 102 |
vapier |
1.23 |
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 |
solar |
1.16 |
show_banner = 0;
|
| 109 |
vapier |
1.10 |
}
|
| 110 |
|
|
|
| 111 |
|
|
/* dump all the good stuff */
|
| 112 |
|
|
if (!be_quiet)
|
| 113 |
vapier |
1.26 |
printf("%-7s ", get_elfetype(elf));
|
| 114 |
vapier |
1.10 |
|
| 115 |
vapier |
1.14 |
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 |
vapier |
1.10 |
|
| 123 |
|
|
/* stack fun */
|
| 124 |
|
|
if (show_stack) {
|
| 125 |
vapier |
1.26 |
#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 |
vapier |
1.10 |
}
|
| 141 |
vapier |
1.26 |
SHOW_STACK(32)
|
| 142 |
|
|
SHOW_STACK(64)
|
| 143 |
vapier |
1.23 |
if (!be_quiet && !found_stack) printf("--- ");
|
| 144 |
|
|
if (!be_quiet && !found_relro) printf("--- ");
|
| 145 |
vapier |
1.10 |
}
|
| 146 |
|
|
|
| 147 |
|
|
/* textrel fun */
|
| 148 |
|
|
if (show_textrel) {
|
| 149 |
vapier |
1.26 |
#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 |
vapier |
1.23 |
if (!be_quiet && !found_textrel) printf("------- ");
|
| 169 |
vapier |
1.10 |
}
|
| 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 |
vapier |
1.23 |
char *rpath, *runpath;
|
| 175 |
vapier |
1.26 |
void *strtbl_void = elf_findsecbyname(elf, ".dynstr");
|
| 176 |
vapier |
1.23 |
rpath = runpath = NULL;
|
| 177 |
vapier |
1.10 |
|
| 178 |
vapier |
1.26 |
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 |
vapier |
1.10 |
}
|
| 202 |
vapier |
1.23 |
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 |
vapier |
1.10 |
}
|
| 214 |
|
|
|
| 215 |
vapier |
1.27 |
if (find_sym) {
|
| 216 |
|
|
void *symtab_void, *strtab_void;
|
| 217 |
vapier |
1.29 |
char *versioned_symname = malloc(strlen(find_sym)+2);
|
| 218 |
solar |
1.28 |
|
| 219 |
|
|
sprintf(versioned_symname, "%s@", find_sym);
|
| 220 |
vapier |
1.27 |
symtab_void = elf_findsecbyname(elf, ".symtab");
|
| 221 |
|
|
strtab_void = elf_findsecbyname(elf, ".strtab");
|
| 222 |
|
|
|
| 223 |
|
|
if (symtab_void && strtab_void) {
|
| 224 |
|
|
#define FIND_SYM(B) \
|
| 225 |
|
|
if (elf->elf_class == ELFCLASS ## B) { \
|
| 226 |
|
|
Elf ## B ## _Shdr *symtab = SHDR ## B (symtab_void); \
|
| 227 |
|
|
Elf ## B ## _Shdr *strtab = SHDR ## B (strtab_void); \
|
| 228 |
|
|
Elf ## B ## _Sym *sym = SYM ## B (elf->data + EGET(symtab->sh_offset)); \
|
| 229 |
|
|
int cnt = EGET(symtab->sh_size) / EGET(symtab->sh_entsize); \
|
| 230 |
|
|
char *symname; \
|
| 231 |
|
|
for (i = 0; i < cnt; ++i) { \
|
| 232 |
|
|
if (sym->st_name) { \
|
| 233 |
|
|
symname = (char *)(elf->data + EGET(strtab->sh_offset) + EGET(sym->st_name)); \
|
| 234 |
solar |
1.28 |
if (*find_sym == '*') { \
|
| 235 |
|
|
printf("%s(%s) %5lX %15s %s\n", ((found_sym == 0) ? "\n\t" : "\t"), \
|
| 236 |
|
|
(char *) basename(filename), \
|
| 237 |
|
|
(long)sym->st_size, (char *) get_elfstttype(sym->st_info & 0xF), \
|
| 238 |
|
|
symname); \
|
| 239 |
|
|
found_sym = 1; \
|
| 240 |
|
|
} \
|
| 241 |
|
|
if ((strcmp(find_sym, symname) == 0) || \
|
| 242 |
|
|
(strncmp(symname, versioned_symname, strlen(versioned_symname)) == 0)) \
|
| 243 |
|
|
found_sym++; \
|
| 244 |
vapier |
1.27 |
} \
|
| 245 |
|
|
++sym; \
|
| 246 |
|
|
} }
|
| 247 |
|
|
FIND_SYM(32)
|
| 248 |
|
|
FIND_SYM(64)
|
| 249 |
|
|
}
|
| 250 |
solar |
1.28 |
free(versioned_symname);
|
| 251 |
vapier |
1.29 |
if (*find_sym != '*') {
|
| 252 |
|
|
if (found_sym)
|
| 253 |
|
|
printf(" %s ", find_sym);
|
| 254 |
|
|
else if (!be_quiet)
|
| 255 |
|
|
printf(" - ");
|
| 256 |
|
|
}
|
| 257 |
vapier |
1.27 |
}
|
| 258 |
vapier |
1.10 |
|
| 259 |
vapier |
1.29 |
if (!be_quiet || found_pax || found_stack || found_textrel || found_rpath || found_sym)
|
| 260 |
solar |
1.28 |
printf("%s\n", filename);
|
| 261 |
|
|
|
| 262 |
vapier |
1.10 |
unreadelf(elf);
|
| 263 |
solar |
1.6 |
}
|
| 264 |
|
|
|
| 265 |
solar |
1.1 |
/* scan a directory for ET_EXEC files and print when we find one */
|
| 266 |
vapier |
1.10 |
static void scanelf_dir(const char *path)
|
| 267 |
solar |
1.1 |
{
|
| 268 |
vapier |
1.10 |
register DIR *dir;
|
| 269 |
|
|
register struct dirent *dentry;
|
| 270 |
vapier |
1.14 |
struct stat st_top, st;
|
| 271 |
solar |
1.21 |
char buf[_POSIX_PATH_MAX];
|
| 272 |
solar |
1.20 |
size_t len = 0;
|
| 273 |
vapier |
1.10 |
|
| 274 |
|
|
/* make sure path exists */
|
| 275 |
vapier |
1.14 |
if (lstat(path, &st_top) == -1)
|
| 276 |
vapier |
1.10 |
return;
|
| 277 |
solar |
1.11 |
|
| 278 |
vapier |
1.10 |
/* ok, if it isn't a directory, assume we can open it */
|
| 279 |
vapier |
1.14 |
if (!S_ISDIR(st_top.st_mode)) {
|
| 280 |
vapier |
1.10 |
scanelf_file(path);
|
| 281 |
|
|
return;
|
| 282 |
|
|
}
|
| 283 |
|
|
|
| 284 |
|
|
/* now scan the dir looking for fun stuff */
|
| 285 |
|
|
if ((dir = opendir(path)) == NULL) {
|
| 286 |
|
|
warnf("could not opendir %s: %s", path, strerror(errno));
|
| 287 |
|
|
return;
|
| 288 |
|
|
}
|
| 289 |
vapier |
1.15 |
if (be_verbose) printf("%s: scanning dir\n", path);
|
| 290 |
solar |
1.11 |
|
| 291 |
vapier |
1.10 |
while ((dentry = readdir(dir))) {
|
| 292 |
|
|
if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, ".."))
|
| 293 |
|
|
continue;
|
| 294 |
|
|
len = (strlen(path) + 2 + strlen(dentry->d_name));
|
| 295 |
solar |
1.31 |
if (len >= sizeof(buf)) warn("len > sizeof(buf); %d %d = %s\n", len, sizeof(buf), path);
|
| 296 |
solar |
1.20 |
assert(len < sizeof(buf));
|
| 297 |
solar |
1.31 |
sprintf(buf, "%s/%s", path, dentry->d_name);
|
| 298 |
solar |
1.20 |
if (lstat(buf, &st) != -1) {
|
| 299 |
vapier |
1.10 |
if (S_ISREG(st.st_mode))
|
| 300 |
solar |
1.20 |
scanelf_file(buf);
|
| 301 |
vapier |
1.10 |
else if (dir_recurse && S_ISDIR(st.st_mode)) {
|
| 302 |
vapier |
1.14 |
if (dir_crossmount || (st_top.st_dev == st.st_dev))
|
| 303 |
solar |
1.20 |
scanelf_dir(buf);
|
| 304 |
vapier |
1.10 |
}
|
| 305 |
|
|
}
|
| 306 |
|
|
}
|
| 307 |
|
|
closedir(dir);
|
| 308 |
solar |
1.1 |
}
|
| 309 |
|
|
|
| 310 |
vapier |
1.10 |
/* scan /etc/ld.so.conf for paths */
|
| 311 |
|
|
static void scanelf_ldpath()
|
| 312 |
|
|
{
|
| 313 |
vapier |
1.17 |
char scan_l, scan_ul, scan_ull;
|
| 314 |
vapier |
1.10 |
char *path, *p;
|
| 315 |
|
|
FILE *fp;
|
| 316 |
|
|
|
| 317 |
|
|
if ((fp = fopen("/etc/ld.so.conf", "r")) == NULL)
|
| 318 |
|
|
err("Unable to open ld.so.conf: %s", strerror(errno));
|
| 319 |
|
|
|
| 320 |
vapier |
1.17 |
scan_l = scan_ul = scan_ull = 0;
|
| 321 |
|
|
|
| 322 |
solar |
1.30 |
if ((path = malloc(_POSIX_PATH_MAX)) == NULL) {
|
| 323 |
|
|
warn("Can not malloc() memory for ldpath scanning");
|
| 324 |
|
|
return;
|
| 325 |
|
|
}
|
| 326 |
vapier |
1.10 |
while ((fgets(path, _POSIX_PATH_MAX, fp)) != NULL)
|
| 327 |
|
|
if (*path == '/') {
|
| 328 |
|
|
if ((p = strrchr(path, '\r')) != NULL)
|
| 329 |
|
|
*p = 0;
|
| 330 |
|
|
if ((p = strrchr(path, '\n')) != NULL)
|
| 331 |
|
|
*p = 0;
|
| 332 |
vapier |
1.17 |
if (!scan_l && !strcmp(path, "/lib")) scan_l = 1;
|
| 333 |
|
|
if (!scan_ul && !strcmp(path, "/usr/lib")) scan_ul = 1;
|
| 334 |
|
|
if (!scan_ull && !strcmp(path, "/usr/local/lib")) scan_ull = 1;
|
| 335 |
vapier |
1.10 |
scanelf_dir(path);
|
| 336 |
|
|
}
|
| 337 |
|
|
free(path);
|
| 338 |
|
|
|
| 339 |
vapier |
1.17 |
if (!scan_l) scanelf_dir("/lib");
|
| 340 |
|
|
if (!scan_ul) scanelf_dir("/usr/lib");
|
| 341 |
|
|
if (!scan_ull) scanelf_dir("/usr/local/lib");
|
| 342 |
|
|
|
| 343 |
vapier |
1.10 |
fclose(fp);
|
| 344 |
|
|
}
|
| 345 |
solar |
1.1 |
|
| 346 |
vapier |
1.10 |
/* scan env PATH for paths */
|
| 347 |
|
|
static void scanelf_envpath()
|
| 348 |
solar |
1.1 |
{
|
| 349 |
vapier |
1.10 |
char *path, *p;
|
| 350 |
|
|
|
| 351 |
|
|
path = getenv("PATH");
|
| 352 |
|
|
if (!path)
|
| 353 |
|
|
err("PATH is not set in your env !");
|
| 354 |
|
|
|
| 355 |
|
|
if ((path = strdup(path)) == NULL)
|
| 356 |
|
|
err("stdup failed: %s", strerror(errno));
|
| 357 |
|
|
|
| 358 |
|
|
while ((p = strrchr(path, ':')) != NULL) {
|
| 359 |
|
|
scanelf_dir(p + 1);
|
| 360 |
|
|
*p = 0;
|
| 361 |
|
|
}
|
| 362 |
vapier |
1.17 |
|
| 363 |
vapier |
1.10 |
free(path);
|
| 364 |
solar |
1.1 |
}
|
| 365 |
|
|
|
| 366 |
|
|
|
| 367 |
vapier |
1.10 |
|
| 368 |
|
|
/* usage / invocation handling functions */
|
| 369 |
vapier |
1.27 |
#define PARSE_FLAGS "plRmxetrs:aqvo:BhV"
|
| 370 |
|
|
#define a_argument required_argument
|
| 371 |
vapier |
1.10 |
static struct option const long_opts[] = {
|
| 372 |
|
|
{"path", no_argument, NULL, 'p'},
|
| 373 |
|
|
{"ldpath", no_argument, NULL, 'l'},
|
| 374 |
|
|
{"recursive", no_argument, NULL, 'R'},
|
| 375 |
vapier |
1.14 |
{"mount", no_argument, NULL, 'm'},
|
| 376 |
vapier |
1.10 |
{"pax", no_argument, NULL, 'x'},
|
| 377 |
solar |
1.16 |
{"header", no_argument, NULL, 'e'},
|
| 378 |
vapier |
1.10 |
{"textrel", no_argument, NULL, 't'},
|
| 379 |
|
|
{"rpath", no_argument, NULL, 'r'},
|
| 380 |
vapier |
1.27 |
{"symbol", a_argument, NULL, 's'},
|
| 381 |
vapier |
1.10 |
{"all", no_argument, NULL, 'a'},
|
| 382 |
|
|
{"quiet", no_argument, NULL, 'q'},
|
| 383 |
vapier |
1.14 |
{"verbose", no_argument, NULL, 'v'},
|
| 384 |
vapier |
1.27 |
{"file", a_argument, NULL, 'o'},
|
| 385 |
solar |
1.16 |
{"nobanner", no_argument, NULL, 'B'},
|
| 386 |
vapier |
1.10 |
{"help", no_argument, NULL, 'h'},
|
| 387 |
|
|
{"version", no_argument, NULL, 'V'},
|
| 388 |
|
|
{NULL, no_argument, NULL, 0x0}
|
| 389 |
|
|
};
|
| 390 |
|
|
static char *opts_help[] = {
|
| 391 |
|
|
"Scan all directories in PATH environment",
|
| 392 |
|
|
"Scan all directories in /etc/ld.so.conf",
|
| 393 |
vapier |
1.14 |
"Scan directories recursively",
|
| 394 |
|
|
"Don't recursively cross mount points\n",
|
| 395 |
vapier |
1.10 |
"Print PaX markings",
|
| 396 |
|
|
"Print GNU_STACK markings",
|
| 397 |
|
|
"Print TEXTREL information",
|
| 398 |
|
|
"Print RPATH information",
|
| 399 |
vapier |
1.27 |
"Find a specified symbol",
|
| 400 |
solar |
1.16 |
"Print all scanned info (-x -e -t -r)\n",
|
| 401 |
vapier |
1.14 |
"Only output 'bad' things",
|
| 402 |
|
|
"Be verbose (can be specified more than once)",
|
| 403 |
vapier |
1.24 |
"Write output stream to a filename",
|
| 404 |
vapier |
1.14 |
"Don't display the header",
|
| 405 |
vapier |
1.10 |
"Print this help and exit",
|
| 406 |
|
|
"Print version and exit",
|
| 407 |
|
|
NULL
|
| 408 |
|
|
};
|
| 409 |
|
|
|
| 410 |
|
|
/* display usage and exit */
|
| 411 |
|
|
static void usage(int status)
|
| 412 |
solar |
1.1 |
{
|
| 413 |
vapier |
1.10 |
int i;
|
| 414 |
vapier |
1.27 |
printf("¤ Scan ELF binaries for stuff\n"
|
| 415 |
vapier |
1.10 |
"Usage: %s [options] <dir1> [dir2 dirN ...]\n\n", argv0);
|
| 416 |
vapier |
1.23 |
printf("Options:\n");
|
| 417 |
vapier |
1.10 |
for (i = 0; long_opts[i].name; ++i)
|
| 418 |
vapier |
1.27 |
if (long_opts[i].has_arg == no_argument)
|
| 419 |
|
|
printf(" -%c, --%-13s× %s\n", long_opts[i].val,
|
| 420 |
|
|
long_opts[i].name, opts_help[i]);
|
| 421 |
|
|
else
|
| 422 |
|
|
printf(" -%c, --%-6s <arg> × %s\n", long_opts[i].val,
|
| 423 |
|
|
long_opts[i].name, opts_help[i]);
|
| 424 |
vapier |
1.10 |
exit(status);
|
| 425 |
solar |
1.1 |
}
|
| 426 |
|
|
|
| 427 |
|
|
/* parse command line arguments and preform needed actions */
|
| 428 |
vapier |
1.10 |
static void parseargs(int argc, char *argv[])
|
| 429 |
|
|
{
|
| 430 |
|
|
int flag;
|
| 431 |
|
|
|
| 432 |
|
|
opterr = 0;
|
| 433 |
|
|
while ((flag=getopt_long(argc, argv, PARSE_FLAGS, long_opts, NULL)) != -1) {
|
| 434 |
|
|
switch (flag) {
|
| 435 |
|
|
|
| 436 |
|
|
case 'V': /* version info */
|
| 437 |
solar |
1.19 |
printf("%s compiled %s\n%s\n"
|
| 438 |
|
|
"%s written for Gentoo Linux by <solar and vapier @ gentoo.org>\n",
|
| 439 |
|
|
__FILE__, __DATE__, rcsid, argv0);
|
| 440 |
vapier |
1.10 |
exit(EXIT_SUCCESS);
|
| 441 |
|
|
break;
|
| 442 |
|
|
case 'h': usage(EXIT_SUCCESS); break;
|
| 443 |
|
|
|
| 444 |
vapier |
1.24 |
case 'o': {
|
| 445 |
solar |
1.21 |
FILE *fp = NULL;
|
| 446 |
|
|
fp = freopen(optarg, "w", stdout);
|
| 447 |
vapier |
1.24 |
if (fp == NULL)
|
| 448 |
|
|
err("Could not open output stream '%s': %s", optarg, strerror(errno));
|
| 449 |
|
|
stdout = fp;
|
| 450 |
solar |
1.21 |
break;
|
| 451 |
|
|
}
|
| 452 |
vapier |
1.24 |
|
| 453 |
vapier |
1.27 |
case 's': find_sym = strdup(optarg); break;
|
| 454 |
|
|
|
| 455 |
solar |
1.16 |
case 'B': show_banner = 0; break;
|
| 456 |
vapier |
1.10 |
case 'l': scan_ldpath = 1; break;
|
| 457 |
|
|
case 'p': scan_envpath = 1; break;
|
| 458 |
|
|
case 'R': dir_recurse = 1; break;
|
| 459 |
vapier |
1.14 |
case 'm': dir_crossmount = 0; break;
|
| 460 |
vapier |
1.10 |
case 'x': show_pax = 1; break;
|
| 461 |
solar |
1.16 |
case 'e': show_stack = 1; break;
|
| 462 |
vapier |
1.10 |
case 't': show_textrel = 1; break;
|
| 463 |
|
|
case 'r': show_rpath = 1; break;
|
| 464 |
|
|
case 'q': be_quiet = 1; break;
|
| 465 |
vapier |
1.14 |
case 'v': be_verbose = (be_verbose % 20) + 1; break;
|
| 466 |
vapier |
1.10 |
case 'a': show_pax = show_stack = show_textrel = show_rpath = 1; break;
|
| 467 |
|
|
|
| 468 |
|
|
case ':':
|
| 469 |
|
|
warn("Option missing parameter");
|
| 470 |
|
|
usage(EXIT_FAILURE);
|
| 471 |
|
|
break;
|
| 472 |
|
|
case '?':
|
| 473 |
|
|
warn("Unknown option");
|
| 474 |
|
|
usage(EXIT_FAILURE);
|
| 475 |
|
|
break;
|
| 476 |
|
|
default:
|
| 477 |
|
|
err("Unhandled option '%c'", flag);
|
| 478 |
|
|
break;
|
| 479 |
|
|
}
|
| 480 |
|
|
}
|
| 481 |
|
|
|
| 482 |
vapier |
1.14 |
if (be_quiet && be_verbose)
|
| 483 |
|
|
err("You can be quiet or you can be verbose, not both, stupid");
|
| 484 |
|
|
|
| 485 |
vapier |
1.10 |
if (scan_ldpath) scanelf_ldpath();
|
| 486 |
|
|
if (scan_envpath) scanelf_envpath();
|
| 487 |
vapier |
1.25 |
if (optind == argc && !scan_ldpath && !scan_envpath)
|
| 488 |
|
|
err("Nothing to scan !?");
|
| 489 |
vapier |
1.10 |
while (optind < argc)
|
| 490 |
|
|
scanelf_dir(argv[optind++]);
|
| 491 |
vapier |
1.27 |
|
| 492 |
|
|
if (find_sym) free(find_sym);
|
| 493 |
vapier |
1.10 |
}
|
| 494 |
|
|
|
| 495 |
|
|
|
| 496 |
|
|
|
| 497 |
|
|
int main(int argc, char *argv[])
|
| 498 |
solar |
1.1 |
{
|
| 499 |
vapier |
1.10 |
if (argc < 2)
|
| 500 |
|
|
usage(EXIT_FAILURE);
|
| 501 |
|
|
parseargs(argc, argv);
|
| 502 |
solar |
1.21 |
fclose(stdout);
|
| 503 |
vapier |
1.10 |
return EXIT_SUCCESS;
|
| 504 |
solar |
1.1 |
}
|