From 03fb295565b4af183d5c9a9642bcc313a8f2a3f8 Mon Sep 17 00:00:00 2001 From: iefa m Date: Sat, 14 Nov 2026 23:45:12 +0530 Subject: [PATCH 1/2] new_connection: ignore a PTR host name that does not resolve back With tls_checkhost enabled, the client's certificate is matched against the host name returned by a reverse lookup of the client's address. The PTR record is controlled by whoever controls the reverse zone for that address, not by the owner of the name in the certificate, so a client holding a certificate issued for another host could connect from any address whose PTR record names that host. Only use the name if it resolves back to the client's address. Otherwise only an IP address in the certificate can match, the same as when the reverse lookup fails. --- logsrvd/logsrvd.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/logsrvd/logsrvd.c b/logsrvd/logsrvd.c index 12447b2d5b..f86df64f82 100644 --- a/logsrvd/logsrvd.c +++ b/logsrvd/logsrvd.c @@ -1560,6 +1560,51 @@ tls_handshake_cb(int fd, int what, void *v) connection_close(closure); debug_return; } + +/* + * Check whether name, obtained via a reverse lookup of the peer's + * address, resolves back to that same address. + * Returns true if it does, else false. + */ +static bool +name_resolves_to_peer(const char *name, const union sockaddr_union *sa_un) +{ + struct addrinfo hints, *res0, *res; + bool matched = false; + int error; + debug_decl(name_resolves_to_peer, SUDO_DEBUG_UTIL); + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = sa_un->sa.sa_family; + hints.ai_socktype = SOCK_STREAM; + error = getaddrinfo(name, NULL, &hints, &res0); + if (error != 0) { + sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, + "unable to resolve %s: %s", name, gai_strerror(error)); + debug_return_bool(false); + } + + for (res = res0; res != NULL && !matched; res = res->ai_next) { + const union sockaddr_union *addr = + (const union sockaddr_union *)res->ai_addr; + + switch (res->ai_family) { + case AF_INET: + matched = memcmp(&addr->sin.sin_addr, &sa_un->sin.sin_addr, + sizeof(addr->sin.sin_addr)) == 0; + break; +#ifdef HAVE_STRUCT_IN6_ADDR + case AF_INET6: + matched = memcmp(&addr->sin6.sin6_addr, &sa_un->sin6.sin6_addr, + sizeof(addr->sin6.sin6_addr)) == 0; + break; +#endif /* HAVE_STRUCT_IN6_ADDR */ + } + } + freeaddrinfo(res0); + + debug_return_bool(matched); +} #endif /* HAVE_OPENSSL */ /* @@ -1620,7 +1665,11 @@ new_connection(int sock, bool tls, const union sockaddr_union *sa_un, char hbuf[NI_MAXHOST]; const int error = getnameinfo(&sa_un->sa, salen, hbuf, sizeof(hbuf), NULL, 0, NI_NAMEREQD); - if (error == 0) { + if (error == 0 && !name_resolves_to_peer(hbuf, sa_un)) { + /* The PTR record is not proof of identity, ignore it. */ + sudo_warnx(U_("host name %s does not resolve to %s"), + hbuf, closure->ipaddr); + } else if (error == 0) { closure->name = strdup(hbuf); if (closure->name == NULL) { sudo_warnx(U_("%s: %s"), __func__, From 9a0351c67b2d78499196fab7f0626141383235a4 Mon Sep 17 00:00:00 2001 From: iefa m Date: Mon, 16 Nov 2026 11:35:42 +0530 Subject: [PATCH 2/2] name_resolves_to_peer: drop the ai_socktype hint Without a service name the socket type doesn't affect which addresses are returned, it only stops getaddrinfo() from listing each address once per socket type. --- logsrvd/logsrvd.c | 1 - 1 file changed, 1 deletion(-) diff --git a/logsrvd/logsrvd.c b/logsrvd/logsrvd.c index f86df64f82..7b5cfc5667 100644 --- a/logsrvd/logsrvd.c +++ b/logsrvd/logsrvd.c @@ -1576,7 +1576,6 @@ name_resolves_to_peer(const char *name, const union sockaddr_union *sa_un) memset(&hints, 0, sizeof(hints)); hints.ai_family = sa_un->sa.sa_family; - hints.ai_socktype = SOCK_STREAM; error = getaddrinfo(name, NULL, &hints, &res0); if (error != 0) { sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,