[PATCH v3] nss: Fix getnameinfo memory leak with NI_IDN
Adhemerval Zanella Netto
adhemerval.zanella@linaro.org
Tue Nov 3 18:26:13 GMT 2026
On 20/10/26 04:41, XiaoLei Wu wrote:
> With NI_IDN, getnameinfo returns EAI_OVERFLOW without freeing the
> allocated name if the host buffer is too small. Use checked_copy
> so that this path also reaches free.
>
> Add an mtrace test for the overflow path with libidn2 disabled.
>
> Signed-off-by: XiaoLei Wu <wuxiaolei@kylinos.cn>
> ---
> Thanks, DJ Delorie and Florian Weimer.
>
> Changes in v3:
> - Remove the have-thread-library checks. I initially followed the
> IDN tests in resolv/, since resolv_test_start uses threads.
> Both NPTL and HTL currently set have-thread-library to yes, so
> these checks have no effect in those configurations. Removing
> them keeps the rules simpler and follows nss/tst-getaddrinfo4,
> which uses the same helper without these checks.
> - Keep $(shared-thread-library), since resolv_test_start starts DNS
> server threads internally. This follows nss/tst-getaddrinfo4,
> with a comment added to explain the dependency.
> - Add (C) and the year to the test's copyright notice.
> - Clarify how the empty DSO forces the strdup fallback.
>
> I kept the test in nss/ because it checks getnameinfo's overflow
> cleanup, following DJ's suggestion to keep tests with the code.
>
> The empty shared object is used for failure injection. It
> prevents use of the real libidn2 and forces the strdup fallback.
> The comment before xdlopen now explains this.
The fix and the testcase looks ok, but the patch does not apply on master
since 00151c74f4e (rebase should be simple enough). I will fix it and
apply for you. Thanks for working on this.
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
>
> Changes in v2:
> - Add an mtrace regression test.
>
> nss/Makefile | 27 +++++++++++-
> nss/getnameinfo.c | 7 +--
> nss/tst-getnameinfo-idn-leak.c | 78 ++++++++++++++++++++++++++++++++++
> nss/tst-no-libidn2.c | 2 +
> 4 files changed, 108 insertions(+), 6 deletions(-)
> create mode 100644 nss/tst-getnameinfo-idn-leak.c
> create mode 100644 nss/tst-no-libidn2.c
>
> diff --git a/nss/Makefile b/nss/Makefile
> index 5a4d5794b3..49c4aa43a7 100644
> --- a/nss/Makefile
> +++ b/nss/Makefile
> @@ -371,6 +371,12 @@ tests += tst-nss-files-hosts-getent
> tests += tst-nss-files-alias-leak
> tests += tst-nss-files-alias-truncated
> tests += tst-nss-files-network
> +
> +tests += tst-getnameinfo-idn-leak
> +modules-names += tst-no-libidn2
> +extra-test-objs += tst-no-libidn2.os
> +LDFLAGS-tst-no-libidn2.so = -Wl,-soname,libidn2.so.0
> +
> # tst_fgetgrent currently only works with shared libraries
> test-srcs := tst_fgetgrent
> ifeq ($(run-built-tests),yes)
> @@ -436,11 +442,16 @@ extra-test-objs += nss_test1.os nss_test2.os nss_test_errno.os \
> ifeq ($(run-built-tests),yes)
> ifneq (no,$(PERL))
> tests-special += $(objpfx)mtrace-tst-nss-gai-hv2-canonname.out
> +ifeq (yes,$(build-shared))
> +tests-special += $(objpfx)mtrace-tst-getnameinfo-idn-leak.out
> +endif
> endif
> endif
>
> generated += mtrace-tst-nss-gai-hv2-canonname.out \
> - tst-nss-gai-hv2-canonname.mtrace
> + tst-nss-gai-hv2-canonname.mtrace \
> + mtrace-tst-getnameinfo-idn-leak.out \
> + tst-getnameinfo-idn-leak.mtrace
>
> include ../Rules
>
> @@ -516,8 +527,22 @@ $(objpfx)tst-nss-files-alias-truncated.out: $(objpfx)libnss_files.so
> $(objpfx)tst-getaddrinfo4: \
> $(common-objpfx)resolv/libresolv.so $(shared-thread-library)
>
> +# The resolver test support starts DNS server threads.
> +$(objpfx)tst-getnameinfo-idn-leak: \
> + $(common-objpfx)resolv/libresolv.so $(shared-thread-library)
> +$(objpfx)tst-getnameinfo-idn-leak.out: $(objpfx)tst-no-libidn2.so
> +
> $(objpfx)tst-default-domain: $(objpfx)nisdomain.os
>
> +tst-getnameinfo-idn-leak-ENV = \
> + MALLOC_TRACE=$(objpfx)tst-getnameinfo-idn-leak.mtrace \
> + LD_PRELOAD=$(common-objpfx)/malloc/libc_malloc_debug.so
> +$(objpfx)mtrace-tst-getnameinfo-idn-leak.out: \
> + $(objpfx)tst-getnameinfo-idn-leak.out
> + $(common-objpfx)malloc/mtrace \
> + $(objpfx)tst-getnameinfo-idn-leak.mtrace > $@; \
> + $(evaluate-test)
> +
> tst-nss-gai-hv2-canonname-ENV = \
> MALLOC_TRACE=$(objpfx)tst-nss-gai-hv2-canonname.mtrace \
> LD_PRELOAD=$(common-objpfx)/malloc/libc_malloc_debug.so
> diff --git a/nss/getnameinfo.c b/nss/getnameinfo.c
> index f88c4d8973..1e8beb4913 100644
> --- a/nss/getnameinfo.c
> +++ b/nss/getnameinfo.c
> @@ -315,15 +315,12 @@ gni_host_inet_name (struct scratch_buffer *tmpbuf,
> if (!do_idn)
> h_name = h->h_name;
>
> - size_t len = strlen (h_name) + 1;
> - if (len > hostlen)
> - return EAI_OVERFLOW;
> - memcpy (host, h_name, len);
> + int result = checked_copy (host, hostlen, h_name);
>
> if (do_idn)
> free (h_name);
>
> - return 0;
> + return result;
> }
>
> return EAI_NONAME;
> diff --git a/nss/tst-getnameinfo-idn-leak.c b/nss/tst-getnameinfo-idn-leak.c
> new file mode 100644
> index 0000000000..3826125111
> --- /dev/null
> +++ b/nss/tst-getnameinfo-idn-leak.c
> @@ -0,0 +1,78 @@
> +/* Test for a getnameinfo memory leak with NI_IDN and a short buffer.
> + Copyright (C) 2026 The GNU Toolchain Authors.
> + This file is part of the GNU C Library.
> +
> + The GNU C Library is free software; you can redistribute it and/or
> + modify it under the terms of the GNU Lesser General Public
> + License as published by the Free Software Foundation; either
> + version 2.1 of the License, or (at your option) any later version.
> +
> + The GNU C Library is distributed in the hope that it will be useful,
> + but WITHOUT ANY WARRANTY; without even the implied warranty of
> + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> + Lesser General Public License for more details.
> +
> + You should have received a copy of the GNU Lesser General Public
> + License along with the GNU C Library; if not, see
> + <https://www.gnu.org/licenses/>. */
> +
> +#include <mcheck.h>
> +#include <netdb.h>
> +#include <netinet/in.h>
> +#include <support/check.h>
> +#include <support/resolv_test.h>
> +#include <support/xdlfcn.h>
> +
> +static void
> +response (const struct resolv_response_context *ctx,
> + struct resolv_response_builder *b,
> + const char *qname, uint16_t qclass, uint16_t qtype)
> +{
> + TEST_COMPARE_STRING (qname, "1.2.0.192.in-addr.arpa");
> + TEST_COMPARE (qclass, C_IN);
> + TEST_COMPARE (qtype, T_PTR);
> +
> + resolv_response_init (b, (struct resolv_response_flags) { });
> + resolv_response_add_question (b, qname, qclass, qtype);
> + resolv_response_section (b, ns_s_an);
> + resolv_response_open_record (b, qname, qclass, qtype, 0);
> + resolv_response_add_name (b, "non-idn.example");
> + resolv_response_close_record (b);
> +}
> +
> +static int
> +do_test (void)
> +{
> + mtrace ();
> +
> + /* Load an empty DSO with libidn2's soname to force the
> + strdup fallback in __idna_from_dns_encoding. */
> + void *handle = xdlopen ("tst-no-libidn2.so", RTLD_LAZY);
> + void *handle2 = xdlopen ("libidn2.so.0", RTLD_LAZY | RTLD_NOLOAD);
> + TEST_VERIFY (handle2 == handle);
> + xdlclose (handle2);
> +
> + struct resolv_test *aux = resolv_test_start
> + ((struct resolv_redirect_config)
> + {
> + .response_callback = response,
> + .nscount = 1,
> + });
> +
> + struct sockaddr_in sa =
> + {
> + .sin_family = AF_INET,
> + .sin_addr = { htonl (0xc0000201) },
> + };
> + char host[1];
> +
> + int ret = getnameinfo ((const struct sockaddr *) &sa, sizeof (sa),
> + host, sizeof (host), NULL, 0, NI_IDN | NI_NAMEREQD);
> + TEST_COMPARE (ret, EAI_OVERFLOW);
> +
> + resolv_test_end (aux);
> + xdlclose (handle);
> + return 0;
> +}
> +
> +#include <support/test-driver.c>
> diff --git a/nss/tst-no-libidn2.c b/nss/tst-no-libidn2.c
> new file mode 100644
> index 0000000000..fc0214bc5d
> --- /dev/null
> +++ b/nss/tst-no-libidn2.c
> @@ -0,0 +1,2 @@
> +/* Compiled into an empty shared object. Used by
> + tst-getnameinfo-idn-leak to disable libidn2. */
More information about the Libc-alpha
mailing list