|
|
0233e9 |
diff --git a/configure.ac b/configure.ac
|
|
|
0233e9 |
index 1511c9a..97a4689 100644
|
|
|
0233e9 |
--- a/configure.ac
|
|
|
0233e9 |
+++ b/configure.ac
|
|
|
0233e9 |
@@ -159,7 +159,7 @@ dnl Checks for functions
|
|
|
0233e9 |
AC_CHECK_FUNCS(mmap strerror strndup strtoul mbrtowc mkstemp utimes utime wcwidth strtof fork)
|
|
|
0233e9 |
|
|
|
0233e9 |
dnl Provide implementation of some required functions if necessary
|
|
|
0233e9 |
-AC_REPLACE_FUNCS(getopt_long asprintf vasprintf strlcpy strlcat getline)
|
|
|
0233e9 |
+AC_REPLACE_FUNCS(getopt_long asprintf vasprintf strlcpy strlcat getline pread)
|
|
|
0233e9 |
|
|
|
0233e9 |
dnl Checks for libraries
|
|
|
0233e9 |
AC_CHECK_LIB(z,gzopen)
|
|
|
0233e9 |
diff --git a/src/cdf.c b/src/cdf.c
|
|
|
0233e9 |
index d05d279..3b2b79b 100644
|
|
|
0233e9 |
--- a/src/cdf.c
|
|
|
0233e9 |
+++ b/src/cdf.c
|
|
|
0233e9 |
@@ -35,7 +35,7 @@
|
|
|
0233e9 |
#include "file.h"
|
|
|
0233e9 |
|
|
|
0233e9 |
#ifndef lint
|
|
|
0233e9 |
-FILE_RCSID("@(#)$File: cdf.c,v 1.50 2012/02/20 22:35:29 christos Exp $")
|
|
|
0233e9 |
+FILE_RCSID("@(#)$File: cdf.c,v 1.51 2012/03/20 18:28:02 christos Exp $")
|
|
|
0233e9 |
#endif
|
|
|
0233e9 |
|
|
|
0233e9 |
#include <assert.h>
|
|
|
0233e9 |
@@ -296,10 +296,7 @@ cdf_read(const cdf_info_t *info, off_t off, void *buf, size_t len)
|
|
|
0233e9 |
if (info->i_fd == -1)
|
|
|
0233e9 |
return -1;
|
|
|
0233e9 |
|
|
|
0233e9 |
- if (lseek(info->i_fd, off, SEEK_SET) == (off_t)-1)
|
|
|
0233e9 |
- return -1;
|
|
|
0233e9 |
-
|
|
|
0233e9 |
- if (read(info->i_fd, buf, len) != (ssize_t)len)
|
|
|
0233e9 |
+ if (pread(info->i_fd, buf, len, off) != (ssize_t)len)
|
|
|
0233e9 |
return -1;
|
|
|
0233e9 |
|
|
|
0233e9 |
return (ssize_t)len;
|
|
|
0233e9 |
diff --git a/src/elfclass.h b/src/elfclass.h
|
|
|
0233e9 |
index 2e7741b..010958a 100644
|
|
|
0233e9 |
--- a/src/elfclass.h
|
|
|
0233e9 |
+++ b/src/elfclass.h
|
|
|
0233e9 |
@@ -59,7 +59,8 @@
|
|
|
0233e9 |
(off_t)elf_getu(swap, elfhdr.e_shoff),
|
|
|
0233e9 |
elf_getu16(swap, elfhdr.e_shnum),
|
|
|
0233e9 |
(size_t)elf_getu16(swap, elfhdr.e_shentsize),
|
|
|
0233e9 |
- fsize, &flags, elf_getu16(swap, elfhdr.e_machine)) == -1)
|
|
|
0233e9 |
+ fsize, &flags, elf_getu16(swap, elfhdr.e_machine),
|
|
|
0233e9 |
+ (int)elf_getu16(swap, elfhdr.e_shstrndx)) == -1)
|
|
|
0233e9 |
return -1;
|
|
|
0233e9 |
break;
|
|
|
0233e9 |
|
|
|
0233e9 |
diff --git a/src/file.h b/src/file.h
|
|
|
0233e9 |
index 1b5f53f..28f9bc7 100644
|
|
|
0233e9 |
--- a/src/file.h
|
|
|
0233e9 |
+++ b/src/file.h
|
|
|
0233e9 |
@@ -462,6 +462,9 @@ extern char *sys_errlist[];
|
|
|
0233e9 |
#define strtoul(a, b, c) strtol(a, b, c)
|
|
|
0233e9 |
#endif
|
|
|
0233e9 |
|
|
|
0233e9 |
+#ifndef HAVE_PREAD
|
|
|
0233e9 |
+ssize_t pread(int, void *, size_t, off_t);
|
|
|
0233e9 |
+#endif
|
|
|
0233e9 |
#ifndef HAVE_VASPRINTF
|
|
|
0233e9 |
int vasprintf(char **, const char *, va_list);
|
|
|
0233e9 |
#endif
|
|
|
0233e9 |
diff --git a/src/readelf.c b/src/readelf.c
|
|
|
0233e9 |
index ce4832a..8d355c5 100644
|
|
|
0233e9 |
--- a/src/readelf.c
|
|
|
0233e9 |
+++ b/src/readelf.c
|
|
|
0233e9 |
@@ -48,7 +48,7 @@ private int dophn_core(struct magic_set *, int, int, int, off_t, int, size_t,
|
|
|
0233e9 |
private int dophn_exec(struct magic_set *, int, int, int, off_t, int, size_t,
|
|
|
0233e9 |
off_t, int *, int);
|
|
|
0233e9 |
private int doshn(struct magic_set *, int, int, int, off_t, int, size_t,
|
|
|
0233e9 |
- off_t, int *, int);
|
|
|
0233e9 |
+ off_t, int *, int, int);
|
|
|
0233e9 |
private size_t donote(struct magic_set *, void *, size_t, size_t, int,
|
|
|
0233e9 |
int, size_t, int *);
|
|
|
0233e9 |
|
|
|
0233e9 |
@@ -129,19 +129,21 @@ getu64(int swap, uint64_t value)
|
|
|
0233e9 |
#define elf_getu32(swap, value) getu32(swap, value)
|
|
|
0233e9 |
#ifdef USE_ARRAY_FOR_64BIT_TYPES
|
|
|
0233e9 |
# define elf_getu64(swap, array) \
|
|
|
0233e9 |
- ((swap ? ((uint64_t)elf_getu32(swap, array[0])) << 32 : elf_getu32(swap, array[0])) + \
|
|
|
0233e9 |
- (swap ? elf_getu32(swap, array[1]) : ((uint64_t)elf_getu32(swap, array[1]) << 32)))
|
|
|
0233e9 |
+ ((swap ? ((uint64_t)elf_getu32(swap, array[0])) << 32 \
|
|
|
0233e9 |
+ : elf_getu32(swap, array[0])) + \
|
|
|
0233e9 |
+ (swap ? elf_getu32(swap, array[1]) : \
|
|
|
0233e9 |
+ ((uint64_t)elf_getu32(swap, array[1]) << 32)))
|
|
|
0233e9 |
#else
|
|
|
0233e9 |
# define elf_getu64(swap, value) getu64(swap, value)
|
|
|
0233e9 |
#endif
|
|
|
0233e9 |
|
|
|
0233e9 |
#define xsh_addr (clazz == ELFCLASS32 \
|
|
|
0233e9 |
- ? (void *) &sh32 \
|
|
|
0233e9 |
- : (void *) &sh64)
|
|
|
0233e9 |
+ ? (void *)&sh32 \
|
|
|
0233e9 |
+ : (void *)&sh64)
|
|
|
0233e9 |
#define xsh_sizeof (clazz == ELFCLASS32 \
|
|
|
0233e9 |
- ? sizeof sh32 \
|
|
|
0233e9 |
- : sizeof sh64)
|
|
|
0233e9 |
-#define xsh_size (clazz == ELFCLASS32 \
|
|
|
0233e9 |
+ ? sizeof(sh32) \
|
|
|
0233e9 |
+ : sizeof(sh64))
|
|
|
0233e9 |
+#define xsh_size (size_t)(clazz == ELFCLASS32 \
|
|
|
0233e9 |
? elf_getu32(swap, sh32.sh_size) \
|
|
|
0233e9 |
: elf_getu64(swap, sh64.sh_size))
|
|
|
0233e9 |
#define xsh_offset (off_t)(clazz == ELFCLASS32 \
|
|
|
0233e9 |
@@ -150,12 +152,15 @@ getu64(int swap, uint64_t value)
|
|
|
0233e9 |
#define xsh_type (clazz == ELFCLASS32 \
|
|
|
0233e9 |
? elf_getu32(swap, sh32.sh_type) \
|
|
|
0233e9 |
: elf_getu32(swap, sh64.sh_type))
|
|
|
0233e9 |
+#define xsh_name (clazz == ELFCLASS32 \
|
|
|
0233e9 |
+ ? elf_getu32(swap, sh32.sh_name) \
|
|
|
0233e9 |
+ : elf_getu32(swap, sh64.sh_name))
|
|
|
0233e9 |
#define xph_addr (clazz == ELFCLASS32 \
|
|
|
0233e9 |
? (void *) &ph32 \
|
|
|
0233e9 |
: (void *) &ph64)
|
|
|
0233e9 |
#define xph_sizeof (clazz == ELFCLASS32 \
|
|
|
0233e9 |
- ? sizeof ph32 \
|
|
|
0233e9 |
- : sizeof ph64)
|
|
|
0233e9 |
+ ? sizeof(ph32) \
|
|
|
0233e9 |
+ : sizeof(ph64))
|
|
|
0233e9 |
#define xph_type (clazz == ELFCLASS32 \
|
|
|
0233e9 |
? elf_getu32(swap, ph32.p_type) \
|
|
|
0233e9 |
: elf_getu32(swap, ph64.p_type))
|
|
|
0233e9 |
@@ -171,8 +176,8 @@ getu64(int swap, uint64_t value)
|
|
|
0233e9 |
? elf_getu32(swap, ph32.p_filesz) \
|
|
|
0233e9 |
: elf_getu64(swap, ph64.p_filesz)))
|
|
|
0233e9 |
#define xnh_addr (clazz == ELFCLASS32 \
|
|
|
0233e9 |
- ? (void *) &nh32 \
|
|
|
0233e9 |
- : (void *) &nh64)
|
|
|
0233e9 |
+ ? (void *)&nh32 \
|
|
|
0233e9 |
+ : (void *)&nh64)
|
|
|
0233e9 |
#define xph_memsz (size_t)((clazz == ELFCLASS32 \
|
|
|
0233e9 |
? elf_getu32(swap, ph32.p_memsz) \
|
|
|
0233e9 |
: elf_getu64(swap, ph64.p_memsz)))
|
|
|
0233e9 |
@@ -192,8 +197,8 @@ getu64(int swap, uint64_t value)
|
|
|
0233e9 |
? prpsoffsets32[i] \
|
|
|
0233e9 |
: prpsoffsets64[i])
|
|
|
0233e9 |
#define xcap_addr (clazz == ELFCLASS32 \
|
|
|
0233e9 |
- ? (void *) &cap32 \
|
|
|
0233e9 |
- : (void *) &cap64)
|
|
|
0233e9 |
+ ? (void *)&cap32 \
|
|
|
0233e9 |
+ : (void *)&cap64)
|
|
|
0233e9 |
#define xcap_sizeof (clazz == ELFCLASS32 \
|
|
|
0233e9 |
? sizeof cap32 \
|
|
|
0233e9 |
: sizeof cap64)
|
|
|
0233e9 |
@@ -296,7 +301,7 @@ dophn_core(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
|
|
|
0233e9 |
{
|
|
|
0233e9 |
Elf32_Phdr ph32;
|
|
|
0233e9 |
Elf64_Phdr ph64;
|
|
|
0233e9 |
- size_t offset;
|
|
|
0233e9 |
+ size_t offset, len;
|
|
|
0233e9 |
unsigned char nbuf[BUFSIZ];
|
|
|
0233e9 |
ssize_t bufsize;
|
|
|
0233e9 |
|
|
|
0233e9 |
@@ -310,11 +315,7 @@ dophn_core(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
|
|
|
0233e9 |
* Loop through all the program headers.
|
|
|
0233e9 |
*/
|
|
|
0233e9 |
for ( ; num; num--) {
|
|
|
0233e9 |
- if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
|
|
|
0233e9 |
- file_badseek(ms);
|
|
|
0233e9 |
- return -1;
|
|
|
0233e9 |
- }
|
|
|
0233e9 |
- if (read(fd, xph_addr, xph_sizeof) == -1) {
|
|
|
0233e9 |
+ if (pread(fd, xph_addr, xph_sizeof, off) == -1) {
|
|
|
0233e9 |
file_badread(ms);
|
|
|
0233e9 |
return -1;
|
|
|
0233e9 |
}
|
|
|
0233e9 |
@@ -332,13 +333,8 @@ dophn_core(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
|
|
|
0233e9 |
* This is a PT_NOTE section; loop through all the notes
|
|
|
0233e9 |
* in the section.
|
|
|
0233e9 |
*/
|
|
|
0233e9 |
- if (lseek(fd, xph_offset, SEEK_SET) == (off_t)-1) {
|
|
|
0233e9 |
- file_badseek(ms);
|
|
|
0233e9 |
- return -1;
|
|
|
0233e9 |
- }
|
|
|
0233e9 |
- bufsize = read(fd, nbuf,
|
|
|
0233e9 |
- ((xph_filesz < sizeof(nbuf)) ? xph_filesz : sizeof(nbuf)));
|
|
|
0233e9 |
- if (bufsize == -1) {
|
|
|
0233e9 |
+ len = xph_filesz < sizeof(nbuf) ? xph_filesz : sizeof(nbuf);
|
|
|
0233e9 |
+ if ((bufsize = pread(fd, nbuf, len, xph_offset)) == -1) {
|
|
|
0233e9 |
file_badread(ms);
|
|
|
0233e9 |
return -1;
|
|
|
0233e9 |
}
|
|
|
0233e9 |
@@ -843,15 +839,16 @@ static const cap_desc_t cap_desc_386[] = {
|
|
|
0233e9 |
|
|
|
0233e9 |
private int
|
|
|
0233e9 |
doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
|
|
|
0233e9 |
- size_t size, off_t fsize, int *flags, int mach)
|
|
|
0233e9 |
+ size_t size, off_t fsize, int *flags, int mach, int strtab)
|
|
|
0233e9 |
{
|
|
|
0233e9 |
Elf32_Shdr sh32;
|
|
|
0233e9 |
Elf64_Shdr sh64;
|
|
|
0233e9 |
int stripped = 1;
|
|
|
0233e9 |
void *nbuf;
|
|
|
0233e9 |
- off_t noff, coff;
|
|
|
0233e9 |
+ off_t noff, coff, name_off;
|
|
|
0233e9 |
uint64_t cap_hw1 = 0; /* SunOS 5.x hardware capabilites */
|
|
|
0233e9 |
uint64_t cap_sf1 = 0; /* SunOS 5.x software capabilites */
|
|
|
0233e9 |
+ char name[50];
|
|
|
0233e9 |
|
|
|
0233e9 |
if (size != xsh_sizeof) {
|
|
|
0233e9 |
if (file_printf(ms, ", corrupted section header size") == -1)
|
|
|
0233e9 |
@@ -859,12 +856,24 @@ doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
|
|
|
0233e9 |
return 0;
|
|
|
0233e9 |
}
|
|
|
0233e9 |
|
|
|
0233e9 |
+ /* Read offset of name section to be able to read section names later */
|
|
|
0233e9 |
+ if (pread(fd, xsh_addr, xsh_sizeof, off + size * strtab) == -1) {
|
|
|
0233e9 |
+ file_badread(ms);
|
|
|
0233e9 |
+ return -1;
|
|
|
0233e9 |
+ }
|
|
|
0233e9 |
+ name_off = xsh_offset;
|
|
|
0233e9 |
+
|
|
|
0233e9 |
for ( ; num; num--) {
|
|
|
0233e9 |
- if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
|
|
|
0233e9 |
- file_badseek(ms);
|
|
|
0233e9 |
+ /* Read the name of this section. */
|
|
|
0233e9 |
+ if (pread(fd, name, sizeof(name), name_off + xsh_name) == -1) {
|
|
|
0233e9 |
+ file_badread(ms);
|
|
|
0233e9 |
return -1;
|
|
|
0233e9 |
}
|
|
|
0233e9 |
- if (read(fd, xsh_addr, xsh_sizeof) == -1) {
|
|
|
0233e9 |
+ name[sizeof(name) - 1] = '\0';
|
|
|
0233e9 |
+ if (strcmp(name, ".debug_info") == 0)
|
|
|
0233e9 |
+ stripped = 0;
|
|
|
0233e9 |
+
|
|
|
0233e9 |
+ if (pread(fd, xsh_addr, xsh_sizeof, off) == -1) {
|
|
|
0233e9 |
file_badread(ms);
|
|
|
0233e9 |
return -1;
|
|
|
0233e9 |
}
|
|
|
0233e9 |
@@ -889,39 +898,30 @@ doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
|
|
|
0233e9 |
/* Things we can determine when we seek */
|
|
|
0233e9 |
switch (xsh_type) {
|
|
|
0233e9 |
case SHT_NOTE:
|
|
|
0233e9 |
- if ((nbuf = malloc((size_t)xsh_size)) == NULL) {
|
|
|
0233e9 |
+ if ((nbuf = malloc(xsh_size)) == NULL) {
|
|
|
0233e9 |
file_error(ms, errno, "Cannot allocate memory"
|
|
|
0233e9 |
" for note");
|
|
|
0233e9 |
return -1;
|
|
|
0233e9 |
}
|
|
|
0233e9 |
- if ((noff = lseek(fd, (off_t)xsh_offset, SEEK_SET)) ==
|
|
|
0233e9 |
- (off_t)-1) {
|
|
|
0233e9 |
+ if (pread(fd, nbuf, xsh_size, xsh_offset) == -1) {
|
|
|
0233e9 |
file_badread(ms);
|
|
|
0233e9 |
free(nbuf);
|
|
|
0233e9 |
return -1;
|
|
|
0233e9 |
}
|
|
|
0233e9 |
- if (read(fd, nbuf, (size_t)xsh_size) !=
|
|
|
0233e9 |
- (ssize_t)xsh_size) {
|
|
|
0233e9 |
- free(nbuf);
|
|
|
0233e9 |
- file_badread(ms);
|
|
|
0233e9 |
- return -1;
|
|
|
0233e9 |
- }
|
|
|
0233e9 |
|
|
|
0233e9 |
noff = 0;
|
|
|
0233e9 |
for (;;) {
|
|
|
0233e9 |
if (noff >= (off_t)xsh_size)
|
|
|
0233e9 |
break;
|
|
|
0233e9 |
noff = donote(ms, nbuf, (size_t)noff,
|
|
|
0233e9 |
- (size_t)xsh_size, clazz, swap, 4,
|
|
|
0233e9 |
- flags);
|
|
|
0233e9 |
+ xsh_size, clazz, swap, 4, flags);
|
|
|
0233e9 |
if (noff == 0)
|
|
|
0233e9 |
break;
|
|
|
0233e9 |
}
|
|
|
0233e9 |
free(nbuf);
|
|
|
0233e9 |
break;
|
|
|
0233e9 |
case SHT_SUNW_cap:
|
|
|
0233e9 |
- if (lseek(fd, (off_t)xsh_offset, SEEK_SET) ==
|
|
|
0233e9 |
- (off_t)-1) {
|
|
|
0233e9 |
+ if (lseek(fd, xsh_offset, SEEK_SET) == (off_t)-1) {
|
|
|
0233e9 |
file_badseek(ms);
|
|
|
0233e9 |
return -1;
|
|
|
0233e9 |
}
|
|
|
0233e9 |
@@ -1043,7 +1043,7 @@ dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
|
|
|
0233e9 |
const char *shared_libraries = "";
|
|
|
0233e9 |
unsigned char nbuf[BUFSIZ];
|
|
|
0233e9 |
ssize_t bufsize;
|
|
|
0233e9 |
- size_t offset, align;
|
|
|
0233e9 |
+ size_t offset, align, len;
|
|
|
0233e9 |
|
|
|
0233e9 |
if (size != xph_sizeof) {
|
|
|
0233e9 |
if (file_printf(ms, ", corrupted program header size") == -1)
|
|
|
0233e9 |
@@ -1052,13 +1052,8 @@ dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
|
|
|
0233e9 |
}
|
|
|
0233e9 |
|
|
|
0233e9 |
for ( ; num; num--) {
|
|
|
0233e9 |
- if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
|
|
|
0233e9 |
- file_badseek(ms);
|
|
|
0233e9 |
- return -1;
|
|
|
0233e9 |
- }
|
|
|
0233e9 |
-
|
|
|
0233e9 |
- if (read(fd, xph_addr, xph_sizeof) == -1) {
|
|
|
0233e9 |
- file_badread(ms);
|
|
|
0233e9 |
+ if (pread(fd, xph_addr, xph_sizeof, off) == -1) {
|
|
|
0233e9 |
+ file_badread(ms);
|
|
|
0233e9 |
return -1;
|
|
|
0233e9 |
}
|
|
|
0233e9 |
|
|
|
0233e9 |
@@ -1096,12 +1091,9 @@ dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
|
|
|
0233e9 |
* This is a PT_NOTE section; loop through all the notes
|
|
|
0233e9 |
* in the section.
|
|
|
0233e9 |
*/
|
|
|
0233e9 |
- if (lseek(fd, xph_offset, SEEK_SET) == (off_t)-1) {
|
|
|
0233e9 |
- file_badseek(ms);
|
|
|
0233e9 |
- return -1;
|
|
|
0233e9 |
- }
|
|
|
0233e9 |
- bufsize = read(fd, nbuf, ((xph_filesz < sizeof(nbuf)) ?
|
|
|
0233e9 |
- xph_filesz : sizeof(nbuf)));
|
|
|
0233e9 |
+ len = xph_filesz < sizeof(nbuf) ? xph_filesz
|
|
|
0233e9 |
+ : sizeof(nbuf);
|
|
|
0233e9 |
+ bufsize = pread(fd, nbuf, len, xph_offset);
|
|
|
0233e9 |
if (bufsize == -1) {
|
|
|
0233e9 |
file_badread(ms);
|
|
|
0233e9 |
return -1;
|