Blame SOURCES/bind-9.16-CVE-2022-3080.patch

361a11
From 3bcd32572504ac9b92e3c6ec1e2cee3df3b68309 Mon Sep 17 00:00:00 2001
361a11
From: Petr Mensik <pemensik@redhat.com>
361a11
Date: Tue, 20 Sep 2022 11:34:42 +0200
361a11
Subject: [PATCH 2/4] Fix CVE-2022-3080
361a11
361a11
5960.	[security]	Fix serve-stale crash that could happen when
361a11
			stale-answer-client-timeout was set to 0 and there was
361a11
			a stale CNAME in the cache for an incoming query.
361a11
			(CVE-2022-3080) [GL #3517]
361a11
---
361a11
 lib/ns/include/ns/query.h |  1 +
361a11
 lib/ns/query.c            | 42 ++++++++++++++++++++++++---------------
361a11
 2 files changed, 27 insertions(+), 16 deletions(-)
361a11
361a11
diff --git a/lib/ns/include/ns/query.h b/lib/ns/include/ns/query.h
361a11
index 4d48cf6..34b3070 100644
361a11
--- a/lib/ns/include/ns/query.h
361a11
+++ b/lib/ns/include/ns/query.h
361a11
@@ -145,6 +145,7 @@ struct query_ctx {
361a11
 	bool authoritative;		    /* authoritative query? */
361a11
 	bool want_restart;		    /* CNAME chain or other
361a11
 					     * restart needed */
361a11
+	bool		refresh_rrset;	    /* stale RRset refresh needed */
361a11
 	bool		need_wildcardproof; /* wildcard proof needed */
361a11
 	bool		nxrewrite;	    /* negative answer from RPZ */
361a11
 	bool		findcoveringnsec;   /* lookup covering NSEC */
361a11
diff --git a/lib/ns/query.c b/lib/ns/query.c
361a11
index 249321c..a450cb7 100644
361a11
--- a/lib/ns/query.c
361a11
+++ b/lib/ns/query.c
361a11
@@ -5686,7 +5686,6 @@ query_lookup(query_ctx_t *qctx) {
361a11
 	bool dbfind_stale = false;
361a11
 	bool stale_timeout = false;
361a11
 	bool stale_found = false;
361a11
-	bool refresh_rrset = false;
361a11
 	bool stale_refresh_window = false;
361a11
 
361a11
 	CCTRACE(ISC_LOG_DEBUG(3), "query_lookup");
361a11
@@ -5868,8 +5867,7 @@ query_lookup(query_ctx_t *qctx) {
361a11
 					"%s stale answer used, an attempt to "
361a11
 					"refresh the RRset will still be made",
361a11
 					namebuf);
361a11
-				refresh_rrset = STALE(qctx->rdataset);
361a11
-				qctx->client->nodetach = refresh_rrset;
361a11
+				qctx->refresh_rrset = STALE(qctx->rdataset);
361a11
 			}
361a11
 		} else {
361a11
 			/*
361a11
@@ -5907,17 +5905,6 @@ query_lookup(query_ctx_t *qctx) {
361a11
 
361a11
 	result = query_gotanswer(qctx, result);
361a11
 
361a11
-	if (refresh_rrset) {
361a11
-		/*
361a11
-		 * If we reached this point then it means that we have found a
361a11
-		 * stale RRset entry in cache and BIND is configured to allow
361a11
-		 * queries to be answered with stale data if no active RRset
361a11
-		 * is available, i.e. "stale-anwer-client-timeout 0". But, we
361a11
-		 * still need to refresh the RRset.
361a11
-		 */
361a11
-		query_refresh_rrset(qctx);
361a11
-	}
361a11
-
361a11
 cleanup:
361a11
 	return (result);
361a11
 }
361a11
@@ -7737,11 +7724,14 @@ query_addanswer(query_ctx_t *qctx) {
361a11
 
361a11
 	/*
361a11
 	 * On normal lookups, clear any rdatasets that were added on a
361a11
-	 * lookup due to stale-answer-client-timeout.
361a11
+	 * lookup due to stale-answer-client-timeout. Do not clear if we
361a11
+	 * are going to refresh the RRset, because the stale contents are
361a11
+	 * prioritized.
361a11
 	 */
361a11
 	if (QUERY_STALEOK(&qctx->client->query) &&
361a11
-	    !QUERY_STALETIMEOUT(&qctx->client->query))
361a11
+	    !QUERY_STALETIMEOUT(&qctx->client->query) && !qctx->refresh_rrset)
361a11
 	{
361a11
+		CCTRACE(ISC_LOG_DEBUG(3), "query_clear_stale");
361a11
 		query_clear_stale(qctx->client);
361a11
 		/*
361a11
 		 * We can clear the attribute to prevent redundant clearing
361a11
@@ -11457,9 +11447,29 @@ ns_query_done(query_ctx_t *qctx) {
361a11
 	/*
361a11
 	 * Client may have been detached after query_send(), so
361a11
 	 * we test and store the flag state here, for safety.
361a11
+	 * If we are refreshing the RRSet, we must not detach from the client
361a11
+	 * in the query_send(), so we need to override the flag.
361a11
 	 */
361a11
+	if (qctx->refresh_rrset) {
361a11
+		qctx->client->nodetach = true;
361a11
+	}
361a11
 	nodetach = qctx->client->nodetach;
361a11
 	query_send(qctx->client);
361a11
+
361a11
+	if (qctx->refresh_rrset) {
361a11
+		/*
361a11
+		 * If we reached this point then it means that we have found a
361a11
+		 * stale RRset entry in cache and BIND is configured to allow
361a11
+		 * queries to be answered with stale data if no active RRset
361a11
+		 * is available, i.e. "stale-anwer-client-timeout 0". But, we
361a11
+		 * still need to refresh the RRset. To prevent adding duplicate
361a11
+		 * RRsets, clear the RRsets from the message before doing the
361a11
+		 * refresh.
361a11
+		 */
361a11
+		message_clearrdataset(qctx->client->message, 0);
361a11
+		query_refresh_rrset(qctx);
361a11
+	}
361a11
+
361a11
 	if (!nodetach) {
361a11
 		qctx->detach_client = true;
361a11
 	}
361a11
-- 
361a11
2.37.3
361a11