Skip to content

Commit

Permalink
Avoid freeaddrinfo(NULL)
Browse files Browse the repository at this point in the history
Found by: Release_
Patch by: michaelortmann
Fixes: #1329

The behavior of freeadrinfo(NULL) is left unspecified by RFC 3493. Some Operating Systems like FreeBSD handle this for compatibility / portability but others like OpenBSD do not (as reported in #1329), so let eggdrop avoid calling freeadrinfo(NULL).
  • Loading branch information
michaelortmann authored Oct 5, 2022
1 parent b3b658b commit 55595ab
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
27 changes: 17 additions & 10 deletions src/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ void *thread_dns_hostbyip(void *arg)
i = getnameinfo((const struct sockaddr *) &addr->addr.sa, addr->addrlen,
dtn->host, sizeof dtn->host, NULL, 0, 0);
if (i) {
debug1("dns: thread_dns_hostbyip(): getnameinfo(): error = %s", gai_strerror(i));
#ifdef IPV6
if (addr->family == AF_INET6)
inet_ntop(AF_INET6, &addr->addr.s6.sin6_addr, dtn->host, sizeof dtn->host);
Expand All @@ -520,13 +521,13 @@ void *thread_dns_hostbyip(void *arg)
void *thread_dns_ipbyhost(void *arg)
{
struct dns_thread_node *dtn = (struct dns_thread_node *) arg;
struct addrinfo *res0, *res;
int i;
struct addrinfo *res0 = NULL, *res;
int error;
sockname_t *addr = &dtn->addr;

i = getaddrinfo(dtn->host, NULL, NULL, &res0);
error = getaddrinfo(dtn->host, NULL, NULL, &res0);
memset(addr, 0, sizeof *addr);
if (!i) {
if (!error) {
#ifdef IPV6
for (res = res0; res; res = res->ai_next) {
if (res == res0 || res->ai_family == (pref_af ? AF_INET6 : AF_INET)) {
Expand All @@ -538,21 +539,27 @@ void *thread_dns_ipbyhost(void *arg)
}
}
#else
i = 1;
error = 1;
for (res = res0; res; res = res->ai_next) {
if (res->ai_family == AF_INET) {
addr->family = res->ai_family;
addr->addrlen = res->ai_addrlen;
memcpy(&addr->addr.sa, res->ai_addr, res->ai_addrlen);
i = 0;
error = 0;
break;
}
}
#endif
freeaddrinfo(res0);
if (res0) /* The behavior of freeadrinfo(NULL) is left unspecified by RFCs
* 2553 and 3493. Avoid to be compatible with all OSes. */
freeaddrinfo(res0);
}
else if (error == EAI_NONAME)
debug1("dns: thread_dns_ipbyhost(): getaddrinfo(): hostname %s not known", dtn->host);
else
debug1("dns: thread_dns_ipbyhost(): getaddrinfo(): error = %s", gai_strerror(error));
pthread_mutex_lock(&dtn->mutex);
dtn->ok = !i;
dtn->ok = !error;
close(dtn->fildes[1]);
pthread_mutex_unlock(&dtn->mutex);
return NULL;
Expand Down Expand Up @@ -661,7 +668,7 @@ void core_dns_hostbyip(sockname_t *addr)
sizeof (struct sockaddr_in), host, sizeof host, NULL, 0, 0);
alarm(0);
if (i)
debug1("dns: getnameinfo(): error = %s", gai_strerror(i));
debug1("dns: core_dns_hostbyip(): getnameinfo(): error = %s", gai_strerror(i));
}
if (i)
inet_ntop(AF_INET, &addr->addr.s4.sin_addr.s_addr, host, sizeof host);
Expand All @@ -673,7 +680,7 @@ void core_dns_hostbyip(sockname_t *addr)
sizeof (struct sockaddr_in6), host, sizeof host, NULL, 0, 0);
alarm(0);
if (i)
debug1("dns: getnameinfo(): error = %s", gai_strerror(i));
debug1("dns: core_dns_hostbyip(): getnameinfo(): error = %s", gai_strerror(i));
}
if (i)
inet_ntop(AF_INET6, &addr->addr.s6.sin6_addr, host, sizeof host);
Expand Down
20 changes: 14 additions & 6 deletions src/tcldcc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1028,13 +1028,12 @@ static int tcl_connect STDVAR
}

static int setlisten(Tcl_Interp *irp, char *ip, char *portp, char *type, char *maskproc, char *flag) {
int i, idx = -1, port, realport, found=0, ipv4=1;
int i, idx = -1, port, realport, found=0, ipv4=1, error;
char s[11], msg[256], newip[EGG_INET_ADDRSTRLEN];
struct portmap *pmap = NULL, *pold = NULL;
sockname_t name;
struct in_addr ipaddr4;
struct addrinfo hint, *ipaddr = NULL;
int ret;
#ifdef IPV6
struct in6_addr ipaddr6;
#endif
Expand All @@ -1056,8 +1055,8 @@ static int setlisten(Tcl_Interp *irp, char *ip, char *portp, char *type, char *m
strlcpy(newip, ip, sizeof newip);
}
/* Return addrinfo struct ipaddr containing family... */
ret = getaddrinfo(newip, NULL, &hint, &ipaddr);
if (!ret) {
error = getaddrinfo(newip, NULL, &hint, &ipaddr);
if (!error) {
/* Load network address to in(6)_addr struct for later byte comparisons */
if (ipaddr->ai_family == AF_INET) {
inet_pton(AF_INET, newip, &ipaddr4);
Expand All @@ -1068,8 +1067,17 @@ static int setlisten(Tcl_Interp *irp, char *ip, char *portp, char *type, char *m
ipv4 = 0;
}
#endif
if (ipaddr) /* The behavior of freeadrinfo(NULL) is left unspecified by RFCs
* 2553 and 3493. Avoid to be compatible with all OSes. */
freeaddrinfo(ipaddr);
}
freeaddrinfo(ipaddr);
else if (error == EAI_NONAME)
/* currently setlisten() handles only ip not hostname */
putlog(LOG_MISC, "*",
"tcldcc: setlisten(): getaddrinfo(): ip %s not known", newip);
else
putlog(LOG_MISC, "*", "tcldcc: setlisten(): getaddrinfo(): error = %s",
gai_strerror(error));
port = realport = atoi(portp);
for (pmap = root; pmap; pold = pmap, pmap = pmap->next) {
if (pmap->realport == port) {
Expand Down Expand Up @@ -1282,7 +1290,7 @@ static int tcl_listen STDVAR
strlcpy(ip, argv[1], sizeof(ip));
i++;
} else {
Tcl_AppendResult(irp, "invalid ip address", NULL);
Tcl_AppendResult(irp, "invalid IP address argument", NULL);
return TCL_ERROR;
}
}
Expand Down

0 comments on commit 55595ab

Please sign in to comment.