Commit 4ed209d687 for bind

commit 4ed209d687c59918eae0a272758f97ece3b0c9eb
Author: OndÅ™ej Surý <ondrej@isc.org>
Date:   Mon Nov 16 21:31:19 2026 +0100

    Answer ANY queries only over TCP

    An ANY response is the largest answer a name can produce, which makes
    ANY over UDP the query of choice for reflection and amplification
    attacks, and minimal-any only reduces the gain. Answer ANY queries
    received over UDP with an empty, truncated response, as RFC 8482
    section 4.4 allows, unless the query carries a valid server cookie,
    so that the actual response, whatever it is, is only ever sent to an
    address that cannot have been spoofed. dig, host and nslookup already
    default to TCP for ANY queries.

    Assisted-by: Claude:claude-fable-5-2

diff --git a/bin/tests/system/additional/tests.sh b/bin/tests/system/additional/tests.sh
index 792ad6345c..1198348502 100644
--- a/bin/tests/system/additional/tests.sh
+++ b/bin/tests/system/additional/tests.sh
@@ -292,8 +292,11 @@ rndc_reconfig ns1 10.53.0.1
 n=$((n + 1))
 echo_i "testing with 'minimal-any yes;' over UDP ($n)"
 ret=0
-$DIG $DIGOPTS -t ANY +notcp www.rt.example @10.53.0.1 >dig.out.$n || ret=1
-grep "ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1" dig.out.$n >/dev/null || ret=1
+# ANY queries are only answered over TCP; over UDP the response is
+# empty and truncated
+$DIG $DIGOPTS -t ANY +notcp +ignore www.rt.example @10.53.0.1 >dig.out.$n || ret=1
+grep "flags:[^;]* tc[ ;]" dig.out.$n >/dev/null || ret=1
+grep "ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 1" dig.out.$n >/dev/null || ret=1
 if [ $ret -eq 1 ]; then
   echo_i "failed"
   status=$((status + 1))
@@ -310,9 +313,11 @@ if [ $ret -eq 1 ]; then
 fi

 n=$((n + 1))
-echo_i "testing with 'minimal-any yes;' over UDP ($n)"
+echo_i "testing with 'minimal-any yes;' over UDP with TCP fallback ($n)"
 ret=0
+# the truncated UDP response makes the client retry over TCP
 $DIG $DIGOPTS -t ANY +notcp www.rt.example @10.53.0.1 >dig.out.$n || ret=1
+grep "Truncated, retrying in TCP mode" dig.out.$n >/dev/null || ret=1
 grep "ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1" dig.out.$n >/dev/null || ret=1
 if [ $ret -eq 1 ]; then
   echo_i "failed"
diff --git a/bin/tests/system/digdelv/tests_mdig.py b/bin/tests/system/digdelv/tests_mdig.py
index 3647a6a8d4..84bbc868f9 100644
--- a/bin/tests/system/digdelv/tests_mdig.py
+++ b/bin/tests/system/digdelv/tests_mdig.py
@@ -88,7 +88,7 @@ def test_yaml_output(mdig, ns2):
     """
     Check the structure of mdig +yaml output.
     """
-    result = mdig(f"+yaml @{ns2.ip} -t any ns2.example")
+    result = mdig(f"+tcp +yaml @{ns2.ip} -t any ns2.example")
     response = parse_yaml(result.out)[0]["message"]["response_message_data"]
     assert response["status"] == "NOERROR"
     assert response["QUESTION_SECTION"][0] == "ns2.example. IN ANY"
diff --git a/bin/tests/system/redirect/tests_redirect_qminzone.py b/bin/tests/system/redirect/tests_redirect_qminzone.py
index 28a84976d7..cc0710fbc3 100644
--- a/bin/tests/system/redirect/tests_redirect_qminzone.py
+++ b/bin/tests/system/redirect/tests_redirect_qminzone.py
@@ -58,9 +58,9 @@ def bootstrap():
     }


-def check(ns, qtype, dnssec, has_ad, rcode, has_answer):
+def check(ns, qtype, dnssec, has_ad, rcode, has_answer, transport=isctest.query.udp):
     msg = isctest.query.create("foo.test.", qtype, dnssec=dnssec)
-    response = isctest.query.udp(msg, ns.ip)
+    response = transport(msg, ns.ip)
     isctest.check.rcode(response, rcode)

     if has_ad:
@@ -99,6 +99,6 @@ def test_redirect_zone_qmin(ns11, qtype, dnssec, has_ad, rcode, has_answer):
 def test_redirect_zone_qmin_any_refused(ns11, dnssec):
     ns11.rndc("flush")
     # recursive ANY queries are refused, with a cold or a warm cache,
-    # instead of being redirected
+    # instead of being redirected; ANY queries are only answered over TCP
     for _ in range(2):
