From 2182eff3584b1e4a3660c69393baf338cf4841be Mon Sep 17 00:00:00 2001 From: rofl0r Date: Sat, 6 Jun 2015 11:41:36 +0100 Subject: [PATCH] fix segfault in DNS mapping lookup code the allocatorthread got pointers to RAM which were reallocated behind the back, and if realloc() couldn't grow in-place, lead to segfaults in applications that do a lot of DNS-lookups such as webbrowsers. closes #66 closes #31 thanks to @ravomavain for tracking down the issue. --- Makefile | 4 ++-- src/allocator_thread.c | 8 +++++-- src/core.c | 1 - src/libproxychains.c | 3 --- src/shm.c | 53 ------------------------------------------ src/shm.h | 17 -------------- src/stringdump.c | 13 ----------- src/stringdump.h | 12 ---------- 8 files changed, 8 insertions(+), 103 deletions(-) delete mode 100644 src/shm.c delete mode 100644 src/shm.h delete mode 100644 src/stringdump.c delete mode 100644 src/stringdump.h diff --git a/Makefile b/Makefile index 3cacaa9..e2f3e23 100644 --- a/Makefile +++ b/Makefile @@ -16,8 +16,8 @@ sysconfdir=$(prefix)/etc SRCS = $(sort $(wildcard src/*.c)) OBJS = $(SRCS:.c=.o) LOBJS = src/nameinfo.o src/version.o \ - src/core.o src/common.o src/libproxychains.o src/shm.o \ - src/allocator_thread.o src/ip_type.o src/stringdump.o \ + src/core.o src/common.o src/libproxychains.o \ + src/allocator_thread.o src/ip_type.o \ src/hostsreader.o src/hash.o src/debug.o GENH = src/version.h diff --git a/src/allocator_thread.c b/src/allocator_thread.c index 5ed6b13..12130b5 100644 --- a/src/allocator_thread.c +++ b/src/allocator_thread.c @@ -10,12 +10,10 @@ #include #include #include "allocator_thread.h" -#include "shm.h" #include "debug.h" #include "ip_type.h" #include "mutex.h" #include "hash.h" -#include "stringdump.h" /* stuff for our internal translation table */ @@ -30,6 +28,12 @@ typedef struct { string_hash_tuple** list; } internal_ip_lookup_table; +static void *dumpstring(char* s, size_t len) { + char* p = malloc(len); + if(p) memcpy(p, s, len); + return p; +} + pthread_mutex_t internal_ips_lock; internal_ip_lookup_table *internal_ips = NULL; internal_ip_lookup_table internal_ips_buf; diff --git a/src/core.c b/src/core.c index 25483c2..eb05566 100644 --- a/src/core.c +++ b/src/core.c @@ -37,7 +37,6 @@ #include "core.h" #include "common.h" -#include "shm.h" #include "allocator_thread.h" extern int tcp_read_time_out; diff --git a/src/libproxychains.c b/src/libproxychains.c index c9eaeaa..9e898b9 100644 --- a/src/libproxychains.c +++ b/src/libproxychains.c @@ -95,15 +95,12 @@ static void* load_sym(char* symname, void* proxyfunc) { #define SETUP_SYM(X) do { true_ ## X = load_sym( # X, X ); } while(0) -#include "shm.h" #include "allocator_thread.h" -#include "stringdump.h" const char *proxychains_get_version(void); static void do_init(void) { srand(time(NULL)); - dumpstring_init(); // global string garbage can core_initialize(); at_init(); diff --git a/src/shm.c b/src/shm.c deleted file mode 100644 index d51470a..000000000 --- a/src/shm.c +++ /dev/null @@ -1,53 +0,0 @@ -#include -#include -#include -#include -#include -#ifndef PAGE_SIZE -#define PAGE_SIZE 4096 -#endif -#include "shm.h" -#include "debug.h" - -#if 0 -#include -#include -#include - -/* allocates shared memory which can be accessed from the parent and its childs */ -void *shm_realloc(void* old, size_t old_size, size_t new_size) { - //PFUNC(); - void *nu = mmap(NULL, new_size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); - if(old) { - if(!nu) return NULL; - assert(new_size >= old_size); - memcpy(nu, old, old_size); - munmap(old, old_size); - } - return nu; -} -#endif - -void stringpool_init(struct stringpool* sp) { - PFUNC(); - memset(sp, 0, sizeof *sp); -} - -char* stringpool_add(struct stringpool *sp, char* s, size_t len) { - //PFUNC(); - if(len > sp->alloced - sp->used) { - size_t newsz = sp->used + len; - size_t inc = PAGE_SIZE - (newsz % PAGE_SIZE); - newsz += (inc == PAGE_SIZE) ? 0 : inc; - void* p = realloc(sp->start, newsz); - if(p) { - sp->start = p; - sp->alloced = newsz; - } else - return 0; - } - char* ret = sp->start + sp->used; - memcpy(ret, s, len); - sp->used += len; - return ret; -} diff --git a/src/shm.h b/src/shm.h deleted file mode 100644 index 13b5249..000000000 --- a/src/shm.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef SHM_H -#define SHM_H -#include - -struct stringpool { - size_t alloced; - size_t used; - char* start; -}; - -void stringpool_init(struct stringpool* sp); -char* stringpool_add(struct stringpool *sp, char* s, size_t len); -#if 0 -void *shm_realloc(void* old, size_t old_size, size_t new_size); -#endif -//RcB: DEP "shm.c" -#endif diff --git a/src/stringdump.c b/src/stringdump.c deleted file mode 100644 index ff946f0..000000000 --- a/src/stringdump.c +++ /dev/null @@ -1,13 +0,0 @@ -#include "stringdump.h" -#include "debug.h" - -struct stringpool mem; - -char *dumpstring(char* s, size_t len) { - PFUNC(); - return stringpool_add(&mem, s, len); -} - -void dumpstring_init(void) { - stringpool_init(&mem); -} diff --git a/src/stringdump.h b/src/stringdump.h deleted file mode 100644 index 4c16d6f..000000000 --- a/src/stringdump.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef STRINGDUMP_H -#define STRINGDUMP_H - -#include "shm.h" -#include - -char *dumpstring(char* s, size_t len); -void dumpstring_init(void); - -//RcB: DEP "stringdump.h" - -#endif