public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Siddhesh Poyarekar <siddhesh@sourceware.org>
To: glibc-cvs@sourceware.org
Subject: [glibc/siddhesh/gai-cleanup2] gaih_inet: Fix leak with AF_INET6 and AI_ALL [BZ #28852]
Date: Tue,  1 Mar 2022 02:40:57 +0000 (GMT)	[thread overview]
Message-ID: <20220301024057.26F2E3858D20@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=38454ba670d978ee9a33fdbdab4f80bdd611907b

commit 38454ba670d978ee9a33fdbdab4f80bdd611907b
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
Date:   Mon Feb 28 20:39:02 2022 +0530

    gaih_inet: Fix leak with AF_INET6 and AI_ALL [BZ #28852]
    
    Resolves: BZ #28852
    
    Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

Diff:
---
 sysdeps/posix/getaddrinfo.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c
index 010360986a..0173397fe1 100644
--- a/sysdeps/posix/getaddrinfo.c
+++ b/sysdeps/posix/getaddrinfo.c
@@ -243,8 +243,7 @@ convert_hostent_to_gaih_addrtuple (const struct addrinfo *req,
 				   struct hostent *h,
 				   struct gaih_addrtuple **result)
 {
-  while (*result)
-    result = &(*result)->next;
+  assert (*result == NULL);
 
   /* Count the number of addresses in h->h_addr_list.  */
   size_t count = 0;
@@ -319,14 +318,22 @@ convert_hostent_to_gaih_addrtuple (const struct addrinfo *req,
     }									      \
   else if (status == NSS_STATUS_SUCCESS)				      \
     {									      \
+      struct gaih_addrtuple *addrmem = NULL;				      \
       if (!convert_hostent_to_gaih_addrtuple (req, _family, &th, &addrmem))   \
 	{								      \
 	  __resolv_context_put (res_ctx);				      \
 	  result = -EAI_SYSTEM;						      \
 	  goto free_and_return;						      \
 	}								      \
+      if (addrmem && !gaih_lookup_result_push_alloc (&res, addrmem))	      \
+	{								      \
+	  free (addrmem);						      \
+	  result = -EAI_MEMORY;						      \
+	  goto free_and_return;						      \
+	}								      \
       *pat = addrmem;							      \
-									      \
+      while (*pat != NULL)						      \
+	pat = &((*pat)->next);						      \
       if (localcanon != NULL && canon == NULL)				      \
 	{								      \
 	  canonbuf = __strdup (localcanon);				      \
@@ -338,7 +345,7 @@ convert_hostent_to_gaih_addrtuple (const struct addrinfo *req,
 	    }								      \
 	  canon = canonbuf;						      \
 	}								      \
-      if (_family == AF_INET6 && *pat != NULL)				      \
+      if (_family == AF_INET6 && addrmem != NULL)			      \
 	got_ipv6 = true;						      \
     }									      \
  }
@@ -783,7 +790,6 @@ gaih_inet (const char *name, const struct gaih_service *service,
     return rc;
 
   bool malloc_name = false;
-  struct gaih_addrtuple *addrmem = NULL;
   char *canonbuf = NULL;
   int result = 0;
   struct gaih_lookup_result res = {0};
@@ -1228,7 +1234,6 @@ gaih_inet (const char *name, const struct gaih_service *service,
  free_and_return:
   if (malloc_name)
     free ((char *) name);
-  free (addrmem);
   free (canonbuf);
   gaih_lookup_result_free (&res);
   scratch_buffer_free (&resbuf);


                 reply	other threads:[~2022-03-01  2:40 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220301024057.26F2E3858D20@sourceware.org \
    --to=siddhesh@sourceware.org \
    --cc=glibc-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).