-        check(ns11, "ANY", dnssec, False, dns.rcode.REFUSED, False)
+        check(ns11, "ANY", dnssec, False, dns.rcode.REFUSED, False, isctest.query.tcp)
diff --git a/bin/tests/system/resolver/tests_resolver.py b/bin/tests/system/resolver/tests_resolver.py
index e1a5469fa3..c81dc1cf57 100644
--- a/bin/tests/system/resolver/tests_resolver.py
+++ b/bin/tests/system/resolver/tests_resolver.py
@@ -13,6 +13,7 @@ import time

 from dns.edns import EDECode

+import dns.edns
 import dns.message
 import dns.rcode
 import dns.rdatatype
@@ -21,14 +22,82 @@ import pytest
 import isctest


-@pytest.mark.parametrize("transport", [isctest.query.udp, isctest.query.tcp])
-def test_resolver_any_mx_only(ns5, transport):
+@pytest.mark.parametrize(
+    "server,qname,rd",
+    [
+        pytest.param("10.53.0.4", "mx-only.tld.", False, id="authoritative"),
+        pytest.param("10.53.0.4", "nxdomain-any.tld.", False, id="nxdomain"),
+        pytest.param("10.53.0.5", "mx-only.tld.", True, id="recursive"),
+        pytest.param("10.53.0.5", "mx-only.tld.", False, id="non-recursive"),
+    ],
+)
+def test_any_udp_truncated(server, qname, rd):
+    # ANY queries are only answered over TCP. Over UDP the response is
+    # always empty and truncated, whatever the response over TCP would
+    # be: authoritative data or NXDOMAIN (ns4), REFUSED from a resolver
+    # (ns5) when recursion is requested, or the cache contents when it
+    # is not.
+    msg = isctest.query.create(qname, "ANY", rd=rd, dnssec=False)
+    res = isctest.query.udp(msg, server)
+    isctest.check.noerror(res)
+    isctest.check.tcflag(res)
+    isctest.check.noaaflag(res)
+    isctest.check.empty_answer(res)
+    isctest.check.empty_authority(res)
+    isctest.check.empty_additional(res)
+
+
+def test_any_udp_server_cookie():
+    # A valid server cookie proves that the client address is not
+    # spoofed, so the ANY query is answered over UDP.  Get the server
+    # cookie with a query of another type first.
+    client_cookie = dns.edns.GenericOption(dns.edns.OptionType.COOKIE, b"\x01" * 8)
+    msg = isctest.query.create("mx-only.tld.", "MX", rd=False, dnssec=False)
+    msg.use_edns(edns=0, options=[client_cookie])
+    res = isctest.query.udp(msg, "10.53.0.4")
+    isctest.check.noerror(res)
+    cookies = [
+        option.to_wire()
+        for option in res.options
+        if option.otype == dns.edns.OptionType.COOKIE
+    ]
+    assert len(cookies) == 1 and len(cookies[0]) > 8, res
+
+    # A client cookie alone proves nothing.
+    msg = isctest.query.create("mx-only.tld.", "ANY", rd=False, dnssec=False)
+    msg.use_edns(edns=0, options=[client_cookie])
+    res = isctest.query.udp(msg, "10.53.0.4")
+    isctest.check.noerror(res)
+    isctest.check.tcflag(res)
+    isctest.check.empty_answer(res)
+
+    full_cookie = dns.edns.GenericOption(dns.edns.OptionType.COOKIE, cookies[0])
+    msg = isctest.query.create("mx-only.tld.", "ANY", rd=False, dnssec=False)
+    msg.use_edns(edns=0, options=[full_cookie])
+    res = isctest.query.udp(msg, "10.53.0.4")
+    isctest.check.noerror(res)
+    isctest.check.notcflag(res)
+    isctest.check.aaflag(res)
+    assert [rrset.rdtype for rrset in res.answer] == [dns.rdatatype.MX]
+
+
+def test_any_tcp_authoritative():
+    # The authoritative answer withheld over UDP is given over TCP.
+    msg = isctest.query.create("mx-only.tld.", "ANY", rd=False, dnssec=False)
+    res = isctest.query.tcp(msg, "10.53.0.4")
+    isctest.check.noerror(res)
+    isctest.check.notcflag(res)
+    isctest.check.aaflag(res)
+    assert [rrset.rdtype for rrset in res.answer] == [dns.rdatatype.MX]
+
+
+def test_resolver_any_mx_only(ns5):
     ns5.rndc("flush")

     # A cache miss cannot tell us which types exist at the name. Refuse
     # ANY instead of substituting an address query or a NODATA answer.
     msg = isctest.query.create("mx-only.tld.", "ANY", dnssec=False)
