From patchwork Fri Dec 10 11:07:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 48771 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 246B6385803A for ; Fri, 10 Dec 2021 11:08:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 246B6385803A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1639134521; bh=Lz3QbDTDHHGeG4Pb5D+gU9VZBhkDT0aIO+TB70kRLlQ=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=AwOw0AW5+GohJqpq6oxhL3QOOKOioxDBI1TEH0+ny6uK1GpgCvidpPG3wzmSh6CAr t4DwA6p5p/gVme5dIB4LkwBWVYjBHv7IA73uWEAA3swv7A93T5sG8uynEJwW6Nl950 I92Qznfwi6tYUdPXbhTmAFgDCf9ci03J8jQjmvhI= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ua1-x929.google.com (mail-ua1-x929.google.com [IPv6:2607:f8b0:4864:20::929]) by sourceware.org (Postfix) with ESMTPS id ED1F43858C27 for ; Fri, 10 Dec 2021 11:07:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org ED1F43858C27 Received: by mail-ua1-x929.google.com with SMTP id ay21so16029265uab.12 for ; Fri, 10 Dec 2021 03:07:37 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Lz3QbDTDHHGeG4Pb5D+gU9VZBhkDT0aIO+TB70kRLlQ=; b=gwEq0PlY06FWV72MkRWjYy0jgR3gZcOGo0nfbP1GaVWZQ5azHh2+OAmvjFG9OWujsj TRPUxuHFYAp8q5eMQBDvEd0QWu8I6KKiedYr/bwR+roX2DwG9AbXWYU4tfLHsrmjxnpH dqdtEduf5mtD0BzoF5sWuRJAtI4jDGUoPLHWqYJ1NS+YlrsPNHdc91MevzMvHSCrCk79 Q6knBK6Fz7sMW7CbZ7nmlhs45DAkB4YXtwQiY7V45rkVA5tWatOcfbql3BJDjyZhUeCW RGmYk4zedoxGWm3jYoV9OxUh4my4pz1bZwCAia3o/5vsqAd60EbIxO5lRGIBJkLfAxKd 4yGg== X-Gm-Message-State: AOAM530R30FqYeigtcXjeB8A4E8iJnZA/z470uVH+oOT5pQwpCVTO8PR NHkFs9zsyK4o5MT1aE09Mz55mu/EkcosGg== X-Google-Smtp-Source: ABdhPJwwSUHaWdI+5Noe1Vz6laNVDrX2jleT2+5CSH3G8wyYlij9/20CU8G1nxihm4e4vODHdMlr5Q== X-Received: by 2002:a67:e910:: with SMTP id c16mr14952988vso.13.1639134457312; Fri, 10 Dec 2021 03:07:37 -0800 (PST) Received: from birita.. ([2804:431:c7ca:a776:ce11:d591:8bd5:613f]) by smtp.gmail.com with ESMTPSA id w11sm1704758vkm.14.2021.12.10.03.07.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Dec 2021 03:07:37 -0800 (PST) To: libc-alpha@sourceware.org Subject: [PATCH v2 1/3] inet: Fix getnameinfo (NI_NOFQDN) race condition (BZ#28566) Date: Fri, 10 Dec 2021 08:07:31 -0300 Message-Id: <20211210110733.1499984-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211210110733.1499984-1-adhemerval.zanella@linaro.org> References: <20211210110733.1499984-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" The 'not_first' is accessed on nrl_domainname() in a non atomically way, although it is only updated after the lock is taken. This patch fix the double-checked locking by using acquire-release atomic operation instead of plain load and by moving the 'not_first' store only after 'domain' is actually set. Checked on x86_64-linux-gnu. --- inet/getnameinfo.c | 148 ++++++++++++++++++++++++--------------------- 1 file changed, 78 insertions(+), 70 deletions(-) diff --git a/inet/getnameinfo.c b/inet/getnameinfo.c index 8380d85783..5eee354200 100644 --- a/inet/getnameinfo.c +++ b/inet/getnameinfo.c @@ -83,104 +83,112 @@ libc_freeres_ptr (static char *domain); now ignored. */ #define DEPRECATED_NI_IDN 192 -static char * -nrl_domainname (void) +static void +nrl_domainname_core (struct scratch_buffer *tmpbuf) { - static int not_first; + char *c; + struct hostent *h, th; + int herror; - if (! not_first) + while (__gethostbyname_r ("localhost", &th, + tmpbuf->data, tmpbuf->length, + &h, &herror)) { - __libc_lock_define_initialized (static, lock); - __libc_lock_lock (lock); - - if (! not_first) + if (herror == NETDB_INTERNAL && errno == ERANGE) { - char *c; - struct hostent *h, th; - int herror; - struct scratch_buffer tmpbuf; - - scratch_buffer_init (&tmpbuf); - not_first = 1; + if (!scratch_buffer_grow (tmpbuf)) + return; + } + else + break; + } - while (__gethostbyname_r ("localhost", &th, - tmpbuf.data, tmpbuf.length, + if (h != NULL && (c = strchr (h->h_name, '.')) != NULL) + domain = __strdup (++c); + else + { + /* 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; + + 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)) - goto done; + if (!scratch_buffer_grow (tmpbuf)) + return; } else break; } - if (h && (c = strchr (h->h_name, '.'))) + if (h != NULL && (c = strchr(h->h_name, '.')) != NULL) domain = __strdup (++c); else { - /* 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)) - goto done; + struct in_addr in_addr; - if ((c = strchr (tmpbuf.data, '.'))) - domain = __strdup (++c); - else - { - /* We need to preserve the hostname. */ - const char *hstname = strdupa (tmpbuf.data); + in_addr.s_addr = htonl (INADDR_LOOPBACK); - while (__gethostbyname_r (hstname, &th, - tmpbuf.data, tmpbuf.length, - &h, &herror)) + 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 (herror == NETDB_INTERNAL && errno == ERANGE) - { - if (!scratch_buffer_grow (&tmpbuf)) - goto done; - } - else - break; + if (!scratch_buffer_grow (tmpbuf)) + return; } - - if (h && (c = strchr(h->h_name, '.'))) - 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)) - goto done; - } - else - break; - } - - if (h && (c = strchr (h->h_name, '.'))) - domain = __strdup (++c); - } + break; } + + if (h != NULL && (c = strchr (h->h_name, '.')) != NULL) + domain = __strdup (++c); } - done: - scratch_buffer_free (&tmpbuf); } + } +} - __libc_lock_unlock (lock); +static char * +nrl_domainname (void) +{ + static int not_first; + + if (__glibc_likely (atomic_load_acquire (¬_first) != 0)) + return domain; + + __libc_lock_define_initialized (static, lock); + __libc_lock_lock (lock); + + if (atomic_load_relaxed (¬_first) == 0) + { + struct scratch_buffer tmpbuf; + scratch_buffer_init (&tmpbuf); + + nrl_domainname_core (&tmpbuf); + + scratch_buffer_free (&tmpbuf); + + atomic_store_release (¬_first, 1); } + __libc_lock_unlock (lock); + return domain; }; From patchwork Fri Dec 10 11:07:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 48772 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8788D3858025 for ; Fri, 10 Dec 2021 11:09:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8788D3858025 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1639134563; bh=mj46Gqt6Cm0Er3p7GuzZXhAKypiyWs4l6fjNYsPmnPM=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=dNE0Pb1qrhHPhzXv/vblDiFehtm//SSE8TfHNrnm5NHYDVIJvgH81P443YaGPaG9F EmbVHjdE+rZ2+Badk8svm8iweTnM9RLnUsUiFK1JHL6KztGyec0+IfaLIeNnH+FOkZ skus7r6cgJFMc6fby3/zD+lieReJQayQg6p8UPCI= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ua1-x92a.google.com (mail-ua1-x92a.google.com [IPv6:2607:f8b0:4864:20::92a]) by sourceware.org (Postfix) with ESMTPS id E14613858D35 for ; Fri, 10 Dec 2021 11:07:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E14613858D35 Received: by mail-ua1-x92a.google.com with SMTP id o1so16117128uap.4 for ; Fri, 10 Dec 2021 03:07:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=mj46Gqt6Cm0Er3p7GuzZXhAKypiyWs4l6fjNYsPmnPM=; b=vIAHqviiukX8Vmq0rrHNau0TvWnMpK0dKLkmzVCfsDdsUj6tP2qqNkgHfYykNJMs+H fWfvk8vyLsi5OqrCft/IwKjLvD52iQiqnR8FBohr6RSBEQHiLD63C9xviXSpUNy3q9fL 11dT/0AEBQZXe1+Un+1NNwCZnIGYp3Pv3AsMYWJCoJEKX9ix/0q0NFQgixO9woGIEYJS 3atJtCDFPflXhO2mA+3Ec/XgJ+YdZHlvWjpVBh+Xx8ZvV4TFuUogTZQWNx1qBQNNNex4 hfuglSObWBpyUUGPW+65dVzJiga+jcWmV5sXNAcrpLKKnmrPvNPVcd7V8MRTq2tE1Zg2 f5xw== X-Gm-Message-State: AOAM532mgvCtlPJfbTktpI96NDO/k1CRuJP2+NotMeyuf6xT3OEbs0+J Msr1mcvhqk8pi+Y2Z0aHmENbZSw8MVEIjw== X-Google-Smtp-Source: ABdhPJzkPI/P0/Aw2RVajT4LqMmeKyu4Pg+XyVr1qb/xTq3dK3zNbGQljAyN1KdePGjqvxRcjb/iKw== X-Received: by 2002:a05:6102:3ec9:: with SMTP id n9mr14849105vsv.22.1639134458295; Fri, 10 Dec 2021 03:07:38 -0800 (PST) Received: from birita.. ([2804:431:c7ca:a776:ce11:d591:8bd5:613f]) by smtp.gmail.com with ESMTPSA id w11sm1704758vkm.14.2021.12.10.03.07.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Dec 2021 03:07:38 -0800 (PST) To: libc-alpha@sourceware.org Subject: [PATCH v2 2/3] inet: Remove strdupa from nrl_domainname() Date: Fri, 10 Dec 2021 08:07:32 -0300 Message-Id: <20211210110733.1499984-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211210110733.1499984-1-adhemerval.zanella@linaro.org> References: <20211210110733.1499984-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" We can use the already in place scratch_buffer. Checked on x86_64-linux-gnu. --- inet/getnameinfo.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/inet/getnameinfo.c b/inet/getnameinfo.c index 5eee354200..2d2397e7dc 100644 --- a/inet/getnameinfo.c +++ b/inet/getnameinfo.c @@ -118,15 +118,15 @@ nrl_domainname_core (struct scratch_buffer *tmpbuf) else { /* We need to preserve the hostname. */ - const char *hstname = strdupa (tmpbuf->data); - while (__gethostbyname_r (hstname, &th, - tmpbuf->data, - tmpbuf->length, + size_t hstnamelen = strlen (tmpbuf->data) + 1; + while (__gethostbyname_r (tmpbuf->data, &th, + tmpbuf->data + hstnamelen, + tmpbuf->length - hstnamelen, &h, &herror)) { if (herror == NETDB_INTERNAL && errno == ERANGE) { - if (!scratch_buffer_grow (tmpbuf)) + if (!scratch_buffer_grow_preserve (tmpbuf)) return; } else From patchwork Fri Dec 10 11:07:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 48773 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C1EE33858C27 for ; Fri, 10 Dec 2021 11:10:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C1EE33858C27 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1639134605; bh=X//bKTx0lfVmQu39ccV38FcCMAkJPedcGdiuPIdKDS8=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=pWDxz1uA6MnQ+9LyDHufrOMa8nsLPpihUCoR0goXN2rF2vhy68ps7+P0lDn0Y1wTl WUqEV5SXbq+Z32SItoQR+xK+XD0VyL4rNzld4cEUYhpNOjevz52IkF6L4u18r7dzI9 HJ4DlyXo6ORDSXG8DnUfFaf2rnm3zWvQHh5cwio4= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ua1-x92a.google.com (mail-ua1-x92a.google.com [IPv6:2607:f8b0:4864:20::92a]) by sourceware.org (Postfix) with ESMTPS id 7B0D53858404 for ; Fri, 10 Dec 2021 11:07:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7B0D53858404 Received: by mail-ua1-x92a.google.com with SMTP id p2so16048531uad.11 for ; Fri, 10 Dec 2021 03:07:41 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=X//bKTx0lfVmQu39ccV38FcCMAkJPedcGdiuPIdKDS8=; b=c++LVZ4TnYwt+DjM8T1RUIVvuKjUlqtGwuXIkCxjINF1HGN2p11XLD/My2W+h3co+E MNIVVAPsINsiZcLiZgPR5mtu4F/+eMLc5NPJTUvO0NJ50H4bainKoFxRNx4BJRVe3Ng8 V6JcTjeC/r9BzT+qXc62eYPKzX5qJLxfBheA1pUAPjGlhax4MwIYHjXWYrXMAfM3GRJ/ OJa9ifET+42zQO8ohCQ1krvBjDIG6llDL6bygQJeHGnQhO+NfkSrAFmoAoy0ddgbdcZL RANnRr5QKfsEoFTqbdwNraKv/+41wqP6UIX9Ng8m3RZ1YWe+vccPXJxNRWi4qxg2V55M vN3g== X-Gm-Message-State: AOAM5330cw5Qce31H6X8AuM+wGKwGSOnZJih16jlwZrslfHXAkgaALDM LWbur7Hv9LXdMyiL2HW9QM2IEuwpZVa7Mw== X-Google-Smtp-Source: ABdhPJzApwt+hLaqM0G0lzFi9z2ZJRqW30P1G7nCC03hSkxlAvCD5O0ynFIhSMVkRBUyhWW2dO+IOw== X-Received: by 2002:a67:e109:: with SMTP id d9mr14890944vsl.19.1639134459375; Fri, 10 Dec 2021 03:07:39 -0800 (PST) Received: from birita.. ([2804:431:c7ca:a776:ce11:d591:8bd5:613f]) by smtp.gmail.com with ESMTPSA id w11sm1704758vkm.14.2021.12.10.03.07.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Dec 2021 03:07:39 -0800 (PST) To: libc-alpha@sourceware.org Subject: [PATCH v2 3/3] inet: Return EAI_MEMORY when nrl_domainname() fails to allocate memory Date: Fri, 10 Dec 2021 08:07:33 -0300 Message-Id: <20211210110733.1499984-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211210110733.1499984-1-adhemerval.zanella@linaro.org> References: <20211210110733.1499984-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" It aligns NI_NOFQDN with default behavior for getnameinfo(). Checked on x86_64-linux-gnu. --- inet/getnameinfo.c | 140 +++++++++++++++++++++++---------------------- 1 file changed, 72 insertions(+), 68 deletions(-) diff --git a/inet/getnameinfo.c b/inet/getnameinfo.c index 2d2397e7dc..acf1538a37 100644 --- a/inet/getnameinfo.c +++ b/inet/getnameinfo.c @@ -83,94 +83,95 @@ libc_freeres_ptr (static char *domain); now ignored. */ #define DEPRECATED_NI_IDN 192 -static void +static bool nrl_domainname_core (struct scratch_buffer *tmpbuf) { char *c; struct hostent *h, th; int herror; - while (__gethostbyname_r ("localhost", &th, - tmpbuf->data, tmpbuf->length, + while (__gethostbyname_r ("localhost", &th, tmpbuf->data, tmpbuf->length, &h, &herror)) { if (herror == NETDB_INTERNAL && errno == ERANGE) { if (!scratch_buffer_grow (tmpbuf)) - return; + return false; } else break; } if (h != NULL && (c = strchr (h->h_name, '.')) != NULL) - domain = __strdup (++c); - else { - /* 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; - - if ((c = strchr (tmpbuf->data, '.')) != NULL) - domain = __strdup (++c); - else - { - /* We need to preserve the hostname. */ - size_t hstnamelen = strlen (tmpbuf->data) + 1; - while (__gethostbyname_r (tmpbuf->data, &th, - tmpbuf->data + hstnamelen, - tmpbuf->length - hstnamelen, - &h, &herror)) - { - if (herror == NETDB_INTERNAL && errno == ERANGE) - { - if (!scratch_buffer_grow_preserve (tmpbuf)) - return; - } - else - break; - } + domain = __strdup (++c); + return domain != NULL; + } - if (h != NULL && (c = strchr(h->h_name, '.')) != NULL) - domain = __strdup (++c); - else - { - struct in_addr in_addr; + /* 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 false; - in_addr.s_addr = htonl (INADDR_LOOPBACK); + if ((c = strchr (tmpbuf->data, '.')) != NULL) + { + domain = __strdup (++c); + return domain != NULL; + } - 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; - } + /* We need to preserve the hostname. */ + size_t hstnamelen = strlen (tmpbuf->data) + 1; + while (__gethostbyname_r (tmpbuf->data, &th, tmpbuf->data + hstnamelen, + tmpbuf->length - hstnamelen, &h, &herror)) + { + if (herror == NETDB_INTERNAL && errno == ERANGE) + { + if (!scratch_buffer_grow_preserve (tmpbuf)) + return false; + } + else + break; + } - if (h != NULL && (c = strchr (h->h_name, '.')) != NULL) - domain = __strdup (++c); - } + if (h != NULL && (c = strchr(h->h_name, '.')) != NULL) + { + domain = __strdup (++c); + return domain != NULL; + } + + struct 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 false; } + else + break; + } + + if (h != NULL && (c = strchr (h->h_name, '.')) != NULL) + { + domain = __strdup (++c); + return domain != NULL; } + return true; } -static char * +static bool nrl_domainname (void) { static int not_first; if (__glibc_likely (atomic_load_acquire (¬_first) != 0)) - return domain; + return true; + + int r = true; __libc_lock_define_initialized (static, lock); __libc_lock_lock (lock); @@ -180,16 +181,15 @@ nrl_domainname (void) struct scratch_buffer tmpbuf; scratch_buffer_init (&tmpbuf); - nrl_domainname_core (&tmpbuf); + if ((r = nrl_domainname_core (&tmpbuf))) + atomic_store_release (¬_first, 1); scratch_buffer_free (&tmpbuf); - - atomic_store_release (¬_first, 1); } __libc_lock_unlock (lock); - return domain; + return r; }; /* Copy a string to a destination buffer with length checking. Return @@ -285,13 +285,17 @@ gni_host_inet_name (struct scratch_buffer *tmpbuf, if (h) { - char *c; - if ((flags & NI_NOFQDN) - && (c = nrl_domainname ()) - && (c = strstr (h->h_name, c)) - && (c != h->h_name) && (*(--c) == '.')) - /* Terminate the string after the prefix. */ - *c = '\0'; + if (flags & NI_NOFQDN) + { + if (!nrl_domainname ()) + return EAI_MEMORY; + + char *c = domain; + if (c != NULL && (c = strstr (h->h_name, c)) + && (c != h->h_name) && (*(--c) == '.')) + /* Terminate the string after the prefix. */ + *c = '\0'; + } /* If requested, convert from the IDN format. */ bool do_idn = flags & NI_IDN;