60edc5
From 0be99f8799e90eaed4e8eeb7d5be7de81dd71360 Mon Sep 17 00:00:00 2001
60edc5
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
60edc5
Date: Thu, 11 Apr 2019 18:17:16 +0200
60edc5
Subject: [PATCH] inet_aton: Use getaddrinfo() if possible
60edc5
MIME-Version: 1.0
60edc5
Content-Type: text/plain; charset=UTF-8
60edc5
Content-Transfer-Encoding: 8bit
60edc5
60edc5
Socket::inet_aton() used gethostbyname() to process arguments that are
60edc5
not an IP addres. However, gethostbyname() is not thread-safe and when
60edc5
called from multiple threads a bogus value can be returned.
60edc5
60edc5
This patch does add any new test because a basic inet_aton() usage is
60edc5
already covered and because reproducing the thread failure would
60edc5
require flodding DNS servers with thousounds of request.
60edc5
60edc5
<https://rt.perl.org/Public/Bug/Display.html?id=97860>
60edc5
<https://bugzilla.redhat.com/show_bug.cgi?id=1693293>
60edc5
60edc5
Signed-off-by: Petr Písař <ppisar@redhat.com>
60edc5
---
60edc5
 Socket.xs | 16 +++++++++++++++-
60edc5
 1 file changed, 15 insertions(+), 1 deletion(-)
60edc5
60edc5
diff --git a/Socket.xs b/Socket.xs
60edc5
index e46c93e..65244dd 100644
60edc5
--- a/Socket.xs
60edc5
+++ b/Socket.xs
60edc5
@@ -764,19 +764,33 @@ inet_aton(host)
60edc5
 	char *	host
60edc5
 	CODE:
60edc5
 	{
60edc5
+#ifdef HAS_GETADDRINFO
60edc5
+	struct addrinfo *res;
60edc5
+	struct addrinfo hints = {0,};
60edc5
+	hints.ai_family = AF_INET;
60edc5
+	if (!getaddrinfo(host, NULL, &hints, &res)) {
60edc5
+	    ST(0) = sv_2mortal(newSVpvn(
60edc5
+		(char *)&(((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr),
60edc5
+		4
60edc5
+	));
60edc5
+	freeaddrinfo(res);
60edc5
+	XSRETURN(1);
60edc5
+    }
60edc5
+#else
60edc5
 	struct in_addr ip_address;
60edc5
 	struct hostent * phe;
60edc5
-
60edc5
 	if ((*host != '\0') && inet_aton(host, &ip_address)) {
60edc5
 		ST(0) = sv_2mortal(newSVpvn((char *)&ip_address, sizeof(ip_address)));
60edc5
 		XSRETURN(1);
60edc5
 	}
60edc5
 #ifdef HAS_GETHOSTBYNAME
60edc5
+	/* gethostbyname is not thread-safe */
60edc5
 	phe = gethostbyname(host);
60edc5
 	if (phe && phe->h_addrtype == AF_INET && phe->h_length == 4) {
60edc5
 		ST(0) = sv_2mortal(newSVpvn((char *)phe->h_addr, phe->h_length));
60edc5
 		XSRETURN(1);
60edc5
 	}
60edc5
+#endif
60edc5
 #endif
60edc5
 	XSRETURN_UNDEF;
60edc5
 	}
60edc5
-- 
60edc5
2.20.1
60edc5