From e8df2802ac62016ea68585893eb4310fc3329028 Mon Sep 17 00:00:00 2001 From: Mark Andrews Date: Tue, 1 Mar 2022 09:48:05 +1100 Subject: [PATCH 1/7] Add additional name checks when using a forwarder When using a forwarder, check that the owner name of response records are within the bailiwick of the forwarded name space. --- lib/dns/resolver.c | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/lib/dns/resolver.c b/lib/dns/resolver.c index b34cb12b73..98c169e4d2 100644 --- a/lib/dns/resolver.c +++ b/lib/dns/resolver.c @@ -311,6 +311,8 @@ struct fetchctx { bool ns_ttl_ok; uint32_t ns_ttl; isc_counter_t * qc; + dns_fixedname_t fwdfname; + dns_name_t *fwdname; /*% * The number of events we're waiting for. @@ -3389,6 +3391,7 @@ fctx_getaddresses(fetchctx_t *fctx, bool badcache) { if (result == ISC_R_SUCCESS) { fwd = ISC_LIST_HEAD(forwarders->fwdrs); fctx->fwdpolicy = forwarders->fwdpolicy; + dns_name_copy(domain, fctx->fwdname, NULL); if (fctx->fwdpolicy == dns_fwdpolicy_only && isstrictsubdomain(domain, &fctx->domain)) { fcount_decr(fctx); @@ -4418,6 +4421,9 @@ fctx_create(dns_resolver_t *res, dns_name_t *name, dns_rdatatype_t type, fctx->restarts = 0; fctx->querysent = 0; fctx->referrals = 0; + + fctx->fwdname = dns_fixedname_initname(&fctx->fwdfname); + TIME_NOW(&fctx->start); fctx->timeouts = 0; fctx->lamecount = 0; @@ -4476,8 +4482,10 @@ fctx_create(dns_resolver_t *res, dns_name_t *name, dns_rdatatype_t type, domain = dns_fixedname_initname(&fixed); result = dns_fwdtable_find2(fctx->res->view->fwdtable, fwdname, domain, &forwarders); - if (result == ISC_R_SUCCESS) + if (result == ISC_R_SUCCESS) { fctx->fwdpolicy = forwarders->fwdpolicy; + dns_name_copy(domain, fctx->fwdname, NULL); + } if (fctx->fwdpolicy != dns_fwdpolicy_only) { /* @@ -6226,6 +6234,15 @@ mark_related(dns_name_t *name, dns_rdataset_t *rdataset, rdataset->attributes |= DNS_RDATASETATTR_EXTERNAL; } +static inline bool +name_external(const dns_name_t *name, fetchctx_t *fctx) { + if (ISFORWARDER(fctx->addrinfo)) { + return (!dns_name_issubdomain(name, fctx->fwdname)); + } + + return (!dns_name_issubdomain(name, &fctx->domain)); +} + static isc_result_t check_section(void *arg, dns_name_t *addname, dns_rdatatype_t type, dns_section_t section) @@ -6254,7 +6271,7 @@ check_section(void *arg, dns_name_t *addname, dns_rdatatype_t type, result = dns_message_findname(rmessage, section, addname, dns_rdatatype_any, 0, &name, NULL); if (result == ISC_R_SUCCESS) { - external = !dns_name_issubdomain(name, &fctx->domain); + external = name_external(name, fctx); if (type == dns_rdatatype_a) { for (rdataset = ISC_LIST_HEAD(name->list); rdataset != NULL; @@ -7136,6 +7153,13 @@ answer_response(fetchctx_t *fctx, dns_message_t *message) { break; case dns_namereln_subdomain: + /* + * Don't accept DNAME from parent namespace. + */ + if (name_external(name, fctx)) { + continue; + } + /* * In-scope DNAME records must have at least * as many labels as the domain being queried. @@ -7371,11 +7395,9 @@ answer_response(fetchctx_t *fctx, dns_message_t *message) { */ result = dns_message_firstname(message, DNS_SECTION_AUTHORITY); while (!done && result == ISC_R_SUCCESS) { - bool external; name = NULL; dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name); - external = !dns_name_issubdomain(name, &fctx->domain); - if (!external) { + if (!name_external(name, fctx)) { /* * We expect to find NS or SIG NS rdatasets, and * nothing else. From 590f8698fc876d6d72f75cf35359e7546c3af972 Mon Sep 17 00:00:00 2001 From: Mark Andrews Date: Fri, 21 Jan 2022 10:52:02 +1100 Subject: [PATCH 2/7] Check that the forward declaration is unchanged and not overridden If we are using a fowarder, in addition to checking that names to be cached are subdomains of the forwarded namespace, we must also check that there are no subsidiary forwarded namespaces which would take precedence. To be safe, we don't cache any responses if the forwarding configuration has changed since the query was sent. --- lib/dns/resolver.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/dns/resolver.c b/lib/dns/resolver.c index 98c169e4d2..ad7d875329 100644 --- a/lib/dns/resolver.c +++ b/lib/dns/resolver.c @@ -6237,7 +6237,31 @@ mark_related(dns_name_t *name, dns_rdataset_t *rdataset, static inline bool name_external(const dns_name_t *name, fetchctx_t *fctx) { if (ISFORWARDER(fctx->addrinfo)) { - return (!dns_name_issubdomain(name, fctx->fwdname)); + isc_result_t result; + dns_fixedname_t fixed; + dns_forwarders_t *forwarders = NULL; + dns_name_t *fname; + + if (!dns_name_issubdomain(name, fctx->fwdname)) { + return (true); + } + + /* + * Is there a child forwarder declaration that is better? + * This lookup should always succeed if the configuration + * has not changed. + */ + fname = dns_fixedname_initname(&fixed); + result = dns_fwdtable_find(fctx->res->view->fwdtable, name, fname, + &forwarders); + if (result == ISC_R_SUCCESS) { + return (!dns_name_equal(fname, fctx->fwdname)); + } + + /* + * Play it safe if the configuration has changed. + */ + return (true); } return (!dns_name_issubdomain(name, &fctx->domain)); From 4a144fae16e70517be894a971cef1d085ee68ebe Mon Sep 17 00:00:00 2001 From: Mark Andrews Date: Tue, 1 Mar 2022 10:02:55 +1100 Subject: [PATCH 3/7] Check cached names for possible "forward only" clause When caching additional and glue data *not* from a forwarder, we must check that there is no "forward only" clause covering the owner name that would take precedence. Such names would normally be allowed by baliwick rules, but a "forward only" zone introduces a new baliwick scope. --- lib/dns/resolver.c | 79 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 58 insertions(+), 21 deletions(-) diff --git a/lib/dns/resolver.c b/lib/dns/resolver.c index ad7d875329..6ef3547bcd 100644 --- a/lib/dns/resolver.c +++ b/lib/dns/resolver.c @@ -6234,37 +6234,74 @@ mark_related(dns_name_t *name, dns_rdataset_t *rdataset, rdataset->attributes |= DNS_RDATASETATTR_EXTERNAL; } +/* + * Returns true if 'name' is external to the namespace for which + * the server being queried can answer, either because it's not a + * subdomain or because it's below a forward declaration. + */ static inline bool -name_external(const dns_name_t *name, fetchctx_t *fctx) { +name_external(dns_name_t *name, dns_rdatatype_t type, fetchctx_t *fctx) { + isc_result_t result; + dns_forwarders_t *forwarders = NULL; + dns_fixedname_t fixed; + dns_name_t *fname = dns_fixedname_initname(&fixed); + dns_name_t suffix; + unsigned int labels; + + /* + * The name is outside the queried namespace. + */ + if (!dns_name_issubdomain(name, ISFORWARDER(fctx->addrinfo) + ? fctx->fwdname + : &fctx->domain)) + { + return (true); + } + + /* + * If the record lives in the parent zone, adjust the name so we + * look for the correct forward clause. + */ + labels = dns_name_countlabels(name); + if (dns_rdatatype_atparent(type) && labels > 1U) { + dns_name_init(&suffix, NULL); + dns_name_getlabelsequence(name, 1, labels - 1, &suffix); + name = &suffix; + } + + /* + * Look for a forward declaration below 'name'. + */ + result = dns_fwdtable_find2(fctx->res->view->fwdtable, name, fname, + &forwarders); + if (ISFORWARDER(fctx->addrinfo)) { - isc_result_t result; - dns_fixedname_t fixed; - dns_forwarders_t *forwarders = NULL; - dns_name_t *fname; - - if (!dns_name_issubdomain(name, fctx->fwdname)) { - return (true); - } - /* - * Is there a child forwarder declaration that is better? - * This lookup should always succeed if the configuration - * has not changed. + * See if the forwarder declaration is better. */ - fname = dns_fixedname_initname(&fixed); - result = dns_fwdtable_find(fctx->res->view->fwdtable, name, fname, - &forwarders); if (result == ISC_R_SUCCESS) { return (!dns_name_equal(fname, fctx->fwdname)); } /* - * Play it safe if the configuration has changed. + * If the lookup failed, the configuration must have + * changed: play it safe and don't cache. */ return (true); } - return (!dns_name_issubdomain(name, &fctx->domain)); + /* + * If name is covered by 'forward only' then we can't + * cache this repsonse. + */ + if (result == ISC_R_SUCCESS && + forwarders->fwdpolicy == dns_fwdpolicy_only && + !ISC_LIST_EMPTY(forwarders->fwdrs)) + { + return (true); + } + + return (false); } static isc_result_t @@ -6295,7 +6332,7 @@ check_section(void *arg, dns_name_t *addname, dns_rdatatype_t type, result = dns_message_findname(rmessage, section, addname, dns_rdatatype_any, 0, &name, NULL); if (result == ISC_R_SUCCESS) { - external = name_external(name, fctx); + external = name_external(name, type, fctx); if (type == dns_rdatatype_a) { for (rdataset = ISC_LIST_HEAD(name->list); rdataset != NULL; @@ -7180,7 +7217,7 @@ answer_response(fetchctx_t *fctx, dns_message_t *message) { /* * Don't accept DNAME from parent namespace. */ - if (name_external(name, fctx)) { + if (name_external(name, dns_rdatatype_dname, fctx)) { continue; } @@ -7421,7 +7458,7 @@ answer_response(fetchctx_t *fctx, dns_message_t *message) { while (!done && result == ISC_R_SUCCESS) { name = NULL; dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name); - if (!name_external(name, fctx)) { + if (!name_external(name, dns_rdatatype_ns, fctx)) { /* * We expect to find NS or SIG NS rdatasets, and * nothing else. From 42f8c538d3fb9d075b98d82688aeb71621798754 Mon Sep 17 00:00:00 2001 From: Mark Andrews Date: Wed, 2 Feb 2022 15:13:39 +1100 Subject: [PATCH 4/7] Look for zones deeper than the current domain or forward name When caching glue, we need to ensure that there is no closer source of truth for the name. If the owner name for the glue record would be answered by a locally configured zone, do not cache. --- lib/dns/resolver.c | 64 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 16 deletions(-) diff --git a/lib/dns/resolver.c b/lib/dns/resolver.c index 6ef3547bcd..74c477e401 100644 --- a/lib/dns/resolver.c +++ b/lib/dns/resolver.c @@ -63,6 +63,7 @@ #include #include #include +#include #ifdef WANT_QUERYTRACE #define RTRACE(m) isc_log_write(dns_lctx, \ @@ -6237,38 +6238,71 @@ mark_related(dns_name_t *name, dns_rdataset_t *rdataset, /* * Returns true if 'name' is external to the namespace for which * the server being queried can answer, either because it's not a - * subdomain or because it's below a forward declaration. + * subdomain or because it's below a forward declaration or a + * locally served zone. */ static inline bool name_external(dns_name_t *name, dns_rdatatype_t type, fetchctx_t *fctx) { isc_result_t result; dns_forwarders_t *forwarders = NULL; - dns_fixedname_t fixed; + dns_fixedname_t fixed, zfixed; dns_name_t *fname = dns_fixedname_initname(&fixed); + dns_name_t *zfname = dns_fixedname_initname(&zfixed); + dns_name_t *apex = NULL; dns_name_t suffix; + dns_zone_t *zone = NULL; unsigned int labels; + dns_namereln_t rel; + + apex = ISFORWARDER(fctx->addrinfo) ? fctx->fwdname : &fctx->domain; /* * The name is outside the queried namespace. */ - if (!dns_name_issubdomain(name, ISFORWARDER(fctx->addrinfo) - ? fctx->fwdname - : &fctx->domain)) - { + rel = dns_name_fullcompare(name, apex, &(int){ 0 }, + &(unsigned int){ 0U }); + if (rel != dns_namereln_subdomain && rel != dns_namereln_equal) { return (true); } /* * If the record lives in the parent zone, adjust the name so we - * look for the correct forward clause. + * look for the correct zone or forward clause. */ labels = dns_name_countlabels(name); if (dns_rdatatype_atparent(type) && labels > 1U) { dns_name_init(&suffix, NULL); dns_name_getlabelsequence(name, 1, labels - 1, &suffix); name = &suffix; + } else if (rel == dns_namereln_equal) { + /* If 'name' is 'apex', no further checking is needed. */ + return (false); } + /* + * If there is a locally served zone between 'apex' and 'name' + * then don't cache. + */ + LOCK(&fctx->res->view->lock); + if (fctx->res->view->zonetable != NULL) { + unsigned int options = DNS_ZTFIND_NOEXACT; + result = dns_zt_find(fctx->res->view->zonetable, name, options, + zfname, &zone); + if (zone != NULL) { + dns_zone_detach(&zone); + } + if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) { + if (dns_name_fullcompare(zfname, apex, &(int){ 0 }, + &(unsigned int){ 0U }) == + dns_namereln_subdomain) + { + UNLOCK(&fctx->res->view->lock); + return (true); + } + } + } + UNLOCK(&fctx->res->view->lock); + /* * Look for a forward declaration below 'name'. */ @@ -6288,16 +6322,14 @@ name_external(dns_name_t *name, dns_rdatatype_t type, fetchctx_t *fctx) { * changed: play it safe and don't cache. */ return (true); - } - - /* - * If name is covered by 'forward only' then we can't - * cache this repsonse. - */ - if (result == ISC_R_SUCCESS && - forwarders->fwdpolicy == dns_fwdpolicy_only && - !ISC_LIST_EMPTY(forwarders->fwdrs)) + } else if (result == ISC_R_SUCCESS && + forwarders->fwdpolicy == dns_fwdpolicy_only && + !ISC_LIST_EMPTY(forwarders->fwdrs)) { + /* + * If 'name' is covered by a 'forward only' clause then we + * can't cache this repsonse. + */ return (true); } From 5d7905fc546982975a2d29ac1861e0773467f25a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20=C5=A0pa=C4=8Dek?= Date: Fri, 25 Feb 2022 15:14:10 +0100 Subject: [PATCH 5/7] Add CHANGES note for [GL #2950] --- CHANGES | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGES b/CHANGES index 0ebef39960..00cd9a619c 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,8 @@ +5817. [security] The rules for acceptance of records into the cache + have been tightened to prevent the possibility of + poisoning if forwarders send records outside + the configured bailiwick. (CVE-2021-25220) [GL #2950] + --- 9.11.36 released --- 5736. [security] The "lame-ttl" option is now forcibly set to 0. This From 561f87d9e690ae969a92285c82a1372b5777423d Mon Sep 17 00:00:00 2001 From: Mark Andrews Date: Tue, 1 Mar 2022 10:13:03 +1100 Subject: [PATCH 6/7] Add Release Note for [GL #2950] --- doc/arm/notes-9.11.37.xml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/doc/arm/notes-9.11.37.xml b/doc/arm/notes-9.11.37.xml index 488a2355a6..fee6259bb7 100644 --- a/doc/arm/notes-9.11.37.xml +++ b/doc/arm/notes-9.11.37.xml @@ -15,7 +15,15 @@ - None. + The rules for acceptance of records into the cache have been tightened + to prevent the possibility of poisoning if forwarders send records + outside the configured bailiwick. (CVE-2021-25220) + + + ISC would like to thank Xiang Li, Baojun Liu, and Chaoyi Lu from + Network and Information Security Lab, Tsinghua University, and + Changgen Zou from Qi An Xin Group Corp. for bringing this + vulnerability to our attention. [GL #2950] From d4b1efbcbd4dfb8c6ef303968992440c5bdeed15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20=C5=A0pa=C4=8Dek?= Date: Thu, 3 Mar 2022 12:21:37 +0100 Subject: [PATCH 7/7] Avoid use of compound literals Compound literals are not used in BIND 9.11, in order to ensure backward compatibility with ancient compilers. Rework the relevant parts of the BIND 9.11 backport of the CVE-2021-25220 fix so that compound literals are not used. --- lib/dns/resolver.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/dns/resolver.c b/lib/dns/resolver.c index 74c477e401..8ae9a993bb 100644 --- a/lib/dns/resolver.c +++ b/lib/dns/resolver.c @@ -6253,14 +6253,19 @@ name_external(dns_name_t *name, dns_rdatatype_t type, fetchctx_t *fctx) { dns_zone_t *zone = NULL; unsigned int labels; dns_namereln_t rel; + /* + * The following two variables do not influence code flow; they are + * only necessary for calling dns_name_fullcompare(). + */ + int _orderp = 0; + unsigned int _nlabelsp = 0; apex = ISFORWARDER(fctx->addrinfo) ? fctx->fwdname : &fctx->domain; /* * The name is outside the queried namespace. */ - rel = dns_name_fullcompare(name, apex, &(int){ 0 }, - &(unsigned int){ 0U }); + rel = dns_name_fullcompare(name, apex, &_orderp, &_nlabelsp); if (rel != dns_namereln_subdomain && rel != dns_namereln_equal) { return (true); } @@ -6292,8 +6297,8 @@ name_external(dns_name_t *name, dns_rdatatype_t type, fetchctx_t *fctx) { dns_zone_detach(&zone); } if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) { - if (dns_name_fullcompare(zfname, apex, &(int){ 0 }, - &(unsigned int){ 0U }) == + if (dns_name_fullcompare(zfname, apex, &_orderp, + &_nlabelsp) == dns_namereln_subdomain) { UNLOCK(&fctx->res->view->lock);