put a mutex lock around gethostent() usage, to prevent internal

races. the external usage was covered by the latest commit.
pull/18/head
rofl0r 2012-11-04 05:23:51 +01:00
parent 8ce1e51a60
commit e05cafc8e2
3 changed files with 6 additions and 0 deletions

View File

@ -37,6 +37,7 @@
#ifdef THREAD_SAFE
#include <pthread.h>
pthread_mutex_t internal_ips_lock;
pthread_mutex_t hostdb_lock;
#endif
#include "core.h"
@ -774,11 +775,14 @@ struct hostent *proxy_gethostbyname(const char *name, struct gethostbyname_data*
memset(buff, 0, sizeof(buff));
// this iterates over the "known hosts" db, usually /etc/hosts
MUTEX_LOCK(&hostdb_lock);
while((hp = gethostent()))
if(!strcmp(hp->h_name, name) && hp->h_addrtype == AF_INET && hp->h_length == sizeof(in_addr_t)) {
data->resolved_addr = *((in_addr_t*)(hp->h_addr_list[0]));
MUTEX_UNLOCK(&hostdb_lock);
goto retname;
}
MUTEX_UNLOCK(&hostdb_lock);
hash = dalias_hash((char *) name);

View File

@ -45,6 +45,7 @@ extern internal_ip_lookup_table internal_ips;
#ifdef THREAD_SAFE
#include <pthread.h>
extern pthread_mutex_t internal_ips_lock;
extern pthread_mutex_t hostdb_lock;
# define MUTEX_LOCK(x) pthread_mutex_lock(x)
# define MUTEX_UNLOCK(x) pthread_mutex_unlock(x)
# define MUTEX_INIT(x,y) pthread_mutex_init(x, y)

View File

@ -93,6 +93,7 @@ static void* load_sym(char* symname, void* proxyfunc) {
static void do_init(void) {
MUTEX_INIT(&internal_ips_lock, NULL);
MUTEX_INIT(&hostdb_lock, NULL);
/* read the config file */
get_chain_data(proxychains_pd, &proxychains_proxy_count, &proxychains_ct);