glibc/glibc-RHEL-33536-2.patch
2026-07-31 11:09:18 +00:00

197 lines
6.4 KiB
Diff

commit 81763a4f7ea1c0ac0e1f475bf92440b908624301
Author: Sergey Kolosov <skolosov@redhat.com>
Date: Mon Dec 15 13:00:01 2025 +0100
resolv: Add test for NOERROR/NODATA handling [BZ #14308]
Add a test which verifies that getaddrinfo does not fail if one of A/AAAA
responses is NOERROR/NODATA reply with recursion unavailable and the other
response provides an address.
Reviewed-by: Florian Weimer <fweimer@redhat.com>
diff --git a/resolv/Makefile b/resolv/Makefile
index 9927676edac49e63..37d899835ea2da80 100644
--- a/resolv/Makefile
+++ b/resolv/Makefile
@@ -144,6 +144,7 @@ tests-static += tst-ns_rr_cursor
ifeq (yes,$(build-shared))
tests += \
tst-getaddrinfo-eai-again-timeout \
+ tst-resolv-af-unspec-noerror-nodata \
tst-resolv-ai_idn \
tst-resolv-ai_idn-latin1 \
tst-resolv-ai_idn-nolibidn2 \
@@ -288,6 +289,8 @@ $(objpfx)mtrace-tst-resolv-res_ninit.out: $(objpfx)tst-resolv-res_ninit.out
$(objpfx)tst-bug18665-tcp: $(objpfx)libresolv.so $(shared-thread-library)
$(objpfx)tst-bug18665: $(objpfx)libresolv.so $(shared-thread-library)
+$(objpfx)tst-resolv-af-unspec-noerror-nodata: \
+ $(objpfx)libresolv.so $(shared-thread-library)
$(objpfx)tst-getaddrinfo-eai-again-timeout: \
$(objpfx)libresolv.so $(shared-thread-library)
$(objpfx)tst-resolv-ai_idn: $(objpfx)libresolv.so $(shared-thread-library)
diff --git a/resolv/tst-resolv-af-unspec-noerror-nodata.c b/resolv/tst-resolv-af-unspec-noerror-nodata.c
new file mode 100644
index 0000000000000000..be925c5f724f9301
--- /dev/null
+++ b/resolv/tst-resolv-af-unspec-noerror-nodata.c
@@ -0,0 +1,157 @@
+/* Test for BZ #14308.
+ Verify that getaddrinfo (AF_UNSPEC) succeeds if one of the A/AAAA
+ responses is a NOERROR/NODATA reply with recursion unavailable (RA=0),
+ but the other response contains a usable address.
+
+ Copyright (C) 2025 Free Software Foundation, Inc.
+ 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 <netdb.h>
+#include <resolv.h>
+#include <stdlib.h>
+#include <string.h>
+#include <support/check.h>
+#include <support/check_nss.h>
+#include <support/resolv_test.h>
+#include <support/support.h>
+
+enum scenario
+{
+ /* A contains data, AAAA is NOERROR/NODATA with RA cleared. */
+ A_AUTH_AAAA_FAKE,
+
+ /* AAAA contains data, A is NOERROR/NODATA with RA cleared. */
+ A_FAKE_AAAA_AUTH
+};
+
+static enum scenario current_scenario;
+
+/* Create a NOERROR reply without answers and with RA cleared. */
+static void
+add_fake (struct resolv_response_builder *b,
+ const char *qname, uint16_t qclass, uint16_t qtype)
+{
+ struct resolv_response_flags flags =
+ {
+ .rcode = ns_r_noerror,
+ .clear_ra = true
+ };
+ resolv_response_init (b, flags);
+ resolv_response_add_question (b, qname, qclass, qtype);
+}
+
+static void
+response (const struct resolv_response_context *ctx,
+ struct resolv_response_builder *b,
+ const char *qname, uint16_t qclass, uint16_t qtype)
+{
+ if (strcmp (qname, "foo.site.example") != 0)
+ FAIL_EXIT1 ("Unexpected qname: %s", qname);
+
+ if (qtype == T_A)
+ {
+ if (current_scenario == A_AUTH_AAAA_FAKE)
+ {
+ struct resolv_response_flags flags = { .rcode = ns_r_noerror };
+ resolv_response_init (b, flags);
+ resolv_response_add_question (b, qname, qclass, qtype);
+
+ resolv_response_section (b, ns_s_an);
+ resolv_response_open_record (b, qname, qclass, T_A, 100);
+ char addr_ipv4[4] = { 127, 128, 129, 130 };
+ resolv_response_add_data (b, addr_ipv4, sizeof (addr_ipv4));
+ resolv_response_close_record (b);
+ }
+ else if (current_scenario == A_FAKE_AAAA_AUTH)
+ add_fake (b, qname, qclass, qtype);
+ else
+ FAIL_EXIT1 ("Unknown scenario: %d", current_scenario);
+
+ return;
+ }
+
+ if (qtype == T_AAAA)
+ {
+ if (current_scenario == A_AUTH_AAAA_FAKE)
+ add_fake (b, qname, qclass, qtype);
+ else if (current_scenario == A_FAKE_AAAA_AUTH)
+ {
+ struct resolv_response_flags flags = { .rcode = ns_r_noerror };
+ resolv_response_init (b, flags);
+ resolv_response_add_question (b, qname, qclass, qtype);
+
+ resolv_response_section (b, ns_s_an);
+ resolv_response_open_record (b, qname, qclass, T_AAAA, 100);
+ char addr_ipv6[16] =
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
+ resolv_response_add_data (b, addr_ipv6, sizeof (addr_ipv6));
+ resolv_response_close_record (b);
+ }
+ else
+ FAIL_EXIT1 ("Unknown scenario: %d", current_scenario);
+
+ return;
+ }
+
+ FAIL_EXIT1 ("qtype must be one of A, AAAA");
+}
+
+static void
+query_host (const char *host_name)
+{
+ struct addrinfo hints =
+ {
+ .ai_socktype = SOCK_STREAM,
+ .ai_family = AF_UNSPEC,
+ };
+
+ struct addrinfo *result = NULL;
+ int res = getaddrinfo (host_name, "80", &hints, &result);
+
+ if (current_scenario == A_AUTH_AAAA_FAKE)
+ check_addrinfo (host_name, result, res,
+ "address: STREAM/TCP 127.128.129.130 80\n");
+ else if (current_scenario == A_FAKE_AAAA_AUTH)
+ check_addrinfo (host_name, result, res,
+ "address: STREAM/TCP ::1 80\n");
+ else
+ FAIL_EXIT1 ("Unexpected scenario: %d", current_scenario);
+
+ if (res == 0)
+ freeaddrinfo (result);
+}
+
+static int
+do_test (void)
+{
+ struct resolv_test *aux = resolv_test_start
+ ((struct resolv_redirect_config)
+ {
+ .response_callback = response,
+ });
+
+ current_scenario = A_AUTH_AAAA_FAKE;
+ query_host ("foo.site.example");
+
+ current_scenario = A_FAKE_AAAA_AUTH;
+ query_host ("foo.site.example");
+
+ resolv_test_end (aux);
+ return 0;
+}
+
+#include <support/test-driver.c>