Commit 1a1ac5ccd1 for bind
commit 1a1ac5ccd16234ba9156e4195b2c50044d255448
Author: Štěpán Balážik <stepan@isc.org>
Date: Mon Oct 19 18:05:07 2026 +0200
Derive the forged RRSIGs' key tag from the zone
The forgery only works if its RRSIG names a key tag none of the zone's
keys has. With a fixed tag, the freshly generated key got that tag
once in 65536 runs; the resolver then tried the key, failed the
signature and answered SERVFAIL, so the tests were flaky, as they were
before the rewrite. Pick the lowest tag the zone's DNSKEYs do not use,
and check that in the tests, against the keys in the signed zone,
instead of a fixed number.
Assisted-by: Claude:claude-fable-5-1
diff --git a/bin/tests/system/nsec_synthesis/ans1/ans.py b/bin/tests/system/nsec_synthesis/ans1/ans.py
index b8ed03a942..8617fcf283 100644
--- a/bin/tests/system/nsec_synthesis/ans1/ans.py
+++ b/bin/tests/system/nsec_synthesis/ans1/ans.py
@@ -29,7 +29,6 @@ class AttackCoveringNsecHandler(AttackNsecHandler):
Qname("attacker.f004.test.") & Qtype(dns.rdatatype.NSEC) & Flags(dns.flags.CD)
)
nsec = "b.victim.f004.test. NS SOA RRSIG NSEC DNSKEY"
- key_tag = 9999
class AttackNodataNsecHandler(AttackNsecHandler):
@@ -43,7 +42,6 @@ class AttackNodataNsecHandler(AttackNsecHandler):
Qname("victim.f004.test.") & Qtype(dns.rdatatype.NSEC) & Flags(dns.flags.CD)
)
nsec = "z.f004.test. TXT RRSIG NSEC"
- key_tag = 9999
def main() -> None:
diff --git a/bin/tests/system/nsec_synthesis/ans4/ans.py b/bin/tests/system/nsec_synthesis/ans4/ans.py
index 5c91b5988c..3240a83eb4 100644
--- a/bin/tests/system/nsec_synthesis/ans4/ans.py
+++ b/bin/tests/system/nsec_synthesis/ans4/ans.py
@@ -27,7 +27,6 @@ class AttackChildNsecHandler(AttackNsecHandler):
matcher = Qname("0.evil.f023.test.") & Qtype(dns.rdatatype.NSEC)
nsec = "zzz.f023.test. A RRSIG NSEC"
- key_tag = 12345
def main() -> None:
diff --git a/bin/tests/system/nsec_synthesis/common.py b/bin/tests/system/nsec_synthesis/common.py
index 21c23cff7d..99ab5d85a8 100644
--- a/bin/tests/system/nsec_synthesis/common.py
+++ b/bin/tests/system/nsec_synthesis/common.py
@@ -36,6 +36,8 @@ F004_ZONE = "f004.test."
F004_VICTIM = f"victim.{F004_ZONE}"
F004_VICTIM_A = "203.0.113.1"
+F023_ZONE = "f023.test."
+
def f004_bootstrap():
zone = isctest.zone.Zone(F004_ZONE, isctest.template.ANS1, signed=True)
@@ -70,6 +72,6 @@ def check_nsec(response, section, owner, next_name, signer):
return rrsig
-def check_forged_nsec(response, section, owner, next_name, zone, key_tag):
+def check_forged_nsec(response, section, owner, next_name, zone, key_tags):
rrsig = check_nsec(response, section, owner, next_name, zone)
- assert rrsig[0].key_tag == key_tag, response.to_text()
+ assert rrsig[0].key_tag not in key_tags, response.to_text()
diff --git a/bin/tests/system/nsec_synthesis/conftest.py b/bin/tests/system/nsec_synthesis/conftest.py
new file mode 100644
index 0000000000..c0b8018aec
--- /dev/null
+++ b/bin/tests/system/nsec_synthesis/conftest.py
@@ -0,0 +1,42 @@
+# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
+#
+# SPDX-License-Identifier: MPL-2.0
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, you can obtain one at https://mozilla.org/MPL/2.0/.
+#
+# See the COPYRIGHT file distributed with this work for additional
+# information regarding copyright ownership.
+
+
+import dns.dnssec
+import dns.rdatatype
+import dns.zone
+import pytest
+
+from nsec_synthesis.common import F004_ZONE, F023_ZONE
+
+import isctest.template
+import isctest.zone
+
+
+def zone_key_tags(zone):
+ signed = dns.zone.from_file(str(zone.path_signed), zone.dname, relativize=False)
+ dnskeys = signed.get_rdataset(zone.dname, dns.rdatatype.DNSKEY)
+ assert dnskeys, zone.path_signed
+ return {dns.dnssec.key_id(dnskey) for dnskey in dnskeys}
+
+
+@pytest.fixture(scope="module")
+def f004_key_tags():
+ return zone_key_tags(
+ isctest.zone.Zone(F004_ZONE, isctest.template.ANS1, signed=True)
+ )
+
+
+@pytest.fixture(scope="module")
+def f023_key_tags():
+ return zone_key_tags(
+ isctest.zone.Zone(F023_ZONE, isctest.template.ANS4, signed=True)
+ )
diff --git a/bin/tests/system/nsec_synthesis/nsec_synthesis_ans.py b/bin/tests/system/nsec_synthesis/nsec_synthesis_ans.py
index 93af3a7a06..bbee5a0177 100644
--- a/bin/tests/system/nsec_synthesis/nsec_synthesis_ans.py
+++ b/bin/tests/system/nsec_synthesis/nsec_synthesis_ans.py
@@ -12,6 +12,7 @@
from collections.abc import AsyncGenerator
+import dns.dnssec
import dns.name
import dns.rdataclass
import dns.rdatatype
@@ -38,16 +39,24 @@ class AttackNsecHandler(ResponseHandler):
"""
nsec: str
- key_tag: int
async def get_responses(
self, qctx: QueryContext
) -> AsyncGenerator[DnsResponseSend, None]:
qctx.prepare_new_response(with_zone_data=False)
+
nsec_rrset = rrset(qctx.qname, dns.rdatatype.NSEC, self.nsec)
qctx.response.answer.append(nsec_rrset)
- rrsig = qctx.sign(nsec_rrset, bogus=True)[0].replace(key_tag=self.key_tag)
+
+ assert qctx.zone and qctx.zone.origin
+ dnskeys = qctx.zone.get_rrset(qctx.zone.origin, dns.rdatatype.DNSKEY)
+ assert dnskeys
+ zone_tags = {dns.dnssec.key_id(dnskey) for dnskey in dnskeys}
+ unknown_tag = next((tag for tag in range(65536) if tag not in zone_tags), 0)
+
+ rrsig = qctx.sign(nsec_rrset, bogus=True)[0].replace(key_tag=unknown_tag)
qctx.response.answer.append(
dns.rrset.from_rdata(nsec_rrset.name, nsec_rrset.ttl, rrsig)
)
+
yield DnsResponseSend(qctx.response, authoritative=True)
diff --git a/bin/tests/system/nsec_synthesis/tests_nsec_child_next.py b/bin/tests/system/nsec_synthesis/tests_nsec_child_next.py
index 983c1d3c4e..68a7c8cec2 100644
--- a/bin/tests/system/nsec_synthesis/tests_nsec_child_next.py
+++ b/bin/tests/system/nsec_synthesis/tests_nsec_child_next.py
@@ -9,13 +9,18 @@
# See the COPYRIGHT file distributed with this work for additional
# information regarding copyright ownership.
-from nsec_synthesis.common import SIGNED_ZONE_MARKS, check_forged_nsec, get_nsec, has_a
+from nsec_synthesis.common import (
+ F023_ZONE,
+ SIGNED_ZONE_MARKS,
+ check_forged_nsec,
+ get_nsec,
+ has_a,
+)
import isctest
import isctest.template
import isctest.zone
-F023_ZONE = "f023.test."
EVIL = f"evil.{F023_ZONE}"
POISON = f"0.{EVIL}"
POISON_NEXT = f"zzz.{F023_ZONE}"
@@ -45,20 +50,24 @@ def _prime_parent_for_aggressive_nsec():
isctest.check.adflag(nsec)
-def test_direct_insecure_child_nsec_next_fixture():
+def test_direct_insecure_child_nsec_next_fixture(f023_key_tags):
poison = isctest.query.tcp(isctest.query.create(POISON, "NSEC"), AUTH)
isctest.check.noerror(poison)
isctest.check.aaflag(poison)
- check_forged_nsec(poison, poison.answer, POISON, POISON_NEXT, F023_ZONE, 12345)
+ check_forged_nsec(
+ poison, poison.answer, POISON, POISON_NEXT, F023_ZONE, f023_key_tags
+ )
-def test_resolver_does_not_synth_from_insecure_child_nsec():
+def test_resolver_does_not_synth_from_insecure_child_nsec(f023_key_tags):
_prime_parent_for_aggressive_nsec()
poison = isctest.query.tcp(isctest.query.create(POISON, "NSEC"), RESOLVER)
isctest.check.noerror(poison)
isctest.check.noadflag(poison)
- check_forged_nsec(poison, poison.answer, POISON, POISON_NEXT, F023_ZONE, 12345)
+ check_forged_nsec(
+ poison, poison.answer, POISON, POISON_NEXT, F023_ZONE, f023_key_tags
+ )
response = isctest.query.tcp(isctest.query.create(TARGET, "A"), RESOLVER)
isctest.check.noerror(response)
diff --git a/bin/tests/system/nsec_synthesis/tests_nsec_exact_nodata.py b/bin/tests/system/nsec_synthesis/tests_nsec_exact_nodata.py
index c8d743cbde..40bb87cd13 100644
--- a/bin/tests/system/nsec_synthesis/tests_nsec_exact_nodata.py
+++ b/bin/tests/system/nsec_synthesis/tests_nsec_exact_nodata.py
@@ -33,7 +33,7 @@ def bootstrap():
return f004_bootstrap()
-def test_pending_exact_nodata_nsec_cache_poisoning():
+def test_pending_exact_nodata_nsec_cache_poisoning(f004_key_tags):
"""
Companion to #5872 that covers the query_coveringnsec() trust check on
the exact-match NODATA branch (the `if (exists)` path in ns/query.c),
@@ -56,7 +56,9 @@ def test_pending_exact_nodata_nsec_cache_poisoning():
)
isctest.check.noerror(poison)
isctest.check.noadflag(poison)
- check_forged_nsec(poison, poison.answer, F004_VICTIM, POISON_NEXT, F004_ZONE, 9999)
+ check_forged_nsec(
+ poison, poison.answer, F004_VICTIM, POISON_NEXT, F004_ZONE, f004_key_tags
+ )
# Query the victim's A record. The pending NSEC must not be used to
# synthesize a NODATA; the resolver fetches and validates the real A.
diff --git a/bin/tests/system/nsec_synthesis/tests_nsec_pending_cd.py b/bin/tests/system/nsec_synthesis/tests_nsec_pending_cd.py
index aadb0853e7..b0533d1263 100644
--- a/bin/tests/system/nsec_synthesis/tests_nsec_pending_cd.py
+++ b/bin/tests/system/nsec_synthesis/tests_nsec_pending_cd.py
@@ -34,7 +34,7 @@ def bootstrap():
return f004_bootstrap()
-def test_pending_trust_nsec_cd_cache_poisoning():
+def test_pending_trust_nsec_cd_cache_poisoning(f004_key_tags):
"""
Reproducer for #5872:
F-004 Cache Poisoning via Pending-Trust NSEC in Aggressive Cache
@@ -50,7 +50,9 @@ def test_pending_trust_nsec_cd_cache_poisoning():
)
isctest.check.noerror(poison)
isctest.check.noadflag(poison)
- check_forged_nsec(poison, poison.answer, ATTACKER, POISON_NEXT, F004_ZONE, 9999)
+ check_forged_nsec(
+ poison, poison.answer, ATTACKER, POISON_NEXT, F004_ZONE, f004_key_tags
+ )
# Step 3: Exploit Against Victim Domain
response = isctest.query.tcp(isctest.query.create(F004_VICTIM, "A"), RESOLVER)