Race condition when get IP address from host in proxy mode

- ip_from_hostname() is not thread safe
- use global lock mechanism
This commit is contained in:
lxsang 2021-02-10 18:41:01 +01:00
parent d3276a7df3
commit b584007d49
2 changed files with 28 additions and 2 deletions

View File

@ -839,6 +839,7 @@ static void *proxify(void *data)
int port = atoi(dvalue(rq->request, "PROXY_PORT")); int port = atoi(dvalue(rq->request, "PROXY_PORT"));
char *path = dvalue(rq->request, "PROXY_PATH"); char *path = dvalue(rq->request, "PROXY_PATH");
char *query = dvalue(rq->request, "PROXY_QUERY"); char *query = dvalue(rq->request, "PROXY_QUERY");
char* ptr, *ip;
dictionary_t xheader = dvalue(rq->request, "REQUEST_HEADER"); dictionary_t xheader = dvalue(rq->request, "REQUEST_HEADER");
antd_task_t *task = antd_create_task(NULL, data, NULL, rq->client->last_io); antd_task_t *task = antd_create_task(NULL, data, NULL, rq->client->last_io);
if (!xheader) if (!xheader)
@ -846,7 +847,24 @@ static void *proxify(void *data)
antd_error(rq->client, 400, "Badd Request"); antd_error(rq->client, 400, "Badd Request");
return task; return task;
} }
sock_fd = request_socket(ip_from_hostname(host), port); pthread_mutex_lock(&server_mux);
ip = NULL;
// ip_from_host is not threadsafe, need to lock it
ptr = ip_from_hostname(host);
if(ptr)
{
ip = strdup(ptr);
}
pthread_mutex_unlock(&server_mux);
if(!ip)
{
antd_error(rq->client, 502, "Badd address");
return task;
}
sock_fd = request_socket(ip, port);
free(ip);
if (sock_fd == -1) if (sock_fd == -1)
{ {
antd_error(rq->client, 503, "Service Unavailable"); antd_error(rq->client, 503, "Service Unavailable");

View File

@ -605,7 +605,11 @@ int request_socket(const char *ip, int port)
{ {
int sockfd; int sockfd;
struct sockaddr_in dest; struct sockaddr_in dest;
if(!ip)
{
ERROR("Invalid IP address");
return -1;
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{ {
ERROR("Socket: %s", strerror(errno)); ERROR("Socket: %s", strerror(errno));
@ -638,6 +642,10 @@ char* ip_from_hostname(const char *hostname)
struct hostent *he; struct hostent *he;
struct in_addr **addr_list; struct in_addr **addr_list;
int i; int i;
if(!hostname)
{
return NULL;
}
if ((he = gethostbyname(hostname)) == NULL) if ((he = gethostbyname(hostname)) == NULL)
{ {
// get the host info // get the host info