-    res = transport(msg, ns5.ip, expected_rcode=dns.rcode.REFUSED)
+    res = isctest.query.tcp(msg, ns5.ip, expected_rcode=dns.rcode.REFUSED)
     isctest.check.refused(res)
     isctest.check.empty_answer(res)
     isctest.check.ede(res, EDECode.NOT_SUPPORTED)
@@ -36,20 +105,19 @@ def test_resolver_any_mx_only(ns5, transport):
     # An explicit MX query still resolves. Refuse ANY consistently even
     # after that RRset has been cached.
     msg = isctest.query.create("mx-only.tld.", "MX", dnssec=False)
-    mx = transport(msg, ns5.ip)
+    mx = isctest.query.tcp(msg, ns5.ip)
     isctest.check.noerror(mx)
     assert len(mx.answer) == 1
     assert mx.answer[0].rdtype == dns.rdatatype.MX

     msg = isctest.query.create("mx-only.tld.", "ANY", dnssec=False)
-    res = transport(msg, ns5.ip, expected_rcode=dns.rcode.REFUSED)
+    res = isctest.query.tcp(msg, ns5.ip, expected_rcode=dns.rcode.REFUSED)
     isctest.check.refused(res)
     isctest.check.empty_answer(res)
     isctest.check.ede(res, EDECode.NOT_SUPPORTED)


-@pytest.mark.parametrize("transport", [isctest.query.udp, isctest.query.tcp])
-def test_resolver_any_norecurse(ns5, transport):
+def test_resolver_any_norecurse(ns5):
     ns5.rndc("flush")

     # Without RD, an ANY query only inspects the cache. On a cache miss
@@ -58,7 +126,7 @@ def test_resolver_any_norecurse(ns5, transport):
     rcodes = {}
     for qtype in ("MX", "ANY"):
         msg = isctest.query.create("mx-only.tld.", qtype, rd=False, dnssec=False)
-        res = transport(msg, ns5.ip)
+        res = isctest.query.tcp(msg, ns5.ip)
         isctest.check.empty_answer(res)
         rcodes[qtype] = res.rcode()
     assert rcodes["ANY"] == rcodes["MX"]
@@ -67,21 +135,21 @@ def test_resolver_any_norecurse(ns5, transport):
     # Once a recursive query has populated the cache, the non-recursive
     # ANY query returns what is cached at the name.
     msg = isctest.query.create("mx-only.tld.", "MX", dnssec=False)
-    mx = transport(msg, ns5.ip)
+    mx = isctest.query.tcp(msg, ns5.ip)
     isctest.check.noerror(mx)

     msg = isctest.query.create("mx-only.tld.", "ANY", rd=False, dnssec=False)
-    res = transport(msg, ns5.ip)
+    res = isctest.query.tcp(msg, ns5.ip)
     isctest.check.noerror(res)
     assert [rrset.rdtype for rrset in res.answer] == [dns.rdatatype.MX]

     # A cached NXDOMAIN is returned for a non-recursive ANY query, too.
     msg = isctest.query.create("nxdomain-any.tld.", "A", dnssec=False)
-    res = transport(msg, ns5.ip, expected_rcode=dns.rcode.NXDOMAIN)
+    res = isctest.query.tcp(msg, ns5.ip, expected_rcode=dns.rcode.NXDOMAIN)
     isctest.check.nxdomain(res)

     msg = isctest.query.create("nxdomain-any.tld.", "ANY", rd=False, dnssec=False)
-    res = transport(msg, ns5.ip, expected_rcode=dns.rcode.NXDOMAIN)
+    res = isctest.query.tcp(msg, ns5.ip, expected_rcode=dns.rcode.NXDOMAIN)
     isctest.check.nxdomain(res)
     isctest.check.empty_answer(res)

@@ -164,7 +232,7 @@ def test_resolver_any_refused(ns1):
     # (Not Supported), without looking the name up or sending anything
     # upstream
     msg = isctest.query.create("www.example.org.", "ANY")
-    res = isctest.query.udp(msg, "10.53.0.1", expected_rcode=dns.rcode.REFUSED)
+    res = isctest.query.tcp(msg, "10.53.0.1", expected_rcode=dns.rcode.REFUSED)
     isctest.check.refused(res)
     isctest.check.empty_answer(res)
     isctest.check.ede(res, EDECode.NOT_SUPPORTED)
@@ -176,7 +244,7 @@ def test_resolver_any_refused(ns1):
     assert res.answer[0].rdtype == dns.rdatatype.A

     msg = isctest.query.create("www.example.org.", "ANY")
-    res = isctest.query.udp(msg, "10.53.0.1", expected_rcode=dns.rcode.REFUSED)
+    res = isctest.query.tcp(msg, "10.53.0.1", expected_rcode=dns.rcode.REFUSED)
     isctest.check.refused(res)
     isctest.check.empty_answer(res)
     isctest.check.ede(res, EDECode.NOT_SUPPORTED)
