summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDarren Tucker <dtucker@cvs.openbsd.org>2021-10-22 10:51:58 +0000
committerDarren Tucker <dtucker@cvs.openbsd.org>2021-10-22 10:51:58 +0000
commitfe7d7883fe0028207067d3b92f3a88e88e133c30 (patch)
treef8d0dbe69671b54b24eff32d2f2428b398f02f62
parentfae6b7105ac909ce0964e749e6ffe2819a9dfcb3 (diff)
Prevent mem leaks in the (unlikely) event that getaddrinfo returns
no addresses. ALso, remove an unneeded NULL check in addr_ntop. From khaleesicodes via github PR#281, ok deraadt@
-rw-r--r--usr.bin/ssh/addr.c22
1 files changed, 17 insertions, 5 deletions
diff --git a/usr.bin/ssh/addr.c b/usr.bin/ssh/addr.c
index d7e73f92ee9..ac0ef96c041 100644
--- a/usr.bin/ssh/addr.c
+++ b/usr.bin/ssh/addr.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: addr.c,v 1.3 2021/10/22 09:22:04 dtucker Exp $ */
+/* $OpenBSD: addr.c,v 1.4 2021/10/22 10:51:57 dtucker Exp $ */
/*
* Copyright (c) 2004-2008 Damien Miller <djm@mindrot.org>
@@ -308,9 +308,14 @@ addr_pton(const char *p, struct xaddr *n)
if (p == NULL || getaddrinfo(p, NULL, &hints, &ai) != 0)
return -1;
- if (ai == NULL || ai->ai_addr == NULL)
+ if (ai == NULL)
return -1;
+ if (ai->ai_addr == NULL) {
+ freeaddrinfo(ai);
+ return -1;
+ }
+
if (n != NULL && addr_sa_to_xaddr(ai->ai_addr, ai->ai_addrlen,
n) == -1) {
freeaddrinfo(ai);
@@ -332,12 +337,19 @@ addr_sa_pton(const char *h, const char *s, struct sockaddr *sa, socklen_t slen)
if (h == NULL || getaddrinfo(h, s, &hints, &ai) != 0)
return -1;
- if (ai == NULL || ai->ai_addr == NULL)
+ if (ai == NULL)
return -1;
+ if (ai->ai_addr == NULL) {
+ freeaddrinfo(ai);
+ return -1;
+ }
+
if (sa != NULL) {
- if (slen < ai->ai_addrlen)
+ if (slen < ai->ai_addrlen) {
+ freeaddrinfo(ai);
return -1;
+ }
memcpy(sa, &ai->ai_addr, ai->ai_addrlen);
}
@@ -353,7 +365,7 @@ addr_ntop(const struct xaddr *n, char *p, size_t len)
if (addr_xaddr_to_sa(n, _SA(&ss), &slen, 0) == -1)
return -1;
- if (n == NULL || p == NULL || len == 0)
+ if (p == NULL || len == 0)
return -1;
if (getnameinfo(_SA(&ss), slen, p, len, NULL, 0,
NI_NUMERICHOST) == -1)