[PATCH] daemon: fix network address handling bugs
From: Sebastien Tardif via GitGitGadget <hidden>
Date: 2026-05-09 01:48:14
Subsystem:
the rest · Maintainer:
Linus Torvalds
From: Sebastien Tardif <redacted>
Fix three related issues in daemon.c's network address handling:
lookup_hostname() calls getaddrinfo() 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
by checking ai_family and handling both AF_INET and AF_INET6.
ip2str() passes the sockaddr struct size (ai_addrlen) as the output
buffer size argument to inet_ntop(). For IPv6, sizeof(sockaddr_in6) is
28 bytes but INET6_ADDRSTRLEN is 46, so long IPv6 addresses are silently
truncated. Fix by passing sizeof(ip) instead, and drop the now-unused
len parameter.
execute() logs "Connection from %s:%s" using REMOTE_ADDR and
REMOTE_PORT environment variables, but only checks REMOTE_ADDR for NULL.
If REMOTE_PORT is unset, NULL is passed to printf's %s, which is
undefined behavior. Fix by using a fallback string.
Signed-off-by: Sebastien Tardif <redacted>
---
daemon: fix network address handling bugs
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2299%2FSebTardif%2Ffix%2Fdaemon-ipv6-and-null-port-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2299/SebTardif/fix/daemon-ipv6-and-null-port-v1
Pull-Request: https://github.com/git/git/pull/2299
daemon.c | 31 +++++++++++++++++++------------
1 file changed, 19 insertions(+), 12 deletions(-)
diff --git a/daemon.c b/daemon.c
index 0a7b1aae44..84a5e38f92 100644
--- a/daemon.c
+++ b/daemon.c@@ -674,10 +674,17 @@ 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; - - inet_ntop(AF_INET, &sin_addr->sin_addr, - addrbuf, sizeof(addrbuf)); + if (ai->ai_family == AF_INET) { + struct sockaddr_in *sa = + (struct sockaddr_in *)ai->ai_addr; + inet_ntop(AF_INET, &sa->sin_addr, + addrbuf, sizeof(addrbuf)); + } else if (ai->ai_family == AF_INET6) { + struct sockaddr_in6 *sa6 = + (struct sockaddr_in6 *)ai->ai_addr; + inet_ntop(AF_INET6, &sa6->sin6_addr, + addrbuf, sizeof(addrbuf)); + } strbuf_addstr(&hi->ip_address, addrbuf); if (ai->ai_canonname)
@@ -742,7 +749,7 @@ static int execute(void) struct strvec env = STRVEC_INIT; if (addr) - loginfo("Connection from %s:%s", addr, port); + loginfo("Connection from %s:%s", addr, port ? port : "?"); set_keep_alive(0); alarm(init_timeout ? init_timeout : timeout);
@@ -936,7 +943,7 @@ struct socketlist { size_t alloc; }; -static const char *ip2str(int family, struct sockaddr *sin, socklen_t len) +static const char *ip2str(int family, struct sockaddr *sin) { #ifdef NO_IPV6 static char ip[INET_ADDRSTRLEN];
@@ -947,11 +954,11 @@ static const char *ip2str(int family, struct sockaddr *sin, socklen_t len) switch (family) { #ifndef NO_IPV6 case AF_INET6: - inet_ntop(family, &((struct sockaddr_in6*)sin)->sin6_addr, ip, len); + inet_ntop(family, &((struct sockaddr_in6*)sin)->sin6_addr, ip, sizeof(ip)); break; #endif case AF_INET: - inet_ntop(family, &((struct sockaddr_in*)sin)->sin_addr, ip, len); + inet_ntop(family, &((struct sockaddr_in*)sin)->sin_addr, ip, sizeof(ip)); break; default: xsnprintf(ip, sizeof(ip), "<unknown>");
@@ -1008,14 +1015,14 @@ static int setup_named_sock(char *listen_addr, int listen_port, struct socketlis if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) { logerror("Could not bind to %s: %s", - ip2str(ai->ai_family, ai->ai_addr, ai->ai_addrlen), + ip2str(ai->ai_family, ai->ai_addr), strerror(errno)); close(sockfd); continue; /* not fatal */ } if (listen(sockfd, 5) < 0) { logerror("Could not listen to %s: %s", - ip2str(ai->ai_family, ai->ai_addr, ai->ai_addrlen), + ip2str(ai->ai_family, ai->ai_addr), strerror(errno)); close(sockfd); continue; /* not fatal */
@@ -1069,7 +1076,7 @@ static int setup_named_sock(char *listen_addr, int listen_port, struct socketlis if ( bind(sockfd, (struct sockaddr *)&sin, sizeof sin) < 0 ) { logerror("Could not bind to %s: %s", - ip2str(AF_INET, (struct sockaddr *)&sin, sizeof(sin)), + ip2str(AF_INET, (struct sockaddr *)&sin), strerror(errno)); close(sockfd); return 0;
@@ -1077,7 +1084,7 @@ static int setup_named_sock(char *listen_addr, int listen_port, struct socketlis if (listen(sockfd, 5) < 0) { logerror("Could not listen to %s: %s", - ip2str(AF_INET, (struct sockaddr *)&sin, sizeof(sin)), + ip2str(AF_INET, (struct sockaddr *)&sin), strerror(errno)); close(sockfd); return 0;
base-commit: 94f057755b7941b321fd11fec1b2e3ca5313a4e0 -- gitgitgadget