From 7bf3e12c30d0585743792982ed8bcfc44aecae34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yves=20Rutschl=C3=A9?= Date: Sun, 7 Jan 2018 16:43:50 +0000 Subject: [PATCH] Don't clobber data in libconfig space, copy it before changing it. So far it worked, but really that's not respecting the contract. --- common.c | 16 +++++++++++----- common.h | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/common.c b/common.c index 12c668e..225c28a 100644 --- a/common.c +++ b/common.c @@ -450,19 +450,25 @@ char* sprintaddr(char* buf, size_t size, struct addrinfo *a) /* Turns a hostname and port (or service) into a list of struct addrinfo * returns 0 on success, -1 otherwise and logs error - * - * *host gets modified - **/ -int resolve_split_name(struct addrinfo **out, char* host, const char* serv) + */ +int resolve_split_name(struct addrinfo **out, const char* ct_host, const char* serv) { struct addrinfo hint; char *end; int res; + char* host; memset(&hint, 0, sizeof(hint)); hint.ai_family = PF_UNSPEC; hint.ai_socktype = SOCK_STREAM; + /* Copy parameter so not to clobber data in libconfig */ + res = asprintf(&host, "%s", ct_host); + if (res == -1) { + log_message(LOG_ERR, "asprintf: cannot allocate memory"); + return -1; + } + /* If it is a RFC-Compliant IPv6 address ("[1234::12]:443"), remove brackets * around IP address */ if (host[0] == '[') { @@ -474,10 +480,10 @@ int resolve_split_name(struct addrinfo **out, char* host, const char* serv) *end = 0; /* remove last bracket */ } - res = getaddrinfo(host, serv, &hint, out); if (res) log_message(LOG_ERR, "%s `%s:%s'\n", gai_strerror(res), host, serv); + free(host); return res; } diff --git a/common.h b/common.h index 9f4665c..1e31345 100644 --- a/common.h +++ b/common.h @@ -106,7 +106,7 @@ void drop_privileges(const char* user_name, const char* chroot_path); void write_pid_file(const char* pidfile); void log_message(int type, char* msg, ...); void dump_connection(struct connection *cnx); -int resolve_split_name(struct addrinfo **out, char* hostname, const char* port); +int resolve_split_name(struct addrinfo **out, const char* hostname, const char* port); int start_listen_sockets(int *sockfd[], struct addrinfo *addr_list);