Skip to content

Commit

Permalink
removed the need to dynamically allocate memory in load_memmap() (using
Browse files Browse the repository at this point in the history
fopen/fgets), this solves crmulliner#5
and still behaves as the mantainers request to not use malloc/calloc)
  • Loading branch information
Valerio Lupi committed Jan 8, 2016
1 parent e1d4553 commit ab69e43
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 102 deletions.
61 changes: 18 additions & 43 deletions hijack/hijack.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,66 +262,42 @@ load_symtab(char *filename)
static int
load_memmap(pid_t pid, struct mm *mm, int *nmmp)
{
char *raw;
char name[MAX_NAME_LEN];
char name[MAX_NAME_LEN];
char *p;
unsigned long start, end;
struct mm *m;
int nmm = 0;
int fd, rv;
int rv;
int i;

int sizealloc = 1024 * 256;
raw = calloc(1, sizealloc);
if (!raw) {
printf("cant allocate memory for maps\n");
return -1;
}
sprintf(raw, "/proc/%d/maps", pid);
fd = open(raw, O_RDONLY);
if (0 > fd) {
printf("Can't open %s for reading\n", raw);
free(raw);
char line[1024];
char* s;

// read proc/pid/maps line by line
FILE* f = NULL;
sprintf(line, "/proc/%d/maps", pid);
f = fopen(line,"r");
if (!f) {
printf("Can't open %s for reading\n", line);
return -1;
}


p = raw;
m = mm;
while (1) {
rv = read(fd, p, sizealloc - (p - raw));
if (0 > rv) {
perror("read");
free(raw);
return -1;
}
if (0 == rv)
s = fgets(line,sizeof(line),f);
if (!s) {
break;
p += rv;
if (p - raw >= sizealloc) {
printf("Too many memory mapping\n");
free(raw);
return -1;
}
}
close(fd);

p = strtok(raw, "\n");
m = mm;
while (p) {
/* parse current map line */
// parse line
p = strtok(line, "\n");
rv = sscanf(p, "%08lx-%08lx %*s %*s %*s %*s %s\n",
&start, &end, name);

p = strtok(NULL, "\n");

&start, &end, name);
if (rv == 2) {
m = &mm[nmm++];
m->start = start;
m->end = end;
strcpy(m->name, MEMORY_ONLY);
continue;
}

if (strstr(name, "stack") != 0) {
stack_start = start;
stack_end = end;
Expand All @@ -347,9 +323,8 @@ load_memmap(pid_t pid, struct mm *mm, int *nmmp)
strcpy(m->name, name);
}
}

fclose(f);
*nmmp = nmm;
free(raw);
return 0;
}

Expand Down
98 changes: 39 additions & 59 deletions instruments/base/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* (c) Collin Mulliner <collin[at]mulliner.org>
*
* License: LGPL v2.1
*
*
* Termios code taken from glibc with slight modifications for this project
*
*
*/
#define _XOPEN_SOURCE 500
#include <stdio.h>
Expand All @@ -23,7 +23,7 @@
#include <dlfcn.h>
#include <elf.h>
#include <unistd.h>
#include <errno.h>
#include <errno.h>
#include <sys/mman.h>
#include <termios.h>
#include <sys/ioctl.h>
Expand Down Expand Up @@ -78,7 +78,7 @@ static struct symlist* get_syms(int fd, Elf32_Shdr *symh, Elf32_Shdr *strh)
sl->sym = NULL;

/* sanity */
if (symh->sh_size % sizeof(Elf32_Sym)) {
if (symh->sh_size % sizeof(Elf32_Sym)) {
//printf("elf_error\n");
goto out;
}
Expand Down Expand Up @@ -125,7 +125,7 @@ static int do_load(int fd, symtab_t symtab)
char *shstrtab = NULL;
int i;
int ret = -1;

/* elf header */
rv = read(fd, &ehdr, sizeof(ehdr));
if (0 > rv) {
Expand Down Expand Up @@ -157,7 +157,7 @@ static int do_load(int fd, symtab_t symtab)
log("elf error 3 %d %d\n", rv, size)
goto out;
}

/* section header string table */
size = shdr[ehdr.e_shstrndx].sh_size;
shstrtab = (char *) xmalloc(size);
Expand Down Expand Up @@ -252,57 +252,35 @@ static symtab_t load_symtab(char *filename)
}

static int load_memmap(pid_t pid, struct mm *mm, int *nmmp) {
char *raw;
char name[MAX_NAME_LEN];
char *p;
unsigned long start, end;
struct mm *m;
int nmm = 0;
int fd, rv;
int rv;
int i;
int sizealloc = 256 * 1024;
raw = calloc(1, sizealloc);
if (!raw) {
log("can't alloc\n");
return -1;
}
sprintf(raw, "/proc/%d/maps", pid);
fd = open(raw, O_RDONLY);
if (0 > fd) {
//printf("Can't open %s for reading\n", raw);
free(raw);
char line[1024];
char* s;

// read proc/pid/maps line by line
FILE* f = NULL;
sprintf(line, "/proc/%d/maps", pid);
f = fopen(line,"r");
if (!f) {
printf("Can't open %s for reading\n", line);
return -1;
}


p = raw;
m = mm;
while (1) {
rv = read(fd, p, sizealloc - (p - raw));
if (0 > rv) {
//perror("read");
free(raw);
return -1;
}
if (0 == rv)
s = fgets(line,sizeof(line),f);
if (!s) {
break;
p += rv;
if (p - raw >= sizealloc) {
//printf("Too many memory mapping\n");
free(raw);
return -1;
}
}
close(fd);

p = strtok(raw, "\n");
m = mm;
while (p) {
/* parse current map line */
// parse line
p = strtok(line, "\n");
rv = sscanf(p, "%08lx-%08lx %*s %*s %*s %*s %s\n",
&start, &end, name);

p = strtok(NULL, "\n");

&start, &end, name);
if (rv == 2) {
m = &mm[nmm++];
m->start = start;
Expand All @@ -323,15 +301,17 @@ static int load_memmap(pid_t pid, struct mm *mm, int *nmmp) {
m->start = start;
if (end > m->end)
m->end = end;
//printf("found name: %s, start:%x, end=%x\n", m->name, m->start, m->end);
} else {
/* new entry */
m = &mm[nmm++];
m->start = start;
m->end = end;
strcpy(m->name, name);
//printf("new name: %s, start:%x, end=%x\n", m->name, m->start, m->end);
}
}
free(raw);
fclose(f);
*nmmp = nmm;
return 0;
}
Expand Down Expand Up @@ -369,7 +349,7 @@ static int find_libname(char *libn, char *name, int len, unsigned long *start, s
strncpy(name, m->name, len);
if (strlen(m->name) >= len)
name[len-1] = '\0';

mprotect((void*)m->start, m->end - m->start, PROT_READ|PROT_WRITE|PROT_EXEC);
return 0;
}
Expand Down Expand Up @@ -494,7 +474,7 @@ int tcsetattr (int fd, int optional_actions, const struct termios *termios_p)
struct __kernel_termios k_termios;
unsigned long int cmd;
int retval;

switch (optional_actions)
{
case TCSANOW:
Expand All @@ -510,7 +490,7 @@ int tcsetattr (int fd, int optional_actions, const struct termios *termios_p)
//__set_errno (EINVAL);
return -1;
}

k_termios.c_iflag = termios_p->c_iflag & ~IBAUD0;
k_termios.c_oflag = termios_p->c_oflag;
k_termios.c_cflag = termios_p->c_cflag;
Expand All @@ -524,9 +504,9 @@ int tcsetattr (int fd, int optional_actions, const struct termios *termios_p)
#endif
memcpy (&k_termios.c_cc[0], &termios_p->c_cc[0],
__KERNEL_NCCS * sizeof (cc_t));

retval = ioctl (fd, cmd, &k_termios);

if (retval == 0 && cmd == TCSETS)
{
/* The Linux kernel has a bug which silently ignore the invalid
Expand All @@ -553,15 +533,15 @@ int tcsetattr (int fd, int optional_actions, const struct termios *termios_p)
retval = -1;
}
}

return retval;
}

int tcgetattr (int fd, struct termios *termios_p)
{
struct __kernel_termios k_termios;
int retval;

retval = ioctl (fd, TCGETS, &k_termios);
if(retval == 0) {
termios_p->c_iflag = k_termios.c_iflag;
Expand All @@ -575,8 +555,8 @@ int tcgetattr (int fd, struct termios *termios_p)
#ifdef _HAVE_C_OSPEED
termios_p->c_ospeed = k_termios.c_ospeed;
#endif


if (sizeof (cc_t) == 1 || _POSIX_VDISABLE == 0
|| (unsigned char) _POSIX_VDISABLE == (unsigned char) -1)
{
Expand All @@ -588,18 +568,18 @@ int tcgetattr (int fd, struct termios *termios_p)
memset ( (memcpy (&termios_p->c_cc[0], &k_termios.c_cc[0],
__KERNEL_NCCS * sizeof (cc_t)) + (__KERNEL_NCCS * sizeof (cc_t))) ,
_POSIX_VDISABLE, (NCCS - __KERNEL_NCCS) * sizeof (cc_t));

} else {
size_t cnt;

memcpy (&termios_p->c_cc[0], &k_termios.c_cc[0],
__KERNEL_NCCS * sizeof (cc_t));

for (cnt = __KERNEL_NCCS; cnt < NCCS; ++cnt)
termios_p->c_cc[cnt] = _POSIX_VDISABLE;
}
}

return retval;
}
#endif
#endif

0 comments on commit ab69e43

Please sign in to comment.