aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Frysinger <vapier@gentoo.org>2007-08-20 09:54:15 +0000
committerMike Frysinger <vapier@gentoo.org>2007-08-20 09:54:15 +0000
commiteadbddb031aa17d85d3e38921588c0b9a79c8b54 (patch)
tree64ef2f52bd4519b28d0891f6527eb1b8ea8fe9f5
parentas pointed out by swegner, dont scan PHDRs that dont actually have a filesize... (diff)
downloadpax-utils-eadbddb031aa17d85d3e38921588c0b9a79c8b54.tar.gz
pax-utils-eadbddb031aa17d85d3e38921588c0b9a79c8b54.tar.bz2
pax-utils-eadbddb031aa17d85d3e38921588c0b9a79c8b54.zip
split xfuncs off into a sep file for all utils to use, cleanup misc things, and add some more comments
-rw-r--r--Makefile18
-rw-r--r--TODO3
-rw-r--r--dumpelf.c8
-rw-r--r--paxelf.c8
-rw-r--r--paxinc.c26
-rw-r--r--paxinc.h8
-rw-r--r--paxmacho.c4
-rw-r--r--pspax.c13
-rw-r--r--scanelf.c87
9 files changed, 72 insertions, 103 deletions
diff --git a/Makefile b/Makefile
index c4e8612..2c3582c 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
# Copyright 2003-2006 Ned Ludd <solar@linbsd.net>
# Distributed under the terms of the GNU General Public License v2
-# $Header: /var/cvsroot/gentoo-projects/pax-utils/Makefile,v 1.63 2007/08/12 20:06:57 grobian Exp $
+# $Header: /var/cvsroot/gentoo-projects/pax-utils/Makefile,v 1.64 2007/08/20 09:54:15 vapier Exp $
####################################################################
check_gcc=$(shell if $(CC) $(1) -S -o /dev/null -xc /dev/null > /dev/null 2>&1; \
@@ -43,7 +43,8 @@ ELF_TARGETS = scanelf dumpelf $(shell echo | $(CC) -dM -E - | grep -q __svr4__
ELF_OBJS = $(ELF_TARGETS:%=%.o) paxelf.o
MACH_TARGETS = scanmacho
MACH_OBJS = $(MACH_TARGETS:%=%.o) paxmacho.o
-OBJS = $(ELF_OBJS) $(MACH_OBJS) paxinc.o
+COMMON_OBJS = paxinc.o xfuncs.o
+OBJS = $(ELF_OBJS) $(MACH_OBJS) $(COMMON_OBJS)
TARGETS = $(ELF_TARGETS) $(MACH_TARGETS)
MPAGES = $(TARGETS:%=man/%.1)
SOURCES = $(OBJS:%.o=%.c)
@@ -74,11 +75,11 @@ ifeq ($(V),)
endif
$(Q)$(compile.c) $(WFLAGS)
-$(ELF_TARGETS): $(ELF_OBJS) paxinc.o
- $(CC) $(CFLAGS) $(LDFLAGS) paxinc.o paxelf.o -o $@ $@.o $(LIBS) $(LIBS-$@)
+$(ELF_TARGETS): $(ELF_OBJS) $(COMMON_OBJS)
+ $(CC) $(CFLAGS) $(LDFLAGS) $(COMMON_OBJS) paxelf.o -o $@ $@.o $(LIBS) $(LIBS-$@)
-$(MACH_TARGETS): $(MACH_OBJS) paxinc.o
- $(CC) $(CFLAGS) $(LDFLAGS) paxinc.o paxmacho.o -o $@ $@.o $(LIBS) $(LIBS-$@)
+$(MACH_TARGETS): $(MACH_OBJS) $(COMMON_OBJS)
+ $(CC) $(CFLAGS) $(LDFLAGS) $(COMMON_OBJS) paxmacho.o -o $@ $@.o $(LIBS) $(LIBS-$@)
%.so: %.c
$(CC) -shared -fPIC -o $@ $<
@@ -130,3 +131,8 @@ dist: distclean
du -b ../pax-utils-$(PV).tar.bz2
-include .depend
+
+check test:
+ $(MAKE) -C tests
+
+.PHONY: all check clean dist install test
diff --git a/TODO b/TODO
index fc975d4..dad4bce 100644
--- a/TODO
+++ b/TODO
@@ -28,3 +28,6 @@ offset just shy of the end of the file, we'll probably crash ...
dumpelf does not do string checking in very very verbose mode. it assumes the
strings are sane when dumping the sections. only way around this is to check
everything single byte and print it out a character at a time ... sucks ...
+
+we look at the section named ".text" for TEXTRELs when we should look at the
+PT_LOAD program headers which are marked Executable.
diff --git a/dumpelf.c b/dumpelf.c
index fa45776..f4ffc5d 100644
--- a/dumpelf.c
+++ b/dumpelf.c
@@ -1,16 +1,16 @@
/*
* Copyright 2005-2007 Gentoo Foundation
* Distributed under the terms of the GNU General Public License v2
- * $Header: /var/cvsroot/gentoo-projects/pax-utils/dumpelf.c,v 1.22 2007/05/23 22:27:27 solar Exp $
+ * $Header: /var/cvsroot/gentoo-projects/pax-utils/dumpelf.c,v 1.23 2007/08/20 09:54:15 vapier Exp $
*
* Copyright 2005-2007 Ned Ludd - <solar@gentoo.org>
* Copyright 2005-2007 Mike Frysinger - <vapier@gentoo.org>
*/
-#include "paxinc.h"
+static const char *rcsid = "$Id: dumpelf.c,v 1.23 2007/08/20 09:54:15 vapier Exp $";
+const char * const argv0 = "dumpelf";
-static const char *rcsid = "$Id: dumpelf.c,v 1.22 2007/05/23 22:27:27 solar Exp $";
-#define argv0 "dumpelf"
+#include "paxinc.h"
/* prototypes */
static void dumpelf(const char *filename, long file_cnt);
diff --git a/paxelf.c b/paxelf.c
index b38095d..167adcb 100644
--- a/paxelf.c
+++ b/paxelf.c
@@ -1,7 +1,7 @@
/*
* Copyright 2003-2007 Gentoo Foundation
* Distributed under the terms of the GNU General Public License v2
- * $Header: /var/cvsroot/gentoo-projects/pax-utils/paxelf.c,v 1.55 2007/06/29 17:09:12 solar Exp $
+ * $Header: /var/cvsroot/gentoo-projects/pax-utils/paxelf.c,v 1.56 2007/08/20 09:54:15 vapier Exp $
*
* Copyright 2005-2007 Ned Ludd - <solar@gentoo.org>
* Copyright 2005-2007 Mike Frysinger - <vapier@gentoo.org>
@@ -9,8 +9,6 @@
#include "paxinc.h"
-#define argv0 "paxelf"
-
/*
* Setup a bunch of helper functions to translate
* binary defines into readable strings.
@@ -405,7 +403,7 @@ elfobj *readelf_buffer(const char *filename, char *buffer, size_t buffer_len)
if (buffer == NULL || buffer_len < EI_NIDENT)
return NULL;
- elf = (elfobj*)malloc(sizeof(*elf));
+ elf = xmalloc(sizeof(*elf));
if (elf == NULL)
return NULL;
memset(elf, 0x00, sizeof(*elf));
@@ -607,7 +605,7 @@ unsigned long pax_pf2hf_flags(unsigned long paxflags)
char *pf_flags = pax_short_pf_flags(paxflags);
size_t x, len = strlen(pf_flags);
for (x = 0; x < len; x++) {
- switch(pf_flags[x]) {
+ switch (pf_flags[x]) {
case 'p':
flags |= HF_PAX_PAGEEXEC;
break;
diff --git a/paxinc.c b/paxinc.c
index 786fba0..c4ba120 100644
--- a/paxinc.c
+++ b/paxinc.c
@@ -1,7 +1,7 @@
/*
* Copyright 2003-2007 Gentoo Foundation
* Distributed under the terms of the GNU General Public License v2
- * $Header: /var/cvsroot/gentoo-projects/pax-utils/paxinc.c,v 1.7 2007/06/09 18:54:44 solar Exp $
+ * $Header: /var/cvsroot/gentoo-projects/pax-utils/paxinc.c,v 1.8 2007/08/20 09:54:15 vapier Exp $
*
* Copyright 2005-2007 Ned Ludd - <solar@gentoo.org>
* Copyright 2005-2007 Mike Frysinger - <vapier@gentoo.org>
@@ -12,8 +12,6 @@
#define IN_paxinc
#include "paxinc.h"
-#define argv0 "paxinc"
-
char do_reverse_endian;
/* some of this ar code was taken from busybox */
@@ -73,31 +71,31 @@ close_and_ret:
read(ar->fd, ret.buf.raw+59, 1);
}
- if ((ret.buf.formated.magic[0] != '`') || (ret.buf.formated.magic[1] != '\n')) {
+ if ((ret.buf.formatted.magic[0] != '`') || (ret.buf.formatted.magic[1] != '\n')) {
warn("Invalid ar entry");
goto close_and_ret;
}
- if (ret.buf.formated.name[0] == '/' && ret.buf.formated.name[1] == '/') {
+ if (ret.buf.formatted.name[0] == '/' && ret.buf.formatted.name[1] == '/') {
warn("Sorry, long names not yet supported; output will be incomplete for %s", ar->filename);
- ar->skip = atoi(ret.buf.formated.size);
+ ar->skip = atoi(ret.buf.formatted.size);
return ar_next(ar);
}
len = strlen(ar->filename);
- assert(len < sizeof(ret.name)-sizeof(ret.buf.formated.name)-1);
+ assert(len < sizeof(ret.name)-sizeof(ret.buf.formatted.name)-1);
memcpy(ret.name, ar->filename, len);
ret.name[len++] = ':';
- memcpy(ret.name+len, ret.buf.formated.name, sizeof(ret.buf.formated.name));
+ memcpy(ret.name+len, ret.buf.formatted.name, sizeof(ret.buf.formatted.name));
if ((s=strchr(ret.name+len, '/')) != NULL)
*s = '\0';
else
- ret.name[len+sizeof(ret.buf.formated.name)-1] = '\0';
- ret.date = atoi(ret.buf.formated.date);
- ret.uid = atoi(ret.buf.formated.uid);
- ret.gid = atoi(ret.buf.formated.gid);
- ret.mode = strtol(ret.buf.formated.mode, NULL, 8);
- ret.size = atoi(ret.buf.formated.size);
+ ret.name[len+sizeof(ret.buf.formatted.name)-1] = '\0';
+ ret.date = atoi(ret.buf.formatted.date);
+ ret.uid = atoi(ret.buf.formatted.uid);
+ ret.gid = atoi(ret.buf.formatted.gid);
+ ret.mode = strtol(ret.buf.formatted.mode, NULL, 8);
+ ret.size = atoi(ret.buf.formatted.size);
ar->skip = ret.size;
return &ret;
diff --git a/paxinc.h b/paxinc.h
index 41fcefa..00d3779 100644
--- a/paxinc.h
+++ b/paxinc.h
@@ -1,7 +1,7 @@
/*
* Copyright 2005-2007 Gentoo Foundation
* Distributed under the terms of the GNU General Public License v2
- * $Header: /var/cvsroot/gentoo-projects/pax-utils/paxinc.h,v 1.9 2007/05/23 22:27:27 solar Exp $
+ * $Header: /var/cvsroot/gentoo-projects/pax-utils/paxinc.h,v 1.10 2007/08/20 09:54:15 vapier Exp $
*
* Copyright 2005-2007 Ned Ludd - <solar@gentoo.org>
* Copyright 2005-2007 Mike Frysinger - <vapier@gentoo.org>
@@ -13,6 +13,7 @@
#define _PAX_INC_H
#include "porting.h"
+#include "xfuncs.h"
#ifndef VERSION
# define VERSION "cvs"
@@ -56,7 +57,7 @@ typedef struct {
char mode[8];
char size[10];
char magic[2];
- } formated;
+ } formatted;
} buf;
#endif
} archive_member;
@@ -95,6 +96,9 @@ archive_member *ar_next(archive_handle *);
#define RED COLOR("31", "01")
#define YELLOW COLOR("33", "01")
+/* constant pointer to a constant buffer ... each program needs to set this */
+extern const char * const argv0;
+
/* we need the space before the last comma or we trigger a bug in gcc-2 :( */
#define warn(fmt, args...) \
fprintf(stderr, "%s%s%s: " fmt "\n", RED, argv0, NORM , ## args)
diff --git a/paxmacho.c b/paxmacho.c
index 97cda25..6dd4636 100644
--- a/paxmacho.c
+++ b/paxmacho.c
@@ -1,7 +1,7 @@
/*
* Copyright 2003-2007 Gentoo Foundation
* Distributed under the terms of the GNU General Public License v2
- * $Header: /var/cvsroot/gentoo-projects/pax-utils/paxmacho.c,v 1.4 2007/06/09 18:54:44 solar Exp $
+ * $Header: /var/cvsroot/gentoo-projects/pax-utils/paxmacho.c,v 1.5 2007/08/20 09:54:15 vapier Exp $
*
* Copyright 2005-2007 Ned Ludd - <solar@gentoo.org>
* Copyright 2005-2007 Mike Frysinger - <vapier@gentoo.org>
@@ -9,7 +9,7 @@
#include "paxinc.h"
-#define argv0 "paxmacho"
+const char * const argv0 = "paxmacho";
/*
* Setup a bunch of helper functions to translate
diff --git a/pspax.c b/pspax.c
index 16370ef..de39ac9 100644
--- a/pspax.c
+++ b/pspax.c
@@ -12,6 +12,9 @@
* cc -o pspax pspax.c -DWANT_SYSCAP -lcap
*/
+static const char *rcsid = "$Id: pspax.c,v 1.38 2007/08/20 09:54:15 vapier Exp $";
+const char * const argv0 = "pspax";
+
#include "paxinc.h"
#include <grp.h>
@@ -24,8 +27,6 @@
#endif
#define PROC_DIR "/proc"
-static const char *rcsid = "$Id: pspax.c,v 1.37 2006/12/12 23:34:34 solar Exp $";
-#define argv0 "pspax"
@@ -79,8 +80,8 @@ static int get_proc_maps(pid_t pid)
continue;
/* 0x0-0x0 rwxp fffff000 00:00 0 */
/* 0x0-0x0 R+W+XP fffff000 00:00 0 */
- ++p; // ' '
- ++p; // r
+ ++p; /* ' ' */
+ ++p; /* r */
if (*p == '+')
++p;
/* FIXME: all of wx, w+, +x, ++ indicate w|x */
@@ -116,8 +117,8 @@ static int print_executable_mappings(pid_t pid)
continue;
/* 0x0-0x0 rwxp fffff000 00:00 0 */
/* 0x0-0x0 R+W+XP fffff000 00:00 0 */
- ++p; // ' '
- ++p; // r
+ ++p; /* ' ' */
+ ++p; /* r */
if (*p == '+')
++p;
/* FIXME: all of wx, w+, +x, ++ indicate w|x */
diff --git a/scanelf.c b/scanelf.c
index 9428c21..e44b7fb 100644
--- a/scanelf.c
+++ b/scanelf.c
@@ -1,16 +1,16 @@
/*
* Copyright 2003-2007 Gentoo Foundation
* Distributed under the terms of the GNU General Public License v2
- * $Header: /var/cvsroot/gentoo-projects/pax-utils/scanelf.c,v 1.185 2007/08/18 04:59:32 vapier Exp $
+ * $Header: /var/cvsroot/gentoo-projects/pax-utils/scanelf.c,v 1.186 2007/08/20 09:54:15 vapier Exp $
*
* Copyright 2003-2007 Ned Ludd - <solar@gentoo.org>
* Copyright 2004-2007 Mike Frysinger - <vapier@gentoo.org>
*/
-#include "paxinc.h"
+static const char *rcsid = "$Id: scanelf.c,v 1.186 2007/08/20 09:54:15 vapier Exp $";
+const char * const argv0 = "scanelf";
-static const char *rcsid = "$Id: scanelf.c,v 1.185 2007/08/18 04:59:32 vapier Exp $";
-#define argv0 "scanelf"
+#include "paxinc.h"
#define IS_MODIFIER(c) (c == '%' || c == '#' || c == '+')
@@ -27,12 +27,6 @@ static void usage(int status);
static char **get_split_env(const char *envvar);
static void parseenv(void);
static int parseargs(int argc, char *argv[]);
-static char *xstrdup(const char *s);
-static void *xmalloc(size_t size);
-static void *xrealloc(void *ptr, size_t size);
-static void xstrncat(char **dst, const char *src, size_t *curr_len, size_t n);
-#define xstrcat(dst,src,curr_len) xstrncat(dst,src,curr_len,0)
-static inline void xchrcat(char **dst, const char append, size_t *curr_len);
static int rematch(const char *regex, const char *match, int cflags);
/* variables to control behavior */
@@ -82,7 +76,8 @@ unsigned long setpax = 0UL;
int has_objdump = 0;
static char *getstr_perms(const char *fname);
-static char *getstr_perms(const char *fname) {
+static char *getstr_perms(const char *fname)
+{
struct stat st;
static char buf[8];
@@ -341,6 +336,10 @@ static char *scanelf_file_phdr(elfobj *elf, char *found_phdr, char *found_relro,
return ret;
}
+/*
+ * See if this ELF contains a DT_TEXTREL tag in any of its
+ * PT_DYNAMIC sections.
+ */
static const char *scanelf_file_textrel(elfobj *elf, char *found_textrel)
{
static const char *ret = "TEXTREL";
@@ -381,6 +380,11 @@ static const char *scanelf_file_textrel(elfobj *elf, char *found_textrel)
return " - ";
}
+/*
+ * Scan the .text section to see if there are any relocations in it.
+ * Should rewrite this to check PT_LOAD sections that are marked
+ * Executable rather than the section named '.text'.
+ */
static char *scanelf_file_textrels(elfobj *elf, char *found_textrels, char *found_textrel)
{
unsigned long s, r, rmax;
@@ -1122,7 +1126,7 @@ static int scanelf_elfobj(elfobj *elf)
/* init output buffer */
if (!out_buffer) {
out_len = sizeof(char) * 80;
- out_buffer = (char*)xmalloc(out_len);
+ out_buffer = xmalloc(out_len);
}
*out_buffer = '\0';
@@ -1280,7 +1284,7 @@ static int scanelf_elf(const char *filename, int fd, size_t len)
strncpy(sbuf, match_etypes, sizeof(sbuf));
if (strchr(match_etypes, ',') != NULL) {
char *p;
- while((p = strrchr(sbuf, ',')) != NULL) {
+ while ((p = strrchr(sbuf, ',')) != NULL) {
*p = 0;
if (etype_lookup(p+1) == get_etype(elf))
goto label_ret;
@@ -1451,7 +1455,7 @@ static int load_ld_cache_config(int i, const char *fname)
if ((p = strchr(path, '\n')) != NULL)
*p = 0;
#ifdef __linux__
- // recursive includes of the same file will make this segfault.
+ /* recursive includes of the same file will make this segfault. */
if ((memcmp(path, "include", 7) == 0) && isblank(path[7])) {
glob64_t gl;
size_t x;
@@ -1516,7 +1520,7 @@ static int load_ld_cache_config(int i, const char *fname)
return i;
}
- b = (char*)malloc(hdr.dirlistlen+1);
+ b = xmalloc(hdr.dirlistlen + 1);
if (fread(b, 1, hdr.dirlistlen+1, fp) != hdr.dirlistlen+1) {
fclose(fp);
free(b);
@@ -1748,7 +1752,7 @@ static int parseargs(int argc, char *argv[])
case 's': {
if (find_sym) warn("You prob don't want to specify -s twice");
find_sym = optarg;
- versioned_symname = (char*)xmalloc(sizeof(char) * (strlen(find_sym)+1+1));
+ versioned_symname = xmalloc(sizeof(char) * (strlen(find_sym)+1+1));
sprintf(versioned_symname, "%s@", find_sym);
break;
}
@@ -1767,8 +1771,8 @@ static int parseargs(int argc, char *argv[])
unsigned long flags = (PF_NOEMUTRAMP | PF_NORANDEXEC);
size_t x;
- for (x = 0 ; x < strlen(optarg); x++) {
- switch(optarg[x]) {
+ for (x = 0; x < strlen(optarg); x++) {
+ switch (optarg[x]) {
case 'p':
case 'P':
do_pax_state(optarg[x], PAGEEXEC);
@@ -1882,7 +1886,7 @@ static int parseargs(int argc, char *argv[])
/* construct our default format */
} else {
size_t fmt_len = 30;
- out_format = (char*)xmalloc(sizeof(char) * fmt_len);
+ out_format = xmalloc(sizeof(char) * fmt_len);
if (!be_quiet) xstrcat(&out_format, "%o ", &fmt_len);
if (show_pax) xstrcat(&out_format, "%x ", &fmt_len);
if (show_perms) xstrcat(&out_format, "%O ", &fmt_len);
@@ -2000,51 +2004,6 @@ int main(int argc, char *argv[])
}
-
-/* utility funcs */
-static char *xstrdup(const char *s)
-{
- char *ret = strdup(s);
- if (!ret) err("Could not strdup(): %s", strerror(errno));
- return ret;
-}
-static void *xmalloc(size_t size)
-{
- void *ret = malloc(size);
- if (!ret) err("Could not malloc() %li bytes", (unsigned long)size);
- return ret;
-}
-static void *xrealloc(void *ptr, size_t size)
-{
- void *ret = realloc(ptr, size);
- if (!ret) err("Could not realloc() %li bytes", (unsigned long)size);
- return ret;
-}
-static void xstrncat(char **dst, const char *src, size_t *curr_len, size_t n)
-{
- size_t new_len;
-
- new_len = strlen(*dst) + strlen(src);
- if (*curr_len <= new_len) {
- *curr_len = new_len + (*curr_len / 2);
- *dst = realloc(*dst, *curr_len);
- if (!*dst)
- err("could not realloc() %li bytes", (unsigned long)*curr_len);
- }
-
- if (n)
- strncat(*dst, src, n);
- else
- strcat(*dst, src);
-}
-static inline void xchrcat(char **dst, const char append, size_t *curr_len)
-{
- static char my_app[2];
- my_app[0] = append;
- my_app[1] = '\0';
- xstrcat(dst, my_app, curr_len);
-}
-
/* Match filename against entries in matchlist, return TRUE
* if the file is listed */
static int file_matches_list(const char *filename, char **matchlist)