@@ -188,14 +256,14 @@ def test_resolver_any_refused_below_delegation():
     # only be answered by recursing, so it is refused when recursion
     # is requested...
     msg = isctest.query.create("foo.bar.sub.tld1.", "ANY")
-    res = isctest.query.udp(msg, "10.53.0.7", expected_rcode=dns.rcode.REFUSED)
+    res = isctest.query.tcp(msg, "10.53.0.7", expected_rcode=dns.rcode.REFUSED)
     isctest.check.refused(res)
     isctest.check.empty_answer(res)
     isctest.check.ede(res, EDECode.NOT_SUPPORTED)

     # ...and answered with the referral when it is not
     msg = isctest.query.create("foo.bar.sub.tld1.", "ANY", rd=False)
-    res = isctest.query.udp(msg, "10.53.0.7")
+    res = isctest.query.tcp(msg, "10.53.0.7")
     isctest.check.noerror(res)
     isctest.check.empty_answer(res)
     assert any(
diff --git a/bin/tests/system/synthrecord/tests_synthrecord.py b/bin/tests/system/synthrecord/tests_synthrecord.py
index 0d44ced848..5938adf405 100644
--- a/bin/tests/system/synthrecord/tests_synthrecord.py
+++ b/bin/tests/system/synthrecord/tests_synthrecord.py
@@ -112,7 +112,9 @@ def test_synthrecord_reverse_hasdata(qname, rname, ttl):
 )
 def test_synthrecord_forward(qname, qtype, rname, rtype, ttl):
     msg = isctest.query.create(qname, qtype)
-    res = isctest.query.udp(msg, SERVER)
+    # ANY queries are only answered over TCP
+    transport = isctest.query.tcp if qtype == "ANY" else isctest.query.udp
+    res = transport(msg, SERVER)
     isctest.check.noerror(res)
     if qname[-1] != ".":
         qname += "."
@@ -283,7 +285,8 @@ def test_synthrecord_defaults(qname, rname):
 )
 def test_synthrecord_reverse_anysoa(qname, qtype, rcode, answerscount):
     msg = isctest.query.create(qname, qtype)
-    res = isctest.query.udp(msg, SERVER)
+    # ANY queries are only answered over TCP
+    res = isctest.query.tcp(msg, SERVER)
     assert len(res.answer) == answerscount
     assert res.rcode() == rcode

diff --git a/doc/arm/reference.rst b/doc/arm/reference.rst
index 301f1cbeea..823e0635a7 100644
--- a/doc/arm/reference.rst
+++ b/doc/arm/reference.rst
@@ -2086,6 +2086,16 @@ Boolean Options
    unnecessary records are added to the authority or additional
    sections. The default is ``yes``.

+   Queries of type ANY are only answered when the client address
+   cannot be spoofed: over TCP and other connection-oriented
+   transports, or over UDP when the query carries a valid server
+   cookie. Any other query of type ANY received over UDP is answered
+   with an empty, truncated (TC=1) response, which prompts a legitimate
+   client to retry over TCP; :iscman:`dig`, :iscman:`host`, and
+   :iscman:`nslookup` already send ANY queries over TCP by default.
+   This option therefore only affects the responses to the queries
+   that are answered.
+
    Queries of type ANY are never resolved recursively. When recursion
    is requested and available to the client, a query of type ANY that
    cannot be answered from authoritative data is refused with the
diff --git a/lib/ns/query.c b/lib/ns/query.c
index 7c5d81a284..00ce222aea 100644
--- a/lib/ns/query.c
+++ b/lib/ns/query.c
@@ -5186,8 +5186,18 @@ ns__query_start(query_ctx_t *qctx) {

 	/*
 	 * Respond with TC=1 if we need TCP for this request.
-	 */
-	if (!qctx->client->inner.tcp && qctx->client->inner.needtcp) {
+	 *
+	 * ANY queries are only answered to clients whose address is known
+	 * not to be spoofed, that is over TCP (RFC 8482, Section 4.4) or
+	 * with a valid server cookie: otherwise the response is empty and
+	 * truncated, whatever the answer would be, so that ANY queries
+	 * cannot be used to reflect and amplify traffic towards a spoofed
+	 * address.
+	 */
+	if (!qctx->client->inner.tcp &&
+	    (qctx->client->inner.needtcp || (qctx->qtype == dns_rdatatype_any &&
+					     !qctx->client->inner.havecookie)))
+	{
 		qctx->client->message->flags &= ~DNS_MESSAGEFLAG_AA;
 		qctx->client->message->flags &= ~DNS_MESSAGEFLAG_AD;
 		qctx->client->message->flags |= DNS_MESSAGEFLAG_TC;