public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: DJ Delorie <dj@redhat.com>
To: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH v2 1/3] inet: Fix getnameinfo (NI_NOFQDN) race condition (BZ#28566)
Date: Mon, 07 Mar 2022 22:44:21 -0500	[thread overview]
Message-ID: <xnpmmx5cru.fsf@greed.delorie.com> (raw)
In-Reply-To: <20211210110733.1499984-2-adhemerval.zanella@linaro.org> (message from Adhemerval Zanella via Libc-alpha on Fri, 10 Dec 2021 08:07:31 -0300)


Reviewing the resulting text intead of the patch, because... easier?

Adhemerval Zanella via Libc-alpha <libc-alpha@sourceware.org> writes:
> static void
> nrl_domainname_core (struct scratch_buffer *tmpbuf)
> {
>   char *c;
>   struct hostent *h, th;
>   int herror;
> 
>   while (__gethostbyname_r ("localhost", &th,
> 			    tmpbuf->data, tmpbuf->length,
> 			    &h, &herror))
>     {
>       if (herror == NETDB_INTERNAL && errno == ERANGE)
> 	{
> 	  if (!scratch_buffer_grow (tmpbuf))
> 	    return;
> 	}
>       else
> 	break;
>     }

Ok.

>   if (h != NULL && (c = strchr (h->h_name, '.')) != NULL)
>     domain = __strdup (++c);
>   else

Ok.

>     {
>       /* The name contains no domain information.  Use the name
> 	 now to get more information.  */
>       while (__gethostname (tmpbuf->data, tmpbuf->length))
> 	if (!scratch_buffer_grow (tmpbuf))
> 	  return;

Ok.

>       if ((c = strchr (tmpbuf->data, '.')) != NULL)
> 	domain = __strdup (++c);
>       else
> 	{
> 	  /* We need to preserve the hostname.  */
> 	  const char *hstname = strdupa (tmpbuf->data);
> 	  while (__gethostbyname_r (hstname, &th,
> 				    tmpbuf->data,
> 				    tmpbuf->length,
> 				    &h, &herror))
> 	    {
> 	      if (herror == NETDB_INTERNAL && errno == ERANGE)
> 		{
> 		  if (!scratch_buffer_grow (tmpbuf))
> 		    return;
> 		}
> 	      else
> 		break;
> 	    }

Ok.

> 	  if (h != NULL && (c = strchr(h->h_name, '.')) != NULL)
> 	    domain = __strdup (++c);
> 	  else
> 	    {
> 	      struct in_addr in_addr;
> 
> 	      in_addr.s_addr = htonl (INADDR_LOOPBACK);
> 
> 	      while (__gethostbyaddr_r ((const char *) &in_addr,
> 					sizeof (struct in_addr),
> 					AF_INET, &th,
> 					tmpbuf->data,
> 					tmpbuf->length,
> 					&h, &herror))
> 		{
> 		  if (herror == NETDB_INTERNAL && errno == ERANGE)
> 		    {
> 		      if (!scratch_buffer_grow (tmpbuf))
> 			return;
> 		    }
> 		  else
> 		    break;
> 		}

Ok.

> 	      if (h != NULL && (c = strchr (h->h_name, '.')) != NULL)
> 		domain = __strdup (++c);
> 	    }
> 	}
>     }
> }

Ok.

> static char *
> nrl_domainname (void)
> {
>   static int not_first;
> 
>   if (__glibc_likely (atomic_load_acquire (&not_first) != 0))
>     return domain;

Multiple threads may have gotten to this point the "first time" (esp if
they get called while the above function is running)...

>   __libc_lock_define_initialized (static, lock);
>   __libc_lock_lock (lock);
> 
>   if (atomic_load_relaxed (&not_first) == 0)
>     {

... so we test again inside the lock, only one will get the lock *and*
see not_first still zero; ok.

>       struct scratch_buffer tmpbuf;
>       scratch_buffer_init (&tmpbuf);
> 
>       nrl_domainname_core (&tmpbuf);
> 
>       scratch_buffer_free (&tmpbuf);

Calculate the static "domain", ok

>       atomic_store_release (&not_first, 1);

Set not_first before releasing lock; Ok.

>     }
> 
>   __libc_lock_unlock (lock);

This is the only exit path, so OK.

>   return domain;
> };

Ok.

LGTM

Reviewed-by: DJ Delorie <dj@redhat.com>


  parent reply	other threads:[~2022-03-08  3:44 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-10 11:07 [PATCH v2 0/3] Fixes for getnameinfo() with NI_NOFQDN Adhemerval Zanella
2021-12-10 11:07 ` [PATCH v2 1/3] inet: Fix getnameinfo (NI_NOFQDN) race condition (BZ#28566) Adhemerval Zanella
2022-02-03 20:52   ` Adhemerval Zanella
2022-03-08  3:44   ` DJ Delorie [this message]
2021-12-10 11:07 ` [PATCH v2 2/3] inet: Remove strdupa from nrl_domainname() Adhemerval Zanella
2022-02-03 20:52   ` Adhemerval Zanella
2022-03-08  3:53   ` DJ Delorie
2021-12-10 11:07 ` [PATCH v2 3/3] inet: Return EAI_MEMORY when nrl_domainname() fails to allocate memory Adhemerval Zanella
2022-02-03 20:53   ` Adhemerval Zanella
2022-03-08  4:12   ` DJ Delorie
2022-03-08 15:43     ` Adhemerval Zanella
  -- strict thread matches above, loose matches on Subject: below --
2021-11-12 14:21 [PATCH v2 0/3] Fixes for getnameinfo() with NI_NOFQDN Adhemerval Zanella
2021-11-12 14:21 ` [PATCH v2 1/3] inet: Fix getnameinfo (NI_NOFQDN) race condition (BZ#28566) Adhemerval Zanella

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=xnpmmx5cru.fsf@greed.delorie.com \
    --to=dj@redhat.com \
    --cc=adhemerval.zanella@linaro.org \
    --cc=libc-alpha@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).