]> git.ipfire.org Git - thirdparty/git.git/commitdiff
daemon: fix IPv6 address corruption in lookup_hostname()
authorSebastien Tardif <sebtardif@ncf.ca>
Wed, 27 May 2026 18:18:29 +0000 (18:18 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 27 May 2026 20:28:37 +0000 (05:28 +0900)
getaddrinfo() is called with AF_UNSPEC hints, so it may return IPv6
results. However, the code unconditionally casts ai_addr to
sockaddr_in and passes AF_INET to inet_ntop(). On IPv6-only hosts,
this reads from the wrong struct offset, producing garbage IP
addresses.

Fix this by checking ai_family and extracting the address pointer
into a local variable before calling inet_ntop() once with the
correct family. Die on unexpected address families.

Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
daemon.c

index 0a7b1aae447912aadfbbedea9bd10e6b5dddf7f9..80fa0226d89f03215f9c75eaca0576f3343982eb 100644 (file)
--- a/daemon.c
+++ b/daemon.c
@@ -674,9 +674,20 @@ static void lookup_hostname(struct hostinfo *hi)
 
                gai = getaddrinfo(hi->hostname.buf, NULL, &hints, &ai);
                if (!gai) {
-                       struct sockaddr_in *sin_addr = (void *)ai->ai_addr;
+                       void *addr;
+
+                       if (ai->ai_family == AF_INET) {
+                               struct sockaddr_in *sa = (void *)ai->ai_addr;
+                               addr = &sa->sin_addr;
+                       } else if (ai->ai_family == AF_INET6) {
+                               struct sockaddr_in6 *sa6 = (void *)ai->ai_addr;
+                               addr = &sa6->sin6_addr;
+                       } else {
+                               die("unexpected address family: %d",
+                                   ai->ai_family);
+                       }
 
-                       inet_ntop(AF_INET, &sin_addr->sin_addr,
+                       inet_ntop(ai->ai_family, addr,
                                  addrbuf, sizeof(addrbuf));
                        strbuf_addstr(&hi->ip_address, addrbuf);